ACPI / SBS: Add 5 us delay to fix SBS hangs on MacBook
[linux-2.6-block.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/types.h>
15 #include <linux/module.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/rculist.h>
20 #include <linux/netdevice.h>
21 #include <linux/in.h>
22 #include <linux/ip.h>
23 #include <linux/udp.h>
24 #include <linux/igmp.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/hash.h>
29 #include <linux/ethtool.h>
30 #include <net/arp.h>
31 #include <net/ndisc.h>
32 #include <net/ip.h>
33 #include <net/ip_tunnels.h>
34 #include <net/icmp.h>
35 #include <net/udp.h>
36 #include <net/udp_tunnel.h>
37 #include <net/rtnetlink.h>
38 #include <net/route.h>
39 #include <net/dsfield.h>
40 #include <net/inet_ecn.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <net/vxlan.h>
44 #include <net/protocol.h>
45 #include <net/udp_tunnel.h>
46 #if IS_ENABLED(CONFIG_IPV6)
47 #include <net/ipv6.h>
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
50 #include <net/ip6_checksum.h>
51 #endif
52
53 #define VXLAN_VERSION   "0.1"
54
55 #define PORT_HASH_BITS  8
56 #define PORT_HASH_SIZE  (1<<PORT_HASH_BITS)
57 #define VNI_HASH_BITS   10
58 #define VNI_HASH_SIZE   (1<<VNI_HASH_BITS)
59 #define FDB_HASH_BITS   8
60 #define FDB_HASH_SIZE   (1<<FDB_HASH_BITS)
61 #define FDB_AGE_DEFAULT 300 /* 5 min */
62 #define FDB_AGE_INTERVAL (10 * HZ)      /* rescan interval */
63
64 /* UDP port for VXLAN traffic.
65  * The IANA assigned port is 4789, but the Linux default is 8472
66  * for compatibility with early adopters.
67  */
68 static unsigned short vxlan_port __read_mostly = 8472;
69 module_param_named(udp_port, vxlan_port, ushort, 0444);
70 MODULE_PARM_DESC(udp_port, "Destination UDP port");
71
72 static bool log_ecn_error = true;
73 module_param(log_ecn_error, bool, 0644);
74 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
75
76 static int vxlan_net_id;
77
78 static const u8 all_zeros_mac[ETH_ALEN];
79
80 /* per-network namespace private data for this module */
81 struct vxlan_net {
82         struct list_head  vxlan_list;
83         struct hlist_head sock_list[PORT_HASH_SIZE];
84         spinlock_t        sock_lock;
85 };
86
87 union vxlan_addr {
88         struct sockaddr_in sin;
89         struct sockaddr_in6 sin6;
90         struct sockaddr sa;
91 };
92
93 struct vxlan_rdst {
94         union vxlan_addr         remote_ip;
95         __be16                   remote_port;
96         u32                      remote_vni;
97         u32                      remote_ifindex;
98         struct list_head         list;
99         struct rcu_head          rcu;
100 };
101
102 /* Forwarding table entry */
103 struct vxlan_fdb {
104         struct hlist_node hlist;        /* linked list of entries */
105         struct rcu_head   rcu;
106         unsigned long     updated;      /* jiffies */
107         unsigned long     used;
108         struct list_head  remotes;
109         u16               state;        /* see ndm_state */
110         u8                flags;        /* see ndm_flags */
111         u8                eth_addr[ETH_ALEN];
112 };
113
114 /* Pseudo network device */
115 struct vxlan_dev {
116         struct hlist_node hlist;        /* vni hash table */
117         struct list_head  next;         /* vxlan's per namespace list */
118         struct vxlan_sock *vn_sock;     /* listening socket */
119         struct net_device *dev;
120         struct net        *net;         /* netns for packet i/o */
121         struct vxlan_rdst default_dst;  /* default destination */
122         union vxlan_addr  saddr;        /* source address */
123         __be16            dst_port;
124         __u16             port_min;     /* source port range */
125         __u16             port_max;
126         __u8              tos;          /* TOS override */
127         __u8              ttl;
128         u32               flags;        /* VXLAN_F_* in vxlan.h */
129
130         unsigned long     age_interval;
131         struct timer_list age_timer;
132         spinlock_t        hash_lock;
133         unsigned int      addrcnt;
134         unsigned int      addrmax;
135
136         struct hlist_head fdb_head[FDB_HASH_SIZE];
137 };
138
139 /* salt for hash table */
140 static u32 vxlan_salt __read_mostly;
141 static struct workqueue_struct *vxlan_wq;
142
143 #if IS_ENABLED(CONFIG_IPV6)
144 static inline
145 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
146 {
147         if (a->sa.sa_family != b->sa.sa_family)
148                 return false;
149         if (a->sa.sa_family == AF_INET6)
150                 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
151         else
152                 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
153 }
154
155 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
156 {
157         if (ipa->sa.sa_family == AF_INET6)
158                 return ipv6_addr_any(&ipa->sin6.sin6_addr);
159         else
160                 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
161 }
162
163 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
164 {
165         if (ipa->sa.sa_family == AF_INET6)
166                 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
167         else
168                 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
169 }
170
171 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
172 {
173         if (nla_len(nla) >= sizeof(struct in6_addr)) {
174                 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
175                 ip->sa.sa_family = AF_INET6;
176                 return 0;
177         } else if (nla_len(nla) >= sizeof(__be32)) {
178                 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
179                 ip->sa.sa_family = AF_INET;
180                 return 0;
181         } else {
182                 return -EAFNOSUPPORT;
183         }
184 }
185
186 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
187                               const union vxlan_addr *ip)
188 {
189         if (ip->sa.sa_family == AF_INET6)
190                 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
191         else
192                 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
193 }
194
195 #else /* !CONFIG_IPV6 */
196
197 static inline
198 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
199 {
200         return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
201 }
202
203 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
204 {
205         return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
206 }
207
208 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
209 {
210         return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
211 }
212
213 static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
214 {
215         if (nla_len(nla) >= sizeof(struct in6_addr)) {
216                 return -EAFNOSUPPORT;
217         } else if (nla_len(nla) >= sizeof(__be32)) {
218                 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
219                 ip->sa.sa_family = AF_INET;
220                 return 0;
221         } else {
222                 return -EAFNOSUPPORT;
223         }
224 }
225
226 static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
227                               const union vxlan_addr *ip)
228 {
229         return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
230 }
231 #endif
232
233 /* Virtual Network hash table head */
234 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
235 {
236         return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
237 }
238
239 /* Socket hash table head */
240 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
241 {
242         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
243
244         return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
245 }
246
247 /* First remote destination for a forwarding entry.
248  * Guaranteed to be non-NULL because remotes are never deleted.
249  */
250 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
251 {
252         return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
253 }
254
255 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
256 {
257         return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
258 }
259
260 /* Find VXLAN socket based on network namespace, address family and UDP port
261  * and enabled unshareable flags.
262  */
263 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
264                                           __be16 port, u32 flags)
265 {
266         struct vxlan_sock *vs;
267
268         flags &= VXLAN_F_RCV_FLAGS;
269
270         hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
271                 if (inet_sk(vs->sock->sk)->inet_sport == port &&
272                     inet_sk(vs->sock->sk)->sk.sk_family == family &&
273                     vs->flags == flags)
274                         return vs;
275         }
276         return NULL;
277 }
278
279 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
280 {
281         struct vxlan_dev *vxlan;
282
283         hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
284                 if (vxlan->default_dst.remote_vni == id)
285                         return vxlan;
286         }
287
288         return NULL;
289 }
290
291 /* Look up VNI in a per net namespace table */
292 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
293                                         sa_family_t family, __be16 port,
294                                         u32 flags)
295 {
296         struct vxlan_sock *vs;
297
298         vs = vxlan_find_sock(net, family, port, flags);
299         if (!vs)
300                 return NULL;
301
302         return vxlan_vs_find_vni(vs, id);
303 }
304
305 /* Fill in neighbour message in skbuff. */
306 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
307                           const struct vxlan_fdb *fdb,
308                           u32 portid, u32 seq, int type, unsigned int flags,
309                           const struct vxlan_rdst *rdst)
310 {
311         unsigned long now = jiffies;
312         struct nda_cacheinfo ci;
313         struct nlmsghdr *nlh;
314         struct ndmsg *ndm;
315         bool send_ip, send_eth;
316
317         nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
318         if (nlh == NULL)
319                 return -EMSGSIZE;
320
321         ndm = nlmsg_data(nlh);
322         memset(ndm, 0, sizeof(*ndm));
323
324         send_eth = send_ip = true;
325
326         if (type == RTM_GETNEIGH) {
327                 ndm->ndm_family = AF_INET;
328                 send_ip = !vxlan_addr_any(&rdst->remote_ip);
329                 send_eth = !is_zero_ether_addr(fdb->eth_addr);
330         } else
331                 ndm->ndm_family = AF_BRIDGE;
332         ndm->ndm_state = fdb->state;
333         ndm->ndm_ifindex = vxlan->dev->ifindex;
334         ndm->ndm_flags = fdb->flags;
335         ndm->ndm_type = RTN_UNICAST;
336
337         if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
338             nla_put_s32(skb, NDA_LINK_NETNSID,
339                         peernet2id(dev_net(vxlan->dev), vxlan->net)))
340                 goto nla_put_failure;
341
342         if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
343                 goto nla_put_failure;
344
345         if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
346                 goto nla_put_failure;
347
348         if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
349             nla_put_be16(skb, NDA_PORT, rdst->remote_port))
350                 goto nla_put_failure;
351         if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
352             nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
353                 goto nla_put_failure;
354         if (rdst->remote_ifindex &&
355             nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
356                 goto nla_put_failure;
357
358         ci.ndm_used      = jiffies_to_clock_t(now - fdb->used);
359         ci.ndm_confirmed = 0;
360         ci.ndm_updated   = jiffies_to_clock_t(now - fdb->updated);
361         ci.ndm_refcnt    = 0;
362
363         if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
364                 goto nla_put_failure;
365
366         nlmsg_end(skb, nlh);
367         return 0;
368
369 nla_put_failure:
370         nlmsg_cancel(skb, nlh);
371         return -EMSGSIZE;
372 }
373
374 static inline size_t vxlan_nlmsg_size(void)
375 {
376         return NLMSG_ALIGN(sizeof(struct ndmsg))
377                 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
378                 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
379                 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
380                 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
381                 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
382                 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
383                 + nla_total_size(sizeof(struct nda_cacheinfo));
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         struct net *net = dev_net(vxlan->dev);
390         struct sk_buff *skb;
391         int err = -ENOBUFS;
392
393         skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
394         if (skb == NULL)
395                 goto errout;
396
397         err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
398         if (err < 0) {
399                 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
400                 WARN_ON(err == -EMSGSIZE);
401                 kfree_skb(skb);
402                 goto errout;
403         }
404
405         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
406         return;
407 errout:
408         if (err < 0)
409                 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
410 }
411
412 static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
413 {
414         struct vxlan_dev *vxlan = netdev_priv(dev);
415         struct vxlan_fdb f = {
416                 .state = NUD_STALE,
417         };
418         struct vxlan_rdst remote = {
419                 .remote_ip = *ipa, /* goes to NDA_DST */
420                 .remote_vni = VXLAN_N_VID,
421         };
422
423         vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
424 }
425
426 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
427 {
428         struct vxlan_fdb f = {
429                 .state = NUD_STALE,
430         };
431         struct vxlan_rdst remote = { };
432
433         memcpy(f.eth_addr, eth_addr, ETH_ALEN);
434
435         vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
436 }
437
438 /* Hash Ethernet address */
439 static u32 eth_hash(const unsigned char *addr)
440 {
441         u64 value = get_unaligned((u64 *)addr);
442
443         /* only want 6 bytes */
444 #ifdef __BIG_ENDIAN
445         value >>= 16;
446 #else
447         value <<= 16;
448 #endif
449         return hash_64(value, FDB_HASH_BITS);
450 }
451
452 /* Hash chain to use given mac address */
453 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
454                                                 const u8 *mac)
455 {
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)
462 {
463         struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
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                         return f;
469         }
470
471         return NULL;
472 }
473
474 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
475                                         const u8 *mac)
476 {
477         struct vxlan_fdb *f;
478
479         f = __vxlan_find_mac(vxlan, mac);
480         if (f)
481                 f->used = jiffies;
482
483         return f;
484 }
485
486 /* caller should hold vxlan->hash_lock */
487 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
488                                               union vxlan_addr *ip, __be16 port,
489                                               __u32 vni, __u32 ifindex)
490 {
491         struct vxlan_rdst *rd;
492
493         list_for_each_entry(rd, &f->remotes, list) {
494                 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
495                     rd->remote_port == port &&
496                     rd->remote_vni == vni &&
497                     rd->remote_ifindex == ifindex)
498                         return rd;
499         }
500
501         return NULL;
502 }
503
504 /* Replace destination of unicast mac */
505 static int vxlan_fdb_replace(struct vxlan_fdb *f,
506                              union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
507 {
508         struct vxlan_rdst *rd;
509
510         rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
511         if (rd)
512                 return 0;
513
514         rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
515         if (!rd)
516                 return 0;
517         rd->remote_ip = *ip;
518         rd->remote_port = port;
519         rd->remote_vni = vni;
520         rd->remote_ifindex = ifindex;
521         return 1;
522 }
523
524 /* Add/update destinations for multicast */
525 static int vxlan_fdb_append(struct vxlan_fdb *f,
526                             union vxlan_addr *ip, __be16 port, __u32 vni,
527                             __u32 ifindex, struct vxlan_rdst **rdp)
528 {
529         struct vxlan_rdst *rd;
530
531         rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
532         if (rd)
533                 return 0;
534
535         rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
536         if (rd == NULL)
537                 return -ENOBUFS;
538         rd->remote_ip = *ip;
539         rd->remote_port = port;
540         rd->remote_vni = vni;
541         rd->remote_ifindex = ifindex;
542
543         list_add_tail_rcu(&rd->list, &f->remotes);
544
545         *rdp = rd;
546         return 1;
547 }
548
549 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
550                                           unsigned int off,
551                                           struct vxlanhdr *vh, size_t hdrlen,
552                                           u32 data, struct gro_remcsum *grc,
553                                           bool nopartial)
554 {
555         size_t start, offset, plen;
556
557         if (skb->remcsum_offload)
558                 return NULL;
559
560         if (!NAPI_GRO_CB(skb)->csum_valid)
561                 return NULL;
562
563         start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
564         offset = start + ((data & VXLAN_RCO_UDP) ?
565                           offsetof(struct udphdr, check) :
566                           offsetof(struct tcphdr, check));
567
568         plen = hdrlen + offset + sizeof(u16);
569
570         /* Pull checksum that will be written */
571         if (skb_gro_header_hard(skb, off + plen)) {
572                 vh = skb_gro_header_slow(skb, off + plen, off);
573                 if (!vh)
574                         return NULL;
575         }
576
577         skb_gro_remcsum_process(skb, (void *)vh + hdrlen,
578                                 start, offset, grc, nopartial);
579
580         skb->remcsum_offload = 1;
581
582         return vh;
583 }
584
585 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
586                                           struct sk_buff *skb,
587                                           struct udp_offload *uoff)
588 {
589         struct sk_buff *p, **pp = NULL;
590         struct vxlanhdr *vh, *vh2;
591         unsigned int hlen, off_vx;
592         int flush = 1;
593         struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
594                                              udp_offloads);
595         u32 flags;
596         struct gro_remcsum grc;
597
598         skb_gro_remcsum_init(&grc);
599
600         off_vx = skb_gro_offset(skb);
601         hlen = off_vx + sizeof(*vh);
602         vh   = skb_gro_header_fast(skb, off_vx);
603         if (skb_gro_header_hard(skb, hlen)) {
604                 vh = skb_gro_header_slow(skb, hlen, off_vx);
605                 if (unlikely(!vh))
606                         goto out;
607         }
608
609         skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
610         skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
611
612         flags = ntohl(vh->vx_flags);
613
614         if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
615                 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
616                                        ntohl(vh->vx_vni), &grc,
617                                        !!(vs->flags &
618                                           VXLAN_F_REMCSUM_NOPARTIAL));
619
620                 if (!vh)
621                         goto out;
622         }
623
624         flush = 0;
625
626         for (p = *head; p; p = p->next) {
627                 if (!NAPI_GRO_CB(p)->same_flow)
628                         continue;
629
630                 vh2 = (struct vxlanhdr *)(p->data + off_vx);
631                 if (vh->vx_flags != vh2->vx_flags ||
632                     vh->vx_vni != vh2->vx_vni) {
633                         NAPI_GRO_CB(p)->same_flow = 0;
634                         continue;
635                 }
636         }
637
638         pp = eth_gro_receive(head, skb);
639
640 out:
641         skb_gro_remcsum_cleanup(skb, &grc);
642         NAPI_GRO_CB(skb)->flush |= flush;
643
644         return pp;
645 }
646
647 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
648                               struct udp_offload *uoff)
649 {
650         udp_tunnel_gro_complete(skb, nhoff);
651
652         return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
653 }
654
655 /* Notify netdevs that UDP port started listening */
656 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
657 {
658         struct net_device *dev;
659         struct sock *sk = vs->sock->sk;
660         struct net *net = sock_net(sk);
661         sa_family_t sa_family = sk->sk_family;
662         __be16 port = inet_sk(sk)->inet_sport;
663         int err;
664
665         if (sa_family == AF_INET) {
666                 err = udp_add_offload(&vs->udp_offloads);
667                 if (err)
668                         pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
669         }
670
671         rcu_read_lock();
672         for_each_netdev_rcu(net, dev) {
673                 if (dev->netdev_ops->ndo_add_vxlan_port)
674                         dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
675                                                             port);
676         }
677         rcu_read_unlock();
678 }
679
680 /* Notify netdevs that UDP port is no more listening */
681 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
682 {
683         struct net_device *dev;
684         struct sock *sk = vs->sock->sk;
685         struct net *net = sock_net(sk);
686         sa_family_t sa_family = sk->sk_family;
687         __be16 port = inet_sk(sk)->inet_sport;
688
689         rcu_read_lock();
690         for_each_netdev_rcu(net, dev) {
691                 if (dev->netdev_ops->ndo_del_vxlan_port)
692                         dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
693                                                             port);
694         }
695         rcu_read_unlock();
696
697         if (sa_family == AF_INET)
698                 udp_del_offload(&vs->udp_offloads);
699 }
700
701 /* Add new entry to forwarding table -- assumes lock held */
702 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
703                             const u8 *mac, union vxlan_addr *ip,
704                             __u16 state, __u16 flags,
705                             __be16 port, __u32 vni, __u32 ifindex,
706                             __u8 ndm_flags)
707 {
708         struct vxlan_rdst *rd = NULL;
709         struct vxlan_fdb *f;
710         int notify = 0;
711
712         f = __vxlan_find_mac(vxlan, mac);
713         if (f) {
714                 if (flags & NLM_F_EXCL) {
715                         netdev_dbg(vxlan->dev,
716                                    "lost race to create %pM\n", mac);
717                         return -EEXIST;
718                 }
719                 if (f->state != state) {
720                         f->state = state;
721                         f->updated = jiffies;
722                         notify = 1;
723                 }
724                 if (f->flags != ndm_flags) {
725                         f->flags = ndm_flags;
726                         f->updated = jiffies;
727                         notify = 1;
728                 }
729                 if ((flags & NLM_F_REPLACE)) {
730                         /* Only change unicasts */
731                         if (!(is_multicast_ether_addr(f->eth_addr) ||
732                              is_zero_ether_addr(f->eth_addr))) {
733                                 int rc = vxlan_fdb_replace(f, ip, port, vni,
734                                                            ifindex);
735
736                                 if (rc < 0)
737                                         return rc;
738                                 notify |= rc;
739                         } else
740                                 return -EOPNOTSUPP;
741                 }
742                 if ((flags & NLM_F_APPEND) &&
743                     (is_multicast_ether_addr(f->eth_addr) ||
744                      is_zero_ether_addr(f->eth_addr))) {
745                         int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
746                                                   &rd);
747
748                         if (rc < 0)
749                                 return rc;
750                         notify |= rc;
751                 }
752         } else {
753                 if (!(flags & NLM_F_CREATE))
754                         return -ENOENT;
755
756                 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
757                         return -ENOSPC;
758
759                 /* Disallow replace to add a multicast entry */
760                 if ((flags & NLM_F_REPLACE) &&
761                     (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
762                         return -EOPNOTSUPP;
763
764                 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
765                 f = kmalloc(sizeof(*f), GFP_ATOMIC);
766                 if (!f)
767                         return -ENOMEM;
768
769                 notify = 1;
770                 f->state = state;
771                 f->flags = ndm_flags;
772                 f->updated = f->used = jiffies;
773                 INIT_LIST_HEAD(&f->remotes);
774                 memcpy(f->eth_addr, mac, ETH_ALEN);
775
776                 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
777
778                 ++vxlan->addrcnt;
779                 hlist_add_head_rcu(&f->hlist,
780                                    vxlan_fdb_head(vxlan, mac));
781         }
782
783         if (notify) {
784                 if (rd == NULL)
785                         rd = first_remote_rtnl(f);
786                 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
787         }
788
789         return 0;
790 }
791
792 static void vxlan_fdb_free(struct rcu_head *head)
793 {
794         struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
795         struct vxlan_rdst *rd, *nd;
796
797         list_for_each_entry_safe(rd, nd, &f->remotes, list)
798                 kfree(rd);
799         kfree(f);
800 }
801
802 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
803 {
804         netdev_dbg(vxlan->dev,
805                     "delete %pM\n", f->eth_addr);
806
807         --vxlan->addrcnt;
808         vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
809
810         hlist_del_rcu(&f->hlist);
811         call_rcu(&f->rcu, vxlan_fdb_free);
812 }
813
814 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
815                            union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
816 {
817         struct net *net = dev_net(vxlan->dev);
818         int err;
819
820         if (tb[NDA_DST]) {
821                 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
822                 if (err)
823                         return err;
824         } else {
825                 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
826                 if (remote->sa.sa_family == AF_INET) {
827                         ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
828                         ip->sa.sa_family = AF_INET;
829 #if IS_ENABLED(CONFIG_IPV6)
830                 } else {
831                         ip->sin6.sin6_addr = in6addr_any;
832                         ip->sa.sa_family = AF_INET6;
833 #endif
834                 }
835         }
836
837         if (tb[NDA_PORT]) {
838                 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
839                         return -EINVAL;
840                 *port = nla_get_be16(tb[NDA_PORT]);
841         } else {
842                 *port = vxlan->dst_port;
843         }
844
845         if (tb[NDA_VNI]) {
846                 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
847                         return -EINVAL;
848                 *vni = nla_get_u32(tb[NDA_VNI]);
849         } else {
850                 *vni = vxlan->default_dst.remote_vni;
851         }
852
853         if (tb[NDA_IFINDEX]) {
854                 struct net_device *tdev;
855
856                 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
857                         return -EINVAL;
858                 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
859                 tdev = __dev_get_by_index(net, *ifindex);
860                 if (!tdev)
861                         return -EADDRNOTAVAIL;
862         } else {
863                 *ifindex = 0;
864         }
865
866         return 0;
867 }
868
869 /* Add static entry (via netlink) */
870 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
871                          struct net_device *dev,
872                          const unsigned char *addr, u16 vid, u16 flags)
873 {
874         struct vxlan_dev *vxlan = netdev_priv(dev);
875         /* struct net *net = dev_net(vxlan->dev); */
876         union vxlan_addr ip;
877         __be16 port;
878         u32 vni, ifindex;
879         int err;
880
881         if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
882                 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
883                         ndm->ndm_state);
884                 return -EINVAL;
885         }
886
887         if (tb[NDA_DST] == NULL)
888                 return -EINVAL;
889
890         err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
891         if (err)
892                 return err;
893
894         if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
895                 return -EAFNOSUPPORT;
896
897         spin_lock_bh(&vxlan->hash_lock);
898         err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
899                                port, vni, ifindex, ndm->ndm_flags);
900         spin_unlock_bh(&vxlan->hash_lock);
901
902         return err;
903 }
904
905 /* Delete entry (via netlink) */
906 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
907                             struct net_device *dev,
908                             const unsigned char *addr, u16 vid)
909 {
910         struct vxlan_dev *vxlan = netdev_priv(dev);
911         struct vxlan_fdb *f;
912         struct vxlan_rdst *rd = NULL;
913         union vxlan_addr ip;
914         __be16 port;
915         u32 vni, ifindex;
916         int err;
917
918         err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
919         if (err)
920                 return err;
921
922         err = -ENOENT;
923
924         spin_lock_bh(&vxlan->hash_lock);
925         f = vxlan_find_mac(vxlan, addr);
926         if (!f)
927                 goto out;
928
929         if (!vxlan_addr_any(&ip)) {
930                 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
931                 if (!rd)
932                         goto out;
933         }
934
935         err = 0;
936
937         /* remove a destination if it's not the only one on the list,
938          * otherwise destroy the fdb entry
939          */
940         if (rd && !list_is_singular(&f->remotes)) {
941                 list_del_rcu(&rd->list);
942                 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
943                 kfree_rcu(rd, rcu);
944                 goto out;
945         }
946
947         vxlan_fdb_destroy(vxlan, f);
948
949 out:
950         spin_unlock_bh(&vxlan->hash_lock);
951
952         return err;
953 }
954
955 /* Dump forwarding table */
956 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
957                           struct net_device *dev,
958                           struct net_device *filter_dev, int idx)
959 {
960         struct vxlan_dev *vxlan = netdev_priv(dev);
961         unsigned int h;
962
963         for (h = 0; h < FDB_HASH_SIZE; ++h) {
964                 struct vxlan_fdb *f;
965                 int err;
966
967                 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
968                         struct vxlan_rdst *rd;
969
970                         if (idx < cb->args[0])
971                                 goto skip;
972
973                         list_for_each_entry_rcu(rd, &f->remotes, list) {
974                                 err = vxlan_fdb_info(skb, vxlan, f,
975                                                      NETLINK_CB(cb->skb).portid,
976                                                      cb->nlh->nlmsg_seq,
977                                                      RTM_NEWNEIGH,
978                                                      NLM_F_MULTI, rd);
979                                 if (err < 0)
980                                         goto out;
981                         }
982 skip:
983                         ++idx;
984                 }
985         }
986 out:
987         return idx;
988 }
989
990 /* Watch incoming packets to learn mapping between Ethernet address
991  * and Tunnel endpoint.
992  * Return true if packet is bogus and should be dropped.
993  */
994 static bool vxlan_snoop(struct net_device *dev,
995                         union vxlan_addr *src_ip, const u8 *src_mac)
996 {
997         struct vxlan_dev *vxlan = netdev_priv(dev);
998         struct vxlan_fdb *f;
999
1000         f = vxlan_find_mac(vxlan, src_mac);
1001         if (likely(f)) {
1002                 struct vxlan_rdst *rdst = first_remote_rcu(f);
1003
1004                 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
1005                         return false;
1006
1007                 /* Don't migrate static entries, drop packets */
1008                 if (f->state & NUD_NOARP)
1009                         return true;
1010
1011                 if (net_ratelimit())
1012                         netdev_info(dev,
1013                                     "%pM migrated from %pIS to %pIS\n",
1014                                     src_mac, &rdst->remote_ip.sa, &src_ip->sa);
1015
1016                 rdst->remote_ip = *src_ip;
1017                 f->updated = jiffies;
1018                 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
1019         } else {
1020                 /* learned new entry */
1021                 spin_lock(&vxlan->hash_lock);
1022
1023                 /* close off race between vxlan_flush and incoming packets */
1024                 if (netif_running(dev))
1025                         vxlan_fdb_create(vxlan, src_mac, src_ip,
1026                                          NUD_REACHABLE,
1027                                          NLM_F_EXCL|NLM_F_CREATE,
1028                                          vxlan->dst_port,
1029                                          vxlan->default_dst.remote_vni,
1030                                          0, NTF_SELF);
1031                 spin_unlock(&vxlan->hash_lock);
1032         }
1033
1034         return false;
1035 }
1036
1037 /* See if multicast group is already in use by other ID */
1038 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
1039 {
1040         struct vxlan_dev *vxlan;
1041
1042         /* The vxlan_sock is only used by dev, leaving group has
1043          * no effect on other vxlan devices.
1044          */
1045         if (atomic_read(&dev->vn_sock->refcnt) == 1)
1046                 return false;
1047
1048         list_for_each_entry(vxlan, &vn->vxlan_list, next) {
1049                 if (!netif_running(vxlan->dev) || vxlan == dev)
1050                         continue;
1051
1052                 if (vxlan->vn_sock != dev->vn_sock)
1053                         continue;
1054
1055                 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1056                                       &dev->default_dst.remote_ip))
1057                         continue;
1058
1059                 if (vxlan->default_dst.remote_ifindex !=
1060                     dev->default_dst.remote_ifindex)
1061                         continue;
1062
1063                 return true;
1064         }
1065
1066         return false;
1067 }
1068
1069 void vxlan_sock_release(struct vxlan_sock *vs)
1070 {
1071         struct sock *sk = vs->sock->sk;
1072         struct net *net = sock_net(sk);
1073         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1074
1075         if (!atomic_dec_and_test(&vs->refcnt))
1076                 return;
1077
1078         spin_lock(&vn->sock_lock);
1079         hlist_del_rcu(&vs->hlist);
1080         vxlan_notify_del_rx_port(vs);
1081         spin_unlock(&vn->sock_lock);
1082
1083         queue_work(vxlan_wq, &vs->del_work);
1084 }
1085 EXPORT_SYMBOL_GPL(vxlan_sock_release);
1086
1087 /* Update multicast group membership when first VNI on
1088  * multicast address is brought up
1089  */
1090 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
1091 {
1092         struct vxlan_sock *vs = vxlan->vn_sock;
1093         struct sock *sk = vs->sock->sk;
1094         union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1095         int ifindex = vxlan->default_dst.remote_ifindex;
1096         int ret = -EINVAL;
1097
1098         lock_sock(sk);
1099         if (ip->sa.sa_family == AF_INET) {
1100                 struct ip_mreqn mreq = {
1101                         .imr_multiaddr.s_addr   = ip->sin.sin_addr.s_addr,
1102                         .imr_ifindex            = ifindex,
1103                 };
1104
1105                 ret = ip_mc_join_group(sk, &mreq);
1106 #if IS_ENABLED(CONFIG_IPV6)
1107         } else {
1108                 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1109                                                    &ip->sin6.sin6_addr);
1110 #endif
1111         }
1112         release_sock(sk);
1113
1114         return ret;
1115 }
1116
1117 /* Inverse of vxlan_igmp_join when last VNI is brought down */
1118 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
1119 {
1120         struct vxlan_sock *vs = vxlan->vn_sock;
1121         struct sock *sk = vs->sock->sk;
1122         union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1123         int ifindex = vxlan->default_dst.remote_ifindex;
1124         int ret = -EINVAL;
1125
1126         lock_sock(sk);
1127         if (ip->sa.sa_family == AF_INET) {
1128                 struct ip_mreqn mreq = {
1129                         .imr_multiaddr.s_addr   = ip->sin.sin_addr.s_addr,
1130                         .imr_ifindex            = ifindex,
1131                 };
1132
1133                 ret = ip_mc_leave_group(sk, &mreq);
1134 #if IS_ENABLED(CONFIG_IPV6)
1135         } else {
1136                 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1137                                                    &ip->sin6.sin6_addr);
1138 #endif
1139         }
1140         release_sock(sk);
1141
1142         return ret;
1143 }
1144
1145 static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1146                                       size_t hdrlen, u32 data, bool nopartial)
1147 {
1148         size_t start, offset, plen;
1149
1150         start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1151         offset = start + ((data & VXLAN_RCO_UDP) ?
1152                           offsetof(struct udphdr, check) :
1153                           offsetof(struct tcphdr, check));
1154
1155         plen = hdrlen + offset + sizeof(u16);
1156
1157         if (!pskb_may_pull(skb, plen))
1158                 return NULL;
1159
1160         vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1161
1162         skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset,
1163                             nopartial);
1164
1165         return vh;
1166 }
1167
1168 /* Callback from net/ipv4/udp.c to receive packets */
1169 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1170 {
1171         struct vxlan_sock *vs;
1172         struct vxlanhdr *vxh;
1173         u32 flags, vni;
1174         struct vxlan_metadata md = {0};
1175
1176         /* Need Vxlan and inner Ethernet header to be present */
1177         if (!pskb_may_pull(skb, VXLAN_HLEN))
1178                 goto error;
1179
1180         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1181         flags = ntohl(vxh->vx_flags);
1182         vni = ntohl(vxh->vx_vni);
1183
1184         if (flags & VXLAN_HF_VNI) {
1185                 flags &= ~VXLAN_HF_VNI;
1186         } else {
1187                 /* VNI flag always required to be set */
1188                 goto bad_flags;
1189         }
1190
1191         if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1192                 goto drop;
1193         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1194
1195         vs = rcu_dereference_sk_user_data(sk);
1196         if (!vs)
1197                 goto drop;
1198
1199         if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1200                 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni,
1201                                     !!(vs->flags & VXLAN_F_REMCSUM_NOPARTIAL));
1202                 if (!vxh)
1203                         goto drop;
1204
1205                 flags &= ~VXLAN_HF_RCO;
1206                 vni &= VXLAN_VNI_MASK;
1207         }
1208
1209         /* For backwards compatibility, only allow reserved fields to be
1210          * used by VXLAN extensions if explicitly requested.
1211          */
1212         if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1213                 struct vxlanhdr_gbp *gbp;
1214
1215                 gbp = (struct vxlanhdr_gbp *)vxh;
1216                 md.gbp = ntohs(gbp->policy_id);
1217
1218                 if (gbp->dont_learn)
1219                         md.gbp |= VXLAN_GBP_DONT_LEARN;
1220
1221                 if (gbp->policy_applied)
1222                         md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1223
1224                 flags &= ~VXLAN_GBP_USED_BITS;
1225         }
1226
1227         if (flags || vni & ~VXLAN_VNI_MASK) {
1228                 /* If there are any unprocessed flags remaining treat
1229                  * this as a malformed packet. This behavior diverges from
1230                  * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1231                  * in reserved fields are to be ignored. The approach here
1232                  * maintains compatibility with previous stack code, and also
1233                  * is more robust and provides a little more security in
1234                  * adding extensions to VXLAN.
1235                  */
1236
1237                 goto bad_flags;
1238         }
1239
1240         md.vni = vxh->vx_vni;
1241         vs->rcv(vs, skb, &md);
1242         return 0;
1243
1244 drop:
1245         /* Consume bad packet */
1246         kfree_skb(skb);
1247         return 0;
1248
1249 bad_flags:
1250         netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1251                    ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1252
1253 error:
1254         /* Return non vxlan pkt */
1255         return 1;
1256 }
1257
1258 static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1259                       struct vxlan_metadata *md)
1260 {
1261         struct iphdr *oip = NULL;
1262         struct ipv6hdr *oip6 = NULL;
1263         struct vxlan_dev *vxlan;
1264         struct pcpu_sw_netstats *stats;
1265         union vxlan_addr saddr;
1266         __u32 vni;
1267         int err = 0;
1268         union vxlan_addr *remote_ip;
1269
1270         vni = ntohl(md->vni) >> 8;
1271         /* Is this VNI defined? */
1272         vxlan = vxlan_vs_find_vni(vs, vni);
1273         if (!vxlan)
1274                 goto drop;
1275
1276         remote_ip = &vxlan->default_dst.remote_ip;
1277         skb_reset_mac_header(skb);
1278         skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
1279         skb->protocol = eth_type_trans(skb, vxlan->dev);
1280         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1281
1282         /* Ignore packet loops (and multicast echo) */
1283         if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1284                 goto drop;
1285
1286         /* Re-examine inner Ethernet packet */
1287         if (remote_ip->sa.sa_family == AF_INET) {
1288                 oip = ip_hdr(skb);
1289                 saddr.sin.sin_addr.s_addr = oip->saddr;
1290                 saddr.sa.sa_family = AF_INET;
1291 #if IS_ENABLED(CONFIG_IPV6)
1292         } else {
1293                 oip6 = ipv6_hdr(skb);
1294                 saddr.sin6.sin6_addr = oip6->saddr;
1295                 saddr.sa.sa_family = AF_INET6;
1296 #endif
1297         }
1298
1299         if ((vxlan->flags & VXLAN_F_LEARN) &&
1300             vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
1301                 goto drop;
1302
1303         skb_reset_network_header(skb);
1304         skb->mark = md->gbp;
1305
1306         if (oip6)
1307                 err = IP6_ECN_decapsulate(oip6, skb);
1308         if (oip)
1309                 err = IP_ECN_decapsulate(oip, skb);
1310
1311         if (unlikely(err)) {
1312                 if (log_ecn_error) {
1313                         if (oip6)
1314                                 net_info_ratelimited("non-ECT from %pI6\n",
1315                                                      &oip6->saddr);
1316                         if (oip)
1317                                 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1318                                                      &oip->saddr, oip->tos);
1319                 }
1320                 if (err > 1) {
1321                         ++vxlan->dev->stats.rx_frame_errors;
1322                         ++vxlan->dev->stats.rx_errors;
1323                         goto drop;
1324                 }
1325         }
1326
1327         stats = this_cpu_ptr(vxlan->dev->tstats);
1328         u64_stats_update_begin(&stats->syncp);
1329         stats->rx_packets++;
1330         stats->rx_bytes += skb->len;
1331         u64_stats_update_end(&stats->syncp);
1332
1333         netif_rx(skb);
1334
1335         return;
1336 drop:
1337         /* Consume bad packet */
1338         kfree_skb(skb);
1339 }
1340
1341 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1342 {
1343         struct vxlan_dev *vxlan = netdev_priv(dev);
1344         struct arphdr *parp;
1345         u8 *arpptr, *sha;
1346         __be32 sip, tip;
1347         struct neighbour *n;
1348
1349         if (dev->flags & IFF_NOARP)
1350                 goto out;
1351
1352         if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1353                 dev->stats.tx_dropped++;
1354                 goto out;
1355         }
1356         parp = arp_hdr(skb);
1357
1358         if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1359              parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1360             parp->ar_pro != htons(ETH_P_IP) ||
1361             parp->ar_op != htons(ARPOP_REQUEST) ||
1362             parp->ar_hln != dev->addr_len ||
1363             parp->ar_pln != 4)
1364                 goto out;
1365         arpptr = (u8 *)parp + sizeof(struct arphdr);
1366         sha = arpptr;
1367         arpptr += dev->addr_len;        /* sha */
1368         memcpy(&sip, arpptr, sizeof(sip));
1369         arpptr += sizeof(sip);
1370         arpptr += dev->addr_len;        /* tha */
1371         memcpy(&tip, arpptr, sizeof(tip));
1372
1373         if (ipv4_is_loopback(tip) ||
1374             ipv4_is_multicast(tip))
1375                 goto out;
1376
1377         n = neigh_lookup(&arp_tbl, &tip, dev);
1378
1379         if (n) {
1380                 struct vxlan_fdb *f;
1381                 struct sk_buff  *reply;
1382
1383                 if (!(n->nud_state & NUD_CONNECTED)) {
1384                         neigh_release(n);
1385                         goto out;
1386                 }
1387
1388                 f = vxlan_find_mac(vxlan, n->ha);
1389                 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1390                         /* bridge-local neighbor */
1391                         neigh_release(n);
1392                         goto out;
1393                 }
1394
1395                 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1396                                 n->ha, sha);
1397
1398                 neigh_release(n);
1399
1400                 if (reply == NULL)
1401                         goto out;
1402
1403                 skb_reset_mac_header(reply);
1404                 __skb_pull(reply, skb_network_offset(reply));
1405                 reply->ip_summed = CHECKSUM_UNNECESSARY;
1406                 reply->pkt_type = PACKET_HOST;
1407
1408                 if (netif_rx_ni(reply) == NET_RX_DROP)
1409                         dev->stats.rx_dropped++;
1410         } else if (vxlan->flags & VXLAN_F_L3MISS) {
1411                 union vxlan_addr ipa = {
1412                         .sin.sin_addr.s_addr = tip,
1413                         .sin.sin_family = AF_INET,
1414                 };
1415
1416                 vxlan_ip_miss(dev, &ipa);
1417         }
1418 out:
1419         consume_skb(skb);
1420         return NETDEV_TX_OK;
1421 }
1422
1423 #if IS_ENABLED(CONFIG_IPV6)
1424 static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1425         struct neighbour *n, bool isrouter)
1426 {
1427         struct net_device *dev = request->dev;
1428         struct sk_buff *reply;
1429         struct nd_msg *ns, *na;
1430         struct ipv6hdr *pip6;
1431         u8 *daddr;
1432         int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1433         int ns_olen;
1434         int i, len;
1435
1436         if (dev == NULL)
1437                 return NULL;
1438
1439         len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1440                 sizeof(*na) + na_olen + dev->needed_tailroom;
1441         reply = alloc_skb(len, GFP_ATOMIC);
1442         if (reply == NULL)
1443                 return NULL;
1444
1445         reply->protocol = htons(ETH_P_IPV6);
1446         reply->dev = dev;
1447         skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1448         skb_push(reply, sizeof(struct ethhdr));
1449         skb_set_mac_header(reply, 0);
1450
1451         ns = (struct nd_msg *)skb_transport_header(request);
1452
1453         daddr = eth_hdr(request)->h_source;
1454         ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1455         for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1456                 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1457                         daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1458                         break;
1459                 }
1460         }
1461
1462         /* Ethernet header */
1463         ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1464         ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1465         eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1466         reply->protocol = htons(ETH_P_IPV6);
1467
1468         skb_pull(reply, sizeof(struct ethhdr));
1469         skb_set_network_header(reply, 0);
1470         skb_put(reply, sizeof(struct ipv6hdr));
1471
1472         /* IPv6 header */
1473
1474         pip6 = ipv6_hdr(reply);
1475         memset(pip6, 0, sizeof(struct ipv6hdr));
1476         pip6->version = 6;
1477         pip6->priority = ipv6_hdr(request)->priority;
1478         pip6->nexthdr = IPPROTO_ICMPV6;
1479         pip6->hop_limit = 255;
1480         pip6->daddr = ipv6_hdr(request)->saddr;
1481         pip6->saddr = *(struct in6_addr *)n->primary_key;
1482
1483         skb_pull(reply, sizeof(struct ipv6hdr));
1484         skb_set_transport_header(reply, 0);
1485
1486         na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1487
1488         /* Neighbor Advertisement */
1489         memset(na, 0, sizeof(*na)+na_olen);
1490         na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1491         na->icmph.icmp6_router = isrouter;
1492         na->icmph.icmp6_override = 1;
1493         na->icmph.icmp6_solicited = 1;
1494         na->target = ns->target;
1495         ether_addr_copy(&na->opt[2], n->ha);
1496         na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1497         na->opt[1] = na_olen >> 3;
1498
1499         na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1500                 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1501                 csum_partial(na, sizeof(*na)+na_olen, 0));
1502
1503         pip6->payload_len = htons(sizeof(*na)+na_olen);
1504
1505         skb_push(reply, sizeof(struct ipv6hdr));
1506
1507         reply->ip_summed = CHECKSUM_UNNECESSARY;
1508
1509         return reply;
1510 }
1511
1512 static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1513 {
1514         struct vxlan_dev *vxlan = netdev_priv(dev);
1515         struct nd_msg *msg;
1516         const struct ipv6hdr *iphdr;
1517         const struct in6_addr *saddr, *daddr;
1518         struct neighbour *n;
1519         struct inet6_dev *in6_dev;
1520
1521         in6_dev = __in6_dev_get(dev);
1522         if (!in6_dev)
1523                 goto out;
1524
1525         iphdr = ipv6_hdr(skb);
1526         saddr = &iphdr->saddr;
1527         daddr = &iphdr->daddr;
1528
1529         msg = (struct nd_msg *)skb_transport_header(skb);
1530         if (msg->icmph.icmp6_code != 0 ||
1531             msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1532                 goto out;
1533
1534         if (ipv6_addr_loopback(daddr) ||
1535             ipv6_addr_is_multicast(&msg->target))
1536                 goto out;
1537
1538         n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
1539
1540         if (n) {
1541                 struct vxlan_fdb *f;
1542                 struct sk_buff *reply;
1543
1544                 if (!(n->nud_state & NUD_CONNECTED)) {
1545                         neigh_release(n);
1546                         goto out;
1547                 }
1548
1549                 f = vxlan_find_mac(vxlan, n->ha);
1550                 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1551                         /* bridge-local neighbor */
1552                         neigh_release(n);
1553                         goto out;
1554                 }
1555
1556                 reply = vxlan_na_create(skb, n,
1557                                         !!(f ? f->flags & NTF_ROUTER : 0));
1558
1559                 neigh_release(n);
1560
1561                 if (reply == NULL)
1562                         goto out;
1563
1564                 if (netif_rx_ni(reply) == NET_RX_DROP)
1565                         dev->stats.rx_dropped++;
1566
1567         } else if (vxlan->flags & VXLAN_F_L3MISS) {
1568                 union vxlan_addr ipa = {
1569                         .sin6.sin6_addr = msg->target,
1570                         .sin6.sin6_family = AF_INET6,
1571                 };
1572
1573                 vxlan_ip_miss(dev, &ipa);
1574         }
1575
1576 out:
1577         consume_skb(skb);
1578         return NETDEV_TX_OK;
1579 }
1580 #endif
1581
1582 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1583 {
1584         struct vxlan_dev *vxlan = netdev_priv(dev);
1585         struct neighbour *n;
1586
1587         if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1588                 return false;
1589
1590         n = NULL;
1591         switch (ntohs(eth_hdr(skb)->h_proto)) {
1592         case ETH_P_IP:
1593         {
1594                 struct iphdr *pip;
1595
1596                 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1597                         return false;
1598                 pip = ip_hdr(skb);
1599                 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1600                 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1601                         union vxlan_addr ipa = {
1602                                 .sin.sin_addr.s_addr = pip->daddr,
1603                                 .sin.sin_family = AF_INET,
1604                         };
1605
1606                         vxlan_ip_miss(dev, &ipa);
1607                         return false;
1608                 }
1609
1610                 break;
1611         }
1612 #if IS_ENABLED(CONFIG_IPV6)
1613         case ETH_P_IPV6:
1614         {
1615                 struct ipv6hdr *pip6;
1616
1617                 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1618                         return false;
1619                 pip6 = ipv6_hdr(skb);
1620                 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1621                 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1622                         union vxlan_addr ipa = {
1623                                 .sin6.sin6_addr = pip6->daddr,
1624                                 .sin6.sin6_family = AF_INET6,
1625                         };
1626
1627                         vxlan_ip_miss(dev, &ipa);
1628                         return false;
1629                 }
1630
1631                 break;
1632         }
1633 #endif
1634         default:
1635                 return false;
1636         }
1637
1638         if (n) {
1639                 bool diff;
1640
1641                 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
1642                 if (diff) {
1643                         memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1644                                 dev->addr_len);
1645                         memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1646                 }
1647                 neigh_release(n);
1648                 return diff;
1649         }
1650
1651         return false;
1652 }
1653
1654 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1655                                 struct vxlan_metadata *md)
1656 {
1657         struct vxlanhdr_gbp *gbp;
1658
1659         if (!md->gbp)
1660                 return;
1661
1662         gbp = (struct vxlanhdr_gbp *)vxh;
1663         vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1664
1665         if (md->gbp & VXLAN_GBP_DONT_LEARN)
1666                 gbp->dont_learn = 1;
1667
1668         if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1669                 gbp->policy_applied = 1;
1670
1671         gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1672 }
1673
1674 #if IS_ENABLED(CONFIG_IPV6)
1675 static int vxlan6_xmit_skb(struct dst_entry *dst, struct sock *sk,
1676                            struct sk_buff *skb,
1677                            struct net_device *dev, struct in6_addr *saddr,
1678                            struct in6_addr *daddr, __u8 prio, __u8 ttl,
1679                            __be16 src_port, __be16 dst_port,
1680                            struct vxlan_metadata *md, bool xnet, u32 vxflags)
1681 {
1682         struct vxlanhdr *vxh;
1683         int min_headroom;
1684         int err;
1685         bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
1686         int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1687         u16 hdrlen = sizeof(struct vxlanhdr);
1688
1689         if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1690             skb->ip_summed == CHECKSUM_PARTIAL) {
1691                 int csum_start = skb_checksum_start_offset(skb);
1692
1693                 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1694                     !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1695                     (skb->csum_offset == offsetof(struct udphdr, check) ||
1696                      skb->csum_offset == offsetof(struct tcphdr, check))) {
1697                         udp_sum = false;
1698                         type |= SKB_GSO_TUNNEL_REMCSUM;
1699                 }
1700         }
1701
1702         skb_scrub_packet(skb, xnet);
1703
1704         min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1705                         + VXLAN_HLEN + sizeof(struct ipv6hdr)
1706                         + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1707
1708         /* Need space for new headers (invalidates iph ptr) */
1709         err = skb_cow_head(skb, min_headroom);
1710         if (unlikely(err)) {
1711                 kfree_skb(skb);
1712                 goto err;
1713         }
1714
1715         skb = vlan_hwaccel_push_inside(skb);
1716         if (WARN_ON(!skb)) {
1717                 err = -ENOMEM;
1718                 goto err;
1719         }
1720
1721         skb = iptunnel_handle_offloads(skb, udp_sum, type);
1722         if (IS_ERR(skb)) {
1723                 err = -EINVAL;
1724                 goto err;
1725         }
1726
1727         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1728         vxh->vx_flags = htonl(VXLAN_HF_VNI);
1729         vxh->vx_vni = md->vni;
1730
1731         if (type & SKB_GSO_TUNNEL_REMCSUM) {
1732                 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1733                            VXLAN_RCO_SHIFT;
1734
1735                 if (skb->csum_offset == offsetof(struct udphdr, check))
1736                         data |= VXLAN_RCO_UDP;
1737
1738                 vxh->vx_vni |= htonl(data);
1739                 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1740
1741                 if (!skb_is_gso(skb)) {
1742                         skb->ip_summed = CHECKSUM_NONE;
1743                         skb->encapsulation = 0;
1744                 }
1745         }
1746
1747         if (vxflags & VXLAN_F_GBP)
1748                 vxlan_build_gbp_hdr(vxh, vxflags, md);
1749
1750         skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1751
1752         udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio,
1753                              ttl, src_port, dst_port,
1754                              !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
1755         return 0;
1756 err:
1757         dst_release(dst);
1758         return err;
1759 }
1760 #endif
1761
1762 int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
1763                    __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1764                    __be16 src_port, __be16 dst_port,
1765                    struct vxlan_metadata *md, bool xnet, u32 vxflags)
1766 {
1767         struct vxlanhdr *vxh;
1768         int min_headroom;
1769         int err;
1770         bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
1771         int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1772         u16 hdrlen = sizeof(struct vxlanhdr);
1773
1774         if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1775             skb->ip_summed == CHECKSUM_PARTIAL) {
1776                 int csum_start = skb_checksum_start_offset(skb);
1777
1778                 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1779                     !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1780                     (skb->csum_offset == offsetof(struct udphdr, check) ||
1781                      skb->csum_offset == offsetof(struct tcphdr, check))) {
1782                         udp_sum = false;
1783                         type |= SKB_GSO_TUNNEL_REMCSUM;
1784                 }
1785         }
1786
1787         min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1788                         + VXLAN_HLEN + sizeof(struct iphdr)
1789                         + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1790
1791         /* Need space for new headers (invalidates iph ptr) */
1792         err = skb_cow_head(skb, min_headroom);
1793         if (unlikely(err)) {
1794                 kfree_skb(skb);
1795                 return err;
1796         }
1797
1798         skb = vlan_hwaccel_push_inside(skb);
1799         if (WARN_ON(!skb))
1800                 return -ENOMEM;
1801
1802         skb = iptunnel_handle_offloads(skb, udp_sum, type);
1803         if (IS_ERR(skb))
1804                 return PTR_ERR(skb);
1805
1806         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1807         vxh->vx_flags = htonl(VXLAN_HF_VNI);
1808         vxh->vx_vni = md->vni;
1809
1810         if (type & SKB_GSO_TUNNEL_REMCSUM) {
1811                 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1812                            VXLAN_RCO_SHIFT;
1813
1814                 if (skb->csum_offset == offsetof(struct udphdr, check))
1815                         data |= VXLAN_RCO_UDP;
1816
1817                 vxh->vx_vni |= htonl(data);
1818                 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1819
1820                 if (!skb_is_gso(skb)) {
1821                         skb->ip_summed = CHECKSUM_NONE;
1822                         skb->encapsulation = 0;
1823                 }
1824         }
1825
1826         if (vxflags & VXLAN_F_GBP)
1827                 vxlan_build_gbp_hdr(vxh, vxflags, md);
1828
1829         skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1830
1831         return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
1832                                    ttl, df, src_port, dst_port, xnet,
1833                                    !(vxflags & VXLAN_F_UDP_CSUM));
1834 }
1835 EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1836
1837 /* Bypass encapsulation if the destination is local */
1838 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1839                                struct vxlan_dev *dst_vxlan)
1840 {
1841         struct pcpu_sw_netstats *tx_stats, *rx_stats;
1842         union vxlan_addr loopback;
1843         union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
1844         struct net_device *dev = skb->dev;
1845         int len = skb->len;
1846
1847         tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1848         rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1849         skb->pkt_type = PACKET_HOST;
1850         skb->encapsulation = 0;
1851         skb->dev = dst_vxlan->dev;
1852         __skb_pull(skb, skb_network_offset(skb));
1853
1854         if (remote_ip->sa.sa_family == AF_INET) {
1855                 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1856                 loopback.sa.sa_family =  AF_INET;
1857 #if IS_ENABLED(CONFIG_IPV6)
1858         } else {
1859                 loopback.sin6.sin6_addr = in6addr_loopback;
1860                 loopback.sa.sa_family =  AF_INET6;
1861 #endif
1862         }
1863
1864         if (dst_vxlan->flags & VXLAN_F_LEARN)
1865                 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
1866
1867         u64_stats_update_begin(&tx_stats->syncp);
1868         tx_stats->tx_packets++;
1869         tx_stats->tx_bytes += len;
1870         u64_stats_update_end(&tx_stats->syncp);
1871
1872         if (netif_rx(skb) == NET_RX_SUCCESS) {
1873                 u64_stats_update_begin(&rx_stats->syncp);
1874                 rx_stats->rx_packets++;
1875                 rx_stats->rx_bytes += len;
1876                 u64_stats_update_end(&rx_stats->syncp);
1877         } else {
1878                 dev->stats.rx_dropped++;
1879         }
1880 }
1881
1882 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1883                            struct vxlan_rdst *rdst, bool did_rsc)
1884 {
1885         struct vxlan_dev *vxlan = netdev_priv(dev);
1886         struct sock *sk = vxlan->vn_sock->sock->sk;
1887         struct rtable *rt = NULL;
1888         const struct iphdr *old_iph;
1889         struct flowi4 fl4;
1890         union vxlan_addr *dst;
1891         struct vxlan_metadata md;
1892         __be16 src_port = 0, dst_port;
1893         u32 vni;
1894         __be16 df = 0;
1895         __u8 tos, ttl;
1896         int err;
1897
1898         dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1899         vni = rdst->remote_vni;
1900         dst = &rdst->remote_ip;
1901
1902         if (vxlan_addr_any(dst)) {
1903                 if (did_rsc) {
1904                         /* short-circuited back to local bridge */
1905                         vxlan_encap_bypass(skb, vxlan, vxlan);
1906                         return;
1907                 }
1908                 goto drop;
1909         }
1910
1911         old_iph = ip_hdr(skb);
1912
1913         ttl = vxlan->ttl;
1914         if (!ttl && vxlan_addr_multicast(dst))
1915                 ttl = 1;
1916
1917         tos = vxlan->tos;
1918         if (tos == 1)
1919                 tos = ip_tunnel_get_dsfield(old_iph, skb);
1920
1921         src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1922                                      vxlan->port_max, true);
1923
1924         if (dst->sa.sa_family == AF_INET) {
1925                 memset(&fl4, 0, sizeof(fl4));
1926                 fl4.flowi4_oif = rdst->remote_ifindex;
1927                 fl4.flowi4_tos = RT_TOS(tos);
1928                 fl4.daddr = dst->sin.sin_addr.s_addr;
1929                 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1930
1931                 rt = ip_route_output_key(vxlan->net, &fl4);
1932                 if (IS_ERR(rt)) {
1933                         netdev_dbg(dev, "no route to %pI4\n",
1934                                    &dst->sin.sin_addr.s_addr);
1935                         dev->stats.tx_carrier_errors++;
1936                         goto tx_error;
1937                 }
1938
1939                 if (rt->dst.dev == dev) {
1940                         netdev_dbg(dev, "circular route to %pI4\n",
1941                                    &dst->sin.sin_addr.s_addr);
1942                         dev->stats.collisions++;
1943                         goto rt_tx_error;
1944                 }
1945
1946                 /* Bypass encapsulation if the destination is local */
1947                 if (rt->rt_flags & RTCF_LOCAL &&
1948                     !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1949                         struct vxlan_dev *dst_vxlan;
1950
1951                         ip_rt_put(rt);
1952                         dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1953                                                    dst->sa.sa_family, dst_port,
1954                                                    vxlan->flags);
1955                         if (!dst_vxlan)
1956                                 goto tx_error;
1957                         vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1958                         return;
1959                 }
1960
1961                 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1962                 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1963                 md.vni = htonl(vni << 8);
1964                 md.gbp = skb->mark;
1965
1966                 err = vxlan_xmit_skb(rt, sk, skb, fl4.saddr,
1967                                      dst->sin.sin_addr.s_addr, tos, ttl, df,
1968                                      src_port, dst_port, &md,
1969                                      !net_eq(vxlan->net, dev_net(vxlan->dev)),
1970                                      vxlan->flags);
1971                 if (err < 0) {
1972                         /* skb is already freed. */
1973                         skb = NULL;
1974                         goto rt_tx_error;
1975                 }
1976
1977                 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1978 #if IS_ENABLED(CONFIG_IPV6)
1979         } else {
1980                 struct dst_entry *ndst;
1981                 struct flowi6 fl6;
1982                 u32 flags;
1983
1984                 memset(&fl6, 0, sizeof(fl6));
1985                 fl6.flowi6_oif = rdst->remote_ifindex;
1986                 fl6.daddr = dst->sin6.sin6_addr;
1987                 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
1988                 fl6.flowi6_proto = IPPROTO_UDP;
1989
1990                 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1991                         netdev_dbg(dev, "no route to %pI6\n",
1992                                    &dst->sin6.sin6_addr);
1993                         dev->stats.tx_carrier_errors++;
1994                         goto tx_error;
1995                 }
1996
1997                 if (ndst->dev == dev) {
1998                         netdev_dbg(dev, "circular route to %pI6\n",
1999                                    &dst->sin6.sin6_addr);
2000                         dst_release(ndst);
2001                         dev->stats.collisions++;
2002                         goto tx_error;
2003                 }
2004
2005                 /* Bypass encapsulation if the destination is local */
2006                 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2007                 if (flags & RTF_LOCAL &&
2008                     !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2009                         struct vxlan_dev *dst_vxlan;
2010
2011                         dst_release(ndst);
2012                         dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2013                                                    dst->sa.sa_family, dst_port,
2014                                                    vxlan->flags);
2015                         if (!dst_vxlan)
2016                                 goto tx_error;
2017                         vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2018                         return;
2019                 }
2020
2021                 ttl = ttl ? : ip6_dst_hoplimit(ndst);
2022                 md.vni = htonl(vni << 8);
2023                 md.gbp = skb->mark;
2024
2025                 err = vxlan6_xmit_skb(ndst, sk, skb, dev, &fl6.saddr, &fl6.daddr,
2026                                       0, ttl, src_port, dst_port, &md,
2027                                       !net_eq(vxlan->net, dev_net(vxlan->dev)),
2028                                       vxlan->flags);
2029 #endif
2030         }
2031
2032         return;
2033
2034 drop:
2035         dev->stats.tx_dropped++;
2036         goto tx_free;
2037
2038 rt_tx_error:
2039         ip_rt_put(rt);
2040 tx_error:
2041         dev->stats.tx_errors++;
2042 tx_free:
2043         dev_kfree_skb(skb);
2044 }
2045
2046 /* Transmit local packets over Vxlan
2047  *
2048  * Outer IP header inherits ECN and DF from inner header.
2049  * Outer UDP destination is the VXLAN assigned port.
2050  *           source port is based on hash of flow
2051  */
2052 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2053 {
2054         struct vxlan_dev *vxlan = netdev_priv(dev);
2055         struct ethhdr *eth;
2056         bool did_rsc = false;
2057         struct vxlan_rdst *rdst, *fdst = NULL;
2058         struct vxlan_fdb *f;
2059
2060         skb_reset_mac_header(skb);
2061         eth = eth_hdr(skb);
2062
2063         if ((vxlan->flags & VXLAN_F_PROXY)) {
2064                 if (ntohs(eth->h_proto) == ETH_P_ARP)
2065                         return arp_reduce(dev, skb);
2066 #if IS_ENABLED(CONFIG_IPV6)
2067                 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2068                          pskb_may_pull(skb, sizeof(struct ipv6hdr)
2069                                        + sizeof(struct nd_msg)) &&
2070                          ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2071                                 struct nd_msg *msg;
2072
2073                                 msg = (struct nd_msg *)skb_transport_header(skb);
2074                                 if (msg->icmph.icmp6_code == 0 &&
2075                                     msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2076                                         return neigh_reduce(dev, skb);
2077                 }
2078                 eth = eth_hdr(skb);
2079 #endif
2080         }
2081
2082         f = vxlan_find_mac(vxlan, eth->h_dest);
2083         did_rsc = false;
2084
2085         if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
2086             (ntohs(eth->h_proto) == ETH_P_IP ||
2087              ntohs(eth->h_proto) == ETH_P_IPV6)) {
2088                 did_rsc = route_shortcircuit(dev, skb);
2089                 if (did_rsc)
2090                         f = vxlan_find_mac(vxlan, eth->h_dest);
2091         }
2092
2093         if (f == NULL) {
2094                 f = vxlan_find_mac(vxlan, all_zeros_mac);
2095                 if (f == NULL) {
2096                         if ((vxlan->flags & VXLAN_F_L2MISS) &&
2097                             !is_multicast_ether_addr(eth->h_dest))
2098                                 vxlan_fdb_miss(vxlan, eth->h_dest);
2099
2100                         dev->stats.tx_dropped++;
2101                         kfree_skb(skb);
2102                         return NETDEV_TX_OK;
2103                 }
2104         }
2105
2106         list_for_each_entry_rcu(rdst, &f->remotes, list) {
2107                 struct sk_buff *skb1;
2108
2109                 if (!fdst) {
2110                         fdst = rdst;
2111                         continue;
2112                 }
2113                 skb1 = skb_clone(skb, GFP_ATOMIC);
2114                 if (skb1)
2115                         vxlan_xmit_one(skb1, dev, rdst, did_rsc);
2116         }
2117
2118         if (fdst)
2119                 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2120         else
2121                 kfree_skb(skb);
2122         return NETDEV_TX_OK;
2123 }
2124
2125 /* Walk the forwarding table and purge stale entries */
2126 static void vxlan_cleanup(unsigned long arg)
2127 {
2128         struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2129         unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2130         unsigned int h;
2131
2132         if (!netif_running(vxlan->dev))
2133                 return;
2134
2135         spin_lock_bh(&vxlan->hash_lock);
2136         for (h = 0; h < FDB_HASH_SIZE; ++h) {
2137                 struct hlist_node *p, *n;
2138                 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2139                         struct vxlan_fdb *f
2140                                 = container_of(p, struct vxlan_fdb, hlist);
2141                         unsigned long timeout;
2142
2143                         if (f->state & NUD_PERMANENT)
2144                                 continue;
2145
2146                         timeout = f->used + vxlan->age_interval * HZ;
2147                         if (time_before_eq(timeout, jiffies)) {
2148                                 netdev_dbg(vxlan->dev,
2149                                            "garbage collect %pM\n",
2150                                            f->eth_addr);
2151                                 f->state = NUD_STALE;
2152                                 vxlan_fdb_destroy(vxlan, f);
2153                         } else if (time_before(timeout, next_timer))
2154                                 next_timer = timeout;
2155                 }
2156         }
2157         spin_unlock_bh(&vxlan->hash_lock);
2158
2159         mod_timer(&vxlan->age_timer, next_timer);
2160 }
2161
2162 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2163 {
2164         struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2165         __u32 vni = vxlan->default_dst.remote_vni;
2166
2167         vxlan->vn_sock = vs;
2168         spin_lock(&vn->sock_lock);
2169         hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2170         spin_unlock(&vn->sock_lock);
2171 }
2172
2173 /* Setup stats when device is created */
2174 static int vxlan_init(struct net_device *dev)
2175 {
2176         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
2177         if (!dev->tstats)
2178                 return -ENOMEM;
2179
2180         return 0;
2181 }
2182
2183 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
2184 {
2185         struct vxlan_fdb *f;
2186
2187         spin_lock_bh(&vxlan->hash_lock);
2188         f = __vxlan_find_mac(vxlan, all_zeros_mac);
2189         if (f)
2190                 vxlan_fdb_destroy(vxlan, f);
2191         spin_unlock_bh(&vxlan->hash_lock);
2192 }
2193
2194 static void vxlan_uninit(struct net_device *dev)
2195 {
2196         struct vxlan_dev *vxlan = netdev_priv(dev);
2197
2198         vxlan_fdb_delete_default(vxlan);
2199
2200         free_percpu(dev->tstats);
2201 }
2202
2203 /* Start ageing timer and join group when device is brought up */
2204 static int vxlan_open(struct net_device *dev)
2205 {
2206         struct vxlan_dev *vxlan = netdev_priv(dev);
2207         struct vxlan_sock *vs;
2208         int ret = 0;
2209
2210         vs = vxlan_sock_add(vxlan->net, vxlan->dst_port, vxlan_rcv, NULL,
2211                             false, vxlan->flags);
2212         if (IS_ERR(vs))
2213                 return PTR_ERR(vs);
2214
2215         vxlan_vs_add_dev(vs, vxlan);
2216
2217         if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
2218                 ret = vxlan_igmp_join(vxlan);
2219                 if (ret) {
2220                         vxlan_sock_release(vs);
2221                         return ret;
2222                 }
2223         }
2224
2225         if (vxlan->age_interval)
2226                 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2227
2228         return ret;
2229 }
2230
2231 /* Purge the forwarding table */
2232 static void vxlan_flush(struct vxlan_dev *vxlan)
2233 {
2234         unsigned int h;
2235
2236         spin_lock_bh(&vxlan->hash_lock);
2237         for (h = 0; h < FDB_HASH_SIZE; ++h) {
2238                 struct hlist_node *p, *n;
2239                 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2240                         struct vxlan_fdb *f
2241                                 = container_of(p, struct vxlan_fdb, hlist);
2242                         /* the all_zeros_mac entry is deleted at vxlan_uninit */
2243                         if (!is_zero_ether_addr(f->eth_addr))
2244                                 vxlan_fdb_destroy(vxlan, f);
2245                 }
2246         }
2247         spin_unlock_bh(&vxlan->hash_lock);
2248 }
2249
2250 /* Cleanup timer and forwarding table on shutdown */
2251 static int vxlan_stop(struct net_device *dev)
2252 {
2253         struct vxlan_dev *vxlan = netdev_priv(dev);
2254         struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2255         struct vxlan_sock *vs = vxlan->vn_sock;
2256         int ret = 0;
2257
2258         if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
2259             !vxlan_group_used(vn, vxlan))
2260                 ret = vxlan_igmp_leave(vxlan);
2261
2262         del_timer_sync(&vxlan->age_timer);
2263
2264         vxlan_flush(vxlan);
2265         vxlan_sock_release(vs);
2266
2267         return ret;
2268 }
2269
2270 /* Stub, nothing needs to be done. */
2271 static void vxlan_set_multicast_list(struct net_device *dev)
2272 {
2273 }
2274
2275 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2276 {
2277         struct vxlan_dev *vxlan = netdev_priv(dev);
2278         struct vxlan_rdst *dst = &vxlan->default_dst;
2279         struct net_device *lowerdev;
2280         int max_mtu;
2281
2282         lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
2283         if (lowerdev == NULL)
2284                 return eth_change_mtu(dev, new_mtu);
2285
2286         if (dst->remote_ip.sa.sa_family == AF_INET6)
2287                 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2288         else
2289                 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2290
2291         if (new_mtu < 68 || new_mtu > max_mtu)
2292                 return -EINVAL;
2293
2294         dev->mtu = new_mtu;
2295         return 0;
2296 }
2297
2298 static const struct net_device_ops vxlan_netdev_ops = {
2299         .ndo_init               = vxlan_init,
2300         .ndo_uninit             = vxlan_uninit,
2301         .ndo_open               = vxlan_open,
2302         .ndo_stop               = vxlan_stop,
2303         .ndo_start_xmit         = vxlan_xmit,
2304         .ndo_get_stats64        = ip_tunnel_get_stats64,
2305         .ndo_set_rx_mode        = vxlan_set_multicast_list,
2306         .ndo_change_mtu         = vxlan_change_mtu,
2307         .ndo_validate_addr      = eth_validate_addr,
2308         .ndo_set_mac_address    = eth_mac_addr,
2309         .ndo_fdb_add            = vxlan_fdb_add,
2310         .ndo_fdb_del            = vxlan_fdb_delete,
2311         .ndo_fdb_dump           = vxlan_fdb_dump,
2312 };
2313
2314 /* Info for udev, that this is a virtual tunnel endpoint */
2315 static struct device_type vxlan_type = {
2316         .name = "vxlan",
2317 };
2318
2319 /* Calls the ndo_add_vxlan_port of the caller in order to
2320  * supply the listening VXLAN udp ports. Callers are expected
2321  * to implement the ndo_add_vxlan_port.
2322  */
2323 void vxlan_get_rx_port(struct net_device *dev)
2324 {
2325         struct vxlan_sock *vs;
2326         struct net *net = dev_net(dev);
2327         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2328         sa_family_t sa_family;
2329         __be16 port;
2330         unsigned int i;
2331
2332         spin_lock(&vn->sock_lock);
2333         for (i = 0; i < PORT_HASH_SIZE; ++i) {
2334                 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2335                         port = inet_sk(vs->sock->sk)->inet_sport;
2336                         sa_family = vs->sock->sk->sk_family;
2337                         dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2338                                                             port);
2339                 }
2340         }
2341         spin_unlock(&vn->sock_lock);
2342 }
2343 EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2344
2345 /* Initialize the device structure. */
2346 static void vxlan_setup(struct net_device *dev)
2347 {
2348         struct vxlan_dev *vxlan = netdev_priv(dev);
2349         unsigned int h;
2350
2351         eth_hw_addr_random(dev);
2352         ether_setup(dev);
2353         if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2354                 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
2355         else
2356                 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
2357
2358         dev->netdev_ops = &vxlan_netdev_ops;
2359         dev->destructor = free_netdev;
2360         SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2361
2362         dev->tx_queue_len = 0;
2363         dev->features   |= NETIF_F_LLTX;
2364         dev->features   |= NETIF_F_SG | NETIF_F_HW_CSUM;
2365         dev->features   |= NETIF_F_RXCSUM;
2366         dev->features   |= NETIF_F_GSO_SOFTWARE;
2367
2368         dev->vlan_features = dev->features;
2369         dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2370         dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2371         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
2372         dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
2373         netif_keep_dst(dev);
2374         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2375
2376         INIT_LIST_HEAD(&vxlan->next);
2377         spin_lock_init(&vxlan->hash_lock);
2378
2379         init_timer_deferrable(&vxlan->age_timer);
2380         vxlan->age_timer.function = vxlan_cleanup;
2381         vxlan->age_timer.data = (unsigned long) vxlan;
2382
2383         vxlan->dst_port = htons(vxlan_port);
2384
2385         vxlan->dev = dev;
2386
2387         for (h = 0; h < FDB_HASH_SIZE; ++h)
2388                 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2389 }
2390
2391 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2392         [IFLA_VXLAN_ID]         = { .type = NLA_U32 },
2393         [IFLA_VXLAN_GROUP]      = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
2394         [IFLA_VXLAN_GROUP6]     = { .len = sizeof(struct in6_addr) },
2395         [IFLA_VXLAN_LINK]       = { .type = NLA_U32 },
2396         [IFLA_VXLAN_LOCAL]      = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
2397         [IFLA_VXLAN_LOCAL6]     = { .len = sizeof(struct in6_addr) },
2398         [IFLA_VXLAN_TOS]        = { .type = NLA_U8 },
2399         [IFLA_VXLAN_TTL]        = { .type = NLA_U8 },
2400         [IFLA_VXLAN_LEARNING]   = { .type = NLA_U8 },
2401         [IFLA_VXLAN_AGEING]     = { .type = NLA_U32 },
2402         [IFLA_VXLAN_LIMIT]      = { .type = NLA_U32 },
2403         [IFLA_VXLAN_PORT_RANGE] = { .len  = sizeof(struct ifla_vxlan_port_range) },
2404         [IFLA_VXLAN_PROXY]      = { .type = NLA_U8 },
2405         [IFLA_VXLAN_RSC]        = { .type = NLA_U8 },
2406         [IFLA_VXLAN_L2MISS]     = { .type = NLA_U8 },
2407         [IFLA_VXLAN_L3MISS]     = { .type = NLA_U8 },
2408         [IFLA_VXLAN_PORT]       = { .type = NLA_U16 },
2409         [IFLA_VXLAN_UDP_CSUM]   = { .type = NLA_U8 },
2410         [IFLA_VXLAN_UDP_ZERO_CSUM6_TX]  = { .type = NLA_U8 },
2411         [IFLA_VXLAN_UDP_ZERO_CSUM6_RX]  = { .type = NLA_U8 },
2412         [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2413         [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
2414         [IFLA_VXLAN_GBP]        = { .type = NLA_FLAG, },
2415         [IFLA_VXLAN_REMCSUM_NOPARTIAL]  = { .type = NLA_FLAG },
2416 };
2417
2418 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2419 {
2420         if (tb[IFLA_ADDRESS]) {
2421                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2422                         pr_debug("invalid link address (not ethernet)\n");
2423                         return -EINVAL;
2424                 }
2425
2426                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2427                         pr_debug("invalid all zero ethernet address\n");
2428                         return -EADDRNOTAVAIL;
2429                 }
2430         }
2431
2432         if (!data)
2433                 return -EINVAL;
2434
2435         if (data[IFLA_VXLAN_ID]) {
2436                 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2437                 if (id >= VXLAN_VID_MASK)
2438                         return -ERANGE;
2439         }
2440
2441         if (data[IFLA_VXLAN_PORT_RANGE]) {
2442                 const struct ifla_vxlan_port_range *p
2443                         = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2444
2445                 if (ntohs(p->high) < ntohs(p->low)) {
2446                         pr_debug("port range %u .. %u not valid\n",
2447                                  ntohs(p->low), ntohs(p->high));
2448                         return -EINVAL;
2449                 }
2450         }
2451
2452         return 0;
2453 }
2454
2455 static void vxlan_get_drvinfo(struct net_device *netdev,
2456                               struct ethtool_drvinfo *drvinfo)
2457 {
2458         strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2459         strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2460 }
2461
2462 static const struct ethtool_ops vxlan_ethtool_ops = {
2463         .get_drvinfo    = vxlan_get_drvinfo,
2464         .get_link       = ethtool_op_get_link,
2465 };
2466
2467 static void vxlan_del_work(struct work_struct *work)
2468 {
2469         struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2470         udp_tunnel_sock_release(vs->sock);
2471         kfree_rcu(vs, rcu);
2472 }
2473
2474 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2475                                         __be16 port, u32 flags)
2476 {
2477         struct socket *sock;
2478         struct udp_port_cfg udp_conf;
2479         int err;
2480
2481         memset(&udp_conf, 0, sizeof(udp_conf));
2482
2483         if (ipv6) {
2484                 udp_conf.family = AF_INET6;
2485                 udp_conf.use_udp6_rx_checksums =
2486                     !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
2487         } else {
2488                 udp_conf.family = AF_INET;
2489         }
2490
2491         udp_conf.local_udp_port = port;
2492
2493         /* Open UDP socket */
2494         err = udp_sock_create(net, &udp_conf, &sock);
2495         if (err < 0)
2496                 return ERR_PTR(err);
2497
2498         return sock;
2499 }
2500
2501 /* Create new listen socket if needed */
2502 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2503                                               vxlan_rcv_t *rcv, void *data,
2504                                               u32 flags)
2505 {
2506         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2507         struct vxlan_sock *vs;
2508         struct socket *sock;
2509         unsigned int h;
2510         bool ipv6 = !!(flags & VXLAN_F_IPV6);
2511         struct udp_tunnel_sock_cfg tunnel_cfg;
2512
2513         vs = kzalloc(sizeof(*vs), GFP_KERNEL);
2514         if (!vs)
2515                 return ERR_PTR(-ENOMEM);
2516
2517         for (h = 0; h < VNI_HASH_SIZE; ++h)
2518                 INIT_HLIST_HEAD(&vs->vni_list[h]);
2519
2520         INIT_WORK(&vs->del_work, vxlan_del_work);
2521
2522         sock = vxlan_create_sock(net, ipv6, port, flags);
2523         if (IS_ERR(sock)) {
2524                 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
2525                         PTR_ERR(sock));
2526                 kfree(vs);
2527                 return ERR_CAST(sock);
2528         }
2529
2530         vs->sock = sock;
2531         atomic_set(&vs->refcnt, 1);
2532         vs->rcv = rcv;
2533         vs->data = data;
2534         vs->flags = (flags & VXLAN_F_RCV_FLAGS);
2535
2536         /* Initialize the vxlan udp offloads structure */
2537         vs->udp_offloads.port = port;
2538         vs->udp_offloads.callbacks.gro_receive  = vxlan_gro_receive;
2539         vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2540
2541         spin_lock(&vn->sock_lock);
2542         hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
2543         vxlan_notify_add_rx_port(vs);
2544         spin_unlock(&vn->sock_lock);
2545
2546         /* Mark socket as an encapsulation socket. */
2547         tunnel_cfg.sk_user_data = vs;
2548         tunnel_cfg.encap_type = 1;
2549         tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2550         tunnel_cfg.encap_destroy = NULL;
2551
2552         setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
2553
2554         return vs;
2555 }
2556
2557 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2558                                   vxlan_rcv_t *rcv, void *data,
2559                                   bool no_share, u32 flags)
2560 {
2561         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2562         struct vxlan_sock *vs;
2563         bool ipv6 = flags & VXLAN_F_IPV6;
2564
2565         if (!no_share) {
2566                 spin_lock(&vn->sock_lock);
2567                 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port,
2568                                      flags);
2569                 if (vs && vs->rcv == rcv) {
2570                         if (!atomic_add_unless(&vs->refcnt, 1, 0))
2571                                 vs = ERR_PTR(-EBUSY);
2572                         spin_unlock(&vn->sock_lock);
2573                         return vs;
2574                 }
2575                 spin_unlock(&vn->sock_lock);
2576         }
2577
2578         return vxlan_socket_create(net, port, rcv, data, flags);
2579 }
2580 EXPORT_SYMBOL_GPL(vxlan_sock_add);
2581
2582 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2583                          struct nlattr *tb[], struct nlattr *data[])
2584 {
2585         struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
2586         struct vxlan_dev *vxlan = netdev_priv(dev);
2587         struct vxlan_rdst *dst = &vxlan->default_dst;
2588         __u32 vni;
2589         int err;
2590         bool use_ipv6 = false;
2591
2592         if (!data[IFLA_VXLAN_ID])
2593                 return -EINVAL;
2594
2595         vxlan->net = src_net;
2596
2597         vni = nla_get_u32(data[IFLA_VXLAN_ID]);
2598         dst->remote_vni = vni;
2599
2600         /* Unless IPv6 is explicitly requested, assume IPv4 */
2601         dst->remote_ip.sa.sa_family = AF_INET;
2602         if (data[IFLA_VXLAN_GROUP]) {
2603                 dst->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
2604         } else if (data[IFLA_VXLAN_GROUP6]) {
2605                 if (!IS_ENABLED(CONFIG_IPV6))
2606                         return -EPFNOSUPPORT;
2607
2608                 dst->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
2609                 dst->remote_ip.sa.sa_family = AF_INET6;
2610                 use_ipv6 = true;
2611         }
2612
2613         if (data[IFLA_VXLAN_LOCAL]) {
2614                 vxlan->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
2615                 vxlan->saddr.sa.sa_family = AF_INET;
2616         } else if (data[IFLA_VXLAN_LOCAL6]) {
2617                 if (!IS_ENABLED(CONFIG_IPV6))
2618                         return -EPFNOSUPPORT;
2619
2620                 /* TODO: respect scope id */
2621                 vxlan->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
2622                 vxlan->saddr.sa.sa_family = AF_INET6;
2623                 use_ipv6 = true;
2624         }
2625
2626         if (data[IFLA_VXLAN_LINK] &&
2627             (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
2628                 struct net_device *lowerdev
2629                          = __dev_get_by_index(src_net, dst->remote_ifindex);
2630
2631                 if (!lowerdev) {
2632                         pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
2633                         return -ENODEV;
2634                 }
2635
2636 #if IS_ENABLED(CONFIG_IPV6)
2637                 if (use_ipv6) {
2638                         struct inet6_dev *idev = __in6_dev_get(lowerdev);
2639                         if (idev && idev->cnf.disable_ipv6) {
2640                                 pr_info("IPv6 is disabled via sysctl\n");
2641                                 return -EPERM;
2642                         }
2643                         vxlan->flags |= VXLAN_F_IPV6;
2644                 }
2645 #endif
2646
2647                 if (!tb[IFLA_MTU])
2648                         dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2649
2650                 dev->needed_headroom = lowerdev->hard_header_len +
2651                                        (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2652         } else if (use_ipv6)
2653                 vxlan->flags |= VXLAN_F_IPV6;
2654
2655         if (data[IFLA_VXLAN_TOS])
2656                 vxlan->tos  = nla_get_u8(data[IFLA_VXLAN_TOS]);
2657
2658         if (data[IFLA_VXLAN_TTL])
2659                 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2660
2661         if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
2662                 vxlan->flags |= VXLAN_F_LEARN;
2663
2664         if (data[IFLA_VXLAN_AGEING])
2665                 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2666         else
2667                 vxlan->age_interval = FDB_AGE_DEFAULT;
2668
2669         if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2670                 vxlan->flags |= VXLAN_F_PROXY;
2671
2672         if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2673                 vxlan->flags |= VXLAN_F_RSC;
2674
2675         if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2676                 vxlan->flags |= VXLAN_F_L2MISS;
2677
2678         if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2679                 vxlan->flags |= VXLAN_F_L3MISS;
2680
2681         if (data[IFLA_VXLAN_LIMIT])
2682                 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2683
2684         if (data[IFLA_VXLAN_PORT_RANGE]) {
2685                 const struct ifla_vxlan_port_range *p
2686                         = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2687                 vxlan->port_min = ntohs(p->low);
2688                 vxlan->port_max = ntohs(p->high);
2689         }
2690
2691         if (data[IFLA_VXLAN_PORT])
2692                 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2693
2694         if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2695                 vxlan->flags |= VXLAN_F_UDP_CSUM;
2696
2697         if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2698             nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2699                 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2700
2701         if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2702             nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2703                 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2704
2705         if (data[IFLA_VXLAN_REMCSUM_TX] &&
2706             nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2707                 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2708
2709         if (data[IFLA_VXLAN_REMCSUM_RX] &&
2710             nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2711                 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2712
2713         if (data[IFLA_VXLAN_GBP])
2714                 vxlan->flags |= VXLAN_F_GBP;
2715
2716         if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL])
2717                 vxlan->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
2718
2719         if (vxlan_find_vni(src_net, vni, use_ipv6 ? AF_INET6 : AF_INET,
2720                            vxlan->dst_port, vxlan->flags)) {
2721                 pr_info("duplicate VNI %u\n", vni);
2722                 return -EEXIST;
2723         }
2724
2725         dev->ethtool_ops = &vxlan_ethtool_ops;
2726
2727         /* create an fdb entry for a valid default destination */
2728         if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2729                 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2730                                        &vxlan->default_dst.remote_ip,
2731                                        NUD_REACHABLE|NUD_PERMANENT,
2732                                        NLM_F_EXCL|NLM_F_CREATE,
2733                                        vxlan->dst_port,
2734                                        vxlan->default_dst.remote_vni,
2735                                        vxlan->default_dst.remote_ifindex,
2736                                        NTF_SELF);
2737                 if (err)
2738                         return err;
2739         }
2740
2741         err = register_netdevice(dev);
2742         if (err) {
2743                 vxlan_fdb_delete_default(vxlan);
2744                 return err;
2745         }
2746
2747         list_add(&vxlan->next, &vn->vxlan_list);
2748
2749         return 0;
2750 }
2751
2752 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2753 {
2754         struct vxlan_dev *vxlan = netdev_priv(dev);
2755         struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2756
2757         spin_lock(&vn->sock_lock);
2758         if (!hlist_unhashed(&vxlan->hlist))
2759                 hlist_del_rcu(&vxlan->hlist);
2760         spin_unlock(&vn->sock_lock);
2761
2762         list_del(&vxlan->next);
2763         unregister_netdevice_queue(dev, head);
2764 }
2765
2766 static size_t vxlan_get_size(const struct net_device *dev)
2767 {
2768
2769         return nla_total_size(sizeof(__u32)) +  /* IFLA_VXLAN_ID */
2770                 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2771                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2772                 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2773                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TTL */
2774                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TOS */
2775                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_LEARNING */
2776                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_PROXY */
2777                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_RSC */
2778                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L2MISS */
2779                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L3MISS */
2780                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2781                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2782                 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2783                 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2784                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2785                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2786                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2787                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2788                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
2789                 0;
2790 }
2791
2792 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2793 {
2794         const struct vxlan_dev *vxlan = netdev_priv(dev);
2795         const struct vxlan_rdst *dst = &vxlan->default_dst;
2796         struct ifla_vxlan_port_range ports = {
2797                 .low =  htons(vxlan->port_min),
2798                 .high = htons(vxlan->port_max),
2799         };
2800
2801         if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
2802                 goto nla_put_failure;
2803
2804         if (!vxlan_addr_any(&dst->remote_ip)) {
2805                 if (dst->remote_ip.sa.sa_family == AF_INET) {
2806                         if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
2807                                             dst->remote_ip.sin.sin_addr.s_addr))
2808                                 goto nla_put_failure;
2809 #if IS_ENABLED(CONFIG_IPV6)
2810                 } else {
2811                         if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
2812                                              &dst->remote_ip.sin6.sin6_addr))
2813                                 goto nla_put_failure;
2814 #endif
2815                 }
2816         }
2817
2818         if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
2819                 goto nla_put_failure;
2820
2821         if (!vxlan_addr_any(&vxlan->saddr)) {
2822                 if (vxlan->saddr.sa.sa_family == AF_INET) {
2823                         if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
2824                                             vxlan->saddr.sin.sin_addr.s_addr))
2825                                 goto nla_put_failure;
2826 #if IS_ENABLED(CONFIG_IPV6)
2827                 } else {
2828                         if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
2829                                              &vxlan->saddr.sin6.sin6_addr))
2830                                 goto nla_put_failure;
2831 #endif
2832                 }
2833         }
2834
2835         if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2836             nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
2837             nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2838                         !!(vxlan->flags & VXLAN_F_LEARN)) ||
2839             nla_put_u8(skb, IFLA_VXLAN_PROXY,
2840                         !!(vxlan->flags & VXLAN_F_PROXY)) ||
2841             nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2842             nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2843                         !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2844             nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2845                         !!(vxlan->flags & VXLAN_F_L3MISS)) ||
2846             nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
2847             nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2848             nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2849             nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2850                         !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2851             nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2852                         !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2853             nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
2854                         !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2855             nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2856                         !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2857             nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2858                         !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
2859                 goto nla_put_failure;
2860
2861         if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2862                 goto nla_put_failure;
2863
2864         if (vxlan->flags & VXLAN_F_GBP &&
2865             nla_put_flag(skb, IFLA_VXLAN_GBP))
2866                 goto nla_put_failure;
2867
2868         if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
2869             nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
2870                 goto nla_put_failure;
2871
2872         return 0;
2873
2874 nla_put_failure:
2875         return -EMSGSIZE;
2876 }
2877
2878 static struct net *vxlan_get_link_net(const struct net_device *dev)
2879 {
2880         struct vxlan_dev *vxlan = netdev_priv(dev);
2881
2882         return vxlan->net;
2883 }
2884
2885 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2886         .kind           = "vxlan",
2887         .maxtype        = IFLA_VXLAN_MAX,
2888         .policy         = vxlan_policy,
2889         .priv_size      = sizeof(struct vxlan_dev),
2890         .setup          = vxlan_setup,
2891         .validate       = vxlan_validate,
2892         .newlink        = vxlan_newlink,
2893         .dellink        = vxlan_dellink,
2894         .get_size       = vxlan_get_size,
2895         .fill_info      = vxlan_fill_info,
2896         .get_link_net   = vxlan_get_link_net,
2897 };
2898
2899 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2900                                              struct net_device *dev)
2901 {
2902         struct vxlan_dev *vxlan, *next;
2903         LIST_HEAD(list_kill);
2904
2905         list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2906                 struct vxlan_rdst *dst = &vxlan->default_dst;
2907
2908                 /* In case we created vxlan device with carrier
2909                  * and we loose the carrier due to module unload
2910                  * we also need to remove vxlan device. In other
2911                  * cases, it's not necessary and remote_ifindex
2912                  * is 0 here, so no matches.
2913                  */
2914                 if (dst->remote_ifindex == dev->ifindex)
2915                         vxlan_dellink(vxlan->dev, &list_kill);
2916         }
2917
2918         unregister_netdevice_many(&list_kill);
2919 }
2920
2921 static int vxlan_lowerdev_event(struct notifier_block *unused,
2922                                 unsigned long event, void *ptr)
2923 {
2924         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2925         struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2926
2927         if (event == NETDEV_UNREGISTER)
2928                 vxlan_handle_lowerdev_unregister(vn, dev);
2929
2930         return NOTIFY_DONE;
2931 }
2932
2933 static struct notifier_block vxlan_notifier_block __read_mostly = {
2934         .notifier_call = vxlan_lowerdev_event,
2935 };
2936
2937 static __net_init int vxlan_init_net(struct net *net)
2938 {
2939         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2940         unsigned int h;
2941
2942         INIT_LIST_HEAD(&vn->vxlan_list);
2943         spin_lock_init(&vn->sock_lock);
2944
2945         for (h = 0; h < PORT_HASH_SIZE; ++h)
2946                 INIT_HLIST_HEAD(&vn->sock_list[h]);
2947
2948         return 0;
2949 }
2950
2951 static void __net_exit vxlan_exit_net(struct net *net)
2952 {
2953         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2954         struct vxlan_dev *vxlan, *next;
2955         struct net_device *dev, *aux;
2956         LIST_HEAD(list);
2957
2958         rtnl_lock();
2959         for_each_netdev_safe(net, dev, aux)
2960                 if (dev->rtnl_link_ops == &vxlan_link_ops)
2961                         unregister_netdevice_queue(dev, &list);
2962
2963         list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2964                 /* If vxlan->dev is in the same netns, it has already been added
2965                  * to the list by the previous loop.
2966                  */
2967                 if (!net_eq(dev_net(vxlan->dev), net))
2968                         unregister_netdevice_queue(dev, &list);
2969         }
2970
2971         unregister_netdevice_many(&list);
2972         rtnl_unlock();
2973 }
2974
2975 static struct pernet_operations vxlan_net_ops = {
2976         .init = vxlan_init_net,
2977         .exit = vxlan_exit_net,
2978         .id   = &vxlan_net_id,
2979         .size = sizeof(struct vxlan_net),
2980 };
2981
2982 static int __init vxlan_init_module(void)
2983 {
2984         int rc;
2985
2986         vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2987         if (!vxlan_wq)
2988                 return -ENOMEM;
2989
2990         get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2991
2992         rc = register_pernet_subsys(&vxlan_net_ops);
2993         if (rc)
2994                 goto out1;
2995
2996         rc = register_netdevice_notifier(&vxlan_notifier_block);
2997         if (rc)
2998                 goto out2;
2999
3000         rc = rtnl_link_register(&vxlan_link_ops);
3001         if (rc)
3002                 goto out3;
3003
3004         return 0;
3005 out3:
3006         unregister_netdevice_notifier(&vxlan_notifier_block);
3007 out2:
3008         unregister_pernet_subsys(&vxlan_net_ops);
3009 out1:
3010         destroy_workqueue(vxlan_wq);
3011         return rc;
3012 }
3013 late_initcall(vxlan_init_module);
3014
3015 static void __exit vxlan_cleanup_module(void)
3016 {
3017         rtnl_link_unregister(&vxlan_link_ops);
3018         unregister_netdevice_notifier(&vxlan_notifier_block);
3019         destroy_workqueue(vxlan_wq);
3020         unregister_pernet_subsys(&vxlan_net_ops);
3021         /* rcu_barrier() is called by netns */
3022 }
3023 module_exit(vxlan_cleanup_module);
3024
3025 MODULE_LICENSE("GPL");
3026 MODULE_VERSION(VXLAN_VERSION);
3027 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
3028 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
3029 MODULE_ALIAS_RTNL_LINK("vxlan");