Parallelize in_ifaddrhead operation
[dragonfly.git] / sys / netinet / in.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)in.c 8.4 (Berkeley) 1/9/95
34 * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
1b562c24 35 * $DragonFly: src/sys/netinet/in.c,v 1.37 2008/06/08 08:38:05 sephe Exp $
984263bc
MD
36 */
37
38#include "opt_bootp.h"
b2632176 39
984263bc
MD
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>
b2632176 45#include <sys/msgport.h>
984263bc 46#include <sys/socket.h>
b2632176 47
984263bc
MD
48#include <sys/kernel.h>
49#include <sys/sysctl.h>
1cae611f 50#include <sys/thread2.h>
984263bc
MD
51
52#include <net/if.h>
53#include <net/if_types.h>
54#include <net/route.h>
b2632176 55#include <net/netmsg2.h>
984263bc
MD
56
57#include <netinet/in.h>
58#include <netinet/in_var.h>
59#include <netinet/in_pcb.h>
60
61#include <netinet/igmp_var.h>
62
c66faf37 63MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
984263bc 64
42a7fc75
RG
65static int in_mask2len (struct in_addr *);
66static void in_len2mask (struct in_addr *, int);
67static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
68 struct ifnet *, struct thread *);
984263bc 69
42a7fc75 70static void in_socktrim (struct sockaddr_in *);
44a87e42
SZ
71static int in_ifinit(struct ifnet *, struct in_ifaddr *,
72 const struct sockaddr_in *, int);
984263bc 73
b2632176
SZ
74static void in_control_dispatch(struct netmsg *);
75static int in_control_internal(u_long, caddr_t, struct ifnet *,
76 struct thread *);
77
984263bc 78static int subnetsarelocal = 0;
f23061d4 79SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
984263bc
MD
80 &subnetsarelocal, 0, "");
81
82struct in_multihead in_multihead; /* XXX BSS initialization */
83
84extern struct inpcbinfo ripcbinfo;
85extern struct inpcbinfo udbinfo;
86
87/*
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.
92 */
93int
f3f70f0d 94in_localaddr(struct in_addr in)
984263bc 95{
2256ba69 96 u_long i = ntohl(in.s_addr);
1b562c24 97 struct in_ifaddr_container *iac;
2256ba69 98 struct in_ifaddr *ia;
984263bc
MD
99
100 if (subnetsarelocal) {
1b562c24
SZ
101 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
102 ia = iac->ia;
103
984263bc
MD
104 if ((i & ia->ia_netmask) == ia->ia_net)
105 return (1);
1b562c24 106 }
984263bc 107 } else {
1b562c24
SZ
108 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
109 ia = iac->ia;
110
984263bc
MD
111 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
112 return (1);
1b562c24 113 }
984263bc
MD
114 }
115 return (0);
116}
117
118/*
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.
122 */
123int
f3f70f0d 124in_canforward(struct in_addr in)
984263bc 125{
2256ba69
RG
126 u_long i = ntohl(in.s_addr);
127 u_long net;
984263bc
MD
128
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);
135 }
136 return (1);
137}
138
139/*
140 * Trim a mask in a sockaddr
141 */
142static void
f3f70f0d 143in_socktrim(struct sockaddr_in *ap)
984263bc 144{
2256ba69
RG
145 char *cplim = (char *) &ap->sin_addr;
146 char *cp = (char *) (&ap->sin_addr + 1);
984263bc
MD
147
148 ap->sin_len = 0;
149 while (--cp >= cplim)
f23061d4 150 if (*cp) {
984263bc
MD
151 (ap)->sin_len = cp - (char *) (ap) + 1;
152 break;
153 }
154}
155
156static int
f3f70f0d 157in_mask2len(struct in_addr *mask)
984263bc
MD
158{
159 int x, y;
160 u_char *p;
161
162 p = (u_char *)mask;
0c3c561c 163 for (x = 0; x < sizeof *mask; x++) {
984263bc
MD
164 if (p[x] != 0xff)
165 break;
166 }
167 y = 0;
0c3c561c 168 if (x < sizeof *mask) {
984263bc
MD
169 for (y = 0; y < 8; y++) {
170 if ((p[x] & (0x80 >> y)) == 0)
171 break;
172 }
173 }
174 return x * 8 + y;
175}
176
177static void
f3f70f0d 178in_len2mask(struct in_addr *mask, int len)
984263bc
MD
179{
180 int i;
181 u_char *p;
182
183 p = (u_char *)mask;
0c3c561c 184 bzero(mask, sizeof *mask);
984263bc
MD
185 for (i = 0; i < len / 8; i++)
186 p[i] = 0xff;
187 if (len % 8)
188 p[i] = (0xff00 >> (len % 8)) & 0xff;
189}
190
191static int in_interfaces; /* number of external internet interfaces */
192
b2632176
SZ
193struct in_control_arg {
194 u_long cmd;
195 caddr_t data;
196 struct ifnet *ifp;
197 struct thread *td;
198};
199
200static void
201in_control_dispatch(struct netmsg *nmsg)
202{
203 struct lwkt_msg *msg = &nmsg->nm_lmsg;
204 const struct in_control_arg *arg = msg->u.ms_resultp;
205 int error;
206
207 error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td);
208 lwkt_replymsg(msg, error);
209}
210
984263bc
MD
211/*
212 * Generic internet control operations (ioctl's).
213 * Ifp is 0 if not an interface-specific ioctl.
d5234de5
MD
214 *
215 * NOTE! td might be NULL.
984263bc
MD
216 */
217/* ARGSUSED */
218int
f3f70f0d
SW
219in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
220 struct thread *td)
984263bc 221{
b2632176
SZ
222 struct netmsg nmsg;
223 struct in_control_arg arg;
224 struct lwkt_msg *msg;
225 int error;
984263bc
MD
226
227 switch (cmd) {
228 case SIOCALIFADDR:
229 case SIOCDLIFADDR:
d5234de5 230 if (td && (error = suser(td)) != 0)
984263bc 231 return error;
7cea01ed 232 /* FALLTHROUGH */
984263bc
MD
233 case SIOCGLIFADDR:
234 if (!ifp)
235 return EINVAL;
dadab5e9 236 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
984263bc
MD
237 }
238
b2632176
SZ
239 KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
240 ("recursive SIOC%cLIFADDR!\n",
241 cmd == SIOCDLIFADDR ? 'D' : 'A'));
242
4a3d1a01
SZ
243 /*
244 * IFADDR alterations are serialized by netisr0
245 */
b2632176
SZ
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;
258
259 netmsg_init(&nmsg, &curthread->td_msgport, 0,
260 in_control_dispatch);
261 msg = &nmsg.nm_lmsg;
262 msg->u.ms_resultp = &arg;
263
264 lwkt_domsg(cpu_portfn(0), msg, 0);
265 return msg->ms_error;
266 default:
267 return in_control_internal(cmd, data, ifp, td);
268 }
269}
270
1b562c24
SZ
271static void
272in_ialink_dispatch(struct netmsg *nmsg)
273{
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;
279
280 crit_enter();
281
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"));
286
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);
290
291 crit_exit();
292
293 ifa_forwardmsg(lmsg, cpu + 1);
294}
295
296static void
297in_iaunlink_dispatch(struct netmsg *nmsg)
298{
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;
304
305 crit_enter();
306
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"));
311
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;
315
316 crit_exit();
317
318 ifa_forwardmsg(lmsg, cpu + 1);
319}
320
321static void
322in_ialink(struct in_ifaddr *ia)
323{
324 struct netmsg nmsg;
325 struct lwkt_msg *lmsg;
326
327 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_ialink_dispatch);
328 lmsg = &nmsg.nm_lmsg;
329 lmsg->u.ms_resultp = ia;
330
331 ifa_domsg(lmsg);
332}
333
334void
335in_iaunlink(struct in_ifaddr *ia)
336{
337 struct netmsg nmsg;
338 struct lwkt_msg *lmsg;
339
340 netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iaunlink_dispatch);
341 lmsg = &nmsg.nm_lmsg;
342 lmsg->u.ms_resultp = ia;
343
344 ifa_domsg(lmsg);
345}
346
347static __inline struct in_ifaddr *
348in_ianext(struct in_ifaddr *oia)
349{
350 struct ifaddr_container *ifac;
351 struct in_ifaddr_container *iac;
352
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"));
357
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;
364}
365
b2632176
SZ
366static int
367in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
368 struct thread *td)
369{
370 struct ifreq *ifr = (struct ifreq *)data;
371 struct in_ifaddr *ia = 0, *iap;
372 struct in_addr dst;
b2632176 373 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
1b562c24 374 struct ifaddr_container *ifac;
b2632176
SZ
375 struct sockaddr_in oldaddr;
376 int hostIsNew, iaIsNew, maskIsNew;
377 int error = 0;
378
379 iaIsNew = 0;
380
984263bc
MD
381 /*
382 * Find address for this interface, if it exists.
383 *
384 * If an alias address was specified, find that one instead of
385 * the first one on the interface, if possible
386 */
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;
394 }
b2632176 395 if (ia == NULL) {
b2632176
SZ
396 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
397 ifa_link) {
398 iap = ifatoia(ifac->ifa);
984263bc
MD
399 if (iap->ia_addr.sin_family == AF_INET) {
400 ia = iap;
401 break;
402 }
403 }
b2632176 404 }
984263bc
MD
405 }
406
407 switch (cmd) {
984263bc
MD
408 case SIOCAIFADDR:
409 case SIOCDIFADDR:
822fb498 410 if (ifp == NULL)
984263bc
MD
411 return (EADDRNOTAVAIL);
412 if (ifra->ifra_addr.sin_family == AF_INET) {
88dd5f37 413 while (ia != NULL) {
984263bc
MD
414 if (ia->ia_ifp == ifp &&
415 ia->ia_addr.sin_addr.s_addr ==
416 ifra->ifra_addr.sin_addr.s_addr)
417 break;
1b562c24 418 ia = in_ianext(ia);
984263bc 419 }
7cea01ed
SZ
420 if ((ifp->if_flags & IFF_POINTOPOINT) &&
421 cmd == SIOCAIFADDR &&
422 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
984263bc
MD
423 return EDESTADDRREQ;
424 }
425 }
822fb498 426 if (cmd == SIOCDIFADDR && ia == NULL)
984263bc
MD
427 return (EADDRNOTAVAIL);
428 /* FALLTHROUGH */
429 case SIOCSIFADDR:
430 case SIOCSIFNETMASK:
431 case SIOCSIFDSTADDR:
d5234de5 432 if (td && (error = suser(td)) != 0)
984263bc
MD
433 return error;
434
822fb498 435 if (ifp == NULL)
984263bc 436 return (EADDRNOTAVAIL);
b61e58c4
SZ
437
438 if (cmd == SIOCSIFDSTADDR &&
439 (ifp->if_flags & IFF_POINTOPOINT) == 0)
440 return (EINVAL);
441
31e9fef4 442 if (ia == NULL) {
b2632176 443 struct ifaddr *ifa;
1b562c24 444 int i;
b2632176
SZ
445
446 ia = ifa_create(sizeof(*ia), M_WAITOK);
1b562c24
SZ
447 ifa = &ia->ia_ifa;
448
449 /*
450 * Setup per-CPU information
451 */
452 for (i = 0; i < ncpus; ++i) {
453 struct in_ifaddr_container *iac;
454
455 ifac = &ifa->ifa_containers[i];
456 iac = &ifac->ifa_proto_u.u_in_ifac;
457 iac->ia = ia;
458 iac->ia_ifac = ifac;
459 }
fee75a22 460
984263bc 461 /*
5bdc9beb 462 * Protect from NETISR_IP traversing address list
984263bc
MD
463 * while we're modifying it.
464 */
1cae611f 465 crit_enter();
7cea01ed 466
1b562c24 467 in_ialink(ia);
b2632176 468 ifa_iflink(ifa, ifp, 1);
984263bc
MD
469
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) {
0c3c561c 476 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
984263bc
MD
477 ia->ia_broadaddr.sin_family = AF_INET;
478 }
479 ia->ia_ifp = ifp;
480 if (!(ifp->if_flags & IFF_LOOPBACK))
481 in_interfaces++;
482 iaIsNew = 1;
7cea01ed 483
1cae611f 484 crit_exit();
984263bc
MD
485 }
486 break;
487
488 case SIOCSIFBRDADDR:
d5234de5 489 if (td && (error = suser(td)) != 0)
984263bc
MD
490 return error;
491 /* FALLTHROUGH */
492
493 case SIOCGIFADDR:
494 case SIOCGIFNETMASK:
495 case SIOCGIFDSTADDR:
496 case SIOCGIFBRDADDR:
822fb498 497 if (ia == NULL)
984263bc
MD
498 return (EADDRNOTAVAIL);
499 break;
500 }
984263bc 501
56841fe2 502 switch (cmd) {
984263bc
MD
503 case SIOCGIFADDR:
504 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
505 return (0);
506
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);
512
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);
518
519 case SIOCGIFNETMASK:
520 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
521 return (0);
522
523 case SIOCSIFDSTADDR:
b61e58c4
SZ
524 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
525
984263bc
MD
526 oldaddr = ia->ia_dstaddr;
527 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
bdf8b13d
SZ
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);
78195a76 532 lwkt_serialize_exit(ifp->if_serializer);
bdf8b13d
SZ
533 if (error) {
534 ia->ia_dstaddr = oldaddr;
535 return (error);
536 }
984263bc
MD
537 }
538 if (ia->ia_flags & IFA_ROUTE) {
539 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
f23061d4 540 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
984263bc
MD
541 ia->ia_ifa.ifa_dstaddr =
542 (struct sockaddr *)&ia->ia_dstaddr;
f23061d4 543 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
984263bc
MD
544 }
545 return (0);
546
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);
552
553 case SIOCSIFADDR:
554 error = in_ifinit(ifp, ia,
44a87e42 555 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
984263bc
MD
556 if (error != 0 && iaIsNew)
557 break;
f2bd8b67
JS
558 if (error == 0)
559 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
984263bc
MD
560 return (0);
561
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);
566
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 ==
7cea01ed 576 ia->ia_addr.sin_addr.s_addr) {
984263bc 577 hostIsNew = 0;
7cea01ed 578 }
984263bc
MD
579 }
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 =
7cea01ed 585 ntohl(ia->ia_sockmask.sin_addr.s_addr);
984263bc
MD
586 maskIsNew = 1;
587 }
588 if ((ifp->if_flags & IFF_POINTOPOINT) &&
7cea01ed 589 ifra->ifra_dstaddr.sin_family == AF_INET) {
984263bc
MD
590 in_ifscrub(ifp, ia);
591 ia->ia_dstaddr = ifra->ifra_dstaddr;
592 maskIsNew = 1; /* We lie; but the effect's the same */
593 }
594 if (ifra->ifra_addr.sin_family == AF_INET &&
595 (hostIsNew || maskIsNew))
596 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
597
598 if (error != 0 && iaIsNew)
599 break;
600
601 if ((ifp->if_flags & IFF_BROADCAST) &&
7cea01ed 602 ifra->ifra_broadaddr.sin_family == AF_INET)
984263bc 603 ia->ia_broadaddr = ifra->ifra_broadaddr;
f2bd8b67
JS
604 if (error == 0)
605 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
984263bc
MD
606 return (error);
607
608 case SIOCDIFADDR:
609 /*
610 * in_ifscrub kills the interface route.
611 */
612 in_ifscrub(ifp, ia);
613 /*
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.
618 */
619 in_ifadown(&ia->ia_ifa, 1);
f2bd8b67 620 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
984263bc
MD
621 error = 0;
622 break;
623
624 default:
bd4539cc 625 if (ifp == NULL || ifp->if_ioctl == NULL)
984263bc 626 return (EOPNOTSUPP);
78195a76
MD
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);
984263bc
MD
631 }
632
b2632176 633 ifa_ifunlink(&ia->ia_ifa, ifp);
1b562c24 634 in_iaunlink(ia);
b2632176 635
984263bc 636 /*
5bdc9beb 637 * Protect from NETISR_IP traversing address list while we're modifying
984263bc
MD
638 * it.
639 */
b2632176 640 crit_enter(); /* XXX MP */
44a87e42
SZ
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);
644 }
b2632176
SZ
645 crit_exit(); /* XXX MP */
646
647 ifa_destroy(&ia->ia_ifa);
984263bc
MD
648
649 return (error);
650}
651
652/*
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()
d5234de5
MD
667 *
668 * NOTE! td might be NULL.
984263bc
MD
669 */
670static int
f3f70f0d
SW
671in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
672 struct thread *td)
984263bc
MD
673{
674 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
984263bc
MD
675
676 /* sanity checks */
677 if (!data || !ifp) {
678 panic("invalid argument to in_lifaddr_ioctl");
679 /*NOTRECHED*/
680 }
681
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;
705 }
706 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
707 return EINVAL;
708
709 switch (cmd) {
710 case SIOCALIFADDR:
711 {
712 struct in_aliasreq ifra;
713
714 if (iflr->flags & IFLR_PREFIX)
715 return EINVAL;
716
717 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
0c3c561c
JH
718 bzero(&ifra, sizeof ifra);
719 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
984263bc
MD
720
721 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
722
723 if (iflr->dstaddr.ss_family) { /*XXX*/
724 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
725 iflr->dstaddr.ss_len);
726 }
727
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);
731
dadab5e9 732 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
984263bc
MD
733 }
734 case SIOCGLIFADDR:
735 case SIOCDLIFADDR:
736 {
b2632176 737 struct ifaddr_container *ifac;
984263bc
MD
738 struct in_ifaddr *ia;
739 struct in_addr mask, candidate, match;
740 struct sockaddr_in *sin;
741 int cmp;
742
0c3c561c 743 bzero(&mask, sizeof mask);
984263bc
MD
744 if (iflr->flags & IFLR_PREFIX) {
745 /* lookup a prefix rather than address. */
746 in_len2mask(&mask, iflr->prefixlen);
747
748 sin = (struct sockaddr_in *)&iflr->addr;
749 match.s_addr = sin->sin_addr.s_addr;
750 match.s_addr &= mask.s_addr;
751
752 /* if you set extra bits, that's wrong */
753 if (match.s_addr != sin->sin_addr.s_addr)
754 return EINVAL;
755
756 cmp = 1;
757 } else {
758 if (cmd == SIOCGLIFADDR) {
759 /* on getting an address, take the 1st match */
31e9fef4 760 match.s_addr = 0; /* gcc4 warning */
984263bc
MD
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;
767
768 cmp = 1;
769 }
770 }
771
b2632176
SZ
772 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
773 struct ifaddr *ifa = ifac->ifa;
774
984263bc
MD
775 if (ifa->ifa_addr->sa_family != AF_INET6)
776 continue;
777 if (!cmp)
778 break;
b2632176
SZ
779 candidate.s_addr =
780 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
984263bc
MD
781 candidate.s_addr &= mask.s_addr;
782 if (candidate.s_addr == match.s_addr)
783 break;
784 }
b2632176 785 if (ifac == NULL)
984263bc 786 return EADDRNOTAVAIL;
b2632176 787 ia = (struct in_ifaddr *)(ifac->ifa);
984263bc
MD
788
789 if (cmd == SIOCGLIFADDR) {
790 /* fill in the if_laddrreq structure */
791 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
792
793 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
794 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
795 ia->ia_dstaddr.sin_len);
796 } else
0c3c561c 797 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
984263bc
MD
798
799 iflr->prefixlen =
800 in_mask2len(&ia->ia_sockmask.sin_addr);
801
802 iflr->flags = 0; /*XXX*/
803
804 return 0;
805 } else {
806 struct in_aliasreq ifra;
807
808 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
0c3c561c 809 bzero(&ifra, sizeof ifra);
984263bc 810 bcopy(iflr->iflr_name, ifra.ifra_name,
0c3c561c 811 sizeof ifra.ifra_name);
984263bc
MD
812
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);
818 }
819 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
820 ia->ia_sockmask.sin_len);
821
822 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
dadab5e9 823 ifp, td);
984263bc
MD
824 }
825 }
826 }
827
828 return EOPNOTSUPP; /*just for safety*/
829}
830
831/*
832 * Delete any existing route for an interface.
833 */
834void
f3f70f0d 835in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
984263bc
MD
836{
837
838 if ((ia->ia_flags & IFA_ROUTE) == 0)
839 return;
840 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
f23061d4 841 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
984263bc 842 else
f23061d4 843 rtinit(&ia->ia_ifa, RTM_DELETE, 0);
984263bc
MD
844 ia->ia_flags &= ~IFA_ROUTE;
845}
846
847/*
848 * Initialize an interface's internet address
849 * and routing table entry.
850 */
851static int
44a87e42
SZ
852in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
853 const struct sockaddr_in *sin, int scrub)
984263bc 854{
2256ba69 855 u_long i = ntohl(sin->sin_addr.s_addr);
984263bc 856 struct sockaddr_in oldaddr;
1cae611f 857 int flags = RTF_UP, error = 0;
44a87e42 858 int old_hash = 0, new_hash = 0;
1cae611f 859
92294f5a 860 crit_enter();
984263bc 861 oldaddr = ia->ia_addr;
44a87e42
SZ
862 if (oldaddr.sin_family == AF_INET) {
863 old_hash = 1;
984263bc 864 LIST_REMOVE(ia, ia_hash);
44a87e42 865 }
66fcacb2 866
984263bc 867 ia->ia_addr = *sin;
66fcacb2 868 if (ia->ia_addr.sin_family == AF_INET) {
44a87e42 869 new_hash = 1;
984263bc
MD
870 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
871 ia, ia_hash);
66fcacb2 872 }
92294f5a 873 crit_exit();
66fcacb2 874
984263bc
MD
875 /*
876 * Give the interface a chance to initialize
877 * if this is its first address,
878 * and to validate the address if necessary.
879 */
92294f5a
SZ
880 if (ifp->if_ioctl != NULL) {
881 lwkt_serialize_enter(ifp->if_serializer);
882 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
78195a76 883 lwkt_serialize_exit(ifp->if_serializer);
44a87e42
SZ
884 if (error)
885 goto fail;
984263bc 886 }
66fcacb2
SZ
887
888 /*
889 * Delete old route, if requested.
890 */
984263bc
MD
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;
895 }
66fcacb2
SZ
896
897 /*
898 * Calculate netmask/subnetmask.
899 */
984263bc
MD
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;
906 /*
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.
910 */
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);
66fcacb2 914 } else {
984263bc 915 ia->ia_netmask &= ia->ia_subnetmask;
66fcacb2 916 }
984263bc
MD
917 ia->ia_net = i & ia->ia_netmask;
918 ia->ia_subnet = i & ia->ia_subnetmask;
919 in_socktrim(&ia->ia_sockmask);
66fcacb2 920
984263bc
MD
921 /*
922 * Add route for the network.
923 */
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;
937 }
938
939 /*-
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.
944 *
945 * XXX: This is ugly ! There should be a way for the caller to
946 * say that they don't want a host route.
947 */
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)) {
44a87e42
SZ
951 if ((error = rtinit(&ia->ia_ifa, RTM_ADD, flags)) != 0)
952 goto fail;
984263bc
MD
953 ia->ia_flags |= IFA_ROUTE;
954 }
955
956 /*
957 * If the interface supports multicast, join the "all hosts"
958 * multicast group on that interface.
959 */
960 if (ifp->if_flags & IFF_MULTICAST) {
961 struct in_addr addr;
962
963 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
964 in_addmulti(&addr, ifp);
965 }
44a87e42
SZ
966 return (0);
967fail:
968 crit_enter();
969 if (new_hash)
970 LIST_REMOVE(ia, ia_hash);
971
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);
976 }
977 crit_exit();
984263bc
MD
978 return (error);
979}
980
981
982/*
983 * Return 1 if the address might be a local broadcast address.
984 */
985int
f23061d4 986in_broadcast(struct in_addr in, struct ifnet *ifp)
984263bc 987{
b2632176 988 struct ifaddr_container *ifac;
984263bc
MD
989 u_long t;
990
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);
997 /*
998 * Look through the list of addresses for a match
999 * with a broadcast address.
1000 */
1001#define ia ((struct in_ifaddr *)ifa)
b2632176
SZ
1002 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1003 struct ifaddr *ifa = ifac->ifa;
1004
984263bc
MD
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 ||
1008 /*
1009 * Check for old-style (host 0) broadcast.
1010 */
1011 t == ia->ia_subnet || t == ia->ia_net) &&
1012 /*
1013 * Check for an all one subnetmask. These
1014 * only exist when an interface gets a secondary
1015 * address.
1016 */
1017 ia->ia_subnetmask != (u_long)0xffffffff)
1018 return 1;
b2632176 1019 }
984263bc
MD
1020 return (0);
1021#undef ia
1022}
1023/*
1024 * Add an address to the list of IP multicast addresses for a given interface.
1025 */
1026struct in_multi *
f3f70f0d 1027in_addmulti(struct in_addr *ap, struct ifnet *ifp)
984263bc 1028{
2256ba69 1029 struct in_multi *inm;
984263bc
MD
1030 int error;
1031 struct sockaddr_in sin;
1032 struct ifmultiaddr *ifma;
984263bc
MD
1033
1034 /*
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).
1038 */
1039 bzero(&sin, sizeof sin);
1040 sin.sin_family = AF_INET;
1041 sin.sin_len = sizeof sin;
1042 sin.sin_addr = *ap;
1cae611f 1043 crit_enter();
984263bc
MD
1044 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1045 if (error) {
1cae611f 1046 crit_exit();
984263bc
MD
1047 return 0;
1048 }
1049
1050 /*
1051 * If ifma->ifma_protospec is null, then if_addmulti() created
1052 * a new record. Otherwise, we are done.
1053 */
1054 if (ifma->ifma_protospec != 0) {
1cae611f 1055 crit_exit();
984263bc
MD
1056 return ifma->ifma_protospec;
1057 }
1058
1059 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1060 at interrupt time? If so, need to fix if_addmulti. XXX */
efda3bd0 1061 inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
984263bc
MD
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);
1067
1068 /*
1069 * Let IGMP know that we have joined a new IP multicast group.
1070 */
1071 igmp_joingroup(inm);
1cae611f 1072 crit_exit();
984263bc
MD
1073 return (inm);
1074}
1075
1076/*
1077 * Delete a multicast address record.
1078 */
1079void
f3f70f0d 1080in_delmulti(struct in_multi *inm)
984263bc 1081{
1cae611f 1082 struct ifmultiaddr *ifma;
984263bc 1083 struct in_multi my_inm;
984263bc 1084
1cae611f
MD
1085 crit_enter();
1086 ifma = inm->inm_ifma;
984263bc
MD
1087 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1088 if (ifma->ifma_refcount == 1) {
1089 /*
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.
1094 */
1095 my_inm = *inm ;
1096 ifma->ifma_protospec = 0;
1097 LIST_REMOVE(inm, inm_link);
efda3bd0 1098 kfree(inm, M_IPMADDR);
984263bc
MD
1099 }
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);
1cae611f 1104 crit_exit();
984263bc 1105}
a98eb818
JS
1106
1107void
1108in_ifdetach(struct ifnet *ifp)
1109{
1110 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
1111 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
1112}