wl1251: fix a memory leak in probe
[safe/jmp/linux-2.6] / net / sched / cls_api.c
index 9eeb3c6..5fd0c28 100644 (file)
@@ -23,6 +23,8 @@
 #include <linux/init.h>
 #include <linux/kmod.h>
 #include <linux/netlink.h>
+#include <linux/err.h>
+#include <linux/slab.h>
 #include <net/net_namespace.h>
 #include <net/sock.h>
 #include <net/netlink.h>
@@ -38,14 +40,14 @@ static DEFINE_RWLOCK(cls_mod_lock);
 
 /* Find classifier type by string name */
 
-static struct tcf_proto_ops *tcf_proto_lookup_ops(struct rtattr *kind)
+static struct tcf_proto_ops *tcf_proto_lookup_ops(struct nlattr *kind)
 {
        struct tcf_proto_ops *t = NULL;
 
        if (kind) {
                read_lock(&cls_mod_lock);
                for (t = tcf_proto_base; t; t = t->next) {
-                       if (rtattr_strcmp(kind, t->kind) == 0) {
+                       if (nla_strcmp(kind, t->kind) == 0) {
                                if (!try_module_get(t->owner))
                                        t = NULL;
                                break;
@@ -97,8 +99,9 @@ out:
 }
 EXPORT_SYMBOL(unregister_tcf_proto_ops);
 
-static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
-                         struct tcf_proto *tp, unsigned long fh, int event);
+static int tfilter_notify(struct net *net, struct sk_buff *oskb,
+                         struct nlmsghdr *n, struct tcf_proto *tp,
+                         unsigned long fh, int event);
 
 
 /* Select new prio value from the range, managed by kernel. */
@@ -117,8 +120,9 @@ static inline u32 tcf_auto_prio(struct tcf_proto *tp)
 
 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
 {
-       struct net *net = skb->sk->sk_net;
-       struct rtattr **tca;
+       struct net *net = sock_net(skb->sk);
+       struct nlattr *tca[TCA_MAX + 1];
+       spinlock_t *root_lock;
        struct tcmsg *t;
        u32 protocol;
        u32 prio;
@@ -133,12 +137,9 @@ static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
        unsigned long cl;
        unsigned long fh;
        int err;
-
-       if (net != &init_net)
-               return -EINVAL;
+       int tp_created = 0;
 
 replay:
-       tca = arg;
        t = NLMSG_DATA(n);
        protocol = TC_H_MIN(t->tcm_info);
        prio = TC_H_MAJ(t->tcm_info);
@@ -156,13 +157,17 @@ replay:
        /* Find head of filter chain. */
 
        /* Find link */
-       dev = __dev_get_by_index(&init_net, t->tcm_ifindex);
+       dev = __dev_get_by_index(net, t->tcm_ifindex);
        if (dev == NULL)
                return -ENODEV;
 
+       err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
+       if (err < 0)
+               return err;
+
        /* Find qdisc */
        if (!parent) {
-               q = dev->qdisc_sleeping;
+               q = dev->qdisc;
                parent = q->handle;
        } else {
                q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
@@ -174,6 +179,9 @@ replay:
        if ((cops = q->ops->cl_ops) == NULL)
                return -EINVAL;
 
+       if (cops->tcf_chain == NULL)
+               return -EOPNOTSUPP;
+
        /* Do we search for filter, attached to class? */
        if (TC_H_MIN(parent)) {
                cl = cops->get(q, parent);
@@ -199,10 +207,12 @@ replay:
                }
        }
 
+       root_lock = qdisc_root_sleeping_lock(q);
+
        if (tp == NULL) {
                /* Proto-tcf does not exist, create new one */
 
-               if (tca[TCA_KIND-1] == NULL || !protocol)
+               if (tca[TCA_KIND] == NULL || !protocol)
                        goto errout;
 
                err = -ENOENT;
@@ -216,15 +226,15 @@ replay:
                tp = kzalloc(sizeof(*tp), GFP_KERNEL);
                if (tp == NULL)
                        goto errout;
-               err = -EINVAL;
-               tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
+               err = -ENOENT;
+               tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
                if (tp_ops == NULL) {
-#ifdef CONFIG_KMOD
-                       struct rtattr *kind = tca[TCA_KIND-1];
+#ifdef CONFIG_MODULES
+                       struct nlattr *kind = tca[TCA_KIND];
                        char name[IFNAMSIZ];
 
                        if (kind != NULL &&
-                           rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
+                           nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
                                rtnl_unlock();
                                request_module("cls_%s", name);
                                rtnl_lock();
@@ -246,7 +256,7 @@ replay:
                }
                tp->ops = tp_ops;
                tp->protocol = protocol;
-               tp->prio = nprio ? : tcf_auto_prio(*back);
+               tp->prio = nprio ? : TC_H_MAJ(tcf_auto_prio(*back));
                tp->q = q;
                tp->classify = tp_ops->classify;
                tp->classid = parent;
@@ -258,23 +268,20 @@ replay:
                        goto errout;
                }
 
-               qdisc_lock_tree(dev);
-               tp->next = *back;
-               *back = tp;
-               qdisc_unlock_tree(dev);
+               tp_created = 1;
 
-       } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
+       } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind))
                goto errout;
 
        fh = tp->ops->get(tp, t->tcm_handle);
 
        if (fh == 0) {
                if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
-                       qdisc_lock_tree(dev);
+                       spin_lock_bh(root_lock);
                        *back = tp->next;
-                       qdisc_unlock_tree(dev);
+                       spin_unlock_bh(root_lock);
 
-                       tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
+                       tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
                        tcf_destroy(tp);
                        err = 0;
                        goto errout;
@@ -288,16 +295,19 @@ replay:
                switch (n->nlmsg_type) {
                case RTM_NEWTFILTER:
                        err = -EEXIST;
-                       if (n->nlmsg_flags & NLM_F_EXCL)
+                       if (n->nlmsg_flags & NLM_F_EXCL) {
+                               if (tp_created)
+                                       tcf_destroy(tp);
                                goto errout;
+                       }
                        break;
                case RTM_DELTFILTER:
                        err = tp->ops->delete(tp, fh);
                        if (err == 0)
-                               tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
+                               tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
                        goto errout;
                case RTM_GETTFILTER:
-                       err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
+                       err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
                        goto errout;
                default:
                        err = -EINVAL;
@@ -306,8 +316,18 @@ replay:
        }
 
        err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
-       if (err == 0)
-               tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
+       if (err == 0) {
+               if (tp_created) {
+                       spin_lock_bh(root_lock);
+                       tp->next = *back;
+                       *back = tp;
+                       spin_unlock_bh(root_lock);
+               }
+               tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
+       } else {
+               if (tp_created)
+                       tcf_destroy(tp);
+       }
 
 errout:
        if (cl)
@@ -329,28 +349,29 @@ static int tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp,
        tcm = NLMSG_DATA(nlh);
        tcm->tcm_family = AF_UNSPEC;
        tcm->tcm__pad1 = 0;
-       tcm->tcm__pad1 = 0;
-       tcm->tcm_ifindex = tp->q->dev->ifindex;
+       tcm->tcm__pad2 = 0;
+       tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
        tcm->tcm_parent = tp->classid;
        tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
-       RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
+       NLA_PUT_STRING(skb, TCA_KIND, tp->ops->kind);
        tcm->tcm_handle = fh;
        if (RTM_DELTFILTER != event) {
                tcm->tcm_handle = 0;
                if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
-                       goto rtattr_failure;
+                       goto nla_put_failure;
        }
        nlh->nlmsg_len = skb_tail_pointer(skb) - b;
        return skb->len;
 
 nlmsg_failure:
-rtattr_failure:
+nla_put_failure:
        nlmsg_trim(skb, b);
        return -1;
 }
 
-static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
-                         struct tcf_proto *tp, unsigned long fh, int event)
+static int tfilter_notify(struct net *net, struct sk_buff *oskb,
+                         struct nlmsghdr *n, struct tcf_proto *tp,
+                         unsigned long fh, int event)
 {
        struct sk_buff *skb;
        u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
@@ -364,7 +385,7 @@ static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
                return -EINVAL;
        }
 
