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