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