^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _NET_IP6_TUNNEL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _NET_IP6_TUNNEL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/if_tunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/ip6_tunnel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <net/dst_cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define IP6TUNNEL_ERR_TIMEO (30*HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /* capable of sending packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define IP6_TNL_F_CAP_XMIT 0x10000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /* capable of receiving packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define IP6_TNL_F_CAP_RCV 0x20000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* determine capability on a per-packet basis */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define IP6_TNL_F_CAP_PER_PACKET 0x40000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct __ip6_tnl_parm {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) char name[IFNAMSIZ]; /* name of tunnel device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int link; /* ifindex of underlying L2 interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) __u8 proto; /* tunnel protocol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) __u8 encap_limit; /* encapsulation limit for tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) __u8 hop_limit; /* hop limit for tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) bool collect_md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) __be32 flowinfo; /* traffic class and flowlabel for tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) __u32 flags; /* tunnel flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct in6_addr laddr; /* local tunnel end-point address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct in6_addr raddr; /* remote tunnel end-point address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) __be16 i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) __be16 o_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) __be32 i_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) __be32 o_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) __u32 fwmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) __u32 index; /* ERSPAN type II index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) __u8 erspan_ver; /* ERSPAN version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) __u8 dir; /* direction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) __u16 hwid; /* hwid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* IPv6 tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct ip6_tnl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct ip6_tnl __rcu *next; /* next tunnel in list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct net_device *dev; /* virtual device associated with tunnel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct net *net; /* netns for packet i/o */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct __ip6_tnl_parm parms; /* tunnel configuration parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct flowi fl; /* flowi template for xmit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct dst_cache dst_cache; /* cached dst */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct gro_cells gro_cells;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int err_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned long err_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* These fields used only by GRE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) __u32 i_seqno; /* The last seen seqno */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) __u32 o_seqno; /* The last output seqno */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int hlen; /* tun_hlen + encap_hlen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int tun_hlen; /* Precalculated header length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int encap_hlen; /* Encap header length (FOU,GUE) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct ip_tunnel_encap encap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int mlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ip6_tnl_encap_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) size_t (*encap_hlen)(struct ip_tunnel_encap *e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u8 *protocol, struct flowi6 *fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) u8 type, u8 code, int offset, __be32 info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #ifdef CONFIG_INET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) extern const struct ip6_tnl_encap_ops __rcu *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ip6tun_encaps[MAX_IPTUN_ENCAP_OPS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) unsigned int num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned int num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int ip6_tnl_encap_setup(struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct ip_tunnel_encap *ipencap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static inline int ip6_encap_hlen(struct ip_tunnel_encap *e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) const struct ip6_tnl_encap_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int hlen = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (e->type == TUNNEL_ENCAP_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (e->type >= MAX_IPTUN_ENCAP_OPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ops = rcu_dereference(ip6tun_encaps[e->type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (likely(ops && ops->encap_hlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) hlen = ops->encap_hlen(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static inline int ip6_tnl_encap(struct sk_buff *skb, struct ip6_tnl *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) u8 *protocol, struct flowi6 *fl6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) const struct ip6_tnl_encap_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (t->encap.type == TUNNEL_ENCAP_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (t->encap.type >= MAX_IPTUN_ENCAP_OPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ops = rcu_dereference(ip6tun_encaps[t->encap.type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (likely(ops && ops->build_header))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ret = ops->build_header(skb, &t->encap, protocol, fl6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* Tunnel encapsulation limit destination sub-option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct ipv6_tlv_tnl_enc_lim {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) __u8 type; /* type-code for option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) __u8 length; /* option length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) __u8 encap_limit; /* tunnel encapsulation limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int ip6_tnl_rcv_ctl(struct ip6_tnl *t, const struct in6_addr *laddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) const struct in6_addr *raddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) bool log_ecn_error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int ip6_tnl_xmit_ctl(struct ip6_tnl *t, const struct in6_addr *laddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) const struct in6_addr *raddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev, __u8 dsfield,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct flowi6 *fl6, int encap_limit, __u32 *pmtu, __u8 proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) __u32 ip6_tnl_get_cap(struct ip6_tnl *t, const struct in6_addr *laddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) const struct in6_addr *raddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct net *ip6_tnl_get_link_net(const struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) int ip6_tnl_get_iflink(const struct net_device *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int ip6_tnl_change_mtu(struct net_device *dev, int new_mtu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static inline void ip6tunnel_xmit(struct sock *sk, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int pkt_len, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) pkt_len = skb->len - skb_inner_network_offset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) err = ip6_local_out(dev_net(skb_dst(skb)->dev), sk, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (unlikely(net_xmit_eval(err)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) pkt_len = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) iptunnel_xmit_stats(dev, pkt_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #endif