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