net/sched: act_pedit: remove extra check for key type
[platform/kernel/linux-starfive.git] / net / sched / act_pedit.c
index 655addb..48e14cb 100644 (file)
@@ -180,26 +180,6 @@ static int tcf_pedit_init(struct net *net, struct nlattr *nla,
        }
 
        parm = nla_data(pattr);
-       if (!parm->nkeys) {
-               NL_SET_ERR_MSG_MOD(extack, "Pedit requires keys to be passed");
-               return -EINVAL;
-       }
-       ksize = parm->nkeys * sizeof(struct tc_pedit_key);
-       if (nla_len(pattr) < sizeof(*parm) + ksize) {
-               NL_SET_ERR_MSG_ATTR(extack, pattr, "Length of TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute is invalid");
-               return -EINVAL;
-       }
-
-       nparms = kzalloc(sizeof(*nparms), GFP_KERNEL);
-       if (!nparms)
-               return -ENOMEM;
-
-       nparms->tcfp_keys_ex =
-               tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
-       if (IS_ERR(nparms->tcfp_keys_ex)) {
-               ret = PTR_ERR(nparms->tcfp_keys_ex);
-               goto out_free;
-       }
 
        index = parm->index;
        err = tcf_idr_check_alloc(tn, &index, a, bind);
@@ -208,25 +188,49 @@ static int tcf_pedit_init(struct net *net, struct nlattr *nla,
                                                &act_pedit_ops, bind, flags);
                if (ret) {
                        tcf_idr_cleanup(tn, index);
-                       goto out_free_ex;
+                       return ret;
                }
                ret = ACT_P_CREATED;
        } else if (err > 0) {
                if (bind)
-                       goto out_free;
+                       return 0;
                if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
                        ret = -EEXIST;
                        goto out_release;
                }
        } else {
-               ret = err;
-               goto out_free_ex;
+               return err;
+       }
+
+       if (!parm->nkeys) {
+               NL_SET_ERR_MSG_MOD(extack, "Pedit requires keys to be passed");
+               ret = -EINVAL;
+               goto out_release;
+       }
+       ksize = parm->nkeys * sizeof(struct tc_pedit_key);
+       if (nla_len(pattr) < sizeof(*parm) + ksize) {
+               NL_SET_ERR_MSG_ATTR(extack, pattr, "Length of TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute is invalid");
+               ret = -EINVAL;
+               goto out_release;
+       }
+
+       nparms = kzalloc(sizeof(*nparms), GFP_KERNEL);
+       if (!nparms) {
+               ret = -ENOMEM;
+               goto out_release;
+       }
+
+       nparms->tcfp_keys_ex =
+               tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
+       if (IS_ERR(nparms->tcfp_keys_ex)) {
+               ret = PTR_ERR(nparms->tcfp_keys_ex);
+               goto out_free;
        }
 
        err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
        if (err < 0) {
                ret = err;
-               goto out_release;
+               goto out_free_ex;
        }
 
        nparms->tcfp_off_max_hint = 0;
@@ -277,12 +281,12 @@ static int tcf_pedit_init(struct net *net, struct nlattr *nla,
 put_chain:
        if (goto_ch)
                tcf_chain_put_by_act(goto_ch);
-out_release:
-       tcf_idr_release(*a, bind);
 out_free_ex:
        kfree(nparms->tcfp_keys_ex);
 out_free:
        kfree(nparms);
+out_release:
+       tcf_idr_release(*a, bind);
        return ret;
 }
 
@@ -308,44 +312,39 @@ static bool offset_valid(struct sk_buff *skb, int offset)
        return true;
 }
 
