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> 1445ff5a3fSHerbert Xu #include <net/dst.h> 151da177e4SLinus Torvalds #include <net/xfrm.h> 161da177e4SLinus Torvalds #include <net/ip.h> 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds static struct xfrm_policy_afinfo xfrm4_policy_afinfo; 191da177e4SLinus Torvalds 20c5b3cf46SAlexey Dobriyan static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos, 21c5b3cf46SAlexey Dobriyan xfrm_address_t *saddr, 2266cdb3caSHerbert Xu xfrm_address_t *daddr) 231da177e4SLinus Torvalds { 2466cdb3caSHerbert Xu struct flowi fl = { 25a1e59abfSPatrick McHardy .nl_u = { 26a1e59abfSPatrick McHardy .ip4_u = { 2766cdb3caSHerbert Xu .tos = tos, 28a1e59abfSPatrick McHardy .daddr = daddr->a4, 29a1e59abfSPatrick McHardy }, 30a1e59abfSPatrick McHardy }, 31a1e59abfSPatrick McHardy }; 3266cdb3caSHerbert Xu struct dst_entry *dst; 3366cdb3caSHerbert Xu struct rtable *rt; 3466cdb3caSHerbert Xu int err; 35a1e59abfSPatrick McHardy 3666cdb3caSHerbert Xu if (saddr) 3766cdb3caSHerbert Xu fl.fl4_src = saddr->a4; 3866cdb3caSHerbert Xu 39c5b3cf46SAlexey Dobriyan err = __ip_route_output_key(net, &rt, &fl); 40*d8d1f30bSChangli Gao dst = &rt->dst; 4166cdb3caSHerbert Xu if (err) 4266cdb3caSHerbert Xu dst = ERR_PTR(err); 4366cdb3caSHerbert Xu return dst; 44a1e59abfSPatrick McHardy } 4566cdb3caSHerbert Xu 46fbda33b2SAlexey Dobriyan static int xfrm4_get_saddr(struct net *net, 47fbda33b2SAlexey Dobriyan xfrm_address_t *saddr, xfrm_address_t *daddr) 4866cdb3caSHerbert Xu { 4966cdb3caSHerbert Xu struct dst_entry *dst; 5066cdb3caSHerbert Xu struct rtable *rt; 5166cdb3caSHerbert Xu 52fbda33b2SAlexey Dobriyan dst = xfrm4_dst_lookup(net, 0, NULL, daddr); 5366cdb3caSHerbert Xu if (IS_ERR(dst)) 54a1e59abfSPatrick McHardy return -EHOSTUNREACH; 5566cdb3caSHerbert Xu 5666cdb3caSHerbert Xu rt = (struct rtable *)dst; 5766cdb3caSHerbert Xu saddr->a4 = rt->rt_src; 5866cdb3caSHerbert Xu dst_release(dst); 5966cdb3caSHerbert Xu return 0; 60a1e59abfSPatrick McHardy } 61a1e59abfSPatrick McHardy 6225ee3286SHerbert Xu static int xfrm4_get_tos(struct flowi *fl) 631da177e4SLinus Torvalds { 6425ee3286SHerbert Xu return fl->fl4_tos; 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds 67a1b05140SMasahide NAKAMURA static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst, 68a1b05140SMasahide NAKAMURA int nfheader_len) 69a1b05140SMasahide NAKAMURA { 70a1b05140SMasahide NAKAMURA return 0; 71a1b05140SMasahide NAKAMURA } 72a1b05140SMasahide NAKAMURA 7387c1e12bSHerbert Xu static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, 7487c1e12bSHerbert Xu struct flowi *fl) 7525ee3286SHerbert Xu { 7625ee3286SHerbert Xu struct rtable *rt = (struct rtable *)xdst->route; 771da177e4SLinus Torvalds 7887c1e12bSHerbert Xu xdst->u.rt.fl = *fl; 791da177e4SLinus Torvalds 8025ee3286SHerbert Xu xdst->u.dst.dev = dev; 8125ee3286SHerbert Xu dev_hold(dev); 8243372262SMiika Komu 8325ee3286SHerbert Xu xdst->u.rt.idev = in_dev_get(dev); 8425ee3286SHerbert Xu if (!xdst->u.rt.idev) 8525ee3286SHerbert Xu return -ENODEV; 861da177e4SLinus Torvalds 8725ee3286SHerbert Xu xdst->u.rt.peer = rt->peer; 8825ee3286SHerbert Xu if (rt->peer) 8925ee3286SHerbert Xu atomic_inc(&rt->peer->refcnt); 9066cdb3caSHerbert Xu 911da177e4SLinus Torvalds /* Sheit... I remember I did this right. Apparently, 921da177e4SLinus Torvalds * it was magically lost, so this code needs audit */ 9325ee3286SHerbert Xu xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | 9425ee3286SHerbert Xu RTCF_LOCAL); 9525ee3286SHerbert Xu xdst->u.rt.rt_type = rt->rt_type; 9625ee3286SHerbert Xu xdst->u.rt.rt_src = rt->rt_src; 9725ee3286SHerbert Xu xdst->u.rt.rt_dst = rt->rt_dst; 9825ee3286SHerbert Xu xdst->u.rt.rt_gateway = rt->rt_gateway; 9925ee3286SHerbert Xu xdst->u.rt.rt_spec_dst = rt->rt_spec_dst; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds return 0; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static void 105d5422efeSHerbert Xu _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse) 1061da177e4SLinus Torvalds { 107eddc9ec5SArnaldo Carvalho de Melo struct iphdr *iph = ip_hdr(skb); 108d56f90a7SArnaldo Carvalho de Melo u8 *xprth = skb_network_header(skb) + iph->ihl * 4; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds memset(fl, 0, sizeof(struct flowi)); 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 122d5422efeSHerbert Xu fl->fl_ip_sport = ports[!!reverse]; 123d5422efeSHerbert Xu fl->fl_ip_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 1311da177e4SLinus Torvalds fl->fl_icmp_type = icmp[0]; 1321da177e4SLinus Torvalds fl->fl_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 1401da177e4SLinus Torvalds fl->fl_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 1481da177e4SLinus Torvalds fl->fl_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 1564195f814SAlexey Dobriyan fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1])); 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds break; 1591da177e4SLinus Torvalds default: 1601da177e4SLinus Torvalds fl->fl_ipsec_spi = 0; 1611da177e4SLinus Torvalds break; 1623ff50b79SStephen Hemminger } 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds fl->proto = iph->protocol; 165d5422efeSHerbert Xu fl->fl4_dst = reverse ? iph->saddr : iph->daddr; 166d5422efeSHerbert Xu fl->fl4_src = reverse ? iph->daddr : iph->saddr; 1674da3089fSHerbert Xu fl->fl4_tos = iph->tos; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 170569d3645SDaniel Lezcano static inline int xfrm4_garbage_collect(struct dst_ops *ops) 1711da177e4SLinus Torvalds { 172d7c7544cSAlexey Dobriyan struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops); 173d7c7544cSAlexey Dobriyan 174d7c7544cSAlexey Dobriyan xfrm4_policy_afinfo.garbage_collect(net); 175d7c7544cSAlexey Dobriyan return (atomic_read(&ops->entries) > ops->gc_thresh * 2); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 1811da177e4SLinus Torvalds struct dst_entry *path = xdst->route; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds path->ops->update_pmtu(path, mtu); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 186aabc9761SHerbert Xu static void xfrm4_dst_destroy(struct dst_entry *dst) 187aabc9761SHerbert Xu { 188aabc9761SHerbert Xu struct xfrm_dst *xdst = (struct xfrm_dst *)dst; 189aabc9761SHerbert Xu 190aabc9761SHerbert Xu if (likely(xdst->u.rt.idev)) 191aabc9761SHerbert Xu in_dev_put(xdst->u.rt.idev); 192ed3e37ddSHerbert Xu if (likely(xdst->u.rt.peer)) 19326db1677SDavid S. Miller inet_putpeer(xdst->u.rt.peer); 194aabc9761SHerbert Xu xfrm_dst_destroy(xdst); 195aabc9761SHerbert Xu } 196aabc9761SHerbert Xu 197aabc9761SHerbert Xu static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev, 198aabc9761SHerbert Xu int unregister) 199aabc9761SHerbert Xu { 200aabc9761SHerbert Xu struct xfrm_dst *xdst; 201aabc9761SHerbert Xu 202aabc9761SHerbert Xu if (!unregister) 203aabc9761SHerbert Xu return; 204aabc9761SHerbert Xu 205aabc9761SHerbert Xu xdst = (struct xfrm_dst *)dst; 206aabc9761SHerbert Xu if (xdst->u.rt.idev->dev == dev) { 2075a3e55d6SDenis V. Lunev struct in_device *loopback_idev = 208c346dca1SYOSHIFUJI Hideaki in_dev_get(dev_net(dev)->loopback_dev); 209aabc9761SHerbert Xu BUG_ON(!loopback_idev); 210aabc9761SHerbert Xu 211aabc9761SHerbert Xu do { 212aabc9761SHerbert Xu in_dev_put(xdst->u.rt.idev); 213aabc9761SHerbert Xu xdst->u.rt.idev = loopback_idev; 214aabc9761SHerbert Xu in_dev_hold(loopback_idev); 215aabc9761SHerbert Xu xdst = (struct xfrm_dst *)xdst->u.dst.child; 216aabc9761SHerbert Xu } while (xdst->u.dst.xfrm); 217aabc9761SHerbert Xu 218aabc9761SHerbert Xu __in_dev_put(loopback_idev); 219aabc9761SHerbert Xu } 220aabc9761SHerbert Xu 221aabc9761SHerbert Xu xfrm_dst_ifdown(dst, dev); 222aabc9761SHerbert Xu } 223aabc9761SHerbert Xu 2241da177e4SLinus Torvalds static struct dst_ops xfrm4_dst_ops = { 2251da177e4SLinus Torvalds .family = AF_INET, 22609640e63SHarvey Harrison .protocol = cpu_to_be16(ETH_P_IP), 2271da177e4SLinus Torvalds .gc = xfrm4_garbage_collect, 2281da177e4SLinus Torvalds .update_pmtu = xfrm4_update_pmtu, 229aabc9761SHerbert Xu .destroy = xfrm4_dst_destroy, 230aabc9761SHerbert Xu .ifdown = xfrm4_dst_ifdown, 231862b82c6SHerbert Xu .local_out = __ip_local_out, 2321da177e4SLinus Torvalds .gc_thresh = 1024, 233e2422970SEric Dumazet .entries = ATOMIC_INIT(0), 2341da177e4SLinus Torvalds }; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static struct xfrm_policy_afinfo xfrm4_policy_afinfo = { 2371da177e4SLinus Torvalds .family = AF_INET, 2381da177e4SLinus Torvalds .dst_ops = &xfrm4_dst_ops, 2391da177e4SLinus Torvalds .dst_lookup = xfrm4_dst_lookup, 240a1e59abfSPatrick McHardy .get_saddr = xfrm4_get_saddr, 2411da177e4SLinus Torvalds .decode_session = _decode_session4, 24225ee3286SHerbert Xu .get_tos = xfrm4_get_tos, 243a1b05140SMasahide NAKAMURA .init_path = xfrm4_init_path, 24425ee3286SHerbert Xu .fill_dst = xfrm4_fill_dst, 2451da177e4SLinus Torvalds }; 2461da177e4SLinus Torvalds 247f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL 248a44a4a00SNeil Horman static struct ctl_table xfrm4_policy_table[] = { 249a44a4a00SNeil Horman { 250a44a4a00SNeil Horman .procname = "xfrm4_gc_thresh", 251d7c7544cSAlexey Dobriyan .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh, 252a44a4a00SNeil Horman .maxlen = sizeof(int), 253a44a4a00SNeil Horman .mode = 0644, 254a44a4a00SNeil Horman .proc_handler = proc_dointvec, 255a44a4a00SNeil Horman }, 256a44a4a00SNeil Horman { } 257a44a4a00SNeil Horman }; 258a44a4a00SNeil Horman 259a44a4a00SNeil Horman static struct ctl_table_header *sysctl_hdr; 260f816700aSRandy Dunlap #endif 261a44a4a00SNeil Horman 2621da177e4SLinus Torvalds static void __init xfrm4_policy_init(void) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds xfrm_policy_register_afinfo(&xfrm4_policy_afinfo); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds static void __exit xfrm4_policy_fini(void) 2681da177e4SLinus Torvalds { 269f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL 270a44a4a00SNeil Horman if (sysctl_hdr) 271a44a4a00SNeil Horman unregister_net_sysctl_table(sysctl_hdr); 272f816700aSRandy Dunlap #endif 2731da177e4SLinus Torvalds xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo); 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 276a33bc5c1SNeil Horman void __init xfrm4_init(int rt_max_size) 2771da177e4SLinus Torvalds { 278a33bc5c1SNeil Horman /* 279a33bc5c1SNeil Horman * Select a default value for the gc_thresh based on the main route 280a33bc5c1SNeil Horman * table hash size. It seems to me the worst case scenario is when 281a33bc5c1SNeil Horman * we have ipsec operating in transport mode, in which we create a 282a33bc5c1SNeil Horman * dst_entry per socket. The xfrm gc algorithm starts trying to remove 283a33bc5c1SNeil Horman * entries at gc_thresh, and prevents new allocations as 2*gc_thresh 284a33bc5c1SNeil Horman * so lets set an initial xfrm gc_thresh value at the rt_max_size/2. 285a33bc5c1SNeil Horman * That will let us store an ipsec connection per route table entry, 286a33bc5c1SNeil Horman * and start cleaning when were 1/2 full 287a33bc5c1SNeil Horman */ 288a33bc5c1SNeil Horman xfrm4_dst_ops.gc_thresh = rt_max_size/2; 289d7c7544cSAlexey Dobriyan 290d7c7544cSAlexey Dobriyan xfrm4_state_init(); 291d7c7544cSAlexey Dobriyan xfrm4_policy_init(); 292f816700aSRandy Dunlap #ifdef CONFIG_SYSCTL 293a44a4a00SNeil Horman sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv4_ctl_path, 294a44a4a00SNeil Horman xfrm4_policy_table); 295f816700aSRandy Dunlap #endif 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 298