| Tomáš Lejdar | 23a6b45 | 2003-04-30 15:57:01 +0000 | [diff] [blame] | 1 | /* Library which manipulates firewall rules. Version $Revision: 1.35 $ */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 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 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 14 | #ifndef IPT_LIB_DIR |
| 15 | #define IPT_LIB_DIR "/usr/local/lib/iptables" |
| 16 | #endif |
| 17 | |
| Rusty Russell | 4e242f8 | 2000-05-31 06:33:50 +0000 | [diff] [blame] | 18 | #ifndef __OPTIMIZE__ |
| Harald Welte | ec81ca7 | 2001-05-26 04:35:49 +0000 | [diff] [blame] | 19 | STRUCT_ENTRY_TARGET * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 20 | GET_TARGET(STRUCT_ENTRY *e) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 21 | { |
| 22 | return (void *)e + e->target_offset; |
| 23 | } |
| 24 | #endif |
| 25 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 26 | static int sockfd = -1; |
| 27 | static void *iptc_fn = NULL; |
| 28 | |
| 29 | static const char *hooknames[] |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 30 | = { [HOOK_PRE_ROUTING] "PREROUTING", |
| 31 | [HOOK_LOCAL_IN] "INPUT", |
| 32 | [HOOK_FORWARD] "FORWARD", |
| 33 | [HOOK_LOCAL_OUT] "OUTPUT", |
| Rusty Russell | 10758b7 | 2000-09-14 07:37:33 +0000 | [diff] [blame] | 34 | [HOOK_POST_ROUTING] "POSTROUTING", |
| 35 | #ifdef HOOK_DROPPING |
| 36 | [HOOK_DROPPING] "DROPPING" |
| 37 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct counter_map |
| 41 | { |
| 42 | enum { |
| 43 | COUNTER_MAP_NOMAP, |
| 44 | COUNTER_MAP_NORMAL_MAP, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 45 | COUNTER_MAP_ZEROED, |
| 46 | COUNTER_MAP_SET |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 47 | } maptype; |
| 48 | unsigned int mappos; |
| 49 | }; |
| 50 | |
| 51 | /* Convenience structures */ |
| 52 | struct ipt_error_target |
| 53 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 54 | STRUCT_ENTRY_TARGET t; |
| 55 | char error[TABLE_MAXNAMELEN]; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 58 | struct chain_cache |
| 59 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 60 | char name[TABLE_MAXNAMELEN]; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 61 | /* This is the first rule in chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 62 | STRUCT_ENTRY *start; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 63 | /* Last rule in chain */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 64 | STRUCT_ENTRY *end; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 67 | STRUCT_TC_HANDLE |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 68 | { |
| 69 | /* Have changes been made? */ |
| 70 | int changed; |
| 71 | /* Size in here reflects original state. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 72 | STRUCT_GETINFO info; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 73 | |
| 74 | struct counter_map *counter_map; |
| 75 | /* Array of hook names */ |
| 76 | const char **hooknames; |
| 77 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 78 | /* Cached position of chain heads (NULL = no cache). */ |
| 79 | unsigned int cache_num_chains; |
| 80 | unsigned int cache_num_builtins; |
| 81 | struct chain_cache *cache_chain_heads; |
| 82 | |
| 83 | /* Chain iterator: current chain cache entry. */ |
| 84 | struct chain_cache *cache_chain_iteration; |
| 85 | |
| 86 | /* Rule iterator: terminal rule */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 87 | STRUCT_ENTRY *cache_rule_end; |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 88 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 89 | /* Number in here reflects current state. */ |
| 90 | unsigned int new_number; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 91 | STRUCT_GET_ENTRIES entries; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 94 | static void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 95 | set_changed(TC_HANDLE_T h) |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 96 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 97 | if (h->cache_chain_heads) { |
| 98 | free(h->cache_chain_heads); |
| 99 | h->cache_chain_heads = NULL; |
| 100 | h->cache_num_chains = 0; |
| 101 | h->cache_chain_iteration = NULL; |
| 102 | h->cache_rule_end = NULL; |
| 103 | } |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 104 | h->changed = 1; |
| 105 | } |
| 106 | |
| Harald Welte | 380ba5f | 2002-02-13 16:19:55 +0000 | [diff] [blame] | 107 | #ifdef IPTC_DEBUG |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 108 | static void do_check(TC_HANDLE_T h, unsigned int line); |
| Rusty Russell | 849779c | 2000-04-23 15:51:51 +0000 | [diff] [blame] | 109 | #define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 110 | #else |
| 111 | #define CHECK(h) |
| 112 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 113 | |
| 114 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 115 | get_number(const STRUCT_ENTRY *i, |
| 116 | const STRUCT_ENTRY *seek, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 117 | unsigned int *pos) |
| 118 | { |
| 119 | if (i == seek) |
| 120 | return 1; |
| 121 | (*pos)++; |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static unsigned int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 126 | entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 127 | { |
| 128 | unsigned int pos = 0; |
| 129 | |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 130 | if (ENTRY_ITERATE(h->entries.entrytable, h->entries.size, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 131 | get_number, seek, &pos) == 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 132 | fprintf(stderr, "ERROR: offset %i not an entry!\n", |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 133 | (char *)seek - (char *)h->entries.entrytable); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 134 | abort(); |
| 135 | } |
| 136 | return pos; |
| 137 | } |
| 138 | |
| 139 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 140 | get_entry_n(STRUCT_ENTRY *i, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 141 | unsigned int number, |
| 142 | unsigned int *pos, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 143 | STRUCT_ENTRY **pe) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 144 | { |
| 145 | if (*pos == number) { |
| 146 | *pe = i; |
| 147 | return 1; |
| 148 | } |
| 149 | (*pos)++; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 153 | static STRUCT_ENTRY * |
| 154 | index2entry(TC_HANDLE_T h, unsigned int index) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 155 | { |
| 156 | unsigned int pos = 0; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 157 | STRUCT_ENTRY *ret = NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 158 | |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 159 | ENTRY_ITERATE(h->entries.entrytable, h->entries.size, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 160 | get_entry_n, index, &pos, &ret); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 165 | static inline STRUCT_ENTRY * |
| 166 | get_entry(TC_HANDLE_T h, unsigned int offset) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 167 | { |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 168 | return (STRUCT_ENTRY *)((char *)h->entries.entrytable + offset); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static inline unsigned long |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 172 | entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 173 | { |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 174 | return (char *)e - (char *)h->entries.entrytable; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static unsigned long |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 178 | index2offset(TC_HANDLE_T h, unsigned int index) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 179 | { |
| 180 | return entry2offset(h, index2entry(h, index)); |
| 181 | } |
| 182 | |
| 183 | static const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 184 | get_errorlabel(TC_HANDLE_T h, unsigned int offset) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 185 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 186 | STRUCT_ENTRY *e; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 187 | |
| 188 | e = get_entry(h, offset); |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 189 | if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) != 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 190 | fprintf(stderr, "ERROR: offset %u not an error node!\n", |
| 191 | offset); |
| 192 | abort(); |
| 193 | } |
| 194 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 195 | return (const char *)GET_TARGET(e)->data; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Allocate handle of given size */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 199 | static TC_HANDLE_T |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 200 | alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules) |
| 201 | { |
| 202 | size_t len; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 203 | TC_HANDLE_T h; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 204 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 205 | len = sizeof(STRUCT_TC_HANDLE) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 206 | + size |
| 207 | + num_rules * sizeof(struct counter_map); |
| 208 | |
| 209 | if ((h = malloc(len)) == NULL) { |
| 210 | errno = ENOMEM; |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | h->changed = 0; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 215 | h->cache_num_chains = 0; |
| 216 | h->cache_chain_heads = NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 217 | h->counter_map = (void *)h |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 218 | + sizeof(STRUCT_TC_HANDLE) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 219 | + size; |
| 220 | strcpy(h->info.name, tablename); |
| 221 | strcpy(h->entries.name, tablename); |
| 222 | |
| 223 | return h; |
| 224 | } |
| 225 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 226 | TC_HANDLE_T |
| 227 | TC_INIT(const char *tablename) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 228 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 229 | TC_HANDLE_T h; |
| 230 | STRUCT_GETINFO info; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 231 | unsigned int i; |
| 232 | int tmp; |
| 233 | socklen_t s; |
| 234 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 235 | iptc_fn = TC_INIT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 236 | |
| Harald Welte | 366454b | 2002-01-07 13:46:50 +0000 | [diff] [blame] | 237 | if (sockfd != -1) |
| 238 | close(sockfd); |
| 239 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 240 | sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 241 | if (sockfd < 0) |
| 242 | return NULL; |
| 243 | |
| 244 | s = sizeof(info); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 245 | if (strlen(tablename) >= TABLE_MAXNAMELEN) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 246 | errno = EINVAL; |
| 247 | return NULL; |
| 248 | } |
| 249 | strcpy(info.name, tablename); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 250 | if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 251 | return NULL; |
| 252 | |
| 253 | if ((h = alloc_handle(info.name, info.size, info.num_entries)) |
| 254 | == NULL) |
| 255 | return NULL; |
| 256 | |
| 257 | /* Too hard --RR */ |
| 258 | #if 0 |
| 259 | sprintf(pathname, "%s/%s", IPT_LIB_DIR, info.name); |
| 260 | dynlib = dlopen(pathname, RTLD_NOW); |
| 261 | if (!dynlib) { |
| 262 | errno = ENOENT; |
| 263 | return NULL; |
| 264 | } |
| 265 | h->hooknames = dlsym(dynlib, "hooknames"); |
| 266 | if (!h->hooknames) { |
| 267 | errno = ENOENT; |
| 268 | return NULL; |
| 269 | } |
| 270 | #else |
| 271 | h->hooknames = hooknames; |
| 272 | #endif |
| 273 | |
| 274 | /* Initialize current state */ |
| 275 | h->info = info; |
| 276 | h->new_number = h->info.num_entries; |
| 277 | for (i = 0; i < h->info.num_entries; i++) |
| 278 | h->counter_map[i] |
| 279 | = ((struct counter_map){COUNTER_MAP_NORMAL_MAP, i}); |
| 280 | |
| 281 | h->entries.size = h->info.size; |
| 282 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 283 | tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 284 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 285 | if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, &h->entries, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 286 | &tmp) < 0) { |
| 287 | free(h); |
| 288 | return NULL; |
| 289 | } |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 290 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 291 | CHECK(h); |
| 292 | return h; |
| 293 | } |
| 294 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 295 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 296 | print_match(const STRUCT_ENTRY_MATCH *m) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 297 | { |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 298 | printf("Match name: `%s'\n", m->u.user.name); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 302 | static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle); |
| 303 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 304 | void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 305 | TC_DUMP_ENTRIES(const TC_HANDLE_T handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 306 | { |
| 307 | CHECK(handle); |
| 308 | |
| 309 | printf("libiptc v%s. %u entries, %u bytes.\n", |
| Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 310 | IPTABLES_VERSION, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 311 | handle->new_number, handle->entries.size); |
| 312 | printf("Table `%s'\n", handle->info.name); |
| 313 | printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n", |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 314 | handle->info.hook_entry[HOOK_PRE_ROUTING], |
| 315 | handle->info.hook_entry[HOOK_LOCAL_IN], |
| 316 | handle->info.hook_entry[HOOK_FORWARD], |
| 317 | handle->info.hook_entry[HOOK_LOCAL_OUT], |
| 318 | handle->info.hook_entry[HOOK_POST_ROUTING]); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 319 | printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n", |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 320 | handle->info.underflow[HOOK_PRE_ROUTING], |
| 321 | handle->info.underflow[HOOK_LOCAL_IN], |
| 322 | handle->info.underflow[HOOK_FORWARD], |
| 323 | handle->info.underflow[HOOK_LOCAL_OUT], |
| 324 | handle->info.underflow[HOOK_POST_ROUTING]); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 325 | |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 326 | ENTRY_ITERATE(handle->entries.entrytable, handle->entries.size, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 327 | dump_entry, handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 330 | /* Returns 0 if not hook entry, else hooknumber + 1 */ |
| 331 | static inline unsigned int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 332 | is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 333 | { |
| 334 | unsigned int i; |
| 335 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 336 | for (i = 0; i < NUMHOOKS; i++) { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 337 | if ((h->info.valid_hooks & (1 << i)) |
| 338 | && get_entry(h, h->info.hook_entry[i]) == e) |
| 339 | return i+1; |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 340 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 344 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 345 | add_chain(STRUCT_ENTRY *e, TC_HANDLE_T h, STRUCT_ENTRY **prev) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 346 | { |
| 347 | unsigned int builtin; |
| 348 | |
| 349 | /* Last entry. End it. */ |
| 350 | if (entry2offset(h, e) + e->next_offset == h->entries.size) { |
| 351 | /* This is the ERROR node at end of the table */ |
| 352 | h->cache_chain_heads[h->cache_num_chains-1].end = *prev; |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | /* We know this is the start of a new chain if it's an ERROR |
| 357 | target, or a hook entry point */ |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 358 | if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 359 | /* prev was last entry in previous chain */ |
| 360 | h->cache_chain_heads[h->cache_num_chains-1].end |
| 361 | = *prev; |
| 362 | |
| 363 | strcpy(h->cache_chain_heads[h->cache_num_chains].name, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 364 | (const char *)GET_TARGET(e)->data); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 365 | h->cache_chain_heads[h->cache_num_chains].start |
| 366 | = (void *)e + e->next_offset; |
| 367 | h->cache_num_chains++; |
| 368 | } else if ((builtin = is_hook_entry(e, h)) != 0) { |
| 369 | if (h->cache_num_chains > 0) |
| 370 | /* prev was last entry in previous chain */ |
| 371 | h->cache_chain_heads[h->cache_num_chains-1].end |
| 372 | = *prev; |
| 373 | |
| 374 | strcpy(h->cache_chain_heads[h->cache_num_chains].name, |
| 375 | h->hooknames[builtin-1]); |
| 376 | h->cache_chain_heads[h->cache_num_chains].start |
| 377 | = (void *)e; |
| 378 | h->cache_num_chains++; |
| 379 | } |
| 380 | |
| 381 | *prev = e; |
| 382 | return 0; |
| 383 | } |
| 384 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 385 | static int alphasort(const void *a, const void *b) |
| 386 | { |
| 387 | return strcmp(((struct chain_cache *)a)->name, |
| 388 | ((struct chain_cache *)b)->name); |
| 389 | } |
| 390 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 391 | static int populate_cache(TC_HANDLE_T h) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 392 | { |
| 393 | unsigned int i; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 394 | STRUCT_ENTRY *prev; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 395 | |
| 396 | /* # chains < # rules / 2 + num builtins - 1 */ |
| 397 | h->cache_chain_heads = malloc((h->new_number / 2 + 4) |
| 398 | * sizeof(struct chain_cache)); |
| 399 | if (!h->cache_chain_heads) { |
| 400 | errno = ENOMEM; |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | h->cache_num_chains = 0; |
| 405 | h->cache_num_builtins = 0; |
| 406 | |
| 407 | /* Count builtins */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 408 | for (i = 0; i < NUMHOOKS; i++) { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 409 | if (h->info.valid_hooks & (1 << i)) |
| 410 | h->cache_num_builtins++; |
| 411 | } |
| 412 | |
| 413 | prev = NULL; |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 414 | ENTRY_ITERATE(h->entries.entrytable, h->entries.size, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 415 | add_chain, h, &prev); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 416 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 417 | qsort(h->cache_chain_heads + h->cache_num_builtins, |
| 418 | h->cache_num_chains - h->cache_num_builtins, |
| 419 | sizeof(struct chain_cache), alphasort); |
| 420 | |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | /* Returns cache ptr if found, otherwise NULL. */ |
| 425 | static struct chain_cache * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 426 | find_label(const char *name, TC_HANDLE_T handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 427 | { |
| Rusty Russell | 849779c | 2000-04-23 15:51:51 +0000 | [diff] [blame] | 428 | unsigned int i; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 429 | |
| 430 | if (handle->cache_chain_heads == NULL |
| 431 | && !populate_cache(handle)) |
| 432 | return NULL; |
| 433 | |
| Rusty Russell | 849779c | 2000-04-23 15:51:51 +0000 | [diff] [blame] | 434 | /* FIXME: Linear search through builtins, then binary --RR */ |
| 435 | for (i = 0; i < handle->cache_num_chains; i++) { |
| 436 | if (strcmp(handle->cache_chain_heads[i].name, name) == 0) |
| 437 | return &handle->cache_chain_heads[i]; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| Rusty Russell | 849779c | 2000-04-23 15:51:51 +0000 | [diff] [blame] | 440 | return NULL; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 443 | /* Does this chain exist? */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 444 | int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 445 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 446 | return find_label(chain, handle) != NULL; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* Returns the position of the final (ie. unconditional) element. */ |
| 450 | static unsigned int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 451 | get_chain_end(const TC_HANDLE_T handle, unsigned int start) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 452 | { |
| 453 | unsigned int last_off, off; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 454 | STRUCT_ENTRY *e; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 455 | |
| 456 | last_off = start; |
| 457 | e = get_entry(handle, start); |
| 458 | |
| 459 | /* Terminate when we meet a error label or a hook entry. */ |
| 460 | for (off = start + e->next_offset; |
| 461 | off < handle->entries.size; |
| 462 | last_off = off, off += e->next_offset) { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 463 | STRUCT_ENTRY_TARGET *t; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 464 | unsigned int i; |
| 465 | |
| 466 | e = get_entry(handle, off); |
| 467 | |
| 468 | /* We hit an entry point. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 469 | for (i = 0; i < NUMHOOKS; i++) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 470 | if ((handle->info.valid_hooks & (1 << i)) |
| 471 | && off == handle->info.hook_entry[i]) |
| 472 | return last_off; |
| 473 | } |
| 474 | |
| 475 | /* We hit a user chain label */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 476 | t = GET_TARGET(e); |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 477 | if (strcmp(t->u.user.name, ERROR_TARGET) == 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 478 | return last_off; |
| 479 | } |
| 480 | /* SHOULD NEVER HAPPEN */ |
| 481 | fprintf(stderr, "ERROR: Off end (%u) of chain from %u!\n", |
| 482 | handle->entries.size, off); |
| 483 | abort(); |
| 484 | } |
| 485 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 486 | /* Iterator functions to run through the chains. */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 487 | const char * |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 488 | TC_FIRST_CHAIN(TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 489 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 490 | if ((*handle)->cache_chain_heads == NULL |
| 491 | && !populate_cache(*handle)) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 492 | return NULL; |
| 493 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 494 | (*handle)->cache_chain_iteration |
| 495 | = &(*handle)->cache_chain_heads[0]; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 496 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 497 | return (*handle)->cache_chain_iteration->name; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 500 | /* Iterator functions to run through the chains. Returns NULL at end. */ |
| 501 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 502 | TC_NEXT_CHAIN(TC_HANDLE_T *handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 503 | { |
| 504 | (*handle)->cache_chain_iteration++; |
| 505 | |
| 506 | if ((*handle)->cache_chain_iteration - (*handle)->cache_chain_heads |
| Tomáš Lejdar | 23a6b45 | 2003-04-30 15:57:01 +0000 | [diff] [blame] | 507 | == (*handle)->cache_num_chains) { |
| 508 | free((*handle)->cache_chain_heads); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 509 | return NULL; |
| Tomáš Lejdar | 23a6b45 | 2003-04-30 15:57:01 +0000 | [diff] [blame] | 510 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 511 | |
| 512 | return (*handle)->cache_chain_iteration->name; |
| 513 | } |
| 514 | |
| 515 | /* Get first rule in the given chain: NULL for empty chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 516 | const STRUCT_ENTRY * |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 517 | TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 518 | { |
| 519 | struct chain_cache *c; |
| 520 | |
| 521 | c = find_label(chain, *handle); |
| 522 | if (!c) { |
| 523 | errno = ENOENT; |
| 524 | return NULL; |
| 525 | } |
| 526 | |
| 527 | /* Empty chain: single return/policy rule */ |
| 528 | if (c->start == c->end) |
| 529 | return NULL; |
| 530 | |
| 531 | (*handle)->cache_rule_end = c->end; |
| 532 | return c->start; |
| 533 | } |
| 534 | |
| 535 | /* Returns NULL when rules run out. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 536 | const STRUCT_ENTRY * |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 537 | TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle) |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 538 | { |
| 539 | if ((void *)prev + prev->next_offset |
| 540 | == (void *)(*handle)->cache_rule_end) |
| 541 | return NULL; |
| 542 | |
| 543 | return (void *)prev + prev->next_offset; |
| 544 | } |
| 545 | |
| 546 | #if 0 |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 547 | /* How many rules in this chain? */ |
| 548 | unsigned int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 549 | TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 550 | { |
| 551 | unsigned int off = 0; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 552 | STRUCT_ENTRY *start, *end; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 553 | |
| 554 | CHECK(*handle); |
| 555 | if (!find_label(&off, chain, *handle)) { |
| 556 | errno = ENOENT; |
| 557 | return (unsigned int)-1; |
| 558 | } |
| 559 | |
| 560 | start = get_entry(*handle, off); |
| 561 | end = get_entry(*handle, get_chain_end(*handle, off)); |
| 562 | |
| 563 | return entry2index(*handle, end) - entry2index(*handle, start); |
| 564 | } |
| 565 | |
| 566 | /* Get n'th rule in this chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 567 | const STRUCT_ENTRY *TC_GET_RULE(const char *chain, |
| 568 | unsigned int n, |
| 569 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 570 | { |
| 571 | unsigned int pos = 0, chainindex; |
| 572 | |
| 573 | CHECK(*handle); |
| 574 | if (!find_label(&pos, chain, *handle)) { |
| 575 | errno = ENOENT; |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | chainindex = entry2index(*handle, get_entry(*handle, pos)); |
| 580 | |
| 581 | return index2entry(*handle, chainindex + n); |
| 582 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 583 | #endif |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 584 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 585 | static const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 586 | target_name(TC_HANDLE_T handle, const STRUCT_ENTRY *ce) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 587 | { |
| 588 | int spos; |
| 589 | unsigned int labelidx; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 590 | STRUCT_ENTRY *jumpto; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 591 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 592 | /* To avoid const warnings */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 593 | STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 594 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 595 | if (strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET) != 0) |
| 596 | return GET_TARGET(e)->u.user.name; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 597 | |
| 598 | /* Standard target: evaluate */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 599 | spos = *(int *)GET_TARGET(e)->data; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 600 | if (spos < 0) { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 601 | if (spos == RETURN) |
| 602 | return LABEL_RETURN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 603 | else if (spos == -NF_ACCEPT-1) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 604 | return LABEL_ACCEPT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 605 | else if (spos == -NF_DROP-1) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 606 | return LABEL_DROP; |
| James Morris | 2f4e5d9 | 2000-03-24 02:13:51 +0000 | [diff] [blame] | 607 | else if (spos == -NF_QUEUE-1) |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 608 | return LABEL_QUEUE; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 609 | |
| 610 | fprintf(stderr, "ERROR: off %lu/%u not a valid target (%i)\n", |
| 611 | entry2offset(handle, e), handle->entries.size, |
| 612 | spos); |
| 613 | abort(); |
| 614 | } |
| 615 | |
| 616 | jumpto = get_entry(handle, spos); |
| 617 | |
| 618 | /* Fall through rule */ |
| 619 | if (jumpto == (void *)e + e->next_offset) |
| 620 | return ""; |
| 621 | |
| 622 | /* Must point to head of a chain: ie. after error rule */ |
| 623 | labelidx = entry2index(handle, jumpto) - 1; |
| 624 | return get_errorlabel(handle, index2offset(handle, labelidx)); |
| 625 | } |
| 626 | |
| 627 | /* Returns a pointer to the target name of this position. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 628 | const char *TC_GET_TARGET(const STRUCT_ENTRY *e, |
| 629 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 630 | { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 631 | return target_name(*handle, e); |
| 632 | } |
| 633 | |
| 634 | /* Is this a built-in chain? Actually returns hook + 1. */ |
| 635 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 636 | TC_BUILTIN(const char *chain, const TC_HANDLE_T handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 637 | { |
| 638 | unsigned int i; |
| 639 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 640 | for (i = 0; i < NUMHOOKS; i++) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 641 | if ((handle->info.valid_hooks & (1 << i)) |
| 642 | && handle->hooknames[i] |
| 643 | && strcmp(handle->hooknames[i], chain) == 0) |
| 644 | return i+1; |
| 645 | } |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /* Get the policy of a given built-in chain */ |
| 650 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 651 | TC_GET_POLICY(const char *chain, |
| 652 | STRUCT_COUNTERS *counters, |
| 653 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 654 | { |
| 655 | unsigned int start; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 656 | STRUCT_ENTRY *e; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 657 | int hook; |
| 658 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 659 | hook = TC_BUILTIN(chain, *handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 660 | if (hook != 0) |
| 661 | start = (*handle)->info.hook_entry[hook-1]; |
| 662 | else |
| 663 | return NULL; |
| 664 | |
| 665 | e = get_entry(*handle, get_chain_end(*handle, start)); |
| 666 | *counters = e->counters; |
| 667 | |
| 668 | return target_name(*handle, e); |
| 669 | } |
| 670 | |
| 671 | static int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 672 | correct_verdict(STRUCT_ENTRY *e, |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 673 | char *base, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 674 | unsigned int offset, int delta_offset) |
| 675 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 676 | STRUCT_STANDARD_TARGET *t = (void *)GET_TARGET(e); |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 677 | unsigned int curr = (char *)e - base; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 678 | |
| 679 | /* Trap: insert of fall-through rule. Don't change fall-through |
| 680 | verdict to jump-over-next-rule. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 681 | if (strcmp(t->target.u.user.name, STANDARD_TARGET) == 0 |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 682 | && t->verdict > (int)offset |
| 683 | && !(curr == offset && |
| 684 | t->verdict == curr + e->next_offset)) { |
| 685 | t->verdict += delta_offset; |
| 686 | } |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | /* Adjusts standard verdict jump positions after an insertion/deletion. */ |
| 692 | static int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 693 | set_verdict(unsigned int offset, int delta_offset, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 694 | { |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 695 | ENTRY_ITERATE((*handle)->entries.entrytable, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 696 | (*handle)->entries.size, |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 697 | correct_verdict, (char *)(*handle)->entries.entrytable, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 698 | offset, delta_offset); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 699 | |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 700 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 701 | return 1; |
| 702 | } |
| 703 | |
| 704 | /* If prepend is set, then we are prepending to a chain: if the |
| 705 | * insertion position is an entry point, keep the entry point. */ |
| 706 | static int |
| 707 | insert_rules(unsigned int num_rules, unsigned int rules_size, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 708 | const STRUCT_ENTRY *insert, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 709 | unsigned int offset, unsigned int num_rules_offset, |
| 710 | int prepend, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 711 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 712 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 713 | TC_HANDLE_T newh; |
| 714 | STRUCT_GETINFO newinfo; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 715 | unsigned int i; |
| 716 | |
| 717 | if (offset >= (*handle)->entries.size) { |
| 718 | errno = EINVAL; |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | newinfo = (*handle)->info; |
| 723 | |
| 724 | /* Fix up entry points. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 725 | for (i = 0; i < NUMHOOKS; i++) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 726 | /* Entry points to START of chain, so keep same if |
| 727 | inserting on at that point. */ |
| 728 | if ((*handle)->info.hook_entry[i] > offset) |
| 729 | newinfo.hook_entry[i] += rules_size; |
| 730 | |
| 731 | /* Underflow always points to END of chain (policy), |
| 732 | so if something is inserted at same point, it |
| 733 | should be advanced. */ |
| 734 | if ((*handle)->info.underflow[i] >= offset) |
| 735 | newinfo.underflow[i] += rules_size; |
| 736 | } |
| 737 | |
| 738 | newh = alloc_handle((*handle)->info.name, |
| Rusty Russell | 3c7a6c4 | 2000-09-19 07:01:46 +0000 | [diff] [blame] | 739 | (*handle)->entries.size + rules_size, |
| Harald Welte | 1de8046 | 2000-10-30 12:00:27 +0000 | [diff] [blame] | 740 | (*handle)->new_number + num_rules); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 741 | if (!newh) |
| 742 | return 0; |
| 743 | newh->info = newinfo; |
| 744 | |
| 745 | /* Copy pre... */ |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 746 | memcpy(newh->entries.entrytable, (*handle)->entries.entrytable,offset); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 747 | /* ... Insert new ... */ |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 748 | memcpy((char *)newh->entries.entrytable + offset, insert, rules_size); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 749 | /* ... copy post */ |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 750 | memcpy((char *)newh->entries.entrytable + offset + rules_size, |
| 751 | (char *)(*handle)->entries.entrytable + offset, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 752 | (*handle)->entries.size - offset); |
| 753 | |
| 754 | /* Move counter map. */ |
| 755 | /* Copy pre... */ |
| 756 | memcpy(newh->counter_map, (*handle)->counter_map, |
| 757 | sizeof(struct counter_map) * num_rules_offset); |
| 758 | /* ... copy post */ |
| 759 | memcpy(newh->counter_map + num_rules_offset + num_rules, |
| 760 | (*handle)->counter_map + num_rules_offset, |
| 761 | sizeof(struct counter_map) * ((*handle)->new_number |
| 762 | - num_rules_offset)); |
| 763 | /* Set intermediates to no counter copy */ |
| 764 | for (i = 0; i < num_rules; i++) |
| 765 | newh->counter_map[num_rules_offset+i] |
| Harald Welte | e007294 | 2001-01-23 22:55:04 +0000 | [diff] [blame] | 766 | = ((struct counter_map){ COUNTER_MAP_SET, 0 }); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 767 | |
| 768 | newh->new_number = (*handle)->new_number + num_rules; |
| 769 | newh->entries.size = (*handle)->entries.size + rules_size; |
| 770 | newh->hooknames = (*handle)->hooknames; |
| 771 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 772 | if ((*handle)->cache_chain_heads) |
| 773 | free((*handle)->cache_chain_heads); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 774 | free(*handle); |
| 775 | *handle = newh; |
| 776 | |
| 777 | return set_verdict(offset, rules_size, handle); |
| 778 | } |
| 779 | |
| 780 | static int |
| 781 | delete_rules(unsigned int num_rules, unsigned int rules_size, |
| 782 | unsigned int offset, unsigned int num_rules_offset, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 783 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 784 | { |
| 785 | unsigned int i; |
| 786 | |
| 787 | if (offset + rules_size > (*handle)->entries.size) { |
| 788 | errno = EINVAL; |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | /* Fix up entry points. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 793 | for (i = 0; i < NUMHOOKS; i++) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 794 | /* In practice, we never delete up to a hook entry, |
| 795 | since the built-in chains are always first, |
| 796 | so these two are never equal */ |
| 797 | if ((*handle)->info.hook_entry[i] >= offset + rules_size) |
| 798 | (*handle)->info.hook_entry[i] -= rules_size; |
| 799 | else if ((*handle)->info.hook_entry[i] > offset) { |
| 800 | fprintf(stderr, "ERROR: Deleting entry %u %u %u\n", |
| 801 | i, (*handle)->info.hook_entry[i], offset); |
| 802 | abort(); |
| 803 | } |
| 804 | |
| 805 | /* Underflow points to policy (terminal) rule in |
| 806 | built-in, so sequality is valid here (when deleting |
| 807 | the last rule). */ |
| 808 | if ((*handle)->info.underflow[i] >= offset + rules_size) |
| 809 | (*handle)->info.underflow[i] -= rules_size; |
| 810 | else if ((*handle)->info.underflow[i] > offset) { |
| 811 | fprintf(stderr, "ERROR: Deleting uflow %u %u %u\n", |
| 812 | i, (*handle)->info.underflow[i], offset); |
| 813 | abort(); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /* Move the rules down. */ |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 818 | memmove((char *)(*handle)->entries.entrytable + offset, |
| 819 | (char *)(*handle)->entries.entrytable + offset + rules_size, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 820 | (*handle)->entries.size - (offset + rules_size)); |
| 821 | |
| 822 | /* Move the counter map down. */ |
| 823 | memmove(&(*handle)->counter_map[num_rules_offset], |
| 824 | &(*handle)->counter_map[num_rules_offset + num_rules], |
| 825 | sizeof(struct counter_map) |
| 826 | * ((*handle)->new_number - (num_rules + num_rules_offset))); |
| 827 | |
| 828 | /* Fix numbers */ |
| 829 | (*handle)->new_number -= num_rules; |
| 830 | (*handle)->entries.size -= rules_size; |
| 831 | |
| 832 | return set_verdict(offset, -(int)rules_size, handle); |
| 833 | } |
| 834 | |
| 835 | static int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 836 | standard_map(STRUCT_ENTRY *e, int verdict) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 837 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 838 | STRUCT_STANDARD_TARGET *t; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 839 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 840 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 841 | |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 842 | if (t->target.u.target_size |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 843 | != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 844 | errno = EINVAL; |
| 845 | return 0; |
| 846 | } |
| 847 | /* memset for memcmp convenience on delete/replace */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 848 | memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN); |
| 849 | strcpy(t->target.u.user.name, STANDARD_TARGET); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 850 | t->verdict = verdict; |
| 851 | |
| 852 | return 1; |
| 853 | } |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 854 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 855 | static int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 856 | map_target(const TC_HANDLE_T handle, |
| 857 | STRUCT_ENTRY *e, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 858 | unsigned int offset, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 859 | STRUCT_ENTRY_TARGET *old) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 860 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 861 | STRUCT_ENTRY_TARGET *t = GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 862 | |
| 863 | /* Save old target (except data, which we don't change, except for |
| 864 | standard case, where we don't care). */ |
| 865 | *old = *t; |
| 866 | |
| 867 | /* Maybe it's empty (=> fall through) */ |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 868 | if (strcmp(t->u.user.name, "") == 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 869 | return standard_map(e, offset + e->next_offset); |
| 870 | /* Maybe it's a standard target name... */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 871 | else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 872 | return standard_map(e, -NF_ACCEPT - 1); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 873 | else if (strcmp(t->u.user.name, LABEL_DROP) == 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 874 | return standard_map(e, -NF_DROP - 1); |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 875 | else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 876 | return standard_map(e, -NF_QUEUE - 1); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 877 | else if (strcmp(t->u.user.name, LABEL_RETURN) == 0) |
| 878 | return standard_map(e, RETURN); |
| 879 | else if (TC_BUILTIN(t->u.user.name, handle)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 880 | /* Can't jump to builtins. */ |
| 881 | errno = EINVAL; |
| 882 | return 0; |
| 883 | } else { |
| 884 | /* Maybe it's an existing chain name. */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 885 | struct chain_cache *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 886 | |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 887 | c = find_label(t->u.user.name, handle); |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 888 | if (c) |
| 889 | return standard_map(e, entry2offset(handle, c->start)); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* Must be a module? If not, kernel will reject... */ |
| 893 | /* memset to all 0 for your memcmp convenience. */ |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 894 | memset(t->u.user.name + strlen(t->u.user.name), |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 895 | 0, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 896 | FUNCTION_MAXNAMELEN - strlen(t->u.user.name)); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 897 | return 1; |
| 898 | } |
| 899 | |
| 900 | static void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 901 | unmap_target(STRUCT_ENTRY *e, STRUCT_ENTRY_TARGET *old) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 902 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 903 | STRUCT_ENTRY_TARGET *t = GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 904 | |
| 905 | /* Save old target (except data, which we don't change, except for |
| 906 | standard case, where we don't care). */ |
| 907 | *t = *old; |
| 908 | } |
| 909 | |
| 910 | /* Insert the entry `fw' in chain `chain' into position `rulenum'. */ |
| 911 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 912 | TC_INSERT_ENTRY(const IPT_CHAINLABEL chain, |
| 913 | const STRUCT_ENTRY *e, |
| 914 | unsigned int rulenum, |
| 915 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 916 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 917 | unsigned int chainindex, offset; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 918 | STRUCT_ENTRY_TARGET old; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 919 | struct chain_cache *c; |
| Rusty Russell | 14a1c91 | 2000-08-26 04:41:10 +0000 | [diff] [blame] | 920 | STRUCT_ENTRY *tmp; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 921 | int ret; |
| 922 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 923 | iptc_fn = TC_INSERT_ENTRY; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 924 | if (!(c = find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 925 | errno = ENOENT; |
| 926 | return 0; |
| 927 | } |
| 928 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 929 | chainindex = entry2index(*handle, c->start); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 930 | |
| Rusty Russell | 14a1c91 | 2000-08-26 04:41:10 +0000 | [diff] [blame] | 931 | tmp = index2entry(*handle, chainindex + rulenum); |
| 932 | if (!tmp || tmp > c->end) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 933 | errno = E2BIG; |
| 934 | return 0; |
| 935 | } |
| 936 | offset = index2offset(*handle, chainindex + rulenum); |
| 937 | |
| 938 | /* Mapping target actually alters entry, but that's |
| 939 | transparent to the caller. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 940 | if (!map_target(*handle, (STRUCT_ENTRY *)e, offset, &old)) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 941 | return 0; |
| 942 | |
| 943 | ret = insert_rules(1, e->next_offset, e, offset, |
| 944 | chainindex + rulenum, rulenum == 0, handle); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 945 | unmap_target((STRUCT_ENTRY *)e, &old); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | /* Atomically replace rule `rulenum' in `chain' with `fw'. */ |
| 950 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 951 | TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain, |
| 952 | const STRUCT_ENTRY *e, |
| 953 | unsigned int rulenum, |
| 954 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 955 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 956 | unsigned int chainindex, offset; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 957 | STRUCT_ENTRY_TARGET old; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 958 | struct chain_cache *c; |
| Rusty Russell | 14a1c91 | 2000-08-26 04:41:10 +0000 | [diff] [blame] | 959 | STRUCT_ENTRY *tmp; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 960 | int ret; |
| 961 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 962 | iptc_fn = TC_REPLACE_ENTRY; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 963 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 964 | if (!(c = find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 965 | errno = ENOENT; |
| 966 | return 0; |
| 967 | } |
| 968 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 969 | chainindex = entry2index(*handle, c->start); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 970 | |
| Rusty Russell | 14a1c91 | 2000-08-26 04:41:10 +0000 | [diff] [blame] | 971 | tmp = index2entry(*handle, chainindex + rulenum); |
| 972 | if (!tmp || tmp >= c->end) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 973 | errno = E2BIG; |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | offset = index2offset(*handle, chainindex + rulenum); |
| 978 | /* Replace = delete and insert. */ |
| 979 | if (!delete_rules(1, get_entry(*handle, offset)->next_offset, |
| 980 | offset, chainindex + rulenum, handle)) |
| 981 | return 0; |
| 982 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 983 | if (!map_target(*handle, (STRUCT_ENTRY *)e, offset, &old)) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 984 | return 0; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 985 | |
| 986 | ret = insert_rules(1, e->next_offset, e, offset, |
| 987 | chainindex + rulenum, 1, handle); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 988 | unmap_target((STRUCT_ENTRY *)e, &old); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 989 | return ret; |
| 990 | } |
| 991 | |
| 992 | /* Append entry `fw' to chain `chain'. Equivalent to insert with |
| 993 | rulenum = length of chain. */ |
| 994 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 995 | TC_APPEND_ENTRY(const IPT_CHAINLABEL chain, |
| 996 | const STRUCT_ENTRY *e, |
| 997 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 998 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 999 | struct chain_cache *c; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1000 | STRUCT_ENTRY_TARGET old; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1001 | int ret; |
| 1002 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1003 | iptc_fn = TC_APPEND_ENTRY; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1004 | if (!(c = find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1005 | errno = ENOENT; |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1009 | if (!map_target(*handle, (STRUCT_ENTRY *)e, |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1010 | entry2offset(*handle, c->end), &old)) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1011 | return 0; |
| 1012 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1013 | ret = insert_rules(1, e->next_offset, e, |
| 1014 | entry2offset(*handle, c->end), |
| 1015 | entry2index(*handle, c->end), |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1016 | 0, handle); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1017 | unmap_target((STRUCT_ENTRY *)e, &old); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1018 | return ret; |
| 1019 | } |
| 1020 | |
| 1021 | static inline int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1022 | match_different(const STRUCT_ENTRY_MATCH *a, |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1023 | const unsigned char *a_elems, |
| 1024 | const unsigned char *b_elems, |
| 1025 | unsigned char **maskptr) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1026 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1027 | const STRUCT_ENTRY_MATCH *b; |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1028 | unsigned int i; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1029 | |
| 1030 | /* Offset of b is the same as a. */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1031 | b = (void *)b_elems + ((unsigned char *)a - a_elems); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1032 | |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1033 | if (a->u.match_size != b->u.match_size) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1034 | return 1; |
| 1035 | |
| Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 1036 | if (strcmp(a->u.user.name, b->u.user.name) != 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1037 | return 1; |
| 1038 | |
| Rusty Russell | 73ef09b | 2000-07-03 10:24:04 +0000 | [diff] [blame] | 1039 | *maskptr += ALIGN(sizeof(*a)); |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1040 | |
| Rusty Russell | 73ef09b | 2000-07-03 10:24:04 +0000 | [diff] [blame] | 1041 | for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++) |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1042 | if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0) |
| Rusty Russell | 90e712a | 2000-03-29 04:19:26 +0000 | [diff] [blame] | 1043 | return 1; |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1044 | *maskptr += i; |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | static inline int |
| 1049 | target_different(const unsigned char *a_targdata, |
| 1050 | const unsigned char *b_targdata, |
| 1051 | unsigned int tdatasize, |
| 1052 | const unsigned char *mask) |
| 1053 | { |
| 1054 | unsigned int i; |
| 1055 | for (i = 0; i < tdatasize; i++) |
| 1056 | if (((a_targdata[i] ^ b_targdata[i]) & mask[i]) != 0) |
| 1057 | return 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1062 | static int |
| 1063 | is_same(const STRUCT_ENTRY *a, |
| 1064 | const STRUCT_ENTRY *b, |
| 1065 | unsigned char *matchmask); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1066 | |
| 1067 | /* Delete the first rule in `chain' which matches `fw'. */ |
| 1068 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1069 | TC_DELETE_ENTRY(const IPT_CHAINLABEL chain, |
| 1070 | const STRUCT_ENTRY *origfw, |
| 1071 | unsigned char *matchmask, |
| 1072 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1073 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1074 | unsigned int offset; |
| 1075 | struct chain_cache *c; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1076 | STRUCT_ENTRY *e, *fw; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1077 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1078 | iptc_fn = TC_DELETE_ENTRY; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1079 | if (!(c = find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1080 | errno = ENOENT; |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | fw = malloc(origfw->next_offset); |
| 1085 | if (fw == NULL) { |
| 1086 | errno = ENOMEM; |
| 1087 | return 0; |
| 1088 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1089 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1090 | for (offset = entry2offset(*handle, c->start); |
| 1091 | offset < entry2offset(*handle, c->end); |
| 1092 | offset += e->next_offset) { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1093 | STRUCT_ENTRY_TARGET discard; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1094 | |
| 1095 | memcpy(fw, origfw, origfw->next_offset); |
| 1096 | |
| 1097 | /* FIXME: handle this in is_same --RR */ |
| 1098 | if (!map_target(*handle, fw, offset, &discard)) { |
| 1099 | free(fw); |
| 1100 | return 0; |
| 1101 | } |
| 1102 | e = get_entry(*handle, offset); |
| 1103 | |
| 1104 | #if 0 |
| 1105 | printf("Deleting:\n"); |
| 1106 | dump_entry(newe); |
| 1107 | #endif |
| Rusty Russell | edf14cf | 2000-04-19 11:26:44 +0000 | [diff] [blame] | 1108 | if (is_same(e, fw, matchmask)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1109 | int ret; |
| 1110 | ret = delete_rules(1, e->next_offset, |
| 1111 | offset, entry2index(*handle, e), |
| 1112 | handle); |
| 1113 | free(fw); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1114 | return ret; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | free(fw); |
| 1119 | errno = ENOENT; |
| 1120 | return 0; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1121 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1122 | |
| 1123 | /* Delete the rule in position `rulenum' in `chain'. */ |
| 1124 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1125 | TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain, |
| 1126 | unsigned int rulenum, |
| 1127 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1128 | { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1129 | unsigned int index; |
| 1130 | int ret; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1131 | STRUCT_ENTRY *e; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1132 | struct chain_cache *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1133 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1134 | iptc_fn = TC_DELETE_NUM_ENTRY; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1135 | if (!(c = find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1136 | errno = ENOENT; |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1140 | index = entry2index(*handle, c->start) + rulenum; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1141 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1142 | if (index >= entry2index(*handle, c->end)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1143 | errno = E2BIG; |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | e = index2entry(*handle, index); |
| 1148 | if (e == NULL) { |
| 1149 | errno = EINVAL; |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | ret = delete_rules(1, e->next_offset, entry2offset(*handle, e), |
| 1154 | index, handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1155 | return ret; |
| 1156 | } |
| 1157 | |
| 1158 | /* Check the packet `fw' on chain `chain'. Returns the verdict, or |
| 1159 | NULL and sets errno. */ |
| 1160 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1161 | TC_CHECK_PACKET(const IPT_CHAINLABEL chain, |
| 1162 | STRUCT_ENTRY *entry, |
| 1163 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1164 | { |
| 1165 | errno = ENOSYS; |
| 1166 | return NULL; |
| 1167 | } |
| 1168 | |
| 1169 | /* Flushes the entries in the given chain (ie. empties chain). */ |
| 1170 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1171 | TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1172 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1173 | unsigned int startindex, endindex; |
| 1174 | struct chain_cache *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1175 | int ret; |
| 1176 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1177 | iptc_fn = TC_FLUSH_ENTRIES; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1178 | if (!(c = find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1179 | errno = ENOENT; |
| 1180 | return 0; |
| 1181 | } |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1182 | startindex = entry2index(*handle, c->start); |
| 1183 | endindex = entry2index(*handle, c->end); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1184 | |
| 1185 | ret = delete_rules(endindex - startindex, |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1186 | (char *)c->end - (char *)c->start, |
| 1187 | entry2offset(*handle, c->start), startindex, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1188 | handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1189 | return ret; |
| 1190 | } |
| 1191 | |
| 1192 | /* Zeroes the counters in a chain. */ |
| 1193 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1194 | TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1195 | { |
| 1196 | unsigned int i, end; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1197 | struct chain_cache *c; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1198 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1199 | if (!(c = find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1200 | errno = ENOENT; |
| 1201 | return 0; |
| 1202 | } |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1203 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1204 | i = entry2index(*handle, c->start); |
| 1205 | end = entry2index(*handle, c->end); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1206 | |
| 1207 | for (; i <= end; i++) { |
| 1208 | if ((*handle)->counter_map[i].maptype ==COUNTER_MAP_NORMAL_MAP) |
| 1209 | (*handle)->counter_map[i].maptype = COUNTER_MAP_ZEROED; |
| 1210 | } |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 1211 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1212 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1213 | return 1; |
| 1214 | } |
| 1215 | |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1216 | STRUCT_COUNTERS * |
| 1217 | TC_READ_COUNTER(const IPT_CHAINLABEL chain, |
| 1218 | unsigned int rulenum, |
| 1219 | TC_HANDLE_T *handle) |
| 1220 | { |
| 1221 | STRUCT_ENTRY *e; |
| 1222 | struct chain_cache *c; |
| 1223 | unsigned int chainindex, end; |
| 1224 | |
| 1225 | iptc_fn = TC_READ_COUNTER; |
| 1226 | CHECK(*handle); |
| 1227 | |
| 1228 | if (!(c = find_label(chain, *handle))) { |
| 1229 | errno = ENOENT; |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | |
| 1233 | chainindex = entry2index(*handle, c->start); |
| 1234 | end = entry2index(*handle, c->end); |
| 1235 | |
| 1236 | if (chainindex + rulenum > end) { |
| 1237 | errno = E2BIG; |
| 1238 | return NULL; |
| 1239 | } |
| 1240 | |
| 1241 | e = index2entry(*handle, chainindex + rulenum); |
| 1242 | |
| 1243 | return &e->counters; |
| 1244 | } |
| 1245 | |
| 1246 | int |
| 1247 | TC_ZERO_COUNTER(const IPT_CHAINLABEL chain, |
| 1248 | unsigned int rulenum, |
| 1249 | TC_HANDLE_T *handle) |
| 1250 | { |
| 1251 | STRUCT_ENTRY *e; |
| 1252 | struct chain_cache *c; |
| 1253 | unsigned int chainindex, end; |
| 1254 | |
| 1255 | iptc_fn = TC_ZERO_COUNTER; |
| 1256 | CHECK(*handle); |
| 1257 | |
| 1258 | if (!(c = find_label(chain, *handle))) { |
| 1259 | errno = ENOENT; |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | chainindex = entry2index(*handle, c->start); |
| 1264 | end = entry2index(*handle, c->end); |
| 1265 | |
| 1266 | if (chainindex + rulenum > end) { |
| 1267 | errno = E2BIG; |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | e = index2entry(*handle, chainindex + rulenum); |
| 1272 | |
| 1273 | if ((*handle)->counter_map[chainindex + rulenum].maptype |
| 1274 | == COUNTER_MAP_NORMAL_MAP) { |
| 1275 | (*handle)->counter_map[chainindex + rulenum].maptype |
| 1276 | = COUNTER_MAP_ZEROED; |
| 1277 | } |
| 1278 | |
| 1279 | set_changed(*handle); |
| 1280 | |
| 1281 | return 1; |
| 1282 | } |
| 1283 | |
| 1284 | int |
| 1285 | TC_SET_COUNTER(const IPT_CHAINLABEL chain, |
| 1286 | unsigned int rulenum, |
| 1287 | STRUCT_COUNTERS *counters, |
| 1288 | TC_HANDLE_T *handle) |
| 1289 | { |
| 1290 | STRUCT_ENTRY *e; |
| 1291 | struct chain_cache *c; |
| 1292 | unsigned int chainindex, end; |
| 1293 | |
| 1294 | iptc_fn = TC_SET_COUNTER; |
| 1295 | CHECK(*handle); |
| 1296 | |
| 1297 | if (!(c = find_label(chain, *handle))) { |
| 1298 | errno = ENOENT; |
| 1299 | return 0; |
| 1300 | } |
| 1301 | |
| 1302 | chainindex = entry2index(*handle, c->start); |
| 1303 | end = entry2index(*handle, c->end); |
| 1304 | |
| 1305 | if (chainindex + rulenum > end) { |
| 1306 | errno = E2BIG; |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | e = index2entry(*handle, chainindex + rulenum); |
| 1311 | |
| 1312 | (*handle)->counter_map[chainindex + rulenum].maptype |
| 1313 | = COUNTER_MAP_SET; |
| 1314 | |
| 1315 | memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS)); |
| 1316 | |
| 1317 | set_changed(*handle); |
| 1318 | |
| 1319 | return 1; |
| 1320 | } |
| 1321 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1322 | /* Creates a new chain. */ |
| 1323 | /* To create a chain, create two rules: error node and unconditional |
| 1324 | * return. */ |
| 1325 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1326 | TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1327 | { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1328 | int ret; |
| 1329 | struct { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1330 | STRUCT_ENTRY head; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1331 | struct ipt_error_target name; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1332 | STRUCT_ENTRY ret; |
| 1333 | STRUCT_STANDARD_TARGET target; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1334 | } newc; |
| 1335 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1336 | iptc_fn = TC_CREATE_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1337 | |
| 1338 | /* find_label doesn't cover built-in targets: DROP, ACCEPT, |
| 1339 | QUEUE, RETURN. */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1340 | if (find_label(chain, *handle) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1341 | || strcmp(chain, LABEL_DROP) == 0 |
| 1342 | || strcmp(chain, LABEL_ACCEPT) == 0 |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1343 | || strcmp(chain, LABEL_QUEUE) == 0 |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1344 | || strcmp(chain, LABEL_RETURN) == 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1345 | errno = EEXIST; |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1349 | if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1350 | errno = EINVAL; |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | memset(&newc, 0, sizeof(newc)); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1355 | newc.head.target_offset = sizeof(STRUCT_ENTRY); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1356 | newc.head.next_offset |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1357 | = sizeof(STRUCT_ENTRY) |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1358 | + ALIGN(sizeof(struct ipt_error_target)); |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1359 | strcpy(newc.name.t.u.user.name, ERROR_TARGET); |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1360 | newc.name.t.u.target_size = ALIGN(sizeof(struct ipt_error_target)); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1361 | strcpy(newc.name.error, chain); |
| 1362 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1363 | newc.ret.target_offset = sizeof(STRUCT_ENTRY); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1364 | newc.ret.next_offset |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1365 | = sizeof(STRUCT_ENTRY) |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1366 | + ALIGN(sizeof(STRUCT_STANDARD_TARGET)); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1367 | strcpy(newc.target.target.u.user.name, STANDARD_TARGET); |
| Rusty Russell | 67088e7 | 2000-05-10 01:18:57 +0000 | [diff] [blame] | 1368 | newc.target.target.u.target_size |
| Philip Blundell | 8c70090 | 2000-05-15 02:17:52 +0000 | [diff] [blame] | 1369 | = ALIGN(sizeof(STRUCT_STANDARD_TARGET)); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1370 | newc.target.verdict = RETURN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1371 | |
| 1372 | /* Add just before terminal entry */ |
| 1373 | ret = insert_rules(2, sizeof(newc), &newc.head, |
| 1374 | index2offset(*handle, (*handle)->new_number - 1), |
| 1375 | (*handle)->new_number - 1, |
| 1376 | 0, handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1377 | return ret; |
| 1378 | } |
| 1379 | |
| 1380 | static int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1381 | count_ref(STRUCT_ENTRY *e, unsigned int offset, unsigned int *ref) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1382 | { |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1383 | STRUCT_STANDARD_TARGET *t; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1384 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1385 | if (strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET) == 0) { |
| 1386 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1387 | |
| 1388 | if (t->verdict == offset) |
| 1389 | (*ref)++; |
| 1390 | } |
| 1391 | |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | /* Get the number of references to this chain. */ |
| 1396 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1397 | TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain, |
| 1398 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1399 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1400 | struct chain_cache *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1401 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1402 | if (!(c = find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1403 | errno = ENOENT; |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | *ref = 0; |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 1408 | ENTRY_ITERATE((*handle)->entries.entrytable, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1409 | (*handle)->entries.size, |
| 1410 | count_ref, entry2offset(*handle, c->start), ref); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1411 | return 1; |
| 1412 | } |
| 1413 | |
| 1414 | /* Deletes a chain. */ |
| 1415 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1416 | TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1417 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1418 | unsigned int labelidx, labeloff; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1419 | unsigned int references; |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1420 | struct chain_cache *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1421 | int ret; |
| 1422 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1423 | if (!TC_GET_REFERENCES(&references, chain, handle)) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1424 | return 0; |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1425 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1426 | iptc_fn = TC_DELETE_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1427 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1428 | if (TC_BUILTIN(chain, *handle)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1429 | errno = EINVAL; |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | if (references > 0) { |
| 1434 | errno = EMLINK; |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1438 | if (!(c = find_label(chain, *handle))) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1439 | errno = ENOENT; |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1443 | if ((void *)c->start != c->end) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1444 | errno = ENOTEMPTY; |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | /* Need label index: preceeds chain start */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1449 | labelidx = entry2index(*handle, c->start) - 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1450 | labeloff = index2offset(*handle, labelidx); |
| 1451 | |
| 1452 | ret = delete_rules(2, |
| 1453 | get_entry(*handle, labeloff)->next_offset |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1454 | + c->start->next_offset, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1455 | labeloff, labelidx, handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1456 | return ret; |
| 1457 | } |
| 1458 | |
| 1459 | /* Renames a chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1460 | int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname, |
| 1461 | const IPT_CHAINLABEL newname, |
| 1462 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1463 | { |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1464 | unsigned int labeloff, labelidx; |
| 1465 | struct chain_cache *c; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1466 | struct ipt_error_target *t; |
| 1467 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1468 | iptc_fn = TC_RENAME_CHAIN; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1469 | |
| Harald Welte | 1de8046 | 2000-10-30 12:00:27 +0000 | [diff] [blame] | 1470 | /* find_label doesn't cover built-in targets: DROP, ACCEPT, |
| 1471 | QUEUE, RETURN. */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1472 | if (find_label(newname, *handle) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1473 | || strcmp(newname, LABEL_DROP) == 0 |
| 1474 | || strcmp(newname, LABEL_ACCEPT) == 0 |
| Harald Welte | 1de8046 | 2000-10-30 12:00:27 +0000 | [diff] [blame] | 1475 | || strcmp(newname, LABEL_QUEUE) == 0 |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1476 | || strcmp(newname, LABEL_RETURN) == 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1477 | errno = EEXIST; |
| 1478 | return 0; |
| 1479 | } |
| 1480 | |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1481 | if (!(c = find_label(oldname, *handle)) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1482 | || TC_BUILTIN(oldname, *handle)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1483 | errno = ENOENT; |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1487 | if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1488 | errno = EINVAL; |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | /* Need label index: preceeds chain start */ |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1493 | labelidx = entry2index(*handle, c->start) - 1; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1494 | labeloff = index2offset(*handle, labelidx); |
| 1495 | |
| 1496 | t = (struct ipt_error_target *) |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1497 | GET_TARGET(get_entry(*handle, labeloff)); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1498 | |
| 1499 | memset(t->error, 0, sizeof(t->error)); |
| 1500 | strcpy(t->error, newname); |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 1501 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1502 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1503 | return 1; |
| 1504 | } |
| 1505 | |
| 1506 | /* Sets the policy on a built-in chain. */ |
| 1507 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1508 | TC_SET_POLICY(const IPT_CHAINLABEL chain, |
| 1509 | const IPT_CHAINLABEL policy, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1510 | STRUCT_COUNTERS *counters, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1511 | TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1512 | { |
| 1513 | unsigned int hook; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1514 | unsigned int policyoff, ctrindex; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1515 | STRUCT_ENTRY *e; |
| 1516 | STRUCT_STANDARD_TARGET *t; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1517 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1518 | iptc_fn = TC_SET_POLICY; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1519 | /* Figure out which chain. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1520 | hook = TC_BUILTIN(chain, *handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1521 | if (hook == 0) { |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 1522 | errno = ENOENT; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1523 | return 0; |
| 1524 | } else |
| 1525 | hook--; |
| 1526 | |
| 1527 | policyoff = get_chain_end(*handle, (*handle)->info.hook_entry[hook]); |
| 1528 | if (policyoff != (*handle)->info.underflow[hook]) { |
| 1529 | printf("ERROR: Policy for `%s' offset %u != underflow %u\n", |
| 1530 | chain, policyoff, (*handle)->info.underflow[hook]); |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | e = get_entry(*handle, policyoff); |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1535 | t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1536 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1537 | if (strcmp(policy, LABEL_ACCEPT) == 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1538 | t->verdict = -NF_ACCEPT - 1; |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1539 | else if (strcmp(policy, LABEL_DROP) == 0) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1540 | t->verdict = -NF_DROP - 1; |
| 1541 | else { |
| 1542 | errno = EINVAL; |
| 1543 | return 0; |
| 1544 | } |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1545 | |
| 1546 | ctrindex = entry2index(*handle, e); |
| 1547 | |
| 1548 | if (counters) { |
| 1549 | /* set byte and packet counters */ |
| 1550 | memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS)); |
| 1551 | |
| 1552 | (*handle)->counter_map[ctrindex].maptype |
| 1553 | = COUNTER_MAP_SET; |
| 1554 | |
| 1555 | } else { |
| 1556 | (*handle)->counter_map[ctrindex] |
| 1557 | = ((struct counter_map){ COUNTER_MAP_NOMAP, 0 }); |
| 1558 | } |
| 1559 | |
| Rusty Russell | 175f641 | 2000-03-24 09:32:20 +0000 | [diff] [blame] | 1560 | set_changed(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1561 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1562 | return 1; |
| 1563 | } |
| 1564 | |
| 1565 | /* Without this, on gcc 2.7.2.3, we get: |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1566 | libiptc.c: In function `TC_COMMIT': |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1567 | libiptc.c:833: fixed or forbidden register was spilled. |
| 1568 | This may be due to a compiler bug or to impossible asm |
| 1569 | statements or clauses. |
| 1570 | */ |
| 1571 | static void |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1572 | subtract_counters(STRUCT_COUNTERS *answer, |
| 1573 | const STRUCT_COUNTERS *a, |
| 1574 | const STRUCT_COUNTERS *b) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1575 | { |
| 1576 | answer->pcnt = a->pcnt - b->pcnt; |
| 1577 | answer->bcnt = a->bcnt - b->bcnt; |
| 1578 | } |
| 1579 | |
| 1580 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1581 | TC_COMMIT(TC_HANDLE_T *handle) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1582 | { |
| 1583 | /* Replace, then map back the counters. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1584 | STRUCT_REPLACE *repl; |
| 1585 | STRUCT_COUNTERS_INFO *newcounters; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1586 | unsigned int i; |
| 1587 | size_t counterlen |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1588 | = sizeof(STRUCT_COUNTERS_INFO) |
| 1589 | + sizeof(STRUCT_COUNTERS) * (*handle)->new_number; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1590 | |
| 1591 | CHECK(*handle); |
| 1592 | #if 0 |
| Rusty Russell | 54c307e | 2000-09-04 06:47:28 +0000 | [diff] [blame] | 1593 | TC_DUMP_ENTRIES(*handle); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1594 | #endif |
| 1595 | |
| 1596 | /* Don't commit if nothing changed. */ |
| 1597 | if (!(*handle)->changed) |
| 1598 | goto finished; |
| 1599 | |
| 1600 | repl = malloc(sizeof(*repl) + (*handle)->entries.size); |
| 1601 | if (!repl) { |
| 1602 | errno = ENOMEM; |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | /* These are the old counters we will get from kernel */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1607 | repl->counters = malloc(sizeof(STRUCT_COUNTERS) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1608 | * (*handle)->info.num_entries); |
| 1609 | if (!repl->counters) { |
| 1610 | free(repl); |
| 1611 | errno = ENOMEM; |
| 1612 | return 0; |
| 1613 | } |
| Rusty Russell | 7e53bf9 | 2000-03-20 07:03:28 +0000 | [diff] [blame] | 1614 | |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1615 | /* These are the counters we're going to put back, later. */ |
| 1616 | newcounters = malloc(counterlen); |
| 1617 | if (!newcounters) { |
| 1618 | free(repl->counters); |
| 1619 | free(repl); |
| 1620 | errno = ENOMEM; |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | strcpy(repl->name, (*handle)->info.name); |
| 1625 | repl->num_entries = (*handle)->new_number; |
| 1626 | repl->size = (*handle)->entries.size; |
| 1627 | memcpy(repl->hook_entry, (*handle)->info.hook_entry, |
| 1628 | sizeof(repl->hook_entry)); |
| 1629 | memcpy(repl->underflow, (*handle)->info.underflow, |
| 1630 | sizeof(repl->underflow)); |
| 1631 | repl->num_counters = (*handle)->info.num_entries; |
| 1632 | repl->valid_hooks = (*handle)->info.valid_hooks; |
| Rusty Russell | 725d97a | 2000-07-07 08:54:22 +0000 | [diff] [blame] | 1633 | memcpy(repl->entries, (*handle)->entries.entrytable, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1634 | (*handle)->entries.size); |
| 1635 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1636 | if (setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1637 | sizeof(*repl) + (*handle)->entries.size) < 0) { |
| 1638 | free(repl->counters); |
| 1639 | free(repl); |
| 1640 | free(newcounters); |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
| 1644 | /* Put counters back. */ |
| 1645 | strcpy(newcounters->name, (*handle)->info.name); |
| 1646 | newcounters->num_counters = (*handle)->new_number; |
| 1647 | for (i = 0; i < (*handle)->new_number; i++) { |
| 1648 | unsigned int mappos = (*handle)->counter_map[i].mappos; |
| 1649 | switch ((*handle)->counter_map[i].maptype) { |
| 1650 | case COUNTER_MAP_NOMAP: |
| 1651 | newcounters->counters[i] |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1652 | = ((STRUCT_COUNTERS){ 0, 0 }); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1653 | break; |
| 1654 | |
| 1655 | case COUNTER_MAP_NORMAL_MAP: |
| 1656 | /* Original read: X. |
| 1657 | * Atomic read on replacement: X + Y. |
| 1658 | * Currently in kernel: Z. |
| 1659 | * Want in kernel: X + Y + Z. |
| 1660 | * => Add in X + Y |
| 1661 | * => Add in replacement read. |
| 1662 | */ |
| 1663 | newcounters->counters[i] = repl->counters[mappos]; |
| 1664 | break; |
| 1665 | |
| 1666 | case COUNTER_MAP_ZEROED: |
| 1667 | /* Original read: X. |
| 1668 | * Atomic read on replacement: X + Y. |
| 1669 | * Currently in kernel: Z. |
| 1670 | * Want in kernel: Y + Z. |
| 1671 | * => Add in Y. |
| 1672 | * => Add in (replacement read - original read). |
| 1673 | */ |
| 1674 | subtract_counters(&newcounters->counters[i], |
| 1675 | &repl->counters[mappos], |
| 1676 | &index2entry(*handle, i)->counters); |
| 1677 | break; |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1678 | |
| 1679 | case COUNTER_MAP_SET: |
| 1680 | /* Want to set counter (iptables-restore) */ |
| 1681 | |
| 1682 | memcpy(&newcounters->counters[i], |
| 1683 | &index2entry(*handle, i)->counters, |
| 1684 | sizeof(STRUCT_COUNTERS)); |
| 1685 | |
| 1686 | break; |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1687 | } |
| 1688 | } |
| Rusty Russell | 62527ce | 2000-09-04 09:45:54 +0000 | [diff] [blame] | 1689 | |
| 1690 | #ifdef KERNEL_64_USERSPACE_32 |
| 1691 | { |
| 1692 | /* Kernel will think that pointer should be 64-bits, and get |
| 1693 | padding. So we accomodate here (assumption: alignment of |
| 1694 | `counters' is on 64-bit boundary). */ |
| 1695 | u_int64_t *kernptr = (u_int64_t *)&newcounters->counters; |
| 1696 | if ((unsigned long)&newcounters->counters % 8 != 0) { |
| 1697 | fprintf(stderr, |
| 1698 | "counters alignment incorrect! Mail rusty!\n"); |
| 1699 | abort(); |
| 1700 | } |
| 1701 | *kernptr = newcounters->counters; |
| Rusty Russell | 54c307e | 2000-09-04 06:47:28 +0000 | [diff] [blame] | 1702 | } |
| Rusty Russell | 62527ce | 2000-09-04 09:45:54 +0000 | [diff] [blame] | 1703 | #endif /* KERNEL_64_USERSPACE_32 */ |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1704 | |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1705 | if (setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS, |
| 1706 | newcounters, counterlen) < 0) { |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1707 | free(repl->counters); |
| 1708 | free(repl); |
| 1709 | free(newcounters); |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | free(repl->counters); |
| 1714 | free(repl); |
| 1715 | free(newcounters); |
| 1716 | |
| 1717 | finished: |
| Rusty Russell | 30fd6e5 | 2000-04-23 09:16:06 +0000 | [diff] [blame] | 1718 | if ((*handle)->cache_chain_heads) |
| 1719 | free((*handle)->cache_chain_heads); |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1720 | free(*handle); |
| 1721 | *handle = NULL; |
| 1722 | return 1; |
| 1723 | } |
| 1724 | |
| 1725 | /* Get raw socket. */ |
| 1726 | int |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1727 | TC_GET_RAW_SOCKET() |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1728 | { |
| 1729 | return sockfd; |
| 1730 | } |
| 1731 | |
| 1732 | /* Translates errno numbers into more human-readable form than strerror. */ |
| 1733 | const char * |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1734 | TC_STRERROR(int err) |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1735 | { |
| 1736 | unsigned int i; |
| 1737 | struct table_struct { |
| 1738 | void *fn; |
| 1739 | int err; |
| 1740 | const char *message; |
| 1741 | } table [] = |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 1742 | { { TC_INIT, EPERM, "Permission denied (you must be root)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1743 | { TC_INIT, EINVAL, "Module is wrong version" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 1744 | { TC_INIT, ENOENT, |
| 1745 | "Table does not exist (do you need to insmod?)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1746 | { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" }, |
| 1747 | { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" }, |
| 1748 | { TC_DELETE_CHAIN, EMLINK, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1749 | "Can't delete chain with references left" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1750 | { TC_CREATE_CHAIN, EEXIST, "Chain already exists" }, |
| 1751 | { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" }, |
| 1752 | { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" }, |
| 1753 | { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" }, |
| Harald Welte | 1cef74d | 2001-01-05 15:22:59 +0000 | [diff] [blame] | 1754 | { TC_READ_COUNTER, E2BIG, "Index of counter too big" }, |
| 1755 | { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1756 | { TC_INSERT_ENTRY, ELOOP, "Loop found in table" }, |
| 1757 | { TC_INSERT_ENTRY, EINVAL, "Target problem" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1758 | /* EINVAL for CHECK probably means bad interface. */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1759 | { TC_CHECK_PACKET, EINVAL, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 1760 | "Bad arguments (does that interface exist?)" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 1761 | { TC_CHECK_PACKET, ENOSYS, |
| 1762 | "Checking will most likely never get implemented" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1763 | /* ENOENT for DELETE probably means no matching rule */ |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1764 | { TC_DELETE_ENTRY, ENOENT, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 1765 | "Bad rule (does a matching rule exist in that chain?)" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1766 | { TC_SET_POLICY, ENOENT, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 1767 | "Bad built-in chain name" }, |
| Rusty Russell | 79dee07 | 2000-05-02 16:45:16 +0000 | [diff] [blame] | 1768 | { TC_SET_POLICY, EINVAL, |
| Marc Boucher | c826499 | 2000-04-22 22:34:44 +0000 | [diff] [blame] | 1769 | "Bad policy name" }, |
| Harald Welte | 4ccfa63 | 2001-07-30 15:12:43 +0000 | [diff] [blame] | 1770 | |
| 1771 | { NULL, 0, "Incompatible with this kernel" }, |
| 1772 | { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" }, |
| 1773 | { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" }, |
| 1774 | { NULL, ENOMEM, "Memory allocation problem" }, |
| 1775 | { NULL, ENOENT, "No chain/target/match by that name" }, |
| Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1776 | }; |
| 1777 | |
| 1778 | for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) { |
| 1779 | if ((!table[i].fn || table[i].fn == iptc_fn) |
| 1780 | && table[i].err == err) |
| 1781 | return table[i].message; |
| 1782 | } |
| 1783 | |
| 1784 | return strerror(err); |
| 1785 | } |