]>
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 | ||
d342894c | 561 | /* Add new entry to forwarding table -- assumes lock held */ |
562 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, | |
e4c7ed41 | 563 | const u8 *mac, union vxlan_addr *ip, |
6681712d | 564 | __u16 state, __u16 flags, |
73cf3317 | 565 | __be16 port, __u32 vni, __u32 ifindex, |
ae884082 | 566 | __u8 ndm_flags) |
d342894c | 567 | { |
568 | struct vxlan_fdb *f; | |
569 | int notify = 0; | |
570 | ||
014be2c8 | 571 | f = __vxlan_find_mac(vxlan, mac); |
d342894c | 572 | if (f) { |
573 | if (flags & NLM_F_EXCL) { | |
574 | netdev_dbg(vxlan->dev, | |
575 | "lost race to create %pM\n", mac); | |
576 | return -EEXIST; | |
577 | } | |
578 | if (f->state != state) { | |
579 | f->state = state; | |
580 | f->updated = jiffies; | |
581 | notify = 1; | |
582 | } | |
ae884082 DS |
583 | if (f->flags != ndm_flags) { |
584 | f->flags = ndm_flags; | |
585 | f->updated = jiffies; | |
586 | notify = 1; | |
587 | } | |
906dc186 TR |
588 | if ((flags & NLM_F_REPLACE)) { |
589 | /* Only change unicasts */ | |
590 | if (!(is_multicast_ether_addr(f->eth_addr) || | |
591 | is_zero_ether_addr(f->eth_addr))) { | |
592 | int rc = vxlan_fdb_replace(f, ip, port, vni, | |
593 | ifindex); | |
594 | ||
595 | if (rc < 0) | |
596 | return rc; | |
597 | notify |= rc; | |
598 | } else | |
599 | return -EOPNOTSUPP; | |
600 | } | |
6681712d | 601 | if ((flags & NLM_F_APPEND) && |
58e4c767 MR |
602 | (is_multicast_ether_addr(f->eth_addr) || |
603 | is_zero_ether_addr(f->eth_addr))) { | |
6681712d DS |
604 | int rc = vxlan_fdb_append(f, ip, port, vni, ifindex); |
605 | ||
606 | if (rc < 0) | |
607 | return rc; | |
608 | notify |= rc; | |
609 | } | |
d342894c | 610 | } else { |
611 | if (!(flags & NLM_F_CREATE)) | |
612 | return -ENOENT; | |
613 | ||
614 | if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax) | |
615 | return -ENOSPC; | |
616 | ||
906dc186 TR |
617 | /* Disallow replace to add a multicast entry */ |
618 | if ((flags & NLM_F_REPLACE) && | |
619 | (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) | |
620 | return -EOPNOTSUPP; | |
621 | ||
e4c7ed41 | 622 | netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); |
d342894c | 623 | f = kmalloc(sizeof(*f), GFP_ATOMIC); |
624 | if (!f) | |
625 | return -ENOMEM; | |
626 | ||
627 | notify = 1; | |
d342894c | 628 | f->state = state; |
ae884082 | 629 | f->flags = ndm_flags; |
d342894c | 630 | f->updated = f->used = jiffies; |
3e61aa8f | 631 | INIT_LIST_HEAD(&f->remotes); |
d342894c | 632 | memcpy(f->eth_addr, mac, ETH_ALEN); |
633 | ||
3e61aa8f SH |
634 | vxlan_fdb_append(f, ip, port, vni, ifindex); |
635 | ||
d342894c | 636 | ++vxlan->addrcnt; |
637 | hlist_add_head_rcu(&f->hlist, | |
638 | vxlan_fdb_head(vxlan, mac)); | |
639 | } | |
640 | ||
641 | if (notify) | |
642 | vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); | |
643 | ||
644 | return 0; | |
645 | } | |
646 | ||
6706c82e | 647 | static void vxlan_fdb_free(struct rcu_head *head) |
6681712d DS |
648 | { |
649 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); | |
3e61aa8f | 650 | struct vxlan_rdst *rd, *nd; |
6681712d | 651 | |
3e61aa8f | 652 | list_for_each_entry_safe(rd, nd, &f->remotes, list) |
6681712d | 653 | kfree(rd); |
6681712d DS |
654 | kfree(f); |
655 | } | |
656 | ||
d342894c | 657 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f) |
658 | { | |
659 | netdev_dbg(vxlan->dev, | |
660 | "delete %pM\n", f->eth_addr); | |
661 | ||
662 | --vxlan->addrcnt; | |
663 | vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH); | |
664 | ||
665 | hlist_del_rcu(&f->hlist); | |
6681712d | 666 | call_rcu(&f->rcu, vxlan_fdb_free); |
d342894c | 667 | } |
668 | ||
f0b074be | 669 | static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, |
e4c7ed41 | 670 | union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex) |
d342894c | 671 | { |
6681712d | 672 | struct net *net = dev_net(vxlan->dev); |
e4c7ed41 | 673 | int err; |
d342894c | 674 | |
f0b074be | 675 | if (tb[NDA_DST]) { |
e4c7ed41 CW |
676 | err = vxlan_nla_get_addr(ip, tb[NDA_DST]); |
677 | if (err) | |
678 | return err; | |
f0b074be | 679 | } else { |
e4c7ed41 CW |
680 | union vxlan_addr *remote = &vxlan->default_dst.remote_ip; |
681 | if (remote->sa.sa_family == AF_INET) { | |
682 | ip->sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
683 | ip->sa.sa_family = AF_INET; | |
684 | #if IS_ENABLED(CONFIG_IPV6) | |
685 | } else { | |
686 | ip->sin6.sin6_addr = in6addr_any; | |
687 | ip->sa.sa_family = AF_INET6; | |
688 | #endif | |
689 | } | |
f0b074be | 690 | } |
d342894c | 691 | |
6681712d | 692 | if (tb[NDA_PORT]) { |
73cf3317 | 693 | if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) |
6681712d | 694 | return -EINVAL; |
f0b074be MR |
695 | *port = nla_get_be16(tb[NDA_PORT]); |
696 | } else { | |
697 | *port = vxlan->dst_port; | |
698 | } | |
6681712d DS |
699 | |
700 | if (tb[NDA_VNI]) { | |
701 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) | |
702 | return -EINVAL; | |
f0b074be MR |
703 | *vni = nla_get_u32(tb[NDA_VNI]); |
704 | } else { | |
705 | *vni = vxlan->default_dst.remote_vni; | |
706 | } | |
6681712d DS |
707 | |
708 | if (tb[NDA_IFINDEX]) { | |
5abb0029 | 709 | struct net_device *tdev; |
6681712d DS |
710 | |
711 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) | |
712 | return -EINVAL; | |
f0b074be MR |
713 | *ifindex = nla_get_u32(tb[NDA_IFINDEX]); |
714 | tdev = dev_get_by_index(net, *ifindex); | |
5abb0029 | 715 | if (!tdev) |
6681712d | 716 | return -EADDRNOTAVAIL; |
5abb0029 | 717 | dev_put(tdev); |
f0b074be MR |
718 | } else { |
719 | *ifindex = 0; | |
720 | } | |
721 | ||
722 | return 0; | |
723 | } | |
724 | ||
725 | /* Add static entry (via netlink) */ | |
726 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], | |
727 | struct net_device *dev, | |
728 | const unsigned char *addr, u16 flags) | |
729 | { | |
730 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
731 | /* struct net *net = dev_net(vxlan->dev); */ | |
e4c7ed41 | 732 | union vxlan_addr ip; |
f0b074be MR |
733 | __be16 port; |
734 | u32 vni, ifindex; | |
735 | int err; | |
736 | ||
737 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { | |
738 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", | |
739 | ndm->ndm_state); | |
740 | return -EINVAL; | |
741 | } | |
742 | ||
743 | if (tb[NDA_DST] == NULL) | |
744 | return -EINVAL; | |
745 | ||
746 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); | |
747 | if (err) | |
748 | return err; | |
6681712d | 749 | |
d342894c | 750 | spin_lock_bh(&vxlan->hash_lock); |
e4c7ed41 | 751 | err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags, |
73cf3317 | 752 | port, vni, ifindex, ndm->ndm_flags); |
d342894c | 753 | spin_unlock_bh(&vxlan->hash_lock); |
754 | ||
755 | return err; | |
756 | } | |
757 | ||
758 | /* Delete entry (via netlink) */ | |
1690be63 VY |
759 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], |
760 | struct net_device *dev, | |
d342894c | 761 | const unsigned char *addr) |
762 | { | |
763 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
764 | struct vxlan_fdb *f; | |
bc7892ba | 765 | struct vxlan_rdst *rd = NULL; |
e4c7ed41 | 766 | union vxlan_addr ip; |
bc7892ba MR |
767 | __be16 port; |
768 | u32 vni, ifindex; | |
769 | int err; | |
770 | ||
771 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex); | |
772 | if (err) | |
773 | return err; | |
774 | ||
775 | err = -ENOENT; | |
d342894c | 776 | |
777 | spin_lock_bh(&vxlan->hash_lock); | |
778 | f = vxlan_find_mac(vxlan, addr); | |
bc7892ba MR |
779 | if (!f) |
780 | goto out; | |
781 | ||
e4c7ed41 CW |
782 | if (!vxlan_addr_any(&ip)) { |
783 | rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex); | |
bc7892ba MR |
784 | if (!rd) |
785 | goto out; | |
786 | } | |
787 | ||
788 | err = 0; | |
789 | ||
790 | /* remove a destination if it's not the only one on the list, | |
791 | * otherwise destroy the fdb entry | |
792 | */ | |
793 | if (rd && !list_is_singular(&f->remotes)) { | |
794 | list_del_rcu(&rd->list); | |
dcdc7a59 | 795 | kfree_rcu(rd, rcu); |
bc7892ba | 796 | goto out; |
d342894c | 797 | } |
bc7892ba MR |
798 | |
799 | vxlan_fdb_destroy(vxlan, f); | |
800 | ||
801 | out: | |
d342894c | 802 | spin_unlock_bh(&vxlan->hash_lock); |
803 | ||
804 | return err; | |
805 | } | |
806 | ||
807 | /* Dump forwarding table */ | |
808 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, | |
809 | struct net_device *dev, int idx) | |
810 | { | |
811 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
812 | unsigned int h; | |
813 | ||
814 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
815 | struct vxlan_fdb *f; | |
d342894c | 816 | int err; |
817 | ||
b67bfe0d | 818 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
6681712d | 819 | struct vxlan_rdst *rd; |
6681712d | 820 | |
3e61aa8f SH |
821 | if (idx < cb->args[0]) |
822 | goto skip; | |
823 | ||
824 | list_for_each_entry_rcu(rd, &f->remotes, list) { | |
6681712d DS |
825 | err = vxlan_fdb_info(skb, vxlan, f, |
826 | NETLINK_CB(cb->skb).portid, | |
827 | cb->nlh->nlmsg_seq, | |
828 | RTM_NEWNEIGH, | |
829 | NLM_F_MULTI, rd); | |
830 | if (err < 0) | |
3e61aa8f | 831 | goto out; |
6681712d | 832 | } |
3e61aa8f SH |
833 | skip: |
834 | ++idx; | |
d342894c | 835 | } |
836 | } | |
3e61aa8f | 837 | out: |
d342894c | 838 | return idx; |
839 | } | |
840 | ||
841 | /* Watch incoming packets to learn mapping between Ethernet address | |
842 | * and Tunnel endpoint. | |
26a41ae6 | 843 | * Return true if packet is bogus and should be droppped. |
d342894c | 844 | */ |
26a41ae6 | 845 | static bool vxlan_snoop(struct net_device *dev, |
e4c7ed41 | 846 | union vxlan_addr *src_ip, const u8 *src_mac) |
d342894c | 847 | { |
848 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
849 | struct vxlan_fdb *f; | |
d342894c | 850 | |
851 | f = vxlan_find_mac(vxlan, src_mac); | |
852 | if (likely(f)) { | |
5ca5461c | 853 | struct vxlan_rdst *rdst = first_remote_rcu(f); |
3e61aa8f | 854 | |
e4c7ed41 | 855 | if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip))) |
26a41ae6 | 856 | return false; |
857 | ||
858 | /* Don't migrate static entries, drop packets */ | |
eb064c3b | 859 | if (f->state & NUD_NOARP) |
26a41ae6 | 860 | return true; |
d342894c | 861 | |
862 | if (net_ratelimit()) | |
863 | netdev_info(dev, | |
e4c7ed41 | 864 | "%pM migrated from %pIS to %pIS\n", |
3e61aa8f | 865 | src_mac, &rdst->remote_ip, &src_ip); |
d342894c | 866 | |
e4c7ed41 | 867 | rdst->remote_ip = *src_ip; |
d342894c | 868 | f->updated = jiffies; |
8385f50a | 869 | vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH); |
d342894c | 870 | } else { |
871 | /* learned new entry */ | |
872 | spin_lock(&vxlan->hash_lock); | |
3bf74b1a | 873 | |
874 | /* close off race between vxlan_flush and incoming packets */ | |
875 | if (netif_running(dev)) | |
876 | vxlan_fdb_create(vxlan, src_mac, src_ip, | |
877 | NUD_REACHABLE, | |
878 | NLM_F_EXCL|NLM_F_CREATE, | |
879 | vxlan->dst_port, | |
880 | vxlan->default_dst.remote_vni, | |
881 | 0, NTF_SELF); | |
d342894c | 882 | spin_unlock(&vxlan->hash_lock); |
883 | } | |
26a41ae6 | 884 | |
885 | return false; | |
d342894c | 886 | } |
887 | ||
d342894c | 888 | /* See if multicast group is already in use by other ID */ |
e4c7ed41 | 889 | static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip) |
d342894c | 890 | { |
553675fb | 891 | struct vxlan_dev *vxlan; |
d342894c | 892 | |
553675fb | 893 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
553675fb | 894 | if (!netif_running(vxlan->dev)) |
895 | continue; | |
d342894c | 896 | |
e4c7ed41 CW |
897 | if (vxlan_addr_equal(&vxlan->default_dst.remote_ip, |
898 | remote_ip)) | |
553675fb | 899 | return true; |
900 | } | |
d342894c | 901 | |
902 | return false; | |
903 | } | |
904 | ||
7c47cedf | 905 | static void vxlan_sock_hold(struct vxlan_sock *vs) |
d342894c | 906 | { |
7c47cedf SH |
907 | atomic_inc(&vs->refcnt); |
908 | } | |
d342894c | 909 | |
012a5729 | 910 | void vxlan_sock_release(struct vxlan_sock *vs) |
7c47cedf | 911 | { |
012a5729 PS |
912 | struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id); |
913 | ||
7c47cedf SH |
914 | if (!atomic_dec_and_test(&vs->refcnt)) |
915 | return; | |
d342894c | 916 | |
1c51a915 | 917 | spin_lock(&vn->sock_lock); |
7c47cedf | 918 | hlist_del_rcu(&vs->hlist); |
430eda6d PS |
919 | smp_wmb(); |
920 | vs->sock->sk->sk_user_data = NULL; | |
1c51a915 SH |
921 | spin_unlock(&vn->sock_lock); |
922 | ||
7c47cedf | 923 | queue_work(vxlan_wq, &vs->del_work); |
d342894c | 924 | } |
012a5729 | 925 | EXPORT_SYMBOL_GPL(vxlan_sock_release); |
d342894c | 926 | |
3fc2de2f | 927 | /* Callback to update multicast group membership when first VNI on |
928 | * multicast asddress is brought up | |
929 | * Done as workqueue because ip_mc_join_group acquires RTNL. | |
7c47cedf | 930 | */ |
3fc2de2f | 931 | static void vxlan_igmp_join(struct work_struct *work) |
d342894c | 932 | { |
3fc2de2f | 933 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join); |
7c47cedf SH |
934 | struct vxlan_sock *vs = vxlan->vn_sock; |
935 | struct sock *sk = vs->sock->sk; | |
e4c7ed41 CW |
936 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
937 | int ifindex = vxlan->default_dst.remote_ifindex; | |
d342894c | 938 | |
d342894c | 939 | lock_sock(sk); |
e4c7ed41 CW |
940 | if (ip->sa.sa_family == AF_INET) { |
941 | struct ip_mreqn mreq = { | |
942 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, | |
943 | .imr_ifindex = ifindex, | |
944 | }; | |
945 | ||
946 | ip_mc_join_group(sk, &mreq); | |
947 | #if IS_ENABLED(CONFIG_IPV6) | |
948 | } else { | |
949 | ipv6_stub->ipv6_sock_mc_join(sk, ifindex, | |
950 | &ip->sin6.sin6_addr); | |
951 | #endif | |
952 | } | |
3fc2de2f | 953 | release_sock(sk); |
954 | ||
012a5729 | 955 | vxlan_sock_release(vs); |
3fc2de2f | 956 | dev_put(vxlan->dev); |
957 | } | |
958 | ||
959 | /* Inverse of vxlan_igmp_join when last VNI is brought down */ | |
960 | static void vxlan_igmp_leave(struct work_struct *work) | |
961 | { | |
962 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave); | |
3fc2de2f | 963 | struct vxlan_sock *vs = vxlan->vn_sock; |
964 | struct sock *sk = vs->sock->sk; | |
e4c7ed41 CW |
965 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
966 | int ifindex = vxlan->default_dst.remote_ifindex; | |
3fc2de2f | 967 | |
968 | lock_sock(sk); | |
e4c7ed41 CW |
969 | if (ip->sa.sa_family == AF_INET) { |
970 | struct ip_mreqn mreq = { | |
971 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, | |
972 | .imr_ifindex = ifindex, | |
973 | }; | |
974 | ||
975 | ip_mc_leave_group(sk, &mreq); | |
976 | #if IS_ENABLED(CONFIG_IPV6) | |
977 | } else { | |
978 | ipv6_stub->ipv6_sock_mc_drop(sk, ifindex, | |
979 | &ip->sin6.sin6_addr); | |
980 | #endif | |
981 | } | |
982 | ||
d342894c | 983 | release_sock(sk); |
d342894c | 984 | |
012a5729 | 985 | vxlan_sock_release(vs); |
7c47cedf | 986 | dev_put(vxlan->dev); |
d342894c | 987 | } |
988 | ||
989 | /* Callback from net/ipv4/udp.c to receive packets */ | |
990 | static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) | |
991 | { | |
5cfccc5a | 992 | struct vxlan_sock *vs; |
d342894c | 993 | struct vxlanhdr *vxh; |
553675fb | 994 | __be16 port; |
d342894c | 995 | |
d342894c | 996 | /* Need Vxlan and inner Ethernet header to be present */ |
7ce04758 | 997 | if (!pskb_may_pull(skb, VXLAN_HLEN)) |
d342894c | 998 | goto error; |
999 | ||
7ce04758 PS |
1000 | /* Return packets with reserved bits set */ |
1001 | vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1); | |
d342894c | 1002 | if (vxh->vx_flags != htonl(VXLAN_FLAGS) || |
1003 | (vxh->vx_vni & htonl(0xff))) { | |
1004 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", | |
1005 | ntohl(vxh->vx_flags), ntohl(vxh->vx_vni)); | |
1006 | goto error; | |
1007 | } | |
1008 | ||
5cfccc5a PS |
1009 | if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB))) |
1010 | goto drop; | |
1011 | ||
553675fb | 1012 | port = inet_sk(sk)->inet_sport; |
5cfccc5a | 1013 | |
430eda6d PS |
1014 | smp_read_barrier_depends(); |
1015 | vs = (struct vxlan_sock *)sk->sk_user_data; | |
5cfccc5a | 1016 | if (!vs) |
d342894c | 1017 | goto drop; |
d342894c | 1018 | |
5cfccc5a PS |
1019 | vs->rcv(vs, skb, vxh->vx_vni); |
1020 | return 0; | |
1021 | ||
1022 | drop: | |
1023 | /* Consume bad packet */ | |
1024 | kfree_skb(skb); | |
1025 | return 0; | |
1026 | ||
1027 | error: | |
1028 | /* Return non vxlan pkt */ | |
1029 | return 1; | |
1030 | } | |
1031 | ||
1032 | static void vxlan_rcv(struct vxlan_sock *vs, | |
1033 | struct sk_buff *skb, __be32 vx_vni) | |
1034 | { | |
e4c7ed41 CW |
1035 | struct iphdr *oip = NULL; |
1036 | struct ipv6hdr *oip6 = NULL; | |
5cfccc5a PS |
1037 | struct vxlan_dev *vxlan; |
1038 | struct pcpu_tstats *stats; | |
e4c7ed41 | 1039 | union vxlan_addr saddr; |
5cfccc5a | 1040 | __u32 vni; |
e4c7ed41 CW |
1041 | int err = 0; |
1042 | union vxlan_addr *remote_ip; | |
5cfccc5a PS |
1043 | |
1044 | vni = ntohl(vx_vni) >> 8; | |
1045 | /* Is this VNI defined? */ | |
1046 | vxlan = vxlan_vs_find_vni(vs, vni); | |
1047 | if (!vxlan) | |
d342894c | 1048 | goto drop; |
d342894c | 1049 | |
e4c7ed41 | 1050 | remote_ip = &vxlan->default_dst.remote_ip; |
e4f67add | 1051 | skb_reset_mac_header(skb); |
d342894c | 1052 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
d342894c | 1053 | |
1054 | /* Ignore packet loops (and multicast echo) */ | |
7367d0b5 | 1055 | if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr)) |
d342894c | 1056 | goto drop; |
1057 | ||
7ce04758 | 1058 | /* Re-examine inner Ethernet packet */ |
e4c7ed41 CW |
1059 | if (remote_ip->sa.sa_family == AF_INET) { |
1060 | oip = ip_hdr(skb); | |
1061 | saddr.sin.sin_addr.s_addr = oip->saddr; | |
1062 | saddr.sa.sa_family = AF_INET; | |
1063 | #if IS_ENABLED(CONFIG_IPV6) | |
1064 | } else { | |
1065 | oip6 = ipv6_hdr(skb); | |
1066 | saddr.sin6.sin6_addr = oip6->saddr; | |
1067 | saddr.sa.sa_family = AF_INET6; | |
1068 | #endif | |
1069 | } | |
1070 | ||
26a41ae6 | 1071 | if ((vxlan->flags & VXLAN_F_LEARN) && |
e4c7ed41 | 1072 | vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source)) |
26a41ae6 | 1073 | goto drop; |
d342894c | 1074 | |
d342894c | 1075 | skb_reset_network_header(skb); |
0afb1666 JG |
1076 | |
1077 | /* If the NIC driver gave us an encapsulated packet with | |
1078 | * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled, | |
1079 | * leave the CHECKSUM_UNNECESSARY, the device checksummed it | |
1080 | * for us. Otherwise force the upper layers to verify it. | |
1081 | */ | |
1082 | if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation || | |
1083 | !(vxlan->dev->features & NETIF_F_RXCSUM)) | |
1084 | skb->ip_summed = CHECKSUM_NONE; | |
1085 | ||
1086 | skb->encapsulation = 0; | |
d342894c | 1087 | |
e4c7ed41 CW |
1088 | if (oip6) |
1089 | err = IP6_ECN_decapsulate(oip6, skb); | |
1090 | if (oip) | |
1091 | err = IP_ECN_decapsulate(oip, skb); | |
1092 | ||
d342894c | 1093 | if (unlikely(err)) { |
e4c7ed41 CW |
1094 | if (log_ecn_error) { |
1095 | if (oip6) | |
1096 | net_info_ratelimited("non-ECT from %pI6\n", | |
1097 | &oip6->saddr); | |
1098 | if (oip) | |
1099 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", | |
1100 | &oip->saddr, oip->tos); | |
1101 | } | |
d342894c | 1102 | if (err > 1) { |
1103 | ++vxlan->dev->stats.rx_frame_errors; | |
1104 | ++vxlan->dev->stats.rx_errors; | |
1105 | goto drop; | |
1106 | } | |
1107 | } | |
1108 | ||
e8171045 | 1109 | stats = this_cpu_ptr(vxlan->dev->tstats); |
d342894c | 1110 | u64_stats_update_begin(&stats->syncp); |
1111 | stats->rx_packets++; | |
1112 | stats->rx_bytes += skb->len; | |
1113 | u64_stats_update_end(&stats->syncp); | |
1114 | ||
1115 | netif_rx(skb); | |
1116 | ||
5cfccc5a | 1117 | return; |
d342894c | 1118 | drop: |
1119 | /* Consume bad packet */ | |
1120 | kfree_skb(skb); | |
d342894c | 1121 | } |
1122 | ||
e4f67add DS |
1123 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb) |
1124 | { | |
1125 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1126 | struct arphdr *parp; | |
1127 | u8 *arpptr, *sha; | |
1128 | __be32 sip, tip; | |
1129 | struct neighbour *n; | |
1130 | ||
1131 | if (dev->flags & IFF_NOARP) | |
1132 | goto out; | |
1133 | ||
1134 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { | |
1135 | dev->stats.tx_dropped++; | |
1136 | goto out; | |
1137 | } | |
1138 | parp = arp_hdr(skb); | |
1139 | ||
1140 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && | |
1141 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || | |
1142 | parp->ar_pro != htons(ETH_P_IP) || | |
1143 | parp->ar_op != htons(ARPOP_REQUEST) || | |
1144 | parp->ar_hln != dev->addr_len || | |
1145 | parp->ar_pln != 4) | |
1146 | goto out; | |
1147 | arpptr = (u8 *)parp + sizeof(struct arphdr); | |
1148 | sha = arpptr; | |
1149 | arpptr += dev->addr_len; /* sha */ | |
1150 | memcpy(&sip, arpptr, sizeof(sip)); | |
1151 | arpptr += sizeof(sip); | |
1152 | arpptr += dev->addr_len; /* tha */ | |
1153 | memcpy(&tip, arpptr, sizeof(tip)); | |
1154 | ||
1155 | if (ipv4_is_loopback(tip) || | |
1156 | ipv4_is_multicast(tip)) | |
1157 | goto out; | |
1158 | ||
1159 | n = neigh_lookup(&arp_tbl, &tip, dev); | |
1160 | ||
1161 | if (n) { | |
e4f67add DS |
1162 | struct vxlan_fdb *f; |
1163 | struct sk_buff *reply; | |
1164 | ||
1165 | if (!(n->nud_state & NUD_CONNECTED)) { | |
1166 | neigh_release(n); | |
1167 | goto out; | |
1168 | } | |
1169 | ||
1170 | f = vxlan_find_mac(vxlan, n->ha); | |
e4c7ed41 | 1171 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
e4f67add DS |
1172 | /* bridge-local neighbor */ |
1173 | neigh_release(n); | |
1174 | goto out; | |
1175 | } | |
1176 | ||
1177 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, | |
1178 | n->ha, sha); | |
1179 | ||
1180 | neigh_release(n); | |
1181 | ||
1182 | skb_reset_mac_header(reply); | |
1183 | __skb_pull(reply, skb_network_offset(reply)); | |
1184 | reply->ip_summed = CHECKSUM_UNNECESSARY; | |
1185 | reply->pkt_type = PACKET_HOST; | |
1186 | ||
1187 | if (netif_rx_ni(reply) == NET_RX_DROP) | |
1188 | dev->stats.rx_dropped++; | |
e4c7ed41 CW |
1189 | } else if (vxlan->flags & VXLAN_F_L3MISS) { |
1190 | union vxlan_addr ipa = { | |
1191 | .sin.sin_addr.s_addr = tip, | |
1192 | .sa.sa_family = AF_INET, | |
1193 | }; | |
1194 | ||
1195 | vxlan_ip_miss(dev, &ipa); | |
1196 | } | |
e4f67add DS |
1197 | out: |
1198 | consume_skb(skb); | |
1199 | return NETDEV_TX_OK; | |
1200 | } | |
1201 | ||
f564f45c CW |
1202 | #if IS_ENABLED(CONFIG_IPV6) |
1203 | static int neigh_reduce(struct net_device *dev, struct sk_buff *skb) | |
1204 | { | |
1205 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1206 | struct neighbour *n; | |
1207 | union vxlan_addr ipa; | |
1208 | const struct ipv6hdr *iphdr; | |
1209 | const struct in6_addr *saddr, *daddr; | |
1210 | struct nd_msg *msg; | |
1211 | struct inet6_dev *in6_dev = NULL; | |
1212 | ||
1213 | in6_dev = __in6_dev_get(dev); | |
1214 | if (!in6_dev) | |
1215 | goto out; | |
1216 | ||
1217 | if (!pskb_may_pull(skb, skb->len)) | |
1218 | goto out; | |
1219 | ||
1220 | iphdr = ipv6_hdr(skb); | |
1221 | saddr = &iphdr->saddr; | |
1222 | daddr = &iphdr->daddr; | |
1223 | ||
1224 | if (ipv6_addr_loopback(daddr) || | |
1225 | ipv6_addr_is_multicast(daddr)) | |
1226 | goto out; | |
1227 | ||
1228 | msg = (struct nd_msg *)skb_transport_header(skb); | |
1229 | if (msg->icmph.icmp6_code != 0 || | |
1230 | msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) | |
1231 | goto out; | |
1232 | ||
1233 | n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev); | |
1234 | ||
1235 | if (n) { | |
1236 | struct vxlan_fdb *f; | |
1237 | ||
1238 | if (!(n->nud_state & NUD_CONNECTED)) { | |
1239 | neigh_release(n); | |
1240 | goto out; | |
1241 | } | |
1242 | ||
1243 | f = vxlan_find_mac(vxlan, n->ha); | |
1244 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { | |
1245 | /* bridge-local neighbor */ | |
1246 | neigh_release(n); | |
1247 | goto out; | |
1248 | } | |
1249 | ||
1250 | ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target, | |
1251 | !!in6_dev->cnf.forwarding, | |
1252 | true, false, false); | |
1253 | neigh_release(n); | |
1254 | } else if (vxlan->flags & VXLAN_F_L3MISS) { | |
1255 | ipa.sin6.sin6_addr = *daddr; | |
1256 | ipa.sa.sa_family = AF_INET6; | |
1257 | vxlan_ip_miss(dev, &ipa); | |
1258 | } | |
1259 | ||
1260 | out: | |
1261 | consume_skb(skb); | |
1262 | return NETDEV_TX_OK; | |
1263 | } | |
1264 | #endif | |
1265 | ||
e4f67add DS |
1266 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) |
1267 | { | |
1268 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1269 | struct neighbour *n; | |
e4f67add DS |
1270 | |
1271 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) | |
1272 | return false; | |
1273 | ||
1274 | n = NULL; | |
1275 | switch (ntohs(eth_hdr(skb)->h_proto)) { | |
1276 | case ETH_P_IP: | |
e15a00aa CW |
1277 | { |
1278 | struct iphdr *pip; | |
1279 | ||
e4f67add DS |
1280 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
1281 | return false; | |
1282 | pip = ip_hdr(skb); | |
1283 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); | |
e4c7ed41 CW |
1284 | if (!n && (vxlan->flags & VXLAN_F_L3MISS)) { |
1285 | union vxlan_addr ipa = { | |
1286 | .sin.sin_addr.s_addr = pip->daddr, | |
1287 | .sa.sa_family = AF_INET, | |
1288 | }; | |
1289 | ||
1290 | vxlan_ip_miss(dev, &ipa); | |
1291 | return false; | |
1292 | } | |
1293 | ||
e4f67add | 1294 | break; |
e15a00aa CW |
1295 | } |
1296 | #if IS_ENABLED(CONFIG_IPV6) | |
1297 | case ETH_P_IPV6: | |
1298 | { | |
1299 | struct ipv6hdr *pip6; | |
1300 | ||
1301 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) | |
1302 | return false; | |
1303 | pip6 = ipv6_hdr(skb); | |
1304 | n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev); | |
1305 | if (!n && (vxlan->flags & VXLAN_F_L3MISS)) { | |
1306 | union vxlan_addr ipa = { | |
1307 | .sin6.sin6_addr = pip6->daddr, | |
1308 | .sa.sa_family = AF_INET6, | |
1309 | }; | |
1310 | ||
1311 | vxlan_ip_miss(dev, &ipa); | |
1312 | return false; | |
1313 | } | |
1314 | ||
1315 | break; | |
1316 | } | |
1317 | #endif | |
e4f67add DS |
1318 | default: |
1319 | return false; | |
1320 | } | |
1321 | ||
1322 | if (n) { | |
1323 | bool diff; | |
1324 | ||
7367d0b5 | 1325 | diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha); |
e4f67add DS |
1326 | if (diff) { |
1327 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, | |
1328 | dev->addr_len); | |
1329 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); | |
1330 | } | |
1331 | neigh_release(n); | |
1332 | return diff; | |
e4c7ed41 CW |
1333 | } |
1334 | ||
e4f67add DS |
1335 | return false; |
1336 | } | |
1337 | ||
553675fb | 1338 | static void vxlan_sock_put(struct sk_buff *skb) |
1cad8715 | 1339 | { |
1340 | sock_put(skb->sk); | |
1341 | } | |
1342 | ||
1343 | /* On transmit, associate with the tunnel socket */ | |
49560532 | 1344 | static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb) |
1cad8715 | 1345 | { |
1cad8715 | 1346 | skb_orphan(skb); |
1347 | sock_hold(sk); | |
1348 | skb->sk = sk; | |
553675fb | 1349 | skb->destructor = vxlan_sock_put; |
1cad8715 | 1350 | } |
1351 | ||
05f47d69 | 1352 | /* Compute source port for outgoing packet |
1353 | * first choice to use L4 flow hash since it will spread | |
1354 | * better and maybe available from hardware | |
1355 | * secondary choice is to use jhash on the Ethernet header | |
1356 | */ | |
49560532 | 1357 | __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb) |
05f47d69 | 1358 | { |
49560532 | 1359 | unsigned int range = (port_max - port_min) + 1; |
05f47d69 | 1360 | u32 hash; |
1361 | ||
1362 | hash = skb_get_rxhash(skb); | |
1363 | if (!hash) | |
1364 | hash = jhash(skb->data, 2 * ETH_ALEN, | |
1365 | (__force u32) skb->protocol); | |
1366 | ||
49560532 | 1367 | return htons((((u64) hash * range) >> 32) + port_min); |
05f47d69 | 1368 | } |
49560532 | 1369 | EXPORT_SYMBOL_GPL(vxlan_src_port); |
05f47d69 | 1370 | |
05c0db08 PS |
1371 | static int handle_offloads(struct sk_buff *skb) |
1372 | { | |
1373 | if (skb_is_gso(skb)) { | |
1374 | int err = skb_unclone(skb, GFP_ATOMIC); | |
1375 | if (unlikely(err)) | |
1376 | return err; | |
1377 | ||
f6ace502 | 1378 | skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL; |
05c0db08 PS |
1379 | } else if (skb->ip_summed != CHECKSUM_PARTIAL) |
1380 | skb->ip_summed = CHECKSUM_NONE; | |
1381 | ||
1382 | return 0; | |
1383 | } | |
1384 | ||
e4c7ed41 | 1385 | #if IS_ENABLED(CONFIG_IPV6) |
11796187 | 1386 | static int vxlan6_xmit_skb(struct vxlan_sock *vs, |
e4c7ed41 CW |
1387 | struct dst_entry *dst, struct sk_buff *skb, |
1388 | struct net_device *dev, struct in6_addr *saddr, | |
1389 | struct in6_addr *daddr, __u8 prio, __u8 ttl, | |
1390 | __be16 src_port, __be16 dst_port, __be32 vni) | |
1391 | { | |
1392 | struct ipv6hdr *ip6h; | |
1393 | struct vxlanhdr *vxh; | |
1394 | struct udphdr *uh; | |
1395 | int min_headroom; | |
1396 | int err; | |
1397 | ||
1398 | if (!skb->encapsulation) { | |
1399 | skb_reset_inner_headers(skb); | |
1400 | skb->encapsulation = 1; | |
1401 | } | |
1402 | ||
963a88b3 ND |
1403 | skb_scrub_packet(skb, false); |
1404 | ||
e4c7ed41 CW |
1405 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len |
1406 | + VXLAN_HLEN + sizeof(struct ipv6hdr) | |
1407 | + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0); | |
1408 | ||
1409 | /* Need space for new headers (invalidates iph ptr) */ | |
1410 | err = skb_cow_head(skb, min_headroom); | |
1411 | if (unlikely(err)) | |
1412 | return err; | |
1413 | ||
1414 | if (vlan_tx_tag_present(skb)) { | |
1415 | if (WARN_ON(!__vlan_put_tag(skb, | |
1416 | skb->vlan_proto, | |
1417 | vlan_tx_tag_get(skb)))) | |
1418 | return -ENOMEM; | |
1419 | ||
1420 | skb->vlan_tci = 0; | |
1421 | } | |
1422 | ||
1423 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); | |
1424 | vxh->vx_flags = htonl(VXLAN_FLAGS); | |
1425 | vxh->vx_vni = vni; | |
1426 | ||
1427 | __skb_push(skb, sizeof(*uh)); | |
1428 | skb_reset_transport_header(skb); | |
1429 | uh = udp_hdr(skb); | |
1430 | ||
1431 | uh->dest = dst_port; | |
1432 | uh->source = src_port; | |
1433 | ||
1434 | uh->len = htons(skb->len); | |
1435 | uh->check = 0; | |
1436 | ||
1437 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); | |
1438 | IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED | | |
1439 | IPSKB_REROUTED); | |
e4c7ed41 CW |
1440 | skb_dst_set(skb, dst); |
1441 | ||
1442 | if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) { | |
1443 | __wsum csum = skb_checksum(skb, 0, skb->len, 0); | |
1444 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
1445 | uh->check = csum_ipv6_magic(saddr, daddr, skb->len, | |
1446 | IPPROTO_UDP, csum); | |
1447 | if (uh->check == 0) | |
1448 | uh->check = CSUM_MANGLED_0; | |
1449 | } else { | |
1450 | skb->ip_summed = CHECKSUM_PARTIAL; | |
1451 | skb->csum_start = skb_transport_header(skb) - skb->head; | |
1452 | skb->csum_offset = offsetof(struct udphdr, check); | |
1453 | uh->check = ~csum_ipv6_magic(saddr, daddr, | |
1454 | skb->len, IPPROTO_UDP, 0); | |
1455 | } | |
1456 | ||
1457 | __skb_push(skb, sizeof(*ip6h)); | |
1458 | skb_reset_network_header(skb); | |
1459 | ip6h = ipv6_hdr(skb); | |
1460 | ip6h->version = 6; | |
1461 | ip6h->priority = prio; | |
1462 | ip6h->flow_lbl[0] = 0; | |
1463 | ip6h->flow_lbl[1] = 0; | |
1464 | ip6h->flow_lbl[2] = 0; | |
1465 | ip6h->payload_len = htons(skb->len); | |
1466 | ip6h->nexthdr = IPPROTO_UDP; | |
1467 | ip6h->hop_limit = ttl; | |
1468 | ip6h->daddr = *daddr; | |
1469 | ip6h->saddr = *saddr; | |
1470 | ||
1471 | vxlan_set_owner(vs->sock->sk, skb); | |
1472 | ||
1473 | err = handle_offloads(skb); | |
1474 | if (err) | |
1475 | return err; | |
1476 | ||
1477 | ip6tunnel_xmit(skb, dev); | |
1478 | return 0; | |
1479 | } | |
1480 | #endif | |
1481 | ||
11796187 | 1482 | int vxlan_xmit_skb(struct vxlan_sock *vs, |
49560532 PS |
1483 | struct rtable *rt, struct sk_buff *skb, |
1484 | __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df, | |
1485 | __be16 src_port, __be16 dst_port, __be32 vni) | |
1486 | { | |
1487 | struct vxlanhdr *vxh; | |
1488 | struct udphdr *uh; | |
649c5b8b | 1489 | int min_headroom; |
49560532 PS |
1490 | int err; |
1491 | ||
1492 | if (!skb->encapsulation) { | |
1493 | skb_reset_inner_headers(skb); | |
1494 | skb->encapsulation = 1; | |
1495 | } | |
1496 | ||
649c5b8b | 1497 | min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len |
1eaa8178 PS |
1498 | + VXLAN_HLEN + sizeof(struct iphdr) |
1499 | + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0); | |
649c5b8b PS |
1500 | |
1501 | /* Need space for new headers (invalidates iph ptr) */ | |
1502 | err = skb_cow_head(skb, min_headroom); | |
1503 | if (unlikely(err)) | |
1504 | return err; | |
1505 | ||
1eaa8178 PS |
1506 | if (vlan_tx_tag_present(skb)) { |
1507 | if (WARN_ON(!__vlan_put_tag(skb, | |
1508 | skb->vlan_proto, | |
1509 | vlan_tx_tag_get(skb)))) | |
1510 | return -ENOMEM; | |
1511 | ||
1512 | skb->vlan_tci = 0; | |
1513 | } | |
1514 | ||
49560532 PS |
1515 | vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh)); |
1516 | vxh->vx_flags = htonl(VXLAN_FLAGS); | |
1517 | vxh->vx_vni = vni; | |
1518 | ||
1519 | __skb_push(skb, sizeof(*uh)); | |
1520 | skb_reset_transport_header(skb); | |
1521 | uh = udp_hdr(skb); | |
1522 | ||
1523 | uh->dest = dst_port; | |
1524 | uh->source = src_port; | |
1525 | ||
1526 | uh->len = htons(skb->len); | |
1527 | uh->check = 0; | |
1528 | ||
1529 | vxlan_set_owner(vs->sock->sk, skb); | |
1530 | ||
1531 | err = handle_offloads(skb); | |
1532 | if (err) | |
1533 | return err; | |
1534 | ||
963a88b3 ND |
1535 | return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df, |
1536 | false); | |
49560532 PS |
1537 | } |
1538 | EXPORT_SYMBOL_GPL(vxlan_xmit_skb); | |
1539 | ||
9dcc71e1 SS |
1540 | /* Bypass encapsulation if the destination is local */ |
1541 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, | |
1542 | struct vxlan_dev *dst_vxlan) | |
1543 | { | |
1544 | struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); | |
1545 | struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); | |
e4c7ed41 CW |
1546 | union vxlan_addr loopback; |
1547 | union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip; | |
9dcc71e1 SS |
1548 | |
1549 | skb->pkt_type = PACKET_HOST; | |
1550 | skb->encapsulation = 0; | |
1551 | skb->dev = dst_vxlan->dev; | |
1552 | __skb_pull(skb, skb_network_offset(skb)); | |
1553 | ||
e4c7ed41 CW |
1554 | if (remote_ip->sa.sa_family == AF_INET) { |
1555 | loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
1556 | loopback.sa.sa_family = AF_INET; | |
1557 | #if IS_ENABLED(CONFIG_IPV6) | |
1558 | } else { | |
1559 | loopback.sin6.sin6_addr = in6addr_loopback; | |
1560 | loopback.sa.sa_family = AF_INET6; | |
1561 | #endif | |
1562 | } | |
1563 | ||
9dcc71e1 | 1564 | if (dst_vxlan->flags & VXLAN_F_LEARN) |
e4c7ed41 | 1565 | vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source); |
9dcc71e1 SS |
1566 | |
1567 | u64_stats_update_begin(&tx_stats->syncp); | |
1568 | tx_stats->tx_packets++; | |
1569 | tx_stats->tx_bytes += skb->len; | |
1570 | u64_stats_update_end(&tx_stats->syncp); | |
1571 | ||
1572 | if (netif_rx(skb) == NET_RX_SUCCESS) { | |
1573 | u64_stats_update_begin(&rx_stats->syncp); | |
1574 | rx_stats->rx_packets++; | |
1575 | rx_stats->rx_bytes += skb->len; | |
1576 | u64_stats_update_end(&rx_stats->syncp); | |
1577 | } else { | |
1578 | skb->dev->stats.rx_dropped++; | |
1579 | } | |
1580 | } | |
1581 | ||
4ad16930 SH |
1582 | static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
1583 | struct vxlan_rdst *rdst, bool did_rsc) | |
d342894c | 1584 | { |
1585 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
e4c7ed41 | 1586 | struct rtable *rt = NULL; |
d342894c | 1587 | const struct iphdr *old_iph; |
d342894c | 1588 | struct flowi4 fl4; |
e4c7ed41 CW |
1589 | union vxlan_addr *dst; |
1590 | __be16 src_port = 0, dst_port; | |
234f5b73 | 1591 | u32 vni; |
d342894c | 1592 | __be16 df = 0; |
1593 | __u8 tos, ttl; | |
0e6fbc5b | 1594 | int err; |
d342894c | 1595 | |
823aa873 | 1596 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port; |
6681712d | 1597 | vni = rdst->remote_vni; |
e4c7ed41 | 1598 | dst = &rdst->remote_ip; |
e4f67add | 1599 | |
e4c7ed41 | 1600 | if (vxlan_addr_any(dst)) { |
e4f67add | 1601 | if (did_rsc) { |
e4f67add | 1602 | /* short-circuited back to local bridge */ |
9dcc71e1 | 1603 | vxlan_encap_bypass(skb, vxlan, vxlan); |
4ad16930 | 1604 | return; |
e4f67add | 1605 | } |
ef59febe | 1606 | goto drop; |
e4f67add | 1607 | } |
ef59febe | 1608 | |
d342894c | 1609 | old_iph = ip_hdr(skb); |
1610 | ||
d342894c | 1611 | ttl = vxlan->ttl; |
e4c7ed41 | 1612 | if (!ttl && vxlan_addr_multicast(dst)) |
d342894c | 1613 | ttl = 1; |
1614 | ||
1615 | tos = vxlan->tos; | |
1616 | if (tos == 1) | |
206aaafc | 1617 | tos = ip_tunnel_get_dsfield(old_iph, skb); |
d342894c | 1618 | |
49560532 | 1619 | src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb); |
d342894c | 1620 | |
e4c7ed41 CW |
1621 | if (dst->sa.sa_family == AF_INET) { |
1622 | memset(&fl4, 0, sizeof(fl4)); | |
1623 | fl4.flowi4_oif = rdst->remote_ifindex; | |
1624 | fl4.flowi4_tos = RT_TOS(tos); | |
1625 | fl4.daddr = dst->sin.sin_addr.s_addr; | |
1626 | fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr; | |
1627 | ||
1628 | rt = ip_route_output_key(dev_net(dev), &fl4); | |
1629 | if (IS_ERR(rt)) { | |
1630 | netdev_dbg(dev, "no route to %pI4\n", | |
1631 | &dst->sin.sin_addr.s_addr); | |
1632 | dev->stats.tx_carrier_errors++; | |
1633 | goto tx_error; | |
1634 | } | |
d342894c | 1635 | |
e4c7ed41 CW |
1636 | if (rt->dst.dev == dev) { |
1637 | netdev_dbg(dev, "circular route to %pI4\n", | |
1638 | &dst->sin.sin_addr.s_addr); | |
1639 | dev->stats.collisions++; | |
1640 | goto tx_error; | |
1641 | } | |
1642 | ||
1643 | /* Bypass encapsulation if the destination is local */ | |
1644 | if (rt->rt_flags & RTCF_LOCAL && | |
1645 | !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { | |
1646 | struct vxlan_dev *dst_vxlan; | |
1647 | ||
1648 | ip_rt_put(rt); | |
1649 | dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port); | |
1650 | if (!dst_vxlan) | |
1651 | goto tx_error; | |
1652 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); | |
1653 | return; | |
1654 | } | |
1655 | ||
1656 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); | |
1657 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); | |
d342894c | 1658 | |
11796187 | 1659 | err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb, |
e4c7ed41 CW |
1660 | fl4.saddr, dst->sin.sin_addr.s_addr, |
1661 | tos, ttl, df, src_port, dst_port, | |
1662 | htonl(vni << 8)); | |
9dcc71e1 | 1663 | |
e4c7ed41 CW |
1664 | if (err < 0) |
1665 | goto rt_tx_error; | |
1666 | iptunnel_xmit_stats(err, &dev->stats, dev->tstats); | |
1667 | #if IS_ENABLED(CONFIG_IPV6) | |
1668 | } else { | |
1669 | struct sock *sk = vxlan->vn_sock->sock->sk; | |
1670 | struct dst_entry *ndst; | |
1671 | struct flowi6 fl6; | |
1672 | u32 flags; | |
1673 | ||
1674 | memset(&fl6, 0, sizeof(fl6)); | |
1675 | fl6.flowi6_oif = rdst->remote_ifindex; | |
1676 | fl6.daddr = dst->sin6.sin6_addr; | |
1677 | fl6.saddr = vxlan->saddr.sin6.sin6_addr; | |
8c1bb79f | 1678 | fl6.flowi6_proto = IPPROTO_UDP; |
e4c7ed41 CW |
1679 | |
1680 | if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) { | |
1681 | netdev_dbg(dev, "no route to %pI6\n", | |
1682 | &dst->sin6.sin6_addr); | |
1683 | dev->stats.tx_carrier_errors++; | |
9dcc71e1 | 1684 | goto tx_error; |
e4c7ed41 | 1685 | } |
d342894c | 1686 | |
e4c7ed41 CW |
1687 | if (ndst->dev == dev) { |
1688 | netdev_dbg(dev, "circular route to %pI6\n", | |
1689 | &dst->sin6.sin6_addr); | |
1690 | dst_release(ndst); | |
1691 | dev->stats.collisions++; | |
1692 | goto tx_error; | |
1693 | } | |
0e6fbc5b | 1694 | |
e4c7ed41 CW |
1695 | /* Bypass encapsulation if the destination is local */ |
1696 | flags = ((struct rt6_info *)ndst)->rt6i_flags; | |
1697 | if (flags & RTF_LOCAL && | |
1698 | !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { | |
1699 | struct vxlan_dev *dst_vxlan; | |
1700 | ||
1701 | dst_release(ndst); | |
1702 | dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port); | |
1703 | if (!dst_vxlan) | |
1704 | goto tx_error; | |
1705 | vxlan_encap_bypass(skb, vxlan, dst_vxlan); | |
1706 | return; | |
1707 | } | |
49560532 | 1708 | |
e4c7ed41 CW |
1709 | ttl = ttl ? : ip6_dst_hoplimit(ndst); |
1710 | ||
11796187 | 1711 | err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb, |
e4c7ed41 CW |
1712 | dev, &fl6.saddr, &fl6.daddr, 0, ttl, |
1713 | src_port, dst_port, htonl(vni << 8)); | |
1714 | #endif | |
1715 | } | |
0e6fbc5b | 1716 | |
4ad16930 | 1717 | return; |
d342894c | 1718 | |
1719 | drop: | |
1720 | dev->stats.tx_dropped++; | |
1721 | goto tx_free; | |
1722 | ||
49560532 PS |
1723 | rt_tx_error: |
1724 | ip_rt_put(rt); | |
d342894c | 1725 | tx_error: |
1726 | dev->stats.tx_errors++; | |
1727 | tx_free: | |
1728 | dev_kfree_skb(skb); | |
d342894c | 1729 | } |
1730 | ||
6681712d DS |
1731 | /* Transmit local packets over Vxlan |
1732 | * | |
1733 | * Outer IP header inherits ECN and DF from inner header. | |
1734 | * Outer UDP destination is the VXLAN assigned port. | |
1735 | * source port is based on hash of flow | |
1736 | */ | |
1737 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) | |
1738 | { | |
1739 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1740 | struct ethhdr *eth; | |
1741 | bool did_rsc = false; | |
afbd8bae | 1742 | struct vxlan_rdst *rdst; |
6681712d | 1743 | struct vxlan_fdb *f; |
6681712d DS |
1744 | |
1745 | skb_reset_mac_header(skb); | |
1746 | eth = eth_hdr(skb); | |
1747 | ||
f564f45c CW |
1748 | if ((vxlan->flags & VXLAN_F_PROXY)) { |
1749 | if (ntohs(eth->h_proto) == ETH_P_ARP) | |
1750 | return arp_reduce(dev, skb); | |
1751 | #if IS_ENABLED(CONFIG_IPV6) | |
1752 | else if (ntohs(eth->h_proto) == ETH_P_IPV6 && | |
1753 | skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) && | |
1754 | ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { | |
1755 | struct nd_msg *msg; | |
1756 | ||
1757 | msg = (struct nd_msg *)skb_transport_header(skb); | |
1758 | if (msg->icmph.icmp6_code == 0 && | |
1759 | msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) | |
1760 | return neigh_reduce(dev, skb); | |
1761 | } | |
1762 | #endif | |
1763 | } | |
6681712d DS |
1764 | |
1765 | f = vxlan_find_mac(vxlan, eth->h_dest); | |
ae884082 DS |
1766 | did_rsc = false; |
1767 | ||
1768 | if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) && | |
e15a00aa CW |
1769 | (ntohs(eth->h_proto) == ETH_P_IP || |
1770 | ntohs(eth->h_proto) == ETH_P_IPV6)) { | |
ae884082 DS |
1771 | did_rsc = route_shortcircuit(dev, skb); |
1772 | if (did_rsc) | |
1773 | f = vxlan_find_mac(vxlan, eth->h_dest); | |
1774 | } | |
1775 | ||
6681712d | 1776 | if (f == NULL) { |
afbd8bae MR |
1777 | f = vxlan_find_mac(vxlan, all_zeros_mac); |
1778 | if (f == NULL) { | |
1779 | if ((vxlan->flags & VXLAN_F_L2MISS) && | |
1780 | !is_multicast_ether_addr(eth->h_dest)) | |
1781 | vxlan_fdb_miss(vxlan, eth->h_dest); | |
1782 | ||
1783 | dev->stats.tx_dropped++; | |
1784 | dev_kfree_skb(skb); | |
1785 | return NETDEV_TX_OK; | |
1786 | } | |
1787 | } | |
6681712d | 1788 | |
afbd8bae MR |
1789 | list_for_each_entry_rcu(rdst, &f->remotes, list) { |
1790 | struct sk_buff *skb1; | |
6681712d | 1791 | |
afbd8bae MR |
1792 | skb1 = skb_clone(skb, GFP_ATOMIC); |
1793 | if (skb1) | |
1794 | vxlan_xmit_one(skb1, dev, rdst, did_rsc); | |
6681712d DS |
1795 | } |
1796 | ||
afbd8bae | 1797 | dev_kfree_skb(skb); |
4ad16930 | 1798 | return NETDEV_TX_OK; |
6681712d DS |
1799 | } |
1800 | ||
d342894c | 1801 | /* Walk the forwarding table and purge stale entries */ |
1802 | static void vxlan_cleanup(unsigned long arg) | |
1803 | { | |
1804 | struct vxlan_dev *vxlan = (struct vxlan_dev *) arg; | |
1805 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; | |
1806 | unsigned int h; | |
1807 | ||
1808 | if (!netif_running(vxlan->dev)) | |
1809 | return; | |
1810 | ||
1811 | spin_lock_bh(&vxlan->hash_lock); | |
1812 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
1813 | struct hlist_node *p, *n; | |
1814 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { | |
1815 | struct vxlan_fdb *f | |
1816 | = container_of(p, struct vxlan_fdb, hlist); | |
1817 | unsigned long timeout; | |
1818 | ||
3c172868 | 1819 | if (f->state & NUD_PERMANENT) |
d342894c | 1820 | continue; |
1821 | ||
1822 | timeout = f->used + vxlan->age_interval * HZ; | |
1823 | if (time_before_eq(timeout, jiffies)) { | |
1824 | netdev_dbg(vxlan->dev, | |
1825 | "garbage collect %pM\n", | |
1826 | f->eth_addr); | |
1827 | f->state = NUD_STALE; | |
1828 | vxlan_fdb_destroy(vxlan, f); | |
1829 | } else if (time_before(timeout, next_timer)) | |
1830 | next_timer = timeout; | |
1831 | } | |
1832 | } | |
1833 | spin_unlock_bh(&vxlan->hash_lock); | |
1834 | ||
1835 | mod_timer(&vxlan->age_timer, next_timer); | |
1836 | } | |
1837 | ||
9c2e24e1 PS |
1838 | static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan) |
1839 | { | |
1840 | __u32 vni = vxlan->default_dst.remote_vni; | |
1841 | ||
1842 | vxlan->vn_sock = vs; | |
1843 | hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni)); | |
1844 | } | |
1845 | ||
d342894c | 1846 | /* Setup stats when device is created */ |
1847 | static int vxlan_init(struct net_device *dev) | |
1848 | { | |
1c51a915 SH |
1849 | struct vxlan_dev *vxlan = netdev_priv(dev); |
1850 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); | |
1851 | struct vxlan_sock *vs; | |
1c51a915 | 1852 | |
e8171045 PS |
1853 | dev->tstats = alloc_percpu(struct pcpu_tstats); |
1854 | if (!dev->tstats) | |
d342894c | 1855 | return -ENOMEM; |
1856 | ||
1c51a915 | 1857 | spin_lock(&vn->sock_lock); |
9c2e24e1 | 1858 | vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port); |
1c51a915 SH |
1859 | if (vs) { |
1860 | /* If we have a socket with same port already, reuse it */ | |
1861 | atomic_inc(&vs->refcnt); | |
9c2e24e1 | 1862 | vxlan_vs_add_dev(vs, vxlan); |
1c51a915 SH |
1863 | } else { |
1864 | /* otherwise make new socket outside of RTNL */ | |
1865 | dev_hold(dev); | |
1866 | queue_work(vxlan_wq, &vxlan->sock_work); | |
1867 | } | |
1868 | spin_unlock(&vn->sock_lock); | |
1869 | ||
d342894c | 1870 | return 0; |
1871 | } | |
1872 | ||
ba609e9b | 1873 | static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan) |
afbd8bae MR |
1874 | { |
1875 | struct vxlan_fdb *f; | |
1876 | ||
1877 | spin_lock_bh(&vxlan->hash_lock); | |
1878 | f = __vxlan_find_mac(vxlan, all_zeros_mac); | |
1879 | if (f) | |
1880 | vxlan_fdb_destroy(vxlan, f); | |
1881 | spin_unlock_bh(&vxlan->hash_lock); | |
1882 | } | |
1883 | ||
ebf4063e SH |
1884 | static void vxlan_uninit(struct net_device *dev) |
1885 | { | |
1886 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
ebf4063e SH |
1887 | struct vxlan_sock *vs = vxlan->vn_sock; |
1888 | ||
ba609e9b | 1889 | vxlan_fdb_delete_default(vxlan); |
afbd8bae | 1890 | |
ebf4063e | 1891 | if (vs) |
012a5729 | 1892 | vxlan_sock_release(vs); |
ebf4063e SH |
1893 | free_percpu(dev->tstats); |
1894 | } | |
1895 | ||
d342894c | 1896 | /* Start ageing timer and join group when device is brought up */ |
1897 | static int vxlan_open(struct net_device *dev) | |
1898 | { | |
3fc2de2f | 1899 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
d342894c | 1900 | struct vxlan_dev *vxlan = netdev_priv(dev); |
1c51a915 SH |
1901 | struct vxlan_sock *vs = vxlan->vn_sock; |
1902 | ||
1903 | /* socket hasn't been created */ | |
1904 | if (!vs) | |
1905 | return -ENOTCONN; | |
d342894c | 1906 | |
e4c7ed41 CW |
1907 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && |
1908 | vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) { | |
1c51a915 | 1909 | vxlan_sock_hold(vs); |
7c47cedf | 1910 | dev_hold(dev); |
3fc2de2f | 1911 | queue_work(vxlan_wq, &vxlan->igmp_join); |
d342894c | 1912 | } |
1913 | ||
1914 | if (vxlan->age_interval) | |
1915 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); | |
1916 | ||
1917 | return 0; | |
1918 | } | |
1919 | ||
1920 | /* Purge the forwarding table */ | |
1921 | static void vxlan_flush(struct vxlan_dev *vxlan) | |
1922 | { | |
31fec5aa | 1923 | unsigned int h; |
d342894c | 1924 | |
1925 | spin_lock_bh(&vxlan->hash_lock); | |
1926 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
1927 | struct hlist_node *p, *n; | |
1928 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { | |
1929 | struct vxlan_fdb *f | |
1930 | = container_of(p, struct vxlan_fdb, hlist); | |
afbd8bae MR |
1931 | /* the all_zeros_mac entry is deleted at vxlan_uninit */ |
1932 | if (!is_zero_ether_addr(f->eth_addr)) | |
1933 | vxlan_fdb_destroy(vxlan, f); | |
d342894c | 1934 | } |
1935 | } | |
1936 | spin_unlock_bh(&vxlan->hash_lock); | |
1937 | } | |
1938 | ||
1939 | /* Cleanup timer and forwarding table on shutdown */ | |
1940 | static int vxlan_stop(struct net_device *dev) | |
1941 | { | |
3fc2de2f | 1942 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
d342894c | 1943 | struct vxlan_dev *vxlan = netdev_priv(dev); |
1c51a915 | 1944 | struct vxlan_sock *vs = vxlan->vn_sock; |
d342894c | 1945 | |
e4c7ed41 CW |
1946 | if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && |
1947 | ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) { | |
1c51a915 | 1948 | vxlan_sock_hold(vs); |
7c47cedf | 1949 | dev_hold(dev); |
3fc2de2f | 1950 | queue_work(vxlan_wq, &vxlan->igmp_leave); |
7c47cedf | 1951 | } |
d342894c | 1952 | |
1953 | del_timer_sync(&vxlan->age_timer); | |
1954 | ||
1955 | vxlan_flush(vxlan); | |
1956 | ||
1957 | return 0; | |
1958 | } | |
1959 | ||
d342894c | 1960 | /* Stub, nothing needs to be done. */ |
1961 | static void vxlan_set_multicast_list(struct net_device *dev) | |
1962 | { | |
1963 | } | |
1964 | ||
1965 | static const struct net_device_ops vxlan_netdev_ops = { | |
1966 | .ndo_init = vxlan_init, | |
ebf4063e | 1967 | .ndo_uninit = vxlan_uninit, |
d342894c | 1968 | .ndo_open = vxlan_open, |
1969 | .ndo_stop = vxlan_stop, | |
1970 | .ndo_start_xmit = vxlan_xmit, | |
e8171045 | 1971 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
d342894c | 1972 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
1973 | .ndo_change_mtu = eth_change_mtu, | |
1974 | .ndo_validate_addr = eth_validate_addr, | |
1975 | .ndo_set_mac_address = eth_mac_addr, | |
1976 | .ndo_fdb_add = vxlan_fdb_add, | |
1977 | .ndo_fdb_del = vxlan_fdb_delete, | |
1978 | .ndo_fdb_dump = vxlan_fdb_dump, | |
1979 | }; | |
1980 | ||
1981 | /* Info for udev, that this is a virtual tunnel endpoint */ | |
1982 | static struct device_type vxlan_type = { | |
1983 | .name = "vxlan", | |
1984 | }; | |
1985 | ||
d342894c | 1986 | /* Initialize the device structure. */ |
1987 | static void vxlan_setup(struct net_device *dev) | |
1988 | { | |
1989 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
31fec5aa | 1990 | unsigned int h; |
05f47d69 | 1991 | int low, high; |
d342894c | 1992 | |
1993 | eth_hw_addr_random(dev); | |
1994 | ether_setup(dev); | |
e4c7ed41 CW |
1995 | if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6) |
1996 | dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM; | |
1997 | else | |
1998 | dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM; | |
d342894c | 1999 | |
2000 | dev->netdev_ops = &vxlan_netdev_ops; | |
ebf4063e | 2001 | dev->destructor = free_netdev; |
d342894c | 2002 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
2003 | ||
2004 | dev->tx_queue_len = 0; | |
2005 | dev->features |= NETIF_F_LLTX; | |
2006 | dev->features |= NETIF_F_NETNS_LOCAL; | |
d6727fe3 | 2007 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
0afb1666 | 2008 | dev->features |= NETIF_F_RXCSUM; |
05c0db08 | 2009 | dev->features |= NETIF_F_GSO_SOFTWARE; |
0afb1666 | 2010 | |
1eaa8178 PS |
2011 | dev->vlan_features = dev->features; |
2012 | dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; | |
0afb1666 | 2013 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
05c0db08 | 2014 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
1eaa8178 | 2015 | dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; |
d342894c | 2016 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
6602d007 | 2017 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
d342894c | 2018 | |
553675fb | 2019 | INIT_LIST_HEAD(&vxlan->next); |
d342894c | 2020 | spin_lock_init(&vxlan->hash_lock); |
3fc2de2f | 2021 | INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join); |
2022 | INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave); | |
1c51a915 | 2023 | INIT_WORK(&vxlan->sock_work, vxlan_sock_work); |
d342894c | 2024 | |
2025 | init_timer_deferrable(&vxlan->age_timer); | |
2026 | vxlan->age_timer.function = vxlan_cleanup; | |
2027 | vxlan->age_timer.data = (unsigned long) vxlan; | |
2028 | ||
05f47d69 | 2029 | inet_get_local_port_range(&low, &high); |
2030 | vxlan->port_min = low; | |
2031 | vxlan->port_max = high; | |
823aa873 | 2032 | vxlan->dst_port = htons(vxlan_port); |
05f47d69 | 2033 | |
d342894c | 2034 | vxlan->dev = dev; |
2035 | ||
2036 | for (h = 0; h < FDB_HASH_SIZE; ++h) | |
2037 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); | |
2038 | } | |
2039 | ||
2040 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { | |
2041 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, | |
5d174dd8 | 2042 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
e4c7ed41 | 2043 | [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) }, |
d342894c | 2044 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
2045 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, | |
e4c7ed41 | 2046 | [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) }, |
d342894c | 2047 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
2048 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, | |
2049 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, | |
2050 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, | |
2051 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, | |
05f47d69 | 2052 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
e4f67add DS |
2053 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
2054 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, | |
2055 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, | |
2056 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, | |
823aa873 | 2057 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
d342894c | 2058 | }; |
2059 | ||
2060 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[]) | |
2061 | { | |
2062 | if (tb[IFLA_ADDRESS]) { | |
2063 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { | |
2064 | pr_debug("invalid link address (not ethernet)\n"); | |
2065 | return -EINVAL; | |
2066 | } | |
2067 | ||
2068 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { | |
2069 | pr_debug("invalid all zero ethernet address\n"); | |
2070 | return -EADDRNOTAVAIL; | |
2071 | } | |
2072 | } | |
2073 | ||
2074 | if (!data) | |
2075 | return -EINVAL; | |
2076 | ||
2077 | if (data[IFLA_VXLAN_ID]) { | |
2078 | __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); | |
2079 | if (id >= VXLAN_VID_MASK) | |
2080 | return -ERANGE; | |
2081 | } | |
2082 | ||
05f47d69 | 2083 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
2084 | const struct ifla_vxlan_port_range *p | |
2085 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
2086 | ||
2087 | if (ntohs(p->high) < ntohs(p->low)) { | |
2088 | pr_debug("port range %u .. %u not valid\n", | |
2089 | ntohs(p->low), ntohs(p->high)); | |
2090 | return -EINVAL; | |
2091 | } | |
2092 | } | |
2093 | ||
d342894c | 2094 | return 0; |
2095 | } | |
2096 | ||
1b13c97f YB |
2097 | static void vxlan_get_drvinfo(struct net_device *netdev, |
2098 | struct ethtool_drvinfo *drvinfo) | |
2099 | { | |
2100 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); | |
2101 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); | |
2102 | } | |
2103 | ||
2104 | static const struct ethtool_ops vxlan_ethtool_ops = { | |
2105 | .get_drvinfo = vxlan_get_drvinfo, | |
2106 | .get_link = ethtool_op_get_link, | |
2107 | }; | |
2108 | ||
553675fb | 2109 | static void vxlan_del_work(struct work_struct *work) |
2110 | { | |
2111 | struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work); | |
2112 | ||
2113 | sk_release_kernel(vs->sock->sk); | |
2114 | kfree_rcu(vs, rcu); | |
2115 | } | |
2116 | ||
e4c7ed41 CW |
2117 | #if IS_ENABLED(CONFIG_IPV6) |
2118 | /* Create UDP socket for encapsulation receive. AF_INET6 socket | |
2119 | * could be used for both IPv4 and IPv6 communications, but | |
2120 | * users may set bindv6only=1. | |
2121 | */ | |
2122 | static int create_v6_sock(struct net *net, __be16 port, struct socket **psock) | |
553675fb | 2123 | { |
553675fb | 2124 | struct sock *sk; |
e4c7ed41 CW |
2125 | struct socket *sock; |
2126 | struct sockaddr_in6 vxlan_addr = { | |
2127 | .sin6_family = AF_INET6, | |
2128 | .sin6_port = port, | |
2129 | }; | |
2130 | int rc, val = 1; | |
2131 | ||
2132 | rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock); | |
2133 | if (rc < 0) { | |
2134 | pr_debug("UDPv6 socket create failed\n"); | |
2135 | return rc; | |
2136 | } | |
2137 | ||
2138 | /* Put in proper namespace */ | |
2139 | sk = sock->sk; | |
2140 | sk_change_net(sk, net); | |
2141 | ||
2142 | kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY, | |
2143 | (char *)&val, sizeof(val)); | |
2144 | rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr, | |
2145 | sizeof(struct sockaddr_in6)); | |
2146 | if (rc < 0) { | |
2147 | pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n", | |
2148 | &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc); | |
2149 | sk_release_kernel(sk); | |
2150 | return rc; | |
2151 | } | |
2152 | /* At this point, IPv6 module should have been loaded in | |
2153 | * sock_create_kern(). | |
2154 | */ | |
2155 | BUG_ON(!ipv6_stub); | |
2156 | ||
2157 | *psock = sock; | |
2158 | /* Disable multicast loopback */ | |
2159 | inet_sk(sk)->mc_loop = 0; | |
2160 | return 0; | |
2161 | } | |
2162 | ||
2163 | #else | |
2164 | ||
2165 | static int create_v6_sock(struct net *net, __be16 port, struct socket **psock) | |
2166 | { | |
2167 | return -EPFNOSUPPORT; | |
2168 | } | |
2169 | #endif | |
2170 | ||
2171 | static int create_v4_sock(struct net *net, __be16 port, struct socket **psock) | |
2172 | { | |
2173 | struct sock *sk; | |
2174 | struct socket *sock; | |
553675fb | 2175 | struct sockaddr_in vxlan_addr = { |
2176 | .sin_family = AF_INET, | |
2177 | .sin_addr.s_addr = htonl(INADDR_ANY), | |
bb3fd687 | 2178 | .sin_port = port, |
553675fb | 2179 | }; |
2180 | int rc; | |
553675fb | 2181 | |
2182 | /* Create UDP socket for encapsulation receive. */ | |
e4c7ed41 | 2183 | rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock); |
553675fb | 2184 | if (rc < 0) { |
2185 | pr_debug("UDP socket create failed\n"); | |
e4c7ed41 | 2186 | return rc; |
553675fb | 2187 | } |
2188 | ||
2189 | /* Put in proper namespace */ | |
e4c7ed41 | 2190 | sk = sock->sk; |
553675fb | 2191 | sk_change_net(sk, net); |
2192 | ||
e4c7ed41 | 2193 | rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr, |
553675fb | 2194 | sizeof(vxlan_addr)); |
2195 | if (rc < 0) { | |
2196 | pr_debug("bind for UDP socket %pI4:%u (%d)\n", | |
2197 | &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc); | |
2198 | sk_release_kernel(sk); | |
e4c7ed41 CW |
2199 | return rc; |
2200 | } | |
2201 | ||
2202 | *psock = sock; | |
2203 | /* Disable multicast loopback */ | |
2204 | inet_sk(sk)->mc_loop = 0; | |
2205 | return 0; | |
2206 | } | |
2207 | ||
2208 | /* Create new listen socket if needed */ | |
2209 | static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port, | |
2210 | vxlan_rcv_t *rcv, void *data, bool ipv6) | |
2211 | { | |
2212 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2213 | struct vxlan_sock *vs; | |
2214 | struct socket *sock; | |
2215 | struct sock *sk; | |
2216 | int rc = 0; | |
2217 | unsigned int h; | |
2218 | ||
2219 | vs = kmalloc(sizeof(*vs), GFP_KERNEL); | |
2220 | if (!vs) | |
2221 | return ERR_PTR(-ENOMEM); | |
2222 | ||
2223 | for (h = 0; h < VNI_HASH_SIZE; ++h) | |
2224 | INIT_HLIST_HEAD(&vs->vni_list[h]); | |
2225 | ||
2226 | INIT_WORK(&vs->del_work, vxlan_del_work); | |
2227 | ||
2228 | if (ipv6) | |
2229 | rc = create_v6_sock(net, port, &sock); | |
2230 | else | |
2231 | rc = create_v4_sock(net, port, &sock); | |
2232 | if (rc < 0) { | |
553675fb | 2233 | kfree(vs); |
2234 | return ERR_PTR(rc); | |
2235 | } | |
e4c7ed41 CW |
2236 | |
2237 | vs->sock = sock; | |
2238 | sk = sock->sk; | |
9c2e24e1 | 2239 | atomic_set(&vs->refcnt, 1); |
5cfccc5a | 2240 | vs->rcv = rcv; |
012a5729 | 2241 | vs->data = data; |
430eda6d PS |
2242 | smp_wmb(); |
2243 | vs->sock->sk->sk_user_data = vs; | |
553675fb | 2244 | |
9c2e24e1 PS |
2245 | spin_lock(&vn->sock_lock); |
2246 | hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); | |
2247 | spin_unlock(&vn->sock_lock); | |
553675fb | 2248 | |
2249 | /* Mark socket as an encapsulation socket. */ | |
2250 | udp_sk(sk)->encap_type = 1; | |
2251 | udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv; | |
e4c7ed41 CW |
2252 | #if IS_ENABLED(CONFIG_IPV6) |
2253 | if (ipv6) | |
2254 | ipv6_stub->udpv6_encap_enable(); | |
2255 | else | |
2256 | #endif | |
2257 | udp_encap_enable(); | |
2258 | ||
9c2e24e1 PS |
2259 | return vs; |
2260 | } | |
2261 | ||
012a5729 PS |
2262 | struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port, |
2263 | vxlan_rcv_t *rcv, void *data, | |
e4c7ed41 | 2264 | bool no_share, bool ipv6) |
9c2e24e1 PS |
2265 | { |
2266 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2267 | struct vxlan_sock *vs; | |
2268 | ||
e4c7ed41 | 2269 | vs = vxlan_socket_create(net, port, rcv, data, ipv6); |
9c2e24e1 PS |
2270 | if (!IS_ERR(vs)) |
2271 | return vs; | |
553675fb | 2272 | |
012a5729 PS |
2273 | if (no_share) /* Return error if sharing is not allowed. */ |
2274 | return vs; | |
2275 | ||
9c2e24e1 PS |
2276 | spin_lock(&vn->sock_lock); |
2277 | vs = vxlan_find_sock(net, port); | |
5cfccc5a PS |
2278 | if (vs) { |
2279 | if (vs->rcv == rcv) | |
2280 | atomic_inc(&vs->refcnt); | |
2281 | else | |
2282 | vs = ERR_PTR(-EBUSY); | |
2283 | } | |
2284 | spin_unlock(&vn->sock_lock); | |
2285 | ||
2286 | if (!vs) | |
9c2e24e1 PS |
2287 | vs = ERR_PTR(-EINVAL); |
2288 | ||
553675fb | 2289 | return vs; |
2290 | } | |
012a5729 | 2291 | EXPORT_SYMBOL_GPL(vxlan_sock_add); |
553675fb | 2292 | |
1c51a915 SH |
2293 | /* Scheduled at device creation to bind to a socket */ |
2294 | static void vxlan_sock_work(struct work_struct *work) | |
2295 | { | |
9c2e24e1 PS |
2296 | struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work); |
2297 | struct net *net = dev_net(vxlan->dev); | |
1c51a915 | 2298 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
9c2e24e1 PS |
2299 | __be16 port = vxlan->dst_port; |
2300 | struct vxlan_sock *nvs; | |
1c51a915 | 2301 | |
e4c7ed41 | 2302 | nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6); |
1c51a915 | 2303 | spin_lock(&vn->sock_lock); |
9c2e24e1 PS |
2304 | if (!IS_ERR(nvs)) |
2305 | vxlan_vs_add_dev(nvs, vxlan); | |
2306 | spin_unlock(&vn->sock_lock); | |
2307 | ||
2308 | dev_put(vxlan->dev); | |
1c51a915 SH |
2309 | } |
2310 | ||
d342894c | 2311 | static int vxlan_newlink(struct net *net, struct net_device *dev, |
2312 | struct nlattr *tb[], struct nlattr *data[]) | |
2313 | { | |
553675fb | 2314 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); |
d342894c | 2315 | struct vxlan_dev *vxlan = netdev_priv(dev); |
c7995c43 | 2316 | struct vxlan_rdst *dst = &vxlan->default_dst; |
d342894c | 2317 | __u32 vni; |
2318 | int err; | |
e4c7ed41 | 2319 | bool use_ipv6 = false; |
d342894c | 2320 | |
2321 | if (!data[IFLA_VXLAN_ID]) | |
2322 | return -EINVAL; | |
2323 | ||
2324 | vni = nla_get_u32(data[IFLA_VXLAN_ID]); | |
c7995c43 | 2325 | dst->remote_vni = vni; |
d342894c | 2326 | |
e4c7ed41 CW |
2327 | if (data[IFLA_VXLAN_GROUP]) { |
2328 | dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]); | |
2329 | dst->remote_ip.sa.sa_family = AF_INET; | |
2330 | } else if (data[IFLA_VXLAN_GROUP6]) { | |
2331 | if (!IS_ENABLED(CONFIG_IPV6)) | |
2332 | return -EPFNOSUPPORT; | |
2333 | ||
2334 | nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6], | |
2335 | sizeof(struct in6_addr)); | |
2336 | dst->remote_ip.sa.sa_family = AF_INET6; | |
2337 | use_ipv6 = true; | |
2338 | } | |
d342894c | 2339 | |
e4c7ed41 CW |
2340 | if (data[IFLA_VXLAN_LOCAL]) { |
2341 | vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]); | |
2342 | vxlan->saddr.sa.sa_family = AF_INET; | |
2343 | } else if (data[IFLA_VXLAN_LOCAL6]) { | |
2344 | if (!IS_ENABLED(CONFIG_IPV6)) | |
2345 | return -EPFNOSUPPORT; | |
2346 | ||
2347 | /* TODO: respect scope id */ | |
2348 | nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6], | |
2349 | sizeof(struct in6_addr)); | |
2350 | vxlan->saddr.sa.sa_family = AF_INET6; | |
2351 | use_ipv6 = true; | |
2352 | } | |
d342894c | 2353 | |
34e02aa1 | 2354 | if (data[IFLA_VXLAN_LINK] && |
c7995c43 | 2355 | (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) { |
34e02aa1 | 2356 | struct net_device *lowerdev |
c7995c43 | 2357 | = __dev_get_by_index(net, dst->remote_ifindex); |
34e02aa1 | 2358 | |
2359 | if (!lowerdev) { | |
c7995c43 | 2360 | pr_info("ifindex %d does not exist\n", dst->remote_ifindex); |
34e02aa1 | 2361 | return -ENODEV; |
2362 | } | |
d342894c | 2363 | |
e4c7ed41 CW |
2364 | #if IS_ENABLED(CONFIG_IPV6) |
2365 | if (use_ipv6) { | |
2366 | struct inet6_dev *idev = __in6_dev_get(lowerdev); | |
2367 | if (idev && idev->cnf.disable_ipv6) { | |
2368 | pr_info("IPv6 is disabled via sysctl\n"); | |
2369 | return -EPERM; | |
2370 | } | |
2371 | vxlan->flags |= VXLAN_F_IPV6; | |
2372 | } | |
2373 | #endif | |
2374 | ||
34e02aa1 | 2375 | if (!tb[IFLA_MTU]) |
e4c7ed41 | 2376 | dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); |
1ba56fb4 AD |
2377 | |
2378 | /* update header length based on lower device */ | |
2379 | dev->hard_header_len = lowerdev->hard_header_len + | |
e4c7ed41 | 2380 | (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); |
d342894c | 2381 | } |
2382 | ||
2383 | if (data[IFLA_VXLAN_TOS]) | |
2384 | vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); | |
2385 | ||
afb97186 VB |
2386 | if (data[IFLA_VXLAN_TTL]) |
2387 | vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); | |
2388 | ||
d342894c | 2389 | if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
e4f67add | 2390 | vxlan->flags |= VXLAN_F_LEARN; |
d342894c | 2391 | |
2392 | if (data[IFLA_VXLAN_AGEING]) | |
2393 | vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); | |
2394 | else | |
2395 | vxlan->age_interval = FDB_AGE_DEFAULT; | |
2396 | ||
e4f67add DS |
2397 | if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY])) |
2398 | vxlan->flags |= VXLAN_F_PROXY; | |
2399 | ||
2400 | if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC])) | |
2401 | vxlan->flags |= VXLAN_F_RSC; | |
2402 | ||
2403 | if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS])) | |
2404 | vxlan->flags |= VXLAN_F_L2MISS; | |
2405 | ||
2406 | if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS])) | |
2407 | vxlan->flags |= VXLAN_F_L3MISS; | |
2408 | ||
d342894c | 2409 | if (data[IFLA_VXLAN_LIMIT]) |
2410 | vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); | |
2411 | ||
05f47d69 | 2412 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
2413 | const struct ifla_vxlan_port_range *p | |
2414 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
2415 | vxlan->port_min = ntohs(p->low); | |
2416 | vxlan->port_max = ntohs(p->high); | |
2417 | } | |
2418 | ||
823aa873 | 2419 | if (data[IFLA_VXLAN_PORT]) |
2420 | vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); | |
2421 | ||
553675fb | 2422 | if (vxlan_find_vni(net, vni, vxlan->dst_port)) { |
2423 | pr_info("duplicate VNI %u\n", vni); | |
2424 | return -EEXIST; | |
2425 | } | |
2426 | ||
1b13c97f YB |
2427 | SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops); |
2428 | ||
afbd8bae MR |
2429 | /* create an fdb entry for default destination */ |
2430 | err = vxlan_fdb_create(vxlan, all_zeros_mac, | |
e4c7ed41 | 2431 | &vxlan->default_dst.remote_ip, |
afbd8bae MR |
2432 | NUD_REACHABLE|NUD_PERMANENT, |
2433 | NLM_F_EXCL|NLM_F_CREATE, | |
2434 | vxlan->dst_port, vxlan->default_dst.remote_vni, | |
2435 | vxlan->default_dst.remote_ifindex, NTF_SELF); | |
1c51a915 | 2436 | if (err) |
553675fb | 2437 | return err; |
d342894c | 2438 | |
afbd8bae MR |
2439 | err = register_netdevice(dev); |
2440 | if (err) { | |
ba609e9b | 2441 | vxlan_fdb_delete_default(vxlan); |
afbd8bae MR |
2442 | return err; |
2443 | } | |
2444 | ||
553675fb | 2445 | list_add(&vxlan->next, &vn->vxlan_list); |
553675fb | 2446 | |
2447 | return 0; | |
d342894c | 2448 | } |
2449 | ||
2450 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) | |
2451 | { | |
fe5c3561 | 2452 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
d342894c | 2453 | struct vxlan_dev *vxlan = netdev_priv(dev); |
2454 | ||
fe5c3561 | 2455 | spin_lock(&vn->sock_lock); |
9c2e24e1 PS |
2456 | if (!hlist_unhashed(&vxlan->hlist)) |
2457 | hlist_del_rcu(&vxlan->hlist); | |
fe5c3561 | 2458 | spin_unlock(&vn->sock_lock); |
2459 | ||
553675fb | 2460 | list_del(&vxlan->next); |
d342894c | 2461 | unregister_netdevice_queue(dev, head); |
2462 | } | |
2463 | ||
2464 | static size_t vxlan_get_size(const struct net_device *dev) | |
2465 | { | |
2466 | ||
2467 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ | |
e4c7ed41 | 2468 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */ |
d342894c | 2469 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
e4c7ed41 | 2470 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */ |
d342894c | 2471 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
2472 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ | |
2473 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ | |
e4f67add DS |
2474 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
2475 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ | |
2476 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ | |
2477 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ | |
d342894c | 2478 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
2479 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ | |
05f47d69 | 2480 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
823aa873 | 2481 | nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */ |
d342894c | 2482 | 0; |
2483 | } | |
2484 | ||
2485 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
2486 | { | |
2487 | const struct vxlan_dev *vxlan = netdev_priv(dev); | |
c7995c43 | 2488 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
05f47d69 | 2489 | struct ifla_vxlan_port_range ports = { |
2490 | .low = htons(vxlan->port_min), | |
2491 | .high = htons(vxlan->port_max), | |
2492 | }; | |
d342894c | 2493 | |
c7995c43 | 2494 | if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni)) |
d342894c | 2495 | goto nla_put_failure; |
2496 | ||
e4c7ed41 CW |
2497 | if (!vxlan_addr_any(&dst->remote_ip)) { |
2498 | if (dst->remote_ip.sa.sa_family == AF_INET) { | |
2499 | if (nla_put_be32(skb, IFLA_VXLAN_GROUP, | |
2500 | dst->remote_ip.sin.sin_addr.s_addr)) | |
2501 | goto nla_put_failure; | |
2502 | #if IS_ENABLED(CONFIG_IPV6) | |
2503 | } else { | |
2504 | if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr), | |
2505 | &dst->remote_ip.sin6.sin6_addr)) | |
2506 | goto nla_put_failure; | |
2507 | #endif | |
2508 | } | |
2509 | } | |
d342894c | 2510 | |
c7995c43 | 2511 | if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) |
d342894c | 2512 | goto nla_put_failure; |
2513 | ||
e4c7ed41 CW |
2514 | if (!vxlan_addr_any(&vxlan->saddr)) { |
2515 | if (vxlan->saddr.sa.sa_family == AF_INET) { | |
2516 | if (nla_put_be32(skb, IFLA_VXLAN_LOCAL, | |
2517 | vxlan->saddr.sin.sin_addr.s_addr)) | |
2518 | goto nla_put_failure; | |
2519 | #if IS_ENABLED(CONFIG_IPV6) | |
2520 | } else { | |
2521 | if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr), | |
2522 | &vxlan->saddr.sin6.sin6_addr)) | |
2523 | goto nla_put_failure; | |
2524 | #endif | |
2525 | } | |
2526 | } | |
d342894c | 2527 | |
2528 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) || | |
2529 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) || | |
e4f67add DS |
2530 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
2531 | !!(vxlan->flags & VXLAN_F_LEARN)) || | |
2532 | nla_put_u8(skb, IFLA_VXLAN_PROXY, | |
2533 | !!(vxlan->flags & VXLAN_F_PROXY)) || | |
2534 | nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) || | |
2535 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, | |
2536 | !!(vxlan->flags & VXLAN_F_L2MISS)) || | |
2537 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, | |
2538 | !!(vxlan->flags & VXLAN_F_L3MISS)) || | |
d342894c | 2539 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) || |
823aa873 | 2540 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) || |
2541 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port)) | |
d342894c | 2542 | goto nla_put_failure; |
2543 | ||
05f47d69 | 2544 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
2545 | goto nla_put_failure; | |
2546 | ||
d342894c | 2547 | return 0; |
2548 | ||
2549 | nla_put_failure: | |
2550 | return -EMSGSIZE; | |
2551 | } | |
2552 | ||
2553 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { | |
2554 | .kind = "vxlan", | |
2555 | .maxtype = IFLA_VXLAN_MAX, | |
2556 | .policy = vxlan_policy, | |
2557 | .priv_size = sizeof(struct vxlan_dev), | |
2558 | .setup = vxlan_setup, | |
2559 | .validate = vxlan_validate, | |
2560 | .newlink = vxlan_newlink, | |
2561 | .dellink = vxlan_dellink, | |
2562 | .get_size = vxlan_get_size, | |
2563 | .fill_info = vxlan_fill_info, | |
2564 | }; | |
2565 | ||
2566 | static __net_init int vxlan_init_net(struct net *net) | |
2567 | { | |
2568 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
31fec5aa | 2569 | unsigned int h; |
d342894c | 2570 | |
553675fb | 2571 | INIT_LIST_HEAD(&vn->vxlan_list); |
1c51a915 | 2572 | spin_lock_init(&vn->sock_lock); |
d342894c | 2573 | |
553675fb | 2574 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
2575 | INIT_HLIST_HEAD(&vn->sock_list[h]); | |
d342894c | 2576 | |
2577 | return 0; | |
2578 | } | |
2579 | ||
2580 | static __net_exit void vxlan_exit_net(struct net *net) | |
2581 | { | |
2582 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
9cb6cb7e | 2583 | struct vxlan_dev *vxlan; |
372675a4 | 2584 | LIST_HEAD(list); |
9cb6cb7e ZM |
2585 | |
2586 | rtnl_lock(); | |
553675fb | 2587 | list_for_each_entry(vxlan, &vn->vxlan_list, next) |
372675a4 | 2588 | unregister_netdevice_queue(vxlan->dev, &list); |
2589 | unregister_netdevice_many(&list); | |
9cb6cb7e | 2590 | rtnl_unlock(); |
d342894c | 2591 | } |
2592 | ||
2593 | static struct pernet_operations vxlan_net_ops = { | |
2594 | .init = vxlan_init_net, | |
2595 | .exit = vxlan_exit_net, | |
2596 | .id = &vxlan_net_id, | |
2597 | .size = sizeof(struct vxlan_net), | |
2598 | }; | |
2599 | ||
2600 | static int __init vxlan_init_module(void) | |
2601 | { | |
2602 | int rc; | |
2603 | ||
758c57d1 SH |
2604 | vxlan_wq = alloc_workqueue("vxlan", 0, 0); |
2605 | if (!vxlan_wq) | |
2606 | return -ENOMEM; | |
2607 | ||
d342894c | 2608 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); |
2609 | ||
2610 | rc = register_pernet_device(&vxlan_net_ops); | |
2611 | if (rc) | |
2612 | goto out1; | |
2613 | ||
2614 | rc = rtnl_link_register(&vxlan_link_ops); | |
2615 | if (rc) | |
2616 | goto out2; | |
2617 | ||
2618 | return 0; | |
2619 | ||
2620 | out2: | |
2621 | unregister_pernet_device(&vxlan_net_ops); | |
2622 | out1: | |
758c57d1 | 2623 | destroy_workqueue(vxlan_wq); |
d342894c | 2624 | return rc; |
2625 | } | |
7332a13b | 2626 | late_initcall(vxlan_init_module); |
d342894c | 2627 | |
2628 | static void __exit vxlan_cleanup_module(void) | |
2629 | { | |
b7153984 | 2630 | rtnl_link_unregister(&vxlan_link_ops); |
758c57d1 | 2631 | destroy_workqueue(vxlan_wq); |
f89e57c4 | 2632 | unregister_pernet_device(&vxlan_net_ops); |
6681712d | 2633 | rcu_barrier(); |
d342894c | 2634 | } |
2635 | module_exit(vxlan_cleanup_module); | |
2636 | ||
2637 | MODULE_LICENSE("GPL"); | |
2638 | MODULE_VERSION(VXLAN_VERSION); | |
3b8df3c6 | 2639 | MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); |
d342894c | 2640 | MODULE_ALIAS_RTNL_LINK("vxlan"); |