0001
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
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;
0082
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
0093 void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
0094 struct udp_tunnel_sock_cfg *sock_cfg);
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 enum udp_parsable_tunnel_type {
0113 UDP_TUNNEL_TYPE_VXLAN = BIT(0),
0114 UDP_TUNNEL_TYPE_GENEVE = BIT(1),
0115 UDP_TUNNEL_TYPE_VXLAN_GPE = BIT(2),
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
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
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
0196 UDP_TUNNEL_NIC_INFO_MAY_SLEEP = BIT(0),
0197
0198
0199
0200 UDP_TUNNEL_NIC_INFO_OPEN_ONLY = BIT(1),
0201
0202 UDP_TUNNEL_NIC_INFO_IPV4_ONLY = BIT(2),
0203
0204
0205
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
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254 struct udp_tunnel_nic_info {
0255
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
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
0277
0278
0279
0280
0281
0282
0283
0284
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
0311
0312
0313
0314
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
0350
0351
0352
0353
0354
0355
0356
0357
0358
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