1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (C) 2001 WIDE Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)in.c 8.4 (Berkeley) 1/9/95
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_mpath.h"
39
40 #include <sys/param.h>
41 #include <sys/eventhandler.h>
42 #include <sys/systm.h>
43 #include <sys/sockio.h>
44 #include <sys/malloc.h>
45 #include <sys/priv.h>
46 #include <sys/socket.h>
47 #include <sys/jail.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/proc.h>
51 #include <sys/rmlock.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/sx.h>
55
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_arp.h>
59 #include <net/if_dl.h>
60 #include <net/if_llatbl.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63 #include <net/vnet.h>
64
65 #include <netinet/if_ether.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/ip_carp.h>
71 #include <netinet/igmp_var.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
74
75 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
76 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
77
78 static void in_socktrim(struct sockaddr_in *);
79 static void in_purgemaddrs(struct ifnet *);
80
81 VNET_DEFINE_STATIC(int, nosameprefix);
82 #define V_nosameprefix VNET(nosameprefix)
83 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
84 &VNET_NAME(nosameprefix), 0,
85 "Refuse to create same prefixes on different interfaces");
86
87 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
88 #define V_ripcbinfo VNET(ripcbinfo)
89
90 static struct sx in_control_sx;
91 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
92
93 /*
94 * Return 1 if an internet address is for a ``local'' host
95 * (one to which we have a connection).
96 */
97 int
in_localaddr(struct in_addr in)98 in_localaddr(struct in_addr in)
99 {
100 struct rm_priotracker in_ifa_tracker;
101 u_long i = ntohl(in.s_addr);
102 struct in_ifaddr *ia;
103
104 IN_IFADDR_RLOCK(&in_ifa_tracker);
105 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
106 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
107 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
108 return (1);
109 }
110 }
111 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
112 return (0);
113 }
114
115 /*
116 * Return 1 if an internet address is for the local host and configured
117 * on one of its interfaces.
118 */
119 int
in_localip(struct in_addr in)120 in_localip(struct in_addr in)
121 {
122 struct rm_priotracker in_ifa_tracker;
123 struct in_ifaddr *ia;
124
125 IN_IFADDR_RLOCK(&in_ifa_tracker);
126 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
127 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
128 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
129 return (1);
130 }
131 }
132 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
133 return (0);
134 }
135
136 /*
137 * Return 1 if an internet address is configured on an interface.
138 */
139 int
in_ifhasaddr(struct ifnet * ifp,struct in_addr in)140 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
141 {
142 struct ifaddr *ifa;
143 struct in_ifaddr *ia;
144
145 IF_ADDR_RLOCK(ifp);
146 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
147 if (ifa->ifa_addr->sa_family != AF_INET)
148 continue;
149 ia = (struct in_ifaddr *)ifa;
150 if (ia->ia_addr.sin_addr.s_addr == in.s_addr) {
151 IF_ADDR_RUNLOCK(ifp);
152 return (1);
153 }
154 }
155 IF_ADDR_RUNLOCK(ifp);
156
157 return (0);
158 }
159
160 /*
161 * Return a reference to the interface address which is different to
162 * the supplied one but with same IP address value.
163 */
164 static struct in_ifaddr *
in_localip_more(struct in_ifaddr * ia)165 in_localip_more(struct in_ifaddr *ia)
166 {
167 struct rm_priotracker in_ifa_tracker;
168 in_addr_t in = IA_SIN(ia)->sin_addr.s_addr;
169 struct in_ifaddr *it;
170
171 IN_IFADDR_RLOCK(&in_ifa_tracker);
172 LIST_FOREACH(it, INADDR_HASH(in), ia_hash) {
173 if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) {
174 ifa_ref(&it->ia_ifa);
175 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
176 return (it);
177 }
178 }
179 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
180
181 return (NULL);
182 }
183
184 /*
185 * Determine whether an IP address is in a reserved set of addresses
186 * that may not be forwarded, or whether datagrams to that destination
187 * may be forwarded.
188 */
189 int
in_canforward(struct in_addr in)190 in_canforward(struct in_addr in)
191 {
192 u_long i = ntohl(in.s_addr);
193
194 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i) ||
195 IN_ZERONET(i) || IN_LOOPBACK(i))
196 return (0);
197 return (1);
198 }
199
200 /*
201 * Trim a mask in a sockaddr
202 */
203 static void
in_socktrim(struct sockaddr_in * ap)204 in_socktrim(struct sockaddr_in *ap)
205 {
206 char *cplim = (char *) &ap->sin_addr;
207 char *cp = (char *) (&ap->sin_addr + 1);
208
209 ap->sin_len = 0;
210 while (--cp >= cplim)
211 if (*cp) {
212 (ap)->sin_len = cp - (char *) (ap) + 1;
213 break;
214 }
215 }
216
217 /*
218 * Generic internet control operations (ioctl's).
219 */
220 int
in_control(struct socket * so,u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)221 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
222 struct thread *td)
223 {
224 struct ifreq *ifr = (struct ifreq *)data;
225 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
226 struct ifaddr *ifa;
227 struct in_ifaddr *ia;
228 int error;
229
230 if (ifp == NULL)
231 return (EADDRNOTAVAIL);
232
233 /*
234 * Filter out 4 ioctls we implement directly. Forward the rest
235 * to specific functions and ifp->if_ioctl().
236 */
237 switch (cmd) {
238 case SIOCGIFADDR:
239 case SIOCGIFBRDADDR:
240 case SIOCGIFDSTADDR:
241 case SIOCGIFNETMASK:
242 break;
243 case SIOCDIFADDR:
244 sx_xlock(&in_control_sx);
245 error = in_difaddr_ioctl(cmd, data, ifp, td);
246 sx_xunlock(&in_control_sx);
247 return (error);
248 case OSIOCAIFADDR: /* 9.x compat */
249 case SIOCAIFADDR:
250 sx_xlock(&in_control_sx);
251 error = in_aifaddr_ioctl(cmd, data, ifp, td);
252 sx_xunlock(&in_control_sx);
253 return (error);
254 case SIOCSIFADDR:
255 case SIOCSIFBRDADDR:
256 case SIOCSIFDSTADDR:
257 case SIOCSIFNETMASK:
258 /* We no longer support that old commands. */
259 return (EINVAL);
260 default:
261 if (ifp->if_ioctl == NULL)
262 return (EOPNOTSUPP);
263 return ((*ifp->if_ioctl)(ifp, cmd, data));
264 }
265
266 if (addr->sin_addr.s_addr != INADDR_ANY &&
267 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
268 return (EADDRNOTAVAIL);
269
270 /*
271 * Find address for this interface, if it exists. If an
272 * address was specified, find that one instead of the
273 * first one on the interface, if possible.
274 */
275 IF_ADDR_RLOCK(ifp);
276 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
277 if (ifa->ifa_addr->sa_family != AF_INET)
278 continue;
279 ia = (struct in_ifaddr *)ifa;
280 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
281 break;
282 }
283 if (ifa == NULL)
284 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
285 if (ifa->ifa_addr->sa_family == AF_INET) {
286 ia = (struct in_ifaddr *)ifa;
287 if (prison_check_ip4(td->td_ucred,
288 &ia->ia_addr.sin_addr) == 0)
289 break;
290 }
291
292 if (ifa == NULL) {
293 IF_ADDR_RUNLOCK(ifp);
294 return (EADDRNOTAVAIL);
295 }
296
297 error = 0;
298 switch (cmd) {
299 case SIOCGIFADDR:
300 *addr = ia->ia_addr;
301 break;
302
303 case SIOCGIFBRDADDR:
304 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
305 error = EINVAL;
306 break;
307 }
308 *addr = ia->ia_broadaddr;
309 break;
310
311 case SIOCGIFDSTADDR:
312 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
313 error = EINVAL;
314 break;
315 }
316 *addr = ia->ia_dstaddr;
317 break;
318
319 case SIOCGIFNETMASK:
320 *addr = ia->ia_sockmask;
321 break;
322 }
323
324 IF_ADDR_RUNLOCK(ifp);
325
326 return (error);
327 }
328
329 static int
in_aifaddr_ioctl(u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)330 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
331 {
332 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
333 const struct sockaddr_in *addr = &ifra->ifra_addr;
334 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
335 const struct sockaddr_in *mask = &ifra->ifra_mask;
336 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
337 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
338 struct ifaddr *ifa;
339 struct in_ifaddr *ia;
340 bool iaIsFirst;
341 int error = 0;
342
343 error = priv_check(td, PRIV_NET_ADDIFADDR);
344 if (error)
345 return (error);
346
347 /*
348 * ifra_addr must be present and be of INET family.
349 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
350 */
351 if (addr->sin_len != sizeof(struct sockaddr_in) ||
352 addr->sin_family != AF_INET)
353 return (EINVAL);
354 if (broadaddr->sin_len != 0 &&
355 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
356 broadaddr->sin_family != AF_INET))
357 return (EINVAL);
358 if (mask->sin_len != 0 &&
359 (mask->sin_len != sizeof(struct sockaddr_in) ||
360 mask->sin_family != AF_INET))
361 return (EINVAL);
362 if ((ifp->if_flags & IFF_POINTOPOINT) &&
363 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
364 dstaddr->sin_addr.s_addr == INADDR_ANY))
365 return (EDESTADDRREQ);
366 if (vhid > 0 && carp_attach_p == NULL)
367 return (EPROTONOSUPPORT);
368
369 /*
370 * See whether address already exist.
371 */
372 iaIsFirst = true;
373 ia = NULL;
374 IF_ADDR_RLOCK(ifp);
375 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
376 struct in_ifaddr *it;
377
378 if (ifa->ifa_addr->sa_family != AF_INET)
379 continue;
380
381 it = (struct in_ifaddr *)ifa;
382 iaIsFirst = false;
383 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
384 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
385 ia = it;
386 }
387 IF_ADDR_RUNLOCK(ifp);
388
389 if (ia != NULL)
390 (void )in_difaddr_ioctl(cmd, data, ifp, td);
391
392 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
393 ia = (struct in_ifaddr *)ifa;
394 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
395 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
396 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
397 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
398 CALLOUT_RETURNUNLOCKED);
399
400 ia->ia_ifp = ifp;
401 ia->ia_addr = *addr;
402 if (mask->sin_len != 0) {
403 ia->ia_sockmask = *mask;
404 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
405 } else {
406 in_addr_t i = ntohl(addr->sin_addr.s_addr);
407
408 /*
409 * Be compatible with network classes, if netmask isn't
410 * supplied, guess it based on classes.
411 */
412 if (IN_CLASSA(i))
413 ia->ia_subnetmask = IN_CLASSA_NET;
414 else if (IN_CLASSB(i))
415 ia->ia_subnetmask = IN_CLASSB_NET;
416 else
417 ia->ia_subnetmask = IN_CLASSC_NET;
418 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
419 }
420 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
421 in_socktrim(&ia->ia_sockmask);
422
423 if (ifp->if_flags & IFF_BROADCAST) {
424 if (broadaddr->sin_len != 0) {
425 ia->ia_broadaddr = *broadaddr;
426 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
427 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
428 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
429 ia->ia_broadaddr.sin_family = AF_INET;
430 } else {
431 ia->ia_broadaddr.sin_addr.s_addr =
432 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
433 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
434 ia->ia_broadaddr.sin_family = AF_INET;
435 }
436 }
437
438 if (ifp->if_flags & IFF_POINTOPOINT)
439 ia->ia_dstaddr = *dstaddr;
440
441 /* XXXGL: rtinit() needs this strange assignment. */
442 if (ifp->if_flags & IFF_LOOPBACK)
443 ia->ia_dstaddr = ia->ia_addr;
444
445 if (vhid != 0) {
446 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
447 if (error)
448 return (error);
449 }
450
451 /* if_addrhead is already referenced by ifa_alloc() */
452 IF_ADDR_WLOCK(ifp);
453 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
454 IF_ADDR_WUNLOCK(ifp);
455
456 ifa_ref(ifa); /* in_ifaddrhead */
457 IN_IFADDR_WLOCK();
458 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
459 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
460 IN_IFADDR_WUNLOCK();
461
462 /*
463 * Give the interface a chance to initialize
464 * if this is its first address,
465 * and to validate the address if necessary.
466 */
467 if (ifp->if_ioctl != NULL) {
468 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
469 if (error)
470 goto fail1;
471 }
472
473 /*
474 * Add route for the network.
475 */
476 if (vhid == 0) {
477 int flags = RTF_UP;
478
479 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
480 flags |= RTF_HOST;
481
482 error = in_addprefix(ia, flags);
483 if (error)
484 goto fail1;
485 }
486
487 /*
488 * Add a loopback route to self.
489 */
490 if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
491 ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
492 !((ifp->if_flags & IFF_POINTOPOINT) &&
493 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
494 struct in_ifaddr *eia;
495
496 eia = in_localip_more(ia);
497
498 if (eia == NULL) {
499 error = ifa_add_loopback_route((struct ifaddr *)ia,
500 (struct sockaddr *)&ia->ia_addr);
501 if (error)
502 goto fail2;
503 } else
504 ifa_free(&eia->ia_ifa);
505 }
506
507 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
508 struct in_addr allhosts_addr;
509 struct in_ifinfo *ii;
510
511 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
512 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
513
514 error = in_joingroup(ifp, &allhosts_addr, NULL,
515 &ii->ii_allhosts);
516 }
517
518 /*
519 * Note: we don't need extra reference for ifa, since we called
520 * with sx lock held, and ifaddr can not be deleted in concurrent
521 * thread.
522 */
523 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
524
525 return (error);
526
527 fail2:
528 if (vhid == 0)
529 (void )in_scrubprefix(ia, LLE_STATIC);
530
531 fail1:
532 if (ia->ia_ifa.ifa_carp)
533 (*carp_detach_p)(&ia->ia_ifa, false);
534
535 IF_ADDR_WLOCK(ifp);
536 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
537 IF_ADDR_WUNLOCK(ifp);
538 ifa_free(&ia->ia_ifa); /* if_addrhead */
539
540 IN_IFADDR_WLOCK();
541 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
542 LIST_REMOVE(ia, ia_hash);
543 IN_IFADDR_WUNLOCK();
544 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
545
546 return (error);
547 }
548
549 static int
in_difaddr_ioctl(u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)550 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
551 {
552 const struct ifreq *ifr = (struct ifreq *)data;
553 const struct sockaddr_in *addr = (const struct sockaddr_in *)
554 &ifr->ifr_addr;
555 struct ifaddr *ifa;
556 struct in_ifaddr *ia;
557 bool deleteAny, iaIsLast;
558 int error;
559
560 if (td != NULL) {
561 error = priv_check(td, PRIV_NET_DELIFADDR);
562 if (error)
563 return (error);
564 }
565
566 if (addr->sin_len != sizeof(struct sockaddr_in) ||
567 addr->sin_family != AF_INET)
568 deleteAny = true;
569 else
570 deleteAny = false;
571
572 iaIsLast = true;
573 ia = NULL;
574 IF_ADDR_WLOCK(ifp);
575 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
576 struct in_ifaddr *it;
577
578 if (ifa->ifa_addr->sa_family != AF_INET)
579 continue;
580
581 it = (struct in_ifaddr *)ifa;
582 if (deleteAny && ia == NULL && (td == NULL ||
583 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
584 ia = it;
585
586 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
587 (td == NULL || prison_check_ip4(td->td_ucred,
588 &addr->sin_addr) == 0))
589 ia = it;
590
591 if (it != ia)
592 iaIsLast = false;
593 }
594
595 if (ia == NULL) {
596 IF_ADDR_WUNLOCK(ifp);
597 return (EADDRNOTAVAIL);
598 }
599
600 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
601 IF_ADDR_WUNLOCK(ifp);
602 ifa_free(&ia->ia_ifa); /* if_addrhead */
603
604 IN_IFADDR_WLOCK();
605 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
606 LIST_REMOVE(ia, ia_hash);
607 IN_IFADDR_WUNLOCK();
608
609 /*
610 * in_scrubprefix() kills the interface route.
611 */
612 in_scrubprefix(ia, LLE_STATIC);
613
614 /*
615 * in_ifadown gets rid of all the rest of
616 * the routes. This is not quite the right
617 * thing to do, but at least if we are running
618 * a routing process they will come back.
619 */
620 in_ifadown(&ia->ia_ifa, 1);
621
622 if (ia->ia_ifa.ifa_carp)
623 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
624
625 /*
626 * If this is the last IPv4 address configured on this
627 * interface, leave the all-hosts group.
628 * No state-change report need be transmitted.
629 */
630 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
631 struct in_ifinfo *ii;
632
633 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
634 if (ii->ii_allhosts) {
635 (void)in_leavegroup(ii->ii_allhosts, NULL);
636 ii->ii_allhosts = NULL;
637 }
638 }
639
640 IF_ADDR_WLOCK(ifp);
641 if (callout_stop(&ia->ia_garp_timer) == 1) {
642 ifa_free(&ia->ia_ifa);
643 }
644 IF_ADDR_WUNLOCK(ifp);
645
646 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
647 IFADDR_EVENT_DEL);
648 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
649
650 return (0);
651 }
652
653 #define rtinitflags(x) \
654 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
655 ? RTF_HOST : 0)
656
657 /*
658 * Check if we have a route for the given prefix already or add one accordingly.
659 */
660 int
in_addprefix(struct in_ifaddr * target,int flags)661 in_addprefix(struct in_ifaddr *target, int flags)
662 {
663 struct rm_priotracker in_ifa_tracker;
664 struct in_ifaddr *ia;
665 struct in_addr prefix, mask, p, m;
666 int error;
667
668 if ((flags & RTF_HOST) != 0) {
669 prefix = target->ia_dstaddr.sin_addr;
670 mask.s_addr = 0;
671 } else {
672 prefix = target->ia_addr.sin_addr;
673 mask = target->ia_sockmask.sin_addr;
674 prefix.s_addr &= mask.s_addr;
675 }
676
677 IN_IFADDR_RLOCK(&in_ifa_tracker);
678 /* Look for an existing address with the same prefix, mask, and fib */
679 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
680 if (rtinitflags(ia)) {
681 p = ia->ia_dstaddr.sin_addr;
682
683 if (prefix.s_addr != p.s_addr)
684 continue;
685 } else {
686 p = ia->ia_addr.sin_addr;
687 m = ia->ia_sockmask.sin_addr;
688 p.s_addr &= m.s_addr;
689
690 if (prefix.s_addr != p.s_addr ||
691 mask.s_addr != m.s_addr)
692 continue;
693 }
694 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
695 continue;
696
697 /*
698 * If we got a matching prefix route inserted by other
699 * interface address, we are done here.
700 */
701 if (ia->ia_flags & IFA_ROUTE) {
702 #ifdef RADIX_MPATH
703 if (ia->ia_addr.sin_addr.s_addr ==
704 target->ia_addr.sin_addr.s_addr) {
705 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
706 return (EEXIST);
707 } else
708 break;
709 #endif
710 if (V_nosameprefix) {
711 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
712 return (EEXIST);
713 } else {
714 int fibnum;
715
716 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
717 target->ia_ifp->if_fib;
718 rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
719 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
720 return (0);
721 }
722 }
723 }
724 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
725
726 /*
727 * No-one seem to have this prefix route, so we try to insert it.
728 */
729 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
730 if (!error)
731 target->ia_flags |= IFA_ROUTE;
732 return (error);
733 }
734
735 /*
736 * Removes either all lle entries for given @ia, or lle
737 * corresponding to @ia address.
738 */
739 static void
in_scrubprefixlle(struct in_ifaddr * ia,int all,u_int flags)740 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
741 {
742 struct sockaddr_in addr, mask;
743 struct sockaddr *saddr, *smask;
744 struct ifnet *ifp;
745
746 saddr = (struct sockaddr *)&addr;
747 bzero(&addr, sizeof(addr));
748 addr.sin_len = sizeof(addr);
749 addr.sin_family = AF_INET;
750 smask = (struct sockaddr *)&mask;
751 bzero(&mask, sizeof(mask));
752 mask.sin_len = sizeof(mask);
753 mask.sin_family = AF_INET;
754 mask.sin_addr.s_addr = ia->ia_subnetmask;
755 ifp = ia->ia_ifp;
756
757 if (all) {
758
759 /*
760 * Remove all L2 entries matching given prefix.
761 * Convert address to host representation to avoid
762 * doing this on every callback. ia_subnetmask is already
763 * stored in host representation.
764 */
765 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
766 lltable_prefix_free(AF_INET, saddr, smask, flags);
767 } else {
768 /* Remove interface address only */
769 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
770 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
771 }
772 }
773
774 /*
775 * If there is no other address in the system that can serve a route to the
776 * same prefix, remove the route. Hand over the route to the new address
777 * otherwise.
778 */
779 int
in_scrubprefix(struct in_ifaddr * target,u_int flags)780 in_scrubprefix(struct in_ifaddr *target, u_int flags)
781 {
782 struct rm_priotracker in_ifa_tracker;
783 struct in_ifaddr *ia;
784 struct in_addr prefix, mask, p, m;
785 int error = 0;
786
787 /*
788 * Remove the loopback route to the interface address.
789 */
790 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
791 !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
792 (flags & LLE_STATIC)) {
793 struct in_ifaddr *eia;
794
795 /*
796 * XXXME: add fib-aware in_localip.
797 * We definitely don't want to switch between
798 * prefixes in different fibs.
799 */
800 eia = in_localip_more(target);
801
802 if (eia != NULL) {
803 error = ifa_switch_loopback_route((struct ifaddr *)eia,
804 (struct sockaddr *)&target->ia_addr);
805 ifa_free(&eia->ia_ifa);
806 } else {
807 error = ifa_del_loopback_route((struct ifaddr *)target,
808 (struct sockaddr *)&target->ia_addr);
809 }
810 }
811
812 if (rtinitflags(target)) {
813 prefix = target->ia_dstaddr.sin_addr;
814 mask.s_addr = 0;
815 } else {
816 prefix = target->ia_addr.sin_addr;
817 mask = target->ia_sockmask.sin_addr;
818 prefix.s_addr &= mask.s_addr;
819 }
820
821 if ((target->ia_flags & IFA_ROUTE) == 0) {
822 int fibnum;
823
824 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
825 target->ia_ifp->if_fib;
826 rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
827
828 /*
829 * Removing address from !IFF_UP interface or
830 * prefix which exists on other interface (along with route).
831 * No entries should exist here except target addr.
832 * Given that, delete this entry only.
833 */
834 in_scrubprefixlle(target, 0, flags);
835 return (0);
836 }
837
838 IN_IFADDR_RLOCK(&in_ifa_tracker);
839 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
840 if (rtinitflags(ia)) {
841 p = ia->ia_dstaddr.sin_addr;
842
843 if (prefix.s_addr != p.s_addr)
844 continue;
845 } else {
846 p = ia->ia_addr.sin_addr;
847 m = ia->ia_sockmask.sin_addr;
848 p.s_addr &= m.s_addr;
849
850 if (prefix.s_addr != p.s_addr ||
851 mask.s_addr != m.s_addr)
852 continue;
853 }
854
855 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
856 continue;
857
858 /*
859 * If we got a matching prefix address, move IFA_ROUTE and
860 * the route itself to it. Make sure that routing daemons
861 * get a heads-up.
862 */
863 if ((ia->ia_flags & IFA_ROUTE) == 0) {
864 ifa_ref(&ia->ia_ifa);
865 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
866 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
867 rtinitflags(target));
868 if (error == 0)
869 target->ia_flags &= ~IFA_ROUTE;
870 else
871 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
872 error);
873 /* Scrub all entries IFF interface is different */
874 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
875 flags);
876 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
877 rtinitflags(ia) | RTF_UP);
878 if (error == 0)
879 ia->ia_flags |= IFA_ROUTE;
880 else
881 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
882 error);
883 ifa_free(&ia->ia_ifa);
884 return (error);
885 }
886 }
887 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
888
889 /*
890 * remove all L2 entries on the given prefix
891 */
892 in_scrubprefixlle(target, 1, flags);
893
894 /*
895 * As no-one seem to have this prefix, we can remove the route.
896 */
897 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
898 if (error == 0)
899 target->ia_flags &= ~IFA_ROUTE;
900 else
901 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
902 return (error);
903 }
904
905 #undef rtinitflags
906
907 void
in_ifscrub_all(void)908 in_ifscrub_all(void)
909 {
910 struct ifnet *ifp;
911 struct ifaddr *ifa, *nifa;
912 struct ifaliasreq ifr;
913
914 IFNET_RLOCK();
915 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
916 /* Cannot lock here - lock recursion. */
917 /* IF_ADDR_RLOCK(ifp); */
918 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
919 if (ifa->ifa_addr->sa_family != AF_INET)
920 continue;
921
922 /*
923 * This is ugly but the only way for legacy IP to
924 * cleanly remove addresses and everything attached.
925 */
926 bzero(&ifr, sizeof(ifr));
927 ifr.ifra_addr = *ifa->ifa_addr;
928 if (ifa->ifa_dstaddr)
929 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
930 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
931 ifp, NULL);
932 }
933 /* IF_ADDR_RUNLOCK(ifp); */
934 in_purgemaddrs(ifp);
935 igmp_domifdetach(ifp);
936 }
937 IFNET_RUNLOCK();
938 }
939
940 int
in_ifaddr_broadcast(struct in_addr in,struct in_ifaddr * ia)941 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
942 {
943
944 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
945 /*
946 * Check for old-style (host 0) broadcast, but
947 * taking into account that RFC 3021 obsoletes it.
948 */
949 (ia->ia_subnetmask != IN_RFC3021_MASK &&
950 ntohl(in.s_addr) == ia->ia_subnet)) &&
951 /*
952 * Check for an all one subnetmask. These
953 * only exist when an interface gets a secondary
954 * address.
955 */
956 ia->ia_subnetmask != (u_long)0xffffffff);
957 }
958
959 /*
960 * Return 1 if the address might be a local broadcast address.
961 */
962 int
in_broadcast(struct in_addr in,struct ifnet * ifp)963 in_broadcast(struct in_addr in, struct ifnet *ifp)
964 {
965 struct ifaddr *ifa;
966 int found;
967
968 if (in.s_addr == INADDR_BROADCAST ||
969 in.s_addr == INADDR_ANY)
970 return (1);
971 if ((ifp->if_flags & IFF_BROADCAST) == 0)
972 return (0);
973 found = 0;
974 /*
975 * Look through the list of addresses for a match
976 * with a broadcast address.
977 */
978 IF_ADDR_RLOCK(ifp);
979 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
980 if (ifa->ifa_addr->sa_family == AF_INET &&
981 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
982 found = 1;
983 break;
984 }
985 IF_ADDR_RUNLOCK(ifp);
986 return (found);
987 }
988
989 /*
990 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
991 */
992 void
in_ifdetach(struct ifnet * ifp)993 in_ifdetach(struct ifnet *ifp)
994 {
995 IN_MULTI_LOCK();
996 in_pcbpurgeif0(&V_ripcbinfo, ifp);
997 in_pcbpurgeif0(&V_udbinfo, ifp);
998 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
999 in_purgemaddrs(ifp);
1000 IN_MULTI_UNLOCK();
1001 }
1002
1003 /*
1004 * Delete all IPv4 multicast address records, and associated link-layer
1005 * multicast address records, associated with ifp.
1006 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1007 * XXX This should not race with ifma_protospec being set during
1008 * a new allocation, if it does, we have bigger problems.
1009 */
1010 static void
in_purgemaddrs(struct ifnet * ifp)1011 in_purgemaddrs(struct ifnet *ifp)
1012 {
1013 struct in_multi_head purgeinms;
1014 struct in_multi *inm;
1015 struct ifmultiaddr *ifma, *next;
1016
1017 SLIST_INIT(&purgeinms);
1018 IN_MULTI_LIST_LOCK();
1019
1020 /*
1021 * Extract list of in_multi associated with the detaching ifp
1022 * which the PF_INET layer is about to release.
1023 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1024 * by code further down.
1025 */
1026 IF_ADDR_WLOCK(ifp);
1027 restart:
1028 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1029 if (ifma->ifma_addr->sa_family != AF_INET ||
1030 ifma->ifma_protospec == NULL)
1031 continue;
1032 inm = (struct in_multi *)ifma->ifma_protospec;
1033 inm_rele_locked(&purgeinms, inm);
1034 if (__predict_false(ifma_restart)) {
1035 ifma_restart = true;
1036 goto restart;
1037 }
1038 }
1039 IF_ADDR_WUNLOCK(ifp);
1040
1041 inm_release_list_deferred(&purgeinms);
1042 igmp_ifdetach(ifp);
1043 IN_MULTI_LIST_UNLOCK();
1044 }
1045
1046 struct in_llentry {
1047 struct llentry base;
1048 };
1049
1050 #define IN_LLTBL_DEFAULT_HSIZE 32
1051 #define IN_LLTBL_HASH(k, h) \
1052 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1053
1054 /*
1055 * Do actual deallocation of @lle.
1056 */
1057 static void
in_lltable_destroy_lle_unlocked(epoch_context_t ctx)1058 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1059 {
1060 struct llentry *lle;
1061
1062 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1063 LLE_LOCK_DESTROY(lle);
1064 LLE_REQ_DESTROY(lle);
1065 free(lle, M_LLTABLE);
1066 }
1067
1068 /*
1069 * Called by the datapath to indicate that
1070 * the entry was used.
1071 */
1072 static void
in_lltable_mark_used(struct llentry * lle)1073 in_lltable_mark_used(struct llentry *lle)
1074 {
1075
1076 LLE_REQ_LOCK(lle);
1077 lle->r_skip_req = 0;
1078 LLE_REQ_UNLOCK(lle);
1079 }
1080
1081 /*
1082 * Called by LLE_FREE_LOCKED when number of references
1083 * drops to zero.
1084 */
1085 static void
in_lltable_destroy_lle(struct llentry * lle)1086 in_lltable_destroy_lle(struct llentry *lle)
1087 {
1088
1089 LLE_WUNLOCK(lle);
1090 epoch_call(net_epoch_preempt, &lle->lle_epoch_ctx, in_lltable_destroy_lle_unlocked);
1091 }
1092
1093 static struct llentry *
in_lltable_new(struct in_addr addr4,u_int flags)1094 in_lltable_new(struct in_addr addr4, u_int flags)
1095 {
1096 struct in_llentry *lle;
1097
1098 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1099 if (lle == NULL) /* NB: caller generates msg */
1100 return NULL;
1101
1102 /*
1103 * For IPv4 this will trigger "arpresolve" to generate
1104 * an ARP request.
1105 */
1106 lle->base.la_expire = time_uptime; /* mark expired */
1107 lle->base.r_l3addr.addr4 = addr4;
1108 lle->base.lle_refcnt = 1;
1109 lle->base.lle_free = in_lltable_destroy_lle;
1110 LLE_LOCK_INIT(&lle->base);
1111 LLE_REQ_INIT(&lle->base);
1112 callout_init(&lle->base.lle_timer, 1);
1113
1114 return (&lle->base);
1115 }
1116
1117 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1118 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1119
1120 static int
in_lltable_match_prefix(const struct sockaddr * saddr,const struct sockaddr * smask,u_int flags,struct llentry * lle)1121 in_lltable_match_prefix(const struct sockaddr *saddr,
1122 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1123 {
1124 struct in_addr addr, mask, lle_addr;
1125
1126 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1127 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1128 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1129
1130 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1131 return (0);
1132
1133 if (lle->la_flags & LLE_IFADDR) {
1134
1135 /*
1136 * Delete LLE_IFADDR records IFF address & flag matches.
1137 * Note that addr is the interface address within prefix
1138 * being matched.
1139 * Note also we should handle 'ifdown' cases without removing
1140 * ifaddr macs.
1141 */
1142 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1143 return (1);
1144 return (0);
1145 }
1146
1147 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1148 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1149 return (1);
1150
1151 return (0);
1152 }
1153
1154 static void
in_lltable_free_entry(struct lltable * llt,struct llentry * lle)1155 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1156 {
1157 size_t pkts_dropped;
1158
1159 LLE_WLOCK_ASSERT(lle);
1160 KASSERT(llt != NULL, ("lltable is NULL"));
1161
1162 /* Unlink entry from table if not already */
1163 if ((lle->la_flags & LLE_LINKED) != 0) {
1164 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1165 lltable_unlink_entry(llt, lle);
1166 }
1167
1168 /* Drop hold queue */
1169 pkts_dropped = llentry_free(lle);
1170 ARPSTAT_ADD(dropped, pkts_dropped);
1171 }
1172
1173 static int
in_lltable_rtcheck(struct ifnet * ifp,u_int flags,const struct sockaddr * l3addr)1174 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1175 {
1176 struct rt_addrinfo info;
1177 struct sockaddr_in rt_key, rt_mask;
1178 struct sockaddr rt_gateway;
1179 int rt_flags;
1180
1181 KASSERT(l3addr->sa_family == AF_INET,
1182 ("sin_family %d", l3addr->sa_family));
1183
1184 bzero(&rt_key, sizeof(rt_key));
1185 rt_key.sin_len = sizeof(rt_key);
1186 bzero(&rt_mask, sizeof(rt_mask));
1187 rt_mask.sin_len = sizeof(rt_mask);
1188 bzero(&rt_gateway, sizeof(rt_gateway));
1189 rt_gateway.sa_len = sizeof(rt_gateway);
1190
1191 bzero(&info, sizeof(info));
1192 info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key;
1193 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&rt_mask;
1194 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
1195
1196 if (rib_lookup_info(ifp->if_fib, l3addr, NHR_REF, 0, &info) != 0)
1197 return (EINVAL);
1198
1199 rt_flags = info.rti_flags;
1200
1201 /*
1202 * If the gateway for an existing host route matches the target L3
1203 * address, which is a special route inserted by some implementation
1204 * such as MANET, and the interface is of the correct type, then
1205 * allow for ARP to proceed.
1206 */
1207 if (rt_flags & RTF_GATEWAY) {
1208 if (!(rt_flags & RTF_HOST) || !info.rti_ifp ||
1209 info.rti_ifp->if_type != IFT_ETHER ||
1210 (info.rti_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1211 memcmp(rt_gateway.sa_data, l3addr->sa_data,
1212 sizeof(in_addr_t)) != 0) {
1213 rib_free_info(&info);
1214 return (EINVAL);
1215 }
1216 }
1217 rib_free_info(&info);
1218
1219 /*
1220 * Make sure that at least the destination address is covered
1221 * by the route. This is for handling the case where 2 or more
1222 * interfaces have the same prefix. An incoming packet arrives
1223 * on one interface and the corresponding outgoing packet leaves
1224 * another interface.
1225 */
1226 if (!(rt_flags & RTF_HOST) && info.rti_ifp != ifp) {
1227 const char *sa, *mask, *addr, *lim;
1228 const struct sockaddr_in *l3sin;
1229
1230 mask = (const char *)&rt_mask;
1231 /*
1232 * Just being extra cautious to avoid some custom
1233 * code getting into trouble.
1234 */
1235 if ((info.rti_addrs & RTA_NETMASK) == 0)
1236 return (EINVAL);
1237
1238 sa = (const char *)&rt_key;
1239 addr = (const char *)l3addr;
1240 l3sin = (const struct sockaddr_in *)l3addr;
1241 lim = addr + l3sin->sin_len;
1242
1243 for ( ; addr < lim; sa++, mask++, addr++) {
1244 if ((*sa ^ *addr) & *mask) {
1245 #ifdef DIAGNOSTIC
1246 char addrbuf[INET_ADDRSTRLEN];
1247
1248 log(LOG_INFO, "IPv4 address: \"%s\" "
1249 "is not on the network\n",
1250 inet_ntoa_r(l3sin->sin_addr, addrbuf));
1251 #endif
1252 return (EINVAL);
1253 }
1254 }
1255 }
1256
1257 return (0);
1258 }
1259
1260 static inline uint32_t
in_lltable_hash_dst(const struct in_addr dst,uint32_t hsize)1261 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1262 {
1263
1264 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1265 }
1266
1267 static uint32_t
in_lltable_hash(const struct llentry * lle,uint32_t hsize)1268 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1269 {
1270
1271 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1272 }
1273
1274 static void
in_lltable_fill_sa_entry(const struct llentry * lle,struct sockaddr * sa)1275 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1276 {
1277 struct sockaddr_in *sin;
1278
1279 sin = (struct sockaddr_in *)sa;
1280 bzero(sin, sizeof(*sin));
1281 sin->sin_family = AF_INET;
1282 sin->sin_len = sizeof(*sin);
1283 sin->sin_addr = lle->r_l3addr.addr4;
1284 }
1285
1286 static inline struct llentry *
in_lltable_find_dst(struct lltable * llt,struct in_addr dst)1287 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1288 {
1289 struct llentry *lle;
1290 struct llentries *lleh;
1291 u_int hashidx;
1292
1293 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1294 lleh = &llt->lle_head[hashidx];
1295 CK_LIST_FOREACH(lle, lleh, lle_next) {
1296 if (lle->la_flags & LLE_DELETED)
1297 continue;
1298 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1299 break;
1300 }
1301
1302 return (lle);
1303 }
1304
1305 static void
in_lltable_delete_entry(struct lltable * llt,struct llentry * lle)1306 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1307 {
1308
1309 lle->la_flags |= LLE_DELETED;
1310 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1311 #ifdef DIAGNOSTIC
1312 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1313 #endif
1314 llentry_free(lle);
1315 }
1316
1317 static struct llentry *
in_lltable_alloc(struct lltable * llt,u_int flags,const struct sockaddr * l3addr)1318 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1319 {
1320 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1321 struct ifnet *ifp = llt->llt_ifp;
1322 struct llentry *lle;
1323 char linkhdr[LLE_MAX_LINKHDR];
1324 size_t linkhdrsize;
1325 int lladdr_off;
1326
1327 KASSERT(l3addr->sa_family == AF_INET,
1328 ("sin_family %d", l3addr->sa_family));
1329
1330 /*
1331 * A route that covers the given address must have
1332 * been installed 1st because we are doing a resolution,
1333 * verify this.
1334 */
1335 if (!(flags & LLE_IFADDR) &&
1336 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1337 return (NULL);
1338
1339 lle = in_lltable_new(sin->sin_addr, flags);
1340 if (lle == NULL) {
1341 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1342 return (NULL);
1343 }
1344 lle->la_flags = flags;
1345 if (flags & LLE_STATIC)
1346 lle->r_flags |= RLLE_VALID;
1347 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1348 linkhdrsize = LLE_MAX_LINKHDR;
1349 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1350 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1351 epoch_call(net_epoch_preempt, &lle->lle_epoch_ctx, in_lltable_destroy_lle_unlocked);
1352 return (NULL);
1353 }
1354 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1355 lladdr_off);
1356 lle->la_flags |= LLE_STATIC;
1357 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1358 }
1359
1360 return (lle);
1361 }
1362
1363 /*
1364 * Return NULL if not found or marked for deletion.
1365 * If found return lle read locked.
1366 */
1367 static struct llentry *
in_lltable_lookup(struct lltable * llt,u_int flags,const struct sockaddr * l3addr)1368 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1369 {
1370 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1371 struct llentry *lle;
1372
1373 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1374 KASSERT(l3addr->sa_family == AF_INET,
1375 ("sin_family %d", l3addr->sa_family));
1376 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1377 (LLE_UNLOCKED | LLE_EXCLUSIVE),
1378 ("wrong lle request flags: %#x", flags));
1379
1380 lle = in_lltable_find_dst(llt, sin->sin_addr);
1381 if (lle == NULL)
1382 return (NULL);
1383 if (flags & LLE_UNLOCKED)
1384 return (lle);
1385
1386 if (flags & LLE_EXCLUSIVE)
1387 LLE_WLOCK(lle);
1388 else
1389 LLE_RLOCK(lle);
1390
1391 /*
1392 * If the afdata lock is not held, the LLE may have been unlinked while
1393 * we were blocked on the LLE lock. Check for this case.
1394 */
1395 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1396 if (flags & LLE_EXCLUSIVE)
1397 LLE_WUNLOCK(lle);
1398 else
1399 LLE_RUNLOCK(lle);
1400 return (NULL);
1401 }
1402 return (lle);
1403 }
1404
1405 static int
in_lltable_dump_entry(struct lltable * llt,struct llentry * lle,struct sysctl_req * wr)1406 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1407 struct sysctl_req *wr)
1408 {
1409 struct ifnet *ifp = llt->llt_ifp;
1410 /* XXX stack use */
1411 struct {
1412 struct rt_msghdr rtm;
1413 struct sockaddr_in sin;
1414 struct sockaddr_dl sdl;
1415 } arpc;
1416 struct sockaddr_dl *sdl;
1417 int error;
1418
1419 bzero(&arpc, sizeof(arpc));
1420 /* skip deleted entries */
1421 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1422 return (0);
1423 /* Skip if jailed and not a valid IP of the prison. */
1424 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1425 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1426 return (0);
1427 /*
1428 * produce a msg made of:
1429 * struct rt_msghdr;
1430 * struct sockaddr_in; (IPv4)
1431 * struct sockaddr_dl;
1432 */
1433 arpc.rtm.rtm_msglen = sizeof(arpc);
1434 arpc.rtm.rtm_version = RTM_VERSION;
1435 arpc.rtm.rtm_type = RTM_GET;
1436 arpc.rtm.rtm_flags = RTF_UP;
1437 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1438
1439 /* publish */
1440 if (lle->la_flags & LLE_PUB)
1441 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1442
1443 sdl = &arpc.sdl;
1444 sdl->sdl_family = AF_LINK;
1445 sdl->sdl_len = sizeof(*sdl);
1446 sdl->sdl_index = ifp->if_index;
1447 sdl->sdl_type = ifp->if_type;
1448 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1449 sdl->sdl_alen = ifp->if_addrlen;
1450 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1451 } else {
1452 sdl->sdl_alen = 0;
1453 bzero(LLADDR(sdl), ifp->if_addrlen);
1454 }
1455
1456 arpc.rtm.rtm_rmx.rmx_expire =
1457 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1458 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1459 if (lle->la_flags & LLE_STATIC)
1460 arpc.rtm.rtm_flags |= RTF_STATIC;
1461 if (lle->la_flags & LLE_IFADDR)
1462 arpc.rtm.rtm_flags |= RTF_PINNED;
1463 arpc.rtm.rtm_index = ifp->if_index;
1464 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1465
1466 return (error);
1467 }
1468
1469 static struct lltable *
in_lltattach(struct ifnet * ifp)1470 in_lltattach(struct ifnet *ifp)
1471 {
1472 struct lltable *llt;
1473
1474 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1475 llt->llt_af = AF_INET;
1476 llt->llt_ifp = ifp;
1477
1478 llt->llt_lookup = in_lltable_lookup;
1479 llt->llt_alloc_entry = in_lltable_alloc;
1480 llt->llt_delete_entry = in_lltable_delete_entry;
1481 llt->llt_dump_entry = in_lltable_dump_entry;
1482 llt->llt_hash = in_lltable_hash;
1483 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1484 llt->llt_free_entry = in_lltable_free_entry;
1485 llt->llt_match_prefix = in_lltable_match_prefix;
1486 llt->llt_mark_used = in_lltable_mark_used;
1487 lltable_link(llt);
1488
1489 return (llt);
1490 }
1491
1492 void *
in_domifattach(struct ifnet * ifp)1493 in_domifattach(struct ifnet *ifp)
1494 {
1495 struct in_ifinfo *ii;
1496
1497 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1498
1499 ii->ii_llt = in_lltattach(ifp);
1500 ii->ii_igmp = igmp_domifattach(ifp);
1501
1502 return (ii);
1503 }
1504
1505 void
in_domifdetach(struct ifnet * ifp,void * aux)1506 in_domifdetach(struct ifnet *ifp, void *aux)
1507 {
1508 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1509
1510 igmp_domifdetach(ifp);
1511 lltable_free(ii->ii_llt);
1512 free(ii, M_IFADDR);
1513 }
1514