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