]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/m_xt_old.c
vdpa: add .gitignore
[mirror_iproute2.git] / tc / m_xt_old.c
index 7ed7ecc7a5e68e3e3af4eaec0f3924f57febe79b..db014898590d8252b506e33d0e2ae1c0220adc48 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * m_xt.c      xtables based targets
- *             utilities mostly ripped from iptables <duh, its the linux way>
+ *             utilities mostly ripped from iptables <duh, its the linux way>
  *
  *             This program is free software; you can distribute it and/or
  *             modify it under the terms of the GNU General Public License
@@ -13,7 +13,6 @@
 /*XXX: in the future (xtables 1.4.3?) get rid of everything tagged
  * as TC_CONFIG_XT_H */
 
-#include <syslog.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -41,8 +40,8 @@
 #endif
 
 #ifndef ALIGN
-#define ALIGN(x,a)             __ALIGN_MASK(x,(typeof(x))(a)-1)
-#define __ALIGN_MASK(x,mask)   (((x)+(mask))&~(mask))
+#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
+#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
 #endif
 
 static const char *pname = "tc-ipt";
@@ -63,7 +62,7 @@ static struct option original_opts[] = {
 };
 
 static struct option *opts = original_opts;
-static unsigned int global_option_offset = 0;
+static unsigned int global_option_offset;
 char *lib_dir;
 const char *program_version = XTABLES_VERSION;
 const char *program_name = "tc-ipt";
@@ -96,18 +95,18 @@ merge_options(struct option *oldopts, const struct option *newopts,
        struct option *merge;
        unsigned int num_old, num_new, i;
 
-       for (num_old = 0; oldopts[num_old].name; num_old++) ;
-       for (num_new = 0; newopts[num_new].name; num_new++) ;
+       for (num_old = 0; oldopts[num_old].name; num_old++);
+       for (num_new = 0; newopts[num_new].name; num_new++);
 
        *option_offset = global_option_offset + OPTION_OFFSET;
 
-       merge = malloc(sizeof (struct option) * (num_new + num_old + 1));
-       memcpy(merge, oldopts, num_old * sizeof (struct option));
+       merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
+       memcpy(merge, oldopts, num_old * sizeof(struct option));
        for (i = 0; i < num_new; i++) {
                merge[num_old + i] = newopts[i];
                merge[num_old + i].val += *option_offset;
        }
-       memset(merge + num_old + num_new, 0, sizeof (struct option));
+       memset(merge + num_old + num_new, 0, sizeof(struct option));
 
        return merge;
 }
