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