[NETNS][IPV6]: inet6_addr - make ipv6_chk_home_addr namespace aware
[powerpc.git] / net / ipv6 / reassembly.c
index ecf3400..5cd0bc6 100644 (file)
 #include <net/addrconf.h>
 #include <net/inet_frag.h>
 
-int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
-int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
-
-int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
-
 struct ip6frag_skb_cb
 {
        struct inet6_skb_parm   h;
@@ -102,20 +97,6 @@ int ip6_frag_mem(void)
 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
                          struct net_device *dev);
 
-static __inline__ void __fq_unlink(struct frag_queue *fq)
-{
-       hlist_del(&fq->q.list);
-       list_del(&fq->q.lru_list);
-       ip6_frags.nqueues--;
-}
-
-static __inline__ void fq_unlink(struct frag_queue *fq)
-{
-       write_lock(&ip6_frags.lock);
-       __fq_unlink(fq);
-       write_unlock(&ip6_frags.lock);
-}
-
 /*
  * callers should be careful not to use the hash value outside the ipfrag_lock
  * as doing so could race with ipfrag_hash_rnd being recalculated.
@@ -147,38 +128,25 @@ static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
        return c & (INETFRAGS_HASHSZ - 1);
 }
 
-int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
-
-static void ip6_frag_secret_rebuild(unsigned long dummy)
+static unsigned int ip6_hashfn(struct inet_frag_queue *q)
 {
-       unsigned long now = jiffies;
-       int i;
-
-       write_lock(&ip6_frags.lock);
-       get_random_bytes(&ip6_frags.rnd, sizeof(u32));
-       for (i = 0; i < INETFRAGS_HASHSZ; i++) {
-               struct frag_queue *q;
-               struct hlist_node *p, *n;
-
-               hlist_for_each_entry_safe(q, p, n, &ip6_frags.hash[i], q.list) {
-                       unsigned int hval = ip6qhashfn(q->id,
-                                                      &q->saddr,
-                                                      &q->daddr);
-
-                       if (hval != i) {
-                               hlist_del(&q->q.list);
+       struct frag_queue *fq;
 
-                               /* Relink to new hash chain. */
-                               hlist_add_head(&q->q.list,
-                                              &ip6_frags.hash[hval]);
+       fq = container_of(q, struct frag_queue, q);
+       return ip6qhashfn(fq->id, &fq->saddr, &fq->daddr);
+}
 
-                       }
-               }
-       }
-       write_unlock(&ip6_frags.lock);
+int ip6_frag_match(struct inet_frag_queue *q, void *a)
+{
+       struct frag_queue *fq;
+       struct ip6_create_arg *arg = a;
 
-       mod_timer(&ip6_frags.secret_timer, now + sysctl_ip6frag_secret_interval);
+       fq = container_of(q, struct frag_queue, q);
+       return (fq->id == arg->id &&
+                       ipv6_addr_equal(&fq->saddr, arg->src) &&
+                       ipv6_addr_equal(&fq->daddr, arg->dst));
 }
+EXPORT_SYMBOL(ip6_frag_match);
 
 /* Memory Tracking Functions. */
 static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
@@ -189,50 +157,22 @@ static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
        kfree_skb(skb);
 }
 
-static inline void frag_free_queue(struct frag_queue *fq, int *work)
+void ip6_frag_init(struct inet_frag_queue *q, void *a)
 {
-       if (work)
-               *work -= sizeof(struct frag_queue);
-       atomic_sub(sizeof(struct frag_queue), &ip6_frags.mem);
-       kfree(fq);
-}
-
-static inline struct frag_queue *frag_alloc_queue(void)
-{
-       struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
+       struct frag_queue *fq = container_of(q, struct frag_queue, q);
+       struct ip6_create_arg *arg = a;
 
-       if(!fq)
-               return NULL;
-       atomic_add(sizeof(struct frag_queue), &ip6_frags.mem);
-       return fq;
+       fq->id = arg->id;
+       ipv6_addr_copy(&fq->saddr, arg->src);
+       ipv6_addr_copy(&fq->daddr, arg->dst);
 }
+EXPORT_SYMBOL(ip6_frag_init);
 
 /* Destruction primitives. */
 
