net: Convert net_ratelimit uses to net_<level>_ratelimited
[deliverable/linux.git] / net / ipv6 / sit.c
1 /*
2 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * Changes:
15 * Roger Venning <r.venning@telstra.com>: 6to4 support
16 * Nate Thompson <nate@thebog.net>: 6to4 support
17 * Fred Templin <fred.l.templin@boeing.com>: isatap support
18 */
19
20 #include <linux/module.h>
21 #include <linux/capability.h>
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/socket.h>
25 #include <linux/sockios.h>
26 #include <linux/net.h>
27 #include <linux/in6.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/icmp.h>
31 #include <linux/slab.h>
32 #include <asm/uaccess.h>
33 #include <linux/init.h>
34 #include <linux/netfilter_ipv4.h>
35 #include <linux/if_ether.h>
36
37 #include <net/sock.h>
38 #include <net/snmp.h>
39
40 #include <net/ipv6.h>
41 #include <net/protocol.h>
42 #include <net/transp_v6.h>
43 #include <net/ip6_fib.h>
44 #include <net/ip6_route.h>
45 #include <net/ndisc.h>
46 #include <net/addrconf.h>
47 #include <net/ip.h>
48 #include <net/udp.h>
49 #include <net/icmp.h>
50 #include <net/ipip.h>
51 #include <net/inet_ecn.h>
52 #include <net/xfrm.h>
53 #include <net/dsfield.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
56
57 /*
58 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
59
60 For comments look at net/ipv4/ip_gre.c --ANK
61 */
62
63 #define HASH_SIZE 16
64 #define HASH(addr) (((__force u32)addr^((__force u32)addr>>4))&0xF)
65
66 static int ipip6_tunnel_init(struct net_device *dev);
67 static void ipip6_tunnel_setup(struct net_device *dev);
68 static void ipip6_dev_free(struct net_device *dev);
69
70 static int sit_net_id __read_mostly;
71 struct sit_net {
72 struct ip_tunnel __rcu *tunnels_r_l[HASH_SIZE];
73 struct ip_tunnel __rcu *tunnels_r[HASH_SIZE];
74 struct ip_tunnel __rcu *tunnels_l[HASH_SIZE];
75 struct ip_tunnel __rcu *tunnels_wc[1];
76 struct ip_tunnel __rcu **tunnels[4];
77
78 struct net_device *fb_tunnel_dev;
79 };
80
81 /*
82 * Locking : hash tables are protected by RCU and RTNL
83 */
84
85 #define for_each_ip_tunnel_rcu(start) \
86 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
87
88 /* often modified stats are per cpu, other are shared (netdev->stats) */
89 struct pcpu_tstats {
90 u64 rx_packets;
91 u64 rx_bytes;
92 u64 tx_packets;
93 u64 tx_bytes;
94 struct u64_stats_sync syncp;
95 };
96
97 static struct rtnl_link_stats64 *ipip6_get_stats64(struct net_device *dev,
98 struct rtnl_link_stats64 *tot)
99 {
100 int i;
101
102 for_each_possible_cpu(i) {
103 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
104 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
105 unsigned int start;
106
107 do {
108 start = u64_stats_fetch_begin_bh(&tstats->syncp);
109 rx_packets = tstats->rx_packets;
110 tx_packets = tstats->tx_packets;
111 rx_bytes = tstats->rx_bytes;
112 tx_bytes = tstats->tx_bytes;
113 } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
114
115 tot->rx_packets += rx_packets;
116 tot->tx_packets += tx_packets;
117 tot->rx_bytes += rx_bytes;
118 tot->tx_bytes += tx_bytes;
119 }
120
121 tot->rx_errors = dev->stats.rx_errors;
122 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
123 tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
124 tot->tx_dropped = dev->stats.tx_dropped;
125 tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
126 tot->tx_errors = dev->stats.tx_errors;
127
128 return tot;
129 }
130
131 /*
132 * Must be invoked with rcu_read_lock
133 */
134 static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
135 struct net_device *dev, __be32 remote, __be32 local)
136 {
137 unsigned int h0 = HASH(remote);
138 unsigned int h1 = HASH(local);
139 struct ip_tunnel *t;
140 struct sit_net *sitn = net_generic(net, sit_net_id);
141
142 for_each_ip_tunnel_rcu(sitn->tunnels_r_l[h0 ^ h1]) {
143 if (local == t->parms.iph.saddr &&
144 remote == t->parms.iph.daddr &&
145 (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
146 (t->dev->flags & IFF_UP))
147 return t;
148 }
149 for_each_ip_tunnel_rcu(sitn->tunnels_r[h0]) {
150 if (remote == t->parms.iph.daddr &&
151 (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
152 (t->dev->flags & IFF_UP))
153 return t;
154 }
155 for_each_ip_tunnel_rcu(sitn->tunnels_l[h1]) {
156 if (local == t->parms.iph.saddr &&
157 (!dev || !t->parms.link || dev->iflink == t->parms.link) &&
158 (t->dev->flags & IFF_UP))
159 return t;
160 }
161 t = rcu_dereference(sitn->tunnels_wc[0]);
162 if ((t != NULL) && (t->dev->flags & IFF_UP))
163 return t;
164 return NULL;
165 }
166
167 static struct ip_tunnel __rcu **__ipip6_bucket(struct sit_net *sitn,
168 struct ip_tunnel_parm *parms)
169 {
170 __be32 remote = parms->iph.daddr;
171 __be32 local = parms->iph.saddr;
172 unsigned int h = 0;
173 int prio = 0;
174
175 if (remote) {
176 prio |= 2;
177 h ^= HASH(remote);
178 }
179 if (local) {
180 prio |= 1;
181 h ^= HASH(local);
182 }
183 return &sitn->tunnels[prio][h];
184 }
185
186 static inline struct ip_tunnel __rcu **ipip6_bucket(struct sit_net *sitn,
187 struct ip_tunnel *t)
188 {
189 return __ipip6_bucket(sitn, &t->parms);
190 }
191
192 static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
193 {
194 struct ip_tunnel __rcu **tp;
195 struct ip_tunnel *iter;
196
197 for (tp = ipip6_bucket(sitn, t);
198 (iter = rtnl_dereference(*tp)) != NULL;
199 tp = &iter->next) {
200 if (t == iter) {
201 rcu_assign_pointer(*tp, t->next);
202 break;
203 }
204 }
205 }
206
207 static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
208 {
209 struct ip_tunnel __rcu **tp = ipip6_bucket(sitn, t);
210
211 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
212 rcu_assign_pointer(*tp, t);
213 }
214
215 static void ipip6_tunnel_clone_6rd(struct net_device *dev, struct sit_net *sitn)
216 {
217 #ifdef CONFIG_IPV6_SIT_6RD
218 struct ip_tunnel *t = netdev_priv(dev);
219
220 if (t->dev == sitn->fb_tunnel_dev) {
221 ipv6_addr_set(&t->ip6rd.prefix, htonl(0x20020000), 0, 0, 0);
222 t->ip6rd.relay_prefix = 0;
223 t->ip6rd.prefixlen = 16;
224 t->ip6rd.relay_prefixlen = 0;
225 } else {
226 struct ip_tunnel *t0 = netdev_priv(sitn->fb_tunnel_dev);
227 memcpy(&t->ip6rd, &t0->ip6rd, sizeof(t->ip6rd));
228 }
229 #endif
230 }
231
232 static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
233 struct ip_tunnel_parm *parms, int create)
234 {
235 __be32 remote = parms->iph.daddr;
236 __be32 local = parms->iph.saddr;
237 struct ip_tunnel *t, *nt;
238 struct ip_tunnel __rcu **tp;
239 struct net_device *dev;
240 char name[IFNAMSIZ];
241 struct sit_net *sitn = net_generic(net, sit_net_id);
242
243 for (tp = __ipip6_bucket(sitn, parms);
244 (t = rtnl_dereference(*tp)) != NULL;
245 tp = &t->next) {
246 if (local == t->parms.iph.saddr &&
247 remote == t->parms.iph.daddr &&
248 parms->link == t->parms.link) {
249 if (create)
250 return NULL;
251 else
252 return t;
253 }
254 }
255 if (!create)
256 goto failed;
257
258 if (parms->name[0])
259 strlcpy(name, parms->name, IFNAMSIZ);
260 else
261 strcpy(name, "sit%d");
262
263 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
264 if (dev == NULL)
265 return NULL;
266
267 dev_net_set(dev, net);
268
269 nt = netdev_priv(dev);
270
271 nt->parms = *parms;
272 if (ipip6_tunnel_init(dev) < 0)
273 goto failed_free;
274 ipip6_tunnel_clone_6rd(dev, sitn);
275
276 if (parms->i_flags & SIT_ISATAP)
277 dev->priv_flags |= IFF_ISATAP;
278
279 if (register_netdevice(dev) < 0)
280 goto failed_free;
281
282 strcpy(nt->parms.name, dev->name);
283
284 dev_hold(dev);
285
286 ipip6_tunnel_link(sitn, nt);
287 return nt;
288
289 failed_free:
290 ipip6_dev_free(dev);
291 failed:
292 return NULL;
293 }
294
295 #define for_each_prl_rcu(start) \
296 for (prl = rcu_dereference(start); \
297 prl; \
298 prl = rcu_dereference(prl->next))
299
300 static struct ip_tunnel_prl_entry *
301 __ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
302 {
303 struct ip_tunnel_prl_entry *prl;
304
305 for_each_prl_rcu(t->prl)
306 if (prl->addr == addr)
307 break;
308 return prl;
309
310 }
311
312 static int ipip6_tunnel_get_prl(struct ip_tunnel *t,
313 struct ip_tunnel_prl __user *a)
314 {
315 struct ip_tunnel_prl kprl, *kp;
316 struct ip_tunnel_prl_entry *prl;
317 unsigned int cmax, c = 0, ca, len;
318 int ret = 0;
319
320 if (copy_from_user(&kprl, a, sizeof(kprl)))
321 return -EFAULT;
322 cmax = kprl.datalen / sizeof(kprl);
323 if (cmax > 1 && kprl.addr != htonl(INADDR_ANY))
324 cmax = 1;
325
326 /* For simple GET or for root users,
327 * we try harder to allocate.
328 */
329 kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
330 kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
331 NULL;
332
333 rcu_read_lock();
334
335 ca = t->prl_count < cmax ? t->prl_count : cmax;
336
337 if (!kp) {
338 /* We don't try hard to allocate much memory for
339 * non-root users.
340 * For root users, retry allocating enough memory for
341 * the answer.
342 */
343 kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
344 if (!kp) {
345 ret = -ENOMEM;
346 goto out;
347 }
348 }
349
350 c = 0;
351 for_each_prl_rcu(t->prl) {
352 if (c >= cmax)
353 break;
354 if (kprl.addr != htonl(INADDR_ANY) && prl->addr != kprl.addr)
355 continue;
356 kp[c].addr = prl->addr;
357 kp[c].flags = prl->flags;
358 c++;
359 if (kprl.addr != htonl(INADDR_ANY))
360 break;
361 }
362 out:
363 rcu_read_unlock();
364
365 len = sizeof(*kp) * c;
366 ret = 0;
367 if ((len && copy_to_user(a + 1, kp, len)) || put_user(len, &a->datalen))
368 ret = -EFAULT;
369
370 kfree(kp);
371
372 return ret;
373 }
374
375 static int
376 ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
377 {
378 struct ip_tunnel_prl_entry *p;
379 int err = 0;
380
381 if (a->addr == htonl(INADDR_ANY))
382 return -EINVAL;
383
384 ASSERT_RTNL();
385
386 for (p = rtnl_dereference(t->prl); p; p = rtnl_dereference(p->next)) {
387 if (p->addr == a->addr) {
388 if (chg) {
389 p->flags = a->flags;
390 goto out;
391 }
392 err = -EEXIST;
393 goto out;
394 }
395 }
396
397 if (chg) {
398 err = -ENXIO;
399 goto out;
400 }
401
402 p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
403 if (!p) {
404 err = -ENOBUFS;
405 goto out;
406 }
407
408 p->next = t->prl;
409 p->addr = a->addr;
410 p->flags = a->flags;
411 t->prl_count++;
412 rcu_assign_pointer(t->prl, p);
413 out:
414 return err;
415 }
416
417 static void prl_list_destroy_rcu(struct rcu_head *head)
418 {
419 struct ip_tunnel_prl_entry *p, *n;
420
421 p = container_of(head, struct ip_tunnel_prl_entry, rcu_head);
422 do {
423 n = rcu_dereference_protected(p->next, 1);
424 kfree(p);
425 p = n;
426 } while (p);
427 }
428
429 static int
430 ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
431 {
432 struct ip_tunnel_prl_entry *x;
433 struct ip_tunnel_prl_entry __rcu **p;
434 int err = 0;
435
436 ASSERT_RTNL();
437
438 if (a && a->addr != htonl(INADDR_ANY)) {
439 for (p = &t->prl;
440 (x = rtnl_dereference(*p)) != NULL;
441 p = &x->next) {
442 if (x->addr == a->addr) {
443 *p = x->next;
444 kfree_rcu(x, rcu_head);
445 t->prl_count--;
446 goto out;
447 }
448 }
449 err = -ENXIO;
450 } else {
451 x = rtnl_dereference(t->prl);
452 if (x) {
453 t->prl_count = 0;
454 call_rcu(&x->rcu_head, prl_list_destroy_rcu);
455 t->prl = NULL;
456 }
457 }
458 out:
459 return err;
460 }
461
462 static int
463 isatap_chksrc(struct sk_buff *skb, const struct iphdr *iph, struct ip_tunnel *t)
464 {
465 struct ip_tunnel_prl_entry *p;
466 int ok = 1;
467
468 rcu_read_lock();
469 p = __ipip6_tunnel_locate_prl(t, iph->saddr);
470 if (p) {
471 if (p->flags & PRL_DEFAULT)
472 skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
473 else
474 skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
475 } else {
476 const struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
477
478 if (ipv6_addr_is_isatap(addr6) &&
479 (addr6->s6_addr32[3] == iph->saddr) &&
480 ipv6_chk_prefix(addr6, t->dev))
481 skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
482 else
483 ok = 0;
484 }
485 rcu_read_unlock();
486 return ok;
487 }
488
489 static void ipip6_tunnel_uninit(struct net_device *dev)
490 {
491 struct net *net = dev_net(dev);
492 struct sit_net *sitn = net_generic(net, sit_net_id);
493
494 if (dev == sitn->fb_tunnel_dev) {
495 RCU_INIT_POINTER(sitn->tunnels_wc[0], NULL);
496 } else {
497 ipip6_tunnel_unlink(sitn, netdev_priv(dev));
498 ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
499 }
500 dev_put(dev);
501 }
502
503
504 static int ipip6_err(struct sk_buff *skb, u32 info)
505 {
506
507 /* All the routers (except for Linux) return only
508 8 bytes of packet payload. It means, that precise relaying of
509 ICMP in the real Internet is absolutely infeasible.
510 */
511 const struct iphdr *iph = (const struct iphdr *)skb->data;
512 const int type = icmp_hdr(skb)->type;
513 const int code = icmp_hdr(skb)->code;
514 struct ip_tunnel *t;
515 int err;
516
517 switch (type) {
518 default:
519 case ICMP_PARAMETERPROB:
520 return 0;
521
522 case ICMP_DEST_UNREACH:
523 switch (code) {
524 case ICMP_SR_FAILED:
525 case ICMP_PORT_UNREACH:
526 /* Impossible event. */
527 return 0;
528 case ICMP_FRAG_NEEDED:
529 /* Soft state for pmtu is maintained by IP core. */
530 return 0;
531 default:
532 /* All others are translated to HOST_UNREACH.
533 rfc2003 contains "deep thoughts" about NET_UNREACH,
534 I believe they are just ether pollution. --ANK
535 */
536 break;
537 }
538 break;
539 case ICMP_TIME_EXCEEDED:
540 if (code != ICMP_EXC_TTL)
541 return 0;
542 break;
543 }
544
545 err = -ENOENT;
546
547 rcu_read_lock();
548 t = ipip6_tunnel_lookup(dev_net(skb->dev),
549 skb->dev,
550 iph->daddr,
551 iph->saddr);
552 if (t == NULL || t->parms.iph.daddr == 0)
553 goto out;
554
555 err = 0;
556 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
557 goto out;
558
559 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
560 t->err_count++;
561 else
562 t->err_count = 1;
563 t->err_time = jiffies;
564 out:
565 rcu_read_unlock();
566 return err;
567 }
568
569 static inline void ipip6_ecn_decapsulate(const struct iphdr *iph, struct sk_buff *skb)
570 {
571 if (INET_ECN_is_ce(iph->tos))
572 IP6_ECN_set_ce(ipv6_hdr(skb));
573 }
574
575 static int ipip6_rcv(struct sk_buff *skb)
576 {
577 const struct iphdr *iph;
578 struct ip_tunnel *tunnel;
579
580 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
581 goto out;
582
583 iph = ip_hdr(skb);
584
585 rcu_read_lock();
586 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
587 iph->saddr, iph->daddr);
588 if (tunnel != NULL) {
589 struct pcpu_tstats *tstats;
590
591 secpath_reset(skb);
592 skb->mac_header = skb->network_header;
593 skb_reset_network_header(skb);
594 IPCB(skb)->flags = 0;
595 skb->protocol = htons(ETH_P_IPV6);
596 skb->pkt_type = PACKET_HOST;
597
598 if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
599 !isatap_chksrc(skb, iph, tunnel)) {
600 tunnel->dev->stats.rx_errors++;
601 rcu_read_unlock();
602 kfree_skb(skb);
603 return 0;
604 }
605
606 tstats = this_cpu_ptr(tunnel->dev->tstats);
607 tstats->rx_packets++;
608 tstats->rx_bytes += skb->len;
609
610 __skb_tunnel_rx(skb, tunnel->dev);
611
612 ipip6_ecn_decapsulate(iph, skb);
613
614 netif_rx(skb);
615
616 rcu_read_unlock();
617 return 0;
618 }
619
620 /* no tunnel matched, let upstream know, ipsec may handle it */
621 rcu_read_unlock();
622 return 1;
623 out:
624 kfree_skb(skb);
625 return 0;
626 }
627
628 /*
629 * Returns the embedded IPv4 address if the IPv6 address
630 * comes from 6rd / 6to4 (RFC 3056) addr space.
631 */
632 static inline
633 __be32 try_6rd(const struct in6_addr *v6dst, struct ip_tunnel *tunnel)
634 {
635 __be32 dst = 0;
636
637 #ifdef CONFIG_IPV6_SIT_6RD
638 if (ipv6_prefix_equal(v6dst, &tunnel->ip6rd.prefix,
639 tunnel->ip6rd.prefixlen)) {
640 unsigned int pbw0, pbi0;
641 int pbi1;
642 u32 d;
643
644 pbw0 = tunnel->ip6rd.prefixlen >> 5;
645 pbi0 = tunnel->ip6rd.prefixlen & 0x1f;
646
647 d = (ntohl(v6dst->s6_addr32[pbw0]) << pbi0) >>
648 tunnel->ip6rd.relay_prefixlen;
649
650 pbi1 = pbi0 - tunnel->ip6rd.relay_prefixlen;
651 if (pbi1 > 0)
652 d |= ntohl(v6dst->s6_addr32[pbw0 + 1]) >>
653 (32 - pbi1);
654
655 dst = tunnel->ip6rd.relay_prefix | htonl(d);
656 }
657 #else
658 if (v6dst->s6_addr16[0] == htons(0x2002)) {
659 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
660 memcpy(&dst, &v6dst->s6_addr16[1], 4);
661 }
662 #endif
663 return dst;
664 }
665
666 /*
667 * This function assumes it is being called from dev_queue_xmit()
668 * and that skb is filled properly by that function.
669 */
670
671 static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
672 struct net_device *dev)
673 {
674 struct ip_tunnel *tunnel = netdev_priv(dev);
675 struct pcpu_tstats *tstats;
676 const struct iphdr *tiph = &tunnel->parms.iph;
677 const struct ipv6hdr *iph6 = ipv6_hdr(skb);
678 u8 tos = tunnel->parms.iph.tos;
679 __be16 df = tiph->frag_off;
680 struct rtable *rt; /* Route to the other host */
681 struct net_device *tdev; /* Device to other host */
682 struct iphdr *iph; /* Our new IP header */
683 unsigned int max_headroom; /* The extra header space needed */
684 __be32 dst = tiph->daddr;
685 struct flowi4 fl4;
686 int mtu;
687 const struct in6_addr *addr6;
688 int addr_type;
689
690 if (skb->protocol != htons(ETH_P_IPV6))
691 goto tx_error;
692
693 if (tos == 1)
694 tos = ipv6_get_dsfield(iph6);
695
696 /* ISATAP (RFC4214) - must come before 6to4 */
697 if (dev->priv_flags & IFF_ISATAP) {
698 struct neighbour *neigh = NULL;
699 bool do_tx_error = false;
700
701 if (skb_dst(skb))
702 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
703
704 if (neigh == NULL) {
705 net_dbg_ratelimited("sit: nexthop == NULL\n");
706 goto tx_error;
707 }
708
709 addr6 = (const struct in6_addr *)&neigh->primary_key;
710 addr_type = ipv6_addr_type(addr6);
711
712 if ((addr_type & IPV6_ADDR_UNICAST) &&
713 ipv6_addr_is_isatap(addr6))
714 dst = addr6->s6_addr32[3];
715 else
716 do_tx_error = true;
717
718 neigh_release(neigh);
719 if (do_tx_error)
720 goto tx_error;
721 }
722
723 if (!dst)
724 dst = try_6rd(&iph6->daddr, tunnel);
725
726 if (!dst) {
727 struct neighbour *neigh = NULL;
728 bool do_tx_error = false;
729
730 if (skb_dst(skb))
731 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
732
733 if (neigh == NULL) {
734 net_dbg_ratelimited("sit: nexthop == NULL\n");
735 goto tx_error;
736 }
737
738 addr6 = (const struct in6_addr *)&neigh->primary_key;
739 addr_type = ipv6_addr_type(addr6);
740
741 if (addr_type == IPV6_ADDR_ANY) {
742 addr6 = &ipv6_hdr(skb)->daddr;
743 addr_type = ipv6_addr_type(addr6);
744 }
745
746 if ((addr_type & IPV6_ADDR_COMPATv4) != 0)
747 dst = addr6->s6_addr32[3];
748 else
749 do_tx_error = true;
750
751 neigh_release(neigh);
752 if (do_tx_error)
753 goto tx_error;
754 }
755
756 rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
757 dst, tiph->saddr,
758 0, 0,
759 IPPROTO_IPV6, RT_TOS(tos),
760 tunnel->parms.link);
761 if (IS_ERR(rt)) {
762 dev->stats.tx_carrier_errors++;
763 goto tx_error_icmp;
764 }
765 if (rt->rt_type != RTN_UNICAST) {
766 ip_rt_put(rt);
767 dev->stats.tx_carrier_errors++;
768 goto tx_error_icmp;
769 }
770 tdev = rt->dst.dev;
771
772 if (tdev == dev) {
773 ip_rt_put(rt);
774 dev->stats.collisions++;
775 goto tx_error;
776 }
777
778 if (df) {
779 mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
780
781 if (mtu < 68) {
782 dev->stats.collisions++;
783 ip_rt_put(rt);
784 goto tx_error;
785 }
786
787 if (mtu < IPV6_MIN_MTU) {
788 mtu = IPV6_MIN_MTU;
789 df = 0;
790 }
791
792 if (tunnel->parms.iph.daddr && skb_dst(skb))
793 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), mtu);
794
795 if (skb->len > mtu) {
796 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
797 ip_rt_put(rt);
798 goto tx_error;
799 }
800 }
801
802 if (tunnel->err_count > 0) {
803 if (time_before(jiffies,
804 tunnel->err_time + IPTUNNEL_ERR_TIMEO)) {
805 tunnel->err_count--;
806 dst_link_failure(skb);
807 } else
808 tunnel->err_count = 0;
809 }
810
811 /*
812 * Okay, now see if we can stuff it in the buffer as-is.
813 */
814 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
815
816 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
817 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
818 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
819 if (!new_skb) {
820 ip_rt_put(rt);
821 dev->stats.tx_dropped++;
822 dev_kfree_skb(skb);
823 return NETDEV_TX_OK;
824 }
825 if (skb->sk)
826 skb_set_owner_w(new_skb, skb->sk);
827 dev_kfree_skb(skb);
828 skb = new_skb;
829 iph6 = ipv6_hdr(skb);
830 }
831
832 skb->transport_header = skb->network_header;
833 skb_push(skb, sizeof(struct iphdr));
834 skb_reset_network_header(skb);
835 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
836 IPCB(skb)->flags = 0;
837 skb_dst_drop(skb);
838 skb_dst_set(skb, &rt->dst);
839
840 /*
841 * Push down and install the IPIP header.
842 */
843
844 iph = ip_hdr(skb);
845 iph->version = 4;
846 iph->ihl = sizeof(struct iphdr)>>2;
847 iph->frag_off = df;
848 iph->protocol = IPPROTO_IPV6;
849 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
850 iph->daddr = fl4.daddr;
851 iph->saddr = fl4.saddr;
852
853 if ((iph->ttl = tiph->ttl) == 0)
854 iph->ttl = iph6->hop_limit;
855
856 nf_reset(skb);
857 tstats = this_cpu_ptr(dev->tstats);
858 __IPTUNNEL_XMIT(tstats, &dev->stats);
859 return NETDEV_TX_OK;
860
861 tx_error_icmp:
862 dst_link_failure(skb);
863 tx_error:
864 dev->stats.tx_errors++;
865 dev_kfree_skb(skb);
866 return NETDEV_TX_OK;
867 }
868
869 static void ipip6_tunnel_bind_dev(struct net_device *dev)
870 {
871 struct net_device *tdev = NULL;
872 struct ip_tunnel *tunnel;
873 const struct iphdr *iph;
874 struct flowi4 fl4;
875
876 tunnel = netdev_priv(dev);
877 iph = &tunnel->parms.iph;
878
879 if (iph->daddr) {
880 struct rtable *rt = ip_route_output_ports(dev_net(dev), &fl4, NULL,
881 iph->daddr, iph->saddr,
882 0, 0,
883 IPPROTO_IPV6,
884 RT_TOS(iph->tos),
885 tunnel->parms.link);
886
887 if (!IS_ERR(rt)) {
888 tdev = rt->dst.dev;
889 ip_rt_put(rt);
890 }
891 dev->flags |= IFF_POINTOPOINT;
892 }
893
894 if (!tdev && tunnel->parms.link)
895 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
896
897 if (tdev) {
898 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
899 dev->mtu = tdev->mtu - sizeof(struct iphdr);
900 if (dev->mtu < IPV6_MIN_MTU)
901 dev->mtu = IPV6_MIN_MTU;
902 }
903 dev->iflink = tunnel->parms.link;
904 }
905
906 static int
907 ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
908 {
909 int err = 0;
910 struct ip_tunnel_parm p;
911 struct ip_tunnel_prl prl;
912 struct ip_tunnel *t;
913 struct net *net = dev_net(dev);
914 struct sit_net *sitn = net_generic(net, sit_net_id);
915 #ifdef CONFIG_IPV6_SIT_6RD
916 struct ip_tunnel_6rd ip6rd;
917 #endif
918
919 switch (cmd) {
920 case SIOCGETTUNNEL:
921 #ifdef CONFIG_IPV6_SIT_6RD
922 case SIOCGET6RD:
923 #endif
924 t = NULL;
925 if (dev == sitn->fb_tunnel_dev) {
926 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
927 err = -EFAULT;
928 break;
929 }
930 t = ipip6_tunnel_locate(net, &p, 0);
931 }
932 if (t == NULL)
933 t = netdev_priv(dev);
934
935 err = -EFAULT;
936 if (cmd == SIOCGETTUNNEL) {
937 memcpy(&p, &t->parms, sizeof(p));
938 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p,
939 sizeof(p)))
940 goto done;
941 #ifdef CONFIG_IPV6_SIT_6RD
942 } else {
943 ip6rd.prefix = t->ip6rd.prefix;
944 ip6rd.relay_prefix = t->ip6rd.relay_prefix;
945 ip6rd.prefixlen = t->ip6rd.prefixlen;
946 ip6rd.relay_prefixlen = t->ip6rd.relay_prefixlen;
947 if (copy_to_user(ifr->ifr_ifru.ifru_data, &ip6rd,
948 sizeof(ip6rd)))
949 goto done;
950 #endif
951 }
952 err = 0;
953 break;
954
955 case SIOCADDTUNNEL:
956 case SIOCCHGTUNNEL:
957 err = -EPERM;
958 if (!capable(CAP_NET_ADMIN))
959 goto done;
960
961 err = -EFAULT;
962 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
963 goto done;
964
965 err = -EINVAL;
966 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
967 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
968 goto done;
969 if (p.iph.ttl)
970 p.iph.frag_off |= htons(IP_DF);
971
972 t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
973
974 if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
975 if (t != NULL) {
976 if (t->dev != dev) {
977 err = -EEXIST;
978 break;
979 }
980 } else {
981 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
982 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
983 err = -EINVAL;
984 break;
985 }
986 t = netdev_priv(dev);
987 ipip6_tunnel_unlink(sitn, t);
988 synchronize_net();
989 t->parms.iph.saddr = p.iph.saddr;
990 t->parms.iph.daddr = p.iph.daddr;
991 memcpy(dev->dev_addr, &p.iph.saddr, 4);
992 memcpy(dev->broadcast, &p.iph.daddr, 4);
993 ipip6_tunnel_link(sitn, t);
994 netdev_state_change(dev);
995 }
996 }
997
998 if (t) {
999 err = 0;
1000 if (cmd == SIOCCHGTUNNEL) {
1001 t->parms.iph.ttl = p.iph.ttl;
1002 t->parms.iph.tos = p.iph.tos;
1003 if (t->parms.link != p.link) {
1004 t->parms.link = p.link;
1005 ipip6_tunnel_bind_dev(dev);
1006 netdev_state_change(dev);
1007 }
1008 }
1009 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
1010 err = -EFAULT;
1011 } else
1012 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1013 break;
1014
1015 case SIOCDELTUNNEL:
1016 err = -EPERM;
1017 if (!capable(CAP_NET_ADMIN))
1018 goto done;
1019
1020 if (dev == sitn->fb_tunnel_dev) {
1021 err = -EFAULT;
1022 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1023 goto done;
1024 err = -ENOENT;
1025 if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
1026 goto done;
1027 err = -EPERM;
1028 if (t == netdev_priv(sitn->fb_tunnel_dev))
1029 goto done;
1030 dev = t->dev;
1031 }
1032 unregister_netdevice(dev);
1033 err = 0;
1034 break;
1035
1036 case SIOCGETPRL:
1037 err = -EINVAL;
1038 if (dev == sitn->fb_tunnel_dev)
1039 goto done;
1040 err = -ENOENT;
1041 if (!(t = netdev_priv(dev)))
1042 goto done;
1043 err = ipip6_tunnel_get_prl(t, ifr->ifr_ifru.ifru_data);
1044 break;
1045
1046 case SIOCADDPRL:
1047 case SIOCDELPRL:
1048 case SIOCCHGPRL:
1049 err = -EPERM;
1050 if (!capable(CAP_NET_ADMIN))
1051 goto done;
1052 err = -EINVAL;
1053 if (dev == sitn->fb_tunnel_dev)
1054 goto done;
1055 err = -EFAULT;
1056 if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
1057 goto done;
1058 err = -ENOENT;
1059 if (!(t = netdev_priv(dev)))
1060 goto done;
1061
1062 switch (cmd) {
1063 case SIOCDELPRL:
1064 err = ipip6_tunnel_del_prl(t, &prl);
1065 break;
1066 case SIOCADDPRL:
1067 case SIOCCHGPRL:
1068 err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
1069 break;
1070 }
1071 netdev_state_change(dev);
1072 break;
1073
1074 #ifdef CONFIG_IPV6_SIT_6RD
1075 case SIOCADD6RD:
1076 case SIOCCHG6RD:
1077 case SIOCDEL6RD:
1078 err = -EPERM;
1079 if (!capable(CAP_NET_ADMIN))
1080 goto done;
1081
1082 err = -EFAULT;
1083 if (copy_from_user(&ip6rd, ifr->ifr_ifru.ifru_data,
1084 sizeof(ip6rd)))
1085 goto done;
1086
1087 t = netdev_priv(dev);
1088
1089 if (cmd != SIOCDEL6RD) {
1090 struct in6_addr prefix;
1091 __be32 relay_prefix;
1092
1093 err = -EINVAL;
1094 if (ip6rd.relay_prefixlen > 32 ||
1095 ip6rd.prefixlen + (32 - ip6rd.relay_prefixlen) > 64)
1096 goto done;
1097
1098 ipv6_addr_prefix(&prefix, &ip6rd.prefix,
1099 ip6rd.prefixlen);
1100 if (!ipv6_addr_equal(&prefix, &ip6rd.prefix))
1101 goto done;
1102 if (ip6rd.relay_prefixlen)
1103 relay_prefix = ip6rd.relay_prefix &
1104 htonl(0xffffffffUL <<
1105 (32 - ip6rd.relay_prefixlen));
1106 else
1107 relay_prefix = 0;
1108 if (relay_prefix != ip6rd.relay_prefix)
1109 goto done;
1110
1111 t->ip6rd.prefix = prefix;
1112 t->ip6rd.relay_prefix = relay_prefix;
1113 t->ip6rd.prefixlen = ip6rd.prefixlen;
1114 t->ip6rd.relay_prefixlen = ip6rd.relay_prefixlen;
1115 } else
1116 ipip6_tunnel_clone_6rd(dev, sitn);
1117
1118 err = 0;
1119 break;
1120 #endif
1121
1122 default:
1123 err = -EINVAL;
1124 }
1125
1126 done:
1127 return err;
1128 }
1129
1130 static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1131 {
1132 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
1133 return -EINVAL;
1134 dev->mtu = new_mtu;
1135 return 0;
1136 }
1137
1138 static const struct net_device_ops ipip6_netdev_ops = {
1139 .ndo_uninit = ipip6_tunnel_uninit,
1140 .ndo_start_xmit = ipip6_tunnel_xmit,
1141 .ndo_do_ioctl = ipip6_tunnel_ioctl,
1142 .ndo_change_mtu = ipip6_tunnel_change_mtu,
1143 .ndo_get_stats64= ipip6_get_stats64,
1144 };
1145
1146 static void ipip6_dev_free(struct net_device *dev)
1147 {
1148 free_percpu(dev->tstats);
1149 free_netdev(dev);
1150 }
1151
1152 static void ipip6_tunnel_setup(struct net_device *dev)
1153 {
1154 dev->netdev_ops = &ipip6_netdev_ops;
1155 dev->destructor = ipip6_dev_free;
1156
1157 dev->type = ARPHRD_SIT;
1158 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
1159 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
1160 dev->flags = IFF_NOARP;
1161 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1162 dev->iflink = 0;
1163 dev->addr_len = 4;
1164 dev->features |= NETIF_F_NETNS_LOCAL;
1165 dev->features |= NETIF_F_LLTX;
1166 }
1167
1168 static int ipip6_tunnel_init(struct net_device *dev)
1169 {
1170 struct ip_tunnel *tunnel = netdev_priv(dev);
1171
1172 tunnel->dev = dev;
1173
1174 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
1175 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
1176
1177 ipip6_tunnel_bind_dev(dev);
1178 dev->tstats = alloc_percpu(struct pcpu_tstats);
1179 if (!dev->tstats)
1180 return -ENOMEM;
1181
1182 return 0;
1183 }
1184
1185 static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
1186 {
1187 struct ip_tunnel *tunnel = netdev_priv(dev);
1188 struct iphdr *iph = &tunnel->parms.iph;
1189 struct net *net = dev_net(dev);
1190 struct sit_net *sitn = net_generic(net, sit_net_id);
1191
1192 tunnel->dev = dev;
1193 strcpy(tunnel->parms.name, dev->name);
1194
1195 iph->version = 4;
1196 iph->protocol = IPPROTO_IPV6;
1197 iph->ihl = 5;
1198 iph->ttl = 64;
1199
1200 dev->tstats = alloc_percpu(struct pcpu_tstats);
1201 if (!dev->tstats)
1202 return -ENOMEM;
1203 dev_hold(dev);
1204 rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
1205 return 0;
1206 }
1207
1208 static struct xfrm_tunnel sit_handler __read_mostly = {
1209 .handler = ipip6_rcv,
1210 .err_handler = ipip6_err,
1211 .priority = 1,
1212 };
1213
1214 static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head)
1215 {
1216 int prio;
1217
1218 for (prio = 1; prio < 4; prio++) {
1219 int h;
1220 for (h = 0; h < HASH_SIZE; h++) {
1221 struct ip_tunnel *t;
1222
1223 t = rtnl_dereference(sitn->tunnels[prio][h]);
1224 while (t != NULL) {
1225 unregister_netdevice_queue(t->dev, head);
1226 t = rtnl_dereference(t->next);
1227 }
1228 }
1229 }
1230 }
1231
1232 static int __net_init sit_init_net(struct net *net)
1233 {
1234 struct sit_net *sitn = net_generic(net, sit_net_id);
1235 struct ip_tunnel *t;
1236 int err;
1237
1238 sitn->tunnels[0] = sitn->tunnels_wc;
1239 sitn->tunnels[1] = sitn->tunnels_l;
1240 sitn->tunnels[2] = sitn->tunnels_r;
1241 sitn->tunnels[3] = sitn->tunnels_r_l;
1242
1243 sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
1244 ipip6_tunnel_setup);
1245 if (!sitn->fb_tunnel_dev) {
1246 err = -ENOMEM;
1247 goto err_alloc_dev;
1248 }
1249 dev_net_set(sitn->fb_tunnel_dev, net);
1250
1251 err = ipip6_fb_tunnel_init(sitn->fb_tunnel_dev);
1252 if (err)
1253 goto err_dev_free;
1254
1255 ipip6_tunnel_clone_6rd(sitn->fb_tunnel_dev, sitn);
1256
1257 if ((err = register_netdev(sitn->fb_tunnel_dev)))
1258 goto err_reg_dev;
1259
1260 t = netdev_priv(sitn->fb_tunnel_dev);
1261
1262 strcpy(t->parms.name, sitn->fb_tunnel_dev->name);
1263 return 0;
1264
1265 err_reg_dev:
1266 dev_put(sitn->fb_tunnel_dev);
1267 err_dev_free:
1268 ipip6_dev_free(sitn->fb_tunnel_dev);
1269 err_alloc_dev:
1270 return err;
1271 }
1272
1273 static void __net_exit sit_exit_net(struct net *net)
1274 {
1275 struct sit_net *sitn = net_generic(net, sit_net_id);
1276 LIST_HEAD(list);
1277
1278 rtnl_lock();
1279 sit_destroy_tunnels(sitn, &list);
1280 unregister_netdevice_queue(sitn->fb_tunnel_dev, &list);
1281 unregister_netdevice_many(&list);
1282 rtnl_unlock();
1283 }
1284
1285 static struct pernet_operations sit_net_ops = {
1286 .init = sit_init_net,
1287 .exit = sit_exit_net,
1288 .id = &sit_net_id,
1289 .size = sizeof(struct sit_net),
1290 };
1291
1292 static void __exit sit_cleanup(void)
1293 {
1294 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1295
1296 unregister_pernet_device(&sit_net_ops);
1297 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1298 }
1299
1300 static int __init sit_init(void)
1301 {
1302 int err;
1303
1304 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
1305
1306 err = register_pernet_device(&sit_net_ops);
1307 if (err < 0)
1308 return err;
1309 err = xfrm4_tunnel_register(&sit_handler, AF_INET6);
1310 if (err < 0) {
1311 unregister_pernet_device(&sit_net_ops);
1312 printk(KERN_INFO "sit init: Can't add protocol\n");
1313 }
1314 return err;
1315 }
1316
1317 module_init(sit_init);
1318 module_exit(sit_cleanup);
1319 MODULE_LICENSE("GPL");
1320 MODULE_ALIAS_NETDEV("sit0");
This page took 0.05844 seconds and 5 git commands to generate.