]>
Commit | Line | Data |
---|---|---|
d342894c | 1 | /* |
eb5ce439 | 2 | * VXLAN: Virtual eXtensible Local Area Network |
d342894c | 3 | * |
3b8df3c6 | 4 | * Copyright (c) 2012-2013 Vyatta Inc. |
d342894c | 5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
d342894c | 9 | */ |
10 | ||
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
12 | ||
13 | #include <linux/kernel.h> | |
d342894c | 14 | #include <linux/module.h> |
15 | #include <linux/errno.h> | |
16 | #include <linux/slab.h> | |
d342894c | 17 | #include <linux/udp.h> |
18 | #include <linux/igmp.h> | |
d342894c | 19 | #include <linux/if_ether.h> |
1b13c97f | 20 | #include <linux/ethtool.h> |
e4f67add DS |
21 | #include <net/arp.h> |
22 | #include <net/ndisc.h> | |
d342894c | 23 | #include <net/ip.h> |
24 | #include <net/icmp.h> | |
d342894c | 25 | #include <net/rtnetlink.h> |
d342894c | 26 | #include <net/inet_ecn.h> |
27 | #include <net/net_namespace.h> | |
28 | #include <net/netns/generic.h> | |
012a5729 | 29 | #include <net/vxlan.h> |
40d29af0 | 30 | |
e4c7ed41 | 31 | #if IS_ENABLED(CONFIG_IPV6) |
e4c7ed41 | 32 | #include <net/ip6_tunnel.h> |
660d98ca | 33 | #include <net/ip6_checksum.h> |
e4c7ed41 | 34 | #endif |
d342894c | 35 | |
36 | #define VXLAN_VERSION "0.1" | |
37 | ||
553675fb | 38 | #define PORT_HASH_BITS 8 |
39 | #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) | |
d342894c | 40 | #define FDB_AGE_DEFAULT 300 /* 5 min */ |
41 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ | |
42 | ||
23c578bf | 43 | /* UDP port for VXLAN traffic. |
44 | * The IANA assigned port is 4789, but the Linux default is 8472 | |
234f5b73 | 45 | * for compatibility with early adopters. |
23c578bf | 46 | */ |
9daaa397 SH |
47 | static unsigned short vxlan_port __read_mostly = 8472; |
48 | module_param_named(udp_port, vxlan_port, ushort, 0444); | |
d342894c | 49 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); |
50 | ||
51 | static bool log_ecn_error = true; | |
52 | module_param(log_ecn_error, bool, 0644); | |
53 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
54 | ||
c7d03a00 | 55 | static unsigned int vxlan_net_id; |
0dfbdf41 | 56 | static struct rtnl_link_ops vxlan_link_ops; |
553675fb | 57 | |
7256eac1 | 58 | static const u8 all_zeros_mac[ETH_ALEN + 2]; |
afbd8bae | 59 | |
205f356d | 60 | static int vxlan_sock_add(struct vxlan_dev *vxlan); |
614732ea | 61 | |
a53cb29b MB |
62 | static void vxlan_vs_del_dev(struct vxlan_dev *vxlan); |
63 | ||
553675fb | 64 | /* per-network namespace private data for this module */ |
65 | struct vxlan_net { | |
66 | struct list_head vxlan_list; | |
67 | struct hlist_head sock_list[PORT_HASH_SIZE]; | |
1c51a915 | 68 | spinlock_t sock_lock; |
553675fb | 69 | }; |
70 | ||
d342894c | 71 | /* Forwarding table entry */ |
72 | struct vxlan_fdb { | |
73 | struct hlist_node hlist; /* linked list of entries */ | |
74 | struct rcu_head rcu; | |
75 | unsigned long updated; /* jiffies */ | |
76 | unsigned long used; | |
3e61aa8f | 77 | struct list_head remotes; |
7177a3b0 | 78 | u8 eth_addr[ETH_ALEN]; |
d342894c | 79 | u16 state; /* see ndm_state */ |
3ad7a4b1 | 80 | __be32 vni; |
ae884082 | 81 | u8 flags; /* see ndm_flags */ |
d342894c | 82 | }; |
83 | ||
d342894c | 84 | /* salt for hash table */ |
85 | static u32 vxlan_salt __read_mostly; | |
86 | ||
ee122c79 TG |
87 | static inline bool vxlan_collect_metadata(struct vxlan_sock *vs) |
88 | { | |
e7030878 TG |
89 | return vs->flags & VXLAN_F_COLLECT_METADATA || |
90 | ip_tunnel_collect_metadata(); | |
ee122c79 TG |
91 | } |
92 | ||
e4c7ed41 CW |
93 | #if IS_ENABLED(CONFIG_IPV6) |
94 | static inline | |
95 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) | |
96 | { | |
f0ef3126 JB |
97 | if (a->sa.sa_family != b->sa.sa_family) |
98 | return false; | |
99 | if (a->sa.sa_family == AF_INET6) | |
100 | return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr); | |
101 | else | |
102 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; | |
e4c7ed41 CW |
103 | } |
104 | ||
105 | static inline bool vxlan_addr_any(const union vxlan_addr *ipa) | |
106 | { | |
f0ef3126 JB |
107 | if (ipa->sa.sa_family == AF_INET6) |
108 | return ipv6_addr_any(&ipa->sin6.sin6_addr); | |
109 | else | |
110 | return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); | |
e4c7ed41 CW |
111 | } |
112 | ||
113 | static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) | |
114 | { | |
f0ef3126 JB |
115 | if (ipa->sa.sa_family == AF_INET6) |
116 | return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr); | |
117 | else | |
118 | return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); | |
e4c7ed41 CW |
119 | } |
120 | ||
121 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) | |
122 | { | |
f0ef3126 | 123 | if (nla_len(nla) >= sizeof(struct in6_addr)) { |
67b61f6c | 124 | ip->sin6.sin6_addr = nla_get_in6_addr(nla); |
f0ef3126 JB |
125 | ip->sa.sa_family = AF_INET6; |
126 | return 0; | |
127 | } else if (nla_len(nla) >= sizeof(__be32)) { | |
67b61f6c | 128 | ip->sin.sin_addr.s_addr = nla_get_in_addr(nla); |
f0ef3126 JB |
129 | ip->sa.sa_family = AF_INET; |
130 | return 0; | |
131 | } else { | |
132 | return -EAFNOSUPPORT; | |
133 | } | |
e4c7ed41 CW |
134 | } |
135 | ||
136 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, | |
f0ef3126 | 137 | const union vxlan_addr *ip) |
e4c7ed41 | 138 | { |
f0ef3126 | 139 | if (ip->sa.sa_family == AF_INET6) |
930345ea | 140 | return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr); |
f0ef3126 | 141 | else |
930345ea | 142 | return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr); |
e4c7ed41 CW |
143 | } |
144 | ||
145 | #else /* !CONFIG_IPV6 */ | |
146 | ||
147 | static inline | |
148 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) | |
149 | { | |
f0ef3126 | 150 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; |
e4c7ed41 CW |
151 | } |
152 | ||
153 | static inline bool vxlan_addr_any(const union vxlan_addr *ipa) | |
154 | { | |
f0ef3126 | 155 | return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); |
e4c7ed41 CW |
156 | } |
157 | ||
158 | static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) | |
159 | { | |
f0ef3126 | 160 | return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); |
e4c7ed41 CW |
161 | } |
162 | ||
163 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) | |
164 | { | |
f0ef3126 JB |
165 | if (nla_len(nla) >= sizeof(struct in6_addr)) { |
166 | return -EAFNOSUPPORT; | |
167 | } else if (nla_len(nla) >= sizeof(__be32)) { | |
67b61f6c | 168 | ip->sin.sin_addr.s_addr = nla_get_in_addr(nla); |
f0ef3126 JB |
169 | ip->sa.sa_family = AF_INET; |
170 | return 0; | |
171 | } else { | |
172 | return -EAFNOSUPPORT; | |
173 | } | |
e4c7ed41 CW |
174 | } |
175 | ||
176 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, | |
f0ef3126 | 177 | const union vxlan_addr *ip) |
e4c7ed41 | 178 | { |
930345ea | 179 | return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr); |
e4c7ed41 CW |
180 | } |
181 | #endif | |
182 | ||
553675fb | 183 | /* Virtual Network hash table head */ |
54bfd872 | 184 | static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni) |
553675fb | 185 | { |
54bfd872 | 186 | return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)]; |
553675fb | 187 | } |
188 | ||
189 | /* Socket hash table head */ | |
190 | static inline struct hlist_head *vs_head(struct net *net, __be16 port) | |
d342894c | 191 | { |
192 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
193 | ||
553675fb | 194 | return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; |
195 | } | |
196 | ||
3e61aa8f SH |
197 | /* First remote destination for a forwarding entry. |
198 | * Guaranteed to be non-NULL because remotes are never deleted. | |
199 | */ | |
5ca5461c | 200 | static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb) |
3e61aa8f | 201 | { |
5ca5461c | 202 | return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list); |
203 | } | |
204 | ||
205 | static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb) | |
206 | { | |
207 | return list_first_entry(&fdb->remotes, struct vxlan_rdst, list); | |
3e61aa8f SH |
208 | } |
209 | ||
ac5132d1 TG |
210 | /* Find VXLAN socket based on network namespace, address family and UDP port |
211 | * and enabled unshareable flags. | |
212 | */ | |
213 | static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family, | |
214 | __be16 port, u32 flags) | |
553675fb | 215 | { |
216 | struct vxlan_sock *vs; | |
af33c1ad TH |
217 | |
218 | flags &= VXLAN_F_RCV_FLAGS; | |
553675fb | 219 | |
220 | hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { | |
19ca9fc1 | 221 | if (inet_sk(vs->sock->sk)->inet_sport == port && |
705cc62f | 222 | vxlan_get_sk_family(vs) == family && |
af33c1ad | 223 | vs->flags == flags) |
553675fb | 224 | return vs; |
225 | } | |
226 | return NULL; | |
d342894c | 227 | } |
228 | ||
49f810f0 MS |
229 | static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex, |
230 | __be32 vni) | |
d342894c | 231 | { |
69e76661 | 232 | struct vxlan_dev_node *node; |
d342894c | 233 | |
b9167b2e JB |
234 | /* For flow based devices, map all packets to VNI 0 */ |
235 | if (vs->flags & VXLAN_F_COLLECT_METADATA) | |
236 | vni = 0; | |
237 | ||
69e76661 JB |
238 | hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) { |
239 | if (node->vxlan->default_dst.remote_vni != vni) | |
49f810f0 MS |
240 | continue; |
241 | ||
242 | if (IS_ENABLED(CONFIG_IPV6)) { | |
69e76661 | 243 | const struct vxlan_config *cfg = &node->vxlan->cfg; |
49f810f0 MS |
244 | |
245 | if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) && | |
246 | cfg->remote_ifindex != ifindex) | |
247 | continue; | |
248 | } | |
249 | ||
69e76661 | 250 | return node->vxlan; |
d342894c | 251 | } |
252 | ||
253 | return NULL; | |
254 | } | |
255 | ||
5cfccc5a | 256 | /* Look up VNI in a per net namespace table */ |
49f810f0 MS |
257 | static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex, |
258 | __be32 vni, sa_family_t family, | |
259 | __be16 port, u32 flags) | |
5cfccc5a PS |
260 | { |
261 | struct vxlan_sock *vs; | |
262 | ||
ac5132d1 | 263 | vs = vxlan_find_sock(net, family, port, flags); |
5cfccc5a PS |
264 | if (!vs) |
265 | return NULL; | |
266 | ||
49f810f0 | 267 | return vxlan_vs_find_vni(vs, ifindex, vni); |
5cfccc5a PS |
268 | } |
269 | ||
d342894c | 270 | /* Fill in neighbour message in skbuff. */ |
271 | static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, | |
234f5b73 SH |
272 | const struct vxlan_fdb *fdb, |
273 | u32 portid, u32 seq, int type, unsigned int flags, | |
274 | const struct vxlan_rdst *rdst) | |
d342894c | 275 | { |
276 | unsigned long now = jiffies; | |
277 | struct nda_cacheinfo ci; | |
278 | struct nlmsghdr *nlh; | |
279 | struct ndmsg *ndm; | |
e4f67add | 280 | bool send_ip, send_eth; |
d342894c | 281 | |
282 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); | |
283 | if (nlh == NULL) | |
284 | return -EMSGSIZE; | |
285 | ||
286 | ndm = nlmsg_data(nlh); | |
287 | memset(ndm, 0, sizeof(*ndm)); | |
e4f67add DS |
288 | |
289 | send_eth = send_ip = true; | |
290 | ||
291 | if (type == RTM_GETNEIGH) { | |
e4c7ed41 | 292 | send_ip = !vxlan_addr_any(&rdst->remote_ip); |
e4f67add | 293 | send_eth = !is_zero_ether_addr(fdb->eth_addr); |
8f48ba71 | 294 | ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET; |
e4f67add DS |
295 | } else |
296 | ndm->ndm_family = AF_BRIDGE; | |
d342894c | 297 | ndm->ndm_state = fdb->state; |
298 | ndm->ndm_ifindex = vxlan->dev->ifindex; | |
ae884082 | 299 | ndm->ndm_flags = fdb->flags; |
545469f7 | 300 | ndm->ndm_type = RTN_UNICAST; |
d342894c | 301 | |
193523bf | 302 | if (!net_eq(dev_net(vxlan->dev), vxlan->net) && |
4967082b | 303 | nla_put_s32(skb, NDA_LINK_NETNSID, |
38f507f1 | 304 | peernet2id(dev_net(vxlan->dev), vxlan->net))) |
193523bf ND |
305 | goto nla_put_failure; |
306 | ||
e4f67add | 307 | if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) |
d342894c | 308 | goto nla_put_failure; |
309 | ||
e4c7ed41 | 310 | if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip)) |
6681712d DS |
311 | goto nla_put_failure; |
312 | ||
0dfbdf41 | 313 | if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port && |
6681712d DS |
314 | nla_put_be16(skb, NDA_PORT, rdst->remote_port)) |
315 | goto nla_put_failure; | |
c7995c43 | 316 | if (rdst->remote_vni != vxlan->default_dst.remote_vni && |
54bfd872 | 317 | nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni))) |
6681712d | 318 | goto nla_put_failure; |
dc5321d7 | 319 | if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni && |
3ad7a4b1 RP |
320 | nla_put_u32(skb, NDA_SRC_VNI, |
321 | be32_to_cpu(fdb->vni))) | |
322 | goto nla_put_failure; | |
6681712d DS |
323 | if (rdst->remote_ifindex && |
324 | nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) | |
d342894c | 325 | goto nla_put_failure; |
326 | ||
327 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); | |
328 | ci.ndm_confirmed = 0; | |
329 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); | |
330 | ci.ndm_refcnt = 0; | |
331 | ||
332 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) | |
333 | goto nla_put_failure; | |
334 | ||
053c095a JB |
335 | nlmsg_end(skb, nlh); |
336 | return 0; | |
d342894c | 337 | |
338 | nla_put_failure: | |
339 | nlmsg_cancel(skb, nlh); | |
340 | return -EMSGSIZE; | |
341 | } | |
342 | ||
343 | static inline size_t vxlan_nlmsg_size(void) | |
344 | { | |
345 | return NLMSG_ALIGN(sizeof(struct ndmsg)) | |
346 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ | |
e4c7ed41 | 347 | + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */ |
73cf3317 | 348 | + nla_total_size(sizeof(__be16)) /* NDA_PORT */ |
6681712d DS |
349 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ |
350 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ | |
4967082b | 351 | + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */ |
d342894c | 352 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
353 | } | |
354 | ||
9e4b93f9 ND |
355 | static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, |
356 | struct vxlan_rdst *rd, int type) | |
d342894c | 357 | { |
358 | struct net *net = dev_net(vxlan->dev); | |
359 | struct sk_buff *skb; | |
360 | int err = -ENOBUFS; | |
361 | ||
362 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); | |
363 | if (skb == NULL) | |
364 | goto errout; | |
365 | ||
9e4b93f9 | 366 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd); |
d342894c | 367 | if (err < 0) { |
368 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ | |
369 | WARN_ON(err == -EMSGSIZE); | |
370 | kfree_skb(skb); | |
371 | goto errout; | |
372 | } | |
373 | ||
374 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); | |
375 | return; | |
376 | errout: | |
377 | if (err < 0) | |
378 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); | |
379 | } | |
380 | ||
e4c7ed41 | 381 | static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa) |
e4f67add DS |
382 | { |
383 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
bb3fd687 SH |
384 | struct vxlan_fdb f = { |
385 | .state = NUD_STALE, | |
386 | }; | |
387 | struct vxlan_rdst remote = { | |
e4c7ed41 | 388 | .remote_ip = *ipa, /* goes to NDA_DST */ |
54bfd872 | 389 | .remote_vni = cpu_to_be32(VXLAN_N_VID), |
bb3fd687 | 390 | }; |
3e61aa8f | 391 | |
9e4b93f9 | 392 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH); |
e4f67add DS |
393 | } |
394 | ||
395 | static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) | |
396 | { | |
bb3fd687 SH |
397 | struct vxlan_fdb f = { |
398 | .state = NUD_STALE, | |
399 | }; | |
9e4b93f9 | 400 | struct vxlan_rdst remote = { }; |
e4f67add | 401 | |
e4f67add DS |
402 | memcpy(f.eth_addr, eth_addr, ETH_ALEN); |
403 | ||
9e4b93f9 | 404 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH); |
e4f67add DS |
405 | } |
406 | ||
d342894c | 407 | /* Hash Ethernet address */ |
408 | static u32 eth_hash(const unsigned char *addr) | |
409 | { | |
410 | u64 value = get_unaligned((u64 *)addr); | |
411 | ||
412 | /* only want 6 bytes */ | |
413 | #ifdef __BIG_ENDIAN | |
d342894c | 414 | value >>= 16; |
321fb991 | 415 | #else |
416 | value <<= 16; | |
d342894c | 417 | #endif |
418 | return hash_64(value, FDB_HASH_BITS); | |
419 | } | |
420 | ||
3ad7a4b1 RP |
421 | static u32 eth_vni_hash(const unsigned char *addr, __be32 vni) |
422 | { | |
423 | /* use 1 byte of OUI and 3 bytes of NIC */ | |
424 | u32 key = get_unaligned((u32 *)(addr + 2)); | |
425 | ||
426 | return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1); | |
427 | } | |
428 | ||
d342894c | 429 | /* Hash chain to use given mac address */ |
430 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, | |
3ad7a4b1 | 431 | const u8 *mac, __be32 vni) |
d342894c | 432 | { |
dc5321d7 | 433 | if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) |
3ad7a4b1 RP |
434 | return &vxlan->fdb_head[eth_vni_hash(mac, vni)]; |
435 | else | |
436 | return &vxlan->fdb_head[eth_hash(mac)]; | |
d342894c | 437 | } |
438 | ||
439 | /* Look up Ethernet address in forwarding table */ | |
014be2c8 | 440 | static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan, |
3ad7a4b1 | 441 | const u8 *mac, __be32 vni) |
d342894c | 442 | { |
3ad7a4b1 | 443 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni); |
d342894c | 444 | struct vxlan_fdb *f; |
d342894c | 445 | |
b67bfe0d | 446 | hlist_for_each_entry_rcu(f, head, hlist) { |
3ad7a4b1 | 447 | if (ether_addr_equal(mac, f->eth_addr)) { |
dc5321d7 | 448 | if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) { |
3ad7a4b1 RP |
449 | if (vni == f->vni) |
450 | return f; | |
451 | } else { | |
452 | return f; | |
453 | } | |
454 | } | |
d342894c | 455 | } |
456 | ||
457 | return NULL; | |
458 | } | |
459 | ||
014be2c8 | 460 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, |
3ad7a4b1 | 461 | const u8 *mac, __be32 vni) |
014be2c8 SS |
462 | { |
463 | struct vxlan_fdb *f; | |
464 | ||
3ad7a4b1 | 465 | f = __vxlan_find_mac(vxlan, mac, vni); |
014be2c8 SS |
466 | if (f) |
467 | f->used = jiffies; | |
468 | ||
469 | return f; | |
470 | } | |
471 | ||
a5e7c10a MR |
472 | /* caller should hold vxlan->hash_lock */ |
473 | static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, | |
e4c7ed41 | 474 | union vxlan_addr *ip, __be16 port, |
54bfd872 | 475 | __be32 vni, __u32 ifindex) |
6681712d | 476 | { |
3e61aa8f | 477 | struct vxlan_rdst *rd; |
6681712d | 478 | |
3e61aa8f | 479 | list_for_each_entry(rd, &f->remotes, list) { |
e4c7ed41 | 480 | if (vxlan_addr_equal(&rd->remote_ip, ip) && |
6681712d DS |
481 | rd->remote_port == port && |
482 | rd->remote_vni == vni && | |
483 | rd->remote_ifindex == ifindex) | |
a5e7c10a | 484 | return rd; |
6681712d | 485 | } |
3e61aa8f | 486 | |
a5e7c10a MR |
487 | return NULL; |
488 | } | |
489 | ||
906dc186 TR |
490 | /* Replace destination of unicast mac */ |
491 | static int vxlan_fdb_replace(struct vxlan_fdb *f, | |
54bfd872 JB |
492 | union vxlan_addr *ip, __be16 port, __be32 vni, |
493 | __u32 ifindex) | |
906dc186 TR |
494 | { |
495 | struct vxlan_rdst *rd; | |
496 | ||
497 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); | |
498 | if (rd) | |
499 | return 0; | |
500 | ||
501 | rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list); | |
502 | if (!rd) | |
503 | return 0; | |
0c1d70af PA |
504 | |
505 | dst_cache_reset(&rd->dst_cache); | |
e4c7ed41 | 506 | rd->remote_ip = *ip; |
906dc186 TR |
507 | rd->remote_port = port; |
508 | rd->remote_vni = vni; | |
509 | rd->remote_ifindex = ifindex; | |
510 | return 1; | |
511 | } | |
512 | ||
a5e7c10a MR |
513 | /* Add/update destinations for multicast */ |
514 | static int vxlan_fdb_append(struct vxlan_fdb *f, | |
54bfd872 | 515 | union vxlan_addr *ip, __be16 port, __be32 vni, |
9e4b93f9 | 516 | __u32 ifindex, struct vxlan_rdst **rdp) |
a5e7c10a MR |
517 | { |
518 | struct vxlan_rdst *rd; | |
519 | ||
520 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); | |
521 | if (rd) | |
522 | return 0; | |
523 | ||
6681712d DS |
524 | rd = kmalloc(sizeof(*rd), GFP_ATOMIC); |
525 | if (rd == NULL) | |
526 | return -ENOBUFS; | |
0c1d70af PA |
527 | |
528 | if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) { | |
529 | kfree(rd); | |
530 | return -ENOBUFS; | |
531 | } | |
532 | ||
e4c7ed41 | 533 | rd->remote_ip = *ip; |
6681712d DS |
534 | rd->remote_port = port; |
535 | rd->remote_vni = vni; | |
536 | rd->remote_ifindex = ifindex; | |
3e61aa8f SH |
537 | |
538 | list_add_tail_rcu(&rd->list, &f->remotes); | |
539 | ||
9e4b93f9 | 540 | *rdp = rd; |
6681712d DS |
541 | return 1; |
542 | } | |
543 | ||
dfd8645e TH |
544 | static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb, |
545 | unsigned int off, | |
546 | struct vxlanhdr *vh, size_t hdrlen, | |
54bfd872 JB |
547 | __be32 vni_field, |
548 | struct gro_remcsum *grc, | |
0ace2ca8 | 549 | bool nopartial) |
dfd8645e | 550 | { |
b7fe10e5 | 551 | size_t start, offset; |
dfd8645e TH |
552 | |
553 | if (skb->remcsum_offload) | |
b7fe10e5 | 554 | return vh; |
dfd8645e TH |
555 | |
556 | if (!NAPI_GRO_CB(skb)->csum_valid) | |
557 | return NULL; | |
558 | ||
54bfd872 JB |
559 | start = vxlan_rco_start(vni_field); |
560 | offset = start + vxlan_rco_offset(vni_field); | |
dfd8645e | 561 | |
b7fe10e5 TH |
562 | vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen, |
563 | start, offset, grc, nopartial); | |
dfd8645e TH |
564 | |
565 | skb->remcsum_offload = 1; | |
566 | ||
567 | return vh; | |
568 | } | |
569 | ||
5602c48c TH |
570 | static struct sk_buff **vxlan_gro_receive(struct sock *sk, |
571 | struct sk_buff **head, | |
572 | struct sk_buff *skb) | |
dc01e7d3 OG |
573 | { |
574 | struct sk_buff *p, **pp = NULL; | |
575 | struct vxlanhdr *vh, *vh2; | |
9b174d88 | 576 | unsigned int hlen, off_vx; |
dc01e7d3 | 577 | int flush = 1; |
5602c48c | 578 | struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk); |
54bfd872 | 579 | __be32 flags; |
26c4f7da TH |
580 | struct gro_remcsum grc; |
581 | ||
582 | skb_gro_remcsum_init(&grc); | |
dc01e7d3 OG |
583 | |
584 | off_vx = skb_gro_offset(skb); | |
585 | hlen = off_vx + sizeof(*vh); | |
586 | vh = skb_gro_header_fast(skb, off_vx); | |
587 | if (skb_gro_header_hard(skb, hlen)) { | |
588 | vh = skb_gro_header_slow(skb, hlen, off_vx); | |
589 | if (unlikely(!vh)) | |
590 | goto out; | |
591 | } | |
dc01e7d3 | 592 | |
dfd8645e TH |
593 | skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr)); |
594 | ||
54bfd872 | 595 | flags = vh->vx_flags; |
dfd8645e TH |
596 | |
597 | if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) { | |
598 | vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr), | |
54bfd872 | 599 | vh->vx_vni, &grc, |
0ace2ca8 TH |
600 | !!(vs->flags & |
601 | VXLAN_F_REMCSUM_NOPARTIAL)); | |
dfd8645e TH |
602 | |
603 | if (!vh) | |
604 | goto out; | |
605 | } | |
606 | ||
b7fe10e5 TH |
607 | skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */ |
608 | ||
dc01e7d3 OG |
609 | for (p = *head; p; p = p->next) { |
610 | if (!NAPI_GRO_CB(p)->same_flow) | |
611 | continue; | |
612 | ||
613 | vh2 = (struct vxlanhdr *)(p->data + off_vx); | |
3511494c TG |
614 | if (vh->vx_flags != vh2->vx_flags || |
615 | vh->vx_vni != vh2->vx_vni) { | |
dc01e7d3 OG |
616 | NAPI_GRO_CB(p)->same_flow = 0; |
617 | continue; | |
618 | } | |
dc01e7d3 OG |
619 | } |
620 | ||
fcd91dd4 | 621 | pp = call_gro_receive(eth_gro_receive, head, skb); |
c194cf93 | 622 | flush = 0; |
dc01e7d3 | 623 | |
dc01e7d3 | 624 | out: |
26c4f7da | 625 | skb_gro_remcsum_cleanup(skb, &grc); |
dc01e7d3 OG |
626 | NAPI_GRO_CB(skb)->flush |= flush; |
627 | ||
628 | return pp; | |
629 | } | |
630 | ||
5602c48c | 631 | static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff) |
dc01e7d3 | 632 | { |
229740c6 JR |
633 | /* Sets 'skb->inner_mac_header' since we are always called with |
634 | * 'skb->encapsulation' set. | |
635 | */ | |
9b174d88 | 636 | return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr)); |
dc01e7d3 OG |
637 | } |
638 | ||
d342894c | 639 | /* Add new entry to forwarding table -- assumes lock held */ |
640 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, | |
e4c7ed41 | 641 | const u8 *mac, union vxlan_addr *ip, |
6681712d | 642 | __u16 state, __u16 flags, |
3ad7a4b1 RP |
643 | __be16 port, __be32 src_vni, __be32 vni, |
644 | __u32 ifindex, __u8 ndm_flags) | |
d342894c | 645 | { |
9e4b93f9 | 646 | struct vxlan_rdst *rd = NULL; |
d342894c | 647 | struct vxlan_fdb *f; |
648 | int notify = 0; | |
17b46365 | 649 | int rc; |
d342894c | 650 | |
3ad7a4b1 | 651 | f = __vxlan_find_mac(vxlan, mac, src_vni); |
d342894c | 652 | if (f) { |
653 | if (flags & NLM_F_EXCL) { | |
654 | netdev_dbg(vxlan->dev, | |
655 | "lost race to create %pM\n", mac); | |
656 | return -EEXIST; | |
657 | } | |
658 | if (f->state != state) { | |
659 | f->state = state; | |
660 | f->updated = jiffies; | |
661 | notify = 1; | |
662 | } | |
ae884082 DS |
663 | if (f->flags != ndm_flags) { |
664 | f->flags = ndm_flags; | |
665 | f->updated = jiffies; | |
666 | notify = 1; | |
667 | } | |
906dc186 TR |
668 | if ((flags & NLM_F_REPLACE)) { |
669 | /* Only change unicasts */ | |
670 | if (!(is_multicast_ether_addr(f->eth_addr) || | |
671 | is_zero_ether_addr(f->eth_addr))) { | |
60840429 | 672 | notify |= vxlan_fdb_replace(f, ip, port, vni, |
906dc186 | 673 | ifindex); |
906dc186 TR |
674 | } else |
675 | return -EOPNOTSUPP; | |
676 | } | |
6681712d | 677 | if ((flags & NLM_F_APPEND) && |
58e4c767 MR |
678 | (is_multicast_ether_addr(f->eth_addr) || |
679 | is_zero_ether_addr(f->eth_addr))) { | |
17b46365 | 680 | rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); |
6681712d DS |
681 | |
682 | if (rc < 0) | |
683 | return rc; | |
684 | notify |= rc; | |
685 | } | |
d342894c | 686 | } else { |
687 | if (!(flags & NLM_F_CREATE)) | |
688 | return -ENOENT; | |
689 | ||
0dfbdf41 TG |
690 | if (vxlan->cfg.addrmax && |
691 | vxlan->addrcnt >= vxlan->cfg.addrmax) | |
d342894c | 692 | return -ENOSPC; |
693 | ||
906dc186 TR |
694 | /* Disallow replace to add a multicast entry */ |
695 | if ((flags & NLM_F_REPLACE) && | |
696 | (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) | |
697 | return -EOPNOTSUPP; | |
698 | ||
e4c7ed41 | 699 | netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); |
d342894c | 700 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
701 | if (!f) | |
702 | return -ENOMEM; | |
703 | ||
704 | notify = 1; | |
d342894c | 705 | f->state = state; |
ae884082 | 706 | f->flags = ndm_flags; |
d342894c | 707 | f->updated = f->used = jiffies; |
3ad7a4b1 | 708 | f->vni = src_vni; |
3e61aa8f | 709 | INIT_LIST_HEAD(&f->remotes); |
d342894c | 710 | memcpy(f->eth_addr, mac, ETH_ALEN); |
711 | ||
17b46365 HY |
712 | rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); |
713 | if (rc < 0) { | |
714 | kfree(f); | |
715 | return rc; | |
716 | } | |
3e61aa8f | 717 | |
d342894c | 718 | ++vxlan->addrcnt; |
719 | hlist_add_head_rcu(&f->hlist, | |
3ad7a4b1 | 720 | vxlan_fdb_head(vxlan, mac, src_vni)); |
d342894c | 721 | } |
722 | ||
9e4b93f9 ND |
723 | if (notify) { |
724 | if (rd == NULL) | |
725 | rd = first_remote_rtnl(f); | |
726 | vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH); | |
727 | } | |
d342894c | 728 | |
729 | return 0; | |
730 | } | |
731 | ||
6706c82e | 732 | static void vxlan_fdb_free(struct rcu_head *head) |
6681712d DS |
733 | { |
734 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); | |
3e61aa8f | 735 | struct vxlan_rdst *rd, *nd; |
6681712d | 736 | |
0c1d70af PA |
737 | list_for_each_entry_safe(rd, nd, &f->remotes, list) { |
738 | dst_cache_destroy(&rd->dst_cache); | |
6681712d | 739 | kfree(rd); |
0c1d70af | 740 | } |
6681712d DS |
741 | kfree(f); |
742 | } | |
743 | ||
d342894c | 744 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
745 | { | |
746 | netdev_dbg(vxlan->dev, | |
747 | "delete %pM\n", f->eth_addr); | |
748 | ||
749 | --vxlan->addrcnt; | |
9e4b93f9 | 750 | vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH); |
d342894c | 751 | |
752 | hlist_del_rcu(&f->hlist); | |
6681712d | 753 | call_rcu(&f->rcu, vxlan_fdb_free); |
d342894c | 754 | } |
755 | ||
35cf2845 LR |
756 | static void vxlan_dst_free(struct rcu_head *head) |
757 | { | |
758 | struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu); | |
759 | ||
760 | dst_cache_destroy(&rd->dst_cache); | |
761 | kfree(rd); | |
762 | } | |
763 | ||
764 | static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f, | |
765 | struct vxlan_rdst *rd) | |
766 | { | |
767 | list_del_rcu(&rd->list); | |
768 | vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH); | |
769 | call_rcu(&rd->rcu, vxlan_dst_free); | |
770 | } | |
771 | ||
f0b074be | 772 | static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, |
3ad7a4b1 RP |
773 | union vxlan_addr *ip, __be16 *port, __be32 *src_vni, |
774 | __be32 *vni, u32 *ifindex) | |
d342894c | 775 | { |
6681712d | 776 | struct net *net = dev_net(vxlan->dev); |
e4c7ed41 | 777 | int err; |
d342894c | 778 | |
f0b074be | 779 | if (tb[NDA_DST]) { |
e4c7ed41 CW |
780 | err = vxlan_nla_get_addr(ip, tb[NDA_DST]); |
781 | if (err) | |
782 | return err; | |
f0b074be | 783 | } else { |
e4c7ed41 CW |
784 | union vxlan_addr *remote = &vxlan->default_dst.remote_ip; |
785 | if (remote->sa.sa_family == AF_INET) { | |
786 | ip->sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
787 | ip->sa.sa_family = AF_INET; | |
788 | #if IS_ENABLED(CONFIG_IPV6) | |
789 | } else { | |
790 | ip->sin6.sin6_addr = in6addr_any; | |
791 | ip->sa.sa_family = AF_INET6; | |
792 | #endif | |
793 | } | |
f0b074be | 794 | } |
d342894c | 795 | |
6681712d | 796 | if (tb[NDA_PORT]) { |
73cf3317 | 797 | if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) |
6681712d | 798 | return -EINVAL; |
f0b074be MR |
799 | *port = nla_get_be16(tb[NDA_PORT]); |
800 | } else { | |
0dfbdf41 | 801 | *port = vxlan->cfg.dst_port; |
f0b074be | 802 | } |
6681712d DS |
803 | |
804 | if (tb[NDA_VNI]) { | |
805 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) | |
806 | return -EINVAL; | |
54bfd872 | 807 | *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI])); |
f0b074be MR |
808 | } else { |
809 | *vni = vxlan->default_dst.remote_vni; | |
810 | } | |
6681712d | 811 | |
3ad7a4b1 RP |
812 | if (tb[NDA_SRC_VNI]) { |
813 | if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32)) | |
814 | return -EINVAL; | |
815 | *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI])); | |
816 | } else { | |
817 | *src_vni = vxlan->default_dst.remote_vni; | |
818 | } | |
819 | ||
6681712d | 820 | if (tb[NDA_IFINDEX]) { |
5abb0029 | 821 | struct net_device *tdev; |
6681712d DS |
822 | |
823 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) | |
824 | return -EINVAL; | |
f0b074be | 825 | *ifindex = nla_get_u32(tb[NDA_IFINDEX]); |
73763949 | 826 | tdev = __dev_get_by_index(net, *ifindex); |
5abb0029 | 827 | if (!tdev) |
6681712d | 828 | return -EADDRNOTAVAIL; |
f0b074be MR |
829 | } else { |
830 | *ifindex = 0; | |
831 | } | |
832 | ||
833 | return 0; | |
834 | } | |
835 | ||
836 | /* Add static entry (via netlink) */ | |
837 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], | |
838 | struct net_device *dev, | |
f6f6424b | 839 | const unsigned char *addr, u16 vid, u16 flags) |
f0b074be MR |
840 | { |
841 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
842 | /* struct net *net = dev_net(vxlan->dev); */ | |
e4c7ed41 | 843 | union vxlan_addr ip; |
f0b074be | 844 | __be16 port; |
3ad7a4b1 | 845 | __be32 src_vni, vni; |
54bfd872 | 846 | u32 ifindex; |
f0b074be MR |
847 | int err; |
848 | ||
849 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { | |
850 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", | |
851 | ndm->ndm_state); | |
852 | return -EINVAL; | |
853 | } | |
854 | ||
855 | if (tb[NDA_DST] == NULL) | |
856 | return -EINVAL; | |
857 | ||
3ad7a4b1 | 858 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex); |
f0b074be MR |
859 | if (err) |
860 | return err; | |
6681712d | 861 | |
5933a7bb MR |
862 | if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family) |
863 | return -EAFNOSUPPORT; | |
864 | ||
d342894c | 865 | spin_lock_bh(&vxlan->hash_lock); |
e4c7ed41 | 866 | err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags, |
3ad7a4b1 | 867 | port, src_vni, vni, ifindex, ndm->ndm_flags); |
d342894c | 868 | spin_unlock_bh(&vxlan->hash_lock); |
869 | ||
870 | return err; | |
871 | } | |
872 | ||
3ad7a4b1 RP |
873 | static int __vxlan_fdb_delete(struct vxlan_dev *vxlan, |
874 | const unsigned char *addr, union vxlan_addr ip, | |
875 | __be16 port, __be32 src_vni, u32 vni, u32 ifindex, | |
876 | u16 vid) | |
d342894c | 877 | { |
d342894c | 878 | struct vxlan_fdb *f; |
bc7892ba | 879 | struct vxlan_rdst *rd = NULL; |
3ad7a4b1 | 880 | int err = -ENOENT; |
bc7892ba | 881 | |
3ad7a4b1 | 882 | f = vxlan_find_mac(vxlan, addr, src_vni); |
bc7892ba | 883 | if (!f) |
3ad7a4b1 | 884 | return err; |
bc7892ba | 885 | |
e4c7ed41 CW |
886 | if (!vxlan_addr_any(&ip)) { |
887 | rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex); | |
bc7892ba MR |
888 | if (!rd) |
889 | goto out; | |
890 | } | |
891 | ||
bc7892ba MR |
892 | /* remove a destination if it's not the only one on the list, |
893 | * otherwise destroy the fdb entry | |
894 | */ | |
895 | if (rd && !list_is_singular(&f->remotes)) { | |
35cf2845 | 896 | vxlan_fdb_dst_destroy(vxlan, f, rd); |
bc7892ba | 897 | goto out; |
d342894c | 898 | } |
bc7892ba MR |
899 | |
900 | vxlan_fdb_destroy(vxlan, f); | |
901 | ||
902 | out: | |
3ad7a4b1 RP |
903 | return 0; |
904 | } | |
905 | ||
906 | /* Delete entry (via netlink) */ | |
907 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], | |
908 | struct net_device *dev, | |
909 | const unsigned char *addr, u16 vid) | |
910 | { | |
911 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
912 | union vxlan_addr ip; | |
913 | __be32 src_vni, vni; | |
914 | __be16 port; | |
915 | u32 ifindex; | |
916 | int err; | |
917 | ||
918 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex); | |
919 | if (err) | |
920 | return err; | |
921 | ||
922 | spin_lock_bh(&vxlan->hash_lock); | |
923 | err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex, | |
924 | vid); | |
d342894c | 925 | spin_unlock_bh(&vxlan->hash_lock); |
926 | ||
927 | return err; | |
928 | } | |
929 | ||
930 | /* Dump forwarding table */ | |
931 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, | |
5d5eacb3 | 932 | struct net_device *dev, |
d297653d | 933 | struct net_device *filter_dev, int *idx) |
d342894c | 934 | { |
935 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
936 | unsigned int h; | |
d297653d | 937 | int err = 0; |
d342894c | 938 | |
939 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
940 | struct vxlan_fdb *f; | |
d342894c | 941 | |
b67bfe0d | 942 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
6681712d | 943 | struct vxlan_rdst *rd; |
6681712d | 944 | |
3e61aa8f | 945 | list_for_each_entry_rcu(rd, &f->remotes, list) { |
d297653d | 946 | if (*idx < cb->args[2]) |
07a51cd3 AW |
947 | goto skip; |
948 | ||
6681712d DS |
949 | err = vxlan_fdb_info(skb, vxlan, f, |
950 | NETLINK_CB(cb->skb).portid, | |
951 | cb->nlh->nlmsg_seq, | |
952 | RTM_NEWNEIGH, | |
953 | NLM_F_MULTI, rd); | |
d297653d | 954 | if (err < 0) |
3e61aa8f | 955 | goto out; |
3e61aa8f | 956 | skip: |
d297653d | 957 | *idx += 1; |
07a51cd3 | 958 | } |
d342894c | 959 | } |
960 | } | |
3e61aa8f | 961 | out: |
d297653d | 962 | return err; |
d342894c | 963 | } |
964 | ||
965 | /* Watch incoming packets to learn mapping between Ethernet address | |
966 | * and Tunnel endpoint. | |
c4b49512 | 967 | * Return true if packet is bogus and should be dropped. |
d342894c | 968 | */ |
26a41ae6 | 969 | static bool vxlan_snoop(struct net_device *dev, |
3ad7a4b1 | 970 | union vxlan_addr *src_ip, const u8 *src_mac, |
87613de9 | 971 | u32 src_ifindex, __be32 vni) |
d342894c | 972 | { |
973 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
974 | struct vxlan_fdb *f; | |
87613de9 MS |
975 | u32 ifindex = 0; |
976 | ||
977 | #if IS_ENABLED(CONFIG_IPV6) | |
978 | if (src_ip->sa.sa_family == AF_INET6 && | |
979 | (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)) | |
980 | ifindex = src_ifindex; | |
981 | #endif | |
d342894c | 982 | |
3ad7a4b1 | 983 | f = vxlan_find_mac(vxlan, src_mac, vni); |
d342894c | 984 | if (likely(f)) { |
5ca5461c | 985 | struct vxlan_rdst *rdst = first_remote_rcu(f); |
3e61aa8f | 986 | |
87613de9 MS |
987 | if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) && |
988 | rdst->remote_ifindex == ifindex)) | |
26a41ae6 | 989 | return false; |
990 | ||
991 | /* Don't migrate static entries, drop packets */ | |
e0090a9e | 992 | if (f->state & (NUD_PERMANENT | NUD_NOARP)) |
26a41ae6 | 993 | return true; |
d342894c | 994 | |
995 | if (net_ratelimit()) | |
996 | netdev_info(dev, | |
e4c7ed41 | 997 | "%pM migrated from %pIS to %pIS\n", |
a4870f79 | 998 | src_mac, &rdst->remote_ip.sa, &src_ip->sa); |
d342894c | 999 | |
e4c7ed41 | 1000 | rdst->remote_ip = *src_ip; |
d342894c | 1001 | f->updated = jiffies; |
9e4b93f9 | 1002 | vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH); |
d342894c | 1003 | } else { |
1004 | /* learned new entry */ | |
1005 | spin_lock(&vxlan->hash_lock); | |
3bf74b1a | 1006 | |
1007 | /* close off race between vxlan_flush and incoming packets */ | |
1008 | if (netif_running(dev)) | |
1009 | vxlan_fdb_create(vxlan, src_mac, src_ip, | |
1010 | NUD_REACHABLE, | |
1011 | NLM_F_EXCL|NLM_F_CREATE, | |
0dfbdf41 | 1012 | vxlan->cfg.dst_port, |
3ad7a4b1 | 1013 | vni, |
3bf74b1a | 1014 | vxlan->default_dst.remote_vni, |
87613de9 | 1015 | ifindex, NTF_SELF); |
d342894c | 1016 | spin_unlock(&vxlan->hash_lock); |
1017 | } | |
26a41ae6 | 1018 | |
1019 | return false; | |
d342894c | 1020 | } |
1021 | ||
d342894c | 1022 | /* See if multicast group is already in use by other ID */ |
95ab0991 | 1023 | static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev) |
d342894c | 1024 | { |
553675fb | 1025 | struct vxlan_dev *vxlan; |
c6fcc4fc | 1026 | struct vxlan_sock *sock4; |
4053ab1b AB |
1027 | #if IS_ENABLED(CONFIG_IPV6) |
1028 | struct vxlan_sock *sock6; | |
1029 | #endif | |
b1be00a6 | 1030 | unsigned short family = dev->default_dst.remote_ip.sa.sa_family; |
d342894c | 1031 | |
c6fcc4fc | 1032 | sock4 = rtnl_dereference(dev->vn4_sock); |
1033 | ||
95ab0991 G |
1034 | /* The vxlan_sock is only used by dev, leaving group has |
1035 | * no effect on other vxlan devices. | |
1036 | */ | |
66af846f | 1037 | if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1) |
95ab0991 | 1038 | return false; |
b1be00a6 | 1039 | #if IS_ENABLED(CONFIG_IPV6) |
c6fcc4fc | 1040 | sock6 = rtnl_dereference(dev->vn6_sock); |
66af846f | 1041 | if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1) |
b1be00a6 JB |
1042 | return false; |
1043 | #endif | |
95ab0991 | 1044 | |
553675fb | 1045 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
95ab0991 | 1046 | if (!netif_running(vxlan->dev) || vxlan == dev) |
553675fb | 1047 | continue; |
d342894c | 1048 | |
c6fcc4fc | 1049 | if (family == AF_INET && |
1050 | rtnl_dereference(vxlan->vn4_sock) != sock4) | |
95ab0991 | 1051 | continue; |
b1be00a6 | 1052 | #if IS_ENABLED(CONFIG_IPV6) |
c6fcc4fc | 1053 | if (family == AF_INET6 && |
1054 | rtnl_dereference(vxlan->vn6_sock) != sock6) | |
b1be00a6 JB |
1055 | continue; |
1056 | #endif | |
95ab0991 G |
1057 | |
1058 | if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip, | |
1059 | &dev->default_dst.remote_ip)) | |
1060 | continue; | |
1061 | ||
1062 | if (vxlan->default_dst.remote_ifindex != | |
1063 | dev->default_dst.remote_ifindex) | |
1064 | continue; | |
1065 | ||
1066 | return true; | |
553675fb | 1067 | } |
d342894c | 1068 | |
1069 | return false; | |
1070 | } | |
1071 | ||
544a773a | 1072 | static bool __vxlan_sock_release_prep(struct vxlan_sock *vs) |
7c47cedf | 1073 | { |
b1be00a6 | 1074 | struct vxlan_net *vn; |
012a5729 | 1075 | |
b1be00a6 | 1076 | if (!vs) |
544a773a | 1077 | return false; |
66af846f | 1078 | if (!refcount_dec_and_test(&vs->refcnt)) |
544a773a | 1079 | return false; |
d342894c | 1080 | |
b1be00a6 | 1081 | vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id); |
1c51a915 | 1082 | spin_lock(&vn->sock_lock); |
7c47cedf | 1083 | hlist_del_rcu(&vs->hlist); |
e7b3db5e | 1084 | udp_tunnel_notify_del_rx_port(vs->sock, |
b9adcd69 AD |
1085 | (vs->flags & VXLAN_F_GPE) ? |
1086 | UDP_TUNNEL_TYPE_VXLAN_GPE : | |
e7b3db5e | 1087 | UDP_TUNNEL_TYPE_VXLAN); |
1c51a915 SH |
1088 | spin_unlock(&vn->sock_lock); |
1089 | ||
544a773a | 1090 | return true; |
d342894c | 1091 | } |
1092 | ||
b1be00a6 JB |
1093 | static void vxlan_sock_release(struct vxlan_dev *vxlan) |
1094 | { | |
c6fcc4fc | 1095 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
b1be00a6 | 1096 | #if IS_ENABLED(CONFIG_IPV6) |
c6fcc4fc | 1097 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
1098 | ||
57d88182 | 1099 | RCU_INIT_POINTER(vxlan->vn6_sock, NULL); |
544a773a HFS |
1100 | #endif |
1101 | ||
57d88182 | 1102 | RCU_INIT_POINTER(vxlan->vn4_sock, NULL); |
544a773a HFS |
1103 | synchronize_net(); |
1104 | ||
a53cb29b MB |
1105 | vxlan_vs_del_dev(vxlan); |
1106 | ||
c6fcc4fc | 1107 | if (__vxlan_sock_release_prep(sock4)) { |
1108 | udp_tunnel_sock_release(sock4->sock); | |
1109 | kfree(sock4); | |
544a773a HFS |
1110 | } |
1111 | ||
1112 | #if IS_ENABLED(CONFIG_IPV6) | |
c6fcc4fc | 1113 | if (__vxlan_sock_release_prep(sock6)) { |
1114 | udp_tunnel_sock_release(sock6->sock); | |
1115 | kfree(sock6); | |
544a773a | 1116 | } |
b1be00a6 JB |
1117 | #endif |
1118 | } | |
1119 | ||
56ef9c90 | 1120 | /* Update multicast group membership when first VNI on |
c4b49512 | 1121 | * multicast address is brought up |
7c47cedf | 1122 | */ |
56ef9c90 | 1123 | static int vxlan_igmp_join(struct vxlan_dev *vxlan) |
d342894c | 1124 | { |
b1be00a6 | 1125 | struct sock *sk; |
e4c7ed41 CW |
1126 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
1127 | int ifindex = vxlan->default_dst.remote_ifindex; | |
149d7549 | 1128 | int ret = -EINVAL; |
d342894c | 1129 | |
e4c7ed41 | 1130 | if (ip->sa.sa_family == AF_INET) { |
c6fcc4fc | 1131 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
e4c7ed41 CW |
1132 | struct ip_mreqn mreq = { |
1133 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, | |
1134 | .imr_ifindex = ifindex, | |
1135 | }; | |
1136 | ||
c6fcc4fc | 1137 | sk = sock4->sock->sk; |
b1be00a6 | 1138 | lock_sock(sk); |
56ef9c90 | 1139 | ret = ip_mc_join_group(sk, &mreq); |
b1be00a6 | 1140 | release_sock(sk); |
e4c7ed41 CW |
1141 | #if IS_ENABLED(CONFIG_IPV6) |
1142 | } else { | |
c6fcc4fc | 1143 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
1144 | ||
1145 | sk = sock6->sock->sk; | |
b1be00a6 | 1146 | lock_sock(sk); |
56ef9c90 MRL |
1147 | ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex, |
1148 | &ip->sin6.sin6_addr); | |
b1be00a6 | 1149 | release_sock(sk); |
e4c7ed41 CW |
1150 | #endif |
1151 | } | |
3fc2de2f | 1152 | |
56ef9c90 | 1153 | return ret; |
3fc2de2f | 1154 | } |
1155 | ||
1156 | /* Inverse of vxlan_igmp_join when last VNI is brought down */ | |
56ef9c90 | 1157 | static int vxlan_igmp_leave(struct vxlan_dev *vxlan) |
3fc2de2f | 1158 | { |
b1be00a6 | 1159 | struct sock *sk; |
e4c7ed41 CW |
1160 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
1161 | int ifindex = vxlan->default_dst.remote_ifindex; | |
149d7549 | 1162 | int ret = -EINVAL; |
3fc2de2f | 1163 | |
e4c7ed41 | 1164 | if (ip->sa.sa_family == AF_INET) { |
c6fcc4fc | 1165 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
e4c7ed41 CW |
1166 | struct ip_mreqn mreq = { |
1167 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, | |
1168 | .imr_ifindex = ifindex, | |
1169 | }; | |
1170 | ||
c6fcc4fc | 1171 | sk = sock4->sock->sk; |
b1be00a6 | 1172 | lock_sock(sk); |
56ef9c90 | 1173 | ret = ip_mc_leave_group(sk, &mreq); |
b1be00a6 | 1174 | release_sock(sk); |
e4c7ed41 CW |
1175 | #if IS_ENABLED(CONFIG_IPV6) |
1176 | } else { | |
c6fcc4fc | 1177 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
1178 | ||
1179 | sk = sock6->sock->sk; | |
b1be00a6 | 1180 | lock_sock(sk); |
56ef9c90 MRL |
1181 | ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex, |
1182 | &ip->sin6.sin6_addr); | |
b1be00a6 | 1183 | release_sock(sk); |
e4c7ed41 CW |
1184 | #endif |
1185 | } | |
d342894c | 1186 | |
56ef9c90 | 1187 | return ret; |
d342894c | 1188 | } |
1189 | ||
f14ecebb JB |
1190 | static bool vxlan_remcsum(struct vxlanhdr *unparsed, |
1191 | struct sk_buff *skb, u32 vxflags) | |
dfd8645e | 1192 | { |
7d34fa75 | 1193 | size_t start, offset; |
dfd8645e | 1194 | |
f14ecebb JB |
1195 | if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload) |
1196 | goto out; | |
b7fe10e5 | 1197 | |
f14ecebb JB |
1198 | start = vxlan_rco_start(unparsed->vx_vni); |
1199 | offset = start + vxlan_rco_offset(unparsed->vx_vni); | |
dfd8645e | 1200 | |
7d34fa75 | 1201 | if (!pskb_may_pull(skb, offset + sizeof(u16))) |
be5cfeab | 1202 | return false; |
dfd8645e | 1203 | |
be5cfeab JB |
1204 | skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset, |
1205 | !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL)); | |
f14ecebb JB |
1206 | out: |
1207 | unparsed->vx_flags &= ~VXLAN_HF_RCO; | |
1208 | unparsed->vx_vni &= VXLAN_VNI_MASK; | |
be5cfeab | 1209 | return true; |
dfd8645e TH |
1210 | } |
1211 | ||
f14ecebb | 1212 | static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed, |
64f87d36 | 1213 | struct sk_buff *skb, u32 vxflags, |
10a5af23 | 1214 | struct vxlan_metadata *md) |
3288af08 | 1215 | { |
f14ecebb | 1216 | struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed; |
10a5af23 | 1217 | struct metadata_dst *tun_dst; |
f14ecebb JB |
1218 | |
1219 | if (!(unparsed->vx_flags & VXLAN_HF_GBP)) | |
1220 | goto out; | |
3288af08 | 1221 | |
3288af08 JB |
1222 | md->gbp = ntohs(gbp->policy_id); |
1223 | ||
10a5af23 | 1224 | tun_dst = (struct metadata_dst *)skb_dst(skb); |
810813c4 | 1225 | if (tun_dst) { |
3288af08 | 1226 | tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT; |
810813c4 DM |
1227 | tun_dst->u.tun_info.options_len = sizeof(*md); |
1228 | } | |
3288af08 JB |
1229 | if (gbp->dont_learn) |
1230 | md->gbp |= VXLAN_GBP_DONT_LEARN; | |
1231 | ||
1232 | if (gbp->policy_applied) | |
1233 | md->gbp |= VXLAN_GBP_POLICY_APPLIED; | |
f14ecebb | 1234 | |
64f87d36 JB |
1235 | /* In flow-based mode, GBP is carried in dst_metadata */ |
1236 | if (!(vxflags & VXLAN_F_COLLECT_METADATA)) | |
1237 | skb->mark = md->gbp; | |
f14ecebb JB |
1238 | out: |
1239 | unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS; | |
3288af08 JB |
1240 | } |
1241 | ||
e1e5314d | 1242 | static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed, |
61618eea | 1243 | __be16 *protocol, |
e1e5314d JB |
1244 | struct sk_buff *skb, u32 vxflags) |
1245 | { | |
1246 | struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed; | |
1247 | ||
1248 | /* Need to have Next Protocol set for interfaces in GPE mode. */ | |
1249 | if (!gpe->np_applied) | |
1250 | return false; | |
1251 | /* "The initial version is 0. If a receiver does not support the | |
1252 | * version indicated it MUST drop the packet. | |
1253 | */ | |
1254 | if (gpe->version != 0) | |
1255 | return false; | |
1256 | /* "When the O bit is set to 1, the packet is an OAM packet and OAM | |
1257 | * processing MUST occur." However, we don't implement OAM | |
1258 | * processing, thus drop the packet. | |
1259 | */ | |
1260 | if (gpe->oam_flag) | |
1261 | return false; | |
1262 | ||
1263 | switch (gpe->next_protocol) { | |
1264 | case VXLAN_GPE_NP_IPV4: | |
1265 | *protocol = htons(ETH_P_IP); | |
1266 | break; | |
1267 | case VXLAN_GPE_NP_IPV6: | |
1268 | *protocol = htons(ETH_P_IPV6); | |
1269 | break; | |
1270 | case VXLAN_GPE_NP_ETHERNET: | |
1271 | *protocol = htons(ETH_P_TEB); | |
1272 | break; | |
1273 | default: | |
1274 | return false; | |
1275 | } | |
1276 | ||
1277 | unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS; | |
1278 | return true; | |
1279 | } | |
1280 | ||
1ab016e2 JB |
1281 | static bool vxlan_set_mac(struct vxlan_dev *vxlan, |
1282 | struct vxlan_sock *vs, | |
3ad7a4b1 | 1283 | struct sk_buff *skb, __be32 vni) |
614732ea | 1284 | { |
614732ea | 1285 | union vxlan_addr saddr; |
87613de9 | 1286 | u32 ifindex = skb->dev->ifindex; |
614732ea | 1287 | |
614732ea | 1288 | skb_reset_mac_header(skb); |
614732ea TG |
1289 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
1290 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
1291 | ||
1292 | /* Ignore packet loops (and multicast echo) */ | |
1293 | if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr)) | |
1ab016e2 | 1294 | return false; |
614732ea | 1295 | |
760c6805 | 1296 | /* Get address from the outer IP header */ |
ce212d0f | 1297 | if (vxlan_get_sk_family(vs) == AF_INET) { |
1ab016e2 | 1298 | saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr; |
614732ea TG |
1299 | saddr.sa.sa_family = AF_INET; |
1300 | #if IS_ENABLED(CONFIG_IPV6) | |
1301 | } else { | |
1ab016e2 | 1302 | saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr; |
614732ea TG |
1303 | saddr.sa.sa_family = AF_INET6; |
1304 | #endif | |
1305 | } | |
1306 | ||
dc5321d7 | 1307 | if ((vxlan->cfg.flags & VXLAN_F_LEARN) && |
87613de9 | 1308 | vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni)) |
1ab016e2 JB |
1309 | return false; |
1310 | ||
1311 | return true; | |
1312 | } | |
1313 | ||
760c6805 JB |
1314 | static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph, |
1315 | struct sk_buff *skb) | |
1316 | { | |
1317 | int err = 0; | |
1318 | ||
1319 | if (vxlan_get_sk_family(vs) == AF_INET) | |
1320 | err = IP_ECN_decapsulate(oiph, skb); | |
1321 | #if IS_ENABLED(CONFIG_IPV6) | |
1322 | else | |
1323 | err = IP6_ECN_decapsulate(oiph, skb); | |
1324 | #endif | |
1325 | ||
1326 | if (unlikely(err) && log_ecn_error) { | |
1327 | if (vxlan_get_sk_family(vs) == AF_INET) | |
1328 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", | |
1329 | &((struct iphdr *)oiph)->saddr, | |
1330 | ((struct iphdr *)oiph)->tos); | |
1331 | else | |
1332 | net_info_ratelimited("non-ECT from %pI6\n", | |
1333 | &((struct ipv6hdr *)oiph)->saddr); | |
1334 | } | |
1335 | return err <= 1; | |
1336 | } | |
1337 | ||
d342894c | 1338 | /* Callback from net/ipv4/udp.c to receive packets */ |
f2d1968e | 1339 | static int vxlan_rcv(struct sock *sk, struct sk_buff *skb) |
d342894c | 1340 | { |
f2d1968e | 1341 | struct pcpu_sw_netstats *stats; |
c9e78efb | 1342 | struct vxlan_dev *vxlan; |
5cfccc5a | 1343 | struct vxlan_sock *vs; |
f14ecebb | 1344 | struct vxlanhdr unparsed; |
ee122c79 TG |
1345 | struct vxlan_metadata _md; |
1346 | struct vxlan_metadata *md = &_md; | |
61618eea | 1347 | __be16 protocol = htons(ETH_P_TEB); |
e1e5314d | 1348 | bool raw_proto = false; |
f2d1968e | 1349 | void *oiph; |
3ad7a4b1 | 1350 | __be32 vni = 0; |
d342894c | 1351 | |
e1e5314d | 1352 | /* Need UDP and VXLAN header to be present */ |
7ce04758 | 1353 | if (!pskb_may_pull(skb, VXLAN_HLEN)) |
e5aed006 | 1354 | goto drop; |
d342894c | 1355 | |
f14ecebb | 1356 | unparsed = *vxlan_hdr(skb); |
288b01c8 JB |
1357 | /* VNI flag always required to be set */ |
1358 | if (!(unparsed.vx_flags & VXLAN_HF_VNI)) { | |
1359 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", | |
1360 | ntohl(vxlan_hdr(skb)->vx_flags), | |
1361 | ntohl(vxlan_hdr(skb)->vx_vni)); | |
1362 | /* Return non vxlan pkt */ | |
e5aed006 | 1363 | goto drop; |
d342894c | 1364 | } |
288b01c8 JB |
1365 | unparsed.vx_flags &= ~VXLAN_HF_VNI; |
1366 | unparsed.vx_vni &= ~VXLAN_VNI_MASK; | |
d342894c | 1367 | |
559835ea | 1368 | vs = rcu_dereference_sk_user_data(sk); |
5cfccc5a | 1369 | if (!vs) |
d342894c | 1370 | goto drop; |
d342894c | 1371 | |
3ad7a4b1 RP |
1372 | vni = vxlan_vni(vxlan_hdr(skb)->vx_vni); |
1373 | ||
49f810f0 | 1374 | vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni); |
c9e78efb JB |
1375 | if (!vxlan) |
1376 | goto drop; | |
1377 | ||
e1e5314d JB |
1378 | /* For backwards compatibility, only allow reserved fields to be |
1379 | * used by VXLAN extensions if explicitly requested. | |
1380 | */ | |
1381 | if (vs->flags & VXLAN_F_GPE) { | |
1382 | if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags)) | |
1383 | goto drop; | |
1384 | raw_proto = true; | |
1385 | } | |
1386 | ||
1387 | if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto, | |
1388 | !net_eq(vxlan->net, dev_net(vxlan->dev)))) | |
1389 | goto drop; | |
c9e78efb | 1390 | |
ee122c79 | 1391 | if (vxlan_collect_metadata(vs)) { |
10a5af23 | 1392 | struct metadata_dst *tun_dst; |
07dabf20 | 1393 | |
c29a70d2 | 1394 | tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY, |
d817f432 | 1395 | key32_to_tunnel_id(vni), sizeof(*md)); |
c29a70d2 | 1396 | |
ee122c79 TG |
1397 | if (!tun_dst) |
1398 | goto drop; | |
1399 | ||
0f1b7354 | 1400 | md = ip_tunnel_info_opts(&tun_dst->u.tun_info); |
10a5af23 JB |
1401 | |
1402 | skb_dst_set(skb, (struct dst_entry *)tun_dst); | |
ee122c79 TG |
1403 | } else { |
1404 | memset(md, 0, sizeof(*md)); | |
1405 | } | |
1406 | ||
f14ecebb JB |
1407 | if (vs->flags & VXLAN_F_REMCSUM_RX) |
1408 | if (!vxlan_remcsum(&unparsed, skb, vs->flags)) | |
1409 | goto drop; | |
1410 | if (vs->flags & VXLAN_F_GBP) | |
10a5af23 | 1411 | vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md); |
e1e5314d JB |
1412 | /* Note that GBP and GPE can never be active together. This is |
1413 | * ensured in vxlan_dev_configure. | |
1414 | */ | |
3511494c | 1415 | |
f14ecebb | 1416 | if (unparsed.vx_flags || unparsed.vx_vni) { |
3bf39475 TH |
1417 | /* If there are any unprocessed flags remaining treat |
1418 | * this as a malformed packet. This behavior diverges from | |
1419 | * VXLAN RFC (RFC7348) which stipulates that bits in reserved | |
1420 | * in reserved fields are to be ignored. The approach here | |
c4b49512 | 1421 | * maintains compatibility with previous stack code, and also |
3bf39475 TH |
1422 | * is more robust and provides a little more security in |
1423 | * adding extensions to VXLAN. | |
1424 | */ | |
288b01c8 | 1425 | goto drop; |
3bf39475 TH |
1426 | } |
1427 | ||
e1e5314d | 1428 | if (!raw_proto) { |
3ad7a4b1 | 1429 | if (!vxlan_set_mac(vxlan, vs, skb, vni)) |
e1e5314d JB |
1430 | goto drop; |
1431 | } else { | |
8be0cfa4 | 1432 | skb_reset_mac_header(skb); |
e1e5314d JB |
1433 | skb->dev = vxlan->dev; |
1434 | skb->pkt_type = PACKET_HOST; | |
1435 | } | |
f2d1968e | 1436 | |
f2d1968e JB |
1437 | oiph = skb_network_header(skb); |
1438 | skb_reset_network_header(skb); | |
1439 | ||
1440 | if (!vxlan_ecn_decapsulate(vs, oiph, skb)) { | |
1441 | ++vxlan->dev->stats.rx_frame_errors; | |
1442 | ++vxlan->dev->stats.rx_errors; | |
1443 | goto drop; | |
1444 | } | |
1445 | ||
1446 | stats = this_cpu_ptr(vxlan->dev->tstats); | |
1447 | u64_stats_update_begin(&stats->syncp); | |
1448 | stats->rx_packets++; | |
1449 | stats->rx_bytes += skb->len; | |
1450 | u64_stats_update_end(&stats->syncp); | |
1451 | ||
1452 | gro_cells_receive(&vxlan->gro_cells, skb); | |
5cfccc5a PS |
1453 | return 0; |
1454 | ||
1455 | drop: | |
288b01c8 JB |
1456 | /* Consume bad packet */ |
1457 | kfree_skb(skb); | |
1458 | return 0; | |
5cfccc5a PS |
1459 | } |
1460 | ||
3ad7a4b1 | 1461 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni) |
e4f67add DS |
1462 | { |
1463 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1464 | struct arphdr *parp; | |
1465 | u8 *arpptr, *sha; | |
1466 | __be32 sip, tip; | |
1467 | struct neighbour *n; | |
1468 | ||
1469 | if (dev->flags & IFF_NOARP) | |
1470 | goto out; | |
1471 | ||
1472 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { | |
1473 | dev->stats.tx_dropped++; | |
1474 | goto out; | |
1475 | } | |
1476 | parp = arp_hdr(skb); | |
1477 | ||
1478 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && | |
1479 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || | |
1480 | parp->ar_pro != htons(ETH_P_IP) || | |
1481 | parp->ar_op != htons(ARPOP_REQUEST) || | |
1482 | parp->ar_hln != dev->addr_len || | |
1483 | parp->ar_pln != 4) | |
1484 | goto out; | |
1485 | arpptr = (u8 *)parp + sizeof(struct arphdr); | |
1486 | sha = arpptr; | |
1487 | arpptr += dev->addr_len; /* sha */ | |
1488 | memcpy(&sip, arpptr, sizeof(sip)); | |
1489 | arpptr += sizeof(sip); | |
1490 | arpptr += dev->addr_len; /* tha */ | |
1491 | memcpy(&tip, arpptr, sizeof(tip)); | |
1492 | ||
1493 | if (ipv4_is_loopback(tip) || | |
1494 | ipv4_is_multicast(tip)) | |
1495 | goto out; | |
1496 | ||
1497 | n = neigh_lookup(&arp_tbl, &tip, dev); | |
1498 | ||
1499 | if (n) { | |
e4f67add DS |
1500 | struct vxlan_fdb *f; |
1501 | struct sk_buff *reply; | |
1502 | ||
1503 | if (!(n->nud_state & NUD_CONNECTED)) { | |
1504 | neigh_release(n); | |
1505 | goto out; | |
1506 | } | |
1507 | ||
3ad7a4b1 | 1508 | f = vxlan_find_mac(vxlan, n->ha, vni); |
e4c7ed41 | 1509 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
e4f67add DS |
1510 | /* bridge-local neighbor */ |
1511 | neigh_release(n); | |
1512 | goto out; | |
1513 | } | |
1514 | ||
1515 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, | |
1516 | n->ha, sha); | |
1517 | ||
1518 | neigh_release(n); | |
1519 | ||
7346135d DS |
1520 | if (reply == NULL) |
1521 | goto out; | |
1522 | ||
e4f67add DS |
1523 | skb_reset_mac_header(reply); |
1524 | __skb_pull(reply, skb_network_offset(reply)); | |
1525 | reply->ip_summed = CHECKSUM_UNNECESSARY; | |
1526 | reply->pkt_type = PACKET_HOST; | |
1527 | ||
1528 | if (netif_rx_ni(reply) == NET_RX_DROP) | |
1529 | dev->stats.rx_dropped++; | |
dc5321d7 | 1530 | } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) { |
e4c7ed41 CW |
1531 | union vxlan_addr ipa = { |
1532 | .sin.sin_addr.s_addr = tip, | |
a45e92a5 | 1533 | .sin.sin_family = AF_INET, |
e4c7ed41 CW |
1534 | }; |
1535 | ||
1536 | vxlan_ip_miss(dev, &ipa); | |
1537 | } | |
e4f67add DS |
1538 | out: |
1539 | consume_skb(skb); | |
1540 | return NETDEV_TX_OK; | |
1541 | } | |
1542 | ||
f564f45c | 1543 | #if IS_ENABLED(CONFIG_IPV6) |
4b29dba9 DS |
1544 | static struct sk_buff *vxlan_na_create(struct sk_buff *request, |
1545 | struct neighbour *n, bool isrouter) | |
1546 | { | |
1547 | struct net_device *dev = request->dev; | |
1548 | struct sk_buff *reply; | |
1549 | struct nd_msg *ns, *na; | |
1550 | struct ipv6hdr *pip6; | |
1551 | u8 *daddr; | |
1552 | int na_olen = 8; /* opt hdr + ETH_ALEN for target */ | |
1553 | int ns_olen; | |
1554 | int i, len; | |
1555 | ||
f1fb08f6 | 1556 | if (dev == NULL || !pskb_may_pull(request, request->len)) |
4b29dba9 DS |
1557 | return NULL; |
1558 | ||
1559 | len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) + | |
1560 | sizeof(*na) + na_olen + dev->needed_tailroom; | |
1561 | reply = alloc_skb(len, GFP_ATOMIC); | |
1562 | if (reply == NULL) | |
1563 | return NULL; | |
1564 | ||
1565 | reply->protocol = htons(ETH_P_IPV6); | |
1566 | reply->dev = dev; | |
1567 | skb_reserve(reply, LL_RESERVED_SPACE(request->dev)); | |
1568 | skb_push(reply, sizeof(struct ethhdr)); | |
6297b91c | 1569 | skb_reset_mac_header(reply); |
4b29dba9 | 1570 | |
f1fb08f6 | 1571 | ns = (struct nd_msg *)(ipv6_hdr(request) + 1); |
4b29dba9 DS |
1572 | |
1573 | daddr = eth_hdr(request)->h_source; | |
f1fb08f6 VB |
1574 | ns_olen = request->len - skb_network_offset(request) - |
1575 | sizeof(struct ipv6hdr) - sizeof(*ns); | |
4b29dba9 DS |
1576 | for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) { |
1577 | if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) { | |
1578 | daddr = ns->opt + i + sizeof(struct nd_opt_hdr); | |
1579 | break; | |
1580 | } | |
1581 | } | |
1582 | ||
1583 | /* Ethernet header */ | |
1584 | ether_addr_copy(eth_hdr(reply)->h_dest, daddr); | |
1585 | ether_addr_copy(eth_hdr(reply)->h_source, n->ha); | |
1586 | eth_hdr(reply)->h_proto = htons(ETH_P_IPV6); | |
1587 | reply->protocol = htons(ETH_P_IPV6); | |
1588 | ||
1589 | skb_pull(reply, sizeof(struct ethhdr)); | |
6297b91c | 1590 | skb_reset_network_header(reply); |
4b29dba9 DS |
1591 | skb_put(reply, sizeof(struct ipv6hdr)); |
1592 | ||
1593 | /* IPv6 header */ | |
1594 | ||
1595 | pip6 = ipv6_hdr(reply); | |
1596 | memset(pip6, 0, sizeof(struct ipv6hdr)); | |
1597 | pip6->version = 6; | |
1598 | pip6->priority = ipv6_hdr(request)->priority; | |
1599 | pip6->nexthdr = IPPROTO_ICMPV6; | |
1600 | pip6->hop_limit = 255; | |
1601 | pip6->daddr = ipv6_hdr(request)->saddr; | |
1602 | pip6->saddr = *(struct in6_addr *)n->primary_key; | |
1603 | ||
1604 | skb_pull(reply, sizeof(struct ipv6hdr)); | |
6297b91c | 1605 | skb_reset_transport_header(reply); |
4b29dba9 | 1606 | |
4b29dba9 | 1607 | /* Neighbor Advertisement */ |
b080db58 | 1608 | na = skb_put_zero(reply, sizeof(*na) + na_olen); |
4b29dba9 DS |
1609 | na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; |
1610 | na->icmph.icmp6_router = isrouter; | |
1611 | na->icmph.icmp6_override = 1; | |
1612 | na->icmph.icmp6_solicited = 1; | |
1613 | na->target = ns->target; | |
1614 | ether_addr_copy(&na->opt[2], n->ha); | |
1615 | na->opt[0] = ND_OPT_TARGET_LL_ADDR; | |
1616 | na->opt[1] = na_olen >> 3; | |
1617 | ||
1618 | na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr, | |
1619 | &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6, | |
1620 | csum_partial(na, sizeof(*na)+na_olen, 0)); | |
1621 | ||
1622 | pip6->payload_len = htons(sizeof(*na)+na_olen); | |
1623 | ||
1624 | skb_push(reply, sizeof(struct ipv6hdr)); | |
1625 | ||
1626 | reply->ip_summed = CHECKSUM_UNNECESSARY; | |
1627 | ||
1628 | return reply; | |
1629 | } | |
1630 | ||
3ad7a4b1 | 1631 | static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni) |
f564f45c CW |
1632 | { |
1633 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
4b29dba9 | 1634 | struct nd_msg *msg; |
f564f45c | 1635 | const struct ipv6hdr *iphdr; |
8dcd81a9 | 1636 | const struct in6_addr *daddr; |
4b29dba9 DS |
1637 | struct neighbour *n; |
1638 | struct inet6_dev *in6_dev; | |
f564f45c CW |
1639 | |
1640 | in6_dev = __in6_dev_get(dev); | |
1641 | if (!in6_dev) | |
1642 | goto out; | |
1643 | ||
f1fb08f6 VB |
1644 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg))) |
1645 | goto out; | |
1646 | ||
f564f45c | 1647 | iphdr = ipv6_hdr(skb); |
f564f45c CW |
1648 | daddr = &iphdr->daddr; |
1649 | ||
f1fb08f6 | 1650 | msg = (struct nd_msg *)(iphdr + 1); |
f564f45c CW |
1651 | if (msg->icmph.icmp6_code != 0 || |
1652 | msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) | |
1653 | goto out; | |
1654 | ||
4b29dba9 DS |
1655 | if (ipv6_addr_loopback(daddr) || |
1656 | ipv6_addr_is_multicast(&msg->target)) | |
1657 | goto out; | |
1658 | ||
1659 | n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev); | |
f564f45c CW |
1660 | |
1661 | if (n) { | |
1662 | struct vxlan_fdb *f; | |
4b29dba9 | 1663 | struct sk_buff *reply; |
f564f45c CW |
1664 | |
1665 | if (!(n->nud_state & NUD_CONNECTED)) { | |
1666 | neigh_release(n); | |
1667 | goto out; | |
1668 | } | |
1669 | ||
3ad7a4b1 | 1670 | f = vxlan_find_mac(vxlan, n->ha, vni); |
f564f45c CW |
1671 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
1672 | /* bridge-local neighbor */ | |
1673 | neigh_release(n); | |
1674 | goto out; | |
1675 | } | |
1676 | ||
4b29dba9 DS |
1677 | reply = vxlan_na_create(skb, n, |
1678 | !!(f ? f->flags & NTF_ROUTER : 0)); | |
1679 | ||
f564f45c | 1680 | neigh_release(n); |
4b29dba9 DS |
1681 | |
1682 | if (reply == NULL) | |
1683 | goto out; | |
1684 | ||
1685 | if (netif_rx_ni(reply) == NET_RX_DROP) | |
1686 | dev->stats.rx_dropped++; | |
1687 | ||
dc5321d7 | 1688 | } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) { |
4b29dba9 DS |
1689 | union vxlan_addr ipa = { |
1690 | .sin6.sin6_addr = msg->target, | |
a45e92a5 | 1691 | .sin6.sin6_family = AF_INET6, |
4b29dba9 DS |
1692 | }; |
1693 | ||
f564f45c CW |
1694 | vxlan_ip_miss(dev, &ipa); |
1695 | } | |
1696 | ||
1697 | out: | |
1698 | consume_skb(skb); | |
1699 | return NETDEV_TX_OK; | |
1700 | } | |
1701 | #endif | |
1702 | ||
e4f67add DS |
1703 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) |
1704 | { | |
1705 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1706 | struct neighbour *n; | |
e4f67add DS |
1707 | |
1708 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) | |
1709 | return false; | |
1710 | ||
1711 | n = NULL; | |
1712 | switch (ntohs(eth_hdr(skb)->h_proto)) { | |
1713 | case ETH_P_IP: | |
e15a00aa CW |
1714 | { |
1715 | struct iphdr *pip; | |
1716 | ||
e4f67add DS |
1717 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
1718 | return false; | |
1719 | pip = ip_hdr(skb); | |
1720 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); | |
dc5321d7 | 1721 | if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) { |
e4c7ed41 CW |
1722 | union vxlan_addr ipa = { |
1723 | .sin.sin_addr.s_addr = pip->daddr, | |
a45e92a5 | 1724 | .sin.sin_family = AF_INET, |
e4c7ed41 CW |
1725 | }; |
1726 | ||
1727 | vxlan_ip_miss(dev, &ipa); | |
1728 | return false; | |
1729 | } | |
1730 | ||
e4f67add | 1731 | break; |
e15a00aa CW |
1732 | } |
1733 | #if IS_ENABLED(CONFIG_IPV6) | |
1734 | case ETH_P_IPV6: | |
1735 | { | |
1736 | struct ipv6hdr *pip6; | |
1737 | ||
1738 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) | |
1739 | return false; | |
1740 | pip6 = ipv6_hdr(skb); | |
1741 | n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev); | |
dc5321d7 | 1742 | if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) { |
e15a00aa CW |
1743 | union vxlan_addr ipa = { |
1744 | .sin6.sin6_addr = pip6->daddr, | |
a45e92a5 | 1745 | .sin6.sin6_family = AF_INET6, |
e15a00aa CW |
1746 | }; |
1747 | ||
1748 | vxlan_ip_miss(dev, &ipa); | |
1749 | return false; | |
1750 | } | |
1751 | ||
1752 | break; | |
1753 | } | |
1754 | #endif | |
e4f67add DS |
1755 | default: |
1756 | return false; | |
1757 | } | |
1758 | ||
1759 | if (n) { | |
1760 | bool diff; | |
1761 | ||
7367d0b5 | 1762 | diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha); |
e4f67add DS |
1763 | if (diff) { |
1764 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, | |
1765 | dev->addr_len); | |
1766 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); | |
1767 | } | |
1768 | neigh_release(n); | |
1769 | return diff; | |
e4c7ed41 CW |
1770 | } |
1771 | ||
e4f67add DS |
1772 | return false; |
1773 | } | |
1774 | ||
af33c1ad | 1775 | static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags, |
3511494c TG |
1776 | struct vxlan_metadata *md) |
1777 | { | |
1778 | struct vxlanhdr_gbp *gbp; | |
1779 | ||
db79a621 TG |
1780 | if (!md->gbp) |
1781 | return; | |
1782 | ||
3511494c | 1783 | gbp = (struct vxlanhdr_gbp *)vxh; |
54bfd872 | 1784 | vxh->vx_flags |= VXLAN_HF_GBP; |
3511494c TG |
1785 | |
1786 | if (md->gbp & VXLAN_GBP_DONT_LEARN) | |
1787 | gbp->dont_learn = 1; | |
1788 | ||
1789 | if (md->gbp & VXLAN_GBP_POLICY_APPLIED) | |
1790 | gbp->policy_applied = 1; | |
1791 | ||
1792 | gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK); | |
1793 | } | |
1794 | ||
e1e5314d JB |
1795 | static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags, |
1796 | __be16 protocol) | |
1797 | { | |
1798 | struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh; | |
1799 | ||
1800 | gpe->np_applied = 1; | |
1801 | ||
1802 | switch (protocol) { | |
1803 | case htons(ETH_P_IP): | |
1804 | gpe->next_protocol = VXLAN_GPE_NP_IPV4; | |
1805 | return 0; | |
1806 | case htons(ETH_P_IPV6): | |
1807 | gpe->next_protocol = VXLAN_GPE_NP_IPV6; | |
1808 | return 0; | |
1809 | case htons(ETH_P_TEB): | |
1810 | gpe->next_protocol = VXLAN_GPE_NP_ETHERNET; | |
1811 | return 0; | |
1812 | } | |
1813 | return -EPFNOSUPPORT; | |
1814 | } | |
1815 | ||
f491e56d JB |
1816 | static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst, |
1817 | int iphdr_len, __be32 vni, | |
1818 | struct vxlan_metadata *md, u32 vxflags, | |
b4ed5cad | 1819 | bool udp_sum) |
e4c7ed41 | 1820 | { |
e4c7ed41 | 1821 | struct vxlanhdr *vxh; |
e4c7ed41 CW |
1822 | int min_headroom; |
1823 | int err; | |
dfd8645e | 1824 | int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; |
e1e5314d | 1825 | __be16 inner_protocol = htons(ETH_P_TEB); |
dfd8645e | 1826 | |
af33c1ad | 1827 | if ((vxflags & VXLAN_F_REMCSUM_TX) && |
dfd8645e TH |
1828 | skb->ip_summed == CHECKSUM_PARTIAL) { |
1829 | int csum_start = skb_checksum_start_offset(skb); | |
1830 | ||
1831 | if (csum_start <= VXLAN_MAX_REMCSUM_START && | |
1832 | !(csum_start & VXLAN_RCO_SHIFT_MASK) && | |
1833 | (skb->csum_offset == offsetof(struct udphdr, check) || | |
b5708501 | 1834 | skb->csum_offset == offsetof(struct tcphdr, check))) |
dfd8645e | 1835 | type |= SKB_GSO_TUNNEL_REMCSUM; |
dfd8645e | 1836 | } |
e4c7ed41 | 1837 | |
e4c7ed41 | 1838 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len |
4a4f86cc | 1839 | + VXLAN_HLEN + iphdr_len; |
649c5b8b PS |
1840 | |
1841 | /* Need space for new headers (invalidates iph ptr) */ | |
1842 | err = skb_cow_head(skb, min_headroom); | |
e1e5314d | 1843 | if (unlikely(err)) |
c46b7897 | 1844 | return err; |
649c5b8b | 1845 | |
aed069df AD |
1846 | err = iptunnel_handle_offloads(skb, type); |
1847 | if (err) | |
c46b7897 | 1848 | return err; |
b736a623 | 1849 | |
d58ff351 | 1850 | vxh = __skb_push(skb, sizeof(*vxh)); |
54bfd872 JB |
1851 | vxh->vx_flags = VXLAN_HF_VNI; |
1852 | vxh->vx_vni = vxlan_vni_field(vni); | |
49560532 | 1853 | |
dfd8645e | 1854 | if (type & SKB_GSO_TUNNEL_REMCSUM) { |
54bfd872 | 1855 | unsigned int start; |
dfd8645e | 1856 | |
54bfd872 JB |
1857 | start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr); |
1858 | vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset); | |
1859 | vxh->vx_flags |= VXLAN_HF_RCO; | |
dfd8645e TH |
1860 | |
1861 | if (!skb_is_gso(skb)) { | |
1862 | skb->ip_summed = CHECKSUM_NONE; | |
1863 | skb->encapsulation = 0; | |
1864 | } | |
1865 | } | |
1866 | ||
af33c1ad TH |
1867 | if (vxflags & VXLAN_F_GBP) |
1868 | vxlan_build_gbp_hdr(vxh, vxflags, md); | |
e1e5314d JB |
1869 | if (vxflags & VXLAN_F_GPE) { |
1870 | err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol); | |
1871 | if (err < 0) | |
c46b7897 | 1872 | return err; |
e1e5314d JB |
1873 | inner_protocol = skb->protocol; |
1874 | } | |
3511494c | 1875 | |
e1e5314d | 1876 | skb_set_inner_protocol(skb, inner_protocol); |
039f5062 | 1877 | return 0; |
49560532 | 1878 | } |
49560532 | 1879 | |
655c3de1 | 1880 | static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev, |
1881 | struct vxlan_sock *sock4, | |
1a8496ba | 1882 | struct sk_buff *skb, int oif, u8 tos, |
4ecb1d83 | 1883 | __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport, |
0c1d70af | 1884 | struct dst_cache *dst_cache, |
db3c6139 | 1885 | const struct ip_tunnel_info *info) |
1a8496ba | 1886 | { |
db3c6139 | 1887 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
1a8496ba JB |
1888 | struct rtable *rt = NULL; |
1889 | struct flowi4 fl4; | |
1890 | ||
655c3de1 | 1891 | if (!sock4) |
1892 | return ERR_PTR(-EIO); | |
1893 | ||
db3c6139 DB |
1894 | if (tos && !info) |
1895 | use_cache = false; | |
1896 | if (use_cache) { | |
0c1d70af PA |
1897 | rt = dst_cache_get_ip4(dst_cache, saddr); |
1898 | if (rt) | |
1899 | return rt; | |
1900 | } | |
1901 | ||
1a8496ba JB |
1902 | memset(&fl4, 0, sizeof(fl4)); |
1903 | fl4.flowi4_oif = oif; | |
1904 | fl4.flowi4_tos = RT_TOS(tos); | |
1905 | fl4.flowi4_mark = skb->mark; | |
1906 | fl4.flowi4_proto = IPPROTO_UDP; | |
1907 | fl4.daddr = daddr; | |
272d96a5 | 1908 | fl4.saddr = *saddr; |
4ecb1d83 MP |
1909 | fl4.fl4_dport = dport; |
1910 | fl4.fl4_sport = sport; | |
1a8496ba JB |
1911 | |
1912 | rt = ip_route_output_key(vxlan->net, &fl4); | |
655c3de1 | 1913 | if (likely(!IS_ERR(rt))) { |
1914 | if (rt->dst.dev == dev) { | |
1915 | netdev_dbg(dev, "circular route to %pI4\n", &daddr); | |
1916 | ip_rt_put(rt); | |
1917 | return ERR_PTR(-ELOOP); | |
1918 | } | |
1919 | ||
1a8496ba | 1920 | *saddr = fl4.saddr; |
0c1d70af PA |
1921 | if (use_cache) |
1922 | dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr); | |
655c3de1 | 1923 | } else { |
1924 | netdev_dbg(dev, "no route to %pI4\n", &daddr); | |
1925 | return ERR_PTR(-ENETUNREACH); | |
0c1d70af | 1926 | } |
1a8496ba JB |
1927 | return rt; |
1928 | } | |
1929 | ||
e5d4b29f JB |
1930 | #if IS_ENABLED(CONFIG_IPV6) |
1931 | static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan, | |
655c3de1 | 1932 | struct net_device *dev, |
03dc52a8 | 1933 | struct vxlan_sock *sock6, |
1400615d | 1934 | struct sk_buff *skb, int oif, u8 tos, |
e7f70af1 | 1935 | __be32 label, |
e5d4b29f | 1936 | const struct in6_addr *daddr, |
0c1d70af | 1937 | struct in6_addr *saddr, |
4ecb1d83 | 1938 | __be16 dport, __be16 sport, |
db3c6139 DB |
1939 | struct dst_cache *dst_cache, |
1940 | const struct ip_tunnel_info *info) | |
e5d4b29f | 1941 | { |
db3c6139 | 1942 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
e5d4b29f JB |
1943 | struct dst_entry *ndst; |
1944 | struct flowi6 fl6; | |
1945 | int err; | |
1946 | ||
c6fcc4fc | 1947 | if (!sock6) |
1948 | return ERR_PTR(-EIO); | |
1949 | ||
1400615d DB |
1950 | if (tos && !info) |
1951 | use_cache = false; | |
db3c6139 | 1952 | if (use_cache) { |
0c1d70af PA |
1953 | ndst = dst_cache_get_ip6(dst_cache, saddr); |
1954 | if (ndst) | |
1955 | return ndst; | |
1956 | } | |
1957 | ||
e5d4b29f JB |
1958 | memset(&fl6, 0, sizeof(fl6)); |
1959 | fl6.flowi6_oif = oif; | |
1960 | fl6.daddr = *daddr; | |
272d96a5 | 1961 | fl6.saddr = *saddr; |
eaa93bf4 | 1962 | fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label); |
e5d4b29f JB |
1963 | fl6.flowi6_mark = skb->mark; |
1964 | fl6.flowi6_proto = IPPROTO_UDP; | |
4ecb1d83 MP |
1965 | fl6.fl6_dport = dport; |
1966 | fl6.fl6_sport = sport; | |
e5d4b29f JB |
1967 | |
1968 | err = ipv6_stub->ipv6_dst_lookup(vxlan->net, | |
c6fcc4fc | 1969 | sock6->sock->sk, |
e5d4b29f | 1970 | &ndst, &fl6); |
655c3de1 | 1971 | if (unlikely(err < 0)) { |
1972 | netdev_dbg(dev, "no route to %pI6\n", daddr); | |
1973 | return ERR_PTR(-ENETUNREACH); | |
1974 | } | |
1975 | ||
1976 | if (unlikely(ndst->dev == dev)) { | |
1977 | netdev_dbg(dev, "circular route to %pI6\n", daddr); | |
1978 | dst_release(ndst); | |
1979 | return ERR_PTR(-ELOOP); | |
1980 | } | |
e5d4b29f JB |
1981 | |
1982 | *saddr = fl6.saddr; | |
db3c6139 | 1983 | if (use_cache) |
0c1d70af | 1984 | dst_cache_set_ip6(dst_cache, ndst, saddr); |
e5d4b29f JB |
1985 | return ndst; |
1986 | } | |
1987 | #endif | |
1988 | ||
9dcc71e1 SS |
1989 | /* Bypass encapsulation if the destination is local */ |
1990 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, | |
3ad7a4b1 | 1991 | struct vxlan_dev *dst_vxlan, __be32 vni) |
9dcc71e1 | 1992 | { |
8f84985f | 1993 | struct pcpu_sw_netstats *tx_stats, *rx_stats; |
e4c7ed41 CW |
1994 | union vxlan_addr loopback; |
1995 | union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip; | |
ce6502a8 LR |
1996 | struct net_device *dev = skb->dev; |
1997 | int len = skb->len; | |
9dcc71e1 | 1998 | |
8f84985f LR |
1999 | tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); |
2000 | rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); | |
9dcc71e1 SS |
2001 | skb->pkt_type = PACKET_HOST; |
2002 | skb->encapsulation = 0; | |
2003 | skb->dev = dst_vxlan->dev; | |
2004 | __skb_pull(skb, skb_network_offset(skb)); | |
2005 | ||
e4c7ed41 CW |
2006 | if (remote_ip->sa.sa_family == AF_INET) { |
2007 | loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
2008 | loopback.sa.sa_family = AF_INET; | |
2009 | #if IS_ENABLED(CONFIG_IPV6) | |
2010 | } else { | |
2011 | loopback.sin6.sin6_addr = in6addr_loopback; | |
2012 | loopback.sa.sa_family = AF_INET6; | |
2013 | #endif | |
2014 | } | |
2015 | ||
dc5321d7 | 2016 | if (dst_vxlan->cfg.flags & VXLAN_F_LEARN) |
87613de9 MS |
2017 | vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0, |
2018 | vni); | |
9dcc71e1 SS |
2019 | |
2020 | u64_stats_update_begin(&tx_stats->syncp); | |
2021 | tx_stats->tx_packets++; | |
ce6502a8 | 2022 | tx_stats->tx_bytes += len; |
9dcc71e1 SS |
2023 | u64_stats_update_end(&tx_stats->syncp); |
2024 | ||
2025 | if (netif_rx(skb) == NET_RX_SUCCESS) { | |
2026 | u64_stats_update_begin(&rx_stats->syncp); | |
2027 | rx_stats->rx_packets++; | |
ce6502a8 | 2028 | rx_stats->rx_bytes += len; |
9dcc71e1 SS |
2029 | u64_stats_update_end(&rx_stats->syncp); |
2030 | } else { | |
ce6502a8 | 2031 | dev->stats.rx_dropped++; |
9dcc71e1 SS |
2032 | } |
2033 | } | |
2034 | ||
fee1fad7 | 2035 | static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev, |
49f810f0 MS |
2036 | struct vxlan_dev *vxlan, |
2037 | union vxlan_addr *daddr, | |
2038 | __be16 dst_port, int dst_ifindex, __be32 vni, | |
2039 | struct dst_entry *dst, | |
fee1fad7 | 2040 | u32 rt_flags) |
2041 | { | |
2042 | #if IS_ENABLED(CONFIG_IPV6) | |
2043 | /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of | |
2044 | * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple | |
2045 | * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry. | |
2046 | */ | |
2047 | BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL); | |
2048 | #endif | |
2049 | /* Bypass encapsulation if the destination is local */ | |
2050 | if (rt_flags & RTCF_LOCAL && | |
2051 | !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { | |
2052 | struct vxlan_dev *dst_vxlan; | |
2053 | ||
2054 | dst_release(dst); | |
49f810f0 | 2055 | dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni, |
fee1fad7 | 2056 | daddr->sa.sa_family, dst_port, |
dc5321d7 | 2057 | vxlan->cfg.flags); |
fee1fad7 | 2058 | if (!dst_vxlan) { |
2059 | dev->stats.tx_errors++; | |
2060 | kfree_skb(skb); | |
2061 | ||
2062 | return -ENOENT; | |
2063 | } | |
3ad7a4b1 | 2064 | vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni); |
fee1fad7 | 2065 | return 1; |
2066 | } | |
2067 | ||
2068 | return 0; | |
2069 | } | |
2070 | ||
4ad16930 | 2071 | static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
3ad7a4b1 RP |
2072 | __be32 default_vni, struct vxlan_rdst *rdst, |
2073 | bool did_rsc) | |
d342894c | 2074 | { |
d71785ff | 2075 | struct dst_cache *dst_cache; |
3093fbe7 | 2076 | struct ip_tunnel_info *info; |
d342894c | 2077 | struct vxlan_dev *vxlan = netdev_priv(dev); |
0770b53b | 2078 | const struct iphdr *old_iph = ip_hdr(skb); |
e4c7ed41 | 2079 | union vxlan_addr *dst; |
272d96a5 | 2080 | union vxlan_addr remote_ip, local_ip; |
ee122c79 TG |
2081 | struct vxlan_metadata _md; |
2082 | struct vxlan_metadata *md = &_md; | |
e4c7ed41 | 2083 | __be16 src_port = 0, dst_port; |
655c3de1 | 2084 | struct dst_entry *ndst = NULL; |
e7f70af1 | 2085 | __be32 vni, label; |
d342894c | 2086 | __u8 tos, ttl; |
49f810f0 | 2087 | int ifindex; |
0e6fbc5b | 2088 | int err; |
dc5321d7 | 2089 | u32 flags = vxlan->cfg.flags; |
b4ed5cad | 2090 | bool udp_sum = false; |
f491e56d | 2091 | bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev)); |
d342894c | 2092 | |
61adedf3 | 2093 | info = skb_tunnel_info(skb); |
3093fbe7 | 2094 | |
ee122c79 | 2095 | if (rdst) { |
0770b53b | 2096 | dst = &rdst->remote_ip; |
2097 | if (vxlan_addr_any(dst)) { | |
2098 | if (did_rsc) { | |
2099 | /* short-circuited back to local bridge */ | |
3ad7a4b1 | 2100 | vxlan_encap_bypass(skb, vxlan, vxlan, default_vni); |
0770b53b | 2101 | return; |
2102 | } | |
2103 | goto drop; | |
2104 | } | |
2105 | ||
0dfbdf41 | 2106 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port; |
3ad7a4b1 | 2107 | vni = (rdst->remote_vni) ? : default_vni; |
49f810f0 | 2108 | ifindex = rdst->remote_ifindex; |
1158632b | 2109 | local_ip = vxlan->cfg.saddr; |
d71785ff | 2110 | dst_cache = &rdst->dst_cache; |
0770b53b | 2111 | md->gbp = skb->mark; |
2112 | ttl = vxlan->cfg.ttl; | |
2113 | if (!ttl && vxlan_addr_multicast(dst)) | |
2114 | ttl = 1; | |
2115 | ||
2116 | tos = vxlan->cfg.tos; | |
2117 | if (tos == 1) | |
2118 | tos = ip_tunnel_get_dsfield(old_iph, skb); | |
2119 | ||
2120 | if (dst->sa.sa_family == AF_INET) | |
2121 | udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX); | |
2122 | else | |
2123 | udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX); | |
2124 | label = vxlan->cfg.label; | |
ee122c79 TG |
2125 | } else { |
2126 | if (!info) { | |
2127 | WARN_ONCE(1, "%s: Missing encapsulation instructions\n", | |
2128 | dev->name); | |
2129 | goto drop; | |
2130 | } | |
b1be00a6 | 2131 | remote_ip.sa.sa_family = ip_tunnel_info_af(info); |
272d96a5 | 2132 | if (remote_ip.sa.sa_family == AF_INET) { |
a725e514 | 2133 | remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst; |
272d96a5 | 2134 | local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src; |
2135 | } else { | |
a725e514 | 2136 | remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst; |
272d96a5 | 2137 | local_ip.sin6.sin6_addr = info->key.u.ipv6.src; |
2138 | } | |
ee122c79 | 2139 | dst = &remote_ip; |
0770b53b | 2140 | dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port; |
2141 | vni = tunnel_id_to_key32(info->key.tun_id); | |
49f810f0 | 2142 | ifindex = 0; |
d71785ff | 2143 | dst_cache = &info->dst_cache; |
0770b53b | 2144 | if (info->options_len) |
2145 | md = ip_tunnel_info_opts(info); | |
a725e514 JB |
2146 | ttl = info->key.ttl; |
2147 | tos = info->key.tos; | |
e7f70af1 | 2148 | label = info->key.label; |
b4ed5cad | 2149 | udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); |
a725e514 | 2150 | } |
0770b53b | 2151 | src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min, |
2152 | vxlan->cfg.port_max, true); | |
a725e514 | 2153 | |
56de859e | 2154 | rcu_read_lock(); |
e4c7ed41 | 2155 | if (dst->sa.sa_family == AF_INET) { |
c6fcc4fc | 2156 | struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); |
c46b7897 | 2157 | struct rtable *rt; |
0770b53b | 2158 | __be16 df = 0; |
c6fcc4fc | 2159 | |
49f810f0 | 2160 | rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos, |
272d96a5 | 2161 | dst->sin.sin_addr.s_addr, |
1158632b | 2162 | &local_ip.sin.sin_addr.s_addr, |
4ecb1d83 | 2163 | dst_port, src_port, |
d71785ff | 2164 | dst_cache, info); |
8ebd115b DM |
2165 | if (IS_ERR(rt)) { |
2166 | err = PTR_ERR(rt); | |
c46b7897 | 2167 | goto tx_error; |
8ebd115b | 2168 | } |
e4c7ed41 CW |
2169 | |
2170 | /* Bypass encapsulation if the destination is local */ | |
fee1fad7 | 2171 | if (!info) { |
2172 | err = encap_bypass_if_local(skb, dev, vxlan, dst, | |
49f810f0 MS |
2173 | dst_port, ifindex, vni, |
2174 | &rt->dst, rt->rt_flags); | |
fee1fad7 | 2175 | if (err) |
56de859e | 2176 | goto out_unlock; |
fee1fad7 | 2177 | } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) { |
6ceb31ca | 2178 | df = htons(IP_DF); |
fee1fad7 | 2179 | } |
6ceb31ca | 2180 | |
c46b7897 | 2181 | ndst = &rt->dst; |
e4c7ed41 CW |
2182 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
2183 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); | |
c46b7897 | 2184 | err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr), |
54bfd872 | 2185 | vni, md, flags, udp_sum); |
f491e56d | 2186 | if (err < 0) |
c46b7897 | 2187 | goto tx_error; |
f491e56d | 2188 | |
1158632b | 2189 | udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr, |
f491e56d JB |
2190 | dst->sin.sin_addr.s_addr, tos, ttl, df, |
2191 | src_port, dst_port, xnet, !udp_sum); | |
e4c7ed41 CW |
2192 | #if IS_ENABLED(CONFIG_IPV6) |
2193 | } else { | |
c6fcc4fc | 2194 | struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); |
e4c7ed41 | 2195 | |
49f810f0 | 2196 | ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos, |
272d96a5 | 2197 | label, &dst->sin6.sin6_addr, |
1158632b | 2198 | &local_ip.sin6.sin6_addr, |
4ecb1d83 | 2199 | dst_port, src_port, |
db3c6139 | 2200 | dst_cache, info); |
e5d4b29f | 2201 | if (IS_ERR(ndst)) { |
8ebd115b | 2202 | err = PTR_ERR(ndst); |
c46b7897 | 2203 | ndst = NULL; |
9dcc71e1 | 2204 | goto tx_error; |
e4c7ed41 | 2205 | } |
655c3de1 | 2206 | |
fee1fad7 | 2207 | if (!info) { |
2208 | u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags; | |
49560532 | 2209 | |
fee1fad7 | 2210 | err = encap_bypass_if_local(skb, dev, vxlan, dst, |
49f810f0 MS |
2211 | dst_port, ifindex, vni, |
2212 | ndst, rt6i_flags); | |
fee1fad7 | 2213 | if (err) |
56de859e | 2214 | goto out_unlock; |
fee1fad7 | 2215 | } |
35e2d115 | 2216 | |
1400615d | 2217 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
e4c7ed41 | 2218 | ttl = ttl ? : ip6_dst_hoplimit(ndst); |
f491e56d JB |
2219 | skb_scrub_packet(skb, xnet); |
2220 | err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr), | |
54bfd872 | 2221 | vni, md, flags, udp_sum); |
c46b7897 | 2222 | if (err < 0) |
2223 | goto tx_error; | |
2224 | ||
0770b53b | 2225 | udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev, |
1158632b | 2226 | &local_ip.sin6.sin6_addr, |
272d96a5 | 2227 | &dst->sin6.sin6_addr, tos, ttl, |
e7f70af1 | 2228 | label, src_port, dst_port, !udp_sum); |
e4c7ed41 CW |
2229 | #endif |
2230 | } | |
56de859e JK |
2231 | out_unlock: |
2232 | rcu_read_unlock(); | |
4ad16930 | 2233 | return; |
d342894c | 2234 | |
2235 | drop: | |
2236 | dev->stats.tx_dropped++; | |
c46b7897 | 2237 | dev_kfree_skb(skb); |
2238 | return; | |
d342894c | 2239 | |
2240 | tx_error: | |
56de859e | 2241 | rcu_read_unlock(); |
655c3de1 | 2242 | if (err == -ELOOP) |
2243 | dev->stats.collisions++; | |
2244 | else if (err == -ENETUNREACH) | |
2245 | dev->stats.tx_carrier_errors++; | |
c46b7897 | 2246 | dst_release(ndst); |
d342894c | 2247 | dev->stats.tx_errors++; |
c46b7897 | 2248 | kfree_skb(skb); |
d342894c | 2249 | } |
2250 | ||
6681712d DS |
2251 | /* Transmit local packets over Vxlan |
2252 | * | |
2253 | * Outer IP header inherits ECN and DF from inner header. | |
2254 | * Outer UDP destination is the VXLAN assigned port. | |
2255 | * source port is based on hash of flow | |
2256 | */ | |
2257 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) | |
2258 | { | |
2259 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3093fbe7 | 2260 | const struct ip_tunnel_info *info; |
6681712d DS |
2261 | struct ethhdr *eth; |
2262 | bool did_rsc = false; | |
8f646c92 | 2263 | struct vxlan_rdst *rdst, *fdst = NULL; |
6681712d | 2264 | struct vxlan_fdb *f; |
3ad7a4b1 | 2265 | __be32 vni = 0; |
6681712d | 2266 | |
61adedf3 | 2267 | info = skb_tunnel_info(skb); |
3093fbe7 | 2268 | |
6681712d | 2269 | skb_reset_mac_header(skb); |
6681712d | 2270 | |
dc5321d7 | 2271 | if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) { |
3ad7a4b1 RP |
2272 | if (info && info->mode & IP_TUNNEL_INFO_BRIDGE && |
2273 | info->mode & IP_TUNNEL_INFO_TX) { | |
2274 | vni = tunnel_id_to_key32(info->key.tun_id); | |
2275 | } else { | |
2276 | if (info && info->mode & IP_TUNNEL_INFO_TX) | |
2277 | vxlan_xmit_one(skb, dev, vni, NULL, false); | |
2278 | else | |
2279 | kfree_skb(skb); | |
2280 | return NETDEV_TX_OK; | |
2281 | } | |
47e5d1b0 JB |
2282 | } |
2283 | ||
dc5321d7 | 2284 | if (vxlan->cfg.flags & VXLAN_F_PROXY) { |
47e5d1b0 | 2285 | eth = eth_hdr(skb); |
f564f45c | 2286 | if (ntohs(eth->h_proto) == ETH_P_ARP) |
3ad7a4b1 | 2287 | return arp_reduce(dev, skb, vni); |
f564f45c | 2288 | #if IS_ENABLED(CONFIG_IPV6) |
f1fb08f6 VB |
2289 | else if (ntohs(eth->h_proto) == ETH_P_IPV6) { |
2290 | struct ipv6hdr *hdr, _hdr; | |
2291 | if ((hdr = skb_header_pointer(skb, | |
2292 | skb_network_offset(skb), | |
2293 | sizeof(_hdr), &_hdr)) && | |
2294 | hdr->nexthdr == IPPROTO_ICMPV6) | |
2295 | return neigh_reduce(dev, skb, vni); | |
f564f45c CW |
2296 | } |
2297 | #endif | |
2298 | } | |
6681712d | 2299 | |
47e5d1b0 | 2300 | eth = eth_hdr(skb); |
3ad7a4b1 | 2301 | f = vxlan_find_mac(vxlan, eth->h_dest, vni); |
ae884082 DS |
2302 | did_rsc = false; |
2303 | ||
dc5321d7 | 2304 | if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) && |
e15a00aa CW |
2305 | (ntohs(eth->h_proto) == ETH_P_IP || |
2306 | ntohs(eth->h_proto) == ETH_P_IPV6)) { | |
ae884082 DS |
2307 | did_rsc = route_shortcircuit(dev, skb); |
2308 | if (did_rsc) | |
3ad7a4b1 | 2309 | f = vxlan_find_mac(vxlan, eth->h_dest, vni); |
ae884082 DS |
2310 | } |
2311 | ||
6681712d | 2312 | if (f == NULL) { |
3ad7a4b1 | 2313 | f = vxlan_find_mac(vxlan, all_zeros_mac, vni); |
afbd8bae | 2314 | if (f == NULL) { |
dc5321d7 | 2315 | if ((vxlan->cfg.flags & VXLAN_F_L2MISS) && |
afbd8bae MR |
2316 | !is_multicast_ether_addr(eth->h_dest)) |
2317 | vxlan_fdb_miss(vxlan, eth->h_dest); | |
2318 | ||
2319 | dev->stats.tx_dropped++; | |
8f646c92 | 2320 | kfree_skb(skb); |
afbd8bae MR |
2321 | return NETDEV_TX_OK; |
2322 | } | |
2323 | } | |
6681712d | 2324 | |
afbd8bae MR |
2325 | list_for_each_entry_rcu(rdst, &f->remotes, list) { |
2326 | struct sk_buff *skb1; | |
6681712d | 2327 | |
8f646c92 ED |
2328 | if (!fdst) { |
2329 | fdst = rdst; | |
2330 | continue; | |
2331 | } | |
afbd8bae MR |
2332 | skb1 = skb_clone(skb, GFP_ATOMIC); |
2333 | if (skb1) | |
3ad7a4b1 | 2334 | vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc); |
6681712d DS |
2335 | } |
2336 | ||
8f646c92 | 2337 | if (fdst) |
3ad7a4b1 | 2338 | vxlan_xmit_one(skb, dev, vni, fdst, did_rsc); |
8f646c92 ED |
2339 | else |
2340 | kfree_skb(skb); | |
4ad16930 | 2341 | return NETDEV_TX_OK; |
6681712d DS |
2342 | } |
2343 | ||
d342894c | 2344 | /* Walk the forwarding table and purge stale entries */ |
2345 | static void vxlan_cleanup(unsigned long arg) | |
2346 | { | |
2347 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; | |
2348 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; | |
2349 | unsigned int h; | |
2350 | ||
2351 | if (!netif_running(vxlan->dev)) | |
2352 | return; | |
2353 | ||
d342894c | 2354 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
2355 | struct hlist_node *p, *n; | |
14e1d0fa SD |
2356 | |
2357 | spin_lock_bh(&vxlan->hash_lock); | |
d342894c | 2358 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
2359 | struct vxlan_fdb *f | |
2360 | = container_of(p, struct vxlan_fdb, hlist); | |
2361 | unsigned long timeout; | |
2362 | ||
efb5f68f | 2363 | if (f->state & (NUD_PERMANENT | NUD_NOARP)) |
d342894c | 2364 | continue; |
2365 | ||
def499c9 RP |
2366 | if (f->flags & NTF_EXT_LEARNED) |
2367 | continue; | |
2368 | ||
0dfbdf41 | 2369 | timeout = f->used + vxlan->cfg.age_interval * HZ; |
d342894c | 2370 | if (time_before_eq(timeout, jiffies)) { |
2371 | netdev_dbg(vxlan->dev, | |
2372 | "garbage collect %pM\n", | |
2373 | f->eth_addr); | |
2374 | f->state = NUD_STALE; | |
2375 | vxlan_fdb_destroy(vxlan, f); | |
2376 | } else if (time_before(timeout, next_timer)) | |
2377 | next_timer = timeout; | |
2378 | } | |
14e1d0fa | 2379 | spin_unlock_bh(&vxlan->hash_lock); |
d342894c | 2380 | } |
d342894c | 2381 | |
2382 | mod_timer(&vxlan->age_timer, next_timer); | |
2383 | } | |
2384 | ||
a53cb29b MB |
2385 | static void vxlan_vs_del_dev(struct vxlan_dev *vxlan) |
2386 | { | |
2387 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); | |
2388 | ||
2389 | spin_lock(&vn->sock_lock); | |
69e76661 JB |
2390 | hlist_del_init_rcu(&vxlan->hlist4.hlist); |
2391 | #if IS_ENABLED(CONFIG_IPV6) | |
2392 | hlist_del_init_rcu(&vxlan->hlist6.hlist); | |
2393 | #endif | |
a53cb29b MB |
2394 | spin_unlock(&vn->sock_lock); |
2395 | } | |
2396 | ||
69e76661 JB |
2397 | static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan, |
2398 | struct vxlan_dev_node *node) | |
9c2e24e1 | 2399 | { |
56ef9c90 | 2400 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
54bfd872 | 2401 | __be32 vni = vxlan->default_dst.remote_vni; |
9c2e24e1 | 2402 | |
69e76661 | 2403 | node->vxlan = vxlan; |
56ef9c90 | 2404 | spin_lock(&vn->sock_lock); |
69e76661 | 2405 | hlist_add_head_rcu(&node->hlist, vni_head(vs, vni)); |
56ef9c90 | 2406 | spin_unlock(&vn->sock_lock); |
9c2e24e1 PS |
2407 | } |
2408 | ||
d342894c | 2409 | /* Setup stats when device is created */ |
2410 | static int vxlan_init(struct net_device *dev) | |
2411 | { | |
1c213bd2 | 2412 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
e8171045 | 2413 | if (!dev->tstats) |
d342894c | 2414 | return -ENOMEM; |
2415 | ||
2416 | return 0; | |
2417 | } | |
2418 | ||
3ad7a4b1 | 2419 | static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni) |
afbd8bae MR |
2420 | { |
2421 | struct vxlan_fdb *f; | |
2422 | ||
2423 | spin_lock_bh(&vxlan->hash_lock); | |
3ad7a4b1 | 2424 | f = __vxlan_find_mac(vxlan, all_zeros_mac, vni); |
afbd8bae MR |
2425 | if (f) |
2426 | vxlan_fdb_destroy(vxlan, f); | |
2427 | spin_unlock_bh(&vxlan->hash_lock); | |
2428 | } | |
2429 | ||
ebf4063e SH |
2430 | static void vxlan_uninit(struct net_device *dev) |
2431 | { | |
2432 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
ebf4063e | 2433 | |
3ad7a4b1 | 2434 | vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni); |
afbd8bae | 2435 | |
ebf4063e SH |
2436 | free_percpu(dev->tstats); |
2437 | } | |
2438 | ||
d342894c | 2439 | /* Start ageing timer and join group when device is brought up */ |
2440 | static int vxlan_open(struct net_device *dev) | |
2441 | { | |
2442 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
205f356d | 2443 | int ret; |
56ef9c90 | 2444 | |
205f356d JB |
2445 | ret = vxlan_sock_add(vxlan); |
2446 | if (ret < 0) | |
2447 | return ret; | |
d342894c | 2448 | |
79d4a94f | 2449 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) { |
56ef9c90 | 2450 | ret = vxlan_igmp_join(vxlan); |
bef0057b MRL |
2451 | if (ret == -EADDRINUSE) |
2452 | ret = 0; | |
56ef9c90 | 2453 | if (ret) { |
205f356d | 2454 | vxlan_sock_release(vxlan); |
56ef9c90 MRL |
2455 | return ret; |
2456 | } | |
d342894c | 2457 | } |
2458 | ||
0dfbdf41 | 2459 | if (vxlan->cfg.age_interval) |
d342894c | 2460 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); |
2461 | ||
56ef9c90 | 2462 | return ret; |
d342894c | 2463 | } |
2464 | ||
2465 | /* Purge the forwarding table */ | |
8b3f9337 | 2466 | static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all) |
d342894c | 2467 | { |
31fec5aa | 2468 | unsigned int h; |
d342894c | 2469 | |
2470 | spin_lock_bh(&vxlan->hash_lock); | |
2471 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
2472 | struct hlist_node *p, *n; | |
2473 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { | |
2474 | struct vxlan_fdb *f | |
2475 | = container_of(p, struct vxlan_fdb, hlist); | |
8b3f9337 RP |
2476 | if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP))) |
2477 | continue; | |
afbd8bae MR |
2478 | /* the all_zeros_mac entry is deleted at vxlan_uninit */ |
2479 | if (!is_zero_ether_addr(f->eth_addr)) | |
2480 | vxlan_fdb_destroy(vxlan, f); | |
d342894c | 2481 | } |
2482 | } | |
2483 | spin_unlock_bh(&vxlan->hash_lock); | |
2484 | } | |
2485 | ||
2486 | /* Cleanup timer and forwarding table on shutdown */ | |
2487 | static int vxlan_stop(struct net_device *dev) | |
2488 | { | |
2489 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
f01ec1c0 | 2490 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
56ef9c90 | 2491 | int ret = 0; |
d342894c | 2492 | |
24c0e683 | 2493 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && |
f13b1689 | 2494 | !vxlan_group_used(vn, vxlan)) |
56ef9c90 | 2495 | ret = vxlan_igmp_leave(vxlan); |
d342894c | 2496 | |
2497 | del_timer_sync(&vxlan->age_timer); | |
2498 | ||
8b3f9337 | 2499 | vxlan_flush(vxlan, false); |
205f356d | 2500 | vxlan_sock_release(vxlan); |
d342894c | 2501 | |
56ef9c90 | 2502 | return ret; |
d342894c | 2503 | } |
2504 | ||
d342894c | 2505 | /* Stub, nothing needs to be done. */ |
2506 | static void vxlan_set_multicast_list(struct net_device *dev) | |
2507 | { | |
2508 | } | |
2509 | ||
91572088 | 2510 | static int vxlan_change_mtu(struct net_device *dev, int new_mtu) |
345010b5 | 2511 | { |
91572088 JW |
2512 | struct vxlan_dev *vxlan = netdev_priv(dev); |
2513 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
2514 | struct net_device *lowerdev = __dev_get_by_index(vxlan->net, | |
2515 | dst->remote_ifindex); | |
ce44a4ae | 2516 | bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6); |
345010b5 | 2517 | |
91572088 JW |
2518 | /* This check is different than dev->max_mtu, because it looks at |
2519 | * the lowerdev->mtu, rather than the static dev->max_mtu | |
2520 | */ | |
2521 | if (lowerdev) { | |
2522 | int max_mtu = lowerdev->mtu - | |
2523 | (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); | |
2524 | if (new_mtu > max_mtu) | |
72564b59 | 2525 | return -EINVAL; |
72564b59 DW |
2526 | } |
2527 | ||
345010b5 DB |
2528 | dev->mtu = new_mtu; |
2529 | return 0; | |
2530 | } | |
2531 | ||
fc4099f1 PS |
2532 | static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
2533 | { | |
2534 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
2535 | struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
2536 | __be16 sport, dport; | |
2537 | ||
2538 | sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min, | |
2539 | vxlan->cfg.port_max, true); | |
2540 | dport = info->key.tp_dst ? : vxlan->cfg.dst_port; | |
2541 | ||
239e944f | 2542 | if (ip_tunnel_info_af(info) == AF_INET) { |
c6fcc4fc | 2543 | struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); |
1a8496ba JB |
2544 | struct rtable *rt; |
2545 | ||
655c3de1 | 2546 | rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos, |
1a8496ba | 2547 | info->key.u.ipv4.dst, |
22f0708a PA |
2548 | &info->key.u.ipv4.src, dport, sport, |
2549 | &info->dst_cache, info); | |
1a8496ba JB |
2550 | if (IS_ERR(rt)) |
2551 | return PTR_ERR(rt); | |
2552 | ip_rt_put(rt); | |
239e944f JB |
2553 | } else { |
2554 | #if IS_ENABLED(CONFIG_IPV6) | |
03dc52a8 | 2555 | struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); |
239e944f JB |
2556 | struct dst_entry *ndst; |
2557 | ||
655c3de1 | 2558 | ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos, |
e7f70af1 | 2559 | info->key.label, &info->key.u.ipv6.dst, |
22f0708a PA |
2560 | &info->key.u.ipv6.src, dport, sport, |
2561 | &info->dst_cache, info); | |
239e944f JB |
2562 | if (IS_ERR(ndst)) |
2563 | return PTR_ERR(ndst); | |
2564 | dst_release(ndst); | |
239e944f JB |
2565 | #else /* !CONFIG_IPV6 */ |
2566 | return -EPFNOSUPPORT; | |
2567 | #endif | |
2568 | } | |
1a8496ba JB |
2569 | info->key.tp_src = sport; |
2570 | info->key.tp_dst = dport; | |
239e944f | 2571 | return 0; |
fc4099f1 PS |
2572 | } |
2573 | ||
0c867c9b | 2574 | static const struct net_device_ops vxlan_netdev_ether_ops = { |
d342894c | 2575 | .ndo_init = vxlan_init, |
ebf4063e | 2576 | .ndo_uninit = vxlan_uninit, |
d342894c | 2577 | .ndo_open = vxlan_open, |
2578 | .ndo_stop = vxlan_stop, | |
2579 | .ndo_start_xmit = vxlan_xmit, | |
e8171045 | 2580 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
d342894c | 2581 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
345010b5 | 2582 | .ndo_change_mtu = vxlan_change_mtu, |
d342894c | 2583 | .ndo_validate_addr = eth_validate_addr, |
2584 | .ndo_set_mac_address = eth_mac_addr, | |
2585 | .ndo_fdb_add = vxlan_fdb_add, | |
2586 | .ndo_fdb_del = vxlan_fdb_delete, | |
2587 | .ndo_fdb_dump = vxlan_fdb_dump, | |
fc4099f1 | 2588 | .ndo_fill_metadata_dst = vxlan_fill_metadata_dst, |
d342894c | 2589 | }; |
2590 | ||
e1e5314d JB |
2591 | static const struct net_device_ops vxlan_netdev_raw_ops = { |
2592 | .ndo_init = vxlan_init, | |
2593 | .ndo_uninit = vxlan_uninit, | |
2594 | .ndo_open = vxlan_open, | |
2595 | .ndo_stop = vxlan_stop, | |
2596 | .ndo_start_xmit = vxlan_xmit, | |
2597 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
2598 | .ndo_change_mtu = vxlan_change_mtu, | |
2599 | .ndo_fill_metadata_dst = vxlan_fill_metadata_dst, | |
2600 | }; | |
2601 | ||
d342894c | 2602 | /* Info for udev, that this is a virtual tunnel endpoint */ |
2603 | static struct device_type vxlan_type = { | |
2604 | .name = "vxlan", | |
2605 | }; | |
2606 | ||
e5de25dc | 2607 | /* Calls the ndo_udp_tunnel_add of the caller in order to |
35e42379 | 2608 | * supply the listening VXLAN udp ports. Callers are expected |
e5de25dc | 2609 | * to implement the ndo_udp_tunnel_add. |
53cf5275 | 2610 | */ |
b7aade15 | 2611 | static void vxlan_push_rx_ports(struct net_device *dev) |
53cf5275 JG |
2612 | { |
2613 | struct vxlan_sock *vs; | |
2614 | struct net *net = dev_net(dev); | |
2615 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
35e42379 | 2616 | unsigned int i; |
53cf5275 JG |
2617 | |
2618 | spin_lock(&vn->sock_lock); | |
2619 | for (i = 0; i < PORT_HASH_SIZE; ++i) { | |
e7b3db5e AD |
2620 | hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) |
2621 | udp_tunnel_push_rx_port(dev, vs->sock, | |
b9adcd69 AD |
2622 | (vs->flags & VXLAN_F_GPE) ? |
2623 | UDP_TUNNEL_TYPE_VXLAN_GPE : | |
e7b3db5e | 2624 | UDP_TUNNEL_TYPE_VXLAN); |
53cf5275 JG |
2625 | } |
2626 | spin_unlock(&vn->sock_lock); | |
2627 | } | |
53cf5275 | 2628 | |
d342894c | 2629 | /* Initialize the device structure. */ |
2630 | static void vxlan_setup(struct net_device *dev) | |
2631 | { | |
2632 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
31fec5aa | 2633 | unsigned int h; |
d342894c | 2634 | |
65226ef8 JB |
2635 | eth_hw_addr_random(dev); |
2636 | ether_setup(dev); | |
2637 | ||
cf124db5 | 2638 | dev->needs_free_netdev = true; |
d342894c | 2639 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
2640 | ||
d342894c | 2641 | dev->features |= NETIF_F_LLTX; |
d6727fe3 | 2642 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
0afb1666 | 2643 | dev->features |= NETIF_F_RXCSUM; |
05c0db08 | 2644 | dev->features |= NETIF_F_GSO_SOFTWARE; |
0afb1666 | 2645 | |
1eaa8178 | 2646 | dev->vlan_features = dev->features; |
0afb1666 | 2647 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
05c0db08 | 2648 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
02875878 | 2649 | netif_keep_dst(dev); |
0c867c9b | 2650 | dev->priv_flags |= IFF_NO_QUEUE; |
d342894c | 2651 | |
a985343b MS |
2652 | /* MTU range: 68 - 65535 */ |
2653 | dev->min_mtu = ETH_MIN_MTU; | |
2654 | dev->max_mtu = ETH_MAX_MTU; | |
2655 | ||
553675fb | 2656 | INIT_LIST_HEAD(&vxlan->next); |
d342894c | 2657 | spin_lock_init(&vxlan->hash_lock); |
2658 | ||
2659 | init_timer_deferrable(&vxlan->age_timer); | |
2660 | vxlan->age_timer.function = vxlan_cleanup; | |
2661 | vxlan->age_timer.data = (unsigned long) vxlan; | |
2662 | ||
2663 | vxlan->dev = dev; | |
2664 | ||
58ce31cc TH |
2665 | gro_cells_init(&vxlan->gro_cells, dev); |
2666 | ||
d342894c | 2667 | for (h = 0; h < FDB_HASH_SIZE; ++h) |
2668 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); | |
2669 | } | |
2670 | ||
0c867c9b JB |
2671 | static void vxlan_ether_setup(struct net_device *dev) |
2672 | { | |
0c867c9b JB |
2673 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
2674 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
2675 | dev->netdev_ops = &vxlan_netdev_ether_ops; | |
2676 | } | |
2677 | ||
e1e5314d JB |
2678 | static void vxlan_raw_setup(struct net_device *dev) |
2679 | { | |
65226ef8 | 2680 | dev->header_ops = NULL; |
e1e5314d JB |
2681 | dev->type = ARPHRD_NONE; |
2682 | dev->hard_header_len = 0; | |
2683 | dev->addr_len = 0; | |
e1e5314d JB |
2684 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
2685 | dev->netdev_ops = &vxlan_netdev_raw_ops; | |
2686 | } | |
2687 | ||
d342894c | 2688 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { |
2689 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, | |
5d174dd8 | 2690 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
e4c7ed41 | 2691 | [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) }, |
d342894c | 2692 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
2693 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, | |
e4c7ed41 | 2694 | [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) }, |
d342894c | 2695 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
2696 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, | |
e7f70af1 | 2697 | [IFLA_VXLAN_LABEL] = { .type = NLA_U32 }, |
d342894c | 2698 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, |
2699 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, | |
2700 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, | |
05f47d69 | 2701 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
e4f67add DS |
2702 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
2703 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, | |
2704 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, | |
2705 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, | |
f8a9b1bc | 2706 | [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 }, |
823aa873 | 2707 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
5c91ae08 TH |
2708 | [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 }, |
2709 | [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, | |
2710 | [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, | |
dfd8645e TH |
2711 | [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 }, |
2712 | [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 }, | |
3511494c | 2713 | [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, }, |
e1e5314d | 2714 | [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, }, |
0ace2ca8 | 2715 | [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG }, |
d342894c | 2716 | }; |
2717 | ||
a8b8a889 MS |
2718 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[], |
2719 | struct netlink_ext_ack *extack) | |
d342894c | 2720 | { |
2721 | if (tb[IFLA_ADDRESS]) { | |
2722 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { | |
2723 | pr_debug("invalid link address (not ethernet)\n"); | |
2724 | return -EINVAL; | |
2725 | } | |
2726 | ||
2727 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { | |
2728 | pr_debug("invalid all zero ethernet address\n"); | |
2729 | return -EADDRNOTAVAIL; | |
2730 | } | |
2731 | } | |
2732 | ||
a985343b | 2733 | if (tb[IFLA_MTU]) { |
019b13ae | 2734 | u32 mtu = nla_get_u32(tb[IFLA_MTU]); |
a985343b MS |
2735 | |
2736 | if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) | |
2737 | return -EINVAL; | |
2738 | } | |
2739 | ||
d342894c | 2740 | if (!data) |
2741 | return -EINVAL; | |
2742 | ||
2743 | if (data[IFLA_VXLAN_ID]) { | |
a985343b MS |
2744 | u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); |
2745 | ||
4e37d691 | 2746 | if (id >= VXLAN_N_VID) |
d342894c | 2747 | return -ERANGE; |
2748 | } | |
2749 | ||
05f47d69 | 2750 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
2751 | const struct ifla_vxlan_port_range *p | |
2752 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
2753 | ||
2754 | if (ntohs(p->high) < ntohs(p->low)) { | |
2755 | pr_debug("port range %u .. %u not valid\n", | |
2756 | ntohs(p->low), ntohs(p->high)); | |
2757 | return -EINVAL; | |
2758 | } | |
2759 | } | |
2760 | ||
d342894c | 2761 | return 0; |
2762 | } | |
2763 | ||
1b13c97f YB |
2764 | static void vxlan_get_drvinfo(struct net_device *netdev, |
2765 | struct ethtool_drvinfo *drvinfo) | |
2766 | { | |
2767 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); | |
2768 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); | |
2769 | } | |
2770 | ||
2771 | static const struct ethtool_ops vxlan_ethtool_ops = { | |
2772 | .get_drvinfo = vxlan_get_drvinfo, | |
2773 | .get_link = ethtool_op_get_link, | |
2774 | }; | |
2775 | ||
3ee64f39 TH |
2776 | static struct socket *vxlan_create_sock(struct net *net, bool ipv6, |
2777 | __be16 port, u32 flags) | |
553675fb | 2778 | { |
e4c7ed41 | 2779 | struct socket *sock; |
3ee64f39 TH |
2780 | struct udp_port_cfg udp_conf; |
2781 | int err; | |
e4c7ed41 | 2782 | |
3ee64f39 | 2783 | memset(&udp_conf, 0, sizeof(udp_conf)); |
e4c7ed41 | 2784 | |
3ee64f39 TH |
2785 | if (ipv6) { |
2786 | udp_conf.family = AF_INET6; | |
3ee64f39 | 2787 | udp_conf.use_udp6_rx_checksums = |
3dc2b6a8 | 2788 | !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX); |
a43a9ef6 | 2789 | udp_conf.ipv6_v6only = 1; |
3ee64f39 TH |
2790 | } else { |
2791 | udp_conf.family = AF_INET; | |
e4c7ed41 | 2792 | } |
e4c7ed41 | 2793 | |
3ee64f39 | 2794 | udp_conf.local_udp_port = port; |
553675fb | 2795 | |
3ee64f39 TH |
2796 | /* Open UDP socket */ |
2797 | err = udp_sock_create(net, &udp_conf, &sock); | |
2798 | if (err < 0) | |
2799 | return ERR_PTR(err); | |
e4c7ed41 | 2800 | |
39deb2c7 | 2801 | return sock; |
e4c7ed41 CW |
2802 | } |
2803 | ||
2804 | /* Create new listen socket if needed */ | |
b1be00a6 JB |
2805 | static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6, |
2806 | __be16 port, u32 flags) | |
e4c7ed41 CW |
2807 | { |
2808 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2809 | struct vxlan_sock *vs; | |
2810 | struct socket *sock; | |
e4c7ed41 | 2811 | unsigned int h; |
acbf74a7 | 2812 | struct udp_tunnel_sock_cfg tunnel_cfg; |
e4c7ed41 | 2813 | |
dc01e7d3 | 2814 | vs = kzalloc(sizeof(*vs), GFP_KERNEL); |
e4c7ed41 CW |
2815 | if (!vs) |
2816 | return ERR_PTR(-ENOMEM); | |
2817 | ||
2818 | for (h = 0; h < VNI_HASH_SIZE; ++h) | |
2819 | INIT_HLIST_HEAD(&vs->vni_list[h]); | |
2820 | ||
3ee64f39 | 2821 | sock = vxlan_create_sock(net, ipv6, port, flags); |
39deb2c7 | 2822 | if (IS_ERR(sock)) { |
553675fb | 2823 | kfree(vs); |
e50fddc8 | 2824 | return ERR_CAST(sock); |
553675fb | 2825 | } |
e4c7ed41 CW |
2826 | |
2827 | vs->sock = sock; | |
66af846f | 2828 | refcount_set(&vs->refcnt, 1); |
af33c1ad | 2829 | vs->flags = (flags & VXLAN_F_RCV_FLAGS); |
553675fb | 2830 | |
9c2e24e1 PS |
2831 | spin_lock(&vn->sock_lock); |
2832 | hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); | |
e7b3db5e | 2833 | udp_tunnel_notify_add_rx_port(sock, |
b9adcd69 AD |
2834 | (vs->flags & VXLAN_F_GPE) ? |
2835 | UDP_TUNNEL_TYPE_VXLAN_GPE : | |
e7b3db5e | 2836 | UDP_TUNNEL_TYPE_VXLAN); |
9c2e24e1 | 2837 | spin_unlock(&vn->sock_lock); |
553675fb | 2838 | |
2839 | /* Mark socket as an encapsulation socket. */ | |
5602c48c | 2840 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
acbf74a7 AZ |
2841 | tunnel_cfg.sk_user_data = vs; |
2842 | tunnel_cfg.encap_type = 1; | |
f2d1968e | 2843 | tunnel_cfg.encap_rcv = vxlan_rcv; |
acbf74a7 | 2844 | tunnel_cfg.encap_destroy = NULL; |
5602c48c TH |
2845 | tunnel_cfg.gro_receive = vxlan_gro_receive; |
2846 | tunnel_cfg.gro_complete = vxlan_gro_complete; | |
acbf74a7 AZ |
2847 | |
2848 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); | |
e4c7ed41 | 2849 | |
9c2e24e1 PS |
2850 | return vs; |
2851 | } | |
2852 | ||
b1be00a6 | 2853 | static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6) |
9c2e24e1 | 2854 | { |
205f356d JB |
2855 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
2856 | struct vxlan_sock *vs = NULL; | |
69e76661 | 2857 | struct vxlan_dev_node *node; |
9c2e24e1 | 2858 | |
205f356d | 2859 | if (!vxlan->cfg.no_share) { |
56ef9c90 | 2860 | spin_lock(&vn->sock_lock); |
205f356d | 2861 | vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET, |
dc5321d7 | 2862 | vxlan->cfg.dst_port, vxlan->cfg.flags); |
66af846f | 2863 | if (vs && !refcount_inc_not_zero(&vs->refcnt)) { |
56ef9c90 | 2864 | spin_unlock(&vn->sock_lock); |
205f356d | 2865 | return -EBUSY; |
56ef9c90 MRL |
2866 | } |
2867 | spin_unlock(&vn->sock_lock); | |
2868 | } | |
205f356d | 2869 | if (!vs) |
b1be00a6 | 2870 | vs = vxlan_socket_create(vxlan->net, ipv6, |
dc5321d7 | 2871 | vxlan->cfg.dst_port, vxlan->cfg.flags); |
205f356d JB |
2872 | if (IS_ERR(vs)) |
2873 | return PTR_ERR(vs); | |
b1be00a6 | 2874 | #if IS_ENABLED(CONFIG_IPV6) |
69e76661 | 2875 | if (ipv6) { |
c6fcc4fc | 2876 | rcu_assign_pointer(vxlan->vn6_sock, vs); |
69e76661 JB |
2877 | node = &vxlan->hlist6; |
2878 | } else | |
b1be00a6 | 2879 | #endif |
69e76661 | 2880 | { |
c6fcc4fc | 2881 | rcu_assign_pointer(vxlan->vn4_sock, vs); |
69e76661 JB |
2882 | node = &vxlan->hlist4; |
2883 | } | |
2884 | vxlan_vs_add_dev(vs, vxlan, node); | |
205f356d | 2885 | return 0; |
553675fb | 2886 | } |
2887 | ||
b1be00a6 JB |
2888 | static int vxlan_sock_add(struct vxlan_dev *vxlan) |
2889 | { | |
dc5321d7 MS |
2890 | bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA; |
2891 | bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata; | |
d074bf96 | 2892 | bool ipv4 = !ipv6 || metadata; |
b1be00a6 JB |
2893 | int ret = 0; |
2894 | ||
c6fcc4fc | 2895 | RCU_INIT_POINTER(vxlan->vn4_sock, NULL); |
b1be00a6 | 2896 | #if IS_ENABLED(CONFIG_IPV6) |
c6fcc4fc | 2897 | RCU_INIT_POINTER(vxlan->vn6_sock, NULL); |
d074bf96 | 2898 | if (ipv6) { |
b1be00a6 | 2899 | ret = __vxlan_sock_add(vxlan, true); |
d074bf96 JB |
2900 | if (ret < 0 && ret != -EAFNOSUPPORT) |
2901 | ipv4 = false; | |
2902 | } | |
b1be00a6 | 2903 | #endif |
d074bf96 | 2904 | if (ipv4) |
b1be00a6 JB |
2905 | ret = __vxlan_sock_add(vxlan, false); |
2906 | if (ret < 0) | |
2907 | vxlan_sock_release(vxlan); | |
2908 | return ret; | |
2909 | } | |
2910 | ||
a985343b MS |
2911 | static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf, |
2912 | struct net_device **lower, | |
2913 | struct vxlan_dev *old) | |
d342894c | 2914 | { |
33564bbb | 2915 | struct vxlan_net *vn = net_generic(src_net, vxlan_net_id); |
a985343b | 2916 | struct vxlan_dev *tmp; |
e4c7ed41 | 2917 | bool use_ipv6 = false; |
d342894c | 2918 | |
a985343b MS |
2919 | if (conf->flags & VXLAN_F_GPE) { |
2920 | /* For now, allow GPE only together with | |
2921 | * COLLECT_METADATA. This can be relaxed later; in such | |
2922 | * case, the other side of the PtP link will have to be | |
2923 | * provided. | |
2924 | */ | |
2925 | if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) || | |
2926 | !(conf->flags & VXLAN_F_COLLECT_METADATA)) { | |
2927 | return -EINVAL; | |
3555621d | 2928 | } |
e1e5314d | 2929 | } |
0c867c9b | 2930 | |
ce44a4ae MS |
2931 | if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) { |
2932 | /* Unless IPv6 is explicitly requested, assume IPv4 */ | |
a985343b | 2933 | conf->remote_ip.sa.sa_family = AF_INET; |
ce44a4ae MS |
2934 | conf->saddr.sa.sa_family = AF_INET; |
2935 | } else if (!conf->remote_ip.sa.sa_family) { | |
2936 | conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family; | |
2937 | } else if (!conf->saddr.sa.sa_family) { | |
2938 | conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family; | |
2939 | } | |
2940 | ||
2941 | if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) | |
2942 | return -EINVAL; | |
e4c7ed41 | 2943 | |
0f22a3c6 MS |
2944 | if (vxlan_addr_multicast(&conf->saddr)) |
2945 | return -EINVAL; | |
2946 | ||
ce44a4ae | 2947 | if (conf->saddr.sa.sa_family == AF_INET6) { |
057ba29b JB |
2948 | if (!IS_ENABLED(CONFIG_IPV6)) |
2949 | return -EPFNOSUPPORT; | |
e4c7ed41 | 2950 | use_ipv6 = true; |
a985343b | 2951 | conf->flags |= VXLAN_F_IPV6; |
0f22a3c6 MS |
2952 | |
2953 | if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) { | |
2954 | int local_type = | |
2955 | ipv6_addr_type(&conf->saddr.sin6.sin6_addr); | |
2956 | int remote_type = | |
2957 | ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr); | |
2958 | ||
2959 | if (local_type & IPV6_ADDR_LINKLOCAL) { | |
2960 | if (!(remote_type & IPV6_ADDR_LINKLOCAL) && | |
2961 | (remote_type != IPV6_ADDR_ANY)) | |
2962 | return -EINVAL; | |
2963 | ||
2964 | conf->flags |= VXLAN_F_IPV6_LINKLOCAL; | |
2965 | } else { | |
2966 | if (remote_type == | |
2967 | (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) | |
2968 | return -EINVAL; | |
2969 | ||
2970 | conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL; | |
2971 | } | |
2972 | } | |
057ba29b | 2973 | } |
d342894c | 2974 | |
a985343b | 2975 | if (conf->label && !use_ipv6) |
e7f70af1 | 2976 | return -EINVAL; |
e7f70af1 | 2977 | |
a985343b MS |
2978 | if (conf->remote_ifindex) { |
2979 | struct net_device *lowerdev; | |
34e02aa1 | 2980 | |
a985343b MS |
2981 | lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex); |
2982 | if (!lowerdev) | |
34e02aa1 | 2983 | return -ENODEV; |
d342894c | 2984 | |
e4c7ed41 CW |
2985 | #if IS_ENABLED(CONFIG_IPV6) |
2986 | if (use_ipv6) { | |
2987 | struct inet6_dev *idev = __in6_dev_get(lowerdev); | |
a985343b | 2988 | if (idev && idev->cnf.disable_ipv6) |
e4c7ed41 | 2989 | return -EPERM; |
e4c7ed41 CW |
2990 | } |
2991 | #endif | |
2992 | ||
a985343b MS |
2993 | *lower = lowerdev; |
2994 | } else { | |
2995 | if (vxlan_addr_multicast(&conf->remote_ip)) | |
2996 | return -EINVAL; | |
1ba56fb4 | 2997 | |
0f22a3c6 MS |
2998 | #if IS_ENABLED(CONFIG_IPV6) |
2999 | if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) | |
3000 | return -EINVAL; | |
3001 | #endif | |
3002 | ||
a985343b | 3003 | *lower = NULL; |
9dc2ad10 | 3004 | } |
d342894c | 3005 | |
a985343b MS |
3006 | if (!conf->dst_port) { |
3007 | if (conf->flags & VXLAN_F_GPE) | |
3008 | conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */ | |
3009 | else | |
3010 | conf->dst_port = htons(vxlan_port); | |
d6acfeb1 FM |
3011 | } |
3012 | ||
a985343b MS |
3013 | if (!conf->age_interval) |
3014 | conf->age_interval = FDB_AGE_DEFAULT; | |
91572088 | 3015 | |
a985343b MS |
3016 | list_for_each_entry(tmp, &vn->vxlan_list, next) { |
3017 | if (tmp == old) | |
3018 | continue; | |
91572088 | 3019 | |
49f810f0 MS |
3020 | if (tmp->cfg.vni != conf->vni) |
3021 | continue; | |
3022 | if (tmp->cfg.dst_port != conf->dst_port) | |
3023 | continue; | |
3024 | if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) != | |
ce44a4ae | 3025 | (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6))) |
49f810f0 MS |
3026 | continue; |
3027 | ||
3028 | if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) && | |
3029 | tmp->cfg.remote_ifindex != conf->remote_ifindex) | |
3030 | continue; | |
3031 | ||
3032 | return -EEXIST; | |
a985343b | 3033 | } |
91572088 | 3034 | |
a985343b MS |
3035 | return 0; |
3036 | } | |
3037 | ||
3038 | static void vxlan_config_apply(struct net_device *dev, | |
3039 | struct vxlan_config *conf, | |
889ce937 SD |
3040 | struct net_device *lowerdev, |
3041 | struct net *src_net, | |
3042 | bool changelink) | |
a985343b MS |
3043 | { |
3044 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3045 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
3046 | unsigned short needed_headroom = ETH_HLEN; | |
3047 | bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6); | |
3048 | int max_mtu = ETH_MAX_MTU; | |
3049 | ||
3050 | if (!changelink) { | |
3051 | if (conf->flags & VXLAN_F_GPE) | |
3052 | vxlan_raw_setup(dev); | |
3053 | else | |
3054 | vxlan_ether_setup(dev); | |
3055 | ||
3056 | if (conf->mtu) | |
3057 | dev->mtu = conf->mtu; | |
889ce937 SD |
3058 | |
3059 | vxlan->net = src_net; | |
a985343b MS |
3060 | } |
3061 | ||
3062 | dst->remote_vni = conf->vni; | |
3063 | ||
3064 | memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip)); | |
3065 | ||
3066 | if (lowerdev) { | |
3067 | dst->remote_ifindex = conf->remote_ifindex; | |
3068 | ||
3069 | dev->gso_max_size = lowerdev->gso_max_size; | |
3070 | dev->gso_max_segs = lowerdev->gso_max_segs; | |
91572088 | 3071 | |
a985343b | 3072 | needed_headroom = lowerdev->hard_header_len; |
91572088 | 3073 | |
a985343b MS |
3074 | max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : |
3075 | VXLAN_HEADROOM); | |
7e059158 DW |
3076 | } |
3077 | ||
a985343b MS |
3078 | if (dev->mtu > max_mtu) |
3079 | dev->mtu = max_mtu; | |
3080 | ||
b1be00a6 JB |
3081 | if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA) |
3082 | needed_headroom += VXLAN6_HEADROOM; | |
3083 | else | |
3084 | needed_headroom += VXLAN_HEADROOM; | |
3085 | dev->needed_headroom = needed_headroom; | |
3086 | ||
0dfbdf41 | 3087 | memcpy(&vxlan->cfg, conf, sizeof(*conf)); |
a985343b | 3088 | } |
0dfbdf41 | 3089 | |
a985343b MS |
3090 | static int vxlan_dev_configure(struct net *src_net, struct net_device *dev, |
3091 | struct vxlan_config *conf, | |
3092 | bool changelink) | |
3093 | { | |
3094 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3095 | struct net_device *lowerdev; | |
3096 | int ret; | |
0dfbdf41 | 3097 | |
a985343b MS |
3098 | ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan); |
3099 | if (ret) | |
3100 | return ret; | |
8bcdc4f3 | 3101 | |
889ce937 | 3102 | vxlan_config_apply(dev, conf, lowerdev, src_net, changelink); |
0dfbdf41 | 3103 | |
0dfbdf41 TG |
3104 | return 0; |
3105 | } | |
3106 | ||
c80498e3 ND |
3107 | static int __vxlan_dev_create(struct net *net, struct net_device *dev, |
3108 | struct vxlan_config *conf) | |
3109 | { | |
3110 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
3111 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3112 | int err; | |
3113 | ||
3114 | err = vxlan_dev_configure(net, dev, conf, false); | |
3115 | if (err) | |
3116 | return err; | |
3117 | ||
3118 | dev->ethtool_ops = &vxlan_ethtool_ops; | |
3119 | ||
3120 | /* create an fdb entry for a valid default destination */ | |
3121 | if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) { | |
3122 | err = vxlan_fdb_create(vxlan, all_zeros_mac, | |
3123 | &vxlan->default_dst.remote_ip, | |
3124 | NUD_REACHABLE | NUD_PERMANENT, | |
3125 | NLM_F_EXCL | NLM_F_CREATE, | |
3126 | vxlan->cfg.dst_port, | |
3127 | vxlan->default_dst.remote_vni, | |
3128 | vxlan->default_dst.remote_vni, | |
3129 | vxlan->default_dst.remote_ifindex, | |
3130 | NTF_SELF); | |
3131 | if (err) | |
3132 | return err; | |
3133 | } | |
3134 | ||
3135 | err = register_netdevice(dev); | |
3136 | if (err) { | |
3137 | vxlan_fdb_delete_default(vxlan, vxlan->default_dst.remote_vni); | |
3138 | return err; | |
3139 | } | |
3140 | ||
3141 | list_add(&vxlan->next, &vn->vxlan_list); | |
3142 | return 0; | |
3143 | } | |
3144 | ||
8bcdc4f3 RP |
3145 | static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[], |
3146 | struct net_device *dev, struct vxlan_config *conf, | |
3147 | bool changelink) | |
0dfbdf41 | 3148 | { |
8bcdc4f3 | 3149 | struct vxlan_dev *vxlan = netdev_priv(dev); |
0dfbdf41 | 3150 | |
8bcdc4f3 | 3151 | memset(conf, 0, sizeof(*conf)); |
e277de5f | 3152 | |
8bcdc4f3 RP |
3153 | /* if changelink operation, start with old existing cfg */ |
3154 | if (changelink) | |
3155 | memcpy(conf, &vxlan->cfg, sizeof(*conf)); | |
3156 | ||
3157 | if (data[IFLA_VXLAN_ID]) { | |
3158 | __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID])); | |
3159 | ||
3160 | if (changelink && (vni != conf->vni)) | |
3161 | return -EOPNOTSUPP; | |
3162 | conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID])); | |
3163 | } | |
0dfbdf41 TG |
3164 | |
3165 | if (data[IFLA_VXLAN_GROUP]) { | |
ce44a4ae MS |
3166 | if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) |
3167 | return -EOPNOTSUPP; | |
3168 | ||
8bcdc4f3 | 3169 | conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]); |
ce44a4ae | 3170 | conf->remote_ip.sa.sa_family = AF_INET; |
0dfbdf41 TG |
3171 | } else if (data[IFLA_VXLAN_GROUP6]) { |
3172 | if (!IS_ENABLED(CONFIG_IPV6)) | |
3173 | return -EPFNOSUPPORT; | |
3174 | ||
ce44a4ae MS |
3175 | if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) |
3176 | return -EOPNOTSUPP; | |
3177 | ||
8bcdc4f3 RP |
3178 | conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]); |
3179 | conf->remote_ip.sa.sa_family = AF_INET6; | |
0dfbdf41 TG |
3180 | } |
3181 | ||
3182 | if (data[IFLA_VXLAN_LOCAL]) { | |
ce44a4ae MS |
3183 | if (changelink && (conf->saddr.sa.sa_family != AF_INET)) |
3184 | return -EOPNOTSUPP; | |
3185 | ||
8bcdc4f3 RP |
3186 | conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]); |
3187 | conf->saddr.sa.sa_family = AF_INET; | |
0dfbdf41 TG |
3188 | } else if (data[IFLA_VXLAN_LOCAL6]) { |
3189 | if (!IS_ENABLED(CONFIG_IPV6)) | |
3190 | return -EPFNOSUPPORT; | |
3191 | ||
ce44a4ae MS |
3192 | if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) |
3193 | return -EOPNOTSUPP; | |
3194 | ||
0dfbdf41 | 3195 | /* TODO: respect scope id */ |
8bcdc4f3 RP |
3196 | conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]); |
3197 | conf->saddr.sa.sa_family = AF_INET6; | |
0dfbdf41 TG |
3198 | } |
3199 | ||
3200 | if (data[IFLA_VXLAN_LINK]) | |
8bcdc4f3 | 3201 | conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]); |
0dfbdf41 | 3202 | |
d342894c | 3203 | if (data[IFLA_VXLAN_TOS]) |
8bcdc4f3 | 3204 | conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); |
d342894c | 3205 | |
afb97186 | 3206 | if (data[IFLA_VXLAN_TTL]) |
8bcdc4f3 | 3207 | conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); |
afb97186 | 3208 | |
e7f70af1 | 3209 | if (data[IFLA_VXLAN_LABEL]) |
8bcdc4f3 | 3210 | conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) & |
e7f70af1 DB |
3211 | IPV6_FLOWLABEL_MASK; |
3212 | ||
8bcdc4f3 | 3213 | if (data[IFLA_VXLAN_LEARNING]) { |
dc5321d7 | 3214 | if (nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
8bcdc4f3 | 3215 | conf->flags |= VXLAN_F_LEARN; |
dc5321d7 | 3216 | else |
8bcdc4f3 | 3217 | conf->flags &= ~VXLAN_F_LEARN; |
8bcdc4f3 RP |
3218 | } else if (!changelink) { |
3219 | /* default to learn on a new device */ | |
3220 | conf->flags |= VXLAN_F_LEARN; | |
3221 | } | |
d342894c | 3222 | |
8bcdc4f3 RP |
3223 | if (data[IFLA_VXLAN_AGEING]) { |
3224 | if (changelink) | |
3225 | return -EOPNOTSUPP; | |
3226 | conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); | |
3227 | } | |
d342894c | 3228 | |
8bcdc4f3 RP |
3229 | if (data[IFLA_VXLAN_PROXY]) { |
3230 | if (changelink) | |
3231 | return -EOPNOTSUPP; | |
3232 | if (nla_get_u8(data[IFLA_VXLAN_PROXY])) | |
3233 | conf->flags |= VXLAN_F_PROXY; | |
3234 | } | |
e4f67add | 3235 | |
8bcdc4f3 RP |
3236 | if (data[IFLA_VXLAN_RSC]) { |
3237 | if (changelink) | |
3238 | return -EOPNOTSUPP; | |
3239 | if (nla_get_u8(data[IFLA_VXLAN_RSC])) | |
3240 | conf->flags |= VXLAN_F_RSC; | |
3241 | } | |
e4f67add | 3242 | |
8bcdc4f3 RP |
3243 | if (data[IFLA_VXLAN_L2MISS]) { |
3244 | if (changelink) | |
3245 | return -EOPNOTSUPP; | |
3246 | if (nla_get_u8(data[IFLA_VXLAN_L2MISS])) | |
3247 | conf->flags |= VXLAN_F_L2MISS; | |
3248 | } | |
e4f67add | 3249 | |
8bcdc4f3 RP |
3250 | if (data[IFLA_VXLAN_L3MISS]) { |
3251 | if (changelink) | |
3252 | return -EOPNOTSUPP; | |
3253 | if (nla_get_u8(data[IFLA_VXLAN_L3MISS])) | |
3254 | conf->flags |= VXLAN_F_L3MISS; | |
3255 | } | |
e4f67add | 3256 | |
8bcdc4f3 RP |
3257 | if (data[IFLA_VXLAN_LIMIT]) { |
3258 | if (changelink) | |
3259 | return -EOPNOTSUPP; | |
3260 | conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); | |
3261 | } | |
d342894c | 3262 | |
8bcdc4f3 RP |
3263 | if (data[IFLA_VXLAN_COLLECT_METADATA]) { |
3264 | if (changelink) | |
3265 | return -EOPNOTSUPP; | |
3266 | if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA])) | |
3267 | conf->flags |= VXLAN_F_COLLECT_METADATA; | |
3268 | } | |
f8a9b1bc | 3269 | |
05f47d69 | 3270 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
8bcdc4f3 RP |
3271 | if (!changelink) { |
3272 | const struct ifla_vxlan_port_range *p | |
3273 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
3274 | conf->port_min = ntohs(p->low); | |
3275 | conf->port_max = ntohs(p->high); | |
3276 | } else { | |
3277 | return -EOPNOTSUPP; | |
3278 | } | |
05f47d69 | 3279 | } |
3280 | ||
8bcdc4f3 RP |
3281 | if (data[IFLA_VXLAN_PORT]) { |
3282 | if (changelink) | |
3283 | return -EOPNOTSUPP; | |
3284 | conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); | |
3285 | } | |
823aa873 | 3286 | |
8bcdc4f3 RP |
3287 | if (data[IFLA_VXLAN_UDP_CSUM]) { |
3288 | if (changelink) | |
3289 | return -EOPNOTSUPP; | |
3290 | if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM])) | |
3291 | conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX; | |
3292 | } | |
359a0ea9 | 3293 | |
8bcdc4f3 RP |
3294 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) { |
3295 | if (changelink) | |
3296 | return -EOPNOTSUPP; | |
3297 | if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX])) | |
3298 | conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX; | |
3299 | } | |
359a0ea9 | 3300 | |
8bcdc4f3 RP |
3301 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) { |
3302 | if (changelink) | |
3303 | return -EOPNOTSUPP; | |
3304 | if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX])) | |
3305 | conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX; | |
3306 | } | |
359a0ea9 | 3307 | |
8bcdc4f3 RP |
3308 | if (data[IFLA_VXLAN_REMCSUM_TX]) { |
3309 | if (changelink) | |
3310 | return -EOPNOTSUPP; | |
3311 | if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX])) | |
3312 | conf->flags |= VXLAN_F_REMCSUM_TX; | |
3313 | } | |
dfd8645e | 3314 | |
8bcdc4f3 RP |
3315 | if (data[IFLA_VXLAN_REMCSUM_RX]) { |
3316 | if (changelink) | |
3317 | return -EOPNOTSUPP; | |
3318 | if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX])) | |
3319 | conf->flags |= VXLAN_F_REMCSUM_RX; | |
3320 | } | |
3321 | ||
3322 | if (data[IFLA_VXLAN_GBP]) { | |
3323 | if (changelink) | |
3324 | return -EOPNOTSUPP; | |
3325 | conf->flags |= VXLAN_F_GBP; | |
3326 | } | |
3327 | ||
3328 | if (data[IFLA_VXLAN_GPE]) { | |
3329 | if (changelink) | |
3330 | return -EOPNOTSUPP; | |
3331 | conf->flags |= VXLAN_F_GPE; | |
3332 | } | |
3333 | ||
3334 | if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) { | |
3335 | if (changelink) | |
3336 | return -EOPNOTSUPP; | |
3337 | conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL; | |
3338 | } | |
3339 | ||
3340 | if (tb[IFLA_MTU]) { | |
3341 | if (changelink) | |
3342 | return -EOPNOTSUPP; | |
3343 | conf->mtu = nla_get_u32(tb[IFLA_MTU]); | |
3344 | } | |
3345 | ||
3346 | return 0; | |
3347 | } | |
3348 | ||
3349 | static int vxlan_newlink(struct net *src_net, struct net_device *dev, | |
7a3f4a18 MS |
3350 | struct nlattr *tb[], struct nlattr *data[], |
3351 | struct netlink_ext_ack *extack) | |
8bcdc4f3 | 3352 | { |
8bcdc4f3 RP |
3353 | struct vxlan_config conf; |
3354 | int err; | |
3355 | ||
3356 | err = vxlan_nl2conf(tb, data, dev, &conf, false); | |
3357 | if (err) | |
3358 | return err; | |
3359 | ||
c80498e3 | 3360 | return __vxlan_dev_create(src_net, dev, &conf); |
8bcdc4f3 | 3361 | } |
dfd8645e | 3362 | |
8bcdc4f3 | 3363 | static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[], |
ad744b22 MS |
3364 | struct nlattr *data[], |
3365 | struct netlink_ext_ack *extack) | |
8bcdc4f3 RP |
3366 | { |
3367 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3368 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
3369 | struct vxlan_rdst old_dst; | |
3370 | struct vxlan_config conf; | |
3371 | int err; | |
3372 | ||
3373 | err = vxlan_nl2conf(tb, data, | |
3374 | dev, &conf, true); | |
3375 | if (err) | |
3376 | return err; | |
3511494c | 3377 | |
8bcdc4f3 | 3378 | memcpy(&old_dst, dst, sizeof(struct vxlan_rdst)); |
e1e5314d | 3379 | |
8bcdc4f3 RP |
3380 | err = vxlan_dev_configure(vxlan->net, dev, &conf, true); |
3381 | if (err) | |
3382 | return err; | |
0ace2ca8 | 3383 | |
8bcdc4f3 RP |
3384 | /* handle default dst entry */ |
3385 | if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) { | |
3386 | spin_lock_bh(&vxlan->hash_lock); | |
3387 | if (!vxlan_addr_any(&old_dst.remote_ip)) | |
3388 | __vxlan_fdb_delete(vxlan, all_zeros_mac, | |
3389 | old_dst.remote_ip, | |
3390 | vxlan->cfg.dst_port, | |
3391 | old_dst.remote_vni, | |
3392 | old_dst.remote_vni, | |
3393 | old_dst.remote_ifindex, 0); | |
3394 | ||
3395 | if (!vxlan_addr_any(&dst->remote_ip)) { | |
3396 | err = vxlan_fdb_create(vxlan, all_zeros_mac, | |
3397 | &dst->remote_ip, | |
3398 | NUD_REACHABLE | NUD_PERMANENT, | |
3399 | NLM_F_CREATE | NLM_F_APPEND, | |
3400 | vxlan->cfg.dst_port, | |
3401 | dst->remote_vni, | |
3402 | dst->remote_vni, | |
3403 | dst->remote_ifindex, | |
3404 | NTF_SELF); | |
3405 | if (err) { | |
3406 | spin_unlock_bh(&vxlan->hash_lock); | |
3407 | return err; | |
3408 | } | |
3409 | } | |
3410 | spin_unlock_bh(&vxlan->hash_lock); | |
3411 | } | |
ce577668 | 3412 | |
8bcdc4f3 | 3413 | return 0; |
d342894c | 3414 | } |
3415 | ||
3416 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) | |
3417 | { | |
3418 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3419 | ||
8b3f9337 RP |
3420 | vxlan_flush(vxlan, true); |
3421 | ||
58ce31cc | 3422 | gro_cells_destroy(&vxlan->gro_cells); |
553675fb | 3423 | list_del(&vxlan->next); |
d342894c | 3424 | unregister_netdevice_queue(dev, head); |
3425 | } | |
3426 | ||
3427 | static size_t vxlan_get_size(const struct net_device *dev) | |
3428 | { | |
3429 | ||
3430 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ | |
e4c7ed41 | 3431 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */ |
d342894c | 3432 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
e4c7ed41 | 3433 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */ |
d342894c | 3434 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
3435 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ | |
e7f70af1 | 3436 | nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */ |
d342894c | 3437 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ |
e4f67add DS |
3438 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
3439 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ | |
3440 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ | |
3441 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ | |
da8b43c0 | 3442 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */ |
d342894c | 3443 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
3444 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ | |
05f47d69 | 3445 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
359a0ea9 TH |
3446 | nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */ |
3447 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */ | |
3448 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */ | |
3449 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */ | |
dfd8645e TH |
3450 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */ |
3451 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */ | |
d342894c | 3452 | 0; |
3453 | } | |
3454 | ||
3455 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
3456 | { | |
3457 | const struct vxlan_dev *vxlan = netdev_priv(dev); | |
c7995c43 | 3458 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
05f47d69 | 3459 | struct ifla_vxlan_port_range ports = { |
0dfbdf41 TG |
3460 | .low = htons(vxlan->cfg.port_min), |
3461 | .high = htons(vxlan->cfg.port_max), | |
05f47d69 | 3462 | }; |
d342894c | 3463 | |
54bfd872 | 3464 | if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni))) |
d342894c | 3465 | goto nla_put_failure; |
3466 | ||
e4c7ed41 CW |
3467 | if (!vxlan_addr_any(&dst->remote_ip)) { |
3468 | if (dst->remote_ip.sa.sa_family == AF_INET) { | |
930345ea JB |
3469 | if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP, |
3470 | dst->remote_ip.sin.sin_addr.s_addr)) | |
e4c7ed41 CW |
3471 | goto nla_put_failure; |
3472 | #if IS_ENABLED(CONFIG_IPV6) | |
3473 | } else { | |
930345ea JB |
3474 | if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6, |
3475 | &dst->remote_ip.sin6.sin6_addr)) | |
e4c7ed41 CW |
3476 | goto nla_put_failure; |
3477 | #endif | |
3478 | } | |
3479 | } | |
d342894c | 3480 | |
c7995c43 | 3481 | if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) |
d342894c | 3482 | goto nla_put_failure; |
3483 | ||
0dfbdf41 TG |
3484 | if (!vxlan_addr_any(&vxlan->cfg.saddr)) { |
3485 | if (vxlan->cfg.saddr.sa.sa_family == AF_INET) { | |
930345ea | 3486 | if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL, |
0dfbdf41 | 3487 | vxlan->cfg.saddr.sin.sin_addr.s_addr)) |
e4c7ed41 CW |
3488 | goto nla_put_failure; |
3489 | #if IS_ENABLED(CONFIG_IPV6) | |
3490 | } else { | |
930345ea | 3491 | if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6, |
0dfbdf41 | 3492 | &vxlan->cfg.saddr.sin6.sin6_addr)) |
e4c7ed41 CW |
3493 | goto nla_put_failure; |
3494 | #endif | |
3495 | } | |
3496 | } | |
d342894c | 3497 | |
0dfbdf41 TG |
3498 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) || |
3499 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) || | |
e7f70af1 | 3500 | nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) || |
e4f67add | 3501 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
dc5321d7 | 3502 | !!(vxlan->cfg.flags & VXLAN_F_LEARN)) || |
e4f67add | 3503 | nla_put_u8(skb, IFLA_VXLAN_PROXY, |
dc5321d7 MS |
3504 | !!(vxlan->cfg.flags & VXLAN_F_PROXY)) || |
3505 | nla_put_u8(skb, IFLA_VXLAN_RSC, | |
3506 | !!(vxlan->cfg.flags & VXLAN_F_RSC)) || | |
e4f67add | 3507 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, |
dc5321d7 | 3508 | !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) || |
e4f67add | 3509 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, |
dc5321d7 | 3510 | !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) || |
da8b43c0 | 3511 | nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA, |
dc5321d7 | 3512 | !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) || |
0dfbdf41 TG |
3513 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) || |
3514 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) || | |
3515 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) || | |
359a0ea9 | 3516 | nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM, |
dc5321d7 | 3517 | !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) || |
359a0ea9 | 3518 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, |
dc5321d7 | 3519 | !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) || |
359a0ea9 | 3520 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, |
dc5321d7 | 3521 | !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) || |
dfd8645e | 3522 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX, |
dc5321d7 | 3523 | !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) || |
dfd8645e | 3524 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX, |
dc5321d7 | 3525 | !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX))) |
d342894c | 3526 | goto nla_put_failure; |
3527 | ||
05f47d69 | 3528 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
3529 | goto nla_put_failure; | |
3530 | ||
dc5321d7 | 3531 | if (vxlan->cfg.flags & VXLAN_F_GBP && |
3511494c TG |
3532 | nla_put_flag(skb, IFLA_VXLAN_GBP)) |
3533 | goto nla_put_failure; | |
3534 | ||
dc5321d7 | 3535 | if (vxlan->cfg.flags & VXLAN_F_GPE && |
e1e5314d JB |
3536 | nla_put_flag(skb, IFLA_VXLAN_GPE)) |
3537 | goto nla_put_failure; | |
3538 | ||
dc5321d7 | 3539 | if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL && |
0ace2ca8 TH |
3540 | nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL)) |
3541 | goto nla_put_failure; | |
3542 | ||
d342894c | 3543 | return 0; |
3544 | ||
3545 | nla_put_failure: | |
3546 | return -EMSGSIZE; | |
3547 | } | |
3548 | ||
1728d4fa ND |
3549 | static struct net *vxlan_get_link_net(const struct net_device *dev) |
3550 | { | |
3551 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3552 | ||
3553 | return vxlan->net; | |
3554 | } | |
3555 | ||
d342894c | 3556 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { |
3557 | .kind = "vxlan", | |
3558 | .maxtype = IFLA_VXLAN_MAX, | |
3559 | .policy = vxlan_policy, | |
3560 | .priv_size = sizeof(struct vxlan_dev), | |
3561 | .setup = vxlan_setup, | |
3562 | .validate = vxlan_validate, | |
3563 | .newlink = vxlan_newlink, | |
8bcdc4f3 | 3564 | .changelink = vxlan_changelink, |
d342894c | 3565 | .dellink = vxlan_dellink, |
3566 | .get_size = vxlan_get_size, | |
3567 | .fill_info = vxlan_fill_info, | |
1728d4fa | 3568 | .get_link_net = vxlan_get_link_net, |
d342894c | 3569 | }; |
3570 | ||
cf5da330 ND |
3571 | struct net_device *vxlan_dev_create(struct net *net, const char *name, |
3572 | u8 name_assign_type, | |
3573 | struct vxlan_config *conf) | |
3574 | { | |
3575 | struct nlattr *tb[IFLA_MAX + 1]; | |
3576 | struct net_device *dev; | |
3577 | int err; | |
3578 | ||
3579 | memset(&tb, 0, sizeof(tb)); | |
3580 | ||
3581 | dev = rtnl_create_link(net, name, name_assign_type, | |
3582 | &vxlan_link_ops, tb); | |
3583 | if (IS_ERR(dev)) | |
3584 | return dev; | |
3585 | ||
c80498e3 | 3586 | err = __vxlan_dev_create(net, dev, conf); |
cf5da330 ND |
3587 | if (err < 0) { |
3588 | free_netdev(dev); | |
3589 | return ERR_PTR(err); | |
3590 | } | |
3591 | ||
3592 | err = rtnl_configure_link(dev, NULL); | |
3593 | if (err < 0) { | |
3594 | LIST_HEAD(list_kill); | |
3595 | ||
3596 | vxlan_dellink(dev, &list_kill); | |
3597 | unregister_netdevice_many(&list_kill); | |
3598 | return ERR_PTR(err); | |
3599 | } | |
3600 | ||
3601 | return dev; | |
3602 | } | |
3603 | EXPORT_SYMBOL_GPL(vxlan_dev_create); | |
3604 | ||
acaf4e70 DB |
3605 | static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn, |
3606 | struct net_device *dev) | |
3607 | { | |
3608 | struct vxlan_dev *vxlan, *next; | |
3609 | LIST_HEAD(list_kill); | |
3610 | ||
3611 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { | |
3612 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
3613 | ||
3614 | /* In case we created vxlan device with carrier | |
3615 | * and we loose the carrier due to module unload | |
3616 | * we also need to remove vxlan device. In other | |
3617 | * cases, it's not necessary and remote_ifindex | |
3618 | * is 0 here, so no matches. | |
3619 | */ | |
3620 | if (dst->remote_ifindex == dev->ifindex) | |
3621 | vxlan_dellink(vxlan->dev, &list_kill); | |
3622 | } | |
3623 | ||
3624 | unregister_netdevice_many(&list_kill); | |
3625 | } | |
3626 | ||
b7aade15 HFS |
3627 | static int vxlan_netdevice_event(struct notifier_block *unused, |
3628 | unsigned long event, void *ptr) | |
acaf4e70 DB |
3629 | { |
3630 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
783c1463 | 3631 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
acaf4e70 | 3632 | |
783c1463 | 3633 | if (event == NETDEV_UNREGISTER) |
acaf4e70 | 3634 | vxlan_handle_lowerdev_unregister(vn, dev); |
7c46a640 | 3635 | else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO) |
b7aade15 | 3636 | vxlan_push_rx_ports(dev); |
acaf4e70 DB |
3637 | |
3638 | return NOTIFY_DONE; | |
3639 | } | |
3640 | ||
3641 | static struct notifier_block vxlan_notifier_block __read_mostly = { | |
b7aade15 | 3642 | .notifier_call = vxlan_netdevice_event, |
acaf4e70 DB |
3643 | }; |
3644 | ||
d342894c | 3645 | static __net_init int vxlan_init_net(struct net *net) |
3646 | { | |
3647 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
31fec5aa | 3648 | unsigned int h; |
d342894c | 3649 | |
553675fb | 3650 | INIT_LIST_HEAD(&vn->vxlan_list); |
1c51a915 | 3651 | spin_lock_init(&vn->sock_lock); |
d342894c | 3652 | |
553675fb | 3653 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
3654 | INIT_HLIST_HEAD(&vn->sock_list[h]); | |
d342894c | 3655 | |
3656 | return 0; | |
3657 | } | |
3658 | ||
f01ec1c0 ND |
3659 | static void __net_exit vxlan_exit_net(struct net *net) |
3660 | { | |
3661 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
3662 | struct vxlan_dev *vxlan, *next; | |
3663 | struct net_device *dev, *aux; | |
3664 | LIST_HEAD(list); | |
3665 | ||
3666 | rtnl_lock(); | |
3667 | for_each_netdev_safe(net, dev, aux) | |
3668 | if (dev->rtnl_link_ops == &vxlan_link_ops) | |
3669 | unregister_netdevice_queue(dev, &list); | |
3670 | ||
3671 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { | |
3672 | /* If vxlan->dev is in the same netns, it has already been added | |
3673 | * to the list by the previous loop. | |
3674 | */ | |
58ce31cc TH |
3675 | if (!net_eq(dev_net(vxlan->dev), net)) { |
3676 | gro_cells_destroy(&vxlan->gro_cells); | |
13c3ed6a | 3677 | unregister_netdevice_queue(vxlan->dev, &list); |
58ce31cc | 3678 | } |
f01ec1c0 ND |
3679 | } |
3680 | ||
3681 | unregister_netdevice_many(&list); | |
3682 | rtnl_unlock(); | |
3683 | } | |
3684 | ||
d342894c | 3685 | static struct pernet_operations vxlan_net_ops = { |
3686 | .init = vxlan_init_net, | |
f01ec1c0 | 3687 | .exit = vxlan_exit_net, |
d342894c | 3688 | .id = &vxlan_net_id, |
3689 | .size = sizeof(struct vxlan_net), | |
3690 | }; | |
3691 | ||
3692 | static int __init vxlan_init_module(void) | |
3693 | { | |
3694 | int rc; | |
3695 | ||
3696 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); | |
3697 | ||
783c1463 | 3698 | rc = register_pernet_subsys(&vxlan_net_ops); |
d342894c | 3699 | if (rc) |
3700 | goto out1; | |
3701 | ||
acaf4e70 | 3702 | rc = register_netdevice_notifier(&vxlan_notifier_block); |
d342894c | 3703 | if (rc) |
3704 | goto out2; | |
3705 | ||
acaf4e70 DB |
3706 | rc = rtnl_link_register(&vxlan_link_ops); |
3707 | if (rc) | |
3708 | goto out3; | |
d342894c | 3709 | |
acaf4e70 DB |
3710 | return 0; |
3711 | out3: | |
3712 | unregister_netdevice_notifier(&vxlan_notifier_block); | |
d342894c | 3713 | out2: |
783c1463 | 3714 | unregister_pernet_subsys(&vxlan_net_ops); |
d342894c | 3715 | out1: |
3716 | return rc; | |
3717 | } | |
7332a13b | 3718 | late_initcall(vxlan_init_module); |
d342894c | 3719 | |
3720 | static void __exit vxlan_cleanup_module(void) | |
3721 | { | |
b7153984 | 3722 | rtnl_link_unregister(&vxlan_link_ops); |
acaf4e70 | 3723 | unregister_netdevice_notifier(&vxlan_notifier_block); |
783c1463 DB |
3724 | unregister_pernet_subsys(&vxlan_net_ops); |
3725 | /* rcu_barrier() is called by netns */ | |
d342894c | 3726 | } |
3727 | module_exit(vxlan_cleanup_module); | |
3728 | ||
3729 | MODULE_LICENSE("GPL"); | |
3730 | MODULE_VERSION(VXLAN_VERSION); | |
3b8df3c6 | 3731 | MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); |
ead5139a | 3732 | MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic"); |
d342894c | 3733 | MODULE_ALIAS_RTNL_LINK("vxlan"); |