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