Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __NET_IP_TUNNELS_H
0003 #define __NET_IP_TUNNELS_H 1
0004 
0005 #include <linux/if_tunnel.h>
0006 #include <linux/netdevice.h>
0007 #include <linux/skbuff.h>
0008 #include <linux/socket.h>
0009 #include <linux/types.h>
0010 #include <linux/u64_stats_sync.h>
0011 #include <linux/bitops.h>
0012 
0013 #include <net/dsfield.h>
0014 #include <net/gro_cells.h>
0015 #include <net/inet_ecn.h>
0016 #include <net/netns/generic.h>
0017 #include <net/rtnetlink.h>
0018 #include <net/lwtunnel.h>
0019 #include <net/dst_cache.h>
0020 
0021 #if IS_ENABLED(CONFIG_IPV6)
0022 #include <net/ipv6.h>
0023 #include <net/ip6_fib.h>
0024 #include <net/ip6_route.h>
0025 #endif
0026 
0027 /* Keep error state on tunnel for 30 sec */
0028 #define IPTUNNEL_ERR_TIMEO  (30*HZ)
0029 
0030 /* Used to memset ip_tunnel padding. */
0031 #define IP_TUNNEL_KEY_SIZE  offsetofend(struct ip_tunnel_key, tp_dst)
0032 
0033 /* Used to memset ipv4 address padding. */
0034 #define IP_TUNNEL_KEY_IPV4_PAD  offsetofend(struct ip_tunnel_key, u.ipv4.dst)
0035 #define IP_TUNNEL_KEY_IPV4_PAD_LEN              \
0036     (sizeof_field(struct ip_tunnel_key, u) -        \
0037      sizeof_field(struct ip_tunnel_key, u.ipv4))
0038 
0039 struct ip_tunnel_key {
0040     __be64          tun_id;
0041     union {
0042         struct {
0043             __be32  src;
0044             __be32  dst;
0045         } ipv4;
0046         struct {
0047             struct in6_addr src;
0048             struct in6_addr dst;
0049         } ipv6;
0050     } u;
0051     __be16          tun_flags;
0052     u8          tos;        /* TOS for IPv4, TC for IPv6 */
0053     u8          ttl;        /* TTL for IPv4, HL for IPv6 */
0054     __be32          label;      /* Flow Label for IPv6 */
0055     __be16          tp_src;
0056     __be16          tp_dst;
0057     __u8            flow_flags;
0058 };
0059 
0060 /* Flags for ip_tunnel_info mode. */
0061 #define IP_TUNNEL_INFO_TX   0x01    /* represents tx tunnel parameters */
0062 #define IP_TUNNEL_INFO_IPV6 0x02    /* key contains IPv6 addresses */
0063 #define IP_TUNNEL_INFO_BRIDGE   0x04    /* represents a bridged tunnel id */
0064 
0065 /* Maximum tunnel options length. */
0066 #define IP_TUNNEL_OPTS_MAX                  \
0067     GENMASK((sizeof_field(struct ip_tunnel_info,        \
0068                   options_len) * BITS_PER_BYTE) - 1, 0)
0069 
0070 struct ip_tunnel_info {
0071     struct ip_tunnel_key    key;
0072 #ifdef CONFIG_DST_CACHE
0073     struct dst_cache    dst_cache;
0074 #endif
0075     u8          options_len;
0076     u8          mode;
0077 };
0078 
0079 /* 6rd prefix/relay information */
0080 #ifdef CONFIG_IPV6_SIT_6RD
0081 struct ip_tunnel_6rd_parm {
0082     struct in6_addr     prefix;
0083     __be32          relay_prefix;
0084     u16         prefixlen;
0085     u16         relay_prefixlen;
0086 };
0087 #endif
0088 
0089 struct ip_tunnel_encap {
0090     u16         type;
0091     u16         flags;
0092     __be16          sport;
0093     __be16          dport;
0094 };
0095 
0096 struct ip_tunnel_prl_entry {
0097     struct ip_tunnel_prl_entry __rcu *next;
0098     __be32              addr;
0099     u16             flags;
0100     struct rcu_head         rcu_head;
0101 };
0102 
0103 struct metadata_dst;
0104 
0105 struct ip_tunnel {
0106     struct ip_tunnel __rcu  *next;
0107     struct hlist_node hash_node;
0108 
0109     struct net_device   *dev;
0110     netdevice_tracker   dev_tracker;
0111 
0112     struct net      *net;   /* netns for packet i/o */
0113 
0114     unsigned long   err_time;   /* Time when the last ICMP error
0115                      * arrived */
0116     int     err_count;  /* Number of arrived ICMP errors */
0117 
0118     /* These four fields used only by GRE */
0119     u32     i_seqno;    /* The last seen seqno  */
0120     atomic_t    o_seqno;    /* The last output seqno */
0121     int     tun_hlen;   /* Precalculated header length */
0122 
0123     /* These four fields used only by ERSPAN */
0124     u32     index;      /* ERSPAN type II index */
0125     u8      erspan_ver; /* ERSPAN version */
0126     u8      dir;        /* ERSPAN direction */
0127     u16     hwid;       /* ERSPAN hardware ID */
0128 
0129     struct dst_cache dst_cache;
0130 
0131     struct ip_tunnel_parm parms;
0132 
0133     int     mlink;
0134     int     encap_hlen; /* Encap header length (FOU,GUE) */
0135     int     hlen;       /* tun_hlen + encap_hlen */
0136     struct ip_tunnel_encap encap;
0137 
0138     /* for SIT */
0139 #ifdef CONFIG_IPV6_SIT_6RD
0140     struct ip_tunnel_6rd_parm ip6rd;
0141 #endif
0142     struct ip_tunnel_prl_entry __rcu *prl;  /* potential router list */
0143     unsigned int        prl_count;  /* # of entries in PRL */
0144     unsigned int        ip_tnl_net_id;
0145     struct gro_cells    gro_cells;
0146     __u32           fwmark;
0147     bool            collect_md;
0148     bool            ignore_df;
0149 };
0150 
0151 struct tnl_ptk_info {
0152     __be16 flags;
0153     __be16 proto;
0154     __be32 key;
0155     __be32 seq;
0156     int hdr_len;
0157 };
0158 
0159 #define PACKET_RCVD 0
0160 #define PACKET_REJECT   1
0161 #define PACKET_NEXT 2
0162 
0163 #define IP_TNL_HASH_BITS   7
0164 #define IP_TNL_HASH_SIZE   (1 << IP_TNL_HASH_BITS)
0165 
0166 struct ip_tunnel_net {
0167     struct net_device *fb_tunnel_dev;
0168     struct rtnl_link_ops *rtnl_link_ops;
0169     struct hlist_head tunnels[IP_TNL_HASH_SIZE];
0170     struct ip_tunnel __rcu *collect_md_tun;
0171     int type;
0172 };
0173 
0174 static inline void ip_tunnel_key_init(struct ip_tunnel_key *key,
0175                       __be32 saddr, __be32 daddr,
0176                       u8 tos, u8 ttl, __be32 label,
0177                       __be16 tp_src, __be16 tp_dst,
0178                       __be64 tun_id, __be16 tun_flags)
0179 {
0180     key->tun_id = tun_id;
0181     key->u.ipv4.src = saddr;
0182     key->u.ipv4.dst = daddr;
0183     memset((unsigned char *)key + IP_TUNNEL_KEY_IPV4_PAD,
0184            0, IP_TUNNEL_KEY_IPV4_PAD_LEN);
0185     key->tos = tos;
0186     key->ttl = ttl;
0187     key->label = label;
0188     key->tun_flags = tun_flags;
0189 
0190     /* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
0191      * the upper tunnel are used.
0192      * E.g: GRE over IPSEC, the tp_src and tp_port are zero.
0193      */
0194     key->tp_src = tp_src;
0195     key->tp_dst = tp_dst;
0196 
0197     /* Clear struct padding. */
0198     if (sizeof(*key) != IP_TUNNEL_KEY_SIZE)
0199         memset((unsigned char *)key + IP_TUNNEL_KEY_SIZE,
0200                0, sizeof(*key) - IP_TUNNEL_KEY_SIZE);
0201 }
0202 
0203 static inline bool
0204 ip_tunnel_dst_cache_usable(const struct sk_buff *skb,
0205                const struct ip_tunnel_info *info)
0206 {
0207     if (skb->mark)
0208         return false;
0209     if (!info)
0210         return true;
0211     if (info->key.tun_flags & TUNNEL_NOCACHE)
0212         return false;
0213 
0214     return true;
0215 }
0216 
0217 static inline unsigned short ip_tunnel_info_af(const struct ip_tunnel_info
0218                            *tun_info)
0219 {
0220     return tun_info->mode & IP_TUNNEL_INFO_IPV6 ? AF_INET6 : AF_INET;
0221 }
0222 
0223 static inline __be64 key32_to_tunnel_id(__be32 key)
0224 {
0225 #ifdef __BIG_ENDIAN
0226     return (__force __be64)key;
0227 #else
0228     return (__force __be64)((__force u64)key << 32);
0229 #endif
0230 }
0231 
0232 /* Returns the least-significant 32 bits of a __be64. */
0233 static inline __be32 tunnel_id_to_key32(__be64 tun_id)
0234 {
0235 #ifdef __BIG_ENDIAN
0236     return (__force __be32)tun_id;
0237 #else
0238     return (__force __be32)((__force u64)tun_id >> 32);
0239 #endif
0240 }
0241 
0242 #ifdef CONFIG_INET
0243 
0244 static inline void ip_tunnel_init_flow(struct flowi4 *fl4,
0245                        int proto,
0246                        __be32 daddr, __be32 saddr,
0247                        __be32 key, __u8 tos,
0248                        struct net *net, int oif,
0249                        __u32 mark, __u32 tun_inner_hash,
0250                        __u8 flow_flags)
0251 {
0252     memset(fl4, 0, sizeof(*fl4));
0253 
0254     if (oif) {
0255         fl4->flowi4_l3mdev = l3mdev_master_upper_ifindex_by_index_rcu(net, oif);
0256         /* Legacy VRF/l3mdev use case */
0257         fl4->flowi4_oif = fl4->flowi4_l3mdev ? 0 : oif;
0258     }
0259 
0260     fl4->daddr = daddr;
0261     fl4->saddr = saddr;
0262     fl4->flowi4_tos = tos;
0263     fl4->flowi4_proto = proto;
0264     fl4->fl4_gre_key = key;
0265     fl4->flowi4_mark = mark;
0266     fl4->flowi4_multipath_hash = tun_inner_hash;
0267     fl4->flowi4_flags = flow_flags;
0268 }
0269 
0270 int ip_tunnel_init(struct net_device *dev);
0271 void ip_tunnel_uninit(struct net_device *dev);
0272 void  ip_tunnel_dellink(struct net_device *dev, struct list_head *head);
0273 struct net *ip_tunnel_get_link_net(const struct net_device *dev);
0274 int ip_tunnel_get_iflink(const struct net_device *dev);
0275 int ip_tunnel_init_net(struct net *net, unsigned int ip_tnl_net_id,
0276                struct rtnl_link_ops *ops, char *devname);
0277 
0278 void ip_tunnel_delete_nets(struct list_head *list_net, unsigned int id,
0279                struct rtnl_link_ops *ops);
0280 
0281 void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
0282             const struct iphdr *tnl_params, const u8 protocol);
0283 void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
0284                const u8 proto, int tunnel_hlen);
0285 int ip_tunnel_ctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd);
0286 int ip_tunnel_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
0287                  void __user *data, int cmd);
0288 int __ip_tunnel_change_mtu(struct net_device *dev, int new_mtu, bool strict);
0289 int ip_tunnel_change_mtu(struct net_device *dev, int new_mtu);
0290 
0291 struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
0292                    int link, __be16 flags,
0293                    __be32 remote, __be32 local,
0294                    __be32 key);
0295 
0296 int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
0297           const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
0298           bool log_ecn_error);
0299 int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
0300              struct ip_tunnel_parm *p, __u32 fwmark);
0301 int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],
0302               struct ip_tunnel_parm *p, __u32 fwmark);
0303 void ip_tunnel_setup(struct net_device *dev, unsigned int net_id);
0304 
0305 extern const struct header_ops ip_tunnel_header_ops;
0306 __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb);
0307 
0308 struct ip_tunnel_encap_ops {
0309     size_t (*encap_hlen)(struct ip_tunnel_encap *e);
0310     int (*build_header)(struct sk_buff *skb, struct ip_tunnel_encap *e,
0311                 u8 *protocol, struct flowi4 *fl4);
0312     int (*err_handler)(struct sk_buff *skb, u32 info);
0313 };
0314 
0315 #define MAX_IPTUN_ENCAP_OPS 8
0316 
0317 extern const struct ip_tunnel_encap_ops __rcu *
0318         iptun_encaps[MAX_IPTUN_ENCAP_OPS];
0319 
0320 int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
0321                 unsigned int num);
0322 int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
0323                 unsigned int num);
0324 
0325 int ip_tunnel_encap_setup(struct ip_tunnel *t,
0326               struct ip_tunnel_encap *ipencap);
0327 
0328 static inline bool pskb_inet_may_pull(struct sk_buff *skb)
0329 {
0330     int nhlen;
0331 
0332     switch (skb->protocol) {
0333 #if IS_ENABLED(CONFIG_IPV6)
0334     case htons(ETH_P_IPV6):
0335         nhlen = sizeof(struct ipv6hdr);
0336         break;
0337 #endif
0338     case htons(ETH_P_IP):
0339         nhlen = sizeof(struct iphdr);
0340         break;
0341     default:
0342         nhlen = 0;
0343     }
0344 
0345     return pskb_network_may_pull(skb, nhlen);
0346 }
0347 
0348 static inline int ip_encap_hlen(struct ip_tunnel_encap *e)
0349 {
0350     const struct ip_tunnel_encap_ops *ops;
0351     int hlen = -EINVAL;
0352 
0353     if (e->type == TUNNEL_ENCAP_NONE)
0354         return 0;
0355 
0356     if (e->type >= MAX_IPTUN_ENCAP_OPS)
0357         return -EINVAL;
0358 
0359     rcu_read_lock();
0360     ops = rcu_dereference(iptun_encaps[e->type]);
0361     if (likely(ops && ops->encap_hlen))
0362         hlen = ops->encap_hlen(e);
0363     rcu_read_unlock();
0364 
0365     return hlen;
0366 }
0367 
0368 static inline int ip_tunnel_encap(struct sk_buff *skb, struct ip_tunnel *t,
0369                   u8 *protocol, struct flowi4 *fl4)
0370 {
0371     const struct ip_tunnel_encap_ops *ops;
0372     int ret = -EINVAL;
0373 
0374     if (t->encap.type == TUNNEL_ENCAP_NONE)
0375         return 0;
0376 
0377     if (t->encap.type >= MAX_IPTUN_ENCAP_OPS)
0378         return -EINVAL;
0379 
0380     rcu_read_lock();
0381     ops = rcu_dereference(iptun_encaps[t->encap.type]);
0382     if (likely(ops && ops->build_header))
0383         ret = ops->build_header(skb, &t->encap, protocol, fl4);
0384     rcu_read_unlock();
0385 
0386     return ret;
0387 }
0388 
0389 /* Extract dsfield from inner protocol */
0390 static inline u8 ip_tunnel_get_dsfield(const struct iphdr *iph,
0391                        const struct sk_buff *skb)
0392 {
0393     __be16 payload_protocol = skb_protocol(skb, true);
0394 
0395     if (payload_protocol == htons(ETH_P_IP))
0396         return iph->tos;
0397     else if (payload_protocol == htons(ETH_P_IPV6))
0398         return ipv6_get_dsfield((const struct ipv6hdr *)iph);
0399     else
0400         return 0;
0401 }
0402 
0403 static inline u8 ip_tunnel_get_ttl(const struct iphdr *iph,
0404                        const struct sk_buff *skb)
0405 {
0406     __be16 payload_protocol = skb_protocol(skb, true);
0407 
0408     if (payload_protocol == htons(ETH_P_IP))
0409         return iph->ttl;
0410     else if (payload_protocol == htons(ETH_P_IPV6))
0411         return ((const struct ipv6hdr *)iph)->hop_limit;
0412     else
0413         return 0;
0414 }
0415 
0416 /* Propogate ECN bits out */
0417 static inline u8 ip_tunnel_ecn_encap(u8 tos, const struct iphdr *iph,
0418                      const struct sk_buff *skb)
0419 {
0420     u8 inner = ip_tunnel_get_dsfield(iph, skb);
0421 
0422     return INET_ECN_encapsulate(tos, inner);
0423 }
0424 
0425 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
0426                __be16 inner_proto, bool raw_proto, bool xnet);
0427 
0428 static inline int iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
0429                        __be16 inner_proto, bool xnet)
0430 {
0431     return __iptunnel_pull_header(skb, hdr_len, inner_proto, false, xnet);
0432 }
0433 
0434 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
0435            __be32 src, __be32 dst, u8 proto,
0436            u8 tos, u8 ttl, __be16 df, bool xnet);
0437 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
0438                          gfp_t flags);
0439 int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst,
0440               int headroom, bool reply);
0441 
0442 int iptunnel_handle_offloads(struct sk_buff *skb, int gso_type_mask);
0443 
0444 static inline int iptunnel_pull_offloads(struct sk_buff *skb)
0445 {
0446     if (skb_is_gso(skb)) {
0447         int err;
0448 
0449         err = skb_unclone(skb, GFP_ATOMIC);
0450         if (unlikely(err))
0451             return err;
0452         skb_shinfo(skb)->gso_type &= ~(NETIF_F_GSO_ENCAP_ALL >>
0453                            NETIF_F_GSO_SHIFT);
0454     }
0455 
0456     skb->encapsulation = 0;
0457     return 0;
0458 }
0459 
0460 static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len)
0461 {
0462     if (pkt_len > 0) {
0463         struct pcpu_sw_netstats *tstats = get_cpu_ptr(dev->tstats);
0464 
0465         u64_stats_update_begin(&tstats->syncp);
0466         u64_stats_add(&tstats->tx_bytes, pkt_len);
0467         u64_stats_inc(&tstats->tx_packets);
0468         u64_stats_update_end(&tstats->syncp);
0469         put_cpu_ptr(tstats);
0470     } else {
0471         struct net_device_stats *err_stats = &dev->stats;
0472 
0473         if (pkt_len < 0) {
0474             err_stats->tx_errors++;
0475             err_stats->tx_aborted_errors++;
0476         } else {
0477             err_stats->tx_dropped++;
0478         }
0479     }
0480 }
0481 
0482 static inline void *ip_tunnel_info_opts(struct ip_tunnel_info *info)
0483 {
0484     return info + 1;
0485 }
0486 
0487 static inline void ip_tunnel_info_opts_get(void *to,
0488                        const struct ip_tunnel_info *info)
0489 {
0490     memcpy(to, info + 1, info->options_len);
0491 }
0492 
0493 static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
0494                        const void *from, int len,
0495                        __be16 flags)
0496 {
0497     info->options_len = len;
0498     if (len > 0) {
0499         memcpy(ip_tunnel_info_opts(info), from, len);
0500         info->key.tun_flags |= flags;
0501     }
0502 }
0503 
0504 static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
0505 {
0506     return (struct ip_tunnel_info *)lwtstate->data;
0507 }
0508 
0509 DECLARE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
0510 
0511 /* Returns > 0 if metadata should be collected */
0512 static inline int ip_tunnel_collect_metadata(void)
0513 {
0514     return static_branch_unlikely(&ip_tunnel_metadata_cnt);
0515 }
0516 
0517 void __init ip_tunnel_core_init(void);
0518 
0519 void ip_tunnel_need_metadata(void);
0520 void ip_tunnel_unneed_metadata(void);
0521 
0522 #else /* CONFIG_INET */
0523 
0524 static inline struct ip_tunnel_info *lwt_tun_info(struct lwtunnel_state *lwtstate)
0525 {
0526     return NULL;
0527 }
0528 
0529 static inline void ip_tunnel_need_metadata(void)
0530 {
0531 }
0532 
0533 static inline void ip_tunnel_unneed_metadata(void)
0534 {
0535 }
0536 
0537 static inline void ip_tunnel_info_opts_get(void *to,
0538                        const struct ip_tunnel_info *info)
0539 {
0540 }
0541 
0542 static inline void ip_tunnel_info_opts_set(struct ip_tunnel_info *info,
0543                        const void *from, int len,
0544                        __be16 flags)
0545 {
0546     info->options_len = 0;
0547 }
0548 
0549 #endif /* CONFIG_INET */
0550 
0551 #endif /* __NET_IP_TUNNELS_H */