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