]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/xfrm/xfrm_interface.c
Merge branch 'work.misc' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[mirror_ubuntu-eoan-kernel.git] / net / xfrm / xfrm_interface.c
CommitLineData
f203b76d
SK
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * XFRM virtual interface
4 *
5 * Copyright (C) 2018 secunet Security Networks AG
6 *
7 * Author:
8 * Steffen Klassert <steffen.klassert@secunet.com>
9 */
10
11#include <linux/module.h>
12#include <linux/capability.h>
13#include <linux/errno.h>
14#include <linux/types.h>
15#include <linux/sockios.h>
16#include <linux/icmp.h>
17#include <linux/if.h>
18#include <linux/in.h>
19#include <linux/ip.h>
20#include <linux/net.h>
21#include <linux/in6.h>
22#include <linux/netdevice.h>
23#include <linux/if_link.h>
24#include <linux/if_arp.h>
25#include <linux/icmpv6.h>
26#include <linux/init.h>
27#include <linux/route.h>
28#include <linux/rtnetlink.h>
29#include <linux/netfilter_ipv6.h>
30#include <linux/slab.h>
31#include <linux/hash.h>
32
33#include <linux/uaccess.h>
34#include <linux/atomic.h>
35
36#include <net/icmp.h>
37#include <net/ip.h>
38#include <net/ipv6.h>
39#include <net/ip6_route.h>
40#include <net/addrconf.h>
41#include <net/xfrm.h>
42#include <net/net_namespace.h>
43#include <net/netns/generic.h>
44#include <linux/etherdevice.h>
45
46static int xfrmi_dev_init(struct net_device *dev);
47static void xfrmi_dev_setup(struct net_device *dev);
48static struct rtnl_link_ops xfrmi_link_ops __read_mostly;
49static unsigned int xfrmi_net_id __read_mostly;
50
51struct xfrmi_net {
52 /* lists for storing interfaces in use */
53 struct xfrm_if __rcu *xfrmi[1];
54};
55
56#define for_each_xfrmi_rcu(start, xi) \
57 for (xi = rcu_dereference(start); xi; xi = rcu_dereference(xi->next))
58
59static struct xfrm_if *xfrmi_lookup(struct net *net, struct xfrm_state *x)
60{
61 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
62 struct xfrm_if *xi;
63
64 for_each_xfrmi_rcu(xfrmn->xfrmi[0], xi) {
65 if (x->if_id == xi->p.if_id &&
66 (xi->dev->flags & IFF_UP))
67 return xi;
68 }
69
70 return NULL;
71}
72
025c65e1
MW
73static struct xfrm_if *xfrmi_decode_session(struct sk_buff *skb,
74 unsigned short family)
f203b76d
SK
75{
76 struct xfrmi_net *xfrmn;
f203b76d 77 struct xfrm_if *xi;
025c65e1 78 int ifindex = 0;
f203b76d 79
660899dd 80 if (!secpath_exists(skb) || !skb->dev)
f203b76d
SK
81 return NULL;
82
025c65e1
MW
83 switch (family) {
84 case AF_INET6:
85 ifindex = inet6_sdif(skb);
86 break;
87 case AF_INET:
88 ifindex = inet_sdif(skb);
89 break;
90 }
91 if (!ifindex)
92 ifindex = skb->dev->ifindex;
93
660899dd 94 xfrmn = net_generic(xs_net(xfrm_input_state(skb)), xfrmi_net_id);
f203b76d
SK
95
96 for_each_xfrmi_rcu(xfrmn->xfrmi[0], xi) {
97 if (ifindex == xi->dev->ifindex &&
98 (xi->dev->flags & IFF_UP))
99 return xi;
100 }
101
102 return NULL;
103}
104
105static void xfrmi_link(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
106{
107 struct xfrm_if __rcu **xip = &xfrmn->xfrmi[0];
108
109 rcu_assign_pointer(xi->next , rtnl_dereference(*xip));
110 rcu_assign_pointer(*xip, xi);
111}
112
113static void xfrmi_unlink(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
114{
115 struct xfrm_if __rcu **xip;
116 struct xfrm_if *iter;
117
118 for (xip = &xfrmn->xfrmi[0];
119 (iter = rtnl_dereference(*xip)) != NULL;
120 xip = &iter->next) {
121 if (xi == iter) {
122 rcu_assign_pointer(*xip, xi->next);
123 break;
124 }
125 }
126}
127
128static void xfrmi_dev_free(struct net_device *dev)
129{
4da40259
LR
130 struct xfrm_if *xi = netdev_priv(dev);
131
132 gro_cells_destroy(&xi->gro_cells);
f203b76d
SK
133 free_percpu(dev->tstats);
134}
135
56c5ee1a 136static int xfrmi_create(struct net_device *dev)
f203b76d
SK
137{
138 struct xfrm_if *xi = netdev_priv(dev);
139 struct net *net = dev_net(dev);
140 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
141 int err;
142
143 dev->rtnl_link_ops = &xfrmi_link_ops;
144 err = register_netdevice(dev);
145 if (err < 0)
146 goto out;
147
148 strcpy(xi->p.name, dev->name);
149
150 dev_hold(dev);
151 xfrmi_link(xfrmn, xi);
152
153 return 0;
154
155out:
156 return err;
157}
158
56c5ee1a 159static struct xfrm_if *xfrmi_locate(struct net *net, struct xfrm_if_parms *p)
f203b76d
SK
160{
161 struct xfrm_if __rcu **xip;
162 struct xfrm_if *xi;
163 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
164
165 for (xip = &xfrmn->xfrmi[0];
166 (xi = rtnl_dereference(*xip)) != NULL;
56c5ee1a
ND
167 xip = &xi->next)
168 if (xi->p.if_id == p->if_id)
f203b76d 169 return xi;
56c5ee1a
ND
170
171 return NULL;
f203b76d
SK
172}
173
174static void xfrmi_dev_uninit(struct net_device *dev)
175{
176 struct xfrm_if *xi = netdev_priv(dev);
177 struct xfrmi_net *xfrmn = net_generic(xi->net, xfrmi_net_id);
178
179 xfrmi_unlink(xfrmn, xi);
180 dev_put(xi->phydev);
181 dev_put(dev);
182}
183
184static void xfrmi_scrub_packet(struct sk_buff *skb, bool xnet)
185{
186 skb->tstamp = 0;
187 skb->pkt_type = PACKET_HOST;
188 skb->skb_iif = 0;
189 skb->ignore_df = 0;
190 skb_dst_drop(skb);
191 nf_reset(skb);
192 nf_reset_trace(skb);
193
194 if (!xnet)
195 return;
196
197 ipvs_reset(skb);
198 secpath_reset(skb);
199 skb_orphan(skb);
200 skb->mark = 0;
201}
202
203static int xfrmi_rcv_cb(struct sk_buff *skb, int err)
204{
4c145dce 205 const struct xfrm_mode *inner_mode;
f203b76d 206 struct pcpu_sw_netstats *tstats;
f203b76d
SK
207 struct net_device *dev;
208 struct xfrm_state *x;
209 struct xfrm_if *xi;
210 bool xnet;
211
26912e37 212 if (err && !secpath_exists(skb))
f203b76d
SK
213 return 0;
214
215 x = xfrm_input_state(skb);
216
217 xi = xfrmi_lookup(xs_net(x), x);
218 if (!xi)
219 return 1;
220
221 dev = xi->dev;
222 skb->dev = dev;
223
224 if (err) {
225 dev->stats.rx_errors++;
226 dev->stats.rx_dropped++;
227
228 return 0;
229 }
230
231 xnet = !net_eq(xi->net, dev_net(skb->dev));
232
233 if (xnet) {
c9500d7b 234 inner_mode = &x->inner_mode;
f203b76d
SK
235
236 if (x->sel.family == AF_UNSPEC) {
237 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
238 if (inner_mode == NULL) {
239 XFRM_INC_STATS(dev_net(skb->dev),
240 LINUX_MIB_XFRMINSTATEMODEERROR);
241 return -EINVAL;
242 }
243 }
244
245 if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb,
b45714b1 246 inner_mode->family))
f203b76d
SK
247 return -EPERM;
248 }
249
250 xfrmi_scrub_packet(skb, xnet);
251
252 tstats = this_cpu_ptr(dev->tstats);
253
254 u64_stats_update_begin(&tstats->syncp);
255 tstats->rx_packets++;
256 tstats->rx_bytes += skb->len;
257 u64_stats_update_end(&tstats->syncp);
258
259 return 0;
260}
261
262static int
263xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
264{
265 struct xfrm_if *xi = netdev_priv(dev);
266 struct net_device_stats *stats = &xi->dev->stats;
267 struct dst_entry *dst = skb_dst(skb);
268 unsigned int length = skb->len;
269 struct net_device *tdev;
270 struct xfrm_state *x;
271 int err = -1;
272 int mtu;
273
274 if (!dst)
275 goto tx_err_link_failure;
276
f203b76d 277 dst_hold(dst);
bc56b334 278 dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, xi->p.if_id);
f203b76d
SK
279 if (IS_ERR(dst)) {
280 err = PTR_ERR(dst);
281 dst = NULL;
282 goto tx_err_link_failure;
283 }
284
285 x = dst->xfrm;
286 if (!x)
287 goto tx_err_link_failure;
288
289 if (x->if_id != xi->p.if_id)
290 goto tx_err_link_failure;
291
292 tdev = dst->dev;
293
294 if (tdev == dev) {
295 stats->collisions++;
296 net_warn_ratelimited("%s: Local routing loop detected!\n",
297 xi->p.name);
298 goto tx_err_dst_release;
299 }
300
301 mtu = dst_mtu(dst);
302 if (!skb->ignore_df && skb->len > mtu) {
303 skb_dst_update_pmtu(skb, mtu);
304
305 if (skb->protocol == htons(ETH_P_IPV6)) {
306 if (mtu < IPV6_MIN_MTU)
307 mtu = IPV6_MIN_MTU;
308
309 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
310 } else {
311 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
312 htonl(mtu));
313 }
314
315 dst_release(dst);
316 return -EMSGSIZE;
317 }
318
319 xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev)));
320 skb_dst_set(skb, dst);
321 skb->dev = tdev;
322
323 err = dst_output(xi->net, skb->sk, skb);
324 if (net_xmit_eval(err) == 0) {
325 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
326
327 u64_stats_update_begin(&tstats->syncp);
328 tstats->tx_bytes += length;
329 tstats->tx_packets++;
330 u64_stats_update_end(&tstats->syncp);
331 } else {
332 stats->tx_errors++;
333 stats->tx_aborted_errors++;
334 }
335
336 return 0;
337tx_err_link_failure:
338 stats->tx_carrier_errors++;
339 dst_link_failure(skb);
340tx_err_dst_release:
341 dst_release(dst);
342 return err;
343}
344
345static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev)
346{
347 struct xfrm_if *xi = netdev_priv(dev);
348 struct net_device_stats *stats = &xi->dev->stats;
349 struct flowi fl;
350 int ret;
351
352 memset(&fl, 0, sizeof(fl));
353
354 switch (skb->protocol) {
355 case htons(ETH_P_IPV6):
356 xfrm_decode_session(skb, &fl, AF_INET6);
357 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
358 break;
359 case htons(ETH_P_IP):
360 xfrm_decode_session(skb, &fl, AF_INET);
361 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
362 break;
363 default:
364 goto tx_err;
365 }
366
367 fl.flowi_oif = xi->phydev->ifindex;
368
369 ret = xfrmi_xmit2(skb, dev, &fl);
370 if (ret < 0)
371 goto tx_err;
372
373 return NETDEV_TX_OK;
374
375tx_err:
376 stats->tx_errors++;
377 stats->tx_dropped++;
378 kfree_skb(skb);
379 return NETDEV_TX_OK;
380}
381
382static int xfrmi4_err(struct sk_buff *skb, u32 info)
383{
384 const struct iphdr *iph = (const struct iphdr *)skb->data;
385 struct net *net = dev_net(skb->dev);
386 int protocol = iph->protocol;
387 struct ip_comp_hdr *ipch;
388 struct ip_esp_hdr *esph;
389 struct ip_auth_hdr *ah ;
390 struct xfrm_state *x;
391 struct xfrm_if *xi;
392 __be32 spi;
393
394 switch (protocol) {
395 case IPPROTO_ESP:
396 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
397 spi = esph->spi;
398 break;
399 case IPPROTO_AH:
400 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
401 spi = ah->spi;
402 break;
403 case IPPROTO_COMP:
404 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
405 spi = htonl(ntohs(ipch->cpi));
406 break;
407 default:
408 return 0;
409 }
410
411 switch (icmp_hdr(skb)->type) {
412 case ICMP_DEST_UNREACH:
413 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
414 return 0;
415 case ICMP_REDIRECT:
416 break;
417 default:
418 return 0;
419 }
420
421 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
422 spi, protocol, AF_INET);
423 if (!x)
424 return 0;
425
426 xi = xfrmi_lookup(net, x);
427 if (!xi) {
428 xfrm_state_put(x);
429 return -1;
430 }
431
432 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
d888f396 433 ipv4_update_pmtu(skb, net, info, 0, protocol);
f203b76d 434 else
1042caa7 435 ipv4_redirect(skb, net, 0, protocol);
f203b76d
SK
436 xfrm_state_put(x);
437
438 return 0;
439}
440
441static int xfrmi6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
442 u8 type, u8 code, int offset, __be32 info)
443{
444 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
445 struct net *net = dev_net(skb->dev);
446 int protocol = iph->nexthdr;
447 struct ip_comp_hdr *ipch;
448 struct ip_esp_hdr *esph;
449 struct ip_auth_hdr *ah;
450 struct xfrm_state *x;
451 struct xfrm_if *xi;
452 __be32 spi;
453
454 switch (protocol) {
455 case IPPROTO_ESP:
456 esph = (struct ip_esp_hdr *)(skb->data + offset);
457 spi = esph->spi;
458 break;
459 case IPPROTO_AH:
460 ah = (struct ip_auth_hdr *)(skb->data + offset);
461 spi = ah->spi;
462 break;
463 case IPPROTO_COMP:
464 ipch = (struct ip_comp_hdr *)(skb->data + offset);
465 spi = htonl(ntohs(ipch->cpi));
466 break;
467 default:
468 return 0;
469 }
470
471 if (type != ICMPV6_PKT_TOOBIG &&
472 type != NDISC_REDIRECT)
473 return 0;
474
475 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
476 spi, protocol, AF_INET6);
477 if (!x)
478 return 0;
479
480 xi = xfrmi_lookup(net, x);
481 if (!xi) {
482 xfrm_state_put(x);
483 return -1;
484 }
485
486 if (type == NDISC_REDIRECT)
487 ip6_redirect(skb, net, skb->dev->ifindex, 0,
488 sock_net_uid(net, NULL));
489 else
490 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
491 xfrm_state_put(x);
492
493 return 0;
494}
495
496static int xfrmi_change(struct xfrm_if *xi, const struct xfrm_if_parms *p)
497{
498 if (xi->p.link != p->link)
499 return -EINVAL;
500
501 xi->p.if_id = p->if_id;
502
503 return 0;
504}
505
506static int xfrmi_update(struct xfrm_if *xi, struct xfrm_if_parms *p)
507{
508 struct net *net = dev_net(xi->dev);
509 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
510 int err;
511
512 xfrmi_unlink(xfrmn, xi);
513 synchronize_net();
514 err = xfrmi_change(xi, p);
515 xfrmi_link(xfrmn, xi);
516 netdev_state_change(xi->dev);
517 return err;
518}
519
520static void xfrmi_get_stats64(struct net_device *dev,
521 struct rtnl_link_stats64 *s)
522{
523 int cpu;
524
f203b76d
SK
525 for_each_possible_cpu(cpu) {
526 struct pcpu_sw_netstats *stats;
527 struct pcpu_sw_netstats tmp;
528 int start;
529
530 stats = per_cpu_ptr(dev->tstats, cpu);
531 do {
532 start = u64_stats_fetch_begin_irq(&stats->syncp);
533 tmp.rx_packets = stats->rx_packets;
534 tmp.rx_bytes = stats->rx_bytes;
535 tmp.tx_packets = stats->tx_packets;
536 tmp.tx_bytes = stats->tx_bytes;
537 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
538
539 s->rx_packets += tmp.rx_packets;
540 s->rx_bytes += tmp.rx_bytes;
541 s->tx_packets += tmp.tx_packets;
542 s->tx_bytes += tmp.tx_bytes;
543 }
544
545 s->rx_dropped = dev->stats.rx_dropped;
546 s->tx_dropped = dev->stats.tx_dropped;
547}
548
549static int xfrmi_get_iflink(const struct net_device *dev)
550{
551 struct xfrm_if *xi = netdev_priv(dev);
552
553 return xi->phydev->ifindex;
554}
555
556
557static const struct net_device_ops xfrmi_netdev_ops = {
558 .ndo_init = xfrmi_dev_init,
559 .ndo_uninit = xfrmi_dev_uninit,
560 .ndo_start_xmit = xfrmi_xmit,
561 .ndo_get_stats64 = xfrmi_get_stats64,
562 .ndo_get_iflink = xfrmi_get_iflink,
563};
564
565static void xfrmi_dev_setup(struct net_device *dev)
566{
567 dev->netdev_ops = &xfrmi_netdev_ops;
568 dev->type = ARPHRD_NONE;
569 dev->hard_header_len = ETH_HLEN;
570 dev->min_header_len = ETH_HLEN;
571 dev->mtu = ETH_DATA_LEN;
572 dev->min_mtu = ETH_MIN_MTU;
573 dev->max_mtu = ETH_DATA_LEN;
574 dev->addr_len = ETH_ALEN;
575 dev->flags = IFF_NOARP;
576 dev->needs_free_netdev = true;
577 dev->priv_destructor = xfrmi_dev_free;
578 netif_keep_dst(dev);
579}
580
581static int xfrmi_dev_init(struct net_device *dev)
582{
583 struct xfrm_if *xi = netdev_priv(dev);
584 struct net_device *phydev = xi->phydev;
585 int err;
586
587 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
588 if (!dev->tstats)
589 return -ENOMEM;
590
591 err = gro_cells_init(&xi->gro_cells, dev);
592 if (err) {
593 free_percpu(dev->tstats);
594 return err;
595 }
596
597 dev->features |= NETIF_F_LLTX;
598
599 dev->needed_headroom = phydev->needed_headroom;
600 dev->needed_tailroom = phydev->needed_tailroom;
601
602 if (is_zero_ether_addr(dev->dev_addr))
603 eth_hw_addr_inherit(dev, phydev);
604 if (is_zero_ether_addr(dev->broadcast))
605 memcpy(dev->broadcast, phydev->broadcast, dev->addr_len);
606
607 return 0;
608}
609
610static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[],
611 struct netlink_ext_ack *extack)
612{
613 return 0;
614}
615
616static void xfrmi_netlink_parms(struct nlattr *data[],
617 struct xfrm_if_parms *parms)
618{
619 memset(parms, 0, sizeof(*parms));
620
621 if (!data)
622 return;
623
624 if (data[IFLA_XFRM_LINK])
625 parms->link = nla_get_u32(data[IFLA_XFRM_LINK]);
626
627 if (data[IFLA_XFRM_IF_ID])
628 parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]);
629}
630
631static int xfrmi_newlink(struct net *src_net, struct net_device *dev,
632 struct nlattr *tb[], struct nlattr *data[],
633 struct netlink_ext_ack *extack)
634{
635 struct net *net = dev_net(dev);
56c5ee1a 636 struct xfrm_if_parms p;
f203b76d 637 struct xfrm_if *xi;
56c5ee1a 638 int err;
f203b76d 639
56c5ee1a 640 xfrmi_netlink_parms(data, &p);
f203b76d
SK
641
642 if (!tb[IFLA_IFNAME])
643 return -EINVAL;
644
56c5ee1a 645 nla_strlcpy(p.name, tb[IFLA_IFNAME], IFNAMSIZ);
f203b76d 646
56c5ee1a
ND
647 xi = xfrmi_locate(net, &p);
648 if (xi)
649 return -EEXIST;
650
651 xi = netdev_priv(dev);
652 xi->p = p;
653 xi->net = net;
654 xi->dev = dev;
655 xi->phydev = dev_get_by_index(net, p.link);
656 if (!xi->phydev)
657 return -ENODEV;
658
659 err = xfrmi_create(dev);
660 if (err < 0)
661 dev_put(xi->phydev);
662 return err;
f203b76d
SK
663}
664
665static void xfrmi_dellink(struct net_device *dev, struct list_head *head)
666{
667 unregister_netdevice_queue(dev, head);
668}
669
670static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],
671 struct nlattr *data[],
672 struct netlink_ext_ack *extack)
673{
674 struct xfrm_if *xi = netdev_priv(dev);
675 struct net *net = dev_net(dev);
676
677 xfrmi_netlink_parms(data, &xi->p);
678
56c5ee1a
ND
679 xi = xfrmi_locate(net, &xi->p);
680 if (!xi) {
44e2b838
BW
681 xi = netdev_priv(dev);
682 } else {
f203b76d
SK
683 if (xi->dev != dev)
684 return -EEXIST;
44e2b838 685 }
f203b76d
SK
686
687 return xfrmi_update(xi, &xi->p);
688}
689
690static size_t xfrmi_get_size(const struct net_device *dev)
691{
692 return
693 /* IFLA_XFRM_LINK */
694 nla_total_size(4) +
695 /* IFLA_XFRM_IF_ID */
696 nla_total_size(4) +
697 0;
698}
699
700static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev)
701{
702 struct xfrm_if *xi = netdev_priv(dev);
703 struct xfrm_if_parms *parm = &xi->p;
704
705 if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) ||
706 nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id))
707 goto nla_put_failure;
708 return 0;
709
710nla_put_failure:
711 return -EMSGSIZE;
712}
713
211d6f2d 714static struct net *xfrmi_get_link_net(const struct net_device *dev)
f203b76d
SK
715{
716 struct xfrm_if *xi = netdev_priv(dev);
717
718 return dev_net(xi->phydev);
719}
720
721static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = {
722 [IFLA_XFRM_LINK] = { .type = NLA_U32 },
723 [IFLA_XFRM_IF_ID] = { .type = NLA_U32 },
724};
725
726static struct rtnl_link_ops xfrmi_link_ops __read_mostly = {
727 .kind = "xfrm",
728 .maxtype = IFLA_XFRM_MAX,
729 .policy = xfrmi_policy,
730 .priv_size = sizeof(struct xfrm_if),
731 .setup = xfrmi_dev_setup,
732 .validate = xfrmi_validate,
733 .newlink = xfrmi_newlink,
734 .dellink = xfrmi_dellink,
735 .changelink = xfrmi_changelink,
736 .get_size = xfrmi_get_size,
737 .fill_info = xfrmi_fill_info,
738 .get_link_net = xfrmi_get_link_net,
739};
740
741static void __net_exit xfrmi_destroy_interfaces(struct xfrmi_net *xfrmn)
742{
743 struct xfrm_if *xi;
744 LIST_HEAD(list);
745
746 xi = rtnl_dereference(xfrmn->xfrmi[0]);
747 if (!xi)
748 return;
749
750 unregister_netdevice_queue(xi->dev, &list);
751 unregister_netdevice_many(&list);
752}
753
f203b76d
SK
754static void __net_exit xfrmi_exit_net(struct net *net)
755{
756 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
757
758 rtnl_lock();
759 xfrmi_destroy_interfaces(xfrmn);
760 rtnl_unlock();
761}
762
763static struct pernet_operations xfrmi_net_ops = {
f203b76d
SK
764 .exit = xfrmi_exit_net,
765 .id = &xfrmi_net_id,
766 .size = sizeof(struct xfrmi_net),
767};
768
769static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = {
770 .handler = xfrm6_rcv,
771 .cb_handler = xfrmi_rcv_cb,
772 .err_handler = xfrmi6_err,
773 .priority = 10,
774};
775
776static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = {
777 .handler = xfrm6_rcv,
778 .cb_handler = xfrmi_rcv_cb,
779 .err_handler = xfrmi6_err,
780 .priority = 10,
781};
782
783static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = {
784 .handler = xfrm6_rcv,
785 .cb_handler = xfrmi_rcv_cb,
786 .err_handler = xfrmi6_err,
787 .priority = 10,
788};
789
790static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = {
791 .handler = xfrm4_rcv,
792 .input_handler = xfrm_input,
793 .cb_handler = xfrmi_rcv_cb,
794 .err_handler = xfrmi4_err,
795 .priority = 10,
796};
797
798static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly = {
799 .handler = xfrm4_rcv,
800 .input_handler = xfrm_input,
801 .cb_handler = xfrmi_rcv_cb,
802 .err_handler = xfrmi4_err,
803 .priority = 10,
804};
805
806static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly = {
807 .handler = xfrm4_rcv,
808 .input_handler = xfrm_input,
809 .cb_handler = xfrmi_rcv_cb,
810 .err_handler = xfrmi4_err,
811 .priority = 10,
812};
813
814static int __init xfrmi4_init(void)
815{
816 int err;
817
818 err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP);
819 if (err < 0)
820 goto xfrm_proto_esp_failed;
821 err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH);
822 if (err < 0)
823 goto xfrm_proto_ah_failed;
824 err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
825 if (err < 0)
826 goto xfrm_proto_comp_failed;
827
828 return 0;
829
830xfrm_proto_comp_failed:
831 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
832xfrm_proto_ah_failed:
833 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
834xfrm_proto_esp_failed:
835 return err;
836}
837
838static void xfrmi4_fini(void)
839{
840 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
841 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
842 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
843}
844
845static int __init xfrmi6_init(void)
846{
847 int err;
848
849 err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP);
850 if (err < 0)
851 goto xfrm_proto_esp_failed;
852 err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH);
853 if (err < 0)
854 goto xfrm_proto_ah_failed;
855 err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
856 if (err < 0)
857 goto xfrm_proto_comp_failed;
858
859 return 0;
860
861xfrm_proto_comp_failed:
862 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
863xfrm_proto_ah_failed:
864 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
865xfrm_proto_esp_failed:
866 return err;
867}
868
869static void xfrmi6_fini(void)
870{
871 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
872 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
873 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
874}
875
876static const struct xfrm_if_cb xfrm_if_cb = {
877 .decode_session = xfrmi_decode_session,
878};
879
880static int __init xfrmi_init(void)
881{
882 const char *msg;
883 int err;
884
885 pr_info("IPsec XFRM device driver\n");
886
887 msg = "tunnel device";
888 err = register_pernet_device(&xfrmi_net_ops);
889 if (err < 0)
890 goto pernet_dev_failed;
891
892 msg = "xfrm4 protocols";
893 err = xfrmi4_init();
894 if (err < 0)
895 goto xfrmi4_failed;
896
897 msg = "xfrm6 protocols";
898 err = xfrmi6_init();
899 if (err < 0)
900 goto xfrmi6_failed;
901
902
903 msg = "netlink interface";
904 err = rtnl_link_register(&xfrmi_link_ops);
905 if (err < 0)
906 goto rtnl_link_failed;
907
908 xfrm_if_register_cb(&xfrm_if_cb);
909
910 return err;
911
912rtnl_link_failed:
913 xfrmi6_fini();
914xfrmi6_failed:
915 xfrmi4_fini();
916xfrmi4_failed:
917 unregister_pernet_device(&xfrmi_net_ops);
918pernet_dev_failed:
919 pr_err("xfrmi init: failed to register %s\n", msg);
920 return err;
921}
922
923static void __exit xfrmi_fini(void)
924{
925 xfrm_if_unregister_cb();
926 rtnl_link_unregister(&xfrmi_link_ops);
927 xfrmi4_fini();
928 xfrmi6_fini();
929 unregister_pernet_device(&xfrmi_net_ops);
930}
931
932module_init(xfrmi_init);
933module_exit(xfrmi_fini);
934MODULE_LICENSE("GPL");
935MODULE_ALIAS_RTNL_LINK("xfrm");
936MODULE_ALIAS_NETDEV("xfrm0");
937MODULE_AUTHOR("Steffen Klassert");
938MODULE_DESCRIPTION("XFRM virtual interface");