diff options
Diffstat (limited to 'drivers/net/tun.c')
-rw-r--r-- | drivers/net/tun.c | 458 |
1 files changed, 297 insertions, 161 deletions
diff --git a/drivers/net/tun.c b/drivers/net/tun.c index eba1271b9735..a82b32b40131 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c | |||
@@ -18,15 +18,11 @@ | |||
18 | /* | 18 | /* |
19 | * Changes: | 19 | * Changes: |
20 | * | 20 | * |
21 | * Brian Braunstein <linuxkernel@bristyle.com> 2007/03/23 | ||
22 | * Fixed hw address handling. Now net_device.dev_addr is kept consistent | ||
23 | * with tun.dev_addr when the address is set by this module. | ||
24 | * | ||
25 | * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 | 21 | * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14 |
26 | * Add TUNSETLINK ioctl to set the link encapsulation | 22 | * Add TUNSETLINK ioctl to set the link encapsulation |
27 | * | 23 | * |
28 | * Mark Smith <markzzzsmith@yahoo.com.au> | 24 | * Mark Smith <markzzzsmith@yahoo.com.au> |
29 | * Use random_ether_addr() for tap MAC address. | 25 | * Use random_ether_addr() for tap MAC address. |
30 | * | 26 | * |
31 | * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 | 27 | * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20 |
32 | * Fixes in packet dropping, queue length setting and queue wakeup. | 28 | * Fixes in packet dropping, queue length setting and queue wakeup. |
@@ -64,6 +60,7 @@ | |||
64 | #include <linux/if_tun.h> | 60 | #include <linux/if_tun.h> |
65 | #include <linux/crc32.h> | 61 | #include <linux/crc32.h> |
66 | #include <linux/nsproxy.h> | 62 | #include <linux/nsproxy.h> |
63 | #include <linux/virtio_net.h> | ||
67 | #include <net/net_namespace.h> | 64 | #include <net/net_namespace.h> |
68 | #include <net/netns/generic.h> | 65 | #include <net/netns/generic.h> |
69 | 66 | ||
@@ -83,9 +80,16 @@ static int debug; | |||
83 | #define DBG1( a... ) | 80 | #define DBG1( a... ) |
84 | #endif | 81 | #endif |
85 | 82 | ||
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 | |||
86 | struct tun_struct { | 90 | struct tun_struct { |
87 | struct list_head list; | 91 | struct list_head list; |
88 | unsigned long flags; | 92 | unsigned int flags; |
89 | int attached; | 93 | int attached; |
90 | uid_t owner; | 94 | uid_t owner; |
91 | gid_t group; | 95 | gid_t group; |
@@ -94,19 +98,119 @@ struct tun_struct { | |||
94 | struct sk_buff_head readq; | 98 | struct sk_buff_head readq; |
95 | 99 | ||
96 | struct net_device *dev; | 100 | struct net_device *dev; |
101 | struct fasync_struct *fasync; | ||
97 | 102 | ||
98 | struct fasync_struct *fasync; | 103 | struct tap_filter txflt; |
99 | |||
100 | unsigned long if_flags; | ||
101 | u8 dev_addr[ETH_ALEN]; | ||
102 | u32 chr_filter[2]; | ||
103 | u32 net_filter[2]; | ||
104 | 104 | ||
105 | #ifdef TUN_DEBUG | 105 | #ifdef TUN_DEBUG |
106 | int debug; | 106 | int debug; |
107 | #endif | 107 | #endif |
108 | }; | 108 | }; |
109 | 109 | ||
110 | /* TAP filterting */ | ||
111 | static void addr_hash_set(u32 *mask, const u8 *addr) | ||
112 | { | ||
113 | int n = ether_crc(ETH_ALEN, addr) >> 26; | ||
114 | mask[n >> 5] |= (1 << (n & 31)); | ||
115 | } | ||
116 | |||
117 | static unsigned int addr_hash_test(const u32 *mask, const u8 *addr) | ||
118 | { | ||
119 | int n = ether_crc(ETH_ALEN, addr) >> 26; | ||
120 | return mask[n >> 5] & (1 << (n & 31)); | ||
121 | } | ||
122 | |||
123 | static int update_filter(struct tap_filter *filter, void __user *arg) | ||
124 | { | ||
125 | struct { u8 u[ETH_ALEN]; } *addr; | ||
126 | struct tun_filter uf; | ||
127 | int err, alen, n, nexact; | ||
128 | |||
129 | if (copy_from_user(&uf, arg, sizeof(uf))) | ||
130 | return -EFAULT; | ||
131 | |||
132 | if (!uf.count) { | ||
133 | /* Disabled */ | ||
134 | filter->count = 0; | ||
135 | return 0; | ||
136 | } | ||
137 | |||
138 | alen = ETH_ALEN * uf.count; | ||
139 | addr = kmalloc(alen, GFP_KERNEL); | ||
140 | if (!addr) | ||
141 | return -ENOMEM; | ||
142 | |||
143 | if (copy_from_user(addr, arg + sizeof(uf), alen)) { | ||
144 | err = -EFAULT; | ||
145 | goto done; | ||
146 | } | ||
147 | |||
148 | /* The filter is updated without holding any locks. Which is | ||
149 | * perfectly safe. We disable it first and in the worst | ||
150 | * case we'll accept a few undesired packets. */ | ||
151 | filter->count = 0; | ||
152 | wmb(); | ||
153 | |||
154 | /* Use first set of addresses as an exact filter */ | ||
155 | for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++) | ||
156 | memcpy(filter->addr[n], addr[n].u, ETH_ALEN); | ||
157 | |||
158 | nexact = n; | ||
159 | |||
160 | /* The rest is hashed */ | ||
161 | memset(filter->mask, 0, sizeof(filter->mask)); | ||
162 | for (; n < uf.count; n++) | ||
163 | addr_hash_set(filter->mask, addr[n].u); | ||
164 | |||
165 | /* For ALLMULTI just set the mask to all ones. | ||
166 | * This overrides the mask populated above. */ | ||
167 | if ((uf.flags & TUN_FLT_ALLMULTI)) | ||
168 | memset(filter->mask, ~0, sizeof(filter->mask)); | ||
169 | |||
170 | /* Now enable the filter */ | ||
171 | wmb(); | ||
172 | filter->count = nexact; | ||
173 | |||
174 | /* Return the number of exact filters */ | ||
175 | err = nexact; | ||
176 | |||
177 | done: | ||
178 | kfree(addr); | ||
179 | return err; | ||
180 | } | ||
181 | |||
182 | /* Returns: 0 - drop, !=0 - accept */ | ||
183 | static int run_filter(struct tap_filter *filter, const struct sk_buff *skb) | ||
184 | { | ||
185 | /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect | ||
186 | * at this point. */ | ||
187 | struct ethhdr *eh = (struct ethhdr *) skb->data; | ||
188 | int i; | ||
189 | |||
190 | /* Exact match */ | ||
191 | for (i = 0; i < filter->count; i++) | ||
192 | if (!compare_ether_addr(eh->h_dest, filter->addr[i])) | ||
193 | return 1; | ||
194 | |||
195 | /* Inexact match (multicast only) */ | ||
196 | if (is_multicast_ether_addr(eh->h_dest)) | ||
197 | return addr_hash_test(filter->mask, eh->h_dest); | ||
198 | |||
199 | return 0; | ||
200 | } | ||
201 | |||
202 | /* | ||
203 | * Checks whether the packet is accepted or not. | ||
204 | * Returns: 0 - drop, !=0 - accept | ||
205 | */ | ||
206 | static int check_filter(struct tap_filter *filter, const struct sk_buff *skb) | ||
207 | { | ||
208 | if (!filter->count) | ||
209 | return 1; | ||
210 | |||
211 | return run_filter(filter, skb); | ||
212 | } | ||
213 | |||
110 | /* Network device part of the driver */ | 214 | /* Network device part of the driver */ |
111 | 215 | ||
112 | static unsigned int tun_net_id; | 216 | static unsigned int tun_net_id; |
@@ -141,7 +245,12 @@ static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev) | |||
141 | if (!tun->attached) | 245 | if (!tun->attached) |
142 | goto drop; | 246 | goto drop; |
143 | 247 | ||
144 | /* Packet dropping */ | 248 | /* Drop if the filter does not like it. |
249 | * This is a noop if the filter is disabled. | ||
250 | * Filter can be enabled only for the TAP devices. */ | ||
251 | if (!check_filter(&tun->txflt, skb)) | ||
252 | goto drop; | ||
253 | |||
145 | if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) { | 254 | if (skb_queue_len(&tun->readq) >= dev->tx_queue_len) { |
146 | if (!(tun->flags & TUN_ONE_QUEUE)) { | 255 | if (!(tun->flags & TUN_ONE_QUEUE)) { |
147 | /* Normal queueing mode. */ | 256 | /* Normal queueing mode. */ |
@@ -158,7 +267,7 @@ static int tun_net_xmit(struct sk_buff *skb, struct net_device *dev) | |||
158 | } | 267 | } |
159 | } | 268 | } |
160 | 269 | ||
161 | /* Queue packet */ | 270 | /* Enqueue packet */ |
162 | skb_queue_tail(&tun->readq, skb); | 271 | skb_queue_tail(&tun->readq, skb); |
163 | dev->trans_start = jiffies; | 272 | dev->trans_start = jiffies; |
164 | 273 | ||
@@ -174,41 +283,14 @@ drop: | |||
174 | return 0; | 283 | return 0; |
175 | } | 284 | } |
176 | 285 | ||
177 | /** Add the specified Ethernet address to this multicast filter. */ | 286 | static void tun_net_mclist(struct net_device *dev) |
178 | static void | ||
179 | add_multi(u32* filter, const u8* addr) | ||
180 | { | 287 | { |
181 | int bit_nr = ether_crc(ETH_ALEN, addr) >> 26; | 288 | /* |
182 | filter[bit_nr >> 5] |= 1 << (bit_nr & 31); | 289 | * This callback is supposed to deal with mc filter in |
183 | } | 290 | * _rx_ path and has nothing to do with the _tx_ path. |
184 | 291 | * In rx path we always accept everything userspace gives us. | |
185 | /** Remove the specified Ethernet addres from this multicast filter. */ | 292 | */ |
186 | static void | 293 | return; |
187 | del_multi(u32* filter, const u8* addr) | ||
188 | { | ||
189 | int bit_nr = ether_crc(ETH_ALEN, addr) >> 26; | ||
190 | filter[bit_nr >> 5] &= ~(1 << (bit_nr & 31)); | ||
191 | } | ||
192 | |||
193 | /** Update the list of multicast groups to which the network device belongs. | ||
194 | * This list is used to filter packets being sent from the character device to | ||
195 | * the network device. */ | ||
196 | static void | ||
197 | tun_net_mclist(struct net_device *dev) | ||
198 | { | ||
199 | struct tun_struct *tun = netdev_priv(dev); | ||
200 | const struct dev_mc_list *mclist; | ||
201 | int i; | ||
202 | DECLARE_MAC_BUF(mac); | ||
203 | DBG(KERN_DEBUG "%s: tun_net_mclist: mc_count %d\n", | ||
204 | dev->name, dev->mc_count); | ||
205 | memset(tun->chr_filter, 0, sizeof tun->chr_filter); | ||
206 | for (i = 0, mclist = dev->mc_list; i < dev->mc_count && mclist != NULL; | ||
207 | i++, mclist = mclist->next) { | ||
208 | add_multi(tun->net_filter, mclist->dmi_addr); | ||
209 | DBG(KERN_DEBUG "%s: tun_net_mclist: %s\n", | ||
210 | dev->name, print_mac(mac, mclist->dmi_addr)); | ||
211 | } | ||
212 | } | 294 | } |
213 | 295 | ||
214 | #define MIN_MTU 68 | 296 | #define MIN_MTU 68 |
@@ -244,13 +326,11 @@ static void tun_net_init(struct net_device *dev) | |||
244 | 326 | ||
245 | case TUN_TAP_DEV: | 327 | case TUN_TAP_DEV: |
246 | /* Ethernet TAP Device */ | 328 | /* Ethernet TAP Device */ |
247 | dev->set_multicast_list = tun_net_mclist; | ||
248 | |||
249 | ether_setup(dev); | 329 | ether_setup(dev); |
250 | dev->change_mtu = tun_net_change_mtu; | 330 | dev->change_mtu = tun_net_change_mtu; |
331 | dev->set_multicast_list = tun_net_mclist; | ||
251 | 332 | ||
252 | /* random address already created for us by tun_set_iff, use it */ | 333 | random_ether_addr(dev->dev_addr); |
253 | memcpy(dev->dev_addr, tun->dev_addr, min(sizeof(tun->dev_addr), sizeof(dev->dev_addr)) ); | ||
254 | 334 | ||
255 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ | 335 | dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */ |
256 | break; | 336 | break; |
@@ -284,6 +364,7 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, | |||
284 | struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) }; | 364 | struct tun_pi pi = { 0, __constant_htons(ETH_P_IP) }; |
285 | struct sk_buff *skb; | 365 | struct sk_buff *skb; |
286 | size_t len = count, align = 0; | 366 | size_t len = count, align = 0; |
367 | struct virtio_net_hdr gso = { 0 }; | ||
287 | 368 | ||
288 | if (!(tun->flags & TUN_NO_PI)) { | 369 | if (!(tun->flags & TUN_NO_PI)) { |
289 | if ((len -= sizeof(pi)) > count) | 370 | if ((len -= sizeof(pi)) > count) |
@@ -293,6 +374,17 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, | |||
293 | return -EFAULT; | 374 | return -EFAULT; |
294 | } | 375 | } |
295 | 376 | ||
377 | if (tun->flags & TUN_VNET_HDR) { | ||
378 | if ((len -= sizeof(gso)) > count) | ||
379 | return -EINVAL; | ||
380 | |||
381 | if (memcpy_fromiovec((void *)&gso, iv, sizeof(gso))) | ||
382 | return -EFAULT; | ||
383 | |||
384 | if (gso.hdr_len > len) | ||
385 | return -EINVAL; | ||
386 | } | ||
387 | |||
296 | if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { | 388 | if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { |
297 | align = NET_IP_ALIGN; | 389 | align = NET_IP_ALIGN; |
298 | if (unlikely(len < ETH_HLEN)) | 390 | if (unlikely(len < ETH_HLEN)) |
@@ -312,6 +404,16 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, | |||
312 | return -EFAULT; | 404 | return -EFAULT; |
313 | } | 405 | } |
314 | 406 | ||
407 | if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { | ||
408 | if (!skb_partial_csum_set(skb, gso.csum_start, | ||
409 | gso.csum_offset)) { | ||
410 | tun->dev->stats.rx_frame_errors++; | ||
411 | kfree_skb(skb); | ||
412 | return -EINVAL; | ||
413 | } | ||
414 | } else if (tun->flags & TUN_NOCHECKSUM) | ||
415 | skb->ip_summed = CHECKSUM_UNNECESSARY; | ||
416 | |||
315 | switch (tun->flags & TUN_TYPE_MASK) { | 417 | switch (tun->flags & TUN_TYPE_MASK) { |
316 | case TUN_TUN_DEV: | 418 | case TUN_TUN_DEV: |
317 | if (tun->flags & TUN_NO_PI) { | 419 | if (tun->flags & TUN_NO_PI) { |
@@ -338,8 +440,35 @@ static __inline__ ssize_t tun_get_user(struct tun_struct *tun, struct iovec *iv, | |||
338 | break; | 440 | break; |
339 | }; | 441 | }; |
340 | 442 | ||
341 | if (tun->flags & TUN_NOCHECKSUM) | 443 | if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
342 | skb->ip_summed = CHECKSUM_UNNECESSARY; | 444 | pr_debug("GSO!\n"); |
445 | switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { | ||
446 | case VIRTIO_NET_HDR_GSO_TCPV4: | ||
447 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; | ||
448 | break; | ||
449 | case VIRTIO_NET_HDR_GSO_TCPV6: | ||
450 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; | ||
451 | break; | ||
452 | default: | ||
453 | tun->dev->stats.rx_frame_errors++; | ||
454 | kfree_skb(skb); | ||
455 | return -EINVAL; | ||
456 | } | ||
457 | |||
458 | if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN) | ||
459 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; | ||
460 | |||
461 | skb_shinfo(skb)->gso_size = gso.gso_size; | ||
462 | if (skb_shinfo(skb)->gso_size == 0) { | ||
463 | tun->dev->stats.rx_frame_errors++; | ||
464 | kfree_skb(skb); | ||
465 | return -EINVAL; | ||
466 | } | ||
467 | |||
468 | /* Header must be checked, and gso_segs computed. */ | ||
469 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | ||
470 | skb_shinfo(skb)->gso_segs = 0; | ||
471 | } | ||
343 | 472 | ||
344 | netif_rx_ni(skb); | 473 | netif_rx_ni(skb); |
345 | tun->dev->last_rx = jiffies; | 474 | tun->dev->last_rx = jiffies; |
@@ -385,6 +514,39 @@ static __inline__ ssize_t tun_put_user(struct tun_struct *tun, | |||
385 | total += sizeof(pi); | 514 | total += sizeof(pi); |
386 | } | 515 | } |
387 | 516 | ||
517 | if (tun->flags & TUN_VNET_HDR) { | ||
518 | struct virtio_net_hdr gso = { 0 }; /* no info leak */ | ||
519 | if ((len -= sizeof(gso)) < 0) | ||
520 | return -EINVAL; | ||
521 | |||
522 | if (skb_is_gso(skb)) { | ||
523 | struct skb_shared_info *sinfo = skb_shinfo(skb); | ||
524 | |||
525 | /* This is a hint as to how much should be linear. */ | ||
526 | gso.hdr_len = skb_headlen(skb); | ||
527 | gso.gso_size = sinfo->gso_size; | ||
528 | if (sinfo->gso_type & SKB_GSO_TCPV4) | ||
529 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; | ||
530 | else if (sinfo->gso_type & SKB_GSO_TCPV6) | ||
531 | gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; | ||
532 | else | ||
533 | BUG(); | ||
534 | if (sinfo->gso_type & SKB_GSO_TCP_ECN) | ||
535 | gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN; | ||
536 | } else | ||
537 | gso.gso_type = VIRTIO_NET_HDR_GSO_NONE; | ||
538 | |||
539 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | ||
540 | gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; | ||
541 | gso.csum_start = skb->csum_start - skb_headroom(skb); | ||
542 | gso.csum_offset = skb->csum_offset; | ||
543 | } /* else everything is zero */ | ||
544 | |||
545 | if (unlikely(memcpy_toiovec(iv, (void *)&gso, sizeof(gso)))) | ||
546 | return -EFAULT; | ||
547 | total += sizeof(gso); | ||
548 | } | ||
549 | |||
388 | len = min_t(int, skb->len, len); | 550 | len = min_t(int, skb->len, len); |
389 | 551 | ||
390 | skb_copy_datagram_iovec(skb, 0, iv, len); | 552 | skb_copy_datagram_iovec(skb, 0, iv, len); |
@@ -404,7 +566,6 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, | |||
404 | DECLARE_WAITQUEUE(wait, current); | 566 | DECLARE_WAITQUEUE(wait, current); |
405 | struct sk_buff *skb; | 567 | struct sk_buff *skb; |
406 | ssize_t len, ret = 0; | 568 | ssize_t len, ret = 0; |
407 | DECLARE_MAC_BUF(mac); | ||
408 | 569 | ||
409 | if (!tun) | 570 | if (!tun) |
410 | return -EBADFD; | 571 | return -EBADFD; |
@@ -417,10 +578,6 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, | |||
417 | 578 | ||
418 | add_wait_queue(&tun->read_wait, &wait); | 579 | add_wait_queue(&tun->read_wait, &wait); |
419 | while (len) { | 580 | while (len) { |
420 | const u8 ones[ ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; | ||
421 | u8 addr[ ETH_ALEN]; | ||
422 | int bit_nr; | ||
423 | |||
424 | current->state = TASK_INTERRUPTIBLE; | 581 | current->state = TASK_INTERRUPTIBLE; |
425 | 582 | ||
426 | /* Read frames from the queue */ | 583 | /* Read frames from the queue */ |
@@ -440,36 +597,9 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, | |||
440 | } | 597 | } |
441 | netif_wake_queue(tun->dev); | 598 | netif_wake_queue(tun->dev); |
442 | 599 | ||
443 | /** Decide whether to accept this packet. This code is designed to | 600 | ret = tun_put_user(tun, skb, (struct iovec *) iv, len); |
444 | * behave identically to an Ethernet interface. Accept the packet if | 601 | kfree_skb(skb); |
445 | * - we are promiscuous. | 602 | break; |
446 | * - the packet is addressed to us. | ||
447 | * - the packet is broadcast. | ||
448 | * - the packet is multicast and | ||
449 | * - we are multicast promiscous. | ||
450 | * - we belong to the multicast group. | ||
451 | */ | ||
452 | skb_copy_from_linear_data(skb, addr, min_t(size_t, sizeof addr, | ||
453 | skb->len)); | ||
454 | bit_nr = ether_crc(sizeof addr, addr) >> 26; | ||
455 | if ((tun->if_flags & IFF_PROMISC) || | ||
456 | memcmp(addr, tun->dev_addr, sizeof addr) == 0 || | ||
457 | memcmp(addr, ones, sizeof addr) == 0 || | ||
458 | (((addr[0] == 1 && addr[1] == 0 && addr[2] == 0x5e) || | ||
459 | (addr[0] == 0x33 && addr[1] == 0x33)) && | ||
460 | ((tun->if_flags & IFF_ALLMULTI) || | ||
461 | (tun->chr_filter[bit_nr >> 5] & (1 << (bit_nr & 31)))))) { | ||
462 | DBG(KERN_DEBUG "%s: tun_chr_readv: accepted: %s\n", | ||
463 | tun->dev->name, print_mac(mac, addr)); | ||
464 | ret = tun_put_user(tun, skb, (struct iovec *) iv, len); | ||
465 | kfree_skb(skb); | ||
466 | break; | ||
467 | } else { | ||
468 | DBG(KERN_DEBUG "%s: tun_chr_readv: rejected: %s\n", | ||
469 | tun->dev->name, print_mac(mac, addr)); | ||
470 | kfree_skb(skb); | ||
471 | continue; | ||
472 | } | ||
473 | } | 603 | } |
474 | 604 | ||
475 | current->state = TASK_RUNNING; | 605 | current->state = TASK_RUNNING; |
@@ -565,12 +695,7 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) | |||
565 | tun = netdev_priv(dev); | 695 | tun = netdev_priv(dev); |
566 | tun->dev = dev; | 696 | tun->dev = dev; |
567 | tun->flags = flags; | 697 | tun->flags = flags; |
568 | /* Be promiscuous by default to maintain previous behaviour. */ | 698 | tun->txflt.count = 0; |
569 | tun->if_flags = IFF_PROMISC; | ||
570 | /* Generate random Ethernet address. */ | ||
571 | *(__be16 *)tun->dev_addr = htons(0x00FF); | ||
572 | get_random_bytes(tun->dev_addr + sizeof(u16), 4); | ||
573 | memset(tun->chr_filter, 0, sizeof tun->chr_filter); | ||
574 | 699 | ||
575 | tun_net_init(dev); | 700 | tun_net_init(dev); |
576 | 701 | ||
@@ -599,6 +724,11 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) | |||
599 | else | 724 | else |
600 | tun->flags &= ~TUN_ONE_QUEUE; | 725 | tun->flags &= ~TUN_ONE_QUEUE; |
601 | 726 | ||
727 | if (ifr->ifr_flags & IFF_VNET_HDR) | ||
728 | tun->flags |= TUN_VNET_HDR; | ||
729 | else | ||
730 | tun->flags &= ~TUN_VNET_HDR; | ||
731 | |||
602 | file->private_data = tun; | 732 | file->private_data = tun; |
603 | tun->attached = 1; | 733 | tun->attached = 1; |
604 | get_net(dev_net(tun->dev)); | 734 | get_net(dev_net(tun->dev)); |
@@ -618,12 +748,53 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr) | |||
618 | return err; | 748 | return err; |
619 | } | 749 | } |
620 | 750 | ||
751 | /* This is like a cut-down ethtool ops, except done via tun fd so no | ||
752 | * privs required. */ | ||
753 | static int set_offload(struct net_device *dev, unsigned long arg) | ||
754 | { | ||
755 | unsigned int old_features, features; | ||
756 | |||
757 | old_features = dev->features; | ||
758 | /* Unset features, set them as we chew on the arg. */ | ||
759 | features = (old_features & ~(NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST | ||
760 | |NETIF_F_TSO_ECN|NETIF_F_TSO|NETIF_F_TSO6)); | ||
761 | |||
762 | if (arg & TUN_F_CSUM) { | ||
763 | features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; | ||
764 | arg &= ~TUN_F_CSUM; | ||
765 | |||
766 | if (arg & (TUN_F_TSO4|TUN_F_TSO6)) { | ||
767 | if (arg & TUN_F_TSO_ECN) { | ||
768 | features |= NETIF_F_TSO_ECN; | ||
769 | arg &= ~TUN_F_TSO_ECN; | ||
770 | } | ||
771 | if (arg & TUN_F_TSO4) | ||
772 | features |= NETIF_F_TSO; | ||
773 | if (arg & TUN_F_TSO6) | ||
774 | features |= NETIF_F_TSO6; | ||
775 | arg &= ~(TUN_F_TSO4|TUN_F_TSO6); | ||
776 | } | ||
777 | } | ||
778 | |||
779 | /* This gives the user a way to test for new features in future by | ||
780 | * trying to set them. */ | ||
781 | if (arg) | ||
782 | return -EINVAL; | ||
783 | |||
784 | dev->features = features; | ||
785 | if (old_features != dev->features) | ||
786 | netdev_features_change(dev); | ||
787 | |||
788 | return 0; | ||
789 | } | ||
790 | |||
621 | static int tun_chr_ioctl(struct inode *inode, struct file *file, | 791 | static int tun_chr_ioctl(struct inode *inode, struct file *file, |
622 | unsigned int cmd, unsigned long arg) | 792 | unsigned int cmd, unsigned long arg) |
623 | { | 793 | { |
624 | struct tun_struct *tun = file->private_data; | 794 | struct tun_struct *tun = file->private_data; |
625 | void __user* argp = (void __user*)arg; | 795 | void __user* argp = (void __user*)arg; |
626 | struct ifreq ifr; | 796 | struct ifreq ifr; |
797 | int ret; | ||
627 | DECLARE_MAC_BUF(mac); | 798 | DECLARE_MAC_BUF(mac); |
628 | 799 | ||
629 | if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) | 800 | if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) |
@@ -647,6 +818,15 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file, | |||
647 | return 0; | 818 | return 0; |
648 | } | 819 | } |
649 | 820 | ||
821 | if (cmd == TUNGETFEATURES) { | ||
822 | /* Currently this just means: "what IFF flags are valid?". | ||
823 | * This is needed because we never checked for invalid flags on | ||
824 | * TUNSETIFF. */ | ||
825 | return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | | ||
826 | IFF_VNET_HDR, | ||
827 | (unsigned int __user*)argp); | ||
828 | } | ||
829 | |||
650 | if (!tun) | 830 | if (!tun) |
651 | return -EBADFD; | 831 | return -EBADFD; |
652 | 832 | ||
@@ -690,9 +870,6 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file, | |||
690 | break; | 870 | break; |
691 | 871 | ||
692 | case TUNSETLINK: | 872 | case TUNSETLINK: |
693 | { | ||
694 | int ret; | ||
695 | |||
696 | /* Only allow setting the type when the interface is down */ | 873 | /* Only allow setting the type when the interface is down */ |
697 | rtnl_lock(); | 874 | rtnl_lock(); |
698 | if (tun->dev->flags & IFF_UP) { | 875 | if (tun->dev->flags & IFF_UP) { |
@@ -706,85 +883,44 @@ static int tun_chr_ioctl(struct inode *inode, struct file *file, | |||
706 | } | 883 | } |
707 | rtnl_unlock(); | 884 | rtnl_unlock(); |
708 | return ret; | 885 | return ret; |
709 | } | ||
710 | 886 | ||
711 | #ifdef TUN_DEBUG | 887 | #ifdef TUN_DEBUG |
712 | case TUNSETDEBUG: | 888 | case TUNSETDEBUG: |
713 | tun->debug = arg; | 889 | tun->debug = arg; |
714 | break; | 890 | break; |
715 | #endif | 891 | #endif |
892 | case TUNSETOFFLOAD: | ||
893 | rtnl_lock(); | ||
894 | ret = set_offload(tun->dev, arg); | ||
895 | rtnl_unlock(); | ||
896 | return ret; | ||
716 | 897 | ||
717 | case SIOCGIFFLAGS: | 898 | case TUNSETTXFILTER: |
718 | ifr.ifr_flags = tun->if_flags; | 899 | /* Can be set only for TAPs */ |
719 | if (copy_to_user( argp, &ifr, sizeof ifr)) | 900 | if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV) |
720 | return -EFAULT; | 901 | return -EINVAL; |
721 | return 0; | 902 | rtnl_lock(); |
722 | 903 | ret = update_filter(&tun->txflt, (void *) __user arg); | |
723 | case SIOCSIFFLAGS: | 904 | rtnl_unlock(); |
724 | /** Set the character device's interface flags. Currently only | 905 | return ret; |
725 | * IFF_PROMISC and IFF_ALLMULTI are used. */ | ||
726 | tun->if_flags = ifr.ifr_flags; | ||
727 | DBG(KERN_INFO "%s: interface flags 0x%lx\n", | ||
728 | tun->dev->name, tun->if_flags); | ||
729 | return 0; | ||
730 | 906 | ||
731 | case SIOCGIFHWADDR: | 907 | case SIOCGIFHWADDR: |
732 | /* Note: the actual net device's address may be different */ | 908 | /* Get hw addres */ |
733 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev_addr, | 909 | memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN); |
734 | min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr)); | 910 | ifr.ifr_hwaddr.sa_family = tun->dev->type; |
735 | if (copy_to_user( argp, &ifr, sizeof ifr)) | 911 | if (copy_to_user(argp, &ifr, sizeof ifr)) |
736 | return -EFAULT; | 912 | return -EFAULT; |
737 | return 0; | 913 | return 0; |
738 | 914 | ||
739 | case SIOCSIFHWADDR: | 915 | case SIOCSIFHWADDR: |
740 | { | 916 | /* Set hw address */ |
741 | /* try to set the actual net device's hw address */ | 917 | DBG(KERN_DEBUG "%s: set hw address: %s\n", |
742 | int ret; | 918 | tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data)); |
743 | 919 | ||
744 | rtnl_lock(); | 920 | rtnl_lock(); |
745 | ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); | 921 | ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr); |
746 | rtnl_unlock(); | 922 | rtnl_unlock(); |
747 | 923 | return ret; | |
748 | if (ret == 0) { | ||
749 | /** Set the character device's hardware address. This is used when | ||
750 | * filtering packets being sent from the network device to the character | ||
751 | * device. */ | ||
752 | memcpy(tun->dev_addr, ifr.ifr_hwaddr.sa_data, | ||
753 | min(sizeof ifr.ifr_hwaddr.sa_data, sizeof tun->dev_addr)); | ||
754 | DBG(KERN_DEBUG "%s: set hardware address: %x:%x:%x:%x:%x:%x\n", | ||
755 | tun->dev->name, | ||
756 | tun->dev_addr[0], tun->dev_addr[1], tun->dev_addr[2], | ||
757 | tun->dev_addr[3], tun->dev_addr[4], tun->dev_addr[5]); | ||
758 | } | ||
759 | |||
760 | return ret; | ||
761 | } | ||
762 | |||
763 | case SIOCADDMULTI: | ||
764 | /** Add the specified group to the character device's multicast filter | ||
765 | * list. */ | ||
766 | rtnl_lock(); | ||
767 | netif_tx_lock_bh(tun->dev); | ||
768 | add_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data); | ||
769 | netif_tx_unlock_bh(tun->dev); | ||
770 | rtnl_unlock(); | ||
771 | |||
772 | DBG(KERN_DEBUG "%s: add multi: %s\n", | ||
773 | tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data)); | ||
774 | return 0; | ||
775 | |||
776 | case SIOCDELMULTI: | ||
777 | /** Remove the specified group from the character device's multicast | ||
778 | * filter list. */ | ||
779 | rtnl_lock(); | ||
780 | netif_tx_lock_bh(tun->dev); | ||
781 | del_multi(tun->chr_filter, ifr.ifr_hwaddr.sa_data); | ||
782 | netif_tx_unlock_bh(tun->dev); | ||
783 | rtnl_unlock(); | ||
784 | |||
785 | DBG(KERN_DEBUG "%s: del multi: %s\n", | ||
786 | tun->dev->name, print_mac(mac, ifr.ifr_hwaddr.sa_data)); | ||
787 | return 0; | ||
788 | 924 | ||
789 | default: | 925 | default: |
790 | return -EINVAL; | 926 | return -EINVAL; |