svn commit: r343631 - in head: . sbin sbin/pfilctl share/man/man9 sys/contrib/ipfilter/netinet sys/net sys/netinet sys/netinet6 sys/netpfil/ipfw sys/netpfil/pf

Gleb Smirnoff glebius at FreeBSD.org
Thu Jan 31 23:01:06 UTC 2019


Author: glebius
Date: Thu Jan 31 23:01:03 2019
New Revision: 343631
URL: https://svnweb.freebsd.org/changeset/base/343631

Log:
  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

Added:
  head/sbin/pfilctl/
  head/sbin/pfilctl/Makefile   (contents, props changed)
  head/sbin/pfilctl/pfilctl.8   (contents, props changed)
  head/sbin/pfilctl/pfilctl.c   (contents, props changed)
Modified:
  head/ObsoleteFiles.inc
  head/sbin/Makefile
  head/share/man/man9/Makefile
  head/share/man/man9/pfil.9
  head/sys/contrib/ipfilter/netinet/ip_fil_freebsd.c
  head/sys/net/if_bridge.c
  head/sys/net/if_enc.c
  head/sys/net/if_ethersubr.c
  head/sys/net/if_var.h
  head/sys/net/pfil.c
  head/sys/net/pfil.h
  head/sys/netinet/ip_fastfwd.c
  head/sys/netinet/ip_input.c
  head/sys/netinet/ip_output.c
  head/sys/netinet/ip_var.h
  head/sys/netinet/siftr.c
  head/sys/netinet6/ip6_fastfwd.c
  head/sys/netinet6/ip6_forward.c
  head/sys/netinet6/ip6_input.c
  head/sys/netinet6/ip6_output.c
  head/sys/netinet6/ip6_var.h
  head/sys/netpfil/ipfw/ip_fw_eaction.c
  head/sys/netpfil/ipfw/ip_fw_pfil.c
  head/sys/netpfil/pf/pf_ioctl.c

Modified: head/ObsoleteFiles.inc
==============================================================================
--- head/ObsoleteFiles.inc	Thu Jan 31 22:58:17 2019	(r343630)
+++ head/ObsoleteFiles.inc	Thu Jan 31 23:01:03 2019	(r343631)
@@ -38,6 +38,12 @@
 #   xargs -n1 | sort | uniq -d;
 # done
 
+# 20190131: pfil(9) changed
+OLD_FILES+=usr/share/man/man9/pfil_hook_get.9
+OLD_FILES+=usr/share/man/man9/pfil_rlock.9
+OLD_FILES+=usr/share/man/man9/pfil_runlock.9
+OLD_FILES+=usr/share/man/man9/pfil_wlock.9
+OLD_FILES+=usr/share/man/man9/pfil_wunlock.9
 # 20190126: adv(4) / adw(4) removal
 OLD_FILES+=usr/share/man/man4/adv.4.gz
 OLD_FILES+=usr/share/man/man4/adw.4.gz

Modified: head/sbin/Makefile
==============================================================================
--- head/sbin/Makefile	Thu Jan 31 22:58:17 2019	(r343630)
+++ head/sbin/Makefile	Thu Jan 31 23:01:03 2019	(r343631)
@@ -52,6 +52,7 @@ SUBDIR=adjkerntz \
 	newfs_msdos \
 	nfsiod \
 	nos-tun \
+	pfilctl \
 	ping \
 	rcorder \
 	reboot \

Added: head/sbin/pfilctl/Makefile
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sbin/pfilctl/Makefile	Thu Jan 31 23:01:03 2019	(r343631)
@@ -0,0 +1,9 @@
+# $FreeBSD$
+
+PROG=	pfilctl
+SRCS=	pfilctl.c
+WARNS?=	6
+
+MAN=	pfilctl.8
+
+.include <bsd.prog.mk>

