diff options
Diffstat (limited to 'net/ipv6/sit.c')
-rw-r--r-- | net/ipv6/sit.c | 356 |
1 files changed, 248 insertions, 108 deletions
diff --git a/net/ipv6/sit.c b/net/ipv6/sit.c index dbd19a78ca73..5abae10cd884 100644 --- a/net/ipv6/sit.c +++ b/net/ipv6/sit.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #include <linux/netdevice.h> | 28 | #include <linux/netdevice.h> |
29 | #include <linux/if_arp.h> | 29 | #include <linux/if_arp.h> |
30 | #include <linux/icmp.h> | 30 | #include <linux/icmp.h> |
31 | #include <linux/slab.h> | ||
31 | #include <asm/uaccess.h> | 32 | #include <asm/uaccess.h> |
32 | #include <linux/init.h> | 33 | #include <linux/init.h> |
33 | #include <linux/netfilter_ipv4.h> | 34 | #include <linux/netfilter_ipv4.h> |
@@ -62,11 +63,10 @@ | |||
62 | #define HASH_SIZE 16 | 63 | #define HASH_SIZE 16 |
63 | #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF) | 64 | #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF) |
64 | 65 | ||
65 | static void ipip6_fb_tunnel_init(struct net_device *dev); | ||
66 | static void ipip6_tunnel_init(struct net_device *dev); | 66 | static void ipip6_tunnel_init(struct net_device *dev); |
67 | static void ipip6_tunnel_setup(struct net_device *dev); | 67 | static void ipip6_tunnel_setup(struct net_device *dev); |
68 | 68 | ||
69 | static int sit_net_id; | 69 | static int sit_net_id __read_mostly; |
70 | struct sit_net { | 70 | struct sit_net { |
71 | struct ip_tunnel *tunnels_r_l[HASH_SIZE]; | 71 | struct ip_tunnel *tunnels_r_l[HASH_SIZE]; |
72 | struct ip_tunnel *tunnels_r[HASH_SIZE]; | 72 | struct ip_tunnel *tunnels_r[HASH_SIZE]; |
@@ -77,8 +77,17 @@ struct sit_net { | |||
77 | struct net_device *fb_tunnel_dev; | 77 | struct net_device *fb_tunnel_dev; |
78 | }; | 78 | }; |
79 | 79 | ||
80 | static DEFINE_RWLOCK(ipip6_lock); | 80 | /* |
81 | * Locking : hash tables are protected by RCU and a spinlock | ||
82 | */ | ||
83 | static DEFINE_SPINLOCK(ipip6_lock); | ||
84 | |||
85 | #define for_each_ip_tunnel_rcu(start) \ | ||
86 | for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) | ||
81 | 87 | ||
88 | /* | ||
89 | * Must be invoked with rcu_read_lock | ||
90 | */ | ||
82 | static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net, | 91 | static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net, |
83 | struct net_device *dev, __be32 remote, __be32 local) | 92 | struct net_device *dev, __be32 remote, __be32 local) |
84 | { | 93 | { |
@@ -87,26 +96,26 @@ static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net, | |||
87 | struct ip_tunnel *t; | 96 | struct ip_tunnel *t; |
88 | struct sit_net *sitn = net_generic(net, sit_net_id); | 97 | struct sit_net *sitn = net_generic(net, sit_net_id); |
89 | 98 | ||
90 | for (t = sitn->tunnels_r_l[h0^h1]; t; t = t->next) { | 99 | for_each_ip_tunnel_rcu(sitn->tunnels_r_l[h0 ^ h1]) { |
91 | if (local == t->parms.iph.saddr && | 100 | if (local == t->parms.iph.saddr && |
92 | remote == t->parms.iph.daddr && | 101 | remote == t->parms.iph.daddr && |
93 | (!dev || !t->parms.link || dev->iflink == t->parms.link) && | 102 | (!dev || !t->parms.link || dev->iflink == t->parms.link) && |
94 | (t->dev->flags & IFF_UP)) | 103 | (t->dev->flags & IFF_UP)) |
95 | return t; | 104 | return t; |
96 | } | 105 | } |
97 | for (t = sitn->tunnels_r[h0]; t; t = t->next) { | 106 | for_each_ip_tunnel_rcu(sitn->tunnels_r[h0]) { |
98 | if (remote == t->parms.iph.daddr && | 107 | if (remote == t->parms.iph.daddr && |
99 | (!dev || !t->parms.link || dev->iflink == t->parms.link) && | 108 | (!dev || !t->parms.link || dev->iflink == t->parms.link) && |
100 | (t->dev->flags & IFF_UP)) | 109 | (t->dev->flags & IFF_UP)) |
101 | return t; | 110 | return t; |
102 | } | 111 | } |
103 | for (t = sitn->tunnels_l[h1]; t; t = t->next) { | 112 | for_each_ip_tunnel_rcu(sitn->tunnels_l[h1]) { |
104 | if (local == t->parms.iph.saddr && | 113 | if (local == t->parms.iph.saddr && |
105 | (!dev || !t->parms.link || dev->iflink == t->parms.link) && | 114 | (!dev || !t->parms.link || dev->iflink == t->parms.link) && |
106 | (t->dev->flags & IFF_UP)) | 115 | (t->dev->flags & IFF_UP)) |
107 | return t; | 116 | return t; |
108 | } | 117 | } |
109 | t = sitn->tunnels_wc[0]; | 118 | t = rcu_dereference(sitn->tunnels_wc[0]); |
110 | if ((t != NULL) && (t->dev->flags & IFF_UP)) | 119 | if ((t != NULL) && (t->dev->flags & IFF_UP)) |
111 | return t; | 120 | return t; |
112 | return NULL; | 121 | return NULL; |
@@ -143,9 +152,9 @@ static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t) | |||
143 | 152 | ||
144 | for (tp = ipip6_bucket(sitn, t); *tp; tp = &(*tp)->next) { | 153 | for (tp = ipip6_bucket(sitn, t); *tp; tp = &(*tp)->next) { |
145 | if (t == *tp) { | 154 | if (t == *tp) { |
146 | write_lock_bh(&ipip6_lock); | 155 | spin_lock_bh(&ipip6_lock); |
147 | *tp = t->next; | 156 | *tp = t->next; |
148 | write_unlock_bh(&ipip6_lock); | 157 | spin_unlock_bh(&ipip6_lock); |
149 | break; | 158 | break; |
150 | } | 159 | } |
151 | } | 160 | } |
@@ -155,10 +164,27 @@ static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t) | |||
155 | { | 164 | { |
156 | struct ip_tunnel **tp = ipip6_bucket(sitn, t); | 165 | struct ip_tunnel **tp = ipip6_bucket(sitn, t); |
157 | 166 | ||
167 | spin_lock_bh(&ipip6_lock); | ||
158 | t->next = *tp; | 168 | t->next = *tp; |
159 | write_lock_bh(&ipip6_lock); | 169 | rcu_assign_pointer(*tp, t); |
160 | *tp = t; | 170 | spin_unlock_bh(&ipip6_lock); |
161 | write_unlock_bh(&ipip6_lock); | 171 | } |
172 | |||
173 | static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn) | ||
174 | { | ||
175 | #ifdef CONFIG_IPV6_SIT_6RD | ||
176 | struct ip_tunnel *t = netdev_priv(dev); | ||
177 | |||
178 | if (t->dev == sitn->fb_tunnel_dev) { | ||
179 | ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0); | ||
180 | t->ip6rd.relay_prefix = 0; | ||
181 | t->ip6rd.prefixlen = 16; | ||
182 | t->ip6rd.relay_prefixlen = 0; | ||
183 | } else { | ||
184 | struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev); | ||
185 | memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd)); | ||
186 | } | ||
187 | #endif | ||
162 | } | 188 | } |
163 | 189 | ||
164 | static struct ip_tunnel * ipip6_tunnel_locate(struct net *net, | 190 | static struct ip_tunnel * ipip6_tunnel_locate(struct net *net, |
@@ -204,6 +230,7 @@ static struct ip_tunnel * ipip6_tunnel_locate(struct net *net, | |||
204 | 230 | ||
205 | nt->parms = *parms; | 231 | nt->parms = *parms; |
206 | ipip6_tunnel_init(dev); | 232 | ipip6_tunnel_init(dev); |
233 | ipip6_tunnel_clone_6rd(dev, sitn); | ||
207 | 234 | ||
208 | if (parms->i_flags & SIT_ISATAP) | 235 | if (parms->i_flags & SIT_ISATAP) |
209 | dev->priv_flags |= IFF_ISATAP; | 236 | dev->priv_flags |= IFF_ISATAP; |
@@ -222,15 +249,22 @@ failed: | |||
222 | return NULL; | 249 | return NULL; |
223 | } | 250 | } |
224 | 251 | ||
252 | static DEFINE_SPINLOCK(ipip6_prl_lock); | ||
253 | |||
254 | #define for_each_prl_rcu(start) \ | ||
255 | for (prl = rcu_dereference(start); \ | ||
256 | prl; \ | ||
257 | prl = rcu_dereference(prl->next)) | ||
258 | |||
225 | static struct ip_tunnel_prl_entry * | 259 | static struct ip_tunnel_prl_entry * |
226 | __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr) | 260 | __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr) |
227 | { | 261 | { |
228 | struct ip_tunnel_prl_entry *p = (struct ip_tunnel_prl_entry *)NULL; | 262 | struct ip_tunnel_prl_entry *prl; |
229 | 263 | ||
230 | for (p = t->prl; p; p = p->next) | 264 | for_each_prl_rcu(t->prl) |
231 | if (p->addr == addr) | 265 | if (prl->addr == addr) |
232 | break; | 266 | break; |
233 | return p; | 267 | return prl; |
234 | 268 | ||
235 | } | 269 | } |
236 | 270 | ||
@@ -255,7 +289,7 @@ static int ipip6_tunnel_get_prl(struct ip_tunnel *t, | |||
255 | kcalloc(cmax, sizeof(*kp), GFP_KERNEL) : | 289 | kcalloc(cmax, sizeof(*kp), GFP_KERNEL) : |
256 | NULL; | 290 | NULL; |
257 | 291 | ||
258 | read_lock(&ipip6_lock); | 292 | rcu_read_lock(); |
259 | 293 | ||
260 | ca = t->prl_count < cmax ? t->prl_count : cmax; | 294 | ca = t->prl_count < cmax ? t->prl_count : cmax; |
261 | 295 | ||
@@ -273,7 +307,7 @@ static int ipip6_tunnel_get_prl(struct ip_tunnel *t, | |||
273 | } | 307 | } |
274 | 308 | ||
275 | c = 0; | 309 | c = 0; |
276 | for (prl = t->prl; prl; prl = prl->next) { | 310 | for_each_prl_rcu(t->prl) { |
277 | if (c >= cmax) | 311 | if (c >= cmax) |
278 | break; | 312 | break; |
279 | if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr) | 313 | if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr) |
@@ -285,7 +319,7 @@ static int ipip6_tunnel_get_prl(struct ip_tunnel *t, | |||
285 | break; | 319 | break; |
286 | } | 320 | } |
287 | out: | 321 | out: |
288 | read_unlock(&ipip6_lock); | 322 | rcu_read_unlock(); |
289 | 323 | ||
290 | len = sizeof(*kp) * c; | 324 | len = sizeof(*kp) * c; |
291 | ret = 0; | 325 | ret = 0; |
@@ -306,12 +340,14 @@ ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg) | |||
306 | if (a->addr == htonl(INADDR_ANY)) | 340 | if (a->addr == htonl(INADDR_ANY)) |
307 | return -EINVAL; | 341 | return -EINVAL; |
308 | 342 | ||
309 | write_lock(&ipip6_lock); | 343 | spin_lock(&ipip6_prl_lock); |
310 | 344 | ||
311 | for (p = t->prl; p; p = p->next) { | 345 | for (p = t->prl; p; p = p->next) { |
312 | if (p->addr == a->addr) { | 346 | if (p->addr == a->addr) { |
313 | if (chg) | 347 | if (chg) { |
314 | goto update; | 348 | p->flags = a->flags; |
349 | goto out; | ||
350 | } | ||
315 | err = -EEXIST; | 351 | err = -EEXIST; |
316 | goto out; | 352 | goto out; |
317 | } | 353 | } |
@@ -329,45 +365,61 @@ ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg) | |||
329 | } | 365 | } |
330 | 366 | ||
331 | p->next = t->prl; | 367 | p->next = t->prl; |
332 | t->prl = p; | ||
333 | t->prl_count++; | ||
334 | update: | ||
335 | p->addr = a->addr; | 368 | p->addr = a->addr; |
336 | p->flags = a->flags; | 369 | p->flags = a->flags; |
370 | t->prl_count++; | ||
371 | rcu_assign_pointer(t->prl, p); | ||
337 | out: | 372 | out: |
338 | write_unlock(&ipip6_lock); | 373 | spin_unlock(&ipip6_prl_lock); |
339 | return err; | 374 | return err; |
340 | } | 375 | } |
341 | 376 | ||
377 | static void prl_entry_destroy_rcu(struct rcu_head *head) | ||
378 | { | ||
379 | kfree(container_of(head, struct ip_tunnel_prl_entry, rcu_head)); | ||
380 | } | ||
381 | |||
382 | static void prl_list_destroy_rcu(struct rcu_head *head) | ||
383 | { | ||
384 | struct ip_tunnel_prl_entry *p, *n; | ||
385 | |||
386 | p = container_of(head, struct ip_tunnel_prl_entry, rcu_head); | ||
387 | do { | ||
388 | n = p->next; | ||
389 | kfree(p); | ||
390 | p = n; | ||
391 | } while (p); | ||
392 | } | ||
393 | |||
342 | static int | 394 | static int |
343 | ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a) | 395 | ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a) |
344 | { | 396 | { |
345 | struct ip_tunnel_prl_entry *x, **p; | 397 | struct ip_tunnel_prl_entry *x, **p; |
346 | int err = 0; | 398 | int err = 0; |
347 | 399 | ||
348 | write_lock(&ipip6_lock); | 400 | spin_lock(&ipip6_prl_lock); |
349 | 401 | ||
350 | if (a && a->addr != htonl(INADDR_ANY)) { | 402 | if (a && a->addr != htonl(INADDR_ANY)) { |
351 | for (p = &t->prl; *p; p = &(*p)->next) { | 403 | for (p = &t->prl; *p; p = &(*p)->next) { |
352 | if ((*p)->addr == a->addr) { | 404 | if ((*p)->addr == a->addr) { |
353 | x = *p; | 405 | x = *p; |
354 | *p = x->next; | 406 | *p = x->next; |
355 | kfree(x); | 407 | call_rcu(&x->rcu_head, prl_entry_destroy_rcu); |
356 | t->prl_count--; | 408 | t->prl_count--; |
357 | goto out; | 409 | goto out; |
358 | } | 410 | } |
359 | } | 411 | } |
360 | err = -ENXIO; | 412 | err = -ENXIO; |
361 | } else { | 413 | } else { |
362 | while (t->prl) { | 414 | if (t->prl) { |
415 | t->prl_count = 0; | ||
363 | x = t->prl; | 416 | x = t->prl; |
364 | t->prl = t->prl->next; | 417 | call_rcu(&x->rcu_head, prl_list_destroy_rcu); |
365 | kfree(x); | 418 | t->prl = NULL; |
366 | t->prl_count--; | ||
367 | } | 419 | } |
368 | } | 420 | } |
369 | out: | 421 | out: |
370 | write_unlock(&ipip6_lock); | 422 | spin_unlock(&ipip6_prl_lock); |
371 | return err; | 423 | return err; |
372 | } | 424 | } |
373 | 425 | ||
@@ -377,7 +429,7 @@ isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t) | |||
377 | struct ip_tunnel_prl_entry *p; | 429 | struct ip_tunnel_prl_entry *p; |
378 | int ok = 1; | 430 | int ok = 1; |
379 | 431 | ||
380 | read_lock(&ipip6_lock); | 432 | rcu_read_lock(); |
381 | p = __ipip6_tunnel_locate_prl(t, iph->saddr); | 433 | p = __ipip6_tunnel_locate_prl(t, iph->saddr); |
382 | if (p) { | 434 | if (p) { |
383 | if (p->flags & PRL_DEFAULT) | 435 | if (p->flags & PRL_DEFAULT) |
@@ -393,7 +445,7 @@ isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t) | |||
393 | else | 445 | else |
394 | ok = 0; | 446 | ok = 0; |
395 | } | 447 | } |
396 | read_unlock(&ipip6_lock); | 448 | rcu_read_unlock(); |
397 | return ok; | 449 | return ok; |
398 | } | 450 | } |
399 | 451 | ||
@@ -403,9 +455,9 @@ static void ipip6_tunnel_uninit(struct net_device *dev) | |||
403 | struct sit_net *sitn = net_generic(net, sit_net_id); | 455 | struct sit_net *sitn = net_generic(net, sit_net_id); |
404 | 456 | ||
405 | if (dev == sitn->fb_tunnel_dev) { | 457 | if (dev == sitn->fb_tunnel_dev) { |
406 | write_lock_bh(&ipip6_lock); | 458 | spin_lock_bh(&ipip6_lock); |
407 | sitn->tunnels_wc[0] = NULL; | 459 | sitn->tunnels_wc[0] = NULL; |
408 | write_unlock_bh(&ipip6_lock); | 460 | spin_unlock_bh(&ipip6_lock); |
409 | dev_put(dev); | 461 | dev_put(dev); |
410 | } else { | 462 | } else { |
411 | ipip6_tunnel_unlink(sitn, netdev_priv(dev)); | 463 | ipip6_tunnel_unlink(sitn, netdev_priv(dev)); |
@@ -458,7 +510,7 @@ static int ipip6_err(struct sk_buff *skb, u32 info) | |||
458 | 510 | ||
459 | err = -ENOENT; | 511 | err = -ENOENT; |
460 | 512 | ||
461 | read_lock(&ipip6_lock); | 513 | rcu_read_lock(); |
462 | t = ipip6_tunnel_lookup(dev_net(skb->dev), | 514 | t = ipip6_tunnel_lookup(dev_net(skb->dev), |
463 | skb->dev, | 515 | skb->dev, |
464 | iph->daddr, | 516 | iph->daddr, |
@@ -476,7 +528,7 @@ static int ipip6_err(struct sk_buff *skb, u32 info) | |||
476 | t->err_count = 1; | 528 | t->err_count = 1; |
477 | t->err_time = jiffies; | 529 | t->err_time = jiffies; |
478 | out: | 530 | out: |
479 | read_unlock(&ipip6_lock); | 531 | rcu_read_unlock(); |
480 | return err; | 532 | return err; |
481 | } | 533 | } |
482 | 534 | ||
@@ -496,7 +548,7 @@ static int ipip6_rcv(struct sk_buff *skb) | |||
496 | 548 | ||
497 | iph = ip_hdr(skb); | 549 | iph = ip_hdr(skb); |
498 | 550 | ||
499 | read_lock(&ipip6_lock); | 551 | rcu_read_lock(); |
500 | tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev, | 552 | tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev, |
501 | iph->saddr, iph->daddr); | 553 | iph->saddr, iph->daddr); |
502 | if (tunnel != NULL) { | 554 | if (tunnel != NULL) { |
@@ -510,7 +562,7 @@ static int ipip6_rcv(struct sk_buff *skb) | |||
510 | if ((tunnel->dev->priv_flags & IFF_ISATAP) && | 562 | if ((tunnel->dev->priv_flags & IFF_ISATAP) && |
511 | !isatap_chksrc(skb, iph, tunnel)) { | 563 | !isatap_chksrc(skb, iph, tunnel)) { |
512 | tunnel->dev->stats.rx_errors++; | 564 | tunnel->dev->stats.rx_errors++; |
513 | read_unlock(&ipip6_lock); | 565 | rcu_read_unlock(); |
514 | kfree_skb(skb); | 566 | kfree_skb(skb); |
515 | return 0; | 567 | return 0; |
516 | } | 568 | } |
@@ -521,28 +573,52 @@ static int ipip6_rcv(struct sk_buff *skb) | |||
521 | nf_reset(skb); | 573 | nf_reset(skb); |
522 | ipip6_ecn_decapsulate(iph, skb); | 574 | ipip6_ecn_decapsulate(iph, skb); |
523 | netif_rx(skb); | 575 | netif_rx(skb); |
524 | read_unlock(&ipip6_lock); | 576 | rcu_read_unlock(); |
525 | return 0; | 577 | return 0; |
526 | } | 578 | } |
527 | 579 | ||
528 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); | 580 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0); |
529 | read_unlock(&ipip6_lock); | 581 | rcu_read_unlock(); |
530 | out: | 582 | out: |
531 | kfree_skb(skb); | 583 | kfree_skb(skb); |
532 | return 0; | 584 | return 0; |
533 | } | 585 | } |
534 | 586 | ||
535 | /* Returns the embedded IPv4 address if the IPv6 address | 587 | /* |
536 | comes from 6to4 (RFC 3056) addr space */ | 588 | * Returns the embedded IPv4 address if the IPv6 address |
537 | 589 | * comes from 6rd / 6to4 (RFC 3056) addr space. | |
538 | static inline __be32 try_6to4(struct in6_addr *v6dst) | 590 | */ |
591 | static inline | ||
592 | __be32 try_6rd(struct in6_addr *v6dst, struct ip_tunnel *tunnel) | ||
539 | { | 593 | { |
540 | __be32 dst = 0; | 594 | __be32 dst = 0; |
541 | 595 | ||
596 | #ifdef CONFIG_IPV6_SIT_6RD | ||
597 | if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix, | ||
598 | tunnel->ip6rd.prefixlen)) { | ||
599 | unsigned pbw0, pbi0; | ||
600 | int pbi1; | ||
601 | u32 d; | ||
602 | |||
603 | pbw0 = tunnel->ip6rd.prefixlen >> 5; | ||
604 | pbi0 = tunnel->ip6rd.prefixlen & 0x1f; | ||
605 | |||
606 | d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >> | ||
607 | tunnel->ip6rd.relay_prefixlen; | ||
608 | |||
609 | pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen; | ||
610 | if (pbi1 > 0) | ||
611 | d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >> | ||
612 | (32 - pbi1); | ||
613 | |||
614 | dst = tunnel->ip6rd.relay_prefix | htonl(d); | ||
615 | } | ||
616 | #else | ||
542 | if (v6dst->s6_addr16[0] == htons(0x2002)) { | 617 | if (v6dst->s6_addr16[0] == htons(0x2002)) { |
543 | /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */ | 618 | /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */ |
544 | memcpy(&dst, &v6dst->s6_addr16[1], 4); | 619 | memcpy(&dst, &v6dst->s6_addr16[1], 4); |
545 | } | 620 | } |
621 | #endif | ||
546 | return dst; | 622 | return dst; |
547 | } | 623 | } |
548 | 624 | ||
@@ -555,10 +631,12 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb, | |||
555 | struct net_device *dev) | 631 | struct net_device *dev) |
556 | { | 632 | { |
557 | struct ip_tunnel *tunnel = netdev_priv(dev); | 633 | struct ip_tunnel *tunnel = netdev_priv(dev); |
558 | struct net_device_stats *stats = &tunnel->dev->stats; | 634 | struct net_device_stats *stats = &dev->stats; |
635 | struct netdev_queue *txq = netdev_get_tx_queue(dev, 0); | ||
559 | struct iphdr *tiph = &tunnel->parms.iph; | 636 | struct iphdr *tiph = &tunnel->parms.iph; |
560 | struct ipv6hdr *iph6 = ipv6_hdr(skb); | 637 | struct ipv6hdr *iph6 = ipv6_hdr(skb); |
561 | u8 tos = tunnel->parms.iph.tos; | 638 | u8 tos = tunnel->parms.iph.tos; |
639 | __be16 df = tiph->frag_off; | ||
562 | struct rtable *rt; /* Route to the other host */ | 640 | struct rtable *rt; /* Route to the other host */ |
563 | struct net_device *tdev; /* Device to other host */ | 641 | struct net_device *tdev; /* Device to other host */ |
564 | struct iphdr *iph; /* Our new IP header */ | 642 | struct iphdr *iph; /* Our new IP header */ |
@@ -595,7 +673,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb, | |||
595 | } | 673 | } |
596 | 674 | ||
597 | if (!dst) | 675 | if (!dst) |
598 | dst = try_6to4(&iph6->daddr); | 676 | dst = try_6rd(&iph6->daddr, tunnel); |
599 | 677 | ||
600 | if (!dst) { | 678 | if (!dst) { |
601 | struct neighbour *neigh = NULL; | 679 | struct neighbour *neigh = NULL; |
@@ -648,25 +726,28 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb, | |||
648 | goto tx_error; | 726 | goto tx_error; |
649 | } | 727 | } |
650 | 728 | ||
651 | if (tiph->frag_off) | 729 | if (df) { |
652 | mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr); | 730 | mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr); |
653 | else | ||
654 | mtu = skb_dst(skb) ? dst_mtu(skb_dst(skb)) : dev->mtu; | ||
655 | 731 | ||
656 | if (mtu < 68) { | 732 | if (mtu < 68) { |
657 | stats->collisions++; | 733 | stats->collisions++; |
658 | ip_rt_put(rt); | 734 | ip_rt_put(rt); |
659 | goto tx_error; | 735 | goto tx_error; |
660 | } | 736 | } |
661 | if (mtu < IPV6_MIN_MTU) | ||
662 | mtu = IPV6_MIN_MTU; | ||
663 | if (tunnel->parms.iph.daddr && skb_dst(skb)) | ||
664 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu); | ||
665 | 737 | ||
666 | if (skb->len > mtu) { | 738 | if (mtu < IPV6_MIN_MTU) { |
667 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev); | 739 | mtu = IPV6_MIN_MTU; |
668 | ip_rt_put(rt); | 740 | df = 0; |
669 | goto tx_error; | 741 | } |
742 | |||
743 | if (tunnel->parms.iph.daddr && skb_dst(skb)) | ||
744 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu); | ||
745 | |||
746 | if (skb->len > mtu) { | ||
747 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); | ||
748 | ip_rt_put(rt); | ||
749 | goto tx_error; | ||
750 | } | ||
670 | } | 751 | } |
671 | 752 | ||
672 | if (tunnel->err_count > 0) { | 753 | if (tunnel->err_count > 0) { |
@@ -688,7 +769,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb, | |||
688 | struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); | 769 | struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom); |
689 | if (!new_skb) { | 770 | if (!new_skb) { |
690 | ip_rt_put(rt); | 771 | ip_rt_put(rt); |
691 | stats->tx_dropped++; | 772 | txq->tx_dropped++; |
692 | dev_kfree_skb(skb); | 773 | dev_kfree_skb(skb); |
693 | return NETDEV_TX_OK; | 774 | return NETDEV_TX_OK; |
694 | } | 775 | } |
@@ -714,11 +795,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb, | |||
714 | iph = ip_hdr(skb); | 795 | iph = ip_hdr(skb); |
715 | iph->version = 4; | 796 | iph->version = 4; |
716 | iph->ihl = sizeof(struct iphdr)>>2; | 797 | iph->ihl = sizeof(struct iphdr)>>2; |
717 | if (mtu > IPV6_MIN_MTU) | 798 | iph->frag_off = df; |
718 | iph->frag_off = tiph->frag_off; | ||
719 | else | ||
720 | iph->frag_off = 0; | ||
721 | |||
722 | iph->protocol = IPPROTO_IPV6; | 799 | iph->protocol = IPPROTO_IPV6; |
723 | iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6)); | 800 | iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6)); |
724 | iph->daddr = rt->rt_dst; | 801 | iph->daddr = rt->rt_dst; |
@@ -785,9 +862,15 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) | |||
785 | struct ip_tunnel *t; | 862 | struct ip_tunnel *t; |
786 | struct net *net = dev_net(dev); | 863 | struct net *net = dev_net(dev); |
787 | struct sit_net *sitn = net_generic(net, sit_net_id); | 864 | struct sit_net *sitn = net_generic(net, sit_net_id); |
865 | #ifdef CONFIG_IPV6_SIT_6RD | ||
866 | struct ip_tunnel_6rd ip6rd; | ||
867 | #endif | ||
788 | 868 | ||
789 | switch (cmd) { | 869 | switch (cmd) { |
790 | case SIOCGETTUNNEL: | 870 | case SIOCGETTUNNEL: |
871 | #ifdef CONFIG_IPV6_SIT_6RD | ||
872 | case SIOCGET6RD: | ||
873 | #endif | ||
791 | t = NULL; | 874 | t = NULL; |
792 | if (dev == sitn->fb_tunnel_dev) { | 875 | if (dev == sitn->fb_tunnel_dev) { |
793 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { | 876 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { |
@@ -798,9 +881,25 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) | |||
798 | } | 881 | } |
799 | if (t == NULL) | 882 | if (t == NULL) |
800 | t = netdev_priv(dev); | 883 | t = netdev_priv(dev); |
801 | memcpy(&p, &t->parms, sizeof(p)); | 884 | |
802 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) | 885 | err = -EFAULT; |
803 | err = -EFAULT; | 886 | if (cmd == SIOCGETTUNNEL) { |
887 | memcpy(&p, &t->parms, sizeof(p)); | ||
888 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, | ||
889 | sizeof(p))) | ||
890 | goto done; | ||
891 | #ifdef CONFIG_IPV6_SIT_6RD | ||
892 | } else { | ||
893 | ipv6_addr_copy(&ip6rd.prefix, &t->ip6rd.prefix); | ||
894 | ip6rd.relay_prefix = t->ip6rd.relay_prefix; | ||
895 | ip6rd.prefixlen = t->ip6rd.prefixlen; | ||
896 | ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen; | ||
897 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd, | ||
898 | sizeof(ip6rd))) | ||
899 | goto done; | ||
900 | #endif | ||
901 | } | ||
902 | err = 0; | ||
804 | break; | 903 | break; |
805 | 904 | ||
806 | case SIOCADDTUNNEL: | 905 | case SIOCADDTUNNEL: |
@@ -921,6 +1020,54 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd) | |||
921 | netdev_state_change(dev); | 1020 | netdev_state_change(dev); |
922 | break; | 1021 | break; |
923 | 1022 | ||
1023 | #ifdef CONFIG_IPV6_SIT_6RD | ||
1024 | case SIOCADD6RD: | ||
1025 | case SIOCCHG6RD: | ||
1026 | case SIOCDEL6RD: | ||
1027 | err = -EPERM; | ||
1028 | if (!capable(CAP_NET_ADMIN)) | ||
1029 | goto done; | ||
1030 | |||
1031 | err = -EFAULT; | ||
1032 | if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data, | ||
1033 | sizeof(ip6rd))) | ||
1034 | goto done; | ||
1035 | |||
1036 | t = netdev_priv(dev); | ||
1037 | |||
1038 | if (cmd != SIOCDEL6RD) { | ||
1039 | struct in6_addr prefix; | ||
1040 | __be32 relay_prefix; | ||
1041 | |||
1042 | err = -EINVAL; | ||
1043 | if (ip6rd.relay_prefixlen > 32 || | ||
1044 | ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64) | ||
1045 | goto done; | ||
1046 | |||
1047 | ipv6_addr_prefix(&prefix, &ip6rd.prefix, | ||
1048 | ip6rd.prefixlen); | ||
1049 | if (!ipv6_addr_equal(&prefix, &ip6rd.prefix)) | ||
1050 | goto done; | ||
1051 | if (ip6rd.relay_prefixlen) | ||
1052 | relay_prefix = ip6rd.relay_prefix & | ||
1053 | htonl(0xffffffffUL << | ||
1054 | (32 - ip6rd.relay_prefixlen)); | ||
1055 | else | ||
1056 | relay_prefix = 0; | ||
1057 | if (relay_prefix != ip6rd.relay_prefix) | ||
1058 | goto done; | ||
1059 | |||
1060 | ipv6_addr_copy(&t->ip6rd.prefix, &prefix); | ||
1061 | t->ip6rd.relay_prefix = relay_prefix; | ||
1062 | t->ip6rd.prefixlen = ip6rd.prefixlen; | ||
1063 | t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen; | ||
1064 | } else | ||
1065 | ipip6_tunnel_clone_6rd(dev, sitn); | ||
1066 | |||
1067 | err = 0; | ||
1068 | break; | ||
1069 | #endif | ||
1070 | |||
924 | default: | 1071 | default: |
925 | err = -EINVAL; | 1072 | err = -EINVAL; |
926 | } | 1073 | } |
@@ -972,7 +1119,7 @@ static void ipip6_tunnel_init(struct net_device *dev) | |||
972 | ipip6_tunnel_bind_dev(dev); | 1119 | ipip6_tunnel_bind_dev(dev); |
973 | } | 1120 | } |
974 | 1121 | ||
975 | static void ipip6_fb_tunnel_init(struct net_device *dev) | 1122 | static void __net_init ipip6_fb_tunnel_init(struct net_device *dev) |
976 | { | 1123 | { |
977 | struct ip_tunnel *tunnel = netdev_priv(dev); | 1124 | struct ip_tunnel *tunnel = netdev_priv(dev); |
978 | struct iphdr *iph = &tunnel->parms.iph; | 1125 | struct iphdr *iph = &tunnel->parms.iph; |
@@ -997,33 +1144,27 @@ static struct xfrm_tunnel sit_handler = { | |||
997 | .priority = 1, | 1144 | .priority = 1, |
998 | }; | 1145 | }; |
999 | 1146 | ||
1000 | static void sit_destroy_tunnels(struct sit_net *sitn) | 1147 | static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head) |
1001 | { | 1148 | { |
1002 | int prio; | 1149 | int prio; |
1003 | 1150 | ||
1004 | for (prio = 1; prio < 4; prio++) { | 1151 | for (prio = 1; prio < 4; prio++) { |
1005 | int h; | 1152 | int h; |
1006 | for (h = 0; h < HASH_SIZE; h++) { | 1153 | for (h = 0; h < HASH_SIZE; h++) { |
1007 | struct ip_tunnel *t; | 1154 | struct ip_tunnel *t = sitn->tunnels[prio][h]; |
1008 | while ((t = sitn->tunnels[prio][h]) != NULL) | 1155 | |
1009 | unregister_netdevice(t->dev); | 1156 | while (t != NULL) { |
1157 | unregister_netdevice_queue(t->dev, head); | ||
1158 | t = t->next; | ||
1159 | } | ||
1010 | } | 1160 | } |
1011 | } | 1161 | } |
1012 | } | 1162 | } |
1013 | 1163 | ||
1014 | static int sit_init_net(struct net *net) | 1164 | static int __net_init sit_init_net(struct net *net) |
1015 | { | 1165 | { |
1166 | struct sit_net *sitn = net_generic(net, sit_net_id); | ||
1016 | int err; | 1167 | int err; |
1017 | struct sit_net *sitn; | ||
1018 | |||
1019 | err = -ENOMEM; | ||
1020 | sitn = kzalloc(sizeof(struct sit_net), GFP_KERNEL); | ||
1021 | if (sitn == NULL) | ||
1022 | goto err_alloc; | ||
1023 | |||
1024 | err = net_assign_generic(net, sit_net_id, sitn); | ||
1025 | if (err < 0) | ||
1026 | goto err_assign; | ||
1027 | 1168 | ||
1028 | sitn->tunnels[0] = sitn->tunnels_wc; | 1169 | sitn->tunnels[0] = sitn->tunnels_wc; |
1029 | sitn->tunnels[1] = sitn->tunnels_l; | 1170 | sitn->tunnels[1] = sitn->tunnels_l; |
@@ -1039,6 +1180,7 @@ static int sit_init_net(struct net *net) | |||
1039 | dev_net_set(sitn->fb_tunnel_dev, net); | 1180 | dev_net_set(sitn->fb_tunnel_dev, net); |
1040 | 1181 | ||
1041 | ipip6_fb_tunnel_init(sitn->fb_tunnel_dev); | 1182 | ipip6_fb_tunnel_init(sitn->fb_tunnel_dev); |
1183 | ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn); | ||
1042 | 1184 | ||
1043 | if ((err = register_netdev(sitn->fb_tunnel_dev))) | 1185 | if ((err = register_netdev(sitn->fb_tunnel_dev))) |
1044 | goto err_reg_dev; | 1186 | goto err_reg_dev; |
@@ -1049,35 +1191,34 @@ err_reg_dev: | |||
1049 | dev_put(sitn->fb_tunnel_dev); | 1191 | dev_put(sitn->fb_tunnel_dev); |
1050 | free_netdev(sitn->fb_tunnel_dev); | 1192 | free_netdev(sitn->fb_tunnel_dev); |
1051 | err_alloc_dev: | 1193 | err_alloc_dev: |
1052 | /* nothing */ | ||
1053 | err_assign: | ||
1054 | kfree(sitn); | ||
1055 | err_alloc: | ||
1056 | return err; | 1194 | return err; |
1057 | } | 1195 | } |
1058 | 1196 | ||
1059 | static void sit_exit_net(struct net *net) | 1197 | static void __net_exit sit_exit_net(struct net *net) |
1060 | { | 1198 | { |
1061 | struct sit_net *sitn; | 1199 | struct sit_net *sitn = net_generic(net, sit_net_id); |
1200 | LIST_HEAD(list); | ||
1062 | 1201 | ||
1063 | sitn = net_generic(net, sit_net_id); | ||
1064 | rtnl_lock(); | 1202 | rtnl_lock(); |
1065 | sit_destroy_tunnels(sitn); | 1203 | sit_destroy_tunnels(sitn, &list); |
1066 | unregister_netdevice(sitn->fb_tunnel_dev); | 1204 | unregister_netdevice_queue(sitn->fb_tunnel_dev, &list); |
1205 | unregister_netdevice_many(&list); | ||
1067 | rtnl_unlock(); | 1206 | rtnl_unlock(); |
1068 | kfree(sitn); | ||
1069 | } | 1207 | } |
1070 | 1208 | ||
1071 | static struct pernet_operations sit_net_ops = { | 1209 | static struct pernet_operations sit_net_ops = { |
1072 | .init = sit_init_net, | 1210 | .init = sit_init_net, |
1073 | .exit = sit_exit_net, | 1211 | .exit = sit_exit_net, |
1212 | .id = &sit_net_id, | ||
1213 | .size = sizeof(struct sit_net), | ||
1074 | }; | 1214 | }; |
1075 | 1215 | ||
1076 | static void __exit sit_cleanup(void) | 1216 | static void __exit sit_cleanup(void) |
1077 | { | 1217 | { |
1078 | xfrm4_tunnel_deregister(&sit_handler, AF_INET6); | 1218 | xfrm4_tunnel_deregister(&sit_handler, AF_INET6); |
1079 | 1219 | ||
1080 | unregister_pernet_gen_device(sit_net_id, &sit_net_ops); | 1220 | unregister_pernet_device(&sit_net_ops); |
1221 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ | ||
1081 | } | 1222 | } |
1082 | 1223 | ||
1083 | static int __init sit_init(void) | 1224 | static int __init sit_init(void) |
@@ -1086,15 +1227,14 @@ static int __init sit_init(void) | |||
1086 | 1227 | ||
1087 | printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n"); | 1228 | printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n"); |
1088 | 1229 | ||
1089 | if (xfrm4_tunnel_register(&sit_handler, AF_INET6) < 0) { | 1230 | err = register_pernet_device(&sit_net_ops); |
1231 | if (err < 0) | ||
1232 | return err; | ||
1233 | err = xfrm4_tunnel_register(&sit_handler, AF_INET6); | ||
1234 | if (err < 0) { | ||
1235 | unregister_pernet_device(&sit_net_ops); | ||
1090 | printk(KERN_INFO "sit init: Can't add protocol\n"); | 1236 | printk(KERN_INFO "sit init: Can't add protocol\n"); |
1091 | return -EAGAIN; | ||
1092 | } | 1237 | } |
1093 | |||
1094 | err = register_pernet_gen_device(&sit_net_id, &sit_net_ops); | ||
1095 | if (err < 0) | ||
1096 | xfrm4_tunnel_deregister(&sit_handler, AF_INET6); | ||
1097 | |||
1098 | return err; | 1238 | return err; |
1099 | } | 1239 | } |
1100 | 1240 | ||