xfrm4_policy.c 8.3 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7
 * xfrm4_policy.c
 *
 * Changes:
 *	Kazunori MIYAZAWA @USAGI
 * 	YOSHIFUJI Hideaki @USAGI
 *		Split up af-specific portion
8
 *
L
Linus Torvalds 已提交
9 10
 */

11 12
#include <linux/err.h>
#include <linux/kernel.h>
H
Herbert Xu 已提交
13
#include <linux/inetdevice.h>
14
#include <linux/if_tunnel.h>
15
#include <net/dst.h>
L
Linus Torvalds 已提交
16 17
#include <net/xfrm.h>
#include <net/ip.h>
18
#include <net/l3mdev.h>
L
Linus Torvalds 已提交
19

20
static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4,
D
David Ahern 已提交
21
					    int tos, int oif,
22 23
					    const xfrm_address_t *saddr,
					    const xfrm_address_t *daddr)
L
Linus Torvalds 已提交
24
{
25
	struct rtable *rt;
26

27 28 29
	memset(fl4, 0, sizeof(*fl4));
	fl4->daddr = daddr->a4;
	fl4->flowi4_tos = tos;
30
	fl4->flowi4_oif = l3mdev_master_ifindex_by_index(net, oif);
31
	if (saddr)
32
		fl4->saddr = saddr->a4;
33

34 35
	fl4->flowi4_flags = FLOWI_FLAG_SKIP_NH_OIF;

36
	rt = __ip_route_output_key(net, fl4);
37 38 39 40
	if (!IS_ERR(rt))
		return &rt->dst;

	return ERR_CAST(rt);
41 42
}

D
David Ahern 已提交
43
static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos, int oif,
44 45 46 47 48
					  const xfrm_address_t *saddr,
					  const xfrm_address_t *daddr)
{
	struct flowi4 fl4;

D
David Ahern 已提交
49
	return __xfrm4_dst_lookup(net, &fl4, tos, oif, saddr, daddr);
50 51
}

D
David Ahern 已提交
52
static int xfrm4_get_saddr(struct net *net, int oif,
A
Alexey Dobriyan 已提交
53
			   xfrm_address_t *saddr, xfrm_address_t *daddr)
54 55
{
	struct dst_entry *dst;
56
	struct flowi4 fl4;
57

D
David Ahern 已提交
58
	dst = __xfrm4_dst_lookup(net, &fl4, 0, oif, NULL, daddr);
59 60 61
	if (IS_ERR(dst))
		return -EHOSTUNREACH;

62
	saddr->a4 = fl4.saddr;
63 64
	dst_release(dst);
	return 0;
65 66
}

67
static int xfrm4_get_tos(const struct flowi *fl)
L
Linus Torvalds 已提交
68
{
69
	return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos; /* Strip ECN bits */
70
}
L
Linus Torvalds 已提交
71

72 73 74 75 76 77
static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
			   int nfheader_len)
{
	return 0;
}

H
Herbert Xu 已提交
78
static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
79
			  const struct flowi *fl)
80 81
{
	struct rtable *rt = (struct rtable *)xdst->route;
82
	const struct flowi4 *fl4 = &fl->u.ip4;
L
Linus Torvalds 已提交
83

84
	xdst->u.rt.rt_iif = fl4->flowi4_iif;
L
Linus Torvalds 已提交
85

86 87
	xdst->u.dst.dev = dev;
	dev_hold(dev);
88

89 90
	/* Sheit... I remember I did this right. Apparently,
	 * it was magically lost, so this code needs audit */
91
	xdst->u.rt.rt_is_input = rt->rt_is_input;
92 93 94 95
	xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
					      RTCF_LOCAL);
	xdst->u.rt.rt_type = rt->rt_type;
	xdst->u.rt.rt_gateway = rt->rt_gateway;
J
Julian Anastasov 已提交
96
	xdst->u.rt.rt_uses_gateway = rt->rt_uses_gateway;
97
	xdst->u.rt.rt_pmtu = rt->rt_pmtu;
D
David Ahern 已提交
98
	xdst->u.rt.rt_table_id = rt->rt_table_id;
99
	INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
L
Linus Torvalds 已提交
100 101 102 103 104

	return 0;
}

