blob: 7a9c74281a9795497941f8521b1fb52d0cb28608 [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{
Jan Engelhardt51651b62009-10-23 23:35:49 +0200819 const unsigned char *data;
820
Harald Welteaae69be2004-08-29 23:32:14 +0000821 if (h->chain_iterator_cur) {
822 /* policy rule is last rule */
823 struct rule_head *pr = (struct rule_head *)
824 h->chain_iterator_cur->rules.prev;
825
826 /* save verdict */
Jan Engelhardt51651b62009-10-23 23:35:49 +0200827 data = GET_TARGET(pr->entry)->data;
828 h->chain_iterator_cur->verdict = *(const int *)data;
Harald Welteaae69be2004-08-29 23:32:14 +0000829
830 /* save counter and counter_map information */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100831 h->chain_iterator_cur->counter_map.maptype =
Jan Engelhardt7c4d6682009-10-26 18:43:54 +0100832 COUNTER_MAP_ZEROED;
Harald Welteaae69be2004-08-29 23:32:14 +0000833 h->chain_iterator_cur->counter_map.mappos = num-1;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100834 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
Harald Welteaae69be2004-08-29 23:32:14 +0000835 sizeof(h->chain_iterator_cur->counters));
836
837 /* foot_offset points to verdict rule */
838 h->chain_iterator_cur->foot_index = num;
839 h->chain_iterator_cur->foot_offset = pr->offset;
840
841 /* delete rule from cache */
842 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000843 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000844
845 return 1;
846 }
847 return 0;
848}
849
Harald Welteec30b6c2005-02-01 16:45:56 +0000850/* alphabetically insert a chain into the list */
Christoph Paasch7cd15e32009-03-23 13:50:11 +0100851static void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c)
Harald Welteec30b6c2005-02-01 16:45:56 +0000852{
853 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000854 struct list_head *list_start_pos;
855 unsigned int i=1;
856
857 /* Find a smart place to start the insert search */
858 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
859
860 /* Handle the case, where chain.name is smaller than index[0] */
861 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
862 h->chain_index[0] = c; /* Update chain index head */
863 list_start_pos = h->chains.next;
864 debug("Update chain_index[0] with %s\n", c->name);
865 }
866
867 /* Handel if bsearch bails out early */
868 if (list_start_pos == &h->chains) {
869 list_start_pos = h->chains.next;
870 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000871
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000872 /* sort only user defined chains */
873 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000874 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000875 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000876 list_add(&c->list, tmp->list.prev);
877 return;
878 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000879
880 /* Stop if list head is reached */
881 if (&tmp->list == &h->chains) {
882 debug("Insert, list head reached add to tail\n");
883 break;
884 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000885 }
886 }
887
888 /* survived till end of list: add at tail */
889 list_add_tail(&c->list, &h->chains);
890}
891
Harald Welteaae69be2004-08-29 23:32:14 +0000892/* Another ugly helper function split out of cache_add_entry to make it less
893 * spaghetti code */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100894static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c,
Harald Welteaae69be2004-08-29 23:32:14 +0000895 unsigned int offset, unsigned int *num)
896{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000897 struct list_head *tail = h->chains.prev;
898 struct chain_head *ctail;
899
Harald Welteaae69be2004-08-29 23:32:14 +0000900 __iptcc_p_del_policy(h, *num);
901
902 c->head_offset = offset;
903 c->index = *num;
904
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000905 /* Chains from kernel are already sorted, as they are inserted
906 * sorted. But there exists an issue when shifting to 1.4.0
907 * from an older version, as old versions allow last created
908 * chain to be unsorted.
909 */
910 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
911 list_add_tail(&c->list, &h->chains);
912 else {
913 ctail = list_entry(tail, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200914
Jesper Dangaard Brouer526d3e12008-07-03 20:29:34 +0200915 if (strcmp(c->name, ctail->name) > 0 ||
916 iptcc_is_builtin(ctail))
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000917 list_add_tail(&c->list, &h->chains);/* Already sorted*/
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200918 else {
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000919 iptc_insert_chain(h, c);/* Was not sorted */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200920
921 /* Notice, if chains were not received sorted
922 * from kernel, then an offset bsearch is no
923 * longer valid.
924 */
925 h->sorted_offsets = 0;
926
927 debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n",
928 c->name, ctail->name);
929 }
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000930 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000931
Harald Welteaae69be2004-08-29 23:32:14 +0000932 h->chain_iterator_cur = c;
933}
934
935/* main parser function: add an entry from the blob to the cache */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100936static int cache_add_entry(STRUCT_ENTRY *e,
937 struct xtc_handle *h,
Harald Welteaae69be2004-08-29 23:32:14 +0000938 STRUCT_ENTRY **prev,
939 unsigned int *num)
940{
941 unsigned int builtin;
942 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
943
944 DEBUGP("entering...");
945
946 /* Last entry ("policy rule"). End it.*/
947 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
948 /* This is the ERROR node at the end of the chain */
949 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
950
951 __iptcc_p_del_policy(h, *num);
952
953 h->chain_iterator_cur = NULL;
954 goto out_inc;
955 }
956
957 /* We know this is the start of a new chain if it's an ERROR
958 * target, or a hook entry point */
959
960 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100961 struct chain_head *c =
Harald Welteaae69be2004-08-29 23:32:14 +0000962 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100963 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
Harald Welteaae69be2004-08-29 23:32:14 +0000964 (char *)c->name, c);
965 if (!c) {
966 errno = -ENOMEM;
967 return -1;
968 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000969 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000970
971 __iptcc_p_add_chain(h, c, offset, num);
972
973 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
974 struct chain_head *c =
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100975 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
Harald Welteaae69be2004-08-29 23:32:14 +0000976 builtin);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100977 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
Harald Welteaae69be2004-08-29 23:32:14 +0000978 *num, offset, c, &c->rules);
979 if (!c) {
980 errno = -ENOMEM;
981 return -1;
982 }
983
984 c->hooknum = builtin;
985
986 __iptcc_p_add_chain(h, c, offset, num);
987
988 /* FIXME: this is ugly. */
989 goto new_rule;
990 } else {
991 /* has to be normal rule */
992 struct rule_head *r;
993new_rule:
994
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100995 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
Harald Welteaae69be2004-08-29 23:32:14 +0000996 e->next_offset))) {
997 errno = ENOMEM;
998 return -1;
999 }
1000 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
1001
1002 r->index = *num;
1003 r->offset = offset;
1004 memcpy(r->entry, e, e->next_offset);
1005 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
1006 r->counter_map.mappos = r->index;
1007
1008 /* handling of jumps, etc. */
1009 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
1010 STRUCT_STANDARD_TARGET *t;
1011
1012 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
1013 if (t->target.u.target_size
1014 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
1015 errno = EINVAL;
1016 return -1;
1017 }
1018
1019 if (t->verdict < 0) {
1020 DEBUGP_C("standard, verdict=%d\n", t->verdict);
1021 r->type = IPTCC_R_STANDARD;
1022 } else if (t->verdict == r->offset+e->next_offset) {
1023 DEBUGP_C("fallthrough\n");
1024 r->type = IPTCC_R_FALLTHROUGH;
1025 } else {
1026 DEBUGP_C("jump, target=%u\n", t->verdict);
1027 r->type = IPTCC_R_JUMP;
1028 /* Jump target fixup has to be deferred
1029 * until second pass, since we migh not
1030 * yet have parsed the target */
1031 }
Martin Josefsson52c38022004-09-22 19:39:40 +00001032 } else {
1033 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
1034 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001035 }
1036
1037 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +00001038 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +00001039 }
1040out_inc:
1041 (*num)++;
1042 return 0;
1043}
1044
1045
1046/* parse an iptables blob into it's pieces */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001047static int parse_table(struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +00001048{
1049 STRUCT_ENTRY *prev;
1050 unsigned int num = 0;
1051 struct chain_head *c;
1052
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +02001053 /* Assume that chains offsets are sorted, this verified during
1054 parsing of ruleset (in __iptcc_p_add_chain())*/
1055 h->sorted_offsets = 1;
1056
Harald Welteaae69be2004-08-29 23:32:14 +00001057 /* First pass: over ruleset blob */
1058 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
1059 cache_add_entry, h, &prev, &num);
1060
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001061 /* Build the chain index, used for chain list search speedup */
1062 if ((iptcc_chain_index_alloc(h)) < 0)
1063 return -ENOMEM;
1064 iptcc_chain_index_build(h);
1065
Harald Welteaae69be2004-08-29 23:32:14 +00001066 /* Second pass: fixup parsed data from first pass */
1067 list_for_each_entry(c, &h->chains, list) {
1068 struct rule_head *r;
1069 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001070 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +00001071 STRUCT_STANDARD_TARGET *t;
1072
1073 if (r->type != IPTCC_R_JUMP)
1074 continue;
1075
1076 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001077 lc = iptcc_find_chain_by_offset(h, t->verdict);
1078 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +00001079 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001080 r->jump = lc;
1081 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +00001082 }
1083 }
1084
Harald Welteaae69be2004-08-29 23:32:14 +00001085 return 1;
1086}
1087
1088
1089/**********************************************************************
1090 * RULESET COMPILATION (cache -> blob)
1091 **********************************************************************/
1092
1093/* Convenience structures */
1094struct iptcb_chain_start{
1095 STRUCT_ENTRY e;
1096 struct ipt_error_target name;
1097};
1098#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
1099 ALIGN(sizeof(struct ipt_error_target)))
1100
1101struct iptcb_chain_foot {
1102 STRUCT_ENTRY e;
1103 STRUCT_STANDARD_TARGET target;
1104};
1105#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1106 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1107
1108struct iptcb_chain_error {
1109 STRUCT_ENTRY entry;
1110 struct ipt_error_target target;
1111};
1112#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
1113 ALIGN(sizeof(struct ipt_error_target)))
1114
1115
1116
1117/* compile rule from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001118static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r)
Harald Welteaae69be2004-08-29 23:32:14 +00001119{
1120 /* handle jumps */
1121 if (r->type == IPTCC_R_JUMP) {
1122 STRUCT_STANDARD_TARGET *t;
1123 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1124 /* memset for memcmp convenience on delete/replace */
1125 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1126 strcpy(t->target.u.user.name, STANDARD_TARGET);
1127 /* Jumps can only happen to builtin chains, so we
1128 * can safely assume that they always have a header */
1129 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1130 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1131 STRUCT_STANDARD_TARGET *t;
1132 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1133 t->verdict = r->offset + r->size;
1134 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001135
Harald Welteaae69be2004-08-29 23:32:14 +00001136 /* copy entry from cache to blob */
1137 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1138
1139 return 1;
1140}
1141
1142/* compile chain from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001143static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +00001144{
1145 int ret;
1146 struct rule_head *r;
1147 struct iptcb_chain_start *head;
1148 struct iptcb_chain_foot *foot;
1149
1150 /* only user-defined chains have heaer */
1151 if (!iptcc_is_builtin(c)) {
1152 /* put chain header in place */
1153 head = (void *)repl->entries + c->head_offset;
1154 head->e.target_offset = sizeof(STRUCT_ENTRY);
1155 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
1156 strcpy(head->name.t.u.user.name, ERROR_TARGET);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001157 head->name.t.u.target_size =
Harald Welteaae69be2004-08-29 23:32:14 +00001158 ALIGN(sizeof(struct ipt_error_target));
1159 strcpy(head->name.error, c->name);
1160 } else {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001161 repl->hook_entry[c->hooknum-1] = c->head_offset;
Harald Welteaae69be2004-08-29 23:32:14 +00001162 repl->underflow[c->hooknum-1] = c->foot_offset;
1163 }
1164
1165 /* iterate over rules */
1166 list_for_each_entry(r, &c->rules, list) {
1167 ret = iptcc_compile_rule(h, repl, r);
1168 if (ret < 0)
1169 return ret;
1170 }
1171
1172 /* put chain footer in place */
1173 foot = (void *)repl->entries + c->foot_offset;
1174 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1175 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1176 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1177 foot->target.target.u.target_size =
1178 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1179 /* builtin targets have verdict, others return */
1180 if (iptcc_is_builtin(c))
1181 foot->target.verdict = c->verdict;
1182 else
1183 foot->target.verdict = RETURN;
1184 /* set policy-counters */
1185 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1186
1187 return 0;
1188}
1189
1190/* calculate offset and number for every rule in the cache */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001191static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001192 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001193{
1194 struct rule_head *r;
1195
1196 c->head_offset = *offset;
1197 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1198
1199 if (!iptcc_is_builtin(c)) {
1200 /* Chain has header */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001201 *offset += sizeof(STRUCT_ENTRY)
Harald Welteaae69be2004-08-29 23:32:14 +00001202 + ALIGN(sizeof(struct ipt_error_target));
1203 (*num)++;
1204 }
1205
1206 list_for_each_entry(r, &c->rules, list) {
1207 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1208 r->offset = *offset;
1209 r->index = *num;
1210 *offset += r->size;
1211 (*num)++;
1212 }
1213
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001214 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
Harald Welteaae69be2004-08-29 23:32:14 +00001215 *offset, *num);
1216 c->foot_offset = *offset;
1217 c->foot_index = *num;
1218 *offset += sizeof(STRUCT_ENTRY)
1219 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1220 (*num)++;
1221
1222 return 1;
1223}
1224
1225/* put the pieces back together again */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001226static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size)
Harald Welteaae69be2004-08-29 23:32:14 +00001227{
1228 struct chain_head *c;
1229 unsigned int offset = 0, num = 0;
1230 int ret = 0;
1231
1232 /* First pass: calculate offset for every rule */
1233 list_for_each_entry(c, &h->chains, list) {
1234 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1235 if (ret < 0)
1236 return ret;
1237 }
1238
1239 /* Append one error rule at end of chain */
1240 num++;
1241 offset += sizeof(STRUCT_ENTRY)
1242 + ALIGN(sizeof(struct ipt_error_target));
1243
1244 /* ruleset size is now in offset */
1245 *size = offset;
1246 return num;
1247}
1248
Jan Engelhardtfd187312008-11-10 16:59:27 +01001249static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl)
Harald Welteaae69be2004-08-29 23:32:14 +00001250{
1251 struct chain_head *c;
1252 struct iptcb_chain_error *error;
1253
1254 /* Second pass: copy from cache to offsets, fill in jumps */
1255 list_for_each_entry(c, &h->chains, list) {
1256 int ret = iptcc_compile_chain(h, repl, c);
1257 if (ret < 0)
1258 return ret;
1259 }
1260
1261 /* Append error rule at end of chain */
1262 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1263 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1264 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001265 error->target.t.u.user.target_size =
Harald Welteaae69be2004-08-29 23:32:14 +00001266 ALIGN(sizeof(struct ipt_error_target));
1267 strcpy((char *)&error->target.t.u.user.name, ERROR_TARGET);
1268 strcpy((char *)&error->target.error, "ERROR");
1269
1270 return 1;
1271}
1272
1273/**********************************************************************
1274 * EXTERNAL API (operates on cache only)
1275 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001276
1277/* Allocate handle of given size */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001278static struct xtc_handle *
Harald Welte0113fe72004-01-06 19:04:02 +00001279alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001280{
1281 size_t len;
Jan Engelhardtfd187312008-11-10 16:59:27 +01001282 struct xtc_handle *h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001283
Harald Welteaae69be2004-08-29 23:32:14 +00001284 len = sizeof(STRUCT_TC_HANDLE) + size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001285
Harald Welteaae69be2004-08-29 23:32:14 +00001286 h = malloc(sizeof(STRUCT_TC_HANDLE));
1287 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001288 errno = ENOMEM;
1289 return NULL;
1290 }
Harald Welteaae69be2004-08-29 23:32:14 +00001291 memset(h, 0, sizeof(*h));
1292 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001293 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001294
Harald Welte0371c0c2004-09-19 21:00:12 +00001295 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001296 if (!h->entries)
1297 goto out_free_handle;
1298
1299 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001300 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001301
1302 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001303
1304out_free_handle:
1305 free(h);
1306
1307 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001308}
1309
Harald Welteaae69be2004-08-29 23:32:14 +00001310
Jan Engelhardtfd187312008-11-10 16:59:27 +01001311struct xtc_handle *
Rusty Russell79dee072000-05-02 16:45:16 +00001312TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001313{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001314 struct xtc_handle *h;
Rusty Russell79dee072000-05-02 16:45:16 +00001315 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001316 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001317 socklen_t s;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001318 int sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001319
Rusty Russell79dee072000-05-02 16:45:16 +00001320 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001321
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001322 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1323 errno = EINVAL;
1324 return NULL;
1325 }
Jan Engelhardt175f4512008-11-10 17:25:55 +01001326
1327 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1328 if (sockfd < 0)
1329 return NULL;
1330
Patrick McHardy2f932052008-04-02 14:01:53 +02001331retry:
Marc Bouchere6869a82000-03-20 06:03:29 +00001332 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001333
Marc Bouchere6869a82000-03-20 06:03:29 +00001334 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001335 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001336 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001337 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001338 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001339
Harald Welteaae69be2004-08-29 23:32:14 +00001340 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1341 info.valid_hooks, info.num_entries, info.size);
1342
Harald Welte0113fe72004-01-06 19:04:02 +00001343 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001344 == NULL) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001345 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001346 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001347 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001348
Marc Bouchere6869a82000-03-20 06:03:29 +00001349 /* Initialize current state */
Jan Engelhardt175f4512008-11-10 17:25:55 +01001350 h->sockfd = sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001351 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001352
Harald Welteaae69be2004-08-29 23:32:14 +00001353 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001354
Rusty Russell79dee072000-05-02 16:45:16 +00001355 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001356
Jan Engelhardt175f4512008-11-10 17:25:55 +01001357 if (getsockopt(h->sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
Harald Welteaae69be2004-08-29 23:32:14 +00001358 &tmp) < 0)
1359 goto error;
1360
1361#ifdef IPTC_DEBUG2
1362 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001363 int fd = open("/tmp/libiptc-so_get_entries.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00001364 O_CREAT|O_WRONLY);
1365 if (fd >= 0) {
1366 write(fd, h->entries, tmp);
1367 close(fd);
1368 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001369 }
Harald Welteaae69be2004-08-29 23:32:14 +00001370#endif
1371
1372 if (parse_table(h) < 0)
1373 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001374
Marc Bouchere6869a82000-03-20 06:03:29 +00001375 CHECK(h);
1376 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001377error:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001378 TC_FREE(h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001379 /* A different process changed the ruleset size, retry */
1380 if (errno == EAGAIN)
1381 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001382 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001383}
1384
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001385void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001386TC_FREE(struct xtc_handle *h)
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001387{
Harald Welteaae69be2004-08-29 23:32:14 +00001388 struct chain_head *c, *tmp;
1389
Derrik Pates664c0a32005-02-01 13:28:14 +00001390 iptc_fn = TC_FREE;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001391 close(h->sockfd);
Harald Welteaae69be2004-08-29 23:32:14 +00001392
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001393 list_for_each_entry_safe(c, tmp, &h->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00001394 struct rule_head *r, *rtmp;
1395
1396 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1397 free(r);
1398 }
1399
1400 free(c);
1401 }
1402
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001403 iptcc_chain_index_free(h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001404
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001405 free(h->entries);
1406 free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001407}
1408
Marc Bouchere6869a82000-03-20 06:03:29 +00001409static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001410print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001411{
Rusty Russell228e98d2000-04-27 10:28:06 +00001412 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001413 return 0;
1414}
1415
Jan Engelhardtfd187312008-11-10 16:59:27 +01001416static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001417
Marc Bouchere6869a82000-03-20 06:03:29 +00001418void
Jan Engelhardtfd187312008-11-10 16:59:27 +01001419TC_DUMP_ENTRIES(struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001420{
Derrik Pates664c0a32005-02-01 13:28:14 +00001421 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001422 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001423
Rusty Russelle45c7132004-12-16 13:21:44 +00001424 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001425 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001426 printf("Table `%s'\n", handle->info.name);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001427 printf("Hooks: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001428 handle->info.hook_entry[HOOK_PRE_ROUTING],
1429 handle->info.hook_entry[HOOK_LOCAL_IN],
1430 handle->info.hook_entry[HOOK_FORWARD],
1431 handle->info.hook_entry[HOOK_LOCAL_OUT],
1432 handle->info.hook_entry[HOOK_POST_ROUTING]);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001433 printf("Underflows: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001434 handle->info.underflow[HOOK_PRE_ROUTING],
1435 handle->info.underflow[HOOK_LOCAL_IN],
1436 handle->info.underflow[HOOK_FORWARD],
1437 handle->info.underflow[HOOK_LOCAL_OUT],
1438 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001439
Harald Welteaae69be2004-08-29 23:32:14 +00001440 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001441 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001442}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001443
Marc Bouchere6869a82000-03-20 06:03:29 +00001444/* Does this chain exist? */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001445int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001446{
Derrik Pates664c0a32005-02-01 13:28:14 +00001447 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001448 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001449}
1450
Jan Engelhardtfd187312008-11-10 16:59:27 +01001451static void iptcc_chain_iterator_advance(struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001452{
1453 struct chain_head *c = handle->chain_iterator_cur;
1454
1455 if (c->list.next == &handle->chains)
1456 handle->chain_iterator_cur = NULL;
1457 else
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001458 handle->chain_iterator_cur =
Harald Welteaae69be2004-08-29 23:32:14 +00001459 list_entry(c->list.next, struct chain_head, list);
1460}
Marc Bouchere6869a82000-03-20 06:03:29 +00001461
Rusty Russell30fd6e52000-04-23 09:16:06 +00001462/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001463const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001464TC_FIRST_CHAIN(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001465{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001466 struct chain_head *c = list_entry(handle->chains.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001467 struct chain_head, list);
1468
1469 iptc_fn = TC_FIRST_CHAIN;
1470
1471
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001472 if (list_empty(&handle->chains)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001473 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001474 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001475 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001476
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001477 handle->chain_iterator_cur = c;
1478 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001479
Harald Welteaae69be2004-08-29 23:32:14 +00001480 DEBUGP(": returning `%s'\n", c->name);
1481 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001482}
1483
Rusty Russell30fd6e52000-04-23 09:16:06 +00001484/* Iterator functions to run through the chains. Returns NULL at end. */
1485const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001486TC_NEXT_CHAIN(struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001487{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001488 struct chain_head *c = handle->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001489
Harald Welteaae69be2004-08-29 23:32:14 +00001490 iptc_fn = TC_NEXT_CHAIN;
1491
1492 if (!c) {
1493 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001494 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001495 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001496
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001497 iptcc_chain_iterator_advance(handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001498
Harald Welteaae69be2004-08-29 23:32:14 +00001499 DEBUGP(": returning `%s'\n", c->name);
1500 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001501}
1502
1503/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001504const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001505TC_FIRST_RULE(const char *chain, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001506{
Harald Welteaae69be2004-08-29 23:32:14 +00001507 struct chain_head *c;
1508 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001509
Harald Welteaae69be2004-08-29 23:32:14 +00001510 iptc_fn = TC_FIRST_RULE;
1511
1512 DEBUGP("first rule(%s): ", chain);
1513
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001514 c = iptcc_find_label(chain, handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001515 if (!c) {
1516 errno = ENOENT;
1517 return NULL;
1518 }
1519
1520 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001521 if (list_empty(&c->rules)) {
1522 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001523 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001524 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001525
Harald Welteaae69be2004-08-29 23:32:14 +00001526 r = list_entry(c->rules.next, struct rule_head, list);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001527 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001528 DEBUGP_C("%p\n", r);
1529
1530 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001531}
1532
1533/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001534const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001535TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001536{
Harald Welteaae69be2004-08-29 23:32:14 +00001537 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001538
Derrik Pates664c0a32005-02-01 13:28:14 +00001539 iptc_fn = TC_NEXT_RULE;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001540 DEBUGP("rule_iterator_cur=%p...", handle->rule_iterator_cur);
Harald Welteaae69be2004-08-29 23:32:14 +00001541
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001542 if (handle->rule_iterator_cur == NULL) {
Harald Welteaae69be2004-08-29 23:32:14 +00001543 DEBUGP_C("returning NULL\n");
1544 return NULL;
1545 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001546
1547 r = list_entry(handle->rule_iterator_cur->list.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001548 struct rule_head, list);
1549
1550 iptc_fn = TC_NEXT_RULE;
1551
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001552 DEBUGP_C("next=%p, head=%p...", &r->list,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001553 &handle->rule_iterator_cur->chain->rules);
Harald Welteaae69be2004-08-29 23:32:14 +00001554
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001555 if (&r->list == &handle->rule_iterator_cur->chain->rules) {
1556 handle->rule_iterator_cur = NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001557 DEBUGP_C("finished, returning NULL\n");
1558 return NULL;
1559 }
1560
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001561 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001562
1563 /* NOTE: prev is without any influence ! */
1564 DEBUGP_C("returning rule %p\n", r);
1565 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001566}
1567
Marc Bouchere6869a82000-03-20 06:03:29 +00001568/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001569static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001570{
Harald Welteaae69be2004-08-29 23:32:14 +00001571 switch (verdict) {
1572 case RETURN:
1573 return LABEL_RETURN;
1574 break;
1575 case -NF_ACCEPT-1:
1576 return LABEL_ACCEPT;
1577 break;
1578 case -NF_DROP-1:
1579 return LABEL_DROP;
1580 break;
1581 case -NF_QUEUE-1:
1582 return LABEL_QUEUE;
1583 break;
1584 default:
1585 fprintf(stderr, "ERROR: %d not a valid target)\n",
1586 verdict);
1587 abort();
1588 break;
1589 }
1590 /* not reached */
1591 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001592}
1593
Harald Welteaae69be2004-08-29 23:32:14 +00001594/* Returns a pointer to the target name of this position. */
1595const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001596 struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001597{
1598 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001599 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Jan Engelhardt51651b62009-10-23 23:35:49 +02001600 const unsigned char *data;
Harald Welteaae69be2004-08-29 23:32:14 +00001601
1602 iptc_fn = TC_GET_TARGET;
1603
1604 switch(r->type) {
1605 int spos;
1606 case IPTCC_R_FALLTHROUGH:
1607 return "";
1608 break;
1609 case IPTCC_R_JUMP:
1610 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1611 return r->jump->name;
1612 break;
1613 case IPTCC_R_STANDARD:
Jan Engelhardt51651b62009-10-23 23:35:49 +02001614 data = GET_TARGET(e)->data;
1615 spos = *(const int *)data;
Harald Welteaae69be2004-08-29 23:32:14 +00001616 DEBUGP("r=%p, spos=%d'\n", r, spos);
1617 return standard_target_map(spos);
1618 break;
1619 case IPTCC_R_MODULE:
1620 return GET_TARGET(e)->u.user.name;
1621 break;
1622 }
1623 return NULL;
1624}
Marc Bouchere6869a82000-03-20 06:03:29 +00001625/* Is this a built-in chain? Actually returns hook + 1. */
1626int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001627TC_BUILTIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001628{
Harald Welteaae69be2004-08-29 23:32:14 +00001629 struct chain_head *c;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001630
Harald Welteaae69be2004-08-29 23:32:14 +00001631 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001632
Harald Welteaae69be2004-08-29 23:32:14 +00001633 c = iptcc_find_label(chain, handle);
1634 if (!c) {
1635 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001636 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001637 }
Harald Welteaae69be2004-08-29 23:32:14 +00001638
1639 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001640}
1641
1642/* Get the policy of a given built-in chain */
1643const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001644TC_GET_POLICY(const char *chain,
1645 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001646 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001647{
Harald Welteaae69be2004-08-29 23:32:14 +00001648 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001649
Harald Welteaae69be2004-08-29 23:32:14 +00001650 iptc_fn = TC_GET_POLICY;
1651
1652 DEBUGP("called for chain %s\n", chain);
1653
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001654 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001655 if (!c) {
1656 errno = ENOENT;
1657 return NULL;
1658 }
1659
1660 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001661 return NULL;
1662
Harald Welteaae69be2004-08-29 23:32:14 +00001663 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001664
Harald Welteaae69be2004-08-29 23:32:14 +00001665 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001666}
1667
1668static int
Harald Welteaae69be2004-08-29 23:32:14 +00001669iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001670{
Harald Welteaae69be2004-08-29 23:32:14 +00001671 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001672 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001673
Rusty Russell79dee072000-05-02 16:45:16 +00001674 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001675
Rusty Russell67088e72000-05-10 01:18:57 +00001676 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001677 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001678 errno = EINVAL;
1679 return 0;
1680 }
1681 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001682 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1683 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001684 t->verdict = verdict;
1685
Harald Welteaae69be2004-08-29 23:32:14 +00001686 r->type = IPTCC_R_STANDARD;
1687
Marc Bouchere6869a82000-03-20 06:03:29 +00001688 return 1;
1689}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001690
Marc Bouchere6869a82000-03-20 06:03:29 +00001691static int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001692iptcc_map_target(struct xtc_handle *const handle,
Harald Welteaae69be2004-08-29 23:32:14 +00001693 struct rule_head *r)
Marc Bouchere6869a82000-03-20 06:03:29 +00001694{
Harald Welteaae69be2004-08-29 23:32:14 +00001695 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001696 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001697
Marc Bouchere6869a82000-03-20 06:03:29 +00001698 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001699 if (strcmp(t->u.user.name, "") == 0) {
1700 r->type = IPTCC_R_FALLTHROUGH;
1701 return 1;
1702 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001703 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001704 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001705 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001706 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001707 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001708 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001709 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001710 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001711 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001712 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001713 /* Can't jump to builtins. */
1714 errno = EINVAL;
1715 return 0;
1716 } else {
1717 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001718 struct chain_head *c;
1719 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001720
Harald Welteaae69be2004-08-29 23:32:14 +00001721 c = iptcc_find_label(t->u.user.name, handle);
1722 if (c) {
1723 DEBUGP_C("found!\n");
1724 r->type = IPTCC_R_JUMP;
1725 r->jump = c;
1726 c->references++;
1727 return 1;
1728 }
1729 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001730 }
1731
1732 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001733 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001734 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001735 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001736 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001737 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001738 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001739 return 1;
1740}
1741
Harald Welte0113fe72004-01-06 19:04:02 +00001742/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001743int
Rusty Russell79dee072000-05-02 16:45:16 +00001744TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1745 const STRUCT_ENTRY *e,
1746 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001747 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001748{
Harald Welteaae69be2004-08-29 23:32:14 +00001749 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001750 struct rule_head *r;
1751 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001752
Rusty Russell79dee072000-05-02 16:45:16 +00001753 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001754
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001755 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001756 errno = ENOENT;
1757 return 0;
1758 }
1759
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001760 /* first rulenum index = 0
1761 first c->num_rules index = 1 */
1762 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001763 errno = E2BIG;
1764 return 0;
1765 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001766
Martin Josefsson631f3612004-09-23 19:25:06 +00001767 /* If we are inserting at the end just take advantage of the
1768 double linked list, insert will happen before the entry
1769 prev points to. */
1770 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001771 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001772 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001773 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001774 prev = &r->list;
1775 } else {
1776 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001777 prev = &r->list;
1778 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001779
Harald Welteaae69be2004-08-29 23:32:14 +00001780 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1781 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001782 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001783 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001784
Harald Welteaae69be2004-08-29 23:32:14 +00001785 memcpy(r->entry, e, e->next_offset);
1786 r->counter_map.maptype = COUNTER_MAP_SET;
1787
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001788 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001789 free(r);
1790 return 0;
1791 }
1792
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001793 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001794 c->num_rules++;
1795
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001796 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001797
1798 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001799}
1800
1801/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1802int
Rusty Russell79dee072000-05-02 16:45:16 +00001803TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1804 const STRUCT_ENTRY *e,
1805 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001806 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001807{
Harald Welteaae69be2004-08-29 23:32:14 +00001808 struct chain_head *c;
1809 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001810
Rusty Russell79dee072000-05-02 16:45:16 +00001811 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001812
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001813 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001814 errno = ENOENT;
1815 return 0;
1816 }
1817
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001818 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001819 errno = E2BIG;
1820 return 0;
1821 }
1822
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001823 /* Take advantage of the double linked list if possible. */
1824 if (rulenum + 1 <= c->num_rules/2) {
1825 old = iptcc_get_rule_num(c, rulenum + 1);
1826 } else {
1827 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1828 }
1829
Harald Welteaae69be2004-08-29 23:32:14 +00001830 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1831 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001832 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001833 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001834
Harald Welteaae69be2004-08-29 23:32:14 +00001835 memcpy(r->entry, e, e->next_offset);
1836 r->counter_map.maptype = COUNTER_MAP_SET;
1837
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001838 if (!iptcc_map_target(handle, r)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001839 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001840 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001841 }
Harald Welte0113fe72004-01-06 19:04:02 +00001842
Harald Welteaae69be2004-08-29 23:32:14 +00001843 list_add(&r->list, &old->list);
1844 iptcc_delete_rule(old);
1845
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001846 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001847
1848 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001849}
1850
Harald Welte0113fe72004-01-06 19:04:02 +00001851/* Append entry `fw' to chain `chain'. Equivalent to insert with
Marc Bouchere6869a82000-03-20 06:03:29 +00001852 rulenum = length of chain. */
1853int
Rusty Russell79dee072000-05-02 16:45:16 +00001854TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1855 const STRUCT_ENTRY *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001856 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001857{
Harald Welteaae69be2004-08-29 23:32:14 +00001858 struct chain_head *c;
1859 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001860
Rusty Russell79dee072000-05-02 16:45:16 +00001861 iptc_fn = TC_APPEND_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001862 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00001863 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001864 errno = ENOENT;
1865 return 0;
1866 }
1867
Harald Welteaae69be2004-08-29 23:32:14 +00001868 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1869 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1870 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001871 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001872 }
Harald Welte0113fe72004-01-06 19:04:02 +00001873
Harald Welteaae69be2004-08-29 23:32:14 +00001874 memcpy(r->entry, e, e->next_offset);
1875 r->counter_map.maptype = COUNTER_MAP_SET;
1876
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001877 if (!iptcc_map_target(handle, r)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001878 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001879 free(r);
1880 return 0;
1881 }
1882
1883 list_add_tail(&r->list, &c->rules);
1884 c->num_rules++;
1885
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001886 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001887
1888 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001889}
1890
1891static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001892match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001893 const unsigned char *a_elems,
1894 const unsigned char *b_elems,
1895 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001896{
Rusty Russell79dee072000-05-02 16:45:16 +00001897 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001898 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001899
1900 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001901 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001902
Rusty Russell228e98d2000-04-27 10:28:06 +00001903 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001904 return 1;
1905
Rusty Russell228e98d2000-04-27 10:28:06 +00001906 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001907 return 1;
1908
Rusty Russell73ef09b2000-07-03 10:24:04 +00001909 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001910
Rusty Russell73ef09b2000-07-03 10:24:04 +00001911 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001912 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001913 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001914 *maskptr += i;
1915 return 0;
1916}
1917
1918static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001919target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001920{
1921 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001922 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001923
Rusty Russell733e54b2004-12-16 14:22:23 +00001924 if (a->type != b->type)
1925 return 0;
1926
1927 ta = GET_TARGET(a->entry);
1928 tb = GET_TARGET(b->entry);
1929
1930 switch (a->type) {
1931 case IPTCC_R_FALLTHROUGH:
1932 return 1;
1933 case IPTCC_R_JUMP:
1934 return a->jump == b->jump;
1935 case IPTCC_R_STANDARD:
1936 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1937 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1938 case IPTCC_R_MODULE:
1939 if (ta->u.target_size != tb->u.target_size)
1940 return 0;
1941 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1942 return 0;
1943
1944 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001945 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001946 return 0;
1947 return 1;
1948 default:
1949 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1950 abort();
1951 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001952}
1953
Rusty Russell733e54b2004-12-16 14:22:23 +00001954static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001955is_same(const STRUCT_ENTRY *a,
1956 const STRUCT_ENTRY *b,
1957 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001958
Harald Welte0113fe72004-01-06 19:04:02 +00001959/* Delete the first rule in `chain' which matches `fw'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001960int
Rusty Russell79dee072000-05-02 16:45:16 +00001961TC_DELETE_ENTRY(const IPT_CHAINLABEL chain,
1962 const STRUCT_ENTRY *origfw,
1963 unsigned char *matchmask,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001964 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001965{
Harald Welteaae69be2004-08-29 23:32:14 +00001966 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001967 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001968
Rusty Russell79dee072000-05-02 16:45:16 +00001969 iptc_fn = TC_DELETE_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001970 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001971 errno = ENOENT;
1972 return 0;
1973 }
1974
Rusty Russelle45c7132004-12-16 13:21:44 +00001975 /* Create a rule_head from origfw. */
1976 r = iptcc_alloc_rule(c, origfw->next_offset);
1977 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001978 errno = ENOMEM;
1979 return 0;
1980 }
1981
Rusty Russelle45c7132004-12-16 13:21:44 +00001982 memcpy(r->entry, origfw, origfw->next_offset);
1983 r->counter_map.maptype = COUNTER_MAP_NOMAP;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001984 if (!iptcc_map_target(handle, r)) {
Rusty Russelle45c7132004-12-16 13:21:44 +00001985 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1986 free(r);
1987 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001988 } else {
1989 /* iptcc_map_target increment target chain references
1990 * since this is a fake rule only used for matching
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001991 * the chain references count is decremented again.
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001992 */
1993 if (r->type == IPTCC_R_JUMP
1994 && r->jump)
1995 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001996 }
Harald Welte0113fe72004-01-06 19:04:02 +00001997
Rusty Russelle45c7132004-12-16 13:21:44 +00001998 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001999 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00002000
Rusty Russell733e54b2004-12-16 14:22:23 +00002001 mask = is_same(r->entry, i->entry, matchmask);
2002 if (!mask)
2003 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002004
Rusty Russell733e54b2004-12-16 14:22:23 +00002005 if (!target_same(r, i, mask))
2006 continue;
2007
2008 /* If we are about to delete the rule that is the
2009 * current iterator, move rule iterator back. next
2010 * pointer will then point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002011 if (i == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002012 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002013 list_entry(handle->rule_iterator_cur->list.prev,
Rusty Russell733e54b2004-12-16 14:22:23 +00002014 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00002015 }
Rusty Russell733e54b2004-12-16 14:22:23 +00002016
2017 c->num_rules--;
2018 iptcc_delete_rule(i);
2019
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002020 set_changed(handle);
Rusty Russell733e54b2004-12-16 14:22:23 +00002021 free(r);
2022 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002023 }
2024
Rusty Russelle45c7132004-12-16 13:21:44 +00002025 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00002026 errno = ENOENT;
2027 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002028}
Harald Welteaae69be2004-08-29 23:32:14 +00002029
Marc Bouchere6869a82000-03-20 06:03:29 +00002030
2031/* Delete the rule in position `rulenum' in `chain'. */
2032int
Rusty Russell79dee072000-05-02 16:45:16 +00002033TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
2034 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002035 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002036{
Harald Welteaae69be2004-08-29 23:32:14 +00002037 struct chain_head *c;
2038 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002039
Rusty Russell79dee072000-05-02 16:45:16 +00002040 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002041
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002042 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002043 errno = ENOENT;
2044 return 0;
2045 }
2046
Martin Josefssona5616dc2004-10-24 22:27:31 +00002047 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00002048 errno = E2BIG;
2049 return 0;
2050 }
2051
2052 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00002053 if (rulenum + 1 <= c->num_rules/2) {
2054 r = iptcc_get_rule_num(c, rulenum + 1);
2055 } else {
2056 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002057 }
2058
Harald Welteaae69be2004-08-29 23:32:14 +00002059 /* If we are about to delete the rule that is the current
2060 * iterator, move rule iterator back. next pointer will then
2061 * point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002062 if (r == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002063 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002064 list_entry(handle->rule_iterator_cur->list.prev,
Harald Welteaae69be2004-08-29 23:32:14 +00002065 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002066 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002067
Harald Welteaae69be2004-08-29 23:32:14 +00002068 c->num_rules--;
2069 iptcc_delete_rule(r);
2070
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002071 set_changed(handle);
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002072
Harald Welteaae69be2004-08-29 23:32:14 +00002073 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002074}
2075
Marc Bouchere6869a82000-03-20 06:03:29 +00002076/* Flushes the entries in the given chain (ie. empties chain). */
2077int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002078TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002079{
Harald Welteaae69be2004-08-29 23:32:14 +00002080 struct chain_head *c;
2081 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002082
Harald Welte0113fe72004-01-06 19:04:02 +00002083 iptc_fn = TC_FLUSH_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002084 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002085 errno = ENOENT;
2086 return 0;
2087 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002088
Harald Welteaae69be2004-08-29 23:32:14 +00002089 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2090 iptcc_delete_rule(r);
2091 }
2092
2093 c->num_rules = 0;
2094
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002095 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002096
2097 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002098}
2099
2100/* Zeroes the counters in a chain. */
2101int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002102TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002103{
Harald Welteaae69be2004-08-29 23:32:14 +00002104 struct chain_head *c;
2105 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002106
Derrik Pates664c0a32005-02-01 13:28:14 +00002107 iptc_fn = TC_ZERO_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002108 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002109 errno = ENOENT;
2110 return 0;
2111 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002112
Andy Gaye5bd1d72006-08-22 02:56:41 +00002113 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2114 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2115
Harald Welteaae69be2004-08-29 23:32:14 +00002116 list_for_each_entry(r, &c->rules, list) {
2117 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2118 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002119 }
Harald Welteaae69be2004-08-29 23:32:14 +00002120
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002121 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002122
Marc Bouchere6869a82000-03-20 06:03:29 +00002123 return 1;
2124}
2125
Harald Welte1cef74d2001-01-05 15:22:59 +00002126STRUCT_COUNTERS *
2127TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2128 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002129 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002130{
Harald Welteaae69be2004-08-29 23:32:14 +00002131 struct chain_head *c;
2132 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002133
2134 iptc_fn = TC_READ_COUNTER;
2135 CHECK(*handle);
2136
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002137 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002138 errno = ENOENT;
2139 return NULL;
2140 }
2141
Harald Welteaae69be2004-08-29 23:32:14 +00002142 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002143 errno = E2BIG;
2144 return NULL;
2145 }
2146
Harald Welteaae69be2004-08-29 23:32:14 +00002147 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002148}
2149
2150int
2151TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2152 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002153 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002154{
Harald Welteaae69be2004-08-29 23:32:14 +00002155 struct chain_head *c;
2156 struct rule_head *r;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002157
Harald Welte1cef74d2001-01-05 15:22:59 +00002158 iptc_fn = TC_ZERO_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002159 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002160
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002161 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002162 errno = ENOENT;
2163 return 0;
2164 }
2165
Harald Welteaae69be2004-08-29 23:32:14 +00002166 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002167 errno = E2BIG;
2168 return 0;
2169 }
2170
Harald Welteaae69be2004-08-29 23:32:14 +00002171 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2172 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002173
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002174 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002175
2176 return 1;
2177}
2178
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002179int
Harald Welte1cef74d2001-01-05 15:22:59 +00002180TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2181 unsigned int rulenum,
2182 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002183 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002184{
Harald Welteaae69be2004-08-29 23:32:14 +00002185 struct chain_head *c;
2186 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002187 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002188
2189 iptc_fn = TC_SET_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002190 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002191
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002192 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002193 errno = ENOENT;
2194 return 0;
2195 }
Harald Welte0113fe72004-01-06 19:04:02 +00002196
Harald Welteaae69be2004-08-29 23:32:14 +00002197 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002198 errno = E2BIG;
2199 return 0;
2200 }
2201
Harald Welteaae69be2004-08-29 23:32:14 +00002202 e = r->entry;
2203 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002204
2205 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002206
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002207 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002208
2209 return 1;
2210}
2211
Marc Bouchere6869a82000-03-20 06:03:29 +00002212/* Creates a new chain. */
2213/* To create a chain, create two rules: error node and unconditional
2214 * return. */
2215int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002216TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002217{
Harald Welteaae69be2004-08-29 23:32:14 +00002218 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002219 int capacity;
2220 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002221
Rusty Russell79dee072000-05-02 16:45:16 +00002222 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002223
2224 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2225 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002226 if (iptcc_find_label(chain, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002227 || strcmp(chain, LABEL_DROP) == 0
2228 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002229 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002230 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002231 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002232 errno = EEXIST;
2233 return 0;
2234 }
2235
Rusty Russell79dee072000-05-02 16:45:16 +00002236 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002237 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002238 errno = EINVAL;
2239 return 0;
2240 }
2241
Harald Welteaae69be2004-08-29 23:32:14 +00002242 c = iptcc_alloc_chain_head(chain, 0);
2243 if (!c) {
2244 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2245 errno = ENOMEM;
2246 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002247
Harald Welteaae69be2004-08-29 23:32:14 +00002248 }
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002249 handle->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002250
Harald Welteaae69be2004-08-29 23:32:14 +00002251 DEBUGP("Creating chain `%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002252 iptc_insert_chain(handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002253
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002254 /* Inserting chains don't change the correctness of the chain
2255 * index (except if its smaller than index[0], but that
2256 * handled by iptc_insert_chain). It only causes longer lists
2257 * in the buckets. Thus, only rebuild chain index when the
2258 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2259 */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002260 capacity = handle->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2261 exceeded = handle->num_chains - capacity;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002262 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2263 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002264 capacity, exceeded, handle->num_chains);
2265 iptcc_chain_index_rebuild(handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002266 }
2267
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002268 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002269
Harald Welteaae69be2004-08-29 23:32:14 +00002270 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002271}
2272
2273/* Get the number of references to this chain. */
2274int
Rusty Russell79dee072000-05-02 16:45:16 +00002275TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002276 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002277{
Harald Welteaae69be2004-08-29 23:32:14 +00002278 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002279
Derrik Pates664c0a32005-02-01 13:28:14 +00002280 iptc_fn = TC_GET_REFERENCES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002281 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002282 errno = ENOENT;
2283 return 0;
2284 }
2285
Harald Welteaae69be2004-08-29 23:32:14 +00002286 *ref = c->references;
2287
Marc Bouchere6869a82000-03-20 06:03:29 +00002288 return 1;
2289}
2290
2291/* Deletes a chain. */
2292int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002293TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002294{
Marc Bouchere6869a82000-03-20 06:03:29 +00002295 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002296 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002297
Rusty Russell79dee072000-05-02 16:45:16 +00002298 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002299
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002300 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002301 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002302 errno = ENOENT;
2303 return 0;
2304 }
2305
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002306 if (TC_BUILTIN(chain, handle)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002307 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2308 errno = EINVAL;
2309 return 0;
2310 }
2311
2312 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2313 DEBUGP("cannot get references on chain `%s'\n", chain);
2314 return 0;
2315 }
2316
2317 if (references > 0) {
2318 DEBUGP("chain `%s' still has references\n", chain);
2319 errno = EMLINK;
2320 return 0;
2321 }
2322
2323 if (c->num_rules) {
2324 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002325 errno = ENOTEMPTY;
2326 return 0;
2327 }
2328
Harald Welteaae69be2004-08-29 23:32:14 +00002329 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002330 * iterator, move chain iterator forward. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002331 if (c == handle->chain_iterator_cur)
2332 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002333
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002334 handle->num_chains--; /* One user defined chain deleted */
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002335
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002336 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002337 iptcc_chain_index_delete_chain(c, handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002338 free(c);
2339
Harald Welteaae69be2004-08-29 23:32:14 +00002340 DEBUGP("chain `%s' deleted\n", chain);
2341
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002342 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002343
2344 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002345}
2346
2347/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002348int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2349 const IPT_CHAINLABEL newname,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002350 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002351{
Harald Welteaae69be2004-08-29 23:32:14 +00002352 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002353 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002354
Harald Welte1de80462000-10-30 12:00:27 +00002355 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2356 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002357 if (iptcc_find_label(newname, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002358 || strcmp(newname, LABEL_DROP) == 0
2359 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002360 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002361 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002362 errno = EEXIST;
2363 return 0;
2364 }
2365
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002366 if (!(c = iptcc_find_label(oldname, handle))
2367 || TC_BUILTIN(oldname, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002368 errno = ENOENT;
2369 return 0;
2370 }
2371
Rusty Russell79dee072000-05-02 16:45:16 +00002372 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002373 errno = EINVAL;
2374 return 0;
2375 }
2376
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002377 /* This only unlinks "c" from the list, thus no free(c) */
2378 iptcc_chain_index_delete_chain(c, handle);
2379
2380 /* Change the name of the chain */
Harald Welteaae69be2004-08-29 23:32:14 +00002381 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002382
2383 /* Insert sorted into to list again */
2384 iptc_insert_chain(handle, c);
2385
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002386 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002387
Marc Bouchere6869a82000-03-20 06:03:29 +00002388 return 1;
2389}
2390
2391/* Sets the policy on a built-in chain. */
2392int
Rusty Russell79dee072000-05-02 16:45:16 +00002393TC_SET_POLICY(const IPT_CHAINLABEL chain,
2394 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002395 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002396 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002397{
Harald Welteaae69be2004-08-29 23:32:14 +00002398 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002399
Rusty Russell79dee072000-05-02 16:45:16 +00002400 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002401
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002402 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002403 DEBUGP("cannot find chain `%s'\n", chain);
2404 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002405 return 0;
2406 }
2407
Harald Welteaae69be2004-08-29 23:32:14 +00002408 if (!iptcc_is_builtin(c)) {
2409 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2410 errno = ENOENT;
2411 return 0;
2412 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002413
Rusty Russell79dee072000-05-02 16:45:16 +00002414 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002415 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002416 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002417 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002418 else {
2419 errno = EINVAL;
2420 return 0;
2421 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002422
Harald Welte1cef74d2001-01-05 15:22:59 +00002423 if (counters) {
2424 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002425 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2426 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002427 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002428 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002429 }
2430
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002431 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002432
Marc Bouchere6869a82000-03-20 06:03:29 +00002433 return 1;
2434}
2435
2436/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002437 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002438 libiptc.c:833: fixed or forbidden register was spilled.
2439 This may be due to a compiler bug or to impossible asm
2440 statements or clauses.
2441*/
2442static void
Rusty Russell79dee072000-05-02 16:45:16 +00002443subtract_counters(STRUCT_COUNTERS *answer,
2444 const STRUCT_COUNTERS *a,
2445 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002446{
2447 answer->pcnt = a->pcnt - b->pcnt;
2448 answer->bcnt = a->bcnt - b->bcnt;
2449}
2450
Harald Welteaae69be2004-08-29 23:32:14 +00002451
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002452static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002453{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002454 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002455 DEBUGP_C("NOMAP => zero\n");
2456}
2457
2458static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002459 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002460 unsigned int mappos)
2461{
2462 /* Original read: X.
2463 * Atomic read on replacement: X + Y.
2464 * Currently in kernel: Z.
2465 * Want in kernel: X + Y + Z.
2466 * => Add in X + Y
2467 * => Add in replacement read.
2468 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002469 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002470 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2471}
2472
2473static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002474 STRUCT_REPLACE *repl, unsigned int idx,
2475 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002476{
2477 /* Original read: X.
2478 * Atomic read on replacement: X + Y.
2479 * Currently in kernel: Z.
2480 * Want in kernel: Y + Z.
2481 * => Add in Y.
2482 * => Add in (replacement read - original read).
2483 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002484 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002485 &repl->counters[mappos],
2486 counters);
2487 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2488}
2489
2490static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002491 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002492{
2493 /* Want to set counter (iptables-restore) */
2494
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002495 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002496 sizeof(STRUCT_COUNTERS));
2497
2498 DEBUGP_C("SET\n");
2499}
2500
2501
Marc Bouchere6869a82000-03-20 06:03:29 +00002502int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002503TC_COMMIT(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002504{
2505 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002506 STRUCT_REPLACE *repl;
2507 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002508 struct chain_head *c;
2509 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002510 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002511 int new_number;
2512 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002513
Derrik Pates664c0a32005-02-01 13:28:14 +00002514 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002515 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002516
Marc Bouchere6869a82000-03-20 06:03:29 +00002517 /* Don't commit if nothing changed. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002518 if (!handle->changed)
Marc Bouchere6869a82000-03-20 06:03:29 +00002519 goto finished;
2520
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002521 new_number = iptcc_compile_table_prep(handle, &new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002522 if (new_number < 0) {
2523 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002524 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002525 }
2526
2527 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002528 if (!repl) {
2529 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002530 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002531 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002532 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002533
Rusty Russelle45c7132004-12-16 13:21:44 +00002534#if 0
2535 TC_DUMP_ENTRIES(*handle);
2536#endif
2537
Harald Welteaae69be2004-08-29 23:32:14 +00002538 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2539 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002540
2541 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002542 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002543 * handle->info.num_entries);
Marc Bouchere6869a82000-03-20 06:03:29 +00002544 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002545 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002546 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002547 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002548 /* These are the counters we're going to put back, later. */
2549 newcounters = malloc(counterlen);
2550 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002551 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002552 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002553 }
Harald Welteaae69be2004-08-29 23:32:14 +00002554 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002555
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002556 strcpy(repl->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002557 repl->num_entries = new_number;
2558 repl->size = new_size;
2559
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002560 repl->num_counters = handle->info.num_entries;
2561 repl->valid_hooks = handle->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002562
2563 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2564 repl->num_entries, repl->size, repl->num_counters);
2565
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002566 ret = iptcc_compile_table(handle, repl);
Harald Welteaae69be2004-08-29 23:32:14 +00002567 if (ret < 0) {
2568 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002569 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002570 }
2571
2572
2573#ifdef IPTC_DEBUG2
2574 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002575 int fd = open("/tmp/libiptc-so_set_replace.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00002576 O_CREAT|O_WRONLY);
2577 if (fd >= 0) {
2578 write(fd, repl, sizeof(*repl) + repl->size);
2579 close(fd);
2580 }
2581 }
2582#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002583
Jan Engelhardt175f4512008-11-10 17:25:55 +01002584 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welted6ba6f52005-11-12 10:39:40 +00002585 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002586 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002587 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002588
2589 /* Put counters back. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002590 strcpy(newcounters->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002591 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002592
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002593 list_for_each_entry(c, &handle->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00002594 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002595
Harald Welteaae69be2004-08-29 23:32:14 +00002596 /* Builtin chains have their own counters */
2597 if (iptcc_is_builtin(c)) {
2598 DEBUGP("counter for chain-index %u: ", c->foot_index);
2599 switch(c->counter_map.maptype) {
2600 case COUNTER_MAP_NOMAP:
2601 counters_nomap(newcounters, c->foot_index);
2602 break;
2603 case COUNTER_MAP_NORMAL_MAP:
2604 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002605 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002606 c->counter_map.mappos);
2607 break;
2608 case COUNTER_MAP_ZEROED:
2609 counters_map_zeroed(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002610 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002611 c->counter_map.mappos,
2612 &c->counters);
2613 break;
2614 case COUNTER_MAP_SET:
2615 counters_map_set(newcounters, c->foot_index,
2616 &c->counters);
2617 break;
2618 }
2619 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002620
Harald Welteaae69be2004-08-29 23:32:14 +00002621 list_for_each_entry(r, &c->rules, list) {
2622 DEBUGP("counter for index %u: ", r->index);
2623 switch (r->counter_map.maptype) {
2624 case COUNTER_MAP_NOMAP:
2625 counters_nomap(newcounters, r->index);
2626 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002627
Harald Welteaae69be2004-08-29 23:32:14 +00002628 case COUNTER_MAP_NORMAL_MAP:
2629 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002630 r->index,
Harald Welteaae69be2004-08-29 23:32:14 +00002631 r->counter_map.mappos);
2632 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002633
Harald Welteaae69be2004-08-29 23:32:14 +00002634 case COUNTER_MAP_ZEROED:
2635 counters_map_zeroed(newcounters, repl,
2636 r->index,
2637 r->counter_map.mappos,
2638 &r->entry->counters);
2639 break;
2640
2641 case COUNTER_MAP_SET:
2642 counters_map_set(newcounters, r->index,
2643 &r->entry->counters);
2644 break;
2645 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002646 }
2647 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002648
Harald Welteaae69be2004-08-29 23:32:14 +00002649#ifdef IPTC_DEBUG2
2650 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002651 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
Harald Welteaae69be2004-08-29 23:32:14 +00002652 O_CREAT|O_WRONLY);
2653 if (fd >= 0) {
2654 write(fd, newcounters, counterlen);
2655 close(fd);
2656 }
2657 }
2658#endif
2659
Jan Engelhardt175f4512008-11-10 17:25:55 +01002660 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
Harald Welted6ba6f52005-11-12 10:39:40 +00002661 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002662 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002663 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002664
2665 free(repl->counters);
2666 free(repl);
2667 free(newcounters);
2668
Harald Welted6ba6f52005-11-12 10:39:40 +00002669finished:
Marc Bouchere6869a82000-03-20 06:03:29 +00002670 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002671
2672out_free_newcounters:
2673 free(newcounters);
2674out_free_repl_counters:
2675 free(repl->counters);
2676out_free_repl:
2677 free(repl);
2678out_zero:
2679 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002680}
2681
Marc Bouchere6869a82000-03-20 06:03:29 +00002682/* Translates errno numbers into more human-readable form than strerror. */
2683const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002684TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002685{
2686 unsigned int i;
2687 struct table_struct {
2688 void *fn;
2689 int err;
2690 const char *message;
2691 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002692 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002693 { TC_INIT, EINVAL, "Module is wrong version" },
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002694 { TC_INIT, ENOENT,
Harald Welte4ccfa632001-07-30 15:12:43 +00002695 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002696 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2697 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2698 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002699 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002700 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2701 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2702 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2703 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002704 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2705 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002706 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2707 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002708 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002709 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002710 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002711 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002712 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002713 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002714 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002715
2716 { NULL, 0, "Incompatible with this kernel" },
2717 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2718 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2719 { NULL, ENOMEM, "Memory allocation problem" },
2720 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002721 };
2722
2723 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2724 if ((!table[i].fn || table[i].fn == iptc_fn)
2725 && table[i].err == err)
2726 return table[i].message;
2727 }
2728
2729 return strerror(err);
2730}