This repository has been archived by the owner on Apr 7, 2019. It is now read-only.
forked from freebsd/freebsd-src
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New pfil(9) KPI together with newborn pfil API and control utility.
The KPI have been reviewed and cleansed of features that were planned back 20 years ago and never implemented. The pfil(9) internals have been made opaque to protocols with only returned types and function declarations exposed. The KPI is made more strict, but at the same time more extensible, as kernel uses same command structures that userland ioctl uses. In nutshell [KA]PI is about declaring filtering points, declaring filters and linking and unlinking them together. New [KA]PI makes it possible to reconfigure pfil(9) configuration: change order of hooks, rehook filter from one filtering point to a different one, disconnect a hook on output leaving it on input only, prepend/append a filter to existing list of filters. Now it possible for a single packet filter to provide multiple rulesets that may be linked to different points. Think of per-interface ACLs in Cisco or Juniper. None of existing packet filters yet support that, however limited usage is already possible, e.g. default ruleset can be moved to single interface, as soon as interface would pride their filtering points. Another future feature is possiblity to create pfil heads, that provide not an mbuf pointer but just a memory pointer with length. That would allow filtering at very early stages of a packet lifecycle, e.g. when packet has just been received by a NIC and no mbuf was yet allocated. Differential Revision: https://reviews.freebsd.org/D18951
- Loading branch information
Showing
27 changed files
with
1,556 additions
and
975 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ SUBDIR=adjkerntz \ | |
newfs_msdos \ | ||
nfsiod \ | ||
nos-tun \ | ||
pfilctl \ | ||
ping \ | ||
rcorder \ | ||
reboot \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# $FreeBSD$ | ||
|
||
PROG= pfilctl | ||
SRCS= pfilctl.c | ||
WARNS?= 6 | ||
|
||
MAN= pfilctl.8 | ||
|
||
.include <bsd.prog.mk> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
.\" Copyright (c) 2019 Gleb Smirnoff <glebius@FreeBSD.org> | ||
.\" | ||
.\" Redistribution and use in source and binary forms, with or without | ||
.\" modification, are permitted provided that the following conditions | ||
.\" are met: | ||
.\" 1. Redistributions of source code must retain the above copyright | ||
.\" notice, this list of conditions and the following disclaimer. | ||
.\" 2. Redistributions in binary form must reproduce the above copyright | ||
.\" notice, this list of conditions and the following disclaimer in the | ||
.\" documentation and/or other materials provided with the distribution. | ||
.\" | ||
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
.\" SUCH DAMAGE. | ||
.\" | ||
.\" $FreeBSD$ | ||
.\" | ||
.Dd January 28, 2019 | ||
.Dt PFILCTL 8 | ||
.Os | ||
.Sh NAME | ||
.Nm pfilctl | ||
.Nd pfil(9) control utility | ||
.Sh SYNOPSIS | ||
.Nm | ||
.Cm heads | ||
.Nm | ||
.Cm hooks | ||
.Nm | ||
.Cm link | ||
.Aq Fl i | Fl o | ||
.Op Fl a | ||
.Ar hook Ar head | ||
.Nm | ||
.Cm unlink | ||
.Aq Fl i | Fl o | ||
.Ar hook Ar head | ||
.Sh DESCRIPTION | ||
The | ||
.Nm | ||
utility is intended to view and change configuration of the | ||
.Xr pfil 9 | ||
packet filtering hooks and filters on them. | ||
.Sh COMMANDS | ||
.Bl -tag -width "unlink" | ||
.It Cm heads | ||
List available packet filtering points. | ||
.It Cm hooks | ||
List available packet filters. | ||
.It Xo | ||
.Cm link | ||
.Aq Fl i | Fl o | ||
.Op Fl a | ||
.Ar hook Ar head | ||
.Xc | ||
Link | ||
.Ar hook | ||
to | ||
.Ar head . | ||
With the | ||
.Fl i | ||
flag the hook will be connected as input and with | ||
.Fl o | ||
as output hook. | ||
At least one of | ||
.Fl i | ||
or | ||
.Fl o | ||
is required. | ||
By default | ||
.Nm | ||
will prepend the hook in front of other hooks if any present: | ||
new hook will be as close to the wire as possible, so that on input | ||
it will be the first filter and on output it will be the last. | ||
Adding the | ||
.Fl a | ||
flag switches to appending new hook instead of prepending. | ||
.It Xo | ||
.Cm unlink | ||
.Aq Fl i | Fl o | ||
.Ar hook Ar head | ||
.Xc | ||
Unlink | ||
.Ar hook | ||
on | ||
.Ar head . | ||
At least one of | ||
.Fl i | ||
or | ||
.Fl o | ||
is required. | ||
With the | ||
.Fl i | ||
flag the hook will be removed from the input list of hooks | ||
and with | ||
.Fl o | ||
on output list. | ||
.El | ||
.Sh SEE ALSO | ||
.Xr ipfilter 4 , | ||
.Xr ipfw 4 , | ||
.Xr pf 4 , | ||
.Xr pfil 9 | ||
.Sh AUTHORS | ||
.An -nosplit | ||
The | ||
.Nm | ||
utility was written by | ||
.An Gleb Smirnoff Aq Mt glebius@FreeBSD.org . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
/*- | ||
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD | ||
* | ||
* Copyright (c) 2019 Gleb Smirnoff <glebius@FreeBSD.org> | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
*/ | ||
|
||
#include <sys/cdefs.h> | ||
__FBSDID("$FreeBSD$"); | ||
|
||
#include <sys/param.h> | ||
#include <sys/ioctl.h> | ||
#include <net/if.h> | ||
#include <net/pfil.h> | ||
|
||
#include <err.h> | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
static int dev; | ||
|
||
static const char * const typenames[] = { | ||
[PFIL_TYPE_IP4] = "IPv4", | ||
[PFIL_TYPE_IP6] = "IPv6", | ||
[PFIL_TYPE_ETHERNET] = "Ethernet", | ||
}; | ||
|
||
static void listheads(int argc, char *argv[]); | ||
static void listhooks(int argc, char *argv[]); | ||
static void hook(int argc, char *argv[]); | ||
static void help(void); | ||
|
||
static const struct cmd { | ||
const char *cmd_name; | ||
void (*cmd_func)(int argc, char *argv[]); | ||
} cmds[] = { | ||
{ "heads", listheads }, | ||
{ "hooks", listhooks }, | ||
{ "link", hook }, | ||
{ "unlink", hook }, | ||
{ NULL, NULL }, | ||
}; | ||
|
||
int | ||
main(int argc __unused, char *argv[] __unused) | ||
{ | ||
int cmd = -1; | ||
|
||
if (--argc == 0) | ||
help(); | ||
argv++; | ||
|
||
for (int i = 0; cmds[i].cmd_name != NULL; i++) | ||
if (!strncmp(argv[0], cmds[i].cmd_name, strlen(argv[0]))) { | ||
if (cmd != -1) | ||
errx(1, "ambiguous command: %s", argv[0]); | ||
cmd = i; | ||
} | ||
if (cmd == -1) | ||
errx(1, "unknown command: %s", argv[0]); | ||
|
||
dev = open("/dev/" PFILDEV, O_RDWR); | ||
if (dev == -1) | ||
err(1, "open(%s)", "/dev/" PFILDEV); | ||
|
||
(*cmds[cmd].cmd_func)(argc, argv); | ||
|
||
return (0); | ||
} | ||
|
||
static void | ||
help(void) | ||
{ | ||
extern char *__progname; | ||
|
||
fprintf(stderr, "usage: %s (heads|hooks|link|unlink)\n", __progname); | ||
exit(0); | ||
} | ||
|
||
static void | ||
listheads(int argc __unused, char *argv[] __unused) | ||
{ | ||
struct pfilioc_list plh; | ||
u_int nheads, nhooks, i; | ||
int j, h; | ||
|
||
plh.pio_nheads = 0; | ||
plh.pio_nhooks = 0; | ||
if (ioctl(dev, PFILIOC_LISTHEADS, &plh) != 0) | ||
err(1, "ioctl(PFILIOC_LISTHEADS)"); | ||
|
||
retry: | ||
plh.pio_heads = calloc(plh.pio_nheads, sizeof(struct pfilioc_head)); | ||
if (plh.pio_heads == NULL) | ||
err(1, "malloc"); | ||
plh.pio_hooks = calloc(plh.pio_nhooks, sizeof(struct pfilioc_hook)); | ||
if (plh.pio_hooks == NULL) | ||
err(1, "malloc"); | ||
|
||
nheads = plh.pio_nheads; | ||
nhooks = plh.pio_nhooks; | ||
|
||
if (ioctl(dev, PFILIOC_LISTHEADS, &plh) != 0) | ||
err(1, "ioctl(PFILIOC_LISTHEADS)"); | ||
|
||
if (plh.pio_nheads > nheads || plh.pio_nhooks > nhooks) { | ||
free(plh.pio_heads); | ||
free(plh.pio_hooks); | ||
goto retry; | ||
} | ||
|
||
#define FMTHD "%16s %8s\n" | ||
#define FMTHK "%29s %16s %16s\n" | ||
printf(FMTHD, "Intercept point", "Type"); | ||
for (i = 0, h = 0; i < plh.pio_nheads; i++) { | ||
printf(FMTHD, plh.pio_heads[i].pio_name, | ||
typenames[plh.pio_heads[i].pio_type]); | ||
for (j = 0; j < plh.pio_heads[i].pio_nhooksin; j++, h++) | ||
printf(FMTHK, "In", plh.pio_hooks[h].pio_module, | ||
plh.pio_hooks[h].pio_ruleset); | ||
for (j = 0; j < plh.pio_heads[i].pio_nhooksout; j++, h++) | ||
printf(FMTHK, "Out", plh.pio_hooks[h].pio_module, | ||
plh.pio_hooks[h].pio_ruleset); | ||
} | ||
} | ||
|
||
static void | ||
listhooks(int argc __unused, char *argv[] __unused) | ||
{ | ||
struct pfilioc_list plh; | ||
u_int nhooks, i; | ||
|
||
plh.pio_nhooks = 0; | ||
if (ioctl(dev, PFILIOC_LISTHEADS, &plh) != 0) | ||
err(1, "ioctl(PFILIOC_LISTHEADS)"); | ||
retry: | ||
plh.pio_hooks = calloc(plh.pio_nhooks, sizeof(struct pfilioc_hook)); | ||
if (plh.pio_hooks == NULL) | ||
err(1, "malloc"); | ||
|
||
nhooks = plh.pio_nhooks; | ||
|
||
if (ioctl(dev, PFILIOC_LISTHOOKS, &plh) != 0) | ||
err(1, "ioctl(PFILIOC_LISTHOOKS)"); | ||
|
||
if (plh.pio_nhooks > nhooks) { | ||
free(plh.pio_hooks); | ||
goto retry; | ||
} | ||
|
||
printf("Available hooks:\n"); | ||
for (i = 0; i < plh.pio_nhooks; i++) { | ||
printf("\t%s:%s %s\n", plh.pio_hooks[i].pio_module, | ||
plh.pio_hooks[i].pio_ruleset, | ||
typenames[plh.pio_hooks[i].pio_type]); | ||
} | ||
} | ||
|
||
static void | ||
hook(int argc, char *argv[]) | ||
{ | ||
struct pfilioc_link req; | ||
int c; | ||
char *ruleset; | ||
|
||
if (argv[0][0] == 'u') | ||
req.pio_flags = PFIL_UNLINK; | ||
else | ||
req.pio_flags = 0; | ||
|
||
while ((c = getopt(argc, argv, "ioa")) != -1) | ||
switch (c) { | ||
case 'i': | ||
req.pio_flags |= PFIL_IN; | ||
break; | ||
case 'o': | ||
req.pio_flags |= PFIL_OUT; | ||
break; | ||
case 'a': | ||
req.pio_flags |= PFIL_APPEND; | ||
break; | ||
default: | ||
help(); | ||
} | ||
|
||
if (!PFIL_DIR(req.pio_flags)) | ||
help(); | ||
|
||
argc -= optind; | ||
argv += optind; | ||
|
||
if (argc != 2) | ||
help(); | ||
|
||
/* link mod:ruleset head */ | ||
if ((ruleset = strchr(argv[0], ':')) == NULL) | ||
help(); | ||
*ruleset = '\0'; | ||
ruleset++; | ||
|
||
strlcpy(req.pio_name, argv[1], sizeof(req.pio_name)); | ||
strlcpy(req.pio_module, argv[0], sizeof(req.pio_module)); | ||
strlcpy(req.pio_ruleset, ruleset, sizeof(req.pio_ruleset)); | ||
|
||
if (ioctl(dev, PFILIOC_LINK, &req) != 0) | ||
err(1, "ioctl(PFILIOC_LINK)"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.