Added: head/sbin/pfilctl/pfilctl.8
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sbin/pfilctl/pfilctl.8	Thu Jan 31 23:01:03 2019	(r343631)
@@ -0,0 +1,117 @@
+.\" Copyright (c) 2019 Gleb Smirnoff <glebius at 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 at FreeBSD.org .

Added: head/sbin/pfilctl/pfilctl.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sbin/pfilctl/pfilctl.c	Thu Jan 31 23:01:03 2019	(r343631)
@@ -0,0 +1,230 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2019 Gleb Smirnoff <glebius at 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)");
+}

Modified: head/share/man/man9/Makefile
==============================================================================
--- head/share/man/man9/Makefile	Thu Jan 31 22:58:17 2019	(r343630)
+++ head/share/man/man9/Makefile	Thu Jan 31 23:01:03 2019	(r343631)
@@ -1635,13 +1635,9 @@ MLINKS+=pci_iov_schema.9 pci_iov_schema_alloc_node.9 \
 MLINKS+=pfil.9 pfil_add_hook.9 \
 	pfil.9 pfil_head_register.9 \
 	pfil.9 pfil_head_unregister.9 \
-	pfil.9 pfil_hook_get.9 \
 	pfil.9 pfil_remove_hook.9 \
-	pfil.9 pfil_rlock.9 \
 	pfil.9 pfil_run_hooks.9 \
-	pfil.9 pfil_runlock.9 \
-	pfil.9 pfil_wlock.9 \
-	pfil.9 pfil_wunlock.9
+	pfil.9 pfil_link.9
 MLINKS+=pfind.9 zpfind.9
 MLINKS+=PHOLD.9 PRELE.9 \
 	PHOLD.9 _PHOLD.9 \

Modified: head/share/man/man9/pfil.9
==============================================================================
--- head/share/man/man9/pfil.9	Thu Jan 31 22:58:17 2019	(r343630)
+++ head/share/man/man9/pfil.9	Thu Jan 31 23:01:03 2019	(r343631)
@@ -1,5 +1,6 @@
 .\"	$NetBSD: pfil.9,v 1.22 2003/07/01 13:04:06 wiz Exp $
 .\"
+.\" Copyright (c) 2019 Gleb Smirnoff <glebius at FreeBSD.org>
 .\" Copyright (c) 1996 Matthew R. Green
 .\" All rights reserved.
 .\"
@@ -28,194 +29,127 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd March 10, 2018
+.Dd January 28, 2019
 .Dt PFIL 9
 .Os
 .Sh NAME
 .Nm pfil ,
 .Nm pfil_head_register ,
 .Nm pfil_head_unregister ,
-.Nm pfil_head_get ,
-.Nm pfil_add_hook ,
-.Nm pfil_add_hook_flags ,
-.Nm pfil_remove_hook ,
-.Nm pfil_remove_hook_flags ,
-.Nm pfil_run_hooks ,
-.Nm pfil_rlock ,
-.Nm pfil_runlock ,
-.Nm pfil_wlock ,
-.Nm pfil_wunlock
+.Nm pfil_link ,
+.Nm pfil_run_hooks
 .Nd packet filter interface
 .Sh SYNOPSIS
 .In sys/param.h
 .In sys/mbuf.h
-.In net/if.h
 .In net/pfil.h
-.Bd -literal
-typedef int (*pfil_func_t)(void *arg, struct mbuf **mp, struct ifnet *, int dir, struct inpcb);
-.Bd -literal
-typedef int (*pfil_func_flags_t)(void *arg, struct mbuf **mp, struct ifnet *, int dir, int flags, struct inpcb);
-.Ft int
-.Fn pfil_head_register "struct pfil_head *head"
-.Ft int
-.Fn pfil_head_unregister "struct pfil_head *head"
-.Ft "struct pfil_head *"
-.Fn pfil_head_get "int af" "u_long dlt"
-.Ft int
-.Fn pfil_add_hook "pfil_func_t" "void *arg" "struct pfil_head *"
-.Ft int
-.Fn pfil_add_hook_flags "pfil_func_flags_t" "void *arg" "int flags" "struct pfil_head *"
-.Ft int
-.Fn pfil_remove_hook "pfil_func_t" "void *arg" "struct pfil_head *"
-.Ft int
-.Fn pfil_remove_hook_flags "pfil_func_flags_t" "void *arg" "int flags" "struct pfil_head *"
-.Ft int
-.Fn pfil_run_hooks "struct pfil_head *head" "struct mbuf **mp" "struct ifnet *" "int dir" "int flags" "struct inpcb *"
+.Ft pfil_head_t
+.Fn pfil_head_register "struct pfil_head_args *args"
 .Ft void
