xref: /openbmc/linux/include/net/ip_tunnels.h (revision a8c5f90f)
1c5441932SPravin B Shelar #ifndef __NET_IP_TUNNELS_H
2c5441932SPravin B Shelar #define __NET_IP_TUNNELS_H 1
3c5441932SPravin B Shelar 
4c5441932SPravin B Shelar #include <linux/if_tunnel.h>
5c5441932SPravin B Shelar #include <linux/netdevice.h>
6c5441932SPravin B Shelar #include <linux/skbuff.h>
7c5441932SPravin B Shelar #include <linux/types.h>
8c5441932SPravin B Shelar #include <linux/u64_stats_sync.h>
9c5441932SPravin B Shelar #include <net/dsfield.h>
10c5441932SPravin B Shelar #include <net/gro_cells.h>
11c5441932SPravin B Shelar #include <net/inet_ecn.h>
12c5441932SPravin B Shelar #include <net/ip.h>
1356328486STom Herbert #include <net/netns/generic.h>
14c5441932SPravin B Shelar #include <net/rtnetlink.h>
15c5441932SPravin B Shelar 
16c5441932SPravin B Shelar #if IS_ENABLED(CONFIG_IPV6)
17c5441932SPravin B Shelar #include <net/ipv6.h>
18c5441932SPravin B Shelar #include <net/ip6_fib.h>
19c5441932SPravin B Shelar #include <net/ip6_route.h>
20c5441932SPravin B Shelar #endif
21c5441932SPravin B Shelar 
22c5441932SPravin B Shelar /* Keep error state on tunnel for 30 sec */
23c5441932SPravin B Shelar #define IPTUNNEL_ERR_TIMEO	(30*HZ)
24c5441932SPravin B Shelar 
25c5441932SPravin B Shelar /* 6rd prefix/relay information */
26c5441932SPravin B Shelar #ifdef CONFIG_IPV6_SIT_6RD
27c5441932SPravin B Shelar struct ip_tunnel_6rd_parm {
28c5441932SPravin B Shelar 	struct in6_addr		prefix;
29c5441932SPravin B Shelar 	__be32			relay_prefix;
30c5441932SPravin B Shelar 	u16			prefixlen;
31c5441932SPravin B Shelar 	u16			relay_prefixlen;
32c5441932SPravin B Shelar };
33c5441932SPravin B Shelar #endif
34c5441932SPravin B Shelar 
3556328486STom Herbert struct ip_tunnel_encap {
3656328486STom Herbert 	__u16			type;
3756328486STom Herbert 	__u16			flags;
3856328486STom Herbert 	__be16			sport;
3956328486STom Herbert 	__be16			dport;
4056328486STom Herbert };
4156328486STom Herbert 
42c5441932SPravin B Shelar struct ip_tunnel_prl_entry {
43c5441932SPravin B Shelar 	struct ip_tunnel_prl_entry __rcu *next;
44c5441932SPravin B Shelar 	__be32				addr;
45c5441932SPravin B Shelar 	u16				flags;
46c5441932SPravin B Shelar 	struct rcu_head			rcu_head;
47c5441932SPravin B Shelar };
48c5441932SPravin B Shelar 
499a4aa9afSTom Herbert struct ip_tunnel_dst {
509a4aa9afSTom Herbert 	struct dst_entry __rcu 		*dst;
5195cb5745SDmitry Popov 	__be32				 saddr;
529a4aa9afSTom Herbert };
539a4aa9afSTom Herbert 
54c5441932SPravin B Shelar struct ip_tunnel {
55c5441932SPravin B Shelar 	struct ip_tunnel __rcu	*next;
56c5441932SPravin B Shelar 	struct hlist_node hash_node;
57c5441932SPravin B Shelar 	struct net_device	*dev;
585e6700b3SNicolas Dichtel 	struct net		*net;	/* netns for packet i/o */
59c5441932SPravin B Shelar 
60c5441932SPravin B Shelar 	int		err_count;	/* Number of arrived ICMP errors */
61c5441932SPravin B Shelar 	unsigned long	err_time;	/* Time when the last ICMP error
62c5441932SPravin B Shelar 					 * arrived */
63c5441932SPravin B Shelar 
64c5441932SPravin B Shelar 	/* These four fields used only by GRE */
65c5441932SPravin B Shelar 	__u32		i_seqno;	/* The last seen seqno	*/
66c5441932SPravin B Shelar 	__u32		o_seqno;	/* The last output seqno */
6756328486STom Herbert 	int		tun_hlen;	/* Precalculated header length */
68c5441932SPravin B Shelar 	int		mlink;
69c5441932SPravin B Shelar 
709a4aa9afSTom Herbert 	struct ip_tunnel_dst __percpu *dst_cache;
717d442fabSTom Herbert 
72c5441932SPravin B Shelar 	struct ip_tunnel_parm parms;
73c5441932SPravin B Shelar 
7456328486STom Herbert 	int		encap_hlen;	/* Encap header length (FOU,GUE) */
7556328486STom Herbert 	struct ip_tunnel_encap encap;
7656328486STom Herbert 
7756328486STom Herbert 	int		hlen;		/* tun_hlen + encap_hlen */
7856328486STom Herbert 
79c5441932SPravin B Shelar 	/* for SIT */
80c5441932SPravin B Shelar #ifdef CONFIG_IPV6_SIT_6RD
81c5441932SPravin B Shelar 	struct ip_tunnel_6rd_parm ip6rd;
82c5441932SPravin B Shelar #endif
83c5441932SPravin B Shelar 	struct ip_tunnel_prl_entry __rcu *prl;	/* potential router list */
84c5441932SPravin B Shelar 	unsigned int		prl_count;	/* # of entries in PRL */
85c5441932SPravin B Shelar 	int			ip_tnl_net_id;
86c5441932SPravin B Shelar 	struct gro_cells	gro_cells;
87c5441932SPravin B Shelar };
88c5441932SPravin B Shelar 
89c5441932SPravin B Shelar #define TUNNEL_CSUM		__cpu_to_be16(0x01)
90c5441932SPravin B Shelar #define TUNNEL_ROUTING		__cpu_to_be16(0x02)
91c5441932SPravin B Shelar #define TUNNEL_KEY		__cpu_to_be16(0x04)
92c5441932SPravin B Shelar #define TUNNEL_SEQ		__cpu_to_be16(0x08)
93c5441932SPravin B Shelar #define TUNNEL_STRICT		__cpu_to_be16(0x10)
94c5441932SPravin B Shelar #define TUNNEL_REC		__cpu_to_be16(0x20)
95c5441932SPravin B Shelar #define TUNNEL_VERSION		__cpu_to_be16(0x40)
96c5441932SPravin B Shelar #define TUNNEL_NO_KEY		__cpu_to_be16(0x80)
979a628224SPravin B Shelar #define TUNNEL_DONT_FRAGMENT    __cpu_to_be16(0x0100)
980b5e8b8eSAndy Zhou #define TUNNEL_OAM		__cpu_to_be16(0x0200)
990b5e8b8eSAndy Zhou #define TUNNEL_CRIT_OPT		__cpu_to_be16(0x0400)
100f5796684SJesse Gross #define TUNNEL_OPTIONS_PRESENT	__cpu_to_be16(0x0800)
101c5441932SPravin B Shelar 
102c5441932SPravin B Shelar struct tnl_ptk_info {
103c5441932SPravin B Shelar 	__be16 flags;
104c5441932SPravin B Shelar 	__be16 proto;
105c5441932SPravin B Shelar 	__be32 key;
106c5441932SPravin B Shelar 	__be32 seq;
107c5441932SPravin B Shelar };
108c5441932SPravin B Shelar 
109c5441932SPravin B Shelar #define PACKET_RCVD	0
110c5441932SPravin B Shelar #define PACKET_REJECT	1
111c5441932SPravin B Shelar 
1126261d983Sstephen hemminger #define IP_TNL_HASH_BITS   7
113c5441932SPravin B Shelar #define IP_TNL_HASH_SIZE   (1 << IP_TNL_HASH_BITS)
114c5441932SPravin B Shelar 
115c5441932SPravin B Shelar struct ip_tunnel_net {
116c5441932SPravin B Shelar 	struct net_device *fb_tunnel_dev;
1176261d983Sstephen hemminger 	struct hlist_head tunnels[IP_TNL_HASH_SIZE];
118c5441932SPravin B Shelar };
119c5441932SPravin B Shelar 
120a8c5f90fSTom Herbert struct ip_tunnel_encap_ops {
121a8c5f90fSTom Herbert 	size_t (*encap_hlen)(struct ip_tunnel_encap *e);
122a8c5f90fSTom Herbert 	int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
123a8c5f90fSTom Herbert 			    u8 *protocol, struct flowi4 *fl4);
124a8c5f90fSTom Herbert };
125a8c5f90fSTom Herbert 
126a8c5f90fSTom Herbert #define MAX_IPTUN_ENCAP_OPS 8
127a8c5f90fSTom Herbert 
128a8c5f90fSTom Herbert extern const struct ip_tunnel_encap_ops __rcu *
129a8c5f90fSTom Herbert 		iptun_encaps[MAX_IPTUN_ENCAP_OPS];
130a8c5f90fSTom Herbert 
131a8c5f90fSTom Herbert int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
132a8c5f90fSTom Herbert 			    unsigned int num);
133a8c5f90fSTom Herbert int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
134a8c5f90fSTom Herbert 			    unsigned int num);
135a8c5f90fSTom Herbert 
1365243b6acSJesse Gross #ifdef CONFIG_INET
1375243b6acSJesse Gross 
138c5441932SPravin B Shelar int ip_tunnel_init(struct net_device *dev);
139c5441932SPravin B Shelar void ip_tunnel_uninit(struct net_device *dev);
140c5441932SPravin B Shelar void  ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
141d3b6f614SEric Dumazet int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
142c5441932SPravin B Shelar 		       struct rtnl_link_ops *ops, char *devname);
143c5441932SPravin B Shelar 
1446c742e71SNicolas Dichtel void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops);
145c5441932SPravin B Shelar 
146c5441932SPravin B Shelar void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
147bf3d6a8fSNicolas Dichtel 		    const struct iphdr *tnl_params, const u8 protocol);
148c5441932SPravin B Shelar int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
14956328486STom Herbert int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
15056328486STom Herbert 		    u8 *protocol, struct flowi4 *fl4);
151c5441932SPravin B Shelar int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
152c5441932SPravin B Shelar 
153c5441932SPravin B Shelar struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
154c5441932SPravin B Shelar 						struct rtnl_link_stats64 *tot);
155c5441932SPravin B Shelar struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
156c5441932SPravin B Shelar 				   int link, __be16 flags,
157c5441932SPravin B Shelar 				   __be32 remote, __be32 local,
158c5441932SPravin B Shelar 				   __be32 key);
159c5441932SPravin B Shelar 
160c5441932SPravin B Shelar int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
161c5441932SPravin B Shelar 		  const struct tnl_ptk_info *tpi, bool log_ecn_error);
162c5441932SPravin B Shelar int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
163c5441932SPravin B Shelar 			 struct ip_tunnel_parm *p);
164c5441932SPravin B Shelar int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
165c5441932SPravin B Shelar 		      struct ip_tunnel_parm *p);
166c5441932SPravin B Shelar void ip_tunnel_setup(struct net_device *dev, int net_id);
167cf71d2bcSNicolas Dichtel void ip_tunnel_dst_reset_all(struct ip_tunnel *t);
16856328486STom Herbert int ip_tunnel_encap_setup(struct ip_tunnel *t,
16956328486STom Herbert 			  struct ip_tunnel_encap *ipencap);
170c5441932SPravin B Shelar 
171c5441932SPravin B Shelar /* Extract dsfield from inner protocol */
172c5441932SPravin B Shelar static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
173c5441932SPravin B Shelar 				       const struct sk_buff *skb)
174c5441932SPravin B Shelar {
175c5441932SPravin B Shelar 	if (skb->protocol == htons(ETH_P_IP))
176c5441932SPravin B Shelar 		return iph->tos;
177c5441932SPravin B Shelar 	else if (skb->protocol == htons(ETH_P_IPV6))
178c5441932SPravin B Shelar 		return ipv6_get_dsfield((const struct ipv6hdr *)iph);
179c5441932SPravin B Shelar 	else
180c5441932SPravin B Shelar 		return 0;
181c5441932SPravin B Shelar }
182c5441932SPravin B Shelar 
183c5441932SPravin B Shelar /* Propogate ECN bits out */
184c5441932SPravin B Shelar static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
185c5441932SPravin B Shelar 				     const struct sk_buff *skb)
186c5441932SPravin B Shelar {
187c5441932SPravin B Shelar 	u8 inner = ip_tunnel_get_dsfield(iph, skb);
188c5441932SPravin B Shelar 
189c5441932SPravin B Shelar 	return INET_ECN_encapsulate(tos, inner);
190c5441932SPravin B Shelar }
191c5441932SPravin B Shelar 
1923d7b46cdSPravin B Shelar int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto);
193aad88724SEric Dumazet int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
1940e6fbc5bSPravin B Shelar 		  __be32 src, __be32 dst, __u8 proto,
195963a88b3SNicolas Dichtel 		  __u8 tos, __u8 ttl, __be16 df, bool xnet);
1960e6fbc5bSPravin B Shelar 
1972d26f0a3SEric Dumazet struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, bool gre_csum,
1982d26f0a3SEric Dumazet 					 int gso_type_mask);
1992d26f0a3SEric Dumazet 
2000e6fbc5bSPravin B Shelar static inline void iptunnel_xmit_stats(int err,
2010e6fbc5bSPravin B Shelar 				       struct net_device_stats *err_stats,
2028f84985fSLi RongQing 				       struct pcpu_sw_netstats __percpu *stats)
203c5441932SPravin B Shelar {
2040e6fbc5bSPravin B Shelar 	if (err > 0) {
2058f84985fSLi RongQing 		struct pcpu_sw_netstats *tstats = this_cpu_ptr(stats);
206c5441932SPravin B Shelar 
207c5441932SPravin B Shelar 		u64_stats_update_begin(&tstats->syncp);
2080e6fbc5bSPravin B Shelar 		tstats->tx_bytes += err;
209c5441932SPravin B Shelar 		tstats->tx_packets++;
210c5441932SPravin B Shelar 		u64_stats_update_end(&tstats->syncp);
2110e6fbc5bSPravin B Shelar 	} else if (err < 0) {
2120e6fbc5bSPravin B Shelar 		err_stats->tx_errors++;
2130e6fbc5bSPravin B Shelar 		err_stats->tx_aborted_errors++;
214c5441932SPravin B Shelar 	} else {
2150e6fbc5bSPravin B Shelar 		err_stats->tx_dropped++;
216c5441932SPravin B Shelar 	}
217c5441932SPravin B Shelar }
2185243b6acSJesse Gross 
2195243b6acSJesse Gross #endif /* CONFIG_INET */
2205243b6acSJesse Gross 
221c5441932SPravin B Shelar #endif /* __NET_IP_TUNNELS_H */
222