xref: /freebsd/sys/netgraph/ng_iface.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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 s, error = 0;
290 
291 #ifdef DEBUG
292 	ng_iface_print_ioctl(ifp, command, data);
293 #endif
294 	s = splimp();
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 	(void) splx(s);
347 	return (error);
348 }
349 
350 /*
351  * This routine is called to deliver a packet out the interface.
352  * We simply look at the address family and relay the packet to
353  * the corresponding hook, if it exists and is connected.
354  */
355 
356 static int
357 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
358     		struct sockaddr *dst, struct route *ro)
359 {
360 	struct m_tag *mtag;
361 	uint32_t af;
362 	int error;
363 
364 	/* Check interface flags */
365 	if (!((ifp->if_flags & IFF_UP) &&
366 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
367 		m_freem(m);
368 		return (ENETDOWN);
369 	}
370 
371 	/* Protect from deadly infinite recursion. */
372 	mtag = NULL;
373 	while ((mtag = m_tag_locate(m, MTAG_NGIF, MTAG_NGIF_CALLED, mtag))) {
374 		if (*(struct ifnet **)(mtag + 1) == ifp) {
375 			log(LOG_NOTICE, "Loop detected on %s\n", ifp->if_xname);
376 			m_freem(m);
377 			return (EDEADLK);
378 		}
379 	}
380 	mtag = m_tag_alloc(MTAG_NGIF, MTAG_NGIF_CALLED, sizeof(struct ifnet *),
381 	    M_NOWAIT);
382 	if (mtag == NULL) {
383 		m_freem(m);
384 		return (ENOMEM);
385 	}
386 	*(struct ifnet **)(mtag + 1) = ifp;
387 	m_tag_prepend(m, mtag);
388 
389 	/* BPF writes need to be handled specially. */
390 	if (dst->sa_family == AF_UNSPEC) {
391 		bcopy(dst->sa_data, &af, sizeof(af));
392 		dst->sa_family = af;
393 	}
394 
395 	/* Berkeley packet filter */
396 	ng_iface_bpftap(ifp, m, dst->sa_family);
397 
398 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
399 		M_PREPEND(m, sizeof(sa_family_t), M_DONTWAIT);
400 		if (m == NULL) {
401 			IFQ_LOCK(&ifp->if_snd);
402 			IFQ_INC_DROPS(&ifp->if_snd);
403 			IFQ_UNLOCK(&ifp->if_snd);
404 			ifp->if_oerrors++;
405 			return (ENOBUFS);
406 		}
407 		*(sa_family_t *)m->m_data = dst->sa_family;
408 		error = (ifp->if_transmit)(ifp, m);
409 	} else
410 		error = ng_iface_send(ifp, m, dst->sa_family);
411 
412 	return (error);
413 }
414 
415 /*
416  * Start method is used only when ALTQ is enabled.
417  */
418 static void
419 ng_iface_start(struct ifnet *ifp)
420 {
421 	struct mbuf *m;
422 	sa_family_t sa;
423 
424 	KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
425 
426 	for(;;) {
427 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
428 		if (m == NULL)
429 			break;
430 		sa = *mtod(m, sa_family_t *);
431 		m_adj(m, sizeof(sa_family_t));
432 		ng_iface_send(ifp, m, sa);
433 	}
434 }
435 
436 /*
437  * Flash a packet by the BPF (requires prepending 4 byte AF header)
438  * Note the phoney mbuf; this is OK because BPF treats it read-only.
439  */
440 static void
441 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
442 {
443 	KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
444 	if (bpf_peers_present(ifp->if_bpf)) {
445 		int32_t family4 = (int32_t)family;
446 		bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
447 	}
448 }
449 
450 /*
451  * This routine does actual delivery of the packet into the
452  * netgraph(4). It is called from ng_iface_start() and
453  * ng_iface_output().
454  */
455 static int
456 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
457 {
458 	const priv_p priv = (priv_p) ifp->if_softc;
459 	const iffam_p iffam = get_iffam_from_af(sa);
460 	int error;
461 	int len;
462 
463 	/* Check address family to determine hook (if known) */
464 	if (iffam == NULL) {
465 		m_freem(m);
466 		log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
467 		return (EAFNOSUPPORT);
468 	}
469 
470 	/* Copy length before the mbuf gets invalidated. */
471 	len = m->m_pkthdr.len;
472 
473 	/* Send packet. If hook is not connected, mbuf will get freed. */
474 	NG_OUTBOUND_THREAD_REF();
475 	NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
476 	NG_OUTBOUND_THREAD_UNREF();
477 
478 	/* Update stats. */
479 	if (error == 0) {
480 		ifp->if_obytes += len;
481 		ifp->if_opackets++;
482 	}
483 
484 	return (error);
485 }
486 
487 #ifdef DEBUG
488 /*
489  * Display an ioctl to the virtual interface
490  */
491 
492 static void
493 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
494 {
495 	char   *str;
496 
497 	switch (command & IOC_DIRMASK) {
498 	case IOC_VOID:
499 		str = "IO";
500 		break;
501 	case IOC_OUT:
502 		str = "IOR";
503 		break;
504 	case IOC_IN:
505 		str = "IOW";
506 		break;
507 	case IOC_INOUT:
508 		str = "IORW";
509 		break;
510 	default:
511 		str = "IO??";
512 	}
513 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
514 	       ifp->if_xname,
515 	       str,
516 	       IOCGROUP(command),
517 	       command & 0xff,
518 	       IOCPARM_LEN(command));
519 }
520 #endif /* DEBUG */
521 
522 /************************************************************************
523 			NETGRAPH NODE STUFF
524  ************************************************************************/
525 
526 /*
527  * Constructor for a node
528  */
529 static int
530 ng_iface_constructor(node_p node)
531 {
532 	struct ifnet *ifp;
533 	priv_p priv;
534 
535 	/* Allocate node and interface private structures */
536 	priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_NOWAIT|M_ZERO);
537 	if (priv == NULL)
538 		return (ENOMEM);
539 	ifp = if_alloc(IFT_PROPVIRTUAL);
540 	if (ifp == NULL) {
541 		free(priv, M_NETGRAPH_IFACE);
542 		return (ENOMEM);
543 	}
544 
545 	/* Link them together */
546 	ifp->if_softc = priv;
547 	priv->ifp = ifp;
548 
549 	/* Get an interface unit number */
550 	priv->unit = alloc_unr(V_ng_iface_unit);
551 
552 	/* Link together node and private info */
553 	NG_NODE_SET_PRIVATE(node, priv);
554 	priv->node = node;
555 
556 	/* Initialize interface structure */
557 	if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
558 	ifp->if_output = ng_iface_output;
559 	ifp->if_start = ng_iface_start;
560 	ifp->if_ioctl = ng_iface_ioctl;
561 	ifp->if_watchdog = NULL;
562 	ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
563 	ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
564 	ifp->if_type = IFT_PROPVIRTUAL;		/* XXX */
565 	ifp->if_addrlen = 0;			/* XXX */
566 	ifp->if_hdrlen = 0;			/* XXX */
567 	ifp->if_baudrate = 64000;		/* XXX */
568 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
569 	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
570 	IFQ_SET_READY(&ifp->if_snd);
571 
572 	/* Give this node the same name as the interface (if possible) */
573 	if (ng_name_node(node, ifp->if_xname) != 0)
574 		log(LOG_WARNING, "%s: can't acquire netgraph name\n",
575 		    ifp->if_xname);
576 
577 	/* Attach the interface */
578 	if_attach(ifp);
579 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
580 
581 	/* Done */
582 	return (0);
583 }
584 
585 /*
586  * Give our ok for a hook to be added
587  */
588 static int
589 ng_iface_newhook(node_p node, hook_p hook, const char *name)
590 {
591 	const iffam_p iffam = get_iffam_from_name(name);
592 	hook_p *hookptr;
593 
594 	if (iffam == NULL)
595 		return (EPFNOSUPPORT);
596 	hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
597 	if (*hookptr != NULL)
598 		return (EISCONN);
599 	*hookptr = hook;
600 	NG_HOOK_HI_STACK(hook);
601 	NG_HOOK_SET_TO_INBOUND(hook);
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_CISCO_COOKIE:
667 		switch (msg->header.cmd) {
668 		case NGM_CISCO_GET_IPADDR:	/* we understand this too */
669 		    {
670 			struct ifaddr *ifa;
671 
672 			/* Return the first configured IP address */
673 			if_addr_rlock(ifp);
674 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
675 				struct ng_cisco_ipaddr *ips;
676 
677 				if (ifa->ifa_addr->sa_family != AF_INET)
678 					continue;
679 				NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
680 				if (resp == NULL) {
681 					error = ENOMEM;
682 					break;
683 				}
684 				ips = (struct ng_cisco_ipaddr *)resp->data;
685 				ips->ipaddr = ((struct sockaddr_in *)
686 						ifa->ifa_addr)->sin_addr;
687 				ips->netmask = ((struct sockaddr_in *)
688 						ifa->ifa_netmask)->sin_addr;
689 				break;
690 			}
691 			if_addr_runlock(ifp);
692 
693 			/* No IP addresses on this interface? */
694 			if (ifa == NULL)
695 				error = EADDRNOTAVAIL;
696 			break;
697 		    }
698 		default:
699 			error = EINVAL;
700 			break;
701 		}
702 		break;
703 	case NGM_FLOW_COOKIE:
704 		switch (msg->header.cmd) {
705 		case NGM_LINK_IS_UP:
706 			ifp->if_drv_flags |= IFF_DRV_RUNNING;
707 			break;
708 		case NGM_LINK_IS_DOWN:
709 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
710 			break;
711 		default:
712 			break;
713 		}
714 		break;
715 	default:
716 		error = EINVAL;
717 		break;
718 	}
719 	NG_RESPOND_MSG(error, node, item, resp);
720 	NG_FREE_MSG(msg);
721 	return (error);
722 }
723 
724 /*
725  * Recive data from a hook. Pass the packet to the correct input routine.
726  */
727 static int
728 ng_iface_rcvdata(hook_p hook, item_p item)
729 {
730 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
731 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
732 	struct ifnet *const ifp = priv->ifp;
733 	struct mbuf *m;
734 	int isr;
735 
736 	NGI_GET_M(item, m);
737 	NG_FREE_ITEM(item);
738 	/* Sanity checks */
739 	KASSERT(iffam != NULL, ("%s: iffam", __func__));
740 	M_ASSERTPKTHDR(m);
741 	if ((ifp->if_flags & IFF_UP) == 0) {
742 		NG_FREE_M(m);
743 		return (ENETDOWN);
744 	}
745 
746 	/* Update interface stats */
747 	ifp->if_ipackets++;
748 	ifp->if_ibytes += m->m_pkthdr.len;
749 
750 	/* Note receiving interface */
751 	m->m_pkthdr.rcvif = ifp;
752 
753 	/* Berkeley packet filter */
754 	ng_iface_bpftap(ifp, m, iffam->family);
755 
756 	/* Send packet */
757 	switch (iffam->family) {
758 #ifdef INET
759 	case AF_INET:
760 		isr = NETISR_IP;
761 		break;
762 #endif
763 #ifdef INET6
764 	case AF_INET6:
765 		isr = NETISR_IPV6;
766 		break;
767 #endif
768 #ifdef IPX
769 	case AF_IPX:
770 		isr = NETISR_IPX;
771 		break;
772 #endif
773 #ifdef NETATALK
774 	case AF_APPLETALK:
775 		isr = NETISR_ATALK2;
776 		break;
777 #endif
778 	default:
779 		m_freem(m);
780 		return (EAFNOSUPPORT);
781 	}
782 	/* First chunk of an mbuf contains good junk */
783 	if (harvest.point_to_point)
784 		random_harvest(m, 16, 3, 0, RANDOM_NET);
785 	netisr_dispatch(isr, m);
786 	return (0);
787 }
788 
789 /*
790  * Shutdown and remove the node and its associated interface.
791  */
792 static int
793 ng_iface_shutdown(node_p node)
794 {
795 	const priv_p priv = NG_NODE_PRIVATE(node);
796 
797 	/*
798 	 * The ifnet may be in a different vnet than the netgraph node,
799 	 * hence we have to change the current vnet context here.
800 	 */
801 	CURVNET_SET_QUIET(priv->ifp->if_vnet);
802 	bpfdetach(priv->ifp);
803 	if_detach(priv->ifp);
804 	if_free(priv->ifp);
805 	CURVNET_RESTORE();
806 	priv->ifp = NULL;
807 	free_unr(V_ng_iface_unit, priv->unit);
808 	free(priv, M_NETGRAPH_IFACE);
809 	NG_NODE_SET_PRIVATE(node, NULL);
810 	NG_NODE_UNREF(node);
811 	return (0);
812 }
813 
814 /*
815  * Hook disconnection. Note that we do *not* shutdown when all
816  * hooks have been disconnected.
817  */
818 static int
819 ng_iface_disconnect(hook_p hook)
820 {
821 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
822 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
823 
824 	if (iffam == NULL)
825 		panic(__func__);
826 	*get_hook_from_iffam(priv, iffam) = NULL;
827 	return (0);
828 }
829 
830 /*
831  * Handle loading and unloading for this node type.
832  */
833 static int
834 ng_iface_mod_event(module_t mod, int event, void *data)
835 {
836 	int error = 0;
837 
838 	switch (event) {
839 	case MOD_LOAD:
840 	case MOD_UNLOAD:
841 		break;
842 	default:
843 		error = EOPNOTSUPP;
844 		break;
845 	}
846 	return (error);
847 }
848 
849 static void
850 vnet_ng_iface_init(const void *unused)
851 {
852 
853 	V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
854 }
855 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
856     vnet_ng_iface_init, NULL);
857 
858 static void
859 vnet_ng_iface_uninit(const void *unused)
860 {
861 
862 	delete_unrhdr(V_ng_iface_unit);
863 }
864 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
865     vnet_ng_iface_uninit, NULL);
866