xref: /dragonfly/sys/netgraph/iface/ng_iface.c (revision 6b5c5d0d)
1 
2 /*
3  * ng_iface.c
4  *
5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
6  * All rights reserved.
7  *
8  * Subject to the following obligations and disclaimer of warranty, use and
9  * redistribution of this software, in source or object code forms, with or
10  * without modifications are expressly permitted by Whistle Communications;
11  * provided, however, that:
12  * 1. Any and all reproductions of the source or object code must include the
13  *    copyright notice above and the following disclaimer of warranties; and
14  * 2. No rights are granted, in any manner or form, to use Whistle
15  *    Communications, Inc. trademarks, including the mark "WHISTLE
16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17  *    such appears in the above copyright notice or in the software.
18  *
19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35  * OF SUCH DAMAGE.
36  *
37  * Author: Archie Cobbs <archie@freebsd.org>
38  *
39  * $FreeBSD: src/sys/netgraph/ng_iface.c,v 1.7.2.5 2002/07/02 23:44:02 archie Exp $
40  * $DragonFly: src/sys/netgraph/iface/ng_iface.c,v 1.14 2008/01/05 14:02:39 swildner Exp $
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/sockio.h>
68 #include <sys/socket.h>
69 #include <sys/syslog.h>
70 #include <sys/libkern.h>
71 #include <sys/thread2.h>
72 
73 #include <net/if.h>
74 #include <net/if_types.h>
75 #include <net/netisr.h>
76 #include <net/bpf.h>
77 
78 #include <netinet/in.h>
79 
80 #include <netgraph/ng_message.h>
81 #include <netgraph/netgraph.h>
82 #include <netgraph/ng_parse.h>
83 #include "ng_iface.h"
84 #include <netgraph/cisco/ng_cisco.h>
85 
86 /* This struct describes one address family */
87 struct iffam {
88 	sa_family_t	family;		/* Address family */
89 	const char	*hookname;	/* Name for hook */
90 };
91 typedef const struct iffam *iffam_p;
92 
93 /* List of address families supported by our interface */
94 const static struct iffam gFamilies[] = {
95 	{ AF_INET,	NG_IFACE_HOOK_INET	},
96 	{ AF_INET6,	NG_IFACE_HOOK_INET6	},
97 	{ AF_APPLETALK,	NG_IFACE_HOOK_ATALK	},
98 	{ AF_IPX,	NG_IFACE_HOOK_IPX	},
99 	{ AF_ATM,	NG_IFACE_HOOK_ATM	},
100 	{ AF_NATM,	NG_IFACE_HOOK_NATM	},
101 	{ AF_NS,	NG_IFACE_HOOK_NS	},
102 };
103 #define NUM_FAMILIES		(sizeof(gFamilies) / sizeof(*gFamilies))
104 
105 /* Node private data */
106 struct ng_iface_private {
107 	struct	ifnet *ifp;		/* Our interface */
108 	int	unit;			/* Interface unit number */
109 	node_p	node;			/* Our netgraph node */
110 	hook_p	hooks[NUM_FAMILIES];	/* Hook for each address family */
111 };
112 typedef struct ng_iface_private *priv_p;
113 
114 /* Interface methods */
115 static void	ng_iface_start(struct ifnet *ifp);
116 static int	ng_iface_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
117 			struct ucred *cr);
118 static int	ng_iface_output(struct ifnet *ifp, struct mbuf *m0,
119 			struct sockaddr *dst, struct rtentry *rt0);
120 static void	ng_iface_bpftap(struct ifnet *ifp,
121 			struct mbuf *m, sa_family_t family);
122 #ifdef DEBUG
123 static void	ng_iface_print_ioctl(struct ifnet *ifp, int cmd, caddr_t data);
124 #endif
125 
126 /* Netgraph methods */
127 static ng_constructor_t	ng_iface_constructor;
128 static ng_rcvmsg_t	ng_iface_rcvmsg;
129 static ng_shutdown_t	ng_iface_rmnode;
130 static ng_newhook_t	ng_iface_newhook;
131 static ng_rcvdata_t	ng_iface_rcvdata;
132 static ng_disconnect_t	ng_iface_disconnect;
133 
134 /* Helper stuff */
135 static iffam_p	get_iffam_from_af(sa_family_t family);
136 static iffam_p	get_iffam_from_hook(priv_p priv, hook_p hook);
137 static iffam_p	get_iffam_from_name(const char *name);
138 static hook_p  *get_hook_from_iffam(priv_p priv, iffam_p iffam);
139 
140 /* Parse type for struct ng_iface_ifname */
141 static const struct ng_parse_fixedstring_info ng_iface_ifname_info = {
142 	NG_IFACE_IFACE_NAME_MAX + 1
143 };
144 static const struct ng_parse_type ng_iface_ifname_type = {
145 	&ng_parse_fixedstring_type,
146 	&ng_iface_ifname_info
147 };
148 
149 /* Parse type for struct ng_cisco_ipaddr */
150 static const struct ng_parse_struct_field ng_cisco_ipaddr_type_fields[]
151 	= NG_CISCO_IPADDR_TYPE_INFO;
152 static const struct ng_parse_type ng_cisco_ipaddr_type = {
153 	&ng_parse_struct_type,
154 	&ng_cisco_ipaddr_type_fields
155 };
156 
157 /* List of commands and how to convert arguments to/from ASCII */
158 static const struct ng_cmdlist ng_iface_cmds[] = {
159 	{
160 	  NGM_IFACE_COOKIE,
161 	  NGM_IFACE_GET_IFNAME,
162 	  "getifname",
163 	  NULL,
164 	  &ng_iface_ifname_type
165 	},
166 	{
167 	  NGM_IFACE_COOKIE,
168 	  NGM_IFACE_POINT2POINT,
169 	  "point2point",
170 	  NULL,
171 	  NULL
172 	},
173 	{
174 	  NGM_IFACE_COOKIE,
175 	  NGM_IFACE_BROADCAST,
176 	  "broadcast",
177 	  NULL,
178 	  NULL
179 	},
180 	{
181 	  NGM_CISCO_COOKIE,
182 	  NGM_CISCO_GET_IPADDR,
183 	  "getipaddr",
184 	  NULL,
185 	  &ng_cisco_ipaddr_type
186 	},
187 	{ 0 }
188 };
189 
190 /* Node type descriptor */
191 static struct ng_type typestruct = {
192 	NG_VERSION,
193 	NG_IFACE_NODE_TYPE,
194 	NULL,
195 	ng_iface_constructor,
196 	ng_iface_rcvmsg,
197 	ng_iface_rmnode,
198 	ng_iface_newhook,
199 	NULL,
200 	NULL,
201 	ng_iface_rcvdata,
202 	ng_iface_rcvdata,
203 	ng_iface_disconnect,
204 	ng_iface_cmds
205 };
206 NETGRAPH_INIT(iface, &typestruct);
207 
208 /* We keep a bitmap indicating which unit numbers are free.
209    One means the unit number is free, zero means it's taken. */
210 static int	*ng_iface_units = NULL;
211 static int	ng_iface_units_len = 0;
212 
213 #define UNITS_BITSPERWORD	(sizeof(*ng_iface_units) * NBBY)
214 
215 /************************************************************************
216 			HELPER STUFF
217  ************************************************************************/
218 
219 /*
220  * Get the family descriptor from the family ID
221  */
222 static __inline__ iffam_p
223 get_iffam_from_af(sa_family_t family)
224 {
225 	iffam_p iffam;
226 	int k;
227 
228 	for (k = 0; k < NUM_FAMILIES; k++) {
229 		iffam = &gFamilies[k];
230 		if (iffam->family == family)
231 			return (iffam);
232 	}
233 	return (NULL);
234 }
235 
236 /*
237  * Get the family descriptor from the hook
238  */
239 static __inline__ iffam_p
240 get_iffam_from_hook(priv_p priv, hook_p hook)
241 {
242 	int k;
243 
244 	for (k = 0; k < NUM_FAMILIES; k++)
245 		if (priv->hooks[k] == hook)
246 			return (&gFamilies[k]);
247 	return (NULL);
248 }
249 
250 /*
251  * Get the hook from the iffam descriptor
252  */
253 
254 static __inline__ hook_p *
255 get_hook_from_iffam(priv_p priv, iffam_p iffam)
256 {
257 	return (&priv->hooks[iffam - gFamilies]);
258 }
259 
260 /*
261  * Get the iffam descriptor from the name
262  */
263 static __inline__ iffam_p
264 get_iffam_from_name(const char *name)
265 {
266 	iffam_p iffam;
267 	int k;
268 
269 	for (k = 0; k < NUM_FAMILIES; k++) {
270 		iffam = &gFamilies[k];
271 		if (!strcmp(iffam->hookname, name))
272 			return (iffam);
273 	}
274 	return (NULL);
275 }
276 
277 /*
278  * Find the first free unit number for a new interface.
279  * Increase the size of the unit bitmap as necessary.
280  */
281 static __inline__ int
282 ng_iface_get_unit(int *unit)
283 {
284 	int index, bit;
285 
286 	for (index = 0; index < ng_iface_units_len
287 	    && ng_iface_units[index] == 0; index++);
288 	if (index == ng_iface_units_len) {		/* extend array */
289 		int i, *newarray, newlen;
290 
291 		newlen = (2 * ng_iface_units_len) + 4;
292 		MALLOC(newarray, int *, newlen * sizeof(*ng_iface_units),
293 		    M_NETGRAPH, M_NOWAIT);
294 		if (newarray == NULL)
295 			return (ENOMEM);
296 		bcopy(ng_iface_units, newarray,
297 		    ng_iface_units_len * sizeof(*ng_iface_units));
298 		for (i = ng_iface_units_len; i < newlen; i++)
299 			newarray[i] = ~0;
300 		if (ng_iface_units != NULL)
301 			FREE(ng_iface_units, M_NETGRAPH);
302 		ng_iface_units = newarray;
303 		ng_iface_units_len = newlen;
304 	}
305 	bit = ffs(ng_iface_units[index]) - 1;
306 	KASSERT(bit >= 0 && bit <= UNITS_BITSPERWORD - 1,
307 	    ("%s: word=%d bit=%d", __func__, ng_iface_units[index], bit));
308 	ng_iface_units[index] &= ~(1 << bit);
309 	*unit = (index * UNITS_BITSPERWORD) + bit;
310 	return (0);
311 }
312 
313 /*
314  * Free a no longer needed unit number.
315  */
316 static __inline__ void
317 ng_iface_free_unit(int unit)
318 {
319 	int index, bit;
320 
321 	index = unit / UNITS_BITSPERWORD;
322 	bit = unit % UNITS_BITSPERWORD;
323 	KASSERT(index < ng_iface_units_len,
324 	    ("%s: unit=%d len=%d", __func__, unit, ng_iface_units_len));
325 	KASSERT((ng_iface_units[index] & (1 << bit)) == 0,
326 	    ("%s: unit=%d is free", __func__, unit));
327 	ng_iface_units[index] |= (1 << bit);
328 	/*
329 	 * XXX We could think about reducing the size of ng_iface_units[]
330 	 * XXX here if the last portion is all ones
331 	 */
332 }
333 
334 /************************************************************************
335 			INTERFACE STUFF
336  ************************************************************************/
337 
338 /*
339  * Process an ioctl for the virtual interface
340  */
341 static int
342 ng_iface_ioctl(struct ifnet *ifp, u_long command, caddr_t data,
343     struct ucred *cr)
344 {
345 	struct ifreq *const ifr = (struct ifreq *) data;
346 	int error = 0;
347 
348 #ifdef DEBUG
349 	ng_iface_print_ioctl(ifp, command, data);
350 #endif
351 	crit_enter();
352 	switch (command) {
353 
354 	/* These two are mostly handled at a higher layer */
355 	case SIOCSIFADDR:
356 		ifp->if_flags |= (IFF_UP | IFF_RUNNING);
357 		ifp->if_flags &= ~(IFF_OACTIVE);
358 		break;
359 	case SIOCGIFADDR:
360 		break;
361 
362 	/* Set flags */
363 	case SIOCSIFFLAGS:
364 		/*
365 		 * If the interface is marked up and stopped, then start it.
366 		 * If it is marked down and running, then stop it.
367 		 */
368 		if (ifr->ifr_flags & IFF_UP) {
369 			if (!(ifp->if_flags & IFF_RUNNING)) {
370 				ifp->if_flags &= ~(IFF_OACTIVE);
371 				ifp->if_flags |= IFF_RUNNING;
372 			}
373 		} else {
374 			if (ifp->if_flags & IFF_RUNNING)
375 				ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
376 		}
377 		break;
378 
379 	/* Set the interface MTU */
380 	case SIOCSIFMTU:
381 		if (ifr->ifr_mtu > NG_IFACE_MTU_MAX
382 		    || ifr->ifr_mtu < NG_IFACE_MTU_MIN)
383 			error = EINVAL;
384 		else
385 			ifp->if_mtu = ifr->ifr_mtu;
386 		break;
387 
388 	/* Stuff that's not supported */
389 	case SIOCADDMULTI:
390 	case SIOCDELMULTI:
391 		error = 0;
392 		break;
393 	case SIOCSIFPHYS:
394 		error = EOPNOTSUPP;
395 		break;
396 
397 	default:
398 		error = EINVAL;
399 		break;
400 	}
401 	crit_exit();
402 	return (error);
403 }
404 
405 /*
406  * This routine is called to deliver a packet out the interface.
407  * We simply look at the address family and relay the packet to
408  * the corresponding hook, if it exists and is connected.
409  */
410 
411 static int
412 ng_iface_output(struct ifnet *ifp, struct mbuf *m,
413 		struct sockaddr *dst, struct rtentry *rt0)
414 {
415 	const priv_p priv = (priv_p) ifp->if_softc;
416 	const iffam_p iffam = get_iffam_from_af(dst->sa_family);
417 	meta_p meta = NULL;
418 	int len, error = 0;
419 
420 	/* Check interface flags */
421 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
422 		m_freem(m);
423 		return (ENETDOWN);
424 	}
425 
426 	/* BPF writes need to be handled specially */
427 	if (dst->sa_family == AF_UNSPEC) {
428 		if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
429 			return (ENOBUFS);
430 		dst->sa_family = (sa_family_t)*mtod(m, int32_t *);
431 		m->m_data += 4;
432 		m->m_len -= 4;
433 		m->m_pkthdr.len -= 4;
434 	}
435 
436 	/* Berkeley packet filter */
437 	ng_iface_bpftap(ifp, m, dst->sa_family);
438 
439 	/* Check address family to determine hook (if known) */
440 	if (iffam == NULL) {
441 		m_freem(m);
442 		log(LOG_WARNING, "%s: can't handle af%d\n",
443 		       ifp->if_xname, (int)dst->sa_family);
444 		return (EAFNOSUPPORT);
445 	}
446 
447 	/* Copy length before the mbuf gets invalidated */
448 	len = m->m_pkthdr.len;
449 
450 	/* Send packet; if hook is not connected, mbuf will get freed. */
451 	NG_SEND_DATA(error, *get_hook_from_iffam(priv, iffam), m, meta);
452 
453 	/* Update stats */
454 	if (error == 0) {
455 		ifp->if_obytes += len;
456 		ifp->if_opackets++;
457 	}
458 	return (error);
459 }
460 
461 /*
462  * This routine should never be called
463  */
464 
465 static void
466 ng_iface_start(struct ifnet *ifp)
467 {
468 	kprintf("%s: %s called?", ifp->if_xname, __func__);
469 }
470 
471 /*
472  * Flash a packet by the BPF (requires prepending 4 byte AF header)
473  */
474 static void
475 ng_iface_bpftap(struct ifnet *ifp, struct mbuf *m, sa_family_t family)
476 {
477 	int32_t family4 = (int32_t)family;
478 
479 	KASSERT(family != AF_UNSPEC, ("%s: family=AF_UNSPEC", __func__));
480 
481 	if (ifp->if_bpf)
482 		bpf_ptap(ifp->if_bpf, m, &family, sizeof(family4));
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 *nodep)
529 {
530 	char ifname[NG_IFACE_IFACE_NAME_MAX + 1];
531 	struct ifnet *ifp;
532 	node_p node;
533 	priv_p priv;
534 	int error = 0;
535 
536 	/* Allocate node and interface private structures */
537 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
538 	if (priv == NULL)
539 		return (ENOMEM);
540 	MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_NETGRAPH, M_NOWAIT | M_ZERO);
541 	if (ifp == NULL) {
542 		FREE(priv, M_NETGRAPH);
543 		return (ENOMEM);
544 	}
545 
546 	/* Link them together */
547 	ifp->if_softc = priv;
548 	priv->ifp = ifp;
549 
550 	/* Get an interface unit number */
551 	if ((error = ng_iface_get_unit(&priv->unit)) != 0) {
552 		FREE(ifp, M_NETGRAPH);
553 		FREE(priv, M_NETGRAPH);
554 		return (error);
555 	}
556 
557 	/* Call generic node constructor */
558 	if ((error = ng_make_node_common(&typestruct, nodep)) != 0) {
559 		ng_iface_free_unit(priv->unit);
560 		FREE(ifp, M_NETGRAPH);
561 		FREE(priv, M_NETGRAPH);
562 		return (error);
563 	}
564 	node = *nodep;
565 
566 	/* Link together node and private info */
567 	node->private = priv;
568 	priv->node = node;
569 
570 	/* Initialize interface structure */
571 	if_initname(ifp, NG_IFACE_IFACE_NAME, priv->unit);
572 	ifp->if_output = ng_iface_output;
573 	ifp->if_start = ng_iface_start;
574 	ifp->if_ioctl = ng_iface_ioctl;
575 	ifp->if_watchdog = NULL;
576 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
577 	ifp->if_mtu = NG_IFACE_MTU_DEFAULT;
578 	ifp->if_flags = (IFF_SIMPLEX|IFF_POINTOPOINT|IFF_NOARP|IFF_MULTICAST);
579 	ifp->if_type = IFT_PROPVIRTUAL;		/* XXX */
580 	ifp->if_addrlen = 0;			/* XXX */
581 	ifp->if_hdrlen = 0;			/* XXX */
582 	ifp->if_baudrate = 64000;		/* XXX */
583 	TAILQ_INIT(&ifp->if_addrhead);
584 
585 	/* Give this node the same name as the interface (if possible) */
586 	bzero(ifname, sizeof(ifname));
587 	strlcpy(ifname, ifp->if_xname, sizeof(ifname));
588 	if (ng_name_node(node, ifname) != 0)
589 		log(LOG_WARNING, "%s: can't acquire netgraph name\n", ifname);
590 
591 	/* Attach the interface */
592 	if_attach(ifp, NULL);
593 	bpfattach(ifp, DLT_NULL, sizeof(u_int));
594 
595 	/* Done */
596 	return (0);
597 }
598 
599 /*
600  * Give our ok for a hook to be added
601  */
602 static int
603 ng_iface_newhook(node_p node, hook_p hook, const char *name)
604 {
605 	const iffam_p iffam = get_iffam_from_name(name);
606 	hook_p *hookptr;
607 
608 	if (iffam == NULL)
609 		return (EPFNOSUPPORT);
610 	hookptr = get_hook_from_iffam((priv_p) node->private, iffam);
611 	if (*hookptr != NULL)
612 		return (EISCONN);
613 	*hookptr = hook;
614 	return (0);
615 }
616 
617 /*
618  * Receive a control message
619  */
620 static int
621 ng_iface_rcvmsg(node_p node, struct ng_mesg *msg,
622 		const char *retaddr, struct ng_mesg **rptr)
623 {
624 	const priv_p priv = node->private;
625 	struct ifnet *const ifp = priv->ifp;
626 	struct ng_mesg *resp = NULL;
627 	int error = 0;
628 
629 	switch (msg->header.typecookie) {
630 	case NGM_IFACE_COOKIE:
631 		switch (msg->header.cmd) {
632 		case NGM_IFACE_GET_IFNAME:
633 		    {
634 			struct ng_iface_ifname *arg;
635 
636 			NG_MKRESPONSE(resp, msg, sizeof(*arg), M_NOWAIT);
637 			if (resp == NULL) {
638 				error = ENOMEM;
639 				break;
640 			}
641 			arg = (struct ng_iface_ifname *)resp->data;
642 			strlcpy(arg->ngif_name, ifp->if_xname,
643 			    sizeof(arg->ngif_name));
644 			break;
645 		    }
646 
647 		case NGM_IFACE_POINT2POINT:
648 		case NGM_IFACE_BROADCAST:
649 		    {
650 
651 			/* Deny request if interface is UP */
652 			if ((ifp->if_flags & IFF_UP) != 0)
653 				return (EBUSY);
654 
655 			/* Change flags */
656 			switch (msg->header.cmd) {
657 			case NGM_IFACE_POINT2POINT:
658 				ifp->if_flags |= IFF_POINTOPOINT;
659 				ifp->if_flags &= ~IFF_BROADCAST;
660 				break;
661 			case NGM_IFACE_BROADCAST:
662 				ifp->if_flags &= ~IFF_POINTOPOINT;
663 				ifp->if_flags |= IFF_BROADCAST;
664 				break;
665 			}
666 			break;
667 		    }
668 
669 		default:
670 			error = EINVAL;
671 			break;
672 		}
673 		break;
674 	case NGM_CISCO_COOKIE:
675 		switch (msg->header.cmd) {
676 		case NGM_CISCO_GET_IPADDR:	/* we understand this too */
677 		    {
678 			struct ifaddr *ifa;
679 
680 			/* Return the first configured IP address */
681 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
682 				struct ng_cisco_ipaddr *ips;
683 
684 				if (ifa->ifa_addr->sa_family != AF_INET)
685 					continue;
686 				NG_MKRESPONSE(resp, msg, sizeof(ips), M_NOWAIT);
687 				if (resp == NULL) {
688 					error = ENOMEM;
689 					break;
690 				}
691 				ips = (struct ng_cisco_ipaddr *)resp->data;
692 				ips->ipaddr = ((struct sockaddr_in *)
693 						ifa->ifa_addr)->sin_addr;
694 				ips->netmask = ((struct sockaddr_in *)
695 						ifa->ifa_netmask)->sin_addr;
696 				break;
697 			}
698 
699 			/* No IP addresses on this interface? */
700 			if (ifa == NULL)
701 				error = EADDRNOTAVAIL;
702 			break;
703 		    }
704 		default:
705 			error = EINVAL;
706 			break;
707 		}
708 		break;
709 	default:
710 		error = EINVAL;
711 		break;
712 	}
713 	if (rptr)
714 		*rptr = resp;
715 	else if (resp)
716 		FREE(resp, M_NETGRAPH);
717 	FREE(msg, M_NETGRAPH);
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, struct mbuf *m, meta_p meta)
726 {
727 	const priv_p priv = hook->node->private;
728 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
729 	struct ifnet *const ifp = priv->ifp;
730 	int isr;
731 
732 	/* Sanity checks */
733 	KASSERT(iffam != NULL, ("%s: iffam", __func__));
734 	KASSERT(m->m_flags & M_PKTHDR, ("%s: not pkthdr", __func__));
735 	if (m == NULL)
736 		return (EINVAL);
737 	if ((ifp->if_flags & IFF_UP) == 0) {
738 		NG_FREE_DATA(m, meta);
739 		return (ENETDOWN);
740 	}
741 
742 	/* Update interface stats */
743 	ifp->if_ipackets++;
744 	ifp->if_ibytes += m->m_pkthdr.len;
745 
746 	/* Note receiving interface */
747 	m->m_pkthdr.rcvif = ifp;
748 
749 	/* Berkeley packet filter */
750 	ng_iface_bpftap(ifp, m, iffam->family);
751 
752 	/* Ignore any meta-data */
753 	NG_FREE_META(meta);
754 
755 	/* Send packet */
756 	switch (iffam->family) {
757 #ifdef INET
758 	case AF_INET:
759 		isr = NETISR_IP;
760 		break;
761 #endif
762 #ifdef INET6
763 	case AF_INET6:
764 		isr = NETISR_IPV6;
765 		break;
766 #endif
767 #ifdef IPX
768 	case AF_IPX:
769 		isr = NETISR_IPX;
770 		break;
771 #endif
772 #ifdef NETATALK
773 	case AF_APPLETALK:
774 		isr = NETISR_ATALK2;
775 		break;
776 #endif
777 	default:
778 		m_freem(m);
779 		return (EAFNOSUPPORT);
780 	}
781 	netisr_dispatch(isr, m);
782 	return (0);
783 }
784 
785 /*
786  * Shutdown and remove the node and its associated interface.
787  */
788 static int
789 ng_iface_rmnode(node_p node)
790 {
791 	const priv_p priv = node->private;
792 
793 	ng_cutlinks(node);
794 	ng_unname(node);
795 	bpfdetach(priv->ifp);
796 	if_detach(priv->ifp);
797 	FREE(priv->ifp, M_NETGRAPH);
798 	priv->ifp = NULL;
799 	ng_iface_free_unit(priv->unit);
800 	FREE(priv, M_NETGRAPH);
801 	node->private = NULL;
802 	ng_unref(node);
803 	return (0);
804 }
805 
806 /*
807  * Hook disconnection. Note that we do *not* shutdown when all
808  * hooks have been disconnected.
809  */
810 static int
811 ng_iface_disconnect(hook_p hook)
812 {
813 	const priv_p priv = hook->node->private;
814 	const iffam_p iffam = get_iffam_from_hook(priv, hook);
815 
816 	if (iffam == NULL)
817 		panic(__func__);
818 	*get_hook_from_iffam(priv, iffam) = NULL;
819 	return (0);
820 }
821 
822