10147868eSNuno Antunes /*
20147868eSNuno Antunes * ng_iface.c
30147868eSNuno Antunes */
40147868eSNuno Antunes
50147868eSNuno Antunes /*-
60147868eSNuno Antunes * Copyright (c) 1996-1999 Whistle Communications, Inc.
70147868eSNuno Antunes * All rights reserved.
80147868eSNuno Antunes *
90147868eSNuno Antunes * Subject to the following obligations and disclaimer of warranty, use and
100147868eSNuno Antunes * redistribution of this software, in source or object code forms, with or
110147868eSNuno Antunes * without modifications are expressly permitted by Whistle Communications;
120147868eSNuno Antunes * provided, however, that:
130147868eSNuno Antunes * 1. Any and all reproductions of the source or object code must include the
140147868eSNuno Antunes * copyright notice above and the following disclaimer of warranties; and
150147868eSNuno Antunes * 2. No rights are granted, in any manner or form, to use Whistle
160147868eSNuno Antunes * Communications, Inc. trademarks, including the mark "WHISTLE
170147868eSNuno Antunes * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
180147868eSNuno Antunes * such appears in the above copyright notice or in the software.
190147868eSNuno Antunes *
200147868eSNuno Antunes * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
210147868eSNuno Antunes * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
220147868eSNuno Antunes * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
230147868eSNuno Antunes * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
240147868eSNuno Antunes * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
250147868eSNuno Antunes * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
260147868eSNuno Antunes * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
270147868eSNuno Antunes * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
280147868eSNuno Antunes * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
290147868eSNuno Antunes * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
300147868eSNuno Antunes * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
310147868eSNuno Antunes * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
320147868eSNuno Antunes * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
330147868eSNuno Antunes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
340147868eSNuno Antunes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
350147868eSNuno Antunes * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
360147868eSNuno Antunes * OF SUCH DAMAGE.
370147868eSNuno Antunes *
380147868eSNuno Antunes * Author: Archie Cobbs <archie@freebsd.org>
390147868eSNuno Antunes *
400147868eSNuno Antunes * $FreeBSD: src/sys/netgraph/ng_iface.c,v 1.48 2008/01/31 08:51:48 mav Exp $
410147868eSNuno Antunes * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
420147868eSNuno Antunes */
430147868eSNuno Antunes
440147868eSNuno Antunes /*
450147868eSNuno Antunes * This node is also a system networking interface. It has
466f25d555SSascha Wildner * a hook for each protocol (IP, AppleTalk, etc). Packets
470147868eSNuno Antunes * are simply relayed between the interface and the hooks.
480147868eSNuno Antunes *
490147868eSNuno Antunes * Interfaces are named ng0, ng1, etc. New nodes take the
500147868eSNuno Antunes * first available interface name.
510147868eSNuno Antunes *
520147868eSNuno Antunes * This node also includes Berkeley packet filter support.
530147868eSNuno Antunes */
540147868eSNuno Antunes
550147868eSNuno Antunes #include "opt_inet.h"
560147868eSNuno Antunes #include "opt_inet6.h"
570147868eSNuno Antunes
580147868eSNuno Antunes #include <sys/param.h>
590147868eSNuno Antunes #include <sys/systm.h>
600147868eSNuno Antunes #include <sys/errno.h>
610147868eSNuno Antunes #include <sys/kernel.h>
620147868eSNuno Antunes #include <sys/malloc.h>
630147868eSNuno Antunes #include <sys/mbuf.h>
640147868eSNuno Antunes #include <sys/random.h>
650147868eSNuno Antunes #include <sys/sockio.h>
660147868eSNuno Antunes #include <sys/socket.h>
670147868eSNuno Antunes #include <sys/syslog.h>
680147868eSNuno Antunes #include <sys/libkern.h>
690147868eSNuno Antunes
700147868eSNuno Antunes #include <net/if.h>
710147868eSNuno Antunes #include <net/if_types.h>
720147868eSNuno Antunes #include <net/ifq_var.h>
730147868eSNuno Antunes #include <net/bpf.h>
740147868eSNuno Antunes #include <net/netisr.h>
750147868eSNuno Antunes
760147868eSNuno Antunes #include <netinet/in.h>
770147868eSNuno Antunes
780147868eSNuno Antunes #include <netgraph7/ng_message.h>
790147868eSNuno Antunes #include <netgraph7/netgraph.h>
800147868eSNuno Antunes #include <netgraph7/ng_parse.h>
810147868eSNuno Antunes #include <netgraph7/cisco/ng_cisco.h>
820147868eSNuno Antunes #include "ng_iface.h"
830147868eSNuno Antunes
840147868eSNuno Antunes #ifdef NG_SEPARATE_MALLOC
850147868eSNuno Antunes MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
860147868eSNuno Antunes #else
870147868eSNuno Antunes #define M_NETGRAPH_IFACE M_NETGRAPH
880147868eSNuno Antunes #endif
890147868eSNuno Antunes
900147868eSNuno Antunes /* This struct describes one address family */
910147868eSNuno Antunes struct iffam {
920147868eSNuno Antunes sa_family_t family; /* Address family */
930147868eSNuno Antunes const char *hookname; /* Name for hook */
940147868eSNuno Antunes };
950147868eSNuno Antunes typedef const struct iffam *iffam_p;
960147868eSNuno Antunes
970147868eSNuno Antunes /* List of address families supported by our interface */
9853f03f86SNuno Antunes static const struct iffam gFamilies[] = {
990147868eSNuno Antunes { AF_INET, NG_IFACE_HOOK_INET },
1000147868eSNuno Antunes { AF_INET6, NG_IFACE_HOOK_INET6 },
1010147868eSNuno Antunes { AF_ATM, NG_IFACE_HOOK_ATM },
1020147868eSNuno Antunes { AF_NATM, NG_IFACE_HOOK_NATM },
1030147868eSNuno Antunes };
1040147868eSNuno Antunes #define NUM_FAMILIES NELEM(gFamilies)
1050147868eSNuno Antunes
1060147868eSNuno Antunes /* Node private data */
1070147868eSNuno Antunes struct ng_iface_private {
1080147868eSNuno Antunes struct ifnet *ifp; /* Our interface */
1090147868eSNuno Antunes int unit; /* Interface unit number */
1100147868eSNuno Antunes node_p node; /* Our netgraph node */
1110147868eSNuno Antunes hook_p hooks[NUM_FAMILIES]; /* Hook for each address family */
1120147868eSNuno Antunes };
1130147868eSNuno Antunes typedef struct ng_iface_private *priv_p;
1140147868eSNuno Antunes
1150147868eSNuno Antunes /* Interface methods */
116f0a26983SSepherosa Ziehau static void ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *);
1170147868eSNuno Antunes static int ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
1180147868eSNuno Antunes struct ucred *cr);
1190147868eSNuno Antunes static int ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
1200147868eSNuno Antunes struct sockaddr *dst, struct rtentry *rt0);
1210147868eSNuno Antunes static void ng_iface_bpftap(struct ifnet *ifp,
1220147868eSNuno Antunes struct mbuf *m, sa_family_t family);
1230147868eSNuno Antunes static int ng_iface_send(struct ifnet *ifp, struct mbuf *m,
1240147868eSNuno Antunes sa_family_t sa);
1250147868eSNuno Antunes #ifdef DEBUG
1260147868eSNuno Antunes static void ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
1270147868eSNuno Antunes #endif
1280147868eSNuno Antunes
1290147868eSNuno Antunes /* Netgraph methods */
1300147868eSNuno Antunes static int ng_iface_mod_event(module_t, int, void *);
1310147868eSNuno Antunes static ng_constructor_t ng_iface_constructor;
1320147868eSNuno Antunes static ng_rcvmsg_t ng_iface_rcvmsg;
1330147868eSNuno Antunes static ng_shutdown_t ng_iface_shutdown;
1340147868eSNuno Antunes static ng_newhook_t ng_iface_newhook;
1350147868eSNuno Antunes static ng_rcvdata_t ng_iface_rcvdata;
1360147868eSNuno Antunes static ng_disconnect_t ng_iface_disconnect;
1370147868eSNuno Antunes
1380147868eSNuno Antunes /* Helper stuff */
1390147868eSNuno Antunes static iffam_p get_iffam_from_af(sa_family_t family);
1400147868eSNuno Antunes static iffam_p get_iffam_from_hook(priv_p priv, hook_p hook);
1410147868eSNuno Antunes static iffam_p get_iffam_from_name(const char *name);
1420147868eSNuno Antunes static hook_p *get_hook_from_iffam(priv_p priv, iffam_p iffam);
1430147868eSNuno Antunes
1440147868eSNuno Antunes /* Parse type for struct ng_cisco_ipaddr */
1450147868eSNuno Antunes static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
1460147868eSNuno Antunes = NG_CISCO_IPADDR_TYPE_INFO;
1470147868eSNuno Antunes static const struct ng_parse_type ng_cisco_ipaddr_type = {
1480147868eSNuno Antunes &ng_parse_struct_type,
1490147868eSNuno Antunes &ng_cisco_ipaddr_type_fields
1500147868eSNuno Antunes };
1510147868eSNuno Antunes
1520147868eSNuno Antunes /* List of commands and how to convert arguments to/from ASCII */
1530147868eSNuno Antunes static const struct ng_cmdlist ng_iface_cmds[] = {
1540147868eSNuno Antunes {
1550147868eSNuno Antunes NGM_IFACE_COOKIE,
1560147868eSNuno Antunes NGM_IFACE_GET_IFNAME,
1570147868eSNuno Antunes "getifname",
1580147868eSNuno Antunes NULL,
1590147868eSNuno Antunes &ng_parse_string_type
1600147868eSNuno Antunes },
1610147868eSNuno Antunes {
1620147868eSNuno Antunes NGM_IFACE_COOKIE,
1630147868eSNuno Antunes NGM_IFACE_POINT2POINT,
1640147868eSNuno Antunes "point2point",
1650147868eSNuno Antunes NULL,
1660147868eSNuno Antunes NULL
1670147868eSNuno Antunes },
1680147868eSNuno Antunes {
1690147868eSNuno Antunes NGM_IFACE_COOKIE,
1700147868eSNuno Antunes NGM_IFACE_BROADCAST,
1710147868eSNuno Antunes "broadcast",
1720147868eSNuno Antunes NULL,
1730147868eSNuno Antunes NULL
1740147868eSNuno Antunes },
1750147868eSNuno Antunes {
1760147868eSNuno Antunes NGM_CISCO_COOKIE,
1770147868eSNuno Antunes NGM_CISCO_GET_IPADDR,
1780147868eSNuno Antunes "getipaddr",
1790147868eSNuno Antunes NULL,
1800147868eSNuno Antunes &ng_cisco_ipaddr_type
1810147868eSNuno Antunes },
1820147868eSNuno Antunes {
1830147868eSNuno Antunes NGM_IFACE_COOKIE,
1840147868eSNuno Antunes NGM_IFACE_GET_IFINDEX,
1850147868eSNuno Antunes "getifindex",
1860147868eSNuno Antunes NULL,
1870147868eSNuno Antunes &ng_parse_uint32_type
1880147868eSNuno Antunes },
1890147868eSNuno Antunes { 0 }
1900147868eSNuno Antunes };
1910147868eSNuno Antunes
1920147868eSNuno Antunes /* Node type descriptor */
1930147868eSNuno Antunes static struct ng_type typestruct = {
1940147868eSNuno Antunes .version = NG_ABI_VERSION,
1950147868eSNuno Antunes .name = NG_IFACE_NODE_TYPE,
1960147868eSNuno Antunes .mod_event = ng_iface_mod_event,
1970147868eSNuno Antunes .constructor = ng_iface_constructor,
1980147868eSNuno Antunes .rcvmsg = ng_iface_rcvmsg,
1990147868eSNuno Antunes .shutdown = ng_iface_shutdown,
2000147868eSNuno Antunes .newhook = ng_iface_newhook,
2010147868eSNuno Antunes .rcvdata = ng_iface_rcvdata,
2020147868eSNuno Antunes .disconnect = ng_iface_disconnect,
2030147868eSNuno Antunes .cmdlist = ng_iface_cmds,
2040147868eSNuno Antunes };
2050147868eSNuno Antunes NETGRAPH_INIT(iface, &typestruct);
2060147868eSNuno Antunes
2070147868eSNuno Antunes /* We keep a bitmap indicating which unit numbers are free.
2080147868eSNuno Antunes One means the unit number is free, zero means it's taken. */
2090147868eSNuno Antunes static int *ng_iface_units = NULL;
2100147868eSNuno Antunes static int ng_iface_units_len = 0;
2110147868eSNuno Antunes
2120147868eSNuno Antunes #define UNITS_BITSPERWORD (sizeof(*ng_iface_units) * NBBY)
2130147868eSNuno Antunes
2140147868eSNuno Antunes
2150147868eSNuno Antunes /************************************************************************
2160147868eSNuno Antunes HELPER STUFF
2170147868eSNuno Antunes ************************************************************************/
2180147868eSNuno Antunes
2190147868eSNuno Antunes /*
2200147868eSNuno Antunes * Get the family descriptor from the family ID
2210147868eSNuno Antunes */
2220147868eSNuno Antunes static __inline iffam_p
get_iffam_from_af(sa_family_t family)2230147868eSNuno Antunes get_iffam_from_af(sa_family_t family)
2240147868eSNuno Antunes {
2250147868eSNuno Antunes iffam_p iffam;
2260147868eSNuno Antunes int k;
2270147868eSNuno Antunes
2280147868eSNuno Antunes for (k = 0; k < NUM_FAMILIES; k++) {
2290147868eSNuno Antunes iffam = &gFamilies[k];
2300147868eSNuno Antunes if (iffam->family == family)
2310147868eSNuno Antunes return (iffam);
2320147868eSNuno Antunes }
2330147868eSNuno Antunes return (NULL);
2340147868eSNuno Antunes }
2350147868eSNuno Antunes
2360147868eSNuno Antunes /*
2370147868eSNuno Antunes * Get the family descriptor from the hook
2380147868eSNuno Antunes */
2390147868eSNuno Antunes static __inline iffam_p
get_iffam_from_hook(priv_p priv,hook_p hook)2400147868eSNuno Antunes get_iffam_from_hook(priv_p priv, hook_p hook)
2410147868eSNuno Antunes {
2420147868eSNuno Antunes int k;
2430147868eSNuno Antunes
2440147868eSNuno Antunes for (k = 0; k < NUM_FAMILIES; k++)
2450147868eSNuno Antunes if (priv->hooks[k] == hook)
2460147868eSNuno Antunes return (&gFamilies[k]);
2470147868eSNuno Antunes return (NULL);
2480147868eSNuno Antunes }
2490147868eSNuno Antunes
2500147868eSNuno Antunes /*
2510147868eSNuno Antunes * Get the hook from the iffam descriptor
2520147868eSNuno Antunes */
2530147868eSNuno Antunes
2540147868eSNuno Antunes static __inline hook_p *
get_hook_from_iffam(priv_p priv,iffam_p iffam)2550147868eSNuno Antunes get_hook_from_iffam(priv_p priv, iffam_p iffam)
2560147868eSNuno Antunes {
2570147868eSNuno Antunes return (&priv->hooks[iffam - gFamilies]);
2580147868eSNuno Antunes }
2590147868eSNuno Antunes
2600147868eSNuno Antunes /*
2610147868eSNuno Antunes * Get the iffam descriptor from the name
2620147868eSNuno Antunes */
2630147868eSNuno Antunes static __inline iffam_p
get_iffam_from_name(const char * name)2640147868eSNuno Antunes get_iffam_from_name(const char *name)
2650147868eSNuno Antunes {
2660147868eSNuno Antunes iffam_p iffam;
2670147868eSNuno Antunes int k;
2680147868eSNuno Antunes
2690147868eSNuno Antunes for (k = 0; k < NUM_FAMILIES; k++) {
2700147868eSNuno Antunes iffam = &gFamilies[k];
2710147868eSNuno Antunes if (!strcmp(iffam->hookname, name))
2720147868eSNuno Antunes return (iffam);
2730147868eSNuno Antunes }
2740147868eSNuno Antunes return (NULL);
2750147868eSNuno Antunes }
2760147868eSNuno Antunes
2770147868eSNuno Antunes /*
2780147868eSNuno Antunes * Find the first free unit number for a new interface.
2790147868eSNuno Antunes * Increase the size of the unit bitmap as necessary.
2800147868eSNuno Antunes */
2810147868eSNuno Antunes static __inline__ int
ng_iface_get_unit(int * unit)2820147868eSNuno Antunes ng_iface_get_unit(int *unit)
2830147868eSNuno Antunes {
2840147868eSNuno Antunes int index, bit;
2850147868eSNuno Antunes
2860147868eSNuno Antunes for (index = 0; index < ng_iface_units_len
2870147868eSNuno Antunes && ng_iface_units[index] == 0; index++);
2880147868eSNuno Antunes if (index == ng_iface_units_len) { /* extend array */
2890147868eSNuno Antunes int i, *newarray, newlen;
2900147868eSNuno Antunes
2910147868eSNuno Antunes newlen = (2 * ng_iface_units_len) + 4;
29253f03f86SNuno Antunes newarray = kmalloc(newlen * sizeof(*ng_iface_units),
2930147868eSNuno Antunes M_NETGRAPH_IFACE, M_NOWAIT);
2940147868eSNuno Antunes if (newarray == NULL)
2950147868eSNuno Antunes return (ENOMEM);
2960147868eSNuno Antunes bcopy(ng_iface_units, newarray,
2970147868eSNuno Antunes ng_iface_units_len * sizeof(*ng_iface_units));
2980147868eSNuno Antunes for (i = ng_iface_units_len; i < newlen; i++)
2990147868eSNuno Antunes newarray[i] = ~0;
3000147868eSNuno Antunes if (ng_iface_units != NULL)
30153f03f86SNuno Antunes kfree(ng_iface_units, M_NETGRAPH_IFACE);
3020147868eSNuno Antunes ng_iface_units = newarray;
3030147868eSNuno Antunes ng_iface_units_len = newlen;
3040147868eSNuno Antunes }
3050147868eSNuno Antunes bit = ffs(ng_iface_units[index]) - 1;
3060147868eSNuno Antunes KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
3070147868eSNuno Antunes ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
3080147868eSNuno Antunes ng_iface_units[index] &= ~(1 << bit);
3090147868eSNuno Antunes *unit = (index * UNITS_BITSPERWORD) + bit;
3100147868eSNuno Antunes return (0);
3110147868eSNuno Antunes }
3120147868eSNuno Antunes
3130147868eSNuno Antunes /*
3140147868eSNuno Antunes * Free a no longer needed unit number.
3150147868eSNuno Antunes */
3160147868eSNuno Antunes static __inline__ void
ng_iface_free_unit(int unit)3170147868eSNuno Antunes ng_iface_free_unit(int unit)
3180147868eSNuno Antunes {
3190147868eSNuno Antunes int index, bit;
3200147868eSNuno Antunes
3210147868eSNuno Antunes index = unit / UNITS_BITSPERWORD;
3220147868eSNuno Antunes bit = unit % UNITS_BITSPERWORD;
3230147868eSNuno Antunes KASSERT(index < ng_iface_units_len,
3240147868eSNuno Antunes ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
3250147868eSNuno Antunes KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
3260147868eSNuno Antunes ("%s: unit=%d is free", __func__, unit));
3270147868eSNuno Antunes ng_iface_units[index] |= (1 << bit);
3280147868eSNuno Antunes /*
3290147868eSNuno Antunes * XXX We could think about reducing the size of ng_iface_units[]
3300147868eSNuno Antunes * XXX here if the last portion is all ones
3310147868eSNuno Antunes */
3320147868eSNuno Antunes }
3330147868eSNuno Antunes
3340147868eSNuno Antunes /************************************************************************
3350147868eSNuno Antunes INTERFACE STUFF
3360147868eSNuno Antunes ************************************************************************/
3370147868eSNuno Antunes
3380147868eSNuno Antunes /*
3390147868eSNuno Antunes * Process an ioctl for the virtual interface
3400147868eSNuno Antunes */
3410147868eSNuno Antunes static int
ng_iface_ioctl(struct ifnet * ifp,u_long command,caddr_t data,struct ucred * cr)3420147868eSNuno Antunes ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
3430147868eSNuno Antunes struct ucred *cr)
3440147868eSNuno Antunes {
3450147868eSNuno Antunes struct ifreq *const ifr = (struct ifreq *) data;
3460147868eSNuno Antunes int error = 0;
3470147868eSNuno Antunes
3480147868eSNuno Antunes #ifdef DEBUG
3490147868eSNuno Antunes ng_iface_print_ioctl(ifp, command, data);
3500147868eSNuno Antunes #endif
3510147868eSNuno Antunes crit_enter();
3520147868eSNuno Antunes switch (command) {
3530147868eSNuno Antunes
3540147868eSNuno Antunes /* These two are mostly handled at a higher layer */
3550147868eSNuno Antunes case SIOCSIFADDR:
3560147868eSNuno Antunes ifp->if_flags |= IFF_UP;
3570147868eSNuno Antunes ifp->if_flags |= IFF_RUNNING;
3589ed293e0SSepherosa Ziehau ifq_clr_oactive(&ifp->if_snd);
3590147868eSNuno Antunes break;
3600147868eSNuno Antunes case SIOCGIFADDR:
3610147868eSNuno Antunes break;
3620147868eSNuno Antunes
3630147868eSNuno Antunes /* Set flags */
3640147868eSNuno Antunes case SIOCSIFFLAGS:
3650147868eSNuno Antunes /*
3660147868eSNuno Antunes * If the interface is marked up and stopped, then start it.
3670147868eSNuno Antunes * If it is marked down and running, then stop it.
3680147868eSNuno Antunes */
3690147868eSNuno Antunes if (ifr->ifr_flags & IFF_UP) {
3700147868eSNuno Antunes if (!(ifp->if_flags & IFF_RUNNING)) {
3719ed293e0SSepherosa Ziehau ifq_clr_oactive(&ifp->if_snd);
3720147868eSNuno Antunes ifp->if_flags |= IFF_RUNNING;
3730147868eSNuno Antunes }
3740147868eSNuno Antunes } else {
3759ed293e0SSepherosa Ziehau if (ifp->if_flags & IFF_RUNNING) {
3769ed293e0SSepherosa Ziehau ifp->if_flags &= ~IFF_RUNNING;
3779ed293e0SSepherosa Ziehau ifq_clr_oactive(&ifp->if_snd);
3789ed293e0SSepherosa Ziehau }
3790147868eSNuno Antunes }
3800147868eSNuno Antunes break;
3810147868eSNuno Antunes
3820147868eSNuno Antunes /* Set the interface MTU */
3830147868eSNuno Antunes case SIOCSIFMTU:
3840147868eSNuno Antunes if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
3850147868eSNuno Antunes || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
3860147868eSNuno Antunes error = EINVAL;
3870147868eSNuno Antunes else
3880147868eSNuno Antunes ifp->if_mtu = ifr->ifr_mtu;
3890147868eSNuno Antunes break;
3900147868eSNuno Antunes
3910147868eSNuno Antunes /* Stuff that's not supported */
3920147868eSNuno Antunes case SIOCADDMULTI:
3930147868eSNuno Antunes case SIOCDELMULTI:
3940147868eSNuno Antunes error = 0;
3950147868eSNuno Antunes break;
3960147868eSNuno Antunes case SIOCSIFPHYS:
3970147868eSNuno Antunes error = EOPNOTSUPP;
3980147868eSNuno Antunes break;
3990147868eSNuno Antunes
4000147868eSNuno Antunes default:
4010147868eSNuno Antunes error = EINVAL;
4020147868eSNuno Antunes break;
4030147868eSNuno Antunes }
4040147868eSNuno Antunes crit_exit();
4050147868eSNuno Antunes return (error);
4060147868eSNuno Antunes }
4070147868eSNuno Antunes
4080147868eSNuno Antunes /*
4090147868eSNuno Antunes * This routine is called to deliver a packet out the interface.
4100147868eSNuno Antunes * We simply look at the address family and relay the packet to
4110147868eSNuno Antunes * the corresponding hook, if it exists and is connected.
4120147868eSNuno Antunes */
4130147868eSNuno Antunes
4140147868eSNuno Antunes static int
ng_iface_output(struct ifnet * ifp,struct mbuf * m,struct sockaddr * dst,struct rtentry * rt0)4150147868eSNuno Antunes ng_iface_output(struct ifnet *ifp, struct mbuf *m,
4160147868eSNuno Antunes struct sockaddr *dst, struct rtentry *rt0)
4170147868eSNuno Antunes {
4180147868eSNuno Antunes uint32_t af;
4190147868eSNuno Antunes int error;
4200147868eSNuno Antunes
4210147868eSNuno Antunes /* Check interface flags */
4220147868eSNuno Antunes if (!((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))) {
4230147868eSNuno Antunes m_freem(m);
4240147868eSNuno Antunes return (ENETDOWN);
4250147868eSNuno Antunes }
4260147868eSNuno Antunes
4270147868eSNuno Antunes /* BPF writes need to be handled specially. */
4280147868eSNuno Antunes if (dst->sa_family == AF_UNSPEC) {
4290147868eSNuno Antunes bcopy(dst->sa_data, &af, sizeof(af));
4300147868eSNuno Antunes dst->sa_family = af;
4310147868eSNuno Antunes }
4320147868eSNuno Antunes
4330147868eSNuno Antunes /* Berkeley packet filter */
4340147868eSNuno Antunes ng_iface_bpftap(ifp, m, dst->sa_family);
4350147868eSNuno Antunes
4360147868eSNuno Antunes if (ifq_is_enabled(&ifp->if_snd)) {
437b5523eacSSascha Wildner M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
4380147868eSNuno Antunes if (m == NULL) {
439d40991efSSepherosa Ziehau IFNET_STAT_INC(ifp, oerrors, 1);
4400147868eSNuno Antunes return (ENOBUFS);
4410147868eSNuno Antunes }
4420147868eSNuno Antunes *(sa_family_t *)m->m_data = dst->sa_family;
443491cc480SSepherosa Ziehau error = ifq_dispatch(ifp, m, NULL);
4440147868eSNuno Antunes } else
4450147868eSNuno Antunes error = ng_iface_send(ifp, m, dst->sa_family);
4460147868eSNuno Antunes
4470147868eSNuno Antunes return (error);
4480147868eSNuno Antunes }
4490147868eSNuno Antunes
4500147868eSNuno Antunes /*
4510147868eSNuno Antunes * Start method is used only when ALTQ is enabled.
4520147868eSNuno Antunes */
4530147868eSNuno Antunes static void
ng_iface_start(struct ifnet * ifp,struct ifaltq_subque * ifsq)454f0a26983SSepherosa Ziehau ng_iface_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
4550147868eSNuno Antunes {
4560147868eSNuno Antunes sa_family_t sa;
4570147868eSNuno Antunes
4580147868eSNuno Antunes KASSERT(ifq_is_enabled(&ifp->if_snd), ("%s without ALTQ", __func__));
4590147868eSNuno Antunes
4600147868eSNuno Antunes for(;;) {
46192aa1e65SSepherosa Ziehau struct mbuf *m;
46292aa1e65SSepherosa Ziehau
463ac9843a1SSepherosa Ziehau m = ifq_dequeue(&ifp->if_snd);
4640147868eSNuno Antunes if (m == NULL)
4650147868eSNuno Antunes break;
4660147868eSNuno Antunes sa = *mtod(m, sa_family_t *);
4670147868eSNuno Antunes m_adj(m, sizeof(sa_family_t));
4680147868eSNuno Antunes ng_iface_send(ifp, m, sa);
4690147868eSNuno Antunes }
4700147868eSNuno Antunes }
4710147868eSNuno Antunes
4720147868eSNuno Antunes /*
4730147868eSNuno Antunes * Flash a packet by the BPF (requires prepending 4 byte AF header)
4740147868eSNuno Antunes * Note the phoney mbuf; this is OK because BPF treats it read-only.
4750147868eSNuno Antunes */
4760147868eSNuno Antunes static void
ng_iface_bpftap(struct ifnet * ifp,struct mbuf * m,sa_family_t family)4770147868eSNuno Antunes ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
4780147868eSNuno Antunes {
4790147868eSNuno Antunes int32_t family4 = (int32_t)family;
4800147868eSNuno Antunes
4810147868eSNuno Antunes KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
4820147868eSNuno Antunes
483fda7d388SSepherosa Ziehau if (ifp->if_bpf) {
484fda7d388SSepherosa Ziehau bpf_gettoken();
4850147868eSNuno Antunes if (ifp->if_bpf)
4860147868eSNuno Antunes bpf_ptap(ifp->if_bpf, m, &family4, sizeof(family4));
487fda7d388SSepherosa Ziehau bpf_reltoken();
488fda7d388SSepherosa Ziehau }
4890147868eSNuno Antunes }
4900147868eSNuno Antunes
4910147868eSNuno Antunes /*
4920147868eSNuno Antunes * This routine does actual delivery of the packet into the
4930147868eSNuno Antunes * netgraph(4). It is called from ng_iface_start() and
4940147868eSNuno Antunes * ng_iface_output().
4950147868eSNuno Antunes */
4960147868eSNuno Antunes static int
ng_iface_send(struct ifnet * ifp,struct mbuf * m,sa_family_t sa)4970147868eSNuno Antunes ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
4980147868eSNuno Antunes {
4990147868eSNuno Antunes const priv_p priv = (priv_p) ifp->if_softc;
5000147868eSNuno Antunes const iffam_p iffam = get_iffam_from_af(sa);
5010147868eSNuno Antunes int error;
5020147868eSNuno Antunes int len;
5030147868eSNuno Antunes
5040147868eSNuno Antunes /* Check address family to determine hook (if known) */
5050147868eSNuno Antunes if (iffam == NULL) {
5060147868eSNuno Antunes m_freem(m);
5070147868eSNuno Antunes log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
5080147868eSNuno Antunes return (EAFNOSUPPORT);
5090147868eSNuno Antunes }
5100147868eSNuno Antunes
5110147868eSNuno Antunes /* Copy length before the mbuf gets invalidated. */
5120147868eSNuno Antunes len = m->m_pkthdr.len;
5130147868eSNuno Antunes
5140147868eSNuno Antunes /* Send packet. If hook is not connected,
5150147868eSNuno Antunes mbuf will get freed. */
5160147868eSNuno Antunes NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
5170147868eSNuno Antunes
5180147868eSNuno Antunes /* Update stats. */
5190147868eSNuno Antunes if (error == 0) {
520d40991efSSepherosa Ziehau IFNET_STAT_INC(ifp, obytes, len);
521d40991efSSepherosa Ziehau IFNET_STAT_INC(ifp, opackets, 1);
5220147868eSNuno Antunes }
5230147868eSNuno Antunes
5240147868eSNuno Antunes return (error);
5250147868eSNuno Antunes }
5260147868eSNuno Antunes
5270147868eSNuno Antunes #ifdef DEBUG
5280147868eSNuno Antunes /*
5290147868eSNuno Antunes * Display an ioctl to the virtual interface
5300147868eSNuno Antunes */
5310147868eSNuno Antunes
5320147868eSNuno Antunes static void
ng_iface_print_ioctl(struct ifnet * ifp,int command,caddr_t data)5330147868eSNuno Antunes ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
5340147868eSNuno Antunes {
5350147868eSNuno Antunes char *str;
5360147868eSNuno Antunes
5370147868eSNuno Antunes switch (command & IOC_DIRMASK) {
5380147868eSNuno Antunes case IOC_VOID:
5390147868eSNuno Antunes str = "IO";
5400147868eSNuno Antunes break;
5410147868eSNuno Antunes case IOC_OUT:
5420147868eSNuno Antunes str = "IOR";
5430147868eSNuno Antunes break;
5440147868eSNuno Antunes case IOC_IN:
5450147868eSNuno Antunes str = "IOW";
5460147868eSNuno Antunes break;
5470147868eSNuno Antunes case IOC_INOUT:
5480147868eSNuno Antunes str = "IORW";
5490147868eSNuno Antunes break;
5500147868eSNuno Antunes default:
5510147868eSNuno Antunes str = "IO??";
5520147868eSNuno Antunes }
5530147868eSNuno Antunes log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
5540147868eSNuno Antunes ifp->if_xname,
5550147868eSNuno Antunes str,
5560147868eSNuno Antunes IOCGROUP(command),
5570147868eSNuno Antunes command & 0xff,
5580147868eSNuno Antunes IOCPARM_LEN(command));
5590147868eSNuno Antunes }
5600147868eSNuno Antunes #endif /* DEBUG */
5610147868eSNuno Antunes
5620147868eSNuno Antunes /************************************************************************
5630147868eSNuno Antunes NETGRAPH NODE STUFF
5640147868eSNuno Antunes ************************************************************************/
5650147868eSNuno Antunes
5660147868eSNuno Antunes /*
5670147868eSNuno Antunes * Constructor for a node
5680147868eSNuno Antunes */
5690147868eSNuno Antunes static int
ng_iface_constructor(node_p node)5700147868eSNuno Antunes ng_iface_constructor(node_p node)
5710147868eSNuno Antunes {
5720147868eSNuno Antunes struct ifnet *ifp;
5730147868eSNuno Antunes priv_p priv;
5740147868eSNuno Antunes int error;
5750147868eSNuno Antunes
5760147868eSNuno Antunes /* Allocate node and interface private structures */
5770147868eSNuno Antunes priv = kmalloc(sizeof(*priv), M_NETGRAPH_IFACE,
5780147868eSNuno Antunes M_WAITOK | M_NULLOK | M_ZERO);
5790147868eSNuno Antunes if (priv == NULL)
5800147868eSNuno Antunes return (ENOMEM);
58153f03f86SNuno Antunes ifp = kmalloc(sizeof(*ifp), M_NETGRAPH_IFACE, M_WAITOK | M_NULLOK | M_ZERO);
5820147868eSNuno Antunes if (ifp == NULL) {
5830147868eSNuno Antunes kfree(priv, M_NETGRAPH_IFACE);
5840147868eSNuno Antunes return (ENOMEM);
5850147868eSNuno Antunes }
5860147868eSNuno Antunes
5870147868eSNuno Antunes /* Link them together */
5880147868eSNuno Antunes ifp->if_softc = priv;
5890147868eSNuno Antunes priv->ifp = ifp;
5900147868eSNuno Antunes
5910147868eSNuno Antunes /* Get an interface unit number */
5920147868eSNuno Antunes if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
59353f03f86SNuno Antunes kfree(ifp, M_NETGRAPH_IFACE);
59453f03f86SNuno Antunes kfree(priv, M_NETGRAPH_IFACE);
5950147868eSNuno Antunes return (error);
5960147868eSNuno Antunes }
5970147868eSNuno Antunes
5980147868eSNuno Antunes
5990147868eSNuno Antunes /* Link together node and private info */
6000147868eSNuno Antunes NG_NODE_SET_PRIVATE(node, priv);
6010147868eSNuno Antunes priv->node = node;
6020147868eSNuno Antunes
6030147868eSNuno Antunes /* Initialize interface structure */
6040147868eSNuno Antunes if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
6050147868eSNuno Antunes ifp->if_output = ng_iface_output;
6060147868eSNuno Antunes ifp->if_start = ng_iface_start;
6070147868eSNuno Antunes ifp->if_ioctl = ng_iface_ioctl;
6080147868eSNuno Antunes ifp->if_watchdog = NULL;
6090147868eSNuno Antunes ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
6100147868eSNuno Antunes ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
6110147868eSNuno Antunes ifp->if_type = IFT_PROPVIRTUAL; /* XXX */
6120147868eSNuno Antunes ifp->if_addrlen = 0; /* XXX */
6130147868eSNuno Antunes ifp->if_hdrlen = 0; /* XXX */
6140147868eSNuno Antunes ifp->if_baudrate = 64000; /* XXX */
6150147868eSNuno Antunes ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
6160147868eSNuno Antunes ifq_set_ready(&ifp->if_snd);
6170147868eSNuno Antunes
6180147868eSNuno Antunes /* Give this node the same name as the interface (if possible) */
6190147868eSNuno Antunes if (ng_name_node(node, ifp->if_xname) != 0)
6200147868eSNuno Antunes log(LOG_WARNING, "%s: can't acquire netgraph name\n",
6210147868eSNuno Antunes ifp->if_xname);
6220147868eSNuno Antunes
6230147868eSNuno Antunes /* Attach the interface */
6240147868eSNuno Antunes if_attach(ifp, NULL);
6250147868eSNuno Antunes bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
6260147868eSNuno Antunes
6270147868eSNuno Antunes /* Done */
6280147868eSNuno Antunes return (0);
6290147868eSNuno Antunes }
6300147868eSNuno Antunes
6310147868eSNuno Antunes /*
6320147868eSNuno Antunes * Give our ok for a hook to be added
6330147868eSNuno Antunes */
6340147868eSNuno Antunes static int
ng_iface_newhook(node_p node,hook_p hook,const char * name)6350147868eSNuno Antunes ng_iface_newhook(node_p node, hook_p hook, const char *name)
6360147868eSNuno Antunes {
6370147868eSNuno Antunes const iffam_p iffam = get_iffam_from_name(name);
6380147868eSNuno Antunes hook_p *hookptr;
6390147868eSNuno Antunes
6400147868eSNuno Antunes if (iffam == NULL)
6410147868eSNuno Antunes return (EPFNOSUPPORT);
6420147868eSNuno Antunes hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
6430147868eSNuno Antunes if (*hookptr != NULL)
6440147868eSNuno Antunes return (EISCONN);
6450147868eSNuno Antunes *hookptr = hook;
6460147868eSNuno Antunes NG_HOOK_HI_STACK(hook);
6470147868eSNuno Antunes return (0);
6480147868eSNuno Antunes }
6490147868eSNuno Antunes
6500147868eSNuno Antunes /*
6510147868eSNuno Antunes * Receive a control message
6520147868eSNuno Antunes */
6530147868eSNuno Antunes static int
ng_iface_rcvmsg(node_p node,item_p item,hook_p lasthook)6540147868eSNuno Antunes ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
6550147868eSNuno Antunes {
6560147868eSNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
6570147868eSNuno Antunes struct ifnet *const ifp = priv->ifp;
6580147868eSNuno Antunes struct ng_mesg *resp = NULL;
6590147868eSNuno Antunes int error = 0;
6600147868eSNuno Antunes struct ng_mesg *msg;
6610147868eSNuno Antunes
6620147868eSNuno Antunes NGI_GET_MSG(item, msg);
6630147868eSNuno Antunes switch (msg->header.typecookie) {
6640147868eSNuno Antunes case NGM_IFACE_COOKIE:
6650147868eSNuno Antunes switch (msg->header.cmd) {
6660147868eSNuno Antunes case NGM_IFACE_GET_IFNAME:
6670147868eSNuno Antunes NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_WAITOK | M_NULLOK);
6680147868eSNuno Antunes if (resp == NULL) {
6690147868eSNuno Antunes error = ENOMEM;
6700147868eSNuno Antunes break;
6710147868eSNuno Antunes }
6720147868eSNuno Antunes strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
6730147868eSNuno Antunes break;
6740147868eSNuno Antunes
6750147868eSNuno Antunes case NGM_IFACE_POINT2POINT:
6760147868eSNuno Antunes case NGM_IFACE_BROADCAST:
6770147868eSNuno Antunes {
6780147868eSNuno Antunes
6790147868eSNuno Antunes /* Deny request if interface is UP */
6800147868eSNuno Antunes if ((ifp->if_flags & IFF_UP) != 0)
6810147868eSNuno Antunes return (EBUSY);
6820147868eSNuno Antunes
6830147868eSNuno Antunes /* Change flags */
6840147868eSNuno Antunes switch (msg->header.cmd) {
6850147868eSNuno Antunes case NGM_IFACE_POINT2POINT:
6860147868eSNuno Antunes ifp->if_flags |= IFF_POINTOPOINT;
6870147868eSNuno Antunes ifp->if_flags &= ~IFF_BROADCAST;
6880147868eSNuno Antunes break;
6890147868eSNuno Antunes case NGM_IFACE_BROADCAST:
6900147868eSNuno Antunes ifp->if_flags &= ~IFF_POINTOPOINT;
6910147868eSNuno Antunes ifp->if_flags |= IFF_BROADCAST;
6920147868eSNuno Antunes break;
6930147868eSNuno Antunes }
6940147868eSNuno Antunes break;
6950147868eSNuno Antunes }
6960147868eSNuno Antunes
6970147868eSNuno Antunes case NGM_IFACE_GET_IFINDEX:
6980147868eSNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_WAITOK | M_NULLOK);
6990147868eSNuno Antunes if (resp == NULL) {
7000147868eSNuno Antunes error = ENOMEM;
7010147868eSNuno Antunes break;
7020147868eSNuno Antunes }
7030147868eSNuno Antunes *((uint32_t *)resp->data) = priv->ifp->if_index;
7040147868eSNuno Antunes break;
7050147868eSNuno Antunes
7060147868eSNuno Antunes default:
7070147868eSNuno Antunes error = EINVAL;
7080147868eSNuno Antunes break;
7090147868eSNuno Antunes }
7100147868eSNuno Antunes break;
7110147868eSNuno Antunes case NGM_CISCO_COOKIE:
7120147868eSNuno Antunes switch (msg->header.cmd) {
7130147868eSNuno Antunes case NGM_CISCO_GET_IPADDR: /* we understand this too */
7140147868eSNuno Antunes {
7150147868eSNuno Antunes struct ifaddr_container *ifac;
7160147868eSNuno Antunes
7170147868eSNuno Antunes /* Return the first configured IP address */
7180147868eSNuno Antunes TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
7190147868eSNuno Antunes struct ifaddr *ifa = ifac->ifa;
7200147868eSNuno Antunes struct ng_cisco_ipaddr *ips;
7210147868eSNuno Antunes
7220147868eSNuno Antunes if (ifa->ifa_addr->sa_family != AF_INET)
7230147868eSNuno Antunes continue;
7240147868eSNuno Antunes NG_MKRESPONSE(resp, msg, sizeof(ips), M_WAITOK | M_NULLOK);
7250147868eSNuno Antunes if (resp == NULL) {
7260147868eSNuno Antunes error = ENOMEM;
7270147868eSNuno Antunes break;
7280147868eSNuno Antunes }
7290147868eSNuno Antunes ips = (struct ng_cisco_ipaddr *)resp->data;
7300147868eSNuno Antunes ips->ipaddr = ((struct sockaddr_in *)
7310147868eSNuno Antunes ifa->ifa_addr)->sin_addr;
7320147868eSNuno Antunes ips->netmask = ((struct sockaddr_in *)
7330147868eSNuno Antunes ifa->ifa_netmask)->sin_addr;
7340147868eSNuno Antunes break;
7350147868eSNuno Antunes }
7360147868eSNuno Antunes
7370147868eSNuno Antunes /* No IP addresses on this interface? */
7380147868eSNuno Antunes if (ifac == NULL)
7390147868eSNuno Antunes error = EADDRNOTAVAIL;
7400147868eSNuno Antunes break;
7410147868eSNuno Antunes }
7420147868eSNuno Antunes default:
7430147868eSNuno Antunes error = EINVAL;
7440147868eSNuno Antunes break;
7450147868eSNuno Antunes }
7460147868eSNuno Antunes break;
7470147868eSNuno Antunes case NGM_FLOW_COOKIE:
7480147868eSNuno Antunes switch (msg->header.cmd) {
7490147868eSNuno Antunes case NGM_LINK_IS_UP:
7500147868eSNuno Antunes ifp->if_flags |= IFF_RUNNING;
7510147868eSNuno Antunes break;
7520147868eSNuno Antunes case NGM_LINK_IS_DOWN:
7530147868eSNuno Antunes ifp->if_flags &= ~IFF_RUNNING;
7540147868eSNuno Antunes break;
7550147868eSNuno Antunes default:
7560147868eSNuno Antunes break;
7570147868eSNuno Antunes }
7580147868eSNuno Antunes break;
7590147868eSNuno Antunes default:
7600147868eSNuno Antunes error = EINVAL;
7610147868eSNuno Antunes break;
7620147868eSNuno Antunes }
7630147868eSNuno Antunes NG_RESPOND_MSG(error, node, item, resp);
7640147868eSNuno Antunes NG_FREE_MSG(msg);
7650147868eSNuno Antunes return (error);
7660147868eSNuno Antunes }
7670147868eSNuno Antunes
7680147868eSNuno Antunes /*
7690147868eSNuno Antunes * Recive data from a hook. Pass the packet to the correct input routine.
7700147868eSNuno Antunes */
7710147868eSNuno Antunes static int
ng_iface_rcvdata(hook_p hook,item_p item)7720147868eSNuno Antunes ng_iface_rcvdata(hook_p hook, item_p item)
7730147868eSNuno Antunes {
7740147868eSNuno Antunes const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
7750147868eSNuno Antunes const iffam_p iffam = get_iffam_from_hook(priv, hook);
7760147868eSNuno Antunes struct ifnet *const ifp = priv->ifp;
7770147868eSNuno Antunes struct mbuf *m;
7780147868eSNuno Antunes int isr;
7790147868eSNuno Antunes
7800147868eSNuno Antunes NGI_GET_M(item, m);
7810147868eSNuno Antunes NG_FREE_ITEM(item);
7820147868eSNuno Antunes /* Sanity checks */
7830147868eSNuno Antunes KASSERT(iffam != NULL, ("%s: iffam", __func__));
7840147868eSNuno Antunes M_ASSERTPKTHDR(m);
7850147868eSNuno Antunes if ((ifp->if_flags & IFF_UP) == 0) {
7860147868eSNuno Antunes NG_FREE_M(m);
7870147868eSNuno Antunes return (ENETDOWN);
7880147868eSNuno Antunes }
7890147868eSNuno Antunes
7900147868eSNuno Antunes /* Update interface stats */
791d40991efSSepherosa Ziehau IFNET_STAT_INC(ifp, ipackets, 1);
792d40991efSSepherosa Ziehau IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
7930147868eSNuno Antunes
7940147868eSNuno Antunes /* Note receiving interface */
7950147868eSNuno Antunes m->m_pkthdr.rcvif = ifp;
7960147868eSNuno Antunes
7970147868eSNuno Antunes /* Berkeley packet filter */
7980147868eSNuno Antunes ng_iface_bpftap(ifp, m, iffam->family);
7990147868eSNuno Antunes
8000147868eSNuno Antunes /* Send packet */
8010147868eSNuno Antunes switch (iffam->family) {
8020147868eSNuno Antunes #ifdef INET
8030147868eSNuno Antunes case AF_INET:
8040147868eSNuno Antunes isr = NETISR_IP;
8050147868eSNuno Antunes break;
8060147868eSNuno Antunes #endif
8070147868eSNuno Antunes #ifdef INET6
8080147868eSNuno Antunes case AF_INET6:
8090147868eSNuno Antunes isr = NETISR_IPV6;
8100147868eSNuno Antunes break;
8110147868eSNuno Antunes #endif
8120147868eSNuno Antunes default:
8130147868eSNuno Antunes m_freem(m);
8140147868eSNuno Antunes return (EAFNOSUPPORT);
8150147868eSNuno Antunes }
8160147868eSNuno Antunes #if 0
8170147868eSNuno Antunes /* First chunk of an mbuf contains good junk */
8180147868eSNuno Antunes if (harvest.point_to_point)
8190147868eSNuno Antunes random_harvest(m, 16, 3, 0, RANDOM_NET);
8200147868eSNuno Antunes #endif
821*8f27f4e5SMarkus Pfeiffer m->m_flags &= ~M_HASH;
8220147868eSNuno Antunes netisr_queue(isr, m);
8230147868eSNuno Antunes return (0);
8240147868eSNuno Antunes }
8250147868eSNuno Antunes
8260147868eSNuno Antunes /*
8270147868eSNuno Antunes * Shutdown and remove the node and its associated interface.
8280147868eSNuno Antunes */
8290147868eSNuno Antunes static int
ng_iface_shutdown(node_p node)8300147868eSNuno Antunes ng_iface_shutdown(node_p node)
8310147868eSNuno Antunes {
8320147868eSNuno Antunes const priv_p priv = NG_NODE_PRIVATE(node);
8330147868eSNuno Antunes
8340147868eSNuno Antunes bpfdetach(priv->ifp);
8350147868eSNuno Antunes if_detach(priv->ifp);
83653f03f86SNuno Antunes kfree(priv->ifp, M_NETGRAPH_IFACE);
8370147868eSNuno Antunes priv->ifp = NULL;
8380147868eSNuno Antunes ng_iface_free_unit(priv->unit);
8390147868eSNuno Antunes kfree(priv, M_NETGRAPH_IFACE);
8400147868eSNuno Antunes NG_NODE_SET_PRIVATE(node, NULL);
8410147868eSNuno Antunes NG_NODE_UNREF(node);
8420147868eSNuno Antunes return (0);
8430147868eSNuno Antunes }
8440147868eSNuno Antunes
8450147868eSNuno Antunes /*
8460147868eSNuno Antunes * Hook disconnection. Note that we do *not* shutdown when all
8470147868eSNuno Antunes * hooks have been disconnected.
8480147868eSNuno Antunes */
8490147868eSNuno Antunes static int
ng_iface_disconnect(hook_p hook)8500147868eSNuno Antunes ng_iface_disconnect(hook_p hook)
8510147868eSNuno Antunes {
8520147868eSNuno Antunes const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
8530147868eSNuno Antunes const iffam_p iffam = get_iffam_from_hook(priv, hook);
8540147868eSNuno Antunes
8550147868eSNuno Antunes if (iffam == NULL)
8560147868eSNuno Antunes panic(__func__);
8570147868eSNuno Antunes *get_hook_from_iffam(priv, iffam) = NULL;
8580147868eSNuno Antunes return (0);
8590147868eSNuno Antunes }
8600147868eSNuno Antunes
8610147868eSNuno Antunes /*
8620147868eSNuno Antunes * Handle loading and unloading for this node type.
8630147868eSNuno Antunes */
8640147868eSNuno Antunes static int
ng_iface_mod_event(module_t mod,int event,void * data)8650147868eSNuno Antunes ng_iface_mod_event(module_t mod, int event, void *data)
8660147868eSNuno Antunes {
8670147868eSNuno Antunes int error = 0;
8680147868eSNuno Antunes
8690147868eSNuno Antunes switch (event) {
8700147868eSNuno Antunes case MOD_LOAD:
8710147868eSNuno Antunes break;
8720147868eSNuno Antunes case MOD_UNLOAD:
8730147868eSNuno Antunes break;
8740147868eSNuno Antunes default:
8750147868eSNuno Antunes error = EOPNOTSUPP;
8760147868eSNuno Antunes break;
8770147868eSNuno Antunes }
8780147868eSNuno Antunes return (error);
8790147868eSNuno Antunes }
880