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