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