1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* ng_iface.c
|
|
|
|
*
|
|
|
|
* 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: Archie Cobbs <archie@freebsd.org>
|
1999-10-21 09:06:11 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
1999-11-01 10:00:40 +00:00
|
|
|
* $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
|
1999-10-21 09:06:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This node is also a system networking interface. It has
|
|
|
|
* a hook for each protocol (IP, AppleTalk, IPX, etc). Packets
|
|
|
|
* are simply relayed between the interface and the hooks.
|
|
|
|
*
|
2000-03-13 19:18:10 +00:00
|
|
|
* Interfaces are named ng0, ng1, etc. New nodes take the
|
|
|
|
* first available interface name.
|
1999-10-21 09:06:11 +00:00
|
|
|
*
|
|
|
|
* This node also includes Berkeley packet filter support.
|
|
|
|
*/
|
|
|
|
|
2003-03-08 16:25:27 +00:00
|
|
|
#include "opt_atalk.h"
|
|
|
|
#include "opt_inet.h"
|
|
|
|
#include "opt_inet6.h"
|
|
|
|
#include "opt_ipx.h"
|
|
|
|
|
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/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/errno.h>
|
2003-03-04 23:19:55 +00:00
|
|
|
#include <sys/random.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <sys/sockio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/syslog.h>
|
2000-03-13 19:18:10 +00:00
|
|
|
#include <sys/libkern.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_types.h>
|
2000-03-13 19:18:10 +00:00
|
|
|
#include <net/bpf.h>
|
2003-03-04 23:19:55 +00:00
|
|
|
#include <net/netisr.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
|
1999-11-30 02:45:32 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/netgraph.h>
|
2000-03-13 19:18:10 +00:00
|
|
|
#include <netgraph/ng_parse.h>
|
1999-10-21 09:06:11 +00:00
|
|
|
#include <netgraph/ng_iface.h>
|
|
|
|
#include <netgraph/ng_cisco.h>
|
|
|
|
|
2001-01-10 07:13:58 +00:00
|
|
|
#ifdef NG_SEPARATE_MALLOC
|
|
|
|
MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
|
|
|
|
#else
|
|
|
|
#define M_NETGRAPH_IFACE M_NETGRAPH
|
|
|
|
#endif
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/* This struct describes one address family */
|
|
|
|
struct iffam {
|
2000-03-13 19:18:10 +00:00
|
|
|
sa_family_t family; /* Address family */
|
|
|
|
const char *hookname; /* Name for hook */
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
typedef const struct iffam *iffam_p;
|
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
/* List of address families supported by our interface */
|
1999-10-21 09:06:11 +00:00
|
|
|
const static struct iffam gFamilies[] = {
|
2000-03-13 19:18:10 +00:00
|
|
|
{ AF_INET, NG_IFACE_HOOK_INET },
|
|
|
|
{ AF_INET6, NG_IFACE_HOOK_INET6 },
|
|
|
|
{ AF_APPLETALK, NG_IFACE_HOOK_ATALK },
|
|
|
|
{ AF_IPX, NG_IFACE_HOOK_IPX },
|
|
|
|
{ AF_ATM, NG_IFACE_HOOK_ATM },
|
|
|
|
{ AF_NATM, NG_IFACE_HOOK_NATM },
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
#define NUM_FAMILIES (sizeof(gFamilies) / sizeof(*gFamilies))
|
|
|
|
|
|
|
|
/* Node private data */
|
1999-11-19 04:25:39 +00:00
|
|
|
struct ng_iface_private {
|
2000-03-13 19:18:10 +00:00
|
|
|
struct ifnet *ifp; /* Our interface */
|
|
|
|
int unit; /* Interface unit number */
|
1999-10-21 09:06:11 +00:00
|
|
|
node_p node; /* Our netgraph node */
|
|
|
|
hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
|
|
|
|
};
|
1999-11-19 04:25:39 +00:00
|
|
|
typedef struct ng_iface_private *priv_p;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Interface methods */
|
|
|
|
static void ng_iface_start(struct ifnet *ifp);
|
|
|
|
static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
|
|
|
|
static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
|
2000-03-13 19:18:10 +00:00
|
|
|
struct sockaddr *dst, struct rtentry *rt0);
|
|
|
|
static void ng_iface_bpftap(struct ifnet *ifp,
|
|
|
|
struct mbuf *m, sa_family_t family);
|
1999-10-21 09:06:11 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Netgraph methods */
|
1999-11-01 10:00:40 +00:00
|
|
|
static ng_constructor_t ng_iface_constructor;
|
|
|
|
static ng_rcvmsg_t ng_iface_rcvmsg;
|
2001-01-06 00:46:47 +00:00
|
|
|
static ng_shutdown_t ng_iface_shutdown;
|
1999-11-01 10:00:40 +00:00
|
|
|
static ng_newhook_t ng_iface_newhook;
|
|
|
|
static ng_rcvdata_t ng_iface_rcvdata;
|
|
|
|
static ng_disconnect_t ng_iface_disconnect;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Helper stuff */
|
2000-03-13 19:18:10 +00:00
|
|
|
static iffam_p get_iffam_from_af(sa_family_t family);
|
1999-10-21 09:06:11 +00:00
|
|
|
static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
|
|
|
|
static iffam_p get_iffam_from_name(const char *name);
|
|
|
|
static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
|
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
/* Parse type for struct ng_iface_ifname */
|
|
|
|
static const struct ng_parse_fixedstring_info ng_iface_ifname_info = {
|
|
|
|
NG_IFACE_IFACE_NAME_MAX + 1
|
|
|
|
};
|
|
|
|
static const struct ng_parse_type ng_iface_ifname_type = {
|
|
|
|
&ng_parse_fixedstring_type,
|
|
|
|
&ng_iface_ifname_info
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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;
|
2000-03-13 19:18:10 +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
|
2000-03-13 19:18:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* List of commands and how to convert arguments to/from ASCII */
|
|
|
|
static const struct ng_cmdlist ng_iface_cmds[] = {
|
|
|
|
{
|
|
|
|
NGM_IFACE_COOKIE,
|
|
|
|
NGM_IFACE_GET_IFNAME,
|
|
|
|
"getifname",
|
|
|
|
NULL,
|
|
|
|
&ng_iface_ifname_type
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_IFACE_COOKIE,
|
|
|
|
NGM_IFACE_POINT2POINT,
|
|
|
|
"point2point",
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_IFACE_COOKIE,
|
|
|
|
NGM_IFACE_BROADCAST,
|
|
|
|
"broadcast",
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_CISCO_COOKIE,
|
|
|
|
NGM_CISCO_GET_IPADDR,
|
|
|
|
"getipaddr",
|
|
|
|
NULL,
|
|
|
|
&ng_cisco_ipaddr_type
|
|
|
|
},
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Node type descriptor */
|
|
|
|
static struct ng_type typestruct = {
|
2000-12-18 20:03:32 +00:00
|
|
|
NG_ABI_VERSION,
|
1999-10-21 09:06:11 +00:00
|
|
|
NG_IFACE_NODE_TYPE,
|
|
|
|
NULL,
|
|
|
|
ng_iface_constructor,
|
|
|
|
ng_iface_rcvmsg,
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_iface_shutdown,
|
1999-10-21 09:06:11 +00:00
|
|
|
ng_iface_newhook,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
ng_iface_rcvdata,
|
1999-11-30 02:45:32 +00:00
|
|
|
ng_iface_disconnect,
|
2000-03-13 19:18:10 +00:00
|
|
|
ng_iface_cmds
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
NETGRAPH_INIT(iface, &typestruct);
|
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
/* We keep a bitmap indicating which unit numbers are free.
|
|
|
|
One means the unit number is free, zero means it's taken. */
|
|
|
|
static int *ng_iface_units = NULL;
|
|
|
|
static int ng_iface_units_len = 0;
|
2001-01-10 07:13:58 +00:00
|
|
|
static int ng_units_in_use = 0;
|
2000-03-13 19:18:10 +00:00
|
|
|
|
|
|
|
#define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY)
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
HELPER STUFF
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the family descriptor from the family ID
|
|
|
|
*/
|
|
|
|
static __inline__ iffam_p
|
2000-03-13 19:18:10 +00:00
|
|
|
get_iffam_from_af(sa_family_t family)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
iffam_p iffam;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < NUM_FAMILIES; k++) {
|
|
|
|
iffam = &gFamilies[k];
|
2000-03-13 19:18:10 +00:00
|
|
|
if (iffam->family == family)
|
1999-10-21 09:06:11 +00:00
|
|
|
return (iffam);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the family descriptor from the hook
|
|
|
|
*/
|
|
|
|
static __inline__ iffam_p
|
|
|
|
get_iffam_from_hook(priv_p priv, hook_p hook)
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < NUM_FAMILIES; k++)
|
|
|
|
if (priv->hooks[k] == hook)
|
|
|
|
return (&gFamilies[k]);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the hook from the iffam descriptor
|
|
|
|
*/
|
|
|
|
|
|
|
|
static __inline__ hook_p *
|
|
|
|
get_hook_from_iffam(priv_p priv, iffam_p iffam)
|
|
|
|
{
|
|
|
|
return (&priv->hooks[iffam - gFamilies]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the iffam descriptor from the name
|
|
|
|
*/
|
|
|
|
static __inline__ iffam_p
|
|
|
|
get_iffam_from_name(const char *name)
|
|
|
|
{
|
|
|
|
iffam_p iffam;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (k = 0; k < NUM_FAMILIES; k++) {
|
|
|
|
iffam = &gFamilies[k];
|
|
|
|
if (!strcmp(iffam->hookname, name))
|
|
|
|
return (iffam);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
/*
|
|
|
|
* Find the first free unit number for a new interface.
|
|
|
|
* Increase the size of the unit bitmap as necessary.
|
|
|
|
*/
|
|
|
|
static __inline__ int
|
|
|
|
ng_iface_get_unit(int *unit)
|
|
|
|
{
|
|
|
|
int index, bit;
|
|
|
|
|
|
|
|
for (index = 0; index < ng_iface_units_len
|
|
|
|
&& ng_iface_units[index] == 0; index++);
|
|
|
|
if (index == ng_iface_units_len) { /* extend array */
|
|
|
|
int i, *newarray, newlen;
|
|
|
|
|
|
|
|
newlen = (2 * ng_iface_units_len) + 4;
|
|
|
|
MALLOC(newarray, int *, newlen * sizeof(*ng_iface_units),
|
2001-01-10 07:13:58 +00:00
|
|
|
M_NETGRAPH_IFACE, M_NOWAIT);
|
2000-03-13 19:18:10 +00:00
|
|
|
if (newarray == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
bcopy(ng_iface_units, newarray,
|
|
|
|
ng_iface_units_len * sizeof(*ng_iface_units));
|
|
|
|
for (i = ng_iface_units_len; i < newlen; i++)
|
|
|
|
newarray[i] = ~0;
|
|
|
|
if (ng_iface_units != NULL)
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(ng_iface_units, M_NETGRAPH_IFACE);
|
2000-03-13 19:18:10 +00:00
|
|
|
ng_iface_units = newarray;
|
|
|
|
ng_iface_units_len = newlen;
|
|
|
|
}
|
|
|
|
bit = ffs(ng_iface_units[index]) - 1;
|
|
|
|
KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
|
2001-12-10 08:09:49 +00:00
|
|
|
("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
|
2000-03-13 19:18:10 +00:00
|
|
|
ng_iface_units[index] &= ~(1 << bit);
|
|
|
|
*unit = (index * UNITS_BITSPERWORD) + bit;
|
2001-01-10 07:13:58 +00:00
|
|
|
ng_units_in_use++;
|
2000-03-13 19:18:10 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a no longer needed unit number.
|
|
|
|
*/
|
|
|
|
static __inline__ void
|
|
|
|
ng_iface_free_unit(int unit)
|
|
|
|
{
|
|
|
|
int index, bit;
|
|
|
|
|
|
|
|
index = unit / UNITS_BITSPERWORD;
|
|
|
|
bit = unit % UNITS_BITSPERWORD;
|
|
|
|
KASSERT(index < ng_iface_units_len,
|
2001-12-10 08:09:49 +00:00
|
|
|
("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
|
2000-03-13 19:18:10 +00:00
|
|
|
KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
|
2001-12-10 08:09:49 +00:00
|
|
|
("%s: unit=%d is free", __func__, unit));
|
2000-03-13 19:18:10 +00:00
|
|
|
ng_iface_units[index] |= (1 << bit);
|
|
|
|
/*
|
|
|
|
* XXX We could think about reducing the size of ng_iface_units[]
|
|
|
|
* XXX here if the last portion is all ones
|
2001-01-10 07:13:58 +00:00
|
|
|
* XXX At least free it if no more units.
|
|
|
|
* Needed if we are to eventually be able to unload.
|
2000-03-13 19:18:10 +00:00
|
|
|
*/
|
2001-01-10 07:13:58 +00:00
|
|
|
ng_units_in_use--;
|
|
|
|
if (ng_units_in_use == 0) { /* XXX make SMP safe */
|
|
|
|
FREE(ng_iface_units, M_NETGRAPH_IFACE);
|
|
|
|
ng_iface_units_len = 0;
|
|
|
|
ng_iface_units = NULL;
|
|
|
|
}
|
2000-03-13 19:18:10 +00:00
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/************************************************************************
|
|
|
|
INTERFACE STUFF
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process an ioctl for the virtual interface
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
|
|
|
|
{
|
|
|
|
struct ifreq *const ifr = (struct ifreq *) data;
|
|
|
|
int s, error = 0;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
ng_iface_print_ioctl(ifp, command, data);
|
|
|
|
#endif
|
|
|
|
s = splimp();
|
|
|
|
switch (command) {
|
|
|
|
|
|
|
|
/* These two are mostly handled at a higher layer */
|
|
|
|
case SIOCSIFADDR:
|
|
|
|
ifp->if_flags |= (IFF_UP | IFF_RUNNING);
|
|
|
|
ifp->if_flags &= ~(IFF_OACTIVE);
|
|
|
|
break;
|
|
|
|
case SIOCGIFADDR:
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Set flags */
|
|
|
|
case SIOCSIFFLAGS:
|
|
|
|
/*
|
|
|
|
* If the interface is marked up and stopped, then start it.
|
|
|
|
* If it is marked down and running, then stop it.
|
|
|
|
*/
|
|
|
|
if (ifr->ifr_flags & IFF_UP) {
|
|
|
|
if (!(ifp->if_flags & IFF_RUNNING)) {
|
|
|
|
ifp->if_flags &= ~(IFF_OACTIVE);
|
|
|
|
ifp->if_flags |= IFF_RUNNING;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ifp->if_flags & IFF_RUNNING)
|
|
|
|
ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Set the interface MTU */
|
|
|
|
case SIOCSIFMTU:
|
|
|
|
if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
|
|
|
|
|| ifr->ifr_mtu < NG_IFACE_MTU_MIN)
|
|
|
|
error = EINVAL;
|
|
|
|
else
|
|
|
|
ifp->if_mtu = ifr->ifr_mtu;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Stuff that's not supported */
|
|
|
|
case SIOCADDMULTI:
|
|
|
|
case SIOCDELMULTI:
|
1999-11-08 03:08:59 +00:00
|
|
|
error = 0;
|
|
|
|
break;
|
1999-10-21 09:06:11 +00:00
|
|
|
case SIOCSIFPHYS:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(void) splx(s);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine is called to deliver a packet out the interface.
|
|
|
|
* We simply look at the address family and relay the packet to
|
|
|
|
* the corresponding hook, if it exists and is connected.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
ng_iface_output(struct ifnet *ifp, struct mbuf *m,
|
|
|
|
struct sockaddr *dst, struct rtentry *rt0)
|
|
|
|
{
|
|
|
|
const priv_p priv = (priv_p) ifp->if_softc;
|
|
|
|
const iffam_p iffam = get_iffam_from_af(dst->sa_family);
|
|
|
|
meta_p meta = NULL;
|
|
|
|
int len, error = 0;
|
|
|
|
|
|
|
|
/* Check interface flags */
|
|
|
|
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
|
|
|
|
m_freem(m);
|
|
|
|
return (ENETDOWN);
|
|
|
|
}
|
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
/* BPF writes need to be handled specially */
|
|
|
|
if (dst->sa_family == AF_UNSPEC) {
|
|
|
|
if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
|
|
|
|
return (ENOBUFS);
|
|
|
|
dst->sa_family = (sa_family_t)*mtod(m, int32_t *);
|
|
|
|
m->m_data += 4;
|
|
|
|
m->m_len -= 4;
|
|
|
|
m->m_pkthdr.len -= 4;
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Berkeley packet filter */
|
|
|
|
ng_iface_bpftap(ifp, m, dst->sa_family);
|
|
|
|
|
|
|
|
/* Check address family to determine hook (if known) */
|
|
|
|
if (iffam == NULL) {
|
|
|
|
m_freem(m);
|
|
|
|
log(LOG_WARNING, "%s%d: can't handle af%d\n",
|
2000-03-13 19:18:10 +00:00
|
|
|
ifp->if_name, ifp->if_unit, (int)dst->sa_family);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (EAFNOSUPPORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy length before the mbuf gets invalidated */
|
|
|
|
len = m->m_pkthdr.len;
|
|
|
|
|
|
|
|
/* Send packet; if hook is not connected, mbuf will get freed. */
|
|
|
|
NG_SEND_DATA(error, *get_hook_from_iffam(priv, iffam), m, meta);
|
|
|
|
|
|
|
|
/* Update stats */
|
|
|
|
if (error == 0) {
|
|
|
|
ifp->if_obytes += len;
|
|
|
|
ifp->if_opackets++;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine should never be called
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
ng_iface_start(struct ifnet *ifp)
|
|
|
|
{
|
2002-10-21 02:51:56 +00:00
|
|
|
if_printf(ifp, "%s called?", __func__);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Flash a packet by the BPF (requires prepending 4 byte AF header)
|
|
|
|
* Note the phoney mbuf; this is OK because BPF treats it read-only.
|
|
|
|
*/
|
|
|
|
static void
|
2000-03-13 19:18:10 +00:00
|
|
|
ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2000-03-13 19:18:10 +00:00
|
|
|
int32_t family4 = (int32_t)family;
|
|
|
|
struct mbuf m0;
|
|
|
|
|
2001-12-10 08:09:49 +00:00
|
|
|
KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
|
2000-03-13 19:18:10 +00:00
|
|
|
if (ifp->if_bpf != NULL) {
|
|
|
|
bzero(&m0, sizeof(m0));
|
|
|
|
m0.m_next = m;
|
|
|
|
m0.m_len = sizeof(family4);
|
|
|
|
m0.m_data = (char *)&family4;
|
2002-11-14 23:44:37 +00:00
|
|
|
BPF_MTAP(ifp, &m0);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/*
|
|
|
|
* Display an ioctl to the virtual interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
switch (command & IOC_DIRMASK) {
|
|
|
|
case IOC_VOID:
|
|
|
|
str = "IO";
|
|
|
|
break;
|
|
|
|
case IOC_OUT:
|
|
|
|
str = "IOR";
|
|
|
|
break;
|
|
|
|
case IOC_IN:
|
|
|
|
str = "IOW";
|
|
|
|
break;
|
|
|
|
case IOC_INOUT:
|
|
|
|
str = "IORW";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str = "IO??";
|
|
|
|
}
|
|
|
|
log(LOG_DEBUG, "%s%d: %s('%c', %d, char[%d])\n",
|
|
|
|
ifp->if_name, ifp->if_unit,
|
|
|
|
str,
|
|
|
|
IOCGROUP(command),
|
|
|
|
command & 0xff,
|
|
|
|
IOCPARM_LEN(command));
|
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
NETGRAPH NODE STUFF
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor for a node
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_iface_constructor(node_p node)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
|
|
|
|
struct ifnet *ifp;
|
|
|
|
priv_p priv;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/* Allocate node and interface private structures */
|
2001-01-10 07:13:58 +00:00
|
|
|
MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (priv == NULL)
|
|
|
|
return (ENOMEM);
|
2001-01-10 07:13:58 +00:00
|
|
|
MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (ifp == NULL) {
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(priv, M_NETGRAPH_IFACE);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Link them together */
|
|
|
|
ifp->if_softc = priv;
|
|
|
|
priv->ifp = ifp;
|
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
/* Get an interface unit number */
|
|
|
|
if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(ifp, M_NETGRAPH_IFACE);
|
|
|
|
FREE(priv, M_NETGRAPH_IFACE);
|
2000-03-13 19:18:10 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Link together node and private info */
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, priv);
|
1999-10-21 09:06:11 +00:00
|
|
|
priv->node = node;
|
|
|
|
|
|
|
|
/* Initialize interface structure */
|
2000-03-13 19:18:10 +00:00
|
|
|
ifp->if_name = NG_IFACE_IFACE_NAME;
|
|
|
|
ifp->if_unit = priv->unit;
|
1999-10-21 09:06:11 +00:00
|
|
|
ifp->if_output = ng_iface_output;
|
|
|
|
ifp->if_start = ng_iface_start;
|
|
|
|
ifp->if_ioctl = ng_iface_ioctl;
|
|
|
|
ifp->if_watchdog = NULL;
|
|
|
|
ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
|
|
|
|
ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
|
2000-03-13 19:18:10 +00:00
|
|
|
ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
|
1999-10-21 09:06:11 +00:00
|
|
|
ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
|
|
|
|
ifp->if_addrlen = 0; /* XXX */
|
|
|
|
ifp->if_hdrlen = 0; /* XXX */
|
|
|
|
ifp->if_baudrate = 64000; /* XXX */
|
|
|
|
TAILQ_INIT(&ifp->if_addrhead);
|
|
|
|
|
|
|
|
/* Give this node the same name as the interface (if possible) */
|
|
|
|
bzero(ifname, sizeof(ifname));
|
2000-03-13 19:18:10 +00:00
|
|
|
snprintf(ifname, sizeof(ifname), "%s%d", ifp->if_name, ifp->if_unit);
|
|
|
|
if (ng_name_node(node, ifname) != 0)
|
|
|
|
log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Attach the interface */
|
|
|
|
if_attach(ifp);
|
|
|
|
bpfattach(ifp, DLT_NULL, sizeof(u_int));
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give our ok for a hook to be added
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_iface_newhook(node_p node, hook_p hook, const char *name)
|
|
|
|
{
|
|
|
|
const iffam_p iffam = get_iffam_from_name(name);
|
|
|
|
hook_p *hookptr;
|
|
|
|
|
|
|
|
if (iffam == NULL)
|
|
|
|
return (EPFNOSUPPORT);
|
2001-01-08 05:34:06 +00:00
|
|
|
hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (*hookptr != NULL)
|
|
|
|
return (EISCONN);
|
|
|
|
*hookptr = hook;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a control message
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
1999-10-21 09:06:11 +00:00
|
|
|
struct ifnet *const ifp = priv->ifp;
|
|
|
|
struct ng_mesg *resp = NULL;
|
|
|
|
int error = 0;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct ng_mesg *msg;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
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_IFACE_COOKIE:
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_IFACE_GET_IFNAME:
|
|
|
|
{
|
|
|
|
struct ng_iface_ifname *arg;
|
|
|
|
|
|
|
|
NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
|
|
|
|
if (resp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
2000-03-13 19:18:10 +00:00
|
|
|
arg = (struct ng_iface_ifname *)resp->data;
|
|
|
|
snprintf(arg->ngif_name, sizeof(arg->ngif_name),
|
1999-10-21 09:06:11 +00:00
|
|
|
"%s%d", ifp->if_name, ifp->if_unit);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
case NGM_IFACE_POINT2POINT:
|
|
|
|
case NGM_IFACE_BROADCAST:
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
/* Deny request if interface is UP */
|
|
|
|
if ((ifp->if_flags & IFF_UP) != 0)
|
|
|
|
return (EBUSY);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
/* Change flags */
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_IFACE_POINT2POINT:
|
|
|
|
ifp->if_flags |= IFF_POINTOPOINT;
|
|
|
|
ifp->if_flags &= ~IFF_BROADCAST;
|
|
|
|
break;
|
|
|
|
case NGM_IFACE_BROADCAST:
|
|
|
|
ifp->if_flags &= ~IFF_POINTOPOINT;
|
|
|
|
ifp->if_flags |= IFF_BROADCAST;
|
1999-10-21 09:06:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NGM_CISCO_COOKIE:
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_CISCO_GET_IPADDR: /* we understand this too */
|
|
|
|
{
|
|
|
|
struct ifaddr *ifa;
|
|
|
|
|
|
|
|
/* Return the first configured IP address */
|
|
|
|
TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
|
2000-03-13 19:18:10 +00:00
|
|
|
struct ng_cisco_ipaddr *ips;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_INET)
|
|
|
|
continue;
|
2000-03-13 19:18:10 +00:00
|
|
|
NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
|
1999-10-21 09:06:11 +00:00
|
|
|
if (resp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
2000-03-13 19:18:10 +00:00
|
|
|
ips = (struct ng_cisco_ipaddr *)resp->data;
|
|
|
|
ips->ipaddr = ((struct sockaddr_in *)
|
1999-10-21 09:06:11 +00:00
|
|
|
ifa->ifa_addr)->sin_addr;
|
2000-03-13 19:18:10 +00:00
|
|
|
ips->netmask = ((struct sockaddr_in *)
|
1999-10-21 09:06:11 +00:00
|
|
|
ifa->ifa_netmask)->sin_addr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No IP addresses on this interface? */
|
|
|
|
if (ifa == NULL)
|
|
|
|
error = EADDRNOTAVAIL;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recive data from a hook. Pass the packet to the correct input routine.
|
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_iface_rcvdata(hook_p hook, item_p item)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
1999-10-21 09:06:11 +00:00
|
|
|
const iffam_p iffam = get_iffam_from_hook(priv, hook);
|
|
|
|
struct ifnet *const ifp = priv->ifp;
|
2001-01-06 00:46:47 +00:00
|
|
|
struct mbuf *m;
|
2003-03-04 23:19:55 +00:00
|
|
|
int isr;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2001-01-06 00:46:47 +00:00
|
|
|
NGI_GET_M(item, m);
|
|
|
|
NG_FREE_ITEM(item);
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Sanity checks */
|
2001-12-10 08:09:49 +00:00
|
|
|
KASSERT(iffam != NULL, ("%s: iffam", __func__));
|
|
|
|
KASSERT(m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
|
1999-10-21 09:06:11 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
if ((ifp->if_flags & IFF_UP) == 0) {
|
2001-01-06 00:46:47 +00:00
|
|
|
NG_FREE_M(m);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (ENETDOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update interface stats */
|
|
|
|
ifp->if_ipackets++;
|
|
|
|
ifp->if_ibytes += m->m_pkthdr.len;
|
|
|
|
|
|
|
|
/* Note receiving interface */
|
|
|
|
m->m_pkthdr.rcvif = ifp;
|
|
|
|
|
|
|
|
/* Berkeley packet filter */
|
2000-03-13 19:18:10 +00:00
|
|
|
ng_iface_bpftap(ifp, m, iffam->family);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2000-03-13 19:18:10 +00:00
|
|
|
/* Send packet */
|
2003-03-04 23:19:55 +00:00
|
|
|
switch (iffam->family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
isr = NETISR_IP;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
isr = NETISR_IPV6;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef IPX
|
|
|
|
case AF_IPX:
|
|
|
|
isr = NETISR_IPX;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef NETATALK
|
|
|
|
case AF_APPLETALK:
|
|
|
|
isr = NETISR_ATALK2;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
m_freem(m);
|
|
|
|
return (EAFNOSUPPORT);
|
|
|
|
}
|
|
|
|
/* First chunk of an mbuf contains good junk */
|
|
|
|
if (harvest.point_to_point)
|
|
|
|
random_harvest(m, 16, 3, 0, RANDOM_NET);
|
|
|
|
netisr_dispatch(isr, m);
|
|
|
|
return (0);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-03-13 19:18:10 +00:00
|
|
|
* Shutdown and remove the node and its associated interface.
|
1999-10-21 09:06:11 +00:00
|
|
|
*/
|
|
|
|
static int
|
2001-01-06 00:46:47 +00:00
|
|
|
ng_iface_shutdown(node_p node)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
1999-10-21 09:06:11 +00:00
|
|
|
|
2000-03-21 01:42:56 +00:00
|
|
|
bpfdetach(priv->ifp);
|
2000-03-13 19:18:10 +00:00
|
|
|
if_detach(priv->ifp);
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(priv->ifp, M_NETGRAPH_IFACE);
|
2000-03-13 19:18:10 +00:00
|
|
|
priv->ifp = NULL;
|
|
|
|
ng_iface_free_unit(priv->unit);
|
2001-01-10 07:13:58 +00:00
|
|
|
FREE(priv, M_NETGRAPH_IFACE);
|
2001-01-08 05:34:06 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, NULL);
|
|
|
|
NG_NODE_UNREF(node);
|
1999-10-21 09:06:11 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-03-13 19:18:10 +00:00
|
|
|
* Hook disconnection. Note that we do *not* shutdown when all
|
|
|
|
* hooks have been disconnected.
|
1999-10-21 09:06:11 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_iface_disconnect(hook_p hook)
|
|
|
|
{
|
2001-01-08 05:34:06 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
1999-10-21 09:06:11 +00:00
|
|
|
const iffam_p iffam = get_iffam_from_hook(priv, hook);
|
|
|
|
|
|
|
|
if (iffam == NULL)
|
2001-12-10 08:09:49 +00:00
|
|
|
panic(__func__);
|
1999-10-21 09:06:11 +00:00
|
|
|
*get_hook_from_iffam(priv, iffam) = NULL;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|