xfrm4_policy.c 6.8 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 18 19 20
#include <net/xfrm.h>
#include <net/ip.h>

static struct xfrm_policy_afinfo xfrm4_policy_afinfo;

21
static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos,
22 23
					  const xfrm_address_t *saddr,
					  const xfrm_address_t *daddr)
L
Linus Torvalds 已提交
24
{
25 26 27
	struct flowi4 fl4 = {
		.daddr = daddr->a4,
		.flowi4_tos = tos,
28
	};
29
	struct rtable *rt;
30

31
	if (saddr)
32
		fl4.saddr = saddr->a4;
33

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

	return ERR_CAST(rt);
39 40
}

A
Alexey Dobriyan 已提交
41 42
static int xfrm4_get_saddr(struct net *net,
			   xfrm_address_t *saddr, xfrm_address_t *daddr)
43 44 45 46
{
	struct dst_entry *dst;
	struct rtable *rt;

A
Alexey Dobriyan 已提交
47
	dst = xfrm4_dst_lookup(net, 0, NULL, daddr);
48 49 50 51 52 53 54
	if (IS_ERR(dst))
		return -EHOSTUNREACH;

	rt = (struct rtable *)dst;
	saddr->a4 = rt->rt_src;
	dst_release(dst);
	return 0;
55 56
}

57
static int xfrm4_get_tos(const struct flowi *fl)
L
Linus Torvalds 已提交
58
{
59
	return IPTOS_RT_MASK & fl->fl4_tos; /* Strip ECN bits */
60
}
L
Linus Torvalds 已提交
61

62 63 64 65 66 67
static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
			   int nfheader_len)
{
	return 0;
}

H
Herbert Xu 已提交
68
static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
69
			  const struct flowi *fl)
70 71
{
	struct rtable *rt = (struct rtable *)xdst->route;
L
Linus Torvalds 已提交
72

73 74 75
	rt->rt_key_dst = fl->fl4_dst;
	rt->rt_key_src = fl->fl4_src;
	rt->rt_tos = fl->fl4_tos;
76 77 78
	rt->rt_iif = fl->flowi_iif;
	rt->rt_oif = fl->flowi_oif;
	rt->rt_mark = fl->flowi_mark;
L
Linus Torvalds 已提交
79

80 81
	xdst->u.dst.dev = dev;
	dev_hold(dev);
82

83 84 85
	xdst->u.rt.peer = rt->peer;
	if (rt->peer)
		atomic_inc(&rt->peer->refcnt);
86

87 88 89 90 91 92 93 94 95
	/* Sheit... I remember I did this right. Apparently,
	 * it was magically lost, so this code needs audit */
	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_src = rt->rt_src;
	xdst->u.rt.rt_dst = rt->rt_dst;
	xdst->u.rt.rt_gateway = rt->rt_gateway;
	xdst->u.rt.rt_spec_dst = rt->rt_spec_dst;
L
Linus Torvalds 已提交
96 97 98 99 100

	return 0;
}

static void
101
_decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
L
Linus Torvalds 已提交
102
{
103
	struct iphdr *iph = ip_hdr(skb);
104
	u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
L
Linus Torvalds 已提交
105 106

	memset(fl, 0, sizeof(struct flowi));
107
	fl->flowi_mark = skb->mark;
P
Peter Kosyh 已提交
108

L
Linus Torvalds 已提交
109 110 111
	if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
		switch (iph->protocol) {
		case IPPROTO_UDP:
112
		case IPPROTO_UDPLITE:
L
Linus Torvalds 已提交
113 114
		case IPPROTO_TCP:
		case IPPROTO_SCTP:
115
		case IPPROTO_DCCP:
116 117
			if (xprth + 4 < skb->data ||
			    pskb_may_pull(skb, xprth + 4 - skb->data)) {
A
Al Viro 已提交
118
				__be16 *ports = (__be16 *)xprth;
L
Linus Torvalds 已提交
119

120 121
				fl->fl4_sport = ports[!!reverse];
				fl->fl4_dport = ports[!reverse];
L
Linus Torvalds 已提交
122 123 124 125 126 127 128
			}
			break;

		case IPPROTO_ICMP:
			if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
				u8 *icmp = xprth;

129 130
				fl->fl4_icmp_type = icmp[0];
				fl->fl4_icmp_code = icmp[1];
L
Linus Torvalds 已提交
131 132 133 134 135
			}
			break;

		case IPPROTO_ESP:
			if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
A
Al Viro 已提交
136
				__be32 *ehdr = (__be32 *)xprth;
L
Linus Torvalds 已提交
137

138
				fl->fl4_ipsec_spi = ehdr[0];
L
Linus Torvalds 已提交
139 140 141 142 143
			}
			break;

		case IPPROTO_AH:
			if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
A
Al Viro 已提交
144
				__be32 *ah_hdr = (__be32*)xprth;
L
Linus Torvalds 已提交
145

146
				fl->fl4_ipsec_spi = ah_hdr[1];
L
Linus Torvalds 已提交
147 148 149 150 151
			}
			break;

		case IPPROTO_COMP:
			if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
A
Al Viro 已提交
152
				__be16 *ipcomp_hdr = (__be16 *)xprth;
L
Linus Torvalds 已提交
153

154
				fl->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
