blob: 424d11a4e7ffdd6d42a5bd80b88ea8a3d5139ec1 [file] [log] [blame]
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001/*
2 * GRE over IPv6 protocol decoder.
3 *
4 * Authors: Dmitry Kozlov (xeb@mail.ru)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/capability.h>
16#include <linux/module.h>
17#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/slab.h>
20#include <linux/uaccess.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/in.h>
24#include <linux/tcp.h>
25#include <linux/udp.h>
26#include <linux/if_arp.h>
27#include <linux/mroute.h>
28#include <linux/init.h>
29#include <linux/in6.h>
30#include <linux/inetdevice.h>
31#include <linux/igmp.h>
32#include <linux/netfilter_ipv4.h>
33#include <linux/etherdevice.h>
34#include <linux/if_ether.h>
35#include <linux/hash.h>
36#include <linux/if_tunnel.h>
37#include <linux/ip6_tunnel.h>
38
39#include <net/sock.h>
40#include <net/ip.h>
41#include <net/icmp.h>
42#include <net/protocol.h>
43#include <net/addrconf.h>
44#include <net/arp.h>
45#include <net/checksum.h>
46#include <net/dsfield.h>
47#include <net/inet_ecn.h>
48#include <net/xfrm.h>
49#include <net/net_namespace.h>
50#include <net/netns/generic.h>
51#include <net/rtnetlink.h>
52
53#include <net/ipv6.h>
54#include <net/ip6_fib.h>
55#include <net/ip6_route.h>
56#include <net/ip6_tunnel.h>
57
58
59#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
60#define IPV6_TCLASS_SHIFT 20
61
62#define HASH_SIZE_SHIFT 5
63#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
64
65static int ip6gre_net_id __read_mostly;
66struct ip6gre_net {
67 struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
68
69 struct net_device *fb_tunnel_dev;
70};
71
72static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
73static int ip6gre_tunnel_init(struct net_device *dev);
74static void ip6gre_tunnel_setup(struct net_device *dev);
75static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
76static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
77
78/* Tunnel hash table */
79
80/*
81 4 hash tables:
82
83 3: (remote,local)
84 2: (remote,*)
85 1: (*,local)
86 0: (*,*)
87
88 We require exact key match i.e. if a key is present in packet
89 it will match only tunnel with the same key; if it is not present,
90 it will match only keyless tunnel.
91
92 All keysless packets, if not matched configured keyless tunnels
93 will match fallback tunnel.
94 */
95
96#define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1))
97static u32 HASH_ADDR(const struct in6_addr *addr)
98{
99 u32 hash = ipv6_addr_hash(addr);
100
101 return hash_32(hash, HASH_SIZE_SHIFT);
102}
103
104#define tunnels_r_l tunnels[3]
105#define tunnels_r tunnels[2]
106#define tunnels_l tunnels[1]
107#define tunnels_wc tunnels[0]
108/*
109 * Locking : hash tables are protected by RCU and RTNL
110 */
111
112#define for_each_ip_tunnel_rcu(start) \
113 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
114
115/* often modified stats are per cpu, other are shared (netdev->stats) */
116struct pcpu_tstats {
117 u64 rx_packets;
118 u64 rx_bytes;
119 u64 tx_packets;
120 u64 tx_bytes;
121 struct u64_stats_sync syncp;
122};
123
124static struct rtnl_link_stats64 *ip6gre_get_stats64(struct net_device *dev,
125 struct rtnl_link_stats64 *tot)
126{
127 int i;
128
129 for_each_possible_cpu(i) {
130 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
131 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
132 unsigned int start;
133
134 do {
135 start = u64_stats_fetch_begin_bh(&tstats->syncp);
136 rx_packets = tstats->rx_packets;
137 tx_packets = tstats->tx_packets;
138 rx_bytes = tstats->rx_bytes;
139 tx_bytes = tstats->tx_bytes;
140 } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
141
142 tot->rx_packets += rx_packets;
143 tot->tx_packets += tx_packets;
144 tot->rx_bytes += rx_bytes;
145 tot->tx_bytes += tx_bytes;
146 }
147
148 tot->multicast = dev->stats.multicast;
149 tot->rx_crc_errors = dev->stats.rx_crc_errors;
150 tot->rx_fifo_errors = dev->stats.rx_fifo_errors;
151 tot->rx_length_errors = dev->stats.rx_length_errors;
152 tot->rx_errors = dev->stats.rx_errors;
153 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
154 tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
155 tot->tx_dropped = dev->stats.tx_dropped;
156 tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
157 tot->tx_errors = dev->stats.tx_errors;
158
159 return tot;
160}
161
162/* Given src, dst and key, find appropriate for input tunnel. */
163
164static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
165 const struct in6_addr *remote, const struct in6_addr *local,
166 __be32 key, __be16 gre_proto)
167{
168 struct net *net = dev_net(dev);
169 int link = dev->ifindex;
170 unsigned int h0 = HASH_ADDR(remote);
171 unsigned int h1 = HASH_KEY(key);
172 struct ip6_tnl *t, *cand = NULL;
173 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
174 int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
175 ARPHRD_ETHER : ARPHRD_IP6GRE;
176 int score, cand_score = 4;
177
178 for_each_ip_tunnel_rcu(ign->tunnels_r_l[h0 ^ h1]) {
179 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
180 !ipv6_addr_equal(remote, &t->parms.raddr) ||
181 key != t->parms.i_key ||
182 !(t->dev->flags & IFF_UP))
183 continue;
184
185 if (t->dev->type != ARPHRD_IP6GRE &&
186 t->dev->type != dev_type)
187 continue;
188
189 score = 0;
190 if (t->parms.link != link)
191 score |= 1;
192 if (t->dev->type != dev_type)
193 score |= 2;
194 if (score == 0)
195 return t;
196
197 if (score < cand_score) {
198 cand = t;
199 cand_score = score;
200 }
201 }
202
203 for_each_ip_tunnel_rcu(ign->tunnels_r[h0 ^ h1]) {
204 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
205 key != t->parms.i_key ||
206 !(t->dev->flags & IFF_UP))
207 continue;
208
209 if (t->dev->type != ARPHRD_IP6GRE &&
210 t->dev->type != dev_type)
211 continue;
212
213 score = 0;
214 if (t->parms.link != link)
215 score |= 1;
216 if (t->dev->type != dev_type)
217 score |= 2;
218 if (score == 0)
219 return t;
220
221 if (score < cand_score) {
222 cand = t;
223 cand_score = score;
224 }
225 }
226
227 for_each_ip_tunnel_rcu(ign->tunnels_l[h1]) {
228 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
229 (!ipv6_addr_equal(local, &t->parms.raddr) ||
230 !ipv6_addr_is_multicast(local))) ||
231 key != t->parms.i_key ||
232 !(t->dev->flags & IFF_UP))
233 continue;
234
235 if (t->dev->type != ARPHRD_IP6GRE &&
236 t->dev->type != dev_type)
237 continue;
238
239 score = 0;
240 if (t->parms.link != link)
241 score |= 1;
242 if (t->dev->type != dev_type)
243 score |= 2;
244 if (score == 0)
245 return t;
246
247 if (score < cand_score) {
248 cand = t;
249 cand_score = score;
250 }
251 }
252
253 for_each_ip_tunnel_rcu(ign->tunnels_wc[h1]) {
254 if (t->parms.i_key != key ||
255 !(t->dev->flags & IFF_UP))
256 continue;
257
258 if (t->dev->type != ARPHRD_IP6GRE &&
259 t->dev->type != dev_type)
260 continue;
261
262 score = 0;
263 if (t->parms.link != link)
264 score |= 1;
265 if (t->dev->type != dev_type)
266 score |= 2;
267 if (score == 0)
268 return t;
269
270 if (score < cand_score) {
271 cand = t;
272 cand_score = score;
273 }
274 }
275
276 if (cand != NULL)
277 return cand;
278
279 dev = ign->fb_tunnel_dev;
280 if (dev->flags & IFF_UP)
281 return netdev_priv(dev);
282
283 return NULL;
284}
285
286static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
287 const struct __ip6_tnl_parm *p)
288{
289 const struct in6_addr *remote = &p->raddr;
290 const struct in6_addr *local = &p->laddr;
291 unsigned int h = HASH_KEY(p->i_key);
292 int prio = 0;
293
294 if (!ipv6_addr_any(local))
295 prio |= 1;
296 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
297 prio |= 2;
298 h ^= HASH_ADDR(remote);
299 }
300
301 return &ign->tunnels[prio][h];
302}
303
304static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
305 const struct ip6_tnl *t)
306{
307 return __ip6gre_bucket(ign, &t->parms);
308}
309
310static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
311{
312 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
313
314 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
315 rcu_assign_pointer(*tp, t);
316}
317
318static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
319{
320 struct ip6_tnl __rcu **tp;
321 struct ip6_tnl *iter;
322
323 for (tp = ip6gre_bucket(ign, t);
324 (iter = rtnl_dereference(*tp)) != NULL;
325 tp = &iter->next) {
326 if (t == iter) {
327 rcu_assign_pointer(*tp, t->next);
328 break;
329 }
330 }
331}
332
333static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
334 const struct __ip6_tnl_parm *parms,
335 int type)
336{
337 const struct in6_addr *remote = &parms->raddr;
338 const struct in6_addr *local = &parms->laddr;
339 __be32 key = parms->i_key;
340 int link = parms->link;
341 struct ip6_tnl *t;
342 struct ip6_tnl __rcu **tp;
343 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
344
345 for (tp = __ip6gre_bucket(ign, parms);
346 (t = rtnl_dereference(*tp)) != NULL;
347 tp = &t->next)
348 if (ipv6_addr_equal(local, &t->parms.laddr) &&
349 ipv6_addr_equal(remote, &t->parms.raddr) &&
350 key == t->parms.i_key &&
351 link == t->parms.link &&
352 type == t->dev->type)
353 break;
354
355 return t;
356}
357
358static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
359 const struct __ip6_tnl_parm *parms, int create)
360{
361 struct ip6_tnl *t, *nt;
362 struct net_device *dev;
363 char name[IFNAMSIZ];
364 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
365
366 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
367 if (t || !create)
368 return t;
369
370 if (parms->name[0])
371 strlcpy(name, parms->name, IFNAMSIZ);
372 else
373 strcpy(name, "ip6gre%d");
374
375 dev = alloc_netdev(sizeof(*t), name, ip6gre_tunnel_setup);
376 if (!dev)
377 return NULL;
378
379 dev_net_set(dev, net);
380
381 nt = netdev_priv(dev);
382 nt->parms = *parms;
383 dev->rtnl_link_ops = &ip6gre_link_ops;
384
385 nt->dev = dev;
386 ip6gre_tnl_link_config(nt, 1);
387
388 if (register_netdevice(dev) < 0)
389 goto failed_free;
390
391 /* Can use a lockless transmit, unless we generate output sequences */
392 if (!(nt->parms.o_flags & GRE_SEQ))
393 dev->features |= NETIF_F_LLTX;
394
395 dev_hold(dev);
396 ip6gre_tunnel_link(ign, nt);
397 return nt;
398
399failed_free:
400 free_netdev(dev);
401 return NULL;
402}
403
404static void ip6gre_tunnel_uninit(struct net_device *dev)
405{
406 struct net *net = dev_net(dev);
407 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
408
409 ip6gre_tunnel_unlink(ign, netdev_priv(dev));
410 dev_put(dev);
411}
412
413
414static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
415 u8 type, u8 code, int offset, __be32 info)
416{
417 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
Eric Dumazetb87fb392012-08-19 03:47:30 +0000418 __be16 *p = (__be16 *)(skb->data + offset);
419 int grehlen = offset + 4;
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000420 struct ip6_tnl *t;
421 __be16 flags;
422
423 flags = p[0];
424 if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
425 if (flags&(GRE_VERSION|GRE_ROUTING))
426 return;
427 if (flags&GRE_KEY) {
428 grehlen += 4;
429 if (flags&GRE_CSUM)
430 grehlen += 4;
431 }
432 }
433
434 /* If only 8 bytes returned, keyed message will be dropped here */
Eric Dumazetb87fb392012-08-19 03:47:30 +0000435 if (!pskb_may_pull(skb, grehlen))
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000436 return;
Eric Dumazetb87fb392012-08-19 03:47:30 +0000437 ipv6h = (const struct ipv6hdr *)skb->data;
438 p = (__be16 *)(skb->data + offset);
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000439
440 rcu_read_lock();
441
442 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
443 flags & GRE_KEY ?
444 *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
445 p[1]);
446 if (t == NULL)
447 goto out;
448
449 switch (type) {
450 __u32 teli;
451 struct ipv6_tlv_tnl_enc_lim *tel;
452 __u32 mtu;
453 case ICMPV6_DEST_UNREACH:
454 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
455 t->parms.name);
456 break;
457 case ICMPV6_TIME_EXCEED:
458 if (code == ICMPV6_EXC_HOPLIMIT) {
459 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
460 t->parms.name);
461 }
462 break;
463 case ICMPV6_PARAMPROB:
464 teli = 0;
465 if (code == ICMPV6_HDR_FIELD)
466 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
467
468 if (teli && teli == info - 2) {
469 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
470 if (tel->encap_limit == 0) {
471 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
472 t->parms.name);
473 }
474 } else {
475 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
476 t->parms.name);
477 }
478 break;
479 case ICMPV6_PKT_TOOBIG:
480 mtu = info - offset;
481 if (mtu < IPV6_MIN_MTU)
482 mtu = IPV6_MIN_MTU;
483 t->dev->mtu = mtu;
484 break;
485 }
486
487 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
488 t->err_count++;
489 else
490 t->err_count = 1;
491 t->err_time = jiffies;
492out:
493 rcu_read_unlock();
494}
495
496static inline void ip6gre_ecn_decapsulate_ipv4(const struct ip6_tnl *t,
497 const struct ipv6hdr *ipv6h, struct sk_buff *skb)
498{
499 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
500
501 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
502 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
503
504 if (INET_ECN_is_ce(dsfield))
505 IP_ECN_set_ce(ip_hdr(skb));
506}
507
508static inline void ip6gre_ecn_decapsulate_ipv6(const struct ip6_tnl *t,
509 const struct ipv6hdr *ipv6h, struct sk_buff *skb)
510{
511 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
512 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
513
514 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
515 IP6_ECN_set_ce(ipv6_hdr(skb));
516}
517
518static int ip6gre_rcv(struct sk_buff *skb)
519{
520 const struct ipv6hdr *ipv6h;
521 u8 *h;
522 __be16 flags;
523 __sum16 csum = 0;
524 __be32 key = 0;
525 u32 seqno = 0;
526 struct ip6_tnl *tunnel;
527 int offset = 4;
528 __be16 gre_proto;
529
530 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
531 goto drop_nolock;
532
533 ipv6h = ipv6_hdr(skb);
534 h = skb->data;
535 flags = *(__be16 *)h;
536
537 if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
538 /* - Version must be 0.
539 - We do not support routing headers.
540 */
541 if (flags&(GRE_VERSION|GRE_ROUTING))
542 goto drop_nolock;
543
544 if (flags&GRE_CSUM) {
545 switch (skb->ip_summed) {
546 case CHECKSUM_COMPLETE:
547 csum = csum_fold(skb->csum);
548 if (!csum)
549 break;
550 /* fall through */
551 case CHECKSUM_NONE:
552 skb->csum = 0;
553 csum = __skb_checksum_complete(skb);
554 skb->ip_summed = CHECKSUM_COMPLETE;
555 }
556 offset += 4;
557 }
558 if (flags&GRE_KEY) {
559 key = *(__be32 *)(h + offset);
560 offset += 4;
561 }
562 if (flags&GRE_SEQ) {
563 seqno = ntohl(*(__be32 *)(h + offset));
564 offset += 4;
565 }
566 }
567
568 gre_proto = *(__be16 *)(h + 2);
569
570 rcu_read_lock();
571 tunnel = ip6gre_tunnel_lookup(skb->dev,
572 &ipv6h->saddr, &ipv6h->daddr, key,
573 gre_proto);
574 if (tunnel) {
575 struct pcpu_tstats *tstats;
576
577 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
578 goto drop;
579
580 if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
581 tunnel->dev->stats.rx_dropped++;
582 goto drop;
583 }
584
585 secpath_reset(skb);
586
587 skb->protocol = gre_proto;
588 /* WCCP version 1 and 2 protocol decoding.
589 * - Change protocol to IP
590 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
591 */
592 if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
593 skb->protocol = htons(ETH_P_IP);
594 if ((*(h + offset) & 0xF0) != 0x40)
595 offset += 4;
596 }
597
598 skb->mac_header = skb->network_header;
599 __pskb_pull(skb, offset);
600 skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
601 skb->pkt_type = PACKET_HOST;
602
603 if (((flags&GRE_CSUM) && csum) ||
604 (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
605 tunnel->dev->stats.rx_crc_errors++;
606 tunnel->dev->stats.rx_errors++;
607 goto drop;
608 }
609 if (tunnel->parms.i_flags&GRE_SEQ) {
610 if (!(flags&GRE_SEQ) ||
611 (tunnel->i_seqno &&
612 (s32)(seqno - tunnel->i_seqno) < 0)) {
613 tunnel->dev->stats.rx_fifo_errors++;
614 tunnel->dev->stats.rx_errors++;
615 goto drop;
616 }
617 tunnel->i_seqno = seqno + 1;
618 }
619
620 /* Warning: All skb pointers will be invalidated! */
621 if (tunnel->dev->type == ARPHRD_ETHER) {
622 if (!pskb_may_pull(skb, ETH_HLEN)) {
623 tunnel->dev->stats.rx_length_errors++;
624 tunnel->dev->stats.rx_errors++;
625 goto drop;
626 }
627
628 ipv6h = ipv6_hdr(skb);
629 skb->protocol = eth_type_trans(skb, tunnel->dev);
630 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
631 }
632
633 tstats = this_cpu_ptr(tunnel->dev->tstats);
634 u64_stats_update_begin(&tstats->syncp);
635 tstats->rx_packets++;
636 tstats->rx_bytes += skb->len;
637 u64_stats_update_end(&tstats->syncp);
638
639 __skb_tunnel_rx(skb, tunnel->dev);
640
641 skb_reset_network_header(skb);
642 if (skb->protocol == htons(ETH_P_IP))
643 ip6gre_ecn_decapsulate_ipv4(tunnel, ipv6h, skb);
644 else if (skb->protocol == htons(ETH_P_IPV6))
645 ip6gre_ecn_decapsulate_ipv6(tunnel, ipv6h, skb);
646
647 netif_rx(skb);
648
649 rcu_read_unlock();
650 return 0;
651 }
652 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
653
654drop:
655 rcu_read_unlock();
656drop_nolock:
657 kfree_skb(skb);
658 return 0;
659}
660
661struct ipv6_tel_txoption {
662 struct ipv6_txoptions ops;
663 __u8 dst_opt[8];
664};
665
666static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
667{
668 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
669
670 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
671 opt->dst_opt[3] = 1;
672 opt->dst_opt[4] = encap_limit;
673 opt->dst_opt[5] = IPV6_TLV_PADN;
674 opt->dst_opt[6] = 1;
675
676 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
677 opt->ops.opt_nflen = 8;
678}
679
680static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
681 struct net_device *dev,
682 __u8 dsfield,
683 struct flowi6 *fl6,
684 int encap_limit,
685 __u32 *pmtu)
686{
687 struct net *net = dev_net(dev);
688 struct ip6_tnl *tunnel = netdev_priv(dev);
689 struct net_device *tdev; /* Device to other host */
690 struct ipv6hdr *ipv6h; /* Our new IP header */
691 unsigned int max_headroom; /* The extra header space needed */
692 int gre_hlen;
693 struct ipv6_tel_txoption opt;
694 int mtu;
695 struct dst_entry *dst = NULL, *ndst = NULL;
696 struct net_device_stats *stats = &tunnel->dev->stats;
697 int err = -1;
698 u8 proto;
699 int pkt_len;
700 struct sk_buff *new_skb;
701
702 if (dev->type == ARPHRD_ETHER)
703 IPCB(skb)->flags = 0;
704
705 if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
706 gre_hlen = 0;
707 ipv6h = (struct ipv6hdr *)skb->data;
708 fl6->daddr = ipv6h->daddr;
709 } else {
710 gre_hlen = tunnel->hlen;
711 fl6->daddr = tunnel->parms.raddr;
712 }
713
714 if (!fl6->flowi6_mark)
715 dst = ip6_tnl_dst_check(tunnel);
716
717 if (!dst) {
718 ndst = ip6_route_output(net, NULL, fl6);
719
720 if (ndst->error)
721 goto tx_err_link_failure;
722 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
723 if (IS_ERR(ndst)) {
724 err = PTR_ERR(ndst);
725 ndst = NULL;
726 goto tx_err_link_failure;
727 }
728 dst = ndst;
729 }
730
731 tdev = dst->dev;
732
733 if (tdev == dev) {
734 stats->collisions++;
735 net_warn_ratelimited("%s: Local routing loop detected!\n",
736 tunnel->parms.name);
737 goto tx_err_dst_release;
738 }
739
740 mtu = dst_mtu(dst) - sizeof(*ipv6h);
741 if (encap_limit >= 0) {
742 max_headroom += 8;
743 mtu -= 8;
744 }
745 if (mtu < IPV6_MIN_MTU)
746 mtu = IPV6_MIN_MTU;
747 if (skb_dst(skb))
748 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
749 if (skb->len > mtu) {
750 *pmtu = mtu;
751 err = -EMSGSIZE;
752 goto tx_err_dst_release;
753 }
754
755 if (tunnel->err_count > 0) {
756 if (time_before(jiffies,
757 tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
758 tunnel->err_count--;
759
760 dst_link_failure(skb);
761 } else
762 tunnel->err_count = 0;
763 }
764
765 max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
766
767 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
768 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
769 new_skb = skb_realloc_headroom(skb, max_headroom);
770 if (max_headroom > dev->needed_headroom)
771 dev->needed_headroom = max_headroom;
772 if (!new_skb)
773 goto tx_err_dst_release;
774
775 if (skb->sk)
776 skb_set_owner_w(new_skb, skb->sk);
777 consume_skb(skb);
778 skb = new_skb;
779 }
780
781 skb_dst_drop(skb);
782
783 if (fl6->flowi6_mark) {
784 skb_dst_set(skb, dst);
785 ndst = NULL;
786 } else {
787 skb_dst_set_noref(skb, dst);
788 }
789
790 skb->transport_header = skb->network_header;
791
792 proto = NEXTHDR_GRE;
793 if (encap_limit >= 0) {
794 init_tel_txopt(&opt, encap_limit);
795 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
796 }
797
798 skb_push(skb, gre_hlen);
799 skb_reset_network_header(skb);
800
801 /*
802 * Push down and install the IP header.
803 */
804 ipv6h = ipv6_hdr(skb);
805 *(__be32 *)ipv6h = fl6->flowlabel | htonl(0x60000000);
806 dsfield = INET_ECN_encapsulate(0, dsfield);
807 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
808 ipv6h->hop_limit = tunnel->parms.hop_limit;
809 ipv6h->nexthdr = proto;
810 ipv6h->saddr = fl6->saddr;
811 ipv6h->daddr = fl6->daddr;
812
813 ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
814 ((__be16 *)(ipv6h + 1))[1] = (dev->type == ARPHRD_ETHER) ?
815 htons(ETH_P_TEB) : skb->protocol;
816
817 if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
818 __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
819
820 if (tunnel->parms.o_flags&GRE_SEQ) {
821 ++tunnel->o_seqno;
822 *ptr = htonl(tunnel->o_seqno);
823 ptr--;
824 }
825 if (tunnel->parms.o_flags&GRE_KEY) {
826 *ptr = tunnel->parms.o_key;
827 ptr--;
828 }
829 if (tunnel->parms.o_flags&GRE_CSUM) {
830 *ptr = 0;
831 *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
832 skb->len - sizeof(struct ipv6hdr));
833 }
834 }
835
836 nf_reset(skb);
837 pkt_len = skb->len;
838 err = ip6_local_out(skb);
839
840 if (net_xmit_eval(err) == 0) {
841 struct pcpu_tstats *tstats = this_cpu_ptr(tunnel->dev->tstats);
842
843 tstats->tx_bytes += pkt_len;
844 tstats->tx_packets++;
845 } else {
846 stats->tx_errors++;
847 stats->tx_aborted_errors++;
848 }
849
850 if (ndst)
851 ip6_tnl_dst_store(tunnel, ndst);
852
853 return 0;
854tx_err_link_failure:
855 stats->tx_carrier_errors++;
856 dst_link_failure(skb);
857tx_err_dst_release:
858 dst_release(ndst);
859 return err;
860}
861
862static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
863{
864 struct ip6_tnl *t = netdev_priv(dev);
865 const struct iphdr *iph = ip_hdr(skb);
866 int encap_limit = -1;
867 struct flowi6 fl6;
868 __u8 dsfield;
869 __u32 mtu;
870 int err;
871
872 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
873 encap_limit = t->parms.encap_limit;
874
875 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
876 fl6.flowi6_proto = IPPROTO_IPIP;
877
878 dsfield = ipv4_get_dsfield(iph);
879
880 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
881 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
882 & IPV6_TCLASS_MASK;
883 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
884 fl6.flowi6_mark = skb->mark;
885
886 err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
887 if (err != 0) {
888 /* XXX: send ICMP error even if DF is not set. */
889 if (err == -EMSGSIZE)
890 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
891 htonl(mtu));
892 return -1;
893 }
894
895 return 0;
896}
897
898static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
899{
900 struct ip6_tnl *t = netdev_priv(dev);
901 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
902 int encap_limit = -1;
903 __u16 offset;
904 struct flowi6 fl6;
905 __u8 dsfield;
906 __u32 mtu;
907 int err;
908
909 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
910 return -1;
911
912 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
913 if (offset > 0) {
914 struct ipv6_tlv_tnl_enc_lim *tel;
915 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
916 if (tel->encap_limit == 0) {
917 icmpv6_send(skb, ICMPV6_PARAMPROB,
918 ICMPV6_HDR_FIELD, offset + 2);
919 return -1;
920 }
921 encap_limit = tel->encap_limit - 1;
922 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
923 encap_limit = t->parms.encap_limit;
924
925 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
926 fl6.flowi6_proto = IPPROTO_IPV6;
927
928 dsfield = ipv6_get_dsfield(ipv6h);
929 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
930 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
931 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
932 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
933 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
934 fl6.flowi6_mark = skb->mark;
935
936 err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
937 if (err != 0) {
938 if (err == -EMSGSIZE)
939 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
940 return -1;
941 }
942
943 return 0;
944}
945
946/**
947 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
948 * @t: the outgoing tunnel device
949 * @hdr: IPv6 header from the incoming packet
950 *
951 * Description:
952 * Avoid trivial tunneling loop by checking that tunnel exit-point
953 * doesn't match source of incoming packet.
954 *
955 * Return:
956 * 1 if conflict,
957 * 0 else
958 **/
959
960static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
961 const struct ipv6hdr *hdr)
962{
963 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
964}
965
966static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
967{
968 struct ip6_tnl *t = netdev_priv(dev);
969 int encap_limit = -1;
970 struct flowi6 fl6;
971 __u32 mtu;
972 int err;
973
974 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
975 encap_limit = t->parms.encap_limit;
976
977 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
978 fl6.flowi6_proto = skb->protocol;
979
980 err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
981
982 return err;
983}
984
985static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
986 struct net_device *dev)
987{
988 struct ip6_tnl *t = netdev_priv(dev);
989 struct net_device_stats *stats = &t->dev->stats;
990 int ret;
991
992 if (!ip6_tnl_xmit_ctl(t))
993 return -1;
994
995 switch (skb->protocol) {
996 case htons(ETH_P_IP):
997 ret = ip6gre_xmit_ipv4(skb, dev);
998 break;
999 case htons(ETH_P_IPV6):
1000 ret = ip6gre_xmit_ipv6(skb, dev);
1001 break;
1002 default:
1003 ret = ip6gre_xmit_other(skb, dev);
1004 break;
1005 }
1006
1007 if (ret < 0)
1008 goto tx_err;
1009
1010 return NETDEV_TX_OK;
1011
1012tx_err:
1013 stats->tx_errors++;
1014 stats->tx_dropped++;
1015 kfree_skb(skb);
1016 return NETDEV_TX_OK;
1017}
1018
1019static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
1020{
1021 struct net_device *dev = t->dev;
1022 struct __ip6_tnl_parm *p = &t->parms;
1023 struct flowi6 *fl6 = &t->fl.u.ip6;
1024 int addend = sizeof(struct ipv6hdr) + 4;
1025
1026 if (dev->type != ARPHRD_ETHER) {
1027 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1028 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1029 }
1030
1031 /* Set up flowi template */
1032 fl6->saddr = p->laddr;
1033 fl6->daddr = p->raddr;
1034 fl6->flowi6_oif = p->link;
1035 fl6->flowlabel = 0;
1036
1037 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1038 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1039 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1040 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1041
1042 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1043 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
1044
1045 if (p->flags&IP6_TNL_F_CAP_XMIT &&
1046 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1047 dev->flags |= IFF_POINTOPOINT;
1048 else
1049 dev->flags &= ~IFF_POINTOPOINT;
1050
1051 dev->iflink = p->link;
1052
1053 /* Precalculate GRE options length */
1054 if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
1055 if (t->parms.o_flags&GRE_CSUM)
1056 addend += 4;
1057 if (t->parms.o_flags&GRE_KEY)
1058 addend += 4;
1059 if (t->parms.o_flags&GRE_SEQ)
1060 addend += 4;
1061 }
1062
1063 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1064 int strict = (ipv6_addr_type(&p->raddr) &
1065 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1066
1067 struct rt6_info *rt = rt6_lookup(dev_net(dev),
1068 &p->raddr, &p->laddr,
1069 p->link, strict);
1070
1071 if (rt == NULL)
1072 return;
1073
1074 if (rt->dst.dev) {
1075 dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
1076
1077 if (set_mtu) {
1078 dev->mtu = rt->dst.dev->mtu - addend;
1079 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1080 dev->mtu -= 8;
1081
1082 if (dev->mtu < IPV6_MIN_MTU)
1083 dev->mtu = IPV6_MIN_MTU;
1084 }
1085 }
1086 dst_release(&rt->dst);
1087 }
1088
1089 t->hlen = addend;
1090}
1091
1092static int ip6gre_tnl_change(struct ip6_tnl *t,
1093 const struct __ip6_tnl_parm *p, int set_mtu)
1094{
1095 t->parms.laddr = p->laddr;
1096 t->parms.raddr = p->raddr;
1097 t->parms.flags = p->flags;
1098 t->parms.hop_limit = p->hop_limit;
1099 t->parms.encap_limit = p->encap_limit;
1100 t->parms.flowinfo = p->flowinfo;
1101 t->parms.link = p->link;
1102 t->parms.proto = p->proto;
1103 t->parms.i_key = p->i_key;
1104 t->parms.o_key = p->o_key;
1105 t->parms.i_flags = p->i_flags;
1106 t->parms.o_flags = p->o_flags;
1107 ip6_tnl_dst_reset(t);
1108 ip6gre_tnl_link_config(t, set_mtu);
1109 return 0;
1110}
1111
1112static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1113 const struct ip6_tnl_parm2 *u)
1114{
1115 p->laddr = u->laddr;
1116 p->raddr = u->raddr;
1117 p->flags = u->flags;
1118 p->hop_limit = u->hop_limit;
1119 p->encap_limit = u->encap_limit;
1120 p->flowinfo = u->flowinfo;
1121 p->link = u->link;
1122 p->i_key = u->i_key;
1123 p->o_key = u->o_key;
1124 p->i_flags = u->i_flags;
1125 p->o_flags = u->o_flags;
1126 memcpy(p->name, u->name, sizeof(u->name));
1127}
1128
1129static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1130 const struct __ip6_tnl_parm *p)
1131{
1132 u->proto = IPPROTO_GRE;
1133 u->laddr = p->laddr;
1134 u->raddr = p->raddr;
1135 u->flags = p->flags;
1136 u->hop_limit = p->hop_limit;
1137 u->encap_limit = p->encap_limit;
1138 u->flowinfo = p->flowinfo;
1139 u->link = p->link;
1140 u->i_key = p->i_key;
1141 u->o_key = p->o_key;
1142 u->i_flags = p->i_flags;
1143 u->o_flags = p->o_flags;
1144 memcpy(u->name, p->name, sizeof(u->name));
1145}
1146
1147static int ip6gre_tunnel_ioctl(struct net_device *dev,
1148 struct ifreq *ifr, int cmd)
1149{
1150 int err = 0;
1151 struct ip6_tnl_parm2 p;
1152 struct __ip6_tnl_parm p1;
1153 struct ip6_tnl *t;
1154 struct net *net = dev_net(dev);
1155 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1156
1157 switch (cmd) {
1158 case SIOCGETTUNNEL:
1159 t = NULL;
1160 if (dev == ign->fb_tunnel_dev) {
1161 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1162 err = -EFAULT;
1163 break;
1164 }
1165 ip6gre_tnl_parm_from_user(&p1, &p);
1166 t = ip6gre_tunnel_locate(net, &p1, 0);
1167 }
1168 if (t == NULL)
1169 t = netdev_priv(dev);
1170 ip6gre_tnl_parm_to_user(&p, &t->parms);
1171 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1172 err = -EFAULT;
1173 break;
1174
1175 case SIOCADDTUNNEL:
1176 case SIOCCHGTUNNEL:
1177 err = -EPERM;
1178 if (!capable(CAP_NET_ADMIN))
1179 goto done;
1180
1181 err = -EFAULT;
1182 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1183 goto done;
1184
1185 err = -EINVAL;
1186 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1187 goto done;
1188
1189 if (!(p.i_flags&GRE_KEY))
1190 p.i_key = 0;
1191 if (!(p.o_flags&GRE_KEY))
1192 p.o_key = 0;
1193
1194 ip6gre_tnl_parm_from_user(&p1, &p);
1195 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1196
1197 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1198 if (t != NULL) {
1199 if (t->dev != dev) {
1200 err = -EEXIST;
1201 break;
1202 }
1203 } else {
1204 t = netdev_priv(dev);
1205
1206 ip6gre_tunnel_unlink(ign, t);
1207 synchronize_net();
1208 ip6gre_tnl_change(t, &p1, 1);
1209 ip6gre_tunnel_link(ign, t);
1210 netdev_state_change(dev);
1211 }
1212 }
1213
1214 if (t) {
1215 err = 0;
1216
1217 ip6gre_tnl_parm_to_user(&p, &t->parms);
1218 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1219 err = -EFAULT;
1220 } else
1221 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1222 break;
1223
1224 case SIOCDELTUNNEL:
1225 err = -EPERM;
1226 if (!capable(CAP_NET_ADMIN))
1227 goto done;
1228
1229 if (dev == ign->fb_tunnel_dev) {
1230 err = -EFAULT;
1231 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1232 goto done;
1233 err = -ENOENT;
1234 ip6gre_tnl_parm_from_user(&p1, &p);
1235 t = ip6gre_tunnel_locate(net, &p1, 0);
1236 if (t == NULL)
1237 goto done;
1238 err = -EPERM;
1239 if (t == netdev_priv(ign->fb_tunnel_dev))
1240 goto done;
1241 dev = t->dev;
1242 }
1243 unregister_netdevice(dev);
1244 err = 0;
1245 break;
1246
1247 default:
1248 err = -EINVAL;
1249 }
1250
1251done:
1252 return err;
1253}
1254
1255static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1256{
1257 struct ip6_tnl *tunnel = netdev_priv(dev);
1258 if (new_mtu < 68 ||
1259 new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen)
1260 return -EINVAL;
1261 dev->mtu = new_mtu;
1262 return 0;
1263}
1264
1265static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
1266 unsigned short type,
1267 const void *daddr, const void *saddr, unsigned int len)
1268{
1269 struct ip6_tnl *t = netdev_priv(dev);
1270 struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
1271 __be16 *p = (__be16 *)(ipv6h+1);
1272
1273 *(__be32 *)ipv6h = t->fl.u.ip6.flowlabel | htonl(0x60000000);
1274 ipv6h->hop_limit = t->parms.hop_limit;
1275 ipv6h->nexthdr = NEXTHDR_GRE;
1276 ipv6h->saddr = t->parms.laddr;
1277 ipv6h->daddr = t->parms.raddr;
1278
1279 p[0] = t->parms.o_flags;
1280 p[1] = htons(type);
1281
1282 /*
1283 * Set the source hardware address.
1284 */
1285
1286 if (saddr)
1287 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1288 if (daddr)
1289 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1290 if (!ipv6_addr_any(&ipv6h->daddr))
1291 return t->hlen;
1292
1293 return -t->hlen;
1294}
1295
1296static int ip6gre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
1297{
1298 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb_mac_header(skb);
1299 memcpy(haddr, &ipv6h->saddr, sizeof(struct in6_addr));
1300 return sizeof(struct in6_addr);
1301}
1302
1303static const struct header_ops ip6gre_header_ops = {
1304 .create = ip6gre_header,
1305 .parse = ip6gre_header_parse,
1306};
1307
1308static const struct net_device_ops ip6gre_netdev_ops = {
1309 .ndo_init = ip6gre_tunnel_init,
1310 .ndo_uninit = ip6gre_tunnel_uninit,
1311 .ndo_start_xmit = ip6gre_tunnel_xmit,
1312 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
1313 .ndo_change_mtu = ip6gre_tunnel_change_mtu,
1314 .ndo_get_stats64 = ip6gre_get_stats64,
1315};
1316
1317static void ip6gre_dev_free(struct net_device *dev)
1318{
1319 free_percpu(dev->tstats);
1320 free_netdev(dev);
1321}
1322
1323static void ip6gre_tunnel_setup(struct net_device *dev)
1324{
1325 struct ip6_tnl *t;
1326
1327 dev->netdev_ops = &ip6gre_netdev_ops;
1328 dev->destructor = ip6gre_dev_free;
1329
1330 dev->type = ARPHRD_IP6GRE;
1331 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
1332 dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
1333 t = netdev_priv(dev);
1334 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1335 dev->mtu -= 8;
1336 dev->flags |= IFF_NOARP;
1337 dev->iflink = 0;
1338 dev->addr_len = sizeof(struct in6_addr);
1339 dev->features |= NETIF_F_NETNS_LOCAL;
1340 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1341}
1342
1343static int ip6gre_tunnel_init(struct net_device *dev)
1344{
1345 struct ip6_tnl *tunnel;
1346
1347 tunnel = netdev_priv(dev);
1348
1349 tunnel->dev = dev;
1350 strcpy(tunnel->parms.name, dev->name);
1351
1352 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1353 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1354
1355 if (ipv6_addr_any(&tunnel->parms.raddr))
1356 dev->header_ops = &ip6gre_header_ops;
1357
1358 dev->tstats = alloc_percpu(struct pcpu_tstats);
1359 if (!dev->tstats)
1360 return -ENOMEM;
1361
1362 return 0;
1363}
1364
1365static void ip6gre_fb_tunnel_init(struct net_device *dev)
1366{
1367 struct ip6_tnl *tunnel = netdev_priv(dev);
1368
1369 tunnel->dev = dev;
1370 strcpy(tunnel->parms.name, dev->name);
1371
1372 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1373
1374 dev_hold(dev);
1375}
1376
1377
1378static struct inet6_protocol ip6gre_protocol __read_mostly = {
1379 .handler = ip6gre_rcv,
1380 .err_handler = ip6gre_err,
1381 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1382};
1383
1384static void ip6gre_destroy_tunnels(struct ip6gre_net *ign,
1385 struct list_head *head)
1386{
1387 int prio;
1388
1389 for (prio = 0; prio < 4; prio++) {
1390 int h;
1391 for (h = 0; h < HASH_SIZE; h++) {
1392 struct ip6_tnl *t;
1393
1394 t = rtnl_dereference(ign->tunnels[prio][h]);
1395
1396 while (t != NULL) {
1397 unregister_netdevice_queue(t->dev, head);
1398 t = rtnl_dereference(t->next);
1399 }
1400 }
1401 }
1402}
1403
1404static int __net_init ip6gre_init_net(struct net *net)
1405{
1406 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1407 int err;
1408
1409 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1410 ip6gre_tunnel_setup);
1411 if (!ign->fb_tunnel_dev) {
1412 err = -ENOMEM;
1413 goto err_alloc_dev;
1414 }
1415 dev_net_set(ign->fb_tunnel_dev, net);
1416
1417 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1418 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1419
1420 err = register_netdev(ign->fb_tunnel_dev);
1421 if (err)
1422 goto err_reg_dev;
1423
1424 rcu_assign_pointer(ign->tunnels_wc[0],
1425 netdev_priv(ign->fb_tunnel_dev));
1426 return 0;
1427
1428err_reg_dev:
1429 ip6gre_dev_free(ign->fb_tunnel_dev);
1430err_alloc_dev:
1431 return err;
1432}
1433
1434static void __net_exit ip6gre_exit_net(struct net *net)
1435{
1436 struct ip6gre_net *ign;
1437 LIST_HEAD(list);
1438
1439 ign = net_generic(net, ip6gre_net_id);
1440 rtnl_lock();
1441 ip6gre_destroy_tunnels(ign, &list);
1442 unregister_netdevice_many(&list);
1443 rtnl_unlock();
1444}
1445
1446static struct pernet_operations ip6gre_net_ops = {
1447 .init = ip6gre_init_net,
1448 .exit = ip6gre_exit_net,
1449 .id = &ip6gre_net_id,
1450 .size = sizeof(struct ip6gre_net),
1451};
1452
1453static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
1454{
1455 __be16 flags;
1456
1457 if (!data)
1458 return 0;
1459
1460 flags = 0;
1461 if (data[IFLA_GRE_IFLAGS])
1462 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1463 if (data[IFLA_GRE_OFLAGS])
1464 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1465 if (flags & (GRE_VERSION|GRE_ROUTING))
1466 return -EINVAL;
1467
1468 return 0;
1469}
1470
1471static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
1472{
1473 struct in6_addr daddr;
1474
1475 if (tb[IFLA_ADDRESS]) {
1476 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1477 return -EINVAL;
1478 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1479 return -EADDRNOTAVAIL;
1480 }
1481
1482 if (!data)
1483 goto out;
1484
1485 if (data[IFLA_GRE_REMOTE]) {
1486 nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
1487 if (ipv6_addr_any(&daddr))
1488 return -EINVAL;
1489 }
1490
1491out:
1492 return ip6gre_tunnel_validate(tb, data);
1493}
1494
1495
1496static void ip6gre_netlink_parms(struct nlattr *data[],
1497 struct __ip6_tnl_parm *parms)
1498{
1499 memset(parms, 0, sizeof(*parms));
1500
1501 if (!data)
1502 return;
1503
1504 if (data[IFLA_GRE_LINK])
1505 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1506
1507 if (data[IFLA_GRE_IFLAGS])
1508 parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
1509
1510 if (data[IFLA_GRE_OFLAGS])
1511 parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
1512
1513 if (data[IFLA_GRE_IKEY])
1514 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1515
1516 if (data[IFLA_GRE_OKEY])
1517 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1518
1519 if (data[IFLA_GRE_LOCAL])
1520 nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr));
1521
1522 if (data[IFLA_GRE_REMOTE])
1523 nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
1524
1525 if (data[IFLA_GRE_TTL])
1526 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1527
1528 if (data[IFLA_GRE_ENCAP_LIMIT])
1529 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1530
1531 if (data[IFLA_GRE_FLOWINFO])
1532 parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
1533
1534 if (data[IFLA_GRE_FLAGS])
1535 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1536}
1537
1538static int ip6gre_tap_init(struct net_device *dev)
1539{
1540 struct ip6_tnl *tunnel;
1541
1542 tunnel = netdev_priv(dev);
1543
1544 tunnel->dev = dev;
1545 strcpy(tunnel->parms.name, dev->name);
1546
1547 ip6gre_tnl_link_config(tunnel, 1);
1548
1549 dev->tstats = alloc_percpu(struct pcpu_tstats);
1550 if (!dev->tstats)
1551 return -ENOMEM;
1552
1553 return 0;
1554}
1555
1556static const struct net_device_ops ip6gre_tap_netdev_ops = {
1557 .ndo_init = ip6gre_tap_init,
1558 .ndo_uninit = ip6gre_tunnel_uninit,
1559 .ndo_start_xmit = ip6gre_tunnel_xmit,
1560 .ndo_set_mac_address = eth_mac_addr,
1561 .ndo_validate_addr = eth_validate_addr,
1562 .ndo_change_mtu = ip6gre_tunnel_change_mtu,
1563 .ndo_get_stats64 = ip6gre_get_stats64,
1564};
1565
1566static void ip6gre_tap_setup(struct net_device *dev)
1567{
1568
1569 ether_setup(dev);
1570
1571 dev->netdev_ops = &ip6gre_tap_netdev_ops;
1572 dev->destructor = ip6gre_dev_free;
1573
1574 dev->iflink = 0;
1575 dev->features |= NETIF_F_NETNS_LOCAL;
1576}
1577
1578static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
1579 struct nlattr *tb[], struct nlattr *data[])
1580{
1581 struct ip6_tnl *nt;
1582 struct net *net = dev_net(dev);
1583 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1584 int err;
1585
1586 nt = netdev_priv(dev);
1587 ip6gre_netlink_parms(data, &nt->parms);
1588
1589 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1590 return -EEXIST;
1591
1592 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1593 eth_hw_addr_random(dev);
1594
1595 nt->dev = dev;
1596 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1597
1598 /* Can use a lockless transmit, unless we generate output sequences */
1599 if (!(nt->parms.o_flags & GRE_SEQ))
1600 dev->features |= NETIF_F_LLTX;
1601
1602 err = register_netdevice(dev);
1603 if (err)
1604 goto out;
1605
1606 dev_hold(dev);
1607 ip6gre_tunnel_link(ign, nt);
1608
1609out:
1610 return err;
1611}
1612
1613static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
1614 struct nlattr *data[])
1615{
1616 struct ip6_tnl *t, *nt;
1617 struct net *net = dev_net(dev);
1618 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1619 struct __ip6_tnl_parm p;
1620
1621 if (dev == ign->fb_tunnel_dev)
1622 return -EINVAL;
1623
1624 nt = netdev_priv(dev);
1625 ip6gre_netlink_parms(data, &p);
1626
1627 t = ip6gre_tunnel_locate(net, &p, 0);
1628
1629 if (t) {
1630 if (t->dev != dev)
1631 return -EEXIST;
1632 } else {
1633 t = nt;
1634
1635 ip6gre_tunnel_unlink(ign, t);
1636 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1637 ip6gre_tunnel_link(ign, t);
1638 netdev_state_change(dev);
1639 }
1640
1641 return 0;
1642}
1643
1644static size_t ip6gre_get_size(const struct net_device *dev)
1645{
1646 return
1647 /* IFLA_GRE_LINK */
1648 nla_total_size(4) +
1649 /* IFLA_GRE_IFLAGS */
1650 nla_total_size(2) +
1651 /* IFLA_GRE_OFLAGS */
1652 nla_total_size(2) +
1653 /* IFLA_GRE_IKEY */
1654 nla_total_size(4) +
1655 /* IFLA_GRE_OKEY */
1656 nla_total_size(4) +
1657 /* IFLA_GRE_LOCAL */
1658 nla_total_size(4) +
1659 /* IFLA_GRE_REMOTE */
1660 nla_total_size(4) +
1661 /* IFLA_GRE_TTL */
1662 nla_total_size(1) +
1663 /* IFLA_GRE_TOS */
1664 nla_total_size(1) +
1665 /* IFLA_GRE_ENCAP_LIMIT */
1666 nla_total_size(1) +
1667 /* IFLA_GRE_FLOWINFO */
1668 nla_total_size(4) +
1669 /* IFLA_GRE_FLAGS */
1670 nla_total_size(4) +
1671 0;
1672}
1673
1674static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1675{
1676 struct ip6_tnl *t = netdev_priv(dev);
1677 struct __ip6_tnl_parm *p = &t->parms;
1678
1679 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
1680 nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
1681 nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
1682 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1683 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1684 nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->raddr) ||
1685 nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->laddr) ||
1686 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
1687 /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
1688 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1689 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
1690 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
1691 goto nla_put_failure;
1692 return 0;
1693
1694nla_put_failure:
1695 return -EMSGSIZE;
1696}
1697
1698static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
1699 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1700 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1701 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1702 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1703 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
1704 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
1705 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
1706 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1707 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
1708 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
1709 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
1710};
1711
1712static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
1713 .kind = "ip6gre",
1714 .maxtype = IFLA_GRE_MAX,
1715 .policy = ip6gre_policy,
1716 .priv_size = sizeof(struct ip6_tnl),
1717 .setup = ip6gre_tunnel_setup,
1718 .validate = ip6gre_tunnel_validate,
1719 .newlink = ip6gre_newlink,
1720 .changelink = ip6gre_changelink,
1721 .get_size = ip6gre_get_size,
1722 .fill_info = ip6gre_fill_info,
1723};
1724
1725static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
1726 .kind = "ip6gretap",
1727 .maxtype = IFLA_GRE_MAX,
1728 .policy = ip6gre_policy,
1729 .priv_size = sizeof(struct ip6_tnl),
1730 .setup = ip6gre_tap_setup,
1731 .validate = ip6gre_tap_validate,
1732 .newlink = ip6gre_newlink,
1733 .changelink = ip6gre_changelink,
1734 .get_size = ip6gre_get_size,
1735 .fill_info = ip6gre_fill_info,
1736};
1737
1738/*
1739 * And now the modules code and kernel interface.
1740 */
1741
1742static int __init ip6gre_init(void)
1743{
1744 int err;
1745
1746 pr_info("GRE over IPv6 tunneling driver\n");
1747
1748 err = register_pernet_device(&ip6gre_net_ops);
1749 if (err < 0)
1750 return err;
1751
1752 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
1753 if (err < 0) {
1754 pr_info("%s: can't add protocol\n", __func__);
1755 goto add_proto_failed;
1756 }
1757
1758 err = rtnl_link_register(&ip6gre_link_ops);
1759 if (err < 0)
1760 goto rtnl_link_failed;
1761
1762 err = rtnl_link_register(&ip6gre_tap_ops);
1763 if (err < 0)
1764 goto tap_ops_failed;
1765
1766out:
1767 return err;
1768
1769tap_ops_failed:
1770 rtnl_link_unregister(&ip6gre_link_ops);
1771rtnl_link_failed:
1772 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1773add_proto_failed:
1774 unregister_pernet_device(&ip6gre_net_ops);
1775 goto out;
1776}
1777
1778static void __exit ip6gre_fini(void)
1779{
1780 rtnl_link_unregister(&ip6gre_tap_ops);
1781 rtnl_link_unregister(&ip6gre_link_ops);
1782 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1783 unregister_pernet_device(&ip6gre_net_ops);
1784}
1785
1786module_init(ip6gre_init);
1787module_exit(ip6gre_fini);
1788MODULE_LICENSE("GPL");
1789MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1790MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1791MODULE_ALIAS_RTNL_LINK("ip6gre");
1792MODULE_ALIAS_NETDEV("ip6gre0");