]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/m_pedit.c
treewide: refactor help messages
[mirror_iproute2.git] / tc / m_pedit.c
index a26fd3e5bc5e61fe619b528c993394342472ee06..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)
 {
-       fprintf(stderr, "Usage: ... pedit munge [ex] <MUNGE> [CONTROL]\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 shift value\n"
-               "\t\tCMD:= clear | invert | set <setval>| retain\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"
-               "\tCONTROL:= 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");
 
@@ -110,14 +111,14 @@ reg:
 noexist:
        p = calloc(1, sizeof(*p));
        if (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 m_pedit_sel *_sel, struct m_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;
@@ -144,7 +145,8 @@ int pack_key(struct m_pedit_sel *_sel, struct m_pedit_key *tkey)
        } 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 'munge ex'.\n");
+                       fprintf(stderr,
+                               "Munge parameters not supported. Use 'pedit ex munge ...'.\n");
                        return -1;
                }
        }
@@ -153,8 +155,8 @@ int pack_key(struct m_pedit_sel *_sel, struct m_pedit_key *tkey)
        return 0;
 }
 
-int pack_key32(__u32 retain, struct m_pedit_sel *sel,
-              struct m_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,
@@ -167,8 +169,8 @@ int pack_key32(__u32 retain, struct m_pedit_sel *sel,
        return pack_key(sel, tkey);
 }
 
-int pack_key16(__u32 retain, struct m_pedit_sel *sel,
-              struct m_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 };
@@ -195,10 +197,10 @@ int pack_key16(__u32 retain, struct m_pedit_sel *sel,
                printf("pack_key16: Final val %08x mask %08x\n",
                       tkey->val, tkey->mask);
        return pack_key(sel, tkey);
-
 }
 
-int pack_key8(__u32 retain, struct m_pedit_sel *sel, struct m_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 };
@@ -223,7 +225,65 @@ int pack_key8(__u32 retain, struct m_pedit_sel *sel, struct m_pedit_key *tkey)
        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;
@@ -247,8 +307,24 @@ int parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
                return 0;
        }
 
-       if (type == TIPV6)
-               return -1; /* not implemented yet */
+       if (type == TIPV6) {
+               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;
 }
@@ -256,7 +332,10 @@ int parse_val(int *argc_p, char ***argv_p, __u32 *val, int type)
 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;
@@ -275,10 +354,20 @@ int parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain,
                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;
@@ -298,8 +387,24 @@ int parse_cmd(int *argc_p, char ***argv_p, __u32 len, int type, __u32 retain,
                argv++;
        }
 
-       tkey->val = val;
-       tkey->mask = mask;
+       if (len > 4 && retain != ~0) {
+               fprintf(stderr,
+                       "retain is not supported for fields longer the 32 bits\n");
+               return -1;
+       }
+
+       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);
@@ -328,8 +433,8 @@ done:
 
 }
 
-int parse_offset(int *argc_p, char ***argv_p, struct m_pedit_sel *sel,
-                struct m_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;
@@ -419,7 +524,7 @@ static int parse_munge(int *argc_p, char ***argv_p, struct m_pedit_sel *sel)
                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);
@@ -507,8 +612,8 @@ static int pedit_keys_ex_addattr(struct m_pedit_sel *sel, struct nlmsghdr *n)
        return 0;
 }
 
-int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
-               struct nlmsghdr *n)
+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 = {};
 
@@ -526,7 +631,8 @@ int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
 
                        if (matches(*argv, "ex") == 0) {
                                if (ok > 1) {
-                                       fprintf(stderr, "'ex' must be before first 'munge'\n");
+                                       fprintf(stderr,
+                                               "'ex' must be before first 'munge'\n");
                                        explain();
                                        return -1;
                                }
@@ -564,8 +670,7 @@ int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
                return -1;
        }
 
-       if (argc && !action_a2n(*argv, &sel.sel.action, false))
-               NEXT_ARG();
+       parse_action_control_dflt(&argc, &argv, &sel.sel.action, false, TC_ACT_OK);
 
        if (argc) {
                if (matches(*argv, "index") == 0) {
@@ -580,8 +685,7 @@ int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
                }
        }
 
-       tail = NLMSG_TAIL(n);
-       addattr_l(n, MAX_MSG, tca_id, NULL, 0);
+       tail = addattr_nest(n, MAX_MSG, tca_id);
        if (!sel.extended) {
                addattr_l(n, MAX_MSG, TCA_PEDIT_PARMS, &sel,
                          sizeof(sel.sel) +
@@ -594,14 +698,14 @@ int parse_pedit(struct action_util *a, int *argc_p, char ***argv_p, int tca_id,
                pedit_keys_ex_addattr(&sel, n);
        }
 
-       tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail;
+       addattr_nest_end(n, tail);
 
        *argc_p = argc;
        *argv_p = argv;
        return 0;
 }
 
-const char *pedit_htype_str[] = {
+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",
@@ -626,7 +730,7 @@ static void print_pedit_location(FILE *f,
        fprintf(f, "%c%d", (int)off  >= 0 ? '+' : '-', abs((int)off));
 }
 
-int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg)
+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];
@@ -670,8 +774,9 @@ int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg)
                }
        }
 
-       fprintf(f, " pedit action %s keys %d\n ",
-               action_n2a(sel->action), sel->nkeys);
+       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);
 
@@ -690,9 +795,11 @@ int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg)
                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++;
                        }
@@ -703,7 +810,8 @@ int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg)
 
                        print_pedit_location(f, htype, key->off);
 
-                       fprintf(f, ": val %08x mask %08x",
+                       fprintf(f, ": %s %08x mask %08x",
+                               cmd ? "add" : "val",
                                (unsigned int)ntohl(key->val),
                                (unsigned int)ntohl(key->mask));
                }
@@ -718,11 +826,6 @@ int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg)
        return 0;
 }
 
-int pedit_print_xstats(struct action_util *au, FILE *f, struct rtattr *xstats)
-{
-       return 0;
-}
-
 struct action_util pedit_action_util = {
        .id = "pedit",
        .parse_aopt = parse_pedit,