1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _NET_IP6_TUNNEL_H
31da177e4SLinus Torvalds #define _NET_IP6_TUNNEL_H
41da177e4SLinus Torvalds
51da177e4SLinus Torvalds #include <linux/ipv6.h>
61da177e4SLinus Torvalds #include <linux/netdevice.h>
7c5441932SPravin B Shelar #include <linux/if_tunnel.h>
81da177e4SLinus Torvalds #include <linux/ip6_tunnel.h>
9039f5062SPravin B Shelar #include <net/ip_tunnels.h>
10607f725fSPaolo Abeni #include <net/dst_cache.h>
111da177e4SLinus Torvalds
12c12b395aSxeb@mail.ru #define IP6TUNNEL_ERR_TIMEO (30*HZ)
13c12b395aSxeb@mail.ru
141da177e4SLinus Torvalds /* capable of sending packets */
151da177e4SLinus Torvalds #define IP6_TNL_F_CAP_XMIT 0x10000
161da177e4SLinus Torvalds /* capable of receiving packets */
171da177e4SLinus Torvalds #define IP6_TNL_F_CAP_RCV 0x20000
18d0087b29SVille Nuorvala /* determine capability on a per-packet basis */
19d0087b29SVille Nuorvala #define IP6_TNL_F_CAP_PER_PACKET 0x40000
201da177e4SLinus Torvalds
21c12b395aSxeb@mail.ru struct __ip6_tnl_parm {
22c12b395aSxeb@mail.ru char name[IFNAMSIZ]; /* name of tunnel device */
23c12b395aSxeb@mail.ru int link; /* ifindex of underlying L2 interface */
24c12b395aSxeb@mail.ru __u8 proto; /* tunnel protocol */
25c12b395aSxeb@mail.ru __u8 encap_limit; /* encapsulation limit for tunnel */
26c12b395aSxeb@mail.ru __u8 hop_limit; /* hop limit for tunnel */
278d79266bSAlexei Starovoitov bool collect_md;
28c12b395aSxeb@mail.ru __be32 flowinfo; /* traffic class and flowlabel for tunnel */
29c12b395aSxeb@mail.ru __u32 flags; /* tunnel flags */
30c12b395aSxeb@mail.ru struct in6_addr laddr; /* local tunnel end-point address */
31c12b395aSxeb@mail.ru struct in6_addr raddr; /* remote tunnel end-point address */
321da177e4SLinus Torvalds
33c12b395aSxeb@mail.ru __be16 i_flags;
34c12b395aSxeb@mail.ru __be16 o_flags;
35c12b395aSxeb@mail.ru __be32 i_key;
36c12b395aSxeb@mail.ru __be32 o_key;
370a473b82SCraig Gallek
380a473b82SCraig Gallek __u32 fwmark;
395a963eb6SWilliam Tu __u32 index; /* ERSPAN type II index */
4094d7d8f2SWilliam Tu __u8 erspan_ver; /* ERSPAN version */
4194d7d8f2SWilliam Tu __u8 dir; /* direction */
4294d7d8f2SWilliam Tu __u16 hwid; /* hwid */
43c12b395aSxeb@mail.ru };
44c12b395aSxeb@mail.ru
45c12b395aSxeb@mail.ru /* IPv6 tunnel */
461da177e4SLinus Torvalds struct ip6_tnl {
476f0bcf15SEric Dumazet struct ip6_tnl __rcu *next; /* next tunnel in list */
481da177e4SLinus Torvalds struct net_device *dev; /* virtual device associated with tunnel */
4956c1c779SEric Dumazet netdevice_tracker dev_tracker;
500bd87628SNicolas Dichtel struct net *net; /* netns for packet i/o */
51c12b395aSxeb@mail.ru struct __ip6_tnl_parm parms; /* tunnel configuration parameters */
521da177e4SLinus Torvalds struct flowi fl; /* flowi template for xmit */
53607f725fSPaolo Abeni struct dst_cache dst_cache; /* cached dst */
540d3c703aSTom Herbert struct gro_cells gro_cells;
55c12b395aSxeb@mail.ru
56c12b395aSxeb@mail.ru int err_count;
57c12b395aSxeb@mail.ru unsigned long err_time;
58c12b395aSxeb@mail.ru
59c12b395aSxeb@mail.ru /* These fields used only by GRE */
60c12b395aSxeb@mail.ru __u32 i_seqno; /* The last seen seqno */
61*31c417c9SPeilin Ye atomic_t o_seqno; /* The last output seqno */
6279ecb90eSTom Herbert int hlen; /* tun_hlen + encap_hlen */
6379ecb90eSTom Herbert int tun_hlen; /* Precalculated header length */
64058214a4STom Herbert int encap_hlen; /* Encap header length (FOU,GUE) */
65058214a4STom Herbert struct ip_tunnel_encap encap;
66c12b395aSxeb@mail.ru int mlink;
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds
69058214a4STom Herbert struct ip6_tnl_encap_ops {
70058214a4STom Herbert size_t (*encap_hlen)(struct ip_tunnel_encap *e);
71058214a4STom Herbert int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
72058214a4STom Herbert u8 *protocol, struct flowi6 *fl6);
73e7cc0824SStefano Brivio int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
74e7cc0824SStefano Brivio u8 type, u8 code, int offset, __be32 info);
75058214a4STom Herbert };
76058214a4STom Herbert
779791d8e7SArnd Bergmann #ifdef CONFIG_INET
789791d8e7SArnd Bergmann
79058214a4STom Herbert extern const struct ip6_tnl_encap_ops __rcu *
80058214a4STom Herbert ip6tun_encaps[MAX_IPTUN_ENCAP_OPS];
81058214a4STom Herbert
82058214a4STom Herbert int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
83058214a4STom Herbert unsigned int num);
84058214a4STom Herbert int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
85058214a4STom Herbert unsigned int num);
86058214a4STom Herbert int ip6_tnl_encap_setup(struct ip6_tnl *t,
87058214a4STom Herbert struct ip_tunnel_encap *ipencap);
88058214a4STom Herbert
ip6_encap_hlen(struct ip_tunnel_encap * e)89058214a4STom Herbert static inline int ip6_encap_hlen(struct ip_tunnel_encap *e)
90058214a4STom Herbert {
91058214a4STom Herbert const struct ip6_tnl_encap_ops *ops;
92058214a4STom Herbert int hlen = -EINVAL;
93058214a4STom Herbert
94058214a4STom Herbert if (e->type == TUNNEL_ENCAP_NONE)
95058214a4STom Herbert return 0;
96058214a4STom Herbert
97058214a4STom Herbert if (e->type >= MAX_IPTUN_ENCAP_OPS)
98058214a4STom Herbert return -EINVAL;
99058214a4STom Herbert
100058214a4STom Herbert rcu_read_lock();
101058214a4STom Herbert ops = rcu_dereference(ip6tun_encaps[e->type]);
102058214a4STom Herbert if (likely(ops && ops->encap_hlen))
103058214a4STom Herbert hlen = ops->encap_hlen(e);
104058214a4STom Herbert rcu_read_unlock();
105058214a4STom Herbert
106058214a4STom Herbert return hlen;
107058214a4STom Herbert }
108058214a4STom Herbert
ip6_tnl_encap(struct sk_buff * skb,struct ip6_tnl * t,u8 * protocol,struct flowi6 * fl6)109058214a4STom Herbert static inline int ip6_tnl_encap(struct sk_buff *skb, struct ip6_tnl *t,
110058214a4STom Herbert u8 *protocol, struct flowi6 *fl6)
111058214a4STom Herbert {
112058214a4STom Herbert const struct ip6_tnl_encap_ops *ops;
113058214a4STom Herbert int ret = -EINVAL;
114058214a4STom Herbert
115058214a4STom Herbert if (t->encap.type == TUNNEL_ENCAP_NONE)
116058214a4STom Herbert return 0;
117058214a4STom Herbert
118058214a4STom Herbert if (t->encap.type >= MAX_IPTUN_ENCAP_OPS)
119058214a4STom Herbert return -EINVAL;
120058214a4STom Herbert
121058214a4STom Herbert rcu_read_lock();
122058214a4STom Herbert ops = rcu_dereference(ip6tun_encaps[t->encap.type]);
123058214a4STom Herbert if (likely(ops && ops->build_header))
124058214a4STom Herbert ret = ops->build_header(skb, &t->encap, protocol, fl6);
125058214a4STom Herbert rcu_read_unlock();
126058214a4STom Herbert
127058214a4STom Herbert return ret;
128058214a4STom Herbert }
129058214a4STom Herbert
1301da177e4SLinus Torvalds /* Tunnel encapsulation limit destination sub-option */
1311da177e4SLinus Torvalds
1321da177e4SLinus Torvalds struct ipv6_tlv_tnl_enc_lim {
1331da177e4SLinus Torvalds __u8 type; /* type-code for option */
1341da177e4SLinus Torvalds __u8 length; /* option length */
1351da177e4SLinus Torvalds __u8 encap_limit; /* tunnel encapsulation limit */
136bc10502dSEric Dumazet } __packed;
1371da177e4SLinus Torvalds
138c12b395aSxeb@mail.ru int ip6_tnl_rcv_ctl(struct ip6_tnl *t, const struct in6_addr *laddr,
139c12b395aSxeb@mail.ru const struct in6_addr *raddr);
1400d3c703aSTom Herbert int ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
1410d3c703aSTom Herbert const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
1420d3c703aSTom Herbert bool log_ecn_error);
143d5005140SSteffen Klassert int ip6_tnl_xmit_ctl(struct ip6_tnl *t, const struct in6_addr *laddr,
144d5005140SSteffen Klassert const struct in6_addr *raddr);
1458eb30be0STom Herbert int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
1468eb30be0STom Herbert struct flowi6 *fl6, int encap_limit, __u32 *pmtu, __u8 proto);
147c12b395aSxeb@mail.ru __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw);
148c12b395aSxeb@mail.ru __u32 ip6_tnl_get_cap(struct ip6_tnl *t, const struct in6_addr *laddr,
149c12b395aSxeb@mail.ru const struct in6_addr *raddr);
1501728d4faSNicolas Dichtel struct net *ip6_tnl_get_link_net(const struct net_device *dev);
151ecf2c06aSNicolas Dichtel int ip6_tnl_get_iflink(const struct net_device *dev);
15279ecb90eSTom Herbert int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu);
153c12b395aSxeb@mail.ru
ip6tunnel_xmit(struct sock * sk,struct sk_buff * skb,struct net_device * dev)15479b16aadSDavid Miller static inline void ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb,
15579b16aadSDavid Miller struct net_device *dev)
156e8f72ea4SCong Wang {
157e8f72ea4SCong Wang int pkt_len, err;
158e8f72ea4SCong Wang
15923f4ffedSEli Cooper memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
16083cf9a25SNicolas Dichtel pkt_len = skb->len - skb_inner_network_offset(skb);
16133224b16SEric W. Biederman err = ip6_local_out(dev_net(skb_dst(skb)->dev), sk, skb);
1626f6a8622SXin Long
1636f6a8622SXin Long if (dev) {
164039f5062SPravin B Shelar if (unlikely(net_xmit_eval(err)))
165039f5062SPravin B Shelar pkt_len = -1;
166039f5062SPravin B Shelar iptunnel_xmit_stats(dev, pkt_len);
167e8f72ea4SCong Wang }
1686f6a8622SXin Long }
1691da177e4SLinus Torvalds #endif
1700efeff29SArnd Bergmann #endif
171