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