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