#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/sockio.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <sys/libkern.h>
#include <sys/queue.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/if_media.h>
#include <net/bpf.h>
#include <net/ethernet.h>
#include "opt_inet.h"
#include "opt_inet6.h"
#include <netinet/in.h>
#ifdef INET
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#endif
#ifdef INET6
#include <netinet/ip6.h>
#endif
#include "ng_message.h"
#include "netgraph.h"
#include "ng_parse.h"
#include "ng_fec.h"
#define IFP2NG(ifp) ((ifp)->if_afdata[AF_NETGRAPH])
#define FEC_BUNDLESIZ 4
struct ng_fec_portlist {
struct ifnet *fec_if;
void (*fec_if_input) (struct ifnet *,
struct mbuf *,
const struct pktinfo *,
int);
int fec_idx;
int fec_ifstat;
struct ether_addr fec_mac;
SLIST_HEAD(__mclhd, ng_fec_mc) fec_mc_head;
TAILQ_ENTRY(ng_fec_portlist) fec_list;
};
struct ng_fec_mc {
struct ifmultiaddr *mc_ifma;
SLIST_ENTRY(ng_fec_mc) mc_entries;
};
struct ng_fec_bundle {
TAILQ_HEAD(,ng_fec_portlist) ng_fec_ports;
int fec_ifcnt;
int fec_btype;
int (*fec_if_output) (struct ifnet *,
struct mbuf *,
struct sockaddr *,
struct rtentry *);
};
#define FEC_BTYPE_MAC 0x01
#define FEC_BTYPE_INET 0x02
#define FEC_BTYPE_INET6 0x03
struct ng_fec_private {
struct ifnet *ifp;
struct ifmedia ifmedia;
int if_flags;
int if_error;
int unit;
node_p node;
struct ng_fec_bundle fec_bundle;
struct callout_handle fec_ch;
};
typedef struct ng_fec_private *priv_p;
static void ng_fec_input(struct ifnet *, struct mbuf *,
const struct pktinfo *, int);
static void ng_fec_start(struct ifnet *ifp, struct ifaltq_subque *);
static int ng_fec_choose_port(struct ng_fec_bundle *b,
struct mbuf *m, struct ifnet **ifp);
static int ng_fec_setport(struct ifnet *ifp, u_long cmd, caddr_t data);
static void ng_fec_init(void *arg);
static void ng_fec_stop(struct ifnet *ifp);
static int ng_fec_ifmedia_upd(struct ifnet *ifp);
static void ng_fec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
static int ng_fec_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
static int ng_fec_output(struct ifnet *ifp, struct mbuf *m0,
struct sockaddr *dst, struct rtentry *rt0);
static void ng_fec_tick(void *arg);
static int ng_fec_addport(struct ng_fec_private *priv, char *iface);
static int ng_fec_delport(struct ng_fec_private *priv, char *iface);
static int ng_fec_ether_cmdmulti(struct ifnet *trifp, struct ng_fec_portlist *p, int set);
#ifdef DEBUG
static void ng_fec_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
#endif
static int ng_fec_mod_event(module_t, int, void *);
static ng_constructor_t ng_fec_constructor;
static ng_rcvmsg_t ng_fec_rcvmsg;
static ng_shutdown_t ng_fec_shutdown;
static const struct ng_cmdlist ng_fec_cmds[] = {
{
NGM_FEC_COOKIE,
NGM_FEC_ADD_IFACE,
"add_iface",
&ng_parse_string_type,
NULL,
},
{
NGM_FEC_COOKIE,
NGM_FEC_DEL_IFACE,
"del_iface",
&ng_parse_string_type,
NULL,
},
{
NGM_FEC_COOKIE,
NGM_FEC_SET_MODE_MAC,
"set_mode_mac",
NULL,
NULL,
},
{
NGM_FEC_COOKIE,
NGM_FEC_SET_MODE_INET,
"set_mode_inet",
NULL,
NULL,
},
{ 0 }
};
static struct ng_type typestruct = {
.version = NG_ABI_VERSION,
.name = NG_FEC_NODE_TYPE,
.mod_event = ng_fec_mod_event,
.constructor = ng_fec_constructor,
.rcvmsg = ng_fec_rcvmsg,
.shutdown = ng_fec_shutdown,
.cmdlist = ng_fec_cmds,
};
NETGRAPH_INIT(fec, &typestruct);
static int *ng_fec_units = NULL;
static int ng_fec_units_len = 0;
static int ng_units_in_use = 0;
#define UNITS_BITSPERWORD (sizeof(*ng_fec_units) * NBBY)
static struct mtx ng_fec_mtx;
static __inline int
ng_fec_get_unit(int *unit)
{
int index, bit;
mtx_lock(&ng_fec_mtx);
for (index = 0; index < ng_fec_units_len
&& ng_fec_units[index] == 0; index++);
if (index == ng_fec_units_len) {
int i, *newarray, newlen;
newlen = (2 * ng_fec_units_len) + 4;
newarray = kmalloc(newlen * sizeof(*ng_fec_units),
M_NETGRAPH, M_WAITOK | M_NULLOK);
if (newarray == NULL) {
mtx_unlock(&ng_fec_mtx);
return (ENOMEM);
}
bcopy(ng_fec_units, newarray,
ng_fec_units_len * sizeof(*ng_fec_units));
for (i = ng_fec_units_len; i < newlen; i++)
newarray[i] = ~0;
if (ng_fec_units != NULL)
kfree(ng_fec_units, M_NETGRAPH);
ng_fec_units = newarray;
ng_fec_units_len = newlen;
}
bit = ffs(ng_fec_units[index]) - 1;
KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
("%s: word=%d bit=%d", __func__, ng_fec_units[index], bit));
ng_fec_units[index] &= ~(1 << bit);
*unit = (index * UNITS_BITSPERWORD) + bit;
ng_units_in_use++;
mtx_unlock(&ng_fec_mtx);
return (0);
}
static __inline void
ng_fec_free_unit(int unit)
{
int index, bit;
index = unit / UNITS_BITSPERWORD;
bit = unit % UNITS_BITSPERWORD;
mtx_lock(&ng_fec_mtx);
KASSERT(index < ng_fec_units_len,
("%s: unit=%d len=%d", __func__, unit, ng_fec_units_len));
KASSERT((ng_fec_units[index] & (1 << bit)) == 0,
("%s: unit=%d is free", __func__, unit));
ng_fec_units[index] |= (1 << bit);
ng_units_in_use--;
if (ng_units_in_use == 0) {
kfree(ng_fec_units, M_NETGRAPH);
ng_fec_units_len = 0;
ng_fec_units = NULL;
}
mtx_unlock(&ng_fec_mtx);
}
static int
ng_fec_addport(struct ng_fec_private *priv, char *iface)
{
struct ng_fec_bundle *b;
struct ifnet *ifp, *bifp;
struct ng_fec_portlist *p, *new;
if (priv == NULL || iface == NULL)
return(EINVAL);
b = &priv->fec_bundle;
ifp = priv->ifp;
if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
kprintf("fec%d: can't add new iface; bundle is running\n",
priv->unit);
return (EINVAL);
}
bifp = ifunit(iface);
if (bifp == NULL) {
kprintf("fec%d: tried to add iface %s, which "
"doesn't seem to exist\n", priv->unit, iface);
return(ENOENT);
}
if (b->fec_ifcnt == FEC_BUNDLESIZ) {
kprintf("fec%d: can't add new iface; bundle is full\n",
priv->unit);
return(ENOSPC);
}
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
if (p->fec_if == bifp) {
kprintf("fec%d: iface %s is already in this "
"bundle\n", priv->unit, iface);
return(EINVAL);
}
}
if (b->fec_if_output != NULL) {
if (b->fec_if_output != bifp->if_output) {
kprintf("fec%d: iface %s is not the same type "
"as the other interface(s) already in "
"the bundle\n", priv->unit, iface);
return(EINVAL);
}
}
new = kmalloc(sizeof(struct ng_fec_portlist), M_NETGRAPH,
M_WAITOK | M_NULLOK);
if (new == NULL)
return(ENOMEM);
IF_AFDATA_LOCK(bifp);
IFP2NG(bifp) = priv->node;
IF_AFDATA_UNLOCK(bifp);
if (b->fec_ifcnt == 0)
if_setlladdr(ifp, IF_LLADDR(bifp), ETHER_ADDR_LEN);
b->fec_btype = FEC_BTYPE_MAC;
new->fec_idx = b->fec_ifcnt;
b->fec_ifcnt++;
SLIST_INIT(&new->fec_mc_head);
bcopy(IF_LLADDR(bifp),
(char *)&new->fec_mac, ETHER_ADDR_LEN);
if_setlladdr(bifp, IF_LLADDR(ifp), ETHER_ADDR_LEN);
new->fec_if_input = bifp->if_input;
bifp->if_input = ng_fec_input;
if (b->fec_if_output == NULL)
b->fec_if_output = bifp->if_output;
new->fec_if = bifp;
new->fec_ifstat = -1;
TAILQ_INSERT_TAIL(&b->ng_fec_ports, new, fec_list);
ng_fec_ether_cmdmulti(ifp, new, 1);
return(0);
}
static int
ng_fec_delport(struct ng_fec_private *priv, char *iface)
{
struct ng_fec_bundle *b;
struct ifnet *ifp, *bifp;
struct ng_fec_portlist *p;
if (priv == NULL || iface == NULL)
return(EINVAL);
b = &priv->fec_bundle;
ifp = priv->ifp;
if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
kprintf("fec%d: can't remove iface; bundle is running\n",
priv->unit);
return (EINVAL);
}
bifp = ifunit(iface);
if (bifp == NULL) {
kprintf("fec%d: tried to remove iface %s, which "
"doesn't seem to exist\n", priv->unit, iface);
return(ENOENT);
}
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
if (p->fec_if == bifp)
break;
}
if (p == NULL) {
kprintf("fec%d: tried to remove iface %s which "
"is not in our bundle\n", priv->unit, iface);
return(EINVAL);
}
bifp->if_flags &= ~IFF_UP;
(*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
if_setlladdr(bifp, (u_char *)&p->fec_mac, ETHER_ADDR_LEN);
bifp->if_input = p->fec_if_input;
IF_AFDATA_LOCK(bifp);
IFP2NG(bifp) = NULL;
IF_AFDATA_UNLOCK(bifp);
TAILQ_REMOVE(&b->ng_fec_ports, p, fec_list);
kfree(p, M_NETGRAPH);
b->fec_ifcnt--;
if (b->fec_ifcnt == 0)
b->fec_if_output = NULL;
return(0);
}
static int
ng_fec_ether_cmdmulti(struct ifnet *trifp, struct ng_fec_portlist *p, int set)
{
struct ifnet *ifp = p->fec_if;
struct ng_fec_mc *mc;
struct ifmultiaddr *ifma, *rifma = NULL;
struct sockaddr_dl sdl;
int error;
bzero((char *)&sdl, sizeof(sdl));
sdl.sdl_len = sizeof(sdl);
sdl.sdl_family = AF_LINK;
sdl.sdl_type = IFT_ETHER;
sdl.sdl_alen = ETHER_ADDR_LEN;
sdl.sdl_index = ifp->if_index;
if (set) {
TAILQ_FOREACH(ifma, &trifp->if_multiaddrs, ifma_link) {
if (ifma->ifma_addr->sa_family != AF_LINK)
continue;
bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
LLADDR(&sdl), ETHER_ADDR_LEN);
error = if_addmulti(ifp, (struct sockaddr *)&sdl, &rifma);
if (error)
return (error);
mc = kmalloc(sizeof(struct ng_fec_mc), M_DEVBUF, M_WAITOK | M_NULLOK);
if (mc == NULL)
return (ENOMEM);
mc->mc_ifma = rifma;
SLIST_INSERT_HEAD(&p->fec_mc_head, mc, mc_entries);
}
} else {
while ((mc = SLIST_FIRST(&p->fec_mc_head)) != NULL) {
SLIST_REMOVE(&p->fec_mc_head, mc, ng_fec_mc, mc_entries);
if_delmulti_ifma(mc->mc_ifma);
kfree(mc, M_DEVBUF);
}
}
return (0);
}
static int
ng_fec_ether_setmulti(struct ifnet *ifp)
{
struct ng_fec_private *priv;
struct ng_fec_bundle *b;
struct ng_fec_portlist *p;
priv = ifp->if_softc;
b = &priv->fec_bundle;
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
ng_fec_ether_cmdmulti(ifp, p, 0);
ng_fec_ether_cmdmulti(ifp, p, 1);
}
return (0);
}
static int
ng_fec_setport(struct ifnet *ifp, u_long command, caddr_t data)
{
struct ng_fec_private *priv;
struct ng_fec_bundle *b;
struct ifnet *oifp;
struct ng_fec_portlist *p;
priv = ifp->if_softc;
b = &priv->fec_bundle;
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
oifp = p->fec_if;
if (oifp != NULL)
(*oifp->if_ioctl)(oifp, command, data);
}
return(0);
}
static void
ng_fec_init(void *arg)
{
struct ng_fec_private *priv;
struct ng_fec_bundle *b;
struct ifnet *ifp, *bifp;
struct ng_fec_portlist *p;
priv = arg;
ifp = priv->ifp;
b = &priv->fec_bundle;
if (b->fec_ifcnt != 2 && b->fec_ifcnt != FEC_BUNDLESIZ) {
kprintf("fec%d: invalid bundle "
"size: %d\n", priv->unit,
b->fec_ifcnt);
return;
}
ng_fec_stop(ifp);
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
bifp = p->fec_if;
bifp->if_flags |= IFF_UP;
(*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
p->fec_ifstat = -1;
}
ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
ifp->if_drv_flags |= IFF_DRV_RUNNING;
priv->fec_ch = timeout(ng_fec_tick, priv, hz);
return;
}
static void
ng_fec_stop(struct ifnet *ifp)
{
struct ng_fec_private *priv;
struct ng_fec_bundle *b;
struct ifnet *bifp;
struct ng_fec_portlist *p;
priv = ifp->if_softc;
b = &priv->fec_bundle;
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
bifp = p->fec_if;
bifp->if_flags &= ~IFF_UP;
(*bifp->if_ioctl)(bifp, SIOCSIFFLAGS, NULL);
}
untimeout(ng_fec_tick, priv, priv->fec_ch);
ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
return;
}
static void
ng_fec_tick(void *arg)
{
struct ng_fec_private *priv;
struct ng_fec_bundle *b;
struct ifmediareq ifmr;
struct ifnet *ifp;
struct ng_fec_portlist *p;
int error = 0;
priv = arg;
b = &priv->fec_bundle;
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
bzero((char *)&ifmr, sizeof(ifmr));
ifp = p->fec_if;
error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr);
if (error) {
kprintf("fec%d: failed to check status "
"of link %s\n", priv->unit, ifp->if_xname);
continue;
}
if (ifmr.ifm_status & IFM_AVALID) {
if (ifmr.ifm_status & IFM_ACTIVE) {
if (p->fec_ifstat == -1 ||
p->fec_ifstat == 0) {
p->fec_ifstat = 1;
kprintf("fec%d: port %s in bundle "
"is up\n", priv->unit,
ifp->if_xname);
}
} else {
if (p->fec_ifstat == -1 ||
p->fec_ifstat == 1) {
p->fec_ifstat = 0;
kprintf("fec%d: port %s in bundle "
"is down\n", priv->unit,
ifp->if_xname);
}
}
}
}
ifp = priv->ifp;
if (ifp->if_drv_flags & IFF_DRV_RUNNING)
priv->fec_ch = timeout(ng_fec_tick, priv, hz);
return;
}
static int
ng_fec_ifmedia_upd(struct ifnet *ifp)
{
return(0);
}
static void ng_fec_ifmedia_sts(struct ifnet *ifp,
struct ifmediareq *ifmr)
{
struct ng_fec_private *priv;
struct ng_fec_bundle *b;
struct ng_fec_portlist *p;
priv = ifp->if_softc;
b = &priv->fec_bundle;
ifmr->ifm_status = IFM_AVALID;
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
if (p->fec_ifstat == 1) {
ifmr->ifm_status |= IFM_ACTIVE;
break;
}
}
return;
}
static int
ng_fec_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
{
struct ifreq *const ifr = (struct ifreq *) data;
int s, error = 0;
struct ng_fec_private *priv;
struct ng_fec_bundle *b;
priv = ifp->if_softc;
b = &priv->fec_bundle;
#ifdef DEBUG
ng_fec_print_ioctl(ifp, command, data);
#endif
s = splimp();
switch (command) {
case SIOCSIFADDR:
case SIOCGIFADDR:
error = ether_ioctl(ifp, command, data);
break;
case SIOCSIFMTU:
if (ifr->ifr_mtu >= NG_FEC_MTU_MIN &&
ifr->ifr_mtu <= NG_FEC_MTU_MAX) {
struct ng_fec_portlist *p;
struct ifnet *bifp;
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
bifp = p->fec_if;
error = (*bifp->if_ioctl)(bifp, SIOCSIFMTU,
data);
if (error != 0)
break;
}
if (error == 0)
ifp->if_mtu = ifr->ifr_mtu;
} else
error = EINVAL;
break;
case SIOCSIFFLAGS:
if (ifr->ifr_flags & IFF_UP) {
if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
if (b->fec_ifcnt != 2 &&
b->fec_ifcnt != FEC_BUNDLESIZ) {
kprintf("fec%d: invalid bundle "
"size: %d\n", priv->unit,
b->fec_ifcnt);
error = EINVAL;
break;
}
ng_fec_init(priv);
}
if ((ifp->if_flags & IFF_PROMISC) !=
(priv->if_flags & IFF_PROMISC)) {
ng_fec_setport(ifp, command, data);
priv->if_flags = ifp->if_flags;
}
} else {
if (ifp->if_drv_flags & IFF_DRV_RUNNING)
ng_fec_stop(ifp);
}
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
ng_fec_ether_setmulti(ifp);
error = 0;
break;
case SIOCGIFMEDIA:
case SIOCSIFMEDIA:
error = ifmedia_ioctl(ifp, ifr, &priv->ifmedia, command);
break;
case SIOCSIFPHYS:
error = EOPNOTSUPP;
break;
default:
error = EINVAL;
break;
}
(void) splx(s);
return (error);
}
static void
ng_fec_input(struct ifnet *ifp, struct mbuf *m0)
{
struct ng_node *node;
struct ng_fec_private *priv;
struct ng_fec_bundle *b;
struct ifnet *bifp;
struct ng_fec_portlist *p;
if (ifp == NULL || m0 == NULL)
return;
node = IFP2NG(ifp);
if (node == NULL)
return;
priv = NG_NODE_PRIVATE(node);
b = &priv->fec_bundle;
bifp = priv->ifp;
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
if (p->fec_if == m0->m_pkthdr.rcvif)
break;
}
if (p == NULL)
return;
BPF_MTAP(m0->m_pkthdr.rcvif, m0);
m0->m_pkthdr.rcvif = bifp;
ifp->if_ibytes += m0->m_pkthdr.len;
bifp->if_ipackets++;
bifp->if_input(bifp, m0, NULL, -1);
return;
}
static int
ng_fec_output(struct ifnet *ifp, struct mbuf *m,
struct sockaddr *dst, struct rtentry *rt0)
{
const priv_p priv = (priv_p) ifp->if_softc;
struct ng_fec_bundle *b;
int error;
if (!((ifp->if_flags & IFF_UP) &&
(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
m_freem(m);
return (ENETDOWN);
}
b = &priv->fec_bundle;
switch (b->fec_btype) {
case FEC_BTYPE_MAC:
m->m_flags |= M_FEC_MAC;
break;
#ifdef INET
case FEC_BTYPE_INET:
if (dst->sa_family == AF_INET)
m->m_flags |= M_FEC_INET;
#ifdef INET6
else if (dst->sa_family == AF_INET6)
m->m_flags |= M_FEC_INET6;
#endif
else {
#ifdef DEBUG
if_printf(ifp, "can't do inet aggregation of non "
"inet packet\n");
#endif
m->m_flags |= M_FEC_MAC;
}
break;
#endif
default:
if_printf(ifp, "bogus hash type: %d\n",
b->fec_btype);
m_freem(m);
return(EINVAL);
break;
}
priv->if_error = 0;
error = (*b->fec_if_output)(ifp, m, dst, rt0);
if (priv->if_error && !error)
error = priv->if_error;
return(error);
}
static int
ng_fec_choose_port(struct ng_fec_bundle *b,
struct mbuf *m, struct ifnet **ifp)
{
struct ether_header *eh;
struct mbuf *m0;
#ifdef INET
struct ip *ip;
#ifdef INET6
struct ip6_hdr *ip6;
#endif
#endif
struct ng_fec_portlist *p;
int port = 0, mask;
mask = b->fec_ifcnt == 2 ? 0x1 : 0x3;
eh = mtod(m, struct ether_header *);
#ifdef INET
ip = (struct ip *)(mtod(m, char *) +
sizeof(struct ether_header));
#ifdef INET6
ip6 = (struct ip6_hdr *)(mtod(m, char *) +
sizeof(struct ether_header));
#endif
#endif
m0 = m;
while (m0) {
if (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6))
break;
m0 = m0->m_next;
}
if (m0 == NULL)
return(EINVAL);
switch (m0->m_flags & (M_FEC_MAC|M_FEC_INET|M_FEC_INET6)) {
case M_FEC_MAC:
port = (eh->ether_dhost[5] ^
eh->ether_shost[5]) & mask;
break;
#ifdef INET
case M_FEC_INET:
port = (ntohl(ip->ip_dst.s_addr) ^
ntohl(ip->ip_src.s_addr)) & mask;
break;
#ifdef INET6
case M_FEC_INET6:
port = (ip6->ip6_dst.s6_addr[15] ^
ip6->ip6_dst.s6_addr[15]) & mask;
break;
#endif
#endif
default:
return(EINVAL);
break;
}
TAILQ_FOREACH(p, &b->ng_fec_ports, fec_list) {
if (port == p->fec_idx)
break;
}
if (p->fec_ifstat != 1) {
struct ng_fec_portlist *n = NULL;
n = TAILQ_NEXT(p, fec_list);
if (n == NULL)
n = TAILQ_FIRST(&b->ng_fec_ports);
while (n != p) {
if (n->fec_ifstat == 1)
break;
n = TAILQ_NEXT(n, fec_list);
if (n == NULL)
n = TAILQ_FIRST(&b->ng_fec_ports);
}
if (n == p)
return(EAGAIN);
p = n;
}
*ifp = p->fec_if;
return(0);
}
static void
ng_fec_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
{
struct ng_fec_private *priv;
struct ng_fec_bundle *b;
struct ifnet *oifp = NULL;
struct mbuf *m0;
int error;
priv = ifp->if_softc;
b = &priv->fec_bundle;
m0 = ifq_dequeue(&ifp->if_snd);
if (m0 == NULL)
return;
BPF_MTAP(ifp, m0);
error = ng_fec_choose_port(b, m0, &oifp);
if (error) {
ifp->if_ierrors++;
m_freem(m0);
priv->if_error = ENOBUFS;
return;
}
ifp->if_opackets++;
ifnet_deserialize_tx(ifp, ifsq);
error = ifq_dispatch(oifp, m0, NULL);
ifnet_serialize_tx(ifp, ifsq);
priv->if_error = error;
}
#ifdef DEBUG
static void
ng_fec_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: %s('%c', %d, char[%d])\n",
ifp->if_xname,
str,
IOCGROUP(command),
command & 0xff,
IOCPARM_LEN(command));
}
#endif
static int
ng_fec_constructor(node_p node)
{
char ifname[NG_FEC_FEC_NAME_MAX + 1];
struct ifnet *ifp;
priv_p priv;
const uint8_t eaddr[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
struct ng_fec_bundle *b;
int error = 0;
priv = kmalloc(sizeof(*priv), M_NETGRAPH,
M_WAITOK | M_NULLOK | M_ZERO);
if (priv == NULL)
return (ENOMEM);
ifp = priv->ifp = if_alloc(IFT_ETHER);
if (ifp == NULL) {
kfree(priv, M_NETGRAPH);
return (ENOSPC);
}
b = &priv->fec_bundle;
ifp->if_softc = priv;
if ((error = ng_fec_get_unit(&priv->unit)) != 0) {
if_free(ifp);
kfree(priv, M_NETGRAPH);
return (error);
}
NG_NODE_SET_PRIVATE(node, priv);
priv->node = node;
if_initname(ifp, NG_FEC_FEC_NAME, priv->unit);
ifp->if_start = ng_fec_start;
ifp->if_ioctl = ng_fec_ioctl;
ifp->if_init = ng_fec_init;
ifp->if_watchdog = NULL;
ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
ifp->if_mtu = NG_FEC_MTU_DEFAULT;
ifp->if_flags = (IFF_SIMPLEX|IFF_BROADCAST|IFF_MULTICAST);
ifp->if_addrlen = 0;
ifp->if_hdrlen = 0;
ifp->if_baudrate = 100000000;
TAILQ_INIT(&ifp->if_addrhead);
bzero(ifname, sizeof(ifname));
strlcpy(ifname, ifp->if_xname, sizeof(ifname));
if (ng_name_node(node, ifname) != 0)
log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
ether_ifattach(ifp, eaddr);
callout_handle_init(&priv->fec_ch);
ifp->if_output = ng_fec_output;
TAILQ_INIT(&b->ng_fec_ports);
b->fec_ifcnt = 0;
ifmedia_init(&priv->ifmedia, 0,
ng_fec_ifmedia_upd, ng_fec_ifmedia_sts);
ifmedia_add(&priv->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
ifmedia_set(&priv->ifmedia, IFM_ETHER|IFM_NONE);
return (0);
}
static int
ng_fec_rcvmsg(node_p node, item_p item, hook_p lasthook)
{
const priv_p priv = NG_NODE_PRIVATE(node);
struct ng_fec_bundle *b;
struct ng_mesg *resp = NULL;
struct ng_mesg *msg;
char *ifname;
int error = 0;
NGI_GET_MSG(item, msg);
b = &priv->fec_bundle;
switch (msg->header.typecookie) {
case NGM_FEC_COOKIE:
switch (msg->header.cmd) {
case NGM_FEC_ADD_IFACE:
ifname = msg->data;
error = ng_fec_addport(priv, ifname);
break;
case NGM_FEC_DEL_IFACE:
ifname = msg->data;
error = ng_fec_delport(priv, ifname);
break;
case NGM_FEC_SET_MODE_MAC:
b->fec_btype = FEC_BTYPE_MAC;
break;
#ifdef INET
case NGM_FEC_SET_MODE_INET:
b->fec_btype = FEC_BTYPE_INET;
break;
#ifdef INET6
case NGM_FEC_SET_MODE_INET6:
b->fec_btype = FEC_BTYPE_INET6;
break;
#endif
#endif
default:
error = EINVAL;
break;
}
break;
default:
error = EINVAL;
break;
}
NG_RESPOND_MSG(error, node, item, resp);
NG_FREE_MSG(msg);
return (error);
}
static int
ng_fec_shutdown(node_p node)
{
const priv_p priv = NG_NODE_PRIVATE(node);
struct ng_fec_bundle *b;
struct ng_fec_portlist *p;
b = &priv->fec_bundle;
ng_fec_stop(priv->ifp);
while (!TAILQ_EMPTY(&b->ng_fec_ports)) {
p = TAILQ_FIRST(&b->ng_fec_ports);
ng_fec_ether_cmdmulti(priv->ifp, p, 0);
ng_fec_delport(priv, p->fec_if->if_xname);
}
ether_ifdetach(priv->ifp);
if_free_type(priv->ifp, IFT_ETHER);
ifmedia_removeall(&priv->ifmedia);
ng_fec_free_unit(priv->unit);
kfree(priv, M_NETGRAPH);
NG_NODE_SET_PRIVATE(node, NULL);
NG_NODE_UNREF(node);
return (0);
}
static int
ng_fec_mod_event(module_t mod, int event, void *data)
{
int error = 0;
switch (event) {
case MOD_LOAD:
mtx_init(&ng_fec_mtx, "ng_fec", NULL, MTX_DEF);
break;
case MOD_UNLOAD:
mtx_destroy(&ng_fec_mtx);
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}