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