ng_iface.c revision 184205
1/*
2 * ng_iface.c
3 */
4
5/*-
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 *    copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 *    Communications, Inc. trademarks, including the mark "WHISTLE
17 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 *    such appears in the above copyright notice or in the software.
19 *
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
37 *
38 * Author: Archie Cobbs <archie@freebsd.org>
39 *
40 * $FreeBSD: head/sys/netgraph/ng_iface.c 184205 2008-10-23 15:53:51Z des $
41 * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
42 */
43
44/*
45 * This node is also a system networking interface. It has
46 * a hook for each protocol (IP, AppleTalk, IPX, etc). Packets
47 * are simply relayed between the interface and the hooks.
48 *
49 * Interfaces are named ng0, ng1, etc.  New nodes take the
50 * first available interface name.
51 *
52 * This node also includes Berkeley packet filter support.
53 */
54
55#include "opt_atalk.h"
56#include "opt_inet.h"
57#include "opt_inet6.h"
58#include "opt_ipx.h"
59
60#include <sys/param.h>
61#include <sys/systm.h>
62#include <sys/errno.h>
63#include <sys/kernel.h>
64#include <sys/malloc.h>
65#include <sys/mbuf.h>
66#include <sys/errno.h>
67#include <sys/random.h>
68#include <sys/sockio.h>
69#include <sys/socket.h>
70#include <sys/syslog.h>
71#include <sys/libkern.h>
72#include <sys/vimage.h>
73
74#include <net/if.h>
75#include <net/if_types.h>
76#include <net/bpf.h>
77#include <net/netisr.h>
78
79#include <netinet/in.h>
80
81#include <netgraph/ng_message.h>
82#include <netgraph/netgraph.h>
83#include <netgraph/ng_parse.h>
84#include <netgraph/ng_iface.h>
85#include <netgraph/ng_cisco.h>
86
87#ifdef NG_SEPARATE_MALLOC
88MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
89#else
90#define M_NETGRAPH_IFACE M_NETGRAPH
91#endif
92
93/* This struct describes one address family */
94struct iffam {
95	sa_family_t	family;		/* Address family */
96	const char	*hookname;	/* Name for hook */
97};
98typedef const struct iffam *iffam_p;
99
100/* List of address families supported by our interface */
101const static struct iffam gFamilies[] = {
102	{ AF_INET,	NG_IFACE_HOOK_INET	},
103	{ AF_INET6,	NG_IFACE_HOOK_INET6	},
104	{ AF_APPLETALK,	NG_IFACE_HOOK_ATALK	},
105	{ AF_IPX,	NG_IFACE_HOOK_IPX	},
106	{ AF_ATM,	NG_IFACE_HOOK_ATM	},
107	{ AF_NATM,	NG_IFACE_HOOK_NATM	},
108};
109#define NUM_FAMILIES		(sizeof(gFamilies) / sizeof(*gFamilies))
110
111/* Node private data */
112struct ng_iface_private {
113	struct	ifnet *ifp;		/* Our interface */
114	int	unit;			/* Interface unit number */
115	node_p	node;			/* Our netgraph node */
116	hook_p	hooks[NUM_FAMILIES];	/* Hook for each address family */
117};
118typedef struct ng_iface_private *priv_p;
119
120/* Interface methods */
121static void	ng_iface_start(struct ifnet *ifp);
122static int	ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
123static int	ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
124			struct sockaddr *dst, struct rtentry *rt0);
125static void	ng_iface_bpftap(struct ifnet *ifp,
126			struct mbuf *m, sa_family_t family);
127static int	ng_iface_send(struct ifnet *ifp, struct mbuf *m,
128			sa_family_t sa);
129#ifdef DEBUG
130static void	ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
131#endif
132
133/* Netgraph methods */
134static int		ng_iface_mod_event(module_t, int, void *);
135static ng_constructor_t	ng_iface_constructor;
136static ng_rcvmsg_t	ng_iface_rcvmsg;
137static ng_shutdown_t	ng_iface_shutdown;
138static ng_newhook_t	ng_iface_newhook;
139static ng_rcvdata_t	ng_iface_rcvdata;
140static ng_disconnect_t	ng_iface_disconnect;
141
142/* Helper stuff */
143static iffam_p	get_iffam_from_af(sa_family_t family);
144static iffam_p	get_iffam_from_hook(priv_p priv, hook_p hook);
145static iffam_p	get_iffam_from_name(const char *name);
146static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
147
148/* Parse type for struct ng_cisco_ipaddr */
149static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
150	= NG_CISCO_IPADDR_TYPE_INFO;
151static const struct ng_parse_type ng_cisco_ipaddr_type = {
152	&ng_parse_struct_type,
153	&ng_cisco_ipaddr_type_fields
154};
155
156/* List of commands and how to convert arguments to/from ASCII */
157static const struct ng_cmdlist ng_iface_cmds[] = {
158	{
159	  NGM_IFACE_COOKIE,
160	  NGM_IFACE_GET_IFNAME,
161	  "getifname",
162	  NULL,
163	  &ng_parse_string_type
164	},
165	{
166	  NGM_IFACE_COOKIE,
167	  NGM_IFACE_POINT2POINT,
168	  "point2point",
169	  NULL,
170	  NULL
171	},
172	{
173	  NGM_IFACE_COOKIE,
174	  NGM_IFACE_BROADCAST,
175	  "broadcast",
176	  NULL,
177	  NULL
178	},
179	{
180	  NGM_CISCO_COOKIE,
181	  NGM_CISCO_GET_IPADDR,
182	  "getipaddr",
183	  NULL,
184	  &ng_cisco_ipaddr_type
185	},
186	{
187	  NGM_IFACE_COOKIE,
188	  NGM_IFACE_GET_IFINDEX,
189	  "getifindex",
190	  NULL,
191	  &ng_parse_uint32_type
192	},
193	{ 0 }
194};
195
196/* Node type descriptor */
197static struct ng_type typestruct = {
198	.version =	NG_ABI_VERSION,
199	.name =		NG_IFACE_NODE_TYPE,
200	.mod_event =	ng_iface_mod_event,
201	.constructor =	ng_iface_constructor,
202	.rcvmsg =	ng_iface_rcvmsg,
203	.shutdown =	ng_iface_shutdown,
204	.newhook =	ng_iface_newhook,
205	.rcvdata =	ng_iface_rcvdata,
206	.disconnect =	ng_iface_disconnect,
207	.cmdlist =	ng_iface_cmds,
208};
209NETGRAPH_INIT(iface, &typestruct);
210
211static struct unrhdr	*ng_iface_unit;
212
213/************************************************************************
214			HELPER STUFF
215 ************************************************************************/
216
217/*
218 * Get the family descriptor from the family ID
219 */
220static __inline iffam_p
221get_iffam_from_af(sa_family_t family)
222{
223	iffam_p iffam;
224	int k;
225
226	for (k = 0; k < NUM_FAMILIES; k++) {
227		iffam = &gFamilies[k];
228		if (iffam->family == family)
229			return (iffam);
230	}
231	return (NULL);
232}
233
234/*
235 * Get the family descriptor from the hook
236 */
237static __inline iffam_p
238get_iffam_from_hook(priv_p priv, hook_p hook)
239{
240	int k;
241
242	for (k = 0; k < NUM_FAMILIES; k++)
243		if (priv->hooks[k] == hook)
244			return (&gFamilies[k]);
245	return (NULL);
246}
247
248/*
249 * Get the hook from the iffam descriptor
250 */
251
252static __inline hook_p *
253get_hook_from_iffam(priv_p priv, iffam_p iffam)
254{
255	return (&priv->hooks[iffam - gFamilies]);
256}
257
258/*
259 * Get the iffam descriptor from the name
260 */
261static __inline iffam_p
262get_iffam_from_name(const char *name)
263{
264	iffam_p iffam;
265	int k;
266
267	for (k = 0; k < NUM_FAMILIES; k++) {
268		iffam = &gFamilies[k];
269		if (!strcmp(iffam->hookname, name))
270			return (iffam);
271	}
272	return (NULL);
273}
274
275/************************************************************************
276			INTERFACE STUFF
277 ************************************************************************/
278
279/*
280 * Process an ioctl for the virtual interface
281 */
282static int
283ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
284{
285	struct ifreq *const ifr = (struct ifreq *) data;
286	int s, error = 0;
287
288#ifdef DEBUG
289	ng_iface_print_ioctl(ifp, command, data);
290#endif
291	s = splimp();
292	switch (command) {
293
294	/* These two are mostly handled at a higher layer */
295	case SIOCSIFADDR:
296		ifp->if_flags |= IFF_UP;
297		ifp->if_drv_flags |= IFF_DRV_RUNNING;
298		ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
299		break;
300	case SIOCGIFADDR:
301		break;
302
303	/* Set flags */
304	case SIOCSIFFLAGS:
305		/*
306		 * If the interface is marked up and stopped, then start it.
307		 * If it is marked down and running, then stop it.
308		 */
309		if (ifr->ifr_flags & IFF_UP) {
310			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
311				ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
312				ifp->if_drv_flags |= IFF_DRV_RUNNING;
313			}
314		} else {
315			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
316				ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
317				    IFF_DRV_OACTIVE);
318		}
319		break;
320
321	/* Set the interface MTU */
322	case SIOCSIFMTU:
323		if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
324		    || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
325			error = EINVAL;
326		else
327			ifp->if_mtu = ifr->ifr_mtu;
328		break;
329
330	/* Stuff that's not supported */
331	case SIOCADDMULTI:
332	case SIOCDELMULTI:
333		error = 0;
334		break;
335	case SIOCSIFPHYS:
336		error = EOPNOTSUPP;
337		break;
338
339	default:
340		error = EINVAL;
341		break;
342	}
343	(void) splx(s);
344	return (error);
345}
346
347/*
348 * This routine is called to deliver a packet out the interface.
349 * We simply look at the address family and relay the packet to
350 * the corresponding hook, if it exists and is connected.
351 */
352
353static int
354ng_iface_output(struct ifnet *ifp, struct mbuf *m,
355		struct sockaddr *dst, struct rtentry *rt0)
356{
357	uint32_t af;
358	int error;
359
360	/* Check interface flags */
361	if (!((ifp->if_flags & IFF_UP) &&
362	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
363		m_freem(m);
364		return (ENETDOWN);
365	}
366
367	/* BPF writes need to be handled specially. */
368	if (dst->sa_family == AF_UNSPEC) {
369		bcopy(dst->sa_data, &af, sizeof(af));
370		dst->sa_family = af;
371	}
372
373	/* Berkeley packet filter */
374	ng_iface_bpftap(ifp, m, dst->sa_family);
375
376	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
377		M_PREPEND(m, sizeof(sa_family_t), M_DONTWAIT);
378		if (m == NULL) {
379			IFQ_LOCK(&ifp->if_snd);
380			IFQ_INC_DROPS(&ifp->if_snd);
381			IFQ_UNLOCK(&ifp->if_snd);
382			ifp->if_oerrors++;
383			return (ENOBUFS);
384		}
385		*(sa_family_t *)m->m_data = dst->sa_family;
386		IFQ_HANDOFF(ifp, m, error);
387	} else
388		error = ng_iface_send(ifp, m, dst->sa_family);
389
390	return (error);
391}
392
393/*
394 * Start method is used only when ALTQ is enabled.
395 */
396static void
397ng_iface_start(struct ifnet *ifp)
398{
399	struct mbuf *m;
400	sa_family_t sa;
401
402	KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
403
404	for(;;) {
405		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
406		if (m == NULL)
407			break;
408		sa = *mtod(m, sa_family_t *);
409		m_adj(m, sizeof(sa_family_t));
410		ng_iface_send(ifp, m, sa);
411	}
412}
413
414/*
415 * Flash a packet by the BPF (requires prepending 4 byte AF header)
416 * Note the phoney mbuf; this is OK because BPF treats it read-only.
417 */
418static void
419ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
420{
421	KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
422	if (bpf_peers_present(ifp->if_bpf)) {
423		int32_t family4 = (int32_t)family;
424		bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
425	}
426}
427
428/*
429 * This routine does actual delivery of the packet into the
430 * netgraph(4). It is called from ng_iface_start() and
431 * ng_iface_output().
432 */
433static int
434ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
435{
436	const priv_p priv = (priv_p) ifp->if_softc;
437	const iffam_p iffam = get_iffam_from_af(sa);
438	int error;
439	int len;
440
441	/* Check address family to determine hook (if known) */
442	if (iffam == NULL) {
443		m_freem(m);
444		log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
445		return (EAFNOSUPPORT);
446	}
447
448	/* Copy length before the mbuf gets invalidated. */
449	len = m->m_pkthdr.len;
450
451	/* Send packet. If hook is not connected,
452	   mbuf will get freed. */
453	NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
454
455	/* Update stats. */
456	if (error == 0) {
457		ifp->if_obytes += len;
458		ifp->if_opackets++;
459	}
460
461	return (error);
462}
463
464#ifdef DEBUG
465/*
466 * Display an ioctl to the virtual interface
467 */
468
469static void
470ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
471{
472	char   *str;
473
474	switch (command & IOC_DIRMASK) {
475	case IOC_VOID:
476		str = "IO";
477		break;
478	case IOC_OUT:
479		str = "IOR";
480		break;
481	case IOC_IN:
482		str = "IOW";
483		break;
484	case IOC_INOUT:
485		str = "IORW";
486		break;
487	default:
488		str = "IO??";
489	}
490	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
491	       ifp->if_xname,
492	       str,
493	       IOCGROUP(command),
494	       command & 0xff,
495	       IOCPARM_LEN(command));
496}
497#endif /* DEBUG */
498
499/************************************************************************
500			NETGRAPH NODE STUFF
501 ************************************************************************/
502
503/*
504 * Constructor for a node
505 */
506static int
507ng_iface_constructor(node_p node)
508{
509	INIT_VNET_NETGRAPH(curvnet);
510	struct ifnet *ifp;
511	priv_p priv;
512
513	/* Allocate node and interface private structures */
514	priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
515	if (priv == NULL)
516		return (ENOMEM);
517	ifp = if_alloc(IFT_PROPVIRTUAL);
518	if (ifp == NULL) {
519		free(priv, M_NETGRAPH_IFACE);
520		return (ENOMEM);
521	}
522
523	/* Link them together */
524	ifp->if_softc = priv;
525	priv->ifp = ifp;
526
527	/* Get an interface unit number */
528	priv->unit = alloc_unr(V_ng_iface_unit);
529
530	/* Link together node and private info */
531	NG_NODE_SET_PRIVATE(node, priv);
532	priv->node = node;
533
534	/* Initialize interface structure */
535	if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
536	ifp->if_output = ng_iface_output;
537	ifp->if_start = ng_iface_start;
538	ifp->if_ioctl = ng_iface_ioctl;
539	ifp->if_watchdog = NULL;
540	ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
541	ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
542	ifp->if_type = IFT_PROPVIRTUAL;		/* XXX */
543	ifp->if_addrlen = 0;			/* XXX */
544	ifp->if_hdrlen = 0;			/* XXX */
545	ifp->if_baudrate = 64000;		/* XXX */
546	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
547	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
548	IFQ_SET_READY(&ifp->if_snd);
549
550	/* Give this node the same name as the interface (if possible) */
551	if (ng_name_node(node, ifp->if_xname) != 0)
552		log(LOG_WARNING, "%s: can't acquire netgraph name\n",
553		    ifp->if_xname);
554
555	/* Attach the interface */
556	if_attach(ifp);
557	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
558
559	/* Done */
560	return (0);
561}
562
563/*
564 * Give our ok for a hook to be added
565 */
566static int
567ng_iface_newhook(node_p node, hook_p hook, const char *name)
568{
569	const iffam_p iffam = get_iffam_from_name(name);
570	hook_p *hookptr;
571
572	if (iffam == NULL)
573		return (EPFNOSUPPORT);
574	hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
575	if (*hookptr != NULL)
576		return (EISCONN);
577	*hookptr = hook;
578	NG_HOOK_HI_STACK(hook);
579	return (0);
580}
581
582/*
583 * Receive a control message
584 */
585static int
586ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
587{
588	const priv_p priv = NG_NODE_PRIVATE(node);
589	struct ifnet *const ifp = priv->ifp;
590	struct ng_mesg *resp = NULL;
591	int error = 0;
592	struct ng_mesg *msg;
593
594	NGI_GET_MSG(item, msg);
595	switch (msg->header.typecookie) {
596	case NGM_IFACE_COOKIE:
597		switch (msg->header.cmd) {
598		case NGM_IFACE_GET_IFNAME:
599			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
600			if (resp == NULL) {
601				error = ENOMEM;
602				break;
603			}
604			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
605			break;
606
607		case NGM_IFACE_POINT2POINT:
608		case NGM_IFACE_BROADCAST:
609		    {
610
611			/* Deny request if interface is UP */
612			if ((ifp->if_flags & IFF_UP) != 0)
613				return (EBUSY);
614
615			/* Change flags */
616			switch (msg->header.cmd) {
617			case NGM_IFACE_POINT2POINT:
618				ifp->if_flags |= IFF_POINTOPOINT;
619				ifp->if_flags &= ~IFF_BROADCAST;
620				break;
621			case NGM_IFACE_BROADCAST:
622				ifp->if_flags &= ~IFF_POINTOPOINT;
623				ifp->if_flags |= IFF_BROADCAST;
624				break;
625			}
626			break;
627		    }
628
629		case NGM_IFACE_GET_IFINDEX:
630			NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
631			if (resp == NULL) {
632				error = ENOMEM;
633				break;
634			}
635			*((uint32_t *)resp->data) = priv->ifp->if_index;
636			break;
637
638		default:
639			error = EINVAL;
640			break;
641		}
642		break;
643	case NGM_CISCO_COOKIE:
644		switch (msg->header.cmd) {
645		case NGM_CISCO_GET_IPADDR:	/* we understand this too */
646		    {
647			struct ifaddr *ifa;
648
649			/* Return the first configured IP address */
650			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
651				struct ng_cisco_ipaddr *ips;
652
653				if (ifa->ifa_addr->sa_family != AF_INET)
654					continue;
655				NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
656				if (resp == NULL) {
657					error = ENOMEM;
658					break;
659				}
660				ips = (struct ng_cisco_ipaddr *)resp->data;
661				ips->ipaddr = ((struct sockaddr_in *)
662						ifa->ifa_addr)->sin_addr;
663				ips->netmask = ((struct sockaddr_in *)
664						ifa->ifa_netmask)->sin_addr;
665				break;
666			}
667
668			/* No IP addresses on this interface? */
669			if (ifa == NULL)
670				error = EADDRNOTAVAIL;
671			break;
672		    }
673		default:
674			error = EINVAL;
675			break;
676		}
677		break;
678	case NGM_FLOW_COOKIE:
679		switch (msg->header.cmd) {
680		case NGM_LINK_IS_UP:
681			ifp->if_drv_flags |= IFF_DRV_RUNNING;
682			break;
683		case NGM_LINK_IS_DOWN:
684			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
685			break;
686		default:
687			break;
688		}
689		break;
690	default:
691		error = EINVAL;
692		break;
693	}
694	NG_RESPOND_MSG(error, node, item, resp);
695	NG_FREE_MSG(msg);
696	return (error);
697}
698
699/*
700 * Recive data from a hook. Pass the packet to the correct input routine.
701 */
702static int
703ng_iface_rcvdata(hook_p hook, item_p item)
704{
705	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
706	const iffam_p iffam = get_iffam_from_hook(priv, hook);
707	struct ifnet *const ifp = priv->ifp;
708	struct mbuf *m;
709	int isr;
710
711	NGI_GET_M(item, m);
712	NG_FREE_ITEM(item);
713	/* Sanity checks */
714	KASSERT(iffam != NULL, ("%s: iffam", __func__));
715	M_ASSERTPKTHDR(m);
716	if ((ifp->if_flags & IFF_UP) == 0) {
717		NG_FREE_M(m);
718		return (ENETDOWN);
719	}
720
721	/* Update interface stats */
722	ifp->if_ipackets++;
723	ifp->if_ibytes += m->m_pkthdr.len;
724
725	/* Note receiving interface */
726	m->m_pkthdr.rcvif = ifp;
727
728	/* Berkeley packet filter */
729	ng_iface_bpftap(ifp, m, iffam->family);
730
731	/* Send packet */
732	switch (iffam->family) {
733#ifdef INET
734	case AF_INET:
735		isr = NETISR_IP;
736		break;
737#endif
738#ifdef INET6
739	case AF_INET6:
740		isr = NETISR_IPV6;
741		break;
742#endif
743#ifdef IPX
744	case AF_IPX:
745		isr = NETISR_IPX;
746		break;
747#endif
748#ifdef NETATALK
749	case AF_APPLETALK:
750		isr = NETISR_ATALK2;
751		break;
752#endif
753	default:
754		m_freem(m);
755		return (EAFNOSUPPORT);
756	}
757	/* First chunk of an mbuf contains good junk */
758	if (harvest.point_to_point)
759		random_harvest(m, 16, 3, 0, RANDOM_NET);
760	netisr_dispatch(isr, m);
761	return (0);
762}
763
764/*
765 * Shutdown and remove the node and its associated interface.
766 */
767static int
768ng_iface_shutdown(node_p node)
769{
770	INIT_VNET_NETGRAPH(curvnet);
771	const priv_p priv = NG_NODE_PRIVATE(node);
772
773	/*
774	 * The ifnet may be in a different vnet than the netgraph node,
775	 * hence we have to change the current vnet context here.
776	 */
777	CURVNET_SET_QUIET(priv->ifp->if_vnet);
778	bpfdetach(priv->ifp);
779	if_detach(priv->ifp);
780	if_free(priv->ifp);
781	CURVNET_RESTORE();
782	priv->ifp = NULL;
783	free_unr(V_ng_iface_unit, priv->unit);
784	free(priv, M_NETGRAPH_IFACE);
785	NG_NODE_SET_PRIVATE(node, NULL);
786	NG_NODE_UNREF(node);
787	return (0);
788}
789
790/*
791 * Hook disconnection. Note that we do *not* shutdown when all
792 * hooks have been disconnected.
793 */
794static int
795ng_iface_disconnect(hook_p hook)
796{
797	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
798	const iffam_p iffam = get_iffam_from_hook(priv, hook);
799
800	if (iffam == NULL)
801		panic(__func__);
802	*get_hook_from_iffam(priv, iffam) = NULL;
803	return (0);
804}
805
806/*
807 * Handle loading and unloading for this node type.
808 */
809static int
810ng_iface_mod_event(module_t mod, int event, void *data)
811{
812	int error = 0;
813
814	switch (event) {
815	case MOD_LOAD:
816		V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
817		break;
818	case MOD_UNLOAD:
819		delete_unrhdr(V_ng_iface_unit);
820		break;
821	default:
822		error = EOPNOTSUPP;
823		break;
824	}
825	return (error);
826}
827