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