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