Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 1 | #ifndef __NET_VXLAN_H |
| 2 | #define __NET_VXLAN_H 1 |
| 3 | |
Jesse Gross | 5f35227 | 2014-12-23 22:37:26 -0800 | [diff] [blame] | 4 | #include <linux/ip.h> |
| 5 | #include <linux/ipv6.h> |
| 6 | #include <linux/if_vlan.h> |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 7 | #include <linux/skbuff.h> |
| 8 | #include <linux/netdevice.h> |
| 9 | #include <linux/udp.h> |
| 10 | |
| 11 | #define VNI_HASH_BITS 10 |
| 12 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) |
| 13 | |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 14 | /* |
| 15 | * VXLAN Group Based Policy Extension: |
| 16 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 17 | * |1|-|-|-|1|-|-|-|R|D|R|R|A|R|R|R| Group Policy ID | |
| 18 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 19 | * | VXLAN Network Identifier (VNI) | Reserved | |
| 20 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 21 | * |
| 22 | * D = Don't Learn bit. When set, this bit indicates that the egress |
| 23 | * VTEP MUST NOT learn the source address of the encapsulated frame. |
| 24 | * |
| 25 | * A = Indicates that the group policy has already been applied to |
| 26 | * this packet. Policies MUST NOT be applied by devices when the |
| 27 | * A bit is set. |
| 28 | * |
| 29 | * [0] https://tools.ietf.org/html/draft-smith-vxlan-group-policy |
| 30 | */ |
| 31 | struct vxlanhdr_gbp { |
| 32 | __u8 vx_flags; |
| 33 | #ifdef __LITTLE_ENDIAN_BITFIELD |
| 34 | __u8 reserved_flags1:3, |
| 35 | policy_applied:1, |
| 36 | reserved_flags2:2, |
| 37 | dont_learn:1, |
| 38 | reserved_flags3:1; |
| 39 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 40 | __u8 reserved_flags1:1, |
| 41 | dont_learn:1, |
| 42 | reserved_flags2:2, |
| 43 | policy_applied:1, |
| 44 | reserved_flags3:3; |
| 45 | #else |
| 46 | #error "Please fix <asm/byteorder.h>" |
| 47 | #endif |
| 48 | __be16 policy_id; |
| 49 | __be32 vx_vni; |
| 50 | }; |
| 51 | |
| 52 | #define VXLAN_GBP_USED_BITS (VXLAN_HF_GBP | 0xFFFFFF) |
| 53 | |
| 54 | /* skb->mark mapping |
| 55 | * |
| 56 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 57 | * |R|R|R|R|R|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID | |
| 58 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 59 | */ |
| 60 | #define VXLAN_GBP_DONT_LEARN (BIT(6) << 16) |
| 61 | #define VXLAN_GBP_POLICY_APPLIED (BIT(3) << 16) |
| 62 | #define VXLAN_GBP_ID_MASK (0xFFFF) |
| 63 | |
| 64 | /* VXLAN protocol header: |
| 65 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | * |G|R|R|R|I|R|R|C| Reserved | |
| 67 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 68 | * | VXLAN Network Identifier (VNI) | Reserved | |
| 69 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | * |
| 71 | * G = 1 Group Policy (VXLAN-GBP) |
| 72 | * I = 1 VXLAN Network Identifier (VNI) present |
| 73 | * C = 1 Remote checksum offload (RCO) |
| 74 | */ |
Joe Stringer | 11bf782 | 2014-11-17 16:24:54 -0800 | [diff] [blame] | 75 | struct vxlanhdr { |
| 76 | __be32 vx_flags; |
| 77 | __be32 vx_vni; |
| 78 | }; |
| 79 | |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 80 | /* VXLAN header flags. */ |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 81 | #define VXLAN_HF_RCO BIT(24) |
| 82 | #define VXLAN_HF_VNI BIT(27) |
| 83 | #define VXLAN_HF_GBP BIT(31) |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 84 | |
| 85 | /* Remote checksum offload header option */ |
| 86 | #define VXLAN_RCO_MASK 0x7f /* Last byte of vni field */ |
| 87 | #define VXLAN_RCO_UDP 0x80 /* Indicate UDP RCO (TCP when not set *) */ |
| 88 | #define VXLAN_RCO_SHIFT 1 /* Left shift of start */ |
| 89 | #define VXLAN_RCO_SHIFT_MASK ((1 << VXLAN_RCO_SHIFT) - 1) |
| 90 | #define VXLAN_MAX_REMCSUM_START (VXLAN_RCO_MASK << VXLAN_RCO_SHIFT) |
Tom Herbert | 3bf3947 | 2015-01-08 12:31:18 -0800 | [diff] [blame] | 91 | |
| 92 | #define VXLAN_N_VID (1u << 24) |
| 93 | #define VXLAN_VID_MASK (VXLAN_N_VID - 1) |
| 94 | #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr)) |
| 95 | |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 96 | struct vxlan_metadata { |
| 97 | __be32 vni; |
| 98 | u32 gbp; |
| 99 | }; |
| 100 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 101 | struct vxlan_sock; |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 102 | typedef void (vxlan_rcv_t)(struct vxlan_sock *vh, struct sk_buff *skb, |
| 103 | struct vxlan_metadata *md); |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 104 | |
| 105 | /* per UDP socket information */ |
| 106 | struct vxlan_sock { |
| 107 | struct hlist_node hlist; |
| 108 | vxlan_rcv_t *rcv; |
| 109 | void *data; |
| 110 | struct work_struct del_work; |
| 111 | struct socket *sock; |
| 112 | struct rcu_head rcu; |
| 113 | struct hlist_head vni_list[VNI_HASH_SIZE]; |
| 114 | atomic_t refcnt; |
Or Gerlitz | dc01e7d | 2014-01-20 13:59:21 +0200 | [diff] [blame] | 115 | struct udp_offload udp_offloads; |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 116 | u32 flags; |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 119 | #define VXLAN_F_LEARN 0x01 |
| 120 | #define VXLAN_F_PROXY 0x02 |
| 121 | #define VXLAN_F_RSC 0x04 |
| 122 | #define VXLAN_F_L2MISS 0x08 |
| 123 | #define VXLAN_F_L3MISS 0x10 |
| 124 | #define VXLAN_F_IPV6 0x20 |
| 125 | #define VXLAN_F_UDP_CSUM 0x40 |
| 126 | #define VXLAN_F_UDP_ZERO_CSUM6_TX 0x80 |
| 127 | #define VXLAN_F_UDP_ZERO_CSUM6_RX 0x100 |
Tom Herbert | dfd8645 | 2015-01-12 17:00:38 -0800 | [diff] [blame] | 128 | #define VXLAN_F_REMCSUM_TX 0x200 |
| 129 | #define VXLAN_F_REMCSUM_RX 0x400 |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 130 | #define VXLAN_F_GBP 0x800 |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 131 | |
Thomas Graf | ac5132d | 2015-01-15 03:53:56 +0100 | [diff] [blame^] | 132 | /* These flags must match in order for a socket to be shareable */ |
| 133 | #define VXLAN_F_UNSHAREABLE VXLAN_F_GBP |
| 134 | |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 135 | struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port, |
| 136 | vxlan_rcv_t *rcv, void *data, |
Tom Herbert | 359a0ea | 2014-06-04 17:20:29 -0700 | [diff] [blame] | 137 | bool no_share, u32 flags); |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 138 | |
| 139 | void vxlan_sock_release(struct vxlan_sock *vs); |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 140 | |
Nicolas Dichtel | 1179618 | 2013-09-02 15:34:55 +0200 | [diff] [blame] | 141 | int vxlan_xmit_skb(struct vxlan_sock *vs, |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 142 | struct rtable *rt, struct sk_buff *skb, |
| 143 | __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, |
Thomas Graf | 3511494 | 2015-01-15 03:53:55 +0100 | [diff] [blame] | 144 | __be16 src_port, __be16 dst_port, struct vxlan_metadata *md, |
| 145 | bool xnet); |
Pravin B Shelar | 4956053 | 2013-08-19 11:23:17 -0700 | [diff] [blame] | 146 | |
Jesse Gross | 5f35227 | 2014-12-23 22:37:26 -0800 | [diff] [blame] | 147 | static inline netdev_features_t vxlan_features_check(struct sk_buff *skb, |
| 148 | netdev_features_t features) |
Joe Stringer | 11bf782 | 2014-11-17 16:24:54 -0800 | [diff] [blame] | 149 | { |
Jesse Gross | 5f35227 | 2014-12-23 22:37:26 -0800 | [diff] [blame] | 150 | u8 l4_hdr = 0; |
| 151 | |
| 152 | if (!skb->encapsulation) |
| 153 | return features; |
| 154 | |
| 155 | switch (vlan_get_protocol(skb)) { |
| 156 | case htons(ETH_P_IP): |
| 157 | l4_hdr = ip_hdr(skb)->protocol; |
| 158 | break; |
| 159 | case htons(ETH_P_IPV6): |
| 160 | l4_hdr = ipv6_hdr(skb)->nexthdr; |
| 161 | break; |
| 162 | default: |
| 163 | return features;; |
| 164 | } |
| 165 | |
| 166 | if ((l4_hdr == IPPROTO_UDP) && |
Joe Stringer | 11bf782 | 2014-11-17 16:24:54 -0800 | [diff] [blame] | 167 | (skb->inner_protocol_type != ENCAP_TYPE_ETHER || |
| 168 | skb->inner_protocol != htons(ETH_P_TEB) || |
| 169 | (skb_inner_mac_header(skb) - skb_transport_header(skb) != |
| 170 | sizeof(struct udphdr) + sizeof(struct vxlanhdr)))) |
Jesse Gross | 5f35227 | 2014-12-23 22:37:26 -0800 | [diff] [blame] | 171 | return features & ~(NETIF_F_ALL_CSUM | NETIF_F_GSO_MASK); |
Joe Stringer | 11bf782 | 2014-11-17 16:24:54 -0800 | [diff] [blame] | 172 | |
Jesse Gross | 5f35227 | 2014-12-23 22:37:26 -0800 | [diff] [blame] | 173 | return features; |
Joe Stringer | 11bf782 | 2014-11-17 16:24:54 -0800 | [diff] [blame] | 174 | } |
Joe Stringer | 23e62de | 2014-11-13 16:38:12 -0800 | [diff] [blame] | 175 | |
Joseph Gasparakis | e6cd988 | 2013-10-24 06:27:10 +0000 | [diff] [blame] | 176 | /* IP header + UDP + VXLAN + Ethernet header */ |
| 177 | #define VXLAN_HEADROOM (20 + 8 + 8 + 14) |
| 178 | /* IPv6 header + UDP + VXLAN + Ethernet header */ |
| 179 | #define VXLAN6_HEADROOM (40 + 8 + 8 + 14) |
| 180 | |
| 181 | #if IS_ENABLED(CONFIG_VXLAN) |
Joseph Gasparakis | 53cf5275 | 2013-09-04 02:13:38 -0700 | [diff] [blame] | 182 | void vxlan_get_rx_port(struct net_device *netdev); |
Joseph Gasparakis | e6cd988 | 2013-10-24 06:27:10 +0000 | [diff] [blame] | 183 | #else |
| 184 | static inline void vxlan_get_rx_port(struct net_device *netdev) |
| 185 | { |
| 186 | } |
| 187 | #endif |
Pravin B Shelar | 012a572 | 2013-08-19 11:23:07 -0700 | [diff] [blame] | 188 | #endif |