-.Fn pfil_rlock "struct pfil_head *" "struct rm_priotracker *"
+.Fn pfil_head_unregister "struct pfil_head_t *head"
+.Ft pfil_hook_t
+.Fn pfil_add_hook "struct pfil_hook_args *"
 .Ft void
-.Fn pfil_runlock "struct pfil_head *" "struct rm_priotracker *"
-.Ft void
-.Fn pfil_wlock "struct pfil_head *"
-.Ft void
-.Fn pfil_wunlock "struct pfil_head *"
-.Ed
+.Fn pfil_remove_hook "pfil_hook_t"
+.Ft int
+.Fn pfil_link "struct pfil_link_args *args"
+.Ft int
+.Fn pfil_run_hooks "phil_head_t *" "pfil_packet_t" "struct ifnet *" "int" "struct inpcb *"
 .Sh DESCRIPTION
 The
 .Nm
-framework allows for a specified function to be invoked for every
-incoming or outgoing packet for a particular network I/O stream.
+framework allows for a specified function or a list of functions
+to be invoked for every incoming or outgoing packet for a particular
+network I/O stream.
 These hooks may be used to implement a firewall or perform packet
 transformations.
 .Pp
-Packet filtering points are registered with
+Packet filtering points, for historical reasons named
+.Em heads ,
+are registered with
 .Fn pfil_head_register .
-Filtering points are identified by a key
-.Pq Vt "void *"
-and a data link type
-.Pq Vt int
-in the
-.Vt pfil_head
-structure.
-Packet filters use the key and data link type to look up the filtering
-point with which they register themselves.
-The key is unique to the filtering point.
-The data link type is a
-.Xr bpf 4
-DLT constant indicating what kind of header is present on the packet
-at the filtering point.
-Each filtering point uses common per-VNET rmlock by default.
-This can be changed by specifying
-.Vt PFIL_FLAG_PRIVATE_LOCK
-as
-.Vt "flags"
-field in the
-.Vt pfil_head
-structure.
-Note that specifying private lock can break filters sharing the same
-ruleset and/or state between different data link types.
-Filtering points may be unregistered with the
-.Fn pfil_head_unregister
-function.
+The function is supplied with special versioned
+.Vt struct pfil_head_args
+structure that specifies type and features of the head as well as
+human readable name.
+If the filtering point to be ever destroyed, the subsystem that
+created it must unregister it with call to
+.Fn pfil_head_unregister .
 .Pp
-Packet filters register/unregister themselves with a filtering point
-with the
+Packet filtering systems may register arbitrary number of filters,
+for historical reasons named
+.Em hooks .
+To register a new hook
 .Fn pfil_add_hook
-and
+with special versioned
+.Vt struct pfil_hook_args
+structure is called.
+The structure specifies type and features of the hook, pointer to
+the actual filtering function and user readable name of the filtering
+module and ruleset name.
+Later hooks can be removed with
 .Fn pfil_remove_hook
-functions, respectively.
-.I
-The head is looked up using the
-.Fn pfil_head_get
-function, which takes the key and data link type that the packet filter
-expects.
-Filters may provide an argument to be passed to the filter when
-invoked on a packet.
+functions.
 .Pp