L
Linus Torvalds 已提交
155 156
			}
			break;
157 158 159 160 161 162 163 164 165

		case IPPROTO_GRE:
			if (pskb_may_pull(skb, xprth + 12 - skb->data)) {
				__be16 *greflags = (__be16 *)xprth;
				__be32 *gre_hdr = (__be32 *)xprth;

				if (greflags[0] & GRE_KEY) {
					if (greflags[0] & GRE_CSUM)
						gre_hdr++;
166
					fl->fl4_gre_key = gre_hdr[1];
167 168 169 170
				}
			}
			break;

L
Linus Torvalds 已提交
171
		default:
172
			fl->fl4_ipsec_spi = 0;
L
Linus Torvalds 已提交
173
			break;
174
		}
L
Linus Torvalds 已提交
175
	}
176
	fl->flowi_proto = iph->protocol;
177 178
	fl->fl4_dst = reverse ? iph->saddr : iph->daddr;
	fl->fl4_src = reverse ? iph->daddr : iph->saddr;
179
	fl->fl4_tos = iph->tos;
L
Linus Torvalds 已提交
180 181
}

182
static inline int xfrm4_garbage_collect(struct dst_ops *ops)
L
Linus Torvalds 已提交
183
{
184 185 186
	struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);

	xfrm4_policy_afinfo.garbage_collect(net);
187
	return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
L
Linus Torvalds 已提交
188 189 190 191 192 193 194 195 196 197
}

static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
	struct dst_entry *path = xdst->route;

	path->ops->update_pmtu(path, mtu);
}

H
Herbert Xu 已提交
198 199 200 201
static void xfrm4_dst_destroy(struct dst_entry *dst)
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;

202 203
	dst_destroy_metrics_generic(dst);

204
	if (likely(xdst->u.rt.peer))
205
		inet_putpeer(xdst->u.rt.peer);
206

H
Herbert Xu 已提交
207 208 209 210 211 212 213 214 215 216 217 218
	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);
}

L
Linus Torvalds 已提交
219 220
static struct dst_ops xfrm4_dst_ops = {
	.family =		AF_INET,
221
	.protocol =		cpu_to_be16(ETH_P_IP),
L
Linus Torvalds 已提交
222 223
	.gc =			xfrm4_garbage_collect,
	.update_pmtu =		xfrm4_update_pmtu,
224
	.cow_metrics =		dst_cow_metrics_generic,
H
Herbert Xu 已提交
225 226
	.destroy =		xfrm4_dst_destroy,
	.ifdown =		xfrm4_dst_ifdown,
227
	.local_out =		__ip_local_out,
L
Linus Torvalds 已提交
228 229 230 231 232 233 234
	.gc_thresh =		1024,
};

static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
	.family = 		AF_INET,
	.dst_ops =		&xfrm4_dst_ops,
	.dst_lookup =		xfrm4_dst_lookup,
235
	.get_saddr =		xfrm4_get_saddr,
L
Linus Torvalds 已提交
236
	.decode_session =	_decode_session4,
237
	.get_tos =		xfrm4_get_tos,
238
	.init_path =		xfrm4_init_path,
239
	.fill_dst =		xfrm4_fill_dst,
240
	.blackhole_route =	ipv4_blackhole_route,
L
Linus Torvalds 已提交
241 242
};

243
#ifdef CONFIG_SYSCTL
244 245 246
static struct ctl_table xfrm4_policy_table[] = {
	{
		.procname       = "xfrm4_gc_thresh",
247
		.data           = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
248 249 250 251 252 253 254 255
		.maxlen         = sizeof(int),
		.mode           = 0644,
		.proc_handler   = proc_dointvec,
	},
	{ }
};

static struct ctl_table_header *sysctl_hdr;
256
#endif
257

L
Linus Torvalds 已提交
258 259 260 261 262 263 264
static void __init xfrm4_policy_init(void)
{
	xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
}

static void __exit xfrm4_policy_fini(void)
{
265
#ifdef CONFIG_SYSCTL
266 267
	if (sysctl_hdr)
		unregister_net_sysctl_table(sysctl_hdr);
268
#endif
L
Linus Torvalds 已提交
269 270 271
	xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
}

272
void __init xfrm4_init(int rt_max_size)
L
Linus Torvalds 已提交
273
{
274 275 276 277 278 279 280 281 282 283 284
	/*
	 * Select a default value for the gc_thresh based on the main route
	 * table hash size.  It seems to me the worst case scenario is when
	 * we have ipsec operating in transport mode, in which we create a
	 * dst_entry per socket.  The xfrm gc algorithm starts trying to remove
	 * entries at gc_thresh, and prevents new allocations as 2*gc_thresh
	 * so lets set an initial xfrm gc_thresh value at the rt_max_size/2.
	 * That will let us store an ipsec connection per route table entry,
	 * and start cleaning when were 1/2 full
	 */
	xfrm4_dst_ops.gc_thresh = rt_max_size/2;
285
	dst_entries_init(&xfrm4_dst_ops);
286 287 288

	xfrm4_state_init();
	xfrm4_policy_init();
289
#ifdef CONFIG_SYSCTL
290 291
	sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv4_ctl_path,
						xfrm4_policy_table);
292
#endif
L
Linus Torvalds 已提交
293 294
}