datapath: Add support for lwtunnel
[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         union vxlan_addr *remote_ip;
828
829         /* For flow based devices, map all packets to VNI 0 */
830         if (vs->flags & VXLAN_F_COLLECT_METADATA)
831                 vni = 0;
832
833         /* Is this VNI defined? */
834         vxlan = vxlan_vs_find_vni(vs, vni);
835         if (!vxlan)
836                 goto drop;
837
838         remote_ip = &vxlan->default_dst.remote_ip;
839         skb_reset_mac_header(skb);
840         skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
841         skb->protocol = eth_type_trans(skb, vxlan->dev);
842         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
843
844         /* Ignore packet loops (and multicast echo) */
845         if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
846                 goto drop;
847
848         /* Re-examine inner Ethernet packet */
849         if (remote_ip->sa.sa_family == AF_INET) {
850                 oip = ip_hdr(skb);
851                 saddr.sin.sin_addr.s_addr = oip->saddr;
852                 saddr.sa.sa_family = AF_INET;
853 #if IS_ENABLED(CONFIG_IPV6)
854         } else {
855                 oip6 = ipv6_hdr(skb);
856                 saddr.sin6.sin6_addr = oip6->saddr;
857                 saddr.sa.sa_family = AF_INET6;
858 #endif
859         }
860
861         if (tun_dst) {
862                 ovs_skb_dst_set(skb, (struct dst_entry *)tun_dst);
863                 tun_dst = NULL;
864         } else {
865                 goto drop;
866         }
867
868         if ((vxlan->flags & VXLAN_F_LEARN) &&
869             vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
870                 goto drop;
871
872         skb_reset_network_header(skb);
873         /* In flow-based mode, GBP is carried in dst_metadata */
874         if (!(vs->flags & VXLAN_F_COLLECT_METADATA))
875                 skb->mark = md->gbp;
876
877         if (oip6)
878                 err = IP6_ECN_decapsulate(oip6, skb);
879         if (oip)
880                 err = IP_ECN_decapsulate(oip, skb);
881
882         if (unlikely(err)) {
883                 if (err > 1) {
884                         ++vxlan->dev->stats.rx_frame_errors;
885                         ++vxlan->dev->stats.rx_errors;
886                         goto drop;
887                 }
888         }
889
890 #ifdef HAVE_DEV_TSTATS
891         stats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)vxlan->dev->tstats);
892         u64_stats_update_begin(&stats->syncp);
893         stats->rx_packets++;
894         stats->rx_bytes += skb->len;
895         u64_stats_update_end(&stats->syncp);
896 #endif
897         netdev_port_receive(skb, skb_tunnel_info(skb));
898         return;
899 drop:
900
901         /* Consume bad packet */
902         kfree_skb(skb);
903 }
904
905 /* Callback from net/ipv4/udp.c to receive packets */
906 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
907 {
908         struct vxlan_sock *vs;
909         struct vxlanhdr *vxh;
910         u32 flags, vni;
911         struct vxlan_metadata _md;
912         struct vxlan_metadata *md = &_md;
913         union {
914                 struct metadata_dst dst;
915                 char buf[sizeof(struct metadata_dst) + sizeof(*md)];
916         } buf;
917
918         /* Need Vxlan and inner Ethernet header to be present */
919         if (!pskb_may_pull(skb, VXLAN_HLEN))
920                 goto error;
921
922         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
923         flags = ntohl(vxh->vx_flags);
924         vni = ntohl(vxh->vx_vni);
925
926         if (flags & VXLAN_HF_VNI) {
927                 flags &= ~VXLAN_HF_VNI;
928         } else {
929                 /* VNI flag always required to be set */
930                 goto bad_flags;
931         }
932
933         if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
934                 goto drop;
935         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
936
937         vs = rcu_dereference_sk_user_data(sk);
938         if (!vs)
939                 goto drop;
940
941 #ifdef HAVE_VXLAN_HF_RCO
942         if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
943                 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni,
944                                     !!(vs->flags & VXLAN_F_REMCSUM_NOPARTIAL));
945                 if (!vxh)
946                         goto drop;
947
948                 flags &= ~VXLAN_HF_RCO;
949                 vni &= VXLAN_VNI_MASK;
950         }
951 #endif
952
953         if (vxlan_collect_metadata(vs)) {
954                 ovs_udp_tun_rx_dst(&buf.dst.u.tun_info, skb, AF_INET, TUNNEL_KEY,
955                                    cpu_to_be64(vni >> 8), sizeof(*md));
956
957                 md = ip_tunnel_info_opts(&buf.dst.u.tun_info);
958         } else {
959                 memset(md, 0, sizeof(*md));
960         }
961
962         /* For backwards compatibility, only allow reserved fields to be
963          * used by VXLAN extensions if explicitly requested.
964          */
965         if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
966                 struct vxlanhdr_gbp *gbp;
967
968                 gbp = (struct vxlanhdr_gbp *)vxh;
969                 md->gbp = ntohs(gbp->policy_id);
970
971                 buf.dst.u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
972
973                 if (gbp->dont_learn)
974                         md->gbp |= VXLAN_GBP_DONT_LEARN;
975
976                 if (gbp->policy_applied)
977                         md->gbp |= VXLAN_GBP_POLICY_APPLIED;
978
979                 flags &= ~VXLAN_GBP_USED_BITS;
980         }
981
982         if (flags || vni & ~VXLAN_VNI_MASK) {
983                 /* If there are any unprocessed flags remaining treat
984                  * this as a malformed packet. This behavior diverges from
985                  * VXLAN RFC (RFC7348) which stipulates that bits in reserved
986                  * in reserved fields are to be ignored. The approach here
987                  * maintains compatibility with previous stack code, and also
988                  * is more robust and provides a little more security in
989                  * adding extensions to VXLAN.
990                  */
991
992                 goto bad_flags;
993         }
994
995         vxlan_rcv(vs, skb, md, vni >> 8, &buf.dst);
996         return 0;
997
998 drop:
999         /* Consume bad packet */
1000         kfree_skb(skb);
1001         return 0;
1002
1003 bad_flags:
1004         netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1005                    ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1006
1007 error:
1008         /* Return non vxlan pkt */
1009         return 1;
1010 }
1011
1012 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
1013                                 struct vxlan_metadata *md)
1014 {
1015         struct vxlanhdr_gbp *gbp;
1016
1017         if (!md->gbp)
1018                 return;
1019
1020         gbp = (struct vxlanhdr_gbp *)vxh;
1021         vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1022
1023         if (md->gbp & VXLAN_GBP_DONT_LEARN)
1024                 gbp->dont_learn = 1;
1025
1026         if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1027                 gbp->policy_applied = 1;
1028
1029         gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1030 }
1031
1032 #if IS_ENABLED(CONFIG_IPV6)
1033 static int vxlan6_xmit_skb(struct dst_entry *dst, struct sock *sk,
1034                            struct sk_buff *skb,
1035                            struct net_device *dev, struct in6_addr *saddr,
1036                            struct in6_addr *daddr, __u8 prio, __u8 ttl,
1037                            __be16 src_port, __be16 dst_port, __be32 vni,
1038                            struct vxlan_metadata *md, bool xnet, u32 vxflags)
1039 {
1040         struct vxlanhdr *vxh;
1041         int min_headroom;
1042         int err;
1043         bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
1044         int type = 0;
1045
1046         if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1047             skb->ip_summed == CHECKSUM_PARTIAL) {
1048                 int csum_start = skb_checksum_start_offset(skb);
1049
1050                 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1051                     !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1052                     (skb->csum_offset == offsetof(struct udphdr, check) ||
1053                      skb->csum_offset == offsetof(struct tcphdr, check))) {
1054                         udp_sum = false;
1055                         type |= SKB_GSO_TUNNEL_REMCSUM;
1056                         /* Add support for remote csum. */
1057                         if (!SKB_GSO_TUNNEL_REMCSUM) {
1058                                 kfree_skb(skb);
1059                                 err = -EOPNOTSUPP;
1060                                 goto err;
1061                         }
1062                 }
1063         }
1064
1065         skb_scrub_packet(skb, xnet);
1066
1067         min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1068                         + VXLAN_HLEN + sizeof(struct ipv6hdr)
1069                         + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1070
1071         /* Need space for new headers (invalidates iph ptr) */
1072         err = skb_cow_head(skb, min_headroom);
1073         if (unlikely(err)) {
1074                 kfree_skb(skb);
1075                 goto err;
1076         }
1077
1078         skb = vlan_hwaccel_push_inside(skb);
1079         if (WARN_ON(!skb)) {
1080                 err = -ENOMEM;
1081                 goto err;
1082         }
1083
1084         skb = udp_tunnel_handle_offloads(skb, udp_sum, type, true);
1085         if (IS_ERR(skb)) {
1086                 err = -EINVAL;
1087                 goto err;
1088         }
1089
1090         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1091         vxh->vx_flags = htonl(VXLAN_HF_VNI);
1092         vxh->vx_vni = vni;
1093
1094         if (type & SKB_GSO_TUNNEL_REMCSUM) {
1095                 u16 hdrlen = sizeof(struct vxlanhdr);
1096                 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1097                            VXLAN_RCO_SHIFT;
1098
1099                 if (skb->csum_offset == offsetof(struct udphdr, check))
1100                         data |= VXLAN_RCO_UDP;
1101
1102                 vxh->vx_vni |= htonl(data);
1103                 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1104
1105                 if (!skb_is_gso(skb)) {
1106                         skb->ip_summed = CHECKSUM_NONE;
1107 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
1108                         skb->encapsulation = 0;
1109 #endif
1110                 }
1111         }
1112
1113         if (vxflags & VXLAN_F_GBP)
1114                 vxlan_build_gbp_hdr(vxh, vxflags, md);
1115
1116         ovs_skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1117
1118         udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio,
1119                              ttl, src_port, dst_port,
1120                              !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
1121         return 0;
1122 err:
1123         dst_release(dst);
1124         return err;
1125 }
1126 #endif
1127
1128 static int vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
1129                           __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1130                           __be16 src_port, __be16 dst_port, __be32 vni,
1131                           struct vxlan_metadata *md, bool xnet, u32 vxflags)
1132 {
1133         struct vxlanhdr *vxh;
1134         int min_headroom;
1135         int err;
1136         bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
1137         int type = 0;
1138
1139         if ((vxflags & VXLAN_F_REMCSUM_TX) &&
1140             skb->ip_summed == CHECKSUM_PARTIAL) {
1141                 int csum_start = skb_checksum_start_offset(skb);
1142
1143                 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1144                     !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1145                     (skb->csum_offset == offsetof(struct udphdr, check) ||
1146                      skb->csum_offset == offsetof(struct tcphdr, check))) {
1147                         udp_sum = false;
1148                         type |= SKB_GSO_TUNNEL_REMCSUM;
1149
1150                         if (!SKB_GSO_TUNNEL_REMCSUM) {
1151                                 kfree_skb(skb);
1152                                 return -EOPNOTSUPP;
1153                         }
1154                 }
1155         }
1156
1157         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
1158                         + VXLAN_HLEN + sizeof(struct iphdr)
1159                         + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
1160
1161         /* Need space for new headers (invalidates iph ptr) */
1162         err = skb_cow_head(skb, min_headroom);
1163         if (unlikely(err)) {
1164                 kfree_skb(skb);
1165                 return err;
1166         }
1167
1168         skb = vlan_hwaccel_push_inside(skb);
1169         if (WARN_ON(!skb))
1170                 return -ENOMEM;
1171
1172         skb = udp_tunnel_handle_offloads(skb, udp_sum, type, true);
1173         if (IS_ERR(skb))
1174                 return PTR_ERR(skb);
1175
1176         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1177         vxh->vx_flags = htonl(VXLAN_HF_VNI);
1178         vxh->vx_vni = vni;
1179
1180         if (type & SKB_GSO_TUNNEL_REMCSUM) {
1181                 u16 hdrlen = sizeof(struct vxlanhdr);
1182                 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1183                            VXLAN_RCO_SHIFT;
1184
1185                 if (skb->csum_offset == offsetof(struct udphdr, check))
1186                         data |= VXLAN_RCO_UDP;
1187
1188                 vxh->vx_vni |= htonl(data);
1189                 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1190
1191                 if (!skb_is_gso(skb)) {
1192                         skb->ip_summed = CHECKSUM_NONE;
1193 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
1194                         skb->encapsulation = 0;
1195 #endif
1196                 }
1197         }
1198         if (vxflags & VXLAN_F_GBP)
1199                 vxlan_build_gbp_hdr(vxh, vxflags, md);
1200
1201         ovs_skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1202
1203         return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
1204                                    ttl, df, src_port, dst_port, xnet,
1205                                    !(vxflags & VXLAN_F_UDP_CSUM));
1206 }
1207
1208 static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1209                            struct vxlan_rdst *rdst, bool did_rsc)
1210 {
1211         struct ip_tunnel_info *info;
1212         struct vxlan_dev *vxlan = netdev_priv(dev);
1213         struct sock *sk = vxlan->vn_sock->sock->sk;
1214         unsigned short family = vxlan_get_sk_family(vxlan->vn_sock);
1215         struct rtable *rt = NULL;
1216         const struct iphdr *old_iph;
1217         struct flowi4 fl4;
1218         union vxlan_addr *dst;
1219         union vxlan_addr remote_ip;
1220         struct vxlan_metadata _md;
1221         struct vxlan_metadata *md = &_md;
1222         __be16 src_port = 0, dst_port;
1223         u32 vni;
1224         __be16 df = 0;
1225         __u8 tos, ttl;
1226         int err;
1227         u32 flags = vxlan->flags;
1228
1229         info = skb_tunnel_info(skb);
1230
1231         if (rdst) {
1232                 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
1233                 vni = rdst->remote_vni;
1234                 dst = &rdst->remote_ip;
1235         } else {
1236                 if (!info) {
1237                         WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
1238                                   dev->name);
1239                         goto drop;
1240                 }
1241                 if (family != ip_tunnel_info_af(info))
1242                         goto drop;
1243
1244                 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
1245                 vni = be64_to_cpu(info->key.tun_id);
1246                 remote_ip.sa.sa_family = family;
1247                 if (family == AF_INET)
1248                         remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
1249                 else
1250                         remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
1251                 dst = &remote_ip;
1252         }
1253
1254         if (vxlan_addr_any(dst)) {
1255                 if (did_rsc) {
1256                         /* short-circuited back to local bridge */
1257                         WARN_ONCE(1, "%s: vxlan_encap_bypass not supported\n",
1258                                   dev->name);
1259                 }
1260                 goto drop;
1261         }
1262
1263         old_iph = ip_hdr(skb);
1264
1265         ttl = vxlan->cfg.ttl;
1266         if (!ttl && vxlan_addr_multicast(dst))
1267                 ttl = 1;
1268
1269         tos = vxlan->cfg.tos;
1270         if (tos == 1)
1271                 tos = ip_tunnel_get_dsfield(old_iph, skb);
1272
1273         src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
1274                                      vxlan->cfg.port_max, true);
1275
1276         if (info) {
1277                 if (info->key.tun_flags & TUNNEL_CSUM)
1278                         flags |= VXLAN_F_UDP_CSUM;
1279                 else
1280                         flags &= ~VXLAN_F_UDP_CSUM;
1281
1282                 ttl = info->key.ttl;
1283                 tos = info->key.tos;
1284
1285                 if (info->options_len)
1286                         md = ip_tunnel_info_opts(info);
1287         } else {
1288                 md->gbp = skb->mark;
1289         }
1290
1291         if (dst->sa.sa_family == AF_INET) {
1292                 if (info && (info->key.tun_flags & TUNNEL_DONT_FRAGMENT))
1293                         df = htons(IP_DF);
1294
1295                 memset(&fl4, 0, sizeof(fl4));
1296                 fl4.flowi4_oif = rdst ? rdst->remote_ifindex : 0;
1297                 fl4.flowi4_tos = RT_TOS(tos);
1298                 fl4.flowi4_mark = skb->mark;
1299                 fl4.flowi4_proto = IPPROTO_UDP;
1300                 fl4.daddr = dst->sin.sin_addr.s_addr;
1301                 fl4.saddr = vxlan->cfg.saddr.sin.sin_addr.s_addr;
1302
1303                 rt = ip_route_output_key(vxlan->net, &fl4);
1304                 if (IS_ERR(rt)) {
1305                         netdev_dbg(dev, "no route to %pI4\n",
1306                                    &dst->sin.sin_addr.s_addr);
1307                         dev->stats.tx_carrier_errors++;
1308                         goto tx_error;
1309                 }
1310
1311                 if (rt_dst(rt).dev == dev) {
1312                         netdev_dbg(dev, "circular route to %pI4\n",
1313                                    &dst->sin.sin_addr.s_addr);
1314                         dev->stats.collisions++;
1315                         goto rt_tx_error;
1316                 }
1317
1318                 /* Bypass encapsulation if the destination is local */
1319                 if (rt->rt_flags & RTCF_LOCAL &&
1320                     !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1321                         struct vxlan_dev *dst_vxlan;
1322
1323                         ip_rt_put(rt);
1324                         dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1325                                                    dst->sa.sa_family, dst_port,
1326                                                    vxlan->flags);
1327                         if (!dst_vxlan)
1328                                 goto tx_error;
1329                         WARN_ONCE(1, "%s: vxlan_encap_bypass not supported\n",
1330                                   dev->name);
1331                         goto tx_error;
1332                 }
1333
1334                 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1335                 ttl = ttl ? : ip4_dst_hoplimit(&rt_dst(rt));
1336                 err = vxlan_xmit_skb(rt, sk, skb, fl4.saddr,
1337                                      dst->sin.sin_addr.s_addr, tos, ttl, df,
1338                                      src_port, dst_port, htonl(vni << 8), md,
1339                                      !net_eq(vxlan->net, dev_net(vxlan->dev)),
1340                                      flags);
1341                 if (err < 0) {
1342                         /* skb is already freed. */
1343                         skb = NULL;
1344                         goto rt_tx_error;
1345                 }
1346
1347                 iptunnel_xmit_stats(err, &dev->stats, (struct pcpu_sw_netstats __percpu *)dev->tstats);
1348 #if IS_ENABLED(CONFIG_IPV6)
1349         } else {
1350                 struct dst_entry *ndst;
1351                 struct flowi6 fl6;
1352                 u32 rt6i_flags;
1353
1354                 memset(&fl6, 0, sizeof(fl6));
1355                 fl6.flowi6_oif = rdst ? rdst->remote_ifindex : 0;
1356                 fl6.daddr = dst->sin6.sin6_addr;
1357                 fl6.saddr = vxlan->cfg.saddr.sin6.sin6_addr;
1358                 fl6.flowi6_mark = skb->mark;
1359                 fl6.flowi6_proto = IPPROTO_UDP;
1360
1361 #ifdef HAVE_IPV6_DST_LOOKUP_NET
1362                 if (ipv6_stub->ipv6_dst_lookup(vxlan->net, sk, &ndst, &fl6)) {
1363 #else
1364 #ifdef HAVE_IPV6_STUB
1365                 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1366 #else
1367                 ndst = ip6_route_output(vxlan->net, sk, &fl6);
1368                 if (ndst->error) {
1369 #endif
1370 #endif
1371                         netdev_dbg(dev, "no route to %pI6\n",
1372                                    &dst->sin6.sin6_addr);
1373                         dev->stats.tx_carrier_errors++;
1374                         goto tx_error;
1375                 }
1376
1377                 if (ndst->dev == dev) {
1378                         netdev_dbg(dev, "circular route to %pI6\n",
1379                                    &dst->sin6.sin6_addr);
1380                         dst_release(ndst);
1381                         dev->stats.collisions++;
1382                         goto tx_error;
1383                 }
1384
1385                 /* Bypass encapsulation if the destination is local */
1386                 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
1387                 if (rt6i_flags & RTF_LOCAL &&
1388                     !(rt6i_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1389                         struct vxlan_dev *dst_vxlan;
1390
1391                         dst_release(ndst);
1392                         dst_vxlan = vxlan_find_vni(vxlan->net, vni,
1393                                                    dst->sa.sa_family, dst_port,
1394                                                    vxlan->flags);
1395                         if (!dst_vxlan)
1396                                 goto tx_error;
1397                         WARN_ONCE(1, "%s: vxlan_encap_bypass not supported\n",
1398                                   dev->name);
1399                         goto tx_error;
1400                 }
1401
1402                 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1403                 err = vxlan6_xmit_skb(ndst, sk, skb, dev, &fl6.saddr, &fl6.daddr,
1404                                       0, ttl, src_port, dst_port, htonl(vni << 8), md,
1405                                       !net_eq(vxlan->net, dev_net(vxlan->dev)),
1406                                       flags);
1407 #endif
1408         }
1409
1410         return;
1411
1412 drop:
1413         dev->stats.tx_dropped++;
1414         goto tx_free;
1415
1416 rt_tx_error:
1417         ip_rt_put(rt);
1418 tx_error:
1419         dev->stats.tx_errors++;
1420 tx_free:
1421         dev_kfree_skb(skb);
1422 }
1423
1424 /* Transmit local packets over Vxlan
1425  *
1426  * Outer IP header inherits ECN and DF from inner header.
1427  * Outer UDP destination is the VXLAN assigned port.
1428  *           source port is based on hash of flow
1429  */
1430 netdev_tx_t rpl_vxlan_xmit(struct sk_buff *skb)
1431 {
1432         struct net_device *dev = skb->dev;
1433         struct vxlan_dev *vxlan = netdev_priv(dev);
1434         const struct ip_tunnel_info *info;
1435
1436         info = skb_tunnel_info(skb);
1437
1438         skb_reset_mac_header(skb);
1439
1440         if ((vxlan->flags & VXLAN_F_PROXY))
1441                 goto out;
1442
1443         if (vxlan->flags & VXLAN_F_COLLECT_METADATA &&
1444             info && info->mode & IP_TUNNEL_INFO_TX) {
1445                 vxlan_xmit_one(skb, dev, NULL, false);
1446                 return NETDEV_TX_OK;
1447         }
1448 out:
1449         pr_warn("vxlan: unsupported flag set %x", vxlan->flags);
1450         kfree_skb(skb);
1451         return NETDEV_TX_OK;
1452 }
1453 EXPORT_SYMBOL(rpl_vxlan_xmit);
1454
1455 /* Walk the forwarding table and purge stale entries */
1456 static void vxlan_cleanup(unsigned long arg)
1457 {
1458         struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1459         unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1460         unsigned int h;
1461
1462         if (!netif_running(vxlan->dev))
1463                 return;
1464
1465         for (h = 0; h < FDB_HASH_SIZE; ++h) {
1466                 struct hlist_node *p, *n;
1467
1468                 spin_lock_bh(&vxlan->hash_lock);
1469                 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1470                         struct vxlan_fdb *f
1471                                 = container_of(p, struct vxlan_fdb, hlist);
1472                         unsigned long timeout;
1473
1474                         if (f->state & NUD_PERMANENT)
1475                                 continue;
1476
1477                         timeout = f->used + vxlan->cfg.age_interval * HZ;
1478                         if (time_before_eq(timeout, jiffies)) {
1479                                 netdev_dbg(vxlan->dev,
1480                                            "garbage collect %pM\n",
1481                                            f->eth_addr);
1482                                 f->state = NUD_STALE;
1483                                 vxlan_fdb_destroy(vxlan, f);
1484                         } else if (time_before(timeout, next_timer))
1485                                 next_timer = timeout;
1486                 }
1487                 spin_unlock_bh(&vxlan->hash_lock);
1488         }
1489
1490         mod_timer(&vxlan->age_timer, next_timer);
1491 }
1492
1493 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1494 {
1495         struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1496         __u32 vni = vxlan->default_dst.remote_vni;
1497
1498         vxlan->vn_sock = vs;
1499         spin_lock(&vn->sock_lock);
1500         hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1501         spin_unlock(&vn->sock_lock);
1502 }
1503
1504 /* Setup stats when device is created */
1505 #ifdef HAVE_DEV_TSTATS
1506 static int vxlan_init(struct net_device *dev)
1507 {
1508         dev->tstats = (typeof(dev->tstats)) netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1509         if (!dev->tstats)
1510                 return -ENOMEM;
1511
1512         return 0;
1513 }
1514 #endif
1515
1516 static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
1517 {
1518         struct vxlan_fdb *f;
1519
1520         spin_lock_bh(&vxlan->hash_lock);
1521         f = __vxlan_find_mac(vxlan, all_zeros_mac);
1522         if (f)
1523                 vxlan_fdb_destroy(vxlan, f);
1524         spin_unlock_bh(&vxlan->hash_lock);
1525 }
1526
1527 #ifdef HAVE_DEV_TSTATS
1528 static void vxlan_uninit(struct net_device *dev)
1529 {
1530         struct vxlan_dev *vxlan = netdev_priv(dev);
1531
1532         vxlan_fdb_delete_default(vxlan);
1533
1534         free_percpu(dev->tstats);
1535 }
1536 #endif
1537
1538 /* Start ageing timer and join group when device is brought up */
1539 static int vxlan_open(struct net_device *dev)
1540 {
1541         struct vxlan_dev *vxlan = netdev_priv(dev);
1542         struct vxlan_sock *vs;
1543         int ret = 0;
1544
1545         vs = vxlan_sock_add(vxlan->net, vxlan->cfg.dst_port,
1546                             vxlan->cfg.no_share, vxlan->flags);
1547         if (IS_ERR(vs))
1548                 return PTR_ERR(vs);
1549
1550         vxlan_vs_add_dev(vs, vxlan);
1551
1552         if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
1553                 ret = vxlan_igmp_join(vxlan);
1554                 if (ret == -EADDRINUSE)
1555                         ret = 0;
1556                 if (ret) {
1557                         vxlan_sock_release(vs);
1558                         return ret;
1559                 }
1560         }
1561
1562         if (vxlan->cfg.age_interval)
1563                 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1564
1565         return ret;
1566 }
1567
1568 /* Purge the forwarding table */
1569 static void vxlan_flush(struct vxlan_dev *vxlan)
1570 {
1571         unsigned int h;
1572
1573         spin_lock_bh(&vxlan->hash_lock);
1574         for (h = 0; h < FDB_HASH_SIZE; ++h) {
1575                 struct hlist_node *p, *n;
1576
1577                 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1578                         struct vxlan_fdb *f
1579                                 = container_of(p, struct vxlan_fdb, hlist);
1580                         /* the all_zeros_mac entry is deleted at vxlan_uninit */
1581                         if (!is_zero_ether_addr(f->eth_addr))
1582                                 vxlan_fdb_destroy(vxlan, f);
1583                 }
1584         }
1585         spin_unlock_bh(&vxlan->hash_lock);
1586 }
1587
1588 /* Cleanup timer and forwarding table on shutdown */
1589 static int vxlan_stop(struct net_device *dev)
1590 {
1591         struct vxlan_dev *vxlan = netdev_priv(dev);
1592         struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1593         struct vxlan_sock *vs = vxlan->vn_sock;
1594         int ret = 0;
1595
1596         if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1597             !vxlan_group_used(vn, vxlan))
1598                 ret = vxlan_igmp_leave(vxlan);
1599
1600         del_timer_sync(&vxlan->age_timer);
1601
1602         vxlan_flush(vxlan);
1603         vxlan_sock_release(vs);
1604
1605         return ret;
1606 }
1607
1608 /* Stub, nothing needs to be done. */
1609 static void vxlan_set_multicast_list(struct net_device *dev)
1610 {
1611 }
1612
1613 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
1614 {
1615         struct vxlan_dev *vxlan = netdev_priv(dev);
1616         struct vxlan_rdst *dst = &vxlan->default_dst;
1617         struct net_device *lowerdev;
1618         int max_mtu;
1619
1620         lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
1621         if (lowerdev == NULL)
1622                 return eth_change_mtu(dev, new_mtu);
1623
1624         if (dst->remote_ip.sa.sa_family == AF_INET6)
1625                 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
1626         else
1627                 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
1628
1629         if (new_mtu < 68 || new_mtu > max_mtu)
1630                 return -EINVAL;
1631
1632         dev->mtu = new_mtu;
1633         return 0;
1634 }
1635
1636 static netdev_tx_t vxlan_dev_xmit(struct sk_buff *skb, struct net_device *dev)
1637 {
1638         /* Drop All packets coming from networking stack. OVS-CB is
1639          * not initialized for these packets.
1640          */
1641
1642         dev_kfree_skb(skb);
1643         dev->stats.tx_dropped++;
1644         return NETDEV_TX_OK;
1645 }
1646
1647 static const struct net_device_ops vxlan_netdev_ops = {
1648 #ifdef HAVE_DEV_TSTATS
1649         .ndo_init               = vxlan_init,
1650         .ndo_uninit             = vxlan_uninit,
1651         .ndo_get_stats64        = ip_tunnel_get_stats64,
1652 #endif
1653         .ndo_open               = vxlan_open,
1654         .ndo_stop               = vxlan_stop,
1655         .ndo_start_xmit         = vxlan_dev_xmit,
1656         .ndo_set_rx_mode        = vxlan_set_multicast_list,
1657         .ndo_change_mtu         = vxlan_change_mtu,
1658         .ndo_validate_addr      = eth_validate_addr,
1659         .ndo_set_mac_address    = eth_mac_addr,
1660 };
1661
1662 /* Info for udev, that this is a virtual tunnel endpoint */
1663 static struct device_type vxlan_type = {
1664         .name = "vxlan",
1665 };
1666
1667 /* Initialize the device structure. */
1668 static void vxlan_setup(struct net_device *dev)
1669 {
1670         struct vxlan_dev *vxlan = netdev_priv(dev);
1671         unsigned int h;
1672
1673         eth_hw_addr_random(dev);
1674         ether_setup(dev);
1675
1676         dev->netdev_ops = &vxlan_netdev_ops;
1677         dev->destructor = free_netdev;
1678         SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1679
1680         dev->features   |= NETIF_F_LLTX;
1681         dev->features   |= NETIF_F_SG | NETIF_F_HW_CSUM;
1682         dev->features   |= NETIF_F_RXCSUM;
1683         dev->features   |= NETIF_F_GSO_SOFTWARE;
1684
1685         dev->vlan_features = dev->features;
1686         dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
1687 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
1688         dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1689         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1690         dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
1691 #endif
1692
1693 #if 0
1694         netif_keep_dst(dev);
1695 #endif
1696         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
1697
1698         INIT_LIST_HEAD(&vxlan->next);
1699         spin_lock_init(&vxlan->hash_lock);
1700
1701         init_timer_deferrable(&vxlan->age_timer);
1702         vxlan->age_timer.function = vxlan_cleanup;
1703         vxlan->age_timer.data = (unsigned long) vxlan;
1704
1705         vxlan->cfg.dst_port = htons(vxlan_port);
1706
1707         vxlan->dev = dev;
1708
1709         for (h = 0; h < FDB_HASH_SIZE; ++h)
1710                 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1711 }
1712
1713 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1714         [IFLA_VXLAN_PORT]       = { .type = NLA_U16 },
1715 };
1716
1717 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1718 {
1719         if (tb[IFLA_ADDRESS]) {
1720                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1721                         pr_debug("invalid link address (not ethernet)\n");
1722                         return -EINVAL;
1723                 }
1724
1725                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1726                         pr_debug("invalid all zero ethernet address\n");
1727                         return -EADDRNOTAVAIL;
1728                 }
1729         }
1730
1731         if (!data)
1732                 return -EINVAL;
1733
1734         if (data[IFLA_VXLAN_ID]) {
1735                 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1736                 if (id >= VXLAN_VID_MASK)
1737                         return -ERANGE;
1738         }
1739
1740         if (data[IFLA_VXLAN_PORT_RANGE]) {
1741                 const struct ifla_vxlan_port_range *p
1742                         = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1743
1744                 if (ntohs(p->high) < ntohs(p->low)) {
1745                         pr_debug("port range %u .. %u not valid\n",
1746                                  ntohs(p->low), ntohs(p->high));
1747                         return -EINVAL;
1748                 }
1749         }
1750
1751         return 0;
1752 }
1753
1754 static void vxlan_get_drvinfo(struct net_device *netdev,
1755                               struct ethtool_drvinfo *drvinfo)
1756 {
1757         strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1758         strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1759 }
1760
1761 static const struct ethtool_ops vxlan_ethtool_ops = {
1762         .get_drvinfo    = vxlan_get_drvinfo,
1763         .get_link       = ethtool_op_get_link,
1764 };
1765
1766 static void free_vs_rcu(struct rcu_head *rcu)
1767 {
1768         struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu);
1769
1770         kfree(vs);
1771 }
1772
1773 static void vxlan_del_work(struct work_struct *work)
1774 {
1775         struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1776         udp_tunnel_sock_release(vs->sock);
1777
1778         call_rcu(&vs->rcu, free_vs_rcu);
1779 }
1780
1781 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
1782                                         __be16 port, u32 flags)
1783 {
1784         struct socket *sock;
1785         struct udp_port_cfg udp_conf;
1786         int err;
1787
1788         memset(&udp_conf, 0, sizeof(udp_conf));
1789
1790         if (ipv6) {
1791                 udp_conf.family = AF_INET6;
1792                 udp_conf.use_udp6_rx_checksums =
1793                     !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
1794                 udp_conf.ipv6_v6only = 1;
1795         } else {
1796                 udp_conf.family = AF_INET;
1797         }
1798
1799         udp_conf.local_udp_port = port;
1800
1801         /* Open UDP socket */
1802         err = udp_sock_create(net, &udp_conf, &sock);
1803         if (err < 0)
1804                 return ERR_PTR(err);
1805
1806         return sock;
1807 }
1808
1809 /* Create new listen socket if needed */
1810 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
1811                                               u32 flags)
1812 {
1813         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1814         struct vxlan_sock *vs;
1815         struct socket *sock;
1816         unsigned int h;
1817         bool ipv6 = !!(flags & VXLAN_F_IPV6);
1818         struct udp_tunnel_sock_cfg tunnel_cfg;
1819
1820         vs = kzalloc(sizeof(*vs), GFP_KERNEL);
1821         if (!vs)
1822                 return ERR_PTR(-ENOMEM);
1823
1824         for (h = 0; h < VNI_HASH_SIZE; ++h)
1825                 INIT_HLIST_HEAD(&vs->vni_list[h]);
1826
1827         INIT_WORK(&vs->del_work, vxlan_del_work);
1828
1829         sock = vxlan_create_sock(net, ipv6, port, flags);
1830         if (IS_ERR(sock)) {
1831                 pr_info("Cannot bind port %d, err=%ld\n", ntohs(port),
1832                         PTR_ERR(sock));
1833                 kfree(vs);
1834                 return ERR_CAST(sock);
1835         }
1836
1837         vs->sock = sock;
1838         atomic_set(&vs->refcnt, 1);
1839         vs->flags = (flags & VXLAN_F_RCV_FLAGS);
1840
1841         /* Initialize the vxlan udp offloads structure */
1842 #ifdef HAVE_UDP_OFFLOAD
1843         vs->udp_offloads.port = port;
1844         vs->udp_offloads.callbacks.gro_receive  = vxlan_gro_receive;
1845         vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
1846         vxlan_notify_add_rx_port(vs);
1847 #endif
1848
1849         spin_lock(&vn->sock_lock);
1850         hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1851         spin_unlock(&vn->sock_lock);
1852
1853         /* Mark socket as an encapsulation socket. */
1854         tunnel_cfg.sk_user_data = vs;
1855         tunnel_cfg.encap_type = 1;
1856         tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
1857         tunnel_cfg.encap_destroy = NULL;
1858
1859         setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
1860
1861         return vs;
1862 }
1863
1864 static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
1865                                          bool no_share, u32 flags)
1866 {
1867         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1868         struct vxlan_sock *vs;
1869         bool ipv6 = flags & VXLAN_F_IPV6;
1870
1871         if (!no_share) {
1872                 spin_lock(&vn->sock_lock);
1873                 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port,
1874                                      flags);
1875                 if (vs) {
1876                         if (!atomic_add_unless(&vs->refcnt, 1, 0))
1877                                 vs = ERR_PTR(-EBUSY);
1878                         spin_unlock(&vn->sock_lock);
1879                         return vs;
1880                 }
1881                 spin_unlock(&vn->sock_lock);
1882         }
1883
1884         return vxlan_socket_create(net, port, flags);
1885 }
1886
1887 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
1888                                struct vxlan_config *conf)
1889 {
1890         struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
1891         struct vxlan_dev *vxlan = netdev_priv(dev);
1892         struct vxlan_rdst *dst = &vxlan->default_dst;
1893         int err;
1894         bool use_ipv6 = false;
1895         __be16 default_port = vxlan->cfg.dst_port;
1896
1897         vxlan->net = src_net;
1898
1899         dst->remote_vni = conf->vni;
1900
1901         memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
1902
1903         /* Unless IPv6 is explicitly requested, assume IPv4 */
1904         if (!dst->remote_ip.sa.sa_family)
1905                 dst->remote_ip.sa.sa_family = AF_INET;
1906
1907         if (dst->remote_ip.sa.sa_family == AF_INET6 ||
1908             vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
1909                 if (!IS_ENABLED(CONFIG_IPV6))
1910                         return -EPFNOSUPPORT;
1911                 use_ipv6 = true;
1912         }
1913
1914         if (conf->remote_ifindex) {
1915                 struct net_device *lowerdev
1916                          = __dev_get_by_index(src_net, conf->remote_ifindex);
1917
1918                 dst->remote_ifindex = conf->remote_ifindex;
1919
1920                 if (!lowerdev) {
1921                         pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
1922                         return -ENODEV;
1923                 }
1924
1925 #if IS_ENABLED(CONFIG_IPV6)
1926                 if (use_ipv6) {
1927                         struct inet6_dev *idev = __in6_dev_get(lowerdev);
1928                         if (idev && idev->cnf.disable_ipv6) {
1929                                 pr_info("IPv6 is disabled via sysctl\n");
1930                                 return -EPERM;
1931                         }
1932                         vxlan->flags |= VXLAN_F_IPV6;
1933                 }
1934 #endif
1935
1936                 if (!conf->mtu)
1937                         dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
1938
1939                 dev->needed_headroom = lowerdev->hard_header_len +
1940                                        (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
1941         } else if (use_ipv6) {
1942                 vxlan->flags |= VXLAN_F_IPV6;
1943                 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
1944         } else {
1945                 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
1946         }
1947
1948         memcpy(&vxlan->cfg, conf, sizeof(*conf));
1949         if (!vxlan->cfg.dst_port)
1950                 vxlan->cfg.dst_port = default_port;
1951         vxlan->flags |= conf->flags;
1952
1953         if (!vxlan->cfg.age_interval)
1954                 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
1955
1956         if (vxlan_find_vni(src_net, conf->vni, use_ipv6 ? AF_INET6 : AF_INET,
1957                            vxlan->cfg.dst_port, vxlan->flags))
1958                 return -EEXIST;
1959
1960         dev->ethtool_ops = &vxlan_ethtool_ops;
1961
1962         /* create an fdb entry for a valid default destination */
1963         if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
1964                 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1965                                        &vxlan->default_dst.remote_ip,
1966                                        NUD_REACHABLE|NUD_PERMANENT,
1967                                        NLM_F_EXCL|NLM_F_CREATE,
1968                                        vxlan->cfg.dst_port,
1969                                        vxlan->default_dst.remote_vni,
1970                                        vxlan->default_dst.remote_ifindex,
1971                                        NTF_SELF);
1972                 if (err)
1973                         return err;
1974         }
1975
1976         err = register_netdevice(dev);
1977         if (err) {
1978                 vxlan_fdb_delete_default(vxlan);
1979                 return err;
1980         }
1981
1982         list_add(&vxlan->next, &vn->vxlan_list);
1983
1984         return 0;
1985 }
1986
1987 struct net_device *rpl_vxlan_dev_create(struct net *net, const char *name,
1988                                     u8 name_assign_type, struct vxlan_config *conf)
1989 {
1990         struct nlattr *tb[IFLA_MAX+1];
1991         struct net_device *dev;
1992         int err;
1993
1994         memset(&tb, 0, sizeof(tb));
1995
1996         dev = rtnl_create_link(net, (char *)name, name_assign_type,
1997                                &vxlan_link_ops, tb);
1998         if (IS_ERR(dev))
1999                 return dev;
2000
2001         err = vxlan_dev_configure(net, dev, conf);
2002         if (err < 0) {
2003                 free_netdev(dev);
2004                 return ERR_PTR(err);
2005         }
2006
2007         return dev;
2008 }
2009 EXPORT_SYMBOL_GPL(rpl_vxlan_dev_create);
2010
2011 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
2012 static int vxlan_newlink(struct net *src_net, struct net_device *dev,
2013                          struct nlattr *tb[], struct nlattr *data[])
2014 #else
2015 static int vxlan_newlink(struct net_device *dev,
2016                          struct nlattr *tb[], struct nlattr *data[])
2017 #endif
2018 {
2019         return -EINVAL;
2020 }
2021
2022 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
2023 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2024 #else
2025 static void vxlan_dellink(struct net_device *dev)
2026 #endif
2027 {
2028         struct vxlan_dev *vxlan = netdev_priv(dev);
2029         struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2030
2031         spin_lock(&vn->sock_lock);
2032         if (!hlist_unhashed(&vxlan->hlist))
2033                 hlist_del_rcu(&vxlan->hlist);
2034         spin_unlock(&vn->sock_lock);
2035
2036         list_del(&vxlan->next);
2037         unregister_netdevice_queue(dev, head);
2038 }
2039
2040 static size_t vxlan_get_size(const struct net_device *dev)
2041 {
2042
2043         return nla_total_size(sizeof(__u32)) +  /* IFLA_VXLAN_ID */
2044                 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
2045                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
2046                 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
2047                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TTL */
2048                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_TOS */
2049                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_LEARNING */
2050                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_PROXY */
2051                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_RSC */
2052                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L2MISS */
2053                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_L3MISS */
2054                 nla_total_size(sizeof(__u8)) +  /* IFLA_VXLAN_COLLECT_METADATA */
2055                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2056                 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
2057                 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
2058                 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2059                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2060                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2061                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
2062                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2063                 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
2064                 0;
2065 }
2066
2067 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2068 {
2069         const struct vxlan_dev *vxlan = netdev_priv(dev);
2070
2071         if (nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port))
2072                 goto nla_put_failure;
2073
2074         return 0;
2075
2076 nla_put_failure:
2077         return -EMSGSIZE;
2078 }
2079
2080 #ifdef HAVE_GET_LINK_NET
2081 static struct net *vxlan_get_link_net(const struct net_device *dev)
2082 {
2083         struct vxlan_dev *vxlan = netdev_priv(dev);
2084
2085         return vxlan->net;
2086 }
2087 #endif
2088
2089 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2090         .kind           = "ovs_vxlan",
2091         .maxtype        = IFLA_VXLAN_MAX,
2092         .policy         = vxlan_policy,
2093         .priv_size      = sizeof(struct vxlan_dev),
2094         .setup          = vxlan_setup,
2095         .validate       = vxlan_validate,
2096         .newlink        = vxlan_newlink,
2097         .dellink        = vxlan_dellink,
2098         .get_size       = vxlan_get_size,
2099         .fill_info      = vxlan_fill_info,
2100 #ifdef HAVE_GET_LINK_NET
2101         .get_link_net   = vxlan_get_link_net,
2102 #endif
2103 };
2104
2105 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2106                                              struct net_device *dev)
2107 {
2108         struct vxlan_dev *vxlan, *next;
2109         LIST_HEAD(list_kill);
2110
2111         list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2112                 struct vxlan_rdst *dst = &vxlan->default_dst;
2113
2114                 /* In case we created vxlan device with carrier
2115                  * and we loose the carrier due to module unload
2116                  * we also need to remove vxlan device. In other
2117                  * cases, it's not necessary and remote_ifindex
2118                  * is 0 here, so no matches.
2119                  */
2120                 if (dst->remote_ifindex == dev->ifindex)
2121 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
2122                         vxlan_dellink(vxlan->dev, &list_kill);
2123 #else
2124                         vxlan_dellink(vxlan->dev);
2125 #endif
2126         }
2127
2128         unregister_netdevice_many(&list_kill);
2129 }
2130
2131 static int vxlan_lowerdev_event(struct notifier_block *unused,
2132                                 unsigned long event, void *ptr)
2133 {
2134         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2135         struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
2136
2137         if (event == NETDEV_UNREGISTER)
2138                 vxlan_handle_lowerdev_unregister(vn, dev);
2139
2140         return NOTIFY_DONE;
2141 }
2142
2143 static struct notifier_block vxlan_notifier_block __read_mostly = {
2144         .notifier_call = vxlan_lowerdev_event,
2145 };
2146
2147 static __net_init int vxlan_init_net(struct net *net)
2148 {
2149         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2150         unsigned int h;
2151
2152         INIT_LIST_HEAD(&vn->vxlan_list);
2153         spin_lock_init(&vn->sock_lock);
2154
2155         for (h = 0; h < PORT_HASH_SIZE; ++h)
2156                 INIT_HLIST_HEAD(&vn->sock_list[h]);
2157
2158         return 0;
2159 }
2160
2161 static void __net_exit vxlan_exit_net(struct net *net)
2162 {
2163         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2164         struct vxlan_dev *vxlan, *next;
2165         struct net_device *dev, *aux;
2166         LIST_HEAD(list);
2167
2168         rtnl_lock();
2169         for_each_netdev_safe(net, dev, aux)
2170                 if (dev->rtnl_link_ops == &vxlan_link_ops)
2171                         unregister_netdevice_queue(dev, &list);
2172
2173         list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2174                 /* If vxlan->dev is in the same netns, it has already been added
2175                  * to the list by the previous loop.
2176                  */
2177                 if (!net_eq(dev_net(vxlan->dev), net))
2178                         unregister_netdevice_queue(vxlan->dev, &list);
2179         }
2180
2181         unregister_netdevice_many(&list);
2182         rtnl_unlock();
2183 }
2184
2185 static struct pernet_operations vxlan_net_ops = {
2186         .init = vxlan_init_net,
2187         .exit = vxlan_exit_net,
2188         .id   = &vxlan_net_id,
2189         .size = sizeof(struct vxlan_net),
2190 };
2191
2192 DEFINE_COMPAT_PNET_REG_FUNC(device)
2193 int rpl_vxlan_init_module(void)
2194 {
2195         int rc;
2196
2197 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
2198         vxlan_wq = create_workqueue("vxlan");
2199 #else
2200         vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2201 #endif
2202         if (!vxlan_wq)
2203                 return -ENOMEM;
2204
2205         get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2206
2207         rc = register_pernet_subsys(&vxlan_net_ops);
2208         if (rc)
2209                 goto out1;
2210
2211         rc = register_netdevice_notifier(&vxlan_notifier_block);
2212         if (rc)
2213                 goto out2;
2214
2215         rc = rtnl_link_register(&vxlan_link_ops);
2216         if (rc)
2217                 goto out3;
2218
2219         pr_info("VxLAN tunneling driver\n");
2220         return 0;
2221 out3:
2222         unregister_netdevice_notifier(&vxlan_notifier_block);
2223 out2:
2224         unregister_pernet_subsys(&vxlan_net_ops);
2225 out1:
2226         destroy_workqueue(vxlan_wq);
2227         return rc;
2228 }
2229
2230 void rpl_vxlan_cleanup_module(void)
2231 {
2232         rtnl_link_unregister(&vxlan_link_ops);
2233         unregister_netdevice_notifier(&vxlan_notifier_block);
2234         destroy_workqueue(vxlan_wq);
2235         unregister_pernet_subsys(&vxlan_net_ops);
2236         /* rcu_barrier() is called by netns */
2237 }
2238 #endif