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