xref: /freebsd/sys/net/if.c (revision 80ff3b155fc42479ec6c512cae13e709f700d28b)
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.3 (Berkeley) 1/4/94
34  * $FreeBSD$
35  */
36 
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
39 #include "opt_inet.h"
40 
41 #include <sys/param.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
49 #include <sys/kernel.h>
50 #include <sys/sockio.h>
51 #include <sys/syslog.h>
52 #include <sys/sysctl.h>
53 
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/radix.h>
59 #include <net/route.h>
60 
61 #if defined(INET) || defined(INET6)
62 /*XXX*/
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #ifdef INET6
66 #include <netinet6/in6_var.h>
67 #include <netinet6/in6_ifattach.h>
68 #endif
69 #endif
70 
71 /*
72  * System initialization
73  */
74 
75 static int ifconf __P((u_long, caddr_t));
76 static void ifinit __P((void *));
77 static void if_qflush __P((struct ifqueue *));
78 static void if_slowtimo __P((void *));
79 static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *));
80 static int  if_rtdel __P((struct radix_node *, void *));
81 
82 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
83 
84 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
85 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
86 
87 int	ifqmaxlen = IFQ_MAXLEN;
88 struct	ifnethead ifnet;	/* depend on static init XXX */
89 
90 #ifdef INET6
91 /*
92  * XXX: declare here to avoid to include many inet6 related files..
93  * should be more generalized?
94  */
95 extern void	nd6_setmtu __P((struct ifnet *));
96 #endif
97 
98 /*
99  * Network interface utility routines.
100  *
101  * Routines with ifa_ifwith* names take sockaddr *'s as
102  * parameters.
103  */
104 /* ARGSUSED*/
105 void
106 ifinit(dummy)
107 	void *dummy;
108 {
109 	struct ifnet *ifp;
110 	int s;
111 
112 	s = splimp();
113 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
114 		if (ifp->if_snd.ifq_maxlen == 0) {
115 			printf("%s%d XXX: driver didn't set ifq_maxlen\n",
116 			    ifp->if_name, ifp->if_unit);
117 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
118 		}
119 		if (ifp->if_snd.ifq_mtx.mtx_description == NULL) {
120 			printf("%s%d XXX: driver didn't initialize queue mtx\n",
121 			    ifp->if_name, ifp->if_unit);
122 			mtx_init(&ifp->if_snd.ifq_mtx, "unknown", MTX_DEF);
123 		}
124 	}
125 	splx(s);
126 	if_slowtimo(0);
127 }
128 
129 int if_index = 0;
130 struct ifaddr **ifnet_addrs;
131 struct ifnet **ifindex2ifnet = NULL;
132 
133 
134 /*
135  * Attach an interface to the
136  * list of "active" interfaces.
137  */
138 void
139 if_attach(ifp)
140 	struct ifnet *ifp;
141 {
142 	unsigned socksize, ifasize;
143 	int namelen, masklen;
144 	char workbuf[64];
145 	register struct sockaddr_dl *sdl;
146 	register struct ifaddr *ifa;
147 	static int if_indexlim = 8;
148 	static int inited;
149 
150 	if (!inited) {
151 		TAILQ_INIT(&ifnet);
152 		inited = 1;
153 	}
154 
155 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
156 	ifp->if_index = ++if_index;
157 	/*
158 	 * XXX -
159 	 * The old code would work if the interface passed a pre-existing
160 	 * chain of ifaddrs to this code.  We don't trust our callers to
161 	 * properly initialize the tailq, however, so we no longer allow
162 	 * this unlikely case.
163 	 */
164 	TAILQ_INIT(&ifp->if_addrhead);
165 	TAILQ_INIT(&ifp->if_prefixhead);
166 	LIST_INIT(&ifp->if_multiaddrs);
167 	getmicrotime(&ifp->if_lastchange);
168 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
169 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
170 		caddr_t q = malloc(n, M_IFADDR, M_WAITOK);
171 		bzero(q, n);
172 		if (ifnet_addrs) {
173 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
174 			free((caddr_t)ifnet_addrs, M_IFADDR);
175 		}
176 		ifnet_addrs = (struct ifaddr **)q;
177 
178 		/* grow ifindex2ifnet */
179 		n = if_indexlim * sizeof(struct ifnet *);
180 		q = malloc(n, M_IFADDR, M_WAITOK);
181 		bzero(q, n);
182 		if (ifindex2ifnet) {
183 			bcopy((caddr_t)ifindex2ifnet, q, n/2);
184 			free((caddr_t)ifindex2ifnet, M_IFADDR);
185 		}
186 		ifindex2ifnet = (struct ifnet **)q;
187 	}
188 
189 	ifindex2ifnet[if_index] = ifp;
190 
191 	mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, MTX_DEF);
192 
193 	/*
194 	 * create a Link Level name for this device
195 	 */
196 	namelen = snprintf(workbuf, sizeof(workbuf),
197 	    "%s%d", ifp->if_name, ifp->if_unit);
198 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
199 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
200 	socksize = masklen + ifp->if_addrlen;
201 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
202 	if (socksize < sizeof(*sdl))
203 		socksize = sizeof(*sdl);
204 	socksize = ROUNDUP(socksize);
205 	ifasize = sizeof(*ifa) + 2 * socksize;
206 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
207 	if (ifa) {
208 		bzero((caddr_t)ifa, ifasize);
209 		sdl = (struct sockaddr_dl *)(ifa + 1);
210 		sdl->sdl_len = socksize;
211 		sdl->sdl_family = AF_LINK;
212 		bcopy(workbuf, sdl->sdl_data, namelen);
213 		sdl->sdl_nlen = namelen;
214 		sdl->sdl_index = ifp->if_index;
215 		sdl->sdl_type = ifp->if_type;
216 		ifnet_addrs[if_index - 1] = ifa;
217 		ifa->ifa_ifp = ifp;
218 		ifa->ifa_rtrequest = link_rtrequest;
219 		ifa->ifa_addr = (struct sockaddr *)sdl;
220 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
221 		ifa->ifa_netmask = (struct sockaddr *)sdl;
222 		sdl->sdl_len = masklen;
223 		while (namelen != 0)
224 			sdl->sdl_data[--namelen] = 0xff;
225 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
226 	}
227 }
228 
229 /*
230  * Detach an interface, removing it from the
231  * list of "active" interfaces.
232  */
233 void
234 if_detach(ifp)
235 	struct ifnet *ifp;
236 {
237 	struct ifaddr *ifa;
238 	struct radix_node_head	*rnh;
239 	int s;
240 	int i;
241 
242 	/*
243 	 * Remove routes and flush queues.
244 	 */
245 	s = splnet();
246 	if_down(ifp);
247 
248 	/*
249 	 * Remove address from ifnet_addrs[] and maybe decrement if_index.
250 	 * Clean up all addresses.
251 	 */
252 	ifnet_addrs[ifp->if_index - 1] = 0;
253 	while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
254 		if_index--;
255 
256 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
257 	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
258 #ifdef INET
259 		/* XXX: Ugly!! ad hoc just for INET */
260 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
261 			struct ifaliasreq ifr;
262 
263 			bzero(&ifr, sizeof(ifr));
264 			ifr.ifra_addr = *ifa->ifa_addr;
265 			if (ifa->ifa_dstaddr)
266 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
267 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
268 			    NULL) == 0)
269 				continue;
270 		}
271 #endif /* INET */
272 #ifdef INET6
273 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
274 			in6_purgeaddr(ifa, ifp);
275 			/* ifp_addrhead is already updated */
276 			continue;
277 		}
278 #endif /* INET6 */
279 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
280 		IFAFREE(ifa);
281 	}
282 
283 	/*
284 	 * Delete all remaining routes using this interface
285 	 * Unfortuneatly the only way to do this is to slog through
286 	 * the entire routing table looking for routes which point
287 	 * to this interface...oh well...
288 	 */
289 	for (i = 1; i <= AF_MAX; i++) {
290 		if ((rnh = rt_tables[i]) == NULL)
291 			continue;
292 		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
293 	}
294 
295 #ifdef INET6
296 	/* nuke all IPv6 kernel structs related to ifp */
297 	in6_ifdetach(ifp);
298 #endif
299 
300 	TAILQ_REMOVE(&ifnet, ifp, if_link);
301 	mtx_destroy(&ifp->if_snd.ifq_mtx);
302 	splx(s);
303 }
304 
305 /*
306  * Delete Routes for a Network Interface
307  *
308  * Called for each routing entry via the rnh->rnh_walktree() call above
309  * to delete all route entries referencing a detaching network interface.
310  *
311  * Arguments:
312  *	rn	pointer to node in the routing table
313  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
314  *
315  * Returns:
316  *	0	successful
317  *	errno	failed - reason indicated
318  *
319  */
320 static int
321 if_rtdel(rn, arg)
322 	struct radix_node	*rn;
323 	void			*arg;
324 {
325 	struct rtentry	*rt = (struct rtentry *)rn;
326 	struct ifnet	*ifp = arg;
327 	int		err;
328 
329 	if (rt->rt_ifp == ifp) {
330 
331 		/*
332 		 * Protect (sorta) against walktree recursion problems
333 		 * with cloned routes
334 		 */
335 		if ((rt->rt_flags & RTF_UP) == 0)
336 			return (0);
337 
338 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
339 				rt_mask(rt), rt->rt_flags,
340 				(struct rtentry **) NULL);
341 		if (err) {
342 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
343 		}
344 	}
345 
346 	return (0);
347 }
348 
349 /*
350  * Locate an interface based on a complete address.
351  */
352 /*ARGSUSED*/
353 struct ifaddr *
354 ifa_ifwithaddr(addr)
355 	register struct sockaddr *addr;
356 {
357 	register struct ifnet *ifp;
358 	register struct ifaddr *ifa;
359 
360 #define	equal(a1, a2) \
361   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
362 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
363 	    for (ifa = ifp->if_addrhead.tqh_first; ifa;
364 		 ifa = ifa->ifa_link.tqe_next) {
365 		if (ifa->ifa_addr->sa_family != addr->sa_family)
366 			continue;
367 		if (equal(addr, ifa->ifa_addr))
368 			return (ifa);
369 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
370 		    /* IP6 doesn't have broadcast */
371 		    ifa->ifa_broadaddr->sa_len != 0 &&
372 		    equal(ifa->ifa_broadaddr, addr))
373 			return (ifa);
374 	}
375 	return ((struct ifaddr *)0);
376 }
377 /*
378  * Locate the point to point interface with a given destination address.
379  */
380 /*ARGSUSED*/
381 struct ifaddr *
382 ifa_ifwithdstaddr(addr)
383 	register struct sockaddr *addr;
384 {
385 	register struct ifnet *ifp;
386 	register struct ifaddr *ifa;
387 
388 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
389 	    if (ifp->if_flags & IFF_POINTOPOINT)
390 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
391 		     ifa = ifa->ifa_link.tqe_next) {
392 			if (ifa->ifa_addr->sa_family != addr->sa_family)
393 				continue;
394 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
395 				return (ifa);
396 	}
397 	return ((struct ifaddr *)0);
398 }
399 
400 /*
401  * Find an interface on a specific network.  If many, choice
402  * is most specific found.
403  */
404 struct ifaddr *
405 ifa_ifwithnet(addr)
406 	struct sockaddr *addr;
407 {
408 	register struct ifnet *ifp;
409 	register struct ifaddr *ifa;
410 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
411 	u_int af = addr->sa_family;
412 	char *addr_data = addr->sa_data, *cplim;
413 
414 	/*
415 	 * AF_LINK addresses can be looked up directly by their index number,
416 	 * so do that if we can.
417 	 */
418 	if (af == AF_LINK) {
419 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
420 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
421 		return (ifnet_addrs[sdl->sdl_index - 1]);
422 	}
423 
424 	/*
425 	 * Scan though each interface, looking for ones that have
426 	 * addresses in this address family.
427 	 */
428 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
429 		for (ifa = ifp->if_addrhead.tqh_first; ifa;
430 		     ifa = ifa->ifa_link.tqe_next) {
431 			register char *cp, *cp2, *cp3;
432 
433 			if (ifa->ifa_addr->sa_family != af)
434 next:				continue;
435 			if (
436 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */
437 			    addr->sa_family != AF_INET6 &&
438 #endif
439 			    ifp->if_flags & IFF_POINTOPOINT) {
440 				/*
441 				 * This is a bit broken as it doesn't
442 				 * take into account that the remote end may
443 				 * be a single node in the network we are
444 				 * looking for.
445 				 * The trouble is that we don't know the
446 				 * netmask for the remote end.
447 				 */
448 				if (ifa->ifa_dstaddr != 0
449 				    && equal(addr, ifa->ifa_dstaddr))
450  					return (ifa);
451 			} else {
452 				/*
453 				 * if we have a special address handler,
454 				 * then use it instead of the generic one.
455 				 */
456 	          		if (ifa->ifa_claim_addr) {
457 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
458 						return (ifa);
459 					} else {
460 						continue;
461 					}
462 				}
463 
464 				/*
465 				 * Scan all the bits in the ifa's address.
466 				 * If a bit dissagrees with what we are
467 				 * looking for, mask it with the netmask
468 				 * to see if it really matters.
469 				 * (A byte at a time)
470 				 */
471 				if (ifa->ifa_netmask == 0)
472 					continue;
473 				cp = addr_data;
474 				cp2 = ifa->ifa_addr->sa_data;
475 				cp3 = ifa->ifa_netmask->sa_data;
476 				cplim = ifa->ifa_netmask->sa_len
477 					+ (char *)ifa->ifa_netmask;
478 				while (cp3 < cplim)
479 					if ((*cp++ ^ *cp2++) & *cp3++)
480 						goto next; /* next address! */
481 				/*
482 				 * If the netmask of what we just found
483 				 * is more specific than what we had before
484 				 * (if we had one) then remember the new one
485 				 * before continuing to search
486 				 * for an even better one.
487 				 */
488 				if (ifa_maybe == 0 ||
489 				    rn_refines((caddr_t)ifa->ifa_netmask,
490 				    (caddr_t)ifa_maybe->ifa_netmask))
491 					ifa_maybe = ifa;
492 			}
493 		}
494 	}
495 	return (ifa_maybe);
496 }
497 
498 /*
499  * Find an interface address specific to an interface best matching
500  * a given address.
501  */
502 struct ifaddr *
503 ifaof_ifpforaddr(addr, ifp)
504 	struct sockaddr *addr;
505 	register struct ifnet *ifp;
506 {
507 	register struct ifaddr *ifa;
508 	register char *cp, *cp2, *cp3;
509 	register char *cplim;
510 	struct ifaddr *ifa_maybe = 0;
511 	u_int af = addr->sa_family;
512 
513 	if (af >= AF_MAX)
514 		return (0);
515 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
516 	     ifa = ifa->ifa_link.tqe_next) {
517 		if (ifa->ifa_addr->sa_family != af)
518 			continue;
519 		if (ifa_maybe == 0)
520 			ifa_maybe = ifa;
521 		if (ifa->ifa_netmask == 0) {
522 			if (equal(addr, ifa->ifa_addr) ||
523 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
524 				return (ifa);
525 			continue;
526 		}
527 		if (ifp->if_flags & IFF_POINTOPOINT) {
528 			if (equal(addr, ifa->ifa_dstaddr))
529 				return (ifa);
530 		} else {
531 			cp = addr->sa_data;
532 			cp2 = ifa->ifa_addr->sa_data;
533 			cp3 = ifa->ifa_netmask->sa_data;
534 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
535 			for (; cp3 < cplim; cp3++)
536 				if ((*cp++ ^ *cp2++) & *cp3)
537 					break;
538 			if (cp3 == cplim)
539 				return (ifa);
540 		}
541 	}
542 	return (ifa_maybe);
543 }
544 
545 #include <net/route.h>
546 
547 /*
548  * Default action when installing a route with a Link Level gateway.
549  * Lookup an appropriate real ifa to point to.
550  * This should be moved to /sys/net/link.c eventually.
551  */
552 static void
553 link_rtrequest(cmd, rt, sa)
554 	int cmd;
555 	register struct rtentry *rt;
556 	struct sockaddr *sa;
557 {
558 	register struct ifaddr *ifa;
559 	struct sockaddr *dst;
560 	struct ifnet *ifp;
561 
562 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
563 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
564 		return;
565 	ifa = ifaof_ifpforaddr(dst, ifp);
566 	if (ifa) {
567 		IFAFREE(rt->rt_ifa);
568 		rt->rt_ifa = ifa;
569 		ifa->ifa_refcnt++;
570 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
571 			ifa->ifa_rtrequest(cmd, rt, sa);
572 	}
573 }
574 
575 /*
576  * Mark an interface down and notify protocols of
577  * the transition.
578  * NOTE: must be called at splnet or eqivalent.
579  */
580 void
581 if_unroute(ifp, flag, fam)
582 	register struct ifnet *ifp;
583 	int flag, fam;
584 {
585 	register struct ifaddr *ifa;
586 
587 	ifp->if_flags &= ~flag;
588 	getmicrotime(&ifp->if_lastchange);
589 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
590 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
591 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
592 	if_qflush(&ifp->if_snd);
593 	rt_ifmsg(ifp);
594 }
595 
596 /*
597  * Mark an interface up and notify protocols of
598  * the transition.
599  * NOTE: must be called at splnet or eqivalent.
600  */
601 void
602 if_route(ifp, flag, fam)
603 	register struct ifnet *ifp;
604 	int flag, fam;
605 {
606 	register struct ifaddr *ifa;
607 
608 	ifp->if_flags |= flag;
609 	getmicrotime(&ifp->if_lastchange);
610 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
611 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
612 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
613 	rt_ifmsg(ifp);
614 #ifdef INET6
615 	in6_if_up(ifp);
616 #endif
617 }
618 
619 /*
620  * Mark an interface down and notify protocols of
621  * the transition.
622  * NOTE: must be called at splnet or eqivalent.
623  */
624 void
625 if_down(ifp)
626 	register struct ifnet *ifp;
627 {
628 
629 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
630 }
631 
632 /*
633  * Mark an interface up and notify protocols of
634  * the transition.
635  * NOTE: must be called at splnet or eqivalent.
636  */
637 void
638 if_up(ifp)
639 	register struct ifnet *ifp;
640 {
641 
642 	if_route(ifp, IFF_UP, AF_UNSPEC);
643 }
644 
645 /*
646  * Flush an interface queue.
647  */
648 static void
649 if_qflush(ifq)
650 	register struct ifqueue *ifq;
651 {
652 	register struct mbuf *m, *n;
653 
654 	n = ifq->ifq_head;
655 	while ((m = n) != 0) {
656 		n = m->m_act;
657 		m_freem(m);
658 	}
659 	ifq->ifq_head = 0;
660 	ifq->ifq_tail = 0;
661 	ifq->ifq_len = 0;
662 }
663 
664 /*
665  * Handle interface watchdog timer routines.  Called
666  * from softclock, we decrement timers (if set) and
667  * call the appropriate interface routine on expiration.
668  */
669 static void
670 if_slowtimo(arg)
671 	void *arg;
672 {
673 	register struct ifnet *ifp;
674 	int s = splimp();
675 
676 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
677 		if (ifp->if_timer == 0 || --ifp->if_timer)
678 			continue;
679 		if (ifp->if_watchdog)
680 			(*ifp->if_watchdog)(ifp);
681 	}
682 	splx(s);
683 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
684 }
685 
686 /*
687  * Map interface name to
688  * interface structure pointer.
689  */
690 struct ifnet *
691 ifunit(char *name)
692 {
693 	char namebuf[IFNAMSIZ + 1];
694 	char *cp;
695 	struct ifnet *ifp;
696 	int unit;
697 	unsigned len, m;
698 	char c;
699 
700 	len = strlen(name);
701 	if (len < 2 || len > IFNAMSIZ)
702 		return NULL;
703 	cp = name + len - 1;
704 	c = *cp;
705 	if (c < '0' || c > '9')
706 		return NULL;		/* trailing garbage */
707 	unit = 0;
708 	m = 1;
709 	do {
710 		if (cp == name)
711 			return NULL;	/* no interface name */
712 		unit += (c - '0') * m;
713 		if (unit > 1000000)
714 			return NULL;	/* number is unreasonable */
715 		m *= 10;
716 		c = *--cp;
717 	} while (c >= '0' && c <= '9');
718 	len = cp - name + 1;
719 	bcopy(name, namebuf, len);
720 	namebuf[len] = '\0';
721 	/*
722 	 * Now search all the interfaces for this name/number
723 	 */
724 	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) {
725 		if (strcmp(ifp->if_name, namebuf))
726 			continue;
727 		if (unit == ifp->if_unit)
728 			break;
729 	}
730 	return (ifp);
731 }
732 
733 
734 /*
735  * Map interface name in a sockaddr_dl to
736  * interface structure pointer.
737  */
738 struct ifnet *
739 if_withname(sa)
740 	struct sockaddr *sa;
741 {
742 	char ifname[IFNAMSIZ+1];
743 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
744 
745 	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
746 	     (sdl->sdl_nlen > IFNAMSIZ) )
747 		return NULL;
748 
749 	/*
750 	 * ifunit wants a null-terminated name.  It may not be null-terminated
751 	 * in the sockaddr.  We don't want to change the caller's sockaddr,
752 	 * and there might not be room to put the trailing null anyway, so we
753 	 * make a local copy that we know we can null terminate safely.
754 	 */
755 
756 	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
757 	ifname[sdl->sdl_nlen] = '\0';
758 	return ifunit(ifname);
759 }
760 
761 
762 /*
763  * Interface ioctls.
764  */
765 int
766 ifioctl(so, cmd, data, p)
767 	struct socket *so;
768 	u_long cmd;
769 	caddr_t data;
770 	struct proc *p;
771 {
772 	register struct ifnet *ifp;
773 	register struct ifreq *ifr;
774 	struct ifstat *ifs;
775 	int error;
776 	short oif_flags;
777 
778 	switch (cmd) {
779 
780 	case SIOCGIFCONF:
781 	case OSIOCGIFCONF:
782 		return (ifconf(cmd, data));
783 	}
784 	ifr = (struct ifreq *)data;
785 	ifp = ifunit(ifr->ifr_name);
786 	if (ifp == 0)
787 		return (ENXIO);
788 	switch (cmd) {
789 
790 	case SIOCGIFFLAGS:
791 		ifr->ifr_flags = ifp->if_flags;
792 		break;
793 
794 	case SIOCGIFMETRIC:
795 		ifr->ifr_metric = ifp->if_metric;
796 		break;
797 
798 	case SIOCGIFMTU:
799 		ifr->ifr_mtu = ifp->if_mtu;
800 		break;
801 
802 	case SIOCGIFPHYS:
803 		ifr->ifr_phys = ifp->if_physical;
804 		break;
805 
806 	case SIOCSIFFLAGS:
807 		error = suser(p);
808 		if (error)
809 			return (error);
810 		ifr->ifr_prevflags = ifp->if_flags;
811 		if (ifp->if_flags & IFF_SMART) {
812 			/* Smart drivers twiddle their own routes */
813 		} else if (ifp->if_flags & IFF_UP &&
814 		    (ifr->ifr_flags & IFF_UP) == 0) {
815 			int s = splimp();
816 			if_down(ifp);
817 			splx(s);
818 		} else if (ifr->ifr_flags & IFF_UP &&
819 		    (ifp->if_flags & IFF_UP) == 0) {
820 			int s = splimp();
821 			if_up(ifp);
822 			splx(s);
823 		}
824 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
825 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
826 		if (ifp->if_ioctl)
827 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
828 		getmicrotime(&ifp->if_lastchange);
829 		break;
830 
831 	case SIOCSIFMETRIC:
832 		error = suser(p);
833 		if (error)
834 			return (error);
835 		ifp->if_metric = ifr->ifr_metric;
836 		getmicrotime(&ifp->if_lastchange);
837 		break;
838 
839 	case SIOCSIFPHYS:
840 		error = suser(p);
841 		if (error)
842 			return error;
843 		if (!ifp->if_ioctl)
844 		        return EOPNOTSUPP;
845 		error = (*ifp->if_ioctl)(ifp, cmd, data);
846 		if (error == 0)
847 			getmicrotime(&ifp->if_lastchange);
848 		return(error);
849 
850 	case SIOCSIFMTU:
851 	{
852 		u_long oldmtu = ifp->if_mtu;
853 
854 		error = suser(p);
855 		if (error)
856 			return (error);
857 		if (ifp->if_ioctl == NULL)
858 			return (EOPNOTSUPP);
859 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
860 			return (EINVAL);
861 		error = (*ifp->if_ioctl)(ifp, cmd, data);
862 		if (error == 0) {
863 			getmicrotime(&ifp->if_lastchange);
864 			rt_ifmsg(ifp);
865 		}
866 		/*
867 		 * If the link MTU changed, do network layer specific procedure.
868 		 */
869 		if (ifp->if_mtu != oldmtu) {
870 #ifdef INET6
871 			nd6_setmtu(ifp);
872 #endif
873 		}
874 		return (error);
875 	}
876 
877 	case SIOCADDMULTI:
878 	case SIOCDELMULTI:
879 		error = suser(p);
880 		if (error)
881 			return (error);
882 
883 		/* Don't allow group membership on non-multicast interfaces. */
884 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
885 			return EOPNOTSUPP;
886 
887 		/* Don't let users screw up protocols' entries. */
888 		if (ifr->ifr_addr.sa_family != AF_LINK)
889 			return EINVAL;
890 
891 		if (cmd == SIOCADDMULTI) {
892 			struct ifmultiaddr *ifma;
893 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
894 		} else {
895 			error = if_delmulti(ifp, &ifr->ifr_addr);
896 		}
897 		if (error == 0)
898 			getmicrotime(&ifp->if_lastchange);
899 		return error;
900 
901 	case SIOCSIFPHYADDR:
902 	case SIOCDIFPHYADDR:
903 #ifdef INET6
904 	case SIOCSIFPHYADDR_IN6:
905 #endif
906         case SIOCSIFMEDIA:
907 	case SIOCSIFGENERIC:
908 		error = suser(p);
909 		if (error)
910 			return (error);
911 		if (ifp->if_ioctl == 0)
912 			return (EOPNOTSUPP);
913 		error = (*ifp->if_ioctl)(ifp, cmd, data);
914 		if (error == 0)
915 			getmicrotime(&ifp->if_lastchange);
916 		return error;
917 
918 	case SIOCGIFSTATUS:
919 		ifs = (struct ifstat *)data;
920 		ifs->ascii[0] = '\0';
921 
922 	case SIOCGIFMEDIA:
923 	case SIOCGIFGENERIC:
924 		if (ifp->if_ioctl == 0)
925 			return (EOPNOTSUPP);
926 		return ((*ifp->if_ioctl)(ifp, cmd, data));
927 
928 	case SIOCSIFLLADDR:
929 		error = suser(p);
930 		if (error)
931 			return (error);
932 		return if_setlladdr(ifp,
933 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
934 
935 	default:
936 		oif_flags = ifp->if_flags;
937 		if (so->so_proto == 0)
938 			return (EOPNOTSUPP);
939 #ifndef COMPAT_43
940 		error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
941 								 data,
942 								 ifp, p));
943 #else
944 	    {
945 		int ocmd = cmd;
946 
947 		switch (cmd) {
948 
949 		case SIOCSIFDSTADDR:
950 		case SIOCSIFADDR:
951 		case SIOCSIFBRDADDR:
952 		case SIOCSIFNETMASK:
953 #if BYTE_ORDER != BIG_ENDIAN
954 			if (ifr->ifr_addr.sa_family == 0 &&
955 			    ifr->ifr_addr.sa_len < 16) {
956 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
957 				ifr->ifr_addr.sa_len = 16;
958 			}
959 #else
960 			if (ifr->ifr_addr.sa_len == 0)
961 				ifr->ifr_addr.sa_len = 16;
962 #endif
963 			break;
964 
965 		case OSIOCGIFADDR:
966 			cmd = SIOCGIFADDR;
967 			break;
968 
969 		case OSIOCGIFDSTADDR:
970 			cmd = SIOCGIFDSTADDR;
971 			break;
972 
973 		case OSIOCGIFBRDADDR:
974 			cmd = SIOCGIFBRDADDR;
975 			break;
976 
977 		case OSIOCGIFNETMASK:
978 			cmd = SIOCGIFNETMASK;
979 		}
980 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
981 								   cmd,
982 								   data,
983 								   ifp, p));
984 		switch (ocmd) {
985 
986 		case OSIOCGIFADDR:
987 		case OSIOCGIFDSTADDR:
988 		case OSIOCGIFBRDADDR:
989 		case OSIOCGIFNETMASK:
990 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
991 
992 		}
993 	    }
994 #endif /* COMPAT_43 */
995 
996 		if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
997 #ifdef INET6
998 			DELAY(100);/* XXX: temporal workaround for fxp issue*/
999 			if (ifp->if_flags & IFF_UP) {
1000 				int s = splimp();
1001 				in6_if_up(ifp);
1002 				splx(s);
1003 			}
1004 #endif
1005 		}
1006 		return (error);
1007 
1008 	}
1009 	return (0);
1010 }
1011 
1012 /*
1013  * Set/clear promiscuous mode on interface ifp based on the truth value
1014  * of pswitch.  The calls are reference counted so that only the first
1015  * "on" request actually has an effect, as does the final "off" request.
1016  * Results are undefined if the "off" and "on" requests are not matched.
1017  */
1018 int
1019 ifpromisc(ifp, pswitch)
1020 	struct ifnet *ifp;
1021 	int pswitch;
1022 {
1023 	struct ifreq ifr;
1024 	int error;
1025 
1026 	if (pswitch) {
1027 		/*
1028 		 * If the device is not configured up, we cannot put it in
1029 		 * promiscuous mode.
1030 		 */
1031 		if ((ifp->if_flags & IFF_UP) == 0)
1032 			return (ENETDOWN);
1033 		if (ifp->if_pcount++ != 0)
1034 			return (0);
1035 		ifp->if_flags |= IFF_PROMISC;
1036 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
1037 		    ifp->if_name, ifp->if_unit);
1038 	} else {
1039 		if (--ifp->if_pcount > 0)
1040 			return (0);
1041 		ifp->if_flags &= ~IFF_PROMISC;
1042 		log(LOG_INFO, "%s%d: promiscuous mode disabled\n",
1043 		    ifp->if_name, ifp->if_unit);
1044 	}
1045 	ifr.ifr_flags = ifp->if_flags;
1046 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1047 	if (error == 0)
1048 		rt_ifmsg(ifp);
1049 	return error;
1050 }
1051 
1052 /*
1053  * Return interface configuration
1054  * of system.  List may be used
1055  * in later ioctl's (above) to get
1056  * other information.
1057  */
1058 /*ARGSUSED*/
1059 static int
1060 ifconf(cmd, data)
1061 	u_long cmd;
1062 	caddr_t data;
1063 {
1064 	register struct ifconf *ifc = (struct ifconf *)data;
1065 	register struct ifnet *ifp = ifnet.tqh_first;
1066 	register struct ifaddr *ifa;
1067 	struct ifreq ifr, *ifrp;
1068 	int space = ifc->ifc_len, error = 0;
1069 
1070 	ifrp = ifc->ifc_req;
1071 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) {
1072 		char workbuf[64];
1073 		int ifnlen, addrs;
1074 
1075 		ifnlen = snprintf(workbuf, sizeof(workbuf),
1076 		    "%s%d", ifp->if_name, ifp->if_unit);
1077 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
1078 			error = ENAMETOOLONG;
1079 			break;
1080 		} else {
1081 			strcpy(ifr.ifr_name, workbuf);
1082 		}
1083 
1084 		addrs = 0;
1085 		ifa = ifp->if_addrhead.tqh_first;
1086 		for ( ; space > sizeof (ifr) && ifa;
1087 		    ifa = ifa->ifa_link.tqe_next) {
1088 			register struct sockaddr *sa = ifa->ifa_addr;
1089 			if (curproc->p_prison && prison_if(curproc, sa))
1090 				continue;
1091 			addrs++;
1092 #ifdef COMPAT_43
1093 			if (cmd == OSIOCGIFCONF) {
1094 				struct osockaddr *osa =
1095 					 (struct osockaddr *)&ifr.ifr_addr;
1096 				ifr.ifr_addr = *sa;
1097 				osa->sa_family = sa->sa_family;
1098 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1099 						sizeof (ifr));
1100 				ifrp++;
1101 			} else
1102 #endif
1103 			if (sa->sa_len <= sizeof(*sa)) {
1104 				ifr.ifr_addr = *sa;
1105 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1106 						sizeof (ifr));
1107 				ifrp++;
1108 			} else {
1109 				if (space < sizeof (ifr) + sa->sa_len -
1110 					    sizeof(*sa))
1111 					break;
1112 				space -= sa->sa_len - sizeof(*sa);
1113 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1114 						sizeof (ifr.ifr_name));
1115 				if (error == 0)
1116 				    error = copyout((caddr_t)sa,
1117 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1118 				ifrp = (struct ifreq *)
1119 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1120 			}
1121 			if (error)
1122 				break;
1123 			space -= sizeof (ifr);
1124 		}
1125 		if (error)
1126 			break;
1127 		if (!addrs) {
1128 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
1129 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1130 			    sizeof (ifr));
1131 			if (error)
1132 				break;
1133 			space -= sizeof (ifr);
1134 			ifrp++;
1135 		}
1136 	}
1137 	ifc->ifc_len -= space;
1138 	return (error);
1139 }
1140 
1141 /*
1142  * Just like if_promisc(), but for all-multicast-reception mode.
1143  */
1144 int
1145 if_allmulti(ifp, onswitch)
1146 	struct ifnet *ifp;
1147 	int onswitch;
1148 {
1149 	int error = 0;
1150 	int s = splimp();
1151 
1152 	if (onswitch) {
1153 		if (ifp->if_amcount++ == 0) {
1154 			ifp->if_flags |= IFF_ALLMULTI;
1155 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1156 		}
1157 	} else {
1158 		if (ifp->if_amcount > 1) {
1159 			ifp->if_amcount--;
1160 		} else {
1161 			ifp->if_amcount = 0;
1162 			ifp->if_flags &= ~IFF_ALLMULTI;
1163 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0);
1164 		}
1165 	}
1166 	splx(s);
1167 
1168 	if (error == 0)
1169 		rt_ifmsg(ifp);
1170 	return error;
1171 }
1172 
1173 /*
1174  * Add a multicast listenership to the interface in question.
1175  * The link layer provides a routine which converts
1176  */
1177 int
1178 if_addmulti(ifp, sa, retifma)
1179 	struct ifnet *ifp;	/* interface to manipulate */
1180 	struct sockaddr *sa;	/* address to add */
1181 	struct ifmultiaddr **retifma;
1182 {
1183 	struct sockaddr *llsa, *dupsa;
1184 	int error, s;
1185 	struct ifmultiaddr *ifma;
1186 
1187 	/*
1188 	 * If the matching multicast address already exists
1189 	 * then don't add a new one, just add a reference
1190 	 */
1191 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1192 	     ifma = ifma->ifma_link.le_next) {
1193 		if (equal(sa, ifma->ifma_addr)) {
1194 			ifma->ifma_refcount++;
1195 			if (retifma)
1196 				*retifma = ifma;
1197 			return 0;
1198 		}
1199 	}
1200 
1201 	/*
1202 	 * Give the link layer a chance to accept/reject it, and also
1203 	 * find out which AF_LINK address this maps to, if it isn't one
1204 	 * already.
1205 	 */
1206 	if (ifp->if_resolvemulti) {
1207 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
1208 		if (error) return error;
1209 	} else {
1210 		llsa = 0;
1211 	}
1212 
1213 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
1214 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
1215 	bcopy(sa, dupsa, sa->sa_len);
1216 
1217 	ifma->ifma_addr = dupsa;
1218 	ifma->ifma_lladdr = llsa;
1219 	ifma->ifma_ifp = ifp;
1220 	ifma->ifma_refcount = 1;
1221 	ifma->ifma_protospec = 0;
1222 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1223 
1224 	/*
1225 	 * Some network interfaces can scan the address list at
1226 	 * interrupt time; lock them out.
1227 	 */
1228 	s = splimp();
1229 	LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1230 	splx(s);
1231 	*retifma = ifma;
1232 
1233 	if (llsa != 0) {
1234 		for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1235 		     ifma = ifma->ifma_link.le_next) {
1236 			if (equal(ifma->ifma_addr, llsa))
1237 				break;
1238 		}
1239 		if (ifma) {
1240 			ifma->ifma_refcount++;
1241 		} else {
1242 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
1243 			       M_IFMADDR, M_WAITOK);
1244 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1245 			       M_IFMADDR, M_WAITOK);
1246 			bcopy(llsa, dupsa, llsa->sa_len);
1247 			ifma->ifma_addr = dupsa;
1248 			ifma->ifma_ifp = ifp;
1249 			ifma->ifma_refcount = 1;
1250 			s = splimp();
1251 			LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
1252 			splx(s);
1253 		}
1254 	}
1255 	/*
1256 	 * We are certain we have added something, so call down to the
1257 	 * interface to let them know about it.
1258 	 */
1259 	s = splimp();
1260 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
1261 	splx(s);
1262 
1263 	return 0;
1264 }
1265 
1266 /*
1267  * Remove a reference to a multicast address on this interface.  Yell
1268  * if the request does not match an existing membership.
1269  */
1270 int
1271 if_delmulti(ifp, sa)
1272 	struct ifnet *ifp;
1273 	struct sockaddr *sa;
1274 {
1275 	struct ifmultiaddr *ifma;
1276 	int s;
1277 
1278 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1279 	     ifma = ifma->ifma_link.le_next)
1280 		if (equal(sa, ifma->ifma_addr))
1281 			break;
1282 	if (ifma == 0)
1283 		return ENOENT;
1284 
1285 	if (ifma->ifma_refcount > 1) {
1286 		ifma->ifma_refcount--;
1287 		return 0;
1288 	}
1289 
1290 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
1291 	sa = ifma->ifma_lladdr;
1292 	s = splimp();
1293 	LIST_REMOVE(ifma, ifma_link);
1294 	splx(s);
1295 	free(ifma->ifma_addr, M_IFMADDR);
1296 	free(ifma, M_IFMADDR);
1297 	if (sa == 0)
1298 		return 0;
1299 
1300 	/*
1301 	 * Now look for the link-layer address which corresponds to
1302 	 * this network address.  It had been squirreled away in
1303 	 * ifma->ifma_lladdr for this purpose (so we don't have
1304 	 * to call ifp->if_resolvemulti() again), and we saved that
1305 	 * value in sa above.  If some nasty deleted the
1306 	 * link-layer address out from underneath us, we can deal because
1307 	 * the address we stored was is not the same as the one which was
1308 	 * in the record for the link-layer address.  (So we don't complain
1309 	 * in that case.)
1310 	 */
1311 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1312 	     ifma = ifma->ifma_link.le_next)
1313 		if (equal(sa, ifma->ifma_addr))
1314 			break;
1315 	if (ifma == 0)
1316 		return 0;
1317 
1318 	if (ifma->ifma_refcount > 1) {
1319 		ifma->ifma_refcount--;
1320 		return 0;
1321 	}
1322 
1323 	s = splimp();
1324 	LIST_REMOVE(ifma, ifma_link);
1325 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
1326 	splx(s);
1327 	free(ifma->ifma_addr, M_IFMADDR);
1328 	free(sa, M_IFMADDR);
1329 	free(ifma, M_IFMADDR);
1330 
1331 	return 0;
1332 }
1333 
1334 /*
1335  * Set the link layer address on an interface.
1336  *
1337  * At this time we only support certain types of interfaces,
1338  * and we don't allow the length of the address to change.
1339  */
1340 int
1341 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
1342 {
1343 	struct sockaddr_dl *sdl;
1344 	struct ifaddr *ifa;
1345 
1346 	ifa = ifnet_addrs[ifp->if_index - 1];
1347 	if (ifa == NULL)
1348 		return (EINVAL);
1349 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1350 	if (sdl == NULL)
1351 		return (EINVAL);
1352 	if (len != sdl->sdl_alen)	/* don't allow length to change */
1353 		return (EINVAL);
1354 	switch (ifp->if_type) {
1355 	case IFT_ETHER:			/* these types use struct arpcom */
1356 	case IFT_FDDI:
1357 	case IFT_XETHER:
1358 	case IFT_ISO88025:
1359 	case IFT_PROPVIRTUAL:		/* XXX waiting for IFT_8021_VLAN */
1360 		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
1361 		bcopy(lladdr, LLADDR(sdl), len);
1362 		break;
1363 	default:
1364 		return (ENODEV);
1365 	}
1366 	/*
1367 	 * If the interface is already up, we need
1368 	 * to re-init it in order to reprogram its
1369 	 * address filter.
1370 	 */
1371 	if ((ifp->if_flags & IFF_UP) != 0) {
1372 		ifp->if_flags &= ~IFF_UP;
1373 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1374 		ifp->if_flags |= IFF_UP;
1375 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL);
1376 	}
1377 	return (0);
1378 }
1379 
1380 struct ifmultiaddr *
1381 ifmaof_ifpforaddr(sa, ifp)
1382 	struct sockaddr *sa;
1383 	struct ifnet *ifp;
1384 {
1385 	struct ifmultiaddr *ifma;
1386 
1387 	for (ifma = ifp->if_multiaddrs.lh_first; ifma;
1388 	     ifma = ifma->ifma_link.le_next)
1389 		if (equal(ifma->ifma_addr, sa))
1390 			break;
1391 
1392 	return ifma;
1393 }
1394 
1395 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
1396 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1397