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