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