1// SPDX-License-Identifier: GPL-2.0
2/*
3 * xfrm6_policy.c: based on xfrm4_policy.c
4 *
5 * Authors:
6 *	Mitsuru KANDA @USAGI
7 *	Kazunori MIYAZAWA @USAGI
8 *	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 *		IPv6 support
10 *	YOSHIFUJI Hideaki
11 *		Split up af-specific portion
12 *
13 */
14
15#include <linux/err.h>
16#include <linux/kernel.h>
17#include <linux/netdevice.h>
18#include <net/addrconf.h>
19#include <net/dst.h>
20#include <net/xfrm.h>
21#include <net/ip.h>
22#include <net/ipv6.h>
23#include <net/ip6_route.h>
24#include <net/l3mdev.h>
25
26static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
27					  const xfrm_address_t *saddr,
28					  const xfrm_address_t *daddr,
29					  u32 mark)
30{
31	struct flowi6 fl6;
32	struct dst_entry *dst;
33	int err;
34
35	memset(&fl6, 0, sizeof(fl6));
36	fl6.flowi6_oif = l3mdev_master_ifindex_by_index(net, oif);
37	fl6.flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF;
38	fl6.flowi6_mark = mark;
39	memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
40	if (saddr)
41		memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
42
43	dst = ip6_route_output(net, NULL, &fl6);
44
45	err = dst->error;
46	if (dst->error) {
47		dst_release(dst);
48		dst = ERR_PTR(err);
49	}
50
51	return dst;
52}
53
54static int xfrm6_get_saddr(struct net *net, int oif,
55			   xfrm_address_t *saddr, xfrm_address_t *daddr,
56			   u32 mark)
57{
58	struct dst_entry *dst;
59	struct net_device *dev;
60	struct inet6_dev *idev;
61
62	dst = xfrm6_dst_lookup(net, 0, oif, NULL, daddr, mark);
63	if (IS_ERR(dst))
64		return -EHOSTUNREACH;
65
66	idev = ip6_dst_idev(dst);
67	if (!idev) {
68		dst_release(dst);
69		return -EHOSTUNREACH;
70	}
71	dev = idev->dev;
72	ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
73	dst_release(dst);
74	return 0;
75}
76
77static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
78			  const struct flowi *fl)
79{
80	struct rt6_info *rt = (struct rt6_info *)xdst->route;
81
82	xdst->u.dst.dev = dev;
83	dev_hold(dev);
84
85	xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
86	if (!xdst->u.rt6.rt6i_idev) {
87		dev_put(dev);
88		return -ENODEV;
89	}
90
91	/* Sheit... I remember I did this right. Apparently,
92	 * it was magically lost, so this code needs audit */
93	xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
94						   RTF_LOCAL);
95	xdst->route_cookie = rt6_get_cookie(rt);
96	xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
97	xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
98	xdst->u.rt6.rt6i_src = rt->rt6i_src;
99	INIT_LIST_HEAD(&xdst->u.rt6.rt6i_uncached);
100	rt6_uncached_list_add(&xdst->u.rt6);
101	atomic_inc(&dev_net(dev)->ipv6.rt6_stats->fib_rt_uncache);
102
103	return 0;
104}
105
106static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
107			      struct sk_buff *skb, u32 mtu,
108			      bool confirm_neigh)
109{
110	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
111	struct dst_entry *path = xdst->route;
112
113	path->ops->update_pmtu(path, sk, skb, mtu, confirm_neigh);
114}
115
116static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
117			   struct sk_buff *skb)
118{
119	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
120	struct dst_entry *path = xdst->route;
121
122	path->ops->redirect(path, sk, skb);
123}
124
125static void xfrm6_dst_destroy(struct dst_entry *dst)
126{
127	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
128
129	dst_destroy_metrics_generic(dst);
130	if (xdst->u.rt6.rt6i_uncached_list)
131		rt6_uncached_list_del(&xdst->u.rt6);
132	if (likely(xdst->u.rt6.rt6i_idev))
133		in6_dev_put(xdst->u.rt6.rt6i_idev);
134	xfrm_dst_destroy(xdst);
135}
136
137static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
138			     int unregister)
139{
140	struct xfrm_dst *xdst;
141
142	if (!unregister)
143		return;
144
145	xdst = (struct xfrm_dst *)dst;
146	if (xdst->u.rt6.rt6i_idev->dev == dev) {
147		struct inet6_dev *loopback_idev =
148			in6_dev_get(dev_net(dev)->loopback_dev);
149
150		do {
151			in6_dev_put(xdst->u.rt6.rt6i_idev);
152			xdst->u.rt6.rt6i_idev = loopback_idev;
153			in6_dev_hold(loopback_idev);
154			xdst = (struct xfrm_dst *)xfrm_dst_child(&xdst->u.dst);
155		} while (xdst->u.dst.xfrm);
156
157		__in6_dev_put(loopback_idev);
158	}
159
160	xfrm_dst_ifdown(dst, dev);
161}
162
163static struct dst_ops xfrm6_dst_ops_template = {
164	.family =		AF_INET6,
165	.update_pmtu =		xfrm6_update_pmtu,
166	.redirect =		xfrm6_redirect,
167	.cow_metrics =		dst_cow_metrics_generic,
168	.destroy =		xfrm6_dst_destroy,
169	.ifdown =		xfrm6_dst_ifdown,
170	.local_out =		__ip6_local_out,
171	.gc_thresh =		32768,
172};
173
174static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
175	.dst_ops =		&xfrm6_dst_ops_template,
176	.dst_lookup =		xfrm6_dst_lookup,
177	.get_saddr =		xfrm6_get_saddr,
178	.fill_dst =		xfrm6_fill_dst,
179	.blackhole_route =	ip6_blackhole_route,
180};
181
182static int __init xfrm6_policy_init(void)
183{
184	return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo, AF_INET6);
185}
186
187static void xfrm6_policy_fini(void)
188{
189	xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
190}
191
192#ifdef CONFIG_SYSCTL
193static struct ctl_table xfrm6_policy_table[] = {
194	{
195		.procname       = "xfrm6_gc_thresh",
196		.data		= &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
197		.maxlen		= sizeof(int),
198		.mode		= 0644,
199		.proc_handler   = proc_dointvec,
200	},
201	{ }
202};
203
204static int __net_init xfrm6_net_sysctl_init(struct net *net)
205{
206	struct ctl_table *table;
207	struct ctl_table_header *hdr;
208
209	table = xfrm6_policy_table;
210	if (!net_eq(net, &init_net)) {
211		table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
212		if (!table)
213			goto err_alloc;
214
215		table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
216	}
217
218	hdr = register_net_sysctl(net, "net/ipv6", table);
219	if (!hdr)
220		goto err_reg;
221
222	net->ipv6.sysctl.xfrm6_hdr = hdr;
223	return 0;
224
225err_reg:
226	if (!net_eq(net, &init_net))
227		kfree(table);
228err_alloc:
229	return -ENOMEM;
230}
231
232static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
233{
234	struct ctl_table *table;
235
236	if (!net->ipv6.sysctl.xfrm6_hdr)
237		return;
238
239	table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
240	unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
241	if (!net_eq(net, &init_net))
242		kfree(table);
243}
244#else /* CONFIG_SYSCTL */
245static inline int xfrm6_net_sysctl_init(struct net *net)
246{
247	return 0;
248}
249
250static inline void xfrm6_net_sysctl_exit(struct net *net)
251{
252}
253#endif
254
255static int __net_init xfrm6_net_init(struct net *net)
256{
257	int ret;
258
259	memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
260	       sizeof(xfrm6_dst_ops_template));
261	ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
262	if (ret)
263		return ret;
264
265	ret = xfrm6_net_sysctl_init(net);
266	if (ret)
267		dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
268
269	return ret;
270}
271
272static void __net_exit xfrm6_net_exit(struct net *net)
273{
274	xfrm6_net_sysctl_exit(net);
275	dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
276}
277
278static struct pernet_operations xfrm6_net_ops = {
279	.init	= xfrm6_net_init,
280	.exit	= xfrm6_net_exit,
281};
282
283int __init xfrm6_init(void)
284{
285	int ret;
286
287	ret = xfrm6_policy_init();
288	if (ret)
289		goto out;
290	ret = xfrm6_state_init();
291	if (ret)
292		goto out_policy;
293
294	ret = xfrm6_protocol_init();
295	if (ret)
296		goto out_state;
297
298	ret = register_pernet_subsys(&xfrm6_net_ops);
299	if (ret)
300		goto out_protocol;
301out:
302	return ret;
303out_protocol:
304	xfrm6_protocol_fini();
305out_state:
306	xfrm6_state_fini();
307out_policy:
308	xfrm6_policy_fini();
309	goto out;
310}
311
312void xfrm6_fini(void)
313{
314	unregister_pernet_subsys(&xfrm6_net_ops);
315	xfrm6_protocol_fini();
316	xfrm6_policy_fini();
317	xfrm6_state_fini();
318}
319