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