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-26 14:18:30 +00:00
|
|
|
#if !defined(KLD_MODULE)
|
2004-08-17 22:05:54 +00:00
|
|
|
#include "opt_ipfw.h"
|
|
|
|
#include "opt_ipdn.h"
|
|
|
|
#include "opt_inet.h"
|
|
|
|
#ifndef INET
|
|
|
|
#error IPFIREWALL requires INET.
|
|
|
|
#endif /* INET */
|
2004-08-27 15:16:24 +00:00
|
|
|
#endif /* KLD_MODULE */
|
2005-11-29 17:56:11 +00:00
|
|
|
#include "opt_inet6.h"
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
#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/socketvar.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/ucred.h>
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
#include <sys/vimage.h>
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/pfil.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/ip_fw.h>
|
|
|
|
#include <netinet/ip_divert.h>
|
|
|
|
#include <netinet/ip_dummynet.h>
|
2009-04-27 17:37:36 +00:00
|
|
|
#include <netinet/vinet.h>
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2005-02-05 12:06:33 +00:00
|
|
|
#include <netgraph/ng_ipfw.h>
|
|
|
|
|
2004-08-17 22:05:54 +00:00
|
|
|
#include <machine/in_cksum.h>
|
|
|
|
|
2008-12-11 16:26:38 +00:00
|
|
|
#ifdef VIMAGE_GLOBALS
|
2006-05-12 04:41:27 +00:00
|
|
|
int fw_enable = 1;
|
|
|
|
#ifdef INET6
|
|
|
|
int fw6_enable = 1;
|
|
|
|
#endif
|
2008-12-11 16:26:38 +00:00
|
|
|
#endif
|
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
|
|
|
/* Divert hooks. */
|
|
|
|
ip_divert_packet_t *ip_divert_ptr = NULL;
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2005-02-05 12:06:33 +00:00
|
|
|
/* ng_ipfw hooks. */
|
|
|
|
ng_ipfw_input_t *ng_ipfw_input_p = NULL;
|
|
|
|
|
2004-10-19 21:14:57 +00:00
|
|
|
/* Forward declarations. */
|
2004-08-17 22:05:54 +00:00
|
|
|
static int ipfw_divert(struct mbuf **, int, int);
|
2004-10-19 21:14:57 +00:00
|
|
|
#define DIV_DIR_IN 1
|
|
|
|
#define DIV_DIR_OUT 0
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
int
|
2004-09-29 04:54:33 +00:00
|
|
|
ipfw_check_in(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
|
|
|
|
struct inpcb *inp)
|
2004-08-17 22:05:54 +00:00
|
|
|
{
|
Permit buiding kernels with options VIMAGE, restricted to only a single
active network stack instance. Turning on options VIMAGE at compile
time yields the following changes relative to default kernel build:
1) V_ accessor macros for virtualized variables resolve to structure
fields via base pointers, instead of being resolved as fields in global
structs or plain global variables. As an example, V_ifnet becomes:
options VIMAGE: ((struct vnet_net *) vnet_net)->_ifnet
default build: vnet_net_0._ifnet
options VIMAGE_GLOBALS: ifnet
2) INIT_VNET_* macros will declare and set up base pointers to be used
by V_ accessor macros, instead of resolving to whitespace:
INIT_VNET_NET(ifp->if_vnet); becomes
struct vnet_net *vnet_net = (ifp->if_vnet)->mod_data[VNET_MOD_NET];
3) Memory for vnet modules registered via vnet_mod_register() is now
allocated at run time in sys/kern/kern_vimage.c, instead of per vnet
module structs being declared as globals. If required, vnet modules
can now request the framework to provide them with allocated bzeroed
memory by filling in the vmi_size field in their vmi_modinfo structures.
4) structs socket, ifnet, inpcbinfo, tcpcb and syncache_head are
extended to hold a pointer to the parent vnet. options VIMAGE builds
will fill in those fields as required.
5) curvnet is introduced as a new global variable in options VIMAGE
builds, always pointing to the default and only struct vnet.
6) struct sysctl_oid has been extended with additional two fields to
store major and minor virtualization module identifiers, oid_v_subs and
oid_v_mod. SYSCTL_V_* family of macros will fill in those fields
accordingly, and store the offset in the appropriate vnet container
struct in oid_arg1.
In sysctl handlers dealing with virtualized sysctls, the
SYSCTL_RESOLVE_V_ARG1() macro will compute the address of the target
variable and make it available in arg1 variable for further processing.
Unused fields in structs vnet_inet, vnet_inet6 and vnet_ipfw have
been deleted.
Reviewed by: bz, rwatson
Approved by: julian (mentor)
2009-04-30 13:36:26 +00:00
|
|
|
INIT_VNET_INET(curvnet);
|
2004-08-17 22:05:54 +00:00
|
|
|
struct ip_fw_args args;
|
2005-02-05 12:06:33 +00:00
|
|
|
struct ng_ipfw_tag *ng_tag;
|
2004-08-17 22:05:54 +00:00
|
|
|
struct m_tag *dn_tag;
|
|
|
|
int ipfw = 0;
|
|
|
|
int divert;
|
2005-01-14 09:00:46 +00:00
|
|
|
int tee;
|
2004-08-17 22:05:54 +00:00
|
|
|
#ifdef IPFIREWALL_FORWARD
|
|
|
|
struct m_tag *fwd_tag;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
KASSERT(dir == PFIL_IN, ("ipfw_check_in wrong direction!"));
|
|
|
|
|
|
|
|
bzero(&args, sizeof(args));
|
|
|
|
|
2005-02-05 12:06:33 +00:00
|
|
|
ng_tag = (struct ng_ipfw_tag *)m_tag_locate(*m0, NGM_IPFW_COOKIE, 0,
|
|
|
|
NULL);
|
|
|
|
if (ng_tag != NULL) {
|
|
|
|
KASSERT(ng_tag->dir == NG_IPFW_IN,
|
|
|
|
("ng_ipfw tag with wrong direction"));
|
|
|
|
args.rule = ng_tag->rule;
|
2009-06-09 21:27:11 +00:00
|
|
|
args.rule_id = ng_tag->rule_id;
|
|
|
|
args.chain_id = ng_tag->chain_id;
|
2005-02-05 12:06:33 +00:00
|
|
|
m_tag_delete(*m0, (struct m_tag *)ng_tag);
|
|
|
|
}
|
|
|
|
|
2004-09-13 16:46:05 +00:00
|
|
|
again:
|
2007-11-06 23:01:42 +00:00
|
|
|
dn_tag = m_tag_find(*m0, PACKET_TAG_DUMMYNET, NULL);
|
|
|
|
if (dn_tag != NULL){
|
|
|
|
struct dn_pkt_tag *dt;
|
|
|
|
|
|
|
|
dt = (struct dn_pkt_tag *)(dn_tag+1);
|
|
|
|
args.rule = dt->rule;
|
2009-06-09 21:27:11 +00:00
|
|
|
args.rule_id = dt->rule_id;
|
|
|
|
args.chain_id = dt->chain_id;
|
2007-11-06 23:01:42 +00:00
|
|
|
|
|
|
|
m_tag_delete(*m0, dn_tag);
|
|
|
|
}
|
|
|
|
|
2004-08-17 22:05:54 +00:00
|
|
|
args.m = *m0;
|
2004-09-29 04:54:33 +00:00
|
|
|
args.inp = inp;
|
2005-01-14 09:00:46 +00:00
|
|
|
tee = 0;
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2009-04-27 17:37:36 +00:00
|
|
|
if (V_fw_one_pass == 0 || args.rule == NULL) {
|
|
|
|
ipfw = ipfw_chk(&args);
|
|
|
|
*m0 = args.m;
|
|
|
|
} else
|
|
|
|
ipfw = IP_FW_PASS;
|
|
|
|
|
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
|
|
|
|
2005-01-14 09:00:46 +00:00
|
|
|
switch (ipfw) {
|
|
|
|
case IP_FW_PASS:
|
|
|
|
if (args.next_hop == NULL)
|
|
|
|
goto pass;
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
#ifdef IPFIREWALL_FORWARD
|
|
|
|
fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD,
|
|
|
|
sizeof(struct sockaddr_in), M_NOWAIT);
|
|
|
|
if (fwd_tag == NULL)
|
|
|
|
goto drop;
|
|
|
|
bcopy(args.next_hop, (fwd_tag+1), sizeof(struct sockaddr_in));
|
|
|
|
m_tag_prepend(*m0, fwd_tag);
|
|
|
|
|
|
|
|
if (in_localip(args.next_hop->sin_addr))
|
|
|
|
(*m0)->m_flags |= M_FASTFWD_OURS;
|
|
|
|
goto pass;
|
|
|
|
#endif
|
2005-01-14 09:00:46 +00:00
|
|
|
break; /* not reached */
|
|
|
|
|
|
|
|
case IP_FW_DENY:
|
|
|
|
goto drop;
|
|
|
|
break; /* not reached */
|
|
|
|
|
|
|
|
case IP_FW_DUMMYNET:
|
2009-06-05 13:44:30 +00:00
|
|
|
if (ip_dn_io_ptr == NULL)
|
2005-01-14 09:00:46 +00:00
|
|
|
goto drop;
|
2005-04-18 18:35:05 +00:00
|
|
|
if (mtod(*m0, struct ip *)->ip_v == 4)
|
2007-11-06 23:01:42 +00:00
|
|
|
ip_dn_io_ptr(m0, DN_TO_IP_IN, &args);
|
2005-04-18 18:35:05 +00:00
|
|
|
else if (mtod(*m0, struct ip *)->ip_v == 6)
|
2007-11-06 23:01:42 +00:00
|
|
|
ip_dn_io_ptr(m0, DN_TO_IP6_IN, &args);
|
|
|
|
if (*m0 != NULL)
|
|
|
|
goto again;
|
2005-01-14 09:00:46 +00:00
|
|
|
return 0; /* packet consumed */
|
|
|
|
|
|
|
|
case IP_FW_TEE:
|
|
|
|
tee = 1;
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case IP_FW_DIVERT:
|
|
|
|
divert = ipfw_divert(m0, DIV_DIR_IN, tee);
|
|
|
|
if (divert) {
|
|
|
|
*m0 = NULL;
|
|
|
|
return 0; /* packet consumed */
|
2005-04-06 14:00:33 +00:00
|
|
|
} else {
|
|
|
|
args.rule = NULL;
|
2005-01-14 09:00:46 +00:00
|
|
|
goto again; /* continue with packet */
|
2005-04-06 14:00:33 +00:00
|
|
|
}
|
2005-01-14 09:00:46 +00:00
|
|
|
|
2005-02-05 12:06:33 +00:00
|
|
|
case IP_FW_NGTEE:
|
|
|
|
if (!NG_IPFW_LOADED)
|
|
|
|
goto drop;
|
|
|
|
(void)ng_ipfw_input_p(m0, NG_IPFW_IN, &args, 1);
|
|
|
|
goto again; /* continue with packet */
|
|
|
|
|
|
|
|
case IP_FW_NETGRAPH:
|
|
|
|
if (!NG_IPFW_LOADED)
|
|
|
|
goto drop;
|
|
|
|
return ng_ipfw_input_p(m0, NG_IPFW_IN, &args, 0);
|
2006-12-29 21:59:17 +00:00
|
|
|
|
|
|
|
case IP_FW_NAT:
|
|
|
|
goto again; /* continue with packet */
|
2005-02-05 12:06:33 +00:00
|
|
|
|
2009-04-01 20:23:47 +00:00
|
|
|
case IP_FW_REASS:
|
|
|
|
goto again;
|
|
|
|
|
2005-01-14 09:00:46 +00:00
|
|
|
default:
|
|
|
|
KASSERT(0, ("%s: unknown retval", __func__));
|
|
|
|
}
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
drop:
|
|
|
|
if (*m0)
|
|
|
|
m_freem(*m0);
|
|
|
|
*m0 = NULL;
|
|
|
|
return (EACCES);
|
|
|
|
pass:
|
|
|
|
return 0; /* not filtered */
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-09-29 04:54:33 +00:00
|
|
|
ipfw_check_out(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
|
|
|
|
struct inpcb *inp)
|
2004-08-17 22:05:54 +00:00
|
|
|
{
|
Permit buiding kernels with options VIMAGE, restricted to only a single
active network stack instance. Turning on options VIMAGE at compile
time yields the following changes relative to default kernel build:
1) V_ accessor macros for virtualized variables resolve to structure
fields via base pointers, instead of being resolved as fields in global
structs or plain global variables. As an example, V_ifnet becomes:
options VIMAGE: ((struct vnet_net *) vnet_net)->_ifnet
default build: vnet_net_0._ifnet
options VIMAGE_GLOBALS: ifnet
2) INIT_VNET_* macros will declare and set up base pointers to be used
by V_ accessor macros, instead of resolving to whitespace:
INIT_VNET_NET(ifp->if_vnet); becomes
struct vnet_net *vnet_net = (ifp->if_vnet)->mod_data[VNET_MOD_NET];
3) Memory for vnet modules registered via vnet_mod_register() is now
allocated at run time in sys/kern/kern_vimage.c, instead of per vnet
module structs being declared as globals. If required, vnet modules
can now request the framework to provide them with allocated bzeroed
memory by filling in the vmi_size field in their vmi_modinfo structures.
4) structs socket, ifnet, inpcbinfo, tcpcb and syncache_head are
extended to hold a pointer to the parent vnet. options VIMAGE builds
will fill in those fields as required.
5) curvnet is introduced as a new global variable in options VIMAGE
builds, always pointing to the default and only struct vnet.
6) struct sysctl_oid has been extended with additional two fields to
store major and minor virtualization module identifiers, oid_v_subs and
oid_v_mod. SYSCTL_V_* family of macros will fill in those fields
accordingly, and store the offset in the appropriate vnet container
struct in oid_arg1.
In sysctl handlers dealing with virtualized sysctls, the
SYSCTL_RESOLVE_V_ARG1() macro will compute the address of the target
variable and make it available in arg1 variable for further processing.
Unused fields in structs vnet_inet, vnet_inet6 and vnet_ipfw have
been deleted.
Reviewed by: bz, rwatson
Approved by: julian (mentor)
2009-04-30 13:36:26 +00:00
|
|
|
INIT_VNET_INET(curvnet);
|
2004-08-17 22:05:54 +00:00
|
|
|
struct ip_fw_args args;
|
2005-02-05 12:06:33 +00:00
|
|
|
struct ng_ipfw_tag *ng_tag;
|
2004-08-17 22:05:54 +00:00
|
|
|
struct m_tag *dn_tag;
|
|
|
|
int ipfw = 0;
|
|
|
|
int divert;
|
2005-01-14 09:00:46 +00:00
|
|
|
int tee;
|
2004-08-17 22:05:54 +00:00
|
|
|
#ifdef IPFIREWALL_FORWARD
|
|
|
|
struct m_tag *fwd_tag;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
KASSERT(dir == PFIL_OUT, ("ipfw_check_out wrong direction!"));
|
|
|
|
|
|
|
|
bzero(&args, sizeof(args));
|
|
|
|
|
2005-02-05 12:06:33 +00:00
|
|
|
ng_tag = (struct ng_ipfw_tag *)m_tag_locate(*m0, NGM_IPFW_COOKIE, 0,
|
|
|
|
NULL);
|
|
|
|
if (ng_tag != NULL) {
|
|
|
|
KASSERT(ng_tag->dir == NG_IPFW_OUT,
|
|
|
|
("ng_ipfw tag with wrong direction"));
|
|
|
|
args.rule = ng_tag->rule;
|
2009-06-09 21:27:11 +00:00
|
|
|
args.rule_id = ng_tag->rule_id;
|
|
|
|
args.chain_id = ng_tag->chain_id;
|
2005-02-05 12:06:33 +00:00
|
|
|
m_tag_delete(*m0, (struct m_tag *)ng_tag);
|
|
|
|
}
|
|
|
|
|
2004-09-13 16:46:05 +00:00
|
|
|
again:
|
2007-11-06 23:01:42 +00:00
|
|
|
dn_tag = m_tag_find(*m0, PACKET_TAG_DUMMYNET, NULL);
|
|
|
|
if (dn_tag != NULL) {
|
|
|
|
struct dn_pkt_tag *dt;
|
|
|
|
|
|
|
|
dt = (struct dn_pkt_tag *)(dn_tag+1);
|
|
|
|
args.rule = dt->rule;
|
2009-06-09 21:27:11 +00:00
|
|
|
args.rule_id = dt->rule_id;
|
|
|
|
args.chain_id = dt->chain_id;
|
2007-11-06 23:01:42 +00:00
|
|
|
|
|
|
|
m_tag_delete(*m0, dn_tag);
|
|
|
|
}
|
|
|
|
|
2004-08-17 22:05:54 +00:00
|
|
|
args.m = *m0;
|
|
|
|
args.oif = ifp;
|
2004-09-29 04:54:33 +00:00
|
|
|
args.inp = inp;
|
2005-01-14 09:00:46 +00:00
|
|
|
tee = 0;
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2009-04-27 17:37:36 +00:00
|
|
|
if (V_fw_one_pass == 0 || args.rule == NULL) {
|
|
|
|
ipfw = ipfw_chk(&args);
|
|
|
|
*m0 = args.m;
|
|
|
|
} else
|
|
|
|
ipfw = IP_FW_PASS;
|
|
|
|
|
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
|
|
|
|
2005-01-14 09:00:46 +00:00
|
|
|
switch (ipfw) {
|
|
|
|
case IP_FW_PASS:
|
|
|
|
if (args.next_hop == NULL)
|
|
|
|
goto pass;
|
2004-08-17 22:05:54 +00:00
|
|
|
#ifdef IPFIREWALL_FORWARD
|
|
|
|
/* Overwrite existing tag. */
|
|
|
|
fwd_tag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
|
2004-09-13 19:20:14 +00:00
|
|
|
if (fwd_tag == NULL) {
|
2004-08-17 22:05:54 +00:00
|
|
|
fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD,
|
|
|
|
sizeof(struct sockaddr_in), M_NOWAIT);
|
2004-09-13 19:20:14 +00:00
|
|
|
if (fwd_tag == NULL)
|
|
|
|
goto drop;
|
|
|
|
} else
|
|
|
|
m_tag_unlink(*m0, fwd_tag);
|
2004-08-17 22:05:54 +00:00
|
|
|
bcopy(args.next_hop, (fwd_tag+1), sizeof(struct sockaddr_in));
|
|
|
|
m_tag_prepend(*m0, fwd_tag);
|
|
|
|
|
|
|
|
if (in_localip(args.next_hop->sin_addr))
|
|
|
|
(*m0)->m_flags |= M_FASTFWD_OURS;
|
|
|
|
goto pass;
|
|
|
|
#endif
|
2005-01-14 09:00:46 +00:00
|
|
|
break; /* not reached */
|
|
|
|
|
|
|
|
case IP_FW_DENY:
|
|
|
|
goto drop;
|
|
|
|
break; /* not reached */
|
|
|
|
|
|
|
|
case IP_FW_DUMMYNET:
|
2009-06-05 13:44:30 +00:00
|
|
|
if (ip_dn_io_ptr == NULL)
|
2005-01-14 09:00:46 +00:00
|
|
|
break;
|
2005-04-18 18:35:05 +00:00
|
|
|
if (mtod(*m0, struct ip *)->ip_v == 4)
|
2007-11-06 23:01:42 +00:00
|
|
|
ip_dn_io_ptr(m0, DN_TO_IP_OUT, &args);
|
2005-04-18 18:35:05 +00:00
|
|
|
else if (mtod(*m0, struct ip *)->ip_v == 6)
|
2007-11-06 23:01:42 +00:00
|
|
|
ip_dn_io_ptr(m0, DN_TO_IP6_OUT, &args);
|
|
|
|
if (*m0 != NULL)
|
|
|
|
goto again;
|
2005-01-14 09:00:46 +00:00
|
|
|
return 0; /* packet consumed */
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IP_FW_TEE:
|
|
|
|
tee = 1;
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case IP_FW_DIVERT:
|
|
|
|
divert = ipfw_divert(m0, DIV_DIR_OUT, tee);
|
|
|
|
if (divert) {
|
|
|
|
*m0 = NULL;
|
|
|
|
return 0; /* packet consumed */
|
2005-04-06 14:00:33 +00:00
|
|
|
} else {
|
|
|
|
args.rule = NULL;
|
2005-01-14 09:00:46 +00:00
|
|
|
goto again; /* continue with packet */
|
2005-04-06 14:00:33 +00:00
|
|
|
}
|
2005-01-14 09:00:46 +00:00
|
|
|
|
2005-02-05 12:06:33 +00:00
|
|
|
case IP_FW_NGTEE:
|
|
|
|
if (!NG_IPFW_LOADED)
|
|
|
|
goto drop;
|
|
|
|
(void)ng_ipfw_input_p(m0, NG_IPFW_OUT, &args, 1);
|
|
|
|
goto again; /* continue with packet */
|
|
|
|
|
|
|
|
case IP_FW_NETGRAPH:
|
|
|
|
if (!NG_IPFW_LOADED)
|
|
|
|
goto drop;
|
|
|
|
return ng_ipfw_input_p(m0, NG_IPFW_OUT, &args, 0);
|
|
|
|
|
2006-12-29 21:59:17 +00:00
|
|
|
case IP_FW_NAT:
|
|
|
|
goto again; /* continue with packet */
|
|
|
|
|
2009-04-01 20:23:47 +00:00
|
|
|
case IP_FW_REASS:
|
|
|
|
goto again;
|
|
|
|
|
2005-01-14 09:00:46 +00:00
|
|
|
default:
|
|
|
|
KASSERT(0, ("%s: unknown retval", __func__));
|
|
|
|
}
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
drop:
|
|
|
|
if (*m0)
|
|
|
|
m_freem(*m0);
|
|
|
|
*m0 = NULL;
|
|
|
|
return (EACCES);
|
|
|
|
pass:
|
|
|
|
return 0; /* not filtered */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipfw_divert(struct mbuf **m, int incoming, int tee)
|
|
|
|
{
|
|
|
|
/*
|
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.
|
|
|
|
*/
|
|
|
|
struct mbuf *clone, *reass;
|
|
|
|
struct ip *ip;
|
|
|
|
int hlen;
|
|
|
|
|
|
|
|
reass = NULL;
|
|
|
|
|
2004-10-19 21:14:57 +00:00
|
|
|
/* Is divert module loaded? */
|
|
|
|
if (ip_divert_ptr == NULL)
|
|
|
|
goto nodivert;
|
|
|
|
|
2004-08-17 22:05:54 +00:00
|
|
|
/* Cloning needed for tee? */
|
|
|
|
if (tee)
|
|
|
|
clone = m_dup(*m, M_DONTWAIT);
|
|
|
|
else
|
|
|
|
clone = *m;
|
|
|
|
|
|
|
|
/* In case m_dup was unable to allocate mbufs. */
|
|
|
|
if (clone == NULL)
|
|
|
|
goto teeout;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Divert listeners can only handle non-fragmented packets.
|
|
|
|
* However when tee is set we will *not* de-fragment the packets;
|
|
|
|
* Doing do would put the reassembly into double-jeopardy. On top
|
|
|
|
* of that someone doing a tee will probably want to get the packet
|
|
|
|
* in its original form.
|
|
|
|
*/
|
|
|
|
ip = mtod(clone, struct ip *);
|
|
|
|
if (!tee && ip->ip_off & (IP_MF | IP_OFFMASK)) {
|
|
|
|
|
|
|
|
/* Reassemble packet. */
|
|
|
|
reass = ip_reass(clone);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* IP header checksum fixup after reassembly and leave header
|
|
|
|
* in network byte order.
|
|
|
|
*/
|
|
|
|
if (reass != NULL) {
|
|
|
|
ip = mtod(reass, struct ip *);
|
|
|
|
hlen = ip->ip_hl << 2;
|
|
|
|
ip->ip_len = htons(ip->ip_len);
|
|
|
|
ip->ip_off = htons(ip->ip_off);
|
|
|
|
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;
|
|
|
|
} else
|
|
|
|
clone = NULL;
|
|
|
|
} else {
|
|
|
|
/* Convert header to network byte order. */
|
|
|
|
ip->ip_len = htons(ip->ip_len);
|
|
|
|
ip->ip_off = htons(ip->ip_off);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the dirty job... */
|
2004-10-19 21:14:57 +00:00
|
|
|
if (clone && ip_divert_ptr != NULL)
|
|
|
|
ip_divert_ptr(clone, incoming);
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
teeout:
|
2004-09-13 16:46:05 +00:00
|
|
|
/*
|
|
|
|
* For tee we leave the divert tag attached to original packet.
|
|
|
|
* It will then continue rule evaluation after the tee rule.
|
|
|
|
*/
|
|
|
|
if (tee)
|
|
|
|
return 0;
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
/* Packet diverted and consumed */
|
|
|
|
return 1;
|
2004-10-19 21:14:57 +00:00
|
|
|
|
|
|
|
nodivert:
|
2004-08-17 22:05:54 +00:00
|
|
|
m_freem(*m);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipfw_hook(void)
|
|
|
|
{
|
|
|
|
struct pfil_head *pfh_inet;
|
|
|
|
|
|
|
|
pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
|
|
|
|
if (pfh_inet == NULL)
|
|
|
|
return ENOENT;
|
|
|
|
|
2008-12-16 15:05:35 +00:00
|
|
|
(void)pfil_add_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK,
|
|
|
|
pfh_inet);
|
|
|
|
(void)pfil_add_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
|
|
|
|
pfh_inet);
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipfw_unhook(void)
|
|
|
|
{
|
|
|
|
struct pfil_head *pfh_inet;
|
|
|
|
|
|
|
|
pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
|
|
|
|
if (pfh_inet == NULL)
|
|
|
|
return ENOENT;
|
2006-05-12 04:41:27 +00:00
|
|
|
|
2008-12-16 15:05:35 +00:00
|
|
|
(void)pfil_remove_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK,
|
|
|
|
pfh_inet);
|
|
|
|
(void)pfil_remove_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
|
|
|
|
pfh_inet);
|
2006-05-12 04:41:27 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-18 18:35:05 +00:00
|
|
|
#ifdef INET6
|
2006-05-12 04:41:27 +00:00
|
|
|
static int
|
|
|
|
ipfw6_hook(void)
|
|
|
|
{
|
|
|
|
struct pfil_head *pfh_inet6;
|
|
|
|
|
|
|
|
pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
|
|
|
|
if (pfh_inet6 == NULL)
|
|
|
|
return ENOENT;
|
|
|
|
|
2008-12-16 15:05:35 +00:00
|
|
|
(void)pfil_add_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK,
|
|
|
|
pfh_inet6);
|
|
|
|
(void)pfil_add_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
|
|
|
|
pfh_inet6);
|
2006-05-12 04:41:27 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ipfw6_unhook(void)
|
|
|
|
{
|
|
|
|
struct pfil_head *pfh_inet6;
|
|
|
|
|
2005-04-18 18:35:05 +00:00
|
|
|
pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
|
|
|
|
if (pfh_inet6 == NULL)
|
|
|
|
return ENOENT;
|
2004-08-17 22:05:54 +00:00
|
|
|
|
2008-12-16 15:05:35 +00:00
|
|
|
(void)pfil_remove_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK,
|
|
|
|
pfh_inet6);
|
|
|
|
(void)pfil_remove_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
|
|
|
|
pfh_inet6);
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-05-12 04:41:27 +00:00
|
|
|
#endif /* INET6 */
|
|
|
|
|
|
|
|
int
|
|
|
|
ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
2008-11-26 22:32:07 +00:00
|
|
|
INIT_VNET_IPFW(curvnet);
|
2006-05-12 04:41:27 +00:00
|
|
|
int enable = *(int *)arg1;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = sysctl_handle_int(oidp, &enable, 0, req);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
enable = (enable) ? 1 : 0;
|
|
|
|
|
|
|
|
if (enable == *(int *)arg1)
|
|
|
|
return (0);
|
|
|
|
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (arg1 == &V_fw_enable) {
|
2006-05-12 04:41:27 +00:00
|
|
|
if (enable)
|
|
|
|
error = ipfw_hook();
|
|
|
|
else
|
|
|
|
error = ipfw_unhook();
|
|
|
|
}
|
|
|
|
#ifdef INET6
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (arg1 == &V_fw6_enable) {
|
2006-05-12 04:41:27 +00:00
|
|
|
if (enable)
|
|
|
|
error = ipfw6_hook();
|
|
|
|
else
|
|
|
|
error = ipfw6_unhook();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
*(int *)arg1 = enable;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2004-08-17 22:05:54 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
ipfw_modevent(module_t mod, int type, void *unused)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MOD_LOAD:
|
2006-05-12 04:41:27 +00:00
|
|
|
if ((err = ipfw_init()) != 0) {
|
|
|
|
printf("ipfw_init() error\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((err = ipfw_hook()) != 0) {
|
|
|
|
printf("ipfw_hook() error\n");
|
|
|
|
break;
|
2004-08-17 22:05:54 +00:00
|
|
|
}
|
2006-05-12 04:41:27 +00:00
|
|
|
#ifdef INET6
|
|
|
|
if ((err = ipfw6_hook()) != 0) {
|
|
|
|
printf("ipfw_hook() error\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2004-08-17 22:05:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MOD_UNLOAD:
|
2006-05-12 04:41:27 +00:00
|
|
|
if ((err = ipfw_unhook()) > 0)
|
|
|
|
break;
|
|
|
|
#ifdef INET6
|
|
|
|
if ((err = ipfw6_unhook()) > 0)
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
ipfw_destroy();
|
2004-08-17 22:05:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static moduledata_t ipfwmod = {
|
|
|
|
"ipfw",
|
|
|
|
ipfw_modevent,
|
|
|
|
0
|
|
|
|
};
|
2008-03-03 10:12:46 +00:00
|
|
|
DECLARE_MODULE(ipfw, ipfwmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY - 256);
|
2004-08-17 22:05:54 +00:00
|
|
|
MODULE_VERSION(ipfw, 2);
|