]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/m_pedit.c
treewide: refactor help messages
[mirror_iproute2.git] / tc / m_pedit.c
index 8ccec8bc4a99f56bf2d505517cbf2fc6691e9c92..8eb15f4b276d344aa7dd2213b75db7073badec8a 100644 (file)
@@ -18,7 +18,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <fcntl.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include "utils.h"
 #include "tc_util.h"
 #include "m_pedit.h"
+#include "rt_names.h"
 
 static struct m_pedit_util *pedit_list;
 static int pedit_debug;
 
-static void
-explain(void)
+static void explain(void)
 {
-       fprintf(stderr, "Usage: ... pedit munge <MUNGE> [<BRANCH>]\n");
        fprintf(stderr,
+               "Usage: ... pedit munge [ex] <MUNGE> [CONTROL]\n"
                "Where: MUNGE := <RAW>|<LAYERED>\n"
-               "\t<RAW>:= <OFFSETC>[ATC]<CMD>\n \t\tOFFSETC:= offset <offval> <u8|u16|u32>\n "
-               "\t\tATC:= at <atval> offmask <maskval> shift <shiftval>\n \t\tNOTE: offval is byte offset, must be multiple of 4\n "
-               "\t\tNOTE: maskval is a 32 bit hex number\n \t\tNOTE: shiftval is a is a shift value\n "
-               "\t\tCMD:= clear | invert | set <setval>| retain\n \t<LAYERED>:= ip <ipdata> | ip6 <ip6data> \n "
+               "\t<RAW>:= <OFFSETC>[ATC]<CMD>\n \t\tOFFSETC:= offset <offval> <u8|u16|u32>\n"
+               "\t\tATC:= at <atval> offmask <maskval> shift <shiftval>\n"
+               "\t\tNOTE: offval is byte offset, must be multiple of 4\n"
+               "\t\tNOTE: maskval is a 32 bit hex number\n \t\tNOTE: shiftval is a shift value\n"
+               "\t\tCMD:= clear | invert | set <setval>| add <addval> | retain\n"
+               "\t<LAYERED>:= ip <ipdata> | ip6 <ip6data>\n"
                " \t\t| udp <udpdata> | tcp <tcpdata> | icmp <icmpdata>\n"
-               "\t<BRANCH>:= reclassify | pipe | drop | continue | pass\n"
+               "\tCONTROL:= reclassify | pipe | drop | continue | pass |\n"
+               "\t          goto chain <CHAIN_INDEX>\n"
+               "\tNOTE: if 'ex' is set, extended functionality will be supported (kernel >= 4.11)\n"
                "For Example usage look at the examples directory\n");
 
 }
 
-static void
-usage(void)
+static void usage(void)
 {
        explain();
        exit(-1);
 }
 
