xref: /dragonfly/sys/netinet/in.c (revision 0085a56d)
1 /*
2  * Copyright (c) 1982, 1986, 1991, 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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)in.c	8.4 (Berkeley) 1/9/95
30  * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
31  */
32 
33 #include "opt_bootp.h"
34 #include "opt_carp.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41 #include <sys/priv.h>
42 #include <sys/msgport.h>
43 #include <sys/socket.h>
44 
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
48 
49 #include <net/if.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
52 #include <net/netmsg2.h>
53 #include <net/netisr2.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/udp_var.h>
59 
60 #include <netinet/igmp_var.h>
61 
62 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
63 
64 static int in_mask2len (struct in_addr *);
65 static void in_len2mask (struct in_addr *, int);
66 static int in_lifaddr_ioctl (u_long, caddr_t, struct ifnet *, struct thread *);
67 
68 static void	in_socktrim (struct sockaddr_in *);
69 static int	in_ifinit(struct ifnet *, struct in_ifaddr *,
70 		    const struct sockaddr_in *, int);
71 
72 static int	in_control_internal(u_long, caddr_t, struct ifnet *,
73 		    struct thread *);
74 
75 static int	in_addprefix(struct in_ifaddr *, int);
76 static void	in_scrubprefix(struct in_ifaddr *);
77 
78 static int subnetsarelocal = 0;
79 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
80     &subnetsarelocal, 0,
81     "Count all internet addresses of subnets of the local net as local");
82 
83 struct in_multihead in_multihead; /* XXX BSS initialization */
84 
85 extern struct inpcbinfo ripcbinfo;
86 
87 /*
88  * Return 1 if an internet address is for a ``local'' host
89  * (one to which we have a connection).  If subnetsarelocal
90  * is true, this includes other subnets of the local net.
91  * Otherwise, it includes only the directly-connected (sub)nets.
92  */
93 int
94 in_localaddr(struct in_addr in)
95 {
96 	u_long i = ntohl(in.s_addr);
97 	struct in_ifaddr_container *iac;
98 	struct in_ifaddr *ia;
99 
100 	if (subnetsarelocal) {
101 		TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
102 			ia = iac->ia;
103 
104 			if ((i & ia->ia_netmask) == ia->ia_net)
105 				return (1);
106 		}
107 	} else {
108 		TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
109 			ia = iac->ia;
110 
111 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
112 				return (1);
113 		}
114 	}
115 	return (0);
116 }
117 
118 /*
119  * Determine whether an IP address is in a reserved set of addresses
120  * that may not be forwarded, or whether datagrams to that destination
121  * may be forwarded.
122  */
123 int
124 in_canforward(struct in_addr in)
125 {
126 	u_long i = ntohl(in.s_addr);
127 	u_long net;
128 
129 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
130 		return (0);
131 	if (IN_CLASSA(i)) {
132 		net = i & IN_CLASSA_NET;
133 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
134 			return (0);
135 	}
136 	return (1);
137 }
138 
139 /*
140  * Trim a mask in a sockaddr
141  */
142 static void
143 in_socktrim(struct sockaddr_in *ap)
144 {
145     char *cplim = (char *) &ap->sin_addr;
146     char *cp = (char *) (&ap->sin_addr + 1);
147 
148     ap->sin_len = 0;
149     while (--cp >= cplim)
150 	if (*cp) {
151 	    (ap)->sin_len = cp - (char *) (ap) + 1;
152 	    break;
153 	}
154 }
155 
156 static int
157 in_mask2len(struct in_addr *mask)
158 {
159 	int x, y;
160 	u_char *p;
161 
162 	p = (u_char *)mask;
163 	for (x = 0; x < sizeof *mask; x++) {
164 		if (p[x] != 0xff)
165 			break;
166 	}
167 	y = 0;
168 	if (x < sizeof *mask) {
169 		for (y = 0; y < 8; y++) {
170 			if ((p[x] & (0x80 >> y)) == 0)
171 				break;
172 		}
173 	}
174 	return x * 8 + y;
175 }
176 
177 static void
178 in_len2mask(struct in_addr *mask, int len)
179 {
180 	int i;
181 	u_char *p;
182 
183 	p = (u_char *)mask;
184 	bzero(mask, sizeof *mask);
185 	for (i = 0; i < len / 8; i++)
186 		p[i] = 0xff;
187 	if (len % 8)
188 		p[i] = (0xff00 >> (len % 8)) & 0xff;
189 }
190 
191 void
192 in_control_dispatch(netmsg_t msg)
193 {
194 	int error;
195 
196 	error = in_control(msg->control.nm_cmd, msg->control.nm_data,
197 	    msg->control.nm_ifp, msg->control.nm_td);
198 	lwkt_replymsg(&msg->lmsg, error);
199 }
200 
201 static void
202 in_control_internal_dispatch(netmsg_t msg)
203 {
204 	int error;
205 
206 	error = in_control_internal(msg->control.nm_cmd,
207 				    msg->control.nm_data,
208 				    msg->control.nm_ifp,
209 				    msg->control.nm_td);
210 	lwkt_replymsg(&msg->lmsg, error);
211 }
212 
213 /*
214  * Generic internet control operations (ioctl's).
215  * Ifp is 0 if not an interface-specific ioctl.
216  *
217  * NOTE! td might be NULL.
218  */
219 int
220 in_control(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
221 {
222 	struct netmsg_pru_control msg;
223 
224 	switch (cmd) {
225 	/* change address */
226 	case SIOCALIFADDR:
227 	case SIOCDLIFADDR:
228 	case SIOCSIFDSTADDR:
229 	case SIOCSIFBRDADDR:
230 	case SIOCSIFADDR:
231 	case SIOCSIFNETMASK:
232 	case SIOCAIFADDR:
233 	case SIOCDIFADDR:
234 	case SIOCGIFALIAS:
235 		/*
236 		 * Dispatch these SIOCs to netisr0.
237 		 */
238 		netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
239 		    in_control_internal_dispatch);
240 		msg.nm_cmd = cmd;
241 		msg.nm_data = data;
242 		msg.nm_ifp = ifp;
243 		msg.nm_td = td;
244 		lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
245 		return msg.base.lmsg.ms_error;
246 
247 	default:
248 		return in_control_internal(cmd, data, ifp, td);
249 	}
250 }
251 
252 static void
253 in_ialink_dispatch(netmsg_t msg)
254 {
255 	struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
256 	struct ifaddr_container *ifac;
257 	struct in_ifaddr_container *iac;
258 	int cpu = mycpuid;
259 
260 	crit_enter();
261 
262 	ifac = &ia->ia_ifa.ifa_containers[cpu];
263 	ASSERT_IFAC_VALID(ifac);
264 	KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
265 		("ia is on in_ifaddrheads"));
266 
267 	ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
268 	iac = &ifac->ifa_proto_u.u_in_ifac;
269 	TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
270 
271 	crit_exit();
272 
273 	netisr_forwardmsg_all(&msg->base, cpu + 1);
274 }
275 
276 static void
277 in_iaunlink_dispatch(netmsg_t msg)
278 {
279 	struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
280 	struct ifaddr_container *ifac;
281 	struct in_ifaddr_container *iac;
282 	int cpu = mycpuid;
283 
284 	crit_enter();
285 
286 	ifac = &ia->ia_ifa.ifa_containers[cpu];
287 	ASSERT_IFAC_VALID(ifac);
288 	KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
289 		("ia is not on in_ifaddrheads"));
290 
291 	iac = &ifac->ifa_proto_u.u_in_ifac;
292 	TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
293 	ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
294 
295 	crit_exit();
296 
297 	netisr_forwardmsg_all(&msg->base, cpu + 1);
298 }
299 
300 static void
301 in_iahashins_dispatch(netmsg_t msg)
302 {
303 	struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
304 	struct ifaddr_container *ifac;
305 	struct in_ifaddr_container *iac;
306 	int cpu = mycpuid;
307 
308 	crit_enter();
309 
310 	ifac = &ia->ia_ifa.ifa_containers[cpu];
311 	ASSERT_IFAC_VALID(ifac);
312 	KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
313 		("ia is on in_ifaddrhashtbls"));
314 
315 	ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
316 	iac = &ifac->ifa_proto_u.u_in_ifac;
317 	LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
318 			 iac, ia_hash);
319 
320 	crit_exit();
321 
322 	netisr_forwardmsg_all(&msg->base, cpu + 1);
323 }
324 
325 static void
326 in_iahashrem_dispatch(netmsg_t msg)
327 {
328 	struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
329 	struct ifaddr_container *ifac;
330 	struct in_ifaddr_container *iac;
331 	int cpu = mycpuid;
332 
333 	crit_enter();
334 
335 	ifac = &ia->ia_ifa.ifa_containers[cpu];
336 	ASSERT_IFAC_VALID(ifac);
337 	KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
338 		("ia is not on in_ifaddrhashtbls"));
339 
340 	iac = &ifac->ifa_proto_u.u_in_ifac;
341 	LIST_REMOVE(iac, ia_hash);
342 	ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
343 
344 	crit_exit();
345 
346 	netisr_forwardmsg_all(&msg->base, cpu + 1);
347 }
348 
349 static void
350 in_ialink(struct in_ifaddr *ia)
351 {
352 	struct netmsg_base msg;
353 
354 	netmsg_init(&msg, NULL, &curthread->td_msgport,
355 		    0, in_ialink_dispatch);
356 	msg.lmsg.u.ms_resultp = ia;
357 
358 	netisr_domsg(&msg, 0);
359 }
360 
361 void
362 in_iaunlink(struct in_ifaddr *ia)
363 {
364 	struct netmsg_base msg;
365 
366 	netmsg_init(&msg, NULL, &curthread->td_msgport,
367 		    0, in_iaunlink_dispatch);
368 	msg.lmsg.u.ms_resultp = ia;
369 
370 	netisr_domsg(&msg, 0);
371 }
372 
373 void
374 in_iahash_insert(struct in_ifaddr *ia)
375 {
376 	struct netmsg_base msg;
377 
378 	netmsg_init(&msg, NULL, &curthread->td_msgport,
379 		    0, in_iahashins_dispatch);
380 	msg.lmsg.u.ms_resultp = ia;
381 
382 	netisr_domsg(&msg, 0);
383 }
384 
385 void
386 in_iahash_remove(struct in_ifaddr *ia)
387 {
388 	struct netmsg_base msg;
389 
390 	netmsg_init(&msg, NULL, &curthread->td_msgport,
391 		    0, in_iahashrem_dispatch);
392 	msg.lmsg.u.ms_resultp = ia;
393 
394 	netisr_domsg(&msg, 0);
395 }
396 
397 static __inline struct in_ifaddr *
398 in_ianext(struct in_ifaddr *oia)
399 {
400 	struct ifaddr_container *ifac;
401 	struct in_ifaddr_container *iac;
402 
403 	ifac = &oia->ia_ifa.ifa_containers[mycpuid];
404 	ASSERT_IFAC_VALID(ifac);
405 	KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
406 		("ia is not on in_ifaddrheads"));
407 
408 	iac = &ifac->ifa_proto_u.u_in_ifac;
409 	iac = TAILQ_NEXT(iac, ia_link);
410 	if (iac != NULL)
411 		return iac->ia;
412 	else
413 		return NULL;
414 }
415 
416 static int
417 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
418 		    struct thread *td)
419 {
420 	struct ifreq *ifr = (struct ifreq *)data;
421 	struct in_ifaddr *ia = NULL;
422 	struct in_addr dst;
423 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
424 	struct ifaddr_container *ifac;
425 	struct in_ifaddr_container *iac;
426 	struct sockaddr_in oldaddr;
427 	int hostIsNew, iaIsNew, maskIsNew, ifpWasUp;
428 	int error = 0;
429 
430 	switch (cmd) {
431 	case SIOCALIFADDR:
432 	case SIOCDLIFADDR:
433 		if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
434 			return error;
435 		/* FALLTHROUGH */
436 	case SIOCGLIFADDR:
437 		if (!ifp)
438 			return EINVAL;
439 		return in_lifaddr_ioctl(cmd, data, ifp, td);
440 	}
441 
442 	iaIsNew = 0;
443 	ifpWasUp = 0;
444 
445 	/*
446 	 * Find address for this interface, if it exists.
447 	 *
448 	 * If an alias address was specified, find that one instead of
449 	 * the first one on the interface, if possible
450 	 */
451 	if (ifp) {
452 		struct in_ifaddr *iap;
453 
454 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
455 		LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
456 			iap = iac->ia;
457 			if (iap->ia_ifp == ifp &&
458 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
459 				ia = iap;
460 				break;
461 			}
462 		}
463 		if (ia == NULL) {
464 			TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
465 				      ifa_link) {
466 				iap = ifatoia(ifac->ifa);
467 				if (iap->ia_addr.sin_family == AF_INET) {
468 					ia = iap;
469 					break;
470 				}
471 			}
472 		}
473 
474 		if (ifp->if_flags & IFF_UP)
475 			ifpWasUp = 1;
476 	}
477 
478 	switch (cmd) {
479 	case SIOCAIFADDR:
480 	case SIOCDIFADDR:
481 	case SIOCGIFALIAS:
482 		if (ifp == NULL)
483 			return (EADDRNOTAVAIL);
484 		if (ifra->ifra_addr.sin_family == AF_INET) {
485 			while (ia != NULL) {
486 				if (ia->ia_ifp == ifp  &&
487 				    ia->ia_addr.sin_addr.s_addr ==
488 				    ifra->ifra_addr.sin_addr.s_addr)
489 					break;
490 				ia = in_ianext(ia);
491 			}
492 			if ((ifp->if_flags & IFF_POINTOPOINT) &&
493 			    cmd == SIOCAIFADDR &&
494 			    ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
495 				return EDESTADDRREQ;
496 			}
497 		}
498 		if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL)
499 			return (EADDRNOTAVAIL);
500 		if (cmd == SIOCGIFALIAS)
501 			break;
502 		/* FALLTHROUGH */
503 	case SIOCSIFADDR:
504 	case SIOCSIFNETMASK:
505 	case SIOCSIFDSTADDR:
506 		if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
507 			return error;
508 
509 		if (ifp == NULL)
510 			return (EADDRNOTAVAIL);
511 
512 		if (cmd == SIOCSIFDSTADDR &&
513 		    (ifp->if_flags & IFF_POINTOPOINT) == 0)
514 			return (EINVAL);
515 
516 		if (ia == NULL) {
517 			struct ifaddr *ifa;
518 			int i;
519 
520 			ia = ifa_create(sizeof(*ia));
521 			ifa = &ia->ia_ifa;
522 
523 			/*
524 			 * Setup per-CPU information
525 			 */
526 			for (i = 0; i < ncpus; ++i) {
527 				ifac = &ifa->ifa_containers[i];
528 				iac = &ifac->ifa_proto_u.u_in_ifac;
529 				iac->ia = ia;
530 				iac->ia_ifac = ifac;
531 			}
532 
533 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
534 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
535 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
536 			ia->ia_sockmask.sin_len = 8;
537 			ia->ia_sockmask.sin_family = AF_INET;
538 			if (ifp->if_flags & IFF_BROADCAST) {
539 				ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
540 				ia->ia_broadaddr.sin_family = AF_INET;
541 			}
542 			ia->ia_ifp = ifp;
543 			iaIsNew = 1;
544 
545 			in_ialink(ia);
546 			ifa_iflink(ifa, ifp, 1);
547 		}
548 		break;
549 
550 	case SIOCSIFBRDADDR:
551 		if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
552 			return error;
553 		/* FALLTHROUGH */
554 
555 	case SIOCGIFADDR:
556 	case SIOCGIFNETMASK:
557 	case SIOCGIFDSTADDR:
558 	case SIOCGIFBRDADDR:
559 		if (ia == NULL)
560 			return (EADDRNOTAVAIL);
561 		break;
562 	}
563 
564 	switch (cmd) {
565 	case SIOCGIFADDR:
566 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
567 		return (0);
568 
569 	case SIOCGIFBRDADDR:
570 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
571 			return (EINVAL);
572 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
573 		return (0);
574 
575 	case SIOCGIFDSTADDR:
576 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
577 			return (EINVAL);
578 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
579 		return (0);
580 
581 	case SIOCGIFNETMASK:
582 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
583 		return (0);
584 
585 	case SIOCSIFDSTADDR:
586 		KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
587 
588 		oldaddr = ia->ia_dstaddr;
589 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
590 		if (ifp->if_ioctl != NULL) {
591 			ifnet_serialize_all(ifp);
592 			error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
593 					      td->td_proc->p_ucred);
594 			ifnet_deserialize_all(ifp);
595 			if (error) {
596 				ia->ia_dstaddr = oldaddr;
597 				return (error);
598 			}
599 		}
600 		if (ia->ia_flags & IFA_ROUTE) {
601 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
602 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
603 			ia->ia_ifa.ifa_dstaddr =
604 					(struct sockaddr *)&ia->ia_dstaddr;
605 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
606 		}
607 		return (0);
608 
609 	case SIOCSIFBRDADDR:
610 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
611 			return (EINVAL);
612 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
613 		return (0);
614 
615 	case SIOCSIFADDR:
616 		error = in_ifinit(ifp, ia,
617 		    (const struct sockaddr_in *)&ifr->ifr_addr, 1);
618 		if (error != 0 && iaIsNew)
619 			break;
620 		if (error == 0) {
621 			EVENTHANDLER_INVOKE(ifaddr_event, ifp,
622 			iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
623 			&ia->ia_ifa);
624 		}
625 		if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
626 			/*
627 			 * Interface is brought up by in_ifinit()
628 			 * (via ifp->if_ioctl).  We act as if the
629 			 * interface got IFF_UP flag turned on.
630 			 */
631 			if_up(ifp);
632 		}
633 		return (0);
634 
635 	case SIOCSIFNETMASK:
636 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
637 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
638 		return (0);
639 
640 	case SIOCAIFADDR:
641 		maskIsNew = 0;
642 		hostIsNew = 1;
643 		error = 0;
644 		if (ia->ia_addr.sin_family == AF_INET) {
645 			if (ifra->ifra_addr.sin_len == 0) {
646 				ifra->ifra_addr = ia->ia_addr;
647 				hostIsNew = 0;
648 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
649 				   ia->ia_addr.sin_addr.s_addr) {
650 				hostIsNew = 0;
651 			}
652 		}
653 		if (ifra->ifra_mask.sin_len) {
654 			in_ifscrub(ifp, ia);
655 			ia->ia_sockmask = ifra->ifra_mask;
656 			ia->ia_sockmask.sin_family = AF_INET;
657 			ia->ia_subnetmask =
658 			    ntohl(ia->ia_sockmask.sin_addr.s_addr);
659 			maskIsNew = 1;
660 		}
661 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
662 		    ifra->ifra_dstaddr.sin_family == AF_INET) {
663 			in_ifscrub(ifp, ia);
664 			ia->ia_dstaddr = ifra->ifra_dstaddr;
665 			maskIsNew  = 1; /* We lie; but the effect's the same */
666 		}
667 		if (ifra->ifra_addr.sin_family == AF_INET &&
668 		    (hostIsNew || maskIsNew))
669 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
670 
671 		if (error != 0 && iaIsNew)
672 			break;
673 
674 		if ((ifp->if_flags & IFF_BROADCAST) &&
675 		    ifra->ifra_broadaddr.sin_family == AF_INET)
676 			ia->ia_broadaddr = ifra->ifra_broadaddr;
677 		if (error == 0) {
678 			EVENTHANDLER_INVOKE(ifaddr_event, ifp,
679 			iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
680 			&ia->ia_ifa);
681 		}
682 		if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
683 			/* See the comment in SIOCSIFADDR */
684 			if_up(ifp);
685 		}
686 		return (error);
687 
688 	case SIOCGIFALIAS:
689 		ifra->ifra_mask = ia->ia_sockmask;
690 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
691 		    (ia->ia_dstaddr.sin_family == AF_INET))
692 			ifra->ifra_dstaddr = ia->ia_dstaddr;
693 		else if ((ifp->if_flags & IFF_BROADCAST) &&
694 		    (ia->ia_broadaddr.sin_family == AF_INET))
695 			ifra->ifra_broadaddr = ia->ia_broadaddr;
696 		else
697 			memset(&ifra->ifra_broadaddr, 0,
698 			    sizeof(ifra->ifra_broadaddr));
699 		return (0);
700 
701 	case SIOCDIFADDR:
702 		/*
703 		 * in_ifscrub kills the interface route.
704 		 */
705 		in_ifscrub(ifp, ia);
706 		/*
707 		 * in_ifadown gets rid of all the rest of
708 		 * the routes.  This is not quite the right
709 		 * thing to do, but at least if we are running
710 		 * a routing process they will come back.
711 		 */
712 		in_ifadown(&ia->ia_ifa, 1);
713 		EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
714 				    &ia->ia_ifa);
715 		error = 0;
716 		break;
717 
718 	default:
719 		if (ifp == NULL || ifp->if_ioctl == NULL)
720 			return (EOPNOTSUPP);
721 		ifnet_serialize_all(ifp);
722 		error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
723 		ifnet_deserialize_all(ifp);
724 		return (error);
725 	}
726 
727 	KKASSERT(cmd == SIOCDIFADDR ||
728 		 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
729 
730 	ifa_ifunlink(&ia->ia_ifa, ifp);
731 	in_iaunlink(ia);
732 
733 	if (cmd == SIOCDIFADDR) {
734 		ifac = &ia->ia_ifa.ifa_containers[mycpuid];
735 		if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
736 			in_iahash_remove(ia);
737 	}
738 #ifdef INVARIANTS
739 	else {
740 		/*
741 		 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
742 		 * already taken care of the deletion from hash table
743 		 */
744 		ifac = &ia->ia_ifa.ifa_containers[mycpuid];
745 		KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
746 			("SIOC%cIFADDR failed on new ia, "
747 			 "but the new ia is still in hash table",
748 			 cmd == SIOCSIFADDR ? 'S' : 'A'));
749 	}
750 #endif
751 
752 	ifa_destroy(&ia->ia_ifa);
753 
754 	if ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) &&
755 	    !ifpWasUp && (ifp->if_flags & IFF_UP)) {
756 		/*
757 		 * Though the address assignment failed, the
758 		 * interface is brought up by in_ifinit()
759 		 * (via ifp->if_ioctl).  With the hope that
760 		 * the interface has some valid addresses, we
761 		 * act as if IFF_UP flag was just set on the
762 		 * interface.
763 		 *
764 		 * NOTE:
765 		 * This could only be done after the failed
766 		 * address is unlinked from the global address
767 		 * list.
768 		 */
769 		if_up(ifp);
770 	}
771 
772 	return (error);
773 }
774 
775 /*
776  * SIOC[GAD]LIFADDR.
777  *	SIOCGLIFADDR: get first address. (?!?)
778  *	SIOCGLIFADDR with IFLR_PREFIX:
779  *		get first address that matches the specified prefix.
780  *	SIOCALIFADDR: add the specified address.
781  *	SIOCALIFADDR with IFLR_PREFIX:
782  *		EINVAL since we can't deduce hostid part of the address.
783  *	SIOCDLIFADDR: delete the specified address.
784  *	SIOCDLIFADDR with IFLR_PREFIX:
785  *		delete the first address that matches the specified prefix.
786  * return values:
787  *	EINVAL on invalid parameters
788  *	EADDRNOTAVAIL on prefix match failed/specified address not found
789  *	other values may be returned from in_ioctl()
790  *
791  * NOTE! td might be NULL.
792  */
793 static int
794 in_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
795 {
796 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
797 
798 	/* sanity checks */
799 	if (!data || !ifp) {
800 		panic("invalid argument to in_lifaddr_ioctl");
801 		/*NOTRECHED*/
802 	}
803 
804 	switch (cmd) {
805 	case SIOCGLIFADDR:
806 		/* address must be specified on GET with IFLR_PREFIX */
807 		if ((iflr->flags & IFLR_PREFIX) == 0)
808 			break;
809 		/*FALLTHROUGH*/
810 	case SIOCALIFADDR:
811 	case SIOCDLIFADDR:
812 		/* address must be specified on ADD and DELETE */
813 		if (iflr->addr.ss_family != AF_INET)
814 			return EINVAL;
815 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
816 			return EINVAL;
817 		/* XXX need improvement */
818 		if (iflr->dstaddr.ss_family
819 		 && iflr->dstaddr.ss_family != AF_INET)
820 			return EINVAL;
821 		if (iflr->dstaddr.ss_family
822 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
823 			return EINVAL;
824 		break;
825 	default: /*shouldn't happen*/
826 		return EOPNOTSUPP;
827 	}
828 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
829 		return EINVAL;
830 
831 	switch (cmd) {
832 	case SIOCALIFADDR:
833 	    {
834 		struct in_aliasreq ifra;
835 
836 		if (iflr->flags & IFLR_PREFIX)
837 			return EINVAL;
838 
839 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
840 		bzero(&ifra, sizeof ifra);
841 		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
842 
843 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
844 
845 		if (iflr->dstaddr.ss_family) {	/*XXX*/
846 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
847 				iflr->dstaddr.ss_len);
848 		}
849 
850 		ifra.ifra_mask.sin_family = AF_INET;
851 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
852 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
853 
854 		return in_control_internal(SIOCAIFADDR, (caddr_t)&ifra, ifp,
855 		    td);
856 	    }
857 	case SIOCGLIFADDR:
858 	case SIOCDLIFADDR:
859 	    {
860 		struct ifaddr_container *ifac;
861 		struct in_ifaddr *ia;
862 		struct in_addr mask, candidate, match;
863 		struct sockaddr_in *sin;
864 		int cmp;
865 
866 		bzero(&mask, sizeof mask);
867 		if (iflr->flags & IFLR_PREFIX) {
868 			/* lookup a prefix rather than address. */
869 			in_len2mask(&mask, iflr->prefixlen);
870 
871 			sin = (struct sockaddr_in *)&iflr->addr;
872 			match.s_addr = sin->sin_addr.s_addr;
873 			match.s_addr &= mask.s_addr;
874 
875 			/* if you set extra bits, that's wrong */
876 			if (match.s_addr != sin->sin_addr.s_addr)
877 				return EINVAL;
878 
879 			cmp = 1;
880 		} else {
881 			if (cmd == SIOCGLIFADDR) {
882 				/* on getting an address, take the 1st match */
883 				match.s_addr = 0; /* gcc4 warning */
884 				cmp = 0;	/*XXX*/
885 			} else {
886 				/* on deleting an address, do exact match */
887 				in_len2mask(&mask, 32);
888 				sin = (struct sockaddr_in *)&iflr->addr;
889 				match.s_addr = sin->sin_addr.s_addr;
890 
891 				cmp = 1;
892 			}
893 		}
894 
895 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
896 			struct ifaddr *ifa = ifac->ifa;
897 
898 			if (ifa->ifa_addr->sa_family != AF_INET6)
899 				continue;
900 			if (!cmp)
901 				break;
902 			candidate.s_addr =
903 			((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
904 			candidate.s_addr &= mask.s_addr;
905 			if (candidate.s_addr == match.s_addr)
906 				break;
907 		}
908 		if (ifac == NULL)
909 			return EADDRNOTAVAIL;
910 		ia = (struct in_ifaddr *)(ifac->ifa);
911 
912 		if (cmd == SIOCGLIFADDR) {
913 			/* fill in the if_laddrreq structure */
914 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
915 
916 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
917 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
918 					ia->ia_dstaddr.sin_len);
919 			} else
920 				bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
921 
922 			iflr->prefixlen =
923 				in_mask2len(&ia->ia_sockmask.sin_addr);
924 
925 			iflr->flags = 0;	/*XXX*/
926 
927 			return 0;
928 		} else {
929 			struct in_aliasreq ifra;
930 
931 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
932 			bzero(&ifra, sizeof ifra);
933 			bcopy(iflr->iflr_name, ifra.ifra_name,
934 				sizeof ifra.ifra_name);
935 
936 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
937 				ia->ia_addr.sin_len);
938 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
939 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
940 					ia->ia_dstaddr.sin_len);
941 			}
942 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
943 				ia->ia_sockmask.sin_len);
944 
945 			return in_control_internal(SIOCDIFADDR, (caddr_t)&ifra,
946 			    ifp, td);
947 		}
948 	    }
949 	}
950 
951 	return EOPNOTSUPP;	/*just for safety*/
952 }
953 
954 /*
955  * Delete any existing route for an interface.
956  */
957 void
958 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
959 {
960 	in_scrubprefix(ia);
961 }
962 
963 /*
964  * Initialize an interface's internet address
965  * and routing table entry.
966  */
967 static int
968 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
969 	  const struct sockaddr_in *sin, int scrub)
970 {
971 	u_long i = ntohl(sin->sin_addr.s_addr);
972 	struct sockaddr_in oldaddr;
973 	struct ifaddr_container *ifac;
974 	int flags = RTF_UP, error = 0;
975 	int was_hash = 0;
976 
977 	ifac = &ia->ia_ifa.ifa_containers[mycpuid];
978 	oldaddr = ia->ia_addr;
979 
980 	if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
981 		was_hash = 1;
982 		in_iahash_remove(ia);
983 	}
984 
985 	ia->ia_addr = *sin;
986 	if (ia->ia_addr.sin_family == AF_INET)
987 		in_iahash_insert(ia);
988 
989 	/*
990 	 * Give the interface a chance to initialize
991 	 * if this is its first address,
992 	 * and to validate the address if necessary.
993 	 */
994 	if (ifp->if_ioctl != NULL) {
995 		ifnet_serialize_all(ifp);
996 		error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
997 		ifnet_deserialize_all(ifp);
998 		if (error)
999 			goto fail;
1000 	}
1001 
1002 	/*
1003 	 * Delete old route, if requested.
1004 	 */
1005 	if (scrub) {
1006 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1007 		in_ifscrub(ifp, ia);
1008 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1009 	}
1010 
1011 	/*
1012 	 * Calculate netmask/subnetmask.
1013 	 */
1014 	if (IN_CLASSA(i))
1015 		ia->ia_netmask = IN_CLASSA_NET;
1016 	else if (IN_CLASSB(i))
1017 		ia->ia_netmask = IN_CLASSB_NET;
1018 	else
1019 		ia->ia_netmask = IN_CLASSC_NET;
1020 	/*
1021 	 * The subnet mask usually includes at least the standard network part,
1022 	 * but may may be smaller in the case of supernetting.
1023 	 * If it is set, we believe it.
1024 	 */
1025 	if (ia->ia_subnetmask == 0) {
1026 		ia->ia_subnetmask = ia->ia_netmask;
1027 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1028 	} else {
1029 		ia->ia_netmask &= ia->ia_subnetmask;
1030 	}
1031 	ia->ia_net = i & ia->ia_netmask;
1032 	ia->ia_subnet = i & ia->ia_subnetmask;
1033 	in_socktrim(&ia->ia_sockmask);
1034 
1035 	/*
1036 	 * Add route for the network.
1037 	 */
1038 	ia->ia_ifa.ifa_metric = ifp->if_metric;
1039 	if (ifp->if_flags & IFF_BROADCAST) {
1040 		if (ia->ia_subnetmask == IN_RFC3021_MASK) {
1041 			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
1042 			ia->ia_netbroadcast.s_addr = INADDR_BROADCAST;
1043 		} else {
1044 			ia->ia_broadaddr.sin_addr.s_addr =
1045 				htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1046 			ia->ia_netbroadcast.s_addr =
1047 				htonl(ia->ia_net | ~ ia->ia_netmask);
1048 		}
1049 	} else if (ifp->if_flags & IFF_LOOPBACK) {
1050 		ia->ia_dstaddr = ia->ia_addr;
1051 		flags |= RTF_HOST;
1052 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
1053 		if (ia->ia_dstaddr.sin_family != AF_INET)
1054 			return (0);
1055 		flags |= RTF_HOST;
1056 	}
1057 
1058 	/*-
1059 	 * Don't add host routes for interface addresses of
1060 	 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
1061 	 * possible to assign several such address pairs with consistent
1062 	 * results (no host route) and is required by BOOTP.
1063 	 *
1064 	 * XXX: This is ugly !  There should be a way for the caller to
1065 	 *      say that they don't want a host route.
1066 	 */
1067 	if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1068 	    ia->ia_netmask != IN_CLASSA_NET ||
1069 	    ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1070 		error = in_addprefix(ia, flags);
1071 		if (error)
1072 			goto fail;
1073 	}
1074 
1075 	/*
1076 	 * If the interface supports multicast, join the "all hosts"
1077 	 * multicast group on that interface.
1078 	 */
1079 	if (ifp->if_flags & IFF_MULTICAST) {
1080 		struct in_addr addr;
1081 
1082 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1083 		in_addmulti(&addr, ifp);
1084 	}
1085 	return (0);
1086 fail:
1087 	if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1088 		in_iahash_remove(ia);
1089 
1090 	ia->ia_addr = oldaddr;
1091 	if (was_hash)
1092 		in_iahash_insert(ia);
1093 	return (error);
1094 }
1095 
1096 #define rtinitflags(x) \
1097 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
1098 	    ? RTF_HOST : 0)
1099 
1100 /*
1101  * Add a route to prefix ("connected route" in cisco terminology).
1102  * Do nothing, if there are some interface addresses with the same
1103  * prefix already.  This function assumes that the 'target' parent
1104  * interface is UP.
1105  */
1106 static int
1107 in_addprefix(struct in_ifaddr *target, int flags)
1108 {
1109 	struct in_ifaddr_container *iac;
1110 	struct in_addr prefix, mask;
1111 	int error;
1112 
1113 #ifdef CARP
1114 	/*
1115 	 * Don't add prefix routes for CARP interfaces.
1116 	 * Prefix routes creation is handled by CARP
1117 	 * interfaces themselves.
1118 	 */
1119 	if (target->ia_ifp->if_type == IFT_CARP)
1120 		return 0;
1121 #endif
1122 
1123 	mask = target->ia_sockmask.sin_addr;
1124 	if (flags & RTF_HOST) {
1125 		prefix = target->ia_dstaddr.sin_addr;
1126 	} else {
1127 		prefix = target->ia_addr.sin_addr;
1128 		prefix.s_addr &= mask.s_addr;
1129 	}
1130 
1131 	TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1132 		struct in_ifaddr *ia = iac->ia;
1133 		struct in_addr p;
1134 
1135 		/* Don't test against self */
1136 		if (ia == target)
1137 			continue;
1138 
1139 		/* The tested address does not own a route entry */
1140 		if ((ia->ia_flags & IFA_ROUTE) == 0)
1141 			continue;
1142 
1143 		/* Prefix test */
1144 		if (rtinitflags(ia) != 0) {
1145 			p = ia->ia_dstaddr.sin_addr;
1146 		} else {
1147 			p = ia->ia_addr.sin_addr;
1148 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1149 		}
1150 		if (prefix.s_addr != p.s_addr)
1151 			continue;
1152 
1153 		/*
1154 		 * If the to-be-added address and the currently being
1155 		 * tested address are not host addresses, we need to
1156 		 * take subnetmask into consideration.
1157 		 */
1158 		if (!(flags & RTF_HOST) && rtinitflags(ia) == 0 &&
1159 		    mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1160 			continue;
1161 
1162 		/*
1163 		 * If we got a matching prefix route inserted by other
1164 		 * interface address, we don't need to bother.
1165 		 */
1166 		return 0;
1167 	}
1168 
1169 	/*
1170 	 * No one seem to have prefix route; insert it.
1171 	 */
1172 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1173 	if (!error)
1174 		target->ia_flags |= IFA_ROUTE;
1175 	return error;
1176 }
1177 
1178 /*
1179  * Remove a route to prefix ("connected route" in cisco terminology).
1180  * Re-installs the route by using another interface address, if there's
1181  * one with the same prefix (otherwise we lose the route mistakenly).
1182  */
1183 static void
1184 in_scrubprefix(struct in_ifaddr *target)
1185 {
1186 	struct in_ifaddr_container *iac;
1187 	struct in_addr prefix, mask;
1188 	int error;
1189 
1190 #ifdef CARP
1191 	/*
1192 	 * Don't scrub prefix routes for CARP interfaces.
1193 	 * Prefix routes deletion is handled by CARP
1194 	 * interfaces themselves.
1195 	 */
1196 	if (target->ia_ifp->if_type == IFT_CARP)
1197 		return;
1198 #endif
1199 
1200 	if ((target->ia_flags & IFA_ROUTE) == 0)
1201 		return;
1202 
1203 	mask = target->ia_sockmask.sin_addr;
1204 	if (rtinitflags(target) != 0) {
1205 		prefix = target->ia_dstaddr.sin_addr;
1206 	} else {
1207 		prefix = target->ia_addr.sin_addr;
1208 		prefix.s_addr &= mask.s_addr;
1209 	}
1210 
1211 	TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1212 		struct in_ifaddr *ia = iac->ia;
1213 		struct in_addr p;
1214 
1215 		/* Don't test against self */
1216 		if (ia == target)
1217 			continue;
1218 
1219 		/* The tested address already owns a route entry */
1220 		if (ia->ia_flags & IFA_ROUTE)
1221 			continue;
1222 
1223 		/*
1224 		 * The prefix route of the tested address should
1225 		 * never be installed if its parent interface is
1226 		 * not UP yet.
1227 		 */
1228 		if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1229 			continue;
1230 
1231 #ifdef CARP
1232 		/*
1233 		 * Don't add prefix routes for CARP interfaces.
1234 		 * Prefix routes creation is handled by CARP
1235 		 * interfaces themselves.
1236 		 */
1237 		if (ia->ia_ifp->if_type == IFT_CARP)
1238 			continue;
1239 #endif
1240 
1241 		/* Prefix test */
1242 		if (rtinitflags(ia) != 0) {
1243 			p = ia->ia_dstaddr.sin_addr;
1244 		} else {
1245 			p = ia->ia_addr.sin_addr;
1246 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1247 		}
1248 		if (prefix.s_addr != p.s_addr)
1249 			continue;
1250 
1251 		/*
1252 		 * We don't need to test subnetmask here, as what we do
1253 		 * in in_addprefix(), since if the the tested address's
1254 		 * parent interface is UP, the tested address should own
1255 		 * a prefix route entry and we would never reach here.
1256 		 */
1257 
1258 		/*
1259 		 * If we got a matching prefix route, move IFA_ROUTE to him
1260 		 */
1261 		rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1262 		target->ia_flags &= ~IFA_ROUTE;
1263 
1264 		error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
1265 		if (!error)
1266 			ia->ia_flags |= IFA_ROUTE;
1267 		return;
1268 	}
1269 
1270 	/*
1271 	 * No candidates for this prefix route; just remove it.
1272 	 */
1273 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1274 	target->ia_flags &= ~IFA_ROUTE;
1275 }
1276 
1277 #undef rtinitflags
1278 
1279 /*
1280  * Return 1 if the address might be a local broadcast address.
1281  */
1282 int
1283 in_broadcast(struct in_addr in, struct ifnet *ifp)
1284 {
1285 	struct ifaddr_container *ifac;
1286 	u_long t;
1287 
1288 	if (in.s_addr == INADDR_BROADCAST ||
1289 	    in.s_addr == INADDR_ANY)
1290 		return 1;
1291 	if (ifp == NULL || (ifp->if_flags & IFF_BROADCAST) == 0)
1292 		return 0;
1293 	t = ntohl(in.s_addr);
1294 	/*
1295 	 * Look through the list of addresses for a match
1296 	 * with a broadcast address.
1297 	 */
1298 #define ia ((struct in_ifaddr *)ifa)
1299 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1300 		struct ifaddr *ifa = ifac->ifa;
1301 
1302 		if (ifa->ifa_addr->sa_family == AF_INET &&
1303 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1304 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
1305 		     /*
1306 		      * Check for old-style (host 0) broadcast, but
1307 		      * taking into account that RFC 3021 obsoletes it.
1308 		      */
1309 		     (ia->ia_subnetmask != IN_RFC3021_MASK &&
1310 		     (t == ia->ia_subnet || t == ia->ia_net))) &&
1311 		     /*
1312 		      * Check for an all one subnetmask. These
1313 		      * only exist when an interface gets a secondary
1314 		      * address.
1315 		      */
1316 		     ia->ia_subnetmask != (u_long)0xffffffff)
1317 			    return 1;
1318 	}
1319 	return (0);
1320 #undef ia
1321 }
1322 
1323 /*
1324  * Add an address to the list of IP multicast addresses for a given interface.
1325  */
1326 struct in_multi *
1327 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1328 {
1329 	struct in_multi *inm;
1330 	int error;
1331 	struct sockaddr_in sin;
1332 	struct ifmultiaddr *ifma;
1333 
1334 	ASSERT_NETISR0;
1335 
1336 	/*
1337 	 * Call generic routine to add membership or increment
1338 	 * refcount.  It wants addresses in the form of a sockaddr,
1339 	 * so we build one here (being careful to zero the unused bytes).
1340 	 */
1341 	bzero(&sin, sizeof sin);
1342 	sin.sin_family = AF_INET;
1343 	sin.sin_len = sizeof sin;
1344 	sin.sin_addr = *ap;
1345 	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1346 	if (error)
1347 		return NULL;
1348 
1349 	/*
1350 	 * If ifma->ifma_protospec is null, then if_addmulti() created
1351 	 * a new record.  Otherwise, we are done.
1352 	 */
1353 	if (ifma->ifma_protospec != NULL)
1354 		return ifma->ifma_protospec;
1355 
1356 	inm = kmalloc(sizeof *inm, M_IPMADDR, M_INTWAIT | M_ZERO);
1357 	inm->inm_addr = *ap;
1358 	inm->inm_ifp = ifp;
1359 	inm->inm_ifma = ifma;
1360 	ifma->ifma_protospec = inm;
1361 	LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1362 
1363 	/*
1364 	 * Let IGMP know that we have joined a new IP multicast group.
1365 	 */
1366 	igmp_joingroup(inm);
1367 	return inm;
1368 }
1369 
1370 /*
1371  * Delete a multicast address record.
1372  */
1373 void
1374 in_delmulti(struct in_multi *inm)
1375 {
1376 	struct ifmultiaddr *ifma;
1377 	struct in_multi my_inm;
1378 
1379 	ASSERT_NETISR0;
1380 
1381 	ifma = inm->inm_ifma;
1382 	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1383 	if (ifma->ifma_refcount == 1) {
1384 		/*
1385 		 * No remaining claims to this record; let IGMP know that
1386 		 * we are leaving the multicast group.
1387 		 * But do it after the if_delmulti() which might reset
1388 		 * the interface and nuke the packet.
1389 		 */
1390 		my_inm = *inm ;
1391 		ifma->ifma_protospec = NULL;
1392 		LIST_REMOVE(inm, inm_link);
1393 		kfree(inm, M_IPMADDR);
1394 	}
1395 	/* XXX - should be separate API for when we have an ifma? */
1396 	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1397 	if (my_inm.inm_ifp != NULL)
1398 		igmp_leavegroup(&my_inm);
1399 }
1400 
1401 void
1402 in_if_down(struct ifnet *ifp)
1403 {
1404 	rt_purgecloned(ifp, AF_INET);
1405 }
1406 
1407 static void
1408 in_ifdetach_dispatch(netmsg_t nmsg)
1409 {
1410 	struct lwkt_msg *lmsg = &nmsg->lmsg;
1411 	struct ifnet *ifp = lmsg->u.ms_resultp;
1412 	int cpu;
1413 
1414 	in_pcbpurgeif0(&ripcbinfo, ifp);
1415 	for (cpu = 0; cpu < netisr_ncpus; ++cpu)
1416 		in_pcbpurgeif0(&udbinfo[cpu], ifp);
1417 
1418 	lwkt_replymsg(lmsg, 0);
1419 }
1420 
1421 void
1422 in_ifdetach(struct ifnet *ifp)
1423 {
1424 	struct netmsg_base nmsg;
1425 	struct lwkt_msg *lmsg = &nmsg.lmsg;
1426 
1427 	netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
1428 	    in_ifdetach_dispatch);
1429 	lmsg->u.ms_resultp = ifp;
1430 
1431 	lwkt_domsg(netisr_cpuport(0), lmsg, 0);
1432 }
1433