]> Pileus Git - ~andy/linux/blob - drivers/net/vxlan.c
vxlan: Add vxlan recv demux.
[~andy/linux] / 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  * TODO
11  *  - IPv6 (not in RFC)
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/rculist.h>
23 #include <linux/netdevice.h>
24 #include <linux/in.h>
25 #include <linux/ip.h>
26 #include <linux/udp.h>
27 #include <linux/igmp.h>
28 #include <linux/etherdevice.h>
29 #include <linux/if_ether.h>
30 #include <linux/hash.h>
31 #include <linux/ethtool.h>
32 #include <net/arp.h>
33 #include <net/ndisc.h>
34 #include <net/ip.h>
35 #include <net/ip_tunnels.h>
36 #include <net/icmp.h>
37 #include <net/udp.h>
38 #include <net/rtnetlink.h>
39 #include <net/route.h>
40 #include <net/dsfield.h>
41 #include <net/inet_ecn.h>
42 #include <net/net_namespace.h>
43 #include <net/netns/generic.h>
44
45 #define VXLAN_VERSION   "0.1"
46
47 #define PORT_HASH_BITS  8
48 #define PORT_HASH_SIZE  (1<<PORT_HASH_BITS)
49 #define VNI_HASH_BITS   10
50 #define VNI_HASH_SIZE   (1<<VNI_HASH_BITS)
51 #define FDB_HASH_BITS   8
52 #define FDB_HASH_SIZE   (1<<FDB_HASH_BITS)
53 #define FDB_AGE_DEFAULT 300 /* 5 min */
54 #define FDB_AGE_INTERVAL (10 * HZ)      /* rescan interval */
55
56 #define VXLAN_N_VID     (1u << 24)
57 #define VXLAN_VID_MASK  (VXLAN_N_VID - 1)
58 /* IP header + UDP + VXLAN + Ethernet header */
59 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
60 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
61
62 #define VXLAN_FLAGS 0x08000000  /* struct vxlanhdr.vx_flags required value. */
63
64 /* VXLAN protocol header */
65 struct vxlanhdr {
66         __be32 vx_flags;
67         __be32 vx_vni;
68 };
69
70 /* UDP port for VXLAN traffic.
71  * The IANA assigned port is 4789, but the Linux default is 8472
72  * for compatibility with early adopters.
73  */
74 static unsigned short vxlan_port __read_mostly = 8472;
75 module_param_named(udp_port, vxlan_port, ushort, 0444);
76 MODULE_PARM_DESC(udp_port, "Destination UDP port");
77
78 static bool log_ecn_error = true;
79 module_param(log_ecn_error, bool, 0644);
80 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
81
82 static int vxlan_net_id;
83
84 static const u8 all_zeros_mac[ETH_ALEN];
85
86 struct vxlan_sock;
87 typedef void (vxlan_rcv_t)(struct vxlan_sock *vh, struct sk_buff *skb, __be32 key);
88
89 /* per UDP socket information */
90 struct vxlan_sock {
91         vxlan_rcv_t      *rcv;
92         struct hlist_node hlist;
93         struct rcu_head   rcu;
94         struct work_struct del_work;
95         atomic_t          refcnt;
96         struct socket     *sock;
97         struct hlist_head vni_list[VNI_HASH_SIZE];
98 };
99
100 /* per-network namespace private data for this module */
101 struct vxlan_net {
102         struct list_head  vxlan_list;
103         struct hlist_head sock_list[PORT_HASH_SIZE];
104         spinlock_t        sock_lock;
105 };
106
107 struct vxlan_rdst {
108         __be32                   remote_ip;
109         __be16                   remote_port;
110         u32                      remote_vni;
111         u32                      remote_ifindex;
112         struct list_head         list;
113         struct rcu_head          rcu;
114 };
115
116 /* Forwarding table entry */
117 struct vxlan_fdb {
118         struct hlist_node hlist;        /* linked list of entries */
119         struct rcu_head   rcu;
120         unsigned long     updated;      /* jiffies */
121         unsigned long     used;
122         struct list_head  remotes;
123         u16               state;        /* see ndm_state */
124         u8                flags;        /* see ndm_flags */
125         u8                eth_addr[ETH_ALEN];
126 };
127
128 /* Pseudo network device */
129 struct vxlan_dev {
130         struct hlist_node hlist;        /* vni hash table */
131         struct list_head  next;         /* vxlan's per namespace list */
132         struct vxlan_sock *vn_sock;     /* listening socket */
133         struct net_device *dev;
134         struct vxlan_rdst default_dst;  /* default destination */
135         __be32            saddr;        /* source address */
136         __be16            dst_port;
137         __u16             port_min;     /* source port range */
138         __u16             port_max;
139         __u8              tos;          /* TOS override */
140         __u8              ttl;
141         u32               flags;        /* VXLAN_F_* below */
142
143         struct work_struct sock_work;
144         struct work_struct igmp_join;
145         struct work_struct igmp_leave;
146
147         unsigned long     age_interval;
148         struct timer_list age_timer;
149         spinlock_t        hash_lock;
150         unsigned int      addrcnt;
151         unsigned int      addrmax;
152
153         struct hlist_head fdb_head[FDB_HASH_SIZE];
154 };
155
156 #define VXLAN_F_LEARN   0x01
157 #define VXLAN_F_PROXY   0x02
158 #define VXLAN_F_RSC     0x04
159 #define VXLAN_F_L2MISS  0x08
160 #define VXLAN_F_L3MISS  0x10
161
162 /* salt for hash table */
163 static u32 vxlan_salt __read_mostly;
164 static struct workqueue_struct *vxlan_wq;
165
166 static void vxlan_sock_work(struct work_struct *work);
167
168 /* Virtual Network hash table head */
169 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
170 {
171         return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
172 }
173
174 /* Socket hash table head */
175 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
176 {
177         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
178
179         return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
180 }
181
182 /* First remote destination for a forwarding entry.
183  * Guaranteed to be non-NULL because remotes are never deleted.
184  */
185 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
186 {
187         return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
188 }
189
190 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
191 {
192         return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
193 }
194
195 /* Find VXLAN socket based on network namespace and UDP port */
196 static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
197 {
198         struct vxlan_sock *vs;
199
200         hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
201                 if (inet_sk(vs->sock->sk)->inet_sport == port)
202                         return vs;
203         }
204         return NULL;
205 }
206
207 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
208 {
209         struct vxlan_dev *vxlan;
210
211         hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
212                 if (vxlan->default_dst.remote_vni == id)
213                         return vxlan;
214         }
215
216         return NULL;
217 }
218
219 /* Look up VNI in a per net namespace table */
220 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
221 {
222         struct vxlan_sock *vs;
223
224         vs = vxlan_find_sock(net, port);
225         if (!vs)
226                 return NULL;
227
228         return vxlan_vs_find_vni(vs, id);
229 }
230
231 /* Fill in neighbour message in skbuff. */
232 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
233                           const struct vxlan_fdb *fdb,
234                           u32 portid, u32 seq, int type, unsigned int flags,
235                           const struct vxlan_rdst *rdst)
236 {
237         unsigned long now = jiffies;
238         struct nda_cacheinfo ci;
239         struct nlmsghdr *nlh;
240         struct ndmsg *ndm;
241         bool send_ip, send_eth;
242
243         nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
244         if (nlh == NULL)
245                 return -EMSGSIZE;
246
247         ndm = nlmsg_data(nlh);
248         memset(ndm, 0, sizeof(*ndm));
249
250         send_eth = send_ip = true;
251
252         if (type == RTM_GETNEIGH) {
253                 ndm->ndm_family = AF_INET;
254                 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
255                 send_eth = !is_zero_ether_addr(fdb->eth_addr);
256         } else
257                 ndm->ndm_family = AF_BRIDGE;
258         ndm->ndm_state = fdb->state;
259         ndm->ndm_ifindex = vxlan->dev->ifindex;
260         ndm->ndm_flags = fdb->flags;
261         ndm->ndm_type = NDA_DST;
262
263         if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
264                 goto nla_put_failure;
265
266         if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
267                 goto nla_put_failure;
268
269         if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
270             nla_put_be16(skb, NDA_PORT, rdst->remote_port))
271                 goto nla_put_failure;
272         if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
273             nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
274                 goto nla_put_failure;
275         if (rdst->remote_ifindex &&
276             nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
277                 goto nla_put_failure;
278
279         ci.ndm_used      = jiffies_to_clock_t(now - fdb->used);
280         ci.ndm_confirmed = 0;
281         ci.ndm_updated   = jiffies_to_clock_t(now - fdb->updated);
282         ci.ndm_refcnt    = 0;
283
284         if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
285                 goto nla_put_failure;
286
287         return nlmsg_end(skb, nlh);
288
289 nla_put_failure:
290         nlmsg_cancel(skb, nlh);
291         return -EMSGSIZE;
292 }
293
294 static inline size_t vxlan_nlmsg_size(void)
295 {
296         return NLMSG_ALIGN(sizeof(struct ndmsg))
297                 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
298                 + nla_total_size(sizeof(__be32)) /* NDA_DST */
299                 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
300                 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
301                 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
302                 + nla_total_size(sizeof(struct nda_cacheinfo));
303 }
304
305 static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
306                              struct vxlan_fdb *fdb, int type)
307 {
308         struct net *net = dev_net(vxlan->dev);
309         struct sk_buff *skb;
310         int err = -ENOBUFS;
311
312         skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
313         if (skb == NULL)
314                 goto errout;
315
316         err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
317                              first_remote_rtnl(fdb));
318         if (err < 0) {
319                 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
320                 WARN_ON(err == -EMSGSIZE);
321                 kfree_skb(skb);
322                 goto errout;
323         }
324
325         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
326         return;
327 errout:
328         if (err < 0)
329                 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
330 }
331
332 static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
333 {
334         struct vxlan_dev *vxlan = netdev_priv(dev);
335         struct vxlan_fdb f = {
336                 .state = NUD_STALE,
337         };
338         struct vxlan_rdst remote = {
339                 .remote_ip = ipa, /* goes to NDA_DST */
340                 .remote_vni = VXLAN_N_VID,
341         };
342
343         INIT_LIST_HEAD(&f.remotes);
344         list_add_rcu(&remote.list, &f.remotes);
345
346         vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
347 }
348
349 static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
350 {
351         struct vxlan_fdb f = {
352                 .state = NUD_STALE,
353         };
354
355         INIT_LIST_HEAD(&f.remotes);
356         memcpy(f.eth_addr, eth_addr, ETH_ALEN);
357
358         vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
359 }
360
361 /* Hash Ethernet address */
362 static u32 eth_hash(const unsigned char *addr)
363 {
364         u64 value = get_unaligned((u64 *)addr);
365
366         /* only want 6 bytes */
367 #ifdef __BIG_ENDIAN
368         value >>= 16;
369 #else
370         value <<= 16;
371 #endif
372         return hash_64(value, FDB_HASH_BITS);
373 }
374
375 /* Hash chain to use given mac address */
376 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
377                                                 const u8 *mac)
378 {
379         return &vxlan->fdb_head[eth_hash(mac)];
380 }
381
382 /* Look up Ethernet address in forwarding table */
383 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
384                                         const u8 *mac)
385
386 {
387         struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
388         struct vxlan_fdb *f;
389
390         hlist_for_each_entry_rcu(f, head, hlist) {
391                 if (compare_ether_addr(mac, f->eth_addr) == 0)
392                         return f;
393         }
394
395         return NULL;
396 }
397
398 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
399                                         const u8 *mac)
400 {
401         struct vxlan_fdb *f;
402
403         f = __vxlan_find_mac(vxlan, mac);
404         if (f)
405                 f->used = jiffies;
406
407         return f;
408 }
409
410 /* caller should hold vxlan->hash_lock */
411 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
412                                               __be32 ip, __be16 port,
413                                               __u32 vni, __u32 ifindex)
414 {
415         struct vxlan_rdst *rd;
416
417         list_for_each_entry(rd, &f->remotes, list) {
418                 if (rd->remote_ip == ip &&
419                     rd->remote_port == port &&
420                     rd->remote_vni == vni &&
421                     rd->remote_ifindex == ifindex)
422                         return rd;
423         }
424
425         return NULL;
426 }
427
428 /* Replace destination of unicast mac */
429 static int vxlan_fdb_replace(struct vxlan_fdb *f,
430                             __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
431 {
432         struct vxlan_rdst *rd;
433
434         rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
435         if (rd)
436                 return 0;
437
438         rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
439         if (!rd)
440                 return 0;
441         rd->remote_ip = ip;
442         rd->remote_port = port;
443         rd->remote_vni = vni;
444         rd->remote_ifindex = ifindex;
445         return 1;
446 }
447
448 /* Add/update destinations for multicast */
449 static int vxlan_fdb_append(struct vxlan_fdb *f,
450                             __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
451 {
452         struct vxlan_rdst *rd;
453
454         rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
455         if (rd)
456                 return 0;
457
458         rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
459         if (rd == NULL)
460                 return -ENOBUFS;
461         rd->remote_ip = ip;
462         rd->remote_port = port;
463         rd->remote_vni = vni;
464         rd->remote_ifindex = ifindex;
465
466         list_add_tail_rcu(&rd->list, &f->remotes);
467
468         return 1;
469 }
470
471 /* Add new entry to forwarding table -- assumes lock held */
472 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
473                             const u8 *mac, __be32 ip,
474                             __u16 state, __u16 flags,
475                             __be16 port, __u32 vni, __u32 ifindex,
476                             __u8 ndm_flags)
477 {
478         struct vxlan_fdb *f;
479         int notify = 0;
480
481         f = __vxlan_find_mac(vxlan, mac);
482         if (f) {
483                 if (flags & NLM_F_EXCL) {
484                         netdev_dbg(vxlan->dev,
485                                    "lost race to create %pM\n", mac);
486                         return -EEXIST;
487                 }
488                 if (f->state != state) {
489                         f->state = state;
490                         f->updated = jiffies;
491                         notify = 1;
492                 }
493                 if (f->flags != ndm_flags) {
494                         f->flags = ndm_flags;
495                         f->updated = jiffies;
496                         notify = 1;
497                 }
498                 if ((flags & NLM_F_REPLACE)) {
499                         /* Only change unicasts */
500                         if (!(is_multicast_ether_addr(f->eth_addr) ||
501                              is_zero_ether_addr(f->eth_addr))) {
502                                 int rc = vxlan_fdb_replace(f, ip, port, vni,
503                                                            ifindex);
504
505                                 if (rc < 0)
506                                         return rc;
507                                 notify |= rc;
508                         } else
509                                 return -EOPNOTSUPP;
510                 }
511                 if ((flags & NLM_F_APPEND) &&
512                     (is_multicast_ether_addr(f->eth_addr) ||
513                      is_zero_ether_addr(f->eth_addr))) {
514                         int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
515
516                         if (rc < 0)
517                                 return rc;
518                         notify |= rc;
519                 }
520         } else {
521                 if (!(flags & NLM_F_CREATE))
522                         return -ENOENT;
523
524                 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
525                         return -ENOSPC;
526
527                 /* Disallow replace to add a multicast entry */
528                 if ((flags & NLM_F_REPLACE) &&
529                     (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
530                         return -EOPNOTSUPP;
531
532                 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
533                 f = kmalloc(sizeof(*f), GFP_ATOMIC);
534                 if (!f)
535                         return -ENOMEM;
536
537                 notify = 1;
538                 f->state = state;
539                 f->flags = ndm_flags;
540                 f->updated = f->used = jiffies;
541                 INIT_LIST_HEAD(&f->remotes);
542                 memcpy(f->eth_addr, mac, ETH_ALEN);
543
544                 vxlan_fdb_append(f, ip, port, vni, ifindex);
545
546                 ++vxlan->addrcnt;
547                 hlist_add_head_rcu(&f->hlist,
548                                    vxlan_fdb_head(vxlan, mac));
549         }
550
551         if (notify)
552                 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
553
554         return 0;
555 }
556
557 static void vxlan_fdb_free_rdst(struct rcu_head *head)
558 {
559         struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
560         kfree(rd);
561 }
562
563 static void vxlan_fdb_free(struct rcu_head *head)
564 {
565         struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
566         struct vxlan_rdst *rd, *nd;
567
568         list_for_each_entry_safe(rd, nd, &f->remotes, list)
569                 kfree(rd);
570         kfree(f);
571 }
572
573 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
574 {
575         netdev_dbg(vxlan->dev,
576                     "delete %pM\n", f->eth_addr);
577
578         --vxlan->addrcnt;
579         vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
580
581         hlist_del_rcu(&f->hlist);
582         call_rcu(&f->rcu, vxlan_fdb_free);
583 }
584
585 static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
586                            __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
587 {
588         struct net *net = dev_net(vxlan->dev);
589
590         if (tb[NDA_DST]) {
591                 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
592                         return -EAFNOSUPPORT;
593
594                 *ip = nla_get_be32(tb[NDA_DST]);
595         } else {
596                 *ip = htonl(INADDR_ANY);
597         }
598
599         if (tb[NDA_PORT]) {
600                 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
601                         return -EINVAL;
602                 *port = nla_get_be16(tb[NDA_PORT]);
603         } else {
604                 *port = vxlan->dst_port;
605         }
606
607         if (tb[NDA_VNI]) {
608                 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
609                         return -EINVAL;
610                 *vni = nla_get_u32(tb[NDA_VNI]);
611         } else {
612                 *vni = vxlan->default_dst.remote_vni;
613         }
614
615         if (tb[NDA_IFINDEX]) {
616                 struct net_device *tdev;
617
618                 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
619                         return -EINVAL;
620                 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
621                 tdev = dev_get_by_index(net, *ifindex);
622                 if (!tdev)
623                         return -EADDRNOTAVAIL;
624                 dev_put(tdev);
625         } else {
626                 *ifindex = 0;
627         }
628
629         return 0;
630 }
631
632 /* Add static entry (via netlink) */
633 static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
634                          struct net_device *dev,
635                          const unsigned char *addr, u16 flags)
636 {
637         struct vxlan_dev *vxlan = netdev_priv(dev);
638         /* struct net *net = dev_net(vxlan->dev); */
639         __be32 ip;
640         __be16 port;
641         u32 vni, ifindex;
642         int err;
643
644         if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
645                 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
646                         ndm->ndm_state);
647                 return -EINVAL;
648         }
649
650         if (tb[NDA_DST] == NULL)
651                 return -EINVAL;
652
653         err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
654         if (err)
655                 return err;
656
657         spin_lock_bh(&vxlan->hash_lock);
658         err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
659                                port, vni, ifindex, ndm->ndm_flags);
660         spin_unlock_bh(&vxlan->hash_lock);
661
662         return err;
663 }
664
665 /* Delete entry (via netlink) */
666 static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
667                             struct net_device *dev,
668                             const unsigned char *addr)
669 {
670         struct vxlan_dev *vxlan = netdev_priv(dev);
671         struct vxlan_fdb *f;
672         struct vxlan_rdst *rd = NULL;
673         __be32 ip;
674         __be16 port;
675         u32 vni, ifindex;
676         int err;
677
678         err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
679         if (err)
680                 return err;
681
682         err = -ENOENT;
683
684         spin_lock_bh(&vxlan->hash_lock);
685         f = vxlan_find_mac(vxlan, addr);
686         if (!f)
687                 goto out;
688
689         if (ip != htonl(INADDR_ANY)) {
690                 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
691                 if (!rd)
692                         goto out;
693         }
694
695         err = 0;
696
697         /* remove a destination if it's not the only one on the list,
698          * otherwise destroy the fdb entry
699          */
700         if (rd && !list_is_singular(&f->remotes)) {
701                 list_del_rcu(&rd->list);
702                 call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
703                 goto out;
704         }
705
706         vxlan_fdb_destroy(vxlan, f);
707
708 out:
709         spin_unlock_bh(&vxlan->hash_lock);
710
711         return err;
712 }
713
714 /* Dump forwarding table */
715 static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
716                           struct net_device *dev, int idx)
717 {
718         struct vxlan_dev *vxlan = netdev_priv(dev);
719         unsigned int h;
720
721         for (h = 0; h < FDB_HASH_SIZE; ++h) {
722                 struct vxlan_fdb *f;
723                 int err;
724
725                 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
726                         struct vxlan_rdst *rd;
727
728                         if (idx < cb->args[0])
729                                 goto skip;
730
731                         list_for_each_entry_rcu(rd, &f->remotes, list) {
732                                 err = vxlan_fdb_info(skb, vxlan, f,
733                                                      NETLINK_CB(cb->skb).portid,
734                                                      cb->nlh->nlmsg_seq,
735                                                      RTM_NEWNEIGH,
736                                                      NLM_F_MULTI, rd);
737                                 if (err < 0)
738                                         goto out;
739                         }
740 skip:
741                         ++idx;
742                 }
743         }
744 out:
745         return idx;
746 }
747
748 /* Watch incoming packets to learn mapping between Ethernet address
749  * and Tunnel endpoint.
750  * Return true if packet is bogus and should be droppped.
751  */
752 static bool vxlan_snoop(struct net_device *dev,
753                         __be32 src_ip, const u8 *src_mac)
754 {
755         struct vxlan_dev *vxlan = netdev_priv(dev);
756         struct vxlan_fdb *f;
757
758         f = vxlan_find_mac(vxlan, src_mac);
759         if (likely(f)) {
760                 struct vxlan_rdst *rdst = first_remote_rcu(f);
761
762                 if (likely(rdst->remote_ip == src_ip))
763                         return false;
764
765                 /* Don't migrate static entries, drop packets */
766                 if (f->state & NUD_NOARP)
767                         return true;
768
769                 if (net_ratelimit())
770                         netdev_info(dev,
771                                     "%pM migrated from %pI4 to %pI4\n",
772                                     src_mac, &rdst->remote_ip, &src_ip);
773
774                 rdst->remote_ip = src_ip;
775                 f->updated = jiffies;
776                 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
777         } else {
778                 /* learned new entry */
779                 spin_lock(&vxlan->hash_lock);
780
781                 /* close off race between vxlan_flush and incoming packets */
782                 if (netif_running(dev))
783                         vxlan_fdb_create(vxlan, src_mac, src_ip,
784                                          NUD_REACHABLE,
785                                          NLM_F_EXCL|NLM_F_CREATE,
786                                          vxlan->dst_port,
787                                          vxlan->default_dst.remote_vni,
788                                          0, NTF_SELF);
789                 spin_unlock(&vxlan->hash_lock);
790         }
791
792         return false;
793 }
794
795 /* See if multicast group is already in use by other ID */
796 static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
797 {
798         struct vxlan_dev *vxlan;
799
800         list_for_each_entry(vxlan, &vn->vxlan_list, next) {
801                 if (!netif_running(vxlan->dev))
802                         continue;
803
804                 if (vxlan->default_dst.remote_ip == remote_ip)
805                         return true;
806         }
807
808         return false;
809 }
810
811 static void vxlan_sock_hold(struct vxlan_sock *vs)
812 {
813         atomic_inc(&vs->refcnt);
814 }
815
816 static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
817 {
818         if (!atomic_dec_and_test(&vs->refcnt))
819                 return;
820
821         spin_lock(&vn->sock_lock);
822         hlist_del_rcu(&vs->hlist);
823         spin_unlock(&vn->sock_lock);
824
825         queue_work(vxlan_wq, &vs->del_work);
826 }
827
828 /* Callback to update multicast group membership when first VNI on
829  * multicast asddress is brought up
830  * Done as workqueue because ip_mc_join_group acquires RTNL.
831  */
832 static void vxlan_igmp_join(struct work_struct *work)
833 {
834         struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
835         struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
836         struct vxlan_sock *vs = vxlan->vn_sock;
837         struct sock *sk = vs->sock->sk;
838         struct ip_mreqn mreq = {
839                 .imr_multiaddr.s_addr   = vxlan->default_dst.remote_ip,
840                 .imr_ifindex            = vxlan->default_dst.remote_ifindex,
841         };
842
843         lock_sock(sk);
844         ip_mc_join_group(sk, &mreq);
845         release_sock(sk);
846
847         vxlan_sock_release(vn, vs);
848         dev_put(vxlan->dev);
849 }
850
851 /* Inverse of vxlan_igmp_join when last VNI is brought down */
852 static void vxlan_igmp_leave(struct work_struct *work)
853 {
854         struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
855         struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
856         struct vxlan_sock *vs = vxlan->vn_sock;
857         struct sock *sk = vs->sock->sk;
858         struct ip_mreqn mreq = {
859                 .imr_multiaddr.s_addr   = vxlan->default_dst.remote_ip,
860                 .imr_ifindex            = vxlan->default_dst.remote_ifindex,
861         };
862
863         lock_sock(sk);
864         ip_mc_leave_group(sk, &mreq);
865         release_sock(sk);
866
867         vxlan_sock_release(vn, vs);
868         dev_put(vxlan->dev);
869 }
870
871 /* Callback from net/ipv4/udp.c to receive packets */
872 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
873 {
874         struct vxlan_sock *vs;
875         struct vxlanhdr *vxh;
876         __be16 port;
877
878         /* Need Vxlan and inner Ethernet header to be present */
879         if (!pskb_may_pull(skb, VXLAN_HLEN))
880                 goto error;
881
882         /* Return packets with reserved bits set */
883         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
884         if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
885             (vxh->vx_vni & htonl(0xff))) {
886                 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
887                            ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
888                 goto error;
889         }
890
891         if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
892                 goto drop;
893
894         port = inet_sk(sk)->inet_sport;
895
896         vs = vxlan_find_sock(sock_net(sk), port);
897         if (!vs)
898                 goto drop;
899
900         vs->rcv(vs, skb, vxh->vx_vni);
901         return 0;
902
903 drop:
904         /* Consume bad packet */
905         kfree_skb(skb);
906         return 0;
907
908 error:
909         /* Return non vxlan pkt */
910         return 1;
911 }
912
913 static void vxlan_rcv(struct vxlan_sock *vs,
914                       struct sk_buff *skb, __be32 vx_vni)
915 {
916         struct iphdr *oip;
917         struct vxlan_dev *vxlan;
918         struct pcpu_tstats *stats;
919         __u32 vni;
920         int err;
921
922         vni = ntohl(vx_vni) >> 8;
923         /* Is this VNI defined? */
924         vxlan = vxlan_vs_find_vni(vs, vni);
925         if (!vxlan)
926                 goto drop;
927
928         skb_reset_mac_header(skb);
929         skb->protocol = eth_type_trans(skb, vxlan->dev);
930
931         /* Ignore packet loops (and multicast echo) */
932         if (compare_ether_addr(eth_hdr(skb)->h_source,
933                                vxlan->dev->dev_addr) == 0)
934                 goto drop;
935
936         /* Re-examine inner Ethernet packet */
937         oip = ip_hdr(skb);
938         if ((vxlan->flags & VXLAN_F_LEARN) &&
939             vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
940                 goto drop;
941
942         skb_reset_network_header(skb);
943
944         /* If the NIC driver gave us an encapsulated packet with
945          * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
946          * leave the CHECKSUM_UNNECESSARY, the device checksummed it
947          * for us. Otherwise force the upper layers to verify it.
948          */
949         if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
950             !(vxlan->dev->features & NETIF_F_RXCSUM))
951                 skb->ip_summed = CHECKSUM_NONE;
952
953         skb->encapsulation = 0;
954
955         err = IP_ECN_decapsulate(oip, skb);
956         if (unlikely(err)) {
957                 if (log_ecn_error)
958                         net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
959                                              &oip->saddr, oip->tos);
960                 if (err > 1) {
961                         ++vxlan->dev->stats.rx_frame_errors;
962                         ++vxlan->dev->stats.rx_errors;
963                         goto drop;
964                 }
965         }
966
967         stats = this_cpu_ptr(vxlan->dev->tstats);
968         u64_stats_update_begin(&stats->syncp);
969         stats->rx_packets++;
970         stats->rx_bytes += skb->len;
971         u64_stats_update_end(&stats->syncp);
972
973         netif_rx(skb);
974
975         return;
976 drop:
977         /* Consume bad packet */
978         kfree_skb(skb);
979 }
980
981 static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
982 {
983         struct vxlan_dev *vxlan = netdev_priv(dev);
984         struct arphdr *parp;
985         u8 *arpptr, *sha;
986         __be32 sip, tip;
987         struct neighbour *n;
988
989         if (dev->flags & IFF_NOARP)
990                 goto out;
991
992         if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
993                 dev->stats.tx_dropped++;
994                 goto out;
995         }
996         parp = arp_hdr(skb);
997
998         if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
999              parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1000             parp->ar_pro != htons(ETH_P_IP) ||
1001             parp->ar_op != htons(ARPOP_REQUEST) ||
1002             parp->ar_hln != dev->addr_len ||
1003             parp->ar_pln != 4)
1004                 goto out;
1005         arpptr = (u8 *)parp + sizeof(struct arphdr);
1006         sha = arpptr;
1007         arpptr += dev->addr_len;        /* sha */
1008         memcpy(&sip, arpptr, sizeof(sip));
1009         arpptr += sizeof(sip);
1010         arpptr += dev->addr_len;        /* tha */
1011         memcpy(&tip, arpptr, sizeof(tip));
1012
1013         if (ipv4_is_loopback(tip) ||
1014             ipv4_is_multicast(tip))
1015                 goto out;
1016
1017         n = neigh_lookup(&arp_tbl, &tip, dev);
1018
1019         if (n) {
1020                 struct vxlan_fdb *f;
1021                 struct sk_buff  *reply;
1022
1023                 if (!(n->nud_state & NUD_CONNECTED)) {
1024                         neigh_release(n);
1025                         goto out;
1026                 }
1027
1028                 f = vxlan_find_mac(vxlan, n->ha);
1029                 if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
1030                         /* bridge-local neighbor */
1031                         neigh_release(n);
1032                         goto out;
1033                 }
1034
1035                 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1036                                 n->ha, sha);
1037
1038                 neigh_release(n);
1039
1040                 skb_reset_mac_header(reply);
1041                 __skb_pull(reply, skb_network_offset(reply));
1042                 reply->ip_summed = CHECKSUM_UNNECESSARY;
1043                 reply->pkt_type = PACKET_HOST;
1044
1045                 if (netif_rx_ni(reply) == NET_RX_DROP)
1046                         dev->stats.rx_dropped++;
1047         } else if (vxlan->flags & VXLAN_F_L3MISS)
1048                 vxlan_ip_miss(dev, tip);
1049 out:
1050         consume_skb(skb);
1051         return NETDEV_TX_OK;
1052 }
1053
1054 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1055 {
1056         struct vxlan_dev *vxlan = netdev_priv(dev);
1057         struct neighbour *n;
1058         struct iphdr *pip;
1059
1060         if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1061                 return false;
1062
1063         n = NULL;
1064         switch (ntohs(eth_hdr(skb)->h_proto)) {
1065         case ETH_P_IP:
1066                 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1067                         return false;
1068                 pip = ip_hdr(skb);
1069                 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
1070                 break;
1071         default:
1072                 return false;
1073         }
1074
1075         if (n) {
1076                 bool diff;
1077
1078                 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
1079                 if (diff) {
1080                         memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1081                                 dev->addr_len);
1082                         memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1083                 }
1084                 neigh_release(n);
1085                 return diff;
1086         } else if (vxlan->flags & VXLAN_F_L3MISS)
1087                 vxlan_ip_miss(dev, pip->daddr);
1088         return false;
1089 }
1090
1091 static void vxlan_sock_put(struct sk_buff *skb)
1092 {
1093         sock_put(skb->sk);
1094 }
1095
1096 /* On transmit, associate with the tunnel socket */
1097 static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1098 {
1099         struct vxlan_dev *vxlan = netdev_priv(dev);
1100         struct sock *sk = vxlan->vn_sock->sock->sk;
1101
1102         skb_orphan(skb);
1103         sock_hold(sk);
1104         skb->sk = sk;
1105         skb->destructor = vxlan_sock_put;
1106 }
1107
1108 /* Compute source port for outgoing packet
1109  *   first choice to use L4 flow hash since it will spread
1110  *     better and maybe available from hardware
1111  *   secondary choice is to use jhash on the Ethernet header
1112  */
1113 static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
1114 {
1115         unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1116         u32 hash;
1117
1118         hash = skb_get_rxhash(skb);
1119         if (!hash)
1120                 hash = jhash(skb->data, 2 * ETH_ALEN,
1121                              (__force u32) skb->protocol);
1122
1123         return htons((((u64) hash * range) >> 32) + vxlan->port_min);
1124 }
1125
1126 static int handle_offloads(struct sk_buff *skb)
1127 {
1128         if (skb_is_gso(skb)) {
1129                 int err = skb_unclone(skb, GFP_ATOMIC);
1130                 if (unlikely(err))
1131                         return err;
1132
1133                 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1134         } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1135                 skb->ip_summed = CHECKSUM_NONE;
1136
1137         return 0;
1138 }
1139
1140 /* Bypass encapsulation if the destination is local */
1141 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1142                                struct vxlan_dev *dst_vxlan)
1143 {
1144         struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1145         struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1146
1147         skb->pkt_type = PACKET_HOST;
1148         skb->encapsulation = 0;
1149         skb->dev = dst_vxlan->dev;
1150         __skb_pull(skb, skb_network_offset(skb));
1151
1152         if (dst_vxlan->flags & VXLAN_F_LEARN)
1153                 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1154                             eth_hdr(skb)->h_source);
1155
1156         u64_stats_update_begin(&tx_stats->syncp);
1157         tx_stats->tx_packets++;
1158         tx_stats->tx_bytes += skb->len;
1159         u64_stats_update_end(&tx_stats->syncp);
1160
1161         if (netif_rx(skb) == NET_RX_SUCCESS) {
1162                 u64_stats_update_begin(&rx_stats->syncp);
1163                 rx_stats->rx_packets++;
1164                 rx_stats->rx_bytes += skb->len;
1165                 u64_stats_update_end(&rx_stats->syncp);
1166         } else {
1167                 skb->dev->stats.rx_dropped++;
1168         }
1169 }
1170
1171 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1172                            struct vxlan_rdst *rdst, bool did_rsc)
1173 {
1174         struct vxlan_dev *vxlan = netdev_priv(dev);
1175         struct rtable *rt;
1176         const struct iphdr *old_iph;
1177         struct vxlanhdr *vxh;
1178         struct udphdr *uh;
1179         struct flowi4 fl4;
1180         __be32 dst;
1181         __be16 src_port, dst_port;
1182         u32 vni;
1183         __be16 df = 0;
1184         __u8 tos, ttl;
1185         int err;
1186
1187         dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
1188         vni = rdst->remote_vni;
1189         dst = rdst->remote_ip;
1190
1191         if (!dst) {
1192                 if (did_rsc) {
1193                         /* short-circuited back to local bridge */
1194                         vxlan_encap_bypass(skb, vxlan, vxlan);
1195                         return;
1196                 }
1197                 goto drop;
1198         }
1199
1200         if (!skb->encapsulation) {
1201                 skb_reset_inner_headers(skb);
1202                 skb->encapsulation = 1;
1203         }
1204
1205         /* Need space for new headers (invalidates iph ptr) */
1206         if (skb_cow_head(skb, VXLAN_HEADROOM))
1207                 goto drop;
1208
1209         old_iph = ip_hdr(skb);
1210
1211         ttl = vxlan->ttl;
1212         if (!ttl && IN_MULTICAST(ntohl(dst)))
1213                 ttl = 1;
1214
1215         tos = vxlan->tos;
1216         if (tos == 1)
1217                 tos = ip_tunnel_get_dsfield(old_iph, skb);
1218
1219         src_port = vxlan_src_port(vxlan, skb);
1220
1221         memset(&fl4, 0, sizeof(fl4));
1222         fl4.flowi4_oif = rdst->remote_ifindex;
1223         fl4.flowi4_tos = RT_TOS(tos);
1224         fl4.daddr = dst;
1225         fl4.saddr = vxlan->saddr;
1226
1227         rt = ip_route_output_key(dev_net(dev), &fl4);
1228         if (IS_ERR(rt)) {
1229                 netdev_dbg(dev, "no route to %pI4\n", &dst);
1230                 dev->stats.tx_carrier_errors++;
1231                 goto tx_error;
1232         }
1233
1234         if (rt->dst.dev == dev) {
1235                 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1236                 ip_rt_put(rt);
1237                 dev->stats.collisions++;
1238                 goto tx_error;
1239         }
1240
1241         /* Bypass encapsulation if the destination is local */
1242         if (rt->rt_flags & RTCF_LOCAL &&
1243             !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1244                 struct vxlan_dev *dst_vxlan;
1245
1246                 ip_rt_put(rt);
1247                 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1248                 if (!dst_vxlan)
1249                         goto tx_error;
1250                 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1251                 return;
1252         }
1253         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1254         vxh->vx_flags = htonl(VXLAN_FLAGS);
1255         vxh->vx_vni = htonl(vni << 8);
1256
1257         __skb_push(skb, sizeof(*uh));
1258         skb_reset_transport_header(skb);
1259         uh = udp_hdr(skb);
1260
1261         uh->dest = dst_port;
1262         uh->source = src_port;
1263
1264         uh->len = htons(skb->len);
1265         uh->check = 0;
1266
1267         vxlan_set_owner(dev, skb);
1268
1269         if (handle_offloads(skb))
1270                 goto drop;
1271
1272         tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1273         ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1274
1275         err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1276                             IPPROTO_UDP, tos, ttl, df);
1277         iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1278
1279         return;
1280
1281 drop:
1282         dev->stats.tx_dropped++;
1283         goto tx_free;
1284
1285 tx_error:
1286         dev->stats.tx_errors++;
1287 tx_free:
1288         dev_kfree_skb(skb);
1289 }
1290
1291 /* Transmit local packets over Vxlan
1292  *
1293  * Outer IP header inherits ECN and DF from inner header.
1294  * Outer UDP destination is the VXLAN assigned port.
1295  *           source port is based on hash of flow
1296  */
1297 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1298 {
1299         struct vxlan_dev *vxlan = netdev_priv(dev);
1300         struct ethhdr *eth;
1301         bool did_rsc = false;
1302         struct vxlan_rdst *rdst;
1303         struct vxlan_fdb *f;
1304
1305         skb_reset_mac_header(skb);
1306         eth = eth_hdr(skb);
1307
1308         if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1309                 return arp_reduce(dev, skb);
1310
1311         f = vxlan_find_mac(vxlan, eth->h_dest);
1312         did_rsc = false;
1313
1314         if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1315             ntohs(eth->h_proto) == ETH_P_IP) {
1316                 did_rsc = route_shortcircuit(dev, skb);
1317                 if (did_rsc)
1318                         f = vxlan_find_mac(vxlan, eth->h_dest);
1319         }
1320
1321         if (f == NULL) {
1322                 f = vxlan_find_mac(vxlan, all_zeros_mac);
1323                 if (f == NULL) {
1324                         if ((vxlan->flags & VXLAN_F_L2MISS) &&
1325                             !is_multicast_ether_addr(eth->h_dest))
1326                                 vxlan_fdb_miss(vxlan, eth->h_dest);
1327
1328                         dev->stats.tx_dropped++;
1329                         dev_kfree_skb(skb);
1330                         return NETDEV_TX_OK;
1331                 }
1332         }
1333
1334         list_for_each_entry_rcu(rdst, &f->remotes, list) {
1335                 struct sk_buff *skb1;
1336
1337                 skb1 = skb_clone(skb, GFP_ATOMIC);
1338                 if (skb1)
1339                         vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1340         }
1341
1342         dev_kfree_skb(skb);
1343         return NETDEV_TX_OK;
1344 }
1345
1346 /* Walk the forwarding table and purge stale entries */
1347 static void vxlan_cleanup(unsigned long arg)
1348 {
1349         struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1350         unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1351         unsigned int h;
1352
1353         if (!netif_running(vxlan->dev))
1354                 return;
1355
1356         spin_lock_bh(&vxlan->hash_lock);
1357         for (h = 0; h < FDB_HASH_SIZE; ++h) {
1358                 struct hlist_node *p, *n;
1359                 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1360                         struct vxlan_fdb *f
1361                                 = container_of(p, struct vxlan_fdb, hlist);
1362                         unsigned long timeout;
1363
1364                         if (f->state & NUD_PERMANENT)
1365                                 continue;
1366
1367                         timeout = f->used + vxlan->age_interval * HZ;
1368                         if (time_before_eq(timeout, jiffies)) {
1369                                 netdev_dbg(vxlan->dev,
1370                                            "garbage collect %pM\n",
1371                                            f->eth_addr);
1372                                 f->state = NUD_STALE;
1373                                 vxlan_fdb_destroy(vxlan, f);
1374                         } else if (time_before(timeout, next_timer))
1375                                 next_timer = timeout;
1376                 }
1377         }
1378         spin_unlock_bh(&vxlan->hash_lock);
1379
1380         mod_timer(&vxlan->age_timer, next_timer);
1381 }
1382
1383 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1384 {
1385         __u32 vni = vxlan->default_dst.remote_vni;
1386
1387         vxlan->vn_sock = vs;
1388         hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1389 }
1390
1391 /* Setup stats when device is created */
1392 static int vxlan_init(struct net_device *dev)
1393 {
1394         struct vxlan_dev *vxlan = netdev_priv(dev);
1395         struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1396         struct vxlan_sock *vs;
1397
1398         dev->tstats = alloc_percpu(struct pcpu_tstats);
1399         if (!dev->tstats)
1400                 return -ENOMEM;
1401
1402         spin_lock(&vn->sock_lock);
1403         vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
1404         if (vs) {
1405                 /* If we have a socket with same port already, reuse it */
1406                 atomic_inc(&vs->refcnt);
1407                 vxlan_vs_add_dev(vs, vxlan);
1408         } else {
1409                 /* otherwise make new socket outside of RTNL */
1410                 dev_hold(dev);
1411                 queue_work(vxlan_wq, &vxlan->sock_work);
1412         }
1413         spin_unlock(&vn->sock_lock);
1414
1415         return 0;
1416 }
1417
1418 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
1419 {
1420         struct vxlan_fdb *f;
1421
1422         spin_lock_bh(&vxlan->hash_lock);
1423         f = __vxlan_find_mac(vxlan, all_zeros_mac);
1424         if (f)
1425                 vxlan_fdb_destroy(vxlan, f);
1426         spin_unlock_bh(&vxlan->hash_lock);
1427 }
1428
1429 static void vxlan_uninit(struct net_device *dev)
1430 {
1431         struct vxlan_dev *vxlan = netdev_priv(dev);
1432         struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1433         struct vxlan_sock *vs = vxlan->vn_sock;
1434
1435         vxlan_fdb_delete_default(vxlan);
1436
1437         if (vs)
1438                 vxlan_sock_release(vn, vs);
1439         free_percpu(dev->tstats);
1440 }
1441
1442 /* Start ageing timer and join group when device is brought up */
1443 static int vxlan_open(struct net_device *dev)
1444 {
1445         struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1446         struct vxlan_dev *vxlan = netdev_priv(dev);
1447         struct vxlan_sock *vs = vxlan->vn_sock;
1448
1449         /* socket hasn't been created */
1450         if (!vs)
1451                 return -ENOTCONN;
1452
1453         if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1454             vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
1455                 vxlan_sock_hold(vs);
1456                 dev_hold(dev);
1457                 queue_work(vxlan_wq, &vxlan->igmp_join);
1458         }
1459
1460         if (vxlan->age_interval)
1461                 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1462
1463         return 0;
1464 }
1465
1466 /* Purge the forwarding table */
1467 static void vxlan_flush(struct vxlan_dev *vxlan)
1468 {
1469         unsigned int h;
1470
1471         spin_lock_bh(&vxlan->hash_lock);
1472         for (h = 0; h < FDB_HASH_SIZE; ++h) {
1473                 struct hlist_node *p, *n;
1474                 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1475                         struct vxlan_fdb *f
1476                                 = container_of(p, struct vxlan_fdb, hlist);
1477                         /* the all_zeros_mac entry is deleted at vxlan_uninit */
1478                         if (!is_zero_ether_addr(f->eth_addr))
1479                                 vxlan_fdb_destroy(vxlan, f);
1480                 }
1481         }
1482         spin_unlock_bh(&vxlan->hash_lock);
1483 }
1484
1485 /* Cleanup timer and forwarding table on shutdown */
1486 static int vxlan_stop(struct net_device *dev)
1487 {
1488         struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1489         struct vxlan_dev *vxlan = netdev_priv(dev);
1490         struct vxlan_sock *vs = vxlan->vn_sock;
1491
1492         if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
1493             ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
1494                 vxlan_sock_hold(vs);
1495                 dev_hold(dev);
1496                 queue_work(vxlan_wq, &vxlan->igmp_leave);
1497         }
1498
1499         del_timer_sync(&vxlan->age_timer);
1500
1501         vxlan_flush(vxlan);
1502
1503         return 0;
1504 }
1505
1506 /* Stub, nothing needs to be done. */
1507 static void vxlan_set_multicast_list(struct net_device *dev)
1508 {
1509 }
1510
1511 static const struct net_device_ops vxlan_netdev_ops = {
1512         .ndo_init               = vxlan_init,
1513         .ndo_uninit             = vxlan_uninit,
1514         .ndo_open               = vxlan_open,
1515         .ndo_stop               = vxlan_stop,
1516         .ndo_start_xmit         = vxlan_xmit,
1517         .ndo_get_stats64        = ip_tunnel_get_stats64,
1518         .ndo_set_rx_mode        = vxlan_set_multicast_list,
1519         .ndo_change_mtu         = eth_change_mtu,
1520         .ndo_validate_addr      = eth_validate_addr,
1521         .ndo_set_mac_address    = eth_mac_addr,
1522         .ndo_fdb_add            = vxlan_fdb_add,
1523         .ndo_fdb_del            = vxlan_fdb_delete,
1524         .ndo_fdb_dump           = vxlan_fdb_dump,
1525 };
1526
1527 /* Info for udev, that this is a virtual tunnel endpoint */
1528 static struct device_type vxlan_type = {
1529         .name = "vxlan",
1530 };
1531
1532 /* Initialize the device structure. */
1533 static void vxlan_setup(struct net_device *dev)
1534 {
1535         struct vxlan_dev *vxlan = netdev_priv(dev);
1536         unsigned int h;
1537         int low, high;
1538
1539         eth_hw_addr_random(dev);
1540         ether_setup(dev);
1541         dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
1542
1543         dev->netdev_ops = &vxlan_netdev_ops;
1544         dev->destructor = free_netdev;
1545         SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1546
1547         dev->tx_queue_len = 0;
1548         dev->features   |= NETIF_F_LLTX;
1549         dev->features   |= NETIF_F_NETNS_LOCAL;
1550         dev->features   |= NETIF_F_SG | NETIF_F_HW_CSUM;
1551         dev->features   |= NETIF_F_RXCSUM;
1552         dev->features   |= NETIF_F_GSO_SOFTWARE;
1553
1554         dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1555         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1556         dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1557         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1558
1559         INIT_LIST_HEAD(&vxlan->next);
1560         spin_lock_init(&vxlan->hash_lock);
1561         INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
1562         INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
1563         INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
1564
1565         init_timer_deferrable(&vxlan->age_timer);
1566         vxlan->age_timer.function = vxlan_cleanup;
1567         vxlan->age_timer.data = (unsigned long) vxlan;
1568
1569         inet_get_local_port_range(&low, &high);
1570         vxlan->port_min = low;
1571         vxlan->port_max = high;
1572         vxlan->dst_port = htons(vxlan_port);
1573
1574         vxlan->dev = dev;
1575
1576         for (h = 0; h < FDB_HASH_SIZE; ++h)
1577                 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1578 }
1579
1580 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1581         [IFLA_VXLAN_ID]         = { .type = NLA_U32 },
1582         [IFLA_VXLAN_GROUP]      = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1583         [IFLA_VXLAN_LINK]       = { .type = NLA_U32 },
1584         [IFLA_VXLAN_LOCAL]      = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1585         [IFLA_VXLAN_TOS]        = { .type = NLA_U8 },
1586         [IFLA_VXLAN_TTL]        = { .type = NLA_U8 },
1587         [IFLA_VXLAN_LEARNING]   = { .type = NLA_U8 },
1588         [IFLA_VXLAN_AGEING]     = { .type = NLA_U32 },
1589         [IFLA_VXLAN_LIMIT]      = { .type = NLA_U32 },
1590         [IFLA_VXLAN_PORT_RANGE] = { .len  = sizeof(struct ifla_vxlan_port_range) },
1591         [IFLA_VXLAN_PROXY]      = { .type = NLA_U8 },
1592         [IFLA_VXLAN_RSC]        = { .type = NLA_U8 },
1593         [IFLA_VXLAN_L2MISS]     = { .type = NLA_U8 },
1594         [IFLA_VXLAN_L3MISS]     = { .type = NLA_U8 },
1595         [IFLA_VXLAN_PORT]       = { .type = NLA_U16 },
1596 };
1597
1598 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1599 {
1600         if (tb[IFLA_ADDRESS]) {
1601                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1602                         pr_debug("invalid link address (not ethernet)\n");
1603                         return -EINVAL;
1604                 }
1605
1606                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1607                         pr_debug("invalid all zero ethernet address\n");
1608                         return -EADDRNOTAVAIL;
1609                 }
1610         }
1611
1612         if (!data)
1613                 return -EINVAL;
1614
1615         if (data[IFLA_VXLAN_ID]) {
1616                 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1617                 if (id >= VXLAN_VID_MASK)
1618                         return -ERANGE;
1619         }
1620
1621         if (data[IFLA_VXLAN_PORT_RANGE]) {
1622                 const struct ifla_vxlan_port_range *p
1623                         = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1624
1625                 if (ntohs(p->high) < ntohs(p->low)) {
1626                         pr_debug("port range %u .. %u not valid\n",
1627                                  ntohs(p->low), ntohs(p->high));
1628                         return -EINVAL;
1629                 }
1630         }
1631
1632         return 0;
1633 }
1634
1635 static void vxlan_get_drvinfo(struct net_device *netdev,
1636                               struct ethtool_drvinfo *drvinfo)
1637 {
1638         strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1639         strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1640 }
1641
1642 static const struct ethtool_ops vxlan_ethtool_ops = {
1643         .get_drvinfo    = vxlan_get_drvinfo,
1644         .get_link       = ethtool_op_get_link,
1645 };
1646
1647 static void vxlan_del_work(struct work_struct *work)
1648 {
1649         struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1650
1651         sk_release_kernel(vs->sock->sk);
1652         kfree_rcu(vs, rcu);
1653 }
1654
1655 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
1656                                               vxlan_rcv_t *rcv)
1657 {
1658         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1659         struct vxlan_sock *vs;
1660         struct sock *sk;
1661         struct sockaddr_in vxlan_addr = {
1662                 .sin_family = AF_INET,
1663                 .sin_addr.s_addr = htonl(INADDR_ANY),
1664                 .sin_port = port,
1665         };
1666         int rc;
1667         unsigned int h;
1668
1669         vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1670         if (!vs) {
1671                 pr_debug("memory alocation failure\n");
1672                 return ERR_PTR(-ENOMEM);
1673         }
1674
1675         for (h = 0; h < VNI_HASH_SIZE; ++h)
1676                 INIT_HLIST_HEAD(&vs->vni_list[h]);
1677
1678         INIT_WORK(&vs->del_work, vxlan_del_work);
1679
1680         /* Create UDP socket for encapsulation receive. */
1681         rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1682         if (rc < 0) {
1683                 pr_debug("UDP socket create failed\n");
1684                 kfree(vs);
1685                 return ERR_PTR(rc);
1686         }
1687
1688         /* Put in proper namespace */
1689         sk = vs->sock->sk;
1690         sk_change_net(sk, net);
1691
1692         rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1693                          sizeof(vxlan_addr));
1694         if (rc < 0) {
1695                 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1696                          &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1697                 sk_release_kernel(sk);
1698                 kfree(vs);
1699                 return ERR_PTR(rc);
1700         }
1701         atomic_set(&vs->refcnt, 1);
1702         vs->rcv = rcv;
1703
1704         /* Disable multicast loopback */
1705         inet_sk(sk)->mc_loop = 0;
1706         spin_lock(&vn->sock_lock);
1707         hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1708         spin_unlock(&vn->sock_lock);
1709
1710         /* Mark socket as an encapsulation socket. */
1711         udp_sk(sk)->encap_type = 1;
1712         udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1713         udp_encap_enable();
1714         return vs;
1715 }
1716
1717 static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
1718                                          vxlan_rcv_t *rcv)
1719 {
1720         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1721         struct vxlan_sock *vs;
1722
1723         vs = vxlan_socket_create(net, port, rcv);
1724         if (!IS_ERR(vs))
1725                 return vs;
1726
1727         spin_lock(&vn->sock_lock);
1728         vs = vxlan_find_sock(net, port);
1729         if (vs) {
1730                 if (vs->rcv == rcv)
1731                         atomic_inc(&vs->refcnt);
1732                 else
1733                         vs = ERR_PTR(-EBUSY);
1734         }
1735         spin_unlock(&vn->sock_lock);
1736
1737         if (!vs)
1738                 vs = ERR_PTR(-EINVAL);
1739
1740         return vs;
1741 }
1742
1743 /* Scheduled at device creation to bind to a socket */
1744 static void vxlan_sock_work(struct work_struct *work)
1745 {
1746         struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
1747         struct net *net = dev_net(vxlan->dev);
1748         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1749         __be16 port = vxlan->dst_port;
1750         struct vxlan_sock *nvs;
1751
1752         nvs = vxlan_sock_add(net, port, vxlan_rcv);
1753         spin_lock(&vn->sock_lock);
1754         if (!IS_ERR(nvs))
1755                 vxlan_vs_add_dev(nvs, vxlan);
1756         spin_unlock(&vn->sock_lock);
1757
1758         dev_put(vxlan->dev);
1759 }
1760
1761 static int vxlan_newlink(struct net *net, struct net_device *dev,
1762                          struct nlattr *tb[], struct nlattr *data[])
1763 {
1764         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1765         struct vxlan_dev *vxlan = netdev_priv(dev);
1766         struct vxlan_rdst *dst = &vxlan->default_dst;
1767         __u32 vni;
1768         int err;
1769
1770         if (!data[IFLA_VXLAN_ID])
1771                 return -EINVAL;
1772
1773         vni = nla_get_u32(data[IFLA_VXLAN_ID]);
1774         dst->remote_vni = vni;
1775
1776         if (data[IFLA_VXLAN_GROUP])
1777                 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
1778
1779         if (data[IFLA_VXLAN_LOCAL])
1780                 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1781
1782         if (data[IFLA_VXLAN_LINK] &&
1783             (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
1784                 struct net_device *lowerdev
1785                          = __dev_get_by_index(net, dst->remote_ifindex);
1786
1787                 if (!lowerdev) {
1788                         pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
1789                         return -ENODEV;
1790                 }
1791
1792                 if (!tb[IFLA_MTU])
1793                         dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1794
1795                 /* update header length based on lower device */
1796                 dev->hard_header_len = lowerdev->hard_header_len +
1797                                        VXLAN_HEADROOM;
1798         }
1799
1800         if (data[IFLA_VXLAN_TOS])
1801                 vxlan->tos  = nla_get_u8(data[IFLA_VXLAN_TOS]);
1802
1803         if (data[IFLA_VXLAN_TTL])
1804                 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1805
1806         if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
1807                 vxlan->flags |= VXLAN_F_LEARN;
1808
1809         if (data[IFLA_VXLAN_AGEING])
1810                 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1811         else
1812                 vxlan->age_interval = FDB_AGE_DEFAULT;
1813
1814         if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1815                 vxlan->flags |= VXLAN_F_PROXY;
1816
1817         if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1818                 vxlan->flags |= VXLAN_F_RSC;
1819
1820         if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1821                 vxlan->flags |= VXLAN_F_L2MISS;
1822
1823         if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1824                 vxlan->flags |= VXLAN_F_L3MISS;
1825
1826         if (data[IFLA_VXLAN_LIMIT])
1827                 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1828
1829         if (data[IFLA_VXLAN_PORT_RANGE]) {
1830                 const struct ifla_vxlan_port_range *p
1831                         = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1832                 vxlan->port_min = ntohs(p->low);
1833                 vxlan->port_max = ntohs(p->high);
1834         }
1835
1836         if (data[IFLA_VXLAN_PORT])
1837                 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1838
1839         if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1840                 pr_info("duplicate VNI %u\n", vni);
1841                 return -EEXIST;
1842         }
1843
1844         SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1845
1846         /* create an fdb entry for default destination */
1847         err = vxlan_fdb_create(vxlan, all_zeros_mac,
1848                                vxlan->default_dst.remote_ip,
1849                                NUD_REACHABLE|NUD_PERMANENT,
1850                                NLM_F_EXCL|NLM_F_CREATE,
1851                                vxlan->dst_port, vxlan->default_dst.remote_vni,
1852                                vxlan->default_dst.remote_ifindex, NTF_SELF);
1853         if (err)
1854                 return err;
1855
1856         err = register_netdevice(dev);
1857         if (err) {
1858                 vxlan_fdb_delete_default(vxlan);
1859                 return err;
1860         }
1861
1862         list_add(&vxlan->next, &vn->vxlan_list);
1863
1864         return 0;
1865 }
1866
1867 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1868 {
1869         struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1870         struct vxlan_dev *vxlan = netdev_priv(dev);
1871
1872         spin_lock(&vn->sock_lock);
1873         if (!hlist_unhashed(&vxlan->hlist))
1874                 hlist_del_rcu(&vxlan->hlist);
1875         spin_unlock(&vn->sock_lock);
1876
1877         list_del(&vxlan->next);
1878         unregister_netdevice_queue(dev, head);
1879 }
1880
1881 static size_t vxlan_get_size(const struct net_device *dev)
1882 {
1883
1884         return nla_total_size(sizeof(__u32)) +  /* IFLA_VXLAN_ID */
1885                 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
1886                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1887                 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1888                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TTL */
1889                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TOS */
1890                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_LEARNING */
1891                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_PROXY */
1892                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_RSC */
1893                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L2MISS */
1894                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L3MISS */
1895                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1896                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
1897                 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
1898                 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
1899                 0;
1900 }
1901
1902 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1903 {
1904         const struct vxlan_dev *vxlan = netdev_priv(dev);
1905         const struct vxlan_rdst *dst = &vxlan->default_dst;
1906         struct ifla_vxlan_port_range ports = {
1907                 .low =  htons(vxlan->port_min),
1908                 .high = htons(vxlan->port_max),
1909         };
1910
1911         if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
1912                 goto nla_put_failure;
1913
1914         if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
1915                 goto nla_put_failure;
1916
1917         if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
1918                 goto nla_put_failure;
1919
1920         if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
1921                 goto nla_put_failure;
1922
1923         if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1924             nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
1925             nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1926                         !!(vxlan->flags & VXLAN_F_LEARN)) ||
1927             nla_put_u8(skb, IFLA_VXLAN_PROXY,
1928                         !!(vxlan->flags & VXLAN_F_PROXY)) ||
1929             nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1930             nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1931                         !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1932             nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1933                         !!(vxlan->flags & VXLAN_F_L3MISS)) ||
1934             nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
1935             nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1936             nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
1937                 goto nla_put_failure;
1938
1939         if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1940                 goto nla_put_failure;
1941
1942         return 0;
1943
1944 nla_put_failure:
1945         return -EMSGSIZE;
1946 }
1947
1948 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1949         .kind           = "vxlan",
1950         .maxtype        = IFLA_VXLAN_MAX,
1951         .policy         = vxlan_policy,
1952         .priv_size      = sizeof(struct vxlan_dev),
1953         .setup          = vxlan_setup,
1954         .validate       = vxlan_validate,
1955         .newlink        = vxlan_newlink,
1956         .dellink        = vxlan_dellink,
1957         .get_size       = vxlan_get_size,
1958         .fill_info      = vxlan_fill_info,
1959 };
1960
1961 static __net_init int vxlan_init_net(struct net *net)
1962 {
1963         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1964         unsigned int h;
1965
1966         INIT_LIST_HEAD(&vn->vxlan_list);
1967         spin_lock_init(&vn->sock_lock);
1968
1969         for (h = 0; h < PORT_HASH_SIZE; ++h)
1970                 INIT_HLIST_HEAD(&vn->sock_list[h]);
1971
1972         return 0;
1973 }
1974
1975 static __net_exit void vxlan_exit_net(struct net *net)
1976 {
1977         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1978         struct vxlan_dev *vxlan;
1979         LIST_HEAD(list);
1980
1981         rtnl_lock();
1982         list_for_each_entry(vxlan, &vn->vxlan_list, next)
1983                 unregister_netdevice_queue(vxlan->dev, &list);
1984         unregister_netdevice_many(&list);
1985         rtnl_unlock();
1986 }
1987
1988 static struct pernet_operations vxlan_net_ops = {
1989         .init = vxlan_init_net,
1990         .exit = vxlan_exit_net,
1991         .id   = &vxlan_net_id,
1992         .size = sizeof(struct vxlan_net),
1993 };
1994
1995 static int __init vxlan_init_module(void)
1996 {
1997         int rc;
1998
1999         vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2000         if (!vxlan_wq)
2001                 return -ENOMEM;
2002
2003         get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2004
2005         rc = register_pernet_device(&vxlan_net_ops);
2006         if (rc)
2007                 goto out1;
2008
2009         rc = rtnl_link_register(&vxlan_link_ops);
2010         if (rc)
2011                 goto out2;
2012
2013         return 0;
2014
2015 out2:
2016         unregister_pernet_device(&vxlan_net_ops);
2017 out1:
2018         destroy_workqueue(vxlan_wq);
2019         return rc;
2020 }
2021 late_initcall(vxlan_init_module);
2022
2023 static void __exit vxlan_cleanup_module(void)
2024 {
2025         rtnl_link_unregister(&vxlan_link_ops);
2026         destroy_workqueue(vxlan_wq);
2027         unregister_pernet_device(&vxlan_net_ops);
2028         rcu_barrier();
2029 }
2030 module_exit(vxlan_cleanup_module);
2031
2032 MODULE_LICENSE("GPL");
2033 MODULE_VERSION(VXLAN_VERSION);
2034 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
2035 MODULE_ALIAS_RTNL_LINK("vxlan");