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 #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 xtables_set_revision(target->t->u.user.name, 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 res = -1; 128 int size = 0; 129 int iok = 0, ok = 0; 130 __u32 hook = 0, index = 0; 131 res = 0; 132 133 xtables_init_all(&tcipt_globals, NFPROTO_IPV4); 134 set_lib_dir(); 135 136 { 137 int i; 138 for (i = 0; i < rargc; i++) { 139 if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) { 140 break; 141 } 142 } 143 iargc = argc = i; 144 } 145 146 if (argc <= 2) { 147 fprintf(stderr,"bad arguements to ipt %d vs %d \n", argc, rargc); 148 return -1; 149 } 150 151 while (1) { 152 c = getopt_long(argc, argv, "j:", tcipt_globals.opts, NULL); 153 if (c == -1) 154 break; 155 switch (c) { 156 case 'j': 157 m = xtables_find_target(optarg, XTF_TRY_LOAD); 158 if (NULL != m) { 159 160 if (0 > build_st(m, NULL)) { 161 printf(" %s error \n", m->name); 162 return -1; 163 } 164 tcipt_globals.opts = 165 xtables_merge_options(tcipt_globals.opts, 166 m->extra_opts, 167 &m->option_offset); 168 } else { 169 fprintf(stderr," failed to find target %s\n\n", optarg); 170 return -1; 171 } 172 ok++; 173 break; 174 175 default: 176 memset(&fw, 0, sizeof (fw)); 177 if (m) { 178 m->parse(c - m->option_offset, argv, 0, 179 &m->tflags, NULL, &m->t); 180 } else { 181 fprintf(stderr," failed to find target %s\n\n", optarg); 182 return -1; 183 184 } 185 ok++; 186 break; 187 188 } 189 } 190 191 if (iargc > optind) { 192 if (matches(argv[optind], "index") == 0) { 193 if (get_u32(&index, argv[optind + 1], 10)) { 194 fprintf(stderr, "Illegal \"index\"\n"); 195 xtables_free_opts(1); 196 return -1; 197 } 198 iok++; 199 200 optind += 2; 201 } 202 } 203 204 if (!ok && !iok) { 205 fprintf(stderr," ipt Parser BAD!! (%s)\n", *argv); 206 return -1; 207 } 208 209 /* check that we passed the correct parameters to the target */ 210 if (m && m->final_check) 211 m->final_check(m->tflags); 212 213 { 214 struct tcmsg *t = NLMSG_DATA(n); 215 if (t->tcm_parent != TC_H_ROOT 216 && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) { 217 hook = NF_IP_PRE_ROUTING; 218 } else { 219 hook = NF_IP_POST_ROUTING; 220 } 221 } 222 223 tail = NLMSG_TAIL(n); 224 addattr_l(n, MAX_MSG, tca_id, NULL, 0); 225 fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]); 226 fprintf(stdout, "\ttarget: "); 227 228 if (m) 229 m->print(NULL, m->t, 0); 230 fprintf(stdout, " index %d\n", index); 231 232 if (strlen(tname) > 16) { 233 size = 16; 234 k[15] = 0; 235 } else { 236 size = 1 + strlen(tname); 237 } 238 strncpy(k, tname, size); 239 240 addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size); 241 addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4); 242 addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4); 243 if (m) 244 addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size); 245 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; 246 247 argc -= optind; 248 argv += optind; 249 *argc_p = rargc - iargc; 250 *argv_p = argv; 251 252 optind = 0; 253 xtables_free_opts(1); 254 /* Clear flags if target will be used again */ 255 m->tflags=0; 256 m->used=0; 257 /* Free allocated memory */ 258 if (m->t) 259 free(m->t); 260 261 262 return 0; 263 264 } 265 266 static int 267 print_ipt(struct action_util *au,FILE * f, struct rtattr *arg) 268 { 269 struct rtattr *tb[TCA_IPT_MAX + 1]; 270 struct xt_entry_target *t = NULL; 271 272 if (arg == NULL) 273 return -1; 274 275 xtables_init_all(&tcipt_globals, NFPROTO_IPV4); 276 set_lib_dir(); 277 278 parse_rtattr_nested(tb, TCA_IPT_MAX, arg); 279 280 if (tb[TCA_IPT_TABLE] == NULL) { 281 fprintf(f, "[NULL ipt table name ] assuming mangle "); 282 } else { 283 fprintf(f, "tablename: %s ", 284 (char *) RTA_DATA(tb[TCA_IPT_TABLE])); 285 } 286 287 if (tb[TCA_IPT_HOOK] == NULL) { 288 fprintf(f, "[NULL ipt hook name ]\n "); 289 return -1; 290 } else { 291 __u32 hook; 292 hook = *(__u32 *) RTA_DATA(tb[TCA_IPT_HOOK]); 293 fprintf(f, " hook: %s \n", ipthooks[hook]); 294 } 295 296 if (tb[TCA_IPT_TARG] == NULL) { 297 fprintf(f, "\t[NULL ipt target parameters ] \n"); 298 return -1; 299 } else { 300 struct xtables_target *m = NULL; 301 t = RTA_DATA(tb[TCA_IPT_TARG]); 302 m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD); 303 if (NULL != m) { 304 if (0 > build_st(m, t)) { 305 fprintf(stderr, " %s error \n", m->name); 306 return -1; 307 } 308 309 tcipt_globals.opts = 310 xtables_merge_options(tcipt_globals.opts, 311 m->extra_opts, 312 &m->option_offset); 313 } else { 314 fprintf(stderr, " failed to find target %s\n\n", 315 t->u.user.name); 316 return -1; 317 } 318 fprintf(f, "\ttarget "); 319 m->print(NULL, m->t, 0); 320 if (tb[TCA_IPT_INDEX] == NULL) { 321 fprintf(f, " [NULL ipt target index ]\n"); 322 } else { 323 __u32 index; 324 index = *(__u32 *) RTA_DATA(tb[TCA_IPT_INDEX]); 325 fprintf(f, " \n\tindex %d", index); 326 } 327 328 if (tb[TCA_IPT_CNT]) { 329 struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);; 330 fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt); 331 } 332 if (show_stats) { 333 if (tb[TCA_IPT_TM]) { 334 struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]); 335 print_tm(f,tm); 336 } 337 } 338 fprintf(f, " \n"); 339 340 } 341 xtables_free_opts(1); 342 343 return 0; 344 } 345 346 struct action_util ipt_action_util = { 347 .id = "ipt", 348 .parse_aopt = parse_ipt, 349 .print_aopt = print_ipt, 350 }; 351 352