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