Skip to content

Commit 9940516

Browse files
mstsirkindavem330
authored andcommitted
tun: socket filter support
This patch adds Linux Socket Filter support to tun driver. Signed-off-by: Michael S. Tsirkin <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 5ff3f07 commit 9940516

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

drivers/net/tun.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include <linux/crc32.h>
6262
#include <linux/nsproxy.h>
6363
#include <linux/virtio_net.h>
64+
#include <linux/rcupdate.h>
6465
#include <net/net_namespace.h>
6566
#include <net/netns/generic.h>
6667
#include <net/rtnetlink.h>
@@ -366,6 +367,10 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
366367
if (!check_filter(&tun->txflt, skb))
367368
goto drop;
368369

370+
if (tun->socket.sk->sk_filter &&
371+
sk_filter(tun->socket.sk, skb))
372+
goto drop;
373+
369374
if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
370375
if (!(tun->flags & TUN_ONE_QUEUE)) {
371376
/* Normal queueing mode. */
@@ -1162,6 +1167,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
11621167
struct tun_file *tfile = file->private_data;
11631168
struct tun_struct *tun;
11641169
void __user* argp = (void __user*)arg;
1170+
struct sock_fprog fprog;
11651171
struct ifreq ifr;
11661172
int sndbuf;
11671173
int ret;
@@ -1309,6 +1315,26 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
13091315
tun->socket.sk->sk_sndbuf = sndbuf;
13101316
break;
13111317

1318+
case TUNATTACHFILTER:
1319+
/* Can be set only for TAPs */
1320+
ret = -EINVAL;
1321+
if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1322+
break;
1323+
ret = -EFAULT;
1324+
if (copy_from_user(&fprog, argp, sizeof(fprog)))
1325+
break;
1326+
1327+
ret = sk_attach_filter(&fprog, tun->socket.sk);
1328+
break;
1329+
1330+
case TUNDETACHFILTER:
1331+
/* Can be set only for TAPs */
1332+
ret = -EINVAL;
1333+
if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
1334+
break;
1335+
ret = sk_detach_filter(tun->socket.sk);
1336+
break;
1337+
13121338
default:
13131339
ret = -EINVAL;
13141340
break;

include/linux/if_tun.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <linux/types.h>
2020
#include <linux/if_ether.h>
21+
#include <linux/filter.h>
2122

2223
/* Read queue size */
2324
#define TUN_READQ_SIZE 500
@@ -48,6 +49,8 @@
4849
#define TUNGETIFF _IOR('T', 210, unsigned int)
4950
#define TUNGETSNDBUF _IOR('T', 211, int)
5051
#define TUNSETSNDBUF _IOW('T', 212, int)
52+
#define TUNATTACHFILTER _IOW('T', 213, struct sock_fprog)
53+
#define TUNDETACHFILTER _IOW('T', 214, struct sock_fprog)
5154

5255
/* TUNSETIFF ifr flags */
5356
#define IFF_TUN 0x0001

0 commit comments

Comments
 (0)