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