1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* ng_cisco.c
|
2005-01-07 01:45:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
1999-10-21 09:06:11 +00:00
|
|
|
* Copyright (c) 1996-1999 Whistle Communications, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Subject to the following obligations and disclaimer of warranty, use and
|
|
|
|
* redistribution of this software, in source or object code forms, with or
|
|
|
|
* without modifications are expressly permitted by Whistle Communications;
|
|
|
|
* provided, however, that:
|
|
|
|
* 1. Any and all reproductions of the source or object code must include the
|
|
|
|
* copyright notice above and the following disclaimer of warranties; and
|
|
|
|
* 2. No rights are granted, in any manner or form, to use Whistle
|
|
|
|
* Communications, Inc. trademarks, including the mark "WHISTLE
|
|
|
|
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
|
|
|
|
* such appears in the above copyright notice or in the software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
|
|
|
|
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
|
|
|
|
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
|
|
|
|
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
|
|
|
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
|
|
|
|
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
|
|
|
|
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
|
|
|
|
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
|
|
|
|
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
|
|
|
|
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER 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 WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
2000-10-24 17:32:45 +00:00
|
|
|
* Author: Julian Elischer <julian@freebsd.org>
|
1999-10-21 09:06:11 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
1999-11-01 10:00:40 +00:00
|
|
|
* $Whistle: ng_cisco.c,v 1.25 1999/11/01 09:24:51 julian Exp $
|
1999-10-21 09:06:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
|
|
|
|
#include <netatalk/at.h>
|
|
|
|
|
|
|
|
#include <netipx/ipx.h>
|
|
|
|
#include <netipx/ipx_if.h>
|
|
|
|
|
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/netgraph.h>
|
1999-11-30 02:45:32 +00:00
|
|
|
#include <netgraph/ng_parse.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <netgraph/ng_cisco.h>
|
|
|
|
|
2004-11-01 20:12:03 +00:00
|
|
|
#define CISCO_MULTICAST 0x8f /* Cisco multicast address */
|
|
|
|
#define CISCO_UNICAST 0x0f /* Cisco unicast address */
|
|
|
|
#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
|
|
|
|
#define CISCO_ADDR_REQ 0 /* Cisco address request */
|
|
|
|
#define CISCO_ADDR_REPLY 1 /* Cisco address reply */
|
|
|
|
#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
#define KEEPALIVE_SECS 10
|
|
|
|
|
|
|
|
struct cisco_header {
|
|
|
|
u_char address;
|
|
|
|
u_char control;
|
|
|
|
u_short protocol;
|
|
|
|
};
|
|
|
|
|
2004-11-01 20:12:03 +00:00
|
|
|
#define CISCO_HEADER_LEN sizeof (struct cisco_header)
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
struct cisco_packet {
|
|
|
|
u_long type;
|
|
|
|
u_long par1;
|
|
|
|
u_long par2;
|
|
|
|
u_short rel;
|
|
|
|
u_short time0;
|
|
|
|
u_short time1;
|
|
|
|
};
|
|
|
|
|
2004-11-01 20:12:03 +00:00
|
|
|
#define CISCO_PACKET_LEN (sizeof(struct cisco_packet))
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
struct protoent {
|
|
|
|
hook_p hook; /* the hook for this proto */
|
|
|
|
u_short af; /* address family, -1 = downstream */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cisco_priv {
|
|
|
|
u_long local_seq;
|
|
|
|
u_long remote_seq;
|
1999-11-30 02:45:32 +00:00
|
|
|
u_long seqRetries; /* how many times we've been here throwing out
|
1999-10-21 09:06:11 +00:00
|
|
|
* the same sequence number without ack */
|
|
|
|
node_p node;
|
2004-11-23 09:30:27 +00:00
|
|
|
struct callout handle;
|
1999-10-21 09:06:11 +00:00
|
|
|
struct protoent downstream;
|
|
|
|
struct protoent inet; /* IP information */
|
|
|
|
struct in_addr localip;
|
|
|
|
struct in_addr localmask;
|
2000-05-10 16:54:00 +00:00
|
|
|
struct protoent inet6; /* IPv6 information */
|
1999-10-21 09:06:11 +00:00
|
|
|
struct protoent atalk; /* AppleTalk information */
|
|
|
|
struct protoent ipx; /* IPX information */
|
|
|
|
};
|
|
|
|
typedef struct cisco_priv *sc_p;
|
|
|
|
|
|
|
|
/* Netgraph methods */
|
1999-11-01 10:00:40 +00:00
|
|
|
static ng_constructor_t cisco_constructor;
|
|
|
|
static ng_rcvmsg_t cisco_rcvmsg;
|
2001-01-06 00:46:47 +00:00
|
|
|
static ng_shutdown_t cisco_shutdown;
|
1999-11-01 10:00:40 +00:00
|
|
|
static ng_newhook_t cisco_newhook;
|
|
|
|
static ng_rcvdata_t cisco_rcvdata;
|
|
|
|
static ng_disconnect_t cisco_disconnect;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Other functions */
|
2001-01-06 00:46:47 +00:00
|
|
|
static int cisco_input(sc_p sc, item_p item);
|
2004-11-23 09:30:27 +00:00
|
|
|
static void cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2);
|
1999-10-21 09:06:11 +00:00
|
|
|
static int cisco_send(sc_p sc, int type, long par1, long par2);
|
2004-11-23 09:30:27 +00:00
|
|
|
static void cisco_notify(sc_p sc, uint32_t cmd);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
1999-11-30 02:45:32 +00:00
|
|
|
/* Parse type for struct ng_cisco_ipaddr */
|
2002-05-31 23:48:03 +00:00
|
|
|
static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
|
|
|
|
= NG_CISCO_IPADDR_TYPE_INFO;
|
1999-11-30 02:45:32 +00:00
|
|
|
static const struct ng_parse_type ng_cisco_ipaddr_type = {
|
|
|
|
&ng_parse_struct_type,
|
2002-05-31 23:48:03 +00:00
|
|
|
&ng_cisco_ipaddr_type_fields
|
1999-11-30 02:45:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Parse type for struct ng_async_stat */
|
2002-05-31 23:48:03 +00:00
|
|
|
static const struct ng_parse_struct_field ng_cisco_stats_type_fields[]
|
|
|
|
= NG_CISCO_STATS_TYPE_INFO;
|
1999-11-30 02:45:32 +00:00
|
|
|
static const struct ng_parse_type ng_cisco_stats_type = {
|
|
|
|
&ng_parse_struct_type,
|
2002-05-31 23:48:03 +00:00
|
|
|
&ng_cisco_stats_type_fields
|
1999-11-30 02:45:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* List of commands and how to convert arguments to/from ASCII */
|
|
|
|
static const struct ng_cmdlist ng_cisco_cmdlist[] = {
|
|
|
|
{
|
|
|
|
NGM_CISCO_COOKIE,
|
|
|
|
NGM_CISCO_SET_IPADDR,
|
|
|
|
"setipaddr",
|
|
|
|
&ng_cisco_ipaddr_type,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_CISCO_COOKIE,
|
|
|
|
NGM_CISCO_GET_IPADDR,
|
|
|
|
"getipaddr",
|
|
|
|
NULL,
|
|
|
|
&ng_cisco_ipaddr_type
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_CISCO_COOKIE,
|
|
|
|
NGM_CISCO_GET_STATUS,
|
|
|
|
"getstats",
|
|
|
|
NULL,
|
|
|
|
&ng_cisco_stats_type
|
|
|
|
},
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Node type */
|
|
|
|
static struct ng_type typestruct = {
|
2004-05-29 00:51:19 +00:00
|
|
|
.version = NG_ABI_VERSION,
|
|
|
|
.name = NG_CISCO_NODE_TYPE,
|
|
|
|
.constructor = cisco_constructor,
|
|
|
|
.rcvmsg = cisco_rcvmsg,
|
|
|
|
.shutdown = cisco_shutdown,
|
|
|
|
.newhook = cisco_newhook,
|
|
|
|
.rcvdata = cisco_rcvdata,
|
|
|
|
.disconnect = cisco_disconnect,
|
|
|
|
.cmdlist = ng_cisco_cmdlist,
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
NETGRAPH_INIT(cisco, &typestruct);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Node constructor
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
cisco_constructor(node_p node)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
sc_p sc;
|
|
|
|
|
2000-11-18 15:17:43 +00:00
|
|
|
MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (sc == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
|
2004-11-23 09:30:27 +00:00
|
|
|
ng_callout_init(&sc->handle);
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, sc);
|
2001-01-06 00:46:47 +00:00
|
|
|
sc->node = node;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Initialise the varous protocol hook holders */
|
|
|
|
sc->downstream.af = 0xffff;
|
|
|
|
sc->inet.af = AF_INET;
|
2000-05-10 16:54:00 +00:00
|
|
|
sc->inet6.af = AF_INET6;
|
1999-10-21 09:06:11 +00:00
|
|
|
sc->atalk.af = AF_APPLETALK;
|
|
|
|
sc->ipx.af = AF_IPX;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check new hook
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cisco_newhook(node_p node, hook_p hook, const char *name)
|
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const sc_p sc = NG_NODE_PRIVATE(node);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
if (strcmp(name, NG_CISCO_HOOK_DOWNSTREAM) == 0) {
|
|
|
|
sc->downstream.hook = hook;
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Start keepalives */
|
2004-12-01 11:56:32 +00:00
|
|
|
ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
|
2004-11-23 09:30:27 +00:00
|
|
|
&cisco_keepalive, (void *)sc, 0);
|
1999-10-21 09:06:11 +00:00
|
|
|
} else if (strcmp(name, NG_CISCO_HOOK_INET) == 0) {
|
|
|
|
sc->inet.hook = hook;
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_HOOK_SET_PRIVATE(hook, &sc->inet);
|
1999-10-21 09:06:11 +00:00
|
|
|
} else if (strcmp(name, NG_CISCO_HOOK_APPLETALK) == 0) {
|
|
|
|
sc->atalk.hook = hook;
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_HOOK_SET_PRIVATE(hook, &sc->atalk);
|
1999-10-21 09:06:11 +00:00
|
|
|
} else if (strcmp(name, NG_CISCO_HOOK_IPX) == 0) {
|
|
|
|
sc->ipx.hook = hook;
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_HOOK_SET_PRIVATE(hook, &sc->ipx);
|
1999-10-21 09:06:11 +00:00
|
|
|
} else if (strcmp(name, NG_CISCO_HOOK_DEBUG) == 0) {
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_HOOK_SET_PRIVATE(hook, NULL); /* unimplemented */
|
1999-10-21 09:06:11 +00:00
|
|
|
} else
|
|
|
|
return (EINVAL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive control message.
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
cisco_rcvmsg(node_p node, item_p item, hook_p lasthook)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-01-06 00:46:47 +00:00
|
|
|
struct ng_mesg *msg;
|
2001-01-08 05:34:06 +00:00
|
|
|
const sc_p sc = NG_NODE_PRIVATE(node);
|
1999-10-21 09:06:11 +00:00
|
|
|
struct ng_mesg *resp = NULL;
|
|
|
|
int error = 0;
|
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
NGI_GET_MSG(item, msg);
|
1999-10-21 09:06:11 +00:00
|
|
|
switch (msg->header.typecookie) {
|
|
|
|
case NGM_GENERIC_COOKIE:
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_TEXT_STATUS:
|
|
|
|
{
|
|
|
|
char *arg;
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
NG_MKRESPONSE(resp, msg, sizeof(struct ng_mesg)
|
|
|
|
+ NG_TEXTRESPONSE, M_NOWAIT);
|
|
|
|
if (resp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
arg = (char *) resp->data;
|
|
|
|
pos = sprintf(arg,
|
|
|
|
"keepalive period: %d sec; ", KEEPALIVE_SECS);
|
|
|
|
pos += sprintf(arg + pos,
|
1999-11-30 02:45:32 +00:00
|
|
|
"unacknowledged keepalives: %ld", sc->seqRetries);
|
1999-10-21 09:06:11 +00:00
|
|
|
resp->header.arglen = pos + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NGM_CISCO_COOKIE:
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_CISCO_GET_IPADDR: /* could be a late reply! */
|
|
|
|
if ((msg->header.flags & NGF_RESP) == 0) {
|
|
|
|
struct in_addr *ips;
|
|
|
|
|
|
|
|
NG_MKRESPONSE(resp, msg,
|
|
|
|
2 * sizeof(*ips), M_NOWAIT);
|
|
|
|
if (!resp) {
|
|
|
|
error = ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ips = (struct in_addr *) resp->data;
|
|
|
|
ips[0] = sc->localip;
|
|
|
|
ips[1] = sc->localmask;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */ /* ...if it's a reply */
|
|
|
|
case NGM_CISCO_SET_IPADDR:
|
|
|
|
{
|
|
|
|
struct in_addr *const ips = (struct in_addr *)msg->data;
|
|
|
|
|
|
|
|
if (msg->header.arglen < 2 * sizeof(*ips)) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sc->localip = ips[0];
|
|
|
|
sc->localmask = ips[1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGM_CISCO_GET_STATUS:
|
|
|
|
{
|
1999-11-30 02:45:32 +00:00
|
|
|
struct ng_cisco_stats *stat;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
NG_MKRESPONSE(resp, msg, sizeof(*stat), M_NOWAIT);
|
|
|
|
if (!resp) {
|
|
|
|
error = ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
1999-11-30 02:45:32 +00:00
|
|
|
stat = (struct ng_cisco_stats *)resp->data;
|
|
|
|
stat->seqRetries = sc->seqRetries;
|
|
|
|
stat->keepAlivePeriod = KEEPALIVE_SECS;
|
1999-10-21 09:06:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_RESPOND_MSG(error, node, item, resp);
|
|
|
|
NG_FREE_MSG(msg);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive data
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
cisco_rcvdata(hook_p hook, item_p item)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
1999-10-21 09:06:11 +00:00
|
|
|
struct protoent *pep;
|
|
|
|
struct cisco_header *h;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct mbuf *m;
|
2004-11-01 20:12:03 +00:00
|
|
|
int error = 0;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2001-01-08 05:34:06 +00:00
|
|
|
if ((pep = NG_HOOK_PRIVATE(hook)) == NULL)
|
1999-10-21 09:06:11 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* If it came from our downlink, deal with it separately */
|
|
|
|
if (pep->af == 0xffff)
|
2001-01-06 00:46:47 +00:00
|
|
|
return (cisco_input(sc, item));
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* OK so it came from a protocol, heading out. Prepend general data
|
|
|
|
packet header. For now, IP,IPX only */
|
2001-01-06 00:46:47 +00:00
|
|
|
m = NGI_M(item); /* still associated with item */
|
2003-02-19 05:47:46 +00:00
|
|
|
M_PREPEND(m, CISCO_HEADER_LEN, M_DONTWAIT);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (!m) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
h = mtod(m, struct cisco_header *);
|
2000-03-17 17:06:33 +00:00
|
|
|
h->address = CISCO_UNICAST;
|
1999-10-21 09:06:11 +00:00
|
|
|
h->control = 0;
|
|
|
|
|
|
|
|
switch (pep->af) {
|
|
|
|
case AF_INET: /* Internet Protocol */
|
|
|
|
h->protocol = htons(ETHERTYPE_IP);
|
|
|
|
break;
|
2000-05-10 16:54:00 +00:00
|
|
|
case AF_INET6:
|
|
|
|
h->protocol = htons(ETHERTYPE_IPV6);
|
|
|
|
break;
|
1999-10-21 09:06:11 +00:00
|
|
|
case AF_APPLETALK: /* AppleTalk Protocol */
|
|
|
|
h->protocol = htons(ETHERTYPE_AT);
|
|
|
|
break;
|
|
|
|
case AF_IPX: /* Novell IPX Protocol */
|
|
|
|
h->protocol = htons(ETHERTYPE_IPX);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EAFNOSUPPORT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send it */
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (error);
|
|
|
|
|
|
|
|
out:
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shutdown node
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
cisco_shutdown(node_p node)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const sc_p sc = NG_NODE_PRIVATE(node);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, NULL);
|
|
|
|
NG_NODE_UNREF(sc->node);
|
1999-10-21 09:06:11 +00:00
|
|
|
FREE(sc, M_NETGRAPH);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disconnection of a hook
|
|
|
|
*
|
|
|
|
* For this type, removal of the last link destroys the node
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cisco_disconnect(hook_p hook)
|
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
1999-10-21 09:06:11 +00:00
|
|
|
struct protoent *pep;
|
|
|
|
|
|
|
|
/* Check it's not the debug hook */
|
2001-01-08 05:34:06 +00:00
|
|
|
if ((pep = NG_HOOK_PRIVATE(hook))) {
|
1999-10-21 09:06:11 +00:00
|
|
|
pep->hook = NULL;
|
2004-11-23 09:30:27 +00:00
|
|
|
if (pep->af == 0xffff)
|
1999-10-21 09:06:11 +00:00
|
|
|
/* If it is the downstream hook, stop the timers */
|
2004-12-01 11:56:32 +00:00
|
|
|
ng_uncallout(&sc->handle, NG_HOOK_NODE(hook));
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If no more hooks, remove the node */
|
2001-01-08 05:34:06 +00:00
|
|
|
if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
|
|
|
|
&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
|
|
|
|
ng_rmnode_self(NG_HOOK_NODE(hook));
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive data
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
cisco_input(sc_p sc, item_p item)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2002-06-05 23:29:29 +00:00
|
|
|
const struct cisco_header *h;
|
|
|
|
struct cisco_header hdrbuf;
|
1999-10-21 09:06:11 +00:00
|
|
|
struct protoent *pep;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct mbuf *m;
|
2004-11-01 20:12:03 +00:00
|
|
|
int error = 0;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2002-06-05 23:29:29 +00:00
|
|
|
/* Get data */
|
2001-01-06 00:46:47 +00:00
|
|
|
m = NGI_M(item);
|
2002-06-05 23:29:29 +00:00
|
|
|
|
|
|
|
/* Sanity check header length */
|
|
|
|
if (m->m_pkthdr.len < sizeof(*h)) {
|
|
|
|
error = EINVAL;
|
1999-10-21 09:06:11 +00:00
|
|
|
goto drop;
|
2002-06-05 23:29:29 +00:00
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2002-06-05 23:29:29 +00:00
|
|
|
/* Get cisco header */
|
|
|
|
if (m->m_len >= sizeof(*h)) /* the common case */
|
|
|
|
h = mtod(m, const struct cisco_header *);
|
|
|
|
else {
|
|
|
|
m_copydata(m, 0, sizeof(*h), (caddr_t)&hdrbuf);
|
|
|
|
h = &hdrbuf;
|
|
|
|
}
|
|
|
|
m_adj(m, sizeof(*h));
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2002-06-05 23:29:29 +00:00
|
|
|
/* Check header address */
|
1999-10-21 09:06:11 +00:00
|
|
|
switch (h->address) {
|
|
|
|
default: /* Invalid Cisco packet. */
|
|
|
|
goto drop;
|
|
|
|
case CISCO_UNICAST:
|
|
|
|
case CISCO_MULTICAST:
|
|
|
|
/* Don't check the control field here (RFC 1547). */
|
|
|
|
switch (ntohs(h->protocol)) {
|
|
|
|
default:
|
|
|
|
goto drop;
|
|
|
|
case CISCO_KEEPALIVE:
|
2002-06-05 23:29:29 +00:00
|
|
|
{
|
|
|
|
const struct cisco_packet *p;
|
|
|
|
struct cisco_packet pktbuf;
|
|
|
|
|
|
|
|
/* Sanity check packet length */
|
|
|
|
if (m->m_pkthdr.len < sizeof(*p)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get cisco packet */
|
|
|
|
if (m->m_len >= sizeof(*p)) /* the common case */
|
|
|
|
p = mtod(m, const struct cisco_packet *);
|
|
|
|
else {
|
|
|
|
m_copydata(m, 0, sizeof(*p), (caddr_t)&pktbuf);
|
|
|
|
p = &pktbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check packet type */
|
1999-10-21 09:06:11 +00:00
|
|
|
switch (ntohl(p->type)) {
|
|
|
|
default:
|
|
|
|
log(LOG_WARNING,
|
2001-06-15 07:35:25 +00:00
|
|
|
"cisco: unknown cisco packet type: 0x%lx\n",
|
2001-10-29 02:22:49 +00:00
|
|
|
(long)ntohl(p->type));
|
1999-10-21 09:06:11 +00:00
|
|
|
break;
|
|
|
|
case CISCO_ADDR_REPLY:
|
|
|
|
/* Reply on address request, ignore */
|
|
|
|
break;
|
|
|
|
case CISCO_KEEPALIVE_REQ:
|
|
|
|
sc->remote_seq = ntohl(p->par1);
|
|
|
|
if (sc->local_seq == ntohl(p->par2)) {
|
|
|
|
sc->local_seq++;
|
2004-11-23 09:30:27 +00:00
|
|
|
if (sc->seqRetries > 1)
|
|
|
|
cisco_notify(sc, NGM_LINK_IS_UP);
|
1999-11-30 02:45:32 +00:00
|
|
|
sc->seqRetries = 0;
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CISCO_ADDR_REQ:
|
|
|
|
{
|
2001-01-06 00:46:47 +00:00
|
|
|
struct ng_mesg *msg;
|
|
|
|
int dummy_error = 0;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Ask inet peer for IP address information */
|
|
|
|
if (sc->inet.hook == NULL)
|
|
|
|
goto nomsg;
|
|
|
|
NG_MKMESSAGE(msg, NGM_CISCO_COOKIE,
|
|
|
|
NGM_CISCO_GET_IPADDR, 0, M_NOWAIT);
|
|
|
|
if (msg == NULL)
|
|
|
|
goto nomsg;
|
2002-08-22 00:30:03 +00:00
|
|
|
NG_SEND_MSG_HOOK(dummy_error,
|
|
|
|
sc->node, msg, sc->inet.hook, 0);
|
2001-01-06 00:46:47 +00:00
|
|
|
/*
|
|
|
|
* XXX Now maybe we should set a flag telling
|
|
|
|
* our receiver to send this message when the response comes in
|
|
|
|
* instead of now when the data may be bad.
|
|
|
|
*/
|
1999-10-21 09:06:11 +00:00
|
|
|
nomsg:
|
|
|
|
/* Send reply to peer device */
|
|
|
|
error = cisco_send(sc, CISCO_ADDR_REPLY,
|
|
|
|
ntohl(sc->localip.s_addr),
|
|
|
|
ntohl(sc->localmask.s_addr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto drop;
|
2002-06-05 23:29:29 +00:00
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
case ETHERTYPE_IP:
|
|
|
|
pep = &sc->inet;
|
|
|
|
break;
|
2000-05-10 16:54:00 +00:00
|
|
|
case ETHERTYPE_IPV6:
|
|
|
|
pep = &sc->inet6;
|
|
|
|
break;
|
1999-10-21 09:06:11 +00:00
|
|
|
case ETHERTYPE_AT:
|
|
|
|
pep = &sc->atalk;
|
|
|
|
break;
|
|
|
|
case ETHERTYPE_IPX:
|
|
|
|
pep = &sc->ipx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-06-05 23:29:29 +00:00
|
|
|
/* Drop if payload is empty */
|
|
|
|
if (m->m_pkthdr.len == 0) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto drop;
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Send it on */
|
|
|
|
if (pep->hook == NULL)
|
|
|
|
goto drop;
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FWD_NEW_DATA(error, item, pep->hook, m);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (error);
|
|
|
|
|
|
|
|
drop:
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_ITEM(item);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send keepalive packets, every 10 seconds.
|
|
|
|
*/
|
|
|
|
static void
|
2004-11-23 09:30:27 +00:00
|
|
|
cisco_keepalive(node_p node, hook_p hook, void *arg1, int arg2)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2004-11-23 09:30:27 +00:00
|
|
|
const sc_p sc = arg1;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
cisco_send(sc, CISCO_KEEPALIVE_REQ, sc->local_seq, sc->remote_seq);
|
2004-11-23 09:30:27 +00:00
|
|
|
if (sc->seqRetries++ > 1)
|
|
|
|
cisco_notify(sc, NGM_LINK_IS_DOWN);
|
2004-12-01 11:56:32 +00:00
|
|
|
ng_callout(&sc->handle, node, NULL, (hz * KEEPALIVE_SECS),
|
2004-11-23 09:30:27 +00:00
|
|
|
&cisco_keepalive, (void *)sc, 0);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send Cisco keepalive packet.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cisco_send(sc_p sc, int type, long par1, long par2)
|
|
|
|
{
|
|
|
|
struct cisco_header *h;
|
|
|
|
struct cisco_packet *ch;
|
|
|
|
struct mbuf *m;
|
2004-11-01 20:12:03 +00:00
|
|
|
struct timeval time;
|
1999-10-21 09:06:11 +00:00
|
|
|
u_long t;
|
|
|
|
int error = 0;
|
|
|
|
|
2004-01-21 19:20:57 +00:00
|
|
|
getmicrouptime(&time);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2003-02-19 05:47:46 +00:00
|
|
|
MGETHDR(m, M_DONTWAIT, MT_DATA);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (!m)
|
|
|
|
return (ENOBUFS);
|
|
|
|
|
2004-01-21 19:20:57 +00:00
|
|
|
t = time.tv_sec * 1000 + time.tv_usec / 1000;
|
1999-10-21 09:06:11 +00:00
|
|
|
m->m_pkthdr.len = m->m_len = CISCO_HEADER_LEN + CISCO_PACKET_LEN;
|
|
|
|
m->m_pkthdr.rcvif = 0;
|
|
|
|
|
|
|
|
h = mtod(m, struct cisco_header *);
|
|
|
|
h->address = CISCO_MULTICAST;
|
|
|
|
h->control = 0;
|
|
|
|
h->protocol = htons(CISCO_KEEPALIVE);
|
|
|
|
|
|
|
|
ch = (struct cisco_packet *) (h + 1);
|
|
|
|
ch->type = htonl(type);
|
|
|
|
ch->par1 = htonl(par1);
|
|
|
|
ch->par2 = htonl(par2);
|
|
|
|
ch->rel = -1;
|
|
|
|
ch->time0 = htons((u_short) (t >> 16));
|
|
|
|
ch->time1 = htons((u_short) t);
|
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_SEND_DATA_ONLY(error, sc->downstream.hook, m);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2004-11-23 09:30:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Send linkstate to upstream node.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
cisco_notify(sc_p sc, uint32_t cmd)
|
|
|
|
{
|
|
|
|
struct ng_mesg *msg;
|
|
|
|
int dummy_error = 0;
|
|
|
|
|
|
|
|
if (sc->inet.hook == NULL) /* nothing to notify */
|
|
|
|
return;
|
|
|
|
|
|
|
|
NG_MKMESSAGE(msg, NGM_FLOW_COOKIE, cmd, 0, M_NOWAIT);
|
|
|
|
if (msg != NULL)
|
|
|
|
NG_SEND_MSG_HOOK(dummy_error, sc->node, msg, sc->inet.hook, 0);
|
|
|
|
}
|