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