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