blob: a6e70571ad8d21c42cb00b076378bd130db6d213 [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 */
Maciej Żenczykowskic0aa38e2012-03-21 00:52:00 +000032#include <unistd.h>
33#include <fcntl.h>
Harald Welte15920d12004-05-16 09:05:07 +000034#include <sys/types.h>
35#include <sys/socket.h>
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010036#include <stdbool.h>
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020037#include <xtables.h>
Jan Engelhardtde4d2d32011-08-27 12:50:32 +020038#include <libiptc/xtcshared.h>
Stephane Ouellette7cd00282004-05-14 08:21:06 +000039
Harald Welteaae69be2004-08-29 23:32:14 +000040#include "linux_list.h"
41
42//#define IPTC_DEBUG2 1
43
44#ifdef IPTC_DEBUG2
45#include <fcntl.h>
46#define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
47#define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
48#else
49#define DEBUGP(x, args...)
50#define DEBUGP_C(x, args...)
51#endif
52
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +000053#ifdef DEBUG
54#define debug(x, args...) fprintf(stderr, x, ## args)
55#else
56#define debug(x, args...)
57#endif
58
Marc Bouchere6869a82000-03-20 06:03:29 +000059static void *iptc_fn = NULL;
60
Patrick McHardy0b639362007-09-08 16:00:01 +000061static const char *hooknames[] = {
62 [HOOK_PRE_ROUTING] = "PREROUTING",
63 [HOOK_LOCAL_IN] = "INPUT",
64 [HOOK_FORWARD] = "FORWARD",
65 [HOOK_LOCAL_OUT] = "OUTPUT",
66 [HOOK_POST_ROUTING] = "POSTROUTING",
Marc Bouchere6869a82000-03-20 06:03:29 +000067};
68
Harald Welteaae69be2004-08-29 23:32:14 +000069/* Convenience structures */
Harald Welteaae69be2004-08-29 23:32:14 +000070struct chain_head;
71struct rule_head;
72
Marc Bouchere6869a82000-03-20 06:03:29 +000073struct counter_map
74{
75 enum {
76 COUNTER_MAP_NOMAP,
77 COUNTER_MAP_NORMAL_MAP,
Harald Welte1cef74d2001-01-05 15:22:59 +000078 COUNTER_MAP_ZEROED,
79 COUNTER_MAP_SET
Marc Bouchere6869a82000-03-20 06:03:29 +000080 } maptype;
81 unsigned int mappos;
82};
83
Harald Welteaae69be2004-08-29 23:32:14 +000084enum iptcc_rule_type {
85 IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
86 IPTCC_R_MODULE, /* extension module (SNAT, ...) */
87 IPTCC_R_FALLTHROUGH, /* fallthrough rule */
88 IPTCC_R_JUMP, /* jump to other chain */
Marc Bouchere6869a82000-03-20 06:03:29 +000089};
90
Harald Welteaae69be2004-08-29 23:32:14 +000091struct rule_head
Rusty Russell30fd6e52000-04-23 09:16:06 +000092{
Harald Welteaae69be2004-08-29 23:32:14 +000093 struct list_head list;
94 struct chain_head *chain;
95 struct counter_map counter_map;
96
97 unsigned int index; /* index (needed for counter_map) */
98 unsigned int offset; /* offset in rule blob */
99
100 enum iptcc_rule_type type;
101 struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
102
103 unsigned int size; /* size of entry data */
104 STRUCT_ENTRY entry[0];
105};
106
107struct chain_head
108{
109 struct list_head list;
Rusty Russell79dee072000-05-02 16:45:16 +0000110 char name[TABLE_MAXNAMELEN];
Harald Welteaae69be2004-08-29 23:32:14 +0000111 unsigned int hooknum; /* hook number+1 if builtin */
112 unsigned int references; /* how many jumps reference us */
113 int verdict; /* verdict if builtin */
114
115 STRUCT_COUNTERS counters; /* per-chain counters */
116 struct counter_map counter_map;
117
118 unsigned int num_rules; /* number of rules in list */
119 struct list_head rules; /* list of rules */
120
121 unsigned int index; /* index (needed for jump resolval) */
122 unsigned int head_offset; /* offset in rule blob */
123 unsigned int foot_index; /* index (needed for counter_map) */
124 unsigned int foot_offset; /* offset in rule blob */
Rusty Russell30fd6e52000-04-23 09:16:06 +0000125};
126
Jan Engelhardt1639fe82011-08-27 11:39:52 +0200127struct xtc_handle {
Jan Engelhardt175f4512008-11-10 17:25:55 +0100128 int sockfd;
Harald Welteaae69be2004-08-29 23:32:14 +0000129 int changed; /* Have changes been made? */
130
131 struct list_head chains;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100132
Harald Welteaae69be2004-08-29 23:32:14 +0000133 struct chain_head *chain_iterator_cur;
134 struct rule_head *rule_iterator_cur;
135
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000136 unsigned int num_chains; /* number of user defined chains */
137
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000138 struct chain_head **chain_index; /* array for fast chain list access*/
139 unsigned int chain_index_sz;/* size of chain index array */
140
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200141 int sorted_offsets; /* if chains are received sorted from kernel,
142 * then the offsets are also sorted. Says if its
143 * possible to bsearch offsets using chain_index.
144 */
145
Rusty Russell79dee072000-05-02 16:45:16 +0000146 STRUCT_GETINFO info;
Harald Welteaae69be2004-08-29 23:32:14 +0000147 STRUCT_GET_ENTRIES *entries;
Marc Bouchere6869a82000-03-20 06:03:29 +0000148};
149
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200150enum bsearch_type {
151 BSEARCH_NAME, /* Binary search after chain name */
152 BSEARCH_OFFSET, /* Binary search based on offset */
153};
154
Harald Welteaae69be2004-08-29 23:32:14 +0000155/* allocate a new chain head for the cache */
156static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
157{
158 struct chain_head *c = malloc(sizeof(*c));
159 if (!c)
160 return NULL;
161 memset(c, 0, sizeof(*c));
162
163 strncpy(c->name, name, TABLE_MAXNAMELEN);
164 c->hooknum = hooknum;
165 INIT_LIST_HEAD(&c->rules);
166
167 return c;
168}
169
170/* allocate and initialize a new rule for the cache */
171static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
172{
173 struct rule_head *r = malloc(sizeof(*r)+size);
174 if (!r)
175 return NULL;
176 memset(r, 0, sizeof(*r));
177
178 r->chain = c;
179 r->size = size;
180
181 return r;
182}
183
184/* notify us that the ruleset has been modified by the user */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000185static inline void
Jan Engelhardtfd187312008-11-10 16:59:27 +0100186set_changed(struct xtc_handle *h)
Rusty Russell175f6412000-03-24 09:32:20 +0000187{
Rusty Russell175f6412000-03-24 09:32:20 +0000188 h->changed = 1;
189}
190
Harald Welte380ba5f2002-02-13 16:19:55 +0000191#ifdef IPTC_DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100192static void do_check(struct xtc_handle *h, unsigned int line);
Rusty Russell849779c2000-04-23 15:51:51 +0000193#define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
Rusty Russell30fd6e52000-04-23 09:16:06 +0000194#else
195#define CHECK(h)
196#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000197
Harald Welteaae69be2004-08-29 23:32:14 +0000198
199/**********************************************************************
200 * iptc blob utility functions (iptcb_*)
201 **********************************************************************/
202
Marc Bouchere6869a82000-03-20 06:03:29 +0000203static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000204iptcb_get_number(const STRUCT_ENTRY *i,
Rusty Russell79dee072000-05-02 16:45:16 +0000205 const STRUCT_ENTRY *seek,
Marc Bouchere6869a82000-03-20 06:03:29 +0000206 unsigned int *pos)
207{
208 if (i == seek)
209 return 1;
210 (*pos)++;
211 return 0;
212}
213
Marc Bouchere6869a82000-03-20 06:03:29 +0000214static inline int
Harald Welteaae69be2004-08-29 23:32:14 +0000215iptcb_get_entry_n(STRUCT_ENTRY *i,
Marc Bouchere6869a82000-03-20 06:03:29 +0000216 unsigned int number,
217 unsigned int *pos,
Rusty Russell79dee072000-05-02 16:45:16 +0000218 STRUCT_ENTRY **pe)
Marc Bouchere6869a82000-03-20 06:03:29 +0000219{
220 if (*pos == number) {
221 *pe = i;
222 return 1;
223 }
224 (*pos)++;
225 return 0;
226}
227
Harald Welteaae69be2004-08-29 23:32:14 +0000228static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100229iptcb_get_entry(struct xtc_handle *h, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000230{
231 return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
232}
233
234static unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100235iptcb_entry2index(struct xtc_handle *const h, const STRUCT_ENTRY *seek)
Marc Bouchere6869a82000-03-20 06:03:29 +0000236{
237 unsigned int pos = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000238
Harald Welteaae69be2004-08-29 23:32:14 +0000239 if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
240 iptcb_get_number, seek, &pos) == 0) {
241 fprintf(stderr, "ERROR: offset %u not an entry!\n",
242 (unsigned int)((char *)seek - (char *)h->entries->entrytable));
243 abort();
244 }
245 return pos;
Marc Bouchere6869a82000-03-20 06:03:29 +0000246}
247
Harald Welte0113fe72004-01-06 19:04:02 +0000248static inline STRUCT_ENTRY *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100249iptcb_offset2entry(struct xtc_handle *h, unsigned int offset)
Harald Welte0113fe72004-01-06 19:04:02 +0000250{
Harald Welteaae69be2004-08-29 23:32:14 +0000251 return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
Harald Welte0113fe72004-01-06 19:04:02 +0000252}
253
Harald Welteaae69be2004-08-29 23:32:14 +0000254
Harald Welte0113fe72004-01-06 19:04:02 +0000255static inline unsigned long
Jan Engelhardtfd187312008-11-10 16:59:27 +0100256iptcb_entry2offset(struct xtc_handle *const h, const STRUCT_ENTRY *e)
Harald Welte0113fe72004-01-06 19:04:02 +0000257{
Harald Welteaae69be2004-08-29 23:32:14 +0000258 return (void *)e - (void *)h->entries->entrytable;
Harald Welte3ea8f402003-06-23 18:25:59 +0000259}
260
261static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100262iptcb_offset2index(struct xtc_handle *const h, unsigned int offset)
Harald Welte3ea8f402003-06-23 18:25:59 +0000263{
Harald Welteaae69be2004-08-29 23:32:14 +0000264 return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
265}
266
267/* Returns 0 if not hook entry, else hooknumber + 1 */
268static inline unsigned int
Jan Engelhardtfd187312008-11-10 16:59:27 +0100269iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +0000270{
271 unsigned int i;
272
273 for (i = 0; i < NUMHOOKS; i++) {
274 if ((h->info.valid_hooks & (1 << i))
275 && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
276 return i+1;
277 }
278 return 0;
Harald Welte3ea8f402003-06-23 18:25:59 +0000279}
280
281
Harald Welteaae69be2004-08-29 23:32:14 +0000282/**********************************************************************
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000283 * Chain index (cache utility) functions
284 **********************************************************************
285 * The chain index is an array with pointers into the chain list, with
286 * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
287 * speedup chain list searching, by find a more optimal starting
288 * points when searching the linked list.
289 *
290 * The starting point can be found fast by using a binary search of
291 * the chain index. Thus, reducing the previous search complexity of
292 * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
293 *
294 * A nice property of the chain index, is that the "bucket" list
295 * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
296 * change this). Oppose to hashing, where the "bucket" list length can
297 * vary a lot.
298 */
299#ifndef CHAIN_INDEX_BUCKET_LEN
300#define CHAIN_INDEX_BUCKET_LEN 40
301#endif
302
303/* Another nice property of the chain index is that inserting/creating
304 * chains in chain list don't change the correctness of the chain
305 * index, it only causes longer lists in the buckets.
306 *
307 * To mitigate the performance penalty of longer bucket lists and the
308 * penalty of rebuilding, the chain index is rebuild only when
309 * CHAIN_INDEX_INSERT_MAX chains has been added.
310 */
311#ifndef CHAIN_INDEX_INSERT_MAX
312#define CHAIN_INDEX_INSERT_MAX 355
313#endif
314
315static inline unsigned int iptcc_is_builtin(struct chain_head *c);
316
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000317/* Use binary search in the chain index array, to find a chain_head
318 * pointer closest to the place of the searched name element.
319 *
320 * Notes that, binary search (obviously) requires that the chain list
321 * is sorted by name.
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200322 *
323 * The not so obvious: The chain index array, is actually both sorted
324 * by name and offset, at the same time!. This is only true because,
325 * chain are stored sorted in the kernel (as we pushed it in sorted).
326 *
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000327 */
328static struct list_head *
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200329__iptcc_bsearch_chain_index(const char *name, unsigned int offset,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100330 unsigned int *idx, struct xtc_handle *handle,
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200331 enum bsearch_type type)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000332{
333 unsigned int pos, end;
334 int res;
335
336 struct list_head *list_pos;
337 list_pos=&handle->chains;
338
339 /* Check for empty array, e.g. no user defined chains */
340 if (handle->chain_index_sz == 0) {
341 debug("WARNING: handle->chain_index_sz == 0\n");
342 return list_pos;
343 }
344
345 /* Init */
346 end = handle->chain_index_sz;
347 pos = end / 2;
348
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200349 debug("bsearch Find chain:%s (pos:%d end:%d) (offset:%d)\n",
350 name, pos, end, offset);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000351
352 /* Loop */
353 loop:
354 if (!handle->chain_index[pos]) {
355 fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
356 return &handle->chains; /* Be safe, return orig start pos */
357 }
358
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200359 debug("bsearch Index[%d] name:%s ",
360 pos, handle->chain_index[pos]->name);
361
362 /* Support for different compare functions */
363 switch (type) {
364 case BSEARCH_NAME:
365 res = strcmp(name, handle->chain_index[pos]->name);
366 break;
367 case BSEARCH_OFFSET:
368 debug("head_offset:[%d] foot_offset:[%d] ",
369 handle->chain_index[pos]->head_offset,
370 handle->chain_index[pos]->foot_offset);
371 res = offset - handle->chain_index[pos]->head_offset;
372 break;
373 default:
374 fprintf(stderr, "ERROR: %d not a valid bsearch type\n",
375 type);
376 abort();
377 break;
378 }
379 debug("res:%d ", res);
380
381
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000382 list_pos = &handle->chain_index[pos]->list;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100383 *idx = pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000384
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000385 if (res == 0) { /* Found element, by direct hit */
386 debug("[found] Direct hit pos:%d end:%d\n", pos, end);
387 return list_pos;
388 } else if (res < 0) { /* Too far, jump back */
389 end = pos;
390 pos = pos / 2;
391
392 /* Exit case: First element of array */
393 if (end == 0) {
394 debug("[found] Reached first array elem (end%d)\n",end);
395 return list_pos;
396 }
397 debug("jump back to pos:%d (end:%d)\n", pos, end);
398 goto loop;
Jiri Popelka96d0d012011-06-10 15:25:55 +0200399 } else { /* res > 0; Not far enough, jump forward */
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000400
401 /* Exit case: Last element of array */
402 if (pos == handle->chain_index_sz-1) {
403 debug("[found] Last array elem (end:%d)\n", end);
404 return list_pos;
405 }
406
407 /* Exit case: Next index less, thus elem in this list section */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200408 switch (type) {
409 case BSEARCH_NAME:
410 res = strcmp(name, handle->chain_index[pos+1]->name);
411 break;
412 case BSEARCH_OFFSET:
413 res = offset - handle->chain_index[pos+1]->head_offset;
414 break;
415 }
416
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000417 if (res < 0) {
418 debug("[found] closest list (end:%d)\n", end);
419 return list_pos;
420 }
421
422 pos = (pos+end)/2;
423 debug("jump forward to pos:%d (end:%d)\n", pos, end);
424 goto loop;
425 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000426}
427
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200428/* Wrapper for string chain name based bsearch */
429static struct list_head *
430iptcc_bsearch_chain_index(const char *name, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100431 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200432{
433 return __iptcc_bsearch_chain_index(name, 0, idx, handle, BSEARCH_NAME);
434}
435
436
437/* Wrapper for offset chain based bsearch */
438static struct list_head *
439iptcc_bsearch_chain_offset(unsigned int offset, unsigned int *idx,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100440 struct xtc_handle *handle)
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200441{
442 struct list_head *pos;
443
444 /* If chains were not received sorted from kernel, then the
445 * offset bsearch is not possible.
446 */
447 if (!handle->sorted_offsets)
448 pos = handle->chains.next;
449 else
450 pos = __iptcc_bsearch_chain_index(NULL, offset, idx, handle,
451 BSEARCH_OFFSET);
452 return pos;
453}
454
455
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000456#ifdef DEBUG
457/* Trivial linear search of chain index. Function used for verifying
458 the output of bsearch function */
459static struct list_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100460iptcc_linearly_search_chain_index(const char *name, struct xtc_handle *handle)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000461{
462 unsigned int i=0;
463 int res=0;
464
465 struct list_head *list_pos;
466 list_pos = &handle->chains;
467
468 if (handle->chain_index_sz)
469 list_pos = &handle->chain_index[0]->list;
470
471 /* Linearly walk of chain index array */
472
473 for (i=0; i < handle->chain_index_sz; i++) {
474 if (handle->chain_index[i]) {
475 res = strcmp(handle->chain_index[i]->name, name);
476 if (res > 0)
477 break; // One step too far
478 list_pos = &handle->chain_index[i]->list;
479 if (res == 0)
480 break; // Direct hit
481 }
482 }
483
484 return list_pos;
485}
486#endif
487
Jan Engelhardtfd187312008-11-10 16:59:27 +0100488static int iptcc_chain_index_alloc(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000489{
490 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
491 unsigned int array_elems;
492 unsigned int array_mem;
493
494 /* Allocate memory for the chain index array */
495 array_elems = (h->num_chains / list_length) +
496 (h->num_chains % list_length ? 1 : 0);
497 array_mem = sizeof(h->chain_index) * array_elems;
498
499 debug("Alloc Chain index, elems:%d mem:%d bytes\n",
500 array_elems, array_mem);
501
502 h->chain_index = malloc(array_mem);
Jan Engelhardt0eee3002008-11-26 17:18:08 +0100503 if (h->chain_index == NULL && array_mem > 0) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000504 h->chain_index_sz = 0;
505 return -ENOMEM;
506 }
507 memset(h->chain_index, 0, array_mem);
508 h->chain_index_sz = array_elems;
509
510 return 1;
511}
512
Jan Engelhardtfd187312008-11-10 16:59:27 +0100513static void iptcc_chain_index_free(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000514{
515 h->chain_index_sz = 0;
516 free(h->chain_index);
517}
518
519
520#ifdef DEBUG
Jan Engelhardtfd187312008-11-10 16:59:27 +0100521static void iptcc_chain_index_dump(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000522{
523 unsigned int i = 0;
524
525 /* Dump: contents of chain index array */
526 for (i=0; i < h->chain_index_sz; i++) {
527 if (h->chain_index[i]) {
528 fprintf(stderr, "Chain index[%d].name: %s\n",
529 i, h->chain_index[i]->name);
530 }
531 }
532}
533#endif
534
535/* Build the chain index */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100536static int iptcc_chain_index_build(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000537{
538 unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
539 unsigned int chains = 0;
540 unsigned int cindex = 0;
541 struct chain_head *c;
542
543 /* Build up the chain index array here */
544 debug("Building chain index\n");
545
546 debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
547 h->num_chains, list_length, h->chain_index_sz);
548
549 if (h->chain_index_sz == 0)
550 return 0;
551
552 list_for_each_entry(c, &h->chains, list) {
553
554 /* Issue: The index array needs to start after the
555 * builtin chains, as they are not sorted */
556 if (!iptcc_is_builtin(c)) {
557 cindex=chains / list_length;
558
559 /* Safe guard, break out on array limit, this
560 * is useful if chains are added and array is
561 * rebuild, without realloc of memory. */
562 if (cindex >= h->chain_index_sz)
563 break;
564
565 if ((chains % list_length)== 0) {
566 debug("\nIndex[%d] Chains:", cindex);
567 h->chain_index[cindex] = c;
568 }
569 chains++;
570 }
571 debug("%s, ", c->name);
572 }
573 debug("\n");
574
575 return 1;
576}
577
Jan Engelhardtfd187312008-11-10 16:59:27 +0100578static int iptcc_chain_index_rebuild(struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000579{
580 debug("REBUILD chain index array\n");
581 iptcc_chain_index_free(h);
582 if ((iptcc_chain_index_alloc(h)) < 0)
583 return -ENOMEM;
584 iptcc_chain_index_build(h);
585 return 1;
586}
587
588/* Delete chain (pointer) from index array. Removing an element from
589 * the chain list only affects the chain index array, if the chain
590 * index points-to/uses that list pointer.
591 *
592 * There are different strategies, the simple and safe is to rebuild
593 * the chain index every time. The more advanced is to update the
594 * array index to point to the next element, but that requires some
Ville Skyttä92dc4f62015-09-06 09:22:30 +0300595 * house keeping and boundary checks. The advanced is implemented, as
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000596 * the simple approach behaves badly when all chains are deleted
597 * because list_for_each processing will always hit the first chain
598 * index, thus causing a rebuild for every chain.
599 */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100600static int iptcc_chain_index_delete_chain(struct chain_head *c, struct xtc_handle *h)
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000601{
Jan Engelhardt7d91a2a2011-05-30 01:39:54 +0200602 struct list_head *index_ptr, *next;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000603 struct chain_head *c2;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100604 unsigned int idx, idx2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000605
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100606 index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000607
608 debug("Del chain[%s] c->list:%p index_ptr:%p\n",
609 c->name, &c->list, index_ptr);
610
611 /* Save the next pointer */
612 next = c->list.next;
613 list_del(&c->list);
614
615 if (index_ptr == &c->list) { /* Chain used as index ptr */
616
617 /* See if its possible to avoid a rebuild, by shifting
618 * to next pointer. Its possible if the next pointer
619 * is located in the same index bucket.
620 */
621 c2 = list_entry(next, struct chain_head, list);
Jan Engelhardt7d91a2a2011-05-30 01:39:54 +0200622 iptcc_bsearch_chain_index(c2->name, &idx2, h);
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100623 if (idx != idx2) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000624 /* Rebuild needed */
625 return iptcc_chain_index_rebuild(h);
626 } else {
627 /* Avoiding rebuild */
628 debug("Update cindex[%d] with next ptr name:[%s]\n",
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100629 idx, c2->name);
630 h->chain_index[idx]=c2;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000631 return 0;
632 }
633 }
634 return 0;
635}
636
637
638/**********************************************************************
Harald Welteaae69be2004-08-29 23:32:14 +0000639 * iptc cache utility functions (iptcc_*)
640 **********************************************************************/
Harald Welte0113fe72004-01-06 19:04:02 +0000641
Harald Welteaae69be2004-08-29 23:32:14 +0000642/* Is the given chain builtin (1) or user-defined (0) */
Jesper Dangaard Brouer91093982008-01-15 17:01:58 +0000643static inline unsigned int iptcc_is_builtin(struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +0000644{
645 return (c->hooknum ? 1 : 0);
646}
647
648/* Get a specific rule within a chain */
649static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
650 unsigned int rulenum)
651{
652 struct rule_head *r;
653 unsigned int num = 0;
654
655 list_for_each_entry(r, &c->rules, list) {
656 num++;
657 if (num == rulenum)
658 return r;
659 }
660 return NULL;
661}
662
Martin Josefssona5616dc2004-10-24 22:27:31 +0000663/* Get a specific rule within a chain backwards */
664static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
665 unsigned int rulenum)
666{
667 struct rule_head *r;
668 unsigned int num = 0;
669
670 list_for_each_entry_reverse(r, &c->rules, list) {
671 num++;
672 if (num == rulenum)
673 return r;
674 }
675 return NULL;
676}
677
Harald Welteaae69be2004-08-29 23:32:14 +0000678/* Returns chain head if found, otherwise NULL. */
679static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100680iptcc_find_chain_by_offset(struct xtc_handle *handle, unsigned int offset)
Harald Welteaae69be2004-08-29 23:32:14 +0000681{
682 struct list_head *pos;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200683 struct list_head *list_start_pos;
684 unsigned int i;
Harald Welteaae69be2004-08-29 23:32:14 +0000685
686 if (list_empty(&handle->chains))
687 return NULL;
688
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200689 /* Find a smart place to start the search */
690 list_start_pos = iptcc_bsearch_chain_offset(offset, &i, handle);
691
692 /* Note that iptcc_bsearch_chain_offset() skips builtin
693 * chains, but this function is only used for finding jump
694 * targets, and a buildin chain is not a valid jump target */
695
696 debug("Offset:[%u] starting search at index:[%u]\n", offset, i);
697// list_for_each(pos, &handle->chains) {
698 list_for_each(pos, list_start_pos->prev) {
Harald Welteaae69be2004-08-29 23:32:14 +0000699 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200700 debug(".");
701 if (offset >= c->head_offset && offset <= c->foot_offset) {
702 debug("Offset search found chain:[%s]\n", c->name);
Harald Welteaae69be2004-08-29 23:32:14 +0000703 return c;
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200704 }
Harald Welte0113fe72004-01-06 19:04:02 +0000705 }
706
Harald Welteaae69be2004-08-29 23:32:14 +0000707 return NULL;
Harald Welte0113fe72004-01-06 19:04:02 +0000708}
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000709
Harald Welteaae69be2004-08-29 23:32:14 +0000710/* Returns chain head if found, otherwise NULL. */
711static struct chain_head *
Jan Engelhardtfd187312008-11-10 16:59:27 +0100712iptcc_find_label(const char *name, struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +0000713{
714 struct list_head *pos;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000715 struct list_head *list_start_pos;
716 unsigned int i=0;
717 int res;
Harald Welteaae69be2004-08-29 23:32:14 +0000718
719 if (list_empty(&handle->chains))
720 return NULL;
721
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000722 /* First look at builtin chains */
Harald Welteaae69be2004-08-29 23:32:14 +0000723 list_for_each(pos, &handle->chains) {
724 struct chain_head *c = list_entry(pos, struct chain_head, list);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000725 if (!iptcc_is_builtin(c))
726 break;
Harald Welteaae69be2004-08-29 23:32:14 +0000727 if (!strcmp(c->name, name))
728 return c;
729 }
730
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000731 /* Find a smart place to start the search via chain index */
732 //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
733 list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
734
735 /* Handel if bsearch bails out early */
736 if (list_start_pos == &handle->chains) {
737 list_start_pos = pos;
738 }
739#ifdef DEBUG
740 else {
741 /* Verify result of bsearch against linearly index search */
742 struct list_head *test_pos;
743 struct chain_head *test_c, *tmp_c;
744 test_pos = iptcc_linearly_search_chain_index(name, handle);
745 if (list_start_pos != test_pos) {
746 debug("BUG in chain_index search\n");
747 test_c=list_entry(test_pos, struct chain_head,list);
748 tmp_c =list_entry(list_start_pos,struct chain_head,list);
749 debug("Verify search found:\n");
750 debug(" Chain:%s\n", test_c->name);
751 debug("BSearch found:\n");
752 debug(" Chain:%s\n", tmp_c->name);
753 exit(42);
754 }
755 }
756#endif
757
758 /* Initial/special case, no user defined chains */
759 if (handle->num_chains == 0)
760 return NULL;
761
762 /* Start searching through the chain list */
763 list_for_each(pos, list_start_pos->prev) {
764 struct chain_head *c = list_entry(pos, struct chain_head, list);
765 res = strcmp(c->name, name);
766 debug("List search name:%s == %s res:%d\n", name, c->name, res);
767 if (res==0)
768 return c;
769
770 /* We can stop earlier as we know list is sorted */
771 if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
772 debug(" Not in list, walked too far, sorted list\n");
773 return NULL;
774 }
775
776 /* Stop on wrap around, if list head is reached */
777 if (pos == &handle->chains) {
778 debug("Stop, list head reached\n");
779 return NULL;
780 }
781 }
782
783 debug("List search NOT found name:%s\n", name);
Harald Welteaae69be2004-08-29 23:32:14 +0000784 return NULL;
785}
786
787/* called when rule is to be removed from cache */
788static void iptcc_delete_rule(struct rule_head *r)
789{
790 DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
791 /* clean up reference count of called chain */
792 if (r->type == IPTCC_R_JUMP
793 && r->jump)
794 r->jump->references--;
795
796 list_del(&r->list);
797 free(r);
798}
799
800
801/**********************************************************************
802 * RULESET PARSER (blob -> cache)
803 **********************************************************************/
804
Harald Welteaae69be2004-08-29 23:32:14 +0000805/* Delete policy rule of previous chain, since cache doesn't contain
806 * chain policy rules.
807 * WARNING: This function has ugly design and relies on a lot of context, only
808 * to be called from specific places within the parser */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100809static int __iptcc_p_del_policy(struct xtc_handle *h, unsigned int num)
Harald Welteaae69be2004-08-29 23:32:14 +0000810{
Jan Engelhardt51651b62009-10-23 23:35:49 +0200811 const unsigned char *data;
812
Harald Welteaae69be2004-08-29 23:32:14 +0000813 if (h->chain_iterator_cur) {
814 /* policy rule is last rule */
815 struct rule_head *pr = (struct rule_head *)
816 h->chain_iterator_cur->rules.prev;
817
818 /* save verdict */
Jan Engelhardt51651b62009-10-23 23:35:49 +0200819 data = GET_TARGET(pr->entry)->data;
820 h->chain_iterator_cur->verdict = *(const int *)data;
Harald Welteaae69be2004-08-29 23:32:14 +0000821
822 /* save counter and counter_map information */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100823 h->chain_iterator_cur->counter_map.maptype =
Jan Engelhardt7c4d6682009-10-26 18:43:54 +0100824 COUNTER_MAP_ZEROED;
Harald Welteaae69be2004-08-29 23:32:14 +0000825 h->chain_iterator_cur->counter_map.mappos = num-1;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100826 memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
Harald Welteaae69be2004-08-29 23:32:14 +0000827 sizeof(h->chain_iterator_cur->counters));
828
829 /* foot_offset points to verdict rule */
830 h->chain_iterator_cur->foot_index = num;
831 h->chain_iterator_cur->foot_offset = pr->offset;
832
833 /* delete rule from cache */
834 iptcc_delete_rule(pr);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +0000835 h->chain_iterator_cur->num_rules--;
Harald Welteaae69be2004-08-29 23:32:14 +0000836
837 return 1;
838 }
839 return 0;
840}
841
Harald Welteec30b6c2005-02-01 16:45:56 +0000842/* alphabetically insert a chain into the list */
Christoph Paasch7cd15e32009-03-23 13:50:11 +0100843static void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c)
Harald Welteec30b6c2005-02-01 16:45:56 +0000844{
845 struct chain_head *tmp;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000846 struct list_head *list_start_pos;
847 unsigned int i=1;
848
849 /* Find a smart place to start the insert search */
850 list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
851
852 /* Handle the case, where chain.name is smaller than index[0] */
853 if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
854 h->chain_index[0] = c; /* Update chain index head */
855 list_start_pos = h->chains.next;
856 debug("Update chain_index[0] with %s\n", c->name);
857 }
858
859 /* Handel if bsearch bails out early */
860 if (list_start_pos == &h->chains) {
861 list_start_pos = h->chains.next;
862 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000863
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000864 /* sort only user defined chains */
865 if (!c->hooknum) {
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000866 list_for_each_entry(tmp, list_start_pos->prev, list) {
Robert de Barthfeca0572005-07-31 07:04:59 +0000867 if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
Olaf Rempel9d3ed772005-03-04 23:08:30 +0000868 list_add(&c->list, tmp->list.prev);
869 return;
870 }
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +0000871
872 /* Stop if list head is reached */
873 if (&tmp->list == &h->chains) {
874 debug("Insert, list head reached add to tail\n");
875 break;
876 }
Harald Welteec30b6c2005-02-01 16:45:56 +0000877 }
878 }
879
880 /* survived till end of list: add at tail */
881 list_add_tail(&c->list, &h->chains);
882}
883
Harald Welteaae69be2004-08-29 23:32:14 +0000884/* Another ugly helper function split out of cache_add_entry to make it less
885 * spaghetti code */
Jan Engelhardtfd187312008-11-10 16:59:27 +0100886static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c,
Harald Welteaae69be2004-08-29 23:32:14 +0000887 unsigned int offset, unsigned int *num)
888{
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000889 struct list_head *tail = h->chains.prev;
890 struct chain_head *ctail;
891
Harald Welteaae69be2004-08-29 23:32:14 +0000892 __iptcc_p_del_policy(h, *num);
893
894 c->head_offset = offset;
895 c->index = *num;
896
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000897 /* Chains from kernel are already sorted, as they are inserted
898 * sorted. But there exists an issue when shifting to 1.4.0
899 * from an older version, as old versions allow last created
900 * chain to be unsorted.
901 */
902 if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
903 list_add_tail(&c->list, &h->chains);
904 else {
905 ctail = list_entry(tail, struct chain_head, list);
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200906
Jesper Dangaard Brouer526d3e12008-07-03 20:29:34 +0200907 if (strcmp(c->name, ctail->name) > 0 ||
908 iptcc_is_builtin(ctail))
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000909 list_add_tail(&c->list, &h->chains);/* Already sorted*/
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200910 else {
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000911 iptc_insert_chain(h, c);/* Was not sorted */
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +0200912
913 /* Notice, if chains were not received sorted
914 * from kernel, then an offset bsearch is no
915 * longer valid.
916 */
917 h->sorted_offsets = 0;
918
919 debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n",
920 c->name, ctail->name);
921 }
Jesper Dangaard Brouer13364512007-12-12 15:20:42 +0000922 }
Jesper Dangaard Brouerd8cb7872007-11-28 08:40:26 +0000923
Harald Welteaae69be2004-08-29 23:32:14 +0000924 h->chain_iterator_cur = c;
925}
926
927/* main parser function: add an entry from the blob to the cache */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100928static int cache_add_entry(STRUCT_ENTRY *e,
929 struct xtc_handle *h,
Harald Welteaae69be2004-08-29 23:32:14 +0000930 STRUCT_ENTRY **prev,
931 unsigned int *num)
932{
933 unsigned int builtin;
934 unsigned int offset = (char *)e - (char *)h->entries->entrytable;
935
936 DEBUGP("entering...");
937
938 /* Last entry ("policy rule"). End it.*/
939 if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
940 /* This is the ERROR node at the end of the chain */
941 DEBUGP_C("%u:%u: end of table:\n", *num, offset);
942
943 __iptcc_p_del_policy(h, *num);
944
945 h->chain_iterator_cur = NULL;
946 goto out_inc;
947 }
948
949 /* We know this is the start of a new chain if it's an ERROR
950 * target, or a hook entry point */
951
952 if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100953 struct chain_head *c =
Harald Welteaae69be2004-08-29 23:32:14 +0000954 iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100955 DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
Harald Welteaae69be2004-08-29 23:32:14 +0000956 (char *)c->name, c);
957 if (!c) {
958 errno = -ENOMEM;
959 return -1;
960 }
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +0000961 h->num_chains++; /* New user defined chain */
Harald Welteaae69be2004-08-29 23:32:14 +0000962
963 __iptcc_p_add_chain(h, c, offset, num);
964
965 } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
966 struct chain_head *c =
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100967 iptcc_alloc_chain_head((char *)hooknames[builtin-1],
Harald Welteaae69be2004-08-29 23:32:14 +0000968 builtin);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100969 DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
Harald Welteaae69be2004-08-29 23:32:14 +0000970 *num, offset, c, &c->rules);
971 if (!c) {
972 errno = -ENOMEM;
973 return -1;
974 }
975
976 c->hooknum = builtin;
977
978 __iptcc_p_add_chain(h, c, offset, num);
979
980 /* FIXME: this is ugly. */
981 goto new_rule;
982 } else {
983 /* has to be normal rule */
984 struct rule_head *r;
985new_rule:
986
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +0100987 if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
Harald Welteaae69be2004-08-29 23:32:14 +0000988 e->next_offset))) {
989 errno = ENOMEM;
990 return -1;
991 }
992 DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
993
994 r->index = *num;
995 r->offset = offset;
996 memcpy(r->entry, e, e->next_offset);
997 r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
998 r->counter_map.mappos = r->index;
999
1000 /* handling of jumps, etc. */
1001 if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
1002 STRUCT_STANDARD_TARGET *t;
1003
1004 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
1005 if (t->target.u.target_size
1006 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
1007 errno = EINVAL;
Franz Flasch1a7732f2012-03-08 04:20:37 +00001008 free(r);
Harald Welteaae69be2004-08-29 23:32:14 +00001009 return -1;
1010 }
1011
1012 if (t->verdict < 0) {
1013 DEBUGP_C("standard, verdict=%d\n", t->verdict);
1014 r->type = IPTCC_R_STANDARD;
1015 } else if (t->verdict == r->offset+e->next_offset) {
1016 DEBUGP_C("fallthrough\n");
1017 r->type = IPTCC_R_FALLTHROUGH;
1018 } else {
1019 DEBUGP_C("jump, target=%u\n", t->verdict);
1020 r->type = IPTCC_R_JUMP;
1021 /* Jump target fixup has to be deferred
1022 * until second pass, since we migh not
1023 * yet have parsed the target */
1024 }
Martin Josefsson52c38022004-09-22 19:39:40 +00001025 } else {
1026 DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
1027 r->type = IPTCC_R_MODULE;
Harald Welteaae69be2004-08-29 23:32:14 +00001028 }
1029
1030 list_add_tail(&r->list, &h->chain_iterator_cur->rules);
Martin Josefsson8d1b38a2004-09-22 21:00:19 +00001031 h->chain_iterator_cur->num_rules++;
Harald Welteaae69be2004-08-29 23:32:14 +00001032 }
1033out_inc:
1034 (*num)++;
1035 return 0;
1036}
1037
1038
1039/* parse an iptables blob into it's pieces */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001040static int parse_table(struct xtc_handle *h)
Harald Welteaae69be2004-08-29 23:32:14 +00001041{
1042 STRUCT_ENTRY *prev;
1043 unsigned int num = 0;
1044 struct chain_head *c;
1045
Jesper Dangaard Brouer4bae3f12008-07-03 20:31:42 +02001046 /* Assume that chains offsets are sorted, this verified during
1047 parsing of ruleset (in __iptcc_p_add_chain())*/
1048 h->sorted_offsets = 1;
1049
Harald Welteaae69be2004-08-29 23:32:14 +00001050 /* First pass: over ruleset blob */
1051 ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
1052 cache_add_entry, h, &prev, &num);
1053
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001054 /* Build the chain index, used for chain list search speedup */
1055 if ((iptcc_chain_index_alloc(h)) < 0)
1056 return -ENOMEM;
1057 iptcc_chain_index_build(h);
1058
Harald Welteaae69be2004-08-29 23:32:14 +00001059 /* Second pass: fixup parsed data from first pass */
1060 list_for_each_entry(c, &h->chains, list) {
1061 struct rule_head *r;
1062 list_for_each_entry(r, &c->rules, list) {
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001063 struct chain_head *lc;
Harald Welteaae69be2004-08-29 23:32:14 +00001064 STRUCT_STANDARD_TARGET *t;
1065
1066 if (r->type != IPTCC_R_JUMP)
1067 continue;
1068
1069 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001070 lc = iptcc_find_chain_by_offset(h, t->verdict);
1071 if (!lc)
Harald Welteaae69be2004-08-29 23:32:14 +00001072 return -1;
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001073 r->jump = lc;
1074 lc->references++;
Harald Welteaae69be2004-08-29 23:32:14 +00001075 }
1076 }
1077
Harald Welteaae69be2004-08-29 23:32:14 +00001078 return 1;
1079}
1080
1081
1082/**********************************************************************
1083 * RULESET COMPILATION (cache -> blob)
1084 **********************************************************************/
1085
1086/* Convenience structures */
1087struct iptcb_chain_start{
1088 STRUCT_ENTRY e;
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001089 struct xt_error_target name;
Harald Welteaae69be2004-08-29 23:32:14 +00001090};
1091#define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001092 ALIGN(sizeof(struct xt_error_target)))
Harald Welteaae69be2004-08-29 23:32:14 +00001093
1094struct iptcb_chain_foot {
1095 STRUCT_ENTRY e;
1096 STRUCT_STANDARD_TARGET target;
1097};
1098#define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
1099 ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
1100
1101struct iptcb_chain_error {
1102 STRUCT_ENTRY entry;
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001103 struct xt_error_target target;
Harald Welteaae69be2004-08-29 23:32:14 +00001104};
1105#define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001106 ALIGN(sizeof(struct xt_error_target)))
Harald Welteaae69be2004-08-29 23:32:14 +00001107
1108
1109
1110/* compile rule from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001111static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r)
Harald Welteaae69be2004-08-29 23:32:14 +00001112{
1113 /* handle jumps */
1114 if (r->type == IPTCC_R_JUMP) {
1115 STRUCT_STANDARD_TARGET *t;
1116 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1117 /* memset for memcmp convenience on delete/replace */
1118 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1119 strcpy(t->target.u.user.name, STANDARD_TARGET);
1120 /* Jumps can only happen to builtin chains, so we
1121 * can safely assume that they always have a header */
1122 t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
1123 } else if (r->type == IPTCC_R_FALLTHROUGH) {
1124 STRUCT_STANDARD_TARGET *t;
1125 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
1126 t->verdict = r->offset + r->size;
1127 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001128
Harald Welteaae69be2004-08-29 23:32:14 +00001129 /* copy entry from cache to blob */
1130 memcpy((char *)repl->entries+r->offset, r->entry, r->size);
1131
1132 return 1;
1133}
1134
1135/* compile chain from cache into blob */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001136static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c)
Harald Welteaae69be2004-08-29 23:32:14 +00001137{
1138 int ret;
1139 struct rule_head *r;
1140 struct iptcb_chain_start *head;
1141 struct iptcb_chain_foot *foot;
1142
1143 /* only user-defined chains have heaer */
1144 if (!iptcc_is_builtin(c)) {
1145 /* put chain header in place */
1146 head = (void *)repl->entries + c->head_offset;
1147 head->e.target_offset = sizeof(STRUCT_ENTRY);
1148 head->e.next_offset = IPTCB_CHAIN_START_SIZE;
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001149 strcpy(head->name.target.u.user.name, ERROR_TARGET);
1150 head->name.target.u.target_size =
1151 ALIGN(sizeof(struct xt_error_target));
1152 strcpy(head->name.errorname, c->name);
Harald Welteaae69be2004-08-29 23:32:14 +00001153 } else {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001154 repl->hook_entry[c->hooknum-1] = c->head_offset;
Harald Welteaae69be2004-08-29 23:32:14 +00001155 repl->underflow[c->hooknum-1] = c->foot_offset;
1156 }
1157
1158 /* iterate over rules */
1159 list_for_each_entry(r, &c->rules, list) {
1160 ret = iptcc_compile_rule(h, repl, r);
1161 if (ret < 0)
1162 return ret;
1163 }
1164
1165 /* put chain footer in place */
1166 foot = (void *)repl->entries + c->foot_offset;
1167 foot->e.target_offset = sizeof(STRUCT_ENTRY);
1168 foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
1169 strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
1170 foot->target.target.u.target_size =
1171 ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1172 /* builtin targets have verdict, others return */
1173 if (iptcc_is_builtin(c))
1174 foot->target.verdict = c->verdict;
1175 else
1176 foot->target.verdict = RETURN;
1177 /* set policy-counters */
1178 memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
1179
1180 return 0;
1181}
1182
1183/* calculate offset and number for every rule in the cache */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001184static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c,
Harald Welteefa8fc22005-07-19 22:03:49 +00001185 unsigned int *offset, unsigned int *num)
Harald Welteaae69be2004-08-29 23:32:14 +00001186{
1187 struct rule_head *r;
1188
1189 c->head_offset = *offset;
1190 DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
1191
1192 if (!iptcc_is_builtin(c)) {
1193 /* Chain has header */
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001194 *offset += sizeof(STRUCT_ENTRY)
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001195 + ALIGN(sizeof(struct xt_error_target));
Harald Welteaae69be2004-08-29 23:32:14 +00001196 (*num)++;
1197 }
1198
1199 list_for_each_entry(r, &c->rules, list) {
1200 DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
1201 r->offset = *offset;
1202 r->index = *num;
1203 *offset += r->size;
1204 (*num)++;
1205 }
1206
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001207 DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
Harald Welteaae69be2004-08-29 23:32:14 +00001208 *offset, *num);
1209 c->foot_offset = *offset;
1210 c->foot_index = *num;
1211 *offset += sizeof(STRUCT_ENTRY)
1212 + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
1213 (*num)++;
1214
1215 return 1;
1216}
1217
1218/* put the pieces back together again */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001219static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size)
Harald Welteaae69be2004-08-29 23:32:14 +00001220{
1221 struct chain_head *c;
1222 unsigned int offset = 0, num = 0;
1223 int ret = 0;
1224
1225 /* First pass: calculate offset for every rule */
1226 list_for_each_entry(c, &h->chains, list) {
1227 ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
1228 if (ret < 0)
1229 return ret;
1230 }
1231
1232 /* Append one error rule at end of chain */
1233 num++;
1234 offset += sizeof(STRUCT_ENTRY)
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001235 + ALIGN(sizeof(struct xt_error_target));
Harald Welteaae69be2004-08-29 23:32:14 +00001236
1237 /* ruleset size is now in offset */
1238 *size = offset;
1239 return num;
1240}
1241
Jan Engelhardtfd187312008-11-10 16:59:27 +01001242static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl)
Harald Welteaae69be2004-08-29 23:32:14 +00001243{
1244 struct chain_head *c;
1245 struct iptcb_chain_error *error;
1246
1247 /* Second pass: copy from cache to offsets, fill in jumps */
1248 list_for_each_entry(c, &h->chains, list) {
1249 int ret = iptcc_compile_chain(h, repl, c);
1250 if (ret < 0)
1251 return ret;
1252 }
1253
1254 /* Append error rule at end of chain */
1255 error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
1256 error->entry.target_offset = sizeof(STRUCT_ENTRY);
1257 error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
Jan Engelhardt9cf67de2011-09-11 17:24:26 +02001258 error->target.target.u.user.target_size =
1259 ALIGN(sizeof(struct xt_error_target));
1260 strcpy((char *)&error->target.target.u.user.name, ERROR_TARGET);
1261 strcpy((char *)&error->target.errorname, "ERROR");
Harald Welteaae69be2004-08-29 23:32:14 +00001262
1263 return 1;
1264}
1265
1266/**********************************************************************
1267 * EXTERNAL API (operates on cache only)
1268 **********************************************************************/
Marc Bouchere6869a82000-03-20 06:03:29 +00001269
1270/* Allocate handle of given size */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001271static struct xtc_handle *
Harald Welte0113fe72004-01-06 19:04:02 +00001272alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
Marc Bouchere6869a82000-03-20 06:03:29 +00001273{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001274 struct xtc_handle *h;
Marc Bouchere6869a82000-03-20 06:03:29 +00001275
Jan Engelhardt1639fe82011-08-27 11:39:52 +02001276 h = malloc(sizeof(*h));
Harald Welteaae69be2004-08-29 23:32:14 +00001277 if (!h) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001278 errno = ENOMEM;
1279 return NULL;
1280 }
Harald Welteaae69be2004-08-29 23:32:14 +00001281 memset(h, 0, sizeof(*h));
1282 INIT_LIST_HEAD(&h->chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001283 strcpy(h->info.name, tablename);
Harald Welteaae69be2004-08-29 23:32:14 +00001284
Harald Welte0371c0c2004-09-19 21:00:12 +00001285 h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
Harald Welteaae69be2004-08-29 23:32:14 +00001286 if (!h->entries)
1287 goto out_free_handle;
1288
1289 strcpy(h->entries->name, tablename);
Harald Welte0371c0c2004-09-19 21:00:12 +00001290 h->entries->size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001291
1292 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001293
1294out_free_handle:
1295 free(h);
1296
1297 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001298}
1299
Harald Welteaae69be2004-08-29 23:32:14 +00001300
Jan Engelhardtfd187312008-11-10 16:59:27 +01001301struct xtc_handle *
Rusty Russell79dee072000-05-02 16:45:16 +00001302TC_INIT(const char *tablename)
Marc Bouchere6869a82000-03-20 06:03:29 +00001303{
Jan Engelhardtfd187312008-11-10 16:59:27 +01001304 struct xtc_handle *h;
Rusty Russell79dee072000-05-02 16:45:16 +00001305 STRUCT_GETINFO info;
Harald Welteefa8fc22005-07-19 22:03:49 +00001306 unsigned int tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001307 socklen_t s;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001308 int sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001309
Miguel GAIO8db10442012-04-19 00:14:33 +00001310retry:
Rusty Russell79dee072000-05-02 16:45:16 +00001311 iptc_fn = TC_INIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00001312
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001313 if (strlen(tablename) >= TABLE_MAXNAMELEN) {
1314 errno = EINVAL;
1315 return NULL;
1316 }
Jan Engelhardt175f4512008-11-10 17:25:55 +01001317
1318 sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
1319 if (sockfd < 0)
1320 return NULL;
1321
Maciej Żenczykowskic0aa38e2012-03-21 00:52:00 +00001322 if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) == -1) {
1323 fprintf(stderr, "Could not set close on exec: %s\n",
1324 strerror(errno));
1325 abort();
1326 }
1327
Marc Bouchere6869a82000-03-20 06:03:29 +00001328 s = sizeof(info);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001329
Marc Bouchere6869a82000-03-20 06:03:29 +00001330 strcpy(info.name, tablename);
Derrik Pates664c0a32005-02-01 13:28:14 +00001331 if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001332 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001333 return NULL;
Derrik Pates664c0a32005-02-01 13:28:14 +00001334 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001335
Harald Welteaae69be2004-08-29 23:32:14 +00001336 DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
1337 info.valid_hooks, info.num_entries, info.size);
1338
Harald Welte0113fe72004-01-06 19:04:02 +00001339 if ((h = alloc_handle(info.name, info.size, info.num_entries))
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001340 == NULL) {
Jan Engelhardt175f4512008-11-10 17:25:55 +01001341 close(sockfd);
Marc Bouchere6869a82000-03-20 06:03:29 +00001342 return NULL;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001343 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001344
Marc Bouchere6869a82000-03-20 06:03:29 +00001345 /* Initialize current state */
Jan Engelhardt175f4512008-11-10 17:25:55 +01001346 h->sockfd = sockfd;
Marc Bouchere6869a82000-03-20 06:03:29 +00001347 h->info = info;
Harald Welte0113fe72004-01-06 19:04:02 +00001348
Harald Welteaae69be2004-08-29 23:32:14 +00001349 h->entries->size = h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001350
Rusty Russell79dee072000-05-02 16:45:16 +00001351 tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001352
Jan Engelhardt175f4512008-11-10 17:25:55 +01001353 if (getsockopt(h->sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
Harald Welteaae69be2004-08-29 23:32:14 +00001354 &tmp) < 0)
1355 goto error;
1356
1357#ifdef IPTC_DEBUG2
1358 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001359 int fd = open("/tmp/libiptc-so_get_entries.blob",
Mike Frysinger6028d4a2015-08-20 07:12:59 -04001360 O_CREAT|O_WRONLY, 0644);
Harald Welteaae69be2004-08-29 23:32:14 +00001361 if (fd >= 0) {
1362 write(fd, h->entries, tmp);
1363 close(fd);
1364 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001365 }
Harald Welteaae69be2004-08-29 23:32:14 +00001366#endif
1367
1368 if (parse_table(h) < 0)
1369 goto error;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001370
Marc Bouchere6869a82000-03-20 06:03:29 +00001371 CHECK(h);
1372 return h;
Harald Welteaae69be2004-08-29 23:32:14 +00001373error:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001374 TC_FREE(h);
Patrick McHardy2f932052008-04-02 14:01:53 +02001375 /* A different process changed the ruleset size, retry */
1376 if (errno == EAGAIN)
1377 goto retry;
Harald Welteaae69be2004-08-29 23:32:14 +00001378 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001379}
1380
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001381void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001382TC_FREE(struct xtc_handle *h)
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001383{
Harald Welteaae69be2004-08-29 23:32:14 +00001384 struct chain_head *c, *tmp;
1385
Derrik Pates664c0a32005-02-01 13:28:14 +00001386 iptc_fn = TC_FREE;
Jan Engelhardt175f4512008-11-10 17:25:55 +01001387 close(h->sockfd);
Harald Welteaae69be2004-08-29 23:32:14 +00001388
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001389 list_for_each_entry_safe(c, tmp, &h->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00001390 struct rule_head *r, *rtmp;
1391
1392 list_for_each_entry_safe(r, rtmp, &c->rules, list) {
1393 free(r);
1394 }
1395
1396 free(c);
1397 }
1398
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001399 iptcc_chain_index_free(h);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00001400
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001401 free(h->entries);
1402 free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00001403}
1404
Marc Bouchere6869a82000-03-20 06:03:29 +00001405static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001406print_match(const STRUCT_ENTRY_MATCH *m)
Marc Bouchere6869a82000-03-20 06:03:29 +00001407{
Rusty Russell228e98d2000-04-27 10:28:06 +00001408 printf("Match name: `%s'\n", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001409 return 0;
1410}
1411
Jan Engelhardtfd187312008-11-10 16:59:27 +01001412static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001413
Marc Bouchere6869a82000-03-20 06:03:29 +00001414void
Jan Engelhardtfd187312008-11-10 16:59:27 +01001415TC_DUMP_ENTRIES(struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001416{
Derrik Pates664c0a32005-02-01 13:28:14 +00001417 iptc_fn = TC_DUMP_ENTRIES;
Marc Bouchere6869a82000-03-20 06:03:29 +00001418 CHECK(handle);
Patrick McHardy97fb2f12007-09-08 16:52:25 +00001419
Rusty Russelle45c7132004-12-16 13:21:44 +00001420 printf("libiptc v%s. %u bytes.\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001421 XTABLES_VERSION, handle->entries->size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001422 printf("Table `%s'\n", handle->info.name);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001423 printf("Hooks: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001424 handle->info.hook_entry[HOOK_PRE_ROUTING],
1425 handle->info.hook_entry[HOOK_LOCAL_IN],
1426 handle->info.hook_entry[HOOK_FORWARD],
1427 handle->info.hook_entry[HOOK_LOCAL_OUT],
1428 handle->info.hook_entry[HOOK_POST_ROUTING]);
Jan Engelhardtd73af642008-11-10 17:07:31 +01001429 printf("Underflows: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
Rusty Russell67088e72000-05-10 01:18:57 +00001430 handle->info.underflow[HOOK_PRE_ROUTING],
1431 handle->info.underflow[HOOK_LOCAL_IN],
1432 handle->info.underflow[HOOK_FORWARD],
1433 handle->info.underflow[HOOK_LOCAL_OUT],
1434 handle->info.underflow[HOOK_POST_ROUTING]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001435
Harald Welteaae69be2004-08-29 23:32:14 +00001436 ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
Rusty Russell79dee072000-05-02 16:45:16 +00001437 dump_entry, handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001438}
Rusty Russell30fd6e52000-04-23 09:16:06 +00001439
Marc Bouchere6869a82000-03-20 06:03:29 +00001440/* Does this chain exist? */
Jan Engelhardtfd187312008-11-10 16:59:27 +01001441int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001442{
Derrik Pates664c0a32005-02-01 13:28:14 +00001443 iptc_fn = TC_IS_CHAIN;
Harald Welteaae69be2004-08-29 23:32:14 +00001444 return iptcc_find_label(chain, handle) != NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001445}
1446
Jan Engelhardtfd187312008-11-10 16:59:27 +01001447static void iptcc_chain_iterator_advance(struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001448{
1449 struct chain_head *c = handle->chain_iterator_cur;
1450
1451 if (c->list.next == &handle->chains)
1452 handle->chain_iterator_cur = NULL;
1453 else
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001454 handle->chain_iterator_cur =
Harald Welteaae69be2004-08-29 23:32:14 +00001455 list_entry(c->list.next, struct chain_head, list);
1456}
Marc Bouchere6869a82000-03-20 06:03:29 +00001457
Rusty Russell30fd6e52000-04-23 09:16:06 +00001458/* Iterator functions to run through the chains. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001459const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001460TC_FIRST_CHAIN(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001461{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001462 struct chain_head *c = list_entry(handle->chains.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001463 struct chain_head, list);
1464
1465 iptc_fn = TC_FIRST_CHAIN;
1466
1467
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001468 if (list_empty(&handle->chains)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001469 DEBUGP(": no chains\n");
Harald Welte0113fe72004-01-06 19:04:02 +00001470 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001471 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001472
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001473 handle->chain_iterator_cur = c;
1474 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00001475
Harald Welteaae69be2004-08-29 23:32:14 +00001476 DEBUGP(": returning `%s'\n", c->name);
1477 return c->name;
Marc Bouchere6869a82000-03-20 06:03:29 +00001478}
1479
Rusty Russell30fd6e52000-04-23 09:16:06 +00001480/* Iterator functions to run through the chains. Returns NULL at end. */
1481const char *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001482TC_NEXT_CHAIN(struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001483{
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001484 struct chain_head *c = handle->chain_iterator_cur;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001485
Harald Welteaae69be2004-08-29 23:32:14 +00001486 iptc_fn = TC_NEXT_CHAIN;
1487
1488 if (!c) {
1489 DEBUGP(": no more chains\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001490 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001491 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001492
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001493 iptcc_chain_iterator_advance(handle);
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001494
Harald Welteaae69be2004-08-29 23:32:14 +00001495 DEBUGP(": returning `%s'\n", c->name);
1496 return c->name;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001497}
1498
1499/* Get first rule in the given chain: NULL for empty chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00001500const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001501TC_FIRST_RULE(const char *chain, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001502{
Harald Welteaae69be2004-08-29 23:32:14 +00001503 struct chain_head *c;
1504 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001505
Harald Welteaae69be2004-08-29 23:32:14 +00001506 iptc_fn = TC_FIRST_RULE;
1507
1508 DEBUGP("first rule(%s): ", chain);
1509
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001510 c = iptcc_find_label(chain, handle);
Rusty Russell30fd6e52000-04-23 09:16:06 +00001511 if (!c) {
1512 errno = ENOENT;
1513 return NULL;
1514 }
1515
1516 /* Empty chain: single return/policy rule */
Harald Welteaae69be2004-08-29 23:32:14 +00001517 if (list_empty(&c->rules)) {
1518 DEBUGP_C("no rules, returning NULL\n");
Rusty Russell30fd6e52000-04-23 09:16:06 +00001519 return NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001520 }
Rusty Russell30fd6e52000-04-23 09:16:06 +00001521
Harald Welteaae69be2004-08-29 23:32:14 +00001522 r = list_entry(c->rules.next, struct rule_head, list);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001523 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001524 DEBUGP_C("%p\n", r);
1525
1526 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001527}
1528
1529/* Returns NULL when rules run out. */
Rusty Russell79dee072000-05-02 16:45:16 +00001530const STRUCT_ENTRY *
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001531TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle *handle)
Rusty Russell30fd6e52000-04-23 09:16:06 +00001532{
Harald Welteaae69be2004-08-29 23:32:14 +00001533 struct rule_head *r;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001534
Derrik Pates664c0a32005-02-01 13:28:14 +00001535 iptc_fn = TC_NEXT_RULE;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001536 DEBUGP("rule_iterator_cur=%p...", handle->rule_iterator_cur);
Harald Welteaae69be2004-08-29 23:32:14 +00001537
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001538 if (handle->rule_iterator_cur == NULL) {
Harald Welteaae69be2004-08-29 23:32:14 +00001539 DEBUGP_C("returning NULL\n");
1540 return NULL;
1541 }
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001542
1543 r = list_entry(handle->rule_iterator_cur->list.next,
Harald Welteaae69be2004-08-29 23:32:14 +00001544 struct rule_head, list);
1545
1546 iptc_fn = TC_NEXT_RULE;
1547
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001548 DEBUGP_C("next=%p, head=%p...", &r->list,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001549 &handle->rule_iterator_cur->chain->rules);
Harald Welteaae69be2004-08-29 23:32:14 +00001550
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001551 if (&r->list == &handle->rule_iterator_cur->chain->rules) {
1552 handle->rule_iterator_cur = NULL;
Harald Welteaae69be2004-08-29 23:32:14 +00001553 DEBUGP_C("finished, returning NULL\n");
1554 return NULL;
1555 }
1556
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001557 handle->rule_iterator_cur = r;
Harald Welteaae69be2004-08-29 23:32:14 +00001558
1559 /* NOTE: prev is without any influence ! */
1560 DEBUGP_C("returning rule %p\n", r);
1561 return r->entry;
Rusty Russell30fd6e52000-04-23 09:16:06 +00001562}
1563
Marc Bouchere6869a82000-03-20 06:03:29 +00001564/* Returns a pointer to the target name of this position. */
Jan Engelhardt33690a12008-02-11 00:54:00 +01001565static const char *standard_target_map(int verdict)
Marc Bouchere6869a82000-03-20 06:03:29 +00001566{
Harald Welteaae69be2004-08-29 23:32:14 +00001567 switch (verdict) {
1568 case RETURN:
1569 return LABEL_RETURN;
1570 break;
1571 case -NF_ACCEPT-1:
1572 return LABEL_ACCEPT;
1573 break;
1574 case -NF_DROP-1:
1575 return LABEL_DROP;
1576 break;
1577 case -NF_QUEUE-1:
1578 return LABEL_QUEUE;
1579 break;
1580 default:
1581 fprintf(stderr, "ERROR: %d not a valid target)\n",
1582 verdict);
1583 abort();
1584 break;
1585 }
1586 /* not reached */
1587 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001588}
1589
Harald Welteaae69be2004-08-29 23:32:14 +00001590/* Returns a pointer to the target name of this position. */
1591const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001592 struct xtc_handle *handle)
Harald Welteaae69be2004-08-29 23:32:14 +00001593{
1594 STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
Rusty Russelle45c7132004-12-16 13:21:44 +00001595 struct rule_head *r = container_of(e, struct rule_head, entry[0]);
Jan Engelhardt51651b62009-10-23 23:35:49 +02001596 const unsigned char *data;
Harald Welteaae69be2004-08-29 23:32:14 +00001597
1598 iptc_fn = TC_GET_TARGET;
1599
1600 switch(r->type) {
1601 int spos;
1602 case IPTCC_R_FALLTHROUGH:
1603 return "";
1604 break;
1605 case IPTCC_R_JUMP:
1606 DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
1607 return r->jump->name;
1608 break;
1609 case IPTCC_R_STANDARD:
Jan Engelhardt51651b62009-10-23 23:35:49 +02001610 data = GET_TARGET(e)->data;
1611 spos = *(const int *)data;
Harald Welteaae69be2004-08-29 23:32:14 +00001612 DEBUGP("r=%p, spos=%d'\n", r, spos);
1613 return standard_target_map(spos);
1614 break;
1615 case IPTCC_R_MODULE:
1616 return GET_TARGET(e)->u.user.name;
1617 break;
1618 }
1619 return NULL;
1620}
Marc Bouchere6869a82000-03-20 06:03:29 +00001621/* Is this a built-in chain? Actually returns hook + 1. */
1622int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001623TC_BUILTIN(const char *chain, struct xtc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001624{
Harald Welteaae69be2004-08-29 23:32:14 +00001625 struct chain_head *c;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001626
Harald Welteaae69be2004-08-29 23:32:14 +00001627 iptc_fn = TC_BUILTIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00001628
Harald Welteaae69be2004-08-29 23:32:14 +00001629 c = iptcc_find_label(chain, handle);
1630 if (!c) {
1631 errno = ENOENT;
Martin Josefssonb0f3d2d2004-09-23 18:23:20 +00001632 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001633 }
Harald Welteaae69be2004-08-29 23:32:14 +00001634
1635 return iptcc_is_builtin(c);
Marc Bouchere6869a82000-03-20 06:03:29 +00001636}
1637
1638/* Get the policy of a given built-in chain */
1639const char *
Rusty Russell79dee072000-05-02 16:45:16 +00001640TC_GET_POLICY(const char *chain,
1641 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001642 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001643{
Harald Welteaae69be2004-08-29 23:32:14 +00001644 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00001645
Harald Welteaae69be2004-08-29 23:32:14 +00001646 iptc_fn = TC_GET_POLICY;
1647
1648 DEBUGP("called for chain %s\n", chain);
1649
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001650 c = iptcc_find_label(chain, handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001651 if (!c) {
1652 errno = ENOENT;
1653 return NULL;
1654 }
1655
1656 if (!iptcc_is_builtin(c))
Marc Bouchere6869a82000-03-20 06:03:29 +00001657 return NULL;
1658
Harald Welteaae69be2004-08-29 23:32:14 +00001659 *counters = c->counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00001660
Harald Welteaae69be2004-08-29 23:32:14 +00001661 return standard_target_map(c->verdict);
Harald Welte0113fe72004-01-06 19:04:02 +00001662}
1663
1664static int
Harald Welteaae69be2004-08-29 23:32:14 +00001665iptcc_standard_map(struct rule_head *r, int verdict)
Harald Welte0113fe72004-01-06 19:04:02 +00001666{
Harald Welteaae69be2004-08-29 23:32:14 +00001667 STRUCT_ENTRY *e = r->entry;
Rusty Russell79dee072000-05-02 16:45:16 +00001668 STRUCT_STANDARD_TARGET *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001669
Rusty Russell79dee072000-05-02 16:45:16 +00001670 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001671
Rusty Russell67088e72000-05-10 01:18:57 +00001672 if (t->target.u.target_size
Philip Blundell8c700902000-05-15 02:17:52 +00001673 != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001674 errno = EINVAL;
1675 return 0;
1676 }
1677 /* memset for memcmp convenience on delete/replace */
Rusty Russell79dee072000-05-02 16:45:16 +00001678 memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
1679 strcpy(t->target.u.user.name, STANDARD_TARGET);
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 t->verdict = verdict;
1681
Harald Welteaae69be2004-08-29 23:32:14 +00001682 r->type = IPTCC_R_STANDARD;
1683
Marc Bouchere6869a82000-03-20 06:03:29 +00001684 return 1;
1685}
Rusty Russell7e53bf92000-03-20 07:03:28 +00001686
Marc Bouchere6869a82000-03-20 06:03:29 +00001687static int
Jan Engelhardtfd187312008-11-10 16:59:27 +01001688iptcc_map_target(struct xtc_handle *const handle,
Dan Williams9b8cb752017-02-25 22:02:03 -06001689 struct rule_head *r,
1690 bool dry_run)
Marc Bouchere6869a82000-03-20 06:03:29 +00001691{
Harald Welteaae69be2004-08-29 23:32:14 +00001692 STRUCT_ENTRY *e = r->entry;
Harald Welte0113fe72004-01-06 19:04:02 +00001693 STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
Marc Bouchere6869a82000-03-20 06:03:29 +00001694
Marc Bouchere6869a82000-03-20 06:03:29 +00001695 /* Maybe it's empty (=> fall through) */
Harald Welteaae69be2004-08-29 23:32:14 +00001696 if (strcmp(t->u.user.name, "") == 0) {
1697 r->type = IPTCC_R_FALLTHROUGH;
1698 return 1;
1699 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001700 /* Maybe it's a standard target name... */
Rusty Russell79dee072000-05-02 16:45:16 +00001701 else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001702 return iptcc_standard_map(r, -NF_ACCEPT - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001703 else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001704 return iptcc_standard_map(r, -NF_DROP - 1);
Rusty Russell67088e72000-05-10 01:18:57 +00001705 else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001706 return iptcc_standard_map(r, -NF_QUEUE - 1);
Rusty Russell79dee072000-05-02 16:45:16 +00001707 else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00001708 return iptcc_standard_map(r, RETURN);
Rusty Russell79dee072000-05-02 16:45:16 +00001709 else if (TC_BUILTIN(t->u.user.name, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001710 /* Can't jump to builtins. */
1711 errno = EINVAL;
1712 return 0;
1713 } else {
1714 /* Maybe it's an existing chain name. */
Harald Welteaae69be2004-08-29 23:32:14 +00001715 struct chain_head *c;
1716 DEBUGP("trying to find chain `%s': ", t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001717
Harald Welteaae69be2004-08-29 23:32:14 +00001718 c = iptcc_find_label(t->u.user.name, handle);
1719 if (c) {
1720 DEBUGP_C("found!\n");
1721 r->type = IPTCC_R_JUMP;
1722 r->jump = c;
1723 c->references++;
1724 return 1;
1725 }
1726 DEBUGP_C("not found :(\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001727 }
1728
1729 /* Must be a module? If not, kernel will reject... */
Rusty Russell3aef54d2005-01-03 03:48:40 +00001730 /* memset to all 0 for your memcmp convenience: don't clear version */
Rusty Russell228e98d2000-04-27 10:28:06 +00001731 memset(t->u.user.name + strlen(t->u.user.name),
Marc Bouchere6869a82000-03-20 06:03:29 +00001732 0,
Rusty Russell3aef54d2005-01-03 03:48:40 +00001733 FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
Rusty Russell733e54b2004-12-16 14:22:23 +00001734 r->type = IPTCC_R_MODULE;
Dan Williams9b8cb752017-02-25 22:02:03 -06001735 if (!dry_run)
1736 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001737 return 1;
1738}
1739
Harald Welte0113fe72004-01-06 19:04:02 +00001740/* Insert the entry `fw' in chain `chain' into position `rulenum'. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001741int
Rusty Russell79dee072000-05-02 16:45:16 +00001742TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
1743 const STRUCT_ENTRY *e,
1744 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001745 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001746{
Harald Welteaae69be2004-08-29 23:32:14 +00001747 struct chain_head *c;
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001748 struct rule_head *r;
1749 struct list_head *prev;
Marc Bouchere6869a82000-03-20 06:03:29 +00001750
Rusty Russell79dee072000-05-02 16:45:16 +00001751 iptc_fn = TC_INSERT_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00001752
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001753 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001754 errno = ENOENT;
1755 return 0;
1756 }
1757
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001758 /* first rulenum index = 0
1759 first c->num_rules index = 1 */
1760 if (rulenum > c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001761 errno = E2BIG;
1762 return 0;
1763 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001764
Martin Josefsson631f3612004-09-23 19:25:06 +00001765 /* If we are inserting at the end just take advantage of the
1766 double linked list, insert will happen before the entry
1767 prev points to. */
1768 if (rulenum == c->num_rules) {
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001769 prev = &c->rules;
Martin Josefssona5616dc2004-10-24 22:27:31 +00001770 } else if (rulenum + 1 <= c->num_rules/2) {
Martin Josefsson631f3612004-09-23 19:25:06 +00001771 r = iptcc_get_rule_num(c, rulenum + 1);
Martin Josefssona5616dc2004-10-24 22:27:31 +00001772 prev = &r->list;
1773 } else {
1774 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Martin Josefsson631f3612004-09-23 19:25:06 +00001775 prev = &r->list;
1776 }
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001777
Harald Welteaae69be2004-08-29 23:32:14 +00001778 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1779 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001780 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001781 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001782
Harald Welteaae69be2004-08-29 23:32:14 +00001783 memcpy(r->entry, e, e->next_offset);
1784 r->counter_map.maptype = COUNTER_MAP_SET;
1785
Dan Williams9b8cb752017-02-25 22:02:03 -06001786 if (!iptcc_map_target(handle, r, false)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001787 free(r);
1788 return 0;
1789 }
1790
Martin Josefssoneb066cc2004-09-22 21:04:07 +00001791 list_add_tail(&r->list, prev);
Harald Welteaae69be2004-08-29 23:32:14 +00001792 c->num_rules++;
1793
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001794 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001795
1796 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001797}
1798
1799/* Atomically replace rule `rulenum' in `chain' with `fw'. */
1800int
Rusty Russell79dee072000-05-02 16:45:16 +00001801TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
1802 const STRUCT_ENTRY *e,
1803 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001804 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001805{
Harald Welteaae69be2004-08-29 23:32:14 +00001806 struct chain_head *c;
1807 struct rule_head *r, *old;
Marc Bouchere6869a82000-03-20 06:03:29 +00001808
Rusty Russell79dee072000-05-02 16:45:16 +00001809 iptc_fn = TC_REPLACE_ENTRY;
Marc Bouchere6869a82000-03-20 06:03:29 +00001810
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001811 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001812 errno = ENOENT;
1813 return 0;
1814 }
1815
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001816 if (rulenum >= c->num_rules) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001817 errno = E2BIG;
1818 return 0;
1819 }
1820
Martin Josefsson0f9b8b12004-12-18 17:18:49 +00001821 /* Take advantage of the double linked list if possible. */
1822 if (rulenum + 1 <= c->num_rules/2) {
1823 old = iptcc_get_rule_num(c, rulenum + 1);
1824 } else {
1825 old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
1826 }
1827
Harald Welteaae69be2004-08-29 23:32:14 +00001828 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1829 errno = ENOMEM;
Marc Bouchere6869a82000-03-20 06:03:29 +00001830 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001831 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001832
Harald Welteaae69be2004-08-29 23:32:14 +00001833 memcpy(r->entry, e, e->next_offset);
1834 r->counter_map.maptype = COUNTER_MAP_SET;
1835
Dan Williams9b8cb752017-02-25 22:02:03 -06001836 if (!iptcc_map_target(handle, r, false)) {
Harald Welteaae69be2004-08-29 23:32:14 +00001837 free(r);
Harald Welte0113fe72004-01-06 19:04:02 +00001838 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001839 }
Harald Welte0113fe72004-01-06 19:04:02 +00001840
Harald Welteaae69be2004-08-29 23:32:14 +00001841 list_add(&r->list, &old->list);
1842 iptcc_delete_rule(old);
1843
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001844 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001845
1846 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001847}
1848
Harald Welte0113fe72004-01-06 19:04:02 +00001849/* Append entry `fw' to chain `chain'. Equivalent to insert with
Pablo Neira Ayuso7c1b69b2012-03-01 00:27:50 +01001850 rulenum = length of chain. */
Marc Bouchere6869a82000-03-20 06:03:29 +00001851int
Rusty Russell79dee072000-05-02 16:45:16 +00001852TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
1853 const STRUCT_ENTRY *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001854 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001855{
Harald Welteaae69be2004-08-29 23:32:14 +00001856 struct chain_head *c;
1857 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00001858
Rusty Russell79dee072000-05-02 16:45:16 +00001859 iptc_fn = TC_APPEND_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001860 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00001861 DEBUGP("unable to find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001862 errno = ENOENT;
1863 return 0;
1864 }
1865
Harald Welteaae69be2004-08-29 23:32:14 +00001866 if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
1867 DEBUGP("unable to allocate rule for chain `%s'\n", chain);
1868 errno = ENOMEM;
Harald Welte0113fe72004-01-06 19:04:02 +00001869 return 0;
Harald Welteaae69be2004-08-29 23:32:14 +00001870 }
Harald Welte0113fe72004-01-06 19:04:02 +00001871
Harald Welteaae69be2004-08-29 23:32:14 +00001872 memcpy(r->entry, e, e->next_offset);
1873 r->counter_map.maptype = COUNTER_MAP_SET;
1874
Dan Williams9b8cb752017-02-25 22:02:03 -06001875 if (!iptcc_map_target(handle, r, false)) {
Martin Josefsson12009532004-09-23 18:24:29 +00001876 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
Harald Welteaae69be2004-08-29 23:32:14 +00001877 free(r);
1878 return 0;
1879 }
1880
1881 list_add_tail(&r->list, &c->rules);
1882 c->num_rules++;
1883
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001884 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00001885
Pablo Neira Ayuso7c1b69b2012-03-01 00:27:50 +01001886 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00001887}
1888
1889static inline int
Rusty Russell79dee072000-05-02 16:45:16 +00001890match_different(const STRUCT_ENTRY_MATCH *a,
Rusty Russelledf14cf2000-04-19 11:26:44 +00001891 const unsigned char *a_elems,
1892 const unsigned char *b_elems,
1893 unsigned char **maskptr)
Marc Bouchere6869a82000-03-20 06:03:29 +00001894{
Rusty Russell79dee072000-05-02 16:45:16 +00001895 const STRUCT_ENTRY_MATCH *b;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001896 unsigned int i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001897
1898 /* Offset of b is the same as a. */
Rusty Russell30fd6e52000-04-23 09:16:06 +00001899 b = (void *)b_elems + ((unsigned char *)a - a_elems);
Marc Bouchere6869a82000-03-20 06:03:29 +00001900
Rusty Russell228e98d2000-04-27 10:28:06 +00001901 if (a->u.match_size != b->u.match_size)
Marc Bouchere6869a82000-03-20 06:03:29 +00001902 return 1;
1903
Rusty Russell228e98d2000-04-27 10:28:06 +00001904 if (strcmp(a->u.user.name, b->u.user.name) != 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001905 return 1;
1906
Rusty Russell73ef09b2000-07-03 10:24:04 +00001907 *maskptr += ALIGN(sizeof(*a));
Rusty Russelledf14cf2000-04-19 11:26:44 +00001908
Rusty Russell73ef09b2000-07-03 10:24:04 +00001909 for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001910 if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
Rusty Russell90e712a2000-03-29 04:19:26 +00001911 return 1;
Rusty Russelledf14cf2000-04-19 11:26:44 +00001912 *maskptr += i;
1913 return 0;
1914}
1915
1916static inline int
Rusty Russell733e54b2004-12-16 14:22:23 +00001917target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
Rusty Russelledf14cf2000-04-19 11:26:44 +00001918{
1919 unsigned int i;
Rusty Russell733e54b2004-12-16 14:22:23 +00001920 STRUCT_ENTRY_TARGET *ta, *tb;
Marc Bouchere6869a82000-03-20 06:03:29 +00001921
Rusty Russell733e54b2004-12-16 14:22:23 +00001922 if (a->type != b->type)
1923 return 0;
1924
1925 ta = GET_TARGET(a->entry);
1926 tb = GET_TARGET(b->entry);
1927
1928 switch (a->type) {
1929 case IPTCC_R_FALLTHROUGH:
1930 return 1;
1931 case IPTCC_R_JUMP:
1932 return a->jump == b->jump;
1933 case IPTCC_R_STANDARD:
1934 return ((STRUCT_STANDARD_TARGET *)ta)->verdict
1935 == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
1936 case IPTCC_R_MODULE:
1937 if (ta->u.target_size != tb->u.target_size)
1938 return 0;
1939 if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
1940 return 0;
1941
1942 for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
Rusty Russelldaade442004-12-29 11:14:52 +00001943 if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
Rusty Russell733e54b2004-12-16 14:22:23 +00001944 return 0;
1945 return 1;
1946 default:
1947 fprintf(stderr, "ERROR: bad type %i\n", a->type);
1948 abort();
1949 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001950}
1951
Rusty Russell733e54b2004-12-16 14:22:23 +00001952static unsigned char *
Rusty Russell79dee072000-05-02 16:45:16 +00001953is_same(const STRUCT_ENTRY *a,
1954 const STRUCT_ENTRY *b,
1955 unsigned char *matchmask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001956
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001957
1958/* find the first rule in `chain' which matches `fw' and remove it unless dry_run is set */
1959static int delete_entry(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
1960 unsigned char *matchmask, struct xtc_handle *handle,
1961 bool dry_run)
Marc Bouchere6869a82000-03-20 06:03:29 +00001962{
Harald Welteaae69be2004-08-29 23:32:14 +00001963 struct chain_head *c;
Rusty Russelle45c7132004-12-16 13:21:44 +00001964 struct rule_head *r, *i;
Marc Bouchere6869a82000-03-20 06:03:29 +00001965
Rusty Russell79dee072000-05-02 16:45:16 +00001966 iptc_fn = TC_DELETE_ENTRY;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001967 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001968 errno = ENOENT;
1969 return 0;
1970 }
1971
Rusty Russelle45c7132004-12-16 13:21:44 +00001972 /* Create a rule_head from origfw. */
1973 r = iptcc_alloc_rule(c, origfw->next_offset);
1974 if (!r) {
Harald Welte0113fe72004-01-06 19:04:02 +00001975 errno = ENOMEM;
1976 return 0;
1977 }
1978
Rusty Russelle45c7132004-12-16 13:21:44 +00001979 memcpy(r->entry, origfw, origfw->next_offset);
1980 r->counter_map.maptype = COUNTER_MAP_NOMAP;
Dan Williams9b8cb752017-02-25 22:02:03 -06001981 if (!iptcc_map_target(handle, r, dry_run)) {
Rusty Russelle45c7132004-12-16 13:21:44 +00001982 DEBUGP("unable to map target of rule for chain `%s'\n", chain);
1983 free(r);
1984 return 0;
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001985 } else {
1986 /* iptcc_map_target increment target chain references
1987 * since this is a fake rule only used for matching
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01001988 * the chain references count is decremented again.
Patrick McHardyJesper Brouer04a1e4c2006-07-25 01:50:48 +00001989 */
1990 if (r->type == IPTCC_R_JUMP
1991 && r->jump)
1992 r->jump->references--;
Rusty Russelle45c7132004-12-16 13:21:44 +00001993 }
Harald Welte0113fe72004-01-06 19:04:02 +00001994
Rusty Russelle45c7132004-12-16 13:21:44 +00001995 list_for_each_entry(i, &c->rules, list) {
Rusty Russell733e54b2004-12-16 14:22:23 +00001996 unsigned char *mask;
Harald Weltefe537072004-08-30 20:28:53 +00001997
Rusty Russell733e54b2004-12-16 14:22:23 +00001998 mask = is_same(r->entry, i->entry, matchmask);
1999 if (!mask)
2000 continue;
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002001
Rusty Russell733e54b2004-12-16 14:22:23 +00002002 if (!target_same(r, i, mask))
2003 continue;
2004
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01002005 /* if we are just doing a dry run, we simply skip the rest */
Franz Flasch61b8f7e2012-03-08 04:20:41 +00002006 if (dry_run){
2007 free(r);
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01002008 return 1;
Franz Flasch61b8f7e2012-03-08 04:20:41 +00002009 }
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01002010
Rusty Russell733e54b2004-12-16 14:22:23 +00002011 /* If we are about to delete the rule that is the
2012 * current iterator, move rule iterator back. next
2013 * pointer will then point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002014 if (i == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002015 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002016 list_entry(handle->rule_iterator_cur->list.prev,
Rusty Russell733e54b2004-12-16 14:22:23 +00002017 struct rule_head, list);
Marc Bouchere6869a82000-03-20 06:03:29 +00002018 }
Rusty Russell733e54b2004-12-16 14:22:23 +00002019
2020 c->num_rules--;
2021 iptcc_delete_rule(i);
2022
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002023 set_changed(handle);
Rusty Russell733e54b2004-12-16 14:22:23 +00002024 free(r);
2025 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002026 }
2027
Rusty Russelle45c7132004-12-16 13:21:44 +00002028 free(r);
Marc Bouchere6869a82000-03-20 06:03:29 +00002029 errno = ENOENT;
2030 return 0;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002031}
Harald Welteaae69be2004-08-29 23:32:14 +00002032
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01002033/* check whether a specified rule is present */
2034int TC_CHECK_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
2035 unsigned char *matchmask, struct xtc_handle *handle)
2036{
2037 /* do a dry-run delete to find out whether a matching rule exists */
2038 return delete_entry(chain, origfw, matchmask, handle, true);
2039}
2040
2041/* Delete the first rule in `chain' which matches `fw'. */
2042int TC_DELETE_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
2043 unsigned char *matchmask, struct xtc_handle *handle)
2044{
2045 return delete_entry(chain, origfw, matchmask, handle, false);
2046}
Marc Bouchere6869a82000-03-20 06:03:29 +00002047
2048/* Delete the rule in position `rulenum' in `chain'. */
2049int
Rusty Russell79dee072000-05-02 16:45:16 +00002050TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
2051 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002052 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002053{
Harald Welteaae69be2004-08-29 23:32:14 +00002054 struct chain_head *c;
2055 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002056
Rusty Russell79dee072000-05-02 16:45:16 +00002057 iptc_fn = TC_DELETE_NUM_ENTRY;
Harald Welteaae69be2004-08-29 23:32:14 +00002058
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002059 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002060 errno = ENOENT;
2061 return 0;
2062 }
2063
Martin Josefssona5616dc2004-10-24 22:27:31 +00002064 if (rulenum >= c->num_rules) {
Martin Josefsson631f3612004-09-23 19:25:06 +00002065 errno = E2BIG;
2066 return 0;
2067 }
2068
2069 /* Take advantage of the double linked list if possible. */
Martin Josefssona5616dc2004-10-24 22:27:31 +00002070 if (rulenum + 1 <= c->num_rules/2) {
2071 r = iptcc_get_rule_num(c, rulenum + 1);
2072 } else {
2073 r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
Marc Bouchere6869a82000-03-20 06:03:29 +00002074 }
2075
Harald Welteaae69be2004-08-29 23:32:14 +00002076 /* If we are about to delete the rule that is the current
2077 * iterator, move rule iterator back. next pointer will then
2078 * point to real next node */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002079 if (r == handle->rule_iterator_cur) {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002080 handle->rule_iterator_cur =
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002081 list_entry(handle->rule_iterator_cur->list.prev,
Harald Welteaae69be2004-08-29 23:32:14 +00002082 struct rule_head, list);
Harald Welte0113fe72004-01-06 19:04:02 +00002083 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002084
Harald Welteaae69be2004-08-29 23:32:14 +00002085 c->num_rules--;
2086 iptcc_delete_rule(r);
2087
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002088 set_changed(handle);
Martin Josefsson2a5dbbb2004-09-22 21:37:41 +00002089
Harald Welteaae69be2004-08-29 23:32:14 +00002090 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002091}
2092
Marc Bouchere6869a82000-03-20 06:03:29 +00002093/* Flushes the entries in the given chain (ie. empties chain). */
2094int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002095TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002096{
Harald Welteaae69be2004-08-29 23:32:14 +00002097 struct chain_head *c;
2098 struct rule_head *r, *tmp;
Marc Bouchere6869a82000-03-20 06:03:29 +00002099
Harald Welte0113fe72004-01-06 19:04:02 +00002100 iptc_fn = TC_FLUSH_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002101 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002102 errno = ENOENT;
2103 return 0;
2104 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002105
Harald Welteaae69be2004-08-29 23:32:14 +00002106 list_for_each_entry_safe(r, tmp, &c->rules, list) {
2107 iptcc_delete_rule(r);
2108 }
2109
2110 c->num_rules = 0;
2111
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002112 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002113
2114 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002115}
2116
2117/* Zeroes the counters in a chain. */
2118int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002119TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002120{
Harald Welteaae69be2004-08-29 23:32:14 +00002121 struct chain_head *c;
2122 struct rule_head *r;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002123
Derrik Pates664c0a32005-02-01 13:28:14 +00002124 iptc_fn = TC_ZERO_ENTRIES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002125 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002126 errno = ENOENT;
2127 return 0;
2128 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002129
Andy Gaye5bd1d72006-08-22 02:56:41 +00002130 if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2131 c->counter_map.maptype = COUNTER_MAP_ZEROED;
2132
Harald Welteaae69be2004-08-29 23:32:14 +00002133 list_for_each_entry(r, &c->rules, list) {
2134 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2135 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Marc Bouchere6869a82000-03-20 06:03:29 +00002136 }
Harald Welteaae69be2004-08-29 23:32:14 +00002137
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002138 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002139
Marc Bouchere6869a82000-03-20 06:03:29 +00002140 return 1;
2141}
2142
Harald Welte1cef74d2001-01-05 15:22:59 +00002143STRUCT_COUNTERS *
2144TC_READ_COUNTER(const IPT_CHAINLABEL chain,
2145 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002146 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002147{
Harald Welteaae69be2004-08-29 23:32:14 +00002148 struct chain_head *c;
2149 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002150
2151 iptc_fn = TC_READ_COUNTER;
2152 CHECK(*handle);
2153
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002154 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002155 errno = ENOENT;
2156 return NULL;
2157 }
2158
Harald Welteaae69be2004-08-29 23:32:14 +00002159 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002160 errno = E2BIG;
2161 return NULL;
2162 }
2163
Harald Welteaae69be2004-08-29 23:32:14 +00002164 return &r->entry[0].counters;
Harald Welte1cef74d2001-01-05 15:22:59 +00002165}
2166
2167int
2168TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
2169 unsigned int rulenum,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002170 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002171{
Harald Welteaae69be2004-08-29 23:32:14 +00002172 struct chain_head *c;
2173 struct rule_head *r;
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002174
Harald Welte1cef74d2001-01-05 15:22:59 +00002175 iptc_fn = TC_ZERO_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002176 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002177
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002178 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002179 errno = ENOENT;
2180 return 0;
2181 }
2182
Harald Welteaae69be2004-08-29 23:32:14 +00002183 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002184 errno = E2BIG;
2185 return 0;
2186 }
2187
Harald Welteaae69be2004-08-29 23:32:14 +00002188 if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
2189 r->counter_map.maptype = COUNTER_MAP_ZEROED;
Harald Welte1cef74d2001-01-05 15:22:59 +00002190
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002191 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002192
2193 return 1;
2194}
2195
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002196int
Harald Welte1cef74d2001-01-05 15:22:59 +00002197TC_SET_COUNTER(const IPT_CHAINLABEL chain,
2198 unsigned int rulenum,
2199 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002200 struct xtc_handle *handle)
Harald Welte1cef74d2001-01-05 15:22:59 +00002201{
Harald Welteaae69be2004-08-29 23:32:14 +00002202 struct chain_head *c;
2203 struct rule_head *r;
Harald Welte1cef74d2001-01-05 15:22:59 +00002204 STRUCT_ENTRY *e;
Harald Welte1cef74d2001-01-05 15:22:59 +00002205
2206 iptc_fn = TC_SET_COUNTER;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002207 CHECK(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002208
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002209 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welte1cef74d2001-01-05 15:22:59 +00002210 errno = ENOENT;
2211 return 0;
2212 }
Harald Welte0113fe72004-01-06 19:04:02 +00002213
Harald Welteaae69be2004-08-29 23:32:14 +00002214 if (!(r = iptcc_get_rule_num(c, rulenum))) {
Harald Welte0113fe72004-01-06 19:04:02 +00002215 errno = E2BIG;
2216 return 0;
2217 }
2218
Harald Welteaae69be2004-08-29 23:32:14 +00002219 e = r->entry;
2220 r->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte0113fe72004-01-06 19:04:02 +00002221
2222 memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
Harald Welte1cef74d2001-01-05 15:22:59 +00002223
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002224 set_changed(handle);
Harald Welte1cef74d2001-01-05 15:22:59 +00002225
2226 return 1;
2227}
2228
Marc Bouchere6869a82000-03-20 06:03:29 +00002229/* Creates a new chain. */
2230/* To create a chain, create two rules: error node and unconditional
2231 * return. */
2232int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002233TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002234{
Harald Welteaae69be2004-08-29 23:32:14 +00002235 static struct chain_head *c;
Patrick McHardy1f23d3c2008-06-07 15:04:34 +02002236 int capacity;
2237 int exceeded;
Marc Bouchere6869a82000-03-20 06:03:29 +00002238
Rusty Russell79dee072000-05-02 16:45:16 +00002239 iptc_fn = TC_CREATE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002240
2241 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2242 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002243 if (iptcc_find_label(chain, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002244 || strcmp(chain, LABEL_DROP) == 0
2245 || strcmp(chain, LABEL_ACCEPT) == 0
Rusty Russell67088e72000-05-10 01:18:57 +00002246 || strcmp(chain, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002247 || strcmp(chain, LABEL_RETURN) == 0) {
Harald Welteaae69be2004-08-29 23:32:14 +00002248 DEBUGP("Chain `%s' already exists\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002249 errno = EEXIST;
2250 return 0;
2251 }
2252
Rusty Russell79dee072000-05-02 16:45:16 +00002253 if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002254 DEBUGP("Chain name `%s' too long\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002255 errno = EINVAL;
2256 return 0;
2257 }
2258
Harald Welteaae69be2004-08-29 23:32:14 +00002259 c = iptcc_alloc_chain_head(chain, 0);
2260 if (!c) {
2261 DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
2262 errno = ENOMEM;
2263 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002264
Harald Welteaae69be2004-08-29 23:32:14 +00002265 }
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002266 handle->num_chains++; /* New user defined chain */
Marc Bouchere6869a82000-03-20 06:03:29 +00002267
Harald Welteaae69be2004-08-29 23:32:14 +00002268 DEBUGP("Creating chain `%s'\n", chain);
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002269 iptc_insert_chain(handle, c); /* Insert sorted */
Harald Welte0113fe72004-01-06 19:04:02 +00002270
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002271 /* Inserting chains don't change the correctness of the chain
2272 * index (except if its smaller than index[0], but that
2273 * handled by iptc_insert_chain). It only causes longer lists
2274 * in the buckets. Thus, only rebuild chain index when the
2275 * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
2276 */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002277 capacity = handle->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
2278 exceeded = handle->num_chains - capacity;
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002279 if (exceeded > CHAIN_INDEX_INSERT_MAX) {
2280 debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002281 capacity, exceeded, handle->num_chains);
2282 iptcc_chain_index_rebuild(handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002283 }
2284
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002285 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002286
Harald Welteaae69be2004-08-29 23:32:14 +00002287 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002288}
2289
2290/* Get the number of references to this chain. */
2291int
Rusty Russell79dee072000-05-02 16:45:16 +00002292TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002293 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002294{
Harald Welteaae69be2004-08-29 23:32:14 +00002295 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002296
Derrik Pates664c0a32005-02-01 13:28:14 +00002297 iptc_fn = TC_GET_REFERENCES;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002298 if (!(c = iptcc_find_label(chain, handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002299 errno = ENOENT;
2300 return 0;
2301 }
2302
Harald Welteaae69be2004-08-29 23:32:14 +00002303 *ref = c->references;
2304
Marc Bouchere6869a82000-03-20 06:03:29 +00002305 return 1;
2306}
2307
2308/* Deletes a chain. */
2309int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002310TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002311{
Marc Bouchere6869a82000-03-20 06:03:29 +00002312 unsigned int references;
Harald Welteaae69be2004-08-29 23:32:14 +00002313 struct chain_head *c;
Rusty Russell7e53bf92000-03-20 07:03:28 +00002314
Rusty Russell79dee072000-05-02 16:45:16 +00002315 iptc_fn = TC_DELETE_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002316
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002317 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002318 DEBUGP("cannot find chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002319 errno = ENOENT;
2320 return 0;
2321 }
2322
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002323 if (TC_BUILTIN(chain, handle)) {
Harald Welteaae69be2004-08-29 23:32:14 +00002324 DEBUGP("cannot remove builtin chain `%s'\n", chain);
2325 errno = EINVAL;
2326 return 0;
2327 }
2328
2329 if (!TC_GET_REFERENCES(&references, chain, handle)) {
2330 DEBUGP("cannot get references on chain `%s'\n", chain);
2331 return 0;
2332 }
2333
2334 if (references > 0) {
2335 DEBUGP("chain `%s' still has references\n", chain);
2336 errno = EMLINK;
2337 return 0;
2338 }
2339
2340 if (c->num_rules) {
2341 DEBUGP("chain `%s' is not empty\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002342 errno = ENOTEMPTY;
2343 return 0;
2344 }
2345
Harald Welteaae69be2004-08-29 23:32:14 +00002346 /* If we are about to delete the chain that is the current
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002347 * iterator, move chain iterator forward. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002348 if (c == handle->chain_iterator_cur)
2349 iptcc_chain_iterator_advance(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002350
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002351 handle->num_chains--; /* One user defined chain deleted */
Jesper Dangaard Brouer48bde402008-01-15 17:06:48 +00002352
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002353 //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002354 iptcc_chain_index_delete_chain(c, handle);
Jesper Dangaard Brouer01444da2008-01-15 17:18:15 +00002355 free(c);
2356
Harald Welteaae69be2004-08-29 23:32:14 +00002357 DEBUGP("chain `%s' deleted\n", chain);
2358
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002359 set_changed(handle);
Harald Welteaae69be2004-08-29 23:32:14 +00002360
2361 return 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002362}
2363
2364/* Renames a chain. */
Rusty Russell79dee072000-05-02 16:45:16 +00002365int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
2366 const IPT_CHAINLABEL newname,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002367 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002368{
Harald Welteaae69be2004-08-29 23:32:14 +00002369 struct chain_head *c;
Rusty Russell79dee072000-05-02 16:45:16 +00002370 iptc_fn = TC_RENAME_CHAIN;
Marc Bouchere6869a82000-03-20 06:03:29 +00002371
Harald Welte1de80462000-10-30 12:00:27 +00002372 /* find_label doesn't cover built-in targets: DROP, ACCEPT,
2373 QUEUE, RETURN. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002374 if (iptcc_find_label(newname, handle)
Rusty Russell79dee072000-05-02 16:45:16 +00002375 || strcmp(newname, LABEL_DROP) == 0
2376 || strcmp(newname, LABEL_ACCEPT) == 0
Harald Welte1de80462000-10-30 12:00:27 +00002377 || strcmp(newname, LABEL_QUEUE) == 0
Rusty Russell79dee072000-05-02 16:45:16 +00002378 || strcmp(newname, LABEL_RETURN) == 0) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002379 errno = EEXIST;
2380 return 0;
2381 }
2382
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002383 if (!(c = iptcc_find_label(oldname, handle))
2384 || TC_BUILTIN(oldname, handle)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002385 errno = ENOENT;
2386 return 0;
2387 }
2388
Rusty Russell79dee072000-05-02 16:45:16 +00002389 if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002390 errno = EINVAL;
2391 return 0;
2392 }
2393
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002394 /* This only unlinks "c" from the list, thus no free(c) */
2395 iptcc_chain_index_delete_chain(c, handle);
2396
2397 /* Change the name of the chain */
Harald Welteaae69be2004-08-29 23:32:14 +00002398 strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
Jesper Dangaard Brouer64ff47c2009-03-23 14:25:49 +01002399
2400 /* Insert sorted into to list again */
2401 iptc_insert_chain(handle, c);
2402
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002403 set_changed(handle);
Harald Welte0113fe72004-01-06 19:04:02 +00002404
Marc Bouchere6869a82000-03-20 06:03:29 +00002405 return 1;
2406}
2407
2408/* Sets the policy on a built-in chain. */
2409int
Rusty Russell79dee072000-05-02 16:45:16 +00002410TC_SET_POLICY(const IPT_CHAINLABEL chain,
2411 const IPT_CHAINLABEL policy,
Harald Welte1cef74d2001-01-05 15:22:59 +00002412 STRUCT_COUNTERS *counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002413 struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002414{
Harald Welteaae69be2004-08-29 23:32:14 +00002415 struct chain_head *c;
Marc Bouchere6869a82000-03-20 06:03:29 +00002416
Rusty Russell79dee072000-05-02 16:45:16 +00002417 iptc_fn = TC_SET_POLICY;
Marc Bouchere6869a82000-03-20 06:03:29 +00002418
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002419 if (!(c = iptcc_find_label(chain, handle))) {
Harald Welteaae69be2004-08-29 23:32:14 +00002420 DEBUGP("cannot find chain `%s'\n", chain);
2421 errno = ENOENT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002422 return 0;
2423 }
2424
Harald Welteaae69be2004-08-29 23:32:14 +00002425 if (!iptcc_is_builtin(c)) {
2426 DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
2427 errno = ENOENT;
2428 return 0;
2429 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002430
Rusty Russell79dee072000-05-02 16:45:16 +00002431 if (strcmp(policy, LABEL_ACCEPT) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002432 c->verdict = -NF_ACCEPT - 1;
Rusty Russell79dee072000-05-02 16:45:16 +00002433 else if (strcmp(policy, LABEL_DROP) == 0)
Harald Welteaae69be2004-08-29 23:32:14 +00002434 c->verdict = -NF_DROP - 1;
Marc Bouchere6869a82000-03-20 06:03:29 +00002435 else {
2436 errno = EINVAL;
2437 return 0;
2438 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002439
Harald Welte1cef74d2001-01-05 15:22:59 +00002440 if (counters) {
2441 /* set byte and packet counters */
Harald Welteaae69be2004-08-29 23:32:14 +00002442 memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
2443 c->counter_map.maptype = COUNTER_MAP_SET;
Harald Welte1cef74d2001-01-05 15:22:59 +00002444 } else {
Harald Welteaae69be2004-08-29 23:32:14 +00002445 c->counter_map.maptype = COUNTER_MAP_NOMAP;
Harald Welte1cef74d2001-01-05 15:22:59 +00002446 }
2447
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002448 set_changed(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002449
Marc Bouchere6869a82000-03-20 06:03:29 +00002450 return 1;
2451}
2452
2453/* Without this, on gcc 2.7.2.3, we get:
Rusty Russell79dee072000-05-02 16:45:16 +00002454 libiptc.c: In function `TC_COMMIT':
Marc Bouchere6869a82000-03-20 06:03:29 +00002455 libiptc.c:833: fixed or forbidden register was spilled.
2456 This may be due to a compiler bug or to impossible asm
2457 statements or clauses.
2458*/
2459static void
Rusty Russell79dee072000-05-02 16:45:16 +00002460subtract_counters(STRUCT_COUNTERS *answer,
2461 const STRUCT_COUNTERS *a,
2462 const STRUCT_COUNTERS *b)
Marc Bouchere6869a82000-03-20 06:03:29 +00002463{
2464 answer->pcnt = a->pcnt - b->pcnt;
2465 answer->bcnt = a->bcnt - b->bcnt;
2466}
2467
Harald Welteaae69be2004-08-29 23:32:14 +00002468
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002469static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
Harald Welteaae69be2004-08-29 23:32:14 +00002470{
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002471 newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
Harald Welteaae69be2004-08-29 23:32:14 +00002472 DEBUGP_C("NOMAP => zero\n");
2473}
2474
2475static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002476 STRUCT_REPLACE *repl, unsigned int idx,
Harald Welteaae69be2004-08-29 23:32:14 +00002477 unsigned int mappos)
2478{
2479 /* Original read: X.
2480 * Atomic read on replacement: X + Y.
2481 * Currently in kernel: Z.
2482 * Want in kernel: X + Y + Z.
2483 * => Add in X + Y
2484 * => Add in replacement read.
2485 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002486 newcounters->counters[idx] = repl->counters[mappos];
Harald Welteaae69be2004-08-29 23:32:14 +00002487 DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
2488}
2489
2490static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002491 STRUCT_REPLACE *repl, unsigned int idx,
2492 unsigned int mappos, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002493{
2494 /* Original read: X.
2495 * Atomic read on replacement: X + Y.
2496 * Currently in kernel: Z.
2497 * Want in kernel: Y + Z.
2498 * => Add in Y.
2499 * => Add in (replacement read - original read).
2500 */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002501 subtract_counters(&newcounters->counters[idx],
Harald Welteaae69be2004-08-29 23:32:14 +00002502 &repl->counters[mappos],
2503 counters);
2504 DEBUGP_C("ZEROED => mappos %u\n", mappos);
2505}
2506
2507static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002508 unsigned int idx, STRUCT_COUNTERS *counters)
Harald Welteaae69be2004-08-29 23:32:14 +00002509{
2510 /* Want to set counter (iptables-restore) */
2511
Jan Engelhardtdbb77542008-02-11 00:33:30 +01002512 memcpy(&newcounters->counters[idx], counters,
Harald Welteaae69be2004-08-29 23:32:14 +00002513 sizeof(STRUCT_COUNTERS));
2514
2515 DEBUGP_C("SET\n");
2516}
2517
2518
Marc Bouchere6869a82000-03-20 06:03:29 +00002519int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002520TC_COMMIT(struct xtc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00002521{
2522 /* Replace, then map back the counters. */
Rusty Russell79dee072000-05-02 16:45:16 +00002523 STRUCT_REPLACE *repl;
2524 STRUCT_COUNTERS_INFO *newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002525 struct chain_head *c;
2526 int ret;
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002527 size_t counterlen;
Harald Welteaae69be2004-08-29 23:32:14 +00002528 int new_number;
2529 unsigned int new_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002530
Derrik Pates664c0a32005-02-01 13:28:14 +00002531 iptc_fn = TC_COMMIT;
Marc Bouchere6869a82000-03-20 06:03:29 +00002532 CHECK(*handle);
Martin Josefsson841e4ae2003-05-02 15:30:11 +00002533
Marc Bouchere6869a82000-03-20 06:03:29 +00002534 /* Don't commit if nothing changed. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002535 if (!handle->changed)
Marc Bouchere6869a82000-03-20 06:03:29 +00002536 goto finished;
2537
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002538 new_number = iptcc_compile_table_prep(handle, &new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002539 if (new_number < 0) {
2540 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002541 goto out_zero;
Harald Welteaae69be2004-08-29 23:32:14 +00002542 }
2543
2544 repl = malloc(sizeof(*repl) + new_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00002545 if (!repl) {
2546 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002547 goto out_zero;
Marc Bouchere6869a82000-03-20 06:03:29 +00002548 }
Martin Josefssonad3b4f92004-09-22 22:04:07 +00002549 memset(repl, 0, sizeof(*repl) + new_size);
Harald Welteaae69be2004-08-29 23:32:14 +00002550
Rusty Russelle45c7132004-12-16 13:21:44 +00002551#if 0
2552 TC_DUMP_ENTRIES(*handle);
2553#endif
2554
Harald Welteaae69be2004-08-29 23:32:14 +00002555 counterlen = sizeof(STRUCT_COUNTERS_INFO)
2556 + sizeof(STRUCT_COUNTERS) * new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002557
2558 /* These are the old counters we will get from kernel */
Rusty Russell79dee072000-05-02 16:45:16 +00002559 repl->counters = malloc(sizeof(STRUCT_COUNTERS)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002560 * handle->info.num_entries);
Marc Bouchere6869a82000-03-20 06:03:29 +00002561 if (!repl->counters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002562 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002563 goto out_free_repl;
Marc Bouchere6869a82000-03-20 06:03:29 +00002564 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002565 /* These are the counters we're going to put back, later. */
2566 newcounters = malloc(counterlen);
2567 if (!newcounters) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002568 errno = ENOMEM;
Harald Welted6ba6f52005-11-12 10:39:40 +00002569 goto out_free_repl_counters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002570 }
Harald Welteaae69be2004-08-29 23:32:14 +00002571 memset(newcounters, 0, counterlen);
Marc Bouchere6869a82000-03-20 06:03:29 +00002572
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002573 strcpy(repl->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002574 repl->num_entries = new_number;
2575 repl->size = new_size;
2576
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002577 repl->num_counters = handle->info.num_entries;
2578 repl->valid_hooks = handle->info.valid_hooks;
Harald Welteaae69be2004-08-29 23:32:14 +00002579
2580 DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
2581 repl->num_entries, repl->size, repl->num_counters);
2582
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002583 ret = iptcc_compile_table(handle, repl);
Harald Welteaae69be2004-08-29 23:32:14 +00002584 if (ret < 0) {
2585 errno = ret;
Harald Welted6ba6f52005-11-12 10:39:40 +00002586 goto out_free_newcounters;
Harald Welteaae69be2004-08-29 23:32:14 +00002587 }
2588
2589
2590#ifdef IPTC_DEBUG2
2591 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002592 int fd = open("/tmp/libiptc-so_set_replace.blob",
Mike Frysinger6028d4a2015-08-20 07:12:59 -04002593 O_CREAT|O_WRONLY, 0644);
Harald Welteaae69be2004-08-29 23:32:14 +00002594 if (fd >= 0) {
2595 write(fd, repl, sizeof(*repl) + repl->size);
2596 close(fd);
2597 }
2598 }
2599#endif
Marc Bouchere6869a82000-03-20 06:03:29 +00002600
Jan Engelhardt175f4512008-11-10 17:25:55 +01002601 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
Harald Welted6ba6f52005-11-12 10:39:40 +00002602 sizeof(*repl) + repl->size);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002603 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002604 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002605
2606 /* Put counters back. */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002607 strcpy(newcounters->name, handle->info.name);
Harald Welteaae69be2004-08-29 23:32:14 +00002608 newcounters->num_counters = new_number;
Marc Bouchere6869a82000-03-20 06:03:29 +00002609
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002610 list_for_each_entry(c, &handle->chains, list) {
Harald Welteaae69be2004-08-29 23:32:14 +00002611 struct rule_head *r;
Marc Bouchere6869a82000-03-20 06:03:29 +00002612
Harald Welteaae69be2004-08-29 23:32:14 +00002613 /* Builtin chains have their own counters */
2614 if (iptcc_is_builtin(c)) {
2615 DEBUGP("counter for chain-index %u: ", c->foot_index);
2616 switch(c->counter_map.maptype) {
2617 case COUNTER_MAP_NOMAP:
2618 counters_nomap(newcounters, c->foot_index);
2619 break;
2620 case COUNTER_MAP_NORMAL_MAP:
2621 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002622 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002623 c->counter_map.mappos);
2624 break;
2625 case COUNTER_MAP_ZEROED:
2626 counters_map_zeroed(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002627 c->foot_index,
Harald Welteaae69be2004-08-29 23:32:14 +00002628 c->counter_map.mappos,
2629 &c->counters);
2630 break;
2631 case COUNTER_MAP_SET:
2632 counters_map_set(newcounters, c->foot_index,
2633 &c->counters);
2634 break;
2635 }
2636 }
Harald Welte1cef74d2001-01-05 15:22:59 +00002637
Harald Welteaae69be2004-08-29 23:32:14 +00002638 list_for_each_entry(r, &c->rules, list) {
2639 DEBUGP("counter for index %u: ", r->index);
2640 switch (r->counter_map.maptype) {
2641 case COUNTER_MAP_NOMAP:
2642 counters_nomap(newcounters, r->index);
2643 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002644
Harald Welteaae69be2004-08-29 23:32:14 +00002645 case COUNTER_MAP_NORMAL_MAP:
2646 counters_normal_map(newcounters, repl,
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002647 r->index,
Harald Welteaae69be2004-08-29 23:32:14 +00002648 r->counter_map.mappos);
2649 break;
Harald Welte1cef74d2001-01-05 15:22:59 +00002650
Harald Welteaae69be2004-08-29 23:32:14 +00002651 case COUNTER_MAP_ZEROED:
2652 counters_map_zeroed(newcounters, repl,
2653 r->index,
2654 r->counter_map.mappos,
2655 &r->entry->counters);
2656 break;
2657
2658 case COUNTER_MAP_SET:
2659 counters_map_set(newcounters, r->index,
2660 &r->entry->counters);
2661 break;
2662 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002663 }
2664 }
Rusty Russell62527ce2000-09-04 09:45:54 +00002665
Harald Welteaae69be2004-08-29 23:32:14 +00002666#ifdef IPTC_DEBUG2
2667 {
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002668 int fd = open("/tmp/libiptc-so_set_add_counters.blob",
Mike Frysinger6028d4a2015-08-20 07:12:59 -04002669 O_CREAT|O_WRONLY, 0644);
Harald Welteaae69be2004-08-29 23:32:14 +00002670 if (fd >= 0) {
2671 write(fd, newcounters, counterlen);
2672 close(fd);
2673 }
2674 }
2675#endif
2676
Jan Engelhardt175f4512008-11-10 17:25:55 +01002677 ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
Harald Welted6ba6f52005-11-12 10:39:40 +00002678 newcounters, counterlen);
Patrick McHardye0865ad2006-04-22 02:08:56 +00002679 if (ret < 0)
Harald Welted6ba6f52005-11-12 10:39:40 +00002680 goto out_free_newcounters;
Marc Bouchere6869a82000-03-20 06:03:29 +00002681
2682 free(repl->counters);
2683 free(repl);
2684 free(newcounters);
2685
Harald Welted6ba6f52005-11-12 10:39:40 +00002686finished:
Marc Bouchere6869a82000-03-20 06:03:29 +00002687 return 1;
Harald Welted6ba6f52005-11-12 10:39:40 +00002688
2689out_free_newcounters:
2690 free(newcounters);
2691out_free_repl_counters:
2692 free(repl->counters);
2693out_free_repl:
2694 free(repl);
2695out_zero:
2696 return 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00002697}
2698
Marc Bouchere6869a82000-03-20 06:03:29 +00002699/* Translates errno numbers into more human-readable form than strerror. */
2700const char *
Rusty Russell79dee072000-05-02 16:45:16 +00002701TC_STRERROR(int err)
Marc Bouchere6869a82000-03-20 06:03:29 +00002702{
2703 unsigned int i;
2704 struct table_struct {
2705 void *fn;
2706 int err;
2707 const char *message;
2708 } table [] =
Harald Welte4ccfa632001-07-30 15:12:43 +00002709 { { TC_INIT, EPERM, "Permission denied (you must be root)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002710 { TC_INIT, EINVAL, "Module is wrong version" },
Jesper Dangaard Brouera9fe5b32009-03-23 14:26:56 +01002711 { TC_INIT, ENOENT,
Harald Welte4ccfa632001-07-30 15:12:43 +00002712 "Table does not exist (do you need to insmod?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002713 { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
2714 { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
2715 { TC_DELETE_CHAIN, EMLINK,
Marc Bouchere6869a82000-03-20 06:03:29 +00002716 "Can't delete chain with references left" },
Rusty Russell79dee072000-05-02 16:45:16 +00002717 { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
2718 { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
2719 { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
2720 { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
Harald Welte1cef74d2001-01-05 15:22:59 +00002721 { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
2722 { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
Rusty Russell79dee072000-05-02 16:45:16 +00002723 { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
2724 { TC_INSERT_ENTRY, EINVAL, "Target problem" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002725 /* ENOENT for DELETE probably means no matching rule */
Rusty Russell79dee072000-05-02 16:45:16 +00002726 { TC_DELETE_ENTRY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002727 "Bad rule (does a matching rule exist in that chain?)" },
Rusty Russell79dee072000-05-02 16:45:16 +00002728 { TC_SET_POLICY, ENOENT,
Marc Boucherc8264992000-04-22 22:34:44 +00002729 "Bad built-in chain name" },
Rusty Russell79dee072000-05-02 16:45:16 +00002730 { TC_SET_POLICY, EINVAL,
Marc Boucherc8264992000-04-22 22:34:44 +00002731 "Bad policy name" },
Harald Welte4ccfa632001-07-30 15:12:43 +00002732
2733 { NULL, 0, "Incompatible with this kernel" },
2734 { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
2735 { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
2736 { NULL, ENOMEM, "Memory allocation problem" },
2737 { NULL, ENOENT, "No chain/target/match by that name" },
Marc Bouchere6869a82000-03-20 06:03:29 +00002738 };
2739
2740 for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
2741 if ((!table[i].fn || table[i].fn == iptc_fn)
2742 && table[i].err == err)
2743 return table[i].message;
2744 }
2745
2746 return strerror(err);
2747}
Jan Engelhardtde4d2d32011-08-27 12:50:32 +02002748
2749const struct xtc_ops TC_OPS = {
2750 .commit = TC_COMMIT,
2751 .free = TC_FREE,
2752 .builtin = TC_BUILTIN,
2753 .is_chain = TC_IS_CHAIN,
2754 .flush_entries = TC_FLUSH_ENTRIES,
2755 .create_chain = TC_CREATE_CHAIN,
2756 .set_policy = TC_SET_POLICY,
2757 .strerror = TC_STRERROR,
2758};