iproute2/tc/m_xt.c
<<
>>
Prefs
   1/*
   2 * m_xt.c       xtables based targets
   3 *              utilities mostly ripped from iptables <duh, its the linux way>
   4 *
   5 *              This program is free software; you can distribute it and/or
   6 *              modify it under the terms of the GNU General Public License
   7 *              as published by the Free Software Foundation; either version
   8 *              2 of the License, or (at your option) any later version.
   9 *
  10 * Authors:  J Hadi Salim (hadi@cyberus.ca)
  11 */
  12
  13#include <sys/socket.h>
  14#include <netinet/in.h>
  15#include <arpa/inet.h>
  16#include <net/if.h>
  17#include <limits.h>
  18#include <linux/netfilter.h>
  19#include <linux/netfilter_ipv4/ip_tables.h>
  20#include <xtables.h>
  21#include "utils.h"
  22#include "tc_util.h"
  23#include <linux/tc_act/tc_ipt.h>
  24#include <stdio.h>
  25#include <dlfcn.h>
  26#include <getopt.h>
  27#include <errno.h>
  28#include <string.h>
  29#include <netdb.h>
  30#include <stdlib.h>
  31#include <ctype.h>
  32#include <stdarg.h>
  33#include <unistd.h>
  34#include <fcntl.h>
  35#include <sys/wait.h>
  36#ifndef XT_LIB_DIR
  37#       define XT_LIB_DIR "/lib/xtables"
  38#endif
  39
  40#ifndef __ALIGN_KERNEL
  41#define __ALIGN_KERNEL(x, a)    \
  42        __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
  43#define __ALIGN_KERNEL_MASK(x, mask) \
  44        (((x) + (mask)) & ~(mask))
  45#endif
  46
  47#ifndef ALIGN
  48#define ALIGN(x, a)     __ALIGN_KERNEL((x), (a))
  49#endif
  50
  51static const char *tname = "mangle";
  52
  53char *lib_dir;
  54
  55static const char * const ipthooks[] = {
  56        "NF_IP_PRE_ROUTING",
  57        "NF_IP_LOCAL_IN",
  58        "NF_IP_FORWARD",
  59        "NF_IP_LOCAL_OUT",
  60        "NF_IP_POST_ROUTING",
  61};
  62
  63static struct option original_opts[] = {
  64        {
  65                .name = "jump",
  66                .has_arg = 1,
  67                .val = 'j'
  68        },
  69        {0, 0, 0, 0}
  70};
  71
  72static struct xtables_globals tcipt_globals = {
  73        .option_offset = 0,
  74        .program_name = "tc-ipt",
  75        .program_version = "0.2",
  76        .orig_opts = original_opts,
  77        .opts = original_opts,
  78        .exit_err = NULL,
  79#if XTABLES_VERSION_CODE >= 11
  80        .compat_rev = xtables_compatible_revision,
  81#endif
  82};
  83
  84/*
  85 * we may need to check for version mismatch
  86*/
  87static int
  88build_st(struct xtables_target *target, struct xt_entry_target *t)
  89{
  90
  91        size_t size =
  92                    XT_ALIGN(sizeof(struct xt_entry_target)) + target->size;
  93
  94        if (t == NULL) {
  95                target->t = xtables_calloc(1, size);
  96                target->t->u.target_size = size;
  97                strncpy(target->t->u.user.name, target->name,
  98                        sizeof(target->t->u.user.name) - 1);
  99                target->t->u.user.revision = target->revision;
 100
 101                if (target->init != NULL)
 102                        target->init(target->t);
 103        } else {
 104                target->t = t;
 105        }
 106        return 0;
 107
 108}
 109
 110static void set_lib_dir(void)
 111{
 112
 113        lib_dir = getenv("XTABLES_LIBDIR");
 114        if (!lib_dir) {
 115                lib_dir = getenv("IPTABLES_LIB_DIR");
 116                if (lib_dir)
 117                        fprintf(stderr, "using deprecated IPTABLES_LIB_DIR\n");
 118        }
 119        if (lib_dir == NULL)
 120                lib_dir = XT_LIB_DIR;
 121
 122}
 123
 124static int get_xtables_target_opts(struct xtables_globals *globals,
 125                                   struct xtables_target *m)
 126{
 127        struct option *opts;
 128
 129#if XTABLES_VERSION_CODE >= 6
 130        opts = xtables_options_xfrm(globals->orig_opts,
 131                                    globals->opts,
 132                                    m->x6_options,
 133                                    &m->option_offset);
 134#else
 135        opts = xtables_merge_options(globals->opts,
 136                                     m->extra_opts,
 137                                     &m->option_offset);
 138#endif
 139        if (!opts)
 140                return -1;
 141        globals->opts = opts;
 142        return 0;
 143}
 144
 145static int parse_ipt(struct action_util *a, int *argc_p,
 146                     char ***argv_p, int tca_id, struct nlmsghdr *n)
 147{
 148        struct xtables_target *m = NULL;
 149#if XTABLES_VERSION_CODE >= 6
 150        struct ipt_entry fw = {};
 151#endif
 152        struct rtattr *tail;
 153
 154        int c;
 155        char **argv = *argv_p;
 156        int argc;
 157        char k[FILTER_NAMESZ];
 158        int size = 0;
 159        int iok = 0, ok = 0;
 160        __u32 hook = 0, index = 0;
 161
 162        /* copy tcipt_globals because .opts will be modified by iptables */
 163        struct xtables_globals tmp_tcipt_globals = tcipt_globals;
 164
 165        xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
 166        set_lib_dir();
 167
 168        /* parse only up until the next action */
 169        for (argc = 0; argc < *argc_p; argc++) {
 170                if (!argv[argc] || !strcmp(argv[argc], "action"))
 171                        break;
 172        }
 173
 174        if (argc <= 2) {
 175                fprintf(stderr,
 176                        "too few arguments for xt, need at least '-j <target>'\n");
 177                return -1;
 178        }
 179
 180        while (1) {
 181                c = getopt_long(argc, argv, "j:", tmp_tcipt_globals.opts, NULL);
 182                if (c == -1)
 183                        break;
 184                switch (c) {
 185                case 'j':
 186                        m = xtables_find_target(optarg, XTF_TRY_LOAD);
 187                        if (!m) {
 188                                fprintf(stderr,
 189                                        " failed to find target %s\n\n",
 190                                        optarg);
 191                                return -1;
 192                        }
 193
 194                        if (build_st(m, NULL) < 0) {
 195                                printf(" %s error\n", m->name);
 196                                return -1;
 197                        }
 198
 199                        if (get_xtables_target_opts(&tmp_tcipt_globals,
 200                                                    m) < 0) {
 201                                fprintf(stderr,
 202                                        " failed to find additional options for target %s\n\n",
 203                                        optarg);
 204                                return -1;
 205                        }
 206                        ok++;
 207                        break;
 208
 209                default:
 210#if XTABLES_VERSION_CODE >= 6
 211                        if (m != NULL && m->x6_parse != NULL) {
 212                                xtables_option_tpcall(c, argv, 0, m, &fw);
 213#else
 214                        if (m != NULL && m->parse != NULL) {
 215                                m->parse(c - m->option_offset, argv, 0,
 216                                         &m->tflags, NULL, &m->t);
 217#endif
 218                        } else {
 219                                fprintf(stderr,
 220                                        "failed to find target %s\n\n", optarg);
 221                                return -1;
 222
 223                        }
 224                        ok++;
 225                        break;
 226                }
 227        }
 228
 229        if (argc > optind) {
 230                if (matches(argv[optind], "index") == 0) {
 231                        if (get_u32(&index, argv[optind + 1], 10)) {
 232                                fprintf(stderr, "Illegal \"index\"\n");
 233                                xtables_free_opts(1);
 234                                return -1;
 235                        }
 236                        iok++;
 237
 238                        optind += 2;
 239                }
 240        }
 241
 242        if (!ok && !iok) {
 243                fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv);
 244                return -1;
 245        }
 246
 247        /* check that we passed the correct parameters to the target */
 248#if XTABLES_VERSION_CODE >= 6
 249        if (m)
 250                xtables_option_tfcall(m);
 251#else
 252        if (m && m->final_check)
 253                m->final_check(m->tflags);
 254#endif
 255
 256        {
 257                struct tcmsg *t = NLMSG_DATA(n);
 258
 259                if (t->tcm_parent != TC_H_ROOT
 260                    && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
 261                        hook = NF_IP_PRE_ROUTING;
 262                } else {
 263                        hook = NF_IP_POST_ROUTING;
 264                }
 265        }
 266
 267        tail = addattr_nest(n, MAX_MSG, tca_id);
 268        fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
 269        fprintf(stdout, "\ttarget: ");
 270
 271        if (m) {
 272                if (m->print)
 273                        m->print(NULL, m->t, 0);
 274                else
 275                        printf("%s ", m->name);
 276        }
 277        fprintf(stdout, " index %d\n", index);
 278
 279        if (strlen(tname) >= 16) {
 280                size = 15;
 281                k[15] = 0;
 282        } else {
 283                size = 1 + strlen(tname);
 284        }
 285        strncpy(k, tname, size);
 286
 287        addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
 288        addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
 289        addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
 290        if (m)
 291                addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
 292        addattr_nest_end(n, tail);
 293
 294        argv += optind;
 295        *argc_p -= argc;
 296        *argv_p = argv;
 297
 298        optind = 0;
 299        xtables_free_opts(1);
 300
 301        if (m) {
 302                /* Clear flags if target will be used again */
 303                m->tflags = 0;
 304                m->used = 0;
 305                /* Free allocated memory */
 306                if (m->t)
 307                        free(m->t);
 308        }
 309
 310        return 0;
 311
 312}
 313
 314static int
 315print_ipt(struct action_util *au, FILE *f, struct rtattr *arg)
 316{
 317        struct xtables_target *m;
 318        struct rtattr *tb[TCA_IPT_MAX + 1];
 319        struct xt_entry_target *t = NULL;
 320        __u32 hook;
 321
 322        if (arg == NULL)
 323                return 0;
 324
 325        /* copy tcipt_globals because .opts will be modified by iptables */
 326        struct xtables_globals tmp_tcipt_globals = tcipt_globals;
 327
 328        xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
 329        set_lib_dir();
 330
 331        parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
 332
 333        if (tb[TCA_IPT_TABLE] == NULL) {
 334                fprintf(stderr, "Missing ipt table name, assuming mangle\n");
 335        } else {
 336                fprintf(f, "tablename: %s ",
 337                        rta_getattr_str(tb[TCA_IPT_TABLE]));
 338        }
 339
 340        if (tb[TCA_IPT_HOOK] == NULL) {
 341                fprintf(stderr, "Missing ipt hook name\n ");
 342                return -1;
 343        }
 344
 345        if (tb[TCA_IPT_TARG] == NULL) {
 346                fprintf(stderr, "Missing ipt target parameters\n");
 347                return -1;
 348        }
 349
 350        hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
 351        fprintf(f, " hook: %s\n", ipthooks[hook]);
 352
 353        t = RTA_DATA(tb[TCA_IPT_TARG]);
 354        m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
 355        if (!m) {
 356                fprintf(stderr, " failed to find target %s\n\n",
 357                        t->u.user.name);
 358                return -1;
 359        }
 360        if (build_st(m, t) < 0) {
 361                fprintf(stderr, " %s error\n", m->name);
 362                return -1;
 363        }
 364
 365        if (get_xtables_target_opts(&tmp_tcipt_globals, m) < 0) {
 366                fprintf(stderr,
 367                        " failed to find additional options for target %s\n\n",
 368                        t->u.user.name);
 369                return -1;
 370        }
 371        fprintf(f, "\ttarget ");
 372        m->print(NULL, m->t, 0);
 373        if (tb[TCA_IPT_INDEX] == NULL) {
 374                fprintf(f, " [NULL ipt target index ]\n");
 375        } else {
 376                __u32 index;
 377
 378                index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
 379                fprintf(f, "\n\tindex %u", index);
 380        }
 381
 382        if (tb[TCA_IPT_CNT]) {
 383                struct tc_cnt *c  = RTA_DATA(tb[TCA_IPT_CNT]);
 384
 385                fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
 386        }
 387        if (show_stats) {
 388                if (tb[TCA_IPT_TM]) {
 389                        struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
 390
 391                        print_tm(f, tm);
 392                }
 393        }
 394        print_nl();
 395
 396        xtables_free_opts(1);
 397
 398        return 0;
 399}
 400
 401struct action_util xt_action_util = {
 402        .id = "xt",
 403        .parse_aopt = parse_ipt,
 404        .print_aopt = print_ipt,
 405};
 406