Hold ifnet.if_serializer around ifnet.if_ioctl; rtinit() does not need
[dragonfly.git] / sys / netinet / in.c
blob4aeb24cc60fda35d2625aae95b6047bfa8a27e02
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.30 2008/05/24 05:22:44 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 *,
72 struct in_ifaddr *, 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 *ia;
99 if (subnetsarelocal) {
100 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
101 if ((i & ia->ia_netmask) == ia->ia_net)
102 return (1);
103 } else {
104 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
105 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
106 return (1);
108 return (0);
112 * Determine whether an IP address is in a reserved set of addresses
113 * that may not be forwarded, or whether datagrams to that destination
114 * may be forwarded.
117 in_canforward(struct in_addr in)
119 u_long i = ntohl(in.s_addr);
120 u_long net;
122 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
123 return (0);
124 if (IN_CLASSA(i)) {
125 net = i & IN_CLASSA_NET;
126 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
127 return (0);
129 return (1);
133 * Trim a mask in a sockaddr
135 static void
136 in_socktrim(struct sockaddr_in *ap)
138 char *cplim = (char *) &ap->sin_addr;
139 char *cp = (char *) (&ap->sin_addr + 1);
141 ap->sin_len = 0;
142 while (--cp >= cplim)
143 if (*cp) {
144 (ap)->sin_len = cp - (char *) (ap) + 1;
145 break;
149 static int
150 in_mask2len(struct in_addr *mask)
152 int x, y;
153 u_char *p;
155 p = (u_char *)mask;
156 for (x = 0; x < sizeof *mask; x++) {
157 if (p[x] != 0xff)
158 break;
160 y = 0;
161 if (x < sizeof *mask) {
162 for (y = 0; y < 8; y++) {
163 if ((p[x] & (0x80 >> y)) == 0)
164 break;
167 return x * 8 + y;
170 static void
171 in_len2mask(struct in_addr *mask, int len)
173 int i;
174 u_char *p;
176 p = (u_char *)mask;
177 bzero(mask, sizeof *mask);
178 for (i = 0; i < len / 8; i++)
179 p[i] = 0xff;
180 if (len % 8)
181 p[i] = (0xff00 >> (len % 8)) & 0xff;
184 static int in_interfaces; /* number of external internet interfaces */
186 struct in_control_arg {
187 u_long cmd;
188 caddr_t data;
189 struct ifnet *ifp;
190 struct thread *td;
193 static void
194 in_control_dispatch(struct netmsg *nmsg)
196 struct lwkt_msg *msg = &nmsg->nm_lmsg;
197 const struct in_control_arg *arg = msg->u.ms_resultp;
198 int error;
200 error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td);
201 lwkt_replymsg(msg, error);
205 * Generic internet control operations (ioctl's).
206 * Ifp is 0 if not an interface-specific ioctl.
208 * NOTE! td might be NULL.
210 /* ARGSUSED */
212 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
213 struct thread *td)
215 struct netmsg nmsg;
216 struct in_control_arg arg;
217 struct lwkt_msg *msg;
218 int error;
220 switch (cmd) {
221 case SIOCALIFADDR:
222 case SIOCDLIFADDR:
223 if (td && (error = suser(td)) != 0)
224 return error;
225 /* FALLTHROUGH */
226 case SIOCGLIFADDR:
227 if (!ifp)
228 return EINVAL;
229 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
232 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
233 ("recursive SIOC%cLIFADDR!\n",
234 cmd == SIOCDLIFADDR ? 'D' : 'A'));
237 * IFADDR alterations are serialized by netisr0
239 switch (cmd) {
240 case SIOCSIFDSTADDR:
241 case SIOCSIFBRDADDR:
242 case SIOCSIFADDR:
243 case SIOCSIFNETMASK:
244 case SIOCAIFADDR:
245 case SIOCDIFADDR:
246 bzero(&arg, sizeof(arg));
247 arg.cmd = cmd;
248 arg.data = data;
249 arg.ifp = ifp;
250 arg.td = td;
252 netmsg_init(&nmsg, &curthread->td_msgport, 0,
253 in_control_dispatch);
254 msg = &nmsg.nm_lmsg;
255 msg->u.ms_resultp = &arg;
257 lwkt_domsg(cpu_portfn(0), msg, 0);
258 return msg->ms_error;
259 default:
260 return in_control_internal(cmd, data, ifp, td);
264 static int
265 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
266 struct thread *td)
268 struct ifreq *ifr = (struct ifreq *)data;
269 struct in_ifaddr *ia = 0, *iap;
270 struct in_addr dst;
271 struct in_ifaddr *oia;
272 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
273 struct sockaddr_in oldaddr;
274 int hostIsNew, iaIsNew, maskIsNew;
275 int error = 0;
277 iaIsNew = 0;
280 * Find address for this interface, if it exists.
282 * If an alias address was specified, find that one instead of
283 * the first one on the interface, if possible
285 if (ifp) {
286 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
287 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
288 if (iap->ia_ifp == ifp &&
289 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
290 ia = iap;
291 break;
293 if (ia == NULL) {
294 struct ifaddr_container *ifac;
296 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
297 ifa_link) {
298 iap = ifatoia(ifac->ifa);
299 if (iap->ia_addr.sin_family == AF_INET) {
300 ia = iap;
301 break;
307 switch (cmd) {
308 case SIOCAIFADDR:
309 case SIOCDIFADDR:
310 if (ifp == NULL)
311 return (EADDRNOTAVAIL);
312 if (ifra->ifra_addr.sin_family == AF_INET) {
313 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
314 if (ia->ia_ifp == ifp &&
315 ia->ia_addr.sin_addr.s_addr ==
316 ifra->ifra_addr.sin_addr.s_addr)
317 break;
319 if ((ifp->if_flags & IFF_POINTOPOINT) &&
320 cmd == SIOCAIFADDR &&
321 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
322 return EDESTADDRREQ;
325 if (cmd == SIOCDIFADDR && ia == NULL)
326 return (EADDRNOTAVAIL);
327 /* FALLTHROUGH */
328 case SIOCSIFADDR:
329 case SIOCSIFNETMASK:
330 case SIOCSIFDSTADDR:
331 if (td && (error = suser(td)) != 0)
332 return error;
334 if (ifp == NULL)
335 return (EADDRNOTAVAIL);
336 if (ia == NULL) {
337 struct ifaddr *ifa;
339 ia = ifa_create(sizeof(*ia), M_WAITOK);
342 * Protect from NETISR_IP traversing address list
343 * while we're modifying it.
345 crit_enter();
347 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
348 ifa = &ia->ia_ifa;
349 ifa_iflink(ifa, ifp, 1);
351 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
352 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
353 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
354 ia->ia_sockmask.sin_len = 8;
355 ia->ia_sockmask.sin_family = AF_INET;
356 if (ifp->if_flags & IFF_BROADCAST) {
357 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
358 ia->ia_broadaddr.sin_family = AF_INET;
360 ia->ia_ifp = ifp;
361 if (!(ifp->if_flags & IFF_LOOPBACK))
362 in_interfaces++;
363 iaIsNew = 1;
365 crit_exit();
367 break;
369 case SIOCSIFBRDADDR:
370 if (td && (error = suser(td)) != 0)
371 return error;
372 /* FALLTHROUGH */
374 case SIOCGIFADDR:
375 case SIOCGIFNETMASK:
376 case SIOCGIFDSTADDR:
377 case SIOCGIFBRDADDR:
378 if (ia == NULL)
379 return (EADDRNOTAVAIL);
380 break;
383 switch (cmd) {
384 case SIOCGIFADDR:
385 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
386 return (0);
388 case SIOCGIFBRDADDR:
389 if ((ifp->if_flags & IFF_BROADCAST) == 0)
390 return (EINVAL);
391 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
392 return (0);
394 case SIOCGIFDSTADDR:
395 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
396 return (EINVAL);
397 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
398 return (0);
400 case SIOCGIFNETMASK:
401 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
402 return (0);
404 case SIOCSIFDSTADDR:
405 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
406 return (EINVAL);
407 oldaddr = ia->ia_dstaddr;
408 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
409 if (ifp->if_ioctl != NULL) {
410 lwkt_serialize_enter(ifp->if_serializer);
411 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
412 td->td_proc->p_ucred);
413 lwkt_serialize_exit(ifp->if_serializer);
414 if (error) {
415 ia->ia_dstaddr = oldaddr;
416 return (error);
419 if (ia->ia_flags & IFA_ROUTE) {
420 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
421 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
422 ia->ia_ifa.ifa_dstaddr =
423 (struct sockaddr *)&ia->ia_dstaddr;
424 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
426 return (0);
428 case SIOCSIFBRDADDR:
429 if ((ifp->if_flags & IFF_BROADCAST) == 0)
430 return (EINVAL);
431 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
432 return (0);
434 case SIOCSIFADDR:
435 error = in_ifinit(ifp, ia,
436 (struct sockaddr_in *) &ifr->ifr_addr, 1);
437 if (error != 0 && iaIsNew)
438 break;
439 if (error == 0)
440 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
441 return (0);
443 case SIOCSIFNETMASK:
444 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
445 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
446 return (0);
448 case SIOCAIFADDR:
449 maskIsNew = 0;
450 hostIsNew = 1;
451 error = 0;
452 if (ia->ia_addr.sin_family == AF_INET) {
453 if (ifra->ifra_addr.sin_len == 0) {
454 ifra->ifra_addr = ia->ia_addr;
455 hostIsNew = 0;
456 } else if (ifra->ifra_addr.sin_addr.s_addr ==
457 ia->ia_addr.sin_addr.s_addr) {
458 hostIsNew = 0;
461 if (ifra->ifra_mask.sin_len) {
462 in_ifscrub(ifp, ia);
463 ia->ia_sockmask = ifra->ifra_mask;
464 ia->ia_sockmask.sin_family = AF_INET;
465 ia->ia_subnetmask =
466 ntohl(ia->ia_sockmask.sin_addr.s_addr);
467 maskIsNew = 1;
469 if ((ifp->if_flags & IFF_POINTOPOINT) &&
470 ifra->ifra_dstaddr.sin_family == AF_INET) {
471 in_ifscrub(ifp, ia);
472 ia->ia_dstaddr = ifra->ifra_dstaddr;
473 maskIsNew = 1; /* We lie; but the effect's the same */
475 if (ifra->ifra_addr.sin_family == AF_INET &&
476 (hostIsNew || maskIsNew))
477 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
479 if (error != 0 && iaIsNew)
480 break;
482 if ((ifp->if_flags & IFF_BROADCAST) &&
483 ifra->ifra_broadaddr.sin_family == AF_INET)
484 ia->ia_broadaddr = ifra->ifra_broadaddr;
485 if (error == 0)
486 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
487 return (error);
489 case SIOCDIFADDR:
491 * in_ifscrub kills the interface route.
493 in_ifscrub(ifp, ia);
495 * in_ifadown gets rid of all the rest of
496 * the routes. This is not quite the right
497 * thing to do, but at least if we are running
498 * a routing process they will come back.
500 in_ifadown(&ia->ia_ifa, 1);
501 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
502 error = 0;
503 break;
505 default:
506 if (ifp == NULL || ifp->if_ioctl == NULL)
507 return (EOPNOTSUPP);
508 lwkt_serialize_enter(ifp->if_serializer);
509 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
510 lwkt_serialize_exit(ifp->if_serializer);
511 return (error);
514 ifa_ifunlink(&ia->ia_ifa, ifp);
517 * Protect from NETISR_IP traversing address list while we're modifying
518 * it.
520 crit_enter(); /* XXX MP */
521 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
522 LIST_REMOVE(ia, ia_hash);
523 crit_exit(); /* XXX MP */
525 ifa_destroy(&ia->ia_ifa);
527 return (error);
531 * SIOC[GAD]LIFADDR.
532 * SIOCGLIFADDR: get first address. (?!?)
533 * SIOCGLIFADDR with IFLR_PREFIX:
534 * get first address that matches the specified prefix.
535 * SIOCALIFADDR: add the specified address.
536 * SIOCALIFADDR with IFLR_PREFIX:
537 * EINVAL since we can't deduce hostid part of the address.
538 * SIOCDLIFADDR: delete the specified address.
539 * SIOCDLIFADDR with IFLR_PREFIX:
540 * delete the first address that matches the specified prefix.
541 * return values:
542 * EINVAL on invalid parameters
543 * EADDRNOTAVAIL on prefix match failed/specified address not found
544 * other values may be returned from in_ioctl()
546 * NOTE! td might be NULL.
548 static int
549 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
550 struct thread *td)
552 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
554 /* sanity checks */
555 if (!data || !ifp) {
556 panic("invalid argument to in_lifaddr_ioctl");
557 /*NOTRECHED*/
560 switch (cmd) {
561 case SIOCGLIFADDR:
562 /* address must be specified on GET with IFLR_PREFIX */
563 if ((iflr->flags & IFLR_PREFIX) == 0)
564 break;
565 /*FALLTHROUGH*/
566 case SIOCALIFADDR:
567 case SIOCDLIFADDR:
568 /* address must be specified on ADD and DELETE */
569 if (iflr->addr.ss_family != AF_INET)
570 return EINVAL;
571 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
572 return EINVAL;
573 /* XXX need improvement */
574 if (iflr->dstaddr.ss_family
575 && iflr->dstaddr.ss_family != AF_INET)
576 return EINVAL;
577 if (iflr->dstaddr.ss_family
578 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
579 return EINVAL;
580 break;
581 default: /*shouldn't happen*/
582 return EOPNOTSUPP;
584 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
585 return EINVAL;
587 switch (cmd) {
588 case SIOCALIFADDR:
590 struct in_aliasreq ifra;
592 if (iflr->flags & IFLR_PREFIX)
593 return EINVAL;
595 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
596 bzero(&ifra, sizeof ifra);
597 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
599 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
601 if (iflr->dstaddr.ss_family) { /*XXX*/
602 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
603 iflr->dstaddr.ss_len);
606 ifra.ifra_mask.sin_family = AF_INET;
607 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
608 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
610 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
612 case SIOCGLIFADDR:
613 case SIOCDLIFADDR:
615 struct ifaddr_container *ifac;
616 struct in_ifaddr *ia;
617 struct in_addr mask, candidate, match;
618 struct sockaddr_in *sin;
619 int cmp;
621 bzero(&mask, sizeof mask);
622 if (iflr->flags & IFLR_PREFIX) {
623 /* lookup a prefix rather than address. */
624 in_len2mask(&mask, iflr->prefixlen);
626 sin = (struct sockaddr_in *)&iflr->addr;
627 match.s_addr = sin->sin_addr.s_addr;
628 match.s_addr &= mask.s_addr;
630 /* if you set extra bits, that's wrong */
631 if (match.s_addr != sin->sin_addr.s_addr)
632 return EINVAL;
634 cmp = 1;
635 } else {
636 if (cmd == SIOCGLIFADDR) {
637 /* on getting an address, take the 1st match */
638 match.s_addr = 0; /* gcc4 warning */
639 cmp = 0; /*XXX*/
640 } else {
641 /* on deleting an address, do exact match */
642 in_len2mask(&mask, 32);
643 sin = (struct sockaddr_in *)&iflr->addr;
644 match.s_addr = sin->sin_addr.s_addr;
646 cmp = 1;
650 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
651 struct ifaddr *ifa = ifac->ifa;
653 if (ifa->ifa_addr->sa_family != AF_INET6)
654 continue;
655 if (!cmp)
656 break;
657 candidate.s_addr =
658 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
659 candidate.s_addr &= mask.s_addr;
660 if (candidate.s_addr == match.s_addr)
661 break;
663 if (ifac == NULL)
664 return EADDRNOTAVAIL;
665 ia = (struct in_ifaddr *)(ifac->ifa);
667 if (cmd == SIOCGLIFADDR) {
668 /* fill in the if_laddrreq structure */
669 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
671 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
672 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
673 ia->ia_dstaddr.sin_len);
674 } else
675 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
677 iflr->prefixlen =
678 in_mask2len(&ia->ia_sockmask.sin_addr);
680 iflr->flags = 0; /*XXX*/
682 return 0;
683 } else {
684 struct in_aliasreq ifra;
686 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
687 bzero(&ifra, sizeof ifra);
688 bcopy(iflr->iflr_name, ifra.ifra_name,
689 sizeof ifra.ifra_name);
691 bcopy(&ia->ia_addr, &ifra.ifra_addr,
692 ia->ia_addr.sin_len);
693 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
694 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
695 ia->ia_dstaddr.sin_len);
697 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
698 ia->ia_sockmask.sin_len);
700 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
701 ifp, td);
706 return EOPNOTSUPP; /*just for safety*/
710 * Delete any existing route for an interface.
712 void
713 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
716 if ((ia->ia_flags & IFA_ROUTE) == 0)
717 return;
718 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
719 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
720 else
721 rtinit(&ia->ia_ifa, RTM_DELETE, 0);
722 ia->ia_flags &= ~IFA_ROUTE;
726 * Initialize an interface's internet address
727 * and routing table entry.
729 static int
730 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, int scrub)
732 u_long i = ntohl(sin->sin_addr.s_addr);
733 struct sockaddr_in oldaddr;
734 int flags = RTF_UP, error = 0;
736 lwkt_serialize_enter(ifp->if_serializer);
738 oldaddr = ia->ia_addr;
739 if (oldaddr.sin_family == AF_INET)
740 LIST_REMOVE(ia, ia_hash);
741 ia->ia_addr = *sin;
742 if (ia->ia_addr.sin_family == AF_INET)
743 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
744 ia, ia_hash);
746 * Give the interface a chance to initialize
747 * if this is its first address,
748 * and to validate the address if necessary.
750 if (ifp->if_ioctl &&
751 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL))) {
752 lwkt_serialize_exit(ifp->if_serializer);
753 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
754 ia->ia_addr = oldaddr;
755 if (ia->ia_addr.sin_family == AF_INET)
756 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
757 ia, ia_hash);
758 return (error);
760 lwkt_serialize_exit(ifp->if_serializer);
761 if (scrub) {
762 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
763 in_ifscrub(ifp, ia);
764 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
766 if (IN_CLASSA(i))
767 ia->ia_netmask = IN_CLASSA_NET;
768 else if (IN_CLASSB(i))
769 ia->ia_netmask = IN_CLASSB_NET;
770 else
771 ia->ia_netmask = IN_CLASSC_NET;
773 * The subnet mask usually includes at least the standard network part,
774 * but may may be smaller in the case of supernetting.
775 * If it is set, we believe it.
777 if (ia->ia_subnetmask == 0) {
778 ia->ia_subnetmask = ia->ia_netmask;
779 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
780 } else
781 ia->ia_netmask &= ia->ia_subnetmask;
782 ia->ia_net = i & ia->ia_netmask;
783 ia->ia_subnet = i & ia->ia_subnetmask;
784 in_socktrim(&ia->ia_sockmask);
786 * Add route for the network.
788 ia->ia_ifa.ifa_metric = ifp->if_metric;
789 if (ifp->if_flags & IFF_BROADCAST) {
790 ia->ia_broadaddr.sin_addr.s_addr =
791 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
792 ia->ia_netbroadcast.s_addr =
793 htonl(ia->ia_net | ~ ia->ia_netmask);
794 } else if (ifp->if_flags & IFF_LOOPBACK) {
795 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
796 flags |= RTF_HOST;
797 } else if (ifp->if_flags & IFF_POINTOPOINT) {
798 if (ia->ia_dstaddr.sin_family != AF_INET)
799 return (0);
800 flags |= RTF_HOST;
804 * Don't add host routes for interface addresses of
805 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
806 * possible to assign several such address pairs with consistent
807 * results (no host route) and is required by BOOTP.
809 * XXX: This is ugly ! There should be a way for the caller to
810 * say that they don't want a host route.
812 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
813 ia->ia_netmask != IN_CLASSA_NET ||
814 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
815 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
816 ia->ia_addr = oldaddr;
817 return (error);
819 ia->ia_flags |= IFA_ROUTE;
823 * If the interface supports multicast, join the "all hosts"
824 * multicast group on that interface.
826 if (ifp->if_flags & IFF_MULTICAST) {
827 struct in_addr addr;
829 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
830 in_addmulti(&addr, ifp);
832 return (error);
837 * Return 1 if the address might be a local broadcast address.
840 in_broadcast(struct in_addr in, struct ifnet *ifp)
842 struct ifaddr_container *ifac;
843 u_long t;
845 if (in.s_addr == INADDR_BROADCAST ||
846 in.s_addr == INADDR_ANY)
847 return 1;
848 if ((ifp->if_flags & IFF_BROADCAST) == 0)
849 return 0;
850 t = ntohl(in.s_addr);
852 * Look through the list of addresses for a match
853 * with a broadcast address.
855 #define ia ((struct in_ifaddr *)ifa)
856 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
857 struct ifaddr *ifa = ifac->ifa;
859 if (ifa->ifa_addr->sa_family == AF_INET &&
860 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
861 in.s_addr == ia->ia_netbroadcast.s_addr ||
863 * Check for old-style (host 0) broadcast.
865 t == ia->ia_subnet || t == ia->ia_net) &&
867 * Check for an all one subnetmask. These
868 * only exist when an interface gets a secondary
869 * address.
871 ia->ia_subnetmask != (u_long)0xffffffff)
872 return 1;
874 return (0);
875 #undef ia
878 * Add an address to the list of IP multicast addresses for a given interface.
880 struct in_multi *
881 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
883 struct in_multi *inm;
884 int error;
885 struct sockaddr_in sin;
886 struct ifmultiaddr *ifma;
889 * Call generic routine to add membership or increment
890 * refcount. It wants addresses in the form of a sockaddr,
891 * so we build one here (being careful to zero the unused bytes).
893 bzero(&sin, sizeof sin);
894 sin.sin_family = AF_INET;
895 sin.sin_len = sizeof sin;
896 sin.sin_addr = *ap;
897 crit_enter();
898 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
899 if (error) {
900 crit_exit();
901 return 0;
905 * If ifma->ifma_protospec is null, then if_addmulti() created
906 * a new record. Otherwise, we are done.
908 if (ifma->ifma_protospec != 0) {
909 crit_exit();
910 return ifma->ifma_protospec;
913 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
914 at interrupt time? If so, need to fix if_addmulti. XXX */
915 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
916 inm->inm_addr = *ap;
917 inm->inm_ifp = ifp;
918 inm->inm_ifma = ifma;
919 ifma->ifma_protospec = inm;
920 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
923 * Let IGMP know that we have joined a new IP multicast group.
925 igmp_joingroup(inm);
926 crit_exit();
927 return (inm);
931 * Delete a multicast address record.
933 void
934 in_delmulti(struct in_multi *inm)
936 struct ifmultiaddr *ifma;
937 struct in_multi my_inm;
939 crit_enter();
940 ifma = inm->inm_ifma;
941 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
942 if (ifma->ifma_refcount == 1) {
944 * No remaining claims to this record; let IGMP know that
945 * we are leaving the multicast group.
946 * But do it after the if_delmulti() which might reset
947 * the interface and nuke the packet.
949 my_inm = *inm ;
950 ifma->ifma_protospec = 0;
951 LIST_REMOVE(inm, inm_link);
952 kfree(inm, M_IPMADDR);
954 /* XXX - should be separate API for when we have an ifma? */
955 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
956 if (my_inm.inm_ifp != NULL)
957 igmp_leavegroup(&my_inm);
958 crit_exit();
961 void
962 in_ifdetach(struct ifnet *ifp)
964 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
965 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);