blob: ec5317bc47e1152edd3333a2871b19930565399e [file] [log] [blame]
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001/* Library which manipulates firewall rules. Version $Revision$ */
Marc Bouchere6869a82000-03-20 06:03:29 +00002
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
Harald Welte3ea8f402003-06-23 18:25:59 +000011/* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 * COPYING for details).
Harald Welteaae69be2004-08-29 23:32:14 +000013 * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org>
Harald Welte3ea8f402003-06-23 18:25:59 +000014 *
Harald Weltefbc85232003-06-24 17:37:21 +000015 * 2003-Jun-20: Harald Welte <laforge@netfilter.org>:
Harald Welte3ea8f402003-06-23 18:25:59 +000016 * - Reimplementation of chain cache to use offsets instead of entries
Harald Weltefbc85232003-06-24 17:37:21 +000017 * 2003-Jun-23: Harald Welte <laforge@netfilter.org>:
Harald Welte0113fe72004-01-06 19:04:02 +000018 * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/)
Harald Weltefbc85232003-06-24 17:37:21 +000019 * don't rebuild the chain cache after every operation, instead fix it
20 * up after a ruleset change.
Harald Welteaae69be2004-08-29 23:32:14 +000021 * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
22 * - futher performance work: total reimplementation of libiptc.
23 * - libiptc now has a real internal (linked-list) represntation of the
24 * ruleset and a parser/compiler from/to this internal representation
25 * - again sponsored by Astaro AG (http://www.astaro.com/)
Harald Welte3ea8f402003-06-23 18:25:59 +000026 */
Harald Welte15920d12004-05-16 09:05:07 +000027#include <sys/types.h>
28#include <sys/socket.h>
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020029#include <xtables.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000030
Harald Welteaae69be2004-08-29 23:32:14 +000031#include "linux_list.h"
32
33//#define IPTC_DEBUG2 1
34
35#ifdef IPTC_DEBUG2
36#include <fcntl.h>
37#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
38#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
39#else
40#define DEBUGP(x, args...)
41#define DEBUGP_C(x, args...)
42#endif
43
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +000044#ifdef DEBUG
45#define debug(x, args...) fprintf(stderr, x, ## args)
46#else
47#define debug(x, args...)
48#endif
49
Marc Bouchere6869a82000-03-20 06:03:29 +000050static int sockfd = -1;
Derrik Pates664c0a32005-02-01 13:28:14 +000051static int sockfd_use = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +000052static void *iptc_fn = NULL;
53
Patrick McHardy0b639362007-09-08 16:00:01 +000054static const char *hooknames[] = {
55 [HOOK_PRE_ROUTING] = "PREROUTING",
56 [HOOK_LOCAL_IN] = "INPUT",
57 [HOOK_FORWARD] = "FORWARD",
58 [HOOK_LOCAL_OUT] = "OUTPUT",
59 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000060#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000061 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000062#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000063};
64
Harald Welteaae69be2004-08-29 23:32:14 +000065/* Convenience structures */
66struct ipt_error_target
67{
68 STRUCT_ENTRY_TARGET t;
69 char error[TABLE_MAXNAMELEN];
70};
71
72struct chain_head;
73struct rule_head;
74
Marc Bouchere6869a82000-03-20 06:03:29 +000075struct counter_map
76{
77 enum {
78 COUNTER_MAP_NOMAP,
79 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000080 COUNTER_MAP_ZEROED,
81 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000082 } maptype;
83 unsigned int mappos;
84};
85
Harald Welteaae69be2004-08-29 23:32:14 +000086enum iptcc_rule_type {
87 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
88 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
89 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
90 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000091};
92
Harald Welteaae69be2004-08-29 23:32:14 +000093struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000094{
Harald Welteaae69be2004-08-29 23:32:14 +000095 struct list_head list;
96 struct chain_head *chain;
97 struct counter_map counter_map;
98
99 unsigned int index; /* index (needed for counter_map) */
100 unsigned int offset; /* offset in rule blob */
101
102 enum iptcc_rule_type type;
103 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
104
105 unsigned int size; /* size of entry data */
106 STRUCT_ENTRY entry[0];
107};
108
109struct chain_head
110{
111 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000112 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000113 unsigned int hooknum; /* hook number+1 if builtin */
114 unsigned int references; /* how many jumps reference us */
115 int verdict; /* verdict if builtin */
116
117 STRUCT_COUNTERS counters; /* per-chain counters */
118 struct counter_map counter_map;
119
120 unsigned int num_rules; /* number of rules in list */
121 struct list_head rules; /* list of rules */
122
123 unsigned int index; /* index (needed for jump resolval) */
124 unsigned int head_offset; /* offset in rule blob */
125 unsigned int foot_index; /* index (needed for counter_map) */
126 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000127};
128
Rusty Russell79dee072000-05-02 16:45:16 +0000129STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000130{
Harald Welteaae69be2004-08-29 23:32:14 +0000131 int changed; /* Have changes been made? */
132
133 struct list_head chains;
134
135 struct chain_head *chain_iterator_cur;
136 struct rule_head *rule_iterator_cur;
137
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000138 unsigned int num_chains; /* number of user defined chains */
139
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000140 struct chain_head **chain_index; /* array for fast chain list access*/
141 unsigned int chain_index_sz;/* size of chain index array */
142
Rusty Russell79dee072000-05-02 16:45:16 +0000143 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000144 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000145};
146
Harald Welteaae69be2004-08-29 23:32:14 +0000147/* allocate a new chain head for the cache */
148static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
149{
150 struct chain_head *c = malloc(sizeof(*c));
151 if (!c)
152 return NULL;
153 memset(c, 0, sizeof(*c));
154
155 strncpy(c->name, name, TABLE_MAXNAMELEN);
156 c->hooknum = hooknum;
157 INIT_LIST_HEAD(&c->rules);
158
159 return c;
160}
161
162/* allocate and initialize a new rule for the cache */
163static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
164{
165 struct rule_head *r = malloc(sizeof(*r)+size);
166 if (!r)
167 return NULL;
168 memset(r, 0, sizeof(*r));
169
170 r->chain = c;
171 r->size = size;
172
173 return r;
174}
175
176/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000177static inline void
Rusty Russell79dee072000-05-02 16:45:16 +0000178set_changed(TC_HANDLE_T h)
Rusty Russell175f6412000-03-24 09:32:20 +0000179{
Rusty Russell175f6412000-03-24 09:32:20 +0000180 h->changed = 1;
181}
182
Harald Welte380ba5f2002-02-13 16:19:55 +0000183#ifdef IPTC_DEBUG
Rusty Russell79dee072000-05-02 16:45:16 +0000184static void do_check(TC_HANDLE_T h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000185#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000186#else
187#define CHECK(h)
188#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000189
Harald Welteaae69be2004-08-29 23:32:14 +0000190
191/**********************************************************************
192 * iptc blob utility functions (iptcb_*)
193 **********************************************************************/
194
Marc Bouchere6869a82000-03-20 06:03:29 +0000195static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000196iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000197 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000198 unsigned int *pos)
199{
200 if (i == seek)
201 return 1;
202 (*pos)++;
203 return 0;
204}
205
Marc Bouchere6869a82000-03-20 06:03:29 +0000206static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000207iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000208 unsigned int number,
209 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000210 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000211{
212 if (*pos == number) {
213 *pe = i;
214 return 1;
215 }
216 (*pos)++;
217 return 0;
218}
219
Harald Welteaae69be2004-08-29 23:32:14 +0000220static inline STRUCT_ENTRY *
221iptcb_get_entry(TC_HANDLE_T h, unsigned int offset)
222{
223 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
224}
225
226static unsigned int
227iptcb_entry2index(const TC_HANDLE_T h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000228{
229 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000230
Harald Welteaae69be2004-08-29 23:32:14 +0000231 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
232 iptcb_get_number, seek, &pos) == 0) {
233 fprintf(stderr, "ERROR: offset %u not an entry!\n",
234 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
235 abort();
236 }
237 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000238}
239
Harald Welte0113fe72004-01-06 19:04:02 +0000240static inline STRUCT_ENTRY *
Harald Welteaae69be2004-08-29 23:32:14 +0000241iptcb_offset2entry(TC_HANDLE_T h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000242{
Harald Welteaae69be2004-08-29 23:32:14 +0000243 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000244}
245
Harald Welteaae69be2004-08-29 23:32:14 +0000246
Harald Welte0113fe72004-01-06 19:04:02 +0000247static inline unsigned long
Harald Welteaae69be2004-08-29 23:32:14 +0000248iptcb_entry2offset(const TC_HANDLE_T h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000249{
Harald Welteaae69be2004-08-29 23:32:14 +0000250 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000251}
252
253static inline unsigned int
Harald Welteaae69be2004-08-29 23:32:14 +0000254iptcb_offset2index(const TC_HANDLE_T h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000255{
Harald Welteaae69be2004-08-29 23:32:14 +0000256 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
257}
258
259/* Returns 0 if not hook entry, else hooknumber + 1 */
260static inline unsigned int
261iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, TC_HANDLE_T h)
262{
263 unsigned int i;
264
265 for (i = 0; i < NUMHOOKS; i++) {
266 if ((h->info.valid_hooks & (1 << i))
267 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
268 return i+1;
269 }
270 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000271}
272
273
Harald Welteaae69be2004-08-29 23:32:14 +0000274/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000275 * Chain index (cache utility) functions
276 **********************************************************************
277 * The chain index is an array with pointers into the chain list, with
278 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
279 * speedup chain list searching, by find a more optimal starting
280 * points when searching the linked list.
281 *
282 * The starting point can be found fast by using a binary search of
283 * the chain index. Thus, reducing the previous search complexity of
284 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
285 *
286 * A nice property of the chain index, is that the "bucket" list
287 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
288 * change this). Oppose to hashing, where the "bucket" list length can
289 * vary a lot.
290 */
291#ifndef CHAIN_INDEX_BUCKET_LEN
292#define CHAIN_INDEX_BUCKET_LEN 40
293#endif
294
295/* Another nice property of the chain index is that inserting/creating
296 * chains in chain list don't change the correctness of the chain
297 * index, it only causes longer lists in the buckets.
298 *
299 * To mitigate the performance penalty of longer bucket lists and the
300 * penalty of rebuilding, the chain index is rebuild only when
301 * CHAIN_INDEX_INSERT_MAX chains has been added.
302 */
303#ifndef CHAIN_INDEX_INSERT_MAX
304#define CHAIN_INDEX_INSERT_MAX 355
305#endif
306
307static inline unsigned int iptcc_is_builtin(struct chain_head *c);
308
309
310/* Use binary search in the chain index array, to find a chain_head
311 * pointer closest to the place of the searched name element.
312 *
313 * Notes that, binary search (obviously) requires that the chain list
314 * is sorted by name.
315 */
316static struct list_head *
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100317iptcc_bsearch_chain_index(const char *name, unsigned int *idx, TC_HANDLE_T handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000318{
319 unsigned int pos, end;
320 int res;
321
322 struct list_head *list_pos;
323 list_pos=&handle->chains;
324
325 /* Check for empty array, e.g. no user defined chains */
326 if (handle->chain_index_sz == 0) {
327 debug("WARNING: handle->chain_index_sz == 0\n");
328 return list_pos;
329 }
330
331 /* Init */
332 end = handle->chain_index_sz;
333 pos = end / 2;
334
335 debug("bsearch Find chain:%s (pos:%d end:%d)\n", name, pos, end);
336
337 /* Loop */
338 loop:
339 if (!handle->chain_index[pos]) {
340 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
341 return &handle->chains; /* Be safe, return orig start pos */
342 }
343
344 res = strcmp(name, handle->chain_index[pos]->name);
345 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100346 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000347
348 debug("bsearch Index[%d] name:%s res:%d ",
349 pos, handle->chain_index[pos]->name, res);
350
351 if (res == 0) { /* Found element, by direct hit */
352 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
353 return list_pos;
354 } else if (res < 0) { /* Too far, jump back */
355 end = pos;
356 pos = pos / 2;
357
358 /* Exit case: First element of array */
359 if (end == 0) {
360 debug("[found] Reached first array elem (end%d)\n",end);
361 return list_pos;
362 }
363 debug("jump back to pos:%d (end:%d)\n", pos, end);
364 goto loop;
365 } else if (res > 0 ){ /* Not far enough, jump forward */
366
367 /* Exit case: Last element of array */
368 if (pos == handle->chain_index_sz-1) {
369 debug("[found] Last array elem (end:%d)\n", end);
370 return list_pos;
371 }
372
373 /* Exit case: Next index less, thus elem in this list section */
374 res = strcmp(name, handle->chain_index[pos+1]->name);
375 if (res < 0) {
376 debug("[found] closest list (end:%d)\n", end);
377 return list_pos;
378 }
379
380 pos = (pos+end)/2;
381 debug("jump forward to pos:%d (end:%d)\n", pos, end);
382 goto loop;
383 }
384
385 return list_pos;
386}
387
388#ifdef DEBUG
389/* Trivial linear search of chain index. Function used for verifying
390 the output of bsearch function */
391static struct list_head *
392iptcc_linearly_search_chain_index(const char *name, TC_HANDLE_T handle)
393{
394 unsigned int i=0;
395 int res=0;
396
397 struct list_head *list_pos;
398 list_pos = &handle->chains;
399
400 if (handle->chain_index_sz)
401 list_pos = &handle->chain_index[0]->list;
402
403 /* Linearly walk of chain index array */
404
405 for (i=0; i < handle->chain_index_sz; i++) {
406 if (handle->chain_index[i]) {
407 res = strcmp(handle->chain_index[i]->name, name);
408 if (res > 0)
409 break; // One step too far
410 list_pos = &handle->chain_index[i]->list;
411 if (res == 0)
412 break; // Direct hit
413 }
414 }
415
416 return list_pos;
417}
418#endif
419
420static int iptcc_chain_index_alloc(TC_HANDLE_T h)
421{
422 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
423 unsigned int array_elems;
424 unsigned int array_mem;
425
426 /* Allocate memory for the chain index array */
427 array_elems = (h->num_chains / list_length) +
428 (h->num_chains % list_length ? 1 : 0);
429 array_mem = sizeof(h->chain_index) * array_elems;
430
431 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
432 array_elems, array_mem);
433
434 h->chain_index = malloc(array_mem);
435 if (!h->chain_index) {
436 h->chain_index_sz = 0;
437 return -ENOMEM;
438 }
439 memset(h->chain_index, 0, array_mem);
440 h->chain_index_sz = array_elems;
441
442 return 1;
443}
444
445static void iptcc_chain_index_free(TC_HANDLE_T h)
446{
447 h->chain_index_sz = 0;
448 free(h->chain_index);
449}
450
451
452#ifdef DEBUG
453static void iptcc_chain_index_dump(TC_HANDLE_T h)
454{
455 unsigned int i = 0;
456
457 /* Dump: contents of chain index array */
458 for (i=0; i < h->chain_index_sz; i++) {
459 if (h->chain_index[i]) {
460 fprintf(stderr, "Chain index[%d].name: %s\n",
461 i, h->chain_index[i]->name);
462 }
463 }
464}
465#endif
466
467/* Build the chain index */
468static int iptcc_chain_index_build(TC_HANDLE_T h)
469{
470 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
471 unsigned int chains = 0;
472 unsigned int cindex = 0;
473 struct chain_head *c;
474
475 /* Build up the chain index array here */
476 debug("Building chain index\n");
477
478 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
479 h->num_chains, list_length, h->chain_index_sz);
480
481 if (h->chain_index_sz == 0)
482 return 0;
483
484 list_for_each_entry(c, &h->chains, list) {
485
486 /* Issue: The index array needs to start after the
487 * builtin chains, as they are not sorted */
488 if (!iptcc_is_builtin(c)) {
489 cindex=chains / list_length;
490
491 /* Safe guard, break out on array limit, this
492 * is useful if chains are added and array is
493 * rebuild, without realloc of memory. */
494 if (cindex >= h->chain_index_sz)
495 break;
496
497 if ((chains % list_length)== 0) {
498 debug("\nIndex[%d] Chains:", cindex);
499 h->chain_index[cindex] = c;
500 }
501 chains++;
502 }
503 debug("%s, ", c->name);
504 }
505 debug("\n");
506
507 return 1;
508}
509
510static int iptcc_chain_index_rebuild(TC_HANDLE_T h)
511{
512 debug("REBUILD chain index array\n");
513 iptcc_chain_index_free(h);
514 if ((iptcc_chain_index_alloc(h)) < 0)
515 return -ENOMEM;
516 iptcc_chain_index_build(h);
517 return 1;
518}
519
520/* Delete chain (pointer) from index array. Removing an element from
521 * the chain list only affects the chain index array, if the chain
522 * index points-to/uses that list pointer.
523 *
524 * There are different strategies, the simple and safe is to rebuild
525 * the chain index every time. The more advanced is to update the
526 * array index to point to the next element, but that requires some
527 * house keeping and boundry checks. The advanced is implemented, as
528 * the simple approach behaves badly when all chains are deleted
529 * because list_for_each processing will always hit the first chain
530 * index, thus causing a rebuild for every chain.
531 */
532static int iptcc_chain_index_delete_chain(struct chain_head *c, TC_HANDLE_T h)
533{
534 struct list_head *index_ptr, *index_ptr2, *next;
535 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100536 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000537
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100538 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000539
540 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
541 c->name, &c->list, index_ptr);
542
543 /* Save the next pointer */
544 next = c->list.next;
545 list_del(&c->list);
546
547 if (index_ptr == &c->list) { /* Chain used as index ptr */
548
549 /* See if its possible to avoid a rebuild, by shifting
550 * to next pointer. Its possible if the next pointer
551 * is located in the same index bucket.
552 */
553 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100554 index_ptr2 = iptcc_bsearch_chain_index(c2->name, &idx2, h);
555 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000556 /* Rebuild needed */
557 return iptcc_chain_index_rebuild(h);
558 } else {
559 /* Avoiding rebuild */
560 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100561 idx, c2->name);
562 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000563 return 0;
564 }
565 }
566 return 0;
567}
568
569
570/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000571 * iptc cache utility functions (iptcc_*)
572 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000573
Harald Welteaae69be2004-08-29 23:32:14 +0000574/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000575static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000576{
577 return (c->hooknum ? 1 : 0);
578}
579
580/* Get a specific rule within a chain */
581static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
582 unsigned int rulenum)
583{
584 struct rule_head *r;
585 unsigned int num = 0;
586
587 list_for_each_entry(r, &c->rules, list) {
588 num++;
589 if (num == rulenum)
590 return r;
591 }
592 return NULL;
593}
594
Martin Josefssona5616dc2004-10-24 22:27:31 +0000595/* Get a specific rule within a chain backwards */
596static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
597 unsigned int rulenum)
598{
599 struct rule_head *r;
600 unsigned int num = 0;
601
602 list_for_each_entry_reverse(r, &c->rules, list) {
603 num++;
604 if (num == rulenum)
605 return r;
606 }
607 return NULL;
608}
609
Harald Welteaae69be2004-08-29 23:32:14 +0000610/* Returns chain head if found, otherwise NULL. */
611static struct chain_head *
612iptcc_find_chain_by_offset(TC_HANDLE_T handle, unsigned int offset)
613{
614 struct list_head *pos;
615
616 if (list_empty(&handle->chains))
617 return NULL;
618
619 list_for_each(pos, &handle->chains) {
620 struct chain_head *c = list_entry(pos, struct chain_head, list);
621 if (offset >= c->head_offset && offset <= c->foot_offset)
622 return c;
Harald Welte0113fe72004-01-06 19:04:02 +0000623 }
624
Harald Welteaae69be2004-08-29 23:32:14 +0000625 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000626}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000627
Harald Welteaae69be2004-08-29 23:32:14 +0000628/* Returns chain head if found, otherwise NULL. */
629static struct chain_head *
630iptcc_find_label(const char *name, TC_HANDLE_T handle)
631{
632 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000633 struct list_head *list_start_pos;
634 unsigned int i=0;
635 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000636
637 if (list_empty(&handle->chains))
638 return NULL;
639
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000640 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000641 list_for_each(pos, &handle->chains) {
642 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000643 if (!iptcc_is_builtin(c))
644 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000645 if (!strcmp(c->name, name))
646 return c;
647 }
648
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000649 /* Find a smart place to start the search via chain index */
650 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
651 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
652
653 /* Handel if bsearch bails out early */
654 if (list_start_pos == &handle->chains) {
655 list_start_pos = pos;
656 }
657#ifdef DEBUG
658 else {
659 /* Verify result of bsearch against linearly index search */
660 struct list_head *test_pos;
661 struct chain_head *test_c, *tmp_c;
662 test_pos = iptcc_linearly_search_chain_index(name, handle);
663 if (list_start_pos != test_pos) {
664 debug("BUG in chain_index search\n");
665 test_c=list_entry(test_pos, struct chain_head,list);
666 tmp_c =list_entry(list_start_pos,struct chain_head,list);
667 debug("Verify search found:\n");
668 debug(" Chain:%s\n", test_c->name);
669 debug("BSearch found:\n");
670 debug(" Chain:%s\n", tmp_c->name);
671 exit(42);
672 }
673 }
674#endif
675
676 /* Initial/special case, no user defined chains */
677 if (handle->num_chains == 0)
678 return NULL;
679
680 /* Start searching through the chain list */
681 list_for_each(pos, list_start_pos->prev) {
682 struct chain_head *c = list_entry(pos, struct chain_head, list);
683 res = strcmp(c->name, name);
684 debug("List search name:%s == %s res:%d\n", name, c->name, res);
685 if (res==0)
686 return c;
687
688 /* We can stop earlier as we know list is sorted */
689 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
690 debug(" Not in list, walked too far, sorted list\n");
691 return NULL;
692 }
693
694 /* Stop on wrap around, if list head is reached */
695 if (pos == &handle->chains) {
696 debug("Stop, list head reached\n");
697 return NULL;
698 }
699 }
700
701 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000702 return NULL;
703}
704
705/* called when rule is to be removed from cache */
706static void iptcc_delete_rule(struct rule_head *r)
707{
708 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
709 /* clean up reference count of called chain */
710 if (r->type == IPTCC_R_JUMP
711 && r->jump)
712 r->jump->references--;
713
714 list_del(&r->list);
715 free(r);
716}
717
718
719/**********************************************************************
720 * RULESET PARSER (blob -> cache)
721 **********************************************************************/
722
Harald Welteaae69be2004-08-29 23:32:14 +0000723/* Delete policy rule of previous chain, since cache doesn't contain
724 * chain policy rules.
725 * WARNING: This function has ugly design and relies on a lot of context, only
726 * to be called from specific places within the parser */
727static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
728{
729 if (h->chain_iterator_cur) {
730 /* policy rule is last rule */
731 struct rule_head *pr = (struct rule_head *)
732 h->chain_iterator_cur->rules.prev;
733
734 /* save verdict */
735 h->chain_iterator_cur->verdict =
736 *(int *)GET_TARGET(pr->entry)->data;
737
738 /* save counter and counter_map information */
739 h->chain_iterator_cur->counter_map.maptype =
740 COUNTER_MAP_NORMAL_MAP;
741 h->chain_iterator_cur->counter_map.mappos = num-1;
742 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
743 sizeof(h->chain_iterator_cur->counters));
744
745 /* foot_offset points to verdict rule */
746 h->chain_iterator_cur->foot_index = num;
747 h->chain_iterator_cur->foot_offset = pr->offset;
748
749 /* delete rule from cache */
750 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000751 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000752
753 return 1;
754 }
755 return 0;
756}
757
Harald Welteec30b6c2005-02-01 16:45:56 +0000758/* alphabetically insert a chain into the list */
759static inline void iptc_insert_chain(TC_HANDLE_T h, struct chain_head *c)
760{
761 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000762 struct list_head *list_start_pos;
763 unsigned int i=1;
764
765 /* Find a smart place to start the insert search */
766 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
767
768 /* Handle the case, where chain.name is smaller than index[0] */
769 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
770 h->chain_index[0] = c; /* Update chain index head */
771 list_start_pos = h->chains.next;
772 debug("Update chain_index[0] with %s\n", c->name);
773 }
774
775 /* Handel if bsearch bails out early */
776 if (list_start_pos == &h->chains) {
777 list_start_pos = h->chains.next;
778 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000779
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000780 /* sort only user defined chains */
781 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000782 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000783 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000784 list_add(&c->list, tmp->list.prev);
785 return;
786 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000787
788 /* Stop if list head is reached */
789 if (&tmp->list == &h->chains) {
790 debug("Insert, list head reached add to tail\n");
791 break;
792 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000793 }
794 }
795
796 /* survived till end of list: add at tail */
797 list_add_tail(&c->list, &h->chains);
798}
799
Harald Welteaae69be2004-08-29 23:32:14 +0000800/* Another ugly helper function split out of cache_add_entry to make it less
801 * spaghetti code */
802static void __iptcc_p_add_chain(TC_HANDLE_T h, struct chain_head *c,
803 unsigned int offset, unsigned int *num)
804{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000805 struct list_head *tail = h->chains.prev;
806 struct chain_head *ctail;
807
Harald Welteaae69be2004-08-29 23:32:14 +0000808 __iptcc_p_del_policy(h, *num);
809
810 c->head_offset = offset;
811 c->index = *num;
812
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000813 /* Chains from kernel are already sorted, as they are inserted
814 * sorted. But there exists an issue when shifting to 1.4.0
815 * from an older version, as old versions allow last created
816 * chain to be unsorted.
817 */
818 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
819 list_add_tail(&c->list, &h->chains);
820 else {
821 ctail = list_entry(tail, struct chain_head, list);
Jesper Dangaard Brouer526d3e12008-07-03 20:29:34 +0200822 if (strcmp(c->name, ctail->name) > 0 ||
823 iptcc_is_builtin(ctail))
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000824 list_add_tail(&c->list, &h->chains);/* Already sorted*/
825 else
826 iptc_insert_chain(h, c);/* Was not sorted */
827 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000828
Harald Welteaae69be2004-08-29 23:32:14 +0000829 h->chain_iterator_cur = c;
830}
831
832/* main parser function: add an entry from the blob to the cache */
833static int cache_add_entry(STRUCT_ENTRY *e,
834 TC_HANDLE_T h,
835 STRUCT_ENTRY **prev,
836 unsigned int *num)
837{
838 unsigned int builtin;
839 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
840
841 DEBUGP("entering...");
842
843 /* Last entry ("policy rule"). End it.*/
844 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
845 /* This is the ERROR node at the end of the chain */
846 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
847
848 __iptcc_p_del_policy(h, *num);
849
850 h->chain_iterator_cur = NULL;
851 goto out_inc;
852 }
853
854 /* We know this is the start of a new chain if it's an ERROR
855 * target, or a hook entry point */
856
857 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
858 struct chain_head *c =
859 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
860 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
861 (char *)c->name, c);
862 if (!c) {
863 errno = -ENOMEM;
864 return -1;
865 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000866 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000867
868 __iptcc_p_add_chain(h, c, offset, num);
869
870 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
871 struct chain_head *c =
872 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
873 builtin);
874 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
875 *num, offset, c, &c->rules);
876 if (!c) {
877 errno = -ENOMEM;
878 return -1;
879 }
880
881 c->hooknum = builtin;
882
883 __iptcc_p_add_chain(h, c, offset, num);
884
885 /* FIXME: this is ugly. */
886 goto new_rule;
887 } else {
888 /* has to be normal rule */
889 struct rule_head *r;
890new_rule:
891
892 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
893 e->next_offset))) {
894 errno = ENOMEM;
895 return -1;
896 }
897 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
898
899 r->index = *num;
900 r->offset = offset;
901 memcpy(r->entry, e, e->next_offset);
902 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
903 r->counter_map.mappos = r->index;
904
905 /* handling of jumps, etc. */
906 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
907 STRUCT_STANDARD_TARGET *t;
908
909 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
910 if (t->target.u.target_size
911 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
912 errno = EINVAL;
913 return -1;
914 }
915
916 if (t->verdict < 0) {
917 DEBUGP_C("standard, verdict=%d\n", t->verdict);
918 r->type = IPTCC_R_STANDARD;
919 } else if (t->verdict == r->offset+e->next_offset) {
920 DEBUGP_C("fallthrough\n");
921 r->type = IPTCC_R_FALLTHROUGH;
922 } else {
923 DEBUGP_C("jump, target=%u\n", t->verdict);
924 r->type = IPTCC_R_JUMP;
925 /* Jump target fixup has to be deferred
926 * until second pass, since we migh not
927 * yet have parsed the target */
928 }
Martin Josefsson52c38022004-09-22 19:39:40 +0000929 } else {
930 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
931 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +0000932 }
933
934 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000935 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +0000936 }
937out_inc:
938 (*num)++;
939 return 0;
940}
941
942
943/* parse an iptables blob into it's pieces */
944static int parse_table(TC_HANDLE_T h)
945{
946 STRUCT_ENTRY *prev;
947 unsigned int num = 0;
948 struct chain_head *c;
949
950 /* First pass: over ruleset blob */
951 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
952 cache_add_entry, h, &prev, &num);
953
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000954 /* Build the chain index, used for chain list search speedup */
955 if ((iptcc_chain_index_alloc(h)) < 0)
956 return -ENOMEM;
957 iptcc_chain_index_build(h);
958
Harald Welteaae69be2004-08-29 23:32:14 +0000959 /* Second pass: fixup parsed data from first pass */
960 list_for_each_entry(c, &h->chains, list) {
961 struct rule_head *r;
962 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100963 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +0000964 STRUCT_STANDARD_TARGET *t;
965
966 if (r->type != IPTCC_R_JUMP)
967 continue;
968
969 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100970 lc = iptcc_find_chain_by_offset(h, t->verdict);
971 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +0000972 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100973 r->jump = lc;
974 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +0000975 }
976 }
977
978 /* FIXME: sort chains */
979
980 return 1;
981}
982
983
984/**********************************************************************
985 * RULESET COMPILATION (cache -> blob)
986 **********************************************************************/
987
988/* Convenience structures */
989struct iptcb_chain_start{
990 STRUCT_ENTRY e;
991 struct ipt_error_target name;
992};
993#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
994 ALIGN(sizeof(struct ipt_error_target)))
995
996struct iptcb_chain_foot {
997 STRUCT_ENTRY e;
998 STRUCT_STANDARD_TARGET target;
999};
1000#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1001 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1002
1003struct iptcb_chain_error {
1004 STRUCT_ENTRY entry;
1005 struct ipt_error_target target;
1006};
1007#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1008 ALIGN(sizeof(struct ipt_error_target)))
1009
1010
1011
1012/* compile rule from cache into blob */
1013static inline int iptcc_compile_rule (TC_HANDLE_T h, STRUCT_REPLACE *repl, struct rule_head *r)
1014{
1015 /* handle jumps */
1016 if (r->type == IPTCC_R_JUMP) {
1017 STRUCT_STANDARD_TARGET *t;
1018 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1019 /* memset for memcmp convenience on delete/replace */
1020 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1021 strcpy(t->target.u.user.name, STANDARD_TARGET);
1022 /* Jumps can only happen to builtin chains, so we
1023 * can safely assume that they always have a header */
1024 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1025 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1026 STRUCT_STANDARD_TARGET *t;
1027 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1028 t->verdict = r->offset + r->size;
1029 }
1030
1031 /* copy entry from cache to blob */
1032 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1033
1034 return 1;
1035}
1036
1037/* compile chain from cache into blob */
1038static int iptcc_compile_chain(TC_HANDLE_T h, STRUCT_REPLACE *repl, struct chain_head *c)
1039{
1040 int ret;
1041 struct rule_head *r;
1042 struct iptcb_chain_start *head;
1043 struct iptcb_chain_foot *foot;
1044
1045 /* only user-defined chains have heaer */
1046 if (!iptcc_is_builtin(c)) {
1047 /* put chain header in place */
1048 head = (void *)repl->entries + c->head_offset;
1049 head->e.target_offset = sizeof(STRUCT_ENTRY);
1050 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1051 strcpy(head->name.t.u.user.name, ERROR_TARGET);
1052 head->name.t.u.target_size =
1053 ALIGN(sizeof(struct ipt_error_target));
1054 strcpy(head->name.error, c->name);
1055 } else {
1056 repl->hook_entry[c->hooknum-1] = c->head_offset;
1057 repl->underflow[c->hooknum-1] = c->foot_offset;
1058 }
1059
1060 /* iterate over rules */
1061 list_for_each_entry(r, &c->rules, list) {
1062 ret = iptcc_compile_rule(h, repl, r);
1063 if (ret < 0)
1064 return ret;
1065 }
1066
1067 /* put chain footer in place */
1068 foot = (void *)repl->entries + c->foot_offset;
1069 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1070 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1071 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1072 foot->target.target.u.target_size =
1073 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1074 /* builtin targets have verdict, others return */
1075 if (iptcc_is_builtin(c))
1076 foot->target.verdict = c->verdict;
1077 else
1078 foot->target.verdict = RETURN;
1079 /* set policy-counters */
1080 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1081
1082 return 0;
1083}
1084
1085/* calculate offset and number for every rule in the cache */
1086static int iptcc_compile_chain_offsets(TC_HANDLE_T h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001087 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001088{
1089 struct rule_head *r;
1090
1091 c->head_offset = *offset;
1092 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1093
1094 if (!iptcc_is_builtin(c)) {
1095 /* Chain has header */
1096 *offset += sizeof(STRUCT_ENTRY)
1097 + ALIGN(sizeof(struct ipt_error_target));
1098 (*num)++;
1099 }
1100
1101 list_for_each_entry(r, &c->rules, list) {
1102 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1103 r->offset = *offset;
1104 r->index = *num;
1105 *offset += r->size;
1106 (*num)++;
1107 }
1108
1109 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
1110 *offset, *num);
1111 c->foot_offset = *offset;
1112 c->foot_index = *num;
1113 *offset += sizeof(STRUCT_ENTRY)
1114 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1115 (*num)++;
1116
1117 return 1;
1118}
1119
1120/* put the pieces back together again */
1121static int iptcc_compile_table_prep(TC_HANDLE_T h, unsigned int *size)
1122{
1123 struct chain_head *c;
1124 unsigned int offset = 0, num = 0;
1125 int ret = 0;
1126
1127 /* First pass: calculate offset for every rule */
1128 list_for_each_entry(c, &h->chains, list) {
1129 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1130 if (ret < 0)
1131 return ret;
1132 }
1133
1134 /* Append one error rule at end of chain */
1135 num++;
1136 offset += sizeof(STRUCT_ENTRY)
1137 + ALIGN(sizeof(struct ipt_error_target));
1138
1139 /* ruleset size is now in offset */
1140 *size = offset;
1141 return num;
1142}
1143
1144static int iptcc_compile_table(TC_HANDLE_T h, STRUCT_REPLACE *repl)
1145{
1146 struct chain_head *c;
1147 struct iptcb_chain_error *error;
1148
1149 /* Second pass: copy from cache to offsets, fill in jumps */
1150 list_for_each_entry(c, &h->chains, list) {
1151 int ret = iptcc_compile_chain(h, repl, c);
1152 if (ret < 0)
1153 return ret;
1154 }
1155
1156 /* Append error rule at end of chain */
1157 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1158 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1159 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
1160 error->target.t.u.user.target_size =
1161 ALIGN(sizeof(struct ipt_error_target));
1162 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1163 strcpy((char *)&error->target.error, "ERROR");
1164
1165 return 1;
1166}
1167
1168/**********************************************************************
1169 * EXTERNAL API (operates on cache only)
1170 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001171
1172/* Allocate handle of given size */
Rusty Russell79dee072000-05-02 16:45:16 +00001173static TC_HANDLE_T
Harald Welte0113fe72004-01-06 19:04:02 +00001174alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001175{
1176 size_t len;
Rusty Russell79dee072000-05-02 16:45:16 +00001177 TC_HANDLE_T h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001178
Harald Welteaae69be2004-08-29 23:32:14 +00001179 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001180
Harald Welteaae69be2004-08-29 23:32:14 +00001181 h = malloc(sizeof(STRUCT_TC_HANDLE));
1182 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001183 errno = ENOMEM;
1184 return NULL;
1185 }
Harald Welteaae69be2004-08-29 23:32:14 +00001186 memset(h, 0, sizeof(*h));
1187 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001188 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001189
Harald Welte0371c0c2004-09-19 21:00:12 +00001190 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001191 if (!h->entries)
1192 goto out_free_handle;
1193
1194 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001195 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001196
1197 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001198
1199out_free_handle:
1200 free(h);
1201
1202 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001203}
1204
Harald Welteaae69be2004-08-29 23:32:14 +00001205
Rusty Russell79dee072000-05-02 16:45:16 +00001206TC_HANDLE_T
1207TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001208{
Rusty Russell79dee072000-05-02 16:45:16 +00001209 TC_HANDLE_T h;
1210 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001211 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001212 socklen_t s;
1213
Rusty Russell79dee072000-05-02 16:45:16 +00001214 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001215
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001216 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1217 errno = EINVAL;
1218 return NULL;
1219 }
1220
Derrik Pates664c0a32005-02-01 13:28:14 +00001221 if (sockfd_use == 0) {
1222 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1223 if (sockfd < 0)
1224 return NULL;
1225 }
1226 sockfd_use++;
Patrick McHardy2f932052008-04-02 14:01:53 +02001227retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001228 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001229
Marc Bouchere6869a82000-03-20 06:03:29 +00001230 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001231 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
1232 if (--sockfd_use == 0) {
1233 close(sockfd);
1234 sockfd = -1;
1235 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001236 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001237 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001238
Harald Welteaae69be2004-08-29 23:32:14 +00001239 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1240 info.valid_hooks, info.num_entries, info.size);
1241
Harald Welte0113fe72004-01-06 19:04:02 +00001242 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001243 == NULL) {
Derrik Pates664c0a32005-02-01 13:28:14 +00001244 if (--sockfd_use == 0) {
1245 close(sockfd);
1246 sockfd = -1;
1247 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001248 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001249 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001250
Marc Bouchere6869a82000-03-20 06:03:29 +00001251 /* Initialize current state */
1252 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001253
Harald Welteaae69be2004-08-29 23:32:14 +00001254 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001255
Rusty Russell79dee072000-05-02 16:45:16 +00001256 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001257
Harald Welteaae69be2004-08-29 23:32:14 +00001258 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
1259 &tmp) < 0)
1260 goto error;
1261
1262#ifdef IPTC_DEBUG2
1263 {
1264 int fd = open("/tmp/libiptc-so_get_entries.blob",
1265 O_CREAT|O_WRONLY);
1266 if (fd >= 0) {
1267 write(fd, h->entries, tmp);
1268 close(fd);
1269 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001270 }
Harald Welteaae69be2004-08-29 23:32:14 +00001271#endif
1272
1273 if (parse_table(h) < 0)
1274 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001275
Marc Bouchere6869a82000-03-20 06:03:29 +00001276 CHECK(h);
1277 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001278error:
1279 TC_FREE(&h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001280 /* A different process changed the ruleset size, retry */
1281 if (errno == EAGAIN)
1282 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001283 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001284}
1285
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001286void
1287TC_FREE(TC_HANDLE_T *h)
1288{
Harald Welteaae69be2004-08-29 23:32:14 +00001289 struct chain_head *c, *tmp;
1290
Derrik Pates664c0a32005-02-01 13:28:14 +00001291 iptc_fn = TC_FREE;
1292 if (--sockfd_use == 0) {
1293 close(sockfd);
1294 sockfd = -1;
1295 }
Harald Welteaae69be2004-08-29 23:32:14 +00001296
1297 list_for_each_entry_safe(c, tmp, &(*h)->chains, list) {
1298 struct rule_head *r, *rtmp;
1299
1300 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1301 free(r);
1302 }
1303
1304 free(c);
1305 }
1306
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001307 iptcc_chain_index_free(*h);
1308
Harald Welteaae69be2004-08-29 23:32:14 +00001309 free((*h)->entries);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001310 free(*h);
Harald Welteaae69be2004-08-29 23:32:14 +00001311
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001312 *h = NULL;
1313}
1314
Marc Bouchere6869a82000-03-20 06:03:29 +00001315static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001316print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001317{
Rusty Russell228e98d2000-04-27 10:28:06 +00001318 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001319 return 0;
1320}
1321
Rusty Russell79dee072000-05-02 16:45:16 +00001322static int dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle);
1323
Marc Bouchere6869a82000-03-20 06:03:29 +00001324void
Rusty Russell79dee072000-05-02 16:45:16 +00001325TC_DUMP_ENTRIES(const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001326{
Derrik Pates664c0a32005-02-01 13:28:14 +00001327 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001328 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001329
Rusty Russelle45c7132004-12-16 13:21:44 +00001330 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001331 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001332 printf("Table `%s'\n", handle->info.name);
1333 printf("Hooks: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001334 handle->info.hook_entry[HOOK_PRE_ROUTING],
1335 handle->info.hook_entry[HOOK_LOCAL_IN],
1336 handle->info.hook_entry[HOOK_FORWARD],
1337 handle->info.hook_entry[HOOK_LOCAL_OUT],
1338 handle->info.hook_entry[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001339 printf("Underflows: pre/in/fwd/out/post = %u/%u/%u/%u/%u\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001340 handle->info.underflow[HOOK_PRE_ROUTING],
1341 handle->info.underflow[HOOK_LOCAL_IN],
1342 handle->info.underflow[HOOK_FORWARD],
1343 handle->info.underflow[HOOK_LOCAL_OUT],
1344 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001345
Harald Welteaae69be2004-08-29 23:32:14 +00001346 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001347 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001348}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001349
Marc Bouchere6869a82000-03-20 06:03:29 +00001350/* Does this chain exist? */
Rusty Russell79dee072000-05-02 16:45:16 +00001351int TC_IS_CHAIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001352{
Derrik Pates664c0a32005-02-01 13:28:14 +00001353 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001354 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001355}
1356
Harald Welteaae69be2004-08-29 23:32:14 +00001357static void iptcc_chain_iterator_advance(TC_HANDLE_T handle)
1358{
1359 struct chain_head *c = handle->chain_iterator_cur;
1360
1361 if (c->list.next == &handle->chains)
1362 handle->chain_iterator_cur = NULL;
1363 else
1364 handle->chain_iterator_cur =
1365 list_entry(c->list.next, struct chain_head, list);
1366}
Marc Bouchere6869a82000-03-20 06:03:29 +00001367
Rusty Russell30fd6e52000-04-23 09:16:06 +00001368/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001369const char *
Philip Blundell8c700902000-05-15 02:17:52 +00001370TC_FIRST_CHAIN(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001371{
Harald Welteaae69be2004-08-29 23:32:14 +00001372 struct chain_head *c = list_entry((*handle)->chains.next,
1373 struct chain_head, list);
1374
1375 iptc_fn = TC_FIRST_CHAIN;
1376
1377
1378 if (list_empty(&(*handle)->chains)) {
1379 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001380 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001381 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001382
Harald Welteaae69be2004-08-29 23:32:14 +00001383 (*handle)->chain_iterator_cur = c;
1384 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001385
Harald Welteaae69be2004-08-29 23:32:14 +00001386 DEBUGP(": returning `%s'\n", c->name);
1387 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001388}
1389
Rusty Russell30fd6e52000-04-23 09:16:06 +00001390/* Iterator functions to run through the chains. Returns NULL at end. */
1391const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001392TC_NEXT_CHAIN(TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001393{
Harald Welteaae69be2004-08-29 23:32:14 +00001394 struct chain_head *c = (*handle)->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001395
Harald Welteaae69be2004-08-29 23:32:14 +00001396 iptc_fn = TC_NEXT_CHAIN;
1397
1398 if (!c) {
1399 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001400 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001401 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001402
Harald Welteaae69be2004-08-29 23:32:14 +00001403 iptcc_chain_iterator_advance(*handle);
1404
1405 DEBUGP(": returning `%s'\n", c->name);
1406 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001407}
1408
1409/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001410const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001411TC_FIRST_RULE(const char *chain, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001412{
Harald Welteaae69be2004-08-29 23:32:14 +00001413 struct chain_head *c;
1414 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001415
Harald Welteaae69be2004-08-29 23:32:14 +00001416 iptc_fn = TC_FIRST_RULE;
1417
1418 DEBUGP("first rule(%s): ", chain);
1419
1420 c = iptcc_find_label(chain, *handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001421 if (!c) {
1422 errno = ENOENT;
1423 return NULL;
1424 }
1425
1426 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001427 if (list_empty(&c->rules)) {
1428 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001429 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001430 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001431
Harald Welteaae69be2004-08-29 23:32:14 +00001432 r = list_entry(c->rules.next, struct rule_head, list);
1433 (*handle)->rule_iterator_cur = r;
1434 DEBUGP_C("%p\n", r);
1435
1436 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001437}
1438
1439/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001440const STRUCT_ENTRY *
Philip Blundell8c700902000-05-15 02:17:52 +00001441TC_NEXT_RULE(const STRUCT_ENTRY *prev, TC_HANDLE_T *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001442{
Harald Welteaae69be2004-08-29 23:32:14 +00001443 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001444
Derrik Pates664c0a32005-02-01 13:28:14 +00001445 iptc_fn = TC_NEXT_RULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001446 DEBUGP("rule_iterator_cur=%p...", (*handle)->rule_iterator_cur);
1447
1448 if (!(*handle)->rule_iterator_cur) {
1449 DEBUGP_C("returning NULL\n");
1450 return NULL;
1451 }
1452
1453 r = list_entry((*handle)->rule_iterator_cur->list.next,
1454 struct rule_head, list);
1455
1456 iptc_fn = TC_NEXT_RULE;
1457
1458 DEBUGP_C("next=%p, head=%p...", &r->list,
1459 &(*handle)->rule_iterator_cur->chain->rules);
1460
1461 if (&r->list == &(*handle)->rule_iterator_cur->chain->rules) {
1462 (*handle)->rule_iterator_cur = NULL;
1463 DEBUGP_C("finished, returning NULL\n");
1464 return NULL;
1465 }
1466
1467 (*handle)->rule_iterator_cur = r;
1468
1469 /* NOTE: prev is without any influence ! */
1470 DEBUGP_C("returning rule %p\n", r);
1471 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001472}
1473
Marc Bouchere6869a82000-03-20 06:03:29 +00001474/* How many rules in this chain? */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001475static unsigned int
Rusty Russell79dee072000-05-02 16:45:16 +00001476TC_NUM_RULES(const char *chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001477{
Harald Welteaae69be2004-08-29 23:32:14 +00001478 struct chain_head *c;
1479 iptc_fn = TC_NUM_RULES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001480 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001481
1482 c = iptcc_find_label(chain, *handle);
1483 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001484 errno = ENOENT;
1485 return (unsigned int)-1;
1486 }
Harald Welteaae69be2004-08-29 23:32:14 +00001487
1488 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001489}
1490
Jan Engelhardt33690a12008-02-11 00:54:00 +01001491static const STRUCT_ENTRY *
1492TC_GET_RULE(const char *chain, unsigned int n, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001493{
Harald Welteaae69be2004-08-29 23:32:14 +00001494 struct chain_head *c;
1495 struct rule_head *r;
1496
1497 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001498
1499 CHECK(*handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001500
1501 c = iptcc_find_label(chain, *handle);
1502 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001503 errno = ENOENT;
1504 return NULL;
1505 }
1506
Harald Welteaae69be2004-08-29 23:32:14 +00001507 r = iptcc_get_rule_num(c, n);
1508 if (!r)
1509 return NULL;
1510 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001511}
1512
1513/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001514static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001515{
Harald Welteaae69be2004-08-29 23:32:14 +00001516 switch (verdict) {
1517 case RETURN:
1518 return LABEL_RETURN;
1519 break;
1520 case -NF_ACCEPT-1:
1521 return LABEL_ACCEPT;
1522 break;
1523 case -NF_DROP-1:
1524 return LABEL_DROP;
1525 break;
1526 case -NF_QUEUE-1:
1527 return LABEL_QUEUE;
1528 break;
1529 default:
1530 fprintf(stderr, "ERROR: %d not a valid target)\n",
1531 verdict);
1532 abort();
1533 break;
1534 }
1535 /* not reached */
1536 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001537}
1538
Harald Welteaae69be2004-08-29 23:32:14 +00001539/* Returns a pointer to the target name of this position. */
1540const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
1541 TC_HANDLE_T *handle)
1542{
1543 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001544 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001545
1546 iptc_fn = TC_GET_TARGET;
1547
1548 switch(r->type) {
1549 int spos;
1550 case IPTCC_R_FALLTHROUGH:
1551 return "";
1552 break;
1553 case IPTCC_R_JUMP:
1554 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1555 return r->jump->name;
1556 break;
1557 case IPTCC_R_STANDARD:
1558 spos = *(int *)GET_TARGET(e)->data;
1559 DEBUGP("r=%p, spos=%d'\n", r, spos);
1560 return standard_target_map(spos);
1561 break;
1562 case IPTCC_R_MODULE:
1563 return GET_TARGET(e)->u.user.name;
1564 break;
1565 }
1566 return NULL;
1567}
Marc Bouchere6869a82000-03-20 06:03:29 +00001568/* Is this a built-in chain? Actually returns hook + 1. */
1569int
Rusty Russell79dee072000-05-02 16:45:16 +00001570TC_BUILTIN(const char *chain, const TC_HANDLE_T handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001571{
Harald Welteaae69be2004-08-29 23:32:14 +00001572 struct chain_head *c;
1573
1574 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001575
Harald Welteaae69be2004-08-29 23:32:14 +00001576 c = iptcc_find_label(chain, handle);
1577 if (!c) {
1578 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001579 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001580 }
Harald Welteaae69be2004-08-29 23:32:14 +00001581
1582 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001583}
1584
1585/* Get the policy of a given built-in chain */
1586const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001587TC_GET_POLICY(const char *chain,
1588 STRUCT_COUNTERS *counters,
1589 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001590{
Harald Welteaae69be2004-08-29 23:32:14 +00001591 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001592
Harald Welteaae69be2004-08-29 23:32:14 +00001593 iptc_fn = TC_GET_POLICY;
1594
1595 DEBUGP("called for chain %s\n", chain);
1596
1597 c = iptcc_find_label(chain, *handle);
1598 if (!c) {
1599 errno = ENOENT;
1600 return NULL;
1601 }
1602
1603 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001604 return NULL;
1605
Harald Welteaae69be2004-08-29 23:32:14 +00001606 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001607
Harald Welteaae69be2004-08-29 23:32:14 +00001608 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001609}
1610
1611static int
Harald Welteaae69be2004-08-29 23:32:14 +00001612iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001613{
Harald Welteaae69be2004-08-29 23:32:14 +00001614 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001615 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001616
Rusty Russell79dee072000-05-02 16:45:16 +00001617 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001618
Rusty Russell67088e72000-05-10 01:18:57 +00001619 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001620 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001621 errno = EINVAL;
1622 return 0;
1623 }
1624 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001625 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1626 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001627 t->verdict = verdict;
1628
Harald Welteaae69be2004-08-29 23:32:14 +00001629 r->type = IPTCC_R_STANDARD;
1630
Marc Bouchere6869a82000-03-20 06:03:29 +00001631 return 1;
1632}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001633
Marc Bouchere6869a82000-03-20 06:03:29 +00001634static int
Harald Welteaae69be2004-08-29 23:32:14 +00001635iptcc_map_target(const TC_HANDLE_T handle,
1636 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001637{
Harald Welteaae69be2004-08-29 23:32:14 +00001638 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001639 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001640
Marc Bouchere6869a82000-03-20 06:03:29 +00001641 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001642 if (strcmp(t->u.user.name, "") == 0) {
1643 r->type = IPTCC_R_FALLTHROUGH;
1644 return 1;
1645 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001646 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001647 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001648 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001649 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001650 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001651 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001652 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001653 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001654 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001655 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001656 /* Can't jump to builtins. */
1657 errno = EINVAL;
1658 return 0;
1659 } else {
1660 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001661 struct chain_head *c;
1662 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001663
Harald Welteaae69be2004-08-29 23:32:14 +00001664 c = iptcc_find_label(t->u.user.name, handle);
1665 if (c) {
1666 DEBUGP_C("found!\n");
1667 r->type = IPTCC_R_JUMP;
1668 r->jump = c;
1669 c->references++;
1670 return 1;
1671 }
1672 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001673 }
1674
1675 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001676 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001677 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001678 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001679 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001680 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001681 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001682 return 1;
1683}
1684
Harald Welte0113fe72004-01-06 19:04:02 +00001685/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001686int
Rusty Russell79dee072000-05-02 16:45:16 +00001687TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1688 const STRUCT_ENTRY *e,
1689 unsigned int rulenum,
1690 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001691{
Harald Welteaae69be2004-08-29 23:32:14 +00001692 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001693 struct rule_head *r;
1694 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001695
Rusty Russell79dee072000-05-02 16:45:16 +00001696 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001697
1698 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001699 errno = ENOENT;
1700 return 0;
1701 }
1702
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001703 /* first rulenum index = 0
1704 first c->num_rules index = 1 */
1705 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001706 errno = E2BIG;
1707 return 0;
1708 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001709
Martin Josefsson631f3612004-09-23 19:25:06 +00001710 /* If we are inserting at the end just take advantage of the
1711 double linked list, insert will happen before the entry
1712 prev points to. */
1713 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001714 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001715 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001716 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001717 prev = &r->list;
1718 } else {
1719 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001720 prev = &r->list;
1721 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001722
Harald Welteaae69be2004-08-29 23:32:14 +00001723 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1724 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001725 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001726 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001727
Harald Welteaae69be2004-08-29 23:32:14 +00001728 memcpy(r->entry, e, e->next_offset);
1729 r->counter_map.maptype = COUNTER_MAP_SET;
1730
1731 if (!iptcc_map_target(*handle, r)) {
1732 free(r);
1733 return 0;
1734 }
1735
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001736 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001737 c->num_rules++;
1738
1739 set_changed(*handle);
1740
1741 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001742}
1743
1744/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1745int
Rusty Russell79dee072000-05-02 16:45:16 +00001746TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1747 const STRUCT_ENTRY *e,
1748 unsigned int rulenum,
1749 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001750{
Harald Welteaae69be2004-08-29 23:32:14 +00001751 struct chain_head *c;
1752 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001753
Rusty Russell79dee072000-05-02 16:45:16 +00001754 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001755
Harald Welteaae69be2004-08-29 23:32:14 +00001756 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001757 errno = ENOENT;
1758 return 0;
1759 }
1760
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001761 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001762 errno = E2BIG;
1763 return 0;
1764 }
1765
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001766 /* Take advantage of the double linked list if possible. */
1767 if (rulenum + 1 <= c->num_rules/2) {
1768 old = iptcc_get_rule_num(c, rulenum + 1);
1769 } else {
1770 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1771 }
1772
Harald Welteaae69be2004-08-29 23:32:14 +00001773 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1774 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001775 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001776 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001777
Harald Welteaae69be2004-08-29 23:32:14 +00001778 memcpy(r->entry, e, e->next_offset);
1779 r->counter_map.maptype = COUNTER_MAP_SET;
1780
1781 if (!iptcc_map_target(*handle, r)) {
1782 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001783 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001784 }
Harald Welte0113fe72004-01-06 19:04:02 +00001785
Harald Welteaae69be2004-08-29 23:32:14 +00001786 list_add(&r->list, &old->list);
1787 iptcc_delete_rule(old);
1788
1789 set_changed(*handle);
1790
1791 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001792}
1793
Harald Welte0113fe72004-01-06 19:04:02 +00001794/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001795 rulenum = length of chain. */
1796int
Rusty Russell79dee072000-05-02 16:45:16 +00001797TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1798 const STRUCT_ENTRY *e,
1799 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001800{
Harald Welteaae69be2004-08-29 23:32:14 +00001801 struct chain_head *c;
1802 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001803
Rusty Russell79dee072000-05-02 16:45:16 +00001804 iptc_fn = TC_APPEND_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001805 if (!(c = iptcc_find_label(chain, *handle))) {
1806 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001807 errno = ENOENT;
1808 return 0;
1809 }
1810
Harald Welteaae69be2004-08-29 23:32:14 +00001811 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1812 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1813 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001814 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001815 }
Harald Welte0113fe72004-01-06 19:04:02 +00001816
Harald Welteaae69be2004-08-29 23:32:14 +00001817 memcpy(r->entry, e, e->next_offset);
1818 r->counter_map.maptype = COUNTER_MAP_SET;
1819
1820 if (!iptcc_map_target(*handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001821 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001822 free(r);
1823 return 0;
1824 }
1825
1826 list_add_tail(&r->list, &c->rules);
1827 c->num_rules++;
1828
1829 set_changed(*handle);
1830
1831 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001832}
1833
1834static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001835match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001836 const unsigned char *a_elems,
1837 const unsigned char *b_elems,
1838 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001839{
Rusty Russell79dee072000-05-02 16:45:16 +00001840 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001841 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001842
1843 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001844 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001845
Rusty Russell228e98d2000-04-27 10:28:06 +00001846 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001847 return 1;
1848
Rusty Russell228e98d2000-04-27 10:28:06 +00001849 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001850 return 1;
1851
Rusty Russell73ef09b2000-07-03 10:24:04 +00001852 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001853
Rusty Russell73ef09b2000-07-03 10:24:04 +00001854 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001855 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001856 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001857 *maskptr += i;
1858 return 0;
1859}
1860
1861static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001862target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001863{
1864 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001865 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001866
Rusty Russell733e54b2004-12-16 14:22:23 +00001867 if (a->type != b->type)
1868 return 0;
1869
1870 ta = GET_TARGET(a->entry);
1871 tb = GET_TARGET(b->entry);
1872
1873 switch (a->type) {
1874 case IPTCC_R_FALLTHROUGH:
1875 return 1;
1876 case IPTCC_R_JUMP:
1877 return a->jump == b->jump;
1878 case IPTCC_R_STANDARD:
1879 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1880 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1881 case IPTCC_R_MODULE:
1882 if (ta->u.target_size != tb->u.target_size)
1883 return 0;
1884 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1885 return 0;
1886
1887 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001888 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001889 return 0;
1890 return 1;
1891 default:
1892 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1893 abort();
1894 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001895}
1896
Rusty Russell733e54b2004-12-16 14:22:23 +00001897static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001898is_same(const STRUCT_ENTRY *a,
1899 const STRUCT_ENTRY *b,
1900 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001901
Harald Welte0113fe72004-01-06 19:04:02 +00001902/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001903int
Rusty Russell79dee072000-05-02 16:45:16 +00001904TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1905 const STRUCT_ENTRY *origfw,
1906 unsigned char *matchmask,
1907 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001908{
Harald Welteaae69be2004-08-29 23:32:14 +00001909 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001910 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001911
Rusty Russell79dee072000-05-02 16:45:16 +00001912 iptc_fn = TC_DELETE_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001913 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001914 errno = ENOENT;
1915 return 0;
1916 }
1917
Rusty Russelle45c7132004-12-16 13:21:44 +00001918 /* Create a rule_head from origfw. */
1919 r = iptcc_alloc_rule(c, origfw->next_offset);
1920 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001921 errno = ENOMEM;
1922 return 0;
1923 }
1924
Rusty Russelle45c7132004-12-16 13:21:44 +00001925 memcpy(r->entry, origfw, origfw->next_offset);
1926 r->counter_map.maptype = COUNTER_MAP_NOMAP;
1927 if (!iptcc_map_target(*handle, r)) {
1928 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1929 free(r);
1930 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001931 } else {
1932 /* iptcc_map_target increment target chain references
1933 * since this is a fake rule only used for matching
1934 * the chain references count is decremented again.
1935 */
1936 if (r->type == IPTCC_R_JUMP
1937 && r->jump)
1938 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001939 }
Harald Welte0113fe72004-01-06 19:04:02 +00001940
Rusty Russelle45c7132004-12-16 13:21:44 +00001941 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001942 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001943
Rusty Russell733e54b2004-12-16 14:22:23 +00001944 mask = is_same(r->entry, i->entry, matchmask);
1945 if (!mask)
1946 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001947
Rusty Russell733e54b2004-12-16 14:22:23 +00001948 if (!target_same(r, i, mask))
1949 continue;
1950
1951 /* If we are about to delete the rule that is the
1952 * current iterator, move rule iterator back. next
1953 * pointer will then point to real next node */
1954 if (i == (*handle)->rule_iterator_cur) {
1955 (*handle)->rule_iterator_cur =
1956 list_entry((*handle)->rule_iterator_cur->list.prev,
1957 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00001958 }
Rusty Russell733e54b2004-12-16 14:22:23 +00001959
1960 c->num_rules--;
1961 iptcc_delete_rule(i);
1962
1963 set_changed(*handle);
1964 free(r);
1965 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001966 }
1967
Rusty Russelle45c7132004-12-16 13:21:44 +00001968 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00001969 errno = ENOENT;
1970 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001971}
Harald Welteaae69be2004-08-29 23:32:14 +00001972
Marc Bouchere6869a82000-03-20 06:03:29 +00001973
1974/* Delete the rule in position `rulenum' in `chain'. */
1975int
Rusty Russell79dee072000-05-02 16:45:16 +00001976TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
1977 unsigned int rulenum,
1978 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001979{
Harald Welteaae69be2004-08-29 23:32:14 +00001980 struct chain_head *c;
1981 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001982
Rusty Russell79dee072000-05-02 16:45:16 +00001983 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001984
1985 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001986 errno = ENOENT;
1987 return 0;
1988 }
1989
Martin Josefssona5616dc2004-10-24 22:27:31 +00001990 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001991 errno = E2BIG;
1992 return 0;
1993 }
1994
1995 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00001996 if (rulenum + 1 <= c->num_rules/2) {
1997 r = iptcc_get_rule_num(c, rulenum + 1);
1998 } else {
1999 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002000 }
2001
Harald Welteaae69be2004-08-29 23:32:14 +00002002 /* If we are about to delete the rule that is the current
2003 * iterator, move rule iterator back. next pointer will then
2004 * point to real next node */
2005 if (r == (*handle)->rule_iterator_cur) {
2006 (*handle)->rule_iterator_cur =
2007 list_entry((*handle)->rule_iterator_cur->list.prev,
2008 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002009 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002010
Harald Welteaae69be2004-08-29 23:32:14 +00002011 c->num_rules--;
2012 iptcc_delete_rule(r);
2013
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002014 set_changed(*handle);
2015
Harald Welteaae69be2004-08-29 23:32:14 +00002016 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002017}
2018
2019/* Check the packet `fw' on chain `chain'. Returns the verdict, or
2020 NULL and sets errno. */
2021const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002022TC_CHECK_PACKET(const IPT_CHAINLABEL chain,
2023 STRUCT_ENTRY *entry,
2024 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002025{
Derrik Pates664c0a32005-02-01 13:28:14 +00002026 iptc_fn = TC_CHECK_PACKET;
Marc Bouchere6869a82000-03-20 06:03:29 +00002027 errno = ENOSYS;
2028 return NULL;
2029}
2030
2031/* Flushes the entries in the given chain (ie. empties chain). */
2032int
Rusty Russell79dee072000-05-02 16:45:16 +00002033TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002034{
Harald Welteaae69be2004-08-29 23:32:14 +00002035 struct chain_head *c;
2036 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002037
Harald Welte0113fe72004-01-06 19:04:02 +00002038 iptc_fn = TC_FLUSH_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002039 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002040 errno = ENOENT;
2041 return 0;
2042 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002043
Harald Welteaae69be2004-08-29 23:32:14 +00002044 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2045 iptcc_delete_rule(r);
2046 }
2047
2048 c->num_rules = 0;
2049
2050 set_changed(*handle);
2051
2052 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002053}
2054
2055/* Zeroes the counters in a chain. */
2056int
Rusty Russell79dee072000-05-02 16:45:16 +00002057TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002058{
Harald Welteaae69be2004-08-29 23:32:14 +00002059 struct chain_head *c;
2060 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002061
Derrik Pates664c0a32005-02-01 13:28:14 +00002062 iptc_fn = TC_ZERO_ENTRIES;
Harald Welteaae69be2004-08-29 23:32:14 +00002063 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002064 errno = ENOENT;
2065 return 0;
2066 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002067
Andy Gaye5bd1d72006-08-22 02:56:41 +00002068 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2069 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2070
Harald Welteaae69be2004-08-29 23:32:14 +00002071 list_for_each_entry(r, &c->rules, list) {
2072 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2073 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002074 }
Harald Welteaae69be2004-08-29 23:32:14 +00002075
Rusty Russell175f6412000-03-24 09:32:20 +00002076 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002077
Marc Bouchere6869a82000-03-20 06:03:29 +00002078 return 1;
2079}
2080
Harald Welte1cef74d2001-01-05 15:22:59 +00002081STRUCT_COUNTERS *
2082TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2083 unsigned int rulenum,
2084 TC_HANDLE_T *handle)
2085{
Harald Welteaae69be2004-08-29 23:32:14 +00002086 struct chain_head *c;
2087 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002088
2089 iptc_fn = TC_READ_COUNTER;
2090 CHECK(*handle);
2091
Harald Welteaae69be2004-08-29 23:32:14 +00002092 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002093 errno = ENOENT;
2094 return NULL;
2095 }
2096
Harald Welteaae69be2004-08-29 23:32:14 +00002097 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002098 errno = E2BIG;
2099 return NULL;
2100 }
2101
Harald Welteaae69be2004-08-29 23:32:14 +00002102 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002103}
2104
2105int
2106TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2107 unsigned int rulenum,
2108 TC_HANDLE_T *handle)
2109{
Harald Welteaae69be2004-08-29 23:32:14 +00002110 struct chain_head *c;
2111 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002112
2113 iptc_fn = TC_ZERO_COUNTER;
2114 CHECK(*handle);
2115
Harald Welteaae69be2004-08-29 23:32:14 +00002116 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002117 errno = ENOENT;
2118 return 0;
2119 }
2120
Harald Welteaae69be2004-08-29 23:32:14 +00002121 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002122 errno = E2BIG;
2123 return 0;
2124 }
2125
Harald Welteaae69be2004-08-29 23:32:14 +00002126 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2127 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002128
2129 set_changed(*handle);
2130
2131 return 1;
2132}
2133
2134int
2135TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2136 unsigned int rulenum,
2137 STRUCT_COUNTERS *counters,
2138 TC_HANDLE_T *handle)
2139{
Harald Welteaae69be2004-08-29 23:32:14 +00002140 struct chain_head *c;
2141 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002142 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002143
2144 iptc_fn = TC_SET_COUNTER;
2145 CHECK(*handle);
2146
Harald Welteaae69be2004-08-29 23:32:14 +00002147 if (!(c = iptcc_find_label(chain, *handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002148 errno = ENOENT;
2149 return 0;
2150 }
Harald Welte0113fe72004-01-06 19:04:02 +00002151
Harald Welteaae69be2004-08-29 23:32:14 +00002152 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002153 errno = E2BIG;
2154 return 0;
2155 }
2156
Harald Welteaae69be2004-08-29 23:32:14 +00002157 e = r->entry;
2158 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002159
2160 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002161
2162 set_changed(*handle);
2163
2164 return 1;
2165}
2166
Marc Bouchere6869a82000-03-20 06:03:29 +00002167/* Creates a new chain. */
2168/* To create a chain, create two rules: error node and unconditional
2169 * return. */
2170int
Rusty Russell79dee072000-05-02 16:45:16 +00002171TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002172{
Harald Welteaae69be2004-08-29 23:32:14 +00002173 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002174 int capacity;
2175 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002176
Rusty Russell79dee072000-05-02 16:45:16 +00002177 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002178
2179 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2180 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002181 if (iptcc_find_label(chain, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002182 || strcmp(chain, LABEL_DROP) == 0
2183 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002184 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002185 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002186 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002187 errno = EEXIST;
2188 return 0;
2189 }
2190
Rusty Russell79dee072000-05-02 16:45:16 +00002191 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002192 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002193 errno = EINVAL;
2194 return 0;
2195 }
2196
Harald Welteaae69be2004-08-29 23:32:14 +00002197 c = iptcc_alloc_chain_head(chain, 0);
2198 if (!c) {
2199 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2200 errno = ENOMEM;
2201 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002202
Harald Welteaae69be2004-08-29 23:32:14 +00002203 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002204 (*handle)->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002205
Harald Welteaae69be2004-08-29 23:32:14 +00002206 DEBUGP("Creating chain `%s'\n", chain);
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +00002207 iptc_insert_chain(*handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002208
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002209 /* Inserting chains don't change the correctness of the chain
2210 * index (except if its smaller than index[0], but that
2211 * handled by iptc_insert_chain). It only causes longer lists
2212 * in the buckets. Thus, only rebuild chain index when the
2213 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2214 */
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002215 capacity = (*handle)->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2216 exceeded = ((((*handle)->num_chains)-capacity));
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002217 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2218 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
2219 capacity, exceeded, (*handle)->num_chains);
2220 iptcc_chain_index_rebuild(*handle);
2221 }
2222
Harald Welte0113fe72004-01-06 19:04:02 +00002223 set_changed(*handle);
2224
Harald Welteaae69be2004-08-29 23:32:14 +00002225 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002226}
2227
2228/* Get the number of references to this chain. */
2229int
Rusty Russell79dee072000-05-02 16:45:16 +00002230TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
2231 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002232{
Harald Welteaae69be2004-08-29 23:32:14 +00002233 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002234
Derrik Pates664c0a32005-02-01 13:28:14 +00002235 iptc_fn = TC_GET_REFERENCES;
Harald Welteaae69be2004-08-29 23:32:14 +00002236 if (!(c = iptcc_find_label(chain, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002237 errno = ENOENT;
2238 return 0;
2239 }
2240
Harald Welteaae69be2004-08-29 23:32:14 +00002241 *ref = c->references;
2242
Marc Bouchere6869a82000-03-20 06:03:29 +00002243 return 1;
2244}
2245
2246/* Deletes a chain. */
2247int
Rusty Russell79dee072000-05-02 16:45:16 +00002248TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002249{
Marc Bouchere6869a82000-03-20 06:03:29 +00002250 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002251 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002252
Rusty Russell79dee072000-05-02 16:45:16 +00002253 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002254
Harald Welteaae69be2004-08-29 23:32:14 +00002255 if (!(c = iptcc_find_label(chain, *handle))) {
2256 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002257 errno = ENOENT;
2258 return 0;
2259 }
2260
Harald Welteaae69be2004-08-29 23:32:14 +00002261 if (TC_BUILTIN(chain, *handle)) {
2262 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2263 errno = EINVAL;
2264 return 0;
2265 }
2266
2267 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2268 DEBUGP("cannot get references on chain `%s'\n", chain);
2269 return 0;
2270 }
2271
2272 if (references > 0) {
2273 DEBUGP("chain `%s' still has references\n", chain);
2274 errno = EMLINK;
2275 return 0;
2276 }
2277
2278 if (c->num_rules) {
2279 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002280 errno = ENOTEMPTY;
2281 return 0;
2282 }
2283
Harald Welteaae69be2004-08-29 23:32:14 +00002284 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002285 * iterator, move chain iterator forward. */
Harald Welteaae69be2004-08-29 23:32:14 +00002286 if (c == (*handle)->chain_iterator_cur)
2287 iptcc_chain_iterator_advance(*handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002288
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002289 (*handle)->num_chains--; /* One user defined chain deleted */
2290
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002291 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
2292 iptcc_chain_index_delete_chain(c, *handle);
2293 free(c);
2294
Harald Welteaae69be2004-08-29 23:32:14 +00002295 DEBUGP("chain `%s' deleted\n", chain);
2296
2297 set_changed(*handle);
2298
2299 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002300}
2301
2302/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002303int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2304 const IPT_CHAINLABEL newname,
2305 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002306{
Harald Welteaae69be2004-08-29 23:32:14 +00002307 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002308 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002309
Harald Welte1de80462000-10-30 12:00:27 +00002310 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2311 QUEUE, RETURN. */
Harald Welteaae69be2004-08-29 23:32:14 +00002312 if (iptcc_find_label(newname, *handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002313 || strcmp(newname, LABEL_DROP) == 0
2314 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002315 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002316 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002317 errno = EEXIST;
2318 return 0;
2319 }
2320
Harald Welteaae69be2004-08-29 23:32:14 +00002321 if (!(c = iptcc_find_label(oldname, *handle))
Rusty Russell79dee072000-05-02 16:45:16 +00002322 || TC_BUILTIN(oldname, *handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002323 errno = ENOENT;
2324 return 0;
2325 }
2326
Rusty Russell79dee072000-05-02 16:45:16 +00002327 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002328 errno = EINVAL;
2329 return 0;
2330 }
2331
Harald Welteaae69be2004-08-29 23:32:14 +00002332 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
2333
Harald Welte0113fe72004-01-06 19:04:02 +00002334 set_changed(*handle);
2335
Marc Bouchere6869a82000-03-20 06:03:29 +00002336 return 1;
2337}
2338
2339/* Sets the policy on a built-in chain. */
2340int
Rusty Russell79dee072000-05-02 16:45:16 +00002341TC_SET_POLICY(const IPT_CHAINLABEL chain,
2342 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002343 STRUCT_COUNTERS *counters,
Rusty Russell79dee072000-05-02 16:45:16 +00002344 TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002345{
Harald Welteaae69be2004-08-29 23:32:14 +00002346 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002347
Rusty Russell79dee072000-05-02 16:45:16 +00002348 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002349
Harald Welteaae69be2004-08-29 23:32:14 +00002350 if (!(c = iptcc_find_label(chain, *handle))) {
2351 DEBUGP("cannot find chain `%s'\n", chain);
2352 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002353 return 0;
2354 }
2355
Harald Welteaae69be2004-08-29 23:32:14 +00002356 if (!iptcc_is_builtin(c)) {
2357 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2358 errno = ENOENT;
2359 return 0;
2360 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002361
Rusty Russell79dee072000-05-02 16:45:16 +00002362 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002363 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002364 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002365 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002366 else {
2367 errno = EINVAL;
2368 return 0;
2369 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002370
Harald Welte1cef74d2001-01-05 15:22:59 +00002371 if (counters) {
2372 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002373 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2374 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002375 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002376 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002377 }
2378
Rusty Russell175f6412000-03-24 09:32:20 +00002379 set_changed(*handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002380
Marc Bouchere6869a82000-03-20 06:03:29 +00002381 return 1;
2382}
2383
2384/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002385 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002386 libiptc.c:833: fixed or forbidden register was spilled.
2387 This may be due to a compiler bug or to impossible asm
2388 statements or clauses.
2389*/
2390static void
Rusty Russell79dee072000-05-02 16:45:16 +00002391subtract_counters(STRUCT_COUNTERS *answer,
2392 const STRUCT_COUNTERS *a,
2393 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002394{
2395 answer->pcnt = a->pcnt - b->pcnt;
2396 answer->bcnt = a->bcnt - b->bcnt;
2397}
2398
Harald Welteaae69be2004-08-29 23:32:14 +00002399
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002400static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002401{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002402 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002403 DEBUGP_C("NOMAP => zero\n");
2404}
2405
2406static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002407 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002408 unsigned int mappos)
2409{
2410 /* Original read: X.
2411 * Atomic read on replacement: X + Y.
2412 * Currently in kernel: Z.
2413 * Want in kernel: X + Y + Z.
2414 * => Add in X + Y
2415 * => Add in replacement read.
2416 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002417 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002418 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2419}
2420
2421static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002422 STRUCT_REPLACE *repl, unsigned int idx,
2423 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002424{
2425 /* Original read: X.
2426 * Atomic read on replacement: X + Y.
2427 * Currently in kernel: Z.
2428 * Want in kernel: Y + Z.
2429 * => Add in Y.
2430 * => Add in (replacement read - original read).
2431 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002432 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002433 &repl->counters[mappos],
2434 counters);
2435 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2436}
2437
2438static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002439 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002440{
2441 /* Want to set counter (iptables-restore) */
2442
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002443 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002444 sizeof(STRUCT_COUNTERS));
2445
2446 DEBUGP_C("SET\n");
2447}
2448
2449
Marc Bouchere6869a82000-03-20 06:03:29 +00002450int
Rusty Russell79dee072000-05-02 16:45:16 +00002451TC_COMMIT(TC_HANDLE_T *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002452{
2453 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002454 STRUCT_REPLACE *repl;
2455 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002456 struct chain_head *c;
2457 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002458 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002459 int new_number;
2460 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002461
Derrik Pates664c0a32005-02-01 13:28:14 +00002462 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002463 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002464
Marc Bouchere6869a82000-03-20 06:03:29 +00002465 /* Don't commit if nothing changed. */
2466 if (!(*handle)->changed)
2467 goto finished;
2468
Harald Welteaae69be2004-08-29 23:32:14 +00002469 new_number = iptcc_compile_table_prep(*handle, &new_size);
2470 if (new_number < 0) {
2471 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002472 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002473 }
2474
2475 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002476 if (!repl) {
2477 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002478 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002479 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002480 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002481
Rusty Russelle45c7132004-12-16 13:21:44 +00002482#if 0
2483 TC_DUMP_ENTRIES(*handle);
2484#endif
2485
Harald Welteaae69be2004-08-29 23:32:14 +00002486 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2487 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002488
2489 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002490 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Marc Bouchere6869a82000-03-20 06:03:29 +00002491 * (*handle)->info.num_entries);
2492 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002493 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002494 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002495 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002496 /* These are the counters we're going to put back, later. */
2497 newcounters = malloc(counterlen);
2498 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002499 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002500 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002501 }
Harald Welteaae69be2004-08-29 23:32:14 +00002502 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002503
2504 strcpy(repl->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002505 repl->num_entries = new_number;
2506 repl->size = new_size;
2507
Marc Bouchere6869a82000-03-20 06:03:29 +00002508 repl->num_counters = (*handle)->info.num_entries;
2509 repl->valid_hooks = (*handle)->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002510
2511 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2512 repl->num_entries, repl->size, repl->num_counters);
2513
2514 ret = iptcc_compile_table(*handle, repl);
2515 if (ret < 0) {
2516 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002517 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002518 }
2519
2520
2521#ifdef IPTC_DEBUG2
2522 {
2523 int fd = open("/tmp/libiptc-so_set_replace.blob",
2524 O_CREAT|O_WRONLY);
2525 if (fd >= 0) {
2526 write(fd, repl, sizeof(*repl) + repl->size);
2527 close(fd);
2528 }
2529 }
2530#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002531
Harald Welted6ba6f52005-11-12 10:39:40 +00002532 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
2533 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002534 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002535 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002536
2537 /* Put counters back. */
2538 strcpy(newcounters->name, (*handle)->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002539 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002540
Harald Welteaae69be2004-08-29 23:32:14 +00002541 list_for_each_entry(c, &(*handle)->chains, list) {
2542 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002543
Harald Welteaae69be2004-08-29 23:32:14 +00002544 /* Builtin chains have their own counters */
2545 if (iptcc_is_builtin(c)) {
2546 DEBUGP("counter for chain-index %u: ", c->foot_index);
2547 switch(c->counter_map.maptype) {
2548 case COUNTER_MAP_NOMAP:
2549 counters_nomap(newcounters, c->foot_index);
2550 break;
2551 case COUNTER_MAP_NORMAL_MAP:
2552 counters_normal_map(newcounters, repl,
2553 c->foot_index,
2554 c->counter_map.mappos);
2555 break;
2556 case COUNTER_MAP_ZEROED:
2557 counters_map_zeroed(newcounters, repl,
2558 c->foot_index,
2559 c->counter_map.mappos,
2560 &c->counters);
2561 break;
2562 case COUNTER_MAP_SET:
2563 counters_map_set(newcounters, c->foot_index,
2564 &c->counters);
2565 break;
2566 }
2567 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002568
Harald Welteaae69be2004-08-29 23:32:14 +00002569 list_for_each_entry(r, &c->rules, list) {
2570 DEBUGP("counter for index %u: ", r->index);
2571 switch (r->counter_map.maptype) {
2572 case COUNTER_MAP_NOMAP:
2573 counters_nomap(newcounters, r->index);
2574 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002575
Harald Welteaae69be2004-08-29 23:32:14 +00002576 case COUNTER_MAP_NORMAL_MAP:
2577 counters_normal_map(newcounters, repl,
2578 r->index,
2579 r->counter_map.mappos);
2580 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002581
Harald Welteaae69be2004-08-29 23:32:14 +00002582 case COUNTER_MAP_ZEROED:
2583 counters_map_zeroed(newcounters, repl,
2584 r->index,
2585 r->counter_map.mappos,
2586 &r->entry->counters);
2587 break;
2588
2589 case COUNTER_MAP_SET:
2590 counters_map_set(newcounters, r->index,
2591 &r->entry->counters);
2592 break;
2593 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002594 }
2595 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002596
Harald Welteaae69be2004-08-29 23:32:14 +00002597#ifdef IPTC_DEBUG2
2598 {
2599 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
2600 O_CREAT|O_WRONLY);
2601 if (fd >= 0) {
2602 write(fd, newcounters, counterlen);
2603 close(fd);
2604 }
2605 }
2606#endif
2607
Harald Welted6ba6f52005-11-12 10:39:40 +00002608 ret = setsockopt(sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
2609 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002610 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002611 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002612
2613 free(repl->counters);
2614 free(repl);
2615 free(newcounters);
2616
Harald Welted6ba6f52005-11-12 10:39:40 +00002617finished:
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002618 TC_FREE(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002619 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002620
2621out_free_newcounters:
2622 free(newcounters);
2623out_free_repl_counters:
2624 free(repl->counters);
2625out_free_repl:
2626 free(repl);
2627out_zero:
2628 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002629}
2630
2631/* Get raw socket. */
2632int
Patrick McHardy0b639362007-09-08 16:00:01 +00002633TC_GET_RAW_SOCKET(void)
Marc Bouchere6869a82000-03-20 06:03:29 +00002634{
2635 return sockfd;
2636}
2637
2638/* Translates errno numbers into more human-readable form than strerror. */
2639const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002640TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002641{
2642 unsigned int i;
2643 struct table_struct {
2644 void *fn;
2645 int err;
2646 const char *message;
2647 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002648 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002649 { TC_INIT, EINVAL, "Module is wrong version" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002650 { TC_INIT, ENOENT,
2651 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002652 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2653 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2654 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002655 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002656 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2657 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2658 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2659 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002660 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2661 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002662 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2663 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002664 /* EINVAL for CHECK probably means bad interface. */
Rusty Russell79dee072000-05-02 16:45:16 +00002665 { TC_CHECK_PACKET, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002666 "Bad arguments (does that interface exist?)" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002667 { TC_CHECK_PACKET, ENOSYS,
2668 "Checking will most likely never get implemented" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002669 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002670 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002671 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002672 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002673 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002674 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002675 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002676
2677 { NULL, 0, "Incompatible with this kernel" },
2678 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2679 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2680 { NULL, ENOMEM, "Memory allocation problem" },
2681 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002682 };
2683
2684 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2685 if ((!table[i].fn || table[i].fn == iptc_fn)
2686 && table[i].err == err)
2687 return table[i].message;
2688 }
2689
2690 return strerror(err);
2691}