-       return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
+       return rtnetlink_send(skb, net, pid, RTNLGRP_TC,
                              n->nlmsg_flags & NLM_F_ECHO);
 }
 
@@ -383,9 +404,10 @@ static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
                             a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
 }
 
+/* called with RTNL */
 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
 {
-       struct net *net = skb->sk->sk_net;
+       struct net *net = sock_net(skb->sk);
        int t;
        int s_t;
        struct net_device *dev;
@@ -396,22 +418,21 @@ static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
        const struct Qdisc_class_ops *cops;
        struct tcf_dump_args arg;
 
-       if (net != &init_net)
-               return 0;
-
        if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
                return skb->len;
-       if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
+       if ((dev = __dev_get_by_index(net, tcm->tcm_ifindex)) == NULL)
                return skb->len;
 
        if (!tcm->tcm_parent)
-               q = dev->qdisc_sleeping;
+               q = dev->qdisc;
        else
                q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
        if (!q)
                goto out;
        if ((cops = q->ops->cl_ops) == NULL)
                goto errout;
+       if (cops->tcf_chain == NULL)
+               goto errout;
        if (TC_H_MIN(tcm->tcm_parent)) {
                cl = cops->get(q, tcm->tcm_parent);
                if (cl == 0)
@@ -461,7 +482,6 @@ errout:
        if (cl)
                cops->put(q, cl);
 out:
-       dev_put(dev);
        return skb->len;
 }
 
@@ -476,38 +496,37 @@ void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
 }
 EXPORT_SYMBOL(tcf_exts_destroy);
 