-When a filter is invoked, the packet appears just as if it
-.Dq came off the wire .
-That is, all protocol fields are in network byte order.
-The filter is called with its specified argument, the pointer to the
-pointer to the
-.Vt mbuf
-containing the packet, the pointer to the network
-interface that the packet is traversing, and the direction
-.Dv ( PFIL_IN
-or
-.Dv PFIL_OUT )
-that the packet is traveling.
-The
-.Vt flags
-argument will indicate if an outgoing packet is simply being forwarded with the
-value PFIL_FWD.
-The filter may change which mbuf the
-.Vt "mbuf\ **"
-argument references.
-The filter returns an error (errno) if the packet processing is to stop, or 0
-if the processing is to continue.
-If the packet processing is to stop, it is the responsibility of the
-filter to free the packet.
-.Pp
-Every filter hook is called with
+To connect existing
+.Em hook
+to an existing
+.Em head
+function
+.Fn pfil_link
+shall be used.
+The function is supplied with versioned
+.Vt struct pfil_link_args
+structure that specifies either literal names of hook and head or
+pointers to them.
+Typically
+.Fn pfil_link
+is called by filtering modules to autoregister their default ruleset
+and default filtering points.
+It also serves on the kernel side of
+.Xr ioctl 2
+when user changes
 .Nm
-read lock held.
-All heads uses the same lock within the same VNET instance.
-Packet filter can use this lock instead of own locking model to
-improve performance.
-Since
-.Nm
-uses
-.Xr rmlock 9
-.Fn pfil_rlock
-and
-.Fn pfil_runlock
-require
-.Va struct rm_priotracker
-to be passed as argument.
-Filter can acquire and release writer lock via
-.Fn pfil_wlock
-and
-.Fn pfil_wunlock
-functions.
-See
-.Xr rmlock 9
-for more details.
-.Sh FILTERING POINTS
-Currently, filtering points are implemented for the following link types:
+configuration with help of
+.Xr pfilctl 8
+utility.
 .Pp
-.Bl -tag -width "AF_INET6" -offset XXX -compact
-.It AF_INET
+For every packet traveling through a
+.Em head
+the latter shall invoke
+.Fn pfil_run_hooks .
+The function can accept either
+.Vt struct mbuf *
+pointer or a
+.Vt void *
+pointer and length.
+In case if a hooked filtering module cannot understand
+.Vt void *
+pointer
+.Nm
+will provide it with a fake one.
+All calls to
+.Fn pfil_run_hooks
+are performed in network
+.Xr epoch 9 .
+.Sh HEADS (filtering points)
+By default kernel creates the following heads:
+.Bl -tag -width "ethernet"
+.It inet
 IPv4 packets.
-.It AF_INET6
+.It inet6
 IPv6 packets.
-.It AF_LINK
+.It ethernet
 Link-layer packets.
 .El
-.Sh RETURN VALUES
-If successful,
-.Fn pfil_head_get
-returns the
-.Vt pfil_head
-structure for the given key/dlt.
-The
-.Fn pfil_add_hook
-and
-.Fn pfil_remove_hook
-functions
-return 0 if successful.
-If called with flag
-.Dv PFIL_WAITOK ,
-.Fn pfil_remove_hook
-is expected to always succeed.
 .Pp
-The
-.Fn pfil_head_unregister
-function
-might sleep!
+Default rulesets are automatically linked to these heads to preserve
+historical behavavior.
 .Sh SEE ALSO
-.Xr bpf 4 ,
-.Xr if_bridge 4 ,
-.Xr rmlock 9
+.Xr ipfilter 4 ,
+.Xr ipfw 4 ,
+.Xr pf 4 ,
+.Xr pfilctl 8
 .Sh HISTORY
 The
 .Nm
@@ -223,45 +157,8 @@ interface first appeared in
 .Nx 1.3 .
 The
 .Nm