-/* Complete destruction of fq. */
-static void ip6_frag_destroy(struct frag_queue *fq, int *work)
+static __inline__ void fq_put(struct frag_queue *fq)
 {
-       struct sk_buff *fp;
-
-       BUG_TRAP(fq->q.last_in&COMPLETE);
-       BUG_TRAP(del_timer(&fq->q.timer) == 0);
-
-       /* Release all fragment data. */
-       fp = fq->q.fragments;
-       while (fp) {
-               struct sk_buff *xp = fp->next;
-
-               frag_kfree_skb(fp, work);
-               fp = xp;
-       }
-
-       frag_free_queue(fq, work);
-}
-
-static __inline__ void fq_put(struct frag_queue *fq, int *work)
-{
-       if (atomic_dec_and_test(&fq->q.refcnt))
-               ip6_frag_destroy(fq, work);
+       inet_frag_put(&fq->q, &ip6_frags);
 }
 
 /* Kill fq entry. It is not destroyed immediately,
@@ -240,52 +180,25 @@ static __inline__ void fq_put(struct frag_queue *fq, int *work)
  */
 static __inline__ void fq_kill(struct frag_queue *fq)
 {
-       if (del_timer(&fq->q.timer))
-               atomic_dec(&fq->q.refcnt);
-
-       if (!(fq->q.last_in & COMPLETE)) {
-               fq_unlink(fq);
-               atomic_dec(&fq->q.refcnt);
-               fq->q.last_in |= COMPLETE;
-       }
+       inet_frag_kill(&fq->q, &ip6_frags);
 }
 
 static void ip6_evictor(struct inet6_dev *idev)
 {
-       struct frag_queue *fq;
-       struct list_head *tmp;
-       int work;
-
-       work = atomic_read(&ip6_frags.mem) - sysctl_ip6frag_low_thresh;
-       if (work <= 0)
-               return;
-
-       while(work > 0) {
-               read_lock(&ip6_frags.lock);
-               if (list_empty(&ip6_frags.lru_list)) {
-                       read_unlock(&ip6_frags.lock);
-                       return;
-               }
-               tmp = ip6_frags.lru_list.next;
-               fq = list_entry(tmp, struct frag_queue, q.lru_list);
-               atomic_inc(&fq->q.refcnt);
-               read_unlock(&ip6_frags.lock);
+       int evicted;
 
-               spin_lock(&fq->q.lock);
-               if (!(fq->q.last_in&COMPLETE))
-                       fq_kill(fq);
-               spin_unlock(&fq->q.lock);
-
-               fq_put(fq, &work);
-               IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
-       }
+       evicted = inet_frag_evictor(&ip6_frags);
+       if (evicted)
+               IP6_ADD_STATS_BH(idev, IPSTATS_MIB_REASMFAILS, evicted);
 }
 
 static void ip6_frag_expire(unsigned long data)
 {
-       struct frag_queue *fq = (struct frag_queue *) data;
+       struct frag_queue *fq;
        struct net_device *dev = NULL;
 
+       fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
+
        spin_lock(&fq->q.lock);
 
        if (fq->q.last_in & COMPLETE)
@@ -317,101 +230,33 @@ out:
        if (dev)
                dev_put(dev);
        spin_unlock(&fq->q.lock);
-       fq_put(fq, NULL);
+       fq_put(fq);
 }
 
-/* Creation primitives. */
-
-
-static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
+static __inline__ struct frag_queue *
+fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
+       struct inet6_dev *idev)
 {
-       struct frag_queue *fq;
+       struct inet_frag_queue *q;
+       struct ip6_create_arg arg;
        unsigned int hash;
-#ifdef CONFIG_SMP
-       struct hlist_node *n;
-#endif
-
-       write_lock(&ip6_frags.lock);
-       hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
-#ifdef CONFIG_SMP
-       hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
-               if (fq->id == fq_in->id &&
-                   ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
-                   ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
-                       atomic_inc(&fq->q.refcnt);
-                       write_unlock(&ip6_frags.lock);
-                       fq_in->q.last_in |= COMPLETE;
-                       fq_put(fq_in, NULL);
-                       return fq;
-               }
-       }
-#endif
-       fq = fq_in;
 
