]>
Commit | Line | Data |
---|---|---|
d342894c | 1 | /* |
eb5ce439 | 2 | * VXLAN: Virtual eXtensible Local Area Network |
d342894c | 3 | * |
3b8df3c6 | 4 | * Copyright (c) 2012-2013 Vyatta Inc. |
d342894c | 5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
d342894c | 9 | */ |
10 | ||
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
12 | ||
13 | #include <linux/kernel.h> | |
14 | #include <linux/types.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/errno.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/skbuff.h> | |
19 | #include <linux/rculist.h> | |
20 | #include <linux/netdevice.h> | |
21 | #include <linux/in.h> | |
22 | #include <linux/ip.h> | |
23 | #include <linux/udp.h> | |
24 | #include <linux/igmp.h> | |
25 | #include <linux/etherdevice.h> | |
26 | #include <linux/if_ether.h> | |
1eaa8178 | 27 | #include <linux/if_vlan.h> |
d342894c | 28 | #include <linux/hash.h> |
1b13c97f | 29 | #include <linux/ethtool.h> |
e4f67add DS |
30 | #include <net/arp.h> |
31 | #include <net/ndisc.h> | |
d342894c | 32 | #include <net/ip.h> |
c5441932 | 33 | #include <net/ip_tunnels.h> |
d342894c | 34 | #include <net/icmp.h> |
35 | #include <net/udp.h> | |
3ee64f39 | 36 | #include <net/udp_tunnel.h> |
d342894c | 37 | #include <net/rtnetlink.h> |
38 | #include <net/route.h> | |
39 | #include <net/dsfield.h> | |
40 | #include <net/inet_ecn.h> | |
41 | #include <net/net_namespace.h> | |
42 | #include <net/netns/generic.h> | |
012a5729 | 43 | #include <net/vxlan.h> |
dc01e7d3 | 44 | #include <net/protocol.h> |
acbf74a7 | 45 | #include <net/udp_tunnel.h> |
e4c7ed41 CW |
46 | #if IS_ENABLED(CONFIG_IPV6) |
47 | #include <net/ipv6.h> | |
48 | #include <net/addrconf.h> | |
49 | #include <net/ip6_tunnel.h> | |
660d98ca | 50 | #include <net/ip6_checksum.h> |
e4c7ed41 | 51 | #endif |
d342894c | 52 | |
53 | #define VXLAN_VERSION "0.1" | |
54 | ||
553675fb | 55 | #define PORT_HASH_BITS 8 |
56 | #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) | |
d342894c | 57 | #define VNI_HASH_BITS 10 |
58 | #define VNI_HASH_SIZE (1<<VNI_HASH_BITS) | |
59 | #define FDB_HASH_BITS 8 | |
60 | #define FDB_HASH_SIZE (1<<FDB_HASH_BITS) | |
61 | #define FDB_AGE_DEFAULT 300 /* 5 min */ | |
62 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ | |
63 | ||
23c578bf | 64 | /* UDP port for VXLAN traffic. |
65 | * The IANA assigned port is 4789, but the Linux default is 8472 | |
234f5b73 | 66 | * for compatibility with early adopters. |
23c578bf | 67 | */ |
9daaa397 SH |
68 | static unsigned short vxlan_port __read_mostly = 8472; |
69 | module_param_named(udp_port, vxlan_port, ushort, 0444); | |
d342894c | 70 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); |
71 | ||
72 | static bool log_ecn_error = true; | |
73 | module_param(log_ecn_error, bool, 0644); | |
74 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
75 | ||
60d9d4c6 | 76 | static int vxlan_net_id; |
553675fb | 77 | |
afbd8bae MR |
78 | static const u8 all_zeros_mac[ETH_ALEN]; |
79 | ||
553675fb | 80 | /* per-network namespace private data for this module */ |
81 | struct vxlan_net { | |
82 | struct list_head vxlan_list; | |
83 | struct hlist_head sock_list[PORT_HASH_SIZE]; | |
1c51a915 | 84 | spinlock_t sock_lock; |
553675fb | 85 | }; |
86 | ||
e4c7ed41 CW |
87 | union vxlan_addr { |
88 | struct sockaddr_in sin; | |
89 | struct sockaddr_in6 sin6; | |
90 | struct sockaddr sa; | |
91 | }; | |
92 | ||
6681712d | 93 | struct vxlan_rdst { |
e4c7ed41 | 94 | union vxlan_addr remote_ip; |
6681712d DS |
95 | __be16 remote_port; |
96 | u32 remote_vni; | |
97 | u32 remote_ifindex; | |
3e61aa8f | 98 | struct list_head list; |
bc7892ba | 99 | struct rcu_head rcu; |
6681712d DS |
100 | }; |
101 | ||
d342894c | 102 | /* Forwarding table entry */ |
103 | struct vxlan_fdb { | |
104 | struct hlist_node hlist; /* linked list of entries */ | |
105 | struct rcu_head rcu; | |
106 | unsigned long updated; /* jiffies */ | |
107 | unsigned long used; | |
3e61aa8f | 108 | struct list_head remotes; |
d342894c | 109 | u16 state; /* see ndm_state */ |
ae884082 | 110 | u8 flags; /* see ndm_flags */ |
d342894c | 111 | u8 eth_addr[ETH_ALEN]; |
112 | }; | |
113 | ||
d342894c | 114 | /* Pseudo network device */ |
115 | struct vxlan_dev { | |
553675fb | 116 | struct hlist_node hlist; /* vni hash table */ |
117 | struct list_head next; /* vxlan's per namespace list */ | |
118 | struct vxlan_sock *vn_sock; /* listening socket */ | |
d342894c | 119 | struct net_device *dev; |
f01ec1c0 | 120 | struct net *net; /* netns for packet i/o */ |
c7995c43 | 121 | struct vxlan_rdst default_dst; /* default destination */ |
e4c7ed41 | 122 | union vxlan_addr saddr; /* source address */ |
823aa873 | 123 | __be16 dst_port; |
05f47d69 | 124 | __u16 port_min; /* source port range */ |
125 | __u16 port_max; | |
d342894c | 126 | __u8 tos; /* TOS override */ |
127 | __u8 ttl; | |
359a0ea9 | 128 | u32 flags; /* VXLAN_F_* in vxlan.h */ |
d342894c | 129 | |
1c51a915 | 130 | struct work_struct sock_work; |
3fc2de2f | 131 | struct work_struct igmp_join; |
132 | struct work_struct igmp_leave; | |
1c51a915 | 133 | |
d342894c | 134 | unsigned long age_interval; |
135 | struct timer_list age_timer; | |
136 | spinlock_t hash_lock; | |
137 | unsigned int addrcnt; | |
138 | unsigned int addrmax; | |
d342894c | 139 | |
140 | struct hlist_head fdb_head[FDB_HASH_SIZE]; | |
141 | }; | |
142 | ||
143 | /* salt for hash table */ | |
144 | static u32 vxlan_salt __read_mostly; | |
758c57d1 | 145 | static struct workqueue_struct *vxlan_wq; |
d342894c | 146 | |
1c51a915 SH |
147 | static void vxlan_sock_work(struct work_struct *work); |
148 | ||
e4c7ed41 CW |
149 | #if IS_ENABLED(CONFIG_IPV6) |
150 | static inline | |
151 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) | |
152 | { | |
153 | if (a->sa.sa_family != b->sa.sa_family) | |
154 | return false; | |
155 | if (a->sa.sa_family == AF_INET6) | |
156 | return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr); | |
157 | else | |
158 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; | |
159 | } | |
160 | ||
161 | static inline bool vxlan_addr_any(const union vxlan_addr *ipa) | |
162 | { | |
163 | if (ipa->sa.sa_family == AF_INET6) | |
164 | return ipv6_addr_any(&ipa->sin6.sin6_addr); | |
165 | else | |
166 | return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); | |
167 | } | |
168 | ||
169 | static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) | |
170 | { | |
171 | if (ipa->sa.sa_family == AF_INET6) | |
172 | return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr); | |
173 | else | |
174 | return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); | |
175 | } | |
176 | ||
177 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) | |
178 | { | |
179 | if (nla_len(nla) >= sizeof(struct in6_addr)) { | |
180 | nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr)); | |
181 | ip->sa.sa_family = AF_INET6; | |
182 | return 0; | |
183 | } else if (nla_len(nla) >= sizeof(__be32)) { | |
184 | ip->sin.sin_addr.s_addr = nla_get_be32(nla); | |
185 | ip->sa.sa_family = AF_INET; | |
186 | return 0; | |
187 | } else { | |
188 | return -EAFNOSUPPORT; | |
189 | } | |
190 | } | |
191 | ||
192 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, | |
193 | const union vxlan_addr *ip) | |
194 | { | |
195 | if (ip->sa.sa_family == AF_INET6) | |
196 | return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr); | |
197 | else | |
198 | return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr); | |
199 | } | |
200 | ||
201 | #else /* !CONFIG_IPV6 */ | |
202 | ||
203 | static inline | |
204 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) | |
205 | { | |
206 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; | |
207 | } | |
208 | ||
209 | static inline bool vxlan_addr_any(const union vxlan_addr *ipa) | |
210 | { | |
211 | return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY); | |
212 | } | |
213 | ||
214 | static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa) | |
215 | { | |
216 | return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr)); | |
217 | } | |
218 | ||
219 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) | |
220 | { | |
221 | if (nla_len(nla) >= sizeof(struct in6_addr)) { | |
222 | return -EAFNOSUPPORT; | |
223 | } else if (nla_len(nla) >= sizeof(__be32)) { | |
224 | ip->sin.sin_addr.s_addr = nla_get_be32(nla); | |
225 | ip->sa.sa_family = AF_INET; | |
226 | return 0; | |
227 | } else { | |
228 | return -EAFNOSUPPORT; | |
229 | } | |
230 | } | |
231 | ||
232 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, | |
233 | const union vxlan_addr *ip) | |
234 | { | |
235 | return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr); | |
236 | } | |
237 | #endif | |
238 | ||
553675fb | 239 | /* Virtual Network hash table head */ |
240 | static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id) | |
241 | { | |
242 | return &vs->vni_list[hash_32(id, VNI_HASH_BITS)]; | |
243 | } | |
244 | ||
245 | /* Socket hash table head */ | |
246 | static inline struct hlist_head *vs_head(struct net *net, __be16 port) | |
d342894c | 247 | { |
248 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
249 | ||
553675fb | 250 | return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; |
251 | } | |
252 | ||
3e61aa8f SH |
253 | /* First remote destination for a forwarding entry. |
254 | * Guaranteed to be non-NULL because remotes are never deleted. | |
255 | */ | |
5ca5461c | 256 | static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb) |
3e61aa8f | 257 | { |
5ca5461c | 258 | return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list); |
259 | } | |
260 | ||
261 | static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb) | |
262 | { | |
263 | return list_first_entry(&fdb->remotes, struct vxlan_rdst, list); | |
3e61aa8f SH |
264 | } |
265 | ||
19ca9fc1 ML |
266 | /* Find VXLAN socket based on network namespace, address family and UDP port */ |
267 | static struct vxlan_sock *vxlan_find_sock(struct net *net, | |
268 | sa_family_t family, __be16 port) | |
553675fb | 269 | { |
270 | struct vxlan_sock *vs; | |
271 | ||
272 | hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { | |
19ca9fc1 ML |
273 | if (inet_sk(vs->sock->sk)->inet_sport == port && |
274 | inet_sk(vs->sock->sk)->sk.sk_family == family) | |
553675fb | 275 | return vs; |
276 | } | |
277 | return NULL; | |
d342894c | 278 | } |
279 | ||
5cfccc5a | 280 | static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id) |
d342894c | 281 | { |
282 | struct vxlan_dev *vxlan; | |
d342894c | 283 | |
553675fb | 284 | hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) { |
c7995c43 | 285 | if (vxlan->default_dst.remote_vni == id) |
d342894c | 286 | return vxlan; |
287 | } | |
288 | ||
289 | return NULL; | |
290 | } | |
291 | ||
5cfccc5a | 292 | /* Look up VNI in a per net namespace table */ |
19ca9fc1 ML |
293 | static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, |
294 | sa_family_t family, __be16 port) | |
5cfccc5a PS |
295 | { |
296 | struct vxlan_sock *vs; | |
297 | ||
19ca9fc1 | 298 | vs = vxlan_find_sock(net, family, port); |
5cfccc5a PS |
299 | if (!vs) |
300 | return NULL; | |
301 | ||
302 | return vxlan_vs_find_vni(vs, id); | |
303 | } | |
304 | ||
d342894c | 305 | /* Fill in neighbour message in skbuff. */ |
306 | static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, | |
234f5b73 SH |
307 | const struct vxlan_fdb *fdb, |
308 | u32 portid, u32 seq, int type, unsigned int flags, | |
309 | const struct vxlan_rdst *rdst) | |
d342894c | 310 | { |
311 | unsigned long now = jiffies; | |
312 | struct nda_cacheinfo ci; | |
313 | struct nlmsghdr *nlh; | |
314 | struct ndmsg *ndm; | |
e4f67add | 315 | bool send_ip, send_eth; |
d342894c | 316 | |
317 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); | |
318 | if (nlh == NULL) | |
319 | return -EMSGSIZE; | |
320 | ||
321 | ndm = nlmsg_data(nlh); | |
322 | memset(ndm, 0, sizeof(*ndm)); | |
e4f67add DS |
323 | |
324 | send_eth = send_ip = true; | |
325 | ||
326 | if (type == RTM_GETNEIGH) { | |
327 | ndm->ndm_family = AF_INET; | |
e4c7ed41 | 328 | send_ip = !vxlan_addr_any(&rdst->remote_ip); |
e4f67add DS |
329 | send_eth = !is_zero_ether_addr(fdb->eth_addr); |
330 | } else | |
331 | ndm->ndm_family = AF_BRIDGE; | |
d342894c | 332 | ndm->ndm_state = fdb->state; |
333 | ndm->ndm_ifindex = vxlan->dev->ifindex; | |
ae884082 | 334 | ndm->ndm_flags = fdb->flags; |
545469f7 | 335 | ndm->ndm_type = RTN_UNICAST; |
d342894c | 336 | |
e4f67add | 337 | if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) |
d342894c | 338 | goto nla_put_failure; |
339 | ||
e4c7ed41 | 340 | if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip)) |
6681712d DS |
341 | goto nla_put_failure; |
342 | ||
823aa873 | 343 | if (rdst->remote_port && rdst->remote_port != vxlan->dst_port && |
6681712d DS |
344 | nla_put_be16(skb, NDA_PORT, rdst->remote_port)) |
345 | goto nla_put_failure; | |
c7995c43 | 346 | if (rdst->remote_vni != vxlan->default_dst.remote_vni && |
60d9d4c6 | 347 | nla_put_u32(skb, NDA_VNI, rdst->remote_vni)) |
6681712d DS |
348 | goto nla_put_failure; |
349 | if (rdst->remote_ifindex && | |
350 | nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) | |
d342894c | 351 | goto nla_put_failure; |
352 | ||
353 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); | |
354 | ci.ndm_confirmed = 0; | |
355 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); | |
356 | ci.ndm_refcnt = 0; | |
357 | ||
358 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) | |
359 | goto nla_put_failure; | |
360 | ||
361 | return nlmsg_end(skb, nlh); | |
362 | ||
363 | nla_put_failure: | |
364 | nlmsg_cancel(skb, nlh); | |
365 | return -EMSGSIZE; | |
366 | } | |
367 | ||
368 | static inline size_t vxlan_nlmsg_size(void) | |
369 | { | |
370 | return NLMSG_ALIGN(sizeof(struct ndmsg)) | |
371 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ | |
e4c7ed41 | 372 | + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */ |
73cf3317 | 373 | + nla_total_size(sizeof(__be16)) /* NDA_PORT */ |
6681712d DS |
374 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ |
375 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ | |
d342894c | 376 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
377 | } | |
378 | ||
9e4b93f9 ND |
379 | static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, |
380 | struct vxlan_rdst *rd, int type) | |
d342894c | 381 | { |
382 | struct net *net = dev_net(vxlan->dev); | |
383 | struct sk_buff *skb; | |
384 | int err = -ENOBUFS; | |
385 | ||
386 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); | |
387 | if (skb == NULL) | |
388 | goto errout; | |
389 | ||
9e4b93f9 | 390 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd); |
d342894c | 391 | if (err < 0) { |
392 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ | |
393 | WARN_ON(err == -EMSGSIZE); | |
394 | kfree_skb(skb); | |
395 | goto errout; | |
396 | } | |
397 | ||
398 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); | |
399 | return; | |
400 | errout: | |
401 | if (err < 0) | |
402 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); | |
403 | } | |
404 | ||
e4c7ed41 | 405 | static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa) |
e4f67add DS |
406 | { |
407 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
bb3fd687 SH |
408 | struct vxlan_fdb f = { |
409 | .state = NUD_STALE, | |
410 | }; | |
411 | struct vxlan_rdst remote = { | |
e4c7ed41 | 412 | .remote_ip = *ipa, /* goes to NDA_DST */ |
bb3fd687 SH |
413 | .remote_vni = VXLAN_N_VID, |
414 | }; | |
3e61aa8f | 415 | |
9e4b93f9 | 416 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH); |
e4f67add DS |
417 | } |
418 | ||
419 | static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) | |
420 | { | |
bb3fd687 SH |
421 | struct vxlan_fdb f = { |
422 | .state = NUD_STALE, | |
423 | }; | |
9e4b93f9 | 424 | struct vxlan_rdst remote = { }; |
e4f67add | 425 | |
e4f67add DS |
426 | memcpy(f.eth_addr, eth_addr, ETH_ALEN); |
427 | ||
9e4b93f9 | 428 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH); |
e4f67add DS |
429 | } |
430 | ||
d342894c | 431 | /* Hash Ethernet address */ |
432 | static u32 eth_hash(const unsigned char *addr) | |
433 | { | |
434 | u64 value = get_unaligned((u64 *)addr); | |
435 | ||
436 | /* only want 6 bytes */ | |
437 | #ifdef __BIG_ENDIAN | |
d342894c | 438 | value >>= 16; |
321fb991 | 439 | #else |
440 | value <<= 16; | |
d342894c | 441 | #endif |
442 | return hash_64(value, FDB_HASH_BITS); | |
443 | } | |
444 | ||
445 | /* Hash chain to use given mac address */ | |
446 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, | |
447 | const u8 *mac) | |
448 | { | |
449 | return &vxlan->fdb_head[eth_hash(mac)]; | |
450 | } | |
451 | ||
452 | /* Look up Ethernet address in forwarding table */ | |
014be2c8 | 453 | static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan, |
d342894c | 454 | const u8 *mac) |
d342894c | 455 | { |
456 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac); | |
457 | struct vxlan_fdb *f; | |
d342894c | 458 | |
b67bfe0d | 459 | hlist_for_each_entry_rcu(f, head, hlist) { |
7367d0b5 | 460 | if (ether_addr_equal(mac, f->eth_addr)) |
d342894c | 461 | return f; |
462 | } | |
463 | ||
464 | return NULL; | |
465 | } | |
466 | ||
014be2c8 SS |
467 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, |
468 | const u8 *mac) | |
469 | { | |
470 | struct vxlan_fdb *f; | |
471 | ||
472 | f = __vxlan_find_mac(vxlan, mac); | |
473 | if (f) | |
474 | f->used = jiffies; | |
475 | ||
476 | return f; | |
477 | } | |
478 | ||
a5e7c10a MR |
479 | /* caller should hold vxlan->hash_lock */ |
480 | static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, | |
e4c7ed41 | 481 | union vxlan_addr *ip, __be16 port, |
a5e7c10a | 482 | __u32 vni, __u32 ifindex) |
6681712d | 483 | { |
3e61aa8f | 484 | struct vxlan_rdst *rd; |
6681712d | 485 | |
3e61aa8f | 486 | list_for_each_entry(rd, &f->remotes, list) { |
e4c7ed41 | 487 | if (vxlan_addr_equal(&rd->remote_ip, ip) && |
6681712d DS |
488 | rd->remote_port == port && |
489 | rd->remote_vni == vni && | |
490 | rd->remote_ifindex == ifindex) | |
a5e7c10a | 491 | return rd; |
6681712d | 492 | } |
3e61aa8f | 493 | |
a5e7c10a MR |
494 | return NULL; |
495 | } | |
496 | ||
906dc186 TR |
497 | /* Replace destination of unicast mac */ |
498 | static int vxlan_fdb_replace(struct vxlan_fdb *f, | |
e4c7ed41 | 499 | union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex) |
906dc186 TR |
500 | { |
501 | struct vxlan_rdst *rd; | |
502 | ||
503 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); | |
504 | if (rd) | |
505 | return 0; | |
506 | ||
507 | rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list); | |
508 | if (!rd) | |
509 | return 0; | |
e4c7ed41 | 510 | rd->remote_ip = *ip; |
906dc186 TR |
511 | rd->remote_port = port; |
512 | rd->remote_vni = vni; | |
513 | rd->remote_ifindex = ifindex; | |
514 | return 1; | |
515 | } | |
516 | ||
a5e7c10a MR |
517 | /* Add/update destinations for multicast */ |
518 | static int vxlan_fdb_append(struct vxlan_fdb *f, | |
9e4b93f9 ND |
519 | union vxlan_addr *ip, __be16 port, __u32 vni, |
520 | __u32 ifindex, struct vxlan_rdst **rdp) | |
a5e7c10a MR |
521 | { |
522 | struct vxlan_rdst *rd; | |
523 | ||
524 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); | |
525 | if (rd) | |
526 | return 0; | |
527 | ||
6681712d DS |
528 | rd = kmalloc(sizeof(*rd), GFP_ATOMIC); |
529 | if (rd == NULL) | |
530 | return -ENOBUFS; | |
e4c7ed41 | 531 | rd->remote_ip = *ip; |
6681712d DS |
532 | rd->remote_port = port; |
533 | rd->remote_vni = vni; | |
534 | rd->remote_ifindex = ifindex; | |
3e61aa8f SH |
535 | |
536 | list_add_tail_rcu(&rd->list, &f->remotes); | |
537 | ||
9e4b93f9 | 538 | *rdp = rd; |
6681712d DS |
539 | return 1; |
540 | } | |
541 | ||
a2b12f3c TH |
542 | static struct sk_buff **vxlan_gro_receive(struct sk_buff **head, |
543 | struct sk_buff *skb, | |
544 | struct udp_offload *uoff) | |
dc01e7d3 OG |
545 | { |
546 | struct sk_buff *p, **pp = NULL; | |
547 | struct vxlanhdr *vh, *vh2; | |
9b174d88 | 548 | unsigned int hlen, off_vx; |
dc01e7d3 OG |
549 | int flush = 1; |
550 | ||
551 | off_vx = skb_gro_offset(skb); | |
552 | hlen = off_vx + sizeof(*vh); | |
553 | vh = skb_gro_header_fast(skb, off_vx); | |
554 | if (skb_gro_header_hard(skb, hlen)) { | |
555 | vh = skb_gro_header_slow(skb, hlen, off_vx); | |
556 | if (unlikely(!vh)) | |
557 | goto out; | |
558 | } | |
dc01e7d3 OG |
559 | |
560 | flush = 0; | |
561 | ||
562 | for (p = *head; p; p = p->next) { | |
563 | if (!NAPI_GRO_CB(p)->same_flow) | |
564 | continue; | |
565 | ||
566 | vh2 = (struct vxlanhdr *)(p->data + off_vx); | |
9b174d88 | 567 | if (vh->vx_vni != vh2->vx_vni) { |
dc01e7d3 OG |
568 | NAPI_GRO_CB(p)->same_flow = 0; |
569 | continue; | |
570 | } | |
dc01e7d3 OG |
571 | } |
572 | ||
9b174d88 JG |
573 | skb_gro_pull(skb, sizeof(struct vxlanhdr)); |
574 | skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr)); | |
575 | pp = eth_gro_receive(head, skb); | |
dc01e7d3 | 576 | |
dc01e7d3 OG |
577 | out: |
578 | NAPI_GRO_CB(skb)->flush |= flush; | |
579 | ||
580 | return pp; | |
581 | } | |
582 | ||
a2b12f3c TH |
583 | static int vxlan_gro_complete(struct sk_buff *skb, int nhoff, |
584 | struct udp_offload *uoff) | |
dc01e7d3 | 585 | { |
cfdf1e1b JG |
586 | udp_tunnel_gro_complete(skb, nhoff); |
587 | ||
9b174d88 | 588 | return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr)); |
dc01e7d3 OG |
589 | } |
590 | ||
53cf5275 | 591 | /* Notify netdevs that UDP port started listening */ |
dc01e7d3 | 592 | static void vxlan_notify_add_rx_port(struct vxlan_sock *vs) |
53cf5275 JG |
593 | { |
594 | struct net_device *dev; | |
dc01e7d3 | 595 | struct sock *sk = vs->sock->sk; |
53cf5275 JG |
596 | struct net *net = sock_net(sk); |
597 | sa_family_t sa_family = sk->sk_family; | |
35e42379 | 598 | __be16 port = inet_sk(sk)->inet_sport; |
dc01e7d3 OG |
599 | int err; |
600 | ||
601 | if (sa_family == AF_INET) { | |
602 | err = udp_add_offload(&vs->udp_offloads); | |
603 | if (err) | |
604 | pr_warn("vxlan: udp_add_offload failed with status %d\n", err); | |
605 | } | |
53cf5275 JG |
606 | |
607 | rcu_read_lock(); | |
608 | for_each_netdev_rcu(net, dev) { | |
609 | if (dev->netdev_ops->ndo_add_vxlan_port) | |
610 | dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family, | |
611 | port); | |
612 | } | |
613 | rcu_read_unlock(); | |
614 | } | |
615 | ||
616 | /* Notify netdevs that UDP port is no more listening */ | |
dc01e7d3 | 617 | static void vxlan_notify_del_rx_port(struct vxlan_sock *vs) |
53cf5275 JG |
618 | { |
619 | struct net_device *dev; | |
dc01e7d3 | 620 | struct sock *sk = vs->sock->sk; |
53cf5275 JG |
621 | struct net *net = sock_net(sk); |
622 | sa_family_t sa_family = sk->sk_family; | |
35e42379 | 623 | __be16 port = inet_sk(sk)->inet_sport; |
53cf5275 JG |
624 | |
625 | rcu_read_lock(); | |
626 | for_each_netdev_rcu(net, dev) { | |
627 | if (dev->netdev_ops->ndo_del_vxlan_port) | |
628 | dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family, | |
629 | port); | |
630 | } | |
631 | rcu_read_unlock(); | |
dc01e7d3 OG |
632 | |
633 | if (sa_family == AF_INET) | |
634 | udp_del_offload(&vs->udp_offloads); | |
53cf5275 JG |
635 | } |
636 | ||
d342894c | 637 | /* Add new entry to forwarding table -- assumes lock held */ |
638 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, | |
e4c7ed41 | 639 | const u8 *mac, union vxlan_addr *ip, |
6681712d | 640 | __u16 state, __u16 flags, |
73cf3317 | 641 | __be16 port, __u32 vni, __u32 ifindex, |
ae884082 | 642 | __u8 ndm_flags) |
d342894c | 643 | { |
9e4b93f9 | 644 | struct vxlan_rdst *rd = NULL; |
d342894c | 645 | struct vxlan_fdb *f; |
646 | int notify = 0; | |
647 | ||
014be2c8 | 648 | f = __vxlan_find_mac(vxlan, mac); |
d342894c | 649 | if (f) { |
650 | if (flags & NLM_F_EXCL) { | |
651 | netdev_dbg(vxlan->dev, | |
652 | "lost race to create %pM\n", mac); | |
653 | return -EEXIST; | |
654 | } | |
655 | if (f->state != state) { | |
656 | f->state = state; | |
657 | f->updated = jiffies; | |
658 | notify = 1; | |
659 | } | |
ae884082 DS |
660 | if (f->flags != ndm_flags) { |
661 | f->flags = ndm_flags; | |
662 | f->updated = jiffies; | |
663 | notify = 1; | |
664 | } | |
906dc186 TR |
665 | if ((flags & NLM_F_REPLACE)) { |
666 | /* Only change unicasts */ | |
667 | if (!(is_multicast_ether_addr(f->eth_addr) || | |
668 | is_zero_ether_addr(f->eth_addr))) { | |
669 | int rc = vxlan_fdb_replace(f, ip, port, vni, | |
670 | ifindex); | |
671 | ||
672 | if (rc < 0) | |
673 | return rc; | |
674 | notify |= rc; | |
675 | } else | |
676 | return -EOPNOTSUPP; | |
677 | } | |
6681712d | 678 | if ((flags & NLM_F_APPEND) && |
58e4c767 MR |
679 | (is_multicast_ether_addr(f->eth_addr) || |
680 | is_zero_ether_addr(f->eth_addr))) { | |
9e4b93f9 ND |
681 | int rc = vxlan_fdb_append(f, ip, port, vni, ifindex, |
682 | &rd); | |
6681712d DS |
683 | |
684 | if (rc < 0) | |
685 | return rc; | |
686 | notify |= rc; | |
687 | } | |
d342894c | 688 | } else { |
689 | if (!(flags & NLM_F_CREATE)) | |
690 | return -ENOENT; | |
691 | ||
692 | if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax) | |
693 | return -ENOSPC; | |
694 | ||
906dc186 TR |
695 | /* Disallow replace to add a multicast entry */ |
696 | if ((flags & NLM_F_REPLACE) && | |
697 | (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) | |
698 | return -EOPNOTSUPP; | |
699 | ||
e4c7ed41 | 700 | netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); |
d342894c | 701 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
702 | if (!f) | |
703 | return -ENOMEM; | |
704 | ||
705 | notify = 1; | |
d342894c | 706 | f->state = state; |
ae884082 | 707 | f->flags = ndm_flags; |
d342894c | 708 | f->updated = f->used = jiffies; |
3e61aa8f | 709 | INIT_LIST_HEAD(&f->remotes); |
d342894c | 710 | memcpy(f->eth_addr, mac, ETH_ALEN); |
711 | ||
9e4b93f9 | 712 | vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); |
3e61aa8f | 713 | |
d342894c | 714 | ++vxlan->addrcnt; |
715 | hlist_add_head_rcu(&f->hlist, | |
716 | vxlan_fdb_head(vxlan, mac)); | |
717 | } | |
718 | ||
9e4b93f9 ND |
719 | if (notify) { |
720 | if (rd == NULL) | |
721 | rd = first_remote_rtnl(f); | |
722 | vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH); | |
723 | } | |
d342894c | 724 | |
725 | return 0; | |
726 | } | |
727 | ||
6706c82e | 728 | static void vxlan_fdb_free(struct rcu_head *head) |
6681712d DS |
729 | { |
730 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); | |
3e61aa8f | 731 | struct vxlan_rdst *rd, *nd; |
6681712d | 732 | |
3e61aa8f | 733 | list_for_each_entry_safe(rd, nd, &f->remotes, list) |
6681712d | 734 | kfree(rd); |
6681712d DS |
735 | kfree(f); |
736 | } | |
737 | ||
d342894c | 738 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
739 | { | |
740 | netdev_dbg(vxlan->dev, | |
741 | "delete %pM\n", f->eth_addr); | |
742 | ||
743 | --vxlan->addrcnt; | |
9e4b93f9 | 744 | vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH); |
d342894c | 745 | |
746 | hlist_del_rcu(&f->hlist); | |
6681712d | 747 | call_rcu(&f->rcu, vxlan_fdb_free); |
d342894c | 748 | } |
749 | ||
f0b074be | 750 | static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, |
e4c7ed41 | 751 | union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex) |
d342894c | 752 | { |
6681712d | 753 | struct net *net = dev_net(vxlan->dev); |
e4c7ed41 | 754 | int err; |
d342894c | 755 | |
f0b074be | 756 | if (tb[NDA_DST]) { |
e4c7ed41 CW |
757 | err = vxlan_nla_get_addr(ip, tb[NDA_DST]); |
758 | if (err) | |
759 | return err; | |
f0b074be | 760 | } else { |
e4c7ed41 CW |
761 | union vxlan_addr *remote = &vxlan->default_dst.remote_ip; |
762 | if (remote->sa.sa_family == AF_INET) { | |
763 | ip->sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
764 | ip->sa.sa_family = AF_INET; | |
765 | #if IS_ENABLED(CONFIG_IPV6) | |
766 | } else { | |
767 | ip->sin6.sin6_addr = in6addr_any; | |
768 | ip->sa.sa_family = AF_INET6; | |
769 | #endif | |
770 | } | |
f0b074be | 771 | } |
d342894c | 772 | |
6681712d | 773 | if (tb[NDA_PORT]) { |
73cf3317 | 774 | if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) |
6681712d | 775 | return -EINVAL; |
f0b074be MR |
776 | *port = nla_get_be16(tb[NDA_PORT]); |
777 | } else { | |
778 | *port = vxlan->dst_port; | |
779 | } | |
6681712d DS |
780 | |
781 | if (tb[NDA_VNI]) { | |
782 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) | |
783 | return -EINVAL; | |
f0b074be MR |
784 | *vni = nla_get_u32(tb[NDA_VNI]); |
785 | } else { | |
786 | *vni = vxlan->default_dst.remote_vni; | |
787 | } | |
6681712d DS |
788 | |
789 | if (tb[NDA_IFINDEX]) { | |
5abb0029 | 790 | struct net_device *tdev; |
6681712d DS |
791 | |
792 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) | |
793 | return -EINVAL; | |
f0b074be | 794 | *ifindex = nla_get_u32(tb[NDA_IFINDEX]); |
73763949 | 795 | tdev = __dev_get_by_index(net, *ifindex); |
5abb0029 | 796 | if (!tdev) |
6681712d | 797 | return -EADDRNOTAVAIL; |
f0b074be MR |
798 | } else { |
799 | *ifindex = 0; | |
800 | } | |
801 | ||
802 | return 0; | |
803 | } | |
804 | ||
805 | /* Add static entry (via netlink) */ | |
806 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], | |
807 | struct net_device *dev, | |
f6f6424b | 808 | const unsigned char *addr, u16 vid, u16 flags) |
f0b074be MR |
809 | { |
810 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
811 | /* struct net *net = dev_net(vxlan->dev); */ | |
e4c7ed41 | 812 | union vxlan_addr ip; |
f0b074be MR |
813 | __be16 port; |
814 | u32 vni, ifindex; | |
815 | int err; | |
816 | ||
817 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { | |
818 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", | |
819 | ndm->ndm_state); | |
820 | return -EINVAL; | |
821 | } | |
822 | ||
823 | if (tb[NDA_DST] == NULL) | |
824 | return -EINVAL; | |
825 | ||
826 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); | |
827 | if (err) | |
828 | return err; | |
6681712d | 829 | |
5933a7bb MR |
830 | if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family) |
831 | return -EAFNOSUPPORT; | |
832 | ||
d342894c | 833 | spin_lock_bh(&vxlan->hash_lock); |
e4c7ed41 | 834 | err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags, |
73cf3317 | 835 | port, vni, ifindex, ndm->ndm_flags); |
d342894c | 836 | spin_unlock_bh(&vxlan->hash_lock); |
837 | ||
838 | return err; | |
839 | } | |
840 | ||
841 | /* Delete entry (via netlink) */ | |
1690be63 VY |
842 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
843 | struct net_device *dev, | |
f6f6424b | 844 | const unsigned char *addr, u16 vid) |
d342894c | 845 | { |
846 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
847 | struct vxlan_fdb *f; | |
bc7892ba | 848 | struct vxlan_rdst *rd = NULL; |
e4c7ed41 | 849 | union vxlan_addr ip; |
bc7892ba MR |
850 | __be16 port; |
851 | u32 vni, ifindex; | |
852 | int err; | |
853 | ||
854 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); | |
855 | if (err) | |
856 | return err; | |
857 | ||
858 | err = -ENOENT; | |
d342894c | 859 | |
860 | spin_lock_bh(&vxlan->hash_lock); | |
861 | f = vxlan_find_mac(vxlan, addr); | |
bc7892ba MR |
862 | if (!f) |
863 | goto out; | |
864 | ||
e4c7ed41 CW |
865 | if (!vxlan_addr_any(&ip)) { |
866 | rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex); | |
bc7892ba MR |
867 | if (!rd) |
868 | goto out; | |
869 | } | |
870 | ||
871 | err = 0; | |
872 | ||
873 | /* remove a destination if it's not the only one on the list, | |
874 | * otherwise destroy the fdb entry | |
875 | */ | |
876 | if (rd && !list_is_singular(&f->remotes)) { | |
877 | list_del_rcu(&rd->list); | |
9e4b93f9 | 878 | vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH); |
dcdc7a59 | 879 | kfree_rcu(rd, rcu); |
bc7892ba | 880 | goto out; |
d342894c | 881 | } |
bc7892ba MR |
882 | |
883 | vxlan_fdb_destroy(vxlan, f); | |
884 | ||
885 | out: | |
d342894c | 886 | spin_unlock_bh(&vxlan->hash_lock); |
887 | ||
888 | return err; | |
889 | } | |
890 | ||
891 | /* Dump forwarding table */ | |
892 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, | |
5d5eacb3 JHS |
893 | struct net_device *dev, |
894 | struct net_device *filter_dev, int idx) | |
d342894c | 895 | { |
896 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
897 | unsigned int h; | |
898 | ||
899 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
900 | struct vxlan_fdb *f; | |
d342894c | 901 | int err; |
902 | ||
b67bfe0d | 903 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
6681712d | 904 | struct vxlan_rdst *rd; |
6681712d | 905 | |
3e61aa8f SH |
906 | if (idx < cb->args[0]) |
907 | goto skip; | |
908 | ||
909 | list_for_each_entry_rcu(rd, &f->remotes, list) { | |
6681712d DS |
910 | err = vxlan_fdb_info(skb, vxlan, f, |
911 | NETLINK_CB(cb->skb).portid, | |
912 | cb->nlh->nlmsg_seq, | |
913 | RTM_NEWNEIGH, | |
914 | NLM_F_MULTI, rd); | |
915 | if (err < 0) | |
3e61aa8f | 916 | goto out; |
6681712d | 917 | } |
3e61aa8f SH |
918 | skip: |
919 | ++idx; | |
d342894c | 920 | } |
921 | } | |
3e61aa8f | 922 | out: |
d342894c | 923 | return idx; |
924 | } | |
925 | ||
926 | /* Watch incoming packets to learn mapping between Ethernet address | |
927 | * and Tunnel endpoint. | |
26a41ae6 | 928 | * Return true if packet is bogus and should be droppped. |
d342894c | 929 | */ |
26a41ae6 | 930 | static bool vxlan_snoop(struct net_device *dev, |
e4c7ed41 | 931 | union vxlan_addr *src_ip, const u8 *src_mac) |
d342894c | 932 | { |
933 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
934 | struct vxlan_fdb *f; | |
d342894c | 935 | |
936 | f = vxlan_find_mac(vxlan, src_mac); | |
937 | if (likely(f)) { | |
5ca5461c | 938 | struct vxlan_rdst *rdst = first_remote_rcu(f); |
3e61aa8f | 939 | |
e4c7ed41 | 940 | if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip))) |
26a41ae6 | 941 | return false; |
942 | ||
943 | /* Don't migrate static entries, drop packets */ | |
eb064c3b | 944 | if (f->state & NUD_NOARP) |
26a41ae6 | 945 | return true; |
d342894c | 946 | |
947 | if (net_ratelimit()) | |
948 | netdev_info(dev, | |
e4c7ed41 | 949 | "%pM migrated from %pIS to %pIS\n", |
3e61aa8f | 950 | src_mac, &rdst->remote_ip, &src_ip); |
d342894c | 951 | |
e4c7ed41 | 952 | rdst->remote_ip = *src_ip; |
d342894c | 953 | f->updated = jiffies; |
9e4b93f9 | 954 | vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH); |
d342894c | 955 | } else { |
956 | /* learned new entry */ | |
957 | spin_lock(&vxlan->hash_lock); | |
3bf74b1a | 958 | |
959 | /* close off race between vxlan_flush and incoming packets */ | |
960 | if (netif_running(dev)) | |
961 | vxlan_fdb_create(vxlan, src_mac, src_ip, | |
962 | NUD_REACHABLE, | |
963 | NLM_F_EXCL|NLM_F_CREATE, | |
964 | vxlan->dst_port, | |
965 | vxlan->default_dst.remote_vni, | |
966 | 0, NTF_SELF); | |
d342894c | 967 | spin_unlock(&vxlan->hash_lock); |
968 | } | |
26a41ae6 | 969 | |
970 | return false; | |
d342894c | 971 | } |
972 | ||
d342894c | 973 | /* See if multicast group is already in use by other ID */ |
95ab0991 | 974 | static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev) |
d342894c | 975 | { |
553675fb | 976 | struct vxlan_dev *vxlan; |
d342894c | 977 | |
95ab0991 G |
978 | /* The vxlan_sock is only used by dev, leaving group has |
979 | * no effect on other vxlan devices. | |
980 | */ | |
981 | if (atomic_read(&dev->vn_sock->refcnt) == 1) | |
982 | return false; | |
983 | ||
553675fb | 984 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
95ab0991 | 985 | if (!netif_running(vxlan->dev) || vxlan == dev) |
553675fb | 986 | continue; |
d342894c | 987 | |
95ab0991 G |
988 | if (vxlan->vn_sock != dev->vn_sock) |
989 | continue; | |
990 | ||
991 | if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip, | |
992 | &dev->default_dst.remote_ip)) | |
993 | continue; | |
994 | ||
995 | if (vxlan->default_dst.remote_ifindex != | |
996 | dev->default_dst.remote_ifindex) | |
997 | continue; | |
998 | ||
999 | return true; | |
553675fb | 1000 | } |
d342894c | 1001 | |
1002 | return false; | |
1003 | } | |
1004 | ||
7c47cedf | 1005 | static void vxlan_sock_hold(struct vxlan_sock *vs) |
d342894c | 1006 | { |
7c47cedf SH |
1007 | atomic_inc(&vs->refcnt); |
1008 | } | |
d342894c | 1009 | |
012a5729 | 1010 | void vxlan_sock_release(struct vxlan_sock *vs) |
7c47cedf | 1011 | { |
53cf5275 JG |
1012 | struct sock *sk = vs->sock->sk; |
1013 | struct net *net = sock_net(sk); | |
1014 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
012a5729 | 1015 | |
7c47cedf SH |
1016 | if (!atomic_dec_and_test(&vs->refcnt)) |
1017 | return; | |
d342894c | 1018 | |
1c51a915 | 1019 | spin_lock(&vn->sock_lock); |
7c47cedf | 1020 | hlist_del_rcu(&vs->hlist); |
dc01e7d3 | 1021 | vxlan_notify_del_rx_port(vs); |
1c51a915 SH |
1022 | spin_unlock(&vn->sock_lock); |
1023 | ||
7c47cedf | 1024 | queue_work(vxlan_wq, &vs->del_work); |
d342894c | 1025 | } |
012a5729 | 1026 | EXPORT_SYMBOL_GPL(vxlan_sock_release); |
d342894c | 1027 | |
3fc2de2f | 1028 | /* Callback to update multicast group membership when first VNI on |
1029 | * multicast asddress is brought up | |
1030 | * Done as workqueue because ip_mc_join_group acquires RTNL. | |
7c47cedf | 1031 | */ |
3fc2de2f | 1032 | static void vxlan_igmp_join(struct work_struct *work) |
d342894c | 1033 | { |
3fc2de2f | 1034 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join); |
7c47cedf SH |
1035 | struct vxlan_sock *vs = vxlan->vn_sock; |
1036 | struct sock *sk = vs->sock->sk; | |
e4c7ed41 CW |
1037 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
1038 | int ifindex = vxlan->default_dst.remote_ifindex; | |
d342894c | 1039 | |
d342894c | 1040 | lock_sock(sk); |
e4c7ed41 CW |
1041 | if (ip->sa.sa_family == AF_INET) { |
1042 | struct ip_mreqn mreq = { | |
1043 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, | |
1044 | .imr_ifindex = ifindex, | |
1045 | }; | |
1046 | ||
1047 | ip_mc_join_group(sk, &mreq); | |
1048 | #if IS_ENABLED(CONFIG_IPV6) | |
1049 | } else { | |
1050 | ipv6_stub->ipv6_sock_mc_join(sk, ifindex, | |
1051 | &ip->sin6.sin6_addr); | |
1052 | #endif | |
1053 | } | |
3fc2de2f | 1054 | release_sock(sk); |
1055 | ||
012a5729 | 1056 | vxlan_sock_release(vs); |
3fc2de2f | 1057 | dev_put(vxlan->dev); |
1058 | } | |
1059 | ||
1060 | /* Inverse of vxlan_igmp_join when last VNI is brought down */ | |
1061 | static void vxlan_igmp_leave(struct work_struct *work) | |
1062 | { | |
1063 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave); | |
3fc2de2f | 1064 | struct vxlan_sock *vs = vxlan->vn_sock; |
1065 | struct sock *sk = vs->sock->sk; | |
e4c7ed41 CW |
1066 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
1067 | int ifindex = vxlan->default_dst.remote_ifindex; | |
3fc2de2f | 1068 | |
1069 | lock_sock(sk); | |
e4c7ed41 CW |
1070 | if (ip->sa.sa_family == AF_INET) { |
1071 | struct ip_mreqn mreq = { | |
1072 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, | |
1073 | .imr_ifindex = ifindex, | |
1074 | }; | |
1075 | ||
1076 | ip_mc_leave_group(sk, &mreq); | |
1077 | #if IS_ENABLED(CONFIG_IPV6) | |
1078 | } else { | |
1079 | ipv6_stub->ipv6_sock_mc_drop(sk, ifindex, | |
1080 | &ip->sin6.sin6_addr); | |
1081 | #endif | |
1082 | } | |
1083 | ||
d342894c | 1084 | release_sock(sk); |
d342894c | 1085 | |
012a5729 | 1086 | vxlan_sock_release(vs); |
7c47cedf | 1087 | dev_put(vxlan->dev); |
d342894c | 1088 | } |
1089 | ||
1090 | /* Callback from net/ipv4/udp.c to receive packets */ | |
1091 | static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) | |
1092 | { | |
5cfccc5a | 1093 | struct vxlan_sock *vs; |
d342894c | 1094 | struct vxlanhdr *vxh; |
3bf39475 | 1095 | u32 flags, vni; |
d342894c | 1096 | |
d342894c | 1097 | /* Need Vxlan and inner Ethernet header to be present */ |
7ce04758 | 1098 | if (!pskb_may_pull(skb, VXLAN_HLEN)) |
d342894c | 1099 | goto error; |
1100 | ||
7ce04758 | 1101 | vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1); |
3bf39475 TH |
1102 | flags = ntohl(vxh->vx_flags); |
1103 | vni = ntohl(vxh->vx_vni); | |
1104 | ||
1105 | if (flags & VXLAN_HF_VNI) { | |
1106 | flags &= ~VXLAN_HF_VNI; | |
1107 | } else { | |
1108 | /* VNI flag always required to be set */ | |
1109 | goto bad_flags; | |
d342894c | 1110 | } |
1111 | ||
5cfccc5a PS |
1112 | if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB))) |
1113 | goto drop; | |
1114 | ||
559835ea | 1115 | vs = rcu_dereference_sk_user_data(sk); |
5cfccc5a | 1116 | if (!vs) |
d342894c | 1117 | goto drop; |
d342894c | 1118 | |
3bf39475 TH |
1119 | if (flags || (vni & 0xff)) { |
1120 | /* If there are any unprocessed flags remaining treat | |
1121 | * this as a malformed packet. This behavior diverges from | |
1122 | * VXLAN RFC (RFC7348) which stipulates that bits in reserved | |
1123 | * in reserved fields are to be ignored. The approach here | |
1124 | * maintains compatbility with previous stack code, and also | |
1125 | * is more robust and provides a little more security in | |
1126 | * adding extensions to VXLAN. | |
1127 | */ | |
1128 | ||
1129 | goto bad_flags; | |
1130 | } | |
1131 | ||
5cfccc5a PS |
1132 | vs->rcv(vs, skb, vxh->vx_vni); |
1133 | return 0; | |
1134 | ||
1135 | drop: | |
1136 | /* Consume bad packet */ | |
1137 | kfree_skb(skb); | |
1138 | return 0; | |
1139 | ||
3bf39475 TH |
1140 | bad_flags: |
1141 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", | |
1142 | ntohl(vxh->vx_flags), ntohl(vxh->vx_vni)); | |
1143 | ||
5cfccc5a PS |
1144 | error: |
1145 | /* Return non vxlan pkt */ | |
1146 | return 1; | |
1147 | } | |
1148 | ||
1149 | static void vxlan_rcv(struct vxlan_sock *vs, | |
1150 | struct sk_buff *skb, __be32 vx_vni) | |
1151 | { | |
e4c7ed41 CW |
1152 | struct iphdr *oip = NULL; |
1153 | struct ipv6hdr *oip6 = NULL; | |
5cfccc5a | 1154 | struct vxlan_dev *vxlan; |
8f84985f | 1155 | struct pcpu_sw_netstats *stats; |
e4c7ed41 | 1156 | union vxlan_addr saddr; |
5cfccc5a | 1157 | __u32 vni; |
e4c7ed41 CW |
1158 | int err = 0; |
1159 | union vxlan_addr *remote_ip; | |
5cfccc5a PS |
1160 | |
1161 | vni = ntohl(vx_vni) >> 8; | |
1162 | /* Is this VNI defined? */ | |
1163 | vxlan = vxlan_vs_find_vni(vs, vni); | |
1164 | if (!vxlan) | |
d342894c | 1165 | goto drop; |
d342894c | 1166 | |
e4c7ed41 | 1167 | remote_ip = &vxlan->default_dst.remote_ip; |
e4f67add | 1168 | skb_reset_mac_header(skb); |
f01ec1c0 | 1169 | skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev))); |
d342894c | 1170 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
f79b064c | 1171 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
d342894c | 1172 | |
1173 | /* Ignore packet loops (and multicast echo) */ | |
7367d0b5 | 1174 | if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr)) |
d342894c | 1175 | goto drop; |
1176 | ||
7ce04758 | 1177 | /* Re-examine inner Ethernet packet */ |
e4c7ed41 CW |
1178 | if (remote_ip->sa.sa_family == AF_INET) { |
1179 | oip = ip_hdr(skb); | |
1180 | saddr.sin.sin_addr.s_addr = oip->saddr; | |
1181 | saddr.sa.sa_family = AF_INET; | |
1182 | #if IS_ENABLED(CONFIG_IPV6) | |
1183 | } else { | |
1184 | oip6 = ipv6_hdr(skb); | |
1185 | saddr.sin6.sin6_addr = oip6->saddr; | |
1186 | saddr.sa.sa_family = AF_INET6; | |
1187 | #endif | |
1188 | } | |
1189 | ||
26a41ae6 | 1190 | if ((vxlan->flags & VXLAN_F_LEARN) && |
e4c7ed41 | 1191 | vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source)) |
26a41ae6 | 1192 | goto drop; |
d342894c | 1193 | |
d342894c | 1194 | skb_reset_network_header(skb); |
0afb1666 | 1195 | |
e4c7ed41 CW |
1196 | if (oip6) |
1197 | err = IP6_ECN_decapsulate(oip6, skb); | |
1198 | if (oip) | |
1199 | err = IP_ECN_decapsulate(oip, skb); | |
1200 | ||
d342894c | 1201 | if (unlikely(err)) { |
e4c7ed41 CW |
1202 | if (log_ecn_error) { |
1203 | if (oip6) | |
1204 | net_info_ratelimited("non-ECT from %pI6\n", | |
1205 | &oip6->saddr); | |
1206 | if (oip) | |
1207 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", | |
1208 | &oip->saddr, oip->tos); | |
1209 | } | |
d342894c | 1210 | if (err > 1) { |
1211 | ++vxlan->dev->stats.rx_frame_errors; | |
1212 | ++vxlan->dev->stats.rx_errors; | |
1213 | goto drop; | |
1214 | } | |
1215 | } | |
1216 | ||
e8171045 | 1217 | stats = this_cpu_ptr(vxlan->dev->tstats); |
d342894c | 1218 | u64_stats_update_begin(&stats->syncp); |
1219 | stats->rx_packets++; | |
1220 | stats->rx_bytes += skb->len; | |
1221 | u64_stats_update_end(&stats->syncp); | |
1222 | ||
1223 | netif_rx(skb); | |
1224 | ||
5cfccc5a | 1225 | return; |
d342894c | 1226 | drop: |
1227 | /* Consume bad packet */ | |
1228 | kfree_skb(skb); | |
d342894c | 1229 | } |
1230 | ||
e4f67add DS |
1231 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb) |
1232 | { | |
1233 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1234 | struct arphdr *parp; | |
1235 | u8 *arpptr, *sha; | |
1236 | __be32 sip, tip; | |
1237 | struct neighbour *n; | |
1238 | ||
1239 | if (dev->flags & IFF_NOARP) | |
1240 | goto out; | |
1241 | ||
1242 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { | |
1243 | dev->stats.tx_dropped++; | |
1244 | goto out; | |
1245 | } | |
1246 | parp = arp_hdr(skb); | |
1247 | ||
1248 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && | |
1249 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || | |
1250 | parp->ar_pro != htons(ETH_P_IP) || | |
1251 | parp->ar_op != htons(ARPOP_REQUEST) || | |
1252 | parp->ar_hln != dev->addr_len || | |
1253 | parp->ar_pln != 4) | |
1254 | goto out; | |
1255 | arpptr = (u8 *)parp + sizeof(struct arphdr); | |
1256 | sha = arpptr; | |
1257 | arpptr += dev->addr_len; /* sha */ | |
1258 | memcpy(&sip, arpptr, sizeof(sip)); | |
1259 | arpptr += sizeof(sip); | |
1260 | arpptr += dev->addr_len; /* tha */ | |
1261 | memcpy(&tip, arpptr, sizeof(tip)); | |
1262 | ||
1263 | if (ipv4_is_loopback(tip) || | |
1264 | ipv4_is_multicast(tip)) | |
1265 | goto out; | |
1266 | ||
1267 | n = neigh_lookup(&arp_tbl, &tip, dev); | |
1268 | ||
1269 | if (n) { | |
e4f67add DS |
1270 | struct vxlan_fdb *f; |
1271 | struct sk_buff *reply; | |
1272 | ||
1273 | if (!(n->nud_state & NUD_CONNECTED)) { | |
1274 | neigh_release(n); | |
1275 | goto out; | |
1276 | } | |
1277 | ||
1278 | f = vxlan_find_mac(vxlan, n->ha); | |
e4c7ed41 | 1279 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
e4f67add DS |
1280 | /* bridge-local neighbor */ |
1281 | neigh_release(n); | |
1282 | goto out; | |
1283 | } | |
1284 | ||
1285 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, | |
1286 | n->ha, sha); | |
1287 | ||
1288 | neigh_release(n); | |
1289 | ||
7346135d DS |
1290 | if (reply == NULL) |
1291 | goto out; | |
1292 | ||
e4f67add DS |
1293 | skb_reset_mac_header(reply); |
1294 | __skb_pull(reply, skb_network_offset(reply)); | |
1295 | reply->ip_summed = CHECKSUM_UNNECESSARY; | |
1296 | reply->pkt_type = PACKET_HOST; | |
1297 | ||
1298 | if (netif_rx_ni(reply) == NET_RX_DROP) | |
1299 | dev->stats.rx_dropped++; | |
e4c7ed41 CW |
1300 | } else if (vxlan->flags & VXLAN_F_L3MISS) { |
1301 | union vxlan_addr ipa = { | |
1302 | .sin.sin_addr.s_addr = tip, | |
a45e92a5 | 1303 | .sin.sin_family = AF_INET, |
e4c7ed41 CW |
1304 | }; |
1305 | ||
1306 | vxlan_ip_miss(dev, &ipa); | |
1307 | } | |
e4f67add DS |
1308 | out: |
1309 | consume_skb(skb); | |
1310 | return NETDEV_TX_OK; | |
1311 | } | |
1312 | ||
f564f45c | 1313 | #if IS_ENABLED(CONFIG_IPV6) |
4b29dba9 DS |
1314 | static struct sk_buff *vxlan_na_create(struct sk_buff *request, |
1315 | struct neighbour *n, bool isrouter) | |
1316 | { | |
1317 | struct net_device *dev = request->dev; | |
1318 | struct sk_buff *reply; | |
1319 | struct nd_msg *ns, *na; | |
1320 | struct ipv6hdr *pip6; | |
1321 | u8 *daddr; | |
1322 | int na_olen = 8; /* opt hdr + ETH_ALEN for target */ | |
1323 | int ns_olen; | |
1324 | int i, len; | |
1325 | ||
1326 | if (dev == NULL) | |
1327 | return NULL; | |
1328 | ||
1329 | len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) + | |
1330 | sizeof(*na) + na_olen + dev->needed_tailroom; | |
1331 | reply = alloc_skb(len, GFP_ATOMIC); | |
1332 | if (reply == NULL) | |
1333 | return NULL; | |
1334 | ||
1335 | reply->protocol = htons(ETH_P_IPV6); | |
1336 | reply->dev = dev; | |
1337 | skb_reserve(reply, LL_RESERVED_SPACE(request->dev)); | |
1338 | skb_push(reply, sizeof(struct ethhdr)); | |
1339 | skb_set_mac_header(reply, 0); | |
1340 | ||
1341 | ns = (struct nd_msg *)skb_transport_header(request); | |
1342 | ||
1343 | daddr = eth_hdr(request)->h_source; | |
1344 | ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns); | |
1345 | for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) { | |
1346 | if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) { | |
1347 | daddr = ns->opt + i + sizeof(struct nd_opt_hdr); | |
1348 | break; | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | /* Ethernet header */ | |
1353 | ether_addr_copy(eth_hdr(reply)->h_dest, daddr); | |
1354 | ether_addr_copy(eth_hdr(reply)->h_source, n->ha); | |
1355 | eth_hdr(reply)->h_proto = htons(ETH_P_IPV6); | |
1356 | reply->protocol = htons(ETH_P_IPV6); | |
1357 | ||
1358 | skb_pull(reply, sizeof(struct ethhdr)); | |
1359 | skb_set_network_header(reply, 0); | |
1360 | skb_put(reply, sizeof(struct ipv6hdr)); | |
1361 | ||
1362 | /* IPv6 header */ | |
1363 | ||
1364 | pip6 = ipv6_hdr(reply); | |
1365 | memset(pip6, 0, sizeof(struct ipv6hdr)); | |
1366 | pip6->version = 6; | |
1367 | pip6->priority = ipv6_hdr(request)->priority; | |
1368 | pip6->nexthdr = IPPROTO_ICMPV6; | |
1369 | pip6->hop_limit = 255; | |
1370 | pip6->daddr = ipv6_hdr(request)->saddr; | |
1371 | pip6->saddr = *(struct in6_addr *)n->primary_key; | |
1372 | ||
1373 | skb_pull(reply, sizeof(struct ipv6hdr)); | |
1374 | skb_set_transport_header(reply, 0); | |
1375 | ||
1376 | na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen); | |
1377 | ||
1378 | /* Neighbor Advertisement */ | |
1379 | memset(na, 0, sizeof(*na)+na_olen); | |
1380 | na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; | |
1381 | na->icmph.icmp6_router = isrouter; | |
1382 | na->icmph.icmp6_override = 1; | |
1383 | na->icmph.icmp6_solicited = 1; | |
1384 | na->target = ns->target; | |
1385 | ether_addr_copy(&na->opt[2], n->ha); | |
1386 | na->opt[0] = ND_OPT_TARGET_LL_ADDR; | |
1387 | na->opt[1] = na_olen >> 3; | |
1388 | ||
1389 | na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr, | |
1390 | &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6, | |
1391 | csum_partial(na, sizeof(*na)+na_olen, 0)); | |
1392 | ||
1393 | pip6->payload_len = htons(sizeof(*na)+na_olen); | |
1394 | ||
1395 | skb_push(reply, sizeof(struct ipv6hdr)); | |
1396 | ||
1397 | reply->ip_summed = CHECKSUM_UNNECESSARY; | |
1398 | ||
1399 | return reply; | |
1400 | } | |
1401 | ||
f564f45c CW |
1402 | static int neigh_reduce(struct net_device *dev, struct sk_buff *skb) |
1403 | { | |
1404 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
4b29dba9 | 1405 | struct nd_msg *msg; |
f564f45c CW |
1406 | const struct ipv6hdr *iphdr; |
1407 | const struct in6_addr *saddr, *daddr; | |
4b29dba9 DS |
1408 | struct neighbour *n; |
1409 | struct inet6_dev *in6_dev; | |
f564f45c CW |
1410 | |
1411 | in6_dev = __in6_dev_get(dev); | |
1412 | if (!in6_dev) | |
1413 | goto out; | |
1414 | ||
f564f45c CW |
1415 | iphdr = ipv6_hdr(skb); |
1416 | saddr = &iphdr->saddr; | |
1417 | daddr = &iphdr->daddr; | |
1418 | ||
f564f45c CW |
1419 | msg = (struct nd_msg *)skb_transport_header(skb); |
1420 | if (msg->icmph.icmp6_code != 0 || | |
1421 | msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) | |
1422 | goto out; | |
1423 | ||
4b29dba9 DS |
1424 | if (ipv6_addr_loopback(daddr) || |
1425 | ipv6_addr_is_multicast(&msg->target)) | |
1426 | goto out; | |
1427 | ||
1428 | n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev); | |
f564f45c CW |
1429 | |
1430 | if (n) { | |
1431 | struct vxlan_fdb *f; | |
4b29dba9 | 1432 | struct sk_buff *reply; |
f564f45c CW |
1433 | |
1434 | if (!(n->nud_state & NUD_CONNECTED)) { | |
1435 | neigh_release(n); | |
1436 | goto out; | |
1437 | } | |
1438 | ||
1439 | f = vxlan_find_mac(vxlan, n->ha); | |
1440 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { | |
1441 | /* bridge-local neighbor */ | |
1442 | neigh_release(n); | |
1443 | goto out; | |
1444 | } | |
1445 | ||
4b29dba9 DS |
1446 | reply = vxlan_na_create(skb, n, |
1447 | !!(f ? f->flags & NTF_ROUTER : 0)); | |
1448 | ||
f564f45c | 1449 | neigh_release(n); |
4b29dba9 DS |
1450 | |
1451 | if (reply == NULL) | |
1452 | goto out; | |
1453 | ||
1454 | if (netif_rx_ni(reply) == NET_RX_DROP) | |
1455 | dev->stats.rx_dropped++; | |
1456 | ||
f564f45c | 1457 | } else if (vxlan->flags & VXLAN_F_L3MISS) { |
4b29dba9 DS |
1458 | union vxlan_addr ipa = { |
1459 | .sin6.sin6_addr = msg->target, | |
a45e92a5 | 1460 | .sin6.sin6_family = AF_INET6, |
4b29dba9 DS |
1461 | }; |
1462 | ||
f564f45c CW |
1463 | vxlan_ip_miss(dev, &ipa); |
1464 | } | |
1465 | ||
1466 | out: | |
1467 | consume_skb(skb); | |
1468 | return NETDEV_TX_OK; | |
1469 | } | |
1470 | #endif | |
1471 | ||
e4f67add DS |
1472 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) |
1473 | { | |
1474 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1475 | struct neighbour *n; | |
e4f67add DS |
1476 | |
1477 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) | |
1478 | return false; | |
1479 | ||
1480 | n = NULL; | |
1481 | switch (ntohs(eth_hdr(skb)->h_proto)) { | |
1482 | case ETH_P_IP: | |
e15a00aa CW |
1483 | { |
1484 | struct iphdr *pip; | |
1485 | ||
e4f67add DS |
1486 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
1487 | return false; | |
1488 | pip = ip_hdr(skb); | |
1489 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); | |
e4c7ed41 CW |
1490 | if (!n && (vxlan->flags & VXLAN_F_L3MISS)) { |
1491 | union vxlan_addr ipa = { | |
1492 | .sin.sin_addr.s_addr = pip->daddr, | |
a45e92a5 | 1493 | .sin.sin_family = AF_INET, |
e4c7ed41 CW |
1494 | }; |
1495 | ||
1496 | vxlan_ip_miss(dev, &ipa); | |
1497 | return false; | |
1498 | } | |
1499 | ||
e4f67add | 1500 | break; |
e15a00aa CW |
1501 | } |
1502 | #if IS_ENABLED(CONFIG_IPV6) | |
1503 | case ETH_P_IPV6: | |
1504 | { | |
1505 | struct ipv6hdr *pip6; | |
1506 | ||
1507 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) | |
1508 | return false; | |
1509 | pip6 = ipv6_hdr(skb); | |
1510 | n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev); | |
1511 | if (!n && (vxlan->flags & VXLAN_F_L3MISS)) { | |
1512 | union vxlan_addr ipa = { | |
1513 | .sin6.sin6_addr = pip6->daddr, | |
a45e92a5 | 1514 | .sin6.sin6_family = AF_INET6, |
e15a00aa CW |
1515 | }; |
1516 | ||
1517 | vxlan_ip_miss(dev, &ipa); | |
1518 | return false; | |
1519 | } | |
1520 | ||
1521 | break; | |
1522 | } | |
1523 | #endif | |
e4f67add DS |
1524 | default: |
1525 | return false; | |
1526 | } | |
1527 | ||
1528 | if (n) { | |
1529 | bool diff; | |
1530 | ||
7367d0b5 | 1531 | diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha); |
e4f67add DS |
1532 | if (diff) { |
1533 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, | |
1534 | dev->addr_len); | |
1535 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); | |
1536 | } | |
1537 | neigh_release(n); | |
1538 | return diff; | |
e4c7ed41 CW |
1539 | } |
1540 | ||
e4f67add DS |
1541 | return false; |
1542 | } | |
1543 | ||
e4c7ed41 | 1544 | #if IS_ENABLED(CONFIG_IPV6) |
11796187 | 1545 | static int vxlan6_xmit_skb(struct vxlan_sock *vs, |
e4c7ed41 CW |
1546 | struct dst_entry *dst, struct sk_buff *skb, |
1547 | struct net_device *dev, struct in6_addr *saddr, | |
1548 | struct in6_addr *daddr, __u8 prio, __u8 ttl, | |
f01ec1c0 ND |
1549 | __be16 src_port, __be16 dst_port, __be32 vni, |
1550 | bool xnet) | |
e4c7ed41 | 1551 | { |
e4c7ed41 | 1552 | struct vxlanhdr *vxh; |
e4c7ed41 CW |
1553 | int min_headroom; |
1554 | int err; | |
acbf74a7 | 1555 | bool udp_sum = !udp_get_no_check6_tx(vs->sock->sk); |
e4c7ed41 | 1556 | |
acbf74a7 | 1557 | skb = udp_tunnel_handle_offloads(skb, udp_sum); |
74f47278 PS |
1558 | if (IS_ERR(skb)) { |
1559 | err = -EINVAL; | |
1560 | goto err; | |
1561 | } | |
e4c7ed41 | 1562 | |
f01ec1c0 | 1563 | skb_scrub_packet(skb, xnet); |
963a88b3 | 1564 | |
e4c7ed41 CW |
1565 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len |
1566 | + VXLAN_HLEN + sizeof(struct ipv6hdr) | |
df8a39de | 1567 | + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); |
e4c7ed41 CW |
1568 | |
1569 | /* Need space for new headers (invalidates iph ptr) */ | |
1570 | err = skb_cow_head(skb, min_headroom); | |
74f47278 PS |
1571 | if (unlikely(err)) { |
1572 | kfree_skb(skb); | |
1573 | goto err; | |
1574 | } | |
e4c7ed41 | 1575 | |
5968250c | 1576 | skb = vlan_hwaccel_push_inside(skb); |
74f47278 PS |
1577 | if (WARN_ON(!skb)) { |
1578 | err = -ENOMEM; | |
1579 | goto err; | |
1580 | } | |
e4c7ed41 CW |
1581 | |
1582 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); | |
3bf39475 | 1583 | vxh->vx_flags = htonl(VXLAN_HF_VNI); |
e4c7ed41 CW |
1584 | vxh->vx_vni = vni; |
1585 | ||
996c9fd1 TH |
1586 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
1587 | ||
acbf74a7 AZ |
1588 | udp_tunnel6_xmit_skb(vs->sock, dst, skb, dev, saddr, daddr, prio, |
1589 | ttl, src_port, dst_port); | |
e4c7ed41 | 1590 | return 0; |
74f47278 PS |
1591 | err: |
1592 | dst_release(dst); | |
1593 | return err; | |
e4c7ed41 CW |
1594 | } |
1595 | #endif | |
1596 | ||
11796187 | 1597 | int vxlan_xmit_skb(struct vxlan_sock *vs, |
49560532 PS |
1598 | struct rtable *rt, struct sk_buff *skb, |
1599 | __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, | |
f01ec1c0 | 1600 | __be16 src_port, __be16 dst_port, __be32 vni, bool xnet) |
49560532 PS |
1601 | { |
1602 | struct vxlanhdr *vxh; | |
649c5b8b | 1603 | int min_headroom; |
49560532 | 1604 | int err; |
acbf74a7 | 1605 | bool udp_sum = !vs->sock->sk->sk_no_check_tx; |
49560532 | 1606 | |
acbf74a7 | 1607 | skb = udp_tunnel_handle_offloads(skb, udp_sum); |
359a0ea9 | 1608 | if (IS_ERR(skb)) |
74f47278 | 1609 | return PTR_ERR(skb); |
49560532 | 1610 | |
649c5b8b | 1611 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
1eaa8178 | 1612 | + VXLAN_HLEN + sizeof(struct iphdr) |
df8a39de | 1613 | + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); |
649c5b8b PS |
1614 | |
1615 | /* Need space for new headers (invalidates iph ptr) */ | |
1616 | err = skb_cow_head(skb, min_headroom); | |
74f47278 PS |
1617 | if (unlikely(err)) { |
1618 | kfree_skb(skb); | |
649c5b8b | 1619 | return err; |
74f47278 | 1620 | } |
649c5b8b | 1621 | |
5968250c JP |
1622 | skb = vlan_hwaccel_push_inside(skb); |
1623 | if (WARN_ON(!skb)) | |
1624 | return -ENOMEM; | |
1eaa8178 | 1625 | |
49560532 | 1626 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); |
3bf39475 | 1627 | vxh->vx_flags = htonl(VXLAN_HF_VNI); |
49560532 PS |
1628 | vxh->vx_vni = vni; |
1629 | ||
996c9fd1 TH |
1630 | skb_set_inner_protocol(skb, htons(ETH_P_TEB)); |
1631 | ||
acbf74a7 AZ |
1632 | return udp_tunnel_xmit_skb(vs->sock, rt, skb, src, dst, tos, |
1633 | ttl, df, src_port, dst_port, xnet); | |
49560532 PS |
1634 | } |
1635 | EXPORT_SYMBOL_GPL(vxlan_xmit_skb); | |
1636 | ||
9dcc71e1 SS |
1637 | /* Bypass encapsulation if the destination is local */ |
1638 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, | |
1639 | struct vxlan_dev *dst_vxlan) | |
1640 | { | |
8f84985f | 1641 | struct pcpu_sw_netstats *tx_stats, *rx_stats; |
e4c7ed41 CW |
1642 | union vxlan_addr loopback; |
1643 | union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip; | |
ce6502a8 LR |
1644 | struct net_device *dev = skb->dev; |
1645 | int len = skb->len; | |
9dcc71e1 | 1646 | |
8f84985f LR |
1647 | tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); |
1648 | rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); | |
9dcc71e1 SS |
1649 | skb->pkt_type = PACKET_HOST; |
1650 | skb->encapsulation = 0; | |
1651 | skb->dev = dst_vxlan->dev; | |
1652 | __skb_pull(skb, skb_network_offset(skb)); | |
1653 | ||
e4c7ed41 CW |
1654 | if (remote_ip->sa.sa_family == AF_INET) { |
1655 | loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
1656 | loopback.sa.sa_family = AF_INET; | |
1657 | #if IS_ENABLED(CONFIG_IPV6) | |
1658 | } else { | |
1659 | loopback.sin6.sin6_addr = in6addr_loopback; | |
1660 | loopback.sa.sa_family = AF_INET6; | |
1661 | #endif | |
1662 | } | |
1663 | ||
9dcc71e1 | 1664 | if (dst_vxlan->flags & VXLAN_F_LEARN) |
e4c7ed41 | 1665 | vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source); |
9dcc71e1 SS |
1666 | |
1667 | u64_stats_update_begin(&tx_stats->syncp); | |
1668 | tx_stats->tx_packets++; | |
ce6502a8 | 1669 | tx_stats->tx_bytes += len; |
9dcc71e1 SS |
1670 | u64_stats_update_end(&tx_stats->syncp); |
1671 | ||
1672 | if (netif_rx(skb) == NET_RX_SUCCESS) { | |
1673 | u64_stats_update_begin(&rx_stats->syncp); | |
1674 | rx_stats->rx_packets++; | |
ce6502a8 | 1675 | rx_stats->rx_bytes += len; |
9dcc71e1 SS |
1676 | u64_stats_update_end(&rx_stats->syncp); |
1677 | } else { | |
ce6502a8 | 1678 | dev->stats.rx_dropped++; |
9dcc71e1 SS |
1679 | } |
1680 | } | |
1681 | ||
4ad16930 SH |
1682 | static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
1683 | struct vxlan_rdst *rdst, bool did_rsc) | |
d342894c | 1684 | { |
1685 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
e4c7ed41 | 1686 | struct rtable *rt = NULL; |
d342894c | 1687 | const struct iphdr *old_iph; |
d342894c | 1688 | struct flowi4 fl4; |
e4c7ed41 CW |
1689 | union vxlan_addr *dst; |
1690 | __be16 src_port = 0, dst_port; | |
234f5b73 | 1691 | u32 vni; |
d342894c | 1692 | __be16 df = 0; |
1693 | __u8 tos, ttl; | |
0e6fbc5b | 1694 | int err; |
d342894c | 1695 | |
823aa873 | 1696 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port; |
6681712d | 1697 | vni = rdst->remote_vni; |
e4c7ed41 | 1698 | dst = &rdst->remote_ip; |
e4f67add | 1699 | |
e4c7ed41 | 1700 | if (vxlan_addr_any(dst)) { |
e4f67add | 1701 | if (did_rsc) { |
e4f67add | 1702 | /* short-circuited back to local bridge */ |
9dcc71e1 | 1703 | vxlan_encap_bypass(skb, vxlan, vxlan); |
4ad16930 | 1704 | return; |
e4f67add | 1705 | } |
ef59febe | 1706 | goto drop; |
e4f67add | 1707 | } |
ef59febe | 1708 | |
d342894c | 1709 | old_iph = ip_hdr(skb); |
1710 | ||
d342894c | 1711 | ttl = vxlan->ttl; |
e4c7ed41 | 1712 | if (!ttl && vxlan_addr_multicast(dst)) |
d342894c | 1713 | ttl = 1; |
1714 | ||
1715 | tos = vxlan->tos; | |
1716 | if (tos == 1) | |
206aaafc | 1717 | tos = ip_tunnel_get_dsfield(old_iph, skb); |
d342894c | 1718 | |
535fb8d0 TH |
1719 | src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min, |
1720 | vxlan->port_max, true); | |
d342894c | 1721 | |
e4c7ed41 CW |
1722 | if (dst->sa.sa_family == AF_INET) { |
1723 | memset(&fl4, 0, sizeof(fl4)); | |
1724 | fl4.flowi4_oif = rdst->remote_ifindex; | |
1725 | fl4.flowi4_tos = RT_TOS(tos); | |
1726 | fl4.daddr = dst->sin.sin_addr.s_addr; | |
1727 | fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr; | |
1728 | ||
f01ec1c0 | 1729 | rt = ip_route_output_key(vxlan->net, &fl4); |
e4c7ed41 CW |
1730 | if (IS_ERR(rt)) { |
1731 | netdev_dbg(dev, "no route to %pI4\n", | |
1732 | &dst->sin.sin_addr.s_addr); | |
1733 | dev->stats.tx_carrier_errors++; | |
1734 | goto tx_error; | |
1735 | } | |
d342894c | 1736 | |
e4c7ed41 CW |
1737 | if (rt->dst.dev == dev) { |
1738 | netdev_dbg(dev, "circular route to %pI4\n", | |
1739 | &dst->sin.sin_addr.s_addr); | |
1740 | dev->stats.collisions++; | |
fffc15a5 | 1741 | goto rt_tx_error; |
e4c7ed41 CW |
1742 | } |
1743 | ||
1744 | /* Bypass encapsulation if the destination is local */ | |
1745 | if (rt->rt_flags & RTCF_LOCAL && | |
1746 | !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { | |
1747 | struct vxlan_dev *dst_vxlan; | |
1748 | ||
1749 | ip_rt_put(rt); | |
19ca9fc1 ML |
1750 | dst_vxlan = vxlan_find_vni(vxlan->net, vni, |
1751 | dst->sa.sa_family, dst_port); | |
e4c7ed41 CW |
1752 | if (!dst_vxlan) |
1753 | goto tx_error; | |
1754 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); | |
1755 | return; | |
1756 | } | |
1757 | ||
1758 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); | |
1759 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); | |
d342894c | 1760 | |
3c4d1dae AZ |
1761 | err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb, |
1762 | fl4.saddr, dst->sin.sin_addr.s_addr, | |
1763 | tos, ttl, df, src_port, dst_port, | |
1764 | htonl(vni << 8), | |
1765 | !net_eq(vxlan->net, dev_net(vxlan->dev))); | |
74f47278 PS |
1766 | if (err < 0) { |
1767 | /* skb is already freed. */ | |
1768 | skb = NULL; | |
e4c7ed41 | 1769 | goto rt_tx_error; |
74f47278 PS |
1770 | } |
1771 | ||
e4c7ed41 CW |
1772 | iptunnel_xmit_stats(err, &dev->stats, dev->tstats); |
1773 | #if IS_ENABLED(CONFIG_IPV6) | |
1774 | } else { | |
1775 | struct sock *sk = vxlan->vn_sock->sock->sk; | |
1776 | struct dst_entry *ndst; | |
1777 | struct flowi6 fl6; | |
1778 | u32 flags; | |
1779 | ||
1780 | memset(&fl6, 0, sizeof(fl6)); | |
1781 | fl6.flowi6_oif = rdst->remote_ifindex; | |
1782 | fl6.daddr = dst->sin6.sin6_addr; | |
1783 | fl6.saddr = vxlan->saddr.sin6.sin6_addr; | |
8c1bb79f | 1784 | fl6.flowi6_proto = IPPROTO_UDP; |
e4c7ed41 CW |
1785 | |
1786 | if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) { | |
1787 | netdev_dbg(dev, "no route to %pI6\n", | |
1788 | &dst->sin6.sin6_addr); | |
1789 | dev->stats.tx_carrier_errors++; | |
9dcc71e1 | 1790 | goto tx_error; |
e4c7ed41 | 1791 | } |
d342894c | 1792 | |
e4c7ed41 CW |
1793 | if (ndst->dev == dev) { |
1794 | netdev_dbg(dev, "circular route to %pI6\n", | |
1795 | &dst->sin6.sin6_addr); | |
1796 | dst_release(ndst); | |
1797 | dev->stats.collisions++; | |
1798 | goto tx_error; | |
1799 | } | |
0e6fbc5b | 1800 | |
e4c7ed41 CW |
1801 | /* Bypass encapsulation if the destination is local */ |
1802 | flags = ((struct rt6_info *)ndst)->rt6i_flags; | |
1803 | if (flags & RTF_LOCAL && | |
1804 | !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { | |
1805 | struct vxlan_dev *dst_vxlan; | |
1806 | ||
1807 | dst_release(ndst); | |
19ca9fc1 ML |
1808 | dst_vxlan = vxlan_find_vni(vxlan->net, vni, |
1809 | dst->sa.sa_family, dst_port); | |
e4c7ed41 CW |
1810 | if (!dst_vxlan) |
1811 | goto tx_error; | |
1812 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); | |
1813 | return; | |
1814 | } | |
49560532 | 1815 | |
e4c7ed41 CW |
1816 | ttl = ttl ? : ip6_dst_hoplimit(ndst); |
1817 | ||
11796187 | 1818 | err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb, |
e4c7ed41 | 1819 | dev, &fl6.saddr, &fl6.daddr, 0, ttl, |
f01ec1c0 ND |
1820 | src_port, dst_port, htonl(vni << 8), |
1821 | !net_eq(vxlan->net, dev_net(vxlan->dev))); | |
e4c7ed41 CW |
1822 | #endif |
1823 | } | |
0e6fbc5b | 1824 | |
4ad16930 | 1825 | return; |
d342894c | 1826 | |
1827 | drop: | |
1828 | dev->stats.tx_dropped++; | |
1829 | goto tx_free; | |
1830 | ||
49560532 PS |
1831 | rt_tx_error: |
1832 | ip_rt_put(rt); | |
d342894c | 1833 | tx_error: |
1834 | dev->stats.tx_errors++; | |
1835 | tx_free: | |
1836 | dev_kfree_skb(skb); | |
d342894c | 1837 | } |
1838 | ||
6681712d DS |
1839 | /* Transmit local packets over Vxlan |
1840 | * | |
1841 | * Outer IP header inherits ECN and DF from inner header. | |
1842 | * Outer UDP destination is the VXLAN assigned port. | |
1843 | * source port is based on hash of flow | |
1844 | */ | |
1845 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) | |
1846 | { | |
1847 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1848 | struct ethhdr *eth; | |
1849 | bool did_rsc = false; | |
8f646c92 | 1850 | struct vxlan_rdst *rdst, *fdst = NULL; |
6681712d | 1851 | struct vxlan_fdb *f; |
6681712d DS |
1852 | |
1853 | skb_reset_mac_header(skb); | |
1854 | eth = eth_hdr(skb); | |
1855 | ||
f564f45c CW |
1856 | if ((vxlan->flags & VXLAN_F_PROXY)) { |
1857 | if (ntohs(eth->h_proto) == ETH_P_ARP) | |
1858 | return arp_reduce(dev, skb); | |
1859 | #if IS_ENABLED(CONFIG_IPV6) | |
1860 | else if (ntohs(eth->h_proto) == ETH_P_IPV6 && | |
91269e39 LR |
1861 | pskb_may_pull(skb, sizeof(struct ipv6hdr) |
1862 | + sizeof(struct nd_msg)) && | |
f564f45c CW |
1863 | ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { |
1864 | struct nd_msg *msg; | |
1865 | ||
1866 | msg = (struct nd_msg *)skb_transport_header(skb); | |
1867 | if (msg->icmph.icmp6_code == 0 && | |
1868 | msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) | |
1869 | return neigh_reduce(dev, skb); | |
1870 | } | |
7a9f526f | 1871 | eth = eth_hdr(skb); |
f564f45c CW |
1872 | #endif |
1873 | } | |
6681712d DS |
1874 | |
1875 | f = vxlan_find_mac(vxlan, eth->h_dest); | |
ae884082 DS |
1876 | did_rsc = false; |
1877 | ||
1878 | if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) && | |
e15a00aa CW |
1879 | (ntohs(eth->h_proto) == ETH_P_IP || |
1880 | ntohs(eth->h_proto) == ETH_P_IPV6)) { | |
ae884082 DS |
1881 | did_rsc = route_shortcircuit(dev, skb); |
1882 | if (did_rsc) | |
1883 | f = vxlan_find_mac(vxlan, eth->h_dest); | |
1884 | } | |
1885 | ||
6681712d | 1886 | if (f == NULL) { |
afbd8bae MR |
1887 | f = vxlan_find_mac(vxlan, all_zeros_mac); |
1888 | if (f == NULL) { | |
1889 | if ((vxlan->flags & VXLAN_F_L2MISS) && | |
1890 | !is_multicast_ether_addr(eth->h_dest)) | |
1891 | vxlan_fdb_miss(vxlan, eth->h_dest); | |
1892 | ||
1893 | dev->stats.tx_dropped++; | |
8f646c92 | 1894 | kfree_skb(skb); |
afbd8bae MR |
1895 | return NETDEV_TX_OK; |
1896 | } | |
1897 | } | |
6681712d | 1898 | |
afbd8bae MR |
1899 | list_for_each_entry_rcu(rdst, &f->remotes, list) { |
1900 | struct sk_buff *skb1; | |
6681712d | 1901 | |
8f646c92 ED |
1902 | if (!fdst) { |
1903 | fdst = rdst; | |
1904 | continue; | |
1905 | } | |
afbd8bae MR |
1906 | skb1 = skb_clone(skb, GFP_ATOMIC); |
1907 | if (skb1) | |
1908 | vxlan_xmit_one(skb1, dev, rdst, did_rsc); | |
6681712d DS |
1909 | } |
1910 | ||
8f646c92 ED |
1911 | if (fdst) |
1912 | vxlan_xmit_one(skb, dev, fdst, did_rsc); | |
1913 | else | |
1914 | kfree_skb(skb); | |
4ad16930 | 1915 | return NETDEV_TX_OK; |
6681712d DS |
1916 | } |
1917 | ||
d342894c | 1918 | /* Walk the forwarding table and purge stale entries */ |
1919 | static void vxlan_cleanup(unsigned long arg) | |
1920 | { | |
1921 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; | |
1922 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; | |
1923 | unsigned int h; | |
1924 | ||
1925 | if (!netif_running(vxlan->dev)) | |
1926 | return; | |
1927 | ||
1928 | spin_lock_bh(&vxlan->hash_lock); | |
1929 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
1930 | struct hlist_node *p, *n; | |
1931 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { | |
1932 | struct vxlan_fdb *f | |
1933 | = container_of(p, struct vxlan_fdb, hlist); | |
1934 | unsigned long timeout; | |
1935 | ||
3c172868 | 1936 | if (f->state & NUD_PERMANENT) |
d342894c | 1937 | continue; |
1938 | ||
1939 | timeout = f->used + vxlan->age_interval * HZ; | |
1940 | if (time_before_eq(timeout, jiffies)) { | |
1941 | netdev_dbg(vxlan->dev, | |
1942 | "garbage collect %pM\n", | |
1943 | f->eth_addr); | |
1944 | f->state = NUD_STALE; | |
1945 | vxlan_fdb_destroy(vxlan, f); | |
1946 | } else if (time_before(timeout, next_timer)) | |
1947 | next_timer = timeout; | |
1948 | } | |
1949 | } | |
1950 | spin_unlock_bh(&vxlan->hash_lock); | |
1951 | ||
1952 | mod_timer(&vxlan->age_timer, next_timer); | |
1953 | } | |
1954 | ||
9c2e24e1 PS |
1955 | static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan) |
1956 | { | |
1957 | __u32 vni = vxlan->default_dst.remote_vni; | |
1958 | ||
1959 | vxlan->vn_sock = vs; | |
1960 | hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni)); | |
1961 | } | |
1962 | ||
d342894c | 1963 | /* Setup stats when device is created */ |
1964 | static int vxlan_init(struct net_device *dev) | |
1965 | { | |
1c51a915 | 1966 | struct vxlan_dev *vxlan = netdev_priv(dev); |
f01ec1c0 | 1967 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
1c51a915 | 1968 | struct vxlan_sock *vs; |
19ca9fc1 | 1969 | bool ipv6 = vxlan->flags & VXLAN_F_IPV6; |
1c51a915 | 1970 | |
1c213bd2 | 1971 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
e8171045 | 1972 | if (!dev->tstats) |
d342894c | 1973 | return -ENOMEM; |
1974 | ||
1c51a915 | 1975 | spin_lock(&vn->sock_lock); |
19ca9fc1 ML |
1976 | vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET, |
1977 | vxlan->dst_port); | |
00c83b01 | 1978 | if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) { |
1c51a915 | 1979 | /* If we have a socket with same port already, reuse it */ |
9c2e24e1 | 1980 | vxlan_vs_add_dev(vs, vxlan); |
1c51a915 SH |
1981 | } else { |
1982 | /* otherwise make new socket outside of RTNL */ | |
1983 | dev_hold(dev); | |
1984 | queue_work(vxlan_wq, &vxlan->sock_work); | |
1985 | } | |
1986 | spin_unlock(&vn->sock_lock); | |
1987 | ||
d342894c | 1988 | return 0; |
1989 | } | |
1990 | ||
ba609e9b | 1991 | static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan) |
afbd8bae MR |
1992 | { |
1993 | struct vxlan_fdb *f; | |
1994 | ||
1995 | spin_lock_bh(&vxlan->hash_lock); | |
1996 | f = __vxlan_find_mac(vxlan, all_zeros_mac); | |
1997 | if (f) | |
1998 | vxlan_fdb_destroy(vxlan, f); | |
1999 | spin_unlock_bh(&vxlan->hash_lock); | |
2000 | } | |
2001 | ||
ebf4063e SH |
2002 | static void vxlan_uninit(struct net_device *dev) |
2003 | { | |
2004 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
ebf4063e SH |
2005 | struct vxlan_sock *vs = vxlan->vn_sock; |
2006 | ||
ba609e9b | 2007 | vxlan_fdb_delete_default(vxlan); |
afbd8bae | 2008 | |
ebf4063e | 2009 | if (vs) |
012a5729 | 2010 | vxlan_sock_release(vs); |
ebf4063e SH |
2011 | free_percpu(dev->tstats); |
2012 | } | |
2013 | ||
d342894c | 2014 | /* Start ageing timer and join group when device is brought up */ |
2015 | static int vxlan_open(struct net_device *dev) | |
2016 | { | |
2017 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1c51a915 SH |
2018 | struct vxlan_sock *vs = vxlan->vn_sock; |
2019 | ||
2020 | /* socket hasn't been created */ | |
2021 | if (!vs) | |
2022 | return -ENOTCONN; | |
d342894c | 2023 | |
79d4a94f | 2024 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) { |
1c51a915 | 2025 | vxlan_sock_hold(vs); |
7c47cedf | 2026 | dev_hold(dev); |
3fc2de2f | 2027 | queue_work(vxlan_wq, &vxlan->igmp_join); |
d342894c | 2028 | } |
2029 | ||
2030 | if (vxlan->age_interval) | |
2031 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); | |
2032 | ||
2033 | return 0; | |
2034 | } | |
2035 | ||
2036 | /* Purge the forwarding table */ | |
2037 | static void vxlan_flush(struct vxlan_dev *vxlan) | |
2038 | { | |
31fec5aa | 2039 | unsigned int h; |
d342894c | 2040 | |
2041 | spin_lock_bh(&vxlan->hash_lock); | |
2042 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
2043 | struct hlist_node *p, *n; | |
2044 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { | |
2045 | struct vxlan_fdb *f | |
2046 | = container_of(p, struct vxlan_fdb, hlist); | |
afbd8bae MR |
2047 | /* the all_zeros_mac entry is deleted at vxlan_uninit */ |
2048 | if (!is_zero_ether_addr(f->eth_addr)) | |
2049 | vxlan_fdb_destroy(vxlan, f); | |
d342894c | 2050 | } |
2051 | } | |
2052 | spin_unlock_bh(&vxlan->hash_lock); | |
2053 | } | |
2054 | ||
2055 | /* Cleanup timer and forwarding table on shutdown */ | |
2056 | static int vxlan_stop(struct net_device *dev) | |
2057 | { | |
2058 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
f01ec1c0 | 2059 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
1c51a915 | 2060 | struct vxlan_sock *vs = vxlan->vn_sock; |
d342894c | 2061 | |
e4c7ed41 | 2062 | if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && |
95ab0991 | 2063 | !vxlan_group_used(vn, vxlan)) { |
1c51a915 | 2064 | vxlan_sock_hold(vs); |
7c47cedf | 2065 | dev_hold(dev); |
3fc2de2f | 2066 | queue_work(vxlan_wq, &vxlan->igmp_leave); |
7c47cedf | 2067 | } |
d342894c | 2068 | |
2069 | del_timer_sync(&vxlan->age_timer); | |
2070 | ||
2071 | vxlan_flush(vxlan); | |
2072 | ||
2073 | return 0; | |
2074 | } | |
2075 | ||
d342894c | 2076 | /* Stub, nothing needs to be done. */ |
2077 | static void vxlan_set_multicast_list(struct net_device *dev) | |
2078 | { | |
2079 | } | |
2080 | ||
345010b5 DB |
2081 | static int vxlan_change_mtu(struct net_device *dev, int new_mtu) |
2082 | { | |
2083 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
2084 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
2085 | struct net_device *lowerdev; | |
2086 | int max_mtu; | |
2087 | ||
f01ec1c0 | 2088 | lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex); |
345010b5 DB |
2089 | if (lowerdev == NULL) |
2090 | return eth_change_mtu(dev, new_mtu); | |
2091 | ||
2092 | if (dst->remote_ip.sa.sa_family == AF_INET6) | |
2093 | max_mtu = lowerdev->mtu - VXLAN6_HEADROOM; | |
2094 | else | |
2095 | max_mtu = lowerdev->mtu - VXLAN_HEADROOM; | |
2096 | ||
2097 | if (new_mtu < 68 || new_mtu > max_mtu) | |
2098 | return -EINVAL; | |
2099 | ||
2100 | dev->mtu = new_mtu; | |
2101 | return 0; | |
2102 | } | |
2103 | ||
d342894c | 2104 | static const struct net_device_ops vxlan_netdev_ops = { |
2105 | .ndo_init = vxlan_init, | |
ebf4063e | 2106 | .ndo_uninit = vxlan_uninit, |
d342894c | 2107 | .ndo_open = vxlan_open, |
2108 | .ndo_stop = vxlan_stop, | |
2109 | .ndo_start_xmit = vxlan_xmit, | |
e8171045 | 2110 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
d342894c | 2111 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
345010b5 | 2112 | .ndo_change_mtu = vxlan_change_mtu, |
d342894c | 2113 | .ndo_validate_addr = eth_validate_addr, |
2114 | .ndo_set_mac_address = eth_mac_addr, | |
2115 | .ndo_fdb_add = vxlan_fdb_add, | |
2116 | .ndo_fdb_del = vxlan_fdb_delete, | |
2117 | .ndo_fdb_dump = vxlan_fdb_dump, | |
2118 | }; | |
2119 | ||
2120 | /* Info for udev, that this is a virtual tunnel endpoint */ | |
2121 | static struct device_type vxlan_type = { | |
2122 | .name = "vxlan", | |
2123 | }; | |
2124 | ||
53cf5275 | 2125 | /* Calls the ndo_add_vxlan_port of the caller in order to |
35e42379 JG |
2126 | * supply the listening VXLAN udp ports. Callers are expected |
2127 | * to implement the ndo_add_vxlan_port. | |
53cf5275 JG |
2128 | */ |
2129 | void vxlan_get_rx_port(struct net_device *dev) | |
2130 | { | |
2131 | struct vxlan_sock *vs; | |
2132 | struct net *net = dev_net(dev); | |
2133 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2134 | sa_family_t sa_family; | |
35e42379 JG |
2135 | __be16 port; |
2136 | unsigned int i; | |
53cf5275 JG |
2137 | |
2138 | spin_lock(&vn->sock_lock); | |
2139 | for (i = 0; i < PORT_HASH_SIZE; ++i) { | |
35e42379 JG |
2140 | hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) { |
2141 | port = inet_sk(vs->sock->sk)->inet_sport; | |
53cf5275 JG |
2142 | sa_family = vs->sock->sk->sk_family; |
2143 | dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family, | |
2144 | port); | |
2145 | } | |
2146 | } | |
2147 | spin_unlock(&vn->sock_lock); | |
2148 | } | |
2149 | EXPORT_SYMBOL_GPL(vxlan_get_rx_port); | |
2150 | ||
d342894c | 2151 | /* Initialize the device structure. */ |
2152 | static void vxlan_setup(struct net_device *dev) | |
2153 | { | |
2154 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
31fec5aa | 2155 | unsigned int h; |
d342894c | 2156 | |
2157 | eth_hw_addr_random(dev); | |
2158 | ether_setup(dev); | |
e4c7ed41 | 2159 | if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6) |
2853af6a | 2160 | dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM; |
e4c7ed41 | 2161 | else |
2853af6a | 2162 | dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM; |
d342894c | 2163 | |
2164 | dev->netdev_ops = &vxlan_netdev_ops; | |
ebf4063e | 2165 | dev->destructor = free_netdev; |
d342894c | 2166 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
2167 | ||
2168 | dev->tx_queue_len = 0; | |
2169 | dev->features |= NETIF_F_LLTX; | |
d6727fe3 | 2170 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
0afb1666 | 2171 | dev->features |= NETIF_F_RXCSUM; |
05c0db08 | 2172 | dev->features |= NETIF_F_GSO_SOFTWARE; |
0afb1666 | 2173 | |
1eaa8178 PS |
2174 | dev->vlan_features = dev->features; |
2175 | dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; | |
0afb1666 | 2176 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
05c0db08 | 2177 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
1eaa8178 | 2178 | dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; |
02875878 | 2179 | netif_keep_dst(dev); |
6602d007 | 2180 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
d342894c | 2181 | |
553675fb | 2182 | INIT_LIST_HEAD(&vxlan->next); |
d342894c | 2183 | spin_lock_init(&vxlan->hash_lock); |
3fc2de2f | 2184 | INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join); |
2185 | INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave); | |
1c51a915 | 2186 | INIT_WORK(&vxlan->sock_work, vxlan_sock_work); |
d342894c | 2187 | |
2188 | init_timer_deferrable(&vxlan->age_timer); | |
2189 | vxlan->age_timer.function = vxlan_cleanup; | |
2190 | vxlan->age_timer.data = (unsigned long) vxlan; | |
2191 | ||
823aa873 | 2192 | vxlan->dst_port = htons(vxlan_port); |
05f47d69 | 2193 | |
d342894c | 2194 | vxlan->dev = dev; |
2195 | ||
2196 | for (h = 0; h < FDB_HASH_SIZE; ++h) | |
2197 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); | |
2198 | } | |
2199 | ||
2200 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { | |
2201 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, | |
5d174dd8 | 2202 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
e4c7ed41 | 2203 | [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) }, |
d342894c | 2204 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
2205 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, | |
e4c7ed41 | 2206 | [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) }, |
d342894c | 2207 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
2208 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, | |
2209 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, | |
2210 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, | |
2211 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, | |
05f47d69 | 2212 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
e4f67add DS |
2213 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
2214 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, | |
2215 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, | |
2216 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, | |
823aa873 | 2217 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
5c91ae08 TH |
2218 | [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 }, |
2219 | [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, | |
2220 | [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, | |
d342894c | 2221 | }; |
2222 | ||
2223 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) | |
2224 | { | |
2225 | if (tb[IFLA_ADDRESS]) { | |
2226 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { | |
2227 | pr_debug("invalid link address (not ethernet)\n"); | |
2228 | return -EINVAL; | |
2229 | } | |
2230 | ||
2231 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { | |
2232 | pr_debug("invalid all zero ethernet address\n"); | |
2233 | return -EADDRNOTAVAIL; | |
2234 | } | |
2235 | } | |
2236 | ||
2237 | if (!data) | |
2238 | return -EINVAL; | |
2239 | ||
2240 | if (data[IFLA_VXLAN_ID]) { | |
2241 | __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); | |
2242 | if (id >= VXLAN_VID_MASK) | |
2243 | return -ERANGE; | |
2244 | } | |
2245 | ||
05f47d69 | 2246 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
2247 | const struct ifla_vxlan_port_range *p | |
2248 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
2249 | ||
2250 | if (ntohs(p->high) < ntohs(p->low)) { | |
2251 | pr_debug("port range %u .. %u not valid\n", | |
2252 | ntohs(p->low), ntohs(p->high)); | |
2253 | return -EINVAL; | |
2254 | } | |
2255 | } | |
2256 | ||
d342894c | 2257 | return 0; |
2258 | } | |
2259 | ||
1b13c97f YB |
2260 | static void vxlan_get_drvinfo(struct net_device *netdev, |
2261 | struct ethtool_drvinfo *drvinfo) | |
2262 | { | |
2263 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); | |
2264 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); | |
2265 | } | |
2266 | ||
2267 | static const struct ethtool_ops vxlan_ethtool_ops = { | |
2268 | .get_drvinfo = vxlan_get_drvinfo, | |
2269 | .get_link = ethtool_op_get_link, | |
2270 | }; | |
2271 | ||
553675fb | 2272 | static void vxlan_del_work(struct work_struct *work) |
2273 | { | |
2274 | struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work); | |
acbf74a7 | 2275 | udp_tunnel_sock_release(vs->sock); |
553675fb | 2276 | kfree_rcu(vs, rcu); |
2277 | } | |
2278 | ||
3ee64f39 TH |
2279 | static struct socket *vxlan_create_sock(struct net *net, bool ipv6, |
2280 | __be16 port, u32 flags) | |
553675fb | 2281 | { |
e4c7ed41 | 2282 | struct socket *sock; |
3ee64f39 TH |
2283 | struct udp_port_cfg udp_conf; |
2284 | int err; | |
e4c7ed41 | 2285 | |
3ee64f39 | 2286 | memset(&udp_conf, 0, sizeof(udp_conf)); |
e4c7ed41 | 2287 | |
3ee64f39 TH |
2288 | if (ipv6) { |
2289 | udp_conf.family = AF_INET6; | |
2290 | udp_conf.use_udp6_tx_checksums = | |
3dc2b6a8 | 2291 | !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX); |
3ee64f39 | 2292 | udp_conf.use_udp6_rx_checksums = |
3dc2b6a8 | 2293 | !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX); |
3ee64f39 TH |
2294 | } else { |
2295 | udp_conf.family = AF_INET; | |
2296 | udp_conf.local_ip.s_addr = INADDR_ANY; | |
2297 | udp_conf.use_udp_checksums = | |
2298 | !!(flags & VXLAN_F_UDP_CSUM); | |
e4c7ed41 | 2299 | } |
e4c7ed41 | 2300 | |
3ee64f39 | 2301 | udp_conf.local_udp_port = port; |
553675fb | 2302 | |
3ee64f39 TH |
2303 | /* Open UDP socket */ |
2304 | err = udp_sock_create(net, &udp_conf, &sock); | |
2305 | if (err < 0) | |
2306 | return ERR_PTR(err); | |
e4c7ed41 | 2307 | |
39deb2c7 | 2308 | return sock; |
e4c7ed41 CW |
2309 | } |
2310 | ||
2311 | /* Create new listen socket if needed */ | |
2312 | static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port, | |
359a0ea9 TH |
2313 | vxlan_rcv_t *rcv, void *data, |
2314 | u32 flags) | |
e4c7ed41 CW |
2315 | { |
2316 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2317 | struct vxlan_sock *vs; | |
2318 | struct socket *sock; | |
e4c7ed41 | 2319 | unsigned int h; |
359a0ea9 | 2320 | bool ipv6 = !!(flags & VXLAN_F_IPV6); |
acbf74a7 | 2321 | struct udp_tunnel_sock_cfg tunnel_cfg; |
e4c7ed41 | 2322 | |
dc01e7d3 | 2323 | vs = kzalloc(sizeof(*vs), GFP_KERNEL); |
e4c7ed41 CW |
2324 | if (!vs) |
2325 | return ERR_PTR(-ENOMEM); | |
2326 | ||
2327 | for (h = 0; h < VNI_HASH_SIZE; ++h) | |
2328 | INIT_HLIST_HEAD(&vs->vni_list[h]); | |
2329 | ||
2330 | INIT_WORK(&vs->del_work, vxlan_del_work); | |
2331 | ||
3ee64f39 | 2332 | sock = vxlan_create_sock(net, ipv6, port, flags); |
39deb2c7 | 2333 | if (IS_ERR(sock)) { |
553675fb | 2334 | kfree(vs); |
e50fddc8 | 2335 | return ERR_CAST(sock); |
553675fb | 2336 | } |
e4c7ed41 CW |
2337 | |
2338 | vs->sock = sock; | |
9c2e24e1 | 2339 | atomic_set(&vs->refcnt, 1); |
5cfccc5a | 2340 | vs->rcv = rcv; |
012a5729 | 2341 | vs->data = data; |
553675fb | 2342 | |
dc01e7d3 OG |
2343 | /* Initialize the vxlan udp offloads structure */ |
2344 | vs->udp_offloads.port = port; | |
2345 | vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive; | |
2346 | vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete; | |
2347 | ||
9c2e24e1 PS |
2348 | spin_lock(&vn->sock_lock); |
2349 | hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); | |
dc01e7d3 | 2350 | vxlan_notify_add_rx_port(vs); |
9c2e24e1 | 2351 | spin_unlock(&vn->sock_lock); |
553675fb | 2352 | |
2353 | /* Mark socket as an encapsulation socket. */ | |
acbf74a7 AZ |
2354 | tunnel_cfg.sk_user_data = vs; |
2355 | tunnel_cfg.encap_type = 1; | |
2356 | tunnel_cfg.encap_rcv = vxlan_udp_encap_recv; | |
2357 | tunnel_cfg.encap_destroy = NULL; | |
2358 | ||
2359 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); | |
e4c7ed41 | 2360 | |
9c2e24e1 PS |
2361 | return vs; |
2362 | } | |
2363 | ||
012a5729 PS |
2364 | struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port, |
2365 | vxlan_rcv_t *rcv, void *data, | |
359a0ea9 | 2366 | bool no_share, u32 flags) |
9c2e24e1 PS |
2367 | { |
2368 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2369 | struct vxlan_sock *vs; | |
19ca9fc1 | 2370 | bool ipv6 = flags & VXLAN_F_IPV6; |
9c2e24e1 | 2371 | |
359a0ea9 | 2372 | vs = vxlan_socket_create(net, port, rcv, data, flags); |
9c2e24e1 PS |
2373 | if (!IS_ERR(vs)) |
2374 | return vs; | |
553675fb | 2375 | |
012a5729 PS |
2376 | if (no_share) /* Return error if sharing is not allowed. */ |
2377 | return vs; | |
2378 | ||
9c2e24e1 | 2379 | spin_lock(&vn->sock_lock); |
19ca9fc1 | 2380 | vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port); |
00c83b01 ML |
2381 | if (vs && ((vs->rcv != rcv) || |
2382 | !atomic_add_unless(&vs->refcnt, 1, 0))) | |
5cfccc5a | 2383 | vs = ERR_PTR(-EBUSY); |
5cfccc5a PS |
2384 | spin_unlock(&vn->sock_lock); |
2385 | ||
2386 | if (!vs) | |
9c2e24e1 PS |
2387 | vs = ERR_PTR(-EINVAL); |
2388 | ||
553675fb | 2389 | return vs; |
2390 | } | |
012a5729 | 2391 | EXPORT_SYMBOL_GPL(vxlan_sock_add); |
553675fb | 2392 | |
1c51a915 SH |
2393 | /* Scheduled at device creation to bind to a socket */ |
2394 | static void vxlan_sock_work(struct work_struct *work) | |
2395 | { | |
9c2e24e1 | 2396 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work); |
f01ec1c0 | 2397 | struct net *net = vxlan->net; |
1c51a915 | 2398 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
9c2e24e1 PS |
2399 | __be16 port = vxlan->dst_port; |
2400 | struct vxlan_sock *nvs; | |
1c51a915 | 2401 | |
359a0ea9 | 2402 | nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags); |
1c51a915 | 2403 | spin_lock(&vn->sock_lock); |
9c2e24e1 PS |
2404 | if (!IS_ERR(nvs)) |
2405 | vxlan_vs_add_dev(nvs, vxlan); | |
2406 | spin_unlock(&vn->sock_lock); | |
2407 | ||
2408 | dev_put(vxlan->dev); | |
1c51a915 SH |
2409 | } |
2410 | ||
d342894c | 2411 | static int vxlan_newlink(struct net *net, struct net_device *dev, |
2412 | struct nlattr *tb[], struct nlattr *data[]) | |
2413 | { | |
553675fb | 2414 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
d342894c | 2415 | struct vxlan_dev *vxlan = netdev_priv(dev); |
c7995c43 | 2416 | struct vxlan_rdst *dst = &vxlan->default_dst; |
d342894c | 2417 | __u32 vni; |
2418 | int err; | |
e4c7ed41 | 2419 | bool use_ipv6 = false; |
d342894c | 2420 | |
2421 | if (!data[IFLA_VXLAN_ID]) | |
2422 | return -EINVAL; | |
2423 | ||
f01ec1c0 ND |
2424 | vxlan->net = dev_net(dev); |
2425 | ||
d342894c | 2426 | vni = nla_get_u32(data[IFLA_VXLAN_ID]); |
c7995c43 | 2427 | dst->remote_vni = vni; |
d342894c | 2428 | |
5933a7bb MR |
2429 | /* Unless IPv6 is explicitly requested, assume IPv4 */ |
2430 | dst->remote_ip.sa.sa_family = AF_INET; | |
e4c7ed41 CW |
2431 | if (data[IFLA_VXLAN_GROUP]) { |
2432 | dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]); | |
e4c7ed41 CW |
2433 | } else if (data[IFLA_VXLAN_GROUP6]) { |
2434 | if (!IS_ENABLED(CONFIG_IPV6)) | |
2435 | return -EPFNOSUPPORT; | |
2436 | ||
2437 | nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6], | |
2438 | sizeof(struct in6_addr)); | |
2439 | dst->remote_ip.sa.sa_family = AF_INET6; | |
2440 | use_ipv6 = true; | |
2441 | } | |
d342894c | 2442 | |
e4c7ed41 CW |
2443 | if (data[IFLA_VXLAN_LOCAL]) { |
2444 | vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); | |
2445 | vxlan->saddr.sa.sa_family = AF_INET; | |
2446 | } else if (data[IFLA_VXLAN_LOCAL6]) { | |
2447 | if (!IS_ENABLED(CONFIG_IPV6)) | |
2448 | return -EPFNOSUPPORT; | |
2449 | ||
2450 | /* TODO: respect scope id */ | |
2451 | nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6], | |
2452 | sizeof(struct in6_addr)); | |
2453 | vxlan->saddr.sa.sa_family = AF_INET6; | |
2454 | use_ipv6 = true; | |
2455 | } | |
d342894c | 2456 | |
34e02aa1 | 2457 | if (data[IFLA_VXLAN_LINK] && |
c7995c43 | 2458 | (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) { |
34e02aa1 | 2459 | struct net_device *lowerdev |
c7995c43 | 2460 | = __dev_get_by_index(net, dst->remote_ifindex); |
34e02aa1 | 2461 | |
2462 | if (!lowerdev) { | |
c7995c43 | 2463 | pr_info("ifindex %d does not exist\n", dst->remote_ifindex); |
34e02aa1 | 2464 | return -ENODEV; |
2465 | } | |
d342894c | 2466 | |
e4c7ed41 CW |
2467 | #if IS_ENABLED(CONFIG_IPV6) |
2468 | if (use_ipv6) { | |
2469 | struct inet6_dev *idev = __in6_dev_get(lowerdev); | |
2470 | if (idev && idev->cnf.disable_ipv6) { | |
2471 | pr_info("IPv6 is disabled via sysctl\n"); | |
2472 | return -EPERM; | |
2473 | } | |
2474 | vxlan->flags |= VXLAN_F_IPV6; | |
2475 | } | |
2476 | #endif | |
2477 | ||
34e02aa1 | 2478 | if (!tb[IFLA_MTU]) |
e4c7ed41 | 2479 | dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); |
1ba56fb4 | 2480 | |
2853af6a | 2481 | dev->needed_headroom = lowerdev->hard_header_len + |
e4c7ed41 | 2482 | (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); |
7bda701e | 2483 | } else if (use_ipv6) |
2484 | vxlan->flags |= VXLAN_F_IPV6; | |
d342894c | 2485 | |
2486 | if (data[IFLA_VXLAN_TOS]) | |
2487 | vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); | |
2488 | ||
afb97186 VB |
2489 | if (data[IFLA_VXLAN_TTL]) |
2490 | vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); | |
2491 | ||
d342894c | 2492 | if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
e4f67add | 2493 | vxlan->flags |= VXLAN_F_LEARN; |
d342894c | 2494 | |
2495 | if (data[IFLA_VXLAN_AGEING]) | |
2496 | vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); | |
2497 | else | |
2498 | vxlan->age_interval = FDB_AGE_DEFAULT; | |
2499 | ||
e4f67add DS |
2500 | if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY])) |
2501 | vxlan->flags |= VXLAN_F_PROXY; | |
2502 | ||
2503 | if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC])) | |
2504 | vxlan->flags |= VXLAN_F_RSC; | |
2505 | ||
2506 | if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS])) | |
2507 | vxlan->flags |= VXLAN_F_L2MISS; | |
2508 | ||
2509 | if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS])) | |
2510 | vxlan->flags |= VXLAN_F_L3MISS; | |
2511 | ||
d342894c | 2512 | if (data[IFLA_VXLAN_LIMIT]) |
2513 | vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); | |
2514 | ||
05f47d69 | 2515 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
2516 | const struct ifla_vxlan_port_range *p | |
2517 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
2518 | vxlan->port_min = ntohs(p->low); | |
2519 | vxlan->port_max = ntohs(p->high); | |
2520 | } | |
2521 | ||
823aa873 | 2522 | if (data[IFLA_VXLAN_PORT]) |
2523 | vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); | |
2524 | ||
359a0ea9 TH |
2525 | if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM])) |
2526 | vxlan->flags |= VXLAN_F_UDP_CSUM; | |
2527 | ||
2528 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] && | |
2529 | nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX])) | |
2530 | vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX; | |
2531 | ||
2532 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] && | |
2533 | nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX])) | |
2534 | vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX; | |
2535 | ||
19ca9fc1 ML |
2536 | if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET, |
2537 | vxlan->dst_port)) { | |
553675fb | 2538 | pr_info("duplicate VNI %u\n", vni); |
2539 | return -EEXIST; | |
2540 | } | |
2541 | ||
7ad24ea4 | 2542 | dev->ethtool_ops = &vxlan_ethtool_ops; |
1b13c97f | 2543 | |
2936b6ab SS |
2544 | /* create an fdb entry for a valid default destination */ |
2545 | if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) { | |
2546 | err = vxlan_fdb_create(vxlan, all_zeros_mac, | |
2547 | &vxlan->default_dst.remote_ip, | |
2548 | NUD_REACHABLE|NUD_PERMANENT, | |
2549 | NLM_F_EXCL|NLM_F_CREATE, | |
2550 | vxlan->dst_port, | |
2551 | vxlan->default_dst.remote_vni, | |
2552 | vxlan->default_dst.remote_ifindex, | |
2553 | NTF_SELF); | |
2554 | if (err) | |
2555 | return err; | |
2556 | } | |
d342894c | 2557 | |
afbd8bae MR |
2558 | err = register_netdevice(dev); |
2559 | if (err) { | |
ba609e9b | 2560 | vxlan_fdb_delete_default(vxlan); |
afbd8bae MR |
2561 | return err; |
2562 | } | |
2563 | ||
553675fb | 2564 | list_add(&vxlan->next, &vn->vxlan_list); |
553675fb | 2565 | |
2566 | return 0; | |
d342894c | 2567 | } |
2568 | ||
2569 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) | |
2570 | { | |
2571 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
f01ec1c0 | 2572 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
d342894c | 2573 | |
fe5c3561 | 2574 | spin_lock(&vn->sock_lock); |
9c2e24e1 PS |
2575 | if (!hlist_unhashed(&vxlan->hlist)) |
2576 | hlist_del_rcu(&vxlan->hlist); | |
fe5c3561 | 2577 | spin_unlock(&vn->sock_lock); |
2578 | ||
553675fb | 2579 | list_del(&vxlan->next); |
d342894c | 2580 | unregister_netdevice_queue(dev, head); |
2581 | } | |
2582 | ||
2583 | static size_t vxlan_get_size(const struct net_device *dev) | |
2584 | { | |
2585 | ||
2586 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ | |
e4c7ed41 | 2587 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */ |
d342894c | 2588 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
e4c7ed41 | 2589 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */ |
d342894c | 2590 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
2591 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ | |
2592 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ | |
e4f67add DS |
2593 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
2594 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ | |
2595 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ | |
2596 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ | |
d342894c | 2597 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
2598 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ | |
05f47d69 | 2599 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
359a0ea9 TH |
2600 | nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */ |
2601 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */ | |
2602 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */ | |
2603 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */ | |
d342894c | 2604 | 0; |
2605 | } | |
2606 | ||
2607 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
2608 | { | |
2609 | const struct vxlan_dev *vxlan = netdev_priv(dev); | |
c7995c43 | 2610 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
05f47d69 | 2611 | struct ifla_vxlan_port_range ports = { |
2612 | .low = htons(vxlan->port_min), | |
2613 | .high = htons(vxlan->port_max), | |
2614 | }; | |
d342894c | 2615 | |
c7995c43 | 2616 | if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni)) |
d342894c | 2617 | goto nla_put_failure; |
2618 | ||
e4c7ed41 CW |
2619 | if (!vxlan_addr_any(&dst->remote_ip)) { |
2620 | if (dst->remote_ip.sa.sa_family == AF_INET) { | |
2621 | if (nla_put_be32(skb, IFLA_VXLAN_GROUP, | |
2622 | dst->remote_ip.sin.sin_addr.s_addr)) | |
2623 | goto nla_put_failure; | |
2624 | #if IS_ENABLED(CONFIG_IPV6) | |
2625 | } else { | |
2626 | if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr), | |
2627 | &dst->remote_ip.sin6.sin6_addr)) | |
2628 | goto nla_put_failure; | |
2629 | #endif | |
2630 | } | |
2631 | } | |
d342894c | 2632 | |
c7995c43 | 2633 | if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) |
d342894c | 2634 | goto nla_put_failure; |
2635 | ||
e4c7ed41 CW |
2636 | if (!vxlan_addr_any(&vxlan->saddr)) { |
2637 | if (vxlan->saddr.sa.sa_family == AF_INET) { | |
2638 | if (nla_put_be32(skb, IFLA_VXLAN_LOCAL, | |
2639 | vxlan->saddr.sin.sin_addr.s_addr)) | |
2640 | goto nla_put_failure; | |
2641 | #if IS_ENABLED(CONFIG_IPV6) | |
2642 | } else { | |
2643 | if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr), | |
2644 | &vxlan->saddr.sin6.sin6_addr)) | |
2645 | goto nla_put_failure; | |
2646 | #endif | |
2647 | } | |
2648 | } | |
d342894c | 2649 | |
2650 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) || | |
2651 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) || | |
e4f67add DS |
2652 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
2653 | !!(vxlan->flags & VXLAN_F_LEARN)) || | |
2654 | nla_put_u8(skb, IFLA_VXLAN_PROXY, | |
2655 | !!(vxlan->flags & VXLAN_F_PROXY)) || | |
2656 | nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) || | |
2657 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, | |
2658 | !!(vxlan->flags & VXLAN_F_L2MISS)) || | |
2659 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, | |
2660 | !!(vxlan->flags & VXLAN_F_L3MISS)) || | |
d342894c | 2661 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) || |
823aa873 | 2662 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) || |
359a0ea9 TH |
2663 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) || |
2664 | nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM, | |
2665 | !!(vxlan->flags & VXLAN_F_UDP_CSUM)) || | |
2666 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, | |
2667 | !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) || | |
2668 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, | |
2669 | !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX))) | |
d342894c | 2670 | goto nla_put_failure; |
2671 | ||
05f47d69 | 2672 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
2673 | goto nla_put_failure; | |
2674 | ||
d342894c | 2675 | return 0; |
2676 | ||
2677 | nla_put_failure: | |
2678 | return -EMSGSIZE; | |
2679 | } | |
2680 | ||
2681 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { | |
2682 | .kind = "vxlan", | |
2683 | .maxtype = IFLA_VXLAN_MAX, | |
2684 | .policy = vxlan_policy, | |
2685 | .priv_size = sizeof(struct vxlan_dev), | |
2686 | .setup = vxlan_setup, | |
2687 | .validate = vxlan_validate, | |
2688 | .newlink = vxlan_newlink, | |
2689 | .dellink = vxlan_dellink, | |
2690 | .get_size = vxlan_get_size, | |
2691 | .fill_info = vxlan_fill_info, | |
2692 | }; | |
2693 | ||
acaf4e70 DB |
2694 | static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn, |
2695 | struct net_device *dev) | |
2696 | { | |
2697 | struct vxlan_dev *vxlan, *next; | |
2698 | LIST_HEAD(list_kill); | |
2699 | ||
2700 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { | |
2701 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
2702 | ||
2703 | /* In case we created vxlan device with carrier | |
2704 | * and we loose the carrier due to module unload | |
2705 | * we also need to remove vxlan device. In other | |
2706 | * cases, it's not necessary and remote_ifindex | |
2707 | * is 0 here, so no matches. | |
2708 | */ | |
2709 | if (dst->remote_ifindex == dev->ifindex) | |
2710 | vxlan_dellink(vxlan->dev, &list_kill); | |
2711 | } | |
2712 | ||
2713 | unregister_netdevice_many(&list_kill); | |
2714 | } | |
2715 | ||
2716 | static int vxlan_lowerdev_event(struct notifier_block *unused, | |
2717 | unsigned long event, void *ptr) | |
2718 | { | |
2719 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
783c1463 | 2720 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
acaf4e70 | 2721 | |
783c1463 | 2722 | if (event == NETDEV_UNREGISTER) |
acaf4e70 DB |
2723 | vxlan_handle_lowerdev_unregister(vn, dev); |
2724 | ||
2725 | return NOTIFY_DONE; | |
2726 | } | |
2727 | ||
2728 | static struct notifier_block vxlan_notifier_block __read_mostly = { | |
2729 | .notifier_call = vxlan_lowerdev_event, | |
2730 | }; | |
2731 | ||
d342894c | 2732 | static __net_init int vxlan_init_net(struct net *net) |
2733 | { | |
2734 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
31fec5aa | 2735 | unsigned int h; |
d342894c | 2736 | |
553675fb | 2737 | INIT_LIST_HEAD(&vn->vxlan_list); |
1c51a915 | 2738 | spin_lock_init(&vn->sock_lock); |
d342894c | 2739 | |
553675fb | 2740 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
2741 | INIT_HLIST_HEAD(&vn->sock_list[h]); | |
d342894c | 2742 | |
2743 | return 0; | |
2744 | } | |
2745 | ||
f01ec1c0 ND |
2746 | static void __net_exit vxlan_exit_net(struct net *net) |
2747 | { | |
2748 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2749 | struct vxlan_dev *vxlan, *next; | |
2750 | struct net_device *dev, *aux; | |
2751 | LIST_HEAD(list); | |
2752 | ||
2753 | rtnl_lock(); | |
2754 | for_each_netdev_safe(net, dev, aux) | |
2755 | if (dev->rtnl_link_ops == &vxlan_link_ops) | |
2756 | unregister_netdevice_queue(dev, &list); | |
2757 | ||
2758 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { | |
2759 | /* If vxlan->dev is in the same netns, it has already been added | |
2760 | * to the list by the previous loop. | |
2761 | */ | |
2762 | if (!net_eq(dev_net(vxlan->dev), net)) | |
2763 | unregister_netdevice_queue(dev, &list); | |
2764 | } | |
2765 | ||
2766 | unregister_netdevice_many(&list); | |
2767 | rtnl_unlock(); | |
2768 | } | |
2769 | ||
d342894c | 2770 | static struct pernet_operations vxlan_net_ops = { |
2771 | .init = vxlan_init_net, | |
f01ec1c0 | 2772 | .exit = vxlan_exit_net, |
d342894c | 2773 | .id = &vxlan_net_id, |
2774 | .size = sizeof(struct vxlan_net), | |
2775 | }; | |
2776 | ||
2777 | static int __init vxlan_init_module(void) | |
2778 | { | |
2779 | int rc; | |
2780 | ||
758c57d1 SH |
2781 | vxlan_wq = alloc_workqueue("vxlan", 0, 0); |
2782 | if (!vxlan_wq) | |
2783 | return -ENOMEM; | |
2784 | ||
d342894c | 2785 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); |
2786 | ||
783c1463 | 2787 | rc = register_pernet_subsys(&vxlan_net_ops); |
d342894c | 2788 | if (rc) |
2789 | goto out1; | |
2790 | ||
acaf4e70 | 2791 | rc = register_netdevice_notifier(&vxlan_notifier_block); |
d342894c | 2792 | if (rc) |
2793 | goto out2; | |
2794 | ||
acaf4e70 DB |
2795 | rc = rtnl_link_register(&vxlan_link_ops); |
2796 | if (rc) | |
2797 | goto out3; | |
d342894c | 2798 | |
acaf4e70 DB |
2799 | return 0; |
2800 | out3: | |
2801 | unregister_netdevice_notifier(&vxlan_notifier_block); | |
d342894c | 2802 | out2: |
783c1463 | 2803 | unregister_pernet_subsys(&vxlan_net_ops); |
d342894c | 2804 | out1: |
758c57d1 | 2805 | destroy_workqueue(vxlan_wq); |
d342894c | 2806 | return rc; |
2807 | } | |
7332a13b | 2808 | late_initcall(vxlan_init_module); |
d342894c | 2809 | |
2810 | static void __exit vxlan_cleanup_module(void) | |
2811 | { | |
b7153984 | 2812 | rtnl_link_unregister(&vxlan_link_ops); |
acaf4e70 | 2813 | unregister_netdevice_notifier(&vxlan_notifier_block); |
758c57d1 | 2814 | destroy_workqueue(vxlan_wq); |
783c1463 DB |
2815 | unregister_pernet_subsys(&vxlan_net_ops); |
2816 | /* rcu_barrier() is called by netns */ | |
d342894c | 2817 | } |
2818 | module_exit(vxlan_cleanup_module); | |
2819 | ||
2820 | MODULE_LICENSE("GPL"); | |
2821 | MODULE_VERSION(VXLAN_VERSION); | |
3b8df3c6 | 2822 | MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); |
ead5139a | 2823 | MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic"); |
d342894c | 2824 | MODULE_ALIAS_RTNL_LINK("vxlan"); |