]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - tc/m_xt.c
13bf19f8abf503e02315047afa2e5685aa72751a
[lisovros/iproute2_canprio.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 #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>
34 #include <limits.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <sys/wait.h>
38 #ifndef XT_LIB_DIR
39 #       define XT_LIB_DIR "/lib/xtables"
40 #endif
41
42 #ifndef ALIGN
43 #define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
44 #define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))
45 #endif
46
47 static const char *tname = "mangle";
48
49 char *lib_dir;
50
51 static const char *ipthooks[] = {
52         "NF_IP_PRE_ROUTING",
53         "NF_IP_LOCAL_IN",
54         "NF_IP_FORWARD",
55         "NF_IP_LOCAL_OUT",
56         "NF_IP_POST_ROUTING",
57 };
58
59 static struct option original_opts[] = {
60         {
61                 .name = "jump",
62                 .has_arg = 1,
63                 .val = 'j'
64         },
65         {0, 0, 0, 0}
66 };
67
68 static struct xtables_globals tcipt_globals = {
69         .option_offset = 0,
70         .program_name = "tc-ipt",
71         .program_version = "0.2",
72         .orig_opts = original_opts,
73         .opts = original_opts,
74         .exit_err = NULL,
75 };
76
77 /*
78  * we may need to check for version mismatch
79 */
80 int
81 build_st(struct xtables_target *target, struct xt_entry_target *t)
82 {
83
84         size_t size =
85                     XT_ALIGN(sizeof (struct xt_entry_target)) + target->size;
86
87         if (NULL == t) {
88                 target->t = xtables_calloc(1, size);
89                 target->t->u.target_size = size;
90                 strcpy(target->t->u.user.name, target->name);
91                 target->t->u.user.revision = target->revision;
92
93                 if (target->init != NULL)
94                         target->init(target->t);
95         } else {
96                 target->t = t;
97         }
98         return 0;
99
100 }
101
102 inline void set_lib_dir(void)
103 {
104
105         lib_dir = getenv("XTABLES_LIBDIR");
106         if (!lib_dir) {
107                 lib_dir = getenv("IPTABLES_LIB_DIR");
108                 if (lib_dir)
109                         fprintf(stderr, "using deprecated IPTABLES_LIB_DIR \n");
110         }
111         if (lib_dir == NULL)
112                 lib_dir = XT_LIB_DIR;
113
114 }
115
116 static int parse_ipt(struct action_util *a,int *argc_p,
117                      char ***argv_p, int tca_id, struct nlmsghdr *n)
118 {
119         struct xtables_target *m = NULL;
120         struct ipt_entry fw;
121         struct rtattr *tail;
122         int c;
123         int rargc = *argc_p;
124         char **argv = *argv_p;
125         int argc = 0, iargc = 0;
126         char k[16];
127         int size = 0;
128         int iok = 0, ok = 0;
129         __u32 hook = 0, index = 0;
130
131         xtables_init_all(&tcipt_globals, NFPROTO_IPV4);
132         set_lib_dir();
133
134         {
135                 int i;
136                 for (i = 0; i < rargc; i++) {
137                         if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) {
138                                 break;
139                         }
140                 }
141                 iargc = argc = i;
142         }
143
144         if (argc <= 2) {
145                 fprintf(stderr,"bad arguements to ipt %d vs %d \n", argc, rargc);
146                 return -1;
147         }
148
149         while (1) {
150                 c = getopt_long(argc, argv, "j:", tcipt_globals.opts, NULL);
151                 if (c == -1)
152                         break;
153                 switch (c) {
154                 case 'j':
155                         m = xtables_find_target(optarg, XTF_TRY_LOAD);
156                         if (NULL != m) {
157
158                                 if (0 > build_st(m, NULL)) {
159                                         printf(" %s error \n", m->name);
160                                         return -1;
161                                 }
162                                 tcipt_globals.opts =
163                                     xtables_merge_options(
164 #if (XTABLES_VERSION_CODE >= 6)
165                                         tcipt_globals.orig_opts,
166 #endif
167                                         tcipt_globals.opts,
168                                         m->extra_opts,
169                                         &m->option_offset);
170                         } else {
171                                 fprintf(stderr," failed to find target %s\n\n", optarg);
172                                 return -1;
173                         }
174                         ok++;
175                         break;
176
177                 default:
178                         memset(&fw, 0, sizeof (fw));
179                         if (m) {
180                                 m->parse(c - m->option_offset, argv, 0,
181                                          &m->tflags, NULL, &m->t);
182                         } else {
183                                 fprintf(stderr," failed to find target %s\n\n", optarg);
184                                 return -1;
185
186                         }
187                         ok++;
188                         break;
189
190                 }
191         }
192
193         if (iargc > optind) {
194                 if (matches(argv[optind], "index") == 0) {
195                         if (get_u32(&index, argv[optind + 1], 10)) {
196                                 fprintf(stderr, "Illegal \"index\"\n");
197                                 xtables_free_opts(1);
198                                 return -1;
199                         }
200                         iok++;
201
202                         optind += 2;
203                 }
204         }
205
206         if (!ok && !iok) {
207                 fprintf(stderr," ipt Parser BAD!! (%s)\n", *argv);
208                 return -1;
209         }
210
211         /* check that we passed the correct parameters to the target */
212         if (m && m->final_check)
213                 m->final_check(m->tflags);
214
215         {
216                 struct tcmsg *t = NLMSG_DATA(n);
217                 if (t->tcm_parent != TC_H_ROOT
218                     && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) {
219                         hook = NF_IP_PRE_ROUTING;
220                 } else {
221                         hook = NF_IP_POST_ROUTING;
222                 }
223         }
224
225         tail = NLMSG_TAIL(n);
226         addattr_l(n, MAX_MSG, tca_id, NULL, 0);
227         fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]);
228         fprintf(stdout, "\ttarget: ");
229
230         if (m)
231                 m->print(NULL, m->t, 0);
232         fprintf(stdout, " index %d\n", index);
233
234         if (strlen(tname) > 16) {
235                 size = 16;
236                 k[15] = 0;
237         } else {
238                 size = 1 + strlen(tname);
239         }
240         strncpy(k, tname, size);
241
242         addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size);
243         addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4);
244         addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4);
245         if (m)
246                 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size);
247         tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
248
249         argc -= optind;
250         argv += optind;
251         *argc_p = rargc - iargc;
252         *argv_p = argv;
253
254         optind = 0;
255         xtables_free_opts(1);
256         /* Clear flags if target will be used again */
257         m->tflags=0;
258         m->used=0;
259         /* Free allocated memory */
260         if (m->t)
261             free(m->t);
262
263
264         return 0;
265
266 }
267
268 static int
269 print_ipt(struct action_util *au,FILE * f, struct rtattr *arg)
270 {
271         struct rtattr *tb[TCA_IPT_MAX + 1];
272         struct xt_entry_target *t = NULL;
273
274         if (arg == NULL)
275                 return -1;
276
277         xtables_init_all(&tcipt_globals, NFPROTO_IPV4);
278         set_lib_dir();
279
280         parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
281
282         if (tb[TCA_IPT_TABLE] == NULL) {
283                 fprintf(f, "[NULL ipt table name ] assuming mangle ");
284         } else {
285                 fprintf(f, "tablename: %s ",
286                         (char *) RTA_DATA(tb[TCA_IPT_TABLE]));
287         }
288
289         if (tb[TCA_IPT_HOOK] == NULL) {
290                 fprintf(f, "[NULL ipt hook name ]\n ");
291                 return -1;
292         } else {
293                 __u32 hook;
294                 hook = *(__u32 *) RTA_DATA(tb[TCA_IPT_HOOK]);
295                 fprintf(f, " hook: %s \n", ipthooks[hook]);
296         }
297
298         if (tb[TCA_IPT_TARG] == NULL) {
299                 fprintf(f, "\t[NULL ipt target parameters ] \n");
300                 return -1;
301         } else {
302                 struct xtables_target *m = NULL;
303                 t = RTA_DATA(tb[TCA_IPT_TARG]);
304                 m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
305                 if (NULL != m) {
306                         if (0 > build_st(m, t)) {
307                                 fprintf(stderr, " %s error \n", m->name);
308                                 return -1;
309                         }
310
311                         tcipt_globals.opts =
312                             xtables_merge_options(
313 #if (XTABLES_VERSION_CODE >= 6)
314                                                   tcipt_globals.orig_opts,
315 #endif
316                                                   tcipt_globals.opts,
317                                                   m->extra_opts,
318                                                   &m->option_offset);
319                 } else {
320                         fprintf(stderr, " failed to find target %s\n\n",
321                                 t->u.user.name);
322                         return -1;
323                 }
324                 fprintf(f, "\ttarget ");
325                 m->print(NULL, m->t, 0);
326                 if (tb[TCA_IPT_INDEX] == NULL) {
327                         fprintf(f, " [NULL ipt target index ]\n");
328                 } else {
329                         __u32 index;
330                         index = *(__u32 *) RTA_DATA(tb[TCA_IPT_INDEX]);
331                         fprintf(f, " \n\tindex %d", index);
332                 }
333
334                 if (tb[TCA_IPT_CNT]) {
335                         struct tc_cnt *c  = RTA_DATA(tb[TCA_IPT_CNT]);;
336                         fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt);
337                 }
338                 if (show_stats) {
339                         if (tb[TCA_IPT_TM]) {
340                                 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]);
341                                 print_tm(f,tm);
342                         }
343                 }
344                 fprintf(f, " \n");
345
346         }
347         xtables_free_opts(1);
348
349         return 0;
350 }
351
352 struct action_util ipt_action_util = {
353         .id = "ipt",
354         .parse_aopt = parse_ipt,
355         .print_aopt = print_ipt,
356 };
357