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