-       if (!mod_timer(&fq->q.timer, jiffies + sysctl_ip6frag_time))
-               atomic_inc(&fq->q.refcnt);
-
-       atomic_inc(&fq->q.refcnt);
-       hlist_add_head(&fq->q.list, &ip6_frags.hash[hash]);
-       INIT_LIST_HEAD(&fq->q.lru_list);
-       list_add_tail(&fq->q.lru_list, &ip6_frags.lru_list);
-       ip6_frags.nqueues++;
-       write_unlock(&ip6_frags.lock);
-       return fq;
-}
-
-
-static struct frag_queue *
-ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
-               struct inet6_dev *idev)
-{
-       struct frag_queue *fq;
+       arg.id = id;
+       arg.src = src;
+       arg.dst = dst;
+       hash = ip6qhashfn(id, src, dst);
 
-       if ((fq = frag_alloc_queue()) == NULL)
+       q = inet_frag_find(&ip6_frags, &arg, hash);
+       if (q == NULL)
                goto oom;
 
-       fq->id = id;
-       ipv6_addr_copy(&fq->saddr, src);
-       ipv6_addr_copy(&fq->daddr, dst);
-
-       init_timer(&fq->q.timer);
-       fq->q.timer.function = ip6_frag_expire;
-       fq->q.timer.data = (long) fq;
-       spin_lock_init(&fq->q.lock);
-       atomic_set(&fq->q.refcnt, 1);
-
-       return ip6_frag_intern(fq);
+       return container_of(q, struct frag_queue, q);
 
 oom:
        IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
        return NULL;
 }
 
-static __inline__ struct frag_queue *
-fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
-       struct inet6_dev *idev)
-{
-       struct frag_queue *fq;
-       struct hlist_node *n;
-       unsigned int hash;
-
-       read_lock(&ip6_frags.lock);
-       hash = ip6qhashfn(id, src, dst);
-       hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
-               if (fq->id == id &&
-                   ipv6_addr_equal(src, &fq->saddr) &&
-                   ipv6_addr_equal(dst, &fq->daddr)) {
-                       atomic_inc(&fq->q.refcnt);
-                       read_unlock(&ip6_frags.lock);
-                       return fq;
-               }
-       }
-       read_unlock(&ip6_frags.lock);
-
-       return ip6_frag_create(id, src, dst, idev);
-}
-
-
 static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
                           struct frag_hdr *fhdr, int nhoff)
 {
@@ -718,9 +563,8 @@ out_fail:
        return -1;
 }
 
-static int ipv6_frag_rcv(struct sk_buff **skbp)
+static int ipv6_frag_rcv(struct sk_buff *skb)
 {
-       struct sk_buff *skb = *skbp;
        struct frag_hdr *fhdr;
        struct frag_queue *fq;
        struct ipv6hdr *hdr = ipv6_hdr(skb);
@@ -754,7 +598,7 @@ static int ipv6_frag_rcv(struct sk_buff **skbp)
                return 1;
        }
 
-       if (atomic_read(&ip6_frags.mem) > sysctl_ip6frag_high_thresh)
+       if (atomic_read(&ip6_frags.mem) > init_net.ipv6.sysctl.frags.high_thresh)
                ip6_evictor(ip6_dst_idev(skb->dst));
 
        if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
@@ -766,7 +610,7 @@ static int ipv6_frag_rcv(struct sk_buff **skbp)
                ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
 
                spin_unlock(&fq->q.lock);
-               fq_put(fq, NULL);
+               fq_put(fq);
                return ret;
        }
 
@@ -781,15 +625,33 @@ static struct inet6_protocol frag_protocol =
        .flags          =       INET6_PROTO_NOPOLICY,
 };
 
-void __init ipv6_frag_init(void)
+void ipv6_frag_sysctl_init(struct net *net)
+{
+       ip6_frags.ctl = &net->ipv6.sysctl.frags;
+}
+
+int __init ipv6_frag_init(void)
 {
-       if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
-               printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
+       int ret;
 
-       init_timer(&ip6_frags.secret_timer);
-       ip6_frags.secret_timer.function = ip6_frag_secret_rebuild;
-       ip6_frags.secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
-       add_timer(&ip6_frags.secret_timer);
+       ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
+       if (ret)
+               goto out;
 
+       ip6_frags.hashfn = ip6_hashfn;
+       ip6_frags.constructor = ip6_frag_init;
+       ip6_frags.destructor = NULL;
+       ip6_frags.skb_free = NULL;
+       ip6_frags.qsize = sizeof(struct frag_queue);
+       ip6_frags.match = ip6_frag_match;
+       ip6_frags.frag_expire = ip6_frag_expire;
        inet_frags_init(&ip6_frags);
+out:
+       return ret;
+}
+
+void ipv6_frag_exit(void)
+{
+       inet_frags_fini(&ip6_frags);
+       inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
 }