drm/i915/gem: Switch to the Linux scatterlist API
[dragonfly.git] / sys / netinet / in.c
bloba2f5f5c3fb3bd11d6242a0571449b03bb6b46568
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)in.c 8.4 (Berkeley) 1/9/95
30 * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
33 #include "opt_bootp.h"
34 #include "opt_carp.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41 #include <sys/priv.h>
42 #include <sys/msgport.h>
43 #include <sys/socket.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
49 #include <net/if.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
52 #include <net/netmsg2.h>
53 #include <net/netisr2.h>
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/udp_var.h>
60 #include <netinet/igmp_var.h>
62 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
64 static int in_mask2len (struct in_addr *);
65 static void in_len2mask (struct in_addr *, int);
66 static int in_lifaddr_ioctl (u_long, caddr_t, struct ifnet *, struct thread *);
68 static void in_socktrim (struct sockaddr_in *);
69 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
70 const struct sockaddr_in *, int);
72 static int in_control_internal(u_long, caddr_t, struct ifnet *,
73 struct thread *);
75 static int in_addprefix(struct in_ifaddr *, int);
76 static void in_scrubprefix(struct in_ifaddr *);
78 static int subnetsarelocal = 0;
79 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
80 &subnetsarelocal, 0,
81 "Count all internet addresses of subnets of the local net as local");
83 struct in_multihead in_multihead; /* XXX BSS initialization */
85 extern struct inpcbinfo ripcbinfo;
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 void
192 in_control_dispatch(netmsg_t msg)
194 int error;
196 error = in_control(msg->control.nm_cmd, msg->control.nm_data,
197 msg->control.nm_ifp, msg->control.nm_td);
198 lwkt_replymsg(&msg->lmsg, error);
201 static void
202 in_control_internal_dispatch(netmsg_t msg)
204 int error;
206 error = in_control_internal(msg->control.nm_cmd,
207 msg->control.nm_data,
208 msg->control.nm_ifp,
209 msg->control.nm_td);
210 lwkt_replymsg(&msg->lmsg, error);
214 * Generic internet control operations (ioctl's).
215 * Ifp is 0 if not an interface-specific ioctl.
217 * NOTE! td might be NULL.
220 in_control(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
222 struct netmsg_pru_control msg;
224 switch (cmd) {
225 /* change address */
226 case SIOCALIFADDR:
227 case SIOCDLIFADDR:
228 case SIOCSIFDSTADDR:
229 case SIOCSIFBRDADDR:
230 case SIOCSIFADDR:
231 case SIOCSIFNETMASK:
232 case SIOCAIFADDR:
233 case SIOCDIFADDR:
235 * Dispatch these SIOCs to netisr0.
237 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
238 in_control_internal_dispatch);
239 msg.nm_cmd = cmd;
240 msg.nm_data = data;
241 msg.nm_ifp = ifp;
242 msg.nm_td = td;
243 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
244 return msg.base.lmsg.ms_error;
246 default:
247 return in_control_internal(cmd, data, ifp, td);
251 static void
252 in_ialink_dispatch(netmsg_t msg)
254 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
255 struct ifaddr_container *ifac;
256 struct in_ifaddr_container *iac;
257 int cpu = mycpuid;
259 crit_enter();
261 ifac = &ia->ia_ifa.ifa_containers[cpu];
262 ASSERT_IFAC_VALID(ifac);
263 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
264 ("ia is on in_ifaddrheads"));
266 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
267 iac = &ifac->ifa_proto_u.u_in_ifac;
268 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
270 crit_exit();
272 ifa_forwardmsg(&msg->lmsg, cpu + 1);
275 static void
276 in_iaunlink_dispatch(netmsg_t msg)
278 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
279 struct ifaddr_container *ifac;
280 struct in_ifaddr_container *iac;
281 int cpu = mycpuid;
283 crit_enter();
285 ifac = &ia->ia_ifa.ifa_containers[cpu];
286 ASSERT_IFAC_VALID(ifac);
287 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
288 ("ia is not on in_ifaddrheads"));
290 iac = &ifac->ifa_proto_u.u_in_ifac;
291 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
292 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
294 crit_exit();
296 ifa_forwardmsg(&msg->lmsg, cpu + 1);
299 static void
300 in_iahashins_dispatch(netmsg_t msg)
302 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
303 struct ifaddr_container *ifac;
304 struct in_ifaddr_container *iac;
305 int cpu = mycpuid;
307 crit_enter();
309 ifac = &ia->ia_ifa.ifa_containers[cpu];
310 ASSERT_IFAC_VALID(ifac);
311 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
312 ("ia is on in_ifaddrhashtbls"));
314 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
315 iac = &ifac->ifa_proto_u.u_in_ifac;
316 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
317 iac, ia_hash);
319 crit_exit();
321 ifa_forwardmsg(&msg->lmsg, cpu + 1);
324 static void
325 in_iahashrem_dispatch(netmsg_t msg)
327 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
328 struct ifaddr_container *ifac;
329 struct in_ifaddr_container *iac;
330 int cpu = mycpuid;
332 crit_enter();
334 ifac = &ia->ia_ifa.ifa_containers[cpu];
335 ASSERT_IFAC_VALID(ifac);
336 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
337 ("ia is not on in_ifaddrhashtbls"));
339 iac = &ifac->ifa_proto_u.u_in_ifac;
340 LIST_REMOVE(iac, ia_hash);
341 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
343 crit_exit();
345 ifa_forwardmsg(&msg->lmsg, cpu + 1);
348 static void
349 in_ialink(struct in_ifaddr *ia)
351 struct netmsg_base msg;
353 netmsg_init(&msg, NULL, &curthread->td_msgport,
354 0, in_ialink_dispatch);
355 msg.lmsg.u.ms_resultp = ia;
357 ifa_domsg(&msg.lmsg, 0);
360 void
361 in_iaunlink(struct in_ifaddr *ia)
363 struct netmsg_base msg;
365 netmsg_init(&msg, NULL, &curthread->td_msgport,
366 0, in_iaunlink_dispatch);
367 msg.lmsg.u.ms_resultp = ia;
369 ifa_domsg(&msg.lmsg, 0);
372 void
373 in_iahash_insert(struct in_ifaddr *ia)
375 struct netmsg_base msg;
377 netmsg_init(&msg, NULL, &curthread->td_msgport,
378 0, in_iahashins_dispatch);
379 msg.lmsg.u.ms_resultp = ia;
381 ifa_domsg(&msg.lmsg, 0);
384 void
385 in_iahash_remove(struct in_ifaddr *ia)
387 struct netmsg_base msg;
389 netmsg_init(&msg, NULL, &curthread->td_msgport,
390 0, in_iahashrem_dispatch);
391 msg.lmsg.u.ms_resultp = ia;
393 ifa_domsg(&msg.lmsg, 0);
396 static __inline struct in_ifaddr *
397 in_ianext(struct in_ifaddr *oia)
399 struct ifaddr_container *ifac;
400 struct in_ifaddr_container *iac;
402 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
403 ASSERT_IFAC_VALID(ifac);
404 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
405 ("ia is not on in_ifaddrheads"));
407 iac = &ifac->ifa_proto_u.u_in_ifac;
408 iac = TAILQ_NEXT(iac, ia_link);
409 if (iac != NULL)
410 return iac->ia;
411 else
412 return NULL;
415 static int
416 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
417 struct thread *td)
419 struct ifreq *ifr = (struct ifreq *)data;
420 struct in_ifaddr *ia = NULL;
421 struct in_addr dst;
422 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
423 struct ifaddr_container *ifac;
424 struct in_ifaddr_container *iac;
425 struct sockaddr_in oldaddr;
426 int hostIsNew, iaIsNew, maskIsNew, ifpWasUp;
427 int error = 0;
429 switch (cmd) {
430 case SIOCALIFADDR:
431 case SIOCDLIFADDR:
432 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
433 return error;
434 /* FALLTHROUGH */
435 case SIOCGLIFADDR:
436 if (!ifp)
437 return EINVAL;
438 return in_lifaddr_ioctl(cmd, data, ifp, td);
441 iaIsNew = 0;
442 ifpWasUp = 0;
445 * Find address for this interface, if it exists.
447 * If an alias address was specified, find that one instead of
448 * the first one on the interface, if possible
450 if (ifp) {
451 struct in_ifaddr *iap;
453 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
454 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
455 iap = iac->ia;
456 if (iap->ia_ifp == ifp &&
457 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
458 ia = iap;
459 break;
462 if (ia == NULL) {
463 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
464 ifa_link) {
465 iap = ifatoia(ifac->ifa);
466 if (iap->ia_addr.sin_family == AF_INET) {
467 ia = iap;
468 break;
473 if (ifp->if_flags & IFF_UP)
474 ifpWasUp = 1;
477 switch (cmd) {
478 case SIOCAIFADDR:
479 case SIOCDIFADDR:
480 if (ifp == NULL)
481 return (EADDRNOTAVAIL);
482 if (ifra->ifra_addr.sin_family == AF_INET) {
483 while (ia != NULL) {
484 if (ia->ia_ifp == ifp &&
485 ia->ia_addr.sin_addr.s_addr ==
486 ifra->ifra_addr.sin_addr.s_addr)
487 break;
488 ia = in_ianext(ia);
490 if ((ifp->if_flags & IFF_POINTOPOINT) &&
491 cmd == SIOCAIFADDR &&
492 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
493 return EDESTADDRREQ;
496 if (cmd == SIOCDIFADDR && ia == NULL)
497 return (EADDRNOTAVAIL);
498 /* FALLTHROUGH */
499 case SIOCSIFADDR:
500 case SIOCSIFNETMASK:
501 case SIOCSIFDSTADDR:
502 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
503 return error;
505 if (ifp == NULL)
506 return (EADDRNOTAVAIL);
508 if (cmd == SIOCSIFDSTADDR &&
509 (ifp->if_flags & IFF_POINTOPOINT) == 0)
510 return (EINVAL);
512 if (ia == NULL) {
513 struct ifaddr *ifa;
514 int i;
516 ia = ifa_create(sizeof(*ia));
517 ifa = &ia->ia_ifa;
520 * Setup per-CPU information
522 for (i = 0; i < ncpus; ++i) {
523 ifac = &ifa->ifa_containers[i];
524 iac = &ifac->ifa_proto_u.u_in_ifac;
525 iac->ia = ia;
526 iac->ia_ifac = ifac;
529 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
530 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
531 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
532 ia->ia_sockmask.sin_len = 8;
533 ia->ia_sockmask.sin_family = AF_INET;
534 if (ifp->if_flags & IFF_BROADCAST) {
535 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
536 ia->ia_broadaddr.sin_family = AF_INET;
538 ia->ia_ifp = ifp;
539 iaIsNew = 1;
541 in_ialink(ia);
542 ifa_iflink(ifa, ifp, 1);
544 break;
546 case SIOCSIFBRDADDR:
547 if (td && (error = priv_check(td, PRIV_ROOT)) != 0)
548 return error;
549 /* FALLTHROUGH */
551 case SIOCGIFADDR:
552 case SIOCGIFNETMASK:
553 case SIOCGIFDSTADDR:
554 case SIOCGIFBRDADDR:
555 if (ia == NULL)
556 return (EADDRNOTAVAIL);
557 break;
560 switch (cmd) {
561 case SIOCGIFADDR:
562 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
563 return (0);
565 case SIOCGIFBRDADDR:
566 if ((ifp->if_flags & IFF_BROADCAST) == 0)
567 return (EINVAL);
568 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
569 return (0);
571 case SIOCGIFDSTADDR:
572 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
573 return (EINVAL);
574 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
575 return (0);
577 case SIOCGIFNETMASK:
578 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
579 return (0);
581 case SIOCSIFDSTADDR:
582 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
584 oldaddr = ia->ia_dstaddr;
585 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
586 if (ifp->if_ioctl != NULL) {
587 ifnet_serialize_all(ifp);
588 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
589 td->td_proc->p_ucred);
590 ifnet_deserialize_all(ifp);
591 if (error) {
592 ia->ia_dstaddr = oldaddr;
593 return (error);
596 if (ia->ia_flags & IFA_ROUTE) {
597 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
598 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
599 ia->ia_ifa.ifa_dstaddr =
600 (struct sockaddr *)&ia->ia_dstaddr;
601 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
603 return (0);
605 case SIOCSIFBRDADDR:
606 if ((ifp->if_flags & IFF_BROADCAST) == 0)
607 return (EINVAL);
608 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
609 return (0);
611 case SIOCSIFADDR:
612 error = in_ifinit(ifp, ia,
613 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
614 if (error != 0 && iaIsNew)
615 break;
616 if (error == 0) {
617 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
618 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
619 &ia->ia_ifa);
621 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
623 * Interface is brought up by in_ifinit()
624 * (via ifp->if_ioctl). We act as if the
625 * interface got IFF_UP flag turned on.
627 if_up(ifp);
629 return (0);
631 case SIOCSIFNETMASK:
632 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
633 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
634 return (0);
636 case SIOCAIFADDR:
637 maskIsNew = 0;
638 hostIsNew = 1;
639 error = 0;
640 if (ia->ia_addr.sin_family == AF_INET) {
641 if (ifra->ifra_addr.sin_len == 0) {
642 ifra->ifra_addr = ia->ia_addr;
643 hostIsNew = 0;
644 } else if (ifra->ifra_addr.sin_addr.s_addr ==
645 ia->ia_addr.sin_addr.s_addr) {
646 hostIsNew = 0;
649 if (ifra->ifra_mask.sin_len) {
650 in_ifscrub(ifp, ia);
651 ia->ia_sockmask = ifra->ifra_mask;
652 ia->ia_sockmask.sin_family = AF_INET;
653 ia->ia_subnetmask =
654 ntohl(ia->ia_sockmask.sin_addr.s_addr);
655 maskIsNew = 1;
657 if ((ifp->if_flags & IFF_POINTOPOINT) &&
658 ifra->ifra_dstaddr.sin_family == AF_INET) {
659 in_ifscrub(ifp, ia);
660 ia->ia_dstaddr = ifra->ifra_dstaddr;
661 maskIsNew = 1; /* We lie; but the effect's the same */
663 if (ifra->ifra_addr.sin_family == AF_INET &&
664 (hostIsNew || maskIsNew))
665 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
667 if (error != 0 && iaIsNew)
668 break;
670 if ((ifp->if_flags & IFF_BROADCAST) &&
671 ifra->ifra_broadaddr.sin_family == AF_INET)
672 ia->ia_broadaddr = ifra->ifra_broadaddr;
673 if (error == 0) {
674 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
675 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
676 &ia->ia_ifa);
678 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
679 /* See the comment in SIOCSIFADDR */
680 if_up(ifp);
682 return (error);
684 case SIOCDIFADDR:
686 * in_ifscrub kills the interface route.
688 in_ifscrub(ifp, ia);
690 * in_ifadown gets rid of all the rest of
691 * the routes. This is not quite the right
692 * thing to do, but at least if we are running
693 * a routing process they will come back.
695 in_ifadown(&ia->ia_ifa, 1);
696 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
697 &ia->ia_ifa);
698 error = 0;
699 break;
701 default:
702 if (ifp == NULL || ifp->if_ioctl == NULL)
703 return (EOPNOTSUPP);
704 ifnet_serialize_all(ifp);
705 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
706 ifnet_deserialize_all(ifp);
707 return (error);
710 KKASSERT(cmd == SIOCDIFADDR ||
711 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
713 ifa_ifunlink(&ia->ia_ifa, ifp);
714 in_iaunlink(ia);
716 if (cmd == SIOCDIFADDR) {
717 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
718 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
719 in_iahash_remove(ia);
721 #ifdef INVARIANTS
722 else {
724 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
725 * already taken care of the deletion from hash table
727 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
728 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
729 ("SIOC%cIFADDR failed on new ia, "
730 "but the new ia is still in hash table",
731 cmd == SIOCSIFADDR ? 'S' : 'A'));
733 #endif
735 ifa_destroy(&ia->ia_ifa);
737 if ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) &&
738 !ifpWasUp && (ifp->if_flags & IFF_UP)) {
740 * Though the address assignment failed, the
741 * interface is brought up by in_ifinit()
742 * (via ifp->if_ioctl). With the hope that
743 * the interface has some valid addresses, we
744 * act as if IFF_UP flag was just set on the
745 * interface.
747 * NOTE:
748 * This could only be done after the failed
749 * address is unlinked from the global address
750 * list.
752 if_up(ifp);
755 return (error);
759 * SIOC[GAD]LIFADDR.
760 * SIOCGLIFADDR: get first address. (?!?)
761 * SIOCGLIFADDR with IFLR_PREFIX:
762 * get first address that matches the specified prefix.
763 * SIOCALIFADDR: add the specified address.
764 * SIOCALIFADDR with IFLR_PREFIX:
765 * EINVAL since we can't deduce hostid part of the address.
766 * SIOCDLIFADDR: delete the specified address.
767 * SIOCDLIFADDR with IFLR_PREFIX:
768 * delete the first address that matches the specified prefix.
769 * return values:
770 * EINVAL on invalid parameters
771 * EADDRNOTAVAIL on prefix match failed/specified address not found
772 * other values may be returned from in_ioctl()
774 * NOTE! td might be NULL.
776 static int
777 in_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
779 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
781 /* sanity checks */
782 if (!data || !ifp) {
783 panic("invalid argument to in_lifaddr_ioctl");
784 /*NOTRECHED*/
787 switch (cmd) {
788 case SIOCGLIFADDR:
789 /* address must be specified on GET with IFLR_PREFIX */
790 if ((iflr->flags & IFLR_PREFIX) == 0)
791 break;
792 /*FALLTHROUGH*/
793 case SIOCALIFADDR:
794 case SIOCDLIFADDR:
795 /* address must be specified on ADD and DELETE */
796 if (iflr->addr.ss_family != AF_INET)
797 return EINVAL;
798 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
799 return EINVAL;
800 /* XXX need improvement */
801 if (iflr->dstaddr.ss_family
802 && iflr->dstaddr.ss_family != AF_INET)
803 return EINVAL;
804 if (iflr->dstaddr.ss_family
805 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
806 return EINVAL;
807 break;
808 default: /*shouldn't happen*/
809 return EOPNOTSUPP;
811 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
812 return EINVAL;
814 switch (cmd) {
815 case SIOCALIFADDR:
817 struct in_aliasreq ifra;
819 if (iflr->flags & IFLR_PREFIX)
820 return EINVAL;
822 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
823 bzero(&ifra, sizeof ifra);
824 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
826 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
828 if (iflr->dstaddr.ss_family) { /*XXX*/
829 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
830 iflr->dstaddr.ss_len);
833 ifra.ifra_mask.sin_family = AF_INET;
834 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
835 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
837 return in_control_internal(SIOCAIFADDR, (caddr_t)&ifra, ifp,
838 td);
840 case SIOCGLIFADDR:
841 case SIOCDLIFADDR:
843 struct ifaddr_container *ifac;
844 struct in_ifaddr *ia;
845 struct in_addr mask, candidate, match;
846 struct sockaddr_in *sin;
847 int cmp;
849 bzero(&mask, sizeof mask);
850 if (iflr->flags & IFLR_PREFIX) {
851 /* lookup a prefix rather than address. */
852 in_len2mask(&mask, iflr->prefixlen);
854 sin = (struct sockaddr_in *)&iflr->addr;
855 match.s_addr = sin->sin_addr.s_addr;
856 match.s_addr &= mask.s_addr;
858 /* if you set extra bits, that's wrong */
859 if (match.s_addr != sin->sin_addr.s_addr)
860 return EINVAL;
862 cmp = 1;
863 } else {
864 if (cmd == SIOCGLIFADDR) {
865 /* on getting an address, take the 1st match */
866 match.s_addr = 0; /* gcc4 warning */
867 cmp = 0; /*XXX*/
868 } else {
869 /* on deleting an address, do exact match */
870 in_len2mask(&mask, 32);
871 sin = (struct sockaddr_in *)&iflr->addr;
872 match.s_addr = sin->sin_addr.s_addr;
874 cmp = 1;
878 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
879 struct ifaddr *ifa = ifac->ifa;
881 if (ifa->ifa_addr->sa_family != AF_INET6)
882 continue;
883 if (!cmp)
884 break;
885 candidate.s_addr =
886 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
887 candidate.s_addr &= mask.s_addr;
888 if (candidate.s_addr == match.s_addr)
889 break;
891 if (ifac == NULL)
892 return EADDRNOTAVAIL;
893 ia = (struct in_ifaddr *)(ifac->ifa);
895 if (cmd == SIOCGLIFADDR) {
896 /* fill in the if_laddrreq structure */
897 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
899 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
900 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
901 ia->ia_dstaddr.sin_len);
902 } else
903 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
905 iflr->prefixlen =
906 in_mask2len(&ia->ia_sockmask.sin_addr);
908 iflr->flags = 0; /*XXX*/
910 return 0;
911 } else {
912 struct in_aliasreq ifra;
914 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
915 bzero(&ifra, sizeof ifra);
916 bcopy(iflr->iflr_name, ifra.ifra_name,
917 sizeof ifra.ifra_name);
919 bcopy(&ia->ia_addr, &ifra.ifra_addr,
920 ia->ia_addr.sin_len);
921 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
922 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
923 ia->ia_dstaddr.sin_len);
925 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
926 ia->ia_sockmask.sin_len);
928 return in_control_internal(SIOCDIFADDR, (caddr_t)&ifra,
929 ifp, td);
934 return EOPNOTSUPP; /*just for safety*/
938 * Delete any existing route for an interface.
940 void
941 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
943 in_scrubprefix(ia);
947 * Initialize an interface's internet address
948 * and routing table entry.
950 static int
951 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
952 const struct sockaddr_in *sin, int scrub)
954 u_long i = ntohl(sin->sin_addr.s_addr);
955 struct sockaddr_in oldaddr;
956 struct ifaddr_container *ifac;
957 int flags = RTF_UP, error = 0;
958 int was_hash = 0;
960 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
961 oldaddr = ia->ia_addr;
963 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
964 was_hash = 1;
965 in_iahash_remove(ia);
968 ia->ia_addr = *sin;
969 if (ia->ia_addr.sin_family == AF_INET)
970 in_iahash_insert(ia);
973 * Give the interface a chance to initialize
974 * if this is its first address,
975 * and to validate the address if necessary.
977 if (ifp->if_ioctl != NULL) {
978 ifnet_serialize_all(ifp);
979 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
980 ifnet_deserialize_all(ifp);
981 if (error)
982 goto fail;
986 * Delete old route, if requested.
988 if (scrub) {
989 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
990 in_ifscrub(ifp, ia);
991 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
995 * Calculate netmask/subnetmask.
997 if (IN_CLASSA(i))
998 ia->ia_netmask = IN_CLASSA_NET;
999 else if (IN_CLASSB(i))
1000 ia->ia_netmask = IN_CLASSB_NET;
1001 else
1002 ia->ia_netmask = IN_CLASSC_NET;
1004 * The subnet mask usually includes at least the standard network part,
1005 * but may may be smaller in the case of supernetting.
1006 * If it is set, we believe it.
1008 if (ia->ia_subnetmask == 0) {
1009 ia->ia_subnetmask = ia->ia_netmask;
1010 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1011 } else {
1012 ia->ia_netmask &= ia->ia_subnetmask;
1014 ia->ia_net = i & ia->ia_netmask;
1015 ia->ia_subnet = i & ia->ia_subnetmask;
1016 in_socktrim(&ia->ia_sockmask);
1019 * Add route for the network.
1021 ia->ia_ifa.ifa_metric = ifp->if_metric;
1022 if (ifp->if_flags & IFF_BROADCAST) {
1023 ia->ia_broadaddr.sin_addr.s_addr =
1024 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1025 ia->ia_netbroadcast.s_addr =
1026 htonl(ia->ia_net | ~ ia->ia_netmask);
1027 } else if (ifp->if_flags & IFF_LOOPBACK) {
1028 ia->ia_dstaddr = ia->ia_addr;
1029 flags |= RTF_HOST;
1030 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1031 if (ia->ia_dstaddr.sin_family != AF_INET)
1032 return (0);
1033 flags |= RTF_HOST;
1037 * Don't add host routes for interface addresses of
1038 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
1039 * possible to assign several such address pairs with consistent
1040 * results (no host route) and is required by BOOTP.
1042 * XXX: This is ugly ! There should be a way for the caller to
1043 * say that they don't want a host route.
1045 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1046 ia->ia_netmask != IN_CLASSA_NET ||
1047 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1048 error = in_addprefix(ia, flags);
1049 if (error)
1050 goto fail;
1054 * If the interface supports multicast, join the "all hosts"
1055 * multicast group on that interface.
1057 if (ifp->if_flags & IFF_MULTICAST) {
1058 struct in_addr addr;
1060 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1061 in_addmulti(&addr, ifp);
1063 return (0);
1064 fail:
1065 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1066 in_iahash_remove(ia);
1068 ia->ia_addr = oldaddr;
1069 if (was_hash)
1070 in_iahash_insert(ia);
1071 return (error);
1074 #define rtinitflags(x) \
1075 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
1076 ? RTF_HOST : 0)
1079 * Add a route to prefix ("connected route" in cisco terminology).
1080 * Do nothing, if there are some interface addresses with the same
1081 * prefix already. This function assumes that the 'target' parent
1082 * interface is UP.
1084 static int
1085 in_addprefix(struct in_ifaddr *target, int flags)
1087 struct in_ifaddr_container *iac;
1088 struct in_addr prefix, mask;
1089 int error;
1091 #ifdef CARP
1093 * Don't add prefix routes for CARP interfaces.
1094 * Prefix routes creation is handled by CARP
1095 * interfaces themselves.
1097 if (target->ia_ifp->if_type == IFT_CARP)
1098 return 0;
1099 #endif
1101 mask = target->ia_sockmask.sin_addr;
1102 if (flags & RTF_HOST) {
1103 prefix = target->ia_dstaddr.sin_addr;
1104 } else {
1105 prefix = target->ia_addr.sin_addr;
1106 prefix.s_addr &= mask.s_addr;
1109 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1110 struct in_ifaddr *ia = iac->ia;
1111 struct in_addr p;
1113 /* Don't test against self */
1114 if (ia == target)
1115 continue;
1117 /* The tested address does not own a route entry */
1118 if ((ia->ia_flags & IFA_ROUTE) == 0)
1119 continue;
1121 /* Prefix test */
1122 if (rtinitflags(ia)) {
1123 p = ia->ia_dstaddr.sin_addr;
1124 } else {
1125 p = ia->ia_addr.sin_addr;
1126 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1128 if (prefix.s_addr != p.s_addr)
1129 continue;
1132 * If the to-be-added address and the curretly being
1133 * tested address are not host addresses, we need to
1134 * take subnetmask into consideration.
1136 if (!(flags & RTF_HOST) && !rtinitflags(ia) &&
1137 mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1138 continue;
1141 * If we got a matching prefix route inserted by other
1142 * interface address, we don't need to bother.
1144 return 0;
1148 * No one seem to have prefix route; insert it.
1150 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1151 if (!error)
1152 target->ia_flags |= IFA_ROUTE;
1153 return error;
1157 * Remove a route to prefix ("connected route" in cisco terminology).
1158 * Re-installs the route by using another interface address, if there's
1159 * one with the same prefix (otherwise we lose the route mistakenly).
1161 static void
1162 in_scrubprefix(struct in_ifaddr *target)
1164 struct in_ifaddr_container *iac;
1165 struct in_addr prefix, mask;
1166 int error;
1168 #ifdef CARP
1170 * Don't scrub prefix routes for CARP interfaces.
1171 * Prefix routes deletion is handled by CARP
1172 * interfaces themselves.
1174 if (target->ia_ifp->if_type == IFT_CARP)
1175 return;
1176 #endif
1178 if ((target->ia_flags & IFA_ROUTE) == 0)
1179 return;
1181 mask = target->ia_sockmask.sin_addr;
1182 if (rtinitflags(target)) {
1183 prefix = target->ia_dstaddr.sin_addr;
1184 } else {
1185 prefix = target->ia_addr.sin_addr;
1186 prefix.s_addr &= mask.s_addr;
1189 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1190 struct in_ifaddr *ia = iac->ia;
1191 struct in_addr p;
1193 /* Don't test against self */
1194 if (ia == target)
1195 continue;
1197 /* The tested address already owns a route entry */
1198 if (ia->ia_flags & IFA_ROUTE)
1199 continue;
1202 * The prefix route of the tested address should
1203 * never be installed if its parent interface is
1204 * not UP yet.
1206 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1207 continue;
1209 #ifdef CARP
1211 * Don't add prefix routes for CARP interfaces.
1212 * Prefix routes creation is handled by CARP
1213 * interfaces themselves.
1215 if (ia->ia_ifp->if_type == IFT_CARP)
1216 continue;
1217 #endif
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 == NULL || (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
1300 * Add an address to the list of IP multicast addresses for a given interface.
1302 struct in_multi *
1303 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1305 struct in_multi *inm;
1306 int error;
1307 struct sockaddr_in sin;
1308 struct ifmultiaddr *ifma;
1310 ASSERT_IN_NETISR(0);
1313 * Call generic routine to add membership or increment
1314 * refcount. It wants addresses in the form of a sockaddr,
1315 * so we build one here (being careful to zero the unused bytes).
1317 bzero(&sin, sizeof sin);
1318 sin.sin_family = AF_INET;
1319 sin.sin_len = sizeof sin;
1320 sin.sin_addr = *ap;
1321 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1322 if (error)
1323 return NULL;
1326 * If ifma->ifma_protospec is null, then if_addmulti() created
1327 * a new record. Otherwise, we are done.
1329 if (ifma->ifma_protospec != NULL)
1330 return ifma->ifma_protospec;
1332 inm = kmalloc(sizeof *inm, M_IPMADDR, M_INTWAIT | M_ZERO);
1333 inm->inm_addr = *ap;
1334 inm->inm_ifp = ifp;
1335 inm->inm_ifma = ifma;
1336 ifma->ifma_protospec = inm;
1337 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1340 * Let IGMP know that we have joined a new IP multicast group.
1342 igmp_joingroup(inm);
1343 return inm;
1347 * Delete a multicast address record.
1349 void
1350 in_delmulti(struct in_multi *inm)
1352 struct ifmultiaddr *ifma;
1353 struct in_multi my_inm;
1355 ASSERT_IN_NETISR(0);
1357 ifma = inm->inm_ifma;
1358 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1359 if (ifma->ifma_refcount == 1) {
1361 * No remaining claims to this record; let IGMP know that
1362 * we are leaving the multicast group.
1363 * But do it after the if_delmulti() which might reset
1364 * the interface and nuke the packet.
1366 my_inm = *inm ;
1367 ifma->ifma_protospec = NULL;
1368 LIST_REMOVE(inm, inm_link);
1369 kfree(inm, M_IPMADDR);
1371 /* XXX - should be separate API for when we have an ifma? */
1372 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1373 if (my_inm.inm_ifp != NULL)
1374 igmp_leavegroup(&my_inm);
1377 static void
1378 in_ifdetach_dispatch(netmsg_t nmsg)
1380 struct lwkt_msg *lmsg = &nmsg->lmsg;
1381 struct ifnet *ifp = lmsg->u.ms_resultp;
1382 int cpu;
1384 in_pcbpurgeif0(&ripcbinfo, ifp);
1385 for (cpu = 0; cpu < ncpus2; ++cpu)
1386 in_pcbpurgeif0(&udbinfo[cpu], ifp);
1388 lwkt_replymsg(lmsg, 0);
1391 void
1392 in_ifdetach(struct ifnet *ifp)
1394 struct netmsg_base nmsg;
1395 struct lwkt_msg *lmsg = &nmsg.lmsg;
1397 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
1398 in_ifdetach_dispatch);
1399 lmsg->u.ms_resultp = ifp;
1401 lwkt_domsg(netisr_cpuport(0), lmsg, 0);