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 419a4aa9afSTom Herbert struct ip_tunnel_dst { 429a4aa9afSTom Herbert struct dst_entry __rcu *dst; 439a4aa9afSTom Herbert spinlock_t lock; 449a4aa9afSTom Herbert }; 459a4aa9afSTom Herbert 46c5441932SPravin B Shelar struct ip_tunnel { 47c5441932SPravin B Shelar struct ip_tunnel __rcu *next; 48c5441932SPravin B Shelar struct hlist_node hash_node; 49c5441932SPravin B Shelar struct net_device *dev; 505e6700b3SNicolas Dichtel struct net *net; /* netns for packet i/o */ 51c5441932SPravin B Shelar 52c5441932SPravin B Shelar int err_count; /* Number of arrived ICMP errors */ 53c5441932SPravin B Shelar unsigned long err_time; /* Time when the last ICMP error 54c5441932SPravin B Shelar * arrived */ 55c5441932SPravin B Shelar 56c5441932SPravin B Shelar /* These four fields used only by GRE */ 57c5441932SPravin B Shelar __u32 i_seqno; /* The last seen seqno */ 58c5441932SPravin B Shelar __u32 o_seqno; /* The last output seqno */ 59c5441932SPravin B Shelar int hlen; /* Precalculated header length */ 60c5441932SPravin B Shelar int mlink; 61c5441932SPravin B Shelar 629a4aa9afSTom Herbert struct ip_tunnel_dst __percpu *dst_cache; 637d442fabSTom Herbert 64c5441932SPravin B Shelar struct ip_tunnel_parm parms; 65c5441932SPravin B Shelar 66c5441932SPravin B Shelar /* for SIT */ 67c5441932SPravin B Shelar #ifdef CONFIG_IPV6_SIT_6RD 68c5441932SPravin B Shelar struct ip_tunnel_6rd_parm ip6rd; 69c5441932SPravin B Shelar #endif 70c5441932SPravin B Shelar struct ip_tunnel_prl_entry __rcu *prl; /* potential router list */ 71c5441932SPravin B Shelar unsigned int prl_count; /* # of entries in PRL */ 72c5441932SPravin B Shelar int ip_tnl_net_id; 73c5441932SPravin B Shelar struct gro_cells gro_cells; 74c5441932SPravin B Shelar }; 75c5441932SPravin B Shelar 76c5441932SPravin B Shelar #define TUNNEL_CSUM __cpu_to_be16(0x01) 77c5441932SPravin B Shelar #define TUNNEL_ROUTING __cpu_to_be16(0x02) 78c5441932SPravin B Shelar #define TUNNEL_KEY __cpu_to_be16(0x04) 79c5441932SPravin B Shelar #define TUNNEL_SEQ __cpu_to_be16(0x08) 80c5441932SPravin B Shelar #define TUNNEL_STRICT __cpu_to_be16(0x10) 81c5441932SPravin B Shelar #define TUNNEL_REC __cpu_to_be16(0x20) 82c5441932SPravin B Shelar #define TUNNEL_VERSION __cpu_to_be16(0x40) 83c5441932SPravin B Shelar #define TUNNEL_NO_KEY __cpu_to_be16(0x80) 849a628224SPravin B Shelar #define TUNNEL_DONT_FRAGMENT __cpu_to_be16(0x0100) 85c5441932SPravin B Shelar 86c5441932SPravin B Shelar struct tnl_ptk_info { 87c5441932SPravin B Shelar __be16 flags; 88c5441932SPravin B Shelar __be16 proto; 89c5441932SPravin B Shelar __be32 key; 90c5441932SPravin B Shelar __be32 seq; 91c5441932SPravin B Shelar }; 92c5441932SPravin B Shelar 93c5441932SPravin B Shelar #define PACKET_RCVD 0 94c5441932SPravin B Shelar #define PACKET_REJECT 1 95c5441932SPravin B Shelar 966261d983Sstephen hemminger #define IP_TNL_HASH_BITS 7 97c5441932SPravin B Shelar #define IP_TNL_HASH_SIZE (1 << IP_TNL_HASH_BITS) 98c5441932SPravin B Shelar 99c5441932SPravin B Shelar struct ip_tunnel_net { 100c5441932SPravin B Shelar struct net_device *fb_tunnel_dev; 1016261d983Sstephen hemminger struct hlist_head tunnels[IP_TNL_HASH_SIZE]; 102c5441932SPravin B Shelar }; 103c5441932SPravin B Shelar 1045243b6acSJesse Gross #ifdef CONFIG_INET 1055243b6acSJesse Gross 106c5441932SPravin B Shelar int ip_tunnel_init(struct net_device *dev); 107c5441932SPravin B Shelar void ip_tunnel_uninit(struct net_device *dev); 108c5441932SPravin B Shelar void ip_tunnel_dellink(struct net_device *dev, struct list_head *head); 109d3b6f614SEric Dumazet int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id, 110c5441932SPravin B Shelar struct rtnl_link_ops *ops, char *devname); 111c5441932SPravin B Shelar 1126c742e71SNicolas Dichtel void ip_tunnel_delete_net(struct ip_tunnel_net *itn, struct rtnl_link_ops *ops); 113c5441932SPravin B Shelar 114c5441932SPravin B Shelar void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev, 115bf3d6a8fSNicolas Dichtel const struct iphdr *tnl_params, const u8 protocol); 116c5441932SPravin B Shelar int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd); 117c5441932SPravin B Shelar int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu); 118c5441932SPravin B Shelar 119c5441932SPravin B Shelar struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev, 120c5441932SPravin B Shelar struct rtnl_link_stats64 *tot); 121c5441932SPravin B Shelar struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn, 122c5441932SPravin B Shelar int link, __be16 flags, 123c5441932SPravin B Shelar __be32 remote, __be32 local, 124c5441932SPravin B Shelar __be32 key); 125c5441932SPravin B Shelar 126c5441932SPravin B Shelar int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb, 127c5441932SPravin B Shelar const struct tnl_ptk_info *tpi, bool log_ecn_error); 128c5441932SPravin B Shelar int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[], 129c5441932SPravin B Shelar struct ip_tunnel_parm *p); 130c5441932SPravin B Shelar int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[], 131c5441932SPravin B Shelar struct ip_tunnel_parm *p); 132c5441932SPravin B Shelar void ip_tunnel_setup(struct net_device *dev, int net_id); 133c5441932SPravin B Shelar 134c5441932SPravin B Shelar /* Extract dsfield from inner protocol */ 135c5441932SPravin B Shelar static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph, 136c5441932SPravin B Shelar const struct sk_buff *skb) 137c5441932SPravin B Shelar { 138c5441932SPravin B Shelar if (skb->protocol == htons(ETH_P_IP)) 139c5441932SPravin B Shelar return iph->tos; 140c5441932SPravin B Shelar else if (skb->protocol == htons(ETH_P_IPV6)) 141c5441932SPravin B Shelar return ipv6_get_dsfield((const struct ipv6hdr *)iph); 142c5441932SPravin B Shelar else 143c5441932SPravin B Shelar return 0; 144c5441932SPravin B Shelar } 145c5441932SPravin B Shelar 146c5441932SPravin B Shelar /* Propogate ECN bits out */ 147c5441932SPravin B Shelar static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph, 148c5441932SPravin B Shelar const struct sk_buff *skb) 149c5441932SPravin B Shelar { 150c5441932SPravin B Shelar u8 inner = ip_tunnel_get_dsfield(iph, skb); 151c5441932SPravin B Shelar 152c5441932SPravin B Shelar return INET_ECN_encapsulate(tos, inner); 153c5441932SPravin B Shelar } 154c5441932SPravin B Shelar 1553d7b46cdSPravin B Shelar int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto); 1568b7ed2d9SNicolas Dichtel int iptunnel_xmit(struct rtable *rt, struct sk_buff *skb, 1570e6fbc5bSPravin B Shelar __be32 src, __be32 dst, __u8 proto, 158963a88b3SNicolas Dichtel __u8 tos, __u8 ttl, __be16 df, bool xnet); 1590e6fbc5bSPravin B Shelar 1602d26f0a3SEric Dumazet struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, bool gre_csum, 1612d26f0a3SEric Dumazet int gso_type_mask); 1622d26f0a3SEric Dumazet 1630e6fbc5bSPravin B Shelar static inline void iptunnel_xmit_stats(int err, 1640e6fbc5bSPravin B Shelar struct net_device_stats *err_stats, 1658f84985fSLi RongQing struct pcpu_sw_netstats __percpu *stats) 166c5441932SPravin B Shelar { 1670e6fbc5bSPravin B Shelar if (err > 0) { 1688f84985fSLi RongQing struct pcpu_sw_netstats *tstats = this_cpu_ptr(stats); 169c5441932SPravin B Shelar 170c5441932SPravin B Shelar u64_stats_update_begin(&tstats->syncp); 1710e6fbc5bSPravin B Shelar tstats->tx_bytes += err; 172c5441932SPravin B Shelar tstats->tx_packets++; 173c5441932SPravin B Shelar u64_stats_update_end(&tstats->syncp); 1740e6fbc5bSPravin B Shelar } else if (err < 0) { 1750e6fbc5bSPravin B Shelar err_stats->tx_errors++; 1760e6fbc5bSPravin B Shelar err_stats->tx_aborted_errors++; 177c5441932SPravin B Shelar } else { 1780e6fbc5bSPravin B Shelar err_stats->tx_dropped++; 179c5441932SPravin B Shelar } 180c5441932SPravin B Shelar } 1815243b6acSJesse Gross 1825243b6acSJesse Gross #endif /* CONFIG_INET */ 1835243b6acSJesse Gross 184c5441932SPravin B Shelar #endif /* __NET_IP_TUNNELS_H */ 185