-int tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb,
-                 struct rtattr *rate_tlv, struct tcf_exts *exts,
-                 struct tcf_ext_map *map)
+int tcf_exts_validate(struct tcf_proto *tp, struct nlattr **tb,
+                 struct nlattr *rate_tlv, struct tcf_exts *exts,
+                 const struct tcf_ext_map *map)
 {
        memset(exts, 0, sizeof(*exts));
 
 #ifdef CONFIG_NET_CLS_ACT
        {
-               int err;
                struct tc_action *act;
 
-               if (map->police && tb[map->police-1]) {
-                       act = tcf_action_init_1(tb[map->police-1], rate_tlv,
+               if (map->police && tb[map->police]) {
+                       act = tcf_action_init_1(tb[map->police], rate_tlv,
                                                "police", TCA_ACT_NOREPLACE,
-                                               TCA_ACT_BIND, &err);
-                       if (act == NULL)
-                               return err;
+                                               TCA_ACT_BIND);
+                       if (IS_ERR(act))
+                               return PTR_ERR(act);
 
                        act->type = TCA_OLD_COMPAT;
                        exts->action = act;
-               } else if (map->action && tb[map->action-1]) {
-                       act = tcf_action_init(tb[map->action-1], rate_tlv, NULL,
-                               TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
-                       if (act == NULL)
-                               return err;
+               } else if (map->action && tb[map->action]) {
+                       act = tcf_action_init(tb[map->action], rate_tlv, NULL,
+                                             TCA_ACT_NOREPLACE, TCA_ACT_BIND);
+                       if (IS_ERR(act))
+                               return PTR_ERR(act);
 
                        exts->action = act;
                }
        }
 #else
-       if ((map->action && tb[map->action-1]) ||
-           (map->police && tb[map->police-1]))
+       if ((map->action && tb[map->action]) ||
+           (map->police && tb[map->police]))
                return -EOPNOTSUPP;
 #endif
 
@@ -522,7 +541,8 @@ void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
        if (src->action) {
                struct tc_action *act;
                tcf_tree_lock(tp);
-               act = xchg(&dst->action, src->action);
+               act = dst->action;
+               dst->action = src->action;
                tcf_tree_unlock(tp);
                if (act)
                        tcf_action_destroy(act, TCA_ACT_UNBIND);
@@ -532,7 +552,7 @@ void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
 EXPORT_SYMBOL(tcf_exts_change);
 
 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
-             struct tcf_ext_map *map)
+                 const struct tcf_ext_map *map)
 {
 #ifdef CONFIG_NET_CLS_ACT
        if (map->action && exts->action) {
@@ -541,38 +561,42 @@ int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
                 * to work with both old and new modes of entering
                 * tc data even if iproute2  was newer - jhs
                 */
-               struct rtattr *p_rta = (struct rtattr *)skb_tail_pointer(skb);
+               struct nlattr *nest;
 
                if (exts->action->type != TCA_OLD_COMPAT) {
-                       RTA_PUT(skb, map->action, 0, NULL);
+                       nest = nla_nest_start(skb, map->action);
+                       if (nest == NULL)
+                               goto nla_put_failure;
                        if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
-                               goto rtattr_failure;
-                       p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
+                               goto nla_put_failure;
+                       nla_nest_end(skb, nest);
                } else if (map->police) {
-                       RTA_PUT(skb, map->police, 0, NULL);
+                       nest = nla_nest_start(skb, map->police);
+                       if (nest == NULL)
+                               goto nla_put_failure;
                        if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
-                               goto rtattr_failure;
-                       p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
+                               goto nla_put_failure;
+                       nla_nest_end(skb, nest);
                }
        }
 #endif
        return 0;
-rtattr_failure: __attribute__ ((unused))
+nla_put_failure: __attribute__ ((unused))
        return -1;
 }
 EXPORT_SYMBOL(tcf_exts_dump);
 
 
 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
-                       struct tcf_ext_map *map)
+                       const struct tcf_ext_map *map)
 {
 #ifdef CONFIG_NET_CLS_ACT
        if (exts->action)
                if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
-                       goto rtattr_failure;
+                       goto nla_put_failure;
 #endif
        return 0;
-rtattr_failure: __attribute__ ((unused))
+nla_put_failure: __attribute__ ((unused))
        return -1;
 }
 EXPORT_SYMBOL(tcf_exts_dump_stats);