libiconv: Uncomment iconv_vfs_refcount.
[dragonfly.git] / sys / netinet / in.c
blob43d265ad328da3f75891b2efb8f00b12cc70254b
1 /*
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
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 * $DragonFly: src/sys/netinet/in.c,v 1.41 2008/08/17 05:20:10 sephe Exp $
38 #include "opt_bootp.h"
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>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51 #include <sys/thread2.h>
53 #include <net/if.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
56 #include <net/netmsg2.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/in_pcb.h>
62 #include <netinet/igmp_var.h>
64 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
66 static int in_mask2len (struct in_addr *);
67 static void in_len2mask (struct in_addr *, int);
68 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
69 struct ifnet *, struct thread *);
71 static void in_socktrim (struct sockaddr_in *);
72 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
73 const struct sockaddr_in *, int);
75 static void in_control_dispatch(struct netmsg *);
76 static int in_control_internal(u_long, caddr_t, struct ifnet *,
77 struct thread *);
79 static int in_addprefix(struct in_ifaddr *, int);
80 static void in_scrubprefix(struct in_ifaddr *);
82 static int subnetsarelocal = 0;
83 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
84 &subnetsarelocal, 0, "");
86 struct in_multihead in_multihead; /* XXX BSS initialization */
88 extern struct inpcbinfo ripcbinfo;
89 extern struct inpcbinfo udbinfo;
92 * Return 1 if an internet address is for a ``local'' host
93 * (one to which we have a connection). If subnetsarelocal
94 * is true, this includes other subnets of the local net.
95 * Otherwise, it includes only the directly-connected (sub)nets.
97 int
98 in_localaddr(struct in_addr in)
100 u_long i = ntohl(in.s_addr);
101 struct in_ifaddr_container *iac;
102 struct in_ifaddr *ia;
104 if (subnetsarelocal) {
105 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
106 ia = iac->ia;
108 if ((i & ia->ia_netmask) == ia->ia_net)
109 return (1);
111 } else {
112 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
113 ia = iac->ia;
115 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
116 return (1);
119 return (0);
123 * Determine whether an IP address is in a reserved set of addresses
124 * that may not be forwarded, or whether datagrams to that destination
125 * may be forwarded.
128 in_canforward(struct in_addr in)
130 u_long i = ntohl(in.s_addr);
131 u_long net;
133 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
134 return (0);
135 if (IN_CLASSA(i)) {
136 net = i & IN_CLASSA_NET;
137 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
138 return (0);
140 return (1);
144 * Trim a mask in a sockaddr
146 static void
147 in_socktrim(struct sockaddr_in *ap)
149 char *cplim = (char *) &ap->sin_addr;
150 char *cp = (char *) (&ap->sin_addr + 1);
152 ap->sin_len = 0;
153 while (--cp >= cplim)
154 if (*cp) {
155 (ap)->sin_len = cp - (char *) (ap) + 1;
156 break;
160 static int
161 in_mask2len(struct in_addr *mask)
163 int x, y;
164 u_char *p;
166 p = (u_char *)mask;
167 for (x = 0; x < sizeof *mask; x++) {
168 if (p[x] != 0xff)
169 break;
171 y = 0;
172 if (x < sizeof *mask) {
173 for (y = 0; y < 8; y++) {
174 if ((p[x] & (0x80 >> y)) == 0)
175 break;
178 return x * 8 + y;
181 static void
182 in_len2mask(struct in_addr *mask, int len)
184 int i;
185 u_char *p;
187 p = (u_char *)mask;
188 bzero(mask, sizeof *mask);
189 for (i = 0; i < len / 8; i++)
190 p[i] = 0xff;
191 if (len % 8)
192 p[i] = (0xff00 >> (len % 8)) & 0xff;
195 static int in_interfaces; /* number of external internet interfaces */
197 struct in_control_arg {
198 u_long cmd;
199 caddr_t data;
200 struct ifnet *ifp;
201 struct thread *td;
204 static void
205 in_control_dispatch(struct netmsg *nmsg)
207 struct lwkt_msg *msg = &nmsg->nm_lmsg;
208 const struct in_control_arg *arg = msg->u.ms_resultp;
209 int error;
211 error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td);
212 lwkt_replymsg(msg, error);
216 * Generic internet control operations (ioctl's).
217 * Ifp is 0 if not an interface-specific ioctl.
219 * NOTE! td might be NULL.
221 /* ARGSUSED */
223 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
224 struct thread *td)
226 struct netmsg nmsg;
227 struct in_control_arg arg;
228 struct lwkt_msg *msg;
229 int error;
231 switch (cmd) {
232 case SIOCALIFADDR:
233 case SIOCDLIFADDR:
234 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
235 return error;
236 /* FALLTHROUGH */
237 case SIOCGLIFADDR:
238 if (!ifp)
239 return EINVAL;
240 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
243 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
244 ("recursive SIOC%cLIFADDR!\n",
245 cmd == SIOCDLIFADDR ? 'D' : 'A'));
248 * IFADDR alterations are serialized by netisr0
250 switch (cmd) {
251 case SIOCSIFDSTADDR:
252 case SIOCSIFBRDADDR:
253 case SIOCSIFADDR:
254 case SIOCSIFNETMASK:
255 case SIOCAIFADDR:
256 case SIOCDIFADDR:
257 bzero(&arg, sizeof(arg));
258 arg.cmd = cmd;
259 arg.data = data;
260 arg.ifp = ifp;
261 arg.td = td;
263 netmsg_init(&nmsg, &curthread->td_msgport, 0,
264 in_control_dispatch);
265 msg = &nmsg.nm_lmsg;
266 msg->u.ms_resultp = &arg;
268 lwkt_domsg(cpu_portfn(0), msg, 0);
269 return msg->ms_error;
270 default:
271 return in_control_internal(cmd, data, ifp, td);
275 static void
276 in_ialink_dispatch(struct netmsg *nmsg)
278 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
279 struct in_ifaddr *ia = lmsg->u.ms_resultp;
280 struct ifaddr_container *ifac;
281 struct in_ifaddr_container *iac;
282 int cpu = mycpuid;
284 crit_enter();
286 ifac = &ia->ia_ifa.ifa_containers[cpu];
287 ASSERT_IFAC_VALID(ifac);
288 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
289 ("ia is on in_ifaddrheads\n"));
291 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
292 iac = &ifac->ifa_proto_u.u_in_ifac;
293 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
295 crit_exit();
297 ifa_forwardmsg(lmsg, cpu + 1);
300 static void
301 in_iaunlink_dispatch(struct netmsg *nmsg)
303 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
304 struct in_ifaddr *ia = lmsg->u.ms_resultp;
305 struct ifaddr_container *ifac;
306 struct in_ifaddr_container *iac;
307 int cpu = mycpuid;
309 crit_enter();
311 ifac = &ia->ia_ifa.ifa_containers[cpu];
312 ASSERT_IFAC_VALID(ifac);
313 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
314 ("ia is not on in_ifaddrheads\n"));
316 iac = &ifac->ifa_proto_u.u_in_ifac;
317 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
318 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
320 crit_exit();
322 ifa_forwardmsg(lmsg, cpu + 1);
325 static void
326 in_iahashins_dispatch(struct netmsg *nmsg)
328 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
329 struct in_ifaddr *ia = lmsg->u.ms_resultp;
330 struct ifaddr_container *ifac;
331 struct in_ifaddr_container *iac;
332 int cpu = mycpuid;
334 crit_enter();
336 ifac = &ia->ia_ifa.ifa_containers[cpu];
337 ASSERT_IFAC_VALID(ifac);
338 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
339 ("ia is on in_ifaddrhashtbls\n"));
341 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
342 iac = &ifac->ifa_proto_u.u_in_ifac;
343 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
344 iac, ia_hash);
346 crit_exit();
348 ifa_forwardmsg(lmsg, cpu + 1);
351 static void
352 in_iahashrem_dispatch(struct netmsg *nmsg)
354 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
355 struct in_ifaddr *ia = lmsg->u.ms_resultp;
356 struct ifaddr_container *ifac;
357 struct in_ifaddr_container *iac;
358 int cpu = mycpuid;
360 crit_enter();
362 ifac = &ia->ia_ifa.ifa_containers[cpu];
363 ASSERT_IFAC_VALID(ifac);
364 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
365 ("ia is not on in_ifaddrhashtbls\n"));
367 iac = &ifac->ifa_proto_u.u_in_ifac;
368 LIST_REMOVE(iac, ia_hash);
369 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
371 crit_exit();
373 ifa_forwardmsg(lmsg, cpu + 1);
376 static void
377 in_ialink(struct in_ifaddr *ia)
379 struct netmsg nmsg;
380 struct lwkt_msg *lmsg;
382 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_ialink_dispatch);
383 lmsg = &nmsg.nm_lmsg;
384 lmsg->u.ms_resultp = ia;
386 ifa_domsg(lmsg, 0);
389 void
390 in_iaunlink(struct in_ifaddr *ia)
392 struct netmsg nmsg;
393 struct lwkt_msg *lmsg;
395 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iaunlink_dispatch);
396 lmsg = &nmsg.nm_lmsg;
397 lmsg->u.ms_resultp = ia;
399 ifa_domsg(lmsg, 0);
402 void
403 in_iahash_insert(struct in_ifaddr *ia)
405 struct netmsg nmsg;
406 struct lwkt_msg *lmsg;
408 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashins_dispatch);
409 lmsg = &nmsg.nm_lmsg;
410 lmsg->u.ms_resultp = ia;
412 ifa_domsg(lmsg, 0);
415 void
416 in_iahash_remove(struct in_ifaddr *ia)
418 struct netmsg nmsg;
419 struct lwkt_msg *lmsg;
421 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashrem_dispatch);
422 lmsg = &nmsg.nm_lmsg;
423 lmsg->u.ms_resultp = ia;
425 ifa_domsg(lmsg, 0);
428 static __inline struct in_ifaddr *
429 in_ianext(struct in_ifaddr *oia)
431 struct ifaddr_container *ifac;
432 struct in_ifaddr_container *iac;
434 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
435 ASSERT_IFAC_VALID(ifac);
436 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
437 ("ia is not on in_ifaddrheads\n"));
439 iac = &ifac->ifa_proto_u.u_in_ifac;
440 iac = TAILQ_NEXT(iac, ia_link);
441 if (iac != NULL)
442 return iac->ia;
443 else
444 return NULL;
447 static int
448 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
449 struct thread *td)
451 struct ifreq *ifr = (struct ifreq *)data;
452 struct in_ifaddr *ia = NULL;
453 struct in_addr dst;
454 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
455 struct ifaddr_container *ifac;
456 struct in_ifaddr_container *iac;
457 struct sockaddr_in oldaddr;
458 int hostIsNew, iaIsNew, maskIsNew, ifpWasUp;
459 int error = 0;
461 iaIsNew = 0;
462 ifpWasUp = 0;
465 * Find address for this interface, if it exists.
467 * If an alias address was specified, find that one instead of
468 * the first one on the interface, if possible
470 if (ifp) {
471 struct in_ifaddr *iap;
473 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
474 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
475 iap = iac->ia;
476 if (iap->ia_ifp == ifp &&
477 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
478 ia = iap;
479 break;
482 if (ia == NULL) {
483 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
484 ifa_link) {
485 iap = ifatoia(ifac->ifa);
486 if (iap->ia_addr.sin_family == AF_INET) {
487 ia = iap;
488 break;
493 if (ifp->if_flags & IFF_UP)
494 ifpWasUp = 1;
497 switch (cmd) {
498 case SIOCAIFADDR:
499 case SIOCDIFADDR:
500 if (ifp == NULL)
501 return (EADDRNOTAVAIL);
502 if (ifra->ifra_addr.sin_family == AF_INET) {
503 while (ia != NULL) {
504 if (ia->ia_ifp == ifp &&
505 ia->ia_addr.sin_addr.s_addr ==
506 ifra->ifra_addr.sin_addr.s_addr)
507 break;
508 ia = in_ianext(ia);
510 if ((ifp->if_flags & IFF_POINTOPOINT) &&
511 cmd == SIOCAIFADDR &&
512 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
513 return EDESTADDRREQ;
516 if (cmd == SIOCDIFADDR && ia == NULL)
517 return (EADDRNOTAVAIL);
518 /* FALLTHROUGH */
519 case SIOCSIFADDR:
520 case SIOCSIFNETMASK:
521 case SIOCSIFDSTADDR:
522 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
523 return error;
525 if (ifp == NULL)
526 return (EADDRNOTAVAIL);
528 if (cmd == SIOCSIFDSTADDR &&
529 (ifp->if_flags & IFF_POINTOPOINT) == 0)
530 return (EINVAL);
532 if (ia == NULL) {
533 struct ifaddr *ifa;
534 int i;
536 ia = ifa_create(sizeof(*ia), M_WAITOK);
537 ifa = &ia->ia_ifa;
540 * Setup per-CPU information
542 for (i = 0; i < ncpus; ++i) {
543 ifac = &ifa->ifa_containers[i];
544 iac = &ifac->ifa_proto_u.u_in_ifac;
545 iac->ia = ia;
546 iac->ia_ifac = ifac;
550 * Protect from NETISR_IP traversing address list
551 * while we're modifying it.
553 crit_enter();
555 in_ialink(ia);
556 ifa_iflink(ifa, ifp, 1);
558 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
559 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
560 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
561 ia->ia_sockmask.sin_len = 8;
562 ia->ia_sockmask.sin_family = AF_INET;
563 if (ifp->if_flags & IFF_BROADCAST) {
564 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
565 ia->ia_broadaddr.sin_family = AF_INET;
567 ia->ia_ifp = ifp;
568 if (!(ifp->if_flags & IFF_LOOPBACK))
569 in_interfaces++;
570 iaIsNew = 1;
572 crit_exit();
574 break;
576 case SIOCSIFBRDADDR:
577 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
578 return error;
579 /* FALLTHROUGH */
581 case SIOCGIFADDR:
582 case SIOCGIFNETMASK:
583 case SIOCGIFDSTADDR:
584 case SIOCGIFBRDADDR:
585 if (ia == NULL)
586 return (EADDRNOTAVAIL);
587 break;
590 switch (cmd) {
591 case SIOCGIFADDR:
592 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
593 return (0);
595 case SIOCGIFBRDADDR:
596 if ((ifp->if_flags & IFF_BROADCAST) == 0)
597 return (EINVAL);
598 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
599 return (0);
601 case SIOCGIFDSTADDR:
602 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
603 return (EINVAL);
604 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
605 return (0);
607 case SIOCGIFNETMASK:
608 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
609 return (0);
611 case SIOCSIFDSTADDR:
612 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
614 oldaddr = ia->ia_dstaddr;
615 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
616 if (ifp->if_ioctl != NULL) {
617 ifnet_serialize_all(ifp);
618 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
619 td->td_proc->p_ucred);
620 ifnet_deserialize_all(ifp);
621 if (error) {
622 ia->ia_dstaddr = oldaddr;
623 return (error);
626 if (ia->ia_flags & IFA_ROUTE) {
627 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
628 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
629 ia->ia_ifa.ifa_dstaddr =
630 (struct sockaddr *)&ia->ia_dstaddr;
631 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
633 return (0);
635 case SIOCSIFBRDADDR:
636 if ((ifp->if_flags & IFF_BROADCAST) == 0)
637 return (EINVAL);
638 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
639 return (0);
641 case SIOCSIFADDR:
642 error = in_ifinit(ifp, ia,
643 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
644 if (error != 0 && iaIsNew)
645 break;
646 if (error == 0) {
647 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
648 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
649 &ia->ia_ifa);
651 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
653 * Interface is brought up by in_ifinit()
654 * (via ifp->if_ioctl). We act as if the
655 * interface got IFF_UP flag turned on.
657 if_up(ifp);
659 return (0);
661 case SIOCSIFNETMASK:
662 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
663 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
664 return (0);
666 case SIOCAIFADDR:
667 maskIsNew = 0;
668 hostIsNew = 1;
669 error = 0;
670 if (ia->ia_addr.sin_family == AF_INET) {
671 if (ifra->ifra_addr.sin_len == 0) {
672 ifra->ifra_addr = ia->ia_addr;
673 hostIsNew = 0;
674 } else if (ifra->ifra_addr.sin_addr.s_addr ==
675 ia->ia_addr.sin_addr.s_addr) {
676 hostIsNew = 0;
679 if (ifra->ifra_mask.sin_len) {
680 in_ifscrub(ifp, ia);
681 ia->ia_sockmask = ifra->ifra_mask;
682 ia->ia_sockmask.sin_family = AF_INET;
683 ia->ia_subnetmask =
684 ntohl(ia->ia_sockmask.sin_addr.s_addr);
685 maskIsNew = 1;
687 if ((ifp->if_flags & IFF_POINTOPOINT) &&
688 ifra->ifra_dstaddr.sin_family == AF_INET) {
689 in_ifscrub(ifp, ia);
690 ia->ia_dstaddr = ifra->ifra_dstaddr;
691 maskIsNew = 1; /* We lie; but the effect's the same */
693 if (ifra->ifra_addr.sin_family == AF_INET &&
694 (hostIsNew || maskIsNew))
695 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
697 if (error != 0 && iaIsNew)
698 break;
700 if ((ifp->if_flags & IFF_BROADCAST) &&
701 ifra->ifra_broadaddr.sin_family == AF_INET)
702 ia->ia_broadaddr = ifra->ifra_broadaddr;
703 if (error == 0) {
704 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
705 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
706 &ia->ia_ifa);
708 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
709 /* See the comment in SIOCSIFADDR */
710 if_up(ifp);
712 return (error);
714 case SIOCDIFADDR:
716 * in_ifscrub kills the interface route.
718 in_ifscrub(ifp, ia);
720 * in_ifadown gets rid of all the rest of
721 * the routes. This is not quite the right
722 * thing to do, but at least if we are running
723 * a routing process they will come back.
725 in_ifadown(&ia->ia_ifa, 1);
726 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
727 &ia->ia_ifa);
728 error = 0;
729 break;
731 default:
732 if (ifp == NULL || ifp->if_ioctl == NULL)
733 return (EOPNOTSUPP);
734 ifnet_serialize_all(ifp);
735 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
736 ifnet_deserialize_all(ifp);
737 return (error);
740 KKASSERT(cmd == SIOCDIFADDR ||
741 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
743 ifa_ifunlink(&ia->ia_ifa, ifp);
744 in_iaunlink(ia);
746 if (cmd == SIOCDIFADDR) {
747 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
748 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
749 in_iahash_remove(ia);
751 #ifdef INVARIANTS
752 else {
754 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
755 * already taken care of the deletion from hash table
757 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
758 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
759 ("SIOC%cIFADDR failed on new ia, "
760 "but the new ia is still in hash table\n",
761 cmd == SIOCSIFADDR ? 'S' : 'A'));
763 #endif
765 ifa_destroy(&ia->ia_ifa);
767 if ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) &&
768 !ifpWasUp && (ifp->if_flags & IFF_UP)) {
770 * Though the address assignment failed, the
771 * interface is brought up by in_ifinit()
772 * (via ifp->if_ioctl). With the hope that
773 * the interface has some valid addresses, we
774 * act as if IFF_UP flag was just set on the
775 * interface.
777 * NOTE:
778 * This could only be done after the failed
779 * address is unlinked from the global address
780 * list.
782 if_up(ifp);
785 return (error);
789 * SIOC[GAD]LIFADDR.
790 * SIOCGLIFADDR: get first address. (?!?)
791 * SIOCGLIFADDR with IFLR_PREFIX:
792 * get first address that matches the specified prefix.
793 * SIOCALIFADDR: add the specified address.
794 * SIOCALIFADDR with IFLR_PREFIX:
795 * EINVAL since we can't deduce hostid part of the address.
796 * SIOCDLIFADDR: delete the specified address.
797 * SIOCDLIFADDR with IFLR_PREFIX:
798 * delete the first address that matches the specified prefix.
799 * return values:
800 * EINVAL on invalid parameters
801 * EADDRNOTAVAIL on prefix match failed/specified address not found
802 * other values may be returned from in_ioctl()
804 * NOTE! td might be NULL.
806 static int
807 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
808 struct thread *td)
810 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
812 /* sanity checks */
813 if (!data || !ifp) {
814 panic("invalid argument to in_lifaddr_ioctl");
815 /*NOTRECHED*/
818 switch (cmd) {
819 case SIOCGLIFADDR:
820 /* address must be specified on GET with IFLR_PREFIX */
821 if ((iflr->flags & IFLR_PREFIX) == 0)
822 break;
823 /*FALLTHROUGH*/
824 case SIOCALIFADDR:
825 case SIOCDLIFADDR:
826 /* address must be specified on ADD and DELETE */
827 if (iflr->addr.ss_family != AF_INET)
828 return EINVAL;
829 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
830 return EINVAL;
831 /* XXX need improvement */
832 if (iflr->dstaddr.ss_family
833 && iflr->dstaddr.ss_family != AF_INET)
834 return EINVAL;
835 if (iflr->dstaddr.ss_family
836 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
837 return EINVAL;
838 break;
839 default: /*shouldn't happen*/
840 return EOPNOTSUPP;
842 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
843 return EINVAL;
845 switch (cmd) {
846 case SIOCALIFADDR:
848 struct in_aliasreq ifra;
850 if (iflr->flags & IFLR_PREFIX)
851 return EINVAL;
853 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
854 bzero(&ifra, sizeof ifra);
855 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
857 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
859 if (iflr->dstaddr.ss_family) { /*XXX*/
860 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
861 iflr->dstaddr.ss_len);
864 ifra.ifra_mask.sin_family = AF_INET;
865 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
866 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
868 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
870 case SIOCGLIFADDR:
871 case SIOCDLIFADDR:
873 struct ifaddr_container *ifac;
874 struct in_ifaddr *ia;
875 struct in_addr mask, candidate, match;
876 struct sockaddr_in *sin;
877 int cmp;
879 bzero(&mask, sizeof mask);
880 if (iflr->flags & IFLR_PREFIX) {
881 /* lookup a prefix rather than address. */
882 in_len2mask(&mask, iflr->prefixlen);
884 sin = (struct sockaddr_in *)&iflr->addr;
885 match.s_addr = sin->sin_addr.s_addr;
886 match.s_addr &= mask.s_addr;
888 /* if you set extra bits, that's wrong */
889 if (match.s_addr != sin->sin_addr.s_addr)
890 return EINVAL;
892 cmp = 1;
893 } else {
894 if (cmd == SIOCGLIFADDR) {
895 /* on getting an address, take the 1st match */
896 match.s_addr = 0; /* gcc4 warning */
897 cmp = 0; /*XXX*/
898 } else {
899 /* on deleting an address, do exact match */
900 in_len2mask(&mask, 32);
901 sin = (struct sockaddr_in *)&iflr->addr;
902 match.s_addr = sin->sin_addr.s_addr;
904 cmp = 1;
908 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
909 struct ifaddr *ifa = ifac->ifa;
911 if (ifa->ifa_addr->sa_family != AF_INET6)
912 continue;
913 if (!cmp)
914 break;
915 candidate.s_addr =
916 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
917 candidate.s_addr &= mask.s_addr;
918 if (candidate.s_addr == match.s_addr)
919 break;
921 if (ifac == NULL)
922 return EADDRNOTAVAIL;
923 ia = (struct in_ifaddr *)(ifac->ifa);
925 if (cmd == SIOCGLIFADDR) {
926 /* fill in the if_laddrreq structure */
927 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
929 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
930 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
931 ia->ia_dstaddr.sin_len);
932 } else
933 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
935 iflr->prefixlen =
936 in_mask2len(&ia->ia_sockmask.sin_addr);
938 iflr->flags = 0; /*XXX*/
940 return 0;
941 } else {
942 struct in_aliasreq ifra;
944 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
945 bzero(&ifra, sizeof ifra);
946 bcopy(iflr->iflr_name, ifra.ifra_name,
947 sizeof ifra.ifra_name);
949 bcopy(&ia->ia_addr, &ifra.ifra_addr,
950 ia->ia_addr.sin_len);
951 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
952 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
953 ia->ia_dstaddr.sin_len);
955 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
956 ia->ia_sockmask.sin_len);
958 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
959 ifp, td);
964 return EOPNOTSUPP; /*just for safety*/
968 * Delete any existing route for an interface.
970 void
971 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
973 in_scrubprefix(ia);
977 * Initialize an interface's internet address
978 * and routing table entry.
980 static int
981 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
982 const struct sockaddr_in *sin, int scrub)
984 u_long i = ntohl(sin->sin_addr.s_addr);
985 struct sockaddr_in oldaddr;
986 struct ifaddr_container *ifac;
987 int flags = RTF_UP, error = 0;
988 int was_hash = 0;
990 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
991 oldaddr = ia->ia_addr;
993 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
994 was_hash = 1;
995 in_iahash_remove(ia);
998 ia->ia_addr = *sin;
999 if (ia->ia_addr.sin_family == AF_INET)
1000 in_iahash_insert(ia);
1003 * Give the interface a chance to initialize
1004 * if this is its first address,
1005 * and to validate the address if necessary.
1007 if (ifp->if_ioctl != NULL) {
1008 ifnet_serialize_all(ifp);
1009 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
1010 ifnet_deserialize_all(ifp);
1011 if (error)
1012 goto fail;
1016 * Delete old route, if requested.
1018 if (scrub) {
1019 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1020 in_ifscrub(ifp, ia);
1021 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1025 * Calculate netmask/subnetmask.
1027 if (IN_CLASSA(i))
1028 ia->ia_netmask = IN_CLASSA_NET;
1029 else if (IN_CLASSB(i))
1030 ia->ia_netmask = IN_CLASSB_NET;
1031 else
1032 ia->ia_netmask = IN_CLASSC_NET;
1034 * The subnet mask usually includes at least the standard network part,
1035 * but may may be smaller in the case of supernetting.
1036 * If it is set, we believe it.
1038 if (ia->ia_subnetmask == 0) {
1039 ia->ia_subnetmask = ia->ia_netmask;
1040 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1041 } else {
1042 ia->ia_netmask &= ia->ia_subnetmask;
1044 ia->ia_net = i & ia->ia_netmask;
1045 ia->ia_subnet = i & ia->ia_subnetmask;
1046 in_socktrim(&ia->ia_sockmask);
1049 * Add route for the network.
1051 ia->ia_ifa.ifa_metric = ifp->if_metric;
1052 if (ifp->if_flags & IFF_BROADCAST) {
1053 ia->ia_broadaddr.sin_addr.s_addr =
1054 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1055 ia->ia_netbroadcast.s_addr =
1056 htonl(ia->ia_net | ~ ia->ia_netmask);
1057 } else if (ifp->if_flags & IFF_LOOPBACK) {
1058 ia->ia_dstaddr = ia->ia_addr;
1059 flags |= RTF_HOST;
1060 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1061 if (ia->ia_dstaddr.sin_family != AF_INET)
1062 return (0);
1063 flags |= RTF_HOST;
1067 * Don't add host routes for interface addresses of
1068 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1069 * possible to assign several such address pairs with consistent
1070 * results (no host route) and is required by BOOTP.
1072 * XXX: This is ugly ! There should be a way for the caller to
1073 * say that they don't want a host route.
1075 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1076 ia->ia_netmask != IN_CLASSA_NET ||
1077 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1078 error = in_addprefix(ia, flags);
1079 if (error)
1080 goto fail;
1084 * If the interface supports multicast, join the "all hosts"
1085 * multicast group on that interface.
1087 if (ifp->if_flags & IFF_MULTICAST) {
1088 struct in_addr addr;
1090 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1091 in_addmulti(&addr, ifp);
1093 return (0);
1094 fail:
1095 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1096 in_iahash_remove(ia);
1098 ia->ia_addr = oldaddr;
1099 if (was_hash)
1100 in_iahash_insert(ia);
1101 return (error);
1104 #define rtinitflags(x) \
1105 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1106 ? RTF_HOST : 0)
1109 * Add a route to prefix ("connected route" in cisco terminology).
1110 * Do nothing, if there are some interface addresses with the same
1111 * prefix already. This function assumes that the 'target' parent
1112 * interface is UP.
1114 static int
1115 in_addprefix(struct in_ifaddr *target, int flags)
1117 struct in_ifaddr_container *iac;
1118 struct in_addr prefix, mask;
1119 int error;
1121 mask = target->ia_sockmask.sin_addr;
1122 if (flags & RTF_HOST) {
1123 prefix = target->ia_dstaddr.sin_addr;
1124 } else {
1125 prefix = target->ia_addr.sin_addr;
1126 prefix.s_addr &= mask.s_addr;
1129 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1130 struct in_ifaddr *ia = iac->ia;
1131 struct in_addr p;
1133 /* Don't test against self */
1134 if (ia == target)
1135 continue;
1137 /* The tested address does not own a route entry */
1138 if ((ia->ia_flags & IFA_ROUTE) == 0)
1139 continue;
1141 /* Prefix test */
1142 if (rtinitflags(ia)) {
1143 p = ia->ia_dstaddr.sin_addr;
1144 } else {
1145 p = ia->ia_addr.sin_addr;
1146 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1148 if (prefix.s_addr != p.s_addr)
1149 continue;
1152 * If the to-be-added address and the curretly being
1153 * tested address are not host addresses, we need to
1154 * take subnetmask into consideration.
1156 if (!(flags & RTF_HOST) && !rtinitflags(ia) &&
1157 mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1158 continue;
1161 * If we got a matching prefix route inserted by other
1162 * interface address, we don't need to bother.
1164 return 0;
1168 * No one seem to have prefix route; insert it.
1170 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1171 if (!error)
1172 target->ia_flags |= IFA_ROUTE;
1173 return error;
1177 * Remove a route to prefix ("connected route" in cisco terminology).
1178 * Re-installs the route by using another interface address, if there's
1179 * one with the same prefix (otherwise we lose the route mistakenly).
1181 static void
1182 in_scrubprefix(struct in_ifaddr *target)
1184 struct in_ifaddr_container *iac;
1185 struct in_addr prefix, mask;
1186 int error;
1188 if ((target->ia_flags & IFA_ROUTE) == 0)
1189 return;
1191 mask = target->ia_sockmask.sin_addr;
1192 if (rtinitflags(target)) {
1193 prefix = target->ia_dstaddr.sin_addr;
1194 } else {
1195 prefix = target->ia_addr.sin_addr;
1196 prefix.s_addr &= mask.s_addr;
1199 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1200 struct in_ifaddr *ia = iac->ia;
1201 struct in_addr p;
1203 /* Don't test against self */
1204 if (ia == target)
1205 continue;
1207 /* The tested address already owns a route entry */
1208 if (ia->ia_flags & IFA_ROUTE)
1209 continue;
1212 * The prefix route of the tested address should
1213 * never be installed if its parent interface is
1214 * not UP yet.
1216 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1217 continue;
1219 /* Prefix test */
1220 if (rtinitflags(ia)) {
1221 p = ia->ia_dstaddr.sin_addr;
1222 } else {
1223 p = ia->ia_addr.sin_addr;
1224 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1226 if (prefix.s_addr != p.s_addr)
1227 continue;
1230 * We don't need to test subnetmask here, as what we do
1231 * in in_addprefix(), since if the the tested address's
1232 * parent interface is UP, the tested address should own
1233 * a prefix route entry and we would never reach here.
1237 * If we got a matching prefix route, move IFA_ROUTE to him
1239 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1240 target->ia_flags &= ~IFA_ROUTE;
1242 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
1243 if (!error)
1244 ia->ia_flags |= IFA_ROUTE;
1245 return;
1249 * No candidates for this prefix route; just remove it.
1251 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1252 target->ia_flags &= ~IFA_ROUTE;
1255 #undef rtinitflags
1258 * Return 1 if the address might be a local broadcast address.
1261 in_broadcast(struct in_addr in, struct ifnet *ifp)
1263 struct ifaddr_container *ifac;
1264 u_long t;
1266 if (in.s_addr == INADDR_BROADCAST ||
1267 in.s_addr == INADDR_ANY)
1268 return 1;
1269 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1270 return 0;
1271 t = ntohl(in.s_addr);
1273 * Look through the list of addresses for a match
1274 * with a broadcast address.
1276 #define ia ((struct in_ifaddr *)ifa)
1277 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1278 struct ifaddr *ifa = ifac->ifa;
1280 if (ifa->ifa_addr->sa_family == AF_INET &&
1281 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1282 in.s_addr == ia->ia_netbroadcast.s_addr ||
1284 * Check for old-style (host 0) broadcast.
1286 t == ia->ia_subnet || t == ia->ia_net) &&
1288 * Check for an all one subnetmask. These
1289 * only exist when an interface gets a secondary
1290 * address.
1292 ia->ia_subnetmask != (u_long)0xffffffff)
1293 return 1;
1295 return (0);
1296 #undef ia
1299 * Add an address to the list of IP multicast addresses for a given interface.
1301 struct in_multi *
1302 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1304 struct in_multi *inm;
1305 int error;
1306 struct sockaddr_in sin;
1307 struct ifmultiaddr *ifma;
1310 * Call generic routine to add membership or increment
1311 * refcount. It wants addresses in the form of a sockaddr,
1312 * so we build one here (being careful to zero the unused bytes).
1314 bzero(&sin, sizeof sin);
1315 sin.sin_family = AF_INET;
1316 sin.sin_len = sizeof sin;
1317 sin.sin_addr = *ap;
1318 crit_enter();
1319 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1320 if (error) {
1321 crit_exit();
1322 return 0;
1326 * If ifma->ifma_protospec is null, then if_addmulti() created
1327 * a new record. Otherwise, we are done.
1329 if (ifma->ifma_protospec != 0) {
1330 crit_exit();
1331 return ifma->ifma_protospec;
1334 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1335 at interrupt time? If so, need to fix if_addmulti. XXX */
1336 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1337 inm->inm_addr = *ap;
1338 inm->inm_ifp = ifp;
1339 inm->inm_ifma = ifma;
1340 ifma->ifma_protospec = inm;
1341 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1344 * Let IGMP know that we have joined a new IP multicast group.
1346 igmp_joingroup(inm);
1347 crit_exit();
1348 return (inm);
1352 * Delete a multicast address record.
1354 void
1355 in_delmulti(struct in_multi *inm)
1357 struct ifmultiaddr *ifma;
1358 struct in_multi my_inm;
1360 crit_enter();
1361 ifma = inm->inm_ifma;
1362 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1363 if (ifma->ifma_refcount == 1) {
1365 * No remaining claims to this record; let IGMP know that
1366 * we are leaving the multicast group.
1367 * But do it after the if_delmulti() which might reset
1368 * the interface and nuke the packet.
1370 my_inm = *inm ;
1371 ifma->ifma_protospec = 0;
1372 LIST_REMOVE(inm, inm_link);
1373 kfree(inm, M_IPMADDR);
1375 /* XXX - should be separate API for when we have an ifma? */
1376 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1377 if (my_inm.inm_ifp != NULL)
1378 igmp_leavegroup(&my_inm);
1379 crit_exit();
1382 void
1383 in_ifdetach(struct ifnet *ifp)
1385 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
1386 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);