]>
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> | |
d342894c | 14 | #include <linux/module.h> |
15 | #include <linux/errno.h> | |
16 | #include <linux/slab.h> | |
d342894c | 17 | #include <linux/udp.h> |
18 | #include <linux/igmp.h> | |
d342894c | 19 | #include <linux/if_ether.h> |
1b13c97f | 20 | #include <linux/ethtool.h> |
e4f67add DS |
21 | #include <net/arp.h> |
22 | #include <net/ndisc.h> | |
d342894c | 23 | #include <net/ip.h> |
24 | #include <net/icmp.h> | |
d342894c | 25 | #include <net/rtnetlink.h> |
d342894c | 26 | #include <net/inet_ecn.h> |
27 | #include <net/net_namespace.h> | |
28 | #include <net/netns/generic.h> | |
fa20e0e3 | 29 | #include <net/tun_proto.h> |
012a5729 | 30 | #include <net/vxlan.h> |
40d29af0 | 31 | |
e4c7ed41 | 32 | #if IS_ENABLED(CONFIG_IPV6) |
e4c7ed41 | 33 | #include <net/ip6_tunnel.h> |
660d98ca | 34 | #include <net/ip6_checksum.h> |
e4c7ed41 | 35 | #endif |
d342894c | 36 | |
37 | #define VXLAN_VERSION "0.1" | |
38 | ||
553675fb | 39 | #define PORT_HASH_BITS 8 |
40 | #define PORT_HASH_SIZE (1<<PORT_HASH_BITS) | |
d342894c | 41 | #define FDB_AGE_DEFAULT 300 /* 5 min */ |
42 | #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */ | |
43 | ||
23c578bf | 44 | /* UDP port for VXLAN traffic. |
45 | * The IANA assigned port is 4789, but the Linux default is 8472 | |
234f5b73 | 46 | * for compatibility with early adopters. |
23c578bf | 47 | */ |
9daaa397 SH |
48 | static unsigned short vxlan_port __read_mostly = 8472; |
49 | module_param_named(udp_port, vxlan_port, ushort, 0444); | |
d342894c | 50 | MODULE_PARM_DESC(udp_port, "Destination UDP port"); |
51 | ||
52 | static bool log_ecn_error = true; | |
53 | module_param(log_ecn_error, bool, 0644); | |
54 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); | |
55 | ||
c7d03a00 | 56 | static unsigned int vxlan_net_id; |
0dfbdf41 | 57 | static struct rtnl_link_ops vxlan_link_ops; |
553675fb | 58 | |
7256eac1 | 59 | static const u8 all_zeros_mac[ETH_ALEN + 2]; |
afbd8bae | 60 | |
205f356d | 61 | static int vxlan_sock_add(struct vxlan_dev *vxlan); |
614732ea | 62 | |
a53cb29b MB |
63 | static void vxlan_vs_del_dev(struct vxlan_dev *vxlan); |
64 | ||
553675fb | 65 | /* per-network namespace private data for this module */ |
66 | struct vxlan_net { | |
67 | struct list_head vxlan_list; | |
68 | struct hlist_head sock_list[PORT_HASH_SIZE]; | |
1c51a915 | 69 | spinlock_t sock_lock; |
553675fb | 70 | }; |
71 | ||
d342894c | 72 | /* Forwarding table entry */ |
73 | struct vxlan_fdb { | |
74 | struct hlist_node hlist; /* linked list of entries */ | |
75 | struct rcu_head rcu; | |
76 | unsigned long updated; /* jiffies */ | |
77 | unsigned long used; | |
3e61aa8f | 78 | struct list_head remotes; |
7177a3b0 | 79 | u8 eth_addr[ETH_ALEN]; |
d342894c | 80 | u16 state; /* see ndm_state */ |
3ad7a4b1 | 81 | __be32 vni; |
ae884082 | 82 | u8 flags; /* see ndm_flags */ |
d342894c | 83 | }; |
84 | ||
d342894c | 85 | /* salt for hash table */ |
86 | static u32 vxlan_salt __read_mostly; | |
87 | ||
ee122c79 TG |
88 | static inline bool vxlan_collect_metadata(struct vxlan_sock *vs) |
89 | { | |
e7030878 TG |
90 | return vs->flags & VXLAN_F_COLLECT_METADATA || |
91 | ip_tunnel_collect_metadata(); | |
ee122c79 TG |
92 | } |
93 | ||
e4c7ed41 CW |
94 | #if IS_ENABLED(CONFIG_IPV6) |
95 | static inline | |
96 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) | |
97 | { | |
f0ef3126 JB |
98 | if (a->sa.sa_family != b->sa.sa_family) |
99 | return false; | |
100 | if (a->sa.sa_family == AF_INET6) | |
101 | return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr); | |
102 | else | |
103 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; | |
e4c7ed41 CW |
104 | } |
105 | ||
e4c7ed41 CW |
106 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) |
107 | { | |
f0ef3126 | 108 | if (nla_len(nla) >= sizeof(struct in6_addr)) { |
67b61f6c | 109 | ip->sin6.sin6_addr = nla_get_in6_addr(nla); |
f0ef3126 JB |
110 | ip->sa.sa_family = AF_INET6; |
111 | return 0; | |
112 | } else if (nla_len(nla) >= sizeof(__be32)) { | |
67b61f6c | 113 | ip->sin.sin_addr.s_addr = nla_get_in_addr(nla); |
f0ef3126 JB |
114 | ip->sa.sa_family = AF_INET; |
115 | return 0; | |
116 | } else { | |
117 | return -EAFNOSUPPORT; | |
118 | } | |
e4c7ed41 CW |
119 | } |
120 | ||
121 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, | |
f0ef3126 | 122 | const union vxlan_addr *ip) |
e4c7ed41 | 123 | { |
f0ef3126 | 124 | if (ip->sa.sa_family == AF_INET6) |
930345ea | 125 | return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr); |
f0ef3126 | 126 | else |
930345ea | 127 | return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr); |
e4c7ed41 CW |
128 | } |
129 | ||
130 | #else /* !CONFIG_IPV6 */ | |
131 | ||
132 | static inline | |
133 | bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b) | |
134 | { | |
f0ef3126 | 135 | return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr; |
e4c7ed41 CW |
136 | } |
137 | ||
e4c7ed41 CW |
138 | static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla) |
139 | { | |
f0ef3126 JB |
140 | if (nla_len(nla) >= sizeof(struct in6_addr)) { |
141 | return -EAFNOSUPPORT; | |
142 | } else if (nla_len(nla) >= sizeof(__be32)) { | |
67b61f6c | 143 | ip->sin.sin_addr.s_addr = nla_get_in_addr(nla); |
f0ef3126 JB |
144 | ip->sa.sa_family = AF_INET; |
145 | return 0; | |
146 | } else { | |
147 | return -EAFNOSUPPORT; | |
148 | } | |
e4c7ed41 CW |
149 | } |
150 | ||
151 | static int vxlan_nla_put_addr(struct sk_buff *skb, int attr, | |
f0ef3126 | 152 | const union vxlan_addr *ip) |
e4c7ed41 | 153 | { |
930345ea | 154 | return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr); |
e4c7ed41 CW |
155 | } |
156 | #endif | |
157 | ||
553675fb | 158 | /* Virtual Network hash table head */ |
54bfd872 | 159 | static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni) |
553675fb | 160 | { |
54bfd872 | 161 | return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)]; |
553675fb | 162 | } |
163 | ||
164 | /* Socket hash table head */ | |
165 | static inline struct hlist_head *vs_head(struct net *net, __be16 port) | |
d342894c | 166 | { |
167 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
168 | ||
553675fb | 169 | return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)]; |
170 | } | |
171 | ||
3e61aa8f SH |
172 | /* First remote destination for a forwarding entry. |
173 | * Guaranteed to be non-NULL because remotes are never deleted. | |
174 | */ | |
5ca5461c | 175 | static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb) |
3e61aa8f | 176 | { |
5ca5461c | 177 | return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list); |
178 | } | |
179 | ||
180 | static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb) | |
181 | { | |
182 | return list_first_entry(&fdb->remotes, struct vxlan_rdst, list); | |
3e61aa8f SH |
183 | } |
184 | ||
ac5132d1 TG |
185 | /* Find VXLAN socket based on network namespace, address family and UDP port |
186 | * and enabled unshareable flags. | |
187 | */ | |
188 | static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family, | |
189 | __be16 port, u32 flags) | |
553675fb | 190 | { |
191 | struct vxlan_sock *vs; | |
af33c1ad TH |
192 | |
193 | flags &= VXLAN_F_RCV_FLAGS; | |
553675fb | 194 | |
195 | hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) { | |
19ca9fc1 | 196 | if (inet_sk(vs->sock->sk)->inet_sport == port && |
705cc62f | 197 | vxlan_get_sk_family(vs) == family && |
af33c1ad | 198 | vs->flags == flags) |
553675fb | 199 | return vs; |
200 | } | |
201 | return NULL; | |
d342894c | 202 | } |
203 | ||
49f810f0 MS |
204 | static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex, |
205 | __be32 vni) | |
d342894c | 206 | { |
69e76661 | 207 | struct vxlan_dev_node *node; |
d342894c | 208 | |
b9167b2e JB |
209 | /* For flow based devices, map all packets to VNI 0 */ |
210 | if (vs->flags & VXLAN_F_COLLECT_METADATA) | |
211 | vni = 0; | |
212 | ||
69e76661 JB |
213 | hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) { |
214 | if (node->vxlan->default_dst.remote_vni != vni) | |
49f810f0 MS |
215 | continue; |
216 | ||
217 | if (IS_ENABLED(CONFIG_IPV6)) { | |
69e76661 | 218 | const struct vxlan_config *cfg = &node->vxlan->cfg; |
49f810f0 MS |
219 | |
220 | if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) && | |
221 | cfg->remote_ifindex != ifindex) | |
222 | continue; | |
223 | } | |
224 | ||
69e76661 | 225 | return node->vxlan; |
d342894c | 226 | } |
227 | ||
228 | return NULL; | |
229 | } | |
230 | ||
5cfccc5a | 231 | /* Look up VNI in a per net namespace table */ |
49f810f0 MS |
232 | static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex, |
233 | __be32 vni, sa_family_t family, | |
234 | __be16 port, u32 flags) | |
5cfccc5a PS |
235 | { |
236 | struct vxlan_sock *vs; | |
237 | ||
ac5132d1 | 238 | vs = vxlan_find_sock(net, family, port, flags); |
5cfccc5a PS |
239 | if (!vs) |
240 | return NULL; | |
241 | ||
49f810f0 | 242 | return vxlan_vs_find_vni(vs, ifindex, vni); |
5cfccc5a PS |
243 | } |
244 | ||
d342894c | 245 | /* Fill in neighbour message in skbuff. */ |
246 | static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, | |
234f5b73 SH |
247 | const struct vxlan_fdb *fdb, |
248 | u32 portid, u32 seq, int type, unsigned int flags, | |
249 | const struct vxlan_rdst *rdst) | |
d342894c | 250 | { |
251 | unsigned long now = jiffies; | |
252 | struct nda_cacheinfo ci; | |
253 | struct nlmsghdr *nlh; | |
254 | struct ndmsg *ndm; | |
e4f67add | 255 | bool send_ip, send_eth; |
d342894c | 256 | |
257 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags); | |
258 | if (nlh == NULL) | |
259 | return -EMSGSIZE; | |
260 | ||
261 | ndm = nlmsg_data(nlh); | |
262 | memset(ndm, 0, sizeof(*ndm)); | |
e4f67add DS |
263 | |
264 | send_eth = send_ip = true; | |
265 | ||
266 | if (type == RTM_GETNEIGH) { | |
e4c7ed41 | 267 | send_ip = !vxlan_addr_any(&rdst->remote_ip); |
e4f67add | 268 | send_eth = !is_zero_ether_addr(fdb->eth_addr); |
8f48ba71 | 269 | ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET; |
e4f67add DS |
270 | } else |
271 | ndm->ndm_family = AF_BRIDGE; | |
d342894c | 272 | ndm->ndm_state = fdb->state; |
273 | ndm->ndm_ifindex = vxlan->dev->ifindex; | |
ae884082 | 274 | ndm->ndm_flags = fdb->flags; |
545469f7 | 275 | ndm->ndm_type = RTN_UNICAST; |
d342894c | 276 | |
193523bf | 277 | if (!net_eq(dev_net(vxlan->dev), vxlan->net) && |
4967082b | 278 | nla_put_s32(skb, NDA_LINK_NETNSID, |
38f507f1 | 279 | peernet2id(dev_net(vxlan->dev), vxlan->net))) |
193523bf ND |
280 | goto nla_put_failure; |
281 | ||
e4f67add | 282 | if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr)) |
d342894c | 283 | goto nla_put_failure; |
284 | ||
e4c7ed41 | 285 | if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip)) |
6681712d DS |
286 | goto nla_put_failure; |
287 | ||
0dfbdf41 | 288 | if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port && |
6681712d DS |
289 | nla_put_be16(skb, NDA_PORT, rdst->remote_port)) |
290 | goto nla_put_failure; | |
c7995c43 | 291 | if (rdst->remote_vni != vxlan->default_dst.remote_vni && |
54bfd872 | 292 | nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni))) |
6681712d | 293 | goto nla_put_failure; |
dc5321d7 | 294 | if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni && |
3ad7a4b1 RP |
295 | nla_put_u32(skb, NDA_SRC_VNI, |
296 | be32_to_cpu(fdb->vni))) | |
297 | goto nla_put_failure; | |
6681712d DS |
298 | if (rdst->remote_ifindex && |
299 | nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex)) | |
d342894c | 300 | goto nla_put_failure; |
301 | ||
302 | ci.ndm_used = jiffies_to_clock_t(now - fdb->used); | |
303 | ci.ndm_confirmed = 0; | |
304 | ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated); | |
305 | ci.ndm_refcnt = 0; | |
306 | ||
307 | if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci)) | |
308 | goto nla_put_failure; | |
309 | ||
053c095a JB |
310 | nlmsg_end(skb, nlh); |
311 | return 0; | |
d342894c | 312 | |
313 | nla_put_failure: | |
314 | nlmsg_cancel(skb, nlh); | |
315 | return -EMSGSIZE; | |
316 | } | |
317 | ||
318 | static inline size_t vxlan_nlmsg_size(void) | |
319 | { | |
320 | return NLMSG_ALIGN(sizeof(struct ndmsg)) | |
321 | + nla_total_size(ETH_ALEN) /* NDA_LLADDR */ | |
e4c7ed41 | 322 | + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */ |
73cf3317 | 323 | + nla_total_size(sizeof(__be16)) /* NDA_PORT */ |
6681712d DS |
324 | + nla_total_size(sizeof(__be32)) /* NDA_VNI */ |
325 | + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */ | |
4967082b | 326 | + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */ |
d342894c | 327 | + nla_total_size(sizeof(struct nda_cacheinfo)); |
328 | } | |
329 | ||
9a997353 PM |
330 | static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, |
331 | struct vxlan_rdst *rd, int type) | |
d342894c | 332 | { |
333 | struct net *net = dev_net(vxlan->dev); | |
334 | struct sk_buff *skb; | |
335 | int err = -ENOBUFS; | |
336 | ||
337 | skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC); | |
338 | if (skb == NULL) | |
339 | goto errout; | |
340 | ||
9e4b93f9 | 341 | err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd); |
d342894c | 342 | if (err < 0) { |
343 | /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */ | |
344 | WARN_ON(err == -EMSGSIZE); | |
345 | kfree_skb(skb); | |
346 | goto errout; | |
347 | } | |
348 | ||
349 | rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); | |
350 | return; | |
351 | errout: | |
352 | if (err < 0) | |
353 | rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); | |
354 | } | |
355 | ||
9a997353 PM |
356 | static void vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan, |
357 | struct vxlan_fdb *fdb, | |
358 | struct vxlan_rdst *rd, | |
359 | bool adding) | |
360 | { | |
361 | struct switchdev_notifier_vxlan_fdb_info info; | |
362 | enum switchdev_notifier_type notifier_type; | |
363 | ||
364 | if (WARN_ON(!rd)) | |
365 | return; | |
366 | ||
367 | notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE | |
368 | : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE; | |
369 | ||
370 | info = (struct switchdev_notifier_vxlan_fdb_info){ | |
371 | .remote_ip = rd->remote_ip, | |
372 | .remote_port = rd->remote_port, | |
373 | .remote_vni = rd->remote_vni, | |
374 | .remote_ifindex = rd->remote_ifindex, | |
375 | .vni = fdb->vni, | |
376 | }; | |
377 | memcpy(info.eth_addr, fdb->eth_addr, ETH_ALEN); | |
378 | ||
379 | call_switchdev_notifiers(notifier_type, vxlan->dev, | |
380 | &info.info); | |
381 | } | |
382 | ||
383 | static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb, | |
384 | struct vxlan_rdst *rd, int type) | |
385 | { | |
386 | switch (type) { | |
387 | case RTM_NEWNEIGH: | |
388 | vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd, true); | |
389 | break; | |
390 | case RTM_DELNEIGH: | |
391 | vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd, false); | |
392 | break; | |
393 | } | |
394 | ||
395 | __vxlan_fdb_notify(vxlan, fdb, rd, type); | |
396 | } | |
397 | ||
e4c7ed41 | 398 | static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa) |
e4f67add DS |
399 | { |
400 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
bb3fd687 SH |
401 | struct vxlan_fdb f = { |
402 | .state = NUD_STALE, | |
403 | }; | |
404 | struct vxlan_rdst remote = { | |
e4c7ed41 | 405 | .remote_ip = *ipa, /* goes to NDA_DST */ |
54bfd872 | 406 | .remote_vni = cpu_to_be32(VXLAN_N_VID), |
bb3fd687 | 407 | }; |
3e61aa8f | 408 | |
9e4b93f9 | 409 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH); |
e4f67add DS |
410 | } |
411 | ||
412 | static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN]) | |
413 | { | |
bb3fd687 SH |
414 | struct vxlan_fdb f = { |
415 | .state = NUD_STALE, | |
416 | }; | |
9e4b93f9 | 417 | struct vxlan_rdst remote = { }; |
e4f67add | 418 | |
e4f67add DS |
419 | memcpy(f.eth_addr, eth_addr, ETH_ALEN); |
420 | ||
9e4b93f9 | 421 | vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH); |
e4f67add DS |
422 | } |
423 | ||
d342894c | 424 | /* Hash Ethernet address */ |
425 | static u32 eth_hash(const unsigned char *addr) | |
426 | { | |
427 | u64 value = get_unaligned((u64 *)addr); | |
428 | ||
429 | /* only want 6 bytes */ | |
430 | #ifdef __BIG_ENDIAN | |
d342894c | 431 | value >>= 16; |
321fb991 | 432 | #else |
433 | value <<= 16; | |
d342894c | 434 | #endif |
435 | return hash_64(value, FDB_HASH_BITS); | |
436 | } | |
437 | ||
3ad7a4b1 RP |
438 | static u32 eth_vni_hash(const unsigned char *addr, __be32 vni) |
439 | { | |
440 | /* use 1 byte of OUI and 3 bytes of NIC */ | |
441 | u32 key = get_unaligned((u32 *)(addr + 2)); | |
442 | ||
443 | return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1); | |
444 | } | |
445 | ||
d342894c | 446 | /* Hash chain to use given mac address */ |
447 | static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan, | |
3ad7a4b1 | 448 | const u8 *mac, __be32 vni) |
d342894c | 449 | { |
dc5321d7 | 450 | if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) |
3ad7a4b1 RP |
451 | return &vxlan->fdb_head[eth_vni_hash(mac, vni)]; |
452 | else | |
453 | return &vxlan->fdb_head[eth_hash(mac)]; | |
d342894c | 454 | } |
455 | ||
456 | /* Look up Ethernet address in forwarding table */ | |
014be2c8 | 457 | static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan, |
3ad7a4b1 | 458 | const u8 *mac, __be32 vni) |
d342894c | 459 | { |
3ad7a4b1 | 460 | struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni); |
d342894c | 461 | struct vxlan_fdb *f; |
d342894c | 462 | |
b67bfe0d | 463 | hlist_for_each_entry_rcu(f, head, hlist) { |
3ad7a4b1 | 464 | if (ether_addr_equal(mac, f->eth_addr)) { |
dc5321d7 | 465 | if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) { |
3ad7a4b1 RP |
466 | if (vni == f->vni) |
467 | return f; | |
468 | } else { | |
469 | return f; | |
470 | } | |
471 | } | |
d342894c | 472 | } |
473 | ||
474 | return NULL; | |
475 | } | |
476 | ||
014be2c8 | 477 | static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan, |
3ad7a4b1 | 478 | const u8 *mac, __be32 vni) |
014be2c8 SS |
479 | { |
480 | struct vxlan_fdb *f; | |
481 | ||
3ad7a4b1 | 482 | f = __vxlan_find_mac(vxlan, mac, vni); |
016f3d18 | 483 | if (f && f->used != jiffies) |
014be2c8 SS |
484 | f->used = jiffies; |
485 | ||
486 | return f; | |
487 | } | |
488 | ||
a5e7c10a MR |
489 | /* caller should hold vxlan->hash_lock */ |
490 | static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f, | |
e4c7ed41 | 491 | union vxlan_addr *ip, __be16 port, |
54bfd872 | 492 | __be32 vni, __u32 ifindex) |
6681712d | 493 | { |
3e61aa8f | 494 | struct vxlan_rdst *rd; |
6681712d | 495 | |
3e61aa8f | 496 | list_for_each_entry(rd, &f->remotes, list) { |
e4c7ed41 | 497 | if (vxlan_addr_equal(&rd->remote_ip, ip) && |
6681712d DS |
498 | rd->remote_port == port && |
499 | rd->remote_vni == vni && | |
500 | rd->remote_ifindex == ifindex) | |
a5e7c10a | 501 | return rd; |
6681712d | 502 | } |
3e61aa8f | 503 | |
a5e7c10a MR |
504 | return NULL; |
505 | } | |
506 | ||
906dc186 TR |
507 | /* Replace destination of unicast mac */ |
508 | static int vxlan_fdb_replace(struct vxlan_fdb *f, | |
54bfd872 JB |
509 | union vxlan_addr *ip, __be16 port, __be32 vni, |
510 | __u32 ifindex) | |
906dc186 TR |
511 | { |
512 | struct vxlan_rdst *rd; | |
513 | ||
514 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); | |
515 | if (rd) | |
516 | return 0; | |
517 | ||
518 | rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list); | |
519 | if (!rd) | |
520 | return 0; | |
0c1d70af PA |
521 | |
522 | dst_cache_reset(&rd->dst_cache); | |
e4c7ed41 | 523 | rd->remote_ip = *ip; |
906dc186 TR |
524 | rd->remote_port = port; |
525 | rd->remote_vni = vni; | |
526 | rd->remote_ifindex = ifindex; | |
527 | return 1; | |
528 | } | |
529 | ||
a5e7c10a MR |
530 | /* Add/update destinations for multicast */ |
531 | static int vxlan_fdb_append(struct vxlan_fdb *f, | |
54bfd872 | 532 | union vxlan_addr *ip, __be16 port, __be32 vni, |
9e4b93f9 | 533 | __u32 ifindex, struct vxlan_rdst **rdp) |
a5e7c10a MR |
534 | { |
535 | struct vxlan_rdst *rd; | |
536 | ||
537 | rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex); | |
538 | if (rd) | |
539 | return 0; | |
540 | ||
6681712d DS |
541 | rd = kmalloc(sizeof(*rd), GFP_ATOMIC); |
542 | if (rd == NULL) | |
543 | return -ENOBUFS; | |
0c1d70af PA |
544 | |
545 | if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) { | |
546 | kfree(rd); | |
547 | return -ENOBUFS; | |
548 | } | |
549 | ||
e4c7ed41 | 550 | rd->remote_ip = *ip; |
6681712d DS |
551 | rd->remote_port = port; |
552 | rd->remote_vni = vni; | |
553 | rd->remote_ifindex = ifindex; | |
3e61aa8f SH |
554 | |
555 | list_add_tail_rcu(&rd->list, &f->remotes); | |
556 | ||
9e4b93f9 | 557 | *rdp = rd; |
6681712d DS |
558 | return 1; |
559 | } | |
560 | ||
dfd8645e TH |
561 | static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb, |
562 | unsigned int off, | |
563 | struct vxlanhdr *vh, size_t hdrlen, | |
54bfd872 JB |
564 | __be32 vni_field, |
565 | struct gro_remcsum *grc, | |
0ace2ca8 | 566 | bool nopartial) |
dfd8645e | 567 | { |
b7fe10e5 | 568 | size_t start, offset; |
dfd8645e TH |
569 | |
570 | if (skb->remcsum_offload) | |
b7fe10e5 | 571 | return vh; |
dfd8645e TH |
572 | |
573 | if (!NAPI_GRO_CB(skb)->csum_valid) | |
574 | return NULL; | |
575 | ||
54bfd872 JB |
576 | start = vxlan_rco_start(vni_field); |
577 | offset = start + vxlan_rco_offset(vni_field); | |
dfd8645e | 578 | |
b7fe10e5 TH |
579 | vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen, |
580 | start, offset, grc, nopartial); | |
dfd8645e TH |
581 | |
582 | skb->remcsum_offload = 1; | |
583 | ||
584 | return vh; | |
585 | } | |
586 | ||
d4546c25 DM |
587 | static struct sk_buff *vxlan_gro_receive(struct sock *sk, |
588 | struct list_head *head, | |
589 | struct sk_buff *skb) | |
dc01e7d3 | 590 | { |
d4546c25 DM |
591 | struct sk_buff *pp = NULL; |
592 | struct sk_buff *p; | |
dc01e7d3 | 593 | struct vxlanhdr *vh, *vh2; |
9b174d88 | 594 | unsigned int hlen, off_vx; |
dc01e7d3 | 595 | int flush = 1; |
5602c48c | 596 | struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk); |
54bfd872 | 597 | __be32 flags; |
26c4f7da TH |
598 | struct gro_remcsum grc; |
599 | ||
600 | skb_gro_remcsum_init(&grc); | |
dc01e7d3 OG |
601 | |
602 | off_vx = skb_gro_offset(skb); | |
603 | hlen = off_vx + sizeof(*vh); | |
604 | vh = skb_gro_header_fast(skb, off_vx); | |
605 | if (skb_gro_header_hard(skb, hlen)) { | |
606 | vh = skb_gro_header_slow(skb, hlen, off_vx); | |
607 | if (unlikely(!vh)) | |
608 | goto out; | |
609 | } | |
dc01e7d3 | 610 | |
dfd8645e TH |
611 | skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr)); |
612 | ||
54bfd872 | 613 | flags = vh->vx_flags; |
dfd8645e TH |
614 | |
615 | if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) { | |
616 | vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr), | |
54bfd872 | 617 | vh->vx_vni, &grc, |
0ace2ca8 TH |
618 | !!(vs->flags & |
619 | VXLAN_F_REMCSUM_NOPARTIAL)); | |
dfd8645e TH |
620 | |
621 | if (!vh) | |
622 | goto out; | |
623 | } | |
624 | ||
b7fe10e5 TH |
625 | skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */ |
626 | ||
d4546c25 | 627 | list_for_each_entry(p, head, list) { |
dc01e7d3 OG |
628 | if (!NAPI_GRO_CB(p)->same_flow) |
629 | continue; | |
630 | ||
631 | vh2 = (struct vxlanhdr *)(p->data + off_vx); | |
3511494c TG |
632 | if (vh->vx_flags != vh2->vx_flags || |
633 | vh->vx_vni != vh2->vx_vni) { | |
dc01e7d3 OG |
634 | NAPI_GRO_CB(p)->same_flow = 0; |
635 | continue; | |
636 | } | |
dc01e7d3 OG |
637 | } |
638 | ||
fcd91dd4 | 639 | pp = call_gro_receive(eth_gro_receive, head, skb); |
c194cf93 | 640 | flush = 0; |
dc01e7d3 | 641 | |
dc01e7d3 | 642 | out: |
603d4cf8 | 643 | skb_gro_flush_final_remcsum(skb, pp, flush, &grc); |
dc01e7d3 OG |
644 | |
645 | return pp; | |
646 | } | |
647 | ||
5602c48c | 648 | static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff) |
dc01e7d3 | 649 | { |
229740c6 JR |
650 | /* Sets 'skb->inner_mac_header' since we are always called with |
651 | * 'skb->encapsulation' set. | |
652 | */ | |
9b174d88 | 653 | return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr)); |
dc01e7d3 OG |
654 | } |
655 | ||
25e20e73 RP |
656 | static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, |
657 | const u8 *mac, __u16 state, | |
658 | __be32 src_vni, __u8 ndm_flags) | |
659 | { | |
660 | struct vxlan_fdb *f; | |
661 | ||
662 | f = kmalloc(sizeof(*f), GFP_ATOMIC); | |
663 | if (!f) | |
664 | return NULL; | |
665 | f->state = state; | |
666 | f->flags = ndm_flags; | |
667 | f->updated = f->used = jiffies; | |
668 | f->vni = src_vni; | |
669 | INIT_LIST_HEAD(&f->remotes); | |
670 | memcpy(f->eth_addr, mac, ETH_ALEN); | |
671 | ||
672 | return f; | |
673 | } | |
674 | ||
d342894c | 675 | static int vxlan_fdb_create(struct vxlan_dev *vxlan, |
25e20e73 RP |
676 | const u8 *mac, union vxlan_addr *ip, |
677 | __u16 state, __be16 port, __be32 src_vni, | |
678 | __be32 vni, __u32 ifindex, __u8 ndm_flags, | |
679 | struct vxlan_fdb **fdb) | |
680 | { | |
681 | struct vxlan_rdst *rd = NULL; | |
682 | struct vxlan_fdb *f; | |
683 | int rc; | |
684 | ||
685 | if (vxlan->cfg.addrmax && | |
686 | vxlan->addrcnt >= vxlan->cfg.addrmax) | |
687 | return -ENOSPC; | |
688 | ||
689 | netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); | |
690 | f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags); | |
691 | if (!f) | |
692 | return -ENOMEM; | |
693 | ||
694 | rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); | |
695 | if (rc < 0) { | |
696 | kfree(f); | |
697 | return rc; | |
698 | } | |
699 | ||
700 | ++vxlan->addrcnt; | |
701 | hlist_add_head_rcu(&f->hlist, | |
702 | vxlan_fdb_head(vxlan, mac, src_vni)); | |
703 | ||
704 | *fdb = f; | |
705 | ||
706 | return 0; | |
707 | } | |
708 | ||
709 | /* Add new entry to forwarding table -- assumes lock held */ | |
710 | static int vxlan_fdb_update(struct vxlan_dev *vxlan, | |
e4c7ed41 | 711 | const u8 *mac, union vxlan_addr *ip, |
6681712d | 712 | __u16 state, __u16 flags, |
3ad7a4b1 RP |
713 | __be16 port, __be32 src_vni, __be32 vni, |
714 | __u32 ifindex, __u8 ndm_flags) | |
d342894c | 715 | { |
0813e957 | 716 | __u8 fdb_flags = (ndm_flags & ~NTF_USE); |
9e4b93f9 | 717 | struct vxlan_rdst *rd = NULL; |
d342894c | 718 | struct vxlan_fdb *f; |
719 | int notify = 0; | |
17b46365 | 720 | int rc; |
d342894c | 721 | |
3ad7a4b1 | 722 | f = __vxlan_find_mac(vxlan, mac, src_vni); |
d342894c | 723 | if (f) { |
724 | if (flags & NLM_F_EXCL) { | |
725 | netdev_dbg(vxlan->dev, | |
726 | "lost race to create %pM\n", mac); | |
727 | return -EEXIST; | |
728 | } | |
729 | if (f->state != state) { | |
730 | f->state = state; | |
731 | f->updated = jiffies; | |
732 | notify = 1; | |
733 | } | |
0813e957 RP |
734 | if (f->flags != fdb_flags) { |
735 | f->flags = fdb_flags; | |
ae884082 DS |
736 | f->updated = jiffies; |
737 | notify = 1; | |
738 | } | |
906dc186 TR |
739 | if ((flags & NLM_F_REPLACE)) { |
740 | /* Only change unicasts */ | |
741 | if (!(is_multicast_ether_addr(f->eth_addr) || | |
742 | is_zero_ether_addr(f->eth_addr))) { | |
60840429 | 743 | notify |= vxlan_fdb_replace(f, ip, port, vni, |
906dc186 | 744 | ifindex); |
906dc186 TR |
745 | } else |
746 | return -EOPNOTSUPP; | |
747 | } | |
6681712d | 748 | if ((flags & NLM_F_APPEND) && |
58e4c767 MR |
749 | (is_multicast_ether_addr(f->eth_addr) || |
750 | is_zero_ether_addr(f->eth_addr))) { | |
17b46365 | 751 | rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd); |
6681712d DS |
752 | |
753 | if (rc < 0) | |
754 | return rc; | |
755 | notify |= rc; | |
756 | } | |
0813e957 RP |
757 | |
758 | if (ndm_flags & NTF_USE) | |
759 | f->used = jiffies; | |
d342894c | 760 | } else { |
761 | if (!(flags & NLM_F_CREATE)) | |
762 | return -ENOENT; | |
763 | ||
906dc186 TR |
764 | /* Disallow replace to add a multicast entry */ |
765 | if ((flags & NLM_F_REPLACE) && | |
766 | (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac))) | |
767 | return -EOPNOTSUPP; | |
768 | ||
e4c7ed41 | 769 | netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip); |
25e20e73 | 770 | rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni, |
0813e957 | 771 | vni, ifindex, fdb_flags, &f); |
25e20e73 | 772 | if (rc < 0) |
17b46365 | 773 | return rc; |
25e20e73 | 774 | notify = 1; |
d342894c | 775 | } |
776 | ||
9e4b93f9 ND |
777 | if (notify) { |
778 | if (rd == NULL) | |
779 | rd = first_remote_rtnl(f); | |
780 | vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH); | |
781 | } | |
d342894c | 782 | |
783 | return 0; | |
784 | } | |
785 | ||
6706c82e | 786 | static void vxlan_fdb_free(struct rcu_head *head) |
6681712d DS |
787 | { |
788 | struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu); | |
3e61aa8f | 789 | struct vxlan_rdst *rd, *nd; |
6681712d | 790 | |
0c1d70af PA |
791 | list_for_each_entry_safe(rd, nd, &f->remotes, list) { |
792 | dst_cache_destroy(&rd->dst_cache); | |
6681712d | 793 | kfree(rd); |
0c1d70af | 794 | } |
6681712d DS |
795 | kfree(f); |
796 | } | |
797 | ||
4c2438ba RP |
798 | static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f, |
799 | bool do_notify) | |
d342894c | 800 | { |
801 | netdev_dbg(vxlan->dev, | |
802 | "delete %pM\n", f->eth_addr); | |
803 | ||
804 | --vxlan->addrcnt; | |
4c2438ba RP |
805 | if (do_notify) |
806 | vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH); | |
d342894c | 807 | |
808 | hlist_del_rcu(&f->hlist); | |
6681712d | 809 | call_rcu(&f->rcu, vxlan_fdb_free); |
d342894c | 810 | } |
811 | ||
35cf2845 LR |
812 | static void vxlan_dst_free(struct rcu_head *head) |
813 | { | |
814 | struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu); | |
815 | ||
816 | dst_cache_destroy(&rd->dst_cache); | |
817 | kfree(rd); | |
818 | } | |
819 | ||
820 | static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f, | |
821 | struct vxlan_rdst *rd) | |
822 | { | |
823 | list_del_rcu(&rd->list); | |
824 | vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH); | |
825 | call_rcu(&rd->rcu, vxlan_dst_free); | |
826 | } | |
827 | ||
f0b074be | 828 | static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan, |
3ad7a4b1 RP |
829 | union vxlan_addr *ip, __be16 *port, __be32 *src_vni, |
830 | __be32 *vni, u32 *ifindex) | |
d342894c | 831 | { |
6681712d | 832 | struct net *net = dev_net(vxlan->dev); |
e4c7ed41 | 833 | int err; |
d342894c | 834 | |
f0b074be | 835 | if (tb[NDA_DST]) { |
e4c7ed41 CW |
836 | err = vxlan_nla_get_addr(ip, tb[NDA_DST]); |
837 | if (err) | |
838 | return err; | |
f0b074be | 839 | } else { |
e4c7ed41 CW |
840 | union vxlan_addr *remote = &vxlan->default_dst.remote_ip; |
841 | if (remote->sa.sa_family == AF_INET) { | |
842 | ip->sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
843 | ip->sa.sa_family = AF_INET; | |
844 | #if IS_ENABLED(CONFIG_IPV6) | |
845 | } else { | |
846 | ip->sin6.sin6_addr = in6addr_any; | |
847 | ip->sa.sa_family = AF_INET6; | |
848 | #endif | |
849 | } | |
f0b074be | 850 | } |
d342894c | 851 | |
6681712d | 852 | if (tb[NDA_PORT]) { |
73cf3317 | 853 | if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) |
6681712d | 854 | return -EINVAL; |
f0b074be MR |
855 | *port = nla_get_be16(tb[NDA_PORT]); |
856 | } else { | |
0dfbdf41 | 857 | *port = vxlan->cfg.dst_port; |
f0b074be | 858 | } |
6681712d DS |
859 | |
860 | if (tb[NDA_VNI]) { | |
861 | if (nla_len(tb[NDA_VNI]) != sizeof(u32)) | |
862 | return -EINVAL; | |
54bfd872 | 863 | *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI])); |
f0b074be MR |
864 | } else { |
865 | *vni = vxlan->default_dst.remote_vni; | |
866 | } | |
6681712d | 867 | |
3ad7a4b1 RP |
868 | if (tb[NDA_SRC_VNI]) { |
869 | if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32)) | |
870 | return -EINVAL; | |
871 | *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI])); | |
872 | } else { | |
873 | *src_vni = vxlan->default_dst.remote_vni; | |
874 | } | |
875 | ||
6681712d | 876 | if (tb[NDA_IFINDEX]) { |
5abb0029 | 877 | struct net_device *tdev; |
6681712d DS |
878 | |
879 | if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) | |
880 | return -EINVAL; | |
f0b074be | 881 | *ifindex = nla_get_u32(tb[NDA_IFINDEX]); |
73763949 | 882 | tdev = __dev_get_by_index(net, *ifindex); |
5abb0029 | 883 | if (!tdev) |
6681712d | 884 | return -EADDRNOTAVAIL; |
f0b074be MR |
885 | } else { |
886 | *ifindex = 0; | |
887 | } | |
888 | ||
889 | return 0; | |
890 | } | |
891 | ||
892 | /* Add static entry (via netlink) */ | |
893 | static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], | |
894 | struct net_device *dev, | |
f6f6424b | 895 | const unsigned char *addr, u16 vid, u16 flags) |
f0b074be MR |
896 | { |
897 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
898 | /* struct net *net = dev_net(vxlan->dev); */ | |
e4c7ed41 | 899 | union vxlan_addr ip; |
f0b074be | 900 | __be16 port; |
3ad7a4b1 | 901 | __be32 src_vni, vni; |
54bfd872 | 902 | u32 ifindex; |
f0b074be MR |
903 | int err; |
904 | ||
905 | if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) { | |
906 | pr_info("RTM_NEWNEIGH with invalid state %#x\n", | |
907 | ndm->ndm_state); | |
908 | return -EINVAL; | |
909 | } | |
910 | ||
911 | if (tb[NDA_DST] == NULL) | |
912 | return -EINVAL; | |
913 | ||
3ad7a4b1 | 914 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex); |
f0b074be MR |
915 | if (err) |
916 | return err; | |
6681712d | 917 | |
5933a7bb MR |
918 | if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family) |
919 | return -EAFNOSUPPORT; | |
920 | ||
d342894c | 921 | spin_lock_bh(&vxlan->hash_lock); |
25e20e73 | 922 | err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags, |
3ad7a4b1 | 923 | port, src_vni, vni, ifindex, ndm->ndm_flags); |
d342894c | 924 | spin_unlock_bh(&vxlan->hash_lock); |
925 | ||
926 | return err; | |
927 | } | |
928 | ||
3ad7a4b1 RP |
929 | static int __vxlan_fdb_delete(struct vxlan_dev *vxlan, |
930 | const unsigned char *addr, union vxlan_addr ip, | |
fc39c38b XL |
931 | __be16 port, __be32 src_vni, __be32 vni, |
932 | u32 ifindex, u16 vid) | |
d342894c | 933 | { |
d342894c | 934 | struct vxlan_fdb *f; |
bc7892ba | 935 | struct vxlan_rdst *rd = NULL; |
3ad7a4b1 | 936 | int err = -ENOENT; |
bc7892ba | 937 | |
3ad7a4b1 | 938 | f = vxlan_find_mac(vxlan, addr, src_vni); |
bc7892ba | 939 | if (!f) |
3ad7a4b1 | 940 | return err; |
bc7892ba | 941 | |
e4c7ed41 CW |
942 | if (!vxlan_addr_any(&ip)) { |
943 | rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex); | |
bc7892ba MR |
944 | if (!rd) |
945 | goto out; | |
946 | } | |
947 | ||
bc7892ba MR |
948 | /* remove a destination if it's not the only one on the list, |
949 | * otherwise destroy the fdb entry | |
950 | */ | |
951 | if (rd && !list_is_singular(&f->remotes)) { | |
35cf2845 | 952 | vxlan_fdb_dst_destroy(vxlan, f, rd); |
bc7892ba | 953 | goto out; |
d342894c | 954 | } |
bc7892ba | 955 | |
4c2438ba | 956 | vxlan_fdb_destroy(vxlan, f, true); |
bc7892ba MR |
957 | |
958 | out: | |
3ad7a4b1 RP |
959 | return 0; |
960 | } | |
961 | ||
962 | /* Delete entry (via netlink) */ | |
963 | static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], | |
964 | struct net_device *dev, | |
965 | const unsigned char *addr, u16 vid) | |
966 | { | |
967 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
968 | union vxlan_addr ip; | |
969 | __be32 src_vni, vni; | |
970 | __be16 port; | |
971 | u32 ifindex; | |
972 | int err; | |
973 | ||
974 | err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex); | |
975 | if (err) | |
976 | return err; | |
977 | ||
978 | spin_lock_bh(&vxlan->hash_lock); | |
979 | err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex, | |
980 | vid); | |
d342894c | 981 | spin_unlock_bh(&vxlan->hash_lock); |
982 | ||
983 | return err; | |
984 | } | |
985 | ||
986 | /* Dump forwarding table */ | |
987 | static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, | |
5d5eacb3 | 988 | struct net_device *dev, |
d297653d | 989 | struct net_device *filter_dev, int *idx) |
d342894c | 990 | { |
991 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
992 | unsigned int h; | |
d297653d | 993 | int err = 0; |
d342894c | 994 | |
995 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
996 | struct vxlan_fdb *f; | |
d342894c | 997 | |
b67bfe0d | 998 | hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) { |
6681712d | 999 | struct vxlan_rdst *rd; |
6681712d | 1000 | |
3e61aa8f | 1001 | list_for_each_entry_rcu(rd, &f->remotes, list) { |
d297653d | 1002 | if (*idx < cb->args[2]) |
07a51cd3 AW |
1003 | goto skip; |
1004 | ||
6681712d DS |
1005 | err = vxlan_fdb_info(skb, vxlan, f, |
1006 | NETLINK_CB(cb->skb).portid, | |
1007 | cb->nlh->nlmsg_seq, | |
1008 | RTM_NEWNEIGH, | |
1009 | NLM_F_MULTI, rd); | |
d297653d | 1010 | if (err < 0) |
3e61aa8f | 1011 | goto out; |
3e61aa8f | 1012 | skip: |
d297653d | 1013 | *idx += 1; |
07a51cd3 | 1014 | } |
d342894c | 1015 | } |
1016 | } | |
3e61aa8f | 1017 | out: |
d297653d | 1018 | return err; |
d342894c | 1019 | } |
1020 | ||
1021 | /* Watch incoming packets to learn mapping between Ethernet address | |
1022 | * and Tunnel endpoint. | |
c4b49512 | 1023 | * Return true if packet is bogus and should be dropped. |
d342894c | 1024 | */ |
26a41ae6 | 1025 | static bool vxlan_snoop(struct net_device *dev, |
3ad7a4b1 | 1026 | union vxlan_addr *src_ip, const u8 *src_mac, |
87613de9 | 1027 | u32 src_ifindex, __be32 vni) |
d342894c | 1028 | { |
1029 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1030 | struct vxlan_fdb *f; | |
87613de9 MS |
1031 | u32 ifindex = 0; |
1032 | ||
1033 | #if IS_ENABLED(CONFIG_IPV6) | |
1034 | if (src_ip->sa.sa_family == AF_INET6 && | |
1035 | (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)) | |
1036 | ifindex = src_ifindex; | |
1037 | #endif | |
d342894c | 1038 | |
3ad7a4b1 | 1039 | f = vxlan_find_mac(vxlan, src_mac, vni); |
d342894c | 1040 | if (likely(f)) { |
5ca5461c | 1041 | struct vxlan_rdst *rdst = first_remote_rcu(f); |
3e61aa8f | 1042 | |
87613de9 MS |
1043 | if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) && |
1044 | rdst->remote_ifindex == ifindex)) | |
26a41ae6 | 1045 | return false; |
1046 | ||
1047 | /* Don't migrate static entries, drop packets */ | |
e0090a9e | 1048 | if (f->state & (NUD_PERMANENT | NUD_NOARP)) |
26a41ae6 | 1049 | return true; |
d342894c | 1050 | |
1051 | if (net_ratelimit()) | |
1052 | netdev_info(dev, | |
e4c7ed41 | 1053 | "%pM migrated from %pIS to %pIS\n", |
a4870f79 | 1054 | src_mac, &rdst->remote_ip.sa, &src_ip->sa); |
d342894c | 1055 | |
e4c7ed41 | 1056 | rdst->remote_ip = *src_ip; |
d342894c | 1057 | f->updated = jiffies; |
9e4b93f9 | 1058 | vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH); |
d342894c | 1059 | } else { |
1060 | /* learned new entry */ | |
1061 | spin_lock(&vxlan->hash_lock); | |
3bf74b1a | 1062 | |
1063 | /* close off race between vxlan_flush and incoming packets */ | |
1064 | if (netif_running(dev)) | |
25e20e73 | 1065 | vxlan_fdb_update(vxlan, src_mac, src_ip, |
3bf74b1a | 1066 | NUD_REACHABLE, |
1067 | NLM_F_EXCL|NLM_F_CREATE, | |
0dfbdf41 | 1068 | vxlan->cfg.dst_port, |
3ad7a4b1 | 1069 | vni, |
3bf74b1a | 1070 | vxlan->default_dst.remote_vni, |
87613de9 | 1071 | ifindex, NTF_SELF); |
d342894c | 1072 | spin_unlock(&vxlan->hash_lock); |
1073 | } | |
26a41ae6 | 1074 | |
1075 | return false; | |
d342894c | 1076 | } |
1077 | ||
d342894c | 1078 | /* See if multicast group is already in use by other ID */ |
95ab0991 | 1079 | static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev) |
d342894c | 1080 | { |
553675fb | 1081 | struct vxlan_dev *vxlan; |
c6fcc4fc | 1082 | struct vxlan_sock *sock4; |
4053ab1b AB |
1083 | #if IS_ENABLED(CONFIG_IPV6) |
1084 | struct vxlan_sock *sock6; | |
1085 | #endif | |
b1be00a6 | 1086 | unsigned short family = dev->default_dst.remote_ip.sa.sa_family; |
d342894c | 1087 | |
c6fcc4fc | 1088 | sock4 = rtnl_dereference(dev->vn4_sock); |
1089 | ||
95ab0991 G |
1090 | /* The vxlan_sock is only used by dev, leaving group has |
1091 | * no effect on other vxlan devices. | |
1092 | */ | |
66af846f | 1093 | if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1) |
95ab0991 | 1094 | return false; |
b1be00a6 | 1095 | #if IS_ENABLED(CONFIG_IPV6) |
c6fcc4fc | 1096 | sock6 = rtnl_dereference(dev->vn6_sock); |
66af846f | 1097 | if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1) |
b1be00a6 JB |
1098 | return false; |
1099 | #endif | |
95ab0991 | 1100 | |
553675fb | 1101 | list_for_each_entry(vxlan, &vn->vxlan_list, next) { |
95ab0991 | 1102 | if (!netif_running(vxlan->dev) || vxlan == dev) |
553675fb | 1103 | continue; |
d342894c | 1104 | |
c6fcc4fc | 1105 | if (family == AF_INET && |
1106 | rtnl_dereference(vxlan->vn4_sock) != sock4) | |
95ab0991 | 1107 | continue; |
b1be00a6 | 1108 | #if IS_ENABLED(CONFIG_IPV6) |
c6fcc4fc | 1109 | if (family == AF_INET6 && |
1110 | rtnl_dereference(vxlan->vn6_sock) != sock6) | |
b1be00a6 JB |
1111 | continue; |
1112 | #endif | |
95ab0991 G |
1113 | |
1114 | if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip, | |
1115 | &dev->default_dst.remote_ip)) | |
1116 | continue; | |
1117 | ||
1118 | if (vxlan->default_dst.remote_ifindex != | |
1119 | dev->default_dst.remote_ifindex) | |
1120 | continue; | |
1121 | ||
1122 | return true; | |
553675fb | 1123 | } |
d342894c | 1124 | |
1125 | return false; | |
1126 | } | |
1127 | ||
544a773a | 1128 | static bool __vxlan_sock_release_prep(struct vxlan_sock *vs) |
7c47cedf | 1129 | { |
b1be00a6 | 1130 | struct vxlan_net *vn; |
012a5729 | 1131 | |
b1be00a6 | 1132 | if (!vs) |
544a773a | 1133 | return false; |
66af846f | 1134 | if (!refcount_dec_and_test(&vs->refcnt)) |
544a773a | 1135 | return false; |
d342894c | 1136 | |
b1be00a6 | 1137 | vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id); |
1c51a915 | 1138 | spin_lock(&vn->sock_lock); |
7c47cedf | 1139 | hlist_del_rcu(&vs->hlist); |
e7b3db5e | 1140 | udp_tunnel_notify_del_rx_port(vs->sock, |
b9adcd69 AD |
1141 | (vs->flags & VXLAN_F_GPE) ? |
1142 | UDP_TUNNEL_TYPE_VXLAN_GPE : | |
e7b3db5e | 1143 | UDP_TUNNEL_TYPE_VXLAN); |
1c51a915 SH |
1144 | spin_unlock(&vn->sock_lock); |
1145 | ||
544a773a | 1146 | return true; |
d342894c | 1147 | } |
1148 | ||
b1be00a6 JB |
1149 | static void vxlan_sock_release(struct vxlan_dev *vxlan) |
1150 | { | |
c6fcc4fc | 1151 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
b1be00a6 | 1152 | #if IS_ENABLED(CONFIG_IPV6) |
c6fcc4fc | 1153 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
1154 | ||
57d88182 | 1155 | RCU_INIT_POINTER(vxlan->vn6_sock, NULL); |
544a773a HFS |
1156 | #endif |
1157 | ||
57d88182 | 1158 | RCU_INIT_POINTER(vxlan->vn4_sock, NULL); |
544a773a HFS |
1159 | synchronize_net(); |
1160 | ||
a53cb29b MB |
1161 | vxlan_vs_del_dev(vxlan); |
1162 | ||
c6fcc4fc | 1163 | if (__vxlan_sock_release_prep(sock4)) { |
1164 | udp_tunnel_sock_release(sock4->sock); | |
1165 | kfree(sock4); | |
544a773a HFS |
1166 | } |
1167 | ||
1168 | #if IS_ENABLED(CONFIG_IPV6) | |
c6fcc4fc | 1169 | if (__vxlan_sock_release_prep(sock6)) { |
1170 | udp_tunnel_sock_release(sock6->sock); | |
1171 | kfree(sock6); | |
544a773a | 1172 | } |
b1be00a6 JB |
1173 | #endif |
1174 | } | |
1175 | ||
56ef9c90 | 1176 | /* Update multicast group membership when first VNI on |
c4b49512 | 1177 | * multicast address is brought up |
7c47cedf | 1178 | */ |
56ef9c90 | 1179 | static int vxlan_igmp_join(struct vxlan_dev *vxlan) |
d342894c | 1180 | { |
b1be00a6 | 1181 | struct sock *sk; |
e4c7ed41 CW |
1182 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
1183 | int ifindex = vxlan->default_dst.remote_ifindex; | |
149d7549 | 1184 | int ret = -EINVAL; |
d342894c | 1185 | |
e4c7ed41 | 1186 | if (ip->sa.sa_family == AF_INET) { |
c6fcc4fc | 1187 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
e4c7ed41 CW |
1188 | struct ip_mreqn mreq = { |
1189 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, | |
1190 | .imr_ifindex = ifindex, | |
1191 | }; | |
1192 | ||
c6fcc4fc | 1193 | sk = sock4->sock->sk; |
b1be00a6 | 1194 | lock_sock(sk); |
56ef9c90 | 1195 | ret = ip_mc_join_group(sk, &mreq); |
b1be00a6 | 1196 | release_sock(sk); |
e4c7ed41 CW |
1197 | #if IS_ENABLED(CONFIG_IPV6) |
1198 | } else { | |
c6fcc4fc | 1199 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
1200 | ||
1201 | sk = sock6->sock->sk; | |
b1be00a6 | 1202 | lock_sock(sk); |
56ef9c90 MRL |
1203 | ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex, |
1204 | &ip->sin6.sin6_addr); | |
b1be00a6 | 1205 | release_sock(sk); |
e4c7ed41 CW |
1206 | #endif |
1207 | } | |
3fc2de2f | 1208 | |
56ef9c90 | 1209 | return ret; |
3fc2de2f | 1210 | } |
1211 | ||
1212 | /* Inverse of vxlan_igmp_join when last VNI is brought down */ | |
56ef9c90 | 1213 | static int vxlan_igmp_leave(struct vxlan_dev *vxlan) |
3fc2de2f | 1214 | { |
b1be00a6 | 1215 | struct sock *sk; |
e4c7ed41 CW |
1216 | union vxlan_addr *ip = &vxlan->default_dst.remote_ip; |
1217 | int ifindex = vxlan->default_dst.remote_ifindex; | |
149d7549 | 1218 | int ret = -EINVAL; |
3fc2de2f | 1219 | |
e4c7ed41 | 1220 | if (ip->sa.sa_family == AF_INET) { |
c6fcc4fc | 1221 | struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock); |
e4c7ed41 CW |
1222 | struct ip_mreqn mreq = { |
1223 | .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr, | |
1224 | .imr_ifindex = ifindex, | |
1225 | }; | |
1226 | ||
c6fcc4fc | 1227 | sk = sock4->sock->sk; |
b1be00a6 | 1228 | lock_sock(sk); |
56ef9c90 | 1229 | ret = ip_mc_leave_group(sk, &mreq); |
b1be00a6 | 1230 | release_sock(sk); |
e4c7ed41 CW |
1231 | #if IS_ENABLED(CONFIG_IPV6) |
1232 | } else { | |
c6fcc4fc | 1233 | struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock); |
1234 | ||
1235 | sk = sock6->sock->sk; | |
b1be00a6 | 1236 | lock_sock(sk); |
56ef9c90 MRL |
1237 | ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex, |
1238 | &ip->sin6.sin6_addr); | |
b1be00a6 | 1239 | release_sock(sk); |
e4c7ed41 CW |
1240 | #endif |
1241 | } | |
d342894c | 1242 | |
56ef9c90 | 1243 | return ret; |
d342894c | 1244 | } |
1245 | ||
f14ecebb JB |
1246 | static bool vxlan_remcsum(struct vxlanhdr *unparsed, |
1247 | struct sk_buff *skb, u32 vxflags) | |
dfd8645e | 1248 | { |
7d34fa75 | 1249 | size_t start, offset; |
dfd8645e | 1250 | |
f14ecebb JB |
1251 | if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload) |
1252 | goto out; | |
b7fe10e5 | 1253 | |
f14ecebb JB |
1254 | start = vxlan_rco_start(unparsed->vx_vni); |
1255 | offset = start + vxlan_rco_offset(unparsed->vx_vni); | |
dfd8645e | 1256 | |
7d34fa75 | 1257 | if (!pskb_may_pull(skb, offset + sizeof(u16))) |
be5cfeab | 1258 | return false; |
dfd8645e | 1259 | |
be5cfeab JB |
1260 | skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset, |
1261 | !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL)); | |
f14ecebb JB |
1262 | out: |
1263 | unparsed->vx_flags &= ~VXLAN_HF_RCO; | |
1264 | unparsed->vx_vni &= VXLAN_VNI_MASK; | |
be5cfeab | 1265 | return true; |
dfd8645e TH |
1266 | } |
1267 | ||
f14ecebb | 1268 | static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed, |
64f87d36 | 1269 | struct sk_buff *skb, u32 vxflags, |
10a5af23 | 1270 | struct vxlan_metadata *md) |
3288af08 | 1271 | { |
f14ecebb | 1272 | struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed; |
10a5af23 | 1273 | struct metadata_dst *tun_dst; |
f14ecebb JB |
1274 | |
1275 | if (!(unparsed->vx_flags & VXLAN_HF_GBP)) | |
1276 | goto out; | |
3288af08 | 1277 | |
3288af08 JB |
1278 | md->gbp = ntohs(gbp->policy_id); |
1279 | ||
10a5af23 | 1280 | tun_dst = (struct metadata_dst *)skb_dst(skb); |
810813c4 | 1281 | if (tun_dst) { |
3288af08 | 1282 | tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT; |
810813c4 DM |
1283 | tun_dst->u.tun_info.options_len = sizeof(*md); |
1284 | } | |
3288af08 JB |
1285 | if (gbp->dont_learn) |
1286 | md->gbp |= VXLAN_GBP_DONT_LEARN; | |
1287 | ||
1288 | if (gbp->policy_applied) | |
1289 | md->gbp |= VXLAN_GBP_POLICY_APPLIED; | |
f14ecebb | 1290 | |
64f87d36 JB |
1291 | /* In flow-based mode, GBP is carried in dst_metadata */ |
1292 | if (!(vxflags & VXLAN_F_COLLECT_METADATA)) | |
1293 | skb->mark = md->gbp; | |
f14ecebb JB |
1294 | out: |
1295 | unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS; | |
3288af08 JB |
1296 | } |
1297 | ||
e1e5314d | 1298 | static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed, |
61618eea | 1299 | __be16 *protocol, |
e1e5314d JB |
1300 | struct sk_buff *skb, u32 vxflags) |
1301 | { | |
1302 | struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed; | |
1303 | ||
1304 | /* Need to have Next Protocol set for interfaces in GPE mode. */ | |
1305 | if (!gpe->np_applied) | |
1306 | return false; | |
1307 | /* "The initial version is 0. If a receiver does not support the | |
1308 | * version indicated it MUST drop the packet. | |
1309 | */ | |
1310 | if (gpe->version != 0) | |
1311 | return false; | |
1312 | /* "When the O bit is set to 1, the packet is an OAM packet and OAM | |
1313 | * processing MUST occur." However, we don't implement OAM | |
1314 | * processing, thus drop the packet. | |
1315 | */ | |
1316 | if (gpe->oam_flag) | |
1317 | return false; | |
1318 | ||
fa20e0e3 JB |
1319 | *protocol = tun_p_to_eth_p(gpe->next_protocol); |
1320 | if (!*protocol) | |
e1e5314d | 1321 | return false; |
e1e5314d JB |
1322 | |
1323 | unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS; | |
1324 | return true; | |
1325 | } | |
1326 | ||
1ab016e2 JB |
1327 | static bool vxlan_set_mac(struct vxlan_dev *vxlan, |
1328 | struct vxlan_sock *vs, | |
3ad7a4b1 | 1329 | struct sk_buff *skb, __be32 vni) |
614732ea | 1330 | { |
614732ea | 1331 | union vxlan_addr saddr; |
87613de9 | 1332 | u32 ifindex = skb->dev->ifindex; |
614732ea | 1333 | |
614732ea | 1334 | skb_reset_mac_header(skb); |
614732ea TG |
1335 | skb->protocol = eth_type_trans(skb, vxlan->dev); |
1336 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
1337 | ||
1338 | /* Ignore packet loops (and multicast echo) */ | |
1339 | if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr)) | |
1ab016e2 | 1340 | return false; |
614732ea | 1341 | |
760c6805 | 1342 | /* Get address from the outer IP header */ |
ce212d0f | 1343 | if (vxlan_get_sk_family(vs) == AF_INET) { |
1ab016e2 | 1344 | saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr; |
614732ea TG |
1345 | saddr.sa.sa_family = AF_INET; |
1346 | #if IS_ENABLED(CONFIG_IPV6) | |
1347 | } else { | |
1ab016e2 | 1348 | saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr; |
614732ea TG |
1349 | saddr.sa.sa_family = AF_INET6; |
1350 | #endif | |
1351 | } | |
1352 | ||
dc5321d7 | 1353 | if ((vxlan->cfg.flags & VXLAN_F_LEARN) && |
87613de9 | 1354 | vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni)) |
1ab016e2 JB |
1355 | return false; |
1356 | ||
1357 | return true; | |
1358 | } | |
1359 | ||
760c6805 JB |
1360 | static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph, |
1361 | struct sk_buff *skb) | |
1362 | { | |
1363 | int err = 0; | |
1364 | ||
1365 | if (vxlan_get_sk_family(vs) == AF_INET) | |
1366 | err = IP_ECN_decapsulate(oiph, skb); | |
1367 | #if IS_ENABLED(CONFIG_IPV6) | |
1368 | else | |
1369 | err = IP6_ECN_decapsulate(oiph, skb); | |
1370 | #endif | |
1371 | ||
1372 | if (unlikely(err) && log_ecn_error) { | |
1373 | if (vxlan_get_sk_family(vs) == AF_INET) | |
1374 | net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n", | |
1375 | &((struct iphdr *)oiph)->saddr, | |
1376 | ((struct iphdr *)oiph)->tos); | |
1377 | else | |
1378 | net_info_ratelimited("non-ECT from %pI6\n", | |
1379 | &((struct ipv6hdr *)oiph)->saddr); | |
1380 | } | |
1381 | return err <= 1; | |
1382 | } | |
1383 | ||
d342894c | 1384 | /* Callback from net/ipv4/udp.c to receive packets */ |
f2d1968e | 1385 | static int vxlan_rcv(struct sock *sk, struct sk_buff *skb) |
d342894c | 1386 | { |
f2d1968e | 1387 | struct pcpu_sw_netstats *stats; |
c9e78efb | 1388 | struct vxlan_dev *vxlan; |
5cfccc5a | 1389 | struct vxlan_sock *vs; |
f14ecebb | 1390 | struct vxlanhdr unparsed; |
ee122c79 TG |
1391 | struct vxlan_metadata _md; |
1392 | struct vxlan_metadata *md = &_md; | |
61618eea | 1393 | __be16 protocol = htons(ETH_P_TEB); |
e1e5314d | 1394 | bool raw_proto = false; |
f2d1968e | 1395 | void *oiph; |
3ad7a4b1 | 1396 | __be32 vni = 0; |
d342894c | 1397 | |
e1e5314d | 1398 | /* Need UDP and VXLAN header to be present */ |
7ce04758 | 1399 | if (!pskb_may_pull(skb, VXLAN_HLEN)) |
e5aed006 | 1400 | goto drop; |
d342894c | 1401 | |
f14ecebb | 1402 | unparsed = *vxlan_hdr(skb); |
288b01c8 JB |
1403 | /* VNI flag always required to be set */ |
1404 | if (!(unparsed.vx_flags & VXLAN_HF_VNI)) { | |
1405 | netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", | |
1406 | ntohl(vxlan_hdr(skb)->vx_flags), | |
1407 | ntohl(vxlan_hdr(skb)->vx_vni)); | |
1408 | /* Return non vxlan pkt */ | |
e5aed006 | 1409 | goto drop; |
d342894c | 1410 | } |
288b01c8 JB |
1411 | unparsed.vx_flags &= ~VXLAN_HF_VNI; |
1412 | unparsed.vx_vni &= ~VXLAN_VNI_MASK; | |
d342894c | 1413 | |
559835ea | 1414 | vs = rcu_dereference_sk_user_data(sk); |
5cfccc5a | 1415 | if (!vs) |
d342894c | 1416 | goto drop; |
d342894c | 1417 | |
3ad7a4b1 RP |
1418 | vni = vxlan_vni(vxlan_hdr(skb)->vx_vni); |
1419 | ||
49f810f0 | 1420 | vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni); |
c9e78efb JB |
1421 | if (!vxlan) |
1422 | goto drop; | |
1423 | ||
e1e5314d JB |
1424 | /* For backwards compatibility, only allow reserved fields to be |
1425 | * used by VXLAN extensions if explicitly requested. | |
1426 | */ | |
1427 | if (vs->flags & VXLAN_F_GPE) { | |
1428 | if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags)) | |
1429 | goto drop; | |
1430 | raw_proto = true; | |
1431 | } | |
1432 | ||
1433 | if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto, | |
1434 | !net_eq(vxlan->net, dev_net(vxlan->dev)))) | |
1435 | goto drop; | |
c9e78efb | 1436 | |
ee122c79 | 1437 | if (vxlan_collect_metadata(vs)) { |
10a5af23 | 1438 | struct metadata_dst *tun_dst; |
07dabf20 | 1439 | |
c29a70d2 | 1440 | tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY, |
d817f432 | 1441 | key32_to_tunnel_id(vni), sizeof(*md)); |
c29a70d2 | 1442 | |
ee122c79 TG |
1443 | if (!tun_dst) |
1444 | goto drop; | |
1445 | ||
0f1b7354 | 1446 | md = ip_tunnel_info_opts(&tun_dst->u.tun_info); |
10a5af23 JB |
1447 | |
1448 | skb_dst_set(skb, (struct dst_entry *)tun_dst); | |
ee122c79 TG |
1449 | } else { |
1450 | memset(md, 0, sizeof(*md)); | |
1451 | } | |
1452 | ||
f14ecebb JB |
1453 | if (vs->flags & VXLAN_F_REMCSUM_RX) |
1454 | if (!vxlan_remcsum(&unparsed, skb, vs->flags)) | |
1455 | goto drop; | |
1456 | if (vs->flags & VXLAN_F_GBP) | |
10a5af23 | 1457 | vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md); |
e1e5314d JB |
1458 | /* Note that GBP and GPE can never be active together. This is |
1459 | * ensured in vxlan_dev_configure. | |
1460 | */ | |
3511494c | 1461 | |
f14ecebb | 1462 | if (unparsed.vx_flags || unparsed.vx_vni) { |
3bf39475 TH |
1463 | /* If there are any unprocessed flags remaining treat |
1464 | * this as a malformed packet. This behavior diverges from | |
1465 | * VXLAN RFC (RFC7348) which stipulates that bits in reserved | |
1466 | * in reserved fields are to be ignored. The approach here | |
c4b49512 | 1467 | * maintains compatibility with previous stack code, and also |
3bf39475 TH |
1468 | * is more robust and provides a little more security in |
1469 | * adding extensions to VXLAN. | |
1470 | */ | |
288b01c8 | 1471 | goto drop; |
3bf39475 TH |
1472 | } |
1473 | ||
e1e5314d | 1474 | if (!raw_proto) { |
3ad7a4b1 | 1475 | if (!vxlan_set_mac(vxlan, vs, skb, vni)) |
e1e5314d JB |
1476 | goto drop; |
1477 | } else { | |
8be0cfa4 | 1478 | skb_reset_mac_header(skb); |
e1e5314d JB |
1479 | skb->dev = vxlan->dev; |
1480 | skb->pkt_type = PACKET_HOST; | |
1481 | } | |
f2d1968e | 1482 | |
f2d1968e JB |
1483 | oiph = skb_network_header(skb); |
1484 | skb_reset_network_header(skb); | |
1485 | ||
1486 | if (!vxlan_ecn_decapsulate(vs, oiph, skb)) { | |
1487 | ++vxlan->dev->stats.rx_frame_errors; | |
1488 | ++vxlan->dev->stats.rx_errors; | |
1489 | goto drop; | |
1490 | } | |
1491 | ||
1492 | stats = this_cpu_ptr(vxlan->dev->tstats); | |
1493 | u64_stats_update_begin(&stats->syncp); | |
1494 | stats->rx_packets++; | |
1495 | stats->rx_bytes += skb->len; | |
1496 | u64_stats_update_end(&stats->syncp); | |
1497 | ||
1498 | gro_cells_receive(&vxlan->gro_cells, skb); | |
5cfccc5a PS |
1499 | return 0; |
1500 | ||
1501 | drop: | |
288b01c8 JB |
1502 | /* Consume bad packet */ |
1503 | kfree_skb(skb); | |
1504 | return 0; | |
5cfccc5a PS |
1505 | } |
1506 | ||
3ad7a4b1 | 1507 | static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni) |
e4f67add DS |
1508 | { |
1509 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1510 | struct arphdr *parp; | |
1511 | u8 *arpptr, *sha; | |
1512 | __be32 sip, tip; | |
1513 | struct neighbour *n; | |
1514 | ||
1515 | if (dev->flags & IFF_NOARP) | |
1516 | goto out; | |
1517 | ||
1518 | if (!pskb_may_pull(skb, arp_hdr_len(dev))) { | |
1519 | dev->stats.tx_dropped++; | |
1520 | goto out; | |
1521 | } | |
1522 | parp = arp_hdr(skb); | |
1523 | ||
1524 | if ((parp->ar_hrd != htons(ARPHRD_ETHER) && | |
1525 | parp->ar_hrd != htons(ARPHRD_IEEE802)) || | |
1526 | parp->ar_pro != htons(ETH_P_IP) || | |
1527 | parp->ar_op != htons(ARPOP_REQUEST) || | |
1528 | parp->ar_hln != dev->addr_len || | |
1529 | parp->ar_pln != 4) | |
1530 | goto out; | |
1531 | arpptr = (u8 *)parp + sizeof(struct arphdr); | |
1532 | sha = arpptr; | |
1533 | arpptr += dev->addr_len; /* sha */ | |
1534 | memcpy(&sip, arpptr, sizeof(sip)); | |
1535 | arpptr += sizeof(sip); | |
1536 | arpptr += dev->addr_len; /* tha */ | |
1537 | memcpy(&tip, arpptr, sizeof(tip)); | |
1538 | ||
1539 | if (ipv4_is_loopback(tip) || | |
1540 | ipv4_is_multicast(tip)) | |
1541 | goto out; | |
1542 | ||
1543 | n = neigh_lookup(&arp_tbl, &tip, dev); | |
1544 | ||
1545 | if (n) { | |
e4f67add DS |
1546 | struct vxlan_fdb *f; |
1547 | struct sk_buff *reply; | |
1548 | ||
1549 | if (!(n->nud_state & NUD_CONNECTED)) { | |
1550 | neigh_release(n); | |
1551 | goto out; | |
1552 | } | |
1553 | ||
3ad7a4b1 | 1554 | f = vxlan_find_mac(vxlan, n->ha, vni); |
e4c7ed41 | 1555 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
e4f67add DS |
1556 | /* bridge-local neighbor */ |
1557 | neigh_release(n); | |
1558 | goto out; | |
1559 | } | |
1560 | ||
1561 | reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha, | |
1562 | n->ha, sha); | |
1563 | ||
1564 | neigh_release(n); | |
1565 | ||
7346135d DS |
1566 | if (reply == NULL) |
1567 | goto out; | |
1568 | ||
e4f67add DS |
1569 | skb_reset_mac_header(reply); |
1570 | __skb_pull(reply, skb_network_offset(reply)); | |
1571 | reply->ip_summed = CHECKSUM_UNNECESSARY; | |
1572 | reply->pkt_type = PACKET_HOST; | |
1573 | ||
1574 | if (netif_rx_ni(reply) == NET_RX_DROP) | |
1575 | dev->stats.rx_dropped++; | |
dc5321d7 | 1576 | } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) { |
e4c7ed41 CW |
1577 | union vxlan_addr ipa = { |
1578 | .sin.sin_addr.s_addr = tip, | |
a45e92a5 | 1579 | .sin.sin_family = AF_INET, |
e4c7ed41 CW |
1580 | }; |
1581 | ||
1582 | vxlan_ip_miss(dev, &ipa); | |
1583 | } | |
e4f67add DS |
1584 | out: |
1585 | consume_skb(skb); | |
1586 | return NETDEV_TX_OK; | |
1587 | } | |
1588 | ||
f564f45c | 1589 | #if IS_ENABLED(CONFIG_IPV6) |
4b29dba9 DS |
1590 | static struct sk_buff *vxlan_na_create(struct sk_buff *request, |
1591 | struct neighbour *n, bool isrouter) | |
1592 | { | |
1593 | struct net_device *dev = request->dev; | |
1594 | struct sk_buff *reply; | |
1595 | struct nd_msg *ns, *na; | |
1596 | struct ipv6hdr *pip6; | |
1597 | u8 *daddr; | |
1598 | int na_olen = 8; /* opt hdr + ETH_ALEN for target */ | |
1599 | int ns_olen; | |
1600 | int i, len; | |
1601 | ||
f1fb08f6 | 1602 | if (dev == NULL || !pskb_may_pull(request, request->len)) |
4b29dba9 DS |
1603 | return NULL; |
1604 | ||
1605 | len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) + | |
1606 | sizeof(*na) + na_olen + dev->needed_tailroom; | |
1607 | reply = alloc_skb(len, GFP_ATOMIC); | |
1608 | if (reply == NULL) | |
1609 | return NULL; | |
1610 | ||
1611 | reply->protocol = htons(ETH_P_IPV6); | |
1612 | reply->dev = dev; | |
1613 | skb_reserve(reply, LL_RESERVED_SPACE(request->dev)); | |
1614 | skb_push(reply, sizeof(struct ethhdr)); | |
6297b91c | 1615 | skb_reset_mac_header(reply); |
4b29dba9 | 1616 | |
f1fb08f6 | 1617 | ns = (struct nd_msg *)(ipv6_hdr(request) + 1); |
4b29dba9 DS |
1618 | |
1619 | daddr = eth_hdr(request)->h_source; | |
f1fb08f6 VB |
1620 | ns_olen = request->len - skb_network_offset(request) - |
1621 | sizeof(struct ipv6hdr) - sizeof(*ns); | |
4b29dba9 DS |
1622 | for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) { |
1623 | if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) { | |
1624 | daddr = ns->opt + i + sizeof(struct nd_opt_hdr); | |
1625 | break; | |
1626 | } | |
1627 | } | |
1628 | ||
1629 | /* Ethernet header */ | |
1630 | ether_addr_copy(eth_hdr(reply)->h_dest, daddr); | |
1631 | ether_addr_copy(eth_hdr(reply)->h_source, n->ha); | |
1632 | eth_hdr(reply)->h_proto = htons(ETH_P_IPV6); | |
1633 | reply->protocol = htons(ETH_P_IPV6); | |
1634 | ||
1635 | skb_pull(reply, sizeof(struct ethhdr)); | |
6297b91c | 1636 | skb_reset_network_header(reply); |
4b29dba9 DS |
1637 | skb_put(reply, sizeof(struct ipv6hdr)); |
1638 | ||
1639 | /* IPv6 header */ | |
1640 | ||
1641 | pip6 = ipv6_hdr(reply); | |
1642 | memset(pip6, 0, sizeof(struct ipv6hdr)); | |
1643 | pip6->version = 6; | |
1644 | pip6->priority = ipv6_hdr(request)->priority; | |
1645 | pip6->nexthdr = IPPROTO_ICMPV6; | |
1646 | pip6->hop_limit = 255; | |
1647 | pip6->daddr = ipv6_hdr(request)->saddr; | |
1648 | pip6->saddr = *(struct in6_addr *)n->primary_key; | |
1649 | ||
1650 | skb_pull(reply, sizeof(struct ipv6hdr)); | |
6297b91c | 1651 | skb_reset_transport_header(reply); |
4b29dba9 | 1652 | |
4b29dba9 | 1653 | /* Neighbor Advertisement */ |
b080db58 | 1654 | na = skb_put_zero(reply, sizeof(*na) + na_olen); |
4b29dba9 DS |
1655 | na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; |
1656 | na->icmph.icmp6_router = isrouter; | |
1657 | na->icmph.icmp6_override = 1; | |
1658 | na->icmph.icmp6_solicited = 1; | |
1659 | na->target = ns->target; | |
1660 | ether_addr_copy(&na->opt[2], n->ha); | |
1661 | na->opt[0] = ND_OPT_TARGET_LL_ADDR; | |
1662 | na->opt[1] = na_olen >> 3; | |
1663 | ||
1664 | na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr, | |
1665 | &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6, | |
1666 | csum_partial(na, sizeof(*na)+na_olen, 0)); | |
1667 | ||
1668 | pip6->payload_len = htons(sizeof(*na)+na_olen); | |
1669 | ||
1670 | skb_push(reply, sizeof(struct ipv6hdr)); | |
1671 | ||
1672 | reply->ip_summed = CHECKSUM_UNNECESSARY; | |
1673 | ||
1674 | return reply; | |
1675 | } | |
1676 | ||
3ad7a4b1 | 1677 | static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni) |
f564f45c CW |
1678 | { |
1679 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
8dcd81a9 | 1680 | const struct in6_addr *daddr; |
8bff3685 | 1681 | const struct ipv6hdr *iphdr; |
4b29dba9 | 1682 | struct inet6_dev *in6_dev; |
8bff3685 XL |
1683 | struct neighbour *n; |
1684 | struct nd_msg *msg; | |
f564f45c CW |
1685 | |
1686 | in6_dev = __in6_dev_get(dev); | |
1687 | if (!in6_dev) | |
1688 | goto out; | |
1689 | ||
f564f45c | 1690 | iphdr = ipv6_hdr(skb); |
f564f45c | 1691 | daddr = &iphdr->daddr; |
f1fb08f6 | 1692 | msg = (struct nd_msg *)(iphdr + 1); |
f564f45c | 1693 | |
4b29dba9 DS |
1694 | if (ipv6_addr_loopback(daddr) || |
1695 | ipv6_addr_is_multicast(&msg->target)) | |
1696 | goto out; | |
1697 | ||
1698 | n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev); | |
f564f45c CW |
1699 | |
1700 | if (n) { | |
1701 | struct vxlan_fdb *f; | |
4b29dba9 | 1702 | struct sk_buff *reply; |
f564f45c CW |
1703 | |
1704 | if (!(n->nud_state & NUD_CONNECTED)) { | |
1705 | neigh_release(n); | |
1706 | goto out; | |
1707 | } | |
1708 | ||
3ad7a4b1 | 1709 | f = vxlan_find_mac(vxlan, n->ha, vni); |
f564f45c CW |
1710 | if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) { |
1711 | /* bridge-local neighbor */ | |
1712 | neigh_release(n); | |
1713 | goto out; | |
1714 | } | |
1715 | ||
4b29dba9 DS |
1716 | reply = vxlan_na_create(skb, n, |
1717 | !!(f ? f->flags & NTF_ROUTER : 0)); | |
1718 | ||
f564f45c | 1719 | neigh_release(n); |
4b29dba9 DS |
1720 | |
1721 | if (reply == NULL) | |
1722 | goto out; | |
1723 | ||
1724 | if (netif_rx_ni(reply) == NET_RX_DROP) | |
1725 | dev->stats.rx_dropped++; | |
1726 | ||
dc5321d7 | 1727 | } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) { |
4b29dba9 DS |
1728 | union vxlan_addr ipa = { |
1729 | .sin6.sin6_addr = msg->target, | |
a45e92a5 | 1730 | .sin6.sin6_family = AF_INET6, |
4b29dba9 DS |
1731 | }; |
1732 | ||
f564f45c CW |
1733 | vxlan_ip_miss(dev, &ipa); |
1734 | } | |
1735 | ||
1736 | out: | |
1737 | consume_skb(skb); | |
1738 | return NETDEV_TX_OK; | |
1739 | } | |
1740 | #endif | |
1741 | ||
e4f67add DS |
1742 | static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb) |
1743 | { | |
1744 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
1745 | struct neighbour *n; | |
e4f67add DS |
1746 | |
1747 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) | |
1748 | return false; | |
1749 | ||
1750 | n = NULL; | |
1751 | switch (ntohs(eth_hdr(skb)->h_proto)) { | |
1752 | case ETH_P_IP: | |
e15a00aa CW |
1753 | { |
1754 | struct iphdr *pip; | |
1755 | ||
e4f67add DS |
1756 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
1757 | return false; | |
1758 | pip = ip_hdr(skb); | |
1759 | n = neigh_lookup(&arp_tbl, &pip->daddr, dev); | |
dc5321d7 | 1760 | if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) { |
e4c7ed41 CW |
1761 | union vxlan_addr ipa = { |
1762 | .sin.sin_addr.s_addr = pip->daddr, | |
a45e92a5 | 1763 | .sin.sin_family = AF_INET, |
e4c7ed41 CW |
1764 | }; |
1765 | ||
1766 | vxlan_ip_miss(dev, &ipa); | |
1767 | return false; | |
1768 | } | |
1769 | ||
e4f67add | 1770 | break; |
e15a00aa CW |
1771 | } |
1772 | #if IS_ENABLED(CONFIG_IPV6) | |
1773 | case ETH_P_IPV6: | |
1774 | { | |
1775 | struct ipv6hdr *pip6; | |
1776 | ||
1777 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) | |
1778 | return false; | |
1779 | pip6 = ipv6_hdr(skb); | |
1780 | n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev); | |
dc5321d7 | 1781 | if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) { |
e15a00aa CW |
1782 | union vxlan_addr ipa = { |
1783 | .sin6.sin6_addr = pip6->daddr, | |
a45e92a5 | 1784 | .sin6.sin6_family = AF_INET6, |
e15a00aa CW |
1785 | }; |
1786 | ||
1787 | vxlan_ip_miss(dev, &ipa); | |
1788 | return false; | |
1789 | } | |
1790 | ||
1791 | break; | |
1792 | } | |
1793 | #endif | |
e4f67add DS |
1794 | default: |
1795 | return false; | |
1796 | } | |
1797 | ||
1798 | if (n) { | |
1799 | bool diff; | |
1800 | ||
7367d0b5 | 1801 | diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha); |
e4f67add DS |
1802 | if (diff) { |
1803 | memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, | |
1804 | dev->addr_len); | |
1805 | memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len); | |
1806 | } | |
1807 | neigh_release(n); | |
1808 | return diff; | |
e4c7ed41 CW |
1809 | } |
1810 | ||
e4f67add DS |
1811 | return false; |
1812 | } | |
1813 | ||
af33c1ad | 1814 | static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags, |
3511494c TG |
1815 | struct vxlan_metadata *md) |
1816 | { | |
1817 | struct vxlanhdr_gbp *gbp; | |
1818 | ||
db79a621 TG |
1819 | if (!md->gbp) |
1820 | return; | |
1821 | ||
3511494c | 1822 | gbp = (struct vxlanhdr_gbp *)vxh; |
54bfd872 | 1823 | vxh->vx_flags |= VXLAN_HF_GBP; |
3511494c TG |
1824 | |
1825 | if (md->gbp & VXLAN_GBP_DONT_LEARN) | |
1826 | gbp->dont_learn = 1; | |
1827 | ||
1828 | if (md->gbp & VXLAN_GBP_POLICY_APPLIED) | |
1829 | gbp->policy_applied = 1; | |
1830 | ||
1831 | gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK); | |
1832 | } | |
1833 | ||
e1e5314d JB |
1834 | static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags, |
1835 | __be16 protocol) | |
1836 | { | |
1837 | struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh; | |
1838 | ||
1839 | gpe->np_applied = 1; | |
fa20e0e3 JB |
1840 | gpe->next_protocol = tun_p_from_eth_p(protocol); |
1841 | if (!gpe->next_protocol) | |
1842 | return -EPFNOSUPPORT; | |
1843 | return 0; | |
e1e5314d JB |
1844 | } |
1845 | ||
f491e56d JB |
1846 | static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst, |
1847 | int iphdr_len, __be32 vni, | |
1848 | struct vxlan_metadata *md, u32 vxflags, | |
b4ed5cad | 1849 | bool udp_sum) |
e4c7ed41 | 1850 | { |
e4c7ed41 | 1851 | struct vxlanhdr *vxh; |
e4c7ed41 CW |
1852 | int min_headroom; |
1853 | int err; | |
dfd8645e | 1854 | int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL; |
e1e5314d | 1855 | __be16 inner_protocol = htons(ETH_P_TEB); |
dfd8645e | 1856 | |
af33c1ad | 1857 | if ((vxflags & VXLAN_F_REMCSUM_TX) && |
dfd8645e TH |
1858 | skb->ip_summed == CHECKSUM_PARTIAL) { |
1859 | int csum_start = skb_checksum_start_offset(skb); | |
1860 | ||
1861 | if (csum_start <= VXLAN_MAX_REMCSUM_START && | |
1862 | !(csum_start & VXLAN_RCO_SHIFT_MASK) && | |
1863 | (skb->csum_offset == offsetof(struct udphdr, check) || | |
b5708501 | 1864 | skb->csum_offset == offsetof(struct tcphdr, check))) |
dfd8645e | 1865 | type |= SKB_GSO_TUNNEL_REMCSUM; |
dfd8645e | 1866 | } |
e4c7ed41 | 1867 | |
e4c7ed41 | 1868 | min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len |
4a4f86cc | 1869 | + VXLAN_HLEN + iphdr_len; |
649c5b8b PS |
1870 | |
1871 | /* Need space for new headers (invalidates iph ptr) */ | |
1872 | err = skb_cow_head(skb, min_headroom); | |
e1e5314d | 1873 | if (unlikely(err)) |
c46b7897 | 1874 | return err; |
649c5b8b | 1875 | |
aed069df AD |
1876 | err = iptunnel_handle_offloads(skb, type); |
1877 | if (err) | |
c46b7897 | 1878 | return err; |
b736a623 | 1879 | |
d58ff351 | 1880 | vxh = __skb_push(skb, sizeof(*vxh)); |
54bfd872 JB |
1881 | vxh->vx_flags = VXLAN_HF_VNI; |
1882 | vxh->vx_vni = vxlan_vni_field(vni); | |
49560532 | 1883 | |
dfd8645e | 1884 | if (type & SKB_GSO_TUNNEL_REMCSUM) { |
54bfd872 | 1885 | unsigned int start; |
dfd8645e | 1886 | |
54bfd872 JB |
1887 | start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr); |
1888 | vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset); | |
1889 | vxh->vx_flags |= VXLAN_HF_RCO; | |
dfd8645e TH |
1890 | |
1891 | if (!skb_is_gso(skb)) { | |
1892 | skb->ip_summed = CHECKSUM_NONE; | |
1893 | skb->encapsulation = 0; | |
1894 | } | |
1895 | } | |
1896 | ||
af33c1ad TH |
1897 | if (vxflags & VXLAN_F_GBP) |
1898 | vxlan_build_gbp_hdr(vxh, vxflags, md); | |
e1e5314d JB |
1899 | if (vxflags & VXLAN_F_GPE) { |
1900 | err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol); | |
1901 | if (err < 0) | |
c46b7897 | 1902 | return err; |
e1e5314d JB |
1903 | inner_protocol = skb->protocol; |
1904 | } | |
3511494c | 1905 | |
e1e5314d | 1906 | skb_set_inner_protocol(skb, inner_protocol); |
039f5062 | 1907 | return 0; |
49560532 | 1908 | } |
49560532 | 1909 | |
655c3de1 | 1910 | static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev, |
1911 | struct vxlan_sock *sock4, | |
1a8496ba | 1912 | struct sk_buff *skb, int oif, u8 tos, |
4ecb1d83 | 1913 | __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport, |
0c1d70af | 1914 | struct dst_cache *dst_cache, |
db3c6139 | 1915 | const struct ip_tunnel_info *info) |
1a8496ba | 1916 | { |
db3c6139 | 1917 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
1a8496ba JB |
1918 | struct rtable *rt = NULL; |
1919 | struct flowi4 fl4; | |
1920 | ||
655c3de1 | 1921 | if (!sock4) |
1922 | return ERR_PTR(-EIO); | |
1923 | ||
db3c6139 DB |
1924 | if (tos && !info) |
1925 | use_cache = false; | |
1926 | if (use_cache) { | |
0c1d70af PA |
1927 | rt = dst_cache_get_ip4(dst_cache, saddr); |
1928 | if (rt) | |
1929 | return rt; | |
1930 | } | |
1931 | ||
1a8496ba JB |
1932 | memset(&fl4, 0, sizeof(fl4)); |
1933 | fl4.flowi4_oif = oif; | |
1934 | fl4.flowi4_tos = RT_TOS(tos); | |
1935 | fl4.flowi4_mark = skb->mark; | |
1936 | fl4.flowi4_proto = IPPROTO_UDP; | |
1937 | fl4.daddr = daddr; | |
272d96a5 | 1938 | fl4.saddr = *saddr; |
4ecb1d83 MP |
1939 | fl4.fl4_dport = dport; |
1940 | fl4.fl4_sport = sport; | |
1a8496ba JB |
1941 | |
1942 | rt = ip_route_output_key(vxlan->net, &fl4); | |
655c3de1 | 1943 | if (likely(!IS_ERR(rt))) { |
1944 | if (rt->dst.dev == dev) { | |
1945 | netdev_dbg(dev, "circular route to %pI4\n", &daddr); | |
1946 | ip_rt_put(rt); | |
1947 | return ERR_PTR(-ELOOP); | |
1948 | } | |
1949 | ||
1a8496ba | 1950 | *saddr = fl4.saddr; |
0c1d70af PA |
1951 | if (use_cache) |
1952 | dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr); | |
655c3de1 | 1953 | } else { |
1954 | netdev_dbg(dev, "no route to %pI4\n", &daddr); | |
1955 | return ERR_PTR(-ENETUNREACH); | |
0c1d70af | 1956 | } |
1a8496ba JB |
1957 | return rt; |
1958 | } | |
1959 | ||
e5d4b29f JB |
1960 | #if IS_ENABLED(CONFIG_IPV6) |
1961 | static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan, | |
655c3de1 | 1962 | struct net_device *dev, |
03dc52a8 | 1963 | struct vxlan_sock *sock6, |
1400615d | 1964 | struct sk_buff *skb, int oif, u8 tos, |
e7f70af1 | 1965 | __be32 label, |
e5d4b29f | 1966 | const struct in6_addr *daddr, |
0c1d70af | 1967 | struct in6_addr *saddr, |
4ecb1d83 | 1968 | __be16 dport, __be16 sport, |
db3c6139 DB |
1969 | struct dst_cache *dst_cache, |
1970 | const struct ip_tunnel_info *info) | |
e5d4b29f | 1971 | { |
db3c6139 | 1972 | bool use_cache = ip_tunnel_dst_cache_usable(skb, info); |
e5d4b29f JB |
1973 | struct dst_entry *ndst; |
1974 | struct flowi6 fl6; | |
1975 | int err; | |
1976 | ||
c6fcc4fc | 1977 | if (!sock6) |
1978 | return ERR_PTR(-EIO); | |
1979 | ||
1400615d DB |
1980 | if (tos && !info) |
1981 | use_cache = false; | |
db3c6139 | 1982 | if (use_cache) { |
0c1d70af PA |
1983 | ndst = dst_cache_get_ip6(dst_cache, saddr); |
1984 | if (ndst) | |
1985 | return ndst; | |
1986 | } | |
1987 | ||
e5d4b29f JB |
1988 | memset(&fl6, 0, sizeof(fl6)); |
1989 | fl6.flowi6_oif = oif; | |
1990 | fl6.daddr = *daddr; | |
272d96a5 | 1991 | fl6.saddr = *saddr; |
eaa93bf4 | 1992 | fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label); |
e5d4b29f JB |
1993 | fl6.flowi6_mark = skb->mark; |
1994 | fl6.flowi6_proto = IPPROTO_UDP; | |
4ecb1d83 MP |
1995 | fl6.fl6_dport = dport; |
1996 | fl6.fl6_sport = sport; | |
e5d4b29f JB |
1997 | |
1998 | err = ipv6_stub->ipv6_dst_lookup(vxlan->net, | |
c6fcc4fc | 1999 | sock6->sock->sk, |
e5d4b29f | 2000 | &ndst, &fl6); |
655c3de1 | 2001 | if (unlikely(err < 0)) { |
2002 | netdev_dbg(dev, "no route to %pI6\n", daddr); | |
2003 | return ERR_PTR(-ENETUNREACH); | |
2004 | } | |
2005 | ||
2006 | if (unlikely(ndst->dev == dev)) { | |
2007 | netdev_dbg(dev, "circular route to %pI6\n", daddr); | |
2008 | dst_release(ndst); | |
2009 | return ERR_PTR(-ELOOP); | |
2010 | } | |
e5d4b29f JB |
2011 | |
2012 | *saddr = fl6.saddr; | |
db3c6139 | 2013 | if (use_cache) |
0c1d70af | 2014 | dst_cache_set_ip6(dst_cache, ndst, saddr); |
e5d4b29f JB |
2015 | return ndst; |
2016 | } | |
2017 | #endif | |
2018 | ||
9dcc71e1 SS |
2019 | /* Bypass encapsulation if the destination is local */ |
2020 | static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, | |
3ad7a4b1 | 2021 | struct vxlan_dev *dst_vxlan, __be32 vni) |
9dcc71e1 | 2022 | { |
8f84985f | 2023 | struct pcpu_sw_netstats *tx_stats, *rx_stats; |
e4c7ed41 CW |
2024 | union vxlan_addr loopback; |
2025 | union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip; | |
ce6502a8 LR |
2026 | struct net_device *dev = skb->dev; |
2027 | int len = skb->len; | |
9dcc71e1 | 2028 | |
8f84985f LR |
2029 | tx_stats = this_cpu_ptr(src_vxlan->dev->tstats); |
2030 | rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats); | |
9dcc71e1 SS |
2031 | skb->pkt_type = PACKET_HOST; |
2032 | skb->encapsulation = 0; | |
2033 | skb->dev = dst_vxlan->dev; | |
2034 | __skb_pull(skb, skb_network_offset(skb)); | |
2035 | ||
e4c7ed41 CW |
2036 | if (remote_ip->sa.sa_family == AF_INET) { |
2037 | loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
2038 | loopback.sa.sa_family = AF_INET; | |
2039 | #if IS_ENABLED(CONFIG_IPV6) | |
2040 | } else { | |
2041 | loopback.sin6.sin6_addr = in6addr_loopback; | |
2042 | loopback.sa.sa_family = AF_INET6; | |
2043 | #endif | |
2044 | } | |
2045 | ||
dc5321d7 | 2046 | if (dst_vxlan->cfg.flags & VXLAN_F_LEARN) |
87613de9 MS |
2047 | vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0, |
2048 | vni); | |
9dcc71e1 SS |
2049 | |
2050 | u64_stats_update_begin(&tx_stats->syncp); | |
2051 | tx_stats->tx_packets++; | |
ce6502a8 | 2052 | tx_stats->tx_bytes += len; |
9dcc71e1 SS |
2053 | u64_stats_update_end(&tx_stats->syncp); |
2054 | ||
2055 | if (netif_rx(skb) == NET_RX_SUCCESS) { | |
2056 | u64_stats_update_begin(&rx_stats->syncp); | |
2057 | rx_stats->rx_packets++; | |
ce6502a8 | 2058 | rx_stats->rx_bytes += len; |
9dcc71e1 SS |
2059 | u64_stats_update_end(&rx_stats->syncp); |
2060 | } else { | |
ce6502a8 | 2061 | dev->stats.rx_dropped++; |
9dcc71e1 SS |
2062 | } |
2063 | } | |
2064 | ||
fee1fad7 | 2065 | static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev, |
49f810f0 MS |
2066 | struct vxlan_dev *vxlan, |
2067 | union vxlan_addr *daddr, | |
2068 | __be16 dst_port, int dst_ifindex, __be32 vni, | |
2069 | struct dst_entry *dst, | |
fee1fad7 | 2070 | u32 rt_flags) |
2071 | { | |
2072 | #if IS_ENABLED(CONFIG_IPV6) | |
2073 | /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of | |
2074 | * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple | |
2075 | * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry. | |
2076 | */ | |
2077 | BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL); | |
2078 | #endif | |
2079 | /* Bypass encapsulation if the destination is local */ | |
2080 | if (rt_flags & RTCF_LOCAL && | |
2081 | !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) { | |
2082 | struct vxlan_dev *dst_vxlan; | |
2083 | ||
2084 | dst_release(dst); | |
49f810f0 | 2085 | dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni, |
fee1fad7 | 2086 | daddr->sa.sa_family, dst_port, |
dc5321d7 | 2087 | vxlan->cfg.flags); |
fee1fad7 | 2088 | if (!dst_vxlan) { |
2089 | dev->stats.tx_errors++; | |
2090 | kfree_skb(skb); | |
2091 | ||
2092 | return -ENOENT; | |
2093 | } | |
3ad7a4b1 | 2094 | vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni); |
fee1fad7 | 2095 | return 1; |
2096 | } | |
2097 | ||
2098 | return 0; | |
2099 | } | |
2100 | ||
4ad16930 | 2101 | static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, |
3ad7a4b1 RP |
2102 | __be32 default_vni, struct vxlan_rdst *rdst, |
2103 | bool did_rsc) | |
d342894c | 2104 | { |
d71785ff | 2105 | struct dst_cache *dst_cache; |
3093fbe7 | 2106 | struct ip_tunnel_info *info; |
d342894c | 2107 | struct vxlan_dev *vxlan = netdev_priv(dev); |
0770b53b | 2108 | const struct iphdr *old_iph = ip_hdr(skb); |
e4c7ed41 | 2109 | union vxlan_addr *dst; |
272d96a5 | 2110 | union vxlan_addr remote_ip, local_ip; |
ee122c79 TG |
2111 | struct vxlan_metadata _md; |
2112 | struct vxlan_metadata *md = &_md; | |
e4c7ed41 | 2113 | __be16 src_port = 0, dst_port; |
655c3de1 | 2114 | struct dst_entry *ndst = NULL; |
e7f70af1 | 2115 | __be32 vni, label; |
d342894c | 2116 | __u8 tos, ttl; |
49f810f0 | 2117 | int ifindex; |
0e6fbc5b | 2118 | int err; |
dc5321d7 | 2119 | u32 flags = vxlan->cfg.flags; |
b4ed5cad | 2120 | bool udp_sum = false; |
f491e56d | 2121 | bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev)); |
d342894c | 2122 | |
61adedf3 | 2123 | info = skb_tunnel_info(skb); |
3093fbe7 | 2124 | |
ee122c79 | 2125 | if (rdst) { |
0770b53b | 2126 | dst = &rdst->remote_ip; |
2127 | if (vxlan_addr_any(dst)) { | |
2128 | if (did_rsc) { | |
2129 | /* short-circuited back to local bridge */ | |
3ad7a4b1 | 2130 | vxlan_encap_bypass(skb, vxlan, vxlan, default_vni); |
0770b53b | 2131 | return; |
2132 | } | |
2133 | goto drop; | |
2134 | } | |
2135 | ||
0dfbdf41 | 2136 | dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port; |
3ad7a4b1 | 2137 | vni = (rdst->remote_vni) ? : default_vni; |
49f810f0 | 2138 | ifindex = rdst->remote_ifindex; |
1158632b | 2139 | local_ip = vxlan->cfg.saddr; |
d71785ff | 2140 | dst_cache = &rdst->dst_cache; |
0770b53b | 2141 | md->gbp = skb->mark; |
72f6d71e HL |
2142 | if (flags & VXLAN_F_TTL_INHERIT) { |
2143 | ttl = ip_tunnel_get_ttl(old_iph, skb); | |
2144 | } else { | |
2145 | ttl = vxlan->cfg.ttl; | |
2146 | if (!ttl && vxlan_addr_multicast(dst)) | |
2147 | ttl = 1; | |
2148 | } | |
0770b53b | 2149 | |
2150 | tos = vxlan->cfg.tos; | |
2151 | if (tos == 1) | |
2152 | tos = ip_tunnel_get_dsfield(old_iph, skb); | |
2153 | ||
2154 | if (dst->sa.sa_family == AF_INET) | |
2155 | udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX); | |
2156 | else | |
2157 | udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX); | |
2158 | label = vxlan->cfg.label; | |
ee122c79 TG |
2159 | } else { |
2160 | if (!info) { | |
2161 | WARN_ONCE(1, "%s: Missing encapsulation instructions\n", | |
2162 | dev->name); | |
2163 | goto drop; | |
2164 | } | |
b1be00a6 | 2165 | remote_ip.sa.sa_family = ip_tunnel_info_af(info); |
272d96a5 | 2166 | if (remote_ip.sa.sa_family == AF_INET) { |
a725e514 | 2167 | remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst; |
272d96a5 | 2168 | local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src; |
2169 | } else { | |
a725e514 | 2170 | remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst; |
272d96a5 | 2171 | local_ip.sin6.sin6_addr = info->key.u.ipv6.src; |
2172 | } | |
ee122c79 | 2173 | dst = &remote_ip; |
0770b53b | 2174 | dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port; |
2175 | vni = tunnel_id_to_key32(info->key.tun_id); | |
49f810f0 | 2176 | ifindex = 0; |
d71785ff | 2177 | dst_cache = &info->dst_cache; |
256c87c1 PJV |
2178 | if (info->options_len && |
2179 | info->key.tun_flags & TUNNEL_VXLAN_OPT) | |
0770b53b | 2180 | md = ip_tunnel_info_opts(info); |
a725e514 JB |
2181 | ttl = info->key.ttl; |
2182 | tos = info->key.tos; | |
e7f70af1 | 2183 | label = info->key.label; |
b4ed5cad | 2184 | udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM); |
a725e514 | 2185 | } |
0770b53b | 2186 | src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min, |
2187 | vxlan->cfg.port_max, true); | |
a725e514 | 2188 | |
56de859e | 2189 | rcu_read_lock(); |
e4c7ed41 | 2190 | if (dst->sa.sa_family == AF_INET) { |
c6fcc4fc | 2191 | struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); |
c46b7897 | 2192 | struct rtable *rt; |
0770b53b | 2193 | __be16 df = 0; |
c6fcc4fc | 2194 | |
49f810f0 | 2195 | rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos, |
272d96a5 | 2196 | dst->sin.sin_addr.s_addr, |
1158632b | 2197 | &local_ip.sin.sin_addr.s_addr, |
4ecb1d83 | 2198 | dst_port, src_port, |
d71785ff | 2199 | dst_cache, info); |
8ebd115b DM |
2200 | if (IS_ERR(rt)) { |
2201 | err = PTR_ERR(rt); | |
c46b7897 | 2202 | goto tx_error; |
8ebd115b | 2203 | } |
e4c7ed41 CW |
2204 | |
2205 | /* Bypass encapsulation if the destination is local */ | |
fee1fad7 | 2206 | if (!info) { |
2207 | err = encap_bypass_if_local(skb, dev, vxlan, dst, | |
49f810f0 MS |
2208 | dst_port, ifindex, vni, |
2209 | &rt->dst, rt->rt_flags); | |
fee1fad7 | 2210 | if (err) |
56de859e | 2211 | goto out_unlock; |
fee1fad7 | 2212 | } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) { |
6ceb31ca | 2213 | df = htons(IP_DF); |
fee1fad7 | 2214 | } |
6ceb31ca | 2215 | |
c46b7897 | 2216 | ndst = &rt->dst; |
a93bf0ff XL |
2217 | if (skb_dst(skb)) { |
2218 | int mtu = dst_mtu(ndst) - VXLAN_HEADROOM; | |
2219 | ||
f15ca723 | 2220 | skb_dst_update_pmtu(skb, mtu); |
a93bf0ff XL |
2221 | } |
2222 | ||
e4c7ed41 CW |
2223 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
2224 | ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); | |
c46b7897 | 2225 | err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr), |
54bfd872 | 2226 | vni, md, flags, udp_sum); |
f491e56d | 2227 | if (err < 0) |
c46b7897 | 2228 | goto tx_error; |
f491e56d | 2229 | |
1158632b | 2230 | udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr, |
f491e56d JB |
2231 | dst->sin.sin_addr.s_addr, tos, ttl, df, |
2232 | src_port, dst_port, xnet, !udp_sum); | |
e4c7ed41 CW |
2233 | #if IS_ENABLED(CONFIG_IPV6) |
2234 | } else { | |
c6fcc4fc | 2235 | struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); |
e4c7ed41 | 2236 | |
49f810f0 | 2237 | ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos, |
272d96a5 | 2238 | label, &dst->sin6.sin6_addr, |
1158632b | 2239 | &local_ip.sin6.sin6_addr, |
4ecb1d83 | 2240 | dst_port, src_port, |
db3c6139 | 2241 | dst_cache, info); |
e5d4b29f | 2242 | if (IS_ERR(ndst)) { |
8ebd115b | 2243 | err = PTR_ERR(ndst); |
c46b7897 | 2244 | ndst = NULL; |
9dcc71e1 | 2245 | goto tx_error; |
e4c7ed41 | 2246 | } |
655c3de1 | 2247 | |
fee1fad7 | 2248 | if (!info) { |
2249 | u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags; | |
49560532 | 2250 | |
fee1fad7 | 2251 | err = encap_bypass_if_local(skb, dev, vxlan, dst, |
49f810f0 MS |
2252 | dst_port, ifindex, vni, |
2253 | ndst, rt6i_flags); | |
fee1fad7 | 2254 | if (err) |
56de859e | 2255 | goto out_unlock; |
fee1fad7 | 2256 | } |
35e2d115 | 2257 | |
a93bf0ff XL |
2258 | if (skb_dst(skb)) { |
2259 | int mtu = dst_mtu(ndst) - VXLAN6_HEADROOM; | |
2260 | ||
f15ca723 | 2261 | skb_dst_update_pmtu(skb, mtu); |
a93bf0ff XL |
2262 | } |
2263 | ||
1400615d | 2264 | tos = ip_tunnel_ecn_encap(tos, old_iph, skb); |
e4c7ed41 | 2265 | ttl = ttl ? : ip6_dst_hoplimit(ndst); |
f491e56d JB |
2266 | skb_scrub_packet(skb, xnet); |
2267 | err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr), | |
54bfd872 | 2268 | vni, md, flags, udp_sum); |
c46b7897 | 2269 | if (err < 0) |
2270 | goto tx_error; | |
2271 | ||
0770b53b | 2272 | udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev, |
1158632b | 2273 | &local_ip.sin6.sin6_addr, |
272d96a5 | 2274 | &dst->sin6.sin6_addr, tos, ttl, |
e7f70af1 | 2275 | label, src_port, dst_port, !udp_sum); |
e4c7ed41 CW |
2276 | #endif |
2277 | } | |
56de859e JK |
2278 | out_unlock: |
2279 | rcu_read_unlock(); | |
4ad16930 | 2280 | return; |
d342894c | 2281 | |
2282 | drop: | |
2283 | dev->stats.tx_dropped++; | |
c46b7897 | 2284 | dev_kfree_skb(skb); |
2285 | return; | |
d342894c | 2286 | |
2287 | tx_error: | |
56de859e | 2288 | rcu_read_unlock(); |
655c3de1 | 2289 | if (err == -ELOOP) |
2290 | dev->stats.collisions++; | |
2291 | else if (err == -ENETUNREACH) | |
2292 | dev->stats.tx_carrier_errors++; | |
c46b7897 | 2293 | dst_release(ndst); |
d342894c | 2294 | dev->stats.tx_errors++; |
c46b7897 | 2295 | kfree_skb(skb); |
d342894c | 2296 | } |
2297 | ||
6681712d DS |
2298 | /* Transmit local packets over Vxlan |
2299 | * | |
2300 | * Outer IP header inherits ECN and DF from inner header. | |
2301 | * Outer UDP destination is the VXLAN assigned port. | |
2302 | * source port is based on hash of flow | |
2303 | */ | |
2304 | static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev) | |
2305 | { | |
2306 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
8bff3685 | 2307 | struct vxlan_rdst *rdst, *fdst = NULL; |
3093fbe7 | 2308 | const struct ip_tunnel_info *info; |
6681712d | 2309 | bool did_rsc = false; |
6681712d | 2310 | struct vxlan_fdb *f; |
8bff3685 | 2311 | struct ethhdr *eth; |
3ad7a4b1 | 2312 | __be32 vni = 0; |
6681712d | 2313 | |
61adedf3 | 2314 | info = skb_tunnel_info(skb); |
3093fbe7 | 2315 | |
6681712d | 2316 | skb_reset_mac_header(skb); |
6681712d | 2317 | |
dc5321d7 | 2318 | if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) { |
3ad7a4b1 RP |
2319 | if (info && info->mode & IP_TUNNEL_INFO_BRIDGE && |
2320 | info->mode & IP_TUNNEL_INFO_TX) { | |
2321 | vni = tunnel_id_to_key32(info->key.tun_id); | |
2322 | } else { | |
2323 | if (info && info->mode & IP_TUNNEL_INFO_TX) | |
2324 | vxlan_xmit_one(skb, dev, vni, NULL, false); | |
2325 | else | |
2326 | kfree_skb(skb); | |
2327 | return NETDEV_TX_OK; | |
2328 | } | |
47e5d1b0 JB |
2329 | } |
2330 | ||
dc5321d7 | 2331 | if (vxlan->cfg.flags & VXLAN_F_PROXY) { |
47e5d1b0 | 2332 | eth = eth_hdr(skb); |
f564f45c | 2333 | if (ntohs(eth->h_proto) == ETH_P_ARP) |
3ad7a4b1 | 2334 | return arp_reduce(dev, skb, vni); |
f564f45c | 2335 | #if IS_ENABLED(CONFIG_IPV6) |
8bff3685 XL |
2336 | else if (ntohs(eth->h_proto) == ETH_P_IPV6 && |
2337 | pskb_may_pull(skb, sizeof(struct ipv6hdr) + | |
2338 | sizeof(struct nd_msg)) && | |
2339 | ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { | |
2340 | struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1); | |
2341 | ||
2342 | if (m->icmph.icmp6_code == 0 && | |
2343 | m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) | |
f1fb08f6 | 2344 | return neigh_reduce(dev, skb, vni); |
f564f45c CW |
2345 | } |
2346 | #endif | |
2347 | } | |
6681712d | 2348 | |
47e5d1b0 | 2349 | eth = eth_hdr(skb); |
3ad7a4b1 | 2350 | f = vxlan_find_mac(vxlan, eth->h_dest, vni); |
ae884082 DS |
2351 | did_rsc = false; |
2352 | ||
dc5321d7 | 2353 | if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) && |
e15a00aa CW |
2354 | (ntohs(eth->h_proto) == ETH_P_IP || |
2355 | ntohs(eth->h_proto) == ETH_P_IPV6)) { | |
ae884082 DS |
2356 | did_rsc = route_shortcircuit(dev, skb); |
2357 | if (did_rsc) | |
3ad7a4b1 | 2358 | f = vxlan_find_mac(vxlan, eth->h_dest, vni); |
ae884082 DS |
2359 | } |
2360 | ||
6681712d | 2361 | if (f == NULL) { |
3ad7a4b1 | 2362 | f = vxlan_find_mac(vxlan, all_zeros_mac, vni); |
afbd8bae | 2363 | if (f == NULL) { |
dc5321d7 | 2364 | if ((vxlan->cfg.flags & VXLAN_F_L2MISS) && |
afbd8bae MR |
2365 | !is_multicast_ether_addr(eth->h_dest)) |
2366 | vxlan_fdb_miss(vxlan, eth->h_dest); | |
2367 | ||
2368 | dev->stats.tx_dropped++; | |
8f646c92 | 2369 | kfree_skb(skb); |
afbd8bae MR |
2370 | return NETDEV_TX_OK; |
2371 | } | |
2372 | } | |
6681712d | 2373 | |
afbd8bae MR |
2374 | list_for_each_entry_rcu(rdst, &f->remotes, list) { |
2375 | struct sk_buff *skb1; | |
6681712d | 2376 | |
8f646c92 ED |
2377 | if (!fdst) { |
2378 | fdst = rdst; | |
2379 | continue; | |
2380 | } | |
afbd8bae MR |
2381 | skb1 = skb_clone(skb, GFP_ATOMIC); |
2382 | if (skb1) | |
3ad7a4b1 | 2383 | vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc); |
6681712d DS |
2384 | } |
2385 | ||
8f646c92 | 2386 | if (fdst) |
3ad7a4b1 | 2387 | vxlan_xmit_one(skb, dev, vni, fdst, did_rsc); |
8f646c92 ED |
2388 | else |
2389 | kfree_skb(skb); | |
4ad16930 | 2390 | return NETDEV_TX_OK; |
6681712d DS |
2391 | } |
2392 | ||
d342894c | 2393 | /* Walk the forwarding table and purge stale entries */ |
df7e828c | 2394 | static void vxlan_cleanup(struct timer_list *t) |
d342894c | 2395 | { |
df7e828c | 2396 | struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer); |
d342894c | 2397 | unsigned long next_timer = jiffies + FDB_AGE_INTERVAL; |
2398 | unsigned int h; | |
2399 | ||
2400 | if (!netif_running(vxlan->dev)) | |
2401 | return; | |
2402 | ||
d342894c | 2403 | for (h = 0; h < FDB_HASH_SIZE; ++h) { |
2404 | struct hlist_node *p, *n; | |
14e1d0fa SD |
2405 | |
2406 | spin_lock_bh(&vxlan->hash_lock); | |
d342894c | 2407 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { |
2408 | struct vxlan_fdb *f | |
2409 | = container_of(p, struct vxlan_fdb, hlist); | |
2410 | unsigned long timeout; | |
2411 | ||
efb5f68f | 2412 | if (f->state & (NUD_PERMANENT | NUD_NOARP)) |
d342894c | 2413 | continue; |
2414 | ||
def499c9 RP |
2415 | if (f->flags & NTF_EXT_LEARNED) |
2416 | continue; | |
2417 | ||
0dfbdf41 | 2418 | timeout = f->used + vxlan->cfg.age_interval * HZ; |
d342894c | 2419 | if (time_before_eq(timeout, jiffies)) { |
2420 | netdev_dbg(vxlan->dev, | |
2421 | "garbage collect %pM\n", | |
2422 | f->eth_addr); | |
2423 | f->state = NUD_STALE; | |
4c2438ba | 2424 | vxlan_fdb_destroy(vxlan, f, true); |
d342894c | 2425 | } else if (time_before(timeout, next_timer)) |
2426 | next_timer = timeout; | |
2427 | } | |
14e1d0fa | 2428 | spin_unlock_bh(&vxlan->hash_lock); |
d342894c | 2429 | } |
d342894c | 2430 | |
2431 | mod_timer(&vxlan->age_timer, next_timer); | |
2432 | } | |
2433 | ||
a53cb29b MB |
2434 | static void vxlan_vs_del_dev(struct vxlan_dev *vxlan) |
2435 | { | |
2436 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); | |
2437 | ||
2438 | spin_lock(&vn->sock_lock); | |
69e76661 JB |
2439 | hlist_del_init_rcu(&vxlan->hlist4.hlist); |
2440 | #if IS_ENABLED(CONFIG_IPV6) | |
2441 | hlist_del_init_rcu(&vxlan->hlist6.hlist); | |
2442 | #endif | |
a53cb29b MB |
2443 | spin_unlock(&vn->sock_lock); |
2444 | } | |
2445 | ||
69e76661 JB |
2446 | static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan, |
2447 | struct vxlan_dev_node *node) | |
9c2e24e1 | 2448 | { |
56ef9c90 | 2449 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
54bfd872 | 2450 | __be32 vni = vxlan->default_dst.remote_vni; |
9c2e24e1 | 2451 | |
69e76661 | 2452 | node->vxlan = vxlan; |
56ef9c90 | 2453 | spin_lock(&vn->sock_lock); |
69e76661 | 2454 | hlist_add_head_rcu(&node->hlist, vni_head(vs, vni)); |
56ef9c90 | 2455 | spin_unlock(&vn->sock_lock); |
9c2e24e1 PS |
2456 | } |
2457 | ||
d342894c | 2458 | /* Setup stats when device is created */ |
2459 | static int vxlan_init(struct net_device *dev) | |
2460 | { | |
1c213bd2 | 2461 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
e8171045 | 2462 | if (!dev->tstats) |
d342894c | 2463 | return -ENOMEM; |
2464 | ||
2465 | return 0; | |
2466 | } | |
2467 | ||
3ad7a4b1 | 2468 | static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni) |
afbd8bae MR |
2469 | { |
2470 | struct vxlan_fdb *f; | |
2471 | ||
2472 | spin_lock_bh(&vxlan->hash_lock); | |
3ad7a4b1 | 2473 | f = __vxlan_find_mac(vxlan, all_zeros_mac, vni); |
afbd8bae | 2474 | if (f) |
4c2438ba | 2475 | vxlan_fdb_destroy(vxlan, f, true); |
afbd8bae MR |
2476 | spin_unlock_bh(&vxlan->hash_lock); |
2477 | } | |
2478 | ||
ebf4063e SH |
2479 | static void vxlan_uninit(struct net_device *dev) |
2480 | { | |
2481 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
ebf4063e | 2482 | |
3ad7a4b1 | 2483 | vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni); |
afbd8bae | 2484 | |
ebf4063e SH |
2485 | free_percpu(dev->tstats); |
2486 | } | |
2487 | ||
d342894c | 2488 | /* Start ageing timer and join group when device is brought up */ |
2489 | static int vxlan_open(struct net_device *dev) | |
2490 | { | |
2491 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
205f356d | 2492 | int ret; |
56ef9c90 | 2493 | |
205f356d JB |
2494 | ret = vxlan_sock_add(vxlan); |
2495 | if (ret < 0) | |
2496 | return ret; | |
d342894c | 2497 | |
79d4a94f | 2498 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) { |
56ef9c90 | 2499 | ret = vxlan_igmp_join(vxlan); |
bef0057b MRL |
2500 | if (ret == -EADDRINUSE) |
2501 | ret = 0; | |
56ef9c90 | 2502 | if (ret) { |
205f356d | 2503 | vxlan_sock_release(vxlan); |
56ef9c90 MRL |
2504 | return ret; |
2505 | } | |
d342894c | 2506 | } |
2507 | ||
0dfbdf41 | 2508 | if (vxlan->cfg.age_interval) |
d342894c | 2509 | mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL); |
2510 | ||
56ef9c90 | 2511 | return ret; |
d342894c | 2512 | } |
2513 | ||
2514 | /* Purge the forwarding table */ | |
8b3f9337 | 2515 | static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all) |
d342894c | 2516 | { |
31fec5aa | 2517 | unsigned int h; |
d342894c | 2518 | |
2519 | spin_lock_bh(&vxlan->hash_lock); | |
2520 | for (h = 0; h < FDB_HASH_SIZE; ++h) { | |
2521 | struct hlist_node *p, *n; | |
2522 | hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) { | |
2523 | struct vxlan_fdb *f | |
2524 | = container_of(p, struct vxlan_fdb, hlist); | |
8b3f9337 RP |
2525 | if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP))) |
2526 | continue; | |
afbd8bae MR |
2527 | /* the all_zeros_mac entry is deleted at vxlan_uninit */ |
2528 | if (!is_zero_ether_addr(f->eth_addr)) | |
4c2438ba | 2529 | vxlan_fdb_destroy(vxlan, f, true); |
d342894c | 2530 | } |
2531 | } | |
2532 | spin_unlock_bh(&vxlan->hash_lock); | |
2533 | } | |
2534 | ||
2535 | /* Cleanup timer and forwarding table on shutdown */ | |
2536 | static int vxlan_stop(struct net_device *dev) | |
2537 | { | |
2538 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
f01ec1c0 | 2539 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
56ef9c90 | 2540 | int ret = 0; |
d342894c | 2541 | |
24c0e683 | 2542 | if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) && |
f13b1689 | 2543 | !vxlan_group_used(vn, vxlan)) |
56ef9c90 | 2544 | ret = vxlan_igmp_leave(vxlan); |
d342894c | 2545 | |
2546 | del_timer_sync(&vxlan->age_timer); | |
2547 | ||
8b3f9337 | 2548 | vxlan_flush(vxlan, false); |
205f356d | 2549 | vxlan_sock_release(vxlan); |
d342894c | 2550 | |
56ef9c90 | 2551 | return ret; |
d342894c | 2552 | } |
2553 | ||
d342894c | 2554 | /* Stub, nothing needs to be done. */ |
2555 | static void vxlan_set_multicast_list(struct net_device *dev) | |
2556 | { | |
2557 | } | |
2558 | ||
91572088 | 2559 | static int vxlan_change_mtu(struct net_device *dev, int new_mtu) |
345010b5 | 2560 | { |
91572088 JW |
2561 | struct vxlan_dev *vxlan = netdev_priv(dev); |
2562 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
2563 | struct net_device *lowerdev = __dev_get_by_index(vxlan->net, | |
2564 | dst->remote_ifindex); | |
ce44a4ae | 2565 | bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6); |
345010b5 | 2566 | |
91572088 JW |
2567 | /* This check is different than dev->max_mtu, because it looks at |
2568 | * the lowerdev->mtu, rather than the static dev->max_mtu | |
2569 | */ | |
2570 | if (lowerdev) { | |
2571 | int max_mtu = lowerdev->mtu - | |
2572 | (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM); | |
2573 | if (new_mtu > max_mtu) | |
72564b59 | 2574 | return -EINVAL; |
72564b59 DW |
2575 | } |
2576 | ||
345010b5 DB |
2577 | dev->mtu = new_mtu; |
2578 | return 0; | |
2579 | } | |
2580 | ||
fc4099f1 PS |
2581 | static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) |
2582 | { | |
2583 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
2584 | struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
2585 | __be16 sport, dport; | |
2586 | ||
2587 | sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min, | |
2588 | vxlan->cfg.port_max, true); | |
2589 | dport = info->key.tp_dst ? : vxlan->cfg.dst_port; | |
2590 | ||
239e944f | 2591 | if (ip_tunnel_info_af(info) == AF_INET) { |
c6fcc4fc | 2592 | struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); |
1a8496ba JB |
2593 | struct rtable *rt; |
2594 | ||
655c3de1 | 2595 | rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos, |
1a8496ba | 2596 | info->key.u.ipv4.dst, |
22f0708a PA |
2597 | &info->key.u.ipv4.src, dport, sport, |
2598 | &info->dst_cache, info); | |
1a8496ba JB |
2599 | if (IS_ERR(rt)) |
2600 | return PTR_ERR(rt); | |
2601 | ip_rt_put(rt); | |
239e944f JB |
2602 | } else { |
2603 | #if IS_ENABLED(CONFIG_IPV6) | |
03dc52a8 | 2604 | struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock); |
239e944f JB |
2605 | struct dst_entry *ndst; |
2606 | ||
655c3de1 | 2607 | ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos, |
e7f70af1 | 2608 | info->key.label, &info->key.u.ipv6.dst, |
22f0708a PA |
2609 | &info->key.u.ipv6.src, dport, sport, |
2610 | &info->dst_cache, info); | |
239e944f JB |
2611 | if (IS_ERR(ndst)) |
2612 | return PTR_ERR(ndst); | |
2613 | dst_release(ndst); | |
239e944f JB |
2614 | #else /* !CONFIG_IPV6 */ |
2615 | return -EPFNOSUPPORT; | |
2616 | #endif | |
2617 | } | |
1a8496ba JB |
2618 | info->key.tp_src = sport; |
2619 | info->key.tp_dst = dport; | |
239e944f | 2620 | return 0; |
fc4099f1 PS |
2621 | } |
2622 | ||
0c867c9b | 2623 | static const struct net_device_ops vxlan_netdev_ether_ops = { |
d342894c | 2624 | .ndo_init = vxlan_init, |
ebf4063e | 2625 | .ndo_uninit = vxlan_uninit, |
d342894c | 2626 | .ndo_open = vxlan_open, |
2627 | .ndo_stop = vxlan_stop, | |
2628 | .ndo_start_xmit = vxlan_xmit, | |
e8171045 | 2629 | .ndo_get_stats64 = ip_tunnel_get_stats64, |
d342894c | 2630 | .ndo_set_rx_mode = vxlan_set_multicast_list, |
345010b5 | 2631 | .ndo_change_mtu = vxlan_change_mtu, |
d342894c | 2632 | .ndo_validate_addr = eth_validate_addr, |
2633 | .ndo_set_mac_address = eth_mac_addr, | |
2634 | .ndo_fdb_add = vxlan_fdb_add, | |
2635 | .ndo_fdb_del = vxlan_fdb_delete, | |
2636 | .ndo_fdb_dump = vxlan_fdb_dump, | |
fc4099f1 | 2637 | .ndo_fill_metadata_dst = vxlan_fill_metadata_dst, |
d342894c | 2638 | }; |
2639 | ||
e1e5314d JB |
2640 | static const struct net_device_ops vxlan_netdev_raw_ops = { |
2641 | .ndo_init = vxlan_init, | |
2642 | .ndo_uninit = vxlan_uninit, | |
2643 | .ndo_open = vxlan_open, | |
2644 | .ndo_stop = vxlan_stop, | |
2645 | .ndo_start_xmit = vxlan_xmit, | |
2646 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
2647 | .ndo_change_mtu = vxlan_change_mtu, | |
2648 | .ndo_fill_metadata_dst = vxlan_fill_metadata_dst, | |
2649 | }; | |
2650 | ||
d342894c | 2651 | /* Info for udev, that this is a virtual tunnel endpoint */ |
2652 | static struct device_type vxlan_type = { | |
2653 | .name = "vxlan", | |
2654 | }; | |
2655 | ||
e5de25dc | 2656 | /* Calls the ndo_udp_tunnel_add of the caller in order to |
35e42379 | 2657 | * supply the listening VXLAN udp ports. Callers are expected |
e5de25dc | 2658 | * to implement the ndo_udp_tunnel_add. |
53cf5275 | 2659 | */ |
2d2b13fc | 2660 | static void vxlan_offload_rx_ports(struct net_device *dev, bool push) |
53cf5275 JG |
2661 | { |
2662 | struct vxlan_sock *vs; | |
2663 | struct net *net = dev_net(dev); | |
2664 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
35e42379 | 2665 | unsigned int i; |
53cf5275 JG |
2666 | |
2667 | spin_lock(&vn->sock_lock); | |
2668 | for (i = 0; i < PORT_HASH_SIZE; ++i) { | |
2d2b13fc SD |
2669 | hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) { |
2670 | unsigned short type; | |
2671 | ||
2672 | if (vs->flags & VXLAN_F_GPE) | |
2673 | type = UDP_TUNNEL_TYPE_VXLAN_GPE; | |
2674 | else | |
2675 | type = UDP_TUNNEL_TYPE_VXLAN; | |
2676 | ||
2677 | if (push) | |
2678 | udp_tunnel_push_rx_port(dev, vs->sock, type); | |
2679 | else | |
2680 | udp_tunnel_drop_rx_port(dev, vs->sock, type); | |
2681 | } | |
53cf5275 JG |
2682 | } |
2683 | spin_unlock(&vn->sock_lock); | |
2684 | } | |
53cf5275 | 2685 | |
d342894c | 2686 | /* Initialize the device structure. */ |
2687 | static void vxlan_setup(struct net_device *dev) | |
2688 | { | |
2689 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
31fec5aa | 2690 | unsigned int h; |
d342894c | 2691 | |
65226ef8 JB |
2692 | eth_hw_addr_random(dev); |
2693 | ether_setup(dev); | |
2694 | ||
cf124db5 | 2695 | dev->needs_free_netdev = true; |
d342894c | 2696 | SET_NETDEV_DEVTYPE(dev, &vxlan_type); |
2697 | ||
d342894c | 2698 | dev->features |= NETIF_F_LLTX; |
d6727fe3 | 2699 | dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; |
0afb1666 | 2700 | dev->features |= NETIF_F_RXCSUM; |
05c0db08 | 2701 | dev->features |= NETIF_F_GSO_SOFTWARE; |
0afb1666 | 2702 | |
1eaa8178 | 2703 | dev->vlan_features = dev->features; |
0afb1666 | 2704 | dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; |
05c0db08 | 2705 | dev->hw_features |= NETIF_F_GSO_SOFTWARE; |
02875878 | 2706 | netif_keep_dst(dev); |
0c867c9b | 2707 | dev->priv_flags |= IFF_NO_QUEUE; |
d342894c | 2708 | |
a985343b MS |
2709 | /* MTU range: 68 - 65535 */ |
2710 | dev->min_mtu = ETH_MIN_MTU; | |
2711 | dev->max_mtu = ETH_MAX_MTU; | |
2712 | ||
553675fb | 2713 | INIT_LIST_HEAD(&vxlan->next); |
d342894c | 2714 | spin_lock_init(&vxlan->hash_lock); |
2715 | ||
df7e828c | 2716 | timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE); |
d342894c | 2717 | |
2718 | vxlan->dev = dev; | |
2719 | ||
58ce31cc TH |
2720 | gro_cells_init(&vxlan->gro_cells, dev); |
2721 | ||
d342894c | 2722 | for (h = 0; h < FDB_HASH_SIZE; ++h) |
2723 | INIT_HLIST_HEAD(&vxlan->fdb_head[h]); | |
2724 | } | |
2725 | ||
0c867c9b JB |
2726 | static void vxlan_ether_setup(struct net_device *dev) |
2727 | { | |
0c867c9b JB |
2728 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
2729 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; | |
2730 | dev->netdev_ops = &vxlan_netdev_ether_ops; | |
2731 | } | |
2732 | ||
e1e5314d JB |
2733 | static void vxlan_raw_setup(struct net_device *dev) |
2734 | { | |
65226ef8 | 2735 | dev->header_ops = NULL; |
e1e5314d JB |
2736 | dev->type = ARPHRD_NONE; |
2737 | dev->hard_header_len = 0; | |
2738 | dev->addr_len = 0; | |
e1e5314d JB |
2739 | dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; |
2740 | dev->netdev_ops = &vxlan_netdev_raw_ops; | |
2741 | } | |
2742 | ||
d342894c | 2743 | static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = { |
2744 | [IFLA_VXLAN_ID] = { .type = NLA_U32 }, | |
5d174dd8 | 2745 | [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) }, |
e4c7ed41 | 2746 | [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) }, |
d342894c | 2747 | [IFLA_VXLAN_LINK] = { .type = NLA_U32 }, |
2748 | [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) }, | |
e4c7ed41 | 2749 | [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) }, |
d342894c | 2750 | [IFLA_VXLAN_TOS] = { .type = NLA_U8 }, |
2751 | [IFLA_VXLAN_TTL] = { .type = NLA_U8 }, | |
e7f70af1 | 2752 | [IFLA_VXLAN_LABEL] = { .type = NLA_U32 }, |
d342894c | 2753 | [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 }, |
2754 | [IFLA_VXLAN_AGEING] = { .type = NLA_U32 }, | |
2755 | [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 }, | |
05f47d69 | 2756 | [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) }, |
e4f67add DS |
2757 | [IFLA_VXLAN_PROXY] = { .type = NLA_U8 }, |
2758 | [IFLA_VXLAN_RSC] = { .type = NLA_U8 }, | |
2759 | [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 }, | |
2760 | [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 }, | |
f8a9b1bc | 2761 | [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 }, |
823aa873 | 2762 | [IFLA_VXLAN_PORT] = { .type = NLA_U16 }, |
5c91ae08 TH |
2763 | [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 }, |
2764 | [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 }, | |
2765 | [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 }, | |
dfd8645e TH |
2766 | [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 }, |
2767 | [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 }, | |
3511494c | 2768 | [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, }, |
e1e5314d | 2769 | [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, }, |
0ace2ca8 | 2770 | [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG }, |
72f6d71e | 2771 | [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG }, |
d342894c | 2772 | }; |
2773 | ||
a8b8a889 MS |
2774 | static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[], |
2775 | struct netlink_ext_ack *extack) | |
d342894c | 2776 | { |
2777 | if (tb[IFLA_ADDRESS]) { | |
2778 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { | |
653ef6a3 GM |
2779 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], |
2780 | "Provided link layer address is not Ethernet"); | |
d342894c | 2781 | return -EINVAL; |
2782 | } | |
2783 | ||
2784 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { | |
653ef6a3 GM |
2785 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS], |
2786 | "Provided Ethernet address is not unicast"); | |
d342894c | 2787 | return -EADDRNOTAVAIL; |
2788 | } | |
2789 | } | |
2790 | ||
a985343b | 2791 | if (tb[IFLA_MTU]) { |
019b13ae | 2792 | u32 mtu = nla_get_u32(tb[IFLA_MTU]); |
a985343b | 2793 | |
653ef6a3 GM |
2794 | if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) { |
2795 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU], | |
2796 | "MTU must be between 68 and 65535"); | |
a985343b | 2797 | return -EINVAL; |
653ef6a3 | 2798 | } |
a985343b MS |
2799 | } |
2800 | ||
653ef6a3 GM |
2801 | if (!data) { |
2802 | NL_SET_ERR_MSG(extack, | |
2803 | "Required attributes not provided to perform the operation"); | |
d342894c | 2804 | return -EINVAL; |
653ef6a3 | 2805 | } |
d342894c | 2806 | |
2807 | if (data[IFLA_VXLAN_ID]) { | |
a985343b MS |
2808 | u32 id = nla_get_u32(data[IFLA_VXLAN_ID]); |
2809 | ||
653ef6a3 GM |
2810 | if (id >= VXLAN_N_VID) { |
2811 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], | |
2812 | "VXLAN ID must be lower than 16777216"); | |
d342894c | 2813 | return -ERANGE; |
653ef6a3 | 2814 | } |
d342894c | 2815 | } |
2816 | ||
05f47d69 | 2817 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
2818 | const struct ifla_vxlan_port_range *p | |
2819 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
2820 | ||
2821 | if (ntohs(p->high) < ntohs(p->low)) { | |
653ef6a3 GM |
2822 | NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE], |
2823 | "Invalid source port range"); | |
05f47d69 | 2824 | return -EINVAL; |
2825 | } | |
2826 | } | |
2827 | ||
d342894c | 2828 | return 0; |
2829 | } | |
2830 | ||
1b13c97f YB |
2831 | static void vxlan_get_drvinfo(struct net_device *netdev, |
2832 | struct ethtool_drvinfo *drvinfo) | |
2833 | { | |
2834 | strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version)); | |
2835 | strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver)); | |
2836 | } | |
2837 | ||
2838 | static const struct ethtool_ops vxlan_ethtool_ops = { | |
2839 | .get_drvinfo = vxlan_get_drvinfo, | |
2840 | .get_link = ethtool_op_get_link, | |
2841 | }; | |
2842 | ||
3ee64f39 TH |
2843 | static struct socket *vxlan_create_sock(struct net *net, bool ipv6, |
2844 | __be16 port, u32 flags) | |
553675fb | 2845 | { |
e4c7ed41 | 2846 | struct socket *sock; |
3ee64f39 TH |
2847 | struct udp_port_cfg udp_conf; |
2848 | int err; | |
e4c7ed41 | 2849 | |
3ee64f39 | 2850 | memset(&udp_conf, 0, sizeof(udp_conf)); |
e4c7ed41 | 2851 | |
3ee64f39 TH |
2852 | if (ipv6) { |
2853 | udp_conf.family = AF_INET6; | |
3ee64f39 | 2854 | udp_conf.use_udp6_rx_checksums = |
3dc2b6a8 | 2855 | !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX); |
a43a9ef6 | 2856 | udp_conf.ipv6_v6only = 1; |
3ee64f39 TH |
2857 | } else { |
2858 | udp_conf.family = AF_INET; | |
e4c7ed41 | 2859 | } |
e4c7ed41 | 2860 | |
3ee64f39 | 2861 | udp_conf.local_udp_port = port; |
553675fb | 2862 | |
3ee64f39 TH |
2863 | /* Open UDP socket */ |
2864 | err = udp_sock_create(net, &udp_conf, &sock); | |
2865 | if (err < 0) | |
2866 | return ERR_PTR(err); | |
e4c7ed41 | 2867 | |
39deb2c7 | 2868 | return sock; |
e4c7ed41 CW |
2869 | } |
2870 | ||
2871 | /* Create new listen socket if needed */ | |
b1be00a6 JB |
2872 | static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6, |
2873 | __be16 port, u32 flags) | |
e4c7ed41 CW |
2874 | { |
2875 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
2876 | struct vxlan_sock *vs; | |
2877 | struct socket *sock; | |
e4c7ed41 | 2878 | unsigned int h; |
acbf74a7 | 2879 | struct udp_tunnel_sock_cfg tunnel_cfg; |
e4c7ed41 | 2880 | |
dc01e7d3 | 2881 | vs = kzalloc(sizeof(*vs), GFP_KERNEL); |
e4c7ed41 CW |
2882 | if (!vs) |
2883 | return ERR_PTR(-ENOMEM); | |
2884 | ||
2885 | for (h = 0; h < VNI_HASH_SIZE; ++h) | |
2886 | INIT_HLIST_HEAD(&vs->vni_list[h]); | |
2887 | ||
3ee64f39 | 2888 | sock = vxlan_create_sock(net, ipv6, port, flags); |
39deb2c7 | 2889 | if (IS_ERR(sock)) { |
553675fb | 2890 | kfree(vs); |
e50fddc8 | 2891 | return ERR_CAST(sock); |
553675fb | 2892 | } |
e4c7ed41 CW |
2893 | |
2894 | vs->sock = sock; | |
66af846f | 2895 | refcount_set(&vs->refcnt, 1); |
af33c1ad | 2896 | vs->flags = (flags & VXLAN_F_RCV_FLAGS); |
553675fb | 2897 | |
9c2e24e1 PS |
2898 | spin_lock(&vn->sock_lock); |
2899 | hlist_add_head_rcu(&vs->hlist, vs_head(net, port)); | |
e7b3db5e | 2900 | udp_tunnel_notify_add_rx_port(sock, |
b9adcd69 AD |
2901 | (vs->flags & VXLAN_F_GPE) ? |
2902 | UDP_TUNNEL_TYPE_VXLAN_GPE : | |
e7b3db5e | 2903 | UDP_TUNNEL_TYPE_VXLAN); |
9c2e24e1 | 2904 | spin_unlock(&vn->sock_lock); |
553675fb | 2905 | |
2906 | /* Mark socket as an encapsulation socket. */ | |
5602c48c | 2907 | memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); |
acbf74a7 AZ |
2908 | tunnel_cfg.sk_user_data = vs; |
2909 | tunnel_cfg.encap_type = 1; | |
f2d1968e | 2910 | tunnel_cfg.encap_rcv = vxlan_rcv; |
acbf74a7 | 2911 | tunnel_cfg.encap_destroy = NULL; |
5602c48c TH |
2912 | tunnel_cfg.gro_receive = vxlan_gro_receive; |
2913 | tunnel_cfg.gro_complete = vxlan_gro_complete; | |
acbf74a7 AZ |
2914 | |
2915 | setup_udp_tunnel_sock(net, sock, &tunnel_cfg); | |
e4c7ed41 | 2916 | |
9c2e24e1 PS |
2917 | return vs; |
2918 | } | |
2919 | ||
b1be00a6 | 2920 | static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6) |
9c2e24e1 | 2921 | { |
205f356d JB |
2922 | struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id); |
2923 | struct vxlan_sock *vs = NULL; | |
69e76661 | 2924 | struct vxlan_dev_node *node; |
9c2e24e1 | 2925 | |
205f356d | 2926 | if (!vxlan->cfg.no_share) { |
56ef9c90 | 2927 | spin_lock(&vn->sock_lock); |
205f356d | 2928 | vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET, |
dc5321d7 | 2929 | vxlan->cfg.dst_port, vxlan->cfg.flags); |
66af846f | 2930 | if (vs && !refcount_inc_not_zero(&vs->refcnt)) { |
56ef9c90 | 2931 | spin_unlock(&vn->sock_lock); |
205f356d | 2932 | return -EBUSY; |
56ef9c90 MRL |
2933 | } |
2934 | spin_unlock(&vn->sock_lock); | |
2935 | } | |
205f356d | 2936 | if (!vs) |
b1be00a6 | 2937 | vs = vxlan_socket_create(vxlan->net, ipv6, |
dc5321d7 | 2938 | vxlan->cfg.dst_port, vxlan->cfg.flags); |
205f356d JB |
2939 | if (IS_ERR(vs)) |
2940 | return PTR_ERR(vs); | |
b1be00a6 | 2941 | #if IS_ENABLED(CONFIG_IPV6) |
69e76661 | 2942 | if (ipv6) { |
c6fcc4fc | 2943 | rcu_assign_pointer(vxlan->vn6_sock, vs); |
69e76661 JB |
2944 | node = &vxlan->hlist6; |
2945 | } else | |
b1be00a6 | 2946 | #endif |
69e76661 | 2947 | { |
c6fcc4fc | 2948 | rcu_assign_pointer(vxlan->vn4_sock, vs); |
69e76661 JB |
2949 | node = &vxlan->hlist4; |
2950 | } | |
2951 | vxlan_vs_add_dev(vs, vxlan, node); | |
205f356d | 2952 | return 0; |
553675fb | 2953 | } |
2954 | ||
b1be00a6 JB |
2955 | static int vxlan_sock_add(struct vxlan_dev *vxlan) |
2956 | { | |
dc5321d7 MS |
2957 | bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA; |
2958 | bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata; | |
d074bf96 | 2959 | bool ipv4 = !ipv6 || metadata; |
b1be00a6 JB |
2960 | int ret = 0; |
2961 | ||
c6fcc4fc | 2962 | RCU_INIT_POINTER(vxlan->vn4_sock, NULL); |
b1be00a6 | 2963 | #if IS_ENABLED(CONFIG_IPV6) |
c6fcc4fc | 2964 | RCU_INIT_POINTER(vxlan->vn6_sock, NULL); |
d074bf96 | 2965 | if (ipv6) { |
b1be00a6 | 2966 | ret = __vxlan_sock_add(vxlan, true); |
d074bf96 JB |
2967 | if (ret < 0 && ret != -EAFNOSUPPORT) |
2968 | ipv4 = false; | |
2969 | } | |
b1be00a6 | 2970 | #endif |
d074bf96 | 2971 | if (ipv4) |
b1be00a6 JB |
2972 | ret = __vxlan_sock_add(vxlan, false); |
2973 | if (ret < 0) | |
2974 | vxlan_sock_release(vxlan); | |
2975 | return ret; | |
2976 | } | |
2977 | ||
a985343b MS |
2978 | static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf, |
2979 | struct net_device **lower, | |
653ef6a3 GM |
2980 | struct vxlan_dev *old, |
2981 | struct netlink_ext_ack *extack) | |
d342894c | 2982 | { |
33564bbb | 2983 | struct vxlan_net *vn = net_generic(src_net, vxlan_net_id); |
a985343b | 2984 | struct vxlan_dev *tmp; |
e4c7ed41 | 2985 | bool use_ipv6 = false; |
d342894c | 2986 | |
a985343b MS |
2987 | if (conf->flags & VXLAN_F_GPE) { |
2988 | /* For now, allow GPE only together with | |
2989 | * COLLECT_METADATA. This can be relaxed later; in such | |
2990 | * case, the other side of the PtP link will have to be | |
2991 | * provided. | |
2992 | */ | |
2993 | if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) || | |
2994 | !(conf->flags & VXLAN_F_COLLECT_METADATA)) { | |
653ef6a3 GM |
2995 | NL_SET_ERR_MSG(extack, |
2996 | "VXLAN GPE does not support this combination of attributes"); | |
a985343b | 2997 | return -EINVAL; |
3555621d | 2998 | } |
e1e5314d | 2999 | } |
0c867c9b | 3000 | |
ce44a4ae MS |
3001 | if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) { |
3002 | /* Unless IPv6 is explicitly requested, assume IPv4 */ | |
a985343b | 3003 | conf->remote_ip.sa.sa_family = AF_INET; |
ce44a4ae MS |
3004 | conf->saddr.sa.sa_family = AF_INET; |
3005 | } else if (!conf->remote_ip.sa.sa_family) { | |
3006 | conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family; | |
3007 | } else if (!conf->saddr.sa.sa_family) { | |
3008 | conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family; | |
3009 | } | |
3010 | ||
653ef6a3 GM |
3011 | if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) { |
3012 | NL_SET_ERR_MSG(extack, | |
3013 | "Local and remote address must be from the same family"); | |
ce44a4ae | 3014 | return -EINVAL; |
653ef6a3 | 3015 | } |
e4c7ed41 | 3016 | |
653ef6a3 GM |
3017 | if (vxlan_addr_multicast(&conf->saddr)) { |
3018 | NL_SET_ERR_MSG(extack, "Local address cannot be multicast"); | |
0f22a3c6 | 3019 | return -EINVAL; |
653ef6a3 | 3020 | } |
0f22a3c6 | 3021 | |
ce44a4ae | 3022 | if (conf->saddr.sa.sa_family == AF_INET6) { |
653ef6a3 GM |
3023 | if (!IS_ENABLED(CONFIG_IPV6)) { |
3024 | NL_SET_ERR_MSG(extack, | |
3025 | "IPv6 support not enabled in the kernel"); | |
057ba29b | 3026 | return -EPFNOSUPPORT; |
653ef6a3 | 3027 | } |
e4c7ed41 | 3028 | use_ipv6 = true; |
a985343b | 3029 | conf->flags |= VXLAN_F_IPV6; |
0f22a3c6 MS |
3030 | |
3031 | if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) { | |
3032 | int local_type = | |
3033 | ipv6_addr_type(&conf->saddr.sin6.sin6_addr); | |
3034 | int remote_type = | |
3035 | ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr); | |
3036 | ||
3037 | if (local_type & IPV6_ADDR_LINKLOCAL) { | |
3038 | if (!(remote_type & IPV6_ADDR_LINKLOCAL) && | |
653ef6a3 GM |
3039 | (remote_type != IPV6_ADDR_ANY)) { |
3040 | NL_SET_ERR_MSG(extack, | |
3041 | "Invalid combination of local and remote address scopes"); | |
0f22a3c6 | 3042 | return -EINVAL; |
653ef6a3 | 3043 | } |
0f22a3c6 MS |
3044 | |
3045 | conf->flags |= VXLAN_F_IPV6_LINKLOCAL; | |
3046 | } else { | |
3047 | if (remote_type == | |
653ef6a3 GM |
3048 | (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) { |
3049 | NL_SET_ERR_MSG(extack, | |
3050 | "Invalid combination of local and remote address scopes"); | |
0f22a3c6 | 3051 | return -EINVAL; |
653ef6a3 | 3052 | } |
0f22a3c6 MS |
3053 | |
3054 | conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL; | |
3055 | } | |
3056 | } | |
057ba29b | 3057 | } |
d342894c | 3058 | |
653ef6a3 GM |
3059 | if (conf->label && !use_ipv6) { |
3060 | NL_SET_ERR_MSG(extack, | |
3061 | "Label attribute only applies to IPv6 VXLAN devices"); | |
e7f70af1 | 3062 | return -EINVAL; |
653ef6a3 | 3063 | } |
e7f70af1 | 3064 | |
a985343b MS |
3065 | if (conf->remote_ifindex) { |
3066 | struct net_device *lowerdev; | |
34e02aa1 | 3067 | |
a985343b | 3068 | lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex); |
653ef6a3 GM |
3069 | if (!lowerdev) { |
3070 | NL_SET_ERR_MSG(extack, | |
3071 | "Invalid local interface, device not found"); | |
34e02aa1 | 3072 | return -ENODEV; |
653ef6a3 | 3073 | } |
d342894c | 3074 | |
e4c7ed41 CW |
3075 | #if IS_ENABLED(CONFIG_IPV6) |
3076 | if (use_ipv6) { | |
3077 | struct inet6_dev *idev = __in6_dev_get(lowerdev); | |
653ef6a3 GM |
3078 | if (idev && idev->cnf.disable_ipv6) { |
3079 | NL_SET_ERR_MSG(extack, | |
3080 | "IPv6 support disabled by administrator"); | |
e4c7ed41 | 3081 | return -EPERM; |
653ef6a3 | 3082 | } |
e4c7ed41 CW |
3083 | } |
3084 | #endif | |
3085 | ||
a985343b MS |
3086 | *lower = lowerdev; |
3087 | } else { | |
653ef6a3 GM |
3088 | if (vxlan_addr_multicast(&conf->remote_ip)) { |
3089 | NL_SET_ERR_MSG(extack, | |
3090 | "Local interface required for multicast remote destination"); | |
3091 | ||
a985343b | 3092 | return -EINVAL; |
653ef6a3 | 3093 | } |
1ba56fb4 | 3094 | |
0f22a3c6 | 3095 | #if IS_ENABLED(CONFIG_IPV6) |
653ef6a3 GM |
3096 | if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) { |
3097 | NL_SET_ERR_MSG(extack, | |
3098 | "Local interface required for link-local local/remote addresses"); | |
0f22a3c6 | 3099 | return -EINVAL; |
653ef6a3 | 3100 | } |
0f22a3c6 MS |
3101 | #endif |
3102 | ||
a985343b | 3103 | *lower = NULL; |
9dc2ad10 | 3104 | } |
d342894c | 3105 | |
a985343b MS |
3106 | if (!conf->dst_port) { |
3107 | if (conf->flags & VXLAN_F_GPE) | |
3108 | conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */ | |
3109 | else | |
3110 | conf->dst_port = htons(vxlan_port); | |
d6acfeb1 FM |
3111 | } |
3112 | ||
a985343b MS |
3113 | if (!conf->age_interval) |
3114 | conf->age_interval = FDB_AGE_DEFAULT; | |
91572088 | 3115 | |
a985343b MS |
3116 | list_for_each_entry(tmp, &vn->vxlan_list, next) { |
3117 | if (tmp == old) | |
3118 | continue; | |
91572088 | 3119 | |
49f810f0 MS |
3120 | if (tmp->cfg.vni != conf->vni) |
3121 | continue; | |
3122 | if (tmp->cfg.dst_port != conf->dst_port) | |
3123 | continue; | |
3124 | if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) != | |
ce44a4ae | 3125 | (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6))) |
49f810f0 MS |
3126 | continue; |
3127 | ||
3128 | if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) && | |
3129 | tmp->cfg.remote_ifindex != conf->remote_ifindex) | |
3130 | continue; | |
3131 | ||
653ef6a3 GM |
3132 | NL_SET_ERR_MSG(extack, |
3133 | "A VXLAN device with the specified VNI already exists"); | |
49f810f0 | 3134 | return -EEXIST; |
a985343b | 3135 | } |
91572088 | 3136 | |
a985343b MS |
3137 | return 0; |
3138 | } | |
3139 | ||
3140 | static void vxlan_config_apply(struct net_device *dev, | |
3141 | struct vxlan_config *conf, | |
889ce937 SD |
3142 | struct net_device *lowerdev, |
3143 | struct net *src_net, | |
3144 | bool changelink) | |
a985343b MS |
3145 | { |
3146 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3147 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
3148 | unsigned short needed_headroom = ETH_HLEN; | |
3149 | bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6); | |
3150 | int max_mtu = ETH_MAX_MTU; | |
3151 | ||
3152 | if (!changelink) { | |
3153 | if (conf->flags & VXLAN_F_GPE) | |
3154 | vxlan_raw_setup(dev); | |
3155 | else | |
3156 | vxlan_ether_setup(dev); | |
3157 | ||
3158 | if (conf->mtu) | |
3159 | dev->mtu = conf->mtu; | |
889ce937 SD |
3160 | |
3161 | vxlan->net = src_net; | |
a985343b MS |
3162 | } |
3163 | ||
3164 | dst->remote_vni = conf->vni; | |
3165 | ||
3166 | memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip)); | |
3167 | ||
3168 | if (lowerdev) { | |
3169 | dst->remote_ifindex = conf->remote_ifindex; | |
3170 | ||
3171 | dev->gso_max_size = lowerdev->gso_max_size; | |
3172 | dev->gso_max_segs = lowerdev->gso_max_segs; | |
91572088 | 3173 | |
a985343b | 3174 | needed_headroom = lowerdev->hard_header_len; |
91572088 | 3175 | |
a985343b MS |
3176 | max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : |
3177 | VXLAN_HEADROOM); | |
f870c1ff AK |
3178 | if (max_mtu < ETH_MIN_MTU) |
3179 | max_mtu = ETH_MIN_MTU; | |
3180 | ||
3181 | if (!changelink && !conf->mtu) | |
3182 | dev->mtu = max_mtu; | |
7e059158 DW |
3183 | } |
3184 | ||
a985343b MS |
3185 | if (dev->mtu > max_mtu) |
3186 | dev->mtu = max_mtu; | |
3187 | ||
b1be00a6 JB |
3188 | if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA) |
3189 | needed_headroom += VXLAN6_HEADROOM; | |
3190 | else | |
3191 | needed_headroom += VXLAN_HEADROOM; | |
3192 | dev->needed_headroom = needed_headroom; | |
3193 | ||
0dfbdf41 | 3194 | memcpy(&vxlan->cfg, conf, sizeof(*conf)); |
a985343b | 3195 | } |
0dfbdf41 | 3196 | |
a985343b | 3197 | static int vxlan_dev_configure(struct net *src_net, struct net_device *dev, |
653ef6a3 GM |
3198 | struct vxlan_config *conf, bool changelink, |
3199 | struct netlink_ext_ack *extack) | |
a985343b MS |
3200 | { |
3201 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3202 | struct net_device *lowerdev; | |
3203 | int ret; | |
0dfbdf41 | 3204 | |
653ef6a3 | 3205 | ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack); |
a985343b MS |
3206 | if (ret) |
3207 | return ret; | |
8bcdc4f3 | 3208 | |
889ce937 | 3209 | vxlan_config_apply(dev, conf, lowerdev, src_net, changelink); |
0dfbdf41 | 3210 | |
0dfbdf41 TG |
3211 | return 0; |
3212 | } | |
3213 | ||
c80498e3 | 3214 | static int __vxlan_dev_create(struct net *net, struct net_device *dev, |
653ef6a3 GM |
3215 | struct vxlan_config *conf, |
3216 | struct netlink_ext_ack *extack) | |
c80498e3 ND |
3217 | { |
3218 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
3219 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
0241b836 | 3220 | struct vxlan_fdb *f = NULL; |
c80498e3 ND |
3221 | int err; |
3222 | ||
653ef6a3 | 3223 | err = vxlan_dev_configure(net, dev, conf, false, extack); |
c80498e3 ND |
3224 | if (err) |
3225 | return err; | |
3226 | ||
3227 | dev->ethtool_ops = &vxlan_ethtool_ops; | |
3228 | ||
3229 | /* create an fdb entry for a valid default destination */ | |
3230 | if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) { | |
0241b836 | 3231 | err = vxlan_fdb_create(vxlan, all_zeros_mac, |
c80498e3 ND |
3232 | &vxlan->default_dst.remote_ip, |
3233 | NUD_REACHABLE | NUD_PERMANENT, | |
c80498e3 ND |
3234 | vxlan->cfg.dst_port, |
3235 | vxlan->default_dst.remote_vni, | |
3236 | vxlan->default_dst.remote_vni, | |
3237 | vxlan->default_dst.remote_ifindex, | |
0241b836 | 3238 | NTF_SELF, &f); |
c80498e3 ND |
3239 | if (err) |
3240 | return err; | |
3241 | } | |
3242 | ||
3243 | err = register_netdevice(dev); | |
0241b836 RP |
3244 | if (err) |
3245 | goto errout; | |
3246 | ||
3247 | err = rtnl_configure_link(dev, NULL); | |
c80498e3 | 3248 | if (err) { |
0241b836 RP |
3249 | unregister_netdevice(dev); |
3250 | goto errout; | |
c80498e3 ND |
3251 | } |
3252 | ||
0241b836 RP |
3253 | /* notify default fdb entry */ |
3254 | if (f) | |
3255 | vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH); | |
3256 | ||
c80498e3 ND |
3257 | list_add(&vxlan->next, &vn->vxlan_list); |
3258 | return 0; | |
0241b836 RP |
3259 | errout: |
3260 | if (f) | |
3261 | vxlan_fdb_destroy(vxlan, f, false); | |
3262 | return err; | |
c80498e3 ND |
3263 | } |
3264 | ||
8bcdc4f3 RP |
3265 | static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[], |
3266 | struct net_device *dev, struct vxlan_config *conf, | |
3267 | bool changelink) | |
0dfbdf41 | 3268 | { |
8bcdc4f3 | 3269 | struct vxlan_dev *vxlan = netdev_priv(dev); |
0dfbdf41 | 3270 | |
8bcdc4f3 | 3271 | memset(conf, 0, sizeof(*conf)); |
e277de5f | 3272 | |
8bcdc4f3 RP |
3273 | /* if changelink operation, start with old existing cfg */ |
3274 | if (changelink) | |
3275 | memcpy(conf, &vxlan->cfg, sizeof(*conf)); | |
3276 | ||
3277 | if (data[IFLA_VXLAN_ID]) { | |
3278 | __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID])); | |
3279 | ||
3280 | if (changelink && (vni != conf->vni)) | |
3281 | return -EOPNOTSUPP; | |
3282 | conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID])); | |
3283 | } | |
0dfbdf41 TG |
3284 | |
3285 | if (data[IFLA_VXLAN_GROUP]) { | |
ce44a4ae MS |
3286 | if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) |
3287 | return -EOPNOTSUPP; | |
3288 | ||
8bcdc4f3 | 3289 | conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]); |
ce44a4ae | 3290 | conf->remote_ip.sa.sa_family = AF_INET; |
0dfbdf41 TG |
3291 | } else if (data[IFLA_VXLAN_GROUP6]) { |
3292 | if (!IS_ENABLED(CONFIG_IPV6)) | |
3293 | return -EPFNOSUPPORT; | |
3294 | ||
ce44a4ae MS |
3295 | if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) |
3296 | return -EOPNOTSUPP; | |
3297 | ||
8bcdc4f3 RP |
3298 | conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]); |
3299 | conf->remote_ip.sa.sa_family = AF_INET6; | |
0dfbdf41 TG |
3300 | } |
3301 | ||
3302 | if (data[IFLA_VXLAN_LOCAL]) { | |
ce44a4ae MS |
3303 | if (changelink && (conf->saddr.sa.sa_family != AF_INET)) |
3304 | return -EOPNOTSUPP; | |
3305 | ||
8bcdc4f3 RP |
3306 | conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]); |
3307 | conf->saddr.sa.sa_family = AF_INET; | |
0dfbdf41 TG |
3308 | } else if (data[IFLA_VXLAN_LOCAL6]) { |
3309 | if (!IS_ENABLED(CONFIG_IPV6)) | |
3310 | return -EPFNOSUPPORT; | |
3311 | ||
ce44a4ae MS |
3312 | if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) |
3313 | return -EOPNOTSUPP; | |
3314 | ||
0dfbdf41 | 3315 | /* TODO: respect scope id */ |
8bcdc4f3 RP |
3316 | conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]); |
3317 | conf->saddr.sa.sa_family = AF_INET6; | |
0dfbdf41 TG |
3318 | } |
3319 | ||
3320 | if (data[IFLA_VXLAN_LINK]) | |
8bcdc4f3 | 3321 | conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]); |
0dfbdf41 | 3322 | |
d342894c | 3323 | if (data[IFLA_VXLAN_TOS]) |
8bcdc4f3 | 3324 | conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]); |
d342894c | 3325 | |
afb97186 | 3326 | if (data[IFLA_VXLAN_TTL]) |
8bcdc4f3 | 3327 | conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]); |
afb97186 | 3328 | |
72f6d71e HL |
3329 | if (data[IFLA_VXLAN_TTL_INHERIT]) { |
3330 | if (changelink) | |
3331 | return -EOPNOTSUPP; | |
3332 | conf->flags |= VXLAN_F_TTL_INHERIT; | |
3333 | } | |
3334 | ||
e7f70af1 | 3335 | if (data[IFLA_VXLAN_LABEL]) |
8bcdc4f3 | 3336 | conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) & |
e7f70af1 DB |
3337 | IPV6_FLOWLABEL_MASK; |
3338 | ||
8bcdc4f3 | 3339 | if (data[IFLA_VXLAN_LEARNING]) { |
dc5321d7 | 3340 | if (nla_get_u8(data[IFLA_VXLAN_LEARNING])) |
8bcdc4f3 | 3341 | conf->flags |= VXLAN_F_LEARN; |
dc5321d7 | 3342 | else |
8bcdc4f3 | 3343 | conf->flags &= ~VXLAN_F_LEARN; |
8bcdc4f3 RP |
3344 | } else if (!changelink) { |
3345 | /* default to learn on a new device */ | |
3346 | conf->flags |= VXLAN_F_LEARN; | |
3347 | } | |
d342894c | 3348 | |
8bcdc4f3 RP |
3349 | if (data[IFLA_VXLAN_AGEING]) { |
3350 | if (changelink) | |
3351 | return -EOPNOTSUPP; | |
3352 | conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]); | |
3353 | } | |
d342894c | 3354 | |
8bcdc4f3 RP |
3355 | if (data[IFLA_VXLAN_PROXY]) { |
3356 | if (changelink) | |
3357 | return -EOPNOTSUPP; | |
3358 | if (nla_get_u8(data[IFLA_VXLAN_PROXY])) | |
3359 | conf->flags |= VXLAN_F_PROXY; | |
3360 | } | |
e4f67add | 3361 | |
8bcdc4f3 RP |
3362 | if (data[IFLA_VXLAN_RSC]) { |
3363 | if (changelink) | |
3364 | return -EOPNOTSUPP; | |
3365 | if (nla_get_u8(data[IFLA_VXLAN_RSC])) | |
3366 | conf->flags |= VXLAN_F_RSC; | |
3367 | } | |
e4f67add | 3368 | |
8bcdc4f3 RP |
3369 | if (data[IFLA_VXLAN_L2MISS]) { |
3370 | if (changelink) | |
3371 | return -EOPNOTSUPP; | |
3372 | if (nla_get_u8(data[IFLA_VXLAN_L2MISS])) | |
3373 | conf->flags |= VXLAN_F_L2MISS; | |
3374 | } | |
e4f67add | 3375 | |
8bcdc4f3 RP |
3376 | if (data[IFLA_VXLAN_L3MISS]) { |
3377 | if (changelink) | |
3378 | return -EOPNOTSUPP; | |
3379 | if (nla_get_u8(data[IFLA_VXLAN_L3MISS])) | |
3380 | conf->flags |= VXLAN_F_L3MISS; | |
3381 | } | |
e4f67add | 3382 | |
8bcdc4f3 RP |
3383 | if (data[IFLA_VXLAN_LIMIT]) { |
3384 | if (changelink) | |
3385 | return -EOPNOTSUPP; | |
3386 | conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]); | |
3387 | } | |
d342894c | 3388 | |
8bcdc4f3 RP |
3389 | if (data[IFLA_VXLAN_COLLECT_METADATA]) { |
3390 | if (changelink) | |
3391 | return -EOPNOTSUPP; | |
3392 | if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA])) | |
3393 | conf->flags |= VXLAN_F_COLLECT_METADATA; | |
3394 | } | |
f8a9b1bc | 3395 | |
05f47d69 | 3396 | if (data[IFLA_VXLAN_PORT_RANGE]) { |
8bcdc4f3 RP |
3397 | if (!changelink) { |
3398 | const struct ifla_vxlan_port_range *p | |
3399 | = nla_data(data[IFLA_VXLAN_PORT_RANGE]); | |
3400 | conf->port_min = ntohs(p->low); | |
3401 | conf->port_max = ntohs(p->high); | |
3402 | } else { | |
3403 | return -EOPNOTSUPP; | |
3404 | } | |
05f47d69 | 3405 | } |
3406 | ||
8bcdc4f3 RP |
3407 | if (data[IFLA_VXLAN_PORT]) { |
3408 | if (changelink) | |
3409 | return -EOPNOTSUPP; | |
3410 | conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]); | |
3411 | } | |
823aa873 | 3412 | |
8bcdc4f3 RP |
3413 | if (data[IFLA_VXLAN_UDP_CSUM]) { |
3414 | if (changelink) | |
3415 | return -EOPNOTSUPP; | |
3416 | if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM])) | |
3417 | conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX; | |
3418 | } | |
359a0ea9 | 3419 | |
8bcdc4f3 RP |
3420 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) { |
3421 | if (changelink) | |
3422 | return -EOPNOTSUPP; | |
3423 | if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX])) | |
3424 | conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX; | |
3425 | } | |
359a0ea9 | 3426 | |
8bcdc4f3 RP |
3427 | if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) { |
3428 | if (changelink) | |
3429 | return -EOPNOTSUPP; | |
3430 | if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX])) | |
3431 | conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX; | |
3432 | } | |
359a0ea9 | 3433 | |
8bcdc4f3 RP |
3434 | if (data[IFLA_VXLAN_REMCSUM_TX]) { |
3435 | if (changelink) | |
3436 | return -EOPNOTSUPP; | |
3437 | if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX])) | |
3438 | conf->flags |= VXLAN_F_REMCSUM_TX; | |
3439 | } | |
dfd8645e | 3440 | |
8bcdc4f3 RP |
3441 | if (data[IFLA_VXLAN_REMCSUM_RX]) { |
3442 | if (changelink) | |
3443 | return -EOPNOTSUPP; | |
3444 | if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX])) | |
3445 | conf->flags |= VXLAN_F_REMCSUM_RX; | |
3446 | } | |
3447 | ||
3448 | if (data[IFLA_VXLAN_GBP]) { | |
3449 | if (changelink) | |
3450 | return -EOPNOTSUPP; | |
3451 | conf->flags |= VXLAN_F_GBP; | |
3452 | } | |
3453 | ||
3454 | if (data[IFLA_VXLAN_GPE]) { | |
3455 | if (changelink) | |
3456 | return -EOPNOTSUPP; | |
3457 | conf->flags |= VXLAN_F_GPE; | |
3458 | } | |
3459 | ||
3460 | if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) { | |
3461 | if (changelink) | |
3462 | return -EOPNOTSUPP; | |
3463 | conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL; | |
3464 | } | |
3465 | ||
3466 | if (tb[IFLA_MTU]) { | |
3467 | if (changelink) | |
3468 | return -EOPNOTSUPP; | |
3469 | conf->mtu = nla_get_u32(tb[IFLA_MTU]); | |
3470 | } | |
3471 | ||
3472 | return 0; | |
3473 | } | |
3474 | ||
3475 | static int vxlan_newlink(struct net *src_net, struct net_device *dev, | |
7a3f4a18 MS |
3476 | struct nlattr *tb[], struct nlattr *data[], |
3477 | struct netlink_ext_ack *extack) | |
8bcdc4f3 | 3478 | { |
8bcdc4f3 RP |
3479 | struct vxlan_config conf; |
3480 | int err; | |
3481 | ||
3482 | err = vxlan_nl2conf(tb, data, dev, &conf, false); | |
3483 | if (err) | |
3484 | return err; | |
3485 | ||
653ef6a3 | 3486 | return __vxlan_dev_create(src_net, dev, &conf, extack); |
8bcdc4f3 | 3487 | } |
dfd8645e | 3488 | |
8bcdc4f3 | 3489 | static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[], |
ad744b22 MS |
3490 | struct nlattr *data[], |
3491 | struct netlink_ext_ack *extack) | |
8bcdc4f3 RP |
3492 | { |
3493 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3494 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
3495 | struct vxlan_rdst old_dst; | |
3496 | struct vxlan_config conf; | |
0241b836 | 3497 | struct vxlan_fdb *f = NULL; |
8bcdc4f3 RP |
3498 | int err; |
3499 | ||
3500 | err = vxlan_nl2conf(tb, data, | |
3501 | dev, &conf, true); | |
3502 | if (err) | |
3503 | return err; | |
3511494c | 3504 | |
8bcdc4f3 | 3505 | memcpy(&old_dst, dst, sizeof(struct vxlan_rdst)); |
e1e5314d | 3506 | |
653ef6a3 | 3507 | err = vxlan_dev_configure(vxlan->net, dev, &conf, true, extack); |
8bcdc4f3 RP |
3508 | if (err) |
3509 | return err; | |
0ace2ca8 | 3510 | |
8bcdc4f3 RP |
3511 | /* handle default dst entry */ |
3512 | if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) { | |
3513 | spin_lock_bh(&vxlan->hash_lock); | |
3514 | if (!vxlan_addr_any(&old_dst.remote_ip)) | |
3515 | __vxlan_fdb_delete(vxlan, all_zeros_mac, | |
3516 | old_dst.remote_ip, | |
3517 | vxlan->cfg.dst_port, | |
3518 | old_dst.remote_vni, | |
3519 | old_dst.remote_vni, | |
3520 | old_dst.remote_ifindex, 0); | |
3521 | ||
3522 | if (!vxlan_addr_any(&dst->remote_ip)) { | |
0241b836 | 3523 | err = vxlan_fdb_create(vxlan, all_zeros_mac, |
8bcdc4f3 RP |
3524 | &dst->remote_ip, |
3525 | NUD_REACHABLE | NUD_PERMANENT, | |
8bcdc4f3 RP |
3526 | vxlan->cfg.dst_port, |
3527 | dst->remote_vni, | |
3528 | dst->remote_vni, | |
3529 | dst->remote_ifindex, | |
0241b836 | 3530 | NTF_SELF, &f); |
8bcdc4f3 RP |
3531 | if (err) { |
3532 | spin_unlock_bh(&vxlan->hash_lock); | |
3533 | return err; | |
3534 | } | |
0241b836 | 3535 | vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH); |
8bcdc4f3 RP |
3536 | } |
3537 | spin_unlock_bh(&vxlan->hash_lock); | |
3538 | } | |
ce577668 | 3539 | |
8bcdc4f3 | 3540 | return 0; |
d342894c | 3541 | } |
3542 | ||
3543 | static void vxlan_dellink(struct net_device *dev, struct list_head *head) | |
3544 | { | |
3545 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3546 | ||
8b3f9337 RP |
3547 | vxlan_flush(vxlan, true); |
3548 | ||
58ce31cc | 3549 | gro_cells_destroy(&vxlan->gro_cells); |
553675fb | 3550 | list_del(&vxlan->next); |
d342894c | 3551 | unregister_netdevice_queue(dev, head); |
3552 | } | |
3553 | ||
3554 | static size_t vxlan_get_size(const struct net_device *dev) | |
3555 | { | |
3556 | ||
3557 | return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */ | |
e4c7ed41 | 3558 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */ |
d342894c | 3559 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */ |
e4c7ed41 | 3560 | nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */ |
d342894c | 3561 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */ |
8fd78069 | 3562 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */ |
d342894c | 3563 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */ |
e7f70af1 | 3564 | nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */ |
d342894c | 3565 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */ |
e4f67add DS |
3566 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */ |
3567 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */ | |
3568 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */ | |
3569 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */ | |
da8b43c0 | 3570 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */ |
d342894c | 3571 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */ |
3572 | nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */ | |
05f47d69 | 3573 | nla_total_size(sizeof(struct ifla_vxlan_port_range)) + |
359a0ea9 TH |
3574 | nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */ |
3575 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */ | |
3576 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */ | |
3577 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */ | |
dfd8645e TH |
3578 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */ |
3579 | nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */ | |
d342894c | 3580 | 0; |
3581 | } | |
3582 | ||
3583 | static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev) | |
3584 | { | |
3585 | const struct vxlan_dev *vxlan = netdev_priv(dev); | |
c7995c43 | 3586 | const struct vxlan_rdst *dst = &vxlan->default_dst; |
05f47d69 | 3587 | struct ifla_vxlan_port_range ports = { |
0dfbdf41 TG |
3588 | .low = htons(vxlan->cfg.port_min), |
3589 | .high = htons(vxlan->cfg.port_max), | |
05f47d69 | 3590 | }; |
d342894c | 3591 | |
54bfd872 | 3592 | if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni))) |
d342894c | 3593 | goto nla_put_failure; |
3594 | ||
e4c7ed41 CW |
3595 | if (!vxlan_addr_any(&dst->remote_ip)) { |
3596 | if (dst->remote_ip.sa.sa_family == AF_INET) { | |
930345ea JB |
3597 | if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP, |
3598 | dst->remote_ip.sin.sin_addr.s_addr)) | |
e4c7ed41 CW |
3599 | goto nla_put_failure; |
3600 | #if IS_ENABLED(CONFIG_IPV6) | |
3601 | } else { | |
930345ea JB |
3602 | if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6, |
3603 | &dst->remote_ip.sin6.sin6_addr)) | |
e4c7ed41 CW |
3604 | goto nla_put_failure; |
3605 | #endif | |
3606 | } | |
3607 | } | |
d342894c | 3608 | |
c7995c43 | 3609 | if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex)) |
d342894c | 3610 | goto nla_put_failure; |
3611 | ||
0dfbdf41 TG |
3612 | if (!vxlan_addr_any(&vxlan->cfg.saddr)) { |
3613 | if (vxlan->cfg.saddr.sa.sa_family == AF_INET) { | |
930345ea | 3614 | if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL, |
0dfbdf41 | 3615 | vxlan->cfg.saddr.sin.sin_addr.s_addr)) |
e4c7ed41 CW |
3616 | goto nla_put_failure; |
3617 | #if IS_ENABLED(CONFIG_IPV6) | |
3618 | } else { | |
930345ea | 3619 | if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6, |
0dfbdf41 | 3620 | &vxlan->cfg.saddr.sin6.sin6_addr)) |
e4c7ed41 CW |
3621 | goto nla_put_failure; |
3622 | #endif | |
3623 | } | |
3624 | } | |
d342894c | 3625 | |
0dfbdf41 | 3626 | if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) || |
8fd78069 HL |
3627 | nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT, |
3628 | !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) || | |
0dfbdf41 | 3629 | nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) || |
e7f70af1 | 3630 | nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) || |
e4f67add | 3631 | nla_put_u8(skb, IFLA_VXLAN_LEARNING, |
dc5321d7 | 3632 | !!(vxlan->cfg.flags & VXLAN_F_LEARN)) || |
e4f67add | 3633 | nla_put_u8(skb, IFLA_VXLAN_PROXY, |
dc5321d7 MS |
3634 | !!(vxlan->cfg.flags & VXLAN_F_PROXY)) || |
3635 | nla_put_u8(skb, IFLA_VXLAN_RSC, | |
3636 | !!(vxlan->cfg.flags & VXLAN_F_RSC)) || | |
e4f67add | 3637 | nla_put_u8(skb, IFLA_VXLAN_L2MISS, |
dc5321d7 | 3638 | !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) || |
e4f67add | 3639 | nla_put_u8(skb, IFLA_VXLAN_L3MISS, |
dc5321d7 | 3640 | !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) || |
da8b43c0 | 3641 | nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA, |
dc5321d7 | 3642 | !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) || |
0dfbdf41 TG |
3643 | nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) || |
3644 | nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) || | |
3645 | nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) || | |
359a0ea9 | 3646 | nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM, |
dc5321d7 | 3647 | !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) || |
359a0ea9 | 3648 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, |
dc5321d7 | 3649 | !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) || |
359a0ea9 | 3650 | nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, |
dc5321d7 | 3651 | !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) || |
dfd8645e | 3652 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX, |
dc5321d7 | 3653 | !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) || |
dfd8645e | 3654 | nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX, |
dc5321d7 | 3655 | !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX))) |
d342894c | 3656 | goto nla_put_failure; |
3657 | ||
05f47d69 | 3658 | if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports)) |
3659 | goto nla_put_failure; | |
3660 | ||
dc5321d7 | 3661 | if (vxlan->cfg.flags & VXLAN_F_GBP && |
3511494c TG |
3662 | nla_put_flag(skb, IFLA_VXLAN_GBP)) |
3663 | goto nla_put_failure; | |
3664 | ||
dc5321d7 | 3665 | if (vxlan->cfg.flags & VXLAN_F_GPE && |
e1e5314d JB |
3666 | nla_put_flag(skb, IFLA_VXLAN_GPE)) |
3667 | goto nla_put_failure; | |
3668 | ||
dc5321d7 | 3669 | if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL && |
0ace2ca8 TH |
3670 | nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL)) |
3671 | goto nla_put_failure; | |
3672 | ||
d342894c | 3673 | return 0; |
3674 | ||
3675 | nla_put_failure: | |
3676 | return -EMSGSIZE; | |
3677 | } | |
3678 | ||
1728d4fa ND |
3679 | static struct net *vxlan_get_link_net(const struct net_device *dev) |
3680 | { | |
3681 | struct vxlan_dev *vxlan = netdev_priv(dev); | |
3682 | ||
3683 | return vxlan->net; | |
3684 | } | |
3685 | ||
d342894c | 3686 | static struct rtnl_link_ops vxlan_link_ops __read_mostly = { |
3687 | .kind = "vxlan", | |
3688 | .maxtype = IFLA_VXLAN_MAX, | |
3689 | .policy = vxlan_policy, | |
3690 | .priv_size = sizeof(struct vxlan_dev), | |
3691 | .setup = vxlan_setup, | |
3692 | .validate = vxlan_validate, | |
3693 | .newlink = vxlan_newlink, | |
8bcdc4f3 | 3694 | .changelink = vxlan_changelink, |
d342894c | 3695 | .dellink = vxlan_dellink, |
3696 | .get_size = vxlan_get_size, | |
3697 | .fill_info = vxlan_fill_info, | |
1728d4fa | 3698 | .get_link_net = vxlan_get_link_net, |
d342894c | 3699 | }; |
3700 | ||
cf5da330 ND |
3701 | struct net_device *vxlan_dev_create(struct net *net, const char *name, |
3702 | u8 name_assign_type, | |
3703 | struct vxlan_config *conf) | |
3704 | { | |
3705 | struct nlattr *tb[IFLA_MAX + 1]; | |
3706 | struct net_device *dev; | |
3707 | int err; | |
3708 | ||
3709 | memset(&tb, 0, sizeof(tb)); | |
3710 | ||
3711 | dev = rtnl_create_link(net, name, name_assign_type, | |
3712 | &vxlan_link_ops, tb); | |
3713 | if (IS_ERR(dev)) | |
3714 | return dev; | |
3715 | ||
653ef6a3 | 3716 | err = __vxlan_dev_create(net, dev, conf, NULL); |
cf5da330 ND |
3717 | if (err < 0) { |
3718 | free_netdev(dev); | |
3719 | return ERR_PTR(err); | |
3720 | } | |
3721 | ||
3722 | err = rtnl_configure_link(dev, NULL); | |
3723 | if (err < 0) { | |
3724 | LIST_HEAD(list_kill); | |
3725 | ||
3726 | vxlan_dellink(dev, &list_kill); | |
3727 | unregister_netdevice_many(&list_kill); | |
3728 | return ERR_PTR(err); | |
3729 | } | |
3730 | ||
3731 | return dev; | |
3732 | } | |
3733 | EXPORT_SYMBOL_GPL(vxlan_dev_create); | |
3734 | ||
acaf4e70 DB |
3735 | static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn, |
3736 | struct net_device *dev) | |
3737 | { | |
3738 | struct vxlan_dev *vxlan, *next; | |
3739 | LIST_HEAD(list_kill); | |
3740 | ||
3741 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { | |
3742 | struct vxlan_rdst *dst = &vxlan->default_dst; | |
3743 | ||
3744 | /* In case we created vxlan device with carrier | |
3745 | * and we loose the carrier due to module unload | |
3746 | * we also need to remove vxlan device. In other | |
3747 | * cases, it's not necessary and remote_ifindex | |
3748 | * is 0 here, so no matches. | |
3749 | */ | |
3750 | if (dst->remote_ifindex == dev->ifindex) | |
3751 | vxlan_dellink(vxlan->dev, &list_kill); | |
3752 | } | |
3753 | ||
3754 | unregister_netdevice_many(&list_kill); | |
3755 | } | |
3756 | ||
b7aade15 HFS |
3757 | static int vxlan_netdevice_event(struct notifier_block *unused, |
3758 | unsigned long event, void *ptr) | |
acaf4e70 DB |
3759 | { |
3760 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
783c1463 | 3761 | struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); |
acaf4e70 | 3762 | |
04584957 SD |
3763 | if (event == NETDEV_UNREGISTER) { |
3764 | vxlan_offload_rx_ports(dev, false); | |
acaf4e70 | 3765 | vxlan_handle_lowerdev_unregister(vn, dev); |
04584957 SD |
3766 | } else if (event == NETDEV_REGISTER) { |
3767 | vxlan_offload_rx_ports(dev, true); | |
3768 | } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO || | |
3769 | event == NETDEV_UDP_TUNNEL_DROP_INFO) { | |
2d2b13fc | 3770 | vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO); |
04584957 | 3771 | } |
acaf4e70 DB |
3772 | |
3773 | return NOTIFY_DONE; | |
3774 | } | |
3775 | ||
3776 | static struct notifier_block vxlan_notifier_block __read_mostly = { | |
b7aade15 | 3777 | .notifier_call = vxlan_netdevice_event, |
acaf4e70 DB |
3778 | }; |
3779 | ||
d342894c | 3780 | static __net_init int vxlan_init_net(struct net *net) |
3781 | { | |
3782 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
31fec5aa | 3783 | unsigned int h; |
d342894c | 3784 | |
553675fb | 3785 | INIT_LIST_HEAD(&vn->vxlan_list); |
1c51a915 | 3786 | spin_lock_init(&vn->sock_lock); |
d342894c | 3787 | |
553675fb | 3788 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
3789 | INIT_HLIST_HEAD(&vn->sock_list[h]); | |
d342894c | 3790 | |
3791 | return 0; | |
3792 | } | |
3793 | ||
57b61127 | 3794 | static void vxlan_destroy_tunnels(struct net *net, struct list_head *head) |
f01ec1c0 ND |
3795 | { |
3796 | struct vxlan_net *vn = net_generic(net, vxlan_net_id); | |
3797 | struct vxlan_dev *vxlan, *next; | |
3798 | struct net_device *dev, *aux; | |
0e4ec5ac | 3799 | unsigned int h; |
f01ec1c0 | 3800 | |
f01ec1c0 ND |
3801 | for_each_netdev_safe(net, dev, aux) |
3802 | if (dev->rtnl_link_ops == &vxlan_link_ops) | |
57b61127 | 3803 | unregister_netdevice_queue(dev, head); |
f01ec1c0 ND |
3804 | |
3805 | list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) { | |
3806 | /* If vxlan->dev is in the same netns, it has already been added | |
3807 | * to the list by the previous loop. | |
3808 | */ | |
58ce31cc TH |
3809 | if (!net_eq(dev_net(vxlan->dev), net)) { |
3810 | gro_cells_destroy(&vxlan->gro_cells); | |
57b61127 | 3811 | unregister_netdevice_queue(vxlan->dev, head); |
58ce31cc | 3812 | } |
f01ec1c0 ND |
3813 | } |
3814 | ||
0e4ec5ac VA |
3815 | for (h = 0; h < PORT_HASH_SIZE; ++h) |
3816 | WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h])); | |
f01ec1c0 ND |
3817 | } |
3818 | ||
57b61127 HY |
3819 | static void __net_exit vxlan_exit_batch_net(struct list_head *net_list) |
3820 | { | |
3821 | struct net *net; | |
3822 | LIST_HEAD(list); | |
3823 | ||
3824 | rtnl_lock(); | |
3825 | list_for_each_entry(net, net_list, exit_list) | |
3826 | vxlan_destroy_tunnels(net, &list); | |
3827 | ||
3828 | unregister_netdevice_many(&list); | |
3829 | rtnl_unlock(); | |
3830 | } | |
3831 | ||
d342894c | 3832 | static struct pernet_operations vxlan_net_ops = { |
3833 | .init = vxlan_init_net, | |
57b61127 | 3834 | .exit_batch = vxlan_exit_batch_net, |
d342894c | 3835 | .id = &vxlan_net_id, |
3836 | .size = sizeof(struct vxlan_net), | |
3837 | }; | |
3838 | ||
3839 | static int __init vxlan_init_module(void) | |
3840 | { | |
3841 | int rc; | |
3842 | ||
3843 | get_random_bytes(&vxlan_salt, sizeof(vxlan_salt)); | |
3844 | ||
783c1463 | 3845 | rc = register_pernet_subsys(&vxlan_net_ops); |
d342894c | 3846 | if (rc) |
3847 | goto out1; | |
3848 | ||
acaf4e70 | 3849 | rc = register_netdevice_notifier(&vxlan_notifier_block); |
d342894c | 3850 | if (rc) |
3851 | goto out2; | |
3852 | ||
acaf4e70 DB |
3853 | rc = rtnl_link_register(&vxlan_link_ops); |
3854 | if (rc) | |
3855 | goto out3; | |
d342894c | 3856 | |
acaf4e70 DB |
3857 | return 0; |
3858 | out3: | |
3859 | unregister_netdevice_notifier(&vxlan_notifier_block); | |
d342894c | 3860 | out2: |
783c1463 | 3861 | unregister_pernet_subsys(&vxlan_net_ops); |
d342894c | 3862 | out1: |
3863 | return rc; | |
3864 | } | |
7332a13b | 3865 | late_initcall(vxlan_init_module); |
d342894c | 3866 | |
3867 | static void __exit vxlan_cleanup_module(void) | |
3868 | { | |
b7153984 | 3869 | rtnl_link_unregister(&vxlan_link_ops); |
acaf4e70 | 3870 | unregister_netdevice_notifier(&vxlan_notifier_block); |
783c1463 DB |
3871 | unregister_pernet_subsys(&vxlan_net_ops); |
3872 | /* rcu_barrier() is called by netns */ | |
d342894c | 3873 | } |
3874 | module_exit(vxlan_cleanup_module); | |
3875 | ||
3876 | MODULE_LICENSE("GPL"); | |
3877 | MODULE_VERSION(VXLAN_VERSION); | |
3b8df3c6 | 3878 | MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>"); |
ead5139a | 3879 | MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic"); |
d342894c | 3880 | MODULE_ALIAS_RTNL_LINK("vxlan"); |