blob: d6f62bd6b32c98aca862bf437c32205916a00f01 [file] [log] [blame]
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +00001/*
Stephen Hemmingerae665a52006-12-05 10:10:22 -08002 * m_ipt.c iptables based targets
Stephen Hemminger3d0b7432014-12-20 15:47:17 -08003 * utilities mostly ripped from iptables <duh, its the linux way>
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +00004 *
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 *
Stephen Hemmingerae665a52006-12-05 10:10:22 -080010 * Authors: J Hadi Salim (hadi@cyberus.ca)
Stephen Hemmingerde539ec2007-03-06 13:03:19 -080011 */
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000012
13#include <syslog.h>
14#include <sys/socket.h>
15#include <netinet/in.h>
16#include <arpa/inet.h>
17#include <iptables.h>
Stephen Hemmingerece02ea2007-12-10 09:40:45 -080018#include <linux/netfilter.h>
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000019#include <linux/netfilter_ipv4/ip_tables.h>
20#include "utils.h"
21#include "tc_util.h"
22#include <linux/tc_act/tc_ipt.h>
23#include <stdio.h>
24#include <dlfcn.h>
25#include <getopt.h>
26#include <errno.h>
27#include <string.h>
28#include <netdb.h>
29#include <stdlib.h>
30#include <ctype.h>
31#include <stdarg.h>
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000032#include <unistd.h>
33#include <fcntl.h>
34#include <sys/wait.h>
35
Mike Frysinger95dd5952007-03-05 17:50:49 -080036static const char *pname = "tc-ipt";
37static const char *tname = "mangle";
38static const char *pversion = "0.1";
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000039
40static const char *ipthooks[] = {
41 "NF_IP_PRE_ROUTING",
42 "NF_IP_LOCAL_IN",
43 "NF_IP_FORWARD",
44 "NF_IP_LOCAL_OUT",
45 "NF_IP_POST_ROUTING",
46};
47
48static struct option original_opts[] = {
49 {"jump", 1, 0, 'j'},
50 {0, 0, 0, 0}
51};
52
Alexander Aring9b32f892016-05-29 20:27:13 +020053static struct xtables_target *t_list;
shemminger6d4662d2005-06-23 17:36:38 +000054static struct option *opts = original_opts;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070055static unsigned int global_option_offset;
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000056#define OPTION_OFFSET 256
57
Stephen Hemmingerde539ec2007-03-06 13:03:19 -080058char *lib_dir;
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000059
60void
Alexander Aring9b32f892016-05-29 20:27:13 +020061xtables_register_target(struct xtables_target *me)
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000062{
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000063 me->next = t_list;
64 t_list = me;
65
66}
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000067
Alexander Aring9b32f892016-05-29 20:27:13 +020068static void exit_tryhelp(int status)
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000069{
70 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
71 pname, pname);
72 exit(status);
73}
74
Alexander Aring9b32f892016-05-29 20:27:13 +020075static void exit_error(enum xtables_exittype status, char *msg, ...)
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000076{
77 va_list args;
78
79 va_start(args, msg);
80 fprintf(stderr, "%s v%s: ", pname, pversion);
81 vfprintf(stderr, msg, args);
82 va_end(args);
83 fprintf(stderr, "\n");
84 if (status == PARAMETER_PROBLEM)
85 exit_tryhelp(status);
86 if (status == VERSION_PROBLEM)
87 fprintf(stderr,
88 "Perhaps iptables or your kernel needs to be upgraded.\n");
89 exit(status);
90}
91
92/* stolen from iptables 1.2.11
93They should really have them as a library so i can link to them
94Email them next time i remember
95*/
96
Denys Fedoryschenkoa589dcd2009-02-07 08:49:32 -050097static void free_opts(struct option *local_opts)
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +000098{
Denys Fedoryschenkoa589dcd2009-02-07 08:49:32 -050099 if (local_opts != original_opts) {
100 free(local_opts);
shemminger6d4662d2005-06-23 17:36:38 +0000101 opts = original_opts;
102 global_option_offset = 0;
103 }
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000104}
105
106static struct option *
107merge_options(struct option *oldopts, const struct option *newopts,
108 unsigned int *option_offset)
109{
110 struct option *merge;
111 unsigned int num_old, num_new, i;
112
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700113 for (num_old = 0; oldopts[num_old].name; num_old++);
114 for (num_new = 0; newopts[num_new].name; num_new++);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000115
116 *option_offset = global_option_offset + OPTION_OFFSET;
117
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700118 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
119 memcpy(merge, oldopts, num_old * sizeof(struct option));
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000120 for (i = 0; i < num_new; i++) {
121 merge[num_old + i] = newopts[i];
122 merge[num_old + i].val += *option_offset;
123 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700124 memset(merge + num_old + num_new, 0, sizeof(struct option));
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000125
126 return merge;
127}
128
129static void *
130fw_calloc(size_t count, size_t size)
131{
132 void *p;
133
134 if ((p = (void *) calloc(count, size)) == NULL) {
135 perror("iptables: calloc failed");
136 exit(1);
137 }
138 return p;
139}
140
Alexander Aring9b32f892016-05-29 20:27:13 +0200141static struct xtables_target *
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000142find_t(char *name)
143{
Alexander Aring9b32f892016-05-29 20:27:13 +0200144 struct xtables_target *m;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700145
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000146 for (m = t_list; m; m = m->next) {
147 if (strcmp(m->name, name) == 0)
148 return m;
149 }
150
151 return NULL;
152}
153
Alexander Aring9b32f892016-05-29 20:27:13 +0200154static struct xtables_target *
Stephen Hemmingerde539ec2007-03-06 13:03:19 -0800155get_target_name(const char *name)
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000156{
157 void *handle;
158 char *error;
159 char *new_name, *lname;
Alexander Aring9b32f892016-05-29 20:27:13 +0200160 struct xtables_target *m;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700161 char path[strlen(lib_dir) + sizeof("/libipt_.so") + strlen(name)];
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000162
Mike Frysingerf2e27cf2009-11-06 06:09:22 -0500163#ifdef NO_SHARED_LIBS
164 return NULL;
165#endif
166
Phil Sutterf89bb022016-07-18 16:48:43 +0200167 new_name = calloc(1, strlen(name) + 1);
168 lname = calloc(1, strlen(name) + 1);
169 if (!new_name)
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000170 exit_error(PARAMETER_PROBLEM, "get_target_name");
Phil Sutterf89bb022016-07-18 16:48:43 +0200171 if (!lname)
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000172 exit_error(PARAMETER_PROBLEM, "get_target_name");
173
174 strcpy(new_name, name);
175 strcpy(lname, name);
176
177 if (isupper(lname[0])) {
178 int i;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700179
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000180 for (i = 0; i < strlen(name); i++) {
181 lname[i] = tolower(lname[i]);
182 }
183 }
184
185 if (islower(new_name[0])) {
186 int i;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700187
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000188 for (i = 0; i < strlen(new_name); i++) {
189 new_name[i] = toupper(new_name[i]);
190 }
191 }
192
Denys Fedoryshchenko53c01782007-12-24 11:51:11 -0500193 /* try libxt_xx first */
194 sprintf(path, "%s/libxt_%s.so", lib_dir, new_name);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000195 handle = dlopen(path, RTLD_LAZY);
196 if (!handle) {
Denys Fedoryshchenko53c01782007-12-24 11:51:11 -0500197 /* try libipt_xx next */
198 sprintf(path, "%s/libipt_%s.so", lib_dir, new_name);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000199 handle = dlopen(path, RTLD_LAZY);
Denys Fedoryshchenko53c01782007-12-24 11:51:11 -0500200
201 if (!handle) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700202 sprintf(path, "%s/libxt_%s.so", lib_dir, lname);
Denys Fedoryshchenko53c01782007-12-24 11:51:11 -0500203 handle = dlopen(path, RTLD_LAZY);
204 }
205
206 if (!handle) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700207 sprintf(path, "%s/libipt_%s.so", lib_dir, lname);
Denys Fedoryshchenko53c01782007-12-24 11:51:11 -0500208 handle = dlopen(path, RTLD_LAZY);
209 }
210 /* ok, lets give up .. */
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000211 if (!handle) {
212 fputs(dlerror(), stderr);
213 printf("\n");
Denys Fedoryshchenko6e34e7d2009-01-06 19:41:50 -0800214 free(new_name);
Thomas Jarosch1a6543c2011-10-03 05:23:30 +0000215 free(lname);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000216 return NULL;
217 }
218 }
219
220 m = dlsym(handle, new_name);
221 if ((error = dlerror()) != NULL) {
Alexander Aring9b32f892016-05-29 20:27:13 +0200222 m = (struct xtables_target *) dlsym(handle, lname);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000223 if ((error = dlerror()) != NULL) {
224 m = find_t(new_name);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700225 if (m == NULL) {
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000226 m = find_t(lname);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700227 if (m == NULL) {
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000228 fputs(error, stderr);
229 fprintf(stderr, "\n");
230 dlclose(handle);
Denys Fedoryshchenko6e34e7d2009-01-06 19:41:50 -0800231 free(new_name);
Thomas Jarosch1a6543c2011-10-03 05:23:30 +0000232 free(lname);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000233 return NULL;
234 }
235 }
236 }
237 }
238
Denys Fedoryshchenko6e34e7d2009-01-06 19:41:50 -0800239 free(new_name);
Thomas Jarosch1a6543c2011-10-03 05:23:30 +0000240 free(lname);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000241 return m;
242}
243
net[shemminger]!shemminger894b1c62005-03-30 18:19:55 +0000244static void set_revision(char *name, u_int8_t revision)
245{
246 /* Old kernel sources don't have ".revision" field,
247 * but we stole a byte from name. */
248 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
249 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
250}
251
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800252/*
net[shemminger]!shemminger894b1c62005-03-30 18:19:55 +0000253 * we may need to check for version mismatch
254*/
Alexander Aring9b32f892016-05-29 20:27:13 +0200255static int build_st(struct xtables_target *target, struct ipt_entry_target *t)
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000256{
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000257 if (target) {
258 size_t size;
259
260 size =
Alexander Aring9b32f892016-05-29 20:27:13 +0200261 XT_ALIGN(sizeof(struct ipt_entry_target)) + target->size;
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000262
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700263 if (t == NULL) {
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000264 target->t = fw_calloc(1, size);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000265 target->t->u.target_size = size;
net[shemminger]!shemminger894b1c62005-03-30 18:19:55 +0000266
267 if (target->init != NULL)
Alexander Aring9b32f892016-05-29 20:27:13 +0200268 target->init(target->t);
net[shemminger]!shemminger894b1c62005-03-30 18:19:55 +0000269 set_revision(target->t->u.user.name, target->revision);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000270 } else {
271 target->t = t;
272 }
273 strcpy(target->t->u.user.name, target->name);
274 return 0;
275 }
276
277 return -1;
278}
279
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700280static int parse_ipt(struct action_util *a, int *argc_p,
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000281 char ***argv_p, int tca_id, struct nlmsghdr *n)
282{
Alexander Aring9b32f892016-05-29 20:27:13 +0200283 struct xtables_target *m = NULL;
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000284 struct ipt_entry fw;
285 struct rtattr *tail;
286 int c;
287 int rargc = *argc_p;
288 char **argv = *argv_p;
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000289 int argc = 0, iargc = 0;
290 char k[16];
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000291 int size = 0;
292 int iok = 0, ok = 0;
293 __u32 hook = 0, index = 0;
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000294
Stephen Hemmingerde539ec2007-03-06 13:03:19 -0800295 lib_dir = getenv("IPTABLES_LIB_DIR");
296 if (!lib_dir)
297 lib_dir = IPT_LIB_DIR;
298
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000299 {
300 int i;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700301
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000302 for (i = 0; i < rargc; i++) {
303 if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) {
304 break;
305 }
306 }
307 iargc = argc = i;
308 }
309
310 if (argc <= 2) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700311 fprintf(stderr, "bad arguments to ipt %d vs %d\n", argc, rargc);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000312 return -1;
313 }
314
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000315 while (1) {
316 c = getopt_long(argc, argv, "j:", opts, NULL);
317 if (c == -1)
318 break;
319 switch (c) {
320 case 'j':
321 m = get_target_name(optarg);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700322 if (m != NULL) {
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000323
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700324 if (build_st(m, NULL) < 0) {
325 printf(" %s error\n", m->name);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000326 return -1;
327 }
328 opts =
329 merge_options(opts, m->extra_opts,
330 &m->option_offset);
331 } else {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700332 fprintf(stderr, " failed to find target %s\n\n", optarg);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000333 return -1;
334 }
335 ok++;
336 break;
337
338 default:
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700339 memset(&fw, 0, sizeof(fw));
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000340 if (m) {
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000341 m->parse(c - m->option_offset, argv, 0,
shemminger6d4662d2005-06-23 17:36:38 +0000342 &m->tflags, NULL, &m->t);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000343 } else {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700344 fprintf(stderr, " failed to find target %s\n\n", optarg);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000345 return -1;
346
347 }
348 ok++;
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000349 break;
350
351 }
352 }
353
354 if (iargc > optind) {
355 if (matches(argv[optind], "index") == 0) {
356 if (get_u32(&index, argv[optind + 1], 10)) {
357 fprintf(stderr, "Illegal \"index\"\n");
shemminger6d4662d2005-06-23 17:36:38 +0000358 free_opts(opts);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000359 return -1;
360 }
361 iok++;
362
363 optind += 2;
364 }
365 }
366
367 if (!ok && !iok) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700368 fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000369 return -1;
370 }
371
shemminger6d4662d2005-06-23 17:36:38 +0000372 /* check that we passed the correct parameters to the target */
373 if (m)
374 m->final_check(m->tflags);
375
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000376 {
377 struct tcmsg *t = NLMSG_DATA(n);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700378
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000379 if (t->tcm_parent != TC_H_ROOT
380 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
381 hook = NF_IP_PRE_ROUTING;
382 } else {
383 hook = NF_IP_POST_ROUTING;
384 }
385 }
386
2!tgraffc78a8e2005-01-18 01:24:18 +0000387 tail = NLMSG_TAIL(n);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000388 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
389 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
390 fprintf(stdout, "\ttarget: ");
391
392 if (m)
393 m->print(NULL, m->t, 0);
394 fprintf(stdout, " index %d\n", index);
395
396 if (strlen(tname) > 16) {
397 size = 16;
398 k[15] = 0;
399 } else {
400 size = 1 + strlen(tname);
401 }
402 strncpy(k, tname, size);
403
404 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
405 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
406 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
407 if (m)
408 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
2!tgraffc78a8e2005-01-18 01:24:18 +0000409 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000410
411 argc -= optind;
412 argv += optind;
413 *argc_p = rargc - iargc;
414 *argv_p = argv;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800415
Denys Fedoryshchenko6e34e7d2009-01-06 19:41:50 -0800416 optind = 0;
shemminger6d4662d2005-06-23 17:36:38 +0000417 free_opts(opts);
Denys Fedoryshchenko6e34e7d2009-01-06 19:41:50 -0800418 /* Clear flags if target will be used again */
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700419 m->tflags = 0;
420 m->used = 0;
Denys Fedoryshchenko6e34e7d2009-01-06 19:41:50 -0800421 /* Free allocated memory */
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700422 if (m->t)
423 free(m->t);
Denys Fedoryshchenko6e34e7d2009-01-06 19:41:50 -0800424
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000425
426 return 0;
427
428}
429
430static int
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700431print_ipt(struct action_util *au, FILE * f, struct rtattr *arg)
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000432{
433 struct rtattr *tb[TCA_IPT_MAX + 1];
434 struct ipt_entry_target *t = NULL;
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000435
436 if (arg == NULL)
437 return -1;
438
Patrick McHardyc6ab5b82007-06-20 01:37:53 +0200439 lib_dir = getenv("IPTABLES_LIB_DIR");
440 if (!lib_dir)
441 lib_dir = IPT_LIB_DIR;
442
2!tgraf78934002005-01-18 22:11:58 +0000443 parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000444
445 if (tb[TCA_IPT_TABLE] == NULL) {
446 fprintf(f, "[NULL ipt table name ] assuming mangle ");
447 } else {
448 fprintf(f, "tablename: %s ",
Stephen Hemmingerff247462012-04-10 08:47:55 -0700449 rta_getattr_str(tb[TCA_IPT_TABLE]));
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000450 }
451
452 if (tb[TCA_IPT_HOOK] == NULL) {
453 fprintf(f, "[NULL ipt hook name ]\n ");
454 return -1;
455 } else {
456 __u32 hook;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700457
Stephen Hemmingerff247462012-04-10 08:47:55 -0700458 hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700459 fprintf(f, " hook: %s\n", ipthooks[hook]);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000460 }
461
462 if (tb[TCA_IPT_TARG] == NULL) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700463 fprintf(f, "\t[NULL ipt target parameters ]\n");
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000464 return -1;
465 } else {
Alexander Aring9b32f892016-05-29 20:27:13 +0200466 struct xtables_target *m = NULL;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700467
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000468 t = RTA_DATA(tb[TCA_IPT_TARG]);
469 m = get_target_name(t->u.user.name);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700470 if (m != NULL) {
471 if (build_st(m, t) < 0) {
472 fprintf(stderr, " %s error\n", m->name);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000473 return -1;
474 }
475
476 opts =
477 merge_options(opts, m->extra_opts,
478 &m->option_offset);
479 } else {
480 fprintf(stderr, " failed to find target %s\n\n",
481 t->u.user.name);
482 return -1;
483 }
484 fprintf(f, "\ttarget ");
485 m->print(NULL, m->t, 0);
486 if (tb[TCA_IPT_INDEX] == NULL) {
487 fprintf(f, " [NULL ipt target index ]\n");
488 } else {
489 __u32 index;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700490
Stephen Hemmingerff247462012-04-10 08:47:55 -0700491 index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700492 fprintf(f, "\n\tindex %d", index);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000493 }
494
495 if (tb[TCA_IPT_CNT]) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700496 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);
497
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000498 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
499 }
500 if (show_stats) {
501 if (tb[TCA_IPT_TM]) {
502 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700503
504 print_tm(f, tm);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000505 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800506 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700507 fprintf(f, "\n");
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000508
509 }
shemminger6d4662d2005-06-23 17:36:38 +0000510 free_opts(opts);
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000511
512 return 0;
513}
514
515struct action_util ipt_action_util = {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700516 .id = "ipt",
517 .parse_aopt = parse_ipt,
518 .print_aopt = print_ipt,
net[shemminger]!shemminger1ffd7fd2005-01-17 23:26:23 +0000519};