npfctl.c revision 1.12 1 1.12 rmind /* $NetBSD: npfctl.c,v 1.12 2012/06/15 23:24:08 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.8 rmind * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.2 rmind #include <sys/cdefs.h>
33 1.12 rmind __RCSID("$NetBSD: npfctl.c,v 1.12 2012/06/15 23:24:08 rmind Exp $");
34 1.2 rmind
35 1.1 rmind #include <sys/ioctl.h>
36 1.1 rmind #include <sys/stat.h>
37 1.1 rmind #include <sys/types.h>
38 1.1 rmind
39 1.1 rmind #include <stdio.h>
40 1.1 rmind #include <stdlib.h>
41 1.1 rmind #include <string.h>
42 1.1 rmind #include <err.h>
43 1.1 rmind #include <fcntl.h>
44 1.1 rmind #include <unistd.h>
45 1.1 rmind
46 1.1 rmind #include "npfctl.h"
47 1.1 rmind
48 1.8 rmind extern int yylineno, yycolumn;
49 1.8 rmind extern const char * yyfilename;
50 1.8 rmind extern int yyparse(void);
51 1.8 rmind extern void yyrestart(FILE *);
52 1.8 rmind
53 1.11 rmind enum {
54 1.11 rmind NPFCTL_START,
55 1.11 rmind NPFCTL_STOP,
56 1.11 rmind NPFCTL_RELOAD,
57 1.11 rmind NPFCTL_SHOWCONF,
58 1.11 rmind NPFCTL_FLUSH,
59 1.11 rmind NPFCTL_TABLE,
60 1.11 rmind NPFCTL_STATS,
61 1.11 rmind NPFCTL_SESSIONS_SAVE,
62 1.11 rmind NPFCTL_SESSIONS_LOAD,
63 1.11 rmind };
64 1.1 rmind
65 1.1 rmind static struct operations_s {
66 1.1 rmind const char * cmd;
67 1.1 rmind int action;
68 1.1 rmind } operations[] = {
69 1.1 rmind /* Start, stop, reload */
70 1.3 rmind { "start", NPFCTL_START },
71 1.3 rmind { "stop", NPFCTL_STOP },
72 1.3 rmind { "reload", NPFCTL_RELOAD },
73 1.11 rmind { "show", NPFCTL_SHOWCONF, },
74 1.3 rmind { "flush", NPFCTL_FLUSH },
75 1.1 rmind /* Table */
76 1.3 rmind { "table", NPFCTL_TABLE },
77 1.3 rmind /* Stats */
78 1.3 rmind { "stats", NPFCTL_STATS },
79 1.3 rmind /* Sessions */
80 1.3 rmind { "sess-save", NPFCTL_SESSIONS_SAVE },
81 1.3 rmind { "sess-load", NPFCTL_SESSIONS_LOAD },
82 1.1 rmind /* --- */
83 1.3 rmind { NULL, 0 }
84 1.1 rmind };
85 1.1 rmind
86 1.1 rmind void *
87 1.1 rmind zalloc(size_t sz)
88 1.1 rmind {
89 1.8 rmind void *p = malloc(sz);
90 1.1 rmind
91 1.1 rmind if (p == NULL) {
92 1.4 rmind err(EXIT_FAILURE, "zalloc");
93 1.1 rmind }
94 1.1 rmind memset(p, 0, sz);
95 1.1 rmind return p;
96 1.1 rmind }
97 1.1 rmind
98 1.8 rmind void *
99 1.8 rmind xrealloc(void *ptr, size_t size)
100 1.8 rmind {
101 1.8 rmind void *p = realloc(ptr, size);
102 1.8 rmind
103 1.8 rmind if (p == NULL) {
104 1.8 rmind err(EXIT_FAILURE, "xrealloc");
105 1.8 rmind }
106 1.8 rmind return p;
107 1.8 rmind }
108 1.8 rmind
109 1.1 rmind char *
110 1.1 rmind xstrdup(const char *s)
111 1.1 rmind {
112 1.8 rmind char *p = strdup(s);
113 1.8 rmind
114 1.8 rmind if (p == NULL) {
115 1.8 rmind err(EXIT_FAILURE, "xstrdup");
116 1.8 rmind }
117 1.8 rmind return p;
118 1.8 rmind }
119 1.8 rmind
120 1.8 rmind char *
121 1.8 rmind xstrndup(const char *s, size_t len)
122 1.8 rmind {
123 1.1 rmind char *p;
124 1.1 rmind
125 1.8 rmind p = strndup(s, len);
126 1.1 rmind if (p == NULL) {
127 1.8 rmind err(EXIT_FAILURE, "xstrndup");
128 1.1 rmind }
129 1.1 rmind return p;
130 1.1 rmind }
131 1.1 rmind
132 1.6 joerg __dead static void
133 1.1 rmind usage(void)
134 1.1 rmind {
135 1.1 rmind const char *progname = getprogname();
136 1.1 rmind
137 1.1 rmind fprintf(stderr,
138 1.3 rmind "usage:\t%s [ start | stop | reload | flush | stats ]\n",
139 1.3 rmind progname);
140 1.3 rmind fprintf(stderr,
141 1.3 rmind "usage:\t%s [ sess-save | sess-load ]\n",
142 1.1 rmind progname);
143 1.1 rmind fprintf(stderr,
144 1.1 rmind "\t%s table <tid> [ flush ]\n",
145 1.1 rmind progname);
146 1.1 rmind fprintf(stderr,
147 1.1 rmind "\t%s table <tid> { add | rem } <address/mask>\n",
148 1.1 rmind progname);
149 1.1 rmind
150 1.1 rmind exit(EXIT_FAILURE);
151 1.1 rmind }
152 1.1 rmind
153 1.1 rmind static void
154 1.1 rmind npfctl_parsecfg(const char *cfg)
155 1.1 rmind {
156 1.1 rmind FILE *fp;
157 1.1 rmind
158 1.1 rmind fp = fopen(cfg, "r");
159 1.1 rmind if (fp == NULL) {
160 1.4 rmind err(EXIT_FAILURE, "open '%s'", cfg);
161 1.1 rmind }
162 1.8 rmind yyrestart(fp);
163 1.8 rmind yylineno = 1;
164 1.8 rmind yycolumn = 0;
165 1.8 rmind yyfilename = cfg;
166 1.8 rmind yyparse();
167 1.8 rmind fclose(fp);
168 1.1 rmind }
169 1.1 rmind
170 1.3 rmind static int
171 1.3 rmind npfctl_print_stats(int fd)
172 1.3 rmind {
173 1.8 rmind uint64_t *st = zalloc(NPF_STATS_SIZE);
174 1.3 rmind
175 1.3 rmind if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
176 1.3 rmind err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
177 1.3 rmind }
178 1.3 rmind
179 1.3 rmind printf("Packets passed:\n\t%"PRIu64" default pass\n\t"
180 1.3 rmind "%"PRIu64 " ruleset pass\n\t%"PRIu64" session pass\n\n",
181 1.3 rmind st[NPF_STAT_PASS_DEFAULT], st[NPF_STAT_PASS_RULESET],
182 1.3 rmind st[NPF_STAT_PASS_SESSION]);
183 1.3 rmind
184 1.3 rmind printf("Packets blocked:\n\t%"PRIu64" default block\n\t"
185 1.3 rmind "%"PRIu64 " ruleset block\n\n", st[NPF_STAT_BLOCK_DEFAULT],
186 1.3 rmind st[NPF_STAT_BLOCK_RULESET]);
187 1.3 rmind
188 1.3 rmind printf("Session and NAT entries:\n\t%"PRIu64" session allocations\n\t"
189 1.3 rmind "%"PRIu64" session destructions\n\t%"PRIu64" NAT entry allocations\n\t"
190 1.3 rmind "%"PRIu64" NAT entry destructions\n\n", st[NPF_STAT_SESSION_CREATE],
191 1.3 rmind st[NPF_STAT_SESSION_DESTROY], st[NPF_STAT_NAT_CREATE],
192 1.3 rmind st[NPF_STAT_NAT_DESTROY]);
193 1.3 rmind
194 1.3 rmind printf("Invalid packet state cases:\n\t%"PRIu64" cases in total\n\t"
195 1.3 rmind "%"PRIu64" TCP case I\n\t%"PRIu64" TCP case II\n\t%"PRIu64
196 1.3 rmind " TCP case III\n\n", st[NPF_STAT_INVALID_STATE],
197 1.3 rmind st[NPF_STAT_INVALID_STATE_TCP1], st[NPF_STAT_INVALID_STATE_TCP2],
198 1.3 rmind st[NPF_STAT_INVALID_STATE_TCP3]);
199 1.3 rmind
200 1.3 rmind printf("Packet race cases:\n\t%"PRIu64" NAT association race\n\t"
201 1.4 rmind "%"PRIu64" duplicate session race\n\n", st[NPF_STAT_RACE_NAT],
202 1.3 rmind st[NPF_STAT_RACE_SESSION]);
203 1.3 rmind
204 1.4 rmind printf("Rule processing procedure cases:\n"
205 1.4 rmind "\t%"PRIu64" packets logged\n\t%"PRIu64" packets normalized\n\n",
206 1.4 rmind st[NPF_STAT_RPROC_LOG], st[NPF_STAT_RPROC_NORM]);
207 1.4 rmind
208 1.4 rmind printf("Unexpected error cases:\n\t%"PRIu64"\n", st[NPF_STAT_ERROR]);
209 1.4 rmind
210 1.3 rmind free(st);
211 1.3 rmind return 0;
212 1.3 rmind }
213 1.3 rmind
214 1.10 rmind void
215 1.10 rmind npfctl_print_error(const nl_error_t *ne)
216 1.10 rmind {
217 1.10 rmind static const char *ncode_errors[] = {
218 1.10 rmind [-NPF_ERR_OPCODE] = "invalid instruction",
219 1.10 rmind [-NPF_ERR_JUMP] = "invalid jump",
220 1.10 rmind [-NPF_ERR_REG] = "invalid register",
221 1.10 rmind [-NPF_ERR_INVAL] = "invalid argument value",
222 1.10 rmind [-NPF_ERR_RANGE] = "processing out of range"
223 1.10 rmind };
224 1.10 rmind const int nc_err = ne->ne_ncode_error;
225 1.10 rmind const char *srcfile = ne->ne_source_file;
226 1.10 rmind
227 1.10 rmind if (srcfile) {
228 1.10 rmind warnx("source %s line %d", srcfile, ne->ne_source_line);
229 1.10 rmind }
230 1.10 rmind if (nc_err) {
231 1.10 rmind warnx("n-code error (%d): %s at offset 0x%x",
232 1.10 rmind nc_err, ncode_errors[-nc_err], ne->ne_ncode_errat);
233 1.10 rmind }
234 1.10 rmind if (ne->ne_id) {
235 1.10 rmind warnx("object: %d", ne->ne_id);
236 1.10 rmind }
237 1.10 rmind }
238 1.10 rmind
239 1.1 rmind static void
240 1.1 rmind npfctl(int action, int argc, char **argv)
241 1.1 rmind {
242 1.1 rmind int fd, ret, ver, boolval;
243 1.1 rmind npf_ioctl_table_t tbl;
244 1.1 rmind char *arg;
245 1.1 rmind
246 1.1 rmind fd = open(NPF_DEV_PATH, O_RDONLY);
247 1.1 rmind if (fd == -1) {
248 1.4 rmind err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
249 1.1 rmind }
250 1.1 rmind ret = ioctl(fd, IOC_NPF_VERSION, &ver);
251 1.1 rmind if (ver != NPF_VERSION) {
252 1.4 rmind errx(EXIT_FAILURE,
253 1.4 rmind "incompatible NPF interface version (%d, kernel %d)",
254 1.4 rmind NPF_VERSION, ver);
255 1.1 rmind }
256 1.1 rmind switch (action) {
257 1.1 rmind case NPFCTL_START:
258 1.1 rmind boolval = true;
259 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
260 1.1 rmind break;
261 1.1 rmind case NPFCTL_STOP:
262 1.1 rmind boolval = false;
263 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
264 1.1 rmind break;
265 1.1 rmind case NPFCTL_RELOAD:
266 1.8 rmind npfctl_config_init(false);
267 1.1 rmind npfctl_parsecfg(argc < 3 ? NPF_CONF_PATH : argv[2]);
268 1.8 rmind ret = npfctl_config_send(fd);
269 1.1 rmind break;
270 1.11 rmind case NPFCTL_SHOWCONF:
271 1.11 rmind ret = npfctl_config_show(fd);
272 1.11 rmind break;
273 1.1 rmind case NPFCTL_FLUSH:
274 1.9 rmind ret = npf_config_flush(fd);
275 1.1 rmind break;
276 1.1 rmind case NPFCTL_TABLE:
277 1.1 rmind if (argc < 5) {
278 1.1 rmind usage();
279 1.1 rmind }
280 1.1 rmind tbl.nct_tid = atoi(argv[2]);
281 1.1 rmind if (strcmp(argv[3], "add") == 0) {
282 1.4 rmind /* Add table entry. */
283 1.1 rmind tbl.nct_action = NPF_IOCTL_TBLENT_ADD;
284 1.1 rmind arg = argv[4];
285 1.1 rmind } else if (strcmp(argv[3], "rem") == 0) {
286 1.4 rmind /* Remove entry. */
287 1.1 rmind tbl.nct_action = NPF_IOCTL_TBLENT_REM;
288 1.1 rmind arg = argv[4];
289 1.1 rmind } else {
290 1.4 rmind /* Default: lookup. */
291 1.1 rmind tbl.nct_action = 0;
292 1.1 rmind arg = argv[3];
293 1.1 rmind }
294 1.8 rmind fam_addr_mask_t *fam = npfctl_parse_cidr(arg);
295 1.8 rmind if (fam == NULL) {
296 1.1 rmind errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
297 1.1 rmind }
298 1.8 rmind memcpy(&tbl.nct_addr, &fam->fam_addr, sizeof(npf_addr_t));
299 1.8 rmind tbl.nct_mask = fam->fam_mask;
300 1.1 rmind ret = ioctl(fd, IOC_NPF_TABLE, &tbl);
301 1.8 rmind if (tbl.nct_action == 0) {
302 1.8 rmind printf("%s\n", ret ? "not found" : "found");
303 1.8 rmind exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
304 1.8 rmind }
305 1.1 rmind break;
306 1.3 rmind case NPFCTL_STATS:
307 1.3 rmind ret = npfctl_print_stats(fd);
308 1.3 rmind break;
309 1.3 rmind case NPFCTL_SESSIONS_SAVE:
310 1.5 rmind ret = npf_sessions_recv(fd, NPF_SESSDB_PATH);
311 1.5 rmind if (ret) {
312 1.5 rmind errx(EXIT_FAILURE, "could not save sessions to '%s'",
313 1.5 rmind NPF_SESSDB_PATH);
314 1.5 rmind }
315 1.3 rmind break;
316 1.3 rmind case NPFCTL_SESSIONS_LOAD:
317 1.5 rmind ret = npf_sessions_send(fd, NPF_SESSDB_PATH);
318 1.5 rmind if (ret) {
319 1.5 rmind errx(EXIT_FAILURE, "no sessions loaded from '%s'",
320 1.5 rmind NPF_SESSDB_PATH);
321 1.5 rmind }
322 1.3 rmind break;
323 1.1 rmind }
324 1.7 zoltan if (ret) {
325 1.1 rmind err(EXIT_FAILURE, "ioctl");
326 1.1 rmind }
327 1.1 rmind close(fd);
328 1.1 rmind }
329 1.1 rmind
330 1.1 rmind int
331 1.1 rmind main(int argc, char **argv)
332 1.1 rmind {
333 1.1 rmind char *cmd;
334 1.1 rmind
335 1.1 rmind if (argc < 2) {
336 1.1 rmind usage();
337 1.1 rmind }
338 1.1 rmind cmd = argv[1];
339 1.1 rmind
340 1.8 rmind if (strcmp(cmd, "debug") == 0) {
341 1.12 rmind const char *cfg = argc > 2 ? argv[2] : "/tmp/npf.conf";
342 1.8 rmind npfctl_config_init(true);
343 1.8 rmind npfctl_parsecfg(cfg);
344 1.8 rmind npfctl_config_send(0);
345 1.8 rmind return EXIT_SUCCESS;
346 1.8 rmind }
347 1.4 rmind
348 1.12 rmind /* Find and call the subroutine. */
349 1.8 rmind for (int n = 0; operations[n].cmd != NULL; n++) {
350 1.1 rmind if (strcmp(cmd, operations[n].cmd) != 0)
351 1.1 rmind continue;
352 1.1 rmind npfctl(operations[n].action, argc, argv);
353 1.8 rmind return EXIT_SUCCESS;
354 1.1 rmind }
355 1.3 rmind usage();
356 1.1 rmind }
357