blob: 282ca7f8890b721270f7ea80e07b5545db3a15b1 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to save the iptables state, in human readable-form. */
Harald Welteae1ff9f2000-12-01 14:26:20 +00002/* Authors: Paul 'Rusty' Russel <rusty@linuxcare.com.au> and
3 * 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 {
51 if (iface[i] != '\0')
52 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;
151
152 /* print counters */
Marc Bouchere6869a82000-03-20 06:03:29 +0000153 if (counters)
Harald Welted8e65632001-01-05 15:20:07 +0000154 printf("[%llu:%llu] ", e->counters.pcnt, e->counters.bcnt);
Marc Bouchere6869a82000-03-20 06:03:29 +0000155
Harald Welte9f7fa492001-03-15 15:12:02 +0000156 /* print chain name */
157 printf("-A %s ", chain);
158
Marc Bouchere6869a82000-03-20 06:03:29 +0000159 /* Print IP part. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000160 print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr,
161 e->ip.invflags & IPT_INV_SRCIP);
162
163 print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
Harald Weltefa7625a2001-01-26 03:28:18 +0000164 e->ip.invflags & IPT_INV_DSTIP);
Marc Bouchere6869a82000-03-20 06:03:29 +0000165
166 print_iface('i', e->ip.iniface, e->ip.iniface_mask,
167 e->ip.invflags & IPT_INV_VIA_IN);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000168
Marc Bouchere6869a82000-03-20 06:03:29 +0000169 print_iface('o', e->ip.outiface, e->ip.outiface_mask,
170 e->ip.invflags & IPT_INV_VIA_OUT);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000171
Marc Bouchere6869a82000-03-20 06:03:29 +0000172 print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
173
174 if (e->ip.flags & IPT_F_FRAG)
175 printf("%s-f ",
176 e->ip.invflags & IPT_INV_FRAG ? "! " : "");
177
Marc Bouchere6869a82000-03-20 06:03:29 +0000178 /* Print matchinfo part */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000179 if (e->target_offset) {
Harald Welte32db5242001-01-23 22:46:22 +0000180 IPT_MATCH_ITERATE(e, print_match, &e->ip);
Marc Bouchere6869a82000-03-20 06:03:29 +0000181 }
182
Harald Welteae1ff9f2000-12-01 14:26:20 +0000183 /* Print target name */
184 printf("-j %s ", iptc_get_target(e, h));
185
Marc Bouchere6869a82000-03-20 06:03:29 +0000186 /* Print targinfo part */
Rusty Russell082ba022001-01-07 06:54:51 +0000187 t = ipt_get_target((struct ipt_entry *)e);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000188 if (t->u.user.name[0]) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000189 struct iptables_target *target
Harald Welteae1ff9f2000-12-01 14:26:20 +0000190 = find_target(t->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000191
192 if (target)
Harald Welte32db5242001-01-23 22:46:22 +0000193 target->save(&e->ip, t);
Marc Bouchere6869a82000-03-20 06:03:29 +0000194 else {
195 /* If some bits are non-zero, it implies we *need*
196 to understand it */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000197 if (t->u.target_size) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000198 fprintf(stderr,
199 "Can't find library for target `%s'\n",
Harald Welteae1ff9f2000-12-01 14:26:20 +0000200 t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000201 exit(1);
202 }
203 }
204 }
205 printf("\n");
206}
207
208/* Debugging prototype. */
Rusty Russella8f033e2000-07-30 01:43:01 +0000209static int for_each_table(int (*func)(const char *tablename))
210{
211 int ret = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000212 FILE *procfile = NULL;
Rusty Russella8f033e2000-07-30 01:43:01 +0000213 char tablename[IPT_TABLE_MAXNAMELEN+1];
214
Harald Welteae1ff9f2000-12-01 14:26:20 +0000215 procfile = fopen("/proc/net/ip_tables_names", "r");
Rusty Russella8f033e2000-07-30 01:43:01 +0000216 if (!procfile)
217 return 0;
218
219 while (fgets(tablename, sizeof(tablename), procfile)) {
220 if (tablename[strlen(tablename) - 1] != '\n')
221 exit_error(OTHER_PROBLEM,
222 "Badly formed tablename `%s'\n",
223 tablename);
224 tablename[strlen(tablename) - 1] = '\0';
225 ret &= func(tablename);
226 }
227
228 return ret;
229}
230
231
Rusty Russella8f033e2000-07-30 01:43:01 +0000232static int do_output(const char *tablename)
233{
234 iptc_handle_t h;
235 const char *chain = NULL;
236
237 if (!tablename)
238 return for_each_table(&do_output);
239
240 h = iptc_init(tablename);
241 if (!h)
242 exit_error(OTHER_PROBLEM, "Can't initialize: %s\n",
243 iptc_strerror(errno));
244
245 if (!binary) {
246 time_t now = time(NULL);
247
248 printf("# Generated by iptables-save v%s on %s",
249 NETFILTER_VERSION, ctime(&now));
Harald Welteae1ff9f2000-12-01 14:26:20 +0000250 printf("*%s\n", tablename);
Rusty Russella8f033e2000-07-30 01:43:01 +0000251
Harald Welte9f7fa492001-03-15 15:12:02 +0000252 /* Dump out chain names first,
253 * thereby preventing dependency conflicts */
Rusty Russella8f033e2000-07-30 01:43:01 +0000254 for (chain = iptc_first_chain(&h);
255 chain;
256 chain = iptc_next_chain(&h)) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000257
Rusty Russella8f033e2000-07-30 01:43:01 +0000258 printf(":%s ", chain);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000259 if (iptc_builtin(chain, h)) {
Rusty Russella8f033e2000-07-30 01:43:01 +0000260 struct ipt_counters count;
261 printf("%s ",
262 iptc_get_policy(chain, &count, &h));
Harald Welted8e65632001-01-05 15:20:07 +0000263 printf("[%llu:%llu]\n", count.pcnt, count.bcnt);
Rusty Russella8f033e2000-07-30 01:43:01 +0000264 } else {
Harald Welted8e65632001-01-05 15:20:07 +0000265 printf("- [0:0]\n");
Rusty Russella8f033e2000-07-30 01:43:01 +0000266 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000267 }
268
269
270 for (chain = iptc_first_chain(&h);
271 chain;
272 chain = iptc_next_chain(&h)) {
273 const struct ipt_entry *e;
Rusty Russella8f033e2000-07-30 01:43:01 +0000274
Harald Welteae1ff9f2000-12-01 14:26:20 +0000275 /* Dump out rules */
276 e = iptc_first_rule(chain, &h);
277 while(e) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000278 print_rule(e, &h, chain, counters);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000279 e = iptc_next_rule(e, &h);
Rusty Russella8f033e2000-07-30 01:43:01 +0000280 }
281 }
282
283 now = time(NULL);
284 printf("COMMIT\n");
285 printf("# Completed on %s", ctime(&now));
286 } else {
287 /* Binary, huh? OK. */
288 exit_error(OTHER_PROBLEM, "Binary NYI\n");
289 }
290
291 return 1;
292}
293
Marc Bouchere6869a82000-03-20 06:03:29 +0000294/* Format:
295 * :Chain name POLICY packets bytes
296 * rule
297 */
298int main(int argc, char *argv[])
299{
Rusty Russella8f033e2000-07-30 01:43:01 +0000300 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000301 int c;
Marc Bouchere6869a82000-03-20 06:03:29 +0000302
303 program_name = "iptables-save";
304 program_version = NETFILTER_VERSION;
305
306 while ((c = getopt_long(argc, argv, "bc", options, NULL)) != -1) {
307 switch (c) {
308 case 'b':
309 binary = 1;
310 break;
311
312 case 'c':
313 counters = 1;
314 break;
315
Rusty Russella8f033e2000-07-30 01:43:01 +0000316 case 't':
317 /* Select specific table. */
318 tablename = optarg;
319 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000320 case 'd':
Harald Welteae1ff9f2000-12-01 14:26:20 +0000321 do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000322 exit(0);
323 }
324 }
325
326 if (optind < argc) {
327 fprintf(stderr, "Unknown arguments found on commandline");
328 exit(1);
329 }
330
Rusty Russella8f033e2000-07-30 01:43:01 +0000331 return !do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000332}