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