xref: /linux/net/ipv4/xfrm4_policy.c (revision 6700c2709c08d74ae2c3c29b84a30da012dbc7f1)
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 
218f01cb08SDavid S. Miller static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4,
228f01cb08SDavid S. Miller 					    int tos,
235e6b930fSDavid S. Miller 					    const xfrm_address_t *saddr,
245e6b930fSDavid S. Miller 					    const xfrm_address_t *daddr)
251da177e4SLinus Torvalds {
2666cdb3caSHerbert Xu 	struct rtable *rt;
27a1e59abfSPatrick McHardy 
288f01cb08SDavid S. Miller 	memset(fl4, 0, sizeof(*fl4));
298f01cb08SDavid S. Miller 	fl4->daddr = daddr->a4;
308f01cb08SDavid S. Miller 	fl4->flowi4_tos = tos;
3166cdb3caSHerbert Xu 	if (saddr)
328f01cb08SDavid S. Miller 		fl4->saddr = saddr->a4;
3366cdb3caSHerbert Xu 
348f01cb08SDavid 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 
418f01cb08SDavid S. Miller static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos,
428f01cb08SDavid S. Miller 					  const xfrm_address_t *saddr,
438f01cb08SDavid S. Miller 					  const xfrm_address_t *daddr)
448f01cb08SDavid S. Miller {
458f01cb08SDavid S. Miller 	struct flowi4 fl4;
468f01cb08SDavid S. Miller 
478f01cb08SDavid S. Miller 	return __xfrm4_dst_lookup(net, &fl4, tos, saddr, daddr);
488f01cb08SDavid S. Miller }
498f01cb08SDavid S. Miller 
50fbda33b2SAlexey Dobriyan static int xfrm4_get_saddr(struct net *net,
51fbda33b2SAlexey Dobriyan 			   xfrm_address_t *saddr, xfrm_address_t *daddr)
5266cdb3caSHerbert Xu {
5366cdb3caSHerbert Xu 	struct dst_entry *dst;
548f01cb08SDavid S. Miller 	struct flowi4 fl4;
5566cdb3caSHerbert Xu 
568f01cb08SDavid S. Miller 	dst = __xfrm4_dst_lookup(net, &fl4, 0, NULL, daddr);
5766cdb3caSHerbert Xu 	if (IS_ERR(dst))
58a1e59abfSPatrick McHardy 		return -EHOSTUNREACH;
5966cdb3caSHerbert Xu 
608f01cb08SDavid S. Miller 	saddr->a4 = fl4.saddr;
6166cdb3caSHerbert Xu 	dst_release(dst);
6266cdb3caSHerbert Xu 	return 0;
63a1e59abfSPatrick McHardy }
64a1e59abfSPatrick McHardy 
6505d84025SDavid S. Miller static int xfrm4_get_tos(const struct flowi *fl)
661da177e4SLinus Torvalds {
677e1dc7b6SDavid S. Miller 	return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos; /* Strip ECN bits */
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
70a1b05140SMasahide NAKAMURA static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
71a1b05140SMasahide NAKAMURA 			   int nfheader_len)
72a1b05140SMasahide NAKAMURA {
73a1b05140SMasahide NAKAMURA 	return 0;
74a1b05140SMasahide NAKAMURA }
75a1b05140SMasahide NAKAMURA 
7687c1e12bSHerbert Xu static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
770c7b3eefSDavid S. Miller 			  const struct flowi *fl)
7825ee3286SHerbert Xu {
7925ee3286SHerbert Xu 	struct rtable *rt = (struct rtable *)xdst->route;
807e1dc7b6SDavid S. Miller 	const struct flowi4 *fl4 = &fl->u.ip4;
811da177e4SLinus Torvalds 
82b7323396SYan, Zheng 	xdst->u.rt.rt_key_dst = fl4->daddr;
83b7323396SYan, Zheng 	xdst->u.rt.rt_key_src = fl4->saddr;
84b7323396SYan, Zheng 	xdst->u.rt.rt_key_tos = fl4->flowi4_tos;
85b7323396SYan, Zheng 	xdst->u.rt.rt_route_iif = fl4->flowi4_iif;
86b7323396SYan, Zheng 	xdst->u.rt.rt_iif = fl4->flowi4_iif;
87b7323396SYan, Zheng 	xdst->u.rt.rt_oif = fl4->flowi4_oif;
88b7323396SYan, Zheng 	xdst->u.rt.rt_mark = fl4->flowi4_mark;
891da177e4SLinus Torvalds 
9025ee3286SHerbert Xu 	xdst->u.dst.dev = dev;
9125ee3286SHerbert Xu 	dev_hold(dev);
9243372262SMiika Komu 
931da177e4SLinus Torvalds 	/* Sheit... I remember I did this right. Apparently,
941da177e4SLinus Torvalds 	 * it was magically lost, so this code needs audit */
9525ee3286SHerbert Xu 	xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
9625ee3286SHerbert Xu 					      RTCF_LOCAL);
9725ee3286SHerbert Xu 	xdst->u.rt.rt_type = rt->rt_type;
9825ee3286SHerbert Xu 	xdst->u.rt.rt_src = rt->rt_src;
9925ee3286SHerbert Xu 	xdst->u.rt.rt_dst = rt->rt_dst;
10025ee3286SHerbert Xu 	xdst->u.rt.rt_gateway = rt->rt_gateway;
1015943634fSDavid S. Miller 	xdst->u.rt.rt_pmtu = rt->rt_pmtu;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	return 0;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds static void
107d5422efeSHerbert Xu _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
1081da177e4SLinus Torvalds {
109b71d1d42SEric Dumazet 	const struct iphdr *iph = ip_hdr(skb);
110d56f90a7SArnaldo Carvalho de Melo 	u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
1117e1dc7b6SDavid S. Miller 	struct flowi4 *fl4 = &fl->u.ip4;
1121da177e4SLinus Torvalds 
1137e1dc7b6SDavid S. Miller 	memset(fl4, 0, sizeof(struct flowi4));
1147e1dc7b6SDavid S. Miller 	fl4->flowi4_mark = skb->mark;
11544b451f1SPeter Kosyh 
11656f8a75cSPaul Gortmaker 	if (!ip_is_fragment(iph)) {
1171da177e4SLinus Torvalds 		switch (iph->protocol) {
1181da177e4SLinus Torvalds 		case IPPROTO_UDP:
119ba4e58ecSGerrit Renker 		case IPPROTO_UDPLITE:
1201da177e4SLinus Torvalds 		case IPPROTO_TCP:
1211da177e4SLinus Torvalds 		case IPPROTO_SCTP:
1229e999993SPatrick McHardy 		case IPPROTO_DCCP:
123c615c9f3SWei Yongjun 			if (xprth + 4 < skb->data ||
124c615c9f3SWei Yongjun 			    pskb_may_pull(skb, xprth + 4 - skb->data)) {
1258c689a6eSAl Viro 				__be16 *ports = (__be16 *)xprth;
1261da177e4SLinus Torvalds 
1279cce96dfSDavid S. Miller 				fl4->fl4_sport = ports[!!reverse];
1289cce96dfSDavid S. Miller 				fl4->fl4_dport = ports[!reverse];
1291da177e4SLinus Torvalds 			}
1301da177e4SLinus Torvalds 			break;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 		case IPPROTO_ICMP:
1331da177e4SLinus Torvalds 			if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
1341da177e4SLinus Torvalds 				u8 *icmp = xprth;
1351da177e4SLinus Torvalds 
1369cce96dfSDavid S. Miller 				fl4->fl4_icmp_type = icmp[0];
1379cce96dfSDavid S. Miller 				fl4->fl4_icmp_code = icmp[1];
1381da177e4SLinus Torvalds 			}
1391da177e4SLinus Torvalds 			break;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 		case IPPROTO_ESP:
1421da177e4SLinus Torvalds 			if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
1434324a174SAl Viro 				__be32 *ehdr = (__be32 *)xprth;
1441da177e4SLinus Torvalds 
1459cce96dfSDavid S. Miller 				fl4->fl4_ipsec_spi = ehdr[0];
1461da177e4SLinus Torvalds 			}
1471da177e4SLinus Torvalds 			break;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		case IPPROTO_AH:
1501da177e4SLinus Torvalds 			if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
1514324a174SAl Viro 				__be32 *ah_hdr = (__be32 *)xprth;
1521da177e4SLinus Torvalds 
1539cce96dfSDavid S. Miller 				fl4->fl4_ipsec_spi = ah_hdr[1];
1541da177e4SLinus Torvalds 			}
1551da177e4SLinus Torvalds 			break;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 		case IPPROTO_COMP:
1581da177e4SLinus Torvalds 			if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
1594324a174SAl Viro 				__be16 *ipcomp_hdr = (__be16 *)xprth;
1601da177e4SLinus Torvalds 
1619cce96dfSDavid S. Miller 				fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
1621da177e4SLinus Torvalds 			}
1631da177e4SLinus Torvalds 			break;
164cc9ff19dSTimo Teräs 
165cc9ff19dSTimo Teräs 		case IPPROTO_GRE:
166cc9ff19dSTimo Teräs 			if (pskb_may_pull(skb, xprth + 12 - skb->data)) {
167cc9ff19dSTimo Teräs 				__be16 *greflags = (__be16 *)xprth;
168cc9ff19dSTimo Teräs 				__be32 *gre_hdr = (__be32 *)xprth;
169cc9ff19dSTimo Teräs 
170cc9ff19dSTimo Teräs 				if (greflags[0] & GRE_KEY) {
171cc9ff19dSTimo Teräs 					if (greflags[0] & GRE_CSUM)
172cc9ff19dSTimo Teräs 						gre_hdr++;
1739cce96dfSDavid S. Miller 					fl4->fl4_gre_key = gre_hdr[1];
174cc9ff19dSTimo Teräs 				}
175cc9ff19dSTimo Teräs 			}
176cc9ff19dSTimo Teräs 			break;
177cc9ff19dSTimo Teräs 
1781da177e4SLinus Torvalds 		default:
1799cce96dfSDavid S. Miller 			fl4->fl4_ipsec_spi = 0;
1801da177e4SLinus Torvalds 			break;
1813ff50b79SStephen Hemminger 		}
1821da177e4SLinus Torvalds 	}
1837e1dc7b6SDavid S. Miller 	fl4->flowi4_proto = iph->protocol;
1847e1dc7b6SDavid S. Miller 	fl4->daddr = reverse ? iph->saddr : iph->daddr;
1857e1dc7b6SDavid S. Miller 	fl4->saddr = reverse ? iph->daddr : iph->saddr;
1867e1dc7b6SDavid S. Miller 	fl4->flowi4_tos = iph->tos;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
189569d3645SDaniel Lezcano static inline int xfrm4_garbage_collect(struct dst_ops *ops)
1901da177e4SLinus Torvalds {
191d7c7544cSAlexey Dobriyan 	struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
192d7c7544cSAlexey Dobriyan 
193d7c7544cSAlexey Dobriyan 	xfrm4_policy_afinfo.garbage_collect(net);
194fc66f95cSEric Dumazet 	return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
197*6700c270SDavid S. Miller static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
198*6700c270SDavid S. Miller 			      struct sk_buff *skb, u32 mtu)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2011da177e4SLinus Torvalds 	struct dst_entry *path = xdst->route;
2021da177e4SLinus Torvalds 
203*6700c270SDavid S. Miller 	path->ops->update_pmtu(path, sk, skb, mtu);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
206*6700c270SDavid S. Miller static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
207*6700c270SDavid S. Miller 			   struct sk_buff *skb)
20855be7a9cSDavid S. Miller {
20955be7a9cSDavid S. Miller 	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
21055be7a9cSDavid S. Miller 	struct dst_entry *path = xdst->route;
21155be7a9cSDavid S. Miller 
212*6700c270SDavid S. Miller 	path->ops->redirect(path, sk, skb);
21355be7a9cSDavid S. Miller }
21455be7a9cSDavid S. Miller 
215aabc9761SHerbert Xu static void xfrm4_dst_destroy(struct dst_entry *dst)
216aabc9761SHerbert Xu {
217aabc9761SHerbert Xu 	struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
218aabc9761SHerbert Xu 
21962fa8a84SDavid S. Miller 	dst_destroy_metrics_generic(dst);
22062fa8a84SDavid S. Miller 
221aabc9761SHerbert Xu 	xfrm_dst_destroy(xdst);
222aabc9761SHerbert Xu }
223aabc9761SHerbert Xu 
224aabc9761SHerbert Xu static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
225aabc9761SHerbert Xu 			     int unregister)
226aabc9761SHerbert Xu {
227aabc9761SHerbert Xu 	if (!unregister)
228aabc9761SHerbert Xu 		return;
229aabc9761SHerbert Xu 
230aabc9761SHerbert Xu 	xfrm_dst_ifdown(dst, dev);
231aabc9761SHerbert Xu }
232aabc9761SHerbert Xu 
2331da177e4SLinus Torvalds static struct dst_ops xfrm4_dst_ops = {
2341da177e4SLinus Torvalds 	.family =		AF_INET,
23509640e63SHarvey Harrison 	.protocol =		cpu_to_be16(ETH_P_IP),
2361da177e4SLinus Torvalds 	.gc =			xfrm4_garbage_collect,
2371da177e4SLinus Torvalds 	.update_pmtu =		xfrm4_update_pmtu,
23855be7a9cSDavid S. Miller 	.redirect =		xfrm4_redirect,
23962fa8a84SDavid S. Miller 	.cow_metrics =		dst_cow_metrics_generic,
240aabc9761SHerbert Xu 	.destroy =		xfrm4_dst_destroy,
241aabc9761SHerbert Xu 	.ifdown =		xfrm4_dst_ifdown,
242862b82c6SHerbert Xu 	.local_out =		__ip_local_out,
2431da177e4SLinus Torvalds 	.gc_thresh =		1024,
2441da177e4SLinus Torvalds };
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
2471da177e4SLinus Torvalds 	.family = 		AF_INET,
2481da177e4SLinus Torvalds 	.dst_ops =		&xfrm4_dst_ops,
2491da177e4SLinus Torvalds 	.dst_lookup =		xfrm4_dst_lookup,
250a1e59abfSPatrick McHardy 	.get_saddr =		xfrm4_get_saddr,
2511da177e4SLinus Torvalds 	.decode_session =	_decode_session4,
25225ee3286SHerbert Xu 	.get_tos =		xfrm4_get_tos,
253a1b05140SMasahide NAKAMURA 	.init_path =		xfrm4_init_path,
25425ee3286SHerbert Xu 	.fill_dst =		xfrm4_fill_dst,
2552774c131SDavid S. Miller 	.blackhole_route =	ipv4_blackhole_route,
2561da177e4SLinus Torvalds };
2571da177e4SLinus Torvalds 
258f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL
259a44a4a00SNeil Horman static struct ctl_table xfrm4_policy_table[] = {
260a44a4a00SNeil Horman 	{
261a44a4a00SNeil Horman 		.procname       = "xfrm4_gc_thresh",
262d7c7544cSAlexey Dobriyan 		.data           = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
263a44a4a00SNeil Horman 		.maxlen         = sizeof(int),
264a44a4a00SNeil Horman 		.mode           = 0644,
265a44a4a00SNeil Horman 		.proc_handler   = proc_dointvec,
266a44a4a00SNeil Horman 	},
267a44a4a00SNeil Horman 	{ }
268a44a4a00SNeil Horman };
269a44a4a00SNeil Horman 
270a44a4a00SNeil Horman static struct ctl_table_header *sysctl_hdr;
271f816700aSRandy Dunlap #endif
272a44a4a00SNeil Horman 
2731da177e4SLinus Torvalds static void __init xfrm4_policy_init(void)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static void __exit xfrm4_policy_fini(void)
2791da177e4SLinus Torvalds {
280f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL
281a44a4a00SNeil Horman 	if (sysctl_hdr)
282a44a4a00SNeil Horman 		unregister_net_sysctl_table(sysctl_hdr);
283f816700aSRandy Dunlap #endif
2841da177e4SLinus Torvalds 	xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
287a33bc5c1SNeil Horman void __init xfrm4_init(int rt_max_size)
2881da177e4SLinus Torvalds {
289a33bc5c1SNeil Horman 	/*
290a33bc5c1SNeil Horman 	 * Select a default value for the gc_thresh based on the main route
291a33bc5c1SNeil Horman 	 * table hash size.  It seems to me the worst case scenario is when
292a33bc5c1SNeil Horman 	 * we have ipsec operating in transport mode, in which we create a
293a33bc5c1SNeil Horman 	 * dst_entry per socket.  The xfrm gc algorithm starts trying to remove
294a33bc5c1SNeil Horman 	 * entries at gc_thresh, and prevents new allocations as 2*gc_thresh
295a33bc5c1SNeil Horman 	 * so lets set an initial xfrm gc_thresh value at the rt_max_size/2.
296a33bc5c1SNeil Horman 	 * That will let us store an ipsec connection per route table entry,
297a33bc5c1SNeil Horman 	 * and start cleaning when were 1/2 full
298a33bc5c1SNeil Horman 	 */
299a33bc5c1SNeil Horman 	xfrm4_dst_ops.gc_thresh = rt_max_size/2;
300fc66f95cSEric Dumazet 	dst_entries_init(&xfrm4_dst_ops);
301d7c7544cSAlexey Dobriyan 
302d7c7544cSAlexey Dobriyan 	xfrm4_state_init();
303d7c7544cSAlexey Dobriyan 	xfrm4_policy_init();
304f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL
305ec8f23ceSEric W. Biederman 	sysctl_hdr = register_net_sysctl(&init_net, "net/ipv4",
306a44a4a00SNeil Horman 					 xfrm4_policy_table);
307f816700aSRandy Dunlap #endif
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
310