xref: /freebsd/sbin/pfilctl/pfilctl.c (revision 1bfe195143ffb6832ac6702e281964541554fcb3)
1b252313fSGleb Smirnoff /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3b252313fSGleb Smirnoff  *
4b252313fSGleb Smirnoff  * Copyright (c) 2019 Gleb Smirnoff <glebius@FreeBSD.org>
5b252313fSGleb Smirnoff  *
6b252313fSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
7b252313fSGleb Smirnoff  * modification, are permitted provided that the following conditions
8b252313fSGleb Smirnoff  * are met:
9b252313fSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
10b252313fSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
11b252313fSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
12b252313fSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
13b252313fSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
14b252313fSGleb Smirnoff  *
15b252313fSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16b252313fSGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b252313fSGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b252313fSGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19b252313fSGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20b252313fSGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21b252313fSGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22b252313fSGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23b252313fSGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b252313fSGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b252313fSGleb Smirnoff  * SUCH DAMAGE.
26b252313fSGleb Smirnoff  */
27b252313fSGleb Smirnoff 
28b252313fSGleb Smirnoff #include <sys/param.h>
29b252313fSGleb Smirnoff #include <sys/ioctl.h>
30b252313fSGleb Smirnoff #include <net/if.h>
31b252313fSGleb Smirnoff #include <net/pfil.h>
32b252313fSGleb Smirnoff 
33b252313fSGleb Smirnoff #include <err.h>
34b252313fSGleb Smirnoff #include <fcntl.h>
35b252313fSGleb Smirnoff #include <stdio.h>
36b252313fSGleb Smirnoff #include <stdlib.h>
37b252313fSGleb Smirnoff #include <string.h>
38b252313fSGleb Smirnoff #include <unistd.h>
39b252313fSGleb Smirnoff 
40b252313fSGleb Smirnoff static int dev;
41b252313fSGleb Smirnoff 
42b252313fSGleb Smirnoff static const char * const typenames[] = {
43b252313fSGleb Smirnoff 	[PFIL_TYPE_IP4] = "IPv4",
44b252313fSGleb Smirnoff 	[PFIL_TYPE_IP6] = "IPv6",
45b252313fSGleb Smirnoff 	[PFIL_TYPE_ETHERNET] = "Ethernet",
46b252313fSGleb Smirnoff };
47b252313fSGleb Smirnoff 
48b252313fSGleb Smirnoff static void listheads(int argc, char *argv[]);
49b252313fSGleb Smirnoff static void listhooks(int argc, char *argv[]);
50b252313fSGleb Smirnoff static void hook(int argc, char *argv[]);
51b252313fSGleb Smirnoff static void help(void);
52b252313fSGleb Smirnoff 
53b252313fSGleb Smirnoff static const struct cmd {
54b252313fSGleb Smirnoff 	const char	*cmd_name;
55b252313fSGleb Smirnoff 	void		(*cmd_func)(int argc, char *argv[]);
56b252313fSGleb Smirnoff } cmds[] = {
57b252313fSGleb Smirnoff 	{ "heads",	listheads },
58b252313fSGleb Smirnoff 	{ "hooks",	listhooks },
59b252313fSGleb Smirnoff 	{ "link",	hook },
60b252313fSGleb Smirnoff 	{ "unlink",	hook },
61b252313fSGleb Smirnoff 	{ NULL,		NULL },
62b252313fSGleb Smirnoff };
63b252313fSGleb Smirnoff 
64b252313fSGleb Smirnoff int
main(int argc __unused,char * argv[]__unused)65b252313fSGleb Smirnoff main(int argc __unused, char *argv[] __unused)
66b252313fSGleb Smirnoff {
67b252313fSGleb Smirnoff 	int cmd = -1;
68b252313fSGleb Smirnoff 
69b252313fSGleb Smirnoff 	if (--argc == 0)
70b252313fSGleb Smirnoff 		help();
71b252313fSGleb Smirnoff 	argv++;
72b252313fSGleb Smirnoff 
73b252313fSGleb Smirnoff 	for (int i = 0; cmds[i].cmd_name != NULL; i++)
74b252313fSGleb Smirnoff 		if (!strncmp(argv[0], cmds[i].cmd_name, strlen(argv[0]))) {
75b252313fSGleb Smirnoff 			if (cmd != -1)
76b252313fSGleb Smirnoff 				errx(1, "ambiguous command: %s", argv[0]);
77b252313fSGleb Smirnoff 			cmd = i;
78b252313fSGleb Smirnoff 		}
79b252313fSGleb Smirnoff 	if (cmd == -1)
80b252313fSGleb Smirnoff 		errx(1, "unknown command: %s", argv[0]);
81b252313fSGleb Smirnoff 
82b252313fSGleb Smirnoff 	dev = open("/dev/" PFILDEV, O_RDWR);
83b252313fSGleb Smirnoff 	if (dev == -1)
84b252313fSGleb Smirnoff 		err(1, "open(%s)", "/dev/" PFILDEV);
85b252313fSGleb Smirnoff 
86b252313fSGleb Smirnoff 	(*cmds[cmd].cmd_func)(argc, argv);
87b252313fSGleb Smirnoff 
88b252313fSGleb Smirnoff 	return (0);
89b252313fSGleb Smirnoff }
90b252313fSGleb Smirnoff 
91b252313fSGleb Smirnoff static void
help(void)92b252313fSGleb Smirnoff help(void)
93b252313fSGleb Smirnoff {
94b252313fSGleb Smirnoff 
952e15db7bSGleb Smirnoff 	fprintf(stderr, "usage: %s (heads|hooks|link|unlink)\n", getprogname());
96b252313fSGleb Smirnoff 	exit(0);
97b252313fSGleb Smirnoff }
98b252313fSGleb Smirnoff 
99b252313fSGleb Smirnoff static void
listheads(int argc __unused,char * argv[]__unused)100b252313fSGleb Smirnoff listheads(int argc __unused, char *argv[] __unused)
101b252313fSGleb Smirnoff {
102b252313fSGleb Smirnoff 	struct pfilioc_list plh;
103b252313fSGleb Smirnoff 	u_int nheads, nhooks, i;
104b252313fSGleb Smirnoff 	int j, h;
105b252313fSGleb Smirnoff 
106b252313fSGleb Smirnoff 	plh.pio_nheads = 0;
107b252313fSGleb Smirnoff 	plh.pio_nhooks = 0;
108b252313fSGleb Smirnoff 	if (ioctl(dev, PFILIOC_LISTHEADS, &plh) != 0)
109b252313fSGleb Smirnoff 		err(1, "ioctl(PFILIOC_LISTHEADS)");
110b252313fSGleb Smirnoff 
111b252313fSGleb Smirnoff retry:
112b252313fSGleb Smirnoff 	plh.pio_heads = calloc(plh.pio_nheads, sizeof(struct pfilioc_head));
113b252313fSGleb Smirnoff 	if (plh.pio_heads == NULL)
114b252313fSGleb Smirnoff 		err(1, "malloc");
115b252313fSGleb Smirnoff 	plh.pio_hooks = calloc(plh.pio_nhooks, sizeof(struct pfilioc_hook));
116b252313fSGleb Smirnoff 	if (plh.pio_hooks == NULL)
117b252313fSGleb Smirnoff 		err(1, "malloc");
118b252313fSGleb Smirnoff 
119b252313fSGleb Smirnoff 	nheads = plh.pio_nheads;
120b252313fSGleb Smirnoff 	nhooks = plh.pio_nhooks;
121b252313fSGleb Smirnoff 
122b252313fSGleb Smirnoff 	if (ioctl(dev, PFILIOC_LISTHEADS, &plh) != 0)
123b252313fSGleb Smirnoff 		err(1, "ioctl(PFILIOC_LISTHEADS)");
124b252313fSGleb Smirnoff 
125b252313fSGleb Smirnoff 	if (plh.pio_nheads > nheads || plh.pio_nhooks > nhooks) {
126b252313fSGleb Smirnoff 		free(plh.pio_heads);
127b252313fSGleb Smirnoff 		free(plh.pio_hooks);
128b252313fSGleb Smirnoff 		goto retry;
129b252313fSGleb Smirnoff 	}
130b252313fSGleb Smirnoff 
131b252313fSGleb Smirnoff #define	FMTHD	"%16s %8s\n"
132d76f6b8eSGleb Smirnoff #define	FMTHK	"%29s %16s:%s\n"
133d76f6b8eSGleb Smirnoff 	printf("%16s %8s %3s %16s\n", "Intercept point", "Type", "Dir", "Hook");
134b252313fSGleb Smirnoff 	for (i = 0, h = 0; i < plh.pio_nheads; i++) {
135b252313fSGleb Smirnoff 		printf(FMTHD, plh.pio_heads[i].pio_name,
136b252313fSGleb Smirnoff 		    typenames[plh.pio_heads[i].pio_type]);
137b252313fSGleb Smirnoff 		for (j = 0; j < plh.pio_heads[i].pio_nhooksin; j++, h++)
138b252313fSGleb Smirnoff 			printf(FMTHK, "In", plh.pio_hooks[h].pio_module,
139b252313fSGleb Smirnoff 			    plh.pio_hooks[h].pio_ruleset);
140b252313fSGleb Smirnoff 		for (j = 0; j < plh.pio_heads[i].pio_nhooksout; j++, h++)
141b252313fSGleb Smirnoff 			printf(FMTHK, "Out", plh.pio_hooks[h].pio_module,
142b252313fSGleb Smirnoff 			    plh.pio_hooks[h].pio_ruleset);
143b252313fSGleb Smirnoff 	}
144b252313fSGleb Smirnoff }
145b252313fSGleb Smirnoff 
146b252313fSGleb Smirnoff static void
listhooks(int argc __unused,char * argv[]__unused)147b252313fSGleb Smirnoff listhooks(int argc __unused, char *argv[] __unused)
148b252313fSGleb Smirnoff {
149b252313fSGleb Smirnoff 	struct pfilioc_list plh;
150b252313fSGleb Smirnoff 	u_int nhooks, i;
151b252313fSGleb Smirnoff 
152b252313fSGleb Smirnoff 	plh.pio_nhooks = 0;
153*1bfe1951SGleb Smirnoff 	if (ioctl(dev, PFILIOC_LISTHOOKS, &plh) != 0)
154*1bfe1951SGleb Smirnoff 		err(1, "ioctl(PFILIOC_LISTHOOKS)");
155b252313fSGleb Smirnoff retry:
156b252313fSGleb Smirnoff 	plh.pio_hooks = calloc(plh.pio_nhooks, sizeof(struct pfilioc_hook));
157b252313fSGleb Smirnoff 	if (plh.pio_hooks == NULL)
158b252313fSGleb Smirnoff 		err(1, "malloc");
159b252313fSGleb Smirnoff 
160b252313fSGleb Smirnoff 	nhooks = plh.pio_nhooks;
161b252313fSGleb Smirnoff 
162b252313fSGleb Smirnoff 	if (ioctl(dev, PFILIOC_LISTHOOKS, &plh) != 0)
163b252313fSGleb Smirnoff 		err(1, "ioctl(PFILIOC_LISTHOOKS)");
164b252313fSGleb Smirnoff 
165b252313fSGleb Smirnoff 	if (plh.pio_nhooks > nhooks) {
166b252313fSGleb Smirnoff 		free(plh.pio_hooks);
167b252313fSGleb Smirnoff 		goto retry;
168b252313fSGleb Smirnoff 	}
169b252313fSGleb Smirnoff 
170d76f6b8eSGleb Smirnoff 	printf("%16s %16s %8s\n", "Hook", "", "Type");
171b252313fSGleb Smirnoff 	for (i = 0; i < plh.pio_nhooks; i++) {
172d76f6b8eSGleb Smirnoff 		printf("%16s:%-16s %8s\n", plh.pio_hooks[i].pio_module,
173b252313fSGleb Smirnoff 		    plh.pio_hooks[i].pio_ruleset,
174b252313fSGleb Smirnoff 		    typenames[plh.pio_hooks[i].pio_type]);
175b252313fSGleb Smirnoff 	}
176b252313fSGleb Smirnoff }
177b252313fSGleb Smirnoff 
178b252313fSGleb Smirnoff static void
hook(int argc,char * argv[])179b252313fSGleb Smirnoff hook(int argc, char *argv[])
180b252313fSGleb Smirnoff {
181b252313fSGleb Smirnoff 	struct pfilioc_link req;
182b252313fSGleb Smirnoff 	int c;
183b252313fSGleb Smirnoff 	char *ruleset;
184b252313fSGleb Smirnoff 
185b252313fSGleb Smirnoff 	if (argv[0][0] == 'u')
186b252313fSGleb Smirnoff 		req.pio_flags = PFIL_UNLINK;
187b252313fSGleb Smirnoff 	else
188b252313fSGleb Smirnoff 		req.pio_flags = 0;
189b252313fSGleb Smirnoff 
190b252313fSGleb Smirnoff 	while ((c = getopt(argc, argv, "ioa")) != -1)
191b252313fSGleb Smirnoff 		switch (c) {
192b252313fSGleb Smirnoff 		case 'i':
193b252313fSGleb Smirnoff 			req.pio_flags |= PFIL_IN;
194b252313fSGleb Smirnoff 			break;
195b252313fSGleb Smirnoff 		case 'o':
196b252313fSGleb Smirnoff 			req.pio_flags |= PFIL_OUT;
197b252313fSGleb Smirnoff 			break;
198b252313fSGleb Smirnoff 		case 'a':
199b252313fSGleb Smirnoff 			req.pio_flags |= PFIL_APPEND;
200b252313fSGleb Smirnoff 			break;
201b252313fSGleb Smirnoff 		default:
202b252313fSGleb Smirnoff 			help();
203b252313fSGleb Smirnoff 		}
204b252313fSGleb Smirnoff 
205b252313fSGleb Smirnoff 	if (!PFIL_DIR(req.pio_flags))
206b252313fSGleb Smirnoff 		help();
207b252313fSGleb Smirnoff 
208b252313fSGleb Smirnoff 	argc -= optind;
209b252313fSGleb Smirnoff 	argv += optind;
210b252313fSGleb Smirnoff 
211b252313fSGleb Smirnoff 	if (argc != 2)
212b252313fSGleb Smirnoff 		help();
213b252313fSGleb Smirnoff 
214b252313fSGleb Smirnoff 	/* link mod:ruleset head */
215b252313fSGleb Smirnoff 	if ((ruleset = strchr(argv[0], ':')) == NULL)
216b252313fSGleb Smirnoff 		help();
217b252313fSGleb Smirnoff 	*ruleset = '\0';
218b252313fSGleb Smirnoff 	ruleset++;
219b252313fSGleb Smirnoff 
220b252313fSGleb Smirnoff 	strlcpy(req.pio_name, argv[1], sizeof(req.pio_name));
221b252313fSGleb Smirnoff 	strlcpy(req.pio_module, argv[0], sizeof(req.pio_module));
222b252313fSGleb Smirnoff 	strlcpy(req.pio_ruleset, ruleset, sizeof(req.pio_ruleset));
223b252313fSGleb Smirnoff 
224b252313fSGleb Smirnoff 	if (ioctl(dev, PFILIOC_LINK, &req) != 0)
225b252313fSGleb Smirnoff 		err(1, "ioctl(PFILIOC_LINK)");
226b252313fSGleb Smirnoff }
227