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