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