npfctl.c revision 1.10.2.14 1 1.10.2.14 riz /* $NetBSD: npfctl.c,v 1.10.2.14 2013/02/11 21:49:48 riz Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.10.2.14 riz * Copyright (c) 2009-2013 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.2.14 riz __RCSID("$NetBSD: npfctl.c,v 1.10.2.14 2013/02/11 21:49:48 riz 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.10.2.3 riz #include <errno.h>
46 1.1 rmind
47 1.10.2.14 riz #include <openssl/sha.h>
48 1.10.2.14 riz
49 1.1 rmind #include "npfctl.h"
50 1.1 rmind
51 1.10.2.14 riz extern void npf_yyparse_string(const char *);
52 1.8 rmind
53 1.10.2.1 riz enum {
54 1.10.2.1 riz NPFCTL_START,
55 1.10.2.1 riz NPFCTL_STOP,
56 1.10.2.1 riz NPFCTL_RELOAD,
57 1.10.2.1 riz NPFCTL_SHOWCONF,
58 1.10.2.1 riz NPFCTL_FLUSH,
59 1.10.2.10 riz NPFCTL_VALIDATE,
60 1.10.2.1 riz NPFCTL_TABLE,
61 1.10.2.14 riz NPFCTL_RULE,
62 1.10.2.1 riz NPFCTL_STATS,
63 1.10.2.1 riz NPFCTL_SESSIONS_SAVE,
64 1.10.2.1 riz NPFCTL_SESSIONS_LOAD,
65 1.10.2.1 riz };
66 1.1 rmind
67 1.10.2.3 riz static const struct operations_s {
68 1.1 rmind const char * cmd;
69 1.1 rmind int action;
70 1.1 rmind } operations[] = {
71 1.1 rmind /* Start, stop, reload */
72 1.3 rmind { "start", NPFCTL_START },
73 1.3 rmind { "stop", NPFCTL_STOP },
74 1.3 rmind { "reload", NPFCTL_RELOAD },
75 1.10.2.1 riz { "show", NPFCTL_SHOWCONF, },
76 1.3 rmind { "flush", NPFCTL_FLUSH },
77 1.10.2.10 riz { "valid", NPFCTL_VALIDATE },
78 1.1 rmind /* Table */
79 1.3 rmind { "table", NPFCTL_TABLE },
80 1.10.2.14 riz /* Rule */
81 1.10.2.14 riz { "rule", NPFCTL_RULE },
82 1.3 rmind /* Stats */
83 1.3 rmind { "stats", NPFCTL_STATS },
84 1.3 rmind /* Sessions */
85 1.3 rmind { "sess-save", NPFCTL_SESSIONS_SAVE },
86 1.3 rmind { "sess-load", NPFCTL_SESSIONS_LOAD },
87 1.1 rmind /* --- */
88 1.3 rmind { NULL, 0 }
89 1.1 rmind };
90 1.1 rmind
91 1.10.2.14 riz static bool
92 1.10.2.14 riz join(char *buf, size_t buflen, int count, char **args)
93 1.10.2.14 riz {
94 1.10.2.14 riz char *s = buf, *p = NULL;
95 1.10.2.14 riz
96 1.10.2.14 riz for (int i = 0; i < count; i++) {
97 1.10.2.14 riz size_t len;
98 1.10.2.14 riz
99 1.10.2.14 riz p = stpncpy(s, args[i], buflen);
100 1.10.2.14 riz len = p - s + 1;
101 1.10.2.14 riz if (len >= buflen) {
102 1.10.2.14 riz return false;
103 1.10.2.14 riz }
104 1.10.2.14 riz buflen -= len;
105 1.10.2.14 riz *p = ' ';
106 1.10.2.14 riz s = p + 1;
107 1.10.2.14 riz }
108 1.10.2.14 riz *p = '\0';
109 1.10.2.14 riz return true;
110 1.10.2.14 riz }
111 1.10.2.14 riz
112 1.6 joerg __dead static void
113 1.1 rmind usage(void)
114 1.1 rmind {
115 1.1 rmind const char *progname = getprogname();
116 1.1 rmind
117 1.1 rmind fprintf(stderr,
118 1.10.2.2 riz "usage:\t%s [ start | stop | reload | flush | show | stats ]\n",
119 1.3 rmind progname);
120 1.3 rmind fprintf(stderr,
121 1.10.2.14 riz "\t%s rule \"rule-name\" { add | rem } <rule-syntax>\n",
122 1.10.2.14 riz progname);
123 1.10.2.14 riz fprintf(stderr,
124 1.10.2.14 riz "\t%s rule \"rule-name\" rem-id <rule-id>\n",
125 1.1 rmind progname);
126 1.1 rmind fprintf(stderr,
127 1.10.2.8 riz "\t%s table <tid> { add | rem | test } <address/mask>\n",
128 1.1 rmind progname);
129 1.1 rmind fprintf(stderr,
130 1.10.2.8 riz "\t%s table <tid> { list | flush }\n",
131 1.1 rmind progname);
132 1.10.2.14 riz fprintf(stderr,
133 1.10.2.14 riz "\t%s ( sess-save | sess-load )\n",
134 1.10.2.14 riz progname);
135 1.1 rmind exit(EXIT_FAILURE);
136 1.1 rmind }
137 1.1 rmind
138 1.3 rmind static int
139 1.3 rmind npfctl_print_stats(int fd)
140 1.3 rmind {
141 1.10.2.4 jdc static const struct stats_s {
142 1.10.2.4 jdc /* Note: -1 indicates a new section. */
143 1.10.2.4 jdc int index;
144 1.10.2.4 jdc const char * name;
145 1.10.2.4 jdc } stats[] = {
146 1.10.2.4 jdc { -1, "Packets passed" },
147 1.10.2.4 jdc { NPF_STAT_PASS_DEFAULT, "default pass" },
148 1.10.2.4 jdc { NPF_STAT_PASS_RULESET, "ruleset pass" },
149 1.10.2.4 jdc { NPF_STAT_PASS_SESSION, "session pass" },
150 1.10.2.4 jdc
151 1.10.2.4 jdc { -1, "Packets blocked" },
152 1.10.2.4 jdc { NPF_STAT_BLOCK_DEFAULT, "default block" },
153 1.10.2.4 jdc { NPF_STAT_BLOCK_RULESET, "ruleset block" },
154 1.10.2.4 jdc
155 1.10.2.4 jdc { -1, "Session and NAT entries" },
156 1.10.2.4 jdc { NPF_STAT_SESSION_CREATE, "session allocations" },
157 1.10.2.4 jdc { NPF_STAT_SESSION_DESTROY, "session destructions" },
158 1.10.2.4 jdc { NPF_STAT_NAT_CREATE, "NAT entry allocations" },
159 1.10.2.4 jdc { NPF_STAT_NAT_DESTROY, "NAT entry destructions"},
160 1.10.2.4 jdc
161 1.10.2.13 riz { -1, "Network buffers" },
162 1.10.2.13 riz { NPF_STAT_NBUF_NONCONTIG, "non-contiguous cases" },
163 1.10.2.13 riz { NPF_STAT_NBUF_CONTIG_FAIL, "contig alloc failures" },
164 1.10.2.13 riz
165 1.10.2.4 jdc { -1, "Invalid packet state cases" },
166 1.10.2.4 jdc { NPF_STAT_INVALID_STATE, "cases in total" },
167 1.10.2.4 jdc { NPF_STAT_INVALID_STATE_TCP1, "TCP case I" },
168 1.10.2.4 jdc { NPF_STAT_INVALID_STATE_TCP2, "TCP case II" },
169 1.10.2.4 jdc { NPF_STAT_INVALID_STATE_TCP3, "TCP case III" },
170 1.10.2.4 jdc
171 1.10.2.4 jdc { -1, "Packet race cases" },
172 1.10.2.4 jdc { NPF_STAT_RACE_NAT, "NAT association race" },
173 1.10.2.4 jdc { NPF_STAT_RACE_SESSION, "duplicate session race"},
174 1.10.2.4 jdc
175 1.10.2.4 jdc { -1, "Fragmentation" },
176 1.10.2.4 jdc { NPF_STAT_FRAGMENTS, "fragments" },
177 1.10.2.4 jdc { NPF_STAT_REASSEMBLY, "reassembled" },
178 1.10.2.4 jdc { NPF_STAT_REASSFAIL, "failed reassembly" },
179 1.10.2.4 jdc
180 1.10.2.4 jdc { -1, "Other" },
181 1.10.2.4 jdc { NPF_STAT_ERROR, "unexpected errors" },
182 1.10.2.4 jdc };
183 1.10.2.9 riz uint64_t *st = ecalloc(1, NPF_STATS_SIZE);
184 1.3 rmind
185 1.3 rmind if (ioctl(fd, IOC_NPF_STATS, &st) != 0) {
186 1.3 rmind err(EXIT_FAILURE, "ioctl(IOC_NPF_STATS)");
187 1.3 rmind }
188 1.3 rmind
189 1.10.2.4 jdc for (unsigned i = 0; i < __arraycount(stats); i++) {
190 1.10.2.4 jdc const char *sname = stats[i].name;
191 1.10.2.4 jdc int sidx = stats[i].index;
192 1.10.2.4 jdc
193 1.10.2.4 jdc if (sidx == -1) {
194 1.10.2.4 jdc printf("%s:\n", sname);
195 1.10.2.4 jdc } else {
196 1.10.2.4 jdc printf("\t%"PRIu64" %s\n", st[sidx], sname);
197 1.10.2.4 jdc }
198 1.10.2.4 jdc }
199 1.4 rmind
200 1.3 rmind free(st);
201 1.3 rmind return 0;
202 1.3 rmind }
203 1.3 rmind
204 1.10 rmind void
205 1.10 rmind npfctl_print_error(const nl_error_t *ne)
206 1.10 rmind {
207 1.10 rmind static const char *ncode_errors[] = {
208 1.10 rmind [-NPF_ERR_OPCODE] = "invalid instruction",
209 1.10 rmind [-NPF_ERR_JUMP] = "invalid jump",
210 1.10 rmind [-NPF_ERR_REG] = "invalid register",
211 1.10 rmind [-NPF_ERR_INVAL] = "invalid argument value",
212 1.10 rmind [-NPF_ERR_RANGE] = "processing out of range"
213 1.10 rmind };
214 1.10 rmind const int nc_err = ne->ne_ncode_error;
215 1.10 rmind const char *srcfile = ne->ne_source_file;
216 1.10 rmind
217 1.10 rmind if (srcfile) {
218 1.10 rmind warnx("source %s line %d", srcfile, ne->ne_source_line);
219 1.10 rmind }
220 1.10 rmind if (nc_err) {
221 1.10 rmind warnx("n-code error (%d): %s at offset 0x%x",
222 1.10 rmind nc_err, ncode_errors[-nc_err], ne->ne_ncode_errat);
223 1.10 rmind }
224 1.10 rmind if (ne->ne_id) {
225 1.10 rmind warnx("object: %d", ne->ne_id);
226 1.10 rmind }
227 1.10 rmind }
228 1.10 rmind
229 1.10.2.8 riz char *
230 1.10.2.8 riz npfctl_print_addrmask(int alen, npf_addr_t *addr, npf_netmask_t mask)
231 1.10.2.8 riz {
232 1.10.2.8 riz struct sockaddr_storage ss;
233 1.10.2.9 riz char *buf = ecalloc(1, 64);
234 1.10.2.8 riz int len;
235 1.10.2.8 riz
236 1.10.2.8 riz switch (alen) {
237 1.10.2.8 riz case 4: {
238 1.10.2.8 riz struct sockaddr_in *sin = (void *)&ss;
239 1.10.2.8 riz sin->sin_len = sizeof(*sin);
240 1.10.2.8 riz sin->sin_family = AF_INET;
241 1.10.2.8 riz sin->sin_port = 0;
242 1.10.2.8 riz memcpy(&sin->sin_addr, addr, sizeof(sin->sin_addr));
243 1.10.2.8 riz break;
244 1.10.2.8 riz }
245 1.10.2.8 riz case 16: {
246 1.10.2.8 riz struct sockaddr_in6 *sin6 = (void *)&ss;
247 1.10.2.8 riz sin6->sin6_len = sizeof(*sin6);
248 1.10.2.8 riz sin6->sin6_family = AF_INET6;
249 1.10.2.8 riz sin6->sin6_port = 0;
250 1.10.2.14 riz sin6->sin6_scope_id = 0;
251 1.10.2.8 riz memcpy(&sin6->sin6_addr, addr, sizeof(sin6->sin6_addr));
252 1.10.2.8 riz break;
253 1.10.2.8 riz }
254 1.10.2.8 riz default:
255 1.10.2.8 riz assert(false);
256 1.10.2.8 riz }
257 1.10.2.8 riz len = sockaddr_snprintf(buf, 64, "%a", (struct sockaddr *)&ss);
258 1.10.2.8 riz if (mask) {
259 1.10.2.8 riz snprintf(&buf[len], 64 - len, "/%u", mask);
260 1.10.2.8 riz }
261 1.10.2.8 riz return buf;
262 1.10.2.8 riz }
263 1.10.2.8 riz
264 1.10.2.4 jdc __dead static void
265 1.10.2.8 riz npfctl_table(int fd, int argc, char **argv)
266 1.10.2.3 riz {
267 1.10.2.3 riz static const struct tblops_s {
268 1.10.2.3 riz const char * cmd;
269 1.10.2.3 riz int action;
270 1.10.2.3 riz } tblops[] = {
271 1.10.2.14 riz { "add", NPF_CMD_TABLE_ADD },
272 1.10.2.14 riz { "rem", NPF_CMD_TABLE_REMOVE },
273 1.10.2.14 riz { "del", NPF_CMD_TABLE_REMOVE },
274 1.10.2.14 riz { "test", NPF_CMD_TABLE_LOOKUP },
275 1.10.2.14 riz { "list", NPF_CMD_TABLE_LIST },
276 1.10.2.8 riz { NULL, 0 }
277 1.10.2.3 riz };
278 1.10.2.3 riz npf_ioctl_table_t nct;
279 1.10.2.3 riz fam_addr_mask_t fam;
280 1.10.2.8 riz size_t buflen = 512;
281 1.10.2.9 riz char *cmd, *arg = NULL; /* XXX gcc */
282 1.10.2.3 riz int n, alen;
283 1.10.2.3 riz
284 1.10.2.8 riz /* Default action is list. */
285 1.10.2.3 riz memset(&nct, 0, sizeof(npf_ioctl_table_t));
286 1.10.2.8 riz nct.nct_tid = atoi(argv[0]);
287 1.10.2.8 riz cmd = argv[1];
288 1.10.2.3 riz
289 1.10.2.3 riz for (n = 0; tblops[n].cmd != NULL; n++) {
290 1.10.2.8 riz if (strcmp(cmd, tblops[n].cmd) != 0) {
291 1.10.2.8 riz continue;
292 1.10.2.8 riz }
293 1.10.2.14 riz nct.nct_cmd = tblops[n].action;
294 1.10.2.8 riz break;
295 1.10.2.8 riz }
296 1.10.2.8 riz if (tblops[n].cmd == NULL) {
297 1.10.2.8 riz errx(EXIT_FAILURE, "invalid command '%s'", cmd);
298 1.10.2.8 riz }
299 1.10.2.14 riz if (nct.nct_cmd != NPF_CMD_TABLE_LIST) {
300 1.10.2.8 riz if (argc < 3) {
301 1.10.2.8 riz usage();
302 1.10.2.3 riz }
303 1.10.2.8 riz arg = argv[2];
304 1.10.2.3 riz }
305 1.10.2.8 riz again:
306 1.10.2.14 riz if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
307 1.10.2.9 riz nct.nct_data.buf.buf = ecalloc(1, buflen);
308 1.10.2.8 riz nct.nct_data.buf.len = buflen;
309 1.10.2.8 riz } else {
310 1.10.2.8 riz if (!npfctl_parse_cidr(arg, &fam, &alen)) {
311 1.10.2.8 riz errx(EXIT_FAILURE, "invalid CIDR '%s'", arg);
312 1.10.2.8 riz }
313 1.10.2.8 riz nct.nct_data.ent.alen = alen;
314 1.10.2.11 riz memcpy(&nct.nct_data.ent.addr, &fam.fam_addr, alen);
315 1.10.2.8 riz nct.nct_data.ent.mask = fam.fam_mask;
316 1.10.2.3 riz }
317 1.10.2.3 riz
318 1.10.2.3 riz if (ioctl(fd, IOC_NPF_TABLE, &nct) != -1) {
319 1.10.2.3 riz errno = 0;
320 1.10.2.3 riz }
321 1.10.2.3 riz switch (errno) {
322 1.10.2.8 riz case 0:
323 1.10.2.3 riz break;
324 1.10.2.8 riz case EEXIST:
325 1.10.2.8 riz errx(EXIT_FAILURE, "entry already exists or is conflicting");
326 1.10.2.3 riz case ENOENT:
327 1.10.2.8 riz errx(EXIT_FAILURE, "no matching entry was not found");
328 1.10.2.3 riz case EINVAL:
329 1.10.2.8 riz errx(EXIT_FAILURE, "invalid address, mask or table ID");
330 1.10.2.8 riz case ENOMEM:
331 1.10.2.14 riz if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
332 1.10.2.8 riz /* XXX */
333 1.10.2.8 riz free(nct.nct_data.buf.buf);
334 1.10.2.8 riz buflen <<= 1;
335 1.10.2.8 riz goto again;
336 1.10.2.8 riz }
337 1.10.2.8 riz /* FALLTHROUGH */
338 1.10.2.3 riz default:
339 1.10.2.8 riz err(EXIT_FAILURE, "ioctl");
340 1.10.2.8 riz }
341 1.10.2.8 riz
342 1.10.2.14 riz if (nct.nct_cmd == NPF_CMD_TABLE_LIST) {
343 1.10.2.8 riz npf_ioctl_ent_t *ent = nct.nct_data.buf.buf;
344 1.10.2.8 riz char *buf;
345 1.10.2.8 riz
346 1.10.2.8 riz while (nct.nct_data.buf.len--) {
347 1.10.2.8 riz if (!ent->alen)
348 1.10.2.8 riz break;
349 1.10.2.8 riz buf = npfctl_print_addrmask(ent->alen,
350 1.10.2.8 riz &ent->addr, ent->mask);
351 1.10.2.8 riz puts(buf);
352 1.10.2.8 riz ent++;
353 1.10.2.8 riz }
354 1.10.2.8 riz free(nct.nct_data.buf.buf);
355 1.10.2.8 riz } else {
356 1.10.2.8 riz printf("%s: %s\n", getprogname(),
357 1.10.2.14 riz nct.nct_cmd == NPF_CMD_TABLE_LOOKUP ?
358 1.10.2.8 riz "matching entry found" : "success");
359 1.10.2.3 riz }
360 1.10.2.8 riz exit(EXIT_SUCCESS);
361 1.10.2.3 riz }
362 1.10.2.3 riz
363 1.10.2.14 riz static nl_rule_t *
364 1.10.2.14 riz npfctl_parse_rule(int argc, char **argv)
365 1.10.2.14 riz {
366 1.10.2.14 riz char rule_string[1024];
367 1.10.2.14 riz nl_rule_t *rl;
368 1.10.2.14 riz
369 1.10.2.14 riz /* Get the rule string and parse it. */
370 1.10.2.14 riz if (!join(rule_string, sizeof(rule_string), argc, argv)) {
371 1.10.2.14 riz errx(EXIT_FAILURE, "command too long");
372 1.10.2.14 riz }
373 1.10.2.14 riz npfctl_parse_string(rule_string);
374 1.10.2.14 riz if ((rl = npfctl_rule_ref()) == NULL) {
375 1.10.2.14 riz errx(EXIT_FAILURE, "could not parse the rule");
376 1.10.2.14 riz }
377 1.10.2.14 riz return rl;
378 1.10.2.14 riz }
379 1.10.2.14 riz
380 1.10.2.14 riz static void
381 1.10.2.14 riz npfctl_generate_key(nl_rule_t *rl, void *key)
382 1.10.2.14 riz {
383 1.10.2.14 riz void *meta;
384 1.10.2.14 riz size_t len;
385 1.10.2.14 riz
386 1.10.2.14 riz if ((meta = npf_rule_export(rl, &len)) == NULL) {
387 1.10.2.14 riz errx(EXIT_FAILURE, "error generating rule key");
388 1.10.2.14 riz }
389 1.10.2.14 riz __CTASSERT(NPF_RULE_MAXKEYLEN >= SHA_DIGEST_LENGTH);
390 1.10.2.14 riz memset(key, 0, NPF_RULE_MAXKEYLEN);
391 1.10.2.14 riz SHA1(meta, len, key);
392 1.10.2.14 riz free(meta);
393 1.10.2.14 riz }
394 1.10.2.14 riz
395 1.10.2.14 riz __dead static void
396 1.10.2.14 riz npfctl_rule(int fd, int argc, char **argv)
397 1.10.2.14 riz {
398 1.10.2.14 riz static const struct ruleops_s {
399 1.10.2.14 riz const char * cmd;
400 1.10.2.14 riz int action;
401 1.10.2.14 riz } ruleops[] = {
402 1.10.2.14 riz { "add", NPF_CMD_RULE_ADD },
403 1.10.2.14 riz { "rem", NPF_CMD_RULE_REMKEY },
404 1.10.2.14 riz { "del", NPF_CMD_RULE_REMKEY },
405 1.10.2.14 riz { "rem-id", NPF_CMD_RULE_REMOVE },
406 1.10.2.14 riz { "list", NPF_CMD_RULE_LIST },
407 1.10.2.14 riz { "flush", NPF_CMD_RULE_FLUSH },
408 1.10.2.14 riz { NULL, 0 }
409 1.10.2.14 riz };
410 1.10.2.14 riz uint8_t key[NPF_RULE_MAXKEYLEN];
411 1.10.2.14 riz const char *ruleset_name = argv[0];
412 1.10.2.14 riz const char *cmd = argv[1];
413 1.10.2.14 riz int error, action = 0;
414 1.10.2.14 riz uintptr_t rule_id;
415 1.10.2.14 riz nl_rule_t *rl;
416 1.10.2.14 riz
417 1.10.2.14 riz for (int n = 0; ruleops[n].cmd != NULL; n++) {
418 1.10.2.14 riz if (strcmp(cmd, ruleops[n].cmd) == 0) {
419 1.10.2.14 riz action = ruleops[n].action;
420 1.10.2.14 riz break;
421 1.10.2.14 riz }
422 1.10.2.14 riz }
423 1.10.2.14 riz
424 1.10.2.14 riz bool narg = action == NPF_CMD_RULE_LIST || action == NPF_CMD_RULE_FLUSH;
425 1.10.2.14 riz if (!action || (argc < 3 && !narg)) {
426 1.10.2.14 riz usage();
427 1.10.2.14 riz }
428 1.10.2.14 riz argc -= 2;
429 1.10.2.14 riz argv += 2;
430 1.10.2.14 riz
431 1.10.2.14 riz switch (action) {
432 1.10.2.14 riz case NPF_CMD_RULE_ADD:
433 1.10.2.14 riz rl = npfctl_parse_rule(argc, argv);
434 1.10.2.14 riz npfctl_generate_key(rl, key);
435 1.10.2.14 riz npf_rule_setkey(rl, key, sizeof(key));
436 1.10.2.14 riz error = npf_ruleset_add(fd, ruleset_name, rl, &rule_id);
437 1.10.2.14 riz break;
438 1.10.2.14 riz case NPF_CMD_RULE_REMKEY:
439 1.10.2.14 riz rl = npfctl_parse_rule(argc, argv);
440 1.10.2.14 riz npfctl_generate_key(rl, key);
441 1.10.2.14 riz error = npf_ruleset_remkey(fd, ruleset_name, key, sizeof(key));
442 1.10.2.14 riz break;
443 1.10.2.14 riz case NPF_CMD_RULE_REMOVE:
444 1.10.2.14 riz rule_id = (uintptr_t)strtoull(argv[0], NULL, 16);
445 1.10.2.14 riz error = npf_ruleset_remove(fd, ruleset_name, rule_id);
446 1.10.2.14 riz break;
447 1.10.2.14 riz case NPF_CMD_RULE_LIST:
448 1.10.2.14 riz error = npfctl_ruleset_show(fd, ruleset_name);
449 1.10.2.14 riz break;
450 1.10.2.14 riz case NPF_CMD_RULE_FLUSH:
451 1.10.2.14 riz error = npf_ruleset_flush(fd, ruleset_name);
452 1.10.2.14 riz break;
453 1.10.2.14 riz default:
454 1.10.2.14 riz assert(false);
455 1.10.2.14 riz }
456 1.10.2.14 riz
457 1.10.2.14 riz switch (error) {
458 1.10.2.14 riz case 0:
459 1.10.2.14 riz /* Success. */
460 1.10.2.14 riz break;
461 1.10.2.14 riz case ENOENT:
462 1.10.2.14 riz errx(EXIT_FAILURE, "ruleset \"%s\" or the specified rule in "
463 1.10.2.14 riz "it not found", ruleset_name);
464 1.10.2.14 riz break;
465 1.10.2.14 riz default:
466 1.10.2.14 riz errx(EXIT_FAILURE, "rule operation: %s", strerror(error));
467 1.10.2.14 riz }
468 1.10.2.14 riz if (action == NPF_CMD_RULE_ADD) {
469 1.10.2.14 riz printf("OK %" PRIXPTR "\n", rule_id);
470 1.10.2.14 riz }
471 1.10.2.14 riz exit(EXIT_SUCCESS);
472 1.10.2.14 riz }
473 1.10.2.14 riz
474 1.10.2.3 riz static void
475 1.1 rmind npfctl(int action, int argc, char **argv)
476 1.1 rmind {
477 1.10.2.14 riz int fd, ver, boolval, ret = 0;
478 1.1 rmind
479 1.1 rmind fd = open(NPF_DEV_PATH, O_RDONLY);
480 1.1 rmind if (fd == -1) {
481 1.4 rmind err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
482 1.1 rmind }
483 1.10.2.14 riz if (ioctl(fd, IOC_NPF_VERSION, &ver) == -1) {
484 1.10.2.3 riz err(EXIT_FAILURE, "ioctl");
485 1.10.2.3 riz }
486 1.1 rmind if (ver != NPF_VERSION) {
487 1.4 rmind errx(EXIT_FAILURE,
488 1.10.2.2 riz "incompatible NPF interface version (%d, kernel %d)\n"
489 1.10.2.2 riz "Hint: update userland?", NPF_VERSION, ver);
490 1.1 rmind }
491 1.10.2.14 riz
492 1.1 rmind switch (action) {
493 1.1 rmind case NPFCTL_START:
494 1.1 rmind boolval = true;
495 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
496 1.1 rmind break;
497 1.1 rmind case NPFCTL_STOP:
498 1.1 rmind boolval = false;
499 1.1 rmind ret = ioctl(fd, IOC_NPF_SWITCH, &boolval);
500 1.1 rmind break;
501 1.1 rmind case NPFCTL_RELOAD:
502 1.8 rmind npfctl_config_init(false);
503 1.10.2.14 riz npfctl_parse_file(argc < 3 ? NPF_CONF_PATH : argv[2]);
504 1.10.2.5 riz ret = npfctl_config_send(fd, NULL);
505 1.10.2.3 riz if (ret) {
506 1.10.2.3 riz errx(EXIT_FAILURE, "ioctl: %s", strerror(ret));
507 1.10.2.3 riz }
508 1.1 rmind break;
509 1.10.2.1 riz case NPFCTL_SHOWCONF:
510 1.10.2.1 riz ret = npfctl_config_show(fd);
511 1.10.2.1 riz break;
512 1.1 rmind case NPFCTL_FLUSH:
513 1.9 rmind ret = npf_config_flush(fd);
514 1.1 rmind break;
515 1.10.2.10 riz case NPFCTL_VALIDATE:
516 1.10.2.10 riz npfctl_config_init(false);
517 1.10.2.14 riz npfctl_parse_file(argc < 3 ? NPF_CONF_PATH : argv[2]);
518 1.10.2.10 riz ret = npfctl_config_show(0);
519 1.10.2.10 riz break;
520 1.1 rmind case NPFCTL_TABLE:
521 1.10.2.8 riz if ((argc -= 2) < 2) {
522 1.1 rmind usage();
523 1.1 rmind }
524 1.10.2.8 riz argv += 2;
525 1.10.2.8 riz npfctl_table(fd, argc, argv);
526 1.1 rmind break;
527 1.10.2.14 riz case NPFCTL_RULE:
528 1.10.2.14 riz if ((argc -= 2) < 2) {
529 1.10.2.14 riz usage();
530 1.10.2.14 riz }
531 1.10.2.14 riz argv += 2;
532 1.10.2.14 riz npfctl_rule(fd, argc, argv);
533 1.10.2.14 riz break;
534 1.3 rmind case NPFCTL_STATS:
535 1.3 rmind ret = npfctl_print_stats(fd);
536 1.3 rmind break;
537 1.3 rmind case NPFCTL_SESSIONS_SAVE:
538 1.10.2.3 riz if (npf_sessions_recv(fd, NPF_SESSDB_PATH) != 0) {
539 1.5 rmind errx(EXIT_FAILURE, "could not save sessions to '%s'",
540 1.5 rmind NPF_SESSDB_PATH);
541 1.5 rmind }
542 1.3 rmind break;
543 1.3 rmind case NPFCTL_SESSIONS_LOAD:
544 1.10.2.3 riz if (npf_sessions_send(fd, NPF_SESSDB_PATH) != 0) {
545 1.5 rmind errx(EXIT_FAILURE, "no sessions loaded from '%s'",
546 1.5 rmind NPF_SESSDB_PATH);
547 1.5 rmind }
548 1.3 rmind break;
549 1.1 rmind }
550 1.7 zoltan if (ret) {
551 1.1 rmind err(EXIT_FAILURE, "ioctl");
552 1.1 rmind }
553 1.1 rmind close(fd);
554 1.1 rmind }
555 1.1 rmind
556 1.1 rmind int
557 1.1 rmind main(int argc, char **argv)
558 1.1 rmind {
559 1.1 rmind char *cmd;
560 1.1 rmind
561 1.1 rmind if (argc < 2) {
562 1.1 rmind usage();
563 1.1 rmind }
564 1.1 rmind cmd = argv[1];
565 1.1 rmind
566 1.8 rmind if (strcmp(cmd, "debug") == 0) {
567 1.10.2.2 riz const char *cfg = argc > 2 ? argv[2] : "/etc/npf.conf";
568 1.10.2.5 riz const char *out = argc > 3 ? argv[3] : "/tmp/npf.plist";
569 1.10.2.5 riz
570 1.8 rmind npfctl_config_init(true);
571 1.10.2.14 riz npfctl_parse_file(cfg);
572 1.10.2.5 riz npfctl_config_send(0, out);
573 1.8 rmind return EXIT_SUCCESS;
574 1.8 rmind }
575 1.4 rmind
576 1.10.2.1 riz /* Find and call the subroutine. */
577 1.8 rmind for (int n = 0; operations[n].cmd != NULL; n++) {
578 1.10.2.10 riz const char *opcmd = operations[n].cmd;
579 1.10.2.10 riz if (strncmp(cmd, opcmd, strlen(opcmd)) != 0)
580 1.1 rmind continue;
581 1.1 rmind npfctl(operations[n].action, argc, argv);
582 1.8 rmind return EXIT_SUCCESS;
583 1.1 rmind }
584 1.3 rmind usage();
585 1.1 rmind }
586