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