netns xfrm: deal with dst entries in netns
[safe/jmp/linux-2.6] / net / ipv4 / xfrm4_policy.c
index 2ad24ba..67107d6 100644 (file)
@@ -15,7 +15,6 @@
 #include <net/xfrm.h>
 #include <net/ip.h>
 
-static struct dst_ops xfrm4_dst_ops;
 static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
 
 static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos,
@@ -136,7 +135,8 @@ _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
                case IPPROTO_TCP:
                case IPPROTO_SCTP:
                case IPPROTO_DCCP:
-                       if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
+                       if (xprth + 4 < skb->data ||
+                           pskb_may_pull(skb, xprth + 4 - skb->data)) {
                                __be16 *ports = (__be16 *)xprth;
 
                                fl->fl_ip_sport = ports[!!reverse];
@@ -189,8 +189,10 @@ _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
 
 static inline int xfrm4_garbage_collect(struct dst_ops *ops)
 {
-       xfrm4_policy_afinfo.garbage_collect(&init_net);
-       return (atomic_read(&xfrm4_dst_ops.entries) > xfrm4_dst_ops.gc_thresh*2);
+       struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
+
+       xfrm4_policy_afinfo.garbage_collect(net);
+       return (atomic_read(&ops->entries) > ops->gc_thresh * 2);
 }
 
 static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
@@ -241,7 +243,7 @@ static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
 
 static struct dst_ops xfrm4_dst_ops = {
        .family =               AF_INET,
-       .protocol =             __constant_htons(ETH_P_IP),
+       .protocol =             cpu_to_be16(ETH_P_IP),
        .gc =                   xfrm4_garbage_collect,
        .update_pmtu =          xfrm4_update_pmtu,
        .destroy =              xfrm4_dst_destroy,
@@ -263,6 +265,21 @@ static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
        .fill_dst =             xfrm4_fill_dst,
 };
 
+#ifdef CONFIG_SYSCTL
+static struct ctl_table xfrm4_policy_table[] = {
+       {
+               .procname       = "xfrm4_gc_thresh",
+               .data           = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = proc_dointvec,
+       },
+       { }
+};
+
+static struct ctl_table_header *sysctl_hdr;
+#endif
+
 static void __init xfrm4_policy_init(void)
 {
        xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
@@ -270,12 +287,32 @@ static void __init xfrm4_policy_init(void)
 
 static void __exit xfrm4_policy_fini(void)
 {
+#ifdef CONFIG_SYSCTL
+       if (sysctl_hdr)
+               unregister_net_sysctl_table(sysctl_hdr);
+#endif
        xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
 }
 
-void __init xfrm4_init(void)
+void __init xfrm4_init(int rt_max_size)
 {
+       /*
+        * Select a default value for the gc_thresh based on the main route
+        * table hash size.  It seems to me the worst case scenario is when
+        * we have ipsec operating in transport mode, in which we create a
+        * dst_entry per socket.  The xfrm gc algorithm starts trying to remove
+        * entries at gc_thresh, and prevents new allocations as 2*gc_thresh
+        * so lets set an initial xfrm gc_thresh value at the rt_max_size/2.
+        * That will let us store an ipsec connection per route table entry,
+        * and start cleaning when were 1/2 full
+        */
+       xfrm4_dst_ops.gc_thresh = rt_max_size/2;
+
        xfrm4_state_init();
        xfrm4_policy_init();
+#ifdef CONFIG_SYSCTL
+       sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv4_ctl_path,
+                                               xfrm4_policy_table);
+#endif
 }