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 #ifndef USE_UPSTREAM_TUNNEL
16 #include <linux/capability.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/skbuff.h>
23 #include <linux/netdevice.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/if_arp.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>
59 #include <net/erspan.h>
60 #include <net/dst_metadata.h>
62 #include "vport-netdev.h"
64 #define IP6_GRE_HASH_SIZE_SHIFT 5
65 #define IP6_GRE_HASH_SIZE (1 << IP6_GRE_HASH_SIZE_SHIFT)
67 static unsigned int ip6gre_net_id __read_mostly
;
68 static bool ip6_gre_loaded
= false;
70 struct ip6_tnl __rcu
*tunnels
[4][IP6_GRE_HASH_SIZE
];
72 struct ip6_tnl __rcu
*collect_md_tun
;
73 struct ip6_tnl __rcu
*collect_md_tun_erspan
;
74 struct net_device
*fb_tunnel_dev
;
77 static struct rtnl_link_ops ip6gre_link_ops __read_mostly
;
78 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly
;
79 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly
;
80 static int ip6gre_tunnel_init(struct net_device
*dev
);
81 static void ip6gre_tunnel_setup(struct net_device
*dev
);
82 static void ip6gre_tunnel_link(struct ip6gre_net
*ign
, struct ip6_tnl
*t
);
83 static void ip6gre_tnl_link_config(struct ip6_tnl
*t
, int set_mtu
);
84 static void ip6erspan_tnl_link_config(struct ip6_tnl
*t
, int set_mtu
);
86 /* Tunnel hash table */
96 We require exact key match i.e. if a key is present in packet
97 it will match only tunnel with the same key; if it is not present,
98 it will match only keyless tunnel.
100 All keysless packets, if not matched configured keyless tunnels
101 will match fallback tunnel.
104 #define HASH_KEY(key) (((__force u32)key^((__force u32)key>>4))&(IP6_GRE_HASH_SIZE - 1))
105 static u32
HASH_ADDR(const struct in6_addr
*addr
)
107 u32 hash
= ipv6_addr_hash(addr
);
109 return hash_32(hash
, IP6_GRE_HASH_SIZE_SHIFT
);
112 #define tunnels_r_l tunnels[3]
113 #define tunnels_r tunnels[2]
114 #define tunnels_l tunnels[1]
115 #define tunnels_wc tunnels[0]
117 /* Given src, dst and key, find appropriate for input tunnel. */
119 static struct ip6_tnl
*ip6gre_tunnel_lookup(struct net_device
*dev
,
120 const struct in6_addr
*remote
, const struct in6_addr
*local
,
121 __be32 key
, __be16 gre_proto
)
123 struct net
*net
= dev_net(dev
);
124 int link
= dev
->ifindex
;
125 unsigned int h0
= HASH_ADDR(remote
);
126 unsigned int h1
= HASH_KEY(key
);
127 struct ip6_tnl
*t
, *cand
= NULL
;
128 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
129 int dev_type
= (gre_proto
== htons(ETH_P_TEB
) ||
130 gre_proto
== htons(ETH_P_ERSPAN
) ||
131 gre_proto
== htons(ETH_P_ERSPAN2
)) ?
132 ARPHRD_ETHER
: ARPHRD_IP6GRE
;
133 int score
, cand_score
= 4;
135 for_each_ip_tunnel_rcu(t
, ign
->tunnels_r_l
[h0
^ h1
]) {
136 if (!ipv6_addr_equal(local
, &t
->parms
.laddr
) ||
137 !ipv6_addr_equal(remote
, &t
->parms
.raddr
) ||
138 key
!= t
->parms
.i_key
||
139 !(t
->dev
->flags
& IFF_UP
))
142 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
143 t
->dev
->type
!= dev_type
)
147 if (t
->parms
.link
!= link
)
149 if (t
->dev
->type
!= dev_type
)
154 if (score
< cand_score
) {
160 for_each_ip_tunnel_rcu(t
, ign
->tunnels_r
[h0
^ h1
]) {
161 if (!ipv6_addr_equal(remote
, &t
->parms
.raddr
) ||
162 key
!= t
->parms
.i_key
||
163 !(t
->dev
->flags
& IFF_UP
))
166 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
167 t
->dev
->type
!= dev_type
)
171 if (t
->parms
.link
!= link
)
173 if (t
->dev
->type
!= dev_type
)
178 if (score
< cand_score
) {
184 for_each_ip_tunnel_rcu(t
, ign
->tunnels_l
[h1
]) {
185 if ((!ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
186 (!ipv6_addr_equal(local
, &t
->parms
.raddr
) ||
187 !ipv6_addr_is_multicast(local
))) ||
188 key
!= t
->parms
.i_key
||
189 !(t
->dev
->flags
& IFF_UP
))
192 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
193 t
->dev
->type
!= dev_type
)
197 if (t
->parms
.link
!= link
)
199 if (t
->dev
->type
!= dev_type
)
204 if (score
< cand_score
) {
210 for_each_ip_tunnel_rcu(t
, ign
->tunnels_wc
[h1
]) {
211 if (t
->parms
.i_key
!= key
||
212 !(t
->dev
->flags
& IFF_UP
))
215 if (t
->dev
->type
!= ARPHRD_IP6GRE
&&
216 t
->dev
->type
!= dev_type
)
220 if (t
->parms
.link
!= link
)
222 if (t
->dev
->type
!= dev_type
)
227 if (score
< cand_score
) {
236 if (gre_proto
== htons(ETH_P_ERSPAN
) ||
237 gre_proto
== htons(ETH_P_ERSPAN2
))
238 t
= rcu_dereference(ign
->collect_md_tun_erspan
);
240 t
= rcu_dereference(ign
->collect_md_tun
);
242 if (t
&& t
->dev
->flags
& IFF_UP
)
245 dev
= ign
->fb_tunnel_dev
;
246 if (dev
->flags
& IFF_UP
)
247 return netdev_priv(dev
);
252 static struct ip6_tnl __rcu
**__ip6gre_bucket(struct ip6gre_net
*ign
,
253 const struct __ip6_tnl_parm
*p
)
255 const struct in6_addr
*remote
= &p
->raddr
;
256 const struct in6_addr
*local
= &p
->laddr
;
257 unsigned int h
= HASH_KEY(p
->i_key
);
260 if (!ipv6_addr_any(local
))
262 if (!ipv6_addr_any(remote
) && !ipv6_addr_is_multicast(remote
)) {
264 h
^= HASH_ADDR(remote
);
267 return &ign
->tunnels
[prio
][h
];
270 static void ip6gre_tunnel_link_md(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
272 if (t
->parms
.collect_md
)
273 rcu_assign_pointer(ign
->collect_md_tun
, t
);
276 static void ip6erspan_tunnel_link_md(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
278 if (t
->parms
.collect_md
)
279 rcu_assign_pointer(ign
->collect_md_tun_erspan
, t
);
282 static void ip6gre_tunnel_unlink_md(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
284 if (t
->parms
.collect_md
)
285 rcu_assign_pointer(ign
->collect_md_tun
, NULL
);
288 static void ip6erspan_tunnel_unlink_md(struct ip6gre_net
*ign
,
291 if (t
->parms
.collect_md
)
292 rcu_assign_pointer(ign
->collect_md_tun_erspan
, NULL
);
295 static inline struct ip6_tnl __rcu
**ip6gre_bucket(struct ip6gre_net
*ign
,
296 const struct ip6_tnl
*t
)
298 return __ip6gre_bucket(ign
, &t
->parms
);
301 static void ip6gre_tunnel_link(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
303 struct ip6_tnl __rcu
**tp
= ip6gre_bucket(ign
, t
);
305 rcu_assign_pointer(t
->next
, rtnl_dereference(*tp
));
306 rcu_assign_pointer(*tp
, t
);
309 static void ip6gre_tunnel_unlink(struct ip6gre_net
*ign
, struct ip6_tnl
*t
)
311 struct ip6_tnl __rcu
**tp
;
312 struct ip6_tnl
*iter
;
314 for (tp
= ip6gre_bucket(ign
, t
);
315 (iter
= rtnl_dereference(*tp
)) != NULL
;
318 rcu_assign_pointer(*tp
, t
->next
);
324 static struct ip6_tnl
*ip6gre_tunnel_find(struct net
*net
,
325 const struct __ip6_tnl_parm
*parms
,
328 const struct in6_addr
*remote
= &parms
->raddr
;
329 const struct in6_addr
*local
= &parms
->laddr
;
330 __be32 key
= parms
->i_key
;
331 int link
= parms
->link
;
333 struct ip6_tnl __rcu
**tp
;
334 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
336 for (tp
= __ip6gre_bucket(ign
, parms
);
337 (t
= rtnl_dereference(*tp
)) != NULL
;
339 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
340 ipv6_addr_equal(remote
, &t
->parms
.raddr
) &&
341 key
== t
->parms
.i_key
&&
342 link
== t
->parms
.link
&&
343 type
== t
->dev
->type
)
349 static struct ip6_tnl
*ip6gre_tunnel_locate(struct net
*net
,
350 const struct __ip6_tnl_parm
*parms
, int create
)
352 struct ip6_tnl
*t
, *nt
;
353 struct net_device
*dev
;
355 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
357 t
= ip6gre_tunnel_find(net
, parms
, ARPHRD_IP6GRE
);
364 strlcpy(name
, parms
->name
, IFNAMSIZ
);
366 strlcpy(name
, "ovs-ip6gre%d", IFNAMSIZ
);
368 dev
= alloc_netdev(sizeof(*t
), name
, NET_NAME_UNKNOWN
,
369 ip6gre_tunnel_setup
);
373 dev_net_set(dev
, net
);
375 nt
= netdev_priv(dev
);
377 dev
->rtnl_link_ops
= &ip6gre_link_ops
;
380 nt
->net
= dev_net(dev
);
382 if (register_netdevice(dev
) < 0)
385 ip6gre_tnl_link_config(nt
, 1);
387 /* Can use a lockless transmit, unless we generate output sequences */
388 if (!(nt
->parms
.o_flags
& TUNNEL_SEQ
))
389 dev
->features
|= NETIF_F_LLTX
;
392 ip6gre_tunnel_link(ign
, nt
);
400 static void ip6erspan_tunnel_uninit(struct net_device
*dev
)
402 struct ip6_tnl
*t
= netdev_priv(dev
);
403 struct ip6gre_net
*ign
= net_generic(t
->net
, ip6gre_net_id
);
405 ip6erspan_tunnel_unlink_md(ign
, t
);
406 ip6gre_tunnel_unlink(ign
, t
);
407 dst_cache_reset(&t
->dst_cache
);
411 static void ip6gre_tunnel_uninit(struct net_device
*dev
)
413 struct ip6_tnl
*t
= netdev_priv(dev
);
414 struct ip6gre_net
*ign
= net_generic(t
->net
, ip6gre_net_id
);
416 ip6gre_tunnel_unlink_md(ign
, t
);
417 ip6gre_tunnel_unlink(ign
, t
);
418 dst_cache_reset(&t
->dst_cache
);
423 static void ip6gre_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
424 u8 type
, u8 code
, int offset
, __be32 info
)
427 struct net
*net
= dev_net(skb
->dev
);
428 const struct gre_base_hdr
*greh
;
429 const struct ipv6hdr
*ipv6h
;
430 int grehlen
= sizeof(*greh
);
436 if (!pskb_may_pull(skb
, offset
+ grehlen
))
438 greh
= (const struct gre_base_hdr
*)(skb
->data
+ offset
);
440 if (flags
& (GRE_VERSION
| GRE_ROUTING
))
442 if (flags
& GRE_CSUM
)
444 if (flags
& GRE_KEY
) {
445 key_off
= grehlen
+ offset
;
449 if (!pskb_may_pull(skb
, offset
+ grehlen
))
451 ipv6h
= (const struct ipv6hdr
*)skb
->data
;
452 greh
= (const struct gre_base_hdr
*)(skb
->data
+ offset
);
453 key
= key_off
? *(__be32
*)(skb
->data
+ key_off
) : 0;
455 t
= ip6gre_tunnel_lookup(skb
->dev
, &ipv6h
->daddr
, &ipv6h
->saddr
,
456 key
, greh
->protocol
);
461 struct ipv6_tlv_tnl_enc_lim
*tel
;
463 case ICMPV6_DEST_UNREACH
:
464 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
466 if (code
!= ICMPV6_PORT_UNREACH
)
469 case ICMPV6_TIME_EXCEED
:
470 if (code
== ICMPV6_EXC_HOPLIMIT
) {
471 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
476 case ICMPV6_PARAMPROB
:
478 if (code
== ICMPV6_HDR_FIELD
)
479 teli
= ip6_tnl_parse_tlv_enc_lim(skb
, skb
->data
);
481 if (teli
&& teli
== be32_to_cpu(info
) - 2) {
482 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &skb
->data
[teli
];
483 if (tel
->encap_limit
== 0) {
484 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
488 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
492 case ICMPV6_PKT_TOOBIG
:
493 ip6_update_pmtu(skb
, net
, info
, 0, 0, sock_net_uid(net
, NULL
));
496 ip6_redirect(skb
, net
, skb
->dev
->ifindex
, 0,
497 sock_net_uid(net
, NULL
));
501 if (time_before(jiffies
, t
->err_time
+ IP6TUNNEL_ERR_TIMEO
))
505 t
->err_time
= jiffies
;
509 static int ip6gre_rcv(struct sk_buff
*skb
, const struct tnl_ptk_info
*tpi
)
511 const struct ipv6hdr
*ipv6h
;
512 struct ip6_tnl
*tunnel
;
514 ipv6h
= ipv6_hdr(skb
);
515 tunnel
= ip6gre_tunnel_lookup(skb
->dev
,
516 &ipv6h
->saddr
, &ipv6h
->daddr
, tpi
->key
,
519 struct metadata_dst
*tun_dst
= NULL
;
520 if (tunnel
->parms
.collect_md
) {
525 tun_id
= key32_to_tunnel_id(tpi
->key
);
527 tun_dst
= rpl_ipv6_tun_rx_dst(skb
, flags
, tun_id
, 0);
529 return PACKET_REJECT
;
533 ip6_tnl_rcv(tunnel
, skb
, tpi
, tun_dst
, false);
541 static int ip6erspan_rcv(struct sk_buff
*skb
,
542 struct tnl_ptk_info
*tpi
,
545 struct erspan_base_hdr
*ershdr
;
546 const struct ipv6hdr
*ipv6h
;
547 struct erspan_md2
*md2
;
548 struct ip6_tnl
*tunnel
;
551 if (unlikely(!pskb_may_pull(skb
, sizeof(*ershdr
))))
552 return PACKET_REJECT
;
554 ipv6h
= ipv6_hdr(skb
);
555 ershdr
= (struct erspan_base_hdr
*)skb
->data
;
557 tpi
->key
= cpu_to_be32(get_session_id(ershdr
));
559 tunnel
= ip6gre_tunnel_lookup(skb
->dev
,
560 &ipv6h
->saddr
, &ipv6h
->daddr
, 0,
563 struct metadata_dst
*tun_dst
= NULL
;
564 int len
= erspan_hdr_len(ver
);
566 if (unlikely(!pskb_may_pull(skb
, len
)))
567 return PACKET_REJECT
;
569 if (__iptunnel_pull_header(skb
, len
,
572 return PACKET_REJECT
;
574 if (tunnel
->parms
.collect_md
) {
575 struct erspan_metadata
*pkt_md
, *md
;
576 struct ip_tunnel_info
*info
;
581 tpi
->flags
|= TUNNEL_KEY
;
583 tun_id
= key32_to_tunnel_id(tpi
->key
);
585 tun_dst
= rpl_ipv6_tun_rx_dst(skb
, flags
, tun_id
,
588 return PACKET_REJECT
;
590 /* skb can be uncloned in __iptunnel_pull_header, so
591 * old pkt_md is no longer valid and we need to reset
594 gh
= skb_network_header(skb
) +
595 skb_network_header_len(skb
);
596 pkt_md
= (struct erspan_metadata
*)(gh
+ gre_hdr_len
+
598 info
= &tun_dst
->u
.tun_info
;
599 md
= ip_tunnel_info_opts(info
);
602 memcpy(md2
, pkt_md
, ver
== 1 ? ERSPAN_V1_MDSIZE
:
604 info
->key
.tun_flags
|= TUNNEL_ERSPAN_OPT
;
605 info
->options_len
= sizeof(*md
);
608 ip6_tnl_rcv(tunnel
, skb
, tpi
, tun_dst
, false);
617 static int gre_rcv(struct sk_buff
*skb
)
619 struct tnl_ptk_info tpi
;
620 bool csum_err
= false;
623 hdr_len
= gre_parse_header(skb
, &tpi
, &csum_err
, htons(ETH_P_IPV6
), 0);
627 if (iptunnel_pull_header(skb
, hdr_len
, tpi
.proto
, false))
630 if (unlikely(tpi
.proto
== htons(ETH_P_ERSPAN
) ||
631 tpi
.proto
== htons(ETH_P_ERSPAN2
))) {
632 if (ip6erspan_rcv(skb
, &tpi
, hdr_len
) == PACKET_RCVD
)
637 if (ip6gre_rcv(skb
, &tpi
) == PACKET_RCVD
)
641 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
647 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0)
649 /* gre_handle_offloads() has different return type on older kernsl. */
650 static void gre_nop_fix(struct sk_buff
*skb
) { }
652 static void gre_csum_fix(struct sk_buff
*skb
)
654 struct gre_base_hdr
*greh
;
656 int gre_offset
= skb_transport_offset(skb
);
658 greh
= (struct gre_base_hdr
*)skb_transport_header(skb
);
659 options
= ((__be32
*)greh
+ 1);
662 *(__sum16
*)options
= csum_fold(skb_checksum(skb
, gre_offset
,
663 skb
->len
- gre_offset
, 0));
666 #define gre_handle_offloads rpl_gre_handle_offloads
667 static int rpl_gre_handle_offloads(struct sk_buff
*skb
, bool gre_csum
)
669 int type
= gre_csum
? SKB_GSO_GRE_CSUM
: SKB_GSO_GRE
;
670 gso_fix_segment_t fix_segment
;
673 fix_segment
= gre_csum_fix
;
675 fix_segment
= gre_nop_fix
;
677 return ovs_iptunnel_handle_offloads(skb
, type
, fix_segment
);
680 static int gre_handle_offloads(struct sk_buff
*skb
, bool csum
)
682 return iptunnel_handle_offloads(skb
, csum
,
683 csum
? SKB_GSO_GRE_CSUM
: SKB_GSO_GRE
);
687 static void prepare_ip6gre_xmit_ipv4(struct sk_buff
*skb
,
688 struct net_device
*dev
,
689 struct flowi6
*fl6
, __u8
*dsfield
,
692 const struct iphdr
*iph
= ip_hdr(skb
);
693 struct ip6_tnl
*t
= netdev_priv(dev
);
695 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
696 *encap_limit
= t
->parms
.encap_limit
;
698 memcpy(fl6
, &t
->fl
.u
.ip6
, sizeof(*fl6
));
700 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
701 *dsfield
= ipv4_get_dsfield(iph
);
703 *dsfield
= ip6_tclass(t
->parms
.flowinfo
);
705 #ifndef IP6_TNL_F_USE_ORIG_FWMARK
706 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
707 fl6
->flowi6_mark
= skb
->mark
;
709 fl6
->flowi6_mark
= t
->parms
.fwmark
;
711 fl6
->flowi6_uid
= sock_net_uid(dev_net(dev
), NULL
);
715 static int prepare_ip6gre_xmit_ipv6(struct sk_buff
*skb
,
716 struct net_device
*dev
,
717 struct flowi6
*fl6
, __u8
*dsfield
,
720 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
721 struct ip6_tnl
*t
= netdev_priv(dev
);
724 offset
= ip6_tnl_parse_tlv_enc_lim(skb
, skb_network_header(skb
));
725 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
728 struct ipv6_tlv_tnl_enc_lim
*tel
;
730 tel
= (struct ipv6_tlv_tnl_enc_lim
*)&skb_network_header(skb
)[offset
];
731 if (tel
->encap_limit
== 0) {
732 icmpv6_send(skb
, ICMPV6_PARAMPROB
,
733 ICMPV6_HDR_FIELD
, offset
+ 2);
736 *encap_limit
= tel
->encap_limit
- 1;
737 } else if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)) {
738 *encap_limit
= t
->parms
.encap_limit
;
741 memcpy(fl6
, &t
->fl
.u
.ip6
, sizeof(*fl6
));
743 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
744 *dsfield
= ipv6_get_dsfield(ipv6h
);
746 *dsfield
= ip6_tclass(t
->parms
.flowinfo
);
748 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
749 fl6
->flowlabel
|= ip6_flowlabel(ipv6h
);
751 #ifndef IP6_TNL_F_USE_ORIG_FWMARK
752 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
753 fl6
->flowi6_mark
= skb
->mark
;
755 fl6
->flowi6_mark
= t
->parms
.fwmark
;
757 fl6
->flowi6_uid
= sock_net_uid(dev_net(dev
), NULL
);
763 static netdev_tx_t
__gre6_xmit(struct sk_buff
*skb
,
764 struct net_device
*dev
, __u8 dsfield
,
765 struct flowi6
*fl6
, int encap_limit
,
766 __u32
*pmtu
, __be16 proto
)
768 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
769 struct tnl_ptk_info tpi
;
772 if (dev
->header_ops
&& dev
->type
== ARPHRD_IP6GRE
)
773 fl6
->daddr
= ((struct ipv6hdr
*)skb
->data
)->daddr
;
775 fl6
->daddr
= tunnel
->parms
.raddr
;
777 if (tunnel
->parms
.o_flags
& TUNNEL_SEQ
)
780 if (skb_cow_head(skb
, dev
->needed_headroom
?: tunnel
->hlen
))
783 /* Push GRE header. */
784 protocol
= (dev
->type
== ARPHRD_ETHER
) ? htons(ETH_P_TEB
) : proto
;
786 if (tunnel
->parms
.collect_md
) {
787 struct ip_tunnel_info
*tun_info
;
788 const struct ip_tunnel_key
*key
;
791 tun_info
= skb_tunnel_info(skb
);
792 if (unlikely(!tun_info
||
793 !(tun_info
->mode
& IP_TUNNEL_INFO_TX
) ||
794 ip_tunnel_info_af(tun_info
) != AF_INET6
))
797 key
= &tun_info
->key
;
798 memset(fl6
, 0, sizeof(*fl6
));
799 fl6
->flowi6_proto
= IPPROTO_GRE
;
800 fl6
->daddr
= key
->u
.ipv6
.dst
;
801 fl6
->flowlabel
= key
->label
;
803 // fl6->flowi6_uid = sock_net_uid(dev_net(dev), NULL);
806 flags
= key
->tun_flags
& (TUNNEL_CSUM
| TUNNEL_KEY
);
807 tunnel
->tun_hlen
= gre_calc_hlen(flags
);
810 tpi
.proto
= protocol
;
811 tpi
.key
= tunnel_id_to_key32(key
->tun_id
);
812 tpi
.seq
= htonl(tunnel
->o_seqno
++);
813 tpi
.hdr_len
= tunnel
->tun_hlen
;
815 gre_build_header(skb
, &tpi
, 8);
817 tpi
.flags
= tunnel
->parms
.o_flags
;
818 tpi
.proto
= protocol
;
819 tpi
.key
= tunnel
->parms
.o_key
;
820 tpi
.seq
= htonl(tunnel
->o_seqno
++);
821 tpi
.hdr_len
= tunnel
->tun_hlen
;
823 gre_build_header(skb
, &tpi
, 8);
826 return ip6_tnl_xmit(skb
, dev
, dsfield
, fl6
, encap_limit
, pmtu
,
830 static inline int ip6gre_xmit_ipv4(struct sk_buff
*skb
, struct net_device
*dev
)
832 struct ip6_tnl
*t
= netdev_priv(dev
);
833 int encap_limit
= -1;
839 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
841 if (!t
->parms
.collect_md
)
842 prepare_ip6gre_xmit_ipv4(skb
, dev
, &fl6
,
843 &dsfield
, &encap_limit
);
845 err
= gre_handle_offloads(skb
, !!(t
->parms
.o_flags
& TUNNEL_CSUM
));
849 err
= __gre6_xmit(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
,
852 /* XXX: send ICMP error even if DF is not set. */
853 if (err
== -EMSGSIZE
)
854 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
862 static inline int ip6gre_xmit_ipv6(struct sk_buff
*skb
, struct net_device
*dev
)
864 struct ip6_tnl
*t
= netdev_priv(dev
);
865 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
866 int encap_limit
= -1;
872 if (ipv6_addr_equal(&t
->parms
.raddr
, &ipv6h
->saddr
))
875 if (!t
->parms
.collect_md
&&
876 prepare_ip6gre_xmit_ipv6(skb
, dev
, &fl6
, &dsfield
, &encap_limit
))
879 if (gre_handle_offloads(skb
, !!(t
->parms
.o_flags
& TUNNEL_CSUM
)))
882 err
= __gre6_xmit(skb
, dev
, dsfield
, &fl6
, encap_limit
,
883 &mtu
, skb
->protocol
);
885 if (err
== -EMSGSIZE
)
886 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
894 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
895 * @t: the outgoing tunnel device
896 * @hdr: IPv6 header from the incoming packet
899 * Avoid trivial tunneling loop by checking that tunnel exit-point
900 * doesn't match source of incoming packet.
907 static inline bool ip6gre_tnl_addr_conflict(const struct ip6_tnl
*t
,
908 const struct ipv6hdr
*hdr
)
910 return ipv6_addr_equal(&t
->parms
.raddr
, &hdr
->saddr
);
913 static int ip6gre_xmit_other(struct sk_buff
*skb
, struct net_device
*dev
)
915 struct ip6_tnl
*t
= netdev_priv(dev
);
916 int encap_limit
= -1;
921 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
922 encap_limit
= t
->parms
.encap_limit
;
924 if (!t
->parms
.collect_md
)
925 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
927 err
= gre_handle_offloads(skb
, !!(t
->parms
.o_flags
& TUNNEL_CSUM
));
931 err
= __gre6_xmit(skb
, dev
, 0, &fl6
, encap_limit
, &mtu
, skb
->protocol
);
936 static netdev_tx_t
ip6gre_tunnel_xmit(struct sk_buff
*skb
,
937 struct net_device
*dev
)
939 struct ip6_tnl
*t
= netdev_priv(dev
);
940 struct net_device_stats
*stats
= &t
->dev
->stats
;
943 if (!ip6_tnl_xmit_ctl(t
, &t
->parms
.laddr
, &t
->parms
.raddr
))
946 switch (skb
->protocol
) {
947 case htons(ETH_P_IP
):
948 ret
= ip6gre_xmit_ipv4(skb
, dev
);
950 case htons(ETH_P_IPV6
):
951 ret
= ip6gre_xmit_ipv6(skb
, dev
);
954 ret
= ip6gre_xmit_other(skb
, dev
);
970 static netdev_tx_t
__ip6gre_tunnel_xmit(struct sk_buff
*skb
)
972 return ip6gre_tunnel_xmit(skb
, skb
->dev
);
975 static bool erspan_skb_would_panic(struct sk_buff
*skb
, int erspan_md_size
)
977 /* check if there is enough headroom in packet, if not
978 * drop it. Checking for 8 bytes of gre header space +
979 * erspan base hdr and erspan type specific header.
981 if (skb_headroom(skb
) < (8 + sizeof(struct erspan_base_hdr
) +
988 static netdev_tx_t
ip6erspan_tunnel_xmit(struct sk_buff
*skb
,
989 struct net_device
*dev
)
991 struct ip6_tnl
*t
= netdev_priv(dev
);
992 struct dst_entry
*dst
= skb_dst(skb
);
993 struct ip_tunnel_info
*tun_info
;
994 const struct ip_tunnel_key
*key
;
995 struct net_device_stats
*stats
;
996 struct erspan_metadata
*md
;
997 struct tnl_ptk_info tpi
;
998 bool truncate
= false;
999 int encap_limit
= -1;
1000 __u8 dsfield
= false;
1009 /* OVS doesn't support native mode ip6 tunnel traffic so
1010 * take an early exit in that case. */
1011 if (!t
->parms
.collect_md
)
1014 if (!ip6_tnl_xmit_ctl(t
, &t
->parms
.laddr
, &t
->parms
.raddr
))
1017 if (gre_handle_offloads(skb
, false))
1020 if (skb
->len
> dev
->mtu
+ dev
->hard_header_len
) {
1021 pskb_trim(skb
, dev
->mtu
+ dev
->hard_header_len
);
1025 nhoff
= skb_network_header(skb
) - skb_mac_header(skb
);
1026 if (skb
->protocol
== htons(ETH_P_IP
) &&
1027 (ntohs(ip_hdr(skb
)->tot_len
) > skb
->len
- nhoff
))
1030 thoff
= skb_transport_header(skb
) - skb_mac_header(skb
);
1031 if (skb
->protocol
== htons(ETH_P_IPV6
) &&
1032 (ntohs(ipv6_hdr(skb
)->payload_len
) > skb
->len
- thoff
))
1035 if (skb_cow_head(skb
, dev
->needed_headroom
? : t
->hlen
))
1038 t
->parms
.o_flags
&= ~TUNNEL_KEY
;
1040 tun_info
= ovs_skb_tunnel_info(skb
);
1041 if (unlikely(!tun_info
||
1042 !(tun_info
->mode
& IP_TUNNEL_INFO_TX
) ||
1043 ip_tunnel_info_af(tun_info
) != AF_INET6
))
1046 key
= &tun_info
->key
;
1047 memset(&fl6
, 0, sizeof(fl6
));
1048 fl6
.flowi6_proto
= IPPROTO_GRE
;
1049 fl6
.daddr
= key
->u
.ipv6
.dst
;
1050 fl6
.flowlabel
= key
->label
;
1051 // fl6.flowi6_uid = sock_net_uid(dev_net(dev), NULL);
1054 md
= ip_tunnel_info_opts(tun_info
);
1058 if (erspan_skb_would_panic(skb
,
1060 ERSPAN_V1_MDSIZE
: ERSPAN_V2_MDSIZE
))
1063 tun_id
= tunnel_id_to_key32(key
->tun_id
);
1064 if (md
->version
== 1) {
1065 erspan_build_header(skb
,
1067 ntohl(md
->u
.index
), truncate
,
1069 tpi
.hdr_len
= ERSPAN_V1_MDSIZE
;
1070 tpi
.proto
= htons(ETH_P_ERSPAN
);
1071 } else if (md
->version
== 2) {
1072 erspan_build_header_v2(skb
,
1075 get_hwid(&md
->u
.md2
),
1077 tpi
.hdr_len
= ERSPAN_V2_MDSIZE
;
1078 tpi
.proto
= htons(ETH_P_ERSPAN2
);
1083 tpi
.flags
= TUNNEL_SEQ
;
1085 tpi
.seq
= htonl(t
->o_seqno
++);
1087 /* Push GRE header. */
1088 gre_build_header(skb
, &tpi
, 8);
1090 /* TooBig packet may have updated dst->dev's mtu */
1091 if (!t
->parms
.collect_md
&& dst
&& dst_mtu(dst
) > dst
->dev
->mtu
)
1092 #ifndef HAVE_DST_OPS_CONFIRM_NEIGH
1093 dst
->ops
->update_pmtu(dst
, NULL
, skb
, dst
->dev
->mtu
);
1095 dst
->ops
->update_pmtu(dst
, NULL
, skb
, dst
->dev
->mtu
, false);
1098 err
= ip6_tnl_xmit(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
,
1103 return NETDEV_TX_OK
;
1106 stats
= &t
->dev
->stats
;
1108 stats
->tx_dropped
++;
1110 return NETDEV_TX_OK
;
1113 static netdev_tx_t
__ip6erspan_tunnel_xmit(struct sk_buff
*skb
)
1115 return ip6erspan_tunnel_xmit(skb
, skb
->dev
);
1118 static void ip6gre_tnl_link_config_common(struct ip6_tnl
*t
)
1120 struct net_device
*dev
= t
->dev
;
1121 struct __ip6_tnl_parm
*p
= &t
->parms
;
1122 struct flowi6
*fl6
= &t
->fl
.u
.ip6
;
1124 if (dev
->type
!= ARPHRD_ETHER
) {
1125 memcpy(dev
->dev_addr
, &p
->laddr
, sizeof(struct in6_addr
));
1126 memcpy(dev
->broadcast
, &p
->raddr
, sizeof(struct in6_addr
));
1129 /* Set up flowi template */
1130 fl6
->saddr
= p
->laddr
;
1131 fl6
->daddr
= p
->raddr
;
1132 fl6
->flowi6_oif
= p
->link
;
1134 fl6
->flowi6_proto
= IPPROTO_GRE
;
1136 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_TCLASS
))
1137 fl6
->flowlabel
|= IPV6_TCLASS_MASK
& p
->flowinfo
;
1138 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_FLOWLABEL
))
1139 fl6
->flowlabel
|= IPV6_FLOWLABEL_MASK
& p
->flowinfo
;
1141 p
->flags
&= ~(IP6_TNL_F_CAP_XMIT
|IP6_TNL_F_CAP_RCV
|IP6_TNL_F_CAP_PER_PACKET
);
1142 p
->flags
|= ip6_tnl_get_cap(t
, &p
->laddr
, &p
->raddr
);
1144 if (p
->flags
&IP6_TNL_F_CAP_XMIT
&&
1145 p
->flags
&IP6_TNL_F_CAP_RCV
&& dev
->type
!= ARPHRD_ETHER
)
1146 dev
->flags
|= IFF_POINTOPOINT
;
1148 dev
->flags
&= ~IFF_POINTOPOINT
;
1151 static void ip6gre_tnl_link_config_route(struct ip6_tnl
*t
, int set_mtu
,
1154 const struct __ip6_tnl_parm
*p
= &t
->parms
;
1155 struct net_device
*dev
= t
->dev
;
1157 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
1158 int strict
= (ipv6_addr_type(&p
->raddr
) &
1159 (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
));
1161 struct rt6_info
*rt
= rt6_lookup(t
->net
,
1162 &p
->raddr
, &p
->laddr
,
1169 dev
->hard_header_len
= rt
->dst
.dev
->hard_header_len
+
1173 dev
->mtu
= rt
->dst
.dev
->mtu
- t_hlen
;
1174 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1176 if (dev
->type
== ARPHRD_ETHER
)
1177 dev
->mtu
-= ETH_HLEN
;
1179 if (dev
->mtu
< IPV6_MIN_MTU
)
1180 dev
->mtu
= IPV6_MIN_MTU
;
1187 static int ip6gre_calc_hlen(struct ip6_tnl
*tunnel
)
1191 tunnel
->tun_hlen
= gre_calc_hlen(tunnel
->parms
.o_flags
);
1192 tunnel
->hlen
= tunnel
->tun_hlen
+ tunnel
->encap_hlen
;
1194 t_hlen
= tunnel
->hlen
+ sizeof(struct ipv6hdr
);
1195 tunnel
->dev
->hard_header_len
= LL_MAX_HEADER
+ t_hlen
;
1199 static void ip6gre_tnl_link_config(struct ip6_tnl
*t
, int set_mtu
)
1201 ip6gre_tnl_link_config_common(t
);
1202 ip6gre_tnl_link_config_route(t
, set_mtu
, ip6gre_calc_hlen(t
));
1205 static void ip6gre_tnl_copy_tnl_parm(struct ip6_tnl
*t
,
1206 const struct __ip6_tnl_parm
*p
)
1208 t
->parms
.laddr
= p
->laddr
;
1209 t
->parms
.raddr
= p
->raddr
;
1210 t
->parms
.flags
= p
->flags
;
1211 t
->parms
.hop_limit
= p
->hop_limit
;
1212 t
->parms
.encap_limit
= p
->encap_limit
;
1213 t
->parms
.flowinfo
= p
->flowinfo
;
1214 t
->parms
.link
= p
->link
;
1215 t
->parms
.proto
= p
->proto
;
1216 t
->parms
.i_key
= p
->i_key
;
1217 t
->parms
.o_key
= p
->o_key
;
1218 t
->parms
.i_flags
= p
->i_flags
;
1219 t
->parms
.o_flags
= p
->o_flags
;
1220 t
->parms
.fwmark
= p
->fwmark
;
1221 dst_cache_reset(&t
->dst_cache
);
1224 static int ip6gre_tnl_change(struct ip6_tnl
*t
, const struct __ip6_tnl_parm
*p
,
1227 ip6gre_tnl_copy_tnl_parm(t
, p
);
1228 ip6gre_tnl_link_config(t
, set_mtu
);
1232 static void ip6gre_tnl_parm_from_user(struct __ip6_tnl_parm
*p
,
1233 const struct ip6_tnl_parm2
*u
)
1235 p
->laddr
= u
->laddr
;
1236 p
->raddr
= u
->raddr
;
1237 p
->flags
= u
->flags
;
1238 p
->hop_limit
= u
->hop_limit
;
1239 p
->encap_limit
= u
->encap_limit
;
1240 p
->flowinfo
= u
->flowinfo
;
1242 p
->i_key
= u
->i_key
;
1243 p
->o_key
= u
->o_key
;
1244 p
->i_flags
= gre_flags_to_tnl_flags(u
->i_flags
);
1245 p
->o_flags
= gre_flags_to_tnl_flags(u
->o_flags
);
1246 memcpy(p
->name
, u
->name
, sizeof(u
->name
));
1249 static void ip6gre_tnl_parm_to_user(struct ip6_tnl_parm2
*u
,
1250 const struct __ip6_tnl_parm
*p
)
1252 u
->proto
= IPPROTO_GRE
;
1253 u
->laddr
= p
->laddr
;
1254 u
->raddr
= p
->raddr
;
1255 u
->flags
= p
->flags
;
1256 u
->hop_limit
= p
->hop_limit
;
1257 u
->encap_limit
= p
->encap_limit
;
1258 u
->flowinfo
= p
->flowinfo
;
1260 u
->i_key
= p
->i_key
;
1261 u
->o_key
= p
->o_key
;
1262 u
->i_flags
= gre_tnl_flags_to_gre_flags(p
->i_flags
);
1263 u
->o_flags
= gre_tnl_flags_to_gre_flags(p
->o_flags
);
1264 memcpy(u
->name
, p
->name
, sizeof(u
->name
));
1267 static int ip6gre_tunnel_ioctl(struct net_device
*dev
,
1268 struct ifreq
*ifr
, int cmd
)
1271 struct ip6_tnl_parm2 p
;
1272 struct __ip6_tnl_parm p1
;
1273 struct ip6_tnl
*t
= netdev_priv(dev
);
1274 struct net
*net
= t
->net
;
1275 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1277 memset(&p1
, 0, sizeof(p1
));
1281 if (dev
== ign
->fb_tunnel_dev
) {
1282 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
))) {
1286 ip6gre_tnl_parm_from_user(&p1
, &p
);
1287 t
= ip6gre_tunnel_locate(net
, &p1
, 0);
1289 t
= netdev_priv(dev
);
1291 memset(&p
, 0, sizeof(p
));
1292 ip6gre_tnl_parm_to_user(&p
, &t
->parms
);
1293 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1300 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1304 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1308 if ((p
.i_flags
|p
.o_flags
)&(GRE_VERSION
|GRE_ROUTING
))
1311 if (!(p
.i_flags
&GRE_KEY
))
1313 if (!(p
.o_flags
&GRE_KEY
))
1316 ip6gre_tnl_parm_from_user(&p1
, &p
);
1317 t
= ip6gre_tunnel_locate(net
, &p1
, cmd
== SIOCADDTUNNEL
);
1319 if (dev
!= ign
->fb_tunnel_dev
&& cmd
== SIOCCHGTUNNEL
) {
1321 if (t
->dev
!= dev
) {
1326 t
= netdev_priv(dev
);
1328 ip6gre_tunnel_unlink(ign
, t
);
1330 ip6gre_tnl_change(t
, &p1
, 1);
1331 ip6gre_tunnel_link(ign
, t
);
1332 netdev_state_change(dev
);
1339 memset(&p
, 0, sizeof(p
));
1340 ip6gre_tnl_parm_to_user(&p
, &t
->parms
);
1341 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1344 err
= (cmd
== SIOCADDTUNNEL
? -ENOBUFS
: -ENOENT
);
1349 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1352 if (dev
== ign
->fb_tunnel_dev
) {
1354 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1357 ip6gre_tnl_parm_from_user(&p1
, &p
);
1358 t
= ip6gre_tunnel_locate(net
, &p1
, 0);
1362 if (t
== netdev_priv(ign
->fb_tunnel_dev
))
1366 unregister_netdevice(dev
);
1378 static int ip6gre_header(struct sk_buff
*skb
, struct net_device
*dev
,
1379 unsigned short type
, const void *daddr
,
1380 const void *saddr
, unsigned int len
)
1382 struct ip6_tnl
*t
= netdev_priv(dev
);
1383 struct ipv6hdr
*ipv6h
;
1386 ipv6h
= (struct ipv6hdr
*)skb_push(skb
, t
->hlen
+ sizeof(*ipv6h
));
1387 ip6_flow_hdr(ipv6h
, 0, ip6_make_flowlabel(dev_net(dev
), skb
,
1388 t
->fl
.u
.ip6
.flowlabel
,
1389 true, &t
->fl
.u
.ip6
));
1390 ipv6h
->hop_limit
= t
->parms
.hop_limit
;
1391 ipv6h
->nexthdr
= NEXTHDR_GRE
;
1392 ipv6h
->saddr
= t
->parms
.laddr
;
1393 ipv6h
->daddr
= t
->parms
.raddr
;
1395 p
= (__be16
*)(ipv6h
+ 1);
1396 p
[0] = t
->parms
.o_flags
;
1400 * Set the source hardware address.
1404 memcpy(&ipv6h
->saddr
, saddr
, sizeof(struct in6_addr
));
1406 memcpy(&ipv6h
->daddr
, daddr
, sizeof(struct in6_addr
));
1407 if (!ipv6_addr_any(&ipv6h
->daddr
))
1413 static const struct header_ops ip6gre_header_ops
= {
1414 .create
= ip6gre_header
,
1417 static const struct net_device_ops ip6gre_netdev_ops
= {
1418 .ndo_init
= ip6gre_tunnel_init
,
1419 .ndo_uninit
= ip6gre_tunnel_uninit
,
1420 .ndo_start_xmit
= ip6gre_tunnel_xmit
,
1421 .ndo_do_ioctl
= ip6gre_tunnel_ioctl
,
1422 #ifdef HAVE_RHEL7_MAX_MTU
1423 .ndo_size
= sizeof(struct net_device_ops
),
1424 .extended
.ndo_change_mtu
= ip6_tnl_change_mtu
,
1426 .ndo_change_mtu
= ip6_tnl_change_mtu
,
1428 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1429 #ifdef HAVE_NDO_GET_IFLINK
1430 .ndo_get_iflink
= ip6_tnl_get_iflink
,
1434 #ifdef HAVE_NEEDS_FREE_NETDEV
1435 static void ip6gre_dev_free(struct net_device
*dev
)
1437 struct ip6_tnl
*t
= netdev_priv(dev
);
1439 dst_cache_destroy(&t
->dst_cache
);
1440 free_percpu(dev
->tstats
);
1444 static void ip6gre_tunnel_setup(struct net_device
*dev
)
1446 dev
->netdev_ops
= &ip6gre_netdev_ops
;
1447 #ifndef HAVE_NEEDS_FREE_NETDEV
1448 dev
->destructor
= free_netdev
;
1450 dev
->needs_free_netdev
= true;
1451 dev
->priv_destructor
= ip6gre_dev_free
;
1454 dev
->type
= ARPHRD_IP6GRE
;
1456 dev
->flags
|= IFF_NOARP
;
1457 dev
->addr_len
= sizeof(struct in6_addr
);
1458 netif_keep_dst(dev
);
1459 /* This perm addr will be used as interface identifier by IPv6 */
1460 dev
->addr_assign_type
= NET_ADDR_RANDOM
;
1461 eth_random_addr(dev
->perm_addr
);
1464 #define GRE6_FEATURES (NETIF_F_SG | \
1465 NETIF_F_FRAGLIST | \
1469 static void ip6gre_tnl_init_features(struct net_device
*dev
)
1471 struct ip6_tnl
*nt
= netdev_priv(dev
);
1473 dev
->features
|= GRE6_FEATURES
;
1474 dev
->hw_features
|= GRE6_FEATURES
;
1476 if (!(nt
->parms
.o_flags
& TUNNEL_SEQ
)) {
1477 /* TCP offload with GRE SEQ is not supported, nor
1478 * can we support 2 levels of outer headers requiring
1481 if (!(nt
->parms
.o_flags
& TUNNEL_CSUM
) ||
1482 nt
->encap
.type
== TUNNEL_ENCAP_NONE
) {
1483 dev
->features
|= NETIF_F_GSO_SOFTWARE
;
1484 dev
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1487 /* Can use a lockless transmit, unless we generate
1490 dev
->features
|= NETIF_F_LLTX
;
1494 static int ip6gre_tunnel_init_common(struct net_device
*dev
)
1496 struct ip6_tnl
*tunnel
;
1500 tunnel
= netdev_priv(dev
);
1503 tunnel
->net
= dev_net(dev
);
1504 strcpy(tunnel
->parms
.name
, dev
->name
);
1506 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
1510 ret
= dst_cache_init(&tunnel
->dst_cache
, GFP_KERNEL
);
1512 free_percpu(dev
->tstats
);
1517 t_hlen
= ip6gre_calc_hlen(tunnel
);
1518 dev
->mtu
= ETH_DATA_LEN
- t_hlen
;
1519 if (dev
->type
== ARPHRD_ETHER
)
1520 dev
->mtu
-= ETH_HLEN
;
1521 if (!(tunnel
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1524 if (tunnel
->parms
.collect_md
) {
1525 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1526 netif_keep_dst(dev
);
1528 ip6gre_tnl_init_features(dev
);
1533 static int ip6gre_tunnel_init(struct net_device
*dev
)
1535 struct ip6_tnl
*tunnel
;
1538 ret
= ip6gre_tunnel_init_common(dev
);
1542 tunnel
= netdev_priv(dev
);
1544 if (tunnel
->parms
.collect_md
)
1547 memcpy(dev
->dev_addr
, &tunnel
->parms
.laddr
, sizeof(struct in6_addr
));
1548 memcpy(dev
->broadcast
, &tunnel
->parms
.raddr
, sizeof(struct in6_addr
));
1550 if (ipv6_addr_any(&tunnel
->parms
.raddr
))
1551 dev
->header_ops
= &ip6gre_header_ops
;
1556 static void ip6gre_fb_tunnel_init(struct net_device
*dev
)
1558 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1561 tunnel
->net
= dev_net(dev
);
1562 strcpy(tunnel
->parms
.name
, dev
->name
);
1564 tunnel
->hlen
= sizeof(struct ipv6hdr
) + 4;
1569 static struct inet6_protocol ip6gre_protocol __read_mostly
= {
1571 .err_handler
= ip6gre_err
,
1572 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
1575 static void ip6gre_destroy_tunnels(struct net
*net
, struct list_head
*head
)
1577 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1578 struct net_device
*dev
, *aux
;
1581 for_each_netdev_safe(net
, dev
, aux
)
1582 if (dev
->rtnl_link_ops
== &ip6gre_link_ops
||
1583 dev
->rtnl_link_ops
== &ip6gre_tap_ops
||
1584 dev
->rtnl_link_ops
== &ip6erspan_tap_ops
)
1585 unregister_netdevice_queue(dev
, head
);
1587 for (prio
= 0; prio
< 4; prio
++) {
1589 for (h
= 0; h
< IP6_GRE_HASH_SIZE
; h
++) {
1592 t
= rtnl_dereference(ign
->tunnels
[prio
][h
]);
1595 /* If dev is in the same netns, it has already
1596 * been added to the list by the previous loop.
1598 if (!net_eq(dev_net(t
->dev
), net
))
1599 unregister_netdevice_queue(t
->dev
,
1601 t
= rtnl_dereference(t
->next
);
1607 static int __net_init
ip6gre_init_net(struct net
*net
)
1609 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
1612 ign
->fb_tunnel_dev
= alloc_netdev(sizeof(struct ip6_tnl
),
1615 ip6gre_tunnel_setup
);
1616 if (!ign
->fb_tunnel_dev
) {
1620 dev_net_set(ign
->fb_tunnel_dev
, net
);
1621 /* FB netdevice is special: we have one, and only one per netns.
1622 * Allowing to move it to another netns is clearly unsafe.
1624 ign
->fb_tunnel_dev
->features
|= NETIF_F_NETNS_LOCAL
;
1627 ip6gre_fb_tunnel_init(ign
->fb_tunnel_dev
);
1628 ign
->fb_tunnel_dev
->rtnl_link_ops
= &ip6gre_link_ops
;
1630 err
= register_netdev(ign
->fb_tunnel_dev
);
1634 rcu_assign_pointer(ign
->tunnels_wc
[0],
1635 netdev_priv(ign
->fb_tunnel_dev
));
1639 free_netdev(ign
->fb_tunnel_dev
);
1644 static void __net_exit
ip6gre_exit_batch_net(struct list_head
*net_list
)
1650 list_for_each_entry(net
, net_list
, exit_list
)
1651 ip6gre_destroy_tunnels(net
, &list
);
1652 unregister_netdevice_many(&list
);
1657 #ifndef HAVE_IFLA_GRE_ENCAP_DPORT
1658 IFLA_GRE_ENCAP_TYPE
= IFLA_GRE_FLAGS
+ 1,
1659 IFLA_GRE_ENCAP_FLAGS
,
1660 IFLA_GRE_ENCAP_SPORT
,
1661 IFLA_GRE_ENCAP_DPORT
,
1663 #ifndef HAVE_IFLA_GRE_COLLECT_METADATA
1664 IFLA_GRE_COLLECT_METADATA
= IFLA_GRE_ENCAP_DPORT
+ 1,
1666 #ifndef HAVE_IFLA_GRE_IGNORE_DF
1667 IFLA_GRE_IGNORE_DF
= IFLA_GRE_COLLECT_METADATA
+ 1,
1669 #ifndef HAVE_IFLA_GRE_FWMARK
1670 IFLA_GRE_FWMARK
= IFLA_GRE_IGNORE_DF
+ 1,
1672 #ifndef HAVE_IFLA_GRE_ERSPAN_INDEX
1673 IFLA_GRE_ERSPAN_INDEX
= IFLA_GRE_FWMARK
+ 1,
1675 #ifndef HAVE_IFLA_GRE_ERSPAN_HWID
1676 IFLA_GRE_ERSPAN_VER
= IFLA_GRE_ERSPAN_INDEX
+ 1,
1677 IFLA_GRE_ERSPAN_DIR
,
1678 IFLA_GRE_ERSPAN_HWID
,
1682 #define RPL_IFLA_GRE_MAX (IFLA_GRE_ERSPAN_HWID + 1)
1684 static struct pernet_operations ip6gre_net_ops
= {
1685 .init
= ip6gre_init_net
,
1686 .exit_batch
= ip6gre_exit_batch_net
,
1687 .id
= &ip6gre_net_id
,
1688 .size
= sizeof(struct ip6gre_net
),
1690 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
1691 static int rpl_ip6gre_tunnel_validate(struct nlattr
*tb
[],
1692 struct nlattr
*data
[],
1693 struct netlink_ext_ack
*extack
)
1695 static int rpl_ip6gre_tunnel_validate(struct nlattr
*tb
[],
1696 struct nlattr
*data
[])
1705 if (data
[IFLA_GRE_IFLAGS
])
1706 flags
|= nla_get_be16(data
[IFLA_GRE_IFLAGS
]);
1707 if (data
[IFLA_GRE_OFLAGS
])
1708 flags
|= nla_get_be16(data
[IFLA_GRE_OFLAGS
]);
1709 if (flags
& (GRE_VERSION
|GRE_ROUTING
))
1714 #define ip6gre_tunnel_validate rpl_ip6gre_tunnel_validate
1716 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
1717 static int rpl_ip6gre_tap_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
1718 struct netlink_ext_ack
*extack
)
1720 static int rpl_ip6gre_tap_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1723 struct in6_addr daddr
;
1725 if (tb
[IFLA_ADDRESS
]) {
1726 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1728 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1729 return -EADDRNOTAVAIL
;
1735 if (data
[IFLA_GRE_REMOTE
]) {
1736 daddr
= nla_get_in6_addr(data
[IFLA_GRE_REMOTE
]);
1737 if (ipv6_addr_any(&daddr
))
1742 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
1743 return ip6gre_tunnel_validate(tb
, data
, extack
);
1745 return ip6gre_tunnel_validate(tb
, data
);
1748 #define ip6gre_tap_validate rpl_ip6gre_tap_validate
1750 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
1751 static int rpl_ip6erspan_tap_validate(struct nlattr
*tb
[],
1752 struct nlattr
*data
[],
1753 struct netlink_ext_ack
*extack
)
1755 static int rpl_ip6erspan_tap_validate(struct nlattr
*tb
[],
1756 struct nlattr
*data
[])
1765 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
1766 ret
= ip6gre_tap_validate(tb
, data
, extack
);
1768 ret
= ip6gre_tap_validate(tb
, data
);
1773 /* ERSPAN should only have GRE sequence and key flag */
1774 if (data
[IFLA_GRE_OFLAGS
])
1775 flags
|= nla_get_be16(data
[IFLA_GRE_OFLAGS
]);
1776 if (data
[IFLA_GRE_IFLAGS
])
1777 flags
|= nla_get_be16(data
[IFLA_GRE_IFLAGS
]);
1778 if (!data
[IFLA_GRE_COLLECT_METADATA
] &&
1779 flags
!= (GRE_SEQ
| GRE_KEY
))
1782 /* ERSPAN Session ID only has 10-bit. Since we reuse
1783 * 32-bit key field as ID, check it's range.
1785 if (data
[IFLA_GRE_IKEY
] &&
1786 (ntohl(nla_get_be32(data
[IFLA_GRE_IKEY
])) & ~ID_MASK
))
1789 if (data
[IFLA_GRE_OKEY
] &&
1790 (ntohl(nla_get_be32(data
[IFLA_GRE_OKEY
])) & ~ID_MASK
))
1793 if (data
[IFLA_GRE_ERSPAN_VER
]) {
1794 ver
= nla_get_u8(data
[IFLA_GRE_ERSPAN_VER
]);
1795 if (ver
!= 1 && ver
!= 2)
1800 if (data
[IFLA_GRE_ERSPAN_INDEX
]) {
1801 u32 index
= nla_get_u32(data
[IFLA_GRE_ERSPAN_INDEX
]);
1803 if (index
& ~INDEX_MASK
)
1806 } else if (ver
== 2) {
1807 if (data
[IFLA_GRE_ERSPAN_DIR
]) {
1808 u16 dir
= nla_get_u8(data
[IFLA_GRE_ERSPAN_DIR
]);
1810 if (dir
& ~(DIR_MASK
>> DIR_OFFSET
))
1814 if (data
[IFLA_GRE_ERSPAN_HWID
]) {
1815 u16 hwid
= nla_get_u16(data
[IFLA_GRE_ERSPAN_HWID
]);
1817 if (hwid
& ~(HWID_MASK
>> HWID_OFFSET
))
1824 #define ip6erspan_tap_validate rpl_ip6erspan_tap_validate
1826 static void ip6gre_netlink_parms(struct nlattr
*data
[],
1827 struct __ip6_tnl_parm
*parms
)
1830 /* Do not use in case of OVS - our vport needs to set a parm
1831 * directly and this erases it
1833 memset(parms
, 0, sizeof(*parms
));
1839 if (data
[IFLA_GRE_LINK
])
1840 parms
->link
= nla_get_u32(data
[IFLA_GRE_LINK
]);
1842 if (data
[IFLA_GRE_IFLAGS
])
1843 parms
->i_flags
= gre_flags_to_tnl_flags(
1844 nla_get_be16(data
[IFLA_GRE_IFLAGS
]));
1846 if (data
[IFLA_GRE_OFLAGS
])
1847 parms
->o_flags
= gre_flags_to_tnl_flags(
1848 nla_get_be16(data
[IFLA_GRE_OFLAGS
]));
1850 if (data
[IFLA_GRE_IKEY
])
1851 parms
->i_key
= nla_get_be32(data
[IFLA_GRE_IKEY
]);
1853 if (data
[IFLA_GRE_OKEY
])
1854 parms
->o_key
= nla_get_be32(data
[IFLA_GRE_OKEY
]);
1856 if (data
[IFLA_GRE_LOCAL
])
1857 parms
->laddr
= nla_get_in6_addr(data
[IFLA_GRE_LOCAL
]);
1859 if (data
[IFLA_GRE_REMOTE
])
1860 parms
->raddr
= nla_get_in6_addr(data
[IFLA_GRE_REMOTE
]);
1862 if (data
[IFLA_GRE_TTL
])
1863 parms
->hop_limit
= nla_get_u8(data
[IFLA_GRE_TTL
]);
1865 if (data
[IFLA_GRE_ENCAP_LIMIT
])
1866 parms
->encap_limit
= nla_get_u8(data
[IFLA_GRE_ENCAP_LIMIT
]);
1868 if (data
[IFLA_GRE_FLOWINFO
])
1869 parms
->flowinfo
= nla_get_be32(data
[IFLA_GRE_FLOWINFO
]);
1871 if (data
[IFLA_GRE_FLAGS
])
1872 parms
->flags
= nla_get_u32(data
[IFLA_GRE_FLAGS
]);
1874 if (data
[IFLA_GRE_FWMARK
])
1875 parms
->fwmark
= nla_get_u32(data
[IFLA_GRE_FWMARK
]);
1877 if (data
[IFLA_GRE_COLLECT_METADATA
])
1878 parms
->collect_md
= true;
1880 parms
->erspan_ver
= 1;
1881 if (data
[IFLA_GRE_ERSPAN_VER
])
1882 parms
->erspan_ver
= nla_get_u8(data
[IFLA_GRE_ERSPAN_VER
]);
1884 if (parms
->erspan_ver
== 1) {
1885 if (data
[IFLA_GRE_ERSPAN_INDEX
])
1886 parms
->index
= nla_get_u32(data
[IFLA_GRE_ERSPAN_INDEX
]);
1887 } else if (parms
->erspan_ver
== 2) {
1888 if (data
[IFLA_GRE_ERSPAN_DIR
])
1889 parms
->dir
= nla_get_u8(data
[IFLA_GRE_ERSPAN_DIR
]);
1890 if (data
[IFLA_GRE_ERSPAN_HWID
])
1891 parms
->hwid
= nla_get_u16(data
[IFLA_GRE_ERSPAN_HWID
]);
1895 static int ip6gre_tap_init(struct net_device
*dev
)
1899 ret
= ip6gre_tunnel_init_common(dev
);
1903 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
1908 static const struct net_device_ops ip6gre_tap_netdev_ops
= {
1909 .ndo_init
= ip6gre_tap_init
,
1910 .ndo_uninit
= ip6gre_tunnel_uninit
,
1911 .ndo_start_xmit
= ip6gre_tunnel_xmit
,
1912 .ndo_set_mac_address
= eth_mac_addr
,
1913 .ndo_validate_addr
= eth_validate_addr
,
1914 #ifdef HAVE_RHEL7_MAX_MTU
1915 .ndo_size
= sizeof(struct net_device_ops
),
1916 .extended
.ndo_change_mtu
= ip6_tnl_change_mtu
,
1918 .ndo_change_mtu
= ip6_tnl_change_mtu
,
1920 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1921 #ifdef HAVE_NDO_GET_IFLINK
1922 .ndo_get_iflink
= ip6_tnl_get_iflink
,
1926 static int ip6erspan_calc_hlen(struct ip6_tnl
*tunnel
)
1930 tunnel
->tun_hlen
= 8;
1931 tunnel
->hlen
= tunnel
->tun_hlen
+ tunnel
->encap_hlen
+
1932 erspan_hdr_len(tunnel
->parms
.erspan_ver
);
1934 t_hlen
= tunnel
->hlen
+ sizeof(struct ipv6hdr
);
1935 tunnel
->dev
->hard_header_len
= LL_MAX_HEADER
+ t_hlen
;
1939 static int ip6erspan_tap_init(struct net_device
*dev
)
1941 struct ip6_tnl
*tunnel
;
1945 tunnel
= netdev_priv(dev
);
1948 tunnel
->net
= dev_net(dev
);
1949 strcpy(tunnel
->parms
.name
, dev
->name
);
1951 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
1955 ret
= dst_cache_init(&tunnel
->dst_cache
, GFP_KERNEL
);
1957 free_percpu(dev
->tstats
);
1962 t_hlen
= ip6erspan_calc_hlen(tunnel
);
1963 dev
->mtu
= ETH_DATA_LEN
- t_hlen
;
1964 if (dev
->type
== ARPHRD_ETHER
)
1965 dev
->mtu
-= ETH_HLEN
;
1966 if (!(tunnel
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1969 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
1970 tunnel
= netdev_priv(dev
);
1971 ip6erspan_tnl_link_config(tunnel
, 1);
1976 static const struct net_device_ops ip6erspan_netdev_ops
= {
1977 .ndo_init
= ip6erspan_tap_init
,
1978 .ndo_uninit
= ip6erspan_tunnel_uninit
,
1979 .ndo_start_xmit
= ip6erspan_tunnel_xmit
,
1980 .ndo_set_mac_address
= eth_mac_addr
,
1981 .ndo_validate_addr
= eth_validate_addr
,
1982 #ifdef HAVE_RHEL7_MAX_MTU
1983 .ndo_size
= sizeof(struct net_device_ops
),
1984 .extended
.ndo_change_mtu
= ip6_tnl_change_mtu
,
1986 .ndo_change_mtu
= ip6_tnl_change_mtu
,
1988 .ndo_get_stats64
= ip_tunnel_get_stats64
,
1989 #ifdef HAVE_NDO_GET_IFLINK
1990 .ndo_get_iflink
= ip6_tnl_get_iflink
,
1994 static void ip6gre_tap_setup(struct net_device
*dev
)
1998 #ifdef HAVE_NET_DEVICE_MAX_MTU
2001 dev
->netdev_ops
= &ip6gre_tap_netdev_ops
;
2002 #ifndef HAVE_NEEDS_FREE_NETDEV
2003 dev
->destructor
= free_netdev
;
2005 dev
->needs_free_netdev
= true;
2006 dev
->priv_destructor
= ip6gre_dev_free
;
2009 dev
->features
|= NETIF_F_NETNS_LOCAL
;
2010 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
2011 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2012 netif_keep_dst(dev
);
2015 static bool ip6gre_netlink_encap_parms(struct nlattr
*data
[],
2016 struct ip_tunnel_encap
*ipencap
)
2020 memset(ipencap
, 0, sizeof(*ipencap
));
2025 if (data
[IFLA_GRE_ENCAP_TYPE
]) {
2027 ipencap
->type
= nla_get_u16(data
[IFLA_GRE_ENCAP_TYPE
]);
2030 if (data
[IFLA_GRE_ENCAP_FLAGS
]) {
2032 ipencap
->flags
= nla_get_u16(data
[IFLA_GRE_ENCAP_FLAGS
]);
2035 if (data
[IFLA_GRE_ENCAP_SPORT
]) {
2037 ipencap
->sport
= nla_get_be16(data
[IFLA_GRE_ENCAP_SPORT
]);
2040 if (data
[IFLA_GRE_ENCAP_DPORT
]) {
2042 ipencap
->dport
= nla_get_be16(data
[IFLA_GRE_ENCAP_DPORT
]);
2048 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2049 static int rpl_ip6gre_newlink_common(struct net
*src_net
, struct net_device
*dev
,
2050 struct nlattr
*tb
[], struct nlattr
*data
[],
2051 struct netlink_ext_ack
*extack
)
2053 static int rpl_ip6gre_newlink_common(struct net
*src_net
, struct net_device
*dev
,
2054 struct nlattr
*tb
[], struct nlattr
*data
[])
2058 struct ip_tunnel_encap ipencap
;
2061 nt
= netdev_priv(dev
);
2063 if (ip6gre_netlink_encap_parms(data
, &ipencap
)) {
2064 int err
= ip6_tnl_encap_setup(nt
, &ipencap
);
2070 if (dev
->type
== ARPHRD_ETHER
&& !tb
[IFLA_ADDRESS
])
2071 eth_hw_addr_random(dev
);
2074 nt
->net
= dev_net(dev
);
2076 err
= register_netdevice(dev
);
2081 ip6_tnl_change_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
2088 #define ip6gre_newlink_common rpl_ip6gre_newlink_common
2090 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2091 static int rpl_ip6gre_newlink(struct net
*src_net
, struct net_device
*dev
,
2092 struct nlattr
*tb
[], struct nlattr
*data
[],
2093 struct netlink_ext_ack
*extack
)
2095 static int rpl_ip6gre_newlink(struct net
*src_net
, struct net_device
*dev
,
2096 struct nlattr
*tb
[], struct nlattr
*data
[])
2099 struct ip6_tnl
*nt
= netdev_priv(dev
);
2100 struct net
*net
= dev_net(dev
);
2101 struct ip6gre_net
*ign
;
2104 ip6gre_netlink_parms(data
, &nt
->parms
);
2105 ign
= net_generic(net
, ip6gre_net_id
);
2107 if (nt
->parms
.collect_md
) {
2108 if (rtnl_dereference(ign
->collect_md_tun
))
2111 if (ip6gre_tunnel_find(net
, &nt
->parms
, dev
->type
))
2115 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2116 err
= ip6gre_newlink_common(src_net
, dev
, tb
, data
, extack
);
2118 err
= ip6gre_newlink_common(src_net
, dev
, tb
, data
);
2121 ip6gre_tnl_link_config(nt
, !tb
[IFLA_MTU
]);
2122 ip6gre_tunnel_link_md(ign
, nt
);
2123 ip6gre_tunnel_link(net_generic(net
, ip6gre_net_id
), nt
);
2128 #define ip6gre_newlink rpl_ip6gre_newlink
2130 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2131 static struct ip6_tnl
*
2132 rpl_ip6gre_changelink_common(struct net_device
*dev
, struct nlattr
*tb
[],
2133 struct nlattr
*data
[], struct __ip6_tnl_parm
*p_p
,
2134 struct netlink_ext_ack
*extack
)
2136 static struct ip6_tnl
*
2137 rpl_ip6gre_changelink_common(struct net_device
*dev
, struct nlattr
*tb
[],
2138 struct nlattr
*data
[], struct __ip6_tnl_parm
*p_p
)
2141 struct ip6_tnl
*t
, *nt
= netdev_priv(dev
);
2142 struct net
*net
= nt
->net
;
2143 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
2144 struct ip_tunnel_encap ipencap
;
2146 if (dev
== ign
->fb_tunnel_dev
)
2147 return ERR_PTR(-EINVAL
);
2149 if (ip6gre_netlink_encap_parms(data
, &ipencap
)) {
2150 int err
= ip6_tnl_encap_setup(nt
, &ipencap
);
2153 return ERR_PTR(err
);
2156 ip6gre_netlink_parms(data
, p_p
);
2158 t
= ip6gre_tunnel_locate(net
, p_p
, 0);
2162 return ERR_PTR(-EEXIST
);
2169 #define ip6gre_changelink_common rpl_ip6gre_changelink_common
2171 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2172 static int rpl_ip6gre_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
2173 struct nlattr
*data
[],
2174 struct netlink_ext_ack
*extack
)
2176 static int rpl_ip6gre_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
2177 struct nlattr
*data
[])
2180 struct ip6gre_net
*ign
= net_generic(dev_net(dev
), ip6gre_net_id
);
2181 struct __ip6_tnl_parm p
;
2184 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2185 t
= ip6gre_changelink_common(dev
, tb
, data
, &p
, extack
);
2187 t
= ip6gre_changelink_common(dev
, tb
, data
, &p
);
2192 ip6gre_tunnel_unlink_md(ign
, t
);
2193 ip6gre_tunnel_unlink(ign
, t
);
2194 ip6gre_tnl_change(t
, &p
, !tb
[IFLA_MTU
]);
2195 ip6gre_tunnel_link_md(ign
, t
);
2196 ip6gre_tunnel_link(ign
, t
);
2199 #define ip6gre_changelink rpl_ip6gre_changelink
2201 static void ip6gre_dellink(struct net_device
*dev
, struct list_head
*head
)
2203 struct net
*net
= dev_net(dev
);
2204 struct ip6gre_net
*ign
= net_generic(net
, ip6gre_net_id
);
2206 if (dev
!= ign
->fb_tunnel_dev
)
2207 unregister_netdevice_queue(dev
, head
);
2210 static size_t ip6gre_get_size(const struct net_device
*dev
)
2215 /* IFLA_GRE_IFLAGS */
2217 /* IFLA_GRE_OFLAGS */
2223 /* IFLA_GRE_LOCAL */
2224 nla_total_size(sizeof(struct in6_addr
)) +
2225 /* IFLA_GRE_REMOTE */
2226 nla_total_size(sizeof(struct in6_addr
)) +
2229 /* IFLA_GRE_ENCAP_LIMIT */
2231 /* IFLA_GRE_FLOWINFO */
2233 /* IFLA_GRE_FLAGS */
2235 /* IFLA_GRE_ENCAP_TYPE */
2237 /* IFLA_GRE_ENCAP_FLAGS */
2239 /* IFLA_GRE_ENCAP_SPORT */
2241 /* IFLA_GRE_ENCAP_DPORT */
2243 /* IFLA_GRE_COLLECT_METADATA */
2245 /* IFLA_GRE_FWMARK */
2247 /* IFLA_GRE_ERSPAN_INDEX */
2252 static int ip6gre_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
2254 struct ip6_tnl
*t
= netdev_priv(dev
);
2255 struct __ip6_tnl_parm
*p
= &t
->parms
;
2257 if (nla_put_u32(skb
, IFLA_GRE_LINK
, p
->link
) ||
2258 nla_put_be16(skb
, IFLA_GRE_IFLAGS
,
2259 gre_tnl_flags_to_gre_flags(p
->i_flags
)) ||
2260 nla_put_be16(skb
, IFLA_GRE_OFLAGS
,
2261 gre_tnl_flags_to_gre_flags(p
->o_flags
)) ||
2262 nla_put_be32(skb
, IFLA_GRE_IKEY
, p
->i_key
) ||
2263 nla_put_be32(skb
, IFLA_GRE_OKEY
, p
->o_key
) ||
2264 nla_put_in6_addr(skb
, IFLA_GRE_LOCAL
, &p
->laddr
) ||
2265 nla_put_in6_addr(skb
, IFLA_GRE_REMOTE
, &p
->raddr
) ||
2266 nla_put_u8(skb
, IFLA_GRE_TTL
, p
->hop_limit
) ||
2267 nla_put_u8(skb
, IFLA_GRE_ENCAP_LIMIT
, p
->encap_limit
) ||
2268 nla_put_be32(skb
, IFLA_GRE_FLOWINFO
, p
->flowinfo
) ||
2269 nla_put_u32(skb
, IFLA_GRE_FLAGS
, p
->flags
) ||
2270 nla_put_u32(skb
, IFLA_GRE_FWMARK
, p
->fwmark
) ||
2271 nla_put_u32(skb
, IFLA_GRE_ERSPAN_INDEX
, p
->index
))
2272 goto nla_put_failure
;
2274 if (nla_put_u16(skb
, IFLA_GRE_ENCAP_TYPE
,
2276 nla_put_be16(skb
, IFLA_GRE_ENCAP_SPORT
,
2278 nla_put_be16(skb
, IFLA_GRE_ENCAP_DPORT
,
2280 nla_put_u16(skb
, IFLA_GRE_ENCAP_FLAGS
,
2282 goto nla_put_failure
;
2284 if (p
->collect_md
) {
2285 if (nla_put_flag(skb
, IFLA_GRE_COLLECT_METADATA
))
2286 goto nla_put_failure
;
2289 if (nla_put_u8(skb
, IFLA_GRE_ERSPAN_VER
, p
->erspan_ver
))
2290 goto nla_put_failure
;
2292 if (p
->erspan_ver
== 1) {
2293 if (nla_put_u32(skb
, IFLA_GRE_ERSPAN_INDEX
, p
->index
))
2294 goto nla_put_failure
;
2295 } else if (p
->erspan_ver
== 2) {
2296 if (nla_put_u8(skb
, IFLA_GRE_ERSPAN_DIR
, p
->dir
))
2297 goto nla_put_failure
;
2298 if (nla_put_u16(skb
, IFLA_GRE_ERSPAN_HWID
, p
->hwid
))
2299 goto nla_put_failure
;
2308 static const struct nla_policy ip6gre_policy
[RPL_IFLA_GRE_MAX
+ 1] = {
2309 [IFLA_GRE_LINK
] = { .type
= NLA_U32
},
2310 [IFLA_GRE_IFLAGS
] = { .type
= NLA_U16
},
2311 [IFLA_GRE_OFLAGS
] = { .type
= NLA_U16
},
2312 [IFLA_GRE_IKEY
] = { .type
= NLA_U32
},
2313 [IFLA_GRE_OKEY
] = { .type
= NLA_U32
},
2314 [IFLA_GRE_LOCAL
] = { .len
= FIELD_SIZEOF(struct ipv6hdr
, saddr
) },
2315 [IFLA_GRE_REMOTE
] = { .len
= FIELD_SIZEOF(struct ipv6hdr
, daddr
) },
2316 [IFLA_GRE_TTL
] = { .type
= NLA_U8
},
2317 [IFLA_GRE_ENCAP_LIMIT
] = { .type
= NLA_U8
},
2318 [IFLA_GRE_FLOWINFO
] = { .type
= NLA_U32
},
2319 [IFLA_GRE_FLAGS
] = { .type
= NLA_U32
},
2320 [IFLA_GRE_ENCAP_TYPE
] = { .type
= NLA_U16
},
2321 [IFLA_GRE_ENCAP_FLAGS
] = { .type
= NLA_U16
},
2322 [IFLA_GRE_ENCAP_SPORT
] = { .type
= NLA_U16
},
2323 [IFLA_GRE_ENCAP_DPORT
] = { .type
= NLA_U16
},
2324 [IFLA_GRE_COLLECT_METADATA
] = { .type
= NLA_FLAG
},
2325 [IFLA_GRE_FWMARK
] = { .type
= NLA_U32
},
2326 [IFLA_GRE_ERSPAN_INDEX
] = { .type
= NLA_U32
},
2327 [IFLA_GRE_ERSPAN_VER
] = { .type
= NLA_U8
},
2328 [IFLA_GRE_ERSPAN_DIR
] = { .type
= NLA_U8
},
2329 [IFLA_GRE_ERSPAN_HWID
] = { .type
= NLA_U16
},
2332 static void ip6erspan_tap_setup(struct net_device
*dev
)
2336 dev
->netdev_ops
= &ip6erspan_netdev_ops
;
2337 #ifndef HAVE_NEEDS_FREE_NETDEV
2338 dev
->destructor
= free_netdev
;
2340 dev
->needs_free_netdev
= true;
2341 dev
->priv_destructor
= ip6gre_dev_free
;
2344 dev
->features
|= NETIF_F_NETNS_LOCAL
;
2345 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
2346 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
2347 netif_keep_dst(dev
);
2350 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2351 static int rpl_ip6erspan_newlink(struct net
*src_net
, struct net_device
*dev
,
2352 struct nlattr
*tb
[], struct nlattr
*data
[],
2353 struct netlink_ext_ack
*extack
)
2355 static int rpl_ip6erspan_newlink(struct net
*src_net
, struct net_device
*dev
,
2356 struct nlattr
*tb
[], struct nlattr
*data
[])
2359 struct ip6_tnl
*nt
= netdev_priv(dev
);
2360 struct net
*net
= dev_net(dev
);
2361 struct ip6gre_net
*ign
;
2364 ip6gre_netlink_parms(data
, &nt
->parms
);
2365 ign
= net_generic(net
, ip6gre_net_id
);
2367 if (nt
->parms
.collect_md
) {
2368 if (rtnl_dereference(ign
->collect_md_tun_erspan
))
2371 if (ip6gre_tunnel_find(net
, &nt
->parms
, dev
->type
))
2375 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2376 err
= ip6gre_newlink_common(src_net
, dev
, tb
, data
, extack
);
2378 err
= ip6gre_newlink_common(src_net
, dev
, tb
, data
);
2381 ip6erspan_tnl_link_config(nt
, !tb
[IFLA_MTU
]);
2382 ip6erspan_tunnel_link_md(ign
, nt
);
2383 ip6gre_tunnel_link(net_generic(net
, ip6gre_net_id
), nt
);
2387 #define ip6erspan_newlink rpl_ip6erspan_newlink
2389 static void ip6erspan_tnl_link_config(struct ip6_tnl
*t
, int set_mtu
)
2391 ip6gre_tnl_link_config_common(t
);
2392 ip6gre_tnl_link_config_route(t
, set_mtu
, ip6erspan_calc_hlen(t
));
2395 static int ip6erspan_tnl_change(struct ip6_tnl
*t
,
2396 const struct __ip6_tnl_parm
*p
, int set_mtu
)
2398 ip6gre_tnl_copy_tnl_parm(t
, p
);
2399 ip6erspan_tnl_link_config(t
, set_mtu
);
2403 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2404 static int rpl_ip6erspan_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
2405 struct nlattr
*data
[],
2406 struct netlink_ext_ack
*extack
)
2408 static int rpl_ip6erspan_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
2409 struct nlattr
*data
[])
2412 struct ip6gre_net
*ign
= net_generic(dev_net(dev
), ip6gre_net_id
);
2413 struct __ip6_tnl_parm p
;
2415 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2416 t
= ip6gre_changelink_common(dev
, tb
, data
, &p
, extack
);
2418 t
= ip6gre_changelink_common(dev
, tb
, data
, &p
);
2423 ip6gre_tunnel_unlink_md(ign
, t
);
2424 ip6gre_tunnel_unlink(ign
, t
);
2425 ip6erspan_tnl_change(t
, &p
, !tb
[IFLA_MTU
]);
2426 ip6erspan_tunnel_link_md(ign
, t
);
2427 ip6gre_tunnel_link(ign
, t
);
2430 #define ip6erspan_changelink rpl_ip6erspan_changelink
2432 static struct rtnl_link_ops ip6gre_link_ops __read_mostly
= {
2434 .maxtype
= RPL_IFLA_GRE_MAX
,
2435 .policy
= ip6gre_policy
,
2436 .priv_size
= sizeof(struct ip6_tnl
),
2437 .setup
= ip6gre_tunnel_setup
,
2438 .validate
= ip6gre_tunnel_validate
,
2439 .newlink
= ip6gre_newlink
,
2440 .changelink
= ip6gre_changelink
,
2441 .dellink
= ip6gre_dellink
,
2442 .get_size
= ip6gre_get_size
,
2443 .fill_info
= ip6gre_fill_info
,
2444 #ifdef HAVE_GET_LINK_NET
2445 .get_link_net
= ip6_tnl_get_link_net
,
2449 static struct rtnl_link_ops ip6gre_tap_ops __read_mostly
= {
2450 .kind
= "ip6gretap",
2451 .maxtype
= RPL_IFLA_GRE_MAX
,
2452 .policy
= ip6gre_policy
,
2453 .priv_size
= sizeof(struct ip6_tnl
),
2454 .setup
= ip6gre_tap_setup
,
2455 .validate
= ip6gre_tap_validate
,
2456 .newlink
= ip6gre_newlink
,
2457 .changelink
= ip6gre_changelink
,
2458 .dellink
= ip6gre_dellink
,
2459 .get_size
= ip6gre_get_size
,
2460 .fill_info
= ip6gre_fill_info
,
2461 #ifdef HAVE_GET_LINK_NET
2462 .get_link_net
= ip6_tnl_get_link_net
,
2466 static struct rtnl_link_ops ip6erspan_tap_ops __read_mostly
= {
2467 .kind
= "ip6erspan",
2468 .maxtype
= RPL_IFLA_GRE_MAX
,
2469 .policy
= ip6gre_policy
,
2470 .priv_size
= sizeof(struct ip6_tnl
),
2471 .setup
= ip6erspan_tap_setup
,
2472 .validate
= ip6erspan_tap_validate
,
2473 .newlink
= ip6erspan_newlink
,
2474 .changelink
= ip6erspan_changelink
,
2475 .dellink
= ip6gre_dellink
,
2476 .get_size
= ip6gre_get_size
,
2477 .fill_info
= ip6gre_fill_info
,
2478 #ifdef HAVE_GET_LINK_NET
2479 .get_link_net
= ip6_tnl_get_link_net
,
2483 struct net_device
*ip6erspan_fb_dev_create(struct net
*net
, const char *name
,
2484 u8 name_assign_type
)
2486 struct nlattr
*tb
[IFLA_MAX
+ 1];
2487 struct net_device
*dev
;
2488 LIST_HEAD(list_kill
);
2492 memset(&tb
, 0, sizeof(tb
));
2494 dev
= rtnl_create_link(net
, (char *)name
, name_assign_type
,
2495 &ip6erspan_tap_ops
, tb
);
2499 t
= netdev_priv(dev
);
2500 t
->parms
.collect_md
= true;
2502 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2503 err
= ip6erspan_newlink(net
, dev
, tb
, NULL
, NULL
);
2505 err
= ip6erspan_newlink(net
, dev
, tb
, NULL
);
2509 return ERR_PTR(err
);
2512 /* openvswitch users expect packet sizes to be unrestricted,
2513 * so set the largest MTU we can.
2515 err
= ip6_tnl_change_mtu(dev
, 64000);
2521 ip6gre_dellink(dev
, &list_kill
);
2522 unregister_netdevice_many(&list_kill
);
2523 return ERR_PTR(err
);
2526 static struct vport_ops ovs_erspan6_vport_ops
;
2528 static struct vport
*erspan6_tnl_create(const struct vport_parms
*parms
)
2530 struct net
*net
= ovs_dp_get_net(parms
->dp
);
2531 struct net_device
*dev
;
2532 struct vport
*vport
;
2535 vport
= ovs_vport_alloc(0, &ovs_erspan6_vport_ops
, parms
);
2540 dev
= ip6erspan_fb_dev_create(net
, parms
->name
, NET_NAME_USER
);
2543 ovs_vport_free(vport
);
2544 return ERR_CAST(dev
);
2547 err
= dev_change_flags(dev
, dev
->flags
| IFF_UP
, NULL
);
2549 rtnl_delete_link(dev
);
2551 ovs_vport_free(vport
);
2552 return ERR_PTR(err
);
2559 static struct vport
*erspan6_create(const struct vport_parms
*parms
)
2561 struct vport
*vport
;
2563 vport
= erspan6_tnl_create(parms
);
2567 return ovs_netdev_link(vport
, parms
->name
);
2570 #ifndef OVS_VPORT_TYPE_IP6ERSPAN
2571 /* Just until integration */
2572 #define OVS_VPORT_TYPE_IP6ERSPAN 108
2574 static struct vport_ops ovs_erspan6_vport_ops
= {
2575 .type
= OVS_VPORT_TYPE_IP6ERSPAN
,
2576 .create
= erspan6_create
,
2577 .send
= __ip6erspan_tunnel_xmit
,
2578 #ifndef USE_UPSTREAM_TUNNEL
2579 .fill_metadata_dst
= gre_fill_metadata_dst
,
2581 .destroy
= ovs_netdev_tunnel_destroy
,
2584 struct net_device
*ip6gre_fb_dev_create(struct net
*net
, const char *name
,
2585 u8 name_assign_type
)
2587 struct nlattr
*tb
[IFLA_MAX
+ 1];
2588 struct net_device
*dev
;
2589 LIST_HEAD(list_kill
);
2593 memset(&tb
, 0, sizeof(tb
));
2595 dev
= rtnl_create_link(net
, (char *)name
, name_assign_type
,
2596 &ip6gre_tap_ops
, tb
);
2600 t
= netdev_priv(dev
);
2601 t
->parms
.collect_md
= true;
2603 #ifdef HAVE_EXT_ACK_IN_RTNL_LINKOPS
2604 err
= ip6gre_newlink(net
, dev
, tb
, NULL
, NULL
);
2606 err
= ip6gre_newlink(net
, dev
, tb
, NULL
);
2610 return ERR_PTR(err
);
2613 /* openvswitch users expect packet sizes to be unrestricted,
2614 * so set the largest MTU we can.
2616 err
= ip6_tnl_change_mtu(dev
, 64000);
2622 ip6gre_dellink(dev
, &list_kill
);
2623 unregister_netdevice_many(&list_kill
);
2624 return ERR_PTR(err
);
2627 static struct vport_ops ovs_ip6gre_vport_ops
;
2629 static struct vport
*ip6gre_tnl_create(const struct vport_parms
*parms
)
2631 struct net
*net
= ovs_dp_get_net(parms
->dp
);
2632 struct net_device
*dev
;
2633 struct vport
*vport
;
2636 vport
= ovs_vport_alloc(0, &ovs_ip6gre_vport_ops
, parms
);
2641 dev
= ip6gre_fb_dev_create(net
, parms
->name
, NET_NAME_USER
);
2644 ovs_vport_free(vport
);
2645 return ERR_CAST(dev
);
2648 err
= dev_change_flags(dev
, dev
->flags
| IFF_UP
, NULL
);
2650 rtnl_delete_link(dev
);
2652 ovs_vport_free(vport
);
2653 return ERR_PTR(err
);
2660 static struct vport
*ip6gre_create(const struct vport_parms
*parms
)
2662 struct vport
*vport
;
2664 vport
= ip6gre_tnl_create(parms
);
2668 return ovs_netdev_link(vport
, parms
->name
);
2671 static struct vport_ops ovs_ip6gre_vport_ops
= {
2672 .type
= OVS_VPORT_TYPE_IP6GRE
,
2673 .create
= ip6gre_create
,
2674 .send
= __ip6gre_tunnel_xmit
,
2675 #ifndef USE_UPSTREAM_TUNNEL
2676 .fill_metadata_dst
= gre_fill_metadata_dst
,
2678 .destroy
= ovs_netdev_tunnel_destroy
,
2683 * And now the modules code and kernel interface.
2686 int rpl_ip6gre_init(void)
2690 err
= register_pernet_device(&ip6gre_net_ops
);
2693 goto ip6_gre_loaded
;
2698 err
= inet6_add_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
2700 pr_info("%s: can't add protocol\n", __func__
);
2701 unregister_pernet_device(&ip6gre_net_ops
);
2703 * inet6_add_protocol will return a -1 if it fails
2704 * to grab the pointer but the vport initialization
2705 * expects a return value of -EEXIST. Set err to
2706 * -EEXIST here to ensure proper handling.
2709 goto ip6_gre_loaded
;
2712 pr_info("GRE over IPv6 tunneling driver\n");
2713 ovs_vport_ops_register(&ovs_ip6gre_vport_ops
);
2714 ovs_vport_ops_register(&ovs_erspan6_vport_ops
);
2718 /* Since IPv6 GRE only allows single receiver to be registerd,
2719 * we skip here so only transmit works, see:
2721 * commit f9242b6b28d61295f2bf7e8adfb1060b382e5381
2722 * Author: David S. Miller <davem@davemloft.net>
2723 * Date: Tue Jun 19 18:56:21 2012 -0700
2725 * inet: Sanitize inet{,6} protocol demux.
2727 * OVS GRE receive part is disabled.
2729 pr_info("GRE TX only over IPv6 tunneling driver\n");
2730 ip6_gre_loaded
= true;
2731 ovs_vport_ops_register(&ovs_ip6gre_vport_ops
);
2732 ovs_vport_ops_register(&ovs_erspan6_vport_ops
);
2737 void rpl_ip6gre_fini(void)
2739 ovs_vport_ops_unregister(&ovs_erspan6_vport_ops
);
2740 ovs_vport_ops_unregister(&ovs_ip6gre_vport_ops
);
2741 if (!ip6_gre_loaded
) {
2742 inet6_del_protocol(&ip6gre_protocol
, IPPROTO_GRE
);
2743 unregister_pernet_device(&ip6gre_net_ops
);
2746 #endif /* USE_UPSTREAM_TUNNEL */