static void
105
_decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
L
Linus Torvalds 已提交
106
{
107
	const struct iphdr *iph = ip_hdr(skb);
108
	u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
109
	struct flowi4 *fl4 = &fl->u.ip4;
110 111
	int oif = 0;

112
	if (skb_dst(skb))
113
		oif = skb_dst(skb)->dev->ifindex;
L
Linus Torvalds 已提交
114

115 116
	memset(fl4, 0, sizeof(struct flowi4));
	fl4->flowi4_mark = skb->mark;
117
	fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
P
Peter Kosyh 已提交
118

119
	if (!ip_is_fragment(iph)) {
L
Linus Torvalds 已提交
120 121
		switch (iph->protocol) {
		case IPPROTO_UDP:
122
		case IPPROTO_UDPLITE:
L
Linus Torvalds 已提交
123 124
		case IPPROTO_TCP:
		case IPPROTO_SCTP:
125
		case IPPROTO_DCCP:
126 127
			if (xprth + 4 < skb->data ||
			    pskb_may_pull(skb, xprth + 4 - skb->data)) {
128 129 130 131
				__be16 *ports;

				xprth = skb_network_header(skb) + iph->ihl * 4;
				ports = (__be16 *)xprth;
L
Linus Torvalds 已提交
132

133 134
				fl4->fl4_sport = ports[!!reverse];
				fl4->fl4_dport = ports[!reverse];
L
Linus Torvalds 已提交
135 136 137 138
			}
			break;

		case IPPROTO_ICMP:
139 140
			if (xprth + 2 < skb->data ||
			    pskb_may_pull(skb, xprth + 2 - skb->data)) {
141 142 143 144
				u8 *icmp;

				xprth = skb_network_header(skb) + iph->ihl * 4;
				icmp = xprth;
L
Linus Torvalds 已提交
145

146 147
				fl4->fl4_icmp_type = icmp[0];
				fl4->fl4_icmp_code = icmp[1];
L
Linus Torvalds 已提交
148 149 150 151
			}
			break;

		case IPPROTO_ESP:
152 153
			if (xprth + 4 < skb->data ||
			    pskb_may_pull(skb, xprth + 4 - skb->data)) {
154 155 156 157
				__be32 *ehdr;

				xprth = skb_network_header(skb) + iph->ihl * 4;
				ehdr = (__be32 *)xprth;
L
Linus Torvalds 已提交
158

159
				fl4->fl4_ipsec_spi = ehdr[0];
L
Linus Torvalds 已提交
160 161 162 163
			}
			break;

		case IPPROTO_AH:
164 165
			if (xprth + 8 < skb->data ||
			    pskb_may_pull(skb, xprth + 8 - skb->data)) {
166 167 168 169
				__be32 *ah_hdr;

				xprth = skb_network_header(skb) + iph->ihl * 4;
				ah_hdr = (__be32 *)xprth;
L
Linus Torvalds 已提交
170

171
				fl4->fl4_ipsec_spi = ah_hdr[1];
L
Linus Torvalds 已提交
172 173 174 175
			}
			break;

		case IPPROTO_COMP:
176 177
			if (xprth + 4 < skb->data ||
			    pskb_may_pull(skb, xprth + 4 - skb->data)) {
178 179 180 181
				__be16 *ipcomp_hdr;

				xprth = skb_network_header(skb) + iph->ihl * 4;
				ipcomp_hdr = (__be16 *)xprth;
L
Linus Torvalds 已提交
182

183
				fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
L
Linus Torvalds 已提交
184 185
			}
			break;
186 187

		case IPPROTO_GRE:
188 189
			if (xprth + 12 < skb->data ||
			    pskb_may_pull(skb, xprth + 12 - skb->data)) {
190 191 192 193 194 195
				__be16 *greflags;
				__be32 *gre_hdr;

				xprth = skb_network_header(skb) + iph->ihl * 4;
				greflags = (__be16 *)xprth;
				gre_hdr = (__be32 *)xprth;
196 197 198 199

				if (greflags[0] & GRE_KEY) {
					if (greflags[0] & GRE_CSUM)
						gre_hdr++;
200
					fl4->fl4_gre_key = gre_hdr[1];
201 202 203 204
				}
			}
			break;

L
Linus Torvalds 已提交
205
		default:
206
			fl4->fl4_ipsec_spi = 0;
L
Linus Torvalds 已提交
207
			break;
208
		}
L
Linus Torvalds 已提交
209
	}
210 211 212 213
	fl4->flowi4_proto = iph->protocol;
	fl4->daddr = reverse ? iph->saddr : iph->daddr;
	fl4->saddr = reverse ? iph->daddr : iph->saddr;
	fl4->flowi4_tos = iph->tos;
L
Linus Torvalds 已提交
214 215
}

