xfrm6_policy.c 6.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6
/*
 * xfrm6_policy.c: based on xfrm4_policy.c
 *
 * Authors:
 *	Mitsuru KANDA @USAGI
7 8 9 10 11
 *	Kazunori MIYAZAWA @USAGI
 *	Kunihiro Ishiguro <kunihiro@ipinfusion.com>
 *		IPv6 support
 *	YOSHIFUJI Hideaki
 *		Split up af-specific portion
12
 *
L
Linus Torvalds 已提交
13 14
 */

15 16
#include <linux/err.h>
#include <linux/kernel.h>
H
Herbert Xu 已提交
17 18
#include <linux/netdevice.h>
#include <net/addrconf.h>
19
#include <net/dst.h>
L
Linus Torvalds 已提交
20 21 22 23
#include <net/xfrm.h>
#include <net/ip.h>
#include <net/ipv6.h>
#include <net/ip6_route.h>
24
#include <net/l3mdev.h>
L
Linus Torvalds 已提交
25

D
David Ahern 已提交
26
static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,
27
					  const xfrm_address_t *saddr,
28 29
					  const xfrm_address_t *daddr,
					  u32 mark)
L
Linus Torvalds 已提交
30
{
31
	struct flowi6 fl6;
32 33 34
	struct dst_entry *dst;
	int err;

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 45

	err = dst->error;
	if (dst->error) {
46
		dst_release(dst);
47 48 49 50
		dst = ERR_PTR(err);
	}

	return dst;
L
Linus Torvalds 已提交
51 52
}

D
David Ahern 已提交
53
static int xfrm6_get_saddr(struct net *net, int oif,
54 55
			   xfrm_address_t *saddr, xfrm_address_t *daddr,
			   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 62 63
	if (IS_ERR(dst))
		return -EHOSTUNREACH;

64
	dev = ip6_dst_idev(dst)->dev;
J
Jiri Benc 已提交
65
	ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
66 67
	dst_release(dst);
	return 0;
68 69
}

H
Herbert Xu 已提交
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;
L
Linus Torvalds 已提交
74

75
	xdst->u.dst.dev = dev;
76
	dev_hold_track(dev, &xdst->u.dst.dev_tracker, GFP_ATOMIC);
L
Linus Torvalds 已提交
77

78
	xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
79
	if (!xdst->u.rt6.rt6i_idev) {
80
		dev_put_track(dev, &xdst->u.dst.dev_tracker);
81
		return -ENODEV;
82
	}
L
Linus Torvalds 已提交
83

84 85 86 87
	/* Sheit... I remember I did this right. Apparently,
	 * it was magically lost, so this code needs audit */
	xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST |
						   RTF_LOCAL);
88
	xdst->route_cookie = rt6_get_cookie(rt);
89 90 91
	xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway;
	xdst->u.rt6.rt6i_dst = rt->rt6i_dst;
	xdst->u.rt6.rt6i_src = rt->rt6i_src;
92 93
	INIT_LIST_HEAD(&xdst->u.rt6.rt6i_uncached);
	rt6_uncached_list_add(&xdst->u.rt6);
L
Linus Torvalds 已提交
94 95 96 97

	return 0;
}

98
static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk,
99 100
			      struct sk_buff *skb, u32 mtu,
			      bool confirm_neigh)
L
Linus Torvalds 已提交
101 102 103 104
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
	struct dst_entry *path = xdst->route;

105
	path->ops->update_pmtu(path, sk, skb, mtu, confirm_neigh);
L
Linus Torvalds 已提交
106 107
}

108 109
static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk,
			   struct sk_buff *skb)
110 111 112 113
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
	struct dst_entry *path = xdst->route;

114
	path->ops->redirect(path, sk, skb);
115 116
}

H
Herbert Xu 已提交
117 118 119 120 121 122
static void xfrm6_dst_destroy(struct dst_entry *dst)
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;

	if (likely(xdst->u.rt6.rt6i_idev))
		in6_dev_put(xdst->u.rt6.rt6i_idev);
123
	dst_destroy_metrics_generic(dst);
124 125
	if (xdst->u.rt6.rt6i_uncached_list)
		rt6_uncached_list_del(&xdst->u.rt6);
H
Herbert Xu 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138
	xfrm_dst_destroy(xdst);
}

static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
			     int unregister)
{
	struct xfrm_dst *xdst;

	if (!unregister)
		return;

	xdst = (struct xfrm_dst *)dst;
	if (xdst->u.rt6.rt6i_idev->dev == dev) {
139
		struct inet6_dev *loopback_idev =
140
			in6_dev_get(dev_net(dev)->loopback_dev);
H
Herbert Xu 已提交
141 142 143 144 145

		do {
			in6_dev_put(xdst->u.rt6.rt6i_idev);
			xdst->u.rt6.rt6i_idev = loopback_idev;
			in6_dev_hold(loopback_idev);
146
			xdst = (struct xfrm_dst *)xfrm_dst_child(&xdst->u.dst);
H
Herbert Xu 已提交
147 148 149 150 151 152 153 154
		} while (xdst->u.dst.xfrm);

		__in6_dev_put(loopback_idev);
	}

	xfrm_dst_ifdown(dst, dev);
}

