]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | * | |
ff4cc3ac MK |
21 | * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 |
22 | * Add TUNSETLINK ioctl to set the link encapsulation | |
23 | * | |
1da177e4 | 24 | * Mark Smith <markzzzsmith@yahoo.com.au> |
344dc8ed | 25 | * Use eth_random_addr() for tap MAC address. |
1da177e4 LT |
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 | ||
6b8a66ee JP |
37 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
38 | ||
1da177e4 LT |
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 | ||
1da177e4 LT |
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> | |
50857e2a | 58 | #include <linux/compat.h> |
1da177e4 LT |
59 | #include <linux/if.h> |
60 | #include <linux/if_arp.h> | |
61 | #include <linux/if_ether.h> | |
62 | #include <linux/if_tun.h> | |
6680ec68 | 63 | #include <linux/if_vlan.h> |
1da177e4 | 64 | #include <linux/crc32.h> |
d647a591 | 65 | #include <linux/nsproxy.h> |
f43798c2 | 66 | #include <linux/virtio_net.h> |
99405162 | 67 | #include <linux/rcupdate.h> |
5188cd44 | 68 | #include <net/ipv6.h> |
881d966b | 69 | #include <net/net_namespace.h> |
79d17604 | 70 | #include <net/netns/generic.h> |
f019a7a5 | 71 | #include <net/rtnetlink.h> |
33dccbb0 | 72 | #include <net/sock.h> |
93e14b6d | 73 | #include <linux/seq_file.h> |
1da177e4 | 74 | |
1da177e4 LT |
75 | #include <asm/uaccess.h> |
76 | ||
14daa021 RR |
77 | /* Uncomment to enable debugging */ |
78 | /* #define TUN_DEBUG 1 */ | |
79 | ||
1da177e4 LT |
80 | #ifdef TUN_DEBUG |
81 | static int debug; | |
14daa021 | 82 | |
6b8a66ee JP |
83 | #define tun_debug(level, tun, fmt, args...) \ |
84 | do { \ | |
85 | if (tun->debug) \ | |
86 | netdev_printk(level, tun->dev, fmt, ##args); \ | |
87 | } while (0) | |
88 | #define DBG1(level, fmt, args...) \ | |
89 | do { \ | |
90 | if (debug == 2) \ | |
91 | printk(level fmt, ##args); \ | |
92 | } while (0) | |
14daa021 | 93 | #else |
6b8a66ee JP |
94 | #define tun_debug(level, tun, fmt, args...) \ |
95 | do { \ | |
96 | if (0) \ | |
97 | netdev_printk(level, tun->dev, fmt, ##args); \ | |
98 | } while (0) | |
99 | #define DBG1(level, fmt, args...) \ | |
100 | do { \ | |
101 | if (0) \ | |
102 | printk(level fmt, ##args); \ | |
103 | } while (0) | |
14daa021 RR |
104 | #endif |
105 | ||
031f5e03 MT |
106 | /* TUN device flags */ |
107 | ||
108 | /* IFF_ATTACH_QUEUE is never stored in device flags, | |
109 | * overload it to mean fasync when stored there. | |
110 | */ | |
111 | #define TUN_FASYNC IFF_ATTACH_QUEUE | |
031f5e03 MT |
112 | |
113 | #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ | |
114 | IFF_MULTI_QUEUE) | |
0690899b MT |
115 | #define GOODCOPY_LEN 128 |
116 | ||
f271b2cc MK |
117 | #define FLT_EXACT_COUNT 8 |
118 | struct tap_filter { | |
119 | unsigned int count; /* Number of addrs. Zero means disabled */ | |
120 | u32 mask[2]; /* Mask of the hashed addrs */ | |
121 | unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; | |
122 | }; | |
123 | ||
92d4ea6e | 124 | /* DEFAULT_MAX_NUM_RSS_QUEUES were chosen to let the rx/tx queues allocated for |
edfb6a14 JW |
125 | * the netdevice to be fit in one page. So we can make sure the success of |
126 | * memory allocation. TODO: increase the limit. */ | |
127 | #define MAX_TAP_QUEUES DEFAULT_MAX_NUM_RSS_QUEUES | |
b8732fb7 | 128 | #define MAX_TAP_FLOWS 4096 |
c8d68e6b | 129 | |
96442e42 JW |
130 | #define TUN_FLOW_EXPIRE (3 * HZ) |
131 | ||
54f968d6 | 132 | /* A tun_file connects an open character device to a tuntap netdevice. It |
92d4ea6e | 133 | * also contains all socket related structures (except sock_fprog and tap_filter) |
54f968d6 JW |
134 | * to serve as one transmit queue for tuntap device. The sock_fprog and |
135 | * tap_filter were kept in tun_struct since they were used for filtering for the | |
36fe8c09 | 136 | * netdevice not for a specific queue (at least I didn't see the requirement for |
54f968d6 | 137 | * this). |
6e914fc7 JW |
138 | * |
139 | * RCU usage: | |
36fe8c09 | 140 | * The tun_file and tun_struct are loosely coupled, the pointer from one to the |
6e914fc7 | 141 | * other can only be read while rcu_read_lock or rtnl_lock is held. |
54f968d6 | 142 | */ |
631ab46b | 143 | struct tun_file { |
54f968d6 JW |
144 | struct sock sk; |
145 | struct socket socket; | |
146 | struct socket_wq wq; | |
6e914fc7 | 147 | struct tun_struct __rcu *tun; |
36b50bab | 148 | struct net *net; |
54f968d6 JW |
149 | struct fasync_struct *fasync; |
150 | /* only used for fasnyc */ | |
151 | unsigned int flags; | |
fb7589a1 PE |
152 | union { |
153 | u16 queue_index; | |
154 | unsigned int ifindex; | |
155 | }; | |
4008e97f JW |
156 | struct list_head next; |
157 | struct tun_struct *detached; | |
631ab46b EB |
158 | }; |
159 | ||
96442e42 JW |
160 | struct tun_flow_entry { |
161 | struct hlist_node hash_link; | |
162 | struct rcu_head rcu; | |
163 | struct tun_struct *tun; | |
164 | ||
165 | u32 rxhash; | |
9bc88939 | 166 | u32 rps_rxhash; |
96442e42 JW |
167 | int queue_index; |
168 | unsigned long updated; | |
169 | }; | |
170 | ||
171 | #define TUN_NUM_FLOW_ENTRIES 1024 | |
172 | ||
54f968d6 | 173 | /* Since the socket were moved to tun_file, to preserve the behavior of persist |
36fe8c09 | 174 | * device, socket filter, sndbuf and vnet header size were restore when the |
54f968d6 JW |
175 | * file were attached to a persist device. |
176 | */ | |
14daa021 | 177 | struct tun_struct { |
c8d68e6b JW |
178 | struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; |
179 | unsigned int numqueues; | |
f271b2cc | 180 | unsigned int flags; |
0625c883 EB |
181 | kuid_t owner; |
182 | kgid_t group; | |
14daa021 | 183 | |
14daa021 | 184 | struct net_device *dev; |
c8f44aff | 185 | netdev_features_t set_features; |
88255375 | 186 | #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ |
3d0ad094 | 187 | NETIF_F_TSO6) |
d9d52b51 MT |
188 | |
189 | int vnet_hdr_sz; | |
54f968d6 JW |
190 | int sndbuf; |
191 | struct tap_filter txflt; | |
192 | struct sock_fprog fprog; | |
193 | /* protected by rtnl lock */ | |
194 | bool filter_attached; | |
14daa021 RR |
195 | #ifdef TUN_DEBUG |
196 | int debug; | |
1da177e4 | 197 | #endif |
96442e42 | 198 | spinlock_t lock; |
96442e42 JW |
199 | struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; |
200 | struct timer_list flow_gc_timer; | |
201 | unsigned long ageing_time; | |
4008e97f JW |
202 | unsigned int numdisabled; |
203 | struct list_head disabled; | |
5dbbaf2d | 204 | void *security; |
b8732fb7 | 205 | u32 flow_count; |
14daa021 | 206 | }; |
1da177e4 | 207 | |
96442e42 JW |
208 | static inline u32 tun_hashfn(u32 rxhash) |
209 | { | |
210 | return rxhash & 0x3ff; | |
211 | } | |
212 | ||
213 | static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) | |
214 | { | |
215 | struct tun_flow_entry *e; | |
96442e42 | 216 | |
b67bfe0d | 217 | hlist_for_each_entry_rcu(e, head, hash_link) { |
96442e42 JW |
218 | if (e->rxhash == rxhash) |
219 | return e; | |
220 | } | |
221 | return NULL; | |
222 | } | |
223 | ||
224 | static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, | |
225 | struct hlist_head *head, | |
226 | u32 rxhash, u16 queue_index) | |
227 | { | |
9fdc6bef ED |
228 | struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); |
229 | ||
96442e42 JW |
230 | if (e) { |
231 | tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", | |
232 | rxhash, queue_index); | |
233 | e->updated = jiffies; | |
234 | e->rxhash = rxhash; | |
9bc88939 | 235 | e->rps_rxhash = 0; |
96442e42 JW |
236 | e->queue_index = queue_index; |
237 | e->tun = tun; | |
238 | hlist_add_head_rcu(&e->hash_link, head); | |
b8732fb7 | 239 | ++tun->flow_count; |
96442e42 JW |
240 | } |
241 | return e; | |
242 | } | |
243 | ||
96442e42 JW |
244 | static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) |
245 | { | |
246 | tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", | |
247 | e->rxhash, e->queue_index); | |
9bc88939 | 248 | sock_rps_reset_flow_hash(e->rps_rxhash); |
96442e42 | 249 | hlist_del_rcu(&e->hash_link); |
9fdc6bef | 250 | kfree_rcu(e, rcu); |
b8732fb7 | 251 | --tun->flow_count; |
96442e42 JW |
252 | } |
253 | ||
254 | static void tun_flow_flush(struct tun_struct *tun) | |
255 | { | |
256 | int i; | |
257 | ||
258 | spin_lock_bh(&tun->lock); | |
259 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { | |
260 | struct tun_flow_entry *e; | |
b67bfe0d | 261 | struct hlist_node *n; |
96442e42 | 262 | |
b67bfe0d | 263 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) |
96442e42 JW |
264 | tun_flow_delete(tun, e); |
265 | } | |
266 | spin_unlock_bh(&tun->lock); | |
267 | } | |
268 | ||
269 | static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) | |
270 | { | |
271 | int i; | |
272 | ||
273 | spin_lock_bh(&tun->lock); | |
274 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { | |
275 | struct tun_flow_entry *e; | |
b67bfe0d | 276 | struct hlist_node *n; |
96442e42 | 277 | |
b67bfe0d | 278 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
96442e42 JW |
279 | if (e->queue_index == queue_index) |
280 | tun_flow_delete(tun, e); | |
281 | } | |
282 | } | |
283 | spin_unlock_bh(&tun->lock); | |
284 | } | |
285 | ||
286 | static void tun_flow_cleanup(unsigned long data) | |
287 | { | |
288 | struct tun_struct *tun = (struct tun_struct *)data; | |
289 | unsigned long delay = tun->ageing_time; | |
290 | unsigned long next_timer = jiffies + delay; | |
291 | unsigned long count = 0; | |
292 | int i; | |
293 | ||
294 | tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); | |
295 | ||
296 | spin_lock_bh(&tun->lock); | |
297 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { | |
298 | struct tun_flow_entry *e; | |
b67bfe0d | 299 | struct hlist_node *n; |
96442e42 | 300 | |
b67bfe0d | 301 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
96442e42 JW |
302 | unsigned long this_timer; |
303 | count++; | |
304 | this_timer = e->updated + delay; | |
305 | if (time_before_eq(this_timer, jiffies)) | |
306 | tun_flow_delete(tun, e); | |
307 | else if (time_before(this_timer, next_timer)) | |
308 | next_timer = this_timer; | |
309 | } | |
310 | } | |
311 | ||
312 | if (count) | |
313 | mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); | |
314 | spin_unlock_bh(&tun->lock); | |
315 | } | |
316 | ||
49974420 | 317 | static void tun_flow_update(struct tun_struct *tun, u32 rxhash, |
9e85722d | 318 | struct tun_file *tfile) |
96442e42 JW |
319 | { |
320 | struct hlist_head *head; | |
321 | struct tun_flow_entry *e; | |
322 | unsigned long delay = tun->ageing_time; | |
9e85722d | 323 | u16 queue_index = tfile->queue_index; |
96442e42 JW |
324 | |
325 | if (!rxhash) | |
326 | return; | |
327 | else | |
328 | head = &tun->flows[tun_hashfn(rxhash)]; | |
329 | ||
330 | rcu_read_lock(); | |
331 | ||
9e85722d JW |
332 | /* We may get a very small possibility of OOO during switching, not |
333 | * worth to optimize.*/ | |
334 | if (tun->numqueues == 1 || tfile->detached) | |
96442e42 JW |
335 | goto unlock; |
336 | ||
337 | e = tun_flow_find(head, rxhash); | |
338 | if (likely(e)) { | |
339 | /* TODO: keep queueing to old queue until it's empty? */ | |
340 | e->queue_index = queue_index; | |
341 | e->updated = jiffies; | |
9bc88939 | 342 | sock_rps_record_flow_hash(e->rps_rxhash); |
96442e42 JW |
343 | } else { |
344 | spin_lock_bh(&tun->lock); | |
b8732fb7 JW |
345 | if (!tun_flow_find(head, rxhash) && |
346 | tun->flow_count < MAX_TAP_FLOWS) | |
96442e42 JW |
347 | tun_flow_create(tun, head, rxhash, queue_index); |
348 | ||
349 | if (!timer_pending(&tun->flow_gc_timer)) | |
350 | mod_timer(&tun->flow_gc_timer, | |
351 | round_jiffies_up(jiffies + delay)); | |
352 | spin_unlock_bh(&tun->lock); | |
353 | } | |
354 | ||
355 | unlock: | |
356 | rcu_read_unlock(); | |
357 | } | |
358 | ||
9bc88939 TH |
359 | /** |
360 | * Save the hash received in the stack receive path and update the | |
361 | * flow_hash table accordingly. | |
362 | */ | |
363 | static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) | |
364 | { | |
365 | if (unlikely(e->rps_rxhash != hash)) { | |
366 | sock_rps_reset_flow_hash(e->rps_rxhash); | |
367 | e->rps_rxhash = hash; | |
368 | } | |
369 | } | |
370 | ||
c8d68e6b | 371 | /* We try to identify a flow through its rxhash first. The reason that |
92d4ea6e | 372 | * we do not check rxq no. is because some cards(e.g 82599), chooses |
c8d68e6b JW |
373 | * the rxq based on the txq where the last packet of the flow comes. As |
374 | * the userspace application move between processors, we may get a | |
375 | * different rxq no. here. If we could not get rxhash, then we would | |
376 | * hope the rxq no. may help here. | |
377 | */ | |
f663dd9a | 378 | static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, |
99932d4f | 379 | void *accel_priv, select_queue_fallback_t fallback) |
c8d68e6b JW |
380 | { |
381 | struct tun_struct *tun = netdev_priv(dev); | |
96442e42 | 382 | struct tun_flow_entry *e; |
c8d68e6b JW |
383 | u32 txq = 0; |
384 | u32 numqueues = 0; | |
385 | ||
386 | rcu_read_lock(); | |
92bb73ea | 387 | numqueues = ACCESS_ONCE(tun->numqueues); |
c8d68e6b | 388 | |
3958afa1 | 389 | txq = skb_get_hash(skb); |
c8d68e6b | 390 | if (txq) { |
96442e42 | 391 | e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); |
9bc88939 | 392 | if (e) { |
9bc88939 | 393 | tun_flow_save_rps_rxhash(e, txq); |
fbe4d456 | 394 | txq = e->queue_index; |
9bc88939 | 395 | } else |
96442e42 JW |
396 | /* use multiply and shift instead of expensive divide */ |
397 | txq = ((u64)txq * numqueues) >> 32; | |
c8d68e6b JW |
398 | } else if (likely(skb_rx_queue_recorded(skb))) { |
399 | txq = skb_get_rx_queue(skb); | |
400 | while (unlikely(txq >= numqueues)) | |
401 | txq -= numqueues; | |
402 | } | |
403 | ||
404 | rcu_read_unlock(); | |
405 | return txq; | |
406 | } | |
407 | ||
cde8b15f JW |
408 | static inline bool tun_not_capable(struct tun_struct *tun) |
409 | { | |
410 | const struct cred *cred = current_cred(); | |
c260b772 | 411 | struct net *net = dev_net(tun->dev); |
cde8b15f JW |
412 | |
413 | return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || | |
414 | (gid_valid(tun->group) && !in_egroup_p(tun->group))) && | |
c260b772 | 415 | !ns_capable(net->user_ns, CAP_NET_ADMIN); |
cde8b15f JW |
416 | } |
417 | ||
c8d68e6b JW |
418 | static void tun_set_real_num_queues(struct tun_struct *tun) |
419 | { | |
420 | netif_set_real_num_tx_queues(tun->dev, tun->numqueues); | |
421 | netif_set_real_num_rx_queues(tun->dev, tun->numqueues); | |
422 | } | |
423 | ||
4008e97f JW |
424 | static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) |
425 | { | |
426 | tfile->detached = tun; | |
427 | list_add_tail(&tfile->next, &tun->disabled); | |
428 | ++tun->numdisabled; | |
429 | } | |
430 | ||
d32649d1 | 431 | static struct tun_struct *tun_enable_queue(struct tun_file *tfile) |
4008e97f JW |
432 | { |
433 | struct tun_struct *tun = tfile->detached; | |
434 | ||
435 | tfile->detached = NULL; | |
436 | list_del_init(&tfile->next); | |
437 | --tun->numdisabled; | |
438 | return tun; | |
439 | } | |
440 | ||
4bfb0513 JW |
441 | static void tun_queue_purge(struct tun_file *tfile) |
442 | { | |
443 | skb_queue_purge(&tfile->sk.sk_receive_queue); | |
444 | skb_queue_purge(&tfile->sk.sk_error_queue); | |
445 | } | |
446 | ||
c8d68e6b JW |
447 | static void __tun_detach(struct tun_file *tfile, bool clean) |
448 | { | |
449 | struct tun_file *ntfile; | |
450 | struct tun_struct *tun; | |
c8d68e6b | 451 | |
b8deabd3 JW |
452 | tun = rtnl_dereference(tfile->tun); |
453 | ||
9e85722d | 454 | if (tun && !tfile->detached) { |
c8d68e6b JW |
455 | u16 index = tfile->queue_index; |
456 | BUG_ON(index >= tun->numqueues); | |
c8d68e6b JW |
457 | |
458 | rcu_assign_pointer(tun->tfiles[index], | |
459 | tun->tfiles[tun->numqueues - 1]); | |
b8deabd3 | 460 | ntfile = rtnl_dereference(tun->tfiles[index]); |
c8d68e6b JW |
461 | ntfile->queue_index = index; |
462 | ||
463 | --tun->numqueues; | |
9e85722d | 464 | if (clean) { |
c956674b | 465 | RCU_INIT_POINTER(tfile->tun, NULL); |
4008e97f | 466 | sock_put(&tfile->sk); |
9e85722d | 467 | } else |
4008e97f | 468 | tun_disable_queue(tun, tfile); |
c8d68e6b JW |
469 | |
470 | synchronize_net(); | |
96442e42 | 471 | tun_flow_delete_by_queue(tun, tun->numqueues + 1); |
c8d68e6b | 472 | /* Drop read queue */ |
4bfb0513 | 473 | tun_queue_purge(tfile); |
c8d68e6b | 474 | tun_set_real_num_queues(tun); |
dd38bd85 | 475 | } else if (tfile->detached && clean) { |
4008e97f | 476 | tun = tun_enable_queue(tfile); |
dd38bd85 JW |
477 | sock_put(&tfile->sk); |
478 | } | |
c8d68e6b JW |
479 | |
480 | if (clean) { | |
af668b3c MT |
481 | if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { |
482 | netif_carrier_off(tun->dev); | |
483 | ||
40630b82 | 484 | if (!(tun->flags & IFF_PERSIST) && |
af668b3c | 485 | tun->dev->reg_state == NETREG_REGISTERED) |
4008e97f | 486 | unregister_netdevice(tun->dev); |
af668b3c | 487 | } |
4008e97f | 488 | |
c8d68e6b JW |
489 | BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, |
490 | &tfile->socket.flags)); | |
491 | sk_release_kernel(&tfile->sk); | |
492 | } | |
493 | } | |
494 | ||
495 | static void tun_detach(struct tun_file *tfile, bool clean) | |
496 | { | |
497 | rtnl_lock(); | |
498 | __tun_detach(tfile, clean); | |
499 | rtnl_unlock(); | |
500 | } | |
501 | ||
502 | static void tun_detach_all(struct net_device *dev) | |
503 | { | |
504 | struct tun_struct *tun = netdev_priv(dev); | |
4008e97f | 505 | struct tun_file *tfile, *tmp; |
c8d68e6b JW |
506 | int i, n = tun->numqueues; |
507 | ||
508 | for (i = 0; i < n; i++) { | |
b8deabd3 | 509 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b | 510 | BUG_ON(!tfile); |
9e641bdc | 511 | tfile->socket.sk->sk_data_ready(tfile->socket.sk); |
c956674b | 512 | RCU_INIT_POINTER(tfile->tun, NULL); |
c8d68e6b JW |
513 | --tun->numqueues; |
514 | } | |
9e85722d | 515 | list_for_each_entry(tfile, &tun->disabled, next) { |
9e641bdc | 516 | tfile->socket.sk->sk_data_ready(tfile->socket.sk); |
c956674b | 517 | RCU_INIT_POINTER(tfile->tun, NULL); |
9e85722d | 518 | } |
c8d68e6b JW |
519 | BUG_ON(tun->numqueues != 0); |
520 | ||
521 | synchronize_net(); | |
522 | for (i = 0; i < n; i++) { | |
b8deabd3 | 523 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b | 524 | /* Drop read queue */ |
4bfb0513 | 525 | tun_queue_purge(tfile); |
c8d68e6b JW |
526 | sock_put(&tfile->sk); |
527 | } | |
4008e97f JW |
528 | list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { |
529 | tun_enable_queue(tfile); | |
4bfb0513 | 530 | tun_queue_purge(tfile); |
4008e97f JW |
531 | sock_put(&tfile->sk); |
532 | } | |
533 | BUG_ON(tun->numdisabled != 0); | |
dd38bd85 | 534 | |
40630b82 | 535 | if (tun->flags & IFF_PERSIST) |
dd38bd85 | 536 | module_put(THIS_MODULE); |
c8d68e6b JW |
537 | } |
538 | ||
849c9b6f | 539 | static int tun_attach(struct tun_struct *tun, struct file *file, bool skip_filter) |
a7385ba2 | 540 | { |
631ab46b | 541 | struct tun_file *tfile = file->private_data; |
38231b7a | 542 | int err; |
a7385ba2 | 543 | |
5dbbaf2d PM |
544 | err = security_tun_dev_attach(tfile->socket.sk, tun->security); |
545 | if (err < 0) | |
546 | goto out; | |
547 | ||
38231b7a | 548 | err = -EINVAL; |
9e85722d | 549 | if (rtnl_dereference(tfile->tun) && !tfile->detached) |
38231b7a EB |
550 | goto out; |
551 | ||
552 | err = -EBUSY; | |
40630b82 | 553 | if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) |
c8d68e6b JW |
554 | goto out; |
555 | ||
556 | err = -E2BIG; | |
4008e97f JW |
557 | if (!tfile->detached && |
558 | tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) | |
38231b7a EB |
559 | goto out; |
560 | ||
561 | err = 0; | |
54f968d6 | 562 | |
92d4ea6e | 563 | /* Re-attach the filter to persist device */ |
849c9b6f | 564 | if (!skip_filter && (tun->filter_attached == true)) { |
54f968d6 JW |
565 | err = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
566 | if (!err) | |
567 | goto out; | |
568 | } | |
c8d68e6b | 569 | tfile->queue_index = tun->numqueues; |
6e914fc7 | 570 | rcu_assign_pointer(tfile->tun, tun); |
c8d68e6b | 571 | rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); |
c8d68e6b | 572 | tun->numqueues++; |
a7385ba2 | 573 | |
4008e97f JW |
574 | if (tfile->detached) |
575 | tun_enable_queue(tfile); | |
576 | else | |
577 | sock_hold(&tfile->sk); | |
578 | ||
c8d68e6b | 579 | tun_set_real_num_queues(tun); |
a7385ba2 | 580 | |
c8d68e6b JW |
581 | /* device is allowed to go away first, so no need to hold extra |
582 | * refcnt. | |
583 | */ | |
584 | ||
585 | out: | |
586 | return err; | |
631ab46b EB |
587 | } |
588 | ||
589 | static struct tun_struct *__tun_get(struct tun_file *tfile) | |
590 | { | |
6e914fc7 | 591 | struct tun_struct *tun; |
c70f1829 | 592 | |
6e914fc7 JW |
593 | rcu_read_lock(); |
594 | tun = rcu_dereference(tfile->tun); | |
595 | if (tun) | |
596 | dev_hold(tun->dev); | |
597 | rcu_read_unlock(); | |
c70f1829 EB |
598 | |
599 | return tun; | |
631ab46b EB |
600 | } |
601 | ||
602 | static struct tun_struct *tun_get(struct file *file) | |
603 | { | |
604 | return __tun_get(file->private_data); | |
605 | } | |
606 | ||
607 | static void tun_put(struct tun_struct *tun) | |
608 | { | |
6e914fc7 | 609 | dev_put(tun->dev); |
631ab46b EB |
610 | } |
611 | ||
6b8a66ee | 612 | /* TAP filtering */ |
f271b2cc MK |
613 | static void addr_hash_set(u32 *mask, const u8 *addr) |
614 | { | |
615 | int n = ether_crc(ETH_ALEN, addr) >> 26; | |
616 | mask[n >> 5] |= (1 << (n & 31)); | |
617 | } | |
618 | ||
619 | static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) | |
620 | { | |
621 | int n = ether_crc(ETH_ALEN, addr) >> 26; | |
622 | return mask[n >> 5] & (1 << (n & 31)); | |
623 | } | |
624 | ||
625 | static int update_filter(struct tap_filter *filter, void __user *arg) | |
626 | { | |
627 | struct { u8 u[ETH_ALEN]; } *addr; | |
628 | struct tun_filter uf; | |
629 | int err, alen, n, nexact; | |
630 | ||
631 | if (copy_from_user(&uf, arg, sizeof(uf))) | |
632 | return -EFAULT; | |
633 | ||
634 | if (!uf.count) { | |
635 | /* Disabled */ | |
636 | filter->count = 0; | |
637 | return 0; | |
638 | } | |
639 | ||
640 | alen = ETH_ALEN * uf.count; | |
641 | addr = kmalloc(alen, GFP_KERNEL); | |
642 | if (!addr) | |
643 | return -ENOMEM; | |
644 | ||
645 | if (copy_from_user(addr, arg + sizeof(uf), alen)) { | |
646 | err = -EFAULT; | |
647 | goto done; | |
648 | } | |
649 | ||
650 | /* The filter is updated without holding any locks. Which is | |
651 | * perfectly safe. We disable it first and in the worst | |
652 | * case we'll accept a few undesired packets. */ | |
653 | filter->count = 0; | |
654 | wmb(); | |
655 | ||
656 | /* Use first set of addresses as an exact filter */ | |
657 | for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) | |
658 | memcpy(filter->addr[n], addr[n].u, ETH_ALEN); | |
659 | ||
660 | nexact = n; | |
661 | ||
cfbf84fc AW |
662 | /* Remaining multicast addresses are hashed, |
663 | * unicast will leave the filter disabled. */ | |
f271b2cc | 664 | memset(filter->mask, 0, sizeof(filter->mask)); |
cfbf84fc AW |
665 | for (; n < uf.count; n++) { |
666 | if (!is_multicast_ether_addr(addr[n].u)) { | |
667 | err = 0; /* no filter */ | |
668 | goto done; | |
669 | } | |
f271b2cc | 670 | addr_hash_set(filter->mask, addr[n].u); |
cfbf84fc | 671 | } |
f271b2cc MK |
672 | |
673 | /* For ALLMULTI just set the mask to all ones. | |
674 | * This overrides the mask populated above. */ | |
675 | if ((uf.flags & TUN_FLT_ALLMULTI)) | |
676 | memset(filter->mask, ~0, sizeof(filter->mask)); | |
677 | ||
678 | /* Now enable the filter */ | |
679 | wmb(); | |
680 | filter->count = nexact; | |
681 | ||
682 | /* Return the number of exact filters */ | |
683 | err = nexact; | |
684 | ||
685 | done: | |
686 | kfree(addr); | |
687 | return err; | |
688 | } | |
689 | ||
690 | /* Returns: 0 - drop, !=0 - accept */ | |
691 | static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) | |
692 | { | |
693 | /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect | |
694 | * at this point. */ | |
695 | struct ethhdr *eh = (struct ethhdr *) skb->data; | |
696 | int i; | |
697 | ||
698 | /* Exact match */ | |
699 | for (i = 0; i < filter->count; i++) | |
2e42e474 | 700 | if (ether_addr_equal(eh->h_dest, filter->addr[i])) |
f271b2cc MK |
701 | return 1; |
702 | ||
703 | /* Inexact match (multicast only) */ | |
704 | if (is_multicast_ether_addr(eh->h_dest)) | |
705 | return addr_hash_test(filter->mask, eh->h_dest); | |
706 | ||
707 | return 0; | |
708 | } | |
709 | ||
710 | /* | |
711 | * Checks whether the packet is accepted or not. | |
712 | * Returns: 0 - drop, !=0 - accept | |
713 | */ | |
714 | static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) | |
715 | { | |
716 | if (!filter->count) | |
717 | return 1; | |
718 | ||
719 | return run_filter(filter, skb); | |
720 | } | |
721 | ||
1da177e4 LT |
722 | /* Network device part of the driver */ |
723 | ||
7282d491 | 724 | static const struct ethtool_ops tun_ethtool_ops; |
1da177e4 | 725 | |
c70f1829 EB |
726 | /* Net device detach from fd. */ |
727 | static void tun_net_uninit(struct net_device *dev) | |
728 | { | |
c8d68e6b | 729 | tun_detach_all(dev); |
c70f1829 EB |
730 | } |
731 | ||
1da177e4 LT |
732 | /* Net device open. */ |
733 | static int tun_net_open(struct net_device *dev) | |
734 | { | |
c8d68e6b | 735 | netif_tx_start_all_queues(dev); |
1da177e4 LT |
736 | return 0; |
737 | } | |
738 | ||
739 | /* Net device close. */ | |
740 | static int tun_net_close(struct net_device *dev) | |
741 | { | |
c8d68e6b | 742 | netif_tx_stop_all_queues(dev); |
1da177e4 LT |
743 | return 0; |
744 | } | |
745 | ||
746 | /* Net device start xmit */ | |
424efe9c | 747 | static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
748 | { |
749 | struct tun_struct *tun = netdev_priv(dev); | |
c8d68e6b | 750 | int txq = skb->queue_mapping; |
6e914fc7 | 751 | struct tun_file *tfile; |
fa35864e | 752 | u32 numqueues = 0; |
1da177e4 | 753 | |
6e914fc7 | 754 | rcu_read_lock(); |
c8d68e6b | 755 | tfile = rcu_dereference(tun->tfiles[txq]); |
fa35864e | 756 | numqueues = ACCESS_ONCE(tun->numqueues); |
c8d68e6b | 757 | |
1da177e4 | 758 | /* Drop packet if interface is not attached */ |
fa35864e | 759 | if (txq >= numqueues) |
1da177e4 LT |
760 | goto drop; |
761 | ||
fa35864e | 762 | if (numqueues == 1) { |
9bc88939 TH |
763 | /* Select queue was not called for the skbuff, so we extract the |
764 | * RPS hash and save it into the flow_table here. | |
765 | */ | |
766 | __u32 rxhash; | |
767 | ||
768 | rxhash = skb_get_hash(skb); | |
769 | if (rxhash) { | |
770 | struct tun_flow_entry *e; | |
771 | e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], | |
772 | rxhash); | |
773 | if (e) | |
774 | tun_flow_save_rps_rxhash(e, rxhash); | |
775 | } | |
776 | } | |
777 | ||
6e914fc7 JW |
778 | tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); |
779 | ||
c8d68e6b JW |
780 | BUG_ON(!tfile); |
781 | ||
f271b2cc MK |
782 | /* Drop if the filter does not like it. |
783 | * This is a noop if the filter is disabled. | |
784 | * Filter can be enabled only for the TAP devices. */ | |
785 | if (!check_filter(&tun->txflt, skb)) | |
786 | goto drop; | |
787 | ||
54f968d6 JW |
788 | if (tfile->socket.sk->sk_filter && |
789 | sk_filter(tfile->socket.sk, skb)) | |
99405162 MT |
790 | goto drop; |
791 | ||
36fe8c09 | 792 | /* Limit the number of packets queued by dividing txq length with the |
c8d68e6b JW |
793 | * number of queues. |
794 | */ | |
fa35864e DC |
795 | if (skb_queue_len(&tfile->socket.sk->sk_receive_queue) * numqueues |
796 | >= dev->tx_queue_len) | |
5d097109 | 797 | goto drop; |
1da177e4 | 798 | |
7bf66305 JW |
799 | if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) |
800 | goto drop; | |
801 | ||
eda29772 RC |
802 | if (skb->sk) { |
803 | sock_tx_timestamp(skb->sk, &skb_shinfo(skb)->tx_flags); | |
804 | sw_tx_timestamp(skb); | |
805 | } | |
806 | ||
0110d6f2 | 807 | /* Orphan the skb - required as we might hang on to it |
7bf66305 JW |
808 | * for indefinite time. |
809 | */ | |
0110d6f2 MT |
810 | skb_orphan(skb); |
811 | ||
f8af75f3 ED |
812 | nf_reset(skb); |
813 | ||
f271b2cc | 814 | /* Enqueue packet */ |
54f968d6 | 815 | skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb); |
1da177e4 LT |
816 | |
817 | /* Notify and wake up reader process */ | |
54f968d6 JW |
818 | if (tfile->flags & TUN_FASYNC) |
819 | kill_fasync(&tfile->fasync, SIGIO, POLL_IN); | |
9e641bdc | 820 | tfile->socket.sk->sk_data_ready(tfile->socket.sk); |
6e914fc7 JW |
821 | |
822 | rcu_read_unlock(); | |
6ed10654 | 823 | return NETDEV_TX_OK; |
1da177e4 LT |
824 | |
825 | drop: | |
09f75cd7 | 826 | dev->stats.tx_dropped++; |
149d36f7 | 827 | skb_tx_error(skb); |
1da177e4 | 828 | kfree_skb(skb); |
6e914fc7 | 829 | rcu_read_unlock(); |
6ed10654 | 830 | return NETDEV_TX_OK; |
1da177e4 LT |
831 | } |
832 | ||
f271b2cc | 833 | static void tun_net_mclist(struct net_device *dev) |
1da177e4 | 834 | { |
f271b2cc MK |
835 | /* |
836 | * This callback is supposed to deal with mc filter in | |
837 | * _rx_ path and has nothing to do with the _tx_ path. | |
838 | * In rx path we always accept everything userspace gives us. | |
839 | */ | |
1da177e4 LT |
840 | } |
841 | ||
4885a504 ES |
842 | #define MIN_MTU 68 |
843 | #define MAX_MTU 65535 | |
844 | ||
845 | static int | |
846 | tun_net_change_mtu(struct net_device *dev, int new_mtu) | |
847 | { | |
848 | if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) | |
849 | return -EINVAL; | |
850 | dev->mtu = new_mtu; | |
851 | return 0; | |
852 | } | |
853 | ||
c8f44aff MM |
854 | static netdev_features_t tun_net_fix_features(struct net_device *dev, |
855 | netdev_features_t features) | |
88255375 MM |
856 | { |
857 | struct tun_struct *tun = netdev_priv(dev); | |
858 | ||
859 | return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); | |
860 | } | |
bebd097a NH |
861 | #ifdef CONFIG_NET_POLL_CONTROLLER |
862 | static void tun_poll_controller(struct net_device *dev) | |
863 | { | |
864 | /* | |
865 | * Tun only receives frames when: | |
866 | * 1) the char device endpoint gets data from user space | |
867 | * 2) the tun socket gets a sendmsg call from user space | |
92d4ea6e | 868 | * Since both of those are synchronous operations, we are guaranteed |
bebd097a | 869 | * never to have pending data when we poll for it |
92d4ea6e | 870 | * so there is nothing to do here but return. |
bebd097a NH |
871 | * We need this though so netpoll recognizes us as an interface that |
872 | * supports polling, which enables bridge devices in virt setups to | |
873 | * still use netconsole | |
874 | */ | |
875 | return; | |
876 | } | |
877 | #endif | |
758e43b7 | 878 | static const struct net_device_ops tun_netdev_ops = { |
c70f1829 | 879 | .ndo_uninit = tun_net_uninit, |
758e43b7 SH |
880 | .ndo_open = tun_net_open, |
881 | .ndo_stop = tun_net_close, | |
00829823 | 882 | .ndo_start_xmit = tun_net_xmit, |
758e43b7 | 883 | .ndo_change_mtu = tun_net_change_mtu, |
88255375 | 884 | .ndo_fix_features = tun_net_fix_features, |
c8d68e6b | 885 | .ndo_select_queue = tun_select_queue, |
bebd097a NH |
886 | #ifdef CONFIG_NET_POLL_CONTROLLER |
887 | .ndo_poll_controller = tun_poll_controller, | |
888 | #endif | |
758e43b7 SH |
889 | }; |
890 | ||
891 | static const struct net_device_ops tap_netdev_ops = { | |
c70f1829 | 892 | .ndo_uninit = tun_net_uninit, |
758e43b7 SH |
893 | .ndo_open = tun_net_open, |
894 | .ndo_stop = tun_net_close, | |
00829823 | 895 | .ndo_start_xmit = tun_net_xmit, |
758e43b7 | 896 | .ndo_change_mtu = tun_net_change_mtu, |
88255375 | 897 | .ndo_fix_features = tun_net_fix_features, |
afc4b13d | 898 | .ndo_set_rx_mode = tun_net_mclist, |
758e43b7 SH |
899 | .ndo_set_mac_address = eth_mac_addr, |
900 | .ndo_validate_addr = eth_validate_addr, | |
c8d68e6b | 901 | .ndo_select_queue = tun_select_queue, |
bebd097a NH |
902 | #ifdef CONFIG_NET_POLL_CONTROLLER |
903 | .ndo_poll_controller = tun_poll_controller, | |
904 | #endif | |
758e43b7 SH |
905 | }; |
906 | ||
944a1376 | 907 | static void tun_flow_init(struct tun_struct *tun) |
96442e42 JW |
908 | { |
909 | int i; | |
910 | ||
96442e42 JW |
911 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) |
912 | INIT_HLIST_HEAD(&tun->flows[i]); | |
913 | ||
914 | tun->ageing_time = TUN_FLOW_EXPIRE; | |
915 | setup_timer(&tun->flow_gc_timer, tun_flow_cleanup, (unsigned long)tun); | |
916 | mod_timer(&tun->flow_gc_timer, | |
917 | round_jiffies_up(jiffies + tun->ageing_time)); | |
96442e42 JW |
918 | } |
919 | ||
920 | static void tun_flow_uninit(struct tun_struct *tun) | |
921 | { | |
922 | del_timer_sync(&tun->flow_gc_timer); | |
923 | tun_flow_flush(tun); | |
96442e42 JW |
924 | } |
925 | ||
1da177e4 LT |
926 | /* Initialize net device. */ |
927 | static void tun_net_init(struct net_device *dev) | |
928 | { | |
929 | struct tun_struct *tun = netdev_priv(dev); | |
6aa20a22 | 930 | |
1da177e4 | 931 | switch (tun->flags & TUN_TYPE_MASK) { |
40630b82 | 932 | case IFF_TUN: |
758e43b7 SH |
933 | dev->netdev_ops = &tun_netdev_ops; |
934 | ||
1da177e4 LT |
935 | /* Point-to-Point TUN Device */ |
936 | dev->hard_header_len = 0; | |
937 | dev->addr_len = 0; | |
938 | dev->mtu = 1500; | |
939 | ||
940 | /* Zero header length */ | |
6aa20a22 | 941 | dev->type = ARPHRD_NONE; |
1da177e4 LT |
942 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
943 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ | |
944 | break; | |
945 | ||
40630b82 | 946 | case IFF_TAP: |
7a0a9608 | 947 | dev->netdev_ops = &tap_netdev_ops; |
1da177e4 | 948 | /* Ethernet TAP Device */ |
1da177e4 | 949 | ether_setup(dev); |
550fd08c | 950 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
a676847b | 951 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
36226a8d | 952 | |
f2cedb63 | 953 | eth_hw_addr_random(dev); |
36226a8d | 954 | |
1da177e4 LT |
955 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ |
956 | break; | |
957 | } | |
958 | } | |
959 | ||
960 | /* Character device part */ | |
961 | ||
962 | /* Poll */ | |
c8d68e6b | 963 | static unsigned int tun_chr_poll(struct file *file, poll_table *wait) |
6aa20a22 | 964 | { |
b2430de3 EB |
965 | struct tun_file *tfile = file->private_data; |
966 | struct tun_struct *tun = __tun_get(tfile); | |
3c8a9c63 | 967 | struct sock *sk; |
33dccbb0 | 968 | unsigned int mask = 0; |
1da177e4 LT |
969 | |
970 | if (!tun) | |
eac9e902 | 971 | return POLLERR; |
1da177e4 | 972 | |
54f968d6 | 973 | sk = tfile->socket.sk; |
3c8a9c63 | 974 | |
6b8a66ee | 975 | tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); |
1da177e4 | 976 | |
9e641bdc | 977 | poll_wait(file, sk_sleep(sk), wait); |
6aa20a22 | 978 | |
89f56d1e | 979 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
1da177e4 LT |
980 | mask |= POLLIN | POLLRDNORM; |
981 | ||
33dccbb0 HX |
982 | if (sock_writeable(sk) || |
983 | (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) && | |
984 | sock_writeable(sk))) | |
985 | mask |= POLLOUT | POLLWRNORM; | |
986 | ||
c70f1829 EB |
987 | if (tun->dev->reg_state != NETREG_REGISTERED) |
988 | mask = POLLERR; | |
989 | ||
631ab46b | 990 | tun_put(tun); |
1da177e4 LT |
991 | return mask; |
992 | } | |
993 | ||
f42157cb RR |
994 | /* prepad is the amount to reserve at front. len is length after that. |
995 | * linear is a hint as to how much to copy (usually headers). */ | |
54f968d6 | 996 | static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, |
6f7c156c | 997 | size_t prepad, size_t len, |
998 | size_t linear, int noblock) | |
f42157cb | 999 | { |
54f968d6 | 1000 | struct sock *sk = tfile->socket.sk; |
f42157cb | 1001 | struct sk_buff *skb; |
33dccbb0 | 1002 | int err; |
f42157cb RR |
1003 | |
1004 | /* Under a page? Don't bother with paged skb. */ | |
0eca93bc | 1005 | if (prepad + len < PAGE_SIZE || !linear) |
33dccbb0 | 1006 | linear = len; |
f42157cb | 1007 | |
33dccbb0 | 1008 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, |
28d64271 | 1009 | &err, 0); |
f42157cb | 1010 | if (!skb) |
33dccbb0 | 1011 | return ERR_PTR(err); |
f42157cb RR |
1012 | |
1013 | skb_reserve(skb, prepad); | |
1014 | skb_put(skb, linear); | |
33dccbb0 HX |
1015 | skb->data_len = len - linear; |
1016 | skb->len += len - linear; | |
f42157cb RR |
1017 | |
1018 | return skb; | |
1019 | } | |
1020 | ||
1da177e4 | 1021 | /* Get packet from user space buffer */ |
54f968d6 JW |
1022 | static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, |
1023 | void *msg_control, const struct iovec *iv, | |
1024 | size_t total_len, size_t count, int noblock) | |
1da177e4 | 1025 | { |
09640e63 | 1026 | struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; |
1da177e4 | 1027 | struct sk_buff *skb; |
3dd5c330 | 1028 | size_t len = total_len, align = NET_SKB_PAD, linear; |
f43798c2 | 1029 | struct virtio_net_hdr gso = { 0 }; |
96f8d9ec | 1030 | int good_linear; |
6f26c9a7 | 1031 | int offset = 0; |
0690899b MT |
1032 | int copylen; |
1033 | bool zerocopy = false; | |
1034 | int err; | |
49974420 | 1035 | u32 rxhash; |
1da177e4 | 1036 | |
40630b82 | 1037 | if (!(tun->flags & IFF_NO_PI)) { |
15718ea0 | 1038 | if (len < sizeof(pi)) |
1da177e4 | 1039 | return -EINVAL; |
15718ea0 | 1040 | len -= sizeof(pi); |
1da177e4 | 1041 | |
6f26c9a7 | 1042 | if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi))) |
1da177e4 | 1043 | return -EFAULT; |
6f26c9a7 | 1044 | offset += sizeof(pi); |
1da177e4 LT |
1045 | } |
1046 | ||
40630b82 | 1047 | if (tun->flags & IFF_VNET_HDR) { |
15718ea0 | 1048 | if (len < tun->vnet_hdr_sz) |
f43798c2 | 1049 | return -EINVAL; |
15718ea0 | 1050 | len -= tun->vnet_hdr_sz; |
f43798c2 | 1051 | |
6f26c9a7 | 1052 | if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso))) |
f43798c2 RR |
1053 | return -EFAULT; |
1054 | ||
4909122f HX |
1055 | if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && |
1056 | gso.csum_start + gso.csum_offset + 2 > gso.hdr_len) | |
1057 | gso.hdr_len = gso.csum_start + gso.csum_offset + 2; | |
1058 | ||
f43798c2 RR |
1059 | if (gso.hdr_len > len) |
1060 | return -EINVAL; | |
d9d52b51 | 1061 | offset += tun->vnet_hdr_sz; |
f43798c2 RR |
1062 | } |
1063 | ||
40630b82 | 1064 | if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { |
a504b86e | 1065 | align += NET_IP_ALIGN; |
0eca93bc HX |
1066 | if (unlikely(len < ETH_HLEN || |
1067 | (gso.hdr_len && gso.hdr_len < ETH_HLEN))) | |
e01bf1c8 RR |
1068 | return -EINVAL; |
1069 | } | |
6aa20a22 | 1070 | |
96f8d9ec JW |
1071 | good_linear = SKB_MAX_HEAD(align); |
1072 | ||
88529176 JW |
1073 | if (msg_control) { |
1074 | /* There are 256 bytes to be copied in skb, so there is | |
1075 | * enough room for skb expand head in case it is used. | |
0690899b MT |
1076 | * The rest of the buffer is mapped from userspace. |
1077 | */ | |
88529176 | 1078 | copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN; |
96f8d9ec JW |
1079 | if (copylen > good_linear) |
1080 | copylen = good_linear; | |
3dd5c330 | 1081 | linear = copylen; |
88529176 JW |
1082 | if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS) |
1083 | zerocopy = true; | |
1084 | } | |
1085 | ||
1086 | if (!zerocopy) { | |
0690899b | 1087 | copylen = len; |
96f8d9ec JW |
1088 | if (gso.hdr_len > good_linear) |
1089 | linear = good_linear; | |
1090 | else | |
1091 | linear = gso.hdr_len; | |
3dd5c330 | 1092 | } |
0690899b | 1093 | |
3dd5c330 | 1094 | skb = tun_alloc_skb(tfile, align, copylen, linear, noblock); |
33dccbb0 HX |
1095 | if (IS_ERR(skb)) { |
1096 | if (PTR_ERR(skb) != -EAGAIN) | |
1097 | tun->dev->stats.rx_dropped++; | |
1098 | return PTR_ERR(skb); | |
1da177e4 LT |
1099 | } |
1100 | ||
0690899b MT |
1101 | if (zerocopy) |
1102 | err = zerocopy_sg_from_iovec(skb, iv, offset, count); | |
88529176 | 1103 | else { |
0690899b | 1104 | err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len); |
88529176 JW |
1105 | if (!err && msg_control) { |
1106 | struct ubuf_info *uarg = msg_control; | |
1107 | uarg->callback(uarg, false); | |
1108 | } | |
1109 | } | |
0690899b MT |
1110 | |
1111 | if (err) { | |
09f75cd7 | 1112 | tun->dev->stats.rx_dropped++; |
8f22757e | 1113 | kfree_skb(skb); |
1da177e4 | 1114 | return -EFAULT; |
8f22757e | 1115 | } |
1da177e4 | 1116 | |
f43798c2 RR |
1117 | if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
1118 | if (!skb_partial_csum_set(skb, gso.csum_start, | |
1119 | gso.csum_offset)) { | |
1120 | tun->dev->stats.rx_frame_errors++; | |
1121 | kfree_skb(skb); | |
1122 | return -EINVAL; | |
1123 | } | |
88255375 | 1124 | } |
f43798c2 | 1125 | |
1da177e4 | 1126 | switch (tun->flags & TUN_TYPE_MASK) { |
40630b82 MT |
1127 | case IFF_TUN: |
1128 | if (tun->flags & IFF_NO_PI) { | |
f09f7ee2 AWC |
1129 | switch (skb->data[0] & 0xf0) { |
1130 | case 0x40: | |
1131 | pi.proto = htons(ETH_P_IP); | |
1132 | break; | |
1133 | case 0x60: | |
1134 | pi.proto = htons(ETH_P_IPV6); | |
1135 | break; | |
1136 | default: | |
1137 | tun->dev->stats.rx_dropped++; | |
1138 | kfree_skb(skb); | |
1139 | return -EINVAL; | |
1140 | } | |
1141 | } | |
1142 | ||
459a98ed | 1143 | skb_reset_mac_header(skb); |
1da177e4 | 1144 | skb->protocol = pi.proto; |
4c13eb66 | 1145 | skb->dev = tun->dev; |
1da177e4 | 1146 | break; |
40630b82 | 1147 | case IFF_TAP: |
1da177e4 LT |
1148 | skb->protocol = eth_type_trans(skb, tun->dev); |
1149 | break; | |
6403eab1 | 1150 | } |
1da177e4 | 1151 | |
5188cd44 BH |
1152 | skb_reset_network_header(skb); |
1153 | ||
f43798c2 RR |
1154 | if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
1155 | pr_debug("GSO!\n"); | |
1156 | switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { | |
1157 | case VIRTIO_NET_HDR_GSO_TCPV4: | |
c9af6db4 | 1158 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
f43798c2 RR |
1159 | break; |
1160 | case VIRTIO_NET_HDR_GSO_TCPV6: | |
c9af6db4 | 1161 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
f43798c2 | 1162 | break; |
e36aa25a | 1163 | case VIRTIO_NET_HDR_GSO_UDP: |
3d0ad094 BH |
1164 | { |
1165 | static bool warned; | |
1166 | ||
1167 | if (!warned) { | |
1168 | warned = true; | |
1169 | netdev_warn(tun->dev, | |
1170 | "%s: using disabled UFO feature; please fix this program\n", | |
1171 | current->comm); | |
1172 | } | |
c9af6db4 | 1173 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; |
5188cd44 BH |
1174 | if (skb->protocol == htons(ETH_P_IPV6)) |
1175 | ipv6_proxy_select_ident(skb); | |
e36aa25a | 1176 | break; |
3d0ad094 | 1177 | } |
f43798c2 RR |
1178 | default: |
1179 | tun->dev->stats.rx_frame_errors++; | |
1180 | kfree_skb(skb); | |
1181 | return -EINVAL; | |
1182 | } | |
1183 | ||
1184 | if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN) | |
c9af6db4 | 1185 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; |
f43798c2 RR |
1186 | |
1187 | skb_shinfo(skb)->gso_size = gso.gso_size; | |
1188 | if (skb_shinfo(skb)->gso_size == 0) { | |
1189 | tun->dev->stats.rx_frame_errors++; | |
1190 | kfree_skb(skb); | |
1191 | return -EINVAL; | |
1192 | } | |
1193 | ||
1194 | /* Header must be checked, and gso_segs computed. */ | |
1195 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
1196 | skb_shinfo(skb)->gso_segs = 0; | |
1197 | } | |
6aa20a22 | 1198 | |
0690899b MT |
1199 | /* copy skb_ubuf_info for callback when skb has no error */ |
1200 | if (zerocopy) { | |
1201 | skb_shinfo(skb)->destructor_arg = msg_control; | |
1202 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; | |
c9af6db4 | 1203 | skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; |
0690899b MT |
1204 | } |
1205 | ||
40893fd0 | 1206 | skb_probe_transport_header(skb, 0); |
38502af7 | 1207 | |
3958afa1 | 1208 | rxhash = skb_get_hash(skb); |
1da177e4 | 1209 | netif_rx_ni(skb); |
6aa20a22 | 1210 | |
09f75cd7 JG |
1211 | tun->dev->stats.rx_packets++; |
1212 | tun->dev->stats.rx_bytes += len; | |
1da177e4 | 1213 | |
9e85722d | 1214 | tun_flow_update(tun, rxhash, tfile); |
0690899b | 1215 | return total_len; |
6aa20a22 | 1216 | } |
1da177e4 | 1217 | |
ee0b3e67 BP |
1218 | static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, |
1219 | unsigned long count, loff_t pos) | |
1da177e4 | 1220 | { |
33dccbb0 | 1221 | struct file *file = iocb->ki_filp; |
ab46d779 | 1222 | struct tun_struct *tun = tun_get(file); |
54f968d6 | 1223 | struct tun_file *tfile = file->private_data; |
631ab46b | 1224 | ssize_t result; |
1da177e4 LT |
1225 | |
1226 | if (!tun) | |
1227 | return -EBADFD; | |
1228 | ||
6b8a66ee | 1229 | tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count); |
1da177e4 | 1230 | |
54f968d6 JW |
1231 | result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count), |
1232 | count, file->f_flags & O_NONBLOCK); | |
631ab46b EB |
1233 | |
1234 | tun_put(tun); | |
1235 | return result; | |
1da177e4 LT |
1236 | } |
1237 | ||
1da177e4 | 1238 | /* Put packet to the user space buffer */ |
6f7c156c | 1239 | static ssize_t tun_put_user(struct tun_struct *tun, |
54f968d6 | 1240 | struct tun_file *tfile, |
6f7c156c | 1241 | struct sk_buff *skb, |
1242 | const struct iovec *iv, int len) | |
1da177e4 LT |
1243 | { |
1244 | struct tun_pi pi = { 0, skb->protocol }; | |
1245 | ssize_t total = 0; | |
e6fd07c8 | 1246 | int vlan_offset = 0, copied; |
a8f9bfdf | 1247 | int vlan_hlen = 0; |
2eb783c4 | 1248 | int vnet_hdr_sz = 0; |
a8f9bfdf HX |
1249 | |
1250 | if (vlan_tx_tag_present(skb)) | |
1251 | vlan_hlen = VLAN_HLEN; | |
1da177e4 | 1252 | |
40630b82 | 1253 | if (tun->flags & IFF_VNET_HDR) |
2eb783c4 HX |
1254 | vnet_hdr_sz = tun->vnet_hdr_sz; |
1255 | ||
40630b82 | 1256 | if (!(tun->flags & IFF_NO_PI)) { |
1da177e4 LT |
1257 | if ((len -= sizeof(pi)) < 0) |
1258 | return -EINVAL; | |
1259 | ||
2eb783c4 | 1260 | if (len < skb->len + vlan_hlen + vnet_hdr_sz) { |
1da177e4 LT |
1261 | /* Packet will be striped */ |
1262 | pi.flags |= TUN_PKT_STRIP; | |
1263 | } | |
6aa20a22 | 1264 | |
43b39dcd | 1265 | if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi))) |
1da177e4 LT |
1266 | return -EFAULT; |
1267 | total += sizeof(pi); | |
6aa20a22 | 1268 | } |
1da177e4 | 1269 | |
2eb783c4 | 1270 | if (vnet_hdr_sz) { |
f43798c2 | 1271 | struct virtio_net_hdr gso = { 0 }; /* no info leak */ |
2eb783c4 | 1272 | if ((len -= vnet_hdr_sz) < 0) |
f43798c2 RR |
1273 | return -EINVAL; |
1274 | ||
1275 | if (skb_is_gso(skb)) { | |
1276 | struct skb_shared_info *sinfo = skb_shinfo(skb); | |
1277 | ||
1278 | /* This is a hint as to how much should be linear. */ | |
1279 | gso.hdr_len = skb_headlen(skb); | |
1280 | gso.gso_size = sinfo->gso_size; | |
1281 | if (sinfo->gso_type & SKB_GSO_TCPV4) | |
1282 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; | |
1283 | else if (sinfo->gso_type & SKB_GSO_TCPV6) | |
1284 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; | |
ef3db4a5 | 1285 | else { |
6b8a66ee | 1286 | pr_err("unexpected GSO type: " |
ef3db4a5 MT |
1287 | "0x%x, gso_size %d, hdr_len %d\n", |
1288 | sinfo->gso_type, gso.gso_size, | |
1289 | gso.hdr_len); | |
1290 | print_hex_dump(KERN_ERR, "tun: ", | |
1291 | DUMP_PREFIX_NONE, | |
1292 | 16, 1, skb->head, | |
1293 | min((int)gso.hdr_len, 64), true); | |
1294 | WARN_ON_ONCE(1); | |
1295 | return -EINVAL; | |
1296 | } | |
f43798c2 RR |
1297 | if (sinfo->gso_type & SKB_GSO_TCP_ECN) |
1298 | gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN; | |
1299 | } else | |
1300 | gso.gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
1301 | ||
1302 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
1303 | gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; | |
a8f9bfdf HX |
1304 | gso.csum_start = skb_checksum_start_offset(skb) + |
1305 | vlan_hlen; | |
f43798c2 | 1306 | gso.csum_offset = skb->csum_offset; |
10a8d94a JW |
1307 | } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) { |
1308 | gso.flags = VIRTIO_NET_HDR_F_DATA_VALID; | |
f43798c2 RR |
1309 | } /* else everything is zero */ |
1310 | ||
43b39dcd MT |
1311 | if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total, |
1312 | sizeof(gso)))) | |
f43798c2 | 1313 | return -EFAULT; |
2eb783c4 | 1314 | total += vnet_hdr_sz; |
f43798c2 RR |
1315 | } |
1316 | ||
e6fd07c8 | 1317 | copied = total; |
a8f9bfdf HX |
1318 | len = min_t(int, skb->len + vlan_hlen, len); |
1319 | total += skb->len + vlan_hlen; | |
1320 | if (vlan_hlen) { | |
6680ec68 JW |
1321 | int copy, ret; |
1322 | struct { | |
1323 | __be16 h_vlan_proto; | |
1324 | __be16 h_vlan_TCI; | |
1325 | } veth; | |
1326 | ||
1327 | veth.h_vlan_proto = skb->vlan_proto; | |
1328 | veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb)); | |
1329 | ||
1330 | vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); | |
6680ec68 JW |
1331 | |
1332 | copy = min_t(int, vlan_offset, len); | |
e6fd07c8 | 1333 | ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy); |
6680ec68 | 1334 | len -= copy; |
e6fd07c8 | 1335 | copied += copy; |
6680ec68 JW |
1336 | if (ret || !len) |
1337 | goto done; | |
1338 | ||
1339 | copy = min_t(int, sizeof(veth), len); | |
e6fd07c8 | 1340 | ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy); |
6680ec68 | 1341 | len -= copy; |
e6fd07c8 | 1342 | copied += copy; |
6680ec68 JW |
1343 | if (ret || !len) |
1344 | goto done; | |
1345 | } | |
1da177e4 | 1346 | |
e6fd07c8 | 1347 | skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len); |
1da177e4 | 1348 | |
6680ec68 | 1349 | done: |
09f75cd7 JG |
1350 | tun->dev->stats.tx_packets++; |
1351 | tun->dev->stats.tx_bytes += len; | |
1da177e4 LT |
1352 | |
1353 | return total; | |
1354 | } | |
1355 | ||
54f968d6 | 1356 | static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, |
f96eb74c | 1357 | const struct iovec *iv, ssize_t len, int noblock) |
1da177e4 | 1358 | { |
1da177e4 | 1359 | struct sk_buff *skb; |
05c2828c | 1360 | ssize_t ret = 0; |
9e641bdc | 1361 | int peeked, err, off = 0; |
1da177e4 | 1362 | |
3872baf6 | 1363 | tun_debug(KERN_INFO, tun, "tun_do_read\n"); |
1da177e4 | 1364 | |
9e641bdc XW |
1365 | if (!len) |
1366 | return ret; | |
1da177e4 | 1367 | |
9e641bdc XW |
1368 | if (tun->dev->reg_state != NETREG_REGISTERED) |
1369 | return -EIO; | |
1da177e4 | 1370 | |
9e641bdc XW |
1371 | /* Read frames from queue */ |
1372 | skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0, | |
1373 | &peeked, &off, &err); | |
1374 | if (skb) { | |
54f968d6 | 1375 | ret = tun_put_user(tun, tfile, skb, iv, len); |
f271b2cc | 1376 | kfree_skb(skb); |
9e641bdc XW |
1377 | } else |
1378 | ret = err; | |
1da177e4 | 1379 | |
05c2828c MT |
1380 | return ret; |
1381 | } | |
1382 | ||
1383 | static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, | |
1384 | unsigned long count, loff_t pos) | |
1385 | { | |
1386 | struct file *file = iocb->ki_filp; | |
1387 | struct tun_file *tfile = file->private_data; | |
1388 | struct tun_struct *tun = __tun_get(tfile); | |
1389 | ssize_t len, ret; | |
1390 | ||
1391 | if (!tun) | |
1392 | return -EBADFD; | |
1393 | len = iov_length(iv, count); | |
1394 | if (len < 0) { | |
1395 | ret = -EINVAL; | |
1396 | goto out; | |
1397 | } | |
1398 | ||
f96eb74c | 1399 | ret = tun_do_read(tun, tfile, iv, len, |
54f968d6 | 1400 | file->f_flags & O_NONBLOCK); |
42404c09 | 1401 | ret = min_t(ssize_t, ret, len); |
d0b7da8a ZYW |
1402 | if (ret > 0) |
1403 | iocb->ki_pos = ret; | |
631ab46b EB |
1404 | out: |
1405 | tun_put(tun); | |
1da177e4 LT |
1406 | return ret; |
1407 | } | |
1408 | ||
96442e42 JW |
1409 | static void tun_free_netdev(struct net_device *dev) |
1410 | { | |
1411 | struct tun_struct *tun = netdev_priv(dev); | |
1412 | ||
4008e97f | 1413 | BUG_ON(!(list_empty(&tun->disabled))); |
96442e42 | 1414 | tun_flow_uninit(tun); |
5dbbaf2d | 1415 | security_tun_dev_free_security(tun->security); |
96442e42 JW |
1416 | free_netdev(dev); |
1417 | } | |
1418 | ||
1da177e4 LT |
1419 | static void tun_setup(struct net_device *dev) |
1420 | { | |
1421 | struct tun_struct *tun = netdev_priv(dev); | |
1422 | ||
0625c883 EB |
1423 | tun->owner = INVALID_UID; |
1424 | tun->group = INVALID_GID; | |
1da177e4 | 1425 | |
1da177e4 | 1426 | dev->ethtool_ops = &tun_ethtool_ops; |
96442e42 | 1427 | dev->destructor = tun_free_netdev; |
1da177e4 LT |
1428 | } |
1429 | ||
f019a7a5 EB |
1430 | /* Trivial set of netlink ops to allow deleting tun or tap |
1431 | * device with netlink. | |
1432 | */ | |
1433 | static int tun_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1434 | { | |
1435 | return -EINVAL; | |
1436 | } | |
1437 | ||
1438 | static struct rtnl_link_ops tun_link_ops __read_mostly = { | |
1439 | .kind = DRV_NAME, | |
1440 | .priv_size = sizeof(struct tun_struct), | |
1441 | .setup = tun_setup, | |
1442 | .validate = tun_validate, | |
1443 | }; | |
1444 | ||
33dccbb0 HX |
1445 | static void tun_sock_write_space(struct sock *sk) |
1446 | { | |
54f968d6 | 1447 | struct tun_file *tfile; |
43815482 | 1448 | wait_queue_head_t *wqueue; |
33dccbb0 HX |
1449 | |
1450 | if (!sock_writeable(sk)) | |
1451 | return; | |
1452 | ||
33dccbb0 HX |
1453 | if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags)) |
1454 | return; | |
1455 | ||
43815482 ED |
1456 | wqueue = sk_sleep(sk); |
1457 | if (wqueue && waitqueue_active(wqueue)) | |
1458 | wake_up_interruptible_sync_poll(wqueue, POLLOUT | | |
05c2828c | 1459 | POLLWRNORM | POLLWRBAND); |
c722c625 | 1460 | |
54f968d6 JW |
1461 | tfile = container_of(sk, struct tun_file, sk); |
1462 | kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); | |
33dccbb0 HX |
1463 | } |
1464 | ||
05c2828c MT |
1465 | static int tun_sendmsg(struct kiocb *iocb, struct socket *sock, |
1466 | struct msghdr *m, size_t total_len) | |
1467 | { | |
54f968d6 JW |
1468 | int ret; |
1469 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); | |
1470 | struct tun_struct *tun = __tun_get(tfile); | |
1471 | ||
1472 | if (!tun) | |
1473 | return -EBADFD; | |
54f968d6 JW |
1474 | ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len, |
1475 | m->msg_iovlen, m->msg_flags & MSG_DONTWAIT); | |
1476 | tun_put(tun); | |
1477 | return ret; | |
05c2828c MT |
1478 | } |
1479 | ||
1480 | static int tun_recvmsg(struct kiocb *iocb, struct socket *sock, | |
1481 | struct msghdr *m, size_t total_len, | |
1482 | int flags) | |
1483 | { | |
54f968d6 JW |
1484 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
1485 | struct tun_struct *tun = __tun_get(tfile); | |
05c2828c | 1486 | int ret; |
54f968d6 JW |
1487 | |
1488 | if (!tun) | |
1489 | return -EBADFD; | |
1490 | ||
eda29772 | 1491 | if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { |
3811ae76 G |
1492 | ret = -EINVAL; |
1493 | goto out; | |
1494 | } | |
eda29772 RC |
1495 | if (flags & MSG_ERRQUEUE) { |
1496 | ret = sock_recv_errqueue(sock->sk, m, total_len, | |
1497 | SOL_PACKET, TUN_TX_TIMESTAMP); | |
1498 | goto out; | |
1499 | } | |
f96eb74c | 1500 | ret = tun_do_read(tun, tfile, m->msg_iov, total_len, |
05c2828c | 1501 | flags & MSG_DONTWAIT); |
42404c09 DM |
1502 | if (ret > total_len) { |
1503 | m->msg_flags |= MSG_TRUNC; | |
1504 | ret = flags & MSG_TRUNC ? ret : total_len; | |
1505 | } | |
3811ae76 | 1506 | out: |
54f968d6 | 1507 | tun_put(tun); |
05c2828c MT |
1508 | return ret; |
1509 | } | |
1510 | ||
1ab5ecb9 SK |
1511 | static int tun_release(struct socket *sock) |
1512 | { | |
1513 | if (sock->sk) | |
1514 | sock_put(sock->sk); | |
1515 | return 0; | |
1516 | } | |
1517 | ||
05c2828c MT |
1518 | /* Ops structure to mimic raw sockets with tun */ |
1519 | static const struct proto_ops tun_socket_ops = { | |
1520 | .sendmsg = tun_sendmsg, | |
1521 | .recvmsg = tun_recvmsg, | |
1ab5ecb9 | 1522 | .release = tun_release, |
05c2828c MT |
1523 | }; |
1524 | ||
33dccbb0 HX |
1525 | static struct proto tun_proto = { |
1526 | .name = "tun", | |
1527 | .owner = THIS_MODULE, | |
54f968d6 | 1528 | .obj_size = sizeof(struct tun_file), |
33dccbb0 | 1529 | }; |
f019a7a5 | 1530 | |
980c9e8c DW |
1531 | static int tun_flags(struct tun_struct *tun) |
1532 | { | |
031f5e03 | 1533 | return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); |
980c9e8c DW |
1534 | } |
1535 | ||
1536 | static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, | |
1537 | char *buf) | |
1538 | { | |
1539 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); | |
1540 | return sprintf(buf, "0x%x\n", tun_flags(tun)); | |
1541 | } | |
1542 | ||
1543 | static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, | |
1544 | char *buf) | |
1545 | { | |
1546 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); | |
0625c883 EB |
1547 | return uid_valid(tun->owner)? |
1548 | sprintf(buf, "%u\n", | |
1549 | from_kuid_munged(current_user_ns(), tun->owner)): | |
1550 | sprintf(buf, "-1\n"); | |
980c9e8c DW |
1551 | } |
1552 | ||
1553 | static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, | |
1554 | char *buf) | |
1555 | { | |
1556 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); | |
0625c883 EB |
1557 | return gid_valid(tun->group) ? |
1558 | sprintf(buf, "%u\n", | |
1559 | from_kgid_munged(current_user_ns(), tun->group)): | |
1560 | sprintf(buf, "-1\n"); | |
980c9e8c DW |
1561 | } |
1562 | ||
1563 | static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); | |
1564 | static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); | |
1565 | static DEVICE_ATTR(group, 0444, tun_show_group, NULL); | |
1566 | ||
d647a591 | 1567 | static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) |
1da177e4 LT |
1568 | { |
1569 | struct tun_struct *tun; | |
54f968d6 | 1570 | struct tun_file *tfile = file->private_data; |
1da177e4 LT |
1571 | struct net_device *dev; |
1572 | int err; | |
1573 | ||
7c0c3b1a JW |
1574 | if (tfile->detached) |
1575 | return -EINVAL; | |
1576 | ||
74a3e5a7 EB |
1577 | dev = __dev_get_by_name(net, ifr->ifr_name); |
1578 | if (dev) { | |
f85ba780 DW |
1579 | if (ifr->ifr_flags & IFF_TUN_EXCL) |
1580 | return -EBUSY; | |
74a3e5a7 EB |
1581 | if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) |
1582 | tun = netdev_priv(dev); | |
1583 | else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) | |
1584 | tun = netdev_priv(dev); | |
1585 | else | |
1586 | return -EINVAL; | |
1587 | ||
8e6d91ae | 1588 | if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != |
40630b82 | 1589 | !!(tun->flags & IFF_MULTI_QUEUE)) |
8e6d91ae JW |
1590 | return -EINVAL; |
1591 | ||
cde8b15f | 1592 | if (tun_not_capable(tun)) |
2b980dbd | 1593 | return -EPERM; |
5dbbaf2d | 1594 | err = security_tun_dev_open(tun->security); |
2b980dbd PM |
1595 | if (err < 0) |
1596 | return err; | |
1597 | ||
849c9b6f | 1598 | err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER); |
a7385ba2 EB |
1599 | if (err < 0) |
1600 | return err; | |
4008e97f | 1601 | |
40630b82 | 1602 | if (tun->flags & IFF_MULTI_QUEUE && |
e8dbad66 JW |
1603 | (tun->numqueues + tun->numdisabled > 1)) { |
1604 | /* One or more queue has already been attached, no need | |
1605 | * to initialize the device again. | |
1606 | */ | |
1607 | return 0; | |
1608 | } | |
6aa20a22 | 1609 | } |
1da177e4 LT |
1610 | else { |
1611 | char *name; | |
1612 | unsigned long flags = 0; | |
edfb6a14 JW |
1613 | int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? |
1614 | MAX_TAP_QUEUES : 1; | |
1da177e4 | 1615 | |
c260b772 | 1616 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
ca6bb5d7 | 1617 | return -EPERM; |
2b980dbd PM |
1618 | err = security_tun_dev_create(); |
1619 | if (err < 0) | |
1620 | return err; | |
ca6bb5d7 | 1621 | |
1da177e4 LT |
1622 | /* Set dev type */ |
1623 | if (ifr->ifr_flags & IFF_TUN) { | |
1624 | /* TUN device */ | |
40630b82 | 1625 | flags |= IFF_TUN; |
1da177e4 LT |
1626 | name = "tun%d"; |
1627 | } else if (ifr->ifr_flags & IFF_TAP) { | |
1628 | /* TAP device */ | |
40630b82 | 1629 | flags |= IFF_TAP; |
1da177e4 | 1630 | name = "tap%d"; |
6aa20a22 | 1631 | } else |
36989b90 | 1632 | return -EINVAL; |
6aa20a22 | 1633 | |
1da177e4 LT |
1634 | if (*ifr->ifr_name) |
1635 | name = ifr->ifr_name; | |
1636 | ||
c8d68e6b | 1637 | dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, |
c835a677 TG |
1638 | NET_NAME_UNKNOWN, tun_setup, queues, |
1639 | queues); | |
edfb6a14 | 1640 | |
1da177e4 LT |
1641 | if (!dev) |
1642 | return -ENOMEM; | |
1643 | ||
fc54c658 | 1644 | dev_net_set(dev, net); |
f019a7a5 | 1645 | dev->rtnl_link_ops = &tun_link_ops; |
fb7589a1 | 1646 | dev->ifindex = tfile->ifindex; |
758e43b7 | 1647 | |
1da177e4 LT |
1648 | tun = netdev_priv(dev); |
1649 | tun->dev = dev; | |
1650 | tun->flags = flags; | |
f271b2cc | 1651 | tun->txflt.count = 0; |
d9d52b51 | 1652 | tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); |
33dccbb0 | 1653 | |
54f968d6 JW |
1654 | tun->filter_attached = false; |
1655 | tun->sndbuf = tfile->socket.sk->sk_sndbuf; | |
33dccbb0 | 1656 | |
96442e42 JW |
1657 | spin_lock_init(&tun->lock); |
1658 | ||
5dbbaf2d PM |
1659 | err = security_tun_dev_alloc_security(&tun->security); |
1660 | if (err < 0) | |
1661 | goto err_free_dev; | |
2b980dbd | 1662 | |
1da177e4 | 1663 | tun_net_init(dev); |
944a1376 | 1664 | tun_flow_init(tun); |
96442e42 | 1665 | |
88255375 | 1666 | dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | |
6680ec68 JW |
1667 | TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | |
1668 | NETIF_F_HW_VLAN_STAG_TX; | |
88255375 | 1669 | dev->features = dev->hw_features; |
6671b224 FLVC |
1670 | dev->vlan_features = dev->features & |
1671 | ~(NETIF_F_HW_VLAN_CTAG_TX | | |
1672 | NETIF_F_HW_VLAN_STAG_TX); | |
88255375 | 1673 | |
4008e97f | 1674 | INIT_LIST_HEAD(&tun->disabled); |
849c9b6f | 1675 | err = tun_attach(tun, file, false); |
eb0fb363 | 1676 | if (err < 0) |
662ca437 | 1677 | goto err_free_flow; |
eb0fb363 | 1678 | |
1da177e4 LT |
1679 | err = register_netdevice(tun->dev); |
1680 | if (err < 0) | |
662ca437 | 1681 | goto err_detach; |
9c3fea6a | 1682 | |
980c9e8c DW |
1683 | if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) || |
1684 | device_create_file(&tun->dev->dev, &dev_attr_owner) || | |
1685 | device_create_file(&tun->dev->dev, &dev_attr_group)) | |
6b8a66ee | 1686 | pr_err("Failed to create tun sysfs files\n"); |
1da177e4 LT |
1687 | } |
1688 | ||
af668b3c MT |
1689 | netif_carrier_on(tun->dev); |
1690 | ||
6b8a66ee | 1691 | tun_debug(KERN_INFO, tun, "tun_set_iff\n"); |
1da177e4 | 1692 | |
031f5e03 MT |
1693 | tun->flags = (tun->flags & ~TUN_FEATURES) | |
1694 | (ifr->ifr_flags & TUN_FEATURES); | |
c8d68e6b | 1695 | |
e35259a9 MK |
1696 | /* Make sure persistent devices do not get stuck in |
1697 | * xoff state. | |
1698 | */ | |
1699 | if (netif_running(tun->dev)) | |
c8d68e6b | 1700 | netif_tx_wake_all_queues(tun->dev); |
e35259a9 | 1701 | |
1da177e4 LT |
1702 | strcpy(ifr->ifr_name, tun->dev->name); |
1703 | return 0; | |
1704 | ||
662ca437 JW |
1705 | err_detach: |
1706 | tun_detach_all(dev); | |
1707 | err_free_flow: | |
1708 | tun_flow_uninit(tun); | |
1709 | security_tun_dev_free_security(tun->security); | |
1710 | err_free_dev: | |
1da177e4 | 1711 | free_netdev(dev); |
1da177e4 LT |
1712 | return err; |
1713 | } | |
1714 | ||
9ce99cf6 | 1715 | static void tun_get_iff(struct net *net, struct tun_struct *tun, |
876bfd4d | 1716 | struct ifreq *ifr) |
e3b99556 | 1717 | { |
6b8a66ee | 1718 | tun_debug(KERN_INFO, tun, "tun_get_iff\n"); |
e3b99556 MM |
1719 | |
1720 | strcpy(ifr->ifr_name, tun->dev->name); | |
1721 | ||
980c9e8c | 1722 | ifr->ifr_flags = tun_flags(tun); |
e3b99556 | 1723 | |
e3b99556 MM |
1724 | } |
1725 | ||
5228ddc9 RR |
1726 | /* This is like a cut-down ethtool ops, except done via tun fd so no |
1727 | * privs required. */ | |
88255375 | 1728 | static int set_offload(struct tun_struct *tun, unsigned long arg) |
5228ddc9 | 1729 | { |
c8f44aff | 1730 | netdev_features_t features = 0; |
5228ddc9 RR |
1731 | |
1732 | if (arg & TUN_F_CSUM) { | |
88255375 | 1733 | features |= NETIF_F_HW_CSUM; |
5228ddc9 RR |
1734 | arg &= ~TUN_F_CSUM; |
1735 | ||
1736 | if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { | |
1737 | if (arg & TUN_F_TSO_ECN) { | |
1738 | features |= NETIF_F_TSO_ECN; | |
1739 | arg &= ~TUN_F_TSO_ECN; | |
1740 | } | |
1741 | if (arg & TUN_F_TSO4) | |
1742 | features |= NETIF_F_TSO; | |
1743 | if (arg & TUN_F_TSO6) | |
1744 | features |= NETIF_F_TSO6; | |
1745 | arg &= ~(TUN_F_TSO4|TUN_F_TSO6); | |
1746 | } | |
1747 | } | |
1748 | ||
1749 | /* This gives the user a way to test for new features in future by | |
1750 | * trying to set them. */ | |
1751 | if (arg) | |
1752 | return -EINVAL; | |
1753 | ||
88255375 MM |
1754 | tun->set_features = features; |
1755 | netdev_update_features(tun->dev); | |
5228ddc9 RR |
1756 | |
1757 | return 0; | |
1758 | } | |
1759 | ||
c8d68e6b JW |
1760 | static void tun_detach_filter(struct tun_struct *tun, int n) |
1761 | { | |
1762 | int i; | |
1763 | struct tun_file *tfile; | |
1764 | ||
1765 | for (i = 0; i < n; i++) { | |
b8deabd3 | 1766 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b JW |
1767 | sk_detach_filter(tfile->socket.sk); |
1768 | } | |
1769 | ||
1770 | tun->filter_attached = false; | |
1771 | } | |
1772 | ||
1773 | static int tun_attach_filter(struct tun_struct *tun) | |
1774 | { | |
1775 | int i, ret = 0; | |
1776 | struct tun_file *tfile; | |
1777 | ||
1778 | for (i = 0; i < tun->numqueues; i++) { | |
b8deabd3 | 1779 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b JW |
1780 | ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); |
1781 | if (ret) { | |
1782 | tun_detach_filter(tun, i); | |
1783 | return ret; | |
1784 | } | |
1785 | } | |
1786 | ||
1787 | tun->filter_attached = true; | |
1788 | return ret; | |
1789 | } | |
1790 | ||
1791 | static void tun_set_sndbuf(struct tun_struct *tun) | |
1792 | { | |
1793 | struct tun_file *tfile; | |
1794 | int i; | |
1795 | ||
1796 | for (i = 0; i < tun->numqueues; i++) { | |
b8deabd3 | 1797 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b JW |
1798 | tfile->socket.sk->sk_sndbuf = tun->sndbuf; |
1799 | } | |
1800 | } | |
1801 | ||
cde8b15f JW |
1802 | static int tun_set_queue(struct file *file, struct ifreq *ifr) |
1803 | { | |
1804 | struct tun_file *tfile = file->private_data; | |
1805 | struct tun_struct *tun; | |
cde8b15f JW |
1806 | int ret = 0; |
1807 | ||
1808 | rtnl_lock(); | |
1809 | ||
1810 | if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { | |
4008e97f | 1811 | tun = tfile->detached; |
5dbbaf2d | 1812 | if (!tun) { |
cde8b15f | 1813 | ret = -EINVAL; |
5dbbaf2d PM |
1814 | goto unlock; |
1815 | } | |
1816 | ret = security_tun_dev_attach_queue(tun->security); | |
1817 | if (ret < 0) | |
1818 | goto unlock; | |
849c9b6f | 1819 | ret = tun_attach(tun, file, false); |
4008e97f | 1820 | } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { |
b8deabd3 | 1821 | tun = rtnl_dereference(tfile->tun); |
40630b82 | 1822 | if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) |
4008e97f JW |
1823 | ret = -EINVAL; |
1824 | else | |
1825 | __tun_detach(tfile, false); | |
1826 | } else | |
cde8b15f JW |
1827 | ret = -EINVAL; |
1828 | ||
5dbbaf2d | 1829 | unlock: |
cde8b15f JW |
1830 | rtnl_unlock(); |
1831 | return ret; | |
1832 | } | |
1833 | ||
50857e2a AB |
1834 | static long __tun_chr_ioctl(struct file *file, unsigned int cmd, |
1835 | unsigned long arg, int ifreq_len) | |
1da177e4 | 1836 | { |
36b50bab | 1837 | struct tun_file *tfile = file->private_data; |
631ab46b | 1838 | struct tun_struct *tun; |
1da177e4 LT |
1839 | void __user* argp = (void __user*)arg; |
1840 | struct ifreq ifr; | |
0625c883 EB |
1841 | kuid_t owner; |
1842 | kgid_t group; | |
33dccbb0 | 1843 | int sndbuf; |
d9d52b51 | 1844 | int vnet_hdr_sz; |
fb7589a1 | 1845 | unsigned int ifindex; |
f271b2cc | 1846 | int ret; |
1da177e4 | 1847 | |
cde8b15f | 1848 | if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) { |
50857e2a | 1849 | if (copy_from_user(&ifr, argp, ifreq_len)) |
1da177e4 | 1850 | return -EFAULT; |
8bbb1813 | 1851 | } else { |
a117dacd | 1852 | memset(&ifr, 0, sizeof(ifr)); |
8bbb1813 | 1853 | } |
631ab46b EB |
1854 | if (cmd == TUNGETFEATURES) { |
1855 | /* Currently this just means: "what IFF flags are valid?". | |
1856 | * This is needed because we never checked for invalid flags on | |
031f5e03 MT |
1857 | * TUNSETIFF. |
1858 | */ | |
1859 | return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, | |
631ab46b | 1860 | (unsigned int __user*)argp); |
cde8b15f JW |
1861 | } else if (cmd == TUNSETQUEUE) |
1862 | return tun_set_queue(file, &ifr); | |
631ab46b | 1863 | |
c8d68e6b | 1864 | ret = 0; |
876bfd4d HX |
1865 | rtnl_lock(); |
1866 | ||
36b50bab | 1867 | tun = __tun_get(tfile); |
1da177e4 | 1868 | if (cmd == TUNSETIFF && !tun) { |
1da177e4 LT |
1869 | ifr.ifr_name[IFNAMSIZ-1] = '\0'; |
1870 | ||
876bfd4d | 1871 | ret = tun_set_iff(tfile->net, file, &ifr); |
1da177e4 | 1872 | |
876bfd4d HX |
1873 | if (ret) |
1874 | goto unlock; | |
1da177e4 | 1875 | |
50857e2a | 1876 | if (copy_to_user(argp, &ifr, ifreq_len)) |
876bfd4d HX |
1877 | ret = -EFAULT; |
1878 | goto unlock; | |
1da177e4 | 1879 | } |
fb7589a1 PE |
1880 | if (cmd == TUNSETIFINDEX) { |
1881 | ret = -EPERM; | |
1882 | if (tun) | |
1883 | goto unlock; | |
1884 | ||
1885 | ret = -EFAULT; | |
1886 | if (copy_from_user(&ifindex, argp, sizeof(ifindex))) | |
1887 | goto unlock; | |
1888 | ||
1889 | ret = 0; | |
1890 | tfile->ifindex = ifindex; | |
1891 | goto unlock; | |
1892 | } | |
1da177e4 | 1893 | |
876bfd4d | 1894 | ret = -EBADFD; |
1da177e4 | 1895 | if (!tun) |
876bfd4d | 1896 | goto unlock; |
1da177e4 | 1897 | |
1e588338 | 1898 | tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); |
1da177e4 | 1899 | |
631ab46b | 1900 | ret = 0; |
1da177e4 | 1901 | switch (cmd) { |
e3b99556 | 1902 | case TUNGETIFF: |
9ce99cf6 | 1903 | tun_get_iff(current->nsproxy->net_ns, tun, &ifr); |
e3b99556 | 1904 | |
3d407a80 PE |
1905 | if (tfile->detached) |
1906 | ifr.ifr_flags |= IFF_DETACH_QUEUE; | |
849c9b6f PE |
1907 | if (!tfile->socket.sk->sk_filter) |
1908 | ifr.ifr_flags |= IFF_NOFILTER; | |
3d407a80 | 1909 | |
50857e2a | 1910 | if (copy_to_user(argp, &ifr, ifreq_len)) |
631ab46b | 1911 | ret = -EFAULT; |
e3b99556 MM |
1912 | break; |
1913 | ||
1da177e4 LT |
1914 | case TUNSETNOCSUM: |
1915 | /* Disable/Enable checksum */ | |
1da177e4 | 1916 | |
88255375 MM |
1917 | /* [unimplemented] */ |
1918 | tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", | |
6b8a66ee | 1919 | arg ? "disabled" : "enabled"); |
1da177e4 LT |
1920 | break; |
1921 | ||
1922 | case TUNSETPERSIST: | |
54f968d6 JW |
1923 | /* Disable/Enable persist mode. Keep an extra reference to the |
1924 | * module to prevent the module being unprobed. | |
1925 | */ | |
40630b82 MT |
1926 | if (arg && !(tun->flags & IFF_PERSIST)) { |
1927 | tun->flags |= IFF_PERSIST; | |
54f968d6 | 1928 | __module_get(THIS_MODULE); |
dd38bd85 | 1929 | } |
40630b82 MT |
1930 | if (!arg && (tun->flags & IFF_PERSIST)) { |
1931 | tun->flags &= ~IFF_PERSIST; | |
54f968d6 JW |
1932 | module_put(THIS_MODULE); |
1933 | } | |
1da177e4 | 1934 | |
6b8a66ee JP |
1935 | tun_debug(KERN_INFO, tun, "persist %s\n", |
1936 | arg ? "enabled" : "disabled"); | |
1da177e4 LT |
1937 | break; |
1938 | ||
1939 | case TUNSETOWNER: | |
1940 | /* Set owner of the device */ | |
0625c883 EB |
1941 | owner = make_kuid(current_user_ns(), arg); |
1942 | if (!uid_valid(owner)) { | |
1943 | ret = -EINVAL; | |
1944 | break; | |
1945 | } | |
1946 | tun->owner = owner; | |
1e588338 | 1947 | tun_debug(KERN_INFO, tun, "owner set to %u\n", |
0625c883 | 1948 | from_kuid(&init_user_ns, tun->owner)); |
1da177e4 LT |
1949 | break; |
1950 | ||
8c644623 GG |
1951 | case TUNSETGROUP: |
1952 | /* Set group of the device */ | |
0625c883 EB |
1953 | group = make_kgid(current_user_ns(), arg); |
1954 | if (!gid_valid(group)) { | |
1955 | ret = -EINVAL; | |
1956 | break; | |
1957 | } | |
1958 | tun->group = group; | |
1e588338 | 1959 | tun_debug(KERN_INFO, tun, "group set to %u\n", |
0625c883 | 1960 | from_kgid(&init_user_ns, tun->group)); |
8c644623 GG |
1961 | break; |
1962 | ||
ff4cc3ac MK |
1963 | case TUNSETLINK: |
1964 | /* Only allow setting the type when the interface is down */ | |
1965 | if (tun->dev->flags & IFF_UP) { | |
6b8a66ee JP |
1966 | tun_debug(KERN_INFO, tun, |
1967 | "Linktype set failed because interface is up\n"); | |
48abfe05 | 1968 | ret = -EBUSY; |
ff4cc3ac MK |
1969 | } else { |
1970 | tun->dev->type = (int) arg; | |
6b8a66ee JP |
1971 | tun_debug(KERN_INFO, tun, "linktype set to %d\n", |
1972 | tun->dev->type); | |
48abfe05 | 1973 | ret = 0; |
ff4cc3ac | 1974 | } |
631ab46b | 1975 | break; |
ff4cc3ac | 1976 | |
1da177e4 LT |
1977 | #ifdef TUN_DEBUG |
1978 | case TUNSETDEBUG: | |
1979 | tun->debug = arg; | |
1980 | break; | |
1981 | #endif | |
5228ddc9 | 1982 | case TUNSETOFFLOAD: |
88255375 | 1983 | ret = set_offload(tun, arg); |
631ab46b | 1984 | break; |
5228ddc9 | 1985 | |
f271b2cc MK |
1986 | case TUNSETTXFILTER: |
1987 | /* Can be set only for TAPs */ | |
631ab46b | 1988 | ret = -EINVAL; |
40630b82 | 1989 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
631ab46b | 1990 | break; |
c0e5a8c2 | 1991 | ret = update_filter(&tun->txflt, (void __user *)arg); |
631ab46b | 1992 | break; |
1da177e4 LT |
1993 | |
1994 | case SIOCGIFHWADDR: | |
b595076a | 1995 | /* Get hw address */ |
f271b2cc MK |
1996 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); |
1997 | ifr.ifr_hwaddr.sa_family = tun->dev->type; | |
50857e2a | 1998 | if (copy_to_user(argp, &ifr, ifreq_len)) |
631ab46b EB |
1999 | ret = -EFAULT; |
2000 | break; | |
1da177e4 LT |
2001 | |
2002 | case SIOCSIFHWADDR: | |
f271b2cc | 2003 | /* Set hw address */ |
6b8a66ee JP |
2004 | tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", |
2005 | ifr.ifr_hwaddr.sa_data); | |
40102371 | 2006 | |
40102371 | 2007 | ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); |
631ab46b | 2008 | break; |
33dccbb0 HX |
2009 | |
2010 | case TUNGETSNDBUF: | |
54f968d6 | 2011 | sndbuf = tfile->socket.sk->sk_sndbuf; |
33dccbb0 HX |
2012 | if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) |
2013 | ret = -EFAULT; | |
2014 | break; | |
2015 | ||
2016 | case TUNSETSNDBUF: | |
2017 | if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { | |
2018 | ret = -EFAULT; | |
2019 | break; | |
2020 | } | |
2021 | ||
c8d68e6b JW |
2022 | tun->sndbuf = sndbuf; |
2023 | tun_set_sndbuf(tun); | |
33dccbb0 HX |
2024 | break; |
2025 | ||
d9d52b51 MT |
2026 | case TUNGETVNETHDRSZ: |
2027 | vnet_hdr_sz = tun->vnet_hdr_sz; | |
2028 | if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) | |
2029 | ret = -EFAULT; | |
2030 | break; | |
2031 | ||
2032 | case TUNSETVNETHDRSZ: | |
2033 | if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { | |
2034 | ret = -EFAULT; | |
2035 | break; | |
2036 | } | |
2037 | if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { | |
2038 | ret = -EINVAL; | |
2039 | break; | |
2040 | } | |
2041 | ||
2042 | tun->vnet_hdr_sz = vnet_hdr_sz; | |
2043 | break; | |
2044 | ||
99405162 MT |
2045 | case TUNATTACHFILTER: |
2046 | /* Can be set only for TAPs */ | |
2047 | ret = -EINVAL; | |
40630b82 | 2048 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
99405162 MT |
2049 | break; |
2050 | ret = -EFAULT; | |
54f968d6 | 2051 | if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) |
99405162 MT |
2052 | break; |
2053 | ||
c8d68e6b | 2054 | ret = tun_attach_filter(tun); |
99405162 MT |
2055 | break; |
2056 | ||
2057 | case TUNDETACHFILTER: | |
2058 | /* Can be set only for TAPs */ | |
2059 | ret = -EINVAL; | |
40630b82 | 2060 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
99405162 | 2061 | break; |
c8d68e6b JW |
2062 | ret = 0; |
2063 | tun_detach_filter(tun, tun->numqueues); | |
99405162 MT |
2064 | break; |
2065 | ||
76975e9c PE |
2066 | case TUNGETFILTER: |
2067 | ret = -EINVAL; | |
40630b82 | 2068 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
76975e9c PE |
2069 | break; |
2070 | ret = -EFAULT; | |
2071 | if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) | |
2072 | break; | |
2073 | ret = 0; | |
2074 | break; | |
2075 | ||
1da177e4 | 2076 | default: |
631ab46b EB |
2077 | ret = -EINVAL; |
2078 | break; | |
ee289b64 | 2079 | } |
1da177e4 | 2080 | |
876bfd4d HX |
2081 | unlock: |
2082 | rtnl_unlock(); | |
2083 | if (tun) | |
2084 | tun_put(tun); | |
631ab46b | 2085 | return ret; |
1da177e4 LT |
2086 | } |
2087 | ||
50857e2a AB |
2088 | static long tun_chr_ioctl(struct file *file, |
2089 | unsigned int cmd, unsigned long arg) | |
2090 | { | |
2091 | return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); | |
2092 | } | |
2093 | ||
2094 | #ifdef CONFIG_COMPAT | |
2095 | static long tun_chr_compat_ioctl(struct file *file, | |
2096 | unsigned int cmd, unsigned long arg) | |
2097 | { | |
2098 | switch (cmd) { | |
2099 | case TUNSETIFF: | |
2100 | case TUNGETIFF: | |
2101 | case TUNSETTXFILTER: | |
2102 | case TUNGETSNDBUF: | |
2103 | case TUNSETSNDBUF: | |
2104 | case SIOCGIFHWADDR: | |
2105 | case SIOCSIFHWADDR: | |
2106 | arg = (unsigned long)compat_ptr(arg); | |
2107 | break; | |
2108 | default: | |
2109 | arg = (compat_ulong_t)arg; | |
2110 | break; | |
2111 | } | |
2112 | ||
2113 | /* | |
2114 | * compat_ifreq is shorter than ifreq, so we must not access beyond | |
2115 | * the end of that structure. All fields that are used in this | |
2116 | * driver are compatible though, we don't need to convert the | |
2117 | * contents. | |
2118 | */ | |
2119 | return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); | |
2120 | } | |
2121 | #endif /* CONFIG_COMPAT */ | |
2122 | ||
1da177e4 LT |
2123 | static int tun_chr_fasync(int fd, struct file *file, int on) |
2124 | { | |
54f968d6 | 2125 | struct tun_file *tfile = file->private_data; |
1da177e4 LT |
2126 | int ret; |
2127 | ||
54f968d6 | 2128 | if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) |
9d319522 | 2129 | goto out; |
6aa20a22 | 2130 | |
1da177e4 | 2131 | if (on) { |
e0b93edd | 2132 | __f_setown(file, task_pid(current), PIDTYPE_PID, 0); |
54f968d6 | 2133 | tfile->flags |= TUN_FASYNC; |
6aa20a22 | 2134 | } else |
54f968d6 | 2135 | tfile->flags &= ~TUN_FASYNC; |
9d319522 JC |
2136 | ret = 0; |
2137 | out: | |
9d319522 | 2138 | return ret; |
1da177e4 LT |
2139 | } |
2140 | ||
2141 | static int tun_chr_open(struct inode *inode, struct file * file) | |
2142 | { | |
631ab46b | 2143 | struct tun_file *tfile; |
deed49fb | 2144 | |
6b8a66ee | 2145 | DBG1(KERN_INFO, "tunX: tun_chr_open\n"); |
631ab46b | 2146 | |
54f968d6 JW |
2147 | tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, |
2148 | &tun_proto); | |
631ab46b EB |
2149 | if (!tfile) |
2150 | return -ENOMEM; | |
c956674b | 2151 | RCU_INIT_POINTER(tfile->tun, NULL); |
36b50bab | 2152 | tfile->net = get_net(current->nsproxy->net_ns); |
54f968d6 | 2153 | tfile->flags = 0; |
fb7589a1 | 2154 | tfile->ifindex = 0; |
54f968d6 | 2155 | |
54f968d6 | 2156 | init_waitqueue_head(&tfile->wq.wait); |
9e641bdc | 2157 | RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); |
54f968d6 JW |
2158 | |
2159 | tfile->socket.file = file; | |
2160 | tfile->socket.ops = &tun_socket_ops; | |
2161 | ||
2162 | sock_init_data(&tfile->socket, &tfile->sk); | |
2163 | sk_change_net(&tfile->sk, tfile->net); | |
2164 | ||
2165 | tfile->sk.sk_write_space = tun_sock_write_space; | |
2166 | tfile->sk.sk_sndbuf = INT_MAX; | |
2167 | ||
631ab46b | 2168 | file->private_data = tfile; |
54f968d6 | 2169 | set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags); |
4008e97f | 2170 | INIT_LIST_HEAD(&tfile->next); |
54f968d6 | 2171 | |
19a6afb2 JW |
2172 | sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); |
2173 | ||
1da177e4 LT |
2174 | return 0; |
2175 | } | |
2176 | ||
2177 | static int tun_chr_close(struct inode *inode, struct file *file) | |
2178 | { | |
631ab46b | 2179 | struct tun_file *tfile = file->private_data; |
54f968d6 | 2180 | struct net *net = tfile->net; |
1da177e4 | 2181 | |
c8d68e6b | 2182 | tun_detach(tfile, true); |
54f968d6 | 2183 | put_net(net); |
1da177e4 LT |
2184 | |
2185 | return 0; | |
2186 | } | |
2187 | ||
93e14b6d MY |
2188 | #ifdef CONFIG_PROC_FS |
2189 | static int tun_chr_show_fdinfo(struct seq_file *m, struct file *f) | |
2190 | { | |
2191 | struct tun_struct *tun; | |
2192 | struct ifreq ifr; | |
2193 | ||
2194 | memset(&ifr, 0, sizeof(ifr)); | |
2195 | ||
2196 | rtnl_lock(); | |
2197 | tun = tun_get(f); | |
2198 | if (tun) | |
2199 | tun_get_iff(current->nsproxy->net_ns, tun, &ifr); | |
2200 | rtnl_unlock(); | |
2201 | ||
2202 | if (tun) | |
2203 | tun_put(tun); | |
2204 | ||
2205 | return seq_printf(m, "iff:\t%s\n", ifr.ifr_name); | |
2206 | } | |
2207 | #endif | |
2208 | ||
d54b1fdb | 2209 | static const struct file_operations tun_fops = { |
6aa20a22 | 2210 | .owner = THIS_MODULE, |
1da177e4 | 2211 | .llseek = no_llseek, |
ee0b3e67 BP |
2212 | .read = do_sync_read, |
2213 | .aio_read = tun_chr_aio_read, | |
2214 | .write = do_sync_write, | |
2215 | .aio_write = tun_chr_aio_write, | |
1da177e4 | 2216 | .poll = tun_chr_poll, |
50857e2a AB |
2217 | .unlocked_ioctl = tun_chr_ioctl, |
2218 | #ifdef CONFIG_COMPAT | |
2219 | .compat_ioctl = tun_chr_compat_ioctl, | |
2220 | #endif | |
1da177e4 LT |
2221 | .open = tun_chr_open, |
2222 | .release = tun_chr_close, | |
93e14b6d MY |
2223 | .fasync = tun_chr_fasync, |
2224 | #ifdef CONFIG_PROC_FS | |
2225 | .show_fdinfo = tun_chr_show_fdinfo, | |
2226 | #endif | |
1da177e4 LT |
2227 | }; |
2228 | ||
2229 | static struct miscdevice tun_miscdev = { | |
2230 | .minor = TUN_MINOR, | |
2231 | .name = "tun", | |
e454cea2 | 2232 | .nodename = "net/tun", |
1da177e4 | 2233 | .fops = &tun_fops, |
1da177e4 LT |
2234 | }; |
2235 | ||
2236 | /* ethtool interface */ | |
2237 | ||
2238 | static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) | |
2239 | { | |
2240 | cmd->supported = 0; | |
2241 | cmd->advertising = 0; | |
70739497 | 2242 | ethtool_cmd_speed_set(cmd, SPEED_10); |
1da177e4 LT |
2243 | cmd->duplex = DUPLEX_FULL; |
2244 | cmd->port = PORT_TP; | |
2245 | cmd->phy_address = 0; | |
2246 | cmd->transceiver = XCVR_INTERNAL; | |
2247 | cmd->autoneg = AUTONEG_DISABLE; | |
2248 | cmd->maxtxpkt = 0; | |
2249 | cmd->maxrxpkt = 0; | |
2250 | return 0; | |
2251 | } | |
2252 | ||
2253 | static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) | |
2254 | { | |
2255 | struct tun_struct *tun = netdev_priv(dev); | |
2256 | ||
33a5ba14 RJ |
2257 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
2258 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); | |
1da177e4 LT |
2259 | |
2260 | switch (tun->flags & TUN_TYPE_MASK) { | |
40630b82 | 2261 | case IFF_TUN: |
33a5ba14 | 2262 | strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); |
1da177e4 | 2263 | break; |
40630b82 | 2264 | case IFF_TAP: |
33a5ba14 | 2265 | strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); |
1da177e4 LT |
2266 | break; |
2267 | } | |
2268 | } | |
2269 | ||
2270 | static u32 tun_get_msglevel(struct net_device *dev) | |
2271 | { | |
2272 | #ifdef TUN_DEBUG | |
2273 | struct tun_struct *tun = netdev_priv(dev); | |
2274 | return tun->debug; | |
2275 | #else | |
2276 | return -EOPNOTSUPP; | |
2277 | #endif | |
2278 | } | |
2279 | ||
2280 | static void tun_set_msglevel(struct net_device *dev, u32 value) | |
2281 | { | |
2282 | #ifdef TUN_DEBUG | |
2283 | struct tun_struct *tun = netdev_priv(dev); | |
2284 | tun->debug = value; | |
2285 | #endif | |
2286 | } | |
2287 | ||
7282d491 | 2288 | static const struct ethtool_ops tun_ethtool_ops = { |
1da177e4 LT |
2289 | .get_settings = tun_get_settings, |
2290 | .get_drvinfo = tun_get_drvinfo, | |
2291 | .get_msglevel = tun_get_msglevel, | |
2292 | .set_msglevel = tun_set_msglevel, | |
bee31369 | 2293 | .get_link = ethtool_op_get_link, |
eda29772 | 2294 | .get_ts_info = ethtool_op_get_ts_info, |
1da177e4 LT |
2295 | }; |
2296 | ||
79d17604 | 2297 | |
1da177e4 LT |
2298 | static int __init tun_init(void) |
2299 | { | |
2300 | int ret = 0; | |
2301 | ||
6b8a66ee JP |
2302 | pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); |
2303 | pr_info("%s\n", DRV_COPYRIGHT); | |
1da177e4 | 2304 | |
f019a7a5 | 2305 | ret = rtnl_link_register(&tun_link_ops); |
79d17604 | 2306 | if (ret) { |
6b8a66ee | 2307 | pr_err("Can't register link_ops\n"); |
f019a7a5 | 2308 | goto err_linkops; |
79d17604 PE |
2309 | } |
2310 | ||
1da177e4 | 2311 | ret = misc_register(&tun_miscdev); |
79d17604 | 2312 | if (ret) { |
6b8a66ee | 2313 | pr_err("Can't register misc device %d\n", TUN_MINOR); |
79d17604 PE |
2314 | goto err_misc; |
2315 | } | |
f019a7a5 | 2316 | return 0; |
79d17604 | 2317 | err_misc: |
f019a7a5 EB |
2318 | rtnl_link_unregister(&tun_link_ops); |
2319 | err_linkops: | |
1da177e4 LT |
2320 | return ret; |
2321 | } | |
2322 | ||
2323 | static void tun_cleanup(void) | |
2324 | { | |
6aa20a22 | 2325 | misc_deregister(&tun_miscdev); |
f019a7a5 | 2326 | rtnl_link_unregister(&tun_link_ops); |
1da177e4 LT |
2327 | } |
2328 | ||
05c2828c MT |
2329 | /* Get an underlying socket object from tun file. Returns error unless file is |
2330 | * attached to a device. The returned object works like a packet socket, it | |
2331 | * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for | |
2332 | * holding a reference to the file for as long as the socket is in use. */ | |
2333 | struct socket *tun_get_socket(struct file *file) | |
2334 | { | |
6e914fc7 | 2335 | struct tun_file *tfile; |
05c2828c MT |
2336 | if (file->f_op != &tun_fops) |
2337 | return ERR_PTR(-EINVAL); | |
6e914fc7 JW |
2338 | tfile = file->private_data; |
2339 | if (!tfile) | |
05c2828c | 2340 | return ERR_PTR(-EBADFD); |
54f968d6 | 2341 | return &tfile->socket; |
05c2828c MT |
2342 | } |
2343 | EXPORT_SYMBOL_GPL(tun_get_socket); | |
2344 | ||
1da177e4 LT |
2345 | module_init(tun_init); |
2346 | module_exit(tun_cleanup); | |
2347 | MODULE_DESCRIPTION(DRV_DESCRIPTION); | |
2348 | MODULE_AUTHOR(DRV_COPYRIGHT); | |
2349 | MODULE_LICENSE("GPL"); | |
2350 | MODULE_ALIAS_MISCDEV(TUN_MINOR); | |
578454ff | 2351 | MODULE_ALIAS("devname:net/tun"); |