2005-01-07 01:45:51 +00:00
|
|
|
/*-
|
2004-08-17 22:05:54 +00:00
|
|
|
* Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 20:44:24 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2004-08-17 22:05:54 +00:00
|
|
|
#include "opt_ipfw.h"
|
|
|
|
#include "opt_inet.h"
|
2011-09-27 13:27:17 +00:00
|
|
|
#include "opt_inet6.h"
|
2004-08-17 22:05:54 +00:00
|
|
|
#ifndef INET
|
|
|
|
#error IPFIREWALL requires INET.
|
|
|
|
#endif /* INET */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/kernel.h>
|
Conditionally compile out V_ globals while instantiating the appropriate
container structures, depending on VIMAGE_GLOBALS compile time option.
Make VIMAGE_GLOBALS a new compile-time option, which by default will not
be defined, resulting in instatiations of global variables selected for
V_irtualization (enclosed in #ifdef VIMAGE_GLOBALS blocks) to be
effectively compiled out. Instantiate new global container structures
to hold V_irtualized variables: vnet_net_0, vnet_inet_0, vnet_inet6_0,
vnet_ipsec_0, vnet_netgraph_0, and vnet_gif_0.
Update the VSYM() macro so that depending on VIMAGE_GLOBALS the V_
macros resolve either to the original globals, or to fields inside
container structures, i.e. effectively
#ifdef VIMAGE_GLOBALS
#define V_rt_tables rt_tables
#else
#define V_rt_tables vnet_net_0._rt_tables
#endif
Update SYSCTL_V_*() macros to operate either on globals or on fields
inside container structs.
Extend the internal kldsym() lookups with the ability to resolve
selected fields inside the virtualization container structs. This
applies only to the fields which are explicitly registered for kldsym()
visibility via VNET_MOD_DECLARE() and vnet_mod_register(), currently
this is done only in sys/net/if.c.
Fix a few broken instances of MODULE_GLOBAL() macro use in SCTP code,
and modify the MODULE_GLOBAL() macro to resolve to V_ macros, which in
turn result in proper code being generated depending on VIMAGE_GLOBALS.
De-virtualize local static variables in sys/contrib/pf/net/pf_subr.c
which were prematurely V_irtualized by automated V_ prepending scripts
during earlier merging steps. PF virtualization will be done
separately, most probably after next PF import.
Convert a few variable initializations at instantiation to
initialization in init functions, most notably in ipfw. Also convert
TUNABLE_INT() initializers for V_ variables to TUNABLE_FETCH_INT() in
initializer functions.
Discussed at: devsummit Strassburg
Reviewed by: bz, julian
Approved by: julian (mentor)
Obtained from: //depot/projects/vimage-commit2/...
X-MFC after: never
Sponsored by: NLnet Foundation, The FreeBSD Foundation
2008-12-10 23:12:39 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/rwlock.h>
|
2004-08-17 22:05:54 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
2012-09-04 19:43:26 +00:00
|
|
|
#include <net/ethernet.h>
|
2004-08-17 22:05:54 +00:00
|
|
|
#include <net/pfil.h>
|
2009-08-21 11:20:10 +00:00
|
|
|
#include <net/vnet.h>
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/ip_fw.h>
|
2011-06-27 12:21:11 +00:00
|
|
|
#ifdef INET6
|
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet6/ip6_var.h>
|
|
|
|
#endif
|
2012-09-14 11:51:49 +00:00
|
|
|
|
2009-12-28 12:29:13 +00:00
|
|
|
#include <netgraph/ng_ipfw.h>
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2012-09-14 11:51:49 +00:00
|
|
|
#include <netpfil/ipfw/ip_fw_private.h>
|
|
|
|
|
2004-08-17 22:05:54 +00:00
|
|
|
#include <machine/in_cksum.h>
|
|
|
|
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(int, fw_enable) = 1;
|
2009-12-16 10:48:40 +00:00
|
|
|
#define V_fw_enable VNET(fw_enable)
|
|
|
|
|
2006-05-12 04:41:27 +00:00
|
|
|
#ifdef INET6
|
2010-11-22 19:32:54 +00:00
|
|
|
static VNET_DEFINE(int, fw6_enable) = 1;
|
2009-12-16 10:48:40 +00:00
|
|
|
#define V_fw6_enable VNET(fw6_enable)
|
2008-12-11 16:26:38 +00:00
|
|
|
#endif
|
2006-05-12 04:41:27 +00:00
|
|
|
|
2012-09-04 19:43:26 +00:00
|
|
|
static VNET_DEFINE(int, fwlink_enable) = 0;
|
|
|
|
#define V_fwlink_enable VNET(fwlink_enable)
|
|
|
|
|
2006-05-12 04:41:27 +00:00
|
|
|
int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2004-10-19 21:14:57 +00:00
|
|
|
/* Forward declarations. */
|
2010-01-04 19:01:22 +00:00
|
|
|
static int ipfw_divert(struct mbuf **, int, struct ipfw_rule_ref *, int);
|
2012-09-04 19:43:26 +00:00
|
|
|
static int ipfw_check_packet(void *, struct mbuf **, struct ifnet *, int,
|
|
|
|
struct inpcb *);
|
|
|
|
static int ipfw_check_frame(void *, struct mbuf **, struct ifnet *, int,
|
|
|
|
struct inpcb *);
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2009-12-16 10:48:40 +00:00
|
|
|
#ifdef SYSCTL_NODE
|
Bring in the most recent version of ipfw and dummynet, developed
and tested over the past two months in the ipfw3-head branch. This
also happens to be the same code available in the Linux and Windows
ports of ipfw and dummynet.
The major enhancement is a completely restructured version of
dummynet, with support for different packet scheduling algorithms
(loadable at runtime), faster queue/pipe lookup, and a much cleaner
internal architecture and kernel/userland ABI which simplifies
future extensions.
In addition to the existing schedulers (FIFO and WF2Q+), we include
a Deficit Round Robin (DRR or RR for brevity) scheduler, and a new,
very fast version of WF2Q+ called QFQ.
Some test code is also present (in sys/netinet/ipfw/test) that
lets you build and test schedulers in userland.
Also, we have added a compatibility layer that understands requests
from the RELENG_7 and RELENG_8 versions of the /sbin/ipfw binaries,
and replies correctly (at least, it does its best; sometimes you
just cannot tell who sent the request and how to answer).
The compatibility layer should make it possible to MFC this code in a
relatively short time.
Some minor glitches (e.g. handling of ipfw set enable/disable,
and a workaround for a bug in RELENG_7's /sbin/ipfw) will be
fixed with separate commits.
CREDITS:
This work has been partly supported by the ONELAB2 project, and
mostly developed by Riccardo Panicucci and myself.
The code for the qfq scheduler is mostly from Fabio Checconi,
and Marta Carbone and Francesco Magno have helped with testing,
debugging and some bug fixes.
2010-03-02 17:40:48 +00:00
|
|
|
|
|
|
|
SYSBEGIN(f1)
|
|
|
|
|
2009-12-16 10:48:40 +00:00
|
|
|
SYSCTL_DECL(_net_inet_ip_fw);
|
|
|
|
SYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, enable,
|
|
|
|
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_enable), 0,
|
|
|
|
ipfw_chg_hook, "I", "Enable ipfw");
|
|
|
|
#ifdef INET6
|
|
|
|
SYSCTL_DECL(_net_inet6_ip6_fw);
|
|
|
|
SYSCTL_VNET_PROC(_net_inet6_ip6_fw, OID_AUTO, enable,
|
|
|
|
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw6_enable), 0,
|
|
|
|
ipfw_chg_hook, "I", "Enable ipfw+6");
|
|
|
|
#endif /* INET6 */
|
Bring in the most recent version of ipfw and dummynet, developed
and tested over the past two months in the ipfw3-head branch. This
also happens to be the same code available in the Linux and Windows
ports of ipfw and dummynet.
The major enhancement is a completely restructured version of
dummynet, with support for different packet scheduling algorithms
(loadable at runtime), faster queue/pipe lookup, and a much cleaner
internal architecture and kernel/userland ABI which simplifies
future extensions.
In addition to the existing schedulers (FIFO and WF2Q+), we include
a Deficit Round Robin (DRR or RR for brevity) scheduler, and a new,
very fast version of WF2Q+ called QFQ.
Some test code is also present (in sys/netinet/ipfw/test) that
lets you build and test schedulers in userland.
Also, we have added a compatibility layer that understands requests
from the RELENG_7 and RELENG_8 versions of the /sbin/ipfw binaries,
and replies correctly (at least, it does its best; sometimes you
just cannot tell who sent the request and how to answer).
The compatibility layer should make it possible to MFC this code in a
relatively short time.
Some minor glitches (e.g. handling of ipfw set enable/disable,
and a workaround for a bug in RELENG_7's /sbin/ipfw) will be
fixed with separate commits.
CREDITS:
This work has been partly supported by the ONELAB2 project, and
mostly developed by Riccardo Panicucci and myself.
The code for the qfq scheduler is mostly from Fabio Checconi,
and Marta Carbone and Francesco Magno have helped with testing,
debugging and some bug fixes.
2010-03-02 17:40:48 +00:00
|
|
|
|
2012-09-04 19:43:26 +00:00
|
|
|
SYSCTL_DECL(_net_link_ether);
|
|
|
|
SYSCTL_VNET_PROC(_net_link_ether, OID_AUTO, ipfw,
|
|
|
|
CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fwlink_enable), 0,
|
|
|
|
ipfw_chg_hook, "I", "Pass ether pkts through firewall");
|
|
|
|
|
Bring in the most recent version of ipfw and dummynet, developed
and tested over the past two months in the ipfw3-head branch. This
also happens to be the same code available in the Linux and Windows
ports of ipfw and dummynet.
The major enhancement is a completely restructured version of
dummynet, with support for different packet scheduling algorithms
(loadable at runtime), faster queue/pipe lookup, and a much cleaner
internal architecture and kernel/userland ABI which simplifies
future extensions.
In addition to the existing schedulers (FIFO and WF2Q+), we include
a Deficit Round Robin (DRR or RR for brevity) scheduler, and a new,
very fast version of WF2Q+ called QFQ.
Some test code is also present (in sys/netinet/ipfw/test) that
lets you build and test schedulers in userland.
Also, we have added a compatibility layer that understands requests
from the RELENG_7 and RELENG_8 versions of the /sbin/ipfw binaries,
and replies correctly (at least, it does its best; sometimes you
just cannot tell who sent the request and how to answer).
The compatibility layer should make it possible to MFC this code in a
relatively short time.
Some minor glitches (e.g. handling of ipfw set enable/disable,
and a workaround for a bug in RELENG_7's /sbin/ipfw) will be
fixed with separate commits.
CREDITS:
This work has been partly supported by the ONELAB2 project, and
mostly developed by Riccardo Panicucci and myself.
The code for the qfq scheduler is mostly from Fabio Checconi,
and Marta Carbone and Francesco Magno have helped with testing,
debugging and some bug fixes.
2010-03-02 17:40:48 +00:00
|
|
|
SYSEND
|
|
|
|
|
2009-12-16 10:48:40 +00:00
|
|
|
#endif /* SYSCTL_NODE */
|
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
/*
|
|
|
|
* The pfilter hook to pass packets to ipfw_chk and then to
|
|
|
|
* dummynet, divert, netgraph or other modules.
|
|
|
|
* The packet may be consumed.
|
|
|
|
*/
|
2012-09-04 19:43:26 +00:00
|
|
|
static int
|
|
|
|
ipfw_check_packet(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
|
2004-09-29 04:54:33 +00:00
|
|
|
struct inpcb *inp)
|
2004-08-17 22:05:54 +00:00
|
|
|
{
|
|
|
|
struct ip_fw_args args;
|
2010-01-04 19:01:22 +00:00
|
|
|
struct m_tag *tag;
|
2009-12-28 10:47:04 +00:00
|
|
|
int ipfw;
|
|
|
|
int ret;
|
2010-01-04 19:01:22 +00:00
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
/* convert dir to IPFW values */
|
|
|
|
dir = (dir == PFIL_IN) ? DIR_IN : DIR_OUT;
|
2004-08-17 22:05:54 +00:00
|
|
|
bzero(&args, sizeof(args));
|
|
|
|
|
2004-09-13 16:46:05 +00:00
|
|
|
again:
|
2010-01-04 19:01:22 +00:00
|
|
|
/*
|
|
|
|
* extract and remove the tag if present. If we are left
|
|
|
|
* with onepass, optimize the outgoing path.
|
|
|
|
*/
|
|
|
|
tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
|
|
|
|
if (tag != NULL) {
|
|
|
|
args.rule = *((struct ipfw_rule_ref *)(tag+1));
|
|
|
|
m_tag_delete(*m0, tag);
|
2012-10-08 22:58:28 +00:00
|
|
|
if (args.rule.info & IPFW_ONEPASS)
|
2011-06-21 06:06:47 +00:00
|
|
|
return (0);
|
2007-11-06 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
2004-08-17 22:05:54 +00:00
|
|
|
args.m = *m0;
|
2009-12-28 10:47:04 +00:00
|
|
|
args.oif = dir == DIR_OUT ? ifp : NULL;
|
2004-09-29 04:54:33 +00:00
|
|
|
args.inp = inp;
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2010-01-04 19:01:22 +00:00
|
|
|
ipfw = ipfw_chk(&args);
|
|
|
|
*m0 = args.m;
|
2009-04-27 17:37:36 +00:00
|
|
|
|
2005-01-14 09:00:46 +00:00
|
|
|
KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
|
|
|
|
__func__));
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
/* breaking out of the switch means drop */
|
|
|
|
ret = 0; /* default return value for pass */
|
2005-01-14 09:00:46 +00:00
|
|
|
switch (ipfw) {
|
|
|
|
case IP_FW_PASS:
|
2009-12-28 10:47:04 +00:00
|
|
|
/* next_hop may be set by ipfw_chk */
|
2011-08-20 17:05:11 +00:00
|
|
|
if (args.next_hop == NULL && args.next_hop6 == NULL)
|
2010-04-19 16:17:30 +00:00
|
|
|
break; /* pass */
|
2012-10-25 09:39:14 +00:00
|
|
|
#if (!defined(INET6) && !defined(INET))
|
2009-12-28 10:47:04 +00:00
|
|
|
ret = EACCES;
|
|
|
|
#else
|
2010-01-04 19:01:22 +00:00
|
|
|
{
|
|
|
|
struct m_tag *fwd_tag;
|
2011-08-20 17:05:11 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
KASSERT(args.next_hop == NULL || args.next_hop6 == NULL,
|
|
|
|
("%s: both next_hop=%p and next_hop6=%p not NULL", __func__,
|
|
|
|
args.next_hop, args.next_hop6));
|
|
|
|
#ifdef INET6
|
|
|
|
if (args.next_hop6 != NULL)
|
|
|
|
len = sizeof(struct sockaddr_in6);
|
|
|
|
#endif
|
|
|
|
#ifdef INET
|
|
|
|
if (args.next_hop != NULL)
|
|
|
|
len = sizeof(struct sockaddr_in);
|
|
|
|
#endif
|
2010-01-04 19:01:22 +00:00
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
/* Incoming packets should not be tagged so we do not
|
|
|
|
* m_tag_find. Outgoing packets may be tagged, so we
|
|
|
|
* reuse the tag if present.
|
|
|
|
*/
|
|
|
|
fwd_tag = (dir == DIR_IN) ? NULL :
|
|
|
|
m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
|
|
|
|
if (fwd_tag != NULL) {
|
|
|
|
m_tag_unlink(*m0, fwd_tag);
|
|
|
|
} else {
|
2011-08-20 17:05:11 +00:00
|
|
|
fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD, len,
|
|
|
|
M_NOWAIT);
|
2009-12-28 10:47:04 +00:00
|
|
|
if (fwd_tag == NULL) {
|
|
|
|
ret = EACCES;
|
|
|
|
break; /* i.e. drop */
|
|
|
|
}
|
|
|
|
}
|
2011-08-20 17:05:11 +00:00
|
|
|
#ifdef INET6
|
|
|
|
if (args.next_hop6 != NULL) {
|
|
|
|
bcopy(args.next_hop6, (fwd_tag+1), len);
|
|
|
|
if (in6_localip(&args.next_hop6->sin6_addr))
|
|
|
|
(*m0)->m_flags |= M_FASTFWD_OURS;
|
2012-11-02 01:20:55 +00:00
|
|
|
(*m0)->m_flags |= M_IP6_NEXTHOP;
|
2011-08-20 17:05:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef INET
|
|
|
|
if (args.next_hop != NULL) {
|
|
|
|
bcopy(args.next_hop, (fwd_tag+1), len);
|
|
|
|
if (in_localip(args.next_hop->sin_addr))
|
|
|
|
(*m0)->m_flags |= M_FASTFWD_OURS;
|
2012-11-02 01:20:55 +00:00
|
|
|
(*m0)->m_flags |= M_IP_NEXTHOP;
|
2011-08-20 17:05:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
2004-08-17 22:05:54 +00:00
|
|
|
m_tag_prepend(*m0, fwd_tag);
|
2010-01-04 19:01:22 +00:00
|
|
|
}
|
2012-10-25 09:39:14 +00:00
|
|
|
#endif /* INET || INET6 */
|
2009-12-28 10:47:04 +00:00
|
|
|
break;
|
2005-01-14 09:00:46 +00:00
|
|
|
|
|
|
|
case IP_FW_DENY:
|
2009-12-28 10:47:04 +00:00
|
|
|
ret = EACCES;
|
|
|
|
break; /* i.e. drop */
|
2005-01-14 09:00:46 +00:00
|
|
|
|
|
|
|
case IP_FW_DUMMYNET:
|
2009-12-28 10:47:04 +00:00
|
|
|
ret = EACCES;
|
2009-06-05 13:44:30 +00:00
|
|
|
if (ip_dn_io_ptr == NULL)
|
2009-12-28 10:47:04 +00:00
|
|
|
break; /* i.e. drop */
|
2005-04-18 18:35:05 +00:00
|
|
|
if (mtod(*m0, struct ip *)->ip_v == 4)
|
2009-12-28 10:47:04 +00:00
|
|
|
ret = ip_dn_io_ptr(m0, dir, &args);
|
2005-04-18 18:35:05 +00:00
|
|
|
else if (mtod(*m0, struct ip *)->ip_v == 6)
|
2009-12-28 10:47:04 +00:00
|
|
|
ret = ip_dn_io_ptr(m0, dir | PROTO_IPV6, &args);
|
|
|
|
else
|
|
|
|
break; /* drop it */
|
|
|
|
/*
|
|
|
|
* XXX should read the return value.
|
|
|
|
* dummynet normally eats the packet and sets *m0=NULL
|
|
|
|
* unless the packet can be sent immediately. In this
|
|
|
|
* case args is updated and we should re-run the
|
|
|
|
* check without clearing args.
|
|
|
|
*/
|
2007-11-06 23:01:42 +00:00
|
|
|
if (*m0 != NULL)
|
|
|
|
goto again;
|
2005-01-14 09:00:46 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IP_FW_TEE:
|
|
|
|
case IP_FW_DIVERT:
|
2009-12-28 10:47:04 +00:00
|
|
|
if (ip_divert_ptr == NULL) {
|
|
|
|
ret = EACCES;
|
|
|
|
break; /* i.e. drop */
|
|
|
|
}
|
2010-01-04 19:01:22 +00:00
|
|
|
ret = ipfw_divert(m0, dir, &args.rule,
|
|
|
|
(ipfw == IP_FW_TEE) ? 1 : 0);
|
|
|
|
/* continue processing for the original packet (tee). */
|
|
|
|
if (*m0)
|
|
|
|
goto again;
|
2009-12-28 10:47:04 +00:00
|
|
|
break;
|
2005-01-14 09:00:46 +00:00
|
|
|
|
2005-02-05 12:06:33 +00:00
|
|
|
case IP_FW_NGTEE:
|
|
|
|
case IP_FW_NETGRAPH:
|
2010-01-07 10:08:05 +00:00
|
|
|
if (ng_ipfw_input_p == NULL) {
|
2009-12-28 10:47:04 +00:00
|
|
|
ret = EACCES;
|
|
|
|
break; /* i.e. drop */
|
|
|
|
}
|
|
|
|
ret = ng_ipfw_input_p(m0, dir, &args,
|
|
|
|
(ipfw == IP_FW_NGTEE) ? 1 : 0);
|
|
|
|
if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
|
|
|
|
goto again; /* continue with packet */
|
|
|
|
break;
|
2005-02-05 12:06:33 +00:00
|
|
|
|
2006-12-29 21:59:17 +00:00
|
|
|
case IP_FW_NAT:
|
2010-09-28 23:23:23 +00:00
|
|
|
/* honor one-pass in case of successful nat */
|
|
|
|
if (V_fw_one_pass)
|
|
|
|
break; /* ret is already 0 */
|
|
|
|
goto again;
|
|
|
|
|
2009-04-01 20:23:47 +00:00
|
|
|
case IP_FW_REASS:
|
2009-12-28 10:47:04 +00:00
|
|
|
goto again; /* continue with packet */
|
2009-04-01 20:23:47 +00:00
|
|
|
|
2005-01-14 09:00:46 +00:00
|
|
|
default:
|
|
|
|
KASSERT(0, ("%s: unknown retval", __func__));
|
|
|
|
}
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
if (*m0)
|
2010-01-04 19:01:22 +00:00
|
|
|
FREE_PKT(*m0);
|
2009-12-28 10:47:04 +00:00
|
|
|
*m0 = NULL;
|
|
|
|
}
|
2012-10-06 10:02:11 +00:00
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
return ret;
|
2004-08-17 22:05:54 +00:00
|
|
|
}
|
|
|
|
|
2012-09-04 19:43:26 +00:00
|
|
|
/*
|
|
|
|
* ipfw processing for ethernet packets (in and out).
|
|
|
|
* Inteface is NULL from ether_demux, and ifp from
|
|
|
|
* ether_output_frame.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ipfw_check_frame(void *arg, struct mbuf **m0, struct ifnet *dst, int dir,
|
|
|
|
struct inpcb *inp)
|
|
|
|
{
|
|
|
|
struct ether_header *eh;
|
|
|
|
struct ether_header save_eh;
|
|
|
|
struct mbuf *m;
|
|
|
|
int i, ret;
|
|
|
|
struct ip_fw_args args;
|
|
|
|
struct m_tag *mtag;
|
|
|
|
|
|
|
|
/* fetch start point from rule, if any */
|
|
|
|
mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
|
|
|
|
if (mtag == NULL) {
|
|
|
|
args.rule.slot = 0;
|
|
|
|
} else {
|
|
|
|
/* dummynet packet, already partially processed */
|
|
|
|
struct ipfw_rule_ref *r;
|
|
|
|
|
|
|
|
/* XXX can we free it after use ? */
|
|
|
|
mtag->m_tag_id = PACKET_TAG_NONE;
|
|
|
|
r = (struct ipfw_rule_ref *)(mtag + 1);
|
|
|
|
if (r->info & IPFW_ONEPASS)
|
|
|
|
return (0);
|
|
|
|
args.rule = *r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* I need some amt of data to be contiguous */
|
|
|
|
m = *m0;
|
|
|
|
i = min(m->m_pkthdr.len, max_protohdr);
|
|
|
|
if (m->m_len < i) {
|
|
|
|
m = m_pullup(m, i);
|
|
|
|
if (m == NULL) {
|
|
|
|
*m0 = m;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eh = mtod(m, struct ether_header *);
|
|
|
|
save_eh = *eh; /* save copy for restore below */
|
|
|
|
m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */
|
|
|
|
|
|
|
|
args.m = m; /* the packet we are looking at */
|
|
|
|
args.oif = dst; /* destination, if any */
|
|
|
|
args.next_hop = NULL; /* we do not support forward yet */
|
|
|
|
args.next_hop6 = NULL; /* we do not support forward yet */
|
|
|
|
args.eh = &save_eh; /* MAC header for bridged/MAC packets */
|
|
|
|
args.inp = NULL; /* used by ipfw uid/gid/jail rules */
|
|
|
|
i = ipfw_chk(&args);
|
|
|
|
m = args.m;
|
|
|
|
if (m != NULL) {
|
|
|
|
/*
|
|
|
|
* Restore Ethernet header, as needed, in case the
|
|
|
|
* mbuf chain was replaced by ipfw.
|
|
|
|
*/
|
2012-12-05 08:04:20 +00:00
|
|
|
M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
|
2012-09-04 19:43:26 +00:00
|
|
|
if (m == NULL) {
|
|
|
|
*m0 = NULL;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (eh != mtod(m, struct ether_header *))
|
|
|
|
bcopy(&save_eh, mtod(m, struct ether_header *),
|
|
|
|
ETHER_HDR_LEN);
|
|
|
|
}
|
|
|
|
*m0 = m;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
/* Check result of ipfw_chk() */
|
|
|
|
switch (i) {
|
|
|
|
case IP_FW_PASS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IP_FW_DENY:
|
|
|
|
ret = EACCES;
|
|
|
|
break; /* i.e. drop */
|
|
|
|
|
|
|
|
case IP_FW_DUMMYNET:
|
|
|
|
ret = EACCES;
|
|
|
|
int dir;
|
|
|
|
|
|
|
|
if (ip_dn_io_ptr == NULL)
|
|
|
|
break; /* i.e. drop */
|
|
|
|
|
|
|
|
*m0 = NULL;
|
|
|
|
dir = PROTO_LAYER2 | (dst ? DIR_OUT : DIR_IN);
|
|
|
|
ip_dn_io_ptr(&m, dir, &args);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
KASSERT(0, ("%s: unknown retval", __func__));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
if (*m0)
|
|
|
|
FREE_PKT(*m0);
|
|
|
|
*m0 = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-01-04 19:01:22 +00:00
|
|
|
/* do the divert, return 1 on error 0 on success */
|
|
|
|
static int
|
|
|
|
ipfw_divert(struct mbuf **m0, int incoming, struct ipfw_rule_ref *rule,
|
|
|
|
int tee)
|
2004-08-17 22:05:54 +00:00
|
|
|
{
|
|
|
|
/*
|
2004-09-13 16:46:05 +00:00
|
|
|
* ipfw_chk() has already tagged the packet with the divert tag.
|
2004-08-17 22:05:54 +00:00
|
|
|
* If tee is set, copy packet and return original.
|
|
|
|
* If not tee, consume packet and send it to divert socket.
|
|
|
|
*/
|
2009-12-28 10:47:04 +00:00
|
|
|
struct mbuf *clone;
|
2011-06-27 12:21:11 +00:00
|
|
|
struct ip *ip = mtod(*m0, struct ip *);
|
2010-01-04 19:01:22 +00:00
|
|
|
struct m_tag *tag;
|
2004-10-19 21:14:57 +00:00
|
|
|
|
2004-08-17 22:05:54 +00:00
|
|
|
/* Cloning needed for tee? */
|
2009-12-28 10:47:04 +00:00
|
|
|
if (tee == 0) {
|
|
|
|
clone = *m0; /* use the original mbuf */
|
|
|
|
*m0 = NULL;
|
|
|
|
} else {
|
2012-12-05 08:04:20 +00:00
|
|
|
clone = m_dup(*m0, M_NOWAIT);
|
2009-12-28 10:47:04 +00:00
|
|
|
/* If we cannot duplicate the mbuf, we sacrifice the divert
|
|
|
|
* chain and continue with the tee-ed packet.
|
|
|
|
*/
|
|
|
|
if (clone == NULL)
|
2010-01-04 19:01:22 +00:00
|
|
|
return 1;
|
2009-12-28 10:47:04 +00:00
|
|
|
}
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
/*
|
2009-12-28 10:47:04 +00:00
|
|
|
* Divert listeners can normally handle non-fragmented packets,
|
|
|
|
* but we can only reass in the non-tee case.
|
|
|
|
* This means that listeners on a tee rule may get fragments,
|
|
|
|
* and have to live with that.
|
|
|
|
* Note that we now have the 'reass' ipfw option so if we care
|
|
|
|
* we can do it before a 'tee'.
|
2004-08-17 22:05:54 +00:00
|
|
|
*/
|
2011-06-27 12:21:11 +00:00
|
|
|
if (!tee) switch (ip->ip_v) {
|
|
|
|
case IPVERSION:
|
|
|
|
if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
|
2009-12-28 10:47:04 +00:00
|
|
|
int hlen;
|
|
|
|
struct mbuf *reass;
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
reass = ip_reass(clone); /* Reassemble packet. */
|
|
|
|
if (reass == NULL)
|
2010-01-04 19:01:22 +00:00
|
|
|
return 0; /* not an error */
|
2009-12-28 10:47:04 +00:00
|
|
|
/* if reass = NULL then it was consumed by ip_reass */
|
2004-08-17 22:05:54 +00:00
|
|
|
/*
|
|
|
|
* IP header checksum fixup after reassembly and leave header
|
|
|
|
* in network byte order.
|
|
|
|
*/
|
2009-12-28 10:47:04 +00:00
|
|
|
ip = mtod(reass, struct ip *);
|
|
|
|
hlen = ip->ip_hl << 2;
|
|
|
|
ip->ip_sum = 0;
|
|
|
|
if (hlen == sizeof(struct ip))
|
|
|
|
ip->ip_sum = in_cksum_hdr(ip);
|
|
|
|
else
|
|
|
|
ip->ip_sum = in_cksum(reass, hlen);
|
|
|
|
clone = reass;
|
2011-06-27 12:21:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
case IPV6_VERSION >> 4:
|
|
|
|
{
|
|
|
|
struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *);
|
|
|
|
|
|
|
|
if (ip6->ip6_nxt == IPPROTO_FRAGMENT) {
|
|
|
|
int nxt, off;
|
|
|
|
|
|
|
|
off = sizeof(struct ip6_hdr);
|
|
|
|
nxt = frag6_input(&clone, &off, 0);
|
|
|
|
if (nxt == IPPROTO_DONE)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2004-08-17 22:05:54 +00:00
|
|
|
}
|
2011-06-27 12:21:11 +00:00
|
|
|
|
2010-01-04 19:01:22 +00:00
|
|
|
/* attach a tag to the packet with the reinject info */
|
|
|
|
tag = m_tag_alloc(MTAG_IPFW_RULE, 0,
|
|
|
|
sizeof(struct ipfw_rule_ref), M_NOWAIT);
|
|
|
|
if (tag == NULL) {
|
|
|
|
FREE_PKT(clone);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*((struct ipfw_rule_ref *)(tag+1)) = *rule;
|
|
|
|
m_tag_prepend(clone, tag);
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
/* Do the dirty job... */
|
2009-12-28 10:47:04 +00:00
|
|
|
ip_divert_ptr(clone, incoming);
|
2010-01-04 19:01:22 +00:00
|
|
|
return 0;
|
2006-05-12 04:41:27 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
/*
|
|
|
|
* attach or detach hooks for a given protocol family
|
|
|
|
*/
|
2009-12-16 10:48:40 +00:00
|
|
|
static int
|
2009-12-28 10:47:04 +00:00
|
|
|
ipfw_hook(int onoff, int pf)
|
2006-05-12 04:41:27 +00:00
|
|
|
{
|
2009-12-28 10:47:04 +00:00
|
|
|
struct pfil_head *pfh;
|
2012-09-04 19:43:26 +00:00
|
|
|
void *hook_func;
|
2006-05-12 04:41:27 +00:00
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
pfh = pfil_head_get(PFIL_TYPE_AF, pf);
|
|
|
|
if (pfh == NULL)
|
2005-04-18 18:35:05 +00:00
|
|
|
return ENOENT;
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2012-09-04 19:43:26 +00:00
|
|
|
hook_func = (pf == AF_LINK) ? ipfw_check_frame : ipfw_check_packet;
|
|
|
|
|
2010-01-04 19:01:22 +00:00
|
|
|
(void) (onoff ? pfil_add_hook : pfil_remove_hook)
|
2012-09-04 19:43:26 +00:00
|
|
|
(hook_func, NULL, PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh);
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-05-12 04:41:27 +00:00
|
|
|
|
2009-12-16 10:48:40 +00:00
|
|
|
int
|
2009-12-28 10:47:04 +00:00
|
|
|
ipfw_attach_hooks(int arg)
|
2009-12-16 10:48:40 +00:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
2009-12-28 10:47:04 +00:00
|
|
|
if (arg == 0) /* detach */
|
|
|
|
ipfw_hook(0, AF_INET);
|
2010-04-19 16:17:30 +00:00
|
|
|
else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) {
|
2009-12-16 10:48:40 +00:00
|
|
|
error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */
|
|
|
|
printf("ipfw_hook() error\n");
|
|
|
|
}
|
|
|
|
#ifdef INET6
|
2009-12-28 10:47:04 +00:00
|
|
|
if (arg == 0) /* detach */
|
|
|
|
ipfw_hook(0, AF_INET6);
|
2010-04-19 16:17:30 +00:00
|
|
|
else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) {
|
2009-12-16 10:48:40 +00:00
|
|
|
error = ENOENT;
|
|
|
|
printf("ipfw6_hook() error\n");
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-04 19:43:26 +00:00
|
|
|
if (arg == 0) /* detach */
|
|
|
|
ipfw_hook(0, AF_LINK);
|
|
|
|
else if (V_fwlink_enable && ipfw_hook(1, AF_LINK) != 0) {
|
|
|
|
error = ENOENT;
|
|
|
|
printf("ipfw_link_hook() error\n");
|
|
|
|
}
|
2009-12-16 10:48:40 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2006-05-12 04:41:27 +00:00
|
|
|
int
|
|
|
|
ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
2012-09-04 19:43:26 +00:00
|
|
|
int *enable;
|
|
|
|
int newval;
|
2006-05-12 04:41:27 +00:00
|
|
|
int error;
|
2009-12-28 10:47:04 +00:00
|
|
|
int af;
|
2006-05-12 04:41:27 +00:00
|
|
|
|
2009-10-11 05:59:43 +00:00
|
|
|
if (arg1 == &VNET_NAME(fw_enable)) {
|
2012-09-04 19:43:26 +00:00
|
|
|
enable = &V_fw_enable;
|
2009-12-28 10:47:04 +00:00
|
|
|
af = AF_INET;
|
2009-10-11 05:59:43 +00:00
|
|
|
}
|
|
|
|
#ifdef INET6
|
|
|
|
else if (arg1 == &VNET_NAME(fw6_enable)) {
|
2012-09-04 19:43:26 +00:00
|
|
|
enable = &V_fw6_enable;
|
2009-12-28 10:47:04 +00:00
|
|
|
af = AF_INET6;
|
2009-10-11 05:59:43 +00:00
|
|
|
}
|
2009-08-21 11:20:10 +00:00
|
|
|
#endif
|
2012-09-04 19:43:26 +00:00
|
|
|
else if (arg1 == &VNET_NAME(fwlink_enable)) {
|
|
|
|
enable = &V_fwlink_enable;
|
|
|
|
af = AF_LINK;
|
|
|
|
}
|
2009-10-11 05:59:43 +00:00
|
|
|
else
|
|
|
|
return (EINVAL);
|
|
|
|
|
2012-09-04 19:43:26 +00:00
|
|
|
newval = *enable;
|
2009-10-11 05:59:43 +00:00
|
|
|
|
2012-09-04 19:43:26 +00:00
|
|
|
/* Handle sysctl change */
|
|
|
|
error = sysctl_handle_int(oidp, &newval, 0, req);
|
2009-10-11 05:59:43 +00:00
|
|
|
|
2006-05-12 04:41:27 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
2012-09-04 19:43:26 +00:00
|
|
|
/* Formalize new value */
|
|
|
|
newval = (newval) ? 1 : 0;
|
2006-05-12 04:41:27 +00:00
|
|
|
|
2012-09-04 19:43:26 +00:00
|
|
|
if (*enable == newval)
|
2006-05-12 04:41:27 +00:00
|
|
|
return (0);
|
|
|
|
|
2012-09-04 19:43:26 +00:00
|
|
|
error = ipfw_hook(newval, af);
|
2009-12-28 10:47:04 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2012-09-04 19:43:26 +00:00
|
|
|
*enable = newval;
|
2006-05-12 04:41:27 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2009-12-16 10:48:40 +00:00
|
|
|
/* end of file */
|