npfctl.c revision 1.60 1 1.1 rmind /*-
2 1.41 rmind * Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
3 1.1 rmind * All rights reserved.
4 1.1 rmind *
5 1.1 rmind * This material is based upon work partially supported by The
6 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
7 1.1 rmind *
8 1.1 rmind * Redistribution and use in source and binary forms, with or without
9 1.1 rmind * modification, are permitted provided that the following conditions
10 1.1 rmind * are met:
11 1.1 rmind * 1. Redistributions of source code must retain the above copyright
12 1.1 rmind * notice, this list of conditions and the following disclaimer.
13 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer in the
15 1.1 rmind * documentation and/or other materials provided with the distribution.
16 1.1 rmind *
17 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
28 1.1 rmind */
29 1.1 rmind
30 1.2 rmind #include <sys/cdefs.h>
31 1.60 rmind __RCSID("$NetBSD: npfctl.c,v 1.60 2019/07/25 00:48:55 rmind Exp $");
32 1.2 rmind
33 1.1 rmind #include <sys/stat.h>
34 1.1 rmind #include <sys/types.h>
35 1.48 christos #include <sys/mman.h>
36 1.48 christos #ifdef __NetBSD__
37 1.48 christos #include <sha1.h>
38 1.48 christos #include <sys/ioctl.h>
39 1.44 rmind #include <sys/module.h>
40 1.49 christos #define SHA_DIGEST_LENGTH SHA1_DIGEST_LENGTH
41 1.49 christos #else
42 1.49 christos #include <openssl/sha.h>
43 1.48 christos #endif
44 1.1 rmind
45 1.1 rmind #include <stdio.h>
46 1.1 rmind #include <stdlib.h>
47 1.1 rmind #include <string.h>
48 1.1 rmind #include <err.h>
49 1.1 rmind #include <fcntl.h>
50 1.1 rmind #include <unistd.h>
51 1.15 rmind #include <errno.h>
52 1.48 christos
53 1.48 christos #include <arpa/inet.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.52 rmind NPFCTL_DEBUG,
72 1.50 christos NPFCTL_CONN_LIST,
73 1.11 rmind };
74 1.1 rmind
75 1.15 rmind static const struct operations_s {
76 1.1 rmind const char * cmd;
77 1.1 rmind int action;
78 1.1 rmind } operations[] = {
79 1.1 rmind /* Start, stop, reload */
80 1.41 rmind { "start", NPFCTL_START },
81 1.41 rmind { "stop", NPFCTL_STOP },
82 1.41 rmind { "reload", NPFCTL_RELOAD },
83 1.41 rmind { "show", NPFCTL_SHOWCONF, },
84 1.41 rmind { "flush", NPFCTL_FLUSH },
85 1.1 rmind /* Table */
86 1.41 rmind { "table", NPFCTL_TABLE },
87 1.29 rmind /* Rule */
88 1.41 rmind { "rule", NPFCTL_RULE },
89 1.3 rmind /* Stats */
90 1.41 rmind { "stats", NPFCTL_STATS },
91 1.41 rmind /* Full state save/load */
92 1.41 rmind { "save", NPFCTL_SAVE },
93 1.41 rmind { "load", NPFCTL_LOAD },
94 1.50 christos { "list", NPFCTL_CONN_LIST },
95 1.52 rmind /* Misc. */
96 1.52 rmind { "valid", NPFCTL_VALIDATE },
97 1.52 rmind { "debug", NPFCTL_DEBUG },
98 1.1 rmind /* --- */
99 1.41 rmind { NULL, 0 }
100 1.1 rmind };
101 1.1 rmind
102 1.38 rmind bool
103 1.38 rmind join(char *buf, size_t buflen, int count, char **args, const char *sep)
104 1.29 rmind {
105 1.38 rmind const u_int seplen = strlen(sep);
106 1.29 rmind char *s = buf, *p = NULL;
107 1.29 rmind
108 1.29 rmind for (int i = 0; i < count; i++) {
109 1.29 rmind size_t len;
110 1.29 rmind
111 1.29 rmind p = stpncpy(s, args[i], buflen);
112 1.38 rmind len = p - s + seplen;
113 1.29 rmind if (len >= buflen) {
114 1.29 rmind return false;
115 1.29 rmind }
116 1.29 rmind buflen -= len;
117 1.38 rmind strcpy(p, sep);
118 1.38 rmind s = p + seplen;
119 1.29 rmind }
120 1.29 rmind *p = '\0';
121 1.29 rmind return true;
122 1.29 rmind }
123 1.29 rmind
124 1.6 joerg __dead static void
125 1.1 rmind usage(void)
126 1.1 rmind {
127 1.1 rmind const char *progname = getprogname();
128 1.1 rmind
129 1.1 rmind fprintf(stderr,
130 1.37 rmind "Usage:\t%s start | stop | flush | show | stats\n",
131 1.33 christos progname);
132 1.33 christos fprintf(stderr,
133 1.33 christos "\t%s validate | reload [<rule-file>]\n",
134 1.3 rmind progname);
135 1.3 rmind fprintf(stderr,
136 1.29 rmind "\t%s rule \"rule-name\" { add | rem } <rule-syntax>\n",
137 1.29 rmind progname);
138 1.29 rmind fprintf(stderr,
139 1.29 rmind "\t%s rule \"rule-name\" rem-id <rule-id>\n",
140 1.1 rmind progname);
141 1.1 rmind fprintf(stderr,
142 1.31 rmind "\t%s rule \"rule-name\" { list | flush }\n",
143 1.31 rmind progname);
144 1.31 rmind fprintf(stderr,
145 1.21 rmind "\t%s table <tid> { add | rem | test } <address/mask>\n",
146 1.1 rmind progname);
147 1.1 rmind fprintf(stderr,
148 1.21 rmind "\t%s table <tid> { list | flush }\n",
149 1.1 rmind progname);
150 1.37 rmind fprintf(stderr,
151 1.41 rmind "\t%s save | load\n",
152 1.37 rmind progname);
153 1.50 christos fprintf(stderr,
154 1.51 wiz "\t%s list [-46hNnw] [-i <ifname>]\n",
155 1.50 christos progname);
156 1.57 rmind fprintf(stderr,
157 1.57 rmind "\t%s debug [<rule-file>] [<raw-output>]\n",
158 1.57 rmind progname);
159 1.1 rmind exit(EXIT_FAILURE);
160 1.1 rmind }
161 1.1 rmind
162 1.3 rmind static int
163 1.3 rmind npfctl_print_stats(int fd)
164 1.3 rmind {
165 1.17 rmind static const struct stats_s {
166 1.17 rmind /* Note: -1 indicates a new section. */
167 1.17 rmind int index;
168 1.17 rmind const char * name;
169 1.17 rmind } stats[] = {
170 1.17 rmind { -1, "Packets passed" },
171 1.17 rmind { NPF_STAT_PASS_DEFAULT, "default pass" },
172 1.17 rmind { NPF_STAT_PASS_RULESET, "ruleset pass" },
173 1.41 rmind { NPF_STAT_PASS_CONN, "state pass" },
174 1.17 rmind
175 1.17 rmind { -1, "Packets blocked" },
176 1.17 rmind { NPF_STAT_BLOCK_DEFAULT, "default block" },
177 1.17 rmind { NPF_STAT_BLOCK_RULESET, "ruleset block" },
178 1.17 rmind
179 1.41 rmind { -1, "State and NAT entries" },
180 1.41 rmind { NPF_STAT_CONN_CREATE, "state allocations"},
181 1.41 rmind { NPF_STAT_CONN_DESTROY, "state destructions"},
182 1.17 rmind { NPF_STAT_NAT_CREATE, "NAT entry allocations" },
183 1.17 rmind { NPF_STAT_NAT_DESTROY, "NAT entry destructions"},
184 1.17 rmind
185 1.27 rmind { -1, "Network buffers" },
186 1.27 rmind { NPF_STAT_NBUF_NONCONTIG, "non-contiguous cases" },
187 1.27 rmind { NPF_STAT_NBUF_CONTIG_FAIL, "contig alloc failures" },
188 1.27 rmind
189 1.17 rmind { -1, "Invalid packet state cases" },
190 1.17 rmind { NPF_STAT_INVALID_STATE, "cases in total" },
191 1.17 rmind { NPF_STAT_INVALID_STATE_TCP1, "TCP case I" },
192 1.17 rmind { NPF_STAT_INVALID_STATE_TCP2, "TCP case II" },
193 1.17 rmind { NPF_STAT_INVALID_STATE_TCP3, "TCP case III" },
194 1.17 rmind
195 1.17 rmind { -1, "Packet race cases" },
196 1.17 rmind { NPF_STAT_RACE_NAT, "NAT association race" },
197 1.41 rmind { NPF_STAT_RACE_CONN, "duplicate state race" },
198 1.17 rmind
199 1.17 rmind { -1, "Fragmentation" },
200 1.17 rmind { NPF_STAT_FRAGMENTS, "fragments" },
201 1.17 rmind { NPF_STAT_REASSEMBLY, "reassembled" },
202 1.17 rmind { NPF_STAT_REASSFAIL, "failed reassembly" },
203 1.17 rmind
204 1.17 rmind { -1, "Other" },
205 1.17 rmind { NPF_STAT_ERROR, "unexpected errors" },
206 1.17 rmind };
207 1.24 rmind uint64_t *st = ecalloc(1, NPF_STATS_SIZE);
208 1.3 rmind
209 1.3 rmind if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
210 1.3 rmind err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
211 1.3 rmind }
212 1.3 rmind
213 1.17 rmind for (unsigned i = 0; i < __arraycount(stats); i++) {
214 1.17 rmind const char *sname = stats[i].name;
215 1.17 rmind int sidx = stats[i].index;
216 1.17 rmind
217 1.17 rmind if (sidx == -1) {
218 1.17 rmind printf("%s:\n", sname);
219 1.17 rmind } else {
220 1.17 rmind printf("\t%"PRIu64" %s\n", st[sidx], sname);
221 1.17 rmind }
222 1.17 rmind }
223 1.4 rmind
224 1.3 rmind free(st);
225 1.3 rmind return 0;
226 1.3 rmind }
227 1.3 rmind
228 1.10 rmind void
229 1.48 christos npfctl_print_error(const npf_error_t *ne)
230 1.10 rmind {
231 1.48 christos const char *srcfile = ne->source_file;
232 1.10 rmind
233 1.59 rmind if (ne->error_msg) {
234 1.60 rmind errx(EXIT_FAILURE, "%s", ne->error_msg);
235 1.59 rmind }
236 1.10 rmind if (srcfile) {
237 1.48 christos warnx("source %s line %d", srcfile, ne->source_line);
238 1.10 rmind }
239 1.48 christos if (ne->id) {
240 1.48 christos warnx("object: %" PRIi64, ne->id);
241 1.10 rmind }
242 1.10 rmind }
243 1.10 rmind
244 1.21 rmind char *
245 1.50 christos npfctl_print_addrmask(int alen, const char *fmt, const npf_addr_t *addr,
246 1.50 christos npf_netmask_t mask)
247 1.21 rmind {
248 1.50 christos const unsigned buflen = 256;
249 1.48 christos char *buf = ecalloc(1, buflen);
250 1.21 rmind struct sockaddr_storage ss;
251 1.48 christos
252 1.48 christos memset(&ss, 0, sizeof(ss));
253 1.21 rmind
254 1.21 rmind switch (alen) {
255 1.21 rmind case 4: {
256 1.21 rmind struct sockaddr_in *sin = (void *)&ss;
257 1.21 rmind sin->sin_family = AF_INET;
258 1.21 rmind memcpy(&sin->sin_addr, addr, sizeof(sin->sin_addr));
259 1.21 rmind break;
260 1.21 rmind }
261 1.21 rmind case 16: {
262 1.21 rmind struct sockaddr_in6 *sin6 = (void *)&ss;
263 1.21 rmind sin6->sin6_family = AF_INET6;
264 1.21 rmind memcpy(&sin6->sin6_addr, addr, sizeof(sin6->sin6_addr));
265 1.21 rmind break;
266 1.21 rmind }
267 1.21 rmind default:
268 1.21 rmind assert(false);
269 1.21 rmind }
270 1.50 christos sockaddr_snprintf(buf, buflen, fmt, (const void *)&ss);
271 1.38 rmind if (mask && mask != NPF_NO_NETMASK) {
272 1.48 christos const unsigned len = strlen(buf);
273 1.48 christos snprintf(&buf[len], buflen - len, "/%u", mask);
274 1.21 rmind }
275 1.21 rmind return buf;
276 1.21 rmind }
277 1.21 rmind
278 1.16 joerg __dead static void
279 1.21 rmind npfctl_table(int fd, int argc, char **argv)
280 1.15 rmind {
281 1.15 rmind static const struct tblops_s {
282 1.15 rmind const char * cmd;
283 1.15 rmind int action;
284 1.15 rmind } tblops[] = {
285 1.29 rmind { "add", NPF_CMD_TABLE_ADD },
286 1.29 rmind { "rem", NPF_CMD_TABLE_REMOVE },
287 1.29 rmind { "del", NPF_CMD_TABLE_REMOVE },
288 1.29 rmind { "test", NPF_CMD_TABLE_LOOKUP },
289 1.29 rmind { "list", NPF_CMD_TABLE_LIST },
290 1.37 rmind { "flush", NPF_CMD_TABLE_FLUSH },
291 1.21 rmind { NULL, 0 }
292 1.15 rmind };
293 1.15 rmind npf_ioctl_table_t nct;
294 1.15 rmind fam_addr_mask_t fam;
295 1.21 rmind size_t buflen = 512;
296 1.40 rmind char *cmd, *arg;
297 1.15 rmind int n, alen;
298 1.15 rmind
299 1.21 rmind /* Default action is list. */
300 1.15 rmind memset(&nct, 0, sizeof(npf_ioctl_table_t));
301 1.40 rmind nct.nct_name = argv[0];
302 1.21 rmind cmd = argv[1];
303 1.15 rmind
304 1.15 rmind for (n = 0; tblops[n].cmd != NULL; n++) {
305 1.21 rmind if (strcmp(cmd, tblops[n].cmd) != 0) {
306 1.21 rmind continue;
307 1.21 rmind }
308 1.29 rmind nct.nct_cmd = tblops[n].action;
309 1.21 rmind break;
310 1.21 rmind }
311 1.21 rmind if (tblops[n].cmd == NULL) {
312 1.21 rmind errx(EXIT_FAILURE, "invalid command '%s'", cmd);
313 1.21 rmind }
314 1.37 rmind
315 1.37 rmind switch (nct.nct_cmd) {
316 1.37 rmind case NPF_CMD_TABLE_LIST:
317 1.37 rmind case NPF_CMD_TABLE_FLUSH:
318 1.40 rmind arg = NULL;
319 1.37 rmind break;
320 1.37 rmind default:
321 1.21 rmind if (argc < 3) {
322 1.21 rmind usage();
323 1.15 rmind }
324 1.21 rmind arg = argv[2];
325 1.15 rmind }
326 1.37 rmind
327 1.21 rmind again:
328 1.37 rmind switch (nct.nct_cmd) {
329 1.37 rmind case NPF_CMD_TABLE_LIST:
330 1.24 rmind nct.nct_data.buf.buf = ecalloc(1, buflen);
331 1.21 rmind nct.nct_data.buf.len = buflen;
332 1.37 rmind break;
333 1.37 rmind case NPF_CMD_TABLE_FLUSH:
334 1.37 rmind break;
335 1.37 rmind default:
336 1.21 rmind if (!npfctl_parse_cidr(arg, &fam, &alen)) {
337 1.21 rmind errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
338 1.21 rmind }
339 1.21 rmind nct.nct_data.ent.alen = alen;
340 1.26 rmind memcpy(&nct.nct_data.ent.addr, &fam.fam_addr, alen);
341 1.21 rmind nct.nct_data.ent.mask = fam.fam_mask;
342 1.15 rmind }
343 1.15 rmind
344 1.15 rmind if (ioctl(fd, IOC_NPF_TABLE, &nct) != -1) {
345 1.15 rmind errno = 0;
346 1.15 rmind }
347 1.15 rmind switch (errno) {
348 1.21 rmind case 0:
349 1.21 rmind break;
350 1.15 rmind case EEXIST:
351 1.21 rmind errx(EXIT_FAILURE, "entry already exists or is conflicting");
352 1.15 rmind case ENOENT:
353 1.57 rmind errx(EXIT_FAILURE, "not found");
354 1.15 rmind case EINVAL:
355 1.21 rmind errx(EXIT_FAILURE, "invalid address, mask or table ID");
356 1.21 rmind case ENOMEM:
357 1.29 rmind if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
358 1.21 rmind /* XXX */
359 1.21 rmind free(nct.nct_data.buf.buf);
360 1.21 rmind buflen <<= 1;
361 1.21 rmind goto again;
362 1.21 rmind }
363 1.21 rmind /* FALLTHROUGH */
364 1.21 rmind default:
365 1.32 christos err(EXIT_FAILURE, "ioctl(IOC_NPF_TABLE)");
366 1.21 rmind }
367 1.21 rmind
368 1.29 rmind if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
369 1.21 rmind npf_ioctl_ent_t *ent = nct.nct_data.buf.buf;
370 1.21 rmind char *buf;
371 1.21 rmind
372 1.21 rmind while (nct.nct_data.buf.len--) {
373 1.21 rmind if (!ent->alen)
374 1.21 rmind break;
375 1.50 christos buf = npfctl_print_addrmask(ent->alen, "%a",
376 1.21 rmind &ent->addr, ent->mask);
377 1.21 rmind puts(buf);
378 1.21 rmind ent++;
379 1.21 rmind }
380 1.21 rmind free(nct.nct_data.buf.buf);
381 1.21 rmind } else {
382 1.21 rmind printf("%s: %s\n", getprogname(),
383 1.29 rmind nct.nct_cmd == NPF_CMD_TABLE_LOOKUP ?
384 1.57 rmind "match" : "success");
385 1.15 rmind }
386 1.21 rmind exit(EXIT_SUCCESS);
387 1.15 rmind }
388 1.15 rmind
389 1.29 rmind static nl_rule_t *
390 1.29 rmind npfctl_parse_rule(int argc, char **argv)
391 1.29 rmind {
392 1.29 rmind char rule_string[1024];
393 1.29 rmind nl_rule_t *rl;
394 1.29 rmind
395 1.29 rmind /* Get the rule string and parse it. */
396 1.38 rmind if (!join(rule_string, sizeof(rule_string), argc, argv, " ")) {
397 1.29 rmind errx(EXIT_FAILURE, "command too long");
398 1.29 rmind }
399 1.29 rmind npfctl_parse_string(rule_string);
400 1.29 rmind if ((rl = npfctl_rule_ref()) == NULL) {
401 1.29 rmind errx(EXIT_FAILURE, "could not parse the rule");
402 1.29 rmind }
403 1.29 rmind return rl;
404 1.29 rmind }
405 1.29 rmind
406 1.48 christos #ifdef __NetBSD__
407 1.49 christos static unsigned char *
408 1.49 christos SHA1(const unsigned char *d, size_t l, unsigned char *md)
409 1.48 christos {
410 1.48 christos SHA1_CTX c;
411 1.48 christos
412 1.48 christos SHA1Init(&c);
413 1.48 christos SHA1Update(&c, d, l);
414 1.48 christos SHA1Final(md, &c);
415 1.48 christos return md;
416 1.47 christos }
417 1.48 christos #endif
418 1.47 christos
419 1.47 christos static void
420 1.29 rmind npfctl_generate_key(nl_rule_t *rl, void *key)
421 1.29 rmind {
422 1.29 rmind void *meta;
423 1.29 rmind size_t len;
424 1.29 rmind
425 1.29 rmind if ((meta = npf_rule_export(rl, &len)) == NULL) {
426 1.29 rmind errx(EXIT_FAILURE, "error generating rule key");
427 1.29 rmind }
428 1.48 christos __CTASSERT(NPF_RULE_MAXKEYLEN >= SHA_DIGEST_LENGTH);
429 1.29 rmind memset(key, 0, NPF_RULE_MAXKEYLEN);
430 1.48 christos SHA1(meta, len, key);
431 1.29 rmind free(meta);
432 1.29 rmind }
433 1.29 rmind
434 1.29 rmind __dead static void
435 1.29 rmind npfctl_rule(int fd, int argc, char **argv)
436 1.29 rmind {
437 1.29 rmind static const struct ruleops_s {
438 1.29 rmind const char * cmd;
439 1.29 rmind int action;
440 1.36 rmind bool extra_arg;
441 1.29 rmind } ruleops[] = {
442 1.36 rmind { "add", NPF_CMD_RULE_ADD, true },
443 1.36 rmind { "rem", NPF_CMD_RULE_REMKEY, true },
444 1.36 rmind { "del", NPF_CMD_RULE_REMKEY, true },
445 1.36 rmind { "rem-id", NPF_CMD_RULE_REMOVE, true },
446 1.36 rmind { "list", NPF_CMD_RULE_LIST, false },
447 1.36 rmind { "flush", NPF_CMD_RULE_FLUSH, false },
448 1.36 rmind { NULL, 0, 0 }
449 1.29 rmind };
450 1.29 rmind uint8_t key[NPF_RULE_MAXKEYLEN];
451 1.29 rmind const char *ruleset_name = argv[0];
452 1.29 rmind const char *cmd = argv[1];
453 1.29 rmind int error, action = 0;
454 1.31 rmind uint64_t rule_id;
455 1.36 rmind bool extra_arg;
456 1.29 rmind nl_rule_t *rl;
457 1.29 rmind
458 1.29 rmind for (int n = 0; ruleops[n].cmd != NULL; n++) {
459 1.29 rmind if (strcmp(cmd, ruleops[n].cmd) == 0) {
460 1.29 rmind action = ruleops[n].action;
461 1.36 rmind extra_arg = ruleops[n].extra_arg;
462 1.29 rmind break;
463 1.29 rmind }
464 1.29 rmind }
465 1.36 rmind argc -= 2;
466 1.36 rmind argv += 2;
467 1.29 rmind
468 1.36 rmind if (!action || (extra_arg && argc == 0)) {
469 1.29 rmind usage();
470 1.29 rmind }
471 1.29 rmind
472 1.29 rmind switch (action) {
473 1.29 rmind case NPF_CMD_RULE_ADD:
474 1.29 rmind rl = npfctl_parse_rule(argc, argv);
475 1.29 rmind npfctl_generate_key(rl, key);
476 1.29 rmind npf_rule_setkey(rl, key, sizeof(key));
477 1.29 rmind error = npf_ruleset_add(fd, ruleset_name, rl, &rule_id);
478 1.29 rmind break;
479 1.29 rmind case NPF_CMD_RULE_REMKEY:
480 1.29 rmind rl = npfctl_parse_rule(argc, argv);
481 1.29 rmind npfctl_generate_key(rl, key);
482 1.29 rmind error = npf_ruleset_remkey(fd, ruleset_name, key, sizeof(key));
483 1.29 rmind break;
484 1.29 rmind case NPF_CMD_RULE_REMOVE:
485 1.31 rmind rule_id = strtoull(argv[0], NULL, 16);
486 1.29 rmind error = npf_ruleset_remove(fd, ruleset_name, rule_id);
487 1.29 rmind break;
488 1.30 rmind case NPF_CMD_RULE_LIST:
489 1.30 rmind error = npfctl_ruleset_show(fd, ruleset_name);
490 1.30 rmind break;
491 1.30 rmind case NPF_CMD_RULE_FLUSH:
492 1.30 rmind error = npf_ruleset_flush(fd, ruleset_name);
493 1.30 rmind break;
494 1.29 rmind default:
495 1.48 christos abort();
496 1.29 rmind }
497 1.29 rmind
498 1.29 rmind switch (error) {
499 1.29 rmind case 0:
500 1.29 rmind /* Success. */
501 1.29 rmind break;
502 1.31 rmind case ESRCH:
503 1.31 rmind errx(EXIT_FAILURE, "ruleset \"%s\" not found", ruleset_name);
504 1.29 rmind case ENOENT:
505 1.31 rmind errx(EXIT_FAILURE, "rule was not found");
506 1.29 rmind default:
507 1.29 rmind errx(EXIT_FAILURE, "rule operation: %s", strerror(error));
508 1.29 rmind }
509 1.29 rmind if (action == NPF_CMD_RULE_ADD) {
510 1.31 rmind printf("OK %" PRIx64 "\n", rule_id);
511 1.29 rmind }
512 1.29 rmind exit(EXIT_SUCCESS);
513 1.29 rmind }
514 1.29 rmind
515 1.45 christos static bool bpfjit = true;
516 1.45 christos
517 1.45 christos void
518 1.45 christos npfctl_bpfjit(bool onoff)
519 1.45 christos {
520 1.45 christos bpfjit = onoff;
521 1.45 christos }
522 1.45 christos
523 1.44 rmind static void
524 1.44 rmind npfctl_preload_bpfjit(void)
525 1.44 rmind {
526 1.48 christos #ifdef __NetBSD__
527 1.44 rmind modctl_load_t args = {
528 1.44 rmind .ml_filename = "bpfjit",
529 1.44 rmind .ml_flags = MODCTL_NO_PROP,
530 1.44 rmind .ml_props = NULL,
531 1.44 rmind .ml_propslen = 0
532 1.44 rmind };
533 1.44 rmind
534 1.45 christos if (!bpfjit)
535 1.45 christos return;
536 1.45 christos
537 1.44 rmind if (modctl(MODCTL_LOAD, &args) != 0 && errno != EEXIST) {
538 1.45 christos static const char *p = "; performance will be degraded";
539 1.45 christos if (errno == ENOENT)
540 1.45 christos warnx("the bpfjit module seems to be missing%s", p);
541 1.45 christos else
542 1.45 christos warn("error loading the bpfjit module%s", p);
543 1.45 christos warnx("To disable this warning `set bpf.jit off' in "
544 1.45 christos "/etc/npf.conf");
545 1.44 rmind }
546 1.48 christos #endif
547 1.44 rmind }
548 1.44 rmind
549 1.41 rmind static int
550 1.48 christos npfctl_load(int fd)
551 1.41 rmind {
552 1.41 rmind nl_config_t *ncf;
553 1.48 christos npf_error_t errinfo;
554 1.48 christos struct stat sb;
555 1.48 christos size_t blen;
556 1.48 christos void *blob;
557 1.41 rmind int error;
558 1.41 rmind
559 1.48 christos /*
560 1.48 christos * The file may change while reading - we are not handling this,
561 1.48 christos * leaving this responsibility for the caller.
562 1.48 christos */
563 1.48 christos if (stat(NPF_DB_PATH, &sb) == -1) {
564 1.48 christos err(EXIT_FAILURE, "stat");
565 1.48 christos }
566 1.48 christos if ((blen = sb.st_size) == 0) {
567 1.48 christos err(EXIT_FAILURE, "saved configuration file is empty");
568 1.48 christos }
569 1.48 christos if ((blob = mmap(NULL, blen, PROT_READ,
570 1.48 christos MAP_FILE | MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
571 1.48 christos err(EXIT_FAILURE, "mmap");
572 1.48 christos }
573 1.48 christos ncf = npf_config_import(blob, blen);
574 1.48 christos munmap(blob, blen);
575 1.41 rmind if (ncf == NULL) {
576 1.41 rmind return errno;
577 1.41 rmind }
578 1.41 rmind
579 1.48 christos /*
580 1.48 christos * Configuration imported - submit it now.
581 1.48 christos **/
582 1.48 christos errno = error = npf_config_submit(ncf, fd, &errinfo);
583 1.43 rmind if (error) {
584 1.48 christos npfctl_print_error(&errinfo);
585 1.43 rmind }
586 1.41 rmind npf_config_destroy(ncf);
587 1.41 rmind return error;
588 1.41 rmind }
589 1.41 rmind
590 1.50 christos struct npf_conn_filter {
591 1.59 rmind uint16_t alen;
592 1.59 rmind const char * ifname;
593 1.59 rmind bool nat;
594 1.59 rmind bool wide;
595 1.59 rmind bool name;
596 1.59 rmind int width;
597 1.59 rmind FILE * fp;
598 1.50 christos };
599 1.50 christos
600 1.50 christos static int
601 1.52 rmind npfctl_conn_print(unsigned alen, const npf_addr_t *a, const in_port_t *p,
602 1.50 christos const char *ifname, void *v)
603 1.50 christos {
604 1.50 christos struct npf_conn_filter *fil = v;
605 1.50 christos FILE *fp = fil->fp;
606 1.50 christos char *src, *dst;
607 1.50 christos
608 1.50 christos if (fil->ifname && strcmp(ifname, fil->ifname) != 0)
609 1.50 christos return 0;
610 1.50 christos if (fil->alen && alen != fil->alen)
611 1.50 christos return 0;
612 1.50 christos if (fil->nat && !p[2])
613 1.50 christos return 0;
614 1.50 christos
615 1.50 christos int w = fil->width;
616 1.50 christos const char *fmt = fil->name ? "%A" :
617 1.50 christos (alen == sizeof(struct in_addr) ? "%a" : "[%a]");
618 1.50 christos src = npfctl_print_addrmask(alen, fmt, &a[0], NPF_NO_NETMASK);
619 1.50 christos dst = npfctl_print_addrmask(alen, fmt, &a[1], NPF_NO_NETMASK);
620 1.59 rmind
621 1.59 rmind if (fil->wide) {
622 1.50 christos fprintf(fp, "%s:%d %s:%d", src, p[0], dst, p[1]);
623 1.59 rmind } else {
624 1.50 christos fprintf(fp, "%*.*s:%-5d %*.*s:%-5d", w, w, src, p[0],
625 1.50 christos w, w, dst, p[1]);
626 1.59 rmind }
627 1.50 christos free(src);
628 1.50 christos free(dst);
629 1.50 christos if (!p[2]) {
630 1.50 christos fputc('\n', fp);
631 1.50 christos return 1;
632 1.50 christos }
633 1.54 ozaki fprintf(fp, " via %s:%d\n", ifname, p[2]);
634 1.50 christos return 1;
635 1.50 christos }
636 1.50 christos
637 1.50 christos static int
638 1.50 christos npfctl_conn_list(int fd, int argc, char **argv)
639 1.50 christos {
640 1.50 christos struct npf_conn_filter f;
641 1.59 rmind int c, w, header = true;
642 1.59 rmind
643 1.50 christos memset(&f, 0, sizeof(f));
644 1.50 christos argc--;
645 1.50 christos argv++;
646 1.50 christos
647 1.50 christos while ((c = getopt(argc, argv, "46hi:nNw")) != -1) {
648 1.50 christos switch (c) {
649 1.50 christos case '4':
650 1.50 christos f.alen = sizeof(struct in_addr);
651 1.50 christos break;
652 1.50 christos case '6':
653 1.50 christos f.alen = sizeof(struct in6_addr);
654 1.50 christos break;
655 1.50 christos case 'h':
656 1.50 christos header = false;
657 1.58 mrg break;
658 1.50 christos case 'i':
659 1.50 christos f.ifname = optarg;
660 1.50 christos break;
661 1.50 christos case 'n':
662 1.50 christos f.nat = true;
663 1.50 christos break;
664 1.50 christos case 'N':
665 1.50 christos f.name = true;
666 1.50 christos break;
667 1.50 christos case 'w':
668 1.50 christos f.wide = true;
669 1.50 christos break;
670 1.50 christos default:
671 1.50 christos fprintf(stderr,
672 1.50 christos "Usage: %s list [-46hnNw] [-i <ifname>]\n",
673 1.50 christos getprogname());
674 1.50 christos exit(EXIT_FAILURE);
675 1.50 christos }
676 1.50 christos }
677 1.50 christos f.width = f.alen == sizeof(struct in_addr) ? 25 : 41;
678 1.59 rmind w = f.width + 6;
679 1.50 christos f.fp = stdout;
680 1.59 rmind
681 1.59 rmind if (header) {
682 1.50 christos fprintf(f.fp, "%*.*s %*.*s\n",
683 1.50 christos w, w, "From address:port ", w, w, "To address:port ");
684 1.59 rmind }
685 1.50 christos npf_conn_list(fd, npfctl_conn_print, &f);
686 1.50 christos return 0;
687 1.50 christos }
688 1.50 christos
689 1.52 rmind static int
690 1.52 rmind npfctl_open_dev(const char *path)
691 1.1 rmind {
692 1.56 rmind int fd, kernver;
693 1.1 rmind
694 1.52 rmind fd = open(path, O_RDONLY);
695 1.1 rmind if (fd == -1) {
696 1.52 rmind err(EXIT_FAILURE, "cannot open '%s'", path);
697 1.1 rmind }
698 1.56 rmind if (ioctl(fd, IOC_NPF_VERSION, &kernver) == -1) {
699 1.32 christos err(EXIT_FAILURE, "ioctl(IOC_NPF_VERSION)");
700 1.15 rmind }
701 1.56 rmind if (kernver != NPF_VERSION) {
702 1.4 rmind errx(EXIT_FAILURE,
703 1.14 rmind "incompatible NPF interface version (%d, kernel %d)\n"
704 1.56 rmind "Hint: update %s?", NPF_VERSION, kernver,
705 1.56 rmind kernver > NPF_VERSION ? "userland" : "kernel");
706 1.1 rmind }
707 1.52 rmind return fd;
708 1.52 rmind }
709 1.52 rmind
710 1.52 rmind static void
711 1.52 rmind npfctl(int action, int argc, char **argv)
712 1.52 rmind {
713 1.52 rmind int fd, boolval, ret = 0;
714 1.52 rmind const char *fun = "";
715 1.52 rmind nl_config_t *ncf;
716 1.52 rmind
717 1.52 rmind switch (action) {
718 1.52 rmind case NPFCTL_VALIDATE:
719 1.52 rmind case NPFCTL_DEBUG:
720 1.52 rmind fd = 0;
721 1.52 rmind break;
722 1.52 rmind default:
723 1.52 rmind fd = npfctl_open_dev(NPF_DEV_PATH);
724 1.52 rmind }
725 1.29 rmind
726 1.1 rmind switch (action) {
727 1.1 rmind case NPFCTL_START:
728 1.1 rmind boolval = true;
729 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
730 1.32 christos fun = "ioctl(IOC_NPF_SWITCH)";
731 1.1 rmind break;
732 1.1 rmind case NPFCTL_STOP:
733 1.1 rmind boolval = false;
734 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
735 1.32 christos fun = "ioctl(IOC_NPF_SWITCH)";
736 1.1 rmind break;
737 1.1 rmind case NPFCTL_RELOAD:
738 1.8 rmind npfctl_config_init(false);
739 1.29 rmind npfctl_parse_file(argc < 3 ? NPF_CONF_PATH : argv[2]);
740 1.46 christos npfctl_preload_bpfjit();
741 1.56 rmind errno = ret = npfctl_config_send(fd);
742 1.32 christos fun = "npfctl_config_send";
743 1.1 rmind break;
744 1.11 rmind case NPFCTL_SHOWCONF:
745 1.11 rmind ret = npfctl_config_show(fd);
746 1.32 christos fun = "npfctl_config_show";
747 1.11 rmind break;
748 1.1 rmind case NPFCTL_FLUSH:
749 1.9 rmind ret = npf_config_flush(fd);
750 1.32 christos fun = "npf_config_flush";
751 1.1 rmind break;
752 1.1 rmind case NPFCTL_TABLE:
753 1.21 rmind if ((argc -= 2) < 2) {
754 1.1 rmind usage();
755 1.1 rmind }
756 1.21 rmind argv += 2;
757 1.21 rmind npfctl_table(fd, argc, argv);
758 1.1 rmind break;
759 1.29 rmind case NPFCTL_RULE:
760 1.29 rmind if ((argc -= 2) < 2) {
761 1.29 rmind usage();
762 1.29 rmind }
763 1.29 rmind argv += 2;
764 1.29 rmind npfctl_rule(fd, argc, argv);
765 1.29 rmind break;
766 1.41 rmind case NPFCTL_LOAD:
767 1.44 rmind npfctl_preload_bpfjit();
768 1.41 rmind ret = npfctl_load(fd);
769 1.41 rmind fun = "npfctl_config_load";
770 1.41 rmind break;
771 1.41 rmind case NPFCTL_SAVE:
772 1.48 christos ncf = npf_config_retrieve(fd);
773 1.48 christos if (ncf) {
774 1.48 christos npfctl_config_save(ncf, NPF_DB_PATH);
775 1.48 christos npf_config_destroy(ncf);
776 1.48 christos } else {
777 1.48 christos ret = errno;
778 1.48 christos }
779 1.41 rmind fun = "npfctl_config_save";
780 1.41 rmind break;
781 1.3 rmind case NPFCTL_STATS:
782 1.3 rmind ret = npfctl_print_stats(fd);
783 1.32 christos fun = "npfctl_print_stats";
784 1.3 rmind break;
785 1.50 christos case NPFCTL_CONN_LIST:
786 1.50 christos ret = npfctl_conn_list(fd, argc, argv);
787 1.50 christos fun = "npfctl_conn_list";
788 1.50 christos break;
789 1.52 rmind case NPFCTL_VALIDATE:
790 1.52 rmind npfctl_config_init(false);
791 1.52 rmind npfctl_parse_file(argc > 2 ? argv[2] : NPF_CONF_PATH);
792 1.52 rmind ret = npfctl_config_show(0);
793 1.52 rmind fun = "npfctl_config_show";
794 1.52 rmind break;
795 1.52 rmind case NPFCTL_DEBUG:
796 1.52 rmind npfctl_config_init(true);
797 1.52 rmind npfctl_parse_file(argc > 2 ? argv[2] : NPF_CONF_PATH);
798 1.56 rmind npfctl_config_debug(argc > 3 ? argv[3] : "/tmp/npf.nvlist");
799 1.52 rmind break;
800 1.1 rmind }
801 1.7 zoltan if (ret) {
802 1.32 christos err(EXIT_FAILURE, "%s", fun);
803 1.1 rmind }
804 1.52 rmind if (fd) {
805 1.52 rmind close(fd);
806 1.52 rmind }
807 1.1 rmind }
808 1.1 rmind
809 1.1 rmind int
810 1.1 rmind main(int argc, char **argv)
811 1.1 rmind {
812 1.1 rmind char *cmd;
813 1.1 rmind
814 1.1 rmind if (argc < 2) {
815 1.1 rmind usage();
816 1.1 rmind }
817 1.48 christos npfctl_show_init();
818 1.1 rmind cmd = argv[1];
819 1.1 rmind
820 1.12 rmind /* Find and call the subroutine. */
821 1.8 rmind for (int n = 0; operations[n].cmd != NULL; n++) {
822 1.25 rmind const char *opcmd = operations[n].cmd;
823 1.25 rmind if (strncmp(cmd, opcmd, strlen(opcmd)) != 0)
824 1.1 rmind continue;
825 1.1 rmind npfctl(operations[n].action, argc, argv);
826 1.8 rmind return EXIT_SUCCESS;
827 1.1 rmind }
828 1.3 rmind usage();
829 1.1 rmind }
830