Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __NET_UDP_TUNNEL_H
0003 #define __NET_UDP_TUNNEL_H
0004 
0005 #include <net/ip_tunnels.h>
0006 #include <net/udp.h>
0007 
0008 #if IS_ENABLED(CONFIG_IPV6)
0009 #include <net/ipv6.h>
0010 #include <net/ipv6_stubs.h>
0011 #endif
0012 
0013 struct udp_port_cfg {
0014     u8          family;
0015 
0016     /* Used only for kernel-created sockets */
0017     union {
0018         struct in_addr      local_ip;
0019 #if IS_ENABLED(CONFIG_IPV6)
0020         struct in6_addr     local_ip6;
0021 #endif
0022     };
0023 
0024     union {
0025         struct in_addr      peer_ip;
0026 #if IS_ENABLED(CONFIG_IPV6)
0027         struct in6_addr     peer_ip6;
0028 #endif
0029     };
0030 
0031     __be16          local_udp_port;
0032     __be16          peer_udp_port;
0033     int         bind_ifindex;
0034     unsigned int        use_udp_checksums:1,
0035                 use_udp6_tx_checksums:1,
0036                 use_udp6_rx_checksums:1,
0037                 ipv6_v6only:1;
0038 };
0039 
0040 int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
0041              struct socket **sockp);
0042 
0043 #if IS_ENABLED(CONFIG_IPV6)
0044 int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
0045              struct socket **sockp);
0046 #else
0047 static inline int udp_sock_create6(struct net *net, struct udp_port_cfg *cfg,
0048                    struct socket **sockp)
0049 {
0050     return 0;
0051 }
0052 #endif
0053 
0054 static inline int udp_sock_create(struct net *net,
0055                   struct udp_port_cfg *cfg,
0056                   struct socket **sockp)
0057 {
0058     if (cfg->family == AF_INET)
0059         return udp_sock_create4(net, cfg, sockp);
0060 
0061     if (cfg->family == AF_INET6)
0062         return udp_sock_create6(net, cfg, sockp);
0063 
0064     return -EPFNOSUPPORT;
0065 }
0066 
0067 typedef int (*udp_tunnel_encap_rcv_t)(struct sock *sk, struct sk_buff *skb);
0068 typedef int (*udp_tunnel_encap_err_lookup_t)(struct sock *sk,
0069                          struct sk_buff *skb);
0070 typedef void (*udp_tunnel_encap_err_rcv_t)(struct sock *sk,
0071                        struct sk_buff *skb,
0072                        unsigned int udp_offset);
0073 typedef void (*udp_tunnel_encap_destroy_t)(struct sock *sk);
0074 typedef struct sk_buff *(*udp_tunnel_gro_receive_t)(struct sock *sk,
0075                             struct list_head *head,
0076                             struct sk_buff *skb);
0077 typedef int (*udp_tunnel_gro_complete_t)(struct sock *sk, struct sk_buff *skb,
0078                      int nhoff);
0079 
0080 struct udp_tunnel_sock_cfg {
0081     void *sk_user_data;     /* user data used by encap_rcv call back */
0082     /* Used for setting up udp_sock fields, see udp.h for details */
0083     __u8  encap_type;
0084     udp_tunnel_encap_rcv_t encap_rcv;
0085     udp_tunnel_encap_err_lookup_t encap_err_lookup;
0086     udp_tunnel_encap_err_rcv_t encap_err_rcv;
0087     udp_tunnel_encap_destroy_t encap_destroy;
0088     udp_tunnel_gro_receive_t gro_receive;
0089     udp_tunnel_gro_complete_t gro_complete;
0090 };
0091 
0092 /* Setup the given (UDP) sock to receive UDP encapsulated packets */
0093 void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
0094                struct udp_tunnel_sock_cfg *sock_cfg);
0095 
0096 /* -- List of parsable UDP tunnel types --
0097  *
0098  * Adding to this list will result in serious debate.  The main issue is
0099  * that this list is essentially a list of workarounds for either poorly
0100  * designed tunnels, or poorly designed device offloads.
0101  *
0102  * The parsing supported via these types should really be used for Rx
0103  * traffic only as the network stack will have already inserted offsets for
0104  * the location of the headers in the skb.  In addition any ports that are
0105  * pushed should be kept within the namespace without leaking to other
0106  * devices such as VFs or other ports on the same device.
0107  *
0108  * It is strongly encouraged to use CHECKSUM_COMPLETE for Rx to avoid the
0109  * need to use this for Rx checksum offload.  It should not be necessary to
0110  * call this function to perform Tx offloads on outgoing traffic.
0111  */
0112 enum udp_parsable_tunnel_type {
0113     UDP_TUNNEL_TYPE_VXLAN     = BIT(0), /* RFC 7348 */
0114     UDP_TUNNEL_TYPE_GENEVE    = BIT(1), /* draft-ietf-nvo3-geneve */
0115     UDP_TUNNEL_TYPE_VXLAN_GPE = BIT(2), /* draft-ietf-nvo3-vxlan-gpe */
0116 };
0117 
0118 struct udp_tunnel_info {
0119     unsigned short type;
0120     sa_family_t sa_family;
0121     __be16 port;
0122     u8 hw_priv;
0123 };
0124 
0125 /* Notify network devices of offloadable types */
0126 void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
0127                  unsigned short type);
0128 void udp_tunnel_drop_rx_port(struct net_device *dev, struct socket *sock,
0129                  unsigned short type);
0130 void udp_tunnel_notify_add_rx_port(struct socket *sock, unsigned short type);
0131 void udp_tunnel_notify_del_rx_port(struct socket *sock, unsigned short type);
0132 
0133 static inline void udp_tunnel_get_rx_info(struct net_device *dev)
0134 {
0135     ASSERT_RTNL();
0136     if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
0137         return;
0138     call_netdevice_notifiers(NETDEV_UDP_TUNNEL_PUSH_INFO, dev);
0139 }
0140 
0141 static inline void udp_tunnel_drop_rx_info(struct net_device *dev)
0142 {
0143     ASSERT_RTNL();
0144     if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
0145         return;
0146     call_netdevice_notifiers(NETDEV_UDP_TUNNEL_DROP_INFO, dev);
0147 }
0148 
0149 /* Transmit the skb using UDP encapsulation. */
0150 void udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
0151              __be32 src, __be32 dst, __u8 tos, __u8 ttl,
0152              __be16 df, __be16 src_port, __be16 dst_port,
0153              bool xnet, bool nocheck);
0154 
0155 int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
0156              struct sk_buff *skb,
0157              struct net_device *dev, struct in6_addr *saddr,
0158              struct in6_addr *daddr,
0159              __u8 prio, __u8 ttl, __be32 label,
0160              __be16 src_port, __be16 dst_port, bool nocheck);
0161 
0162 void udp_tunnel_sock_release(struct socket *sock);
0163 
0164 struct metadata_dst *udp_tun_rx_dst(struct sk_buff *skb, unsigned short family,
0165                     __be16 flags, __be64 tunnel_id,
0166                     int md_size);
0167 
0168 #ifdef CONFIG_INET
0169 static inline int udp_tunnel_handle_offloads(struct sk_buff *skb, bool udp_csum)
0170 {
0171     int type = udp_csum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
0172 
0173     return iptunnel_handle_offloads(skb, type);
0174 }
0175 #endif
0176 
0177 static inline void udp_tunnel_encap_enable(struct socket *sock)
0178 {
0179     struct udp_sock *up = udp_sk(sock->sk);
0180 
0181     if (up->encap_enabled)
0182         return;
0183 
0184     up->encap_enabled = 1;
0185 #if IS_ENABLED(CONFIG_IPV6)
0186     if (sock->sk->sk_family == PF_INET6)
0187         ipv6_stub->udpv6_encap_enable();
0188 #endif
0189     udp_encap_enable();
0190 }
0191 
0192 #define UDP_TUNNEL_NIC_MAX_TABLES   4
0193 
0194 enum udp_tunnel_nic_info_flags {
0195     /* Device callbacks may sleep */
0196     UDP_TUNNEL_NIC_INFO_MAY_SLEEP   = BIT(0),
0197     /* Device only supports offloads when it's open, all ports
0198      * will be removed before close and re-added after open.
0199      */
0200     UDP_TUNNEL_NIC_INFO_OPEN_ONLY   = BIT(1),
0201     /* Device supports only IPv4 tunnels */
0202     UDP_TUNNEL_NIC_INFO_IPV4_ONLY   = BIT(2),
0203     /* Device has hard-coded the IANA VXLAN port (4789) as VXLAN.
0204      * This port must not be counted towards n_entries of any table.
0205      * Driver will not receive any callback associated with port 4789.
0206      */
0207     UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN   = BIT(3),
0208 };
0209 
0210 struct udp_tunnel_nic;
0211 
0212 #define UDP_TUNNEL_NIC_MAX_SHARING_DEVICES  (U16_MAX / 2)
0213 
0214 struct udp_tunnel_nic_shared {
0215     struct udp_tunnel_nic *udp_tunnel_nic_info;
0216 
0217     struct list_head devices;
0218 };
0219 
0220 struct udp_tunnel_nic_shared_node {
0221     struct net_device *dev;
0222     struct list_head list;
0223 };
0224 
0225 /**
0226  * struct udp_tunnel_nic_info - driver UDP tunnel offload information
0227  * @set_port:   callback for adding a new port
0228  * @unset_port: callback for removing a port
0229  * @sync_table: callback for syncing the entire port table at once
0230  * @shared: reference to device global state (optional)
0231  * @flags:  device flags from enum udp_tunnel_nic_info_flags
0232  * @tables: UDP port tables this device has
0233  * @tables.n_entries:       number of entries in this table
0234  * @tables.tunnel_types:    types of tunnels this table accepts
0235  *
0236  * Drivers are expected to provide either @set_port and @unset_port callbacks
0237  * or the @sync_table callback. Callbacks are invoked with rtnl lock held.
0238  *
0239  * Devices which (misguidedly) share the UDP tunnel port table across multiple
0240  * netdevs should allocate an instance of struct udp_tunnel_nic_shared and
0241  * point @shared at it.
0242  * There must never be more than %UDP_TUNNEL_NIC_MAX_SHARING_DEVICES devices
0243  * sharing a table.
0244  *
0245  * Known limitations:
0246  *  - UDP tunnel port notifications are fundamentally best-effort -
0247  *    it is likely the driver will both see skbs which use a UDP tunnel port,
0248  *    while not being a tunneled skb, and tunnel skbs from other ports -
0249  *    drivers should only use these ports for non-critical RX-side offloads,
0250  *    e.g. the checksum offload;
0251  *  - none of the devices care about the socket family at present, so we don't
0252  *    track it. Please extend this code if you care.
0253  */
0254 struct udp_tunnel_nic_info {
0255     /* one-by-one */
0256     int (*set_port)(struct net_device *dev,
0257             unsigned int table, unsigned int entry,
0258             struct udp_tunnel_info *ti);
0259     int (*unset_port)(struct net_device *dev,
0260               unsigned int table, unsigned int entry,
0261               struct udp_tunnel_info *ti);
0262 
0263     /* all at once */
0264     int (*sync_table)(struct net_device *dev, unsigned int table);
0265 
0266     struct udp_tunnel_nic_shared *shared;
0267 
0268     unsigned int flags;
0269 
0270     struct udp_tunnel_nic_table_info {
0271         unsigned int n_entries;
0272         unsigned int tunnel_types;
0273     } tables[UDP_TUNNEL_NIC_MAX_TABLES];
0274 };
0275 
0276 /* UDP tunnel module dependencies
0277  *
0278  * Tunnel drivers are expected to have a hard dependency on the udp_tunnel
0279  * module. NIC drivers are not, they just attach their
0280  * struct udp_tunnel_nic_info to the netdev and wait for callbacks to come.
0281  * Loading a tunnel driver will cause the udp_tunnel module to be loaded
0282  * and only then will all the required state structures be allocated.
0283  * Since we want a weak dependency from the drivers and the core to udp_tunnel
0284  * we call things through the following stubs.
0285  */
0286 struct udp_tunnel_nic_ops {
0287     void (*get_port)(struct net_device *dev, unsigned int table,
0288              unsigned int idx, struct udp_tunnel_info *ti);
0289     void (*set_port_priv)(struct net_device *dev, unsigned int table,
0290                   unsigned int idx, u8 priv);
0291     void (*add_port)(struct net_device *dev, struct udp_tunnel_info *ti);
0292     void (*del_port)(struct net_device *dev, struct udp_tunnel_info *ti);
0293     void (*reset_ntf)(struct net_device *dev);
0294 
0295     size_t (*dump_size)(struct net_device *dev, unsigned int table);
0296     int (*dump_write)(struct net_device *dev, unsigned int table,
0297               struct sk_buff *skb);
0298 };
0299 
0300 #ifdef CONFIG_INET
0301 extern const struct udp_tunnel_nic_ops *udp_tunnel_nic_ops;
0302 #else
0303 #define udp_tunnel_nic_ops  ((struct udp_tunnel_nic_ops *)NULL)
0304 #endif
0305 
0306 static inline void
0307 udp_tunnel_nic_get_port(struct net_device *dev, unsigned int table,
0308             unsigned int idx, struct udp_tunnel_info *ti)
0309 {
0310     /* This helper is used from .sync_table, we indicate empty entries
0311      * by zero'ed @ti. Drivers which need to know the details of a port
0312      * when it gets deleted should use the .set_port / .unset_port
0313      * callbacks.
0314      * Zero out here, otherwise !CONFIG_INET causes uninitilized warnings.
0315      */
0316     memset(ti, 0, sizeof(*ti));
0317 
0318     if (udp_tunnel_nic_ops)
0319         udp_tunnel_nic_ops->get_port(dev, table, idx, ti);
0320 }
0321 
0322 static inline void
0323 udp_tunnel_nic_set_port_priv(struct net_device *dev, unsigned int table,
0324                  unsigned int idx, u8 priv)
0325 {
0326     if (udp_tunnel_nic_ops)
0327         udp_tunnel_nic_ops->set_port_priv(dev, table, idx, priv);
0328 }
0329 
0330 static inline void
0331 udp_tunnel_nic_add_port(struct net_device *dev, struct udp_tunnel_info *ti)
0332 {
0333     if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
0334         return;
0335     if (udp_tunnel_nic_ops)
0336         udp_tunnel_nic_ops->add_port(dev, ti);
0337 }
0338 
0339 static inline void
0340 udp_tunnel_nic_del_port(struct net_device *dev, struct udp_tunnel_info *ti)
0341 {
0342     if (!(dev->features & NETIF_F_RX_UDP_TUNNEL_PORT))
0343         return;
0344     if (udp_tunnel_nic_ops)
0345         udp_tunnel_nic_ops->del_port(dev, ti);
0346 }
0347 
0348 /**
0349  * udp_tunnel_nic_reset_ntf() - device-originating reset notification
0350  * @dev: network interface device structure
0351  *
0352  * Called by the driver to inform the core that the entire UDP tunnel port
0353  * state has been lost, usually due to device reset. Core will assume device
0354  * forgot all the ports and issue .set_port and .sync_table callbacks as
0355  * necessary.
0356  *
0357  * This function must be called with rtnl lock held, and will issue all
0358  * the callbacks before returning.
0359  */
0360 static inline void udp_tunnel_nic_reset_ntf(struct net_device *dev)
0361 {
0362     if (udp_tunnel_nic_ops)
0363         udp_tunnel_nic_ops->reset_ntf(dev);
0364 }
0365 
0366 static inline size_t
0367 udp_tunnel_nic_dump_size(struct net_device *dev, unsigned int table)
0368 {
0369     if (!udp_tunnel_nic_ops)
0370         return 0;
0371     return udp_tunnel_nic_ops->dump_size(dev, table);
0372 }
0373 
0374 static inline int
0375 udp_tunnel_nic_dump_write(struct net_device *dev, unsigned int table,
0376               struct sk_buff *skb)
0377 {
0378     if (!udp_tunnel_nic_ops)
0379         return 0;
0380     return udp_tunnel_nic_ops->dump_write(dev, table, skb);
0381 }
0382 #endif