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