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. 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.
16 *
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.
28 *
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 $
31 */
32
33 #include "opt_bootp.h"
34 #include "opt_carp.h"
35
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/caps.h>
42 #include <sys/msgport.h>
43 #include <sys/socket.h>
44
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
48
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>
54
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/udp_var.h>
59
60 #include <netinet/igmp_var.h>
61
62 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
63
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 *);
67
68 static void in_socktrim (struct sockaddr_in *);
69 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
70 const struct sockaddr_in *, int);
71
72 static int in_control_internal(u_long, caddr_t, struct ifnet *,
73 struct thread *);
74
75 static int in_addprefix(struct in_ifaddr *, int);
76 static void in_scrubprefix(struct in_ifaddr *);
77
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");
82
83 struct in_multihead in_multihead; /* XXX BSS initialization */
84
85 extern struct inpcbinfo ripcbinfo;
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 */
93 int
in_localaddr(struct in_addr in)94 in_localaddr(struct in_addr in)
95 {
96 u_long i = ntohl(in.s_addr);
97 struct in_ifaddr_container *iac;
98 struct in_ifaddr *ia;
99
100 if (subnetsarelocal) {
101 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
102 ia = iac->ia;
103 if ((i & ia->ia_netmask) == ia->ia_net)
104 return (1);
105 }
106 } else {
107 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
108 ia = iac->ia;
109 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
110 return (1);
111 }
112 }
113
114 return (0);
115 }
116
117 /*
118 * Determine whether an IP address is in a reserved set of addresses
119 * that may not be forwarded, or whether datagrams to that destination
120 * may be forwarded.
121 */
122 int
in_canforward(struct in_addr in)123 in_canforward(struct in_addr in)
124 {
125 u_long i = ntohl(in.s_addr);
126 u_long net;
127
128 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
129 return (0);
130 if (IN_CLASSA(i)) {
131 net = i & IN_CLASSA_NET;
132 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
133 return (0);
134 }
135 return (1);
136 }
137
138 /*
139 * Trim a mask in a sockaddr
140 */
141 static void
in_socktrim(struct sockaddr_in * ap)142 in_socktrim(struct sockaddr_in *ap)
143 {
144 char *cplim = (char *) &ap->sin_addr;
145 char *cp = (char *) (&ap->sin_addr + 1);
146
147 ap->sin_len = 0;
148 while (--cp >= cplim) {
149 if (*cp) {
150 (ap)->sin_len = cp - (char *) (ap) + 1;
151 break;
152 }
153 }
154 }
155
156 static int
in_mask2len(struct in_addr * mask)157 in_mask2len(struct in_addr *mask)
158 {
159 int x, y;
160 u_char *p;
161
162 p = (u_char *)mask;
163 for (x = 0; x < sizeof *mask; x++) {
164 if (p[x] != 0xff)
165 break;
166 }
167 y = 0;
168 if (x < sizeof *mask) {
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
177 static void
in_len2mask(struct in_addr * mask,int len)178 in_len2mask(struct in_addr *mask, int len)
179 {
180 int i;
181 u_char *p;
182
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;
189 }
190
191 void
in_control_dispatch(netmsg_t msg)192 in_control_dispatch(netmsg_t msg)
193 {
194 int error;
195
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);
199 }
200
201 static void
in_control_internal_dispatch(netmsg_t msg)202 in_control_internal_dispatch(netmsg_t msg)
203 {
204 int error;
205
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);
211 }
212
213 /*
214 * Generic internet control operations (ioctl's).
215 * Ifp is 0 if not an interface-specific ioctl.
216 *
217 * NOTE! td might be NULL.
218 */
219 int
in_control(u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)220 in_control(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
221 {
222 struct netmsg_pru_control msg;
223
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:
234 case SIOCGIFALIAS:
235 /*
236 * Dispatch these SIOCs to netisr0.
237 */
238 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
239 in_control_internal_dispatch);
240 msg.nm_cmd = cmd;
241 msg.nm_data = data;
242 msg.nm_ifp = ifp;
243 msg.nm_td = td;
244 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
245 return msg.base.lmsg.ms_error;
246
247 default:
248 return in_control_internal(cmd, data, ifp, td);
249 }
250 }
251
252 static void
in_ialink_dispatch(netmsg_t msg)253 in_ialink_dispatch(netmsg_t msg)
254 {
255 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
256 struct ifaddr_container *ifac;
257 struct in_ifaddr_container *iac;
258 int cpu = mycpuid;
259
260 crit_enter();
261
262 ifac = &ia->ia_ifa.ifa_containers[cpu];
263 ASSERT_IFAC_VALID(ifac);
264 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
265 ("ia is on in_ifaddrheads"));
266
267 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
268 iac = &ifac->ifa_proto_u.u_in_ifac;
269 TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
270
271 crit_exit();
272
273 netisr_forwardmsg_all(&msg->base, cpu + 1);
274 }
275
276 static void
in_iaunlink_dispatch(netmsg_t msg)277 in_iaunlink_dispatch(netmsg_t msg)
278 {
279 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
280 struct ifaddr_container *ifac;
281 struct in_ifaddr_container *iac;
282 int cpu = mycpuid;
283
284 crit_enter();
285
286 ifac = &ia->ia_ifa.ifa_containers[cpu];
287 ASSERT_IFAC_VALID(ifac);
288 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
289 ("ia is not on in_ifaddrheads"));
290
291 iac = &ifac->ifa_proto_u.u_in_ifac;
292 TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
293 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
294
295 crit_exit();
296
297 netisr_forwardmsg_all(&msg->base, cpu + 1);
298 }
299
300 static void
in_iahashins_dispatch(netmsg_t msg)301 in_iahashins_dispatch(netmsg_t msg)
302 {
303 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
304 struct ifaddr_container *ifac;
305 struct in_ifaddr_container *iac;
306 int cpu = mycpuid;
307
308 crit_enter();
309
310 ifac = &ia->ia_ifa.ifa_containers[cpu];
311 ASSERT_IFAC_VALID(ifac);
312 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
313 ("ia is on in_ifaddrhashtbls"));
314
315 ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
316 iac = &ifac->ifa_proto_u.u_in_ifac;
317 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
318 iac, ia_hash);
319
320 crit_exit();
321
322 netisr_forwardmsg_all(&msg->base, cpu + 1);
323 }
324
325 static void
in_iahashrem_dispatch(netmsg_t msg)326 in_iahashrem_dispatch(netmsg_t msg)
327 {
328 struct in_ifaddr *ia = msg->lmsg.u.ms_resultp;
329 struct ifaddr_container *ifac;
330 struct in_ifaddr_container *iac;
331 int cpu = mycpuid;
332
333 crit_enter();
334
335 ifac = &ia->ia_ifa.ifa_containers[cpu];
336 ASSERT_IFAC_VALID(ifac);
337 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
338 ("ia is not on in_ifaddrhashtbls"));
339
340 iac = &ifac->ifa_proto_u.u_in_ifac;
341 LIST_REMOVE(iac, ia_hash);
342 ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
343
344 crit_exit();
345
346 netisr_forwardmsg_all(&msg->base, cpu + 1);
347 }
348
349 static void
in_ialink(struct in_ifaddr * ia)350 in_ialink(struct in_ifaddr *ia)
351 {
352 struct netmsg_base msg;
353
354 netmsg_init(&msg, NULL, &curthread->td_msgport,
355 0, in_ialink_dispatch);
356 msg.lmsg.u.ms_resultp = ia;
357
358 netisr_domsg(&msg, 0);
359 }
360
361 void
in_iaunlink(struct in_ifaddr * ia)362 in_iaunlink(struct in_ifaddr *ia)
363 {
364 struct netmsg_base msg;
365
366 netmsg_init(&msg, NULL, &curthread->td_msgport,
367 0, in_iaunlink_dispatch);
368 msg.lmsg.u.ms_resultp = ia;
369
370 netisr_domsg(&msg, 0);
371 }
372
373 void
in_iahash_insert(struct in_ifaddr * ia)374 in_iahash_insert(struct in_ifaddr *ia)
375 {
376 struct netmsg_base msg;
377
378 netmsg_init(&msg, NULL, &curthread->td_msgport,
379 0, in_iahashins_dispatch);
380 msg.lmsg.u.ms_resultp = ia;
381
382 netisr_domsg(&msg, 0);
383 }
384
385 void
in_iahash_remove(struct in_ifaddr * ia)386 in_iahash_remove(struct in_ifaddr *ia)
387 {
388 struct netmsg_base msg;
389
390 netmsg_init(&msg, NULL, &curthread->td_msgport,
391 0, in_iahashrem_dispatch);
392 msg.lmsg.u.ms_resultp = ia;
393
394 netisr_domsg(&msg, 0);
395 }
396
397 static __inline struct in_ifaddr *
in_ianext(struct in_ifaddr * oia)398 in_ianext(struct in_ifaddr *oia)
399 {
400 struct ifaddr_container *ifac;
401 struct in_ifaddr_container *iac;
402
403 ifac = &oia->ia_ifa.ifa_containers[mycpuid];
404 ASSERT_IFAC_VALID(ifac);
405 KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
406 ("ia is not on in_ifaddrheads"));
407
408 iac = &ifac->ifa_proto_u.u_in_ifac;
409 iac = TAILQ_NEXT(iac, ia_link);
410 if (iac != NULL)
411 return iac->ia;
412 else
413 return NULL;
414 }
415
416 static int
in_control_internal(u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)417 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
418 struct thread *td)
419 {
420 struct ifreq *ifr = (struct ifreq *)data;
421 struct in_ifaddr *ia = NULL;
422 struct in_addr dst;
423 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
424 struct ifaddr_container *ifac;
425 struct in_ifaddr_container *iac;
426 struct sockaddr_in oldaddr;
427 int hostIsNew, iaIsNew, maskIsNew, ifpWasUp;
428 int error = 0;
429
430 switch (cmd) {
431 case SIOCALIFADDR:
432 case SIOCDLIFADDR:
433 if (td && (error = caps_priv_check_td(td,
434 SYSCAP_RESTRICTEDROOT)) != 0)
435 {
436 return error;
437 }
438 /* FALLTHROUGH */
439 case SIOCGLIFADDR:
440 if (!ifp)
441 return EINVAL;
442 return in_lifaddr_ioctl(cmd, data, ifp, td);
443 }
444
445 iaIsNew = 0;
446 ifpWasUp = 0;
447
448 /*
449 * Find address for this interface, if it exists.
450 *
451 * If an alias address was specified, find that one instead of
452 * the first one on the interface, if possible.
453 */
454 if (ifp) {
455 struct in_ifaddr *iap;
456
457 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
458 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
459 iap = iac->ia;
460 if (iap->ia_ifp == ifp &&
461 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
462 ia = iap;
463 break;
464 }
465 }
466 if (ia == NULL) {
467 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
468 ifa_link) {
469 iap = ifatoia(ifac->ifa);
470 if (iap->ia_addr.sin_family == AF_INET) {
471 ia = iap;
472 break;
473 }
474 }
475 }
476
477 if (ifp->if_flags & IFF_UP)
478 ifpWasUp = 1;
479 }
480
481 switch (cmd) {
482 case SIOCAIFADDR:
483 case SIOCDIFADDR:
484 case SIOCGIFALIAS:
485 if (ifp == NULL)
486 return (EADDRNOTAVAIL);
487 if (ifra->ifra_addr.sin_family == AF_INET) {
488 while (ia != NULL) {
489 if (ia->ia_ifp == ifp &&
490 ia->ia_addr.sin_addr.s_addr ==
491 ifra->ifra_addr.sin_addr.s_addr)
492 break;
493 ia = in_ianext(ia);
494 }
495 if ((ifp->if_flags & IFF_POINTOPOINT) &&
496 cmd == SIOCAIFADDR &&
497 ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
498 return EDESTADDRREQ;
499 }
500 }
501 if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL)
502 return (EADDRNOTAVAIL);
503 if (cmd == SIOCGIFALIAS)
504 break;
505 /* FALLTHROUGH */
506 case SIOCSIFADDR:
507 case SIOCSIFNETMASK:
508 case SIOCSIFDSTADDR:
509 if (td && (error = caps_priv_check_td(td,
510 SYSCAP_RESTRICTEDROOT)) != 0)
511 {
512 return error;
513 }
514
515 if (ifp == NULL)
516 return (EADDRNOTAVAIL);
517
518 if (cmd == SIOCSIFDSTADDR &&
519 (ifp->if_flags & IFF_POINTOPOINT) == 0)
520 return (EINVAL);
521
522 if (ia == NULL) {
523 struct ifaddr *ifa;
524 int i;
525
526 ia = ifa_create(sizeof(*ia));
527 ifa = &ia->ia_ifa;
528
529 /*
530 * Setup per-CPU information
531 */
532 for (i = 0; i < ncpus; ++i) {
533 ifac = &ifa->ifa_containers[i];
534 iac = &ifac->ifa_proto_u.u_in_ifac;
535 iac->ia = ia;
536 iac->ia_ifac = ifac;
537 }
538
539 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
540 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
541 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
542 ia->ia_sockmask.sin_len = 8;
543 ia->ia_sockmask.sin_family = AF_INET;
544 if (ifp->if_flags & IFF_BROADCAST) {
545 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
546 ia->ia_broadaddr.sin_family = AF_INET;
547 }
548 ia->ia_ifp = ifp;
549 iaIsNew = 1;
550
551 in_ialink(ia);
552 ifa_iflink(ifa, ifp, 1);
553 }
554 break;
555
556 case SIOCSIFBRDADDR:
557 if (td && (error = caps_priv_check_td(td,
558 SYSCAP_RESTRICTEDROOT)) != 0)
559 {
560 return error;
561 }
562 /* FALLTHROUGH */
563
564 case SIOCGIFADDR:
565 case SIOCGIFNETMASK:
566 case SIOCGIFDSTADDR:
567 case SIOCGIFBRDADDR:
568 if (ia == NULL)
569 return (EADDRNOTAVAIL);
570 break;
571 }
572
573 switch (cmd) {
574 case SIOCGIFADDR:
575 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
576 return (0);
577
578 case SIOCGIFBRDADDR:
579 if ((ifp->if_flags & IFF_BROADCAST) == 0)
580 return (EINVAL);
581 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
582 return (0);
583
584 case SIOCGIFDSTADDR:
585 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
586 return (EINVAL);
587 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
588 return (0);
589
590 case SIOCGIFNETMASK:
591 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
592 return (0);
593
594 case SIOCSIFDSTADDR:
595 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
596
597 oldaddr = ia->ia_dstaddr;
598 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
599 if (ifp->if_ioctl != NULL) {
600 ifnet_serialize_all(ifp);
601 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
602 td->td_proc->p_ucred);
603 ifnet_deserialize_all(ifp);
604 if (error) {
605 ia->ia_dstaddr = oldaddr;
606 return (error);
607 }
608 }
609 if (ia->ia_flags & IFA_ROUTE) {
610 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
611 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
612 ia->ia_ifa.ifa_dstaddr =
613 (struct sockaddr *)&ia->ia_dstaddr;
614 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
615 }
616 return (0);
617
618 case SIOCSIFBRDADDR:
619 if ((ifp->if_flags & IFF_BROADCAST) == 0)
620 return (EINVAL);
621 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
622 return (0);
623
624 case SIOCSIFADDR:
625 error = in_ifinit(ifp, ia,
626 (const struct sockaddr_in *)&ifr->ifr_addr, 1);
627 if (error != 0 && iaIsNew)
628 break;
629 if (error == 0) {
630 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
631 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
632 &ia->ia_ifa);
633 }
634 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
635 /*
636 * Interface is brought up by in_ifinit()
637 * (via ifp->if_ioctl). We act as if the
638 * interface got IFF_UP flag turned on.
639 */
640 if_up(ifp);
641 }
642 return (0);
643
644 case SIOCSIFNETMASK:
645 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
646 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
647 return (0);
648
649 case SIOCAIFADDR:
650 maskIsNew = 0;
651 hostIsNew = 1;
652 error = 0;
653 if (ia->ia_addr.sin_family == AF_INET) {
654 if (ifra->ifra_addr.sin_len == 0) {
655 ifra->ifra_addr = ia->ia_addr;
656 hostIsNew = 0;
657 } else if (ifra->ifra_addr.sin_addr.s_addr ==
658 ia->ia_addr.sin_addr.s_addr) {
659 hostIsNew = 0;
660 }
661 }
662 if (ifra->ifra_mask.sin_len) {
663 in_ifscrub(ifp, ia);
664 ia->ia_sockmask = ifra->ifra_mask;
665 ia->ia_sockmask.sin_family = AF_INET;
666 ia->ia_subnetmask =
667 ntohl(ia->ia_sockmask.sin_addr.s_addr);
668 maskIsNew = 1;
669 }
670 if ((ifp->if_flags & IFF_POINTOPOINT) &&
671 ifra->ifra_dstaddr.sin_family == AF_INET) {
672 in_ifscrub(ifp, ia);
673 ia->ia_dstaddr = ifra->ifra_dstaddr;
674 maskIsNew = 1; /* We lie; but the effect's the same */
675 }
676 if (ifra->ifra_addr.sin_family == AF_INET &&
677 (hostIsNew || maskIsNew))
678 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
679
680 if (error != 0 && iaIsNew)
681 break;
682
683 if ((ifp->if_flags & IFF_BROADCAST) &&
684 ifra->ifra_broadaddr.sin_family == AF_INET)
685 ia->ia_broadaddr = ifra->ifra_broadaddr;
686 if (error == 0) {
687 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
688 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
689 &ia->ia_ifa);
690 }
691 if (!ifpWasUp && (ifp->if_flags & IFF_UP)) {
692 /* See the comment in SIOCSIFADDR */
693 if_up(ifp);
694 }
695 return (error);
696
697 case SIOCGIFALIAS:
698 ifra->ifra_mask = ia->ia_sockmask;
699 if ((ifp->if_flags & IFF_POINTOPOINT) &&
700 (ia->ia_dstaddr.sin_family == AF_INET))
701 ifra->ifra_dstaddr = ia->ia_dstaddr;
702 else if ((ifp->if_flags & IFF_BROADCAST) &&
703 (ia->ia_broadaddr.sin_family == AF_INET))
704 ifra->ifra_broadaddr = ia->ia_broadaddr;
705 else
706 memset(&ifra->ifra_broadaddr, 0,
707 sizeof(ifra->ifra_broadaddr));
708 return (0);
709
710 case SIOCDIFADDR:
711 /*
712 * in_ifscrub kills the interface route.
713 */
714 in_ifscrub(ifp, ia);
715 /*
716 * in_ifadown gets rid of all the rest of
717 * the routes. This is not quite the right
718 * thing to do, but at least if we are running
719 * a routing process they will come back.
720 */
721 in_ifadown(&ia->ia_ifa, 1);
722 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
723 &ia->ia_ifa);
724 error = 0;
725 break;
726
727 default:
728 if (ifp == NULL || ifp->if_ioctl == NULL)
729 return (EOPNOTSUPP);
730 ifnet_serialize_all(ifp);
731 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
732 ifnet_deserialize_all(ifp);
733 return (error);
734 }
735
736 KKASSERT(cmd == SIOCDIFADDR ||
737 ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
738
739 ifa_ifunlink(&ia->ia_ifa, ifp);
740 in_iaunlink(ia);
741
742 if (cmd == SIOCDIFADDR) {
743 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
744 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
745 in_iahash_remove(ia);
746 }
747 #ifdef INVARIANTS
748 else {
749 /*
750 * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
751 * already taken care of the deletion from hash table
752 */
753 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
754 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
755 ("SIOC%cIFADDR failed on new ia, "
756 "but the new ia is still in hash table",
757 cmd == SIOCSIFADDR ? 'S' : 'A'));
758 }
759 #endif
760
761 ifa_destroy(&ia->ia_ifa);
762
763 if ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) &&
764 !ifpWasUp && (ifp->if_flags & IFF_UP)) {
765 /*
766 * Though the address assignment failed, the
767 * interface is brought up by in_ifinit()
768 * (via ifp->if_ioctl). With the hope that
769 * the interface has some valid addresses, we
770 * act as if IFF_UP flag was just set on the
771 * interface.
772 *
773 * NOTE:
774 * This could only be done after the failed
775 * address is unlinked from the global address
776 * list.
777 */
778 if_up(ifp);
779 }
780
781 return (error);
782 }
783
784 /*
785 * SIOC[GAD]LIFADDR.
786 * SIOCGLIFADDR: get first address. (?!?)
787 * SIOCGLIFADDR with IFLR_PREFIX:
788 * get first address that matches the specified prefix.
789 * SIOCALIFADDR: add the specified address.
790 * SIOCALIFADDR with IFLR_PREFIX:
791 * EINVAL since we can't deduce hostid part of the address.
792 * SIOCDLIFADDR: delete the specified address.
793 * SIOCDLIFADDR with IFLR_PREFIX:
794 * delete the first address that matches the specified prefix.
795 * return values:
796 * EINVAL on invalid parameters
797 * EADDRNOTAVAIL on prefix match failed/specified address not found
798 * other values may be returned from in_ioctl()
799 *
800 * NOTE! td might be NULL.
801 */
802 static int
in_lifaddr_ioctl(u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)803 in_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
804 {
805 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
806
807 /* sanity checks */
808 if (!data || !ifp) {
809 panic("invalid argument to in_lifaddr_ioctl");
810 /*NOTRECHED*/
811 }
812
813 switch (cmd) {
814 case SIOCGLIFADDR:
815 /* address must be specified on GET with IFLR_PREFIX */
816 if ((iflr->flags & IFLR_PREFIX) == 0)
817 break;
818 /*FALLTHROUGH*/
819 case SIOCALIFADDR:
820 case SIOCDLIFADDR:
821 /* address must be specified on ADD and DELETE */
822 if (iflr->addr.ss_family != AF_INET)
823 return EINVAL;
824 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
825 return EINVAL;
826 /* XXX need improvement */
827 if (iflr->dstaddr.ss_family &&
828 iflr->dstaddr.ss_family != AF_INET)
829 return EINVAL;
830 if (iflr->dstaddr.ss_family &&
831 iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
832 return EINVAL;
833 break;
834 default: /*shouldn't happen*/
835 return EOPNOTSUPP;
836 }
837 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
838 return EINVAL;
839
840 switch (cmd) {
841 case SIOCALIFADDR:
842 {
843 struct in_aliasreq ifra;
844
845 if (iflr->flags & IFLR_PREFIX)
846 return EINVAL;
847
848 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
849 bzero(&ifra, sizeof ifra);
850 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
851
852 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
853
854 if (iflr->dstaddr.ss_family) { /*XXX*/
855 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
856 iflr->dstaddr.ss_len);
857 }
858
859 ifra.ifra_mask.sin_family = AF_INET;
860 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
861 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
862
863 return in_control_internal(SIOCAIFADDR, (caddr_t)&ifra, ifp,
864 td);
865 }
866 case SIOCGLIFADDR:
867 case SIOCDLIFADDR:
868 {
869 struct ifaddr_container *ifac;
870 struct in_ifaddr *ia;
871 struct in_addr mask, candidate, match;
872 struct sockaddr_in *sin;
873 int cmp;
874
875 bzero(&mask, sizeof mask);
876 if (iflr->flags & IFLR_PREFIX) {
877 /* lookup a prefix rather than address. */
878 in_len2mask(&mask, iflr->prefixlen);
879
880 sin = (struct sockaddr_in *)&iflr->addr;
881 match.s_addr = sin->sin_addr.s_addr;
882 match.s_addr &= mask.s_addr;
883
884 /* if you set extra bits, that's wrong */
885 if (match.s_addr != sin->sin_addr.s_addr)
886 return EINVAL;
887
888 cmp = 1;
889 } else {
890 if (cmd == SIOCGLIFADDR) {
891 /* on getting an address, take the 1st match */
892 match.s_addr = 0; /* gcc4 warning */
893 cmp = 0; /*XXX*/
894 } else {
895 /* on deleting an address, do exact match */
896 in_len2mask(&mask, 32);
897 sin = (struct sockaddr_in *)&iflr->addr;
898 match.s_addr = sin->sin_addr.s_addr;
899
900 cmp = 1;
901 }
902 }
903
904 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
905 struct ifaddr *ifa = ifac->ifa;
906
907 if (ifa->ifa_addr->sa_family != AF_INET6)
908 continue;
909 if (!cmp)
910 break;
911 candidate.s_addr =
912 ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
913 candidate.s_addr &= mask.s_addr;
914 if (candidate.s_addr == match.s_addr)
915 break;
916 }
917 if (ifac == NULL)
918 return EADDRNOTAVAIL;
919 ia = (struct in_ifaddr *)(ifac->ifa);
920
921 if (cmd == SIOCGLIFADDR) {
922 /* fill in the if_laddrreq structure */
923 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
924
925 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
926 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
927 ia->ia_dstaddr.sin_len);
928 } else
929 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
930
931 iflr->prefixlen =
932 in_mask2len(&ia->ia_sockmask.sin_addr);
933
934 iflr->flags = 0; /*XXX*/
935
936 return 0;
937 } else {
938 struct in_aliasreq ifra;
939
940 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
941 bzero(&ifra, sizeof ifra);
942 bcopy(iflr->iflr_name, ifra.ifra_name,
943 sizeof ifra.ifra_name);
944
945 bcopy(&ia->ia_addr, &ifra.ifra_addr,
946 ia->ia_addr.sin_len);
947 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
948 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
949 ia->ia_dstaddr.sin_len);
950 }
951 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
952 ia->ia_sockmask.sin_len);
953
954 return in_control_internal(SIOCDIFADDR, (caddr_t)&ifra,
955 ifp, td);
956 }
957 }
958 }
959
960 return EOPNOTSUPP; /*just for safety*/
961 }
962
963 /*
964 * Delete any existing route for an interface.
965 */
966 void
in_ifscrub(struct ifnet * ifp __unused,struct in_ifaddr * ia)967 in_ifscrub(struct ifnet *ifp __unused, struct in_ifaddr *ia)
968 {
969 in_scrubprefix(ia);
970 }
971
972 /*
973 * Initialize an interface's internet address
974 * and routing table entry.
975 */
976 static int
in_ifinit(struct ifnet * ifp,struct in_ifaddr * ia,const struct sockaddr_in * sin,int scrub)977 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
978 const struct sockaddr_in *sin, int scrub)
979 {
980 u_long i = ntohl(sin->sin_addr.s_addr);
981 struct sockaddr_in oldaddr;
982 struct ifaddr_container *ifac;
983 int flags = RTF_UP, error = 0;
984 int was_hash = 0;
985
986 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
987 oldaddr = ia->ia_addr;
988
989 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
990 was_hash = 1;
991 in_iahash_remove(ia);
992 }
993
994 ia->ia_addr = *sin;
995 if (ia->ia_addr.sin_family == AF_INET)
996 in_iahash_insert(ia);
997
998 /*
999 * Give the interface a chance to initialize
1000 * if this is its first address,
1001 * and to validate the address if necessary.
1002 */
1003 if (ifp->if_ioctl != NULL) {
1004 ifnet_serialize_all(ifp);
1005 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
1006 ifnet_deserialize_all(ifp);
1007 if (error)
1008 goto fail;
1009 }
1010
1011 /*
1012 * Delete old route, if requested.
1013 */
1014 if (scrub) {
1015 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
1016 in_ifscrub(ifp, ia);
1017 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1018 }
1019
1020 /*
1021 * Calculate netmask/subnetmask.
1022 */
1023 if (IN_CLASSA(i))
1024 ia->ia_netmask = IN_CLASSA_NET;
1025 else if (IN_CLASSB(i))
1026 ia->ia_netmask = IN_CLASSB_NET;
1027 else
1028 ia->ia_netmask = IN_CLASSC_NET;
1029 /*
1030 * The subnet mask usually includes at least the standard network part,
1031 * but may be smaller in the case of supernetting.
1032 * If it is set, we believe it.
1033 */
1034 if (ia->ia_subnetmask == 0) {
1035 ia->ia_subnetmask = ia->ia_netmask;
1036 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1037 } else {
1038 ia->ia_netmask &= ia->ia_subnetmask;
1039 }
1040 ia->ia_net = i & ia->ia_netmask;
1041 ia->ia_subnet = i & ia->ia_subnetmask;
1042 in_socktrim(&ia->ia_sockmask);
1043
1044 /*
1045 * Add route for the network.
1046 */
1047 ia->ia_ifa.ifa_metric = ifp->if_metric;
1048 if (ifp->if_flags & IFF_BROADCAST) {
1049 if (ia->ia_subnetmask == IN_RFC3021_MASK) {
1050 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
1051 ia->ia_netbroadcast.s_addr = INADDR_BROADCAST;
1052 } else {
1053 ia->ia_broadaddr.sin_addr.s_addr =
1054 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1055 ia->ia_netbroadcast.s_addr =
1056 htonl(ia->ia_net | ~ia->ia_netmask);
1057 }
1058 } else if (ifp->if_flags & IFF_LOOPBACK) {
1059 ia->ia_dstaddr = ia->ia_addr;
1060 flags |= RTF_HOST;
1061 } else if (ifp->if_flags & IFF_POINTOPOINT) {
1062 if (ia->ia_dstaddr.sin_family != AF_INET)
1063 return (0);
1064 flags |= RTF_HOST;
1065 }
1066
1067 /*
1068 * Ignore the INADDR_ANY address added by DHCP/BOOTP.
1069 */
1070 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY) {
1071 error = in_addprefix(ia, flags);
1072 if (error)
1073 goto fail;
1074 }
1075
1076 /*
1077 * If the interface supports multicast, join the "all hosts"
1078 * multicast group on that interface.
1079 */
1080 if (ifp->if_flags & IFF_MULTICAST) {
1081 struct in_addr addr;
1082
1083 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1084 in_addmulti(&addr, ifp);
1085 }
1086
1087 return (0);
1088
1089 fail:
1090 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1091 in_iahash_remove(ia);
1092 ia->ia_addr = oldaddr;
1093 if (was_hash)
1094 in_iahash_insert(ia);
1095 return (error);
1096 }
1097
1098 #define rtinitflags(x) \
1099 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
1100 ? RTF_HOST : 0)
1101
1102 /*
1103 * Add a route to prefix ("connected route" in cisco terminology).
1104 * Do nothing, if there are some interface addresses with the same
1105 * prefix already. This function assumes that the 'target' parent
1106 * interface is UP.
1107 */
1108 static int
in_addprefix(struct in_ifaddr * target,int flags)1109 in_addprefix(struct in_ifaddr *target, int flags)
1110 {
1111 struct in_ifaddr_container *iac;
1112 struct in_addr prefix, mask;
1113 int error;
1114
1115 #ifdef CARP
1116 /*
1117 * Don't add prefix routes for CARP interfaces.
1118 * Prefix routes creation is handled by CARP
1119 * interfaces themselves.
1120 */
1121 if (target->ia_ifp->if_type == IFT_CARP)
1122 return 0;
1123 #endif
1124
1125 /*
1126 * Add a loopback route to the interface address.
1127 */
1128 if ((target->ia_ifp->if_flags & IFF_LOOPBACK) == 0)
1129 ifa_add_loopback_route(&target->ia_ifa,
1130 (struct sockaddr *)&target->ia_addr);
1131
1132 mask = target->ia_sockmask.sin_addr;
1133 if (flags & RTF_HOST) {
1134 prefix = target->ia_dstaddr.sin_addr;
1135 } else {
1136 prefix = target->ia_addr.sin_addr;
1137 prefix.s_addr &= mask.s_addr;
1138 }
1139
1140 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1141 struct in_ifaddr *ia = iac->ia;
1142 struct in_addr p;
1143
1144 /* Don't test against self */
1145 if (ia == target)
1146 continue;
1147
1148 /* The tested address does not own a route entry */
1149 if ((ia->ia_flags & IFA_ROUTE) == 0)
1150 continue;
1151
1152 /* Prefix test */
1153 if (rtinitflags(ia) != 0) {
1154 p = ia->ia_dstaddr.sin_addr;
1155 } else {
1156 p = ia->ia_addr.sin_addr;
1157 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1158 }
1159 if (prefix.s_addr != p.s_addr)
1160 continue;
1161
1162 /*
1163 * If the to-be-added address and the currently being
1164 * tested address are not host addresses, we need to
1165 * take subnetmask into consideration.
1166 */
1167 if (!(flags & RTF_HOST) && rtinitflags(ia) == 0 &&
1168 mask.s_addr != ia->ia_sockmask.sin_addr.s_addr)
1169 continue;
1170
1171 /*
1172 * If we got a matching prefix route inserted by other
1173 * interface address, we don't need to bother.
1174 */
1175 return 0;
1176 }
1177
1178 /*
1179 * No one seem to have prefix route; insert it.
1180 */
1181 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1182 if (!error)
1183 target->ia_flags |= IFA_ROUTE;
1184 return error;
1185 }
1186
1187 /*
1188 * Remove a route to prefix ("connected route" in cisco terminology).
1189 * Re-installs the route by using another interface address, if there's
1190 * one with the same prefix (otherwise we lose the route mistakenly).
1191 */
1192 static void
in_scrubprefix(struct in_ifaddr * target)1193 in_scrubprefix(struct in_ifaddr *target)
1194 {
1195 struct in_ifaddr_container *iac;
1196 struct in_addr prefix, mask;
1197 int error;
1198
1199 #ifdef CARP
1200 /*
1201 * Don't scrub prefix routes for CARP interfaces.
1202 * Prefix routes deletion is handled by CARP
1203 * interfaces themselves.
1204 */
1205 if (target->ia_ifp->if_type == IFT_CARP)
1206 return;
1207 #endif
1208
1209 /*
1210 * Remove or change the loopback route to the interface address.
1211 */
1212 if (target->ia_addr.sin_addr.s_addr != INADDR_ANY &&
1213 (target->ia_ifp->if_flags & IFF_LOOPBACK) == 0) {
1214 struct in_ifaddr *ia;
1215 struct in_addr addr;
1216
1217 /*
1218 * rtrequest1() doesn't yet support to change a route (i.e.,
1219 * RTM_CHANGE), so always delete the old one and then add
1220 * the new one if needed.
1221 */
1222 ifa_del_loopback_route(&target->ia_ifa,
1223 (struct sockaddr *)&target->ia_addr);
1224
1225 /*
1226 * Check whether the host address is still bound to another
1227 * interface address. If yes, then re-add a loopback route
1228 * for it.
1229 */
1230 addr = target->ia_addr.sin_addr;
1231 LIST_FOREACH(iac, INADDR_HASH(addr.s_addr), ia_hash) {
1232 ia = iac->ia;
1233 if (ia != target &&
1234 ia->ia_addr.sin_addr.s_addr == addr.s_addr) {
1235 IFAREF(&ia->ia_ifa);
1236 ifa_add_loopback_route(&ia->ia_ifa,
1237 (struct sockaddr *)&target->ia_addr);
1238 IFAFREE(&ia->ia_ifa);
1239 break;
1240 }
1241 }
1242 }
1243
1244 if ((target->ia_flags & IFA_ROUTE) == 0)
1245 return;
1246
1247 mask = target->ia_sockmask.sin_addr;
1248 if (rtinitflags(target) != 0) {
1249 prefix = target->ia_dstaddr.sin_addr;
1250 } else {
1251 prefix = target->ia_addr.sin_addr;
1252 prefix.s_addr &= mask.s_addr;
1253 }
1254
1255 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1256 struct in_ifaddr *ia = iac->ia;
1257 struct in_addr p;
1258
1259 /* Don't test against self */
1260 if (ia == target)
1261 continue;
1262
1263 /* The tested address already owns a route entry */
1264 if (ia->ia_flags & IFA_ROUTE)
1265 continue;
1266
1267 /*
1268 * The prefix route of the tested address should
1269 * never be installed if its parent interface is
1270 * not UP yet.
1271 */
1272 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1273 continue;
1274
1275 #ifdef CARP
1276 /*
1277 * Don't add prefix routes for CARP interfaces.
1278 * Prefix routes creation is handled by CARP
1279 * interfaces themselves.
1280 */
1281 if (ia->ia_ifp->if_type == IFT_CARP)
1282 continue;
1283 #endif
1284
1285 /* Prefix test */
1286 if (rtinitflags(ia) != 0) {
1287 p = ia->ia_dstaddr.sin_addr;
1288 } else {
1289 p = ia->ia_addr.sin_addr;
1290 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1291 }
1292 if (prefix.s_addr != p.s_addr)
1293 continue;
1294
1295 /*
1296 * We don't need to test subnetmask here, as what we do
1297 * in in_addprefix(), since if the the tested address's
1298 * parent interface is UP, the tested address should own
1299 * a prefix route entry and we would never reach here.
1300 */
1301
1302 /*
1303 * If we got a matching prefix route, move IFA_ROUTE to him
1304 */
1305 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1306 target->ia_flags &= ~IFA_ROUTE;
1307
1308 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
1309 if (!error)
1310 ia->ia_flags |= IFA_ROUTE;
1311 return;
1312 }
1313
1314 /*
1315 * No candidates for this prefix route; just remove it.
1316 */
1317 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1318 target->ia_flags &= ~IFA_ROUTE;
1319 }
1320
1321 #undef rtinitflags
1322
1323 /*
1324 * Return 1 if the address might be a local broadcast address.
1325 */
1326 int
in_broadcast(struct in_addr in,struct ifnet * ifp)1327 in_broadcast(struct in_addr in, struct ifnet *ifp)
1328 {
1329 struct ifaddr_container *ifac;
1330 u_long t;
1331
1332 if (in.s_addr == INADDR_BROADCAST ||
1333 in.s_addr == INADDR_ANY)
1334 return 1;
1335 if (ifp == NULL || (ifp->if_flags & IFF_BROADCAST) == 0)
1336 return 0;
1337 t = ntohl(in.s_addr);
1338 /*
1339 * Look through the list of addresses for a match
1340 * with a broadcast address.
1341 */
1342 #define ia ((struct in_ifaddr *)ifa)
1343 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1344 struct ifaddr *ifa = ifac->ifa;
1345
1346 if (ifa->ifa_addr->sa_family == AF_INET &&
1347 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1348 in.s_addr == ia->ia_netbroadcast.s_addr ||
1349 /*
1350 * Check for old-style (host 0) broadcast, but
1351 * taking into account that RFC 3021 obsoletes it.
1352 */
1353 (ia->ia_subnetmask != IN_RFC3021_MASK &&
1354 (t == ia->ia_subnet || t == ia->ia_net))) &&
1355 /*
1356 * Check for an all one subnetmask. These
1357 * only exist when an interface gets a secondary
1358 * address.
1359 */
1360 ia->ia_subnetmask != (u_long)0xffffffff)
1361 return 1;
1362 }
1363 return (0);
1364 #undef ia
1365 }
1366
1367 /*
1368 * Add an address to the list of IP multicast addresses for a given interface.
1369 */
1370 struct in_multi *
in_addmulti(struct in_addr * ap,struct ifnet * ifp)1371 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1372 {
1373 struct in_multi *inm;
1374 int error;
1375 struct sockaddr_in sin;
1376 struct ifmultiaddr *ifma;
1377
1378 ASSERT_NETISR0;
1379
1380 /*
1381 * Call generic routine to add membership or increment
1382 * refcount. It wants addresses in the form of a sockaddr,
1383 * so we build one here (being careful to zero the unused bytes).
1384 */
1385 bzero(&sin, sizeof sin);
1386 sin.sin_family = AF_INET;
1387 sin.sin_len = sizeof sin;
1388 sin.sin_addr = *ap;
1389 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1390 if (error)
1391 return NULL;
1392
1393 /*
1394 * If ifma->ifma_protospec is null, then if_addmulti() created
1395 * a new record. Otherwise, we are done.
1396 */
1397 if (ifma->ifma_protospec != NULL)
1398 return ifma->ifma_protospec;
1399
1400 inm = kmalloc(sizeof *inm, M_IPMADDR, M_INTWAIT | M_ZERO);
1401 inm->inm_addr = *ap;
1402 inm->inm_ifp = ifp;
1403 inm->inm_ifma = ifma;
1404 ifma->ifma_protospec = inm;
1405 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1406
1407 /*
1408 * Let IGMP know that we have joined a new IP multicast group.
1409 */
1410 igmp_joingroup(inm);
1411 return inm;
1412 }
1413
1414 /*
1415 * Delete a multicast address record.
1416 */
1417 void
in_delmulti(struct in_multi * inm)1418 in_delmulti(struct in_multi *inm)
1419 {
1420 struct ifmultiaddr *ifma;
1421 struct in_multi my_inm;
1422
1423 ASSERT_NETISR0;
1424
1425 ifma = inm->inm_ifma;
1426 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1427 if (ifma->ifma_refcount == 1) {
1428 /*
1429 * No remaining claims to this record; let IGMP know that
1430 * we are leaving the multicast group.
1431 * But do it after the if_delmulti() which might reset
1432 * the interface and nuke the packet.
1433 */
1434 my_inm = *inm ;
1435 ifma->ifma_protospec = NULL;
1436 LIST_REMOVE(inm, inm_link);
1437 kfree(inm, M_IPMADDR);
1438 }
1439 /* XXX - should be separate API for when we have an ifma? */
1440 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1441 if (my_inm.inm_ifp != NULL)
1442 igmp_leavegroup(&my_inm);
1443 }
1444
1445 void
in_if_down(struct ifnet * ifp)1446 in_if_down(struct ifnet *ifp)
1447 {
1448 rt_purgecloned(ifp, AF_INET);
1449 }
1450
1451 static void
in_ifdetach_dispatch(netmsg_t nmsg)1452 in_ifdetach_dispatch(netmsg_t nmsg)
1453 {
1454 struct lwkt_msg *lmsg = &nmsg->lmsg;
1455 struct ifnet *ifp = lmsg->u.ms_resultp;
1456 int cpu;
1457
1458 in_pcbpurgeif0(&ripcbinfo, ifp);
1459 for (cpu = 0; cpu < netisr_ncpus; ++cpu)
1460 in_pcbpurgeif0(&udbinfo[cpu], ifp);
1461
1462 lwkt_replymsg(lmsg, 0);
1463 }
1464
1465 void
in_ifdetach(struct ifnet * ifp)1466 in_ifdetach(struct ifnet *ifp)
1467 {
1468 struct netmsg_base nmsg;
1469 struct lwkt_msg *lmsg = &nmsg.lmsg;
1470
1471 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
1472 in_ifdetach_dispatch);
1473 lmsg->u.ms_resultp = ifp;
1474
1475 lwkt_domsg(netisr_cpuport(0), lmsg, 0);
1476 }
1477