Merge tag 'v4.4.32' into linux-linaro-lsk-v4.4
[firefly-linux-kernel-4.4.55.git] / drivers / net / geneve.c
1 /*
2  * GENEVE: Generic Network Virtualization Encapsulation
3  *
4  * Copyright (c) 2015 Red Hat, 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/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/hash.h>
18 #include <net/dst_metadata.h>
19 #include <net/gro_cells.h>
20 #include <net/rtnetlink.h>
21 #include <net/geneve.h>
22 #include <net/protocol.h>
23
24 #define GENEVE_NETDEV_VER       "0.6"
25
26 #define GENEVE_UDP_PORT         6081
27
28 #define GENEVE_N_VID            (1u << 24)
29 #define GENEVE_VID_MASK         (GENEVE_N_VID - 1)
30
31 #define VNI_HASH_BITS           10
32 #define VNI_HASH_SIZE           (1<<VNI_HASH_BITS)
33
34 static bool log_ecn_error = true;
35 module_param(log_ecn_error, bool, 0644);
36 MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
37
38 #define GENEVE_VER 0
39 #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
40
41 /* per-network namespace private data for this module */
42 struct geneve_net {
43         struct list_head        geneve_list;
44         struct list_head        sock_list;
45 };
46
47 static int geneve_net_id;
48
49 union geneve_addr {
50         struct sockaddr_in sin;
51         struct sockaddr_in6 sin6;
52         struct sockaddr sa;
53 };
54
55 static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, };
56
57 /* Pseudo network device */
58 struct geneve_dev {
59         struct hlist_node  hlist;       /* vni hash table */
60         struct net         *net;        /* netns for packet i/o */
61         struct net_device  *dev;        /* netdev for geneve tunnel */
62         struct geneve_sock *sock4;      /* IPv4 socket used for geneve tunnel */
63 #if IS_ENABLED(CONFIG_IPV6)
64         struct geneve_sock *sock6;      /* IPv6 socket used for geneve tunnel */
65 #endif
66         u8                 vni[3];      /* virtual network ID for tunnel */
67         u8                 ttl;         /* TTL override */
68         u8                 tos;         /* TOS override */
69         union geneve_addr  remote;      /* IP address for link partner */
70         struct list_head   next;        /* geneve's per namespace list */
71         __be16             dst_port;
72         bool               collect_md;
73         struct gro_cells   gro_cells;
74 };
75
76 struct geneve_sock {
77         bool                    collect_md;
78         struct list_head        list;
79         struct socket           *sock;
80         struct rcu_head         rcu;
81         int                     refcnt;
82         struct udp_offload      udp_offloads;
83         struct hlist_head       vni_list[VNI_HASH_SIZE];
84 };
85
86 static inline __u32 geneve_net_vni_hash(u8 vni[3])
87 {
88         __u32 vnid;
89
90         vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
91         return hash_32(vnid, VNI_HASH_BITS);
92 }
93
94 static __be64 vni_to_tunnel_id(const __u8 *vni)
95 {
96 #ifdef __BIG_ENDIAN
97         return (vni[0] << 16) | (vni[1] << 8) | vni[2];
98 #else
99         return (__force __be64)(((__force u64)vni[0] << 40) |
100                                 ((__force u64)vni[1] << 48) |
101                                 ((__force u64)vni[2] << 56));
102 #endif
103 }
104
105 static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
106                                         __be32 addr, u8 vni[])
107 {
108         struct hlist_head *vni_list_head;
109         struct geneve_dev *geneve;
110         __u32 hash;
111
112         /* Find the device for this VNI */
113         hash = geneve_net_vni_hash(vni);
114         vni_list_head = &gs->vni_list[hash];
115         hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
116                 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
117                     addr == geneve->remote.sin.sin_addr.s_addr)
118                         return geneve;
119         }
120         return NULL;
121 }
122
123 #if IS_ENABLED(CONFIG_IPV6)
124 static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
125                                          struct in6_addr addr6, u8 vni[])
126 {
127         struct hlist_head *vni_list_head;
128         struct geneve_dev *geneve;
129         __u32 hash;
130
131         /* Find the device for this VNI */
132         hash = geneve_net_vni_hash(vni);
133         vni_list_head = &gs->vni_list[hash];
134         hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
135                 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
136                     ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
137                         return geneve;
138         }
139         return NULL;
140 }
141 #endif
142
143 static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
144 {
145         return (struct genevehdr *)(udp_hdr(skb) + 1);
146 }
147
148 /* geneve receive/decap routine */
149 static void geneve_rx(struct geneve_sock *gs, struct sk_buff *skb)
150 {
151         struct genevehdr *gnvh = geneve_hdr(skb);
152         struct metadata_dst *tun_dst = NULL;
153         struct geneve_dev *geneve = NULL;
154         struct pcpu_sw_netstats *stats;
155         struct iphdr *iph = NULL;
156         __be32 addr;
157         static u8 zero_vni[3];
158         u8 *vni;
159         int err = 0;
160         sa_family_t sa_family;
161 #if IS_ENABLED(CONFIG_IPV6)
162         struct ipv6hdr *ip6h = NULL;
163         struct in6_addr addr6;
164         static struct in6_addr zero_addr6;
165 #endif
166
167         sa_family = gs->sock->sk->sk_family;
168
169         if (sa_family == AF_INET) {
170                 iph = ip_hdr(skb); /* outer IP header... */
171
172                 if (gs->collect_md) {
173                         vni = zero_vni;
174                         addr = 0;
175                 } else {
176                         vni = gnvh->vni;
177
178                         addr = iph->saddr;
179                 }
180
181                 geneve = geneve_lookup(gs, addr, vni);
182 #if IS_ENABLED(CONFIG_IPV6)
183         } else if (sa_family == AF_INET6) {
184                 ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
185
186                 if (gs->collect_md) {
187                         vni = zero_vni;
188                         addr6 = zero_addr6;
189                 } else {
190                         vni = gnvh->vni;
191
192                         addr6 = ip6h->saddr;
193                 }
194
195                 geneve = geneve6_lookup(gs, addr6, vni);
196 #endif
197         }
198         if (!geneve)
199                 goto drop;
200
201         if (ip_tunnel_collect_metadata() || gs->collect_md) {
202                 __be16 flags;
203
204                 flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
205                         (gnvh->oam ? TUNNEL_OAM : 0) |
206                         (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
207
208                 tun_dst = udp_tun_rx_dst(skb, sa_family, flags,
209                                          vni_to_tunnel_id(gnvh->vni),
210                                          gnvh->opt_len * 4);
211                 if (!tun_dst)
212                         goto drop;
213                 /* Update tunnel dst according to Geneve options. */
214                 ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
215                                         gnvh->options, gnvh->opt_len * 4);
216         } else {
217                 /* Drop packets w/ critical options,
218                  * since we don't support any...
219                  */
220                 if (gnvh->critical)
221                         goto drop;
222         }
223
224         skb_reset_mac_header(skb);
225         skb_scrub_packet(skb, !net_eq(geneve->net, dev_net(geneve->dev)));
226         skb->protocol = eth_type_trans(skb, geneve->dev);
227         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
228
229         if (tun_dst)
230                 skb_dst_set(skb, &tun_dst->dst);
231
232         /* Ignore packet loops (and multicast echo) */
233         if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr))
234                 goto drop;
235
236         skb_reset_network_header(skb);
237
238         if (iph)
239                 err = IP_ECN_decapsulate(iph, skb);
240 #if IS_ENABLED(CONFIG_IPV6)
241         if (ip6h)
242                 err = IP6_ECN_decapsulate(ip6h, skb);
243 #endif
244
245         if (unlikely(err)) {
246                 if (log_ecn_error) {
247                         if (iph)
248                                 net_info_ratelimited("non-ECT from %pI4 "
249                                                      "with TOS=%#x\n",
250                                                      &iph->saddr, iph->tos);
251 #if IS_ENABLED(CONFIG_IPV6)
252                         if (ip6h)
253                                 net_info_ratelimited("non-ECT from %pI6\n",
254                                                      &ip6h->saddr);
255 #endif
256                 }
257                 if (err > 1) {
258                         ++geneve->dev->stats.rx_frame_errors;
259                         ++geneve->dev->stats.rx_errors;
260                         goto drop;
261                 }
262         }
263
264         stats = this_cpu_ptr(geneve->dev->tstats);
265         u64_stats_update_begin(&stats->syncp);
266         stats->rx_packets++;
267         stats->rx_bytes += skb->len;
268         u64_stats_update_end(&stats->syncp);
269
270         gro_cells_receive(&geneve->gro_cells, skb);
271         return;
272 drop:
273         /* Consume bad packet */
274         kfree_skb(skb);
275 }
276
277 /* Setup stats when device is created */
278 static int geneve_init(struct net_device *dev)
279 {
280         struct geneve_dev *geneve = netdev_priv(dev);
281         int err;
282
283         dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
284         if (!dev->tstats)
285                 return -ENOMEM;
286
287         err = gro_cells_init(&geneve->gro_cells, dev);
288         if (err) {
289                 free_percpu(dev->tstats);
290                 return err;
291         }
292
293         return 0;
294 }
295
296 static void geneve_uninit(struct net_device *dev)
297 {
298         struct geneve_dev *geneve = netdev_priv(dev);
299
300         gro_cells_destroy(&geneve->gro_cells);
301         free_percpu(dev->tstats);
302 }
303
304 /* Callback from net/ipv4/udp.c to receive packets */
305 static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
306 {
307         struct genevehdr *geneveh;
308         struct geneve_sock *gs;
309         int opts_len;
310
311         /* Need Geneve and inner Ethernet header to be present */
312         if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
313                 goto drop;
314
315         /* Return packets with reserved bits set */
316         geneveh = geneve_hdr(skb);
317         if (unlikely(geneveh->ver != GENEVE_VER))
318                 goto drop;
319
320         if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
321                 goto drop;
322
323         opts_len = geneveh->opt_len * 4;
324         if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
325                                  htons(ETH_P_TEB)))
326                 goto drop;
327
328         gs = rcu_dereference_sk_user_data(sk);
329         if (!gs)
330                 goto drop;
331
332         geneve_rx(gs, skb);
333         return 0;
334
335 drop:
336         /* Consume bad packet */
337         kfree_skb(skb);
338         return 0;
339 }
340
341 static struct socket *geneve_create_sock(struct net *net, bool ipv6,
342                                          __be16 port)
343 {
344         struct socket *sock;
345         struct udp_port_cfg udp_conf;
346         int err;
347
348         memset(&udp_conf, 0, sizeof(udp_conf));
349
350         if (ipv6) {
351                 udp_conf.family = AF_INET6;
352                 udp_conf.ipv6_v6only = 1;
353         } else {
354                 udp_conf.family = AF_INET;
355                 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
356         }
357
358         udp_conf.local_udp_port = port;
359
360         /* Open UDP socket */
361         err = udp_sock_create(net, &udp_conf, &sock);
362         if (err < 0)
363                 return ERR_PTR(err);
364
365         return sock;
366 }
367
368 static void geneve_notify_add_rx_port(struct geneve_sock *gs)
369 {
370         struct sock *sk = gs->sock->sk;
371         sa_family_t sa_family = sk->sk_family;
372         int err;
373
374         if (sa_family == AF_INET) {
375                 err = udp_add_offload(&gs->udp_offloads);
376                 if (err)
377                         pr_warn("geneve: udp_add_offload failed with status %d\n",
378                                 err);
379         }
380 }
381
382 static int geneve_hlen(struct genevehdr *gh)
383 {
384         return sizeof(*gh) + gh->opt_len * 4;
385 }
386
387 static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
388                                            struct sk_buff *skb,
389                                            struct udp_offload *uoff)
390 {
391         struct sk_buff *p, **pp = NULL;
392         struct genevehdr *gh, *gh2;
393         unsigned int hlen, gh_len, off_gnv;
394         const struct packet_offload *ptype;
395         __be16 type;
396         int flush = 1;
397
398         off_gnv = skb_gro_offset(skb);
399         hlen = off_gnv + sizeof(*gh);
400         gh = skb_gro_header_fast(skb, off_gnv);
401         if (skb_gro_header_hard(skb, hlen)) {
402                 gh = skb_gro_header_slow(skb, hlen, off_gnv);
403                 if (unlikely(!gh))
404                         goto out;
405         }
406
407         if (gh->ver != GENEVE_VER || gh->oam)
408                 goto out;
409         gh_len = geneve_hlen(gh);
410
411         hlen = off_gnv + gh_len;
412         if (skb_gro_header_hard(skb, hlen)) {
413                 gh = skb_gro_header_slow(skb, hlen, off_gnv);
414                 if (unlikely(!gh))
415                         goto out;
416         }
417
418         flush = 0;
419
420         for (p = *head; p; p = p->next) {
421                 if (!NAPI_GRO_CB(p)->same_flow)
422                         continue;
423
424                 gh2 = (struct genevehdr *)(p->data + off_gnv);
425                 if (gh->opt_len != gh2->opt_len ||
426                     memcmp(gh, gh2, gh_len)) {
427                         NAPI_GRO_CB(p)->same_flow = 0;
428                         continue;
429                 }
430         }
431
432         type = gh->proto_type;
433
434         rcu_read_lock();
435         ptype = gro_find_receive_by_type(type);
436         if (!ptype) {
437                 flush = 1;
438                 goto out_unlock;
439         }
440
441         skb_gro_pull(skb, gh_len);
442         skb_gro_postpull_rcsum(skb, gh, gh_len);
443         pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
444
445 out_unlock:
446         rcu_read_unlock();
447 out:
448         NAPI_GRO_CB(skb)->flush |= flush;
449
450         return pp;
451 }
452
453 static int geneve_gro_complete(struct sk_buff *skb, int nhoff,
454                                struct udp_offload *uoff)
455 {
456         struct genevehdr *gh;
457         struct packet_offload *ptype;
458         __be16 type;
459         int gh_len;
460         int err = -ENOSYS;
461
462         udp_tunnel_gro_complete(skb, nhoff);
463
464         gh = (struct genevehdr *)(skb->data + nhoff);
465         gh_len = geneve_hlen(gh);
466         type = gh->proto_type;
467
468         rcu_read_lock();
469         ptype = gro_find_complete_by_type(type);
470         if (ptype)
471                 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
472
473         rcu_read_unlock();
474         return err;
475 }
476
477 /* Create new listen socket if needed */
478 static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
479                                                 bool ipv6)
480 {
481         struct geneve_net *gn = net_generic(net, geneve_net_id);
482         struct geneve_sock *gs;
483         struct socket *sock;
484         struct udp_tunnel_sock_cfg tunnel_cfg;
485         int h;
486
487         gs = kzalloc(sizeof(*gs), GFP_KERNEL);
488         if (!gs)
489                 return ERR_PTR(-ENOMEM);
490
491         sock = geneve_create_sock(net, ipv6, port);
492         if (IS_ERR(sock)) {
493                 kfree(gs);
494                 return ERR_CAST(sock);
495         }
496
497         gs->sock = sock;
498         gs->refcnt = 1;
499         for (h = 0; h < VNI_HASH_SIZE; ++h)
500                 INIT_HLIST_HEAD(&gs->vni_list[h]);
501
502         /* Initialize the geneve udp offloads structure */
503         gs->udp_offloads.port = port;
504         gs->udp_offloads.callbacks.gro_receive  = geneve_gro_receive;
505         gs->udp_offloads.callbacks.gro_complete = geneve_gro_complete;
506         geneve_notify_add_rx_port(gs);
507
508         /* Mark socket as an encapsulation socket */
509         tunnel_cfg.sk_user_data = gs;
510         tunnel_cfg.encap_type = 1;
511         tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
512         tunnel_cfg.encap_destroy = NULL;
513         setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
514         list_add(&gs->list, &gn->sock_list);
515         return gs;
516 }
517
518 static void geneve_notify_del_rx_port(struct geneve_sock *gs)
519 {
520         struct sock *sk = gs->sock->sk;
521         sa_family_t sa_family = sk->sk_family;
522
523         if (sa_family == AF_INET)
524                 udp_del_offload(&gs->udp_offloads);
525 }
526
527 static void __geneve_sock_release(struct geneve_sock *gs)
528 {
529         if (!gs || --gs->refcnt)
530                 return;
531
532         list_del(&gs->list);
533         geneve_notify_del_rx_port(gs);
534         udp_tunnel_sock_release(gs->sock);
535         kfree_rcu(gs, rcu);
536 }
537
538 static void geneve_sock_release(struct geneve_dev *geneve)
539 {
540         __geneve_sock_release(geneve->sock4);
541 #if IS_ENABLED(CONFIG_IPV6)
542         __geneve_sock_release(geneve->sock6);
543 #endif
544 }
545
546 static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
547                                             sa_family_t family,
548                                             __be16 dst_port)
549 {
550         struct geneve_sock *gs;
551
552         list_for_each_entry(gs, &gn->sock_list, list) {
553                 if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
554                     inet_sk(gs->sock->sk)->sk.sk_family == family) {
555                         return gs;
556                 }
557         }
558         return NULL;
559 }
560
561 static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
562 {
563         struct net *net = geneve->net;
564         struct geneve_net *gn = net_generic(net, geneve_net_id);
565         struct geneve_sock *gs;
566         __u32 hash;
567
568         gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
569         if (gs) {
570                 gs->refcnt++;
571                 goto out;
572         }
573
574         gs = geneve_socket_create(net, geneve->dst_port, ipv6);
575         if (IS_ERR(gs))
576                 return PTR_ERR(gs);
577
578 out:
579         gs->collect_md = geneve->collect_md;
580 #if IS_ENABLED(CONFIG_IPV6)
581         if (ipv6)
582                 geneve->sock6 = gs;
583         else
584 #endif
585                 geneve->sock4 = gs;
586
587         hash = geneve_net_vni_hash(geneve->vni);
588         hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
589         return 0;
590 }
591
592 static int geneve_open(struct net_device *dev)
593 {
594         struct geneve_dev *geneve = netdev_priv(dev);
595         bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
596         bool metadata = geneve->collect_md;
597         int ret = 0;
598
599         geneve->sock4 = NULL;
600 #if IS_ENABLED(CONFIG_IPV6)
601         geneve->sock6 = NULL;
602         if (ipv6 || metadata)
603                 ret = geneve_sock_add(geneve, true);
604 #endif
605         if (!ret && (!ipv6 || metadata))
606                 ret = geneve_sock_add(geneve, false);
607         if (ret < 0)
608                 geneve_sock_release(geneve);
609
610         return ret;
611 }
612
613 static int geneve_stop(struct net_device *dev)
614 {
615         struct geneve_dev *geneve = netdev_priv(dev);
616
617         if (!hlist_unhashed(&geneve->hlist))
618                 hlist_del_rcu(&geneve->hlist);
619         geneve_sock_release(geneve);
620         return 0;
621 }
622
623 static void geneve_build_header(struct genevehdr *geneveh,
624                                 __be16 tun_flags, u8 vni[3],
625                                 u8 options_len, u8 *options)
626 {
627         geneveh->ver = GENEVE_VER;
628         geneveh->opt_len = options_len / 4;
629         geneveh->oam = !!(tun_flags & TUNNEL_OAM);
630         geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
631         geneveh->rsvd1 = 0;
632         memcpy(geneveh->vni, vni, 3);
633         geneveh->proto_type = htons(ETH_P_TEB);
634         geneveh->rsvd2 = 0;
635
636         memcpy(geneveh->options, options, options_len);
637 }
638
639 static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
640                             __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
641                             bool csum, bool xnet)
642 {
643         struct genevehdr *gnvh;
644         int min_headroom;
645         int err;
646
647         skb_scrub_packet(skb, xnet);
648
649         min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
650                         + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
651         err = skb_cow_head(skb, min_headroom);
652         if (unlikely(err)) {
653                 kfree_skb(skb);
654                 goto free_rt;
655         }
656
657         skb = udp_tunnel_handle_offloads(skb, csum);
658         if (IS_ERR(skb)) {
659                 err = PTR_ERR(skb);
660                 goto free_rt;
661         }
662
663         gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
664         geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
665
666         skb_set_inner_protocol(skb, htons(ETH_P_TEB));
667         return 0;
668
669 free_rt:
670         ip_rt_put(rt);
671         return err;
672 }
673
674 #if IS_ENABLED(CONFIG_IPV6)
675 static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
676                              __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
677                              bool csum, bool xnet)
678 {
679         struct genevehdr *gnvh;
680         int min_headroom;
681         int err;
682
683         skb_scrub_packet(skb, xnet);
684
685         min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
686                         + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
687         err = skb_cow_head(skb, min_headroom);
688         if (unlikely(err)) {
689                 kfree_skb(skb);
690                 goto free_dst;
691         }
692
693         skb = udp_tunnel_handle_offloads(skb, csum);
694         if (IS_ERR(skb)) {
695                 err = PTR_ERR(skb);
696                 goto free_dst;
697         }
698
699         gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
700         geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
701
702         skb_set_inner_protocol(skb, htons(ETH_P_TEB));
703         return 0;
704
705 free_dst:
706         dst_release(dst);
707         return err;
708 }
709 #endif
710
711 static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
712                                        struct net_device *dev,
713                                        struct flowi4 *fl4,
714                                        struct ip_tunnel_info *info)
715 {
716         struct geneve_dev *geneve = netdev_priv(dev);
717         struct rtable *rt = NULL;
718         __u8 tos;
719
720         memset(fl4, 0, sizeof(*fl4));
721         fl4->flowi4_mark = skb->mark;
722         fl4->flowi4_proto = IPPROTO_UDP;
723
724         if (info) {
725                 fl4->daddr = info->key.u.ipv4.dst;
726                 fl4->saddr = info->key.u.ipv4.src;
727                 fl4->flowi4_tos = RT_TOS(info->key.tos);
728         } else {
729                 tos = geneve->tos;
730                 if (tos == 1) {
731                         const struct iphdr *iip = ip_hdr(skb);
732
733                         tos = ip_tunnel_get_dsfield(iip, skb);
734                 }
735
736                 fl4->flowi4_tos = RT_TOS(tos);
737                 fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
738         }
739
740         rt = ip_route_output_key(geneve->net, fl4);
741         if (IS_ERR(rt)) {
742                 netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
743                 return ERR_PTR(-ENETUNREACH);
744         }
745         if (rt->dst.dev == dev) { /* is this necessary? */
746                 netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
747                 ip_rt_put(rt);
748                 return ERR_PTR(-ELOOP);
749         }
750         return rt;
751 }
752
753 #if IS_ENABLED(CONFIG_IPV6)
754 static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
755                                            struct net_device *dev,
756                                            struct flowi6 *fl6,
757                                            struct ip_tunnel_info *info)
758 {
759         struct geneve_dev *geneve = netdev_priv(dev);
760         struct geneve_sock *gs6 = geneve->sock6;
761         struct dst_entry *dst = NULL;
762         __u8 prio;
763
764         memset(fl6, 0, sizeof(*fl6));
765         fl6->flowi6_mark = skb->mark;
766         fl6->flowi6_proto = IPPROTO_UDP;
767
768         if (info) {
769                 fl6->daddr = info->key.u.ipv6.dst;
770                 fl6->saddr = info->key.u.ipv6.src;
771                 fl6->flowi6_tos = RT_TOS(info->key.tos);
772         } else {
773                 prio = geneve->tos;
774                 if (prio == 1) {
775                         const struct iphdr *iip = ip_hdr(skb);
776
777                         prio = ip_tunnel_get_dsfield(iip, skb);
778                 }
779
780                 fl6->flowi6_tos = RT_TOS(prio);
781                 fl6->daddr = geneve->remote.sin6.sin6_addr;
782         }
783
784         if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
785                 netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
786                 return ERR_PTR(-ENETUNREACH);
787         }
788         if (dst->dev == dev) { /* is this necessary? */
789                 netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
790                 dst_release(dst);
791                 return ERR_PTR(-ELOOP);
792         }
793
794         return dst;
795 }
796 #endif
797
798 /* Convert 64 bit tunnel ID to 24 bit VNI. */
799 static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
800 {
801 #ifdef __BIG_ENDIAN
802         vni[0] = (__force __u8)(tun_id >> 16);
803         vni[1] = (__force __u8)(tun_id >> 8);
804         vni[2] = (__force __u8)tun_id;
805 #else
806         vni[0] = (__force __u8)((__force u64)tun_id >> 40);
807         vni[1] = (__force __u8)((__force u64)tun_id >> 48);
808         vni[2] = (__force __u8)((__force u64)tun_id >> 56);
809 #endif
810 }
811
812 static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
813                                    struct ip_tunnel_info *info)
814 {
815         struct geneve_dev *geneve = netdev_priv(dev);
816         struct geneve_sock *gs4 = geneve->sock4;
817         struct rtable *rt = NULL;
818         const struct iphdr *iip; /* interior IP header */
819         int err = -EINVAL;
820         struct flowi4 fl4;
821         __u8 tos, ttl;
822         __be16 sport;
823         bool udp_csum;
824         __be16 df;
825         bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
826
827         if (geneve->collect_md) {
828                 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
829                         netdev_dbg(dev, "no tunnel metadata\n");
830                         goto tx_error;
831                 }
832                 if (info && ip_tunnel_info_af(info) != AF_INET)
833                         goto tx_error;
834         }
835
836         rt = geneve_get_v4_rt(skb, dev, &fl4, info);
837         if (IS_ERR(rt)) {
838                 err = PTR_ERR(rt);
839                 goto tx_error;
840         }
841
842         sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
843         skb_reset_mac_header(skb);
844
845         iip = ip_hdr(skb);
846
847         if (info) {
848                 const struct ip_tunnel_key *key = &info->key;
849                 u8 *opts = NULL;
850                 u8 vni[3];
851
852                 tunnel_id_to_vni(key->tun_id, vni);
853                 if (key->tun_flags & TUNNEL_GENEVE_OPT)
854                         opts = ip_tunnel_info_opts(info);
855
856                 udp_csum = !!(key->tun_flags & TUNNEL_CSUM);
857                 err = geneve_build_skb(rt, skb, key->tun_flags, vni,
858                                        info->options_len, opts, udp_csum, xnet);
859                 if (unlikely(err))
860                         goto err;
861
862                 tos = ip_tunnel_ecn_encap(key->tos, iip, skb);
863                 ttl = key->ttl;
864                 df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
865         } else {
866                 udp_csum = false;
867                 err = geneve_build_skb(rt, skb, 0, geneve->vni,
868                                        0, NULL, udp_csum, xnet);
869                 if (unlikely(err))
870                         goto err;
871
872                 tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, iip, skb);
873                 ttl = geneve->ttl;
874                 if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
875                         ttl = 1;
876                 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
877                 df = 0;
878         }
879         err = udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
880                                   tos, ttl, df, sport, geneve->dst_port,
881                                   !net_eq(geneve->net, dev_net(geneve->dev)),
882                                   !udp_csum);
883
884         iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
885         return NETDEV_TX_OK;
886
887 tx_error:
888         dev_kfree_skb(skb);
889 err:
890         if (err == -ELOOP)
891                 dev->stats.collisions++;
892         else if (err == -ENETUNREACH)
893                 dev->stats.tx_carrier_errors++;
894         else
895                 dev->stats.tx_errors++;
896         return NETDEV_TX_OK;
897 }
898
899 #if IS_ENABLED(CONFIG_IPV6)
900 static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
901                                     struct ip_tunnel_info *info)
902 {
903         struct geneve_dev *geneve = netdev_priv(dev);
904         struct geneve_sock *gs6 = geneve->sock6;
905         struct dst_entry *dst = NULL;
906         const struct iphdr *iip; /* interior IP header */
907         int err = -EINVAL;
908         struct flowi6 fl6;
909         __u8 prio, ttl;
910         __be16 sport;
911         bool udp_csum;
912         bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
913
914         if (geneve->collect_md) {
915                 if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
916                         netdev_dbg(dev, "no tunnel metadata\n");
917                         goto tx_error;
918                 }
919         }
920
921         dst = geneve_get_v6_dst(skb, dev, &fl6, info);
922         if (IS_ERR(dst)) {
923                 err = PTR_ERR(dst);
924                 goto tx_error;
925         }
926
927         sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
928         skb_reset_mac_header(skb);
929
930         iip = ip_hdr(skb);
931
932         if (info) {
933                 const struct ip_tunnel_key *key = &info->key;
934                 u8 *opts = NULL;
935                 u8 vni[3];
936
937                 tunnel_id_to_vni(key->tun_id, vni);
938                 if (key->tun_flags & TUNNEL_GENEVE_OPT)
939                         opts = ip_tunnel_info_opts(info);
940
941                 udp_csum = !!(key->tun_flags & TUNNEL_CSUM);
942                 err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
943                                         info->options_len, opts,
944                                         udp_csum, xnet);
945                 if (unlikely(err))
946                         goto err;
947
948                 prio = ip_tunnel_ecn_encap(key->tos, iip, skb);
949                 ttl = key->ttl;
950         } else {
951                 udp_csum = false;
952                 err = geneve6_build_skb(dst, skb, 0, geneve->vni,
953                                         0, NULL, udp_csum, xnet);
954                 if (unlikely(err))
955                         goto err;
956
957                 prio = ip_tunnel_ecn_encap(fl6.flowi6_tos, iip, skb);
958                 ttl = geneve->ttl;
959                 if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
960                         ttl = 1;
961                 ttl = ttl ? : ip6_dst_hoplimit(dst);
962         }
963         err = udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
964                                    &fl6.saddr, &fl6.daddr, prio, ttl,
965                                    sport, geneve->dst_port, !udp_csum);
966         return NETDEV_TX_OK;
967
968 tx_error:
969         dev_kfree_skb(skb);
970 err:
971         if (err == -ELOOP)
972                 dev->stats.collisions++;
973         else if (err == -ENETUNREACH)
974                 dev->stats.tx_carrier_errors++;
975         else
976                 dev->stats.tx_errors++;
977         return NETDEV_TX_OK;
978 }
979 #endif
980
981 static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
982 {
983         struct geneve_dev *geneve = netdev_priv(dev);
984         struct ip_tunnel_info *info = NULL;
985
986         if (geneve->collect_md)
987                 info = skb_tunnel_info(skb);
988
989 #if IS_ENABLED(CONFIG_IPV6)
990         if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
991             (!info && geneve->remote.sa.sa_family == AF_INET6))
992                 return geneve6_xmit_skb(skb, dev, info);
993 #endif
994         return geneve_xmit_skb(skb, dev, info);
995 }
996
997 static int geneve_change_mtu(struct net_device *dev, int new_mtu)
998 {
999         /* GENEVE overhead is not fixed, so we can't enforce a more
1000          * precise max MTU.
1001          */
1002         if (new_mtu < 68 || new_mtu > IP_MAX_MTU)
1003                 return -EINVAL;
1004         dev->mtu = new_mtu;
1005         return 0;
1006 }
1007
1008 static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
1009 {
1010         struct ip_tunnel_info *info = skb_tunnel_info(skb);
1011         struct geneve_dev *geneve = netdev_priv(dev);
1012         struct rtable *rt;
1013         struct flowi4 fl4;
1014 #if IS_ENABLED(CONFIG_IPV6)
1015         struct dst_entry *dst;
1016         struct flowi6 fl6;
1017 #endif
1018
1019         if (ip_tunnel_info_af(info) == AF_INET) {
1020                 rt = geneve_get_v4_rt(skb, dev, &fl4, info);
1021                 if (IS_ERR(rt))
1022                         return PTR_ERR(rt);
1023
1024                 ip_rt_put(rt);
1025                 info->key.u.ipv4.src = fl4.saddr;
1026 #if IS_ENABLED(CONFIG_IPV6)
1027         } else if (ip_tunnel_info_af(info) == AF_INET6) {
1028                 dst = geneve_get_v6_dst(skb, dev, &fl6, info);
1029                 if (IS_ERR(dst))
1030                         return PTR_ERR(dst);
1031
1032                 dst_release(dst);
1033                 info->key.u.ipv6.src = fl6.saddr;
1034 #endif
1035         } else {
1036                 return -EINVAL;
1037         }
1038
1039         info->key.tp_src = udp_flow_src_port(geneve->net, skb,
1040                                              1, USHRT_MAX, true);
1041         info->key.tp_dst = geneve->dst_port;
1042         return 0;
1043 }
1044
1045 static const struct net_device_ops geneve_netdev_ops = {
1046         .ndo_init               = geneve_init,
1047         .ndo_uninit             = geneve_uninit,
1048         .ndo_open               = geneve_open,
1049         .ndo_stop               = geneve_stop,
1050         .ndo_start_xmit         = geneve_xmit,
1051         .ndo_get_stats64        = ip_tunnel_get_stats64,
1052         .ndo_change_mtu         = geneve_change_mtu,
1053         .ndo_validate_addr      = eth_validate_addr,
1054         .ndo_set_mac_address    = eth_mac_addr,
1055         .ndo_fill_metadata_dst  = geneve_fill_metadata_dst,
1056 };
1057
1058 static void geneve_get_drvinfo(struct net_device *dev,
1059                                struct ethtool_drvinfo *drvinfo)
1060 {
1061         strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
1062         strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
1063 }
1064
1065 static const struct ethtool_ops geneve_ethtool_ops = {
1066         .get_drvinfo    = geneve_get_drvinfo,
1067         .get_link       = ethtool_op_get_link,
1068 };
1069
1070 /* Info for udev, that this is a virtual tunnel endpoint */
1071 static struct device_type geneve_type = {
1072         .name = "geneve",
1073 };
1074
1075 /* Initialize the device structure. */
1076 static void geneve_setup(struct net_device *dev)
1077 {
1078         ether_setup(dev);
1079
1080         dev->netdev_ops = &geneve_netdev_ops;
1081         dev->ethtool_ops = &geneve_ethtool_ops;
1082         dev->destructor = free_netdev;
1083
1084         SET_NETDEV_DEVTYPE(dev, &geneve_type);
1085
1086         dev->features    |= NETIF_F_LLTX;
1087         dev->features    |= NETIF_F_SG | NETIF_F_HW_CSUM;
1088         dev->features    |= NETIF_F_RXCSUM;
1089         dev->features    |= NETIF_F_GSO_SOFTWARE;
1090
1091         dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1092         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1093
1094         netif_keep_dst(dev);
1095         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
1096         eth_hw_addr_random(dev);
1097 }
1098
1099 static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
1100         [IFLA_GENEVE_ID]                = { .type = NLA_U32 },
1101         [IFLA_GENEVE_REMOTE]            = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
1102         [IFLA_GENEVE_REMOTE6]           = { .len = sizeof(struct in6_addr) },
1103         [IFLA_GENEVE_TTL]               = { .type = NLA_U8 },
1104         [IFLA_GENEVE_TOS]               = { .type = NLA_U8 },
1105         [IFLA_GENEVE_PORT]              = { .type = NLA_U16 },
1106         [IFLA_GENEVE_COLLECT_METADATA]  = { .type = NLA_FLAG },
1107 };
1108
1109 static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
1110 {
1111         if (tb[IFLA_ADDRESS]) {
1112                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1113                         return -EINVAL;
1114
1115                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1116                         return -EADDRNOTAVAIL;
1117         }
1118
1119         if (!data)
1120                 return -EINVAL;
1121
1122         if (data[IFLA_GENEVE_ID]) {
1123                 __u32 vni =  nla_get_u32(data[IFLA_GENEVE_ID]);
1124
1125                 if (vni >= GENEVE_VID_MASK)
1126                         return -ERANGE;
1127         }
1128
1129         return 0;
1130 }
1131
1132 static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
1133                                           __be16 dst_port,
1134                                           union geneve_addr *remote,
1135                                           u8 vni[],
1136                                           bool *tun_on_same_port,
1137                                           bool *tun_collect_md)
1138 {
1139         struct geneve_dev *geneve, *t;
1140
1141         *tun_on_same_port = false;
1142         *tun_collect_md = false;
1143         t = NULL;
1144         list_for_each_entry(geneve, &gn->geneve_list, next) {
1145                 if (geneve->dst_port == dst_port) {
1146                         *tun_collect_md = geneve->collect_md;
1147                         *tun_on_same_port = true;
1148                 }
1149                 if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
1150                     !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
1151                     dst_port == geneve->dst_port)
1152                         t = geneve;
1153         }
1154         return t;
1155 }
1156
1157 static int geneve_configure(struct net *net, struct net_device *dev,
1158                             union geneve_addr *remote,
1159                             __u32 vni, __u8 ttl, __u8 tos, __be16 dst_port,
1160                             bool metadata)
1161 {
1162         struct geneve_net *gn = net_generic(net, geneve_net_id);
1163         struct geneve_dev *t, *geneve = netdev_priv(dev);
1164         bool tun_collect_md, tun_on_same_port;
1165         int err, encap_len;
1166
1167         if (!remote)
1168                 return -EINVAL;
1169         if (metadata &&
1170             (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl))
1171                 return -EINVAL;
1172
1173         geneve->net = net;
1174         geneve->dev = dev;
1175
1176         geneve->vni[0] = (vni & 0x00ff0000) >> 16;
1177         geneve->vni[1] = (vni & 0x0000ff00) >> 8;
1178         geneve->vni[2] =  vni & 0x000000ff;
1179
1180         if ((remote->sa.sa_family == AF_INET &&
1181              IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
1182             (remote->sa.sa_family == AF_INET6 &&
1183              ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
1184                 return -EINVAL;
1185         geneve->remote = *remote;
1186
1187         geneve->ttl = ttl;
1188         geneve->tos = tos;
1189         geneve->dst_port = dst_port;
1190         geneve->collect_md = metadata;
1191
1192         t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
1193                             &tun_on_same_port, &tun_collect_md);
1194         if (t)
1195                 return -EBUSY;
1196
1197         /* make enough headroom for basic scenario */
1198         encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
1199         if (remote->sa.sa_family == AF_INET)
1200                 encap_len += sizeof(struct iphdr);
1201         else
1202                 encap_len += sizeof(struct ipv6hdr);
1203         dev->needed_headroom = encap_len + ETH_HLEN;
1204
1205         if (metadata) {
1206                 if (tun_on_same_port)
1207                         return -EPERM;
1208         } else {
1209                 if (tun_collect_md)
1210                         return -EPERM;
1211         }
1212
1213         err = register_netdevice(dev);
1214         if (err)
1215                 return err;
1216
1217         list_add(&geneve->next, &gn->geneve_list);
1218         return 0;
1219 }
1220
1221 static int geneve_newlink(struct net *net, struct net_device *dev,
1222                           struct nlattr *tb[], struct nlattr *data[])
1223 {
1224         __be16 dst_port = htons(GENEVE_UDP_PORT);
1225         __u8 ttl = 0, tos = 0;
1226         bool metadata = false;
1227         union geneve_addr remote = geneve_remote_unspec;
1228         __u32 vni = 0;
1229
1230         if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
1231                 return -EINVAL;
1232
1233         if (data[IFLA_GENEVE_REMOTE]) {
1234                 remote.sa.sa_family = AF_INET;
1235                 remote.sin.sin_addr.s_addr =
1236                         nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
1237         }
1238
1239         if (data[IFLA_GENEVE_REMOTE6]) {
1240                 if (!IS_ENABLED(CONFIG_IPV6))
1241                         return -EPFNOSUPPORT;
1242
1243                 remote.sa.sa_family = AF_INET6;
1244                 remote.sin6.sin6_addr =
1245                         nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
1246
1247                 if (ipv6_addr_type(&remote.sin6.sin6_addr) &
1248                     IPV6_ADDR_LINKLOCAL) {
1249                         netdev_dbg(dev, "link-local remote is unsupported\n");
1250                         return -EINVAL;
1251                 }
1252         }
1253
1254         if (data[IFLA_GENEVE_ID])
1255                 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
1256
1257         if (data[IFLA_GENEVE_TTL])
1258                 ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
1259
1260         if (data[IFLA_GENEVE_TOS])
1261                 tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
1262
1263         if (data[IFLA_GENEVE_PORT])
1264                 dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
1265
1266         if (data[IFLA_GENEVE_COLLECT_METADATA])
1267                 metadata = true;
1268
1269         return geneve_configure(net, dev, &remote, vni, ttl, tos, dst_port,
1270                                 metadata);
1271 }
1272
1273 static void geneve_dellink(struct net_device *dev, struct list_head *head)
1274 {
1275         struct geneve_dev *geneve = netdev_priv(dev);
1276
1277         list_del(&geneve->next);
1278         unregister_netdevice_queue(dev, head);
1279 }
1280
1281 static size_t geneve_get_size(const struct net_device *dev)
1282 {
1283         return nla_total_size(sizeof(__u32)) +  /* IFLA_GENEVE_ID */
1284                 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
1285                 nla_total_size(sizeof(__u8)) +  /* IFLA_GENEVE_TTL */
1286                 nla_total_size(sizeof(__u8)) +  /* IFLA_GENEVE_TOS */
1287                 nla_total_size(sizeof(__be16)) +  /* IFLA_GENEVE_PORT */
1288                 nla_total_size(0) +      /* IFLA_GENEVE_COLLECT_METADATA */
1289                 0;
1290 }
1291
1292 static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
1293 {
1294         struct geneve_dev *geneve = netdev_priv(dev);
1295         __u32 vni;
1296
1297         vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
1298         if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
1299                 goto nla_put_failure;
1300
1301         if (geneve->remote.sa.sa_family == AF_INET) {
1302                 if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
1303                                     geneve->remote.sin.sin_addr.s_addr))
1304                         goto nla_put_failure;
1305 #if IS_ENABLED(CONFIG_IPV6)
1306         } else {
1307                 if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
1308                                      &geneve->remote.sin6.sin6_addr))
1309                         goto nla_put_failure;
1310 #endif
1311         }
1312
1313         if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
1314             nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos))
1315                 goto nla_put_failure;
1316
1317         if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
1318                 goto nla_put_failure;
1319
1320         if (geneve->collect_md) {
1321                 if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
1322                         goto nla_put_failure;
1323         }
1324
1325         return 0;
1326
1327 nla_put_failure:
1328         return -EMSGSIZE;
1329 }
1330
1331 static struct rtnl_link_ops geneve_link_ops __read_mostly = {
1332         .kind           = "geneve",
1333         .maxtype        = IFLA_GENEVE_MAX,
1334         .policy         = geneve_policy,
1335         .priv_size      = sizeof(struct geneve_dev),
1336         .setup          = geneve_setup,
1337         .validate       = geneve_validate,
1338         .newlink        = geneve_newlink,
1339         .dellink        = geneve_dellink,
1340         .get_size       = geneve_get_size,
1341         .fill_info      = geneve_fill_info,
1342 };
1343
1344 struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
1345                                         u8 name_assign_type, u16 dst_port)
1346 {
1347         struct nlattr *tb[IFLA_MAX + 1];
1348         struct net_device *dev;
1349         int err;
1350
1351         memset(tb, 0, sizeof(tb));
1352         dev = rtnl_create_link(net, name, name_assign_type,
1353                                &geneve_link_ops, tb);
1354         if (IS_ERR(dev))
1355                 return dev;
1356
1357         err = geneve_configure(net, dev, &geneve_remote_unspec,
1358                                0, 0, 0, htons(dst_port), true);
1359         if (err)
1360                 goto err;
1361
1362         /* openvswitch users expect packet sizes to be unrestricted,
1363          * so set the largest MTU we can.
1364          */
1365         err = geneve_change_mtu(dev, IP_MAX_MTU);
1366         if (err)
1367                 goto err;
1368
1369         return dev;
1370
1371  err:
1372         free_netdev(dev);
1373         return ERR_PTR(err);
1374 }
1375 EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
1376
1377 static __net_init int geneve_init_net(struct net *net)
1378 {
1379         struct geneve_net *gn = net_generic(net, geneve_net_id);
1380
1381         INIT_LIST_HEAD(&gn->geneve_list);
1382         INIT_LIST_HEAD(&gn->sock_list);
1383         return 0;
1384 }
1385
1386 static void __net_exit geneve_exit_net(struct net *net)
1387 {
1388         struct geneve_net *gn = net_generic(net, geneve_net_id);
1389         struct geneve_dev *geneve, *next;
1390         struct net_device *dev, *aux;
1391         LIST_HEAD(list);
1392
1393         rtnl_lock();
1394
1395         /* gather any geneve devices that were moved into this ns */
1396         for_each_netdev_safe(net, dev, aux)
1397                 if (dev->rtnl_link_ops == &geneve_link_ops)
1398                         unregister_netdevice_queue(dev, &list);
1399
1400         /* now gather any other geneve devices that were created in this ns */
1401         list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
1402                 /* If geneve->dev is in the same netns, it was already added
1403                  * to the list by the previous loop.
1404                  */
1405                 if (!net_eq(dev_net(geneve->dev), net))
1406                         unregister_netdevice_queue(geneve->dev, &list);
1407         }
1408
1409         /* unregister the devices gathered above */
1410         unregister_netdevice_many(&list);
1411         rtnl_unlock();
1412 }
1413
1414 static struct pernet_operations geneve_net_ops = {
1415         .init = geneve_init_net,
1416         .exit = geneve_exit_net,
1417         .id   = &geneve_net_id,
1418         .size = sizeof(struct geneve_net),
1419 };
1420
1421 static int __init geneve_init_module(void)
1422 {
1423         int rc;
1424
1425         rc = register_pernet_subsys(&geneve_net_ops);
1426         if (rc)
1427                 goto out1;
1428
1429         rc = rtnl_link_register(&geneve_link_ops);
1430         if (rc)
1431                 goto out2;
1432
1433         return 0;
1434 out2:
1435         unregister_pernet_subsys(&geneve_net_ops);
1436 out1:
1437         return rc;
1438 }
1439 late_initcall(geneve_init_module);
1440
1441 static void __exit geneve_cleanup_module(void)
1442 {
1443         rtnl_link_unregister(&geneve_link_ops);
1444         unregister_pernet_subsys(&geneve_net_ops);
1445 }
1446 module_exit(geneve_cleanup_module);
1447
1448 MODULE_LICENSE("GPL");
1449 MODULE_VERSION(GENEVE_NETDEV_VER);
1450 MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
1451 MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
1452 MODULE_ALIAS_RTNL_LINK("geneve");