blob: c3f866df14749ff61a4a2e8b963f943028c14951 [file] [log] [blame]
Andreas Henrikssona36ceb82009-12-02 16:11:50 +01001/*
2 * m_xt.c xtables based targets
Stephen Hemminger3d0b7432014-12-20 15:47:17 -08003 * utilities mostly ripped from iptables <duh, its the linux way>
Andreas Henrikssona36ceb82009-12-02 16:11:50 +01004 *
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 Henrikssona36ceb82009-12-02 16:11:50 +010034#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 Duyckcfa292d2013-04-29 05:50:13 +000041#ifndef __ALIGN_KERNEL
42#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
43#define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
44#endif
45
Stephen Hemminger609ceb82010-03-29 15:17:48 -070046#ifndef ALIGN
Stephen Hemminger32a121c2016-03-21 11:48:36 -070047#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
Stephen Hemminger609ceb82010-03-29 15:17:48 -070048#endif
49
Andreas Henrikssona36ceb82009-12-02 16:11:50 +010050static const char *tname = "mangle";
51
52char *lib_dir;
53
54static const char *ipthooks[] = {
55 "NF_IP_PRE_ROUTING",
56 "NF_IP_LOCAL_IN",
57 "NF_IP_FORWARD",
58 "NF_IP_LOCAL_OUT",
59 "NF_IP_POST_ROUTING",
60};
61
62static struct option original_opts[] = {
63 {
64 .name = "jump",
65 .has_arg = 1,
66 .val = 'j'
67 },
68 {0, 0, 0, 0}
69};
70
71static struct xtables_globals tcipt_globals = {
72 .option_offset = 0,
73 .program_name = "tc-ipt",
74 .program_version = "0.2",
75 .orig_opts = original_opts,
76 .opts = original_opts,
77 .exit_err = NULL,
78};
79
80/*
81 * we may need to check for version mismatch
82*/
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080083static int
Andreas Henrikssona36ceb82009-12-02 16:11:50 +010084build_st(struct xtables_target *target, struct xt_entry_target *t)
85{
86
87 size_t size =
Stephen Hemminger32a121c2016-03-21 11:48:36 -070088 XT_ALIGN(sizeof(struct xt_entry_target)) + target->size;
Andreas Henrikssona36ceb82009-12-02 16:11:50 +010089
Stephen Hemminger32a121c2016-03-21 11:48:36 -070090 if (t == NULL) {
Andreas Henrikssona36ceb82009-12-02 16:11:50 +010091 target->t = xtables_calloc(1, size);
92 target->t->u.target_size = size;
93 strcpy(target->t->u.user.name, target->name);
Mike Frysingerbe3c4d42010-11-21 16:16:54 -050094 target->t->u.user.revision = target->revision;
Andreas Henrikssona36ceb82009-12-02 16:11:50 +010095
96 if (target->init != NULL)
97 target->init(target->t);
98 } else {
99 target->t = t;
100 }
101 return 0;
102
103}
104
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800105static void set_lib_dir(void)
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100106{
107
108 lib_dir = getenv("XTABLES_LIBDIR");
109 if (!lib_dir) {
110 lib_dir = getenv("IPTABLES_LIB_DIR");
111 if (lib_dir)
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700112 fprintf(stderr, "using deprecated IPTABLES_LIB_DIR\n");
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100113 }
114 if (lib_dir == NULL)
115 lib_dir = XT_LIB_DIR;
116
117}
118
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700119static int parse_ipt(struct action_util *a, int *argc_p,
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100120 char ***argv_p, int tca_id, struct nlmsghdr *n)
121{
122 struct xtables_target *m = NULL;
123 struct ipt_entry fw;
124 struct rtattr *tail;
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000125
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100126 int c;
127 int rargc = *argc_p;
128 char **argv = *argv_p;
129 int argc = 0, iargc = 0;
130 char k[16];
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100131 int size = 0;
132 int iok = 0, ok = 0;
133 __u32 hook = 0, index = 0;
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000134 struct option *opts = NULL;
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100135
136 xtables_init_all(&tcipt_globals, NFPROTO_IPV4);
137 set_lib_dir();
138
139 {
140 int i;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700141
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100142 for (i = 0; i < rargc; i++) {
143 if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) {
144 break;
145 }
146 }
147 iargc = argc = i;
148 }
149
150 if (argc <= 2) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700151 fprintf(stderr, "bad arguments to ipt %d vs %d\n", argc, rargc);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100152 return -1;
153 }
154
155 while (1) {
156 c = getopt_long(argc, argv, "j:", tcipt_globals.opts, NULL);
157 if (c == -1)
158 break;
159 switch (c) {
160 case 'j':
161 m = xtables_find_target(optarg, XTF_TRY_LOAD);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700162 if (m != NULL) {
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100163
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700164 if (build_st(m, NULL) < 0) {
165 printf(" %s error\n", m->name);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100166 return -1;
167 }
Andreas Henriksson73de5d92011-07-04 05:17:42 +0000168#if (XTABLES_VERSION_CODE >= 6)
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000169 opts = xtables_options_xfrm(tcipt_globals.orig_opts,
170 tcipt_globals.opts,
171 m->x6_options,
172 &m->option_offset);
173#else
Alexander Duyckcfa292d2013-04-29 05:50:13 +0000174 opts = xtables_merge_options(tcipt_globals.opts,
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000175 m->extra_opts,
176 &m->option_offset);
Andreas Henriksson73de5d92011-07-04 05:17:42 +0000177#endif
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000178 if (opts == NULL) {
Stephen Hemmingerb2e116d2014-12-03 19:28:34 -0800179 fprintf(stderr, " failed to find additional options for target %s\n\n", optarg);
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000180 return -1;
181 } else
182 tcipt_globals.opts = opts;
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100183 } else {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700184 fprintf(stderr, " failed to find target %s\n\n", optarg);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100185 return -1;
186 }
187 ok++;
188 break;
189
190 default:
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700191 memset(&fw, 0, sizeof(fw));
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000192#if (XTABLES_VERSION_CODE >= 6)
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700193 if (m != NULL && m->x6_parse != NULL) {
194 xtables_option_tpcall(c, argv, 0, m, NULL);
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000195#else
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700196 if (m != NULL && m->parse != NULL) {
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000197 m->parse(c - m->option_offset, argv, 0, &m->tflags,
198 NULL, &m->t);
199#endif
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100200 } else {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700201 fprintf(stderr, "failed to find target %s\n\n", optarg);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100202 return -1;
203
204 }
205 ok++;
206 break;
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100207 }
208 }
209
210 if (iargc > optind) {
211 if (matches(argv[optind], "index") == 0) {
212 if (get_u32(&index, argv[optind + 1], 10)) {
213 fprintf(stderr, "Illegal \"index\"\n");
214 xtables_free_opts(1);
215 return -1;
216 }
217 iok++;
218
219 optind += 2;
220 }
221 }
222
223 if (!ok && !iok) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700224 fprintf(stderr, " ipt Parser BAD!! (%s)\n", *argv);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100225 return -1;
226 }
227
228 /* check that we passed the correct parameters to the target */
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000229#if (XTABLES_VERSION_CODE >= 6)
230 if (m)
231 xtables_option_tfcall(m);
232#else
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100233 if (m && m->final_check)
234 m->final_check(m->tflags);
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000235#endif
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100236
237 {
238 struct tcmsg *t = NLMSG_DATA(n);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700239
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100240 if (t->tcm_parent != TC_H_ROOT
241 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
242 hook = NF_IP_PRE_ROUTING;
243 } else {
244 hook = NF_IP_POST_ROUTING;
245 }
246 }
247
248 tail = NLMSG_TAIL(n);
249 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
250 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
251 fprintf(stdout, "\ttarget: ");
252
253 if (m)
254 m->print(NULL, m->t, 0);
255 fprintf(stdout, " index %d\n", index);
256
257 if (strlen(tname) > 16) {
258 size = 16;
259 k[15] = 0;
260 } else {
261 size = 1 + strlen(tname);
262 }
263 strncpy(k, tname, size);
264
265 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
266 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
267 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
268 if (m)
269 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
270 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
271
272 argc -= optind;
273 argv += optind;
274 *argc_p = rargc - iargc;
275 *argv_p = argv;
276
277 optind = 0;
278 xtables_free_opts(1);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100279
Dan McGee4f3626f2011-08-31 12:18:49 -0700280 if (m) {
281 /* Clear flags if target will be used again */
282 m->tflags = 0;
283 m->used = 0;
284 /* Free allocated memory */
285 if (m->t)
286 free(m->t);
287 }
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100288
289 return 0;
290
291}
292
293static int
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700294print_ipt(struct action_util *au, FILE * f, struct rtattr *arg)
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100295{
296 struct rtattr *tb[TCA_IPT_MAX + 1];
297 struct xt_entry_target *t = NULL;
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000298 struct option *opts = NULL;
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100299
300 if (arg == NULL)
301 return -1;
302
Andreas Greve6e2e5ec2014-05-10 11:19:18 +0200303 /* copy tcipt_globals because .opts will be modified by iptables */
304 struct xtables_globals tmp_tcipt_globals = tcipt_globals;
305
306 xtables_init_all(&tmp_tcipt_globals, NFPROTO_IPV4);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100307 set_lib_dir();
308
309 parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
310
311 if (tb[TCA_IPT_TABLE] == NULL) {
312 fprintf(f, "[NULL ipt table name ] assuming mangle ");
313 } else {
314 fprintf(f, "tablename: %s ",
Stephen Hemmingerff247462012-04-10 08:47:55 -0700315 rta_getattr_str(tb[TCA_IPT_TABLE]));
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100316 }
317
318 if (tb[TCA_IPT_HOOK] == NULL) {
319 fprintf(f, "[NULL ipt hook name ]\n ");
320 return -1;
321 } else {
322 __u32 hook;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700323
Stephen Hemmingerff247462012-04-10 08:47:55 -0700324 hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700325 fprintf(f, " hook: %s\n", ipthooks[hook]);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100326 }
327
328 if (tb[TCA_IPT_TARG] == NULL) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700329 fprintf(f, "\t[NULL ipt target parameters ]\n");
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100330 return -1;
331 } else {
332 struct xtables_target *m = NULL;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700333
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100334 t = RTA_DATA(tb[TCA_IPT_TARG]);
335 m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700336 if (m != NULL) {
337 if (build_st(m, t) < 0) {
338 fprintf(stderr, " %s error\n", m->name);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100339 return -1;
340 }
341
Andreas Henriksson73de5d92011-07-04 05:17:42 +0000342#if (XTABLES_VERSION_CODE >= 6)
Andreas Greve6e2e5ec2014-05-10 11:19:18 +0200343 opts = xtables_options_xfrm(tmp_tcipt_globals.orig_opts,
344 tmp_tcipt_globals.opts,
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000345 m->x6_options,
346 &m->option_offset);
347#else
Andreas Greve6e2e5ec2014-05-10 11:19:18 +0200348 opts = xtables_merge_options(tmp_tcipt_globals.opts,
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000349 m->extra_opts,
350 &m->option_offset);
Andreas Henriksson73de5d92011-07-04 05:17:42 +0000351#endif
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000352 if (opts == NULL) {
Stephen Hemmingerb2e116d2014-12-03 19:28:34 -0800353 fprintf(stderr, " failed to find additional options for target %s\n\n", optarg);
Jamal Hadi Salim852d5122012-12-16 10:41:39 +0000354 return -1;
355 } else
Andreas Greve6e2e5ec2014-05-10 11:19:18 +0200356 tmp_tcipt_globals.opts = opts;
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100357 } else {
358 fprintf(stderr, " failed to find target %s\n\n",
359 t->u.user.name);
360 return -1;
361 }
362 fprintf(f, "\ttarget ");
363 m->print(NULL, m->t, 0);
364 if (tb[TCA_IPT_INDEX] == NULL) {
365 fprintf(f, " [NULL ipt target index ]\n");
366 } else {
367 __u32 index;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700368
Stephen Hemmingerff247462012-04-10 08:47:55 -0700369 index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700370 fprintf(f, "\n\tindex %d", index);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100371 }
372
373 if (tb[TCA_IPT_CNT]) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700374 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);
375
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100376 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
377 }
378 if (show_stats) {
379 if (tb[TCA_IPT_TM]) {
380 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700381
382 print_tm(f, tm);
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100383 }
384 }
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700385 fprintf(f, "\n");
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100386
387 }
388 xtables_free_opts(1);
389
390 return 0;
391}
392
Jan Engelhardt8e91a802011-06-01 00:52:07 +0200393struct action_util xt_action_util = {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700394 .id = "xt",
395 .parse_aopt = parse_ipt,
396 .print_aopt = print_ipt,
Andreas Henrikssona36ceb82009-12-02 16:11:50 +0100397};