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> |
f271b2cc | 25 | * Use random_ether_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 | ||
37 | #define DRV_NAME "tun" | |
38 | #define DRV_VERSION "1.6" | |
39 | #define DRV_DESCRIPTION "Universal TUN/TAP device driver" | |
40 | #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>" | |
41 | ||
1da177e4 LT |
42 | #include <linux/module.h> |
43 | #include <linux/errno.h> | |
44 | #include <linux/kernel.h> | |
45 | #include <linux/major.h> | |
46 | #include <linux/slab.h> | |
fd3e05b6 | 47 | #include <linux/smp_lock.h> |
1da177e4 LT |
48 | #include <linux/poll.h> |
49 | #include <linux/fcntl.h> | |
50 | #include <linux/init.h> | |
51 | #include <linux/skbuff.h> | |
52 | #include <linux/netdevice.h> | |
53 | #include <linux/etherdevice.h> | |
54 | #include <linux/miscdevice.h> | |
55 | #include <linux/ethtool.h> | |
56 | #include <linux/rtnetlink.h> | |
57 | #include <linux/if.h> | |
58 | #include <linux/if_arp.h> | |
59 | #include <linux/if_ether.h> | |
60 | #include <linux/if_tun.h> | |
61 | #include <linux/crc32.h> | |
d647a591 | 62 | #include <linux/nsproxy.h> |
f43798c2 | 63 | #include <linux/virtio_net.h> |
881d966b | 64 | #include <net/net_namespace.h> |
79d17604 | 65 | #include <net/netns/generic.h> |
1da177e4 LT |
66 | |
67 | #include <asm/system.h> | |
68 | #include <asm/uaccess.h> | |
69 | ||
14daa021 RR |
70 | /* Uncomment to enable debugging */ |
71 | /* #define TUN_DEBUG 1 */ | |
72 | ||
1da177e4 LT |
73 | #ifdef TUN_DEBUG |
74 | static int debug; | |
14daa021 RR |
75 | |
76 | #define DBG if(tun->debug)printk | |
77 | #define DBG1 if(debug==2)printk | |
78 | #else | |
79 | #define DBG( a... ) | |
80 | #define DBG1( a... ) | |
81 | #endif | |
82 | ||
f271b2cc MK |
83 | #define FLT_EXACT_COUNT 8 |
84 | struct tap_filter { | |
85 | unsigned int count; /* Number of addrs. Zero means disabled */ | |
86 | u32 mask[2]; /* Mask of the hashed addrs */ | |
87 | unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN]; | |
88 | }; | |
89 | ||
631ab46b EB |
90 | struct tun_file { |
91 | struct tun_struct *tun; | |
36b50bab | 92 | struct net *net; |
631ab46b EB |
93 | }; |
94 | ||
14daa021 | 95 | struct tun_struct { |
631ab46b | 96 | struct tun_file *tfile; |
f271b2cc | 97 | unsigned int flags; |
14daa021 RR |
98 | uid_t owner; |
99 | gid_t group; | |
100 | ||
101 | wait_queue_head_t read_wait; | |
102 | struct sk_buff_head readq; | |
103 | ||
104 | struct net_device *dev; | |
f271b2cc | 105 | struct fasync_struct *fasync; |
14daa021 | 106 | |
f271b2cc | 107 | struct tap_filter txflt; |
14daa021 RR |
108 | |
109 | #ifdef TUN_DEBUG | |
110 | int debug; | |
1da177e4 | 111 | #endif |
14daa021 | 112 | }; |
1da177e4 | 113 | |
a7385ba2 EB |
114 | static int tun_attach(struct tun_struct *tun, struct file *file) |
115 | { | |
631ab46b | 116 | struct tun_file *tfile = file->private_data; |
a7385ba2 EB |
117 | const struct cred *cred = current_cred(); |
118 | ||
119 | ASSERT_RTNL(); | |
120 | ||
631ab46b | 121 | if (tfile->tun) |
a7385ba2 EB |
122 | return -EINVAL; |
123 | ||
631ab46b | 124 | if (tun->tfile) |
a7385ba2 EB |
125 | return -EBUSY; |
126 | ||
127 | /* Check permissions */ | |
128 | if (((tun->owner != -1 && cred->euid != tun->owner) || | |
129 | (tun->group != -1 && cred->egid != tun->group)) && | |
130 | !capable(CAP_NET_ADMIN)) | |
131 | return -EPERM; | |
132 | ||
631ab46b EB |
133 | tfile->tun = tun; |
134 | tun->tfile = tfile; | |
a7385ba2 EB |
135 | |
136 | return 0; | |
137 | } | |
138 | ||
631ab46b EB |
139 | static void __tun_detach(struct tun_struct *tun) |
140 | { | |
141 | struct tun_file *tfile = tun->tfile; | |
142 | ||
143 | /* Detach from net device */ | |
144 | tfile->tun = NULL; | |
145 | tun->tfile = NULL; | |
631ab46b EB |
146 | |
147 | /* Drop read queue */ | |
148 | skb_queue_purge(&tun->readq); | |
149 | } | |
150 | ||
151 | static struct tun_struct *__tun_get(struct tun_file *tfile) | |
152 | { | |
153 | return tfile->tun; | |
154 | } | |
155 | ||
156 | static struct tun_struct *tun_get(struct file *file) | |
157 | { | |
158 | return __tun_get(file->private_data); | |
159 | } | |
160 | ||
161 | static void tun_put(struct tun_struct *tun) | |
162 | { | |
163 | /* Noop for now */ | |
164 | } | |
165 | ||
f271b2cc MK |
166 | /* TAP filterting */ |
167 | static void addr_hash_set(u32 *mask, const u8 *addr) | |
168 | { | |
169 | int n = ether_crc(ETH_ALEN, addr) >> 26; | |
170 | mask[n >> 5] |= (1 << (n & 31)); | |
171 | } | |
172 | ||
173 | static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) | |
174 | { | |
175 | int n = ether_crc(ETH_ALEN, addr) >> 26; | |
176 | return mask[n >> 5] & (1 << (n & 31)); | |
177 | } | |
178 | ||
179 | static int update_filter(struct tap_filter *filter, void __user *arg) | |
180 | { | |
181 | struct { u8 u[ETH_ALEN]; } *addr; | |
182 | struct tun_filter uf; | |
183 | int err, alen, n, nexact; | |
184 | ||
185 | if (copy_from_user(&uf, arg, sizeof(uf))) | |
186 | return -EFAULT; | |
187 | ||
188 | if (!uf.count) { | |
189 | /* Disabled */ | |
190 | filter->count = 0; | |
191 | return 0; | |
192 | } | |
193 | ||
194 | alen = ETH_ALEN * uf.count; | |
195 | addr = kmalloc(alen, GFP_KERNEL); | |
196 | if (!addr) | |
197 | return -ENOMEM; | |
198 | ||
199 | if (copy_from_user(addr, arg + sizeof(uf), alen)) { | |
200 | err = -EFAULT; | |
201 | goto done; | |
202 | } | |
203 | ||
204 | /* The filter is updated without holding any locks. Which is | |
205 | * perfectly safe. We disable it first and in the worst | |
206 | * case we'll accept a few undesired packets. */ | |
207 | filter->count = 0; | |
208 | wmb(); | |
209 | ||
210 | /* Use first set of addresses as an exact filter */ | |
211 | for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) | |
212 | memcpy(filter->addr[n], addr[n].u, ETH_ALEN); | |
213 | ||
214 | nexact = n; | |
215 | ||
216 | /* The rest is hashed */ | |
217 | memset(filter->mask, 0, sizeof(filter->mask)); | |
218 | for (; n < uf.count; n++) | |
219 | addr_hash_set(filter->mask, addr[n].u); | |
220 | ||
221 | /* For ALLMULTI just set the mask to all ones. | |
222 | * This overrides the mask populated above. */ | |
223 | if ((uf.flags & TUN_FLT_ALLMULTI)) | |
224 | memset(filter->mask, ~0, sizeof(filter->mask)); | |
225 | ||
226 | /* Now enable the filter */ | |
227 | wmb(); | |
228 | filter->count = nexact; | |
229 | ||
230 | /* Return the number of exact filters */ | |
231 | err = nexact; | |
232 | ||
233 | done: | |
234 | kfree(addr); | |
235 | return err; | |
236 | } | |
237 | ||
238 | /* Returns: 0 - drop, !=0 - accept */ | |
239 | static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) | |
240 | { | |
241 | /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect | |
242 | * at this point. */ | |
243 | struct ethhdr *eh = (struct ethhdr *) skb->data; | |
244 | int i; | |
245 | ||
246 | /* Exact match */ | |
247 | for (i = 0; i < filter->count; i++) | |
248 | if (!compare_ether_addr(eh->h_dest, filter->addr[i])) | |
249 | return 1; | |
250 | ||
251 | /* Inexact match (multicast only) */ | |
252 | if (is_multicast_ether_addr(eh->h_dest)) | |
253 | return addr_hash_test(filter->mask, eh->h_dest); | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | /* | |
259 | * Checks whether the packet is accepted or not. | |
260 | * Returns: 0 - drop, !=0 - accept | |
261 | */ | |
262 | static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) | |
263 | { | |
264 | if (!filter->count) | |
265 | return 1; | |
266 | ||
267 | return run_filter(filter, skb); | |
268 | } | |
269 | ||
1da177e4 LT |
270 | /* Network device part of the driver */ |
271 | ||
7282d491 | 272 | static const struct ethtool_ops tun_ethtool_ops; |
1da177e4 LT |
273 | |
274 | /* Net device open. */ | |
275 | static int tun_net_open(struct net_device *dev) | |
276 | { | |
277 | netif_start_queue(dev); | |
278 | return 0; | |
279 | } | |
280 | ||
281 | /* Net device close. */ | |
282 | static int tun_net_close(struct net_device *dev) | |
283 | { | |
284 | netif_stop_queue(dev); | |
285 | return 0; | |
286 | } | |
287 | ||
288 | /* Net device start xmit */ | |
289 | static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev) | |
290 | { | |
291 | struct tun_struct *tun = netdev_priv(dev); | |
292 | ||
293 | DBG(KERN_INFO "%s: tun_net_xmit %d\n", tun->dev->name, skb->len); | |
294 | ||
295 | /* Drop packet if interface is not attached */ | |
631ab46b | 296 | if (!tun->tfile) |
1da177e4 LT |
297 | goto drop; |
298 | ||
f271b2cc MK |
299 | /* Drop if the filter does not like it. |
300 | * This is a noop if the filter is disabled. | |
301 | * Filter can be enabled only for the TAP devices. */ | |
302 | if (!check_filter(&tun->txflt, skb)) | |
303 | goto drop; | |
304 | ||
1da177e4 LT |
305 | if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) { |
306 | if (!(tun->flags & TUN_ONE_QUEUE)) { | |
307 | /* Normal queueing mode. */ | |
308 | /* Packet scheduler handles dropping of further packets. */ | |
309 | netif_stop_queue(dev); | |
310 | ||
311 | /* We won't see all dropped packets individually, so overrun | |
312 | * error is more appropriate. */ | |
09f75cd7 | 313 | dev->stats.tx_fifo_errors++; |
1da177e4 LT |
314 | } else { |
315 | /* Single queue mode. | |
316 | * Driver handles dropping of all packets itself. */ | |
317 | goto drop; | |
318 | } | |
319 | } | |
320 | ||
f271b2cc | 321 | /* Enqueue packet */ |
1da177e4 LT |
322 | skb_queue_tail(&tun->readq, skb); |
323 | dev->trans_start = jiffies; | |
324 | ||
325 | /* Notify and wake up reader process */ | |
326 | if (tun->flags & TUN_FASYNC) | |
327 | kill_fasync(&tun->fasync, SIGIO, POLL_IN); | |
328 | wake_up_interruptible(&tun->read_wait); | |
329 | return 0; | |
330 | ||
331 | drop: | |
09f75cd7 | 332 | dev->stats.tx_dropped++; |
1da177e4 LT |
333 | kfree_skb(skb); |
334 | return 0; | |
335 | } | |
336 | ||
f271b2cc | 337 | static void tun_net_mclist(struct net_device *dev) |
1da177e4 | 338 | { |
f271b2cc MK |
339 | /* |
340 | * This callback is supposed to deal with mc filter in | |
341 | * _rx_ path and has nothing to do with the _tx_ path. | |
342 | * In rx path we always accept everything userspace gives us. | |
343 | */ | |
344 | return; | |
1da177e4 LT |
345 | } |
346 | ||
4885a504 ES |
347 | #define MIN_MTU 68 |
348 | #define MAX_MTU 65535 | |
349 | ||
350 | static int | |
351 | tun_net_change_mtu(struct net_device *dev, int new_mtu) | |
352 | { | |
353 | if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU) | |
354 | return -EINVAL; | |
355 | dev->mtu = new_mtu; | |
356 | return 0; | |
357 | } | |
358 | ||
758e43b7 SH |
359 | static const struct net_device_ops tun_netdev_ops = { |
360 | .ndo_open = tun_net_open, | |
361 | .ndo_stop = tun_net_close, | |
00829823 | 362 | .ndo_start_xmit = tun_net_xmit, |
758e43b7 | 363 | .ndo_change_mtu = tun_net_change_mtu, |
758e43b7 SH |
364 | }; |
365 | ||
366 | static const struct net_device_ops tap_netdev_ops = { | |
367 | .ndo_open = tun_net_open, | |
368 | .ndo_stop = tun_net_close, | |
00829823 | 369 | .ndo_start_xmit = tun_net_xmit, |
758e43b7 SH |
370 | .ndo_change_mtu = tun_net_change_mtu, |
371 | .ndo_set_multicast_list = tun_net_mclist, | |
372 | .ndo_set_mac_address = eth_mac_addr, | |
373 | .ndo_validate_addr = eth_validate_addr, | |
374 | }; | |
375 | ||
1da177e4 LT |
376 | /* Initialize net device. */ |
377 | static void tun_net_init(struct net_device *dev) | |
378 | { | |
379 | struct tun_struct *tun = netdev_priv(dev); | |
6aa20a22 | 380 | |
1da177e4 LT |
381 | switch (tun->flags & TUN_TYPE_MASK) { |
382 | case TUN_TUN_DEV: | |
758e43b7 SH |
383 | dev->netdev_ops = &tun_netdev_ops; |
384 | ||
1da177e4 LT |
385 | /* Point-to-Point TUN Device */ |
386 | dev->hard_header_len = 0; | |
387 | dev->addr_len = 0; | |
388 | dev->mtu = 1500; | |
389 | ||
390 | /* Zero header length */ | |
6aa20a22 | 391 | dev->type = ARPHRD_NONE; |
1da177e4 LT |
392 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
393 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ | |
394 | break; | |
395 | ||
396 | case TUN_TAP_DEV: | |
7a0a9608 | 397 | dev->netdev_ops = &tap_netdev_ops; |
1da177e4 | 398 | /* Ethernet TAP Device */ |
1da177e4 | 399 | ether_setup(dev); |
36226a8d | 400 | |
f271b2cc | 401 | random_ether_addr(dev->dev_addr); |
36226a8d | 402 | |
1da177e4 LT |
403 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ |
404 | break; | |
405 | } | |
406 | } | |
407 | ||
408 | /* Character device part */ | |
409 | ||
410 | /* Poll */ | |
411 | static unsigned int tun_chr_poll(struct file *file, poll_table * wait) | |
6aa20a22 | 412 | { |
631ab46b | 413 | struct tun_struct *tun = tun_get(file); |
1da177e4 LT |
414 | unsigned int mask = POLLOUT | POLLWRNORM; |
415 | ||
416 | if (!tun) | |
eac9e902 | 417 | return POLLERR; |
1da177e4 LT |
418 | |
419 | DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name); | |
420 | ||
421 | poll_wait(file, &tun->read_wait, wait); | |
6aa20a22 | 422 | |
b03efcfb | 423 | if (!skb_queue_empty(&tun->readq)) |
1da177e4 LT |
424 | mask |= POLLIN | POLLRDNORM; |
425 | ||
631ab46b | 426 | tun_put(tun); |
1da177e4 LT |
427 | return mask; |
428 | } | |
429 | ||
f42157cb RR |
430 | /* prepad is the amount to reserve at front. len is length after that. |
431 | * linear is a hint as to how much to copy (usually headers). */ | |
432 | static struct sk_buff *tun_alloc_skb(size_t prepad, size_t len, size_t linear, | |
433 | gfp_t gfp) | |
434 | { | |
435 | struct sk_buff *skb; | |
436 | unsigned int i; | |
437 | ||
438 | skb = alloc_skb(prepad + len, gfp|__GFP_NOWARN); | |
439 | if (skb) { | |
440 | skb_reserve(skb, prepad); | |
441 | skb_put(skb, len); | |
442 | return skb; | |
443 | } | |
444 | ||
445 | /* Under a page? Don't bother with paged skb. */ | |
446 | if (prepad + len < PAGE_SIZE) | |
447 | return NULL; | |
448 | ||
449 | /* Start with a normal skb, and add pages. */ | |
450 | skb = alloc_skb(prepad + linear, gfp); | |
451 | if (!skb) | |
452 | return NULL; | |
453 | ||
454 | skb_reserve(skb, prepad); | |
455 | skb_put(skb, linear); | |
456 | ||
457 | len -= linear; | |
458 | ||
459 | for (i = 0; i < MAX_SKB_FRAGS; i++) { | |
460 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; | |
461 | ||
462 | f->page = alloc_page(gfp|__GFP_ZERO); | |
463 | if (!f->page) | |
464 | break; | |
465 | ||
466 | f->page_offset = 0; | |
467 | f->size = PAGE_SIZE; | |
468 | ||
469 | skb->data_len += PAGE_SIZE; | |
470 | skb->len += PAGE_SIZE; | |
471 | skb->truesize += PAGE_SIZE; | |
472 | skb_shinfo(skb)->nr_frags++; | |
473 | ||
474 | if (len < PAGE_SIZE) { | |
475 | len = 0; | |
476 | break; | |
477 | } | |
478 | len -= PAGE_SIZE; | |
479 | } | |
480 | ||
481 | /* Too large, or alloc fail? */ | |
482 | if (unlikely(len)) { | |
483 | kfree_skb(skb); | |
484 | skb = NULL; | |
485 | } | |
486 | ||
487 | return skb; | |
488 | } | |
489 | ||
1da177e4 LT |
490 | /* Get packet from user space buffer */ |
491 | static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, size_t count) | |
492 | { | |
493 | struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) }; | |
494 | struct sk_buff *skb; | |
495 | size_t len = count, align = 0; | |
f43798c2 | 496 | struct virtio_net_hdr gso = { 0 }; |
1da177e4 LT |
497 | |
498 | if (!(tun->flags & TUN_NO_PI)) { | |
499 | if ((len -= sizeof(pi)) > count) | |
500 | return -EINVAL; | |
501 | ||
502 | if(memcpy_fromiovec((void *)&pi, iv, sizeof(pi))) | |
503 | return -EFAULT; | |
504 | } | |
505 | ||
f43798c2 RR |
506 | if (tun->flags & TUN_VNET_HDR) { |
507 | if ((len -= sizeof(gso)) > count) | |
508 | return -EINVAL; | |
509 | ||
510 | if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso))) | |
511 | return -EFAULT; | |
512 | ||
513 | if (gso.hdr_len > len) | |
514 | return -EINVAL; | |
515 | } | |
516 | ||
e01bf1c8 | 517 | if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { |
1da177e4 | 518 | align = NET_IP_ALIGN; |
e01bf1c8 RR |
519 | if (unlikely(len < ETH_HLEN)) |
520 | return -EINVAL; | |
521 | } | |
6aa20a22 | 522 | |
f42157cb | 523 | if (!(skb = tun_alloc_skb(align, len, gso.hdr_len, GFP_KERNEL))) { |
09f75cd7 | 524 | tun->dev->stats.rx_dropped++; |
1da177e4 LT |
525 | return -ENOMEM; |
526 | } | |
527 | ||
f42157cb | 528 | if (skb_copy_datagram_from_iovec(skb, 0, iv, len)) { |
09f75cd7 | 529 | tun->dev->stats.rx_dropped++; |
8f22757e | 530 | kfree_skb(skb); |
1da177e4 | 531 | return -EFAULT; |
8f22757e | 532 | } |
1da177e4 | 533 | |
f43798c2 RR |
534 | if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
535 | if (!skb_partial_csum_set(skb, gso.csum_start, | |
536 | gso.csum_offset)) { | |
537 | tun->dev->stats.rx_frame_errors++; | |
538 | kfree_skb(skb); | |
539 | return -EINVAL; | |
540 | } | |
541 | } else if (tun->flags & TUN_NOCHECKSUM) | |
542 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
543 | ||
1da177e4 LT |
544 | switch (tun->flags & TUN_TYPE_MASK) { |
545 | case TUN_TUN_DEV: | |
f09f7ee2 AWC |
546 | if (tun->flags & TUN_NO_PI) { |
547 | switch (skb->data[0] & 0xf0) { | |
548 | case 0x40: | |
549 | pi.proto = htons(ETH_P_IP); | |
550 | break; | |
551 | case 0x60: | |
552 | pi.proto = htons(ETH_P_IPV6); | |
553 | break; | |
554 | default: | |
555 | tun->dev->stats.rx_dropped++; | |
556 | kfree_skb(skb); | |
557 | return -EINVAL; | |
558 | } | |
559 | } | |
560 | ||
459a98ed | 561 | skb_reset_mac_header(skb); |
1da177e4 | 562 | skb->protocol = pi.proto; |
4c13eb66 | 563 | skb->dev = tun->dev; |
1da177e4 LT |
564 | break; |
565 | case TUN_TAP_DEV: | |
566 | skb->protocol = eth_type_trans(skb, tun->dev); | |
567 | break; | |
568 | }; | |
569 | ||
f43798c2 RR |
570 | if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
571 | pr_debug("GSO!\n"); | |
572 | switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { | |
573 | case VIRTIO_NET_HDR_GSO_TCPV4: | |
574 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; | |
575 | break; | |
576 | case VIRTIO_NET_HDR_GSO_TCPV6: | |
577 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; | |
578 | break; | |
579 | default: | |
580 | tun->dev->stats.rx_frame_errors++; | |
581 | kfree_skb(skb); | |
582 | return -EINVAL; | |
583 | } | |
584 | ||
585 | if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN) | |
586 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; | |
587 | ||
588 | skb_shinfo(skb)->gso_size = gso.gso_size; | |
589 | if (skb_shinfo(skb)->gso_size == 0) { | |
590 | tun->dev->stats.rx_frame_errors++; | |
591 | kfree_skb(skb); | |
592 | return -EINVAL; | |
593 | } | |
594 | ||
595 | /* Header must be checked, and gso_segs computed. */ | |
596 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
597 | skb_shinfo(skb)->gso_segs = 0; | |
598 | } | |
6aa20a22 | 599 | |
1da177e4 | 600 | netif_rx_ni(skb); |
6aa20a22 | 601 | |
09f75cd7 JG |
602 | tun->dev->stats.rx_packets++; |
603 | tun->dev->stats.rx_bytes += len; | |
1da177e4 LT |
604 | |
605 | return count; | |
6aa20a22 | 606 | } |
1da177e4 | 607 | |
ee0b3e67 BP |
608 | static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, |
609 | unsigned long count, loff_t pos) | |
1da177e4 | 610 | { |
631ab46b EB |
611 | struct tun_struct *tun = tun_get(iocb->ki_filp); |
612 | ssize_t result; | |
1da177e4 LT |
613 | |
614 | if (!tun) | |
615 | return -EBADFD; | |
616 | ||
617 | DBG(KERN_INFO "%s: tun_chr_write %ld\n", tun->dev->name, count); | |
618 | ||
631ab46b EB |
619 | result = tun_get_user(tun, (struct iovec *) iv, iov_length(iv, count)); |
620 | ||
621 | tun_put(tun); | |
622 | return result; | |
1da177e4 LT |
623 | } |
624 | ||
1da177e4 LT |
625 | /* Put packet to the user space buffer */ |
626 | static __inline__ ssize_t tun_put_user(struct tun_struct *tun, | |
627 | struct sk_buff *skb, | |
628 | struct iovec *iv, int len) | |
629 | { | |
630 | struct tun_pi pi = { 0, skb->protocol }; | |
631 | ssize_t total = 0; | |
632 | ||
633 | if (!(tun->flags & TUN_NO_PI)) { | |
634 | if ((len -= sizeof(pi)) < 0) | |
635 | return -EINVAL; | |
636 | ||
637 | if (len < skb->len) { | |
638 | /* Packet will be striped */ | |
639 | pi.flags |= TUN_PKT_STRIP; | |
640 | } | |
6aa20a22 | 641 | |
1da177e4 LT |
642 | if (memcpy_toiovec(iv, (void *) &pi, sizeof(pi))) |
643 | return -EFAULT; | |
644 | total += sizeof(pi); | |
6aa20a22 | 645 | } |
1da177e4 | 646 | |
f43798c2 RR |
647 | if (tun->flags & TUN_VNET_HDR) { |
648 | struct virtio_net_hdr gso = { 0 }; /* no info leak */ | |
649 | if ((len -= sizeof(gso)) < 0) | |
650 | return -EINVAL; | |
651 | ||
652 | if (skb_is_gso(skb)) { | |
653 | struct skb_shared_info *sinfo = skb_shinfo(skb); | |
654 | ||
655 | /* This is a hint as to how much should be linear. */ | |
656 | gso.hdr_len = skb_headlen(skb); | |
657 | gso.gso_size = sinfo->gso_size; | |
658 | if (sinfo->gso_type & SKB_GSO_TCPV4) | |
659 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; | |
660 | else if (sinfo->gso_type & SKB_GSO_TCPV6) | |
661 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; | |
662 | else | |
663 | BUG(); | |
664 | if (sinfo->gso_type & SKB_GSO_TCP_ECN) | |
665 | gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN; | |
666 | } else | |
667 | gso.gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
668 | ||
669 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
670 | gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; | |
671 | gso.csum_start = skb->csum_start - skb_headroom(skb); | |
672 | gso.csum_offset = skb->csum_offset; | |
673 | } /* else everything is zero */ | |
674 | ||
675 | if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso)))) | |
676 | return -EFAULT; | |
677 | total += sizeof(gso); | |
678 | } | |
679 | ||
1da177e4 LT |
680 | len = min_t(int, skb->len, len); |
681 | ||
682 | skb_copy_datagram_iovec(skb, 0, iv, len); | |
683 | total += len; | |
684 | ||
09f75cd7 JG |
685 | tun->dev->stats.tx_packets++; |
686 | tun->dev->stats.tx_bytes += len; | |
1da177e4 LT |
687 | |
688 | return total; | |
689 | } | |
690 | ||
ee0b3e67 BP |
691 | static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, |
692 | unsigned long count, loff_t pos) | |
1da177e4 | 693 | { |
ee0b3e67 | 694 | struct file *file = iocb->ki_filp; |
631ab46b | 695 | struct tun_struct *tun = tun_get(file); |
1da177e4 LT |
696 | DECLARE_WAITQUEUE(wait, current); |
697 | struct sk_buff *skb; | |
698 | ssize_t len, ret = 0; | |
699 | ||
700 | if (!tun) | |
701 | return -EBADFD; | |
702 | ||
703 | DBG(KERN_INFO "%s: tun_chr_read\n", tun->dev->name); | |
704 | ||
52427c9d | 705 | len = iov_length(iv, count); |
631ab46b EB |
706 | if (len < 0) { |
707 | ret = -EINVAL; | |
708 | goto out; | |
709 | } | |
1da177e4 LT |
710 | |
711 | add_wait_queue(&tun->read_wait, &wait); | |
712 | while (len) { | |
1da177e4 LT |
713 | current->state = TASK_INTERRUPTIBLE; |
714 | ||
715 | /* Read frames from the queue */ | |
716 | if (!(skb=skb_dequeue(&tun->readq))) { | |
717 | if (file->f_flags & O_NONBLOCK) { | |
718 | ret = -EAGAIN; | |
719 | break; | |
720 | } | |
721 | if (signal_pending(current)) { | |
722 | ret = -ERESTARTSYS; | |
723 | break; | |
724 | } | |
725 | ||
726 | /* Nothing to read, let's sleep */ | |
727 | schedule(); | |
728 | continue; | |
729 | } | |
730 | netif_wake_queue(tun->dev); | |
731 | ||
f271b2cc MK |
732 | ret = tun_put_user(tun, skb, (struct iovec *) iv, len); |
733 | kfree_skb(skb); | |
734 | break; | |
1da177e4 LT |
735 | } |
736 | ||
737 | current->state = TASK_RUNNING; | |
738 | remove_wait_queue(&tun->read_wait, &wait); | |
739 | ||
631ab46b EB |
740 | out: |
741 | tun_put(tun); | |
1da177e4 LT |
742 | return ret; |
743 | } | |
744 | ||
1da177e4 LT |
745 | static void tun_setup(struct net_device *dev) |
746 | { | |
747 | struct tun_struct *tun = netdev_priv(dev); | |
748 | ||
749 | skb_queue_head_init(&tun->readq); | |
750 | init_waitqueue_head(&tun->read_wait); | |
751 | ||
752 | tun->owner = -1; | |
8c644623 | 753 | tun->group = -1; |
1da177e4 | 754 | |
1da177e4 LT |
755 | dev->ethtool_ops = &tun_ethtool_ops; |
756 | dev->destructor = free_netdev; | |
fc54c658 | 757 | dev->features |= NETIF_F_NETNS_LOCAL; |
1da177e4 LT |
758 | } |
759 | ||
d647a591 | 760 | static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) |
1da177e4 LT |
761 | { |
762 | struct tun_struct *tun; | |
763 | struct net_device *dev; | |
764 | int err; | |
765 | ||
74a3e5a7 EB |
766 | dev = __dev_get_by_name(net, ifr->ifr_name); |
767 | if (dev) { | |
768 | if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops) | |
769 | tun = netdev_priv(dev); | |
770 | else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops) | |
771 | tun = netdev_priv(dev); | |
772 | else | |
773 | return -EINVAL; | |
774 | ||
a7385ba2 EB |
775 | err = tun_attach(tun, file); |
776 | if (err < 0) | |
777 | return err; | |
6aa20a22 | 778 | } |
1da177e4 LT |
779 | else { |
780 | char *name; | |
781 | unsigned long flags = 0; | |
782 | ||
783 | err = -EINVAL; | |
784 | ||
ca6bb5d7 DW |
785 | if (!capable(CAP_NET_ADMIN)) |
786 | return -EPERM; | |
787 | ||
1da177e4 LT |
788 | /* Set dev type */ |
789 | if (ifr->ifr_flags & IFF_TUN) { | |
790 | /* TUN device */ | |
791 | flags |= TUN_TUN_DEV; | |
792 | name = "tun%d"; | |
793 | } else if (ifr->ifr_flags & IFF_TAP) { | |
794 | /* TAP device */ | |
795 | flags |= TUN_TAP_DEV; | |
796 | name = "tap%d"; | |
6aa20a22 | 797 | } else |
1da177e4 | 798 | goto failed; |
6aa20a22 | 799 | |
1da177e4 LT |
800 | if (*ifr->ifr_name) |
801 | name = ifr->ifr_name; | |
802 | ||
803 | dev = alloc_netdev(sizeof(struct tun_struct), name, | |
804 | tun_setup); | |
805 | if (!dev) | |
806 | return -ENOMEM; | |
807 | ||
fc54c658 | 808 | dev_net_set(dev, net); |
758e43b7 | 809 | |
1da177e4 LT |
810 | tun = netdev_priv(dev); |
811 | tun->dev = dev; | |
812 | tun->flags = flags; | |
f271b2cc | 813 | tun->txflt.count = 0; |
1da177e4 LT |
814 | |
815 | tun_net_init(dev); | |
816 | ||
817 | if (strchr(dev->name, '%')) { | |
818 | err = dev_alloc_name(dev, dev->name); | |
819 | if (err < 0) | |
820 | goto err_free_dev; | |
821 | } | |
822 | ||
823 | err = register_netdevice(tun->dev); | |
824 | if (err < 0) | |
825 | goto err_free_dev; | |
a7385ba2 EB |
826 | |
827 | err = tun_attach(tun, file); | |
828 | if (err < 0) | |
829 | goto err_free_dev; | |
1da177e4 LT |
830 | } |
831 | ||
832 | DBG(KERN_INFO "%s: tun_set_iff\n", tun->dev->name); | |
833 | ||
834 | if (ifr->ifr_flags & IFF_NO_PI) | |
835 | tun->flags |= TUN_NO_PI; | |
a26af1e0 NF |
836 | else |
837 | tun->flags &= ~TUN_NO_PI; | |
1da177e4 LT |
838 | |
839 | if (ifr->ifr_flags & IFF_ONE_QUEUE) | |
840 | tun->flags |= TUN_ONE_QUEUE; | |
a26af1e0 NF |
841 | else |
842 | tun->flags &= ~TUN_ONE_QUEUE; | |
1da177e4 | 843 | |
f43798c2 RR |
844 | if (ifr->ifr_flags & IFF_VNET_HDR) |
845 | tun->flags |= TUN_VNET_HDR; | |
846 | else | |
847 | tun->flags &= ~TUN_VNET_HDR; | |
848 | ||
e35259a9 MK |
849 | /* Make sure persistent devices do not get stuck in |
850 | * xoff state. | |
851 | */ | |
852 | if (netif_running(tun->dev)) | |
853 | netif_wake_queue(tun->dev); | |
854 | ||
1da177e4 LT |
855 | strcpy(ifr->ifr_name, tun->dev->name); |
856 | return 0; | |
857 | ||
858 | err_free_dev: | |
859 | free_netdev(dev); | |
860 | failed: | |
861 | return err; | |
862 | } | |
863 | ||
e3b99556 MM |
864 | static int tun_get_iff(struct net *net, struct file *file, struct ifreq *ifr) |
865 | { | |
631ab46b | 866 | struct tun_struct *tun = tun_get(file); |
e3b99556 MM |
867 | |
868 | if (!tun) | |
869 | return -EBADFD; | |
870 | ||
871 | DBG(KERN_INFO "%s: tun_get_iff\n", tun->dev->name); | |
872 | ||
873 | strcpy(ifr->ifr_name, tun->dev->name); | |
874 | ||
875 | ifr->ifr_flags = 0; | |
876 | ||
877 | if (ifr->ifr_flags & TUN_TUN_DEV) | |
878 | ifr->ifr_flags |= IFF_TUN; | |
879 | else | |
880 | ifr->ifr_flags |= IFF_TAP; | |
881 | ||
882 | if (tun->flags & TUN_NO_PI) | |
883 | ifr->ifr_flags |= IFF_NO_PI; | |
884 | ||
885 | if (tun->flags & TUN_ONE_QUEUE) | |
886 | ifr->ifr_flags |= IFF_ONE_QUEUE; | |
887 | ||
888 | if (tun->flags & TUN_VNET_HDR) | |
889 | ifr->ifr_flags |= IFF_VNET_HDR; | |
890 | ||
631ab46b | 891 | tun_put(tun); |
e3b99556 MM |
892 | return 0; |
893 | } | |
894 | ||
5228ddc9 RR |
895 | /* This is like a cut-down ethtool ops, except done via tun fd so no |
896 | * privs required. */ | |
897 | static int set_offload(struct net_device *dev, unsigned long arg) | |
898 | { | |
899 | unsigned int old_features, features; | |
900 | ||
901 | old_features = dev->features; | |
902 | /* Unset features, set them as we chew on the arg. */ | |
903 | features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST | |
904 | |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6)); | |
905 | ||
906 | if (arg & TUN_F_CSUM) { | |
907 | features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; | |
908 | arg &= ~TUN_F_CSUM; | |
909 | ||
910 | if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { | |
911 | if (arg & TUN_F_TSO_ECN) { | |
912 | features |= NETIF_F_TSO_ECN; | |
913 | arg &= ~TUN_F_TSO_ECN; | |
914 | } | |
915 | if (arg & TUN_F_TSO4) | |
916 | features |= NETIF_F_TSO; | |
917 | if (arg & TUN_F_TSO6) | |
918 | features |= NETIF_F_TSO6; | |
919 | arg &= ~(TUN_F_TSO4|TUN_F_TSO6); | |
920 | } | |
921 | } | |
922 | ||
923 | /* This gives the user a way to test for new features in future by | |
924 | * trying to set them. */ | |
925 | if (arg) | |
926 | return -EINVAL; | |
927 | ||
928 | dev->features = features; | |
929 | if (old_features != dev->features) | |
930 | netdev_features_change(dev); | |
931 | ||
932 | return 0; | |
933 | } | |
934 | ||
6aa20a22 | 935 | static int tun_chr_ioctl(struct inode *inode, struct file *file, |
1da177e4 LT |
936 | unsigned int cmd, unsigned long arg) |
937 | { | |
36b50bab | 938 | struct tun_file *tfile = file->private_data; |
631ab46b | 939 | struct tun_struct *tun; |
1da177e4 LT |
940 | void __user* argp = (void __user*)arg; |
941 | struct ifreq ifr; | |
f271b2cc | 942 | int ret; |
1da177e4 LT |
943 | |
944 | if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) | |
945 | if (copy_from_user(&ifr, argp, sizeof ifr)) | |
946 | return -EFAULT; | |
947 | ||
631ab46b EB |
948 | if (cmd == TUNGETFEATURES) { |
949 | /* Currently this just means: "what IFF flags are valid?". | |
950 | * This is needed because we never checked for invalid flags on | |
951 | * TUNSETIFF. */ | |
952 | return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | | |
953 | IFF_VNET_HDR, | |
954 | (unsigned int __user*)argp); | |
955 | } | |
956 | ||
36b50bab | 957 | tun = __tun_get(tfile); |
1da177e4 LT |
958 | if (cmd == TUNSETIFF && !tun) { |
959 | int err; | |
960 | ||
961 | ifr.ifr_name[IFNAMSIZ-1] = '\0'; | |
962 | ||
963 | rtnl_lock(); | |
36b50bab | 964 | err = tun_set_iff(tfile->net, file, &ifr); |
1da177e4 LT |
965 | rtnl_unlock(); |
966 | ||
967 | if (err) | |
968 | return err; | |
969 | ||
970 | if (copy_to_user(argp, &ifr, sizeof(ifr))) | |
971 | return -EFAULT; | |
972 | return 0; | |
973 | } | |
974 | ||
07240fd0 | 975 | |
1da177e4 LT |
976 | if (!tun) |
977 | return -EBADFD; | |
978 | ||
979 | DBG(KERN_INFO "%s: tun_chr_ioctl cmd %d\n", tun->dev->name, cmd); | |
980 | ||
631ab46b | 981 | ret = 0; |
1da177e4 | 982 | switch (cmd) { |
e3b99556 MM |
983 | case TUNGETIFF: |
984 | ret = tun_get_iff(current->nsproxy->net_ns, file, &ifr); | |
985 | if (ret) | |
631ab46b | 986 | break; |
e3b99556 MM |
987 | |
988 | if (copy_to_user(argp, &ifr, sizeof(ifr))) | |
631ab46b | 989 | ret = -EFAULT; |
e3b99556 MM |
990 | break; |
991 | ||
1da177e4 LT |
992 | case TUNSETNOCSUM: |
993 | /* Disable/Enable checksum */ | |
994 | if (arg) | |
995 | tun->flags |= TUN_NOCHECKSUM; | |
996 | else | |
997 | tun->flags &= ~TUN_NOCHECKSUM; | |
998 | ||
999 | DBG(KERN_INFO "%s: checksum %s\n", | |
1000 | tun->dev->name, arg ? "disabled" : "enabled"); | |
1001 | break; | |
1002 | ||
1003 | case TUNSETPERSIST: | |
1004 | /* Disable/Enable persist mode */ | |
1005 | if (arg) | |
1006 | tun->flags |= TUN_PERSIST; | |
1007 | else | |
1008 | tun->flags &= ~TUN_PERSIST; | |
1009 | ||
1010 | DBG(KERN_INFO "%s: persist %s\n", | |
c6e991de | 1011 | tun->dev->name, arg ? "enabled" : "disabled"); |
1da177e4 LT |
1012 | break; |
1013 | ||
1014 | case TUNSETOWNER: | |
1015 | /* Set owner of the device */ | |
1016 | tun->owner = (uid_t) arg; | |
1017 | ||
1018 | DBG(KERN_INFO "%s: owner set to %d\n", tun->dev->name, tun->owner); | |
1019 | break; | |
1020 | ||
8c644623 GG |
1021 | case TUNSETGROUP: |
1022 | /* Set group of the device */ | |
1023 | tun->group= (gid_t) arg; | |
1024 | ||
1025 | DBG(KERN_INFO "%s: group set to %d\n", tun->dev->name, tun->group); | |
1026 | break; | |
1027 | ||
ff4cc3ac MK |
1028 | case TUNSETLINK: |
1029 | /* Only allow setting the type when the interface is down */ | |
48abfe05 | 1030 | rtnl_lock(); |
ff4cc3ac MK |
1031 | if (tun->dev->flags & IFF_UP) { |
1032 | DBG(KERN_INFO "%s: Linktype set failed because interface is up\n", | |
1033 | tun->dev->name); | |
48abfe05 | 1034 | ret = -EBUSY; |
ff4cc3ac MK |
1035 | } else { |
1036 | tun->dev->type = (int) arg; | |
1037 | DBG(KERN_INFO "%s: linktype set to %d\n", tun->dev->name, tun->dev->type); | |
48abfe05 | 1038 | ret = 0; |
ff4cc3ac | 1039 | } |
48abfe05 | 1040 | rtnl_unlock(); |
631ab46b | 1041 | break; |
ff4cc3ac | 1042 | |
1da177e4 LT |
1043 | #ifdef TUN_DEBUG |
1044 | case TUNSETDEBUG: | |
1045 | tun->debug = arg; | |
1046 | break; | |
1047 | #endif | |
5228ddc9 | 1048 | case TUNSETOFFLOAD: |
5228ddc9 RR |
1049 | rtnl_lock(); |
1050 | ret = set_offload(tun->dev, arg); | |
1051 | rtnl_unlock(); | |
631ab46b | 1052 | break; |
5228ddc9 | 1053 | |
f271b2cc MK |
1054 | case TUNSETTXFILTER: |
1055 | /* Can be set only for TAPs */ | |
631ab46b | 1056 | ret = -EINVAL; |
f271b2cc | 1057 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
631ab46b | 1058 | break; |
f271b2cc | 1059 | rtnl_lock(); |
c0e5a8c2 | 1060 | ret = update_filter(&tun->txflt, (void __user *)arg); |
f271b2cc | 1061 | rtnl_unlock(); |
631ab46b | 1062 | break; |
1da177e4 LT |
1063 | |
1064 | case SIOCGIFHWADDR: | |
f271b2cc MK |
1065 | /* Get hw addres */ |
1066 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); | |
1067 | ifr.ifr_hwaddr.sa_family = tun->dev->type; | |
1068 | if (copy_to_user(argp, &ifr, sizeof ifr)) | |
631ab46b EB |
1069 | ret = -EFAULT; |
1070 | break; | |
1da177e4 LT |
1071 | |
1072 | case SIOCSIFHWADDR: | |
f271b2cc | 1073 | /* Set hw address */ |
e174961c JB |
1074 | DBG(KERN_DEBUG "%s: set hw address: %pM\n", |
1075 | tun->dev->name, ifr.ifr_hwaddr.sa_data); | |
40102371 KH |
1076 | |
1077 | rtnl_lock(); | |
1078 | ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); | |
1079 | rtnl_unlock(); | |
631ab46b | 1080 | break; |
1da177e4 | 1081 | default: |
631ab46b EB |
1082 | ret = -EINVAL; |
1083 | break; | |
1da177e4 LT |
1084 | }; |
1085 | ||
631ab46b EB |
1086 | tun_put(tun); |
1087 | return ret; | |
1da177e4 LT |
1088 | } |
1089 | ||
1090 | static int tun_chr_fasync(int fd, struct file *file, int on) | |
1091 | { | |
631ab46b | 1092 | struct tun_struct *tun = tun_get(file); |
1da177e4 LT |
1093 | int ret; |
1094 | ||
1095 | if (!tun) | |
1096 | return -EBADFD; | |
1097 | ||
1098 | DBG(KERN_INFO "%s: tun_chr_fasync %d\n", tun->dev->name, on); | |
1099 | ||
9d319522 | 1100 | lock_kernel(); |
1da177e4 | 1101 | if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0) |
9d319522 | 1102 | goto out; |
6aa20a22 | 1103 | |
1da177e4 | 1104 | if (on) { |
609d7fa9 | 1105 | ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0); |
1da177e4 | 1106 | if (ret) |
9d319522 | 1107 | goto out; |
1da177e4 | 1108 | tun->flags |= TUN_FASYNC; |
6aa20a22 | 1109 | } else |
1da177e4 | 1110 | tun->flags &= ~TUN_FASYNC; |
9d319522 JC |
1111 | ret = 0; |
1112 | out: | |
1113 | unlock_kernel(); | |
631ab46b | 1114 | tun_put(tun); |
9d319522 | 1115 | return ret; |
1da177e4 LT |
1116 | } |
1117 | ||
1118 | static int tun_chr_open(struct inode *inode, struct file * file) | |
1119 | { | |
631ab46b | 1120 | struct tun_file *tfile; |
fd3e05b6 | 1121 | cycle_kernel_lock(); |
1da177e4 | 1122 | DBG1(KERN_INFO "tunX: tun_chr_open\n"); |
631ab46b EB |
1123 | |
1124 | tfile = kmalloc(sizeof(*tfile), GFP_KERNEL); | |
1125 | if (!tfile) | |
1126 | return -ENOMEM; | |
1127 | tfile->tun = NULL; | |
36b50bab | 1128 | tfile->net = get_net(current->nsproxy->net_ns); |
631ab46b | 1129 | file->private_data = tfile; |
1da177e4 LT |
1130 | return 0; |
1131 | } | |
1132 | ||
1133 | static int tun_chr_close(struct inode *inode, struct file *file) | |
1134 | { | |
631ab46b EB |
1135 | struct tun_file *tfile = file->private_data; |
1136 | struct tun_struct *tun = __tun_get(tfile); | |
1da177e4 | 1137 | |
1da177e4 | 1138 | |
631ab46b EB |
1139 | if (tun) { |
1140 | DBG(KERN_INFO "%s: tun_chr_close\n", tun->dev->name); | |
1da177e4 | 1141 | |
631ab46b EB |
1142 | rtnl_lock(); |
1143 | __tun_detach(tun); | |
1da177e4 | 1144 | |
631ab46b EB |
1145 | /* If desireable, unregister the netdevice. */ |
1146 | if (!(tun->flags & TUN_PERSIST)) | |
1147 | unregister_netdevice(tun->dev); | |
1da177e4 | 1148 | |
631ab46b EB |
1149 | rtnl_unlock(); |
1150 | } | |
1da177e4 | 1151 | |
36b50bab | 1152 | put_net(tfile->net); |
631ab46b | 1153 | kfree(tfile); |
1da177e4 LT |
1154 | |
1155 | return 0; | |
1156 | } | |
1157 | ||
d54b1fdb | 1158 | static const struct file_operations tun_fops = { |
6aa20a22 | 1159 | .owner = THIS_MODULE, |
1da177e4 | 1160 | .llseek = no_llseek, |
ee0b3e67 BP |
1161 | .read = do_sync_read, |
1162 | .aio_read = tun_chr_aio_read, | |
1163 | .write = do_sync_write, | |
1164 | .aio_write = tun_chr_aio_write, | |
1da177e4 LT |
1165 | .poll = tun_chr_poll, |
1166 | .ioctl = tun_chr_ioctl, | |
1167 | .open = tun_chr_open, | |
1168 | .release = tun_chr_close, | |
6aa20a22 | 1169 | .fasync = tun_chr_fasync |
1da177e4 LT |
1170 | }; |
1171 | ||
1172 | static struct miscdevice tun_miscdev = { | |
1173 | .minor = TUN_MINOR, | |
1174 | .name = "tun", | |
1175 | .fops = &tun_fops, | |
1da177e4 LT |
1176 | }; |
1177 | ||
1178 | /* ethtool interface */ | |
1179 | ||
1180 | static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) | |
1181 | { | |
1182 | cmd->supported = 0; | |
1183 | cmd->advertising = 0; | |
1184 | cmd->speed = SPEED_10; | |
1185 | cmd->duplex = DUPLEX_FULL; | |
1186 | cmd->port = PORT_TP; | |
1187 | cmd->phy_address = 0; | |
1188 | cmd->transceiver = XCVR_INTERNAL; | |
1189 | cmd->autoneg = AUTONEG_DISABLE; | |
1190 | cmd->maxtxpkt = 0; | |
1191 | cmd->maxrxpkt = 0; | |
1192 | return 0; | |
1193 | } | |
1194 | ||
1195 | static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) | |
1196 | { | |
1197 | struct tun_struct *tun = netdev_priv(dev); | |
1198 | ||
1199 | strcpy(info->driver, DRV_NAME); | |
1200 | strcpy(info->version, DRV_VERSION); | |
1201 | strcpy(info->fw_version, "N/A"); | |
1202 | ||
1203 | switch (tun->flags & TUN_TYPE_MASK) { | |
1204 | case TUN_TUN_DEV: | |
1205 | strcpy(info->bus_info, "tun"); | |
1206 | break; | |
1207 | case TUN_TAP_DEV: | |
1208 | strcpy(info->bus_info, "tap"); | |
1209 | break; | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | static u32 tun_get_msglevel(struct net_device *dev) | |
1214 | { | |
1215 | #ifdef TUN_DEBUG | |
1216 | struct tun_struct *tun = netdev_priv(dev); | |
1217 | return tun->debug; | |
1218 | #else | |
1219 | return -EOPNOTSUPP; | |
1220 | #endif | |
1221 | } | |
1222 | ||
1223 | static void tun_set_msglevel(struct net_device *dev, u32 value) | |
1224 | { | |
1225 | #ifdef TUN_DEBUG | |
1226 | struct tun_struct *tun = netdev_priv(dev); | |
1227 | tun->debug = value; | |
1228 | #endif | |
1229 | } | |
1230 | ||
1231 | static u32 tun_get_link(struct net_device *dev) | |
1232 | { | |
1233 | struct tun_struct *tun = netdev_priv(dev); | |
631ab46b | 1234 | return !!tun->tfile; |
1da177e4 LT |
1235 | } |
1236 | ||
1237 | static u32 tun_get_rx_csum(struct net_device *dev) | |
1238 | { | |
1239 | struct tun_struct *tun = netdev_priv(dev); | |
1240 | return (tun->flags & TUN_NOCHECKSUM) == 0; | |
1241 | } | |
1242 | ||
1243 | static int tun_set_rx_csum(struct net_device *dev, u32 data) | |
1244 | { | |
1245 | struct tun_struct *tun = netdev_priv(dev); | |
1246 | if (data) | |
1247 | tun->flags &= ~TUN_NOCHECKSUM; | |
1248 | else | |
1249 | tun->flags |= TUN_NOCHECKSUM; | |
1250 | return 0; | |
1251 | } | |
1252 | ||
7282d491 | 1253 | static const struct ethtool_ops tun_ethtool_ops = { |
1da177e4 LT |
1254 | .get_settings = tun_get_settings, |
1255 | .get_drvinfo = tun_get_drvinfo, | |
1256 | .get_msglevel = tun_get_msglevel, | |
1257 | .set_msglevel = tun_set_msglevel, | |
1258 | .get_link = tun_get_link, | |
1259 | .get_rx_csum = tun_get_rx_csum, | |
1260 | .set_rx_csum = tun_set_rx_csum | |
1261 | }; | |
1262 | ||
79d17604 PE |
1263 | static int tun_init_net(struct net *net) |
1264 | { | |
79d17604 PE |
1265 | return 0; |
1266 | } | |
1267 | ||
1268 | static void tun_exit_net(struct net *net) | |
1269 | { | |
74a3e5a7 | 1270 | struct net_device *dev, *next; |
d647a591 PE |
1271 | |
1272 | rtnl_lock(); | |
74a3e5a7 EB |
1273 | for_each_netdev_safe(net, dev, next) { |
1274 | if (dev->ethtool_ops != &tun_ethtool_ops) | |
1275 | continue; | |
1276 | DBG(KERN_INFO "%s cleaned up\n", dev->name); | |
1277 | unregister_netdevice(dev); | |
d647a591 PE |
1278 | } |
1279 | rtnl_unlock(); | |
79d17604 PE |
1280 | } |
1281 | ||
1282 | static struct pernet_operations tun_net_ops = { | |
1283 | .init = tun_init_net, | |
1284 | .exit = tun_exit_net, | |
1285 | }; | |
1286 | ||
1da177e4 LT |
1287 | static int __init tun_init(void) |
1288 | { | |
1289 | int ret = 0; | |
1290 | ||
1291 | printk(KERN_INFO "tun: %s, %s\n", DRV_DESCRIPTION, DRV_VERSION); | |
1292 | printk(KERN_INFO "tun: %s\n", DRV_COPYRIGHT); | |
1293 | ||
74a3e5a7 | 1294 | ret = register_pernet_device(&tun_net_ops); |
79d17604 PE |
1295 | if (ret) { |
1296 | printk(KERN_ERR "tun: Can't register pernet ops\n"); | |
1297 | goto err_pernet; | |
1298 | } | |
1299 | ||
1da177e4 | 1300 | ret = misc_register(&tun_miscdev); |
79d17604 | 1301 | if (ret) { |
1da177e4 | 1302 | printk(KERN_ERR "tun: Can't register misc device %d\n", TUN_MINOR); |
79d17604 PE |
1303 | goto err_misc; |
1304 | } | |
1305 | return 0; | |
1306 | ||
1307 | err_misc: | |
74a3e5a7 | 1308 | unregister_pernet_device(&tun_net_ops); |
79d17604 | 1309 | err_pernet: |
1da177e4 LT |
1310 | return ret; |
1311 | } | |
1312 | ||
1313 | static void tun_cleanup(void) | |
1314 | { | |
6aa20a22 | 1315 | misc_deregister(&tun_miscdev); |
74a3e5a7 | 1316 | unregister_pernet_device(&tun_net_ops); |
1da177e4 LT |
1317 | } |
1318 | ||
1319 | module_init(tun_init); | |
1320 | module_exit(tun_cleanup); | |
1321 | MODULE_DESCRIPTION(DRV_DESCRIPTION); | |
1322 | MODULE_AUTHOR(DRV_COPYRIGHT); | |
1323 | MODULE_LICENSE("GPL"); | |
1324 | MODULE_ALIAS_MISCDEV(TUN_MINOR); |