datapath: Backport: vxlan: interpret IP headers for ECN correctly
[cascardo/ovs.git] / datapath / linux / compat / 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 <linux/netdev_features.h>
31 #include <net/arp.h>
32 #include <net/ndisc.h>
33 #include <net/ip.h>
34 #include <net/ip_tunnels.h>
35 #include <net/icmp.h>
36 #include <net/udp.h>
37 #include <net/udp_tunnel.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 #include <net/vxlan.h>
45 #include <net/protocol.h>
46 #include <net/udp_tunnel.h>
47 #include <net/ip6_route.h>
48 #if IS_ENABLED(CONFIG_IPV6)
49 #include <net/ipv6.h>
50 #include <net/addrconf.h>
51 #include <net/ip6_tunnel.h>
52 #include <net/ip6_checksum.h>
53 #endif
54 #include <net/dst_metadata.h>
55
56 #ifndef HAVE_METADATA_DST
57 #include "gso.h"
58 #include "vport-netdev.h"
59
60 #define VXLAN_VERSION   "0.1"
61
62 #define PORT_HASH_BITS  8
63 #define PORT_HASH_SIZE  (1<<PORT_HASH_BITS)
64 #define FDB_AGE_DEFAULT 300 /* 5 min */
65 #define FDB_AGE_INTERVAL (10 * HZ)      /* rescan interval */
66
67 #ifndef NTF_SELF
68 #define NTF_SELF        0x02
69 #endif
70
71 /* UDP port for VXLAN traffic.
72  * The IANA assigned port is 4789, but the Linux default is 8472
73  * for compatibility with early adopters.
74  */
75 static unsigned short vxlan_port __read_mostly = 8472;
76 module_param_named(udp_port, vxlan_port, ushort, 0444);
77 MODULE_PARM_DESC(udp_port, "Destination UDP port");
78
79 static int vxlan_net_id;
80 static struct rtnl_link_ops vxlan_link_ops;
81
82 static const u8 all_zeros_mac[ETH_ALEN];
83
84 static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
85                                          bool no_share, u32 flags);
86
87 /* per-network namespace private data for this module */
88 struct vxlan_net {
89         struct list_head  vxlan_list;
90         struct hlist_head sock_list[PORT_HASH_SIZE];
91         spinlock_t        sock_lock;
92 };
93
94 /* Forwarding table entry */
95 struct vxlan_fdb {
96         struct hlist_node hlist;        /* linked list of entries */
97         struct rcu_head   rcu;
98         unsigned long     updated;      /* jiffies */
99         unsigned long     used;
100         struct list_head  remotes;
101         u8                eth_addr[ETH_ALEN];
102         u16               state;        /* see ndm_state */
103         u8                flags;        /* see ndm_flags */
104 };
105
106 /* salt for hash table */
107 static u32 vxlan_salt __read_mostly;
108 static struct workqueue_struct *vxlan_wq;
109
110 static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
111 {
112         return vs->flags & VXLAN_F_COLLECT_METADATA ||
113                ip_tunnel_collect_metadata();
114 }
115
116 #if IS_ENABLED(CONFIG_IPV6)
117 static inline
118 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
119 {
120         if (a->sa.sa_family != b->sa.sa_family)
121                 return false;
122         if (a->sa.sa_family == AF_INET6)
123                 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
124         else
125                 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
126 }
127
128 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
129 {
130         if (ipa->sa.sa_family == AF_INET6)
131                 return ipv6_addr_any(&ipa->sin6.sin6_addr);
132         else
133                 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
134 }
135
136 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
137 {
138         if (ipa->sa.sa_family == AF_INET6)
139                 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
140         else
141                 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
142 }
143
144 #else /* !CONFIG_IPV6 */
145
146 static inline
147 bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
148 {
149         return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
150 }
151
152 static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
153 {
154         return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
155 }
156
157 static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
158 {
159         return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
160 }
161
162 #endif
163
164 /* Virtual Network hash table head */
165 static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
166 {
167         return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
168 }
169
170 /* Socket hash table head */
171 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
172 {
173         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
174
175         return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
176 }
177
178 /* First remote destination for a forwarding entry.
179  * Guaranteed to be non-NULL because remotes are never deleted.
180  */
181 static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
182 {
183         return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
184 }
185
186 static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
187 {
188         return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
189 }
190
191 /* Find VXLAN socket based on network namespace, address family and UDP port
192  * and enabled unshareable flags.
193  */
194 static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
195                                           __be16 port, u32 flags)
196 {
197         struct vxlan_sock *vs;
198
199         flags &= VXLAN_F_RCV_FLAGS;
200
201         hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
202                 if (inet_sport(vs->sock->sk) == port &&
203                     vxlan_get_sk_family(vs) == family &&
204                     vs->flags == flags)
205                         return vs;
206         }
207         return NULL;
208 }
209
210 static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
211 {
212         struct vxlan_dev *vxlan;
213
214         hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
215                 if (vxlan->default_dst.remote_vni == id)
216                         return vxlan;
217         }
218
219         return NULL;
220 }
221
222 /* Look up VNI in a per net namespace table */
223 static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
224                                         sa_family_t family, __be16 port,
225                                         u32 flags)
226 {
227         struct vxlan_sock *vs;
228
229         vs = vxlan_find_sock(net, family, port, flags);
230         if (!vs)
231                 return NULL;
232
233         return vxlan_vs_find_vni(vs, id);
234 }
235
236 /* Fill in neighbour message in skbuff. */
237 static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
238                           const struct vxlan_fdb *fdb,
239                           u32 portid, u32 seq, int type, unsigned int flags,
240                           const struct vxlan_rdst *rdst)
241 {
242         return -EINVAL;
243 }
244
245 static inline size_t vxlan_nlmsg_size(void)
246 {
247         return NLMSG_ALIGN(sizeof(struct ndmsg))
248                 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
249                 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
250                 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
251                 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
252                 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
253                 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
254                 + nla_total_size(sizeof(struct nda_cacheinfo));
255 }
256
257 static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
258                              struct vxlan_rdst *rd, int type)
259 {
260         struct net *net = dev_net(vxlan->dev);
261         struct sk_buff *skb;
262         int err = -ENOBUFS;
263
264         skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
265         if (skb == NULL)
266                 goto errout;
267
268         err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
269         if (err < 0) {
270                 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
271                 WARN_ON(err == -EMSGSIZE);
272                 kfree_skb(skb);
273                 goto errout;
274         }
275
276         rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
277         return;
278 errout:
279         if (err < 0)
280                 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
281 }
282
283 /* Hash Ethernet address */
284 static u32 eth_hash(const unsigned char *addr)
285 {
286         u64 value = get_unaligned((u64 *)addr);
287
288         /* only want 6 bytes */
289 #ifdef __BIG_ENDIAN
290         value >>= 16;
291 #else
292         value <<= 16;
293 #endif
294         return hash_64(value, FDB_HASH_BITS);
295 }
296
297 /* Hash chain to use given mac address */
298 static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
299                                                 const u8 *mac)
300 {
301         return &vxlan->fdb_head[eth_hash(mac)];
302 }
303
304 /* Look up Ethernet address in forwarding table */
305 static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
306                                         const u8 *mac)
307 {
308         struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
309         struct vxlan_fdb *f;
310
311         hlist_for_each_entry_rcu(f, head, hlist) {
312                 if (ether_addr_equal(mac, f->eth_addr))
313                         return f;
314         }
315
316         return NULL;
317 }
318
319 static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
320                                         const u8 *mac)
321 {
322         struct vxlan_fdb *f;
323
324         f = __vxlan_find_mac(vxlan, mac);
325         if (f)
326                 f->used = jiffies;
327
328         return f;
329 }
330
331 /* caller should hold vxlan->hash_lock */
332 static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
333                                               union vxlan_addr *ip, __be16 port,
334                                               __u32 vni, __u32 ifindex)
335 {
336         struct vxlan_rdst *rd;
337
338         list_for_each_entry(rd, &f->remotes, list) {
339                 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
340                     rd->remote_port == port &&
341                     rd->remote_vni == vni &&
342                     rd->remote_ifindex == ifindex)
343                         return rd;
344         }
345
346         return NULL;
347 }
348
349 /* Replace destination of unicast mac */
350 static int vxlan_fdb_replace(struct vxlan_fdb *f,
351                              union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
352 {
353         struct vxlan_rdst *rd;
354
355         rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
356         if (rd)
357                 return 0;
358
359         rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
360         if (!rd)
361                 return 0;
362         rd->remote_ip = *ip;
363         rd->remote_port = port;
364         rd->remote_vni = vni;
365         rd->remote_ifindex = ifindex;
366         return 1;
367 }
368
369 /* Add/update destinations for multicast */
370 static int vxlan_fdb_append(struct vxlan_fdb *f,
371                             union vxlan_addr *ip, __be16 port, __u32 vni,
372                             __u32 ifindex, struct vxlan_rdst **rdp)
373 {
374         struct vxlan_rdst *rd;
375
376         rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
377         if (rd)
378                 return 0;
379
380         rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
381         if (rd == NULL)
382                 return -ENOBUFS;
383         rd->remote_ip = *ip;
384         rd->remote_port = port;
385         rd->remote_vni = vni;
386         rd->remote_ifindex = ifindex;
387
388         list_add_tail_rcu(&rd->list, &f->remotes);
389
390         *rdp = rd;
391         return 1;
392 }
393
394 #ifdef HAVE_UDP_OFFLOAD
395 #ifdef HAVE_NETIF_F_GSO_TUNNEL_REMCSUM
396 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
397                                           unsigned int off,
398                                           struct vxlanhdr *vh, size_t hdrlen,
399                                           u32 data, struct gro_remcsum *grc,
400                                           bool nopartial)
401 {
402         size_t start, offset;
403
404         if (skb->remcsum_offload)
405                 return vh;
406
407         if (!NAPI_GRO_CB(skb)->csum_valid)
408                 return NULL;
409
410         start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
411         offset = start + ((data & VXLAN_RCO_UDP) ?
412                           offsetof(struct udphdr, check) :
413                           offsetof(struct tcphdr, check));
414
415         vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
416                                      start, offset, grc, nopartial);
417
418         skb->remcsum_offload = 1;
419
420         return vh;
421 }
422 #else
423 static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
424                                           unsigned int off,
425                                           struct vxlanhdr *vh, size_t hdrlen,
426                                           u32 data, struct gro_remcsum *grc,
427                                           bool nopartial)
428 {
429         return NULL;
430 }
431 #endif
432
433 #ifndef HAVE_UDP_OFFLOAD_ARG_UOFF
434 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
435                                            struct sk_buff *skb)
436 #else
437 static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
438                                            struct sk_buff *skb,
439                                            struct udp_offload *uoff)
440 #endif
441 {
442 #ifdef HAVE_UDP_OFFLOAD_ARG_UOFF
443         struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
444                                              udp_offloads);
445 #else
446         struct vxlan_sock *vs = NULL;
447 #endif
448         struct sk_buff *p, **pp = NULL;
449         struct vxlanhdr *vh, *vh2;
450         unsigned int hlen, off_vx;
451         int flush = 1;
452         u32 flags;
453         struct gro_remcsum grc;
454
455         skb_gro_remcsum_init(&grc);
456
457         off_vx = skb_gro_offset(skb);
458         hlen = off_vx + sizeof(*vh);
459         vh   = skb_gro_header_fast(skb, off_vx);
460         if (skb_gro_header_hard(skb, hlen)) {
461                 vh = skb_gro_header_slow(skb, hlen, off_vx);
462                 if (unlikely(!vh))
463                         goto out;
464         }
465
466         skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
467
468         flags = ntohl(vh->vx_flags);
469
470         if ((flags & VXLAN_HF_RCO) && vs && (vs->flags & VXLAN_F_REMCSUM_RX)) {
471
472                 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
473                                        ntohl(vh->vx_vni), &grc,
474                                        !!(vs->flags &
475                                           VXLAN_F_REMCSUM_NOPARTIAL));
476
477                 if (!vh)
478                         goto out;
479         }
480
481         skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
482
483         flush = 0;
484
485         for (p = *head; p; p = p->next) {
486                 if (!NAPI_GRO_CB(p)->same_flow)
487                         continue;
488
489                 vh2 = (struct vxlanhdr *)(p->data + off_vx);
490                 if (vh->vx_flags != vh2->vx_flags ||
491                     vh->vx_vni != vh2->vx_vni) {
492                         NAPI_GRO_CB(p)->same_flow = 0;
493                         continue;
494                 }
495         }
496
497         pp = eth_gro_receive(head, skb);
498
499 out:
500         skb_gro_remcsum_cleanup(skb, &grc);
501         NAPI_GRO_CB(skb)->flush |= flush;
502
503         return pp;
504 }
505
506 #ifndef HAVE_UDP_OFFLOAD_ARG_UOFF
507 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff)
508 #else
509 static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
510                                struct udp_offload *uoff)
511 #endif
512 {
513         udp_tunnel_gro_complete(skb, nhoff);
514
515         return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
516 }
517
518 /* Notify netdevs that UDP port started listening */
519 static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
520 {
521         struct net_device *dev;
522         struct sock *sk = vs->sock->sk;
523         struct net *net = sock_net(sk);
524         sa_family_t sa_family = vxlan_get_sk_family(vs);
525         __be16 port = inet_sk(sk)->inet_sport;
526         int err;
527
528         if (sa_family == AF_INET) {
529                 err = udp_add_offload(&vs->udp_offloads);
530                 if (err)
531                         pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
532         }
533
534         rcu_read_lock();
535         for_each_netdev_rcu(net, dev) {
536                 if (dev->netdev_ops->ndo_add_vxlan_port)
537                         dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
538                                                             port);
539         }
540         rcu_read_unlock();
541 }
542
543 /* Notify netdevs that UDP port is no more listening */
544 static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
545 {
546         struct net_device *dev;
547         struct sock *sk = vs->sock->sk;
548         struct net *net = sock_net(sk);
549         sa_family_t sa_family = vxlan_get_sk_family(vs);
550         __be16 port = inet_sk(sk)->inet_sport;
551
552         rcu_read_lock();
553         for_each_netdev_rcu(net, dev) {
554                 if (dev->netdev_ops->ndo_del_vxlan_port)
555                         dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
556                                                             port);
557         }
558         rcu_read_unlock();
559
560         if (sa_family == AF_INET)
561                 udp_del_offload(&vs->udp_offloads);
562 }
563 #endif
564
565 /* Add new entry to forwarding table -- assumes lock held */
566 static int vxlan_fdb_create(struct vxlan_dev *vxlan,
567                             const u8 *mac, union vxlan_addr *ip,
568                             __u16 state, __u16 flags,
569                             __be16 port, __u32 vni, __u32 ifindex,
570                             __u8 ndm_flags)
571 {
572         struct vxlan_rdst *rd = NULL;
573         struct vxlan_fdb *f;
574         int notify = 0;
575
576         f = __vxlan_find_mac(vxlan, mac);
577         if (f) {
578                 if (flags & NLM_F_EXCL) {
579                         netdev_dbg(vxlan->dev,
580                                    "lost race to create %pM\n", mac);
581                         return -EEXIST;
582                 }
583                 if (f->state != state) {
584                         f->state = state;
585                         f->updated = jiffies;
586                         notify = 1;
587                 }
588                 if (f->flags != ndm_flags) {
589                         f->flags = ndm_flags;
590                         f->updated = jiffies;
591                         notify = 1;
592                 }
593                 if ((flags & NLM_F_REPLACE)) {
594                         /* Only change unicasts */
595                         if (!(is_multicast_ether_addr(f->eth_addr) ||
596                              is_zero_ether_addr(f->eth_addr))) {
597                                 notify |= vxlan_fdb_replace(f, ip, port, vni,
598                                                            ifindex);
599                         } else
600                                 return -EOPNOTSUPP;
601                 }
602                 if ((flags & NLM_F_APPEND) &&
603                     (is_multicast_ether_addr(f->eth_addr) ||
604                      is_zero_ether_addr(f->eth_addr))) {
605                         int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
606                                                   &rd);
607
608                         if (rc < 0)
609                                 return rc;
610                         notify |= rc;
611                 }
612         } else {
613                 if (!(flags & NLM_F_CREATE))
614                         return -ENOENT;
615
616                 if (vxlan->cfg.addrmax &&
617                     vxlan->addrcnt >= vxlan->cfg.addrmax)
618                         return -ENOSPC;
619
620                 /* Disallow replace to add a multicast entry */
621                 if ((flags & NLM_F_REPLACE) &&
622                     (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
623                         return -EOPNOTSUPP;
624
625                 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
626                 f = kmalloc(sizeof(*f), GFP_ATOMIC);
627                 if (!f)
628                         return -ENOMEM;
629
630                 notify = 1;
631                 f->state = state;
632                 f->flags = ndm_flags;
633                 f->updated = f->used = jiffies;
634                 INIT_LIST_HEAD(&f->remotes);
635                 memcpy(f->eth_addr, mac, ETH_ALEN);
636
637                 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
638
639                 ++vxlan->addrcnt;
640                 hlist_add_head_rcu(&f->hlist,
641                                    vxlan_fdb_head(vxlan, mac));
642         }
643
644         if (notify) {
645                 if (rd == NULL)
646                         rd = first_remote_rtnl(f);
647                 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
648         }
649
650         return 0;
651 }
652
653 static void vxlan_fdb_free(struct rcu_head *head)
654 {
655         struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
656         struct vxlan_rdst *rd, *nd;
657
658         list_for_each_entry_safe(rd, nd, &f->remotes, list)
659                 kfree(rd);
660         kfree(f);
661 }
662
663 static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
664 {
665         netdev_dbg(vxlan->dev,
666                     "delete %pM\n", f->eth_addr);
667
668         --vxlan->addrcnt;
669         vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
670
671         hlist_del_rcu(&f->hlist);
672         call_rcu(&f->rcu, vxlan_fdb_free);
673 }
674
675 /* Watch incoming packets to learn mapping between Ethernet address
676  * and Tunnel endpoint.
677  * Return true if packet is bogus and should be dropped.
678  */
679 static bool vxlan_snoop(struct net_device *dev,
680                         union vxlan_addr *src_ip, const u8 *src_mac)
681 {
682         struct vxlan_dev *vxlan = netdev_priv(dev);
683         struct vxlan_fdb *f;
684
685         f = vxlan_find_mac(vxlan, src_mac);
686         if (likely(f)) {
687                 struct vxlan_rdst *rdst = first_remote_rcu(f);
688
689                 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
690                         return false;
691
692                 /* Don't migrate static entries, drop packets */
693                 if (f->state & NUD_NOARP)
694                         return true;
695
696                 if (net_ratelimit())
697                         netdev_info(dev,
698                                     "%pM migrated from %pIS to %pIS\n",
699                                     src_mac, &rdst->remote_ip.sa, &src_ip->sa);
700
701                 rdst->remote_ip = *src_ip;
702                 f->updated = jiffies;
703                 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
704         } else {
705                 /* learned new entry */
706                 spin_lock(&vxlan->hash_lock);
707
708                 /* close off race between vxlan_flush and incoming packets */
709                 if (netif_running(dev))
710                         vxlan_fdb_create(vxlan, src_mac, src_ip,
711                                          NUD_REACHABLE,
712                                          NLM_F_EXCL|NLM_F_CREATE,
713                                          vxlan->cfg.dst_port,
714                                          vxlan->default_dst.remote_vni,
715                                          0, NTF_SELF);
716                 spin_unlock(&vxlan->hash_lock);
717         }
718
719         return false;
720 }
721
722 /* See if multicast group is already in use by other ID */
723 static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
724 {
725         struct vxlan_dev *vxlan;
726
727         /* The vxlan_sock is only used by dev, leaving group has
728          * no effect on other vxlan devices.
729          */
730         if (atomic_read(&dev->vn_sock->refcnt) == 1)
731                 return false;
732
733         list_for_each_entry(vxlan, &vn->vxlan_list, next) {
734                 if (!netif_running(vxlan->dev) || vxlan == dev)
735                         continue;
736
737                 if (vxlan->vn_sock != dev->vn_sock)
738                         continue;
739
740                 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
741                                       &dev->default_dst.remote_ip))
742                         continue;
743
744                 if (vxlan->default_dst.remote_ifindex !=
745                     dev->default_dst.remote_ifindex)
746                         continue;
747
748                 return true;
749         }
750
751         return false;
752 }
753
754 static void vxlan_sock_release(struct vxlan_sock *vs)
755 {
756         struct sock *sk = vs->sock->sk;
757         struct net *net = sock_net(sk);
758         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
759
760         if (!atomic_dec_and_test(&vs->refcnt))
761                 return;
762
763         spin_lock(&vn->sock_lock);
764         hlist_del_rcu(&vs->hlist);
765 #ifdef HAVE_UDP_OFFLOAD
766         vxlan_notify_del_rx_port(vs);
767 #endif
768         spin_unlock(&vn->sock_lock);
769
770         queue_work(vxlan_wq, &vs->del_work);
771 }
772
773 /* Update multicast group membership when first VNI on
774  * multicast address is brought up
775  */
776 static int vxlan_igmp_join(struct vxlan_dev *vxlan)
777 {
778         return -EINVAL;
779 }
780
781 /* Inverse of vxlan_igmp_join when last VNI is brought down */
782 static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
783 {
784         return -EINVAL;
785 }
786
787 #ifdef HAVE_VXLAN_HF_RCO
788 static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
789                                       size_t hdrlen, u32 data, bool nopartial)
790 {
791         size_t start, offset, plen;
792
793         if (skb->remcsum_offload)
794                 return vh;
795
796         start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
797         offset = start + ((data & VXLAN_RCO_UDP) ?
798                           offsetof(struct udphdr, check) :
799                           offsetof(struct tcphdr, check));
800
801         plen = hdrlen + offset + sizeof(u16);
802
803         if (!pskb_may_pull(skb, plen))
804                 return NULL;
805
806         vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
807
808         skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset,
809                             nopartial);
810
811         return vh;
812 }
813 #endif
814
815 static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
816                       struct vxlan_metadata *md, u32 vni,
817                       struct metadata_dst *tun_dst)
818 {
819         struct iphdr *oip = NULL;
820         struct ipv6hdr *oip6 = NULL;
821         struct vxlan_dev *vxlan;
822 #ifdef HAVE_DEV_TSTATS
823         struct pcpu_sw_netstats *stats;
824 #endif
825         union vxlan_addr saddr;
826         int err = 0;
827
828         /* For flow based devices, map all packets to VNI 0 */
829         if (vs->flags & VXLAN_F_COLLECT_METADATA)
830                 vni = 0;
831
832         /* Is this VNI defined? */
833         vxlan = vxlan_vs_find_vni(vs, vni);
834         if (!vxlan)
835                 goto drop;
836
837         skb_reset_mac_header(skb);
838         skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
839         skb->protocol = eth_type_trans(skb, vxlan->dev);
840         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
841
842         /* Ignore packet loops (and multicast echo) */
843         if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
844                 goto drop;
845
846         /* Get data from the outer IP header */
847         if (vxlan_get_sk_family(vs) == AF_INET) {
848                 oip = ip_hdr(skb);
849                 saddr.sin.sin_addr.s_addr = oip->saddr;
850                 saddr.sa.sa_family = AF_INET;
851 #if IS_ENABLED(CONFIG_IPV6)
852         } else {
853                 oip6 = ipv6_hdr(skb);
854                 saddr.sin6.sin6_addr = oip6->saddr;
855                 saddr.sa.sa_family = AF_INET6;
856 #endif
857         }
858
859         if (tun_dst) {
860                 ovs_skb_dst_set(skb, (struct dst_entry *)tun_dst);
861                 tun_dst = NULL;
862         } else {
863                 goto drop;
864         }
865
866         if ((vxlan->flags & VXLAN_F_LEARN) &&
867             vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
868                 goto drop;
869
870         skb_reset_network_header(skb);
871         /* In flow-based mode, GBP is carried in dst_metadata */
872         if (!(vs->flags & VXLAN_F_COLLECT_METADATA))
873                 skb->mark = md->gbp;
874
875         if (oip6)
876                 err = IP6_ECN_decapsulate(oip6, skb);
877         if (oip)
878                 err = IP_ECN_decapsulate(oip, skb);
879
880         if (unlikely(err)) {
881                 if (err > 1) {
882                         ++vxlan->dev->stats.rx_frame_errors;
883                         ++vxlan->dev->stats.rx_errors;
884                         goto drop;
885                 }
886         }
887
888 #ifdef HAVE_DEV_TSTATS
889         stats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)vxlan->dev->tstats);
890         u64_stats_update_begin(&stats->syncp);
891         stats->rx_packets++;
892         stats->rx_bytes += skb->len;
893         u64_stats_update_end(&stats->syncp);
894 #endif
895         netdev_port_receive(skb, skb_tunnel_info(skb));
896         return;
897 drop:
898
899         /* Consume bad packet */
900         kfree_skb(skb);
901 }
902
903 /* Callback from net/ipv4/udp.c to receive packets */
904 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
905 {
906         struct vxlan_sock *vs;
907         struct vxlanhdr *vxh;
908         u32 flags, vni;
909         struct vxlan_metadata _md;
910         struct vxlan_metadata *md = &_md;
911         union {
912                 struct metadata_dst dst;
913                 char buf[sizeof(struct metadata_dst) + sizeof(*md)];
914         } buf;
915
916         /* Need Vxlan and inner Ethernet header to be present */
917         if (!pskb_may_pull(skb, VXLAN_HLEN))
918                 goto error;
919
920         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
921         flags = ntohl(vxh->vx_flags);
922         vni = ntohl(vxh->vx_vni);
923
924         if (flags & VXLAN_HF_VNI) {
925                 flags &= ~VXLAN_HF_VNI;
926         } else {
927                 /* VNI flag always required to be set */
928                 goto bad_flags;
929         }
930
931         if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
932                 goto drop;
933         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
934
935         vs = rcu_dereference_sk_user_data(sk);
936         if (!vs)
937                 goto drop;
938
939 #ifdef HAVE_VXLAN_HF_RCO
940         if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
941                 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni,
942                                     !!(vs->flags & VXLAN_F_REMCSUM_NOPARTIAL));
943                 if (!vxh)
944                         goto drop;
945
946                 flags &= ~VXLAN_HF_RCO;
947                 vni &= VXLAN_VNI_MASK;
948         }
949 #endif
950
951         if (vxlan_collect_metadata(vs)) {
952                 ovs_udp_tun_rx_dst(&buf.dst.u.tun_info, skb, AF_INET, TUNNEL_KEY,
953                                    cpu_to_be64(vni >> 8), sizeof(*md));
954
955                 md = ip_tunnel_info_opts(&buf.dst.u.tun_info);
956         } else {
957                 memset(md, 0, sizeof(*md));
958         }
959
960         /* For backwards compatibility, only allow reserved fields to be
961          * used by VXLAN extensions if explicitly requested.
962          */
963         if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
964                 struct vxlanhdr_gbp *gbp;
965
966                 gbp = (struct vxlanhdr_gbp *)vxh;
967                 md->gbp = ntohs(gbp->policy_id);
968
969                 buf.dst.u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
970
971                 if (gbp->dont_learn)
972                         md->gbp |= VXLAN_GBP_DONT_LEARN;
973
974                 if (gbp->policy_applied)
975                         md->gbp |= VXLAN_GBP_POLICY_APPLIED;
976
977                 flags &= ~VXLAN_GBP_USED_BITS;
978         }
979
980         if (flags || vni & ~VXLAN_VNI_MASK) {
981                 /* If there are any unprocessed flags remaining treat
982                  * this as a malformed packet. This behavior diverges from
983                  * VXLAN RFC (RFC7348) which stipulates that bits in reserved
984                  * in reserved fields are to be ignored. The approach here
985                  * maintains compatibility with previous stack code, and also
986                  * is more robust and provides a little more security in
987                  * adding extensions to VXLAN.
988                  */
989
990                 goto bad_flags;
991         }
992
993         vxlan_rcv(vs, skb, md, vni >> 8, &buf.dst);
994         return 0;
995
996 drop:
997         /* Consume bad packet */
998         kfree_skb(skb);
999         return 0;
1000
1001 bad_flags:
1002         netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1003                    ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1004
1005 error:
1006         /* Return non vxlan pkt */
1007         return 1;
1008 }
1009
1010 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1011                                 struct vxlan_metadata *md)
1012 {
1013         struct vxlanhdr_gbp *gbp;
1014
1015         if (!md->gbp)
1016                 return;
1017
1018         gbp = (struct vxlanhdr_gbp *)vxh;
1019         vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1020
1021         if (md->gbp & VXLAN_GBP_DONT_LEARN)
1022                 gbp->dont_learn = 1;
1023
1024         if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1025                 gbp->policy_applied = 1;
1026
1027         gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1028 }
1029
1030 #if IS_ENABLED(CONFIG_IPV6)
1031 static int vxlan6_xmit_skb(struct dst_entry *dst, struct sock *sk,
1032                            struct sk_buff *skb,
1033                            struct net_device *dev, struct in6_addr *saddr,
1034                            struct in6_addr *daddr, __u8 prio, __u8 ttl,
1035                            __be16 src_port, __be16 dst_port, __be32 vni,
1036                            struct vxlan_metadata *md, bool xnet, u32 vxflags)
1037 {
1038         struct vxlanhdr *vxh;
1039         int min_headroom;
1040         int err;
1041         bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
1042         int type = 0;
1043
1044         if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1045             skb->ip_summed == CHECKSUM_PARTIAL) {
1046                 int csum_start = skb_checksum_start_offset(skb);
1047
1048                 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1049                     !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1050                     (skb->csum_offset == offsetof(struct udphdr, check) ||
1051                      skb->csum_offset == offsetof(struct tcphdr, check))) {
1052                         udp_sum = false;
1053                         type |= SKB_GSO_TUNNEL_REMCSUM;
1054                         /* Add support for remote csum. */
1055                         if (!SKB_GSO_TUNNEL_REMCSUM) {
1056                                 kfree_skb(skb);
1057                                 err = -EOPNOTSUPP;
1058                                 goto err;
1059                         }
1060                 }
1061         }
1062
1063         skb_scrub_packet(skb, xnet);
1064
1065         min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1066                         + VXLAN_HLEN + sizeof(struct ipv6hdr)
1067                         + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1068
1069         /* Need space for new headers (invalidates iph ptr) */
1070         err = skb_cow_head(skb, min_headroom);
1071         if (unlikely(err)) {
1072                 kfree_skb(skb);
1073                 goto err;
1074         }
1075
1076         skb = vlan_hwaccel_push_inside(skb);
1077         if (WARN_ON(!skb)) {
1078                 err = -ENOMEM;
1079                 goto err;
1080         }
1081
1082         skb = udp_tunnel_handle_offloads(skb, udp_sum, type, true);
1083         if (IS_ERR(skb)) {
1084                 err = -EINVAL;
1085                 goto err;
1086         }
1087
1088         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1089         vxh->vx_flags = htonl(VXLAN_HF_VNI);
1090         vxh->vx_vni = vni;
1091
1092         if (type & SKB_GSO_TUNNEL_REMCSUM) {
1093                 u16 hdrlen = sizeof(struct vxlanhdr);
1094                 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1095                            VXLAN_RCO_SHIFT;
1096
1097                 if (skb->csum_offset == offsetof(struct udphdr, check))
1098                         data |= VXLAN_RCO_UDP;
1099
1100                 vxh->vx_vni |= htonl(data);
1101                 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1102
1103                 if (!skb_is_gso(skb)) {
1104                         skb->ip_summed = CHECKSUM_NONE;
1105 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
1106                         skb->encapsulation = 0;
1107 #endif
1108                 }
1109         }
1110
1111         if (vxflags & VXLAN_F_GBP)
1112                 vxlan_build_gbp_hdr(vxh, vxflags, md);
1113
1114         ovs_skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1115
1116         udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio,
1117                              ttl, src_port, dst_port,
1118                              !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
1119         return 0;
1120 err:
1121         dst_release(dst);
1122         return err;
1123 }
1124 #endif
1125
1126 static int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
1127                           __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1128                           __be16 src_port, __be16 dst_port, __be32 vni,
1129                           struct vxlan_metadata *md, bool xnet, u32 vxflags)
1130 {
1131         struct vxlanhdr *vxh;
1132         int min_headroom;
1133         int err;
1134         bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
1135         int type = 0;
1136
1137         if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1138             skb->ip_summed == CHECKSUM_PARTIAL) {
1139                 int csum_start = skb_checksum_start_offset(skb);
1140
1141                 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1142                     !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1143                     (skb->csum_offset == offsetof(struct udphdr, check) ||
1144                      skb->csum_offset == offsetof(struct tcphdr, check))) {
1145                         udp_sum = false;
1146                         type |= SKB_GSO_TUNNEL_REMCSUM;
1147
1148                         if (!SKB_GSO_TUNNEL_REMCSUM) {
1149                                 kfree_skb(skb);
1150                                 return -EOPNOTSUPP;
1151                         }
1152                 }
1153         }
1154
1155         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
1156                         + VXLAN_HLEN + sizeof(struct iphdr)
1157                         + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1158
1159         /* Need space for new headers (invalidates iph ptr) */
1160         err = skb_cow_head(skb, min_headroom);
1161         if (unlikely(err)) {
1162                 kfree_skb(skb);
1163                 return err;
1164         }
1165
1166         skb = vlan_hwaccel_push_inside(skb);
1167         if (WARN_ON(!skb))
1168                 return -ENOMEM;
1169
1170         skb = udp_tunnel_handle_offloads(skb, udp_sum, type, true);
1171         if (IS_ERR(skb))
1172                 return PTR_ERR(skb);
1173
1174         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1175         vxh->vx_flags = htonl(VXLAN_HF_VNI);
1176         vxh->vx_vni = vni;
1177
1178         if (type & SKB_GSO_TUNNEL_REMCSUM) {
1179                 u16 hdrlen = sizeof(struct vxlanhdr);
1180                 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1181                            VXLAN_RCO_SHIFT;
1182
1183                 if (skb->csum_offset == offsetof(struct udphdr, check))
1184                         data |= VXLAN_RCO_UDP;
1185
1186                 vxh->vx_vni |= htonl(data);
1187                 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1188
1189                 if (!skb_is_gso(skb)) {
1190                         skb->ip_summed = CHECKSUM_NONE;
1191 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
1192                         skb->encapsulation = 0;
1193 #endif
1194                 }
1195         }
1196         if (vxflags & VXLAN_F_GBP)
1197                 vxlan_build_gbp_hdr(vxh, vxflags, md);
1198
1199         ovs_skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1200
1201         return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
1202                                    ttl, df, src_port, dst_port, xnet,
1203                                    !(vxflags & VXLAN_F_UDP_CSUM));
1204 }
1205
1206 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1207                            struct vxlan_rdst *rdst, bool did_rsc)
1208 {
1209         struct ip_tunnel_info *info;
1210         struct vxlan_dev *vxlan = netdev_priv(dev);
1211         struct sock *sk = vxlan->vn_sock->sock->sk;
1212         unsigned short family = vxlan_get_sk_family(vxlan->vn_sock);
1213         struct rtable *rt = NULL;
1214         const struct iphdr *old_iph;
1215         struct flowi4 fl4;
1216         union vxlan_addr *dst;
1217         union vxlan_addr remote_ip;
1218         struct vxlan_metadata _md;
1219         struct vxlan_metadata *md = &_md;
1220         __be16 src_port = 0, dst_port;
1221         u32 vni;
1222         __be16 df = 0;
1223         __u8 tos, ttl;
1224         int err;
1225         u32 flags = vxlan->flags;
1226
1227         info = skb_tunnel_info(skb);
1228
1229         if (rdst) {
1230                 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
1231                 vni = rdst->remote_vni;
1232                 dst = &rdst->remote_ip;
1233         } else {
1234                 if (!info) {
1235                         WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1236                                   dev->name);
1237                         goto drop;
1238                 }
1239                 if (family != ip_tunnel_info_af(info))
1240                         goto drop;
1241
1242                 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
1243                 vni = be64_to_cpu(info->key.tun_id);
1244                 remote_ip.sa.sa_family = family;
1245                 if (family == AF_INET)
1246                         remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
1247                 else
1248                         remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
1249                 dst = &remote_ip;
1250         }
1251
1252         if (vxlan_addr_any(dst)) {
1253                 if (did_rsc) {
1254                         /* short-circuited back to local bridge */
1255                         WARN_ONCE(1, "%s: vxlan_encap_bypass not supported\n",
1256                                   dev->name);
1257                 }
1258                 goto drop;
1259         }
1260
1261         old_iph = ip_hdr(skb);
1262
1263         ttl = vxlan->cfg.ttl;
1264         if (!ttl && vxlan_addr_multicast(dst))
1265                 ttl = 1;
1266
1267         tos = vxlan->cfg.tos;
1268         if (tos == 1)
1269                 tos = ip_tunnel_get_dsfield(old_iph, skb);
1270
1271         src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
1272                                      vxlan->cfg.port_max, true);
1273
1274         if (info) {
1275                 if (info->key.tun_flags & TUNNEL_CSUM)
1276                         flags |= VXLAN_F_UDP_CSUM;
1277                 else
1278                         flags &= ~VXLAN_F_UDP_CSUM;
1279
1280                 ttl = info->key.ttl;
1281                 tos = info->key.tos;
1282
1283                 if (info->options_len)
1284                         md = ip_tunnel_info_opts(info);
1285         } else {
1286                 md->gbp = skb->mark;
1287         }
1288
1289         if (dst->sa.sa_family == AF_INET) {
1290                 if (info && (info->key.tun_flags & TUNNEL_DONT_FRAGMENT))
1291                         df = htons(IP_DF);
1292
1293                 memset(&fl4, 0, sizeof(fl4));
1294                 fl4.flowi4_oif = rdst ? rdst->remote_ifindex : 0;
1295                 fl4.flowi4_tos = RT_TOS(tos);
1296                 fl4.flowi4_mark = skb->mark;
1297                 fl4.flowi4_proto = IPPROTO_UDP;
1298                 fl4.daddr = dst->sin.sin_addr.s_addr;
1299                 fl4.saddr = vxlan->cfg.saddr.sin.sin_addr.s_addr;
1300
1301                 rt = ip_route_output_key(vxlan->net, &fl4);
1302                 if (IS_ERR(rt)) {
1303                         netdev_dbg(dev, "no route to %pI4\n",
1304                                    &dst->sin.sin_addr.s_addr);
1305                         dev->stats.tx_carrier_errors++;
1306                         goto tx_error;
1307                 }
1308
1309                 if (rt_dst(rt).dev == dev) {
1310                         netdev_dbg(dev, "circular route to %pI4\n",
1311                                    &dst->sin.sin_addr.s_addr);
1312                         dev->stats.collisions++;
1313                         goto rt_tx_error;
1314                 }
1315
1316                 /* Bypass encapsulation if the destination is local */
1317                 if (rt->rt_flags & RTCF_LOCAL &&
1318                     !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1319                         struct vxlan_dev *dst_vxlan;
1320
1321                         ip_rt_put(rt);
1322                         dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1323                                                    dst->sa.sa_family, dst_port,
1324                                                    vxlan->flags);
1325                         if (!dst_vxlan)
1326                                 goto tx_error;
1327                         WARN_ONCE(1, "%s: vxlan_encap_bypass not supported\n",
1328                                   dev->name);
1329                         goto tx_error;
1330                 }
1331
1332                 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1333                 ttl = ttl ? : ip4_dst_hoplimit(&rt_dst(rt));
1334                 err = vxlan_xmit_skb(rt, sk, skb, fl4.saddr,
1335                                      dst->sin.sin_addr.s_addr, tos, ttl, df,
1336                                      src_port, dst_port, htonl(vni << 8), md,
1337                                      !net_eq(vxlan->net, dev_net(vxlan->dev)),
1338                                      flags);
1339                 if (err < 0) {
1340                         /* skb is already freed. */
1341                         skb = NULL;
1342                         goto rt_tx_error;
1343                 }
1344
1345                 iptunnel_xmit_stats(err, &dev->stats, (struct pcpu_sw_netstats __percpu *)dev->tstats);
1346 #if IS_ENABLED(CONFIG_IPV6)
1347         } else {
1348                 struct dst_entry *ndst;
1349                 struct flowi6 fl6;
1350                 u32 rt6i_flags;
1351
1352                 memset(&fl6, 0, sizeof(fl6));
1353                 fl6.flowi6_oif = rdst ? rdst->remote_ifindex : 0;
1354                 fl6.daddr = dst->sin6.sin6_addr;
1355                 fl6.saddr = vxlan->cfg.saddr.sin6.sin6_addr;
1356                 fl6.flowi6_mark = skb->mark;
1357                 fl6.flowi6_proto = IPPROTO_UDP;
1358
1359 #ifdef HAVE_IPV6_DST_LOOKUP_NET
1360                 if (ipv6_stub->ipv6_dst_lookup(vxlan->net, sk, &ndst, &fl6)) {
1361 #else
1362 #ifdef HAVE_IPV6_STUB
1363                 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1364 #else
1365                 ndst = ip6_route_output(vxlan->net, sk, &fl6);
1366                 if (ndst->error) {
1367 #endif
1368 #endif
1369                         netdev_dbg(dev, "no route to %pI6\n",
1370                                    &dst->sin6.sin6_addr);
1371                         dev->stats.tx_carrier_errors++;
1372                         goto tx_error;
1373                 }
1374
1375                 if (ndst->dev == dev) {
1376                         netdev_dbg(dev, "circular route to %pI6\n",
1377                                    &dst->sin6.sin6_addr);
1378                         dst_release(ndst);
1379                         dev->stats.collisions++;
1380                         goto tx_error;
1381                 }
1382
1383                 /* Bypass encapsulation if the destination is local */
1384                 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
1385                 if (rt6i_flags & RTF_LOCAL &&
1386                     !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1387                         struct vxlan_dev *dst_vxlan;
1388
1389                         dst_release(ndst);
1390                         dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1391                                                    dst->sa.sa_family, dst_port,
1392                                                    vxlan->flags);
1393                         if (!dst_vxlan)
1394                                 goto tx_error;
1395                         WARN_ONCE(1, "%s: vxlan_encap_bypass not supported\n",
1396                                   dev->name);
1397                         goto tx_error;
1398                 }
1399
1400                 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1401                 err = vxlan6_xmit_skb(ndst, sk, skb, dev, &fl6.saddr, &fl6.daddr,
1402                                       0, ttl, src_port, dst_port, htonl(vni << 8), md,
1403                                       !net_eq(vxlan->net, dev_net(vxlan->dev)),
1404                                       flags);
1405 #endif
1406         }
1407
1408         return;
1409
1410 drop:
1411         dev->stats.tx_dropped++;
1412         goto tx_free;
1413
1414 rt_tx_error:
1415         ip_rt_put(rt);
1416 tx_error:
1417         dev->stats.tx_errors++;
1418 tx_free:
1419         dev_kfree_skb(skb);
1420 }
1421
1422 /* Transmit local packets over Vxlan
1423  *
1424  * Outer IP header inherits ECN and DF from inner header.
1425  * Outer UDP destination is the VXLAN assigned port.
1426  *           source port is based on hash of flow
1427  */
1428 netdev_tx_t rpl_vxlan_xmit(struct sk_buff *skb)
1429 {
1430         struct net_device *dev = skb->dev;
1431         struct vxlan_dev *vxlan = netdev_priv(dev);
1432         const struct ip_tunnel_info *info;
1433
1434         info = skb_tunnel_info(skb);
1435
1436         skb_reset_mac_header(skb);
1437
1438         if ((vxlan->flags & VXLAN_F_PROXY))
1439                 goto out;
1440
1441         if (vxlan->flags & VXLAN_F_COLLECT_METADATA &&
1442             info && info->mode & IP_TUNNEL_INFO_TX) {
1443                 vxlan_xmit_one(skb, dev, NULL, false);
1444                 return NETDEV_TX_OK;
1445         }
1446 out:
1447         pr_warn("vxlan: unsupported flag set %x", vxlan->flags);
1448         kfree_skb(skb);
1449         return NETDEV_TX_OK;
1450 }
1451 EXPORT_SYMBOL(rpl_vxlan_xmit);
1452
1453 /* Walk the forwarding table and purge stale entries */
1454 static void vxlan_cleanup(unsigned long arg)
1455 {
1456         struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1457         unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1458         unsigned int h;
1459
1460         if (!netif_running(vxlan->dev))
1461                 return;
1462
1463         for (h = 0; h < FDB_HASH_SIZE; ++h) {
1464                 struct hlist_node *p, *n;
1465
1466                 spin_lock_bh(&vxlan->hash_lock);
1467                 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1468                         struct vxlan_fdb *f
1469                                 = container_of(p, struct vxlan_fdb, hlist);
1470                         unsigned long timeout;
1471
1472                         if (f->state & NUD_PERMANENT)
1473                                 continue;
1474
1475                         timeout = f->used + vxlan->cfg.age_interval * HZ;
1476                         if (time_before_eq(timeout, jiffies)) {
1477                                 netdev_dbg(vxlan->dev,
1478                                            "garbage collect %pM\n",
1479                                            f->eth_addr);
1480                                 f->state = NUD_STALE;
1481                                 vxlan_fdb_destroy(vxlan, f);
1482                         } else if (time_before(timeout, next_timer))
1483                                 next_timer = timeout;
1484                 }
1485                 spin_unlock_bh(&vxlan->hash_lock);
1486         }
1487
1488         mod_timer(&vxlan->age_timer, next_timer);
1489 }
1490
1491 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1492 {
1493         struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1494         __u32 vni = vxlan->default_dst.remote_vni;
1495
1496         vxlan->vn_sock = vs;
1497         spin_lock(&vn->sock_lock);
1498         hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1499         spin_unlock(&vn->sock_lock);
1500 }
1501
1502 /* Setup stats when device is created */
1503 #ifdef HAVE_DEV_TSTATS
1504 static int vxlan_init(struct net_device *dev)
1505 {
1506         dev->tstats = (typeof(dev->tstats)) netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1507         if (!dev->tstats)
1508                 return -ENOMEM;
1509
1510         return 0;
1511 }
1512 #endif
1513
1514 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
1515 {
1516         struct vxlan_fdb *f;
1517
1518         spin_lock_bh(&vxlan->hash_lock);
1519         f = __vxlan_find_mac(vxlan, all_zeros_mac);
1520         if (f)
1521                 vxlan_fdb_destroy(vxlan, f);
1522         spin_unlock_bh(&vxlan->hash_lock);
1523 }
1524
1525 #ifdef HAVE_DEV_TSTATS
1526 static void vxlan_uninit(struct net_device *dev)
1527 {
1528         struct vxlan_dev *vxlan = netdev_priv(dev);
1529
1530         vxlan_fdb_delete_default(vxlan);
1531
1532         free_percpu(dev->tstats);
1533 }
1534 #endif
1535
1536 /* Start ageing timer and join group when device is brought up */
1537 static int vxlan_open(struct net_device *dev)
1538 {
1539         struct vxlan_dev *vxlan = netdev_priv(dev);
1540         struct vxlan_sock *vs;
1541         int ret = 0;
1542
1543         vs = vxlan_sock_add(vxlan->net, vxlan->cfg.dst_port,
1544                             vxlan->cfg.no_share, vxlan->flags);
1545         if (IS_ERR(vs))
1546                 return PTR_ERR(vs);
1547
1548         vxlan_vs_add_dev(vs, vxlan);
1549
1550         if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
1551                 ret = vxlan_igmp_join(vxlan);
1552                 if (ret == -EADDRINUSE)
1553                         ret = 0;
1554                 if (ret) {
1555                         vxlan_sock_release(vs);
1556                         return ret;
1557                 }
1558         }
1559
1560         if (vxlan->cfg.age_interval)
1561                 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1562
1563         return ret;
1564 }
1565
1566 /* Purge the forwarding table */
1567 static void vxlan_flush(struct vxlan_dev *vxlan)
1568 {
1569         unsigned int h;
1570
1571         spin_lock_bh(&vxlan->hash_lock);
1572         for (h = 0; h < FDB_HASH_SIZE; ++h) {
1573                 struct hlist_node *p, *n;
1574
1575                 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1576                         struct vxlan_fdb *f
1577                                 = container_of(p, struct vxlan_fdb, hlist);
1578                         /* the all_zeros_mac entry is deleted at vxlan_uninit */
1579                         if (!is_zero_ether_addr(f->eth_addr))
1580                                 vxlan_fdb_destroy(vxlan, f);
1581                 }
1582         }
1583         spin_unlock_bh(&vxlan->hash_lock);
1584 }
1585
1586 /* Cleanup timer and forwarding table on shutdown */
1587 static int vxlan_stop(struct net_device *dev)
1588 {
1589         struct vxlan_dev *vxlan = netdev_priv(dev);
1590         struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1591         struct vxlan_sock *vs = vxlan->vn_sock;
1592         int ret = 0;
1593
1594         if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1595             !vxlan_group_used(vn, vxlan))
1596                 ret = vxlan_igmp_leave(vxlan);
1597
1598         del_timer_sync(&vxlan->age_timer);
1599
1600         vxlan_flush(vxlan);
1601         vxlan_sock_release(vs);
1602
1603         return ret;
1604 }
1605
1606 /* Stub, nothing needs to be done. */
1607 static void vxlan_set_multicast_list(struct net_device *dev)
1608 {
1609 }
1610
1611 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
1612 {
1613         struct vxlan_dev *vxlan = netdev_priv(dev);
1614         struct vxlan_rdst *dst = &vxlan->default_dst;
1615         struct net_device *lowerdev;
1616         int max_mtu;
1617
1618         lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
1619         if (lowerdev == NULL)
1620                 return eth_change_mtu(dev, new_mtu);
1621
1622         if (dst->remote_ip.sa.sa_family == AF_INET6)
1623                 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
1624         else
1625                 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
1626
1627         if (new_mtu < 68 || new_mtu > max_mtu)
1628                 return -EINVAL;
1629
1630         dev->mtu = new_mtu;
1631         return 0;
1632 }
1633
1634 static netdev_tx_t vxlan_dev_xmit(struct sk_buff *skb, struct net_device *dev)
1635 {
1636         /* Drop All packets coming from networking stack. OVS-CB is
1637          * not initialized for these packets.
1638          */
1639
1640         dev_kfree_skb(skb);
1641         dev->stats.tx_dropped++;
1642         return NETDEV_TX_OK;
1643 }
1644
1645 static const struct net_device_ops vxlan_netdev_ops = {
1646 #ifdef HAVE_DEV_TSTATS
1647         .ndo_init               = vxlan_init,
1648         .ndo_uninit             = vxlan_uninit,
1649         .ndo_get_stats64        = ip_tunnel_get_stats64,
1650 #endif
1651         .ndo_open               = vxlan_open,
1652         .ndo_stop               = vxlan_stop,
1653         .ndo_start_xmit         = vxlan_dev_xmit,
1654         .ndo_set_rx_mode        = vxlan_set_multicast_list,
1655         .ndo_change_mtu         = vxlan_change_mtu,
1656         .ndo_validate_addr      = eth_validate_addr,
1657         .ndo_set_mac_address    = eth_mac_addr,
1658 };
1659
1660 /* Info for udev, that this is a virtual tunnel endpoint */
1661 static struct device_type vxlan_type = {
1662         .name = "vxlan",
1663 };
1664
1665 /* Initialize the device structure. */
1666 static void vxlan_setup(struct net_device *dev)
1667 {
1668         struct vxlan_dev *vxlan = netdev_priv(dev);
1669         unsigned int h;
1670
1671         eth_hw_addr_random(dev);
1672         ether_setup(dev);
1673
1674         dev->netdev_ops = &vxlan_netdev_ops;
1675         dev->destructor = free_netdev;
1676         SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1677
1678         dev->features   |= NETIF_F_LLTX;
1679         dev->features   |= NETIF_F_SG | NETIF_F_HW_CSUM;
1680         dev->features   |= NETIF_F_RXCSUM;
1681         dev->features   |= NETIF_F_GSO_SOFTWARE;
1682
1683         dev->vlan_features = dev->features;
1684         dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
1685 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
1686         dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1687         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1688         dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
1689 #endif
1690
1691 #if 0
1692         netif_keep_dst(dev);
1693 #endif
1694         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
1695
1696         INIT_LIST_HEAD(&vxlan->next);
1697         spin_lock_init(&vxlan->hash_lock);
1698
1699         init_timer_deferrable(&vxlan->age_timer);
1700         vxlan->age_timer.function = vxlan_cleanup;
1701         vxlan->age_timer.data = (unsigned long) vxlan;
1702
1703         vxlan->cfg.dst_port = htons(vxlan_port);
1704
1705         vxlan->dev = dev;
1706
1707         for (h = 0; h < FDB_HASH_SIZE; ++h)
1708                 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1709 }
1710
1711 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1712         [IFLA_VXLAN_PORT]       = { .type = NLA_U16 },
1713 };
1714
1715 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1716 {
1717         if (tb[IFLA_ADDRESS]) {
1718                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1719                         pr_debug("invalid link address (not ethernet)\n");
1720                         return -EINVAL;
1721                 }
1722
1723                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1724                         pr_debug("invalid all zero ethernet address\n");
1725                         return -EADDRNOTAVAIL;
1726                 }
1727         }
1728
1729         if (!data)
1730                 return -EINVAL;
1731
1732         if (data[IFLA_VXLAN_ID]) {
1733                 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1734                 if (id >= VXLAN_VID_MASK)
1735                         return -ERANGE;
1736         }
1737
1738         if (data[IFLA_VXLAN_PORT_RANGE]) {
1739                 const struct ifla_vxlan_port_range *p
1740                         = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1741
1742                 if (ntohs(p->high) < ntohs(p->low)) {
1743                         pr_debug("port range %u .. %u not valid\n",
1744                                  ntohs(p->low), ntohs(p->high));
1745                         return -EINVAL;
1746                 }
1747         }
1748
1749         return 0;
1750 }
1751
1752 static void vxlan_get_drvinfo(struct net_device *netdev,
1753                               struct ethtool_drvinfo *drvinfo)
1754 {
1755         strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1756         strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1757 }
1758
1759 static const struct ethtool_ops vxlan_ethtool_ops = {
1760         .get_drvinfo    = vxlan_get_drvinfo,
1761         .get_link       = ethtool_op_get_link,
1762 };
1763
1764 static void free_vs_rcu(struct rcu_head *rcu)
1765 {
1766         struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu);
1767
1768         kfree(vs);
1769 }
1770
1771 static void vxlan_del_work(struct work_struct *work)
1772 {
1773         struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1774         udp_tunnel_sock_release(vs->sock);
1775
1776         call_rcu(&vs->rcu, free_vs_rcu);
1777 }
1778
1779 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
1780                                         __be16 port, u32 flags)
1781 {
1782         struct socket *sock;
1783         struct udp_port_cfg udp_conf;
1784         int err;
1785
1786         memset(&udp_conf, 0, sizeof(udp_conf));
1787
1788         if (ipv6) {
1789                 udp_conf.family = AF_INET6;
1790                 udp_conf.use_udp6_rx_checksums =
1791                     !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
1792                 udp_conf.ipv6_v6only = 1;
1793         } else {
1794                 udp_conf.family = AF_INET;
1795         }
1796
1797         udp_conf.local_udp_port = port;
1798
1799         /* Open UDP socket */
1800         err = udp_sock_create(net, &udp_conf, &sock);
1801         if (err < 0)
1802                 return ERR_PTR(err);
1803
1804         return sock;
1805 }
1806
1807 /* Create new listen socket if needed */
1808 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
1809                                               u32 flags)
1810 {
1811         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1812         struct vxlan_sock *vs;
1813         struct socket *sock;
1814         unsigned int h;
1815         bool ipv6 = !!(flags & VXLAN_F_IPV6);
1816         struct udp_tunnel_sock_cfg tunnel_cfg;
1817
1818         vs = kzalloc(sizeof(*vs), GFP_KERNEL);
1819         if (!vs)
1820                 return ERR_PTR(-ENOMEM);
1821
1822         for (h = 0; h < VNI_HASH_SIZE; ++h)
1823                 INIT_HLIST_HEAD(&vs->vni_list[h]);
1824
1825         INIT_WORK(&vs->del_work, vxlan_del_work);
1826
1827         sock = vxlan_create_sock(net, ipv6, port, flags);
1828         if (IS_ERR(sock)) {
1829                 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
1830                         PTR_ERR(sock));
1831                 kfree(vs);
1832                 return ERR_CAST(sock);
1833         }
1834
1835         vs->sock = sock;
1836         atomic_set(&vs->refcnt, 1);
1837         vs->flags = (flags & VXLAN_F_RCV_FLAGS);
1838
1839         /* Initialize the vxlan udp offloads structure */
1840 #ifdef HAVE_UDP_OFFLOAD
1841         vs->udp_offloads.port = port;
1842         vs->udp_offloads.callbacks.gro_receive  = vxlan_gro_receive;
1843         vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
1844         vxlan_notify_add_rx_port(vs);
1845 #endif
1846
1847         spin_lock(&vn->sock_lock);
1848         hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1849         spin_unlock(&vn->sock_lock);
1850
1851         /* Mark socket as an encapsulation socket. */
1852         tunnel_cfg.sk_user_data = vs;
1853         tunnel_cfg.encap_type = 1;
1854         tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
1855         tunnel_cfg.encap_destroy = NULL;
1856
1857         setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
1858
1859         return vs;
1860 }
1861
1862 static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
1863                                          bool no_share, u32 flags)
1864 {
1865         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1866         struct vxlan_sock *vs;
1867         bool ipv6 = flags & VXLAN_F_IPV6;
1868
1869         if (!no_share) {
1870                 spin_lock(&vn->sock_lock);
1871                 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port,
1872                                      flags);
1873                 if (vs) {
1874                         if (!atomic_add_unless(&vs->refcnt, 1, 0))
1875                                 vs = ERR_PTR(-EBUSY);
1876                         spin_unlock(&vn->sock_lock);
1877                         return vs;
1878                 }
1879                 spin_unlock(&vn->sock_lock);
1880         }
1881
1882         return vxlan_socket_create(net, port, flags);
1883 }
1884
1885 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
1886                                struct vxlan_config *conf)
1887 {
1888         struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
1889         struct vxlan_dev *vxlan = netdev_priv(dev);
1890         struct vxlan_rdst *dst = &vxlan->default_dst;
1891         int err;
1892         bool use_ipv6 = false;
1893         __be16 default_port = vxlan->cfg.dst_port;
1894
1895         vxlan->net = src_net;
1896
1897         dst->remote_vni = conf->vni;
1898
1899         memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
1900
1901         /* Unless IPv6 is explicitly requested, assume IPv4 */
1902         if (!dst->remote_ip.sa.sa_family)
1903                 dst->remote_ip.sa.sa_family = AF_INET;
1904
1905         if (dst->remote_ip.sa.sa_family == AF_INET6 ||
1906             vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
1907                 if (!IS_ENABLED(CONFIG_IPV6))
1908                         return -EPFNOSUPPORT;
1909                 use_ipv6 = true;
1910         }
1911
1912         if (conf->remote_ifindex) {
1913                 struct net_device *lowerdev
1914                          = __dev_get_by_index(src_net, conf->remote_ifindex);
1915
1916                 dst->remote_ifindex = conf->remote_ifindex;
1917
1918                 if (!lowerdev) {
1919                         pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
1920                         return -ENODEV;
1921                 }
1922
1923 #if IS_ENABLED(CONFIG_IPV6)
1924                 if (use_ipv6) {
1925                         struct inet6_dev *idev = __in6_dev_get(lowerdev);
1926                         if (idev && idev->cnf.disable_ipv6) {
1927                                 pr_info("IPv6 is disabled via sysctl\n");
1928                                 return -EPERM;
1929                         }
1930                         vxlan->flags |= VXLAN_F_IPV6;
1931                 }
1932 #endif
1933
1934                 if (!conf->mtu)
1935                         dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
1936
1937                 dev->needed_headroom = lowerdev->hard_header_len +
1938                                        (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
1939         } else if (use_ipv6) {
1940                 vxlan->flags |= VXLAN_F_IPV6;
1941                 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
1942         } else {
1943                 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
1944         }
1945
1946         memcpy(&vxlan->cfg, conf, sizeof(*conf));
1947         if (!vxlan->cfg.dst_port)
1948                 vxlan->cfg.dst_port = default_port;
1949         vxlan->flags |= conf->flags;
1950
1951         if (!vxlan->cfg.age_interval)
1952                 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
1953
1954         if (vxlan_find_vni(src_net, conf->vni, use_ipv6 ? AF_INET6 : AF_INET,
1955                            vxlan->cfg.dst_port, vxlan->flags))
1956                 return -EEXIST;
1957
1958         dev->ethtool_ops = &vxlan_ethtool_ops;
1959
1960         /* create an fdb entry for a valid default destination */
1961         if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
1962                 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1963                                        &vxlan->default_dst.remote_ip,
1964                                        NUD_REACHABLE|NUD_PERMANENT,
1965                                        NLM_F_EXCL|NLM_F_CREATE,
1966                                        vxlan->cfg.dst_port,
1967                                        vxlan->default_dst.remote_vni,
1968                                        vxlan->default_dst.remote_ifindex,
1969                                        NTF_SELF);
1970                 if (err)
1971                         return err;
1972         }
1973
1974         err = register_netdevice(dev);
1975         if (err) {
1976                 vxlan_fdb_delete_default(vxlan);
1977                 return err;
1978         }
1979
1980         list_add(&vxlan->next, &vn->vxlan_list);
1981
1982         return 0;
1983 }
1984
1985 struct net_device *rpl_vxlan_dev_create(struct net *net, const char *name,
1986                                     u8 name_assign_type, struct vxlan_config *conf)
1987 {
1988         struct nlattr *tb[IFLA_MAX+1];
1989         struct net_device *dev;
1990         int err;
1991
1992         memset(&tb, 0, sizeof(tb));
1993
1994         dev = rtnl_create_link(net, (char *)name, name_assign_type,
1995                                &vxlan_link_ops, tb);
1996         if (IS_ERR(dev))
1997                 return dev;
1998
1999         err = vxlan_dev_configure(net, dev, conf);
2000         if (err < 0) {
2001                 free_netdev(dev);
2002                 return ERR_PTR(err);
2003         }
2004
2005         return dev;
2006 }
2007 EXPORT_SYMBOL_GPL(rpl_vxlan_dev_create);
2008
2009 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
2010 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2011                          struct nlattr *tb[], struct nlattr *data[])
2012 #else
2013 static int vxlan_newlink(struct net_device *dev,
2014                          struct nlattr *tb[], struct nlattr *data[])
2015 #endif
2016 {
2017         return -EINVAL;
2018 }
2019
2020 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
2021 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2022 #else
2023 static void vxlan_dellink(struct net_device *dev)
2024 #endif
2025 {
2026         struct vxlan_dev *vxlan = netdev_priv(dev);
2027         struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2028
2029         spin_lock(&vn->sock_lock);
2030         if (!hlist_unhashed(&vxlan->hlist))
2031                 hlist_del_rcu(&vxlan->hlist);
2032         spin_unlock(&vn->sock_lock);
2033
2034         list_del(&vxlan->next);
2035         unregister_netdevice_queue(dev, head);
2036 }
2037
2038 static size_t vxlan_get_size(const struct net_device *dev)
2039 {
2040
2041         return nla_total_size(sizeof(__u32)) +  /* IFLA_VXLAN_ID */
2042                 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2043                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2044                 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2045                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TTL */
2046                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TOS */
2047                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_LEARNING */
2048                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_PROXY */
2049                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_RSC */
2050                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L2MISS */
2051                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L3MISS */
2052                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_COLLECT_METADATA */
2053                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2054                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2055                 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2056                 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2057                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2058                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2059                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2060                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2061                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
2062                 0;
2063 }
2064
2065 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2066 {
2067         const struct vxlan_dev *vxlan = netdev_priv(dev);
2068
2069         if (nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port))
2070                 goto nla_put_failure;
2071
2072         return 0;
2073
2074 nla_put_failure:
2075         return -EMSGSIZE;
2076 }
2077
2078 #ifdef HAVE_GET_LINK_NET
2079 static struct net *vxlan_get_link_net(const struct net_device *dev)
2080 {
2081         struct vxlan_dev *vxlan = netdev_priv(dev);
2082
2083         return vxlan->net;
2084 }
2085 #endif
2086
2087 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2088         .kind           = "ovs_vxlan",
2089         .maxtype        = IFLA_VXLAN_MAX,
2090         .policy         = vxlan_policy,
2091         .priv_size      = sizeof(struct vxlan_dev),
2092         .setup          = vxlan_setup,
2093         .validate       = vxlan_validate,
2094         .newlink        = vxlan_newlink,
2095         .dellink        = vxlan_dellink,
2096         .get_size       = vxlan_get_size,
2097         .fill_info      = vxlan_fill_info,
2098 #ifdef HAVE_GET_LINK_NET
2099         .get_link_net   = vxlan_get_link_net,
2100 #endif
2101 };
2102
2103 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2104                                              struct net_device *dev)
2105 {
2106         struct vxlan_dev *vxlan, *next;
2107         LIST_HEAD(list_kill);
2108
2109         list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2110                 struct vxlan_rdst *dst = &vxlan->default_dst;
2111
2112                 /* In case we created vxlan device with carrier
2113                  * and we loose the carrier due to module unload
2114                  * we also need to remove vxlan device. In other
2115                  * cases, it's not necessary and remote_ifindex
2116                  * is 0 here, so no matches.
2117                  */
2118                 if (dst->remote_ifindex == dev->ifindex)
2119 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
2120                         vxlan_dellink(vxlan->dev, &list_kill);
2121 #else
2122                         vxlan_dellink(vxlan->dev);
2123 #endif
2124         }
2125
2126         unregister_netdevice_many(&list_kill);
2127 }
2128
2129 static int vxlan_lowerdev_event(struct notifier_block *unused,
2130                                 unsigned long event, void *ptr)
2131 {
2132         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2133         struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2134
2135         if (event == NETDEV_UNREGISTER)
2136                 vxlan_handle_lowerdev_unregister(vn, dev);
2137
2138         return NOTIFY_DONE;
2139 }
2140
2141 static struct notifier_block vxlan_notifier_block __read_mostly = {
2142         .notifier_call = vxlan_lowerdev_event,
2143 };
2144
2145 static __net_init int vxlan_init_net(struct net *net)
2146 {
2147         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2148         unsigned int h;
2149
2150         INIT_LIST_HEAD(&vn->vxlan_list);
2151         spin_lock_init(&vn->sock_lock);
2152
2153         for (h = 0; h < PORT_HASH_SIZE; ++h)
2154                 INIT_HLIST_HEAD(&vn->sock_list[h]);
2155
2156         return 0;
2157 }
2158
2159 static void __net_exit vxlan_exit_net(struct net *net)
2160 {
2161         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2162         struct vxlan_dev *vxlan, *next;
2163         struct net_device *dev, *aux;
2164         LIST_HEAD(list);
2165
2166         rtnl_lock();
2167         for_each_netdev_safe(net, dev, aux)
2168                 if (dev->rtnl_link_ops == &vxlan_link_ops)
2169                         unregister_netdevice_queue(dev, &list);
2170
2171         list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2172                 /* If vxlan->dev is in the same netns, it has already been added
2173                  * to the list by the previous loop.
2174                  */
2175                 if (!net_eq(dev_net(vxlan->dev), net))
2176                         unregister_netdevice_queue(vxlan->dev, &list);
2177         }
2178
2179         unregister_netdevice_many(&list);
2180         rtnl_unlock();
2181 }
2182
2183 static struct pernet_operations vxlan_net_ops = {
2184         .init = vxlan_init_net,
2185         .exit = vxlan_exit_net,
2186         .id   = &vxlan_net_id,
2187         .size = sizeof(struct vxlan_net),
2188 };
2189
2190 DEFINE_COMPAT_PNET_REG_FUNC(device)
2191 int rpl_vxlan_init_module(void)
2192 {
2193         int rc;
2194
2195 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
2196         vxlan_wq = create_workqueue("vxlan");
2197 #else
2198         vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2199 #endif
2200         if (!vxlan_wq)
2201                 return -ENOMEM;
2202
2203         get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2204
2205         rc = register_pernet_subsys(&vxlan_net_ops);
2206         if (rc)
2207                 goto out1;
2208
2209         rc = register_netdevice_notifier(&vxlan_notifier_block);
2210         if (rc)
2211                 goto out2;
2212
2213         rc = rtnl_link_register(&vxlan_link_ops);
2214         if (rc)
2215                 goto out3;
2216
2217         pr_info("VxLAN tunneling driver\n");
2218         return 0;
2219 out3:
2220         unregister_netdevice_notifier(&vxlan_notifier_block);
2221 out2:
2222         unregister_pernet_subsys(&vxlan_net_ops);
2223 out1:
2224         destroy_workqueue(vxlan_wq);
2225         return rc;
2226 }
2227
2228 void rpl_vxlan_cleanup_module(void)
2229 {
2230         rtnl_link_unregister(&vxlan_link_ops);
2231         unregister_netdevice_notifier(&vxlan_notifier_block);
2232         destroy_workqueue(vxlan_wq);
2233         unregister_pernet_subsys(&vxlan_net_ops);
2234         /* rcu_barrier() is called by netns */
2235 }
2236 #endif