blob: 2bfe372730743a06195ebf287e7f93b2e259b4ad [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Library which manipulates firewall rules. Version 0.1. */
2
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
11/* (C)1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
12 COPYING for details). */
13
14#include <assert.h>
15#include <string.h>
16#include <errno.h>
17#include <stdlib.h>
18#include <stdio.h>
19#include <unistd.h>
20#include <arpa/inet.h>
21
22#ifdef DEBUG_CONNTRACK
23#define inline
24#endif
25
26#ifndef __UC_LIBC__
27#if !defined(__GLIBC__) || (__GLIBC__ < 2)
28typedef unsigned int socklen_t;
29#endif
30#endif
31
32#include "libiptc/libiptc.h"
33
34#define IP_VERSION 4
35#define IP_OFFSET 0x1FFF
36
37#define HOOK_PRE_ROUTING NF_IP_PRE_ROUTING
38#define HOOK_LOCAL_IN NF_IP_LOCAL_IN
39#define HOOK_FORWARD NF_IP_FORWARD
40#define HOOK_LOCAL_OUT NF_IP_LOCAL_OUT
41#define HOOK_POST_ROUTING NF_IP_POST_ROUTING
42#ifdef NF_IP_DROPPING
43#define HOOK_DROPPING NF_IP_DROPPING
44#endif
45
46#define STRUCT_ENTRY_TARGET struct ipt_entry_target
47#define STRUCT_ENTRY struct ipt_entry
48#define STRUCT_ENTRY_MATCH struct ipt_entry_match
49#define STRUCT_GETINFO struct ipt_getinfo
50#define STRUCT_GET_ENTRIES struct ipt_get_entries
51#define STRUCT_COUNTERS struct ipt_counters
52#define STRUCT_COUNTERS_INFO struct ipt_counters_info
53#define STRUCT_STANDARD_TARGET struct ipt_standard_target
54#define STRUCT_REPLACE struct ipt_replace
55
56#define STRUCT_TC_HANDLE struct iptc_handle
57#define xtc_handle iptc_handle
58
59#define ENTRY_ITERATE IPT_ENTRY_ITERATE
60#define TABLE_MAXNAMELEN IPT_TABLE_MAXNAMELEN
61#define FUNCTION_MAXNAMELEN IPT_FUNCTION_MAXNAMELEN
62
63#define GET_TARGET ipt_get_target
64
65#define ERROR_TARGET IPT_ERROR_TARGET
66#define NUMHOOKS NF_IP_NUMHOOKS
67
68#define IPT_CHAINLABEL ipt_chainlabel
69
70#define TC_DUMP_ENTRIES dump_entries
71#define TC_IS_CHAIN iptc_is_chain
72#define TC_FIRST_CHAIN iptc_first_chain
73#define TC_NEXT_CHAIN iptc_next_chain
74#define TC_FIRST_RULE iptc_first_rule
75#define TC_NEXT_RULE iptc_next_rule
76#define TC_GET_TARGET iptc_get_target
77#define TC_BUILTIN iptc_builtin
78#define TC_GET_POLICY iptc_get_policy
79#define TC_INSERT_ENTRY iptc_insert_entry
80#define TC_REPLACE_ENTRY iptc_replace_entry
81#define TC_APPEND_ENTRY iptc_append_entry
82#define TC_DELETE_ENTRY iptc_delete_entry
83#define TC_DELETE_NUM_ENTRY iptc_delete_num_entry
84#define TC_FLUSH_ENTRIES iptc_flush_entries
85#define TC_ZERO_ENTRIES iptc_zero_entries
86#define TC_READ_COUNTER iptc_read_counter
87#define TC_ZERO_COUNTER iptc_zero_counter
88#define TC_SET_COUNTER iptc_set_counter
89#define TC_CREATE_CHAIN iptc_create_chain
90#define TC_GET_REFERENCES iptc_get_references
91#define TC_DELETE_CHAIN iptc_delete_chain
92#define TC_RENAME_CHAIN iptc_rename_chain
93#define TC_SET_POLICY iptc_set_policy
94#define TC_GET_RAW_SOCKET iptc_get_raw_socket
95#define TC_INIT iptc_init
96#define TC_FREE iptc_free
97#define TC_COMMIT iptc_commit
98#define TC_STRERROR iptc_strerror
99#define TC_NUM_RULES iptc_num_rules
100#define TC_GET_RULE iptc_get_rule
101
102#define TC_AF AF_INET
103#define TC_IPPROTO IPPROTO_IP
104
105#define SO_SET_REPLACE IPT_SO_SET_REPLACE
106#define SO_SET_ADD_COUNTERS IPT_SO_SET_ADD_COUNTERS
107#define SO_GET_INFO IPT_SO_GET_INFO
108#define SO_GET_ENTRIES IPT_SO_GET_ENTRIES
109#define SO_GET_VERSION IPT_SO_GET_VERSION
110
111#define STANDARD_TARGET IPT_STANDARD_TARGET
112#define LABEL_RETURN IPTC_LABEL_RETURN
113#define LABEL_ACCEPT IPTC_LABEL_ACCEPT
114#define LABEL_DROP IPTC_LABEL_DROP
115#define LABEL_QUEUE IPTC_LABEL_QUEUE
116
117#define ALIGN IPT_ALIGN
118#define RETURN IPT_RETURN
119
120#include "libiptc.c"
121
122#define IP_PARTS_NATIVE(n) \
123(unsigned int)((n)>>24)&0xFF, \
124(unsigned int)((n)>>16)&0xFF, \
125(unsigned int)((n)>>8)&0xFF, \
126(unsigned int)((n)&0xFF)
127
128#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
129
130int
131dump_entry(STRUCT_ENTRY *e, struct iptc_handle *const handle)
132{
133 size_t i;
134 STRUCT_ENTRY_TARGET *t;
135
136 printf("Entry %u (%lu):\n", iptcb_entry2index(handle, e),
137 iptcb_entry2offset(handle, e));
138 printf("SRC IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
139 IP_PARTS(e->ip.src.s_addr),IP_PARTS(e->ip.smsk.s_addr));
140 printf("DST IP: %u.%u.%u.%u/%u.%u.%u.%u\n",
141 IP_PARTS(e->ip.dst.s_addr),IP_PARTS(e->ip.dmsk.s_addr));
142 printf("Interface: `%s'/", e->ip.iniface);
143 for (i = 0; i < IFNAMSIZ; i++)
144 printf("%c", e->ip.iniface_mask[i] ? 'X' : '.');
145 printf("to `%s'/", e->ip.outiface);
146 for (i = 0; i < IFNAMSIZ; i++)
147 printf("%c", e->ip.outiface_mask[i] ? 'X' : '.');
148 printf("\nProtocol: %u\n", e->ip.proto);
149 printf("Flags: %02X\n", e->ip.flags);
150 printf("Invflags: %02X\n", e->ip.invflags);
151 printf("Counters: %llu packets, %llu bytes\n",
152 (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
153 printf("Cache: %08X\n", e->nfcache);
154
155 IPT_MATCH_ITERATE(e, print_match);
156
157 t = GET_TARGET(e);
158 printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
159 if (strcmp(t->u.user.name, STANDARD_TARGET) == 0) {
160 int pos = *(int *)t->data;
161 if (pos < 0)
162 printf("verdict=%s\n",
163 pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
164 : pos == -NF_DROP-1 ? "NF_DROP"
165 : pos == -NF_QUEUE-1 ? "NF_QUEUE"
166 : pos == RETURN ? "RETURN"
167 : "UNKNOWN");
168 else
169 printf("verdict=%u\n", pos);
170 } else if (strcmp(t->u.user.name, IPT_ERROR_TARGET) == 0)
171 printf("error=`%s'\n", t->data);
172
173 printf("\n");
174 return 0;
175}
176
177static unsigned char *
178is_same(const STRUCT_ENTRY *a, const STRUCT_ENTRY *b, unsigned char *matchmask)
179{
180 unsigned int i;
181 unsigned char *mptr;
182
183 /* Always compare head structures: ignore mask here. */
184 if (a->ip.src.s_addr != b->ip.src.s_addr
185 || a->ip.dst.s_addr != b->ip.dst.s_addr
186 || a->ip.smsk.s_addr != b->ip.smsk.s_addr
187 || a->ip.dmsk.s_addr != b->ip.dmsk.s_addr
188 || a->ip.proto != b->ip.proto
189 || a->ip.flags != b->ip.flags
190 || a->ip.invflags != b->ip.invflags)
191 return NULL;
192
193 for (i = 0; i < IFNAMSIZ; i++) {
194 if (a->ip.iniface_mask[i] != b->ip.iniface_mask[i])
195 return NULL;
196 if ((a->ip.iniface[i] & a->ip.iniface_mask[i])
197 != (b->ip.iniface[i] & b->ip.iniface_mask[i]))
198 return NULL;
199 if (a->ip.outiface_mask[i] != b->ip.outiface_mask[i])
200 return NULL;
201 if ((a->ip.outiface[i] & a->ip.outiface_mask[i])
202 != (b->ip.outiface[i] & b->ip.outiface_mask[i]))
203 return NULL;
204 }
205
206 if (a->target_offset != b->target_offset
207 || a->next_offset != b->next_offset)
208 return NULL;
209
210 mptr = matchmask + sizeof(STRUCT_ENTRY);
211 if (IPT_MATCH_ITERATE(a, match_different, a->elems, b->elems, &mptr))
212 return NULL;
213 mptr += IPT_ALIGN(sizeof(struct ipt_entry_target));
214
215 return mptr;
216}
217
218#if 0
219/***************************** DEBUGGING ********************************/
220static inline int
221unconditional(const struct ipt_ip *ip)
222{
223 unsigned int i;
224
225 for (i = 0; i < sizeof(*ip)/sizeof(u_int32_t); i++)
226 if (((u_int32_t *)ip)[i])
227 return 0;
228
229 return 1;
230}
231
232static inline int
233check_match(const STRUCT_ENTRY_MATCH *m, unsigned int *off)
234{
235 assert(m->u.match_size >= sizeof(STRUCT_ENTRY_MATCH));
236 assert(ALIGN(m->u.match_size) == m->u.match_size);
237
238 (*off) += m->u.match_size;
239 return 0;
240}
241
242static inline int
243check_entry(const STRUCT_ENTRY *e, unsigned int *i, unsigned int *off,
244 unsigned int user_offset, int *was_return,
245 struct iptc_handle *h)
246{
247 unsigned int toff;
248 STRUCT_STANDARD_TARGET *t;
249
250 assert(e->target_offset >= sizeof(STRUCT_ENTRY));
251 assert(e->next_offset >= e->target_offset
252 + sizeof(STRUCT_ENTRY_TARGET));
253 toff = sizeof(STRUCT_ENTRY);
254 IPT_MATCH_ITERATE(e, check_match, &toff);
255
256 assert(toff == e->target_offset);
257
258 t = (STRUCT_STANDARD_TARGET *)
259 GET_TARGET((STRUCT_ENTRY *)e);
260 /* next_offset will have to be multiple of entry alignment. */
261 assert(e->next_offset == ALIGN(e->next_offset));
262 assert(e->target_offset == ALIGN(e->target_offset));
263 assert(t->target.u.target_size == ALIGN(t->target.u.target_size));
264 assert(!TC_IS_CHAIN(t->target.u.user.name, h));
265
266 if (strcmp(t->target.u.user.name, STANDARD_TARGET) == 0) {
267 assert(t->target.u.target_size
268 == ALIGN(sizeof(STRUCT_STANDARD_TARGET)));
269
270 assert(t->verdict == -NF_DROP-1
271 || t->verdict == -NF_ACCEPT-1
272 || t->verdict == RETURN
273 || t->verdict < (int)h->entries->size);
274
275 if (t->verdict >= 0) {
276 STRUCT_ENTRY *te = get_entry(h, t->verdict);
277 int idx;
278
279 idx = iptcb_entry2index(h, te);
280 assert(strcmp(GET_TARGET(te)->u.user.name,
281 IPT_ERROR_TARGET)
282 != 0);
283 assert(te != e);
284
285 /* Prior node must be error node, or this node. */
286 assert(t->verdict == iptcb_entry2offset(h, e)+e->next_offset
287 || strcmp(GET_TARGET(index2entry(h, idx-1))
288 ->u.user.name, IPT_ERROR_TARGET)
289 == 0);
290 }
291
292 if (t->verdict == RETURN
293 && unconditional(&e->ip)
294 && e->target_offset == sizeof(*e))
295 *was_return = 1;
296 else
297 *was_return = 0;
298 } else if (strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0) {
299 assert(t->target.u.target_size
300 == ALIGN(sizeof(struct ipt_error_target)));
301
302 /* If this is in user area, previous must have been return */
303 if (*off > user_offset)
304 assert(*was_return);
305
306 *was_return = 0;
307 }
308 else *was_return = 0;
309
310 if (*off == user_offset)
311 assert(strcmp(t->target.u.user.name, IPT_ERROR_TARGET) == 0);
312
313 (*off) += e->next_offset;
314 (*i)++;
315 return 0;
316}
317
318#ifdef IPTC_DEBUG
319/* Do every conceivable sanity check on the handle */
320static void
321do_check(struct iptc_handle *h, unsigned int line)
322{
323 unsigned int i, n;
324 unsigned int user_offset; /* Offset of first user chain */
325 int was_return;
326
327 assert(h->changed == 0 || h->changed == 1);
328 if (strcmp(h->info.name, "filter") == 0) {
329 assert(h->info.valid_hooks
330 == (1 << NF_IP_LOCAL_IN
331 | 1 << NF_IP_FORWARD
332 | 1 << NF_IP_LOCAL_OUT));
333
334 /* Hooks should be first three */
335 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == 0);
336
337 n = get_chain_end(h, 0);
338 n += get_entry(h, n)->next_offset;
339 assert(h->info.hook_entry[NF_IP_FORWARD] == n);
340
341 n = get_chain_end(h, n);
342 n += get_entry(h, n)->next_offset;
343 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
344
345 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
346 } else if (strcmp(h->info.name, "nat") == 0) {
347 assert((h->info.valid_hooks
348 == (1 << NF_IP_PRE_ROUTING
349 | 1 << NF_IP_POST_ROUTING
350 | 1 << NF_IP_LOCAL_OUT)) ||
351 (h->info.valid_hooks
352 == (1 << NF_IP_PRE_ROUTING
353 | 1 << NF_IP_LOCAL_IN
354 | 1 << NF_IP_POST_ROUTING
355 | 1 << NF_IP_LOCAL_OUT)));
356
357 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
358
359 n = get_chain_end(h, 0);
360
361 n += get_entry(h, n)->next_offset;
362 assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
363 n = get_chain_end(h, n);
364
365 n += get_entry(h, n)->next_offset;
366 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
367 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
368
369 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
370 n = get_chain_end(h, n);
371 n += get_entry(h, n)->next_offset;
372 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
373 user_offset = h->info.hook_entry[NF_IP_LOCAL_IN];
374 }
375
376 } else if (strcmp(h->info.name, "mangle") == 0) {
377 /* This code is getting ugly because linux < 2.4.18-pre6 had
378 * two mangle hooks, linux >= 2.4.18-pre6 has five mangle hooks
379 * */
380 assert((h->info.valid_hooks
381 == (1 << NF_IP_PRE_ROUTING
382 | 1 << NF_IP_LOCAL_OUT)) ||
383 (h->info.valid_hooks
384 == (1 << NF_IP_PRE_ROUTING
385 | 1 << NF_IP_LOCAL_IN
386 | 1 << NF_IP_FORWARD
387 | 1 << NF_IP_LOCAL_OUT
388 | 1 << NF_IP_POST_ROUTING)));
389
390 /* Hooks should be first five */
391 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
392
393 n = get_chain_end(h, 0);
394
395 if (h->info.valid_hooks & (1 << NF_IP_LOCAL_IN)) {
396 n += get_entry(h, n)->next_offset;
397 assert(h->info.hook_entry[NF_IP_LOCAL_IN] == n);
398 n = get_chain_end(h, n);
399 }
400
401 if (h->info.valid_hooks & (1 << NF_IP_FORWARD)) {
402 n += get_entry(h, n)->next_offset;
403 assert(h->info.hook_entry[NF_IP_FORWARD] == n);
404 n = get_chain_end(h, n);
405 }
406
407 n += get_entry(h, n)->next_offset;
408 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
409 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
410
411 if (h->info.valid_hooks & (1 << NF_IP_POST_ROUTING)) {
412 n = get_chain_end(h, n);
413 n += get_entry(h, n)->next_offset;
414 assert(h->info.hook_entry[NF_IP_POST_ROUTING] == n);
415 user_offset = h->info.hook_entry[NF_IP_POST_ROUTING];
416 }
417 } else if (strcmp(h->info.name, "raw") == 0) {
418 assert(h->info.valid_hooks
419 == (1 << NF_IP_PRE_ROUTING
420 | 1 << NF_IP_LOCAL_OUT));
421
422 /* Hooks should be first three */
423 assert(h->info.hook_entry[NF_IP_PRE_ROUTING] == 0);
424
425 n = get_chain_end(h, n);
426 n += get_entry(h, n)->next_offset;
427 assert(h->info.hook_entry[NF_IP_LOCAL_OUT] == n);
428
429 user_offset = h->info.hook_entry[NF_IP_LOCAL_OUT];
430
431#ifdef NF_IP_DROPPING
432 } else if (strcmp(h->info.name, "drop") == 0) {
433 assert(h->info.valid_hooks == (1 << NF_IP_DROPPING));
434
435 /* Hook should be first */
436 assert(h->info.hook_entry[NF_IP_DROPPING] == 0);
437 user_offset = 0;
438#endif
439 } else {
440 fprintf(stderr, "Unknown table `%s'\n", h->info.name);
441 abort();
442 }
443
444 /* User chain == end of last builtin + policy entry */
445 user_offset = get_chain_end(h, user_offset);
446 user_offset += get_entry(h, user_offset)->next_offset;
447
448 /* Overflows should be end of entry chains, and unconditional
449 policy nodes. */
450 for (i = 0; i < NUMHOOKS; i++) {
451 STRUCT_ENTRY *e;
452 STRUCT_STANDARD_TARGET *t;
453
454 if (!(h->info.valid_hooks & (1 << i)))
455 continue;
456 assert(h->info.underflow[i]
457 == get_chain_end(h, h->info.hook_entry[i]));
458
459 e = get_entry(h, get_chain_end(h, h->info.hook_entry[i]));
460 assert(unconditional(&e->ip));
461 assert(e->target_offset == sizeof(*e));
462 t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
463 assert(t->target.u.target_size == ALIGN(sizeof(*t)));
464 assert(e->next_offset == sizeof(*e) + ALIGN(sizeof(*t)));
465
466 assert(strcmp(t->target.u.user.name, STANDARD_TARGET)==0);
467 assert(t->verdict == -NF_DROP-1 || t->verdict == -NF_ACCEPT-1);
468
469 /* Hooks and underflows must be valid entries */
470 entry2index(h, get_entry(h, h->info.hook_entry[i]));
471 entry2index(h, get_entry(h, h->info.underflow[i]));
472 }
473
474 assert(h->info.size
475 >= h->info.num_entries * (sizeof(STRUCT_ENTRY)
476 +sizeof(STRUCT_STANDARD_TARGET)));
477
478 assert(h->entries.size
479 >= (h->new_number
480 * (sizeof(STRUCT_ENTRY)
481 + sizeof(STRUCT_STANDARD_TARGET))));
482 assert(strcmp(h->info.name, h->entries.name) == 0);
483
484 i = 0; n = 0;
485 was_return = 0;
486 /* Check all the entries. */
487 ENTRY_ITERATE(h->entries.entrytable, h->entries.size,
488 check_entry, &i, &n, user_offset, &was_return, h);
489
490 assert(i == h->new_number);
491 assert(n == h->entries.size);
492
493 /* Final entry must be error node */
494 assert(strcmp(GET_TARGET(index2entry(h, h->new_number-1))
495 ->u.user.name,
496 ERROR_TARGET) == 0);
497}
498#endif /*IPTC_DEBUG*/
499
500#endif