blob: 2214077eba7d589d0f6957bd272a54f34bb6421d [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
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +010012 * 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
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +010020 * up after a ruleset change.
Harald Welteaae69be2004-08-29 23:32:14 +000021 * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +010022 * - further performance work: total reimplementation of libiptc.
Harald Welteaae69be2004-08-29 23:32:14 +000023 * - 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/)
Jesper Dangaard Brouerc9477d02009-03-23 14:27:44 +010026 *
27 * 2008-Jan+Jul: Jesper Dangaard Brouer <hawk@comx.dk>
28 * - performance work: speedup chain list "name" searching.
29 * - performance work: speedup initial ruleset parsing.
30 * - sponsored by ComX Networks A/S (http://www.comx.dk/)
Harald Welte3ea8f402003-06-23 18:25:59 +000031 */
Harald Welte15920d12004-05-16 09:05:07 +000032#include <sys/types.h>
33#include <sys/socket.h>
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010034#include <stdbool.h>
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020035#include <xtables.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000036
Harald Welteaae69be2004-08-29 23:32:14 +000037#include "linux_list.h"
38
39//#define IPTC_DEBUG2 1
40
41#ifdef IPTC_DEBUG2
42#include <fcntl.h>
43#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
44#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
45#else
46#define DEBUGP(x, args...)
47#define DEBUGP_C(x, args...)
48#endif
49
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +000050#ifdef DEBUG
51#define debug(x, args...) fprintf(stderr, x, ## args)
52#else
53#define debug(x, args...)
54#endif
55
Marc Bouchere6869a82000-03-20 06:03:29 +000056static void *iptc_fn = NULL;
57
Patrick McHardy0b639362007-09-08 16:00:01 +000058static const char *hooknames[] = {
59 [HOOK_PRE_ROUTING] = "PREROUTING",
60 [HOOK_LOCAL_IN] = "INPUT",
61 [HOOK_FORWARD] = "FORWARD",
62 [HOOK_LOCAL_OUT] = "OUTPUT",
63 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000064#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000065 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000066#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000067};
68
Harald Welteaae69be2004-08-29 23:32:14 +000069/* Convenience structures */
Harald Welteaae69be2004-08-29 23:32:14 +000070struct chain_head;
71struct rule_head;
72
Marc Bouchere6869a82000-03-20 06:03:29 +000073struct counter_map
74{
75 enum {
76 COUNTER_MAP_NOMAP,
77 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000078 COUNTER_MAP_ZEROED,
79 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000080 } maptype;
81 unsigned int mappos;
82};
83
Harald Welteaae69be2004-08-29 23:32:14 +000084enum iptcc_rule_type {
85 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
86 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
87 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
88 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000089};
90
Harald Welteaae69be2004-08-29 23:32:14 +000091struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000092{
Harald Welteaae69be2004-08-29 23:32:14 +000093 struct list_head list;
94 struct chain_head *chain;
95 struct counter_map counter_map;
96
97 unsigned int index; /* index (needed for counter_map) */
98 unsigned int offset; /* offset in rule blob */
99
100 enum iptcc_rule_type type;
101 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
102
103 unsigned int size; /* size of entry data */
104 STRUCT_ENTRY entry[0];
105};
106
107struct chain_head
108{
109 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000110 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000111 unsigned int hooknum; /* hook number+1 if builtin */
112 unsigned int references; /* how many jumps reference us */
113 int verdict; /* verdict if builtin */
114
115 STRUCT_COUNTERS counters; /* per-chain counters */
116 struct counter_map counter_map;
117
118 unsigned int num_rules; /* number of rules in list */
119 struct list_head rules; /* list of rules */
120
121 unsigned int index; /* index (needed for jump resolval) */
122 unsigned int head_offset; /* offset in rule blob */
123 unsigned int foot_index; /* index (needed for counter_map) */
124 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000125};
126
Rusty Russell79dee072000-05-02 16:45:16 +0000127STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000128{
Jan Engelhardt175f4512008-11-10 17:25:55 +0100129 int sockfd;
Harald Welteaae69be2004-08-29 23:32:14 +0000130 int changed; /* Have changes been made? */
131
132 struct list_head chains;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100133
Harald Welteaae69be2004-08-29 23:32:14 +0000134 struct chain_head *chain_iterator_cur;
135 struct rule_head *rule_iterator_cur;
136
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000137 unsigned int num_chains; /* number of user defined chains */
138
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000139 struct chain_head **chain_index; /* array for fast chain list access*/
140 unsigned int chain_index_sz;/* size of chain index array */
141
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200142 int sorted_offsets; /* if chains are received sorted from kernel,
143 * then the offsets are also sorted. Says if its
144 * possible to bsearch offsets using chain_index.
145 */
146
Rusty Russell79dee072000-05-02 16:45:16 +0000147 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000148 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000149};
150
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200151enum bsearch_type {
152 BSEARCH_NAME, /* Binary search after chain name */
153 BSEARCH_OFFSET, /* Binary search based on offset */
154};
155
Harald Welteaae69be2004-08-29 23:32:14 +0000156/* allocate a new chain head for the cache */
157static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
158{
159 struct chain_head *c = malloc(sizeof(*c));
160 if (!c)
161 return NULL;
162 memset(c, 0, sizeof(*c));
163
164 strncpy(c->name, name, TABLE_MAXNAMELEN);
165 c->hooknum = hooknum;
166 INIT_LIST_HEAD(&c->rules);
167
168 return c;
169}
170
171/* allocate and initialize a new rule for the cache */
172static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
173{
174 struct rule_head *r = malloc(sizeof(*r)+size);
175 if (!r)
176 return NULL;
177 memset(r, 0, sizeof(*r));
178
179 r->chain = c;
180 r->size = size;
181
182 return r;
183}
184
185/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000186static inline void
Jan Engelhardtfd187312008-11-10 16:59:27 +0100187set_changed(struct xtc_handle *h)
Rusty Russell175f6412000-03-24 09:32:20 +0000188{
Rusty Russell175f6412000-03-24 09:32:20 +0000189 h->changed = 1;
190}
191
Harald Welte380ba5f2002-02-13 16:19:55 +0000192#ifdef IPTC_DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100193static void do_check(struct xtc_handle *h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000194#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000195#else
196#define CHECK(h)
197#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000198
Harald Welteaae69be2004-08-29 23:32:14 +0000199
200/**********************************************************************
201 * iptc blob utility functions (iptcb_*)
202 **********************************************************************/
203
Marc Bouchere6869a82000-03-20 06:03:29 +0000204static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000205iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000206 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000207 unsigned int *pos)
208{
209 if (i == seek)
210 return 1;
211 (*pos)++;
212 return 0;
213}
214
Marc Bouchere6869a82000-03-20 06:03:29 +0000215static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000216iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000217 unsigned int number,
218 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000219 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000220{
221 if (*pos == number) {
222 *pe = i;
223 return 1;
224 }
225 (*pos)++;
226 return 0;
227}
228
Harald Welteaae69be2004-08-29 23:32:14 +0000229static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100230iptcb_get_entry(struct xtc_handle *h, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000231{
232 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
233}
234
235static unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100236iptcb_entry2index(struct xtc_handle *const h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000237{
238 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000239
Harald Welteaae69be2004-08-29 23:32:14 +0000240 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
241 iptcb_get_number, seek, &pos) == 0) {
242 fprintf(stderr, "ERROR: offset %u not an entry!\n",
243 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
244 abort();
245 }
246 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000247}
248
Harald Welte0113fe72004-01-06 19:04:02 +0000249static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100250iptcb_offset2entry(struct xtc_handle *h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000251{
Harald Welteaae69be2004-08-29 23:32:14 +0000252 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000253}
254
Harald Welteaae69be2004-08-29 23:32:14 +0000255
Harald Welte0113fe72004-01-06 19:04:02 +0000256static inline unsigned long
Jan Engelhardtfd187312008-11-10 16:59:27 +0100257iptcb_entry2offset(struct xtc_handle *const h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000258{
Harald Welteaae69be2004-08-29 23:32:14 +0000259 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000260}
261
262static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100263iptcb_offset2index(struct xtc_handle *const h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000264{
Harald Welteaae69be2004-08-29 23:32:14 +0000265 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
266}
267
268/* Returns 0 if not hook entry, else hooknumber + 1 */
269static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100270iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +0000271{
272 unsigned int i;
273
274 for (i = 0; i < NUMHOOKS; i++) {
275 if ((h->info.valid_hooks & (1 << i))
276 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
277 return i+1;
278 }
279 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000280}
281
282
Harald Welteaae69be2004-08-29 23:32:14 +0000283/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000284 * Chain index (cache utility) functions
285 **********************************************************************
286 * The chain index is an array with pointers into the chain list, with
287 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
288 * speedup chain list searching, by find a more optimal starting
289 * points when searching the linked list.
290 *
291 * The starting point can be found fast by using a binary search of
292 * the chain index. Thus, reducing the previous search complexity of
293 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
294 *
295 * A nice property of the chain index, is that the "bucket" list
296 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
297 * change this). Oppose to hashing, where the "bucket" list length can
298 * vary a lot.
299 */
300#ifndef CHAIN_INDEX_BUCKET_LEN
301#define CHAIN_INDEX_BUCKET_LEN 40
302#endif
303
304/* Another nice property of the chain index is that inserting/creating
305 * chains in chain list don't change the correctness of the chain
306 * index, it only causes longer lists in the buckets.
307 *
308 * To mitigate the performance penalty of longer bucket lists and the
309 * penalty of rebuilding, the chain index is rebuild only when
310 * CHAIN_INDEX_INSERT_MAX chains has been added.
311 */
312#ifndef CHAIN_INDEX_INSERT_MAX
313#define CHAIN_INDEX_INSERT_MAX 355
314#endif
315
316static inline unsigned int iptcc_is_builtin(struct chain_head *c);
317
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000318/* Use binary search in the chain index array, to find a chain_head
319 * pointer closest to the place of the searched name element.
320 *
321 * Notes that, binary search (obviously) requires that the chain list
322 * is sorted by name.
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200323 *
324 * The not so obvious: The chain index array, is actually both sorted
325 * by name and offset, at the same time!. This is only true because,
326 * chain are stored sorted in the kernel (as we pushed it in sorted).
327 *
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000328 */
329static struct list_head *
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200330__iptcc_bsearch_chain_index(const char *name, unsigned int offset,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100331 unsigned int *idx, struct xtc_handle *handle,
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200332 enum bsearch_type type)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000333{
334 unsigned int pos, end;
335 int res;
336
337 struct list_head *list_pos;
338 list_pos=&handle->chains;
339
340 /* Check for empty array, e.g. no user defined chains */
341 if (handle->chain_index_sz == 0) {
342 debug("WARNING: handle->chain_index_sz == 0\n");
343 return list_pos;
344 }
345
346 /* Init */
347 end = handle->chain_index_sz;
348 pos = end / 2;
349
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200350 debug("bsearch Find chain:%s (pos:%d end:%d) (offset:%d)\n",
351 name, pos, end, offset);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000352
353 /* Loop */
354 loop:
355 if (!handle->chain_index[pos]) {
356 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
357 return &handle->chains; /* Be safe, return orig start pos */
358 }
359
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200360 debug("bsearch Index[%d] name:%s ",
361 pos, handle->chain_index[pos]->name);
362
363 /* Support for different compare functions */
364 switch (type) {
365 case BSEARCH_NAME:
366 res = strcmp(name, handle->chain_index[pos]->name);
367 break;
368 case BSEARCH_OFFSET:
369 debug("head_offset:[%d] foot_offset:[%d] ",
370 handle->chain_index[pos]->head_offset,
371 handle->chain_index[pos]->foot_offset);
372 res = offset - handle->chain_index[pos]->head_offset;
373 break;
374 default:
375 fprintf(stderr, "ERROR: %d not a valid bsearch type\n",
376 type);
377 abort();
378 break;
379 }
380 debug("res:%d ", res);
381
382
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000383 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100384 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000385
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000386 if (res == 0) { /* Found element, by direct hit */
387 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
388 return list_pos;
389 } else if (res < 0) { /* Too far, jump back */
390 end = pos;
391 pos = pos / 2;
392
393 /* Exit case: First element of array */
394 if (end == 0) {
395 debug("[found] Reached first array elem (end%d)\n",end);
396 return list_pos;
397 }
398 debug("jump back to pos:%d (end:%d)\n", pos, end);
399 goto loop;
Jiri Popelka96d0d012011-06-10 15:25:55 +0200400 } else { /* res > 0; Not far enough, jump forward */
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000401
402 /* Exit case: Last element of array */
403 if (pos == handle->chain_index_sz-1) {
404 debug("[found] Last array elem (end:%d)\n", end);
405 return list_pos;
406 }
407
408 /* Exit case: Next index less, thus elem in this list section */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200409 switch (type) {
410 case BSEARCH_NAME:
411 res = strcmp(name, handle->chain_index[pos+1]->name);
412 break;
413 case BSEARCH_OFFSET:
414 res = offset - handle->chain_index[pos+1]->head_offset;
415 break;
416 }
417
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000418 if (res < 0) {
419 debug("[found] closest list (end:%d)\n", end);
420 return list_pos;
421 }
422
423 pos = (pos+end)/2;
424 debug("jump forward to pos:%d (end:%d)\n", pos, end);
425 goto loop;
426 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000427}
428
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200429/* Wrapper for string chain name based bsearch */
430static struct list_head *
431iptcc_bsearch_chain_index(const char *name, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100432 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200433{
434 return __iptcc_bsearch_chain_index(name, 0, idx, handle, BSEARCH_NAME);
435}
436
437
438/* Wrapper for offset chain based bsearch */
439static struct list_head *
440iptcc_bsearch_chain_offset(unsigned int offset, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100441 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200442{
443 struct list_head *pos;
444
445 /* If chains were not received sorted from kernel, then the
446 * offset bsearch is not possible.
447 */
448 if (!handle->sorted_offsets)
449 pos = handle->chains.next;
450 else
451 pos = __iptcc_bsearch_chain_index(NULL, offset, idx, handle,
452 BSEARCH_OFFSET);
453 return pos;
454}
455
456
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000457#ifdef DEBUG
458/* Trivial linear search of chain index. Function used for verifying
459 the output of bsearch function */
460static struct list_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100461iptcc_linearly_search_chain_index(const char *name, struct xtc_handle *handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000462{
463 unsigned int i=0;
464 int res=0;
465
466 struct list_head *list_pos;
467 list_pos = &handle->chains;
468
469 if (handle->chain_index_sz)
470 list_pos = &handle->chain_index[0]->list;
471
472 /* Linearly walk of chain index array */
473
474 for (i=0; i < handle->chain_index_sz; i++) {
475 if (handle->chain_index[i]) {
476 res = strcmp(handle->chain_index[i]->name, name);
477 if (res > 0)
478 break; // One step too far
479 list_pos = &handle->chain_index[i]->list;
480 if (res == 0)
481 break; // Direct hit
482 }
483 }
484
485 return list_pos;
486}
487#endif
488
Jan Engelhardtfd187312008-11-10 16:59:27 +0100489static int iptcc_chain_index_alloc(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000490{
491 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
492 unsigned int array_elems;
493 unsigned int array_mem;
494
495 /* Allocate memory for the chain index array */
496 array_elems = (h->num_chains / list_length) +
497 (h->num_chains % list_length ? 1 : 0);
498 array_mem = sizeof(h->chain_index) * array_elems;
499
500 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
501 array_elems, array_mem);
502
503 h->chain_index = malloc(array_mem);
Jan Engelhardt0eee3002008-11-26 17:18:08 +0100504 if (h->chain_index == NULL && array_mem > 0) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000505 h->chain_index_sz = 0;
506 return -ENOMEM;
507 }
508 memset(h->chain_index, 0, array_mem);
509 h->chain_index_sz = array_elems;
510
511 return 1;
512}
513
Jan Engelhardtfd187312008-11-10 16:59:27 +0100514static void iptcc_chain_index_free(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000515{
516 h->chain_index_sz = 0;
517 free(h->chain_index);
518}
519
520
521#ifdef DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100522static void iptcc_chain_index_dump(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000523{
524 unsigned int i = 0;
525
526 /* Dump: contents of chain index array */
527 for (i=0; i < h->chain_index_sz; i++) {
528 if (h->chain_index[i]) {
529 fprintf(stderr, "Chain index[%d].name: %s\n",
530 i, h->chain_index[i]->name);
531 }
532 }
533}
534#endif
535
536/* Build the chain index */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100537static int iptcc_chain_index_build(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000538{
539 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
540 unsigned int chains = 0;
541 unsigned int cindex = 0;
542 struct chain_head *c;
543
544 /* Build up the chain index array here */
545 debug("Building chain index\n");
546
547 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
548 h->num_chains, list_length, h->chain_index_sz);
549
550 if (h->chain_index_sz == 0)
551 return 0;
552
553 list_for_each_entry(c, &h->chains, list) {
554
555 /* Issue: The index array needs to start after the
556 * builtin chains, as they are not sorted */
557 if (!iptcc_is_builtin(c)) {
558 cindex=chains / list_length;
559
560 /* Safe guard, break out on array limit, this
561 * is useful if chains are added and array is
562 * rebuild, without realloc of memory. */
563 if (cindex >= h->chain_index_sz)
564 break;
565
566 if ((chains % list_length)== 0) {
567 debug("\nIndex[%d] Chains:", cindex);
568 h->chain_index[cindex] = c;
569 }
570 chains++;
571 }
572 debug("%s, ", c->name);
573 }
574 debug("\n");
575
576 return 1;
577}
578
Jan Engelhardtfd187312008-11-10 16:59:27 +0100579static int iptcc_chain_index_rebuild(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000580{
581 debug("REBUILD chain index array\n");
582 iptcc_chain_index_free(h);
583 if ((iptcc_chain_index_alloc(h)) < 0)
584 return -ENOMEM;
585 iptcc_chain_index_build(h);
586 return 1;
587}
588
589/* Delete chain (pointer) from index array. Removing an element from
590 * the chain list only affects the chain index array, if the chain
591 * index points-to/uses that list pointer.
592 *
593 * There are different strategies, the simple and safe is to rebuild
594 * the chain index every time. The more advanced is to update the
595 * array index to point to the next element, but that requires some
596 * house keeping and boundry checks. The advanced is implemented, as
597 * the simple approach behaves badly when all chains are deleted
598 * because list_for_each processing will always hit the first chain
599 * index, thus causing a rebuild for every chain.
600 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100601static int iptcc_chain_index_delete_chain(struct chain_head *c, struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000602{
Jan Engelhardt7d91a2a2011-05-30 01:39:54 +0200603 struct list_head *index_ptr, *next;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000604 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100605 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000606
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100607 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000608
609 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
610 c->name, &c->list, index_ptr);
611
612 /* Save the next pointer */
613 next = c->list.next;
614 list_del(&c->list);
615
616 if (index_ptr == &c->list) { /* Chain used as index ptr */
617
618 /* See if its possible to avoid a rebuild, by shifting
619 * to next pointer. Its possible if the next pointer
620 * is located in the same index bucket.
621 */
622 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardt7d91a2a2011-05-30 01:39:54 +0200623 iptcc_bsearch_chain_index(c2->name, &idx2, h);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100624 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000625 /* Rebuild needed */
626 return iptcc_chain_index_rebuild(h);
627 } else {
628 /* Avoiding rebuild */
629 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100630 idx, c2->name);
631 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000632 return 0;
633 }
634 }
635 return 0;
636}
637
638
639/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000640 * iptc cache utility functions (iptcc_*)
641 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000642
Harald Welteaae69be2004-08-29 23:32:14 +0000643/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000644static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000645{
646 return (c->hooknum ? 1 : 0);
647}
648
649/* Get a specific rule within a chain */
650static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
651 unsigned int rulenum)
652{
653 struct rule_head *r;
654 unsigned int num = 0;
655
656 list_for_each_entry(r, &c->rules, list) {
657 num++;
658 if (num == rulenum)
659 return r;
660 }
661 return NULL;
662}
663
Martin Josefssona5616dc2004-10-24 22:27:31 +0000664/* Get a specific rule within a chain backwards */
665static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
666 unsigned int rulenum)
667{
668 struct rule_head *r;
669 unsigned int num = 0;
670
671 list_for_each_entry_reverse(r, &c->rules, list) {
672 num++;
673 if (num == rulenum)
674 return r;
675 }
676 return NULL;
677}
678
Harald Welteaae69be2004-08-29 23:32:14 +0000679/* Returns chain head if found, otherwise NULL. */
680static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100681iptcc_find_chain_by_offset(struct xtc_handle *handle, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000682{
683 struct list_head *pos;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200684 struct list_head *list_start_pos;
685 unsigned int i;
Harald Welteaae69be2004-08-29 23:32:14 +0000686
687 if (list_empty(&handle->chains))
688 return NULL;
689
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200690 /* Find a smart place to start the search */
691 list_start_pos = iptcc_bsearch_chain_offset(offset, &i, handle);
692
693 /* Note that iptcc_bsearch_chain_offset() skips builtin
694 * chains, but this function is only used for finding jump
695 * targets, and a buildin chain is not a valid jump target */
696
697 debug("Offset:[%u] starting search at index:[%u]\n", offset, i);
698// list_for_each(pos, &handle->chains) {
699 list_for_each(pos, list_start_pos->prev) {
Harald Welteaae69be2004-08-29 23:32:14 +0000700 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200701 debug(".");
702 if (offset >= c->head_offset && offset <= c->foot_offset) {
703 debug("Offset search found chain:[%s]\n", c->name);
Harald Welteaae69be2004-08-29 23:32:14 +0000704 return c;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200705 }
Harald Welte0113fe72004-01-06 19:04:02 +0000706 }
707
Harald Welteaae69be2004-08-29 23:32:14 +0000708 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000709}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000710
Harald Welteaae69be2004-08-29 23:32:14 +0000711/* Returns chain head if found, otherwise NULL. */
712static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100713iptcc_find_label(const char *name, struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +0000714{
715 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000716 struct list_head *list_start_pos;
717 unsigned int i=0;
718 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000719
720 if (list_empty(&handle->chains))
721 return NULL;
722
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000723 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000724 list_for_each(pos, &handle->chains) {
725 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000726 if (!iptcc_is_builtin(c))
727 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000728 if (!strcmp(c->name, name))
729 return c;
730 }
731
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000732 /* Find a smart place to start the search via chain index */
733 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
734 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
735
736 /* Handel if bsearch bails out early */
737 if (list_start_pos == &handle->chains) {
738 list_start_pos = pos;
739 }
740#ifdef DEBUG
741 else {
742 /* Verify result of bsearch against linearly index search */
743 struct list_head *test_pos;
744 struct chain_head *test_c, *tmp_c;
745 test_pos = iptcc_linearly_search_chain_index(name, handle);
746 if (list_start_pos != test_pos) {
747 debug("BUG in chain_index search\n");
748 test_c=list_entry(test_pos, struct chain_head,list);
749 tmp_c =list_entry(list_start_pos,struct chain_head,list);
750 debug("Verify search found:\n");
751 debug(" Chain:%s\n", test_c->name);
752 debug("BSearch found:\n");
753 debug(" Chain:%s\n", tmp_c->name);
754 exit(42);
755 }
756 }
757#endif
758
759 /* Initial/special case, no user defined chains */
760 if (handle->num_chains == 0)
761 return NULL;
762
763 /* Start searching through the chain list */
764 list_for_each(pos, list_start_pos->prev) {
765 struct chain_head *c = list_entry(pos, struct chain_head, list);
766 res = strcmp(c->name, name);
767 debug("List search name:%s == %s res:%d\n", name, c->name, res);
768 if (res==0)
769 return c;
770
771 /* We can stop earlier as we know list is sorted */
772 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
773 debug(" Not in list, walked too far, sorted list\n");
774 return NULL;
775 }
776
777 /* Stop on wrap around, if list head is reached */
778 if (pos == &handle->chains) {
779 debug("Stop, list head reached\n");
780 return NULL;
781 }
782 }
783
784 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000785 return NULL;
786}
787
788/* called when rule is to be removed from cache */
789static void iptcc_delete_rule(struct rule_head *r)
790{
791 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
792 /* clean up reference count of called chain */
793 if (r->type == IPTCC_R_JUMP
794 && r->jump)
795 r->jump->references--;
796
797 list_del(&r->list);
798 free(r);
799}
800
801
802/**********************************************************************
803 * RULESET PARSER (blob -> cache)
804 **********************************************************************/
805
Harald Welteaae69be2004-08-29 23:32:14 +0000806/* Delete policy rule of previous chain, since cache doesn't contain
807 * chain policy rules.
808 * WARNING: This function has ugly design and relies on a lot of context, only
809 * to be called from specific places within the parser */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100810static int __iptcc_p_del_policy(struct xtc_handle *h, unsigned int num)
Harald Welteaae69be2004-08-29 23:32:14 +0000811{
Jan Engelhardt51651b62009-10-23 23:35:49 +0200812 const unsigned char *data;
813
Harald Welteaae69be2004-08-29 23:32:14 +0000814 if (h->chain_iterator_cur) {
815 /* policy rule is last rule */
816 struct rule_head *pr = (struct rule_head *)
817 h->chain_iterator_cur->rules.prev;
818
819 /* save verdict */
Jan Engelhardt51651b62009-10-23 23:35:49 +0200820 data = GET_TARGET(pr->entry)->data;
821 h->chain_iterator_cur->verdict = *(const int *)data;
Harald Welteaae69be2004-08-29 23:32:14 +0000822
823 /* save counter and counter_map information */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100824 h->chain_iterator_cur->counter_map.maptype =
Jan Engelhardt7c4d6682009-10-26 18:43:54 +0100825 COUNTER_MAP_ZEROED;
Harald Welteaae69be2004-08-29 23:32:14 +0000826 h->chain_iterator_cur->counter_map.mappos = num-1;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100827 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
Harald Welteaae69be2004-08-29 23:32:14 +0000828 sizeof(h->chain_iterator_cur->counters));
829
830 /* foot_offset points to verdict rule */
831 h->chain_iterator_cur->foot_index = num;
832 h->chain_iterator_cur->foot_offset = pr->offset;
833
834 /* delete rule from cache */
835 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000836 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000837
838 return 1;
839 }
840 return 0;
841}
842
Harald Welteec30b6c2005-02-01 16:45:56 +0000843/* alphabetically insert a chain into the list */
Christoph Paasch7cd15e32009-03-23 13:50:11 +0100844static void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c)
Harald Welteec30b6c2005-02-01 16:45:56 +0000845{
846 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000847 struct list_head *list_start_pos;
848 unsigned int i=1;
849
850 /* Find a smart place to start the insert search */
851 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
852
853 /* Handle the case, where chain.name is smaller than index[0] */
854 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
855 h->chain_index[0] = c; /* Update chain index head */
856 list_start_pos = h->chains.next;
857 debug("Update chain_index[0] with %s\n", c->name);
858 }
859
860 /* Handel if bsearch bails out early */
861 if (list_start_pos == &h->chains) {
862 list_start_pos = h->chains.next;
863 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000864
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000865 /* sort only user defined chains */
866 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000867 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000868 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000869 list_add(&c->list, tmp->list.prev);
870 return;
871 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000872
873 /* Stop if list head is reached */
874 if (&tmp->list == &h->chains) {
875 debug("Insert, list head reached add to tail\n");
876 break;
877 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000878 }
879 }
880
881 /* survived till end of list: add at tail */
882 list_add_tail(&c->list, &h->chains);
883}
884
Harald Welteaae69be2004-08-29 23:32:14 +0000885/* Another ugly helper function split out of cache_add_entry to make it less
886 * spaghetti code */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100887static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c,
Harald Welteaae69be2004-08-29 23:32:14 +0000888 unsigned int offset, unsigned int *num)
889{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000890 struct list_head *tail = h->chains.prev;
891 struct chain_head *ctail;
892
Harald Welteaae69be2004-08-29 23:32:14 +0000893 __iptcc_p_del_policy(h, *num);
894
895 c->head_offset = offset;
896 c->index = *num;
897
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000898 /* Chains from kernel are already sorted, as they are inserted
899 * sorted. But there exists an issue when shifting to 1.4.0
900 * from an older version, as old versions allow last created
901 * chain to be unsorted.
902 */
903 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
904 list_add_tail(&c->list, &h->chains);
905 else {
906 ctail = list_entry(tail, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200907
Jesper Dangaard Brouer526d3e12008-07-03 20:29:34 +0200908 if (strcmp(c->name, ctail->name) > 0 ||
909 iptcc_is_builtin(ctail))
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000910 list_add_tail(&c->list, &h->chains);/* Already sorted*/
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200911 else {
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000912 iptc_insert_chain(h, c);/* Was not sorted */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200913
914 /* Notice, if chains were not received sorted
915 * from kernel, then an offset bsearch is no
916 * longer valid.
917 */
918 h->sorted_offsets = 0;
919
920 debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n",
921 c->name, ctail->name);
922 }
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000923 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000924
Harald Welteaae69be2004-08-29 23:32:14 +0000925 h->chain_iterator_cur = c;
926}
927
928/* main parser function: add an entry from the blob to the cache */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100929static int cache_add_entry(STRUCT_ENTRY *e,
930 struct xtc_handle *h,
Harald Welteaae69be2004-08-29 23:32:14 +0000931 STRUCT_ENTRY **prev,
932 unsigned int *num)
933{
934 unsigned int builtin;
935 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
936
937 DEBUGP("entering...");
938
939 /* Last entry ("policy rule"). End it.*/
940 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
941 /* This is the ERROR node at the end of the chain */
942 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
943
944 __iptcc_p_del_policy(h, *num);
945
946 h->chain_iterator_cur = NULL;
947 goto out_inc;
948 }
949
950 /* We know this is the start of a new chain if it's an ERROR
951 * target, or a hook entry point */
952
953 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100954 struct chain_head *c =
Harald Welteaae69be2004-08-29 23:32:14 +0000955 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100956 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
Harald Welteaae69be2004-08-29 23:32:14 +0000957 (char *)c->name, c);
958 if (!c) {
959 errno = -ENOMEM;
960 return -1;
961 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000962 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000963
964 __iptcc_p_add_chain(h, c, offset, num);
965
966 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
967 struct chain_head *c =
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100968 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
Harald Welteaae69be2004-08-29 23:32:14 +0000969 builtin);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100970 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
Harald Welteaae69be2004-08-29 23:32:14 +0000971 *num, offset, c, &c->rules);
972 if (!c) {
973 errno = -ENOMEM;
974 return -1;
975 }
976
977 c->hooknum = builtin;
978
979 __iptcc_p_add_chain(h, c, offset, num);
980
981 /* FIXME: this is ugly. */
982 goto new_rule;
983 } else {
984 /* has to be normal rule */
985 struct rule_head *r;
986new_rule:
987
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100988 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
Harald Welteaae69be2004-08-29 23:32:14 +0000989 e->next_offset))) {
990 errno = ENOMEM;
991 return -1;
992 }
993 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
994
995 r->index = *num;
996 r->offset = offset;
997 memcpy(r->entry, e, e->next_offset);
998 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
999 r->counter_map.mappos = r->index;
1000
1001 /* handling of jumps, etc. */
1002 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
1003 STRUCT_STANDARD_TARGET *t;
1004
1005 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
1006 if (t->target.u.target_size
1007 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
1008 errno = EINVAL;
1009 return -1;
1010 }
1011
1012 if (t->verdict < 0) {
1013 DEBUGP_C("standard, verdict=%d\n", t->verdict);
1014 r->type = IPTCC_R_STANDARD;
1015 } else if (t->verdict == r->offset+e->next_offset) {
1016 DEBUGP_C("fallthrough\n");
1017 r->type = IPTCC_R_FALLTHROUGH;
1018 } else {
1019 DEBUGP_C("jump, target=%u\n", t->verdict);
1020 r->type = IPTCC_R_JUMP;
1021 /* Jump target fixup has to be deferred
1022 * until second pass, since we migh not
1023 * yet have parsed the target */
1024 }
Martin Josefsson52c38022004-09-22 19:39:40 +00001025 } else {
1026 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
1027 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001028 }
1029
1030 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +00001031 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +00001032 }
1033out_inc:
1034 (*num)++;
1035 return 0;
1036}
1037
1038
1039/* parse an iptables blob into it's pieces */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001040static int parse_table(struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +00001041{
1042 STRUCT_ENTRY *prev;
1043 unsigned int num = 0;
1044 struct chain_head *c;
1045
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +02001046 /* Assume that chains offsets are sorted, this verified during
1047 parsing of ruleset (in __iptcc_p_add_chain())*/
1048 h->sorted_offsets = 1;
1049
Harald Welteaae69be2004-08-29 23:32:14 +00001050 /* First pass: over ruleset blob */
1051 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
1052 cache_add_entry, h, &prev, &num);
1053
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001054 /* Build the chain index, used for chain list search speedup */
1055 if ((iptcc_chain_index_alloc(h)) < 0)
1056 return -ENOMEM;
1057 iptcc_chain_index_build(h);
1058
Harald Welteaae69be2004-08-29 23:32:14 +00001059 /* Second pass: fixup parsed data from first pass */
1060 list_for_each_entry(c, &h->chains, list) {
1061 struct rule_head *r;
1062 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001063 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +00001064 STRUCT_STANDARD_TARGET *t;
1065
1066 if (r->type != IPTCC_R_JUMP)
1067 continue;
1068
1069 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001070 lc = iptcc_find_chain_by_offset(h, t->verdict);
1071 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +00001072 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001073 r->jump = lc;
1074 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +00001075 }
1076 }
1077
Harald Welteaae69be2004-08-29 23:32:14 +00001078 return 1;
1079}
1080
1081
1082/**********************************************************************
1083 * RULESET COMPILATION (cache -> blob)
1084 **********************************************************************/
1085
1086/* Convenience structures */
1087struct iptcb_chain_start{
1088 STRUCT_ENTRY e;
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001089 struct xt_error_target name;
Harald Welteaae69be2004-08-29 23:32:14 +00001090};
1091#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001092 ALIGN(sizeof(struct xt_error_target)))
Harald Welteaae69be2004-08-29 23:32:14 +00001093
1094struct iptcb_chain_foot {
1095 STRUCT_ENTRY e;
1096 STRUCT_STANDARD_TARGET target;
1097};
1098#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1099 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1100
1101struct iptcb_chain_error {
1102 STRUCT_ENTRY entry;
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001103 struct xt_error_target target;
Harald Welteaae69be2004-08-29 23:32:14 +00001104};
1105#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001106 ALIGN(sizeof(struct xt_error_target)))
Harald Welteaae69be2004-08-29 23:32:14 +00001107
1108
1109
1110/* compile rule from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001111static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r)
Harald Welteaae69be2004-08-29 23:32:14 +00001112{
1113 /* handle jumps */
1114 if (r->type == IPTCC_R_JUMP) {
1115 STRUCT_STANDARD_TARGET *t;
1116 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1117 /* memset for memcmp convenience on delete/replace */
1118 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1119 strcpy(t->target.u.user.name, STANDARD_TARGET);
1120 /* Jumps can only happen to builtin chains, so we
1121 * can safely assume that they always have a header */
1122 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1123 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1124 STRUCT_STANDARD_TARGET *t;
1125 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1126 t->verdict = r->offset + r->size;
1127 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001128
Harald Welteaae69be2004-08-29 23:32:14 +00001129 /* copy entry from cache to blob */
1130 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1131
1132 return 1;
1133}
1134
1135/* compile chain from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001136static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +00001137{
1138 int ret;
1139 struct rule_head *r;
1140 struct iptcb_chain_start *head;
1141 struct iptcb_chain_foot *foot;
1142
1143 /* only user-defined chains have heaer */
1144 if (!iptcc_is_builtin(c)) {
1145 /* put chain header in place */
1146 head = (void *)repl->entries + c->head_offset;
1147 head->e.target_offset = sizeof(STRUCT_ENTRY);
1148 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001149 strcpy(head->name.target.u.user.name, ERROR_TARGET);
1150 head->name.target.u.target_size =
1151 ALIGN(sizeof(struct xt_error_target));
1152 strcpy(head->name.errorname, c->name);
Harald Welteaae69be2004-08-29 23:32:14 +00001153 } else {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001154 repl->hook_entry[c->hooknum-1] = c->head_offset;
Harald Welteaae69be2004-08-29 23:32:14 +00001155 repl->underflow[c->hooknum-1] = c->foot_offset;
1156 }
1157
1158 /* iterate over rules */
1159 list_for_each_entry(r, &c->rules, list) {
1160 ret = iptcc_compile_rule(h, repl, r);
1161 if (ret < 0)
1162 return ret;
1163 }
1164
1165 /* put chain footer in place */
1166 foot = (void *)repl->entries + c->foot_offset;
1167 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1168 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1169 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1170 foot->target.target.u.target_size =
1171 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1172 /* builtin targets have verdict, others return */
1173 if (iptcc_is_builtin(c))
1174 foot->target.verdict = c->verdict;
1175 else
1176 foot->target.verdict = RETURN;
1177 /* set policy-counters */
1178 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1179
1180 return 0;
1181}
1182
1183/* calculate offset and number for every rule in the cache */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001184static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001185 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001186{
1187 struct rule_head *r;
1188
1189 c->head_offset = *offset;
1190 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1191
1192 if (!iptcc_is_builtin(c)) {
1193 /* Chain has header */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001194 *offset += sizeof(STRUCT_ENTRY)
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001195 + ALIGN(sizeof(struct xt_error_target));
Harald Welteaae69be2004-08-29 23:32:14 +00001196 (*num)++;
1197 }
1198
1199 list_for_each_entry(r, &c->rules, list) {
1200 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1201 r->offset = *offset;
1202 r->index = *num;
1203 *offset += r->size;
1204 (*num)++;
1205 }
1206
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001207 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
Harald Welteaae69be2004-08-29 23:32:14 +00001208 *offset, *num);
1209 c->foot_offset = *offset;
1210 c->foot_index = *num;
1211 *offset += sizeof(STRUCT_ENTRY)
1212 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1213 (*num)++;
1214
1215 return 1;
1216}
1217
1218/* put the pieces back together again */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001219static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size)
Harald Welteaae69be2004-08-29 23:32:14 +00001220{
1221 struct chain_head *c;
1222 unsigned int offset = 0, num = 0;
1223 int ret = 0;
1224
1225 /* First pass: calculate offset for every rule */
1226 list_for_each_entry(c, &h->chains, list) {
1227 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1228 if (ret < 0)
1229 return ret;
1230 }
1231
1232 /* Append one error rule at end of chain */
1233 num++;
1234 offset += sizeof(STRUCT_ENTRY)
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001235 + ALIGN(sizeof(struct xt_error_target));
Harald Welteaae69be2004-08-29 23:32:14 +00001236
1237 /* ruleset size is now in offset */
1238 *size = offset;
1239 return num;
1240}
1241
Jan Engelhardtfd187312008-11-10 16:59:27 +01001242static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl)
Harald Welteaae69be2004-08-29 23:32:14 +00001243{
1244 struct chain_head *c;
1245 struct iptcb_chain_error *error;
1246
1247 /* Second pass: copy from cache to offsets, fill in jumps */
1248 list_for_each_entry(c, &h->chains, list) {
1249 int ret = iptcc_compile_chain(h, repl, c);
1250 if (ret < 0)
1251 return ret;
1252 }
1253
1254 /* Append error rule at end of chain */
1255 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1256 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1257 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001258 error->target.target.u.user.target_size =
1259 ALIGN(sizeof(struct xt_error_target));
1260 strcpy((char *)&error->target.target.u.user.name, ERROR_TARGET);
1261 strcpy((char *)&error->target.errorname, "ERROR");
Harald Welteaae69be2004-08-29 23:32:14 +00001262
1263 return 1;
1264}
1265
1266/**********************************************************************
1267 * EXTERNAL API (operates on cache only)
1268 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001269
1270/* Allocate handle of given size */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001271static struct xtc_handle *
Harald Welte0113fe72004-01-06 19:04:02 +00001272alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001273{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001274 struct xtc_handle *h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001275
Harald Welteaae69be2004-08-29 23:32:14 +00001276 h = malloc(sizeof(STRUCT_TC_HANDLE));
1277 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001278 errno = ENOMEM;
1279 return NULL;
1280 }
Harald Welteaae69be2004-08-29 23:32:14 +00001281 memset(h, 0, sizeof(*h));
1282 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001283 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001284
Harald Welte0371c0c2004-09-19 21:00:12 +00001285 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001286 if (!h->entries)
1287 goto out_free_handle;
1288
1289 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001290 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001291
1292 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001293
1294out_free_handle:
1295 free(h);
1296
1297 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001298}
1299
Harald Welteaae69be2004-08-29 23:32:14 +00001300
Jan Engelhardtfd187312008-11-10 16:59:27 +01001301struct xtc_handle *
Rusty Russell79dee072000-05-02 16:45:16 +00001302TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001303{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001304 struct xtc_handle *h;
Rusty Russell79dee072000-05-02 16:45:16 +00001305 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001306 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001307 socklen_t s;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001308 int sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001309
Rusty Russell79dee072000-05-02 16:45:16 +00001310 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001311
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001312 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1313 errno = EINVAL;
1314 return NULL;
1315 }
Jan Engelhardt175f4512008-11-10 17:25:55 +01001316
1317 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1318 if (sockfd < 0)
1319 return NULL;
1320
Patrick McHardy2f932052008-04-02 14:01:53 +02001321retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001322 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001323
Marc Bouchere6869a82000-03-20 06:03:29 +00001324 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001325 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001326 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001327 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001328 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001329
Harald Welteaae69be2004-08-29 23:32:14 +00001330 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1331 info.valid_hooks, info.num_entries, info.size);
1332
Harald Welte0113fe72004-01-06 19:04:02 +00001333 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001334 == NULL) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001335 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001336 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001337 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001338
Marc Bouchere6869a82000-03-20 06:03:29 +00001339 /* Initialize current state */
Jan Engelhardt175f4512008-11-10 17:25:55 +01001340 h->sockfd = sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001341 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001342
Harald Welteaae69be2004-08-29 23:32:14 +00001343 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001344
Rusty Russell79dee072000-05-02 16:45:16 +00001345 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001346
Jan Engelhardt175f4512008-11-10 17:25:55 +01001347 if (getsockopt(h->sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
Harald Welteaae69be2004-08-29 23:32:14 +00001348 &tmp) < 0)
1349 goto error;
1350
1351#ifdef IPTC_DEBUG2
1352 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001353 int fd = open("/tmp/libiptc-so_get_entries.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00001354 O_CREAT|O_WRONLY);
1355 if (fd >= 0) {
1356 write(fd, h->entries, tmp);
1357 close(fd);
1358 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001359 }
Harald Welteaae69be2004-08-29 23:32:14 +00001360#endif
1361
1362 if (parse_table(h) < 0)
1363 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001364
Marc Bouchere6869a82000-03-20 06:03:29 +00001365 CHECK(h);
1366 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001367error:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001368 TC_FREE(h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001369 /* A different process changed the ruleset size, retry */
1370 if (errno == EAGAIN)
1371 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001372 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001373}
1374
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001375void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001376TC_FREE(struct xtc_handle *h)
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001377{
Harald Welteaae69be2004-08-29 23:32:14 +00001378 struct chain_head *c, *tmp;
1379
Derrik Pates664c0a32005-02-01 13:28:14 +00001380 iptc_fn = TC_FREE;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001381 close(h->sockfd);
Harald Welteaae69be2004-08-29 23:32:14 +00001382
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001383 list_for_each_entry_safe(c, tmp, &h->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00001384 struct rule_head *r, *rtmp;
1385
1386 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1387 free(r);
1388 }
1389
1390 free(c);
1391 }
1392
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001393 iptcc_chain_index_free(h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001394
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001395 free(h->entries);
1396 free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001397}
1398
Marc Bouchere6869a82000-03-20 06:03:29 +00001399static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001400print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001401{
Rusty Russell228e98d2000-04-27 10:28:06 +00001402 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001403 return 0;
1404}
1405
Jan Engelhardtfd187312008-11-10 16:59:27 +01001406static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001407
Marc Bouchere6869a82000-03-20 06:03:29 +00001408void
Jan Engelhardtfd187312008-11-10 16:59:27 +01001409TC_DUMP_ENTRIES(struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001410{
Derrik Pates664c0a32005-02-01 13:28:14 +00001411 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001412 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001413
Rusty Russelle45c7132004-12-16 13:21:44 +00001414 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001415 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001416 printf("Table `%s'\n", handle->info.name);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001417 printf("Hooks: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001418 handle->info.hook_entry[HOOK_PRE_ROUTING],
1419 handle->info.hook_entry[HOOK_LOCAL_IN],
1420 handle->info.hook_entry[HOOK_FORWARD],
1421 handle->info.hook_entry[HOOK_LOCAL_OUT],
1422 handle->info.hook_entry[HOOK_POST_ROUTING]);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001423 printf("Underflows: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001424 handle->info.underflow[HOOK_PRE_ROUTING],
1425 handle->info.underflow[HOOK_LOCAL_IN],
1426 handle->info.underflow[HOOK_FORWARD],
1427 handle->info.underflow[HOOK_LOCAL_OUT],
1428 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001429
Harald Welteaae69be2004-08-29 23:32:14 +00001430 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001431 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001432}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001433
Marc Bouchere6869a82000-03-20 06:03:29 +00001434/* Does this chain exist? */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001435int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001436{
Derrik Pates664c0a32005-02-01 13:28:14 +00001437 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001438 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001439}
1440
Jan Engelhardtfd187312008-11-10 16:59:27 +01001441static void iptcc_chain_iterator_advance(struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001442{
1443 struct chain_head *c = handle->chain_iterator_cur;
1444
1445 if (c->list.next == &handle->chains)
1446 handle->chain_iterator_cur = NULL;
1447 else
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001448 handle->chain_iterator_cur =
Harald Welteaae69be2004-08-29 23:32:14 +00001449 list_entry(c->list.next, struct chain_head, list);
1450}
Marc Bouchere6869a82000-03-20 06:03:29 +00001451
Rusty Russell30fd6e52000-04-23 09:16:06 +00001452/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001453const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001454TC_FIRST_CHAIN(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001455{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001456 struct chain_head *c = list_entry(handle->chains.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001457 struct chain_head, list);
1458
1459 iptc_fn = TC_FIRST_CHAIN;
1460
1461
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001462 if (list_empty(&handle->chains)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001463 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001464 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001465 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001466
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001467 handle->chain_iterator_cur = c;
1468 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001469
Harald Welteaae69be2004-08-29 23:32:14 +00001470 DEBUGP(": returning `%s'\n", c->name);
1471 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001472}
1473
Rusty Russell30fd6e52000-04-23 09:16:06 +00001474/* Iterator functions to run through the chains. Returns NULL at end. */
1475const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001476TC_NEXT_CHAIN(struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001477{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001478 struct chain_head *c = handle->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001479
Harald Welteaae69be2004-08-29 23:32:14 +00001480 iptc_fn = TC_NEXT_CHAIN;
1481
1482 if (!c) {
1483 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001484 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001485 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001486
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001487 iptcc_chain_iterator_advance(handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001488
Harald Welteaae69be2004-08-29 23:32:14 +00001489 DEBUGP(": returning `%s'\n", c->name);
1490 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001491}
1492
1493/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001494const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001495TC_FIRST_RULE(const char *chain, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001496{
Harald Welteaae69be2004-08-29 23:32:14 +00001497 struct chain_head *c;
1498 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001499
Harald Welteaae69be2004-08-29 23:32:14 +00001500 iptc_fn = TC_FIRST_RULE;
1501
1502 DEBUGP("first rule(%s): ", chain);
1503
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001504 c = iptcc_find_label(chain, handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001505 if (!c) {
1506 errno = ENOENT;
1507 return NULL;
1508 }
1509
1510 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001511 if (list_empty(&c->rules)) {
1512 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001513 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001514 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001515
Harald Welteaae69be2004-08-29 23:32:14 +00001516 r = list_entry(c->rules.next, struct rule_head, list);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001517 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001518 DEBUGP_C("%p\n", r);
1519
1520 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001521}
1522
1523/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001524const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001525TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001526{
Harald Welteaae69be2004-08-29 23:32:14 +00001527 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001528
Derrik Pates664c0a32005-02-01 13:28:14 +00001529 iptc_fn = TC_NEXT_RULE;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001530 DEBUGP("rule_iterator_cur=%p...", handle->rule_iterator_cur);
Harald Welteaae69be2004-08-29 23:32:14 +00001531
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001532 if (handle->rule_iterator_cur == NULL) {
Harald Welteaae69be2004-08-29 23:32:14 +00001533 DEBUGP_C("returning NULL\n");
1534 return NULL;
1535 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001536
1537 r = list_entry(handle->rule_iterator_cur->list.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001538 struct rule_head, list);
1539
1540 iptc_fn = TC_NEXT_RULE;
1541
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001542 DEBUGP_C("next=%p, head=%p...", &r->list,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001543 &handle->rule_iterator_cur->chain->rules);
Harald Welteaae69be2004-08-29 23:32:14 +00001544
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001545 if (&r->list == &handle->rule_iterator_cur->chain->rules) {
1546 handle->rule_iterator_cur = NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001547 DEBUGP_C("finished, returning NULL\n");
1548 return NULL;
1549 }
1550
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001551 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001552
1553 /* NOTE: prev is without any influence ! */
1554 DEBUGP_C("returning rule %p\n", r);
1555 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001556}
1557
Marc Bouchere6869a82000-03-20 06:03:29 +00001558/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001559static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001560{
Harald Welteaae69be2004-08-29 23:32:14 +00001561 switch (verdict) {
1562 case RETURN:
1563 return LABEL_RETURN;
1564 break;
1565 case -NF_ACCEPT-1:
1566 return LABEL_ACCEPT;
1567 break;
1568 case -NF_DROP-1:
1569 return LABEL_DROP;
1570 break;
1571 case -NF_QUEUE-1:
1572 return LABEL_QUEUE;
1573 break;
1574 default:
1575 fprintf(stderr, "ERROR: %d not a valid target)\n",
1576 verdict);
1577 abort();
1578 break;
1579 }
1580 /* not reached */
1581 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001582}
1583
Harald Welteaae69be2004-08-29 23:32:14 +00001584/* Returns a pointer to the target name of this position. */
1585const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001586 struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001587{
1588 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001589 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Jan Engelhardt51651b62009-10-23 23:35:49 +02001590 const unsigned char *data;
Harald Welteaae69be2004-08-29 23:32:14 +00001591
1592 iptc_fn = TC_GET_TARGET;
1593
1594 switch(r->type) {
1595 int spos;
1596 case IPTCC_R_FALLTHROUGH:
1597 return "";
1598 break;
1599 case IPTCC_R_JUMP:
1600 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1601 return r->jump->name;
1602 break;
1603 case IPTCC_R_STANDARD:
Jan Engelhardt51651b62009-10-23 23:35:49 +02001604 data = GET_TARGET(e)->data;
1605 spos = *(const int *)data;
Harald Welteaae69be2004-08-29 23:32:14 +00001606 DEBUGP("r=%p, spos=%d'\n", r, spos);
1607 return standard_target_map(spos);
1608 break;
1609 case IPTCC_R_MODULE:
1610 return GET_TARGET(e)->u.user.name;
1611 break;
1612 }
1613 return NULL;
1614}
Marc Bouchere6869a82000-03-20 06:03:29 +00001615/* Is this a built-in chain? Actually returns hook + 1. */
1616int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001617TC_BUILTIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001618{
Harald Welteaae69be2004-08-29 23:32:14 +00001619 struct chain_head *c;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001620
Harald Welteaae69be2004-08-29 23:32:14 +00001621 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001622
Harald Welteaae69be2004-08-29 23:32:14 +00001623 c = iptcc_find_label(chain, handle);
1624 if (!c) {
1625 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001626 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001627 }
Harald Welteaae69be2004-08-29 23:32:14 +00001628
1629 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001630}
1631
1632/* Get the policy of a given built-in chain */
1633const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001634TC_GET_POLICY(const char *chain,
1635 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001636 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001637{
Harald Welteaae69be2004-08-29 23:32:14 +00001638 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001639
Harald Welteaae69be2004-08-29 23:32:14 +00001640 iptc_fn = TC_GET_POLICY;
1641
1642 DEBUGP("called for chain %s\n", chain);
1643
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001644 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001645 if (!c) {
1646 errno = ENOENT;
1647 return NULL;
1648 }
1649
1650 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001651 return NULL;
1652
Harald Welteaae69be2004-08-29 23:32:14 +00001653 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001654
Harald Welteaae69be2004-08-29 23:32:14 +00001655 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001656}
1657
1658static int
Harald Welteaae69be2004-08-29 23:32:14 +00001659iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001660{
Harald Welteaae69be2004-08-29 23:32:14 +00001661 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001662 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001663
Rusty Russell79dee072000-05-02 16:45:16 +00001664 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001665
Rusty Russell67088e72000-05-10 01:18:57 +00001666 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001667 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001668 errno = EINVAL;
1669 return 0;
1670 }
1671 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001672 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1673 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001674 t->verdict = verdict;
1675
Harald Welteaae69be2004-08-29 23:32:14 +00001676 r->type = IPTCC_R_STANDARD;
1677
Marc Bouchere6869a82000-03-20 06:03:29 +00001678 return 1;
1679}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001680
Marc Bouchere6869a82000-03-20 06:03:29 +00001681static int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001682iptcc_map_target(struct xtc_handle *const handle,
Harald Welteaae69be2004-08-29 23:32:14 +00001683 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001684{
Harald Welteaae69be2004-08-29 23:32:14 +00001685 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001686 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001687
Marc Bouchere6869a82000-03-20 06:03:29 +00001688 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001689 if (strcmp(t->u.user.name, "") == 0) {
1690 r->type = IPTCC_R_FALLTHROUGH;
1691 return 1;
1692 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001693 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001694 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001695 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001696 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001697 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001698 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001699 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001700 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001701 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001702 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001703 /* Can't jump to builtins. */
1704 errno = EINVAL;
1705 return 0;
1706 } else {
1707 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001708 struct chain_head *c;
1709 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001710
Harald Welteaae69be2004-08-29 23:32:14 +00001711 c = iptcc_find_label(t->u.user.name, handle);
1712 if (c) {
1713 DEBUGP_C("found!\n");
1714 r->type = IPTCC_R_JUMP;
1715 r->jump = c;
1716 c->references++;
1717 return 1;
1718 }
1719 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001720 }
1721
1722 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001723 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001724 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001725 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001726 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001727 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001728 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001729 return 1;
1730}
1731
Harald Welte0113fe72004-01-06 19:04:02 +00001732/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001733int
Rusty Russell79dee072000-05-02 16:45:16 +00001734TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1735 const STRUCT_ENTRY *e,
1736 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001737 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001738{
Harald Welteaae69be2004-08-29 23:32:14 +00001739 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001740 struct rule_head *r;
1741 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001742
Rusty Russell79dee072000-05-02 16:45:16 +00001743 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001744
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001745 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001746 errno = ENOENT;
1747 return 0;
1748 }
1749
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001750 /* first rulenum index = 0
1751 first c->num_rules index = 1 */
1752 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001753 errno = E2BIG;
1754 return 0;
1755 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001756
Martin Josefsson631f3612004-09-23 19:25:06 +00001757 /* If we are inserting at the end just take advantage of the
1758 double linked list, insert will happen before the entry
1759 prev points to. */
1760 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001761 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001762 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001763 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001764 prev = &r->list;
1765 } else {
1766 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001767 prev = &r->list;
1768 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001769
Harald Welteaae69be2004-08-29 23:32:14 +00001770 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1771 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001772 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001773 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001774
Harald Welteaae69be2004-08-29 23:32:14 +00001775 memcpy(r->entry, e, e->next_offset);
1776 r->counter_map.maptype = COUNTER_MAP_SET;
1777
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001778 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001779 free(r);
1780 return 0;
1781 }
1782
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001783 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001784 c->num_rules++;
1785
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001786 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001787
1788 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001789}
1790
1791/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1792int
Rusty Russell79dee072000-05-02 16:45:16 +00001793TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1794 const STRUCT_ENTRY *e,
1795 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001796 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001797{
Harald Welteaae69be2004-08-29 23:32:14 +00001798 struct chain_head *c;
1799 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001800
Rusty Russell79dee072000-05-02 16:45:16 +00001801 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001802
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001803 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001804 errno = ENOENT;
1805 return 0;
1806 }
1807
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001808 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001809 errno = E2BIG;
1810 return 0;
1811 }
1812
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001813 /* Take advantage of the double linked list if possible. */
1814 if (rulenum + 1 <= c->num_rules/2) {
1815 old = iptcc_get_rule_num(c, rulenum + 1);
1816 } else {
1817 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1818 }
1819
Harald Welteaae69be2004-08-29 23:32:14 +00001820 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1821 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001822 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001823 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001824
Harald Welteaae69be2004-08-29 23:32:14 +00001825 memcpy(r->entry, e, e->next_offset);
1826 r->counter_map.maptype = COUNTER_MAP_SET;
1827
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001828 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001829 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001830 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001831 }
Harald Welte0113fe72004-01-06 19:04:02 +00001832
Harald Welteaae69be2004-08-29 23:32:14 +00001833 list_add(&r->list, &old->list);
1834 iptcc_delete_rule(old);
1835
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001836 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001837
1838 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001839}
1840
Harald Welte0113fe72004-01-06 19:04:02 +00001841/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001842 rulenum = length of chain. */
1843int
Rusty Russell79dee072000-05-02 16:45:16 +00001844TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1845 const STRUCT_ENTRY *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001846 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001847{
Harald Welteaae69be2004-08-29 23:32:14 +00001848 struct chain_head *c;
1849 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001850
Rusty Russell79dee072000-05-02 16:45:16 +00001851 iptc_fn = TC_APPEND_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001852 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00001853 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001854 errno = ENOENT;
1855 return 0;
1856 }
1857
Harald Welteaae69be2004-08-29 23:32:14 +00001858 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1859 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1860 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001861 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001862 }
Harald Welte0113fe72004-01-06 19:04:02 +00001863
Harald Welteaae69be2004-08-29 23:32:14 +00001864 memcpy(r->entry, e, e->next_offset);
1865 r->counter_map.maptype = COUNTER_MAP_SET;
1866
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001867 if (!iptcc_map_target(handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001868 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001869 free(r);
1870 return 0;
1871 }
1872
1873 list_add_tail(&r->list, &c->rules);
1874 c->num_rules++;
1875
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001876 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001877
1878 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001879}
1880
1881static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001882match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001883 const unsigned char *a_elems,
1884 const unsigned char *b_elems,
1885 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001886{
Rusty Russell79dee072000-05-02 16:45:16 +00001887 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001888 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001889
1890 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001891 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001892
Rusty Russell228e98d2000-04-27 10:28:06 +00001893 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001894 return 1;
1895
Rusty Russell228e98d2000-04-27 10:28:06 +00001896 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001897 return 1;
1898
Rusty Russell73ef09b2000-07-03 10:24:04 +00001899 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001900
Rusty Russell73ef09b2000-07-03 10:24:04 +00001901 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001902 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001903 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001904 *maskptr += i;
1905 return 0;
1906}
1907
1908static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001909target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001910{
1911 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001912 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001913
Rusty Russell733e54b2004-12-16 14:22:23 +00001914 if (a->type != b->type)
1915 return 0;
1916
1917 ta = GET_TARGET(a->entry);
1918 tb = GET_TARGET(b->entry);
1919
1920 switch (a->type) {
1921 case IPTCC_R_FALLTHROUGH:
1922 return 1;
1923 case IPTCC_R_JUMP:
1924 return a->jump == b->jump;
1925 case IPTCC_R_STANDARD:
1926 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1927 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1928 case IPTCC_R_MODULE:
1929 if (ta->u.target_size != tb->u.target_size)
1930 return 0;
1931 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1932 return 0;
1933
1934 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001935 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001936 return 0;
1937 return 1;
1938 default:
1939 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1940 abort();
1941 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001942}
1943
Rusty Russell733e54b2004-12-16 14:22:23 +00001944static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001945is_same(const STRUCT_ENTRY *a,
1946 const STRUCT_ENTRY *b,
1947 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001948
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001949
1950/* find the first rule in `chain' which matches `fw' and remove it unless dry_run is set */
1951static int delete_entry(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
1952 unsigned char *matchmask, struct xtc_handle *handle,
1953 bool dry_run)
Marc Bouchere6869a82000-03-20 06:03:29 +00001954{
Harald Welteaae69be2004-08-29 23:32:14 +00001955 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001956 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001957
Rusty Russell79dee072000-05-02 16:45:16 +00001958 iptc_fn = TC_DELETE_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001959 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001960 errno = ENOENT;
1961 return 0;
1962 }
1963
Rusty Russelle45c7132004-12-16 13:21:44 +00001964 /* Create a rule_head from origfw. */
1965 r = iptcc_alloc_rule(c, origfw->next_offset);
1966 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001967 errno = ENOMEM;
1968 return 0;
1969 }
1970
Rusty Russelle45c7132004-12-16 13:21:44 +00001971 memcpy(r->entry, origfw, origfw->next_offset);
1972 r->counter_map.maptype = COUNTER_MAP_NOMAP;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001973 if (!iptcc_map_target(handle, r)) {
Rusty Russelle45c7132004-12-16 13:21:44 +00001974 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1975 free(r);
1976 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001977 } else {
1978 /* iptcc_map_target increment target chain references
1979 * since this is a fake rule only used for matching
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001980 * the chain references count is decremented again.
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001981 */
1982 if (r->type == IPTCC_R_JUMP
1983 && r->jump)
1984 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001985 }
Harald Welte0113fe72004-01-06 19:04:02 +00001986
Rusty Russelle45c7132004-12-16 13:21:44 +00001987 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001988 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001989
Rusty Russell733e54b2004-12-16 14:22:23 +00001990 mask = is_same(r->entry, i->entry, matchmask);
1991 if (!mask)
1992 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00001993
Rusty Russell733e54b2004-12-16 14:22:23 +00001994 if (!target_same(r, i, mask))
1995 continue;
1996
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001997 /* if we are just doing a dry run, we simply skip the rest */
1998 if (dry_run)
1999 return 1;
2000
Rusty Russell733e54b2004-12-16 14:22:23 +00002001 /* If we are about to delete the rule that is the
2002 * current iterator, move rule iterator back. next
2003 * pointer will then point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002004 if (i == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002005 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002006 list_entry(handle->rule_iterator_cur->list.prev,
Rusty Russell733e54b2004-12-16 14:22:23 +00002007 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00002008 }
Rusty Russell733e54b2004-12-16 14:22:23 +00002009
2010 c->num_rules--;
2011 iptcc_delete_rule(i);
2012
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002013 set_changed(handle);
Rusty Russell733e54b2004-12-16 14:22:23 +00002014 free(r);
2015 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002016 }
2017
Rusty Russelle45c7132004-12-16 13:21:44 +00002018 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00002019 errno = ENOENT;
2020 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002021}
Harald Welteaae69be2004-08-29 23:32:14 +00002022
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01002023/* check whether a specified rule is present */
2024int TC_CHECK_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
2025 unsigned char *matchmask, struct xtc_handle *handle)
2026{
2027 /* do a dry-run delete to find out whether a matching rule exists */
2028 return delete_entry(chain, origfw, matchmask, handle, true);
2029}
2030
2031/* Delete the first rule in `chain' which matches `fw'. */
2032int TC_DELETE_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
2033 unsigned char *matchmask, struct xtc_handle *handle)
2034{
2035 return delete_entry(chain, origfw, matchmask, handle, false);
2036}
Marc Bouchere6869a82000-03-20 06:03:29 +00002037
2038/* Delete the rule in position `rulenum' in `chain'. */
2039int
Rusty Russell79dee072000-05-02 16:45:16 +00002040TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
2041 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002042 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002043{
Harald Welteaae69be2004-08-29 23:32:14 +00002044 struct chain_head *c;
2045 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002046
Rusty Russell79dee072000-05-02 16:45:16 +00002047 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002048
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002049 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002050 errno = ENOENT;
2051 return 0;
2052 }
2053
Martin Josefssona5616dc2004-10-24 22:27:31 +00002054 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00002055 errno = E2BIG;
2056 return 0;
2057 }
2058
2059 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00002060 if (rulenum + 1 <= c->num_rules/2) {
2061 r = iptcc_get_rule_num(c, rulenum + 1);
2062 } else {
2063 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002064 }
2065
Harald Welteaae69be2004-08-29 23:32:14 +00002066 /* If we are about to delete the rule that is the current
2067 * iterator, move rule iterator back. next pointer will then
2068 * point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002069 if (r == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002070 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002071 list_entry(handle->rule_iterator_cur->list.prev,
Harald Welteaae69be2004-08-29 23:32:14 +00002072 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002073 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002074
Harald Welteaae69be2004-08-29 23:32:14 +00002075 c->num_rules--;
2076 iptcc_delete_rule(r);
2077
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002078 set_changed(handle);
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002079
Harald Welteaae69be2004-08-29 23:32:14 +00002080 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002081}
2082
Marc Bouchere6869a82000-03-20 06:03:29 +00002083/* Flushes the entries in the given chain (ie. empties chain). */
2084int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002085TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002086{
Harald Welteaae69be2004-08-29 23:32:14 +00002087 struct chain_head *c;
2088 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002089
Harald Welte0113fe72004-01-06 19:04:02 +00002090 iptc_fn = TC_FLUSH_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002091 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002092 errno = ENOENT;
2093 return 0;
2094 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002095
Harald Welteaae69be2004-08-29 23:32:14 +00002096 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2097 iptcc_delete_rule(r);
2098 }
2099
2100 c->num_rules = 0;
2101
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002102 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002103
2104 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002105}
2106
2107/* Zeroes the counters in a chain. */
2108int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002109TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002110{
Harald Welteaae69be2004-08-29 23:32:14 +00002111 struct chain_head *c;
2112 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002113
Derrik Pates664c0a32005-02-01 13:28:14 +00002114 iptc_fn = TC_ZERO_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002115 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002116 errno = ENOENT;
2117 return 0;
2118 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002119
Andy Gaye5bd1d72006-08-22 02:56:41 +00002120 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2121 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2122
Harald Welteaae69be2004-08-29 23:32:14 +00002123 list_for_each_entry(r, &c->rules, list) {
2124 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2125 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002126 }
Harald Welteaae69be2004-08-29 23:32:14 +00002127
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002128 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002129
Marc Bouchere6869a82000-03-20 06:03:29 +00002130 return 1;
2131}
2132
Harald Welte1cef74d2001-01-05 15:22:59 +00002133STRUCT_COUNTERS *
2134TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2135 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002136 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002137{
Harald Welteaae69be2004-08-29 23:32:14 +00002138 struct chain_head *c;
2139 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002140
2141 iptc_fn = TC_READ_COUNTER;
2142 CHECK(*handle);
2143
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002144 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002145 errno = ENOENT;
2146 return NULL;
2147 }
2148
Harald Welteaae69be2004-08-29 23:32:14 +00002149 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002150 errno = E2BIG;
2151 return NULL;
2152 }
2153
Harald Welteaae69be2004-08-29 23:32:14 +00002154 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002155}
2156
2157int
2158TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2159 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002160 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002161{
Harald Welteaae69be2004-08-29 23:32:14 +00002162 struct chain_head *c;
2163 struct rule_head *r;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002164
Harald Welte1cef74d2001-01-05 15:22:59 +00002165 iptc_fn = TC_ZERO_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002166 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002167
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002168 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002169 errno = ENOENT;
2170 return 0;
2171 }
2172
Harald Welteaae69be2004-08-29 23:32:14 +00002173 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002174 errno = E2BIG;
2175 return 0;
2176 }
2177
Harald Welteaae69be2004-08-29 23:32:14 +00002178 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2179 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002180
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002181 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002182
2183 return 1;
2184}
2185
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002186int
Harald Welte1cef74d2001-01-05 15:22:59 +00002187TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2188 unsigned int rulenum,
2189 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002190 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002191{
Harald Welteaae69be2004-08-29 23:32:14 +00002192 struct chain_head *c;
2193 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002194 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002195
2196 iptc_fn = TC_SET_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002197 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002198
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002199 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002200 errno = ENOENT;
2201 return 0;
2202 }
Harald Welte0113fe72004-01-06 19:04:02 +00002203
Harald Welteaae69be2004-08-29 23:32:14 +00002204 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002205 errno = E2BIG;
2206 return 0;
2207 }
2208
Harald Welteaae69be2004-08-29 23:32:14 +00002209 e = r->entry;
2210 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002211
2212 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002213
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002214 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002215
2216 return 1;
2217}
2218
Marc Bouchere6869a82000-03-20 06:03:29 +00002219/* Creates a new chain. */
2220/* To create a chain, create two rules: error node and unconditional
2221 * return. */
2222int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002223TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002224{
Harald Welteaae69be2004-08-29 23:32:14 +00002225 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002226 int capacity;
2227 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002228
Rusty Russell79dee072000-05-02 16:45:16 +00002229 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002230
2231 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2232 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002233 if (iptcc_find_label(chain, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002234 || strcmp(chain, LABEL_DROP) == 0
2235 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002236 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002237 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002238 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002239 errno = EEXIST;
2240 return 0;
2241 }
2242
Rusty Russell79dee072000-05-02 16:45:16 +00002243 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002244 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002245 errno = EINVAL;
2246 return 0;
2247 }
2248
Harald Welteaae69be2004-08-29 23:32:14 +00002249 c = iptcc_alloc_chain_head(chain, 0);
2250 if (!c) {
2251 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2252 errno = ENOMEM;
2253 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002254
Harald Welteaae69be2004-08-29 23:32:14 +00002255 }
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002256 handle->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002257
Harald Welteaae69be2004-08-29 23:32:14 +00002258 DEBUGP("Creating chain `%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002259 iptc_insert_chain(handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002260
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002261 /* Inserting chains don't change the correctness of the chain
2262 * index (except if its smaller than index[0], but that
2263 * handled by iptc_insert_chain). It only causes longer lists
2264 * in the buckets. Thus, only rebuild chain index when the
2265 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2266 */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002267 capacity = handle->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2268 exceeded = handle->num_chains - capacity;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002269 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2270 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002271 capacity, exceeded, handle->num_chains);
2272 iptcc_chain_index_rebuild(handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002273 }
2274
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002275 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002276
Harald Welteaae69be2004-08-29 23:32:14 +00002277 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002278}
2279
2280/* Get the number of references to this chain. */
2281int
Rusty Russell79dee072000-05-02 16:45:16 +00002282TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002283 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002284{
Harald Welteaae69be2004-08-29 23:32:14 +00002285 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002286
Derrik Pates664c0a32005-02-01 13:28:14 +00002287 iptc_fn = TC_GET_REFERENCES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002288 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002289 errno = ENOENT;
2290 return 0;
2291 }
2292
Harald Welteaae69be2004-08-29 23:32:14 +00002293 *ref = c->references;
2294
Marc Bouchere6869a82000-03-20 06:03:29 +00002295 return 1;
2296}
2297
2298/* Deletes a chain. */
2299int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002300TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002301{
Marc Bouchere6869a82000-03-20 06:03:29 +00002302 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002303 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002304
Rusty Russell79dee072000-05-02 16:45:16 +00002305 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002306
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002307 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002308 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002309 errno = ENOENT;
2310 return 0;
2311 }
2312
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002313 if (TC_BUILTIN(chain, handle)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002314 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2315 errno = EINVAL;
2316 return 0;
2317 }
2318
2319 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2320 DEBUGP("cannot get references on chain `%s'\n", chain);
2321 return 0;
2322 }
2323
2324 if (references > 0) {
2325 DEBUGP("chain `%s' still has references\n", chain);
2326 errno = EMLINK;
2327 return 0;
2328 }
2329
2330 if (c->num_rules) {
2331 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002332 errno = ENOTEMPTY;
2333 return 0;
2334 }
2335
Harald Welteaae69be2004-08-29 23:32:14 +00002336 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002337 * iterator, move chain iterator forward. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002338 if (c == handle->chain_iterator_cur)
2339 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002340
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002341 handle->num_chains--; /* One user defined chain deleted */
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002342
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002343 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002344 iptcc_chain_index_delete_chain(c, handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002345 free(c);
2346
Harald Welteaae69be2004-08-29 23:32:14 +00002347 DEBUGP("chain `%s' deleted\n", chain);
2348
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002349 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002350
2351 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002352}
2353
2354/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002355int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2356 const IPT_CHAINLABEL newname,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002357 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002358{
Harald Welteaae69be2004-08-29 23:32:14 +00002359 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002360 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002361
Harald Welte1de80462000-10-30 12:00:27 +00002362 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2363 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002364 if (iptcc_find_label(newname, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002365 || strcmp(newname, LABEL_DROP) == 0
2366 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002367 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002368 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002369 errno = EEXIST;
2370 return 0;
2371 }
2372
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002373 if (!(c = iptcc_find_label(oldname, handle))
2374 || TC_BUILTIN(oldname, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002375 errno = ENOENT;
2376 return 0;
2377 }
2378
Rusty Russell79dee072000-05-02 16:45:16 +00002379 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002380 errno = EINVAL;
2381 return 0;
2382 }
2383
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002384 /* This only unlinks "c" from the list, thus no free(c) */
2385 iptcc_chain_index_delete_chain(c, handle);
2386
2387 /* Change the name of the chain */
Harald Welteaae69be2004-08-29 23:32:14 +00002388 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002389
2390 /* Insert sorted into to list again */
2391 iptc_insert_chain(handle, c);
2392
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002393 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002394
Marc Bouchere6869a82000-03-20 06:03:29 +00002395 return 1;
2396}
2397
2398/* Sets the policy on a built-in chain. */
2399int
Rusty Russell79dee072000-05-02 16:45:16 +00002400TC_SET_POLICY(const IPT_CHAINLABEL chain,
2401 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002402 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002403 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002404{
Harald Welteaae69be2004-08-29 23:32:14 +00002405 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002406
Rusty Russell79dee072000-05-02 16:45:16 +00002407 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002408
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002409 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002410 DEBUGP("cannot find chain `%s'\n", chain);
2411 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002412 return 0;
2413 }
2414
Harald Welteaae69be2004-08-29 23:32:14 +00002415 if (!iptcc_is_builtin(c)) {
2416 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2417 errno = ENOENT;
2418 return 0;
2419 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002420
Rusty Russell79dee072000-05-02 16:45:16 +00002421 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002422 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002423 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002424 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002425 else {
2426 errno = EINVAL;
2427 return 0;
2428 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002429
Harald Welte1cef74d2001-01-05 15:22:59 +00002430 if (counters) {
2431 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002432 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2433 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002434 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002435 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002436 }
2437
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002438 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002439
Marc Bouchere6869a82000-03-20 06:03:29 +00002440 return 1;
2441}
2442
2443/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002444 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002445 libiptc.c:833: fixed or forbidden register was spilled.
2446 This may be due to a compiler bug or to impossible asm
2447 statements or clauses.
2448*/
2449static void
Rusty Russell79dee072000-05-02 16:45:16 +00002450subtract_counters(STRUCT_COUNTERS *answer,
2451 const STRUCT_COUNTERS *a,
2452 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002453{
2454 answer->pcnt = a->pcnt - b->pcnt;
2455 answer->bcnt = a->bcnt - b->bcnt;
2456}
2457
Harald Welteaae69be2004-08-29 23:32:14 +00002458
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002459static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002460{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002461 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002462 DEBUGP_C("NOMAP => zero\n");
2463}
2464
2465static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002466 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002467 unsigned int mappos)
2468{
2469 /* Original read: X.
2470 * Atomic read on replacement: X + Y.
2471 * Currently in kernel: Z.
2472 * Want in kernel: X + Y + Z.
2473 * => Add in X + Y
2474 * => Add in replacement read.
2475 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002476 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002477 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2478}
2479
2480static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002481 STRUCT_REPLACE *repl, unsigned int idx,
2482 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002483{
2484 /* Original read: X.
2485 * Atomic read on replacement: X + Y.
2486 * Currently in kernel: Z.
2487 * Want in kernel: Y + Z.
2488 * => Add in Y.
2489 * => Add in (replacement read - original read).
2490 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002491 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002492 &repl->counters[mappos],
2493 counters);
2494 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2495}
2496
2497static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002498 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002499{
2500 /* Want to set counter (iptables-restore) */
2501
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002502 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002503 sizeof(STRUCT_COUNTERS));
2504
2505 DEBUGP_C("SET\n");
2506}
2507
2508
Marc Bouchere6869a82000-03-20 06:03:29 +00002509int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002510TC_COMMIT(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002511{
2512 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002513 STRUCT_REPLACE *repl;
2514 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002515 struct chain_head *c;
2516 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002517 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002518 int new_number;
2519 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002520
Derrik Pates664c0a32005-02-01 13:28:14 +00002521 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002522 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002523
Marc Bouchere6869a82000-03-20 06:03:29 +00002524 /* Don't commit if nothing changed. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002525 if (!handle->changed)
Marc Bouchere6869a82000-03-20 06:03:29 +00002526 goto finished;
2527
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002528 new_number = iptcc_compile_table_prep(handle, &new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002529 if (new_number < 0) {
2530 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002531 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002532 }
2533
2534 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002535 if (!repl) {
2536 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002537 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002538 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002539 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002540
Rusty Russelle45c7132004-12-16 13:21:44 +00002541#if 0
2542 TC_DUMP_ENTRIES(*handle);
2543#endif
2544
Harald Welteaae69be2004-08-29 23:32:14 +00002545 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2546 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002547
2548 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002549 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002550 * handle->info.num_entries);
Marc Bouchere6869a82000-03-20 06:03:29 +00002551 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002552 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002553 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002554 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002555 /* These are the counters we're going to put back, later. */
2556 newcounters = malloc(counterlen);
2557 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002558 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002559 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002560 }
Harald Welteaae69be2004-08-29 23:32:14 +00002561 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002562
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002563 strcpy(repl->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002564 repl->num_entries = new_number;
2565 repl->size = new_size;
2566
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002567 repl->num_counters = handle->info.num_entries;
2568 repl->valid_hooks = handle->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002569
2570 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2571 repl->num_entries, repl->size, repl->num_counters);
2572
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002573 ret = iptcc_compile_table(handle, repl);
Harald Welteaae69be2004-08-29 23:32:14 +00002574 if (ret < 0) {
2575 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002576 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002577 }
2578
2579
2580#ifdef IPTC_DEBUG2
2581 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002582 int fd = open("/tmp/libiptc-so_set_replace.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00002583 O_CREAT|O_WRONLY);
2584 if (fd >= 0) {
2585 write(fd, repl, sizeof(*repl) + repl->size);
2586 close(fd);
2587 }
2588 }
2589#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002590
Jan Engelhardt175f4512008-11-10 17:25:55 +01002591 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welted6ba6f52005-11-12 10:39:40 +00002592 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002593 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002594 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002595
2596 /* Put counters back. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002597 strcpy(newcounters->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002598 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002599
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002600 list_for_each_entry(c, &handle->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00002601 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002602
Harald Welteaae69be2004-08-29 23:32:14 +00002603 /* Builtin chains have their own counters */
2604 if (iptcc_is_builtin(c)) {
2605 DEBUGP("counter for chain-index %u: ", c->foot_index);
2606 switch(c->counter_map.maptype) {
2607 case COUNTER_MAP_NOMAP:
2608 counters_nomap(newcounters, c->foot_index);
2609 break;
2610 case COUNTER_MAP_NORMAL_MAP:
2611 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002612 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002613 c->counter_map.mappos);
2614 break;
2615 case COUNTER_MAP_ZEROED:
2616 counters_map_zeroed(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002617 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002618 c->counter_map.mappos,
2619 &c->counters);
2620 break;
2621 case COUNTER_MAP_SET:
2622 counters_map_set(newcounters, c->foot_index,
2623 &c->counters);
2624 break;
2625 }
2626 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002627
Harald Welteaae69be2004-08-29 23:32:14 +00002628 list_for_each_entry(r, &c->rules, list) {
2629 DEBUGP("counter for index %u: ", r->index);
2630 switch (r->counter_map.maptype) {
2631 case COUNTER_MAP_NOMAP:
2632 counters_nomap(newcounters, r->index);
2633 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002634
Harald Welteaae69be2004-08-29 23:32:14 +00002635 case COUNTER_MAP_NORMAL_MAP:
2636 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002637 r->index,
Harald Welteaae69be2004-08-29 23:32:14 +00002638 r->counter_map.mappos);
2639 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002640
Harald Welteaae69be2004-08-29 23:32:14 +00002641 case COUNTER_MAP_ZEROED:
2642 counters_map_zeroed(newcounters, repl,
2643 r->index,
2644 r->counter_map.mappos,
2645 &r->entry->counters);
2646 break;
2647
2648 case COUNTER_MAP_SET:
2649 counters_map_set(newcounters, r->index,
2650 &r->entry->counters);
2651 break;
2652 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002653 }
2654 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002655
Harald Welteaae69be2004-08-29 23:32:14 +00002656#ifdef IPTC_DEBUG2
2657 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002658 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00002659 O_CREAT|O_WRONLY);
2660 if (fd >= 0) {
2661 write(fd, newcounters, counterlen);
2662 close(fd);
2663 }
2664 }
2665#endif
2666
Jan Engelhardt175f4512008-11-10 17:25:55 +01002667 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
Harald Welted6ba6f52005-11-12 10:39:40 +00002668 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002669 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002670 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002671
2672 free(repl->counters);
2673 free(repl);
2674 free(newcounters);
2675
Harald Welted6ba6f52005-11-12 10:39:40 +00002676finished:
Marc Bouchere6869a82000-03-20 06:03:29 +00002677 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002678
2679out_free_newcounters:
2680 free(newcounters);
2681out_free_repl_counters:
2682 free(repl->counters);
2683out_free_repl:
2684 free(repl);
2685out_zero:
2686 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002687}
2688
Marc Bouchere6869a82000-03-20 06:03:29 +00002689/* Translates errno numbers into more human-readable form than strerror. */
2690const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002691TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002692{
2693 unsigned int i;
2694 struct table_struct {
2695 void *fn;
2696 int err;
2697 const char *message;
2698 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002699 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002700 { TC_INIT, EINVAL, "Module is wrong version" },
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002701 { TC_INIT, ENOENT,
Harald Welte4ccfa632001-07-30 15:12:43 +00002702 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002703 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2704 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2705 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002706 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002707 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2708 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2709 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2710 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002711 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2712 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002713 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2714 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002715 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002716 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002717 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002718 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002719 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002720 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002721 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002722
2723 { NULL, 0, "Incompatible with this kernel" },
2724 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2725 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2726 { NULL, ENOMEM, "Memory allocation problem" },
2727 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002728 };
2729
2730 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2731 if ((!table[i].fn || table[i].fn == iptc_fn)
2732 && table[i].err == err)
2733 return table[i].message;
2734 }
2735
2736 return strerror(err);
2737}