2 * m_ipt.c iptables based targets
3 * utilities mostly ripped from iptables <duh, its the linux way>
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.
10 * Authors: J Hadi Salim (hadi@cyberus.ca)
12 * TODO: bad bad hardcoding IPT_LIB_DIR and PROC_SYS_MODPROBE
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
21 #include <linux/netfilter_ipv4/ip_tables.h>
24 #include <linux/tc_act/tc_ipt.h>
39 const char *pname
= "tc-ipt";
40 const char *tname
= "mangle";
41 const char *pversion
= "0.1";
51 #define IPT_LIB_DIR "/usr/local/lib/iptables"
54 #ifndef PROC_SYS_MODPROBE
55 #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
58 static const char *ipthooks
[] = {
66 static struct option original_opts
[] = {
71 static struct iptables_target
*t_list
= NULL
;
72 static struct option
*opts
= original_opts
;
73 static unsigned int global_option_offset
= 0;
74 #define OPTION_OFFSET 256
78 register_target(struct iptables_target
*me
)
80 /* fprintf(stderr, "\nDummy register_target %s \n", me->name);
88 exit_tryhelp(int status
)
90 fprintf(stderr
, "Try `%s -h' or '%s --help' for more information.\n",
96 exit_error(enum exittype status
, char *msg
, ...)
101 fprintf(stderr
, "%s v%s: ", pname
, pversion
);
102 vfprintf(stderr
, msg
, args
);
104 fprintf(stderr
, "\n");
105 if (status
== PARAMETER_PROBLEM
)
106 exit_tryhelp(status
);
107 if (status
== VERSION_PROBLEM
)
109 "Perhaps iptables or your kernel needs to be upgraded.\n");
113 /* stolen from iptables 1.2.11
114 They should really have them as a library so i can link to them
115 Email them next time i remember
119 addr_to_dotted(const struct in_addr
*addrp
)
122 const unsigned char *bytep
;
124 bytep
= (const unsigned char *) &(addrp
->s_addr
);
125 sprintf(buf
, "%d.%d.%d.%d", bytep
[0], bytep
[1], bytep
[2], bytep
[3]);
129 int string_to_number_ll(const char *s
, unsigned long long min
,
130 unsigned long long max
,
131 unsigned long long *ret
)
133 unsigned long long number
;
136 /* Handle hex, octal, etc. */
138 number
= strtoull(s
, &end
, 0);
139 if (*end
== '\0' && end
!= s
) {
140 /* we parsed a number, let's see if we want this */
141 if (errno
!= ERANGE
&& min
<= number
&& (!max
|| number
<= max
)) {
149 int string_to_number_l(const char *s
, unsigned long min
, unsigned long max
,
153 unsigned long long number
;
155 result
= string_to_number_ll(s
, min
, max
, &number
);
156 *ret
= (unsigned long)number
;
161 int string_to_number(const char *s
, unsigned int min
, unsigned int max
,
165 unsigned long number
;
167 result
= string_to_number_l(s
, min
, max
, &number
);
168 *ret
= (unsigned int)number
;
173 static void free_opts(struct option
*local_opts
)
175 if (local_opts
!= original_opts
) {
177 opts
= original_opts
;
178 global_option_offset
= 0;
182 static struct option
*
183 merge_options(struct option
*oldopts
, const struct option
*newopts
,
184 unsigned int *option_offset
)
186 struct option
*merge
;
187 unsigned int num_old
, num_new
, i
;
189 for (num_old
= 0; oldopts
[num_old
].name
; num_old
++) ;
190 for (num_new
= 0; newopts
[num_new
].name
; num_new
++) ;
192 *option_offset
= global_option_offset
+ OPTION_OFFSET
;
194 merge
= malloc(sizeof (struct option
) * (num_new
+ num_old
+ 1));
195 memcpy(merge
, oldopts
, num_old
* sizeof (struct option
));
196 for (i
= 0; i
< num_new
; i
++) {
197 merge
[num_old
+ i
] = newopts
[i
];
198 merge
[num_old
+ i
].val
+= *option_offset
;
200 memset(merge
+ num_old
+ num_new
, 0, sizeof (struct option
));
206 fw_calloc(size_t count
, size_t size
)
210 if ((p
= (void *) calloc(count
, size
)) == NULL
) {
211 perror("iptables: calloc failed");
217 static struct iptables_target
*
220 struct iptables_target
*m
;
221 for (m
= t_list
; m
; m
= m
->next
) {
222 if (strcmp(m
->name
, name
) == 0)
229 static struct iptables_target
*
230 get_target_name(char *name
)
234 char *new_name
, *lname
;
235 struct iptables_target
*m
;
237 char path
[sizeof (IPT_LIB_DIR
) + sizeof ("/libipt_.so") + strlen(name
)];
239 new_name
= malloc(strlen(name
) + 1);
240 lname
= malloc(strlen(name
) + 1);
242 memset(new_name
, '\0', strlen(name
) + 1);
244 exit_error(PARAMETER_PROBLEM
, "get_target_name");
247 memset(lname
, '\0', strlen(name
) + 1);
249 exit_error(PARAMETER_PROBLEM
, "get_target_name");
251 strcpy(new_name
, name
);
254 if (isupper(lname
[0])) {
256 for (i
= 0; i
< strlen(name
); i
++) {
257 lname
[i
] = tolower(lname
[i
]);
261 if (islower(new_name
[0])) {
263 for (i
= 0; i
< strlen(new_name
); i
++) {
264 new_name
[i
] = toupper(new_name
[i
]);
268 sprintf(path
, IPT_LIB_DIR
"/libipt_%s.so", new_name
);
269 handle
= dlopen(path
, RTLD_LAZY
);
271 sprintf(path
, IPT_LIB_DIR
"/libipt_%s.so", lname
);
272 handle
= dlopen(path
, RTLD_LAZY
);
274 fputs(dlerror(), stderr
);
282 m
= dlsym(handle
, new_name
);
283 if ((error
= dlerror()) != NULL
) {
284 m
= (struct iptables_target
*) dlsym(handle
, lname
);
285 if ((error
= dlerror()) != NULL
) {
286 m
= find_t(new_name
);
290 fputs(error
, stderr
);
291 fprintf(stderr
, "\n");
307 struct in_addr
*dotted_to_addr(const char *dotted
)
309 static struct in_addr addr
;
310 unsigned char *addrp
;
312 unsigned int onebyte
;
316 /* copy dotted string, because we need to modify it */
317 strncpy(buf
, dotted
, sizeof (buf
) - 1);
318 addrp
= (unsigned char *) &(addr
.s_addr
);
321 for (i
= 0; i
< 3; i
++) {
322 if ((q
= strchr(p
, '.')) == NULL
)
323 return (struct in_addr
*) NULL
;
326 if (string_to_number(p
, 0, 255, &onebyte
) == -1)
327 return (struct in_addr
*) NULL
;
329 addrp
[i
] = (unsigned char) onebyte
;
333 /* we've checked 3 bytes, now we check the last one */
334 if (string_to_number(p
, 0, 255, &onebyte
) == -1)
335 return (struct in_addr
*) NULL
;
337 addrp
[3] = (unsigned char) onebyte
;
342 static void set_revision(char *name
, u_int8_t revision
)
344 /* Old kernel sources don't have ".revision" field,
345 * but we stole a byte from name. */
346 name
[IPT_FUNCTION_MAXNAMELEN
- 2] = '\0';
347 name
[IPT_FUNCTION_MAXNAMELEN
- 1] = revision
;
351 * we may need to check for version mismatch
354 build_st(struct iptables_target
*target
, struct ipt_entry_target
*t
)
356 unsigned int nfcache
= 0;
362 IPT_ALIGN(sizeof (struct ipt_entry_target
)) + target
->size
;
365 target
->t
= fw_calloc(1, size
);
366 target
->t
->u
.target_size
= size
;
368 if (target
->init
!= NULL
)
369 target
->init(target
->t
, &nfcache
);
370 set_revision(target
->t
->u
.user
.name
, target
->revision
);
374 strcpy(target
->t
->u
.user
.name
, target
->name
);
381 static int parse_ipt(struct action_util
*a
,int *argc_p
,
382 char ***argv_p
, int tca_id
, struct nlmsghdr
*n
)
384 struct iptables_target
*m
= NULL
;
389 char **argv
= *argv_p
;
390 int argc
= 0, iargc
= 0;
395 __u32 hook
= 0, index
= 0;
400 for (i
= 0; i
< rargc
; i
++) {
401 if (NULL
== argv
[i
] || 0 == strcmp(argv
[i
], "action")) {
409 fprintf(stderr
,"bad arguements to ipt %d vs %d \n", argc
, rargc
);
414 c
= getopt_long(argc
, argv
, "j:", opts
, NULL
);
419 m
= get_target_name(optarg
);
422 if (0 > build_st(m
, NULL
)) {
423 printf(" %s error \n", m
->name
);
427 merge_options(opts
, m
->extra_opts
,
430 fprintf(stderr
," failed to find target %s\n\n", optarg
);
437 memset(&fw
, 0, sizeof (fw
));
439 m
->parse(c
- m
->option_offset
, argv
, 0,
440 &m
->tflags
, NULL
, &m
->t
);
442 fprintf(stderr
," failed to find target %s\n\n", optarg
);
452 if (iargc
> optind
) {
453 if (matches(argv
[optind
], "index") == 0) {
454 if (get_u32(&index
, argv
[optind
+ 1], 10)) {
455 fprintf(stderr
, "Illegal \"index\"\n");
466 fprintf(stderr
," ipt Parser BAD!! (%s)\n", *argv
);
470 /* check that we passed the correct parameters to the target */
472 m
->final_check(m
->tflags
);
475 struct tcmsg
*t
= NLMSG_DATA(n
);
476 if (t
->tcm_parent
!= TC_H_ROOT
477 && t
->tcm_parent
== TC_H_MAJ(TC_H_INGRESS
)) {
478 hook
= NF_IP_PRE_ROUTING
;
480 hook
= NF_IP_POST_ROUTING
;
484 tail
= NLMSG_TAIL(n
);
485 addattr_l(n
, MAX_MSG
, tca_id
, NULL
, 0);
486 fprintf(stdout
, "tablename: %s hook: %s\n ", tname
, ipthooks
[hook
]);
487 fprintf(stdout
, "\ttarget: ");
490 m
->print(NULL
, m
->t
, 0);
491 fprintf(stdout
, " index %d\n", index
);
493 if (strlen(tname
) > 16) {
497 size
= 1 + strlen(tname
);
499 strncpy(k
, tname
, size
);
501 addattr_l(n
, MAX_MSG
, TCA_IPT_TABLE
, k
, size
);
502 addattr_l(n
, MAX_MSG
, TCA_IPT_HOOK
, &hook
, 4);
503 addattr_l(n
, MAX_MSG
, TCA_IPT_INDEX
, &index
, 4);
505 addattr_l(n
, MAX_MSG
, TCA_IPT_TARG
, m
->t
, m
->t
->u
.target_size
);
506 tail
->rta_len
= (void *) NLMSG_TAIL(n
) - (void *) tail
;
510 *argc_p
= rargc
- iargc
;
515 /* Clear flags if target will be used again */
518 /* Free allocated memory */
528 print_ipt(struct action_util
*au
,FILE * f
, struct rtattr
*arg
)
530 struct rtattr
*tb
[TCA_IPT_MAX
+ 1];
531 struct ipt_entry_target
*t
= NULL
;
536 parse_rtattr_nested(tb
, TCA_IPT_MAX
, arg
);
538 if (tb
[TCA_IPT_TABLE
] == NULL
) {
539 fprintf(f
, "[NULL ipt table name ] assuming mangle ");
541 fprintf(f
, "tablename: %s ",
542 (char *) RTA_DATA(tb
[TCA_IPT_TABLE
]));
545 if (tb
[TCA_IPT_HOOK
] == NULL
) {
546 fprintf(f
, "[NULL ipt hook name ]\n ");
550 hook
= *(__u32
*) RTA_DATA(tb
[TCA_IPT_HOOK
]);
551 fprintf(f
, " hook: %s \n", ipthooks
[hook
]);
554 if (tb
[TCA_IPT_TARG
] == NULL
) {
555 fprintf(f
, "\t[NULL ipt target parameters ] \n");
558 struct iptables_target
*m
= NULL
;
559 t
= RTA_DATA(tb
[TCA_IPT_TARG
]);
560 m
= get_target_name(t
->u
.user
.name
);
562 if (0 > build_st(m
, t
)) {
563 fprintf(stderr
, " %s error \n", m
->name
);
568 merge_options(opts
, m
->extra_opts
,
571 fprintf(stderr
, " failed to find target %s\n\n",
575 fprintf(f
, "\ttarget ");
576 m
->print(NULL
, m
->t
, 0);
577 if (tb
[TCA_IPT_INDEX
] == NULL
) {
578 fprintf(f
, " [NULL ipt target index ]\n");
581 index
= *(__u32
*) RTA_DATA(tb
[TCA_IPT_INDEX
]);
582 fprintf(f
, " \n\tindex %d", index
);
585 if (tb
[TCA_IPT_CNT
]) {
586 struct tc_cnt
*c
= RTA_DATA(tb
[TCA_IPT_CNT
]);;
587 fprintf(f
, " ref %d bind %d", c
->refcnt
, c
->bindcnt
);
590 if (tb
[TCA_IPT_TM
]) {
591 struct tcf_t
*tm
= RTA_DATA(tb
[TCA_IPT_TM
]);
603 struct action_util ipt_action_util
= {
605 .parse_aopt
= parse_ipt
,
606 .print_aopt
= print_ipt
,