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