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