MFC rev. 1.1 of ng_nat.h and rev. 1.5,1.9,1.10 of ng_nat.c

- Add support for setmode and settarget messages.
 - Allow node to bypass traffic while no alias address defined.
 - Fix result of some mechanical change.
This commit is contained in:
mav 2007-07-25 19:10:39 +00:00
parent b23f752cc2
commit bb968fb086
2 changed files with 115 additions and 11 deletions
sys/netgraph

View File

@ -58,6 +58,16 @@ static ng_disconnect_t ng_nat_disconnect;
static struct mbuf * m_megapullup(struct mbuf *, int);
static unsigned int ng_nat_translate_flags(unsigned int x);
/* Parse type for struct ng_nat_mode. */
static const struct ng_parse_struct_field ng_nat_mode_fields[]
= NG_NAT_MODE_INFO;
static const struct ng_parse_type ng_nat_mode_type = {
&ng_parse_struct_type,
ng_nat_mode_fields
};
/* List of commands and how to convert arguments to/from ASCII. */
static const struct ng_cmdlist ng_nat_cmdlist[] = {
{
@ -67,6 +77,20 @@ static const struct ng_cmdlist ng_nat_cmdlist[] = {
&ng_parse_ipaddr_type,
NULL
},
{
NGM_NAT_COOKIE,
NGM_NAT_SET_MODE,
"setmode",
&ng_nat_mode_type,
NULL
},
{
NGM_NAT_COOKIE,
NGM_NAT_SET_TARGET,
"settarget",
&ng_parse_ipaddr_type,
NULL
},
{ 0 }
};
@ -86,17 +110,17 @@ NETGRAPH_INIT(nat, &typestruct);
MODULE_DEPEND(ng_nat, libalias, 1, 1, 1);
/* Information we store for each node. */
struct ng_priv_priv {
struct ng_nat_priv {
node_p node; /* back pointer to node */
hook_p in; /* hook for demasquerading */
hook_p out; /* hook for masquerading */
struct libalias *lib; /* libalias handler */
uint32_t flags; /* status flags */
};
typedef struct ng_priv_priv *priv_p;
typedef struct ng_nat_priv *priv_p;
/* Values of flags */
#define NGNAT_READY 0x1 /* We have everything to work */
#define NGNAT_CONNECTED 0x1 /* We have both hooks connected */
#define NGNAT_ADDR_DEFINED 0x2 /* NGM_NAT_SET_IPADDR happened */
static int
@ -147,9 +171,8 @@ ng_nat_newhook(node_p node, hook_p hook, const char *name)
return (EINVAL);
if (priv->out != NULL &&
priv->in != NULL &&
priv->flags & NGNAT_ADDR_DEFINED)
priv->flags |= NGNAT_READY;
priv->in != NULL)
priv->flags |= NGNAT_CONNECTED;
return(0);
}
@ -179,9 +202,36 @@ ng_nat_rcvmsg(node_p node, item_p item, hook_p lasthook)
LibAliasSetAddress(priv->lib, *ia);
priv->flags |= NGNAT_ADDR_DEFINED;
if (priv->out != NULL &&
priv->in != NULL)
priv->flags |= NGNAT_READY;
}
break;
case NGM_NAT_SET_MODE:
{
struct ng_nat_mode *const mode =
(struct ng_nat_mode *)msg->data;
if (msg->header.arglen < sizeof(*mode)) {
error = EINVAL;
break;
}
if (LibAliasSetMode(priv->lib,
ng_nat_translate_flags(mode->flags),
ng_nat_translate_flags(mode->mask)) < 0) {
error = ENOMEM;
break;
}
}
break;
case NGM_NAT_SET_TARGET:
{
struct in_addr *const ia = (struct in_addr *)msg->data;
if (msg->header.arglen < sizeof(*ia)) {
error = EINVAL;
break;
}
LibAliasSetTarget(priv->lib, *ia);
}
break;
default:
@ -208,11 +258,16 @@ ng_nat_rcvdata(hook_p hook, item_p item )
int rval, error = 0;
char *c;
if (!(priv->flags & NGNAT_READY)) {
/* We have no required hooks. */
if (!(priv->flags & NGNAT_CONNECTED)) {
NG_FREE_ITEM(item);
return (ENXIO);
}
/* We have no alias address yet to do anything. */
if (!(priv->flags & NGNAT_ADDR_DEFINED))
goto send;
m = NGI_M(item);
if ((m = m_megapullup(m, m->m_pkthdr.len)) == NULL) {
@ -290,6 +345,7 @@ ng_nat_rcvdata(hook_p hook, item_p item )
}
}
send:
if (hook == priv->in)
NG_FWD_ITEM_HOOK(error, item, priv->out);
else
@ -316,7 +372,7 @@ ng_nat_disconnect(hook_p hook)
{
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
priv->flags &= ~NGNAT_READY;
priv->flags &= ~NGNAT_CONNECTED;
if (hook == priv->out)
priv->out = NULL;
@ -329,6 +385,31 @@ ng_nat_disconnect(hook_p hook)
return (0);
}
static unsigned int
ng_nat_translate_flags(unsigned int x)
{
unsigned int res = 0;
#ifdef PKT_ALIAS_LOG
if (x & NG_NAT_LOG)
res |= PKT_ALIAS_LOG;
#endif
if (x & NG_NAT_DENY_INCOMING)
res |= PKT_ALIAS_DENY_INCOMING;
if (x & NG_NAT_SAME_PORTS)
res |= PKT_ALIAS_SAME_PORTS;
if (x & NG_NAT_UNREGISTERED_ONLY)
res |= PKT_ALIAS_UNREGISTERED_ONLY;
if (x & NG_NAT_RESET_ON_ADDR_CHANGE)
res |= PKT_ALIAS_RESET_ON_ADDR_CHANGE;
if (x & NG_NAT_PROXY_ONLY)
res |= PKT_ALIAS_PROXY_ONLY;
if (x & NG_NAT_REVERSE)
res |= PKT_ALIAS_REVERSE;
return (res);
}
/*
* m_megapullup() function is a big hack.
*

View File

@ -32,6 +32,29 @@
#define NG_NAT_HOOK_IN "in"
#define NG_NAT_HOOK_OUT "out"
/* Arguments for NGM_NAT_SET_MODE message */
struct ng_nat_mode {
uint32_t flags;
uint32_t mask;
};
/* Keep this in sync with the above structure definition */
#define NG_NAT_MODE_INFO { \
{ "flags", &ng_parse_uint32_type }, \
{ "mask", &ng_parse_uint32_type }, \
{ NULL } \
}
#define NG_NAT_LOG 0x01
#define NG_NAT_DENY_INCOMING 0x02
#define NG_NAT_SAME_PORTS 0x04
#define NG_NAT_UNREGISTERED_ONLY 0x10
#define NG_NAT_RESET_ON_ADDR_CHANGE 0x20
#define NG_NAT_PROXY_ONLY 0x40
#define NG_NAT_REVERSE 0x80
enum {
NGM_NAT_SET_IPADDR = 1,
NGM_NAT_SET_MODE,
NGM_NAT_SET_TARGET,
};