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