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