]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_xt.c
Add missing limits.h
[mirror_iproute2.git] / tc / m_xt.c
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 /*XXX: in the future (xtables 1.4.3?) get rid of everything tagged
14 * as TC_CONFIG_XT_H */
15
16 #include <syslog.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <net/if.h>
21 #include <limits.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter_ipv4/ip_tables.h>
24 #include <xtables.h>
25 #include "utils.h"
26 #include "tc_util.h"
27 #include <linux/tc_act/tc_ipt.h>
28 #include <stdio.h>
29 #include <dlfcn.h>
30 #include <getopt.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <netdb.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <stdarg.h>
37 #include <limits.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <sys/wait.h>
41 #ifdef TC_CONFIG_XT_H
42 #include "xt-internal.h"
43 #endif
44
45 static const char *pname = "tc-ipt";
46 static const char *tname = "mangle";
47 static const char *pversion = "0.2";
48
49 static const char *ipthooks[] = {
50 "NF_IP_PRE_ROUTING",
51 "NF_IP_LOCAL_IN",
52 "NF_IP_FORWARD",
53 "NF_IP_LOCAL_OUT",
54 "NF_IP_POST_ROUTING",
55 };
56
57 static struct option original_opts[] = {
58 {"jump", 1, 0, 'j'},
59 {0, 0, 0, 0}
60 };
61
62 static struct option *opts = original_opts;
63 static unsigned int global_option_offset = 0;
64 char *lib_dir;
65 const char *program_version = XTABLES_VERSION;
66 const char *program_name = "tc-ipt";
67 struct afinfo afinfo = {
68 .family = AF_INET,
69 .libprefix = "libxt_",
70 .ipproto = IPPROTO_IP,
71 .kmod = "ip_tables",
72 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
73 };
74
75
76 #define OPTION_OFFSET 256
77
78 /*XXX: TC_CONFIG_XT_H */
79 static void free_opts(struct option *local_opts)
80 {
81 if (local_opts != original_opts) {
82 free(local_opts);
83 opts = original_opts;
84 global_option_offset = 0;
85 }
86 }
87
88 /*XXX: TC_CONFIG_XT_H */
89 static struct option *
90 merge_options(struct option *oldopts, const struct option *newopts,
91 unsigned int *option_offset)
92 {
93 struct option *merge;
94 unsigned int num_old, num_new, i;
95
96 for (num_old = 0; oldopts[num_old].name; num_old++) ;
97 for (num_new = 0; newopts[num_new].name; num_new++) ;
98
99 *option_offset = global_option_offset + OPTION_OFFSET;
100
101 merge = malloc(sizeof (struct option) * (num_new + num_old + 1));
102 memcpy(merge, oldopts, num_old * sizeof (struct option));
103 for (i = 0; i < num_new; i++) {
104 merge[num_old + i] = newopts[i];
105 merge[num_old + i].val += *option_offset;
106 }
107 memset(merge + num_old + num_new, 0, sizeof (struct option));
108
109 return merge;
110 }
111
112
113 /*XXX: TC_CONFIG_XT_H */
114 #ifndef TRUE
115 #define TRUE 1
116 #endif
117 #ifndef FALSE
118 #define FALSE 0
119 #endif
120
121 /*XXX: TC_CONFIG_XT_H */
122 int
123 check_inverse(const char option[], int *invert, int *my_optind, int argc)
124 {
125 if (option && strcmp(option, "!") == 0) {
126 if (*invert)
127 exit_error(PARAMETER_PROBLEM,
128 "Multiple `!' flags not allowed");
129 *invert = TRUE;
130 if (my_optind != NULL) {
131 ++*my_optind;
132 if (argc && *my_optind > argc)
133 exit_error(PARAMETER_PROBLEM,
134 "no argument following `!'");
135 }
136
137 return TRUE;
138 }
139 return FALSE;
140 }
141
142 /*XXX: TC_CONFIG_XT_H */
143 void exit_error(enum exittype status, const char *msg, ...)
144 {
145 va_list args;
146
147 va_start(args, msg);
148 fprintf(stderr, "%s v%s: ", pname, pversion);
149 vfprintf(stderr, msg, args);
150 va_end(args);
151 fprintf(stderr, "\n");
152 /* On error paths, make sure that we don't leak memory */
153 exit(status);
154 }
155
156 /*XXX: TC_CONFIG_XT_H */
157 static void set_revision(char *name, u_int8_t revision)
158 {
159 /* Old kernel sources don't have ".revision" field,
160 * but we stole a byte from name. */
161 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
162 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
163 }
164
165 /*
166 * we may need to check for version mismatch
167 */
168 int
169 build_st(struct xtables_target *target, struct xt_entry_target *t)
170 {
171
172 size_t size =
173 XT_ALIGN(sizeof (struct xt_entry_target)) + target->size;
174
175 if (NULL == t) {
176 target->t = fw_calloc(1, size);
177 target->t->u.target_size = size;
178 strcpy(target->t->u.user.name, target->name);
179 set_revision(target->t->u.user.name, target->revision);
180
181 if (target->init != NULL)
182 target->init(target->t);
183 } else {
184 target->t = t;
185 }
186 return 0;
187
188 }
189
190 inline void set_lib_dir(void)
191 {
192
193 lib_dir = getenv("XTABLES_LIBDIR");
194 if (!lib_dir) {
195 lib_dir = getenv("IPTABLES_LIB_DIR");
196 if (lib_dir)
197 fprintf(stderr, "using deprecated IPTABLES_LIB_DIR \n");
198 }
199 if (lib_dir == NULL)
200 lib_dir = XT_LIB_DIR;
201
202 }
203
204 static int parse_ipt(struct action_util *a,int *argc_p,
205 char ***argv_p, int tca_id, struct nlmsghdr *n)
206 {
207 struct xtables_target *m = NULL;
208 struct ipt_entry fw;
209 struct rtattr *tail;
210 int c;
211 int rargc = *argc_p;
212 char **argv = *argv_p;
213 int argc = 0, iargc = 0;
214 char k[16];
215 int res = -1;
216 int size = 0;
217 int iok = 0, ok = 0;
218 __u32 hook = 0, index = 0;
219 res = 0;
220
221 set_lib_dir();
222
223 {
224 int i;
225 for (i = 0; i < rargc; i++) {
226 if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) {
227 break;
228 }
229 }
230 iargc = argc = i;
231 }
232
233 if (argc <= 2) {
234 fprintf(stderr,"bad arguements to ipt %d vs %d \n", argc, rargc);
235 return -1;
236 }
237
238 while (1) {
239 c = getopt_long(argc, argv, "j:", opts, NULL);
240 if (c == -1)
241 break;
242 switch (c) {
243 case 'j':
244 m = find_target(optarg, TRY_LOAD);
245 if (NULL != m) {
246
247 if (0 > build_st(m, NULL)) {
248 printf(" %s error \n", m->name);
249 return -1;
250 }
251 opts =
252 merge_options(opts, m->extra_opts,
253 &m->option_offset);
254 } else {
255 fprintf(stderr," failed to find target %s\n\n", optarg);
256 return -1;
257 }
258 ok++;
259 break;
260
261 default:
262 memset(&fw, 0, sizeof (fw));
263 if (m) {
264 m->parse(c - m->option_offset, argv, 0,
265 &m->tflags, NULL, &m->t);
266 } else {
267 fprintf(stderr," failed to find target %s\n\n", optarg);
268 return -1;
269
270 }
271 ok++;
272 break;
273
274 }
275 }
276
277 if (iargc > optind) {
278 if (matches(argv[optind], "index") == 0) {
279 if (get_u32(&index, argv[optind + 1], 10)) {
280 fprintf(stderr, "Illegal \"index\"\n");
281 free_opts(opts);
282 return -1;
283 }
284 iok++;
285
286 optind += 2;
287 }
288 }
289
290 if (!ok && !iok) {
291 fprintf(stderr," ipt Parser BAD!! (%s)\n", *argv);
292 return -1;
293 }
294
295 /* check that we passed the correct parameters to the target */
296 if (m)
297 m->final_check(m->tflags);
298
299 {
300 struct tcmsg *t = NLMSG_DATA(n);
301 if (t->tcm_parent != TC_H_ROOT
302 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
303 hook = NF_IP_PRE_ROUTING;
304 } else {
305 hook = NF_IP_POST_ROUTING;
306 }
307 }
308
309 tail = NLMSG_TAIL(n);
310 addattr_l(n, MAX_MSG, tca_id, NULL, 0);
311 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
312 fprintf(stdout, "\ttarget: ");
313
314 if (m)
315 m->print(NULL, m->t, 0);
316 fprintf(stdout, " index %d\n", index);
317
318 if (strlen(tname) > 16) {
319 size = 16;
320 k[15] = 0;
321 } else {
322 size = 1 + strlen(tname);
323 }
324 strncpy(k, tname, size);
325
326 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
327 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
328 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
329 if (m)
330 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
331 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
332
333 argc -= optind;
334 argv += optind;
335 *argc_p = rargc - iargc;
336 *argv_p = argv;
337
338 optind = 0;
339 free_opts(opts);
340 /* Clear flags if target will be used again */
341 m->tflags=0;
342 m->used=0;
343 /* Free allocated memory */
344 if (m->t)
345 free(m->t);
346
347
348 return 0;
349
350 }
351
352 static int
353 print_ipt(struct action_util *au,FILE * f, struct rtattr *arg)
354 {
355 struct rtattr *tb[TCA_IPT_MAX + 1];
356 struct xt_entry_target *t = NULL;
357
358 if (arg == NULL)
359 return -1;
360
361 set_lib_dir();
362
363 parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
364
365 if (tb[TCA_IPT_TABLE] == NULL) {
366 fprintf(f, "[NULL ipt table name ] assuming mangle ");
367 } else {
368 fprintf(f, "tablename: %s ",
369 (char *) RTA_DATA(tb[TCA_IPT_TABLE]));
370 }
371
372 if (tb[TCA_IPT_HOOK] == NULL) {
373 fprintf(f, "[NULL ipt hook name ]\n ");
374 return -1;
375 } else {
376 __u32 hook;
377 hook = *(__u32 *) RTA_DATA(tb[TCA_IPT_HOOK]);
378 fprintf(f, " hook: %s \n", ipthooks[hook]);
379 }
380
381 if (tb[TCA_IPT_TARG] == NULL) {
382 fprintf(f, "\t[NULL ipt target parameters ] \n");
383 return -1;
384 } else {
385 struct xtables_target *m = NULL;
386 t = RTA_DATA(tb[TCA_IPT_TARG]);
387 m = find_target(t->u.user.name, TRY_LOAD);
388 if (NULL != m) {
389 if (0 > build_st(m, t)) {
390 fprintf(stderr, " %s error \n", m->name);
391 return -1;
392 }
393
394 opts =
395 merge_options(opts, m->extra_opts,
396 &m->option_offset);
397 } else {
398 fprintf(stderr, " failed to find target %s\n\n",
399 t->u.user.name);
400 return -1;
401 }
402 fprintf(f, "\ttarget ");
403 m->print(NULL, m->t, 0);
404 if (tb[TCA_IPT_INDEX] == NULL) {
405 fprintf(f, " [NULL ipt target index ]\n");
406 } else {
407 __u32 index;
408 index = *(__u32 *) RTA_DATA(tb[TCA_IPT_INDEX]);
409 fprintf(f, " \n\tindex %d", index);
410 }
411
412 if (tb[TCA_IPT_CNT]) {
413 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);;
414 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
415 }
416 if (show_stats) {
417 if (tb[TCA_IPT_TM]) {
418 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
419 print_tm(f,tm);
420 }
421 }
422 fprintf(f, " \n");
423
424 }
425 free_opts(opts);
426
427 return 0;
428 }
429
430 struct action_util ipt_action_util = {
431 .id = "ipt",
432 .parse_aopt = parse_ipt,
433 .print_aopt = print_ipt,
434 };
435