0001
0002 #ifndef __NET_VXLAN_H
0003 #define __NET_VXLAN_H 1
0004
0005 #include <linux/if_vlan.h>
0006 #include <net/udp_tunnel.h>
0007 #include <net/dst_metadata.h>
0008 #include <net/rtnetlink.h>
0009 #include <net/switchdev.h>
0010 #include <net/nexthop.h>
0011
0012 #define IANA_VXLAN_UDP_PORT 4789
0013 #define IANA_VXLAN_GPE_UDP_PORT 4790
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 struct vxlanhdr {
0025 __be32 vx_flags;
0026 __be32 vx_vni;
0027 };
0028
0029
0030 #define VXLAN_HF_VNI cpu_to_be32(BIT(27))
0031
0032 #define VXLAN_N_VID (1u << 24)
0033 #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
0034 #define VXLAN_VNI_MASK cpu_to_be32(VXLAN_VID_MASK << 8)
0035 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
0036
0037 #define VNI_HASH_BITS 10
0038 #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
0039 #define FDB_HASH_BITS 8
0040 #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 #define VXLAN_HF_RCO cpu_to_be32(BIT(21))
0062
0063
0064 #define VXLAN_RCO_MASK cpu_to_be32(0x7f)
0065 #define VXLAN_RCO_UDP cpu_to_be32(0x80)
0066 #define VXLAN_RCO_SHIFT 1
0067 #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1)
0068 #define VXLAN_MAX_REMCSUM_START (0x7f << VXLAN_RCO_SHIFT)
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 struct vxlanhdr_gbp {
0090 u8 vx_flags;
0091 #ifdef __LITTLE_ENDIAN_BITFIELD
0092 u8 reserved_flags1:3,
0093 policy_applied:1,
0094 reserved_flags2:2,
0095 dont_learn:1,
0096 reserved_flags3:1;
0097 #elif defined(__BIG_ENDIAN_BITFIELD)
0098 u8 reserved_flags1:1,
0099 dont_learn:1,
0100 reserved_flags2:2,
0101 policy_applied:1,
0102 reserved_flags3:3;
0103 #else
0104 #error "Please fix <asm/byteorder.h>"
0105 #endif
0106 __be16 policy_id;
0107 __be32 vx_vni;
0108 };
0109
0110
0111 #define VXLAN_HF_GBP cpu_to_be32(BIT(31))
0112
0113 #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | cpu_to_be32(0xFFFFFF))
0114
0115
0116
0117
0118
0119
0120
0121 #define VXLAN_GBP_DONT_LEARN (BIT(6) << 16)
0122 #define VXLAN_GBP_POLICY_APPLIED (BIT(3) << 16)
0123 #define VXLAN_GBP_ID_MASK (0xFFFF)
0124
0125 #define VXLAN_GBP_MASK (VXLAN_GBP_DONT_LEARN | VXLAN_GBP_POLICY_APPLIED | \
0126 VXLAN_GBP_ID_MASK)
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 struct vxlanhdr_gpe {
0151 #if defined(__LITTLE_ENDIAN_BITFIELD)
0152 u8 oam_flag:1,
0153 reserved_flags1:1,
0154 np_applied:1,
0155 instance_applied:1,
0156 version:2,
0157 reserved_flags2:2;
0158 #elif defined(__BIG_ENDIAN_BITFIELD)
0159 u8 reserved_flags2:2,
0160 version:2,
0161 instance_applied:1,
0162 np_applied:1,
0163 reserved_flags1:1,
0164 oam_flag:1;
0165 #endif
0166 u8 reserved_flags3;
0167 u8 reserved_flags4;
0168 u8 next_protocol;
0169 __be32 vx_vni;
0170 };
0171
0172
0173 #define VXLAN_HF_VER cpu_to_be32(BIT(29) | BIT(28))
0174 #define VXLAN_HF_NP cpu_to_be32(BIT(26))
0175 #define VXLAN_HF_OAM cpu_to_be32(BIT(24))
0176
0177 #define VXLAN_GPE_USED_BITS (VXLAN_HF_VER | VXLAN_HF_NP | VXLAN_HF_OAM | \
0178 cpu_to_be32(0xff))
0179
0180 struct vxlan_metadata {
0181 u32 gbp;
0182 };
0183
0184
0185 struct vxlan_sock {
0186 struct hlist_node hlist;
0187 struct socket *sock;
0188 struct hlist_head vni_list[VNI_HASH_SIZE];
0189 refcount_t refcnt;
0190 u32 flags;
0191 };
0192
0193 union vxlan_addr {
0194 struct sockaddr_in sin;
0195 struct sockaddr_in6 sin6;
0196 struct sockaddr sa;
0197 };
0198
0199 struct vxlan_rdst {
0200 union vxlan_addr remote_ip;
0201 __be16 remote_port;
0202 u8 offloaded:1;
0203 __be32 remote_vni;
0204 u32 remote_ifindex;
0205 struct net_device *remote_dev;
0206 struct list_head list;
0207 struct rcu_head rcu;
0208 struct dst_cache dst_cache;
0209 };
0210
0211 struct vxlan_config {
0212 union vxlan_addr remote_ip;
0213 union vxlan_addr saddr;
0214 __be32 vni;
0215 int remote_ifindex;
0216 int mtu;
0217 __be16 dst_port;
0218 u16 port_min;
0219 u16 port_max;
0220 u8 tos;
0221 u8 ttl;
0222 __be32 label;
0223 u32 flags;
0224 unsigned long age_interval;
0225 unsigned int addrmax;
0226 bool no_share;
0227 enum ifla_vxlan_df df;
0228 };
0229
0230 enum {
0231 VXLAN_VNI_STATS_RX,
0232 VXLAN_VNI_STATS_RX_DROPS,
0233 VXLAN_VNI_STATS_RX_ERRORS,
0234 VXLAN_VNI_STATS_TX,
0235 VXLAN_VNI_STATS_TX_DROPS,
0236 VXLAN_VNI_STATS_TX_ERRORS,
0237 };
0238
0239 struct vxlan_vni_stats {
0240 u64 rx_packets;
0241 u64 rx_bytes;
0242 u64 rx_drops;
0243 u64 rx_errors;
0244 u64 tx_packets;
0245 u64 tx_bytes;
0246 u64 tx_drops;
0247 u64 tx_errors;
0248 };
0249
0250 struct vxlan_vni_stats_pcpu {
0251 struct vxlan_vni_stats stats;
0252 struct u64_stats_sync syncp;
0253 };
0254
0255 struct vxlan_dev_node {
0256 struct hlist_node hlist;
0257 struct vxlan_dev *vxlan;
0258 };
0259
0260 struct vxlan_vni_node {
0261 struct rhash_head vnode;
0262 struct vxlan_dev_node hlist4;
0263 #if IS_ENABLED(CONFIG_IPV6)
0264 struct vxlan_dev_node hlist6;
0265 #endif
0266 struct list_head vlist;
0267 __be32 vni;
0268 union vxlan_addr remote_ip;
0269 struct vxlan_vni_stats_pcpu __percpu *stats;
0270
0271 struct rcu_head rcu;
0272 };
0273
0274 struct vxlan_vni_group {
0275 struct rhashtable vni_hash;
0276 struct list_head vni_list;
0277 u32 num_vnis;
0278 };
0279
0280
0281 struct vxlan_dev {
0282 struct vxlan_dev_node hlist4;
0283 #if IS_ENABLED(CONFIG_IPV6)
0284 struct vxlan_dev_node hlist6;
0285 #endif
0286 struct list_head next;
0287 struct vxlan_sock __rcu *vn4_sock;
0288 #if IS_ENABLED(CONFIG_IPV6)
0289 struct vxlan_sock __rcu *vn6_sock;
0290 #endif
0291 struct net_device *dev;
0292 struct net *net;
0293 struct vxlan_rdst default_dst;
0294
0295 struct timer_list age_timer;
0296 spinlock_t hash_lock[FDB_HASH_SIZE];
0297 unsigned int addrcnt;
0298 struct gro_cells gro_cells;
0299
0300 struct vxlan_config cfg;
0301
0302 struct vxlan_vni_group __rcu *vnigrp;
0303
0304 struct hlist_head fdb_head[FDB_HASH_SIZE];
0305 };
0306
0307 #define VXLAN_F_LEARN 0x01
0308 #define VXLAN_F_PROXY 0x02
0309 #define VXLAN_F_RSC 0x04
0310 #define VXLAN_F_L2MISS 0x08
0311 #define VXLAN_F_L3MISS 0x10
0312 #define VXLAN_F_IPV6 0x20
0313 #define VXLAN_F_UDP_ZERO_CSUM_TX 0x40
0314 #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80
0315 #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100
0316 #define VXLAN_F_REMCSUM_TX 0x200
0317 #define VXLAN_F_REMCSUM_RX 0x400
0318 #define VXLAN_F_GBP 0x800
0319 #define VXLAN_F_REMCSUM_NOPARTIAL 0x1000
0320 #define VXLAN_F_COLLECT_METADATA 0x2000
0321 #define VXLAN_F_GPE 0x4000
0322 #define VXLAN_F_IPV6_LINKLOCAL 0x8000
0323 #define VXLAN_F_TTL_INHERIT 0x10000
0324 #define VXLAN_F_VNIFILTER 0x20000
0325
0326
0327
0328
0329 #define VXLAN_F_RCV_FLAGS (VXLAN_F_GBP | \
0330 VXLAN_F_GPE | \
0331 VXLAN_F_UDP_ZERO_CSUM6_RX | \
0332 VXLAN_F_REMCSUM_RX | \
0333 VXLAN_F_REMCSUM_NOPARTIAL | \
0334 VXLAN_F_COLLECT_METADATA | \
0335 VXLAN_F_VNIFILTER)
0336
0337
0338 #define VXLAN_F_ALLOWED_GPE (VXLAN_F_GPE | \
0339 VXLAN_F_IPV6 | \
0340 VXLAN_F_IPV6_LINKLOCAL | \
0341 VXLAN_F_UDP_ZERO_CSUM_TX | \
0342 VXLAN_F_UDP_ZERO_CSUM6_TX | \
0343 VXLAN_F_UDP_ZERO_CSUM6_RX | \
0344 VXLAN_F_COLLECT_METADATA | \
0345 VXLAN_F_VNIFILTER)
0346
0347 struct net_device *vxlan_dev_create(struct net *net, const char *name,
0348 u8 name_assign_type, struct vxlan_config *conf);
0349
0350 static inline netdev_features_t vxlan_features_check(struct sk_buff *skb,
0351 netdev_features_t features)
0352 {
0353 u8 l4_hdr = 0;
0354
0355 if (!skb->encapsulation)
0356 return features;
0357
0358 switch (vlan_get_protocol(skb)) {
0359 case htons(ETH_P_IP):
0360 l4_hdr = ip_hdr(skb)->protocol;
0361 break;
0362 case htons(ETH_P_IPV6):
0363 l4_hdr = ipv6_hdr(skb)->nexthdr;
0364 break;
0365 default:
0366 return features;
0367 }
0368
0369 if ((l4_hdr == IPPROTO_UDP) &&
0370 (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
0371 skb->inner_protocol != htons(ETH_P_TEB) ||
0372 (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
0373 sizeof(struct udphdr) + sizeof(struct vxlanhdr)) ||
0374 (skb->ip_summed != CHECKSUM_NONE &&
0375 !can_checksum_protocol(features, inner_eth_hdr(skb)->h_proto))))
0376 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
0377
0378 return features;
0379 }
0380
0381
0382 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
0383
0384 #define VXLAN6_HEADROOM (40 + 8 + 8 + 14)
0385
0386 static inline struct vxlanhdr *vxlan_hdr(struct sk_buff *skb)
0387 {
0388 return (struct vxlanhdr *)(udp_hdr(skb) + 1);
0389 }
0390
0391 static inline __be32 vxlan_vni(__be32 vni_field)
0392 {
0393 #if defined(__BIG_ENDIAN)
0394 return (__force __be32)((__force u32)vni_field >> 8);
0395 #else
0396 return (__force __be32)((__force u32)(vni_field & VXLAN_VNI_MASK) << 8);
0397 #endif
0398 }
0399
0400 static inline __be32 vxlan_vni_field(__be32 vni)
0401 {
0402 #if defined(__BIG_ENDIAN)
0403 return (__force __be32)((__force u32)vni << 8);
0404 #else
0405 return (__force __be32)((__force u32)vni >> 8);
0406 #endif
0407 }
0408
0409 static inline size_t vxlan_rco_start(__be32 vni_field)
0410 {
0411 return be32_to_cpu(vni_field & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
0412 }
0413
0414 static inline size_t vxlan_rco_offset(__be32 vni_field)
0415 {
0416 return (vni_field & VXLAN_RCO_UDP) ?
0417 offsetof(struct udphdr, check) :
0418 offsetof(struct tcphdr, check);
0419 }
0420
0421 static inline __be32 vxlan_compute_rco(unsigned int start, unsigned int offset)
0422 {
0423 __be32 vni_field = cpu_to_be32(start >> VXLAN_RCO_SHIFT);
0424
0425 if (offset == offsetof(struct udphdr, check))
0426 vni_field |= VXLAN_RCO_UDP;
0427 return vni_field;
0428 }
0429
0430 static inline unsigned short vxlan_get_sk_family(struct vxlan_sock *vs)
0431 {
0432 return vs->sock->sk->sk_family;
0433 }
0434
0435 #if IS_ENABLED(CONFIG_IPV6)
0436
0437 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
0438 {
0439 if (ipa->sa.sa_family == AF_INET6)
0440 return ipv6_addr_any(&ipa->sin6.sin6_addr);
0441 else
0442 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
0443 }
0444
0445 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
0446 {
0447 if (ipa->sa.sa_family == AF_INET6)
0448 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
0449 else
0450 return ipv4_is_multicast(ipa->sin.sin_addr.s_addr);
0451 }
0452
0453 #else
0454
0455 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
0456 {
0457 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
0458 }
0459
0460 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
0461 {
0462 return ipv4_is_multicast(ipa->sin.sin_addr.s_addr);
0463 }
0464
0465 #endif
0466
0467 static inline bool netif_is_vxlan(const struct net_device *dev)
0468 {
0469 return dev->rtnl_link_ops &&
0470 !strcmp(dev->rtnl_link_ops->kind, "vxlan");
0471 }
0472
0473 struct switchdev_notifier_vxlan_fdb_info {
0474 struct switchdev_notifier_info info;
0475 union vxlan_addr remote_ip;
0476 __be16 remote_port;
0477 __be32 remote_vni;
0478 u32 remote_ifindex;
0479 u8 eth_addr[ETH_ALEN];
0480 __be32 vni;
0481 bool offloaded;
0482 bool added_by_user;
0483 };
0484
0485 #if IS_ENABLED(CONFIG_VXLAN)
0486 int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
0487 struct switchdev_notifier_vxlan_fdb_info *fdb_info);
0488 int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
0489 struct notifier_block *nb,
0490 struct netlink_ext_ack *extack);
0491 void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni);
0492
0493 #else
0494 static inline int
0495 vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
0496 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
0497 {
0498 return -ENOENT;
0499 }
0500
0501 static inline int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
0502 struct notifier_block *nb,
0503 struct netlink_ext_ack *extack)
0504 {
0505 return -EOPNOTSUPP;
0506 }
0507
0508 static inline void
0509 vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
0510 {
0511 }
0512 #endif
0513
0514 static inline void vxlan_flag_attr_error(int attrtype,
0515 struct netlink_ext_ack *extack)
0516 {
0517 #define VXLAN_FLAG(flg) \
0518 case IFLA_VXLAN_##flg: \
0519 NL_SET_ERR_MSG_MOD(extack, \
0520 "cannot change " #flg " flag"); \
0521 break
0522 switch (attrtype) {
0523 VXLAN_FLAG(TTL_INHERIT);
0524 VXLAN_FLAG(LEARNING);
0525 VXLAN_FLAG(PROXY);
0526 VXLAN_FLAG(RSC);
0527 VXLAN_FLAG(L2MISS);
0528 VXLAN_FLAG(L3MISS);
0529 VXLAN_FLAG(COLLECT_METADATA);
0530 VXLAN_FLAG(UDP_ZERO_CSUM6_TX);
0531 VXLAN_FLAG(UDP_ZERO_CSUM6_RX);
0532 VXLAN_FLAG(REMCSUM_TX);
0533 VXLAN_FLAG(REMCSUM_RX);
0534 VXLAN_FLAG(GBP);
0535 VXLAN_FLAG(GPE);
0536 VXLAN_FLAG(REMCSUM_NOPARTIAL);
0537 default:
0538 NL_SET_ERR_MSG_MOD(extack, \
0539 "cannot change flag");
0540 break;
0541 }
0542 #undef VXLAN_FLAG
0543 }
0544
0545 static inline bool vxlan_fdb_nh_path_select(struct nexthop *nh,
0546 int hash,
0547 struct vxlan_rdst *rdst)
0548 {
0549 struct fib_nh_common *nhc;
0550
0551 nhc = nexthop_path_fdb_result(nh, hash);
0552 if (unlikely(!nhc))
0553 return false;
0554
0555 switch (nhc->nhc_gw_family) {
0556 case AF_INET:
0557 rdst->remote_ip.sin.sin_addr.s_addr = nhc->nhc_gw.ipv4;
0558 rdst->remote_ip.sa.sa_family = AF_INET;
0559 break;
0560 case AF_INET6:
0561 rdst->remote_ip.sin6.sin6_addr = nhc->nhc_gw.ipv6;
0562 rdst->remote_ip.sa.sa_family = AF_INET6;
0563 break;
0564 }
0565
0566 return true;
0567 }
0568
0569 #endif