]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blobdiff - tc/m_ipt.c
Convert to use rta_getattr_ functions
[lisovros/iproute2_canprio.git] / tc / m_ipt.c
index e1f68b33f48cafb7c79f92fd8f7a33b58a864422..dc2dedc0dd9ce7b481b462b797f29f1edf36798d 100644 (file)
@@ -8,16 +8,15 @@
  *             2 of the License, or (at your option) any later version.
  *
  * Authors:  J Hadi Salim (hadi@cyberus.ca)
- *
- * TODO: bad bad hardcoding IPT_LIB_DIR and PROC_SYS_MODPROBE
- *
-*/
+ */
 
 #include <syslog.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <linux/if.h>
 #include <iptables.h>
+#include <linux/netfilter.h>
 #include <linux/netfilter_ipv4/ip_tables.h>
 #include "utils.h"
 #include "tc_util.h"
 #include <stdlib.h>
 #include <ctype.h>
 #include <stdarg.h>
-#include <limits.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/wait.h>
 
-const char *pname = "tc-ipt";
-const char *tname = "mangle";
-const char *pversion = "0.1";
-
-#ifndef TRUE
-#define TRUE 1
-#endif
-#ifndef FALSE
-#define FALSE 0
-#endif
-
-#ifndef IPT_LIB_DIR
-#define IPT_LIB_DIR "/usr/local/lib/iptables"
-#endif
-
-#ifndef PROC_SYS_MODPROBE
-#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
-#endif
+static const char *pname = "tc-ipt";
+static const char *tname = "mangle";
+static const char *pversion = "0.1";
 
 static const char *ipthooks[] = {
        "NF_IP_PRE_ROUTING",
@@ -73,6 +56,7 @@ static struct option *opts = original_opts;
 static unsigned int global_option_offset = 0;
 #define OPTION_OFFSET 256
 
+char *lib_dir;
 
 void
 register_target(struct iptables_target *me)
@@ -84,6 +68,13 @@ register_target(struct iptables_target *me)
 
 }
 
+void
+xtables_register_target(struct iptables_target *me)
+{
+       me->next = t_list;
+       t_list = me;
+}
+
 void
 exit_tryhelp(int status)
 {
@@ -170,10 +161,10 @@ int string_to_number(const char *s, unsigned int min, unsigned int max,
        return result;
 }
 
-static void free_opts(struct option *opts)
+static void free_opts(struct option *local_opts)
 {
-       if (opts != original_opts) {
-               free(opts);
+       if (local_opts != original_opts) {
+               free(local_opts);
                opts = original_opts;
                global_option_offset = 0;
        }
@@ -227,14 +218,17 @@ find_t(char *name)
 }
 
 static struct iptables_target *
-get_target_name(char *name)
+get_target_name(const char *name)
 {
        void *handle;
        char *error;
        char *new_name, *lname;
        struct iptables_target *m;
+       char path[strlen(lib_dir) + sizeof ("/libipt_.so") + strlen(name)];
 
-       char path[sizeof (IPT_LIB_DIR) + sizeof ("/libipt_.so") + strlen(name)];
+#ifdef NO_SHARED_LIBS
+       return NULL;
+#endif
 
        new_name = malloc(strlen(name) + 1);
        lname = malloc(strlen(name) + 1);
@@ -265,14 +259,29 @@ get_target_name(char *name)
                }
        }
 
-       sprintf(path, IPT_LIB_DIR "/libipt_%s.so", new_name);
+       /* try libxt_xx first */
+       sprintf(path, "%s/libxt_%s.so", lib_dir, new_name);
        handle = dlopen(path, RTLD_LAZY);
        if (!handle) {
-               sprintf(path, IPT_LIB_DIR "/libipt_%s.so", lname);
+               /* try libipt_xx next */
+               sprintf(path, "%s/libipt_%s.so", lib_dir, new_name);
                handle = dlopen(path, RTLD_LAZY);
+
+               if (!handle) {
+                       sprintf(path, "%s/libxt_%s.so", lib_dir , lname);
+                       handle = dlopen(path, RTLD_LAZY);
+               }
+
+               if (!handle) {
+                       sprintf(path, "%s/libipt_%s.so", lib_dir , lname);
+                       handle = dlopen(path, RTLD_LAZY);
+               }
+               /* ok, lets give up .. */
                if (!handle) {
                        fputs(dlerror(), stderr);
                        printf("\n");
+                       free(new_name);
+                       free(lname);
                        return NULL;
                }
        }
@@ -288,12 +297,16 @@ get_target_name(char *name)
                                        fputs(error, stderr);
                                        fprintf(stderr, "\n");
                                        dlclose(handle);
+                                       free(new_name);
+                                       free(lname);
                                        return NULL;
                                }
                        }
                }
        }
 
+       free(new_name);
+       free(lname);
        return m;
 }
 
@@ -383,11 +396,13 @@ static int parse_ipt(struct action_util *a,int *argc_p,
        char **argv = *argv_p;
        int argc = 0, iargc = 0;
        char k[16];
-       int res = -1;
        int size = 0;
        int iok = 0, ok = 0;
        __u32 hook = 0, index = 0;
-       res = 0;
+
+       lib_dir = getenv("IPTABLES_LIB_DIR");
+       if (!lib_dir)
+               lib_dir = IPT_LIB_DIR;
 
        {
                int i;
@@ -504,8 +519,15 @@ static int parse_ipt(struct action_util *a,int *argc_p,
        *argc_p = rargc - iargc;
        *argv_p = argv;
 
-       optind = 1;
+       optind = 0;
        free_opts(opts);
+       /* Clear flags if target will be used again */
+        m->tflags=0;
+        m->used=0;
+       /* Free allocated memory */
+        if (m->t)
+            free(m->t);
+
 
        return 0;
 
@@ -520,13 +542,17 @@ print_ipt(struct action_util *au,FILE * f, struct rtattr *arg)
        if (arg == NULL)
                return -1;
 
+       lib_dir = getenv("IPTABLES_LIB_DIR");
+       if (!lib_dir)
+               lib_dir = IPT_LIB_DIR;
+
        parse_rtattr_nested(tb, TCA_IPT_MAX, arg);
 
        if (tb[TCA_IPT_TABLE] == NULL) {
                fprintf(f, "[NULL ipt table name ] assuming mangle ");
        } else {
                fprintf(f, "tablename: %s ",
-                       (char *) RTA_DATA(tb[TCA_IPT_TABLE]));
+                       rta_getattr_str(tb[TCA_IPT_TABLE]));
        }
 
        if (tb[TCA_IPT_HOOK] == NULL) {
@@ -534,7 +560,7 @@ print_ipt(struct action_util *au,FILE * f, struct rtattr *arg)
                return -1;
        } else {
                __u32 hook;
-               hook = *(__u32 *) RTA_DATA(tb[TCA_IPT_HOOK]);
+               hook = rta_getattr_u32(tb[TCA_IPT_HOOK]);
                fprintf(f, " hook: %s \n", ipthooks[hook]);
        }
 
@@ -565,7 +591,7 @@ print_ipt(struct action_util *au,FILE * f, struct rtattr *arg)
                        fprintf(f, " [NULL ipt target index ]\n");
                } else {
                        __u32 index;
-                       index = *(__u32 *) RTA_DATA(tb[TCA_IPT_INDEX]);
+                       index = rta_getattr_u32(tb[TCA_IPT_INDEX]);
                        fprintf(f, " \n\tindex %d", index);
                }