npfctl.c revision 1.10 1 1.10 rmind /* $NetBSD: npfctl.c,v 1.10 2012/02/05 00:37:13 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.10 rmind __RCSID("$NetBSD: npfctl.c,v 1.10 2012/02/05 00:37:13 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.1 rmind #define NPFCTL_START 1
54 1.1 rmind #define NPFCTL_STOP 2
55 1.1 rmind #define NPFCTL_RELOAD 3
56 1.1 rmind #define NPFCTL_FLUSH 4
57 1.1 rmind #define NPFCTL_TABLE 5
58 1.3 rmind #define NPFCTL_STATS 6
59 1.3 rmind #define NPFCTL_SESSIONS_SAVE 7
60 1.3 rmind #define NPFCTL_SESSIONS_LOAD 8
61 1.1 rmind
62 1.1 rmind static struct operations_s {
63 1.1 rmind const char * cmd;
64 1.1 rmind int action;
65 1.1 rmind } operations[] = {
66 1.1 rmind /* Start, stop, reload */
67 1.3 rmind { "start", NPFCTL_START },
68 1.3 rmind { "stop", NPFCTL_STOP },
69 1.3 rmind { "reload", NPFCTL_RELOAD },
70 1.3 rmind { "flush", NPFCTL_FLUSH },
71 1.1 rmind /* Table */
72 1.3 rmind { "table", NPFCTL_TABLE },
73 1.3 rmind /* Stats */
74 1.3 rmind { "stats", NPFCTL_STATS },
75 1.3 rmind /* Sessions */
76 1.3 rmind { "sess-save", NPFCTL_SESSIONS_SAVE },
77 1.3 rmind { "sess-load", NPFCTL_SESSIONS_LOAD },
78 1.1 rmind /* --- */
79 1.3 rmind { NULL, 0 }
80 1.1 rmind };
81 1.1 rmind
82 1.1 rmind void *
83 1.1 rmind zalloc(size_t sz)
84 1.1 rmind {
85 1.8 rmind void *p = malloc(sz);
86 1.1 rmind
87 1.1 rmind if (p == NULL) {
88 1.4 rmind err(EXIT_FAILURE, "zalloc");
89 1.1 rmind }
90 1.1 rmind memset(p, 0, sz);
91 1.1 rmind return p;
92 1.1 rmind }
93 1.1 rmind
94 1.8 rmind void *
95 1.8 rmind xrealloc(void *ptr, size_t size)
96 1.8 rmind {
97 1.8 rmind void *p = realloc(ptr, size);
98 1.8 rmind
99 1.8 rmind if (p == NULL) {
100 1.8 rmind err(EXIT_FAILURE, "xrealloc");
101 1.8 rmind }
102 1.8 rmind return p;
103 1.8 rmind }
104 1.8 rmind
105 1.1 rmind char *
106 1.1 rmind xstrdup(const char *s)
107 1.1 rmind {
108 1.8 rmind char *p = strdup(s);
109 1.8 rmind
110 1.8 rmind if (p == NULL) {
111 1.8 rmind err(EXIT_FAILURE, "xstrdup");
112 1.8 rmind }
113 1.8 rmind return p;
114 1.8 rmind }
115 1.8 rmind
116 1.8 rmind char *
117 1.8 rmind xstrndup(const char *s, size_t len)
118 1.8 rmind {
119 1.1 rmind char *p;
120 1.1 rmind
121 1.8 rmind p = strndup(s, len);
122 1.1 rmind if (p == NULL) {
123 1.8 rmind err(EXIT_FAILURE, "xstrndup");
124 1.1 rmind }
125 1.1 rmind return p;
126 1.1 rmind }
127 1.1 rmind
128 1.6 joerg __dead static void
129 1.1 rmind usage(void)
130 1.1 rmind {
131 1.1 rmind const char *progname = getprogname();
132 1.1 rmind
133 1.1 rmind fprintf(stderr,
134 1.3 rmind "usage:\t%s [ start | stop | reload | flush | stats ]\n",
135 1.3 rmind progname);
136 1.3 rmind fprintf(stderr,
137 1.3 rmind "usage:\t%s [ sess-save | sess-load ]\n",
138 1.1 rmind progname);
139 1.1 rmind fprintf(stderr,
140 1.1 rmind "\t%s table <tid> [ flush ]\n",
141 1.1 rmind progname);
142 1.1 rmind fprintf(stderr,
143 1.1 rmind "\t%s table <tid> { add | rem } <address/mask>\n",
144 1.1 rmind progname);
145 1.1 rmind
146 1.1 rmind exit(EXIT_FAILURE);
147 1.1 rmind }
148 1.1 rmind
149 1.1 rmind static void
150 1.1 rmind npfctl_parsecfg(const char *cfg)
151 1.1 rmind {
152 1.1 rmind FILE *fp;
153 1.1 rmind
154 1.1 rmind fp = fopen(cfg, "r");
155 1.1 rmind if (fp == NULL) {
156 1.4 rmind err(EXIT_FAILURE, "open '%s'", cfg);
157 1.1 rmind }
158 1.8 rmind yyrestart(fp);
159 1.8 rmind yylineno = 1;
160 1.8 rmind yycolumn = 0;
161 1.8 rmind yyfilename = cfg;
162 1.8 rmind yyparse();
163 1.8 rmind fclose(fp);
164 1.1 rmind }
165 1.1 rmind
166 1.3 rmind static int
167 1.3 rmind npfctl_print_stats(int fd)
168 1.3 rmind {
169 1.8 rmind uint64_t *st = zalloc(NPF_STATS_SIZE);
170 1.3 rmind
171 1.3 rmind if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
172 1.3 rmind err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
173 1.3 rmind }
174 1.3 rmind
175 1.3 rmind printf("Packets passed:\n\t%"PRIu64" default pass\n\t"
176 1.3 rmind "%"PRIu64 " ruleset pass\n\t%"PRIu64" session pass\n\n",
177 1.3 rmind st[NPF_STAT_PASS_DEFAULT], st[NPF_STAT_PASS_RULESET],
178 1.3 rmind st[NPF_STAT_PASS_SESSION]);
179 1.3 rmind
180 1.3 rmind printf("Packets blocked:\n\t%"PRIu64" default block\n\t"
181 1.3 rmind "%"PRIu64 " ruleset block\n\n", st[NPF_STAT_BLOCK_DEFAULT],
182 1.3 rmind st[NPF_STAT_BLOCK_RULESET]);
183 1.3 rmind
184 1.3 rmind printf("Session and NAT entries:\n\t%"PRIu64" session allocations\n\t"
185 1.3 rmind "%"PRIu64" session destructions\n\t%"PRIu64" NAT entry allocations\n\t"
186 1.3 rmind "%"PRIu64" NAT entry destructions\n\n", st[NPF_STAT_SESSION_CREATE],
187 1.3 rmind st[NPF_STAT_SESSION_DESTROY], st[NPF_STAT_NAT_CREATE],
188 1.3 rmind st[NPF_STAT_NAT_DESTROY]);
189 1.3 rmind
190 1.3 rmind printf("Invalid packet state cases:\n\t%"PRIu64" cases in total\n\t"
191 1.3 rmind "%"PRIu64" TCP case I\n\t%"PRIu64" TCP case II\n\t%"PRIu64
192 1.3 rmind " TCP case III\n\n", st[NPF_STAT_INVALID_STATE],
193 1.3 rmind st[NPF_STAT_INVALID_STATE_TCP1], st[NPF_STAT_INVALID_STATE_TCP2],
194 1.3 rmind st[NPF_STAT_INVALID_STATE_TCP3]);
195 1.3 rmind
196 1.3 rmind printf("Packet race cases:\n\t%"PRIu64" NAT association race\n\t"
197 1.4 rmind "%"PRIu64" duplicate session race\n\n", st[NPF_STAT_RACE_NAT],
198 1.3 rmind st[NPF_STAT_RACE_SESSION]);
199 1.3 rmind
200 1.4 rmind printf("Rule processing procedure cases:\n"
201 1.4 rmind "\t%"PRIu64" packets logged\n\t%"PRIu64" packets normalized\n\n",
202 1.4 rmind st[NPF_STAT_RPROC_LOG], st[NPF_STAT_RPROC_NORM]);
203 1.4 rmind
204 1.4 rmind printf("Unexpected error cases:\n\t%"PRIu64"\n", st[NPF_STAT_ERROR]);
205 1.4 rmind
206 1.3 rmind free(st);
207 1.3 rmind return 0;
208 1.3 rmind }
209 1.3 rmind
210 1.10 rmind void
211 1.10 rmind npfctl_print_error(const nl_error_t *ne)
212 1.10 rmind {
213 1.10 rmind static const char *ncode_errors[] = {
214 1.10 rmind [-NPF_ERR_OPCODE] = "invalid instruction",
215 1.10 rmind [-NPF_ERR_JUMP] = "invalid jump",
216 1.10 rmind [-NPF_ERR_REG] = "invalid register",
217 1.10 rmind [-NPF_ERR_INVAL] = "invalid argument value",
218 1.10 rmind [-NPF_ERR_RANGE] = "processing out of range"
219 1.10 rmind };
220 1.10 rmind const int nc_err = ne->ne_ncode_error;
221 1.10 rmind const char *srcfile = ne->ne_source_file;
222 1.10 rmind
223 1.10 rmind if (srcfile) {
224 1.10 rmind warnx("source %s line %d", srcfile, ne->ne_source_line);
225 1.10 rmind }
226 1.10 rmind if (nc_err) {
227 1.10 rmind warnx("n-code error (%d): %s at offset 0x%x",
228 1.10 rmind nc_err, ncode_errors[-nc_err], ne->ne_ncode_errat);
229 1.10 rmind }
230 1.10 rmind if (ne->ne_id) {
231 1.10 rmind warnx("object: %d", ne->ne_id);
232 1.10 rmind }
233 1.10 rmind }
234 1.10 rmind
235 1.1 rmind static void
236 1.1 rmind npfctl(int action, int argc, char **argv)
237 1.1 rmind {
238 1.1 rmind int fd, ret, ver, boolval;
239 1.1 rmind npf_ioctl_table_t tbl;
240 1.1 rmind char *arg;
241 1.1 rmind
242 1.1 rmind fd = open(NPF_DEV_PATH, O_RDONLY);
243 1.1 rmind if (fd == -1) {
244 1.4 rmind err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
245 1.1 rmind }
246 1.1 rmind ret = ioctl(fd, IOC_NPF_VERSION, &ver);
247 1.1 rmind if (ver != NPF_VERSION) {
248 1.4 rmind errx(EXIT_FAILURE,
249 1.4 rmind "incompatible NPF interface version (%d, kernel %d)",
250 1.4 rmind NPF_VERSION, ver);
251 1.1 rmind }
252 1.1 rmind switch (action) {
253 1.1 rmind case NPFCTL_START:
254 1.1 rmind boolval = true;
255 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
256 1.1 rmind break;
257 1.1 rmind case NPFCTL_STOP:
258 1.1 rmind boolval = false;
259 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
260 1.1 rmind break;
261 1.1 rmind case NPFCTL_RELOAD:
262 1.8 rmind npfctl_config_init(false);
263 1.1 rmind npfctl_parsecfg(argc < 3 ? NPF_CONF_PATH : argv[2]);
264 1.8 rmind ret = npfctl_config_send(fd);
265 1.1 rmind break;
266 1.1 rmind case NPFCTL_FLUSH:
267 1.9 rmind ret = npf_config_flush(fd);
268 1.1 rmind break;
269 1.1 rmind case NPFCTL_TABLE:
270 1.1 rmind if (argc < 5) {
271 1.1 rmind usage();
272 1.1 rmind }
273 1.1 rmind tbl.nct_tid = atoi(argv[2]);
274 1.1 rmind if (strcmp(argv[3], "add") == 0) {
275 1.4 rmind /* Add table entry. */
276 1.1 rmind tbl.nct_action = NPF_IOCTL_TBLENT_ADD;
277 1.1 rmind arg = argv[4];
278 1.1 rmind } else if (strcmp(argv[3], "rem") == 0) {
279 1.4 rmind /* Remove entry. */
280 1.1 rmind tbl.nct_action = NPF_IOCTL_TBLENT_REM;
281 1.1 rmind arg = argv[4];
282 1.1 rmind } else {
283 1.4 rmind /* Default: lookup. */
284 1.1 rmind tbl.nct_action = 0;
285 1.1 rmind arg = argv[3];
286 1.1 rmind }
287 1.8 rmind fam_addr_mask_t *fam = npfctl_parse_cidr(arg);
288 1.8 rmind if (fam == NULL) {
289 1.1 rmind errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
290 1.1 rmind }
291 1.8 rmind memcpy(&tbl.nct_addr, &fam->fam_addr, sizeof(npf_addr_t));
292 1.8 rmind tbl.nct_mask = fam->fam_mask;
293 1.1 rmind ret = ioctl(fd, IOC_NPF_TABLE, &tbl);
294 1.8 rmind if (tbl.nct_action == 0) {
295 1.8 rmind printf("%s\n", ret ? "not found" : "found");
296 1.8 rmind exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
297 1.8 rmind }
298 1.1 rmind break;
299 1.3 rmind case NPFCTL_STATS:
300 1.3 rmind ret = npfctl_print_stats(fd);
301 1.3 rmind break;
302 1.3 rmind case NPFCTL_SESSIONS_SAVE:
303 1.5 rmind ret = npf_sessions_recv(fd, NPF_SESSDB_PATH);
304 1.5 rmind if (ret) {
305 1.5 rmind errx(EXIT_FAILURE, "could not save sessions to '%s'",
306 1.5 rmind NPF_SESSDB_PATH);
307 1.5 rmind }
308 1.3 rmind break;
309 1.3 rmind case NPFCTL_SESSIONS_LOAD:
310 1.5 rmind ret = npf_sessions_send(fd, NPF_SESSDB_PATH);
311 1.5 rmind if (ret) {
312 1.5 rmind errx(EXIT_FAILURE, "no sessions loaded from '%s'",
313 1.5 rmind NPF_SESSDB_PATH);
314 1.5 rmind }
315 1.3 rmind break;
316 1.1 rmind }
317 1.7 zoltan if (ret) {
318 1.1 rmind err(EXIT_FAILURE, "ioctl");
319 1.1 rmind }
320 1.1 rmind close(fd);
321 1.1 rmind }
322 1.1 rmind
323 1.1 rmind int
324 1.1 rmind main(int argc, char **argv)
325 1.1 rmind {
326 1.1 rmind char *cmd;
327 1.1 rmind
328 1.1 rmind if (argc < 2) {
329 1.1 rmind usage();
330 1.1 rmind }
331 1.1 rmind cmd = argv[1];
332 1.1 rmind
333 1.8 rmind if (strcmp(cmd, "debug") == 0) {
334 1.8 rmind const char *cfg = argc > 2 ? argv[2] : "npf.conf";
335 1.8 rmind npfctl_config_init(true);
336 1.8 rmind npfctl_parsecfg(cfg);
337 1.8 rmind npfctl_config_send(0);
338 1.8 rmind return EXIT_SUCCESS;
339 1.8 rmind }
340 1.4 rmind
341 1.1 rmind /* Find and call the subroutine */
342 1.8 rmind for (int n = 0; operations[n].cmd != NULL; n++) {
343 1.1 rmind if (strcmp(cmd, operations[n].cmd) != 0)
344 1.1 rmind continue;
345 1.1 rmind npfctl(operations[n].action, argc, argv);
346 1.8 rmind return EXIT_SUCCESS;
347 1.1 rmind }
348 1.3 rmind usage();
349 1.1 rmind }
350