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