blob: 4c3437e3c7fa0d20084fd81ea8f50bd39afd1d89 [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>
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020034#include <xtables.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000035
Harald Welteaae69be2004-08-29 23:32:14 +000036#include "linux_list.h"
37
38//#define IPTC_DEBUG2 1
39
40#ifdef IPTC_DEBUG2
41#include <fcntl.h>
42#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
43#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
44#else
45#define DEBUGP(x, args...)
46#define DEBUGP_C(x, args...)
47#endif
48
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +000049#ifdef DEBUG
50#define debug(x, args...) fprintf(stderr, x, ## args)
51#else
52#define debug(x, args...)
53#endif
54
Marc Bouchere6869a82000-03-20 06:03:29 +000055static void *iptc_fn = NULL;
56
Patrick McHardy0b639362007-09-08 16:00:01 +000057static const char *hooknames[] = {
58 [HOOK_PRE_ROUTING] = "PREROUTING",
59 [HOOK_LOCAL_IN] = "INPUT",
60 [HOOK_FORWARD] = "FORWARD",
61 [HOOK_LOCAL_OUT] = "OUTPUT",
62 [HOOK_POST_ROUTING] = "POSTROUTING",
Rusty Russell10758b72000-09-14 07:37:33 +000063#ifdef HOOK_DROPPING
Patrick McHardy0b639362007-09-08 16:00:01 +000064 [HOOK_DROPPING] = "DROPPING"
Rusty Russell10758b72000-09-14 07:37:33 +000065#endif
Marc Bouchere6869a82000-03-20 06:03:29 +000066};
67
Harald Welteaae69be2004-08-29 23:32:14 +000068/* Convenience structures */
69struct ipt_error_target
70{
71 STRUCT_ENTRY_TARGET t;
72 char error[TABLE_MAXNAMELEN];
73};
74
75struct chain_head;
76struct rule_head;
77
Marc Bouchere6869a82000-03-20 06:03:29 +000078struct counter_map
79{
80 enum {
81 COUNTER_MAP_NOMAP,
82 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000083 COUNTER_MAP_ZEROED,
84 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000085 } maptype;
86 unsigned int mappos;
87};
88
Harald Welteaae69be2004-08-29 23:32:14 +000089enum iptcc_rule_type {
90 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
91 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
92 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
93 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000094};
95
Harald Welteaae69be2004-08-29 23:32:14 +000096struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000097{
Harald Welteaae69be2004-08-29 23:32:14 +000098 struct list_head list;
99 struct chain_head *chain;
100 struct counter_map counter_map;
101
102 unsigned int index; /* index (needed for counter_map) */
103 unsigned int offset; /* offset in rule blob */
104
105 enum iptcc_rule_type type;
106 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
107
108 unsigned int size; /* size of entry data */
109 STRUCT_ENTRY entry[0];
110};
111
112struct chain_head
113{
114 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000115 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000116 unsigned int hooknum; /* hook number+1 if builtin */
117 unsigned int references; /* how many jumps reference us */
118 int verdict; /* verdict if builtin */
119
120 STRUCT_COUNTERS counters; /* per-chain counters */
121 struct counter_map counter_map;
122
123 unsigned int num_rules; /* number of rules in list */
124 struct list_head rules; /* list of rules */
125
126 unsigned int index; /* index (needed for jump resolval) */
127 unsigned int head_offset; /* offset in rule blob */
128 unsigned int foot_index; /* index (needed for counter_map) */
129 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000130};
131
Rusty Russell79dee072000-05-02 16:45:16 +0000132STRUCT_TC_HANDLE
Marc Bouchere6869a82000-03-20 06:03:29 +0000133{
Jan Engelhardt175f4512008-11-10 17:25:55 +0100134 int sockfd;
Harald Welteaae69be2004-08-29 23:32:14 +0000135 int changed; /* Have changes been made? */
136
137 struct list_head chains;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100138
Harald Welteaae69be2004-08-29 23:32:14 +0000139 struct chain_head *chain_iterator_cur;
140 struct rule_head *rule_iterator_cur;
141
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000142 unsigned int num_chains; /* number of user defined chains */
143
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000144 struct chain_head **chain_index; /* array for fast chain list access*/
145 unsigned int chain_index_sz;/* size of chain index array */
146
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200147 int sorted_offsets; /* if chains are received sorted from kernel,
148 * then the offsets are also sorted. Says if its
149 * possible to bsearch offsets using chain_index.
150 */
151
Rusty Russell79dee072000-05-02 16:45:16 +0000152 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000153 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000154};
155
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200156enum bsearch_type {
157 BSEARCH_NAME, /* Binary search after chain name */
158 BSEARCH_OFFSET, /* Binary search based on offset */
159};
160
Harald Welteaae69be2004-08-29 23:32:14 +0000161/* allocate a new chain head for the cache */
162static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
163{
164 struct chain_head *c = malloc(sizeof(*c));
165 if (!c)
166 return NULL;
167 memset(c, 0, sizeof(*c));
168
169 strncpy(c->name, name, TABLE_MAXNAMELEN);
170 c->hooknum = hooknum;
171 INIT_LIST_HEAD(&c->rules);
172
173 return c;
174}
175
176/* allocate and initialize a new rule for the cache */
177static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
178{
179 struct rule_head *r = malloc(sizeof(*r)+size);
180 if (!r)
181 return NULL;
182 memset(r, 0, sizeof(*r));
183
184 r->chain = c;
185 r->size = size;
186
187 return r;
188}
189
190/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000191static inline void
Jan Engelhardtfd187312008-11-10 16:59:27 +0100192set_changed(struct xtc_handle *h)
Rusty Russell175f6412000-03-24 09:32:20 +0000193{
Rusty Russell175f6412000-03-24 09:32:20 +0000194 h->changed = 1;
195}
196
Harald Welte380ba5f2002-02-13 16:19:55 +0000197#ifdef IPTC_DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100198static void do_check(struct xtc_handle *h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000199#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000200#else
201#define CHECK(h)
202#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000203
Harald Welteaae69be2004-08-29 23:32:14 +0000204
205/**********************************************************************
206 * iptc blob utility functions (iptcb_*)
207 **********************************************************************/
208
Marc Bouchere6869a82000-03-20 06:03:29 +0000209static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000210iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000211 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000212 unsigned int *pos)
213{
214 if (i == seek)
215 return 1;
216 (*pos)++;
217 return 0;
218}
219
Marc Bouchere6869a82000-03-20 06:03:29 +0000220static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000221iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000222 unsigned int number,
223 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000224 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000225{
226 if (*pos == number) {
227 *pe = i;
228 return 1;
229 }
230 (*pos)++;
231 return 0;
232}
233
Harald Welteaae69be2004-08-29 23:32:14 +0000234static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100235iptcb_get_entry(struct xtc_handle *h, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000236{
237 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
238}
239
240static unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100241iptcb_entry2index(struct xtc_handle *const h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000242{
243 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000244
Harald Welteaae69be2004-08-29 23:32:14 +0000245 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
246 iptcb_get_number, seek, &pos) == 0) {
247 fprintf(stderr, "ERROR: offset %u not an entry!\n",
248 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
249 abort();
250 }
251 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000252}
253
Harald Welte0113fe72004-01-06 19:04:02 +0000254static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100255iptcb_offset2entry(struct xtc_handle *h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000256{
Harald Welteaae69be2004-08-29 23:32:14 +0000257 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000258}
259
Harald Welteaae69be2004-08-29 23:32:14 +0000260
Harald Welte0113fe72004-01-06 19:04:02 +0000261static inline unsigned long
Jan Engelhardtfd187312008-11-10 16:59:27 +0100262iptcb_entry2offset(struct xtc_handle *const h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000263{
Harald Welteaae69be2004-08-29 23:32:14 +0000264 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000265}
266
267static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100268iptcb_offset2index(struct xtc_handle *const h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000269{
Harald Welteaae69be2004-08-29 23:32:14 +0000270 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
271}
272
273/* Returns 0 if not hook entry, else hooknumber + 1 */
274static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100275iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +0000276{
277 unsigned int i;
278
279 for (i = 0; i < NUMHOOKS; i++) {
280 if ((h->info.valid_hooks & (1 << i))
281 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
282 return i+1;
283 }
284 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000285}
286
287
Harald Welteaae69be2004-08-29 23:32:14 +0000288/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000289 * Chain index (cache utility) functions
290 **********************************************************************
291 * The chain index is an array with pointers into the chain list, with
292 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
293 * speedup chain list searching, by find a more optimal starting
294 * points when searching the linked list.
295 *
296 * The starting point can be found fast by using a binary search of
297 * the chain index. Thus, reducing the previous search complexity of
298 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
299 *
300 * A nice property of the chain index, is that the "bucket" list
301 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
302 * change this). Oppose to hashing, where the "bucket" list length can
303 * vary a lot.
304 */
305#ifndef CHAIN_INDEX_BUCKET_LEN
306#define CHAIN_INDEX_BUCKET_LEN 40
307#endif
308
309/* Another nice property of the chain index is that inserting/creating
310 * chains in chain list don't change the correctness of the chain
311 * index, it only causes longer lists in the buckets.
312 *
313 * To mitigate the performance penalty of longer bucket lists and the
314 * penalty of rebuilding, the chain index is rebuild only when
315 * CHAIN_INDEX_INSERT_MAX chains has been added.
316 */
317#ifndef CHAIN_INDEX_INSERT_MAX
318#define CHAIN_INDEX_INSERT_MAX 355
319#endif
320
321static inline unsigned int iptcc_is_builtin(struct chain_head *c);
322
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000323/* Use binary search in the chain index array, to find a chain_head
324 * pointer closest to the place of the searched name element.
325 *
326 * Notes that, binary search (obviously) requires that the chain list
327 * is sorted by name.
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200328 *
329 * The not so obvious: The chain index array, is actually both sorted
330 * by name and offset, at the same time!. This is only true because,
331 * chain are stored sorted in the kernel (as we pushed it in sorted).
332 *
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000333 */
334static struct list_head *
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200335__iptcc_bsearch_chain_index(const char *name, unsigned int offset,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100336 unsigned int *idx, struct xtc_handle *handle,
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200337 enum bsearch_type type)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000338{
339 unsigned int pos, end;
340 int res;
341
342 struct list_head *list_pos;
343 list_pos=&handle->chains;
344
345 /* Check for empty array, e.g. no user defined chains */
346 if (handle->chain_index_sz == 0) {
347 debug("WARNING: handle->chain_index_sz == 0\n");
348 return list_pos;
349 }
350
351 /* Init */
352 end = handle->chain_index_sz;
353 pos = end / 2;
354
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200355 debug("bsearch Find chain:%s (pos:%d end:%d) (offset:%d)\n",
356 name, pos, end, offset);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000357
358 /* Loop */
359 loop:
360 if (!handle->chain_index[pos]) {
361 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
362 return &handle->chains; /* Be safe, return orig start pos */
363 }
364
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200365 debug("bsearch Index[%d] name:%s ",
366 pos, handle->chain_index[pos]->name);
367
368 /* Support for different compare functions */
369 switch (type) {
370 case BSEARCH_NAME:
371 res = strcmp(name, handle->chain_index[pos]->name);
372 break;
373 case BSEARCH_OFFSET:
374 debug("head_offset:[%d] foot_offset:[%d] ",
375 handle->chain_index[pos]->head_offset,
376 handle->chain_index[pos]->foot_offset);
377 res = offset - handle->chain_index[pos]->head_offset;
378 break;
379 default:
380 fprintf(stderr, "ERROR: %d not a valid bsearch type\n",
381 type);
382 abort();
383 break;
384 }
385 debug("res:%d ", res);
386
387
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000388 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100389 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000390
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000391 if (res == 0) { /* Found element, by direct hit */
392 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
393 return list_pos;
394 } else if (res < 0) { /* Too far, jump back */
395 end = pos;
396 pos = pos / 2;
397
398 /* Exit case: First element of array */
399 if (end == 0) {
400 debug("[found] Reached first array elem (end%d)\n",end);
401 return list_pos;
402 }
403 debug("jump back to pos:%d (end:%d)\n", pos, end);
404 goto loop;
405 } else if (res > 0 ){ /* Not far enough, jump forward */
406
407 /* Exit case: Last element of array */
408 if (pos == handle->chain_index_sz-1) {
409 debug("[found] Last array elem (end:%d)\n", end);
410 return list_pos;
411 }
412
413 /* Exit case: Next index less, thus elem in this list section */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200414 switch (type) {
415 case BSEARCH_NAME:
416 res = strcmp(name, handle->chain_index[pos+1]->name);
417 break;
418 case BSEARCH_OFFSET:
419 res = offset - handle->chain_index[pos+1]->head_offset;
420 break;
421 }
422
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000423 if (res < 0) {
424 debug("[found] closest list (end:%d)\n", end);
425 return list_pos;
426 }
427
428 pos = (pos+end)/2;
429 debug("jump forward to pos:%d (end:%d)\n", pos, end);
430 goto loop;
431 }
432
433 return list_pos;
434}
435
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200436/* Wrapper for string chain name based bsearch */
437static struct list_head *
438iptcc_bsearch_chain_index(const char *name, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100439 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200440{
441 return __iptcc_bsearch_chain_index(name, 0, idx, handle, BSEARCH_NAME);
442}
443
444
445/* Wrapper for offset chain based bsearch */
446static struct list_head *
447iptcc_bsearch_chain_offset(unsigned int offset, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100448 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200449{
450 struct list_head *pos;
451
452 /* If chains were not received sorted from kernel, then the
453 * offset bsearch is not possible.
454 */
455 if (!handle->sorted_offsets)
456 pos = handle->chains.next;
457 else
458 pos = __iptcc_bsearch_chain_index(NULL, offset, idx, handle,
459 BSEARCH_OFFSET);
460 return pos;
461}
462
463
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000464#ifdef DEBUG
465/* Trivial linear search of chain index. Function used for verifying
466 the output of bsearch function */
467static struct list_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100468iptcc_linearly_search_chain_index(const char *name, struct xtc_handle *handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000469{
470 unsigned int i=0;
471 int res=0;
472
473 struct list_head *list_pos;
474 list_pos = &handle->chains;
475
476 if (handle->chain_index_sz)
477 list_pos = &handle->chain_index[0]->list;
478
479 /* Linearly walk of chain index array */
480
481 for (i=0; i < handle->chain_index_sz; i++) {
482 if (handle->chain_index[i]) {
483 res = strcmp(handle->chain_index[i]->name, name);
484 if (res > 0)
485 break; // One step too far
486 list_pos = &handle->chain_index[i]->list;
487 if (res == 0)
488 break; // Direct hit
489 }
490 }
491
492 return list_pos;
493}
494#endif
495
Jan Engelhardtfd187312008-11-10 16:59:27 +0100496static int iptcc_chain_index_alloc(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000497{
498 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
499 unsigned int array_elems;
500 unsigned int array_mem;
501
502 /* Allocate memory for the chain index array */
503 array_elems = (h->num_chains / list_length) +
504 (h->num_chains % list_length ? 1 : 0);
505 array_mem = sizeof(h->chain_index) * array_elems;
506
507 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
508 array_elems, array_mem);
509
510 h->chain_index = malloc(array_mem);
Jan Engelhardt0eee3002008-11-26 17:18:08 +0100511 if (h->chain_index == NULL && array_mem > 0) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000512 h->chain_index_sz = 0;
513 return -ENOMEM;
514 }
515 memset(h->chain_index, 0, array_mem);
516 h->chain_index_sz = array_elems;
517
518 return 1;
519}
520
Jan Engelhardtfd187312008-11-10 16:59:27 +0100521static void iptcc_chain_index_free(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000522{
523 h->chain_index_sz = 0;
524 free(h->chain_index);
525}
526
527
528#ifdef DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100529static void iptcc_chain_index_dump(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000530{
531 unsigned int i = 0;
532
533 /* Dump: contents of chain index array */
534 for (i=0; i < h->chain_index_sz; i++) {
535 if (h->chain_index[i]) {
536 fprintf(stderr, "Chain index[%d].name: %s\n",
537 i, h->chain_index[i]->name);
538 }
539 }
540}
541#endif
542
543/* Build the chain index */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100544static int iptcc_chain_index_build(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000545{
546 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
547 unsigned int chains = 0;
548 unsigned int cindex = 0;
549 struct chain_head *c;
550
551 /* Build up the chain index array here */
552 debug("Building chain index\n");
553
554 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
555 h->num_chains, list_length, h->chain_index_sz);
556
557 if (h->chain_index_sz == 0)
558 return 0;
559
560 list_for_each_entry(c, &h->chains, list) {
561
562 /* Issue: The index array needs to start after the
563 * builtin chains, as they are not sorted */
564 if (!iptcc_is_builtin(c)) {
565 cindex=chains / list_length;
566
567 /* Safe guard, break out on array limit, this
568 * is useful if chains are added and array is
569 * rebuild, without realloc of memory. */
570 if (cindex >= h->chain_index_sz)
571 break;
572
573 if ((chains % list_length)== 0) {
574 debug("\nIndex[%d] Chains:", cindex);
575 h->chain_index[cindex] = c;
576 }
577 chains++;
578 }
579 debug("%s, ", c->name);
580 }
581 debug("\n");
582
583 return 1;
584}
585
Jan Engelhardtfd187312008-11-10 16:59:27 +0100586static int iptcc_chain_index_rebuild(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000587{
588 debug("REBUILD chain index array\n");
589 iptcc_chain_index_free(h);
590 if ((iptcc_chain_index_alloc(h)) < 0)
591 return -ENOMEM;
592 iptcc_chain_index_build(h);
593 return 1;
594}
595
596/* Delete chain (pointer) from index array. Removing an element from
597 * the chain list only affects the chain index array, if the chain
598 * index points-to/uses that list pointer.
599 *
600 * There are different strategies, the simple and safe is to rebuild
601 * the chain index every time. The more advanced is to update the
602 * array index to point to the next element, but that requires some
603 * house keeping and boundry checks. The advanced is implemented, as
604 * the simple approach behaves badly when all chains are deleted
605 * because list_for_each processing will always hit the first chain
606 * index, thus causing a rebuild for every chain.
607 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100608static int iptcc_chain_index_delete_chain(struct chain_head *c, struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000609{
610 struct list_head *index_ptr, *index_ptr2, *next;
611 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100612 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000613
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100614 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000615
616 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
617 c->name, &c->list, index_ptr);
618
619 /* Save the next pointer */
620 next = c->list.next;
621 list_del(&c->list);
622
623 if (index_ptr == &c->list) { /* Chain used as index ptr */
624
625 /* See if its possible to avoid a rebuild, by shifting
626 * to next pointer. Its possible if the next pointer
627 * is located in the same index bucket.
628 */
629 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100630 index_ptr2 = iptcc_bsearch_chain_index(c2->name, &idx2, h);
631 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000632 /* Rebuild needed */
633 return iptcc_chain_index_rebuild(h);
634 } else {
635 /* Avoiding rebuild */
636 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100637 idx, c2->name);
638 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000639 return 0;
640 }
641 }
642 return 0;
643}
644
645
646/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000647 * iptc cache utility functions (iptcc_*)
648 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000649
Harald Welteaae69be2004-08-29 23:32:14 +0000650/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000651static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000652{
653 return (c->hooknum ? 1 : 0);
654}
655
656/* Get a specific rule within a chain */
657static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
658 unsigned int rulenum)
659{
660 struct rule_head *r;
661 unsigned int num = 0;
662
663 list_for_each_entry(r, &c->rules, list) {
664 num++;
665 if (num == rulenum)
666 return r;
667 }
668 return NULL;
669}
670
Martin Josefssona5616dc2004-10-24 22:27:31 +0000671/* Get a specific rule within a chain backwards */
672static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
673 unsigned int rulenum)
674{
675 struct rule_head *r;
676 unsigned int num = 0;
677
678 list_for_each_entry_reverse(r, &c->rules, list) {
679 num++;
680 if (num == rulenum)
681 return r;
682 }
683 return NULL;
684}
685
Harald Welteaae69be2004-08-29 23:32:14 +0000686/* Returns chain head if found, otherwise NULL. */
687static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100688iptcc_find_chain_by_offset(struct xtc_handle *handle, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000689{
690 struct list_head *pos;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200691 struct list_head *list_start_pos;
692 unsigned int i;
Harald Welteaae69be2004-08-29 23:32:14 +0000693
694 if (list_empty(&handle->chains))
695 return NULL;
696
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200697 /* Find a smart place to start the search */
698 list_start_pos = iptcc_bsearch_chain_offset(offset, &i, handle);
699
700 /* Note that iptcc_bsearch_chain_offset() skips builtin
701 * chains, but this function is only used for finding jump
702 * targets, and a buildin chain is not a valid jump target */
703
704 debug("Offset:[%u] starting search at index:[%u]\n", offset, i);
705// list_for_each(pos, &handle->chains) {
706 list_for_each(pos, list_start_pos->prev) {
Harald Welteaae69be2004-08-29 23:32:14 +0000707 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200708 debug(".");
709 if (offset >= c->head_offset && offset <= c->foot_offset) {
710 debug("Offset search found chain:[%s]\n", c->name);
Harald Welteaae69be2004-08-29 23:32:14 +0000711 return c;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200712 }
Harald Welte0113fe72004-01-06 19:04:02 +0000713 }
714
Harald Welteaae69be2004-08-29 23:32:14 +0000715 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000716}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000717
Harald Welteaae69be2004-08-29 23:32:14 +0000718/* Returns chain head if found, otherwise NULL. */
719static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100720iptcc_find_label(const char *name, struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +0000721{
722 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000723 struct list_head *list_start_pos;
724 unsigned int i=0;
725 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000726
727 if (list_empty(&handle->chains))
728 return NULL;
729
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000730 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000731 list_for_each(pos, &handle->chains) {
732 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000733 if (!iptcc_is_builtin(c))
734 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000735 if (!strcmp(c->name, name))
736 return c;
737 }
738
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000739 /* Find a smart place to start the search via chain index */
740 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
741 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
742
743 /* Handel if bsearch bails out early */
744 if (list_start_pos == &handle->chains) {
745 list_start_pos = pos;
746 }
747#ifdef DEBUG
748 else {
749 /* Verify result of bsearch against linearly index search */
750 struct list_head *test_pos;
751 struct chain_head *test_c, *tmp_c;
752 test_pos = iptcc_linearly_search_chain_index(name, handle);
753 if (list_start_pos != test_pos) {
754 debug("BUG in chain_index search\n");
755 test_c=list_entry(test_pos, struct chain_head,list);
756 tmp_c =list_entry(list_start_pos,struct chain_head,list);
757 debug("Verify search found:\n");
758 debug(" Chain:%s\n", test_c->name);
759 debug("BSearch found:\n");
760 debug(" Chain:%s\n", tmp_c->name);
761 exit(42);
762 }
763 }
764#endif
765
766 /* Initial/special case, no user defined chains */
767 if (handle->num_chains == 0)
768 return NULL;
769
770 /* Start searching through the chain list */
771 list_for_each(pos, list_start_pos->prev) {
772 struct chain_head *c = list_entry(pos, struct chain_head, list);
773 res = strcmp(c->name, name);
774 debug("List search name:%s == %s res:%d\n", name, c->name, res);
775 if (res==0)
776 return c;
777
778 /* We can stop earlier as we know list is sorted */
779 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
780 debug(" Not in list, walked too far, sorted list\n");
781 return NULL;
782 }
783
784 /* Stop on wrap around, if list head is reached */
785 if (pos == &handle->chains) {
786 debug("Stop, list head reached\n");
787 return NULL;
788 }
789 }
790
791 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000792 return NULL;
793}
794
795/* called when rule is to be removed from cache */
796static void iptcc_delete_rule(struct rule_head *r)
797{
798 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
799 /* clean up reference count of called chain */
800 if (r->type == IPTCC_R_JUMP
801 && r->jump)
802 r->jump->references--;
803
804 list_del(&r->list);
805 free(r);
806}
807
808
809/**********************************************************************
810 * RULESET PARSER (blob -> cache)
811 **********************************************************************/
812
Harald Welteaae69be2004-08-29 23:32:14 +0000813/* Delete policy rule of previous chain, since cache doesn't contain
814 * chain policy rules.
815 * WARNING: This function has ugly design and relies on a lot of context, only
816 * to be called from specific places within the parser */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100817static int __iptcc_p_del_policy(struct xtc_handle *h, unsigned int num)
Harald Welteaae69be2004-08-29 23:32:14 +0000818{
819 if (h->chain_iterator_cur) {
820 /* policy rule is last rule */
821 struct rule_head *pr = (struct rule_head *)
822 h->chain_iterator_cur->rules.prev;
823
824 /* save verdict */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100825 h->chain_iterator_cur->verdict =
Harald Welteaae69be2004-08-29 23:32:14 +0000826 *(int *)GET_TARGET(pr->entry)->data;
827
828 /* save counter and counter_map information */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100829 h->chain_iterator_cur->counter_map.maptype =
Harald Welteaae69be2004-08-29 23:32:14 +0000830 COUNTER_MAP_NORMAL_MAP;
831 h->chain_iterator_cur->counter_map.mappos = num-1;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100832 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
Harald Welteaae69be2004-08-29 23:32:14 +0000833 sizeof(h->chain_iterator_cur->counters));
834
835 /* foot_offset points to verdict rule */
836 h->chain_iterator_cur->foot_index = num;
837 h->chain_iterator_cur->foot_offset = pr->offset;
838
839 /* delete rule from cache */
840 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000841 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000842
843 return 1;
844 }
845 return 0;
846}
847
Harald Welteec30b6c2005-02-01 16:45:56 +0000848/* alphabetically insert a chain into the list */
Christoph Paasch7cd15e32009-03-23 13:50:11 +0100849static void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c)
Harald Welteec30b6c2005-02-01 16:45:56 +0000850{
851 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000852 struct list_head *list_start_pos;
853 unsigned int i=1;
854
855 /* Find a smart place to start the insert search */
856 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
857
858 /* Handle the case, where chain.name is smaller than index[0] */
859 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
860 h->chain_index[0] = c; /* Update chain index head */
861 list_start_pos = h->chains.next;
862 debug("Update chain_index[0] with %s\n", c->name);
863 }
864
865 /* Handel if bsearch bails out early */
866 if (list_start_pos == &h->chains) {
867 list_start_pos = h->chains.next;
868 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000869
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000870 /* sort only user defined chains */
871 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000872 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000873 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000874 list_add(&c->list, tmp->list.prev);
875 return;
876 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000877
878 /* Stop if list head is reached */
879 if (&tmp->list == &h->chains) {
880 debug("Insert, list head reached add to tail\n");
881 break;
882 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000883 }
884 }
885
886 /* survived till end of list: add at tail */
887 list_add_tail(&c->list, &h->chains);
888}
889
Harald Welteaae69be2004-08-29 23:32:14 +0000890/* Another ugly helper function split out of cache_add_entry to make it less
891 * spaghetti code */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100892static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c,
Harald Welteaae69be2004-08-29 23:32:14 +0000893 unsigned int offset, unsigned int *num)
894{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000895 struct list_head *tail = h->chains.prev;
896 struct chain_head *ctail;
897
Harald Welteaae69be2004-08-29 23:32:14 +0000898 __iptcc_p_del_policy(h, *num);
899
900 c->head_offset = offset;
901 c->index = *num;
902
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000903 /* Chains from kernel are already sorted, as they are inserted
904 * sorted. But there exists an issue when shifting to 1.4.0
905 * from an older version, as old versions allow last created
906 * chain to be unsorted.
907 */
908 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
909 list_add_tail(&c->list, &h->chains);
910 else {
911 ctail = list_entry(tail, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200912
Jesper Dangaard Brouer526d3e12008-07-03 20:29:34 +0200913 if (strcmp(c->name, ctail->name) > 0 ||
914 iptcc_is_builtin(ctail))
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000915 list_add_tail(&c->list, &h->chains);/* Already sorted*/
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200916 else {
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000917 iptc_insert_chain(h, c);/* Was not sorted */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200918
919 /* Notice, if chains were not received sorted
920 * from kernel, then an offset bsearch is no
921 * longer valid.
922 */
923 h->sorted_offsets = 0;
924
925 debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n",
926 c->name, ctail->name);
927 }
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000928 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000929
Harald Welteaae69be2004-08-29 23:32:14 +0000930 h->chain_iterator_cur = c;
931}
932
933/* main parser function: add an entry from the blob to the cache */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100934static int cache_add_entry(STRUCT_ENTRY *e,
935 struct xtc_handle *h,
Harald Welteaae69be2004-08-29 23:32:14 +0000936 STRUCT_ENTRY **prev,
937 unsigned int *num)
938{
939 unsigned int builtin;
940 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
941
942 DEBUGP("entering...");
943
944 /* Last entry ("policy rule"). End it.*/
945 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
946 /* This is the ERROR node at the end of the chain */
947 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
948
949 __iptcc_p_del_policy(h, *num);
950
951 h->chain_iterator_cur = NULL;
952 goto out_inc;
953 }
954
955 /* We know this is the start of a new chain if it's an ERROR
956 * target, or a hook entry point */
957
958 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100959 struct chain_head *c =
Harald Welteaae69be2004-08-29 23:32:14 +0000960 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100961 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
Harald Welteaae69be2004-08-29 23:32:14 +0000962 (char *)c->name, c);
963 if (!c) {
964 errno = -ENOMEM;
965 return -1;
966 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000967 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000968
969 __iptcc_p_add_chain(h, c, offset, num);
970
971 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
972 struct chain_head *c =
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100973 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
Harald Welteaae69be2004-08-29 23:32:14 +0000974 builtin);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100975 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
Harald Welteaae69be2004-08-29 23:32:14 +0000976 *num, offset, c, &c->rules);
977 if (!c) {
978 errno = -ENOMEM;
979 return -1;
980 }
981
982 c->hooknum = builtin;
983
984 __iptcc_p_add_chain(h, c, offset, num);
985
986 /* FIXME: this is ugly. */
987 goto new_rule;
988 } else {
989 /* has to be normal rule */
990 struct rule_head *r;
991new_rule:
992
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100993 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
Harald Welteaae69be2004-08-29 23:32:14 +0000994 e->next_offset))) {
995 errno = ENOMEM;
996 return -1;
997 }
998 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
999
1000 r->index = *num;
1001 r->offset = offset;
1002 memcpy(r->entry, e, e->next_offset);
1003 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
1004 r->counter_map.mappos = r->index;
1005
1006 /* handling of jumps, etc. */
1007 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
1008 STRUCT_STANDARD_TARGET *t;
1009
1010 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
1011 if (t->target.u.target_size
1012 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
1013 errno = EINVAL;
1014 return -1;
1015 }
1016
1017 if (t->verdict < 0) {
1018 DEBUGP_C("standard, verdict=%d\n", t->verdict);
1019 r->type = IPTCC_R_STANDARD;
1020 } else if (t->verdict == r->offset+e->next_offset) {
1021 DEBUGP_C("fallthrough\n");
1022 r->type = IPTCC_R_FALLTHROUGH;
1023 } else {
1024 DEBUGP_C("jump, target=%u\n", t->verdict);
1025 r->type = IPTCC_R_JUMP;
1026 /* Jump target fixup has to be deferred
1027 * until second pass, since we migh not
1028 * yet have parsed the target */
1029 }
Martin Josefsson52c38022004-09-22 19:39:40 +00001030 } else {
1031 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
1032 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001033 }
1034
1035 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +00001036 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +00001037 }
1038out_inc:
1039 (*num)++;
1040 return 0;
1041}
1042
1043
1044/* parse an iptables blob into it's pieces */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001045static int parse_table(struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +00001046{
1047 STRUCT_ENTRY *prev;
1048 unsigned int num = 0;
1049 struct chain_head *c;
1050
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +02001051 /* Assume that chains offsets are sorted, this verified during
1052 parsing of ruleset (in __iptcc_p_add_chain())*/
1053 h->sorted_offsets = 1;
1054
Harald Welteaae69be2004-08-29 23:32:14 +00001055 /* First pass: over ruleset blob */
1056 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
1057 cache_add_entry, h, &prev, &num);
1058
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001059 /* Build the chain index, used for chain list search speedup */
1060 if ((iptcc_chain_index_alloc(h)) < 0)
1061 return -ENOMEM;
1062 iptcc_chain_index_build(h);
1063
Harald Welteaae69be2004-08-29 23:32:14 +00001064 /* Second pass: fixup parsed data from first pass */
1065 list_for_each_entry(c, &h->chains, list) {
1066 struct rule_head *r;
1067 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001068 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +00001069 STRUCT_STANDARD_TARGET *t;
1070
1071 if (r->type != IPTCC_R_JUMP)
1072 continue;
1073
1074 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001075 lc = iptcc_find_chain_by_offset(h, t->verdict);
1076 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +00001077 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001078 r->jump = lc;
1079 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +00001080 }
1081 }
1082
Harald Welteaae69be2004-08-29 23:32:14 +00001083 return 1;
1084}
1085
1086
1087/**********************************************************************
1088 * RULESET COMPILATION (cache -> blob)
1089 **********************************************************************/
1090
1091/* Convenience structures */
1092struct iptcb_chain_start{
1093 STRUCT_ENTRY e;
1094 struct ipt_error_target name;
1095};
1096#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
1097 ALIGN(sizeof(struct ipt_error_target)))
1098
1099struct iptcb_chain_foot {
1100 STRUCT_ENTRY e;
1101 STRUCT_STANDARD_TARGET target;
1102};
1103#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1104 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1105
1106struct iptcb_chain_error {
1107 STRUCT_ENTRY entry;
1108 struct ipt_error_target target;
1109};
1110#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1111 ALIGN(sizeof(struct ipt_error_target)))
1112
1113
1114
1115/* compile rule from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001116static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r)
Harald Welteaae69be2004-08-29 23:32:14 +00001117{
1118 /* handle jumps */
1119 if (r->type == IPTCC_R_JUMP) {
1120 STRUCT_STANDARD_TARGET *t;
1121 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1122 /* memset for memcmp convenience on delete/replace */
1123 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1124 strcpy(t->target.u.user.name, STANDARD_TARGET);
1125 /* Jumps can only happen to builtin chains, so we
1126 * can safely assume that they always have a header */
1127 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1128 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1129 STRUCT_STANDARD_TARGET *t;
1130 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1131 t->verdict = r->offset + r->size;
1132 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001133
Harald Welteaae69be2004-08-29 23:32:14 +00001134 /* copy entry from cache to blob */
1135 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1136
1137 return 1;
1138}
1139
1140/* compile chain from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001141static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +00001142{
1143 int ret;
1144 struct rule_head *r;
1145 struct iptcb_chain_start *head;
1146 struct iptcb_chain_foot *foot;
1147
1148 /* only user-defined chains have heaer */
1149 if (!iptcc_is_builtin(c)) {
1150 /* put chain header in place */
1151 head = (void *)repl->entries + c->head_offset;
1152 head->e.target_offset = sizeof(STRUCT_ENTRY);
1153 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1154 strcpy(head->name.t.u.user.name, ERROR_TARGET);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001155 head->name.t.u.target_size =
Harald Welteaae69be2004-08-29 23:32:14 +00001156 ALIGN(sizeof(struct ipt_error_target));
1157 strcpy(head->name.error, c->name);
1158 } else {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001159 repl->hook_entry[c->hooknum-1] = c->head_offset;
Harald Welteaae69be2004-08-29 23:32:14 +00001160 repl->underflow[c->hooknum-1] = c->foot_offset;
1161 }
1162
1163 /* iterate over rules */
1164 list_for_each_entry(r, &c->rules, list) {
1165 ret = iptcc_compile_rule(h, repl, r);
1166 if (ret < 0)
1167 return ret;
1168 }
1169
1170 /* put chain footer in place */
1171 foot = (void *)repl->entries + c->foot_offset;
1172 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1173 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1174 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1175 foot->target.target.u.target_size =
1176 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1177 /* builtin targets have verdict, others return */
1178 if (iptcc_is_builtin(c))
1179 foot->target.verdict = c->verdict;
1180 else
1181 foot->target.verdict = RETURN;
1182 /* set policy-counters */
1183 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1184
1185 return 0;
1186}
1187
1188/* calculate offset and number for every rule in the cache */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001189static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001190 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001191{
1192 struct rule_head *r;
1193
1194 c->head_offset = *offset;
1195 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1196
1197 if (!iptcc_is_builtin(c)) {
1198 /* Chain has header */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001199 *offset += sizeof(STRUCT_ENTRY)
Harald Welteaae69be2004-08-29 23:32:14 +00001200 + ALIGN(sizeof(struct ipt_error_target));
1201 (*num)++;
1202 }
1203
1204 list_for_each_entry(r, &c->rules, list) {
1205 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1206 r->offset = *offset;
1207 r->index = *num;
1208 *offset += r->size;
1209 (*num)++;
1210 }
1211
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001212 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
Harald Welteaae69be2004-08-29 23:32:14 +00001213 *offset, *num);
1214 c->foot_offset = *offset;
1215 c->foot_index = *num;
1216 *offset += sizeof(STRUCT_ENTRY)
1217 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1218 (*num)++;
1219
1220 return 1;
1221}
1222
1223/* put the pieces back together again */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001224static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size)
Harald Welteaae69be2004-08-29 23:32:14 +00001225{
1226 struct chain_head *c;
1227 unsigned int offset = 0, num = 0;
1228 int ret = 0;
1229
1230 /* First pass: calculate offset for every rule */
1231 list_for_each_entry(c, &h->chains, list) {
1232 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1233 if (ret < 0)
1234 return ret;
1235 }
1236
1237 /* Append one error rule at end of chain */
1238 num++;
1239 offset += sizeof(STRUCT_ENTRY)
1240 + ALIGN(sizeof(struct ipt_error_target));
1241
1242 /* ruleset size is now in offset */
1243 *size = offset;
1244 return num;
1245}
1246
Jan Engelhardtfd187312008-11-10 16:59:27 +01001247static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl)
Harald Welteaae69be2004-08-29 23:32:14 +00001248{
1249 struct chain_head *c;
1250 struct iptcb_chain_error *error;
1251
1252 /* Second pass: copy from cache to offsets, fill in jumps */
1253 list_for_each_entry(c, &h->chains, list) {
1254 int ret = iptcc_compile_chain(h, repl, c);
1255 if (ret < 0)
1256 return ret;
1257 }
1258
1259 /* Append error rule at end of chain */
1260 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1261 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1262 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001263 error->target.t.u.user.target_size =
Harald Welteaae69be2004-08-29 23:32:14 +00001264 ALIGN(sizeof(struct ipt_error_target));
1265 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1266 strcpy((char *)&error->target.error, "ERROR");
1267
1268 return 1;
1269}
1270
1271/**********************************************************************
1272 * EXTERNAL API (operates on cache only)
1273 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001274
1275/* Allocate handle of given size */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001276static struct xtc_handle *
Harald Welte0113fe72004-01-06 19:04:02 +00001277alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001278{
1279 size_t len;
Jan Engelhardtfd187312008-11-10 16:59:27 +01001280 struct xtc_handle *h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001281
Harald Welteaae69be2004-08-29 23:32:14 +00001282 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001283
Harald Welteaae69be2004-08-29 23:32:14 +00001284 h = malloc(sizeof(STRUCT_TC_HANDLE));
1285 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001286 errno = ENOMEM;
1287 return NULL;
1288 }
Harald Welteaae69be2004-08-29 23:32:14 +00001289 memset(h, 0, sizeof(*h));
1290 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001291 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001292
Harald Welte0371c0c2004-09-19 21:00:12 +00001293 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001294 if (!h->entries)
1295 goto out_free_handle;
1296
1297 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001298 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001299
1300 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001301
1302out_free_handle:
1303 free(h);
1304
1305 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001306}
1307
Harald Welteaae69be2004-08-29 23:32:14 +00001308
Jan Engelhardtfd187312008-11-10 16:59:27 +01001309struct xtc_handle *
Rusty Russell79dee072000-05-02 16:45:16 +00001310TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001311{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001312 struct xtc_handle *h;
Rusty Russell79dee072000-05-02 16:45:16 +00001313 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001314 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001315 socklen_t s;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001316 int sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001317
Rusty Russell79dee072000-05-02 16:45:16 +00001318 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001319
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001320 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1321 errno = EINVAL;
1322 return NULL;
1323 }
Jan Engelhardt175f4512008-11-10 17:25:55 +01001324
1325 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1326 if (sockfd < 0)
1327 return NULL;
1328
Patrick McHardy2f932052008-04-02 14:01:53 +02001329retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001330 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001331
Marc Bouchere6869a82000-03-20 06:03:29 +00001332 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001333 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001334 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001335 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001336 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001337
Harald Welteaae69be2004-08-29 23:32:14 +00001338 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1339 info.valid_hooks, info.num_entries, info.size);
1340
Harald Welte0113fe72004-01-06 19:04:02 +00001341 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001342 == NULL) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001343 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001344 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001345 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001346
Marc Bouchere6869a82000-03-20 06:03:29 +00001347 /* Initialize current state */
Jan Engelhardt175f4512008-11-10 17:25:55 +01001348 h->sockfd = sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001349 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001350
Harald Welteaae69be2004-08-29 23:32:14 +00001351 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001352
Rusty Russell79dee072000-05-02 16:45:16 +00001353 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001354
Jan Engelhardt175f4512008-11-10 17:25:55 +01001355 if (getsockopt(h->sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
Harald Welteaae69be2004-08-29 23:32:14 +00001356 &tmp) < 0)
1357 goto error;
1358
1359#ifdef IPTC_DEBUG2
1360 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001361 int fd = open("/tmp/libiptc-so_get_entries.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00001362 O_CREAT|O_WRONLY);
1363 if (fd >= 0) {
1364 write(fd, h->entries, tmp);
1365 close(fd);
1366 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001367 }
Harald Welteaae69be2004-08-29 23:32:14 +00001368#endif
1369
1370 if (parse_table(h) < 0)
1371 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001372
Marc Bouchere6869a82000-03-20 06:03:29 +00001373 CHECK(h);
1374 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001375error:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001376 TC_FREE(h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001377 /* A different process changed the ruleset size, retry */
1378 if (errno == EAGAIN)
1379 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001380 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001381}
1382
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001383void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001384TC_FREE(struct xtc_handle *h)
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001385{
Harald Welteaae69be2004-08-29 23:32:14 +00001386 struct chain_head *c, *tmp;
1387
Derrik Pates664c0a32005-02-01 13:28:14 +00001388 iptc_fn = TC_FREE;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001389 close(h->sockfd);
Harald Welteaae69be2004-08-29 23:32:14 +00001390
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001391 list_for_each_entry_safe(c, tmp, &h->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00001392 struct rule_head *r, *rtmp;
1393
1394 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1395 free(r);
1396 }
1397
1398 free(c);
1399 }
1400
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001401 iptcc_chain_index_free(h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001402
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001403 free(h->entries);
1404 free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001405}
1406
Marc Bouchere6869a82000-03-20 06:03:29 +00001407static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001408print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001409{
Rusty Russell228e98d2000-04-27 10:28:06 +00001410 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001411 return 0;
1412}
1413
Jan Engelhardtfd187312008-11-10 16:59:27 +01001414static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001415
Marc Bouchere6869a82000-03-20 06:03:29 +00001416void
Jan Engelhardtfd187312008-11-10 16:59:27 +01001417TC_DUMP_ENTRIES(struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001418{
Derrik Pates664c0a32005-02-01 13:28:14 +00001419 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001420 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001421
Rusty Russelle45c7132004-12-16 13:21:44 +00001422 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001423 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001424 printf("Table `%s'\n", handle->info.name);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001425 printf("Hooks: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001426 handle->info.hook_entry[HOOK_PRE_ROUTING],
1427 handle->info.hook_entry[HOOK_LOCAL_IN],
1428 handle->info.hook_entry[HOOK_FORWARD],
1429 handle->info.hook_entry[HOOK_LOCAL_OUT],
1430 handle->info.hook_entry[HOOK_POST_ROUTING]);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001431 printf("Underflows: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001432 handle->info.underflow[HOOK_PRE_ROUTING],
1433 handle->info.underflow[HOOK_LOCAL_IN],
1434 handle->info.underflow[HOOK_FORWARD],
1435 handle->info.underflow[HOOK_LOCAL_OUT],
1436 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001437
Harald Welteaae69be2004-08-29 23:32:14 +00001438 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001439 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001440}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001441
Marc Bouchere6869a82000-03-20 06:03:29 +00001442/* Does this chain exist? */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001443int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001444{
Derrik Pates664c0a32005-02-01 13:28:14 +00001445 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001446 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001447}
1448
Jan Engelhardtfd187312008-11-10 16:59:27 +01001449static void iptcc_chain_iterator_advance(struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001450{
1451 struct chain_head *c = handle->chain_iterator_cur;
1452
1453 if (c->list.next == &handle->chains)
1454 handle->chain_iterator_cur = NULL;
1455 else
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001456 handle->chain_iterator_cur =
Harald Welteaae69be2004-08-29 23:32:14 +00001457 list_entry(c->list.next, struct chain_head, list);
1458}
Marc Bouchere6869a82000-03-20 06:03:29 +00001459
Rusty Russell30fd6e52000-04-23 09:16:06 +00001460/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001461const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001462TC_FIRST_CHAIN(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001463{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001464 struct chain_head *c = list_entry(handle->chains.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001465 struct chain_head, list);
1466
1467 iptc_fn = TC_FIRST_CHAIN;
1468
1469
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001470 if (list_empty(&handle->chains)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001471 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001472 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001473 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001474
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001475 handle->chain_iterator_cur = c;
1476 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001477
Harald Welteaae69be2004-08-29 23:32:14 +00001478 DEBUGP(": returning `%s'\n", c->name);
1479 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001480}
1481
Rusty Russell30fd6e52000-04-23 09:16:06 +00001482/* Iterator functions to run through the chains. Returns NULL at end. */
1483const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001484TC_NEXT_CHAIN(struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001485{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001486 struct chain_head *c = handle->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001487
Harald Welteaae69be2004-08-29 23:32:14 +00001488 iptc_fn = TC_NEXT_CHAIN;
1489
1490 if (!c) {
1491 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001492 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001493 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001494
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001495 iptcc_chain_iterator_advance(handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001496
Harald Welteaae69be2004-08-29 23:32:14 +00001497 DEBUGP(": returning `%s'\n", c->name);
1498 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001499}
1500
1501/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001502const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001503TC_FIRST_RULE(const char *chain, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001504{
Harald Welteaae69be2004-08-29 23:32:14 +00001505 struct chain_head *c;
1506 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001507
Harald Welteaae69be2004-08-29 23:32:14 +00001508 iptc_fn = TC_FIRST_RULE;
1509
1510 DEBUGP("first rule(%s): ", chain);
1511
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001512 c = iptcc_find_label(chain, handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001513 if (!c) {
1514 errno = ENOENT;
1515 return NULL;
1516 }
1517
1518 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001519 if (list_empty(&c->rules)) {
1520 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001521 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001522 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001523
Harald Welteaae69be2004-08-29 23:32:14 +00001524 r = list_entry(c->rules.next, struct rule_head, list);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001525 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001526 DEBUGP_C("%p\n", r);
1527
1528 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001529}
1530
1531/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001532const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001533TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001534{
Harald Welteaae69be2004-08-29 23:32:14 +00001535 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001536
Derrik Pates664c0a32005-02-01 13:28:14 +00001537 iptc_fn = TC_NEXT_RULE;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001538 DEBUGP("rule_iterator_cur=%p...", handle->rule_iterator_cur);
Harald Welteaae69be2004-08-29 23:32:14 +00001539
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001540 if (handle->rule_iterator_cur == NULL) {
Harald Welteaae69be2004-08-29 23:32:14 +00001541 DEBUGP_C("returning NULL\n");
1542 return NULL;
1543 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001544
1545 r = list_entry(handle->rule_iterator_cur->list.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001546 struct rule_head, list);
1547
1548 iptc_fn = TC_NEXT_RULE;
1549
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001550 DEBUGP_C("next=%p, head=%p...", &r->list,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001551 &handle->rule_iterator_cur->chain->rules);
Harald Welteaae69be2004-08-29 23:32:14 +00001552
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001553 if (&r->list == &handle->rule_iterator_cur->chain->rules) {
1554 handle->rule_iterator_cur = NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001555 DEBUGP_C("finished, returning NULL\n");
1556 return NULL;
1557 }
1558
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001559 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001560
1561 /* NOTE: prev is without any influence ! */
1562 DEBUGP_C("returning rule %p\n", r);
1563 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001564}
1565
Marc Bouchere6869a82000-03-20 06:03:29 +00001566/* How many rules in this chain? */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001567static unsigned int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001568TC_NUM_RULES(const char *chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001569{
Harald Welteaae69be2004-08-29 23:32:14 +00001570 struct chain_head *c;
1571 iptc_fn = TC_NUM_RULES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001572 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001573
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001574 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001575 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001576 errno = ENOENT;
1577 return (unsigned int)-1;
1578 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001579
Harald Welteaae69be2004-08-29 23:32:14 +00001580 return c->num_rules;
Marc Bouchere6869a82000-03-20 06:03:29 +00001581}
1582
Jan Engelhardt33690a12008-02-11 00:54:00 +01001583static const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001584TC_GET_RULE(const char *chain, unsigned int n, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001585{
Harald Welteaae69be2004-08-29 23:32:14 +00001586 struct chain_head *c;
1587 struct rule_head *r;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001588
Harald Welteaae69be2004-08-29 23:32:14 +00001589 iptc_fn = TC_GET_RULE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001590
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001591 CHECK(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001592
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001593 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001594 if (!c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001595 errno = ENOENT;
1596 return NULL;
1597 }
1598
Harald Welteaae69be2004-08-29 23:32:14 +00001599 r = iptcc_get_rule_num(c, n);
1600 if (!r)
1601 return NULL;
1602 return r->entry;
Marc Bouchere6869a82000-03-20 06:03:29 +00001603}
1604
1605/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001606static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001607{
Harald Welteaae69be2004-08-29 23:32:14 +00001608 switch (verdict) {
1609 case RETURN:
1610 return LABEL_RETURN;
1611 break;
1612 case -NF_ACCEPT-1:
1613 return LABEL_ACCEPT;
1614 break;
1615 case -NF_DROP-1:
1616 return LABEL_DROP;
1617 break;
1618 case -NF_QUEUE-1:
1619 return LABEL_QUEUE;
1620 break;
1621 default:
1622 fprintf(stderr, "ERROR: %d not a valid target)\n",
1623 verdict);
1624 abort();
1625 break;
1626 }
1627 /* not reached */
1628 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001629}
1630
Harald Welteaae69be2004-08-29 23:32:14 +00001631/* Returns a pointer to the target name of this position. */
1632const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001633 struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001634{
1635 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001636 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Harald Welteaae69be2004-08-29 23:32:14 +00001637
1638 iptc_fn = TC_GET_TARGET;
1639
1640 switch(r->type) {
1641 int spos;
1642 case IPTCC_R_FALLTHROUGH:
1643 return "";
1644 break;
1645 case IPTCC_R_JUMP:
1646 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1647 return r->jump->name;
1648 break;
1649 case IPTCC_R_STANDARD:
1650 spos = *(int *)GET_TARGET(e)->data;
1651 DEBUGP("r=%p, spos=%d'\n", r, spos);
1652 return standard_target_map(spos);
1653 break;
1654 case IPTCC_R_MODULE:
1655 return GET_TARGET(e)->u.user.name;
1656 break;
1657 }
1658 return NULL;
1659}
Marc Bouchere6869a82000-03-20 06:03:29 +00001660/* Is this a built-in chain? Actually returns hook + 1. */
1661int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001662TC_BUILTIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001663{
Harald Welteaae69be2004-08-29 23:32:14 +00001664 struct chain_head *c;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001665
Harald Welteaae69be2004-08-29 23:32:14 +00001666 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001667
Harald Welteaae69be2004-08-29 23:32:14 +00001668 c = iptcc_find_label(chain, handle);
1669 if (!c) {
1670 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001671 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001672 }
Harald Welteaae69be2004-08-29 23:32:14 +00001673
1674 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001675}
1676
1677/* Get the policy of a given built-in chain */
1678const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001679TC_GET_POLICY(const char *chain,
1680 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001681 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001682{
Harald Welteaae69be2004-08-29 23:32:14 +00001683 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001684
Harald Welteaae69be2004-08-29 23:32:14 +00001685 iptc_fn = TC_GET_POLICY;
1686
1687 DEBUGP("called for chain %s\n", chain);
1688
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001689 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001690 if (!c) {
1691 errno = ENOENT;
1692 return NULL;
1693 }
1694
1695 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001696 return NULL;
1697
Harald Welteaae69be2004-08-29 23:32:14 +00001698 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001699
Harald Welteaae69be2004-08-29 23:32:14 +00001700 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001701}
1702
1703static int
Harald Welteaae69be2004-08-29 23:32:14 +00001704iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001705{
Harald Welteaae69be2004-08-29 23:32:14 +00001706 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001707 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001708
Rusty Russell79dee072000-05-02 16:45:16 +00001709 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001710
Rusty Russell67088e72000-05-10 01:18:57 +00001711 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001712 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001713 errno = EINVAL;
1714 return 0;
1715 }
1716 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001717 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1718 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001719 t->verdict = verdict;
1720
Harald Welteaae69be2004-08-29 23:32:14 +00001721 r->type = IPTCC_R_STANDARD;
1722
Marc Bouchere6869a82000-03-20 06:03:29 +00001723 return 1;
1724}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001725
Marc Bouchere6869a82000-03-20 06:03:29 +00001726static int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001727iptcc_map_target(struct xtc_handle *const handle,
Harald Welteaae69be2004-08-29 23:32:14 +00001728 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001729{
Harald Welteaae69be2004-08-29 23:32:14 +00001730 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001731 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001732
Marc Bouchere6869a82000-03-20 06:03:29 +00001733 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001734 if (strcmp(t->u.user.name, "") == 0) {
1735 r->type = IPTCC_R_FALLTHROUGH;
1736 return 1;
1737 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001738 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001739 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001740 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001741 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001742 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001743 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001744 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001745 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001746 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001747 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001748 /* Can't jump to builtins. */
1749 errno = EINVAL;
1750 return 0;
1751 } else {
1752 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001753 struct chain_head *c;
1754 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001755
Harald Welteaae69be2004-08-29 23:32:14 +00001756 c = iptcc_find_label(t->u.user.name, handle);
1757 if (c) {
1758 DEBUGP_C("found!\n");
1759 r->type = IPTCC_R_JUMP;
1760 r->jump = c;
1761 c->references++;
1762 return 1;
1763 }
1764 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001765 }
1766
1767 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001768 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001769 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001770 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001771 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001772 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001773 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001774 return 1;
1775}
1776
Harald Welte0113fe72004-01-06 19:04:02 +00001777/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001778int
Rusty Russell79dee072000-05-02 16:45:16 +00001779TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1780 const STRUCT_ENTRY *e,
1781 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001782 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001783{
Harald Welteaae69be2004-08-29 23:32:14 +00001784 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001785 struct rule_head *r;
1786 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001787
Rusty Russell79dee072000-05-02 16:45:16 +00001788 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001789
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001790 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001791 errno = ENOENT;
1792 return 0;
1793 }
1794
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001795 /* first rulenum index = 0
1796 first c->num_rules index = 1 */
1797 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001798 errno = E2BIG;
1799 return 0;
1800 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001801
Martin Josefsson631f3612004-09-23 19:25:06 +00001802 /* If we are inserting at the end just take advantage of the
1803 double linked list, insert will happen before the entry
1804 prev points to. */
1805 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001806 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001807 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001808 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001809 prev = &r->list;
1810 } else {
1811 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001812 prev = &r->list;
1813 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001814
Harald Welteaae69be2004-08-29 23:32:14 +00001815 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1816 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001817 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001818 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001819
Harald Welteaae69be2004-08-29 23:32:14 +00001820 memcpy(r->entry, e, e->next_offset);
1821 r->counter_map.maptype = COUNTER_MAP_SET;
1822
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001823 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001824 free(r);
1825 return 0;
1826 }
1827
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001828 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001829 c->num_rules++;
1830
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001831 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001832
1833 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001834}
1835
1836/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1837int
Rusty Russell79dee072000-05-02 16:45:16 +00001838TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1839 const STRUCT_ENTRY *e,
1840 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001841 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001842{
Harald Welteaae69be2004-08-29 23:32:14 +00001843 struct chain_head *c;
1844 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001845
Rusty Russell79dee072000-05-02 16:45:16 +00001846 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001847
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001848 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001849 errno = ENOENT;
1850 return 0;
1851 }
1852
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001853 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001854 errno = E2BIG;
1855 return 0;
1856 }
1857
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001858 /* Take advantage of the double linked list if possible. */
1859 if (rulenum + 1 <= c->num_rules/2) {
1860 old = iptcc_get_rule_num(c, rulenum + 1);
1861 } else {
1862 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1863 }
1864
Harald Welteaae69be2004-08-29 23:32:14 +00001865 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1866 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001867 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001868 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001869
Harald Welteaae69be2004-08-29 23:32:14 +00001870 memcpy(r->entry, e, e->next_offset);
1871 r->counter_map.maptype = COUNTER_MAP_SET;
1872
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001873 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001874 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001875 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001876 }
Harald Welte0113fe72004-01-06 19:04:02 +00001877
Harald Welteaae69be2004-08-29 23:32:14 +00001878 list_add(&r->list, &old->list);
1879 iptcc_delete_rule(old);
1880
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001881 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001882
1883 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001884}
1885
Harald Welte0113fe72004-01-06 19:04:02 +00001886/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001887 rulenum = length of chain. */
1888int
Rusty Russell79dee072000-05-02 16:45:16 +00001889TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1890 const STRUCT_ENTRY *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001891 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001892{
Harald Welteaae69be2004-08-29 23:32:14 +00001893 struct chain_head *c;
1894 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001895
Rusty Russell79dee072000-05-02 16:45:16 +00001896 iptc_fn = TC_APPEND_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001897 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00001898 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001899 errno = ENOENT;
1900 return 0;
1901 }
1902
Harald Welteaae69be2004-08-29 23:32:14 +00001903 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1904 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1905 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001906 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001907 }
Harald Welte0113fe72004-01-06 19:04:02 +00001908
Harald Welteaae69be2004-08-29 23:32:14 +00001909 memcpy(r->entry, e, e->next_offset);
1910 r->counter_map.maptype = COUNTER_MAP_SET;
1911
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001912 if (!iptcc_map_target(handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001913 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001914 free(r);
1915 return 0;
1916 }
1917
1918 list_add_tail(&r->list, &c->rules);
1919 c->num_rules++;
1920
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001921 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001922
1923 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001924}
1925
1926static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001927match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001928 const unsigned char *a_elems,
1929 const unsigned char *b_elems,
1930 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001931{
Rusty Russell79dee072000-05-02 16:45:16 +00001932 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001933 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001934
1935 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001936 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001937
Rusty Russell228e98d2000-04-27 10:28:06 +00001938 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001939 return 1;
1940
Rusty Russell228e98d2000-04-27 10:28:06 +00001941 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001942 return 1;
1943
Rusty Russell73ef09b2000-07-03 10:24:04 +00001944 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001945
Rusty Russell73ef09b2000-07-03 10:24:04 +00001946 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001947 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001948 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001949 *maskptr += i;
1950 return 0;
1951}
1952
1953static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001954target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001955{
1956 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001957 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001958
Rusty Russell733e54b2004-12-16 14:22:23 +00001959 if (a->type != b->type)
1960 return 0;
1961
1962 ta = GET_TARGET(a->entry);
1963 tb = GET_TARGET(b->entry);
1964
1965 switch (a->type) {
1966 case IPTCC_R_FALLTHROUGH:
1967 return 1;
1968 case IPTCC_R_JUMP:
1969 return a->jump == b->jump;
1970 case IPTCC_R_STANDARD:
1971 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1972 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1973 case IPTCC_R_MODULE:
1974 if (ta->u.target_size != tb->u.target_size)
1975 return 0;
1976 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1977 return 0;
1978
1979 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001980 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001981 return 0;
1982 return 1;
1983 default:
1984 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1985 abort();
1986 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001987}
1988
Rusty Russell733e54b2004-12-16 14:22:23 +00001989static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001990is_same(const STRUCT_ENTRY *a,
1991 const STRUCT_ENTRY *b,
1992 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001993
Harald Welte0113fe72004-01-06 19:04:02 +00001994/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001995int
Rusty Russell79dee072000-05-02 16:45:16 +00001996TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1997 const STRUCT_ENTRY *origfw,
1998 unsigned char *matchmask,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001999 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002000{
Harald Welteaae69be2004-08-29 23:32:14 +00002001 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00002002 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00002003
Rusty Russell79dee072000-05-02 16:45:16 +00002004 iptc_fn = TC_DELETE_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002005 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002006 errno = ENOENT;
2007 return 0;
2008 }
2009
Rusty Russelle45c7132004-12-16 13:21:44 +00002010 /* Create a rule_head from origfw. */
2011 r = iptcc_alloc_rule(c, origfw->next_offset);
2012 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00002013 errno = ENOMEM;
2014 return 0;
2015 }
2016
Rusty Russelle45c7132004-12-16 13:21:44 +00002017 memcpy(r->entry, origfw, origfw->next_offset);
2018 r->counter_map.maptype = COUNTER_MAP_NOMAP;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002019 if (!iptcc_map_target(handle, r)) {
Rusty Russelle45c7132004-12-16 13:21:44 +00002020 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
2021 free(r);
2022 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00002023 } else {
2024 /* iptcc_map_target increment target chain references
2025 * since this is a fake rule only used for matching
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002026 * the chain references count is decremented again.
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00002027 */
2028 if (r->type == IPTCC_R_JUMP
2029 && r->jump)
2030 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00002031 }
Harald Welte0113fe72004-01-06 19:04:02 +00002032
Rusty Russelle45c7132004-12-16 13:21:44 +00002033 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00002034 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00002035
Rusty Russell733e54b2004-12-16 14:22:23 +00002036 mask = is_same(r->entry, i->entry, matchmask);
2037 if (!mask)
2038 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002039
Rusty Russell733e54b2004-12-16 14:22:23 +00002040 if (!target_same(r, i, mask))
2041 continue;
2042
2043 /* If we are about to delete the rule that is the
2044 * current iterator, move rule iterator back. next
2045 * pointer will then point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002046 if (i == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002047 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002048 list_entry(handle->rule_iterator_cur->list.prev,
Rusty Russell733e54b2004-12-16 14:22:23 +00002049 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00002050 }
Rusty Russell733e54b2004-12-16 14:22:23 +00002051
2052 c->num_rules--;
2053 iptcc_delete_rule(i);
2054
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002055 set_changed(handle);
Rusty Russell733e54b2004-12-16 14:22:23 +00002056 free(r);
2057 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002058 }
2059
Rusty Russelle45c7132004-12-16 13:21:44 +00002060 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00002061 errno = ENOENT;
2062 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002063}
Harald Welteaae69be2004-08-29 23:32:14 +00002064
Marc Bouchere6869a82000-03-20 06:03:29 +00002065
2066/* Delete the rule in position `rulenum' in `chain'. */
2067int
Rusty Russell79dee072000-05-02 16:45:16 +00002068TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
2069 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002070 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002071{
Harald Welteaae69be2004-08-29 23:32:14 +00002072 struct chain_head *c;
2073 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002074
Rusty Russell79dee072000-05-02 16:45:16 +00002075 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002076
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002077 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002078 errno = ENOENT;
2079 return 0;
2080 }
2081
Martin Josefssona5616dc2004-10-24 22:27:31 +00002082 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00002083 errno = E2BIG;
2084 return 0;
2085 }
2086
2087 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00002088 if (rulenum + 1 <= c->num_rules/2) {
2089 r = iptcc_get_rule_num(c, rulenum + 1);
2090 } else {
2091 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002092 }
2093
Harald Welteaae69be2004-08-29 23:32:14 +00002094 /* If we are about to delete the rule that is the current
2095 * iterator, move rule iterator back. next pointer will then
2096 * point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002097 if (r == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002098 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002099 list_entry(handle->rule_iterator_cur->list.prev,
Harald Welteaae69be2004-08-29 23:32:14 +00002100 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002101 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002102
Harald Welteaae69be2004-08-29 23:32:14 +00002103 c->num_rules--;
2104 iptcc_delete_rule(r);
2105
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002106 set_changed(handle);
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002107
Harald Welteaae69be2004-08-29 23:32:14 +00002108 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002109}
2110
Marc Bouchere6869a82000-03-20 06:03:29 +00002111/* Flushes the entries in the given chain (ie. empties chain). */
2112int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002113TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002114{
Harald Welteaae69be2004-08-29 23:32:14 +00002115 struct chain_head *c;
2116 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002117
Harald Welte0113fe72004-01-06 19:04:02 +00002118 iptc_fn = TC_FLUSH_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002119 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002120 errno = ENOENT;
2121 return 0;
2122 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002123
Harald Welteaae69be2004-08-29 23:32:14 +00002124 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2125 iptcc_delete_rule(r);
2126 }
2127
2128 c->num_rules = 0;
2129
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002130 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002131
2132 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002133}
2134
2135/* Zeroes the counters in a chain. */
2136int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002137TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002138{
Harald Welteaae69be2004-08-29 23:32:14 +00002139 struct chain_head *c;
2140 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002141
Derrik Pates664c0a32005-02-01 13:28:14 +00002142 iptc_fn = TC_ZERO_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002143 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002144 errno = ENOENT;
2145 return 0;
2146 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002147
Andy Gaye5bd1d72006-08-22 02:56:41 +00002148 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2149 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2150
Harald Welteaae69be2004-08-29 23:32:14 +00002151 list_for_each_entry(r, &c->rules, list) {
2152 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2153 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002154 }
Harald Welteaae69be2004-08-29 23:32:14 +00002155
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002156 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002157
Marc Bouchere6869a82000-03-20 06:03:29 +00002158 return 1;
2159}
2160
Harald Welte1cef74d2001-01-05 15:22:59 +00002161STRUCT_COUNTERS *
2162TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2163 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002164 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002165{
Harald Welteaae69be2004-08-29 23:32:14 +00002166 struct chain_head *c;
2167 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002168
2169 iptc_fn = TC_READ_COUNTER;
2170 CHECK(*handle);
2171
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002172 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002173 errno = ENOENT;
2174 return NULL;
2175 }
2176
Harald Welteaae69be2004-08-29 23:32:14 +00002177 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002178 errno = E2BIG;
2179 return NULL;
2180 }
2181
Harald Welteaae69be2004-08-29 23:32:14 +00002182 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002183}
2184
2185int
2186TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2187 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002188 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002189{
Harald Welteaae69be2004-08-29 23:32:14 +00002190 struct chain_head *c;
2191 struct rule_head *r;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002192
Harald Welte1cef74d2001-01-05 15:22:59 +00002193 iptc_fn = TC_ZERO_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002194 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002195
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002196 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002197 errno = ENOENT;
2198 return 0;
2199 }
2200
Harald Welteaae69be2004-08-29 23:32:14 +00002201 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002202 errno = E2BIG;
2203 return 0;
2204 }
2205
Harald Welteaae69be2004-08-29 23:32:14 +00002206 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2207 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002208
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002209 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002210
2211 return 1;
2212}
2213
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002214int
Harald Welte1cef74d2001-01-05 15:22:59 +00002215TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2216 unsigned int rulenum,
2217 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002218 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002219{
Harald Welteaae69be2004-08-29 23:32:14 +00002220 struct chain_head *c;
2221 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002222 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002223
2224 iptc_fn = TC_SET_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002225 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002226
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002227 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002228 errno = ENOENT;
2229 return 0;
2230 }
Harald Welte0113fe72004-01-06 19:04:02 +00002231
Harald Welteaae69be2004-08-29 23:32:14 +00002232 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002233 errno = E2BIG;
2234 return 0;
2235 }
2236
Harald Welteaae69be2004-08-29 23:32:14 +00002237 e = r->entry;
2238 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002239
2240 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002241
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002242 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002243
2244 return 1;
2245}
2246
Marc Bouchere6869a82000-03-20 06:03:29 +00002247/* Creates a new chain. */
2248/* To create a chain, create two rules: error node and unconditional
2249 * return. */
2250int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002251TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002252{
Harald Welteaae69be2004-08-29 23:32:14 +00002253 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002254 int capacity;
2255 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002256
Rusty Russell79dee072000-05-02 16:45:16 +00002257 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002258
2259 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2260 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002261 if (iptcc_find_label(chain, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002262 || strcmp(chain, LABEL_DROP) == 0
2263 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002264 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002265 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002266 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002267 errno = EEXIST;
2268 return 0;
2269 }
2270
Rusty Russell79dee072000-05-02 16:45:16 +00002271 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002272 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002273 errno = EINVAL;
2274 return 0;
2275 }
2276
Harald Welteaae69be2004-08-29 23:32:14 +00002277 c = iptcc_alloc_chain_head(chain, 0);
2278 if (!c) {
2279 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2280 errno = ENOMEM;
2281 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002282
Harald Welteaae69be2004-08-29 23:32:14 +00002283 }
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002284 handle->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002285
Harald Welteaae69be2004-08-29 23:32:14 +00002286 DEBUGP("Creating chain `%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002287 iptc_insert_chain(handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002288
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002289 /* Inserting chains don't change the correctness of the chain
2290 * index (except if its smaller than index[0], but that
2291 * handled by iptc_insert_chain). It only causes longer lists
2292 * in the buckets. Thus, only rebuild chain index when the
2293 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2294 */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002295 capacity = handle->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2296 exceeded = handle->num_chains - capacity;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002297 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2298 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002299 capacity, exceeded, handle->num_chains);
2300 iptcc_chain_index_rebuild(handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002301 }
2302
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002303 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002304
Harald Welteaae69be2004-08-29 23:32:14 +00002305 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002306}
2307
2308/* Get the number of references to this chain. */
2309int
Rusty Russell79dee072000-05-02 16:45:16 +00002310TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002311 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002312{
Harald Welteaae69be2004-08-29 23:32:14 +00002313 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002314
Derrik Pates664c0a32005-02-01 13:28:14 +00002315 iptc_fn = TC_GET_REFERENCES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002316 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002317 errno = ENOENT;
2318 return 0;
2319 }
2320
Harald Welteaae69be2004-08-29 23:32:14 +00002321 *ref = c->references;
2322
Marc Bouchere6869a82000-03-20 06:03:29 +00002323 return 1;
2324}
2325
2326/* Deletes a chain. */
2327int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002328TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002329{
Marc Bouchere6869a82000-03-20 06:03:29 +00002330 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002331 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002332
Rusty Russell79dee072000-05-02 16:45:16 +00002333 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002334
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002335 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002336 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002337 errno = ENOENT;
2338 return 0;
2339 }
2340
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002341 if (TC_BUILTIN(chain, handle)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002342 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2343 errno = EINVAL;
2344 return 0;
2345 }
2346
2347 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2348 DEBUGP("cannot get references on chain `%s'\n", chain);
2349 return 0;
2350 }
2351
2352 if (references > 0) {
2353 DEBUGP("chain `%s' still has references\n", chain);
2354 errno = EMLINK;
2355 return 0;
2356 }
2357
2358 if (c->num_rules) {
2359 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002360 errno = ENOTEMPTY;
2361 return 0;
2362 }
2363
Harald Welteaae69be2004-08-29 23:32:14 +00002364 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002365 * iterator, move chain iterator forward. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002366 if (c == handle->chain_iterator_cur)
2367 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002368
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002369 handle->num_chains--; /* One user defined chain deleted */
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002370
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002371 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002372 iptcc_chain_index_delete_chain(c, handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002373 free(c);
2374
Harald Welteaae69be2004-08-29 23:32:14 +00002375 DEBUGP("chain `%s' deleted\n", chain);
2376
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002377 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002378
2379 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002380}
2381
2382/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002383int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2384 const IPT_CHAINLABEL newname,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002385 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002386{
Harald Welteaae69be2004-08-29 23:32:14 +00002387 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002388 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002389
Harald Welte1de80462000-10-30 12:00:27 +00002390 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2391 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002392 if (iptcc_find_label(newname, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002393 || strcmp(newname, LABEL_DROP) == 0
2394 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002395 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002396 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002397 errno = EEXIST;
2398 return 0;
2399 }
2400
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002401 if (!(c = iptcc_find_label(oldname, handle))
2402 || TC_BUILTIN(oldname, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002403 errno = ENOENT;
2404 return 0;
2405 }
2406
Rusty Russell79dee072000-05-02 16:45:16 +00002407 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002408 errno = EINVAL;
2409 return 0;
2410 }
2411
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002412 /* This only unlinks "c" from the list, thus no free(c) */
2413 iptcc_chain_index_delete_chain(c, handle);
2414
2415 /* Change the name of the chain */
Harald Welteaae69be2004-08-29 23:32:14 +00002416 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002417
2418 /* Insert sorted into to list again */
2419 iptc_insert_chain(handle, c);
2420
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002421 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002422
Marc Bouchere6869a82000-03-20 06:03:29 +00002423 return 1;
2424}
2425
2426/* Sets the policy on a built-in chain. */
2427int
Rusty Russell79dee072000-05-02 16:45:16 +00002428TC_SET_POLICY(const IPT_CHAINLABEL chain,
2429 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002430 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002431 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002432{
Harald Welteaae69be2004-08-29 23:32:14 +00002433 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002434
Rusty Russell79dee072000-05-02 16:45:16 +00002435 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002436
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002437 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002438 DEBUGP("cannot find chain `%s'\n", chain);
2439 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002440 return 0;
2441 }
2442
Harald Welteaae69be2004-08-29 23:32:14 +00002443 if (!iptcc_is_builtin(c)) {
2444 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2445 errno = ENOENT;
2446 return 0;
2447 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002448
Rusty Russell79dee072000-05-02 16:45:16 +00002449 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002450 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002451 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002452 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002453 else {
2454 errno = EINVAL;
2455 return 0;
2456 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002457
Harald Welte1cef74d2001-01-05 15:22:59 +00002458 if (counters) {
2459 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002460 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2461 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002462 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002463 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002464 }
2465
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002466 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002467
Marc Bouchere6869a82000-03-20 06:03:29 +00002468 return 1;
2469}
2470
2471/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002472 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002473 libiptc.c:833: fixed or forbidden register was spilled.
2474 This may be due to a compiler bug or to impossible asm
2475 statements or clauses.
2476*/
2477static void
Rusty Russell79dee072000-05-02 16:45:16 +00002478subtract_counters(STRUCT_COUNTERS *answer,
2479 const STRUCT_COUNTERS *a,
2480 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002481{
2482 answer->pcnt = a->pcnt - b->pcnt;
2483 answer->bcnt = a->bcnt - b->bcnt;
2484}
2485
Harald Welteaae69be2004-08-29 23:32:14 +00002486
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002487static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002488{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002489 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002490 DEBUGP_C("NOMAP => zero\n");
2491}
2492
2493static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002494 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002495 unsigned int mappos)
2496{
2497 /* Original read: X.
2498 * Atomic read on replacement: X + Y.
2499 * Currently in kernel: Z.
2500 * Want in kernel: X + Y + Z.
2501 * => Add in X + Y
2502 * => Add in replacement read.
2503 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002504 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002505 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2506}
2507
2508static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002509 STRUCT_REPLACE *repl, unsigned int idx,
2510 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002511{
2512 /* Original read: X.
2513 * Atomic read on replacement: X + Y.
2514 * Currently in kernel: Z.
2515 * Want in kernel: Y + Z.
2516 * => Add in Y.
2517 * => Add in (replacement read - original read).
2518 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002519 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002520 &repl->counters[mappos],
2521 counters);
2522 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2523}
2524
2525static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002526 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002527{
2528 /* Want to set counter (iptables-restore) */
2529
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002530 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002531 sizeof(STRUCT_COUNTERS));
2532
2533 DEBUGP_C("SET\n");
2534}
2535
2536
Marc Bouchere6869a82000-03-20 06:03:29 +00002537int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002538TC_COMMIT(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002539{
2540 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002541 STRUCT_REPLACE *repl;
2542 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002543 struct chain_head *c;
2544 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002545 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002546 int new_number;
2547 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002548
Derrik Pates664c0a32005-02-01 13:28:14 +00002549 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002550 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002551
Marc Bouchere6869a82000-03-20 06:03:29 +00002552 /* Don't commit if nothing changed. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002553 if (!handle->changed)
Marc Bouchere6869a82000-03-20 06:03:29 +00002554 goto finished;
2555
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002556 new_number = iptcc_compile_table_prep(handle, &new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002557 if (new_number < 0) {
2558 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002559 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002560 }
2561
2562 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002563 if (!repl) {
2564 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002565 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002566 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002567 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002568
Rusty Russelle45c7132004-12-16 13:21:44 +00002569#if 0
2570 TC_DUMP_ENTRIES(*handle);
2571#endif
2572
Harald Welteaae69be2004-08-29 23:32:14 +00002573 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2574 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002575
2576 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002577 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002578 * handle->info.num_entries);
Marc Bouchere6869a82000-03-20 06:03:29 +00002579 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002580 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002581 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002582 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002583 /* These are the counters we're going to put back, later. */
2584 newcounters = malloc(counterlen);
2585 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002586 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002587 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002588 }
Harald Welteaae69be2004-08-29 23:32:14 +00002589 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002590
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002591 strcpy(repl->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002592 repl->num_entries = new_number;
2593 repl->size = new_size;
2594
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002595 repl->num_counters = handle->info.num_entries;
2596 repl->valid_hooks = handle->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002597
2598 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2599 repl->num_entries, repl->size, repl->num_counters);
2600
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002601 ret = iptcc_compile_table(handle, repl);
Harald Welteaae69be2004-08-29 23:32:14 +00002602 if (ret < 0) {
2603 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002604 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002605 }
2606
2607
2608#ifdef IPTC_DEBUG2
2609 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002610 int fd = open("/tmp/libiptc-so_set_replace.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00002611 O_CREAT|O_WRONLY);
2612 if (fd >= 0) {
2613 write(fd, repl, sizeof(*repl) + repl->size);
2614 close(fd);
2615 }
2616 }
2617#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002618
Jan Engelhardt175f4512008-11-10 17:25:55 +01002619 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welted6ba6f52005-11-12 10:39:40 +00002620 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002621 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002622 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002623
2624 /* Put counters back. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002625 strcpy(newcounters->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002626 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002627
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002628 list_for_each_entry(c, &handle->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00002629 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002630
Harald Welteaae69be2004-08-29 23:32:14 +00002631 /* Builtin chains have their own counters */
2632 if (iptcc_is_builtin(c)) {
2633 DEBUGP("counter for chain-index %u: ", c->foot_index);
2634 switch(c->counter_map.maptype) {
2635 case COUNTER_MAP_NOMAP:
2636 counters_nomap(newcounters, c->foot_index);
2637 break;
2638 case COUNTER_MAP_NORMAL_MAP:
2639 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002640 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002641 c->counter_map.mappos);
2642 break;
2643 case COUNTER_MAP_ZEROED:
2644 counters_map_zeroed(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002645 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002646 c->counter_map.mappos,
2647 &c->counters);
2648 break;
2649 case COUNTER_MAP_SET:
2650 counters_map_set(newcounters, c->foot_index,
2651 &c->counters);
2652 break;
2653 }
2654 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002655
Harald Welteaae69be2004-08-29 23:32:14 +00002656 list_for_each_entry(r, &c->rules, list) {
2657 DEBUGP("counter for index %u: ", r->index);
2658 switch (r->counter_map.maptype) {
2659 case COUNTER_MAP_NOMAP:
2660 counters_nomap(newcounters, r->index);
2661 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002662
Harald Welteaae69be2004-08-29 23:32:14 +00002663 case COUNTER_MAP_NORMAL_MAP:
2664 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002665 r->index,
Harald Welteaae69be2004-08-29 23:32:14 +00002666 r->counter_map.mappos);
2667 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002668
Harald Welteaae69be2004-08-29 23:32:14 +00002669 case COUNTER_MAP_ZEROED:
2670 counters_map_zeroed(newcounters, repl,
2671 r->index,
2672 r->counter_map.mappos,
2673 &r->entry->counters);
2674 break;
2675
2676 case COUNTER_MAP_SET:
2677 counters_map_set(newcounters, r->index,
2678 &r->entry->counters);
2679 break;
2680 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002681 }
2682 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002683
Harald Welteaae69be2004-08-29 23:32:14 +00002684#ifdef IPTC_DEBUG2
2685 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002686 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00002687 O_CREAT|O_WRONLY);
2688 if (fd >= 0) {
2689 write(fd, newcounters, counterlen);
2690 close(fd);
2691 }
2692 }
2693#endif
2694
Jan Engelhardt175f4512008-11-10 17:25:55 +01002695 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
Harald Welted6ba6f52005-11-12 10:39:40 +00002696 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002697 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002698 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002699
2700 free(repl->counters);
2701 free(repl);
2702 free(newcounters);
2703
Harald Welted6ba6f52005-11-12 10:39:40 +00002704finished:
Marc Bouchere6869a82000-03-20 06:03:29 +00002705 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002706
2707out_free_newcounters:
2708 free(newcounters);
2709out_free_repl_counters:
2710 free(repl->counters);
2711out_free_repl:
2712 free(repl);
2713out_zero:
2714 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002715}
2716
Marc Bouchere6869a82000-03-20 06:03:29 +00002717/* Translates errno numbers into more human-readable form than strerror. */
2718const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002719TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002720{
2721 unsigned int i;
2722 struct table_struct {
2723 void *fn;
2724 int err;
2725 const char *message;
2726 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002727 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002728 { TC_INIT, EINVAL, "Module is wrong version" },
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002729 { TC_INIT, ENOENT,
Harald Welte4ccfa632001-07-30 15:12:43 +00002730 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002731 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2732 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2733 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002734 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002735 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2736 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2737 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2738 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002739 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2740 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002741 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2742 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002743 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002744 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002745 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002746 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002747 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002748 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002749 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002750
2751 { NULL, 0, "Incompatible with this kernel" },
2752 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2753 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2754 { NULL, ENOMEM, "Memory allocation problem" },
2755 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002756 };
2757
2758 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2759 if ((!table[i].fn || table[i].fn == iptc_fn)
2760 && table[i].err == err)
2761 return table[i].message;
2762 }
2763
2764 return strerror(err);
2765}