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/ip_tunnels.h>
41 #include <net/addrconf.h>
42 #include <net/xfrm.h>
43 #include <net/net_namespace.h>
44 #include <net/netns/generic.h>
45 #include <linux/etherdevice.h>
46
47 static int xfrmi_dev_init(struct net_device *dev);
48 static void xfrmi_dev_setup(struct net_device *dev);
49 static struct rtnl_link_ops xfrmi_link_ops __read_mostly;
50 static unsigned int xfrmi_net_id __read_mostly;
51 static const struct net_device_ops xfrmi_netdev_ops;
52
53 #define XFRMI_HASH_BITS 8
54 #define XFRMI_HASH_SIZE BIT(XFRMI_HASH_BITS)
55
56 struct xfrmi_net {
57 /* lists for storing interfaces in use */
58 struct xfrm_if __rcu *xfrmi[XFRMI_HASH_SIZE];
59 };
60
61 #define for_each_xfrmi_rcu(start, xi) \
62 for (xi = rcu_dereference(start); xi; xi = rcu_dereference(xi->next))
63
xfrmi_hash(u32 if_id)64 static u32 xfrmi_hash(u32 if_id)
65 {
66 return hash_32(if_id, XFRMI_HASH_BITS);
67 }
68
xfrmi_lookup(struct net * net,struct xfrm_state * x)69 static struct xfrm_if *xfrmi_lookup(struct net *net, struct xfrm_state *x)
70 {
71 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
72 struct xfrm_if *xi;
73
74 for_each_xfrmi_rcu(xfrmn->xfrmi[xfrmi_hash(x->if_id)], xi) {
75 if (x->if_id == xi->p.if_id &&
76 (xi->dev->flags & IFF_UP))
77 return xi;
78 }
79
80 return NULL;
81 }
82
xfrmi_decode_session(struct sk_buff * skb,unsigned short family)83 static struct xfrm_if *xfrmi_decode_session(struct sk_buff *skb,
84 unsigned short family)
85 {
86 struct net_device *dev;
87 int ifindex = 0;
88
89 if (!secpath_exists(skb) || !skb->dev)
90 return NULL;
91
92 switch (family) {
93 case AF_INET6:
94 ifindex = inet6_sdif(skb);
95 break;
96 case AF_INET:
97 ifindex = inet_sdif(skb);
98 break;
99 }
100
101 if (ifindex) {
102 struct net *net = xs_net(xfrm_input_state(skb));
103
104 dev = dev_get_by_index_rcu(net, ifindex);
105 } else {
106 dev = skb->dev;
107 }
108
109 if (!dev || !(dev->flags & IFF_UP))
110 return NULL;
111 if (dev->netdev_ops != &xfrmi_netdev_ops)
112 return NULL;
113
114 return netdev_priv(dev);
115 }
116
xfrmi_link(struct xfrmi_net * xfrmn,struct xfrm_if * xi)117 static void xfrmi_link(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
118 {
119 struct xfrm_if __rcu **xip = &xfrmn->xfrmi[xfrmi_hash(xi->p.if_id)];
120
121 rcu_assign_pointer(xi->next , rtnl_dereference(*xip));
122 rcu_assign_pointer(*xip, xi);
123 }
124
xfrmi_unlink(struct xfrmi_net * xfrmn,struct xfrm_if * xi)125 static void xfrmi_unlink(struct xfrmi_net *xfrmn, struct xfrm_if *xi)
126 {
127 struct xfrm_if __rcu **xip;
128 struct xfrm_if *iter;
129
130 for (xip = &xfrmn->xfrmi[xfrmi_hash(xi->p.if_id)];
131 (iter = rtnl_dereference(*xip)) != NULL;
132 xip = &iter->next) {
133 if (xi == iter) {
134 rcu_assign_pointer(*xip, xi->next);
135 break;
136 }
137 }
138 }
139
xfrmi_dev_free(struct net_device * dev)140 static void xfrmi_dev_free(struct net_device *dev)
141 {
142 struct xfrm_if *xi = netdev_priv(dev);
143
144 gro_cells_destroy(&xi->gro_cells);
145 free_percpu(dev->tstats);
146 }
147
xfrmi_create(struct net_device * dev)148 static int xfrmi_create(struct net_device *dev)
149 {
150 struct xfrm_if *xi = netdev_priv(dev);
151 struct net *net = dev_net(dev);
152 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
153 int err;
154
155 dev->rtnl_link_ops = &xfrmi_link_ops;
156 err = register_netdevice(dev);
157 if (err < 0)
158 goto out;
159
160 xfrmi_link(xfrmn, xi);
161
162 return 0;
163
164 out:
165 return err;
166 }
167
xfrmi_locate(struct net * net,struct xfrm_if_parms * p)168 static struct xfrm_if *xfrmi_locate(struct net *net, struct xfrm_if_parms *p)
169 {
170 struct xfrm_if __rcu **xip;
171 struct xfrm_if *xi;
172 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
173
174 for (xip = &xfrmn->xfrmi[xfrmi_hash(p->if_id)];
175 (xi = rtnl_dereference(*xip)) != NULL;
176 xip = &xi->next)
177 if (xi->p.if_id == p->if_id)
178 return xi;
179
180 return NULL;
181 }
182
xfrmi_dev_uninit(struct net_device * dev)183 static void xfrmi_dev_uninit(struct net_device *dev)
184 {
185 struct xfrm_if *xi = netdev_priv(dev);
186 struct xfrmi_net *xfrmn = net_generic(xi->net, xfrmi_net_id);
187
188 xfrmi_unlink(xfrmn, xi);
189 }
190
xfrmi_scrub_packet(struct sk_buff * skb,bool xnet)191 static void xfrmi_scrub_packet(struct sk_buff *skb, bool xnet)
192 {
193 skb->tstamp = 0;
194 skb->pkt_type = PACKET_HOST;
195 skb->skb_iif = 0;
196 skb->ignore_df = 0;
197 skb_dst_drop(skb);
198 nf_reset_ct(skb);
199 nf_reset_trace(skb);
200
201 if (!xnet)
202 return;
203
204 ipvs_reset(skb);
205 secpath_reset(skb);
206 skb_orphan(skb);
207 skb->mark = 0;
208 }
209
xfrmi_rcv_cb(struct sk_buff * skb,int err)210 static int xfrmi_rcv_cb(struct sk_buff *skb, int err)
211 {
212 const struct xfrm_mode *inner_mode;
213 struct net_device *dev;
214 struct xfrm_state *x;
215 struct xfrm_if *xi;
216 bool xnet;
217
218 if (err && !secpath_exists(skb))
219 return 0;
220
221 x = xfrm_input_state(skb);
222
223 xi = xfrmi_lookup(xs_net(x), x);
224 if (!xi)
225 return 1;
226
227 dev = xi->dev;
228 skb->dev = dev;
229
230 if (err) {
231 dev->stats.rx_errors++;
232 dev->stats.rx_dropped++;
233
234 return 0;
235 }
236
237 xnet = !net_eq(xi->net, dev_net(skb->dev));
238
239 if (xnet) {
240 inner_mode = &x->inner_mode;
241
242 if (x->sel.family == AF_UNSPEC) {
243 inner_mode = xfrm_ip2inner_mode(x, XFRM_MODE_SKB_CB(skb)->protocol);
244 if (inner_mode == NULL) {
245 XFRM_INC_STATS(dev_net(skb->dev),
246 LINUX_MIB_XFRMINSTATEMODEERROR);
247 return -EINVAL;
248 }
249 }
250
251 if (!xfrm_policy_check(NULL, XFRM_POLICY_IN, skb,
252 inner_mode->family))
253 return -EPERM;
254 }
255
256 xfrmi_scrub_packet(skb, xnet);
257 dev_sw_netstats_rx_add(dev, skb->len);
258
259 return 0;
260 }
261
262 static int
xfrmi_xmit2(struct sk_buff * skb,struct net_device * dev,struct flowi * fl)263 xfrmi_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 dst_hold(dst);
275 dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, xi->p.if_id);
276 if (IS_ERR(dst)) {
277 err = PTR_ERR(dst);
278 dst = NULL;
279 goto tx_err_link_failure;
280 }
281
282 x = dst->xfrm;
283 if (!x)
284 goto tx_err_link_failure;
285
286 if (x->if_id != xi->p.if_id)
287 goto tx_err_link_failure;
288
289 tdev = dst->dev;
290
291 if (tdev == dev) {
292 stats->collisions++;
293 net_warn_ratelimited("%s: Local routing loop detected!\n",
294 dev->name);
295 goto tx_err_dst_release;
296 }
297
298 mtu = dst_mtu(dst);
299 if (skb->len > mtu) {
300 skb_dst_update_pmtu_no_confirm(skb, mtu);
301
302 if (skb->protocol == htons(ETH_P_IPV6)) {
303 if (mtu < IPV6_MIN_MTU)
304 mtu = IPV6_MIN_MTU;
305
306 if (skb->len > 1280)
307 icmpv6_ndo_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
308 else
309 goto xmit;
310 } else {
311 if (!(ip_hdr(skb)->frag_off & htons(IP_DF)))
312 goto xmit;
313 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
314 htonl(mtu));
315 }
316
317 dst_release(dst);
318 return -EMSGSIZE;
319 }
320
321 xmit:
322 xfrmi_scrub_packet(skb, !net_eq(xi->net, dev_net(dev)));
323 skb_dst_set(skb, dst);
324 skb->dev = tdev;
325
326 err = dst_output(xi->net, skb->sk, skb);
327 if (net_xmit_eval(err) == 0) {
328 struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
329
330 u64_stats_update_begin(&tstats->syncp);
331 tstats->tx_bytes += length;
332 tstats->tx_packets++;
333 u64_stats_update_end(&tstats->syncp);
334 } else {
335 stats->tx_errors++;
336 stats->tx_aborted_errors++;
337 }
338
339 return 0;
340 tx_err_link_failure:
341 stats->tx_carrier_errors++;
342 dst_link_failure(skb);
343 tx_err_dst_release:
344 dst_release(dst);
345 return err;
346 }
347
xfrmi_xmit(struct sk_buff * skb,struct net_device * dev)348 static netdev_tx_t xfrmi_xmit(struct sk_buff *skb, struct net_device *dev)
349 {
350 struct xfrm_if *xi = netdev_priv(dev);
351 struct net_device_stats *stats = &xi->dev->stats;
352 struct dst_entry *dst = skb_dst(skb);
353 struct flowi fl;
354 int ret;
355
356 memset(&fl, 0, sizeof(fl));
357
358 switch (skb->protocol) {
359 case htons(ETH_P_IPV6):
360 xfrm_decode_session(skb, &fl, AF_INET6);
361 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
362 if (!dst) {
363 fl.u.ip6.flowi6_oif = dev->ifindex;
364 fl.u.ip6.flowi6_flags |= FLOWI_FLAG_ANYSRC;
365 dst = ip6_route_output(dev_net(dev), NULL, &fl.u.ip6);
366 if (dst->error) {
367 dst_release(dst);
368 stats->tx_carrier_errors++;
369 goto tx_err;
370 }
371 skb_dst_set(skb, dst);
372 }
373 break;
374 case htons(ETH_P_IP):
375 xfrm_decode_session(skb, &fl, AF_INET);
376 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
377 if (!dst) {
378 struct rtable *rt;
379
380 fl.u.ip4.flowi4_oif = dev->ifindex;
381 fl.u.ip4.flowi4_flags |= FLOWI_FLAG_ANYSRC;
382 rt = __ip_route_output_key(dev_net(dev), &fl.u.ip4);
383 if (IS_ERR(rt)) {
384 stats->tx_carrier_errors++;
385 goto tx_err;
386 }
387 skb_dst_set(skb, &rt->dst);
388 }
389 break;
390 default:
391 goto tx_err;
392 }
393
394 fl.flowi_oif = xi->p.link;
395
396 ret = xfrmi_xmit2(skb, dev, &fl);
397 if (ret < 0)
398 goto tx_err;
399
400 return NETDEV_TX_OK;
401
402 tx_err:
403 stats->tx_errors++;
404 stats->tx_dropped++;
405 kfree_skb(skb);
406 return NETDEV_TX_OK;
407 }
408
xfrmi4_err(struct sk_buff * skb,u32 info)409 static int xfrmi4_err(struct sk_buff *skb, u32 info)
410 {
411 const struct iphdr *iph = (const struct iphdr *)skb->data;
412 struct net *net = dev_net(skb->dev);
413 int protocol = iph->protocol;
414 struct ip_comp_hdr *ipch;
415 struct ip_esp_hdr *esph;
416 struct ip_auth_hdr *ah ;
417 struct xfrm_state *x;
418 struct xfrm_if *xi;
419 __be32 spi;
420
421 switch (protocol) {
422 case IPPROTO_ESP:
423 esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
424 spi = esph->spi;
425 break;
426 case IPPROTO_AH:
427 ah = (struct ip_auth_hdr *)(skb->data+(iph->ihl<<2));
428 spi = ah->spi;
429 break;
430 case IPPROTO_COMP:
431 ipch = (struct ip_comp_hdr *)(skb->data+(iph->ihl<<2));
432 spi = htonl(ntohs(ipch->cpi));
433 break;
434 default:
435 return 0;
436 }
437
438 switch (icmp_hdr(skb)->type) {
439 case ICMP_DEST_UNREACH:
440 if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
441 return 0;
442 case ICMP_REDIRECT:
443 break;
444 default:
445 return 0;
446 }
447
448 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
449 spi, protocol, AF_INET);
450 if (!x)
451 return 0;
452
453 xi = xfrmi_lookup(net, x);
454 if (!xi) {
455 xfrm_state_put(x);
456 return -1;
457 }
458
459 if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
460 ipv4_update_pmtu(skb, net, info, 0, protocol);
461 else
462 ipv4_redirect(skb, net, 0, protocol);
463 xfrm_state_put(x);
464
465 return 0;
466 }
467
xfrmi6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)468 static int xfrmi6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
469 u8 type, u8 code, int offset, __be32 info)
470 {
471 const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
472 struct net *net = dev_net(skb->dev);
473 int protocol = iph->nexthdr;
474 struct ip_comp_hdr *ipch;
475 struct ip_esp_hdr *esph;
476 struct ip_auth_hdr *ah;
477 struct xfrm_state *x;
478 struct xfrm_if *xi;
479 __be32 spi;
480
481 switch (protocol) {
482 case IPPROTO_ESP:
483 esph = (struct ip_esp_hdr *)(skb->data + offset);
484 spi = esph->spi;
485 break;
486 case IPPROTO_AH:
487 ah = (struct ip_auth_hdr *)(skb->data + offset);
488 spi = ah->spi;
489 break;
490 case IPPROTO_COMP:
491 ipch = (struct ip_comp_hdr *)(skb->data + offset);
492 spi = htonl(ntohs(ipch->cpi));
493 break;
494 default:
495 return 0;
496 }
497
498 if (type != ICMPV6_PKT_TOOBIG &&
499 type != NDISC_REDIRECT)
500 return 0;
501
502 x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
503 spi, protocol, AF_INET6);
504 if (!x)
505 return 0;
506
507 xi = xfrmi_lookup(net, x);
508 if (!xi) {
509 xfrm_state_put(x);
510 return -1;
511 }
512
513 if (type == NDISC_REDIRECT)
514 ip6_redirect(skb, net, skb->dev->ifindex, 0,
515 sock_net_uid(net, NULL));
516 else
517 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
518 xfrm_state_put(x);
519
520 return 0;
521 }
522
xfrmi_change(struct xfrm_if * xi,const struct xfrm_if_parms * p)523 static int xfrmi_change(struct xfrm_if *xi, const struct xfrm_if_parms *p)
524 {
525 if (xi->p.link != p->link)
526 return -EINVAL;
527
528 xi->p.if_id = p->if_id;
529
530 return 0;
531 }
532
xfrmi_update(struct xfrm_if * xi,struct xfrm_if_parms * p)533 static int xfrmi_update(struct xfrm_if *xi, struct xfrm_if_parms *p)
534 {
535 struct net *net = xi->net;
536 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
537 int err;
538
539 xfrmi_unlink(xfrmn, xi);
540 synchronize_net();
541 err = xfrmi_change(xi, p);
542 xfrmi_link(xfrmn, xi);
543 netdev_state_change(xi->dev);
544 return err;
545 }
546
xfrmi_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * s)547 static void xfrmi_get_stats64(struct net_device *dev,
548 struct rtnl_link_stats64 *s)
549 {
550 dev_fetch_sw_netstats(s, dev->tstats);
551
552 s->rx_dropped = dev->stats.rx_dropped;
553 s->tx_dropped = dev->stats.tx_dropped;
554 }
555
xfrmi_get_iflink(const struct net_device * dev)556 static int xfrmi_get_iflink(const struct net_device *dev)
557 {
558 struct xfrm_if *xi = netdev_priv(dev);
559
560 return xi->p.link;
561 }
562
563
564 static const struct net_device_ops xfrmi_netdev_ops = {
565 .ndo_init = xfrmi_dev_init,
566 .ndo_uninit = xfrmi_dev_uninit,
567 .ndo_start_xmit = xfrmi_xmit,
568 .ndo_get_stats64 = xfrmi_get_stats64,
569 .ndo_get_iflink = xfrmi_get_iflink,
570 };
571
xfrmi_dev_setup(struct net_device * dev)572 static void xfrmi_dev_setup(struct net_device *dev)
573 {
574 dev->netdev_ops = &xfrmi_netdev_ops;
575 dev->header_ops = &ip_tunnel_header_ops;
576 dev->type = ARPHRD_NONE;
577 dev->mtu = ETH_DATA_LEN;
578 dev->min_mtu = ETH_MIN_MTU;
579 dev->max_mtu = IP_MAX_MTU;
580 dev->flags = IFF_NOARP;
581 dev->needs_free_netdev = true;
582 dev->priv_destructor = xfrmi_dev_free;
583 netif_keep_dst(dev);
584
585 eth_broadcast_addr(dev->broadcast);
586 }
587
xfrmi_dev_init(struct net_device * dev)588 static int xfrmi_dev_init(struct net_device *dev)
589 {
590 struct xfrm_if *xi = netdev_priv(dev);
591 struct net_device *phydev = __dev_get_by_index(xi->net, xi->p.link);
592 int err;
593
594 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
595 if (!dev->tstats)
596 return -ENOMEM;
597
598 err = gro_cells_init(&xi->gro_cells, dev);
599 if (err) {
600 free_percpu(dev->tstats);
601 return err;
602 }
603
604 dev->features |= NETIF_F_LLTX;
605
606 if (phydev) {
607 dev->needed_headroom = phydev->needed_headroom;
608 dev->needed_tailroom = phydev->needed_tailroom;
609
610 if (is_zero_ether_addr(dev->dev_addr))
611 eth_hw_addr_inherit(dev, phydev);
612 if (is_zero_ether_addr(dev->broadcast))
613 memcpy(dev->broadcast, phydev->broadcast,
614 dev->addr_len);
615 } else {
616 eth_hw_addr_random(dev);
617 eth_broadcast_addr(dev->broadcast);
618 }
619
620 return 0;
621 }
622
xfrmi_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)623 static int xfrmi_validate(struct nlattr *tb[], struct nlattr *data[],
624 struct netlink_ext_ack *extack)
625 {
626 return 0;
627 }
628
xfrmi_netlink_parms(struct nlattr * data[],struct xfrm_if_parms * parms)629 static void xfrmi_netlink_parms(struct nlattr *data[],
630 struct xfrm_if_parms *parms)
631 {
632 memset(parms, 0, sizeof(*parms));
633
634 if (!data)
635 return;
636
637 if (data[IFLA_XFRM_LINK])
638 parms->link = nla_get_u32(data[IFLA_XFRM_LINK]);
639
640 if (data[IFLA_XFRM_IF_ID])
641 parms->if_id = nla_get_u32(data[IFLA_XFRM_IF_ID]);
642 }
643
xfrmi_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)644 static int xfrmi_newlink(struct net *src_net, struct net_device *dev,
645 struct nlattr *tb[], struct nlattr *data[],
646 struct netlink_ext_ack *extack)
647 {
648 struct net *net = dev_net(dev);
649 struct xfrm_if_parms p = {};
650 struct xfrm_if *xi;
651 int err;
652
653 xfrmi_netlink_parms(data, &p);
654 if (!p.if_id) {
655 NL_SET_ERR_MSG(extack, "if_id must be non zero");
656 return -EINVAL;
657 }
658
659 xi = xfrmi_locate(net, &p);
660 if (xi)
661 return -EEXIST;
662
663 xi = netdev_priv(dev);
664 xi->p = p;
665 xi->net = net;
666 xi->dev = dev;
667
668 err = xfrmi_create(dev);
669 return err;
670 }
671
xfrmi_dellink(struct net_device * dev,struct list_head * head)672 static void xfrmi_dellink(struct net_device *dev, struct list_head *head)
673 {
674 unregister_netdevice_queue(dev, head);
675 }
676
xfrmi_changelink(struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)677 static int xfrmi_changelink(struct net_device *dev, struct nlattr *tb[],
678 struct nlattr *data[],
679 struct netlink_ext_ack *extack)
680 {
681 struct xfrm_if *xi = netdev_priv(dev);
682 struct net *net = xi->net;
683 struct xfrm_if_parms p = {};
684
685 xfrmi_netlink_parms(data, &p);
686 if (!p.if_id) {
687 NL_SET_ERR_MSG(extack, "if_id must be non zero");
688 return -EINVAL;
689 }
690
691 xi = xfrmi_locate(net, &p);
692 if (!xi) {
693 xi = netdev_priv(dev);
694 } else {
695 if (xi->dev != dev)
696 return -EEXIST;
697 }
698
699 return xfrmi_update(xi, &p);
700 }
701
xfrmi_get_size(const struct net_device * dev)702 static size_t xfrmi_get_size(const struct net_device *dev)
703 {
704 return
705 /* IFLA_XFRM_LINK */
706 nla_total_size(4) +
707 /* IFLA_XFRM_IF_ID */
708 nla_total_size(4) +
709 0;
710 }
711
xfrmi_fill_info(struct sk_buff * skb,const struct net_device * dev)712 static int xfrmi_fill_info(struct sk_buff *skb, const struct net_device *dev)
713 {
714 struct xfrm_if *xi = netdev_priv(dev);
715 struct xfrm_if_parms *parm = &xi->p;
716
717 if (nla_put_u32(skb, IFLA_XFRM_LINK, parm->link) ||
718 nla_put_u32(skb, IFLA_XFRM_IF_ID, parm->if_id))
719 goto nla_put_failure;
720 return 0;
721
722 nla_put_failure:
723 return -EMSGSIZE;
724 }
725
xfrmi_get_link_net(const struct net_device * dev)726 static struct net *xfrmi_get_link_net(const struct net_device *dev)
727 {
728 struct xfrm_if *xi = netdev_priv(dev);
729
730 return xi->net;
731 }
732
733 static const struct nla_policy xfrmi_policy[IFLA_XFRM_MAX + 1] = {
734 [IFLA_XFRM_LINK] = { .type = NLA_U32 },
735 [IFLA_XFRM_IF_ID] = { .type = NLA_U32 },
736 };
737
738 static struct rtnl_link_ops xfrmi_link_ops __read_mostly = {
739 .kind = "xfrm",
740 .maxtype = IFLA_XFRM_MAX,
741 .policy = xfrmi_policy,
742 .priv_size = sizeof(struct xfrm_if),
743 .setup = xfrmi_dev_setup,
744 .validate = xfrmi_validate,
745 .newlink = xfrmi_newlink,
746 .dellink = xfrmi_dellink,
747 .changelink = xfrmi_changelink,
748 .get_size = xfrmi_get_size,
749 .fill_info = xfrmi_fill_info,
750 .get_link_net = xfrmi_get_link_net,
751 };
752
xfrmi_exit_batch_net(struct list_head * net_exit_list)753 static void __net_exit xfrmi_exit_batch_net(struct list_head *net_exit_list)
754 {
755 struct net *net;
756 LIST_HEAD(list);
757
758 rtnl_lock();
759 list_for_each_entry(net, net_exit_list, exit_list) {
760 struct xfrmi_net *xfrmn = net_generic(net, xfrmi_net_id);
761 struct xfrm_if __rcu **xip;
762 struct xfrm_if *xi;
763 int i;
764
765 for (i = 0; i < XFRMI_HASH_SIZE; i++) {
766 for (xip = &xfrmn->xfrmi[i];
767 (xi = rtnl_dereference(*xip)) != NULL;
768 xip = &xi->next)
769 unregister_netdevice_queue(xi->dev, &list);
770 }
771 }
772 unregister_netdevice_many(&list);
773 rtnl_unlock();
774 }
775
776 static struct pernet_operations xfrmi_net_ops = {
777 .exit_batch = xfrmi_exit_batch_net,
778 .id = &xfrmi_net_id,
779 .size = sizeof(struct xfrmi_net),
780 };
781
782 static struct xfrm6_protocol xfrmi_esp6_protocol __read_mostly = {
783 .handler = xfrm6_rcv,
784 .input_handler = xfrm_input,
785 .cb_handler = xfrmi_rcv_cb,
786 .err_handler = xfrmi6_err,
787 .priority = 10,
788 };
789
790 static struct xfrm6_protocol xfrmi_ah6_protocol __read_mostly = {
791 .handler = xfrm6_rcv,
792 .input_handler = xfrm_input,
793 .cb_handler = xfrmi_rcv_cb,
794 .err_handler = xfrmi6_err,
795 .priority = 10,
796 };
797
798 static struct xfrm6_protocol xfrmi_ipcomp6_protocol __read_mostly = {
799 .handler = xfrm6_rcv,
800 .input_handler = xfrm_input,
801 .cb_handler = xfrmi_rcv_cb,
802 .err_handler = xfrmi6_err,
803 .priority = 10,
804 };
805
806 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
xfrmi6_rcv_tunnel(struct sk_buff * skb)807 static int xfrmi6_rcv_tunnel(struct sk_buff *skb)
808 {
809 const xfrm_address_t *saddr;
810 __be32 spi;
811
812 saddr = (const xfrm_address_t *)&ipv6_hdr(skb)->saddr;
813 spi = xfrm6_tunnel_spi_lookup(dev_net(skb->dev), saddr);
814
815 return xfrm6_rcv_spi(skb, IPPROTO_IPV6, spi, NULL);
816 }
817
818 static struct xfrm6_tunnel xfrmi_ipv6_handler __read_mostly = {
819 .handler = xfrmi6_rcv_tunnel,
820 .cb_handler = xfrmi_rcv_cb,
821 .err_handler = xfrmi6_err,
822 .priority = 2,
823 };
824
825 static struct xfrm6_tunnel xfrmi_ip6ip_handler __read_mostly = {
826 .handler = xfrmi6_rcv_tunnel,
827 .cb_handler = xfrmi_rcv_cb,
828 .err_handler = xfrmi6_err,
829 .priority = 2,
830 };
831 #endif
832
833 static struct xfrm4_protocol xfrmi_esp4_protocol __read_mostly = {
834 .handler = xfrm4_rcv,
835 .input_handler = xfrm_input,
836 .cb_handler = xfrmi_rcv_cb,
837 .err_handler = xfrmi4_err,
838 .priority = 10,
839 };
840
841 static struct xfrm4_protocol xfrmi_ah4_protocol __read_mostly = {
842 .handler = xfrm4_rcv,
843 .input_handler = xfrm_input,
844 .cb_handler = xfrmi_rcv_cb,
845 .err_handler = xfrmi4_err,
846 .priority = 10,
847 };
848
849 static struct xfrm4_protocol xfrmi_ipcomp4_protocol __read_mostly = {
850 .handler = xfrm4_rcv,
851 .input_handler = xfrm_input,
852 .cb_handler = xfrmi_rcv_cb,
853 .err_handler = xfrmi4_err,
854 .priority = 10,
855 };
856
857 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
xfrmi4_rcv_tunnel(struct sk_buff * skb)858 static int xfrmi4_rcv_tunnel(struct sk_buff *skb)
859 {
860 return xfrm4_rcv_spi(skb, IPPROTO_IPIP, ip_hdr(skb)->saddr);
861 }
862
863 static struct xfrm_tunnel xfrmi_ipip_handler __read_mostly = {
864 .handler = xfrmi4_rcv_tunnel,
865 .cb_handler = xfrmi_rcv_cb,
866 .err_handler = xfrmi4_err,
867 .priority = 3,
868 };
869
870 static struct xfrm_tunnel xfrmi_ipip6_handler __read_mostly = {
871 .handler = xfrmi4_rcv_tunnel,
872 .cb_handler = xfrmi_rcv_cb,
873 .err_handler = xfrmi4_err,
874 .priority = 2,
875 };
876 #endif
877
xfrmi4_init(void)878 static int __init xfrmi4_init(void)
879 {
880 int err;
881
882 err = xfrm4_protocol_register(&xfrmi_esp4_protocol, IPPROTO_ESP);
883 if (err < 0)
884 goto xfrm_proto_esp_failed;
885 err = xfrm4_protocol_register(&xfrmi_ah4_protocol, IPPROTO_AH);
886 if (err < 0)
887 goto xfrm_proto_ah_failed;
888 err = xfrm4_protocol_register(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
889 if (err < 0)
890 goto xfrm_proto_comp_failed;
891 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
892 err = xfrm4_tunnel_register(&xfrmi_ipip_handler, AF_INET);
893 if (err < 0)
894 goto xfrm_tunnel_ipip_failed;
895 err = xfrm4_tunnel_register(&xfrmi_ipip6_handler, AF_INET6);
896 if (err < 0)
897 goto xfrm_tunnel_ipip6_failed;
898 #endif
899
900 return 0;
901
902 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
903 xfrm_tunnel_ipip6_failed:
904 xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET);
905 xfrm_tunnel_ipip_failed:
906 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
907 #endif
908 xfrm_proto_comp_failed:
909 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
910 xfrm_proto_ah_failed:
911 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
912 xfrm_proto_esp_failed:
913 return err;
914 }
915
xfrmi4_fini(void)916 static void xfrmi4_fini(void)
917 {
918 #if IS_REACHABLE(CONFIG_INET_XFRM_TUNNEL)
919 xfrm4_tunnel_deregister(&xfrmi_ipip6_handler, AF_INET6);
920 xfrm4_tunnel_deregister(&xfrmi_ipip_handler, AF_INET);
921 #endif
922 xfrm4_protocol_deregister(&xfrmi_ipcomp4_protocol, IPPROTO_COMP);
923 xfrm4_protocol_deregister(&xfrmi_ah4_protocol, IPPROTO_AH);
924 xfrm4_protocol_deregister(&xfrmi_esp4_protocol, IPPROTO_ESP);
925 }
926
xfrmi6_init(void)927 static int __init xfrmi6_init(void)
928 {
929 int err;
930
931 err = xfrm6_protocol_register(&xfrmi_esp6_protocol, IPPROTO_ESP);
932 if (err < 0)
933 goto xfrm_proto_esp_failed;
934 err = xfrm6_protocol_register(&xfrmi_ah6_protocol, IPPROTO_AH);
935 if (err < 0)
936 goto xfrm_proto_ah_failed;
937 err = xfrm6_protocol_register(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
938 if (err < 0)
939 goto xfrm_proto_comp_failed;
940 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
941 err = xfrm6_tunnel_register(&xfrmi_ipv6_handler, AF_INET6);
942 if (err < 0)
943 goto xfrm_tunnel_ipv6_failed;
944 err = xfrm6_tunnel_register(&xfrmi_ip6ip_handler, AF_INET);
945 if (err < 0)
946 goto xfrm_tunnel_ip6ip_failed;
947 #endif
948
949 return 0;
950
951 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
952 xfrm_tunnel_ip6ip_failed:
953 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
954 xfrm_tunnel_ipv6_failed:
955 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
956 #endif
957 xfrm_proto_comp_failed:
958 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
959 xfrm_proto_ah_failed:
960 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
961 xfrm_proto_esp_failed:
962 return err;
963 }
964
xfrmi6_fini(void)965 static void xfrmi6_fini(void)
966 {
967 #if IS_REACHABLE(CONFIG_INET6_XFRM_TUNNEL)
968 xfrm6_tunnel_deregister(&xfrmi_ip6ip_handler, AF_INET);
969 xfrm6_tunnel_deregister(&xfrmi_ipv6_handler, AF_INET6);
970 #endif
971 xfrm6_protocol_deregister(&xfrmi_ipcomp6_protocol, IPPROTO_COMP);
972 xfrm6_protocol_deregister(&xfrmi_ah6_protocol, IPPROTO_AH);
973 xfrm6_protocol_deregister(&xfrmi_esp6_protocol, IPPROTO_ESP);
974 }
975
976 static const struct xfrm_if_cb xfrm_if_cb = {
977 .decode_session = xfrmi_decode_session,
978 };
979
xfrmi_init(void)980 static int __init xfrmi_init(void)
981 {
982 const char *msg;
983 int err;
984
985 pr_info("IPsec XFRM device driver\n");
986
987 msg = "tunnel device";
988 err = register_pernet_device(&xfrmi_net_ops);
989 if (err < 0)
990 goto pernet_dev_failed;
991
992 msg = "xfrm4 protocols";
993 err = xfrmi4_init();
994 if (err < 0)
995 goto xfrmi4_failed;
996
997 msg = "xfrm6 protocols";
998 err = xfrmi6_init();
999 if (err < 0)
1000 goto xfrmi6_failed;
1001
1002
1003 msg = "netlink interface";
1004 err = rtnl_link_register(&xfrmi_link_ops);
1005 if (err < 0)
1006 goto rtnl_link_failed;
1007
1008 xfrm_if_register_cb(&xfrm_if_cb);
1009
1010 return err;
1011
1012 rtnl_link_failed:
1013 xfrmi6_fini();
1014 xfrmi6_failed:
1015 xfrmi4_fini();
1016 xfrmi4_failed:
1017 unregister_pernet_device(&xfrmi_net_ops);
1018 pernet_dev_failed:
1019 pr_err("xfrmi init: failed to register %s\n", msg);
1020 return err;
1021 }
1022
xfrmi_fini(void)1023 static void __exit xfrmi_fini(void)
1024 {
1025 xfrm_if_unregister_cb();
1026 rtnl_link_unregister(&xfrmi_link_ops);
1027 xfrmi4_fini();
1028 xfrmi6_fini();
1029 unregister_pernet_device(&xfrmi_net_ops);
1030 }
1031
1032 module_init(xfrmi_init);
1033 module_exit(xfrmi_fini);
1034 MODULE_LICENSE("GPL");
1035 MODULE_ALIAS_RTNL_LINK("xfrm");
1036 MODULE_ALIAS_NETDEV("xfrm0");
1037 MODULE_AUTHOR("Steffen Klassert");
1038 MODULE_DESCRIPTION("XFRM virtual interface");
1039