datapath: lisp: Relax MTU constraints.
[cascardo/ovs.git] / datapath / linux / compat / lisp.c
1 /*
2  * Copyright (c) 2015 Nicira, Inc.
3  * Copyright (c) 2013 Cisco Systems, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  */
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18 #include <linux/version.h>
19
20 #include <linux/in.h>
21 #include <linux/ip.h>
22 #include <linux/net.h>
23 #include <linux/module.h>
24 #include <linux/rculist.h>
25 #include <linux/udp.h>
26
27 #include <net/icmp.h>
28 #include <net/ip.h>
29 #include <net/lisp.h>
30 #include <net/net_namespace.h>
31 #include <net/netns/generic.h>
32 #include <net/route.h>
33 #include <net/udp.h>
34 #include <net/udp_tunnel.h>
35 #include <net/xfrm.h>
36
37 #include "datapath.h"
38 #include "gso.h"
39 #include "vport.h"
40 #include "gso.h"
41 #include "vport-netdev.h"
42
43 #define LISP_UDP_PORT           4341
44 #define LISP_NETDEV_VER         "0.1"
45 static int lisp_net_id;
46
47 /* Pseudo network device */
48 struct lisp_dev {
49         struct net         *net;        /* netns for packet i/o */
50         struct net_device  *dev;        /* netdev for lisp tunnel */
51         struct socket           *sock;
52         __be16             dst_port;
53         struct list_head   next;
54 };
55
56 /* per-network namespace private data for this module */
57 struct lisp_net {
58         struct list_head lisp_list;
59 };
60
61 /*
62  *  LISP encapsulation header:
63  *
64  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65  *  |N|L|E|V|I|flags|            Nonce/Map-Version                  |
66  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67  *  |                 Instance ID/Locator Status Bits               |
68  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69  *
70  */
71
72 /**
73  * struct lisphdr - LISP header
74  * @nonce_present: Flag indicating the presence of a 24 bit nonce value.
75  * @locator_status_bits_present: Flag indicating the presence of Locator Status
76  *                               Bits (LSB).
77  * @solicit_echo_nonce: Flag indicating the use of the echo noncing mechanism.
78  * @map_version_present: Flag indicating the use of mapping versioning.
79  * @instance_id_present: Flag indicating the presence of a 24 bit Instance ID.
80  * @reserved_flags: 3 bits reserved for future flags.
81  * @nonce: 24 bit nonce value.
82  * @map_version: 24 bit mapping version.
83  * @locator_status_bits: Locator Status Bits: 32 bits when instance_id_present
84  *                       is not set, 8 bits when it is.
85  * @instance_id: 24 bit Instance ID
86  */
87 struct lisphdr {
88 #ifdef __LITTLE_ENDIAN_BITFIELD
89         __u8 reserved_flags:3;
90         __u8 instance_id_present:1;
91         __u8 map_version_present:1;
92         __u8 solicit_echo_nonce:1;
93         __u8 locator_status_bits_present:1;
94         __u8 nonce_present:1;
95 #else
96         __u8 nonce_present:1;
97         __u8 locator_status_bits_present:1;
98         __u8 solicit_echo_nonce:1;
99         __u8 map_version_present:1;
100         __u8 instance_id_present:1;
101         __u8 reserved_flags:3;
102 #endif
103         union {
104                 __u8 nonce[3];
105                 __u8 map_version[3];
106         } u1;
107         union {
108                 __be32 locator_status_bits;
109                 struct {
110                         __u8 instance_id[3];
111                         __u8 locator_status_bits;
112                 } word2;
113         } u2;
114 };
115
116 #define LISP_HLEN (sizeof(struct udphdr) + sizeof(struct lisphdr))
117 #define LISP_MAX_MTU (IP_MAX_MTU - LISP_HLEN - sizeof(struct iphdr))
118
119 static inline struct lisphdr *lisp_hdr(const struct sk_buff *skb)
120 {
121         return (struct lisphdr *)(udp_hdr(skb) + 1);
122 }
123
124 /* Convert 64 bit tunnel ID to 24 bit Instance ID. */
125 static void tunnel_id_to_instance_id(__be64 tun_id, __u8 *iid)
126 {
127
128 #ifdef __BIG_ENDIAN
129         iid[0] = (__force __u8)(tun_id >> 16);
130         iid[1] = (__force __u8)(tun_id >> 8);
131         iid[2] = (__force __u8)tun_id;
132 #else
133         iid[0] = (__force __u8)((__force u64)tun_id >> 40);
134         iid[1] = (__force __u8)((__force u64)tun_id >> 48);
135         iid[2] = (__force __u8)((__force u64)tun_id >> 56);
136 #endif
137 }
138
139 /* Convert 24 bit Instance ID to 64 bit tunnel ID. */
140 static __be64 instance_id_to_tunnel_id(__u8 *iid)
141 {
142 #ifdef __BIG_ENDIAN
143         return (iid[0] << 16) | (iid[1] << 8) | iid[2];
144 #else
145         return (__force __be64)(((__force u64)iid[0] << 40) |
146                         ((__force u64)iid[1] << 48) |
147                         ((__force u64)iid[2] << 56));
148 #endif
149 }
150
151 /* Compute source UDP port for outgoing packet.
152  * Currently we use the flow hash.
153  */
154 static u16 get_src_port(struct net *net, struct sk_buff *skb)
155 {
156         u32 hash = skb_get_hash(skb);
157         unsigned int range;
158         int high;
159         int low;
160
161         if (!hash) {
162                 if (skb->protocol == htons(ETH_P_IP)) {
163                         struct iphdr *iph;
164                         int size = (sizeof(iph->saddr) * 2) / sizeof(u32);
165
166                         iph = (struct iphdr *) skb_network_header(skb);
167                         hash = jhash2((const u32 *)&iph->saddr, size, 0);
168                 } else if (skb->protocol == htons(ETH_P_IPV6)) {
169                         struct ipv6hdr *ipv6hdr;
170
171                         ipv6hdr = (struct ipv6hdr *) skb_network_header(skb);
172                         hash = jhash2((const u32 *)&ipv6hdr->saddr,
173                                         (sizeof(struct in6_addr) * 2) / sizeof(u32), 0);
174                 } else {
175                         pr_warn_once("LISP inner protocol is not IP when "
176                                         "calculating hash.\n");
177                 }
178         }
179
180         inet_get_local_port_range(net, &low, &high);
181         range = (high - low) + 1;
182         return (((u64) hash * range) >> 32) + low;
183 }
184
185 static void lisp_build_header(struct sk_buff *skb,
186                               const struct ip_tunnel_key *tun_key)
187 {
188         struct lisphdr *lisph;
189
190         lisph = (struct lisphdr *)__skb_push(skb, sizeof(struct lisphdr));
191         lisph->nonce_present = 0;       /* We don't support echo nonce algorithm */
192         lisph->locator_status_bits_present = 1; /* Set LSB */
193         lisph->solicit_echo_nonce = 0;  /* No echo noncing */
194         lisph->map_version_present = 0; /* No mapping versioning, nonce instead */
195         lisph->instance_id_present = 1; /* Store the tun_id as Instance ID  */
196         lisph->reserved_flags = 0;      /* Reserved flags, set to 0  */
197
198         lisph->u1.nonce[0] = 0;
199         lisph->u1.nonce[1] = 0;
200         lisph->u1.nonce[2] = 0;
201
202         tunnel_id_to_instance_id(tun_key->tun_id, &lisph->u2.word2.instance_id[0]);
203         lisph->u2.word2.locator_status_bits = 1;
204 }
205
206 /* Called with rcu_read_lock and BH disabled. */
207 static int lisp_rcv(struct sock *sk, struct sk_buff *skb)
208 {
209         struct net_device *dev;
210         struct lisphdr *lisph;
211         struct iphdr *inner_iph;
212         struct metadata_dst *tun_dst;
213 #ifndef HAVE_METADATA_DST
214         struct metadata_dst temp;
215 #endif
216         __be64 key;
217         struct ethhdr *ethh;
218         __be16 protocol;
219
220         dev = rcu_dereference_sk_user_data(sk);
221         if (unlikely(!dev))
222                 goto error;
223
224         if (iptunnel_pull_header(skb, LISP_HLEN, 0))
225                 goto error;
226
227         lisph = lisp_hdr(skb);
228
229         if (lisph->instance_id_present != 1)
230                 key = 0;
231         else
232                 key = instance_id_to_tunnel_id(&lisph->u2.word2.instance_id[0]);
233
234         /* Save outer tunnel values */
235 #ifndef HAVE_METADATA_DST
236         tun_dst = &temp;
237         ovs_udp_tun_rx_dst(&tun_dst->u.tun_info, skb, AF_INET, TUNNEL_KEY, key, 0);
238 #else
239         tun_dst = udp_tun_rx_dst(skb, AF_INET, TUNNEL_KEY, key, 0);
240 #endif
241         /* Drop non-IP inner packets */
242         inner_iph = (struct iphdr *)(lisph + 1);
243         switch (inner_iph->version) {
244         case 4:
245                 protocol = htons(ETH_P_IP);
246                 break;
247         case 6:
248                 protocol = htons(ETH_P_IPV6);
249                 break;
250         default:
251                 goto error;
252         }
253         skb->protocol = protocol;
254
255         /* Add Ethernet header */
256         ethh = (struct ethhdr *)skb_push(skb, ETH_HLEN);
257         memset(ethh, 0, ETH_HLEN);
258         ethh->h_dest[0] = 0x02;
259         ethh->h_source[0] = 0x02;
260         ethh->h_proto = protocol;
261
262         ovs_ip_tunnel_rcv(dev, skb, tun_dst);
263         goto out;
264
265 error:
266         kfree_skb(skb);
267 out:
268         return 0;
269 }
270
271 netdev_tx_t rpl_lisp_xmit(struct sk_buff *skb)
272 {
273         struct net_device *dev = skb->dev;
274         struct lisp_dev *lisp_dev = netdev_priv(dev);
275         struct net *net = lisp_dev->net;
276         int network_offset = skb_network_offset(skb);
277         struct ip_tunnel_info *info;
278         struct ip_tunnel_key *tun_key;
279         struct rtable *rt;
280         int min_headroom;
281         __be16 src_port, dst_port;
282         struct flowi4 fl;
283         __be16 df;
284         int err;
285
286         info = skb_tunnel_info(skb);
287         if (unlikely(!info)) {
288                 err = -EINVAL;
289                 goto error;
290         }
291
292         if (skb->protocol != htons(ETH_P_IP) &&
293                         skb->protocol != htons(ETH_P_IPV6)) {
294                 err = 0;
295                 goto error;
296         }
297
298         tun_key = &info->key;
299
300         /* Route lookup */
301         memset(&fl, 0, sizeof(fl));
302         fl.daddr = tun_key->u.ipv4.dst;
303         fl.saddr = tun_key->u.ipv4.src;
304         fl.flowi4_tos = RT_TOS(tun_key->tos);
305         fl.flowi4_mark = skb->mark;
306         fl.flowi4_proto = IPPROTO_UDP;
307         rt = ip_route_output_key(net, &fl);
308         if (IS_ERR(rt)) {
309                 err = PTR_ERR(rt);
310                 goto error;
311         }
312
313         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
314                 + sizeof(struct iphdr) + LISP_HLEN;
315
316         if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
317                 int head_delta = SKB_DATA_ALIGN(min_headroom -
318                                 skb_headroom(skb) +
319                                 16);
320
321                 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
322                                 0, GFP_ATOMIC);
323                 if (unlikely(err))
324                         goto err_free_rt;
325         }
326
327         /* Reset l2 headers. */
328         skb_pull(skb, network_offset);
329         skb_reset_mac_header(skb);
330         vlan_set_tci(skb, 0);
331
332         skb = udp_tunnel_handle_offloads(skb, false, 0, false);
333         if (IS_ERR(skb)) {
334                 err = PTR_ERR(skb);
335                 skb = NULL;
336                 goto err_free_rt;
337         }
338
339         src_port = htons(get_src_port(net, skb));
340         dst_port = lisp_dev->dst_port;
341
342         lisp_build_header(skb, tun_key);
343
344         skb->ignore_df = 1;
345
346         ovs_skb_set_inner_protocol(skb, skb->protocol);
347
348         df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
349         err = udp_tunnel_xmit_skb(rt, lisp_dev->sock->sk, skb,
350                         fl.saddr, tun_key->u.ipv4.dst,
351                         tun_key->tos, tun_key->ttl,
352                         df, src_port, dst_port, false, true);
353
354         iptunnel_xmit_stats(err, &dev->stats, (struct pcpu_sw_netstats __percpu *)dev->tstats);
355         return NETDEV_TX_OK;
356
357 err_free_rt:
358         ip_rt_put(rt);
359 error:
360         kfree_skb(skb);
361         return NETDEV_TX_OK;
362 }
363 EXPORT_SYMBOL(rpl_lisp_xmit);
364
365 #ifdef HAVE_DEV_TSTATS
366 /* Setup stats when device is created */
367 static int lisp_init(struct net_device *dev)
368 {
369         dev->tstats = (typeof(dev->tstats)) netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
370         if (!dev->tstats)
371                 return -ENOMEM;
372
373         return 0;
374 }
375
376 static void lisp_uninit(struct net_device *dev)
377 {
378         free_percpu(dev->tstats);
379 }
380 #endif
381
382 static struct socket *create_sock(struct net *net, bool ipv6,
383                                        __be16 port)
384 {
385         struct socket *sock;
386         struct udp_port_cfg udp_conf;
387         int err;
388
389         memset(&udp_conf, 0, sizeof(udp_conf));
390
391         if (ipv6) {
392                 udp_conf.family = AF_INET6;
393         } else {
394                 udp_conf.family = AF_INET;
395                 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
396         }
397
398         udp_conf.local_udp_port = port;
399
400         /* Open UDP socket */
401         err = udp_sock_create(net, &udp_conf, &sock);
402         if (err < 0)
403                 return ERR_PTR(err);
404
405         return sock;
406 }
407
408 static int lisp_open(struct net_device *dev)
409 {
410         struct lisp_dev *lisp = netdev_priv(dev);
411         struct udp_tunnel_sock_cfg tunnel_cfg;
412         struct net *net = lisp->net;
413
414         lisp->sock = create_sock(net, false, lisp->dst_port);
415         if (IS_ERR(lisp->sock))
416                 return PTR_ERR(lisp->sock);
417
418         /* Mark socket as an encapsulation socket */
419         tunnel_cfg.sk_user_data = dev;
420         tunnel_cfg.encap_type = 1;
421         tunnel_cfg.encap_rcv = lisp_rcv;
422         tunnel_cfg.encap_destroy = NULL;
423         setup_udp_tunnel_sock(net, lisp->sock, &tunnel_cfg);
424         return 0;
425 }
426
427 static int lisp_stop(struct net_device *dev)
428 {
429         struct lisp_dev *lisp = netdev_priv(dev);
430
431         udp_tunnel_sock_release(lisp->sock);
432         lisp->sock = NULL;
433         return 0;
434 }
435
436 static netdev_tx_t lisp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
437 {
438 #ifdef HAVE_METADATA_DST
439         return rpl_lisp_xmit(skb);
440 #else
441         /* Drop All packets coming from networking stack. OVS-CB is
442          * not initialized for these packets.
443          */
444
445         dev_kfree_skb(skb);
446         dev->stats.tx_dropped++;
447         return NETDEV_TX_OK;
448 #endif
449 }
450
451 static int lisp_change_mtu(struct net_device *dev, int new_mtu)
452 {
453         if (new_mtu < 68 || new_mtu > LISP_MAX_MTU)
454                 return -EINVAL;
455
456         dev->mtu = new_mtu;
457         return 0;
458 }
459
460 static const struct net_device_ops lisp_netdev_ops = {
461 #ifdef HAVE_DEV_TSTATS
462         .ndo_init               = lisp_init,
463         .ndo_uninit             = lisp_uninit,
464         .ndo_get_stats64        = ip_tunnel_get_stats64,
465 #endif
466         .ndo_open               = lisp_open,
467         .ndo_stop               = lisp_stop,
468         .ndo_start_xmit         = lisp_dev_xmit,
469         .ndo_change_mtu         = lisp_change_mtu,
470         .ndo_validate_addr      = eth_validate_addr,
471         .ndo_set_mac_address    = eth_mac_addr,
472 };
473
474 static void lisp_get_drvinfo(struct net_device *dev,
475                 struct ethtool_drvinfo *drvinfo)
476 {
477         strlcpy(drvinfo->version, LISP_NETDEV_VER, sizeof(drvinfo->version));
478         strlcpy(drvinfo->driver, "lisp", sizeof(drvinfo->driver));
479 }
480
481 static const struct ethtool_ops lisp_ethtool_ops = {
482         .get_drvinfo    = lisp_get_drvinfo,
483         .get_link       = ethtool_op_get_link,
484 };
485
486 /* Info for udev, that this is a virtual tunnel endpoint */
487 static struct device_type lisp_type = {
488         .name = "lisp",
489 };
490
491 /* Initialize the device structure. */
492 static void lisp_setup(struct net_device *dev)
493 {
494         ether_setup(dev);
495
496         dev->netdev_ops = &lisp_netdev_ops;
497         dev->ethtool_ops = &lisp_ethtool_ops;
498         dev->destructor = free_netdev;
499
500         SET_NETDEV_DEVTYPE(dev, &lisp_type);
501
502         dev->features    |= NETIF_F_LLTX | NETIF_F_NETNS_LOCAL;
503         dev->features    |= NETIF_F_SG | NETIF_F_HW_CSUM;
504         dev->features    |= NETIF_F_RXCSUM;
505         dev->features    |= NETIF_F_GSO_SOFTWARE;
506
507 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
508         dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
509         dev->hw_features |= NETIF_F_GSO_SOFTWARE;
510 #endif
511 #ifdef HAVE_METADATA_DST
512         netif_keep_dst(dev);
513 #endif
514         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
515         eth_hw_addr_random(dev);
516 }
517
518 static const struct nla_policy lisp_policy[IFLA_LISP_MAX + 1] = {
519         [IFLA_LISP_PORT]              = { .type = NLA_U16 },
520 };
521
522 static int lisp_validate(struct nlattr *tb[], struct nlattr *data[])
523 {
524         if (tb[IFLA_ADDRESS]) {
525                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
526                         return -EINVAL;
527
528                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
529                         return -EADDRNOTAVAIL;
530         }
531
532         return 0;
533 }
534
535 static struct lisp_dev *find_dev(struct net *net, __be16 dst_port)
536 {
537         struct lisp_net *ln = net_generic(net, lisp_net_id);
538         struct lisp_dev *dev;
539
540         list_for_each_entry(dev, &ln->lisp_list, next) {
541                 if (dev->dst_port == dst_port)
542                         return dev;
543         }
544         return NULL;
545 }
546
547 static int lisp_configure(struct net *net, struct net_device *dev,
548                           __be16 dst_port)
549 {
550         struct lisp_net *ln = net_generic(net, lisp_net_id);
551         struct lisp_dev *lisp = netdev_priv(dev);
552         int err;
553
554         lisp->net = net;
555         lisp->dev = dev;
556
557         lisp->dst_port = dst_port;
558
559         if (find_dev(net, dst_port))
560                 return -EBUSY;
561
562         err = lisp_change_mtu(dev, LISP_MAX_MTU);
563         if (err)
564                 return err;
565
566         err = register_netdevice(dev);
567         if (err)
568                 return err;
569
570         list_add(&lisp->next, &ln->lisp_list);
571         return 0;
572 }
573
574 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
575 static int lisp_newlink(struct net *net, struct net_device *dev,
576                 struct nlattr *tb[], struct nlattr *data[])
577 {
578 #else
579 static int lisp_newlink(struct net_device *dev,
580                 struct nlattr *tb[], struct nlattr *data[])
581
582 {
583         struct net *net = &init_net;
584 #endif
585         __be16 dst_port = htons(LISP_UDP_PORT);
586
587         if (data[IFLA_LISP_PORT])
588                 dst_port = nla_get_be16(data[IFLA_LISP_PORT]);
589
590         return lisp_configure(net, dev, dst_port);
591 }
592
593 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
594 static void lisp_dellink(struct net_device *dev, struct list_head *head)
595 #else
596 static void lisp_dellink(struct net_device *dev)
597 #endif
598 {
599         struct lisp_dev *lisp = netdev_priv(dev);
600
601         list_del(&lisp->next);
602         unregister_netdevice_queue(dev, head);
603 }
604
605 static size_t lisp_get_size(const struct net_device *dev)
606 {
607         return nla_total_size(sizeof(__be32));  /* IFLA_LISP_PORT */
608 }
609
610 static int lisp_fill_info(struct sk_buff *skb, const struct net_device *dev)
611 {
612         struct lisp_dev *lisp = netdev_priv(dev);
613
614         if (nla_put_be16(skb, IFLA_LISP_PORT, lisp->dst_port))
615                 goto nla_put_failure;
616
617         return 0;
618
619 nla_put_failure:
620         return -EMSGSIZE;
621 }
622
623 static struct rtnl_link_ops lisp_link_ops __read_mostly = {
624         .kind           = "lisp",
625         .maxtype        = IFLA_LISP_MAX,
626         .policy         = lisp_policy,
627         .priv_size      = sizeof(struct lisp_dev),
628         .setup          = lisp_setup,
629         .validate       = lisp_validate,
630         .newlink        = lisp_newlink,
631         .dellink        = lisp_dellink,
632         .get_size       = lisp_get_size,
633         .fill_info      = lisp_fill_info,
634 };
635
636 struct net_device *rpl_lisp_dev_create_fb(struct net *net, const char *name,
637                                       u8 name_assign_type, u16 dst_port)
638 {
639         struct nlattr *tb[IFLA_MAX + 1];
640         struct net_device *dev;
641         int err;
642
643         memset(tb, 0, sizeof(tb));
644         dev = rtnl_create_link(net, (char *) name, name_assign_type,
645                         &lisp_link_ops, tb);
646         if (IS_ERR(dev))
647                 return dev;
648
649         err = lisp_configure(net, dev, htons(dst_port));
650         if (err) {
651                 free_netdev(dev);
652                 return ERR_PTR(err);
653         }
654         return dev;
655 }
656 EXPORT_SYMBOL_GPL(rpl_lisp_dev_create_fb);
657
658 static int lisp_init_net(struct net *net)
659 {
660         struct lisp_net *ln = net_generic(net, lisp_net_id);
661
662         INIT_LIST_HEAD(&ln->lisp_list);
663         return 0;
664 }
665
666 static void lisp_exit_net(struct net *net)
667 {
668         struct lisp_net *ln = net_generic(net, lisp_net_id);
669         struct lisp_dev *lisp, *next;
670         struct net_device *dev, *aux;
671         LIST_HEAD(list);
672
673         rtnl_lock();
674
675         /* gather any lisp devices that were moved into this ns */
676         for_each_netdev_safe(net, dev, aux)
677                 if (dev->rtnl_link_ops == &lisp_link_ops)
678                         unregister_netdevice_queue(dev, &list);
679
680         list_for_each_entry_safe(lisp, next, &ln->lisp_list, next) {
681                 /* If lisp->dev is in the same netns, it was already added
682                  * to the lisp by the previous loop.
683                  */
684                 if (!net_eq(dev_net(lisp->dev), net))
685                         unregister_netdevice_queue(lisp->dev, &list);
686         }
687
688         /* unregister the devices gathered above */
689         unregister_netdevice_many(&list);
690         rtnl_unlock();
691 }
692
693 static struct pernet_operations lisp_net_ops = {
694         .init = lisp_init_net,
695         .exit = lisp_exit_net,
696         .id   = &lisp_net_id,
697         .size = sizeof(struct lisp_net),
698 };
699
700 DEFINE_COMPAT_PNET_REG_FUNC(device)
701 int rpl_lisp_init_module(void)
702 {
703         int rc;
704
705         rc = register_pernet_subsys(&lisp_net_ops);
706         if (rc)
707                 goto out1;
708
709         rc = rtnl_link_register(&lisp_link_ops);
710         if (rc)
711                 goto out2;
712
713         pr_info("LISP tunneling driver\n");
714         return 0;
715 out2:
716         unregister_pernet_subsys(&lisp_net_ops);
717 out1:
718         return rc;
719 }
720
721 void rpl_lisp_cleanup_module(void)
722 {
723         rtnl_link_unregister(&lisp_link_ops);
724         unregister_pernet_subsys(&lisp_net_ops);
725 }