155
static struct dst_ops xfrm6_dst_ops_template = {
L
Linus Torvalds 已提交
156 157
	.family =		AF_INET6,
	.update_pmtu =		xfrm6_update_pmtu,
158
	.redirect =		xfrm6_redirect,
159
	.cow_metrics =		dst_cow_metrics_generic,
H
Herbert Xu 已提交
160 161
	.destroy =		xfrm6_dst_destroy,
	.ifdown =		xfrm6_dst_ifdown,
162
	.local_out =		__ip6_local_out,
163
	.gc_thresh =		32768,
L
Linus Torvalds 已提交
164 165
};

166
static const struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
167
	.dst_ops =		&xfrm6_dst_ops_template,
L
Linus Torvalds 已提交
168
	.dst_lookup =		xfrm6_dst_lookup,
169
	.get_saddr =		xfrm6_get_saddr,
170
	.fill_dst =		xfrm6_fill_dst,
171
	.blackhole_route =	ip6_blackhole_route,
L
Linus Torvalds 已提交
172 173
};

174
static int __init xfrm6_policy_init(void)
L
Linus Torvalds 已提交
175
{
176
	return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo, AF_INET6);
L
Linus Torvalds 已提交
177 178 179 180 181 182 183
}

static void xfrm6_policy_fini(void)
{
	xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
}

184
#ifdef CONFIG_SYSCTL
185 186 187
static struct ctl_table xfrm6_policy_table[] = {
	{
		.procname       = "xfrm6_gc_thresh",
188 189 190
		.data		= &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
		.maxlen		= sizeof(int),
		.mode		= 0644,
191 192 193 194 195
		.proc_handler   = proc_dointvec,
	},
	{ }
};

196
static int __net_init xfrm6_net_sysctl_init(struct net *net)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
{
	struct ctl_table *table;
	struct ctl_table_header *hdr;

	table = xfrm6_policy_table;
	if (!net_eq(net, &init_net)) {
		table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL);
		if (!table)
			goto err_alloc;

		table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh;
	}

	hdr = register_net_sysctl(net, "net/ipv6", table);
	if (!hdr)
		goto err_reg;

	net->ipv6.sysctl.xfrm6_hdr = hdr;
	return 0;

err_reg:
	if (!net_eq(net, &init_net))
		kfree(table);
err_alloc:
	return -ENOMEM;
}

224
static void __net_exit xfrm6_net_sysctl_exit(struct net *net)
225 226 227
{
	struct ctl_table *table;

228
	if (!net->ipv6.sysctl.xfrm6_hdr)
229 230 231 232 233 234 235
		return;

	table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
	unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr);
	if (!net_eq(net, &init_net))
		kfree(table);
}
236
#else /* CONFIG_SYSCTL */
237
static inline int xfrm6_net_sysctl_init(struct net *net)
238 239 240 241
{
	return 0;
}

242
static inline void xfrm6_net_sysctl_exit(struct net *net)
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
{
}
#endif

static int __net_init xfrm6_net_init(struct net *net)
{
	int ret;

	memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template,
	       sizeof(xfrm6_dst_ops_template));
	ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops);
	if (ret)
		return ret;

	ret = xfrm6_net_sysctl_init(net);
	if (ret)
		dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);

	return ret;
}

static void __net_exit xfrm6_net_exit(struct net *net)
{
	xfrm6_net_sysctl_exit(net);
	dst_entries_destroy(&net->xfrm.xfrm6_dst_ops);
}
269 270 271 272 273

static struct pernet_operations xfrm6_net_ops = {
	.init	= xfrm6_net_init,
	.exit	= xfrm6_net_exit,
};
274

275
int __init xfrm6_init(void)
L
Linus Torvalds 已提交
276
{
277
	int ret;
278

279
	ret = xfrm6_policy_init();
280
	if (ret)
281 282 283 284 285
		goto out;
	ret = xfrm6_state_init();
	if (ret)
		goto out_policy;

286 287 288 289
	ret = xfrm6_protocol_init();
	if (ret)
		goto out_state;

290
	register_pernet_subsys(&xfrm6_net_ops);
291 292
out:
	return ret;
293 294
out_state:
	xfrm6_state_fini();
295 296 297
out_policy:
	xfrm6_policy_fini();
	goto out;
L
Linus Torvalds 已提交
298 299 300 301
}

void xfrm6_fini(void)
{
302
	unregister_pernet_subsys(&xfrm6_net_ops);
303
	xfrm6_protocol_fini();
L
Linus Torvalds 已提交
304 305 306
	xfrm6_policy_fini();
	xfrm6_state_fini();
}