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