Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * TUN - Universal TUN/TAP device driver. |
| 3 | * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $ |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * Changes: |
| 20 | * |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 21 | * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 |
| 22 | * Add TUNSETLINK ioctl to set the link encapsulation |
| 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * Mark Smith <markzzzsmith@yahoo.com.au> |
Joe Perches | 344dc8e | 2012-07-12 19:33:09 +0000 | [diff] [blame] | 25 | * Use eth_random_addr() for tap MAC address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * |
| 27 | * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 |
| 28 | * Fixes in packet dropping, queue length setting and queue wakeup. |
| 29 | * Increased default tx queue length. |
| 30 | * Added ethtool API. |
| 31 | * Minor cleanups |
| 32 | * |
| 33 | * Daniel Podlejski <underley@underley.eu.org> |
| 34 | * Modifications for 2.3.99-pre5 kernel. |
| 35 | */ |
| 36 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 37 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #define DRV_NAME "tun" |
| 40 | #define DRV_VERSION "1.6" |
| 41 | #define DRV_DESCRIPTION "Universal TUN/TAP device driver" |
| 42 | #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/module.h> |
| 45 | #include <linux/errno.h> |
| 46 | #include <linux/kernel.h> |
| 47 | #include <linux/major.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/poll.h> |
| 50 | #include <linux/fcntl.h> |
| 51 | #include <linux/init.h> |
| 52 | #include <linux/skbuff.h> |
| 53 | #include <linux/netdevice.h> |
| 54 | #include <linux/etherdevice.h> |
| 55 | #include <linux/miscdevice.h> |
| 56 | #include <linux/ethtool.h> |
| 57 | #include <linux/rtnetlink.h> |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 58 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/if.h> |
| 60 | #include <linux/if_arp.h> |
| 61 | #include <linux/if_ether.h> |
| 62 | #include <linux/if_tun.h> |
| 63 | #include <linux/crc32.h> |
Pavel Emelyanov | d647a59 | 2008-04-16 00:41:16 -0700 | [diff] [blame] | 64 | #include <linux/nsproxy.h> |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 65 | #include <linux/virtio_net.h> |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 66 | #include <linux/rcupdate.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 67 | #include <net/net_namespace.h> |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 68 | #include <net/netns/generic.h> |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 69 | #include <net/rtnetlink.h> |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 70 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <asm/uaccess.h> |
| 73 | |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 74 | /* Uncomment to enable debugging */ |
| 75 | /* #define TUN_DEBUG 1 */ |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #ifdef TUN_DEBUG |
| 78 | static int debug; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 79 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 80 | #define tun_debug(level, tun, fmt, args...) \ |
| 81 | do { \ |
| 82 | if (tun->debug) \ |
| 83 | netdev_printk(level, tun->dev, fmt, ##args); \ |
| 84 | } while (0) |
| 85 | #define DBG1(level, fmt, args...) \ |
| 86 | do { \ |
| 87 | if (debug == 2) \ |
| 88 | printk(level fmt, ##args); \ |
| 89 | } while (0) |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 90 | #else |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 91 | #define tun_debug(level, tun, fmt, args...) \ |
| 92 | do { \ |
| 93 | if (0) \ |
| 94 | netdev_printk(level, tun->dev, fmt, ##args); \ |
| 95 | } while (0) |
| 96 | #define DBG1(level, fmt, args...) \ |
| 97 | do { \ |
| 98 | if (0) \ |
| 99 | printk(level fmt, ##args); \ |
| 100 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | #endif |
| 102 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 103 | #define GOODCOPY_LEN 128 |
| 104 | |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 105 | #define FLT_EXACT_COUNT 8 |
| 106 | struct tap_filter { |
| 107 | unsigned int count; /* Number of addrs. Zero means disabled */ |
| 108 | u32 mask[2]; /* Mask of the hashed addrs */ |
| 109 | unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; |
| 110 | }; |
| 111 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 112 | /* A tun_file connects an open character device to a tuntap netdevice. It |
| 113 | * also contains all socket related strctures (except sock_fprog and tap_filter) |
| 114 | * to serve as one transmit queue for tuntap device. The sock_fprog and |
| 115 | * tap_filter were kept in tun_struct since they were used for filtering for the |
| 116 | * netdevice not for a specific queue (at least I didn't see the reqirement for |
| 117 | * this). |
| 118 | */ |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 119 | struct tun_file { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 120 | struct sock sk; |
| 121 | struct socket socket; |
| 122 | struct socket_wq wq; |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 123 | atomic_t count; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 124 | struct tun_struct *tun; |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 125 | struct net *net; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 126 | struct fasync_struct *fasync; |
| 127 | /* only used for fasnyc */ |
| 128 | unsigned int flags; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 131 | /* Since the socket were moved to tun_file, to preserve the behavior of persist |
| 132 | * device, socket fileter, sndbuf and vnet header size were restore when the |
| 133 | * file were attached to a persist device. |
| 134 | */ |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 135 | struct tun_struct { |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 136 | struct tun_file *tfile; |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 137 | unsigned int flags; |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 138 | kuid_t owner; |
| 139 | kgid_t group; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 140 | |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 141 | struct net_device *dev; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 142 | netdev_features_t set_features; |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 143 | #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ |
| 144 | NETIF_F_TSO6|NETIF_F_UFO) |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 145 | |
| 146 | int vnet_hdr_sz; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 147 | int sndbuf; |
| 148 | struct tap_filter txflt; |
| 149 | struct sock_fprog fprog; |
| 150 | /* protected by rtnl lock */ |
| 151 | bool filter_attached; |
Rusty Russell | 14daa02 | 2008-04-12 18:48:58 -0700 | [diff] [blame] | 152 | #ifdef TUN_DEBUG |
| 153 | int debug; |
| 154 | #endif |
| 155 | }; |
| 156 | |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 157 | static int tun_attach(struct tun_struct *tun, struct file *file) |
| 158 | { |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 159 | struct tun_file *tfile = file->private_data; |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 160 | int err; |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 161 | |
| 162 | ASSERT_RTNL(); |
| 163 | |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 164 | netif_tx_lock_bh(tun->dev); |
| 165 | |
| 166 | err = -EINVAL; |
| 167 | if (tfile->tun) |
| 168 | goto out; |
| 169 | |
| 170 | err = -EBUSY; |
| 171 | if (tun->tfile) |
| 172 | goto out; |
| 173 | |
| 174 | err = 0; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 175 | |
| 176 | /* Re-attach filter when attaching to a persist device */ |
| 177 | if (tun->filter_attached == true) { |
| 178 | err = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
| 179 | if (!err) |
| 180 | goto out; |
| 181 | } |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 182 | tfile->tun = tun; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 183 | tfile->socket.sk->sk_sndbuf = tun->sndbuf; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 184 | tun->tfile = tfile; |
Nolan Leake | bee3136 | 2010-07-27 13:53:43 +0000 | [diff] [blame] | 185 | netif_carrier_on(tun->dev); |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 186 | dev_hold(tun->dev); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 187 | sock_hold(&tfile->sk); |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 188 | atomic_inc(&tfile->count); |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 189 | |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 190 | out: |
| 191 | netif_tx_unlock_bh(tun->dev); |
| 192 | return err; |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 195 | static void __tun_detach(struct tun_struct *tun) |
| 196 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 197 | struct tun_file *tfile = tun->tfile; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 198 | /* Detach from net device */ |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 199 | netif_tx_lock_bh(tun->dev); |
Nolan Leake | bee3136 | 2010-07-27 13:53:43 +0000 | [diff] [blame] | 200 | netif_carrier_off(tun->dev); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 201 | tun->tfile = NULL; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 202 | tfile->tun = NULL; |
Eric W. Biederman | 38231b7 | 2009-01-20 11:02:28 +0000 | [diff] [blame] | 203 | netif_tx_unlock_bh(tun->dev); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 204 | |
| 205 | /* Drop read queue */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 206 | skb_queue_purge(&tfile->socket.sk->sk_receive_queue); |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 207 | |
| 208 | /* Drop the extra count on the net device */ |
| 209 | dev_put(tun->dev); |
| 210 | } |
| 211 | |
| 212 | static void tun_detach(struct tun_struct *tun) |
| 213 | { |
| 214 | rtnl_lock(); |
| 215 | __tun_detach(tun); |
| 216 | rtnl_unlock(); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static struct tun_struct *__tun_get(struct tun_file *tfile) |
| 220 | { |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 221 | struct tun_struct *tun = NULL; |
| 222 | |
| 223 | if (atomic_inc_not_zero(&tfile->count)) |
| 224 | tun = tfile->tun; |
| 225 | |
| 226 | return tun; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static struct tun_struct *tun_get(struct file *file) |
| 230 | { |
| 231 | return __tun_get(file->private_data); |
| 232 | } |
| 233 | |
| 234 | static void tun_put(struct tun_struct *tun) |
| 235 | { |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 236 | struct tun_file *tfile = tun->tfile; |
| 237 | |
| 238 | if (atomic_dec_and_test(&tfile->count)) |
| 239 | tun_detach(tfile->tun); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 242 | /* TAP filtering */ |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 243 | static void addr_hash_set(u32 *mask, const u8 *addr) |
| 244 | { |
| 245 | int n = ether_crc(ETH_ALEN, addr) >> 26; |
| 246 | mask[n >> 5] |= (1 << (n & 31)); |
| 247 | } |
| 248 | |
| 249 | static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) |
| 250 | { |
| 251 | int n = ether_crc(ETH_ALEN, addr) >> 26; |
| 252 | return mask[n >> 5] & (1 << (n & 31)); |
| 253 | } |
| 254 | |
| 255 | static int update_filter(struct tap_filter *filter, void __user *arg) |
| 256 | { |
| 257 | struct { u8 u[ETH_ALEN]; } *addr; |
| 258 | struct tun_filter uf; |
| 259 | int err, alen, n, nexact; |
| 260 | |
| 261 | if (copy_from_user(&uf, arg, sizeof(uf))) |
| 262 | return -EFAULT; |
| 263 | |
| 264 | if (!uf.count) { |
| 265 | /* Disabled */ |
| 266 | filter->count = 0; |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | alen = ETH_ALEN * uf.count; |
| 271 | addr = kmalloc(alen, GFP_KERNEL); |
| 272 | if (!addr) |
| 273 | return -ENOMEM; |
| 274 | |
| 275 | if (copy_from_user(addr, arg + sizeof(uf), alen)) { |
| 276 | err = -EFAULT; |
| 277 | goto done; |
| 278 | } |
| 279 | |
| 280 | /* The filter is updated without holding any locks. Which is |
| 281 | * perfectly safe. We disable it first and in the worst |
| 282 | * case we'll accept a few undesired packets. */ |
| 283 | filter->count = 0; |
| 284 | wmb(); |
| 285 | |
| 286 | /* Use first set of addresses as an exact filter */ |
| 287 | for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) |
| 288 | memcpy(filter->addr[n], addr[n].u, ETH_ALEN); |
| 289 | |
| 290 | nexact = n; |
| 291 | |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 292 | /* Remaining multicast addresses are hashed, |
| 293 | * unicast will leave the filter disabled. */ |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 294 | memset(filter->mask, 0, sizeof(filter->mask)); |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 295 | for (; n < uf.count; n++) { |
| 296 | if (!is_multicast_ether_addr(addr[n].u)) { |
| 297 | err = 0; /* no filter */ |
| 298 | goto done; |
| 299 | } |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 300 | addr_hash_set(filter->mask, addr[n].u); |
Alex Williamson | cfbf84f | 2009-02-08 17:49:17 -0800 | [diff] [blame] | 301 | } |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 302 | |
| 303 | /* For ALLMULTI just set the mask to all ones. |
| 304 | * This overrides the mask populated above. */ |
| 305 | if ((uf.flags & TUN_FLT_ALLMULTI)) |
| 306 | memset(filter->mask, ~0, sizeof(filter->mask)); |
| 307 | |
| 308 | /* Now enable the filter */ |
| 309 | wmb(); |
| 310 | filter->count = nexact; |
| 311 | |
| 312 | /* Return the number of exact filters */ |
| 313 | err = nexact; |
| 314 | |
| 315 | done: |
| 316 | kfree(addr); |
| 317 | return err; |
| 318 | } |
| 319 | |
| 320 | /* Returns: 0 - drop, !=0 - accept */ |
| 321 | static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) |
| 322 | { |
| 323 | /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect |
| 324 | * at this point. */ |
| 325 | struct ethhdr *eh = (struct ethhdr *) skb->data; |
| 326 | int i; |
| 327 | |
| 328 | /* Exact match */ |
| 329 | for (i = 0; i < filter->count; i++) |
Joe Perches | 2e42e47 | 2012-05-09 17:17:46 +0000 | [diff] [blame] | 330 | if (ether_addr_equal(eh->h_dest, filter->addr[i])) |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 331 | return 1; |
| 332 | |
| 333 | /* Inexact match (multicast only) */ |
| 334 | if (is_multicast_ether_addr(eh->h_dest)) |
| 335 | return addr_hash_test(filter->mask, eh->h_dest); |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Checks whether the packet is accepted or not. |
| 342 | * Returns: 0 - drop, !=0 - accept |
| 343 | */ |
| 344 | static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) |
| 345 | { |
| 346 | if (!filter->count) |
| 347 | return 1; |
| 348 | |
| 349 | return run_filter(filter, skb); |
| 350 | } |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | /* Network device part of the driver */ |
| 353 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 354 | static const struct ethtool_ops tun_ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 356 | /* Net device detach from fd. */ |
| 357 | static void tun_net_uninit(struct net_device *dev) |
| 358 | { |
| 359 | struct tun_struct *tun = netdev_priv(dev); |
| 360 | struct tun_file *tfile = tun->tfile; |
| 361 | |
| 362 | /* Inform the methods they need to stop using the dev. |
| 363 | */ |
| 364 | if (tfile) { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 365 | wake_up_all(&tfile->wq.wait); |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 366 | if (atomic_dec_and_test(&tfile->count)) |
| 367 | __tun_detach(tun); |
| 368 | } |
| 369 | } |
| 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | /* Net device open. */ |
| 372 | static int tun_net_open(struct net_device *dev) |
| 373 | { |
| 374 | netif_start_queue(dev); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /* Net device close. */ |
| 379 | static int tun_net_close(struct net_device *dev) |
| 380 | { |
| 381 | netif_stop_queue(dev); |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | /* Net device start xmit */ |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 386 | static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
| 388 | struct tun_struct *tun = netdev_priv(dev); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 389 | struct tun_file *tfile = tun->tfile; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 391 | tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | /* Drop packet if interface is not attached */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 394 | if (!tfile) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | goto drop; |
| 396 | |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 397 | /* Drop if the filter does not like it. |
| 398 | * This is a noop if the filter is disabled. |
| 399 | * Filter can be enabled only for the TAP devices. */ |
| 400 | if (!check_filter(&tun->txflt, skb)) |
| 401 | goto drop; |
| 402 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 403 | if (tfile->socket.sk->sk_filter && |
| 404 | sk_filter(tfile->socket.sk, skb)) |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 405 | goto drop; |
| 406 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 407 | if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) |
| 408 | >= dev->tx_queue_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (!(tun->flags & TUN_ONE_QUEUE)) { |
| 410 | /* Normal queueing mode. */ |
| 411 | /* Packet scheduler handles dropping of further packets. */ |
| 412 | netif_stop_queue(dev); |
| 413 | |
| 414 | /* We won't see all dropped packets individually, so overrun |
| 415 | * error is more appropriate. */ |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 416 | dev->stats.tx_fifo_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } else { |
| 418 | /* Single queue mode. |
| 419 | * Driver handles dropping of all packets itself. */ |
| 420 | goto drop; |
| 421 | } |
| 422 | } |
| 423 | |
Michael S. Tsirkin | 0110d6f | 2010-04-13 04:59:44 +0000 | [diff] [blame] | 424 | /* Orphan the skb - required as we might hang on to it |
| 425 | * for indefinite time. */ |
Michael S. Tsirkin | 868eefe | 2012-07-20 09:23:14 +0000 | [diff] [blame] | 426 | if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) |
| 427 | goto drop; |
Michael S. Tsirkin | 0110d6f | 2010-04-13 04:59:44 +0000 | [diff] [blame] | 428 | skb_orphan(skb); |
| 429 | |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 430 | /* Enqueue packet */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 431 | skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | /* Notify and wake up reader process */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 434 | if (tfile->flags & TUN_FASYNC) |
| 435 | kill_fasync(&tfile->fasync, SIGIO, POLL_IN); |
| 436 | wake_up_interruptible_poll(&tfile->wq.wait, POLLIN | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 437 | POLLRDNORM | POLLRDBAND); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 438 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
| 440 | drop: |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 441 | dev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | kfree_skb(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 443 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 446 | static void tun_net_mclist(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 448 | /* |
| 449 | * This callback is supposed to deal with mc filter in |
| 450 | * _rx_ path and has nothing to do with the _tx_ path. |
| 451 | * In rx path we always accept everything userspace gives us. |
| 452 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Ed Swierk | 4885a50 | 2007-09-16 12:21:38 -0700 | [diff] [blame] | 455 | #define MIN_MTU 68 |
| 456 | #define MAX_MTU 65535 |
| 457 | |
| 458 | static int |
| 459 | tun_net_change_mtu(struct net_device *dev, int new_mtu) |
| 460 | { |
| 461 | if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) |
| 462 | return -EINVAL; |
| 463 | dev->mtu = new_mtu; |
| 464 | return 0; |
| 465 | } |
| 466 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 467 | static netdev_features_t tun_net_fix_features(struct net_device *dev, |
| 468 | netdev_features_t features) |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 469 | { |
| 470 | struct tun_struct *tun = netdev_priv(dev); |
| 471 | |
| 472 | return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); |
| 473 | } |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 474 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 475 | static void tun_poll_controller(struct net_device *dev) |
| 476 | { |
| 477 | /* |
| 478 | * Tun only receives frames when: |
| 479 | * 1) the char device endpoint gets data from user space |
| 480 | * 2) the tun socket gets a sendmsg call from user space |
| 481 | * Since both of those are syncronous operations, we are guaranteed |
| 482 | * never to have pending data when we poll for it |
| 483 | * so theres nothing to do here but return. |
| 484 | * We need this though so netpoll recognizes us as an interface that |
| 485 | * supports polling, which enables bridge devices in virt setups to |
| 486 | * still use netconsole |
| 487 | */ |
| 488 | return; |
| 489 | } |
| 490 | #endif |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 491 | static const struct net_device_ops tun_netdev_ops = { |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 492 | .ndo_uninit = tun_net_uninit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 493 | .ndo_open = tun_net_open, |
| 494 | .ndo_stop = tun_net_close, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 495 | .ndo_start_xmit = tun_net_xmit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 496 | .ndo_change_mtu = tun_net_change_mtu, |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 497 | .ndo_fix_features = tun_net_fix_features, |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 498 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 499 | .ndo_poll_controller = tun_poll_controller, |
| 500 | #endif |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 501 | }; |
| 502 | |
| 503 | static const struct net_device_ops tap_netdev_ops = { |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 504 | .ndo_uninit = tun_net_uninit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 505 | .ndo_open = tun_net_open, |
| 506 | .ndo_stop = tun_net_close, |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 507 | .ndo_start_xmit = tun_net_xmit, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 508 | .ndo_change_mtu = tun_net_change_mtu, |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 509 | .ndo_fix_features = tun_net_fix_features, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 510 | .ndo_set_rx_mode = tun_net_mclist, |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 511 | .ndo_set_mac_address = eth_mac_addr, |
| 512 | .ndo_validate_addr = eth_validate_addr, |
Neil Horman | bebd097 | 2011-06-15 05:25:01 +0000 | [diff] [blame] | 513 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 514 | .ndo_poll_controller = tun_poll_controller, |
| 515 | #endif |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 516 | }; |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* Initialize net device. */ |
| 519 | static void tun_net_init(struct net_device *dev) |
| 520 | { |
| 521 | struct tun_struct *tun = netdev_priv(dev); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | switch (tun->flags & TUN_TYPE_MASK) { |
| 524 | case TUN_TUN_DEV: |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 525 | dev->netdev_ops = &tun_netdev_ops; |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | /* Point-to-Point TUN Device */ |
| 528 | dev->hard_header_len = 0; |
| 529 | dev->addr_len = 0; |
| 530 | dev->mtu = 1500; |
| 531 | |
| 532 | /* Zero header length */ |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 533 | dev->type = ARPHRD_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
| 535 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ |
| 536 | break; |
| 537 | |
| 538 | case TUN_TAP_DEV: |
Kusanagi Kouichi | 7a0a960 | 2008-12-29 18:23:28 -0800 | [diff] [blame] | 539 | dev->netdev_ops = &tap_netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | /* Ethernet TAP Device */ |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 541 | ether_setup(dev); |
Neil Horman | 550fd08 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 542 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
Danny Kukawka | f2cedb6 | 2012-02-15 06:45:39 +0000 | [diff] [blame] | 544 | eth_hw_addr_random(dev); |
Brian Braunstein | 36226a8 | 2007-04-26 01:00:55 -0700 | [diff] [blame] | 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /* Character device part */ |
| 552 | |
| 553 | /* Poll */ |
| 554 | static unsigned int tun_chr_poll(struct file *file, poll_table * wait) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 555 | { |
Eric W. Biederman | b2430de | 2009-01-20 11:03:21 +0000 | [diff] [blame] | 556 | struct tun_file *tfile = file->private_data; |
| 557 | struct tun_struct *tun = __tun_get(tfile); |
Mariusz Kozlowski | 3c8a9c6 | 2009-07-05 19:48:35 +0000 | [diff] [blame] | 558 | struct sock *sk; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 559 | unsigned int mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | if (!tun) |
Eric W. Biederman | eac9e90 | 2009-01-20 10:59:05 +0000 | [diff] [blame] | 562 | return POLLERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 564 | sk = tfile->socket.sk; |
Mariusz Kozlowski | 3c8a9c6 | 2009-07-05 19:48:35 +0000 | [diff] [blame] | 565 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 566 | tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 568 | poll_wait(file, &tfile->wq.wait, wait); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 569 | |
Michael S. Tsirkin | 89f56d1 | 2009-08-30 07:04:42 +0000 | [diff] [blame] | 570 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | mask |= POLLIN | POLLRDNORM; |
| 572 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 573 | if (sock_writeable(sk) || |
| 574 | (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) && |
| 575 | sock_writeable(sk))) |
| 576 | mask |= POLLOUT | POLLWRNORM; |
| 577 | |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 578 | if (tun->dev->reg_state != NETREG_REGISTERED) |
| 579 | mask = POLLERR; |
| 580 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 581 | tun_put(tun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | return mask; |
| 583 | } |
| 584 | |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 585 | /* prepad is the amount to reserve at front. len is length after that. |
| 586 | * linear is a hint as to how much to copy (usually headers). */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 587 | static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, |
stephen hemminger | 6f7c156 | 2011-06-08 14:33:08 +0000 | [diff] [blame] | 588 | size_t prepad, size_t len, |
| 589 | size_t linear, int noblock) |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 590 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 591 | struct sock *sk = tfile->socket.sk; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 592 | struct sk_buff *skb; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 593 | int err; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 594 | |
| 595 | /* Under a page? Don't bother with paged skb. */ |
Herbert Xu | 0eca93b | 2009-04-14 02:09:43 -0700 | [diff] [blame] | 596 | if (prepad + len < PAGE_SIZE || !linear) |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 597 | linear = len; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 598 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 599 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, |
| 600 | &err); |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 601 | if (!skb) |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 602 | return ERR_PTR(err); |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 603 | |
| 604 | skb_reserve(skb, prepad); |
| 605 | skb_put(skb, linear); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 606 | skb->data_len = len - linear; |
| 607 | skb->len += len - linear; |
Rusty Russell | f42157c | 2008-08-15 15:15:10 -0700 | [diff] [blame] | 608 | |
| 609 | return skb; |
| 610 | } |
| 611 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 612 | /* set skb frags from iovec, this can move to core network code for reuse */ |
| 613 | static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from, |
| 614 | int offset, size_t count) |
| 615 | { |
| 616 | int len = iov_length(from, count) - offset; |
| 617 | int copy = skb_headlen(skb); |
| 618 | int size, offset1 = 0; |
| 619 | int i = 0; |
| 620 | |
| 621 | /* Skip over from offset */ |
| 622 | while (count && (offset >= from->iov_len)) { |
| 623 | offset -= from->iov_len; |
| 624 | ++from; |
| 625 | --count; |
| 626 | } |
| 627 | |
| 628 | /* copy up to skb headlen */ |
| 629 | while (count && (copy > 0)) { |
| 630 | size = min_t(unsigned int, copy, from->iov_len - offset); |
| 631 | if (copy_from_user(skb->data + offset1, from->iov_base + offset, |
| 632 | size)) |
| 633 | return -EFAULT; |
| 634 | if (copy > size) { |
| 635 | ++from; |
| 636 | --count; |
| 637 | offset = 0; |
| 638 | } else |
| 639 | offset += size; |
| 640 | copy -= size; |
| 641 | offset1 += size; |
| 642 | } |
| 643 | |
| 644 | if (len == offset1) |
| 645 | return 0; |
| 646 | |
| 647 | while (count--) { |
| 648 | struct page *page[MAX_SKB_FRAGS]; |
| 649 | int num_pages; |
| 650 | unsigned long base; |
| 651 | unsigned long truesize; |
| 652 | |
| 653 | len = from->iov_len - offset; |
| 654 | if (!len) { |
| 655 | offset = 0; |
| 656 | ++from; |
| 657 | continue; |
| 658 | } |
| 659 | base = (unsigned long)from->iov_base + offset; |
| 660 | size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT; |
| 661 | if (i + size > MAX_SKB_FRAGS) |
| 662 | return -EMSGSIZE; |
| 663 | num_pages = get_user_pages_fast(base, size, 0, &page[i]); |
| 664 | if (num_pages != size) { |
| 665 | for (i = 0; i < num_pages; i++) |
| 666 | put_page(page[i]); |
| 667 | return -EFAULT; |
| 668 | } |
| 669 | truesize = size * PAGE_SIZE; |
| 670 | skb->data_len += len; |
| 671 | skb->len += len; |
| 672 | skb->truesize += truesize; |
| 673 | atomic_add(truesize, &skb->sk->sk_wmem_alloc); |
| 674 | while (len) { |
| 675 | int off = base & ~PAGE_MASK; |
| 676 | int size = min_t(int, len, PAGE_SIZE - off); |
| 677 | __skb_fill_page_desc(skb, i, page[i], off, size); |
| 678 | skb_shinfo(skb)->nr_frags++; |
| 679 | /* increase sk_wmem_alloc */ |
| 680 | base += size; |
| 681 | len -= size; |
| 682 | i++; |
| 683 | } |
| 684 | offset = 0; |
| 685 | ++from; |
| 686 | } |
| 687 | return 0; |
| 688 | } |
| 689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | /* Get packet from user space buffer */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 691 | static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, |
| 692 | void *msg_control, const struct iovec *iv, |
| 693 | size_t total_len, size_t count, int noblock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
Harvey Harrison | 09640e63 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 695 | struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | struct sk_buff *skb; |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 697 | size_t len = total_len, align = NET_SKB_PAD; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 698 | struct virtio_net_hdr gso = { 0 }; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 699 | int offset = 0; |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 700 | int copylen; |
| 701 | bool zerocopy = false; |
| 702 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
| 704 | if (!(tun->flags & TUN_NO_PI)) { |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 705 | if ((len -= sizeof(pi)) > total_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | return -EINVAL; |
| 707 | |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 708 | if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | return -EFAULT; |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 710 | offset += sizeof(pi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 713 | if (tun->flags & TUN_VNET_HDR) { |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 714 | if ((len -= tun->vnet_hdr_sz) > total_len) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 715 | return -EINVAL; |
| 716 | |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 717 | if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso))) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 718 | return -EFAULT; |
| 719 | |
Herbert Xu | 4909122 | 2009-06-08 00:20:01 -0700 | [diff] [blame] | 720 | if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && |
| 721 | gso.csum_start + gso.csum_offset + 2 > gso.hdr_len) |
| 722 | gso.hdr_len = gso.csum_start + gso.csum_offset + 2; |
| 723 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 724 | if (gso.hdr_len > len) |
| 725 | return -EINVAL; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 726 | offset += tun->vnet_hdr_sz; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Rusty Russell | e01bf1c | 2008-04-12 18:49:30 -0700 | [diff] [blame] | 729 | if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { |
stephen hemminger | a504b86 | 2011-06-08 14:33:07 +0000 | [diff] [blame] | 730 | align += NET_IP_ALIGN; |
Herbert Xu | 0eca93b | 2009-04-14 02:09:43 -0700 | [diff] [blame] | 731 | if (unlikely(len < ETH_HLEN || |
| 732 | (gso.hdr_len && gso.hdr_len < ETH_HLEN))) |
Rusty Russell | e01bf1c | 2008-04-12 18:49:30 -0700 | [diff] [blame] | 733 | return -EINVAL; |
| 734 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 735 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 736 | if (msg_control) |
| 737 | zerocopy = true; |
| 738 | |
| 739 | if (zerocopy) { |
| 740 | /* Userspace may produce vectors with count greater than |
| 741 | * MAX_SKB_FRAGS, so we need to linearize parts of the skb |
| 742 | * to let the rest of data to be fit in the frags. |
| 743 | */ |
| 744 | if (count > MAX_SKB_FRAGS) { |
| 745 | copylen = iov_length(iv, count - MAX_SKB_FRAGS); |
| 746 | if (copylen < offset) |
| 747 | copylen = 0; |
| 748 | else |
| 749 | copylen -= offset; |
| 750 | } else |
| 751 | copylen = 0; |
| 752 | /* There are 256 bytes to be copied in skb, so there is enough |
| 753 | * room for skb expand head in case it is used. |
| 754 | * The rest of the buffer is mapped from userspace. |
| 755 | */ |
| 756 | if (copylen < gso.hdr_len) |
| 757 | copylen = gso.hdr_len; |
| 758 | if (!copylen) |
| 759 | copylen = GOODCOPY_LEN; |
| 760 | } else |
| 761 | copylen = len; |
| 762 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 763 | skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 764 | if (IS_ERR(skb)) { |
| 765 | if (PTR_ERR(skb) != -EAGAIN) |
| 766 | tun->dev->stats.rx_dropped++; |
| 767 | return PTR_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 770 | if (zerocopy) |
| 771 | err = zerocopy_sg_from_iovec(skb, iv, offset, count); |
| 772 | else |
| 773 | err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len); |
| 774 | |
| 775 | if (err) { |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 776 | tun->dev->stats.rx_dropped++; |
Dave Jones | 8f22757 | 2006-03-11 18:49:13 -0800 | [diff] [blame] | 777 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return -EFAULT; |
Dave Jones | 8f22757 | 2006-03-11 18:49:13 -0800 | [diff] [blame] | 779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 781 | if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
| 782 | if (!skb_partial_csum_set(skb, gso.csum_start, |
| 783 | gso.csum_offset)) { |
| 784 | tun->dev->stats.rx_frame_errors++; |
| 785 | kfree_skb(skb); |
| 786 | return -EINVAL; |
| 787 | } |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 788 | } |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | switch (tun->flags & TUN_TYPE_MASK) { |
| 791 | case TUN_TUN_DEV: |
Ang Way Chuang | f09f7ee | 2008-06-17 21:10:33 -0700 | [diff] [blame] | 792 | if (tun->flags & TUN_NO_PI) { |
| 793 | switch (skb->data[0] & 0xf0) { |
| 794 | case 0x40: |
| 795 | pi.proto = htons(ETH_P_IP); |
| 796 | break; |
| 797 | case 0x60: |
| 798 | pi.proto = htons(ETH_P_IPV6); |
| 799 | break; |
| 800 | default: |
| 801 | tun->dev->stats.rx_dropped++; |
| 802 | kfree_skb(skb); |
| 803 | return -EINVAL; |
| 804 | } |
| 805 | } |
| 806 | |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 807 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | skb->protocol = pi.proto; |
Arnaldo Carvalho de Melo | 4c13eb6 | 2007-04-25 17:40:23 -0700 | [diff] [blame] | 809 | skb->dev = tun->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | break; |
| 811 | case TUN_TAP_DEV: |
| 812 | skb->protocol = eth_type_trans(skb, tun->dev); |
| 813 | break; |
Joe Perches | 6403eab | 2011-06-03 11:51:20 +0000 | [diff] [blame] | 814 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 816 | if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
| 817 | pr_debug("GSO!\n"); |
| 818 | switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
| 819 | case VIRTIO_NET_HDR_GSO_TCPV4: |
| 820 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
| 821 | break; |
| 822 | case VIRTIO_NET_HDR_GSO_TCPV6: |
| 823 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
| 824 | break; |
Sridhar Samudrala | e36aa25 | 2009-07-14 14:21:04 +0000 | [diff] [blame] | 825 | case VIRTIO_NET_HDR_GSO_UDP: |
| 826 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; |
| 827 | break; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 828 | default: |
| 829 | tun->dev->stats.rx_frame_errors++; |
| 830 | kfree_skb(skb); |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | |
| 834 | if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN) |
| 835 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; |
| 836 | |
| 837 | skb_shinfo(skb)->gso_size = gso.gso_size; |
| 838 | if (skb_shinfo(skb)->gso_size == 0) { |
| 839 | tun->dev->stats.rx_frame_errors++; |
| 840 | kfree_skb(skb); |
| 841 | return -EINVAL; |
| 842 | } |
| 843 | |
| 844 | /* Header must be checked, and gso_segs computed. */ |
| 845 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 846 | skb_shinfo(skb)->gso_segs = 0; |
| 847 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 848 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 849 | /* copy skb_ubuf_info for callback when skb has no error */ |
| 850 | if (zerocopy) { |
| 851 | skb_shinfo(skb)->destructor_arg = msg_control; |
| 852 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; |
| 853 | } |
| 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | netif_rx_ni(skb); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 856 | |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 857 | tun->dev->stats.rx_packets++; |
| 858 | tun->dev->stats.rx_bytes += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
Michael S. Tsirkin | 0690899 | 2012-07-20 09:23:23 +0000 | [diff] [blame] | 860 | return total_len; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 861 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 863 | static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, |
| 864 | unsigned long count, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | { |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 866 | struct file *file = iocb->ki_filp; |
Herbert Xu | ab46d779 | 2009-02-14 20:46:39 -0800 | [diff] [blame] | 867 | struct tun_struct *tun = tun_get(file); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 868 | struct tun_file *tfile = file->private_data; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 869 | ssize_t result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
| 871 | if (!tun) |
| 872 | return -EBADFD; |
| 873 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 874 | tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 876 | result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count), |
| 877 | count, file->f_flags & O_NONBLOCK); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 878 | |
| 879 | tun_put(tun); |
| 880 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | /* Put packet to the user space buffer */ |
stephen hemminger | 6f7c156 | 2011-06-08 14:33:08 +0000 | [diff] [blame] | 884 | static ssize_t tun_put_user(struct tun_struct *tun, |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 885 | struct tun_file *tfile, |
stephen hemminger | 6f7c156 | 2011-06-08 14:33:08 +0000 | [diff] [blame] | 886 | struct sk_buff *skb, |
| 887 | const struct iovec *iv, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | { |
| 889 | struct tun_pi pi = { 0, skb->protocol }; |
| 890 | ssize_t total = 0; |
| 891 | |
| 892 | if (!(tun->flags & TUN_NO_PI)) { |
| 893 | if ((len -= sizeof(pi)) < 0) |
| 894 | return -EINVAL; |
| 895 | |
| 896 | if (len < skb->len) { |
| 897 | /* Packet will be striped */ |
| 898 | pi.flags |= TUN_PKT_STRIP; |
| 899 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 900 | |
Michael S. Tsirkin | 43b39dc | 2009-04-20 01:25:59 +0000 | [diff] [blame] | 901 | if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | return -EFAULT; |
| 903 | total += sizeof(pi); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 906 | if (tun->flags & TUN_VNET_HDR) { |
| 907 | struct virtio_net_hdr gso = { 0 }; /* no info leak */ |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 908 | if ((len -= tun->vnet_hdr_sz) < 0) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 909 | return -EINVAL; |
| 910 | |
| 911 | if (skb_is_gso(skb)) { |
| 912 | struct skb_shared_info *sinfo = skb_shinfo(skb); |
| 913 | |
| 914 | /* This is a hint as to how much should be linear. */ |
| 915 | gso.hdr_len = skb_headlen(skb); |
| 916 | gso.gso_size = sinfo->gso_size; |
| 917 | if (sinfo->gso_type & SKB_GSO_TCPV4) |
| 918 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 919 | else if (sinfo->gso_type & SKB_GSO_TCPV6) |
| 920 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
Sridhar Samudrala | e36aa25 | 2009-07-14 14:21:04 +0000 | [diff] [blame] | 921 | else if (sinfo->gso_type & SKB_GSO_UDP) |
| 922 | gso.gso_type = VIRTIO_NET_HDR_GSO_UDP; |
Michael S. Tsirkin | ef3db4a | 2010-07-21 04:32:45 +0000 | [diff] [blame] | 923 | else { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 924 | pr_err("unexpected GSO type: " |
Michael S. Tsirkin | ef3db4a | 2010-07-21 04:32:45 +0000 | [diff] [blame] | 925 | "0x%x, gso_size %d, hdr_len %d\n", |
| 926 | sinfo->gso_type, gso.gso_size, |
| 927 | gso.hdr_len); |
| 928 | print_hex_dump(KERN_ERR, "tun: ", |
| 929 | DUMP_PREFIX_NONE, |
| 930 | 16, 1, skb->head, |
| 931 | min((int)gso.hdr_len, 64), true); |
| 932 | WARN_ON_ONCE(1); |
| 933 | return -EINVAL; |
| 934 | } |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 935 | if (sinfo->gso_type & SKB_GSO_TCP_ECN) |
| 936 | gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
| 937 | } else |
| 938 | gso.gso_type = VIRTIO_NET_HDR_GSO_NONE; |
| 939 | |
| 940 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 941 | gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
Michał Mirosław | 55508d6 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 942 | gso.csum_start = skb_checksum_start_offset(skb); |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 943 | gso.csum_offset = skb->csum_offset; |
Jason Wang | 10a8d94 | 2011-06-10 00:56:17 +0000 | [diff] [blame] | 944 | } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) { |
| 945 | gso.flags = VIRTIO_NET_HDR_F_DATA_VALID; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 946 | } /* else everything is zero */ |
| 947 | |
Michael S. Tsirkin | 43b39dc | 2009-04-20 01:25:59 +0000 | [diff] [blame] | 948 | if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total, |
| 949 | sizeof(gso)))) |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 950 | return -EFAULT; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 951 | total += tun->vnet_hdr_sz; |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | len = min_t(int, skb->len, len); |
| 955 | |
Michael S. Tsirkin | 43b39dc | 2009-04-20 01:25:59 +0000 | [diff] [blame] | 956 | skb_copy_datagram_const_iovec(skb, 0, iv, total, len); |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 957 | total += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 959 | tun->dev->stats.tx_packets++; |
| 960 | tun->dev->stats.tx_bytes += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | return total; |
| 963 | } |
| 964 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 965 | static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 966 | struct kiocb *iocb, const struct iovec *iv, |
| 967 | ssize_t len, int noblock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | DECLARE_WAITQUEUE(wait, current); |
| 970 | struct sk_buff *skb; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 971 | ssize_t ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 973 | tun_debug(KERN_INFO, tun, "tun_chr_read\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | |
Amos Kong | 61a5ff1 | 2011-06-09 00:27:10 -0700 | [diff] [blame] | 975 | if (unlikely(!noblock)) |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 976 | add_wait_queue(&tfile->wq.wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | while (len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | current->state = TASK_INTERRUPTIBLE; |
| 979 | |
| 980 | /* Read frames from the queue */ |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 981 | if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) { |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 982 | if (noblock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | ret = -EAGAIN; |
| 984 | break; |
| 985 | } |
| 986 | if (signal_pending(current)) { |
| 987 | ret = -ERESTARTSYS; |
| 988 | break; |
| 989 | } |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 990 | if (tun->dev->reg_state != NETREG_REGISTERED) { |
| 991 | ret = -EIO; |
| 992 | break; |
| 993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | /* Nothing to read, let's sleep */ |
| 996 | schedule(); |
| 997 | continue; |
| 998 | } |
| 999 | netif_wake_queue(tun->dev); |
| 1000 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1001 | ret = tun_put_user(tun, tfile, skb, iv, len); |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1002 | kfree_skb(skb); |
| 1003 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | current->state = TASK_RUNNING; |
Amos Kong | 61a5ff1 | 2011-06-09 00:27:10 -0700 | [diff] [blame] | 1007 | if (unlikely(!noblock)) |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1008 | remove_wait_queue(&tfile->wq.wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1010 | return ret; |
| 1011 | } |
| 1012 | |
| 1013 | static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, |
| 1014 | unsigned long count, loff_t pos) |
| 1015 | { |
| 1016 | struct file *file = iocb->ki_filp; |
| 1017 | struct tun_file *tfile = file->private_data; |
| 1018 | struct tun_struct *tun = __tun_get(tfile); |
| 1019 | ssize_t len, ret; |
| 1020 | |
| 1021 | if (!tun) |
| 1022 | return -EBADFD; |
| 1023 | len = iov_length(iv, count); |
| 1024 | if (len < 0) { |
| 1025 | ret = -EINVAL; |
| 1026 | goto out; |
| 1027 | } |
| 1028 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1029 | ret = tun_do_read(tun, tfile, iocb, iv, len, |
| 1030 | file->f_flags & O_NONBLOCK); |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1031 | ret = min_t(ssize_t, ret, len); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1032 | out: |
| 1033 | tun_put(tun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | return ret; |
| 1035 | } |
| 1036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | static void tun_setup(struct net_device *dev) |
| 1038 | { |
| 1039 | struct tun_struct *tun = netdev_priv(dev); |
| 1040 | |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1041 | tun->owner = INVALID_UID; |
| 1042 | tun->group = INVALID_GID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | dev->ethtool_ops = &tun_ethtool_ops; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1045 | dev->destructor = free_netdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1048 | /* Trivial set of netlink ops to allow deleting tun or tap |
| 1049 | * device with netlink. |
| 1050 | */ |
| 1051 | static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1052 | { |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | |
| 1056 | static struct rtnl_link_ops tun_link_ops __read_mostly = { |
| 1057 | .kind = DRV_NAME, |
| 1058 | .priv_size = sizeof(struct tun_struct), |
| 1059 | .setup = tun_setup, |
| 1060 | .validate = tun_validate, |
| 1061 | }; |
| 1062 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1063 | static void tun_sock_write_space(struct sock *sk) |
| 1064 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1065 | struct tun_file *tfile; |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1066 | wait_queue_head_t *wqueue; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1067 | |
| 1068 | if (!sock_writeable(sk)) |
| 1069 | return; |
| 1070 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1071 | if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags)) |
| 1072 | return; |
| 1073 | |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 1074 | wqueue = sk_sleep(sk); |
| 1075 | if (wqueue && waitqueue_active(wqueue)) |
| 1076 | wake_up_interruptible_sync_poll(wqueue, POLLOUT | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1077 | POLLWRNORM | POLLWRBAND); |
Herbert Xu | c722c62 | 2009-06-03 21:45:55 -0700 | [diff] [blame] | 1078 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1079 | tfile = container_of(sk, struct tun_file, sk); |
| 1080 | kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1083 | static int tun_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 1084 | struct msghdr *m, size_t total_len) |
| 1085 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1086 | int ret; |
| 1087 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
| 1088 | struct tun_struct *tun = __tun_get(tfile); |
| 1089 | |
| 1090 | if (!tun) |
| 1091 | return -EBADFD; |
| 1092 | |
| 1093 | ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len, |
| 1094 | m->msg_iovlen, m->msg_flags & MSG_DONTWAIT); |
| 1095 | tun_put(tun); |
| 1096 | return ret; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1099 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1100 | static int tun_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 1101 | struct msghdr *m, size_t total_len, |
| 1102 | int flags) |
| 1103 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1104 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
| 1105 | struct tun_struct *tun = __tun_get(tfile); |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1106 | int ret; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1107 | |
| 1108 | if (!tun) |
| 1109 | return -EBADFD; |
| 1110 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1111 | if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) |
| 1112 | return -EINVAL; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1113 | ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len, |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1114 | flags & MSG_DONTWAIT); |
| 1115 | if (ret > total_len) { |
| 1116 | m->msg_flags |= MSG_TRUNC; |
| 1117 | ret = flags & MSG_TRUNC ? ret : total_len; |
| 1118 | } |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1119 | tun_put(tun); |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1120 | return ret; |
| 1121 | } |
| 1122 | |
Stanislav Kinsbursky | 1ab5ecb | 2012-03-12 02:59:41 +0000 | [diff] [blame] | 1123 | static int tun_release(struct socket *sock) |
| 1124 | { |
| 1125 | if (sock->sk) |
| 1126 | sock_put(sock->sk); |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1130 | /* Ops structure to mimic raw sockets with tun */ |
| 1131 | static const struct proto_ops tun_socket_ops = { |
| 1132 | .sendmsg = tun_sendmsg, |
| 1133 | .recvmsg = tun_recvmsg, |
Stanislav Kinsbursky | 1ab5ecb | 2012-03-12 02:59:41 +0000 | [diff] [blame] | 1134 | .release = tun_release, |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1135 | }; |
| 1136 | |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1137 | static struct proto tun_proto = { |
| 1138 | .name = "tun", |
| 1139 | .owner = THIS_MODULE, |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1140 | .obj_size = sizeof(struct tun_file), |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1141 | }; |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1142 | |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1143 | static int tun_flags(struct tun_struct *tun) |
| 1144 | { |
| 1145 | int flags = 0; |
| 1146 | |
| 1147 | if (tun->flags & TUN_TUN_DEV) |
| 1148 | flags |= IFF_TUN; |
| 1149 | else |
| 1150 | flags |= IFF_TAP; |
| 1151 | |
| 1152 | if (tun->flags & TUN_NO_PI) |
| 1153 | flags |= IFF_NO_PI; |
| 1154 | |
| 1155 | if (tun->flags & TUN_ONE_QUEUE) |
| 1156 | flags |= IFF_ONE_QUEUE; |
| 1157 | |
| 1158 | if (tun->flags & TUN_VNET_HDR) |
| 1159 | flags |= IFF_VNET_HDR; |
| 1160 | |
| 1161 | return flags; |
| 1162 | } |
| 1163 | |
| 1164 | static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, |
| 1165 | char *buf) |
| 1166 | { |
| 1167 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); |
| 1168 | return sprintf(buf, "0x%x\n", tun_flags(tun)); |
| 1169 | } |
| 1170 | |
| 1171 | static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, |
| 1172 | char *buf) |
| 1173 | { |
| 1174 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1175 | return uid_valid(tun->owner)? |
| 1176 | sprintf(buf, "%u\n", |
| 1177 | from_kuid_munged(current_user_ns(), tun->owner)): |
| 1178 | sprintf(buf, "-1\n"); |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, |
| 1182 | char *buf) |
| 1183 | { |
| 1184 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1185 | return gid_valid(tun->group) ? |
| 1186 | sprintf(buf, "%u\n", |
| 1187 | from_kgid_munged(current_user_ns(), tun->group)): |
| 1188 | sprintf(buf, "-1\n"); |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); |
| 1192 | static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); |
| 1193 | static DEVICE_ATTR(group, 0444, tun_show_group, NULL); |
| 1194 | |
Pavel Emelyanov | d647a59 | 2008-04-16 00:41:16 -0700 | [diff] [blame] | 1195 | static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | { |
| 1197 | struct tun_struct *tun; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1198 | struct tun_file *tfile = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | struct net_device *dev; |
| 1200 | int err; |
| 1201 | |
Eric W. Biederman | 74a3e5a | 2009-01-20 10:56:20 +0000 | [diff] [blame] | 1202 | dev = __dev_get_by_name(net, ifr->ifr_name); |
| 1203 | if (dev) { |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1204 | const struct cred *cred = current_cred(); |
| 1205 | |
David Woodhouse | f85ba78 | 2009-04-27 03:23:54 -0700 | [diff] [blame] | 1206 | if (ifr->ifr_flags & IFF_TUN_EXCL) |
| 1207 | return -EBUSY; |
Eric W. Biederman | 74a3e5a | 2009-01-20 10:56:20 +0000 | [diff] [blame] | 1208 | if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) |
| 1209 | tun = netdev_priv(dev); |
| 1210 | else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) |
| 1211 | tun = netdev_priv(dev); |
| 1212 | else |
| 1213 | return -EINVAL; |
| 1214 | |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1215 | if (((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || |
| 1216 | (gid_valid(tun->group) && !in_egroup_p(tun->group))) && |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1217 | !capable(CAP_NET_ADMIN)) |
| 1218 | return -EPERM; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1219 | err = security_tun_dev_attach(tfile->socket.sk); |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1220 | if (err < 0) |
| 1221 | return err; |
| 1222 | |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 1223 | err = tun_attach(tun, file); |
| 1224 | if (err < 0) |
| 1225 | return err; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | else { |
| 1228 | char *name; |
| 1229 | unsigned long flags = 0; |
| 1230 | |
David Woodhouse | ca6bb5d | 2006-06-22 16:07:52 -0700 | [diff] [blame] | 1231 | if (!capable(CAP_NET_ADMIN)) |
| 1232 | return -EPERM; |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1233 | err = security_tun_dev_create(); |
| 1234 | if (err < 0) |
| 1235 | return err; |
David Woodhouse | ca6bb5d | 2006-06-22 16:07:52 -0700 | [diff] [blame] | 1236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | /* Set dev type */ |
| 1238 | if (ifr->ifr_flags & IFF_TUN) { |
| 1239 | /* TUN device */ |
| 1240 | flags |= TUN_TUN_DEV; |
| 1241 | name = "tun%d"; |
| 1242 | } else if (ifr->ifr_flags & IFF_TAP) { |
| 1243 | /* TAP device */ |
| 1244 | flags |= TUN_TAP_DEV; |
| 1245 | name = "tap%d"; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1246 | } else |
Kusanagi Kouichi | 36989b9 | 2009-09-16 21:36:13 +0000 | [diff] [blame] | 1247 | return -EINVAL; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | if (*ifr->ifr_name) |
| 1250 | name = ifr->ifr_name; |
| 1251 | |
| 1252 | dev = alloc_netdev(sizeof(struct tun_struct), name, |
| 1253 | tun_setup); |
| 1254 | if (!dev) |
| 1255 | return -ENOMEM; |
| 1256 | |
Pavel Emelyanov | fc54c65 | 2008-04-16 00:41:53 -0700 | [diff] [blame] | 1257 | dev_net_set(dev, net); |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1258 | dev->rtnl_link_ops = &tun_link_ops; |
Stephen Hemminger | 758e43b | 2008-11-19 22:10:37 -0800 | [diff] [blame] | 1259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | tun = netdev_priv(dev); |
| 1261 | tun->dev = dev; |
| 1262 | tun->flags = flags; |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1263 | tun->txflt.count = 0; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 1264 | tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1266 | tun->filter_attached = false; |
| 1267 | tun->sndbuf = tfile->socket.sk->sk_sndbuf; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1268 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1269 | security_tun_dev_post_create(&tfile->sk); |
Paul Moore | 2b980db | 2009-08-28 18:12:43 -0400 | [diff] [blame] | 1270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | tun_net_init(dev); |
| 1272 | |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1273 | dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | |
| 1274 | TUN_USER_FEATURES; |
| 1275 | dev->features = dev->hw_features; |
| 1276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | err = register_netdevice(tun->dev); |
| 1278 | if (err < 0) |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1279 | goto err_free_dev; |
Herbert Xu | 9c3fea6 | 2009-04-18 14:15:52 +0000 | [diff] [blame] | 1280 | |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1281 | if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) || |
| 1282 | device_create_file(&tun->dev->dev, &dev_attr_owner) || |
| 1283 | device_create_file(&tun->dev->dev, &dev_attr_group)) |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1284 | pr_err("Failed to create tun sysfs files\n"); |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1285 | |
Eric W. Biederman | a7385ba | 2009-01-20 10:57:48 +0000 | [diff] [blame] | 1286 | err = tun_attach(tun, file); |
| 1287 | if (err < 0) |
Herbert Xu | 9c3fea6 | 2009-04-18 14:15:52 +0000 | [diff] [blame] | 1288 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1291 | tun_debug(KERN_INFO, tun, "tun_set_iff\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | |
| 1293 | if (ifr->ifr_flags & IFF_NO_PI) |
| 1294 | tun->flags |= TUN_NO_PI; |
Nathaniel Filardo | a26af1e | 2008-02-05 03:05:07 -0800 | [diff] [blame] | 1295 | else |
| 1296 | tun->flags &= ~TUN_NO_PI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | |
| 1298 | if (ifr->ifr_flags & IFF_ONE_QUEUE) |
| 1299 | tun->flags |= TUN_ONE_QUEUE; |
Nathaniel Filardo | a26af1e | 2008-02-05 03:05:07 -0800 | [diff] [blame] | 1300 | else |
| 1301 | tun->flags &= ~TUN_ONE_QUEUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
Rusty Russell | f43798c | 2008-07-03 03:48:02 -0700 | [diff] [blame] | 1303 | if (ifr->ifr_flags & IFF_VNET_HDR) |
| 1304 | tun->flags |= TUN_VNET_HDR; |
| 1305 | else |
| 1306 | tun->flags &= ~TUN_VNET_HDR; |
| 1307 | |
Max Krasnyansky | e35259a | 2008-07-10 16:59:11 -0700 | [diff] [blame] | 1308 | /* Make sure persistent devices do not get stuck in |
| 1309 | * xoff state. |
| 1310 | */ |
| 1311 | if (netif_running(tun->dev)) |
| 1312 | netif_wake_queue(tun->dev); |
| 1313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | strcpy(ifr->ifr_name, tun->dev->name); |
| 1315 | return 0; |
| 1316 | |
| 1317 | err_free_dev: |
| 1318 | free_netdev(dev); |
| 1319 | failed: |
| 1320 | return err; |
| 1321 | } |
| 1322 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1323 | static int tun_get_iff(struct net *net, struct tun_struct *tun, |
| 1324 | struct ifreq *ifr) |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1325 | { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1326 | tun_debug(KERN_INFO, tun, "tun_get_iff\n"); |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1327 | |
| 1328 | strcpy(ifr->ifr_name, tun->dev->name); |
| 1329 | |
David Woodhouse | 980c9e8 | 2009-05-09 22:54:21 -0700 | [diff] [blame] | 1330 | ifr->ifr_flags = tun_flags(tun); |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1331 | |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1335 | /* This is like a cut-down ethtool ops, except done via tun fd so no |
| 1336 | * privs required. */ |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1337 | static int set_offload(struct tun_struct *tun, unsigned long arg) |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1338 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1339 | netdev_features_t features = 0; |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1340 | |
| 1341 | if (arg & TUN_F_CSUM) { |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1342 | features |= NETIF_F_HW_CSUM; |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1343 | arg &= ~TUN_F_CSUM; |
| 1344 | |
| 1345 | if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { |
| 1346 | if (arg & TUN_F_TSO_ECN) { |
| 1347 | features |= NETIF_F_TSO_ECN; |
| 1348 | arg &= ~TUN_F_TSO_ECN; |
| 1349 | } |
| 1350 | if (arg & TUN_F_TSO4) |
| 1351 | features |= NETIF_F_TSO; |
| 1352 | if (arg & TUN_F_TSO6) |
| 1353 | features |= NETIF_F_TSO6; |
| 1354 | arg &= ~(TUN_F_TSO4|TUN_F_TSO6); |
| 1355 | } |
Sridhar Samudrala | e36aa25 | 2009-07-14 14:21:04 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (arg & TUN_F_UFO) { |
| 1358 | features |= NETIF_F_UFO; |
| 1359 | arg &= ~TUN_F_UFO; |
| 1360 | } |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /* This gives the user a way to test for new features in future by |
| 1364 | * trying to set them. */ |
| 1365 | if (arg) |
| 1366 | return -EINVAL; |
| 1367 | |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1368 | tun->set_features = features; |
| 1369 | netdev_update_features(tun->dev); |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1370 | |
| 1371 | return 0; |
| 1372 | } |
| 1373 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1374 | static long __tun_chr_ioctl(struct file *file, unsigned int cmd, |
| 1375 | unsigned long arg, int ifreq_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | { |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 1377 | struct tun_file *tfile = file->private_data; |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1378 | struct tun_struct *tun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | void __user* argp = (void __user*)arg; |
| 1380 | struct ifreq ifr; |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1381 | kuid_t owner; |
| 1382 | kgid_t group; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1383 | int sndbuf; |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 1384 | int vnet_hdr_sz; |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1385 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
Mathias Krause | a117dac | 2012-07-29 19:45:14 +0000 | [diff] [blame] | 1387 | if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) { |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1388 | if (copy_from_user(&ifr, argp, ifreq_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | return -EFAULT; |
David S. Miller | 8bbb181 | 2012-07-30 14:52:48 -0700 | [diff] [blame] | 1390 | } else { |
Mathias Krause | a117dac | 2012-07-29 19:45:14 +0000 | [diff] [blame] | 1391 | memset(&ifr, 0, sizeof(ifr)); |
David S. Miller | 8bbb181 | 2012-07-30 14:52:48 -0700 | [diff] [blame] | 1392 | } |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1393 | if (cmd == TUNGETFEATURES) { |
| 1394 | /* Currently this just means: "what IFF flags are valid?". |
| 1395 | * This is needed because we never checked for invalid flags on |
| 1396 | * TUNSETIFF. */ |
| 1397 | return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | |
| 1398 | IFF_VNET_HDR, |
| 1399 | (unsigned int __user*)argp); |
| 1400 | } |
| 1401 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1402 | rtnl_lock(); |
| 1403 | |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 1404 | tun = __tun_get(tfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | if (cmd == TUNSETIFF && !tun) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | ifr.ifr_name[IFNAMSIZ-1] = '\0'; |
| 1407 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1408 | ret = tun_set_iff(tfile->net, file, &ifr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1410 | if (ret) |
| 1411 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1413 | if (copy_to_user(argp, &ifr, ifreq_len)) |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1414 | ret = -EFAULT; |
| 1415 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1418 | ret = -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | if (!tun) |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1420 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
Jason Wang | 1e58833 | 2012-10-31 19:45:56 +0000 | [diff] [blame] | 1422 | tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1424 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | switch (cmd) { |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1426 | case TUNGETIFF: |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1427 | ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr); |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1428 | if (ret) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1429 | break; |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1430 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1431 | if (copy_to_user(argp, &ifr, ifreq_len)) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1432 | ret = -EFAULT; |
Mark McLoughlin | e3b9955 | 2008-08-15 15:09:56 -0700 | [diff] [blame] | 1433 | break; |
| 1434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | case TUNSETNOCSUM: |
| 1436 | /* Disable/Enable checksum */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1438 | /* [unimplemented] */ |
| 1439 | tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1440 | arg ? "disabled" : "enabled"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | break; |
| 1442 | |
| 1443 | case TUNSETPERSIST: |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1444 | /* Disable/Enable persist mode. Keep an extra reference to the |
| 1445 | * module to prevent the module being unprobed. |
| 1446 | */ |
| 1447 | if (arg) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | tun->flags |= TUN_PERSIST; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1449 | __module_get(THIS_MODULE); |
| 1450 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | tun->flags &= ~TUN_PERSIST; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1452 | module_put(THIS_MODULE); |
| 1453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1455 | tun_debug(KERN_INFO, tun, "persist %s\n", |
| 1456 | arg ? "enabled" : "disabled"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | break; |
| 1458 | |
| 1459 | case TUNSETOWNER: |
| 1460 | /* Set owner of the device */ |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1461 | owner = make_kuid(current_user_ns(), arg); |
| 1462 | if (!uid_valid(owner)) { |
| 1463 | ret = -EINVAL; |
| 1464 | break; |
| 1465 | } |
| 1466 | tun->owner = owner; |
Jason Wang | 1e58833 | 2012-10-31 19:45:56 +0000 | [diff] [blame] | 1467 | tun_debug(KERN_INFO, tun, "owner set to %u\n", |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1468 | from_kuid(&init_user_ns, tun->owner)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | break; |
| 1470 | |
Guido Guenther | 8c64462 | 2007-07-02 22:50:25 -0700 | [diff] [blame] | 1471 | case TUNSETGROUP: |
| 1472 | /* Set group of the device */ |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1473 | group = make_kgid(current_user_ns(), arg); |
| 1474 | if (!gid_valid(group)) { |
| 1475 | ret = -EINVAL; |
| 1476 | break; |
| 1477 | } |
| 1478 | tun->group = group; |
Jason Wang | 1e58833 | 2012-10-31 19:45:56 +0000 | [diff] [blame] | 1479 | tun_debug(KERN_INFO, tun, "group set to %u\n", |
Eric W. Biederman | 0625c88 | 2012-02-07 16:48:55 -0800 | [diff] [blame] | 1480 | from_kgid(&init_user_ns, tun->group)); |
Guido Guenther | 8c64462 | 2007-07-02 22:50:25 -0700 | [diff] [blame] | 1481 | break; |
| 1482 | |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 1483 | case TUNSETLINK: |
| 1484 | /* Only allow setting the type when the interface is down */ |
| 1485 | if (tun->dev->flags & IFF_UP) { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1486 | tun_debug(KERN_INFO, tun, |
| 1487 | "Linktype set failed because interface is up\n"); |
David S. Miller | 48abfe0 | 2008-04-23 19:37:58 -0700 | [diff] [blame] | 1488 | ret = -EBUSY; |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 1489 | } else { |
| 1490 | tun->dev->type = (int) arg; |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1491 | tun_debug(KERN_INFO, tun, "linktype set to %d\n", |
| 1492 | tun->dev->type); |
David S. Miller | 48abfe0 | 2008-04-23 19:37:58 -0700 | [diff] [blame] | 1493 | ret = 0; |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 1494 | } |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1495 | break; |
Mike Kershaw | ff4cc3a | 2005-09-01 17:40:05 -0700 | [diff] [blame] | 1496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | #ifdef TUN_DEBUG |
| 1498 | case TUNSETDEBUG: |
| 1499 | tun->debug = arg; |
| 1500 | break; |
| 1501 | #endif |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1502 | case TUNSETOFFLOAD: |
Michał Mirosław | 8825537 | 2011-04-19 06:13:10 +0000 | [diff] [blame] | 1503 | ret = set_offload(tun, arg); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1504 | break; |
Rusty Russell | 5228ddc | 2008-07-03 03:46:16 -0700 | [diff] [blame] | 1505 | |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1506 | case TUNSETTXFILTER: |
| 1507 | /* Can be set only for TAPs */ |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1508 | ret = -EINVAL; |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1509 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1510 | break; |
Harvey Harrison | c0e5a8c | 2008-07-16 12:45:34 -0700 | [diff] [blame] | 1511 | ret = update_filter(&tun->txflt, (void __user *)arg); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1512 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | |
| 1514 | case SIOCGIFHWADDR: |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 1515 | /* Get hw address */ |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1516 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); |
| 1517 | ifr.ifr_hwaddr.sa_family = tun->dev->type; |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1518 | if (copy_to_user(argp, &ifr, ifreq_len)) |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1519 | ret = -EFAULT; |
| 1520 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
| 1522 | case SIOCSIFHWADDR: |
Max Krasnyansky | f271b2cc | 2008-07-14 22:18:19 -0700 | [diff] [blame] | 1523 | /* Set hw address */ |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1524 | tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", |
| 1525 | ifr.ifr_hwaddr.sa_data); |
Kim B. Heino | 4010237 | 2008-02-29 12:26:21 -0800 | [diff] [blame] | 1526 | |
Kim B. Heino | 4010237 | 2008-02-29 12:26:21 -0800 | [diff] [blame] | 1527 | ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1528 | break; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1529 | |
| 1530 | case TUNGETSNDBUF: |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1531 | sndbuf = tfile->socket.sk->sk_sndbuf; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1532 | if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) |
| 1533 | ret = -EFAULT; |
| 1534 | break; |
| 1535 | |
| 1536 | case TUNSETSNDBUF: |
| 1537 | if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { |
| 1538 | ret = -EFAULT; |
| 1539 | break; |
| 1540 | } |
| 1541 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1542 | tun->sndbuf = tfile->socket.sk->sk_sndbuf = sndbuf; |
Herbert Xu | 33dccbb | 2009-02-05 21:25:32 -0800 | [diff] [blame] | 1543 | break; |
| 1544 | |
Michael S. Tsirkin | d9d52b5 | 2010-03-17 17:45:01 +0200 | [diff] [blame] | 1545 | case TUNGETVNETHDRSZ: |
| 1546 | vnet_hdr_sz = tun->vnet_hdr_sz; |
| 1547 | if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) |
| 1548 | ret = -EFAULT; |
| 1549 | break; |
| 1550 | |
| 1551 | case TUNSETVNETHDRSZ: |
| 1552 | if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { |
| 1553 | ret = -EFAULT; |
| 1554 | break; |
| 1555 | } |
| 1556 | if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { |
| 1557 | ret = -EINVAL; |
| 1558 | break; |
| 1559 | } |
| 1560 | |
| 1561 | tun->vnet_hdr_sz = vnet_hdr_sz; |
| 1562 | break; |
| 1563 | |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 1564 | case TUNATTACHFILTER: |
| 1565 | /* Can be set only for TAPs */ |
| 1566 | ret = -EINVAL; |
| 1567 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
| 1568 | break; |
| 1569 | ret = -EFAULT; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1570 | if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 1571 | break; |
| 1572 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1573 | ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
| 1574 | if (!ret) |
| 1575 | tun->filter_attached = true; |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 1576 | break; |
| 1577 | |
| 1578 | case TUNDETACHFILTER: |
| 1579 | /* Can be set only for TAPs */ |
| 1580 | ret = -EINVAL; |
| 1581 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
| 1582 | break; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1583 | ret = sk_detach_filter(tfile->socket.sk); |
| 1584 | if (!ret) |
| 1585 | tun->filter_attached = false; |
Michael S. Tsirkin | 9940516 | 2010-02-14 01:01:10 +0000 | [diff] [blame] | 1586 | break; |
| 1587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | default: |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1589 | ret = -EINVAL; |
| 1590 | break; |
Joe Perches | ee289b6 | 2010-05-17 22:47:34 -0700 | [diff] [blame] | 1591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
Herbert Xu | 876bfd4 | 2009-08-06 14:22:44 +0000 | [diff] [blame] | 1593 | unlock: |
| 1594 | rtnl_unlock(); |
| 1595 | if (tun) |
| 1596 | tun_put(tun); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1597 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1600 | static long tun_chr_ioctl(struct file *file, |
| 1601 | unsigned int cmd, unsigned long arg) |
| 1602 | { |
| 1603 | return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); |
| 1604 | } |
| 1605 | |
| 1606 | #ifdef CONFIG_COMPAT |
| 1607 | static long tun_chr_compat_ioctl(struct file *file, |
| 1608 | unsigned int cmd, unsigned long arg) |
| 1609 | { |
| 1610 | switch (cmd) { |
| 1611 | case TUNSETIFF: |
| 1612 | case TUNGETIFF: |
| 1613 | case TUNSETTXFILTER: |
| 1614 | case TUNGETSNDBUF: |
| 1615 | case TUNSETSNDBUF: |
| 1616 | case SIOCGIFHWADDR: |
| 1617 | case SIOCSIFHWADDR: |
| 1618 | arg = (unsigned long)compat_ptr(arg); |
| 1619 | break; |
| 1620 | default: |
| 1621 | arg = (compat_ulong_t)arg; |
| 1622 | break; |
| 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | * compat_ifreq is shorter than ifreq, so we must not access beyond |
| 1627 | * the end of that structure. All fields that are used in this |
| 1628 | * driver are compatible though, we don't need to convert the |
| 1629 | * contents. |
| 1630 | */ |
| 1631 | return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); |
| 1632 | } |
| 1633 | #endif /* CONFIG_COMPAT */ |
| 1634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | static int tun_chr_fasync(int fd, struct file *file, int on) |
| 1636 | { |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1637 | struct tun_file *tfile = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | int ret; |
| 1639 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1640 | if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 1641 | goto out; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | if (on) { |
Eric W. Biederman | 609d7fa | 2006-10-02 02:17:15 -0700 | [diff] [blame] | 1644 | ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | if (ret) |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 1646 | goto out; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1647 | tfile->flags |= TUN_FASYNC; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1648 | } else |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1649 | tfile->flags &= ~TUN_FASYNC; |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 1650 | ret = 0; |
| 1651 | out: |
Jonathan Corbet | 9d31952 | 2008-06-19 15:50:37 -0600 | [diff] [blame] | 1652 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | static int tun_chr_open(struct inode *inode, struct file * file) |
| 1656 | { |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1657 | struct tun_file *tfile; |
Thomas Gleixner | deed49f | 2009-10-14 01:19:46 -0700 | [diff] [blame] | 1658 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1659 | DBG1(KERN_INFO, "tunX: tun_chr_open\n"); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1660 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1661 | tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, |
| 1662 | &tun_proto); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1663 | if (!tfile) |
| 1664 | return -ENOMEM; |
Eric W. Biederman | c70f182 | 2009-01-20 11:07:17 +0000 | [diff] [blame] | 1665 | atomic_set(&tfile->count, 0); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1666 | tfile->tun = NULL; |
Eric W. Biederman | 36b50ba | 2009-01-20 11:01:48 +0000 | [diff] [blame] | 1667 | tfile->net = get_net(current->nsproxy->net_ns); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1668 | tfile->flags = 0; |
| 1669 | |
| 1670 | rcu_assign_pointer(tfile->socket.wq, &tfile->wq); |
| 1671 | init_waitqueue_head(&tfile->wq.wait); |
| 1672 | |
| 1673 | tfile->socket.file = file; |
| 1674 | tfile->socket.ops = &tun_socket_ops; |
| 1675 | |
| 1676 | sock_init_data(&tfile->socket, &tfile->sk); |
| 1677 | sk_change_net(&tfile->sk, tfile->net); |
| 1678 | |
| 1679 | tfile->sk.sk_write_space = tun_sock_write_space; |
| 1680 | tfile->sk.sk_sndbuf = INT_MAX; |
| 1681 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1682 | file->private_data = tfile; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1683 | set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags); |
| 1684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | return 0; |
| 1686 | } |
| 1687 | |
| 1688 | static int tun_chr_close(struct inode *inode, struct file *file) |
| 1689 | { |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1690 | struct tun_file *tfile = file->private_data; |
Eric W. Biederman | f0a4d0e | 2009-06-08 00:44:31 -0700 | [diff] [blame] | 1691 | struct tun_struct *tun; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1692 | struct net *net = tfile->net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | |
Eric W. Biederman | f0a4d0e | 2009-06-08 00:44:31 -0700 | [diff] [blame] | 1694 | tun = __tun_get(tfile); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1695 | if (tun) { |
Herbert Xu | d23e436 | 2009-07-02 23:03:55 +0000 | [diff] [blame] | 1696 | struct net_device *dev = tun->dev; |
| 1697 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1698 | tun_debug(KERN_INFO, tun, "tun_chr_close\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1700 | __tun_detach(tun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 1702 | /* If desirable, unregister the netdevice. */ |
Herbert Xu | d23e436 | 2009-07-02 23:03:55 +0000 | [diff] [blame] | 1703 | if (!(tun->flags & TUN_PERSIST)) { |
| 1704 | rtnl_lock(); |
| 1705 | if (dev->reg_state == NETREG_REGISTERED) |
| 1706 | unregister_netdevice(dev); |
| 1707 | rtnl_unlock(); |
| 1708 | } |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1709 | |
| 1710 | /* drop the reference that netdevice holds */ |
| 1711 | sock_put(&tfile->sk); |
Eric W. Biederman | 631ab46 | 2009-01-20 11:00:40 +0000 | [diff] [blame] | 1712 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1714 | /* drop the reference that file holds */ |
| 1715 | BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, |
| 1716 | &tfile->socket.flags)); |
| 1717 | sk_release_kernel(&tfile->sk); |
| 1718 | put_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1723 | static const struct file_operations tun_fops = { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1724 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | .llseek = no_llseek, |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 1726 | .read = do_sync_read, |
| 1727 | .aio_read = tun_chr_aio_read, |
| 1728 | .write = do_sync_write, |
| 1729 | .aio_write = tun_chr_aio_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | .poll = tun_chr_poll, |
Arnd Bergmann | 50857e2 | 2009-11-06 22:52:32 -0800 | [diff] [blame] | 1731 | .unlocked_ioctl = tun_chr_ioctl, |
| 1732 | #ifdef CONFIG_COMPAT |
| 1733 | .compat_ioctl = tun_chr_compat_ioctl, |
| 1734 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | .open = tun_chr_open, |
| 1736 | .release = tun_chr_close, |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1737 | .fasync = tun_chr_fasync |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | }; |
| 1739 | |
| 1740 | static struct miscdevice tun_miscdev = { |
| 1741 | .minor = TUN_MINOR, |
| 1742 | .name = "tun", |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 1743 | .nodename = "net/tun", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | .fops = &tun_fops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | }; |
| 1746 | |
| 1747 | /* ethtool interface */ |
| 1748 | |
| 1749 | static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1750 | { |
| 1751 | cmd->supported = 0; |
| 1752 | cmd->advertising = 0; |
David Decotigny | 7073949 | 2011-04-27 18:32:40 +0000 | [diff] [blame] | 1753 | ethtool_cmd_speed_set(cmd, SPEED_10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | cmd->duplex = DUPLEX_FULL; |
| 1755 | cmd->port = PORT_TP; |
| 1756 | cmd->phy_address = 0; |
| 1757 | cmd->transceiver = XCVR_INTERNAL; |
| 1758 | cmd->autoneg = AUTONEG_DISABLE; |
| 1759 | cmd->maxtxpkt = 0; |
| 1760 | cmd->maxrxpkt = 0; |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
| 1764 | static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) |
| 1765 | { |
| 1766 | struct tun_struct *tun = netdev_priv(dev); |
| 1767 | |
Rick Jones | 33a5ba1 | 2011-11-15 14:59:53 +0000 | [diff] [blame] | 1768 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 1769 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | |
| 1771 | switch (tun->flags & TUN_TYPE_MASK) { |
| 1772 | case TUN_TUN_DEV: |
Rick Jones | 33a5ba1 | 2011-11-15 14:59:53 +0000 | [diff] [blame] | 1773 | strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | break; |
| 1775 | case TUN_TAP_DEV: |
Rick Jones | 33a5ba1 | 2011-11-15 14:59:53 +0000 | [diff] [blame] | 1776 | strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | break; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | static u32 tun_get_msglevel(struct net_device *dev) |
| 1782 | { |
| 1783 | #ifdef TUN_DEBUG |
| 1784 | struct tun_struct *tun = netdev_priv(dev); |
| 1785 | return tun->debug; |
| 1786 | #else |
| 1787 | return -EOPNOTSUPP; |
| 1788 | #endif |
| 1789 | } |
| 1790 | |
| 1791 | static void tun_set_msglevel(struct net_device *dev, u32 value) |
| 1792 | { |
| 1793 | #ifdef TUN_DEBUG |
| 1794 | struct tun_struct *tun = netdev_priv(dev); |
| 1795 | tun->debug = value; |
| 1796 | #endif |
| 1797 | } |
| 1798 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 1799 | static const struct ethtool_ops tun_ethtool_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | .get_settings = tun_get_settings, |
| 1801 | .get_drvinfo = tun_get_drvinfo, |
| 1802 | .get_msglevel = tun_get_msglevel, |
| 1803 | .set_msglevel = tun_set_msglevel, |
Nolan Leake | bee3136 | 2010-07-27 13:53:43 +0000 | [diff] [blame] | 1804 | .get_link = ethtool_op_get_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | }; |
| 1806 | |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 1807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | static int __init tun_init(void) |
| 1809 | { |
| 1810 | int ret = 0; |
| 1811 | |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1812 | pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); |
| 1813 | pr_info("%s\n", DRV_COPYRIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1815 | ret = rtnl_link_register(&tun_link_ops); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 1816 | if (ret) { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1817 | pr_err("Can't register link_ops\n"); |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1818 | goto err_linkops; |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | ret = misc_register(&tun_miscdev); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 1822 | if (ret) { |
Joe Perches | 6b8a66e | 2011-03-02 07:18:10 +0000 | [diff] [blame] | 1823 | pr_err("Can't register misc device %d\n", TUN_MINOR); |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 1824 | goto err_misc; |
| 1825 | } |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1826 | return 0; |
Pavel Emelyanov | 79d1760 | 2008-04-16 00:40:46 -0700 | [diff] [blame] | 1827 | err_misc: |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1828 | rtnl_link_unregister(&tun_link_ops); |
| 1829 | err_linkops: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | return ret; |
| 1831 | } |
| 1832 | |
| 1833 | static void tun_cleanup(void) |
| 1834 | { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 1835 | misc_deregister(&tun_miscdev); |
Eric W. Biederman | f019a7a | 2009-01-21 16:02:16 -0800 | [diff] [blame] | 1836 | rtnl_link_unregister(&tun_link_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1839 | /* Get an underlying socket object from tun file. Returns error unless file is |
| 1840 | * attached to a device. The returned object works like a packet socket, it |
| 1841 | * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for |
| 1842 | * holding a reference to the file for as long as the socket is in use. */ |
| 1843 | struct socket *tun_get_socket(struct file *file) |
| 1844 | { |
| 1845 | struct tun_struct *tun; |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1846 | struct tun_file *tfile = file->private_data; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1847 | if (file->f_op != &tun_fops) |
| 1848 | return ERR_PTR(-EINVAL); |
| 1849 | tun = tun_get(file); |
| 1850 | if (!tun) |
| 1851 | return ERR_PTR(-EBADFD); |
| 1852 | tun_put(tun); |
Jason Wang | 54f968d | 2012-10-31 19:45:57 +0000 | [diff] [blame^] | 1853 | return &tfile->socket; |
Michael S. Tsirkin | 05c2828 | 2010-01-14 06:17:09 +0000 | [diff] [blame] | 1854 | } |
| 1855 | EXPORT_SYMBOL_GPL(tun_get_socket); |
| 1856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | module_init(tun_init); |
| 1858 | module_exit(tun_cleanup); |
| 1859 | MODULE_DESCRIPTION(DRV_DESCRIPTION); |
| 1860 | MODULE_AUTHOR(DRV_COPYRIGHT); |
| 1861 | MODULE_LICENSE("GPL"); |
| 1862 | MODULE_ALIAS_MISCDEV(TUN_MINOR); |
Kay Sievers | 578454f | 2010-05-20 18:07:20 +0200 | [diff] [blame] | 1863 | MODULE_ALIAS("devname:net/tun"); |