#include "opt_inet.h"
#include "opt_inet6.h"
#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/thread2.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/ifq_var.h>
#include <net/netisr.h>
#include <net/bpf.h>
#include <netinet/in.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include <netgraph/ng_parse.h>
#include "ng_iface.h"
#include <netgraph/cisco/ng_cisco.h>
struct iffam {
sa_family_t family;
const char *hookname;
};
typedef const struct iffam *iffam_p;
static const struct iffam gFamilies[] = {
{ AF_INET, NG_IFACE_HOOK_INET },
{ AF_INET6, NG_IFACE_HOOK_INET6 },
{ AF_ATM, NG_IFACE_HOOK_ATM },
{ AF_NATM, NG_IFACE_HOOK_NATM },
};
#define NUM_FAMILIES NELEM(gFamilies)
struct ng_iface_private {
struct ifnet *ifp;
int unit;
node_p node;
hook_p hooks[NUM_FAMILIES];
};
typedef struct ng_iface_private *priv_p;
static void ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *);
static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
struct ucred *cr);
static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
struct sockaddr *dst, struct rtentry *rt0);
static void ng_iface_bpftap(struct ifnet *ifp,
struct mbuf *m, sa_family_t family);
#ifdef DEBUG
static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
#endif
static ng_constructor_t ng_iface_constructor;
static ng_rcvmsg_t ng_iface_rcvmsg;
static ng_shutdown_t ng_iface_rmnode;
static ng_newhook_t ng_iface_newhook;
static ng_rcvdata_t ng_iface_rcvdata;
static ng_disconnect_t ng_iface_disconnect;
static iffam_p get_iffam_from_af(sa_family_t family);
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);
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
};
static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
= NG_CISCO_IPADDR_TYPE_INFO;
static const struct ng_parse_type ng_cisco_ipaddr_type = {
&ng_parse_struct_type,
&ng_cisco_ipaddr_type_fields
};
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 }
};
static struct ng_type typestruct = {
NG_VERSION,
NG_IFACE_NODE_TYPE,
NULL,
ng_iface_constructor,
ng_iface_rcvmsg,
ng_iface_rmnode,
ng_iface_newhook,
NULL,
NULL,
ng_iface_rcvdata,
ng_iface_rcvdata,
ng_iface_disconnect,
ng_iface_cmds
};
NETGRAPH_INIT(iface, &typestruct);
static int *ng_iface_units = NULL;
static int ng_iface_units_len = 0;
#define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY)
static __inline__ iffam_p
get_iffam_from_af(sa_family_t family)
{
iffam_p iffam;
int k;
for (k = 0; k < NUM_FAMILIES; k++) {
iffam = &gFamilies[k];
if (iffam->family == family)
return (iffam);
}
return (NULL);
}
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);
}
static __inline__ hook_p *
get_hook_from_iffam(priv_p priv, iffam_p iffam)
{
return (&priv->hooks[iffam - gFamilies]);
}
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);
}
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) {
int i, *newarray, newlen;
newlen = (2 * ng_iface_units_len) + 4;
newarray = kmalloc(newlen * sizeof(*ng_iface_units),
M_NETGRAPH, M_NOWAIT);
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)
kfree(ng_iface_units, M_NETGRAPH);
ng_iface_units = newarray;
ng_iface_units_len = newlen;
}
bit = ffs(ng_iface_units[index]) - 1;
KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
ng_iface_units[index] &= ~(1 << bit);
*unit = (index * UNITS_BITSPERWORD) + bit;
return (0);
}
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,
("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
("%s: unit=%d is free", __func__, unit));
ng_iface_units[index] |= (1 << bit);
}
static int
ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
struct ucred *cr)
{
struct ifreq *const ifr = (struct ifreq *) data;
int error = 0;
#ifdef DEBUG
ng_iface_print_ioctl(ifp, command, data);
#endif
crit_enter();
switch (command) {
case SIOCSIFADDR:
ifp->if_flags |= (IFF_UP | IFF_RUNNING);
ifq_clr_oactive(&ifp->if_snd);
break;
case SIOCGIFADDR:
break;
case SIOCSIFFLAGS:
if (ifr->ifr_flags & IFF_UP) {
if (!(ifp->if_flags & IFF_RUNNING)) {
ifq_clr_oactive(&ifp->if_snd);
ifp->if_flags |= IFF_RUNNING;
}
} else {
if (ifp->if_flags & IFF_RUNNING) {
ifp->if_flags &= ~IFF_RUNNING;
ifq_clr_oactive(&ifp->if_snd);
}
}
break;
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;
case SIOCADDMULTI:
case SIOCDELMULTI:
error = 0;
break;
case SIOCSIFPHYS:
error = EOPNOTSUPP;
break;
default:
error = EINVAL;
break;
}
crit_exit();
return (error);
}
static int
ng_iface_output_serialized(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;
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
m_freem(m);
return (ENETDOWN);
}
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;
}
ng_iface_bpftap(ifp, m, dst->sa_family);
if (iffam == NULL) {
m_freem(m);
log(LOG_WARNING, "%s: can't handle af%d\n",
ifp->if_xname, (int)dst->sa_family);
return (EAFNOSUPPORT);
}
len = m->m_pkthdr.len;
NG_SEND_DATA(error, *get_hook_from_iffam(priv, iffam), m, meta);
if (error == 0) {
IFNET_STAT_INC(ifp, obytes, len);
IFNET_STAT_INC(ifp, opackets, 1);
}
return (error);
}
static int
ng_iface_output(struct ifnet *ifp, struct mbuf *m,
struct sockaddr *dst, struct rtentry *rt0)
{
struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
int error;
ifsq_serialize_hw(ifsq);
error = ng_iface_output_serialized(ifp, m, dst, rt0);
ifsq_deserialize_hw(ifsq);
return error;
}
static void
ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
{
kprintf("%s: %s called?", ifp->if_xname, __func__);
}
static void
ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
{
int32_t family4 = (int32_t)family;
KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
if (ifp->if_bpf) {
bpf_gettoken();
if (ifp->if_bpf)
bpf_ptap(ifp->if_bpf, m, &family, sizeof(family4));
bpf_reltoken();
}
}
#ifdef DEBUG
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: %s('%c', %d, char[%d])\n",
ifp->if_xname,
str,
IOCGROUP(command),
command & 0xff,
IOCPARM_LEN(command));
}
#endif
static int
ng_iface_constructor(node_p *nodep)
{
char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
struct ifnet *ifp;
node_p node;
priv_p priv;
int error = 0;
priv = kmalloc(sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
if (priv == NULL)
return (ENOMEM);
ifp = kmalloc(sizeof(*ifp), M_NETGRAPH, M_NOWAIT | M_ZERO);
if (ifp == NULL) {
kfree(priv, M_NETGRAPH);
return (ENOMEM);
}
ifp->if_softc = priv;
priv->ifp = ifp;
if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
kfree(ifp, M_NETGRAPH);
kfree(priv, M_NETGRAPH);
return (error);
}
if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
ng_iface_free_unit(priv->unit);
kfree(ifp, M_NETGRAPH);
kfree(priv, M_NETGRAPH);
return (error);
}
node = *nodep;
node->private = priv;
priv->node = node;
if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
ifp->if_output = ng_iface_output;
ifp->if_start = ng_iface_start;
ifp->if_ioctl = ng_iface_ioctl;
ifp->if_watchdog = NULL;
ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
ifp->if_type = IFT_PROPVIRTUAL;
ifp->if_addrlen = 0;
ifp->if_hdrlen = 0;
ifp->if_baudrate = 64000;
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);
if_attach(ifp, NULL);
bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
return (0);
}
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);
hookptr = get_hook_from_iffam((priv_p) node->private, iffam);
if (*hookptr != NULL)
return (EISCONN);
*hookptr = hook;
return (0);
}
static int
ng_iface_rcvmsg(node_p node, struct ng_mesg *msg,
const char *retaddr, struct ng_mesg **rptr)
{
const priv_p priv = node->private;
struct ifnet *const ifp = priv->ifp;
struct ng_mesg *resp = NULL;
int error = 0;
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;
}
arg = (struct ng_iface_ifname *)resp->data;
strlcpy(arg->ngif_name, ifp->if_xname,
sizeof(arg->ngif_name));
break;
}
case NGM_IFACE_POINT2POINT:
case NGM_IFACE_BROADCAST:
{
if ((ifp->if_flags & IFF_UP) != 0)
return (EBUSY);
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;
break;
}
break;
}
default:
error = EINVAL;
break;
}
break;
case NGM_CISCO_COOKIE:
switch (msg->header.cmd) {
case NGM_CISCO_GET_IPADDR:
{
struct ifaddr_container *ifac;
TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
ifa_link) {
struct ifaddr *ifa = ifac->ifa;
struct ng_cisco_ipaddr *ips;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
if (resp == NULL) {
error = ENOMEM;
break;
}
ips = (struct ng_cisco_ipaddr *)resp->data;
ips->ipaddr = ((struct sockaddr_in *)
ifa->ifa_addr)->sin_addr;
ips->netmask = ((struct sockaddr_in *)
ifa->ifa_netmask)->sin_addr;
break;
}
if (ifac == NULL)
error = EADDRNOTAVAIL;
break;
}
default:
error = EINVAL;
break;
}
break;
default:
error = EINVAL;
break;
}
if (rptr)
*rptr = resp;
else if (resp)
kfree(resp, M_NETGRAPH);
kfree(msg, M_NETGRAPH);
return (error);
}
static int
ng_iface_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
{
const priv_p priv = hook->node->private;
const iffam_p iffam = get_iffam_from_hook(priv, hook);
struct ifnet *const ifp = priv->ifp;
int isr;
KASSERT(iffam != NULL, ("%s: iffam", __func__));
KASSERT(m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
if (m == NULL)
return (EINVAL);
if ((ifp->if_flags & IFF_UP) == 0) {
NG_FREE_DATA(m, meta);
return (ENETDOWN);
}
IFNET_STAT_INC(ifp, ipackets, 1);
IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
m->m_pkthdr.rcvif = ifp;
ng_iface_bpftap(ifp, m, iffam->family);
NG_FREE_META(meta);
switch (iffam->family) {
#ifdef INET
case AF_INET:
isr = NETISR_IP;
break;
#endif
#ifdef INET6
case AF_INET6:
isr = NETISR_IPV6;
break;
#endif
default:
m_freem(m);
return (EAFNOSUPPORT);
}
netisr_queue(isr, m);
return (0);
}
static int
ng_iface_rmnode(node_p node)
{
const priv_p priv = node->private;
ng_cutlinks(node);
ng_unname(node);
bpfdetach(priv->ifp);
if_detach(priv->ifp);
kfree(priv->ifp, M_NETGRAPH);
priv->ifp = NULL;
ng_iface_free_unit(priv->unit);
kfree(priv, M_NETGRAPH);
node->private = NULL;
ng_unref(node);
return (0);
}
static int
ng_iface_disconnect(hook_p hook)
{
const priv_p priv = hook->node->private;
const iffam_p iffam = get_iffam_from_hook(priv, hook);
if (iffam == NULL)
panic(__func__);
*get_hook_from_iffam(priv, iffam) = NULL;
return (0);
}