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