Commit 33dccbb0 authored by Herbert Xu's avatar Herbert Xu Committed by David S. Miller

tun: Limit amount of queued packets per device

Unlike a normal socket path, the tuntap device send path does
not have any accounting.  This means that the user-space sender
may be able to pin down arbitrary amounts of kernel memory by
continuing to send data to an end-point that is congested.

Even when this isn't an issue because of limited queueing at
most end points, this can also be a problem because its only
response to congestion is packet loss.  That is, when those
local queues at the end-point fills up, the tuntap device will
start wasting system time because it will continue to send
data there which simply gets dropped straight away.

Of course one could argue that everybody should do congestion
control end-to-end, unfortunately there are people in this world
still hooked on UDP, and they don't appear to be going away
anywhere fast.  In fact, we've always helped them by performing
accounting in our UDP code, the sole purpose of which is to
provide congestion feedback other than through packet loss.

This patch attempts to apply the same bandaid to the tuntap device.
It creates a pseudo-socket object which is used to account our
packets just as a normal socket does for UDP.  Of course things
are a little complex because we're actually reinjecting traffic
back into the stack rather than out of the stack.

The stack complexities however should have been resolved by preceding
patches.  So this one can simply start using skb_set_owner_w.

For now the accounting is essentially disabled by default for
backwards compatibility.  In particular, we set the cap to INT_MAX.
This is so that existing applications don't get confused by the
sudden arrival EAGAIN errors.

