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