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