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