xfrm4_policy.c 7.1 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 22 23 24
static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4,
					    int tos,
					    const xfrm_address_t *saddr,
					    const xfrm_address_t *daddr)
L
Linus Torvalds 已提交
25
{
26
	struct rtable *rt;
27

28 29 30
	memset(fl4, 0, sizeof(*fl4));
	fl4->daddr = daddr->a4;
	fl4->flowi4_tos = tos;
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
}

41 42 43 44 45 46 47 48 49
static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos,
					  const xfrm_address_t *saddr,
					  const xfrm_address_t *daddr)
{
	struct flowi4 fl4;

	return __xfrm4_dst_lookup(net, &fl4, tos, saddr, daddr);
}

A
Alexey Dobriyan 已提交
50 51
static int xfrm4_get_saddr(struct net *net,
			   xfrm_address_t *saddr, xfrm_address_t *daddr)
52 53
{
	struct dst_entry *dst;
54
	struct flowi4 fl4;
55

56
	dst = __xfrm4_dst_lookup(net, &fl4, 0, NULL, daddr);
57 58 59
	if (IS_ERR(dst))
		return -EHOSTUNREACH;

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

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

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

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

82 83 84 85 86 87 88
	xdst->u.rt.rt_key_dst = fl4->daddr;
	xdst->u.rt.rt_key_src = fl4->saddr;
	xdst->u.rt.rt_key_tos = fl4->flowi4_tos;
	xdst->u.rt.rt_route_iif = fl4->flowi4_iif;
	xdst->u.rt.rt_iif = fl4->flowi4_iif;
	xdst->u.rt.rt_oif = fl4->flowi4_oif;
	xdst->u.rt.rt_mark = fl4->flowi4_mark;
L
Linus Torvalds 已提交
89

90 91
	xdst->u.dst.dev = dev;
	dev_hold(dev);
92

93
	rt_transfer_peer(&xdst->u.rt, rt);
94

95 96 97 98 99 100 101 102
	/* 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;
L
Linus Torvalds 已提交
103 104 105 106 107

	return 0;
}

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

114 115
	memset(fl4, 0, sizeof(struct flowi4));
	fl4->flowi4_mark = skb->mark;
P
Peter Kosyh 已提交
116

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

128 129
				fl4->fl4_sport = ports[!!reverse];
				fl4->fl4_dport = ports[!reverse];
L
Linus Torvalds 已提交
130 131 132 133 134 135 136
			}
			break;

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

137 138
				fl4->fl4_icmp_type = icmp[0];
				fl4->fl4_icmp_code = icmp[1];
L
Linus Torvalds 已提交
139 140 141 142 143
			}
			break;

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

146
				fl4->fl4_ipsec_spi = ehdr[0];
L
Linus Torvalds 已提交
147 148 149 150 151
			}
			break;

		case IPPROTO_AH:
			if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
D
Daniel Baluta 已提交
152
				__be32 *ah_hdr = (__be32 *)xprth;
L
Linus Torvalds 已提交
153

154
				fl4->fl4_ipsec_spi = ah_hdr[1];
L
Linus Torvalds 已提交
155 156 157 158 159
			}
			break;

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

162
				fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
L
Linus Torvalds 已提交
163 164
			}
			break;
165 166 167 168 169 170 171 172 173

		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++;
174
					fl4->fl4_gre_key = gre_hdr[1];
175 176 177 178
				}
			}
			break;

L
Linus Torvalds 已提交
179
		default:
180
			fl4->fl4_ipsec_spi = 0;
L
Linus Torvalds 已提交
181
			break;
182
		}
L
Linus Torvalds 已提交
183
	}
184 185 186 187
	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 已提交
188 189
}

190
static inline int xfrm4_garbage_collect(struct dst_ops *ops)
L
Linus Torvalds 已提交
191
{
192 193 194
	struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);

	xfrm4_policy_afinfo.garbage_collect(net);
195
	return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205
}

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 已提交
206 207 208 209
static void xfrm4_dst_destroy(struct dst_entry *dst)
{
	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;

210 211
	dst_destroy_metrics_generic(dst);

212 213 214 215
	if (rt_has_peer(&xdst->u.rt)) {
		struct inet_peer *peer = rt_peer_ptr(&xdst->u.rt);
		inet_putpeer(peer);
	}
216

H
Herbert Xu 已提交
217 218 219 220 221 222 223 224 225 226 227 228
	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 已提交
229 230
static struct dst_ops xfrm4_dst_ops = {
	.family =		AF_INET,
231
	.protocol =		cpu_to_be16(ETH_P_IP),
L
Linus Torvalds 已提交
232 233
	.gc =			xfrm4_garbage_collect,
	.update_pmtu =		xfrm4_update_pmtu,
234
	.cow_metrics =		dst_cow_metrics_generic,
H
Herbert Xu 已提交
235 236
	.destroy =		xfrm4_dst_destroy,
	.ifdown =		xfrm4_dst_ifdown,
237
	.local_out =		__ip_local_out,
L
Linus Torvalds 已提交
238 239 240 241 242 243 244
	.gc_thresh =		1024,
};

static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
	.family = 		AF_INET,
	.dst_ops =		&xfrm4_dst_ops,
	.dst_lookup =		xfrm4_dst_lookup,
245
	.get_saddr =		xfrm4_get_saddr,
L
Linus Torvalds 已提交
246
	.decode_session =	_decode_session4,
247
	.get_tos =		xfrm4_get_tos,
248
	.init_path =		xfrm4_init_path,
249
	.fill_dst =		xfrm4_fill_dst,
250
	.blackhole_route =	ipv4_blackhole_route,
L
Linus Torvalds 已提交
251 252
};

253
#ifdef CONFIG_SYSCTL
254 255 256
static struct ctl_table xfrm4_policy_table[] = {
	{
		.procname       = "xfrm4_gc_thresh",
257
		.data           = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
258 259 260 261 262 263 264 265
		.maxlen         = sizeof(int),
		.mode           = 0644,
		.proc_handler   = proc_dointvec,
	},
	{ }
};

static struct ctl_table_header *sysctl_hdr;
266
#endif
267

L
Linus Torvalds 已提交
268 269 270 271 272 273 274
static void __init xfrm4_policy_init(void)
{
	xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
}

static void __exit xfrm4_policy_fini(void)
{
275
#ifdef CONFIG_SYSCTL
276 277
	if (sysctl_hdr)
		unregister_net_sysctl_table(sysctl_hdr);
278
#endif
L
Linus Torvalds 已提交
279 280 281
	xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
}

282
void __init xfrm4_init(int rt_max_size)
L
Linus Torvalds 已提交
283
{
284 285 286 287 288 289 290 291 292 293 294
	/*
	 * 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;
295
	dst_entries_init(&xfrm4_dst_ops);
296 297 298

	xfrm4_state_init();
	xfrm4_policy_init();
299
#ifdef CONFIG_SYSCTL
300 301
	sysctl_hdr = register_net_sysctl(&init_net, "net/ipv4",
					 xfrm4_policy_table);
302
#endif
L
Linus Torvalds 已提交
303 304
}