In future we may wish (or be forced to) do this by default.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 4cc7f68d
...@@ -64,6 +64,7 @@ ...@@ -64,6 +64,7 @@
#include <net/net_namespace.h> #include <net/net_namespace.h>
#include <net/netns/generic.h> #include <net/netns/generic.h>
#include <net/rtnetlink.h> #include <net/rtnetlink.h>
#include <net/sock.h>
#include <asm/system.h> #include <asm/system.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
...@@ -95,6 +96,8 @@ struct tun_file { ...@@ -95,6 +96,8 @@ struct tun_file {
wait_queue_head_t read_wait; wait_queue_head_t read_wait;
}; };
struct tun_sock;
struct tun_struct { struct tun_struct {
struct tun_file *tfile; struct tun_file *tfile;
unsigned int flags; unsigned int flags;
...@@ -107,12 +110,24 @@ struct tun_struct { ...@@ -107,12 +110,24 @@ struct tun_struct {
struct fasync_struct *fasync; struct fasync_struct *fasync;
struct tap_filter txflt; struct tap_filter txflt;
struct sock *sk;
struct socket socket;
#ifdef TUN_DEBUG #ifdef TUN_DEBUG
int debug; int debug;
#endif #endif
}; };
struct tun_sock {
struct sock sk;
struct tun_struct *tun;
};
static inline struct tun_sock *tun_sk(struct sock *sk)
{
return container_of(sk, struct tun_sock, sk);
}
static int tun_attach(struct tun_struct *tun, struct file *file) static int tun_attach(struct tun_struct *tun, struct file *file)
{ {
struct tun_file *tfile = file->private_data; struct tun_file *tfile = file->private_data;
...@@ -461,7 +476,8 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait) ...@@ -461,7 +476,8 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
{ {
struct tun_file *tfile = file->private_data; struct tun_file *tfile = file->private_data;
struct tun_struct *tun = __tun_get(tfile); struct tun_struct *tun = __tun_get(tfile);
unsigned int mask = POLLOUT | POLLWRNORM; struct sock *sk = tun->sk;
unsigned int mask = 0;
if (!tun) if (!tun)
return POLLERR; return POLLERR;
...@@ -473,6 +489,11 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait) ...@@ -473,6 +489,11 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
if (!skb_queue_empty(&tun->readq)) if (!skb_queue_empty(&tun->readq))
mask |= POLLIN | POLLRDNORM; mask |= POLLIN | POLLRDNORM;
if (sock_writeable(sk) ||
(!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
sock_writeable(sk)))
mask |= POLLOUT | POLLWRNORM;
if (tun->dev->reg_state != NETREG_REGISTERED) if (tun->dev->reg_state != NETREG_REGISTERED)
mask = POLLERR; mask = POLLERR;
...@@ -482,66 +503,35 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait) ...@@ -482,66 +503,35 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
/* prepad is the amount to reserve at front. len is length after that. /* prepad is the amount to reserve at front. len is length after that.
* linear is a hint as to how much to copy (usually headers). */ * linear is a hint as to how much to copy (usually headers). */
static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear, static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
gfp_t gfp) size_t prepad, size_t len,
size_t linear, int noblock)
{ {
struct sock *sk = tun->sk;
struct sk_buff *skb; struct sk_buff *skb;
unsigned int i; int err;
skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN);
if (skb) {
skb_reserve(skb, prepad);
skb_put(skb, len);
return skb;
}
/* Under a page? Don't bother with paged skb. */ /* Under a page? Don't bother with paged skb. */
if (prepad + len < PAGE_SIZE) if (prepad + len < PAGE_SIZE)
return NULL; linear = len;
/* Start with a normal skb, and add pages. */ skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
skb = alloc_skb(prepad + linear, gfp); &err);
if (!skb) if (!skb)
return NULL; return ERR_PTR(err);
skb_reserve(skb, prepad); skb_reserve(skb, prepad);
skb_put(skb, linear); skb_put(skb, linear);
skb->data_len = len - linear;
len -= linear; skb->len += len - linear;
for (i = 0; i < MAX_SKB_FRAGS; i++) {
skb_frag_t *f = &skb_shinfo(skb)->frags[i];
f->page = alloc_page(gfp|__GFP_ZERO);
if (!f->page)
break;
f->page_offset = 0;
f->size = PAGE_SIZE;
skb->data_len += PAGE_SIZE;
skb->len += PAGE_SIZE;
skb->truesize += PAGE_SIZE;
skb_shinfo(skb)->nr_frags++;
if (len < PAGE_SIZE) {
len = 0;
break;
}
len -= PAGE_SIZE;
}
/* Too large, or alloc fail? */
if (unlikely(len)) {
kfree_skb(skb);
skb = NULL;
}
return skb; return skb;
} }
/* Get packet from user space buffer */ /* Get packet from user space buffer */
static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count) static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
struct iovec *iv, size_t count,
int noblock)
{ {
struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
struct sk_buff *skb; struct sk_buff *skb;
...@@ -573,9 +563,11 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, ...@@ -573,9 +563,11 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv,
return -EINVAL; return -EINVAL;
} }
if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) { skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
if (IS_ERR(skb)) {
if (PTR_ERR(skb) != -EAGAIN)
tun->dev->stats.rx_dropped++; tun->dev->stats.rx_dropped++;
return -ENOMEM; return PTR_ERR(skb);
} }
if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) { if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) {
...@@ -661,7 +653,8 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, ...@@ -661,7 +653,8 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv,
static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
unsigned long count, loff_t pos) unsigned long count, loff_t pos)
{ {
struct tun_struct *tun = tun_get(iocb->ki_filp); struct file *file = iocb->ki_filp;
struct tun_struct *tun = file->private_data;
ssize_t result; ssize_t result;
if (!tun) if (!tun)
...@@ -669,7 +662,8 @@ static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, ...@@ -669,7 +662,8 @@ static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count); DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count);
result = tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count)); result = tun_get_user(tun, (struct iovec *)iv, iov_length(iv, count),
file->f_flags & O_NONBLOCK);
tun_put(tun); tun_put(tun);
return result; return result;
...@@ -828,11 +822,40 @@ static struct rtnl_link_ops tun_link_ops __read_mostly = { ...@@ -828,11 +822,40 @@ static struct rtnl_link_ops tun_link_ops __read_mostly = {
.validate = tun_validate, .validate = tun_validate,
}; };
static void tun_sock_write_space(struct sock *sk)
{
struct tun_struct *tun;
if (!sock_writeable(sk))
return;
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
wake_up_interruptible_sync(sk->sk_sleep);
if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
return;
tun = container_of(sk, struct tun_sock, sk)->tun;
kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
}
static void tun_sock_destruct(struct sock *sk)
{
dev_put(container_of(sk, struct tun_sock, sk)->tun->dev);
}
static struct proto tun_proto = {
.name = "tun",
.owner = THIS_MODULE,
.obj_size = sizeof(struct tun_sock),
};
static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
{ {
struct sock *sk;
struct tun_struct *tun; struct tun_struct *tun;
struct net_device *dev; struct net_device *dev;
struct tun_file *tfile = file->private_data;
int err; int err;
dev = __dev_get_by_name(net, ifr->ifr_name); dev = __dev_get_by_name(net, ifr->ifr_name);
...@@ -885,14 +908,31 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) ...@@ -885,14 +908,31 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
tun->flags = flags; tun->flags = flags;
tun->txflt.count = 0; tun->txflt.count = 0;
err = -ENOMEM;
sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
if (!sk)
goto err_free_dev;
/* This ref count is for tun->sk. */
dev_hold(dev);
sock_init_data(&tun->socket, sk);
sk->sk_write_space = tun_sock_write_space;
sk->sk_destruct = tun_sock_destruct;
sk->sk_sndbuf = INT_MAX;
sk->sk_sleep = &tfile->read_wait;
tun->sk = sk;
container_of(sk, struct tun_sock, sk)->tun = tun;
tun_net_init(dev); tun_net_init(dev);
if (strchr(dev->name, '%')) { if (strchr(dev->name, '%')) {
err = dev_alloc_name(dev, dev->name); err = dev_alloc_name(dev, dev->name);
if (err < 0) if (err < 0)
goto err_free_dev; goto err_free_sk;
} }
err = -EINVAL;
err = register_netdevice(tun->dev); err = register_netdevice(tun->dev);
if (err < 0) if (err < 0)
goto err_free_dev; goto err_free_dev;
...@@ -928,6 +968,8 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) ...@@ -928,6 +968,8 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
strcpy(ifr->ifr_name, tun->dev->name); strcpy(ifr->ifr_name, tun->dev->name);
return 0; return 0;
err_free_sk:
sock_put(sk);
err_free_dev: err_free_dev:
free_netdev(dev); free_netdev(dev);
failed: failed:
...@@ -1012,6 +1054,7 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file, ...@@ -1012,6 +1054,7 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file,
struct tun_struct *tun; struct tun_struct *tun;
void __user* argp = (void __user*)arg; void __user* argp = (void __user*)arg;
struct ifreq ifr; struct ifreq ifr;
int sndbuf;
int ret; int ret;
if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
...@@ -1151,6 +1194,22 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file, ...@@ -1151,6 +1194,22 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file,
ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
rtnl_unlock(); rtnl_unlock();
break; break;
case TUNGETSNDBUF:
sndbuf = tun->sk->sk_sndbuf;
if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
ret = -EFAULT;
break;
case TUNSETSNDBUF:
if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
ret = -EFAULT;
break;
}
tun->sk->sk_sndbuf = sndbuf;
break;
default: default:
ret = -EINVAL; ret = -EINVAL;
break; break;
...@@ -1218,8 +1277,10 @@ static int tun_chr_close(struct inode *inode, struct file *file) ...@@ -1218,8 +1277,10 @@ static int tun_chr_close(struct inode *inode, struct file *file)
__tun_detach(tun); __tun_detach(tun);
/* If desireable, unregister the netdevice. */ /* If desireable, unregister the netdevice. */
if (!(tun->flags & TUN_PERSIST)) if (!(tun->flags & TUN_PERSIST)) {
sock_put(tun->sk);
unregister_netdevice(tun->dev); unregister_netdevice(tun->dev);
}
rtnl_unlock(); rtnl_unlock();
} }
......
...@@ -1988,6 +1988,8 @@ COMPATIBLE_IOCTL(TUNSETGROUP) ...@@ -1988,6 +1988,8 @@ COMPATIBLE_IOCTL(TUNSETGROUP)
COMPATIBLE_IOCTL(TUNGETFEATURES) COMPATIBLE_IOCTL(TUNGETFEATURES)
COMPATIBLE_IOCTL(TUNSETOFFLOAD) COMPATIBLE_IOCTL(TUNSETOFFLOAD)
COMPATIBLE_IOCTL(TUNSETTXFILTER) COMPATIBLE_IOCTL(TUNSETTXFILTER)
COMPATIBLE_IOCTL(TUNGETSNDBUF)
COMPATIBLE_IOCTL(TUNSETSNDBUF)
/* Big V */ /* Big V */
COMPATIBLE_IOCTL(VT_SETMODE) COMPATIBLE_IOCTL(VT_SETMODE)
COMPATIBLE_IOCTL(VT_GETMODE) COMPATIBLE_IOCTL(VT_GETMODE)
......
...@@ -46,6 +46,8 @@ ...@@ -46,6 +46,8 @@
#define TUNSETOFFLOAD _IOW('T', 208, unsigned int) #define TUNSETOFFLOAD _IOW('T', 208, unsigned int)
#define TUNSETTXFILTER _IOW('T', 209, unsigned int) #define TUNSETTXFILTER _IOW('T', 209, unsigned int)
#define TUNGETIFF _IOR('T', 210, unsigned int) #define TUNGETIFF _IOR('T', 210, unsigned int)
#define TUNGETSNDBUF _IOR('T', 211, int)
#define TUNSETSNDBUF _IOW('T', 212, int)
/* TUNSETIFF ifr flags */ /* TUNSETIFF ifr flags */
#define IFF_TUN 0x0001 #define IFF_TUN 0x0001
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment