blob: 422a8d2bb91565f8c01f3b794451870a55b87146 [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>
Rusty Russellb1f69be2000-08-27 07:42:54 +000016#include "libiptc/libiptc.h"
17#include "iptables.h"
Marc Bouchere6869a82000-03-20 06:03:29 +000018
Rusty Russella8f033e2000-07-30 01:43:01 +000019static int binary = 0, counters = 0;
20
Marc Bouchere6869a82000-03-20 06:03:29 +000021static struct option options[] = {
Rusty Russella8f033e2000-07-30 01:43:01 +000022 { "binary", 0, 0, 'b' },
23 { "counters", 0, 0, 'c' },
24 { "dump", 0, 0, 'd' },
25 { "table", 1, 0, 't' },
Marc Bouchere6869a82000-03-20 06:03:29 +000026 { 0 }
27};
28
29#define IP_PARTS_NATIVE(n) \
30(unsigned int)((n)>>24)&0xFF, \
31(unsigned int)((n)>>16)&0xFF, \
32(unsigned int)((n)>>8)&0xFF, \
33(unsigned int)((n)&0xFF)
34
35#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
36
37/* This assumes that mask is contiguous, and byte-bounded. */
38static void
39print_iface(char letter, const char *iface, const unsigned char *mask,
40 int invert)
41{
42 unsigned int i;
43
44 if (mask[0] == 0)
45 return;
Rusty Russell7e53bf92000-03-20 07:03:28 +000046
Marc Bouchere6869a82000-03-20 06:03:29 +000047 printf("-%c %s", letter, invert ? "! " : "");
48
49 for (i = 0; i < IFNAMSIZ; i++) {
50 if (mask[i] != 0) {
51 if (iface[i] != '\0')
52 printf("%c", iface[i]);
53 } else {
Harald Welte9535e682001-11-08 22:28:23 +000054 /* we can access iface[i-1] here, because
55 * a few lines above we make sure that mask[0] != 0 */
56 if (iface[i-1] != '\0')
Marc Bouchere6869a82000-03-20 06:03:29 +000057 printf("+");
58 break;
59 }
60 }
Harald Welteae1ff9f2000-12-01 14:26:20 +000061
62 printf(" ");
Marc Bouchere6869a82000-03-20 06:03:29 +000063}
64
65/* These are hardcoded backups in iptables.c, so they are safe */
66struct pprot {
67 char *name;
68 u_int8_t num;
69};
70
András Kis-Szabó764316a2001-02-26 17:31:20 +000071/* FIXME: why don't we use /etc/protocols ? */
Marc Bouchere6869a82000-03-20 06:03:29 +000072static const struct pprot chain_protos[] = {
73 { "tcp", IPPROTO_TCP },
74 { "udp", IPPROTO_UDP },
75 { "icmp", IPPROTO_ICMP },
András Kis-Szabó764316a2001-02-26 17:31:20 +000076 { "esp", IPPROTO_ESP },
77 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +000078 { "sctp", IPPROTO_SCTP },
Marc Bouchere6869a82000-03-20 06:03:29 +000079};
80
81static void print_proto(u_int16_t proto, int invert)
82{
83 if (proto) {
84 unsigned int i;
85 const char *invertstr = invert ? "! " : "";
86
Pedro Lamarão0e3b3372004-04-07 09:33:17 +000087 struct protoent *pent = getprotobynumber(proto);
88 if (pent) {
89 printf("-p %s%s ", invertstr, pent->p_name);
90 return;
91 }
92
Marc Bouchere6869a82000-03-20 06:03:29 +000093 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
94 if (chain_protos[i].num == proto) {
95 printf("-p %s%s ",
96 invertstr, chain_protos[i].name);
97 return;
98 }
99
100 printf("-p %s%u ", invertstr, proto);
101 }
102}
103
Harald Welteae1ff9f2000-12-01 14:26:20 +0000104#if 0
Marc Bouchere6869a82000-03-20 06:03:29 +0000105static int non_zero(const void *ptr, size_t size)
106{
107 unsigned int i;
108
109 for (i = 0; i < size; i++)
110 if (((char *)ptr)[i])
111 return 0;
112
113 return 1;
114}
Harald Welteae1ff9f2000-12-01 14:26:20 +0000115#endif
116
Harald Welte32db5242001-01-23 22:46:22 +0000117static int print_match(const struct ipt_entry_match *e,
118 const struct ipt_ip *ip)
Harald Welteae1ff9f2000-12-01 14:26:20 +0000119{
120 struct iptables_match *match
Martin Josefsson78cafda2004-02-02 20:01:18 +0000121 = find_match(e->u.user.name, TRY_LOAD, NULL);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000122
123 if (match) {
124 printf("-m %s ", e->u.user.name);
Harald Weltee8137792001-01-24 01:32:51 +0000125
126 /* some matches don't provide a save function */
127 if (match->save)
128 match->save(ip, e);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000129 } else {
130 if (e->u.match_size) {
131 fprintf(stderr,
132 "Can't find library for match `%s'\n",
133 e->u.user.name);
134 exit(1);
135 }
136 }
137 return 0;
138}
139
140/* print a given ip including mask if neccessary */
141static void print_ip(char *prefix, u_int32_t ip, u_int32_t mask, int invert)
142{
143 if (!mask && !ip)
144 return;
145
146 printf("%s %s%u.%u.%u.%u",
147 prefix,
148 invert ? "! " : "",
149 IP_PARTS(ip));
150
151 if (mask != 0xffffffff)
152 printf("/%u.%u.%u.%u ", IP_PARTS(mask));
153 else
154 printf(" ");
155}
Marc Bouchere6869a82000-03-20 06:03:29 +0000156
157/* We want this to be readable, so only print out neccessary fields.
158 * Because that's the kind of world I want to live in. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000159static void print_rule(const struct ipt_entry *e,
Harald Welte9f7fa492001-03-15 15:12:02 +0000160 iptc_handle_t *h, const char *chain, int counters)
Marc Bouchere6869a82000-03-20 06:03:29 +0000161{
Harald Welteae1ff9f2000-12-01 14:26:20 +0000162 struct ipt_entry_target *t;
Harald Welteace8a012001-07-05 06:29:10 +0000163 const char *target_name;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000164
165 /* print counters */
Marc Bouchere6869a82000-03-20 06:03:29 +0000166 if (counters)
Harald Welted8e65632001-01-05 15:20:07 +0000167 printf("[%llu:%llu] ", e->counters.pcnt, e->counters.bcnt);
Marc Bouchere6869a82000-03-20 06:03:29 +0000168
Harald Welte9f7fa492001-03-15 15:12:02 +0000169 /* print chain name */
170 printf("-A %s ", chain);
171
Marc Bouchere6869a82000-03-20 06:03:29 +0000172 /* Print IP part. */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000173 print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr,
174 e->ip.invflags & IPT_INV_SRCIP);
175
176 print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
Harald Weltefa7625a2001-01-26 03:28:18 +0000177 e->ip.invflags & IPT_INV_DSTIP);
Marc Bouchere6869a82000-03-20 06:03:29 +0000178
179 print_iface('i', e->ip.iniface, e->ip.iniface_mask,
180 e->ip.invflags & IPT_INV_VIA_IN);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000181
Marc Bouchere6869a82000-03-20 06:03:29 +0000182 print_iface('o', e->ip.outiface, e->ip.outiface_mask,
183 e->ip.invflags & IPT_INV_VIA_OUT);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000184
Marc Bouchere6869a82000-03-20 06:03:29 +0000185 print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
186
187 if (e->ip.flags & IPT_F_FRAG)
188 printf("%s-f ",
189 e->ip.invflags & IPT_INV_FRAG ? "! " : "");
190
Marc Bouchere6869a82000-03-20 06:03:29 +0000191 /* Print matchinfo part */
Harald Welteae1ff9f2000-12-01 14:26:20 +0000192 if (e->target_offset) {
Harald Welte32db5242001-01-23 22:46:22 +0000193 IPT_MATCH_ITERATE(e, print_match, &e->ip);
Marc Bouchere6869a82000-03-20 06:03:29 +0000194 }
195
Harald Welteae1ff9f2000-12-01 14:26:20 +0000196 /* Print target name */
Harald Welte6f83cf02001-05-24 23:22:28 +0000197 target_name = iptc_get_target(e, h);
198 if (target_name && (*target_name != '\0'))
199 printf("-j %s ", target_name);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000200
Marc Bouchere6869a82000-03-20 06:03:29 +0000201 /* Print targinfo part */
Rusty Russell082ba022001-01-07 06:54:51 +0000202 t = ipt_get_target((struct ipt_entry *)e);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000203 if (t->u.user.name[0]) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000204 struct iptables_target *target
Harald Welteae1ff9f2000-12-01 14:26:20 +0000205 = find_target(t->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000206
Harald Welte31098ad2001-10-16 09:40:13 +0000207 if (!target) {
208 fprintf(stderr, "Can't find library for target `%s'\n",
209 t->u.user.name);
210 exit(1);
211 }
212
213 if (target->save)
Harald Welte32db5242001-01-23 22:46:22 +0000214 target->save(&e->ip, t);
Marc Bouchere6869a82000-03-20 06:03:29 +0000215 else {
Harald Welte31098ad2001-10-16 09:40:13 +0000216 /* If the target size is greater than ipt_entry_target
217 * there is something to be saved, we just don't know
218 * how to print it */
219 if (t->u.target_size !=
220 sizeof(struct ipt_entry_target)) {
221 fprintf(stderr, "Target `%s' is missing "
222 "save function\n",
Harald Welteae1ff9f2000-12-01 14:26:20 +0000223 t->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000224 exit(1);
225 }
226 }
227 }
228 printf("\n");
229}
230
231/* Debugging prototype. */
Rusty Russella8f033e2000-07-30 01:43:01 +0000232static int for_each_table(int (*func)(const char *tablename))
233{
234 int ret = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +0000235 FILE *procfile = NULL;
Rusty Russella8f033e2000-07-30 01:43:01 +0000236 char tablename[IPT_TABLE_MAXNAMELEN+1];
237
Harald Welteae1ff9f2000-12-01 14:26:20 +0000238 procfile = fopen("/proc/net/ip_tables_names", "r");
Rusty Russella8f033e2000-07-30 01:43:01 +0000239 if (!procfile)
240 return 0;
241
242 while (fgets(tablename, sizeof(tablename), procfile)) {
243 if (tablename[strlen(tablename) - 1] != '\n')
244 exit_error(OTHER_PROBLEM,
245 "Badly formed tablename `%s'\n",
246 tablename);
247 tablename[strlen(tablename) - 1] = '\0';
248 ret &= func(tablename);
249 }
250
251 return ret;
252}
253
254
Rusty Russella8f033e2000-07-30 01:43:01 +0000255static int do_output(const char *tablename)
256{
257 iptc_handle_t h;
258 const char *chain = NULL;
259
260 if (!tablename)
261 return for_each_table(&do_output);
262
263 h = iptc_init(tablename);
264 if (!h)
265 exit_error(OTHER_PROBLEM, "Can't initialize: %s\n",
266 iptc_strerror(errno));
267
268 if (!binary) {
269 time_t now = time(NULL);
270
271 printf("# Generated by iptables-save v%s on %s",
Harald Welte80fe35d2002-05-29 13:08:15 +0000272 IPTABLES_VERSION, ctime(&now));
Harald Welteae1ff9f2000-12-01 14:26:20 +0000273 printf("*%s\n", tablename);
Rusty Russella8f033e2000-07-30 01:43:01 +0000274
Harald Welte9f7fa492001-03-15 15:12:02 +0000275 /* Dump out chain names first,
276 * thereby preventing dependency conflicts */
Rusty Russella8f033e2000-07-30 01:43:01 +0000277 for (chain = iptc_first_chain(&h);
278 chain;
279 chain = iptc_next_chain(&h)) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000280
Rusty Russella8f033e2000-07-30 01:43:01 +0000281 printf(":%s ", chain);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000282 if (iptc_builtin(chain, h)) {
Rusty Russella8f033e2000-07-30 01:43:01 +0000283 struct ipt_counters count;
284 printf("%s ",
285 iptc_get_policy(chain, &count, &h));
Harald Welted8e65632001-01-05 15:20:07 +0000286 printf("[%llu:%llu]\n", count.pcnt, count.bcnt);
Rusty Russella8f033e2000-07-30 01:43:01 +0000287 } else {
Harald Welted8e65632001-01-05 15:20:07 +0000288 printf("- [0:0]\n");
Rusty Russella8f033e2000-07-30 01:43:01 +0000289 }
Harald Welte9f7fa492001-03-15 15:12:02 +0000290 }
291
292
293 for (chain = iptc_first_chain(&h);
294 chain;
295 chain = iptc_next_chain(&h)) {
296 const struct ipt_entry *e;
Rusty Russella8f033e2000-07-30 01:43:01 +0000297
Harald Welteae1ff9f2000-12-01 14:26:20 +0000298 /* Dump out rules */
299 e = iptc_first_rule(chain, &h);
300 while(e) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000301 print_rule(e, &h, chain, counters);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000302 e = iptc_next_rule(e, &h);
Rusty Russella8f033e2000-07-30 01:43:01 +0000303 }
304 }
305
306 now = time(NULL);
307 printf("COMMIT\n");
308 printf("# Completed on %s", ctime(&now));
309 } else {
310 /* Binary, huh? OK. */
311 exit_error(OTHER_PROBLEM, "Binary NYI\n");
312 }
313
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000314 iptc_free(&h);
315
Rusty Russella8f033e2000-07-30 01:43:01 +0000316 return 1;
317}
318
Marc Bouchere6869a82000-03-20 06:03:29 +0000319/* Format:
320 * :Chain name POLICY packets bytes
321 * rule
322 */
323int main(int argc, char *argv[])
324{
Rusty Russella8f033e2000-07-30 01:43:01 +0000325 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000326 int c;
Marc Bouchere6869a82000-03-20 06:03:29 +0000327
328 program_name = "iptables-save";
Harald Welte80fe35d2002-05-29 13:08:15 +0000329 program_version = IPTABLES_VERSION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000330
Harald Welte3efb6ea2001-08-06 18:50:21 +0000331#ifdef NO_SHARED_LIBS
332 init_extensions();
333#endif
334
Marc Boucher163ad782001-12-06 15:05:48 +0000335 while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000336 switch (c) {
337 case 'b':
338 binary = 1;
339 break;
340
341 case 'c':
342 counters = 1;
343 break;
344
Rusty Russella8f033e2000-07-30 01:43:01 +0000345 case 't':
346 /* Select specific table. */
347 tablename = optarg;
348 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000349 case 'd':
Harald Welteae1ff9f2000-12-01 14:26:20 +0000350 do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000351 exit(0);
352 }
353 }
354
355 if (optind < argc) {
356 fprintf(stderr, "Unknown arguments found on commandline");
357 exit(1);
358 }
359
Rusty Russella8f033e2000-07-30 01:43:01 +0000360 return !do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000361}