@@ -125,35 +124,35 @@ merge_options(struct option *oldopts, const struct option *newopts,
 int
 check_inverse(const char option[], int *invert, int *my_optind, int argc)
 {
-        if (option && strcmp(option, "!") == 0) {
-                if (*invert)
-                        exit_error(PARAMETER_PROBLEM,
-                                   "Multiple `!' flags not allowed");
-                *invert = TRUE;
-                if (my_optind != NULL) {
-                        ++*my_optind;
-                        if (argc && *my_optind > argc)
-                                exit_error(PARAMETER_PROBLEM,
-                                           "no argument following `!'");
-                }
-
-                return TRUE;
-        }
-        return FALSE;
+       if (option && strcmp(option, "!") == 0) {
+               if (*invert)
+                       exit_error(PARAMETER_PROBLEM,
+                                  "Multiple `!' flags not allowed");
+               *invert = TRUE;
+               if (my_optind != NULL) {
+                       ++*my_optind;
+                       if (argc && *my_optind > argc)
+                               exit_error(PARAMETER_PROBLEM,
+                                          "no argument following `!'");
+               }
+
+               return TRUE;
+       }
+       return FALSE;
 }
 
 /*XXX: TC_CONFIG_XT_H */
 void exit_error(enum exittype status, const char *msg, ...)
 {
-        va_list args;
-
-        va_start(args, msg);
-        fprintf(stderr, "%s v%s: ", pname, pversion);
-        vfprintf(stderr, msg, args);
-        va_end(args);
-        fprintf(stderr, "\n");
-        /* On error paths, make sure that we don't leak memory */
-        exit(status);
+       va_list args;
+
+       va_start(args, msg);
+       fprintf(stderr, "%s v%s: ", pname, pversion);
+       vfprintf(stderr, msg, args);
+       va_end(args);
+       fprintf(stderr, "\n");
+       /* On error paths, make sure that we don't leak memory */
+       exit(status);
 }
 
 /*XXX: TC_CONFIG_XT_H */
@@ -173,9 +172,9 @@ build_st(struct xtables_target *target, struct xt_entry_target *t)
 {
 
        size_t size =
-                   XT_ALIGN(sizeof (struct xt_entry_target)) + target->size;
+                   XT_ALIGN(sizeof(struct xt_entry_target)) + target->size;
 
-       if (NULL == t) {
+       if (t == NULL) {
                target->t = fw_calloc(1, size);
                target->t->u.target_size = size;
                strcpy(target->t->u.user.name, target->name);
@@ -197,14 +196,14 @@ inline void set_lib_dir(void)
        if (!lib_dir) {
                lib_dir = getenv("IPTABLES_LIB_DIR");
                if (lib_dir)
-                       fprintf(stderr, "using deprecated IPTABLES_LIB_DIR \n");
+                       fprintf(stderr, "using deprecated IPTABLES_LIB_DIR\n");
        }
        if (lib_dir == NULL)
                lib_dir = XT_LIB_DIR;
 
 }
 
-static int parse_ipt(struct action_util *a,int *argc_p,
+static int parse_ipt(struct action_util *a, int *argc_p,
                     char ***argv_p, int tca_id, struct nlmsghdr *n)
 {
        struct xtables_target *m = NULL;
@@ -214,7 +213,7 @@ static int parse_ipt(struct action_util *a,int *argc_p,
        int rargc = *argc_p;
        char **argv = *argv_p;
        int argc = 0, iargc = 0;
-       char k[16];
+       char k[FILTER_NAMESZ];
        int size = 0;
        int iok = 0, ok = 0;
        __u32 hook = 0, index = 0;
@@ -223,16 +222,16 @@ static int parse_ipt(struct action_util *a,int *argc_p,
 
        {
                int i;
+
                for (i = 0; i < rargc; i++) {
-                       if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) {
+                       if (!argv[i] || strcmp(argv[i], "action") == 0)
                                break;
-                       }
                }
                iargc = argc = i;
        }
 
        if (argc <= 2) {
-               fprintf(stderr,"bad arguements to ipt %d vs %d \n", argc, rargc);
+               fprintf(stderr, "bad arguments to ipt %d vs %d\n", argc, rargc);
                return -1;
        }
 
@@ -243,29 +242,29 @@ static int parse_ipt(struct action_util *a,int *argc_p,
                switch (c) {
                case 'j':
                        m = find_target(optarg, TRY_LOAD);
-                       if (NULL != m) {
+                       if (m != NULL) {
 
-                               if (0 > build_st(m, NULL)) {
-                                       printf(" %s error \n", m->name);
+                               if (build_st(m, NULL) < 0) {
+                                       printf(" %s error\n", m->name);
                                        return -1;
                                }
                                opts =
                                    merge_options(opts, m->extra_opts,
                                                  &m->option_offset);
                        } else {
-                               fprintf(stderr," failed to find target %s\n\n", optarg);
+                               fprintf(stderr, " failed to find target %s\n\n", optarg);
                                return -1;
                        }
                        ok++;
                        break;
 
                default:
-                       memset(&fw, 0, sizeof (fw));
+                       memset(&fw, 0, sizeof(fw));
                        if (m) {
                                m->parse(c - m->option_offset, argv, 0,
                                         &m->tflags, NULL, &m->t);
                        } else {
-                               fprintf(stderr," failed to find target %s\n\n", optarg);
+                               fprintf(stderr, " failed to find target %s\n\n", optarg);
                                return -1;
 
                        }
@@ -289,7 +288,7 @@ static int parse_ipt(struct action_util *a,int *argc_p,
        }
 
        if (!ok && !iok) {
-               fprintf(stderr," ipt Parser BAD!! (%s)\n", *argv);
+               fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv);
                return -1;
        }
 
@@ -299,6 +298,7 @@ static int parse_ipt(struct action_util *a,int *argc_p,
 
        {
                struct tcmsg *t = NLMSG_DATA(n);
+
                if (t->tcm_parent != TC_H_ROOT
                    && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
                        hook = NF_IP_PRE_ROUTING;
@@ -307,8 +307,7 @@ static int parse_ipt(struct action_util *a,int *argc_p,
                }
        }
 
-       tail = NLMSG_TAIL(n);
-       addattr_l(n, MAX_MSG, tca_id, NULL, 0);
+       tail = addattr_nest(n, MAX_MSG, tca_id);
        fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
        fprintf(stdout, "\ttarget: ");
 
@@ -329,7 +328,7 @@ static int parse_ipt(struct action_util *a,int *argc_p,
        addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
        if (m)
                addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
-       tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
+       addattr_nest_end(n, tail);
 
        argc -= optind;
        argv += optind;
@@ -339,11 +338,11 @@ static int parse_ipt(struct action_util *a,int *argc_p,
        optind = 0;
        free_opts(opts);
        /* Clear flags if target will be used again */
-        m->tflags=0;
-        m->used=0;
+        m->tflags = 0;
+        m->used = 0;
        /* Free allocated memory */
-        if (m->t)
-            free(m->t);
+       if (m->t)
+           free(m->t);
 
 
        return 0;
@@ -351,86 +350,88 @@ static int parse_ipt(struct action_util *a,int *argc_p,
 }
 
 static int
-print_ipt(struct action_util *au,FILE * f, struct rtattr *arg)
+print_ipt(struct action_util *au, FILE * f, struct rtattr *arg)
 {
        struct rtattr *tb[TCA_IPT_MAX + 1];
        struct xt_entry_target *t = NULL;
+       struct xtables_target *m;
+       __u32 hook;
 
        if (arg == NULL)
-               return -1;
+               return 0;
 
        set_lib_dir();
 
        parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
 
        if (tb[TCA_IPT_TABLE] == NULL) {
-               fprintf(f, "[NULL ipt table name ] assuming mangle ");
+               fprintf(stderr, "Missing ipt table name, assuming mangle\n");
        } else {
                fprintf(f, "tablename: %s ",
-                       (char *) RTA_DATA(tb[TCA_IPT_TABLE]));
+                       rta_getattr_str(tb[TCA_IPT_TABLE]));
        }
 
        if (tb[TCA_IPT_HOOK] == NULL) {
-               fprintf(f, "[NULL ipt hook name ]\n ");
+               fprintf(stderr, "Missing ipt hook name\n");
                return -1;
-       } else {
-               __u32 hook;
-               hook = *(__u32 *) RTA_DATA(tb[TCA_IPT_HOOK]);
-               fprintf(f, " hook: %s \n", ipthooks[hook]);
        }
 
        if (tb[TCA_IPT_TARG] == NULL) {
-               fprintf(f, "\t[NULL ipt target parameters ] \n");
+               fprintf(stderr, "Missing ipt target parameters\n");
                return -1;
-       } else {
-               struct xtables_target *m = NULL;
-               t = RTA_DATA(tb[TCA_IPT_TARG]);
-               m = find_target(t->u.user.name, TRY_LOAD);
-               if (NULL != m) {
-                       if (0 > build_st(m, t)) {
-                               fprintf(stderr, " %s error \n", m->name);
-                               return -1;
-                       }
+       }
 
-                       opts =
-                           merge_options(opts, m->extra_opts,
-                                         &m->option_offset);
-               } else {
-                       fprintf(stderr, " failed to find target %s\n\n",
-                               t->u.user.name);
+       hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
+       fprintf(f, " hook: %s\n", ipthooks[hook]);
+
+       t = RTA_DATA(tb[TCA_IPT_TARG]);
+       m = find_target(t->u.user.name, TRY_LOAD);
+       if (m != NULL) {
+               if (build_st(m, t) < 0) {
+                       fprintf(stderr, " %s error\n", m->name);
                        return -1;
                }
-               fprintf(f, "\ttarget ");
-               m->print(NULL, m->t, 0);
-               if (tb[TCA_IPT_INDEX] == NULL) {
-                       fprintf(f, " [NULL ipt target index ]\n");
-               } else {
-                       __u32 index;
-                       index = *(__u32 *) RTA_DATA(tb[TCA_IPT_INDEX]);
-                       fprintf(f, " \n\tindex %d", index);
-               }
 
-               if (tb[TCA_IPT_CNT]) {
-                       struct tc_cnt *c  = RTA_DATA(tb[TCA_IPT_CNT]);;
-                       fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
-               }
-               if (show_stats) {
-                       if (tb[TCA_IPT_TM]) {
-                               struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
-                               print_tm(f,tm);
-                       }
-               }
-               fprintf(f, " \n");
+               opts =
+                       merge_options(opts, m->extra_opts,
+                                     &m->option_offset);
+       } else {
+               fprintf(stderr, " failed to find target %s\n\n",
+                       t->u.user.name);
+               return -1;
+       }
+       fprintf(f, "\ttarget ");
+       m->print(NULL, m->t, 0);
+       if (tb[TCA_IPT_INDEX] == NULL) {
+               fprintf(f, " [NULL ipt target index ]\n");
+       } else {
+               __u32 index;
 
+               index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
+               fprintf(f, "\n\tindex %u", index);
        }
+
+       if (tb[TCA_IPT_CNT]) {
+               struct tc_cnt *c  = RTA_DATA(tb[TCA_IPT_CNT]);
+
+               fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
+       }
+       if (show_stats) {
+               if (tb[TCA_IPT_TM]) {
+                       struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
+
+                       print_tm(f, tm);
+               }
+       }
+       fprintf(f, "\n");
+
        free_opts(opts);
 
        return 0;
 }
 
 struct action_util ipt_action_util = {
-        .id = "ipt",
-        .parse_aopt = parse_ipt,
-        .print_aopt = print_ipt,
+       .id = "ipt",
+       .parse_aopt = parse_ipt,
+       .print_aopt = print_ipt,
 };
-