-input and output lists were originally implemented as
-.In sys/queue.h
-.Dv LIST
-structures;
-however this was changed in
-.Nx 1.4
-to
-.Dv TAILQ
-structures.
-This change was to allow the input and output filters to be processed in
-reverse order, to allow the same path to be taken, in or out of the kernel.
-.Pp
-The
-.Nm
-interface was changed in 1.4T to accept a 3rd parameter to both
-.Fn pfil_add_hook
-and
-.Fn pfil_remove_hook ,
-introducing the capability of per-protocol filtering.
-This was done primarily in order to support filtering of IPv6.
-.Pp
-In 1.5K, the
-.Nm
-framework was changed to work with an arbitrary number of filtering points,
-as well as be less IP-centric.
-.Pp
-Fine-grained locking was added in
+interface was imported into
 .Fx 5.2 .
-.Nm
-lock export was added in
-.Fx 10.0 .
-.Sh BUGS
-When a
-.Vt pfil_head
-is being modified, no traffic is diverted
-(to avoid deadlock).
-This means that traffic may be dropped unconditionally for a short period
-of time.
-.Fn pfil_run_hooks
-will return
-.Er ENOBUFS
-to indicate this.
+In
+.Fx 13.0
+the interface was significantly rewritten.

Modified: head/sys/contrib/ipfilter/netinet/ip_fil_freebsd.c
==============================================================================
--- head/sys/contrib/ipfilter/netinet/ip_fil_freebsd.c	Thu Jan 31 22:58:17 2019	(r343630)
+++ head/sys/contrib/ipfilter/netinet/ip_fil_freebsd.c	Thu Jan 31 23:01:03 2019	(r343631)
@@ -25,6 +25,7 @@ static const char rcsid[] = "@(#)$Id$";
 # include "opt_random_ip_id.h"
 #endif
 #include <sys/param.h>
+#include <sys/conf.h>
 #include <sys/errno.h>
 #include <sys/types.h>
 #include <sys/file.h>
@@ -126,32 +127,33 @@ static void ipf_ifevent(arg, ifp)
 
 
 
-static int
-ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
+static pfil_return_t
+ipf_check_wrapper(struct mbuf **mp, struct ifnet *ifp, int flags,
+    void *ruleset __unused, struct inpcb *inp)
 {
 	struct ip *ip = mtod(*mp, struct ip *);
-	int rv;
+	pfil_return_t rv;
 
 	CURVNET_SET(ifp->if_vnet);
-	rv = ipf_check(&V_ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
-		       mp);
+	rv = ipf_check(&V_ipfmain, ip, ip->ip_hl << 2, ifp, (flags & PFIL_OUT),
+	    mp);
 	CURVNET_RESTORE();
-	return rv;
+	return (rv == 0 ? PFIL_PASS : PFIL_DROPPED);
 }
 
-# ifdef USE_INET6
-#  include <netinet/ip6.h>
-
-static int
-ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
+#ifdef USE_INET6
+static pfil_return_t
+ipf_check_wrapper6(struct mbuf **mp, struct ifnet *ifp, int flags,
+    void *ruleset __unused, struct inpcb *inp)
 {
-	int error;
+	pfil_return_t rv;
 
 	CURVNET_SET(ifp->if_vnet);
-	error = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
-			  sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp);
+	rv = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
+	    sizeof(struct ip6_hdr), ifp, (flags & PFIL_OUT), mp);
 	CURVNET_RESTORE();
-	return (error);
+
+	return (rv == 0 ? PFIL_PASS : PFIL_DROPPED);
 }
 # endif
 #if	defined(IPFILTER_LKM)
@@ -1318,53 +1320,62 @@ ipf_inject(fin, m)
 	return error;
 }
 
+VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet_hook);
+VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet6_hook);
+#define	V_ipf_inet_hook		VNET(ipf_inet_hook)
+#define	V_ipf_inet6_hook	VNET(ipf_inet6_hook)
+
 int ipf_pfil_unhook(void) {
-	struct pfil_head *ph_inet;
+
+	pfil_remove_hook(V_ipf_inet_hook);
+
 #ifdef USE_INET6
-	struct pfil_head *ph_inet6;
+	pfil_remove_hook(V_ipf_inet6_hook);
 #endif
 
-	ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
-	if (ph_inet != NULL)
-		pfil_remove_hook((void *)ipf_check_wrapper, NULL,
-		    PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
-# ifdef USE_INET6
-	ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
-	if (ph_inet6 != NULL)
-		pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
-		    PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
-# endif
-
 	return (0);
 }
 
 int ipf_pfil_hook(void) {
-	struct pfil_head *ph_inet;
+	struct pfil_hook_args pha;
+	struct pfil_link_args pla;
+	int error, error6;
+
+	pha.pa_version = PFIL_VERSION;
+	pha.pa_flags = PFIL_IN | PFIL_OUT;
+	pha.pa_modname = "ipfilter";
+	pha.pa_rulname = "default";
+	pha.pa_func = ipf_check_wrapper;
+	pha.pa_ruleset = NULL;
+	pha.pa_type = PFIL_TYPE_IP4;
+	V_ipf_inet_hook = pfil_add_hook(&pha);
+
 #ifdef USE_INET6
-	struct pfil_head *ph_inet6;
+	pha.pa_func = ipf_check_wrapper6;
+	pha.pa_type = PFIL_TYPE_IP6;
+	V_ipf_inet6_hook = pfil_add_hook(&pha);
 #endif
 
-	ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
-#    ifdef USE_INET6
-	ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
-#    endif
-	if (ph_inet == NULL
-#    ifdef USE_INET6
-	    && ph_inet6 == NULL
-#    endif
-	   ) {
-		return ENODEV;
-	}
+	pla.pa_version = PFIL_VERSION;
+	pla.pa_flags = PFIL_IN | PFIL_OUT |
+	    PFIL_HEADPTR | PFIL_HOOKPTR;
+	pla.pa_head = V_inet_pfil_head;
+	pla.pa_hook = V_ipf_inet_hook;
+	error = pfil_link(&pla);
 
-	if (ph_inet != NULL)
-		pfil_add_hook((void *)ipf_check_wrapper, NULL,
-		    PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
-#  ifdef USE_INET6
-	if (ph_inet6 != NULL)
-		pfil_add_hook((void *)ipf_check_wrapper6, NULL,
-				      PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
-#  endif
-	return (0);
+	error6 = 0;
+#ifdef USE_INET6
+	pla.pa_head = V_inet6_pfil_head;
+	pla.pa_hook = V_ipf_inet6_hook;
+	error6 = pfil_link(&pla);
+#endif
+
+	if (error || error6)
+		error = ENODEV;
+	else
+		error = 0;
+
+	return (error);
 }
 
 void

Modified: head/sys/net/if_bridge.c
==============================================================================
--- head/sys/net/if_bridge.c	Thu Jan 31 22:58:17 2019	(r343630)
+++ head/sys/net/if_bridge.c	Thu Jan 31 23:01:03 2019	(r343631)
@@ -1970,9 +1970,9 @@ bridge_dummynet(struct mbuf *m, struct ifnet *ifp)
 		return;
 	}
 
-	if (PFIL_HOOKED(&V_inet_pfil_hook)
+	if (PFIL_HOOKED_OUT(V_inet_pfil_head)
 #ifdef INET6
-	    || PFIL_HOOKED(&V_inet6_pfil_hook)
+	    || PFIL_HOOKED_OUT(V_inet6_pfil_head)
 #endif
 	    ) {
 		if (bridge_pfil(&m, sc->sc_ifp, ifp, PFIL_OUT) != 0)
@@ -2230,9 +2230,9 @@ bridge_forward(struct bridge_softc *sc, struct bridge_
 		ETHER_BPF_MTAP(ifp, m);
 
 	/* run the packet filter */
-	if (PFIL_HOOKED(&V_inet_pfil_hook)
+	if (PFIL_HOOKED_IN(V_inet_pfil_head)
 #ifdef INET6
-	    || PFIL_HOOKED(&V_inet6_pfil_hook)
+	    || PFIL_HOOKED_IN(V_inet6_pfil_head)
 #endif
 	    ) {
 		BRIDGE_UNLOCK(sc);
@@ -2270,9 +2270,9 @@ bridge_forward(struct bridge_softc *sc, struct bridge_
 
 	BRIDGE_UNLOCK(sc);
 
-	if (PFIL_HOOKED(&V_inet_pfil_hook)
+	if (PFIL_HOOKED_OUT(V_inet_pfil_head)
 #ifdef INET6
-	    || PFIL_HOOKED(&V_inet6_pfil_hook)
+	    || PFIL_HOOKED_OUT(V_inet6_pfil_head)
 #endif
 	    ) {
 		if (bridge_pfil(&m, ifp, dst_if, PFIL_OUT) != 0)
@@ -2409,7 +2409,7 @@ bridge_input(struct ifnet *ifp, struct mbuf *m)
 
 #ifdef INET6
 #   define OR_PFIL_HOOKED_INET6 \
-	|| PFIL_HOOKED(&V_inet6_pfil_hook)
+	|| PFIL_HOOKED_IN(V_inet6_pfil_head)
 #else
 #   define OR_PFIL_HOOKED_INET6
 #endif
@@ -2427,7 +2427,7 @@ bridge_input(struct ifnet *ifp, struct mbuf *m)
 			if_inc_counter(iface, IFCOUNTER_IBYTES, m->m_pkthdr.len);		\
 			/* Filter on the physical interface. */		\
 			if (V_pfil_local_phys &&			\
-			    (PFIL_HOOKED(&V_inet_pfil_hook)		\
+			    (PFIL_HOOKED_IN(V_inet_pfil_head)		\
 			     OR_PFIL_HOOKED_INET6)) {			\
 				if (bridge_pfil(&m, NULL, ifp,		\
 				    PFIL_IN) != 0 || m == NULL) {	\
@@ -2517,9 +2517,9 @@ bridge_broadcast(struct bridge_softc *sc, struct ifnet
 	}
 
 	/* Filter on the bridge interface before broadcasting */
-	if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook)
+	if (runfilt && (PFIL_HOOKED_OUT(V_inet_pfil_head)
 #ifdef INET6
-	    || PFIL_HOOKED(&V_inet6_pfil_hook)
+	    || PFIL_HOOKED_OUT(V_inet6_pfil_head)
 #endif
 	    )) {
 		if (bridge_pfil(&m, sc->sc_ifp, NULL, PFIL_OUT) != 0)
@@ -2564,9 +2564,9 @@ bridge_broadcast(struct bridge_softc *sc, struct ifnet
 		 * pointer so we do not redundantly filter on the bridge for
 		 * each interface we broadcast on.
 		 */
-		if (runfilt && (PFIL_HOOKED(&V_inet_pfil_hook)
+		if (runfilt && (PFIL_HOOKED_OUT(V_inet_pfil_head)
 #ifdef INET6
-		    || PFIL_HOOKED(&V_inet6_pfil_hook)
+		    || PFIL_HOOKED_OUT(V_inet6_pfil_head)
 #endif
 		    )) {
 			if (used == 0) {
@@ -3101,6 +3101,7 @@ bridge_pfil(struct mbuf **mp, struct ifnet *bifp, stru
 	struct ip *ip;
 	struct llc llc1;
 	u_int16_t ether_type;
+	pfil_return_t rv;
 
 	snap = 0;
 	error = -1;	/* Default error if not error == 0 */
@@ -3172,14 +3173,14 @@ bridge_pfil(struct mbuf **mp, struct ifnet *bifp, stru
 	}
 
 	/* Run the packet through pfil before stripping link headers */
-	if (PFIL_HOOKED(&V_link_pfil_hook) && V_pfil_ipfw != 0 &&

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-src-head mailing list