if.c revision 126264
1/*
2 * Copyright (c) 1980, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)if.c	8.5 (Berkeley) 1/9/95
34 * $FreeBSD: head/sys/net/if.c 126264 2004-02-26 04:27:55Z mlaier $
35 */
36
37#include "opt_compat.h"
38#include "opt_inet6.h"
39#include "opt_inet.h"
40#include "opt_mac.h"
41
42#include <sys/param.h>
43#include <sys/conf.h>
44#include <sys/mac.h>
45#include <sys/malloc.h>
46#include <sys/bus.h>
47#include <sys/mbuf.h>
48#include <sys/systm.h>
49#include <sys/proc.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/protosw.h>
53#include <sys/kernel.h>
54#include <sys/sockio.h>
55#include <sys/syslog.h>
56#include <sys/sysctl.h>
57#include <sys/domain.h>
58#include <sys/jail.h>
59#include <machine/stdarg.h>
60
61#include <net/if.h>
62#include <net/if_arp.h>
63#include <net/if_dl.h>
64#include <net/if_types.h>
65#include <net/if_var.h>
66#include <net/radix.h>
67#include <net/route.h>
68
69#if defined(INET) || defined(INET6)
70/*XXX*/
71#include <netinet/in.h>
72#include <netinet/in_var.h>
73#ifdef INET6
74#include <netinet6/in6_var.h>
75#include <netinet6/in6_ifattach.h>
76#endif
77#endif
78#ifdef INET
79#include <netinet/if_ether.h>
80#endif
81
82static void	if_attachdomain(void *);
83static void	if_attachdomain1(struct ifnet *);
84static int	ifconf(u_long, caddr_t);
85static void	if_grow(void);
86static void	if_init(void *);
87static void	if_check(void *);
88static int	if_findindex(struct ifnet *);
89static void	if_qflush(struct ifqueue *);
90static void	if_slowtimo(void *);
91static void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
92static int	if_rtdel(struct radix_node *, void *);
93static struct	if_clone *if_clone_lookup(const char *, int *);
94static int	if_clone_list(struct if_clonereq *);
95static int	ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
96#ifdef INET6
97/*
98 * XXX: declare here to avoid to include many inet6 related files..
99 * should be more generalized?
100 */
101extern void	nd6_setmtu(struct ifnet *);
102#endif
103
104int	if_index = 0;
105struct	ifindex_entry *ifindex_table = NULL;
106int	ifqmaxlen = IFQ_MAXLEN;
107struct	ifnethead ifnet;	/* depend on static init XXX */
108struct	mtx ifnet_lock;
109static int	if_cloners_count;
110LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
111
112static int	if_indexlim = 8;
113static struct	klist ifklist;
114
115static void	filt_netdetach(struct knote *kn);
116static int	filt_netdev(struct knote *kn, long hint);
117
118static struct filterops netdev_filtops =
119    { 1, NULL, filt_netdetach, filt_netdev };
120
121/*
122 * System initialization
123 */
124SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
125SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
126
127MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
128MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
129MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
130
131static d_open_t		netopen;
132static d_close_t	netclose;
133static d_ioctl_t	netioctl;
134static d_kqfilter_t	netkqfilter;
135
136static struct cdevsw net_cdevsw = {
137	.d_version =	D_VERSION,
138	.d_flags =	D_NEEDGIANT,
139	.d_open =	netopen,
140	.d_close =	netclose,
141	.d_ioctl =	netioctl,
142	.d_name =	"net",
143	.d_kqfilter =	netkqfilter,
144};
145
146static int
147netopen(dev_t dev, int flag, int mode, struct thread *td)
148{
149	return (0);
150}
151
152static int
153netclose(dev_t dev, int flags, int fmt, struct thread *td)
154{
155	return (0);
156}
157
158static int
159netioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
160{
161	struct ifnet *ifp;
162	int error, idx;
163
164	/* only support interface specific ioctls */
165	if (IOCGROUP(cmd) != 'i')
166		return (EOPNOTSUPP);
167	idx = minor(dev);
168	if (idx == 0) {
169		/*
170		 * special network device, not interface.
171		 */
172		if (cmd == SIOCGIFCONF)
173			return (ifconf(cmd, data));	/* XXX remove cmd */
174		return (EOPNOTSUPP);
175	}
176
177	ifp = ifnet_byindex(idx);
178	if (ifp == NULL)
179		return (ENXIO);
180
181	error = ifhwioctl(cmd, ifp, data, td);
182	if (error == ENOIOCTL)
183		error = EOPNOTSUPP;
184	return (error);
185}
186
187static int
188netkqfilter(dev_t dev, struct knote *kn)
189{
190	struct klist *klist;
191	struct ifnet *ifp;
192	int idx;
193
194	idx = minor(dev);
195	if (idx == 0) {
196		klist = &ifklist;
197	} else {
198		ifp = ifnet_byindex(idx);
199		if (ifp == NULL)
200			return (1);
201		klist = &ifp->if_klist;
202	}
203
204	switch (kn->kn_filter) {
205	case EVFILT_NETDEV:
206		kn->kn_fop = &netdev_filtops;
207		break;
208	default:
209		return (1);
210	}
211
212	kn->kn_hook = (caddr_t)klist;
213
214	/* XXX locking? */
215	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
216
217	return (0);
218}
219
220static void
221filt_netdetach(struct knote *kn)
222{
223	struct klist *klist = (struct klist *)kn->kn_hook;
224
225	if (kn->kn_status & KN_DETACHED)
226		return;
227	SLIST_REMOVE(klist, kn, knote, kn_selnext);
228}
229
230static int
231filt_netdev(struct knote *kn, long hint)
232{
233
234	/*
235	 * Currently NOTE_EXIT is abused to indicate device detach.
236	 */
237	if (hint == NOTE_EXIT) {
238		kn->kn_data = NOTE_LINKINV;
239		kn->kn_status |= KN_DETACHED;
240		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
241		return (1);
242	}
243	kn->kn_data = hint;			/* current status */
244	if (kn->kn_sfflags & hint)
245		kn->kn_fflags |= hint;
246	return (kn->kn_fflags != 0);
247}
248
249/*
250 * Network interface utility routines.
251 *
252 * Routines with ifa_ifwith* names take sockaddr *'s as
253 * parameters.
254 */
255/* ARGSUSED*/
256static void
257if_init(void *dummy __unused)
258{
259
260	IFNET_LOCK_INIT();
261	TAILQ_INIT(&ifnet);
262	SLIST_INIT(&ifklist);
263	if_grow();				/* create initial table */
264	ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
265	    UID_ROOT, GID_WHEEL, 0600, "network");
266}
267
268static void
269if_grow(void)
270{
271	u_int n;
272	struct ifindex_entry *e;
273
274	if_indexlim <<= 1;
275	n = if_indexlim * sizeof(*e);
276	e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
277	if (ifindex_table != NULL) {
278		memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
279		free((caddr_t)ifindex_table, M_IFADDR);
280	}
281	ifindex_table = e;
282}
283
284/* ARGSUSED*/
285static void
286if_check(void *dummy __unused)
287{
288	struct ifnet *ifp;
289	int s;
290
291	s = splimp();
292	IFNET_RLOCK();	/* could sleep on rare error; mostly okay XXX */
293	TAILQ_FOREACH(ifp, &ifnet, if_link) {
294		if (ifp->if_snd.ifq_maxlen == 0) {
295			if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
296			ifp->if_snd.ifq_maxlen = ifqmaxlen;
297		}
298		if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
299			if_printf(ifp,
300			    "XXX: driver didn't initialize queue mtx\n");
301			mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
302			    MTX_NETWORK_LOCK, MTX_DEF);
303		}
304	}
305	IFNET_RUNLOCK();
306	splx(s);
307	if_slowtimo(0);
308}
309
310static int
311if_findindex(struct ifnet *ifp)
312{
313	int i, unit;
314	char eaddr[18], devname[32];
315	const char *name, *p;
316
317	switch (ifp->if_type) {
318	case IFT_ETHER:			/* these types use struct arpcom */
319	case IFT_FDDI:
320	case IFT_XETHER:
321	case IFT_ISO88025:
322	case IFT_L2VLAN:
323		snprintf(eaddr, 18, "%6D",
324		    ((struct arpcom *)ifp->if_softc)->ac_enaddr, ":");
325		break;
326	default:
327		eaddr[0] = '\0';
328		break;
329	}
330	strlcpy(devname, ifp->if_xname, sizeof(devname));
331	name = net_cdevsw.d_name;
332	i = 0;
333	while ((resource_find_dev(&i, name, &unit, NULL, NULL)) == 0) {
334		if (resource_string_value(name, unit, "ether", &p) == 0)
335			if (strcmp(p, eaddr) == 0)
336				goto found;
337		if (resource_string_value(name, unit, "dev", &p) == 0)
338			if (strcmp(p, devname) == 0)
339				goto found;
340	}
341	unit = 0;
342found:
343	if (unit != 0) {
344		if (ifaddr_byindex(unit) == NULL)
345			return (unit);
346		printf("%s%d in use, cannot hardwire it to %s.\n",
347		    name, unit, devname);
348	}
349	for (unit = 1; ; unit++) {
350		if (unit <= if_index && ifaddr_byindex(unit) != NULL)
351			continue;
352		if (resource_string_value(name, unit, "ether", &p) == 0 ||
353		    resource_string_value(name, unit, "dev", &p) == 0)
354			continue;
355		break;
356	}
357	return (unit);
358}
359
360/*
361 * Attach an interface to the
362 * list of "active" interfaces.
363 */
364void
365if_attach(struct ifnet *ifp)
366{
367	unsigned socksize, ifasize;
368	int namelen, masklen;
369	struct sockaddr_dl *sdl;
370	struct ifaddr *ifa;
371
372	IF_AFDATA_LOCK_INIT(ifp);
373	ifp->if_afdata_initialized = 0;
374	IFNET_WLOCK();
375	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
376	IFNET_WUNLOCK();
377	/*
378	 * XXX -
379	 * The old code would work if the interface passed a pre-existing
380	 * chain of ifaddrs to this code.  We don't trust our callers to
381	 * properly initialize the tailq, however, so we no longer allow
382	 * this unlikely case.
383	 */
384	TAILQ_INIT(&ifp->if_addrhead);
385	TAILQ_INIT(&ifp->if_prefixhead);
386	TAILQ_INIT(&ifp->if_multiaddrs);
387	SLIST_INIT(&ifp->if_klist);
388	getmicrotime(&ifp->if_lastchange);
389
390#ifdef MAC
391	mac_init_ifnet(ifp);
392	mac_create_ifnet(ifp);
393#endif
394
395	ifp->if_index = if_findindex(ifp);
396	if (ifp->if_index > if_index)
397		if_index = ifp->if_index;
398	if (if_index >= if_indexlim)
399		if_grow();
400
401	ifnet_byindex(ifp->if_index) = ifp;
402	ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw,
403	    unit2minor(ifp->if_index),
404	    UID_ROOT, GID_WHEEL, 0600, "%s/%s",
405	    net_cdevsw.d_name, ifp->if_xname);
406	make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
407	    net_cdevsw.d_name, ifp->if_index);
408
409	mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF);
410
411	/*
412	 * create a Link Level name for this device
413	 */
414	namelen = strlen(ifp->if_xname);
415	/*
416	 * Always save enough space for any possiable name so we can do
417	 * a rename in place later.
418	 */
419	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ;
420	socksize = masklen + ifp->if_addrlen;
421	if (socksize < sizeof(*sdl))
422		socksize = sizeof(*sdl);
423	socksize = roundup2(socksize, sizeof(long));
424	ifasize = sizeof(*ifa) + 2 * socksize;
425	ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
426	IFA_LOCK_INIT(ifa);
427	sdl = (struct sockaddr_dl *)(ifa + 1);
428	sdl->sdl_len = socksize;
429	sdl->sdl_family = AF_LINK;
430	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
431	sdl->sdl_nlen = namelen;
432	sdl->sdl_index = ifp->if_index;
433	sdl->sdl_type = ifp->if_type;
434	ifaddr_byindex(ifp->if_index) = ifa;
435	ifa->ifa_ifp = ifp;
436	ifa->ifa_rtrequest = link_rtrequest;
437	ifa->ifa_addr = (struct sockaddr *)sdl;
438	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
439	ifa->ifa_netmask = (struct sockaddr *)sdl;
440	sdl->sdl_len = masklen;
441	while (namelen != 0)
442		sdl->sdl_data[--namelen] = 0xff;
443	ifa->ifa_refcnt = 1;
444	TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
445	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
446
447	if (domains)
448		if_attachdomain1(ifp);
449
450	EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
451
452	/* Announce the interface. */
453	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
454}
455
456static void
457if_attachdomain(void *dummy)
458{
459	struct ifnet *ifp;
460	int s;
461
462	s = splnet();
463	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
464		if_attachdomain1(ifp);
465	splx(s);
466}
467SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
468    if_attachdomain, NULL);
469
470static void
471if_attachdomain1(struct ifnet *ifp)
472{
473	struct domain *dp;
474	int s;
475
476	s = splnet();
477
478	/*
479	 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we
480	 * cannot lock ifp->if_afdata initialization, entirely.
481	 */
482	if (IF_AFDATA_TRYLOCK(ifp) == 0) {
483		splx(s);
484		return;
485	}
486	if (ifp->if_afdata_initialized) {
487		IF_AFDATA_UNLOCK(ifp);
488		splx(s);
489		return;
490	}
491	ifp->if_afdata_initialized = 1;
492	IF_AFDATA_UNLOCK(ifp);
493
494	/* address family dependent data region */
495	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
496	for (dp = domains; dp; dp = dp->dom_next) {
497		if (dp->dom_ifattach)
498			ifp->if_afdata[dp->dom_family] =
499			    (*dp->dom_ifattach)(ifp);
500	}
501
502	splx(s);
503}
504
505/*
506 * Detach an interface, removing it from the
507 * list of "active" interfaces.
508 */
509void
510if_detach(struct ifnet *ifp)
511{
512	struct ifaddr *ifa, *next;
513	struct radix_node_head	*rnh;
514	int s;
515	int i;
516	struct domain *dp;
517
518	EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
519	/*
520	 * Remove routes and flush queues.
521	 */
522	s = splnet();
523	if_down(ifp);
524
525	/*
526	 * Remove address from ifindex_table[] and maybe decrement if_index.
527	 * Clean up all addresses.
528	 */
529	ifaddr_byindex(ifp->if_index) = NULL;
530	destroy_dev(ifdev_byindex(ifp->if_index));
531	ifdev_byindex(ifp->if_index) = NULL;
532
533	while (if_index > 0 && ifaddr_byindex(if_index) == NULL)
534		if_index--;
535
536	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; ifa = next) {
537		next = TAILQ_NEXT(ifa, ifa_link);
538
539		if (ifa->ifa_addr->sa_family == AF_LINK)
540			continue;
541#ifdef INET
542		/* XXX: Ugly!! ad hoc just for INET */
543		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
544			struct ifaliasreq ifr;
545
546			bzero(&ifr, sizeof(ifr));
547			ifr.ifra_addr = *ifa->ifa_addr;
548			if (ifa->ifa_dstaddr)
549				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
550			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
551			    NULL) == 0)
552				continue;
553		}
554#endif /* INET */
555#ifdef INET6
556		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
557			in6_purgeaddr(ifa);
558			/* ifp_addrhead is already updated */
559			continue;
560		}
561#endif /* INET6 */
562		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
563		IFAFREE(ifa);
564	}
565
566#ifdef INET6
567	/*
568	 * Remove all IPv6 kernel structs related to ifp.  This should be done
569	 * before removing routing entries below, since IPv6 interface direct
570	 * routes are expected to be removed by the IPv6-specific kernel API.
571	 * Otherwise, the kernel will detect some inconsistency and bark it.
572	 */
573	in6_ifdetach(ifp);
574#endif
575
576	/* We can now free link ifaddr. */
577	ifa = TAILQ_FIRST(&ifp->if_addrhead);
578	TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
579	IFAFREE(ifa);
580
581	/*
582	 * Delete all remaining routes using this interface
583	 * Unfortuneatly the only way to do this is to slog through
584	 * the entire routing table looking for routes which point
585	 * to this interface...oh well...
586	 */
587	for (i = 1; i <= AF_MAX; i++) {
588		if ((rnh = rt_tables[i]) == NULL)
589			continue;
590		RADIX_NODE_HEAD_LOCK(rnh);
591		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
592		RADIX_NODE_HEAD_UNLOCK(rnh);
593	}
594
595	/* Announce that the interface is gone. */
596	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
597
598	IF_AFDATA_LOCK(ifp);
599	for (dp = domains; dp; dp = dp->dom_next) {
600		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
601			(*dp->dom_ifdetach)(ifp,
602			    ifp->if_afdata[dp->dom_family]);
603	}
604	IF_AFDATA_UNLOCK(ifp);
605
606#ifdef MAC
607	mac_destroy_ifnet(ifp);
608#endif /* MAC */
609	KNOTE(&ifp->if_klist, NOTE_EXIT);
610	IFNET_WLOCK();
611	TAILQ_REMOVE(&ifnet, ifp, if_link);
612	IFNET_WUNLOCK();
613	mtx_destroy(&ifp->if_snd.ifq_mtx);
614	IF_AFDATA_DESTROY(ifp);
615	splx(s);
616}
617
618/*
619 * Delete Routes for a Network Interface
620 *
621 * Called for each routing entry via the rnh->rnh_walktree() call above
622 * to delete all route entries referencing a detaching network interface.
623 *
624 * Arguments:
625 *	rn	pointer to node in the routing table
626 *	arg	argument passed to rnh->rnh_walktree() - detaching interface
627 *
628 * Returns:
629 *	0	successful
630 *	errno	failed - reason indicated
631 *
632 */
633static int
634if_rtdel(struct radix_node *rn, void *arg)
635{
636	struct rtentry	*rt = (struct rtentry *)rn;
637	struct ifnet	*ifp = arg;
638	int		err;
639
640	if (rt->rt_ifp == ifp) {
641
642		/*
643		 * Protect (sorta) against walktree recursion problems
644		 * with cloned routes
645		 */
646		if ((rt->rt_flags & RTF_UP) == 0)
647			return (0);
648
649		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
650				rt_mask(rt), rt->rt_flags,
651				(struct rtentry **) NULL);
652		if (err) {
653			log(LOG_WARNING, "if_rtdel: error %d\n", err);
654		}
655	}
656
657	return (0);
658}
659
660/*
661 * Create a clone network interface.
662 */
663int
664if_clone_create(char *name, int len)
665{
666	struct if_clone *ifc;
667	char *dp;
668	int wildcard, bytoff, bitoff;
669	int unit;
670	int err;
671
672	ifc = if_clone_lookup(name, &unit);
673	if (ifc == NULL)
674		return (EINVAL);
675
676	if (ifunit(name) != NULL)
677		return (EEXIST);
678
679	bytoff = bitoff = 0;
680	wildcard = (unit < 0);
681	/*
682	 * Find a free unit if none was given.
683	 */
684	if (wildcard) {
685		while ((bytoff < ifc->ifc_bmlen)
686		    && (ifc->ifc_units[bytoff] == 0xff))
687			bytoff++;
688		if (bytoff >= ifc->ifc_bmlen)
689			return (ENOSPC);
690		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
691			bitoff++;
692		unit = (bytoff << 3) + bitoff;
693	}
694
695	if (unit > ifc->ifc_maxunit)
696		return (ENXIO);
697
698	err = (*ifc->ifc_create)(ifc, unit);
699	if (err != 0)
700		return (err);
701
702	if (!wildcard) {
703		bytoff = unit >> 3;
704		bitoff = unit - (bytoff << 3);
705	}
706
707	/*
708	 * Allocate the unit in the bitmap.
709	 */
710	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
711	    ("%s: bit is already set", __func__));
712	ifc->ifc_units[bytoff] |= (1 << bitoff);
713
714	/* In the wildcard case, we need to update the name. */
715	if (wildcard) {
716		for (dp = name; *dp != '\0'; dp++);
717		if (snprintf(dp, len - (dp-name), "%d", unit) >
718		    len - (dp-name) - 1) {
719			/*
720			 * This can only be a programmer error and
721			 * there's no straightforward way to recover if
722			 * it happens.
723			 */
724			panic("if_clone_create(): interface name too long");
725		}
726
727	}
728
729	return (0);
730}
731
732/*
733 * Destroy a clone network interface.
734 */
735int
736if_clone_destroy(const char *name)
737{
738	struct if_clone *ifc;
739	struct ifnet *ifp;
740	int bytoff, bitoff;
741	int unit;
742
743	ifp = ifunit(name);
744	if (ifp == NULL)
745		return (ENXIO);
746
747	unit = ifp->if_dunit;
748
749	ifc = if_clone_lookup(ifp->if_dname, NULL);
750	if (ifc == NULL)
751		return (EINVAL);
752
753	if (ifc->ifc_destroy == NULL)
754		return (EOPNOTSUPP);
755
756	(*ifc->ifc_destroy)(ifp);
757
758	/*
759	 * Compute offset in the bitmap and deallocate the unit.
760	 */
761	bytoff = unit >> 3;
762	bitoff = unit - (bytoff << 3);
763	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
764	    ("%s: bit is already cleared", __func__));
765	ifc->ifc_units[bytoff] &= ~(1 << bitoff);
766	return (0);
767}
768
769/*
770 * Look up a network interface cloner.
771 */
772static struct if_clone *
773if_clone_lookup(const char *name, int *unitp)
774{
775	struct if_clone *ifc;
776	const char *cp;
777	int i;
778
779	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
780		for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
781			if (ifc->ifc_name[i] != *cp)
782				goto next_ifc;
783		}
784		goto found_name;
785 next_ifc:
786		ifc = LIST_NEXT(ifc, ifc_list);
787	}
788
789	/* No match. */
790	return ((struct if_clone *)NULL);
791
792 found_name:
793	if (*cp == '\0') {
794		i = -1;
795	} else {
796		for (i = 0; *cp != '\0'; cp++) {
797			if (*cp < '0' || *cp > '9') {
798				/* Bogus unit number. */
799				return (NULL);
800			}
801			i = (i * 10) + (*cp - '0');
802		}
803	}
804
805	if (unitp != NULL)
806		*unitp = i;
807	return (ifc);
808}
809
810/*
811 * Register a network interface cloner.
812 */
813void
814if_clone_attach(struct if_clone *ifc)
815{
816	int bytoff, bitoff;
817	int err;
818	int len, maxclone;
819	int unit;
820
821	KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
822	    ("%s: %s requested more units then allowed (%d > %d)",
823	    __func__, ifc->ifc_name, ifc->ifc_minifs,
824	    ifc->ifc_maxunit + 1));
825	/*
826	 * Compute bitmap size and allocate it.
827	 */
828	maxclone = ifc->ifc_maxunit + 1;
829	len = maxclone >> 3;
830	if ((len << 3) < maxclone)
831		len++;
832	ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
833	ifc->ifc_bmlen = len;
834
835	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
836	if_cloners_count++;
837
838	for (unit = 0; unit < ifc->ifc_minifs; unit++) {
839		err = (*ifc->ifc_create)(ifc, unit);
840		KASSERT(err == 0,
841		    ("%s: failed to create required interface %s%d",
842		    __func__, ifc->ifc_name, unit));
843
844		/* Allocate the unit in the bitmap. */
845		bytoff = unit >> 3;
846		bitoff = unit - (bytoff << 3);
847		ifc->ifc_units[bytoff] |= (1 << bitoff);
848	}
849	EVENTHANDLER_INVOKE(if_clone_event, ifc);
850}
851
852/*
853 * Unregister a network interface cloner.
854 */
855void
856if_clone_detach(struct if_clone *ifc)
857{
858
859	LIST_REMOVE(ifc, ifc_list);
860	free(ifc->ifc_units, M_CLONE);
861	if_cloners_count--;
862}
863
864/*
865 * Provide list of interface cloners to userspace.
866 */
867static int
868if_clone_list(struct if_clonereq *ifcr)
869{
870	char outbuf[IFNAMSIZ], *dst;
871	struct if_clone *ifc;
872	int count, error = 0;
873
874	ifcr->ifcr_total = if_cloners_count;
875	if ((dst = ifcr->ifcr_buffer) == NULL) {
876		/* Just asking how many there are. */
877		return (0);
878	}
879
880	if (ifcr->ifcr_count < 0)
881		return (EINVAL);
882
883	count = (if_cloners_count < ifcr->ifcr_count) ?
884	    if_cloners_count : ifcr->ifcr_count;
885
886	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
887	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
888		strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
889		error = copyout(outbuf, dst, IFNAMSIZ);
890		if (error)
891			break;
892	}
893
894	return (error);
895}
896
897#define	equal(a1, a2)	(bcmp((a1), (a2), ((a1))->sa_len) == 0)
898
899/*
900 * Locate an interface based on a complete address.
901 */
902/*ARGSUSED*/
903struct ifaddr *
904ifa_ifwithaddr(struct sockaddr *addr)
905{
906	struct ifnet *ifp;
907	struct ifaddr *ifa;
908
909	IFNET_RLOCK();
910	TAILQ_FOREACH(ifp, &ifnet, if_link)
911		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
912			if (ifa->ifa_addr->sa_family != addr->sa_family)
913				continue;
914			if (equal(addr, ifa->ifa_addr))
915				goto done;
916			/* IP6 doesn't have broadcast */
917			if ((ifp->if_flags & IFF_BROADCAST) &&
918			    ifa->ifa_broadaddr &&
919			    ifa->ifa_broadaddr->sa_len != 0 &&
920			    equal(ifa->ifa_broadaddr, addr))
921				goto done;
922		}
923	ifa = NULL;
924done:
925	IFNET_RUNLOCK();
926	return (ifa);
927}
928
929/*
930 * Locate the point to point interface with a given destination address.
931 */
932/*ARGSUSED*/
933struct ifaddr *
934ifa_ifwithdstaddr(struct sockaddr *addr)
935{
936	struct ifnet *ifp;
937	struct ifaddr *ifa;
938
939	IFNET_RLOCK();
940	TAILQ_FOREACH(ifp, &ifnet, if_link) {
941		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
942			continue;
943		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
944			if (ifa->ifa_addr->sa_family != addr->sa_family)
945				continue;
946			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
947				goto done;
948		}
949	}
950	ifa = NULL;
951done:
952	IFNET_RUNLOCK();
953	return (ifa);
954}
955
956/*
957 * Find an interface on a specific network.  If many, choice
958 * is most specific found.
959 */
960struct ifaddr *
961ifa_ifwithnet(struct sockaddr *addr)
962{
963	struct ifnet *ifp;
964	struct ifaddr *ifa;
965	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
966	u_int af = addr->sa_family;
967	char *addr_data = addr->sa_data, *cplim;
968
969	/*
970	 * AF_LINK addresses can be looked up directly by their index number,
971	 * so do that if we can.
972	 */
973	if (af == AF_LINK) {
974	    struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
975	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
976		return (ifaddr_byindex(sdl->sdl_index));
977	}
978
979	/*
980	 * Scan though each interface, looking for ones that have
981	 * addresses in this address family.
982	 */
983	IFNET_RLOCK();
984	TAILQ_FOREACH(ifp, &ifnet, if_link) {
985		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
986			char *cp, *cp2, *cp3;
987
988			if (ifa->ifa_addr->sa_family != af)
989next:				continue;
990			if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
991				/*
992				 * This is a bit broken as it doesn't
993				 * take into account that the remote end may
994				 * be a single node in the network we are
995				 * looking for.
996				 * The trouble is that we don't know the
997				 * netmask for the remote end.
998				 */
999				if (ifa->ifa_dstaddr != 0
1000				    && equal(addr, ifa->ifa_dstaddr))
1001					goto done;
1002			} else {
1003				/*
1004				 * if we have a special address handler,
1005				 * then use it instead of the generic one.
1006				 */
1007				if (ifa->ifa_claim_addr) {
1008					if ((*ifa->ifa_claim_addr)(ifa, addr))
1009						goto done;
1010					continue;
1011				}
1012
1013				/*
1014				 * Scan all the bits in the ifa's address.
1015				 * If a bit dissagrees with what we are
1016				 * looking for, mask it with the netmask
1017				 * to see if it really matters.
1018				 * (A byte at a time)
1019				 */
1020				if (ifa->ifa_netmask == 0)
1021					continue;
1022				cp = addr_data;
1023				cp2 = ifa->ifa_addr->sa_data;
1024				cp3 = ifa->ifa_netmask->sa_data;
1025				cplim = ifa->ifa_netmask->sa_len
1026					+ (char *)ifa->ifa_netmask;
1027				while (cp3 < cplim)
1028					if ((*cp++ ^ *cp2++) & *cp3++)
1029						goto next; /* next address! */
1030				/*
1031				 * If the netmask of what we just found
1032				 * is more specific than what we had before
1033				 * (if we had one) then remember the new one
1034				 * before continuing to search
1035				 * for an even better one.
1036				 */
1037				if (ifa_maybe == 0 ||
1038				    rn_refines((caddr_t)ifa->ifa_netmask,
1039				    (caddr_t)ifa_maybe->ifa_netmask))
1040					ifa_maybe = ifa;
1041			}
1042		}
1043	}
1044	ifa = ifa_maybe;
1045done:
1046	IFNET_RUNLOCK();
1047	return (ifa);
1048}
1049
1050/*
1051 * Find an interface address specific to an interface best matching
1052 * a given address.
1053 */
1054struct ifaddr *
1055ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1056{
1057	struct ifaddr *ifa;
1058	char *cp, *cp2, *cp3;
1059	char *cplim;
1060	struct ifaddr *ifa_maybe = 0;
1061	u_int af = addr->sa_family;
1062
1063	if (af >= AF_MAX)
1064		return (0);
1065	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1066		if (ifa->ifa_addr->sa_family != af)
1067			continue;
1068		if (ifa_maybe == 0)
1069			ifa_maybe = ifa;
1070		if (ifa->ifa_netmask == 0) {
1071			if (equal(addr, ifa->ifa_addr) ||
1072			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
1073				goto done;
1074			continue;
1075		}
1076		if (ifp->if_flags & IFF_POINTOPOINT) {
1077			if (equal(addr, ifa->ifa_dstaddr))
1078				goto done;
1079		} else {
1080			cp = addr->sa_data;
1081			cp2 = ifa->ifa_addr->sa_data;
1082			cp3 = ifa->ifa_netmask->sa_data;
1083			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1084			for (; cp3 < cplim; cp3++)
1085				if ((*cp++ ^ *cp2++) & *cp3)
1086					break;
1087			if (cp3 == cplim)
1088				goto done;
1089		}
1090	}
1091	ifa = ifa_maybe;
1092done:
1093	return (ifa);
1094}
1095
1096#include <net/route.h>
1097
1098/*
1099 * Default action when installing a route with a Link Level gateway.
1100 * Lookup an appropriate real ifa to point to.
1101 * This should be moved to /sys/net/link.c eventually.
1102 */
1103static void
1104link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1105{
1106	struct ifaddr *ifa, *oifa;
1107	struct sockaddr *dst;
1108	struct ifnet *ifp;
1109
1110	RT_LOCK_ASSERT(rt);
1111
1112	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1113	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1114		return;
1115	ifa = ifaof_ifpforaddr(dst, ifp);
1116	if (ifa) {
1117		IFAREF(ifa);		/* XXX */
1118		oifa = rt->rt_ifa;
1119		rt->rt_ifa = ifa;
1120		IFAFREE(oifa);
1121		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1122			ifa->ifa_rtrequest(cmd, rt, info);
1123	}
1124}
1125
1126/*
1127 * Mark an interface down and notify protocols of
1128 * the transition.
1129 * NOTE: must be called at splnet or eqivalent.
1130 */
1131void
1132if_unroute(struct ifnet *ifp, int flag, int fam)
1133{
1134	struct ifaddr *ifa;
1135
1136	ifp->if_flags &= ~flag;
1137	getmicrotime(&ifp->if_lastchange);
1138	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1139		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1140			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1141	if_qflush(&ifp->if_snd);
1142	rt_ifmsg(ifp);
1143}
1144
1145/*
1146 * Mark an interface up and notify protocols of
1147 * the transition.
1148 * NOTE: must be called at splnet or eqivalent.
1149 */
1150void
1151if_route(struct ifnet *ifp, int flag, int fam)
1152{
1153	struct ifaddr *ifa;
1154
1155	ifp->if_flags |= flag;
1156	getmicrotime(&ifp->if_lastchange);
1157	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1158		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1159			pfctlinput(PRC_IFUP, ifa->ifa_addr);
1160	rt_ifmsg(ifp);
1161#ifdef INET6
1162	in6_if_up(ifp);
1163#endif
1164}
1165
1166/*
1167 * Mark an interface down and notify protocols of
1168 * the transition.
1169 * NOTE: must be called at splnet or eqivalent.
1170 */
1171void
1172if_down(struct ifnet *ifp)
1173{
1174
1175	if_unroute(ifp, IFF_UP, AF_UNSPEC);
1176}
1177
1178/*
1179 * Mark an interface up and notify protocols of
1180 * the transition.
1181 * NOTE: must be called at splnet or eqivalent.
1182 */
1183void
1184if_up(struct ifnet *ifp)
1185{
1186
1187	if_route(ifp, IFF_UP, AF_UNSPEC);
1188}
1189
1190/*
1191 * Flush an interface queue.
1192 */
1193static void
1194if_qflush(struct ifqueue *ifq)
1195{
1196	struct mbuf *m, *n;
1197
1198	n = ifq->ifq_head;
1199	while ((m = n) != 0) {
1200		n = m->m_act;
1201		m_freem(m);
1202	}
1203	ifq->ifq_head = 0;
1204	ifq->ifq_tail = 0;
1205	ifq->ifq_len = 0;
1206}
1207
1208/*
1209 * Handle interface watchdog timer routines.  Called
1210 * from softclock, we decrement timers (if set) and
1211 * call the appropriate interface routine on expiration.
1212 */
1213static void
1214if_slowtimo(void *arg)
1215{
1216	struct ifnet *ifp;
1217	int s = splimp();
1218
1219	IFNET_RLOCK();
1220	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1221		if (ifp->if_timer == 0 || --ifp->if_timer)
1222			continue;
1223		if (ifp->if_watchdog)
1224			(*ifp->if_watchdog)(ifp);
1225	}
1226	IFNET_RUNLOCK();
1227	splx(s);
1228	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1229}
1230
1231/*
1232 * Map interface name to
1233 * interface structure pointer.
1234 */
1235struct ifnet *
1236ifunit(const char *name)
1237{
1238	struct ifnet *ifp;
1239
1240	IFNET_RLOCK();
1241	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1242		if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0)
1243			break;
1244	}
1245	IFNET_RUNLOCK();
1246	return (ifp);
1247}
1248
1249/*
1250 * Map interface name in a sockaddr_dl to
1251 * interface structure pointer.
1252 */
1253struct ifnet *
1254if_withname(struct sockaddr *sa)
1255{
1256	char ifname[IFNAMSIZ+1];
1257	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1258
1259	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1260	     (sdl->sdl_nlen > IFNAMSIZ) )
1261		return NULL;
1262
1263	/*
1264	 * ifunit wants a NUL-terminated string.  It may not be NUL-terminated
1265	 * in the sockaddr, and we don't want to change the caller's sockaddr
1266	 * (there might not be room to add the trailing NUL anyway), so we make
1267	 * a local copy that we know we can NUL-terminate safely.
1268	 */
1269
1270	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1271	ifname[sdl->sdl_nlen] = '\0';
1272	return ifunit(ifname);
1273}
1274
1275/*
1276 * Hardware specific interface ioctls.
1277 */
1278static int
1279ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1280{
1281	struct ifreq *ifr;
1282	struct ifstat *ifs;
1283	int error = 0;
1284	int new_flags;
1285	size_t namelen, onamelen;
1286	char new_name[IFNAMSIZ];
1287	struct ifaddr *ifa;
1288	struct sockaddr_dl *sdl;
1289
1290	ifr = (struct ifreq *)data;
1291	switch (cmd) {
1292	case SIOCGIFINDEX:
1293		ifr->ifr_index = ifp->if_index;
1294		break;
1295
1296	case SIOCGIFFLAGS:
1297		ifr->ifr_flags = ifp->if_flags & 0xffff;
1298		ifr->ifr_flagshigh = ifp->if_flags >> 16;
1299		break;
1300
1301	case SIOCGIFCAP:
1302		ifr->ifr_reqcap = ifp->if_capabilities;
1303		ifr->ifr_curcap = ifp->if_capenable;
1304		break;
1305
1306#ifdef MAC
1307	case SIOCGIFMAC:
1308		error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp);
1309		break;
1310#endif
1311
1312	case SIOCGIFMETRIC:
1313		ifr->ifr_metric = ifp->if_metric;
1314		break;
1315
1316	case SIOCGIFMTU:
1317		ifr->ifr_mtu = ifp->if_mtu;
1318		break;
1319
1320	case SIOCGIFPHYS:
1321		ifr->ifr_phys = ifp->if_physical;
1322		break;
1323
1324	case SIOCSIFFLAGS:
1325		error = suser(td);
1326		if (error)
1327			return (error);
1328		new_flags = (ifr->ifr_flags & 0xffff) |
1329		    (ifr->ifr_flagshigh << 16);
1330		if (ifp->if_flags & IFF_SMART) {
1331			/* Smart drivers twiddle their own routes */
1332		} else if (ifp->if_flags & IFF_UP &&
1333		    (new_flags & IFF_UP) == 0) {
1334			int s = splimp();
1335			if_down(ifp);
1336			splx(s);
1337		} else if (new_flags & IFF_UP &&
1338		    (ifp->if_flags & IFF_UP) == 0) {
1339			int s = splimp();
1340			if_up(ifp);
1341			splx(s);
1342		}
1343		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1344			(new_flags &~ IFF_CANTCHANGE);
1345		if (new_flags & IFF_PPROMISC) {
1346			/* Permanently promiscuous mode requested */
1347			ifp->if_flags |= IFF_PROMISC;
1348		} else if (ifp->if_pcount == 0) {
1349			ifp->if_flags &= ~IFF_PROMISC;
1350		}
1351		if (ifp->if_ioctl)
1352			(void) (*ifp->if_ioctl)(ifp, cmd, data);
1353		getmicrotime(&ifp->if_lastchange);
1354		break;
1355
1356	case SIOCSIFCAP:
1357		error = suser(td);
1358		if (error)
1359			return (error);
1360		if (ifp->if_ioctl == NULL)
1361			return (EOPNOTSUPP);
1362		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1363			return (EINVAL);
1364		error = (*ifp->if_ioctl)(ifp, cmd, data);
1365		if (error == 0)
1366			getmicrotime(&ifp->if_lastchange);
1367		break;
1368
1369#ifdef MAC
1370	case SIOCSIFMAC:
1371		error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp);
1372		break;
1373#endif
1374
1375	case SIOCSIFNAME:
1376		error = suser(td);
1377		if (error)
1378			return (error);
1379		error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1380		if (error)
1381			return (error);
1382		if (ifunit(new_name) != NULL)
1383			return (EEXIST);
1384
1385		EVENTHANDLER_INVOKE(ifnet_departure_event, ifp);
1386		/* Announce the departure of the interface. */
1387		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1388
1389		strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1390		ifa = TAILQ_FIRST(&ifp->if_addrhead);
1391		IFA_LOCK(ifa);
1392		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1393		namelen = strlen(new_name);
1394		onamelen = sdl->sdl_nlen;
1395		/*
1396		 * Move the address if needed.  This is safe because we
1397		 * allocate space for a name of length IFNAMSIZ when we
1398		 * create this in if_attach().
1399		 */
1400		if (namelen != onamelen) {
1401			bcopy(sdl->sdl_data + onamelen,
1402			    sdl->sdl_data + namelen, sdl->sdl_alen);
1403		}
1404		bcopy(new_name, sdl->sdl_data, namelen);
1405		sdl->sdl_nlen = namelen;
1406		sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1407		bzero(sdl->sdl_data, onamelen);
1408		while (namelen != 0)
1409			sdl->sdl_data[--namelen] = 0xff;
1410		IFA_UNLOCK(ifa);
1411
1412		EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp);
1413		/* Announce the return of the interface. */
1414		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1415		break;
1416
1417	case SIOCSIFMETRIC:
1418		error = suser(td);
1419		if (error)
1420			return (error);
1421		ifp->if_metric = ifr->ifr_metric;
1422		getmicrotime(&ifp->if_lastchange);
1423		break;
1424
1425	case SIOCSIFPHYS:
1426		error = suser(td);
1427		if (error)
1428			return (error);
1429		if (ifp->if_ioctl == NULL)
1430			return (EOPNOTSUPP);
1431		error = (*ifp->if_ioctl)(ifp, cmd, data);
1432		if (error == 0)
1433			getmicrotime(&ifp->if_lastchange);
1434		break;
1435
1436	case SIOCSIFMTU:
1437	{
1438		u_long oldmtu = ifp->if_mtu;
1439
1440		error = suser(td);
1441		if (error)
1442			return (error);
1443		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
1444			return (EINVAL);
1445		if (ifp->if_ioctl == NULL)
1446			return (EOPNOTSUPP);
1447		error = (*ifp->if_ioctl)(ifp, cmd, data);
1448		if (error == 0) {
1449			getmicrotime(&ifp->if_lastchange);
1450			rt_ifmsg(ifp);
1451		}
1452		/*
1453		 * If the link MTU changed, do network layer specific procedure.
1454		 */
1455		if (ifp->if_mtu != oldmtu) {
1456#ifdef INET6
1457			nd6_setmtu(ifp);
1458#endif
1459		}
1460		break;
1461	}
1462
1463	case SIOCADDMULTI:
1464	case SIOCDELMULTI:
1465		error = suser(td);
1466		if (error)
1467			return (error);
1468
1469		/* Don't allow group membership on non-multicast interfaces. */
1470		if ((ifp->if_flags & IFF_MULTICAST) == 0)
1471			return (EOPNOTSUPP);
1472
1473		/* Don't let users screw up protocols' entries. */
1474		if (ifr->ifr_addr.sa_family != AF_LINK)
1475			return (EINVAL);
1476
1477		if (cmd == SIOCADDMULTI) {
1478			struct ifmultiaddr *ifma;
1479			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1480		} else {
1481			error = if_delmulti(ifp, &ifr->ifr_addr);
1482		}
1483		if (error == 0)
1484			getmicrotime(&ifp->if_lastchange);
1485		break;
1486
1487	case SIOCSIFPHYADDR:
1488	case SIOCDIFPHYADDR:
1489#ifdef INET6
1490	case SIOCSIFPHYADDR_IN6:
1491#endif
1492	case SIOCSLIFPHYADDR:
1493	case SIOCSIFMEDIA:
1494	case SIOCSIFGENERIC:
1495		error = suser(td);
1496		if (error)
1497			return (error);
1498		if (ifp->if_ioctl == NULL)
1499			return (EOPNOTSUPP);
1500		error = (*ifp->if_ioctl)(ifp, cmd, data);
1501		if (error == 0)
1502			getmicrotime(&ifp->if_lastchange);
1503		break;
1504
1505	case SIOCGIFSTATUS:
1506		ifs = (struct ifstat *)data;
1507		ifs->ascii[0] = '\0';
1508
1509	case SIOCGIFPSRCADDR:
1510	case SIOCGIFPDSTADDR:
1511	case SIOCGLIFPHYADDR:
1512	case SIOCGIFMEDIA:
1513	case SIOCGIFGENERIC:
1514		if (ifp->if_ioctl == NULL)
1515			return (EOPNOTSUPP);
1516		error = (*ifp->if_ioctl)(ifp, cmd, data);
1517		break;
1518
1519	case SIOCSIFLLADDR:
1520		error = suser(td);
1521		if (error)
1522			return (error);
1523		error = if_setlladdr(ifp,
1524		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1525		break;
1526
1527	default:
1528		error = ENOIOCTL;
1529		break;
1530	}
1531	return (error);
1532}
1533
1534/*
1535 * Interface ioctls.
1536 */
1537int
1538ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td)
1539{
1540	struct ifnet *ifp;
1541	struct ifreq *ifr;
1542	int error;
1543	int oif_flags;
1544
1545	switch (cmd) {
1546	case SIOCGIFCONF:
1547	case OSIOCGIFCONF:
1548		return (ifconf(cmd, data));
1549	}
1550	ifr = (struct ifreq *)data;
1551
1552	switch (cmd) {
1553	case SIOCIFCREATE:
1554	case SIOCIFDESTROY:
1555		if ((error = suser(td)) != 0)
1556			return (error);
1557		return ((cmd == SIOCIFCREATE) ?
1558			if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1559			if_clone_destroy(ifr->ifr_name));
1560
1561	case SIOCIFGCLONERS:
1562		return (if_clone_list((struct if_clonereq *)data));
1563	}
1564
1565	ifp = ifunit(ifr->ifr_name);
1566	if (ifp == 0)
1567		return (ENXIO);
1568
1569	error = ifhwioctl(cmd, ifp, data, td);
1570	if (error != ENOIOCTL)
1571		return (error);
1572
1573	oif_flags = ifp->if_flags;
1574	if (so->so_proto == 0)
1575		return (EOPNOTSUPP);
1576#ifndef COMPAT_43
1577	error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
1578								 data,
1579								 ifp, td));
1580#else
1581	{
1582		int ocmd = cmd;
1583
1584		switch (cmd) {
1585
1586		case SIOCSIFDSTADDR:
1587		case SIOCSIFADDR:
1588		case SIOCSIFBRDADDR:
1589		case SIOCSIFNETMASK:
1590#if BYTE_ORDER != BIG_ENDIAN
1591			if (ifr->ifr_addr.sa_family == 0 &&
1592			    ifr->ifr_addr.sa_len < 16) {
1593				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1594				ifr->ifr_addr.sa_len = 16;
1595			}
1596#else
1597			if (ifr->ifr_addr.sa_len == 0)
1598				ifr->ifr_addr.sa_len = 16;
1599#endif
1600			break;
1601
1602		case OSIOCGIFADDR:
1603			cmd = SIOCGIFADDR;
1604			break;
1605
1606		case OSIOCGIFDSTADDR:
1607			cmd = SIOCGIFDSTADDR;
1608			break;
1609
1610		case OSIOCGIFBRDADDR:
1611			cmd = SIOCGIFBRDADDR;
1612			break;
1613
1614		case OSIOCGIFNETMASK:
1615			cmd = SIOCGIFNETMASK;
1616		}
1617		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
1618								   cmd,
1619								   data,
1620								   ifp, td));
1621		switch (ocmd) {
1622
1623		case OSIOCGIFADDR:
1624		case OSIOCGIFDSTADDR:
1625		case OSIOCGIFBRDADDR:
1626		case OSIOCGIFNETMASK:
1627			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1628
1629		}
1630	}
1631#endif /* COMPAT_43 */
1632
1633	if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1634#ifdef INET6
1635		DELAY(100);/* XXX: temporary workaround for fxp issue*/
1636		if (ifp->if_flags & IFF_UP) {
1637			int s = splimp();
1638			in6_if_up(ifp);
1639			splx(s);
1640		}
1641#endif
1642	}
1643	return (error);
1644}
1645
1646/*
1647 * Set/clear promiscuous mode on interface ifp based on the truth value
1648 * of pswitch.  The calls are reference counted so that only the first
1649 * "on" request actually has an effect, as does the final "off" request.
1650 * Results are undefined if the "off" and "on" requests are not matched.
1651 */
1652int
1653ifpromisc(struct ifnet *ifp, int pswitch)
1654{
1655	struct ifreq ifr;
1656	int error;
1657	int oldflags, oldpcount;
1658
1659	oldpcount = ifp->if_pcount;
1660	oldflags = ifp->if_flags;
1661	if (ifp->if_flags & IFF_PPROMISC) {
1662		/* Do nothing if device is in permanently promiscuous mode */
1663		ifp->if_pcount += pswitch ? 1 : -1;
1664		return (0);
1665	}
1666	if (pswitch) {
1667		/*
1668		 * If the device is not configured up, we cannot put it in
1669		 * promiscuous mode.
1670		 */
1671		if ((ifp->if_flags & IFF_UP) == 0)
1672			return (ENETDOWN);
1673		if (ifp->if_pcount++ != 0)
1674			return (0);
1675		ifp->if_flags |= IFF_PROMISC;
1676	} else {
1677		if (--ifp->if_pcount > 0)
1678			return (0);
1679		ifp->if_flags &= ~IFF_PROMISC;
1680	}
1681	ifr.ifr_flags = ifp->if_flags & 0xffff;
1682	ifr.ifr_flagshigh = ifp->if_flags >> 16;
1683	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1684	if (error == 0) {
1685		log(LOG_INFO, "%s: promiscuous mode %s\n",
1686		    ifp->if_xname,
1687		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
1688		rt_ifmsg(ifp);
1689	} else {
1690		ifp->if_pcount = oldpcount;
1691		ifp->if_flags = oldflags;
1692	}
1693	return error;
1694}
1695
1696/*
1697 * Return interface configuration
1698 * of system.  List may be used
1699 * in later ioctl's (above) to get
1700 * other information.
1701 */
1702/*ARGSUSED*/
1703static int
1704ifconf(u_long cmd, caddr_t data)
1705{
1706	struct ifconf *ifc = (struct ifconf *)data;
1707	struct ifnet *ifp;
1708	struct ifaddr *ifa;
1709	struct ifreq ifr, *ifrp;
1710	int space = ifc->ifc_len, error = 0;
1711
1712	ifrp = ifc->ifc_req;
1713	IFNET_RLOCK();		/* could sleep XXX */
1714	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1715		int addrs;
1716
1717		if (space < sizeof(ifr))
1718			break;
1719		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1720		    >= sizeof(ifr.ifr_name)) {
1721			error = ENAMETOOLONG;
1722			break;
1723		}
1724
1725		addrs = 0;
1726		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1727			struct sockaddr *sa = ifa->ifa_addr;
1728
1729			if (space < sizeof(ifr))
1730				break;
1731			if (jailed(curthread->td_ucred) &&
1732			    prison_if(curthread->td_ucred, sa))
1733				continue;
1734			addrs++;
1735#ifdef COMPAT_43
1736			if (cmd == OSIOCGIFCONF) {
1737				struct osockaddr *osa =
1738					 (struct osockaddr *)&ifr.ifr_addr;
1739				ifr.ifr_addr = *sa;
1740				osa->sa_family = sa->sa_family;
1741				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1742						sizeof (ifr));
1743				ifrp++;
1744			} else
1745#endif
1746			if (sa->sa_len <= sizeof(*sa)) {
1747				ifr.ifr_addr = *sa;
1748				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1749						sizeof (ifr));
1750				ifrp++;
1751			} else {
1752				if (space < sizeof (ifr) + sa->sa_len -
1753					    sizeof(*sa))
1754					break;
1755				space -= sa->sa_len - sizeof(*sa);
1756				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1757						sizeof (ifr.ifr_name));
1758				if (error == 0)
1759				    error = copyout((caddr_t)sa,
1760				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1761				ifrp = (struct ifreq *)
1762					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1763			}
1764			if (error)
1765				break;
1766			space -= sizeof (ifr);
1767		}
1768		if (error)
1769			break;
1770		if (!addrs) {
1771			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1772			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1773			    sizeof (ifr));
1774			if (error)
1775				break;
1776			space -= sizeof (ifr);
1777			ifrp++;
1778		}
1779	}
1780	IFNET_RUNLOCK();
1781	ifc->ifc_len -= space;
1782	return (error);
1783}
1784
1785/*
1786 * Just like if_promisc(), but for all-multicast-reception mode.
1787 */
1788int
1789if_allmulti(struct ifnet *ifp, int onswitch)
1790{
1791	int error = 0;
1792	int s = splimp();
1793	struct ifreq ifr;
1794
1795	if (onswitch) {
1796		if (ifp->if_amcount++ == 0) {
1797			ifp->if_flags |= IFF_ALLMULTI;
1798			ifr.ifr_flags = ifp->if_flags & 0xffff;
1799			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1800			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1801		}
1802	} else {
1803		if (ifp->if_amcount > 1) {
1804			ifp->if_amcount--;
1805		} else {
1806			ifp->if_amcount = 0;
1807			ifp->if_flags &= ~IFF_ALLMULTI;
1808			ifr.ifr_flags = ifp->if_flags & 0xffff;;
1809			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1810			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1811		}
1812	}
1813	splx(s);
1814
1815	if (error == 0)
1816		rt_ifmsg(ifp);
1817	return error;
1818}
1819
1820/*
1821 * Add a multicast listenership to the interface in question.
1822 * The link layer provides a routine which converts
1823 */
1824int
1825if_addmulti(struct ifnet *ifp, struct sockaddr *sa, struct ifmultiaddr **retifma)
1826{
1827	struct sockaddr *llsa, *dupsa;
1828	int error, s;
1829	struct ifmultiaddr *ifma;
1830
1831	/*
1832	 * If the matching multicast address already exists
1833	 * then don't add a new one, just add a reference
1834	 */
1835	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1836		if (equal(sa, ifma->ifma_addr)) {
1837			ifma->ifma_refcount++;
1838			if (retifma)
1839				*retifma = ifma;
1840			return 0;
1841		}
1842	}
1843
1844	/*
1845	 * Give the link layer a chance to accept/reject it, and also
1846	 * find out which AF_LINK address this maps to, if it isn't one
1847	 * already.
1848	 */
1849	if (ifp->if_resolvemulti) {
1850		error = ifp->if_resolvemulti(ifp, &llsa, sa);
1851		if (error) return error;
1852	} else {
1853		llsa = 0;
1854	}
1855
1856	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1857	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1858	bcopy(sa, dupsa, sa->sa_len);
1859
1860	ifma->ifma_addr = dupsa;
1861	ifma->ifma_lladdr = llsa;
1862	ifma->ifma_ifp = ifp;
1863	ifma->ifma_refcount = 1;
1864	ifma->ifma_protospec = 0;
1865	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1866
1867	/*
1868	 * Some network interfaces can scan the address list at
1869	 * interrupt time; lock them out.
1870	 */
1871	s = splimp();
1872	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1873	splx(s);
1874	if (retifma != NULL)
1875		*retifma = ifma;
1876
1877	if (llsa != 0) {
1878		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1879			if (equal(ifma->ifma_addr, llsa))
1880				break;
1881		}
1882		if (ifma) {
1883			ifma->ifma_refcount++;
1884		} else {
1885			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1886			       M_IFMADDR, M_WAITOK);
1887			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1888			       M_IFMADDR, M_WAITOK);
1889			bcopy(llsa, dupsa, llsa->sa_len);
1890			ifma->ifma_addr = dupsa;
1891			ifma->ifma_lladdr = NULL;
1892			ifma->ifma_ifp = ifp;
1893			ifma->ifma_refcount = 1;
1894			ifma->ifma_protospec = 0;
1895			s = splimp();
1896			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1897			splx(s);
1898		}
1899	}
1900	/*
1901	 * We are certain we have added something, so call down to the
1902	 * interface to let them know about it.
1903	 */
1904	s = splimp();
1905	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1906	splx(s);
1907
1908	return 0;
1909}
1910
1911/*
1912 * Remove a reference to a multicast address on this interface.  Yell
1913 * if the request does not match an existing membership.
1914 */
1915int
1916if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
1917{
1918	struct ifmultiaddr *ifma;
1919	int s;
1920
1921	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1922		if (equal(sa, ifma->ifma_addr))
1923			break;
1924	if (ifma == 0)
1925		return ENOENT;
1926
1927	if (ifma->ifma_refcount > 1) {
1928		ifma->ifma_refcount--;
1929		return 0;
1930	}
1931
1932	rt_newmaddrmsg(RTM_DELMADDR, ifma);
1933	sa = ifma->ifma_lladdr;
1934	s = splimp();
1935	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1936	/*
1937	 * Make sure the interface driver is notified
1938	 * in the case of a link layer mcast group being left.
1939	 */
1940	if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1941		ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1942	splx(s);
1943	free(ifma->ifma_addr, M_IFMADDR);
1944	free(ifma, M_IFMADDR);
1945	if (sa == 0)
1946		return 0;
1947
1948	/*
1949	 * Now look for the link-layer address which corresponds to
1950	 * this network address.  It had been squirreled away in
1951	 * ifma->ifma_lladdr for this purpose (so we don't have
1952	 * to call ifp->if_resolvemulti() again), and we saved that
1953	 * value in sa above.  If some nasty deleted the
1954	 * link-layer address out from underneath us, we can deal because
1955	 * the address we stored was is not the same as the one which was
1956	 * in the record for the link-layer address.  (So we don't complain
1957	 * in that case.)
1958	 */
1959	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1960		if (equal(sa, ifma->ifma_addr))
1961			break;
1962	if (ifma == 0)
1963		return 0;
1964
1965	if (ifma->ifma_refcount > 1) {
1966		ifma->ifma_refcount--;
1967		return 0;
1968	}
1969
1970	s = splimp();
1971	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1972	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1973	splx(s);
1974	free(ifma->ifma_addr, M_IFMADDR);
1975	free(sa, M_IFMADDR);
1976	free(ifma, M_IFMADDR);
1977
1978	return 0;
1979}
1980
1981/*
1982 * Set the link layer address on an interface.
1983 *
1984 * At this time we only support certain types of interfaces,
1985 * and we don't allow the length of the address to change.
1986 */
1987int
1988if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1989{
1990	struct sockaddr_dl *sdl;
1991	struct ifaddr *ifa;
1992	struct ifreq ifr;
1993
1994	ifa = ifaddr_byindex(ifp->if_index);
1995	if (ifa == NULL)
1996		return (EINVAL);
1997	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1998	if (sdl == NULL)
1999		return (EINVAL);
2000	if (len != sdl->sdl_alen)	/* don't allow length to change */
2001		return (EINVAL);
2002	switch (ifp->if_type) {
2003	case IFT_ETHER:			/* these types use struct arpcom */
2004	case IFT_FDDI:
2005	case IFT_XETHER:
2006	case IFT_ISO88025:
2007	case IFT_L2VLAN:
2008		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2009		/* FALLTHROUGH */
2010	case IFT_ARCNET:
2011		bcopy(lladdr, LLADDR(sdl), len);
2012		break;
2013	default:
2014		return (ENODEV);
2015	}
2016	/*
2017	 * If the interface is already up, we need
2018	 * to re-init it in order to reprogram its
2019	 * address filter.
2020	 */
2021	if ((ifp->if_flags & IFF_UP) != 0) {
2022		ifp->if_flags &= ~IFF_UP;
2023		ifr.ifr_flags = ifp->if_flags & 0xffff;
2024		ifr.ifr_flagshigh = ifp->if_flags >> 16;
2025		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2026		ifp->if_flags |= IFF_UP;
2027		ifr.ifr_flags = ifp->if_flags & 0xffff;
2028		ifr.ifr_flagshigh = ifp->if_flags >> 16;
2029		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
2030#ifdef INET
2031		/*
2032		 * Also send gratuitous ARPs to notify other nodes about
2033		 * the address change.
2034		 */
2035		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2036			if (ifa->ifa_addr != NULL &&
2037			    ifa->ifa_addr->sa_family == AF_INET)
2038				arp_ifinit(ifp, ifa);
2039		}
2040#endif
2041	}
2042	return (0);
2043}
2044
2045struct ifmultiaddr *
2046ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2047{
2048	struct ifmultiaddr *ifma;
2049
2050	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2051		if (equal(ifma->ifma_addr, sa))
2052			break;
2053
2054	return ifma;
2055}
2056
2057/*
2058 * The name argument must be a pointer to storage which will last as
2059 * long as the interface does.  For physical devices, the result of
2060 * device_get_name(dev) is a good choice and for pseudo-devices a
2061 * static string works well.
2062 */
2063void
2064if_initname(struct ifnet *ifp, const char *name, int unit)
2065{
2066	ifp->if_dname = name;
2067	ifp->if_dunit = unit;
2068	if (unit != IF_DUNIT_NONE)
2069		snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2070	else
2071		strlcpy(ifp->if_xname, name, IFNAMSIZ);
2072}
2073
2074int
2075if_printf(struct ifnet *ifp, const char * fmt, ...)
2076{
2077	va_list ap;
2078	int retval;
2079
2080	retval = printf("%s: ", ifp->if_xname);
2081	va_start(ap, fmt);
2082	retval += vprintf(fmt, ap);
2083	va_end(ap);
2084	return (retval);
2085}
2086
2087SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
2088SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
2089