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 | #include <arpa/inet.h> |
| 21 | |
| 22 | #ifdef DEBUG_CONNTRACK |
| 23 | #define inline |
| 24 | #endif |
| 25 | |
| 26 | #if !defined(__GLIBC__) || (__GLIBC__ < 2) |
| 27 | typedef unsigned int socklen_t; |
| 28 | #endif |
| 29 | |
| 30 | #include "libiptc/libip6tc.h" |
| 31 | |
| 32 | #define HOOK_PRE_ROUTING NF_IP6_PRE_ROUTING |
| 33 | #define HOOK_LOCAL_IN NF_IP6_LOCAL_IN |
| 34 | #define HOOK_FORWARD NF_IP6_FORWARD |
| 35 | #define HOOK_LOCAL_OUT NF_IP6_LOCAL_OUT |
| 36 | #define HOOK_POST_ROUTING NF_IP6_POST_ROUTING |
| 37 | |
| 38 | #define STRUCT_ENTRY_TARGET struct ip6t_entry_target |
| 39 | #define STRUCT_ENTRY struct ip6t_entry |
| 40 | #define STRUCT_ENTRY_MATCH struct ip6t_entry_match |
| 41 | #define STRUCT_GETINFO struct ip6t_getinfo |
| 42 | #define STRUCT_GET_ENTRIES struct ip6t_get_entries |
| 43 | #define STRUCT_COUNTERS struct ip6t_counters |
| 44 | #define STRUCT_COUNTERS_INFO struct ip6t_counters_info |
| 45 | #define STRUCT_STANDARD_TARGET struct ip6t_standard_target |
| 46 | #define STRUCT_REPLACE struct ip6t_replace |
| 47 | |
| 48 | #define STRUCT_TC_HANDLE struct ip6tc_handle |
| 49 | #define TC_HANDLE_T ip6tc_handle_t |
| 50 | |
| 51 | #define ENTRY_ITERATE IP6T_ENTRY_ITERATE |
| 52 | #define TABLE_MAXNAMELEN IP6T_TABLE_MAXNAMELEN |
| 53 | #define FUNCTION_MAXNAMELEN IP6T_FUNCTION_MAXNAMELEN |
| 54 | |
| 55 | #define GET_TARGET ip6t_get_target |
| 56 | |
| 57 | #define ERROR_TARGET IP6T_ERROR_TARGET |
| 58 | #define NUMHOOKS NF_IP6_NUMHOOKS |
| 59 | |
| 60 | #define IPT_CHAINLABEL ip6t_chainlabel |
| 61 | |
| 62 | #define TC_DUMP_ENTRIES dump_entries6 |
| 63 | #define TC_IS_CHAIN ip6tc_is_chain |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 64 | #define TC_FIRST_CHAIN ip6tc_first_chain |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 65 | #define TC_NEXT_CHAIN ip6tc_next_chain |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 66 | #define TC_FIRST_RULE ip6tc_first_rule |
| 67 | #define TC_NEXT_RULE ip6tc_next_rule |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 68 | #define TC_GET_TARGET ip6tc_get_target |
| 69 | #define TC_BUILTIN ip6tc_builtin |
| 70 | #define TC_GET_POLICY ip6tc_get_policy |
| 71 | #define TC_INSERT_ENTRY ip6tc_insert_entry |
| 72 | #define TC_REPLACE_ENTRY ip6tc_replace_entry |
| 73 | #define TC_APPEND_ENTRY ip6tc_append_entry |
| 74 | #define TC_DELETE_ENTRY ip6tc_delete_entry |
| 75 | #define TC_DELETE_NUM_ENTRY ip6tc_delete_num_entry |
| 76 | #define TC_CHECK_PACKET ip6tc_check_packet |
| 77 | #define TC_FLUSH_ENTRIES ip6tc_flush_entries |
| 78 | #define TC_ZERO_ENTRIES ip6tc_zero_entries |
Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 79 | #define TC_ZERO_COUNTER ip6tc_zero_counter |
| 80 | #define TC_READ_COUNTER ip6tc_read_counter |
| 81 | #define TC_SET_COUNTER ip6tc_set_counter |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 82 | #define TC_CREATE_CHAIN ip6tc_create_chain |
| 83 | #define TC_GET_REFERENCES ip6tc_get_references |
| 84 | #define TC_DELETE_CHAIN ip6tc_delete_chain |
| 85 | #define TC_RENAME_CHAIN ip6tc_rename_chain |
| 86 | #define TC_SET_POLICY ip6tc_set_policy |
| 87 | #define TC_GET_RAW_SOCKET ip6tc_get_raw_socket |
| 88 | #define TC_INIT ip6tc_init |
Martin Josefsson | 841e4ae | 2003-05-02 15:30:11 +0000 | [diff] [blame] | 89 | #define TC_FREE ip6tc_free |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 90 | #define TC_COMMIT ip6tc_commit |
| 91 | #define TC_STRERROR ip6tc_strerror |
Phil Oester | 7006729 | 2006-07-05 09:31:45 +0000 | [diff] [blame] | 92 | #define TC_NUM_RULES ip6tc_num_rules |
| 93 | #define TC_GET_RULE ip6tc_get_rule |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 94 | |
| 95 | #define TC_AF AF_INET6 |
| 96 | #define TC_IPPROTO IPPROTO_IPV6 |
| 97 | |
| 98 | #define SO_SET_REPLACE IP6T_SO_SET_REPLACE |
| 99 | #define SO_SET_ADD_COUNTERS IP6T_SO_SET_ADD_COUNTERS |
| 100 | #define SO_GET_INFO IP6T_SO_GET_INFO |
| 101 | #define SO_GET_ENTRIES IP6T_SO_GET_ENTRIES |
| 102 | #define SO_GET_VERSION IP6T_SO_GET_VERSION |
| 103 | |
| 104 | #define STANDARD_TARGET IP6T_STANDARD_TARGET |
| 105 | #define LABEL_RETURN IP6TC_LABEL_RETURN |
| 106 | #define LABEL_ACCEPT IP6TC_LABEL_ACCEPT |
| 107 | #define LABEL_DROP IP6TC_LABEL_DROP |
Philip Blundell | 88eb835 | 2000-05-10 00:25:04 +0000 | [diff] [blame] | 108 | #define LABEL_QUEUE IP6TC_LABEL_QUEUE |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 109 | |
| 110 | #define ALIGN IP6T_ALIGN |
| 111 | #define RETURN IP6T_RETURN |
| 112 | |
| 113 | #include "libiptc.c" |
| 114 | |
| 115 | #define BIT6(a, l) \ |
Harald Welte | cc7bb65 | 2003-06-24 18:28:36 +0000 | [diff] [blame] | 116 | ((ntohl(a->in6_u.u6_addr32[(l) / 32]) >> (31 - ((l) & 31))) & 1) |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 117 | |
| 118 | int |
| 119 | ipv6_prefix_length(const struct in6_addr *a) |
| 120 | { |
| 121 | int l, i; |
| 122 | for (l = 0; l < 128; l++) { |
| 123 | if (BIT6(a, l) == 0) |
| 124 | break; |
| 125 | } |
| 126 | for (i = l + 1; i < 128; i++) { |
| 127 | if (BIT6(a, i) == 1) |
| 128 | return -1; |
| 129 | } |
| 130 | return l; |
| 131 | } |
| 132 | |
| 133 | static int |
| 134 | dump_entry(struct ip6t_entry *e, const ip6tc_handle_t handle) |
| 135 | { |
| 136 | size_t i; |
| 137 | char buf[40]; |
| 138 | int len; |
| 139 | struct ip6t_entry_target *t; |
| 140 | |
Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 141 | printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e), |
| 142 | iptcb_entry2offset(handle, e)); |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 143 | puts("SRC IP: "); |
| 144 | inet_ntop(AF_INET6, &e->ipv6.src, buf, sizeof buf); |
| 145 | puts(buf); |
| 146 | putchar('/'); |
| 147 | len = ipv6_prefix_length(&e->ipv6.smsk); |
| 148 | if (len != -1) |
| 149 | printf("%d", len); |
| 150 | else { |
| 151 | inet_ntop(AF_INET6, &e->ipv6.smsk, buf, sizeof buf); |
| 152 | puts(buf); |
| 153 | } |
| 154 | putchar('\n'); |
| 155 | |
| 156 | puts("DST IP: "); |
| 157 | inet_ntop(AF_INET6, &e->ipv6.dst, buf, sizeof buf); |
| 158 | puts(buf); |
| 159 | putchar('/'); |
| 160 | len = ipv6_prefix_length(&e->ipv6.dmsk); |
| 161 | if (len != -1) |
| 162 | printf("%d", len); |
| 163 | else { |
| 164 | inet_ntop(AF_INET6, &e->ipv6.dmsk, buf, sizeof buf); |
| 165 | puts(buf); |
| 166 | } |
| 167 | putchar('\n'); |
| 168 | |
| 169 | printf("Interface: `%s'/", e->ipv6.iniface); |
| 170 | for (i = 0; i < IFNAMSIZ; i++) |
| 171 | printf("%c", e->ipv6.iniface_mask[i] ? 'X' : '.'); |
| 172 | printf("to `%s'/", e->ipv6.outiface); |
| 173 | for (i = 0; i < IFNAMSIZ; i++) |
| 174 | printf("%c", e->ipv6.outiface_mask[i] ? 'X' : '.'); |
| 175 | printf("\nProtocol: %u\n", e->ipv6.proto); |
| 176 | if (e->ipv6.flags & IP6T_F_TOS) |
| 177 | printf("TOS: %u\n", e->ipv6.tos); |
| 178 | printf("Flags: %02X\n", e->ipv6.flags); |
| 179 | printf("Invflags: %02X\n", e->ipv6.invflags); |
| 180 | printf("Counters: %llu packets, %llu bytes\n", |
Martin Josefsson | a28d495 | 2004-05-26 16:04:48 +0000 | [diff] [blame] | 181 | (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt); |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 182 | printf("Cache: %08X\n", e->nfcache); |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 183 | |
| 184 | IP6T_MATCH_ITERATE(e, print_match); |
| 185 | |
| 186 | t = ip6t_get_target(e); |
Philip Blundell | 88eb835 | 2000-05-10 00:25:04 +0000 | [diff] [blame] | 187 | printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size); |
| 188 | if (strcmp(t->u.user.name, IP6T_STANDARD_TARGET) == 0) { |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 189 | int pos = *(int *)t->data; |
| 190 | if (pos < 0) |
| 191 | printf("verdict=%s\n", |
| 192 | pos == -NF_ACCEPT-1 ? "NF_ACCEPT" |
| 193 | : pos == -NF_DROP-1 ? "NF_DROP" |
| 194 | : pos == IP6T_RETURN ? "RETURN" |
| 195 | : "UNKNOWN"); |
| 196 | else |
| 197 | printf("verdict=%u\n", pos); |
Philip Blundell | 88eb835 | 2000-05-10 00:25:04 +0000 | [diff] [blame] | 198 | } else if (strcmp(t->u.user.name, IP6T_ERROR_TARGET) == 0) |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 199 | printf("error=`%s'\n", t->data); |
| 200 | |
| 201 | printf("\n"); |
| 202 | return 0; |
| 203 | } |
| 204 | |
Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 205 | static unsigned char * |
Philip Blundell | 88eb835 | 2000-05-10 00:25:04 +0000 | [diff] [blame] | 206 | is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b, |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 207 | unsigned char *matchmask) |
| 208 | { |
| 209 | unsigned int i; |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 210 | unsigned char *mptr; |
| 211 | |
| 212 | /* Always compare head structures: ignore mask here. */ |
| 213 | if (memcmp(&a->ipv6.src, &b->ipv6.src, sizeof(struct in6_addr)) |
| 214 | || memcmp(&a->ipv6.dst, &b->ipv6.dst, sizeof(struct in6_addr)) |
| 215 | || memcmp(&a->ipv6.smsk, &b->ipv6.smsk, sizeof(struct in6_addr)) |
| 216 | || memcmp(&a->ipv6.dmsk, &b->ipv6.dmsk, sizeof(struct in6_addr)) |
| 217 | || a->ipv6.proto != b->ipv6.proto |
| 218 | || a->ipv6.tos != b->ipv6.tos |
| 219 | || a->ipv6.flags != b->ipv6.flags |
| 220 | || a->ipv6.invflags != b->ipv6.invflags) |
Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 221 | return NULL; |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 222 | |
| 223 | for (i = 0; i < IFNAMSIZ; i++) { |
| 224 | if (a->ipv6.iniface_mask[i] != b->ipv6.iniface_mask[i]) |
Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 225 | return NULL; |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 226 | if ((a->ipv6.iniface[i] & a->ipv6.iniface_mask[i]) |
| 227 | != (b->ipv6.iniface[i] & b->ipv6.iniface_mask[i])) |
Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 228 | return NULL; |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 229 | if (a->ipv6.outiface_mask[i] != b->ipv6.outiface_mask[i]) |
Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 230 | return NULL; |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 231 | if ((a->ipv6.outiface[i] & a->ipv6.outiface_mask[i]) |
| 232 | != (b->ipv6.outiface[i] & b->ipv6.outiface_mask[i])) |
Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 233 | return NULL; |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Peter Riley | ea146a9 | 2007-09-02 13:09:07 +0000 | [diff] [blame] | 236 | if (a->target_offset != b->target_offset |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 237 | || a->next_offset != b->next_offset) |
Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 238 | return NULL; |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 239 | |
Philip Blundell | 88eb835 | 2000-05-10 00:25:04 +0000 | [diff] [blame] | 240 | mptr = matchmask + sizeof(STRUCT_ENTRY); |
| 241 | if (IP6T_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr)) |
Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 242 | return NULL; |
Pablo Neira | 5ee8862 | 2005-06-23 08:51:18 +0000 | [diff] [blame] | 243 | mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_target)); |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 244 | |
Rusty Russell | 733e54b | 2004-12-16 14:22:23 +0000 | [diff] [blame] | 245 | return mptr; |
Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 246 | } |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 247 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 248 | /* All zeroes == unconditional rule. */ |
| 249 | static inline int |
| 250 | unconditional(const struct ip6t_ip6 *ipv6) |
| 251 | { |
| 252 | unsigned int i; |
| 253 | |
| 254 | for (i = 0; i < sizeof(*ipv6); i++) |
| 255 | if (((char *)ipv6)[i]) |
| 256 | break; |
| 257 | |
| 258 | return (i == sizeof(*ipv6)); |
| 259 | } |
| 260 | |
Harald Welte | 380ba5f | 2002-02-13 16:19:55 +0000 | [diff] [blame] | 261 | #ifdef IPTC_DEBUG |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 262 | /* Do every conceivable sanity check on the handle */ |
| 263 | static void |
| 264 | do_check(TC_HANDLE_T h, unsigned int line) |
| 265 | { |
| 266 | unsigned int i, n; |
| 267 | unsigned int user_offset; /* Offset of first user chain */ |
| 268 | int was_return; |
| 269 | |
| 270 | assert(h->changed == 0 || h->changed == 1); |
| 271 | if (strcmp(h->info.name, "filter") == 0) { |
| 272 | assert(h->info.valid_hooks |
| 273 | == (1 << NF_IP6_LOCAL_IN |
| 274 | | 1 << NF_IP6_FORWARD |
| 275 | | 1 << NF_IP6_LOCAL_OUT)); |
| 276 | |
| 277 | /* Hooks should be first three */ |
| 278 | assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == 0); |
| 279 | |
| 280 | n = get_chain_end(h, 0); |
| 281 | n += get_entry(h, n)->next_offset; |
| 282 | assert(h->info.hook_entry[NF_IP6_FORWARD] == n); |
| 283 | |
| 284 | n = get_chain_end(h, n); |
| 285 | n += get_entry(h, n)->next_offset; |
| 286 | assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n); |
| 287 | |
| 288 | user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT]; |
| 289 | } else if (strcmp(h->info.name, "nat") == 0) { |
Harald Welte | 95df8e7 | 2002-02-13 23:13:23 +0000 | [diff] [blame] | 290 | assert((h->info.valid_hooks |
| 291 | == (1 << NF_IP6_PRE_ROUTING |
| 292 | | 1 << NF_IP6_LOCAL_OUT |
| 293 | | 1 << NF_IP6_POST_ROUTING)) || |
| 294 | (h->info.valid_hooks |
| 295 | == (1 << NF_IP6_PRE_ROUTING |
| 296 | | 1 << NF_IP6_LOCAL_IN |
| 297 | | 1 << NF_IP6_LOCAL_OUT |
| 298 | | 1 << NF_IP6_POST_ROUTING))); |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 299 | |
| 300 | assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0); |
| 301 | |
| 302 | n = get_chain_end(h, 0); |
Harald Welte | 95df8e7 | 2002-02-13 23:13:23 +0000 | [diff] [blame] | 303 | |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 304 | n += get_entry(h, n)->next_offset; |
| 305 | assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n); |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 306 | n = get_chain_end(h, n); |
Harald Welte | 95df8e7 | 2002-02-13 23:13:23 +0000 | [diff] [blame] | 307 | |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 308 | n += get_entry(h, n)->next_offset; |
| 309 | assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n); |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 310 | user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT]; |
Harald Welte | 95df8e7 | 2002-02-13 23:13:23 +0000 | [diff] [blame] | 311 | |
| 312 | if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) { |
| 313 | n = get_chain_end(h, n); |
| 314 | n += get_entry(h, n)->next_offset; |
| 315 | assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n); |
| 316 | user_offset = h->info.hook_entry[NF_IP6_LOCAL_IN]; |
| 317 | } |
| 318 | |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 319 | } else if (strcmp(h->info.name, "mangle") == 0) { |
Harald Welte | 596707c | 2002-02-13 16:35:39 +0000 | [diff] [blame] | 320 | /* This code is getting ugly because linux < 2.4.18-pre6 had |
| 321 | * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks |
| 322 | * */ |
Harald Welte | 95df8e7 | 2002-02-13 23:13:23 +0000 | [diff] [blame] | 323 | assert((h->info.valid_hooks |
| 324 | == (1 << NF_IP6_PRE_ROUTING |
| 325 | | 1 << NF_IP6_LOCAL_OUT)) || |
| 326 | (h->info.valid_hooks |
| 327 | == (1 << NF_IP6_PRE_ROUTING |
| 328 | | 1 << NF_IP6_LOCAL_IN |
| 329 | | 1 << NF_IP6_FORWARD |
| 330 | | 1 << NF_IP6_LOCAL_OUT |
| 331 | | 1 << NF_IP6_POST_ROUTING))); |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 332 | |
Harald Welte | 380ba5f | 2002-02-13 16:19:55 +0000 | [diff] [blame] | 333 | /* Hooks should be first five */ |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 334 | assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0); |
| 335 | |
| 336 | n = get_chain_end(h, 0); |
Harald Welte | 380ba5f | 2002-02-13 16:19:55 +0000 | [diff] [blame] | 337 | |
Harald Welte | a540b1b | 2002-02-13 22:42:52 +0000 | [diff] [blame] | 338 | if (h->info.valid_hooks & (1 << NF_IP6_LOCAL_IN)) { |
Harald Welte | 596707c | 2002-02-13 16:35:39 +0000 | [diff] [blame] | 339 | n += get_entry(h, n)->next_offset; |
| 340 | assert(h->info.hook_entry[NF_IP6_LOCAL_IN] == n); |
| 341 | n = get_chain_end(h, n); |
| 342 | } |
Harald Welte | 380ba5f | 2002-02-13 16:19:55 +0000 | [diff] [blame] | 343 | |
Harald Welte | a540b1b | 2002-02-13 22:42:52 +0000 | [diff] [blame] | 344 | if (h->info.valid_hooks & (1 << NF_IP6_FORWARD)) { |
Harald Welte | 596707c | 2002-02-13 16:35:39 +0000 | [diff] [blame] | 345 | n += get_entry(h, n)->next_offset; |
| 346 | assert(h->info.hook_entry[NF_IP6_FORWARD] == n); |
| 347 | n = get_chain_end(h, n); |
| 348 | } |
| 349 | |
Harald Welte | 380ba5f | 2002-02-13 16:19:55 +0000 | [diff] [blame] | 350 | n += get_entry(h, n)->next_offset; |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 351 | assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n); |
Harald Welte | 596707c | 2002-02-13 16:35:39 +0000 | [diff] [blame] | 352 | user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT]; |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 353 | |
Harald Welte | a540b1b | 2002-02-13 22:42:52 +0000 | [diff] [blame] | 354 | if (h->info.valid_hooks & (1 << NF_IP6_POST_ROUTING)) { |
Harald Welte | 596707c | 2002-02-13 16:35:39 +0000 | [diff] [blame] | 355 | n = get_chain_end(h, n); |
| 356 | n += get_entry(h, n)->next_offset; |
| 357 | assert(h->info.hook_entry[NF_IP6_POST_ROUTING] == n); |
| 358 | user_offset = h->info.hook_entry[NF_IP6_POST_ROUTING]; |
| 359 | } |
Harald Welte | 50fceae | 2003-10-07 22:12:31 +0000 | [diff] [blame] | 360 | } else if (strcmp(h->info.name, "raw") == 0) { |
| 361 | assert(h->info.valid_hooks |
| 362 | == (1 << NF_IP6_PRE_ROUTING |
| 363 | | 1 << NF_IP6_LOCAL_OUT)); |
| 364 | |
| 365 | /* Hooks should be first three */ |
| 366 | assert(h->info.hook_entry[NF_IP6_PRE_ROUTING] == 0); |
| 367 | |
| 368 | n = get_chain_end(h, n); |
| 369 | n += get_entry(h, n)->next_offset; |
| 370 | assert(h->info.hook_entry[NF_IP6_LOCAL_OUT] == n); |
| 371 | |
| 372 | user_offset = h->info.hook_entry[NF_IP6_LOCAL_OUT]; |
Harald Welte | a540b1b | 2002-02-13 22:42:52 +0000 | [diff] [blame] | 373 | } else { |
| 374 | fprintf(stderr, "Unknown table `%s'\n", h->info.name); |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 375 | abort(); |
Harald Welte | a540b1b | 2002-02-13 22:42:52 +0000 | [diff] [blame] | 376 | } |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 377 | |
| 378 | /* User chain == end of last builtin + policy entry */ |
| 379 | user_offset = get_chain_end(h, user_offset); |
| 380 | user_offset += get_entry(h, user_offset)->next_offset; |
| 381 | |
| 382 | /* Overflows should be end of entry chains, and unconditional |
| 383 | policy nodes. */ |
| 384 | for (i = 0; i < NUMHOOKS; i++) { |
| 385 | STRUCT_ENTRY *e; |
| 386 | STRUCT_STANDARD_TARGET *t; |
| 387 | |
| 388 | if (!(h->info.valid_hooks & (1 << i))) |
| 389 | continue; |
| 390 | assert(h->info.underflow[i] |
| 391 | == get_chain_end(h, h->info.hook_entry[i])); |
| 392 | |
| 393 | e = get_entry(h, get_chain_end(h, h->info.hook_entry[i])); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 394 | assert(unconditional(&e->ipv6)); |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 395 | assert(e->target_offset == sizeof(*e)); |
| 396 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); |
Harald Welte | a540b1b | 2002-02-13 22:42:52 +0000 | [diff] [blame] | 397 | printf("target_size=%u, align=%u\n", |
| 398 | t->target.u.target_size, ALIGN(sizeof(*t))); |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 399 | assert(t->target.u.target_size == ALIGN(sizeof(*t))); |
| 400 | assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t))); |
| 401 | |
| 402 | assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0); |
| 403 | assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1); |
| 404 | |
| 405 | /* Hooks and underflows must be valid entries */ |
Harald Welte | aae69be | 2004-08-29 23:32:14 +0000 | [diff] [blame] | 406 | iptcb_entry2index(h, get_entry(h, h->info.hook_entry[i])); |
| 407 | iptcb_entry2index(h, get_entry(h, h->info.underflow[i])); |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | assert(h->info.size |
| 411 | >= h->info.num_entries * (sizeof(STRUCT_ENTRY) |
| 412 | +sizeof(STRUCT_STANDARD_TARGET))); |
| 413 | |
| 414 | assert(h->entries.size |
| 415 | >= (h->new_number |
| 416 | * (sizeof(STRUCT_ENTRY) |
| 417 | + sizeof(STRUCT_STANDARD_TARGET)))); |
| 418 | assert(strcmp(h->info.name, h->entries.name) == 0); |
| 419 | |
| 420 | i = 0; n = 0; |
| 421 | was_return = 0; |
| 422 | |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 423 | #if 0 |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 424 | /* Check all the entries. */ |
Harald Welte | a540b1b | 2002-02-13 22:42:52 +0000 | [diff] [blame] | 425 | ENTRY_ITERATE(h->entries.entrytable, h->entries.size, |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 426 | check_entry, &i, &n, user_offset, &was_return, h); |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 427 | |
| 428 | assert(i == h->new_number); |
| 429 | assert(n == h->entries.size); |
| 430 | |
| 431 | /* Final entry must be error node */ |
| 432 | assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1)) |
| 433 | ->u.user.name, |
| 434 | ERROR_TARGET) == 0); |
Rusty Russell | 5eed48a | 2000-06-02 20:12:24 +0000 | [diff] [blame] | 435 | #endif |
Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 436 | } |
Harald Welte | 380ba5f | 2002-02-13 16:19:55 +0000 | [diff] [blame] | 437 | #endif /*IPTC_DEBUG*/ |