1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * VXLAN: Virtual eXtensible Local Area Network
4 *
5 * Copyright (c) 2012-2013 Vyatta Inc.
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/udp.h>
15 #include <linux/igmp.h>
16 #include <linux/if_ether.h>
17 #include <linux/ethtool.h>
18 #include <net/arp.h>
19 #include <net/ndisc.h>
20 #include <net/ipv6_stubs.h>
21 #include <net/ip.h>
22 #include <net/icmp.h>
23 #include <net/rtnetlink.h>
24 #include <net/inet_ecn.h>
25 #include <net/net_namespace.h>
26 #include <net/netns/generic.h>
27 #include <net/tun_proto.h>
28 #include <net/vxlan.h>
29 #include <net/nexthop.h>
30
31 #if IS_ENABLED(CONFIG_IPV6)
32 #include <net/ip6_tunnel.h>
33 #include <net/ip6_checksum.h>
34 #endif
35
36 #define VXLAN_VERSION "0.1"
37
38 #define PORT_HASH_BITS 8
39 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
40 #define FDB_AGE_DEFAULT 300 /* 5 min */
41 #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
42
43 /* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
45 * for compatibility with early adopters.
46 */
47 static unsigned short vxlan_port __read_mostly = 8472;
48 module_param_named(udp_port, vxlan_port, ushort, 0444);
49 MODULE_PARM_DESC(udp_port, "Destination UDP port");
50
51 static bool log_ecn_error = true;
52 module_param(log_ecn_error, bool, 0644);
53 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
54
55 static unsigned int vxlan_net_id;
56 static struct rtnl_link_ops vxlan_link_ops;
57
58 static const u8 all_zeros_mac[ETH_ALEN + 2];
59
60 static int vxlan_sock_add(struct vxlan_dev *vxlan);
61
62 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
63
64 /* per-network namespace private data for this module */
65 struct vxlan_net {
66 struct list_head vxlan_list;
67 struct hlist_head sock_list[PORT_HASH_SIZE];
68 spinlock_t sock_lock;
69 };
70
71 /* Forwarding table entry */
72 struct vxlan_fdb {
73 struct hlist_node hlist; /* linked list of entries */
74 struct rcu_head rcu;
75 unsigned long updated; /* jiffies */
76 unsigned long used;
77 struct list_head remotes;
78 u8 eth_addr[ETH_ALEN];
79 u16 state; /* see ndm_state */
80 __be32 vni;
81 u16 flags; /* see ndm_flags and below */
82 struct list_head nh_list;
83 struct nexthop __rcu *nh;
84 struct vxlan_dev __rcu *vdev;
85 };
86
87 #define NTF_VXLAN_ADDED_BY_USER 0x100
88
89 /* salt for hash table */
90 static u32 vxlan_salt __read_mostly;
91
vxlan_collect_metadata(struct vxlan_sock * vs)92 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
93 {
94 return vs->flags & VXLAN_F_COLLECT_METADATA ||
95 ip_tunnel_collect_metadata();
96 }
97
98 #if IS_ENABLED(CONFIG_IPV6)
99 static inline
vxlan_addr_equal(const union vxlan_addr * a,const union vxlan_addr * b)100 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
101 {
102 if (a->sa.sa_family != b->sa.sa_family)
103 return false;
104 if (a->sa.sa_family == AF_INET6)
105 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
106 else
107 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
108 }
109
vxlan_nla_get_addr(union vxlan_addr * ip,struct nlattr * nla)110 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
111 {
112 if (nla_len(nla) >= sizeof(struct in6_addr)) {
113 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
114 ip->sa.sa_family = AF_INET6;
115 return 0;
116 } else if (nla_len(nla) >= sizeof(__be32)) {
117 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
118 ip->sa.sa_family = AF_INET;
119 return 0;
120 } else {
121 return -EAFNOSUPPORT;
122 }
123 }
124
vxlan_nla_put_addr(struct sk_buff * skb,int attr,const union vxlan_addr * ip)125 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
126 const union vxlan_addr *ip)
127 {
128 if (ip->sa.sa_family == AF_INET6)
129 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
130 else
131 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
132 }
133
134 #else /* !CONFIG_IPV6 */
135
136 static inline
vxlan_addr_equal(const union vxlan_addr * a,const union vxlan_addr * b)137 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
138 {
139 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
140 }
141
vxlan_nla_get_addr(union vxlan_addr * ip,struct nlattr * nla)142 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
143 {
144 if (nla_len(nla) >= sizeof(struct in6_addr)) {
145 return -EAFNOSUPPORT;
146 } else if (nla_len(nla) >= sizeof(__be32)) {
147 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
148 ip->sa.sa_family = AF_INET;
149 return 0;
150 } else {
151 return -EAFNOSUPPORT;
152 }
153 }
154
vxlan_nla_put_addr(struct sk_buff * skb,int attr,const union vxlan_addr * ip)155 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
156 const union vxlan_addr *ip)
157 {
158 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
159 }
160 #endif
161
162 /* Virtual Network hash table head */
vni_head(struct vxlan_sock * vs,__be32 vni)163 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
164 {
165 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
166 }
167
168 /* Socket hash table head */
vs_head(struct net * net,__be16 port)169 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
170 {
171 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
172
173 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
174 }
175
176 /* First remote destination for a forwarding entry.
177 * Guaranteed to be non-NULL because remotes are never deleted.
178 */
first_remote_rcu(struct vxlan_fdb * fdb)179 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
180 {
181 if (rcu_access_pointer(fdb->nh))
182 return NULL;
183 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
184 }
185
first_remote_rtnl(struct vxlan_fdb * fdb)186 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
187 {
188 if (rcu_access_pointer(fdb->nh))
189 return NULL;
190 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
191 }
192
193 /* Find VXLAN socket based on network namespace, address family, UDP port,
194 * enabled unshareable flags and socket device binding (see l3mdev with
195 * non-default VRF).
196 */
vxlan_find_sock(struct net * net,sa_family_t family,__be16 port,u32 flags,int ifindex)197 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
198 __be16 port, u32 flags, int ifindex)
199 {
200 struct vxlan_sock *vs;
201
202 flags &= VXLAN_F_RCV_FLAGS;
203
204 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
205 if (inet_sk(vs->sock->sk)->inet_sport == port &&
206 vxlan_get_sk_family(vs) == family &&
207 vs->flags == flags &&
208 vs->sock->sk->sk_bound_dev_if == ifindex)
209 return vs;
210 }
211 return NULL;
212 }
213
vxlan_vs_find_vni(struct vxlan_sock * vs,int ifindex,__be32 vni)214 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
215 __be32 vni)
216 {
217 struct vxlan_dev_node *node;
218
219 /* For flow based devices, map all packets to VNI 0 */
220 if (vs->flags & VXLAN_F_COLLECT_METADATA)
221 vni = 0;
222
223 hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
224 if (node->vxlan->default_dst.remote_vni != vni)
225 continue;
226
227 if (IS_ENABLED(CONFIG_IPV6)) {
228 const struct vxlan_config *cfg = &node->vxlan->cfg;
229
230 if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
231 cfg->remote_ifindex != ifindex)
232 continue;
233 }
234
235 return node->vxlan;
236 }
237
238 return NULL;
239 }
240
241 /* Look up VNI in a per net namespace table */
vxlan_find_vni(struct net * net,int ifindex,__be32 vni,sa_family_t family,__be16 port,u32 flags)242 static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
243 __be32 vni, sa_family_t family,
244 __be16 port, u32 flags)
245 {
246 struct vxlan_sock *vs;
247
248 vs = vxlan_find_sock(net, family, port, flags, ifindex);
249 if (!vs)
250 return NULL;
251
252 return vxlan_vs_find_vni(vs, ifindex, vni);
253 }
254
255 /* Fill in neighbour message in skbuff. */
vxlan_fdb_info(struct sk_buff * skb,struct vxlan_dev * vxlan,const struct vxlan_fdb * fdb,u32 portid,u32 seq,int type,unsigned int flags,const struct vxlan_rdst * rdst)256 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
257 const struct vxlan_fdb *fdb,
258 u32 portid, u32 seq, int type, unsigned int flags,
259 const struct vxlan_rdst *rdst)
260 {
261 unsigned long now = jiffies;
262 struct nda_cacheinfo ci;
263 bool send_ip, send_eth;
264 struct nlmsghdr *nlh;
265 struct nexthop *nh;
266 struct ndmsg *ndm;
267 int nh_family;
268 u32 nh_id;
269
270 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
271 if (nlh == NULL)
272 return -EMSGSIZE;
273
274 ndm = nlmsg_data(nlh);
275 memset(ndm, 0, sizeof(*ndm));
276
277 send_eth = send_ip = true;
278
279 rcu_read_lock();
280 nh = rcu_dereference(fdb->nh);
281 if (nh) {
282 nh_family = nexthop_get_family(nh);
283 nh_id = nh->id;
284 }
285 rcu_read_unlock();
286
287 if (type == RTM_GETNEIGH) {
288 if (rdst) {
289 send_ip = !vxlan_addr_any(&rdst->remote_ip);
290 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
291 } else if (nh) {
292 ndm->ndm_family = nh_family;
293 }
294 send_eth = !is_zero_ether_addr(fdb->eth_addr);
295 } else
296 ndm->ndm_family = AF_BRIDGE;
297 ndm->ndm_state = fdb->state;
298 ndm->ndm_ifindex = vxlan->dev->ifindex;
299 ndm->ndm_flags = fdb->flags;
300 if (rdst && rdst->offloaded)
301 ndm->ndm_flags |= NTF_OFFLOADED;
302 ndm->ndm_type = RTN_UNICAST;
303
304 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
305 nla_put_s32(skb, NDA_LINK_NETNSID,
306 peernet2id(dev_net(vxlan->dev), vxlan->net)))
307 goto nla_put_failure;
308
309 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
310 goto nla_put_failure;
311 if (nh) {
312 if (nla_put_u32(skb, NDA_NH_ID, nh_id))
313 goto nla_put_failure;
314 } else if (rdst) {
315 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST,
316 &rdst->remote_ip))
317 goto nla_put_failure;
318
319 if (rdst->remote_port &&
320 rdst->remote_port != vxlan->cfg.dst_port &&
321 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
322 goto nla_put_failure;
323 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
324 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
325 goto nla_put_failure;
326 if (rdst->remote_ifindex &&
327 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
328 goto nla_put_failure;
329 }
330
331 if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
332 nla_put_u32(skb, NDA_SRC_VNI,
333 be32_to_cpu(fdb->vni)))
334 goto nla_put_failure;
335
336 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
337 ci.ndm_confirmed = 0;
338 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
339 ci.ndm_refcnt = 0;
340
341 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
342 goto nla_put_failure;
343
344 nlmsg_end(skb, nlh);
345 return 0;
346
347 nla_put_failure:
348 nlmsg_cancel(skb, nlh);
349 return -EMSGSIZE;
350 }
351
vxlan_nlmsg_size(void)352 static inline size_t vxlan_nlmsg_size(void)
353 {
354 return NLMSG_ALIGN(sizeof(struct ndmsg))
355 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
356 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
357 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
358 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
359 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
360 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
361 + nla_total_size(sizeof(struct nda_cacheinfo));
362 }
363
__vxlan_fdb_notify(struct vxlan_dev * vxlan,struct vxlan_fdb * fdb,struct vxlan_rdst * rd,int type)364 static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
365 struct vxlan_rdst *rd, int type)
366 {
367 struct net *net = dev_net(vxlan->dev);
368 struct sk_buff *skb;
369 int err = -ENOBUFS;
370
371 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
372 if (skb == NULL)
373 goto errout;
374
375 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
376 if (err < 0) {
377 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
378 WARN_ON(err == -EMSGSIZE);
379 kfree_skb(skb);
380 goto errout;
381 }
382
383 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
384 return;
385 errout:
386 if (err < 0)
387 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
388 }
389
vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev * vxlan,const struct vxlan_fdb * fdb,const struct vxlan_rdst * rd,struct netlink_ext_ack * extack,struct switchdev_notifier_vxlan_fdb_info * fdb_info)390 static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
391 const struct vxlan_fdb *fdb,
392 const struct vxlan_rdst *rd,
393 struct netlink_ext_ack *extack,
394 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
395 {
396 fdb_info->info.dev = vxlan->dev;
397 fdb_info->info.extack = extack;
398 fdb_info->remote_ip = rd->remote_ip;
399 fdb_info->remote_port = rd->remote_port;
400 fdb_info->remote_vni = rd->remote_vni;
401 fdb_info->remote_ifindex = rd->remote_ifindex;
402 memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
403 fdb_info->vni = fdb->vni;
404 fdb_info->offloaded = rd->offloaded;
405 fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
406 }
407
vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev * vxlan,struct vxlan_fdb * fdb,struct vxlan_rdst * rd,bool adding,struct netlink_ext_ack * extack)408 static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
409 struct vxlan_fdb *fdb,
410 struct vxlan_rdst *rd,
411 bool adding,
412 struct netlink_ext_ack *extack)
413 {
414 struct switchdev_notifier_vxlan_fdb_info info;
415 enum switchdev_notifier_type notifier_type;
416 int ret;
417
418 if (WARN_ON(!rd))
419 return 0;
420
421 notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
422 : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
423 vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
424 ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
425 &info.info, extack);
426 return notifier_to_errno(ret);
427 }
428
vxlan_fdb_notify(struct vxlan_dev * vxlan,struct vxlan_fdb * fdb,struct vxlan_rdst * rd,int type,bool swdev_notify,struct netlink_ext_ack * extack)429 static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
430 struct vxlan_rdst *rd, int type, bool swdev_notify,
431 struct netlink_ext_ack *extack)
432 {
433 int err;
434
435 if (swdev_notify && rd) {
436 switch (type) {
437 case RTM_NEWNEIGH:
438 err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
439 true, extack);
440 if (err)
441 return err;
442 break;
443 case RTM_DELNEIGH:
444 vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
445 false, extack);
446 break;
447 }
448 }
449
450 __vxlan_fdb_notify(vxlan, fdb, rd, type);
451 return 0;
452 }
453
vxlan_ip_miss(struct net_device * dev,union vxlan_addr * ipa)454 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
455 {
456 struct vxlan_dev *vxlan = netdev_priv(dev);
457 struct vxlan_fdb f = {
458 .state = NUD_STALE,
459 };
460 struct vxlan_rdst remote = {
461 .remote_ip = *ipa, /* goes to NDA_DST */
462 .remote_vni = cpu_to_be32(VXLAN_N_VID),
463 };
464
465 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
466 }
467
vxlan_fdb_miss(struct vxlan_dev * vxlan,const u8 eth_addr[ETH_ALEN])468 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
469 {
470 struct vxlan_fdb f = {
471 .state = NUD_STALE,
472 };
473 struct vxlan_rdst remote = { };
474
475 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
476
477 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
478 }
479
480 /* Hash Ethernet address */
eth_hash(const unsigned char * addr)481 static u32 eth_hash(const unsigned char *addr)
482 {
483 u64 value = get_unaligned((u64 *)addr);
484
485 /* only want 6 bytes */
486 #ifdef __BIG_ENDIAN
487 value >>= 16;
488 #else
489 value <<= 16;
490 #endif
491 return hash_64(value, FDB_HASH_BITS);
492 }
493
eth_vni_hash(const unsigned char * addr,__be32 vni)494 static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
495 {
496 /* use 1 byte of OUI and 3 bytes of NIC */
497 u32 key = get_unaligned((u32 *)(addr + 2));
498
499 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
500 }
501
fdb_head_index(struct vxlan_dev * vxlan,const u8 * mac,__be32 vni)502 static u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
503 {
504 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
505 return eth_vni_hash(mac, vni);
506 else
507 return eth_hash(mac);
508 }
509
510 /* Hash chain to use given mac address */
vxlan_fdb_head(struct vxlan_dev * vxlan,const u8 * mac,__be32 vni)511 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
512 const u8 *mac, __be32 vni)
513 {
514 return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
515 }
516
517 /* Look up Ethernet address in forwarding table */
__vxlan_find_mac(struct vxlan_dev * vxlan,const u8 * mac,__be32 vni)518 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
519 const u8 *mac, __be32 vni)
520 {
521 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
522 struct vxlan_fdb *f;
523
524 hlist_for_each_entry_rcu(f, head, hlist) {
525 if (ether_addr_equal(mac, f->eth_addr)) {
526 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
527 if (vni == f->vni)
528 return f;
529 } else {
530 return f;
531 }
532 }
533 }
534
535 return NULL;
536 }
537
vxlan_find_mac(struct vxlan_dev * vxlan,const u8 * mac,__be32 vni)538 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
539 const u8 *mac, __be32 vni)
540 {
541 struct vxlan_fdb *f;
542
543 f = __vxlan_find_mac(vxlan, mac, vni);
544 if (f && f->used != jiffies)
545 f->used = jiffies;
546
547 return f;
548 }
549
550 /* caller should hold vxlan->hash_lock */
vxlan_fdb_find_rdst(struct vxlan_fdb * f,union vxlan_addr * ip,__be16 port,__be32 vni,__u32 ifindex)551 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
552 union vxlan_addr *ip, __be16 port,
553 __be32 vni, __u32 ifindex)
554 {
555 struct vxlan_rdst *rd;
556
557 list_for_each_entry(rd, &f->remotes, list) {
558 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
559 rd->remote_port == port &&
560 rd->remote_vni == vni &&
561 rd->remote_ifindex == ifindex)
562 return rd;
563 }
564
565 return NULL;
566 }
567
vxlan_fdb_find_uc(struct net_device * dev,const u8 * mac,__be32 vni,struct switchdev_notifier_vxlan_fdb_info * fdb_info)568 int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
569 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
570 {
571 struct vxlan_dev *vxlan = netdev_priv(dev);
572 u8 eth_addr[ETH_ALEN + 2] = { 0 };
573 struct vxlan_rdst *rdst;
574 struct vxlan_fdb *f;
575 int rc = 0;
576
577 if (is_multicast_ether_addr(mac) ||
578 is_zero_ether_addr(mac))
579 return -EINVAL;
580
581 ether_addr_copy(eth_addr, mac);
582
583 rcu_read_lock();
584
585 f = __vxlan_find_mac(vxlan, eth_addr, vni);
586 if (!f) {
587 rc = -ENOENT;
588 goto out;
589 }
590
591 rdst = first_remote_rcu(f);
592 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
593
594 out:
595 rcu_read_unlock();
596 return rc;
597 }
598 EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
599
vxlan_fdb_notify_one(struct notifier_block * nb,const struct vxlan_dev * vxlan,const struct vxlan_fdb * f,const struct vxlan_rdst * rdst,struct netlink_ext_ack * extack)600 static int vxlan_fdb_notify_one(struct notifier_block *nb,
601 const struct vxlan_dev *vxlan,
602 const struct vxlan_fdb *f,
603 const struct vxlan_rdst *rdst,
604 struct netlink_ext_ack *extack)
605 {
606 struct switchdev_notifier_vxlan_fdb_info fdb_info;
607 int rc;
608
609 vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
610 rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
611 &fdb_info);
612 return notifier_to_errno(rc);
613 }
614
vxlan_fdb_replay(const struct net_device * dev,__be32 vni,struct notifier_block * nb,struct netlink_ext_ack * extack)615 int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
616 struct notifier_block *nb,
617 struct netlink_ext_ack *extack)
618 {
619 struct vxlan_dev *vxlan;
620 struct vxlan_rdst *rdst;
621 struct vxlan_fdb *f;
622 unsigned int h;
623 int rc = 0;
624
625 if (!netif_is_vxlan(dev))
626 return -EINVAL;
627 vxlan = netdev_priv(dev);
628
629 for (h = 0; h < FDB_HASH_SIZE; ++h) {
630 spin_lock_bh(&vxlan->hash_lock[h]);
631 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
632 if (f->vni == vni) {
633 list_for_each_entry(rdst, &f->remotes, list) {
634 rc = vxlan_fdb_notify_one(nb, vxlan,
635 f, rdst,
636 extack);
637 if (rc)
638 goto unlock;
639 }
640 }
641 }
642 spin_unlock_bh(&vxlan->hash_lock[h]);
643 }
644 return 0;
645
646 unlock:
647 spin_unlock_bh(&vxlan->hash_lock[h]);
648 return rc;
649 }
650 EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
651
vxlan_fdb_clear_offload(const struct net_device * dev,__be32 vni)652 void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
653 {
654 struct vxlan_dev *vxlan;
655 struct vxlan_rdst *rdst;
656 struct vxlan_fdb *f;
657 unsigned int h;
658
659 if (!netif_is_vxlan(dev))
660 return;
661 vxlan = netdev_priv(dev);
662
663 for (h = 0; h < FDB_HASH_SIZE; ++h) {
664 spin_lock_bh(&vxlan->hash_lock[h]);
665 hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
666 if (f->vni == vni)
667 list_for_each_entry(rdst, &f->remotes, list)
668 rdst->offloaded = false;
669 spin_unlock_bh(&vxlan->hash_lock[h]);
670 }
671
672 }
673 EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
674
675 /* Replace destination of unicast mac */
vxlan_fdb_replace(struct vxlan_fdb * f,union vxlan_addr * ip,__be16 port,__be32 vni,__u32 ifindex,struct vxlan_rdst * oldrd)676 static int vxlan_fdb_replace(struct vxlan_fdb *f,
677 union vxlan_addr *ip, __be16 port, __be32 vni,
678 __u32 ifindex, struct vxlan_rdst *oldrd)
679 {
680 struct vxlan_rdst *rd;
681
682 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
683 if (rd)
684 return 0;
685
686 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
687 if (!rd)
688 return 0;
689
690 *oldrd = *rd;
691 dst_cache_reset(&rd->dst_cache);
692 rd->remote_ip = *ip;
693 rd->remote_port = port;
694 rd->remote_vni = vni;
695 rd->remote_ifindex = ifindex;
696 rd->offloaded = false;
697 return 1;
698 }
699
700 /* Add/update destinations for multicast */
vxlan_fdb_append(struct vxlan_fdb * f,union vxlan_addr * ip,__be16 port,__be32 vni,__u32 ifindex,struct vxlan_rdst ** rdp)701 static int vxlan_fdb_append(struct vxlan_fdb *f,
702 union vxlan_addr *ip, __be16 port, __be32 vni,
703 __u32 ifindex, struct vxlan_rdst **rdp)
704 {
705 struct vxlan_rdst *rd;
706
707 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
708 if (rd)
709 return 0;
710
711 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
712 if (rd == NULL)
713 return -ENOMEM;
714
715 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
716 kfree(rd);
717 return -ENOMEM;
718 }
719
720 rd->remote_ip = *ip;
721 rd->remote_port = port;
722 rd->offloaded = false;
723 rd->remote_vni = vni;
724 rd->remote_ifindex = ifindex;
725
726 list_add_tail_rcu(&rd->list, &f->remotes);
727
728 *rdp = rd;
729 return 1;
730 }
731
vxlan_gro_remcsum(struct sk_buff * skb,unsigned int off,struct vxlanhdr * vh,size_t hdrlen,__be32 vni_field,struct gro_remcsum * grc,bool nopartial)732 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
733 unsigned int off,
734 struct vxlanhdr *vh, size_t hdrlen,
735 __be32 vni_field,
736 struct gro_remcsum *grc,
737 bool nopartial)
738 {
739 size_t start, offset;
740
741 if (skb->remcsum_offload)
742 return vh;
743
744 if (!NAPI_GRO_CB(skb)->csum_valid)
745 return NULL;
746
747 start = vxlan_rco_start(vni_field);
748 offset = start + vxlan_rco_offset(vni_field);
749
750 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
751 start, offset, grc, nopartial);
752
753 skb->remcsum_offload = 1;
754
755 return vh;
756 }
757
vxlan_gro_receive(struct sock * sk,struct list_head * head,struct sk_buff * skb)758 static struct sk_buff *vxlan_gro_receive(struct sock *sk,
759 struct list_head *head,
760 struct sk_buff *skb)
761 {
762 struct sk_buff *pp = NULL;
763 struct sk_buff *p;
764 struct vxlanhdr *vh, *vh2;
765 unsigned int hlen, off_vx;
766 int flush = 1;
767 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
768 __be32 flags;
769 struct gro_remcsum grc;
770
771 skb_gro_remcsum_init(&grc);
772
773 off_vx = skb_gro_offset(skb);
774 hlen = off_vx + sizeof(*vh);
775 vh = skb_gro_header_fast(skb, off_vx);
776 if (skb_gro_header_hard(skb, hlen)) {
777 vh = skb_gro_header_slow(skb, hlen, off_vx);
778 if (unlikely(!vh))
779 goto out;
780 }
781
782 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
783
784 flags = vh->vx_flags;
785
786 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
787 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
788 vh->vx_vni, &grc,
789 !!(vs->flags &
790 VXLAN_F_REMCSUM_NOPARTIAL));
791
792 if (!vh)
793 goto out;
794 }
795
796 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
797
798 list_for_each_entry(p, head, list) {
799 if (!NAPI_GRO_CB(p)->same_flow)
800 continue;
801
802 vh2 = (struct vxlanhdr *)(p->data + off_vx);
803 if (vh->vx_flags != vh2->vx_flags ||
804 vh->vx_vni != vh2->vx_vni) {
805 NAPI_GRO_CB(p)->same_flow = 0;
806 continue;
807 }
808 }
809
810 pp = call_gro_receive(eth_gro_receive, head, skb);
811 flush = 0;
812
813 out:
814 skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
815
816 return pp;
817 }
818
vxlan_gro_complete(struct sock * sk,struct sk_buff * skb,int nhoff)819 static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
820 {
821 /* Sets 'skb->inner_mac_header' since we are always called with
822 * 'skb->encapsulation' set.
823 */
824 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
825 }
826
vxlan_fdb_alloc(struct vxlan_dev * vxlan,const u8 * mac,__u16 state,__be32 src_vni,__u16 ndm_flags)827 static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, const u8 *mac,
828 __u16 state, __be32 src_vni,
829 __u16 ndm_flags)
830 {
831 struct vxlan_fdb *f;
832
833 f = kmalloc(sizeof(*f), GFP_ATOMIC);
834 if (!f)
835 return NULL;
836 f->state = state;
837 f->flags = ndm_flags;
838 f->updated = f->used = jiffies;
839 f->vni = src_vni;
840 f->nh = NULL;
841 RCU_INIT_POINTER(f->vdev, vxlan);
842 INIT_LIST_HEAD(&f->nh_list);
843 INIT_LIST_HEAD(&f->remotes);
844 memcpy(f->eth_addr, mac, ETH_ALEN);
845
846 return f;
847 }
848
vxlan_fdb_insert(struct vxlan_dev * vxlan,const u8 * mac,__be32 src_vni,struct vxlan_fdb * f)849 static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
850 __be32 src_vni, struct vxlan_fdb *f)
851 {
852 ++vxlan->addrcnt;
853 hlist_add_head_rcu(&f->hlist,
854 vxlan_fdb_head(vxlan, mac, src_vni));
855 }
856
vxlan_fdb_nh_update(struct vxlan_dev * vxlan,struct vxlan_fdb * fdb,u32 nhid,struct netlink_ext_ack * extack)857 static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
858 u32 nhid, struct netlink_ext_ack *extack)
859 {
860 struct nexthop *old_nh = rtnl_dereference(fdb->nh);
861 struct nexthop *nh;
862 int err = -EINVAL;
863
864 if (old_nh && old_nh->id == nhid)
865 return 0;
866
867 nh = nexthop_find_by_id(vxlan->net, nhid);
868 if (!nh) {
869 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
870 goto err_inval;
871 }
872
873 if (nh) {
874 if (!nexthop_get(nh)) {
875 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
876 nh = NULL;
877 goto err_inval;
878 }
879 if (!nexthop_is_fdb(nh)) {
880 NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop");
881 goto err_inval;
882 }
883
884 if (!nexthop_is_multipath(nh)) {
885 NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group");
886 goto err_inval;
887 }
888
889 /* check nexthop group family */
890 switch (vxlan->default_dst.remote_ip.sa.sa_family) {
891 case AF_INET:
892 if (!nexthop_has_v4(nh)) {
893 err = -EAFNOSUPPORT;
894 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
895 goto err_inval;
896 }
897 break;
898 case AF_INET6:
899 if (nexthop_has_v4(nh)) {
900 err = -EAFNOSUPPORT;
901 NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
902 goto err_inval;
903 }
904 }
905 }
906
907 if (old_nh) {
908 list_del_rcu(&fdb->nh_list);
909 nexthop_put(old_nh);
910 }
911 rcu_assign_pointer(fdb->nh, nh);
912 list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list);
913 return 1;
914
915 err_inval:
916 if (nh)
917 nexthop_put(nh);
918 return err;
919 }
920
vxlan_fdb_create(struct vxlan_dev * vxlan,const u8 * mac,union vxlan_addr * ip,__u16 state,__be16 port,__be32 src_vni,__be32 vni,__u32 ifindex,__u16 ndm_flags,u32 nhid,struct vxlan_fdb ** fdb,struct netlink_ext_ack * extack)921 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
922 const u8 *mac, union vxlan_addr *ip,
923 __u16 state, __be16 port, __be32 src_vni,
924 __be32 vni, __u32 ifindex, __u16 ndm_flags,
925 u32 nhid, struct vxlan_fdb **fdb,
926 struct netlink_ext_ack *extack)
927 {
928 struct vxlan_rdst *rd = NULL;
929 struct vxlan_fdb *f;
930 int rc;
931
932 if (vxlan->cfg.addrmax &&
933 vxlan->addrcnt >= vxlan->cfg.addrmax)
934 return -ENOSPC;
935
936 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
937 f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
938 if (!f)
939 return -ENOMEM;
940
941 if (nhid)
942 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
943 else
944 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
945 if (rc < 0)
946 goto errout;
947
948 *fdb = f;
949
950 return 0;
951
952 errout:
953 kfree(f);
954 return rc;
955 }
956
__vxlan_fdb_free(struct vxlan_fdb * f)957 static void __vxlan_fdb_free(struct vxlan_fdb *f)
958 {
959 struct vxlan_rdst *rd, *nd;
960 struct nexthop *nh;
961
962 nh = rcu_dereference_raw(f->nh);
963 if (nh) {
964 rcu_assign_pointer(f->nh, NULL);
965 rcu_assign_pointer(f->vdev, NULL);
966 nexthop_put(nh);
967 }
968
969 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
970 dst_cache_destroy(&rd->dst_cache);
971 kfree(rd);
972 }
973 kfree(f);
974 }
975
vxlan_fdb_free(struct rcu_head * head)976 static void vxlan_fdb_free(struct rcu_head *head)
977 {
978 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
979
980 __vxlan_fdb_free(f);
981 }
982
vxlan_fdb_destroy(struct vxlan_dev * vxlan,struct vxlan_fdb * f,bool do_notify,bool swdev_notify)983 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
984 bool do_notify, bool swdev_notify)
985 {
986 struct vxlan_rdst *rd;
987
988 netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
989
990 --vxlan->addrcnt;
991 if (do_notify) {
992 if (rcu_access_pointer(f->nh))
993 vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH,
994 swdev_notify, NULL);
995 else
996 list_for_each_entry(rd, &f->remotes, list)
997 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
998 swdev_notify, NULL);
999 }
1000
1001 hlist_del_rcu(&f->hlist);
1002 list_del_rcu(&f->nh_list);
1003 call_rcu(&f->rcu, vxlan_fdb_free);
1004 }
1005
vxlan_dst_free(struct rcu_head * head)1006 static void vxlan_dst_free(struct rcu_head *head)
1007 {
1008 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
1009
1010 dst_cache_destroy(&rd->dst_cache);
1011 kfree(rd);
1012 }
1013
vxlan_fdb_update_existing(struct vxlan_dev * vxlan,union vxlan_addr * ip,__u16 state,__u16 flags,__be16 port,__be32 vni,__u32 ifindex,__u16 ndm_flags,struct vxlan_fdb * f,u32 nhid,bool swdev_notify,struct netlink_ext_ack * extack)1014 static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
1015 union vxlan_addr *ip,
1016 __u16 state, __u16 flags,
1017 __be16 port, __be32 vni,
1018 __u32 ifindex, __u16 ndm_flags,
1019 struct vxlan_fdb *f, u32 nhid,
1020 bool swdev_notify,
1021 struct netlink_ext_ack *extack)
1022 {
1023 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1024 struct vxlan_rdst *rd = NULL;
1025 struct vxlan_rdst oldrd;
1026 int notify = 0;
1027 int rc = 0;
1028 int err;
1029
1030 if (nhid && !rcu_access_pointer(f->nh)) {
1031 NL_SET_ERR_MSG(extack,
1032 "Cannot replace an existing non nexthop fdb with a nexthop");
1033 return -EOPNOTSUPP;
1034 }
1035
1036 if (nhid && (flags & NLM_F_APPEND)) {
1037 NL_SET_ERR_MSG(extack,
1038 "Cannot append to a nexthop fdb");
1039 return -EOPNOTSUPP;
1040 }
1041
1042 /* Do not allow an externally learned entry to take over an entry added
1043 * by the user.
1044 */
1045 if (!(fdb_flags & NTF_EXT_LEARNED) ||
1046 !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
1047 if (f->state != state) {
1048 f->state = state;
1049 f->updated = jiffies;
1050 notify = 1;
1051 }
1052 if (f->flags != fdb_flags) {
1053 f->flags = fdb_flags;
1054 f->updated = jiffies;
1055 notify = 1;
1056 }
1057 }
1058
1059 if ((flags & NLM_F_REPLACE)) {
1060 /* Only change unicasts */
1061 if (!(is_multicast_ether_addr(f->eth_addr) ||
1062 is_zero_ether_addr(f->eth_addr))) {
1063 if (nhid) {
1064 rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
1065 if (rc < 0)
1066 return rc;
1067 } else {
1068 rc = vxlan_fdb_replace(f, ip, port, vni,
1069 ifindex, &oldrd);
1070 }
1071 notify |= rc;
1072 } else {
1073 NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries");
1074 return -EOPNOTSUPP;
1075 }
1076 }
1077 if ((flags & NLM_F_APPEND) &&
1078 (is_multicast_ether_addr(f->eth_addr) ||
1079 is_zero_ether_addr(f->eth_addr))) {
1080 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
1081
1082 if (rc < 0)
1083 return rc;
1084 notify |= rc;
1085 }
1086
1087 if (ndm_flags & NTF_USE)
1088 f->used = jiffies;
1089
1090 if (notify) {
1091 if (rd == NULL)
1092 rd = first_remote_rtnl(f);
1093
1094 err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
1095 swdev_notify, extack);
1096 if (err)
1097 goto err_notify;
1098 }
1099
1100 return 0;
1101
1102 err_notify:
1103 if (nhid)
1104 return err;
1105 if ((flags & NLM_F_REPLACE) && rc)
1106 *rd = oldrd;
1107 else if ((flags & NLM_F_APPEND) && rc) {
1108 list_del_rcu(&rd->list);
1109 call_rcu(&rd->rcu, vxlan_dst_free);
1110 }
1111 return err;
1112 }
1113
vxlan_fdb_update_create(struct vxlan_dev * vxlan,const u8 * mac,union vxlan_addr * ip,__u16 state,__u16 flags,__be16 port,__be32 src_vni,__be32 vni,__u32 ifindex,__u16 ndm_flags,u32 nhid,bool swdev_notify,struct netlink_ext_ack * extack)1114 static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
1115 const u8 *mac, union vxlan_addr *ip,
1116 __u16 state, __u16 flags,
1117 __be16 port, __be32 src_vni, __be32 vni,
1118 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1119 bool swdev_notify,
1120 struct netlink_ext_ack *extack)
1121 {
1122 __u16 fdb_flags = (ndm_flags & ~NTF_USE);
1123 struct vxlan_fdb *f;
1124 int rc;
1125
1126 /* Disallow replace to add a multicast entry */
1127 if ((flags & NLM_F_REPLACE) &&
1128 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
1129 return -EOPNOTSUPP;
1130
1131 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
1132 rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
1133 vni, ifindex, fdb_flags, nhid, &f, extack);
1134 if (rc < 0)
1135 return rc;
1136
1137 vxlan_fdb_insert(vxlan, mac, src_vni, f);
1138 rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
1139 swdev_notify, extack);
1140 if (rc)
1141 goto err_notify;
1142
1143 return 0;
1144
1145 err_notify:
1146 vxlan_fdb_destroy(vxlan, f, false, false);
1147 return rc;
1148 }
1149
1150 /* Add new entry to forwarding table -- assumes lock held */
vxlan_fdb_update(struct vxlan_dev * vxlan,const u8 * mac,union vxlan_addr * ip,__u16 state,__u16 flags,__be16 port,__be32 src_vni,__be32 vni,__u32 ifindex,__u16 ndm_flags,u32 nhid,bool swdev_notify,struct netlink_ext_ack * extack)1151 static int vxlan_fdb_update(struct vxlan_dev *vxlan,
1152 const u8 *mac, union vxlan_addr *ip,
1153 __u16 state, __u16 flags,
1154 __be16 port, __be32 src_vni, __be32 vni,
1155 __u32 ifindex, __u16 ndm_flags, u32 nhid,
1156 bool swdev_notify,
1157 struct netlink_ext_ack *extack)
1158 {
1159 struct vxlan_fdb *f;
1160
1161 f = __vxlan_find_mac(vxlan, mac, src_vni);
1162 if (f) {
1163 if (flags & NLM_F_EXCL) {
1164 netdev_dbg(vxlan->dev,
1165 "lost race to create %pM\n", mac);
1166 return -EEXIST;
1167 }
1168
1169 return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
1170 vni, ifindex, ndm_flags, f,
1171 nhid, swdev_notify, extack);
1172 } else {
1173 if (!(flags & NLM_F_CREATE))
1174 return -ENOENT;
1175
1176 return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
1177 port, src_vni, vni, ifindex,
1178 ndm_flags, nhid, swdev_notify,
1179 extack);
1180 }
1181 }
1182
vxlan_fdb_dst_destroy(struct vxlan_dev * vxlan,struct vxlan_fdb * f,struct vxlan_rdst * rd,bool swdev_notify)1183 static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
1184 struct vxlan_rdst *rd, bool swdev_notify)
1185 {
1186 list_del_rcu(&rd->list);
1187 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
1188 call_rcu(&rd->rcu, vxlan_dst_free);
1189 }
1190
vxlan_fdb_parse(struct nlattr * tb[],struct vxlan_dev * vxlan,union vxlan_addr * ip,__be16 * port,__be32 * src_vni,__be32 * vni,u32 * ifindex,u32 * nhid)1191 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
1192 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
1193 __be32 *vni, u32 *ifindex, u32 *nhid)
1194 {
1195 struct net *net = dev_net(vxlan->dev);
1196 int err;
1197
1198 if (tb[NDA_NH_ID] && (tb[NDA_DST] || tb[NDA_VNI] || tb[NDA_IFINDEX] ||
1199 tb[NDA_PORT]))
1200 return -EINVAL;
1201
1202 if (tb[NDA_DST]) {
1203 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
1204 if (err)
1205 return err;
1206 } else {
1207 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
1208
1209 if (remote->sa.sa_family == AF_INET) {
1210 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
1211 ip->sa.sa_family = AF_INET;
1212 #if IS_ENABLED(CONFIG_IPV6)
1213 } else {
1214 ip->sin6.sin6_addr = in6addr_any;
1215 ip->sa.sa_family = AF_INET6;
1216 #endif
1217 }
1218 }
1219
1220 if (tb[NDA_PORT]) {
1221 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
1222 return -EINVAL;
1223 *port = nla_get_be16(tb[NDA_PORT]);
1224 } else {
1225 *port = vxlan->cfg.dst_port;
1226 }
1227
1228 if (tb[NDA_VNI]) {
1229 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
1230 return -EINVAL;
1231 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1232 } else {
1233 *vni = vxlan->default_dst.remote_vni;
1234 }
1235
1236 if (tb[NDA_SRC_VNI]) {
1237 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
1238 return -EINVAL;
1239 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
1240 } else {
1241 *src_vni = vxlan->default_dst.remote_vni;
1242 }
1243
1244 if (tb[NDA_IFINDEX]) {
1245 struct net_device *tdev;
1246
1247 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
1248 return -EINVAL;
1249 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
1250 tdev = __dev_get_by_index(net, *ifindex);
1251 if (!tdev)
1252 return -EADDRNOTAVAIL;
1253 } else {
1254 *ifindex = 0;
1255 }
1256
1257 if (tb[NDA_NH_ID])
1258 *nhid = nla_get_u32(tb[NDA_NH_ID]);
1259 else
1260 *nhid = 0;
1261
1262 return 0;
1263 }
1264
1265 /* Add static entry (via netlink) */
vxlan_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,struct netlink_ext_ack * extack)1266 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1267 struct net_device *dev,
1268 const unsigned char *addr, u16 vid, u16 flags,
1269 struct netlink_ext_ack *extack)
1270 {
1271 struct vxlan_dev *vxlan = netdev_priv(dev);
1272 /* struct net *net = dev_net(vxlan->dev); */
1273 union vxlan_addr ip;
1274 __be16 port;
1275 __be32 src_vni, vni;
1276 u32 ifindex, nhid;
1277 u32 hash_index;
1278 int err;
1279
1280 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
1281 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
1282 ndm->ndm_state);
1283 return -EINVAL;
1284 }
1285
1286 if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID]))
1287 return -EINVAL;
1288
1289 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1290 &nhid);
1291 if (err)
1292 return err;
1293
1294 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
1295 return -EAFNOSUPPORT;
1296
1297 hash_index = fdb_head_index(vxlan, addr, src_vni);
1298 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1299 err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
1300 port, src_vni, vni, ifindex,
1301 ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
1302 nhid, true, extack);
1303 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1304
1305 return err;
1306 }
1307
__vxlan_fdb_delete(struct vxlan_dev * vxlan,const unsigned char * addr,union vxlan_addr ip,__be16 port,__be32 src_vni,__be32 vni,u32 ifindex,bool swdev_notify)1308 static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
1309 const unsigned char *addr, union vxlan_addr ip,
1310 __be16 port, __be32 src_vni, __be32 vni,
1311 u32 ifindex, bool swdev_notify)
1312 {
1313 struct vxlan_rdst *rd = NULL;
1314 struct vxlan_fdb *f;
1315 int err = -ENOENT;
1316
1317 f = vxlan_find_mac(vxlan, addr, src_vni);
1318 if (!f)
1319 return err;
1320
1321 if (!vxlan_addr_any(&ip)) {
1322 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
1323 if (!rd)
1324 goto out;
1325 }
1326
1327 /* remove a destination if it's not the only one on the list,
1328 * otherwise destroy the fdb entry
1329 */
1330 if (rd && !list_is_singular(&f->remotes)) {
1331 vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
1332 goto out;
1333 }
1334
1335 vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
1336
1337 out:
1338 return 0;
1339 }
1340
1341 /* Delete entry (via netlink) */
vxlan_fdb_delete(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid)1342 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
1343 struct net_device *dev,
1344 const unsigned char *addr, u16 vid)
1345 {
1346 struct vxlan_dev *vxlan = netdev_priv(dev);
1347 union vxlan_addr ip;
1348 __be32 src_vni, vni;
1349 u32 ifindex, nhid;
1350 u32 hash_index;
1351 __be16 port;
1352 int err;
1353
1354 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
1355 &nhid);
1356 if (err)
1357 return err;
1358
1359 hash_index = fdb_head_index(vxlan, addr, src_vni);
1360 spin_lock_bh(&vxlan->hash_lock[hash_index]);
1361 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
1362 true);
1363 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
1364
1365 return err;
1366 }
1367
1368 /* Dump forwarding table */
vxlan_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,struct net_device * filter_dev,int * idx)1369 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
1370 struct net_device *dev,
1371 struct net_device *filter_dev, int *idx)
1372 {
1373 struct vxlan_dev *vxlan = netdev_priv(dev);
1374 unsigned int h;
1375 int err = 0;
1376
1377 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1378 struct vxlan_fdb *f;
1379
1380 rcu_read_lock();
1381 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
1382 struct vxlan_rdst *rd;
1383
1384 if (rcu_access_pointer(f->nh)) {
1385 if (*idx < cb->args[2])
1386 goto skip_nh;
1387 err = vxlan_fdb_info(skb, vxlan, f,
1388 NETLINK_CB(cb->skb).portid,
1389 cb->nlh->nlmsg_seq,
1390 RTM_NEWNEIGH,
1391 NLM_F_MULTI, NULL);
1392 if (err < 0) {
1393 rcu_read_unlock();
1394 goto out;
1395 }
1396 skip_nh:
1397 *idx += 1;
1398 continue;
1399 }
1400
1401 list_for_each_entry_rcu(rd, &f->remotes, list) {
1402 if (*idx < cb->args[2])
1403 goto skip;
1404
1405 err = vxlan_fdb_info(skb, vxlan, f,
1406 NETLINK_CB(cb->skb).portid,
1407 cb->nlh->nlmsg_seq,
1408 RTM_NEWNEIGH,
1409 NLM_F_MULTI, rd);
1410 if (err < 0) {
1411 rcu_read_unlock();
1412 goto out;
1413 }
1414 skip:
1415 *idx += 1;
1416 }
1417 }
1418 rcu_read_unlock();
1419 }
1420 out:
1421 return err;
1422 }
1423
vxlan_fdb_get(struct sk_buff * skb,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u32 portid,u32 seq,struct netlink_ext_ack * extack)1424 static int vxlan_fdb_get(struct sk_buff *skb,
1425 struct nlattr *tb[],
1426 struct net_device *dev,
1427 const unsigned char *addr,
1428 u16 vid, u32 portid, u32 seq,
1429 struct netlink_ext_ack *extack)
1430 {
1431 struct vxlan_dev *vxlan = netdev_priv(dev);
1432 struct vxlan_fdb *f;
1433 __be32 vni;
1434 int err;
1435
1436 if (tb[NDA_VNI])
1437 vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
1438 else
1439 vni = vxlan->default_dst.remote_vni;
1440
1441 rcu_read_lock();
1442
1443 f = __vxlan_find_mac(vxlan, addr, vni);
1444 if (!f) {
1445 NL_SET_ERR_MSG(extack, "Fdb entry not found");
1446 err = -ENOENT;
1447 goto errout;
1448 }
1449
1450 err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
1451 RTM_NEWNEIGH, 0, first_remote_rcu(f));
1452 errout:
1453 rcu_read_unlock();
1454 return err;
1455 }
1456
1457 /* Watch incoming packets to learn mapping between Ethernet address
1458 * and Tunnel endpoint.
1459 * Return true if packet is bogus and should be dropped.
1460 */
vxlan_snoop(struct net_device * dev,union vxlan_addr * src_ip,const u8 * src_mac,u32 src_ifindex,__be32 vni)1461 static bool vxlan_snoop(struct net_device *dev,
1462 union vxlan_addr *src_ip, const u8 *src_mac,
1463 u32 src_ifindex, __be32 vni)
1464 {
1465 struct vxlan_dev *vxlan = netdev_priv(dev);
1466 struct vxlan_fdb *f;
1467 u32 ifindex = 0;
1468
1469 #if IS_ENABLED(CONFIG_IPV6)
1470 if (src_ip->sa.sa_family == AF_INET6 &&
1471 (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
1472 ifindex = src_ifindex;
1473 #endif
1474
1475 f = vxlan_find_mac(vxlan, src_mac, vni);
1476 if (likely(f)) {
1477 struct vxlan_rdst *rdst = first_remote_rcu(f);
1478
1479 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
1480 rdst->remote_ifindex == ifindex))
1481 return false;
1482
1483 /* Don't migrate static entries, drop packets */
1484 if (f->state & (NUD_PERMANENT | NUD_NOARP))
1485 return true;
1486
1487 /* Don't override an fdb with nexthop with a learnt entry */
1488 if (rcu_access_pointer(f->nh))
1489 return true;
1490
1491 if (net_ratelimit())
1492 netdev_info(dev,
1493 "%pM migrated from %pIS to %pIS\n",
1494 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
1495
1496 rdst->remote_ip = *src_ip;
1497 f->updated = jiffies;
1498 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
1499 } else {
1500 u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
1501
1502 /* learned new entry */
1503 spin_lock(&vxlan->hash_lock[hash_index]);
1504
1505 /* close off race between vxlan_flush and incoming packets */
1506 if (netif_running(dev))
1507 vxlan_fdb_update(vxlan, src_mac, src_ip,
1508 NUD_REACHABLE,
1509 NLM_F_EXCL|NLM_F_CREATE,
1510 vxlan->cfg.dst_port,
1511 vni,
1512 vxlan->default_dst.remote_vni,
1513 ifindex, NTF_SELF, 0, true, NULL);
1514 spin_unlock(&vxlan->hash_lock[hash_index]);
1515 }
1516
1517 return false;
1518 }
1519
1520 /* See if multicast group is already in use by other ID */
vxlan_group_used(struct vxlan_net * vn,struct vxlan_dev * dev)1521 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1522 {
1523 struct vxlan_dev *vxlan;
1524 struct vxlan_sock *sock4;
1525 #if IS_ENABLED(CONFIG_IPV6)
1526 struct vxlan_sock *sock6;
1527 #endif
1528 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
1529
1530 sock4 = rtnl_dereference(dev->vn4_sock);
1531
1532 /* The vxlan_sock is only used by dev, leaving group has
1533 * no effect on other vxlan devices.
1534 */
1535 if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
1536 return false;
1537 #if IS_ENABLED(CONFIG_IPV6)
1538 sock6 = rtnl_dereference(dev->vn6_sock);
1539 if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
1540 return false;
1541 #endif
1542
1543 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1544 if (!netif_running(vxlan->dev) || vxlan == dev)
1545 continue;
1546
1547 if (family == AF_INET &&
1548 rtnl_dereference(vxlan->vn4_sock) != sock4)
1549 continue;
1550 #if IS_ENABLED(CONFIG_IPV6)
1551 if (family == AF_INET6 &&
1552 rtnl_dereference(vxlan->vn6_sock) != sock6)
1553 continue;
1554 #endif
1555
1556 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1557 &dev->default_dst.remote_ip))
1558 continue;
1559
1560 if (vxlan->default_dst.remote_ifindex !=
1561 dev->default_dst.remote_ifindex)
1562 continue;
1563
1564 return true;
1565 }
1566
1567 return false;
1568 }
1569
__vxlan_sock_release_prep(struct vxlan_sock * vs)1570 static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
1571 {
1572 struct vxlan_net *vn;
1573
1574 if (!vs)
1575 return false;
1576 if (!refcount_dec_and_test(&vs->refcnt))
1577 return false;
1578
1579 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1580 spin_lock(&vn->sock_lock);
1581 hlist_del_rcu(&vs->hlist);
1582 udp_tunnel_notify_del_rx_port(vs->sock,
1583 (vs->flags & VXLAN_F_GPE) ?
1584 UDP_TUNNEL_TYPE_VXLAN_GPE :
1585 UDP_TUNNEL_TYPE_VXLAN);
1586 spin_unlock(&vn->sock_lock);
1587
1588 return true;
1589 }
1590
vxlan_sock_release(struct vxlan_dev * vxlan)1591 static void vxlan_sock_release(struct vxlan_dev *vxlan)
1592 {
1593 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1594 #if IS_ENABLED(CONFIG_IPV6)
1595 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1596
1597 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
1598 #endif
1599
1600 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
1601 synchronize_net();
1602
1603 vxlan_vs_del_dev(vxlan);
1604
1605 if (__vxlan_sock_release_prep(sock4)) {
1606 udp_tunnel_sock_release(sock4->sock);
1607 kfree(sock4);
1608 }
1609
1610 #if IS_ENABLED(CONFIG_IPV6)
1611 if (__vxlan_sock_release_prep(sock6)) {
1612 udp_tunnel_sock_release(sock6->sock);
1613 kfree(sock6);
1614 }
1615 #endif
1616 }
1617
1618 /* Update multicast group membership when first VNI on
1619 * multicast address is brought up
1620 */
vxlan_igmp_join(struct vxlan_dev * vxlan)1621 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1622 {
1623 struct sock *sk;
1624 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1625 int ifindex = vxlan->default_dst.remote_ifindex;
1626 int ret = -EINVAL;
1627
1628 if (ip->sa.sa_family == AF_INET) {
1629 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1630 struct ip_mreqn mreq = {
1631 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1632 .imr_ifindex = ifindex,
1633 };
1634
1635 sk = sock4->sock->sk;
1636 lock_sock(sk);
1637 ret = ip_mc_join_group(sk, &mreq);
1638 release_sock(sk);
1639 #if IS_ENABLED(CONFIG_IPV6)
1640 } else {
1641 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1642
1643 sk = sock6->sock->sk;
1644 lock_sock(sk);
1645 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1646 &ip->sin6.sin6_addr);
1647 release_sock(sk);
1648 #endif
1649 }
1650
1651 return ret;
1652 }
1653
1654 /* Inverse of vxlan_igmp_join when last VNI is brought down */
vxlan_igmp_leave(struct vxlan_dev * vxlan)1655 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1656 {
1657 struct sock *sk;
1658 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1659 int ifindex = vxlan->default_dst.remote_ifindex;
1660 int ret = -EINVAL;
1661
1662 if (ip->sa.sa_family == AF_INET) {
1663 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
1664 struct ip_mreqn mreq = {
1665 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1666 .imr_ifindex = ifindex,
1667 };
1668
1669 sk = sock4->sock->sk;
1670 lock_sock(sk);
1671 ret = ip_mc_leave_group(sk, &mreq);
1672 release_sock(sk);
1673 #if IS_ENABLED(CONFIG_IPV6)
1674 } else {
1675 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1676
1677 sk = sock6->sock->sk;
1678 lock_sock(sk);
1679 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1680 &ip->sin6.sin6_addr);
1681 release_sock(sk);
1682 #endif
1683 }
1684
1685 return ret;
1686 }
1687
vxlan_remcsum(struct vxlanhdr * unparsed,struct sk_buff * skb,u32 vxflags)1688 static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1689 struct sk_buff *skb, u32 vxflags)
1690 {
1691 size_t start, offset;
1692
1693 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1694 goto out;
1695
1696 start = vxlan_rco_start(unparsed->vx_vni);
1697 offset = start + vxlan_rco_offset(unparsed->vx_vni);
1698
1699 if (!pskb_may_pull(skb, offset + sizeof(u16)))
1700 return false;
1701
1702 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1703 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
1704 out:
1705 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1706 unparsed->vx_vni &= VXLAN_VNI_MASK;
1707 return true;
1708 }
1709
vxlan_parse_gbp_hdr(struct vxlanhdr * unparsed,struct sk_buff * skb,u32 vxflags,struct vxlan_metadata * md)1710 static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
1711 struct sk_buff *skb, u32 vxflags,
1712 struct vxlan_metadata *md)
1713 {
1714 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
1715 struct metadata_dst *tun_dst;
1716
1717 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1718 goto out;
1719
1720 md->gbp = ntohs(gbp->policy_id);
1721
1722 tun_dst = (struct metadata_dst *)skb_dst(skb);
1723 if (tun_dst) {
1724 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
1725 tun_dst->u.tun_info.options_len = sizeof(*md);
1726 }
1727 if (gbp->dont_learn)
1728 md->gbp |= VXLAN_GBP_DONT_LEARN;
1729
1730 if (gbp->policy_applied)
1731 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
1732
1733 /* In flow-based mode, GBP is carried in dst_metadata */
1734 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1735 skb->mark = md->gbp;
1736 out:
1737 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
1738 }
1739
vxlan_parse_gpe_hdr(struct vxlanhdr * unparsed,__be16 * protocol,struct sk_buff * skb,u32 vxflags)1740 static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
1741 __be16 *protocol,
1742 struct sk_buff *skb, u32 vxflags)
1743 {
1744 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1745
1746 /* Need to have Next Protocol set for interfaces in GPE mode. */
1747 if (!gpe->np_applied)
1748 return false;
1749 /* "The initial version is 0. If a receiver does not support the
1750 * version indicated it MUST drop the packet.
1751 */
1752 if (gpe->version != 0)
1753 return false;
1754 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1755 * processing MUST occur." However, we don't implement OAM
1756 * processing, thus drop the packet.
1757 */
1758 if (gpe->oam_flag)
1759 return false;
1760
1761 *protocol = tun_p_to_eth_p(gpe->next_protocol);
1762 if (!*protocol)
1763 return false;
1764
1765 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1766 return true;
1767 }
1768
vxlan_set_mac(struct vxlan_dev * vxlan,struct vxlan_sock * vs,struct sk_buff * skb,__be32 vni)1769 static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1770 struct vxlan_sock *vs,
1771 struct sk_buff *skb, __be32 vni)
1772 {
1773 union vxlan_addr saddr;
1774 u32 ifindex = skb->dev->ifindex;
1775
1776 skb_reset_mac_header(skb);
1777 skb->protocol = eth_type_trans(skb, vxlan->dev);
1778 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1779
1780 /* Ignore packet loops (and multicast echo) */
1781 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1782 return false;
1783
1784 /* Get address from the outer IP header */
1785 if (vxlan_get_sk_family(vs) == AF_INET) {
1786 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
1787 saddr.sa.sa_family = AF_INET;
1788 #if IS_ENABLED(CONFIG_IPV6)
1789 } else {
1790 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
1791 saddr.sa.sa_family = AF_INET6;
1792 #endif
1793 }
1794
1795 if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
1796 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
1797 return false;
1798
1799 return true;
1800 }
1801
vxlan_ecn_decapsulate(struct vxlan_sock * vs,void * oiph,struct sk_buff * skb)1802 static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1803 struct sk_buff *skb)
1804 {
1805 int err = 0;
1806
1807 if (vxlan_get_sk_family(vs) == AF_INET)
1808 err = IP_ECN_decapsulate(oiph, skb);
1809 #if IS_ENABLED(CONFIG_IPV6)
1810 else
1811 err = IP6_ECN_decapsulate(oiph, skb);
1812 #endif
1813
1814 if (unlikely(err) && log_ecn_error) {
1815 if (vxlan_get_sk_family(vs) == AF_INET)
1816 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1817 &((struct iphdr *)oiph)->saddr,
1818 ((struct iphdr *)oiph)->tos);
1819 else
1820 net_info_ratelimited("non-ECT from %pI6\n",
1821 &((struct ipv6hdr *)oiph)->saddr);
1822 }
1823 return err <= 1;
1824 }
1825
1826 /* Callback from net/ipv4/udp.c to receive packets */
vxlan_rcv(struct sock * sk,struct sk_buff * skb)1827 static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
1828 {
1829 struct vxlan_dev *vxlan;
1830 struct vxlan_sock *vs;
1831 struct vxlanhdr unparsed;
1832 struct vxlan_metadata _md;
1833 struct vxlan_metadata *md = &_md;
1834 __be16 protocol = htons(ETH_P_TEB);
1835 bool raw_proto = false;
1836 void *oiph;
1837 __be32 vni = 0;
1838
1839 /* Need UDP and VXLAN header to be present */
1840 if (!pskb_may_pull(skb, VXLAN_HLEN))
1841 goto drop;
1842
1843 unparsed = *vxlan_hdr(skb);
1844 /* VNI flag always required to be set */
1845 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1846 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1847 ntohl(vxlan_hdr(skb)->vx_flags),
1848 ntohl(vxlan_hdr(skb)->vx_vni));
1849 /* Return non vxlan pkt */
1850 goto drop;
1851 }
1852 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1853 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
1854
1855 vs = rcu_dereference_sk_user_data(sk);
1856 if (!vs)
1857 goto drop;
1858
1859 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1860
1861 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1862 if (!vxlan)
1863 goto drop;
1864
1865 /* For backwards compatibility, only allow reserved fields to be
1866 * used by VXLAN extensions if explicitly requested.
1867 */
1868 if (vs->flags & VXLAN_F_GPE) {
1869 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1870 goto drop;
1871 raw_proto = true;
1872 }
1873
1874 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1875 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1876 goto drop;
1877
1878 if (vs->flags & VXLAN_F_REMCSUM_RX)
1879 if (unlikely(!vxlan_remcsum(&unparsed, skb, vs->flags)))
1880 goto drop;
1881
1882 if (vxlan_collect_metadata(vs)) {
1883 struct metadata_dst *tun_dst;
1884
1885 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
1886 key32_to_tunnel_id(vni), sizeof(*md));
1887
1888 if (!tun_dst)
1889 goto drop;
1890
1891 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
1892
1893 skb_dst_set(skb, (struct dst_entry *)tun_dst);
1894 } else {
1895 memset(md, 0, sizeof(*md));
1896 }
1897
1898 if (vs->flags & VXLAN_F_GBP)
1899 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
1900 /* Note that GBP and GPE can never be active together. This is
1901 * ensured in vxlan_dev_configure.
1902 */
1903
1904 if (unparsed.vx_flags || unparsed.vx_vni) {
1905 /* If there are any unprocessed flags remaining treat
1906 * this as a malformed packet. This behavior diverges from
1907 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1908 * in reserved fields are to be ignored. The approach here
1909 * maintains compatibility with previous stack code, and also
1910 * is more robust and provides a little more security in
1911 * adding extensions to VXLAN.
1912 */
1913 goto drop;
1914 }
1915
1916 if (!raw_proto) {
1917 if (!vxlan_set_mac(vxlan, vs, skb, vni))
1918 goto drop;
1919 } else {
1920 skb_reset_mac_header(skb);
1921 skb->dev = vxlan->dev;
1922 skb->pkt_type = PACKET_HOST;
1923 }
1924
1925 oiph = skb_network_header(skb);
1926 skb_reset_network_header(skb);
1927
1928 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1929 ++vxlan->dev->stats.rx_frame_errors;
1930 ++vxlan->dev->stats.rx_errors;
1931 goto drop;
1932 }
1933
1934 rcu_read_lock();
1935
1936 if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
1937 rcu_read_unlock();
1938 atomic_long_inc(&vxlan->dev->rx_dropped);
1939 goto drop;
1940 }
1941
1942 dev_sw_netstats_rx_add(vxlan->dev, skb->len);
1943 gro_cells_receive(&vxlan->gro_cells, skb);
1944
1945 rcu_read_unlock();
1946
1947 return 0;
1948
1949 drop:
1950 /* Consume bad packet */
1951 kfree_skb(skb);
1952 return 0;
1953 }
1954
1955 /* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
vxlan_err_lookup(struct sock * sk,struct sk_buff * skb)1956 static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
1957 {
1958 struct vxlan_dev *vxlan;
1959 struct vxlan_sock *vs;
1960 struct vxlanhdr *hdr;
1961 __be32 vni;
1962
1963 if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
1964 return -EINVAL;
1965
1966 hdr = vxlan_hdr(skb);
1967
1968 if (!(hdr->vx_flags & VXLAN_HF_VNI))
1969 return -EINVAL;
1970
1971 vs = rcu_dereference_sk_user_data(sk);
1972 if (!vs)
1973 return -ENOENT;
1974
1975 vni = vxlan_vni(hdr->vx_vni);
1976 vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
1977 if (!vxlan)
1978 return -ENOENT;
1979
1980 return 0;
1981 }
1982
arp_reduce(struct net_device * dev,struct sk_buff * skb,__be32 vni)1983 static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
1984 {
1985 struct vxlan_dev *vxlan = netdev_priv(dev);
1986 struct arphdr *parp;
1987 u8 *arpptr, *sha;
1988 __be32 sip, tip;
1989 struct neighbour *n;
1990
1991 if (dev->flags & IFF_NOARP)
1992 goto out;
1993
1994 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1995 dev->stats.tx_dropped++;
1996 goto out;
1997 }
1998 parp = arp_hdr(skb);
1999
2000 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
2001 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
2002 parp->ar_pro != htons(ETH_P_IP) ||
2003 parp->ar_op != htons(ARPOP_REQUEST) ||
2004 parp->ar_hln != dev->addr_len ||
2005 parp->ar_pln != 4)
2006 goto out;
2007 arpptr = (u8 *)parp + sizeof(struct arphdr);
2008 sha = arpptr;
2009 arpptr += dev->addr_len; /* sha */
2010 memcpy(&sip, arpptr, sizeof(sip));
2011 arpptr += sizeof(sip);
2012 arpptr += dev->addr_len; /* tha */
2013 memcpy(&tip, arpptr, sizeof(tip));
2014
2015 if (ipv4_is_loopback(tip) ||
2016 ipv4_is_multicast(tip))
2017 goto out;
2018
2019 n = neigh_lookup(&arp_tbl, &tip, dev);
2020
2021 if (n) {
2022 struct vxlan_fdb *f;
2023 struct sk_buff *reply;
2024
2025 if (!(n->nud_state & NUD_CONNECTED)) {
2026 neigh_release(n);
2027 goto out;
2028 }
2029
2030 f = vxlan_find_mac(vxlan, n->ha, vni);
2031 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2032 /* bridge-local neighbor */
2033 neigh_release(n);
2034 goto out;
2035 }
2036
2037 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
2038 n->ha, sha);
2039
2040 neigh_release(n);
2041
2042 if (reply == NULL)
2043 goto out;
2044
2045 skb_reset_mac_header(reply);
2046 __skb_pull(reply, skb_network_offset(reply));
2047 reply->ip_summed = CHECKSUM_UNNECESSARY;
2048 reply->pkt_type = PACKET_HOST;
2049
2050 if (netif_rx_ni(reply) == NET_RX_DROP)
2051 dev->stats.rx_dropped++;
2052 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2053 union vxlan_addr ipa = {
2054 .sin.sin_addr.s_addr = tip,
2055 .sin.sin_family = AF_INET,
2056 };
2057
2058 vxlan_ip_miss(dev, &ipa);
2059 }
2060 out:
2061 consume_skb(skb);
2062 return NETDEV_TX_OK;
2063 }
2064
2065 #if IS_ENABLED(CONFIG_IPV6)
vxlan_na_create(struct sk_buff * request,struct neighbour * n,bool isrouter)2066 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
2067 struct neighbour *n, bool isrouter)
2068 {
2069 struct net_device *dev = request->dev;
2070 struct sk_buff *reply;
2071 struct nd_msg *ns, *na;
2072 struct ipv6hdr *pip6;
2073 u8 *daddr;
2074 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
2075 int ns_olen;
2076 int i, len;
2077
2078 if (dev == NULL || !pskb_may_pull(request, request->len))
2079 return NULL;
2080
2081 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
2082 sizeof(*na) + na_olen + dev->needed_tailroom;
2083 reply = alloc_skb(len, GFP_ATOMIC);
2084 if (reply == NULL)
2085 return NULL;
2086
2087 reply->protocol = htons(ETH_P_IPV6);
2088 reply->dev = dev;
2089 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
2090 skb_push(reply, sizeof(struct ethhdr));
2091 skb_reset_mac_header(reply);
2092
2093 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
2094
2095 daddr = eth_hdr(request)->h_source;
2096 ns_olen = request->len - skb_network_offset(request) -
2097 sizeof(struct ipv6hdr) - sizeof(*ns);
2098 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
2099 if (!ns->opt[i + 1]) {
2100 kfree_skb(reply);
2101 return NULL;
2102 }
2103 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
2104 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
2105 break;
2106 }
2107 }
2108
2109 /* Ethernet header */
2110 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
2111 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
2112 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
2113 reply->protocol = htons(ETH_P_IPV6);
2114
2115 skb_pull(reply, sizeof(struct ethhdr));
2116 skb_reset_network_header(reply);
2117 skb_put(reply, sizeof(struct ipv6hdr));
2118
2119 /* IPv6 header */
2120
2121 pip6 = ipv6_hdr(reply);
2122 memset(pip6, 0, sizeof(struct ipv6hdr));
2123 pip6->version = 6;
2124 pip6->priority = ipv6_hdr(request)->priority;
2125 pip6->nexthdr = IPPROTO_ICMPV6;
2126 pip6->hop_limit = 255;
2127 pip6->daddr = ipv6_hdr(request)->saddr;
2128 pip6->saddr = *(struct in6_addr *)n->primary_key;
2129
2130 skb_pull(reply, sizeof(struct ipv6hdr));
2131 skb_reset_transport_header(reply);
2132
2133 /* Neighbor Advertisement */
2134 na = skb_put_zero(reply, sizeof(*na) + na_olen);
2135 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
2136 na->icmph.icmp6_router = isrouter;
2137 na->icmph.icmp6_override = 1;
2138 na->icmph.icmp6_solicited = 1;
2139 na->target = ns->target;
2140 ether_addr_copy(&na->opt[2], n->ha);
2141 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
2142 na->opt[1] = na_olen >> 3;
2143
2144 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
2145 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
2146 csum_partial(na, sizeof(*na)+na_olen, 0));
2147
2148 pip6->payload_len = htons(sizeof(*na)+na_olen);
2149
2150 skb_push(reply, sizeof(struct ipv6hdr));
2151
2152 reply->ip_summed = CHECKSUM_UNNECESSARY;
2153
2154 return reply;
2155 }
2156
neigh_reduce(struct net_device * dev,struct sk_buff * skb,__be32 vni)2157 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
2158 {
2159 struct vxlan_dev *vxlan = netdev_priv(dev);
2160 const struct in6_addr *daddr;
2161 const struct ipv6hdr *iphdr;
2162 struct inet6_dev *in6_dev;
2163 struct neighbour *n;
2164 struct nd_msg *msg;
2165
2166 rcu_read_lock();
2167 in6_dev = __in6_dev_get(dev);
2168 if (!in6_dev)
2169 goto out;
2170
2171 iphdr = ipv6_hdr(skb);
2172 daddr = &iphdr->daddr;
2173 msg = (struct nd_msg *)(iphdr + 1);
2174
2175 if (ipv6_addr_loopback(daddr) ||
2176 ipv6_addr_is_multicast(&msg->target))
2177 goto out;
2178
2179 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
2180
2181 if (n) {
2182 struct vxlan_fdb *f;
2183 struct sk_buff *reply;
2184
2185 if (!(n->nud_state & NUD_CONNECTED)) {
2186 neigh_release(n);
2187 goto out;
2188 }
2189
2190 f = vxlan_find_mac(vxlan, n->ha, vni);
2191 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
2192 /* bridge-local neighbor */
2193 neigh_release(n);
2194 goto out;
2195 }
2196
2197 reply = vxlan_na_create(skb, n,
2198 !!(f ? f->flags & NTF_ROUTER : 0));
2199
2200 neigh_release(n);
2201
2202 if (reply == NULL)
2203 goto out;
2204
2205 if (netif_rx_ni(reply) == NET_RX_DROP)
2206 dev->stats.rx_dropped++;
2207
2208 } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
2209 union vxlan_addr ipa = {
2210 .sin6.sin6_addr = msg->target,
2211 .sin6.sin6_family = AF_INET6,
2212 };
2213
2214 vxlan_ip_miss(dev, &ipa);
2215 }
2216
2217 out:
2218 rcu_read_unlock();
2219 consume_skb(skb);
2220 return NETDEV_TX_OK;
2221 }
2222 #endif
2223
route_shortcircuit(struct net_device * dev,struct sk_buff * skb)2224 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
2225 {
2226 struct vxlan_dev *vxlan = netdev_priv(dev);
2227 struct neighbour *n;
2228
2229 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2230 return false;
2231
2232 n = NULL;
2233 switch (ntohs(eth_hdr(skb)->h_proto)) {
2234 case ETH_P_IP:
2235 {
2236 struct iphdr *pip;
2237
2238 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
2239 return false;
2240 pip = ip_hdr(skb);
2241 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
2242 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2243 union vxlan_addr ipa = {
2244 .sin.sin_addr.s_addr = pip->daddr,
2245 .sin.sin_family = AF_INET,
2246 };
2247
2248 vxlan_ip_miss(dev, &ipa);
2249 return false;
2250 }
2251
2252 break;
2253 }
2254 #if IS_ENABLED(CONFIG_IPV6)
2255 case ETH_P_IPV6:
2256 {
2257 struct ipv6hdr *pip6;
2258
2259 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
2260 return false;
2261 pip6 = ipv6_hdr(skb);
2262 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
2263 if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
2264 union vxlan_addr ipa = {
2265 .sin6.sin6_addr = pip6->daddr,
2266 .sin6.sin6_family = AF_INET6,
2267 };
2268
2269 vxlan_ip_miss(dev, &ipa);
2270 return false;
2271 }
2272
2273 break;
2274 }
2275 #endif
2276 default:
2277 return false;
2278 }
2279
2280 if (n) {
2281 bool diff;
2282
2283 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
2284 if (diff) {
2285 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2286 dev->addr_len);
2287 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
2288 }
2289 neigh_release(n);
2290 return diff;
2291 }
2292
2293 return false;
2294 }
2295
vxlan_build_gbp_hdr(struct vxlanhdr * vxh,u32 vxflags,struct vxlan_metadata * md)2296 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
2297 struct vxlan_metadata *md)
2298 {
2299 struct vxlanhdr_gbp *gbp;
2300
2301 if (!md->gbp)
2302 return;
2303
2304 gbp = (struct vxlanhdr_gbp *)vxh;
2305 vxh->vx_flags |= VXLAN_HF_GBP;
2306
2307 if (md->gbp & VXLAN_GBP_DONT_LEARN)
2308 gbp->dont_learn = 1;
2309
2310 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
2311 gbp->policy_applied = 1;
2312
2313 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
2314 }
2315
vxlan_build_gpe_hdr(struct vxlanhdr * vxh,u32 vxflags,__be16 protocol)2316 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
2317 __be16 protocol)
2318 {
2319 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2320
2321 gpe->np_applied = 1;
2322 gpe->next_protocol = tun_p_from_eth_p(protocol);
2323 if (!gpe->next_protocol)
2324 return -EPFNOSUPPORT;
2325 return 0;
2326 }
2327
vxlan_build_skb(struct sk_buff * skb,struct dst_entry * dst,int iphdr_len,__be32 vni,struct vxlan_metadata * md,u32 vxflags,bool udp_sum)2328 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2329 int iphdr_len, __be32 vni,
2330 struct vxlan_metadata *md, u32 vxflags,
2331 bool udp_sum)
2332 {
2333 struct vxlanhdr *vxh;
2334 int min_headroom;
2335 int err;
2336 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
2337 __be16 inner_protocol = htons(ETH_P_TEB);
2338
2339 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
2340 skb->ip_summed == CHECKSUM_PARTIAL) {
2341 int csum_start = skb_checksum_start_offset(skb);
2342
2343 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2344 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2345 (skb->csum_offset == offsetof(struct udphdr, check) ||
2346 skb->csum_offset == offsetof(struct tcphdr, check)))
2347 type |= SKB_GSO_TUNNEL_REMCSUM;
2348 }
2349
2350 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
2351 + VXLAN_HLEN + iphdr_len;
2352
2353 /* Need space for new headers (invalidates iph ptr) */
2354 err = skb_cow_head(skb, min_headroom);
2355 if (unlikely(err))
2356 return err;
2357
2358 err = iptunnel_handle_offloads(skb, type);
2359 if (err)
2360 return err;
2361
2362 vxh = __skb_push(skb, sizeof(*vxh));
2363 vxh->vx_flags = VXLAN_HF_VNI;
2364 vxh->vx_vni = vxlan_vni_field(vni);
2365
2366 if (type & SKB_GSO_TUNNEL_REMCSUM) {
2367 unsigned int start;
2368
2369 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2370 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2371 vxh->vx_flags |= VXLAN_HF_RCO;
2372
2373 if (!skb_is_gso(skb)) {
2374 skb->ip_summed = CHECKSUM_NONE;
2375 skb->encapsulation = 0;
2376 }
2377 }
2378
2379 if (vxflags & VXLAN_F_GBP)
2380 vxlan_build_gbp_hdr(vxh, vxflags, md);
2381 if (vxflags & VXLAN_F_GPE) {
2382 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
2383 if (err < 0)
2384 return err;
2385 inner_protocol = skb->protocol;
2386 }
2387
2388 skb_set_inner_protocol(skb, inner_protocol);
2389 return 0;
2390 }
2391
vxlan_get_route(struct vxlan_dev * vxlan,struct net_device * dev,struct vxlan_sock * sock4,struct sk_buff * skb,int oif,u8 tos,__be32 daddr,__be32 * saddr,__be16 dport,__be16 sport,struct dst_cache * dst_cache,const struct ip_tunnel_info * info)2392 static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
2393 struct vxlan_sock *sock4,
2394 struct sk_buff *skb, int oif, u8 tos,
2395 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
2396 struct dst_cache *dst_cache,
2397 const struct ip_tunnel_info *info)
2398 {
2399 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2400 struct rtable *rt = NULL;
2401 struct flowi4 fl4;
2402
2403 if (!sock4)
2404 return ERR_PTR(-EIO);
2405
2406 if (tos && !info)
2407 use_cache = false;
2408 if (use_cache) {
2409 rt = dst_cache_get_ip4(dst_cache, saddr);
2410 if (rt)
2411 return rt;
2412 }
2413
2414 memset(&fl4, 0, sizeof(fl4));
2415 fl4.flowi4_oif = oif;
2416 fl4.flowi4_tos = RT_TOS(tos);
2417 fl4.flowi4_mark = skb->mark;
2418 fl4.flowi4_proto = IPPROTO_UDP;
2419 fl4.daddr = daddr;
2420 fl4.saddr = *saddr;
2421 fl4.fl4_dport = dport;
2422 fl4.fl4_sport = sport;
2423
2424 rt = ip_route_output_key(vxlan->net, &fl4);
2425 if (!IS_ERR(rt)) {
2426 if (rt->dst.dev == dev) {
2427 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
2428 ip_rt_put(rt);
2429 return ERR_PTR(-ELOOP);
2430 }
2431
2432 *saddr = fl4.saddr;
2433 if (use_cache)
2434 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
2435 } else {
2436 netdev_dbg(dev, "no route to %pI4\n", &daddr);
2437 return ERR_PTR(-ENETUNREACH);
2438 }
2439 return rt;
2440 }
2441
2442 #if IS_ENABLED(CONFIG_IPV6)
vxlan6_get_route(struct vxlan_dev * vxlan,struct net_device * dev,struct vxlan_sock * sock6,struct sk_buff * skb,int oif,u8 tos,__be32 label,const struct in6_addr * daddr,struct in6_addr * saddr,__be16 dport,__be16 sport,struct dst_cache * dst_cache,const struct ip_tunnel_info * info)2443 static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
2444 struct net_device *dev,
2445 struct vxlan_sock *sock6,
2446 struct sk_buff *skb, int oif, u8 tos,
2447 __be32 label,
2448 const struct in6_addr *daddr,
2449 struct in6_addr *saddr,
2450 __be16 dport, __be16 sport,
2451 struct dst_cache *dst_cache,
2452 const struct ip_tunnel_info *info)
2453 {
2454 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
2455 struct dst_entry *ndst;
2456 struct flowi6 fl6;
2457
2458 if (!sock6)
2459 return ERR_PTR(-EIO);
2460
2461 if (tos && !info)
2462 use_cache = false;
2463 if (use_cache) {
2464 ndst = dst_cache_get_ip6(dst_cache, saddr);
2465 if (ndst)
2466 return ndst;
2467 }
2468
2469 memset(&fl6, 0, sizeof(fl6));
2470 fl6.flowi6_oif = oif;
2471 fl6.daddr = *daddr;
2472 fl6.saddr = *saddr;
2473 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
2474 fl6.flowi6_mark = skb->mark;
2475 fl6.flowi6_proto = IPPROTO_UDP;
2476 fl6.fl6_dport = dport;
2477 fl6.fl6_sport = sport;
2478
2479 ndst = ipv6_stub->ipv6_dst_lookup_flow(vxlan->net, sock6->sock->sk,
2480 &fl6, NULL);
2481 if (unlikely(IS_ERR(ndst))) {
2482 netdev_dbg(dev, "no route to %pI6\n", daddr);
2483 return ERR_PTR(-ENETUNREACH);
2484 }
2485
2486 if (unlikely(ndst->dev == dev)) {
2487 netdev_dbg(dev, "circular route to %pI6\n", daddr);
2488 dst_release(ndst);
2489 return ERR_PTR(-ELOOP);
2490 }
2491
2492 *saddr = fl6.saddr;
2493 if (use_cache)
2494 dst_cache_set_ip6(dst_cache, ndst, saddr);
2495 return ndst;
2496 }
2497 #endif
2498
2499 /* Bypass encapsulation if the destination is local */
vxlan_encap_bypass(struct sk_buff * skb,struct vxlan_dev * src_vxlan,struct vxlan_dev * dst_vxlan,__be32 vni,bool snoop)2500 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2501 struct vxlan_dev *dst_vxlan, __be32 vni,
2502 bool snoop)
2503 {
2504 struct pcpu_sw_netstats *tx_stats, *rx_stats;
2505 union vxlan_addr loopback;
2506 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
2507 struct net_device *dev;
2508 int len = skb->len;
2509
2510 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
2511 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
2512 skb->pkt_type = PACKET_HOST;
2513 skb->encapsulation = 0;
2514 skb->dev = dst_vxlan->dev;
2515 __skb_pull(skb, skb_network_offset(skb));
2516
2517 if (remote_ip->sa.sa_family == AF_INET) {
2518 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2519 loopback.sa.sa_family = AF_INET;
2520 #if IS_ENABLED(CONFIG_IPV6)
2521 } else {
2522 loopback.sin6.sin6_addr = in6addr_loopback;
2523 loopback.sa.sa_family = AF_INET6;
2524 #endif
2525 }
2526
2527 rcu_read_lock();
2528 dev = skb->dev;
2529 if (unlikely(!(dev->flags & IFF_UP))) {
2530 kfree_skb(skb);
2531 goto drop;
2532 }
2533
2534 if ((dst_vxlan->cfg.flags & VXLAN_F_LEARN) && snoop)
2535 vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
2536
2537 u64_stats_update_begin(&tx_stats->syncp);
2538 tx_stats->tx_packets++;
2539 tx_stats->tx_bytes += len;
2540 u64_stats_update_end(&tx_stats->syncp);
2541
2542 if (netif_rx(skb) == NET_RX_SUCCESS) {
2543 u64_stats_update_begin(&rx_stats->syncp);
2544 rx_stats->rx_packets++;
2545 rx_stats->rx_bytes += len;
2546 u64_stats_update_end(&rx_stats->syncp);
2547 } else {
2548 drop:
2549 dev->stats.rx_dropped++;
2550 }
2551 rcu_read_unlock();
2552 }
2553
encap_bypass_if_local(struct sk_buff * skb,struct net_device * dev,struct vxlan_dev * vxlan,union vxlan_addr * daddr,__be16 dst_port,int dst_ifindex,__be32 vni,struct dst_entry * dst,u32 rt_flags)2554 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2555 struct vxlan_dev *vxlan,
2556 union vxlan_addr *daddr,
2557 __be16 dst_port, int dst_ifindex, __be32 vni,
2558 struct dst_entry *dst,
2559 u32 rt_flags)
2560 {
2561 #if IS_ENABLED(CONFIG_IPV6)
2562 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2563 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2564 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2565 */
2566 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2567 #endif
2568 /* Bypass encapsulation if the destination is local */
2569 if (rt_flags & RTCF_LOCAL &&
2570 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2571 struct vxlan_dev *dst_vxlan;
2572
2573 dst_release(dst);
2574 dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2575 daddr->sa.sa_family, dst_port,
2576 vxlan->cfg.flags);
2577 if (!dst_vxlan) {
2578 dev->stats.tx_errors++;
2579 kfree_skb(skb);
2580
2581 return -ENOENT;
2582 }
2583 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni, true);
2584 return 1;
2585 }
2586
2587 return 0;
2588 }
2589
vxlan_xmit_one(struct sk_buff * skb,struct net_device * dev,__be32 default_vni,struct vxlan_rdst * rdst,bool did_rsc)2590 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2591 __be32 default_vni, struct vxlan_rdst *rdst,
2592 bool did_rsc)
2593 {
2594 struct dst_cache *dst_cache;
2595 struct ip_tunnel_info *info;
2596 struct vxlan_dev *vxlan = netdev_priv(dev);
2597 const struct iphdr *old_iph = ip_hdr(skb);
2598 union vxlan_addr *dst;
2599 union vxlan_addr remote_ip, local_ip;
2600 struct vxlan_metadata _md;
2601 struct vxlan_metadata *md = &_md;
2602 __be16 src_port = 0, dst_port;
2603 struct dst_entry *ndst = NULL;
2604 __be32 vni, label;
2605 __u8 tos, ttl;
2606 int ifindex;
2607 int err;
2608 u32 flags = vxlan->cfg.flags;
2609 bool udp_sum = false;
2610 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2611
2612 info = skb_tunnel_info(skb);
2613
2614 if (rdst) {
2615 dst = &rdst->remote_ip;
2616 if (vxlan_addr_any(dst)) {
2617 if (did_rsc) {
2618 /* short-circuited back to local bridge */
2619 vxlan_encap_bypass(skb, vxlan, vxlan,
2620 default_vni, true);
2621 return;
2622 }
2623 goto drop;
2624 }
2625
2626 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
2627 vni = (rdst->remote_vni) ? : default_vni;
2628 ifindex = rdst->remote_ifindex;
2629 local_ip = vxlan->cfg.saddr;
2630 dst_cache = &rdst->dst_cache;
2631 md->gbp = skb->mark;
2632 if (flags & VXLAN_F_TTL_INHERIT) {
2633 ttl = ip_tunnel_get_ttl(old_iph, skb);
2634 } else {
2635 ttl = vxlan->cfg.ttl;
2636 if (!ttl && vxlan_addr_multicast(dst))
2637 ttl = 1;
2638 }
2639
2640 tos = vxlan->cfg.tos;
2641 if (tos == 1)
2642 tos = ip_tunnel_get_dsfield(old_iph, skb);
2643
2644 if (dst->sa.sa_family == AF_INET)
2645 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2646 else
2647 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2648 label = vxlan->cfg.label;
2649 } else {
2650 if (!info) {
2651 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2652 dev->name);
2653 goto drop;
2654 }
2655 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
2656 if (remote_ip.sa.sa_family == AF_INET) {
2657 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
2658 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2659 } else {
2660 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
2661 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2662 }
2663 dst = &remote_ip;
2664 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2665 vni = tunnel_id_to_key32(info->key.tun_id);
2666 ifindex = 0;
2667 dst_cache = &info->dst_cache;
2668 if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
2669 if (info->options_len < sizeof(*md))
2670 goto drop;
2671 md = ip_tunnel_info_opts(info);
2672 }
2673 ttl = info->key.ttl;
2674 tos = info->key.tos;
2675 label = info->key.label;
2676 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
2677 }
2678 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2679 vxlan->cfg.port_max, true);
2680
2681 rcu_read_lock();
2682 if (dst->sa.sa_family == AF_INET) {
2683 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
2684 struct rtable *rt;
2685 __be16 df = 0;
2686
2687 if (!ifindex)
2688 ifindex = sock4->sock->sk->sk_bound_dev_if;
2689
2690 rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
2691 dst->sin.sin_addr.s_addr,
2692 &local_ip.sin.sin_addr.s_addr,
2693 dst_port, src_port,
2694 dst_cache, info);
2695 if (IS_ERR(rt)) {
2696 err = PTR_ERR(rt);
2697 goto tx_error;
2698 }
2699
2700 if (!info) {
2701 /* Bypass encapsulation if the destination is local */
2702 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2703 dst_port, ifindex, vni,
2704 &rt->dst, rt->rt_flags);
2705 if (err)
2706 goto out_unlock;
2707
2708 if (vxlan->cfg.df == VXLAN_DF_SET) {
2709 df = htons(IP_DF);
2710 } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
2711 struct ethhdr *eth = eth_hdr(skb);
2712
2713 if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2714 (ntohs(eth->h_proto) == ETH_P_IP &&
2715 old_iph->frag_off & htons(IP_DF)))
2716 df = htons(IP_DF);
2717 }
2718 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
2719 df = htons(IP_DF);
2720 }
2721
2722 ndst = &rt->dst;
2723 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN_HEADROOM,
2724 netif_is_any_bridge_port(dev));
2725 if (err < 0) {
2726 goto tx_error;
2727 } else if (err) {
2728 if (info) {
2729 struct ip_tunnel_info *unclone;
2730 struct in_addr src, dst;
2731
2732 unclone = skb_tunnel_info_unclone(skb);
2733 if (unlikely(!unclone))
2734 goto tx_error;
2735
2736 src = remote_ip.sin.sin_addr;
2737 dst = local_ip.sin.sin_addr;
2738 unclone->key.u.ipv4.src = src.s_addr;
2739 unclone->key.u.ipv4.dst = dst.s_addr;
2740 }
2741 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2742 dst_release(ndst);
2743 goto out_unlock;
2744 }
2745
2746 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2747 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2748 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2749 vni, md, flags, udp_sum);
2750 if (err < 0)
2751 goto tx_error;
2752
2753 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
2754 dst->sin.sin_addr.s_addr, tos, ttl, df,
2755 src_port, dst_port, xnet, !udp_sum);
2756 #if IS_ENABLED(CONFIG_IPV6)
2757 } else {
2758 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
2759
2760 if (!ifindex)
2761 ifindex = sock6->sock->sk->sk_bound_dev_if;
2762
2763 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
2764 label, &dst->sin6.sin6_addr,
2765 &local_ip.sin6.sin6_addr,
2766 dst_port, src_port,
2767 dst_cache, info);
2768 if (IS_ERR(ndst)) {
2769 err = PTR_ERR(ndst);
2770 ndst = NULL;
2771 goto tx_error;
2772 }
2773
2774 if (!info) {
2775 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
2776
2777 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2778 dst_port, ifindex, vni,
2779 ndst, rt6i_flags);
2780 if (err)
2781 goto out_unlock;
2782 }
2783
2784 err = skb_tunnel_check_pmtu(skb, ndst, VXLAN6_HEADROOM,
2785 netif_is_any_bridge_port(dev));
2786 if (err < 0) {
2787 goto tx_error;
2788 } else if (err) {
2789 if (info) {
2790 struct ip_tunnel_info *unclone;
2791 struct in6_addr src, dst;
2792
2793 unclone = skb_tunnel_info_unclone(skb);
2794 if (unlikely(!unclone))
2795 goto tx_error;
2796
2797 src = remote_ip.sin6.sin6_addr;
2798 dst = local_ip.sin6.sin6_addr;
2799 unclone->key.u.ipv6.src = src;
2800 unclone->key.u.ipv6.dst = dst;
2801 }
2802
2803 vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
2804 dst_release(ndst);
2805 goto out_unlock;
2806 }
2807
2808 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2809 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2810 skb_scrub_packet(skb, xnet);
2811 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2812 vni, md, flags, udp_sum);
2813 if (err < 0)
2814 goto tx_error;
2815
2816 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
2817 &local_ip.sin6.sin6_addr,
2818 &dst->sin6.sin6_addr, tos, ttl,
2819 label, src_port, dst_port, !udp_sum);
2820 #endif
2821 }
2822 out_unlock:
2823 rcu_read_unlock();
2824 return;
2825
2826 drop:
2827 dev->stats.tx_dropped++;
2828 dev_kfree_skb(skb);
2829 return;
2830
2831 tx_error:
2832 rcu_read_unlock();
2833 if (err == -ELOOP)
2834 dev->stats.collisions++;
2835 else if (err == -ENETUNREACH)
2836 dev->stats.tx_carrier_errors++;
2837 dst_release(ndst);
2838 dev->stats.tx_errors++;
2839 kfree_skb(skb);
2840 }
2841
vxlan_xmit_nh(struct sk_buff * skb,struct net_device * dev,struct vxlan_fdb * f,__be32 vni,bool did_rsc)2842 static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
2843 struct vxlan_fdb *f, __be32 vni, bool did_rsc)
2844 {
2845 struct vxlan_rdst nh_rdst;
2846 struct nexthop *nh;
2847 bool do_xmit;
2848 u32 hash;
2849
2850 memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2851 hash = skb_get_hash(skb);
2852
2853 rcu_read_lock();
2854 nh = rcu_dereference(f->nh);
2855 if (!nh) {
2856 rcu_read_unlock();
2857 goto drop;
2858 }
2859 do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2860 rcu_read_unlock();
2861
2862 if (likely(do_xmit))
2863 vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc);
2864 else
2865 goto drop;
2866
2867 return;
2868
2869 drop:
2870 dev->stats.tx_dropped++;
2871 dev_kfree_skb(skb);
2872 }
2873
2874 /* Transmit local packets over Vxlan
2875 *
2876 * Outer IP header inherits ECN and DF from inner header.
2877 * Outer UDP destination is the VXLAN assigned port.
2878 * source port is based on hash of flow
2879 */
vxlan_xmit(struct sk_buff * skb,struct net_device * dev)2880 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2881 {
2882 struct vxlan_dev *vxlan = netdev_priv(dev);
2883 struct vxlan_rdst *rdst, *fdst = NULL;
2884 const struct ip_tunnel_info *info;
2885 bool did_rsc = false;
2886 struct vxlan_fdb *f;
2887 struct ethhdr *eth;
2888 __be32 vni = 0;
2889
2890 info = skb_tunnel_info(skb);
2891
2892 skb_reset_mac_header(skb);
2893
2894 if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
2895 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2896 info->mode & IP_TUNNEL_INFO_TX) {
2897 vni = tunnel_id_to_key32(info->key.tun_id);
2898 } else {
2899 if (info && info->mode & IP_TUNNEL_INFO_TX)
2900 vxlan_xmit_one(skb, dev, vni, NULL, false);
2901 else
2902 kfree_skb(skb);
2903 return NETDEV_TX_OK;
2904 }
2905 }
2906
2907 if (vxlan->cfg.flags & VXLAN_F_PROXY) {
2908 eth = eth_hdr(skb);
2909 if (ntohs(eth->h_proto) == ETH_P_ARP)
2910 return arp_reduce(dev, skb, vni);
2911 #if IS_ENABLED(CONFIG_IPV6)
2912 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2913 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
2914 sizeof(struct nd_msg)) &&
2915 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2916 struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2917
2918 if (m->icmph.icmp6_code == 0 &&
2919 m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2920 return neigh_reduce(dev, skb, vni);
2921 }
2922 #endif
2923 }
2924
2925 eth = eth_hdr(skb);
2926 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2927 did_rsc = false;
2928
2929 if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
2930 (ntohs(eth->h_proto) == ETH_P_IP ||
2931 ntohs(eth->h_proto) == ETH_P_IPV6)) {
2932 did_rsc = route_shortcircuit(dev, skb);
2933 if (did_rsc)
2934 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
2935 }
2936
2937 if (f == NULL) {
2938 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
2939 if (f == NULL) {
2940 if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
2941 !is_multicast_ether_addr(eth->h_dest))
2942 vxlan_fdb_miss(vxlan, eth->h_dest);
2943
2944 dev->stats.tx_dropped++;
2945 kfree_skb(skb);
2946 return NETDEV_TX_OK;
2947 }
2948 }
2949
2950 if (rcu_access_pointer(f->nh)) {
2951 vxlan_xmit_nh(skb, dev, f,
2952 (vni ? : vxlan->default_dst.remote_vni), did_rsc);
2953 } else {
2954 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2955 struct sk_buff *skb1;
2956
2957 if (!fdst) {
2958 fdst = rdst;
2959 continue;
2960 }
2961 skb1 = skb_clone(skb, GFP_ATOMIC);
2962 if (skb1)
2963 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
2964 }
2965 if (fdst)
2966 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
2967 else
2968 kfree_skb(skb);
2969 }
2970
2971 return NETDEV_TX_OK;
2972 }
2973
2974 /* Walk the forwarding table and purge stale entries */
vxlan_cleanup(struct timer_list * t)2975 static void vxlan_cleanup(struct timer_list *t)
2976 {
2977 struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
2978 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2979 unsigned int h;
2980
2981 if (!netif_running(vxlan->dev))
2982 return;
2983
2984 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2985 struct hlist_node *p, *n;
2986
2987 spin_lock(&vxlan->hash_lock[h]);
2988 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2989 struct vxlan_fdb *f
2990 = container_of(p, struct vxlan_fdb, hlist);
2991 unsigned long timeout;
2992
2993 if (f->state & (NUD_PERMANENT | NUD_NOARP))
2994 continue;
2995
2996 if (f->flags & NTF_EXT_LEARNED)
2997 continue;
2998
2999 timeout = f->used + vxlan->cfg.age_interval * HZ;
3000 if (time_before_eq(timeout, jiffies)) {
3001 netdev_dbg(vxlan->dev,
3002 "garbage collect %pM\n",
3003 f->eth_addr);
3004 f->state = NUD_STALE;
3005 vxlan_fdb_destroy(vxlan, f, true, true);
3006 } else if (time_before(timeout, next_timer))
3007 next_timer = timeout;
3008 }
3009 spin_unlock(&vxlan->hash_lock[h]);
3010 }
3011
3012 mod_timer(&vxlan->age_timer, next_timer);
3013 }
3014
vxlan_vs_del_dev(struct vxlan_dev * vxlan)3015 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
3016 {
3017 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3018
3019 spin_lock(&vn->sock_lock);
3020 hlist_del_init_rcu(&vxlan->hlist4.hlist);
3021 #if IS_ENABLED(CONFIG_IPV6)
3022 hlist_del_init_rcu(&vxlan->hlist6.hlist);
3023 #endif
3024 spin_unlock(&vn->sock_lock);
3025 }
3026
vxlan_vs_add_dev(struct vxlan_sock * vs,struct vxlan_dev * vxlan,struct vxlan_dev_node * node)3027 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
3028 struct vxlan_dev_node *node)
3029 {
3030 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3031 __be32 vni = vxlan->default_dst.remote_vni;
3032
3033 node->vxlan = vxlan;
3034 spin_lock(&vn->sock_lock);
3035 hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
3036 spin_unlock(&vn->sock_lock);
3037 }
3038
3039 /* Setup stats when device is created */
vxlan_init(struct net_device * dev)3040 static int vxlan_init(struct net_device *dev)
3041 {
3042 struct vxlan_dev *vxlan = netdev_priv(dev);
3043 int err;
3044
3045 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3046 if (!dev->tstats)
3047 return -ENOMEM;
3048
3049 err = gro_cells_init(&vxlan->gro_cells, dev);
3050 if (err) {
3051 free_percpu(dev->tstats);
3052 return err;
3053 }
3054
3055 return 0;
3056 }
3057
vxlan_fdb_delete_default(struct vxlan_dev * vxlan,__be32 vni)3058 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
3059 {
3060 struct vxlan_fdb *f;
3061 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
3062
3063 spin_lock_bh(&vxlan->hash_lock[hash_index]);
3064 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
3065 if (f)
3066 vxlan_fdb_destroy(vxlan, f, true, true);
3067 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
3068 }
3069
vxlan_uninit(struct net_device * dev)3070 static void vxlan_uninit(struct net_device *dev)
3071 {
3072 struct vxlan_dev *vxlan = netdev_priv(dev);
3073
3074 gro_cells_destroy(&vxlan->gro_cells);
3075
3076 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
3077
3078 free_percpu(dev->tstats);
3079 }
3080
3081 /* Start ageing timer and join group when device is brought up */
vxlan_open(struct net_device * dev)3082 static int vxlan_open(struct net_device *dev)
3083 {
3084 struct vxlan_dev *vxlan = netdev_priv(dev);
3085 int ret;
3086
3087 ret = vxlan_sock_add(vxlan);
3088 if (ret < 0)
3089 return ret;
3090
3091 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
3092 ret = vxlan_igmp_join(vxlan);
3093 if (ret == -EADDRINUSE)
3094 ret = 0;
3095 if (ret) {
3096 vxlan_sock_release(vxlan);
3097 return ret;
3098 }
3099 }
3100
3101 if (vxlan->cfg.age_interval)
3102 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
3103
3104 return ret;
3105 }
3106
3107 /* Purge the forwarding table */
vxlan_flush(struct vxlan_dev * vxlan,bool do_all)3108 static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
3109 {
3110 unsigned int h;
3111
3112 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3113 struct hlist_node *p, *n;
3114
3115 spin_lock_bh(&vxlan->hash_lock[h]);
3116 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
3117 struct vxlan_fdb *f
3118 = container_of(p, struct vxlan_fdb, hlist);
3119 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
3120 continue;
3121 /* the all_zeros_mac entry is deleted at vxlan_uninit */
3122 if (is_zero_ether_addr(f->eth_addr) &&
3123 f->vni == vxlan->cfg.vni)
3124 continue;
3125 vxlan_fdb_destroy(vxlan, f, true, true);
3126 }
3127 spin_unlock_bh(&vxlan->hash_lock[h]);
3128 }
3129 }
3130
3131 /* Cleanup timer and forwarding table on shutdown */
vxlan_stop(struct net_device * dev)3132 static int vxlan_stop(struct net_device *dev)
3133 {
3134 struct vxlan_dev *vxlan = netdev_priv(dev);
3135 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3136 int ret = 0;
3137
3138 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
3139 !vxlan_group_used(vn, vxlan))
3140 ret = vxlan_igmp_leave(vxlan);
3141
3142 del_timer_sync(&vxlan->age_timer);
3143
3144 vxlan_flush(vxlan, false);
3145 vxlan_sock_release(vxlan);
3146
3147 return ret;
3148 }
3149
3150 /* Stub, nothing needs to be done. */
vxlan_set_multicast_list(struct net_device * dev)3151 static void vxlan_set_multicast_list(struct net_device *dev)
3152 {
3153 }
3154
vxlan_change_mtu(struct net_device * dev,int new_mtu)3155 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
3156 {
3157 struct vxlan_dev *vxlan = netdev_priv(dev);
3158 struct vxlan_rdst *dst = &vxlan->default_dst;
3159 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3160 dst->remote_ifindex);
3161 bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
3162
3163 /* This check is different than dev->max_mtu, because it looks at
3164 * the lowerdev->mtu, rather than the static dev->max_mtu
3165 */
3166 if (lowerdev) {
3167 int max_mtu = lowerdev->mtu -
3168 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
3169 if (new_mtu > max_mtu)
3170 return -EINVAL;
3171 }
3172
3173 dev->mtu = new_mtu;
3174 return 0;
3175 }
3176
vxlan_fill_metadata_dst(struct net_device * dev,struct sk_buff * skb)3177 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
3178 {
3179 struct vxlan_dev *vxlan = netdev_priv(dev);
3180 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3181 __be16 sport, dport;
3182
3183 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
3184 vxlan->cfg.port_max, true);
3185 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
3186
3187 if (ip_tunnel_info_af(info) == AF_INET) {
3188 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
3189 struct rtable *rt;
3190
3191 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
3192 info->key.u.ipv4.dst,
3193 &info->key.u.ipv4.src, dport, sport,
3194 &info->dst_cache, info);
3195 if (IS_ERR(rt))
3196 return PTR_ERR(rt);
3197 ip_rt_put(rt);
3198 } else {
3199 #if IS_ENABLED(CONFIG_IPV6)
3200 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
3201 struct dst_entry *ndst;
3202
3203 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
3204 info->key.label, &info->key.u.ipv6.dst,
3205 &info->key.u.ipv6.src, dport, sport,
3206 &info->dst_cache, info);
3207 if (IS_ERR(ndst))
3208 return PTR_ERR(ndst);
3209 dst_release(ndst);
3210 #else /* !CONFIG_IPV6 */
3211 return -EPFNOSUPPORT;
3212 #endif
3213 }
3214 info->key.tp_src = sport;
3215 info->key.tp_dst = dport;
3216 return 0;
3217 }
3218
3219 static const struct net_device_ops vxlan_netdev_ether_ops = {
3220 .ndo_init = vxlan_init,
3221 .ndo_uninit = vxlan_uninit,
3222 .ndo_open = vxlan_open,
3223 .ndo_stop = vxlan_stop,
3224 .ndo_start_xmit = vxlan_xmit,
3225 .ndo_get_stats64 = ip_tunnel_get_stats64,
3226 .ndo_set_rx_mode = vxlan_set_multicast_list,
3227 .ndo_change_mtu = vxlan_change_mtu,
3228 .ndo_validate_addr = eth_validate_addr,
3229 .ndo_set_mac_address = eth_mac_addr,
3230 .ndo_fdb_add = vxlan_fdb_add,
3231 .ndo_fdb_del = vxlan_fdb_delete,
3232 .ndo_fdb_dump = vxlan_fdb_dump,
3233 .ndo_fdb_get = vxlan_fdb_get,
3234 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3235 .ndo_change_proto_down = dev_change_proto_down_generic,
3236 };
3237
3238 static const struct net_device_ops vxlan_netdev_raw_ops = {
3239 .ndo_init = vxlan_init,
3240 .ndo_uninit = vxlan_uninit,
3241 .ndo_open = vxlan_open,
3242 .ndo_stop = vxlan_stop,
3243 .ndo_start_xmit = vxlan_xmit,
3244 .ndo_get_stats64 = ip_tunnel_get_stats64,
3245 .ndo_change_mtu = vxlan_change_mtu,
3246 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
3247 };
3248
3249 /* Info for udev, that this is a virtual tunnel endpoint */
3250 static struct device_type vxlan_type = {
3251 .name = "vxlan",
3252 };
3253
3254 /* Calls the ndo_udp_tunnel_add of the caller in order to
3255 * supply the listening VXLAN udp ports. Callers are expected
3256 * to implement the ndo_udp_tunnel_add.
3257 */
vxlan_offload_rx_ports(struct net_device * dev,bool push)3258 static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
3259 {
3260 struct vxlan_sock *vs;
3261 struct net *net = dev_net(dev);
3262 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3263 unsigned int i;
3264
3265 spin_lock(&vn->sock_lock);
3266 for (i = 0; i < PORT_HASH_SIZE; ++i) {
3267 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
3268 unsigned short type;
3269
3270 if (vs->flags & VXLAN_F_GPE)
3271 type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3272 else
3273 type = UDP_TUNNEL_TYPE_VXLAN;
3274
3275 if (push)
3276 udp_tunnel_push_rx_port(dev, vs->sock, type);
3277 else
3278 udp_tunnel_drop_rx_port(dev, vs->sock, type);
3279 }
3280 }
3281 spin_unlock(&vn->sock_lock);
3282 }
3283
3284 /* Initialize the device structure. */
vxlan_setup(struct net_device * dev)3285 static void vxlan_setup(struct net_device *dev)
3286 {
3287 struct vxlan_dev *vxlan = netdev_priv(dev);
3288 unsigned int h;
3289
3290 eth_hw_addr_random(dev);
3291 ether_setup(dev);
3292
3293 dev->needs_free_netdev = true;
3294 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3295
3296 dev->features |= NETIF_F_LLTX;
3297 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
3298 dev->features |= NETIF_F_RXCSUM;
3299 dev->features |= NETIF_F_GSO_SOFTWARE;
3300
3301 dev->vlan_features = dev->features;
3302 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3303 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3304 netif_keep_dst(dev);
3305 dev->priv_flags |= IFF_NO_QUEUE;
3306
3307 /* MTU range: 68 - 65535 */
3308 dev->min_mtu = ETH_MIN_MTU;
3309 dev->max_mtu = ETH_MAX_MTU;
3310
3311 INIT_LIST_HEAD(&vxlan->next);
3312
3313 timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
3314
3315 vxlan->dev = dev;
3316
3317 for (h = 0; h < FDB_HASH_SIZE; ++h) {
3318 spin_lock_init(&vxlan->hash_lock[h]);
3319 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
3320 }
3321 }
3322
vxlan_ether_setup(struct net_device * dev)3323 static void vxlan_ether_setup(struct net_device *dev)
3324 {
3325 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3326 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3327 dev->netdev_ops = &vxlan_netdev_ether_ops;
3328 }
3329
vxlan_raw_setup(struct net_device * dev)3330 static void vxlan_raw_setup(struct net_device *dev)
3331 {
3332 dev->header_ops = NULL;
3333 dev->type = ARPHRD_NONE;
3334 dev->hard_header_len = 0;
3335 dev->addr_len = 0;
3336 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3337 dev->netdev_ops = &vxlan_netdev_raw_ops;
3338 }
3339
3340 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3341 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
3342 [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) },
3343 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
3344 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
3345 [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
3346 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
3347 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
3348 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
3349 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
3350 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
3351 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
3352 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
3353 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
3354 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
3355 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
3356 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
3357 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
3358 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
3359 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
3360 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
3361 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
3362 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
3363 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
3364 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3365 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
3366 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
3367 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
3368 [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
3369 [IFLA_VXLAN_DF] = { .type = NLA_U8 },
3370 };
3371
vxlan_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)3372 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3373 struct netlink_ext_ack *extack)
3374 {
3375 if (tb[IFLA_ADDRESS]) {
3376 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
3377 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3378 "Provided link layer address is not Ethernet");
3379 return -EINVAL;
3380 }
3381
3382 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
3383 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3384 "Provided Ethernet address is not unicast");
3385 return -EADDRNOTAVAIL;
3386 }
3387 }
3388
3389 if (tb[IFLA_MTU]) {
3390 u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3391
3392 if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3393 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3394 "MTU must be between 68 and 65535");
3395 return -EINVAL;
3396 }
3397 }
3398
3399 if (!data) {
3400 NL_SET_ERR_MSG(extack,
3401 "Required attributes not provided to perform the operation");
3402 return -EINVAL;
3403 }
3404
3405 if (data[IFLA_VXLAN_ID]) {
3406 u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3407
3408 if (id >= VXLAN_N_VID) {
3409 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
3410 "VXLAN ID must be lower than 16777216");
3411 return -ERANGE;
3412 }
3413 }
3414
3415 if (data[IFLA_VXLAN_PORT_RANGE]) {
3416 const struct ifla_vxlan_port_range *p
3417 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3418
3419 if (ntohs(p->high) < ntohs(p->low)) {
3420 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
3421 "Invalid source port range");
3422 return -EINVAL;
3423 }
3424 }
3425
3426 if (data[IFLA_VXLAN_DF]) {
3427 enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3428
3429 if (df < 0 || df > VXLAN_DF_MAX) {
3430 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
3431 "Invalid DF attribute");
3432 return -EINVAL;
3433 }
3434 }
3435
3436 return 0;
3437 }
3438
vxlan_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)3439 static void vxlan_get_drvinfo(struct net_device *netdev,
3440 struct ethtool_drvinfo *drvinfo)
3441 {
3442 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3443 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3444 }
3445
vxlan_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)3446 static int vxlan_get_link_ksettings(struct net_device *dev,
3447 struct ethtool_link_ksettings *cmd)
3448 {
3449 struct vxlan_dev *vxlan = netdev_priv(dev);
3450 struct vxlan_rdst *dst = &vxlan->default_dst;
3451 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
3452 dst->remote_ifindex);
3453
3454 if (!lowerdev) {
3455 cmd->base.duplex = DUPLEX_UNKNOWN;
3456 cmd->base.port = PORT_OTHER;
3457 cmd->base.speed = SPEED_UNKNOWN;
3458
3459 return 0;
3460 }
3461
3462 return __ethtool_get_link_ksettings(lowerdev, cmd);
3463 }
3464
3465 static const struct ethtool_ops vxlan_ethtool_ops = {
3466 .get_drvinfo = vxlan_get_drvinfo,
3467 .get_link = ethtool_op_get_link,
3468 .get_link_ksettings = vxlan_get_link_ksettings,
3469 };
3470
vxlan_create_sock(struct net * net,bool ipv6,__be16 port,u32 flags,int ifindex)3471 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
3472 __be16 port, u32 flags, int ifindex)
3473 {
3474 struct socket *sock;
3475 struct udp_port_cfg udp_conf;
3476 int err;
3477
3478 memset(&udp_conf, 0, sizeof(udp_conf));
3479
3480 if (ipv6) {
3481 udp_conf.family = AF_INET6;
3482 udp_conf.use_udp6_rx_checksums =
3483 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
3484 udp_conf.ipv6_v6only = 1;
3485 } else {
3486 udp_conf.family = AF_INET;
3487 }
3488
3489 udp_conf.local_udp_port = port;
3490 udp_conf.bind_ifindex = ifindex;
3491
3492 /* Open UDP socket */
3493 err = udp_sock_create(net, &udp_conf, &sock);
3494 if (err < 0)
3495 return ERR_PTR(err);
3496
3497 return sock;
3498 }
3499
3500 /* Create new listen socket if needed */
vxlan_socket_create(struct net * net,bool ipv6,__be16 port,u32 flags,int ifindex)3501 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
3502 __be16 port, u32 flags,
3503 int ifindex)
3504 {
3505 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3506 struct vxlan_sock *vs;
3507 struct socket *sock;
3508 unsigned int h;
3509 struct udp_tunnel_sock_cfg tunnel_cfg;
3510
3511 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
3512 if (!vs)
3513 return ERR_PTR(-ENOMEM);
3514
3515 for (h = 0; h < VNI_HASH_SIZE; ++h)
3516 INIT_HLIST_HEAD(&vs->vni_list[h]);
3517
3518 sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
3519 if (IS_ERR(sock)) {
3520 kfree(vs);
3521 return ERR_CAST(sock);
3522 }
3523
3524 vs->sock = sock;
3525 refcount_set(&vs->refcnt, 1);
3526 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
3527
3528 spin_lock(&vn->sock_lock);
3529 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
3530 udp_tunnel_notify_add_rx_port(sock,
3531 (vs->flags & VXLAN_F_GPE) ?
3532 UDP_TUNNEL_TYPE_VXLAN_GPE :
3533 UDP_TUNNEL_TYPE_VXLAN);
3534 spin_unlock(&vn->sock_lock);
3535
3536 /* Mark socket as an encapsulation socket. */
3537 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
3538 tunnel_cfg.sk_user_data = vs;
3539 tunnel_cfg.encap_type = 1;
3540 tunnel_cfg.encap_rcv = vxlan_rcv;
3541 tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
3542 tunnel_cfg.encap_destroy = NULL;
3543 tunnel_cfg.gro_receive = vxlan_gro_receive;
3544 tunnel_cfg.gro_complete = vxlan_gro_complete;
3545
3546 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
3547
3548 return vs;
3549 }
3550
__vxlan_sock_add(struct vxlan_dev * vxlan,bool ipv6)3551 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
3552 {
3553 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
3554 struct vxlan_sock *vs = NULL;
3555 struct vxlan_dev_node *node;
3556 int l3mdev_index = 0;
3557
3558 if (vxlan->cfg.remote_ifindex)
3559 l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3560 vxlan->net, vxlan->cfg.remote_ifindex);
3561
3562 if (!vxlan->cfg.no_share) {
3563 spin_lock(&vn->sock_lock);
3564 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
3565 vxlan->cfg.dst_port, vxlan->cfg.flags,
3566 l3mdev_index);
3567 if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
3568 spin_unlock(&vn->sock_lock);
3569 return -EBUSY;
3570 }
3571 spin_unlock(&vn->sock_lock);
3572 }
3573 if (!vs)
3574 vs = vxlan_socket_create(vxlan->net, ipv6,
3575 vxlan->cfg.dst_port, vxlan->cfg.flags,
3576 l3mdev_index);
3577 if (IS_ERR(vs))
3578 return PTR_ERR(vs);
3579 #if IS_ENABLED(CONFIG_IPV6)
3580 if (ipv6) {
3581 rcu_assign_pointer(vxlan->vn6_sock, vs);
3582 node = &vxlan->hlist6;
3583 } else
3584 #endif
3585 {
3586 rcu_assign_pointer(vxlan->vn4_sock, vs);
3587 node = &vxlan->hlist4;
3588 }
3589 vxlan_vs_add_dev(vs, vxlan, node);
3590 return 0;
3591 }
3592
vxlan_sock_add(struct vxlan_dev * vxlan)3593 static int vxlan_sock_add(struct vxlan_dev *vxlan)
3594 {
3595 bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
3596 bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
3597 bool ipv4 = !ipv6 || metadata;
3598 int ret = 0;
3599
3600 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
3601 #if IS_ENABLED(CONFIG_IPV6)
3602 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
3603 if (ipv6) {
3604 ret = __vxlan_sock_add(vxlan, true);
3605 if (ret < 0 && ret != -EAFNOSUPPORT)
3606 ipv4 = false;
3607 }
3608 #endif
3609 if (ipv4)
3610 ret = __vxlan_sock_add(vxlan, false);
3611 if (ret < 0)
3612 vxlan_sock_release(vxlan);
3613 return ret;
3614 }
3615
vxlan_config_validate(struct net * src_net,struct vxlan_config * conf,struct net_device ** lower,struct vxlan_dev * old,struct netlink_ext_ack * extack)3616 static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3617 struct net_device **lower,
3618 struct vxlan_dev *old,
3619 struct netlink_ext_ack *extack)
3620 {
3621 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
3622 struct vxlan_dev *tmp;
3623 bool use_ipv6 = false;
3624
3625 if (conf->flags & VXLAN_F_GPE) {
3626 /* For now, allow GPE only together with
3627 * COLLECT_METADATA. This can be relaxed later; in such
3628 * case, the other side of the PtP link will have to be
3629 * provided.
3630 */
3631 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3632 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3633 NL_SET_ERR_MSG(extack,
3634 "VXLAN GPE does not support this combination of attributes");
3635 return -EINVAL;
3636 }
3637 }
3638
3639 if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3640 /* Unless IPv6 is explicitly requested, assume IPv4 */
3641 conf->remote_ip.sa.sa_family = AF_INET;
3642 conf->saddr.sa.sa_family = AF_INET;
3643 } else if (!conf->remote_ip.sa.sa_family) {
3644 conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3645 } else if (!conf->saddr.sa.sa_family) {
3646 conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3647 }
3648
3649 if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3650 NL_SET_ERR_MSG(extack,
3651 "Local and remote address must be from the same family");
3652 return -EINVAL;
3653 }
3654
3655 if (vxlan_addr_multicast(&conf->saddr)) {
3656 NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
3657 return -EINVAL;
3658 }
3659
3660 if (conf->saddr.sa.sa_family == AF_INET6) {
3661 if (!IS_ENABLED(CONFIG_IPV6)) {
3662 NL_SET_ERR_MSG(extack,
3663 "IPv6 support not enabled in the kernel");
3664 return -EPFNOSUPPORT;
3665 }
3666 use_ipv6 = true;
3667 conf->flags |= VXLAN_F_IPV6;
3668
3669 if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3670 int local_type =
3671 ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3672 int remote_type =
3673 ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3674
3675 if (local_type & IPV6_ADDR_LINKLOCAL) {
3676 if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
3677 (remote_type != IPV6_ADDR_ANY)) {
3678 NL_SET_ERR_MSG(extack,
3679 "Invalid combination of local and remote address scopes");
3680 return -EINVAL;
3681 }
3682
3683 conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3684 } else {
3685 if (remote_type ==
3686 (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3687 NL_SET_ERR_MSG(extack,
3688 "Invalid combination of local and remote address scopes");
3689 return -EINVAL;
3690 }
3691
3692 conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3693 }
3694 }
3695 }
3696
3697 if (conf->label && !use_ipv6) {
3698 NL_SET_ERR_MSG(extack,
3699 "Label attribute only applies to IPv6 VXLAN devices");
3700 return -EINVAL;
3701 }
3702
3703 if (conf->remote_ifindex) {
3704 struct net_device *lowerdev;
3705
3706 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
3707 if (!lowerdev) {
3708 NL_SET_ERR_MSG(extack,
3709 "Invalid local interface, device not found");
3710 return -ENODEV;
3711 }
3712
3713 #if IS_ENABLED(CONFIG_IPV6)
3714 if (use_ipv6) {
3715 struct inet6_dev *idev = __in6_dev_get(lowerdev);
3716 if (idev && idev->cnf.disable_ipv6) {
3717 NL_SET_ERR_MSG(extack,
3718 "IPv6 support disabled by administrator");
3719 return -EPERM;
3720 }
3721 }
3722 #endif
3723
3724 *lower = lowerdev;
3725 } else {
3726 if (vxlan_addr_multicast(&conf->remote_ip)) {
3727 NL_SET_ERR_MSG(extack,
3728 "Local interface required for multicast remote destination");
3729
3730 return -EINVAL;
3731 }
3732
3733 #if IS_ENABLED(CONFIG_IPV6)
3734 if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3735 NL_SET_ERR_MSG(extack,
3736 "Local interface required for link-local local/remote addresses");
3737 return -EINVAL;
3738 }
3739 #endif
3740
3741 *lower = NULL;
3742 }
3743
3744 if (!conf->dst_port) {
3745 if (conf->flags & VXLAN_F_GPE)
3746 conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
3747 else
3748 conf->dst_port = htons(vxlan_port);
3749 }
3750
3751 if (!conf->age_interval)
3752 conf->age_interval = FDB_AGE_DEFAULT;
3753
3754 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3755 if (tmp == old)
3756 continue;
3757
3758 if (tmp->cfg.vni != conf->vni)
3759 continue;
3760 if (tmp->cfg.dst_port != conf->dst_port)
3761 continue;
3762 if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3763 (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3764 continue;
3765
3766 if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3767 tmp->cfg.remote_ifindex != conf->remote_ifindex)
3768 continue;
3769
3770 NL_SET_ERR_MSG(extack,
3771 "A VXLAN device with the specified VNI already exists");
3772 return -EEXIST;
3773 }
3774
3775 return 0;
3776 }
3777
vxlan_config_apply(struct net_device * dev,struct vxlan_config * conf,struct net_device * lowerdev,struct net * src_net,bool changelink)3778 static void vxlan_config_apply(struct net_device *dev,
3779 struct vxlan_config *conf,
3780 struct net_device *lowerdev,
3781 struct net *src_net,
3782 bool changelink)
3783 {
3784 struct vxlan_dev *vxlan = netdev_priv(dev);
3785 struct vxlan_rdst *dst = &vxlan->default_dst;
3786 unsigned short needed_headroom = ETH_HLEN;
3787 bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
3788 int max_mtu = ETH_MAX_MTU;
3789
3790 if (!changelink) {
3791 if (conf->flags & VXLAN_F_GPE)
3792 vxlan_raw_setup(dev);
3793 else
3794 vxlan_ether_setup(dev);
3795
3796 if (conf->mtu)
3797 dev->mtu = conf->mtu;
3798
3799 vxlan->net = src_net;
3800 }
3801
3802 dst->remote_vni = conf->vni;
3803
3804 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
3805
3806 if (lowerdev) {
3807 dst->remote_ifindex = conf->remote_ifindex;
3808
3809 dev->gso_max_size = lowerdev->gso_max_size;
3810 dev->gso_max_segs = lowerdev->gso_max_segs;
3811
3812 needed_headroom = lowerdev->hard_header_len;
3813 needed_headroom += lowerdev->needed_headroom;
3814
3815 dev->needed_tailroom = lowerdev->needed_tailroom;
3816
3817 max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
3818 VXLAN_HEADROOM);
3819 if (max_mtu < ETH_MIN_MTU)
3820 max_mtu = ETH_MIN_MTU;
3821
3822 if (!changelink && !conf->mtu)
3823 dev->mtu = max_mtu;
3824 }
3825
3826 if (dev->mtu > max_mtu)
3827 dev->mtu = max_mtu;
3828
3829 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
3830 needed_headroom += VXLAN6_HEADROOM;
3831 else
3832 needed_headroom += VXLAN_HEADROOM;
3833 dev->needed_headroom = needed_headroom;
3834
3835 memcpy(&vxlan->cfg, conf, sizeof(*conf));
3836 }
3837
vxlan_dev_configure(struct net * src_net,struct net_device * dev,struct vxlan_config * conf,bool changelink,struct netlink_ext_ack * extack)3838 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
3839 struct vxlan_config *conf, bool changelink,
3840 struct netlink_ext_ack *extack)
3841 {
3842 struct vxlan_dev *vxlan = netdev_priv(dev);
3843 struct net_device *lowerdev;
3844 int ret;
3845
3846 ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
3847 if (ret)
3848 return ret;
3849
3850 vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
3851
3852 return 0;
3853 }
3854
__vxlan_dev_create(struct net * net,struct net_device * dev,struct vxlan_config * conf,struct netlink_ext_ack * extack)3855 static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3856 struct vxlan_config *conf,
3857 struct netlink_ext_ack *extack)
3858 {
3859 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3860 struct vxlan_dev *vxlan = netdev_priv(dev);
3861 struct net_device *remote_dev = NULL;
3862 struct vxlan_fdb *f = NULL;
3863 bool unregister = false;
3864 struct vxlan_rdst *dst;
3865 int err;
3866
3867 dst = &vxlan->default_dst;
3868 err = vxlan_dev_configure(net, dev, conf, false, extack);
3869 if (err)
3870 return err;
3871
3872 dev->ethtool_ops = &vxlan_ethtool_ops;
3873
3874 /* create an fdb entry for a valid default destination */
3875 if (!vxlan_addr_any(&dst->remote_ip)) {
3876 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3877 &dst->remote_ip,
3878 NUD_REACHABLE | NUD_PERMANENT,
3879 vxlan->cfg.dst_port,
3880 dst->remote_vni,
3881 dst->remote_vni,
3882 dst->remote_ifindex,
3883 NTF_SELF, 0, &f, extack);
3884 if (err)
3885 return err;
3886 }
3887
3888 err = register_netdevice(dev);
3889 if (err)
3890 goto errout;
3891 unregister = true;
3892
3893 if (dst->remote_ifindex) {
3894 remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
3895 if (!remote_dev) {
3896 err = -ENODEV;
3897 goto errout;
3898 }
3899
3900 err = netdev_upper_dev_link(remote_dev, dev, extack);
3901 if (err)
3902 goto errout;
3903 }
3904
3905 err = rtnl_configure_link(dev, NULL);
3906 if (err < 0)
3907 goto unlink;
3908
3909 if (f) {
3910 vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
3911
3912 /* notify default fdb entry */
3913 err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
3914 RTM_NEWNEIGH, true, extack);
3915 if (err) {
3916 vxlan_fdb_destroy(vxlan, f, false, false);
3917 if (remote_dev)
3918 netdev_upper_dev_unlink(remote_dev, dev);
3919 goto unregister;
3920 }
3921 }
3922
3923 list_add(&vxlan->next, &vn->vxlan_list);
3924 if (remote_dev)
3925 dst->remote_dev = remote_dev;
3926 return 0;
3927 unlink:
3928 if (remote_dev)
3929 netdev_upper_dev_unlink(remote_dev, dev);
3930 errout:
3931 /* unregister_netdevice() destroys the default FDB entry with deletion
3932 * notification. But the addition notification was not sent yet, so
3933 * destroy the entry by hand here.
3934 */
3935 if (f)
3936 __vxlan_fdb_free(f);
3937 unregister:
3938 if (unregister)
3939 unregister_netdevice(dev);
3940 return err;
3941 }
3942
3943 /* Set/clear flags based on attribute */
vxlan_nl2flag(struct vxlan_config * conf,struct nlattr * tb[],int attrtype,unsigned long mask,bool changelink,bool changelink_supported,struct netlink_ext_ack * extack)3944 static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
3945 int attrtype, unsigned long mask, bool changelink,
3946 bool changelink_supported,
3947 struct netlink_ext_ack *extack)
3948 {
3949 unsigned long flags;
3950
3951 if (!tb[attrtype])
3952 return 0;
3953
3954 if (changelink && !changelink_supported) {
3955 vxlan_flag_attr_error(attrtype, extack);
3956 return -EOPNOTSUPP;
3957 }
3958
3959 if (vxlan_policy[attrtype].type == NLA_FLAG)
3960 flags = conf->flags | mask;
3961 else if (nla_get_u8(tb[attrtype]))
3962 flags = conf->flags | mask;
3963 else
3964 flags = conf->flags & ~mask;
3965
3966 conf->flags = flags;
3967
3968 return 0;
3969 }
3970
vxlan_nl2conf(struct nlattr * tb[],struct nlattr * data[],struct net_device * dev,struct vxlan_config * conf,bool changelink,struct netlink_ext_ack * extack)3971 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3972 struct net_device *dev, struct vxlan_config *conf,
3973 bool changelink, struct netlink_ext_ack *extack)
3974 {
3975 struct vxlan_dev *vxlan = netdev_priv(dev);
3976 int err = 0;
3977
3978 memset(conf, 0, sizeof(*conf));
3979
3980 /* if changelink operation, start with old existing cfg */
3981 if (changelink)
3982 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3983
3984 if (data[IFLA_VXLAN_ID]) {
3985 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3986
3987 if (changelink && (vni != conf->vni)) {
3988 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
3989 return -EOPNOTSUPP;
3990 }
3991 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3992 }
3993
3994 if (data[IFLA_VXLAN_GROUP]) {
3995 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
3996 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
3997 return -EOPNOTSUPP;
3998 }
3999
4000 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
4001 conf->remote_ip.sa.sa_family = AF_INET;
4002 } else if (data[IFLA_VXLAN_GROUP6]) {
4003 if (!IS_ENABLED(CONFIG_IPV6)) {
4004 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
4005 return -EPFNOSUPPORT;
4006 }
4007
4008 if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
4009 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
4010 return -EOPNOTSUPP;
4011 }
4012
4013 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
4014 conf->remote_ip.sa.sa_family = AF_INET6;
4015 }
4016
4017 if (data[IFLA_VXLAN_LOCAL]) {
4018 if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
4019 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
4020 return -EOPNOTSUPP;
4021 }
4022
4023 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
4024 conf->saddr.sa.sa_family = AF_INET;
4025 } else if (data[IFLA_VXLAN_LOCAL6]) {
4026 if (!IS_ENABLED(CONFIG_IPV6)) {
4027 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
4028 return -EPFNOSUPPORT;
4029 }
4030
4031 if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
4032 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
4033 return -EOPNOTSUPP;
4034 }
4035
4036 /* TODO: respect scope id */
4037 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
4038 conf->saddr.sa.sa_family = AF_INET6;
4039 }
4040
4041 if (data[IFLA_VXLAN_LINK])
4042 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
4043
4044 if (data[IFLA_VXLAN_TOS])
4045 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
4046
4047 if (data[IFLA_VXLAN_TTL])
4048 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
4049
4050 if (data[IFLA_VXLAN_TTL_INHERIT]) {
4051 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
4052 VXLAN_F_TTL_INHERIT, changelink, false,
4053 extack);
4054 if (err)
4055 return err;
4056
4057 }
4058
4059 if (data[IFLA_VXLAN_LABEL])
4060 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
4061 IPV6_FLOWLABEL_MASK;
4062
4063 if (data[IFLA_VXLAN_LEARNING]) {
4064 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
4065 VXLAN_F_LEARN, changelink, true,
4066 extack);
4067 if (err)
4068 return err;
4069 } else if (!changelink) {
4070 /* default to learn on a new device */
4071 conf->flags |= VXLAN_F_LEARN;
4072 }
4073
4074 if (data[IFLA_VXLAN_AGEING])
4075 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
4076
4077 if (data[IFLA_VXLAN_PROXY]) {
4078 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
4079 VXLAN_F_PROXY, changelink, false,
4080 extack);
4081 if (err)
4082 return err;
4083 }
4084
4085 if (data[IFLA_VXLAN_RSC]) {
4086 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
4087 VXLAN_F_RSC, changelink, false,
4088 extack);
4089 if (err)
4090 return err;
4091 }
4092
4093 if (data[IFLA_VXLAN_L2MISS]) {
4094 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
4095 VXLAN_F_L2MISS, changelink, false,
4096 extack);
4097 if (err)
4098 return err;
4099 }
4100
4101 if (data[IFLA_VXLAN_L3MISS]) {
4102 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
4103 VXLAN_F_L3MISS, changelink, false,
4104 extack);
4105 if (err)
4106 return err;
4107 }
4108
4109 if (data[IFLA_VXLAN_LIMIT]) {
4110 if (changelink) {
4111 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
4112 "Cannot change limit");
4113 return -EOPNOTSUPP;
4114 }
4115 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
4116 }
4117
4118 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
4119 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
4120 VXLAN_F_COLLECT_METADATA, changelink, false,
4121 extack);
4122 if (err)
4123 return err;
4124 }
4125
4126 if (data[IFLA_VXLAN_PORT_RANGE]) {
4127 if (!changelink) {
4128 const struct ifla_vxlan_port_range *p
4129 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
4130 conf->port_min = ntohs(p->low);
4131 conf->port_max = ntohs(p->high);
4132 } else {
4133 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
4134 "Cannot change port range");
4135 return -EOPNOTSUPP;
4136 }
4137 }
4138
4139 if (data[IFLA_VXLAN_PORT]) {
4140 if (changelink) {
4141 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
4142 "Cannot change port");
4143 return -EOPNOTSUPP;
4144 }
4145 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
4146 }
4147
4148 if (data[IFLA_VXLAN_UDP_CSUM]) {
4149 if (changelink) {
4150 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
4151 "Cannot change UDP_CSUM flag");
4152 return -EOPNOTSUPP;
4153 }
4154 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
4155 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
4156 }
4157
4158 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
4159 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4160 VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
4161 false, extack);
4162 if (err)
4163 return err;
4164 }
4165
4166 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
4167 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4168 VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
4169 false, extack);
4170 if (err)
4171 return err;
4172 }
4173
4174 if (data[IFLA_VXLAN_REMCSUM_TX]) {
4175 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
4176 VXLAN_F_REMCSUM_TX, changelink, false,
4177 extack);
4178 if (err)
4179 return err;
4180 }
4181
4182 if (data[IFLA_VXLAN_REMCSUM_RX]) {
4183 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
4184 VXLAN_F_REMCSUM_RX, changelink, false,
4185 extack);
4186 if (err)
4187 return err;
4188 }
4189
4190 if (data[IFLA_VXLAN_GBP]) {
4191 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
4192 VXLAN_F_GBP, changelink, false, extack);
4193 if (err)
4194 return err;
4195 }
4196
4197 if (data[IFLA_VXLAN_GPE]) {
4198 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
4199 VXLAN_F_GPE, changelink, false,
4200 extack);
4201 if (err)
4202 return err;
4203 }
4204
4205 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
4206 err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
4207 VXLAN_F_REMCSUM_NOPARTIAL, changelink,
4208 false, extack);
4209 if (err)
4210 return err;
4211 }
4212
4213 if (tb[IFLA_MTU]) {
4214 if (changelink) {
4215 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
4216 "Cannot change mtu");
4217 return -EOPNOTSUPP;
4218 }
4219 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
4220 }
4221
4222 if (data[IFLA_VXLAN_DF])
4223 conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
4224
4225 return 0;
4226 }
4227
vxlan_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)4228 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
4229 struct nlattr *tb[], struct nlattr *data[],
4230 struct netlink_ext_ack *extack)
4231 {
4232 struct vxlan_config conf;
4233 int err;
4234
4235 err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
4236 if (err)
4237 return err;
4238
4239 return __vxlan_dev_create(src_net, dev, &conf, extack);
4240 }
4241
vxlan_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)4242 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
4243 struct nlattr *data[],
4244 struct netlink_ext_ack *extack)
4245 {
4246 struct vxlan_dev *vxlan = netdev_priv(dev);
4247 struct net_device *lowerdev;
4248 struct vxlan_config conf;
4249 struct vxlan_rdst *dst;
4250 int err;
4251
4252 dst = &vxlan->default_dst;
4253 err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
4254 if (err)
4255 return err;
4256
4257 err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
4258 vxlan, extack);
4259 if (err)
4260 return err;
4261
4262 if (dst->remote_dev == lowerdev)
4263 lowerdev = NULL;
4264
4265 err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
4266 extack);
4267 if (err)
4268 return err;
4269
4270 /* handle default dst entry */
4271 if (!vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip)) {
4272 u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
4273
4274 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4275 if (!vxlan_addr_any(&conf.remote_ip)) {
4276 err = vxlan_fdb_update(vxlan, all_zeros_mac,
4277 &conf.remote_ip,
4278 NUD_REACHABLE | NUD_PERMANENT,
4279 NLM_F_APPEND | NLM_F_CREATE,
4280 vxlan->cfg.dst_port,
4281 conf.vni, conf.vni,
4282 conf.remote_ifindex,
4283 NTF_SELF, 0, true, extack);
4284 if (err) {
4285 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4286 netdev_adjacent_change_abort(dst->remote_dev,
4287 lowerdev, dev);
4288 return err;
4289 }
4290 }
4291 if (!vxlan_addr_any(&dst->remote_ip))
4292 __vxlan_fdb_delete(vxlan, all_zeros_mac,
4293 dst->remote_ip,
4294 vxlan->cfg.dst_port,
4295 dst->remote_vni,
4296 dst->remote_vni,
4297 dst->remote_ifindex,
4298 true);
4299 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4300 }
4301
4302 if (conf.age_interval != vxlan->cfg.age_interval)
4303 mod_timer(&vxlan->age_timer, jiffies);
4304
4305 netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
4306 if (lowerdev && lowerdev != dst->remote_dev)
4307 dst->remote_dev = lowerdev;
4308 vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
4309 return 0;
4310 }
4311
vxlan_dellink(struct net_device * dev,struct list_head * head)4312 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4313 {
4314 struct vxlan_dev *vxlan = netdev_priv(dev);
4315
4316 vxlan_flush(vxlan, true);
4317
4318 list_del(&vxlan->next);
4319 unregister_netdevice_queue(dev, head);
4320 if (vxlan->default_dst.remote_dev)
4321 netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
4322 }
4323
vxlan_get_size(const struct net_device * dev)4324 static size_t vxlan_get_size(const struct net_device *dev)
4325 {
4326
4327 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
4328 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
4329 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
4330 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
4331 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
4332 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
4333 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
4334 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
4335 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
4336 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
4337 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
4338 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
4339 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
4340 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
4341 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
4342 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
4343 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
4344 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
4345 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4346 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4347 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4348 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
4349 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4350 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
4351 0;
4352 }
4353
vxlan_fill_info(struct sk_buff * skb,const struct net_device * dev)4354 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4355 {
4356 const struct vxlan_dev *vxlan = netdev_priv(dev);
4357 const struct vxlan_rdst *dst = &vxlan->default_dst;
4358 struct ifla_vxlan_port_range ports = {
4359 .low = htons(vxlan->cfg.port_min),
4360 .high = htons(vxlan->cfg.port_max),
4361 };
4362
4363 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
4364 goto nla_put_failure;
4365
4366 if (!vxlan_addr_any(&dst->remote_ip)) {
4367 if (dst->remote_ip.sa.sa_family == AF_INET) {
4368 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4369 dst->remote_ip.sin.sin_addr.s_addr))
4370 goto nla_put_failure;
4371 #if IS_ENABLED(CONFIG_IPV6)
4372 } else {
4373 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4374 &dst->remote_ip.sin6.sin6_addr))
4375 goto nla_put_failure;
4376 #endif
4377 }
4378 }
4379
4380 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
4381 goto nla_put_failure;
4382
4383 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
4384 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
4385 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
4386 vxlan->cfg.saddr.sin.sin_addr.s_addr))
4387 goto nla_put_failure;
4388 #if IS_ENABLED(CONFIG_IPV6)
4389 } else {
4390 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
4391 &vxlan->cfg.saddr.sin6.sin6_addr))
4392 goto nla_put_failure;
4393 #endif
4394 }
4395 }
4396
4397 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
4398 nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4399 !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
4400 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
4401 nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
4402 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
4403 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
4404 !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
4405 nla_put_u8(skb, IFLA_VXLAN_PROXY,
4406 !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
4407 nla_put_u8(skb, IFLA_VXLAN_RSC,
4408 !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
4409 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
4410 !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
4411 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
4412 !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
4413 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
4414 !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
4415 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
4416 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
4417 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
4418 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
4419 !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
4420 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4421 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
4422 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4423 !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
4424 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
4425 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
4426 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
4427 !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
4428 goto nla_put_failure;
4429
4430 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4431 goto nla_put_failure;
4432
4433 if (vxlan->cfg.flags & VXLAN_F_GBP &&
4434 nla_put_flag(skb, IFLA_VXLAN_GBP))
4435 goto nla_put_failure;
4436
4437 if (vxlan->cfg.flags & VXLAN_F_GPE &&
4438 nla_put_flag(skb, IFLA_VXLAN_GPE))
4439 goto nla_put_failure;
4440
4441 if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
4442 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4443 goto nla_put_failure;
4444
4445 return 0;
4446
4447 nla_put_failure:
4448 return -EMSGSIZE;
4449 }
4450
vxlan_get_link_net(const struct net_device * dev)4451 static struct net *vxlan_get_link_net(const struct net_device *dev)
4452 {
4453 struct vxlan_dev *vxlan = netdev_priv(dev);
4454
4455 return vxlan->net;
4456 }
4457
4458 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4459 .kind = "vxlan",
4460 .maxtype = IFLA_VXLAN_MAX,
4461 .policy = vxlan_policy,
4462 .priv_size = sizeof(struct vxlan_dev),
4463 .setup = vxlan_setup,
4464 .validate = vxlan_validate,
4465 .newlink = vxlan_newlink,
4466 .changelink = vxlan_changelink,
4467 .dellink = vxlan_dellink,
4468 .get_size = vxlan_get_size,
4469 .fill_info = vxlan_fill_info,
4470 .get_link_net = vxlan_get_link_net,
4471 };
4472
vxlan_dev_create(struct net * net,const char * name,u8 name_assign_type,struct vxlan_config * conf)4473 struct net_device *vxlan_dev_create(struct net *net, const char *name,
4474 u8 name_assign_type,
4475 struct vxlan_config *conf)
4476 {
4477 struct nlattr *tb[IFLA_MAX + 1];
4478 struct net_device *dev;
4479 int err;
4480
4481 memset(&tb, 0, sizeof(tb));
4482
4483 dev = rtnl_create_link(net, name, name_assign_type,
4484 &vxlan_link_ops, tb, NULL);
4485 if (IS_ERR(dev))
4486 return dev;
4487
4488 err = __vxlan_dev_create(net, dev, conf, NULL);
4489 if (err < 0) {
4490 free_netdev(dev);
4491 return ERR_PTR(err);
4492 }
4493
4494 err = rtnl_configure_link(dev, NULL);
4495 if (err < 0) {
4496 LIST_HEAD(list_kill);
4497
4498 vxlan_dellink(dev, &list_kill);
4499 unregister_netdevice_many(&list_kill);
4500 return ERR_PTR(err);
4501 }
4502
4503 return dev;
4504 }
4505 EXPORT_SYMBOL_GPL(vxlan_dev_create);
4506
vxlan_handle_lowerdev_unregister(struct vxlan_net * vn,struct net_device * dev)4507 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4508 struct net_device *dev)
4509 {
4510 struct vxlan_dev *vxlan, *next;
4511 LIST_HEAD(list_kill);
4512
4513 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4514 struct vxlan_rdst *dst = &vxlan->default_dst;
4515
4516 /* In case we created vxlan device with carrier
4517 * and we loose the carrier due to module unload
4518 * we also need to remove vxlan device. In other
4519 * cases, it's not necessary and remote_ifindex
4520 * is 0 here, so no matches.
4521 */
4522 if (dst->remote_ifindex == dev->ifindex)
4523 vxlan_dellink(vxlan->dev, &list_kill);
4524 }
4525
4526 unregister_netdevice_many(&list_kill);
4527 }
4528
vxlan_netdevice_event(struct notifier_block * unused,unsigned long event,void * ptr)4529 static int vxlan_netdevice_event(struct notifier_block *unused,
4530 unsigned long event, void *ptr)
4531 {
4532 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4533 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
4534
4535 if (event == NETDEV_UNREGISTER) {
4536 if (!dev->udp_tunnel_nic_info)
4537 vxlan_offload_rx_ports(dev, false);
4538 vxlan_handle_lowerdev_unregister(vn, dev);
4539 } else if (event == NETDEV_REGISTER) {
4540 if (!dev->udp_tunnel_nic_info)
4541 vxlan_offload_rx_ports(dev, true);
4542 } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
4543 event == NETDEV_UDP_TUNNEL_DROP_INFO) {
4544 vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
4545 }
4546
4547 return NOTIFY_DONE;
4548 }
4549
4550 static struct notifier_block vxlan_notifier_block __read_mostly = {
4551 .notifier_call = vxlan_netdevice_event,
4552 };
4553
4554 static void
vxlan_fdb_offloaded_set(struct net_device * dev,struct switchdev_notifier_vxlan_fdb_info * fdb_info)4555 vxlan_fdb_offloaded_set(struct net_device *dev,
4556 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4557 {
4558 struct vxlan_dev *vxlan = netdev_priv(dev);
4559 struct vxlan_rdst *rdst;
4560 struct vxlan_fdb *f;
4561 u32 hash_index;
4562
4563 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4564
4565 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4566
4567 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4568 if (!f)
4569 goto out;
4570
4571 rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4572 fdb_info->remote_port,
4573 fdb_info->remote_vni,
4574 fdb_info->remote_ifindex);
4575 if (!rdst)
4576 goto out;
4577
4578 rdst->offloaded = fdb_info->offloaded;
4579
4580 out:
4581 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4582 }
4583
4584 static int
vxlan_fdb_external_learn_add(struct net_device * dev,struct switchdev_notifier_vxlan_fdb_info * fdb_info)4585 vxlan_fdb_external_learn_add(struct net_device *dev,
4586 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4587 {
4588 struct vxlan_dev *vxlan = netdev_priv(dev);
4589 struct netlink_ext_ack *extack;
4590 u32 hash_index;
4591 int err;
4592
4593 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4594 extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4595
4596 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4597 err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4598 NUD_REACHABLE,
4599 NLM_F_CREATE | NLM_F_REPLACE,
4600 fdb_info->remote_port,
4601 fdb_info->vni,
4602 fdb_info->remote_vni,
4603 fdb_info->remote_ifindex,
4604 NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
4605 0, false, extack);
4606 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4607
4608 return err;
4609 }
4610
4611 static int
vxlan_fdb_external_learn_del(struct net_device * dev,struct switchdev_notifier_vxlan_fdb_info * fdb_info)4612 vxlan_fdb_external_learn_del(struct net_device *dev,
4613 struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4614 {
4615 struct vxlan_dev *vxlan = netdev_priv(dev);
4616 struct vxlan_fdb *f;
4617 u32 hash_index;
4618 int err = 0;
4619
4620 hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
4621 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4622
4623 f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4624 if (!f)
4625 err = -ENOENT;
4626 else if (f->flags & NTF_EXT_LEARNED)
4627 err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4628 fdb_info->remote_ip,
4629 fdb_info->remote_port,
4630 fdb_info->vni,
4631 fdb_info->remote_vni,
4632 fdb_info->remote_ifindex,
4633 false);
4634
4635 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4636
4637 return err;
4638 }
4639
vxlan_switchdev_event(struct notifier_block * unused,unsigned long event,void * ptr)4640 static int vxlan_switchdev_event(struct notifier_block *unused,
4641 unsigned long event, void *ptr)
4642 {
4643 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
4644 struct switchdev_notifier_vxlan_fdb_info *fdb_info;
4645 int err = 0;
4646
4647 switch (event) {
4648 case SWITCHDEV_VXLAN_FDB_OFFLOADED:
4649 vxlan_fdb_offloaded_set(dev, ptr);
4650 break;
4651 case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
4652 fdb_info = ptr;
4653 err = vxlan_fdb_external_learn_add(dev, fdb_info);
4654 if (err) {
4655 err = notifier_from_errno(err);
4656 break;
4657 }
4658 fdb_info->offloaded = true;
4659 vxlan_fdb_offloaded_set(dev, fdb_info);
4660 break;
4661 case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
4662 fdb_info = ptr;
4663 err = vxlan_fdb_external_learn_del(dev, fdb_info);
4664 if (err) {
4665 err = notifier_from_errno(err);
4666 break;
4667 }
4668 fdb_info->offloaded = false;
4669 vxlan_fdb_offloaded_set(dev, fdb_info);
4670 break;
4671 }
4672
4673 return err;
4674 }
4675
4676 static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
4677 .notifier_call = vxlan_switchdev_event,
4678 };
4679
vxlan_fdb_nh_flush(struct nexthop * nh)4680 static void vxlan_fdb_nh_flush(struct nexthop *nh)
4681 {
4682 struct vxlan_fdb *fdb;
4683 struct vxlan_dev *vxlan;
4684 u32 hash_index;
4685
4686 rcu_read_lock();
4687 list_for_each_entry_rcu(fdb, &nh->fdb_list, nh_list) {
4688 vxlan = rcu_dereference(fdb->vdev);
4689 WARN_ON(!vxlan);
4690 hash_index = fdb_head_index(vxlan, fdb->eth_addr,
4691 vxlan->default_dst.remote_vni);
4692 spin_lock_bh(&vxlan->hash_lock[hash_index]);
4693 if (!hlist_unhashed(&fdb->hlist))
4694 vxlan_fdb_destroy(vxlan, fdb, false, false);
4695 spin_unlock_bh(&vxlan->hash_lock[hash_index]);
4696 }
4697 rcu_read_unlock();
4698 }
4699
vxlan_nexthop_event(struct notifier_block * nb,unsigned long event,void * ptr)4700 static int vxlan_nexthop_event(struct notifier_block *nb,
4701 unsigned long event, void *ptr)
4702 {
4703 struct nexthop *nh = ptr;
4704
4705 if (!nh || event != NEXTHOP_EVENT_DEL)
4706 return NOTIFY_DONE;
4707
4708 vxlan_fdb_nh_flush(nh);
4709
4710 return NOTIFY_DONE;
4711 }
4712
4713 static struct notifier_block vxlan_nexthop_notifier_block __read_mostly = {
4714 .notifier_call = vxlan_nexthop_event,
4715 };
4716
vxlan_init_net(struct net * net)4717 static __net_init int vxlan_init_net(struct net *net)
4718 {
4719 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4720 unsigned int h;
4721
4722 INIT_LIST_HEAD(&vn->vxlan_list);
4723 spin_lock_init(&vn->sock_lock);
4724
4725 for (h = 0; h < PORT_HASH_SIZE; ++h)
4726 INIT_HLIST_HEAD(&vn->sock_list[h]);
4727
4728 return register_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
4729 }
4730
vxlan_destroy_tunnels(struct net * net,struct list_head * head)4731 static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
4732 {
4733 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4734 struct vxlan_dev *vxlan, *next;
4735 struct net_device *dev, *aux;
4736
4737 for_each_netdev_safe(net, dev, aux)
4738 if (dev->rtnl_link_ops == &vxlan_link_ops)
4739 unregister_netdevice_queue(dev, head);
4740
4741 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4742 /* If vxlan->dev is in the same netns, it has already been added
4743 * to the list by the previous loop.
4744 */
4745 if (!net_eq(dev_net(vxlan->dev), net))
4746 unregister_netdevice_queue(vxlan->dev, head);
4747 }
4748
4749 }
4750
vxlan_exit_batch_net(struct list_head * net_list)4751 static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
4752 {
4753 struct net *net;
4754 LIST_HEAD(list);
4755 unsigned int h;
4756
4757 rtnl_lock();
4758 list_for_each_entry(net, net_list, exit_list)
4759 unregister_nexthop_notifier(net, &vxlan_nexthop_notifier_block);
4760 list_for_each_entry(net, net_list, exit_list)
4761 vxlan_destroy_tunnels(net, &list);
4762
4763 unregister_netdevice_many(&list);
4764 rtnl_unlock();
4765
4766 list_for_each_entry(net, net_list, exit_list) {
4767 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4768
4769 for (h = 0; h < PORT_HASH_SIZE; ++h)
4770 WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
4771 }
4772 }
4773
4774 static struct pernet_operations vxlan_net_ops = {
4775 .init = vxlan_init_net,
4776 .exit_batch = vxlan_exit_batch_net,
4777 .id = &vxlan_net_id,
4778 .size = sizeof(struct vxlan_net),
4779 };
4780
vxlan_init_module(void)4781 static int __init vxlan_init_module(void)
4782 {
4783 int rc;
4784
4785 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
4786
4787 rc = register_pernet_subsys(&vxlan_net_ops);
4788 if (rc)
4789 goto out1;
4790
4791 rc = register_netdevice_notifier(&vxlan_notifier_block);
4792 if (rc)
4793 goto out2;
4794
4795 rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
4796 if (rc)
4797 goto out3;
4798
4799 rc = rtnl_link_register(&vxlan_link_ops);
4800 if (rc)
4801 goto out4;
4802
4803 return 0;
4804 out4:
4805 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4806 out3:
4807 unregister_netdevice_notifier(&vxlan_notifier_block);
4808 out2:
4809 unregister_pernet_subsys(&vxlan_net_ops);
4810 out1:
4811 return rc;
4812 }
4813 late_initcall(vxlan_init_module);
4814
vxlan_cleanup_module(void)4815 static void __exit vxlan_cleanup_module(void)
4816 {
4817 rtnl_link_unregister(&vxlan_link_ops);
4818 unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
4819 unregister_netdevice_notifier(&vxlan_notifier_block);
4820 unregister_pernet_subsys(&vxlan_net_ops);
4821 /* rcu_barrier() is called by netns */
4822 }
4823 module_exit(vxlan_cleanup_module);
4824
4825 MODULE_LICENSE("GPL");
4826 MODULE_VERSION(VXLAN_VERSION);
4827 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
4828 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
4829 MODULE_ALIAS_RTNL_LINK("vxlan");
4830