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_iif = fl4->flowi4_iif; 831da177e4SLinus Torvalds 8425ee3286SHerbert Xu xdst->u.dst.dev = dev; 8525ee3286SHerbert Xu dev_hold(dev); 8643372262SMiika Komu 871da177e4SLinus Torvalds /* Sheit... I remember I did this right. Apparently, 881da177e4SLinus Torvalds * it was magically lost, so this code needs audit */ 89*9917e1e8SDavid S. Miller xdst->u.rt.rt_is_input = rt->rt_is_input; 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_gateway = rt->rt_gateway; 945943634fSDavid S. Miller xdst->u.rt.rt_pmtu = rt->rt_pmtu; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds return 0; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds static void 100d5422efeSHerbert Xu _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse) 1011da177e4SLinus Torvalds { 102b71d1d42SEric Dumazet const struct iphdr *iph = ip_hdr(skb); 103d56f90a7SArnaldo Carvalho de Melo u8 *xprth = skb_network_header(skb) + iph->ihl * 4; 1047e1dc7b6SDavid S. Miller struct flowi4 *fl4 = &fl->u.ip4; 1051da177e4SLinus Torvalds 1067e1dc7b6SDavid S. Miller memset(fl4, 0, sizeof(struct flowi4)); 1077e1dc7b6SDavid S. Miller fl4->flowi4_mark = skb->mark; 10844b451f1SPeter Kosyh 10956f8a75cSPaul Gortmaker if (!ip_is_fragment(iph)) { 1101da177e4SLinus Torvalds switch (iph->protocol) { 1111da177e4SLinus Torvalds case IPPROTO_UDP: 112ba4e58ecSGerrit Renker case IPPROTO_UDPLITE: 1131da177e4SLinus Torvalds case IPPROTO_TCP: 1141da177e4SLinus Torvalds case IPPROTO_SCTP: 1159e999993SPatrick McHardy case IPPROTO_DCCP: 116c615c9f3SWei Yongjun if (xprth + 4 < skb->data || 117c615c9f3SWei Yongjun pskb_may_pull(skb, xprth + 4 - skb->data)) { 1188c689a6eSAl Viro __be16 *ports = (__be16 *)xprth; 1191da177e4SLinus Torvalds 1209cce96dfSDavid S. Miller fl4->fl4_sport = ports[!!reverse]; 1219cce96dfSDavid S. Miller fl4->fl4_dport = ports[!reverse]; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds break; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds case IPPROTO_ICMP: 1261da177e4SLinus Torvalds if (pskb_may_pull(skb, xprth + 2 - skb->data)) { 1271da177e4SLinus Torvalds u8 *icmp = xprth; 1281da177e4SLinus Torvalds 1299cce96dfSDavid S. Miller fl4->fl4_icmp_type = icmp[0]; 1309cce96dfSDavid S. Miller fl4->fl4_icmp_code = icmp[1]; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds break; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds case IPPROTO_ESP: 1351da177e4SLinus Torvalds if (pskb_may_pull(skb, xprth + 4 - skb->data)) { 1364324a174SAl Viro __be32 *ehdr = (__be32 *)xprth; 1371da177e4SLinus Torvalds 1389cce96dfSDavid S. Miller fl4->fl4_ipsec_spi = ehdr[0]; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds break; 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds case IPPROTO_AH: 1431da177e4SLinus Torvalds if (pskb_may_pull(skb, xprth + 8 - skb->data)) { 1444324a174SAl Viro __be32 *ah_hdr = (__be32 *)xprth; 1451da177e4SLinus Torvalds 1469cce96dfSDavid S. Miller fl4->fl4_ipsec_spi = ah_hdr[1]; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds break; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds case IPPROTO_COMP: 1511da177e4SLinus Torvalds if (pskb_may_pull(skb, xprth + 4 - skb->data)) { 1524324a174SAl Viro __be16 *ipcomp_hdr = (__be16 *)xprth; 1531da177e4SLinus Torvalds 1549cce96dfSDavid S. Miller fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1])); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds break; 157cc9ff19dSTimo Teräs 158cc9ff19dSTimo Teräs case IPPROTO_GRE: 159cc9ff19dSTimo Teräs if (pskb_may_pull(skb, xprth + 12 - skb->data)) { 160cc9ff19dSTimo Teräs __be16 *greflags = (__be16 *)xprth; 161cc9ff19dSTimo Teräs __be32 *gre_hdr = (__be32 *)xprth; 162cc9ff19dSTimo Teräs 163cc9ff19dSTimo Teräs if (greflags[0] & GRE_KEY) { 164cc9ff19dSTimo Teräs if (greflags[0] & GRE_CSUM) 165cc9ff19dSTimo Teräs gre_hdr++; 1669cce96dfSDavid S. Miller fl4->fl4_gre_key = gre_hdr[1]; 167cc9ff19dSTimo Teräs } 168cc9ff19dSTimo Teräs } 169cc9ff19dSTimo Teräs break; 170cc9ff19dSTimo Teräs 1711da177e4SLinus Torvalds default: 1729cce96dfSDavid S. Miller fl4->fl4_ipsec_spi = 0; 1731da177e4SLinus Torvalds break; 1743ff50b79SStephen Hemminger } 1751da177e4SLinus Torvalds } 1767e1dc7b6SDavid S. Miller fl4->flowi4_proto = iph->protocol; 1777e1dc7b6SDavid S. Miller fl4->daddr = reverse ? iph->saddr : iph->daddr; 1787e1dc7b6SDavid S. Miller fl4->saddr = reverse ? iph->daddr : iph->saddr; 1797e1dc7b6SDavid S. Miller fl4->flowi4_tos = iph->tos; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 182569d3645SDaniel Lezcano static inline int xfrm4_garbage_collect(struct dst_ops *ops) 1831da177e4SLinus Torvalds { 184d7c7544cSAlexey Dobriyan struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops); 185d7c7544cSAlexey Dobriyan 186d7c7544cSAlexey Dobriyan xfrm4_policy_afinfo.garbage_collect(net); 187fc66f95cSEric Dumazet return (dst_entries_get_slow(ops) > ops->gc_thresh * 2); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1906700c270SDavid S. Miller static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk, 1916700c270SDavid S. Miller struct sk_buff *skb, u32 mtu) 1921da177e4SLinus Torvalds { 1931da177e4SLinus Torvalds struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 1941da177e4SLinus Torvalds struct dst_entry *path = xdst->route; 1951da177e4SLinus Torvalds 1966700c270SDavid S. Miller path->ops->update_pmtu(path, sk, skb, mtu); 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1996700c270SDavid S. Miller static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk, 2006700c270SDavid S. Miller struct sk_buff *skb) 20155be7a9cSDavid S. Miller { 20255be7a9cSDavid S. Miller struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 20355be7a9cSDavid S. Miller struct dst_entry *path = xdst->route; 20455be7a9cSDavid S. Miller 2056700c270SDavid S. Miller path->ops->redirect(path, sk, skb); 20655be7a9cSDavid S. Miller } 20755be7a9cSDavid S. Miller 208aabc9761SHerbert Xu static void xfrm4_dst_destroy(struct dst_entry *dst) 209aabc9761SHerbert Xu { 210aabc9761SHerbert Xu struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 211aabc9761SHerbert Xu 21262fa8a84SDavid S. Miller dst_destroy_metrics_generic(dst); 21362fa8a84SDavid S. Miller 214aabc9761SHerbert Xu xfrm_dst_destroy(xdst); 215aabc9761SHerbert Xu } 216aabc9761SHerbert Xu 217aabc9761SHerbert Xu static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 218aabc9761SHerbert Xu int unregister) 219aabc9761SHerbert Xu { 220aabc9761SHerbert Xu if (!unregister) 221aabc9761SHerbert Xu return; 222aabc9761SHerbert Xu 223aabc9761SHerbert Xu xfrm_dst_ifdown(dst, dev); 224aabc9761SHerbert Xu } 225aabc9761SHerbert Xu 2261da177e4SLinus Torvalds static struct dst_ops xfrm4_dst_ops = { 2271da177e4SLinus Torvalds .family = AF_INET, 22809640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IP), 2291da177e4SLinus Torvalds .gc = xfrm4_garbage_collect, 2301da177e4SLinus Torvalds .update_pmtu = xfrm4_update_pmtu, 23155be7a9cSDavid S. Miller .redirect = xfrm4_redirect, 23262fa8a84SDavid S. Miller .cow_metrics = dst_cow_metrics_generic, 233aabc9761SHerbert Xu .destroy = xfrm4_dst_destroy, 234aabc9761SHerbert Xu .ifdown = xfrm4_dst_ifdown, 235862b82c6SHerbert Xu .local_out = __ip_local_out, 2361da177e4SLinus Torvalds .gc_thresh = 1024, 2371da177e4SLinus Torvalds }; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds static struct xfrm_policy_afinfo xfrm4_policy_afinfo = { 2401da177e4SLinus Torvalds .family = AF_INET, 2411da177e4SLinus Torvalds .dst_ops = &xfrm4_dst_ops, 2421da177e4SLinus Torvalds .dst_lookup = xfrm4_dst_lookup, 243a1e59abfSPatrick McHardy .get_saddr = xfrm4_get_saddr, 2441da177e4SLinus Torvalds .decode_session = _decode_session4, 24525ee3286SHerbert Xu .get_tos = xfrm4_get_tos, 246a1b05140SMasahide NAKAMURA .init_path = xfrm4_init_path, 24725ee3286SHerbert Xu .fill_dst = xfrm4_fill_dst, 2482774c131SDavid S. Miller .blackhole_route = ipv4_blackhole_route, 2491da177e4SLinus Torvalds }; 2501da177e4SLinus Torvalds 251f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL 252a44a4a00SNeil Horman static struct ctl_table xfrm4_policy_table[] = { 253a44a4a00SNeil Horman { 254a44a4a00SNeil Horman .procname = "xfrm4_gc_thresh", 255d7c7544cSAlexey Dobriyan .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh, 256a44a4a00SNeil Horman .maxlen = sizeof(int), 257a44a4a00SNeil Horman .mode = 0644, 258a44a4a00SNeil Horman .proc_handler = proc_dointvec, 259a44a4a00SNeil Horman }, 260a44a4a00SNeil Horman { } 261a44a4a00SNeil Horman }; 262a44a4a00SNeil Horman 263a44a4a00SNeil Horman static struct ctl_table_header *sysctl_hdr; 264f816700aSRandy Dunlap #endif 265a44a4a00SNeil Horman 2661da177e4SLinus Torvalds static void __init xfrm4_policy_init(void) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds xfrm_policy_register_afinfo(&xfrm4_policy_afinfo); 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds static void __exit xfrm4_policy_fini(void) 2721da177e4SLinus Torvalds { 273f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL 274a44a4a00SNeil Horman if (sysctl_hdr) 275a44a4a00SNeil Horman unregister_net_sysctl_table(sysctl_hdr); 276f816700aSRandy Dunlap #endif 2771da177e4SLinus Torvalds xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo); 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 280a33bc5c1SNeil Horman void __init xfrm4_init(int rt_max_size) 2811da177e4SLinus Torvalds { 282a33bc5c1SNeil Horman /* 283a33bc5c1SNeil Horman * Select a default value for the gc_thresh based on the main route 284a33bc5c1SNeil Horman * table hash size. It seems to me the worst case scenario is when 285a33bc5c1SNeil Horman * we have ipsec operating in transport mode, in which we create a 286a33bc5c1SNeil Horman * dst_entry per socket. The xfrm gc algorithm starts trying to remove 287a33bc5c1SNeil Horman * entries at gc_thresh, and prevents new allocations as 2*gc_thresh 288a33bc5c1SNeil Horman * so lets set an initial xfrm gc_thresh value at the rt_max_size/2. 289a33bc5c1SNeil Horman * That will let us store an ipsec connection per route table entry, 290a33bc5c1SNeil Horman * and start cleaning when were 1/2 full 291a33bc5c1SNeil Horman */ 292a33bc5c1SNeil Horman xfrm4_dst_ops.gc_thresh = rt_max_size/2; 293fc66f95cSEric Dumazet dst_entries_init(&xfrm4_dst_ops); 294d7c7544cSAlexey Dobriyan 295d7c7544cSAlexey Dobriyan xfrm4_state_init(); 296d7c7544cSAlexey Dobriyan xfrm4_policy_init(); 297f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL 298ec8f23ceSEric W. Biederman sysctl_hdr = register_net_sysctl(&init_net, "net/ipv4", 299a44a4a00SNeil Horman xfrm4_policy_table); 300f816700aSRandy Dunlap #endif 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 303