blob: 68336ee00d72b7d74fb4d60f6aa72213e59fbff7 [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
Martin Willi025c65e2019-03-26 13:20:43 +010073static struct xfrm_if *xfrmi_decode_session(struct sk_buff *skb,
74 unsigned short family)
Steffen Klassertf203b762018-06-12 14:07:12 +020075{
76 struct xfrmi_net *xfrmn;
Steffen Klassertf203b762018-06-12 14:07:12 +020077 struct xfrm_if *xi;
Martin Willi025c65e2019-03-26 13:20:43 +010078 int ifindex = 0;
Steffen Klassertf203b762018-06-12 14:07:12 +020079
Tobias Brunner660899d2019-02-18 10:49:39 +010080 if (!secpath_exists(skb) || !skb->dev)
Steffen Klassertf203b762018-06-12 14:07:12 +020081 return NULL;
82
Martin Willi025c65e2019-03-26 13:20:43 +010083 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
Tobias Brunner660899d2019-02-18 10:49:39 +010094 xfrmn = net_generic(xs_net(xfrm_input_state(skb)), xfrmi_net_id);
Steffen Klassertf203b762018-06-12 14:07:12 +020095
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{
Li RongQing4da40252018-09-30 15:06:06 +0800130 struct xfrm_if *xi = netdev_priv(dev);
131
132 gro_cells_destroy(&xi->gro_cells);
Steffen Klassertf203b762018-06-12 14:07:12 +0200133 free_percpu(dev->tstats);
134}
135
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200136static int xfrmi_create(struct net_device *dev)
Steffen Klassertf203b762018-06-12 14:07:12 +0200137{
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
Steffen Klassertf203b762018-06-12 14:07:12 +0200148 dev_hold(dev);
149 xfrmi_link(xfrmn, xi);
150
151 return 0;
152
153out:
154 return err;
155}
156
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200157static struct xfrm_if *xfrmi_locate(struct net *net, struct xfrm_if_parms *p)
Steffen Klassertf203b762018-06-12 14:07:12 +0200158{
159 struct xfrm_if __rcu **xip;
160 struct xfrm_if *xi;
161 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
162
163 for (xip = &xfrmn->xfrmi[0];
164 (xi = rtnl_dereference(*xip)) != NULL;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200165 xip = &xi->next)
166 if (xi->p.if_id == p->if_id)
Steffen Klassertf203b762018-06-12 14:07:12 +0200167 return xi;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200168
169 return NULL;
Steffen Klassertf203b762018-06-12 14:07:12 +0200170}
171
172static void xfrmi_dev_uninit(struct net_device *dev)
173{
174 struct xfrm_if *xi = netdev_priv(dev);
175 struct xfrmi_net *xfrmn = net_generic(xi->net, xfrmi_net_id);
176
177 xfrmi_unlink(xfrmn, xi);
178 dev_put(xi->phydev);
179 dev_put(dev);
180}
181
182static void xfrmi_scrub_packet(struct sk_buff *skb, bool xnet)
183{
184 skb->tstamp = 0;
185 skb->pkt_type = PACKET_HOST;
186 skb->skb_iif = 0;
187 skb->ignore_df = 0;
188 skb_dst_drop(skb);
189 nf_reset(skb);
190 nf_reset_trace(skb);
191
192 if (!xnet)
193 return;
194
195 ipvs_reset(skb);
196 secpath_reset(skb);
197 skb_orphan(skb);
198 skb->mark = 0;
199}
200
201static int xfrmi_rcv_cb(struct sk_buff *skb, int err)
202{
Florian Westphal4c145dc2019-03-29 21:16:31 +0100203 const struct xfrm_mode *inner_mode;
Steffen Klassertf203b762018-06-12 14:07:12 +0200204 struct pcpu_sw_netstats *tstats;
Steffen Klassertf203b762018-06-12 14:07:12 +0200205 struct net_device *dev;
206 struct xfrm_state *x;
207 struct xfrm_if *xi;
208 bool xnet;
209
Florian Westphal26912e32018-12-18 17:15:24 +0100210 if (err && !secpath_exists(skb))
Steffen Klassertf203b762018-06-12 14:07:12 +0200211 return 0;
212
213 x = xfrm_input_state(skb);
214
215 xi = xfrmi_lookup(xs_net(x), x);
216 if (!xi)
217 return 1;
218
219 dev = xi->dev;
220 skb->dev = dev;
221
222 if (err) {
223 dev->stats.rx_errors++;
224 dev->stats.rx_dropped++;
225
226 return 0;
227 }
228
229 xnet = !net_eq(xi->net, dev_net(skb->dev));
230
231 if (xnet) {
Florian Westphalc9500d72019-03-29 21:16:32 +0100232 inner_mode = &x->inner_mode;
Steffen Klassertf203b762018-06-12 14:07:12 +0200233
234 if (x->sel.family == AF_UNSPEC) {
235 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
236 if (inner_mode == NULL) {
237 XFRM_INC_STATS(dev_net(skb->dev),
238 LINUX_MIB_XFRMINSTATEMODEERROR);
239 return -EINVAL;
240 }
241 }
242
243 if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb,
Florian Westphalb45714b2019-03-29 21:16:23 +0100244 inner_mode->family))
Steffen Klassertf203b762018-06-12 14:07:12 +0200245 return -EPERM;
246 }
247
248 xfrmi_scrub_packet(skb, xnet);
249
250 tstats = this_cpu_ptr(dev->tstats);
251
252 u64_stats_update_begin(&tstats->syncp);
253 tstats->rx_packets++;
254 tstats->rx_bytes += skb->len;
255 u64_stats_update_end(&tstats->syncp);
256
257 return 0;
258}
259
260static int
261xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
262{
263 struct xfrm_if *xi = netdev_priv(dev);
264 struct net_device_stats *stats = &xi->dev->stats;
265 struct dst_entry *dst = skb_dst(skb);
266 unsigned int length = skb->len;
267 struct net_device *tdev;
268 struct xfrm_state *x;
269 int err = -1;
270 int mtu;
271
272 if (!dst)
273 goto tx_err_link_failure;
274
Steffen Klassertf203b762018-06-12 14:07:12 +0200275 dst_hold(dst);
Benedict Wongbc56b332018-07-19 10:50:44 -0700276 dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, xi->p.if_id);
Steffen Klassertf203b762018-06-12 14:07:12 +0200277 if (IS_ERR(dst)) {
278 err = PTR_ERR(dst);
279 dst = NULL;
280 goto tx_err_link_failure;
281 }
282
283 x = dst->xfrm;
284 if (!x)
285 goto tx_err_link_failure;
286
287 if (x->if_id != xi->p.if_id)
288 goto tx_err_link_failure;
289
290 tdev = dst->dev;
291
292 if (tdev == dev) {
293 stats->collisions++;
294 net_warn_ratelimited("%s: Local routing loop detected!\n",
Nicolas Dichtele0aaa332019-07-15 12:00:21 +0200295 dev->name);
Steffen Klassertf203b762018-06-12 14:07:12 +0200296 goto tx_err_dst_release;
297 }
298
299 mtu = dst_mtu(dst);
300 if (!skb->ignore_df && skb->len > mtu) {
301 skb_dst_update_pmtu(skb, mtu);
302
303 if (skb->protocol == htons(ETH_P_IPV6)) {
304 if (mtu < IPV6_MIN_MTU)
305 mtu = IPV6_MIN_MTU;
306
307 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
308 } else {
309 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
310 htonl(mtu));
311 }
312
313 dst_release(dst);
314 return -EMSGSIZE;
315 }
316
317 xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev)));
318 skb_dst_set(skb, dst);
319 skb->dev = tdev;
320
321 err = dst_output(xi->net, skb->sk, skb);
322 if (net_xmit_eval(err) == 0) {
323 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
324
325 u64_stats_update_begin(&tstats->syncp);
326 tstats->tx_bytes += length;
327 tstats->tx_packets++;
328 u64_stats_update_end(&tstats->syncp);
329 } else {
330 stats->tx_errors++;
331 stats->tx_aborted_errors++;
332 }
333
334 return 0;
335tx_err_link_failure:
336 stats->tx_carrier_errors++;
337 dst_link_failure(skb);
338tx_err_dst_release:
339 dst_release(dst);
340 return err;
341}
342
343static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev)
344{
345 struct xfrm_if *xi = netdev_priv(dev);
346 struct net_device_stats *stats = &xi->dev->stats;
347 struct flowi fl;
348 int ret;
349
350 memset(&fl, 0, sizeof(fl));
351
352 switch (skb->protocol) {
353 case htons(ETH_P_IPV6):
354 xfrm_decode_session(skb, &fl, AF_INET6);
355 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
356 break;
357 case htons(ETH_P_IP):
358 xfrm_decode_session(skb, &fl, AF_INET);
359 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
360 break;
361 default:
362 goto tx_err;
363 }
364
365 fl.flowi_oif = xi->phydev->ifindex;
366
367 ret = xfrmi_xmit2(skb, dev, &fl);
368 if (ret < 0)
369 goto tx_err;
370
371 return NETDEV_TX_OK;
372
373tx_err:
374 stats->tx_errors++;
375 stats->tx_dropped++;
376 kfree_skb(skb);
377 return NETDEV_TX_OK;
378}
379
380static int xfrmi4_err(struct sk_buff *skb, u32 info)
381{
382 const struct iphdr *iph = (const struct iphdr *)skb->data;
383 struct net *net = dev_net(skb->dev);
384 int protocol = iph->protocol;
385 struct ip_comp_hdr *ipch;
386 struct ip_esp_hdr *esph;
387 struct ip_auth_hdr *ah ;
388 struct xfrm_state *x;
389 struct xfrm_if *xi;
390 __be32 spi;
391
392 switch (protocol) {
393 case IPPROTO_ESP:
394 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
395 spi = esph->spi;
396 break;
397 case IPPROTO_AH:
398 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
399 spi = ah->spi;
400 break;
401 case IPPROTO_COMP:
402 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
403 spi = htonl(ntohs(ipch->cpi));
404 break;
405 default:
406 return 0;
407 }
408
409 switch (icmp_hdr(skb)->type) {
410 case ICMP_DEST_UNREACH:
411 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
412 return 0;
413 case ICMP_REDIRECT:
414 break;
415 default:
416 return 0;
417 }
418
419 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
420 spi, protocol, AF_INET);
421 if (!x)
422 return 0;
423
424 xi = xfrmi_lookup(net, x);
425 if (!xi) {
426 xfrm_state_put(x);
427 return -1;
428 }
429
430 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
Maciej Żenczykowskid888f392018-09-25 20:56:26 -0700431 ipv4_update_pmtu(skb, net, info, 0, protocol);
Steffen Klassertf203b762018-06-12 14:07:12 +0200432 else
Maciej Żenczykowski1042caa2018-09-25 20:56:27 -0700433 ipv4_redirect(skb, net, 0, protocol);
Steffen Klassertf203b762018-06-12 14:07:12 +0200434 xfrm_state_put(x);
435
436 return 0;
437}
438
439static int xfrmi6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
440 u8 type, u8 code, int offset, __be32 info)
441{
442 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
443 struct net *net = dev_net(skb->dev);
444 int protocol = iph->nexthdr;
445 struct ip_comp_hdr *ipch;
446 struct ip_esp_hdr *esph;
447 struct ip_auth_hdr *ah;
448 struct xfrm_state *x;
449 struct xfrm_if *xi;
450 __be32 spi;
451
452 switch (protocol) {
453 case IPPROTO_ESP:
454 esph = (struct ip_esp_hdr *)(skb->data + offset);
455 spi = esph->spi;
456 break;
457 case IPPROTO_AH:
458 ah = (struct ip_auth_hdr *)(skb->data + offset);
459 spi = ah->spi;
460 break;
461 case IPPROTO_COMP:
462 ipch = (struct ip_comp_hdr *)(skb->data + offset);
463 spi = htonl(ntohs(ipch->cpi));
464 break;
465 default:
466 return 0;
467 }
468
469 if (type != ICMPV6_PKT_TOOBIG &&
470 type != NDISC_REDIRECT)
471 return 0;
472
473 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
474 spi, protocol, AF_INET6);
475 if (!x)
476 return 0;
477
478 xi = xfrmi_lookup(net, x);
479 if (!xi) {
480 xfrm_state_put(x);
481 return -1;
482 }
483
484 if (type == NDISC_REDIRECT)
485 ip6_redirect(skb, net, skb->dev->ifindex, 0,
486 sock_net_uid(net, NULL));
487 else
488 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
489 xfrm_state_put(x);
490
491 return 0;
492}
493
494static int xfrmi_change(struct xfrm_if *xi, const struct xfrm_if_parms *p)
495{
496 if (xi->p.link != p->link)
497 return -EINVAL;
498
499 xi->p.if_id = p->if_id;
500
501 return 0;
502}
503
504static int xfrmi_update(struct xfrm_if *xi, struct xfrm_if_parms *p)
505{
506 struct net *net = dev_net(xi->dev);
507 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
508 int err;
509
510 xfrmi_unlink(xfrmn, xi);
511 synchronize_net();
512 err = xfrmi_change(xi, p);
513 xfrmi_link(xfrmn, xi);
514 netdev_state_change(xi->dev);
515 return err;
516}
517
518static void xfrmi_get_stats64(struct net_device *dev,
519 struct rtnl_link_stats64 *s)
520{
521 int cpu;
522
Steffen Klassertf203b762018-06-12 14:07:12 +0200523 for_each_possible_cpu(cpu) {
524 struct pcpu_sw_netstats *stats;
525 struct pcpu_sw_netstats tmp;
526 int start;
527
528 stats = per_cpu_ptr(dev->tstats, cpu);
529 do {
530 start = u64_stats_fetch_begin_irq(&stats->syncp);
531 tmp.rx_packets = stats->rx_packets;
532 tmp.rx_bytes = stats->rx_bytes;
533 tmp.tx_packets = stats->tx_packets;
534 tmp.tx_bytes = stats->tx_bytes;
535 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
536
537 s->rx_packets += tmp.rx_packets;
538 s->rx_bytes += tmp.rx_bytes;
539 s->tx_packets += tmp.tx_packets;
540 s->tx_bytes += tmp.tx_bytes;
541 }
542
543 s->rx_dropped = dev->stats.rx_dropped;
544 s->tx_dropped = dev->stats.tx_dropped;
545}
546
547static int xfrmi_get_iflink(const struct net_device *dev)
548{
549 struct xfrm_if *xi = netdev_priv(dev);
550
551 return xi->phydev->ifindex;
552}
553
554
555static const struct net_device_ops xfrmi_netdev_ops = {
556 .ndo_init = xfrmi_dev_init,
557 .ndo_uninit = xfrmi_dev_uninit,
558 .ndo_start_xmit = xfrmi_xmit,
559 .ndo_get_stats64 = xfrmi_get_stats64,
560 .ndo_get_iflink = xfrmi_get_iflink,
561};
562
563static void xfrmi_dev_setup(struct net_device *dev)
564{
565 dev->netdev_ops = &xfrmi_netdev_ops;
566 dev->type = ARPHRD_NONE;
567 dev->hard_header_len = ETH_HLEN;
568 dev->min_header_len = ETH_HLEN;
569 dev->mtu = ETH_DATA_LEN;
570 dev->min_mtu = ETH_MIN_MTU;
571 dev->max_mtu = ETH_DATA_LEN;
572 dev->addr_len = ETH_ALEN;
573 dev->flags = IFF_NOARP;
574 dev->needs_free_netdev = true;
575 dev->priv_destructor = xfrmi_dev_free;
576 netif_keep_dst(dev);
577}
578
579static int xfrmi_dev_init(struct net_device *dev)
580{
581 struct xfrm_if *xi = netdev_priv(dev);
582 struct net_device *phydev = xi->phydev;
583 int err;
584
585 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
586 if (!dev->tstats)
587 return -ENOMEM;
588
589 err = gro_cells_init(&xi->gro_cells, dev);
590 if (err) {
591 free_percpu(dev->tstats);
592 return err;
593 }
594
595 dev->features |= NETIF_F_LLTX;
596
597 dev->needed_headroom = phydev->needed_headroom;
598 dev->needed_tailroom = phydev->needed_tailroom;
599
600 if (is_zero_ether_addr(dev->dev_addr))
601 eth_hw_addr_inherit(dev, phydev);
602 if (is_zero_ether_addr(dev->broadcast))
603 memcpy(dev->broadcast, phydev->broadcast, dev->addr_len);
604
605 return 0;
606}
607
608static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[],
609 struct netlink_ext_ack *extack)
610{
611 return 0;
612}
613
614static void xfrmi_netlink_parms(struct nlattr *data[],
615 struct xfrm_if_parms *parms)
616{
617 memset(parms, 0, sizeof(*parms));
618
619 if (!data)
620 return;
621
622 if (data[IFLA_XFRM_LINK])
623 parms->link = nla_get_u32(data[IFLA_XFRM_LINK]);
624
625 if (data[IFLA_XFRM_IF_ID])
626 parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]);
627}
628
629static int xfrmi_newlink(struct net *src_net, struct net_device *dev,
630 struct nlattr *tb[], struct nlattr *data[],
631 struct netlink_ext_ack *extack)
632{
633 struct net *net = dev_net(dev);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200634 struct xfrm_if_parms p;
Steffen Klassertf203b762018-06-12 14:07:12 +0200635 struct xfrm_if *xi;
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200636 int err;
Steffen Klassertf203b762018-06-12 14:07:12 +0200637
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200638 xfrmi_netlink_parms(data, &p);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200639 xi = xfrmi_locate(net, &p);
640 if (xi)
641 return -EEXIST;
642
643 xi = netdev_priv(dev);
644 xi->p = p;
645 xi->net = net;
646 xi->dev = dev;
647 xi->phydev = dev_get_by_index(net, p.link);
648 if (!xi->phydev)
649 return -ENODEV;
650
651 err = xfrmi_create(dev);
652 if (err < 0)
653 dev_put(xi->phydev);
654 return err;
Steffen Klassertf203b762018-06-12 14:07:12 +0200655}
656
657static void xfrmi_dellink(struct net_device *dev, struct list_head *head)
658{
659 unregister_netdevice_queue(dev, head);
660}
661
662static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],
663 struct nlattr *data[],
664 struct netlink_ext_ack *extack)
665{
Steffen Klassertf203b762018-06-12 14:07:12 +0200666 struct net *net = dev_net(dev);
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200667 struct xfrm_if_parms p;
668 struct xfrm_if *xi;
Steffen Klassertf203b762018-06-12 14:07:12 +0200669
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200670 xfrmi_netlink_parms(data, &p);
671 xi = xfrmi_locate(net, &p);
Nicolas Dichtel56c5ee12019-07-02 17:51:39 +0200672 if (!xi) {
Benedict Wong44e2b832018-07-25 13:45:29 -0700673 xi = netdev_priv(dev);
674 } else {
Steffen Klassertf203b762018-06-12 14:07:12 +0200675 if (xi->dev != dev)
676 return -EEXIST;
Benedict Wong44e2b832018-07-25 13:45:29 -0700677 }
Steffen Klassertf203b762018-06-12 14:07:12 +0200678
Nicolas Dichtele9e7e852019-07-15 12:00:20 +0200679 return xfrmi_update(xi, &p);
Steffen Klassertf203b762018-06-12 14:07:12 +0200680}
681
682static size_t xfrmi_get_size(const struct net_device *dev)
683{
684 return
685 /* IFLA_XFRM_LINK */
686 nla_total_size(4) +
687 /* IFLA_XFRM_IF_ID */
688 nla_total_size(4) +
689 0;
690}
691
692static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev)
693{
694 struct xfrm_if *xi = netdev_priv(dev);
695 struct xfrm_if_parms *parm = &xi->p;
696
697 if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) ||
698 nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id))
699 goto nla_put_failure;
700 return 0;
701
702nla_put_failure:
703 return -EMSGSIZE;
704}
705
Wei Yongjun211d6f22018-07-28 06:49:48 +0000706static struct net *xfrmi_get_link_net(const struct net_device *dev)
Steffen Klassertf203b762018-06-12 14:07:12 +0200707{
708 struct xfrm_if *xi = netdev_priv(dev);
709
710 return dev_net(xi->phydev);
711}
712
713static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = {
714 [IFLA_XFRM_LINK] = { .type = NLA_U32 },
715 [IFLA_XFRM_IF_ID] = { .type = NLA_U32 },
716};
717
718static struct rtnl_link_ops xfrmi_link_ops __read_mostly = {
719 .kind = "xfrm",
720 .maxtype = IFLA_XFRM_MAX,
721 .policy = xfrmi_policy,
722 .priv_size = sizeof(struct xfrm_if),
723 .setup = xfrmi_dev_setup,
724 .validate = xfrmi_validate,
725 .newlink = xfrmi_newlink,
726 .dellink = xfrmi_dellink,
727 .changelink = xfrmi_changelink,
728 .get_size = xfrmi_get_size,
729 .fill_info = xfrmi_fill_info,
730 .get_link_net = xfrmi_get_link_net,
731};
732
733static void __net_exit xfrmi_destroy_interfaces(struct xfrmi_net *xfrmn)
734{
735 struct xfrm_if *xi;
736 LIST_HEAD(list);
737
738 xi = rtnl_dereference(xfrmn->xfrmi[0]);
739 if (!xi)
740 return;
741
742 unregister_netdevice_queue(xi->dev, &list);
743 unregister_netdevice_many(&list);
744}
745
Steffen Klassertf203b762018-06-12 14:07:12 +0200746static void __net_exit xfrmi_exit_net(struct net *net)
747{
748 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
749
750 rtnl_lock();
751 xfrmi_destroy_interfaces(xfrmn);
752 rtnl_unlock();
753}
754
755static struct pernet_operations xfrmi_net_ops = {
Steffen Klassertf203b762018-06-12 14:07:12 +0200756 .exit = xfrmi_exit_net,
757 .id = &xfrmi_net_id,
758 .size = sizeof(struct xfrmi_net),
759};
760
761static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = {
762 .handler = xfrm6_rcv,
763 .cb_handler = xfrmi_rcv_cb,
764 .err_handler = xfrmi6_err,
765 .priority = 10,
766};
767
768static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = {
769 .handler = xfrm6_rcv,
770 .cb_handler = xfrmi_rcv_cb,
771 .err_handler = xfrmi6_err,
772 .priority = 10,
773};
774
775static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = {
776 .handler = xfrm6_rcv,
777 .cb_handler = xfrmi_rcv_cb,
778 .err_handler = xfrmi6_err,
779 .priority = 10,
780};
781
782static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = {
783 .handler = xfrm4_rcv,
784 .input_handler = xfrm_input,
785 .cb_handler = xfrmi_rcv_cb,
786 .err_handler = xfrmi4_err,
787 .priority = 10,
788};
789
790static struct xfrm4_protocol xfrmi_ah4_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_ipcomp4_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 int __init xfrmi4_init(void)
807{
808 int err;
809
810 err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP);
811 if (err < 0)
812 goto xfrm_proto_esp_failed;
813 err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH);
814 if (err < 0)
815 goto xfrm_proto_ah_failed;
816 err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
817 if (err < 0)
818 goto xfrm_proto_comp_failed;
819
820 return 0;
821
822xfrm_proto_comp_failed:
823 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
824xfrm_proto_ah_failed:
825 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
826xfrm_proto_esp_failed:
827 return err;
828}
829
830static void xfrmi4_fini(void)
831{
832 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
833 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
834 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
835}
836
837static int __init xfrmi6_init(void)
838{
839 int err;
840
841 err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP);
842 if (err < 0)
843 goto xfrm_proto_esp_failed;
844 err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH);
845 if (err < 0)
846 goto xfrm_proto_ah_failed;
847 err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
848 if (err < 0)
849 goto xfrm_proto_comp_failed;
850
851 return 0;
852
853xfrm_proto_comp_failed:
854 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
855xfrm_proto_ah_failed:
856 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
857xfrm_proto_esp_failed:
858 return err;
859}
860
861static void xfrmi6_fini(void)
862{
863 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
864 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
865 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
866}
867
868static const struct xfrm_if_cb xfrm_if_cb = {
869 .decode_session = xfrmi_decode_session,
870};
871
872static int __init xfrmi_init(void)
873{
874 const char *msg;
875 int err;
876
877 pr_info("IPsec XFRM device driver\n");
878
879 msg = "tunnel device";
880 err = register_pernet_device(&xfrmi_net_ops);
881 if (err < 0)
882 goto pernet_dev_failed;
883
884 msg = "xfrm4 protocols";
885 err = xfrmi4_init();
886 if (err < 0)
887 goto xfrmi4_failed;
888
889 msg = "xfrm6 protocols";
890 err = xfrmi6_init();
891 if (err < 0)
892 goto xfrmi6_failed;
893
894
895 msg = "netlink interface";
896 err = rtnl_link_register(&xfrmi_link_ops);
897 if (err < 0)
898 goto rtnl_link_failed;
899
900 xfrm_if_register_cb(&xfrm_if_cb);
901
902 return err;
903
904rtnl_link_failed:
905 xfrmi6_fini();
906xfrmi6_failed:
907 xfrmi4_fini();
908xfrmi4_failed:
909 unregister_pernet_device(&xfrmi_net_ops);
910pernet_dev_failed:
911 pr_err("xfrmi init: failed to register %s\n", msg);
912 return err;
913}
914
915static void __exit xfrmi_fini(void)
916{
917 xfrm_if_unregister_cb();
918 rtnl_link_unregister(&xfrmi_link_ops);
919 xfrmi4_fini();
920 xfrmi6_fini();
921 unregister_pernet_device(&xfrmi_net_ops);
922}
923
924module_init(xfrmi_init);
925module_exit(xfrmi_fini);
926MODULE_LICENSE("GPL");
927MODULE_ALIAS_RTNL_LINK("xfrm");
928MODULE_ALIAS_NETDEV("xfrm0");
929MODULE_AUTHOR("Steffen Klassert");
930MODULE_DESCRIPTION("XFRM virtual interface");