xfrm4_policy.c 6.6 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
	struct flowi fl = {
26 27
		.fl4_dst = daddr->a4,
		.fl4_tos = tos,
28
	};
29 30 31
	struct dst_entry *dst;
	struct rtable *rt;
	int err;
32

33 34 35
	if (saddr)
		fl.fl4_src = saddr->a4;

36
	err = __ip_route_output_key(net, &rt, &fl);
37
	dst = &rt->dst;
38 39 40 41 42
	if (err)
		dst = ERR_PTR(err);
	return dst;
}

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

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

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

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

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

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

H
Herbert Xu 已提交
75
	xdst->u.rt.fl = *fl;
L
Linus Torvalds 已提交
76

77 78
	xdst->u.dst.dev = dev;
	dev_hold(dev);
79

80 81 82
	xdst->u.rt.peer = rt->peer;
	if (rt->peer)
		atomic_inc(&rt->peer->refcnt);
83

84 85 86 87 88 89 90 91 92
	/* 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 已提交
93 94 95 96 97

	return 0;
}

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

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

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

117 118
				fl->fl_ip_sport = ports[!!reverse];
				fl->fl_ip_dport = ports[!reverse];
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132
			}
			break;

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

				fl->fl_icmp_type = icmp[0];
				fl->fl_icmp_code = icmp[1];
			}
			break;

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

				fl->fl_ipsec_spi = ehdr[0];
			}
			break;

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

				fl->fl_ipsec_spi = ah_hdr[1];
			}
			break;

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

A
Alexey Dobriyan 已提交
151
				fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
L
Linus Torvalds 已提交
152 153
			}
			break;
154 155 156 157 158 159 160 161 162 163 164 165 166 167

		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++;
					fl->fl_gre_key = gre_hdr[1];
				}
			}
			break;

L
Linus Torvalds 已提交
168 169 170
		default:
			fl->fl_ipsec_spi = 0;
			break;
171
		}
L
Linus Torvalds 已提交
172 173
	}
	fl->proto = iph->protocol;
174 175
	fl->fl4_dst = reverse ? iph->saddr : iph->daddr;
	fl->fl4_src = reverse ? iph->daddr : iph->saddr;
176
	fl->fl4_tos = iph->tos;
L
Linus Torvalds 已提交
177 178
}

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

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

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 已提交
195 196 197 198
static void xfrm4_dst_destroy(struct dst_entry *dst)
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;

199 200
	dst_destroy_metrics_generic(dst);

201
	if (likely(xdst->u.rt.peer))
202
		inet_putpeer(xdst->u.rt.peer);
203

H
Herbert Xu 已提交
204 205 206 207 208 209 210 211 212 213 214 215
	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 已提交
216 217
static struct dst_ops xfrm4_dst_ops = {
	.family =		AF_INET,
218
	.protocol =		cpu_to_be16(ETH_P_IP),
L
Linus Torvalds 已提交
219 220
	.gc =			xfrm4_garbage_collect,
	.update_pmtu =		xfrm4_update_pmtu,
221
	.cow_metrics =		dst_cow_metrics_generic,
H
Herbert Xu 已提交
222 223
	.destroy =		xfrm4_dst_destroy,
	.ifdown =		xfrm4_dst_ifdown,
224
	.local_out =		__ip_local_out,
L
Linus Torvalds 已提交
225 226 227 228 229 230 231
	.gc_thresh =		1024,
};

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

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

static struct ctl_table_header *sysctl_hdr;
253
#endif
254

L
Linus Torvalds 已提交
255 256 257 258 259 260 261
static void __init xfrm4_policy_init(void)
{
	xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
}

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

269
void __init xfrm4_init(int rt_max_size)
L
Linus Torvalds 已提交
270
{
271 272 273 274 275 276 277 278 279 280 281
	/*
	 * 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;
282
	dst_entries_init(&xfrm4_dst_ops);
283 284 285

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