xref: /freebsd/sys/netgraph/ng_iface.c (revision 5861f9665471e98e544f6fa3ce73c4912229ff82)
1 /*
2  * ng_iface.c
3  */
4 
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  *
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  *
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * Author: Archie Cobbs <archie@freebsd.org>
39  *
40  * $FreeBSD$
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/random.h>
68 #include <sys/sockio.h>
69 #include <sys/socket.h>
70 #include <sys/syslog.h>
71 #include <sys/libkern.h>
72 #include <sys/vimage.h>
73 
74 #include <net/if.h>
75 #include <net/if_types.h>
76 #include <net/bpf.h>
77 #include <net/netisr.h>
78 #include <net/route.h>
79 
80 #include <netinet/in.h>
81 
82 #include <netgraph/ng_message.h>
83 #include <netgraph/netgraph.h>
84 #include <netgraph/ng_parse.h>
85 #include <netgraph/ng_iface.h>
86 #include <netgraph/ng_cisco.h>
87 
88 #ifdef NG_SEPARATE_MALLOC
89 MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node ");
90 #else
91 #define M_NETGRAPH_IFACE M_NETGRAPH
92 #endif
93 
94 /* This struct describes one address family */
95 struct iffam {
96 	sa_family_t	family;		/* Address family */
97 	const char	*hookname;	/* Name for hook */
98 };
99 typedef const struct iffam *iffam_p;
100 
101 /* List of address families supported by our interface */
102 const static struct iffam gFamilies[] = {
103 	{ AF_INET,	NG_IFACE_HOOK_INET	},
104 	{ AF_INET6,	NG_IFACE_HOOK_INET6	},
105 	{ AF_APPLETALK,	NG_IFACE_HOOK_ATALK	},
106 	{ AF_IPX,	NG_IFACE_HOOK_IPX	},
107 	{ AF_ATM,	NG_IFACE_HOOK_ATM	},
108 	{ AF_NATM,	NG_IFACE_HOOK_NATM	},
109 };
110 #define NUM_FAMILIES		(sizeof(gFamilies) / sizeof(*gFamilies))
111 
112 /* Node private data */
113 struct ng_iface_private {
114 	struct	ifnet *ifp;		/* Our interface */
115 	int	unit;			/* Interface unit number */
116 	node_p	node;			/* Our netgraph node */
117 	hook_p	hooks[NUM_FAMILIES];	/* Hook for each address family */
118 };
119 typedef struct ng_iface_private *priv_p;
120 
121 /* Interface methods */
122 static void	ng_iface_start(struct ifnet *ifp);
123 static int	ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
124 static int	ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
125     			struct sockaddr *dst, struct route *ro);
126 static void	ng_iface_bpftap(struct ifnet *ifp,
127 			struct mbuf *m, sa_family_t family);
128 static int	ng_iface_send(struct ifnet *ifp, struct mbuf *m,
129 			sa_family_t sa);
130 #ifdef DEBUG
131 static void	ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
132 #endif
133 
134 /* Netgraph methods */
135 static int		ng_iface_mod_event(module_t, int, void *);
136 static ng_constructor_t	ng_iface_constructor;
137 static ng_rcvmsg_t	ng_iface_rcvmsg;
138 static ng_shutdown_t	ng_iface_shutdown;
139 static ng_newhook_t	ng_iface_newhook;
140 static ng_rcvdata_t	ng_iface_rcvdata;
141 static ng_disconnect_t	ng_iface_disconnect;
142 
143 /* Helper stuff */
144 static iffam_p	get_iffam_from_af(sa_family_t family);
145 static iffam_p	get_iffam_from_hook(priv_p priv, hook_p hook);
146 static iffam_p	get_iffam_from_name(const char *name);
147 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
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_parse_string_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 	{
188 	  NGM_IFACE_COOKIE,
189 	  NGM_IFACE_GET_IFINDEX,
190 	  "getifindex",
191 	  NULL,
192 	  &ng_parse_uint32_type
193 	},
194 	{ 0 }
195 };
196 
197 /* Node type descriptor */
198 static struct ng_type typestruct = {
199 	.version =	NG_ABI_VERSION,
200 	.name =		NG_IFACE_NODE_TYPE,
201 	.mod_event =	ng_iface_mod_event,
202 	.constructor =	ng_iface_constructor,
203 	.rcvmsg =	ng_iface_rcvmsg,
204 	.shutdown =	ng_iface_shutdown,
205 	.newhook =	ng_iface_newhook,
206 	.rcvdata =	ng_iface_rcvdata,
207 	.disconnect =	ng_iface_disconnect,
208 	.cmdlist =	ng_iface_cmds,
209 };
210 NETGRAPH_INIT(iface, &typestruct);
211 
212 static vnet_attach_fn ng_iface_iattach;
213 static vnet_detach_fn ng_iface_idetach;
214 
215 #ifdef VIMAGE_GLOBALS
216 static struct unrhdr	*ng_iface_unit;
217 #endif
218 
219 #ifndef VIMAGE_GLOBALS
220 static vnet_modinfo_t vnet_ng_iface_modinfo = {
221 	.vmi_id		= VNET_MOD_NG_IFACE,
222 	.vmi_name	= "ng_iface",
223 	.vmi_dependson	= VNET_MOD_NETGRAPH,
224 	.vmi_iattach	= ng_iface_iattach,
225 	.vmi_idetach	= ng_iface_idetach
226 };
227 #endif
228 
229 /************************************************************************
230 			HELPER STUFF
231  ************************************************************************/
232 
233 /*
234  * Get the family descriptor from the family ID
235  */
236 static __inline iffam_p
237 get_iffam_from_af(sa_family_t family)
238 {
239 	iffam_p iffam;
240 	int k;
241 
242 	for (k = 0; k < NUM_FAMILIES; k++) {
243 		iffam = &gFamilies[k];
244 		if (iffam->family == family)
245 			return (iffam);
246 	}
247 	return (NULL);
248 }
249 
250 /*
251  * Get the family descriptor from the hook
252  */
253 static __inline iffam_p
254 get_iffam_from_hook(priv_p priv, hook_p hook)
255 {
256 	int k;
257 
258 	for (k = 0; k < NUM_FAMILIES; k++)
259 		if (priv->hooks[k] == hook)
260 			return (&gFamilies[k]);
261 	return (NULL);
262 }
263 
264 /*
265  * Get the hook from the iffam descriptor
266  */
267 
268 static __inline hook_p *
269 get_hook_from_iffam(priv_p priv, iffam_p iffam)
270 {
271 	return (&priv->hooks[iffam - gFamilies]);
272 }
273 
274 /*
275  * Get the iffam descriptor from the name
276  */
277 static __inline iffam_p
278 get_iffam_from_name(const char *name)
279 {
280 	iffam_p iffam;
281 	int k;
282 
283 	for (k = 0; k < NUM_FAMILIES; k++) {
284 		iffam = &gFamilies[k];
285 		if (!strcmp(iffam->hookname, name))
286 			return (iffam);
287 	}
288 	return (NULL);
289 }
290 
291 /************************************************************************
292 			INTERFACE STUFF
293  ************************************************************************/
294 
295 /*
296  * Process an ioctl for the virtual interface
297  */
298 static int
299 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
300 {
301 	struct ifreq *const ifr = (struct ifreq *) data;
302 	int s, error = 0;
303 
304 #ifdef DEBUG
305 	ng_iface_print_ioctl(ifp, command, data);
306 #endif
307 	s = splimp();
308 	switch (command) {
309 
310 	/* These two are mostly handled at a higher layer */
311 	case SIOCSIFADDR:
312 		ifp->if_flags |= IFF_UP;
313 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
314 		ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
315 		break;
316 	case SIOCGIFADDR:
317 		break;
318 
319 	/* Set flags */
320 	case SIOCSIFFLAGS:
321 		/*
322 		 * If the interface is marked up and stopped, then start it.
323 		 * If it is marked down and running, then stop it.
324 		 */
325 		if (ifr->ifr_flags & IFF_UP) {
326 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
327 				ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
328 				ifp->if_drv_flags |= IFF_DRV_RUNNING;
329 			}
330 		} else {
331 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
332 				ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
333 				    IFF_DRV_OACTIVE);
334 		}
335 		break;
336 
337 	/* Set the interface MTU */
338 	case SIOCSIFMTU:
339 		if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
340 		    || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
341 			error = EINVAL;
342 		else
343 			ifp->if_mtu = ifr->ifr_mtu;
344 		break;
345 
346 	/* Stuff that's not supported */
347 	case SIOCADDMULTI:
348 	case SIOCDELMULTI:
349 		error = 0;
350 		break;
351 	case SIOCSIFPHYS:
352 		error = EOPNOTSUPP;
353 		break;
354 
355 	default:
356 		error = EINVAL;
357 		break;
358 	}
359 	(void) splx(s);
360 	return (error);
361 }
362 
363 /*
364  * This routine is called to deliver a packet out the interface.
365  * We simply look at the address family and relay the packet to
366  * the corresponding hook, if it exists and is connected.
367  */
368 
369 static int
370 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
371     		struct sockaddr *dst, struct route *ro)
372 {
373 	struct m_tag *mtag;
374 	uint32_t af;
375 	int error;
376 
377 	/* Check interface flags */
378 	if (!((ifp->if_flags & IFF_UP) &&
379 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
380 		m_freem(m);
381 		return (ENETDOWN);
382 	}
383 
384 	/* Protect from deadly infinite recursion. */
385 	mtag = NULL;
386 	while ((mtag = m_tag_locate(m, MTAG_NGIF, MTAG_NGIF_CALLED, mtag))) {
387 		if (*(struct ifnet **)(mtag + 1) == ifp) {
388 			log(LOG_NOTICE, "Loop detected on %s\n", ifp->if_xname);
389 			m_freem(m);
390 			return (EDEADLK);
391 		}
392 	}
393 	mtag = m_tag_alloc(MTAG_NGIF, MTAG_NGIF_CALLED, sizeof(struct ifnet *),
394 	    M_NOWAIT);
395 	if (mtag == NULL) {
396 		m_freem(m);
397 		return (ENOMEM);
398 	}
399 	*(struct ifnet **)(mtag + 1) = ifp;
400 	m_tag_prepend(m, mtag);
401 
402 	/* BPF writes need to be handled specially. */
403 	if (dst->sa_family == AF_UNSPEC) {
404 		bcopy(dst->sa_data, &af, sizeof(af));
405 		dst->sa_family = af;
406 	}
407 
408 	/* Berkeley packet filter */
409 	ng_iface_bpftap(ifp, m, dst->sa_family);
410 
411 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
412 		M_PREPEND(m, sizeof(sa_family_t), M_DONTWAIT);
413 		if (m == NULL) {
414 			IFQ_LOCK(&ifp->if_snd);
415 			IFQ_INC_DROPS(&ifp->if_snd);
416 			IFQ_UNLOCK(&ifp->if_snd);
417 			ifp->if_oerrors++;
418 			return (ENOBUFS);
419 		}
420 		*(sa_family_t *)m->m_data = dst->sa_family;
421 		error = (ifp->if_transmit)(ifp, m);
422 	} else
423 		error = ng_iface_send(ifp, m, dst->sa_family);
424 
425 	return (error);
426 }
427 
428 /*
429  * Start method is used only when ALTQ is enabled.
430  */
431 static void
432 ng_iface_start(struct ifnet *ifp)
433 {
434 	struct mbuf *m;
435 	sa_family_t sa;
436 
437 	KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
438 
439 	for(;;) {
440 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
441 		if (m == NULL)
442 			break;
443 		sa = *mtod(m, sa_family_t *);
444 		m_adj(m, sizeof(sa_family_t));
445 		ng_iface_send(ifp, m, sa);
446 	}
447 }
448 
449 /*
450  * Flash a packet by the BPF (requires prepending 4 byte AF header)
451  * Note the phoney mbuf; this is OK because BPF treats it read-only.
452  */
453 static void
454 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
455 {
456 	KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
457 	if (bpf_peers_present(ifp->if_bpf)) {
458 		int32_t family4 = (int32_t)family;
459 		bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
460 	}
461 }
462 
463 /*
464  * This routine does actual delivery of the packet into the
465  * netgraph(4). It is called from ng_iface_start() and
466  * ng_iface_output().
467  */
468 static int
469 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
470 {
471 	const priv_p priv = (priv_p) ifp->if_softc;
472 	const iffam_p iffam = get_iffam_from_af(sa);
473 	int error;
474 	int len;
475 
476 	/* Check address family to determine hook (if known) */
477 	if (iffam == NULL) {
478 		m_freem(m);
479 		log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
480 		return (EAFNOSUPPORT);
481 	}
482 
483 	/* Copy length before the mbuf gets invalidated. */
484 	len = m->m_pkthdr.len;
485 
486 	/* Send packet. If hook is not connected, mbuf will get freed. */
487 	NG_OUTBOUND_THREAD_REF();
488 	NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
489 	NG_OUTBOUND_THREAD_UNREF();
490 
491 	/* Update stats. */
492 	if (error == 0) {
493 		ifp->if_obytes += len;
494 		ifp->if_opackets++;
495 	}
496 
497 	return (error);
498 }
499 
500 #ifdef DEBUG
501 /*
502  * Display an ioctl to the virtual interface
503  */
504 
505 static void
506 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
507 {
508 	char   *str;
509 
510 	switch (command & IOC_DIRMASK) {
511 	case IOC_VOID:
512 		str = "IO";
513 		break;
514 	case IOC_OUT:
515 		str = "IOR";
516 		break;
517 	case IOC_IN:
518 		str = "IOW";
519 		break;
520 	case IOC_INOUT:
521 		str = "IORW";
522 		break;
523 	default:
524 		str = "IO??";
525 	}
526 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
527 	       ifp->if_xname,
528 	       str,
529 	       IOCGROUP(command),
530 	       command & 0xff,
531 	       IOCPARM_LEN(command));
532 }
533 #endif /* DEBUG */
534 
535 /************************************************************************
536 			NETGRAPH NODE STUFF
537  ************************************************************************/
538 
539 /*
540  * Constructor for a node
541  */
542 static int
543 ng_iface_constructor(node_p node)
544 {
545 	INIT_VNET_NETGRAPH(curvnet);
546 	struct ifnet *ifp;
547 	priv_p priv;
548 
549 	/* Allocate node and interface private structures */
550 	priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
551 	if (priv == NULL)
552 		return (ENOMEM);
553 	ifp = if_alloc(IFT_PROPVIRTUAL);
554 	if (ifp == NULL) {
555 		free(priv, M_NETGRAPH_IFACE);
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 	priv->unit = alloc_unr(V_ng_iface_unit);
565 
566 	/* Link together node and private info */
567 	NG_NODE_SET_PRIVATE(node, priv);
568 	priv->node = node;
569 
570 	/* Initialize interface structure */
571 	if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
572 	ifp->if_output = ng_iface_output;
573 	ifp->if_start = ng_iface_start;
574 	ifp->if_ioctl = ng_iface_ioctl;
575 	ifp->if_watchdog = NULL;
576 	ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
577 	ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
578 	ifp->if_type = IFT_PROPVIRTUAL;		/* XXX */
579 	ifp->if_addrlen = 0;			/* XXX */
580 	ifp->if_hdrlen = 0;			/* XXX */
581 	ifp->if_baudrate = 64000;		/* XXX */
582 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
583 	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
584 	IFQ_SET_READY(&ifp->if_snd);
585 
586 	/* Give this node the same name as the interface (if possible) */
587 	if (ng_name_node(node, ifp->if_xname) != 0)
588 		log(LOG_WARNING, "%s: can't acquire netgraph name\n",
589 		    ifp->if_xname);
590 
591 	/* Attach the interface */
592 	if_attach(ifp);
593 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
594 
595 	/* Done */
596 	return (0);
597 }
598 
599 /*
600  * Give our ok for a hook to be added
601  */
602 static int
603 ng_iface_newhook(node_p node, hook_p hook, const char *name)
604 {
605 	const iffam_p iffam = get_iffam_from_name(name);
606 	hook_p *hookptr;
607 
608 	if (iffam == NULL)
609 		return (EPFNOSUPPORT);
610 	hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
611 	if (*hookptr != NULL)
612 		return (EISCONN);
613 	*hookptr = hook;
614 	NG_HOOK_HI_STACK(hook);
615 	NG_HOOK_SET_TO_INBOUND(hook);
616 	return (0);
617 }
618 
619 /*
620  * Receive a control message
621  */
622 static int
623 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
624 {
625 	const priv_p priv = NG_NODE_PRIVATE(node);
626 	struct ifnet *const ifp = priv->ifp;
627 	struct ng_mesg *resp = NULL;
628 	int error = 0;
629 	struct ng_mesg *msg;
630 
631 	NGI_GET_MSG(item, msg);
632 	switch (msg->header.typecookie) {
633 	case NGM_IFACE_COOKIE:
634 		switch (msg->header.cmd) {
635 		case NGM_IFACE_GET_IFNAME:
636 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
637 			if (resp == NULL) {
638 				error = ENOMEM;
639 				break;
640 			}
641 			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
642 			break;
643 
644 		case NGM_IFACE_POINT2POINT:
645 		case NGM_IFACE_BROADCAST:
646 		    {
647 
648 			/* Deny request if interface is UP */
649 			if ((ifp->if_flags & IFF_UP) != 0)
650 				return (EBUSY);
651 
652 			/* Change flags */
653 			switch (msg->header.cmd) {
654 			case NGM_IFACE_POINT2POINT:
655 				ifp->if_flags |= IFF_POINTOPOINT;
656 				ifp->if_flags &= ~IFF_BROADCAST;
657 				break;
658 			case NGM_IFACE_BROADCAST:
659 				ifp->if_flags &= ~IFF_POINTOPOINT;
660 				ifp->if_flags |= IFF_BROADCAST;
661 				break;
662 			}
663 			break;
664 		    }
665 
666 		case NGM_IFACE_GET_IFINDEX:
667 			NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
668 			if (resp == NULL) {
669 				error = ENOMEM;
670 				break;
671 			}
672 			*((uint32_t *)resp->data) = priv->ifp->if_index;
673 			break;
674 
675 		default:
676 			error = EINVAL;
677 			break;
678 		}
679 		break;
680 	case NGM_CISCO_COOKIE:
681 		switch (msg->header.cmd) {
682 		case NGM_CISCO_GET_IPADDR:	/* we understand this too */
683 		    {
684 			struct ifaddr *ifa;
685 
686 			/* Return the first configured IP address */
687 			if_addr_rlock(ifp);
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 			if_addr_runlock(ifp);
706 
707 			/* No IP addresses on this interface? */
708 			if (ifa == NULL)
709 				error = EADDRNOTAVAIL;
710 			break;
711 		    }
712 		default:
713 			error = EINVAL;
714 			break;
715 		}
716 		break;
717 	case NGM_FLOW_COOKIE:
718 		switch (msg->header.cmd) {
719 		case NGM_LINK_IS_UP:
720 			ifp->if_drv_flags |= IFF_DRV_RUNNING;
721 			break;
722 		case NGM_LINK_IS_DOWN:
723 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
724 			break;
725 		default:
726 			break;
727 		}
728 		break;
729 	default:
730 		error = EINVAL;
731 		break;
732 	}
733 	NG_RESPOND_MSG(error, node, item, resp);
734 	NG_FREE_MSG(msg);
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, item_p item)
743 {
744 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
745 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
746 	struct ifnet *const ifp = priv->ifp;
747 	struct mbuf *m;
748 	int isr;
749 
750 	NGI_GET_M(item, m);
751 	NG_FREE_ITEM(item);
752 	/* Sanity checks */
753 	KASSERT(iffam != NULL, ("%s: iffam", __func__));
754 	M_ASSERTPKTHDR(m);
755 	if ((ifp->if_flags & IFF_UP) == 0) {
756 		NG_FREE_M(m);
757 		return (ENETDOWN);
758 	}
759 
760 	/* Update interface stats */
761 	ifp->if_ipackets++;
762 	ifp->if_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 	/* Send packet */
771 	switch (iffam->family) {
772 #ifdef INET
773 	case AF_INET:
774 		isr = NETISR_IP;
775 		break;
776 #endif
777 #ifdef INET6
778 	case AF_INET6:
779 		isr = NETISR_IPV6;
780 		break;
781 #endif
782 #ifdef IPX
783 	case AF_IPX:
784 		isr = NETISR_IPX;
785 		break;
786 #endif
787 #ifdef NETATALK
788 	case AF_APPLETALK:
789 		isr = NETISR_ATALK2;
790 		break;
791 #endif
792 	default:
793 		m_freem(m);
794 		return (EAFNOSUPPORT);
795 	}
796 	/* First chunk of an mbuf contains good junk */
797 	if (harvest.point_to_point)
798 		random_harvest(m, 16, 3, 0, RANDOM_NET);
799 	netisr_dispatch(isr, m);
800 	return (0);
801 }
802 
803 /*
804  * Shutdown and remove the node and its associated interface.
805  */
806 static int
807 ng_iface_shutdown(node_p node)
808 {
809 	INIT_VNET_NETGRAPH(curvnet);
810 	const priv_p priv = NG_NODE_PRIVATE(node);
811 
812 	/*
813 	 * The ifnet may be in a different vnet than the netgraph node,
814 	 * hence we have to change the current vnet context here.
815 	 */
816 	CURVNET_SET_QUIET(priv->ifp->if_vnet);
817 	bpfdetach(priv->ifp);
818 	if_detach(priv->ifp);
819 	if_free(priv->ifp);
820 	CURVNET_RESTORE();
821 	priv->ifp = NULL;
822 	free_unr(V_ng_iface_unit, priv->unit);
823 	free(priv, M_NETGRAPH_IFACE);
824 	NG_NODE_SET_PRIVATE(node, NULL);
825 	NG_NODE_UNREF(node);
826 	return (0);
827 }
828 
829 /*
830  * Hook disconnection. Note that we do *not* shutdown when all
831  * hooks have been disconnected.
832  */
833 static int
834 ng_iface_disconnect(hook_p hook)
835 {
836 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
837 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
838 
839 	if (iffam == NULL)
840 		panic(__func__);
841 	*get_hook_from_iffam(priv, iffam) = NULL;
842 	return (0);
843 }
844 
845 /*
846  * Handle loading and unloading for this node type.
847  */
848 static int
849 ng_iface_mod_event(module_t mod, int event, void *data)
850 {
851 	int error = 0;
852 
853 	switch (event) {
854 	case MOD_LOAD:
855 #ifndef VIMAGE_GLOBALS
856 		vnet_mod_register(&vnet_ng_iface_modinfo);
857 #else
858 		ng_iface_iattach(NULL);
859 #endif
860 		break;
861 	case MOD_UNLOAD:
862 #ifndef VIMAGE_GLOBALS
863 		vnet_mod_deregister(&vnet_ng_iface_modinfo);
864 #else
865 		ng_iface_idetach(NULL);
866 #endif
867 		break;
868 	default:
869 		error = EOPNOTSUPP;
870 		break;
871 	}
872 	return (error);
873 }
874 
875 static int ng_iface_iattach(const void *unused)
876 {
877 	INIT_VNET_NETGRAPH(curvnet);
878 
879 	V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
880 
881 	return (0);
882 }
883 
884 static int ng_iface_idetach(const void *unused)
885 {
886 	INIT_VNET_NETGRAPH(curvnet);
887 
888 	delete_unrhdr(V_ng_iface_unit);
889 
890 	return (0);
891 }
892