Parallelize in_ifaddrhead operation
[dragonfly.git] / sys / netinet / in.c
blobe0ac707cd7595dc0997e7b1f7d476bc4ef37a408
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.37 2008/06/08 08:38:05 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/msgport.h>
46 #include <sys/socket.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/thread2.h>
52 #include <net/if.h>
53 #include <net/if_types.h>
54 #include <net/route.h>
55 #include <net/netmsg2.h>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/in_pcb.h>
61 #include <netinet/igmp_var.h>
63 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
65 static int in_mask2len (struct in_addr *);
66 static void in_len2mask (struct in_addr *, int);
67 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
68 struct ifnet *, struct thread *);
70 static void in_socktrim (struct sockaddr_in *);
71 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
72 const struct sockaddr_in *, int);
74 static void in_control_dispatch(struct netmsg *);
75 static int in_control_internal(u_long, caddr_t, struct ifnet *,
76 struct thread *);
78 static int subnetsarelocal = 0;
79 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
80 &subnetsarelocal, 0, "");
82 struct in_multihead in_multihead; /* XXX BSS initialization */
84 extern struct inpcbinfo ripcbinfo;
85 extern struct inpcbinfo udbinfo;
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.
93 int
94 in_localaddr(struct in_addr in)
96 u_long i = ntohl(in.s_addr);
97 struct in_ifaddr_container *iac;
98 struct in_ifaddr *ia;
100 if (subnetsarelocal) {
101 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
102 ia = iac->ia;
104 if ((i & ia->ia_netmask) == ia->ia_net)
105 return (1);
107 } else {
108 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
109 ia = iac->ia;
111 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
112 return (1);
115 return (0);
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.
124 in_canforward(struct in_addr in)
126 u_long i = ntohl(in.s_addr);
127 u_long net;
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);
136 return (1);
140 * Trim a mask in a sockaddr
142 static void
143 in_socktrim(struct sockaddr_in *ap)
145 char *cplim = (char *) &ap->sin_addr;
146 char *cp = (char *) (&ap->sin_addr + 1);
148 ap->sin_len = 0;
149 while (--cp >= cplim)
150 if (*cp) {
151 (ap)->sin_len = cp - (char *) (ap) + 1;
152 break;
156 static int
157 in_mask2len(struct in_addr *mask)
159 int x, y;
160 u_char *p;
162 p = (u_char *)mask;
163 for (x = 0; x < sizeof *mask; x++) {
164 if (p[x] != 0xff)
165 break;
167 y = 0;
168 if (x < sizeof *mask) {
169 for (y = 0; y < 8; y++) {
170 if ((p[x] & (0x80 >> y)) == 0)
171 break;
174 return x * 8 + y;
177 static void
178 in_len2mask(struct in_addr *mask, int len)
180 int i;
181 u_char *p;
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;
191 static int in_interfaces; /* number of external internet interfaces */
193 struct in_control_arg {
194 u_long cmd;
195 caddr_t data;
196 struct ifnet *ifp;
197 struct thread *td;
200 static void
201 in_control_dispatch(struct netmsg *nmsg)
203 struct lwkt_msg *msg = &nmsg->nm_lmsg;
204 const struct in_control_arg *arg = msg->u.ms_resultp;
205 int error;
207 error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td);
208 lwkt_replymsg(msg, error);
212 * Generic internet control operations (ioctl's).
213 * Ifp is 0 if not an interface-specific ioctl.
215 * NOTE! td might be NULL.
217 /* ARGSUSED */
219 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
220 struct thread *td)
222 struct netmsg nmsg;
223 struct in_control_arg arg;
224 struct lwkt_msg *msg;
225 int error;
227 switch (cmd) {
228 case SIOCALIFADDR:
229 case SIOCDLIFADDR:
230 if (td && (error = suser(td)) != 0)
231 return error;
232 /* FALLTHROUGH */
233 case SIOCGLIFADDR:
234 if (!ifp)
235 return EINVAL;
236 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
239 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
240 ("recursive SIOC%cLIFADDR!\n",
241 cmd == SIOCDLIFADDR ? 'D' : 'A'));
244 * IFADDR alterations are serialized by netisr0
246 switch (cmd) {
247 case SIOCSIFDSTADDR:
248 case SIOCSIFBRDADDR:
249 case SIOCSIFADDR:
250 case SIOCSIFNETMASK:
251 case SIOCAIFADDR:
252 case SIOCDIFADDR:
253 bzero(&arg, sizeof(arg));
254 arg.cmd = cmd;
255 arg.data = data;
256 arg.ifp = ifp;
257 arg.td = td;
259 netmsg_init(&nmsg, &curthread->td_msgport, 0,
260 in_control_dispatch);
261 msg = &nmsg.nm_lmsg;
262 msg->u.ms_resultp = &arg;
264 lwkt_domsg(cpu_portfn(0), msg, 0);
265 return msg->ms_error;
266 default:
267 return in_control_internal(cmd, data, ifp, td);
271 static void
272 in_ialink_dispatch(struct netmsg *nmsg)
274 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
275 struct in_ifaddr *ia = lmsg->u.ms_resultp;
276 struct ifaddr_container *ifac;
277 struct in_ifaddr_container *iac;
278 int cpu = mycpuid;
280 crit_enter();
282 ifac = &ia->ia_ifa.ifa_containers[cpu];
283 ASSERT_IFAC_VALID(ifac);
284 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
285 ("ia is on in_ifaddrheads\n"));
287 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
288 iac = &ifac->ifa_proto_u.u_in_ifac;
289 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
291 crit_exit();
293 ifa_forwardmsg(lmsg, cpu + 1);
296 static void
297 in_iaunlink_dispatch(struct netmsg *nmsg)
299 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
300 struct in_ifaddr *ia = lmsg->u.ms_resultp;
301 struct ifaddr_container *ifac;
302 struct in_ifaddr_container *iac;
303 int cpu = mycpuid;
305 crit_enter();
307 ifac = &ia->ia_ifa.ifa_containers[cpu];
308 ASSERT_IFAC_VALID(ifac);
309 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
310 ("ia is not on in_ifaddrheads\n"));
312 iac = &ifac->ifa_proto_u.u_in_ifac;
313 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
314 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
316 crit_exit();
318 ifa_forwardmsg(lmsg, cpu + 1);
321 static void
322 in_ialink(struct in_ifaddr *ia)
324 struct netmsg nmsg;
325 struct lwkt_msg *lmsg;
327 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_ialink_dispatch);
328 lmsg = &nmsg.nm_lmsg;
329 lmsg->u.ms_resultp = ia;
331 ifa_domsg(lmsg);
334 void
335 in_iaunlink(struct in_ifaddr *ia)
337 struct netmsg nmsg;
338 struct lwkt_msg *lmsg;
340 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iaunlink_dispatch);
341 lmsg = &nmsg.nm_lmsg;
342 lmsg->u.ms_resultp = ia;
344 ifa_domsg(lmsg);
347 static __inline struct in_ifaddr *
348 in_ianext(struct in_ifaddr *oia)
350 struct ifaddr_container *ifac;
351 struct in_ifaddr_container *iac;
353 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
354 ASSERT_IFAC_VALID(ifac);
355 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
356 ("ia is not on in_ifaddrheads\n"));
358 iac = &ifac->ifa_proto_u.u_in_ifac;
359 iac = TAILQ_NEXT(iac, ia_link);
360 if (iac != NULL)
361 return iac->ia;
362 else
363 return NULL;
366 static int
367 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
368 struct thread *td)
370 struct ifreq *ifr = (struct ifreq *)data;
371 struct in_ifaddr *ia = 0, *iap;
372 struct in_addr dst;
373 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
374 struct ifaddr_container *ifac;
375 struct sockaddr_in oldaddr;
376 int hostIsNew, iaIsNew, maskIsNew;
377 int error = 0;
379 iaIsNew = 0;
382 * Find address for this interface, if it exists.
384 * If an alias address was specified, find that one instead of
385 * the first one on the interface, if possible
387 if (ifp) {
388 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
389 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
390 if (iap->ia_ifp == ifp &&
391 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
392 ia = iap;
393 break;
395 if (ia == NULL) {
396 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
397 ifa_link) {
398 iap = ifatoia(ifac->ifa);
399 if (iap->ia_addr.sin_family == AF_INET) {
400 ia = iap;
401 break;
407 switch (cmd) {
408 case SIOCAIFADDR:
409 case SIOCDIFADDR:
410 if (ifp == NULL)
411 return (EADDRNOTAVAIL);
412 if (ifra->ifra_addr.sin_family == AF_INET) {
413 while (ia != NULL) {
414 if (ia->ia_ifp == ifp &&
415 ia->ia_addr.sin_addr.s_addr ==
416 ifra->ifra_addr.sin_addr.s_addr)
417 break;
418 ia = in_ianext(ia);
420 if ((ifp->if_flags & IFF_POINTOPOINT) &&
421 cmd == SIOCAIFADDR &&
422 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
423 return EDESTADDRREQ;
426 if (cmd == SIOCDIFADDR && ia == NULL)
427 return (EADDRNOTAVAIL);
428 /* FALLTHROUGH */
429 case SIOCSIFADDR:
430 case SIOCSIFNETMASK:
431 case SIOCSIFDSTADDR:
432 if (td && (error = suser(td)) != 0)
433 return error;
435 if (ifp == NULL)
436 return (EADDRNOTAVAIL);
438 if (cmd == SIOCSIFDSTADDR &&
439 (ifp->if_flags & IFF_POINTOPOINT) == 0)
440 return (EINVAL);
442 if (ia == NULL) {
443 struct ifaddr *ifa;
444 int i;
446 ia = ifa_create(sizeof(*ia), M_WAITOK);
447 ifa = &ia->ia_ifa;
450 * Setup per-CPU information
452 for (i = 0; i < ncpus; ++i) {
453 struct in_ifaddr_container *iac;
455 ifac = &ifa->ifa_containers[i];
456 iac = &ifac->ifa_proto_u.u_in_ifac;
457 iac->ia = ia;
458 iac->ia_ifac = ifac;
462 * Protect from NETISR_IP traversing address list
463 * while we're modifying it.
465 crit_enter();
467 in_ialink(ia);
468 ifa_iflink(ifa, ifp, 1);
470 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
471 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
472 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
473 ia->ia_sockmask.sin_len = 8;
474 ia->ia_sockmask.sin_family = AF_INET;
475 if (ifp->if_flags & IFF_BROADCAST) {
476 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
477 ia->ia_broadaddr.sin_family = AF_INET;
479 ia->ia_ifp = ifp;
480 if (!(ifp->if_flags & IFF_LOOPBACK))
481 in_interfaces++;
482 iaIsNew = 1;
484 crit_exit();
486 break;
488 case SIOCSIFBRDADDR:
489 if (td && (error = suser(td)) != 0)
490 return error;
491 /* FALLTHROUGH */
493 case SIOCGIFADDR:
494 case SIOCGIFNETMASK:
495 case SIOCGIFDSTADDR:
496 case SIOCGIFBRDADDR:
497 if (ia == NULL)
498 return (EADDRNOTAVAIL);
499 break;
502 switch (cmd) {
503 case SIOCGIFADDR:
504 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
505 return (0);
507 case SIOCGIFBRDADDR:
508 if ((ifp->if_flags & IFF_BROADCAST) == 0)
509 return (EINVAL);
510 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
511 return (0);
513 case SIOCGIFDSTADDR:
514 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
515 return (EINVAL);
516 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
517 return (0);
519 case SIOCGIFNETMASK:
520 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
521 return (0);
523 case SIOCSIFDSTADDR:
524 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
526 oldaddr = ia->ia_dstaddr;
527 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
528 if (ifp->if_ioctl != NULL) {
529 lwkt_serialize_enter(ifp->if_serializer);
530 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
531 td->td_proc->p_ucred);
532 lwkt_serialize_exit(ifp->if_serializer);
533 if (error) {
534 ia->ia_dstaddr = oldaddr;
535 return (error);
538 if (ia->ia_flags & IFA_ROUTE) {
539 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
540 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
541 ia->ia_ifa.ifa_dstaddr =
542 (struct sockaddr *)&ia->ia_dstaddr;
543 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
545 return (0);
547 case SIOCSIFBRDADDR:
548 if ((ifp->if_flags & IFF_BROADCAST) == 0)
549 return (EINVAL);
550 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
551 return (0);
553 case SIOCSIFADDR:
554 error = in_ifinit(ifp, ia,
555 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
556 if (error != 0 && iaIsNew)
557 break;
558 if (error == 0)
559 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
560 return (0);
562 case SIOCSIFNETMASK:
563 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
564 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
565 return (0);
567 case SIOCAIFADDR:
568 maskIsNew = 0;
569 hostIsNew = 1;
570 error = 0;
571 if (ia->ia_addr.sin_family == AF_INET) {
572 if (ifra->ifra_addr.sin_len == 0) {
573 ifra->ifra_addr = ia->ia_addr;
574 hostIsNew = 0;
575 } else if (ifra->ifra_addr.sin_addr.s_addr ==
576 ia->ia_addr.sin_addr.s_addr) {
577 hostIsNew = 0;
580 if (ifra->ifra_mask.sin_len) {
581 in_ifscrub(ifp, ia);
582 ia->ia_sockmask = ifra->ifra_mask;
583 ia->ia_sockmask.sin_family = AF_INET;
584 ia->ia_subnetmask =
585 ntohl(ia->ia_sockmask.sin_addr.s_addr);
586 maskIsNew = 1;
588 if ((ifp->if_flags & IFF_POINTOPOINT) &&
589 ifra->ifra_dstaddr.sin_family == AF_INET) {
590 in_ifscrub(ifp, ia);
591 ia->ia_dstaddr = ifra->ifra_dstaddr;
592 maskIsNew = 1; /* We lie; but the effect's the same */
594 if (ifra->ifra_addr.sin_family == AF_INET &&
595 (hostIsNew || maskIsNew))
596 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
598 if (error != 0 && iaIsNew)
599 break;
601 if ((ifp->if_flags & IFF_BROADCAST) &&
602 ifra->ifra_broadaddr.sin_family == AF_INET)
603 ia->ia_broadaddr = ifra->ifra_broadaddr;
604 if (error == 0)
605 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
606 return (error);
608 case SIOCDIFADDR:
610 * in_ifscrub kills the interface route.
612 in_ifscrub(ifp, ia);
614 * in_ifadown gets rid of all the rest of
615 * the routes. This is not quite the right
616 * thing to do, but at least if we are running
617 * a routing process they will come back.
619 in_ifadown(&ia->ia_ifa, 1);
620 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
621 error = 0;
622 break;
624 default:
625 if (ifp == NULL || ifp->if_ioctl == NULL)
626 return (EOPNOTSUPP);
627 lwkt_serialize_enter(ifp->if_serializer);
628 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
629 lwkt_serialize_exit(ifp->if_serializer);
630 return (error);
633 ifa_ifunlink(&ia->ia_ifa, ifp);
634 in_iaunlink(ia);
637 * Protect from NETISR_IP traversing address list while we're modifying
638 * it.
640 crit_enter(); /* XXX MP */
641 if (cmd == SIOCDIFADDR && ia->ia_addr.sin_family == AF_INET) {
642 /* XXX Assume that 'ia' is in hash table */
643 LIST_REMOVE(ia, ia_hash);
645 crit_exit(); /* XXX MP */
647 ifa_destroy(&ia->ia_ifa);
649 return (error);
653 * SIOC[GAD]LIFADDR.
654 * SIOCGLIFADDR: get first address. (?!?)
655 * SIOCGLIFADDR with IFLR_PREFIX:
656 * get first address that matches the specified prefix.
657 * SIOCALIFADDR: add the specified address.
658 * SIOCALIFADDR with IFLR_PREFIX:
659 * EINVAL since we can't deduce hostid part of the address.
660 * SIOCDLIFADDR: delete the specified address.
661 * SIOCDLIFADDR with IFLR_PREFIX:
662 * delete the first address that matches the specified prefix.
663 * return values:
664 * EINVAL on invalid parameters
665 * EADDRNOTAVAIL on prefix match failed/specified address not found
666 * other values may be returned from in_ioctl()
668 * NOTE! td might be NULL.
670 static int
671 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
672 struct thread *td)
674 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
676 /* sanity checks */
677 if (!data || !ifp) {
678 panic("invalid argument to in_lifaddr_ioctl");
679 /*NOTRECHED*/
682 switch (cmd) {
683 case SIOCGLIFADDR:
684 /* address must be specified on GET with IFLR_PREFIX */
685 if ((iflr->flags & IFLR_PREFIX) == 0)
686 break;
687 /*FALLTHROUGH*/
688 case SIOCALIFADDR:
689 case SIOCDLIFADDR:
690 /* address must be specified on ADD and DELETE */
691 if (iflr->addr.ss_family != AF_INET)
692 return EINVAL;
693 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
694 return EINVAL;
695 /* XXX need improvement */
696 if (iflr->dstaddr.ss_family
697 && iflr->dstaddr.ss_family != AF_INET)
698 return EINVAL;
699 if (iflr->dstaddr.ss_family
700 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
701 return EINVAL;
702 break;
703 default: /*shouldn't happen*/
704 return EOPNOTSUPP;
706 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
707 return EINVAL;
709 switch (cmd) {
710 case SIOCALIFADDR:
712 struct in_aliasreq ifra;
714 if (iflr->flags & IFLR_PREFIX)
715 return EINVAL;
717 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
718 bzero(&ifra, sizeof ifra);
719 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
721 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
723 if (iflr->dstaddr.ss_family) { /*XXX*/
724 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
725 iflr->dstaddr.ss_len);
728 ifra.ifra_mask.sin_family = AF_INET;
729 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
730 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
732 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
734 case SIOCGLIFADDR:
735 case SIOCDLIFADDR:
737 struct ifaddr_container *ifac;
738 struct in_ifaddr *ia;
739 struct in_addr mask, candidate, match;
740 struct sockaddr_in *sin;
741 int cmp;
743 bzero(&mask, sizeof mask);
744 if (iflr->flags & IFLR_PREFIX) {
745 /* lookup a prefix rather than address. */
746 in_len2mask(&mask, iflr->prefixlen);
748 sin = (struct sockaddr_in *)&iflr->addr;
749 match.s_addr = sin->sin_addr.s_addr;
750 match.s_addr &= mask.s_addr;
752 /* if you set extra bits, that's wrong */
753 if (match.s_addr != sin->sin_addr.s_addr)
754 return EINVAL;
756 cmp = 1;
757 } else {
758 if (cmd == SIOCGLIFADDR) {
759 /* on getting an address, take the 1st match */
760 match.s_addr = 0; /* gcc4 warning */
761 cmp = 0; /*XXX*/
762 } else {
763 /* on deleting an address, do exact match */
764 in_len2mask(&mask, 32);
765 sin = (struct sockaddr_in *)&iflr->addr;
766 match.s_addr = sin->sin_addr.s_addr;
768 cmp = 1;
772 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
773 struct ifaddr *ifa = ifac->ifa;
775 if (ifa->ifa_addr->sa_family != AF_INET6)
776 continue;
777 if (!cmp)
778 break;
779 candidate.s_addr =
780 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
781 candidate.s_addr &= mask.s_addr;
782 if (candidate.s_addr == match.s_addr)
783 break;
785 if (ifac == NULL)
786 return EADDRNOTAVAIL;
787 ia = (struct in_ifaddr *)(ifac->ifa);
789 if (cmd == SIOCGLIFADDR) {
790 /* fill in the if_laddrreq structure */
791 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
793 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
794 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
795 ia->ia_dstaddr.sin_len);
796 } else
797 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
799 iflr->prefixlen =
800 in_mask2len(&ia->ia_sockmask.sin_addr);
802 iflr->flags = 0; /*XXX*/
804 return 0;
805 } else {
806 struct in_aliasreq ifra;
808 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
809 bzero(&ifra, sizeof ifra);
810 bcopy(iflr->iflr_name, ifra.ifra_name,
811 sizeof ifra.ifra_name);
813 bcopy(&ia->ia_addr, &ifra.ifra_addr,
814 ia->ia_addr.sin_len);
815 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
816 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
817 ia->ia_dstaddr.sin_len);
819 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
820 ia->ia_sockmask.sin_len);
822 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
823 ifp, td);
828 return EOPNOTSUPP; /*just for safety*/
832 * Delete any existing route for an interface.
834 void
835 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
838 if ((ia->ia_flags & IFA_ROUTE) == 0)
839 return;
840 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
841 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
842 else
843 rtinit(&ia->ia_ifa, RTM_DELETE, 0);
844 ia->ia_flags &= ~IFA_ROUTE;
848 * Initialize an interface's internet address
849 * and routing table entry.
851 static int
852 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
853 const struct sockaddr_in *sin, int scrub)
855 u_long i = ntohl(sin->sin_addr.s_addr);
856 struct sockaddr_in oldaddr;
857 int flags = RTF_UP, error = 0;
858 int old_hash = 0, new_hash = 0;
860 crit_enter();
861 oldaddr = ia->ia_addr;
862 if (oldaddr.sin_family == AF_INET) {
863 old_hash = 1;
864 LIST_REMOVE(ia, ia_hash);
867 ia->ia_addr = *sin;
868 if (ia->ia_addr.sin_family == AF_INET) {
869 new_hash = 1;
870 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
871 ia, ia_hash);
873 crit_exit();
876 * Give the interface a chance to initialize
877 * if this is its first address,
878 * and to validate the address if necessary.
880 if (ifp->if_ioctl != NULL) {
881 lwkt_serialize_enter(ifp->if_serializer);
882 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
883 lwkt_serialize_exit(ifp->if_serializer);
884 if (error)
885 goto fail;
889 * Delete old route, if requested.
891 if (scrub) {
892 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
893 in_ifscrub(ifp, ia);
894 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
898 * Calculate netmask/subnetmask.
900 if (IN_CLASSA(i))
901 ia->ia_netmask = IN_CLASSA_NET;
902 else if (IN_CLASSB(i))
903 ia->ia_netmask = IN_CLASSB_NET;
904 else
905 ia->ia_netmask = IN_CLASSC_NET;
907 * The subnet mask usually includes at least the standard network part,
908 * but may may be smaller in the case of supernetting.
909 * If it is set, we believe it.
911 if (ia->ia_subnetmask == 0) {
912 ia->ia_subnetmask = ia->ia_netmask;
913 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
914 } else {
915 ia->ia_netmask &= ia->ia_subnetmask;
917 ia->ia_net = i & ia->ia_netmask;
918 ia->ia_subnet = i & ia->ia_subnetmask;
919 in_socktrim(&ia->ia_sockmask);
922 * Add route for the network.
924 ia->ia_ifa.ifa_metric = ifp->if_metric;
925 if (ifp->if_flags & IFF_BROADCAST) {
926 ia->ia_broadaddr.sin_addr.s_addr =
927 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
928 ia->ia_netbroadcast.s_addr =
929 htonl(ia->ia_net | ~ ia->ia_netmask);
930 } else if (ifp->if_flags & IFF_LOOPBACK) {
931 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
932 flags |= RTF_HOST;
933 } else if (ifp->if_flags & IFF_POINTOPOINT) {
934 if (ia->ia_dstaddr.sin_family != AF_INET)
935 return (0);
936 flags |= RTF_HOST;
940 * Don't add host routes for interface addresses of
941 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
942 * possible to assign several such address pairs with consistent
943 * results (no host route) and is required by BOOTP.
945 * XXX: This is ugly ! There should be a way for the caller to
946 * say that they don't want a host route.
948 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
949 ia->ia_netmask != IN_CLASSA_NET ||
950 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
951 if ((error = rtinit(&ia->ia_ifa, RTM_ADD, flags)) != 0)
952 goto fail;
953 ia->ia_flags |= IFA_ROUTE;
957 * If the interface supports multicast, join the "all hosts"
958 * multicast group on that interface.
960 if (ifp->if_flags & IFF_MULTICAST) {
961 struct in_addr addr;
963 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
964 in_addmulti(&addr, ifp);
966 return (0);
967 fail:
968 crit_enter();
969 if (new_hash)
970 LIST_REMOVE(ia, ia_hash);
972 ia->ia_addr = oldaddr;
973 if (old_hash) {
974 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
975 ia, ia_hash);
977 crit_exit();
978 return (error);
983 * Return 1 if the address might be a local broadcast address.
986 in_broadcast(struct in_addr in, struct ifnet *ifp)
988 struct ifaddr_container *ifac;
989 u_long t;
991 if (in.s_addr == INADDR_BROADCAST ||
992 in.s_addr == INADDR_ANY)
993 return 1;
994 if ((ifp->if_flags & IFF_BROADCAST) == 0)
995 return 0;
996 t = ntohl(in.s_addr);
998 * Look through the list of addresses for a match
999 * with a broadcast address.
1001 #define ia ((struct in_ifaddr *)ifa)
1002 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1003 struct ifaddr *ifa = ifac->ifa;
1005 if (ifa->ifa_addr->sa_family == AF_INET &&
1006 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1007 in.s_addr == ia->ia_netbroadcast.s_addr ||
1009 * Check for old-style (host 0) broadcast.
1011 t == ia->ia_subnet || t == ia->ia_net) &&
1013 * Check for an all one subnetmask. These
1014 * only exist when an interface gets a secondary
1015 * address.
1017 ia->ia_subnetmask != (u_long)0xffffffff)
1018 return 1;
1020 return (0);
1021 #undef ia
1024 * Add an address to the list of IP multicast addresses for a given interface.
1026 struct in_multi *
1027 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1029 struct in_multi *inm;
1030 int error;
1031 struct sockaddr_in sin;
1032 struct ifmultiaddr *ifma;
1035 * Call generic routine to add membership or increment
1036 * refcount. It wants addresses in the form of a sockaddr,
1037 * so we build one here (being careful to zero the unused bytes).
1039 bzero(&sin, sizeof sin);
1040 sin.sin_family = AF_INET;
1041 sin.sin_len = sizeof sin;
1042 sin.sin_addr = *ap;
1043 crit_enter();
1044 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1045 if (error) {
1046 crit_exit();
1047 return 0;
1051 * If ifma->ifma_protospec is null, then if_addmulti() created
1052 * a new record. Otherwise, we are done.
1054 if (ifma->ifma_protospec != 0) {
1055 crit_exit();
1056 return ifma->ifma_protospec;
1059 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1060 at interrupt time? If so, need to fix if_addmulti. XXX */
1061 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1062 inm->inm_addr = *ap;
1063 inm->inm_ifp = ifp;
1064 inm->inm_ifma = ifma;
1065 ifma->ifma_protospec = inm;
1066 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1069 * Let IGMP know that we have joined a new IP multicast group.
1071 igmp_joingroup(inm);
1072 crit_exit();
1073 return (inm);
1077 * Delete a multicast address record.
1079 void
1080 in_delmulti(struct in_multi *inm)
1082 struct ifmultiaddr *ifma;
1083 struct in_multi my_inm;
1085 crit_enter();
1086 ifma = inm->inm_ifma;
1087 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1088 if (ifma->ifma_refcount == 1) {
1090 * No remaining claims to this record; let IGMP know that
1091 * we are leaving the multicast group.
1092 * But do it after the if_delmulti() which might reset
1093 * the interface and nuke the packet.
1095 my_inm = *inm ;
1096 ifma->ifma_protospec = 0;
1097 LIST_REMOVE(inm, inm_link);
1098 kfree(inm, M_IPMADDR);
1100 /* XXX - should be separate API for when we have an ifma? */
1101 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1102 if (my_inm.inm_ifp != NULL)
1103 igmp_leavegroup(&my_inm);
1104 crit_exit();
1107 void
1108 in_ifdetach(struct ifnet *ifp)
1110 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
1111 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);