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