xref: /freebsd/sys/netgraph/ng_iface.c (revision 38d120bc13ac1de5b739b67b87016b9122149374)
1 /*
2  * ng_iface.c
3  */
4 
5 /*-
6  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7  * All rights reserved.
8  *
9  * Subject to the following obligations and disclaimer of warranty, use and
10  * redistribution of this software, in source or object code forms, with or
11  * without modifications are expressly permitted by Whistle Communications;
12  * provided, however, that:
13  * 1. Any and all reproductions of the source or object code must include the
14  *    copyright notice above and the following disclaimer of warranties; and
15  * 2. No rights are granted, in any manner or form, to use Whistle
16  *    Communications, Inc. trademarks, including the mark "WHISTLE
17  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18  *    such appears in the above copyright notice or in the software.
19  *
20  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36  * OF SUCH DAMAGE.
37  *
38  * Author: Archie Cobbs <archie@freebsd.org>
39  *
40  * $FreeBSD$
41  * $Whistle: ng_iface.c,v 1.33 1999/11/01 09:24:51 julian Exp $
42  */
43 
44 /*
45  * This node is also a system networking interface. It has
46  * a hook for each protocol (IP, AppleTalk, etc). Packets
47  * are simply relayed between the interface and the hooks.
48  *
49  * Interfaces are named ng0, ng1, etc.  New nodes take the
50  * first available interface name.
51  *
52  * This node also includes Berkeley packet filter support.
53  */
54 
55 #include "opt_inet.h"
56 #include "opt_inet6.h"
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/errno.h>
61 #include <sys/kernel.h>
62 #include <sys/malloc.h>
63 #include <sys/mbuf.h>
64 #include <sys/errno.h>
65 #include <sys/proc.h>
66 #include <sys/random.h>
67 #include <sys/sockio.h>
68 #include <sys/socket.h>
69 #include <sys/syslog.h>
70 #include <sys/libkern.h>
71 
72 #include <net/if.h>
73 #include <net/if_var.h>
74 #include <net/if_types.h>
75 #include <net/bpf.h>
76 #include <net/netisr.h>
77 #include <net/route.h>
78 #include <net/vnet.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 static 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_ATM,	NG_IFACE_HOOK_ATM	},
106 	{ AF_NATM,	NG_IFACE_HOOK_NATM	},
107 };
108 #define NUM_FAMILIES		(sizeof(gFamilies) / sizeof(*gFamilies))
109 
110 /* Node private data */
111 struct ng_iface_private {
112 	struct	ifnet *ifp;		/* Our interface */
113 	int	unit;			/* Interface unit number */
114 	node_p	node;			/* Our netgraph node */
115 	hook_p	hooks[NUM_FAMILIES];	/* Hook for each address family */
116 };
117 typedef struct ng_iface_private *priv_p;
118 
119 /* Interface methods */
120 static void	ng_iface_start(struct ifnet *ifp);
121 static int	ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
122 static int	ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
123     			const struct sockaddr *dst, struct route *ro);
124 static void	ng_iface_bpftap(struct ifnet *ifp,
125 			struct mbuf *m, sa_family_t family);
126 static int	ng_iface_send(struct ifnet *ifp, struct mbuf *m,
127 			sa_family_t sa);
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 static VNET_DEFINE(struct unrhdr *, ng_iface_unit);
211 #define	V_ng_iface_unit			VNET(ng_iface_unit)
212 
213 /************************************************************************
214 			HELPER STUFF
215  ************************************************************************/
216 
217 /*
218  * Get the family descriptor from the family ID
219  */
220 static __inline iffam_p
221 get_iffam_from_af(sa_family_t family)
222 {
223 	iffam_p iffam;
224 	int k;
225 
226 	for (k = 0; k < NUM_FAMILIES; k++) {
227 		iffam = &gFamilies[k];
228 		if (iffam->family == family)
229 			return (iffam);
230 	}
231 	return (NULL);
232 }
233 
234 /*
235  * Get the family descriptor from the hook
236  */
237 static __inline iffam_p
238 get_iffam_from_hook(priv_p priv, hook_p hook)
239 {
240 	int k;
241 
242 	for (k = 0; k < NUM_FAMILIES; k++)
243 		if (priv->hooks[k] == hook)
244 			return (&gFamilies[k]);
245 	return (NULL);
246 }
247 
248 /*
249  * Get the hook from the iffam descriptor
250  */
251 
252 static __inline hook_p *
253 get_hook_from_iffam(priv_p priv, iffam_p iffam)
254 {
255 	return (&priv->hooks[iffam - gFamilies]);
256 }
257 
258 /*
259  * Get the iffam descriptor from the name
260  */
261 static __inline iffam_p
262 get_iffam_from_name(const char *name)
263 {
264 	iffam_p iffam;
265 	int k;
266 
267 	for (k = 0; k < NUM_FAMILIES; k++) {
268 		iffam = &gFamilies[k];
269 		if (!strcmp(iffam->hookname, name))
270 			return (iffam);
271 	}
272 	return (NULL);
273 }
274 
275 /************************************************************************
276 			INTERFACE STUFF
277  ************************************************************************/
278 
279 /*
280  * Process an ioctl for the virtual interface
281  */
282 static int
283 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
284 {
285 	struct ifreq *const ifr = (struct ifreq *) data;
286 	int error = 0;
287 
288 #ifdef DEBUG
289 	ng_iface_print_ioctl(ifp, command, data);
290 #endif
291 	switch (command) {
292 
293 	/* These two are mostly handled at a higher layer */
294 	case SIOCSIFADDR:
295 		ifp->if_flags |= IFF_UP;
296 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
297 		ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
298 		break;
299 	case SIOCGIFADDR:
300 		break;
301 
302 	/* Set flags */
303 	case SIOCSIFFLAGS:
304 		/*
305 		 * If the interface is marked up and stopped, then start it.
306 		 * If it is marked down and running, then stop it.
307 		 */
308 		if (ifr->ifr_flags & IFF_UP) {
309 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
310 				ifp->if_drv_flags &= ~(IFF_DRV_OACTIVE);
311 				ifp->if_drv_flags |= IFF_DRV_RUNNING;
312 			}
313 		} else {
314 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
315 				ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
316 				    IFF_DRV_OACTIVE);
317 		}
318 		break;
319 
320 	/* Set the interface MTU */
321 	case SIOCSIFMTU:
322 		if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
323 		    || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
324 			error = EINVAL;
325 		else
326 			ifp->if_mtu = ifr->ifr_mtu;
327 		break;
328 
329 	/* Stuff that's not supported */
330 	case SIOCADDMULTI:
331 	case SIOCDELMULTI:
332 		error = 0;
333 		break;
334 	case SIOCSIFPHYS:
335 		error = EOPNOTSUPP;
336 		break;
337 
338 	default:
339 		error = EINVAL;
340 		break;
341 	}
342 	return (error);
343 }
344 
345 /*
346  * This routine is called to deliver a packet out the interface.
347  * We simply look at the address family and relay the packet to
348  * the corresponding hook, if it exists and is connected.
349  */
350 
351 static int
352 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
353 	const struct sockaddr *dst, struct route *ro)
354 {
355 	struct m_tag *mtag;
356 	uint32_t af;
357 	int error;
358 
359 	/* Check interface flags */
360 	if (!((ifp->if_flags & IFF_UP) &&
361 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
362 		m_freem(m);
363 		return (ENETDOWN);
364 	}
365 
366 	/* Protect from deadly infinite recursion. */
367 	mtag = NULL;
368 	while ((mtag = m_tag_locate(m, MTAG_NGIF, MTAG_NGIF_CALLED, mtag))) {
369 		if (*(struct ifnet **)(mtag + 1) == ifp) {
370 			log(LOG_NOTICE, "Loop detected on %s\n", ifp->if_xname);
371 			m_freem(m);
372 			return (EDEADLK);
373 		}
374 	}
375 	mtag = m_tag_alloc(MTAG_NGIF, MTAG_NGIF_CALLED, sizeof(struct ifnet *),
376 	    M_NOWAIT);
377 	if (mtag == NULL) {
378 		m_freem(m);
379 		return (ENOMEM);
380 	}
381 	*(struct ifnet **)(mtag + 1) = ifp;
382 	m_tag_prepend(m, mtag);
383 
384 	/* BPF writes need to be handled specially. */
385 	if (dst->sa_family == AF_UNSPEC)
386 		bcopy(dst->sa_data, &af, sizeof(af));
387 	else
388 		af = dst->sa_family;
389 
390 	/* Berkeley packet filter */
391 	ng_iface_bpftap(ifp, m, af);
392 
393 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
394 		M_PREPEND(m, sizeof(sa_family_t), M_NOWAIT);
395 		if (m == NULL) {
396 			if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
397 			return (ENOBUFS);
398 		}
399 		*(sa_family_t *)m->m_data = af;
400 		error = (ifp->if_transmit)(ifp, m);
401 	} else
402 		error = ng_iface_send(ifp, m, af);
403 
404 	return (error);
405 }
406 
407 /*
408  * Start method is used only when ALTQ is enabled.
409  */
410 static void
411 ng_iface_start(struct ifnet *ifp)
412 {
413 	struct mbuf *m;
414 	sa_family_t sa;
415 
416 	KASSERT(ALTQ_IS_ENABLED(&ifp->if_snd), ("%s without ALTQ", __func__));
417 
418 	for(;;) {
419 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
420 		if (m == NULL)
421 			break;
422 		sa = *mtod(m, sa_family_t *);
423 		m_adj(m, sizeof(sa_family_t));
424 		ng_iface_send(ifp, m, sa);
425 	}
426 }
427 
428 /*
429  * Flash a packet by the BPF (requires prepending 4 byte AF header)
430  * Note the phoney mbuf; this is OK because BPF treats it read-only.
431  */
432 static void
433 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
434 {
435 	KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
436 	if (bpf_peers_present(ifp->if_bpf)) {
437 		int32_t family4 = (int32_t)family;
438 		bpf_mtap2(ifp->if_bpf, &family4, sizeof(family4), m);
439 	}
440 }
441 
442 /*
443  * This routine does actual delivery of the packet into the
444  * netgraph(4). It is called from ng_iface_start() and
445  * ng_iface_output().
446  */
447 static int
448 ng_iface_send(struct ifnet *ifp, struct mbuf *m, sa_family_t sa)
449 {
450 	const priv_p priv = (priv_p) ifp->if_softc;
451 	const iffam_p iffam = get_iffam_from_af(sa);
452 	int error;
453 	int len;
454 
455 	/* Check address family to determine hook (if known) */
456 	if (iffam == NULL) {
457 		m_freem(m);
458 		log(LOG_WARNING, "%s: can't handle af%d\n", ifp->if_xname, sa);
459 		return (EAFNOSUPPORT);
460 	}
461 
462 	/* Copy length before the mbuf gets invalidated. */
463 	len = m->m_pkthdr.len;
464 
465 	/* Send packet. If hook is not connected, mbuf will get freed. */
466 	NG_OUTBOUND_THREAD_REF();
467 	NG_SEND_DATA_ONLY(error, *get_hook_from_iffam(priv, iffam), m);
468 	NG_OUTBOUND_THREAD_UNREF();
469 
470 	/* Update stats. */
471 	if (error == 0) {
472 		if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
473 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
474 	}
475 
476 	return (error);
477 }
478 
479 #ifdef DEBUG
480 /*
481  * Display an ioctl to the virtual interface
482  */
483 
484 static void
485 ng_iface_print_ioctl(struct ifnet *ifp, int command, caddr_t data)
486 {
487 	char   *str;
488 
489 	switch (command & IOC_DIRMASK) {
490 	case IOC_VOID:
491 		str = "IO";
492 		break;
493 	case IOC_OUT:
494 		str = "IOR";
495 		break;
496 	case IOC_IN:
497 		str = "IOW";
498 		break;
499 	case IOC_INOUT:
500 		str = "IORW";
501 		break;
502 	default:
503 		str = "IO??";
504 	}
505 	log(LOG_DEBUG, "%s: %s('%c', %d, char[%d])\n",
506 	       ifp->if_xname,
507 	       str,
508 	       IOCGROUP(command),
509 	       command & 0xff,
510 	       IOCPARM_LEN(command));
511 }
512 #endif /* DEBUG */
513 
514 /************************************************************************
515 			NETGRAPH NODE STUFF
516  ************************************************************************/
517 
518 /*
519  * Constructor for a node
520  */
521 static int
522 ng_iface_constructor(node_p node)
523 {
524 	struct ifnet *ifp;
525 	priv_p priv;
526 
527 	/* Allocate node and interface private structures */
528 	priv = malloc(sizeof(*priv), M_NETGRAPH_IFACE, M_WAITOK | M_ZERO);
529 	ifp = if_alloc(IFT_PROPVIRTUAL);
530 	if (ifp == NULL) {
531 		free(priv, M_NETGRAPH_IFACE);
532 		return (ENOMEM);
533 	}
534 
535 	/* Link them together */
536 	ifp->if_softc = priv;
537 	priv->ifp = ifp;
538 
539 	/* Get an interface unit number */
540 	priv->unit = alloc_unr(V_ng_iface_unit);
541 
542 	/* Link together node and private info */
543 	NG_NODE_SET_PRIVATE(node, priv);
544 	priv->node = node;
545 
546 	/* Initialize interface structure */
547 	if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
548 	ifp->if_output = ng_iface_output;
549 	ifp->if_start = ng_iface_start;
550 	ifp->if_ioctl = ng_iface_ioctl;
551 	ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
552 	ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
553 	ifp->if_type = IFT_PROPVIRTUAL;		/* XXX */
554 	ifp->if_addrlen = 0;			/* XXX */
555 	ifp->if_hdrlen = 0;			/* XXX */
556 	ifp->if_baudrate = 64000;		/* XXX */
557 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
558 	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
559 	IFQ_SET_READY(&ifp->if_snd);
560 
561 	/* Give this node the same name as the interface (if possible) */
562 	if (ng_name_node(node, ifp->if_xname) != 0)
563 		log(LOG_WARNING, "%s: can't acquire netgraph name\n",
564 		    ifp->if_xname);
565 
566 	/* Attach the interface */
567 	if_attach(ifp);
568 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
569 
570 	/* Done */
571 	return (0);
572 }
573 
574 /*
575  * Give our ok for a hook to be added
576  */
577 static int
578 ng_iface_newhook(node_p node, hook_p hook, const char *name)
579 {
580 	const iffam_p iffam = get_iffam_from_name(name);
581 	hook_p *hookptr;
582 
583 	if (iffam == NULL)
584 		return (EPFNOSUPPORT);
585 	hookptr = get_hook_from_iffam(NG_NODE_PRIVATE(node), iffam);
586 	if (*hookptr != NULL)
587 		return (EISCONN);
588 	*hookptr = hook;
589 	NG_HOOK_HI_STACK(hook);
590 	NG_HOOK_SET_TO_INBOUND(hook);
591 	return (0);
592 }
593 
594 /*
595  * Receive a control message
596  */
597 static int
598 ng_iface_rcvmsg(node_p node, item_p item, hook_p lasthook)
599 {
600 	const priv_p priv = NG_NODE_PRIVATE(node);
601 	struct ifnet *const ifp = priv->ifp;
602 	struct ng_mesg *resp = NULL;
603 	int error = 0;
604 	struct ng_mesg *msg;
605 
606 	NGI_GET_MSG(item, msg);
607 	switch (msg->header.typecookie) {
608 	case NGM_IFACE_COOKIE:
609 		switch (msg->header.cmd) {
610 		case NGM_IFACE_GET_IFNAME:
611 			NG_MKRESPONSE(resp, msg, IFNAMSIZ, M_NOWAIT);
612 			if (resp == NULL) {
613 				error = ENOMEM;
614 				break;
615 			}
616 			strlcpy(resp->data, ifp->if_xname, IFNAMSIZ);
617 			break;
618 
619 		case NGM_IFACE_POINT2POINT:
620 		case NGM_IFACE_BROADCAST:
621 		    {
622 
623 			/* Deny request if interface is UP */
624 			if ((ifp->if_flags & IFF_UP) != 0)
625 				return (EBUSY);
626 
627 			/* Change flags */
628 			switch (msg->header.cmd) {
629 			case NGM_IFACE_POINT2POINT:
630 				ifp->if_flags |= IFF_POINTOPOINT;
631 				ifp->if_flags &= ~IFF_BROADCAST;
632 				break;
633 			case NGM_IFACE_BROADCAST:
634 				ifp->if_flags &= ~IFF_POINTOPOINT;
635 				ifp->if_flags |= IFF_BROADCAST;
636 				break;
637 			}
638 			break;
639 		    }
640 
641 		case NGM_IFACE_GET_IFINDEX:
642 			NG_MKRESPONSE(resp, msg, sizeof(uint32_t), M_NOWAIT);
643 			if (resp == NULL) {
644 				error = ENOMEM;
645 				break;
646 			}
647 			*((uint32_t *)resp->data) = priv->ifp->if_index;
648 			break;
649 
650 		default:
651 			error = EINVAL;
652 			break;
653 		}
654 		break;
655 	case NGM_CISCO_COOKIE:
656 		switch (msg->header.cmd) {
657 		case NGM_CISCO_GET_IPADDR:	/* we understand this too */
658 		    {
659 			struct ifaddr *ifa;
660 
661 			/* Return the first configured IP address */
662 			if_addr_rlock(ifp);
663 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
664 				struct ng_cisco_ipaddr *ips;
665 
666 				if (ifa->ifa_addr->sa_family != AF_INET)
667 					continue;
668 				NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
669 				if (resp == NULL) {
670 					error = ENOMEM;
671 					break;
672 				}
673 				ips = (struct ng_cisco_ipaddr *)resp->data;
674 				ips->ipaddr = ((struct sockaddr_in *)
675 						ifa->ifa_addr)->sin_addr;
676 				ips->netmask = ((struct sockaddr_in *)
677 						ifa->ifa_netmask)->sin_addr;
678 				break;
679 			}
680 			if_addr_runlock(ifp);
681 
682 			/* No IP addresses on this interface? */
683 			if (ifa == NULL)
684 				error = EADDRNOTAVAIL;
685 			break;
686 		    }
687 		default:
688 			error = EINVAL;
689 			break;
690 		}
691 		break;
692 	case NGM_FLOW_COOKIE:
693 		switch (msg->header.cmd) {
694 		case NGM_LINK_IS_UP:
695 			ifp->if_drv_flags |= IFF_DRV_RUNNING;
696 			break;
697 		case NGM_LINK_IS_DOWN:
698 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
699 			break;
700 		default:
701 			break;
702 		}
703 		break;
704 	default:
705 		error = EINVAL;
706 		break;
707 	}
708 	NG_RESPOND_MSG(error, node, item, resp);
709 	NG_FREE_MSG(msg);
710 	return (error);
711 }
712 
713 /*
714  * Recive data from a hook. Pass the packet to the correct input routine.
715  */
716 static int
717 ng_iface_rcvdata(hook_p hook, item_p item)
718 {
719 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
720 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
721 	struct ifnet *const ifp = priv->ifp;
722 	struct mbuf *m;
723 	int isr;
724 
725 	NGI_GET_M(item, m);
726 	NG_FREE_ITEM(item);
727 	/* Sanity checks */
728 	KASSERT(iffam != NULL, ("%s: iffam", __func__));
729 	M_ASSERTPKTHDR(m);
730 	if ((ifp->if_flags & IFF_UP) == 0) {
731 		NG_FREE_M(m);
732 		return (ENETDOWN);
733 	}
734 
735 	/* Update interface stats */
736 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
737 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
738 
739 	/* Note receiving interface */
740 	m->m_pkthdr.rcvif = ifp;
741 
742 	/* Berkeley packet filter */
743 	ng_iface_bpftap(ifp, m, iffam->family);
744 
745 	/* Send packet */
746 	switch (iffam->family) {
747 #ifdef INET
748 	case AF_INET:
749 		isr = NETISR_IP;
750 		break;
751 #endif
752 #ifdef INET6
753 	case AF_INET6:
754 		isr = NETISR_IPV6;
755 		break;
756 #endif
757 	default:
758 		m_freem(m);
759 		return (EAFNOSUPPORT);
760 	}
761 	random_harvest(&(m->m_data), 12, 2, RANDOM_NET_NG);
762 	M_SETFIB(m, ifp->if_fib);
763 	netisr_dispatch(isr, m);
764 	return (0);
765 }
766 
767 /*
768  * Shutdown and remove the node and its associated interface.
769  */
770 static int
771 ng_iface_shutdown(node_p node)
772 {
773 	const priv_p priv = NG_NODE_PRIVATE(node);
774 
775 	/*
776 	 * The ifnet may be in a different vnet than the netgraph node,
777 	 * hence we have to change the current vnet context here.
778 	 */
779 	CURVNET_SET_QUIET(priv->ifp->if_vnet);
780 	bpfdetach(priv->ifp);
781 	if_detach(priv->ifp);
782 	if_free(priv->ifp);
783 	CURVNET_RESTORE();
784 	priv->ifp = NULL;
785 	free_unr(V_ng_iface_unit, priv->unit);
786 	free(priv, M_NETGRAPH_IFACE);
787 	NG_NODE_SET_PRIVATE(node, NULL);
788 	NG_NODE_UNREF(node);
789 	return (0);
790 }
791 
792 /*
793  * Hook disconnection. Note that we do *not* shutdown when all
794  * hooks have been disconnected.
795  */
796 static int
797 ng_iface_disconnect(hook_p hook)
798 {
799 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
800 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
801 
802 	if (iffam == NULL)
803 		panic("%s", __func__);
804 	*get_hook_from_iffam(priv, iffam) = NULL;
805 	return (0);
806 }
807 
808 /*
809  * Handle loading and unloading for this node type.
810  */
811 static int
812 ng_iface_mod_event(module_t mod, int event, void *data)
813 {
814 	int error = 0;
815 
816 	switch (event) {
817 	case MOD_LOAD:
818 	case MOD_UNLOAD:
819 		break;
820 	default:
821 		error = EOPNOTSUPP;
822 		break;
823 	}
824 	return (error);
825 }
826 
827 static void
828 vnet_ng_iface_init(const void *unused)
829 {
830 
831 	V_ng_iface_unit = new_unrhdr(0, 0xffff, NULL);
832 }
833 VNET_SYSINIT(vnet_ng_iface_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
834     vnet_ng_iface_init, NULL);
835 
836 static void
837 vnet_ng_iface_uninit(const void *unused)
838 {
839 
840 	delete_unrhdr(V_ng_iface_unit);
841 }
842 VNET_SYSUNINIT(vnet_ng_iface_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
843     vnet_ng_iface_uninit, NULL);
844