-static int
-pedit_parse_nopopt (int *argc_p, char ***argv_p, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
+static int pedit_parse_nopopt(int *argc_p, char ***argv_p,
+                             struct m_pedit_sel *sel,
+                             struct m_pedit_key *tkey)
 {
        int argc = *argc_p;
        char **argv = *argv_p;
 
        if (argc) {
-               fprintf(stderr, "Unknown action  hence option \"%s\" is unparsable\n", *argv);
-                       return -1;
+               fprintf(stderr,
+                       "Unknown action  hence option \"%s\" is unparsable\n",
+                       *argv);
+               return -1;
        }
 
        return 0;
@@ -75,7 +80,7 @@ static struct m_pedit_util *get_pedit_kind(const char *str)
        static void *pBODY;
        void *dlh;
        char buf[256];
-       struct  m_pedit_util *p;
+       struct m_pedit_util *p;
 
        for (p = pedit_list; p; p = p->next) {
                if (strcmp(p->id, str) == 0)
@@ -104,19 +109,19 @@ reg:
        return p;
 
 noexist:
-       p = malloc(sizeof(*p));
+       p = calloc(1, sizeof(*p));
        if (p) {
-               memset(p, 0, sizeof(*p));
-               strncpy(p->id, str, sizeof(p->id)-1);
+               strlcpy(p->id, str, sizeof(p->id));
                p->parse_peopt = pedit_parse_nopopt;
                goto reg;
        }
        return p;
 }
 
-int
-pack_key(struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
+static int pack_key(struct m_pedit_sel *_sel, struct m_pedit_key *tkey)
 {
+       struct tc_pedit_sel *sel = &_sel->sel;
+       struct m_pedit_key_ex *keys_ex = _sel->keys_ex;
        int hwm = sel->nkeys;
 
        if (hwm >= MAX_OFFS)
@@ -133,13 +138,25 @@ pack_key(struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
        sel->keys[hwm].at = tkey->at;
        sel->keys[hwm].offmask = tkey->offmask;
        sel->keys[hwm].shift = tkey->shift;
+
+       if (_sel->extended) {
+               keys_ex[hwm].htype = tkey->htype;
+               keys_ex[hwm].cmd = tkey->cmd;
+       } else {
+               if (tkey->htype != TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK ||
+                   tkey->cmd != TCA_PEDIT_KEY_EX_CMD_SET) {
+                       fprintf(stderr,
+                               "Munge parameters not supported. Use 'pedit ex munge ...'.\n");
+                       return -1;
+               }
+       }
+
        sel->nkeys++;
        return 0;
 }
 
-
-int
-pack_key32(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
+static int pack_key32(__u32 retain, struct m_pedit_sel *sel,
+                     struct m_pedit_key *tkey)
 {
        if (tkey->off > (tkey->off & ~3)) {
                fprintf(stderr,
@@ -152,11 +169,11 @@ pack_key32(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
        return pack_key(sel, tkey);
 }
 
-int
-pack_key16(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
+static int pack_key16(__u32 retain, struct m_pedit_sel *sel,
+                     struct m_pedit_key *tkey)
 {
        int ind, stride;
-       __u32 m[4] = {0x0000FFFF, 0xFF0000FF, 0xFFFF0000};
+       __u32 m[4] = { 0x0000FFFF, 0xFF0000FF, 0xFFFF0000 };
 
        if (tkey->val > 0xFFFF || tkey->mask > 0xFFFF) {
                fprintf(stderr, "pack_key16 bad value\n");
@@ -177,19 +194,20 @@ pack_key16(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
        tkey->off &= ~3;
 
        if (pedit_debug)
-               printf("pack_key16: Final val %08x mask %08x\n", tkey->val, tkey->mask);
+               printf("pack_key16: Final val %08x mask %08x\n",
+                      tkey->val, tkey->mask);
        return pack_key(sel, tkey);
-
 }
 
-int
-pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
+static int pack_key8(__u32 retain, struct m_pedit_sel *sel,
+                    struct m_pedit_key *tkey)
 {
        int ind, stride;
-       __u32 m[4] = {0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00};
+       __u32 m[4] = { 0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00 };
 
        if (tkey->val > 0xFF || tkey->mask > 0xFF) {
-               fprintf(stderr, "pack_key8 bad value (val %x mask %x\n", tkey->val, tkey->mask);
+               fprintf(stderr, "pack_key8 bad value (val %x mask %x\n",
+                       tkey->val, tkey->mask);
                return -1;
        }
 
@@ -202,12 +220,70 @@ pack_key8(__u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
        tkey->off &= ~3;
 
        if (pedit_debug)
-               printf("pack_key8: Final word off %d  val %08x mask %08x\n", tkey->off, tkey->val, tkey->mask);
+               printf("pack_key8: Final word off %d  val %08x mask %08x\n",
+                      tkey->off, tkey->val, tkey->mask);
        return pack_key(sel, tkey);
 }
 
-int
-parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
+static int pack_mac(struct m_pedit_sel *sel, struct m_pedit_key *tkey,
+                   __u8 *mac)
+{
+       int ret = 0;
+
+       if (!(tkey->off & 0x3)) {
+               tkey->mask = 0;
+               tkey->val = ntohl(*((__u32 *)mac));
+               ret |= pack_key32(~0, sel, tkey);
+
+               tkey->off += 4;
+               tkey->mask = 0;
+               tkey->val = ntohs(*((__u16 *)&mac[4]));
+               ret |= pack_key16(~0, sel, tkey);
+       } else if (!(tkey->off & 0x1)) {
+               tkey->mask = 0;
+               tkey->val = ntohs(*((__u16 *)mac));
+               ret |= pack_key16(~0, sel, tkey);
+
+               tkey->off += 4;
+               tkey->mask = 0;
+               tkey->val = ntohl(*((__u32 *)(mac + 2)));
+               ret |= pack_key32(~0, sel, tkey);
+       } else {
+               fprintf(stderr,
+                       "pack_mac: mac offsets must begin in 32bit or 16bit boundaries\n");
+               return -1;
+       }
+
+       return ret;
+}
+
+static int pack_ipv6(struct m_pedit_sel *sel, struct m_pedit_key *tkey,
+                    __u32 *ipv6)
+{
+       int ret = 0;
+       int i;
+
+       if (tkey->off & 0x3) {
+               fprintf(stderr,
+                       "pack_ipv6: IPv6 offsets must begin in 32bit boundaries\n");
+               return -1;
+       }
+
+       for (i = 0; i < 4; i++) {
+               tkey->mask = 0;
+               tkey->val = ntohl(ipv6[i]);
+
+               ret = pack_key32(~0, sel, tkey);
+               if (ret)
+                       return ret;
+
+               tkey->off += 4;
+       }
+
+       return 0;
+}
+
+static int parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
 {
        int argc = *argc_p;
        char **argv = *argv_p;
@@ -216,7 +292,7 @@ parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
                return -1;
 
        if (type == TINT)
-               return get_integer((int *) val, *argv, 0);
+               return get_integer((int *)val, *argv, 0);
 
        if (type == TU32)
                return get_u32(val, *argv, 0);
@@ -224,24 +300,42 @@ parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
        if (type == TIPV4) {
                inet_prefix addr;
 
-               if (get_prefix_1(&addr, *argv, AF_INET)) {
+               if (get_prefix_1(&addr, *argv, AF_INET))
                        return -1;
-               }
+
                *val = addr.data[0];
                return 0;
        }
+
        if (type == TIPV6) {
-               /* not implemented yet */
-               return -1;
+               inet_prefix addr;
+
+               if (get_prefix_1(&addr, *argv, AF_INET6))
+                       return -1;
+
+               memcpy(val, addr.data, addr.bytelen);
+
+               return 0;
+       }
+
+       if (type == TMAC) {
+#define MAC_ALEN 6
+               int ret = ll_addr_a2n((char *)val, MAC_ALEN, *argv);
+
+               if (ret == MAC_ALEN)
+                       return 0;
        }
 
        return -1;
 }
 
-int
-parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
+int parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain,
+             struct m_pedit_sel *sel, struct m_pedit_key *tkey)
 {
-       __u32 mask = 0, val = 0;
+       __u32 mask[4] = { 0 };
+       __u32 val[4] = { 0 };
+       __u32 *m = &mask[0];
+       __u32 *v = &val[0];
        __u32 o = 0xFF;
        int res = -1;
        int argc = *argc_p;
@@ -251,7 +345,8 @@ parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct
                return -1;
 
        if (pedit_debug)
-               printf("parse_cmd argc %d %s offset %d length %d\n", argc, *argv, tkey->off, len);
+               printf("parse_cmd argc %d %s offset %d length %d\n",
+                      argc, *argv, tkey->off, len);
 
        if (len == 2)
                o = 0xFFFF;
@@ -259,10 +354,20 @@ parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct
                o = 0xFFFFFFFF;
 
        if (matches(*argv, "invert") == 0) {
-               val = mask = o;
-       } else if (matches(*argv, "set") == 0) {
+               *v = *m = o;
+       } else if (matches(*argv, "set") == 0 ||
+                  matches(*argv, "add") == 0) {
+               if (matches(*argv, "add") == 0)
+                       tkey->cmd = TCA_PEDIT_KEY_EX_CMD_ADD;
+
+               if (!sel->extended && tkey->cmd) {
+                       fprintf(stderr,
+                               "Non extended mode. only 'set' command is supported\n");
+                       return -1;
+               }
+
                NEXT_ARG();
-               if (parse_val(&argc, &argv, &val, type))
+               if (parse_val(&argc, &argv, val, type))
                        return -1;
        } else if (matches(*argv, "preserve") == 0) {
                retain = 0;
@@ -271,17 +376,35 @@ parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct
                        return -1;
        }
 
-       argc--; argv++;
+       argc--;
+       argv++;
 
        if (argc && matches(*argv, "retain") == 0) {
                NEXT_ARG();
                if (parse_val(&argc, &argv, &retain, TU32))
                        return -1;
-               argc--; argv++;
+               argc--;
+               argv++;
+       }
+
+       if (len > 4 && retain != ~0) {
+               fprintf(stderr,
+                       "retain is not supported for fields longer the 32 bits\n");
+               return -1;
        }
 
-       tkey->val = val;
-       tkey->mask = mask;
+       if (type == TMAC) {
+               res = pack_mac(sel, tkey, (__u8 *)val);
+               goto done;
+       }
+
+       if (type == TIPV6) {
+               res = pack_ipv6(sel, tkey, val);
+               goto done;
+       }
+
+       tkey->val = *v;
+       tkey->mask = *m;
 
        if (type == TIPV4)
                tkey->val = ntohl(tkey->val);
@@ -302,15 +425,16 @@ parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain, struct
        return -1;
 done:
        if (pedit_debug)
-               printf("parse_cmd done argc %d %s offset %d length %d\n", argc, *argv, tkey->off, len);
+               printf("parse_cmd done argc %d %s offset %d length %d\n",
+                      argc, *argv, tkey->off, len);
        *argc_p = argc;
        *argv_p = argv;
        return res;
 
 }
 
-int
-parse_offset(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel, struct tc_pedit_key *tkey)
+static int parse_offset(int *argc_p, char ***argv_p, struct m_pedit_sel *sel,
+                       struct m_pedit_key *tkey)
 {
        int off;
        __u32 len, retain;
@@ -331,7 +455,6 @@ parse_offset(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel, struct tc_pe
        if (argc <= 0)
                return -1;
 
-
        if (matches(*argv, "u32") == 0) {
                len = 4;
                retain = 0xFFFFFFFF;
@@ -386,10 +509,9 @@ done:
        return res;
 }
 
-static int
-parse_munge(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel)
+static int parse_munge(int *argc_p, char ***argv_p, struct m_pedit_sel *sel)
 {
-       struct tc_pedit_key tkey;
+       struct m_pedit_key tkey = {};
        int argc = *argc_p;
        char **argv = *argv_p;
        int res = -1;
@@ -397,14 +519,12 @@ parse_munge(int *argc_p, char ***argv_p, struct tc_pedit_sel *sel)
        if (argc <= 0)
                return -1;
 
-       memset(&tkey, 0, sizeof(tkey));
-
        if (matches(*argv, "offset") == 0) {
                NEXT_ARG();
                res = parse_offset(&argc, &argv, sel, &tkey);
                goto done;
        } else {
-               char k[16];
+               char k[FILTER_NAMESZ];
                struct m_pedit_util *p = NULL;
 
                strncpy(k, *argv, sizeof(k) - 1);
@@ -433,39 +553,108 @@ done:
        return res;
 }
 
-int
-parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
+static int pedit_keys_ex_getattr(struct rtattr *attr,
+                                struct m_pedit_key_ex *keys_ex, int n)
 {
-       struct {
-               struct tc_pedit_sel sel;
-               struct tc_pedit_key keys[MAX_OFFS];
-       } sel;
+       struct rtattr *i;
+       int rem = RTA_PAYLOAD(attr);
+       struct rtattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
+       struct m_pedit_key_ex *k = keys_ex;
+
+       for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
+               if (!n)
+                       return -1;
+
+               if (i->rta_type != TCA_PEDIT_KEY_EX)
+                       return -1;
+
+               parse_rtattr_nested(tb, TCA_PEDIT_KEY_EX_MAX, i);
+
+               k->htype = rta_getattr_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
+               k->cmd = rta_getattr_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
+
+               k++;
+               n--;
+       }
+
+       return !!n;
+}
+
+static int pedit_keys_ex_addattr(struct m_pedit_sel *sel, struct nlmsghdr *n)
+{
+       struct m_pedit_key_ex *k = sel->keys_ex;
+       struct rtattr *keys_start;
+       int i;
+
+       if (!sel->extended)
+               return 0;
+
+       keys_start = addattr_nest(n, MAX_MSG, TCA_PEDIT_KEYS_EX | NLA_F_NESTED);
+
+       for (i = 0; i < sel->sel.nkeys; i++) {
+               struct rtattr *key_start;
+
+               key_start = addattr_nest(n, MAX_MSG,
+                                        TCA_PEDIT_KEY_EX | NLA_F_NESTED);
+
+               if (addattr16(n, MAX_MSG, TCA_PEDIT_KEY_EX_HTYPE, k->htype) ||
+                   addattr16(n, MAX_MSG, TCA_PEDIT_KEY_EX_CMD, k->cmd)) {
+                       return -1;
+               }
+
+               addattr_nest_end(n, key_start);
+
+               k++;
+       }
+
+       addattr_nest_end(n, keys_start);
+
+       return 0;
+}
+
+static int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p,
+                      int tca_id, struct nlmsghdr *n)
+{
+       struct m_pedit_sel sel = {};
 
        int argc = *argc_p;
        char **argv = *argv_p;
        int ok = 0, iok = 0;
        struct rtattr *tail;
 
-       memset(&sel, 0, sizeof(sel));
-
        while (argc > 0) {
                if (pedit_debug > 1)
                        fprintf(stderr, "while pedit (%d:%s)\n", argc, *argv);
                if (matches(*argv, "pedit") == 0) {
                        NEXT_ARG();
                        ok++;
+
+                       if (matches(*argv, "ex") == 0) {
+                               if (ok > 1) {
+                                       fprintf(stderr,
+                                               "'ex' must be before first 'munge'\n");
+                                       explain();
+                                       return -1;
+                               }
+                               sel.extended = true;
+                               NEXT_ARG();
+                       }
+
                        continue;
                } else if (matches(*argv, "help") == 0) {
                        usage();
                } else if (matches(*argv, "munge") == 0) {
                        if (!ok) {
-                               fprintf(stderr, "Illegal pedit construct (%s)\n", *argv);
+                               fprintf(stderr, "Bad pedit construct (%s)\n",
+                                       *argv);
                                explain();
                                return -1;
                        }
                        NEXT_ARG();
-                       if (parse_munge(&argc, &argv, &sel.sel)) {
-                               fprintf(stderr, "Illegal pedit construct (%s)\n", *argv);
+
+                       if (parse_munge(&argc, &argv, &sel)) {
+                               fprintf(stderr, "Bad pedit construct (%s)\n",
+                                       *argv);
                                explain();
                                return -1;
                        }
@@ -481,25 +670,7 @@ parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, stru
                return -1;
        }
 
-       if (argc) {
-               if (matches(*argv, "reclassify") == 0) {
-                       sel.sel.action = TC_ACT_RECLASSIFY;
-                       NEXT_ARG();
-               } else if (matches(*argv, "pipe") == 0) {
-                       sel.sel.action = TC_ACT_PIPE;
-                       NEXT_ARG();
-               } else if (matches(*argv, "drop") == 0 ||
-                       matches(*argv, "shot") == 0) {
-                       sel.sel.action = TC_ACT_SHOT;
-                       NEXT_ARG();
-               } else if (matches(*argv, "continue") == 0) {
-                       sel.sel.action = TC_ACT_UNSPEC;
-                       NEXT_ARG();
-               } else if (matches(*argv, "pass") == 0) {
-                       sel.sel.action = TC_ACT_OK;
-                       NEXT_ARG();
-               }
-       }
+       parse_action_control_dflt(&argc, &argv, &sel.sel.action, false, TC_ACT_OK);
 
        if (argc) {
                if (matches(*argv, "index") == 0) {
@@ -514,37 +685,100 @@ parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id, stru
                }
        }
 
-       tail = NLMSG_TAIL(n);
-       addattr_l(n, MAX_MSG, tca_id, NULL, 0);
-       addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS, &sel, sizeof(sel.sel)+sel.sel.nkeys*sizeof(struct tc_pedit_key));
-       tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
+       tail = addattr_nest(n, MAX_MSG, tca_id);
+       if (!sel.extended) {
+               addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS, &sel,
+                         sizeof(sel.sel) +
+                         sel.sel.nkeys * sizeof(struct tc_pedit_key));
+       } else {
+               addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS_EX, &sel,
+                         sizeof(sel.sel) +
+                         sel.sel.nkeys * sizeof(struct tc_pedit_key));
+
+               pedit_keys_ex_addattr(&sel, n);
+       }
+
+       addattr_nest_end(n, tail);
 
        *argc_p = argc;
        *argv_p = argv;
        return 0;
 }
 
-int
-print_pedit(struct action_util *au, FILE * f, struct rtattr *arg)
+static const char * const pedit_htype_str[] = {
+       [TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK] = "",
+       [TCA_PEDIT_KEY_EX_HDR_TYPE_ETH] = "eth",
+       [TCA_PEDIT_KEY_EX_HDR_TYPE_IP4] = "ipv4",
+       [TCA_PEDIT_KEY_EX_HDR_TYPE_IP6] = "ipv6",
+       [TCA_PEDIT_KEY_EX_HDR_TYPE_TCP] = "tcp",
+       [TCA_PEDIT_KEY_EX_HDR_TYPE_UDP] = "udp",
+};
+
+static void print_pedit_location(FILE *f,
+                                enum pedit_header_type htype, __u32 off)
+{
+       if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK) {
+               fprintf(f, "%d", (unsigned int)off);
+               return;
+       }
+
+       if (htype < ARRAY_SIZE(pedit_htype_str))
+               fprintf(f, "%s", pedit_htype_str[htype]);
+       else
+               fprintf(f, "unknown(%d)", htype);
+
+       fprintf(f, "%c%d", (int)off  >= 0 ? '+' : '-', abs((int)off));
+}
+
+static int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg)
 {
        struct tc_pedit_sel *sel;
        struct rtattr *tb[TCA_PEDIT_MAX + 1];
-
-       SPRINT_BUF(b1);
+       struct m_pedit_key_ex *keys_ex = NULL;
 
        if (arg == NULL)
                return -1;
 
        parse_rtattr_nested(tb, TCA_PEDIT_MAX, arg);
 
-       if (tb[TCA_PEDIT_PARMS] == NULL) {
+       if (!tb[TCA_PEDIT_PARMS] && !tb[TCA_PEDIT_PARMS_EX]) {
                fprintf(f, "[NULL pedit parameters]");
                return -1;
        }
-       sel = RTA_DATA(tb[TCA_PEDIT_PARMS]);
 
-       fprintf(f, " pedit action %s keys %d\n ", action_n2a(sel->action, b1, sizeof (b1)), sel->nkeys);
-       fprintf(f, "\t index %d ref %d bind %d", sel->index, sel->refcnt, sel->bindcnt);
+       if (tb[TCA_PEDIT_PARMS]) {
+               sel = RTA_DATA(tb[TCA_PEDIT_PARMS]);
+       } else {
+               int err;
+
+               sel = RTA_DATA(tb[TCA_PEDIT_PARMS_EX]);
+
+               if (!tb[TCA_PEDIT_KEYS_EX]) {
+                       fprintf(f, "Netlink error\n");
+                       return -1;
+               }
+
+               keys_ex = calloc(sel->nkeys, sizeof(*keys_ex));
+               if (!keys_ex) {
+                       fprintf(f, "Out of memory\n");
+                       return -1;
+               }
+
+               err = pedit_keys_ex_getattr(tb[TCA_PEDIT_KEYS_EX], keys_ex,
+                                           sel->nkeys);
+               if (err) {
+                       fprintf(f, "Netlink error\n");
+
+                       free(keys_ex);
+                       return -1;
+               }
+       }
+
+       fprintf(f, " pedit ");
+       print_action_control(f, "action ", sel->action, " ");
+       fprintf(f,"keys %d\n ", sel->nkeys);
+       fprintf(f, "\t index %u ref %d bind %d", sel->index, sel->refcnt,
+               sel->bindcnt);
 
        if (show_stats) {
                if (tb[TCA_PEDIT_TM]) {
@@ -556,26 +790,39 @@ print_pedit(struct action_util *au, FILE * f, struct rtattr *arg)
        if (sel->nkeys) {
                int i;
                struct tc_pedit_key *key = sel->keys;
+               struct m_pedit_key_ex *key_ex = keys_ex;
 
                for (i = 0; i < sel->nkeys; i++, key++) {
+                       enum pedit_header_type htype =
+                               TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
+                       enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
+
+                       if (keys_ex) {
+                               htype = key_ex->htype;
+                               cmd = key_ex->cmd;
+
+                               key_ex++;
+                       }
+
                        fprintf(f, "\n\t key #%d", i);
-                       fprintf(f, "  at %d: val %08x mask %08x",
-                       (unsigned int)key->off,
-                       (unsigned int)ntohl(key->val),
-                       (unsigned int)ntohl(key->mask));
+
+                       fprintf(f, "  at ");
+
+                       print_pedit_location(f, htype, key->off);
+
+                       fprintf(f, ": %s %08x mask %08x",
+                               cmd ? "add" : "val",
+                               (unsigned int)ntohl(key->val),
+                               (unsigned int)ntohl(key->mask));
                }
        } else {
-               fprintf(f, "\npedit %x keys %d is not LEGIT", sel->index, sel->nkeys);
+               fprintf(f, "\npedit %x keys %d is not LEGIT", sel->index,
+                       sel->nkeys);
        }
 
-
        fprintf(f, "\n ");
-       return 0;
-}
 
-int
-pedit_print_xstats(struct action_util *au, FILE *f, struct rtattr *xstats)
-{
+       free(keys_ex);
        return 0;
 }