net/sched: act_pedit: Add size check for TCA_PEDIT_PARMS_EX
[platform/kernel/linux-starfive.git] / net / sched / act_pedit.c
index 238759c..aee2e13 100644 (file)
 #include <linux/rtnetlink.h>
 #include <linux/module.h>
 #include <linux/init.h>
+#include <linux/ip.h>
+#include <linux/ipv6.h>
 #include <linux/slab.h>
+#include <net/ipv6.h>
 #include <net/netlink.h>
 #include <net/pkt_sched.h>
 #include <linux/tc_act/tc_pedit.h>
@@ -25,6 +28,7 @@ static struct tc_action_ops act_pedit_ops;
 
 static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
        [TCA_PEDIT_PARMS]       = { .len = sizeof(struct tc_pedit) },
+       [TCA_PEDIT_PARMS_EX]    = { .len = sizeof(struct tc_pedit) },
        [TCA_PEDIT_KEYS_EX]   = { .type = NLA_NESTED },
 };
 
@@ -312,11 +316,35 @@ 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 int pedit_l4_skb_offset(struct sk_buff *skb, int *hoffset, const int header_type)
 {
+       const int noff = skb_network_offset(skb);
        int ret = -EINVAL;
+       struct iphdr _iph;
+
+       switch (skb->protocol) {
+       case htons(ETH_P_IP): {
+               const struct iphdr *iph = skb_header_pointer(skb, noff, sizeof(_iph), &_iph);
 
+               if (!iph)
+                       goto out;
+               *hoffset = noff + iph->ihl * 4;
+               ret = 0;
+               break;
+       }
+       case htons(ETH_P_IPV6):
+               ret = ipv6_find_hdr(skb, hoffset, header_type, NULL, NULL) == header_type ? 0 : -EINVAL;
+               break;
+       }
+out:
+       return ret;
+}
+
+static int 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)) {
@@ -331,25 +359,26 @@ static int pedit_skb_hdr_offset(struct sk_buff *skb,
                ret = 0;
                break;
        case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
+               ret = pedit_l4_skb_offset(skb, hoffset, IPPROTO_TCP);
+               break;
        case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
-               if (skb_transport_header_was_set(skb)) {
-                       *hoffset = skb_transport_offset(skb);
-                       ret = 0;
-               }
+               ret = pedit_l4_skb_offset(skb, hoffset, IPPROTO_UDP);
                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;
 
@@ -365,88 +394,80 @@ 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;
+               int rc;
 
-                       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;
-                       }
+               rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
+               if (rc) {
+                       pr_info_ratelimited("tc action pedit unable to extract header offset for header type (0x%x)\n", htype);
+                       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);
+               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++;