1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux NET3: GRE over IP protocol decoder.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/capability.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/uaccess.h>
16 #include <linux/skbuff.h>
17 #include <linux/netdevice.h>
19 #include <linux/tcp.h>
20 #include <linux/udp.h>
21 #include <linux/if_arp.h>
22 #include <linux/if_vlan.h>
23 #include <linux/init.h>
24 #include <linux/in6.h>
25 #include <linux/inetdevice.h>
26 #include <linux/igmp.h>
27 #include <linux/netfilter_ipv4.h>
28 #include <linux/etherdevice.h>
29 #include <linux/if_ether.h>
34 #include <net/protocol.h>
35 #include <net/ip_tunnels.h>
37 #include <net/checksum.h>
38 #include <net/dsfield.h>
39 #include <net/inet_ecn.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <net/rtnetlink.h>
45 #include <net/dst_metadata.h>
46 #include <net/erspan.h>
52 1. The most important issue is detecting local dead loops.
53 They would cause complete host lockup in transmit, which
54 would be "resolved" by stack overflow or, if queueing is enabled,
55 with infinite looping in net_bh.
57 We cannot track such dead loops during route installation,
58 it is infeasible task. The most general solutions would be
59 to keep skb->encapsulation counter (sort of local ttl),
60 and silently drop packet when it expires. It is a good
61 solution, but it supposes maintaining new variable in ALL
62 skb, even if no tunneling is used.
64 Current solution: xmit_recursion breaks dead loops. This is a percpu
65 counter, since when we enter the first ndo_xmit(), cpu migration is
66 forbidden. We force an exit if this counter reaches RECURSION_LIMIT
68 2. Networking dead loops would not kill routers, but would really
69 kill network. IP hop limit plays role of "t->recursion" in this case,
70 if we copy it from packet being encapsulated to upper header.
71 It is very good solution, but it introduces two problems:
73 - Routing protocols, using packets with ttl=1 (OSPF, RIP2),
74 do not work over tunnels.
75 - traceroute does not work. I planned to relay ICMP from tunnel,
76 so that this problem would be solved and traceroute output
77 would even more informative. This idea appeared to be wrong:
78 only Linux complies to rfc1812 now (yes, guys, Linux is the only
79 true router now :-)), all routers (at least, in neighbourhood of mine)
80 return only 8 bytes of payload. It is the end.
82 Hence, if we want that OSPF worked or traceroute said something reasonable,
83 we should search for another solution.
85 One of them is to parse packet trying to detect inner encapsulation
86 made by our node. It is difficult or even impossible, especially,
87 taking into account fragmentation. TO be short, ttl is not solution at all.
89 Current solution: The solution was UNEXPECTEDLY SIMPLE.
90 We force DF flag on tunnels with preconfigured hop limit,
91 that is ALL. :-) Well, it does not remove the problem completely,
92 but exponential growth of network traffic is changed to linear
93 (branches, that exceed pmtu are pruned) and tunnel mtu
94 rapidly degrades to value <68, where looping stops.
95 Yes, it is not good if there exists a router in the loop,
96 which does not force DF, even when encapsulating packets have DF set.
97 But it is not our problem! Nobody could accuse us, we made
98 all that we could make. Even if it is your gated who injected
99 fatal route to network, even if it were you who configured
100 fatal static route: you are innocent. :-)
105 static bool log_ecn_error = true;
106 module_param(log_ecn_error, bool, 0644);
107 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
109 static struct rtnl_link_ops ipgre_link_ops __read_mostly;
110 static const struct header_ops ipgre_header_ops;
112 static int ipgre_tunnel_init(struct net_device *dev);
113 static void erspan_build_header(struct sk_buff *skb,
115 bool truncate, bool is_ipv4);
117 static unsigned int ipgre_net_id __read_mostly;
118 static unsigned int gre_tap_net_id __read_mostly;
119 static unsigned int erspan_net_id __read_mostly;
121 static int ipgre_err(struct sk_buff *skb, u32 info,
122 const struct tnl_ptk_info *tpi)
125 /* All the routers (except for Linux) return only
126 8 bytes of packet payload. It means, that precise relaying of
127 ICMP in the real Internet is absolutely infeasible.
129 Moreover, Cisco "wise men" put GRE key to the third word
130 in GRE header. It makes impossible maintaining even soft
131 state for keyed GRE tunnels with enabled checksum. Tell
134 Well, I wonder, rfc1812 was written by Cisco employee,
135 what the hell these idiots break standards established
138 struct net *net = dev_net(skb->dev);
139 struct ip_tunnel_net *itn;
140 const struct iphdr *iph;
141 const int type = icmp_hdr(skb)->type;
142 const int code = icmp_hdr(skb)->code;
143 unsigned int data_len = 0;
146 if (tpi->proto == htons(ETH_P_TEB))
147 itn = net_generic(net, gre_tap_net_id);
148 else if (tpi->proto == htons(ETH_P_ERSPAN) ||
149 tpi->proto == htons(ETH_P_ERSPAN2))
150 itn = net_generic(net, erspan_net_id);
152 itn = net_generic(net, ipgre_net_id);
154 iph = (const struct iphdr *)(icmp_hdr(skb) + 1);
155 t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
156 iph->daddr, iph->saddr, tpi->key);
163 case ICMP_PARAMETERPROB:
166 case ICMP_DEST_UNREACH:
169 case ICMP_PORT_UNREACH:
170 /* Impossible event. */
173 /* All others are translated to HOST_UNREACH.
174 rfc2003 contains "deep thoughts" about NET_UNREACH,
175 I believe they are just ether pollution. --ANK
181 case ICMP_TIME_EXCEEDED:
182 if (code != ICMP_EXC_TTL)
184 data_len = icmp_hdr(skb)->un.reserved[1] * 4; /* RFC 4884 4.1 */
191 #if IS_ENABLED(CONFIG_IPV6)
192 if (tpi->proto == htons(ETH_P_IPV6) &&
193 !ip6_err_gen_icmpv6_unreach(skb, iph->ihl * 4 + tpi->hdr_len,
198 if (t->parms.iph.daddr == 0 ||
199 ipv4_is_multicast(t->parms.iph.daddr))
202 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
205 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
209 t->err_time = jiffies;
214 static void gre_err(struct sk_buff *skb, u32 info)
216 /* All the routers (except for Linux) return only
217 * 8 bytes of packet payload. It means, that precise relaying of
218 * ICMP in the real Internet is absolutely infeasible.
220 * Moreover, Cisco "wise men" put GRE key to the third word
221 * in GRE header. It makes impossible maintaining even soft
223 * GRE tunnels with enabled checksum. Tell them "thank you".
225 * Well, I wonder, rfc1812 was written by Cisco employee,
226 * what the hell these idiots break standards established
230 const struct iphdr *iph = (struct iphdr *)skb->data;
231 const int type = icmp_hdr(skb)->type;
232 const int code = icmp_hdr(skb)->code;
233 struct tnl_ptk_info tpi;
235 if (gre_parse_header(skb, &tpi, NULL, htons(ETH_P_IP),
239 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
240 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
241 skb->dev->ifindex, IPPROTO_GRE);
244 if (type == ICMP_REDIRECT) {
245 ipv4_redirect(skb, dev_net(skb->dev), skb->dev->ifindex,
250 ipgre_err(skb, info, &tpi);
253 static bool is_erspan_type1(int gre_hdr_len)
255 /* Both ERSPAN type I (version 0) and type II (version 1) use
256 * protocol 0x88BE, but the type I has only 4-byte GRE header,
257 * while type II has 8-byte.
259 return gre_hdr_len == 4;
262 static int erspan_rcv(struct sk_buff *skb, struct tnl_ptk_info *tpi,
265 struct net *net = dev_net(skb->dev);
266 struct metadata_dst *tun_dst = NULL;
267 struct erspan_base_hdr *ershdr;
268 IP_TUNNEL_DECLARE_FLAGS(flags);
269 struct ip_tunnel_net *itn;
270 struct ip_tunnel *tunnel;
271 const struct iphdr *iph;
272 struct erspan_md2 *md2;
276 ip_tunnel_flags_copy(flags, tpi->flags);
278 itn = net_generic(net, erspan_net_id);
280 if (is_erspan_type1(gre_hdr_len)) {
282 __set_bit(IP_TUNNEL_NO_KEY_BIT, flags);
283 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,
284 iph->saddr, iph->daddr, 0);
286 if (unlikely(!pskb_may_pull(skb,
287 gre_hdr_len + sizeof(*ershdr))))
288 return PACKET_REJECT;
290 ershdr = (struct erspan_base_hdr *)(skb->data + gre_hdr_len);
293 __set_bit(IP_TUNNEL_KEY_BIT, flags);
294 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, flags,
295 iph->saddr, iph->daddr, tpi->key);
299 if (is_erspan_type1(gre_hdr_len))
302 len = gre_hdr_len + erspan_hdr_len(ver);
304 if (unlikely(!pskb_may_pull(skb, len)))
305 return PACKET_REJECT;
307 if (__iptunnel_pull_header(skb,
313 if (tunnel->collect_md) {
314 struct erspan_metadata *pkt_md, *md;
315 struct ip_tunnel_info *info;
319 __set_bit(IP_TUNNEL_KEY_BIT, tpi->flags);
320 ip_tunnel_flags_copy(flags, tpi->flags);
321 tun_id = key32_to_tunnel_id(tpi->key);
323 tun_dst = ip_tun_rx_dst(skb, flags,
324 tun_id, sizeof(*md));
326 return PACKET_REJECT;
328 /* skb can be uncloned in __iptunnel_pull_header, so
329 * old pkt_md is no longer valid and we need to reset
332 gh = skb_network_header(skb) +
333 skb_network_header_len(skb);
334 pkt_md = (struct erspan_metadata *)(gh + gre_hdr_len +
336 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
339 memcpy(md2, pkt_md, ver == 1 ? ERSPAN_V1_MDSIZE :
342 info = &tun_dst->u.tun_info;
343 __set_bit(IP_TUNNEL_ERSPAN_OPT_BIT,
344 info->key.tun_flags);
345 info->options_len = sizeof(*md);
348 skb_reset_mac_header(skb);
349 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
352 return PACKET_REJECT;
359 static int __ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
360 struct ip_tunnel_net *itn, int hdr_len, bool raw_proto)
362 struct metadata_dst *tun_dst = NULL;
363 const struct iphdr *iph;
364 struct ip_tunnel *tunnel;
367 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
368 iph->saddr, iph->daddr, tpi->key);
371 const struct iphdr *tnl_params;
373 if (__iptunnel_pull_header(skb, hdr_len, tpi->proto,
374 raw_proto, false) < 0)
377 /* Special case for ipgre_header_parse(), which expects the
378 * mac_header to point to the outer IP header.
380 if (tunnel->dev->header_ops == &ipgre_header_ops)
381 skb_pop_mac_header(skb);
383 skb_reset_mac_header(skb);
385 tnl_params = &tunnel->parms.iph;
386 if (tunnel->collect_md || tnl_params->daddr == 0) {
387 IP_TUNNEL_DECLARE_FLAGS(flags) = { };
390 __set_bit(IP_TUNNEL_CSUM_BIT, flags);
391 __set_bit(IP_TUNNEL_KEY_BIT, flags);
392 ip_tunnel_flags_and(flags, tpi->flags, flags);
394 tun_id = key32_to_tunnel_id(tpi->key);
395 tun_dst = ip_tun_rx_dst(skb, flags, tun_id, 0);
397 return PACKET_REJECT;
400 ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
410 static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
413 struct net *net = dev_net(skb->dev);
414 struct ip_tunnel_net *itn;
417 if (tpi->proto == htons(ETH_P_TEB))
418 itn = net_generic(net, gre_tap_net_id);
420 itn = net_generic(net, ipgre_net_id);
422 res = __ipgre_rcv(skb, tpi, itn, hdr_len, false);
423 if (res == PACKET_NEXT && tpi->proto == htons(ETH_P_TEB)) {
424 /* ipgre tunnels in collect metadata mode should receive
425 * also ETH_P_TEB traffic.
427 itn = net_generic(net, ipgre_net_id);
428 res = __ipgre_rcv(skb, tpi, itn, hdr_len, true);
433 static int gre_rcv(struct sk_buff *skb)
435 struct tnl_ptk_info tpi;
436 bool csum_err = false;
439 #ifdef CONFIG_NET_IPGRE_BROADCAST
440 if (ipv4_is_multicast(ip_hdr(skb)->daddr)) {
441 /* Looped back packet, drop it! */
442 if (rt_is_output_route(skb_rtable(skb)))
447 hdr_len = gre_parse_header(skb, &tpi, &csum_err, htons(ETH_P_IP), 0);
451 if (unlikely(tpi.proto == htons(ETH_P_ERSPAN) ||
452 tpi.proto == htons(ETH_P_ERSPAN2))) {
453 if (erspan_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
458 if (ipgre_rcv(skb, &tpi, hdr_len) == PACKET_RCVD)
462 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
468 static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
469 const struct iphdr *tnl_params,
472 struct ip_tunnel *tunnel = netdev_priv(dev);
473 IP_TUNNEL_DECLARE_FLAGS(flags);
475 ip_tunnel_flags_copy(flags, tunnel->parms.o_flags);
477 /* Push GRE header. */
478 gre_build_header(skb, tunnel->tun_hlen,
479 flags, proto, tunnel->parms.o_key,
480 test_bit(IP_TUNNEL_SEQ_BIT, flags) ?
481 htonl(atomic_fetch_inc(&tunnel->o_seqno)) : 0);
483 ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
486 static int gre_handle_offloads(struct sk_buff *skb, bool csum)
488 return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
491 static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev,
494 struct ip_tunnel *tunnel = netdev_priv(dev);
495 IP_TUNNEL_DECLARE_FLAGS(flags) = { };
496 struct ip_tunnel_info *tun_info;
497 const struct ip_tunnel_key *key;
500 tun_info = skb_tunnel_info(skb);
501 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
502 ip_tunnel_info_af(tun_info) != AF_INET))
505 key = &tun_info->key;
506 tunnel_hlen = gre_calc_hlen(key->tun_flags);
508 if (skb_cow_head(skb, dev->needed_headroom))
511 /* Push Tunnel header. */
512 if (gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
513 tunnel->parms.o_flags)))
516 __set_bit(IP_TUNNEL_CSUM_BIT, flags);
517 __set_bit(IP_TUNNEL_KEY_BIT, flags);
518 __set_bit(IP_TUNNEL_SEQ_BIT, flags);
519 ip_tunnel_flags_and(flags, tun_info->key.tun_flags, flags);
521 gre_build_header(skb, tunnel_hlen, flags, proto,
522 tunnel_id_to_key32(tun_info->key.tun_id),
523 test_bit(IP_TUNNEL_SEQ_BIT, flags) ?
524 htonl(atomic_fetch_inc(&tunnel->o_seqno)) : 0);
526 ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
532 DEV_STATS_INC(dev, tx_dropped);
535 static void erspan_fb_xmit(struct sk_buff *skb, struct net_device *dev)
537 struct ip_tunnel *tunnel = netdev_priv(dev);
538 IP_TUNNEL_DECLARE_FLAGS(flags) = { };
539 struct ip_tunnel_info *tun_info;
540 const struct ip_tunnel_key *key;
541 struct erspan_metadata *md;
542 bool truncate = false;
548 tun_info = skb_tunnel_info(skb);
549 if (unlikely(!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX) ||
550 ip_tunnel_info_af(tun_info) != AF_INET))
553 key = &tun_info->key;
554 if (!test_bit(IP_TUNNEL_ERSPAN_OPT_BIT, tun_info->key.tun_flags))
556 if (tun_info->options_len < sizeof(*md))
558 md = ip_tunnel_info_opts(tun_info);
560 /* ERSPAN has fixed 8 byte GRE header */
561 version = md->version;
562 tunnel_hlen = 8 + erspan_hdr_len(version);
564 if (skb_cow_head(skb, dev->needed_headroom))
567 if (gre_handle_offloads(skb, false))
570 if (skb->len > dev->mtu + dev->hard_header_len) {
571 if (pskb_trim(skb, dev->mtu + dev->hard_header_len))
576 nhoff = skb_network_offset(skb);
577 if (skb->protocol == htons(ETH_P_IP) &&
578 (ntohs(ip_hdr(skb)->tot_len) > skb->len - nhoff))
581 if (skb->protocol == htons(ETH_P_IPV6)) {
584 if (skb_transport_header_was_set(skb))
585 thoff = skb_transport_offset(skb);
587 thoff = nhoff + sizeof(struct ipv6hdr);
588 if (ntohs(ipv6_hdr(skb)->payload_len) > skb->len - thoff)
593 erspan_build_header(skb, ntohl(tunnel_id_to_key32(key->tun_id)),
594 ntohl(md->u.index), truncate, true);
595 proto = htons(ETH_P_ERSPAN);
596 } else if (version == 2) {
597 erspan_build_header_v2(skb,
598 ntohl(tunnel_id_to_key32(key->tun_id)),
600 get_hwid(&md->u.md2),
602 proto = htons(ETH_P_ERSPAN2);
607 __set_bit(IP_TUNNEL_SEQ_BIT, flags);
608 gre_build_header(skb, 8, flags, proto, 0,
609 htonl(atomic_fetch_inc(&tunnel->o_seqno)));
611 ip_md_tunnel_xmit(skb, dev, IPPROTO_GRE, tunnel_hlen);
617 DEV_STATS_INC(dev, tx_dropped);
620 static int gre_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
622 struct ip_tunnel_info *info = skb_tunnel_info(skb);
623 const struct ip_tunnel_key *key;
627 if (ip_tunnel_info_af(info) != AF_INET)
631 ip_tunnel_init_flow(&fl4, IPPROTO_GRE, key->u.ipv4.dst, key->u.ipv4.src,
632 tunnel_id_to_key32(key->tun_id),
633 key->tos & ~INET_ECN_MASK, dev_net(dev), 0,
634 skb->mark, skb_get_hash(skb), key->flow_flags);
635 rt = ip_route_output_key(dev_net(dev), &fl4);
640 info->key.u.ipv4.src = fl4.saddr;
644 static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
645 struct net_device *dev)
647 struct ip_tunnel *tunnel = netdev_priv(dev);
648 const struct iphdr *tnl_params;
650 if (!pskb_inet_may_pull(skb))
653 if (tunnel->collect_md) {
654 gre_fb_xmit(skb, dev, skb->protocol);
658 if (dev->header_ops) {
659 int pull_len = tunnel->hlen + sizeof(struct iphdr);
661 if (skb_cow_head(skb, 0))
664 tnl_params = (const struct iphdr *)skb->data;
666 if (!pskb_network_may_pull(skb, pull_len))
669 /* ip_tunnel_xmit() needs skb->data pointing to gre header. */
670 skb_pull(skb, pull_len);
671 skb_reset_mac_header(skb);
673 if (skb->ip_summed == CHECKSUM_PARTIAL &&
674 skb_checksum_start(skb) < skb->data)
677 if (skb_cow_head(skb, dev->needed_headroom))
680 tnl_params = &tunnel->parms.iph;
683 if (gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
684 tunnel->parms.o_flags)))
687 __gre_xmit(skb, dev, tnl_params, skb->protocol);
692 DEV_STATS_INC(dev, tx_dropped);
696 static netdev_tx_t erspan_xmit(struct sk_buff *skb,
697 struct net_device *dev)
699 struct ip_tunnel *tunnel = netdev_priv(dev);
700 bool truncate = false;
703 if (!pskb_inet_may_pull(skb))
706 if (tunnel->collect_md) {
707 erspan_fb_xmit(skb, dev);
711 if (gre_handle_offloads(skb, false))
714 if (skb_cow_head(skb, dev->needed_headroom))
717 if (skb->len > dev->mtu + dev->hard_header_len) {
718 if (pskb_trim(skb, dev->mtu + dev->hard_header_len))
723 /* Push ERSPAN header */
724 if (tunnel->erspan_ver == 0) {
725 proto = htons(ETH_P_ERSPAN);
726 __clear_bit(IP_TUNNEL_SEQ_BIT, tunnel->parms.o_flags);
727 } else if (tunnel->erspan_ver == 1) {
728 erspan_build_header(skb, ntohl(tunnel->parms.o_key),
731 proto = htons(ETH_P_ERSPAN);
732 } else if (tunnel->erspan_ver == 2) {
733 erspan_build_header_v2(skb, ntohl(tunnel->parms.o_key),
734 tunnel->dir, tunnel->hwid,
736 proto = htons(ETH_P_ERSPAN2);
741 __clear_bit(IP_TUNNEL_KEY_BIT, tunnel->parms.o_flags);
742 __gre_xmit(skb, dev, &tunnel->parms.iph, proto);
747 DEV_STATS_INC(dev, tx_dropped);
751 static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
752 struct net_device *dev)
754 struct ip_tunnel *tunnel = netdev_priv(dev);
756 if (!pskb_inet_may_pull(skb))
759 if (tunnel->collect_md) {
760 gre_fb_xmit(skb, dev, htons(ETH_P_TEB));
764 if (gre_handle_offloads(skb, test_bit(IP_TUNNEL_CSUM_BIT,
765 tunnel->parms.o_flags)))
768 if (skb_cow_head(skb, dev->needed_headroom))
771 __gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
776 DEV_STATS_INC(dev, tx_dropped);
780 static void ipgre_link_update(struct net_device *dev, bool set_mtu)
782 struct ip_tunnel *tunnel = netdev_priv(dev);
785 len = tunnel->tun_hlen;
786 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
787 len = tunnel->tun_hlen - len;
788 tunnel->hlen = tunnel->hlen + len;
791 dev->hard_header_len += len;
793 dev->needed_headroom += len;
796 WRITE_ONCE(dev->mtu, max_t(int, dev->mtu - len, 68));
798 if (test_bit(IP_TUNNEL_SEQ_BIT, tunnel->parms.o_flags) ||
799 (test_bit(IP_TUNNEL_CSUM_BIT, tunnel->parms.o_flags) &&
800 tunnel->encap.type != TUNNEL_ENCAP_NONE)) {
801 dev->features &= ~NETIF_F_GSO_SOFTWARE;
802 dev->hw_features &= ~NETIF_F_GSO_SOFTWARE;
804 dev->features |= NETIF_F_GSO_SOFTWARE;
805 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
809 static int ipgre_tunnel_ctl(struct net_device *dev,
810 struct ip_tunnel_parm_kern *p,
813 __be16 i_flags, o_flags;
816 if (!ip_tunnel_flags_is_be16_compat(p->i_flags) ||
817 !ip_tunnel_flags_is_be16_compat(p->o_flags))
820 i_flags = ip_tunnel_flags_to_be16(p->i_flags);
821 o_flags = ip_tunnel_flags_to_be16(p->o_flags);
823 if (cmd == SIOCADDTUNNEL || cmd == SIOCCHGTUNNEL) {
824 if (p->iph.version != 4 || p->iph.protocol != IPPROTO_GRE ||
825 p->iph.ihl != 5 || (p->iph.frag_off & htons(~IP_DF)) ||
826 ((i_flags | o_flags) & (GRE_VERSION | GRE_ROUTING)))
830 gre_flags_to_tnl_flags(p->i_flags, i_flags);
831 gre_flags_to_tnl_flags(p->o_flags, o_flags);
833 err = ip_tunnel_ctl(dev, p, cmd);
837 if (cmd == SIOCCHGTUNNEL) {
838 struct ip_tunnel *t = netdev_priv(dev);
840 ip_tunnel_flags_copy(t->parms.i_flags, p->i_flags);
841 ip_tunnel_flags_copy(t->parms.o_flags, p->o_flags);
843 if (strcmp(dev->rtnl_link_ops->kind, "erspan"))
844 ipgre_link_update(dev, true);
847 i_flags = gre_tnl_flags_to_gre_flags(p->i_flags);
848 ip_tunnel_flags_from_be16(p->i_flags, i_flags);
849 o_flags = gre_tnl_flags_to_gre_flags(p->o_flags);
850 ip_tunnel_flags_from_be16(p->o_flags, o_flags);
855 /* Nice toy. Unfortunately, useless in real life :-)
856 It allows to construct virtual multiprotocol broadcast "LAN"
857 over the Internet, provided multicast routing is tuned.
860 I have no idea was this bicycle invented before me,
861 so that I had to set ARPHRD_IPGRE to a random value.
862 I have an impression, that Cisco could make something similar,
863 but this feature is apparently missing in IOS<=11.2(8).
865 I set up 10.66.66/24 and fec0:6666:6666::0/96 as virtual networks
866 with broadcast 224.66.66.66. If you have access to mbone, play with me :-)
868 ping -t 255 224.66.66.66
870 If nobody answers, mbone does not work.
872 ip tunnel add Universe mode gre remote 224.66.66.66 local <Your_real_addr> ttl 255
873 ip addr add 10.66.66.<somewhat>/24 dev Universe
875 ifconfig Universe add fe80::<Your_real_addr>/10
876 ifconfig Universe add fec0:6666:6666::<Your_real_addr>/96
879 ftp fec0:6666:6666::193.233.7.65
882 static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
884 const void *daddr, const void *saddr, unsigned int len)
886 struct ip_tunnel *t = netdev_priv(dev);
888 struct gre_base_hdr *greh;
890 iph = skb_push(skb, t->hlen + sizeof(*iph));
891 greh = (struct gre_base_hdr *)(iph+1);
892 greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
893 greh->protocol = htons(type);
895 memcpy(iph, &t->parms.iph, sizeof(struct iphdr));
897 /* Set the source hardware address. */
899 memcpy(&iph->saddr, saddr, 4);
901 memcpy(&iph->daddr, daddr, 4);
903 return t->hlen + sizeof(*iph);
905 return -(t->hlen + sizeof(*iph));
908 static int ipgre_header_parse(const struct sk_buff *skb, unsigned char *haddr)
910 const struct iphdr *iph = (const struct iphdr *) skb_mac_header(skb);
911 memcpy(haddr, &iph->saddr, 4);
915 static const struct header_ops ipgre_header_ops = {
916 .create = ipgre_header,
917 .parse = ipgre_header_parse,
920 #ifdef CONFIG_NET_IPGRE_BROADCAST
921 static int ipgre_open(struct net_device *dev)
923 struct ip_tunnel *t = netdev_priv(dev);
925 if (ipv4_is_multicast(t->parms.iph.daddr)) {
929 rt = ip_route_output_gre(t->net, &fl4,
933 RT_TOS(t->parms.iph.tos),
936 return -EADDRNOTAVAIL;
939 if (!__in_dev_get_rtnl(dev))
940 return -EADDRNOTAVAIL;
941 t->mlink = dev->ifindex;
942 ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
947 static int ipgre_close(struct net_device *dev)
949 struct ip_tunnel *t = netdev_priv(dev);
951 if (ipv4_is_multicast(t->parms.iph.daddr) && t->mlink) {
952 struct in_device *in_dev;
953 in_dev = inetdev_by_index(t->net, t->mlink);
955 ip_mc_dec_group(in_dev, t->parms.iph.daddr);
961 static const struct net_device_ops ipgre_netdev_ops = {
962 .ndo_init = ipgre_tunnel_init,
963 .ndo_uninit = ip_tunnel_uninit,
964 #ifdef CONFIG_NET_IPGRE_BROADCAST
965 .ndo_open = ipgre_open,
966 .ndo_stop = ipgre_close,
968 .ndo_start_xmit = ipgre_xmit,
969 .ndo_siocdevprivate = ip_tunnel_siocdevprivate,
970 .ndo_change_mtu = ip_tunnel_change_mtu,
971 .ndo_get_stats64 = dev_get_tstats64,
972 .ndo_get_iflink = ip_tunnel_get_iflink,
973 .ndo_tunnel_ctl = ipgre_tunnel_ctl,
976 #define GRE_FEATURES (NETIF_F_SG | \
981 static void ipgre_tunnel_setup(struct net_device *dev)
983 dev->netdev_ops = &ipgre_netdev_ops;
984 dev->type = ARPHRD_IPGRE;
985 ip_tunnel_setup(dev, ipgre_net_id);
988 static void __gre_tunnel_init(struct net_device *dev)
990 struct ip_tunnel *tunnel;
992 tunnel = netdev_priv(dev);
993 tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
994 tunnel->parms.iph.protocol = IPPROTO_GRE;
996 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
997 dev->needed_headroom = tunnel->hlen + sizeof(tunnel->parms.iph);
999 dev->features |= GRE_FEATURES | NETIF_F_LLTX;
1000 dev->hw_features |= GRE_FEATURES;
1002 /* TCP offload with GRE SEQ is not supported, nor can we support 2
1003 * levels of outer headers requiring an update.
1005 if (test_bit(IP_TUNNEL_SEQ_BIT, tunnel->parms.o_flags))
1007 if (test_bit(IP_TUNNEL_CSUM_BIT, tunnel->parms.o_flags) &&
1008 tunnel->encap.type != TUNNEL_ENCAP_NONE)
1011 dev->features |= NETIF_F_GSO_SOFTWARE;
1012 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1015 static int ipgre_tunnel_init(struct net_device *dev)
1017 struct ip_tunnel *tunnel = netdev_priv(dev);
1018 struct iphdr *iph = &tunnel->parms.iph;
1020 __gre_tunnel_init(dev);
1022 __dev_addr_set(dev, &iph->saddr, 4);
1023 memcpy(dev->broadcast, &iph->daddr, 4);
1025 dev->flags = IFF_NOARP;
1026 netif_keep_dst(dev);
1029 if (iph->daddr && !tunnel->collect_md) {
1030 #ifdef CONFIG_NET_IPGRE_BROADCAST
1031 if (ipv4_is_multicast(iph->daddr)) {
1034 dev->flags = IFF_BROADCAST;
1035 dev->header_ops = &ipgre_header_ops;
1036 dev->hard_header_len = tunnel->hlen + sizeof(*iph);
1037 dev->needed_headroom = 0;
1040 } else if (!tunnel->collect_md) {
1041 dev->header_ops = &ipgre_header_ops;
1042 dev->hard_header_len = tunnel->hlen + sizeof(*iph);
1043 dev->needed_headroom = 0;
1046 return ip_tunnel_init(dev);
1049 static const struct gre_protocol ipgre_protocol = {
1051 .err_handler = gre_err,
1054 static int __net_init ipgre_init_net(struct net *net)
1056 return ip_tunnel_init_net(net, ipgre_net_id, &ipgre_link_ops, NULL);
1059 static void __net_exit ipgre_exit_batch_rtnl(struct list_head *list_net,
1060 struct list_head *dev_to_kill)
1062 ip_tunnel_delete_nets(list_net, ipgre_net_id, &ipgre_link_ops,
1066 static struct pernet_operations ipgre_net_ops = {
1067 .init = ipgre_init_net,
1068 .exit_batch_rtnl = ipgre_exit_batch_rtnl,
1069 .id = &ipgre_net_id,
1070 .size = sizeof(struct ip_tunnel_net),
1073 static int ipgre_tunnel_validate(struct nlattr *tb[], struct nlattr *data[],
1074 struct netlink_ext_ack *extack)
1082 if (data[IFLA_GRE_IFLAGS])
1083 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1084 if (data[IFLA_GRE_OFLAGS])
1085 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1086 if (flags & (GRE_VERSION|GRE_ROUTING))
1089 if (data[IFLA_GRE_COLLECT_METADATA] &&
1090 data[IFLA_GRE_ENCAP_TYPE] &&
1091 nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]) != TUNNEL_ENCAP_NONE)
1097 static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[],
1098 struct netlink_ext_ack *extack)
1102 if (tb[IFLA_ADDRESS]) {
1103 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1105 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1106 return -EADDRNOTAVAIL;
1112 if (data[IFLA_GRE_REMOTE]) {
1113 memcpy(&daddr, nla_data(data[IFLA_GRE_REMOTE]), 4);
1119 return ipgre_tunnel_validate(tb, data, extack);
1122 static int erspan_validate(struct nlattr *tb[], struct nlattr *data[],
1123 struct netlink_ext_ack *extack)
1131 ret = ipgre_tap_validate(tb, data, extack);
1135 if (data[IFLA_GRE_ERSPAN_VER] &&
1136 nla_get_u8(data[IFLA_GRE_ERSPAN_VER]) == 0)
1139 /* ERSPAN type II/III should only have GRE sequence and key flag */
1140 if (data[IFLA_GRE_OFLAGS])
1141 flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
1142 if (data[IFLA_GRE_IFLAGS])
1143 flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
1144 if (!data[IFLA_GRE_COLLECT_METADATA] &&
1145 flags != (GRE_SEQ | GRE_KEY))
1148 /* ERSPAN Session ID only has 10-bit. Since we reuse
1149 * 32-bit key field as ID, check it's range.
1151 if (data[IFLA_GRE_IKEY] &&
1152 (ntohl(nla_get_be32(data[IFLA_GRE_IKEY])) & ~ID_MASK))
1155 if (data[IFLA_GRE_OKEY] &&
1156 (ntohl(nla_get_be32(data[IFLA_GRE_OKEY])) & ~ID_MASK))
1162 static int ipgre_netlink_parms(struct net_device *dev,
1163 struct nlattr *data[],
1164 struct nlattr *tb[],
1165 struct ip_tunnel_parm_kern *parms,
1168 struct ip_tunnel *t = netdev_priv(dev);
1170 memset(parms, 0, sizeof(*parms));
1172 parms->iph.protocol = IPPROTO_GRE;
1177 if (data[IFLA_GRE_LINK])
1178 parms->link = nla_get_u32(data[IFLA_GRE_LINK]);
1180 if (data[IFLA_GRE_IFLAGS])
1181 gre_flags_to_tnl_flags(parms->i_flags,
1182 nla_get_be16(data[IFLA_GRE_IFLAGS]));
1184 if (data[IFLA_GRE_OFLAGS])
1185 gre_flags_to_tnl_flags(parms->o_flags,
1186 nla_get_be16(data[IFLA_GRE_OFLAGS]));
1188 if (data[IFLA_GRE_IKEY])
1189 parms->i_key = nla_get_be32(data[IFLA_GRE_IKEY]);
1191 if (data[IFLA_GRE_OKEY])
1192 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1194 if (data[IFLA_GRE_LOCAL])
1195 parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
1197 if (data[IFLA_GRE_REMOTE])
1198 parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
1200 if (data[IFLA_GRE_TTL])
1201 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
1203 if (data[IFLA_GRE_TOS])
1204 parms->iph.tos = nla_get_u8(data[IFLA_GRE_TOS]);
1206 if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC])) {
1209 parms->iph.frag_off = htons(IP_DF);
1212 if (data[IFLA_GRE_COLLECT_METADATA]) {
1213 t->collect_md = true;
1214 if (dev->type == ARPHRD_IPGRE)
1215 dev->type = ARPHRD_NONE;
1218 if (data[IFLA_GRE_IGNORE_DF]) {
1219 if (nla_get_u8(data[IFLA_GRE_IGNORE_DF])
1220 && (parms->iph.frag_off & htons(IP_DF)))
1222 t->ignore_df = !!nla_get_u8(data[IFLA_GRE_IGNORE_DF]);
1225 if (data[IFLA_GRE_FWMARK])
1226 *fwmark = nla_get_u32(data[IFLA_GRE_FWMARK]);
1231 static int erspan_netlink_parms(struct net_device *dev,
1232 struct nlattr *data[],
1233 struct nlattr *tb[],
1234 struct ip_tunnel_parm_kern *parms,
1237 struct ip_tunnel *t = netdev_priv(dev);
1240 err = ipgre_netlink_parms(dev, data, tb, parms, fwmark);
1246 if (data[IFLA_GRE_ERSPAN_VER]) {
1247 t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);
1249 if (t->erspan_ver > 2)
1253 if (t->erspan_ver == 1) {
1254 if (data[IFLA_GRE_ERSPAN_INDEX]) {
1255 t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
1256 if (t->index & ~INDEX_MASK)
1259 } else if (t->erspan_ver == 2) {
1260 if (data[IFLA_GRE_ERSPAN_DIR]) {
1261 t->dir = nla_get_u8(data[IFLA_GRE_ERSPAN_DIR]);
1262 if (t->dir & ~(DIR_MASK >> DIR_OFFSET))
1265 if (data[IFLA_GRE_ERSPAN_HWID]) {
1266 t->hwid = nla_get_u16(data[IFLA_GRE_ERSPAN_HWID]);
1267 if (t->hwid & ~(HWID_MASK >> HWID_OFFSET))
1275 /* This function returns true when ENCAP attributes are present in the nl msg */
1276 static bool ipgre_netlink_encap_parms(struct nlattr *data[],
1277 struct ip_tunnel_encap *ipencap)
1281 memset(ipencap, 0, sizeof(*ipencap));
1286 if (data[IFLA_GRE_ENCAP_TYPE]) {
1288 ipencap->type = nla_get_u16(data[IFLA_GRE_ENCAP_TYPE]);
1291 if (data[IFLA_GRE_ENCAP_FLAGS]) {
1293 ipencap->flags = nla_get_u16(data[IFLA_GRE_ENCAP_FLAGS]);
1296 if (data[IFLA_GRE_ENCAP_SPORT]) {
1298 ipencap->sport = nla_get_be16(data[IFLA_GRE_ENCAP_SPORT]);
1301 if (data[IFLA_GRE_ENCAP_DPORT]) {
1303 ipencap->dport = nla_get_be16(data[IFLA_GRE_ENCAP_DPORT]);
1309 static int gre_tap_init(struct net_device *dev)
1311 __gre_tunnel_init(dev);
1312 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1313 netif_keep_dst(dev);
1315 return ip_tunnel_init(dev);
1318 static const struct net_device_ops gre_tap_netdev_ops = {
1319 .ndo_init = gre_tap_init,
1320 .ndo_uninit = ip_tunnel_uninit,
1321 .ndo_start_xmit = gre_tap_xmit,
1322 .ndo_set_mac_address = eth_mac_addr,
1323 .ndo_validate_addr = eth_validate_addr,
1324 .ndo_change_mtu = ip_tunnel_change_mtu,
1325 .ndo_get_stats64 = dev_get_tstats64,
1326 .ndo_get_iflink = ip_tunnel_get_iflink,
1327 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
1330 static int erspan_tunnel_init(struct net_device *dev)
1332 struct ip_tunnel *tunnel = netdev_priv(dev);
1334 if (tunnel->erspan_ver == 0)
1335 tunnel->tun_hlen = 4; /* 4-byte GRE hdr. */
1337 tunnel->tun_hlen = 8; /* 8-byte GRE hdr. */
1339 tunnel->parms.iph.protocol = IPPROTO_GRE;
1340 tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
1341 erspan_hdr_len(tunnel->erspan_ver);
1343 dev->features |= GRE_FEATURES;
1344 dev->hw_features |= GRE_FEATURES;
1345 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1346 netif_keep_dst(dev);
1348 return ip_tunnel_init(dev);
1351 static const struct net_device_ops erspan_netdev_ops = {
1352 .ndo_init = erspan_tunnel_init,
1353 .ndo_uninit = ip_tunnel_uninit,
1354 .ndo_start_xmit = erspan_xmit,
1355 .ndo_set_mac_address = eth_mac_addr,
1356 .ndo_validate_addr = eth_validate_addr,
1357 .ndo_change_mtu = ip_tunnel_change_mtu,
1358 .ndo_get_stats64 = dev_get_tstats64,
1359 .ndo_get_iflink = ip_tunnel_get_iflink,
1360 .ndo_fill_metadata_dst = gre_fill_metadata_dst,
1363 static void ipgre_tap_setup(struct net_device *dev)
1367 dev->netdev_ops = &gre_tap_netdev_ops;
1368 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1369 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1370 ip_tunnel_setup(dev, gre_tap_net_id);
1374 ipgre_newlink_encap_setup(struct net_device *dev, struct nlattr *data[])
1376 struct ip_tunnel_encap ipencap;
1378 if (ipgre_netlink_encap_parms(data, &ipencap)) {
1379 struct ip_tunnel *t = netdev_priv(dev);
1380 int err = ip_tunnel_encap_setup(t, &ipencap);
1389 static int ipgre_newlink(struct net *src_net, struct net_device *dev,
1390 struct nlattr *tb[], struct nlattr *data[],
1391 struct netlink_ext_ack *extack)
1393 struct ip_tunnel_parm_kern p;
1397 err = ipgre_newlink_encap_setup(dev, data);
1401 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
1404 return ip_tunnel_newlink(dev, tb, &p, fwmark);
1407 static int erspan_newlink(struct net *src_net, struct net_device *dev,
1408 struct nlattr *tb[], struct nlattr *data[],
1409 struct netlink_ext_ack *extack)
1411 struct ip_tunnel_parm_kern p;
1415 err = ipgre_newlink_encap_setup(dev, data);
1419 err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1422 return ip_tunnel_newlink(dev, tb, &p, fwmark);
1425 static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
1426 struct nlattr *data[],
1427 struct netlink_ext_ack *extack)
1429 struct ip_tunnel *t = netdev_priv(dev);
1430 struct ip_tunnel_parm_kern p;
1431 __u32 fwmark = t->fwmark;
1434 err = ipgre_newlink_encap_setup(dev, data);
1438 err = ipgre_netlink_parms(dev, data, tb, &p, &fwmark);
1442 err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1446 ip_tunnel_flags_copy(t->parms.i_flags, p.i_flags);
1447 ip_tunnel_flags_copy(t->parms.o_flags, p.o_flags);
1449 ipgre_link_update(dev, !tb[IFLA_MTU]);
1454 static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
1455 struct nlattr *data[],
1456 struct netlink_ext_ack *extack)
1458 struct ip_tunnel *t = netdev_priv(dev);
1459 struct ip_tunnel_parm_kern p;
1460 __u32 fwmark = t->fwmark;
1463 err = ipgre_newlink_encap_setup(dev, data);
1467 err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
1471 err = ip_tunnel_changelink(dev, tb, &p, fwmark);
1475 ip_tunnel_flags_copy(t->parms.i_flags, p.i_flags);
1476 ip_tunnel_flags_copy(t->parms.o_flags, p.o_flags);
1481 static size_t ipgre_get_size(const struct net_device *dev)
1486 /* IFLA_GRE_IFLAGS */
1488 /* IFLA_GRE_OFLAGS */
1494 /* IFLA_GRE_LOCAL */
1496 /* IFLA_GRE_REMOTE */
1502 /* IFLA_GRE_PMTUDISC */
1504 /* IFLA_GRE_ENCAP_TYPE */
1506 /* IFLA_GRE_ENCAP_FLAGS */
1508 /* IFLA_GRE_ENCAP_SPORT */
1510 /* IFLA_GRE_ENCAP_DPORT */
1512 /* IFLA_GRE_COLLECT_METADATA */
1514 /* IFLA_GRE_IGNORE_DF */
1516 /* IFLA_GRE_FWMARK */
1518 /* IFLA_GRE_ERSPAN_INDEX */
1520 /* IFLA_GRE_ERSPAN_VER */
1522 /* IFLA_GRE_ERSPAN_DIR */
1524 /* IFLA_GRE_ERSPAN_HWID */
1529 static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1531 struct ip_tunnel *t = netdev_priv(dev);
1532 struct ip_tunnel_parm_kern *p = &t->parms;
1533 IP_TUNNEL_DECLARE_FLAGS(o_flags);
1535 ip_tunnel_flags_copy(o_flags, p->o_flags);
1537 if (nla_put_u32(skb, IFLA_GRE_LINK, p->link) ||
1538 nla_put_be16(skb, IFLA_GRE_IFLAGS,
1539 gre_tnl_flags_to_gre_flags(p->i_flags)) ||
1540 nla_put_be16(skb, IFLA_GRE_OFLAGS,
1541 gre_tnl_flags_to_gre_flags(o_flags)) ||
1542 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1543 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1544 nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
1545 nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
1546 nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
1547 nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
1548 nla_put_u8(skb, IFLA_GRE_PMTUDISC,
1549 !!(p->iph.frag_off & htons(IP_DF))) ||
1550 nla_put_u32(skb, IFLA_GRE_FWMARK, t->fwmark))
1551 goto nla_put_failure;
1553 if (nla_put_u16(skb, IFLA_GRE_ENCAP_TYPE,
1555 nla_put_be16(skb, IFLA_GRE_ENCAP_SPORT,
1557 nla_put_be16(skb, IFLA_GRE_ENCAP_DPORT,
1559 nla_put_u16(skb, IFLA_GRE_ENCAP_FLAGS,
1561 goto nla_put_failure;
1563 if (nla_put_u8(skb, IFLA_GRE_IGNORE_DF, t->ignore_df))
1564 goto nla_put_failure;
1566 if (t->collect_md) {
1567 if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
1568 goto nla_put_failure;
1577 static int erspan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1579 struct ip_tunnel *t = netdev_priv(dev);
1581 if (t->erspan_ver <= 2) {
1582 if (t->erspan_ver != 0 && !t->collect_md)
1583 __set_bit(IP_TUNNEL_KEY_BIT, t->parms.o_flags);
1585 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_VER, t->erspan_ver))
1586 goto nla_put_failure;
1588 if (t->erspan_ver == 1) {
1589 if (nla_put_u32(skb, IFLA_GRE_ERSPAN_INDEX, t->index))
1590 goto nla_put_failure;
1591 } else if (t->erspan_ver == 2) {
1592 if (nla_put_u8(skb, IFLA_GRE_ERSPAN_DIR, t->dir))
1593 goto nla_put_failure;
1594 if (nla_put_u16(skb, IFLA_GRE_ERSPAN_HWID, t->hwid))
1595 goto nla_put_failure;
1599 return ipgre_fill_info(skb, dev);
1605 static void erspan_setup(struct net_device *dev)
1607 struct ip_tunnel *t = netdev_priv(dev);
1611 dev->netdev_ops = &erspan_netdev_ops;
1612 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1613 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1614 ip_tunnel_setup(dev, erspan_net_id);
1618 static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
1619 [IFLA_GRE_LINK] = { .type = NLA_U32 },
1620 [IFLA_GRE_IFLAGS] = { .type = NLA_U16 },
1621 [IFLA_GRE_OFLAGS] = { .type = NLA_U16 },
1622 [IFLA_GRE_IKEY] = { .type = NLA_U32 },
1623 [IFLA_GRE_OKEY] = { .type = NLA_U32 },
1624 [IFLA_GRE_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
1625 [IFLA_GRE_REMOTE] = { .len = sizeof_field(struct iphdr, daddr) },
1626 [IFLA_GRE_TTL] = { .type = NLA_U8 },
1627 [IFLA_GRE_TOS] = { .type = NLA_U8 },
1628 [IFLA_GRE_PMTUDISC] = { .type = NLA_U8 },
1629 [IFLA_GRE_ENCAP_TYPE] = { .type = NLA_U16 },
1630 [IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
1631 [IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
1632 [IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
1633 [IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
1634 [IFLA_GRE_IGNORE_DF] = { .type = NLA_U8 },
1635 [IFLA_GRE_FWMARK] = { .type = NLA_U32 },
1636 [IFLA_GRE_ERSPAN_INDEX] = { .type = NLA_U32 },
1637 [IFLA_GRE_ERSPAN_VER] = { .type = NLA_U8 },
1638 [IFLA_GRE_ERSPAN_DIR] = { .type = NLA_U8 },
1639 [IFLA_GRE_ERSPAN_HWID] = { .type = NLA_U16 },
1642 static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
1644 .maxtype = IFLA_GRE_MAX,
1645 .policy = ipgre_policy,
1646 .priv_size = sizeof(struct ip_tunnel),
1647 .setup = ipgre_tunnel_setup,
1648 .validate = ipgre_tunnel_validate,
1649 .newlink = ipgre_newlink,
1650 .changelink = ipgre_changelink,
1651 .dellink = ip_tunnel_dellink,
1652 .get_size = ipgre_get_size,
1653 .fill_info = ipgre_fill_info,
1654 .get_link_net = ip_tunnel_get_link_net,
1657 static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
1659 .maxtype = IFLA_GRE_MAX,
1660 .policy = ipgre_policy,
1661 .priv_size = sizeof(struct ip_tunnel),
1662 .setup = ipgre_tap_setup,
1663 .validate = ipgre_tap_validate,
1664 .newlink = ipgre_newlink,
1665 .changelink = ipgre_changelink,
1666 .dellink = ip_tunnel_dellink,
1667 .get_size = ipgre_get_size,
1668 .fill_info = ipgre_fill_info,
1669 .get_link_net = ip_tunnel_get_link_net,
1672 static struct rtnl_link_ops erspan_link_ops __read_mostly = {
1674 .maxtype = IFLA_GRE_MAX,
1675 .policy = ipgre_policy,
1676 .priv_size = sizeof(struct ip_tunnel),
1677 .setup = erspan_setup,
1678 .validate = erspan_validate,
1679 .newlink = erspan_newlink,
1680 .changelink = erspan_changelink,
1681 .dellink = ip_tunnel_dellink,
1682 .get_size = ipgre_get_size,
1683 .fill_info = erspan_fill_info,
1684 .get_link_net = ip_tunnel_get_link_net,
1687 struct net_device *gretap_fb_dev_create(struct net *net, const char *name,
1688 u8 name_assign_type)
1690 struct nlattr *tb[IFLA_MAX + 1];
1691 struct net_device *dev;
1692 LIST_HEAD(list_kill);
1693 struct ip_tunnel *t;
1696 memset(&tb, 0, sizeof(tb));
1698 dev = rtnl_create_link(net, name, name_assign_type,
1699 &ipgre_tap_ops, tb, NULL);
1703 /* Configure flow based GRE device. */
1704 t = netdev_priv(dev);
1705 t->collect_md = true;
1707 err = ipgre_newlink(net, dev, tb, NULL, NULL);
1710 return ERR_PTR(err);
1713 /* openvswitch users expect packet sizes to be unrestricted,
1714 * so set the largest MTU we can.
1716 err = __ip_tunnel_change_mtu(dev, IP_MAX_MTU, false);
1720 err = rtnl_configure_link(dev, NULL, 0, NULL);
1726 ip_tunnel_dellink(dev, &list_kill);
1727 unregister_netdevice_many(&list_kill);
1728 return ERR_PTR(err);
1730 EXPORT_SYMBOL_GPL(gretap_fb_dev_create);
1732 static int __net_init ipgre_tap_init_net(struct net *net)
1734 return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
1737 static void __net_exit ipgre_tap_exit_batch_rtnl(struct list_head *list_net,
1738 struct list_head *dev_to_kill)
1740 ip_tunnel_delete_nets(list_net, gre_tap_net_id, &ipgre_tap_ops,
1744 static struct pernet_operations ipgre_tap_net_ops = {
1745 .init = ipgre_tap_init_net,
1746 .exit_batch_rtnl = ipgre_tap_exit_batch_rtnl,
1747 .id = &gre_tap_net_id,
1748 .size = sizeof(struct ip_tunnel_net),
1751 static int __net_init erspan_init_net(struct net *net)
1753 return ip_tunnel_init_net(net, erspan_net_id,
1754 &erspan_link_ops, "erspan0");
1757 static void __net_exit erspan_exit_batch_rtnl(struct list_head *net_list,
1758 struct list_head *dev_to_kill)
1760 ip_tunnel_delete_nets(net_list, erspan_net_id, &erspan_link_ops,
1764 static struct pernet_operations erspan_net_ops = {
1765 .init = erspan_init_net,
1766 .exit_batch_rtnl = erspan_exit_batch_rtnl,
1767 .id = &erspan_net_id,
1768 .size = sizeof(struct ip_tunnel_net),
1771 static int __init ipgre_init(void)
1775 pr_info("GRE over IPv4 tunneling driver\n");
1777 err = register_pernet_device(&ipgre_net_ops);
1781 err = register_pernet_device(&ipgre_tap_net_ops);
1783 goto pnet_tap_failed;
1785 err = register_pernet_device(&erspan_net_ops);
1787 goto pnet_erspan_failed;
1789 err = gre_add_protocol(&ipgre_protocol, GREPROTO_CISCO);
1791 pr_info("%s: can't add protocol\n", __func__);
1792 goto add_proto_failed;
1795 err = rtnl_link_register(&ipgre_link_ops);
1797 goto rtnl_link_failed;
1799 err = rtnl_link_register(&ipgre_tap_ops);
1801 goto tap_ops_failed;
1803 err = rtnl_link_register(&erspan_link_ops);
1805 goto erspan_link_failed;
1810 rtnl_link_unregister(&ipgre_tap_ops);
1812 rtnl_link_unregister(&ipgre_link_ops);
1814 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
1816 unregister_pernet_device(&erspan_net_ops);
1818 unregister_pernet_device(&ipgre_tap_net_ops);
1820 unregister_pernet_device(&ipgre_net_ops);
1824 static void __exit ipgre_fini(void)
1826 rtnl_link_unregister(&ipgre_tap_ops);
1827 rtnl_link_unregister(&ipgre_link_ops);
1828 rtnl_link_unregister(&erspan_link_ops);
1829 gre_del_protocol(&ipgre_protocol, GREPROTO_CISCO);
1830 unregister_pernet_device(&ipgre_tap_net_ops);
1831 unregister_pernet_device(&ipgre_net_ops);
1832 unregister_pernet_device(&erspan_net_ops);
1835 module_init(ipgre_init);
1836 module_exit(ipgre_fini);
1837 MODULE_DESCRIPTION("IPv4 GRE tunnels over IP library");
1838 MODULE_LICENSE("GPL");
1839 MODULE_ALIAS_RTNL_LINK("gre");
1840 MODULE_ALIAS_RTNL_LINK("gretap");
1841 MODULE_ALIAS_RTNL_LINK("erspan");
1842 MODULE_ALIAS_NETDEV("gre0");
1843 MODULE_ALIAS_NETDEV("gretap0");
1844 MODULE_ALIAS_NETDEV("erspan0");