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