-static int pedit_skb_hdr_offset(struct sk_buff *skb,
-                               enum pedit_header_type htype, int *hoffset)
+static void pedit_skb_hdr_offset(struct sk_buff *skb,
+                                enum pedit_header_type htype, int *hoffset)
 {
-       int ret = -EINVAL;
-
+       /* 'htype' is validated in the netlink parsing */
        switch (htype) {
        case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
-               if (skb_mac_header_was_set(skb)) {
+               if (skb_mac_header_was_set(skb))
                        *hoffset = skb_mac_offset(skb);
-                       ret = 0;
-               }
                break;
        case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
        case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
        case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
                *hoffset = skb_network_offset(skb);
-               ret = 0;
                break;
        case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
        case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
-               if (skb_transport_header_was_set(skb)) {
+               if (skb_transport_header_was_set(skb))
                        *hoffset = skb_transport_offset(skb);
-                       ret = 0;
-               }
                break;
        default:
-               ret = -EINVAL;
                break;
        }
-
-       return ret;
 }
 
 static int tcf_pedit_act(struct sk_buff *skb, const struct tc_action *a,
                         struct tcf_result *res)
 {
+       enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
+       enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
        struct tcf_pedit *p = to_pedit(a);
+       struct tcf_pedit_key_ex *tkey_ex;
        struct tcf_pedit_parms *parms;
+       struct tc_pedit_key *tkey;
        u32 max_offset;
        int i;
 
@@ -361,88 +360,75 @@ static int tcf_pedit_act(struct sk_buff *skb, const struct tc_action *a,
        tcf_lastuse_update(&p->tcf_tm);
        tcf_action_update_bstats(&p->common, skb);
 
-       if (parms->tcfp_nkeys > 0) {
-               struct tc_pedit_key *tkey = parms->tcfp_keys;
-               struct tcf_pedit_key_ex *tkey_ex = parms->tcfp_keys_ex;
-               enum pedit_header_type htype =
-                       TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
-               enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
-
-               for (i = parms->tcfp_nkeys; i > 0; i--, tkey++) {
-                       u32 *ptr, hdata;
-                       int offset = tkey->off;
-                       int hoffset;
-                       u32 val;
-                       int rc;
-
-                       if (tkey_ex) {
-                               htype = tkey_ex->htype;
-                               cmd = tkey_ex->cmd;
-
-                               tkey_ex++;
-                       }
+       tkey = parms->tcfp_keys;
+       tkey_ex = parms->tcfp_keys_ex;
 
-                       rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
-                       if (rc) {
-                               pr_info("tc action pedit bad header type specified (0x%x)\n",
-                                       htype);
-                               goto bad;
-                       }
+       for (i = parms->tcfp_nkeys; i > 0; i--, tkey++) {
+               int offset = tkey->off;
+               int hoffset = 0;
+               u32 *ptr, hdata;
+               u32 val;
 
-                       if (tkey->offmask) {
-                               u8 *d, _d;
-
-                               if (!offset_valid(skb, hoffset + tkey->at)) {
-                                       pr_info("tc action pedit 'at' offset %d out of bounds\n",
-                                               hoffset + tkey->at);
-                                       goto bad;
-                               }
-                               d = skb_header_pointer(skb, hoffset + tkey->at,
-                                                      sizeof(_d), &_d);
-                               if (!d)
-                                       goto bad;
-                               offset += (*d & tkey->offmask) >> tkey->shift;
-                       }
+               if (tkey_ex) {
+                       htype = tkey_ex->htype;
+                       cmd = tkey_ex->cmd;
 
-                       if (offset % 4) {
-                               pr_info("tc action pedit offset must be on 32 bit boundaries\n");
-                               goto bad;
-                       }
+                       tkey_ex++;
+               }
 
-                       if (!offset_valid(skb, hoffset + offset)) {
-                               pr_info("tc action pedit offset %d out of bounds\n",
-                                       hoffset + offset);
-                               goto bad;
-                       }
+               pedit_skb_hdr_offset(skb, htype, &hoffset);
 
-                       ptr = skb_header_pointer(skb, hoffset + offset,
-                                                sizeof(hdata), &hdata);
-                       if (!ptr)
-                               goto bad;
-                       /* just do it, baby */
-                       switch (cmd) {
-                       case TCA_PEDIT_KEY_EX_CMD_SET:
-                               val = tkey->val;
-                               break;
-                       case TCA_PEDIT_KEY_EX_CMD_ADD:
-                               val = (*ptr + tkey->val) & ~tkey->mask;
-                               break;
-                       default:
-                               pr_info("tc action pedit bad command (%d)\n",
-                                       cmd);
+               if (tkey->offmask) {
+                       u8 *d, _d;
+
+                       if (!offset_valid(skb, hoffset + tkey->at)) {
+                               pr_info("tc action pedit 'at' offset %d out of bounds\n",
+                                       hoffset + tkey->at);
                                goto bad;
                        }
+                       d = skb_header_pointer(skb, hoffset + tkey->at,
+                                              sizeof(_d), &_d);
+                       if (!d)
+                               goto bad;
+                       offset += (*d & tkey->offmask) >> tkey->shift;
+               }
 
-                       *ptr = ((*ptr & tkey->mask) ^ val);
-                       if (ptr == &hdata)
-                               skb_store_bits(skb, hoffset + offset, ptr, 4);
+               if (offset % 4) {
+                       pr_info("tc action pedit offset must be on 32 bit boundaries\n");
+                       goto bad;
                }
 
-               goto done;
-       } else {
-               WARN(1, "pedit BUG: index %d\n", p->tcf_index);
+               if (!offset_valid(skb, hoffset + offset)) {
+                       pr_info("tc action pedit offset %d out of bounds\n",
+                               hoffset + offset);
+                       goto bad;
+               }
+
+               ptr = skb_header_pointer(skb, hoffset + offset,
+                                        sizeof(hdata), &hdata);
+               if (!ptr)
+                       goto bad;
+               /* just do it, baby */
+               switch (cmd) {
+               case TCA_PEDIT_KEY_EX_CMD_SET:
+                       val = tkey->val;
+                       break;
+               case TCA_PEDIT_KEY_EX_CMD_ADD:
+                       val = (*ptr + tkey->val) & ~tkey->mask;
+                       break;
+               default:
+                       pr_info("tc action pedit bad command (%d)\n",
+                               cmd);
+                       goto bad;
+               }
+
+               *ptr = ((*ptr & tkey->mask) ^ val);
+               if (ptr == &hdata)
+                       skb_store_bits(skb, hoffset + offset, ptr, 4);
        }
 
+       goto done;
+
 bad:
        spin_lock(&p->tcf_lock);
        p->tcf_qstats.overlimits++;