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