in.c revision 1.158 1 /* $NetBSD: in.c,v 1.158 2015/08/31 08:02:44 ozaki-r Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62 /*
63 * Copyright (c) 1982, 1986, 1991, 1993
64 * The Regents of the University of California. All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 * @(#)in.c 8.4 (Berkeley) 1/9/95
91 */
92
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.158 2015/08/31 08:02:44 ozaki-r Exp $");
95
96 #include "arp.h"
97
98 #ifdef _KERNEL_OPT
99 #include "opt_inet.h"
100 #include "opt_inet_conf.h"
101 #include "opt_mrouting.h"
102 #endif
103
104 #include <sys/param.h>
105 #include <sys/ioctl.h>
106 #include <sys/errno.h>
107 #include <sys/kernel.h>
108 #include <sys/malloc.h>
109 #include <sys/socket.h>
110 #include <sys/socketvar.h>
111 #include <sys/sysctl.h>
112 #include <sys/systm.h>
113 #include <sys/proc.h>
114 #include <sys/syslog.h>
115 #include <sys/kauth.h>
116 #include <sys/kmem.h>
117
118 #include <sys/cprng.h>
119
120 #include <net/if.h>
121 #include <net/route.h>
122 #include <net/pfil.h>
123
124 #include <net/if_ether.h>
125 #include <net/if_types.h>
126 #include <net/if_llatbl.h>
127 #include <net/if_dl.h>
128
129 #include <netinet/in_systm.h>
130 #include <netinet/in.h>
131 #include <netinet/in_var.h>
132 #include <netinet/ip.h>
133 #include <netinet/ip_var.h>
134 #include <netinet/in_ifattach.h>
135 #include <netinet/in_pcb.h>
136 #include <netinet/in_selsrc.h>
137 #include <netinet/if_inarp.h>
138 #include <netinet/ip_mroute.h>
139 #include <netinet/igmp_var.h>
140
141 #ifdef IPSELSRC
142 #include <netinet/in_selsrc.h>
143 #endif
144
145 static u_int in_mask2len(struct in_addr *);
146 static void in_len2mask(struct in_addr *, u_int);
147 static int in_lifaddr_ioctl(struct socket *, u_long, void *,
148 struct ifnet *);
149
150 static int in_addprefix(struct in_ifaddr *, int);
151 static int in_scrubprefix(struct in_ifaddr *);
152 static void in_sysctl_init(struct sysctllog **);
153
154 #ifndef SUBNETSARELOCAL
155 #define SUBNETSARELOCAL 1
156 #endif
157
158 #ifndef HOSTZEROBROADCAST
159 #define HOSTZEROBROADCAST 1
160 #endif
161
162 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
163 #ifndef IN_MULTI_HASH_SIZE
164 #define IN_MULTI_HASH_SIZE 509
165 #endif
166
167 static int subnetsarelocal = SUBNETSARELOCAL;
168 static int hostzeroisbroadcast = HOSTZEROBROADCAST;
169
170 /*
171 * This list is used to keep track of in_multi chains which belong to
172 * deleted interface addresses. We use in_ifaddr so that a chain head
173 * won't be deallocated until all multicast address record are deleted.
174 */
175
176 LIST_HEAD(in_multihashhead, in_multi); /* Type of the hash head */
177
178 static struct pool inmulti_pool;
179 static u_int in_multientries;
180 static struct in_multihashhead *in_multihashtbl;
181 static u_long in_multihash;
182 static krwlock_t in_multilock;
183
184 #define IN_MULTI_HASH(x, ifp) \
185 (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE])
186
187 struct in_ifaddrhashhead * in_ifaddrhashtbl;
188 u_long in_ifaddrhash;
189 struct in_ifaddrhead in_ifaddrhead;
190
191 void
192 in_init(void)
193 {
194 pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
195 NULL, IPL_SOFTNET);
196 TAILQ_INIT(&in_ifaddrhead);
197
198 in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
199 &in_ifaddrhash);
200 in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
201 &in_multihash);
202 rw_init(&in_multilock);
203
204 in_sysctl_init(NULL);
205 }
206
207 /*
208 * Return 1 if an internet address is for a ``local'' host
209 * (one to which we have a connection). If subnetsarelocal
210 * is true, this includes other subnets of the local net.
211 * Otherwise, it includes only the directly-connected (sub)nets.
212 */
213 int
214 in_localaddr(struct in_addr in)
215 {
216 struct in_ifaddr *ia;
217
218 if (subnetsarelocal) {
219 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
220 if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
221 return (1);
222 } else {
223 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
224 if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
225 return (1);
226 }
227 return (0);
228 }
229
230 /*
231 * Determine whether an IP address is in a reserved set of addresses
232 * that may not be forwarded, or whether datagrams to that destination
233 * may be forwarded.
234 */
235 int
236 in_canforward(struct in_addr in)
237 {
238 u_int32_t net;
239
240 if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
241 return (0);
242 if (IN_CLASSA(in.s_addr)) {
243 net = in.s_addr & IN_CLASSA_NET;
244 if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
245 return (0);
246 }
247 return (1);
248 }
249
250 /*
251 * Trim a mask in a sockaddr
252 */
253 void
254 in_socktrim(struct sockaddr_in *ap)
255 {
256 char *cplim = (char *) &ap->sin_addr;
257 char *cp = (char *) (&ap->sin_addr + 1);
258
259 ap->sin_len = 0;
260 while (--cp >= cplim)
261 if (*cp) {
262 (ap)->sin_len = cp - (char *) (ap) + 1;
263 break;
264 }
265 }
266
267 /*
268 * Routine to take an Internet address and convert into a
269 * "dotted quad" representation for printing.
270 */
271 const char *
272 in_fmtaddr(struct in_addr addr)
273 {
274 static char buf[sizeof("123.456.789.123")];
275
276 addr.s_addr = ntohl(addr.s_addr);
277
278 snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
279 (addr.s_addr >> 24) & 0xFF,
280 (addr.s_addr >> 16) & 0xFF,
281 (addr.s_addr >> 8) & 0xFF,
282 (addr.s_addr >> 0) & 0xFF);
283 return buf;
284 }
285
286 /*
287 * Maintain the "in_maxmtu" variable, which is the largest
288 * mtu for non-local interfaces with AF_INET addresses assigned
289 * to them that are up.
290 */
291 unsigned long in_maxmtu;
292
293 void
294 in_setmaxmtu(void)
295 {
296 struct in_ifaddr *ia;
297 struct ifnet *ifp;
298 unsigned long maxmtu = 0;
299
300 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
301 if ((ifp = ia->ia_ifp) == 0)
302 continue;
303 if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
304 continue;
305 if (ifp->if_mtu > maxmtu)
306 maxmtu = ifp->if_mtu;
307 }
308 if (maxmtu)
309 in_maxmtu = maxmtu;
310 }
311
312 static u_int
313 in_mask2len(struct in_addr *mask)
314 {
315 u_int x, y;
316 u_char *p;
317
318 p = (u_char *)mask;
319 for (x = 0; x < sizeof(*mask); x++) {
320 if (p[x] != 0xff)
321 break;
322 }
323 y = 0;
324 if (x < sizeof(*mask)) {
325 for (y = 0; y < NBBY; y++) {
326 if ((p[x] & (0x80 >> y)) == 0)
327 break;
328 }
329 }
330 return x * NBBY + y;
331 }
332
333 static void
334 in_len2mask(struct in_addr *mask, u_int len)
335 {
336 u_int i;
337 u_char *p;
338
339 p = (u_char *)mask;
340 memset(mask, 0, sizeof(*mask));
341 for (i = 0; i < len / NBBY; i++)
342 p[i] = 0xff;
343 if (len % NBBY)
344 p[i] = (0xff00 >> (len % NBBY)) & 0xff;
345 }
346
347 /*
348 * Generic internet control operations (ioctl's).
349 * Ifp is 0 if not an interface-specific ioctl.
350 */
351 /* ARGSUSED */
352 int
353 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
354 {
355 struct ifreq *ifr = (struct ifreq *)data;
356 struct in_ifaddr *ia = NULL;
357 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
358 struct sockaddr_in oldaddr;
359 int error, hostIsNew, maskIsNew;
360 int newifaddr = 0;
361
362 switch (cmd) {
363 case SIOCALIFADDR:
364 case SIOCDLIFADDR:
365 case SIOCGLIFADDR:
366 if (ifp == NULL)
367 return EINVAL;
368 return in_lifaddr_ioctl(so, cmd, data, ifp);
369 case SIOCGIFADDRPREF:
370 case SIOCSIFADDRPREF:
371 if (ifp == NULL)
372 return EINVAL;
373 return ifaddrpref_ioctl(so, cmd, data, ifp);
374 }
375
376 /*
377 * Find address for this interface, if it exists.
378 */
379 if (ifp != NULL)
380 IFP_TO_IA(ifp, ia);
381
382 hostIsNew = 1; /* moved here to appease gcc */
383 switch (cmd) {
384 case SIOCAIFADDR:
385 case SIOCDIFADDR:
386 case SIOCGIFALIAS:
387 case SIOCGIFAFLAG_IN:
388 if (ifra->ifra_addr.sin_family == AF_INET)
389 LIST_FOREACH(ia,
390 &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
391 ia_hash) {
392 if (ia->ia_ifp == ifp &&
393 in_hosteq(ia->ia_addr.sin_addr,
394 ifra->ifra_addr.sin_addr))
395 break;
396 }
397 if ((cmd == SIOCDIFADDR ||
398 cmd == SIOCGIFALIAS ||
399 cmd == SIOCGIFAFLAG_IN) &&
400 ia == NULL)
401 return (EADDRNOTAVAIL);
402
403 if (cmd == SIOCDIFADDR &&
404 ifra->ifra_addr.sin_family == AF_UNSPEC) {
405 ifra->ifra_addr.sin_family = AF_INET;
406 }
407 /* FALLTHROUGH */
408 case SIOCSIFADDR:
409 if (ia == NULL || ia->ia_addr.sin_family != AF_INET)
410 ;
411 else if (ifra->ifra_addr.sin_len == 0) {
412 ifra->ifra_addr = ia->ia_addr;
413 hostIsNew = 0;
414 } else if (in_hosteq(ia->ia_addr.sin_addr,
415 ifra->ifra_addr.sin_addr))
416 hostIsNew = 0;
417 /* FALLTHROUGH */
418 case SIOCSIFDSTADDR:
419 if (ifra->ifra_addr.sin_family != AF_INET)
420 return (EAFNOSUPPORT);
421 /* FALLTHROUGH */
422 case SIOCSIFNETMASK:
423 if (ifp == NULL)
424 panic("in_control");
425
426 if (cmd == SIOCGIFALIAS || cmd == SIOCGIFAFLAG_IN)
427 break;
428
429 if (ia == NULL &&
430 (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
431 return (EADDRNOTAVAIL);
432
433 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
434 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
435 NULL) != 0)
436 return (EPERM);
437
438 if (ia == NULL) {
439 ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
440 if (ia == NULL)
441 return (ENOBUFS);
442 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
443 ifaref(&ia->ia_ifa);
444 ifa_insert(ifp, &ia->ia_ifa);
445 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
446 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
447 ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
448 #ifdef IPSELSRC
449 ia->ia_ifa.ifa_getifa = in_getifa;
450 #else /* IPSELSRC */
451 ia->ia_ifa.ifa_getifa = NULL;
452 #endif /* IPSELSRC */
453 ia->ia_sockmask.sin_len = 8;
454 ia->ia_sockmask.sin_family = AF_INET;
455 if (ifp->if_flags & IFF_BROADCAST) {
456 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
457 ia->ia_broadaddr.sin_family = AF_INET;
458 }
459 ia->ia_ifp = ifp;
460 ia->ia_idsalt = cprng_fast32() % 65535;
461 LIST_INIT(&ia->ia_multiaddrs);
462 newifaddr = 1;
463 }
464 break;
465
466 case SIOCSIFBRDADDR:
467 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
468 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
469 NULL) != 0)
470 return (EPERM);
471 /* FALLTHROUGH */
472
473 case SIOCGIFADDR:
474 case SIOCGIFNETMASK:
475 case SIOCGIFDSTADDR:
476 case SIOCGIFBRDADDR:
477 if (ia == NULL)
478 return (EADDRNOTAVAIL);
479 break;
480 }
481 error = 0;
482 switch (cmd) {
483
484 case SIOCGIFADDR:
485 ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
486 break;
487
488 case SIOCGIFBRDADDR:
489 if ((ifp->if_flags & IFF_BROADCAST) == 0)
490 return (EINVAL);
491 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
492 break;
493
494 case SIOCGIFDSTADDR:
495 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
496 return (EINVAL);
497 ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
498 break;
499
500 case SIOCGIFNETMASK:
501 /*
502 * We keep the number of trailing zero bytes the sin_len field
503 * of ia_sockmask, so we fix this before we pass it back to
504 * userland.
505 */
506 oldaddr = ia->ia_sockmask;
507 oldaddr.sin_len = sizeof(struct sockaddr_in);
508 ifreq_setaddr(cmd, ifr, (const void *)&oldaddr);
509 break;
510
511 case SIOCSIFDSTADDR:
512 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
513 return (EINVAL);
514 oldaddr = ia->ia_dstaddr;
515 ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
516 if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
517 ia->ia_dstaddr = oldaddr;
518 return error;
519 }
520 if (ia->ia_flags & IFA_ROUTE) {
521 ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
522 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
523 ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
524 rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
525 }
526 break;
527
528 case SIOCSIFBRDADDR:
529 if ((ifp->if_flags & IFF_BROADCAST) == 0)
530 return EINVAL;
531 ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
532 break;
533
534 case SIOCSIFADDR:
535 error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
536 1, hostIsNew);
537 if (error == 0) {
538 (void)pfil_run_hooks(if_pfil,
539 (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
540 }
541 break;
542
543 case SIOCSIFNETMASK:
544 in_ifscrub(ifp, ia);
545 ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
546 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
547 error = in_ifinit(ifp, ia, NULL, 0, 0);
548 break;
549
550 case SIOCAIFADDR:
551 maskIsNew = 0;
552 if (ifra->ifra_mask.sin_len) {
553 /* Only scrub if we control the prefix route,
554 * otherwise userland gets a bogus message */
555 if ((ia->ia_flags & IFA_ROUTE))
556 in_ifscrub(ifp, ia);
557 ia->ia_sockmask = ifra->ifra_mask;
558 ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
559 maskIsNew = 1;
560 }
561 if ((ifp->if_flags & IFF_POINTOPOINT) &&
562 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
563 /* Only scrub if we control the prefix route,
564 * otherwise userland gets a bogus message */
565 if ((ia->ia_flags & IFA_ROUTE))
566 in_ifscrub(ifp, ia);
567 ia->ia_dstaddr = ifra->ifra_dstaddr;
568 maskIsNew = 1; /* We lie; but the effect's the same */
569 }
570 if (ifra->ifra_addr.sin_family == AF_INET &&
571 (hostIsNew || maskIsNew)) {
572 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0,
573 hostIsNew);
574 }
575 if ((ifp->if_flags & IFF_BROADCAST) &&
576 (ifra->ifra_broadaddr.sin_family == AF_INET))
577 ia->ia_broadaddr = ifra->ifra_broadaddr;
578 if (error == 0)
579 (void)pfil_run_hooks(if_pfil,
580 (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
581 break;
582
583 case SIOCGIFALIAS:
584 ifra->ifra_mask = ia->ia_sockmask;
585 if ((ifp->if_flags & IFF_POINTOPOINT) &&
586 (ia->ia_dstaddr.sin_family == AF_INET))
587 ifra->ifra_dstaddr = ia->ia_dstaddr;
588 else if ((ifp->if_flags & IFF_BROADCAST) &&
589 (ia->ia_broadaddr.sin_family == AF_INET))
590 ifra->ifra_broadaddr = ia->ia_broadaddr;
591 else
592 memset(&ifra->ifra_broadaddr, 0,
593 sizeof(ifra->ifra_broadaddr));
594 break;
595
596 case SIOCGIFAFLAG_IN:
597 ifr->ifr_addrflags = ia->ia4_flags;
598 break;
599
600 case SIOCDIFADDR:
601 in_purgeaddr(&ia->ia_ifa);
602 (void)pfil_run_hooks(if_pfil, (struct mbuf **)SIOCDIFADDR,
603 ifp, PFIL_IFADDR);
604 break;
605
606 #ifdef MROUTING
607 case SIOCGETVIFCNT:
608 case SIOCGETSGCNT:
609 error = mrt_ioctl(so, cmd, data);
610 break;
611 #endif /* MROUTING */
612
613 default:
614 return ENOTTY;
615 }
616
617 if (error != 0 && newifaddr) {
618 KASSERT(ia != NULL);
619 in_purgeaddr(&ia->ia_ifa);
620 }
621
622 return error;
623 }
624
625 /* Add ownaddr as loopback rtentry. */
626 static void
627 in_ifaddlocal(struct ifaddr *ifa)
628 {
629 struct in_ifaddr *ia;
630
631 ia = (struct in_ifaddr *)ifa;
632 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY ||
633 (ia->ia_ifp->if_flags & IFF_POINTOPOINT &&
634 in_hosteq(ia->ia_dstaddr.sin_addr, ia->ia_addr.sin_addr)))
635 {
636 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
637 return;
638 }
639
640 rt_ifa_addlocal(ifa);
641 }
642
643 /* Rempve loopback entry of ownaddr */
644 static void
645 in_ifremlocal(struct ifaddr *ifa)
646 {
647 struct in_ifaddr *ia, *p;
648 struct ifaddr *alt_ifa = NULL;
649 int ia_count = 0;
650
651 ia = (struct in_ifaddr *)ifa;
652 /* Delete the entry if exactly one ifaddr matches the
653 * address, ifa->ifa_addr. */
654 TAILQ_FOREACH(p, &in_ifaddrhead, ia_list) {
655 if (!in_hosteq(p->ia_addr.sin_addr, ia->ia_addr.sin_addr))
656 continue;
657 if (p->ia_ifp != ia->ia_ifp)
658 alt_ifa = &p->ia_ifa;
659 if (++ia_count > 1 && alt_ifa != NULL)
660 break;
661 }
662
663 if (ia_count == 0)
664 return;
665
666 rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa);
667 }
668
669 void
670 in_purgeaddr(struct ifaddr *ifa)
671 {
672 struct ifnet *ifp = ifa->ifa_ifp;
673 struct in_ifaddr *ia = (void *) ifa;
674
675 /* stop DAD processing */
676 if (ia->ia_dad_stop != NULL)
677 ia->ia_dad_stop(ifa);
678
679 in_ifscrub(ifp, ia);
680 in_ifremlocal(ifa);
681 LIST_REMOVE(ia, ia_hash);
682 ifa_remove(ifp, &ia->ia_ifa);
683 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
684 if (ia->ia_allhosts != NULL)
685 in_delmulti(ia->ia_allhosts);
686 ifafree(&ia->ia_ifa);
687 in_setmaxmtu();
688 }
689
690 void
691 in_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */
692 {
693 if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
694 igmp_purgeif(ifp); /* manipulates pools */
695 #ifdef MROUTING
696 ip_mrouter_detach(ifp);
697 #endif
698 }
699
700 /*
701 * SIOC[GAD]LIFADDR.
702 * SIOCGLIFADDR: get first address. (???)
703 * SIOCGLIFADDR with IFLR_PREFIX:
704 * get first address that matches the specified prefix.
705 * SIOCALIFADDR: add the specified address.
706 * SIOCALIFADDR with IFLR_PREFIX:
707 * EINVAL since we can't deduce hostid part of the address.
708 * SIOCDLIFADDR: delete the specified address.
709 * SIOCDLIFADDR with IFLR_PREFIX:
710 * delete the first address that matches the specified prefix.
711 * return values:
712 * EINVAL on invalid parameters
713 * EADDRNOTAVAIL on prefix match failed/specified address not found
714 * other values may be returned from in_ioctl()
715 */
716 static int
717 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
718 struct ifnet *ifp)
719 {
720 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
721 struct ifaddr *ifa;
722 struct sockaddr *sa;
723
724 /* sanity checks */
725 if (data == NULL || ifp == NULL) {
726 panic("invalid argument to in_lifaddr_ioctl");
727 /*NOTRECHED*/
728 }
729
730 switch (cmd) {
731 case SIOCGLIFADDR:
732 /* address must be specified on GET with IFLR_PREFIX */
733 if ((iflr->flags & IFLR_PREFIX) == 0)
734 break;
735 /*FALLTHROUGH*/
736 case SIOCALIFADDR:
737 case SIOCDLIFADDR:
738 /* address must be specified on ADD and DELETE */
739 sa = (struct sockaddr *)&iflr->addr;
740 if (sa->sa_family != AF_INET)
741 return EINVAL;
742 if (sa->sa_len != sizeof(struct sockaddr_in))
743 return EINVAL;
744 /* XXX need improvement */
745 sa = (struct sockaddr *)&iflr->dstaddr;
746 if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
747 return EINVAL;
748 if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
749 return EINVAL;
750 break;
751 default: /*shouldn't happen*/
752 #if 0
753 panic("invalid cmd to in_lifaddr_ioctl");
754 /*NOTREACHED*/
755 #else
756 return EOPNOTSUPP;
757 #endif
758 }
759 if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
760 return EINVAL;
761
762 switch (cmd) {
763 case SIOCALIFADDR:
764 {
765 struct in_aliasreq ifra;
766
767 if (iflr->flags & IFLR_PREFIX)
768 return EINVAL;
769
770 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
771 memset(&ifra, 0, sizeof(ifra));
772 memcpy(ifra.ifra_name, iflr->iflr_name,
773 sizeof(ifra.ifra_name));
774
775 memcpy(&ifra.ifra_addr, &iflr->addr,
776 ((struct sockaddr *)&iflr->addr)->sa_len);
777
778 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
779 memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
780 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
781 }
782
783 ifra.ifra_mask.sin_family = AF_INET;
784 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
785 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
786
787 return in_control(so, SIOCAIFADDR, &ifra, ifp);
788 }
789 case SIOCGLIFADDR:
790 case SIOCDLIFADDR:
791 {
792 struct in_ifaddr *ia;
793 struct in_addr mask, candidate, match;
794 struct sockaddr_in *sin;
795 int cmp;
796
797 memset(&mask, 0, sizeof(mask));
798 memset(&match, 0, sizeof(match)); /* XXX gcc */
799 if (iflr->flags & IFLR_PREFIX) {
800 /* lookup a prefix rather than address. */
801 in_len2mask(&mask, iflr->prefixlen);
802
803 sin = (struct sockaddr_in *)&iflr->addr;
804 match.s_addr = sin->sin_addr.s_addr;
805 match.s_addr &= mask.s_addr;
806
807 /* if you set extra bits, that's wrong */
808 if (match.s_addr != sin->sin_addr.s_addr)
809 return EINVAL;
810
811 cmp = 1;
812 } else {
813 if (cmd == SIOCGLIFADDR) {
814 /* on getting an address, take the 1st match */
815 cmp = 0; /*XXX*/
816 } else {
817 /* on deleting an address, do exact match */
818 in_len2mask(&mask, 32);
819 sin = (struct sockaddr_in *)&iflr->addr;
820 match.s_addr = sin->sin_addr.s_addr;
821
822 cmp = 1;
823 }
824 }
825
826 IFADDR_FOREACH(ifa, ifp) {
827 if (ifa->ifa_addr->sa_family != AF_INET)
828 continue;
829 if (cmp == 0)
830 break;
831 candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
832 candidate.s_addr &= mask.s_addr;
833 if (candidate.s_addr == match.s_addr)
834 break;
835 }
836 if (ifa == NULL)
837 return EADDRNOTAVAIL;
838 ia = (struct in_ifaddr *)ifa;
839
840 if (cmd == SIOCGLIFADDR) {
841 /* fill in the if_laddrreq structure */
842 memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
843
844 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
845 memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
846 ia->ia_dstaddr.sin_len);
847 } else
848 memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
849
850 iflr->prefixlen =
851 in_mask2len(&ia->ia_sockmask.sin_addr);
852
853 iflr->flags = 0; /*XXX*/
854
855 return 0;
856 } else {
857 struct in_aliasreq ifra;
858
859 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
860 memset(&ifra, 0, sizeof(ifra));
861 memcpy(ifra.ifra_name, iflr->iflr_name,
862 sizeof(ifra.ifra_name));
863
864 memcpy(&ifra.ifra_addr, &ia->ia_addr,
865 ia->ia_addr.sin_len);
866 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
867 memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
868 ia->ia_dstaddr.sin_len);
869 }
870 memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
871 ia->ia_sockmask.sin_len);
872
873 return in_control(so, SIOCDIFADDR, &ifra, ifp);
874 }
875 }
876 }
877
878 return EOPNOTSUPP; /*just for safety*/
879 }
880
881 /*
882 * Delete any existing route for an interface.
883 */
884 void
885 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
886 {
887
888 in_scrubprefix(ia);
889 }
890
891 /*
892 * Initialize an interface's internet address
893 * and routing table entry.
894 */
895 int
896 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
897 const struct sockaddr_in *sin, int scrub, int hostIsNew)
898 {
899 u_int32_t i;
900 struct sockaddr_in oldaddr;
901 int s = splnet(), flags = RTF_UP, error;
902
903 if (sin == NULL)
904 sin = &ia->ia_addr;
905
906 /*
907 * Set up new addresses.
908 */
909 oldaddr = ia->ia_addr;
910 if (ia->ia_addr.sin_family == AF_INET)
911 LIST_REMOVE(ia, ia_hash);
912 ia->ia_addr = *sin;
913 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
914
915 /* Set IN_IFF flags early for if_addr_init() */
916 if (hostIsNew && if_do_dad(ifp) && !in_nullhost(ia->ia_addr.sin_addr)) {
917 if (ifp->if_link_state == LINK_STATE_DOWN)
918 ia->ia4_flags |= IN_IFF_DETACHED;
919 else
920 /* State the intent to try DAD if possible */
921 ia->ia4_flags |= IN_IFF_TRYTENTATIVE;
922 }
923
924 /*
925 * Give the interface a chance to initialize
926 * if this is its first address,
927 * and to validate the address if necessary.
928 */
929 if ((error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0)
930 goto bad;
931 /* Now clear the try tentative flag, it's job is done. */
932 ia->ia4_flags &= ~IN_IFF_TRYTENTATIVE;
933 splx(s);
934
935 if (scrub) {
936 ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
937 in_ifscrub(ifp, ia);
938 ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
939 }
940
941 /* Add the local route to the address */
942 in_ifaddlocal(&ia->ia_ifa);
943
944 i = ia->ia_addr.sin_addr.s_addr;
945 if (IN_CLASSA(i))
946 ia->ia_netmask = IN_CLASSA_NET;
947 else if (IN_CLASSB(i))
948 ia->ia_netmask = IN_CLASSB_NET;
949 else
950 ia->ia_netmask = IN_CLASSC_NET;
951 /*
952 * The subnet mask usually includes at least the standard network part,
953 * but may may be smaller in the case of supernetting.
954 * If it is set, we believe it.
955 */
956 if (ia->ia_subnetmask == 0) {
957 ia->ia_subnetmask = ia->ia_netmask;
958 ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
959 } else
960 ia->ia_netmask &= ia->ia_subnetmask;
961
962 ia->ia_net = i & ia->ia_netmask;
963 ia->ia_subnet = i & ia->ia_subnetmask;
964 in_socktrim(&ia->ia_sockmask);
965 /* re-calculate the "in_maxmtu" value */
966 in_setmaxmtu();
967 /*
968 * Add route for the network.
969 */
970 ia->ia_ifa.ifa_metric = ifp->if_metric;
971 if (ifp->if_flags & IFF_BROADCAST) {
972 ia->ia_broadaddr.sin_addr.s_addr =
973 ia->ia_subnet | ~ia->ia_subnetmask;
974 ia->ia_netbroadcast.s_addr =
975 ia->ia_net | ~ia->ia_netmask;
976 } else if (ifp->if_flags & IFF_LOOPBACK) {
977 ia->ia_dstaddr = ia->ia_addr;
978 flags |= RTF_HOST;
979 } else if (ifp->if_flags & IFF_POINTOPOINT) {
980 if (ia->ia_dstaddr.sin_family != AF_INET)
981 return (0);
982 flags |= RTF_HOST;
983 }
984 error = in_addprefix(ia, flags);
985 /*
986 * If the interface supports multicast, join the "all hosts"
987 * multicast group on that interface.
988 */
989 if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
990 struct in_addr addr;
991
992 addr.s_addr = INADDR_ALLHOSTS_GROUP;
993 ia->ia_allhosts = in_addmulti(&addr, ifp);
994 }
995
996 if (hostIsNew && if_do_dad(ifp) &&
997 !in_nullhost(ia->ia_addr.sin_addr) &&
998 ia->ia4_flags & IN_IFF_TENTATIVE)
999 ia->ia_dad_start((struct ifaddr *)ia);
1000
1001 return (error);
1002 bad:
1003 splx(s);
1004 LIST_REMOVE(ia, ia_hash);
1005 ia->ia_addr = oldaddr;
1006 if (ia->ia_addr.sin_family == AF_INET)
1007 LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
1008 ia, ia_hash);
1009 return (error);
1010 }
1011
1012 #define rtinitflags(x) \
1013 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
1014 ? RTF_HOST : 0)
1015
1016 /*
1017 * add a route to prefix ("connected route" in cisco terminology).
1018 * does nothing if there's some interface address with the same prefix already.
1019 */
1020 static int
1021 in_addprefix(struct in_ifaddr *target, int flags)
1022 {
1023 struct in_ifaddr *ia;
1024 struct in_addr prefix, mask, p;
1025 int error;
1026
1027 if ((flags & RTF_HOST) != 0)
1028 prefix = target->ia_dstaddr.sin_addr;
1029 else {
1030 prefix = target->ia_addr.sin_addr;
1031 mask = target->ia_sockmask.sin_addr;
1032 prefix.s_addr &= mask.s_addr;
1033 }
1034
1035 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1036 if (rtinitflags(ia))
1037 p = ia->ia_dstaddr.sin_addr;
1038 else {
1039 p = ia->ia_addr.sin_addr;
1040 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1041 }
1042
1043 if (prefix.s_addr != p.s_addr)
1044 continue;
1045
1046 /*
1047 * if we got a matching prefix route inserted by other
1048 * interface address, we don't need to bother
1049 *
1050 * XXX RADIX_MPATH implications here? -dyoung
1051 */
1052 if (ia->ia_flags & IFA_ROUTE)
1053 return 0;
1054 }
1055
1056 /*
1057 * noone seem to have prefix route. insert it.
1058 */
1059 error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1060 if (error == 0)
1061 target->ia_flags |= IFA_ROUTE;
1062 else if (error == EEXIST) {
1063 /*
1064 * the fact the route already exists is not an error.
1065 */
1066 error = 0;
1067 }
1068 return error;
1069 }
1070
1071 /*
1072 * remove a route to prefix ("connected route" in cisco terminology).
1073 * re-installs the route by using another interface address, if there's one
1074 * with the same prefix (otherwise we lose the route mistakenly).
1075 */
1076 static int
1077 in_scrubprefix(struct in_ifaddr *target)
1078 {
1079 struct in_ifaddr *ia;
1080 struct in_addr prefix, mask, p;
1081 int error;
1082
1083 /* If we don't have IFA_ROUTE we should still inform userland */
1084 if ((target->ia_flags & IFA_ROUTE) == 0)
1085 return 0;
1086
1087 if (rtinitflags(target))
1088 prefix = target->ia_dstaddr.sin_addr;
1089 else {
1090 prefix = target->ia_addr.sin_addr;
1091 mask = target->ia_sockmask.sin_addr;
1092 prefix.s_addr &= mask.s_addr;
1093 }
1094
1095 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1096 if (rtinitflags(ia))
1097 p = ia->ia_dstaddr.sin_addr;
1098 else {
1099 p = ia->ia_addr.sin_addr;
1100 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1101 }
1102
1103 if (prefix.s_addr != p.s_addr)
1104 continue;
1105
1106 /*
1107 * if we got a matching prefix route, move IFA_ROUTE to him
1108 */
1109 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1110 rtinit(&target->ia_ifa, RTM_DELETE,
1111 rtinitflags(target));
1112 target->ia_flags &= ~IFA_ROUTE;
1113
1114 error = rtinit(&ia->ia_ifa, RTM_ADD,
1115 rtinitflags(ia) | RTF_UP);
1116 if (error == 0)
1117 ia->ia_flags |= IFA_ROUTE;
1118 return error;
1119 }
1120 }
1121
1122 /*
1123 * noone seem to have prefix route. remove it.
1124 */
1125 rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1126 target->ia_flags &= ~IFA_ROUTE;
1127 return 0;
1128 }
1129
1130 #undef rtinitflags
1131
1132 /*
1133 * Return 1 if the address might be a local broadcast address.
1134 */
1135 int
1136 in_broadcast(struct in_addr in, struct ifnet *ifp)
1137 {
1138 struct ifaddr *ifa;
1139
1140 if (in.s_addr == INADDR_BROADCAST ||
1141 in_nullhost(in))
1142 return 1;
1143 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1144 return 0;
1145 /*
1146 * Look through the list of addresses for a match
1147 * with a broadcast address.
1148 */
1149 #define ia (ifatoia(ifa))
1150 IFADDR_FOREACH(ifa, ifp)
1151 if (ifa->ifa_addr->sa_family == AF_INET &&
1152 !in_hosteq(in, ia->ia_addr.sin_addr) &&
1153 (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1154 in_hosteq(in, ia->ia_netbroadcast) ||
1155 (hostzeroisbroadcast &&
1156 /*
1157 * Check for old-style (host 0) broadcast.
1158 */
1159 (in.s_addr == ia->ia_subnet ||
1160 in.s_addr == ia->ia_net))))
1161 return 1;
1162 return (0);
1163 #undef ia
1164 }
1165
1166 /*
1167 * perform DAD when interface becomes IFF_UP.
1168 */
1169 void
1170 in_if_link_up(struct ifnet *ifp)
1171 {
1172 struct ifaddr *ifa;
1173 struct in_ifaddr *ia;
1174
1175 /* Ensure it's sane to run DAD */
1176 if (ifp->if_link_state == LINK_STATE_DOWN)
1177 return;
1178 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
1179 return;
1180
1181 IFADDR_FOREACH(ifa, ifp) {
1182 if (ifa->ifa_addr->sa_family != AF_INET)
1183 continue;
1184 ia = (struct in_ifaddr *)ifa;
1185
1186 /* If detached then mark as tentative */
1187 if (ia->ia4_flags & IN_IFF_DETACHED) {
1188 ia->ia4_flags &= ~IN_IFF_DETACHED;
1189 if (if_do_dad(ifp) && ia->ia_dad_start != NULL)
1190 ia->ia4_flags |= IN_IFF_TENTATIVE;
1191 else if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0)
1192 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1193 }
1194
1195 if (ia->ia4_flags & IN_IFF_TENTATIVE) {
1196 /* Clear the duplicated flag as we're starting DAD. */
1197 ia->ia4_flags &= ~IN_IFF_DUPLICATED;
1198 ia->ia_dad_start(ifa);
1199 }
1200 }
1201 }
1202
1203 void
1204 in_if_up(struct ifnet *ifp)
1205 {
1206
1207 /* interface may not support link state, so bring it up also */
1208 in_if_link_up(ifp);
1209 }
1210
1211 /*
1212 * Mark all addresses as detached.
1213 */
1214 void
1215 in_if_link_down(struct ifnet *ifp)
1216 {
1217 struct ifaddr *ifa;
1218 struct in_ifaddr *ia;
1219
1220 IFADDR_FOREACH(ifa, ifp) {
1221 if (ifa->ifa_addr->sa_family != AF_INET)
1222 continue;
1223 ia = (struct in_ifaddr *)ifa;
1224
1225 /* Stop DAD processing */
1226 if (ia->ia_dad_stop != NULL)
1227 ia->ia_dad_stop(ifa);
1228
1229 /*
1230 * Mark the address as detached.
1231 */
1232 if (!(ia->ia4_flags & IN_IFF_DETACHED)) {
1233 ia->ia4_flags |= IN_IFF_DETACHED;
1234 ia->ia4_flags &=
1235 ~(IN_IFF_TENTATIVE | IN_IFF_DUPLICATED);
1236 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1237 }
1238 }
1239 }
1240
1241 void
1242 in_if_down(struct ifnet *ifp)
1243 {
1244
1245 in_if_link_down(ifp);
1246 }
1247
1248 void
1249 in_if_link_state_change(struct ifnet *ifp, int link_state)
1250 {
1251
1252 switch (link_state) {
1253 case LINK_STATE_DOWN:
1254 in_if_link_down(ifp);
1255 break;
1256 case LINK_STATE_UP:
1257 in_if_link_up(ifp);
1258 break;
1259 }
1260 }
1261
1262 /*
1263 * in_lookup_multi: look up the in_multi record for a given IP
1264 * multicast address on a given interface. If no matching record is
1265 * found, return NULL.
1266 */
1267 struct in_multi *
1268 in_lookup_multi(struct in_addr addr, ifnet_t *ifp)
1269 {
1270 struct in_multi *inm;
1271
1272 KASSERT(rw_lock_held(&in_multilock));
1273
1274 LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) {
1275 if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp)
1276 break;
1277 }
1278 return inm;
1279 }
1280
1281 /*
1282 * in_multi_group: check whether the address belongs to an IP multicast
1283 * group we are joined on this interface. Returns true or false.
1284 */
1285 bool
1286 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags)
1287 {
1288 bool ingroup;
1289
1290 if (__predict_true(flags & IP_IGMP_MCAST) == 0) {
1291 rw_enter(&in_multilock, RW_READER);
1292 ingroup = in_lookup_multi(addr, ifp) != NULL;
1293 rw_exit(&in_multilock);
1294 } else {
1295 /* XXX Recursive call from ip_output(). */
1296 KASSERT(rw_lock_held(&in_multilock));
1297 ingroup = in_lookup_multi(addr, ifp) != NULL;
1298 }
1299 return ingroup;
1300 }
1301
1302 /*
1303 * Add an address to the list of IP multicast addresses for a given interface.
1304 */
1305 struct in_multi *
1306 in_addmulti(struct in_addr *ap, ifnet_t *ifp)
1307 {
1308 struct sockaddr_in sin;
1309 struct in_multi *inm;
1310
1311 /*
1312 * See if address already in list.
1313 */
1314 rw_enter(&in_multilock, RW_WRITER);
1315 inm = in_lookup_multi(*ap, ifp);
1316 if (inm != NULL) {
1317 /*
1318 * Found it; just increment the reference count.
1319 */
1320 inm->inm_refcount++;
1321 rw_exit(&in_multilock);
1322 return inm;
1323 }
1324
1325 /*
1326 * New address; allocate a new multicast record.
1327 */
1328 inm = pool_get(&inmulti_pool, PR_NOWAIT);
1329 if (inm == NULL) {
1330 rw_exit(&in_multilock);
1331 return NULL;
1332 }
1333 inm->inm_addr = *ap;
1334 inm->inm_ifp = ifp;
1335 inm->inm_refcount = 1;
1336
1337 /*
1338 * Ask the network driver to update its multicast reception
1339 * filter appropriately for the new address.
1340 */
1341 sockaddr_in_init(&sin, ap, 0);
1342 if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
1343 rw_exit(&in_multilock);
1344 pool_put(&inmulti_pool, inm);
1345 return NULL;
1346 }
1347
1348 /*
1349 * Let IGMP know that we have joined a new IP multicast group.
1350 */
1351 if (igmp_joingroup(inm) != 0) {
1352 rw_exit(&in_multilock);
1353 pool_put(&inmulti_pool, inm);
1354 return NULL;
1355 }
1356 LIST_INSERT_HEAD(
1357 &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1358 inm, inm_list);
1359 in_multientries++;
1360 rw_exit(&in_multilock);
1361
1362 return inm;
1363 }
1364
1365 /*
1366 * Delete a multicast address record.
1367 */
1368 void
1369 in_delmulti(struct in_multi *inm)
1370 {
1371 struct sockaddr_in sin;
1372
1373 rw_enter(&in_multilock, RW_WRITER);
1374 if (--inm->inm_refcount > 0) {
1375 rw_exit(&in_multilock);
1376 return;
1377 }
1378
1379 /*
1380 * No remaining claims to this record; let IGMP know that
1381 * we are leaving the multicast group.
1382 */
1383 igmp_leavegroup(inm);
1384
1385 /*
1386 * Notify the network driver to update its multicast reception
1387 * filter.
1388 */
1389 sockaddr_in_init(&sin, &inm->inm_addr, 0);
1390 if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
1391
1392 /*
1393 * Unlink from list.
1394 */
1395 LIST_REMOVE(inm, inm_list);
1396 in_multientries--;
1397 rw_exit(&in_multilock);
1398
1399 pool_put(&inmulti_pool, inm);
1400 }
1401
1402 /*
1403 * in_next_multi: step through all of the in_multi records, one at a time.
1404 * The current position is remembered in "step", which the caller must
1405 * provide. in_first_multi(), below, must be called to initialize "step"
1406 * and get the first record. Both macros return a NULL "inm" when there
1407 * are no remaining records.
1408 */
1409 struct in_multi *
1410 in_next_multi(struct in_multistep *step)
1411 {
1412 struct in_multi *inm;
1413
1414 KASSERT(rw_lock_held(&in_multilock));
1415
1416 while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) {
1417 step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]);
1418 }
1419 if ((inm = step->i_inm) != NULL) {
1420 step->i_inm = LIST_NEXT(inm, inm_list);
1421 }
1422 return inm;
1423 }
1424
1425 struct in_multi *
1426 in_first_multi(struct in_multistep *step)
1427 {
1428 KASSERT(rw_lock_held(&in_multilock));
1429
1430 step->i_n = 0;
1431 step->i_inm = LIST_FIRST(&in_multihashtbl[0]);
1432 return in_next_multi(step);
1433 }
1434
1435 void
1436 in_multi_lock(int op)
1437 {
1438 rw_enter(&in_multilock, op);
1439 }
1440
1441 void
1442 in_multi_unlock(void)
1443 {
1444 rw_exit(&in_multilock);
1445 }
1446
1447 int
1448 in_multi_lock_held(void)
1449 {
1450 return rw_lock_held(&in_multilock);
1451 }
1452
1453 struct sockaddr_in *
1454 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
1455 int soopts, struct ip_moptions *mopts, int *errorp)
1456 {
1457 struct rtentry *rt = NULL;
1458 struct in_ifaddr *ia = NULL;
1459
1460 /*
1461 * If route is known or can be allocated now, take the
1462 * source address from the interface. Otherwise, punt.
1463 */
1464 if ((soopts & SO_DONTROUTE) != 0)
1465 rtcache_free(ro);
1466 else {
1467 union {
1468 struct sockaddr dst;
1469 struct sockaddr_in dst4;
1470 } u;
1471
1472 sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
1473 rt = rtcache_lookup(ro, &u.dst);
1474 }
1475 /*
1476 * If we found a route, use the address
1477 * corresponding to the outgoing interface
1478 * unless it is the loopback (in case a route
1479 * to our address on another net goes to loopback).
1480 *
1481 * XXX Is this still true? Do we care?
1482 */
1483 if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
1484 ia = ifatoia(rt->rt_ifa);
1485 if (ia == NULL) {
1486 u_int16_t fport = sin->sin_port;
1487
1488 sin->sin_port = 0;
1489 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
1490 sin->sin_port = fport;
1491 if (ia == NULL) {
1492 /* Find 1st non-loopback AF_INET address */
1493 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1494 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
1495 break;
1496 }
1497 }
1498 if (ia == NULL) {
1499 *errorp = EADDRNOTAVAIL;
1500 return NULL;
1501 }
1502 }
1503 /*
1504 * If the destination address is multicast and an outgoing
1505 * interface has been set as a multicast option, use the
1506 * address of that interface as our source address.
1507 */
1508 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
1509 struct ip_moptions *imo;
1510 struct ifnet *ifp;
1511
1512 imo = mopts;
1513 if (imo->imo_multicast_ifp != NULL) {
1514 ifp = imo->imo_multicast_ifp;
1515 IFP_TO_IA(ifp, ia); /* XXX */
1516 if (ia == 0 || ia->ia4_flags & IN_IFF_NOTREADY) {
1517 *errorp = EADDRNOTAVAIL;
1518 return NULL;
1519 }
1520 }
1521 }
1522 if (ia->ia_ifa.ifa_getifa != NULL) {
1523 ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
1524 sintosa(sin)));
1525 if (ia == NULL) {
1526 *errorp = EADDRNOTAVAIL;
1527 return NULL;
1528 }
1529 }
1530 #ifdef GETIFA_DEBUG
1531 else
1532 printf("%s: missing ifa_getifa\n", __func__);
1533 #endif
1534 return satosin(&ia->ia_addr);
1535 }
1536
1537 struct in_llentry {
1538 struct llentry base;
1539 };
1540
1541 #define IN_LLTBL_DEFAULT_HSIZE 32
1542 #define IN_LLTBL_HASH(k, h) \
1543 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1544
1545 /*
1546 * Do actual deallocation of @lle.
1547 * Called by LLE_FREE_LOCKED when number of references
1548 * drops to zero.
1549 */
1550 static void
1551 in_lltable_destroy_lle(struct llentry *lle)
1552 {
1553
1554 LLE_WUNLOCK(lle);
1555 LLE_LOCK_DESTROY(lle);
1556 kmem_intr_free(lle, sizeof(*lle));
1557 }
1558
1559 static struct llentry *
1560 in_lltable_new(struct in_addr addr4, u_int flags)
1561 {
1562 struct in_llentry *lle;
1563
1564 lle = kmem_intr_zalloc(sizeof(*lle), KM_NOSLEEP);
1565 if (lle == NULL) /* NB: caller generates msg */
1566 return NULL;
1567
1568 /*
1569 * For IPv4 this will trigger "arpresolve" to generate
1570 * an ARP request.
1571 */
1572 lle->base.la_expire = time_uptime; /* mark expired */
1573 lle->base.r_l3addr.addr4 = addr4;
1574 lle->base.lle_refcnt = 1;
1575 lle->base.lle_free = in_lltable_destroy_lle;
1576 LLE_LOCK_INIT(&lle->base);
1577 callout_init(&lle->base.la_timer, 1);
1578
1579 return (&lle->base);
1580 }
1581
1582 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1583 (((ntohl((d).s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1584
1585 static int
1586 in_lltable_match_prefix(const struct sockaddr *prefix,
1587 const struct sockaddr *mask, u_int flags, struct llentry *lle)
1588 {
1589 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1590 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1591
1592 /*
1593 * (flags & LLE_STATIC) means deleting all entries
1594 * including static ARP entries.
1595 */
1596 if (IN_ARE_MASKED_ADDR_EQUAL(lle->r_l3addr.addr4, pfx, msk) &&
1597 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
1598 return (1);
1599
1600 return (0);
1601 }
1602
1603 static void
1604 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1605 {
1606 struct ifnet *ifp;
1607 size_t pkts_dropped;
1608
1609 LLE_WLOCK_ASSERT(lle);
1610 KASSERT(llt != NULL);
1611
1612 /* Unlink entry from table if not already */
1613 if ((lle->la_flags & LLE_LINKED) != 0) {
1614 ifp = llt->llt_ifp;
1615 IF_AFDATA_WLOCK_ASSERT(ifp);
1616 lltable_unlink_entry(llt, lle);
1617 }
1618
1619 /* cancel timer */
1620 if (callout_stop(&lle->lle_timer))
1621 LLE_REMREF(lle);
1622
1623 /* Drop hold queue */
1624 pkts_dropped = llentry_free(lle);
1625 #ifdef __FreeBSD__
1626 ARPSTAT_ADD(dropped, pkts_dropped);
1627 #else
1628 (void) pkts_dropped; /* FIXME */
1629 #endif
1630 }
1631
1632 static int
1633 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1634 {
1635 struct rtentry *rt;
1636 int error = EINVAL;
1637
1638 KASSERTMSG(l3addr->sa_family == AF_INET,
1639 "sin_family %d", l3addr->sa_family);
1640
1641 rt = rtalloc1(l3addr, 0);
1642 if (rt == NULL)
1643 return error;
1644
1645 /*
1646 * If the gateway for an existing host route matches the target L3
1647 * address, which is a special route inserted by some implementation
1648 * such as MANET, and the interface is of the correct type, then
1649 * allow for ARP to proceed.
1650 */
1651 if (rt->rt_flags & RTF_GATEWAY) {
1652 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1653 rt->rt_ifp->if_type != IFT_ETHER ||
1654 #ifdef __FreeBSD__
1655 (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1656 #else /* XXX */
1657 (rt->rt_ifp->if_flags & IFF_NOARP) != 0 ||
1658 #endif
1659 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1660 sizeof(in_addr_t)) != 0) {
1661 goto error;
1662 }
1663 }
1664
1665 /*
1666 * Make sure that at least the destination address is covered
1667 * by the route. This is for handling the case where 2 or more
1668 * interfaces have the same prefix. An incoming packet arrives
1669 * on one interface and the corresponding outgoing packet leaves
1670 * another interface.
1671 */
1672 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1673 const char *sa, *mask, *addr, *lim;
1674 int len;
1675
1676 mask = (const char *)rt_mask(rt);
1677 /*
1678 * Just being extra cautious to avoid some custom
1679 * code getting into trouble.
1680 */
1681 if (mask == NULL)
1682 goto error;
1683
1684 sa = (const char *)rt_getkey(rt);
1685 addr = (const char *)l3addr;
1686 len = ((const struct sockaddr_in *)l3addr)->sin_len;
1687 lim = addr + len;
1688
1689 for ( ; addr < lim; sa++, mask++, addr++) {
1690 if ((*sa ^ *addr) & *mask) {
1691 #ifdef DIAGNOSTIC
1692 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1693 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1694 #endif
1695 goto error;
1696 }
1697 }
1698 }
1699
1700 error = 0;
1701 error:
1702 #ifdef __FreeBSD__
1703 RTFREE_LOCKED(rt);
1704 #endif
1705 return error;
1706 }
1707
1708 static inline uint32_t
1709 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1710 {
1711
1712 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1713 }
1714
1715 static uint32_t
1716 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1717 {
1718
1719 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1720 }
1721
1722 static void
1723 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1724 {
1725 struct sockaddr_in *sin;
1726
1727 sin = (struct sockaddr_in *)sa;
1728 memset(sin, 0, sizeof(*sin));
1729 sin->sin_family = AF_INET;
1730 sin->sin_len = sizeof(*sin);
1731 sin->sin_addr = lle->r_l3addr.addr4;
1732 }
1733
1734 static inline struct llentry *
1735 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1736 {
1737 struct llentry *lle;
1738 struct llentries *lleh;
1739 u_int hashidx;
1740
1741 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1742 lleh = &llt->lle_head[hashidx];
1743 LIST_FOREACH(lle, lleh, lle_next) {
1744 if (lle->la_flags & LLE_DELETED)
1745 continue;
1746 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1747 break;
1748 }
1749
1750 return (lle);
1751 }
1752
1753 static int
1754 in_lltable_delete(struct lltable *llt, u_int flags,
1755 const struct sockaddr *l3addr)
1756 {
1757 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1758 struct ifnet *ifp = llt->llt_ifp;
1759 struct llentry *lle;
1760
1761 IF_AFDATA_WLOCK_ASSERT(ifp);
1762 KASSERTMSG(l3addr->sa_family == AF_INET,
1763 "sin_family %d", l3addr->sa_family);
1764
1765 lle = in_lltable_find_dst(llt, sin->sin_addr);
1766 if (lle == NULL) {
1767 #ifdef DIAGNOSTIC
1768 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle);
1769 #endif
1770 return (ENOENT);
1771 }
1772
1773 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1774 LLE_WLOCK(lle);
1775 lle->la_flags |= LLE_DELETED;
1776 #ifdef __FreeBSD__
1777 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1778 #endif
1779 #ifdef DIAGNOSTIC
1780 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1781 #endif
1782 if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
1783 llentry_free(lle);
1784 else
1785 LLE_WUNLOCK(lle);
1786 }
1787
1788 return (0);
1789 }
1790
1791 static struct llentry *
1792 in_lltable_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1793 {
1794 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1795 struct ifnet *ifp = llt->llt_ifp;
1796 struct llentry *lle;
1797
1798 IF_AFDATA_WLOCK_ASSERT(ifp);
1799 KASSERTMSG(l3addr->sa_family == AF_INET,
1800 "sin_family %d", l3addr->sa_family);
1801
1802 lle = in_lltable_find_dst(llt, sin->sin_addr);
1803
1804 if (lle != NULL) {
1805 LLE_WLOCK(lle);
1806 return (lle);
1807 }
1808
1809 /* no existing record, we need to create new one */
1810
1811 /*
1812 * A route that covers the given address must have
1813 * been installed 1st because we are doing a resolution,
1814 * verify this.
1815 */
1816 if (!(flags & LLE_IFADDR) &&
1817 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1818 return (NULL);
1819
1820 lle = in_lltable_new(sin->sin_addr, flags);
1821 if (lle == NULL) {
1822 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1823 return (NULL);
1824 }
1825 lle->la_flags = flags;
1826 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1827 memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
1828 lle->la_flags |= (LLE_VALID | LLE_STATIC);
1829 }
1830
1831 lltable_link_entry(llt, lle);
1832 LLE_WLOCK(lle);
1833
1834 return (lle);
1835 }
1836
1837 /*
1838 * Return NULL if not found or marked for deletion.
1839 * If found return lle read locked.
1840 */
1841 static struct llentry *
1842 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1843 {
1844 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1845 struct llentry *lle;
1846
1847 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1848 KASSERTMSG(l3addr->sa_family == AF_INET,
1849 "sin_family %d", l3addr->sa_family);
1850
1851 lle = in_lltable_find_dst(llt, sin->sin_addr);
1852
1853 if (lle == NULL)
1854 return NULL;
1855
1856 if (flags & LLE_EXCLUSIVE)
1857 LLE_WLOCK(lle);
1858 else
1859 LLE_RLOCK(lle);
1860
1861 return lle;
1862 }
1863
1864 static void
1865 in_sysctl_init(struct sysctllog **clog)
1866 {
1867 sysctl_createv(clog, 0, NULL, NULL,
1868 CTLFLAG_PERMANENT,
1869 CTLTYPE_NODE, "inet",
1870 SYSCTL_DESCR("PF_INET related settings"),
1871 NULL, 0, NULL, 0,
1872 CTL_NET, PF_INET, CTL_EOL);
1873 sysctl_createv(clog, 0, NULL, NULL,
1874 CTLFLAG_PERMANENT,
1875 CTLTYPE_NODE, "ip",
1876 SYSCTL_DESCR("IPv4 related settings"),
1877 NULL, 0, NULL, 0,
1878 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
1879
1880 sysctl_createv(clog, 0, NULL, NULL,
1881 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1882 CTLTYPE_INT, "subnetsarelocal",
1883 SYSCTL_DESCR("Whether logical subnets are considered "
1884 "local"),
1885 NULL, 0, &subnetsarelocal, 0,
1886 CTL_NET, PF_INET, IPPROTO_IP,
1887 IPCTL_SUBNETSARELOCAL, CTL_EOL);
1888 sysctl_createv(clog, 0, NULL, NULL,
1889 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1890 CTLTYPE_INT, "hostzerobroadcast",
1891 SYSCTL_DESCR("All zeroes address is broadcast address"),
1892 NULL, 0, &hostzeroisbroadcast, 0,
1893 CTL_NET, PF_INET, IPPROTO_IP,
1894 IPCTL_HOSTZEROBROADCAST, CTL_EOL);
1895 }
1896
1897 static struct lltable *
1898 in_lltattach(struct ifnet *ifp)
1899 {
1900 struct lltable *llt;
1901
1902 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1903 llt->llt_af = AF_INET;
1904 llt->llt_ifp = ifp;
1905
1906 llt->llt_lookup = in_lltable_lookup;
1907 llt->llt_create = in_lltable_create;
1908 llt->llt_delete = in_lltable_delete;
1909 #if 0
1910 llt->llt_dump_entry = in_lltable_dump_entry;
1911 #endif
1912 llt->llt_hash = in_lltable_hash;
1913 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1914 llt->llt_free_entry = in_lltable_free_entry;
1915 llt->llt_match_prefix = in_lltable_match_prefix;
1916 lltable_link(llt);
1917
1918 return (llt);
1919 }
1920
1921 void *
1922 in_domifattach(struct ifnet *ifp)
1923 {
1924 struct in_ifinfo *ii;
1925
1926 ii = kmem_zalloc(sizeof(struct in_ifinfo), KM_SLEEP);
1927 KASSERT(ii != NULL);
1928
1929 ii->ii_llt = in_lltattach(ifp);
1930
1931 #ifdef IPSELSRC
1932 ii->ii_selsrc = in_selsrc_domifattach(ifp);
1933 KASSERT(ii->ii_selsrc != NULL);
1934 #endif
1935
1936 return ii;
1937 }
1938
1939 void
1940 in_domifdetach(struct ifnet *ifp, void *aux)
1941 {
1942 struct in_ifinfo *ii = aux;
1943
1944 #ifdef IPSELSRC
1945 in_selsrc_domifdetach(ifp, ii->ii_selsrc);
1946 #endif
1947 lltable_free(ii->ii_llt);
1948 kmem_free(ii, sizeof(struct in_ifinfo));
1949 }
1950