]>
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> | |
174cd4b1 | 47 | #include <linux/sched/signal.h> |
1da177e4 LT |
48 | #include <linux/major.h> |
49 | #include <linux/slab.h> | |
50 | #include <linux/poll.h> | |
51 | #include <linux/fcntl.h> | |
52 | #include <linux/init.h> | |
53 | #include <linux/skbuff.h> | |
54 | #include <linux/netdevice.h> | |
55 | #include <linux/etherdevice.h> | |
56 | #include <linux/miscdevice.h> | |
57 | #include <linux/ethtool.h> | |
58 | #include <linux/rtnetlink.h> | |
50857e2a | 59 | #include <linux/compat.h> |
1da177e4 LT |
60 | #include <linux/if.h> |
61 | #include <linux/if_arp.h> | |
62 | #include <linux/if_ether.h> | |
63 | #include <linux/if_tun.h> | |
6680ec68 | 64 | #include <linux/if_vlan.h> |
1da177e4 | 65 | #include <linux/crc32.h> |
d647a591 | 66 | #include <linux/nsproxy.h> |
f43798c2 | 67 | #include <linux/virtio_net.h> |
99405162 | 68 | #include <linux/rcupdate.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> |
e0b46d0e | 74 | #include <linux/uio.h> |
1576d986 | 75 | #include <linux/skb_array.h> |
761876c8 JW |
76 | #include <linux/bpf.h> |
77 | #include <linux/bpf_trace.h> | |
90e33d45 | 78 | #include <linux/mutex.h> |
1da177e4 | 79 | |
7c0f6ba6 | 80 | #include <linux/uaccess.h> |
1da177e4 | 81 | |
14daa021 RR |
82 | /* Uncomment to enable debugging */ |
83 | /* #define TUN_DEBUG 1 */ | |
84 | ||
1da177e4 LT |
85 | #ifdef TUN_DEBUG |
86 | static int debug; | |
14daa021 | 87 | |
6b8a66ee JP |
88 | #define tun_debug(level, tun, fmt, args...) \ |
89 | do { \ | |
90 | if (tun->debug) \ | |
91 | netdev_printk(level, tun->dev, fmt, ##args); \ | |
92 | } while (0) | |
93 | #define DBG1(level, fmt, args...) \ | |
94 | do { \ | |
95 | if (debug == 2) \ | |
96 | printk(level fmt, ##args); \ | |
97 | } while (0) | |
14daa021 | 98 | #else |
6b8a66ee JP |
99 | #define tun_debug(level, tun, fmt, args...) \ |
100 | do { \ | |
101 | if (0) \ | |
102 | netdev_printk(level, tun->dev, fmt, ##args); \ | |
103 | } while (0) | |
104 | #define DBG1(level, fmt, args...) \ | |
105 | do { \ | |
106 | if (0) \ | |
107 | printk(level fmt, ##args); \ | |
108 | } while (0) | |
14daa021 RR |
109 | #endif |
110 | ||
761876c8 | 111 | #define TUN_HEADROOM 256 |
7df13219 | 112 | #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD) |
66ccbc9c | 113 | |
031f5e03 MT |
114 | /* TUN device flags */ |
115 | ||
116 | /* IFF_ATTACH_QUEUE is never stored in device flags, | |
117 | * overload it to mean fasync when stored there. | |
118 | */ | |
119 | #define TUN_FASYNC IFF_ATTACH_QUEUE | |
1cf8e410 MT |
120 | /* High bits in flags field are unused. */ |
121 | #define TUN_VNET_LE 0x80000000 | |
8b8e658b | 122 | #define TUN_VNET_BE 0x40000000 |
031f5e03 MT |
123 | |
124 | #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \ | |
90e33d45 PP |
125 | IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS) |
126 | ||
0690899b MT |
127 | #define GOODCOPY_LEN 128 |
128 | ||
f271b2cc MK |
129 | #define FLT_EXACT_COUNT 8 |
130 | struct tap_filter { | |
131 | unsigned int count; /* Number of addrs. Zero means disabled */ | |
132 | u32 mask[2]; /* Mask of the hashed addrs */ | |
133 | unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; | |
134 | }; | |
135 | ||
baf71c5c PG |
136 | /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal |
137 | * to max number of VCPUs in guest. */ | |
138 | #define MAX_TAP_QUEUES 256 | |
b8732fb7 | 139 | #define MAX_TAP_FLOWS 4096 |
c8d68e6b | 140 | |
96442e42 JW |
141 | #define TUN_FLOW_EXPIRE (3 * HZ) |
142 | ||
608b9977 PA |
143 | struct tun_pcpu_stats { |
144 | u64 rx_packets; | |
145 | u64 rx_bytes; | |
146 | u64 tx_packets; | |
147 | u64 tx_bytes; | |
148 | struct u64_stats_sync syncp; | |
149 | u32 rx_dropped; | |
150 | u32 tx_dropped; | |
151 | u32 rx_frame_errors; | |
152 | }; | |
153 | ||
54f968d6 | 154 | /* A tun_file connects an open character device to a tuntap netdevice. It |
92d4ea6e | 155 | * also contains all socket related structures (except sock_fprog and tap_filter) |
54f968d6 JW |
156 | * to serve as one transmit queue for tuntap device. The sock_fprog and |
157 | * tap_filter were kept in tun_struct since they were used for filtering for the | |
36fe8c09 | 158 | * netdevice not for a specific queue (at least I didn't see the requirement for |
54f968d6 | 159 | * this). |
6e914fc7 JW |
160 | * |
161 | * RCU usage: | |
36fe8c09 | 162 | * The tun_file and tun_struct are loosely coupled, the pointer from one to the |
6e914fc7 | 163 | * other can only be read while rcu_read_lock or rtnl_lock is held. |
54f968d6 | 164 | */ |
631ab46b | 165 | struct tun_file { |
54f968d6 JW |
166 | struct sock sk; |
167 | struct socket socket; | |
168 | struct socket_wq wq; | |
6e914fc7 | 169 | struct tun_struct __rcu *tun; |
54f968d6 JW |
170 | struct fasync_struct *fasync; |
171 | /* only used for fasnyc */ | |
172 | unsigned int flags; | |
fb7589a1 PE |
173 | union { |
174 | u16 queue_index; | |
175 | unsigned int ifindex; | |
176 | }; | |
94317099 | 177 | struct napi_struct napi; |
aec72f33 | 178 | bool napi_enabled; |
90e33d45 | 179 | struct mutex napi_mutex; /* Protects access to the above napi */ |
4008e97f JW |
180 | struct list_head next; |
181 | struct tun_struct *detached; | |
1576d986 | 182 | struct skb_array tx_array; |
631ab46b EB |
183 | }; |
184 | ||
96442e42 JW |
185 | struct tun_flow_entry { |
186 | struct hlist_node hash_link; | |
187 | struct rcu_head rcu; | |
188 | struct tun_struct *tun; | |
189 | ||
190 | u32 rxhash; | |
9bc88939 | 191 | u32 rps_rxhash; |
96442e42 JW |
192 | int queue_index; |
193 | unsigned long updated; | |
194 | }; | |
195 | ||
196 | #define TUN_NUM_FLOW_ENTRIES 1024 | |
197 | ||
54f968d6 | 198 | /* Since the socket were moved to tun_file, to preserve the behavior of persist |
36fe8c09 | 199 | * device, socket filter, sndbuf and vnet header size were restore when the |
54f968d6 JW |
200 | * file were attached to a persist device. |
201 | */ | |
14daa021 | 202 | struct tun_struct { |
c8d68e6b JW |
203 | struct tun_file __rcu *tfiles[MAX_TAP_QUEUES]; |
204 | unsigned int numqueues; | |
f271b2cc | 205 | unsigned int flags; |
0625c883 EB |
206 | kuid_t owner; |
207 | kgid_t group; | |
14daa021 | 208 | |
14daa021 | 209 | struct net_device *dev; |
c8f44aff | 210 | netdev_features_t set_features; |
88255375 | 211 | #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \ |
d591a1f3 | 212 | NETIF_F_TSO6) |
d9d52b51 | 213 | |
eaea34b2 | 214 | int align; |
d9d52b51 | 215 | int vnet_hdr_sz; |
54f968d6 JW |
216 | int sndbuf; |
217 | struct tap_filter txflt; | |
218 | struct sock_fprog fprog; | |
219 | /* protected by rtnl lock */ | |
220 | bool filter_attached; | |
14daa021 RR |
221 | #ifdef TUN_DEBUG |
222 | int debug; | |
1da177e4 | 223 | #endif |
96442e42 | 224 | spinlock_t lock; |
96442e42 JW |
225 | struct hlist_head flows[TUN_NUM_FLOW_ENTRIES]; |
226 | struct timer_list flow_gc_timer; | |
227 | unsigned long ageing_time; | |
4008e97f JW |
228 | unsigned int numdisabled; |
229 | struct list_head disabled; | |
5dbbaf2d | 230 | void *security; |
b8732fb7 | 231 | u32 flow_count; |
5503fcec | 232 | u32 rx_batched; |
608b9977 | 233 | struct tun_pcpu_stats __percpu *pcpu_stats; |
761876c8 | 234 | struct bpf_prog __rcu *xdp_prog; |
14daa021 | 235 | }; |
1da177e4 | 236 | |
94317099 PP |
237 | static int tun_napi_receive(struct napi_struct *napi, int budget) |
238 | { | |
239 | struct tun_file *tfile = container_of(napi, struct tun_file, napi); | |
240 | struct sk_buff_head *queue = &tfile->sk.sk_write_queue; | |
241 | struct sk_buff_head process_queue; | |
242 | struct sk_buff *skb; | |
243 | int received = 0; | |
244 | ||
245 | __skb_queue_head_init(&process_queue); | |
246 | ||
247 | spin_lock(&queue->lock); | |
248 | skb_queue_splice_tail_init(queue, &process_queue); | |
249 | spin_unlock(&queue->lock); | |
250 | ||
251 | while (received < budget && (skb = __skb_dequeue(&process_queue))) { | |
252 | napi_gro_receive(napi, skb); | |
253 | ++received; | |
254 | } | |
255 | ||
256 | if (!skb_queue_empty(&process_queue)) { | |
257 | spin_lock(&queue->lock); | |
258 | skb_queue_splice(&process_queue, queue); | |
259 | spin_unlock(&queue->lock); | |
260 | } | |
261 | ||
262 | return received; | |
263 | } | |
264 | ||
265 | static int tun_napi_poll(struct napi_struct *napi, int budget) | |
266 | { | |
267 | unsigned int received; | |
268 | ||
269 | received = tun_napi_receive(napi, budget); | |
270 | ||
271 | if (received < budget) | |
272 | napi_complete_done(napi, received); | |
273 | ||
274 | return received; | |
275 | } | |
276 | ||
277 | static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, | |
278 | bool napi_en) | |
279 | { | |
aec72f33 | 280 | tfile->napi_enabled = napi_en; |
94317099 PP |
281 | if (napi_en) { |
282 | netif_napi_add(tun->dev, &tfile->napi, tun_napi_poll, | |
283 | NAPI_POLL_WEIGHT); | |
284 | napi_enable(&tfile->napi); | |
90e33d45 | 285 | mutex_init(&tfile->napi_mutex); |
94317099 PP |
286 | } |
287 | } | |
288 | ||
289 | static void tun_napi_disable(struct tun_struct *tun, struct tun_file *tfile) | |
290 | { | |
aec72f33 | 291 | if (tfile->napi_enabled) |
94317099 PP |
292 | napi_disable(&tfile->napi); |
293 | } | |
294 | ||
295 | static void tun_napi_del(struct tun_struct *tun, struct tun_file *tfile) | |
296 | { | |
aec72f33 | 297 | if (tfile->napi_enabled) |
94317099 PP |
298 | netif_napi_del(&tfile->napi); |
299 | } | |
300 | ||
90e33d45 PP |
301 | static bool tun_napi_frags_enabled(const struct tun_struct *tun) |
302 | { | |
303 | return READ_ONCE(tun->flags) & IFF_NAPI_FRAGS; | |
304 | } | |
305 | ||
8b8e658b GK |
306 | #ifdef CONFIG_TUN_VNET_CROSS_LE |
307 | static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) | |
308 | { | |
309 | return tun->flags & TUN_VNET_BE ? false : | |
310 | virtio_legacy_is_little_endian(); | |
311 | } | |
312 | ||
313 | static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) | |
314 | { | |
315 | int be = !!(tun->flags & TUN_VNET_BE); | |
316 | ||
317 | if (put_user(be, argp)) | |
318 | return -EFAULT; | |
319 | ||
320 | return 0; | |
321 | } | |
322 | ||
323 | static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) | |
324 | { | |
325 | int be; | |
326 | ||
327 | if (get_user(be, argp)) | |
328 | return -EFAULT; | |
329 | ||
330 | if (be) | |
331 | tun->flags |= TUN_VNET_BE; | |
332 | else | |
333 | tun->flags &= ~TUN_VNET_BE; | |
334 | ||
335 | return 0; | |
336 | } | |
337 | #else | |
338 | static inline bool tun_legacy_is_little_endian(struct tun_struct *tun) | |
339 | { | |
340 | return virtio_legacy_is_little_endian(); | |
341 | } | |
342 | ||
343 | static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) | |
344 | { | |
345 | return -EINVAL; | |
346 | } | |
347 | ||
348 | static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) | |
349 | { | |
350 | return -EINVAL; | |
351 | } | |
352 | #endif /* CONFIG_TUN_VNET_CROSS_LE */ | |
353 | ||
25bd55bb GK |
354 | static inline bool tun_is_little_endian(struct tun_struct *tun) |
355 | { | |
7d824109 | 356 | return tun->flags & TUN_VNET_LE || |
8b8e658b | 357 | tun_legacy_is_little_endian(tun); |
25bd55bb GK |
358 | } |
359 | ||
56f0dcc5 MT |
360 | static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val) |
361 | { | |
25bd55bb | 362 | return __virtio16_to_cpu(tun_is_little_endian(tun), val); |
56f0dcc5 MT |
363 | } |
364 | ||
365 | static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val) | |
366 | { | |
25bd55bb | 367 | return __cpu_to_virtio16(tun_is_little_endian(tun), val); |
56f0dcc5 MT |
368 | } |
369 | ||
96442e42 JW |
370 | static inline u32 tun_hashfn(u32 rxhash) |
371 | { | |
372 | return rxhash & 0x3ff; | |
373 | } | |
374 | ||
375 | static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash) | |
376 | { | |
377 | struct tun_flow_entry *e; | |
96442e42 | 378 | |
b67bfe0d | 379 | hlist_for_each_entry_rcu(e, head, hash_link) { |
96442e42 JW |
380 | if (e->rxhash == rxhash) |
381 | return e; | |
382 | } | |
383 | return NULL; | |
384 | } | |
385 | ||
386 | static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun, | |
387 | struct hlist_head *head, | |
388 | u32 rxhash, u16 queue_index) | |
389 | { | |
9fdc6bef ED |
390 | struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC); |
391 | ||
96442e42 JW |
392 | if (e) { |
393 | tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n", | |
394 | rxhash, queue_index); | |
395 | e->updated = jiffies; | |
396 | e->rxhash = rxhash; | |
9bc88939 | 397 | e->rps_rxhash = 0; |
96442e42 JW |
398 | e->queue_index = queue_index; |
399 | e->tun = tun; | |
400 | hlist_add_head_rcu(&e->hash_link, head); | |
b8732fb7 | 401 | ++tun->flow_count; |
96442e42 JW |
402 | } |
403 | return e; | |
404 | } | |
405 | ||
96442e42 JW |
406 | static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e) |
407 | { | |
408 | tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n", | |
409 | e->rxhash, e->queue_index); | |
410 | hlist_del_rcu(&e->hash_link); | |
9fdc6bef | 411 | kfree_rcu(e, rcu); |
b8732fb7 | 412 | --tun->flow_count; |
96442e42 JW |
413 | } |
414 | ||
415 | static void tun_flow_flush(struct tun_struct *tun) | |
416 | { | |
417 | int i; | |
418 | ||
419 | spin_lock_bh(&tun->lock); | |
420 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { | |
421 | struct tun_flow_entry *e; | |
b67bfe0d | 422 | struct hlist_node *n; |
96442e42 | 423 | |
b67bfe0d | 424 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) |
96442e42 JW |
425 | tun_flow_delete(tun, e); |
426 | } | |
427 | spin_unlock_bh(&tun->lock); | |
428 | } | |
429 | ||
430 | static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index) | |
431 | { | |
432 | int i; | |
433 | ||
434 | spin_lock_bh(&tun->lock); | |
435 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { | |
436 | struct tun_flow_entry *e; | |
b67bfe0d | 437 | struct hlist_node *n; |
96442e42 | 438 | |
b67bfe0d | 439 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
96442e42 JW |
440 | if (e->queue_index == queue_index) |
441 | tun_flow_delete(tun, e); | |
442 | } | |
443 | } | |
444 | spin_unlock_bh(&tun->lock); | |
445 | } | |
446 | ||
e99e88a9 | 447 | static void tun_flow_cleanup(struct timer_list *t) |
96442e42 | 448 | { |
e99e88a9 | 449 | struct tun_struct *tun = from_timer(tun, t, flow_gc_timer); |
96442e42 JW |
450 | unsigned long delay = tun->ageing_time; |
451 | unsigned long next_timer = jiffies + delay; | |
452 | unsigned long count = 0; | |
453 | int i; | |
454 | ||
455 | tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n"); | |
456 | ||
7dbfb4ef | 457 | spin_lock(&tun->lock); |
96442e42 JW |
458 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) { |
459 | struct tun_flow_entry *e; | |
b67bfe0d | 460 | struct hlist_node *n; |
96442e42 | 461 | |
b67bfe0d | 462 | hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) { |
96442e42 | 463 | unsigned long this_timer; |
81d98fa4 | 464 | |
96442e42 | 465 | this_timer = e->updated + delay; |
81d98fa4 | 466 | if (time_before_eq(this_timer, jiffies)) { |
96442e42 | 467 | tun_flow_delete(tun, e); |
81d98fa4 ED |
468 | continue; |
469 | } | |
470 | count++; | |
471 | if (time_before(this_timer, next_timer)) | |
96442e42 JW |
472 | next_timer = this_timer; |
473 | } | |
474 | } | |
475 | ||
476 | if (count) | |
477 | mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer)); | |
7dbfb4ef | 478 | spin_unlock(&tun->lock); |
96442e42 JW |
479 | } |
480 | ||
49974420 | 481 | static void tun_flow_update(struct tun_struct *tun, u32 rxhash, |
9e85722d | 482 | struct tun_file *tfile) |
96442e42 JW |
483 | { |
484 | struct hlist_head *head; | |
485 | struct tun_flow_entry *e; | |
486 | unsigned long delay = tun->ageing_time; | |
9e85722d | 487 | u16 queue_index = tfile->queue_index; |
96442e42 JW |
488 | |
489 | if (!rxhash) | |
490 | return; | |
491 | else | |
492 | head = &tun->flows[tun_hashfn(rxhash)]; | |
493 | ||
494 | rcu_read_lock(); | |
495 | ||
9e85722d JW |
496 | /* We may get a very small possibility of OOO during switching, not |
497 | * worth to optimize.*/ | |
498 | if (tun->numqueues == 1 || tfile->detached) | |
96442e42 JW |
499 | goto unlock; |
500 | ||
501 | e = tun_flow_find(head, rxhash); | |
502 | if (likely(e)) { | |
503 | /* TODO: keep queueing to old queue until it's empty? */ | |
504 | e->queue_index = queue_index; | |
505 | e->updated = jiffies; | |
9bc88939 | 506 | sock_rps_record_flow_hash(e->rps_rxhash); |
96442e42 JW |
507 | } else { |
508 | spin_lock_bh(&tun->lock); | |
b8732fb7 JW |
509 | if (!tun_flow_find(head, rxhash) && |
510 | tun->flow_count < MAX_TAP_FLOWS) | |
96442e42 JW |
511 | tun_flow_create(tun, head, rxhash, queue_index); |
512 | ||
513 | if (!timer_pending(&tun->flow_gc_timer)) | |
514 | mod_timer(&tun->flow_gc_timer, | |
515 | round_jiffies_up(jiffies + delay)); | |
516 | spin_unlock_bh(&tun->lock); | |
517 | } | |
518 | ||
519 | unlock: | |
520 | rcu_read_unlock(); | |
521 | } | |
522 | ||
9bc88939 TH |
523 | /** |
524 | * Save the hash received in the stack receive path and update the | |
525 | * flow_hash table accordingly. | |
526 | */ | |
527 | static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash) | |
528 | { | |
567e4b79 | 529 | if (unlikely(e->rps_rxhash != hash)) |
9bc88939 | 530 | e->rps_rxhash = hash; |
9bc88939 TH |
531 | } |
532 | ||
c8d68e6b | 533 | /* We try to identify a flow through its rxhash first. The reason that |
92d4ea6e | 534 | * we do not check rxq no. is because some cards(e.g 82599), chooses |
c8d68e6b JW |
535 | * the rxq based on the txq where the last packet of the flow comes. As |
536 | * the userspace application move between processors, we may get a | |
537 | * different rxq no. here. If we could not get rxhash, then we would | |
538 | * hope the rxq no. may help here. | |
539 | */ | |
f663dd9a | 540 | static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb, |
99932d4f | 541 | void *accel_priv, select_queue_fallback_t fallback) |
c8d68e6b JW |
542 | { |
543 | struct tun_struct *tun = netdev_priv(dev); | |
96442e42 | 544 | struct tun_flow_entry *e; |
c8d68e6b JW |
545 | u32 txq = 0; |
546 | u32 numqueues = 0; | |
547 | ||
548 | rcu_read_lock(); | |
6aa7de05 | 549 | numqueues = READ_ONCE(tun->numqueues); |
c8d68e6b | 550 | |
feec084a | 551 | txq = __skb_get_hash_symmetric(skb); |
c8d68e6b | 552 | if (txq) { |
96442e42 | 553 | e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq); |
9bc88939 | 554 | if (e) { |
9bc88939 | 555 | tun_flow_save_rps_rxhash(e, txq); |
fbe4d456 | 556 | txq = e->queue_index; |
9bc88939 | 557 | } else |
96442e42 JW |
558 | /* use multiply and shift instead of expensive divide */ |
559 | txq = ((u64)txq * numqueues) >> 32; | |
c8d68e6b JW |
560 | } else if (likely(skb_rx_queue_recorded(skb))) { |
561 | txq = skb_get_rx_queue(skb); | |
562 | while (unlikely(txq >= numqueues)) | |
563 | txq -= numqueues; | |
564 | } | |
565 | ||
566 | rcu_read_unlock(); | |
567 | return txq; | |
568 | } | |
569 | ||
cde8b15f JW |
570 | static inline bool tun_not_capable(struct tun_struct *tun) |
571 | { | |
572 | const struct cred *cred = current_cred(); | |
c260b772 | 573 | struct net *net = dev_net(tun->dev); |
cde8b15f JW |
574 | |
575 | return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || | |
576 | (gid_valid(tun->group) && !in_egroup_p(tun->group))) && | |
c260b772 | 577 | !ns_capable(net->user_ns, CAP_NET_ADMIN); |
cde8b15f JW |
578 | } |
579 | ||
c8d68e6b JW |
580 | static void tun_set_real_num_queues(struct tun_struct *tun) |
581 | { | |
582 | netif_set_real_num_tx_queues(tun->dev, tun->numqueues); | |
583 | netif_set_real_num_rx_queues(tun->dev, tun->numqueues); | |
584 | } | |
585 | ||
4008e97f JW |
586 | static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) |
587 | { | |
588 | tfile->detached = tun; | |
589 | list_add_tail(&tfile->next, &tun->disabled); | |
590 | ++tun->numdisabled; | |
591 | } | |
592 | ||
d32649d1 | 593 | static struct tun_struct *tun_enable_queue(struct tun_file *tfile) |
4008e97f JW |
594 | { |
595 | struct tun_struct *tun = tfile->detached; | |
596 | ||
597 | tfile->detached = NULL; | |
598 | list_del_init(&tfile->next); | |
599 | --tun->numdisabled; | |
600 | return tun; | |
601 | } | |
602 | ||
4bfb0513 JW |
603 | static void tun_queue_purge(struct tun_file *tfile) |
604 | { | |
1576d986 JW |
605 | struct sk_buff *skb; |
606 | ||
607 | while ((skb = skb_array_consume(&tfile->tx_array)) != NULL) | |
608 | kfree_skb(skb); | |
609 | ||
5503fcec | 610 | skb_queue_purge(&tfile->sk.sk_write_queue); |
4bfb0513 JW |
611 | skb_queue_purge(&tfile->sk.sk_error_queue); |
612 | } | |
613 | ||
c8d68e6b JW |
614 | static void __tun_detach(struct tun_file *tfile, bool clean) |
615 | { | |
616 | struct tun_file *ntfile; | |
617 | struct tun_struct *tun; | |
c8d68e6b | 618 | |
b8deabd3 JW |
619 | tun = rtnl_dereference(tfile->tun); |
620 | ||
94317099 PP |
621 | if (tun && clean) { |
622 | tun_napi_disable(tun, tfile); | |
623 | tun_napi_del(tun, tfile); | |
624 | } | |
625 | ||
9e85722d | 626 | if (tun && !tfile->detached) { |
c8d68e6b JW |
627 | u16 index = tfile->queue_index; |
628 | BUG_ON(index >= tun->numqueues); | |
c8d68e6b JW |
629 | |
630 | rcu_assign_pointer(tun->tfiles[index], | |
631 | tun->tfiles[tun->numqueues - 1]); | |
b8deabd3 | 632 | ntfile = rtnl_dereference(tun->tfiles[index]); |
c8d68e6b JW |
633 | ntfile->queue_index = index; |
634 | ||
635 | --tun->numqueues; | |
9e85722d | 636 | if (clean) { |
c956674b | 637 | RCU_INIT_POINTER(tfile->tun, NULL); |
4008e97f | 638 | sock_put(&tfile->sk); |
9e85722d | 639 | } else |
4008e97f | 640 | tun_disable_queue(tun, tfile); |
c8d68e6b JW |
641 | |
642 | synchronize_net(); | |
96442e42 | 643 | tun_flow_delete_by_queue(tun, tun->numqueues + 1); |
c8d68e6b | 644 | /* Drop read queue */ |
4bfb0513 | 645 | tun_queue_purge(tfile); |
c8d68e6b | 646 | tun_set_real_num_queues(tun); |
dd38bd85 | 647 | } else if (tfile->detached && clean) { |
4008e97f | 648 | tun = tun_enable_queue(tfile); |
dd38bd85 JW |
649 | sock_put(&tfile->sk); |
650 | } | |
c8d68e6b JW |
651 | |
652 | if (clean) { | |
af668b3c MT |
653 | if (tun && tun->numqueues == 0 && tun->numdisabled == 0) { |
654 | netif_carrier_off(tun->dev); | |
655 | ||
40630b82 | 656 | if (!(tun->flags & IFF_PERSIST) && |
af668b3c | 657 | tun->dev->reg_state == NETREG_REGISTERED) |
4008e97f | 658 | unregister_netdevice(tun->dev); |
af668b3c | 659 | } |
1576d986 JW |
660 | if (tun) |
661 | skb_array_cleanup(&tfile->tx_array); | |
140e807d | 662 | sock_put(&tfile->sk); |
c8d68e6b JW |
663 | } |
664 | } | |
665 | ||
666 | static void tun_detach(struct tun_file *tfile, bool clean) | |
667 | { | |
668 | rtnl_lock(); | |
669 | __tun_detach(tfile, clean); | |
670 | rtnl_unlock(); | |
671 | } | |
672 | ||
673 | static void tun_detach_all(struct net_device *dev) | |
674 | { | |
675 | struct tun_struct *tun = netdev_priv(dev); | |
4008e97f | 676 | struct tun_file *tfile, *tmp; |
c8d68e6b JW |
677 | int i, n = tun->numqueues; |
678 | ||
679 | for (i = 0; i < n; i++) { | |
b8deabd3 | 680 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b | 681 | BUG_ON(!tfile); |
94317099 | 682 | tun_napi_disable(tun, tfile); |
addf8fc4 | 683 | tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; |
9e641bdc | 684 | tfile->socket.sk->sk_data_ready(tfile->socket.sk); |
c956674b | 685 | RCU_INIT_POINTER(tfile->tun, NULL); |
c8d68e6b JW |
686 | --tun->numqueues; |
687 | } | |
9e85722d | 688 | list_for_each_entry(tfile, &tun->disabled, next) { |
addf8fc4 | 689 | tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; |
9e641bdc | 690 | tfile->socket.sk->sk_data_ready(tfile->socket.sk); |
c956674b | 691 | RCU_INIT_POINTER(tfile->tun, NULL); |
9e85722d | 692 | } |
c8d68e6b JW |
693 | BUG_ON(tun->numqueues != 0); |
694 | ||
695 | synchronize_net(); | |
696 | for (i = 0; i < n; i++) { | |
b8deabd3 | 697 | tfile = rtnl_dereference(tun->tfiles[i]); |
94317099 | 698 | tun_napi_del(tun, tfile); |
c8d68e6b | 699 | /* Drop read queue */ |
4bfb0513 | 700 | tun_queue_purge(tfile); |
c8d68e6b JW |
701 | sock_put(&tfile->sk); |
702 | } | |
4008e97f JW |
703 | list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { |
704 | tun_enable_queue(tfile); | |
4bfb0513 | 705 | tun_queue_purge(tfile); |
4008e97f JW |
706 | sock_put(&tfile->sk); |
707 | } | |
708 | BUG_ON(tun->numdisabled != 0); | |
dd38bd85 | 709 | |
40630b82 | 710 | if (tun->flags & IFF_PERSIST) |
dd38bd85 | 711 | module_put(THIS_MODULE); |
c8d68e6b JW |
712 | } |
713 | ||
94317099 PP |
714 | static int tun_attach(struct tun_struct *tun, struct file *file, |
715 | bool skip_filter, bool napi) | |
a7385ba2 | 716 | { |
631ab46b | 717 | struct tun_file *tfile = file->private_data; |
1576d986 | 718 | struct net_device *dev = tun->dev; |
38231b7a | 719 | int err; |
a7385ba2 | 720 | |
5dbbaf2d PM |
721 | err = security_tun_dev_attach(tfile->socket.sk, tun->security); |
722 | if (err < 0) | |
723 | goto out; | |
724 | ||
38231b7a | 725 | err = -EINVAL; |
9e85722d | 726 | if (rtnl_dereference(tfile->tun) && !tfile->detached) |
38231b7a EB |
727 | goto out; |
728 | ||
729 | err = -EBUSY; | |
40630b82 | 730 | if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1) |
c8d68e6b JW |
731 | goto out; |
732 | ||
733 | err = -E2BIG; | |
4008e97f JW |
734 | if (!tfile->detached && |
735 | tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES) | |
38231b7a EB |
736 | goto out; |
737 | ||
738 | err = 0; | |
54f968d6 | 739 | |
92d4ea6e | 740 | /* Re-attach the filter to persist device */ |
849c9b6f | 741 | if (!skip_filter && (tun->filter_attached == true)) { |
8ced425e HFS |
742 | lock_sock(tfile->socket.sk); |
743 | err = sk_attach_filter(&tun->fprog, tfile->socket.sk); | |
744 | release_sock(tfile->socket.sk); | |
54f968d6 JW |
745 | if (!err) |
746 | goto out; | |
747 | } | |
1576d986 JW |
748 | |
749 | if (!tfile->detached && | |
750 | skb_array_init(&tfile->tx_array, dev->tx_queue_len, GFP_KERNEL)) { | |
751 | err = -ENOMEM; | |
752 | goto out; | |
753 | } | |
754 | ||
c8d68e6b | 755 | tfile->queue_index = tun->numqueues; |
addf8fc4 | 756 | tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; |
6e914fc7 | 757 | rcu_assign_pointer(tfile->tun, tun); |
c8d68e6b | 758 | rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); |
c8d68e6b | 759 | tun->numqueues++; |
a7385ba2 | 760 | |
94317099 | 761 | if (tfile->detached) { |
4008e97f | 762 | tun_enable_queue(tfile); |
94317099 | 763 | } else { |
4008e97f | 764 | sock_hold(&tfile->sk); |
94317099 PP |
765 | tun_napi_init(tun, tfile, napi); |
766 | } | |
4008e97f | 767 | |
c8d68e6b | 768 | tun_set_real_num_queues(tun); |
a7385ba2 | 769 | |
c8d68e6b JW |
770 | /* device is allowed to go away first, so no need to hold extra |
771 | * refcnt. | |
772 | */ | |
773 | ||
774 | out: | |
775 | return err; | |
631ab46b EB |
776 | } |
777 | ||
9484dc74 | 778 | static struct tun_struct *tun_get(struct tun_file *tfile) |
631ab46b | 779 | { |
6e914fc7 | 780 | struct tun_struct *tun; |
c70f1829 | 781 | |
6e914fc7 JW |
782 | rcu_read_lock(); |
783 | tun = rcu_dereference(tfile->tun); | |
784 | if (tun) | |
785 | dev_hold(tun->dev); | |
786 | rcu_read_unlock(); | |
c70f1829 EB |
787 | |
788 | return tun; | |
631ab46b EB |
789 | } |
790 | ||
631ab46b EB |
791 | static void tun_put(struct tun_struct *tun) |
792 | { | |
6e914fc7 | 793 | dev_put(tun->dev); |
631ab46b EB |
794 | } |
795 | ||
6b8a66ee | 796 | /* TAP filtering */ |
f271b2cc MK |
797 | static void addr_hash_set(u32 *mask, const u8 *addr) |
798 | { | |
799 | int n = ether_crc(ETH_ALEN, addr) >> 26; | |
800 | mask[n >> 5] |= (1 << (n & 31)); | |
801 | } | |
802 | ||
803 | static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) | |
804 | { | |
805 | int n = ether_crc(ETH_ALEN, addr) >> 26; | |
806 | return mask[n >> 5] & (1 << (n & 31)); | |
807 | } | |
808 | ||
809 | static int update_filter(struct tap_filter *filter, void __user *arg) | |
810 | { | |
811 | struct { u8 u[ETH_ALEN]; } *addr; | |
812 | struct tun_filter uf; | |
813 | int err, alen, n, nexact; | |
814 | ||
815 | if (copy_from_user(&uf, arg, sizeof(uf))) | |
816 | return -EFAULT; | |
817 | ||
818 | if (!uf.count) { | |
819 | /* Disabled */ | |
820 | filter->count = 0; | |
821 | return 0; | |
822 | } | |
823 | ||
824 | alen = ETH_ALEN * uf.count; | |
28e8190d ME |
825 | addr = memdup_user(arg + sizeof(uf), alen); |
826 | if (IS_ERR(addr)) | |
827 | return PTR_ERR(addr); | |
f271b2cc MK |
828 | |
829 | /* The filter is updated without holding any locks. Which is | |
830 | * perfectly safe. We disable it first and in the worst | |
831 | * case we'll accept a few undesired packets. */ | |
832 | filter->count = 0; | |
833 | wmb(); | |
834 | ||
835 | /* Use first set of addresses as an exact filter */ | |
836 | for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) | |
837 | memcpy(filter->addr[n], addr[n].u, ETH_ALEN); | |
838 | ||
839 | nexact = n; | |
840 | ||
cfbf84fc AW |
841 | /* Remaining multicast addresses are hashed, |
842 | * unicast will leave the filter disabled. */ | |
f271b2cc | 843 | memset(filter->mask, 0, sizeof(filter->mask)); |
cfbf84fc AW |
844 | for (; n < uf.count; n++) { |
845 | if (!is_multicast_ether_addr(addr[n].u)) { | |
846 | err = 0; /* no filter */ | |
3b8d2a69 | 847 | goto free_addr; |
cfbf84fc | 848 | } |
f271b2cc | 849 | addr_hash_set(filter->mask, addr[n].u); |
cfbf84fc | 850 | } |
f271b2cc MK |
851 | |
852 | /* For ALLMULTI just set the mask to all ones. | |
853 | * This overrides the mask populated above. */ | |
854 | if ((uf.flags & TUN_FLT_ALLMULTI)) | |
855 | memset(filter->mask, ~0, sizeof(filter->mask)); | |
856 | ||
857 | /* Now enable the filter */ | |
858 | wmb(); | |
859 | filter->count = nexact; | |
860 | ||
861 | /* Return the number of exact filters */ | |
862 | err = nexact; | |
3b8d2a69 | 863 | free_addr: |
f271b2cc MK |
864 | kfree(addr); |
865 | return err; | |
866 | } | |
867 | ||
868 | /* Returns: 0 - drop, !=0 - accept */ | |
869 | static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) | |
870 | { | |
871 | /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect | |
872 | * at this point. */ | |
873 | struct ethhdr *eh = (struct ethhdr *) skb->data; | |
874 | int i; | |
875 | ||
876 | /* Exact match */ | |
877 | for (i = 0; i < filter->count; i++) | |
2e42e474 | 878 | if (ether_addr_equal(eh->h_dest, filter->addr[i])) |
f271b2cc MK |
879 | return 1; |
880 | ||
881 | /* Inexact match (multicast only) */ | |
882 | if (is_multicast_ether_addr(eh->h_dest)) | |
883 | return addr_hash_test(filter->mask, eh->h_dest); | |
884 | ||
885 | return 0; | |
886 | } | |
887 | ||
888 | /* | |
889 | * Checks whether the packet is accepted or not. | |
890 | * Returns: 0 - drop, !=0 - accept | |
891 | */ | |
892 | static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) | |
893 | { | |
894 | if (!filter->count) | |
895 | return 1; | |
896 | ||
897 | return run_filter(filter, skb); | |
898 | } | |
899 | ||
1da177e4 LT |
900 | /* Network device part of the driver */ |
901 | ||
7282d491 | 902 | static const struct ethtool_ops tun_ethtool_ops; |
1da177e4 | 903 | |
c70f1829 EB |
904 | /* Net device detach from fd. */ |
905 | static void tun_net_uninit(struct net_device *dev) | |
906 | { | |
c8d68e6b | 907 | tun_detach_all(dev); |
c70f1829 EB |
908 | } |
909 | ||
1da177e4 LT |
910 | /* Net device open. */ |
911 | static int tun_net_open(struct net_device *dev) | |
912 | { | |
b20e2d54 HFS |
913 | struct tun_struct *tun = netdev_priv(dev); |
914 | int i; | |
915 | ||
c8d68e6b | 916 | netif_tx_start_all_queues(dev); |
b20e2d54 HFS |
917 | |
918 | for (i = 0; i < tun->numqueues; i++) { | |
919 | struct tun_file *tfile; | |
920 | ||
921 | tfile = rtnl_dereference(tun->tfiles[i]); | |
922 | tfile->socket.sk->sk_write_space(tfile->socket.sk); | |
923 | } | |
924 | ||
1da177e4 LT |
925 | return 0; |
926 | } | |
927 | ||
928 | /* Net device close. */ | |
929 | static int tun_net_close(struct net_device *dev) | |
930 | { | |
c8d68e6b | 931 | netif_tx_stop_all_queues(dev); |
1da177e4 LT |
932 | return 0; |
933 | } | |
934 | ||
935 | /* Net device start xmit */ | |
424efe9c | 936 | static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
937 | { |
938 | struct tun_struct *tun = netdev_priv(dev); | |
c8d68e6b | 939 | int txq = skb->queue_mapping; |
6e914fc7 | 940 | struct tun_file *tfile; |
fa35864e | 941 | u32 numqueues = 0; |
1da177e4 | 942 | |
6e914fc7 | 943 | rcu_read_lock(); |
c8d68e6b | 944 | tfile = rcu_dereference(tun->tfiles[txq]); |
6aa7de05 | 945 | numqueues = READ_ONCE(tun->numqueues); |
c8d68e6b | 946 | |
1da177e4 | 947 | /* Drop packet if interface is not attached */ |
fa35864e | 948 | if (txq >= numqueues) |
1da177e4 LT |
949 | goto drop; |
950 | ||
3df97ba8 JW |
951 | #ifdef CONFIG_RPS |
952 | if (numqueues == 1 && static_key_false(&rps_needed)) { | |
9bc88939 TH |
953 | /* Select queue was not called for the skbuff, so we extract the |
954 | * RPS hash and save it into the flow_table here. | |
955 | */ | |
956 | __u32 rxhash; | |
957 | ||
feec084a | 958 | rxhash = __skb_get_hash_symmetric(skb); |
9bc88939 TH |
959 | if (rxhash) { |
960 | struct tun_flow_entry *e; | |
961 | e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], | |
962 | rxhash); | |
963 | if (e) | |
964 | tun_flow_save_rps_rxhash(e, rxhash); | |
965 | } | |
966 | } | |
3df97ba8 | 967 | #endif |
9bc88939 | 968 | |
6e914fc7 JW |
969 | tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len); |
970 | ||
c8d68e6b JW |
971 | BUG_ON(!tfile); |
972 | ||
f271b2cc MK |
973 | /* Drop if the filter does not like it. |
974 | * This is a noop if the filter is disabled. | |
975 | * Filter can be enabled only for the TAP devices. */ | |
976 | if (!check_filter(&tun->txflt, skb)) | |
977 | goto drop; | |
978 | ||
54f968d6 JW |
979 | if (tfile->socket.sk->sk_filter && |
980 | sk_filter(tfile->socket.sk, skb)) | |
99405162 MT |
981 | goto drop; |
982 | ||
1f8b977a | 983 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
7bf66305 JW |
984 | goto drop; |
985 | ||
7b996243 | 986 | skb_tx_timestamp(skb); |
eda29772 | 987 | |
0110d6f2 | 988 | /* Orphan the skb - required as we might hang on to it |
7bf66305 JW |
989 | * for indefinite time. |
990 | */ | |
0110d6f2 MT |
991 | skb_orphan(skb); |
992 | ||
f8af75f3 ED |
993 | nf_reset(skb); |
994 | ||
1576d986 JW |
995 | if (skb_array_produce(&tfile->tx_array, skb)) |
996 | goto drop; | |
1da177e4 LT |
997 | |
998 | /* Notify and wake up reader process */ | |
54f968d6 JW |
999 | if (tfile->flags & TUN_FASYNC) |
1000 | kill_fasync(&tfile->fasync, SIGIO, POLL_IN); | |
9e641bdc | 1001 | tfile->socket.sk->sk_data_ready(tfile->socket.sk); |
6e914fc7 JW |
1002 | |
1003 | rcu_read_unlock(); | |
6ed10654 | 1004 | return NETDEV_TX_OK; |
1da177e4 LT |
1005 | |
1006 | drop: | |
608b9977 | 1007 | this_cpu_inc(tun->pcpu_stats->tx_dropped); |
149d36f7 | 1008 | skb_tx_error(skb); |
1da177e4 | 1009 | kfree_skb(skb); |
6e914fc7 | 1010 | rcu_read_unlock(); |
baeababb | 1011 | return NET_XMIT_DROP; |
1da177e4 LT |
1012 | } |
1013 | ||
f271b2cc | 1014 | static void tun_net_mclist(struct net_device *dev) |
1da177e4 | 1015 | { |
f271b2cc MK |
1016 | /* |
1017 | * This callback is supposed to deal with mc filter in | |
1018 | * _rx_ path and has nothing to do with the _tx_ path. | |
1019 | * In rx path we always accept everything userspace gives us. | |
1020 | */ | |
1da177e4 LT |
1021 | } |
1022 | ||
c8f44aff MM |
1023 | static netdev_features_t tun_net_fix_features(struct net_device *dev, |
1024 | netdev_features_t features) | |
88255375 MM |
1025 | { |
1026 | struct tun_struct *tun = netdev_priv(dev); | |
1027 | ||
1028 | return (features & tun->set_features) | (features & ~TUN_USER_FEATURES); | |
1029 | } | |
bebd097a NH |
1030 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1031 | static void tun_poll_controller(struct net_device *dev) | |
1032 | { | |
1033 | /* | |
1034 | * Tun only receives frames when: | |
1035 | * 1) the char device endpoint gets data from user space | |
1036 | * 2) the tun socket gets a sendmsg call from user space | |
94317099 PP |
1037 | * If NAPI is not enabled, since both of those are synchronous |
1038 | * operations, we are guaranteed never to have pending data when we poll | |
1039 | * for it so there is nothing to do here but return. | |
bebd097a NH |
1040 | * We need this though so netpoll recognizes us as an interface that |
1041 | * supports polling, which enables bridge devices in virt setups to | |
1042 | * still use netconsole | |
94317099 | 1043 | * If NAPI is enabled, however, we need to schedule polling for all |
90e33d45 PP |
1044 | * queues unless we are using napi_gro_frags(), which we call in |
1045 | * process context and not in NAPI context. | |
bebd097a | 1046 | */ |
94317099 PP |
1047 | struct tun_struct *tun = netdev_priv(dev); |
1048 | ||
1049 | if (tun->flags & IFF_NAPI) { | |
1050 | struct tun_file *tfile; | |
1051 | int i; | |
1052 | ||
90e33d45 PP |
1053 | if (tun_napi_frags_enabled(tun)) |
1054 | return; | |
1055 | ||
94317099 PP |
1056 | rcu_read_lock(); |
1057 | for (i = 0; i < tun->numqueues; i++) { | |
1058 | tfile = rcu_dereference(tun->tfiles[i]); | |
aec72f33 ED |
1059 | if (tfile->napi_enabled) |
1060 | napi_schedule(&tfile->napi); | |
94317099 PP |
1061 | } |
1062 | rcu_read_unlock(); | |
1063 | } | |
bebd097a NH |
1064 | return; |
1065 | } | |
1066 | #endif | |
eaea34b2 PA |
1067 | |
1068 | static void tun_set_headroom(struct net_device *dev, int new_hr) | |
1069 | { | |
1070 | struct tun_struct *tun = netdev_priv(dev); | |
1071 | ||
1072 | if (new_hr < NET_SKB_PAD) | |
1073 | new_hr = NET_SKB_PAD; | |
1074 | ||
1075 | tun->align = new_hr; | |
1076 | } | |
1077 | ||
bc1f4470 | 1078 | static void |
608b9977 PA |
1079 | tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) |
1080 | { | |
1081 | u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0; | |
1082 | struct tun_struct *tun = netdev_priv(dev); | |
1083 | struct tun_pcpu_stats *p; | |
1084 | int i; | |
1085 | ||
1086 | for_each_possible_cpu(i) { | |
1087 | u64 rxpackets, rxbytes, txpackets, txbytes; | |
1088 | unsigned int start; | |
1089 | ||
1090 | p = per_cpu_ptr(tun->pcpu_stats, i); | |
1091 | do { | |
1092 | start = u64_stats_fetch_begin(&p->syncp); | |
1093 | rxpackets = p->rx_packets; | |
1094 | rxbytes = p->rx_bytes; | |
1095 | txpackets = p->tx_packets; | |
1096 | txbytes = p->tx_bytes; | |
1097 | } while (u64_stats_fetch_retry(&p->syncp, start)); | |
1098 | ||
1099 | stats->rx_packets += rxpackets; | |
1100 | stats->rx_bytes += rxbytes; | |
1101 | stats->tx_packets += txpackets; | |
1102 | stats->tx_bytes += txbytes; | |
1103 | ||
1104 | /* u32 counters */ | |
1105 | rx_dropped += p->rx_dropped; | |
1106 | rx_frame_errors += p->rx_frame_errors; | |
1107 | tx_dropped += p->tx_dropped; | |
1108 | } | |
1109 | stats->rx_dropped = rx_dropped; | |
1110 | stats->rx_frame_errors = rx_frame_errors; | |
1111 | stats->tx_dropped = tx_dropped; | |
608b9977 PA |
1112 | } |
1113 | ||
761876c8 JW |
1114 | static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog, |
1115 | struct netlink_ext_ack *extack) | |
1116 | { | |
1117 | struct tun_struct *tun = netdev_priv(dev); | |
1118 | struct bpf_prog *old_prog; | |
1119 | ||
1120 | old_prog = rtnl_dereference(tun->xdp_prog); | |
1121 | rcu_assign_pointer(tun->xdp_prog, prog); | |
1122 | if (old_prog) | |
1123 | bpf_prog_put(old_prog); | |
1124 | ||
1125 | return 0; | |
1126 | } | |
1127 | ||
1128 | static u32 tun_xdp_query(struct net_device *dev) | |
1129 | { | |
1130 | struct tun_struct *tun = netdev_priv(dev); | |
1131 | const struct bpf_prog *xdp_prog; | |
1132 | ||
1133 | xdp_prog = rtnl_dereference(tun->xdp_prog); | |
1134 | if (xdp_prog) | |
1135 | return xdp_prog->aux->id; | |
1136 | ||
1137 | return 0; | |
1138 | } | |
1139 | ||
f4e63525 | 1140 | static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp) |
761876c8 JW |
1141 | { |
1142 | switch (xdp->command) { | |
1143 | case XDP_SETUP_PROG: | |
1144 | return tun_xdp_set(dev, xdp->prog, xdp->extack); | |
1145 | case XDP_QUERY_PROG: | |
1146 | xdp->prog_id = tun_xdp_query(dev); | |
1147 | xdp->prog_attached = !!xdp->prog_id; | |
1148 | return 0; | |
1149 | default: | |
1150 | return -EINVAL; | |
1151 | } | |
1152 | } | |
1153 | ||
758e43b7 | 1154 | static const struct net_device_ops tun_netdev_ops = { |
c70f1829 | 1155 | .ndo_uninit = tun_net_uninit, |
758e43b7 SH |
1156 | .ndo_open = tun_net_open, |
1157 | .ndo_stop = tun_net_close, | |
00829823 | 1158 | .ndo_start_xmit = tun_net_xmit, |
88255375 | 1159 | .ndo_fix_features = tun_net_fix_features, |
c8d68e6b | 1160 | .ndo_select_queue = tun_select_queue, |
bebd097a NH |
1161 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1162 | .ndo_poll_controller = tun_poll_controller, | |
1163 | #endif | |
eaea34b2 | 1164 | .ndo_set_rx_headroom = tun_set_headroom, |
608b9977 | 1165 | .ndo_get_stats64 = tun_net_get_stats64, |
758e43b7 SH |
1166 | }; |
1167 | ||
1168 | static const struct net_device_ops tap_netdev_ops = { | |
c70f1829 | 1169 | .ndo_uninit = tun_net_uninit, |
758e43b7 SH |
1170 | .ndo_open = tun_net_open, |
1171 | .ndo_stop = tun_net_close, | |
00829823 | 1172 | .ndo_start_xmit = tun_net_xmit, |
88255375 | 1173 | .ndo_fix_features = tun_net_fix_features, |
afc4b13d | 1174 | .ndo_set_rx_mode = tun_net_mclist, |
758e43b7 SH |
1175 | .ndo_set_mac_address = eth_mac_addr, |
1176 | .ndo_validate_addr = eth_validate_addr, | |
c8d68e6b | 1177 | .ndo_select_queue = tun_select_queue, |
bebd097a NH |
1178 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1179 | .ndo_poll_controller = tun_poll_controller, | |
1180 | #endif | |
5e52796a | 1181 | .ndo_features_check = passthru_features_check, |
eaea34b2 | 1182 | .ndo_set_rx_headroom = tun_set_headroom, |
608b9977 | 1183 | .ndo_get_stats64 = tun_net_get_stats64, |
f4e63525 | 1184 | .ndo_bpf = tun_xdp, |
758e43b7 SH |
1185 | }; |
1186 | ||
944a1376 | 1187 | static void tun_flow_init(struct tun_struct *tun) |
96442e42 JW |
1188 | { |
1189 | int i; | |
1190 | ||
96442e42 JW |
1191 | for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) |
1192 | INIT_HLIST_HEAD(&tun->flows[i]); | |
1193 | ||
1194 | tun->ageing_time = TUN_FLOW_EXPIRE; | |
e99e88a9 KC |
1195 | timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0); |
1196 | mod_timer(&tun->flow_gc_timer, | |
1197 | round_jiffies_up(jiffies + tun->ageing_time)); | |
96442e42 JW |
1198 | } |
1199 | ||
1200 | static void tun_flow_uninit(struct tun_struct *tun) | |
1201 | { | |
1202 | del_timer_sync(&tun->flow_gc_timer); | |
1203 | tun_flow_flush(tun); | |
96442e42 JW |
1204 | } |
1205 | ||
91572088 JW |
1206 | #define MIN_MTU 68 |
1207 | #define MAX_MTU 65535 | |
1208 | ||
1da177e4 LT |
1209 | /* Initialize net device. */ |
1210 | static void tun_net_init(struct net_device *dev) | |
1211 | { | |
1212 | struct tun_struct *tun = netdev_priv(dev); | |
6aa20a22 | 1213 | |
1da177e4 | 1214 | switch (tun->flags & TUN_TYPE_MASK) { |
40630b82 | 1215 | case IFF_TUN: |
758e43b7 SH |
1216 | dev->netdev_ops = &tun_netdev_ops; |
1217 | ||
1da177e4 LT |
1218 | /* Point-to-Point TUN Device */ |
1219 | dev->hard_header_len = 0; | |
1220 | dev->addr_len = 0; | |
1221 | dev->mtu = 1500; | |
1222 | ||
1223 | /* Zero header length */ | |
6aa20a22 | 1224 | dev->type = ARPHRD_NONE; |
1da177e4 | 1225 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
1da177e4 LT |
1226 | break; |
1227 | ||
40630b82 | 1228 | case IFF_TAP: |
7a0a9608 | 1229 | dev->netdev_ops = &tap_netdev_ops; |
1da177e4 | 1230 | /* Ethernet TAP Device */ |
1da177e4 | 1231 | ether_setup(dev); |
550fd08c | 1232 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
a676847b | 1233 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
36226a8d | 1234 | |
f2cedb63 | 1235 | eth_hw_addr_random(dev); |
36226a8d | 1236 | |
1da177e4 LT |
1237 | break; |
1238 | } | |
91572088 JW |
1239 | |
1240 | dev->min_mtu = MIN_MTU; | |
1241 | dev->max_mtu = MAX_MTU - dev->hard_header_len; | |
1da177e4 LT |
1242 | } |
1243 | ||
1244 | /* Character device part */ | |
1245 | ||
1246 | /* Poll */ | |
c8d68e6b | 1247 | static unsigned int tun_chr_poll(struct file *file, poll_table *wait) |
6aa20a22 | 1248 | { |
b2430de3 | 1249 | struct tun_file *tfile = file->private_data; |
9484dc74 | 1250 | struct tun_struct *tun = tun_get(tfile); |
3c8a9c63 | 1251 | struct sock *sk; |
33dccbb0 | 1252 | unsigned int mask = 0; |
1da177e4 LT |
1253 | |
1254 | if (!tun) | |
eac9e902 | 1255 | return POLLERR; |
1da177e4 | 1256 | |
54f968d6 | 1257 | sk = tfile->socket.sk; |
3c8a9c63 | 1258 | |
6b8a66ee | 1259 | tun_debug(KERN_INFO, tun, "tun_chr_poll\n"); |
1da177e4 | 1260 | |
9e641bdc | 1261 | poll_wait(file, sk_sleep(sk), wait); |
6aa20a22 | 1262 | |
1576d986 | 1263 | if (!skb_array_empty(&tfile->tx_array)) |
1da177e4 LT |
1264 | mask |= POLLIN | POLLRDNORM; |
1265 | ||
b20e2d54 HFS |
1266 | if (tun->dev->flags & IFF_UP && |
1267 | (sock_writeable(sk) || | |
1268 | (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) && | |
1269 | sock_writeable(sk)))) | |
33dccbb0 HX |
1270 | mask |= POLLOUT | POLLWRNORM; |
1271 | ||
c70f1829 EB |
1272 | if (tun->dev->reg_state != NETREG_REGISTERED) |
1273 | mask = POLLERR; | |
1274 | ||
631ab46b | 1275 | tun_put(tun); |
1da177e4 LT |
1276 | return mask; |
1277 | } | |
1278 | ||
90e33d45 PP |
1279 | static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, |
1280 | size_t len, | |
1281 | const struct iov_iter *it) | |
1282 | { | |
1283 | struct sk_buff *skb; | |
1284 | size_t linear; | |
1285 | int err; | |
1286 | int i; | |
1287 | ||
1288 | if (it->nr_segs > MAX_SKB_FRAGS + 1) | |
1289 | return ERR_PTR(-ENOMEM); | |
1290 | ||
1291 | local_bh_disable(); | |
1292 | skb = napi_get_frags(&tfile->napi); | |
1293 | local_bh_enable(); | |
1294 | if (!skb) | |
1295 | return ERR_PTR(-ENOMEM); | |
1296 | ||
1297 | linear = iov_iter_single_seg_count(it); | |
1298 | err = __skb_grow(skb, linear); | |
1299 | if (err) | |
1300 | goto free; | |
1301 | ||
1302 | skb->len = len; | |
1303 | skb->data_len = len - linear; | |
1304 | skb->truesize += skb->data_len; | |
1305 | ||
1306 | for (i = 1; i < it->nr_segs; i++) { | |
1307 | size_t fragsz = it->iov[i].iov_len; | |
1308 | unsigned long offset; | |
1309 | struct page *page; | |
1310 | void *data; | |
1311 | ||
1312 | if (fragsz == 0 || fragsz > PAGE_SIZE) { | |
1313 | err = -EINVAL; | |
1314 | goto free; | |
1315 | } | |
1316 | ||
1317 | local_bh_disable(); | |
1318 | data = napi_alloc_frag(fragsz); | |
1319 | local_bh_enable(); | |
1320 | if (!data) { | |
1321 | err = -ENOMEM; | |
1322 | goto free; | |
1323 | } | |
1324 | ||
1325 | page = virt_to_head_page(data); | |
1326 | offset = data - page_address(page); | |
1327 | skb_fill_page_desc(skb, i - 1, page, offset, fragsz); | |
1328 | } | |
1329 | ||
1330 | return skb; | |
1331 | free: | |
1332 | /* frees skb and all frags allocated with napi_alloc_frag() */ | |
1333 | napi_free_frags(&tfile->napi); | |
1334 | return ERR_PTR(err); | |
1335 | } | |
1336 | ||
f42157cb RR |
1337 | /* prepad is the amount to reserve at front. len is length after that. |
1338 | * linear is a hint as to how much to copy (usually headers). */ | |
54f968d6 | 1339 | static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, |
6f7c156c | 1340 | size_t prepad, size_t len, |
1341 | size_t linear, int noblock) | |
f42157cb | 1342 | { |
54f968d6 | 1343 | struct sock *sk = tfile->socket.sk; |
f42157cb | 1344 | struct sk_buff *skb; |
33dccbb0 | 1345 | int err; |
f42157cb RR |
1346 | |
1347 | /* Under a page? Don't bother with paged skb. */ | |
0eca93bc | 1348 | if (prepad + len < PAGE_SIZE || !linear) |
33dccbb0 | 1349 | linear = len; |
f42157cb | 1350 | |
33dccbb0 | 1351 | skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, |
28d64271 | 1352 | &err, 0); |
f42157cb | 1353 | if (!skb) |
33dccbb0 | 1354 | return ERR_PTR(err); |
f42157cb RR |
1355 | |
1356 | skb_reserve(skb, prepad); | |
1357 | skb_put(skb, linear); | |
33dccbb0 HX |
1358 | skb->data_len = len - linear; |
1359 | skb->len += len - linear; | |
f42157cb RR |
1360 | |
1361 | return skb; | |
1362 | } | |
1363 | ||
5503fcec JW |
1364 | static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, |
1365 | struct sk_buff *skb, int more) | |
1366 | { | |
1367 | struct sk_buff_head *queue = &tfile->sk.sk_write_queue; | |
1368 | struct sk_buff_head process_queue; | |
1369 | u32 rx_batched = tun->rx_batched; | |
1370 | bool rcv = false; | |
1371 | ||
1372 | if (!rx_batched || (!more && skb_queue_empty(queue))) { | |
1373 | local_bh_disable(); | |
1374 | netif_receive_skb(skb); | |
1375 | local_bh_enable(); | |
1376 | return; | |
1377 | } | |
1378 | ||
1379 | spin_lock(&queue->lock); | |
1380 | if (!more || skb_queue_len(queue) == rx_batched) { | |
1381 | __skb_queue_head_init(&process_queue); | |
1382 | skb_queue_splice_tail_init(queue, &process_queue); | |
1383 | rcv = true; | |
1384 | } else { | |
1385 | __skb_queue_tail(queue, skb); | |
1386 | } | |
1387 | spin_unlock(&queue->lock); | |
1388 | ||
1389 | if (rcv) { | |
1390 | struct sk_buff *nskb; | |
1391 | ||
1392 | local_bh_disable(); | |
1393 | while ((nskb = __skb_dequeue(&process_queue))) | |
1394 | netif_receive_skb(nskb); | |
1395 | netif_receive_skb(skb); | |
1396 | local_bh_enable(); | |
1397 | } | |
1398 | } | |
1399 | ||
66ccbc9c JW |
1400 | static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, |
1401 | int len, int noblock, bool zerocopy) | |
1402 | { | |
1403 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) | |
1404 | return false; | |
1405 | ||
1406 | if (tfile->socket.sk->sk_sndbuf != INT_MAX) | |
1407 | return false; | |
1408 | ||
1409 | if (!noblock) | |
1410 | return false; | |
1411 | ||
1412 | if (zerocopy) | |
1413 | return false; | |
1414 | ||
1415 | if (SKB_DATA_ALIGN(len + TUN_RX_PAD) + | |
1416 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE) | |
1417 | return false; | |
1418 | ||
1419 | return true; | |
1420 | } | |
1421 | ||
761876c8 JW |
1422 | static struct sk_buff *tun_build_skb(struct tun_struct *tun, |
1423 | struct tun_file *tfile, | |
66ccbc9c | 1424 | struct iov_iter *from, |
761876c8 | 1425 | struct virtio_net_hdr *hdr, |
1cfe6e93 | 1426 | int len, int *skb_xdp) |
66ccbc9c | 1427 | { |
0bbd7dad | 1428 | struct page_frag *alloc_frag = ¤t->task_frag; |
66ccbc9c | 1429 | struct sk_buff *skb; |
761876c8 | 1430 | struct bpf_prog *xdp_prog; |
7df13219 | 1431 | int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
761876c8 | 1432 | unsigned int delta = 0; |
66ccbc9c JW |
1433 | char *buf; |
1434 | size_t copied; | |
761876c8 | 1435 | bool xdp_xmit = false; |
7df13219 JW |
1436 | int err, pad = TUN_RX_PAD; |
1437 | ||
1438 | rcu_read_lock(); | |
1439 | xdp_prog = rcu_dereference(tun->xdp_prog); | |
1440 | if (xdp_prog) | |
1441 | pad += TUN_HEADROOM; | |
1442 | buflen += SKB_DATA_ALIGN(len + pad); | |
1443 | rcu_read_unlock(); | |
66ccbc9c | 1444 | |
63b9ab65 | 1445 | alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES); |
66ccbc9c JW |
1446 | if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL))) |
1447 | return ERR_PTR(-ENOMEM); | |
1448 | ||
1449 | buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset; | |
1450 | copied = copy_page_from_iter(alloc_frag->page, | |
7df13219 | 1451 | alloc_frag->offset + pad, |
66ccbc9c JW |
1452 | len, from); |
1453 | if (copied != len) | |
1454 | return ERR_PTR(-EFAULT); | |
1455 | ||
7df13219 JW |
1456 | /* There's a small window that XDP may be set after the check |
1457 | * of xdp_prog above, this should be rare and for simplicity | |
1458 | * we do XDP on skb in case the headroom is not enough. | |
1459 | */ | |
1460 | if (hdr->gso_type || !xdp_prog) | |
1cfe6e93 | 1461 | *skb_xdp = 1; |
761876c8 | 1462 | else |
1cfe6e93 | 1463 | *skb_xdp = 0; |
761876c8 JW |
1464 | |
1465 | rcu_read_lock(); | |
1466 | xdp_prog = rcu_dereference(tun->xdp_prog); | |
1cfe6e93 | 1467 | if (xdp_prog && !*skb_xdp) { |
761876c8 JW |
1468 | struct xdp_buff xdp; |
1469 | void *orig_data; | |
1470 | u32 act; | |
1471 | ||
1472 | xdp.data_hard_start = buf; | |
7df13219 | 1473 | xdp.data = buf + pad; |
de8f3a83 | 1474 | xdp_set_data_meta_invalid(&xdp); |
761876c8 JW |
1475 | xdp.data_end = xdp.data + len; |
1476 | orig_data = xdp.data; | |
1477 | act = bpf_prog_run_xdp(xdp_prog, &xdp); | |
1478 | ||
1479 | switch (act) { | |
1480 | case XDP_REDIRECT: | |
1481 | get_page(alloc_frag->page); | |
1482 | alloc_frag->offset += buflen; | |
1483 | err = xdp_do_redirect(tun->dev, &xdp, xdp_prog); | |
1484 | if (err) | |
1485 | goto err_redirect; | |
654d5738 | 1486 | rcu_read_unlock(); |
761876c8 JW |
1487 | return NULL; |
1488 | case XDP_TX: | |
1489 | xdp_xmit = true; | |
1490 | /* fall through */ | |
1491 | case XDP_PASS: | |
1492 | delta = orig_data - xdp.data; | |
1493 | break; | |
1494 | default: | |
1495 | bpf_warn_invalid_xdp_action(act); | |
1496 | /* fall through */ | |
1497 | case XDP_ABORTED: | |
1498 | trace_xdp_exception(tun->dev, xdp_prog, act); | |
1499 | /* fall through */ | |
1500 | case XDP_DROP: | |
1501 | goto err_xdp; | |
1502 | } | |
1503 | } | |
1504 | ||
66ccbc9c | 1505 | skb = build_skb(buf, buflen); |
761876c8 JW |
1506 | if (!skb) { |
1507 | rcu_read_unlock(); | |
66ccbc9c | 1508 | return ERR_PTR(-ENOMEM); |
761876c8 | 1509 | } |
66ccbc9c | 1510 | |
7df13219 | 1511 | skb_reserve(skb, pad - delta); |
761876c8 | 1512 | skb_put(skb, len + delta); |
66ccbc9c JW |
1513 | get_page(alloc_frag->page); |
1514 | alloc_frag->offset += buflen; | |
1515 | ||
761876c8 JW |
1516 | if (xdp_xmit) { |
1517 | skb->dev = tun->dev; | |
1518 | generic_xdp_tx(skb, xdp_prog); | |
654d5738 | 1519 | rcu_read_unlock(); |
761876c8 JW |
1520 | return NULL; |
1521 | } | |
1522 | ||
1523 | rcu_read_unlock(); | |
1524 | ||
66ccbc9c | 1525 | return skb; |
761876c8 JW |
1526 | |
1527 | err_redirect: | |
1528 | put_page(alloc_frag->page); | |
1529 | err_xdp: | |
1530 | rcu_read_unlock(); | |
1531 | this_cpu_inc(tun->pcpu_stats->rx_dropped); | |
1532 | return NULL; | |
66ccbc9c JW |
1533 | } |
1534 | ||
1da177e4 | 1535 | /* Get packet from user space buffer */ |
54f968d6 | 1536 | static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, |
f5ff53b4 | 1537 | void *msg_control, struct iov_iter *from, |
5503fcec | 1538 | int noblock, bool more) |
1da177e4 | 1539 | { |
09640e63 | 1540 | struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; |
1da177e4 | 1541 | struct sk_buff *skb; |
f5ff53b4 | 1542 | size_t total_len = iov_iter_count(from); |
eaea34b2 | 1543 | size_t len = total_len, align = tun->align, linear; |
f43798c2 | 1544 | struct virtio_net_hdr gso = { 0 }; |
608b9977 | 1545 | struct tun_pcpu_stats *stats; |
96f8d9ec | 1546 | int good_linear; |
0690899b MT |
1547 | int copylen; |
1548 | bool zerocopy = false; | |
1549 | int err; | |
49974420 | 1550 | u32 rxhash; |
1cfe6e93 | 1551 | int skb_xdp = 1; |
90e33d45 | 1552 | bool frags = tun_napi_frags_enabled(tun); |
1da177e4 | 1553 | |
1bd4978a ED |
1554 | if (!(tun->dev->flags & IFF_UP)) |
1555 | return -EIO; | |
1556 | ||
40630b82 | 1557 | if (!(tun->flags & IFF_NO_PI)) { |
15718ea0 | 1558 | if (len < sizeof(pi)) |
1da177e4 | 1559 | return -EINVAL; |
15718ea0 | 1560 | len -= sizeof(pi); |
1da177e4 | 1561 | |
cbbd26b8 | 1562 | if (!copy_from_iter_full(&pi, sizeof(pi), from)) |
1da177e4 LT |
1563 | return -EFAULT; |
1564 | } | |
1565 | ||
40630b82 | 1566 | if (tun->flags & IFF_VNET_HDR) { |
e1edab87 WB |
1567 | int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); |
1568 | ||
1569 | if (len < vnet_hdr_sz) | |
f43798c2 | 1570 | return -EINVAL; |
e1edab87 | 1571 | len -= vnet_hdr_sz; |
f43798c2 | 1572 | |
cbbd26b8 | 1573 | if (!copy_from_iter_full(&gso, sizeof(gso), from)) |
f43798c2 RR |
1574 | return -EFAULT; |
1575 | ||
4909122f | 1576 | if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && |
56f0dcc5 MT |
1577 | tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) |
1578 | gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); | |
4909122f | 1579 | |
56f0dcc5 | 1580 | if (tun16_to_cpu(tun, gso.hdr_len) > len) |
f43798c2 | 1581 | return -EINVAL; |
e1edab87 | 1582 | iov_iter_advance(from, vnet_hdr_sz - sizeof(gso)); |
f43798c2 RR |
1583 | } |
1584 | ||
40630b82 | 1585 | if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) { |
a504b86e | 1586 | align += NET_IP_ALIGN; |
0eca93bc | 1587 | if (unlikely(len < ETH_HLEN || |
56f0dcc5 | 1588 | (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN))) |
e01bf1c8 RR |
1589 | return -EINVAL; |
1590 | } | |
6aa20a22 | 1591 | |
96f8d9ec JW |
1592 | good_linear = SKB_MAX_HEAD(align); |
1593 | ||
88529176 | 1594 | if (msg_control) { |
f5ff53b4 AV |
1595 | struct iov_iter i = *from; |
1596 | ||
88529176 JW |
1597 | /* There are 256 bytes to be copied in skb, so there is |
1598 | * enough room for skb expand head in case it is used. | |
0690899b MT |
1599 | * The rest of the buffer is mapped from userspace. |
1600 | */ | |
56f0dcc5 | 1601 | copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN; |
96f8d9ec JW |
1602 | if (copylen > good_linear) |
1603 | copylen = good_linear; | |
3dd5c330 | 1604 | linear = copylen; |
f5ff53b4 AV |
1605 | iov_iter_advance(&i, copylen); |
1606 | if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS) | |
88529176 JW |
1607 | zerocopy = true; |
1608 | } | |
1609 | ||
90e33d45 | 1610 | if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { |
1cfe6e93 JW |
1611 | /* For the packet that is not easy to be processed |
1612 | * (e.g gso or jumbo packet), we will do it at after | |
1613 | * skb was created with generic XDP routine. | |
1614 | */ | |
1615 | skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); | |
66ccbc9c | 1616 | if (IS_ERR(skb)) { |
608b9977 | 1617 | this_cpu_inc(tun->pcpu_stats->rx_dropped); |
66ccbc9c JW |
1618 | return PTR_ERR(skb); |
1619 | } | |
761876c8 JW |
1620 | if (!skb) |
1621 | return total_len; | |
66ccbc9c JW |
1622 | } else { |
1623 | if (!zerocopy) { | |
1624 | copylen = len; | |
1625 | if (tun16_to_cpu(tun, gso.hdr_len) > good_linear) | |
1626 | linear = good_linear; | |
1627 | else | |
1628 | linear = tun16_to_cpu(tun, gso.hdr_len); | |
1629 | } | |
1da177e4 | 1630 | |
90e33d45 PP |
1631 | if (frags) { |
1632 | mutex_lock(&tfile->napi_mutex); | |
1633 | skb = tun_napi_alloc_frags(tfile, copylen, from); | |
1634 | /* tun_napi_alloc_frags() enforces a layout for the skb. | |
1635 | * If zerocopy is enabled, then this layout will be | |
1636 | * overwritten by zerocopy_sg_from_iter(). | |
1637 | */ | |
1638 | zerocopy = false; | |
1639 | } else { | |
1640 | skb = tun_alloc_skb(tfile, align, copylen, linear, | |
1641 | noblock); | |
1642 | } | |
1643 | ||
66ccbc9c JW |
1644 | if (IS_ERR(skb)) { |
1645 | if (PTR_ERR(skb) != -EAGAIN) | |
1646 | this_cpu_inc(tun->pcpu_stats->rx_dropped); | |
90e33d45 PP |
1647 | if (frags) |
1648 | mutex_unlock(&tfile->napi_mutex); | |
66ccbc9c JW |
1649 | return PTR_ERR(skb); |
1650 | } | |
0690899b | 1651 | |
66ccbc9c JW |
1652 | if (zerocopy) |
1653 | err = zerocopy_sg_from_iter(skb, from); | |
1654 | else | |
1655 | err = skb_copy_datagram_from_iter(skb, 0, from, len); | |
1656 | ||
1657 | if (err) { | |
1658 | this_cpu_inc(tun->pcpu_stats->rx_dropped); | |
1659 | kfree_skb(skb); | |
90e33d45 PP |
1660 | if (frags) { |
1661 | tfile->napi.skb = NULL; | |
1662 | mutex_unlock(&tfile->napi_mutex); | |
1663 | } | |
1664 | ||
66ccbc9c JW |
1665 | return -EFAULT; |
1666 | } | |
8f22757e | 1667 | } |
1da177e4 | 1668 | |
3e9e40e7 | 1669 | if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { |
df10db98 PA |
1670 | this_cpu_inc(tun->pcpu_stats->rx_frame_errors); |
1671 | kfree_skb(skb); | |
90e33d45 PP |
1672 | if (frags) { |
1673 | tfile->napi.skb = NULL; | |
1674 | mutex_unlock(&tfile->napi_mutex); | |
1675 | } | |
1676 | ||
df10db98 PA |
1677 | return -EINVAL; |
1678 | } | |
1679 | ||
1da177e4 | 1680 | switch (tun->flags & TUN_TYPE_MASK) { |
40630b82 MT |
1681 | case IFF_TUN: |
1682 | if (tun->flags & IFF_NO_PI) { | |
2580c4c1 AP |
1683 | u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0; |
1684 | ||
1685 | switch (ip_version) { | |
1686 | case 4: | |
f09f7ee2 AWC |
1687 | pi.proto = htons(ETH_P_IP); |
1688 | break; | |
2580c4c1 | 1689 | case 6: |
f09f7ee2 AWC |
1690 | pi.proto = htons(ETH_P_IPV6); |
1691 | break; | |
1692 | default: | |
608b9977 | 1693 | this_cpu_inc(tun->pcpu_stats->rx_dropped); |
f09f7ee2 AWC |
1694 | kfree_skb(skb); |
1695 | return -EINVAL; | |
1696 | } | |
1697 | } | |
1698 | ||
459a98ed | 1699 | skb_reset_mac_header(skb); |
1da177e4 | 1700 | skb->protocol = pi.proto; |
4c13eb66 | 1701 | skb->dev = tun->dev; |
1da177e4 | 1702 | break; |
40630b82 | 1703 | case IFF_TAP: |
90e33d45 PP |
1704 | if (!frags) |
1705 | skb->protocol = eth_type_trans(skb, tun->dev); | |
1da177e4 | 1706 | break; |
6403eab1 | 1707 | } |
1da177e4 | 1708 | |
0690899b MT |
1709 | /* copy skb_ubuf_info for callback when skb has no error */ |
1710 | if (zerocopy) { | |
1711 | skb_shinfo(skb)->destructor_arg = msg_control; | |
1712 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; | |
c9af6db4 | 1713 | skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG; |
af1cc7a2 JW |
1714 | } else if (msg_control) { |
1715 | struct ubuf_info *uarg = msg_control; | |
1716 | uarg->callback(uarg, false); | |
0690899b MT |
1717 | } |
1718 | ||
72f65107 | 1719 | skb_reset_network_header(skb); |
40893fd0 | 1720 | skb_probe_transport_header(skb, 0); |
38502af7 | 1721 | |
1cfe6e93 | 1722 | if (skb_xdp) { |
761876c8 JW |
1723 | struct bpf_prog *xdp_prog; |
1724 | int ret; | |
1725 | ||
1726 | rcu_read_lock(); | |
1727 | xdp_prog = rcu_dereference(tun->xdp_prog); | |
1728 | if (xdp_prog) { | |
1729 | ret = do_xdp_generic(xdp_prog, skb); | |
1730 | if (ret != XDP_PASS) { | |
1731 | rcu_read_unlock(); | |
1732 | return total_len; | |
1733 | } | |
1734 | } | |
1735 | rcu_read_unlock(); | |
1736 | } | |
1737 | ||
feec084a | 1738 | rxhash = __skb_get_hash_symmetric(skb); |
94317099 | 1739 | |
90e33d45 PP |
1740 | if (frags) { |
1741 | /* Exercise flow dissector code path. */ | |
1742 | u32 headlen = eth_get_headlen(skb->data, skb_headlen(skb)); | |
1743 | ||
010f245b | 1744 | if (unlikely(headlen > skb_headlen(skb))) { |
90e33d45 PP |
1745 | this_cpu_inc(tun->pcpu_stats->rx_dropped); |
1746 | napi_free_frags(&tfile->napi); | |
1747 | mutex_unlock(&tfile->napi_mutex); | |
1748 | WARN_ON(1); | |
1749 | return -ENOMEM; | |
1750 | } | |
1751 | ||
1752 | local_bh_disable(); | |
1753 | napi_gro_frags(&tfile->napi); | |
1754 | local_bh_enable(); | |
1755 | mutex_unlock(&tfile->napi_mutex); | |
aec72f33 | 1756 | } else if (tfile->napi_enabled) { |
94317099 PP |
1757 | struct sk_buff_head *queue = &tfile->sk.sk_write_queue; |
1758 | int queue_len; | |
1759 | ||
1760 | spin_lock_bh(&queue->lock); | |
1761 | __skb_queue_tail(queue, skb); | |
1762 | queue_len = skb_queue_len(queue); | |
1763 | spin_unlock(&queue->lock); | |
1764 | ||
1765 | if (!more || queue_len > NAPI_POLL_WEIGHT) | |
1766 | napi_schedule(&tfile->napi); | |
1767 | ||
1768 | local_bh_enable(); | |
1769 | } else if (!IS_ENABLED(CONFIG_4KSTACKS)) { | |
1770 | tun_rx_batched(tun, tfile, skb, more); | |
1771 | } else { | |
1772 | netif_rx_ni(skb); | |
1773 | } | |
6aa20a22 | 1774 | |
608b9977 PA |
1775 | stats = get_cpu_ptr(tun->pcpu_stats); |
1776 | u64_stats_update_begin(&stats->syncp); | |
1777 | stats->rx_packets++; | |
1778 | stats->rx_bytes += len; | |
1779 | u64_stats_update_end(&stats->syncp); | |
1780 | put_cpu_ptr(stats); | |
1da177e4 | 1781 | |
9e85722d | 1782 | tun_flow_update(tun, rxhash, tfile); |
0690899b | 1783 | return total_len; |
6aa20a22 | 1784 | } |
1da177e4 | 1785 | |
f5ff53b4 | 1786 | static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) |
1da177e4 | 1787 | { |
33dccbb0 | 1788 | struct file *file = iocb->ki_filp; |
54f968d6 | 1789 | struct tun_file *tfile = file->private_data; |
9484dc74 | 1790 | struct tun_struct *tun = tun_get(tfile); |
631ab46b | 1791 | ssize_t result; |
1da177e4 LT |
1792 | |
1793 | if (!tun) | |
1794 | return -EBADFD; | |
1795 | ||
5503fcec JW |
1796 | result = tun_get_user(tun, tfile, NULL, from, |
1797 | file->f_flags & O_NONBLOCK, false); | |
631ab46b EB |
1798 | |
1799 | tun_put(tun); | |
1800 | return result; | |
1da177e4 LT |
1801 | } |
1802 | ||
1da177e4 | 1803 | /* Put packet to the user space buffer */ |
6f7c156c | 1804 | static ssize_t tun_put_user(struct tun_struct *tun, |
54f968d6 | 1805 | struct tun_file *tfile, |
6f7c156c | 1806 | struct sk_buff *skb, |
e0b46d0e | 1807 | struct iov_iter *iter) |
1da177e4 LT |
1808 | { |
1809 | struct tun_pi pi = { 0, skb->protocol }; | |
608b9977 | 1810 | struct tun_pcpu_stats *stats; |
e0b46d0e | 1811 | ssize_t total; |
8c847d25 | 1812 | int vlan_offset = 0; |
a8f9bfdf | 1813 | int vlan_hlen = 0; |
2eb783c4 | 1814 | int vnet_hdr_sz = 0; |
a8f9bfdf | 1815 | |
df8a39de | 1816 | if (skb_vlan_tag_present(skb)) |
a8f9bfdf | 1817 | vlan_hlen = VLAN_HLEN; |
1da177e4 | 1818 | |
40630b82 | 1819 | if (tun->flags & IFF_VNET_HDR) |
e1edab87 | 1820 | vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz); |
1da177e4 | 1821 | |
e0b46d0e HX |
1822 | total = skb->len + vlan_hlen + vnet_hdr_sz; |
1823 | ||
40630b82 | 1824 | if (!(tun->flags & IFF_NO_PI)) { |
e0b46d0e | 1825 | if (iov_iter_count(iter) < sizeof(pi)) |
1da177e4 LT |
1826 | return -EINVAL; |
1827 | ||
e0b46d0e HX |
1828 | total += sizeof(pi); |
1829 | if (iov_iter_count(iter) < total) { | |
1da177e4 LT |
1830 | /* Packet will be striped */ |
1831 | pi.flags |= TUN_PKT_STRIP; | |
1832 | } | |
6aa20a22 | 1833 | |
e0b46d0e | 1834 | if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi)) |
1da177e4 | 1835 | return -EFAULT; |
6aa20a22 | 1836 | } |
1da177e4 | 1837 | |
2eb783c4 | 1838 | if (vnet_hdr_sz) { |
9403cd7c | 1839 | struct virtio_net_hdr gso; |
34166093 | 1840 | |
e0b46d0e | 1841 | if (iov_iter_count(iter) < vnet_hdr_sz) |
f43798c2 RR |
1842 | return -EINVAL; |
1843 | ||
3e9e40e7 | 1844 | if (virtio_net_hdr_from_skb(skb, &gso, |
6391a448 | 1845 | tun_is_little_endian(tun), true)) { |
f43798c2 | 1846 | struct skb_shared_info *sinfo = skb_shinfo(skb); |
34166093 MR |
1847 | pr_err("unexpected GSO type: " |
1848 | "0x%x, gso_size %d, hdr_len %d\n", | |
1849 | sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size), | |
1850 | tun16_to_cpu(tun, gso.hdr_len)); | |
1851 | print_hex_dump(KERN_ERR, "tun: ", | |
1852 | DUMP_PREFIX_NONE, | |
1853 | 16, 1, skb->head, | |
1854 | min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true); | |
1855 | WARN_ON_ONCE(1); | |
1856 | return -EINVAL; | |
1857 | } | |
f43798c2 | 1858 | |
e0b46d0e | 1859 | if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso)) |
f43798c2 | 1860 | return -EFAULT; |
8c847d25 JW |
1861 | |
1862 | iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso)); | |
f43798c2 RR |
1863 | } |
1864 | ||
a8f9bfdf | 1865 | if (vlan_hlen) { |
e0b46d0e | 1866 | int ret; |
6680ec68 JW |
1867 | struct { |
1868 | __be16 h_vlan_proto; | |
1869 | __be16 h_vlan_TCI; | |
1870 | } veth; | |
1871 | ||
1872 | veth.h_vlan_proto = skb->vlan_proto; | |
df8a39de | 1873 | veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb)); |
6680ec68 JW |
1874 | |
1875 | vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); | |
6680ec68 | 1876 | |
e0b46d0e HX |
1877 | ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset); |
1878 | if (ret || !iov_iter_count(iter)) | |
6680ec68 JW |
1879 | goto done; |
1880 | ||
e0b46d0e HX |
1881 | ret = copy_to_iter(&veth, sizeof(veth), iter); |
1882 | if (ret != sizeof(veth) || !iov_iter_count(iter)) | |
6680ec68 JW |
1883 | goto done; |
1884 | } | |
1da177e4 | 1885 | |
e0b46d0e | 1886 | skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset); |
1da177e4 | 1887 | |
6680ec68 | 1888 | done: |
608b9977 PA |
1889 | /* caller is in process context, */ |
1890 | stats = get_cpu_ptr(tun->pcpu_stats); | |
1891 | u64_stats_update_begin(&stats->syncp); | |
1892 | stats->tx_packets++; | |
1893 | stats->tx_bytes += skb->len + vlan_hlen; | |
1894 | u64_stats_update_end(&stats->syncp); | |
1895 | put_cpu_ptr(tun->pcpu_stats); | |
1da177e4 LT |
1896 | |
1897 | return total; | |
1898 | } | |
1899 | ||
1576d986 JW |
1900 | static struct sk_buff *tun_ring_recv(struct tun_file *tfile, int noblock, |
1901 | int *err) | |
1902 | { | |
1903 | DECLARE_WAITQUEUE(wait, current); | |
1904 | struct sk_buff *skb = NULL; | |
f48cc6b2 | 1905 | int error = 0; |
1576d986 JW |
1906 | |
1907 | skb = skb_array_consume(&tfile->tx_array); | |
1908 | if (skb) | |
1909 | goto out; | |
1910 | if (noblock) { | |
f48cc6b2 | 1911 | error = -EAGAIN; |
1576d986 JW |
1912 | goto out; |
1913 | } | |
1914 | ||
1915 | add_wait_queue(&tfile->wq.wait, &wait); | |
1916 | current->state = TASK_INTERRUPTIBLE; | |
1917 | ||
1918 | while (1) { | |
1919 | skb = skb_array_consume(&tfile->tx_array); | |
1920 | if (skb) | |
1921 | break; | |
1922 | if (signal_pending(current)) { | |
f48cc6b2 | 1923 | error = -ERESTARTSYS; |
1576d986 JW |
1924 | break; |
1925 | } | |
1926 | if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { | |
f48cc6b2 | 1927 | error = -EFAULT; |
1576d986 JW |
1928 | break; |
1929 | } | |
1930 | ||
1931 | schedule(); | |
1932 | } | |
1933 | ||
1934 | current->state = TASK_RUNNING; | |
1935 | remove_wait_queue(&tfile->wq.wait, &wait); | |
1936 | ||
1937 | out: | |
f48cc6b2 | 1938 | *err = error; |
1576d986 JW |
1939 | return skb; |
1940 | } | |
1941 | ||
54f968d6 | 1942 | static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, |
9b067034 | 1943 | struct iov_iter *to, |
ac77cfd4 | 1944 | int noblock, struct sk_buff *skb) |
1da177e4 | 1945 | { |
9b067034 | 1946 | ssize_t ret; |
1576d986 | 1947 | int err; |
1da177e4 | 1948 | |
3872baf6 | 1949 | tun_debug(KERN_INFO, tun, "tun_do_read\n"); |
1da177e4 | 1950 | |
c33ee15b WX |
1951 | if (!iov_iter_count(to)) { |
1952 | if (skb) | |
1953 | kfree_skb(skb); | |
9b067034 | 1954 | return 0; |
c33ee15b | 1955 | } |
1da177e4 | 1956 | |
ac77cfd4 JW |
1957 | if (!skb) { |
1958 | /* Read frames from ring */ | |
1959 | skb = tun_ring_recv(tfile, noblock, &err); | |
1960 | if (!skb) | |
1961 | return err; | |
1962 | } | |
e0b46d0e | 1963 | |
9b067034 | 1964 | ret = tun_put_user(tun, tfile, skb, to); |
f51a5e82 | 1965 | if (unlikely(ret < 0)) |
f271b2cc | 1966 | kfree_skb(skb); |
f51a5e82 JW |
1967 | else |
1968 | consume_skb(skb); | |
1da177e4 | 1969 | |
05c2828c MT |
1970 | return ret; |
1971 | } | |
1972 | ||
9b067034 | 1973 | static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to) |
05c2828c MT |
1974 | { |
1975 | struct file *file = iocb->ki_filp; | |
1976 | struct tun_file *tfile = file->private_data; | |
9484dc74 | 1977 | struct tun_struct *tun = tun_get(tfile); |
9b067034 | 1978 | ssize_t len = iov_iter_count(to), ret; |
05c2828c MT |
1979 | |
1980 | if (!tun) | |
1981 | return -EBADFD; | |
ac77cfd4 | 1982 | ret = tun_do_read(tun, tfile, to, file->f_flags & O_NONBLOCK, NULL); |
42404c09 | 1983 | ret = min_t(ssize_t, ret, len); |
d0b7da8a ZYW |
1984 | if (ret > 0) |
1985 | iocb->ki_pos = ret; | |
631ab46b | 1986 | tun_put(tun); |
1da177e4 LT |
1987 | return ret; |
1988 | } | |
1989 | ||
96442e42 JW |
1990 | static void tun_free_netdev(struct net_device *dev) |
1991 | { | |
1992 | struct tun_struct *tun = netdev_priv(dev); | |
1993 | ||
4008e97f | 1994 | BUG_ON(!(list_empty(&tun->disabled))); |
608b9977 | 1995 | free_percpu(tun->pcpu_stats); |
96442e42 | 1996 | tun_flow_uninit(tun); |
5dbbaf2d | 1997 | security_tun_dev_free_security(tun->security); |
96442e42 JW |
1998 | } |
1999 | ||
1da177e4 LT |
2000 | static void tun_setup(struct net_device *dev) |
2001 | { | |
2002 | struct tun_struct *tun = netdev_priv(dev); | |
2003 | ||
0625c883 EB |
2004 | tun->owner = INVALID_UID; |
2005 | tun->group = INVALID_GID; | |
1da177e4 | 2006 | |
1da177e4 | 2007 | dev->ethtool_ops = &tun_ethtool_ops; |
cf124db5 DM |
2008 | dev->needs_free_netdev = true; |
2009 | dev->priv_destructor = tun_free_netdev; | |
016adb72 JW |
2010 | /* We prefer our own queue length */ |
2011 | dev->tx_queue_len = TUN_READQ_SIZE; | |
1da177e4 LT |
2012 | } |
2013 | ||
f019a7a5 EB |
2014 | /* Trivial set of netlink ops to allow deleting tun or tap |
2015 | * device with netlink. | |
2016 | */ | |
a8b8a889 MS |
2017 | static int tun_validate(struct nlattr *tb[], struct nlattr *data[], |
2018 | struct netlink_ext_ack *extack) | |
f019a7a5 EB |
2019 | { |
2020 | return -EINVAL; | |
2021 | } | |
2022 | ||
2023 | static struct rtnl_link_ops tun_link_ops __read_mostly = { | |
2024 | .kind = DRV_NAME, | |
2025 | .priv_size = sizeof(struct tun_struct), | |
2026 | .setup = tun_setup, | |
2027 | .validate = tun_validate, | |
2028 | }; | |
2029 | ||
33dccbb0 HX |
2030 | static void tun_sock_write_space(struct sock *sk) |
2031 | { | |
54f968d6 | 2032 | struct tun_file *tfile; |
43815482 | 2033 | wait_queue_head_t *wqueue; |
33dccbb0 HX |
2034 | |
2035 | if (!sock_writeable(sk)) | |
2036 | return; | |
2037 | ||
9cd3e072 | 2038 | if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags)) |
33dccbb0 HX |
2039 | return; |
2040 | ||
43815482 ED |
2041 | wqueue = sk_sleep(sk); |
2042 | if (wqueue && waitqueue_active(wqueue)) | |
2043 | wake_up_interruptible_sync_poll(wqueue, POLLOUT | | |
05c2828c | 2044 | POLLWRNORM | POLLWRBAND); |
c722c625 | 2045 | |
54f968d6 JW |
2046 | tfile = container_of(sk, struct tun_file, sk); |
2047 | kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); | |
33dccbb0 HX |
2048 | } |
2049 | ||
1b784140 | 2050 | static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) |
05c2828c | 2051 | { |
54f968d6 JW |
2052 | int ret; |
2053 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); | |
9484dc74 | 2054 | struct tun_struct *tun = tun_get(tfile); |
54f968d6 JW |
2055 | |
2056 | if (!tun) | |
2057 | return -EBADFD; | |
f5ff53b4 | 2058 | |
c0371da6 | 2059 | ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter, |
5503fcec JW |
2060 | m->msg_flags & MSG_DONTWAIT, |
2061 | m->msg_flags & MSG_MORE); | |
54f968d6 JW |
2062 | tun_put(tun); |
2063 | return ret; | |
05c2828c MT |
2064 | } |
2065 | ||
1b784140 | 2066 | static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len, |
05c2828c MT |
2067 | int flags) |
2068 | { | |
54f968d6 | 2069 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); |
9484dc74 | 2070 | struct tun_struct *tun = tun_get(tfile); |
c33ee15b | 2071 | struct sk_buff *skb = m->msg_control; |
05c2828c | 2072 | int ret; |
54f968d6 | 2073 | |
c33ee15b WX |
2074 | if (!tun) { |
2075 | ret = -EBADFD; | |
2076 | goto out_free_skb; | |
2077 | } | |
54f968d6 | 2078 | |
eda29772 | 2079 | if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) { |
3811ae76 | 2080 | ret = -EINVAL; |
c33ee15b | 2081 | goto out_put_tun; |
3811ae76 | 2082 | } |
eda29772 RC |
2083 | if (flags & MSG_ERRQUEUE) { |
2084 | ret = sock_recv_errqueue(sock->sk, m, total_len, | |
2085 | SOL_PACKET, TUN_TX_TIMESTAMP); | |
2086 | goto out; | |
2087 | } | |
c33ee15b | 2088 | ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, skb); |
87897931 | 2089 | if (ret > (ssize_t)total_len) { |
42404c09 DM |
2090 | m->msg_flags |= MSG_TRUNC; |
2091 | ret = flags & MSG_TRUNC ? ret : total_len; | |
2092 | } | |
3811ae76 | 2093 | out: |
54f968d6 | 2094 | tun_put(tun); |
05c2828c | 2095 | return ret; |
c33ee15b WX |
2096 | |
2097 | out_put_tun: | |
2098 | tun_put(tun); | |
2099 | out_free_skb: | |
2100 | if (skb) | |
2101 | kfree_skb(skb); | |
2102 | return ret; | |
05c2828c MT |
2103 | } |
2104 | ||
1576d986 JW |
2105 | static int tun_peek_len(struct socket *sock) |
2106 | { | |
2107 | struct tun_file *tfile = container_of(sock, struct tun_file, socket); | |
2108 | struct tun_struct *tun; | |
2109 | int ret = 0; | |
2110 | ||
9484dc74 | 2111 | tun = tun_get(tfile); |
1576d986 JW |
2112 | if (!tun) |
2113 | return 0; | |
2114 | ||
2115 | ret = skb_array_peek_len(&tfile->tx_array); | |
2116 | tun_put(tun); | |
2117 | ||
2118 | return ret; | |
2119 | } | |
2120 | ||
05c2828c MT |
2121 | /* Ops structure to mimic raw sockets with tun */ |
2122 | static const struct proto_ops tun_socket_ops = { | |
1576d986 | 2123 | .peek_len = tun_peek_len, |
05c2828c MT |
2124 | .sendmsg = tun_sendmsg, |
2125 | .recvmsg = tun_recvmsg, | |
2126 | }; | |
2127 | ||
33dccbb0 HX |
2128 | static struct proto tun_proto = { |
2129 | .name = "tun", | |
2130 | .owner = THIS_MODULE, | |
54f968d6 | 2131 | .obj_size = sizeof(struct tun_file), |
33dccbb0 | 2132 | }; |
f019a7a5 | 2133 | |
980c9e8c DW |
2134 | static int tun_flags(struct tun_struct *tun) |
2135 | { | |
031f5e03 | 2136 | return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP); |
980c9e8c DW |
2137 | } |
2138 | ||
2139 | static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr, | |
2140 | char *buf) | |
2141 | { | |
2142 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); | |
2143 | return sprintf(buf, "0x%x\n", tun_flags(tun)); | |
2144 | } | |
2145 | ||
2146 | static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr, | |
2147 | char *buf) | |
2148 | { | |
2149 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); | |
0625c883 EB |
2150 | return uid_valid(tun->owner)? |
2151 | sprintf(buf, "%u\n", | |
2152 | from_kuid_munged(current_user_ns(), tun->owner)): | |
2153 | sprintf(buf, "-1\n"); | |
980c9e8c DW |
2154 | } |
2155 | ||
2156 | static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr, | |
2157 | char *buf) | |
2158 | { | |
2159 | struct tun_struct *tun = netdev_priv(to_net_dev(dev)); | |
0625c883 EB |
2160 | return gid_valid(tun->group) ? |
2161 | sprintf(buf, "%u\n", | |
2162 | from_kgid_munged(current_user_ns(), tun->group)): | |
2163 | sprintf(buf, "-1\n"); | |
980c9e8c DW |
2164 | } |
2165 | ||
2166 | static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL); | |
2167 | static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL); | |
2168 | static DEVICE_ATTR(group, 0444, tun_show_group, NULL); | |
2169 | ||
c4d33e24 TI |
2170 | static struct attribute *tun_dev_attrs[] = { |
2171 | &dev_attr_tun_flags.attr, | |
2172 | &dev_attr_owner.attr, | |
2173 | &dev_attr_group.attr, | |
2174 | NULL | |
2175 | }; | |
2176 | ||
2177 | static const struct attribute_group tun_attr_group = { | |
2178 | .attrs = tun_dev_attrs | |
2179 | }; | |
2180 | ||
d647a591 | 2181 | static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) |
1da177e4 LT |
2182 | { |
2183 | struct tun_struct *tun; | |
54f968d6 | 2184 | struct tun_file *tfile = file->private_data; |
1da177e4 LT |
2185 | struct net_device *dev; |
2186 | int err; | |
2187 | ||
7c0c3b1a JW |
2188 | if (tfile->detached) |
2189 | return -EINVAL; | |
2190 | ||
90e33d45 PP |
2191 | if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) { |
2192 | if (!capable(CAP_NET_ADMIN)) | |
2193 | return -EPERM; | |
2194 | ||
2195 | if (!(ifr->ifr_flags & IFF_NAPI) || | |
2196 | (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP) | |
2197 | return -EINVAL; | |
2198 | } | |
2199 | ||
74a3e5a7 EB |
2200 | dev = __dev_get_by_name(net, ifr->ifr_name); |
2201 | if (dev) { | |
f85ba780 DW |
2202 | if (ifr->ifr_flags & IFF_TUN_EXCL) |
2203 | return -EBUSY; | |
74a3e5a7 EB |
2204 | if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) |
2205 | tun = netdev_priv(dev); | |
2206 | else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) | |
2207 | tun = netdev_priv(dev); | |
2208 | else | |
2209 | return -EINVAL; | |
2210 | ||
8e6d91ae | 2211 | if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) != |
40630b82 | 2212 | !!(tun->flags & IFF_MULTI_QUEUE)) |
8e6d91ae JW |
2213 | return -EINVAL; |
2214 | ||
cde8b15f | 2215 | if (tun_not_capable(tun)) |
2b980dbd | 2216 | return -EPERM; |
5dbbaf2d | 2217 | err = security_tun_dev_open(tun->security); |
2b980dbd PM |
2218 | if (err < 0) |
2219 | return err; | |
2220 | ||
94317099 PP |
2221 | err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER, |
2222 | ifr->ifr_flags & IFF_NAPI); | |
a7385ba2 EB |
2223 | if (err < 0) |
2224 | return err; | |
4008e97f | 2225 | |
40630b82 | 2226 | if (tun->flags & IFF_MULTI_QUEUE && |
e8dbad66 JW |
2227 | (tun->numqueues + tun->numdisabled > 1)) { |
2228 | /* One or more queue has already been attached, no need | |
2229 | * to initialize the device again. | |
2230 | */ | |
2231 | return 0; | |
2232 | } | |
6aa20a22 | 2233 | } |
1da177e4 LT |
2234 | else { |
2235 | char *name; | |
2236 | unsigned long flags = 0; | |
edfb6a14 JW |
2237 | int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ? |
2238 | MAX_TAP_QUEUES : 1; | |
1da177e4 | 2239 | |
c260b772 | 2240 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
ca6bb5d7 | 2241 | return -EPERM; |
2b980dbd PM |
2242 | err = security_tun_dev_create(); |
2243 | if (err < 0) | |
2244 | return err; | |
ca6bb5d7 | 2245 | |
1da177e4 LT |
2246 | /* Set dev type */ |
2247 | if (ifr->ifr_flags & IFF_TUN) { | |
2248 | /* TUN device */ | |
40630b82 | 2249 | flags |= IFF_TUN; |
1da177e4 LT |
2250 | name = "tun%d"; |
2251 | } else if (ifr->ifr_flags & IFF_TAP) { | |
2252 | /* TAP device */ | |
40630b82 | 2253 | flags |= IFF_TAP; |
1da177e4 | 2254 | name = "tap%d"; |
6aa20a22 | 2255 | } else |
36989b90 | 2256 | return -EINVAL; |
6aa20a22 | 2257 | |
1da177e4 LT |
2258 | if (*ifr->ifr_name) |
2259 | name = ifr->ifr_name; | |
2260 | ||
c8d68e6b | 2261 | dev = alloc_netdev_mqs(sizeof(struct tun_struct), name, |
c835a677 TG |
2262 | NET_NAME_UNKNOWN, tun_setup, queues, |
2263 | queues); | |
edfb6a14 | 2264 | |
1da177e4 LT |
2265 | if (!dev) |
2266 | return -ENOMEM; | |
0ad646c8 | 2267 | err = dev_get_valid_name(net, dev, name); |
5c25f65f | 2268 | if (err < 0) |
0ad646c8 | 2269 | goto err_free_dev; |
1da177e4 | 2270 | |
fc54c658 | 2271 | dev_net_set(dev, net); |
f019a7a5 | 2272 | dev->rtnl_link_ops = &tun_link_ops; |
fb7589a1 | 2273 | dev->ifindex = tfile->ifindex; |
c4d33e24 | 2274 | dev->sysfs_groups[0] = &tun_attr_group; |
758e43b7 | 2275 | |
1da177e4 LT |
2276 | tun = netdev_priv(dev); |
2277 | tun->dev = dev; | |
2278 | tun->flags = flags; | |
f271b2cc | 2279 | tun->txflt.count = 0; |
d9d52b51 | 2280 | tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr); |
33dccbb0 | 2281 | |
eaea34b2 | 2282 | tun->align = NET_SKB_PAD; |
54f968d6 JW |
2283 | tun->filter_attached = false; |
2284 | tun->sndbuf = tfile->socket.sk->sk_sndbuf; | |
5503fcec | 2285 | tun->rx_batched = 0; |
33dccbb0 | 2286 | |
608b9977 PA |
2287 | tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats); |
2288 | if (!tun->pcpu_stats) { | |
2289 | err = -ENOMEM; | |
2290 | goto err_free_dev; | |
2291 | } | |
2292 | ||
96442e42 JW |
2293 | spin_lock_init(&tun->lock); |
2294 | ||
5dbbaf2d PM |
2295 | err = security_tun_dev_alloc_security(&tun->security); |
2296 | if (err < 0) | |
608b9977 | 2297 | goto err_free_stat; |
2b980dbd | 2298 | |
1da177e4 | 2299 | tun_net_init(dev); |
944a1376 | 2300 | tun_flow_init(tun); |
96442e42 | 2301 | |
88255375 | 2302 | dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | |
6680ec68 JW |
2303 | TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX | |
2304 | NETIF_F_HW_VLAN_STAG_TX; | |
2a2bbf17 | 2305 | dev->features = dev->hw_features | NETIF_F_LLTX; |
6671b224 FLVC |
2306 | dev->vlan_features = dev->features & |
2307 | ~(NETIF_F_HW_VLAN_CTAG_TX | | |
2308 | NETIF_F_HW_VLAN_STAG_TX); | |
88255375 | 2309 | |
4008e97f | 2310 | INIT_LIST_HEAD(&tun->disabled); |
94317099 | 2311 | err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI); |
eb0fb363 | 2312 | if (err < 0) |
662ca437 | 2313 | goto err_free_flow; |
eb0fb363 | 2314 | |
1da177e4 LT |
2315 | err = register_netdevice(tun->dev); |
2316 | if (err < 0) | |
662ca437 | 2317 | goto err_detach; |
1da177e4 LT |
2318 | } |
2319 | ||
af668b3c MT |
2320 | netif_carrier_on(tun->dev); |
2321 | ||
6b8a66ee | 2322 | tun_debug(KERN_INFO, tun, "tun_set_iff\n"); |
1da177e4 | 2323 | |
031f5e03 MT |
2324 | tun->flags = (tun->flags & ~TUN_FEATURES) | |
2325 | (ifr->ifr_flags & TUN_FEATURES); | |
c8d68e6b | 2326 | |
e35259a9 MK |
2327 | /* Make sure persistent devices do not get stuck in |
2328 | * xoff state. | |
2329 | */ | |
2330 | if (netif_running(tun->dev)) | |
c8d68e6b | 2331 | netif_tx_wake_all_queues(tun->dev); |
e35259a9 | 2332 | |
1da177e4 LT |
2333 | strcpy(ifr->ifr_name, tun->dev->name); |
2334 | return 0; | |
2335 | ||
662ca437 JW |
2336 | err_detach: |
2337 | tun_detach_all(dev); | |
ff244c6b ED |
2338 | /* register_netdevice() already called tun_free_netdev() */ |
2339 | goto err_free_dev; | |
2340 | ||
662ca437 JW |
2341 | err_free_flow: |
2342 | tun_flow_uninit(tun); | |
2343 | security_tun_dev_free_security(tun->security); | |
608b9977 PA |
2344 | err_free_stat: |
2345 | free_percpu(tun->pcpu_stats); | |
662ca437 | 2346 | err_free_dev: |
1da177e4 | 2347 | free_netdev(dev); |
1da177e4 LT |
2348 | return err; |
2349 | } | |
2350 | ||
9ce99cf6 | 2351 | static void tun_get_iff(struct net *net, struct tun_struct *tun, |
876bfd4d | 2352 | struct ifreq *ifr) |
e3b99556 | 2353 | { |
6b8a66ee | 2354 | tun_debug(KERN_INFO, tun, "tun_get_iff\n"); |
e3b99556 MM |
2355 | |
2356 | strcpy(ifr->ifr_name, tun->dev->name); | |
2357 | ||
980c9e8c | 2358 | ifr->ifr_flags = tun_flags(tun); |
e3b99556 | 2359 | |
e3b99556 MM |
2360 | } |
2361 | ||
5228ddc9 RR |
2362 | /* This is like a cut-down ethtool ops, except done via tun fd so no |
2363 | * privs required. */ | |
88255375 | 2364 | static int set_offload(struct tun_struct *tun, unsigned long arg) |
5228ddc9 | 2365 | { |
c8f44aff | 2366 | netdev_features_t features = 0; |
5228ddc9 RR |
2367 | |
2368 | if (arg & TUN_F_CSUM) { | |
88255375 | 2369 | features |= NETIF_F_HW_CSUM; |
5228ddc9 RR |
2370 | arg &= ~TUN_F_CSUM; |
2371 | ||
2372 | if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { | |
2373 | if (arg & TUN_F_TSO_ECN) { | |
2374 | features |= NETIF_F_TSO_ECN; | |
2375 | arg &= ~TUN_F_TSO_ECN; | |
2376 | } | |
2377 | if (arg & TUN_F_TSO4) | |
2378 | features |= NETIF_F_TSO; | |
2379 | if (arg & TUN_F_TSO6) | |
2380 | features |= NETIF_F_TSO6; | |
2381 | arg &= ~(TUN_F_TSO4|TUN_F_TSO6); | |
2382 | } | |
0c19f846 WB |
2383 | |
2384 | arg &= ~TUN_F_UFO; | |
5228ddc9 RR |
2385 | } |
2386 | ||
2387 | /* This gives the user a way to test for new features in future by | |
2388 | * trying to set them. */ | |
2389 | if (arg) | |
2390 | return -EINVAL; | |
2391 | ||
88255375 | 2392 | tun->set_features = features; |
09050957 YI |
2393 | tun->dev->wanted_features &= ~TUN_USER_FEATURES; |
2394 | tun->dev->wanted_features |= features; | |
88255375 | 2395 | netdev_update_features(tun->dev); |
5228ddc9 RR |
2396 | |
2397 | return 0; | |
2398 | } | |
2399 | ||
c8d68e6b JW |
2400 | static void tun_detach_filter(struct tun_struct *tun, int n) |
2401 | { | |
2402 | int i; | |
2403 | struct tun_file *tfile; | |
2404 | ||
2405 | for (i = 0; i < n; i++) { | |
b8deabd3 | 2406 | tfile = rtnl_dereference(tun->tfiles[i]); |
8ced425e HFS |
2407 | lock_sock(tfile->socket.sk); |
2408 | sk_detach_filter(tfile->socket.sk); | |
2409 | release_sock(tfile->socket.sk); | |
c8d68e6b JW |
2410 | } |
2411 | ||
2412 | tun->filter_attached = false; | |
2413 | } | |
2414 | ||
2415 | static int tun_attach_filter(struct tun_struct *tun) | |
2416 | { | |
2417 | int i, ret = 0; | |
2418 | struct tun_file *tfile; | |
2419 | ||
2420 | for (i = 0; i < tun->numqueues; i++) { | |
b8deabd3 | 2421 | tfile = rtnl_dereference(tun->tfiles[i]); |
8ced425e HFS |
2422 | lock_sock(tfile->socket.sk); |
2423 | ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); | |
2424 | release_sock(tfile->socket.sk); | |
c8d68e6b JW |
2425 | if (ret) { |
2426 | tun_detach_filter(tun, i); | |
2427 | return ret; | |
2428 | } | |
2429 | } | |
2430 | ||
2431 | tun->filter_attached = true; | |
2432 | return ret; | |
2433 | } | |
2434 | ||
2435 | static void tun_set_sndbuf(struct tun_struct *tun) | |
2436 | { | |
2437 | struct tun_file *tfile; | |
2438 | int i; | |
2439 | ||
2440 | for (i = 0; i < tun->numqueues; i++) { | |
b8deabd3 | 2441 | tfile = rtnl_dereference(tun->tfiles[i]); |
c8d68e6b JW |
2442 | tfile->socket.sk->sk_sndbuf = tun->sndbuf; |
2443 | } | |
2444 | } | |
2445 | ||
cde8b15f JW |
2446 | static int tun_set_queue(struct file *file, struct ifreq *ifr) |
2447 | { | |
2448 | struct tun_file *tfile = file->private_data; | |
2449 | struct tun_struct *tun; | |
cde8b15f JW |
2450 | int ret = 0; |
2451 | ||
2452 | rtnl_lock(); | |
2453 | ||
2454 | if (ifr->ifr_flags & IFF_ATTACH_QUEUE) { | |
4008e97f | 2455 | tun = tfile->detached; |
5dbbaf2d | 2456 | if (!tun) { |
cde8b15f | 2457 | ret = -EINVAL; |
5dbbaf2d PM |
2458 | goto unlock; |
2459 | } | |
2460 | ret = security_tun_dev_attach_queue(tun->security); | |
2461 | if (ret < 0) | |
2462 | goto unlock; | |
94317099 | 2463 | ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI); |
4008e97f | 2464 | } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) { |
b8deabd3 | 2465 | tun = rtnl_dereference(tfile->tun); |
40630b82 | 2466 | if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) |
4008e97f JW |
2467 | ret = -EINVAL; |
2468 | else | |
2469 | __tun_detach(tfile, false); | |
2470 | } else | |
cde8b15f JW |
2471 | ret = -EINVAL; |
2472 | ||
5dbbaf2d | 2473 | unlock: |
cde8b15f JW |
2474 | rtnl_unlock(); |
2475 | return ret; | |
2476 | } | |
2477 | ||
50857e2a AB |
2478 | static long __tun_chr_ioctl(struct file *file, unsigned int cmd, |
2479 | unsigned long arg, int ifreq_len) | |
1da177e4 | 2480 | { |
36b50bab | 2481 | struct tun_file *tfile = file->private_data; |
631ab46b | 2482 | struct tun_struct *tun; |
1da177e4 LT |
2483 | void __user* argp = (void __user*)arg; |
2484 | struct ifreq ifr; | |
0625c883 EB |
2485 | kuid_t owner; |
2486 | kgid_t group; | |
33dccbb0 | 2487 | int sndbuf; |
d9d52b51 | 2488 | int vnet_hdr_sz; |
fb7589a1 | 2489 | unsigned int ifindex; |
1cf8e410 | 2490 | int le; |
f271b2cc | 2491 | int ret; |
1da177e4 | 2492 | |
20861f26 | 2493 | if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == SOCK_IOC_TYPE) { |
50857e2a | 2494 | if (copy_from_user(&ifr, argp, ifreq_len)) |
1da177e4 | 2495 | return -EFAULT; |
8bbb1813 | 2496 | } else { |
a117dacd | 2497 | memset(&ifr, 0, sizeof(ifr)); |
8bbb1813 | 2498 | } |
631ab46b EB |
2499 | if (cmd == TUNGETFEATURES) { |
2500 | /* Currently this just means: "what IFF flags are valid?". | |
2501 | * This is needed because we never checked for invalid flags on | |
031f5e03 MT |
2502 | * TUNSETIFF. |
2503 | */ | |
2504 | return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES, | |
631ab46b | 2505 | (unsigned int __user*)argp); |
cde8b15f JW |
2506 | } else if (cmd == TUNSETQUEUE) |
2507 | return tun_set_queue(file, &ifr); | |
631ab46b | 2508 | |
c8d68e6b | 2509 | ret = 0; |
876bfd4d HX |
2510 | rtnl_lock(); |
2511 | ||
9484dc74 | 2512 | tun = tun_get(tfile); |
0f16bc13 GF |
2513 | if (cmd == TUNSETIFF) { |
2514 | ret = -EEXIST; | |
2515 | if (tun) | |
2516 | goto unlock; | |
2517 | ||
1da177e4 LT |
2518 | ifr.ifr_name[IFNAMSIZ-1] = '\0'; |
2519 | ||
140e807d | 2520 | ret = tun_set_iff(sock_net(&tfile->sk), file, &ifr); |
1da177e4 | 2521 | |
876bfd4d HX |
2522 | if (ret) |
2523 | goto unlock; | |
1da177e4 | 2524 | |
50857e2a | 2525 | if (copy_to_user(argp, &ifr, ifreq_len)) |
876bfd4d HX |
2526 | ret = -EFAULT; |
2527 | goto unlock; | |
1da177e4 | 2528 | } |
fb7589a1 PE |
2529 | if (cmd == TUNSETIFINDEX) { |
2530 | ret = -EPERM; | |
2531 | if (tun) | |
2532 | goto unlock; | |
2533 | ||
2534 | ret = -EFAULT; | |
2535 | if (copy_from_user(&ifindex, argp, sizeof(ifindex))) | |
2536 | goto unlock; | |
2537 | ||
2538 | ret = 0; | |
2539 | tfile->ifindex = ifindex; | |
2540 | goto unlock; | |
2541 | } | |
1da177e4 | 2542 | |
876bfd4d | 2543 | ret = -EBADFD; |
1da177e4 | 2544 | if (!tun) |
876bfd4d | 2545 | goto unlock; |
1da177e4 | 2546 | |
1e588338 | 2547 | tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd); |
1da177e4 | 2548 | |
631ab46b | 2549 | ret = 0; |
1da177e4 | 2550 | switch (cmd) { |
e3b99556 | 2551 | case TUNGETIFF: |
9ce99cf6 | 2552 | tun_get_iff(current->nsproxy->net_ns, tun, &ifr); |
e3b99556 | 2553 | |
3d407a80 PE |
2554 | if (tfile->detached) |
2555 | ifr.ifr_flags |= IFF_DETACH_QUEUE; | |
849c9b6f PE |
2556 | if (!tfile->socket.sk->sk_filter) |
2557 | ifr.ifr_flags |= IFF_NOFILTER; | |
3d407a80 | 2558 | |
50857e2a | 2559 | if (copy_to_user(argp, &ifr, ifreq_len)) |
631ab46b | 2560 | ret = -EFAULT; |
e3b99556 MM |
2561 | break; |
2562 | ||
1da177e4 LT |
2563 | case TUNSETNOCSUM: |
2564 | /* Disable/Enable checksum */ | |
1da177e4 | 2565 | |
88255375 MM |
2566 | /* [unimplemented] */ |
2567 | tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n", | |
6b8a66ee | 2568 | arg ? "disabled" : "enabled"); |
1da177e4 LT |
2569 | break; |
2570 | ||
2571 | case TUNSETPERSIST: | |
54f968d6 JW |
2572 | /* Disable/Enable persist mode. Keep an extra reference to the |
2573 | * module to prevent the module being unprobed. | |
2574 | */ | |
40630b82 MT |
2575 | if (arg && !(tun->flags & IFF_PERSIST)) { |
2576 | tun->flags |= IFF_PERSIST; | |
54f968d6 | 2577 | __module_get(THIS_MODULE); |
dd38bd85 | 2578 | } |
40630b82 MT |
2579 | if (!arg && (tun->flags & IFF_PERSIST)) { |
2580 | tun->flags &= ~IFF_PERSIST; | |
54f968d6 JW |
2581 | module_put(THIS_MODULE); |
2582 | } | |
1da177e4 | 2583 | |
6b8a66ee JP |
2584 | tun_debug(KERN_INFO, tun, "persist %s\n", |
2585 | arg ? "enabled" : "disabled"); | |
1da177e4 LT |
2586 | break; |
2587 | ||
2588 | case TUNSETOWNER: | |
2589 | /* Set owner of the device */ | |
0625c883 EB |
2590 | owner = make_kuid(current_user_ns(), arg); |
2591 | if (!uid_valid(owner)) { | |
2592 | ret = -EINVAL; | |
2593 | break; | |
2594 | } | |
2595 | tun->owner = owner; | |
1e588338 | 2596 | tun_debug(KERN_INFO, tun, "owner set to %u\n", |
0625c883 | 2597 | from_kuid(&init_user_ns, tun->owner)); |
1da177e4 LT |
2598 | break; |
2599 | ||
8c644623 GG |
2600 | case TUNSETGROUP: |
2601 | /* Set group of the device */ | |
0625c883 EB |
2602 | group = make_kgid(current_user_ns(), arg); |
2603 | if (!gid_valid(group)) { | |
2604 | ret = -EINVAL; | |
2605 | break; | |
2606 | } | |
2607 | tun->group = group; | |
1e588338 | 2608 | tun_debug(KERN_INFO, tun, "group set to %u\n", |
0625c883 | 2609 | from_kgid(&init_user_ns, tun->group)); |
8c644623 GG |
2610 | break; |
2611 | ||
ff4cc3ac MK |
2612 | case TUNSETLINK: |
2613 | /* Only allow setting the type when the interface is down */ | |
2614 | if (tun->dev->flags & IFF_UP) { | |
6b8a66ee JP |
2615 | tun_debug(KERN_INFO, tun, |
2616 | "Linktype set failed because interface is up\n"); | |
48abfe05 | 2617 | ret = -EBUSY; |
ff4cc3ac MK |
2618 | } else { |
2619 | tun->dev->type = (int) arg; | |
6b8a66ee JP |
2620 | tun_debug(KERN_INFO, tun, "linktype set to %d\n", |
2621 | tun->dev->type); | |
48abfe05 | 2622 | ret = 0; |
ff4cc3ac | 2623 | } |
631ab46b | 2624 | break; |
ff4cc3ac | 2625 | |
1da177e4 LT |
2626 | #ifdef TUN_DEBUG |
2627 | case TUNSETDEBUG: | |
2628 | tun->debug = arg; | |
2629 | break; | |
2630 | #endif | |
5228ddc9 | 2631 | case TUNSETOFFLOAD: |
88255375 | 2632 | ret = set_offload(tun, arg); |
631ab46b | 2633 | break; |
5228ddc9 | 2634 | |
f271b2cc MK |
2635 | case TUNSETTXFILTER: |
2636 | /* Can be set only for TAPs */ | |
631ab46b | 2637 | ret = -EINVAL; |
40630b82 | 2638 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
631ab46b | 2639 | break; |
c0e5a8c2 | 2640 | ret = update_filter(&tun->txflt, (void __user *)arg); |
631ab46b | 2641 | break; |
1da177e4 LT |
2642 | |
2643 | case SIOCGIFHWADDR: | |
b595076a | 2644 | /* Get hw address */ |
f271b2cc MK |
2645 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); |
2646 | ifr.ifr_hwaddr.sa_family = tun->dev->type; | |
50857e2a | 2647 | if (copy_to_user(argp, &ifr, ifreq_len)) |
631ab46b EB |
2648 | ret = -EFAULT; |
2649 | break; | |
1da177e4 LT |
2650 | |
2651 | case SIOCSIFHWADDR: | |
f271b2cc | 2652 | /* Set hw address */ |
6b8a66ee JP |
2653 | tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n", |
2654 | ifr.ifr_hwaddr.sa_data); | |
40102371 | 2655 | |
40102371 | 2656 | ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); |
631ab46b | 2657 | break; |
33dccbb0 HX |
2658 | |
2659 | case TUNGETSNDBUF: | |
54f968d6 | 2660 | sndbuf = tfile->socket.sk->sk_sndbuf; |
33dccbb0 HX |
2661 | if (copy_to_user(argp, &sndbuf, sizeof(sndbuf))) |
2662 | ret = -EFAULT; | |
2663 | break; | |
2664 | ||
2665 | case TUNSETSNDBUF: | |
2666 | if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) { | |
2667 | ret = -EFAULT; | |
2668 | break; | |
2669 | } | |
93161922 CG |
2670 | if (sndbuf <= 0) { |
2671 | ret = -EINVAL; | |
2672 | break; | |
2673 | } | |
33dccbb0 | 2674 | |
c8d68e6b JW |
2675 | tun->sndbuf = sndbuf; |
2676 | tun_set_sndbuf(tun); | |
33dccbb0 HX |
2677 | break; |
2678 | ||
d9d52b51 MT |
2679 | case TUNGETVNETHDRSZ: |
2680 | vnet_hdr_sz = tun->vnet_hdr_sz; | |
2681 | if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz))) | |
2682 | ret = -EFAULT; | |
2683 | break; | |
2684 | ||
2685 | case TUNSETVNETHDRSZ: | |
2686 | if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) { | |
2687 | ret = -EFAULT; | |
2688 | break; | |
2689 | } | |
2690 | if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) { | |
2691 | ret = -EINVAL; | |
2692 | break; | |
2693 | } | |
2694 | ||
2695 | tun->vnet_hdr_sz = vnet_hdr_sz; | |
2696 | break; | |
2697 | ||
1cf8e410 MT |
2698 | case TUNGETVNETLE: |
2699 | le = !!(tun->flags & TUN_VNET_LE); | |
2700 | if (put_user(le, (int __user *)argp)) | |
2701 | ret = -EFAULT; | |
2702 | break; | |
2703 | ||
2704 | case TUNSETVNETLE: | |
2705 | if (get_user(le, (int __user *)argp)) { | |
2706 | ret = -EFAULT; | |
2707 | break; | |
2708 | } | |
2709 | if (le) | |
2710 | tun->flags |= TUN_VNET_LE; | |
2711 | else | |
2712 | tun->flags &= ~TUN_VNET_LE; | |
2713 | break; | |
2714 | ||
8b8e658b GK |
2715 | case TUNGETVNETBE: |
2716 | ret = tun_get_vnet_be(tun, argp); | |
2717 | break; | |
2718 | ||
2719 | case TUNSETVNETBE: | |
2720 | ret = tun_set_vnet_be(tun, argp); | |
2721 | break; | |
2722 | ||
99405162 MT |
2723 | case TUNATTACHFILTER: |
2724 | /* Can be set only for TAPs */ | |
2725 | ret = -EINVAL; | |
40630b82 | 2726 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
99405162 MT |
2727 | break; |
2728 | ret = -EFAULT; | |
54f968d6 | 2729 | if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog))) |
99405162 MT |
2730 | break; |
2731 | ||
c8d68e6b | 2732 | ret = tun_attach_filter(tun); |
99405162 MT |
2733 | break; |
2734 | ||
2735 | case TUNDETACHFILTER: | |
2736 | /* Can be set only for TAPs */ | |
2737 | ret = -EINVAL; | |
40630b82 | 2738 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
99405162 | 2739 | break; |
c8d68e6b JW |
2740 | ret = 0; |
2741 | tun_detach_filter(tun, tun->numqueues); | |
99405162 MT |
2742 | break; |
2743 | ||
76975e9c PE |
2744 | case TUNGETFILTER: |
2745 | ret = -EINVAL; | |
40630b82 | 2746 | if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP) |
76975e9c PE |
2747 | break; |
2748 | ret = -EFAULT; | |
2749 | if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog))) | |
2750 | break; | |
2751 | ret = 0; | |
2752 | break; | |
2753 | ||
1da177e4 | 2754 | default: |
631ab46b EB |
2755 | ret = -EINVAL; |
2756 | break; | |
ee289b64 | 2757 | } |
1da177e4 | 2758 | |
876bfd4d HX |
2759 | unlock: |
2760 | rtnl_unlock(); | |
2761 | if (tun) | |
2762 | tun_put(tun); | |
631ab46b | 2763 | return ret; |
1da177e4 LT |
2764 | } |
2765 | ||
50857e2a AB |
2766 | static long tun_chr_ioctl(struct file *file, |
2767 | unsigned int cmd, unsigned long arg) | |
2768 | { | |
2769 | return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq)); | |
2770 | } | |
2771 | ||
2772 | #ifdef CONFIG_COMPAT | |
2773 | static long tun_chr_compat_ioctl(struct file *file, | |
2774 | unsigned int cmd, unsigned long arg) | |
2775 | { | |
2776 | switch (cmd) { | |
2777 | case TUNSETIFF: | |
2778 | case TUNGETIFF: | |
2779 | case TUNSETTXFILTER: | |
2780 | case TUNGETSNDBUF: | |
2781 | case TUNSETSNDBUF: | |
2782 | case SIOCGIFHWADDR: | |
2783 | case SIOCSIFHWADDR: | |
2784 | arg = (unsigned long)compat_ptr(arg); | |
2785 | break; | |
2786 | default: | |
2787 | arg = (compat_ulong_t)arg; | |
2788 | break; | |
2789 | } | |
2790 | ||
2791 | /* | |
2792 | * compat_ifreq is shorter than ifreq, so we must not access beyond | |
2793 | * the end of that structure. All fields that are used in this | |
2794 | * driver are compatible though, we don't need to convert the | |
2795 | * contents. | |
2796 | */ | |
2797 | return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq)); | |
2798 | } | |
2799 | #endif /* CONFIG_COMPAT */ | |
2800 | ||
1da177e4 LT |
2801 | static int tun_chr_fasync(int fd, struct file *file, int on) |
2802 | { | |
54f968d6 | 2803 | struct tun_file *tfile = file->private_data; |
1da177e4 LT |
2804 | int ret; |
2805 | ||
54f968d6 | 2806 | if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) |
9d319522 | 2807 | goto out; |
6aa20a22 | 2808 | |
1da177e4 | 2809 | if (on) { |
e0b93edd | 2810 | __f_setown(file, task_pid(current), PIDTYPE_PID, 0); |
54f968d6 | 2811 | tfile->flags |= TUN_FASYNC; |
6aa20a22 | 2812 | } else |
54f968d6 | 2813 | tfile->flags &= ~TUN_FASYNC; |
9d319522 JC |
2814 | ret = 0; |
2815 | out: | |
9d319522 | 2816 | return ret; |
1da177e4 LT |
2817 | } |
2818 | ||
2819 | static int tun_chr_open(struct inode *inode, struct file * file) | |
2820 | { | |
140e807d | 2821 | struct net *net = current->nsproxy->net_ns; |
631ab46b | 2822 | struct tun_file *tfile; |
deed49fb | 2823 | |
6b8a66ee | 2824 | DBG1(KERN_INFO, "tunX: tun_chr_open\n"); |
631ab46b | 2825 | |
140e807d | 2826 | tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, |
11aa9c28 | 2827 | &tun_proto, 0); |
631ab46b EB |
2828 | if (!tfile) |
2829 | return -ENOMEM; | |
c956674b | 2830 | RCU_INIT_POINTER(tfile->tun, NULL); |
54f968d6 | 2831 | tfile->flags = 0; |
fb7589a1 | 2832 | tfile->ifindex = 0; |
54f968d6 | 2833 | |
54f968d6 | 2834 | init_waitqueue_head(&tfile->wq.wait); |
9e641bdc | 2835 | RCU_INIT_POINTER(tfile->socket.wq, &tfile->wq); |
54f968d6 JW |
2836 | |
2837 | tfile->socket.file = file; | |
2838 | tfile->socket.ops = &tun_socket_ops; | |
2839 | ||
2840 | sock_init_data(&tfile->socket, &tfile->sk); | |
54f968d6 JW |
2841 | |
2842 | tfile->sk.sk_write_space = tun_sock_write_space; | |
2843 | tfile->sk.sk_sndbuf = INT_MAX; | |
2844 | ||
631ab46b | 2845 | file->private_data = tfile; |
4008e97f | 2846 | INIT_LIST_HEAD(&tfile->next); |
54f968d6 | 2847 | |
19a6afb2 JW |
2848 | sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); |
2849 | ||
1da177e4 LT |
2850 | return 0; |
2851 | } | |
2852 | ||
2853 | static int tun_chr_close(struct inode *inode, struct file *file) | |
2854 | { | |
631ab46b | 2855 | struct tun_file *tfile = file->private_data; |
1da177e4 | 2856 | |
c8d68e6b | 2857 | tun_detach(tfile, true); |
1da177e4 LT |
2858 | |
2859 | return 0; | |
2860 | } | |
2861 | ||
93e14b6d | 2862 | #ifdef CONFIG_PROC_FS |
9484dc74 | 2863 | static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file) |
93e14b6d | 2864 | { |
9484dc74 | 2865 | struct tun_file *tfile = file->private_data; |
93e14b6d MY |
2866 | struct tun_struct *tun; |
2867 | struct ifreq ifr; | |
2868 | ||
2869 | memset(&ifr, 0, sizeof(ifr)); | |
2870 | ||
2871 | rtnl_lock(); | |
9484dc74 | 2872 | tun = tun_get(tfile); |
93e14b6d MY |
2873 | if (tun) |
2874 | tun_get_iff(current->nsproxy->net_ns, tun, &ifr); | |
2875 | rtnl_unlock(); | |
2876 | ||
2877 | if (tun) | |
2878 | tun_put(tun); | |
2879 | ||
a3816ab0 | 2880 | seq_printf(m, "iff:\t%s\n", ifr.ifr_name); |
93e14b6d MY |
2881 | } |
2882 | #endif | |
2883 | ||
d54b1fdb | 2884 | static const struct file_operations tun_fops = { |
6aa20a22 | 2885 | .owner = THIS_MODULE, |
1da177e4 | 2886 | .llseek = no_llseek, |
9b067034 | 2887 | .read_iter = tun_chr_read_iter, |
f5ff53b4 | 2888 | .write_iter = tun_chr_write_iter, |
1da177e4 | 2889 | .poll = tun_chr_poll, |
50857e2a AB |
2890 | .unlocked_ioctl = tun_chr_ioctl, |
2891 | #ifdef CONFIG_COMPAT | |
2892 | .compat_ioctl = tun_chr_compat_ioctl, | |
2893 | #endif | |
1da177e4 LT |
2894 | .open = tun_chr_open, |
2895 | .release = tun_chr_close, | |
93e14b6d MY |
2896 | .fasync = tun_chr_fasync, |
2897 | #ifdef CONFIG_PROC_FS | |
2898 | .show_fdinfo = tun_chr_show_fdinfo, | |
2899 | #endif | |
1da177e4 LT |
2900 | }; |
2901 | ||
2902 | static struct miscdevice tun_miscdev = { | |
2903 | .minor = TUN_MINOR, | |
2904 | .name = "tun", | |
e454cea2 | 2905 | .nodename = "net/tun", |
1da177e4 | 2906 | .fops = &tun_fops, |
1da177e4 LT |
2907 | }; |
2908 | ||
2909 | /* ethtool interface */ | |
2910 | ||
29ccc49d PR |
2911 | static int tun_get_link_ksettings(struct net_device *dev, |
2912 | struct ethtool_link_ksettings *cmd) | |
2913 | { | |
2914 | ethtool_link_ksettings_zero_link_mode(cmd, supported); | |
2915 | ethtool_link_ksettings_zero_link_mode(cmd, advertising); | |
2916 | cmd->base.speed = SPEED_10; | |
2917 | cmd->base.duplex = DUPLEX_FULL; | |
2918 | cmd->base.port = PORT_TP; | |
2919 | cmd->base.phy_address = 0; | |
2920 | cmd->base.autoneg = AUTONEG_DISABLE; | |
1da177e4 LT |
2921 | return 0; |
2922 | } | |
2923 | ||
2924 | static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) | |
2925 | { | |
2926 | struct tun_struct *tun = netdev_priv(dev); | |
2927 | ||
33a5ba14 RJ |
2928 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
2929 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); | |
1da177e4 LT |
2930 | |
2931 | switch (tun->flags & TUN_TYPE_MASK) { | |
40630b82 | 2932 | case IFF_TUN: |
33a5ba14 | 2933 | strlcpy(info->bus_info, "tun", sizeof(info->bus_info)); |
1da177e4 | 2934 | break; |
40630b82 | 2935 | case IFF_TAP: |
33a5ba14 | 2936 | strlcpy(info->bus_info, "tap", sizeof(info->bus_info)); |
1da177e4 LT |
2937 | break; |
2938 | } | |
2939 | } | |
2940 | ||
2941 | static u32 tun_get_msglevel(struct net_device *dev) | |
2942 | { | |
2943 | #ifdef TUN_DEBUG | |
2944 | struct tun_struct *tun = netdev_priv(dev); | |
2945 | return tun->debug; | |
2946 | #else | |
2947 | return -EOPNOTSUPP; | |
2948 | #endif | |
2949 | } | |
2950 | ||
2951 | static void tun_set_msglevel(struct net_device *dev, u32 value) | |
2952 | { | |
2953 | #ifdef TUN_DEBUG | |
2954 | struct tun_struct *tun = netdev_priv(dev); | |
2955 | tun->debug = value; | |
2956 | #endif | |
2957 | } | |
2958 | ||
5503fcec JW |
2959 | static int tun_get_coalesce(struct net_device *dev, |
2960 | struct ethtool_coalesce *ec) | |
2961 | { | |
2962 | struct tun_struct *tun = netdev_priv(dev); | |
2963 | ||
2964 | ec->rx_max_coalesced_frames = tun->rx_batched; | |
2965 | ||
2966 | return 0; | |
2967 | } | |
2968 | ||
2969 | static int tun_set_coalesce(struct net_device *dev, | |
2970 | struct ethtool_coalesce *ec) | |
2971 | { | |
2972 | struct tun_struct *tun = netdev_priv(dev); | |
2973 | ||
2974 | if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT) | |
2975 | tun->rx_batched = NAPI_POLL_WEIGHT; | |
2976 | else | |
2977 | tun->rx_batched = ec->rx_max_coalesced_frames; | |
2978 | ||
2979 | return 0; | |
2980 | } | |
2981 | ||
7282d491 | 2982 | static const struct ethtool_ops tun_ethtool_ops = { |
1da177e4 LT |
2983 | .get_drvinfo = tun_get_drvinfo, |
2984 | .get_msglevel = tun_get_msglevel, | |
2985 | .set_msglevel = tun_set_msglevel, | |
bee31369 | 2986 | .get_link = ethtool_op_get_link, |
eda29772 | 2987 | .get_ts_info = ethtool_op_get_ts_info, |
5503fcec JW |
2988 | .get_coalesce = tun_get_coalesce, |
2989 | .set_coalesce = tun_set_coalesce, | |
29ccc49d | 2990 | .get_link_ksettings = tun_get_link_ksettings, |
1da177e4 LT |
2991 | }; |
2992 | ||
1576d986 JW |
2993 | static int tun_queue_resize(struct tun_struct *tun) |
2994 | { | |
2995 | struct net_device *dev = tun->dev; | |
2996 | struct tun_file *tfile; | |
2997 | struct skb_array **arrays; | |
2998 | int n = tun->numqueues + tun->numdisabled; | |
2999 | int ret, i; | |
3000 | ||
12039046 | 3001 | arrays = kmalloc_array(n, sizeof(*arrays), GFP_KERNEL); |
1576d986 JW |
3002 | if (!arrays) |
3003 | return -ENOMEM; | |
3004 | ||
3005 | for (i = 0; i < tun->numqueues; i++) { | |
3006 | tfile = rtnl_dereference(tun->tfiles[i]); | |
3007 | arrays[i] = &tfile->tx_array; | |
3008 | } | |
3009 | list_for_each_entry(tfile, &tun->disabled, next) | |
3010 | arrays[i++] = &tfile->tx_array; | |
3011 | ||
3012 | ret = skb_array_resize_multiple(arrays, n, | |
3013 | dev->tx_queue_len, GFP_KERNEL); | |
3014 | ||
3015 | kfree(arrays); | |
3016 | return ret; | |
3017 | } | |
3018 | ||
3019 | static int tun_device_event(struct notifier_block *unused, | |
3020 | unsigned long event, void *ptr) | |
3021 | { | |
3022 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
3023 | struct tun_struct *tun = netdev_priv(dev); | |
3024 | ||
86dfb4ac CG |
3025 | if (dev->rtnl_link_ops != &tun_link_ops) |
3026 | return NOTIFY_DONE; | |
3027 | ||
1576d986 JW |
3028 | switch (event) { |
3029 | case NETDEV_CHANGE_TX_QUEUE_LEN: | |
3030 | if (tun_queue_resize(tun)) | |
3031 | return NOTIFY_BAD; | |
3032 | break; | |
3033 | default: | |
3034 | break; | |
3035 | } | |
3036 | ||
3037 | return NOTIFY_DONE; | |
3038 | } | |
3039 | ||
3040 | static struct notifier_block tun_notifier_block __read_mostly = { | |
3041 | .notifier_call = tun_device_event, | |
3042 | }; | |
79d17604 | 3043 | |
1da177e4 LT |
3044 | static int __init tun_init(void) |
3045 | { | |
3046 | int ret = 0; | |
3047 | ||
6b8a66ee | 3048 | pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION); |
1da177e4 | 3049 | |
f019a7a5 | 3050 | ret = rtnl_link_register(&tun_link_ops); |
79d17604 | 3051 | if (ret) { |
6b8a66ee | 3052 | pr_err("Can't register link_ops\n"); |
f019a7a5 | 3053 | goto err_linkops; |
79d17604 PE |
3054 | } |
3055 | ||
1da177e4 | 3056 | ret = misc_register(&tun_miscdev); |
79d17604 | 3057 | if (ret) { |
6b8a66ee | 3058 | pr_err("Can't register misc device %d\n", TUN_MINOR); |
79d17604 PE |
3059 | goto err_misc; |
3060 | } | |
1576d986 | 3061 | |
5edfbd3c TZ |
3062 | ret = register_netdevice_notifier(&tun_notifier_block); |
3063 | if (ret) { | |
3064 | pr_err("Can't register netdevice notifier\n"); | |
3065 | goto err_notifier; | |
3066 | } | |
3067 | ||
f019a7a5 | 3068 | return 0; |
5edfbd3c TZ |
3069 | |
3070 | err_notifier: | |
3071 | misc_deregister(&tun_miscdev); | |
79d17604 | 3072 | err_misc: |
f019a7a5 EB |
3073 | rtnl_link_unregister(&tun_link_ops); |
3074 | err_linkops: | |
1da177e4 LT |
3075 | return ret; |
3076 | } | |
3077 | ||
3078 | static void tun_cleanup(void) | |
3079 | { | |
6aa20a22 | 3080 | misc_deregister(&tun_miscdev); |
f019a7a5 | 3081 | rtnl_link_unregister(&tun_link_ops); |
1576d986 | 3082 | unregister_netdevice_notifier(&tun_notifier_block); |
1da177e4 LT |
3083 | } |
3084 | ||
05c2828c MT |
3085 | /* Get an underlying socket object from tun file. Returns error unless file is |
3086 | * attached to a device. The returned object works like a packet socket, it | |
3087 | * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for | |
3088 | * holding a reference to the file for as long as the socket is in use. */ | |
3089 | struct socket *tun_get_socket(struct file *file) | |
3090 | { | |
6e914fc7 | 3091 | struct tun_file *tfile; |
05c2828c MT |
3092 | if (file->f_op != &tun_fops) |
3093 | return ERR_PTR(-EINVAL); | |
6e914fc7 JW |
3094 | tfile = file->private_data; |
3095 | if (!tfile) | |
05c2828c | 3096 | return ERR_PTR(-EBADFD); |
54f968d6 | 3097 | return &tfile->socket; |
05c2828c MT |
3098 | } |
3099 | EXPORT_SYMBOL_GPL(tun_get_socket); | |
3100 | ||
83339c6b JW |
3101 | struct skb_array *tun_get_skb_array(struct file *file) |
3102 | { | |
3103 | struct tun_file *tfile; | |
3104 | ||
3105 | if (file->f_op != &tun_fops) | |
3106 | return ERR_PTR(-EINVAL); | |
3107 | tfile = file->private_data; | |
3108 | if (!tfile) | |
3109 | return ERR_PTR(-EBADFD); | |
3110 | return &tfile->tx_array; | |
3111 | } | |
3112 | EXPORT_SYMBOL_GPL(tun_get_skb_array); | |
3113 | ||
1da177e4 LT |
3114 | module_init(tun_init); |
3115 | module_exit(tun_cleanup); | |
3116 | MODULE_DESCRIPTION(DRV_DESCRIPTION); | |
3117 | MODULE_AUTHOR(DRV_COPYRIGHT); | |
3118 | MODULE_LICENSE("GPL"); | |
3119 | MODULE_ALIAS_MISCDEV(TUN_MINOR); | |
578454ff | 3120 | MODULE_ALIAS("devname:net/tun"); |