blob: d8b15c317f53bde90167914f626f4946ffa5d09f [file] [log] [blame]
András Kis-Szabó2f523792001-02-27 09:59:48 +00001/* Code to save the ip6tables state, in human readable-form. */
2/* Author: Andras Kis-Szabo <kisza@sch.bme.hu>
3 * Original code: iptables-save
4 * Authors: Paul 'Rusty' Russel <rusty@linuxcare.com.au> and
5 * Harald Welte <laforge@gnumonks.org>
6 */
7#include <getopt.h>
8#include <sys/errno.h>
9#include <stdio.h>
10#include <fcntl.h>
11#include <stdlib.h>
12#include <string.h>
13#include <dlfcn.h>
14#include <time.h>
15#include <netdb.h>
16#include <arpa/inet.h>
17#include "libiptc/libip6tc.h"
18#include "ip6tables.h"
19
20#ifndef IP6T_LIB_DIR
21#define IP6T_LIB_DIR "/usr/local/lib/iptables"
22#endif
23
24static int binary = 0, counters = 0;
25
26static struct option options[] = {
27 { "binary", 0, 0, 'b' },
28 { "counters", 0, 0, 'c' },
29 { "dump", 0, 0, 'd' },
30 { "table", 1, 0, 't' },
31 { 0 }
32};
33
34extern struct ip6tables_match *find_match(const char *name, enum ip6t_tryload tryload);
35extern struct ip6tables_target *find_target(const char *name, enum ip6t_tryload tryload);
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;
46
47 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 Welte6a4542a2001-05-12 04:38:31 +000054 if (iface[i] == '\0')
András Kis-Szabó2f523792001-02-27 09:59:48 +000055 printf("+");
56 break;
57 }
58 }
59
60 printf(" ");
61}
62
63/* These are hardcoded backups in ip6tables.c, so they are safe */
64struct pprot {
65 char *name;
66 u_int8_t num;
67};
68
69static const struct pprot chain_protos[] = {
70 { "tcp", IPPROTO_TCP },
71 { "udp", IPPROTO_UDP },
72 { "icmp", IPPROTO_ICMP },
73 { "esp", IPPROTO_ESP },
74 { "ah", IPPROTO_AH },
75};
76
77/* The ip6tables looks up the /etc/protocols. */
78static void print_proto(u_int16_t proto, int invert)
79{
80 if (proto) {
81 unsigned int i;
82 const char *invertstr = invert ? "! " : "";
83
84 struct protoent *pent = getprotobynumber(proto);
85 if (pent) {
86 printf("-p %s%s ",
87 invertstr, pent->p_name);
88 return;
89 }
90
91 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
92 if (chain_protos[i].num == proto) {
93 printf("-p %s%s ",
94 invertstr, chain_protos[i].name);
95 return;
96 }
97
98 printf("-p %s%u ", invertstr, proto);
99 }
100}
101
102static int print_match(const struct ip6t_entry_match *e,
103 const struct ip6t_ip6 *ip)
104{
105 struct ip6tables_match *match
106 = find_match(e->u.user.name, TRY_LOAD);
107
108 if (match) {
109 printf("-m %s ", e->u.user.name);
110
111 /* some matches don't provide a save function */
112 if (match->save)
113 match->save(ip, e);
114 } else {
115 if (e->u.match_size) {
116 fprintf(stderr,
117 "Can't find library for match `%s'\n",
118 e->u.user.name);
119 exit(1);
120 }
121 }
122 return 0;
123}
124
125/* print a given ip including mask if neccessary */
126static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
127{
128 char buf[51];
129 int l = ipv6_prefix_length(mask);
130
131 if (!mask && !ip)
132 return;
133
134 printf("%s %s%s/",
135 prefix,
136 invert ? "! " : "",
137 inet_ntop(AF_INET6, ip, buf, sizeof buf));
138
139 if (l == -1)
140 printf("%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
141 else
142 printf("%d ", l);
143
144#if 0
145 if (mask != 0xffffffff)
146 printf("/%u.%u.%u.%u ", IP_PARTS(mask));
147 else
148 printf(" ");
149#endif
150}
151
152/* We want this to be readable, so only print out neccessary fields.
153 * Because that's the kind of world I want to live in. */
154static void print_rule(const struct ip6t_entry *e,
155 ip6tc_handle_t *h, int counters)
156{
157 struct ip6t_entry_target *t;
Harald Welteace8a012001-07-05 06:29:10 +0000158 const char *target_name;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000159
160 /* print counters */
161 if (counters)
162 printf("[%llu:%llu] ", e->counters.pcnt, e->counters.bcnt);
163
164 /* Print IP part. */
165 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
166 e->ipv6.invflags & IP6T_INV_SRCIP);
167
168 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
169 e->ipv6.invflags & IP6T_INV_DSTIP);
170
171 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
172 e->ipv6.invflags & IP6T_INV_VIA_IN);
173
174 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
175 e->ipv6.invflags & IP6T_INV_VIA_OUT);
176
177 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
178
179#if 0
180 // not definied in ipv6
181 // FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied?
182 if (e->ipv6.flags & IPT_F_FRAG)
183 printf("%s-f ",
184 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
185#endif
186
187 // TODO: i've got some problem with the code - under understanding ;)
188 // How can I set this?
189 if (e->ipv6.flags & IP6T_F_TOS)
190 printf("%s-? %d ",
191 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
192 e->ipv6.tos);
193
194 /* Print matchinfo part */
195 if (e->target_offset) {
196 IP6T_MATCH_ITERATE(e, print_match, &e->ipv6);
197 }
198
199 /* Print target name */
Harald Welteace8a012001-07-05 06:29:10 +0000200 target_name = ip6tc_get_target(e, h);
201 if (target_name && *target_name != '\0')
202 printf("-j %s ", ip6tc_get_target(e, h));
András Kis-Szabó2f523792001-02-27 09:59:48 +0000203
204 /* Print targinfo part */
205 t = ip6t_get_target((struct ip6t_entry *)e);
206 if (t->u.user.name[0]) {
207 struct ip6tables_target *target
208 = find_target(t->u.user.name, TRY_LOAD);
209
210 if (target)
211 target->save(&e->ipv6, t);
212 else {
213 /* If some bits are non-zero, it implies we *need*
214 to understand it */
215 if (t->u.target_size) {
216 fprintf(stderr,
217 "Can't find library for target `%s'\n",
218 t->u.user.name);
219 exit(1);
220 }
221 }
222 }
223 printf("\n");
224}
225
226/* Debugging prototype. */
227static int for_each_table(int (*func)(const char *tablename))
228{
229 int ret = 1;
230 FILE *procfile = NULL;
231 char tablename[IP6T_TABLE_MAXNAMELEN+1];
232
233 procfile = fopen("/proc/net/ip6_tables_names", "r");
234 if (!procfile)
235 return 0;
236
237 while (fgets(tablename, sizeof(tablename), procfile)) {
238 if (tablename[strlen(tablename) - 1] != '\n')
239 exit_error(OTHER_PROBLEM,
240 "Badly formed tablename `%s'\n",
241 tablename);
242 tablename[strlen(tablename) - 1] = '\0';
243 ret &= func(tablename);
244 }
245
246 return ret;
247}
248
249
250static int do_output(const char *tablename)
251{
252 ip6tc_handle_t h;
253 const char *chain = NULL;
254
255 if (!tablename)
256 return for_each_table(&do_output);
257
258 h = ip6tc_init(tablename);
259 if (!h)
260 exit_error(OTHER_PROBLEM, "Can't initialize: %s\n",
261 ip6tc_strerror(errno));
262
263 if (!binary) {
264 time_t now = time(NULL);
265
266 printf("# Generated by ip6tables-save v%s on %s",
267 NETFILTER_VERSION, ctime(&now));
268 printf("*%s\n", tablename);
269
270 /* Dump out chain names */
271 for (chain = ip6tc_first_chain(&h);
272 chain;
273 chain = ip6tc_next_chain(&h)) {
274 const struct ip6t_entry *e;
275
276 printf(":%s ", chain);
277 if (ip6tc_builtin(chain, h)) {
278 struct ip6t_counters count;
279 printf("%s ",
280 ip6tc_get_policy(chain, &count, &h));
281 printf("[%llu:%llu]\n", count.pcnt, count.bcnt);
282 } else {
283 printf("- [0:0]\n");
284 }
285
286 /* Dump out rules */
287 e = ip6tc_first_rule(chain, &h);
288 while(e) {
289 print_rule(e, &h, counters);
290 e = ip6tc_next_rule(e, &h);
291 }
292 }
293
294 now = time(NULL);
295 printf("COMMIT\n");
296 printf("# Completed on %s", ctime(&now));
297 } else {
298 /* Binary, huh? OK. */
299 exit_error(OTHER_PROBLEM, "Binary NYI\n");
300 }
301
302 return 1;
303}
304
305/* Format:
306 * :Chain name POLICY packets bytes
307 * rule
308 */
309int main(int argc, char *argv[])
310{
311 const char *tablename = NULL;
312 int c;
313
314 program_name = "ip6tables-save";
315 program_version = NETFILTER_VERSION;
316
317 while ((c = getopt_long(argc, argv, "bc", options, NULL)) != -1) {
318 switch (c) {
319 case 'b':
320 binary = 1;
321 break;
322
323 case 'c':
324 counters = 1;
325 break;
326
327 case 't':
328 /* Select specific table. */
329 tablename = optarg;
330 break;
331
332 case 'd':
333 do_output(tablename);
334 exit(0);
335 }
336 }
337
338 if (optind < argc) {
339 fprintf(stderr, "Unknown arguments found on commandline");
340 exit(1);
341 }
342
343 return !do_output(tablename);
344}