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