xref: /linux/net/ipv4/xfrm4_policy.c (revision 9cce96df5b76691712dba22e83ff5efe900361e1)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * xfrm4_policy.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Changes:
51da177e4SLinus Torvalds  *	Kazunori MIYAZAWA @USAGI
61da177e4SLinus Torvalds  * 	YOSHIFUJI Hideaki @USAGI
71da177e4SLinus Torvalds  *		Split up af-specific portion
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
1166cdb3caSHerbert Xu #include <linux/err.h>
1266cdb3caSHerbert Xu #include <linux/kernel.h>
13aabc9761SHerbert Xu #include <linux/inetdevice.h>
14cc9ff19dSTimo Teräs #include <linux/if_tunnel.h>
1545ff5a3fSHerbert Xu #include <net/dst.h>
161da177e4SLinus Torvalds #include <net/xfrm.h>
171da177e4SLinus Torvalds #include <net/ip.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
201da177e4SLinus Torvalds 
21c5b3cf46SAlexey Dobriyan static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos,
225e6b930fSDavid S. Miller 					  const xfrm_address_t *saddr,
235e6b930fSDavid S. Miller 					  const xfrm_address_t *daddr)
241da177e4SLinus Torvalds {
259d6ec938SDavid S. Miller 	struct flowi4 fl4 = {
269d6ec938SDavid S. Miller 		.daddr = daddr->a4,
279d6ec938SDavid S. Miller 		.flowi4_tos = tos,
28a1e59abfSPatrick McHardy 	};
2966cdb3caSHerbert Xu 	struct rtable *rt;
30a1e59abfSPatrick McHardy 
3166cdb3caSHerbert Xu 	if (saddr)
329d6ec938SDavid S. Miller 		fl4.saddr = saddr->a4;
3366cdb3caSHerbert Xu 
349d6ec938SDavid S. Miller 	rt = __ip_route_output_key(net, &fl4);
35b23dd4feSDavid S. Miller 	if (!IS_ERR(rt))
36b23dd4feSDavid S. Miller 		return &rt->dst;
37b23dd4feSDavid S. Miller 
38b23dd4feSDavid S. Miller 	return ERR_CAST(rt);
39a1e59abfSPatrick McHardy }
4066cdb3caSHerbert Xu 
41fbda33b2SAlexey Dobriyan static int xfrm4_get_saddr(struct net *net,
42fbda33b2SAlexey Dobriyan 			   xfrm_address_t *saddr, xfrm_address_t *daddr)
4366cdb3caSHerbert Xu {
4466cdb3caSHerbert Xu 	struct dst_entry *dst;
4566cdb3caSHerbert Xu 	struct rtable *rt;
4666cdb3caSHerbert Xu 
47fbda33b2SAlexey Dobriyan 	dst = xfrm4_dst_lookup(net, 0, NULL, daddr);
4866cdb3caSHerbert Xu 	if (IS_ERR(dst))
49a1e59abfSPatrick McHardy 		return -EHOSTUNREACH;
5066cdb3caSHerbert Xu 
5166cdb3caSHerbert Xu 	rt = (struct rtable *)dst;
5266cdb3caSHerbert Xu 	saddr->a4 = rt->rt_src;
5366cdb3caSHerbert Xu 	dst_release(dst);
5466cdb3caSHerbert Xu 	return 0;
55a1e59abfSPatrick McHardy }
56a1e59abfSPatrick McHardy 
5705d84025SDavid S. Miller static int xfrm4_get_tos(const struct flowi *fl)
581da177e4SLinus Torvalds {
597e1dc7b6SDavid S. Miller 	return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos; /* Strip ECN bits */
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
62a1b05140SMasahide NAKAMURA static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
63a1b05140SMasahide NAKAMURA 			   int nfheader_len)
64a1b05140SMasahide NAKAMURA {
65a1b05140SMasahide NAKAMURA 	return 0;
66a1b05140SMasahide NAKAMURA }
67a1b05140SMasahide NAKAMURA 
6887c1e12bSHerbert Xu static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
690c7b3eefSDavid S. Miller 			  const struct flowi *fl)
7025ee3286SHerbert Xu {
7125ee3286SHerbert Xu 	struct rtable *rt = (struct rtable *)xdst->route;
727e1dc7b6SDavid S. Miller 	const struct flowi4 *fl4 = &fl->u.ip4;
731da177e4SLinus Torvalds 
747e1dc7b6SDavid S. Miller 	rt->rt_key_dst = fl4->daddr;
757e1dc7b6SDavid S. Miller 	rt->rt_key_src = fl4->saddr;
767e1dc7b6SDavid S. Miller 	rt->rt_tos = fl4->flowi4_tos;
777e1dc7b6SDavid S. Miller 	rt->rt_iif = fl4->flowi4_iif;
787e1dc7b6SDavid S. Miller 	rt->rt_oif = fl4->flowi4_oif;
797e1dc7b6SDavid S. Miller 	rt->rt_mark = fl4->flowi4_mark;
801da177e4SLinus Torvalds 
8125ee3286SHerbert Xu 	xdst->u.dst.dev = dev;
8225ee3286SHerbert Xu 	dev_hold(dev);
8343372262SMiika Komu 
8425ee3286SHerbert Xu 	xdst->u.rt.peer = rt->peer;
8525ee3286SHerbert Xu 	if (rt->peer)
8625ee3286SHerbert Xu 		atomic_inc(&rt->peer->refcnt);
8766cdb3caSHerbert Xu 
881da177e4SLinus Torvalds 	/* Sheit... I remember I did this right. Apparently,
891da177e4SLinus Torvalds 	 * it was magically lost, so this code needs audit */
9025ee3286SHerbert Xu 	xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
9125ee3286SHerbert Xu 					      RTCF_LOCAL);
9225ee3286SHerbert Xu 	xdst->u.rt.rt_type = rt->rt_type;
9325ee3286SHerbert Xu 	xdst->u.rt.rt_src = rt->rt_src;
9425ee3286SHerbert Xu 	xdst->u.rt.rt_dst = rt->rt_dst;
9525ee3286SHerbert Xu 	xdst->u.rt.rt_gateway = rt->rt_gateway;
9625ee3286SHerbert Xu 	xdst->u.rt.rt_spec_dst = rt->rt_spec_dst;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	return 0;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds static void
102d5422efeSHerbert Xu _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
1031da177e4SLinus Torvalds {
104eddc9ec5SArnaldo Carvalho de Melo 	struct iphdr *iph = ip_hdr(skb);
105d56f90a7SArnaldo Carvalho de Melo 	u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
1067e1dc7b6SDavid S. Miller 	struct flowi4 *fl4 = &fl->u.ip4;
1071da177e4SLinus Torvalds 
1087e1dc7b6SDavid S. Miller 	memset(fl4, 0, sizeof(struct flowi4));
1097e1dc7b6SDavid S. Miller 	fl4->flowi4_mark = skb->mark;
11044b451f1SPeter Kosyh 
1111da177e4SLinus Torvalds 	if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
1121da177e4SLinus Torvalds 		switch (iph->protocol) {
1131da177e4SLinus Torvalds 		case IPPROTO_UDP:
114ba4e58ecSGerrit Renker 		case IPPROTO_UDPLITE:
1151da177e4SLinus Torvalds 		case IPPROTO_TCP:
1161da177e4SLinus Torvalds 		case IPPROTO_SCTP:
1179e999993SPatrick McHardy 		case IPPROTO_DCCP:
118c615c9f3SWei Yongjun 			if (xprth + 4 < skb->data ||
119c615c9f3SWei Yongjun 			    pskb_may_pull(skb, xprth + 4 - skb->data)) {
1208c689a6eSAl Viro 				__be16 *ports = (__be16 *)xprth;
1211da177e4SLinus Torvalds 
122*9cce96dfSDavid S. Miller 				fl4->fl4_sport = ports[!!reverse];
123*9cce96dfSDavid S. Miller 				fl4->fl4_dport = ports[!reverse];
1241da177e4SLinus Torvalds 			}
1251da177e4SLinus Torvalds 			break;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 		case IPPROTO_ICMP:
1281da177e4SLinus Torvalds 			if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
1291da177e4SLinus Torvalds 				u8 *icmp = xprth;
1301da177e4SLinus Torvalds 
131*9cce96dfSDavid S. Miller 				fl4->fl4_icmp_type = icmp[0];
132*9cce96dfSDavid S. Miller 				fl4->fl4_icmp_code = icmp[1];
1331da177e4SLinus Torvalds 			}
1341da177e4SLinus Torvalds 			break;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 		case IPPROTO_ESP:
1371da177e4SLinus Torvalds 			if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
1384324a174SAl Viro 				__be32 *ehdr = (__be32 *)xprth;
1391da177e4SLinus Torvalds 
140*9cce96dfSDavid S. Miller 				fl4->fl4_ipsec_spi = ehdr[0];
1411da177e4SLinus Torvalds 			}
1421da177e4SLinus Torvalds 			break;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 		case IPPROTO_AH:
1451da177e4SLinus Torvalds 			if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
1464324a174SAl Viro 				__be32 *ah_hdr = (__be32*)xprth;
1471da177e4SLinus Torvalds 
148*9cce96dfSDavid S. Miller 				fl4->fl4_ipsec_spi = ah_hdr[1];
1491da177e4SLinus Torvalds 			}
1501da177e4SLinus Torvalds 			break;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 		case IPPROTO_COMP:
1531da177e4SLinus Torvalds 			if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
1544324a174SAl Viro 				__be16 *ipcomp_hdr = (__be16 *)xprth;
1551da177e4SLinus Torvalds 
156*9cce96dfSDavid S. Miller 				fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
1571da177e4SLinus Torvalds 			}
1581da177e4SLinus Torvalds 			break;
159cc9ff19dSTimo Teräs 
160cc9ff19dSTimo Teräs 		case IPPROTO_GRE:
161cc9ff19dSTimo Teräs 			if (pskb_may_pull(skb, xprth + 12 - skb->data)) {
162cc9ff19dSTimo Teräs 				__be16 *greflags = (__be16 *)xprth;
163cc9ff19dSTimo Teräs 				__be32 *gre_hdr = (__be32 *)xprth;
164cc9ff19dSTimo Teräs 
165cc9ff19dSTimo Teräs 				if (greflags[0] & GRE_KEY) {
166cc9ff19dSTimo Teräs 					if (greflags[0] & GRE_CSUM)
167cc9ff19dSTimo Teräs 						gre_hdr++;
168*9cce96dfSDavid S. Miller 					fl4->fl4_gre_key = gre_hdr[1];
169cc9ff19dSTimo Teräs 				}
170cc9ff19dSTimo Teräs 			}
171cc9ff19dSTimo Teräs 			break;
172cc9ff19dSTimo Teräs 
1731da177e4SLinus Torvalds 		default:
174*9cce96dfSDavid S. Miller 			fl4->fl4_ipsec_spi = 0;
1751da177e4SLinus Torvalds 			break;
1763ff50b79SStephen Hemminger 		}
1771da177e4SLinus Torvalds 	}
1787e1dc7b6SDavid S. Miller 	fl4->flowi4_proto = iph->protocol;
1797e1dc7b6SDavid S. Miller 	fl4->daddr = reverse ? iph->saddr : iph->daddr;
1807e1dc7b6SDavid S. Miller 	fl4->saddr = reverse ? iph->daddr : iph->saddr;
1817e1dc7b6SDavid S. Miller 	fl4->flowi4_tos = iph->tos;
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
184569d3645SDaniel Lezcano static inline int xfrm4_garbage_collect(struct dst_ops *ops)
1851da177e4SLinus Torvalds {
186d7c7544cSAlexey Dobriyan 	struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
187d7c7544cSAlexey Dobriyan 
188d7c7544cSAlexey Dobriyan 	xfrm4_policy_afinfo.garbage_collect(net);
189fc66f95cSEric Dumazet 	return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1951da177e4SLinus Torvalds 	struct dst_entry *path = xdst->route;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	path->ops->update_pmtu(path, mtu);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
200aabc9761SHerbert Xu static void xfrm4_dst_destroy(struct dst_entry *dst)
201aabc9761SHerbert Xu {
202aabc9761SHerbert Xu 	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
203aabc9761SHerbert Xu 
20462fa8a84SDavid S. Miller 	dst_destroy_metrics_generic(dst);
20562fa8a84SDavid S. Miller 
206ed3e37ddSHerbert Xu 	if (likely(xdst->u.rt.peer))
20726db1677SDavid S. Miller 		inet_putpeer(xdst->u.rt.peer);
20862fa8a84SDavid S. Miller 
209aabc9761SHerbert Xu 	xfrm_dst_destroy(xdst);
210aabc9761SHerbert Xu }
211aabc9761SHerbert Xu 
212aabc9761SHerbert Xu static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
213aabc9761SHerbert Xu 			     int unregister)
214aabc9761SHerbert Xu {
215aabc9761SHerbert Xu 	if (!unregister)
216aabc9761SHerbert Xu 		return;
217aabc9761SHerbert Xu 
218aabc9761SHerbert Xu 	xfrm_dst_ifdown(dst, dev);
219aabc9761SHerbert Xu }
220aabc9761SHerbert Xu 
2211da177e4SLinus Torvalds static struct dst_ops xfrm4_dst_ops = {
2221da177e4SLinus Torvalds 	.family =		AF_INET,
22309640e63SHarvey Harrison 	.protocol =		cpu_to_be16(ETH_P_IP),
2241da177e4SLinus Torvalds 	.gc =			xfrm4_garbage_collect,
2251da177e4SLinus Torvalds 	.update_pmtu =		xfrm4_update_pmtu,
22662fa8a84SDavid S. Miller 	.cow_metrics =		dst_cow_metrics_generic,
227aabc9761SHerbert Xu 	.destroy =		xfrm4_dst_destroy,
228aabc9761SHerbert Xu 	.ifdown =		xfrm4_dst_ifdown,
229862b82c6SHerbert Xu 	.local_out =		__ip_local_out,
2301da177e4SLinus Torvalds 	.gc_thresh =		1024,
2311da177e4SLinus Torvalds };
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
2341da177e4SLinus Torvalds 	.family = 		AF_INET,
2351da177e4SLinus Torvalds 	.dst_ops =		&xfrm4_dst_ops,
2361da177e4SLinus Torvalds 	.dst_lookup =		xfrm4_dst_lookup,
237a1e59abfSPatrick McHardy 	.get_saddr =		xfrm4_get_saddr,
2381da177e4SLinus Torvalds 	.decode_session =	_decode_session4,
23925ee3286SHerbert Xu 	.get_tos =		xfrm4_get_tos,
240a1b05140SMasahide NAKAMURA 	.init_path =		xfrm4_init_path,
24125ee3286SHerbert Xu 	.fill_dst =		xfrm4_fill_dst,
2422774c131SDavid S. Miller 	.blackhole_route =	ipv4_blackhole_route,
2431da177e4SLinus Torvalds };
2441da177e4SLinus Torvalds 
245f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL
246a44a4a00SNeil Horman static struct ctl_table xfrm4_policy_table[] = {
247a44a4a00SNeil Horman 	{
248a44a4a00SNeil Horman 		.procname       = "xfrm4_gc_thresh",
249d7c7544cSAlexey Dobriyan 		.data           = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
250a44a4a00SNeil Horman 		.maxlen         = sizeof(int),
251a44a4a00SNeil Horman 		.mode           = 0644,
252a44a4a00SNeil Horman 		.proc_handler   = proc_dointvec,
253a44a4a00SNeil Horman 	},
254a44a4a00SNeil Horman 	{ }
255a44a4a00SNeil Horman };
256a44a4a00SNeil Horman 
257a44a4a00SNeil Horman static struct ctl_table_header *sysctl_hdr;
258f816700aSRandy Dunlap #endif
259a44a4a00SNeil Horman 
2601da177e4SLinus Torvalds static void __init xfrm4_policy_init(void)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds static void __exit xfrm4_policy_fini(void)
2661da177e4SLinus Torvalds {
267f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL
268a44a4a00SNeil Horman 	if (sysctl_hdr)
269a44a4a00SNeil Horman 		unregister_net_sysctl_table(sysctl_hdr);
270f816700aSRandy Dunlap #endif
2711da177e4SLinus Torvalds 	xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
274a33bc5c1SNeil Horman void __init xfrm4_init(int rt_max_size)
2751da177e4SLinus Torvalds {
276a33bc5c1SNeil Horman 	/*
277a33bc5c1SNeil Horman 	 * Select a default value for the gc_thresh based on the main route
278a33bc5c1SNeil Horman 	 * table hash size.  It seems to me the worst case scenario is when
279a33bc5c1SNeil Horman 	 * we have ipsec operating in transport mode, in which we create a
280a33bc5c1SNeil Horman 	 * dst_entry per socket.  The xfrm gc algorithm starts trying to remove
281a33bc5c1SNeil Horman 	 * entries at gc_thresh, and prevents new allocations as 2*gc_thresh
282a33bc5c1SNeil Horman 	 * so lets set an initial xfrm gc_thresh value at the rt_max_size/2.
283a33bc5c1SNeil Horman 	 * That will let us store an ipsec connection per route table entry,
284a33bc5c1SNeil Horman 	 * and start cleaning when were 1/2 full
285a33bc5c1SNeil Horman 	 */
286a33bc5c1SNeil Horman 	xfrm4_dst_ops.gc_thresh = rt_max_size/2;
287fc66f95cSEric Dumazet 	dst_entries_init(&xfrm4_dst_ops);
288d7c7544cSAlexey Dobriyan 
289d7c7544cSAlexey Dobriyan 	xfrm4_state_init();
290d7c7544cSAlexey Dobriyan 	xfrm4_policy_init();
291f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL
292a44a4a00SNeil Horman 	sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv4_ctl_path,
293a44a4a00SNeil Horman 						xfrm4_policy_table);
294f816700aSRandy Dunlap #endif
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
297