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