blob: d825130e04e361c190617de95029044f627c6502 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to save the iptables state, in human readable-form. */
Harald Welte10a907f2002-08-07 09:07:41 +00002/* (C) 1999 by Paul 'Rusty' Russell <rusty@rustcorp.com.au> and
3 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
4 *
5 * This code is distributed under the terms of GNU GPL v2
6 *
Harald Welteae1ff9f2000-12-01 14:26:20 +00007 */
Marc Bouchere6869a82000-03-20 06:03:29 +00008#include <getopt.h>
9#include <sys/errno.h>
10#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000011#include <fcntl.h>
12#include <stdlib.h>
13#include <string.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000014#include <dlfcn.h>
15#include <time.h>
Harald Welted8ac9672004-04-15 10:10:19 +000016#include <netdb.h>
Rusty Russellb1f69be2000-08-27 07:42:54 +000017#include "libiptc/libiptc.h"
18#include "iptables.h"
Marc Bouchere6869a82000-03-20 06:03:29 +000019
Rusty Russella8f033e2000-07-30 01:43:01 +000020static int binary = 0, counters = 0;
21
Marc Bouchere6869a82000-03-20 06:03:29 +000022static struct option options[] = {
Rusty Russella8f033e2000-07-30 01:43:01 +000023 { "binary", 0, 0, 'b' },
24 { "counters", 0, 0, 'c' },
25 { "dump", 0, 0, 'd' },
26 { "table", 1, 0, 't' },
Marc Bouchere6869a82000-03-20 06:03:29 +000027 { 0 }
28};
29
30#define IP_PARTS_NATIVE(n) \
31(unsigned int)((n)>>24)&0xFF, \
32(unsigned int)((n)>>16)&0xFF, \
33(unsigned int)((n)>>8)&0xFF, \
34(unsigned int)((n)&0xFF)
35
36#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
37
38/* This assumes that mask is contiguous, and byte-bounded. */
39static void
40print_iface(char letter, const char *iface, const unsigned char *mask,
41 int invert)
42{
43 unsigned int i;
44
45 if (mask[0] == 0)
46 return;
Rusty Russell7e53bf92000-03-20 07:03:28 +000047
Marc Bouchere6869a82000-03-20 06:03:29 +000048 printf("-%c %s", letter, invert ? "! " : "");
49
50 for (i = 0; i < IFNAMSIZ; i++) {
51 if (mask[i] != 0) {
52 if (iface[i] != '\0')
53 printf("%c", iface[i]);
54 } else {
Harald Welte9535e682001-11-08 22:28:23 +000055 /* we can access iface[i-1] here, because
56 * a few lines above we make sure that mask[0] != 0 */
57 if (iface[i-1] != '\0')
Marc Bouchere6869a82000-03-20 06:03:29 +000058 printf("+");
59 break;
60 }
61 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000062
63 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +000064}
65
66/* These are hardcoded backups in iptables.c, so they are safe */
67struct pprot {
68 char *name;
69 u_int8_t num;
70};
71
András Kis-Szabó764316a2001-02-26 17:31:20 +000072/* FIXME: why don't we use /etc/protocols ? */
Marc Bouchere6869a82000-03-20 06:03:29 +000073static const struct pprot chain_protos[] = {
74 { "tcp", IPPROTO_TCP },
75 { "udp", IPPROTO_UDP },
76 { "icmp", IPPROTO_ICMP },
András Kis-Szabó764316a2001-02-26 17:31:20 +000077 { "esp", IPPROTO_ESP },
78 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +000079 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +000080};
81
82static void print_proto(u_int16_t proto, int invert)
83{
84 if (proto) {
85 unsigned int i;
86 const char *invertstr = invert ? "! " : "";
87
Pedro Lamarão0e3b3372004-04-07 09:33:17 +000088 struct protoent *pent = getprotobynumber(proto);
89 if (pent) {
90 printf("-p %s%s ", invertstr, pent->p_name);
91 return;
92 }
93
Marc Bouchere6869a82000-03-20 06:03:29 +000094 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
95 if (chain_protos[i].num == proto) {
96 printf("-p %s%s ",
97 invertstr, chain_protos[i].name);
98 return;
99 }
100
101 printf("-p %s%u ", invertstr, proto);
102 }
103}
104
Harald Welteae1ff9f2000-12-01 14:26:20 +0000105#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +0000106static int non_zero(const void *ptr, size_t size)
107{
108 unsigned int i;
109
110 for (i = 0; i < size; i++)
111 if (((char *)ptr)[i])
112 return 0;
113
114 return 1;
115}
Harald Welteae1ff9f2000-12-01 14:26:20 +0000116#endif
117
Harald Welte32db5242001-01-23 22:46:22 +0000118static int print_match(const struct ipt_entry_match *e,
119 const struct ipt_ip *ip)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000120{
121 struct iptables_match *match
Martin Josefsson78cafda2004-02-02 20:01:18 +0000122 = find_match(e->u.user.name, TRY_LOAD, NULL);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000123
124 if (match) {
125 printf("-m %s ", e->u.user.name);
Harald Weltee8137792001-01-24 01:32:51 +0000126
127 /* some matches don't provide a save function */
128 if (match->save)
129 match->save(ip, e);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000130 } else {
131 if (e->u.match_size) {
132 fprintf(stderr,
133 "Can't find library for match `%s'\n",
134 e->u.user.name);
135 exit(1);
136 }
137 }
138 return 0;
139}
140
141/* print a given ip including mask if neccessary */
142static void print_ip(char *prefix, u_int32_t ip, u_int32_t mask, int invert)
143{
144 if (!mask && !ip)
145 return;
146
147 printf("%s %s%u.%u.%u.%u",
148 prefix,
149 invert ? "! " : "",
150 IP_PARTS(ip));
151
152 if (mask != 0xffffffff)
153 printf("/%u.%u.%u.%u ", IP_PARTS(mask));
154 else
155 printf(" ");
156}
Marc Bouchere6869a82000-03-20 06:03:29 +0000157
158/* We want this to be readable, so only print out neccessary fields.
159 * Because that's the kind of world I want to live in. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000160static void print_rule(const struct ipt_entry *e,
Harald Welte9f7fa492001-03-15 15:12:02 +0000161 iptc_handle_t *h, const char *chain, int counters)
Marc Bouchere6869a82000-03-20 06:03:29 +0000162{
Harald Welteae1ff9f2000-12-01 14:26:20 +0000163 struct ipt_entry_target *t;
Harald Welteace8a012001-07-05 06:29:10 +0000164 const char *target_name;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000165
166 /* print counters */
Marc Bouchere6869a82000-03-20 06:03:29 +0000167 if (counters)
Martin Josefssona28d4952004-05-26 16:04:48 +0000168 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Marc Bouchere6869a82000-03-20 06:03:29 +0000169
Harald Welte9f7fa492001-03-15 15:12:02 +0000170 /* print chain name */
171 printf("-A %s ", chain);
172
Marc Bouchere6869a82000-03-20 06:03:29 +0000173 /* Print IP part. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000174 print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr,
175 e->ip.invflags & IPT_INV_SRCIP);
176
177 print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
Harald Weltefa7625a2001-01-26 03:28:18 +0000178 e->ip.invflags & IPT_INV_DSTIP);
Marc Bouchere6869a82000-03-20 06:03:29 +0000179
180 print_iface('i', e->ip.iniface, e->ip.iniface_mask,
181 e->ip.invflags & IPT_INV_VIA_IN);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000182
Marc Bouchere6869a82000-03-20 06:03:29 +0000183 print_iface('o', e->ip.outiface, e->ip.outiface_mask,
184 e->ip.invflags & IPT_INV_VIA_OUT);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000185
Marc Bouchere6869a82000-03-20 06:03:29 +0000186 print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
187
188 if (e->ip.flags & IPT_F_FRAG)
189 printf("%s-f ",
190 e->ip.invflags & IPT_INV_FRAG ? "! " : "");
191
Marc Bouchere6869a82000-03-20 06:03:29 +0000192 /* Print matchinfo part */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000193 if (e->target_offset) {
Harald Welte32db5242001-01-23 22:46:22 +0000194 IPT_MATCH_ITERATE(e, print_match, &e->ip);
Marc Bouchere6869a82000-03-20 06:03:29 +0000195 }
196
Harald Welteae1ff9f2000-12-01 14:26:20 +0000197 /* Print target name */
Harald Welte6f83cf02001-05-24 23:22:28 +0000198 target_name = iptc_get_target(e, h);
199 if (target_name && (*target_name != '\0'))
200 printf("-j %s ", target_name);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000201
Marc Bouchere6869a82000-03-20 06:03:29 +0000202 /* Print targinfo part */
Rusty Russell082ba022001-01-07 06:54:51 +0000203 t = ipt_get_target((struct ipt_entry *)e);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000204 if (t->u.user.name[0]) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000205 struct iptables_target *target
Harald Welteae1ff9f2000-12-01 14:26:20 +0000206 = find_target(t->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000207
Harald Welte31098ad2001-10-16 09:40:13 +0000208 if (!target) {
209 fprintf(stderr, "Can't find library for target `%s'\n",
210 t->u.user.name);
211 exit(1);
212 }
213
214 if (target->save)
Harald Welte32db5242001-01-23 22:46:22 +0000215 target->save(&e->ip, t);
Marc Bouchere6869a82000-03-20 06:03:29 +0000216 else {
Harald Welte31098ad2001-10-16 09:40:13 +0000217 /* If the target size is greater than ipt_entry_target
218 * there is something to be saved, we just don't know
219 * how to print it */
220 if (t->u.target_size !=
221 sizeof(struct ipt_entry_target)) {
222 fprintf(stderr, "Target `%s' is missing "
223 "save function\n",
Harald Welteae1ff9f2000-12-01 14:26:20 +0000224 t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000225 exit(1);
226 }
227 }
228 }
229 printf("\n");
230}
231
232/* Debugging prototype. */
Rusty Russella8f033e2000-07-30 01:43:01 +0000233static int for_each_table(int (*func)(const char *tablename))
234{
235 int ret = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000236 FILE *procfile = NULL;
Rusty Russella8f033e2000-07-30 01:43:01 +0000237 char tablename[IPT_TABLE_MAXNAMELEN+1];
238
Harald Welteae1ff9f2000-12-01 14:26:20 +0000239 procfile = fopen("/proc/net/ip_tables_names", "r");
Rusty Russella8f033e2000-07-30 01:43:01 +0000240 if (!procfile)
241 return 0;
242
243 while (fgets(tablename, sizeof(tablename), procfile)) {
244 if (tablename[strlen(tablename) - 1] != '\n')
245 exit_error(OTHER_PROBLEM,
246 "Badly formed tablename `%s'\n",
247 tablename);
248 tablename[strlen(tablename) - 1] = '\0';
249 ret &= func(tablename);
250 }
251
252 return ret;
253}
254
255
Rusty Russella8f033e2000-07-30 01:43:01 +0000256static int do_output(const char *tablename)
257{
258 iptc_handle_t h;
259 const char *chain = NULL;
260
261 if (!tablename)
262 return for_each_table(&do_output);
263
264 h = iptc_init(tablename);
265 if (!h)
266 exit_error(OTHER_PROBLEM, "Can't initialize: %s\n",
267 iptc_strerror(errno));
268
269 if (!binary) {
270 time_t now = time(NULL);
271
272 printf("# Generated by iptables-save v%s on %s",
Harald Welte80fe35d2002-05-29 13:08:15 +0000273 IPTABLES_VERSION, ctime(&now));
Harald Welteae1ff9f2000-12-01 14:26:20 +0000274 printf("*%s\n", tablename);
Rusty Russella8f033e2000-07-30 01:43:01 +0000275
Harald Welte9f7fa492001-03-15 15:12:02 +0000276 /* Dump out chain names first,
277 * thereby preventing dependency conflicts */
Rusty Russella8f033e2000-07-30 01:43:01 +0000278 for (chain = iptc_first_chain(&h);
279 chain;
280 chain = iptc_next_chain(&h)) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000281
Rusty Russella8f033e2000-07-30 01:43:01 +0000282 printf(":%s ", chain);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000283 if (iptc_builtin(chain, h)) {
Rusty Russella8f033e2000-07-30 01:43:01 +0000284 struct ipt_counters count;
285 printf("%s ",
286 iptc_get_policy(chain, &count, &h));
Martin Josefssona28d4952004-05-26 16:04:48 +0000287 printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
Rusty Russella8f033e2000-07-30 01:43:01 +0000288 } else {
Harald Welted8e65632001-01-05 15:20:07 +0000289 printf("- [0:0]\n");
Rusty Russella8f033e2000-07-30 01:43:01 +0000290 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000291 }
292
293
294 for (chain = iptc_first_chain(&h);
295 chain;
296 chain = iptc_next_chain(&h)) {
297 const struct ipt_entry *e;
Rusty Russella8f033e2000-07-30 01:43:01 +0000298
Harald Welteae1ff9f2000-12-01 14:26:20 +0000299 /* Dump out rules */
300 e = iptc_first_rule(chain, &h);
301 while(e) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000302 print_rule(e, &h, chain, counters);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000303 e = iptc_next_rule(e, &h);
Rusty Russella8f033e2000-07-30 01:43:01 +0000304 }
305 }
306
307 now = time(NULL);
308 printf("COMMIT\n");
309 printf("# Completed on %s", ctime(&now));
310 } else {
311 /* Binary, huh? OK. */
312 exit_error(OTHER_PROBLEM, "Binary NYI\n");
313 }
314
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000315 iptc_free(&h);
316
Rusty Russella8f033e2000-07-30 01:43:01 +0000317 return 1;
318}
319
Marc Bouchere6869a82000-03-20 06:03:29 +0000320/* Format:
321 * :Chain name POLICY packets bytes
322 * rule
323 */
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000324#ifdef IPTABLES_MULTI
325int
326iptables_save_main(int argc, char *argv[])
327#else
328int
329main(int argc, char *argv[])
330#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000331{
Rusty Russella8f033e2000-07-30 01:43:01 +0000332 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000333 int c;
Marc Bouchere6869a82000-03-20 06:03:29 +0000334
335 program_name = "iptables-save";
Harald Welte80fe35d2002-05-29 13:08:15 +0000336 program_version = IPTABLES_VERSION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000337
Harald Welte3efb6ea2001-08-06 18:50:21 +0000338#ifdef NO_SHARED_LIBS
339 init_extensions();
340#endif
341
Marc Boucher163ad782001-12-06 15:05:48 +0000342 while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000343 switch (c) {
344 case 'b':
345 binary = 1;
346 break;
347
348 case 'c':
349 counters = 1;
350 break;
351
Rusty Russella8f033e2000-07-30 01:43:01 +0000352 case 't':
353 /* Select specific table. */
354 tablename = optarg;
355 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000356 case 'd':
Harald Welteae1ff9f2000-12-01 14:26:20 +0000357 do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000358 exit(0);
359 }
360 }
361
362 if (optind < argc) {
363 fprintf(stderr, "Unknown arguments found on commandline");
364 exit(1);
365 }
366
Rusty Russella8f033e2000-07-30 01:43:01 +0000367 return !do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000368}