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