libip4tc.c revision 1639fe86579f86f5f6a954a9b0adde2e16ad1980
1/* Library which manipulates firewall rules. Version 0.1. */ 2 3/* Architecture of firewall rules is as follows: 4 * 5 * Chains go INPUT, FORWARD, OUTPUT then user chains. 6 * Each user chain starts with an ERROR node. 7 * Every chain ends with an unconditional jump: a RETURN for user chains, 8 * and a POLICY for built-ins. 9 */ 10 11/* (C)1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See 12 COPYING for details). */ 13 14#include <assert.h> 15#include <string.h> 16#include <errno.h> 17#include <stdlib.h> 18#include <stdio.h> 19#include <unistd.h> 20 21#ifdef DEBUG_CONNTRACK 22#define inline 23#endif 24 25#if !defined(__GLIBC__) || (__GLIBC__ < 2) 26typedef unsigned int socklen_t; 27#endif 28 29#include "libiptc/libiptc.h" 30 31#define IP_VERSION 4 32#define IP_OFFSET 0x1FFF 33 34#define HOOK_PRE_ROUTING NF_IP_PRE_ROUTING 35#define HOOK_LOCAL_IN NF_IP_LOCAL_IN 36#define HOOK_FORWARD NF_IP_FORWARD 37#define HOOK_LOCAL_OUT NF_IP_LOCAL_OUT 38#define HOOK_POST_ROUTING NF_IP_POST_ROUTING 39 40#define STRUCT_ENTRY_TARGET struct ipt_entry_target 41#define STRUCT_ENTRY struct ipt_entry 42#define STRUCT_ENTRY_MATCH struct ipt_entry_match 43#define STRUCT_GETINFO struct ipt_getinfo 44#define STRUCT_GET_ENTRIES struct ipt_get_entries 45#define STRUCT_COUNTERS struct ipt_counters 46#define STRUCT_COUNTERS_INFO struct ipt_counters_info 47#define STRUCT_STANDARD_TARGET struct ipt_standard_target 48#define STRUCT_REPLACE struct ipt_replace 49 50#define ENTRY_ITERATE IPT_ENTRY_ITERATE 51#define TABLE_MAXNAMELEN IPT_TABLE_MAXNAMELEN 52#define FUNCTION_MAXNAMELEN IPT_FUNCTION_MAXNAMELEN 53 54#define GET_TARGET ipt_get_target 55 56#define ERROR_TARGET IPT_ERROR_TARGET 57#define NUMHOOKS NF_IP_NUMHOOKS 58 59#define IPT_CHAINLABEL xt_chainlabel 60 61#define TC_DUMP_ENTRIES dump_entries 62#define TC_IS_CHAIN iptc_is_chain 63#define TC_FIRST_CHAIN iptc_first_chain 64#define TC_NEXT_CHAIN iptc_next_chain 65#define TC_FIRST_RULE iptc_first_rule 66#define TC_NEXT_RULE iptc_next_rule 67#define TC_GET_TARGET iptc_get_target 68#define TC_BUILTIN iptc_builtin 69#define TC_GET_POLICY iptc_get_policy 70#define TC_INSERT_ENTRY iptc_insert_entry 71#define TC_REPLACE_ENTRY iptc_replace_entry 72#define TC_APPEND_ENTRY iptc_append_entry 73#define TC_CHECK_ENTRY iptc_check_entry 74#define TC_DELETE_ENTRY iptc_delete_entry 75#define TC_DELETE_NUM_ENTRY iptc_delete_num_entry 76#define TC_FLUSH_ENTRIES iptc_flush_entries 77#define TC_ZERO_ENTRIES iptc_zero_entries 78#define TC_READ_COUNTER iptc_read_counter 79#define TC_ZERO_COUNTER iptc_zero_counter 80#define TC_SET_COUNTER iptc_set_counter 81#define TC_CREATE_CHAIN iptc_create_chain 82#define TC_GET_REFERENCES iptc_get_references 83#define TC_DELETE_CHAIN iptc_delete_chain 84#define TC_RENAME_CHAIN iptc_rename_chain 85#define TC_SET_POLICY iptc_set_policy 86#define TC_GET_RAW_SOCKET iptc_get_raw_socket 87#define TC_INIT iptc_init 88#define TC_FREE iptc_free 89#define TC_COMMIT iptc_commit 90#define TC_STRERROR iptc_strerror 91#define TC_NUM_RULES iptc_num_rules 92#define TC_GET_RULE iptc_get_rule 93 94#define TC_AF AF_INET 95#define TC_IPPROTO IPPROTO_IP 96 97#define SO_SET_REPLACE IPT_SO_SET_REPLACE 98#define SO_SET_ADD_COUNTERS IPT_SO_SET_ADD_COUNTERS 99#define SO_GET_INFO IPT_SO_GET_INFO 100#define SO_GET_ENTRIES IPT_SO_GET_ENTRIES 101#define SO_GET_VERSION IPT_SO_GET_VERSION 102 103#define STANDARD_TARGET IPT_STANDARD_TARGET 104#define LABEL_RETURN IPTC_LABEL_RETURN 105#define LABEL_ACCEPT IPTC_LABEL_ACCEPT 106#define LABEL_DROP IPTC_LABEL_DROP 107#define LABEL_QUEUE IPTC_LABEL_QUEUE 108 109#define ALIGN XT_ALIGN 110#define RETURN IPT_RETURN 111 112#include "libiptc.c" 113 114#define IP_PARTS_NATIVE(n) \ 115(unsigned int)((n)>>24)&0xFF, \ 116(unsigned int)((n)>>16)&0xFF, \ 117(unsigned int)((n)>>8)&0xFF, \ 118(unsigned int)((n)&0xFF) 119 120#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n)) 121 122static int 123dump_entry(struct ipt_entry *e, struct xtc_handle *const handle) 124{ 125 size_t i; 126 STRUCT_ENTRY_TARGET *t; 127 128 printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e), 129 iptcb_entry2offset(handle, e)); 130 printf("SRC IP: %u.%u.%u.%u/%u.%u.%u.%u\n", 131 IP_PARTS(e->ip.src.s_addr),IP_PARTS(e->ip.smsk.s_addr)); 132 printf("DST IP: %u.%u.%u.%u/%u.%u.%u.%u\n", 133 IP_PARTS(e->ip.dst.s_addr),IP_PARTS(e->ip.dmsk.s_addr)); 134 printf("Interface: `%s'/", e->ip.iniface); 135 for (i = 0; i < IFNAMSIZ; i++) 136 printf("%c", e->ip.iniface_mask[i] ? 'X' : '.'); 137 printf("to `%s'/", e->ip.outiface); 138 for (i = 0; i < IFNAMSIZ; i++) 139 printf("%c", e->ip.outiface_mask[i] ? 'X' : '.'); 140 printf("\nProtocol: %u\n", e->ip.proto); 141 printf("Flags: %02X\n", e->ip.flags); 142 printf("Invflags: %02X\n", e->ip.invflags); 143 printf("Counters: %llu packets, %llu bytes\n", 144 (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt); 145 printf("Cache: %08X\n", e->nfcache); 146 147 IPT_MATCH_ITERATE(e, print_match); 148 149 t = GET_TARGET(e); 150 printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size); 151 if (strcmp(t->u.user.name, STANDARD_TARGET) == 0) { 152 const unsigned char *data = t->data; 153 int pos = *(const int *)data; 154 if (pos < 0) 155 printf("verdict=%s\n", 156 pos == -NF_ACCEPT-1 ? "NF_ACCEPT" 157 : pos == -NF_DROP-1 ? "NF_DROP" 158 : pos == -NF_QUEUE-1 ? "NF_QUEUE" 159 : pos == RETURN ? "RETURN" 160 : "UNKNOWN"); 161 else 162 printf("verdict=%u\n", pos); 163 } else if (strcmp(t->u.user.name, IPT_ERROR_TARGET) == 0) 164 printf("error=`%s'\n", t->data); 165 166 printf("\n"); 167 return 0; 168} 169 170static unsigned char * 171is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b, unsigned char *matchmask) 172{ 173 unsigned int i; 174 unsigned char *mptr; 175 176 /* Always compare head structures: ignore mask here. */ 177 if (a->ip.src.s_addr != b->ip.src.s_addr 178 || a->ip.dst.s_addr != b->ip.dst.s_addr 179 || a->ip.smsk.s_addr != b->ip.smsk.s_addr 180 || a->ip.dmsk.s_addr != b->ip.dmsk.s_addr 181 || a->ip.proto != b->ip.proto 182 || a->ip.flags != b->ip.flags 183 || a->ip.invflags != b->ip.invflags) 184 return NULL; 185 186 for (i = 0; i < IFNAMSIZ; i++) { 187 if (a->ip.iniface_mask[i] != b->ip.iniface_mask[i]) 188 return NULL; 189 if ((a->ip.iniface[i] & a->ip.iniface_mask[i]) 190 != (b->ip.iniface[i] & b->ip.iniface_mask[i])) 191 return NULL; 192 if (a->ip.outiface_mask[i] != b->ip.outiface_mask[i]) 193 return NULL; 194 if ((a->ip.outiface[i] & a->ip.outiface_mask[i]) 195 != (b->ip.outiface[i] & b->ip.outiface_mask[i])) 196 return NULL; 197 } 198 199 if (a->target_offset != b->target_offset 200 || a->next_offset != b->next_offset) 201 return NULL; 202 203 mptr = matchmask + sizeof(STRUCT_ENTRY); 204 if (IPT_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr)) 205 return NULL; 206 mptr += XT_ALIGN(sizeof(struct ipt_entry_target)); 207 208 return mptr; 209} 210 211#if 0 212/***************************** DEBUGGING ********************************/ 213static inline int 214unconditional(const struct ipt_ip *ip) 215{ 216 unsigned int i; 217 218 for (i = 0; i < sizeof(*ip)/sizeof(uint32_t); i++) 219 if (((uint32_t *)ip)[i]) 220 return 0; 221 222 return 1; 223} 224 225static inline int 226check_match(const STRUCT_ENTRY_MATCH *m, unsigned int *off) 227{ 228 assert(m->u.match_size >= sizeof(STRUCT_ENTRY_MATCH)); 229 assert(ALIGN(m->u.match_size) == m->u.match_size); 230 231 (*off) += m->u.match_size; 232 return 0; 233} 234 235static inline int 236check_entry(const STRUCT_ENTRY *e, unsigned int *i, unsigned int *off, 237 unsigned int user_offset, int *was_return, 238 struct xtc_handle *h) 239{ 240 unsigned int toff; 241 STRUCT_STANDARD_TARGET *t; 242 243 assert(e->target_offset >= sizeof(STRUCT_ENTRY)); 244 assert(e->next_offset >= e->target_offset 245 + sizeof(STRUCT_ENTRY_TARGET)); 246 toff = sizeof(STRUCT_ENTRY); 247 IPT_MATCH_ITERATE(e, check_match, &toff); 248 249 assert(toff == e->target_offset); 250 251 t = (STRUCT_STANDARD_TARGET *) 252 GET_TARGET((STRUCT_ENTRY *)e); 253 /* next_offset will have to be multiple of entry alignment. */ 254 assert(e->next_offset == ALIGN(e->next_offset)); 255 assert(e->target_offset == ALIGN(e->target_offset)); 256 assert(t->target.u.target_size == ALIGN(t->target.u.target_size)); 257 assert(!TC_IS_CHAIN(t->target.u.user.name, h)); 258 259 if (strcmp(t->target.u.user.name, STANDARD_TARGET) == 0) { 260 assert(t->target.u.target_size 261 == ALIGN(sizeof(STRUCT_STANDARD_TARGET))); 262 263 assert(t->verdict == -NF_DROP-1 264 || t->verdict == -NF_ACCEPT-1 265 || t->verdict == RETURN 266 || t->verdict < (int)h->entries->size); 267 268 if (t->verdict >= 0) { 269 STRUCT_ENTRY *te = get_entry(h, t->verdict); 270 int idx; 271 272 idx = iptcb_entry2index(h, te); 273 assert(strcmp(GET_TARGET(te)->u.user.name, 274 IPT_ERROR_TARGET) 275 != 0); 276 assert(te != e); 277 278 /* Prior node must be error node, or this node. */ 279 assert(t->verdict == iptcb_entry2offset(h, e)+e->next_offset 280 || strcmp(GET_TARGET(index2entry(h, idx-1)) 281 ->u.user.name, IPT_ERROR_TARGET) 282 == 0); 283 } 284 285 if (t->verdict == RETURN 286 && unconditional(&e->ip) 287 && e->target_offset == sizeof(*e)) 288 *was_return = 1; 289 else 290 *was_return = 0; 291 } else if (strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0) { 292 assert(t->target.u.target_size 293 == ALIGN(sizeof(struct ipt_error_target))); 294 295 /* If this is in user area, previous must have been return */ 296 if (*off > user_offset) 297 assert(*was_return); 298 299 *was_return = 0; 300 } 301 else *was_return = 0; 302 303 if (*off == user_offset) 304 assert(strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0); 305 306 (*off) += e->next_offset; 307 (*i)++; 308 return 0; 309} 310 311#ifdef IPTC_DEBUG 312/* Do every conceivable sanity check on the handle */ 313static void 314do_check(struct xtc_handle *h, unsigned int line) 315{ 316 unsigned int i, n; 317 unsigned int user_offset; /* Offset of first user chain */ 318 int was_return; 319 320 assert(h->changed == 0 || h->changed == 1); 321 if (strcmp(h->info.name, "filter") == 0) { 322 assert(h->info.valid_hooks 323 == (1 << NF_IP_LOCAL_IN 324 | 1 << NF_IP_FORWARD 325 | 1 << NF_IP_LOCAL_OUT)); 326 327 /* Hooks should be first three */ 328 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == 0); 329 330 n = get_chain_end(h, 0); 331 n += get_entry(h, n)->next_offset; 332 assert(h->info.hook_entry[NF_IP_FORWARD] == n); 333 334 n = get_chain_end(h, n); 335 n += get_entry(h, n)->next_offset; 336 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n); 337 338 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT]; 339 } else if (strcmp(h->info.name, "nat") == 0) { 340 assert((h->info.valid_hooks 341 == (1 << NF_IP_PRE_ROUTING 342 | 1 << NF_IP_POST_ROUTING 343 | 1 << NF_IP_LOCAL_OUT)) || 344 (h->info.valid_hooks 345 == (1 << NF_IP_PRE_ROUTING 346 | 1 << NF_IP_LOCAL_IN 347 | 1 << NF_IP_POST_ROUTING 348 | 1 << NF_IP_LOCAL_OUT))); 349 350 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0); 351 352 n = get_chain_end(h, 0); 353 354 n += get_entry(h, n)->next_offset; 355 assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n); 356 n = get_chain_end(h, n); 357 358 n += get_entry(h, n)->next_offset; 359 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n); 360 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT]; 361 362 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) { 363 n = get_chain_end(h, n); 364 n += get_entry(h, n)->next_offset; 365 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n); 366 user_offset = h->info.hook_entry[NF_IP_LOCAL_IN]; 367 } 368 369 } else if (strcmp(h->info.name, "mangle") == 0) { 370 /* This code is getting ugly because linux < 2.4.18-pre6 had 371 * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks 372 * */ 373 assert((h->info.valid_hooks 374 == (1 << NF_IP_PRE_ROUTING 375 | 1 << NF_IP_LOCAL_OUT)) || 376 (h->info.valid_hooks 377 == (1 << NF_IP_PRE_ROUTING 378 | 1 << NF_IP_LOCAL_IN 379 | 1 << NF_IP_FORWARD 380 | 1 << NF_IP_LOCAL_OUT 381 | 1 << NF_IP_POST_ROUTING))); 382 383 /* Hooks should be first five */ 384 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0); 385 386 n = get_chain_end(h, 0); 387 388 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) { 389 n += get_entry(h, n)->next_offset; 390 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n); 391 n = get_chain_end(h, n); 392 } 393 394 if (h->info.valid_hooks & (1 << NF_IP_FORWARD)) { 395 n += get_entry(h, n)->next_offset; 396 assert(h->info.hook_entry[NF_IP_FORWARD] == n); 397 n = get_chain_end(h, n); 398 } 399 400 n += get_entry(h, n)->next_offset; 401 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n); 402 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT]; 403 404 if (h->info.valid_hooks & (1 << NF_IP_POST_ROUTING)) { 405 n = get_chain_end(h, n); 406 n += get_entry(h, n)->next_offset; 407 assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n); 408 user_offset = h->info.hook_entry[NF_IP_POST_ROUTING]; 409 } 410 } else if (strcmp(h->info.name, "raw") == 0) { 411 assert(h->info.valid_hooks 412 == (1 << NF_IP_PRE_ROUTING 413 | 1 << NF_IP_LOCAL_OUT)); 414 415 /* Hooks should be first three */ 416 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0); 417 418 n = get_chain_end(h, n); 419 n += get_entry(h, n)->next_offset; 420 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n); 421 422 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT]; 423 } else { 424 fprintf(stderr, "Unknown table `%s'\n", h->info.name); 425 abort(); 426 } 427 428 /* User chain == end of last builtin + policy entry */ 429 user_offset = get_chain_end(h, user_offset); 430 user_offset += get_entry(h, user_offset)->next_offset; 431 432 /* Overflows should be end of entry chains, and unconditional 433 policy nodes. */ 434 for (i = 0; i < NUMHOOKS; i++) { 435 STRUCT_ENTRY *e; 436 STRUCT_STANDARD_TARGET *t; 437 438 if (!(h->info.valid_hooks & (1 << i))) 439 continue; 440 assert(h->info.underflow[i] 441 == get_chain_end(h, h->info.hook_entry[i])); 442 443 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i])); 444 assert(unconditional(&e->ip)); 445 assert(e->target_offset == sizeof(*e)); 446 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); 447 assert(t->target.u.target_size == ALIGN(sizeof(*t))); 448 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t))); 449 450 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0); 451 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1); 452 453 /* Hooks and underflows must be valid entries */ 454 entry2index(h, get_entry(h, h->info.hook_entry[i])); 455 entry2index(h, get_entry(h, h->info.underflow[i])); 456 } 457 458 assert(h->info.size 459 >= h->info.num_entries * (sizeof(STRUCT_ENTRY) 460 +sizeof(STRUCT_STANDARD_TARGET))); 461 462 assert(h->entries.size 463 >= (h->new_number 464 * (sizeof(STRUCT_ENTRY) 465 + sizeof(STRUCT_STANDARD_TARGET)))); 466 assert(strcmp(h->info.name, h->entries.name) == 0); 467 468 i = 0; n = 0; 469 was_return = 0; 470 /* Check all the entries. */ 471 ENTRY_ITERATE(h->entries.entrytable, h->entries.size, 472 check_entry, &i, &n, user_offset, &was_return, h); 473 474 assert(i == h->new_number); 475 assert(n == h->entries.size); 476 477 /* Final entry must be error node */ 478 assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1)) 479 ->u.user.name, 480 ERROR_TARGET) == 0); 481} 482#endif /*IPTC_DEBUG*/ 483 484#endif 485