xref: /freebsd/sys/netgraph/ng_iface.c (revision 370e009188ba90c3290b1479aa06ec98b66e140a)
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, 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_inet.h"
56 #include "opt_inet6.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/lock.h>
63 #include <sys/malloc.h>
64 #include <sys/mbuf.h>
65 #include <sys/errno.h>
66 #include <sys/proc.h>
67 #include <sys/random.h>
68 #include <sys/rmlock.h>
69 #include <sys/sockio.h>
70 #include <sys/socket.h>
71 #include <sys/sysctl.h>
72 #include <sys/syslog.h>
73 #include <sys/libkern.h>
74 
75 #include <net/if.h>
76 #include <net/if_var.h>
77 #include <net/if_private.h>
78 #include <net/if_types.h>
79 #include <net/bpf.h>
80 #include <net/netisr.h>
81 #include <net/route.h>
82 #include <net/vnet.h>
83 
84 #include <netinet/in.h>
85 
86 #include <netgraph/ng_message.h>
87 #include <netgraph/netgraph.h>
88 #include <netgraph/ng_parse.h>
89 #include <netgraph/ng_iface.h>
90 
91 #ifdef NG_SEPARATE_MALLOC
92 static MALLOC_DEFINE(M_NETGRAPH_IFACE, "netgraph_iface", "netgraph iface node");
93 #else
94 #define M_NETGRAPH_IFACE M_NETGRAPH
95 #endif
96 
97 static SYSCTL_NODE(_net_graph, OID_AUTO, iface, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
98     "Point to point netgraph interface");
99 VNET_DEFINE_STATIC(int, ng_iface_max_nest) = 2;
100 #define	V_ng_iface_max_nest	VNET(ng_iface_max_nest)
101 SYSCTL_INT(_net_graph_iface, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
102     &VNET_NAME(ng_iface_max_nest), 0, "Max nested tunnels");
103 
104 /* This struct describes one address family */
105 struct iffam {
106 	sa_family_t	family;		/* Address family */
107 	const char	*hookname;	/* Name for hook */
108 };
109 typedef const struct iffam *iffam_p;
110 
111 /* List of address families supported by our interface */
112 const static struct iffam gFamilies[] = {
113 	{ AF_INET,	NG_IFACE_HOOK_INET	},
114 	{ AF_INET6,	NG_IFACE_HOOK_INET6	},
115 };
116 #define	NUM_FAMILIES		nitems(gFamilies)
117 
118 /* Node private data */
119 struct ng_iface_private {
120 	struct	ifnet *ifp;		/* Our interface */
121 	int	unit;			/* Interface unit number */
122 	node_p	node;			/* Our netgraph node */
123 	hook_p	hooks[NUM_FAMILIES];	/* Hook for each address family */
124 	struct rmlock	lock;		/* Protect private data changes */
125 };
126 typedef struct ng_iface_private *priv_p;
127 
128 #define	PRIV_RLOCK(priv, t)	rm_rlock(&priv->lock, t)
129 #define	PRIV_RUNLOCK(priv, t)	rm_runlock(&priv->lock, t)
130 #define	PRIV_WLOCK(priv)	rm_wlock(&priv->lock)
131 #define	PRIV_WUNLOCK(priv)	rm_wunlock(&priv->lock)
132 
133 /* Interface methods */
134 static void	ng_iface_start(struct ifnet *ifp);
135 static int	ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
136 static int	ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
137     			const struct sockaddr *dst, struct route *ro);
138 static void	ng_iface_bpftap(struct ifnet *ifp,
139 			struct mbuf *m, sa_family_t family);
140 static int	ng_iface_send(struct ifnet *ifp, struct mbuf *m,
141 			sa_family_t sa);
142 #ifdef DEBUG
143 static void	ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
144 #endif
145 
146 /* Netgraph methods */
147 static int		ng_iface_mod_event(module_t, int, void *);
148 static ng_constructor_t	ng_iface_constructor;
149 static ng_rcvmsg_t	ng_iface_rcvmsg;
150 static ng_shutdown_t	ng_iface_shutdown;
151 static ng_newhook_t	ng_iface_newhook;
152 static ng_rcvdata_t	ng_iface_rcvdata;
153 static ng_disconnect_t	ng_iface_disconnect;
154 
155 /* Helper stuff */
156 static iffam_p	get_iffam_from_af(sa_family_t family);
157 static iffam_p	get_iffam_from_hook(priv_p priv, hook_p hook);
158 static iffam_p	get_iffam_from_name(const char *name);
159 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
160 
161 /* List of commands and how to convert arguments to/from ASCII */
162 static const struct ng_cmdlist ng_iface_cmds[] = {
163 	{
164 	  NGM_IFACE_COOKIE,
165 	  NGM_IFACE_GET_IFNAME,
166 	  "getifname",
167 	  NULL,
168 	  &ng_parse_string_type
169 	},
170 	{
171 	  NGM_IFACE_COOKIE,
172 	  NGM_IFACE_POINT2POINT,
173 	  "point2point",
174 	  NULL,
175 	  NULL
176 	},
177 	{
178 	  NGM_IFACE_COOKIE,
179 	  NGM_IFACE_BROADCAST,
180 	  "broadcast",
181 	  NULL,
182 	  NULL
183 	},
184 	{
185 	  NGM_IFACE_COOKIE,
186 	  NGM_IFACE_GET_IFINDEX,
187 	  "getifindex",
188 	  NULL,
189 	  &ng_parse_uint32_type
190 	},
191 	{ 0 }
192 };
193 
194 /* Node type descriptor */
195 static struct ng_type typestruct = {
196 	.version =	NG_ABI_VERSION,
197 	.name =		NG_IFACE_NODE_TYPE,
198 	.mod_event =	ng_iface_mod_event,
199 	.constructor =	ng_iface_constructor,
200 	.rcvmsg =	ng_iface_rcvmsg,
201 	.shutdown =	ng_iface_shutdown,
202 	.newhook =	ng_iface_newhook,
203 	.rcvdata =	ng_iface_rcvdata,
204 	.disconnect =	ng_iface_disconnect,
205 	.cmdlist =	ng_iface_cmds,
206 };
207 NETGRAPH_INIT(iface, &typestruct);
208 
209 VNET_DEFINE_STATIC(struct unrhdr *, ng_iface_unit);
210 #define	V_ng_iface_unit			VNET(ng_iface_unit)
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 			INTERFACE STUFF
276  ************************************************************************/
277 
278 /*
279  * Process an ioctl for the virtual interface
280  */
281 static int
282 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
283 {
284 	struct ifreq *const ifr = (struct ifreq *) data;
285 	int error = 0;
286 
287 #ifdef DEBUG
288 	ng_iface_print_ioctl(ifp, command, data);
289 #endif
290 	switch (command) {
291 	/* These two are mostly handled at a higher layer */
292 	case SIOCSIFADDR:
293 		ifp->if_flags |= IFF_UP;
294 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
295 		ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
296 		break;
297 	case SIOCGIFADDR:
298 		break;
299 
300 	/* Set flags */
301 	case SIOCSIFFLAGS:
302 		/*
303 		 * If the interface is marked up and stopped, then start it.
304 		 * If it is marked down and running, then stop it.
305 		 */
306 		if (ifr->ifr_flags & IFF_UP) {
307 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
308 				ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
309 				ifp->if_drv_flags |= IFF_DRV_RUNNING;
310 			}
311 		} else {
312 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
313 				ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
314 				    IFF_DRV_OACTIVE);
315 		}
316 		break;
317 
318 	/* Set the interface MTU */
319 	case SIOCSIFMTU:
320 		if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
321 		    || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
322 			error = EINVAL;
323 		else
324 			ifp->if_mtu = ifr->ifr_mtu;
325 		break;
326 
327 	/* Stuff that's not supported */
328 	case SIOCADDMULTI:
329 	case SIOCDELMULTI:
330 		error = 0;
331 		break;
332 	case SIOCSIFPHYS:
333 		error = EOPNOTSUPP;
334 		break;
335 
336 	default:
337 		error = EINVAL;
338 		break;
339 	}
340 	return (error);
341 }
342 
343 /*
344  * This routine is called to deliver a packet out the interface.
345  * We simply look at the address family and relay the packet to
346  * the corresponding hook, if it exists and is connected.
347  */
348 
349 static int
350 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
351 	const struct sockaddr *dst, struct route *ro)
352 {
353 	uint32_t af;
354 	int error;
355 
356 	/* Check interface flags */
357 	if (!((ifp->if_flags & IFF_UP) &&
358 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
359 		m_freem(m);
360 		return (ENETDOWN);
361 	}
362 
363 	/* Protect from deadly infinite recursion. */
364 	error = if_tunnel_check_nesting(ifp, m, NGM_IFACE_COOKIE,
365 	    V_ng_iface_max_nest);
366 	if (error) {
367 		m_freem(m);
368 		return (error);
369 	}
370 
371 	/* BPF writes need to be handled specially. */
372 	if (dst->sa_family == AF_UNSPEC)
373 		bcopy(dst->sa_data, &af, sizeof(af));
374 	else
375 		af = RO_GET_FAMILY(ro, dst);
376 
377 	/* Berkeley packet filter */
378 	ng_iface_bpftap(ifp, m, af);
379 
380 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
381 		M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
382 		if (m == NULL) {
383 			if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
384 			return (ENOBUFS);
385 		}
386 		*(sa_family_t *)m->m_data = af;
387 		error = (ifp->if_transmit)(ifp, m);
388 	} else
389 		error = ng_iface_send(ifp, m, af);
390 
391 	return (error);
392 }
393 
394 /*
395  * Start method is used only when ALTQ is enabled.
396  */
397 static void
398 ng_iface_start(struct ifnet *ifp)
399 {
400 	struct mbuf *m;
401 	sa_family_t sa;
402 
403 	KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
404 
405 	for(;;) {
406 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
407 		if (m == NULL)
408 			break;
409 		sa = *mtod(m, sa_family_t *);
410 		m_adj(m, sizeof(sa_family_t));
411 		ng_iface_send(ifp, m, sa);
412 	}
413 }
414 
415 /*
416  * Flash a packet by the BPF (requires prepending 4 byte AF header)
417  * Note the phoney mbuf; this is OK because BPF treats it read-only.
418  */
419 static void
420 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
421 {
422 	KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
423 	if (bpf_peers_present(ifp->if_bpf)) {
424 		int32_t family4 = (int32_t)family;
425 		bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
426 	}
427 }
428 
429 /*
430  * This routine does actual delivery of the packet into the
431  * netgraph(4). It is called from ng_iface_start() and
432  * ng_iface_output().
433  */
434 static int
435 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
436 {
437 	struct rm_priotracker priv_tracker;
438 	const priv_p priv = (priv_p) ifp->if_softc;
439 	const iffam_p iffam = get_iffam_from_af(sa);
440 	hook_p hook;
441 	int error;
442 	int len;
443 
444 	/* Check address family to determine hook (if known) */
445 	if (iffam == NULL) {
446 		m_freem(m);
447 		log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
448 		return (EAFNOSUPPORT);
449 	}
450 
451 	/* Copy length before the mbuf gets invalidated. */
452 	len = m->m_pkthdr.len;
453 
454 	PRIV_RLOCK(priv, &priv_tracker);
455 	hook = *get_hook_from_iffam(priv, iffam);
456 	if (hook == NULL) {
457 		NG_FREE_M(m);
458 		PRIV_RUNLOCK(priv, &priv_tracker);
459 		return ENETDOWN;
460 	}
461 	NG_HOOK_REF(hook);
462 	PRIV_RUNLOCK(priv, &priv_tracker);
463 
464 	NG_OUTBOUND_THREAD_REF();
465 	NG_SEND_DATA_ONLY(error, hook, m);
466 	NG_OUTBOUND_THREAD_UNREF();
467 	NG_HOOK_UNREF(hook);
468 
469 	/* Update stats. */
470 	if (error == 0) {
471 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
472 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
473 	}
474 
475 	return (error);
476 }
477 
478 #ifdef DEBUG
479 /*
480  * Display an ioctl to the virtual interface
481  */
482 
483 static void
484 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
485 {
486 	char   *str;
487 
488 	switch (command & IOC_DIRMASK) {
489 	case IOC_VOID:
490 		str = "IO";
491 		break;
492 	case IOC_OUT:
493 		str = "IOR";
494 		break;
495 	case IOC_IN:
496 		str = "IOW";
497 		break;
498 	case IOC_INOUT:
499 		str = "IORW";
500 		break;
501 	default:
502 		str = "IO??";
503 	}
504 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
505 	       ifp->if_xname,
506 	       str,
507 	       IOCGROUP(command),
508 	       command & 0xff,
509 	       IOCPARM_LEN(command));
510 }
511 #endif /* DEBUG */
512 
513 /************************************************************************
514 			NETGRAPH NODE STUFF
515  ************************************************************************/
516 
517 /*
518  * Constructor for a node
519  */
520 static int
521 ng_iface_constructor(node_p node)
522 {
523 	struct ifnet *ifp;
524 	priv_p priv;
525 
526 	/* Allocate node and interface private structures */
527 	priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
528 	ifp = if_alloc(IFT_PROPVIRTUAL);
529 	if (ifp == NULL) {
530 		free(priv, M_NETGRAPH_IFACE);
531 		return (ENOMEM);
532 	}
533 
534 	rm_init(&priv->lock, "ng_iface private rmlock");
535 
536 	/* Link them together */
537 	ifp->if_softc = priv;
538 	priv->ifp = ifp;
539 
540 	/* Get an interface unit number */
541 	priv->unit = alloc_unr(V_ng_iface_unit);
542 
543 	/* Link together node and private info */
544 	NG_NODE_SET_PRIVATE(node, priv);
545 	priv->node = node;
546 
547 	/* Initialize interface structure */
548 	if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
549 	ifp->if_output = ng_iface_output;
550 	ifp->if_start = ng_iface_start;
551 	ifp->if_ioctl = ng_iface_ioctl;
552 	ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
553 	ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
554 	ifp->if_type = IFT_PROPVIRTUAL;		/* XXX */
555 	ifp->if_addrlen = 0;			/* XXX */
556 	ifp->if_hdrlen = 0;			/* XXX */
557 	ifp->if_baudrate = 64000;		/* XXX */
558 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
559 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
560 	IFQ_SET_READY(&ifp->if_snd);
561 
562 	/* Give this node the same name as the interface (if possible) */
563 	if (ng_name_node(node, ifp->if_xname) != 0)
564 		log(LOG_WARNING, "%s: can't acquire netgraph name\n",
565 		    ifp->if_xname);
566 
567 	/* Attach the interface */
568 	if_attach(ifp);
569 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
570 
571 	/* Done */
572 	return (0);
573 }
574 
575 /*
576  * Give our ok for a hook to be added
577  */
578 static int
579 ng_iface_newhook(node_p node, hook_p hook, const char *name)
580 {
581 	const iffam_p iffam = get_iffam_from_name(name);
582 	const priv_p priv = NG_NODE_PRIVATE(node);
583 	hook_p *hookptr;
584 
585 	if (iffam == NULL)
586 		return (EPFNOSUPPORT);
587 	PRIV_WLOCK(priv);
588 	hookptr = get_hook_from_iffam(priv, iffam);
589 	if (*hookptr != NULL) {
590 		PRIV_WUNLOCK(priv);
591 		return (EISCONN);
592 	}
593 	*hookptr = hook;
594 	NG_HOOK_HI_STACK(hook);
595 	NG_HOOK_SET_TO_INBOUND(hook);
596 	PRIV_WUNLOCK(priv);
597 	return (0);
598 }
599 
600 /*
601  * Receive a control message
602  */
603 static int
604 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
605 {
606 	const priv_p priv = NG_NODE_PRIVATE(node);
607 	struct ifnet *const ifp = priv->ifp;
608 	struct ng_mesg *resp = NULL;
609 	int error = 0;
610 	struct ng_mesg *msg;
611 
612 	NGI_GET_MSG(item, msg);
613 	switch (msg->header.typecookie) {
614 	case NGM_IFACE_COOKIE:
615 		switch (msg->header.cmd) {
616 		case NGM_IFACE_GET_IFNAME:
617 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
618 			if (resp == NULL) {
619 				error = ENOMEM;
620 				break;
621 			}
622 			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
623 			break;
624 
625 		case NGM_IFACE_POINT2POINT:
626 		case NGM_IFACE_BROADCAST:
627 		    {
628 			/* Deny request if interface is UP */
629 			if ((ifp->if_flags & IFF_UP) != 0)
630 				return (EBUSY);
631 
632 			/* Change flags */
633 			switch (msg->header.cmd) {
634 			case NGM_IFACE_POINT2POINT:
635 				ifp->if_flags |= IFF_POINTOPOINT;
636 				ifp->if_flags &= ~IFF_BROADCAST;
637 				break;
638 			case NGM_IFACE_BROADCAST:
639 				ifp->if_flags &= ~IFF_POINTOPOINT;
640 				ifp->if_flags |= IFF_BROADCAST;
641 				break;
642 			}
643 			break;
644 		    }
645 
646 		case NGM_IFACE_GET_IFINDEX:
647 			NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
648 			if (resp == NULL) {
649 				error = ENOMEM;
650 				break;
651 			}
652 			*((uint32_t *)resp->data) = priv->ifp->if_index;
653 			break;
654 
655 		default:
656 			error = EINVAL;
657 			break;
658 		}
659 		break;
660 	case NGM_FLOW_COOKIE:
661 		switch (msg->header.cmd) {
662 		case NGM_LINK_IS_UP:
663 			if_link_state_change(ifp, LINK_STATE_UP);
664 			break;
665 		case NGM_LINK_IS_DOWN:
666 			if_link_state_change(ifp, LINK_STATE_DOWN);
667 			break;
668 		default:
669 			break;
670 		}
671 		break;
672 	default:
673 		error = EINVAL;
674 		break;
675 	}
676 	NG_RESPOND_MSG(error, node, item, resp);
677 	NG_FREE_MSG(msg);
678 	return (error);
679 }
680 
681 /*
682  * Recive data from a hook. Pass the packet to the correct input routine.
683  */
684 static int
685 ng_iface_rcvdata(hook_p hook, item_p item)
686 {
687 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
688 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
689 	struct ifnet *const ifp = priv->ifp;
690 	struct epoch_tracker et;
691 	struct mbuf *m;
692 	int isr;
693 
694 	NGI_GET_M(item, m);
695 	NG_FREE_ITEM(item);
696 	/* Sanity checks */
697 	KASSERT(iffam != NULL, ("%s: iffam", __func__));
698 	M_ASSERTPKTHDR(m);
699 	if ((ifp->if_flags & IFF_UP) == 0) {
700 		NG_FREE_M(m);
701 		return (ENETDOWN);
702 	}
703 
704 	/* Update interface stats */
705 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
706 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
707 
708 	/* Note receiving interface */
709 	m->m_pkthdr.rcvif = ifp;
710 
711 	/* Berkeley packet filter */
712 	ng_iface_bpftap(ifp, m, iffam->family);
713 
714 	/* Send packet */
715 	switch (iffam->family) {
716 #ifdef INET
717 	case AF_INET:
718 		isr = NETISR_IP;
719 		break;
720 #endif
721 #ifdef INET6
722 	case AF_INET6:
723 		isr = NETISR_IPV6;
724 		break;
725 #endif
726 	default:
727 		m_freem(m);
728 		return (EAFNOSUPPORT);
729 	}
730 	random_harvest_queue(m, sizeof(*m), RANDOM_NET_NG);
731 	M_SETFIB(m, ifp->if_fib);
732 	CURVNET_SET(ifp->if_vnet);
733 	NET_EPOCH_ENTER(et);
734 	netisr_dispatch(isr, m);
735 	NET_EPOCH_EXIT(et);
736 	CURVNET_RESTORE();
737 	return (0);
738 }
739 
740 /*
741  * Shutdown and remove the node and its associated interface.
742  */
743 static int
744 ng_iface_shutdown(node_p node)
745 {
746 	const priv_p priv = NG_NODE_PRIVATE(node);
747 
748 	/*
749 	 * The ifnet may be in a different vnet than the netgraph node,
750 	 * hence we have to change the current vnet context here.
751 	 */
752 	CURVNET_SET_QUIET(priv->ifp->if_vnet);
753 	bpfdetach(priv->ifp);
754 	if_detach(priv->ifp);
755 	if_free(priv->ifp);
756 	CURVNET_RESTORE();
757 	priv->ifp = NULL;
758 	free_unr(V_ng_iface_unit, priv->unit);
759 	rm_destroy(&priv->lock);
760 	free(priv, M_NETGRAPH_IFACE);
761 	NG_NODE_SET_PRIVATE(node, NULL);
762 	NG_NODE_UNREF(node);
763 	return (0);
764 }
765 
766 /*
767  * Hook disconnection. Note that we do *not* shutdown when all
768  * hooks have been disconnected.
769  */
770 static int
771 ng_iface_disconnect(hook_p hook)
772 {
773 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
774 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
775 
776 	if (iffam == NULL)
777 		panic("%s", __func__);
778 	PRIV_WLOCK(priv);
779 	*get_hook_from_iffam(priv, iffam) = NULL;
780 	PRIV_WUNLOCK(priv);
781 	return (0);
782 }
783 
784 /*
785  * Handle loading and unloading for this node type.
786  */
787 static int
788 ng_iface_mod_event(module_t mod, int event, void *data)
789 {
790 	int error = 0;
791 
792 	switch (event) {
793 	case MOD_LOAD:
794 	case MOD_UNLOAD:
795 		break;
796 	default:
797 		error = EOPNOTSUPP;
798 		break;
799 	}
800 	return (error);
801 }
802 
803 static void
804 vnet_ng_iface_init(const void *unused)
805 {
806 
807 	V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
808 }
809 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
810     vnet_ng_iface_init, NULL);
811 
812 static void
813 vnet_ng_iface_uninit(const void *unused)
814 {
815 
816 	delete_unrhdr(V_ng_iface_unit);
817 }
818 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_INIT_IF, SI_ORDER_ANY,
819     vnet_ng_iface_uninit, NULL);
820