2 * GRE over IPv6 protocol decoder.
4 * Authors: Dmitry Kozlov (xeb@mail.ru)
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.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
41 #include <net/ip_tunnels.h>
43 #include <net/protocol.h>
44 #include <net/addrconf.h>
46 #include <net/checksum.h>
47 #include <net/dsfield.h>
48 #include <net/inet_ecn.h>
50 #include <net/net_namespace.h>
51 #include <net/netns/generic.h>
52 #include <net/rtnetlink.h>
55 #include <net/ip6_fib.h>
56 #include <net/ip6_route.h>
57 #include <net/ip6_tunnel.h>
60 static bool log_ecn_error = true;
61 module_param(log_ecn_error, bool, 0644);
62 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
64 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
65 #define IPV6_TCLASS_SHIFT 20
67 #define HASH_SIZE_SHIFT 5
68 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
70 static int ip6gre_net_id __read_mostly;
72 struct ip6_tnl __rcu *tunnels[4][HASH_SIZE];
74 struct net_device *fb_tunnel_dev;
77 static struct rtnl_link_ops ip6gre_link_ops __read_mostly;
78 static int ip6gre_tunnel_init(struct net_device *dev);
79 static void ip6gre_tunnel_setup(struct net_device *dev);
80 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t);
81 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu);
83 /* Tunnel hash table */
93 We require exact key match i.e. if a key is present in packet
94 it will match only tunnel with the same key; if it is not present,
95 it will match only keyless tunnel.
97 All keysless packets, if not matched configured keyless tunnels
98 will match fallback tunnel.
101 #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(HASH_SIZE - 1))
102 static u32 HASH_ADDR(const struct in6_addr *addr)
104 u32 hash = ipv6_addr_hash(addr);
106 return hash_32(hash, HASH_SIZE_SHIFT);
109 #define tunnels_r_l tunnels[3]
110 #define tunnels_r tunnels[2]
111 #define tunnels_l tunnels[1]
112 #define tunnels_wc tunnels[0]
114 static struct rtnl_link_stats64 *ip6gre_get_stats64(struct net_device *dev,
115 struct rtnl_link_stats64 *tot)
119 for_each_possible_cpu(i) {
120 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
121 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
125 start = u64_stats_fetch_begin_bh(&tstats->syncp);
126 rx_packets = tstats->rx_packets;
127 tx_packets = tstats->tx_packets;
128 rx_bytes = tstats->rx_bytes;
129 tx_bytes = tstats->tx_bytes;
130 } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
132 tot->rx_packets += rx_packets;
133 tot->tx_packets += tx_packets;
134 tot->rx_bytes += rx_bytes;
135 tot->tx_bytes += tx_bytes;
138 tot->multicast = dev->stats.multicast;
139 tot->rx_crc_errors = dev->stats.rx_crc_errors;
140 tot->rx_fifo_errors = dev->stats.rx_fifo_errors;
141 tot->rx_length_errors = dev->stats.rx_length_errors;
142 tot->rx_frame_errors = dev->stats.rx_frame_errors;
143 tot->rx_errors = dev->stats.rx_errors;
145 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
146 tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
147 tot->tx_dropped = dev->stats.tx_dropped;
148 tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
149 tot->tx_errors = dev->stats.tx_errors;
154 /* Given src, dst and key, find appropriate for input tunnel. */
156 static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
157 const struct in6_addr *remote, const struct in6_addr *local,
158 __be32 key, __be16 gre_proto)
160 struct net *net = dev_net(dev);
161 int link = dev->ifindex;
162 unsigned int h0 = HASH_ADDR(remote);
163 unsigned int h1 = HASH_KEY(key);
164 struct ip6_tnl *t, *cand = NULL;
165 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
166 int dev_type = (gre_proto == htons(ETH_P_TEB)) ?
167 ARPHRD_ETHER : ARPHRD_IP6GRE;
168 int score, cand_score = 4;
170 for_each_ip_tunnel_rcu(t, ign->tunnels_r_l[h0 ^ h1]) {
171 if (!ipv6_addr_equal(local, &t->parms.laddr) ||
172 !ipv6_addr_equal(remote, &t->parms.raddr) ||
173 key != t->parms.i_key ||
174 !(t->dev->flags & IFF_UP))
177 if (t->dev->type != ARPHRD_IP6GRE &&
178 t->dev->type != dev_type)
182 if (t->parms.link != link)
184 if (t->dev->type != dev_type)
189 if (score < cand_score) {
195 for_each_ip_tunnel_rcu(t, ign->tunnels_r[h0 ^ h1]) {
196 if (!ipv6_addr_equal(remote, &t->parms.raddr) ||
197 key != t->parms.i_key ||
198 !(t->dev->flags & IFF_UP))
201 if (t->dev->type != ARPHRD_IP6GRE &&
202 t->dev->type != dev_type)
206 if (t->parms.link != link)
208 if (t->dev->type != dev_type)
213 if (score < cand_score) {
219 for_each_ip_tunnel_rcu(t, ign->tunnels_l[h1]) {
220 if ((!ipv6_addr_equal(local, &t->parms.laddr) &&
221 (!ipv6_addr_equal(local, &t->parms.raddr) ||
222 !ipv6_addr_is_multicast(local))) ||
223 key != t->parms.i_key ||
224 !(t->dev->flags & IFF_UP))
227 if (t->dev->type != ARPHRD_IP6GRE &&
228 t->dev->type != dev_type)
232 if (t->parms.link != link)
234 if (t->dev->type != dev_type)
239 if (score < cand_score) {
245 for_each_ip_tunnel_rcu(t, ign->tunnels_wc[h1]) {
246 if (t->parms.i_key != key ||
247 !(t->dev->flags & IFF_UP))
250 if (t->dev->type != ARPHRD_IP6GRE &&
251 t->dev->type != dev_type)
255 if (t->parms.link != link)
257 if (t->dev->type != dev_type)
262 if (score < cand_score) {
271 dev = ign->fb_tunnel_dev;
272 if (dev->flags & IFF_UP)
273 return netdev_priv(dev);
278 static struct ip6_tnl __rcu **__ip6gre_bucket(struct ip6gre_net *ign,
279 const struct __ip6_tnl_parm *p)
281 const struct in6_addr *remote = &p->raddr;
282 const struct in6_addr *local = &p->laddr;
283 unsigned int h = HASH_KEY(p->i_key);
286 if (!ipv6_addr_any(local))
288 if (!ipv6_addr_any(remote) && !ipv6_addr_is_multicast(remote)) {
290 h ^= HASH_ADDR(remote);
293 return &ign->tunnels[prio][h];
296 static inline struct ip6_tnl __rcu **ip6gre_bucket(struct ip6gre_net *ign,
297 const struct ip6_tnl *t)
299 return __ip6gre_bucket(ign, &t->parms);
302 static void ip6gre_tunnel_link(struct ip6gre_net *ign, struct ip6_tnl *t)
304 struct ip6_tnl __rcu **tp = ip6gre_bucket(ign, t);
306 rcu_assign_pointer(t->next, rtnl_dereference(*tp));
307 rcu_assign_pointer(*tp, t);
310 static void ip6gre_tunnel_unlink(struct ip6gre_net *ign, struct ip6_tnl *t)
312 struct ip6_tnl __rcu **tp;
313 struct ip6_tnl *iter;
315 for (tp = ip6gre_bucket(ign, t);
316 (iter = rtnl_dereference(*tp)) != NULL;
319 rcu_assign_pointer(*tp, t->next);
325 static struct ip6_tnl *ip6gre_tunnel_find(struct net *net,
326 const struct __ip6_tnl_parm *parms,
329 const struct in6_addr *remote = &parms->raddr;
330 const struct in6_addr *local = &parms->laddr;
331 __be32 key = parms->i_key;
332 int link = parms->link;
334 struct ip6_tnl __rcu **tp;
335 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
337 for (tp = __ip6gre_bucket(ign, parms);
338 (t = rtnl_dereference(*tp)) != NULL;
340 if (ipv6_addr_equal(local, &t->parms.laddr) &&
341 ipv6_addr_equal(remote, &t->parms.raddr) &&
342 key == t->parms.i_key &&
343 link == t->parms.link &&
344 type == t->dev->type)
350 static struct ip6_tnl *ip6gre_tunnel_locate(struct net *net,
351 const struct __ip6_tnl_parm *parms, int create)
353 struct ip6_tnl *t, *nt;
354 struct net_device *dev;
356 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
358 t = ip6gre_tunnel_find(net, parms, ARPHRD_IP6GRE);
363 strlcpy(name, parms->name, IFNAMSIZ);
365 strcpy(name, "ip6gre%d");
367 dev = alloc_netdev(sizeof(*t), name, ip6gre_tunnel_setup);
371 dev_net_set(dev, net);
373 nt = netdev_priv(dev);
375 dev->rtnl_link_ops = &ip6gre_link_ops;
378 ip6gre_tnl_link_config(nt, 1);
380 if (register_netdevice(dev) < 0)
383 /* Can use a lockless transmit, unless we generate output sequences */
384 if (!(nt->parms.o_flags & GRE_SEQ))
385 dev->features |= NETIF_F_LLTX;
388 ip6gre_tunnel_link(ign, nt);
396 static void ip6gre_tunnel_uninit(struct net_device *dev)
398 struct net *net = dev_net(dev);
399 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
401 ip6gre_tunnel_unlink(ign, netdev_priv(dev));
406 static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
407 u8 type, u8 code, int offset, __be32 info)
409 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *)skb->data;
410 __be16 *p = (__be16 *)(skb->data + offset);
411 int grehlen = offset + 4;
416 if (flags&(GRE_CSUM|GRE_KEY|GRE_SEQ|GRE_ROUTING|GRE_VERSION)) {
417 if (flags&(GRE_VERSION|GRE_ROUTING))
426 /* If only 8 bytes returned, keyed message will be dropped here */
427 if (!pskb_may_pull(skb, grehlen))
429 ipv6h = (const struct ipv6hdr *)skb->data;
430 p = (__be16 *)(skb->data + offset);
432 t = ip6gre_tunnel_lookup(skb->dev, &ipv6h->daddr, &ipv6h->saddr,
434 *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
441 struct ipv6_tlv_tnl_enc_lim *tel;
443 case ICMPV6_DEST_UNREACH:
444 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
447 case ICMPV6_TIME_EXCEED:
448 if (code == ICMPV6_EXC_HOPLIMIT) {
449 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
453 case ICMPV6_PARAMPROB:
455 if (code == ICMPV6_HDR_FIELD)
456 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
458 if (teli && teli == info - 2) {
459 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
460 if (tel->encap_limit == 0) {
461 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
465 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
469 case ICMPV6_PKT_TOOBIG:
471 if (mtu < IPV6_MIN_MTU)
477 if (time_before(jiffies, t->err_time + IP6TUNNEL_ERR_TIMEO))
481 t->err_time = jiffies;
484 static int ip6gre_rcv(struct sk_buff *skb)
486 const struct ipv6hdr *ipv6h;
492 struct ip6_tnl *tunnel;
497 if (!pskb_may_pull(skb, sizeof(struct in6_addr)))
500 ipv6h = ipv6_hdr(skb);
502 flags = *(__be16 *)h;
504 if (flags&(GRE_CSUM|GRE_KEY|GRE_ROUTING|GRE_SEQ|GRE_VERSION)) {
505 /* - Version must be 0.
506 - We do not support routing headers.
508 if (flags&(GRE_VERSION|GRE_ROUTING))
511 if (flags&GRE_CSUM) {
512 switch (skb->ip_summed) {
513 case CHECKSUM_COMPLETE:
514 csum = csum_fold(skb->csum);
520 csum = __skb_checksum_complete(skb);
521 skb->ip_summed = CHECKSUM_COMPLETE;
526 key = *(__be32 *)(h + offset);
530 seqno = ntohl(*(__be32 *)(h + offset));
535 gre_proto = *(__be16 *)(h + 2);
537 tunnel = ip6gre_tunnel_lookup(skb->dev,
538 &ipv6h->saddr, &ipv6h->daddr, key,
541 struct pcpu_tstats *tstats;
543 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
546 if (!ip6_tnl_rcv_ctl(tunnel, &ipv6h->daddr, &ipv6h->saddr)) {
547 tunnel->dev->stats.rx_dropped++;
553 skb->protocol = gre_proto;
554 /* WCCP version 1 and 2 protocol decoding.
555 * - Change protocol to IP
556 * - When dealing with WCCPv2, Skip extra 4 bytes in GRE header
558 if (flags == 0 && gre_proto == htons(ETH_P_WCCP)) {
559 skb->protocol = htons(ETH_P_IP);
560 if ((*(h + offset) & 0xF0) != 0x40)
564 skb->mac_header = skb->network_header;
565 __pskb_pull(skb, offset);
566 skb_postpull_rcsum(skb, skb_transport_header(skb), offset);
567 skb->pkt_type = PACKET_HOST;
569 if (((flags&GRE_CSUM) && csum) ||
570 (!(flags&GRE_CSUM) && tunnel->parms.i_flags&GRE_CSUM)) {
571 tunnel->dev->stats.rx_crc_errors++;
572 tunnel->dev->stats.rx_errors++;
575 if (tunnel->parms.i_flags&GRE_SEQ) {
576 if (!(flags&GRE_SEQ) ||
578 (s32)(seqno - tunnel->i_seqno) < 0)) {
579 tunnel->dev->stats.rx_fifo_errors++;
580 tunnel->dev->stats.rx_errors++;
583 tunnel->i_seqno = seqno + 1;
586 /* Warning: All skb pointers will be invalidated! */
587 if (tunnel->dev->type == ARPHRD_ETHER) {
588 if (!pskb_may_pull(skb, ETH_HLEN)) {
589 tunnel->dev->stats.rx_length_errors++;
590 tunnel->dev->stats.rx_errors++;
594 ipv6h = ipv6_hdr(skb);
595 skb->protocol = eth_type_trans(skb, tunnel->dev);
596 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
599 __skb_tunnel_rx(skb, tunnel->dev);
601 skb_reset_network_header(skb);
603 err = IP6_ECN_decapsulate(ipv6h, skb);
606 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
608 ipv6_get_dsfield(ipv6h));
610 ++tunnel->dev->stats.rx_frame_errors;
611 ++tunnel->dev->stats.rx_errors;
616 tstats = this_cpu_ptr(tunnel->dev->tstats);
617 u64_stats_update_begin(&tstats->syncp);
618 tstats->rx_packets++;
619 tstats->rx_bytes += skb->len;
620 u64_stats_update_end(&tstats->syncp);
626 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
633 struct ipv6_tel_txoption {
634 struct ipv6_txoptions ops;
638 static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
640 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
642 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
644 opt->dst_opt[4] = encap_limit;
645 opt->dst_opt[5] = IPV6_TLV_PADN;
648 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
649 opt->ops.opt_nflen = 8;
652 static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
653 struct net_device *dev,
659 struct net *net = dev_net(dev);
660 struct ip6_tnl *tunnel = netdev_priv(dev);
661 struct net_device *tdev; /* Device to other host */
662 struct ipv6hdr *ipv6h; /* Our new IP header */
663 unsigned int max_headroom; /* The extra header space needed */
665 struct ipv6_tel_txoption opt;
667 struct dst_entry *dst = NULL, *ndst = NULL;
668 struct net_device_stats *stats = &tunnel->dev->stats;
671 struct sk_buff *new_skb;
673 if (dev->type == ARPHRD_ETHER)
674 IPCB(skb)->flags = 0;
676 if (dev->header_ops && dev->type == ARPHRD_IP6GRE) {
678 ipv6h = (struct ipv6hdr *)skb->data;
679 fl6->daddr = ipv6h->daddr;
681 gre_hlen = tunnel->hlen;
682 fl6->daddr = tunnel->parms.raddr;
685 if (!fl6->flowi6_mark)
686 dst = ip6_tnl_dst_check(tunnel);
689 ndst = ip6_route_output(net, NULL, fl6);
692 goto tx_err_link_failure;
693 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
697 goto tx_err_link_failure;
706 net_warn_ratelimited("%s: Local routing loop detected!\n",
708 goto tx_err_dst_release;
711 mtu = dst_mtu(dst) - sizeof(*ipv6h);
712 if (encap_limit >= 0) {
716 if (mtu < IPV6_MIN_MTU)
719 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
720 if (skb->len > mtu) {
723 goto tx_err_dst_release;
726 if (tunnel->err_count > 0) {
727 if (time_before(jiffies,
728 tunnel->err_time + IP6TUNNEL_ERR_TIMEO)) {
731 dst_link_failure(skb);
733 tunnel->err_count = 0;
736 max_headroom = LL_RESERVED_SPACE(tdev) + gre_hlen + dst->header_len;
738 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
739 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
740 new_skb = skb_realloc_headroom(skb, max_headroom);
741 if (max_headroom > dev->needed_headroom)
742 dev->needed_headroom = max_headroom;
744 goto tx_err_dst_release;
747 skb_set_owner_w(new_skb, skb->sk);
754 if (fl6->flowi6_mark) {
755 skb_dst_set(skb, dst);
758 skb_dst_set_noref(skb, dst);
762 if (encap_limit >= 0) {
763 init_tel_txopt(&opt, encap_limit);
764 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
767 skb_push(skb, gre_hlen);
768 skb_reset_network_header(skb);
769 skb_set_transport_header(skb, sizeof(*ipv6h));
772 * Push down and install the IP header.
774 ipv6h = ipv6_hdr(skb);
775 ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), fl6->flowlabel);
776 ipv6h->hop_limit = tunnel->parms.hop_limit;
777 ipv6h->nexthdr = proto;
778 ipv6h->saddr = fl6->saddr;
779 ipv6h->daddr = fl6->daddr;
781 ((__be16 *)(ipv6h + 1))[0] = tunnel->parms.o_flags;
782 ((__be16 *)(ipv6h + 1))[1] = (dev->type == ARPHRD_ETHER) ?
783 htons(ETH_P_TEB) : skb->protocol;
785 if (tunnel->parms.o_flags&(GRE_KEY|GRE_CSUM|GRE_SEQ)) {
786 __be32 *ptr = (__be32 *)(((u8 *)ipv6h) + tunnel->hlen - 4);
788 if (tunnel->parms.o_flags&GRE_SEQ) {
790 *ptr = htonl(tunnel->o_seqno);
793 if (tunnel->parms.o_flags&GRE_KEY) {
794 *ptr = tunnel->parms.o_key;
797 if (tunnel->parms.o_flags&GRE_CSUM) {
799 *(__sum16 *)ptr = ip_compute_csum((void *)(ipv6h+1),
800 skb->len - sizeof(struct ipv6hdr));
804 ip6tunnel_xmit(skb, dev);
806 ip6_tnl_dst_store(tunnel, ndst);
809 stats->tx_carrier_errors++;
810 dst_link_failure(skb);
816 static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)
818 struct ip6_tnl *t = netdev_priv(dev);
819 const struct iphdr *iph = ip_hdr(skb);
820 int encap_limit = -1;
826 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
827 encap_limit = t->parms.encap_limit;
829 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
830 fl6.flowi6_proto = IPPROTO_IPIP;
832 dsfield = ipv4_get_dsfield(iph);
834 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
835 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
837 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
838 fl6.flowi6_mark = skb->mark;
840 err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
842 /* XXX: send ICMP error even if DF is not set. */
843 if (err == -EMSGSIZE)
844 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
852 static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)
854 struct ip6_tnl *t = netdev_priv(dev);
855 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
856 int encap_limit = -1;
863 if (ipv6_addr_equal(&t->parms.raddr, &ipv6h->saddr))
866 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
868 struct ipv6_tlv_tnl_enc_lim *tel;
869 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
870 if (tel->encap_limit == 0) {
871 icmpv6_send(skb, ICMPV6_PARAMPROB,
872 ICMPV6_HDR_FIELD, offset + 2);
875 encap_limit = tel->encap_limit - 1;
876 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
877 encap_limit = t->parms.encap_limit;
879 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
880 fl6.flowi6_proto = IPPROTO_IPV6;
882 dsfield = ipv6_get_dsfield(ipv6h);
883 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
884 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
885 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
886 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
887 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
888 fl6.flowi6_mark = skb->mark;
890 err = ip6gre_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
892 if (err == -EMSGSIZE)
893 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
901 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
902 * @t: the outgoing tunnel device
903 * @hdr: IPv6 header from the incoming packet
906 * Avoid trivial tunneling loop by checking that tunnel exit-point
907 * doesn't match source of incoming packet.
914 static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl *t,
915 const struct ipv6hdr *hdr)
917 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
920 static int ip6gre_xmit_other(struct sk_buff *skb, struct net_device *dev)
922 struct ip6_tnl *t = netdev_priv(dev);
923 int encap_limit = -1;
928 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
929 encap_limit = t->parms.encap_limit;
931 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
932 fl6.flowi6_proto = skb->protocol;
934 err = ip6gre_xmit2(skb, dev, 0, &fl6, encap_limit, &mtu);
939 static netdev_tx_t ip6gre_tunnel_xmit(struct sk_buff *skb,
940 struct net_device *dev)
942 struct ip6_tnl *t = netdev_priv(dev);
943 struct net_device_stats *stats = &t->dev->stats;
946 if (!ip6_tnl_xmit_ctl(t))
949 switch (skb->protocol) {
950 case htons(ETH_P_IP):
951 ret = ip6gre_xmit_ipv4(skb, dev);
953 case htons(ETH_P_IPV6):
954 ret = ip6gre_xmit_ipv6(skb, dev);
957 ret = ip6gre_xmit_other(skb, dev);
973 static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
975 struct net_device *dev = t->dev;
976 struct __ip6_tnl_parm *p = &t->parms;
977 struct flowi6 *fl6 = &t->fl.u.ip6;
978 int addend = sizeof(struct ipv6hdr) + 4;
980 if (dev->type != ARPHRD_ETHER) {
981 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
982 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
985 /* Set up flowi template */
986 fl6->saddr = p->laddr;
987 fl6->daddr = p->raddr;
988 fl6->flowi6_oif = p->link;
991 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
992 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
993 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
994 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
996 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
997 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
999 if (p->flags&IP6_TNL_F_CAP_XMIT &&
1000 p->flags&IP6_TNL_F_CAP_RCV && dev->type != ARPHRD_ETHER)
1001 dev->flags |= IFF_POINTOPOINT;
1003 dev->flags &= ~IFF_POINTOPOINT;
1005 dev->iflink = p->link;
1007 /* Precalculate GRE options length */
1008 if (t->parms.o_flags&(GRE_CSUM|GRE_KEY|GRE_SEQ)) {
1009 if (t->parms.o_flags&GRE_CSUM)
1011 if (t->parms.o_flags&GRE_KEY)
1013 if (t->parms.o_flags&GRE_SEQ)
1017 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1018 int strict = (ipv6_addr_type(&p->raddr) &
1019 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1021 struct rt6_info *rt = rt6_lookup(dev_net(dev),
1022 &p->raddr, &p->laddr,
1029 dev->hard_header_len = rt->dst.dev->hard_header_len + addend;
1032 dev->mtu = rt->dst.dev->mtu - addend;
1033 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1036 if (dev->mtu < IPV6_MIN_MTU)
1037 dev->mtu = IPV6_MIN_MTU;
1046 static int ip6gre_tnl_change(struct ip6_tnl *t,
1047 const struct __ip6_tnl_parm *p, int set_mtu)
1049 t->parms.laddr = p->laddr;
1050 t->parms.raddr = p->raddr;
1051 t->parms.flags = p->flags;
1052 t->parms.hop_limit = p->hop_limit;
1053 t->parms.encap_limit = p->encap_limit;
1054 t->parms.flowinfo = p->flowinfo;
1055 t->parms.link = p->link;
1056 t->parms.proto = p->proto;
1057 t->parms.i_key = p->i_key;
1058 t->parms.o_key = p->o_key;
1059 t->parms.i_flags = p->i_flags;
1060 t->parms.o_flags = p->o_flags;
1061 ip6_tnl_dst_reset(t);
1062 ip6gre_tnl_link_config(t, set_mtu);
1066 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm *p,
1067 const struct ip6_tnl_parm2 *u)
1069 p->laddr = u->laddr;
1070 p->raddr = u->raddr;
1071 p->flags = u->flags;
1072 p->hop_limit = u->hop_limit;
1073 p->encap_limit = u->encap_limit;
1074 p->flowinfo = u->flowinfo;
1076 p->i_key = u->i_key;
1077 p->o_key = u->o_key;
1078 p->i_flags = u->i_flags;
1079 p->o_flags = u->o_flags;
1080 memcpy(p->name, u->name, sizeof(u->name));
1083 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2 *u,
1084 const struct __ip6_tnl_parm *p)
1086 u->proto = IPPROTO_GRE;
1087 u->laddr = p->laddr;
1088 u->raddr = p->raddr;
1089 u->flags = p->flags;
1090 u->hop_limit = p->hop_limit;
1091 u->encap_limit = p->encap_limit;
1092 u->flowinfo = p->flowinfo;
1094 u->i_key = p->i_key;
1095 u->o_key = p->o_key;
1096 u->i_flags = p->i_flags;
1097 u->o_flags = p->o_flags;
1098 memcpy(u->name, p->name, sizeof(u->name));
1101 static int ip6gre_tunnel_ioctl(struct net_device *dev,
1102 struct ifreq *ifr, int cmd)
1105 struct ip6_tnl_parm2 p;
1106 struct __ip6_tnl_parm p1;
1108 struct net *net = dev_net(dev);
1109 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1114 if (dev == ign->fb_tunnel_dev) {
1115 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
1119 ip6gre_tnl_parm_from_user(&p1, &p);
1120 t = ip6gre_tunnel_locate(net, &p1, 0);
1123 t = netdev_priv(dev);
1124 ip6gre_tnl_parm_to_user(&p, &t->parms);
1125 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1132 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1136 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1140 if ((p.i_flags|p.o_flags)&(GRE_VERSION|GRE_ROUTING))
1143 if (!(p.i_flags&GRE_KEY))
1145 if (!(p.o_flags&GRE_KEY))
1148 ip6gre_tnl_parm_from_user(&p1, &p);
1149 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1151 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1153 if (t->dev != dev) {
1158 t = netdev_priv(dev);
1160 ip6gre_tunnel_unlink(ign, t);
1162 ip6gre_tnl_change(t, &p1, 1);
1163 ip6gre_tunnel_link(ign, t);
1164 netdev_state_change(dev);
1171 ip6gre_tnl_parm_to_user(&p, &t->parms);
1172 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
1175 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1180 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1183 if (dev == ign->fb_tunnel_dev) {
1185 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
1188 ip6gre_tnl_parm_from_user(&p1, &p);
1189 t = ip6gre_tunnel_locate(net, &p1, 0);
1193 if (t == netdev_priv(ign->fb_tunnel_dev))
1197 unregister_netdevice(dev);
1209 static int ip6gre_tunnel_change_mtu(struct net_device *dev, int new_mtu)
1211 struct ip6_tnl *tunnel = netdev_priv(dev);
1213 new_mtu > 0xFFF8 - dev->hard_header_len - tunnel->hlen)
1219 static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
1220 unsigned short type,
1221 const void *daddr, const void *saddr, unsigned int len)
1223 struct ip6_tnl *t = netdev_priv(dev);
1224 struct ipv6hdr *ipv6h = (struct ipv6hdr *)skb_push(skb, t->hlen);
1225 __be16 *p = (__be16 *)(ipv6h+1);
1227 ip6_flow_hdr(ipv6h, 0, t->fl.u.ip6.flowlabel);
1228 ipv6h->hop_limit = t->parms.hop_limit;
1229 ipv6h->nexthdr = NEXTHDR_GRE;
1230 ipv6h->saddr = t->parms.laddr;
1231 ipv6h->daddr = t->parms.raddr;
1233 p[0] = t->parms.o_flags;
1237 * Set the source hardware address.
1241 memcpy(&ipv6h->saddr, saddr, sizeof(struct in6_addr));
1243 memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
1244 if (!ipv6_addr_any(&ipv6h->daddr))
1250 static const struct header_ops ip6gre_header_ops = {
1251 .create = ip6gre_header,
1254 static const struct net_device_ops ip6gre_netdev_ops = {
1255 .ndo_init = ip6gre_tunnel_init,
1256 .ndo_uninit = ip6gre_tunnel_uninit,
1257 .ndo_start_xmit = ip6gre_tunnel_xmit,
1258 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
1259 .ndo_change_mtu = ip6gre_tunnel_change_mtu,
1260 .ndo_get_stats64 = ip6gre_get_stats64,
1263 static void ip6gre_dev_free(struct net_device *dev)
1265 free_percpu(dev->tstats);
1269 static void ip6gre_tunnel_setup(struct net_device *dev)
1273 dev->netdev_ops = &ip6gre_netdev_ops;
1274 dev->destructor = ip6gre_dev_free;
1276 dev->type = ARPHRD_IP6GRE;
1277 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr) + 4;
1278 dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr) - 4;
1279 t = netdev_priv(dev);
1280 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1282 dev->flags |= IFF_NOARP;
1284 dev->addr_len = sizeof(struct in6_addr);
1285 dev->features |= NETIF_F_NETNS_LOCAL;
1286 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1289 static int ip6gre_tunnel_init(struct net_device *dev)
1291 struct ip6_tnl *tunnel;
1293 tunnel = netdev_priv(dev);
1296 strcpy(tunnel->parms.name, dev->name);
1298 memcpy(dev->dev_addr, &tunnel->parms.laddr, sizeof(struct in6_addr));
1299 memcpy(dev->broadcast, &tunnel->parms.raddr, sizeof(struct in6_addr));
1301 if (ipv6_addr_any(&tunnel->parms.raddr))
1302 dev->header_ops = &ip6gre_header_ops;
1304 dev->tstats = alloc_percpu(struct pcpu_tstats);
1311 static void ip6gre_fb_tunnel_init(struct net_device *dev)
1313 struct ip6_tnl *tunnel = netdev_priv(dev);
1316 strcpy(tunnel->parms.name, dev->name);
1318 tunnel->hlen = sizeof(struct ipv6hdr) + 4;
1324 static struct inet6_protocol ip6gre_protocol __read_mostly = {
1325 .handler = ip6gre_rcv,
1326 .err_handler = ip6gre_err,
1327 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1330 static void ip6gre_destroy_tunnels(struct ip6gre_net *ign,
1331 struct list_head *head)
1335 for (prio = 0; prio < 4; prio++) {
1337 for (h = 0; h < HASH_SIZE; h++) {
1340 t = rtnl_dereference(ign->tunnels[prio][h]);
1343 unregister_netdevice_queue(t->dev, head);
1344 t = rtnl_dereference(t->next);
1350 static int __net_init ip6gre_init_net(struct net *net)
1352 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1355 ign->fb_tunnel_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6gre0",
1356 ip6gre_tunnel_setup);
1357 if (!ign->fb_tunnel_dev) {
1361 dev_net_set(ign->fb_tunnel_dev, net);
1363 ip6gre_fb_tunnel_init(ign->fb_tunnel_dev);
1364 ign->fb_tunnel_dev->rtnl_link_ops = &ip6gre_link_ops;
1366 err = register_netdev(ign->fb_tunnel_dev);
1370 rcu_assign_pointer(ign->tunnels_wc[0],
1371 netdev_priv(ign->fb_tunnel_dev));
1375 ip6gre_dev_free(ign->fb_tunnel_dev);
1380 static void __net_exit ip6gre_exit_net(struct net *net)
1382 struct ip6gre_net *ign;
1385 ign = net_generic(net, ip6gre_net_id);
1387 ip6gre_destroy_tunnels(ign, &list);
1388 unregister_netdevice_many(&list);
1392 static struct pernet_operations ip6gre_net_ops = {
1393 .init = ip6gre_init_net,
1394 .exit = ip6gre_exit_net,
1395 .id = &ip6gre_net_id,
1396 .size = sizeof(struct ip6gre_net),
1399 static int ip6gre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[])
1407 if (data[IFLA_GRE_IFLAGS])
1408 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1409 if (data[IFLA_GRE_OFLAGS])
1410 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1411 if (flags & (GRE_VERSION|GRE_ROUTING))
1417 static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
1419 struct in6_addr daddr;
1421 if (tb[IFLA_ADDRESS]) {
1422 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1424 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1425 return -EADDRNOTAVAIL;
1431 if (data[IFLA_GRE_REMOTE]) {
1432 nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
1433 if (ipv6_addr_any(&daddr))
1438 return ip6gre_tunnel_validate(tb, data);
1442 static void ip6gre_netlink_parms(struct nlattr *data[],
1443 struct __ip6_tnl_parm *parms)
1445 memset(parms, 0, sizeof(*parms));
1450 if (data[IFLA_GRE_LINK])
1451 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1453 if (data[IFLA_GRE_IFLAGS])
1454 parms->i_flags = nla_get_be16(data[IFLA_GRE_IFLAGS]);
1456 if (data[IFLA_GRE_OFLAGS])
1457 parms->o_flags = nla_get_be16(data[IFLA_GRE_OFLAGS]);
1459 if (data[IFLA_GRE_IKEY])
1460 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1462 if (data[IFLA_GRE_OKEY])
1463 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1465 if (data[IFLA_GRE_LOCAL])
1466 nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr));
1468 if (data[IFLA_GRE_REMOTE])
1469 nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr));
1471 if (data[IFLA_GRE_TTL])
1472 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
1474 if (data[IFLA_GRE_ENCAP_LIMIT])
1475 parms->encap_limit = nla_get_u8(data[IFLA_GRE_ENCAP_LIMIT]);
1477 if (data[IFLA_GRE_FLOWINFO])
1478 parms->flowinfo = nla_get_u32(data[IFLA_GRE_FLOWINFO]);
1480 if (data[IFLA_GRE_FLAGS])
1481 parms->flags = nla_get_u32(data[IFLA_GRE_FLAGS]);
1484 static int ip6gre_tap_init(struct net_device *dev)
1486 struct ip6_tnl *tunnel;
1488 tunnel = netdev_priv(dev);
1491 strcpy(tunnel->parms.name, dev->name);
1493 ip6gre_tnl_link_config(tunnel, 1);
1495 dev->tstats = alloc_percpu(struct pcpu_tstats);
1502 static const struct net_device_ops ip6gre_tap_netdev_ops = {
1503 .ndo_init = ip6gre_tap_init,
1504 .ndo_uninit = ip6gre_tunnel_uninit,
1505 .ndo_start_xmit = ip6gre_tunnel_xmit,
1506 .ndo_set_mac_address = eth_mac_addr,
1507 .ndo_validate_addr = eth_validate_addr,
1508 .ndo_change_mtu = ip6gre_tunnel_change_mtu,
1509 .ndo_get_stats64 = ip6gre_get_stats64,
1512 static void ip6gre_tap_setup(struct net_device *dev)
1517 dev->netdev_ops = &ip6gre_tap_netdev_ops;
1518 dev->destructor = ip6gre_dev_free;
1521 dev->features |= NETIF_F_NETNS_LOCAL;
1524 static int ip6gre_newlink(struct net *src_net, struct net_device *dev,
1525 struct nlattr *tb[], struct nlattr *data[])
1528 struct net *net = dev_net(dev);
1529 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1532 nt = netdev_priv(dev);
1533 ip6gre_netlink_parms(data, &nt->parms);
1535 if (ip6gre_tunnel_find(net, &nt->parms, dev->type))
1538 if (dev->type == ARPHRD_ETHER && !tb[IFLA_ADDRESS])
1539 eth_hw_addr_random(dev);
1542 ip6gre_tnl_link_config(nt, !tb[IFLA_MTU]);
1544 /* Can use a lockless transmit, unless we generate output sequences */
1545 if (!(nt->parms.o_flags & GRE_SEQ))
1546 dev->features |= NETIF_F_LLTX;
1548 err = register_netdevice(dev);
1553 ip6gre_tunnel_link(ign, nt);
1559 static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
1560 struct nlattr *data[])
1562 struct ip6_tnl *t, *nt;
1563 struct net *net = dev_net(dev);
1564 struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
1565 struct __ip6_tnl_parm p;
1567 if (dev == ign->fb_tunnel_dev)
1570 nt = netdev_priv(dev);
1571 ip6gre_netlink_parms(data, &p);
1573 t = ip6gre_tunnel_locate(net, &p, 0);
1581 ip6gre_tunnel_unlink(ign, t);
1582 ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
1583 ip6gre_tunnel_link(ign, t);
1584 netdev_state_change(dev);
1590 static size_t ip6gre_get_size(const struct net_device *dev)
1595 /* IFLA_GRE_IFLAGS */
1597 /* IFLA_GRE_OFLAGS */
1603 /* IFLA_GRE_LOCAL */
1604 nla_total_size(sizeof(struct in6_addr)) +
1605 /* IFLA_GRE_REMOTE */
1606 nla_total_size(sizeof(struct in6_addr)) +
1611 /* IFLA_GRE_ENCAP_LIMIT */
1613 /* IFLA_GRE_FLOWINFO */
1615 /* IFLA_GRE_FLAGS */
1620 static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1622 struct ip6_tnl *t = netdev_priv(dev);
1623 struct __ip6_tnl_parm *p = &t->parms;
1625 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
1626 nla_put_be16(skb, IFLA_GRE_IFLAGS, p->i_flags) ||
1627 nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
1628 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1629 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1630 nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->laddr) ||
1631 nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->raddr) ||
1632 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
1633 /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
1634 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
1635 nla_put_be32(skb, IFLA_GRE_FLOWINFO, p->flowinfo) ||
1636 nla_put_u32(skb, IFLA_GRE_FLAGS, p->flags))
1637 goto nla_put_failure;
1644 static const struct nla_policy ip6gre_policy[IFLA_GRE_MAX + 1] = {
1645 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1646 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1647 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1648 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1649 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
1650 [IFLA_GRE_LOCAL] = { .len = FIELD_SIZEOF(struct ipv6hdr, saddr) },
1651 [IFLA_GRE_REMOTE] = { .len = FIELD_SIZEOF(struct ipv6hdr, daddr) },
1652 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1653 [IFLA_GRE_ENCAP_LIMIT] = { .type = NLA_U8 },
1654 [IFLA_GRE_FLOWINFO] = { .type = NLA_U32 },
1655 [IFLA_GRE_FLAGS] = { .type = NLA_U32 },
1658 static struct rtnl_link_ops ip6gre_link_ops __read_mostly = {
1660 .maxtype = IFLA_GRE_MAX,
1661 .policy = ip6gre_policy,
1662 .priv_size = sizeof(struct ip6_tnl),
1663 .setup = ip6gre_tunnel_setup,
1664 .validate = ip6gre_tunnel_validate,
1665 .newlink = ip6gre_newlink,
1666 .changelink = ip6gre_changelink,
1667 .get_size = ip6gre_get_size,
1668 .fill_info = ip6gre_fill_info,
1671 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly = {
1672 .kind = "ip6gretap",
1673 .maxtype = IFLA_GRE_MAX,
1674 .policy = ip6gre_policy,
1675 .priv_size = sizeof(struct ip6_tnl),
1676 .setup = ip6gre_tap_setup,
1677 .validate = ip6gre_tap_validate,
1678 .newlink = ip6gre_newlink,
1679 .changelink = ip6gre_changelink,
1680 .get_size = ip6gre_get_size,
1681 .fill_info = ip6gre_fill_info,
1685 * And now the modules code and kernel interface.
1688 static int __init ip6gre_init(void)
1692 pr_info("GRE over IPv6 tunneling driver\n");
1694 err = register_pernet_device(&ip6gre_net_ops);
1698 err = inet6_add_protocol(&ip6gre_protocol, IPPROTO_GRE);
1700 pr_info("%s: can't add protocol\n", __func__);
1701 goto add_proto_failed;
1704 err = rtnl_link_register(&ip6gre_link_ops);
1706 goto rtnl_link_failed;
1708 err = rtnl_link_register(&ip6gre_tap_ops);
1710 goto tap_ops_failed;
1716 rtnl_link_unregister(&ip6gre_link_ops);
1718 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1720 unregister_pernet_device(&ip6gre_net_ops);
1724 static void __exit ip6gre_fini(void)
1726 rtnl_link_unregister(&ip6gre_tap_ops);
1727 rtnl_link_unregister(&ip6gre_link_ops);
1728 inet6_del_protocol(&ip6gre_protocol, IPPROTO_GRE);
1729 unregister_pernet_device(&ip6gre_net_ops);
1732 module_init(ip6gre_init);
1733 module_exit(ip6gre_fini);
1734 MODULE_LICENSE("GPL");
1735 MODULE_AUTHOR("D. Kozlov (xeb@mail.ru)");
1736 MODULE_DESCRIPTION("GRE over IPv6 tunneling device");
1737 MODULE_ALIAS_RTNL_LINK("ip6gre");
1738 MODULE_ALIAS_NETDEV("ip6gre0");