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