blob: aa3b69af6b8691a109f54277d4333220f7b7bc27 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to save the iptables state, in human readable-form. */
Rusty Russell6df83682001-07-10 13:41:49 +00002/* Authors: Paul 'Rusty' Russell <rusty@rustcorp.com.au> and
Harald Welteae1ff9f2000-12-01 14:26:20 +00003 * Harald Welte <laforge@gnumonks.org>
4 */
Marc Bouchere6869a82000-03-20 06:03:29 +00005#include <getopt.h>
6#include <sys/errno.h>
7#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +00008#include <fcntl.h>
9#include <stdlib.h>
10#include <string.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000011#include <dlfcn.h>
12#include <time.h>
Rusty Russellb1f69be2000-08-27 07:42:54 +000013#include "libiptc/libiptc.h"
14#include "iptables.h"
Marc Bouchere6869a82000-03-20 06:03:29 +000015
Rusty Russella8f033e2000-07-30 01:43:01 +000016static int binary = 0, counters = 0;
17
Marc Bouchere6869a82000-03-20 06:03:29 +000018static struct option options[] = {
Rusty Russella8f033e2000-07-30 01:43:01 +000019 { "binary", 0, 0, 'b' },
20 { "counters", 0, 0, 'c' },
21 { "dump", 0, 0, 'd' },
22 { "table", 1, 0, 't' },
Marc Bouchere6869a82000-03-20 06:03:29 +000023 { 0 }
24};
25
26#define IP_PARTS_NATIVE(n) \
27(unsigned int)((n)>>24)&0xFF, \
28(unsigned int)((n)>>16)&0xFF, \
29(unsigned int)((n)>>8)&0xFF, \
30(unsigned int)((n)&0xFF)
31
32#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
33
34/* This assumes that mask is contiguous, and byte-bounded. */
35static void
36print_iface(char letter, const char *iface, const unsigned char *mask,
37 int invert)
38{
39 unsigned int i;
40
41 if (mask[0] == 0)
42 return;
Rusty Russell7e53bf92000-03-20 07:03:28 +000043
Marc Bouchere6869a82000-03-20 06:03:29 +000044 printf("-%c %s", letter, invert ? "! " : "");
45
46 for (i = 0; i < IFNAMSIZ; i++) {
47 if (mask[i] != 0) {
48 if (iface[i] != '\0')
49 printf("%c", iface[i]);
50 } else {
Harald Weltede1578f2001-05-23 23:07:33 +000051 if (iface[i] != '\0')
Marc Bouchere6869a82000-03-20 06:03:29 +000052 printf("+");
53 break;
54 }
55 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000056
57 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +000058}
59
60/* These are hardcoded backups in iptables.c, so they are safe */
61struct pprot {
62 char *name;
63 u_int8_t num;
64};
65
András Kis-Szabó764316a2001-02-26 17:31:20 +000066/* FIXME: why don't we use /etc/protocols ? */
Marc Bouchere6869a82000-03-20 06:03:29 +000067static const struct pprot chain_protos[] = {
68 { "tcp", IPPROTO_TCP },
69 { "udp", IPPROTO_UDP },
70 { "icmp", IPPROTO_ICMP },
András Kis-Szabó764316a2001-02-26 17:31:20 +000071 { "esp", IPPROTO_ESP },
72 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +000073};
74
75static void print_proto(u_int16_t proto, int invert)
76{
77 if (proto) {
78 unsigned int i;
79 const char *invertstr = invert ? "! " : "";
80
81 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
82 if (chain_protos[i].num == proto) {
83 printf("-p %s%s ",
84 invertstr, chain_protos[i].name);
85 return;
86 }
87
88 printf("-p %s%u ", invertstr, proto);
89 }
90}
91
Harald Welteae1ff9f2000-12-01 14:26:20 +000092#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +000093static int non_zero(const void *ptr, size_t size)
94{
95 unsigned int i;
96
97 for (i = 0; i < size; i++)
98 if (((char *)ptr)[i])
99 return 0;
100
101 return 1;
102}
Harald Welteae1ff9f2000-12-01 14:26:20 +0000103#endif
104
Harald Welte32db5242001-01-23 22:46:22 +0000105static int print_match(const struct ipt_entry_match *e,
106 const struct ipt_ip *ip)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000107{
108 struct iptables_match *match
109 = find_match(e->u.user.name, TRY_LOAD);
110
111 if (match) {
112 printf("-m %s ", e->u.user.name);
Harald Weltee8137792001-01-24 01:32:51 +0000113
114 /* some matches don't provide a save function */
115 if (match->save)
116 match->save(ip, e);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000117 } else {
118 if (e->u.match_size) {
119 fprintf(stderr,
120 "Can't find library for match `%s'\n",
121 e->u.user.name);
122 exit(1);
123 }
124 }
125 return 0;
126}
127
128/* print a given ip including mask if neccessary */
129static void print_ip(char *prefix, u_int32_t ip, u_int32_t mask, int invert)
130{
131 if (!mask && !ip)
132 return;
133
134 printf("%s %s%u.%u.%u.%u",
135 prefix,
136 invert ? "! " : "",
137 IP_PARTS(ip));
138
139 if (mask != 0xffffffff)
140 printf("/%u.%u.%u.%u ", IP_PARTS(mask));
141 else
142 printf(" ");
143}
Marc Bouchere6869a82000-03-20 06:03:29 +0000144
145/* We want this to be readable, so only print out neccessary fields.
146 * Because that's the kind of world I want to live in. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000147static void print_rule(const struct ipt_entry *e,
Harald Welte9f7fa492001-03-15 15:12:02 +0000148 iptc_handle_t *h, const char *chain, int counters)
Marc Bouchere6869a82000-03-20 06:03:29 +0000149{
Harald Welteae1ff9f2000-12-01 14:26:20 +0000150 struct ipt_entry_target *t;
Harald Welteace8a012001-07-05 06:29:10 +0000151 const char *target_name;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000152
153 /* print counters */
Marc Bouchere6869a82000-03-20 06:03:29 +0000154 if (counters)
Harald Welted8e65632001-01-05 15:20:07 +0000155 printf("[%llu:%llu] ", e->counters.pcnt, e->counters.bcnt);
Marc Bouchere6869a82000-03-20 06:03:29 +0000156
Harald Welte9f7fa492001-03-15 15:12:02 +0000157 /* print chain name */
158 printf("-A %s ", chain);
159
Marc Bouchere6869a82000-03-20 06:03:29 +0000160 /* Print IP part. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000161 print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr,
162 e->ip.invflags & IPT_INV_SRCIP);
163
164 print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
Harald Weltefa7625a2001-01-26 03:28:18 +0000165 e->ip.invflags & IPT_INV_DSTIP);
Marc Bouchere6869a82000-03-20 06:03:29 +0000166
167 print_iface('i', e->ip.iniface, e->ip.iniface_mask,
168 e->ip.invflags & IPT_INV_VIA_IN);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000169
Marc Bouchere6869a82000-03-20 06:03:29 +0000170 print_iface('o', e->ip.outiface, e->ip.outiface_mask,
171 e->ip.invflags & IPT_INV_VIA_OUT);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000172
Marc Bouchere6869a82000-03-20 06:03:29 +0000173 print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
174
175 if (e->ip.flags & IPT_F_FRAG)
176 printf("%s-f ",
177 e->ip.invflags & IPT_INV_FRAG ? "! " : "");
178
Marc Bouchere6869a82000-03-20 06:03:29 +0000179 /* Print matchinfo part */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000180 if (e->target_offset) {
Harald Welte32db5242001-01-23 22:46:22 +0000181 IPT_MATCH_ITERATE(e, print_match, &e->ip);
Marc Bouchere6869a82000-03-20 06:03:29 +0000182 }
183
Harald Welteae1ff9f2000-12-01 14:26:20 +0000184 /* Print target name */
Harald Welte6f83cf02001-05-24 23:22:28 +0000185 target_name = iptc_get_target(e, h);
186 if (target_name && (*target_name != '\0'))
187 printf("-j %s ", target_name);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000188
Marc Bouchere6869a82000-03-20 06:03:29 +0000189 /* Print targinfo part */
Rusty Russell082ba022001-01-07 06:54:51 +0000190 t = ipt_get_target((struct ipt_entry *)e);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000191 if (t->u.user.name[0]) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000192 struct iptables_target *target
Harald Welteae1ff9f2000-12-01 14:26:20 +0000193 = find_target(t->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000194
195 if (target)
Harald Welte32db5242001-01-23 22:46:22 +0000196 target->save(&e->ip, t);
Marc Bouchere6869a82000-03-20 06:03:29 +0000197 else {
198 /* If some bits are non-zero, it implies we *need*
199 to understand it */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000200 if (t->u.target_size) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000201 fprintf(stderr,
202 "Can't find library for target `%s'\n",
Harald Welteae1ff9f2000-12-01 14:26:20 +0000203 t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000204 exit(1);
205 }
206 }
207 }
208 printf("\n");
209}
210
211/* Debugging prototype. */
Rusty Russella8f033e2000-07-30 01:43:01 +0000212static int for_each_table(int (*func)(const char *tablename))
213{
214 int ret = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000215 FILE *procfile = NULL;
Rusty Russella8f033e2000-07-30 01:43:01 +0000216 char tablename[IPT_TABLE_MAXNAMELEN+1];
217
Harald Welteae1ff9f2000-12-01 14:26:20 +0000218 procfile = fopen("/proc/net/ip_tables_names", "r");
Rusty Russella8f033e2000-07-30 01:43:01 +0000219 if (!procfile)
220 return 0;
221
222 while (fgets(tablename, sizeof(tablename), procfile)) {
223 if (tablename[strlen(tablename) - 1] != '\n')
224 exit_error(OTHER_PROBLEM,
225 "Badly formed tablename `%s'\n",
226 tablename);
227 tablename[strlen(tablename) - 1] = '\0';
228 ret &= func(tablename);
229 }
230
231 return ret;
232}
233
234
Rusty Russella8f033e2000-07-30 01:43:01 +0000235static int do_output(const char *tablename)
236{
237 iptc_handle_t h;
238 const char *chain = NULL;
239
240 if (!tablename)
241 return for_each_table(&do_output);
242
243 h = iptc_init(tablename);
244 if (!h)
245 exit_error(OTHER_PROBLEM, "Can't initialize: %s\n",
246 iptc_strerror(errno));
247
248 if (!binary) {
249 time_t now = time(NULL);
250
251 printf("# Generated by iptables-save v%s on %s",
252 NETFILTER_VERSION, ctime(&now));
Harald Welteae1ff9f2000-12-01 14:26:20 +0000253 printf("*%s\n", tablename);
Rusty Russella8f033e2000-07-30 01:43:01 +0000254
Harald Welte9f7fa492001-03-15 15:12:02 +0000255 /* Dump out chain names first,
256 * thereby preventing dependency conflicts */
Rusty Russella8f033e2000-07-30 01:43:01 +0000257 for (chain = iptc_first_chain(&h);
258 chain;
259 chain = iptc_next_chain(&h)) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000260
Rusty Russella8f033e2000-07-30 01:43:01 +0000261 printf(":%s ", chain);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000262 if (iptc_builtin(chain, h)) {
Rusty Russella8f033e2000-07-30 01:43:01 +0000263 struct ipt_counters count;
264 printf("%s ",
265 iptc_get_policy(chain, &count, &h));
Harald Welted8e65632001-01-05 15:20:07 +0000266 printf("[%llu:%llu]\n", count.pcnt, count.bcnt);
Rusty Russella8f033e2000-07-30 01:43:01 +0000267 } else {
Harald Welted8e65632001-01-05 15:20:07 +0000268 printf("- [0:0]\n");
Rusty Russella8f033e2000-07-30 01:43:01 +0000269 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000270 }
271
272
273 for (chain = iptc_first_chain(&h);
274 chain;
275 chain = iptc_next_chain(&h)) {
276 const struct ipt_entry *e;
Rusty Russella8f033e2000-07-30 01:43:01 +0000277
Harald Welteae1ff9f2000-12-01 14:26:20 +0000278 /* Dump out rules */
279 e = iptc_first_rule(chain, &h);
280 while(e) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000281 print_rule(e, &h, chain, counters);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000282 e = iptc_next_rule(e, &h);
Rusty Russella8f033e2000-07-30 01:43:01 +0000283 }
284 }
285
286 now = time(NULL);
287 printf("COMMIT\n");
288 printf("# Completed on %s", ctime(&now));
289 } else {
290 /* Binary, huh? OK. */
291 exit_error(OTHER_PROBLEM, "Binary NYI\n");
292 }
293
294 return 1;
295}
296
Marc Bouchere6869a82000-03-20 06:03:29 +0000297/* Format:
298 * :Chain name POLICY packets bytes
299 * rule
300 */
301int main(int argc, char *argv[])
302{
Rusty Russella8f033e2000-07-30 01:43:01 +0000303 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000304 int c;
Marc Bouchere6869a82000-03-20 06:03:29 +0000305
306 program_name = "iptables-save";
307 program_version = NETFILTER_VERSION;
308
Harald Welte3efb6ea2001-08-06 18:50:21 +0000309#ifdef NO_SHARED_LIBS
310 init_extensions();
311#endif
312
Marc Bouchere6869a82000-03-20 06:03:29 +0000313 while ((c = getopt_long(argc, argv, "bc", options, NULL)) != -1) {
314 switch (c) {
315 case 'b':
316 binary = 1;
317 break;
318
319 case 'c':
320 counters = 1;
321 break;
322
Rusty Russella8f033e2000-07-30 01:43:01 +0000323 case 't':
324 /* Select specific table. */
325 tablename = optarg;
326 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000327 case 'd':
Harald Welteae1ff9f2000-12-01 14:26:20 +0000328 do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000329 exit(0);
330 }
331 }
332
333 if (optind < argc) {
334 fprintf(stderr, "Unknown arguments found on commandline");
335 exit(1);
336 }
337
Rusty Russella8f033e2000-07-30 01:43:01 +0000338 return !do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000339}