npfctl.c revision 1.48 1 1.47 christos /* $NetBSD: npfctl.c,v 1.48 2016/12/26 23:05:05 christos Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.41 rmind * Copyright (c) 2009-2014 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.47 christos __RCSID("$NetBSD: npfctl.c,v 1.48 2016/12/26 23:05:05 christos Exp $");
34 1.2 rmind
35 1.1 rmind #include <sys/stat.h>
36 1.1 rmind #include <sys/types.h>
37 1.48 christos #include <sys/mman.h>
38 1.48 christos #ifdef __NetBSD__
39 1.48 christos #include <sha1.h>
40 1.48 christos #include <sys/ioctl.h>
41 1.44 rmind #include <sys/module.h>
42 1.48 christos #endif
43 1.1 rmind
44 1.1 rmind #include <stdio.h>
45 1.1 rmind #include <stdlib.h>
46 1.1 rmind #include <string.h>
47 1.1 rmind #include <err.h>
48 1.1 rmind #include <fcntl.h>
49 1.1 rmind #include <unistd.h>
50 1.15 rmind #include <errno.h>
51 1.48 christos
52 1.48 christos #include <arpa/inet.h>
53 1.48 christos #include <openssl/sha.h>
54 1.29 rmind
55 1.1 rmind #include "npfctl.h"
56 1.1 rmind
57 1.29 rmind extern void npf_yyparse_string(const char *);
58 1.8 rmind
59 1.11 rmind enum {
60 1.11 rmind NPFCTL_START,
61 1.11 rmind NPFCTL_STOP,
62 1.11 rmind NPFCTL_RELOAD,
63 1.11 rmind NPFCTL_SHOWCONF,
64 1.11 rmind NPFCTL_FLUSH,
65 1.25 rmind NPFCTL_VALIDATE,
66 1.11 rmind NPFCTL_TABLE,
67 1.29 rmind NPFCTL_RULE,
68 1.11 rmind NPFCTL_STATS,
69 1.41 rmind NPFCTL_SAVE,
70 1.41 rmind NPFCTL_LOAD,
71 1.11 rmind };
72 1.1 rmind
73 1.15 rmind static const struct operations_s {
74 1.1 rmind const char * cmd;
75 1.1 rmind int action;
76 1.1 rmind } operations[] = {
77 1.1 rmind /* Start, stop, reload */
78 1.41 rmind { "start", NPFCTL_START },
79 1.41 rmind { "stop", NPFCTL_STOP },
80 1.41 rmind { "reload", NPFCTL_RELOAD },
81 1.41 rmind { "show", NPFCTL_SHOWCONF, },
82 1.41 rmind { "flush", NPFCTL_FLUSH },
83 1.41 rmind { "valid", NPFCTL_VALIDATE },
84 1.1 rmind /* Table */
85 1.41 rmind { "table", NPFCTL_TABLE },
86 1.29 rmind /* Rule */
87 1.41 rmind { "rule", NPFCTL_RULE },
88 1.3 rmind /* Stats */
89 1.41 rmind { "stats", NPFCTL_STATS },
90 1.41 rmind /* Full state save/load */
91 1.41 rmind { "save", NPFCTL_SAVE },
92 1.41 rmind { "load", NPFCTL_LOAD },
93 1.1 rmind /* --- */
94 1.41 rmind { NULL, 0 }
95 1.1 rmind };
96 1.1 rmind
97 1.38 rmind bool
98 1.38 rmind join(char *buf, size_t buflen, int count, char **args, const char *sep)
99 1.29 rmind {
100 1.38 rmind const u_int seplen = strlen(sep);
101 1.29 rmind char *s = buf, *p = NULL;
102 1.29 rmind
103 1.29 rmind for (int i = 0; i < count; i++) {
104 1.29 rmind size_t len;
105 1.29 rmind
106 1.29 rmind p = stpncpy(s, args[i], buflen);
107 1.38 rmind len = p - s + seplen;
108 1.29 rmind if (len >= buflen) {
109 1.29 rmind return false;
110 1.29 rmind }
111 1.29 rmind buflen -= len;
112 1.38 rmind strcpy(p, sep);
113 1.38 rmind s = p + seplen;
114 1.29 rmind }
115 1.29 rmind *p = '\0';
116 1.29 rmind return true;
117 1.29 rmind }
118 1.29 rmind
119 1.6 joerg __dead static void
120 1.1 rmind usage(void)
121 1.1 rmind {
122 1.1 rmind const char *progname = getprogname();
123 1.1 rmind
124 1.1 rmind fprintf(stderr,
125 1.37 rmind "Usage:\t%s start | stop | flush | show | stats\n",
126 1.33 christos progname);
127 1.33 christos fprintf(stderr,
128 1.33 christos "\t%s validate | reload [<rule-file>]\n",
129 1.3 rmind progname);
130 1.3 rmind fprintf(stderr,
131 1.29 rmind "\t%s rule \"rule-name\" { add | rem } <rule-syntax>\n",
132 1.29 rmind progname);
133 1.29 rmind fprintf(stderr,
134 1.29 rmind "\t%s rule \"rule-name\" rem-id <rule-id>\n",
135 1.1 rmind progname);
136 1.1 rmind fprintf(stderr,
137 1.31 rmind "\t%s rule \"rule-name\" { list | flush }\n",
138 1.31 rmind progname);
139 1.31 rmind fprintf(stderr,
140 1.21 rmind "\t%s table <tid> { add | rem | test } <address/mask>\n",
141 1.1 rmind progname);
142 1.1 rmind fprintf(stderr,
143 1.21 rmind "\t%s table <tid> { list | flush }\n",
144 1.1 rmind progname);
145 1.37 rmind fprintf(stderr,
146 1.41 rmind "\t%s save | load\n",
147 1.37 rmind progname);
148 1.1 rmind exit(EXIT_FAILURE);
149 1.1 rmind }
150 1.1 rmind
151 1.3 rmind static int
152 1.3 rmind npfctl_print_stats(int fd)
153 1.3 rmind {
154 1.17 rmind static const struct stats_s {
155 1.17 rmind /* Note: -1 indicates a new section. */
156 1.17 rmind int index;
157 1.17 rmind const char * name;
158 1.17 rmind } stats[] = {
159 1.17 rmind { -1, "Packets passed" },
160 1.17 rmind { NPF_STAT_PASS_DEFAULT, "default pass" },
161 1.17 rmind { NPF_STAT_PASS_RULESET, "ruleset pass" },
162 1.41 rmind { NPF_STAT_PASS_CONN, "state pass" },
163 1.17 rmind
164 1.17 rmind { -1, "Packets blocked" },
165 1.17 rmind { NPF_STAT_BLOCK_DEFAULT, "default block" },
166 1.17 rmind { NPF_STAT_BLOCK_RULESET, "ruleset block" },
167 1.17 rmind
168 1.41 rmind { -1, "State and NAT entries" },
169 1.41 rmind { NPF_STAT_CONN_CREATE, "state allocations"},
170 1.41 rmind { NPF_STAT_CONN_DESTROY, "state destructions"},
171 1.17 rmind { NPF_STAT_NAT_CREATE, "NAT entry allocations" },
172 1.17 rmind { NPF_STAT_NAT_DESTROY, "NAT entry destructions"},
173 1.17 rmind
174 1.27 rmind { -1, "Network buffers" },
175 1.27 rmind { NPF_STAT_NBUF_NONCONTIG, "non-contiguous cases" },
176 1.27 rmind { NPF_STAT_NBUF_CONTIG_FAIL, "contig alloc failures" },
177 1.27 rmind
178 1.17 rmind { -1, "Invalid packet state cases" },
179 1.17 rmind { NPF_STAT_INVALID_STATE, "cases in total" },
180 1.17 rmind { NPF_STAT_INVALID_STATE_TCP1, "TCP case I" },
181 1.17 rmind { NPF_STAT_INVALID_STATE_TCP2, "TCP case II" },
182 1.17 rmind { NPF_STAT_INVALID_STATE_TCP3, "TCP case III" },
183 1.17 rmind
184 1.17 rmind { -1, "Packet race cases" },
185 1.17 rmind { NPF_STAT_RACE_NAT, "NAT association race" },
186 1.41 rmind { NPF_STAT_RACE_CONN, "duplicate state race" },
187 1.17 rmind
188 1.17 rmind { -1, "Fragmentation" },
189 1.17 rmind { NPF_STAT_FRAGMENTS, "fragments" },
190 1.17 rmind { NPF_STAT_REASSEMBLY, "reassembled" },
191 1.17 rmind { NPF_STAT_REASSFAIL, "failed reassembly" },
192 1.17 rmind
193 1.17 rmind { -1, "Other" },
194 1.17 rmind { NPF_STAT_ERROR, "unexpected errors" },
195 1.17 rmind };
196 1.24 rmind uint64_t *st = ecalloc(1, NPF_STATS_SIZE);
197 1.3 rmind
198 1.3 rmind if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
199 1.3 rmind err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
200 1.3 rmind }
201 1.3 rmind
202 1.17 rmind for (unsigned i = 0; i < __arraycount(stats); i++) {
203 1.17 rmind const char *sname = stats[i].name;
204 1.17 rmind int sidx = stats[i].index;
205 1.17 rmind
206 1.17 rmind if (sidx == -1) {
207 1.17 rmind printf("%s:\n", sname);
208 1.17 rmind } else {
209 1.17 rmind printf("\t%"PRIu64" %s\n", st[sidx], sname);
210 1.17 rmind }
211 1.17 rmind }
212 1.4 rmind
213 1.3 rmind free(st);
214 1.3 rmind return 0;
215 1.3 rmind }
216 1.3 rmind
217 1.10 rmind void
218 1.48 christos npfctl_print_error(const npf_error_t *ne)
219 1.10 rmind {
220 1.48 christos const char *srcfile = ne->source_file;
221 1.10 rmind
222 1.10 rmind if (srcfile) {
223 1.48 christos warnx("source %s line %d", srcfile, ne->source_line);
224 1.10 rmind }
225 1.48 christos if (ne->id) {
226 1.48 christos warnx("object: %" PRIi64, ne->id);
227 1.10 rmind }
228 1.10 rmind }
229 1.10 rmind
230 1.21 rmind char *
231 1.38 rmind npfctl_print_addrmask(int alen, const npf_addr_t *addr, npf_netmask_t mask)
232 1.21 rmind {
233 1.48 christos const unsigned buflen = 64;
234 1.48 christos char *buf = ecalloc(1, buflen);
235 1.21 rmind struct sockaddr_storage ss;
236 1.48 christos
237 1.48 christos memset(&ss, 0, sizeof(ss));
238 1.21 rmind
239 1.21 rmind switch (alen) {
240 1.21 rmind case 4: {
241 1.21 rmind struct sockaddr_in *sin = (void *)&ss;
242 1.21 rmind sin->sin_family = AF_INET;
243 1.21 rmind memcpy(&sin->sin_addr, addr, sizeof(sin->sin_addr));
244 1.21 rmind break;
245 1.21 rmind }
246 1.21 rmind case 16: {
247 1.21 rmind struct sockaddr_in6 *sin6 = (void *)&ss;
248 1.21 rmind sin6->sin6_family = AF_INET6;
249 1.21 rmind memcpy(&sin6->sin6_addr, addr, sizeof(sin6->sin6_addr));
250 1.21 rmind break;
251 1.21 rmind }
252 1.21 rmind default:
253 1.21 rmind assert(false);
254 1.21 rmind }
255 1.48 christos inet_ntop(ss.ss_family, (const void *)&ss, buf, buflen);
256 1.38 rmind if (mask && mask != NPF_NO_NETMASK) {
257 1.48 christos const unsigned len = strlen(buf);
258 1.48 christos snprintf(&buf[len], buflen - len, "/%u", mask);
259 1.21 rmind }
260 1.21 rmind return buf;
261 1.21 rmind }
262 1.21 rmind
263 1.16 joerg __dead static void
264 1.21 rmind npfctl_table(int fd, int argc, char **argv)
265 1.15 rmind {
266 1.15 rmind static const struct tblops_s {
267 1.15 rmind const char * cmd;
268 1.15 rmind int action;
269 1.15 rmind } tblops[] = {
270 1.29 rmind { "add", NPF_CMD_TABLE_ADD },
271 1.29 rmind { "rem", NPF_CMD_TABLE_REMOVE },
272 1.29 rmind { "del", NPF_CMD_TABLE_REMOVE },
273 1.29 rmind { "test", NPF_CMD_TABLE_LOOKUP },
274 1.29 rmind { "list", NPF_CMD_TABLE_LIST },
275 1.37 rmind { "flush", NPF_CMD_TABLE_FLUSH },
276 1.21 rmind { NULL, 0 }
277 1.15 rmind };
278 1.15 rmind npf_ioctl_table_t nct;
279 1.15 rmind fam_addr_mask_t fam;
280 1.21 rmind size_t buflen = 512;
281 1.40 rmind char *cmd, *arg;
282 1.15 rmind int n, alen;
283 1.15 rmind
284 1.21 rmind /* Default action is list. */
285 1.15 rmind memset(&nct, 0, sizeof(npf_ioctl_table_t));
286 1.40 rmind nct.nct_name = argv[0];
287 1.21 rmind cmd = argv[1];
288 1.15 rmind
289 1.15 rmind for (n = 0; tblops[n].cmd != NULL; n++) {
290 1.21 rmind if (strcmp(cmd, tblops[n].cmd) != 0) {
291 1.21 rmind continue;
292 1.21 rmind }
293 1.29 rmind nct.nct_cmd = tblops[n].action;
294 1.21 rmind break;
295 1.21 rmind }
296 1.21 rmind if (tblops[n].cmd == NULL) {
297 1.21 rmind errx(EXIT_FAILURE, "invalid command '%s'", cmd);
298 1.21 rmind }
299 1.37 rmind
300 1.37 rmind switch (nct.nct_cmd) {
301 1.37 rmind case NPF_CMD_TABLE_LIST:
302 1.37 rmind case NPF_CMD_TABLE_FLUSH:
303 1.40 rmind arg = NULL;
304 1.37 rmind break;
305 1.37 rmind default:
306 1.21 rmind if (argc < 3) {
307 1.21 rmind usage();
308 1.15 rmind }
309 1.21 rmind arg = argv[2];
310 1.15 rmind }
311 1.37 rmind
312 1.21 rmind again:
313 1.37 rmind switch (nct.nct_cmd) {
314 1.37 rmind case NPF_CMD_TABLE_LIST:
315 1.24 rmind nct.nct_data.buf.buf = ecalloc(1, buflen);
316 1.21 rmind nct.nct_data.buf.len = buflen;
317 1.37 rmind break;
318 1.37 rmind case NPF_CMD_TABLE_FLUSH:
319 1.37 rmind break;
320 1.37 rmind default:
321 1.21 rmind if (!npfctl_parse_cidr(arg, &fam, &alen)) {
322 1.21 rmind errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
323 1.21 rmind }
324 1.21 rmind nct.nct_data.ent.alen = alen;
325 1.26 rmind memcpy(&nct.nct_data.ent.addr, &fam.fam_addr, alen);
326 1.21 rmind nct.nct_data.ent.mask = fam.fam_mask;
327 1.15 rmind }
328 1.15 rmind
329 1.15 rmind if (ioctl(fd, IOC_NPF_TABLE, &nct) != -1) {
330 1.15 rmind errno = 0;
331 1.15 rmind }
332 1.15 rmind switch (errno) {
333 1.21 rmind case 0:
334 1.21 rmind break;
335 1.15 rmind case EEXIST:
336 1.21 rmind errx(EXIT_FAILURE, "entry already exists or is conflicting");
337 1.15 rmind case ENOENT:
338 1.21 rmind errx(EXIT_FAILURE, "no matching entry was not found");
339 1.15 rmind case EINVAL:
340 1.21 rmind errx(EXIT_FAILURE, "invalid address, mask or table ID");
341 1.21 rmind case ENOMEM:
342 1.29 rmind if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
343 1.21 rmind /* XXX */
344 1.21 rmind free(nct.nct_data.buf.buf);
345 1.21 rmind buflen <<= 1;
346 1.21 rmind goto again;
347 1.21 rmind }
348 1.21 rmind /* FALLTHROUGH */
349 1.21 rmind default:
350 1.32 christos err(EXIT_FAILURE, "ioctl(IOC_NPF_TABLE)");
351 1.21 rmind }
352 1.21 rmind
353 1.29 rmind if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
354 1.21 rmind npf_ioctl_ent_t *ent = nct.nct_data.buf.buf;
355 1.21 rmind char *buf;
356 1.21 rmind
357 1.21 rmind while (nct.nct_data.buf.len--) {
358 1.21 rmind if (!ent->alen)
359 1.21 rmind break;
360 1.21 rmind buf = npfctl_print_addrmask(ent->alen,
361 1.21 rmind &ent->addr, ent->mask);
362 1.21 rmind puts(buf);
363 1.21 rmind ent++;
364 1.21 rmind }
365 1.21 rmind free(nct.nct_data.buf.buf);
366 1.21 rmind } else {
367 1.21 rmind printf("%s: %s\n", getprogname(),
368 1.29 rmind nct.nct_cmd == NPF_CMD_TABLE_LOOKUP ?
369 1.15 rmind "matching entry found" : "success");
370 1.15 rmind }
371 1.21 rmind exit(EXIT_SUCCESS);
372 1.15 rmind }
373 1.15 rmind
374 1.29 rmind static nl_rule_t *
375 1.29 rmind npfctl_parse_rule(int argc, char **argv)
376 1.29 rmind {
377 1.29 rmind char rule_string[1024];
378 1.29 rmind nl_rule_t *rl;
379 1.29 rmind
380 1.29 rmind /* Get the rule string and parse it. */
381 1.38 rmind if (!join(rule_string, sizeof(rule_string), argc, argv, " ")) {
382 1.29 rmind errx(EXIT_FAILURE, "command too long");
383 1.29 rmind }
384 1.29 rmind npfctl_parse_string(rule_string);
385 1.29 rmind if ((rl = npfctl_rule_ref()) == NULL) {
386 1.29 rmind errx(EXIT_FAILURE, "could not parse the rule");
387 1.29 rmind }
388 1.29 rmind return rl;
389 1.29 rmind }
390 1.29 rmind
391 1.48 christos #ifdef __NetBSD__
392 1.48 christos unsigned char *
393 1.48 christos SHA1(const unsigned char *d, unsigned long l, unsigned char *md)
394 1.48 christos {
395 1.48 christos SHA1_CTX c;
396 1.48 christos
397 1.48 christos SHA1Init(&c);
398 1.48 christos SHA1Update(&c, d, l);
399 1.48 christos SHA1Final(md, &c);
400 1.48 christos return md;
401 1.47 christos }
402 1.48 christos #endif
403 1.47 christos
404 1.47 christos static void
405 1.29 rmind npfctl_generate_key(nl_rule_t *rl, void *key)
406 1.29 rmind {
407 1.29 rmind void *meta;
408 1.29 rmind size_t len;
409 1.29 rmind
410 1.29 rmind if ((meta = npf_rule_export(rl, &len)) == NULL) {
411 1.29 rmind errx(EXIT_FAILURE, "error generating rule key");
412 1.29 rmind }
413 1.48 christos __CTASSERT(NPF_RULE_MAXKEYLEN >= SHA_DIGEST_LENGTH);
414 1.29 rmind memset(key, 0, NPF_RULE_MAXKEYLEN);
415 1.48 christos SHA1(meta, len, key);
416 1.29 rmind free(meta);
417 1.29 rmind }
418 1.29 rmind
419 1.29 rmind __dead static void
420 1.29 rmind npfctl_rule(int fd, int argc, char **argv)
421 1.29 rmind {
422 1.29 rmind static const struct ruleops_s {
423 1.29 rmind const char * cmd;
424 1.29 rmind int action;
425 1.36 rmind bool extra_arg;
426 1.29 rmind } ruleops[] = {
427 1.36 rmind { "add", NPF_CMD_RULE_ADD, true },
428 1.36 rmind { "rem", NPF_CMD_RULE_REMKEY, true },
429 1.36 rmind { "del", NPF_CMD_RULE_REMKEY, true },
430 1.36 rmind { "rem-id", NPF_CMD_RULE_REMOVE, true },
431 1.36 rmind { "list", NPF_CMD_RULE_LIST, false },
432 1.36 rmind { "flush", NPF_CMD_RULE_FLUSH, false },
433 1.36 rmind { NULL, 0, 0 }
434 1.29 rmind };
435 1.29 rmind uint8_t key[NPF_RULE_MAXKEYLEN];
436 1.29 rmind const char *ruleset_name = argv[0];
437 1.29 rmind const char *cmd = argv[1];
438 1.29 rmind int error, action = 0;
439 1.31 rmind uint64_t rule_id;
440 1.36 rmind bool extra_arg;
441 1.29 rmind nl_rule_t *rl;
442 1.29 rmind
443 1.29 rmind for (int n = 0; ruleops[n].cmd != NULL; n++) {
444 1.29 rmind if (strcmp(cmd, ruleops[n].cmd) == 0) {
445 1.29 rmind action = ruleops[n].action;
446 1.36 rmind extra_arg = ruleops[n].extra_arg;
447 1.29 rmind break;
448 1.29 rmind }
449 1.29 rmind }
450 1.36 rmind argc -= 2;
451 1.36 rmind argv += 2;
452 1.29 rmind
453 1.36 rmind if (!action || (extra_arg && argc == 0)) {
454 1.29 rmind usage();
455 1.29 rmind }
456 1.29 rmind
457 1.29 rmind switch (action) {
458 1.29 rmind case NPF_CMD_RULE_ADD:
459 1.29 rmind rl = npfctl_parse_rule(argc, argv);
460 1.29 rmind npfctl_generate_key(rl, key);
461 1.29 rmind npf_rule_setkey(rl, key, sizeof(key));
462 1.29 rmind error = npf_ruleset_add(fd, ruleset_name, rl, &rule_id);
463 1.29 rmind break;
464 1.29 rmind case NPF_CMD_RULE_REMKEY:
465 1.29 rmind rl = npfctl_parse_rule(argc, argv);
466 1.29 rmind npfctl_generate_key(rl, key);
467 1.29 rmind error = npf_ruleset_remkey(fd, ruleset_name, key, sizeof(key));
468 1.29 rmind break;
469 1.29 rmind case NPF_CMD_RULE_REMOVE:
470 1.31 rmind rule_id = strtoull(argv[0], NULL, 16);
471 1.29 rmind error = npf_ruleset_remove(fd, ruleset_name, rule_id);
472 1.29 rmind break;
473 1.30 rmind case NPF_CMD_RULE_LIST:
474 1.30 rmind error = npfctl_ruleset_show(fd, ruleset_name);
475 1.30 rmind break;
476 1.30 rmind case NPF_CMD_RULE_FLUSH:
477 1.30 rmind error = npf_ruleset_flush(fd, ruleset_name);
478 1.30 rmind break;
479 1.29 rmind default:
480 1.48 christos abort();
481 1.29 rmind }
482 1.29 rmind
483 1.29 rmind switch (error) {
484 1.29 rmind case 0:
485 1.29 rmind /* Success. */
486 1.29 rmind break;
487 1.31 rmind case ESRCH:
488 1.31 rmind errx(EXIT_FAILURE, "ruleset \"%s\" not found", ruleset_name);
489 1.29 rmind case ENOENT:
490 1.31 rmind errx(EXIT_FAILURE, "rule was not found");
491 1.29 rmind default:
492 1.29 rmind errx(EXIT_FAILURE, "rule operation: %s", strerror(error));
493 1.29 rmind }
494 1.29 rmind if (action == NPF_CMD_RULE_ADD) {
495 1.31 rmind printf("OK %" PRIx64 "\n", rule_id);
496 1.29 rmind }
497 1.29 rmind exit(EXIT_SUCCESS);
498 1.29 rmind }
499 1.29 rmind
500 1.45 christos static bool bpfjit = true;
501 1.45 christos
502 1.45 christos void
503 1.45 christos npfctl_bpfjit(bool onoff)
504 1.45 christos {
505 1.45 christos bpfjit = onoff;
506 1.45 christos }
507 1.45 christos
508 1.44 rmind static void
509 1.44 rmind npfctl_preload_bpfjit(void)
510 1.44 rmind {
511 1.48 christos #ifdef __NetBSD__
512 1.44 rmind modctl_load_t args = {
513 1.44 rmind .ml_filename = "bpfjit",
514 1.44 rmind .ml_flags = MODCTL_NO_PROP,
515 1.44 rmind .ml_props = NULL,
516 1.44 rmind .ml_propslen = 0
517 1.44 rmind };
518 1.44 rmind
519 1.45 christos if (!bpfjit)
520 1.45 christos return;
521 1.45 christos
522 1.44 rmind if (modctl(MODCTL_LOAD, &args) != 0 && errno != EEXIST) {
523 1.45 christos static const char *p = "; performance will be degraded";
524 1.45 christos if (errno == ENOENT)
525 1.45 christos warnx("the bpfjit module seems to be missing%s", p);
526 1.45 christos else
527 1.45 christos warn("error loading the bpfjit module%s", p);
528 1.45 christos warnx("To disable this warning `set bpf.jit off' in "
529 1.45 christos "/etc/npf.conf");
530 1.44 rmind }
531 1.48 christos #endif
532 1.44 rmind }
533 1.44 rmind
534 1.41 rmind static int
535 1.48 christos npfctl_load(int fd)
536 1.41 rmind {
537 1.41 rmind nl_config_t *ncf;
538 1.48 christos npf_error_t errinfo;
539 1.48 christos struct stat sb;
540 1.48 christos size_t blen;
541 1.48 christos void *blob;
542 1.41 rmind int error;
543 1.41 rmind
544 1.48 christos /*
545 1.48 christos * The file may change while reading - we are not handling this,
546 1.48 christos * leaving this responsibility for the caller.
547 1.48 christos */
548 1.48 christos if (stat(NPF_DB_PATH, &sb) == -1) {
549 1.48 christos err(EXIT_FAILURE, "stat");
550 1.48 christos }
551 1.48 christos if ((blen = sb.st_size) == 0) {
552 1.48 christos err(EXIT_FAILURE, "saved configuration file is empty");
553 1.48 christos }
554 1.48 christos if ((blob = mmap(NULL, blen, PROT_READ,
555 1.48 christos MAP_FILE | MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
556 1.48 christos err(EXIT_FAILURE, "mmap");
557 1.48 christos }
558 1.48 christos ncf = npf_config_import(blob, blen);
559 1.48 christos munmap(blob, blen);
560 1.41 rmind if (ncf == NULL) {
561 1.41 rmind return errno;
562 1.41 rmind }
563 1.41 rmind
564 1.48 christos /*
565 1.48 christos * Configuration imported - submit it now.
566 1.48 christos **/
567 1.48 christos errno = error = npf_config_submit(ncf, fd, &errinfo);
568 1.43 rmind if (error) {
569 1.48 christos npfctl_print_error(&errinfo);
570 1.43 rmind }
571 1.41 rmind npf_config_destroy(ncf);
572 1.41 rmind return error;
573 1.41 rmind }
574 1.41 rmind
575 1.15 rmind static void
576 1.1 rmind npfctl(int action, int argc, char **argv)
577 1.1 rmind {
578 1.29 rmind int fd, ver, boolval, ret = 0;
579 1.48 christos nl_config_t *ncf;
580 1.48 christos const char *fun = "";
581 1.1 rmind
582 1.1 rmind fd = open(NPF_DEV_PATH, O_RDONLY);
583 1.1 rmind if (fd == -1) {
584 1.4 rmind err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
585 1.1 rmind }
586 1.29 rmind if (ioctl(fd, IOC_NPF_VERSION, &ver) == -1) {
587 1.32 christos err(EXIT_FAILURE, "ioctl(IOC_NPF_VERSION)");
588 1.15 rmind }
589 1.1 rmind if (ver != NPF_VERSION) {
590 1.4 rmind errx(EXIT_FAILURE,
591 1.14 rmind "incompatible NPF interface version (%d, kernel %d)\n"
592 1.14 rmind "Hint: update userland?", NPF_VERSION, ver);
593 1.1 rmind }
594 1.29 rmind
595 1.1 rmind switch (action) {
596 1.1 rmind case NPFCTL_START:
597 1.1 rmind boolval = true;
598 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
599 1.32 christos fun = "ioctl(IOC_NPF_SWITCH)";
600 1.1 rmind break;
601 1.1 rmind case NPFCTL_STOP:
602 1.1 rmind boolval = false;
603 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
604 1.32 christos fun = "ioctl(IOC_NPF_SWITCH)";
605 1.1 rmind break;
606 1.1 rmind case NPFCTL_RELOAD:
607 1.8 rmind npfctl_config_init(false);
608 1.29 rmind npfctl_parse_file(argc < 3 ? NPF_CONF_PATH : argv[2]);
609 1.46 christos npfctl_preload_bpfjit();
610 1.32 christos errno = ret = npfctl_config_send(fd, NULL);
611 1.32 christos fun = "npfctl_config_send";
612 1.1 rmind break;
613 1.11 rmind case NPFCTL_SHOWCONF:
614 1.11 rmind ret = npfctl_config_show(fd);
615 1.32 christos fun = "npfctl_config_show";
616 1.11 rmind break;
617 1.1 rmind case NPFCTL_FLUSH:
618 1.9 rmind ret = npf_config_flush(fd);
619 1.32 christos fun = "npf_config_flush";
620 1.1 rmind break;
621 1.25 rmind case NPFCTL_VALIDATE:
622 1.25 rmind npfctl_config_init(false);
623 1.29 rmind npfctl_parse_file(argc < 3 ? NPF_CONF_PATH : argv[2]);
624 1.25 rmind ret = npfctl_config_show(0);
625 1.32 christos fun = "npfctl_config_show";
626 1.25 rmind break;
627 1.1 rmind case NPFCTL_TABLE:
628 1.21 rmind if ((argc -= 2) < 2) {
629 1.1 rmind usage();
630 1.1 rmind }
631 1.21 rmind argv += 2;
632 1.21 rmind npfctl_table(fd, argc, argv);
633 1.1 rmind break;
634 1.29 rmind case NPFCTL_RULE:
635 1.29 rmind if ((argc -= 2) < 2) {
636 1.29 rmind usage();
637 1.29 rmind }
638 1.29 rmind argv += 2;
639 1.29 rmind npfctl_rule(fd, argc, argv);
640 1.29 rmind break;
641 1.41 rmind case NPFCTL_LOAD:
642 1.44 rmind npfctl_preload_bpfjit();
643 1.41 rmind ret = npfctl_load(fd);
644 1.41 rmind fun = "npfctl_config_load";
645 1.41 rmind break;
646 1.41 rmind case NPFCTL_SAVE:
647 1.48 christos ncf = npf_config_retrieve(fd);
648 1.48 christos if (ncf) {
649 1.48 christos npfctl_config_save(ncf, NPF_DB_PATH);
650 1.48 christos npf_config_destroy(ncf);
651 1.48 christos } else {
652 1.48 christos ret = errno;
653 1.48 christos }
654 1.41 rmind fun = "npfctl_config_save";
655 1.41 rmind break;
656 1.3 rmind case NPFCTL_STATS:
657 1.3 rmind ret = npfctl_print_stats(fd);
658 1.32 christos fun = "npfctl_print_stats";
659 1.3 rmind break;
660 1.1 rmind }
661 1.7 zoltan if (ret) {
662 1.32 christos err(EXIT_FAILURE, "%s", fun);
663 1.1 rmind }
664 1.1 rmind close(fd);
665 1.1 rmind }
666 1.1 rmind
667 1.1 rmind int
668 1.1 rmind main(int argc, char **argv)
669 1.1 rmind {
670 1.1 rmind char *cmd;
671 1.1 rmind
672 1.1 rmind if (argc < 2) {
673 1.1 rmind usage();
674 1.1 rmind }
675 1.48 christos npfctl_show_init();
676 1.1 rmind cmd = argv[1];
677 1.1 rmind
678 1.8 rmind if (strcmp(cmd, "debug") == 0) {
679 1.14 rmind const char *cfg = argc > 2 ? argv[2] : "/etc/npf.conf";
680 1.18 rmind const char *out = argc > 3 ? argv[3] : "/tmp/npf.plist";
681 1.18 rmind
682 1.8 rmind npfctl_config_init(true);
683 1.29 rmind npfctl_parse_file(cfg);
684 1.18 rmind npfctl_config_send(0, out);
685 1.8 rmind return EXIT_SUCCESS;
686 1.8 rmind }
687 1.4 rmind
688 1.12 rmind /* Find and call the subroutine. */
689 1.8 rmind for (int n = 0; operations[n].cmd != NULL; n++) {
690 1.25 rmind const char *opcmd = operations[n].cmd;
691 1.25 rmind if (strncmp(cmd, opcmd, strlen(opcmd)) != 0)
692 1.1 rmind continue;
693 1.1 rmind npfctl(operations[n].action, argc, argv);
694 1.8 rmind return EXIT_SUCCESS;
695 1.1 rmind }
696 1.3 rmind usage();
697 1.1 rmind }
698