xref: /openbmc/linux/include/net/ip_tunnels.h (revision 3d7b46cd)
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>
13c5441932SPravin B Shelar #include <net/rtnetlink.h>
14c5441932SPravin B Shelar 
15c5441932SPravin B Shelar #if IS_ENABLED(CONFIG_IPV6)
16c5441932SPravin B Shelar #include <net/ipv6.h>
17c5441932SPravin B Shelar #include <net/ip6_fib.h>
18c5441932SPravin B Shelar #include <net/ip6_route.h>
19c5441932SPravin B Shelar #endif
20c5441932SPravin B Shelar 
21c5441932SPravin B Shelar /* Keep error state on tunnel for 30 sec */
22c5441932SPravin B Shelar #define IPTUNNEL_ERR_TIMEO	(30*HZ)
23c5441932SPravin B Shelar 
24c5441932SPravin B Shelar /* 6rd prefix/relay information */
25c5441932SPravin B Shelar #ifdef CONFIG_IPV6_SIT_6RD
26c5441932SPravin B Shelar struct ip_tunnel_6rd_parm {
27c5441932SPravin B Shelar 	struct in6_addr		prefix;
28c5441932SPravin B Shelar 	__be32			relay_prefix;
29c5441932SPravin B Shelar 	u16			prefixlen;
30c5441932SPravin B Shelar 	u16			relay_prefixlen;
31c5441932SPravin B Shelar };
32c5441932SPravin B Shelar #endif
33c5441932SPravin B Shelar 
34c5441932SPravin B Shelar struct ip_tunnel_prl_entry {
35c5441932SPravin B Shelar 	struct ip_tunnel_prl_entry __rcu *next;
36c5441932SPravin B Shelar 	__be32				addr;
37c5441932SPravin B Shelar 	u16				flags;
38c5441932SPravin B Shelar 	struct rcu_head			rcu_head;
39c5441932SPravin B Shelar };
40c5441932SPravin B Shelar 
41c5441932SPravin B Shelar struct ip_tunnel {
42c5441932SPravin B Shelar 	struct ip_tunnel __rcu	*next;
43c5441932SPravin B Shelar 	struct hlist_node hash_node;
44c5441932SPravin B Shelar 	struct net_device	*dev;
45c5441932SPravin B Shelar 
46c5441932SPravin B Shelar 	int		err_count;	/* Number of arrived ICMP errors */
47c5441932SPravin B Shelar 	unsigned long	err_time;	/* Time when the last ICMP error
48c5441932SPravin B Shelar 					 * arrived */
49c5441932SPravin B Shelar 
50c5441932SPravin B Shelar 	/* These four fields used only by GRE */
51c5441932SPravin B Shelar 	__u32		i_seqno;	/* The last seen seqno	*/
52c5441932SPravin B Shelar 	__u32		o_seqno;	/* The last output seqno */
53c5441932SPravin B Shelar 	int		hlen;		/* Precalculated header length */
54c5441932SPravin B Shelar 	int		mlink;
55c5441932SPravin B Shelar 
56c5441932SPravin B Shelar 	struct ip_tunnel_parm parms;
57c5441932SPravin B Shelar 
58c5441932SPravin B Shelar 	/* for SIT */
59c5441932SPravin B Shelar #ifdef CONFIG_IPV6_SIT_6RD
60c5441932SPravin B Shelar 	struct ip_tunnel_6rd_parm ip6rd;
61c5441932SPravin B Shelar #endif
62c5441932SPravin B Shelar 	struct ip_tunnel_prl_entry __rcu *prl;	/* potential router list */
63c5441932SPravin B Shelar 	unsigned int		prl_count;	/* # of entries in PRL */
64c5441932SPravin B Shelar 	int			ip_tnl_net_id;
65c5441932SPravin B Shelar 	struct gro_cells	gro_cells;
66c5441932SPravin B Shelar };
67c5441932SPravin B Shelar 
68c5441932SPravin B Shelar #define TUNNEL_CSUM	__cpu_to_be16(0x01)
69c5441932SPravin B Shelar #define TUNNEL_ROUTING	__cpu_to_be16(0x02)
70c5441932SPravin B Shelar #define TUNNEL_KEY	__cpu_to_be16(0x04)
71c5441932SPravin B Shelar #define TUNNEL_SEQ	__cpu_to_be16(0x08)
72c5441932SPravin B Shelar #define TUNNEL_STRICT	__cpu_to_be16(0x10)
73c5441932SPravin B Shelar #define TUNNEL_REC	__cpu_to_be16(0x20)
74c5441932SPravin B Shelar #define TUNNEL_VERSION	__cpu_to_be16(0x40)
75c5441932SPravin B Shelar #define TUNNEL_NO_KEY	__cpu_to_be16(0x80)
76c5441932SPravin B Shelar 
77c5441932SPravin B Shelar struct tnl_ptk_info {
78c5441932SPravin B Shelar 	__be16 flags;
79c5441932SPravin B Shelar 	__be16 proto;
80c5441932SPravin B Shelar 	__be32 key;
81c5441932SPravin B Shelar 	__be32 seq;
82c5441932SPravin B Shelar };
83c5441932SPravin B Shelar 
84c5441932SPravin B Shelar #define PACKET_RCVD	0
85c5441932SPravin B Shelar #define PACKET_REJECT	1
86c5441932SPravin B Shelar 
87c5441932SPravin B Shelar #define IP_TNL_HASH_BITS   10
88c5441932SPravin B Shelar #define IP_TNL_HASH_SIZE   (1 << IP_TNL_HASH_BITS)
89c5441932SPravin B Shelar 
90c5441932SPravin B Shelar struct ip_tunnel_net {
91c5441932SPravin B Shelar 	struct hlist_head *tunnels;
92c5441932SPravin B Shelar 	struct net_device *fb_tunnel_dev;
93c5441932SPravin B Shelar };
94c5441932SPravin B Shelar 
95c5441932SPravin B Shelar int ip_tunnel_init(struct net_device *dev);
96c5441932SPravin B Shelar void ip_tunnel_uninit(struct net_device *dev);
97c5441932SPravin B Shelar void  ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
98d3b6f614SEric Dumazet int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
99c5441932SPravin B Shelar 		       struct rtnl_link_ops *ops, char *devname);
100c5441932SPravin B Shelar 
101d3b6f614SEric Dumazet void ip_tunnel_delete_net(struct ip_tunnel_net *itn);
102c5441932SPravin B Shelar 
103c5441932SPravin B Shelar void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
104bf3d6a8fSNicolas Dichtel 		    const struct iphdr *tnl_params, const u8 protocol);
105c5441932SPravin B Shelar int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
106c5441932SPravin B Shelar int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
107c5441932SPravin B Shelar 
108c5441932SPravin B Shelar struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
109c5441932SPravin B Shelar 						struct rtnl_link_stats64 *tot);
110c5441932SPravin B Shelar struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
111c5441932SPravin B Shelar 				   int link, __be16 flags,
112c5441932SPravin B Shelar 				   __be32 remote, __be32 local,
113c5441932SPravin B Shelar 				   __be32 key);
114c5441932SPravin B Shelar 
115c5441932SPravin B Shelar int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
116c5441932SPravin B Shelar 		  const struct tnl_ptk_info *tpi, bool log_ecn_error);
117c5441932SPravin B Shelar int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
118c5441932SPravin B Shelar 			 struct ip_tunnel_parm *p);
119c5441932SPravin B Shelar int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
120c5441932SPravin B Shelar 		      struct ip_tunnel_parm *p);
121c5441932SPravin B Shelar void ip_tunnel_setup(struct net_device *dev, int net_id);
122c5441932SPravin B Shelar 
123c5441932SPravin B Shelar /* Extract dsfield from inner protocol */
124c5441932SPravin B Shelar static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
125c5441932SPravin B Shelar 				       const struct sk_buff *skb)
126c5441932SPravin B Shelar {
127c5441932SPravin B Shelar 	if (skb->protocol == htons(ETH_P_IP))
128c5441932SPravin B Shelar 		return iph->tos;
129c5441932SPravin B Shelar 	else if (skb->protocol == htons(ETH_P_IPV6))
130c5441932SPravin B Shelar 		return ipv6_get_dsfield((const struct ipv6hdr *)iph);
131c5441932SPravin B Shelar 	else
132c5441932SPravin B Shelar 		return 0;
133c5441932SPravin B Shelar }
134c5441932SPravin B Shelar 
135c5441932SPravin B Shelar /* Propogate ECN bits out */
136c5441932SPravin B Shelar static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
137c5441932SPravin B Shelar 				     const struct sk_buff *skb)
138c5441932SPravin B Shelar {
139c5441932SPravin B Shelar 	u8 inner = ip_tunnel_get_dsfield(iph, skb);
140c5441932SPravin B Shelar 
141c5441932SPravin B Shelar 	return INET_ECN_encapsulate(tos, inner);
142c5441932SPravin B Shelar }
143c5441932SPravin B Shelar 
144c5441932SPravin B Shelar static inline void tunnel_ip_select_ident(struct sk_buff *skb,
145c5441932SPravin B Shelar 					  const struct iphdr  *old_iph,
146c5441932SPravin B Shelar 					  struct dst_entry *dst)
147c5441932SPravin B Shelar {
148c5441932SPravin B Shelar 	struct iphdr *iph = ip_hdr(skb);
149c5441932SPravin B Shelar 
150c5441932SPravin B Shelar 	/* Use inner packet iph-id if possible. */
151c5441932SPravin B Shelar 	if (skb->protocol == htons(ETH_P_IP) && old_iph->id)
152c5441932SPravin B Shelar 		iph->id	= old_iph->id;
153c5441932SPravin B Shelar 	else
154c5441932SPravin B Shelar 		__ip_select_ident(iph, dst,
155c5441932SPravin B Shelar 				  (skb_shinfo(skb)->gso_segs ?: 1) - 1);
156c5441932SPravin B Shelar }
157c5441932SPravin B Shelar 
1583d7b46cdSPravin B Shelar int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto);
1590e6fbc5bSPravin B Shelar int iptunnel_xmit(struct net *net, struct rtable *rt,
1600e6fbc5bSPravin B Shelar 		  struct sk_buff *skb,
1610e6fbc5bSPravin B Shelar 		  __be32 src, __be32 dst, __u8 proto,
1620e6fbc5bSPravin B Shelar 		  __u8 tos, __u8 ttl, __be16 df);
1630e6fbc5bSPravin B Shelar 
1640e6fbc5bSPravin B Shelar static inline void iptunnel_xmit_stats(int err,
1650e6fbc5bSPravin B Shelar 				       struct net_device_stats *err_stats,
1660e6fbc5bSPravin B Shelar 				       struct pcpu_tstats __percpu *stats)
167c5441932SPravin B Shelar {
1680e6fbc5bSPravin B Shelar 	if (err > 0) {
1690e6fbc5bSPravin B Shelar 		struct pcpu_tstats *tstats = this_cpu_ptr(stats);
170c5441932SPravin B Shelar 
171c5441932SPravin B Shelar 		u64_stats_update_begin(&tstats->syncp);
1720e6fbc5bSPravin B Shelar 		tstats->tx_bytes += err;
173c5441932SPravin B Shelar 		tstats->tx_packets++;
174c5441932SPravin B Shelar 		u64_stats_update_end(&tstats->syncp);
1750e6fbc5bSPravin B Shelar 	} else if (err < 0) {
1760e6fbc5bSPravin B Shelar 		err_stats->tx_errors++;
1770e6fbc5bSPravin B Shelar 		err_stats->tx_aborted_errors++;
178c5441932SPravin B Shelar 	} else {
1790e6fbc5bSPravin B Shelar 		err_stats->tx_dropped++;
180c5441932SPravin B Shelar 	}
181c5441932SPravin B Shelar }
182c5441932SPravin B Shelar #endif /* __NET_IP_TUNNELS_H */
183