npfctl.c revision 1.8 1 1.8 rmind /* $NetBSD: npfctl.c,v 1.8 2012/01/08 21:34:21 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.8 rmind __RCSID("$NetBSD: npfctl.c,v 1.8 2012/01/08 21:34:21 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.1 rmind static void
211 1.1 rmind npfctl(int action, int argc, char **argv)
212 1.1 rmind {
213 1.1 rmind int fd, ret, ver, boolval;
214 1.1 rmind npf_ioctl_table_t tbl;
215 1.1 rmind char *arg;
216 1.1 rmind
217 1.1 rmind fd = open(NPF_DEV_PATH, O_RDONLY);
218 1.1 rmind if (fd == -1) {
219 1.4 rmind err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
220 1.1 rmind }
221 1.1 rmind ret = ioctl(fd, IOC_NPF_VERSION, &ver);
222 1.1 rmind if (ver != NPF_VERSION) {
223 1.4 rmind errx(EXIT_FAILURE,
224 1.4 rmind "incompatible NPF interface version (%d, kernel %d)",
225 1.4 rmind NPF_VERSION, ver);
226 1.1 rmind }
227 1.1 rmind switch (action) {
228 1.1 rmind case NPFCTL_START:
229 1.1 rmind boolval = true;
230 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
231 1.1 rmind break;
232 1.1 rmind case NPFCTL_STOP:
233 1.1 rmind boolval = false;
234 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
235 1.1 rmind break;
236 1.1 rmind case NPFCTL_RELOAD:
237 1.8 rmind npfctl_config_init(false);
238 1.1 rmind npfctl_parsecfg(argc < 3 ? NPF_CONF_PATH : argv[2]);
239 1.8 rmind ret = npfctl_config_send(fd);
240 1.1 rmind break;
241 1.1 rmind case NPFCTL_FLUSH:
242 1.8 rmind ret = npfctl_config_flush(fd);
243 1.1 rmind break;
244 1.1 rmind case NPFCTL_TABLE:
245 1.1 rmind if (argc < 5) {
246 1.1 rmind usage();
247 1.1 rmind }
248 1.1 rmind tbl.nct_tid = atoi(argv[2]);
249 1.1 rmind if (strcmp(argv[3], "add") == 0) {
250 1.4 rmind /* Add table entry. */
251 1.1 rmind tbl.nct_action = NPF_IOCTL_TBLENT_ADD;
252 1.1 rmind arg = argv[4];
253 1.1 rmind } else if (strcmp(argv[3], "rem") == 0) {
254 1.4 rmind /* Remove entry. */
255 1.1 rmind tbl.nct_action = NPF_IOCTL_TBLENT_REM;
256 1.1 rmind arg = argv[4];
257 1.1 rmind } else {
258 1.4 rmind /* Default: lookup. */
259 1.1 rmind tbl.nct_action = 0;
260 1.1 rmind arg = argv[3];
261 1.1 rmind }
262 1.8 rmind fam_addr_mask_t *fam = npfctl_parse_cidr(arg);
263 1.8 rmind if (fam == NULL) {
264 1.1 rmind errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
265 1.1 rmind }
266 1.8 rmind memcpy(&tbl.nct_addr, &fam->fam_addr, sizeof(npf_addr_t));
267 1.8 rmind tbl.nct_mask = fam->fam_mask;
268 1.1 rmind ret = ioctl(fd, IOC_NPF_TABLE, &tbl);
269 1.8 rmind if (tbl.nct_action == 0) {
270 1.8 rmind printf("%s\n", ret ? "not found" : "found");
271 1.8 rmind exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
272 1.8 rmind }
273 1.1 rmind break;
274 1.3 rmind case NPFCTL_STATS:
275 1.3 rmind ret = npfctl_print_stats(fd);
276 1.3 rmind break;
277 1.3 rmind case NPFCTL_SESSIONS_SAVE:
278 1.5 rmind ret = npf_sessions_recv(fd, NPF_SESSDB_PATH);
279 1.5 rmind if (ret) {
280 1.5 rmind errx(EXIT_FAILURE, "could not save sessions to '%s'",
281 1.5 rmind NPF_SESSDB_PATH);
282 1.5 rmind }
283 1.3 rmind break;
284 1.3 rmind case NPFCTL_SESSIONS_LOAD:
285 1.5 rmind ret = npf_sessions_send(fd, NPF_SESSDB_PATH);
286 1.5 rmind if (ret) {
287 1.5 rmind errx(EXIT_FAILURE, "no sessions loaded from '%s'",
288 1.5 rmind NPF_SESSDB_PATH);
289 1.5 rmind }
290 1.3 rmind break;
291 1.1 rmind }
292 1.7 zoltan if (ret) {
293 1.1 rmind err(EXIT_FAILURE, "ioctl");
294 1.1 rmind }
295 1.1 rmind close(fd);
296 1.1 rmind }
297 1.1 rmind
298 1.1 rmind int
299 1.1 rmind main(int argc, char **argv)
300 1.1 rmind {
301 1.1 rmind char *cmd;
302 1.1 rmind
303 1.1 rmind if (argc < 2) {
304 1.1 rmind usage();
305 1.1 rmind }
306 1.1 rmind cmd = argv[1];
307 1.1 rmind
308 1.8 rmind if (strcmp(cmd, "debug") == 0) {
309 1.8 rmind const char *cfg = argc > 2 ? argv[2] : "npf.conf";
310 1.8 rmind npfctl_config_init(true);
311 1.8 rmind npfctl_parsecfg(cfg);
312 1.8 rmind npfctl_config_send(0);
313 1.8 rmind return EXIT_SUCCESS;
314 1.8 rmind }
315 1.4 rmind
316 1.1 rmind /* Find and call the subroutine */
317 1.8 rmind for (int n = 0; operations[n].cmd != NULL; n++) {
318 1.1 rmind if (strcmp(cmd, operations[n].cmd) != 0)
319 1.1 rmind continue;
320 1.1 rmind npfctl(operations[n].action, argc, argv);
321 1.8 rmind return EXIT_SUCCESS;
322 1.1 rmind }
323 1.3 rmind usage();
324 1.1 rmind }
325