xref: /linux/net/ipv6/xfrm6_policy.c (revision cc04a46f11ea046ed53e2c832ae29e4790f7e35f)
1 /*
2  * xfrm6_policy.c: based on xfrm4_policy.c
3  *
4  * Authors:
5  *	Mitsuru KANDA @USAGI
6  *	Kazunori MIYAZAWA @USAGI
7  *	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8  *		IPv6 support
9  *	YOSHIFUJI Hideaki
10  *		Split up af-specific portion
11  *
12  */
13 
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <net/addrconf.h>
18 #include <net/dst.h>
19 #include <net/xfrm.h>
20 #include <net/ip.h>
21 #include <net/ipv6.h>
22 #include <net/ip6_route.h>
23 #include <net/vrf.h>
24 #if IS_ENABLED(CONFIG_IPV6_MIP6)
25 #include <net/mip6.h>
26 #endif
27 
28 static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
29 
30 static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
31 					  const xfrm_address_t *saddr,
32 					  const xfrm_address_t *daddr)
33 {
34 	struct flowi6 fl6;
35 	struct dst_entry *dst;
36 	int err;
37 
38 	memset(&fl6, 0, sizeof(fl6));
39 	fl6.flowi6_oif = oif;
40 	memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr));
41 	if (saddr)
42 		memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr));
43 
44 	dst = ip6_route_output(net, NULL, &fl6);
45 
46 	err = dst->error;
47 	if (dst->error) {
48 		dst_release(dst);
49 		dst = ERR_PTR(err);
50 	}
51 
52 	return dst;
53 }
54 
55 static int xfrm6_get_saddr(struct net *net, int oif,
56 			   xfrm_address_t *saddr, xfrm_address_t *daddr)
57 {
58 	struct dst_entry *dst;
59 	struct net_device *dev;
60 
61 	dst = xfrm6_dst_lookup(net, 0, oif, NULL, daddr);
62 	if (IS_ERR(dst))
63 		return -EHOSTUNREACH;
64 
65 	dev = ip6_dst_idev(dst)->dev;
66 	ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
67 	dst_release(dst);
68 	return 0;
69 }
70 
71 static int xfrm6_get_tos(const struct flowi *fl)
72 {
73 	return 0;
74 }
75 
76 static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
77 			   int nfheader_len)
78 {
79 	if (dst->ops->family == AF_INET6) {
80 		struct rt6_info *rt = (struct rt6_info *)dst;
81 		path->path_cookie = rt6_get_cookie(rt);
82 	}
83 
84 	path->u.rt6.rt6i_nfheader_len = nfheader_len;
85 
86 	return 0;
87 }
88 
89 static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
90 			  const struct flowi *fl)
91 {
92 	struct rt6_info *rt = (struct rt6_info *)xdst->route;
93 
94 	xdst->u.dst.dev = dev;
95 	dev_hold(dev);
96 
97 	xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
98 	if (!xdst->u.rt6.rt6i_idev) {
99 		dev_put(dev);
100 		return -ENODEV;
101 	}
102 
103 	/* Sheit... I remember I did this right. Apparently,
104 	 * it was magically lost, so this code needs audit */
105 	xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
106 						   RTF_LOCAL);
107 	xdst->u.rt6.rt6i_metric = rt->rt6i_metric;
108 	xdst->u.rt6.rt6i_node = rt->rt6i_node;
109 	xdst->route_cookie = rt6_get_cookie(rt);
110 	xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
111 	xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
112 	xdst->u.rt6.rt6i_src = rt->rt6i_src;
113 
114 	return 0;
115 }
116 
117 static inline void
118 _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
119 {
120 	struct flowi6 *fl6 = &fl->u.ip6;
121 	int onlyproto = 0;
122 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
123 	u16 offset = sizeof(*hdr);
124 	struct ipv6_opt_hdr *exthdr;
125 	const unsigned char *nh = skb_network_header(skb);
126 	u16 nhoff = IP6CB(skb)->nhoff;
127 	int oif = 0;
128 	u8 nexthdr;
129 
130 	if (!nhoff)
131 		nhoff = offsetof(struct ipv6hdr, nexthdr);
132 
133 	nexthdr = nh[nhoff];
134 
135 	if (skb_dst(skb)) {
136 		oif = vrf_master_ifindex(skb_dst(skb)->dev) ?
137 			: skb_dst(skb)->dev->ifindex;
138 	}
139 
140 	memset(fl6, 0, sizeof(struct flowi6));
141 	fl6->flowi6_mark = skb->mark;
142 	fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
143 
144 	fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
145 	fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
146 
147 	while (nh + offset + 1 < skb->data ||
148 	       pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
149 		nh = skb_network_header(skb);
150 		exthdr = (struct ipv6_opt_hdr *)(nh + offset);
151 
152 		switch (nexthdr) {
153 		case NEXTHDR_FRAGMENT:
154 			onlyproto = 1;
155 		case NEXTHDR_ROUTING:
156 		case NEXTHDR_HOP:
157 		case NEXTHDR_DEST:
158 			offset += ipv6_optlen(exthdr);
159 			nexthdr = exthdr->nexthdr;
160 			exthdr = (struct ipv6_opt_hdr *)(nh + offset);
161 			break;
162 
163 		case IPPROTO_UDP:
164 		case IPPROTO_UDPLITE:
165 		case IPPROTO_TCP:
166 		case IPPROTO_SCTP:
167 		case IPPROTO_DCCP:
168 			if (!onlyproto && (nh + offset + 4 < skb->data ||
169 			     pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
170 				__be16 *ports;
171 
172 				nh = skb_network_header(skb);
173 				ports = (__be16 *)(nh + offset);
174 				fl6->fl6_sport = ports[!!reverse];
175 				fl6->fl6_dport = ports[!reverse];
176 			}
177 			fl6->flowi6_proto = nexthdr;
178 			return;
179 
180 		case IPPROTO_ICMPV6:
181 			if (!onlyproto && pskb_may_pull(skb, nh + offset + 2 - skb->data)) {
182 				u8 *icmp;
183 
184 				nh = skb_network_header(skb);
185 				icmp = (u8 *)(nh + offset);
186 				fl6->fl6_icmp_type = icmp[0];
187 				fl6->fl6_icmp_code = icmp[1];
188 			}
189 			fl6->flowi6_proto = nexthdr;
190 			return;
191 
192 #if IS_ENABLED(CONFIG_IPV6_MIP6)
193 		case IPPROTO_MH:
194 			offset += ipv6_optlen(exthdr);
195 			if (!onlyproto && pskb_may_pull(skb, nh + offset + 3 - skb->data)) {
196 				struct ip6_mh *mh;
197 
198 				nh = skb_network_header(skb);
199 				mh = (struct ip6_mh *)(nh + offset);
200 				fl6->fl6_mh_type = mh->ip6mh_type;
201 			}
202 			fl6->flowi6_proto = nexthdr;
203 			return;
204 #endif
205 
206 		/* XXX Why are there these headers? */
207 		case IPPROTO_AH:
208 		case IPPROTO_ESP:
209 		case IPPROTO_COMP:
210 		default:
211 			fl6->fl6_ipsec_spi = 0;
212 			fl6->flowi6_proto = nexthdr;
213 			return;
214 		}
215 	}
216 }
217 
218 static inline int xfrm6_garbage_collect(struct dst_ops *ops)
219 {
220 	struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops);
221 
222 	xfrm6_policy_afinfo.garbage_collect(net);
223 	return dst_entries_get_fast(ops) > ops->gc_thresh * 2;
224 }
225 
226 static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
227 			      struct sk_buff *skb, u32 mtu)
228 {
229 	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
230 	struct dst_entry *path = xdst->route;
231 
232 	path->ops->update_pmtu(path, sk, skb, mtu);
233 }
234 
235 static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
236 			   struct sk_buff *skb)
237 {
238 	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
239 	struct dst_entry *path = xdst->route;
240 
241 	path->ops->redirect(path, sk, skb);
242 }
243 
244 static void xfrm6_dst_destroy(struct dst_entry *dst)
245 {
246 	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
247 
248 	if (likely(xdst->u.rt6.rt6i_idev))
249 		in6_dev_put(xdst->u.rt6.rt6i_idev);
250 	dst_destroy_metrics_generic(dst);
251 	xfrm_dst_destroy(xdst);
252 }
253 
254 static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
255 			     int unregister)
256 {
257 	struct xfrm_dst *xdst;
258 
259 	if (!unregister)
260 		return;
261 
262 	xdst = (struct xfrm_dst *)dst;
263 	if (xdst->u.rt6.rt6i_idev->dev == dev) {
264 		struct inet6_dev *loopback_idev =
265 			in6_dev_get(dev_net(dev)->loopback_dev);
266 		BUG_ON(!loopback_idev);
267 
268 		do {
269 			in6_dev_put(xdst->u.rt6.rt6i_idev);
270 			xdst->u.rt6.rt6i_idev = loopback_idev;
271 			in6_dev_hold(loopback_idev);
272 			xdst = (struct xfrm_dst *)xdst->u.dst.child;
273 		} while (xdst->u.dst.xfrm);
274 
275 		__in6_dev_put(loopback_idev);
276 	}
277 
278 	xfrm_dst_ifdown(dst, dev);
279 }
280 
281 static struct dst_ops xfrm6_dst_ops = {
282 	.family =		AF_INET6,
283 	.gc =			xfrm6_garbage_collect,
284 	.update_pmtu =		xfrm6_update_pmtu,
285 	.redirect =		xfrm6_redirect,
286 	.cow_metrics =		dst_cow_metrics_generic,
287 	.destroy =		xfrm6_dst_destroy,
288 	.ifdown =		xfrm6_dst_ifdown,
289 	.local_out =		__ip6_local_out,
290 	.gc_thresh =		32768,
291 };
292 
293 static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
294 	.family =		AF_INET6,
295 	.dst_ops =		&xfrm6_dst_ops,
296 	.dst_lookup =		xfrm6_dst_lookup,
297 	.get_saddr =		xfrm6_get_saddr,
298 	.decode_session =	_decode_session6,
299 	.get_tos =		xfrm6_get_tos,
300 	.init_path =		xfrm6_init_path,
301 	.fill_dst =		xfrm6_fill_dst,
302 	.blackhole_route =	ip6_blackhole_route,
303 };
304 
305 static int __init xfrm6_policy_init(void)
306 {
307 	return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo);
308 }
309 
310 static void xfrm6_policy_fini(void)
311 {
312 	xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
313 }
314 
315 #ifdef CONFIG_SYSCTL
316 static struct ctl_table xfrm6_policy_table[] = {
317 	{
318 		.procname       = "xfrm6_gc_thresh",
319 		.data		= &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
320 		.maxlen		= sizeof(int),
321 		.mode		= 0644,
322 		.proc_handler   = proc_dointvec,
323 	},
324 	{ }
325 };
326 
327 static int __net_init xfrm6_net_init(struct net *net)
328 {
329 	struct ctl_table *table;
330 	struct ctl_table_header *hdr;
331 
332 	table = xfrm6_policy_table;
333 	if (!net_eq(net, &init_net)) {
334 		table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
335 		if (!table)
336 			goto err_alloc;
337 
338 		table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
339 	}
340 
341 	hdr = register_net_sysctl(net, "net/ipv6", table);
342 	if (!hdr)
343 		goto err_reg;
344 
345 	net->ipv6.sysctl.xfrm6_hdr = hdr;
346 	return 0;
347 
348 err_reg:
349 	if (!net_eq(net, &init_net))
350 		kfree(table);
351 err_alloc:
352 	return -ENOMEM;
353 }
354 
355 static void __net_exit xfrm6_net_exit(struct net *net)
356 {
357 	struct ctl_table *table;
358 
359 	if (!net->ipv6.sysctl.xfrm6_hdr)
360 		return;
361 
362 	table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
363 	unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
364 	if (!net_eq(net, &init_net))
365 		kfree(table);
366 }
367 
368 static struct pernet_operations xfrm6_net_ops = {
369 	.init	= xfrm6_net_init,
370 	.exit	= xfrm6_net_exit,
371 };
372 #endif
373 
374 int __init xfrm6_init(void)
375 {
376 	int ret;
377 
378 	dst_entries_init(&xfrm6_dst_ops);
379 
380 	ret = xfrm6_policy_init();
381 	if (ret) {
382 		dst_entries_destroy(&xfrm6_dst_ops);
383 		goto out;
384 	}
385 	ret = xfrm6_state_init();
386 	if (ret)
387 		goto out_policy;
388 
389 	ret = xfrm6_protocol_init();
390 	if (ret)
391 		goto out_state;
392 
393 #ifdef CONFIG_SYSCTL
394 	register_pernet_subsys(&xfrm6_net_ops);
395 #endif
396 out:
397 	return ret;
398 out_state:
399 	xfrm6_state_fini();
400 out_policy:
401 	xfrm6_policy_fini();
402 	goto out;
403 }
404 
405 void xfrm6_fini(void)
406 {
407 #ifdef CONFIG_SYSCTL
408 	unregister_pernet_subsys(&xfrm6_net_ops);
409 #endif
410 	xfrm6_protocol_fini();
411 	xfrm6_policy_fini();
412 	xfrm6_state_fini();
413 	dst_entries_destroy(&xfrm6_dst_ops);
414 }
415