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