216 217
static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
			      struct sk_buff *skb, u32 mtu)
L
Linus Torvalds 已提交
218 219 220 221
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
	struct dst_entry *path = xdst->route;

222
	path->ops->update_pmtu(path, sk, skb, mtu);
L
Linus Torvalds 已提交
223 224
}

225 226
static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
			   struct sk_buff *skb)
227 228 229 230
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
	struct dst_entry *path = xdst->route;

231
	path->ops->redirect(path, sk, skb);
232 233
}

H
Herbert Xu 已提交
234 235 236 237
static void xfrm4_dst_destroy(struct dst_entry *dst)
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;

238 239
	dst_destroy_metrics_generic(dst);

H
Herbert Xu 已提交
240 241 242 243 244 245 246 247 248 249 250 251
	xfrm_dst_destroy(xdst);
}

static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
			     int unregister)
{
	if (!unregister)
		return;

	xfrm_dst_ifdown(dst, dev);
}

252
static struct dst_ops xfrm4_dst_ops_template = {
L
Linus Torvalds 已提交
253 254
	.family =		AF_INET,
	.update_pmtu =		xfrm4_update_pmtu,
255
	.redirect =		xfrm4_redirect,
256
	.cow_metrics =		dst_cow_metrics_generic,
H
Herbert Xu 已提交
257 258
	.destroy =		xfrm4_dst_destroy,
	.ifdown =		xfrm4_dst_ifdown,
259
	.local_out =		__ip_local_out,
260
	.gc_thresh =		32768,
L
Linus Torvalds 已提交
261 262
};

263
static const struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
264
	.dst_ops =		&xfrm4_dst_ops_template,
L
Linus Torvalds 已提交
265
	.dst_lookup =		xfrm4_dst_lookup,
266
	.get_saddr =		xfrm4_get_saddr,
L
Linus Torvalds 已提交
267
	.decode_session =	_decode_session4,
268
	.get_tos =		xfrm4_get_tos,
269
	.init_path =		xfrm4_init_path,
270
	.fill_dst =		xfrm4_fill_dst,
271
	.blackhole_route =	ipv4_blackhole_route,
L
Linus Torvalds 已提交
272 273
};

274
#ifdef CONFIG_SYSCTL
275 276 277
static struct ctl_table xfrm4_policy_table[] = {
	{
		.procname       = "xfrm4_gc_thresh",
278
		.data           = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
279 280 281 282 283 284 285
		.maxlen         = sizeof(int),
		.mode           = 0644,
		.proc_handler   = proc_dointvec,
	},
	{ }
};

286
static __net_init int xfrm4_net_sysctl_init(struct net *net)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
{
	struct ctl_table *table;
	struct ctl_table_header *hdr;

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

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

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

	net->ipv4.xfrm4_hdr = hdr;
	return 0;

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

314
static __net_exit void xfrm4_net_sysctl_exit(struct net *net)
315 316 317
{
	struct ctl_table *table;

318
	if (!net->ipv4.xfrm4_hdr)
319 320 321 322 323 324 325
		return;

	table = net->ipv4.xfrm4_hdr->ctl_table_arg;
	unregister_net_sysctl_table(net->ipv4.xfrm4_hdr);
	if (!net_eq(net, &init_net))
		kfree(table);
}
326
#else /* CONFIG_SYSCTL */
327
static inline int xfrm4_net_sysctl_init(struct net *net)
328 329 330 331
{
	return 0;
}

332
static inline void xfrm4_net_sysctl_exit(struct net *net)
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
{
}
#endif

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

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

	ret = xfrm4_net_sysctl_init(net);
	if (ret)
		dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);

	return ret;
}

static void __net_exit xfrm4_net_exit(struct net *net)
{
	xfrm4_net_sysctl_exit(net);
	dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
}
359 360 361 362 363

static struct pernet_operations __net_initdata xfrm4_net_ops = {
	.init	= xfrm4_net_init,
	.exit	= xfrm4_net_exit,
};
364

L
Linus Torvalds 已提交
365 366
static void __init xfrm4_policy_init(void)
{
367
	xfrm_policy_register_afinfo(&xfrm4_policy_afinfo, AF_INET);
L
Linus Torvalds 已提交
368 369
}

370
void __init xfrm4_init(void)
L
Linus Torvalds 已提交
371
{
372 373
	xfrm4_state_init();
	xfrm4_policy_init();
374
	xfrm4_protocol_init();
375
	register_pernet_subsys(&xfrm4_net_ops);
L
Linus Torvalds 已提交
376 377
}