2001-02-25 05:46:52 +00:00
|
|
|
/*-
|
|
|
|
*
|
|
|
|
* Copyright (c) 1999-2001, Vitaly V Belekhov
|
|
|
|
* 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 unmodified, 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.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/kernel.h>
|
2004-07-14 20:26:29 +00:00
|
|
|
#include <sys/lock.h>
|
2001-02-25 05:46:52 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
2004-07-14 20:26:29 +00:00
|
|
|
#include <sys/mutex.h>
|
2001-02-25 05:46:52 +00:00
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
2001-02-26 09:31:54 +00:00
|
|
|
#include <net/if_dl.h>
|
2001-02-25 05:46:52 +00:00
|
|
|
#include <net/if_types.h>
|
|
|
|
#include <net/netisr.h>
|
|
|
|
|
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/netgraph.h>
|
|
|
|
#include <netgraph/ng_parse.h>
|
|
|
|
#include <netgraph/ng_eiface.h>
|
|
|
|
|
|
|
|
#include <net/bpf.h>
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
|
2001-02-25 16:49:04 +00:00
|
|
|
static const struct ng_cmdlist ng_eiface_cmdlist[] = {
|
2005-02-03 11:52:42 +00:00
|
|
|
{
|
|
|
|
NGM_EIFACE_COOKIE,
|
|
|
|
NGM_EIFACE_GET_IFNAME,
|
|
|
|
"getifname",
|
|
|
|
NULL,
|
|
|
|
&ng_parse_string_type
|
|
|
|
},
|
2001-02-25 16:49:04 +00:00
|
|
|
{
|
|
|
|
NGM_EIFACE_COOKIE,
|
|
|
|
NGM_EIFACE_SET,
|
|
|
|
"set",
|
2003-12-17 13:03:32 +00:00
|
|
|
&ng_parse_enaddr_type,
|
2001-02-25 16:49:04 +00:00
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
2001-02-25 05:46:52 +00:00
|
|
|
/* Node private data */
|
|
|
|
struct ng_eiface_private {
|
2005-02-02 13:27:03 +00:00
|
|
|
struct arpcom arpcom; /* per-interface network data */
|
|
|
|
struct ifnet *ifp; /* This interface */
|
|
|
|
int unit; /* Interface unit number */
|
|
|
|
node_p node; /* Our netgraph node */
|
|
|
|
hook_p ether; /* Hook for ethernet stream */
|
2001-02-25 05:46:52 +00:00
|
|
|
};
|
|
|
|
typedef struct ng_eiface_private *priv_p;
|
|
|
|
|
|
|
|
/* Interface methods */
|
2005-02-02 13:27:03 +00:00
|
|
|
static void ng_eiface_init(void *xsc);
|
|
|
|
static void ng_eiface_start(struct ifnet *ifp);
|
|
|
|
static int ng_eiface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
|
2001-02-25 05:46:52 +00:00
|
|
|
#ifdef DEBUG
|
2005-02-02 13:27:03 +00:00
|
|
|
static void ng_eiface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
|
2001-02-25 05:46:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Netgraph methods */
|
2005-02-05 08:28:36 +00:00
|
|
|
static int ng_eiface_mod_event(module_t, int, void *);
|
2005-02-02 13:27:03 +00:00
|
|
|
static ng_constructor_t ng_eiface_constructor;
|
|
|
|
static ng_rcvmsg_t ng_eiface_rcvmsg;
|
|
|
|
static ng_shutdown_t ng_eiface_rmnode;
|
|
|
|
static ng_newhook_t ng_eiface_newhook;
|
|
|
|
static ng_rcvdata_t ng_eiface_rcvdata;
|
|
|
|
static ng_connect_t ng_eiface_connect;
|
|
|
|
static ng_disconnect_t ng_eiface_disconnect;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
/* Node type descriptor */
|
|
|
|
static struct ng_type typestruct = {
|
2004-05-29 00:51:19 +00:00
|
|
|
.version = NG_ABI_VERSION,
|
|
|
|
.name = NG_EIFACE_NODE_TYPE,
|
2005-02-05 08:28:36 +00:00
|
|
|
.mod_event = ng_eiface_mod_event,
|
2004-05-29 00:51:19 +00:00
|
|
|
.constructor = ng_eiface_constructor,
|
|
|
|
.rcvmsg = ng_eiface_rcvmsg,
|
|
|
|
.shutdown = ng_eiface_rmnode,
|
|
|
|
.newhook = ng_eiface_newhook,
|
|
|
|
.connect = ng_eiface_connect,
|
|
|
|
.rcvdata = ng_eiface_rcvdata,
|
|
|
|
.disconnect = ng_eiface_disconnect,
|
|
|
|
.cmdlist = ng_eiface_cmdlist
|
2001-02-25 05:46:52 +00:00
|
|
|
};
|
|
|
|
NETGRAPH_INIT(eiface, &typestruct);
|
|
|
|
|
|
|
|
/* 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_eiface_units = NULL;
|
|
|
|
static int ng_eiface_units_len = 0;
|
|
|
|
static int ng_units_in_use = 0;
|
|
|
|
|
|
|
|
#define UNITS_BITSPERWORD (sizeof(*ng_eiface_units) * NBBY)
|
|
|
|
|
2004-07-14 20:26:29 +00:00
|
|
|
static struct mtx ng_eiface_mtx;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
HELPER STUFF
|
|
|
|
************************************************************************/
|
|
|
|
/*
|
|
|
|
* Find the first free unit number for a new interface.
|
|
|
|
* Increase the size of the unit bitmap as necessary.
|
|
|
|
*/
|
2004-07-04 16:11:03 +00:00
|
|
|
static __inline int
|
2001-02-25 05:46:52 +00:00
|
|
|
ng_eiface_get_unit(int *unit)
|
|
|
|
{
|
|
|
|
int index, bit;
|
|
|
|
|
2004-07-14 20:26:29 +00:00
|
|
|
mtx_lock(&ng_eiface_mtx);
|
2001-02-25 05:46:52 +00:00
|
|
|
for (index = 0; index < ng_eiface_units_len
|
|
|
|
&& ng_eiface_units[index] == 0; index++);
|
|
|
|
if (index == ng_eiface_units_len) { /* extend array */
|
|
|
|
int i, *newarray, newlen;
|
|
|
|
|
|
|
|
newlen = (2 * ng_eiface_units_len) + 4;
|
|
|
|
MALLOC(newarray, int *, newlen * sizeof(*ng_eiface_units),
|
|
|
|
M_NETGRAPH, M_NOWAIT);
|
2004-07-14 20:26:29 +00:00
|
|
|
if (newarray == NULL) {
|
|
|
|
mtx_unlock(&ng_eiface_mtx);
|
2001-02-25 05:46:52 +00:00
|
|
|
return (ENOMEM);
|
2004-07-14 20:26:29 +00:00
|
|
|
}
|
2001-02-25 05:46:52 +00:00
|
|
|
bcopy(ng_eiface_units, newarray,
|
|
|
|
ng_eiface_units_len * sizeof(*ng_eiface_units));
|
|
|
|
for (i = ng_eiface_units_len; i < newlen; i++)
|
|
|
|
newarray[i] = ~0;
|
|
|
|
if (ng_eiface_units != NULL)
|
|
|
|
FREE(ng_eiface_units, M_NETGRAPH);
|
|
|
|
ng_eiface_units = newarray;
|
|
|
|
ng_eiface_units_len = newlen;
|
|
|
|
}
|
|
|
|
bit = ffs(ng_eiface_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_eiface_units[index], bit));
|
2001-02-25 05:46:52 +00:00
|
|
|
ng_eiface_units[index] &= ~(1 << bit);
|
|
|
|
*unit = (index * UNITS_BITSPERWORD) + bit;
|
|
|
|
ng_units_in_use++;
|
2004-07-14 20:26:29 +00:00
|
|
|
mtx_unlock(&ng_eiface_mtx);
|
2001-02-25 05:46:52 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a no longer needed unit number.
|
|
|
|
*/
|
2004-07-04 16:11:03 +00:00
|
|
|
static __inline void
|
2001-02-25 05:46:52 +00:00
|
|
|
ng_eiface_free_unit(int unit)
|
|
|
|
{
|
|
|
|
int index, bit;
|
|
|
|
|
|
|
|
index = unit / UNITS_BITSPERWORD;
|
|
|
|
bit = unit % UNITS_BITSPERWORD;
|
2004-07-14 20:26:29 +00:00
|
|
|
mtx_lock(&ng_eiface_mtx);
|
2001-02-25 05:46:52 +00:00
|
|
|
KASSERT(index < ng_eiface_units_len,
|
2001-12-10 08:09:49 +00:00
|
|
|
("%s: unit=%d len=%d", __func__, unit, ng_eiface_units_len));
|
2001-02-25 05:46:52 +00:00
|
|
|
KASSERT((ng_eiface_units[index] & (1 << bit)) == 0,
|
2001-12-10 08:09:49 +00:00
|
|
|
("%s: unit=%d is free", __func__, unit));
|
2001-02-25 05:46:52 +00:00
|
|
|
ng_eiface_units[index] |= (1 << bit);
|
|
|
|
/*
|
|
|
|
* XXX We could think about reducing the size of ng_eiface_units[]
|
|
|
|
* XXX here if the last portion is all ones
|
|
|
|
* XXX At least free it if no more units.
|
|
|
|
* Needed if we are to eventually be able to unload.
|
|
|
|
*/
|
|
|
|
ng_units_in_use--;
|
|
|
|
if (ng_units_in_use == 0) { /* XXX make SMP safe */
|
|
|
|
FREE(ng_eiface_units, M_NETGRAPH);
|
|
|
|
ng_eiface_units_len = 0;
|
|
|
|
ng_eiface_units = NULL;
|
|
|
|
}
|
2004-07-14 20:26:29 +00:00
|
|
|
mtx_unlock(&ng_eiface_mtx);
|
2001-02-25 05:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
INTERFACE STUFF
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process an ioctl for the virtual interface
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_eiface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
|
|
|
|
{
|
2005-02-02 13:27:03 +00:00
|
|
|
struct ifreq *const ifr = (struct ifreq *)data;
|
|
|
|
int s, error = 0;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
ng_eiface_print_ioctl(ifp, command, data);
|
|
|
|
#endif
|
|
|
|
s = splimp();
|
2005-02-02 13:27:03 +00:00
|
|
|
switch (command) {
|
|
|
|
|
2001-02-25 05:46:52 +00:00
|
|
|
/* These two are mostly handled at a higher layer */
|
|
|
|
case SIOCSIFADDR:
|
|
|
|
error = ether_ioctl(ifp, command, data);
|
|
|
|
break;
|
|
|
|
case SIOCGIFADDR:
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Set flags */
|
|
|
|
case SIOCSIFFLAGS:
|
|
|
|
/*
|
2005-02-02 13:27:03 +00:00
|
|
|
* If the interface is marked up and stopped, then start it.
|
|
|
|
* If it is marked down and running, then stop it.
|
2001-02-25 05:46:52 +00:00
|
|
|
*/
|
|
|
|
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)
|
2005-02-02 13:27:03 +00:00
|
|
|
ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
|
2001-02-25 05:46:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Set the interface MTU */
|
|
|
|
case SIOCSIFMTU:
|
2005-02-02 13:27:03 +00:00
|
|
|
if (ifr->ifr_mtu > NG_EIFACE_MTU_MAX ||
|
|
|
|
ifr->ifr_mtu < NG_EIFACE_MTU_MIN)
|
2001-02-25 05:46:52 +00:00
|
|
|
error = EINVAL;
|
|
|
|
else
|
|
|
|
ifp->if_mtu = ifr->ifr_mtu;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Stuff that's not supported */
|
|
|
|
case SIOCADDMULTI:
|
|
|
|
case SIOCDELMULTI:
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
case SIOCSIFPHYS:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2005-02-02 13:27:03 +00:00
|
|
|
splx(s);
|
2001-02-25 05:46:52 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ng_eiface_init(void *xsc)
|
|
|
|
{
|
2005-02-02 13:27:03 +00:00
|
|
|
priv_p sc = xsc;
|
|
|
|
struct ifnet *ifp = sc->ifp;
|
|
|
|
int s;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
s = splimp();
|
|
|
|
|
|
|
|
ifp->if_flags |= IFF_RUNNING;
|
|
|
|
ifp->if_flags &= ~IFF_OACTIVE;
|
|
|
|
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-02-02 14:02:40 +00:00
|
|
|
* We simply relay the packet to the "ether" hook, if it is connected.
|
|
|
|
* We have been through the netgraph locking and are guaranteed to
|
2001-02-25 16:49:04 +00:00
|
|
|
* be the only code running in this node at this time.
|
2001-02-25 05:46:52 +00:00
|
|
|
*/
|
|
|
|
static void
|
2001-02-25 16:49:04 +00:00
|
|
|
ng_eiface_start2(node_p node, hook_p hook, void *arg1, int arg2)
|
2001-02-25 05:46:52 +00:00
|
|
|
{
|
2001-02-25 16:49:04 +00:00
|
|
|
struct ifnet *ifp = arg1;
|
2005-02-02 13:27:03 +00:00
|
|
|
const priv_p priv = (priv_p)ifp->if_softc;
|
|
|
|
int len, error = 0;
|
|
|
|
struct mbuf *m;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
/* Check interface flags */
|
|
|
|
if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Don't do anything if output is active */
|
|
|
|
if (ifp->if_flags & IFF_OACTIVE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ifp->if_flags |= IFF_OACTIVE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Grab a packet to transmit.
|
|
|
|
*/
|
|
|
|
IF_DEQUEUE(&ifp->if_snd, m);
|
|
|
|
|
|
|
|
/* If there's nothing to send, return. */
|
|
|
|
if (m == NULL) {
|
|
|
|
ifp->if_flags &= ~IFF_OACTIVE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-02-02 13:27:03 +00:00
|
|
|
/*
|
|
|
|
* Berkeley packet filter.
|
2001-02-25 05:46:52 +00:00
|
|
|
* Pass packet to bpf if there is a listener.
|
2001-02-25 16:49:04 +00:00
|
|
|
* XXX is this safe? locking?
|
2001-02-25 05:46:52 +00:00
|
|
|
*/
|
2002-11-14 23:44:37 +00:00
|
|
|
BPF_MTAP(ifp, m);
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
/* 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_ONLY(error, priv->ether, m);
|
|
|
|
|
|
|
|
/* Update stats */
|
|
|
|
if (error == 0) {
|
|
|
|
ifp->if_obytes += len;
|
|
|
|
ifp->if_opackets++;
|
|
|
|
}
|
2005-02-02 13:27:03 +00:00
|
|
|
|
2001-02-25 05:46:52 +00:00
|
|
|
ifp->if_flags &= ~IFF_OACTIVE;
|
2005-02-02 13:27:03 +00:00
|
|
|
|
2001-02-25 05:46:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-02-25 16:49:04 +00:00
|
|
|
/*
|
|
|
|
* This routine is called to deliver a packet out the interface.
|
|
|
|
* We simply queue the netgraph version to be called when netgraph locking
|
|
|
|
* allows it to happen.
|
|
|
|
* Until we know what the rest of the networking code is doing for
|
|
|
|
* locking, we don't know how we will interact with it.
|
|
|
|
* Take comfort from the fact that the ifnet struct is part of our
|
|
|
|
* private info and can't go away while we are queued.
|
|
|
|
* [Though we don't know it is still there now....]
|
|
|
|
* it is possible we don't gain anything from this because
|
|
|
|
* we would like to get the mbuf and queue it as data
|
|
|
|
* somehow, but we can't and if we did would we solve anything?
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_eiface_start(struct ifnet *ifp)
|
|
|
|
{
|
2005-02-02 13:27:03 +00:00
|
|
|
|
|
|
|
const priv_p priv = (priv_p)ifp->if_softc;
|
2001-02-25 16:49:04 +00:00
|
|
|
|
|
|
|
ng_send_fn(priv->node, NULL, &ng_eiface_start2, ifp, 0);
|
|
|
|
}
|
|
|
|
|
2001-02-25 05:46:52 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
/*
|
|
|
|
* Display an ioctl to the virtual interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2005-02-02 13:27:03 +00:00
|
|
|
ng_eiface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
|
|
|
|
{
|
|
|
|
char *str;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
2005-02-02 13:27:03 +00:00
|
|
|
switch (command & IOC_DIRMASK) {
|
2001-02-25 05:46:52 +00:00
|
|
|
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??";
|
|
|
|
}
|
2003-10-31 18:32:15 +00:00
|
|
|
log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
|
2005-02-02 13:27:03 +00:00
|
|
|
ifp->if_xname,
|
|
|
|
str,
|
|
|
|
IOCGROUP(command),
|
|
|
|
command & 0xff,
|
|
|
|
IOCPARM_LEN(command));
|
2001-02-25 05:46:52 +00:00
|
|
|
}
|
2005-02-02 13:27:03 +00:00
|
|
|
#endif /* DEBUG */
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
NETGRAPH NODE STUFF
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor for a node
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_eiface_constructor(node_p node)
|
|
|
|
{
|
2005-02-02 13:27:03 +00:00
|
|
|
struct ifnet *ifp;
|
|
|
|
priv_p priv;
|
|
|
|
int error = 0;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
/* Allocate node and interface private structures */
|
2005-02-08 10:31:55 +00:00
|
|
|
MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
|
2005-02-02 13:27:03 +00:00
|
|
|
if (priv == NULL)
|
2001-02-25 05:46:52 +00:00
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
ifp = &(priv->arpcom.ac_if);
|
|
|
|
|
|
|
|
/* Link them together */
|
|
|
|
ifp->if_softc = priv;
|
|
|
|
priv->ifp = ifp;
|
|
|
|
|
|
|
|
/* Get an interface unit number */
|
|
|
|
if ((error = ng_eiface_get_unit(&priv->unit)) != 0) {
|
|
|
|
FREE(priv, M_NETGRAPH);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Link together node and private info */
|
|
|
|
NG_NODE_SET_PRIVATE(node, priv);
|
|
|
|
priv->node = node;
|
|
|
|
|
|
|
|
/* Initialize interface structure */
|
2003-10-31 18:32:15 +00:00
|
|
|
if_initname(ifp, NG_EIFACE_EIFACE_NAME, priv->unit);
|
2001-02-25 05:46:52 +00:00
|
|
|
ifp->if_init = ng_eiface_init;
|
|
|
|
ifp->if_output = ether_output;
|
|
|
|
ifp->if_start = ng_eiface_start;
|
|
|
|
ifp->if_ioctl = ng_eiface_ioctl;
|
|
|
|
ifp->if_watchdog = NULL;
|
|
|
|
ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
|
|
|
|
ifp->if_flags = (IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST);
|
|
|
|
|
2005-02-02 13:27:03 +00:00
|
|
|
#if 0
|
|
|
|
/* Give this node name */
|
|
|
|
bzero(ifname, sizeof(ifname));
|
|
|
|
sprintf(ifname, "if%s", ifp->if_xname);
|
|
|
|
(void)ng_name_node(node, ifname);
|
|
|
|
#endif
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
/* Attach the interface */
|
2002-11-14 23:44:37 +00:00
|
|
|
ether_ifattach(ifp, priv->arpcom.ac_enaddr);
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give our ok for a hook to be added
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_eiface_newhook(node_p node, hook_p hook, const char *name)
|
|
|
|
{
|
2005-02-02 13:27:03 +00:00
|
|
|
priv_p priv = NG_NODE_PRIVATE(node);
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
if (strcmp(name, NG_EIFACE_HOOK_ETHER))
|
|
|
|
return (EPFNOSUPPORT);
|
|
|
|
if (priv->ether != NULL)
|
|
|
|
return (EISCONN);
|
|
|
|
priv->ether = hook;
|
|
|
|
NG_HOOK_SET_PRIVATE(hook, &priv->ether);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a control message
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_eiface_rcvmsg(node_p node, item_p item, hook_p lasthook)
|
|
|
|
{
|
2005-02-02 13:27:03 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
|
|
|
struct ifnet *const ifp = priv->ifp;
|
2001-02-25 05:46:52 +00:00
|
|
|
struct ng_mesg *resp = NULL;
|
2005-02-02 13:27:03 +00:00
|
|
|
int error = 0;
|
2001-02-25 05:46:52 +00:00
|
|
|
struct ng_mesg *msg;
|
|
|
|
|
|
|
|
NGI_GET_MSG(item, msg);
|
2005-02-02 13:27:03 +00:00
|
|
|
switch (msg->header.typecookie) {
|
2001-02-25 05:46:52 +00:00
|
|
|
case NGM_EIFACE_COOKIE:
|
|
|
|
switch (msg->header.cmd) {
|
2005-02-02 13:27:03 +00:00
|
|
|
|
2001-02-25 05:46:52 +00:00
|
|
|
case NGM_EIFACE_SET:
|
2005-02-02 13:27:03 +00:00
|
|
|
{
|
2003-12-17 13:03:32 +00:00
|
|
|
struct ether_addr *eaddr;
|
2001-02-26 09:31:54 +00:00
|
|
|
struct ifaddr *ifa;
|
|
|
|
struct sockaddr_dl *sdl;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
2005-02-02 13:27:03 +00:00
|
|
|
if (msg->header.arglen != sizeof(struct ether_addr)) {
|
2001-02-25 05:46:52 +00:00
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2003-12-17 13:03:32 +00:00
|
|
|
eaddr = (struct ether_addr *)(msg->data);
|
|
|
|
bcopy(eaddr, priv->arpcom.ac_enaddr, ETHER_ADDR_LEN);
|
2001-02-25 05:46:52 +00:00
|
|
|
|
2001-02-26 09:31:54 +00:00
|
|
|
/* And put it in the ifaddr list */
|
|
|
|
TAILQ_FOREACH(ifa, &(ifp->if_addrhead), ifa_link) {
|
|
|
|
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
|
|
|
|
if (sdl->sdl_type == IFT_ETHER) {
|
|
|
|
bcopy((IFP2AC(ifp))->ac_enaddr,
|
|
|
|
LLADDR(sdl), ifp->if_addrlen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-02-25 05:46:52 +00:00
|
|
|
break;
|
2005-02-02 13:27:03 +00:00
|
|
|
}
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
case NGM_EIFACE_GET_IFNAME:
|
2005-02-03 12:50:10 +00:00
|
|
|
NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
|
2001-02-25 05:46:52 +00:00
|
|
|
if (resp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
2005-02-03 12:50:10 +00:00
|
|
|
strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
|
2001-02-25 05:46:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NGM_EIFACE_GET_IFADDRS:
|
2005-02-02 13:27:03 +00:00
|
|
|
{
|
|
|
|
struct ifaddr *ifa;
|
|
|
|
caddr_t ptr;
|
|
|
|
int buflen;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
#define SA_SIZE(s) ((s)->sa_len<sizeof(*(s))? sizeof(*(s)):(s)->sa_len)
|
|
|
|
|
|
|
|
/* Determine size of response and allocate it */
|
|
|
|
buflen = 0;
|
|
|
|
TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
|
2005-02-02 13:27:03 +00:00
|
|
|
buflen += SA_SIZE(ifa->ifa_addr);
|
2001-02-25 05:46:52 +00:00
|
|
|
NG_MKRESPONSE(resp, msg, buflen, M_NOWAIT);
|
|
|
|
if (resp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
2005-02-02 13:27:03 +00:00
|
|
|
|
2001-02-25 05:46:52 +00:00
|
|
|
/* Add addresses */
|
|
|
|
ptr = resp->data;
|
|
|
|
TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
|
2005-02-02 13:27:03 +00:00
|
|
|
const int len = SA_SIZE(ifa->ifa_addr);
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
if (buflen < len) {
|
2003-10-31 18:32:15 +00:00
|
|
|
log(LOG_ERR, "%s: len changed?\n",
|
2005-02-02 13:27:03 +00:00
|
|
|
ifp->if_xname);
|
2001-02-25 05:46:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
bcopy(ifa->ifa_addr, ptr, len);
|
|
|
|
ptr += len;
|
|
|
|
buflen -= len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#undef SA_SIZE
|
2005-02-02 13:27:03 +00:00
|
|
|
}
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
} /* end of inner switch() */
|
|
|
|
break;
|
2005-01-14 11:55:07 +00:00
|
|
|
case NGM_FLOW_COOKIE:
|
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_LINK_IS_UP:
|
|
|
|
ifp->if_flags |= IFF_RUNNING;
|
|
|
|
break;
|
|
|
|
case NGM_LINK_IS_DOWN:
|
|
|
|
ifp->if_flags &= ~IFF_RUNNING;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2001-02-25 05:46:52 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
NG_RESPOND_MSG(error, node, item, resp);
|
|
|
|
NG_FREE_MSG(msg);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-02-02 13:27:03 +00:00
|
|
|
* Receive data from a hook. Pass the packet to the ether_input routine.
|
2001-02-25 05:46:52 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_eiface_rcvdata(hook_p hook, item_p item)
|
|
|
|
{
|
2005-02-02 13:27:03 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
|
|
|
struct ifnet *const ifp = priv->ifp;
|
2001-02-25 05:46:52 +00:00
|
|
|
struct mbuf *m;
|
|
|
|
|
|
|
|
NGI_GET_M(item, m);
|
2005-02-02 13:27:03 +00:00
|
|
|
NG_FREE_ITEM(item);
|
2001-02-25 05:46:52 +00:00
|
|
|
|
2005-02-02 13:27:03 +00:00
|
|
|
if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
|
|
|
|
(IFF_UP | IFF_RUNNING)) {
|
2003-11-17 19:13:01 +00:00
|
|
|
NG_FREE_M(m);
|
2001-02-25 05:46:52 +00:00
|
|
|
return (ENETDOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note receiving interface */
|
|
|
|
m->m_pkthdr.rcvif = ifp;
|
|
|
|
|
|
|
|
/* Update interface stats */
|
|
|
|
ifp->if_ipackets++;
|
|
|
|
|
2002-11-14 23:44:37 +00:00
|
|
|
(*ifp->if_input)(ifp, m);
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
/* Done */
|
2002-11-14 23:44:37 +00:00
|
|
|
return (0);
|
2001-02-25 05:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-02-02 13:27:03 +00:00
|
|
|
* Shutdown processing.
|
2001-02-25 05:46:52 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_eiface_rmnode(node_p node)
|
|
|
|
{
|
2005-02-02 13:27:03 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(node);
|
|
|
|
struct ifnet *const ifp = priv->ifp;
|
2001-02-25 05:46:52 +00:00
|
|
|
|
2002-11-14 23:44:37 +00:00
|
|
|
ether_ifdetach(ifp);
|
2001-02-25 05:46:52 +00:00
|
|
|
ng_eiface_free_unit(priv->unit);
|
|
|
|
FREE(priv, M_NETGRAPH);
|
|
|
|
NG_NODE_SET_PRIVATE(node, NULL);
|
|
|
|
NG_NODE_UNREF(node);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called once we've already connected a new hook to the other node.
|
|
|
|
* It gives us a chance to balk at the last minute.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_eiface_connect(hook_p hook)
|
|
|
|
{
|
|
|
|
/* be really amiable and just say "YUP that's OK by me! " */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hook disconnection
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_eiface_disconnect(hook_p hook)
|
|
|
|
{
|
2005-02-02 13:27:03 +00:00
|
|
|
const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
2001-02-25 05:46:52 +00:00
|
|
|
|
|
|
|
priv->ether = NULL;
|
|
|
|
return (0);
|
|
|
|
}
|
2005-02-05 08:28:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle loading and unloading for this node type.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_eiface_mod_event(module_t mod, int event, void *data)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
mtx_init(&ng_eiface_mtx, "ng_eiface", NULL, MTX_DEF);
|
|
|
|
break;
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
mtx_destroy(&ng_eiface_mtx);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|