Merge branch 'linus' into cont_syslog
[safe/jmp/linux-2.6] / net / ipv6 / xfrm6_policy.c
index 3b67ce7..4a0e77e 100644 (file)
 #include <net/mip6.h>
 #endif
 
-static struct dst_ops xfrm6_dst_ops;
 static struct xfrm_policy_afinfo xfrm6_policy_afinfo;
 
-static struct dst_entry *xfrm6_dst_lookup(int tos, xfrm_address_t *saddr,
+static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos,
+                                         xfrm_address_t *saddr,
                                          xfrm_address_t *daddr)
 {
        struct flowi fl = {};
@@ -38,7 +38,7 @@ static struct dst_entry *xfrm6_dst_lookup(int tos, xfrm_address_t *saddr,
        if (saddr)
                memcpy(&fl.fl6_src, saddr, sizeof(fl.fl6_src));
 
-       dst = ip6_route_output(&init_net, NULL, &fl);
+       dst = ip6_route_output(net, NULL, &fl);
 
        err = dst->error;
        if (dst->error) {
@@ -49,12 +49,13 @@ static struct dst_entry *xfrm6_dst_lookup(int tos, xfrm_address_t *saddr,
        return dst;
 }
 
-static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
+static int xfrm6_get_saddr(struct net *net,
+                          xfrm_address_t *saddr, xfrm_address_t *daddr)
 {
        struct dst_entry *dst;
        struct net_device *dev;
 
-       dst = xfrm6_dst_lookup(0, NULL, daddr);
+       dst = xfrm6_dst_lookup(net, 0, NULL, daddr);
        if (IS_ERR(dst))
                return -EHOSTUNREACH;
 
@@ -66,36 +67,6 @@ static int xfrm6_get_saddr(xfrm_address_t *saddr, xfrm_address_t *daddr)
        return 0;
 }
 
-static struct dst_entry *
-__xfrm6_find_bundle(struct flowi *fl, struct xfrm_policy *policy)
-{
-       struct dst_entry *dst;
-
-       /* Still not clear if we should set fl->fl6_{src,dst}... */
-       read_lock_bh(&policy->lock);
-       for (dst = policy->bundles; dst; dst = dst->next) {
-               struct xfrm_dst *xdst = (struct xfrm_dst*)dst;
-               struct in6_addr fl_dst_prefix, fl_src_prefix;
-
-               ipv6_addr_prefix(&fl_dst_prefix,
-                                &fl->fl6_dst,
-                                xdst->u.rt6.rt6i_dst.plen);
-               ipv6_addr_prefix(&fl_src_prefix,
-                                &fl->fl6_src,
-                                xdst->u.rt6.rt6i_src.plen);
-               if (ipv6_addr_equal(&xdst->u.rt6.rt6i_dst.addr, &fl_dst_prefix) &&
-                   ipv6_addr_equal(&xdst->u.rt6.rt6i_src.addr, &fl_src_prefix) &&
-                   xfrm_bundle_ok(policy, xdst, fl, AF_INET6,
-                                  (xdst->u.rt6.rt6i_dst.plen != 128 ||
-                                   xdst->u.rt6.rt6i_src.plen != 128))) {
-                       dst_clone(dst);
-                       break;
-               }
-       }
-       read_unlock_bh(&policy->lock);
-       return dst;
-}
-
 static int xfrm6_get_tos(struct flowi *fl)
 {
        return 0;
@@ -115,14 +86,15 @@ static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst,
        return 0;
 }
 
-static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
+static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
+                         struct flowi *fl)
 {
        struct rt6_info *rt = (struct rt6_info*)xdst->route;
 
        xdst->u.dst.dev = dev;
        dev_hold(dev);
 
-       xdst->u.rt6.rt6i_idev = in6_dev_get(rt->u.dst.dev);
+       xdst->u.rt6.rt6i_idev = in6_dev_get(dev);
        if (!xdst->u.rt6.rt6i_idev)
                return -ENODEV;
 
@@ -155,7 +127,8 @@ _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
        ipv6_addr_copy(&fl->fl6_dst, reverse ? &hdr->saddr : &hdr->daddr);
        ipv6_addr_copy(&fl->fl6_src, reverse ? &hdr->daddr : &hdr->saddr);
 
-       while (pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
+       while (nh + offset + 1 < skb->data ||
+              pskb_may_pull(skb, nh + offset + 1 - skb->data)) {
                nh = skb_network_header(skb);
                exthdr = (struct ipv6_opt_hdr *)(nh + offset);
 
@@ -175,7 +148,8 @@ _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
                case IPPROTO_TCP:
                case IPPROTO_SCTP:
                case IPPROTO_DCCP:
-                       if (!onlyproto && pskb_may_pull(skb, nh + offset + 4 - skb->data)) {
+                       if (!onlyproto && (nh + offset + 4 < skb->data ||
+                            pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
                                __be16 *ports = (__be16 *)exthdr;
 
                                fl->fl_ip_sport = ports[!!reverse];
@@ -220,8 +194,10 @@ _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
 
 static inline int xfrm6_garbage_collect(struct dst_ops *ops)
 {
-       xfrm6_policy_afinfo.garbage_collect();
-       return (atomic_read(&xfrm6_dst_ops.entries) > xfrm6_dst_ops.gc_thresh*2);
+       struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops);
+
+       xfrm6_policy_afinfo.garbage_collect(net);
+       return (atomic_read(&ops->entries) > ops->gc_thresh * 2);
 }
 
 static void xfrm6_update_pmtu(struct dst_entry *dst, u32 mtu)
@@ -270,7 +246,7 @@ static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
 
 static struct dst_ops xfrm6_dst_ops = {
        .family =               AF_INET6,
-       .protocol =             __constant_htons(ETH_P_IPV6),
+       .protocol =             cpu_to_be16(ETH_P_IPV6),
        .gc =                   xfrm6_garbage_collect,
        .update_pmtu =          xfrm6_update_pmtu,
        .destroy =              xfrm6_dst_destroy,
@@ -285,7 +261,6 @@ static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
        .dst_ops =              &xfrm6_dst_ops,
        .dst_lookup =           xfrm6_dst_lookup,
        .get_saddr =            xfrm6_get_saddr,
-       .find_bundle =          __xfrm6_find_bundle,
        .decode_session =       _decode_session6,
        .get_tos =              xfrm6_get_tos,
        .init_path =            xfrm6_init_path,
@@ -302,9 +277,39 @@ static void xfrm6_policy_fini(void)
        xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo);
 }
 
+#ifdef CONFIG_SYSCTL
+static struct ctl_table xfrm6_policy_table[] = {
+       {
+               .procname       = "xfrm6_gc_thresh",
+               .data           = &init_net.xfrm.xfrm6_dst_ops.gc_thresh,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = proc_dointvec,
+       },
+       { }
+};
+
+static struct ctl_table_header *sysctl_hdr;
+#endif
+
 int __init xfrm6_init(void)
 {
        int ret;
+       unsigned int gc_thresh;
+
+       /*
+        * We need a good default value for the xfrm6 gc threshold.
+        * In ipv4 we set it to the route hash table size * 8, which
+        * is half the size of the maximaum route cache for ipv4.  It
+        * would be good to do the same thing for v6, except the table is
+        * constructed differently here.  Here each table for a net namespace
+        * can have FIB_TABLE_HASHSZ entries, so lets go with the same
+        * computation that we used for ipv4 here.  Also, lets keep the initial
+        * gc_thresh to a minimum of 1024, since, the ipv6 route cache defaults
+        * to that as a minimum as well
+        */
+       gc_thresh = FIB6_TABLE_HASHSZ * 8;
+       xfrm6_dst_ops.gc_thresh = (gc_thresh < 1024) ? 1024 : gc_thresh;
 
        ret = xfrm6_policy_init();
        if (ret)
@@ -313,6 +318,11 @@ int __init xfrm6_init(void)
        ret = xfrm6_state_init();
        if (ret)
                goto out_policy;
+
+#ifdef CONFIG_SYSCTL
+       sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv6_ctl_path,
+                                               xfrm6_policy_table);
+#endif
 out:
        return ret;
 out_policy:
@@ -322,6 +332,10 @@ out_policy:
 
 void xfrm6_fini(void)
 {
+#ifdef CONFIG_SYSCTL
+       if (sysctl_hdr)
+               unregister_net_sysctl_table(sysctl_hdr);
+#endif
        //xfrm6_input_fini();
        xfrm6_policy_fini();
        xfrm6_state_fini();