FreeBSD/Linux Kernel Cross Reference
sys/netinet/in.c
1 /*-
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (C) 2001 WIDE Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * @(#)in.c 8.4 (Berkeley) 1/9/95
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/8.1/sys/netinet/in.c 209524 2010-06-25 21:26:34Z qingli $");
35
36 #include "opt_carp.h"
37 #include "opt_mpath.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/malloc.h>
43 #include <sys/priv.h>
44 #include <sys/socket.h>
45 #include <sys/jail.h>
46 #include <sys/kernel.h>
47 #include <sys/proc.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_dl.h>
54 #include <net/if_llatbl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
57 #include <net/vnet.h>
58
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/igmp_var.h>
64 #include <netinet/udp.h>
65 #include <netinet/udp_var.h>
66
67 static int in_mask2len(struct in_addr *);
68 static void in_len2mask(struct in_addr *, int);
69 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
70 struct ifnet *, struct thread *);
71
72 static int in_addprefix(struct in_ifaddr *, int);
73 static int in_scrubprefix(struct in_ifaddr *);
74 static void in_socktrim(struct sockaddr_in *);
75 static int in_ifinit(struct ifnet *,
76 struct in_ifaddr *, struct sockaddr_in *, int);
77 static void in_purgemaddrs(struct ifnet *);
78
79 static VNET_DEFINE(int, subnetsarelocal);
80 #define V_subnetsarelocal VNET(subnetsarelocal)
81 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
82 &VNET_NAME(subnetsarelocal), 0,
83 "Treat all subnets as directly connected");
84 static VNET_DEFINE(int, sameprefixcarponly);
85 #define V_sameprefixcarponly VNET(sameprefixcarponly)
86 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
87 &VNET_NAME(sameprefixcarponly), 0,
88 "Refuse to create same prefixes on different interfaces");
89
90 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
91 #define V_ripcbinfo VNET(ripcbinfo)
92
93 /*
94 * Return 1 if an internet address is for a ``local'' host
95 * (one to which we have a connection). If subnetsarelocal
96 * is true, this includes other subnets of the local net.
97 * Otherwise, it includes only the directly-connected (sub)nets.
98 */
99 int
100 in_localaddr(struct in_addr in)
101 {
102 register u_long i = ntohl(in.s_addr);
103 register struct in_ifaddr *ia;
104
105 IN_IFADDR_RLOCK();
106 if (V_subnetsarelocal) {
107 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
108 if ((i & ia->ia_netmask) == ia->ia_net) {
109 IN_IFADDR_RUNLOCK();
110 return (1);
111 }
112 }
113 } else {
114 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
115 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
116 IN_IFADDR_RUNLOCK();
117 return (1);
118 }
119 }
120 }
121 IN_IFADDR_RUNLOCK();
122 return (0);
123 }
124
125 /*
126 * Return 1 if an internet address is for the local host and configured
127 * on one of its interfaces.
128 */
129 int
130 in_localip(struct in_addr in)
131 {
132 struct in_ifaddr *ia;
133
134 IN_IFADDR_RLOCK();
135 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
136 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
137 IN_IFADDR_RUNLOCK();
138 return (1);
139 }
140 }
141 IN_IFADDR_RUNLOCK();
142 return (0);
143 }
144
145 /*
146 * Determine whether an IP address is in a reserved set of addresses
147 * that may not be forwarded, or whether datagrams to that destination
148 * may be forwarded.
149 */
150 int
151 in_canforward(struct in_addr in)
152 {
153 register u_long i = ntohl(in.s_addr);
154 register u_long net;
155
156 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
157 return (0);
158 if (IN_CLASSA(i)) {
159 net = i & IN_CLASSA_NET;
160 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
161 return (0);
162 }
163 return (1);
164 }
165
166 /*
167 * Trim a mask in a sockaddr
168 */
169 static void
170 in_socktrim(struct sockaddr_in *ap)
171 {
172 register char *cplim = (char *) &ap->sin_addr;
173 register char *cp = (char *) (&ap->sin_addr + 1);
174
175 ap->sin_len = 0;
176 while (--cp >= cplim)
177 if (*cp) {
178 (ap)->sin_len = cp - (char *) (ap) + 1;
179 break;
180 }
181 }
182
183 static int
184 in_mask2len(mask)
185 struct in_addr *mask;
186 {
187 int x, y;
188 u_char *p;
189
190 p = (u_char *)mask;
191 for (x = 0; x < sizeof(*mask); x++) {
192 if (p[x] != 0xff)
193 break;
194 }
195 y = 0;
196 if (x < sizeof(*mask)) {
197 for (y = 0; y < 8; y++) {
198 if ((p[x] & (0x80 >> y)) == 0)
199 break;
200 }
201 }
202 return (x * 8 + y);
203 }
204
205 static void
206 in_len2mask(struct in_addr *mask, int len)
207 {
208 int i;
209 u_char *p;
210
211 p = (u_char *)mask;
212 bzero(mask, sizeof(*mask));
213 for (i = 0; i < len / 8; i++)
214 p[i] = 0xff;
215 if (len % 8)
216 p[i] = (0xff00 >> (len % 8)) & 0xff;
217 }
218
219 /*
220 * Generic internet control operations (ioctl's).
221 *
222 * ifp is NULL if not an interface-specific ioctl.
223 */
224 /* ARGSUSED */
225 int
226 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
227 struct thread *td)
228 {
229 register struct ifreq *ifr = (struct ifreq *)data;
230 register struct in_ifaddr *ia, *iap;
231 register struct ifaddr *ifa;
232 struct in_addr allhosts_addr;
233 struct in_addr dst;
234 struct in_ifinfo *ii;
235 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
236 struct sockaddr_in oldaddr;
237 int error, hostIsNew, iaIsNew, maskIsNew;
238 int iaIsFirst;
239
240 ia = NULL;
241 iaIsFirst = 0;
242 iaIsNew = 0;
243 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
244
245 /*
246 * Filter out ioctls we implement directly; forward the rest on to
247 * in_lifaddr_ioctl() and ifp->if_ioctl().
248 */
249 switch (cmd) {
250 case SIOCAIFADDR:
251 case SIOCDIFADDR:
252 case SIOCGIFADDR:
253 case SIOCGIFBRDADDR:
254 case SIOCGIFDSTADDR:
255 case SIOCGIFNETMASK:
256 case SIOCSIFADDR:
257 case SIOCSIFBRDADDR:
258 case SIOCSIFDSTADDR:
259 case SIOCSIFNETMASK:
260 break;
261
262 case SIOCALIFADDR:
263 if (td != NULL) {
264 error = priv_check(td, PRIV_NET_ADDIFADDR);
265 if (error)
266 return (error);
267 }
268 if (ifp == NULL)
269 return (EINVAL);
270 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
271
272 case SIOCDLIFADDR:
273 if (td != NULL) {
274 error = priv_check(td, PRIV_NET_DELIFADDR);
275 if (error)
276 return (error);
277 }
278 if (ifp == NULL)
279 return (EINVAL);
280 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
281
282 case SIOCGLIFADDR:
283 if (ifp == NULL)
284 return (EINVAL);
285 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
286
287 default:
288 if (ifp == NULL || ifp->if_ioctl == NULL)
289 return (EOPNOTSUPP);
290 return ((*ifp->if_ioctl)(ifp, cmd, data));
291 }
292
293 if (ifp == NULL)
294 return (EADDRNOTAVAIL);
295
296 /*
297 * Security checks before we get involved in any work.
298 */
299 switch (cmd) {
300 case SIOCAIFADDR:
301 case SIOCSIFADDR:
302 case SIOCSIFBRDADDR:
303 case SIOCSIFNETMASK:
304 case SIOCSIFDSTADDR:
305 if (td != NULL) {
306 error = priv_check(td, PRIV_NET_ADDIFADDR);
307 if (error)
308 return (error);
309 }
310 break;
311
312 case SIOCDIFADDR:
313 if (td != NULL) {
314 error = priv_check(td, PRIV_NET_DELIFADDR);
315 if (error)
316 return (error);
317 }
318 break;
319 }
320
321 /*
322 * Find address for this interface, if it exists.
323 *
324 * If an alias address was specified, find that one instead of the
325 * first one on the interface, if possible.
326 */
327 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
328 IN_IFADDR_RLOCK();
329 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
330 if (iap->ia_ifp == ifp &&
331 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
332 if (td == NULL || prison_check_ip4(td->td_ucred,
333 &dst) == 0)
334 ia = iap;
335 break;
336 }
337 }
338 if (ia != NULL)
339 ifa_ref(&ia->ia_ifa);
340 IN_IFADDR_RUNLOCK();
341 if (ia == NULL) {
342 IF_ADDR_LOCK(ifp);
343 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
344 iap = ifatoia(ifa);
345 if (iap->ia_addr.sin_family == AF_INET) {
346 if (td != NULL &&
347 prison_check_ip4(td->td_ucred,
348 &iap->ia_addr.sin_addr) != 0)
349 continue;
350 ia = iap;
351 break;
352 }
353 }
354 if (ia != NULL)
355 ifa_ref(&ia->ia_ifa);
356 IF_ADDR_UNLOCK(ifp);
357 }
358 if (ia == NULL)
359 iaIsFirst = 1;
360
361 error = 0;
362 switch (cmd) {
363 case SIOCAIFADDR:
364 case SIOCDIFADDR:
365 if (ifra->ifra_addr.sin_family == AF_INET) {
366 struct in_ifaddr *oia;
367
368 IN_IFADDR_RLOCK();
369 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
370 if (ia->ia_ifp == ifp &&
371 ia->ia_addr.sin_addr.s_addr ==
372 ifra->ifra_addr.sin_addr.s_addr)
373 break;
374 }
375 if (ia != NULL && ia != oia)
376 ifa_ref(&ia->ia_ifa);
377 if (oia != NULL && ia != oia)
378 ifa_free(&oia->ia_ifa);
379 IN_IFADDR_RUNLOCK();
380 if ((ifp->if_flags & IFF_POINTOPOINT)
381 && (cmd == SIOCAIFADDR)
382 && (ifra->ifra_dstaddr.sin_addr.s_addr
383 == INADDR_ANY)) {
384 error = EDESTADDRREQ;
385 goto out;
386 }
387 }
388 if (cmd == SIOCDIFADDR && ia == NULL) {
389 error = EADDRNOTAVAIL;
390 goto out;
391 }
392 /* FALLTHROUGH */
393 case SIOCSIFADDR:
394 case SIOCSIFNETMASK:
395 case SIOCSIFDSTADDR:
396 if (ia == NULL) {
397 ia = (struct in_ifaddr *)
398 malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
399 M_ZERO);
400 if (ia == NULL) {
401 error = ENOBUFS;
402 goto out;
403 }
404
405 ifa = &ia->ia_ifa;
406 ifa_init(ifa);
407 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
408 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
409 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
410
411 ia->ia_sockmask.sin_len = 8;
412 ia->ia_sockmask.sin_family = AF_INET;
413 if (ifp->if_flags & IFF_BROADCAST) {
414 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
415 ia->ia_broadaddr.sin_family = AF_INET;
416 }
417 ia->ia_ifp = ifp;
418
419 ifa_ref(ifa); /* if_addrhead */
420 IF_ADDR_LOCK(ifp);
421 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
422 IF_ADDR_UNLOCK(ifp);
423 ifa_ref(ifa); /* in_ifaddrhead */
424 IN_IFADDR_WLOCK();
425 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
426 IN_IFADDR_WUNLOCK();
427 iaIsNew = 1;
428 }
429 break;
430
431 case SIOCSIFBRDADDR:
432 case SIOCGIFADDR:
433 case SIOCGIFNETMASK:
434 case SIOCGIFDSTADDR:
435 case SIOCGIFBRDADDR:
436 if (ia == NULL) {
437 error = EADDRNOTAVAIL;
438 goto out;
439 }
440 break;
441 }
442
443 /*
444 * Most paths in this switch return directly or via out. Only paths
445 * that remove the address break in order to hit common removal code.
446 */
447 switch (cmd) {
448 case SIOCGIFADDR:
449 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
450 goto out;
451
452 case SIOCGIFBRDADDR:
453 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
454 error = EINVAL;
455 goto out;
456 }
457 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
458 goto out;
459
460 case SIOCGIFDSTADDR:
461 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
462 error = EINVAL;
463 goto out;
464 }
465 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
466 goto out;
467
468 case SIOCGIFNETMASK:
469 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
470 goto out;
471
472 case SIOCSIFDSTADDR:
473 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
474 error = EINVAL;
475 goto out;
476 }
477 oldaddr = ia->ia_dstaddr;
478 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
479 if (ifp->if_ioctl != NULL) {
480 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
481 (caddr_t)ia);
482 if (error) {
483 ia->ia_dstaddr = oldaddr;
484 goto out;
485 }
486 }
487 if (ia->ia_flags & IFA_ROUTE) {
488 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
489 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
490 ia->ia_ifa.ifa_dstaddr =
491 (struct sockaddr *)&ia->ia_dstaddr;
492 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
493 }
494 goto out;
495
496 case SIOCSIFBRDADDR:
497 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
498 error = EINVAL;
499 goto out;
500 }
501 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
502 goto out;
503
504 case SIOCSIFADDR:
505 error = in_ifinit(ifp, ia,
506 (struct sockaddr_in *) &ifr->ifr_addr, 1);
507 if (error != 0 && iaIsNew)
508 break;
509 if (error == 0) {
510 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
511 if (iaIsFirst &&
512 (ifp->if_flags & IFF_MULTICAST) != 0) {
513 error = in_joingroup(ifp, &allhosts_addr,
514 NULL, &ii->ii_allhosts);
515 }
516 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
517 }
518 error = 0;
519 goto out;
520
521 case SIOCSIFNETMASK:
522 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
523 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
524 goto out;
525
526 case SIOCAIFADDR:
527 maskIsNew = 0;
528 hostIsNew = 1;
529 error = 0;
530 if (ia->ia_addr.sin_family == AF_INET) {
531 if (ifra->ifra_addr.sin_len == 0) {
532 ifra->ifra_addr = ia->ia_addr;
533 hostIsNew = 0;
534 } else if (ifra->ifra_addr.sin_addr.s_addr ==
535 ia->ia_addr.sin_addr.s_addr)
536 hostIsNew = 0;
537 }
538 if (ifra->ifra_mask.sin_len) {
539 /*
540 * QL: XXX
541 * Need to scrub the prefix here in case
542 * the issued command is SIOCAIFADDR with
543 * the same address, but with a different
544 * prefix length. And if the prefix length
545 * is the same as before, then the call is
546 * un-necessarily executed here.
547 */
548 in_ifscrub(ifp, ia);
549 ia->ia_sockmask = ifra->ifra_mask;
550 ia->ia_sockmask.sin_family = AF_INET;
551 ia->ia_subnetmask =
552 ntohl(ia->ia_sockmask.sin_addr.s_addr);
553 maskIsNew = 1;
554 }
555 if ((ifp->if_flags & IFF_POINTOPOINT) &&
556 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
557 in_ifscrub(ifp, ia);
558 ia->ia_dstaddr = ifra->ifra_dstaddr;
559 maskIsNew = 1; /* We lie; but the effect's the same */
560 }
561 if (ifra->ifra_addr.sin_family == AF_INET &&
562 (hostIsNew || maskIsNew))
563 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
564 if (error != 0 && iaIsNew)
565 goto out;
566
567 if ((ifp->if_flags & IFF_BROADCAST) &&
568 (ifra->ifra_broadaddr.sin_family == AF_INET))
569 ia->ia_broadaddr = ifra->ifra_broadaddr;
570 if (error == 0) {
571 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
572 if (iaIsFirst &&
573 (ifp->if_flags & IFF_MULTICAST) != 0) {
574 error = in_joingroup(ifp, &allhosts_addr,
575 NULL, &ii->ii_allhosts);
576 }
577 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
578 }
579 goto out;
580
581 case SIOCDIFADDR:
582 /*
583 * in_ifscrub kills the interface route.
584 */
585 in_ifscrub(ifp, ia);
586
587 /*
588 * in_ifadown gets rid of all the rest of
589 * the routes. This is not quite the right
590 * thing to do, but at least if we are running
591 * a routing process they will come back.
592 */
593 in_ifadown(&ia->ia_ifa, 1);
594 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
595 error = 0;
596 break;
597
598 default:
599 panic("in_control: unsupported ioctl");
600 }
601
602 IF_ADDR_LOCK(ifp);
603 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
604 IF_ADDR_UNLOCK(ifp);
605 ifa_free(&ia->ia_ifa); /* if_addrhead */
606
607 IN_IFADDR_WLOCK();
608 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
609 if (ia->ia_addr.sin_family == AF_INET) {
610 struct in_ifaddr *if_ia;
611
612 LIST_REMOVE(ia, ia_hash);
613 IN_IFADDR_WUNLOCK();
614 /*
615 * If this is the last IPv4 address configured on this
616 * interface, leave the all-hosts group.
617 * No state-change report need be transmitted.
618 */
619 if_ia = NULL;
620 IFP_TO_IA(ifp, if_ia);
621 if (if_ia == NULL) {
622 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
623 IN_MULTI_LOCK();
624 if (ii->ii_allhosts) {
625 (void)in_leavegroup_locked(ii->ii_allhosts,
626 NULL);
627 ii->ii_allhosts = NULL;
628 }
629 IN_MULTI_UNLOCK();
630 } else
631 ifa_free(&if_ia->ia_ifa);
632 } else
633 IN_IFADDR_WUNLOCK();
634 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
635 out:
636 if (ia != NULL)
637 ifa_free(&ia->ia_ifa);
638 return (error);
639 }
640
641 /*
642 * SIOC[GAD]LIFADDR.
643 * SIOCGLIFADDR: get first address. (?!?)
644 * SIOCGLIFADDR with IFLR_PREFIX:
645 * get first address that matches the specified prefix.
646 * SIOCALIFADDR: add the specified address.
647 * SIOCALIFADDR with IFLR_PREFIX:
648 * EINVAL since we can't deduce hostid part of the address.
649 * SIOCDLIFADDR: delete the specified address.
650 * SIOCDLIFADDR with IFLR_PREFIX:
651 * delete the first address that matches the specified prefix.
652 * return values:
653 * EINVAL on invalid parameters
654 * EADDRNOTAVAIL on prefix match failed/specified address not found
655 * other values may be returned from in_ioctl()
656 */
657 static int
658 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
659 struct ifnet *ifp, struct thread *td)
660 {
661 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
662 struct ifaddr *ifa;
663
664 /* sanity checks */
665 if (data == NULL || ifp == NULL) {
666 panic("invalid argument to in_lifaddr_ioctl");
667 /*NOTRECHED*/
668 }
669
670 switch (cmd) {
671 case SIOCGLIFADDR:
672 /* address must be specified on GET with IFLR_PREFIX */
673 if ((iflr->flags & IFLR_PREFIX) == 0)
674 break;
675 /*FALLTHROUGH*/
676 case SIOCALIFADDR:
677 case SIOCDLIFADDR:
678 /* address must be specified on ADD and DELETE */
679 if (iflr->addr.ss_family != AF_INET)
680 return (EINVAL);
681 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
682 return (EINVAL);
683 /* XXX need improvement */
684 if (iflr->dstaddr.ss_family
685 && iflr->dstaddr.ss_family != AF_INET)
686 return (EINVAL);
687 if (iflr->dstaddr.ss_family
688 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
689 return (EINVAL);
690 break;
691 default: /*shouldn't happen*/
692 return (EOPNOTSUPP);
693 }
694 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
695 return (EINVAL);
696
697 switch (cmd) {
698 case SIOCALIFADDR:
699 {
700 struct in_aliasreq ifra;
701
702 if (iflr->flags & IFLR_PREFIX)
703 return (EINVAL);
704
705 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
706 bzero(&ifra, sizeof(ifra));
707 bcopy(iflr->iflr_name, ifra.ifra_name,
708 sizeof(ifra.ifra_name));
709
710 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
711
712 if (iflr->dstaddr.ss_family) { /*XXX*/
713 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
714 iflr->dstaddr.ss_len);
715 }
716
717 ifra.ifra_mask.sin_family = AF_INET;
718 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
719 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
720
721 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
722 }
723 case SIOCGLIFADDR:
724 case SIOCDLIFADDR:
725 {
726 struct in_ifaddr *ia;
727 struct in_addr mask, candidate, match;
728 struct sockaddr_in *sin;
729
730 bzero(&mask, sizeof(mask));
731 bzero(&match, sizeof(match));
732 if (iflr->flags & IFLR_PREFIX) {
733 /* lookup a prefix rather than address. */
734 in_len2mask(&mask, iflr->prefixlen);
735
736 sin = (struct sockaddr_in *)&iflr->addr;
737 match.s_addr = sin->sin_addr.s_addr;
738 match.s_addr &= mask.s_addr;
739
740 /* if you set extra bits, that's wrong */
741 if (match.s_addr != sin->sin_addr.s_addr)
742 return (EINVAL);
743
744 } else {
745 /* on getting an address, take the 1st match */
746 /* on deleting an address, do exact match */
747 if (cmd != SIOCGLIFADDR) {
748 in_len2mask(&mask, 32);
749 sin = (struct sockaddr_in *)&iflr->addr;
750 match.s_addr = sin->sin_addr.s_addr;
751 }
752 }
753
754 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
755 if (ifa->ifa_addr->sa_family != AF_INET6)
756 continue;
757 if (match.s_addr == 0)
758 break;
759 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
760 candidate.s_addr &= mask.s_addr;
761 if (candidate.s_addr == match.s_addr)
762 break;
763 }
764 if (ifa == NULL)
765 return (EADDRNOTAVAIL);
766 ia = (struct in_ifaddr *)ifa;
767
768 if (cmd == SIOCGLIFADDR) {
769 /* fill in the if_laddrreq structure */
770 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
771
772 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
773 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
774 ia->ia_dstaddr.sin_len);
775 } else
776 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
777
778 iflr->prefixlen =
779 in_mask2len(&ia->ia_sockmask.sin_addr);
780
781 iflr->flags = 0; /*XXX*/
782
783 return (0);
784 } else {
785 struct in_aliasreq ifra;
786
787 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
788 bzero(&ifra, sizeof(ifra));
789 bcopy(iflr->iflr_name, ifra.ifra_name,
790 sizeof(ifra.ifra_name));
791
792 bcopy(&ia->ia_addr, &ifra.ifra_addr,
793 ia->ia_addr.sin_len);
794 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
795 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
796 ia->ia_dstaddr.sin_len);
797 }
798 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
799 ia->ia_sockmask.sin_len);
800
801 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
802 ifp, td));
803 }
804 }
805 }
806
807 return (EOPNOTSUPP); /*just for safety*/
808 }
809
810 /*
811 * Delete any existing route for an interface.
812 */
813 void
814 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
815 {
816
817 in_scrubprefix(ia);
818 }
819
820 /*
821 * Initialize an interface's internet address
822 * and routing table entry.
823 */
824 static int
825 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
826 int scrub)
827 {
828 register u_long i = ntohl(sin->sin_addr.s_addr);
829 struct sockaddr_in oldaddr;
830 int s = splimp(), flags = RTF_UP, error = 0;
831
832 oldaddr = ia->ia_addr;
833 if (oldaddr.sin_family == AF_INET)
834 LIST_REMOVE(ia, ia_hash);
835 ia->ia_addr = *sin;
836 if (ia->ia_addr.sin_family == AF_INET) {
837 IN_IFADDR_WLOCK();
838 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
839 ia, ia_hash);
840 IN_IFADDR_WUNLOCK();
841 }
842 /*
843 * Give the interface a chance to initialize
844 * if this is its first address,
845 * and to validate the address if necessary.
846 */
847 if (ifp->if_ioctl != NULL) {
848 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
849 if (error) {
850 splx(s);
851 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
852 ia->ia_addr = oldaddr;
853 IN_IFADDR_WLOCK();
854 if (ia->ia_addr.sin_family == AF_INET)
855 LIST_INSERT_HEAD(INADDR_HASH(
856 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
857 else
858 /*
859 * If oldaddr family is not AF_INET (e.g.
860 * interface has been just created) in_control
861 * does not call LIST_REMOVE, and we end up
862 * with bogus ia entries in hash
863 */
864 LIST_REMOVE(ia, ia_hash);
865 IN_IFADDR_WUNLOCK();
866 return (error);
867 }
868 }
869 splx(s);
870 if (scrub) {
871 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
872 in_ifscrub(ifp, ia);
873 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
874 }
875 if (IN_CLASSA(i))
876 ia->ia_netmask = IN_CLASSA_NET;
877 else if (IN_CLASSB(i))
878 ia->ia_netmask = IN_CLASSB_NET;
879 else
880 ia->ia_netmask = IN_CLASSC_NET;
881 /*
882 * The subnet mask usually includes at least the standard network part,
883 * but may may be smaller in the case of supernetting.
884 * If it is set, we believe it.
885 */
886 if (ia->ia_subnetmask == 0) {
887 ia->ia_subnetmask = ia->ia_netmask;
888 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
889 } else
890 ia->ia_netmask &= ia->ia_subnetmask;
891 ia->ia_net = i & ia->ia_netmask;
892 ia->ia_subnet = i & ia->ia_subnetmask;
893 in_socktrim(&ia->ia_sockmask);
894 #ifdef DEV_CARP
895 /*
896 * XXX: carp(4) does not have interface route
897 */
898 if (ifp->if_type == IFT_CARP)
899 return (0);
900 #endif
901 /*
902 * Add route for the network.
903 */
904 ia->ia_ifa.ifa_metric = ifp->if_metric;
905 if (ifp->if_flags & IFF_BROADCAST) {
906 ia->ia_broadaddr.sin_addr.s_addr =
907 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
908 ia->ia_netbroadcast.s_addr =
909 htonl(ia->ia_net | ~ ia->ia_netmask);
910 } else if (ifp->if_flags & IFF_LOOPBACK) {
911 ia->ia_dstaddr = ia->ia_addr;
912 flags |= RTF_HOST;
913 } else if (ifp->if_flags & IFF_POINTOPOINT) {
914 if (ia->ia_dstaddr.sin_family != AF_INET)
915 return (0);
916 flags |= RTF_HOST;
917 }
918 if ((error = in_addprefix(ia, flags)) != 0)
919 return (error);
920
921 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
922 return (0);
923
924 if (ifp->if_flags & IFF_POINTOPOINT) {
925 if (ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
926 return (0);
927 }
928
929
930 /*
931 * add a loopback route to self
932 */
933 if (V_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) {
934 struct route ia_ro;
935
936 bzero(&ia_ro, sizeof(ia_ro));
937 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
938 rtalloc_ign_fib(&ia_ro, 0, 0);
939 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
940 (ia_ro.ro_rt->rt_ifp == V_loif)) {
941 RT_LOCK(ia_ro.ro_rt);
942 RT_ADDREF(ia_ro.ro_rt);
943 RTFREE_LOCKED(ia_ro.ro_rt);
944 } else
945 error = ifa_add_loopback_route((struct ifaddr *)ia,
946 (struct sockaddr *)&ia->ia_addr);
947 if (error == 0)
948 ia->ia_flags |= IFA_RTSELF;
949 if (ia_ro.ro_rt != NULL)
950 RTFREE(ia_ro.ro_rt);
951 }
952
953 return (error);
954 }
955
956 #define rtinitflags(x) \
957 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
958 ? RTF_HOST : 0)
959
960 /*
961 * Generate a routing message when inserting or deleting
962 * an interface address alias.
963 */
964 static void in_addralias_rtmsg(int cmd, struct in_addr *prefix,
965 struct in_ifaddr *target)
966 {
967 struct route pfx_ro;
968 struct sockaddr_in *pfx_addr;
969 struct rtentry msg_rt;
970
971 /* QL: XXX
972 * This is a bit questionable because there is no
973 * additional route entry added/deleted for an address
974 * alias. Therefore this route report is inaccurate.
975 */
976 bzero(&pfx_ro, sizeof(pfx_ro));
977 pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst);
978 pfx_addr->sin_len = sizeof(*pfx_addr);
979 pfx_addr->sin_family = AF_INET;
980 pfx_addr->sin_addr = *prefix;
981 rtalloc_ign_fib(&pfx_ro, 0, 0);
982 if (pfx_ro.ro_rt != NULL) {
983 msg_rt = *pfx_ro.ro_rt;
984
985 /* QL: XXX
986 * Point the gateway to the new interface
987 * address as if a new prefix route entry has
988 * been added through the new address alias.
989 * All other parts of the rtentry is accurate,
990 * e.g., rt_key, rt_mask, rt_ifp etc.
991 */
992 msg_rt.rt_gateway =
993 (struct sockaddr *)&target->ia_addr;
994 rt_newaddrmsg(cmd,
995 (struct ifaddr *)target,
996 0, &msg_rt);
997 RTFREE(pfx_ro.ro_rt);
998 }
999 return;
1000 }
1001
1002 /*
1003 * Check if we have a route for the given prefix already or add one accordingly.
1004 */
1005 static int
1006 in_addprefix(struct in_ifaddr *target, int flags)
1007 {
1008 struct in_ifaddr *ia;
1009 struct in_addr prefix, mask, p, m;
1010 int error;
1011
1012 if ((flags & RTF_HOST) != 0) {
1013 prefix = target->ia_dstaddr.sin_addr;
1014 mask.s_addr = 0;
1015 } else {
1016 prefix = target->ia_addr.sin_addr;
1017 mask = target->ia_sockmask.sin_addr;
1018 prefix.s_addr &= mask.s_addr;
1019 }
1020
1021 IN_IFADDR_RLOCK();
1022 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1023 if (rtinitflags(ia)) {
1024 p = ia->ia_addr.sin_addr;
1025
1026 if (prefix.s_addr != p.s_addr)
1027 continue;
1028 } else {
1029 p = ia->ia_addr.sin_addr;
1030 m = ia->ia_sockmask.sin_addr;
1031 p.s_addr &= m.s_addr;
1032
1033 if (prefix.s_addr != p.s_addr ||
1034 mask.s_addr != m.s_addr)
1035 continue;
1036 }
1037
1038 /*
1039 * If we got a matching prefix route inserted by other
1040 * interface address, we are done here.
1041 */
1042 if (ia->ia_flags & IFA_ROUTE) {
1043 #ifdef RADIX_MPATH
1044 if (ia->ia_addr.sin_addr.s_addr ==
1045 target->ia_addr.sin_addr.s_addr)
1046 return (EEXIST);
1047 else
1048 break;
1049 #endif
1050 if (V_sameprefixcarponly &&
1051 target->ia_ifp->if_type != IFT_CARP &&
1052 ia->ia_ifp->if_type != IFT_CARP) {
1053 IN_IFADDR_RUNLOCK();
1054 return (EEXIST);
1055 } else {
1056 in_addralias_rtmsg(RTM_ADD, &prefix, target);
1057 IN_IFADDR_RUNLOCK();
1058 return (0);
1059 }
1060 }
1061 }
1062 IN_IFADDR_RUNLOCK();
1063
1064 /*
1065 * No-one seem to have this prefix route, so we try to insert it.
1066 */
1067 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
1068 if (!error)
1069 target->ia_flags |= IFA_ROUTE;
1070 return (error);
1071 }
1072
1073 extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
1074
1075 /*
1076 * If there is no other address in the system that can serve a route to the
1077 * same prefix, remove the route. Hand over the route to the new address
1078 * otherwise.
1079 */
1080 static int
1081 in_scrubprefix(struct in_ifaddr *target)
1082 {
1083 struct in_ifaddr *ia;
1084 struct in_addr prefix, mask, p;
1085 int error = 0;
1086 struct sockaddr_in prefix0, mask0;
1087
1088 /*
1089 * Remove the loopback route to the interface address.
1090 * The "useloopback" setting is not consulted because if the
1091 * user configures an interface address, turns off this
1092 * setting, and then tries to delete that interface address,
1093 * checking the current setting of "useloopback" would leave
1094 * that interface address loopback route untouched, which
1095 * would be wrong. Therefore the interface address loopback route
1096 * deletion is unconditional.
1097 */
1098 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1099 !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
1100 (target->ia_flags & IFA_RTSELF)) {
1101 struct route ia_ro;
1102 int freeit = 0;
1103
1104 bzero(&ia_ro, sizeof(ia_ro));
1105 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
1106 rtalloc_ign_fib(&ia_ro, 0, 0);
1107 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
1108 (ia_ro.ro_rt->rt_ifp == V_loif)) {
1109 RT_LOCK(ia_ro.ro_rt);
1110 if (ia_ro.ro_rt->rt_refcnt <= 1)
1111 freeit = 1;
1112 else
1113 RT_REMREF(ia_ro.ro_rt);
1114 RTFREE_LOCKED(ia_ro.ro_rt);
1115 }
1116 if (freeit)
1117 error = ifa_del_loopback_route((struct ifaddr *)target,
1118 (struct sockaddr *)&target->ia_addr);
1119 if (error == 0)
1120 target->ia_flags &= ~IFA_RTSELF;
1121 /* remove arp cache */
1122 arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1123 }
1124
1125 if (rtinitflags(target))
1126 prefix = target->ia_dstaddr.sin_addr;
1127 else {
1128 prefix = target->ia_addr.sin_addr;
1129 mask = target->ia_sockmask.sin_addr;
1130 prefix.s_addr &= mask.s_addr;
1131 }
1132
1133 if ((target->ia_flags & IFA_ROUTE) == 0) {
1134 in_addralias_rtmsg(RTM_DELETE, &prefix, target);
1135 return (0);
1136 }
1137
1138 IN_IFADDR_RLOCK();
1139 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1140 if (rtinitflags(ia))
1141 p = ia->ia_dstaddr.sin_addr;
1142 else {
1143 p = ia->ia_addr.sin_addr;
1144 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1145 }
1146
1147 if (prefix.s_addr != p.s_addr)
1148 continue;
1149
1150 /*
1151 * If we got a matching prefix address, move IFA_ROUTE and
1152 * the route itself to it. Make sure that routing daemons
1153 * get a heads-up.
1154 *
1155 * XXX: a special case for carp(4) interface
1156 */
1157 if ((ia->ia_flags & IFA_ROUTE) == 0
1158 #ifdef DEV_CARP
1159 && (ia->ia_ifp->if_type != IFT_CARP)
1160 #endif
1161 ) {
1162 IN_IFADDR_RUNLOCK();
1163 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1164 rtinitflags(target));
1165 target->ia_flags &= ~IFA_ROUTE;
1166
1167 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1168 rtinitflags(ia) | RTF_UP);
1169 if (error == 0)
1170 ia->ia_flags |= IFA_ROUTE;
1171 return (error);
1172 }
1173 }
1174 IN_IFADDR_RUNLOCK();
1175
1176 /*
1177 * remove all L2 entries on the given prefix
1178 */
1179 bzero(&prefix0, sizeof(prefix0));
1180 prefix0.sin_len = sizeof(prefix0);
1181 prefix0.sin_family = AF_INET;
1182 prefix0.sin_addr.s_addr = target->ia_subnet;
1183 bzero(&mask0, sizeof(mask0));
1184 mask0.sin_len = sizeof(mask0);
1185 mask0.sin_family = AF_INET;
1186 mask0.sin_addr.s_addr = target->ia_subnetmask;
1187 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
1188 (struct sockaddr *)&mask0);
1189
1190 /*
1191 * As no-one seem to have this prefix, we can remove the route.
1192 */
1193 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1194 target->ia_flags &= ~IFA_ROUTE;
1195 return (0);
1196 }
1197
1198 #undef rtinitflags
1199
1200 /*
1201 * Return 1 if the address might be a local broadcast address.
1202 */
1203 int
1204 in_broadcast(struct in_addr in, struct ifnet *ifp)
1205 {
1206 register struct ifaddr *ifa;
1207 u_long t;
1208
1209 if (in.s_addr == INADDR_BROADCAST ||
1210 in.s_addr == INADDR_ANY)
1211 return (1);
1212 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1213 return (0);
1214 t = ntohl(in.s_addr);
1215 /*
1216 * Look through the list of addresses for a match
1217 * with a broadcast address.
1218 */
1219 #define ia ((struct in_ifaddr *)ifa)
1220 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1221 if (ifa->ifa_addr->sa_family == AF_INET &&
1222 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1223 in.s_addr == ia->ia_netbroadcast.s_addr ||
1224 /*
1225 * Check for old-style (host 0) broadcast.
1226 */
1227 t == ia->ia_subnet || t == ia->ia_net) &&
1228 /*
1229 * Check for an all one subnetmask. These
1230 * only exist when an interface gets a secondary
1231 * address.
1232 */
1233 ia->ia_subnetmask != (u_long)0xffffffff)
1234 return (1);
1235 return (0);
1236 #undef ia
1237 }
1238
1239 /*
1240 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1241 */
1242 void
1243 in_ifdetach(struct ifnet *ifp)
1244 {
1245
1246 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1247 in_pcbpurgeif0(&V_udbinfo, ifp);
1248 in_purgemaddrs(ifp);
1249 }
1250
1251 /*
1252 * Delete all IPv4 multicast address records, and associated link-layer
1253 * multicast address records, associated with ifp.
1254 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1255 * XXX This should not race with ifma_protospec being set during
1256 * a new allocation, if it does, we have bigger problems.
1257 */
1258 static void
1259 in_purgemaddrs(struct ifnet *ifp)
1260 {
1261 LIST_HEAD(,in_multi) purgeinms;
1262 struct in_multi *inm, *tinm;
1263 struct ifmultiaddr *ifma;
1264
1265 LIST_INIT(&purgeinms);
1266 IN_MULTI_LOCK();
1267
1268 /*
1269 * Extract list of in_multi associated with the detaching ifp
1270 * which the PF_INET layer is about to release.
1271 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1272 * by code further down.
1273 */
1274 IF_ADDR_LOCK(ifp);
1275 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1276 if (ifma->ifma_addr->sa_family != AF_INET ||
1277 ifma->ifma_protospec == NULL)
1278 continue;
1279 #if 0
1280 KASSERT(ifma->ifma_protospec != NULL,
1281 ("%s: ifma_protospec is NULL", __func__));
1282 #endif
1283 inm = (struct in_multi *)ifma->ifma_protospec;
1284 LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1285 }
1286 IF_ADDR_UNLOCK(ifp);
1287
1288 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1289 LIST_REMOVE(inm, inm_link);
1290 inm_release_locked(inm);
1291 }
1292 igmp_ifdetach(ifp);
1293
1294 IN_MULTI_UNLOCK();
1295 }
1296
1297 #include <net/if_dl.h>
1298 #include <netinet/if_ether.h>
1299
1300 struct in_llentry {
1301 struct llentry base;
1302 struct sockaddr_in l3_addr4;
1303 };
1304
1305 static struct llentry *
1306 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
1307 {
1308 struct in_llentry *lle;
1309
1310 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
1311 if (lle == NULL) /* NB: caller generates msg */
1312 return NULL;
1313
1314 callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
1315 /*
1316 * For IPv4 this will trigger "arpresolve" to generate
1317 * an ARP request.
1318 */
1319 lle->base.la_expire = time_second; /* mark expired */
1320 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1321 lle->base.lle_refcnt = 1;
1322 LLE_LOCK_INIT(&lle->base);
1323 return &lle->base;
1324 }
1325
1326 /*
1327 * Deletes an address from the address table.
1328 * This function is called by the timer functions
1329 * such as arptimer() and nd6_llinfo_timer(), and
1330 * the caller does the locking.
1331 */
1332 static void
1333 in_lltable_free(struct lltable *llt, struct llentry *lle)
1334 {
1335 LLE_WUNLOCK(lle);
1336 LLE_LOCK_DESTROY(lle);
1337 free(lle, M_LLTABLE);
1338 }
1339
1340
1341 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1342 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1343
1344 static void
1345 in_lltable_prefix_free(struct lltable *llt,
1346 const struct sockaddr *prefix,
1347 const struct sockaddr *mask)
1348 {
1349 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1350 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1351 struct llentry *lle, *next;
1352 register int i;
1353
1354 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
1355 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1356
1357 if (IN_ARE_MASKED_ADDR_EQUAL((struct sockaddr_in *)L3_ADDR(lle),
1358 pfx, msk)) {
1359 int canceled;
1360
1361 canceled = callout_drain(&lle->la_timer);
1362 LLE_WLOCK(lle);
1363 if (canceled)
1364 LLE_REMREF(lle);
1365 llentry_free(lle);
1366 }
1367 }
1368 }
1369 }
1370
1371
1372 static int
1373 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1374 {
1375 struct rtentry *rt;
1376
1377 KASSERT(l3addr->sa_family == AF_INET,
1378 ("sin_family %d", l3addr->sa_family));
1379
1380 /* XXX rtalloc1 should take a const param */
1381 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1382 if (rt == NULL || (!(flags & LLE_PUB) &&
1383 ((rt->rt_flags & RTF_GATEWAY) ||
1384 (rt->rt_ifp != ifp)))) {
1385 #ifdef DIAGNOSTIC
1386 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1387 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1388 #endif
1389 if (rt != NULL)
1390 RTFREE_LOCKED(rt);
1391 return (EINVAL);
1392 }
1393 RTFREE_LOCKED(rt);
1394 return 0;
1395 }
1396
1397 /*
1398 * Return NULL if not found or marked for deletion.
1399 * If found return lle read locked.
1400 */
1401 static struct llentry *
1402 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1403 {
1404 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1405 struct ifnet *ifp = llt->llt_ifp;
1406 struct llentry *lle;
1407 struct llentries *lleh;
1408 u_int hashkey;
1409
1410 IF_AFDATA_LOCK_ASSERT(ifp);
1411 KASSERT(l3addr->sa_family == AF_INET,
1412 ("sin_family %d", l3addr->sa_family));
1413
1414 hashkey = sin->sin_addr.s_addr;
1415 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1416 LIST_FOREACH(lle, lleh, lle_next) {
1417 struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle);
1418 if (lle->la_flags & LLE_DELETED)
1419 continue;
1420 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1421 break;
1422 }
1423 if (lle == NULL) {
1424 #ifdef DIAGNOSTIC
1425 if (flags & LLE_DELETE)
1426 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle);
1427 #endif
1428 if (!(flags & LLE_CREATE))
1429 return (NULL);
1430 /*
1431 * A route that covers the given address must have
1432 * been installed 1st because we are doing a resolution,
1433 * verify this.
1434 */
1435 if (!(flags & LLE_IFADDR) &&
1436 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1437 goto done;
1438
1439 lle = in_lltable_new(l3addr, flags);
1440 if (lle == NULL) {
1441 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1442 goto done;
1443 }
1444 lle->la_flags = flags & ~LLE_CREATE;
1445 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1446 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1447 lle->la_flags |= (LLE_VALID | LLE_STATIC);
1448 }
1449
1450 lle->lle_tbl = llt;
1451 lle->lle_head = lleh;
1452 LIST_INSERT_HEAD(lleh, lle, lle_next);
1453 } else if (flags & LLE_DELETE) {
1454 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1455 LLE_WLOCK(lle);
1456 lle->la_flags = LLE_DELETED;
1457 LLE_WUNLOCK(lle);
1458 #ifdef DIAGNOSTIC
1459 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1460 #endif
1461 }
1462 lle = (void *)-1;
1463
1464 }
1465 if (LLE_IS_VALID(lle)) {
1466 if (flags & LLE_EXCLUSIVE)
1467 LLE_WLOCK(lle);
1468 else
1469 LLE_RLOCK(lle);
1470 }
1471 done:
1472 return (lle);
1473 }
1474
1475 static int
1476 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1477 {
1478 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle))
1479 struct ifnet *ifp = llt->llt_ifp;
1480 struct llentry *lle;
1481 /* XXX stack use */
1482 struct {
1483 struct rt_msghdr rtm;
1484 struct sockaddr_inarp sin;
1485 struct sockaddr_dl sdl;
1486 } arpc;
1487 int error, i;
1488
1489 LLTABLE_LOCK_ASSERT();
1490
1491 error = 0;
1492 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1493 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1494 struct sockaddr_dl *sdl;
1495
1496 /* skip deleted entries */
1497 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1498 continue;
1499 /* Skip if jailed and not a valid IP of the prison. */
1500 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1501 continue;
1502 /*
1503 * produce a msg made of:
1504 * struct rt_msghdr;
1505 * struct sockaddr_inarp; (IPv4)
1506 * struct sockaddr_dl;
1507 */
1508 bzero(&arpc, sizeof(arpc));
1509 arpc.rtm.rtm_msglen = sizeof(arpc);
1510 arpc.rtm.rtm_version = RTM_VERSION;
1511 arpc.rtm.rtm_type = RTM_GET;
1512 arpc.rtm.rtm_flags = RTF_UP;
1513 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1514 arpc.sin.sin_family = AF_INET;
1515 arpc.sin.sin_len = sizeof(arpc.sin);
1516 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1517
1518 /* publish */
1519 if (lle->la_flags & LLE_PUB) {
1520 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1521 /* proxy only */
1522 if (lle->la_flags & LLE_PROXY)
1523 arpc.sin.sin_other = SIN_PROXY;
1524 }
1525
1526 sdl = &arpc.sdl;
1527 sdl->sdl_family = AF_LINK;
1528 sdl->sdl_len = sizeof(*sdl);
1529 sdl->sdl_index = ifp->if_index;
1530 sdl->sdl_type = ifp->if_type;
1531 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1532 sdl->sdl_alen = ifp->if_addrlen;
1533 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1534 } else {
1535 sdl->sdl_alen = 0;
1536 bzero(LLADDR(sdl), ifp->if_addrlen);
1537 }
1538
1539 arpc.rtm.rtm_rmx.rmx_expire =
1540 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1541 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1542 if (lle->la_flags & LLE_STATIC)
1543 arpc.rtm.rtm_flags |= RTF_STATIC;
1544 arpc.rtm.rtm_index = ifp->if_index;
1545 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1546 if (error)
1547 break;
1548 }
1549 }
1550 return error;
1551 #undef SIN
1552 }
1553
1554 void *
1555 in_domifattach(struct ifnet *ifp)
1556 {
1557 struct in_ifinfo *ii;
1558 struct lltable *llt;
1559
1560 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1561
1562 llt = lltable_init(ifp, AF_INET);
1563 if (llt != NULL) {
1564 llt->llt_new = in_lltable_new;
1565 llt->llt_free = in_lltable_free;
1566 llt->llt_prefix_free = in_lltable_prefix_free;
1567 llt->llt_rtcheck = in_lltable_rtcheck;
1568 llt->llt_lookup = in_lltable_lookup;
1569 llt->llt_dump = in_lltable_dump;
1570 }
1571 ii->ii_llt = llt;
1572
1573 ii->ii_igmp = igmp_domifattach(ifp);
1574
1575 return ii;
1576 }
1577
1578 void
1579 in_domifdetach(struct ifnet *ifp, void *aux)
1580 {
1581 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1582
1583 igmp_domifdetach(ifp);
1584 lltable_free(ii->ii_llt);
1585 free(ii, M_IFADDR);
1586 }
Cache object: b5b5e07746ecacbe955c17f7bf6626f8
|