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