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 #define IN_HISTORICAL_NETS /* include class masks */
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/route/nhop.h>
64 #include <net/route/route_ctl.h>
65 #include <net/vnet.h>
66
67 #include <netinet/if_ether.h>
68 #include <netinet/in.h>
69 #include <netinet/in_fib.h>
70 #include <netinet/in_var.h>
71 #include <netinet/in_pcb.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/ip_carp.h>
74 #include <netinet/igmp_var.h>
75 #include <netinet/udp.h>
76 #include <netinet/udp_var.h>
77
78 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
79 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
80 static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
81
82 static void in_socktrim(struct sockaddr_in *);
83 static void in_purgemaddrs(struct ifnet *);
84
85 static bool ia_need_loopback_route(const struct in_ifaddr *);
86
87 VNET_DEFINE_STATIC(int, nosameprefix);
88 #define V_nosameprefix VNET(nosameprefix)
89 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
90 &VNET_NAME(nosameprefix), 0,
91 "Refuse to create same prefixes on different interfaces");
92
93 VNET_DEFINE_STATIC(bool, broadcast_lowest);
94 #define V_broadcast_lowest VNET(broadcast_lowest)
95 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW,
96 &VNET_NAME(broadcast_lowest), 0,
97 "Treat lowest address on a subnet (host 0) as broadcast");
98
99 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
100 #define V_ripcbinfo VNET(ripcbinfo)
101
102 static struct sx in_control_sx;
103 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
104
105 /*
106 * Return 1 if an internet address is for a ``local'' host
107 * (one to which we have a connection).
108 */
109 int
in_localaddr(struct in_addr in)110 in_localaddr(struct in_addr in)
111 {
112 struct rm_priotracker in_ifa_tracker;
113 u_long i = ntohl(in.s_addr);
114 struct in_ifaddr *ia;
115
116 IN_IFADDR_RLOCK(&in_ifa_tracker);
117 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
118 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
119 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
120 return (1);
121 }
122 }
123 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
124 return (0);
125 }
126
127 /*
128 * Return 1 if an internet address is for the local host and configured
129 * on one of its interfaces.
130 */
131 int
in_localip(struct in_addr in)132 in_localip(struct in_addr in)
133 {
134 struct rm_priotracker in_ifa_tracker;
135 struct in_ifaddr *ia;
136
137 IN_IFADDR_RLOCK(&in_ifa_tracker);
138 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
139 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
140 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
141 return (1);
142 }
143 }
144 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
145 return (0);
146 }
147
148 /*
149 * Return 1 if an internet address is configured on an interface.
150 */
151 int
in_ifhasaddr(struct ifnet * ifp,struct in_addr in)152 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
153 {
154 struct ifaddr *ifa;
155 struct in_ifaddr *ia;
156
157 NET_EPOCH_ASSERT();
158
159 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
160 if (ifa->ifa_addr->sa_family != AF_INET)
161 continue;
162 ia = (struct in_ifaddr *)ifa;
163 if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
164 return (1);
165 }
166
167 return (0);
168 }
169
170 /*
171 * Return a reference to the interface address which is different to
172 * the supplied one but with same IP address value.
173 */
174 static struct in_ifaddr *
in_localip_more(struct in_ifaddr * original_ia)175 in_localip_more(struct in_ifaddr *original_ia)
176 {
177 struct rm_priotracker in_ifa_tracker;
178 in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
179 uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
180 struct in_ifaddr *ia;
181
182 IN_IFADDR_RLOCK(&in_ifa_tracker);
183 LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
184 in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
185 uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
186 if (!V_rt_add_addr_allfibs && (original_fib != fib))
187 continue;
188 if ((original_ia != ia) && (original_addr == addr)) {
189 ifa_ref(&ia->ia_ifa);
190 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
191 return (ia);
192 }
193 }
194 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
195
196 return (NULL);
197 }
198
199 /*
200 * Tries to find first IPv4 address in the provided fib.
201 * Prefers non-loopback addresses and return loopback IFF
202 * @loopback_ok is set.
203 *
204 * Returns ifa or NULL.
205 */
206 struct in_ifaddr *
in_findlocal(uint32_t fibnum,bool loopback_ok)207 in_findlocal(uint32_t fibnum, bool loopback_ok)
208 {
209 struct rm_priotracker in_ifa_tracker;
210 struct in_ifaddr *ia = NULL, *ia_lo = NULL;
211
212 NET_EPOCH_ASSERT();
213
214 IN_IFADDR_RLOCK(&in_ifa_tracker);
215 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
216 uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib;
217 if (!V_rt_add_addr_allfibs && (fibnum != ia_fib))
218 continue;
219
220 if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr)))
221 break;
222 if (loopback_ok)
223 ia_lo = ia;
224 }
225 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
226
227 if (ia == NULL)
228 ia = ia_lo;
229
230 return (ia);
231 }
232
233 /*
234 * Determine whether an IP address is in a reserved set of addresses
235 * that may not be forwarded, or whether datagrams to that destination
236 * may be forwarded.
237 */
238 int
in_canforward(struct in_addr in)239 in_canforward(struct in_addr in)
240 {
241 u_long i = ntohl(in.s_addr);
242
243 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i) ||
244 IN_ZERONET(i) || IN_LOOPBACK(i))
245 return (0);
246 return (1);
247 }
248
249 /*
250 * Trim a mask in a sockaddr
251 */
252 static void
in_socktrim(struct sockaddr_in * ap)253 in_socktrim(struct sockaddr_in *ap)
254 {
255 char *cplim = (char *) &ap->sin_addr;
256 char *cp = (char *) (&ap->sin_addr + 1);
257
258 ap->sin_len = 0;
259 while (--cp >= cplim)
260 if (*cp) {
261 (ap)->sin_len = cp - (char *) (ap) + 1;
262 break;
263 }
264 }
265
266 /*
267 * Generic internet control operations (ioctl's).
268 */
269 int
in_control(struct socket * so,u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)270 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
271 struct thread *td)
272 {
273 struct ifreq *ifr = (struct ifreq *)data;
274 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
275 struct epoch_tracker et;
276 struct ifaddr *ifa;
277 struct in_ifaddr *ia;
278 int error;
279
280 if (ifp == NULL)
281 return (EADDRNOTAVAIL);
282
283 /*
284 * Filter out 4 ioctls we implement directly. Forward the rest
285 * to specific functions and ifp->if_ioctl().
286 */
287 switch (cmd) {
288 case SIOCGIFADDR:
289 case SIOCGIFBRDADDR:
290 case SIOCGIFDSTADDR:
291 case SIOCGIFNETMASK:
292 break;
293 case SIOCGIFALIAS:
294 sx_xlock(&in_control_sx);
295 error = in_gifaddr_ioctl(cmd, data, ifp, td);
296 sx_xunlock(&in_control_sx);
297 return (error);
298 case SIOCDIFADDR:
299 sx_xlock(&in_control_sx);
300 error = in_difaddr_ioctl(cmd, data, ifp, td);
301 sx_xunlock(&in_control_sx);
302 return (error);
303 case OSIOCAIFADDR: /* 9.x compat */
304 case SIOCAIFADDR:
305 sx_xlock(&in_control_sx);
306 error = in_aifaddr_ioctl(cmd, data, ifp, td);
307 sx_xunlock(&in_control_sx);
308 return (error);
309 case SIOCSIFADDR:
310 case SIOCSIFBRDADDR:
311 case SIOCSIFDSTADDR:
312 case SIOCSIFNETMASK:
313 /* We no longer support that old commands. */
314 return (EINVAL);
315 default:
316 if (ifp->if_ioctl == NULL)
317 return (EOPNOTSUPP);
318 return ((*ifp->if_ioctl)(ifp, cmd, data));
319 }
320
321 if (addr->sin_addr.s_addr != INADDR_ANY &&
322 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
323 return (EADDRNOTAVAIL);
324
325 /*
326 * Find address for this interface, if it exists. If an
327 * address was specified, find that one instead of the
328 * first one on the interface, if possible.
329 */
330 NET_EPOCH_ENTER(et);
331 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
332 if (ifa->ifa_addr->sa_family != AF_INET)
333 continue;
334 ia = (struct in_ifaddr *)ifa;
335 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
336 break;
337 }
338 if (ifa == NULL)
339 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
340 if (ifa->ifa_addr->sa_family == AF_INET) {
341 ia = (struct in_ifaddr *)ifa;
342 if (prison_check_ip4(td->td_ucred,
343 &ia->ia_addr.sin_addr) == 0)
344 break;
345 }
346
347 if (ifa == NULL) {
348 NET_EPOCH_EXIT(et);
349 return (EADDRNOTAVAIL);
350 }
351
352 error = 0;
353 switch (cmd) {
354 case SIOCGIFADDR:
355 *addr = ia->ia_addr;
356 break;
357
358 case SIOCGIFBRDADDR:
359 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
360 error = EINVAL;
361 break;
362 }
363 *addr = ia->ia_broadaddr;
364 break;
365
366 case SIOCGIFDSTADDR:
367 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
368 error = EINVAL;
369 break;
370 }
371 *addr = ia->ia_dstaddr;
372 break;
373
374 case SIOCGIFNETMASK:
375 *addr = ia->ia_sockmask;
376 break;
377 }
378
379 NET_EPOCH_EXIT(et);
380
381 return (error);
382 }
383
384 static int
in_aifaddr_ioctl(u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)385 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
386 {
387 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
388 const struct sockaddr_in *addr = &ifra->ifra_addr;
389 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
390 const struct sockaddr_in *mask = &ifra->ifra_mask;
391 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
392 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
393 struct epoch_tracker et;
394 struct ifaddr *ifa;
395 struct in_ifaddr *ia;
396 bool iaIsFirst;
397 int error = 0;
398
399 error = priv_check(td, PRIV_NET_ADDIFADDR);
400 if (error)
401 return (error);
402
403 /*
404 * ifra_addr must be present and be of INET family.
405 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
406 */
407 if (addr->sin_len != sizeof(struct sockaddr_in) ||
408 addr->sin_family != AF_INET)
409 return (EINVAL);
410 if (broadaddr->sin_len != 0 &&
411 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
412 broadaddr->sin_family != AF_INET))
413 return (EINVAL);
414 if (mask->sin_len != 0 &&
415 (mask->sin_len != sizeof(struct sockaddr_in) ||
416 mask->sin_family != AF_INET))
417 return (EINVAL);
418 if ((ifp->if_flags & IFF_POINTOPOINT) &&
419 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
420 dstaddr->sin_addr.s_addr == INADDR_ANY))
421 return (EDESTADDRREQ);
422 if (vhid != 0 && carp_attach_p == NULL)
423 return (EPROTONOSUPPORT);
424
425 /*
426 * See whether address already exist.
427 */
428 iaIsFirst = true;
429 ia = NULL;
430 NET_EPOCH_ENTER(et);
431 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
432 struct in_ifaddr *it;
433
434 if (ifa->ifa_addr->sa_family != AF_INET)
435 continue;
436
437 it = (struct in_ifaddr *)ifa;
438 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
439 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
440 ia = it;
441 else
442 iaIsFirst = false;
443 }
444 NET_EPOCH_EXIT(et);
445
446 if (ia != NULL)
447 (void )in_difaddr_ioctl(cmd, data, ifp, td);
448
449 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
450 ia = (struct in_ifaddr *)ifa;
451 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
452 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
453 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
454 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
455 CALLOUT_RETURNUNLOCKED);
456
457 ia->ia_ifp = ifp;
458 ia->ia_addr = *addr;
459 if (mask->sin_len != 0) {
460 ia->ia_sockmask = *mask;
461 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
462 } else {
463 in_addr_t i = ntohl(addr->sin_addr.s_addr);
464
465 /*
466 * If netmask isn't supplied, use historical default.
467 * This is deprecated for interfaces other than loopback
468 * or point-to-point; warn in other cases. In the future
469 * we should return an error rather than warning.
470 */
471 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0)
472 printf("%s: set address: WARNING: network mask "
473 "should be specified; using historical default\n",
474 ifp->if_xname);
475 if (IN_CLASSA(i))
476 ia->ia_subnetmask = IN_CLASSA_NET;
477 else if (IN_CLASSB(i))
478 ia->ia_subnetmask = IN_CLASSB_NET;
479 else
480 ia->ia_subnetmask = IN_CLASSC_NET;
481 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
482 }
483 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
484 in_socktrim(&ia->ia_sockmask);
485
486 if (ifp->if_flags & IFF_BROADCAST) {
487 if (broadaddr->sin_len != 0) {
488 ia->ia_broadaddr = *broadaddr;
489 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
490 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
491 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
492 ia->ia_broadaddr.sin_family = AF_INET;
493 } else {
494 ia->ia_broadaddr.sin_addr.s_addr =
495 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
496 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
497 ia->ia_broadaddr.sin_family = AF_INET;
498 }
499 }
500
501 if (ifp->if_flags & IFF_POINTOPOINT)
502 ia->ia_dstaddr = *dstaddr;
503
504 if (vhid != 0) {
505 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
506 if (error)
507 return (error);
508 }
509
510 /* if_addrhead is already referenced by ifa_alloc() */
511 IF_ADDR_WLOCK(ifp);
512 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
513 IF_ADDR_WUNLOCK(ifp);
514
515 ifa_ref(ifa); /* in_ifaddrhead */
516 IN_IFADDR_WLOCK();
517 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
518 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
519 IN_IFADDR_WUNLOCK();
520
521 /*
522 * Give the interface a chance to initialize
523 * if this is its first address,
524 * and to validate the address if necessary.
525 */
526 if (ifp->if_ioctl != NULL) {
527 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
528 if (error)
529 goto fail1;
530 }
531
532 /*
533 * Add route for the network.
534 */
535 if (vhid == 0) {
536 error = in_addprefix(ia);
537 if (error)
538 goto fail1;
539 }
540
541 /*
542 * Add a loopback route to self.
543 */
544 if (vhid == 0 && ia_need_loopback_route(ia)) {
545 struct in_ifaddr *eia;
546
547 eia = in_localip_more(ia);
548
549 if (eia == NULL) {
550 error = ifa_add_loopback_route((struct ifaddr *)ia,
551 (struct sockaddr *)&ia->ia_addr);
552 if (error)
553 goto fail2;
554 } else
555 ifa_free(&eia->ia_ifa);
556 }
557
558 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
559 struct in_addr allhosts_addr;
560 struct in_ifinfo *ii;
561
562 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
563 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
564
565 error = in_joingroup(ifp, &allhosts_addr, NULL,
566 &ii->ii_allhosts);
567 }
568
569 /*
570 * Note: we don't need extra reference for ifa, since we called
571 * with sx lock held, and ifaddr can not be deleted in concurrent
572 * thread.
573 */
574 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
575
576 return (error);
577
578 fail2:
579 if (vhid == 0)
580 (void )in_scrubprefix(ia, LLE_STATIC);
581
582 fail1:
583 if (ia->ia_ifa.ifa_carp)
584 (*carp_detach_p)(&ia->ia_ifa, false);
585
586 IF_ADDR_WLOCK(ifp);
587 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
588 IF_ADDR_WUNLOCK(ifp);
589 ifa_free(&ia->ia_ifa); /* if_addrhead */
590
591 IN_IFADDR_WLOCK();
592 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
593 LIST_REMOVE(ia, ia_hash);
594 IN_IFADDR_WUNLOCK();
595 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
596
597 return (error);
598 }
599
600 static int
in_difaddr_ioctl(u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)601 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
602 {
603 const struct ifreq *ifr = (struct ifreq *)data;
604 const struct sockaddr_in *addr = (const struct sockaddr_in *)
605 &ifr->ifr_addr;
606 struct ifaddr *ifa;
607 struct in_ifaddr *ia;
608 bool deleteAny, iaIsLast;
609 int error;
610
611 if (td != NULL) {
612 error = priv_check(td, PRIV_NET_DELIFADDR);
613 if (error)
614 return (error);
615 }
616
617 if (addr->sin_len != sizeof(struct sockaddr_in) ||
618 addr->sin_family != AF_INET)
619 deleteAny = true;
620 else
621 deleteAny = false;
622
623 iaIsLast = true;
624 ia = NULL;
625 IF_ADDR_WLOCK(ifp);
626 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
627 struct in_ifaddr *it;
628
629 if (ifa->ifa_addr->sa_family != AF_INET)
630 continue;
631
632 it = (struct in_ifaddr *)ifa;
633 if (deleteAny && ia == NULL && (td == NULL ||
634 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
635 ia = it;
636
637 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
638 (td == NULL || prison_check_ip4(td->td_ucred,
639 &addr->sin_addr) == 0))
640 ia = it;
641
642 if (it != ia)
643 iaIsLast = false;
644 }
645
646 if (ia == NULL) {
647 IF_ADDR_WUNLOCK(ifp);
648 return (EADDRNOTAVAIL);
649 }
650
651 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
652 IF_ADDR_WUNLOCK(ifp);
653 ifa_free(&ia->ia_ifa); /* if_addrhead */
654
655 IN_IFADDR_WLOCK();
656 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
657 LIST_REMOVE(ia, ia_hash);
658 IN_IFADDR_WUNLOCK();
659
660 /*
661 * in_scrubprefix() kills the interface route.
662 */
663 in_scrubprefix(ia, LLE_STATIC);
664
665 /*
666 * in_ifadown gets rid of all the rest of
667 * the routes. This is not quite the right
668 * thing to do, but at least if we are running
669 * a routing process they will come back.
670 */
671 in_ifadown(&ia->ia_ifa, 1);
672
673 if (ia->ia_ifa.ifa_carp)
674 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
675
676 /*
677 * If this is the last IPv4 address configured on this
678 * interface, leave the all-hosts group.
679 * No state-change report need be transmitted.
680 */
681 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
682 struct in_ifinfo *ii;
683
684 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
685 if (ii->ii_allhosts) {
686 (void)in_leavegroup(ii->ii_allhosts, NULL);
687 ii->ii_allhosts = NULL;
688 }
689 }
690
691 IF_ADDR_WLOCK(ifp);
692 if (callout_stop(&ia->ia_garp_timer) == 1) {
693 ifa_free(&ia->ia_ifa);
694 }
695 IF_ADDR_WUNLOCK(ifp);
696
697 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
698 IFADDR_EVENT_DEL);
699 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
700
701 return (0);
702 }
703
704 static int
in_gifaddr_ioctl(u_long cmd,caddr_t data,struct ifnet * ifp,struct thread * td)705 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
706 {
707 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
708 const struct sockaddr_in *addr = &ifra->ifra_addr;
709 struct epoch_tracker et;
710 struct ifaddr *ifa;
711 struct in_ifaddr *ia;
712
713 /*
714 * ifra_addr must be present and be of INET family.
715 */
716 if (addr->sin_len != sizeof(struct sockaddr_in) ||
717 addr->sin_family != AF_INET)
718 return (EINVAL);
719
720 /*
721 * See whether address exist.
722 */
723 ia = NULL;
724 NET_EPOCH_ENTER(et);
725 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
726 struct in_ifaddr *it;
727
728 if (ifa->ifa_addr->sa_family != AF_INET)
729 continue;
730
731 it = (struct in_ifaddr *)ifa;
732 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
733 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) {
734 ia = it;
735 break;
736 }
737 }
738 if (ia == NULL) {
739 NET_EPOCH_EXIT(et);
740 return (EADDRNOTAVAIL);
741 }
742
743 ifra->ifra_mask = ia->ia_sockmask;
744 if ((ifp->if_flags & IFF_POINTOPOINT) &&
745 ia->ia_dstaddr.sin_family == AF_INET)
746 ifra->ifra_dstaddr = ia->ia_dstaddr;
747 else if ((ifp->if_flags & IFF_BROADCAST) &&
748 ia->ia_broadaddr.sin_family == AF_INET)
749 ifra->ifra_broadaddr = ia->ia_broadaddr;
750 else
751 memset(&ifra->ifra_broadaddr, 0,
752 sizeof(ifra->ifra_broadaddr));
753
754 NET_EPOCH_EXIT(et);
755 return (0);
756 }
757
758 static int
in_match_ifaddr(const struct rtentry * rt,const struct nhop_object * nh,void * arg)759 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
760 {
761
762 if (nh->nh_ifa == (struct ifaddr *)arg)
763 return (1);
764
765 return (0);
766 }
767
768 static int
in_handle_prefix_route(uint32_t fibnum,int cmd,struct sockaddr_in * dst,struct sockaddr_in * netmask,struct ifaddr * ifa,struct ifnet * ifp)769 in_handle_prefix_route(uint32_t fibnum, int cmd,
770 struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
771 struct ifnet *ifp)
772 {
773
774 NET_EPOCH_ASSERT();
775
776 /* Prepare gateway */
777 struct sockaddr_dl_short sdl = {
778 .sdl_family = AF_LINK,
779 .sdl_len = sizeof(struct sockaddr_dl_short),
780 .sdl_type = ifa->ifa_ifp->if_type,
781 .sdl_index = ifa->ifa_ifp->if_index,
782 };
783
784 struct rt_addrinfo info = {
785 .rti_ifa = ifa,
786 .rti_ifp = ifp,
787 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
788 .rti_info = {
789 [RTAX_DST] = (struct sockaddr *)dst,
790 [RTAX_NETMASK] = (struct sockaddr *)netmask,
791 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
792 },
793 /* Ensure we delete the prefix IFF prefix ifa matches */
794 .rti_filter = in_match_ifaddr,
795 .rti_filterdata = ifa,
796 };
797
798 return (rib_handle_ifaddr_info(fibnum, cmd, &info));
799 }
800
801 /*
802 * Routing table interaction with interface addresses.
803 *
804 * In general, two types of routes needs to be installed:
805 * a) "interface" or "prefix" route, telling user that the addresses
806 * behind the ifa prefix are reached directly.
807 * b) "loopback" route installed for the ifa address, telling user that
808 * the address belongs to local system.
809 *
810 * Handling for (a) and (b) differs in multi-fib aspects, hence they
811 * are implemented in different functions below.
812 *
813 * The cases above may intersect - /32 interface aliases results in
814 * the same prefix produced by (a) and (b). This blurs the definition
815 * of the "loopback" route and complicate interactions. The interaction
816 * table is defined below. The case numbers are used in the multiple
817 * functions below to refer to the particular test case.
818 *
819 * There can be multiple options:
820 * 1) Adding address with prefix on non-p2p/non-loopback interface.
821 * Example: 192.0.2.1/24. Action:
822 * * add "prefix" route towards 192.0.2.0/24 via @ia interface,
823 * using @ia as an address source.
824 * * add "loopback" route towards 192.0.2.1 via V_loif, saving
825 * @ia ifp in the gateway and using @ia as an address source.
826 *
827 * 2) Adding address with /32 mask to non-p2p/non-loopback interface.
828 * Example: 192.0.2.2/32. Action:
829 * * add "prefix" host route via V_loif, using @ia as an address source.
830 *
831 * 3) Adding address with or without prefix to p2p interface.
832 * Example: 10.0.0.1/24->10.0.0.2. Action:
833 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
834 * as an address source. Note: no sense in installing full /24 as the interface
835 * is point-to-point.
836 * * add "loopback" route towards 10.0.9.1 via V_loif, saving
837 * @ia ifp in the gateway and using @ia as an address source.
838 *
839 * 4) Adding address with or without prefix to loopback interface.
840 * Example: 192.0.2.1/24. Action:
841 * * add "prefix" host route via @ia interface, using @ia as an address source.
842 * Note: Skip installing /24 prefix as it would introduce TTL loop
843 * for the traffic destined to these addresses.
844 */
845
846 /*
847 * Checks if @ia needs to install loopback route to @ia address via
848 * ifa_maintain_loopback_route().
849 *
850 * Return true on success.
851 */
852 static bool
ia_need_loopback_route(const struct in_ifaddr * ia)853 ia_need_loopback_route(const struct in_ifaddr *ia)
854 {
855 struct ifnet *ifp = ia->ia_ifp;
856
857 /* Case 4: Skip loopback interfaces */
858 if ((ifp->if_flags & IFF_LOOPBACK) ||
859 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
860 return (false);
861
862 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */
863 if ((ifp->if_flags & IFF_POINTOPOINT) &&
864 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
865 return (false);
866
867 /* Case 2: skip /32 prefixes */
868 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
869 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
870 return (false);
871
872 return (true);
873 }
874
875 /*
876 * Calculate "prefix" route corresponding to @ia.
877 */
878 static void
ia_getrtprefix(const struct in_ifaddr * ia,struct in_addr * prefix,struct in_addr * mask)879 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
880 {
881
882 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
883 /* Case 3: return host route for dstaddr */
884 *prefix = ia->ia_dstaddr.sin_addr;
885 mask->s_addr = INADDR_BROADCAST;
886 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
887 /* Case 4: return host route for ifaddr */
888 *prefix = ia->ia_addr.sin_addr;
889 mask->s_addr = INADDR_BROADCAST;
890 } else {
891 /* Cases 1,2: return actual ia prefix */
892 *prefix = ia->ia_addr.sin_addr;
893 *mask = ia->ia_sockmask.sin_addr;
894 prefix->s_addr &= mask->s_addr;
895 }
896 }
897
898 /*
899 * Adds or delete interface "prefix" route corresponding to @ifa.
900 * Returns 0 on success or errno.
901 */
902 int
in_handle_ifaddr_route(int cmd,struct in_ifaddr * ia)903 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
904 {
905 struct ifaddr *ifa = &ia->ia_ifa;
906 struct in_addr daddr, maddr;
907 struct sockaddr_in *pmask;
908 struct epoch_tracker et;
909 int error;
910
911 ia_getrtprefix(ia, &daddr, &maddr);
912
913 struct sockaddr_in mask = {
914 .sin_family = AF_INET,
915 .sin_len = sizeof(struct sockaddr_in),
916 .sin_addr = maddr,
917 };
918
919 pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL;
920
921 struct sockaddr_in dst = {
922 .sin_family = AF_INET,
923 .sin_len = sizeof(struct sockaddr_in),
924 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
925 };
926
927 struct ifnet *ifp = ia->ia_ifp;
928
929 if ((maddr.s_addr == INADDR_BROADCAST) &&
930 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
931 /* Case 2: host route on broadcast interface */
932 ifp = V_loif;
933 }
934
935 uint32_t fibnum = ifa->ifa_ifp->if_fib;
936 NET_EPOCH_ENTER(et);
937 error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp);
938 NET_EPOCH_EXIT(et);
939
940 return (error);
941 }
942
943 /*
944 * Check if we have a route for the given prefix already.
945 */
946 static bool
in_hasrtprefix(struct in_ifaddr * target)947 in_hasrtprefix(struct in_ifaddr *target)
948 {
949 struct rm_priotracker in_ifa_tracker;
950 struct in_ifaddr *ia;
951 struct in_addr prefix, mask, p, m;
952 bool result = false;
953
954 ia_getrtprefix(target, &prefix, &mask);
955
956 IN_IFADDR_RLOCK(&in_ifa_tracker);
957 /* Look for an existing address with the same prefix, mask, and fib */
958 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
959 ia_getrtprefix(ia, &p, &m);
960
961 if (prefix.s_addr != p.s_addr ||
962 mask.s_addr != m.s_addr)
963 continue;
964
965 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
966 continue;
967
968 /*
969 * If we got a matching prefix route inserted by other
970 * interface address, we are done here.
971 */
972 if (ia->ia_flags & IFA_ROUTE) {
973 result = true;
974 break;
975 }
976 }
977 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
978
979 return (result);
980 }
981
982 int
in_addprefix(struct in_ifaddr * target)983 in_addprefix(struct in_ifaddr *target)
984 {
985 int error;
986
987 if (in_hasrtprefix(target)) {
988 if (V_nosameprefix)
989 return (EEXIST);
990 else {
991 rt_addrmsg(RTM_ADD, &target->ia_ifa,
992 target->ia_ifp->if_fib);
993 return (0);
994 }
995 }
996
997 /*
998 * No-one seem to have this prefix route, so we try to insert it.
999 */
1000 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
1001 error = in_handle_ifaddr_route(RTM_ADD, target);
1002 if (!error)
1003 target->ia_flags |= IFA_ROUTE;
1004 return (error);
1005 }
1006
1007 /*
1008 * Removes either all lle entries for given @ia, or lle
1009 * corresponding to @ia address.
1010 */
1011 static void
in_scrubprefixlle(struct in_ifaddr * ia,int all,u_int flags)1012 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
1013 {
1014 struct sockaddr_in addr, mask;
1015 struct sockaddr *saddr, *smask;
1016 struct ifnet *ifp;
1017
1018 saddr = (struct sockaddr *)&addr;
1019 bzero(&addr, sizeof(addr));
1020 addr.sin_len = sizeof(addr);
1021 addr.sin_family = AF_INET;
1022 smask = (struct sockaddr *)&mask;
1023 bzero(&mask, sizeof(mask));
1024 mask.sin_len = sizeof(mask);
1025 mask.sin_family = AF_INET;
1026 mask.sin_addr.s_addr = ia->ia_subnetmask;
1027 ifp = ia->ia_ifp;
1028
1029 if (all) {
1030 /*
1031 * Remove all L2 entries matching given prefix.
1032 * Convert address to host representation to avoid
1033 * doing this on every callback. ia_subnetmask is already
1034 * stored in host representation.
1035 */
1036 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
1037 lltable_prefix_free(AF_INET, saddr, smask, flags);
1038 } else {
1039 /* Remove interface address only */
1040 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
1041 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
1042 }
1043 }
1044
1045 /*
1046 * If there is no other address in the system that can serve a route to the
1047 * same prefix, remove the route. Hand over the route to the new address
1048 * otherwise.
1049 */
1050 int
in_scrubprefix(struct in_ifaddr * target,u_int flags)1051 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1052 {
1053 struct rm_priotracker in_ifa_tracker;
1054 struct in_ifaddr *ia;
1055 struct in_addr prefix, mask, p, m;
1056 int error = 0;
1057
1058 /*
1059 * Remove the loopback route to the interface address.
1060 */
1061 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1062 struct in_ifaddr *eia;
1063
1064 eia = in_localip_more(target);
1065
1066 if (eia != NULL) {
1067 error = ifa_switch_loopback_route((struct ifaddr *)eia,
1068 (struct sockaddr *)&target->ia_addr);
1069 ifa_free(&eia->ia_ifa);
1070 } else {
1071 error = ifa_del_loopback_route((struct ifaddr *)target,
1072 (struct sockaddr *)&target->ia_addr);
1073 }
1074 }
1075
1076 ia_getrtprefix(target, &prefix, &mask);
1077
1078 if ((target->ia_flags & IFA_ROUTE) == 0) {
1079 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1080
1081 /*
1082 * Removing address from !IFF_UP interface or
1083 * prefix which exists on other interface (along with route).
1084 * No entries should exist here except target addr.
1085 * Given that, delete this entry only.
1086 */
1087 in_scrubprefixlle(target, 0, flags);
1088 return (0);
1089 }
1090
1091 IN_IFADDR_RLOCK(&in_ifa_tracker);
1092 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1093 ia_getrtprefix(ia, &p, &m);
1094
1095 if (prefix.s_addr != p.s_addr ||
1096 mask.s_addr != m.s_addr)
1097 continue;
1098
1099 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1100 continue;
1101
1102 /*
1103 * If we got a matching prefix address, move IFA_ROUTE and
1104 * the route itself to it. Make sure that routing daemons
1105 * get a heads-up.
1106 */
1107 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1108 ifa_ref(&ia->ia_ifa);
1109 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1110 error = in_handle_ifaddr_route(RTM_DELETE, target);
1111 if (error == 0)
1112 target->ia_flags &= ~IFA_ROUTE;
1113 else
1114 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1115 error);
1116 /* Scrub all entries IFF interface is different */
1117 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
1118 flags);
1119 error = in_handle_ifaddr_route(RTM_ADD, ia);
1120 if (error == 0)
1121 ia->ia_flags |= IFA_ROUTE;
1122 else
1123 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1124 error);
1125 ifa_free(&ia->ia_ifa);
1126 return (error);
1127 }
1128 }
1129 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1130
1131 /*
1132 * remove all L2 entries on the given prefix
1133 */
1134 in_scrubprefixlle(target, 1, flags);
1135
1136 /*
1137 * As no-one seem to have this prefix, we can remove the route.
1138 */
1139 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1140 error = in_handle_ifaddr_route(RTM_DELETE, target);
1141 if (error == 0)
1142 target->ia_flags &= ~IFA_ROUTE;
1143 else
1144 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1145 return (error);
1146 }
1147
1148 void
in_ifscrub_all(void)1149 in_ifscrub_all(void)
1150 {
1151 struct ifnet *ifp;
1152 struct ifaddr *ifa, *nifa;
1153 struct ifaliasreq ifr;
1154
1155 IFNET_RLOCK();
1156 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1157 /* Cannot lock here - lock recursion. */
1158 /* NET_EPOCH_ENTER(et); */
1159 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1160 if (ifa->ifa_addr->sa_family != AF_INET)
1161 continue;
1162
1163 /*
1164 * This is ugly but the only way for legacy IP to
1165 * cleanly remove addresses and everything attached.
1166 */
1167 bzero(&ifr, sizeof(ifr));
1168 ifr.ifra_addr = *ifa->ifa_addr;
1169 if (ifa->ifa_dstaddr)
1170 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1171 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
1172 ifp, NULL);
1173 }
1174 /* NET_EPOCH_EXIT(et); */
1175 in_purgemaddrs(ifp);
1176 igmp_domifdetach(ifp);
1177 }
1178 IFNET_RUNLOCK();
1179 }
1180
1181 int
in_ifaddr_broadcast(struct in_addr in,struct in_ifaddr * ia)1182 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
1183 {
1184
1185 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1186 /*
1187 * Optionally check for old-style (host 0) broadcast, but
1188 * taking into account that RFC 3021 obsoletes it.
1189 */
1190 (V_broadcast_lowest && ia->ia_subnetmask != IN_RFC3021_MASK &&
1191 ntohl(in.s_addr) == ia->ia_subnet)) &&
1192 /*
1193 * Check for an all one subnetmask. These
1194 * only exist when an interface gets a secondary
1195 * address.
1196 */
1197 ia->ia_subnetmask != (u_long)0xffffffff);
1198 }
1199
1200 /*
1201 * Return 1 if the address might be a local broadcast address.
1202 */
1203 int
in_broadcast(struct in_addr in,struct ifnet * ifp)1204 in_broadcast(struct in_addr in, struct ifnet *ifp)
1205 {
1206 struct ifaddr *ifa;
1207 int found;
1208
1209 NET_EPOCH_ASSERT();
1210
1211 if (in.s_addr == INADDR_BROADCAST ||
1212 in.s_addr == INADDR_ANY)
1213 return (1);
1214 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1215 return (0);
1216 found = 0;
1217 /*
1218 * Look through the list of addresses for a match
1219 * with a broadcast address.
1220 */
1221 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1222 if (ifa->ifa_addr->sa_family == AF_INET &&
1223 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
1224 found = 1;
1225 break;
1226 }
1227 return (found);
1228 }
1229
1230 /*
1231 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1232 */
1233 void
in_ifdetach(struct ifnet * ifp)1234 in_ifdetach(struct ifnet *ifp)
1235 {
1236 IN_MULTI_LOCK();
1237 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1238 in_pcbpurgeif0(&V_udbinfo, ifp);
1239 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1240 in_purgemaddrs(ifp);
1241 IN_MULTI_UNLOCK();
1242
1243 /*
1244 * Make sure all multicast deletions invoking if_ioctl() are
1245 * completed before returning. Else we risk accessing a freed
1246 * ifnet structure pointer.
1247 */
1248 inm_release_wait(NULL);
1249 }
1250
1251 /*
1252 * Delete all IPv4 multicast address records, and associated link-layer
1253 * multicast address records, associated with ifp.
1254 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1255 * XXX This should not race with ifma_protospec being set during
1256 * a new allocation, if it does, we have bigger problems.
1257 */
1258 static void
in_purgemaddrs(struct ifnet * ifp)1259 in_purgemaddrs(struct ifnet *ifp)
1260 {
1261 struct in_multi_head purgeinms;
1262 struct in_multi *inm;
1263 struct ifmultiaddr *ifma, *next;
1264
1265 SLIST_INIT(&purgeinms);
1266 IN_MULTI_LIST_LOCK();
1267
1268 /*
1269 * Extract list of in_multi associated with the detaching ifp
1270 * which the PF_INET layer is about to release.
1271 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1272 * by code further down.
1273 */
1274 IF_ADDR_WLOCK(ifp);
1275 restart:
1276 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1277 if (ifma->ifma_addr->sa_family != AF_INET ||
1278 ifma->ifma_protospec == NULL)
1279 continue;
1280 inm = (struct in_multi *)ifma->ifma_protospec;
1281 inm_rele_locked(&purgeinms, inm);
1282 if (__predict_false(ifma_restart)) {
1283 ifma_restart = true;
1284 goto restart;
1285 }
1286 }
1287 IF_ADDR_WUNLOCK(ifp);
1288
1289 inm_release_list_deferred(&purgeinms);
1290 igmp_ifdetach(ifp);
1291 IN_MULTI_LIST_UNLOCK();
1292 }
1293
1294 struct in_llentry {
1295 struct llentry base;
1296 };
1297
1298 #define IN_LLTBL_DEFAULT_HSIZE 32
1299 #define IN_LLTBL_HASH(k, h) \
1300 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1301
1302 /*
1303 * Do actual deallocation of @lle.
1304 */
1305 static void
in_lltable_destroy_lle_unlocked(epoch_context_t ctx)1306 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1307 {
1308 struct llentry *lle;
1309
1310 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1311 LLE_LOCK_DESTROY(lle);
1312 LLE_REQ_DESTROY(lle);
1313 free(lle, M_LLTABLE);
1314 }
1315
1316 /*
1317 * Called by LLE_FREE_LOCKED when number of references
1318 * drops to zero.
1319 */
1320 static void
in_lltable_destroy_lle(struct llentry * lle)1321 in_lltable_destroy_lle(struct llentry *lle)
1322 {
1323
1324 LLE_WUNLOCK(lle);
1325 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1326 }
1327
1328 static struct llentry *
in_lltable_new(struct in_addr addr4,u_int flags)1329 in_lltable_new(struct in_addr addr4, u_int flags)
1330 {
1331 struct in_llentry *lle;
1332
1333 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1334 if (lle == NULL) /* NB: caller generates msg */
1335 return NULL;
1336
1337 /*
1338 * For IPv4 this will trigger "arpresolve" to generate
1339 * an ARP request.
1340 */
1341 lle->base.la_expire = time_uptime; /* mark expired */
1342 lle->base.r_l3addr.addr4 = addr4;
1343 lle->base.lle_refcnt = 1;
1344 lle->base.lle_free = in_lltable_destroy_lle;
1345 LLE_LOCK_INIT(&lle->base);
1346 LLE_REQ_INIT(&lle->base);
1347 callout_init(&lle->base.lle_timer, 1);
1348
1349 return (&lle->base);
1350 }
1351
1352 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1353 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1354
1355 static int
in_lltable_match_prefix(const struct sockaddr * saddr,const struct sockaddr * smask,u_int flags,struct llentry * lle)1356 in_lltable_match_prefix(const struct sockaddr *saddr,
1357 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1358 {
1359 struct in_addr addr, mask, lle_addr;
1360
1361 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1362 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1363 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1364
1365 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1366 return (0);
1367
1368 if (lle->la_flags & LLE_IFADDR) {
1369 /*
1370 * Delete LLE_IFADDR records IFF address & flag matches.
1371 * Note that addr is the interface address within prefix
1372 * being matched.
1373 * Note also we should handle 'ifdown' cases without removing
1374 * ifaddr macs.
1375 */
1376 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1377 return (1);
1378 return (0);
1379 }
1380
1381 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1382 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1383 return (1);
1384
1385 return (0);
1386 }
1387
1388 static void
in_lltable_free_entry(struct lltable * llt,struct llentry * lle)1389 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1390 {
1391 size_t pkts_dropped;
1392
1393 LLE_WLOCK_ASSERT(lle);
1394 KASSERT(llt != NULL, ("lltable is NULL"));
1395
1396 /* Unlink entry from table if not already */
1397 if ((lle->la_flags & LLE_LINKED) != 0) {
1398 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1399 lltable_unlink_entry(llt, lle);
1400 }
1401
1402 /* Drop hold queue */
1403 pkts_dropped = llentry_free(lle);
1404 ARPSTAT_ADD(dropped, pkts_dropped);
1405 }
1406
1407 static int
in_lltable_rtcheck(struct ifnet * ifp,u_int flags,const struct sockaddr * l3addr)1408 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1409 {
1410 struct nhop_object *nh;
1411 struct in_addr addr;
1412
1413 KASSERT(l3addr->sa_family == AF_INET,
1414 ("sin_family %d", l3addr->sa_family));
1415
1416 addr = ((const struct sockaddr_in *)l3addr)->sin_addr;
1417
1418 nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0);
1419 if (nh == NULL)
1420 return (EINVAL);
1421
1422 /*
1423 * If the gateway for an existing host route matches the target L3
1424 * address, which is a special route inserted by some implementation
1425 * such as MANET, and the interface is of the correct type, then
1426 * allow for ARP to proceed.
1427 */
1428 if (nh->nh_flags & NHF_GATEWAY) {
1429 if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER ||
1430 (nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1431 memcmp(nh->gw_sa.sa_data, l3addr->sa_data,
1432 sizeof(in_addr_t)) != 0) {
1433 return (EINVAL);
1434 }
1435 }
1436
1437 /*
1438 * Make sure that at least the destination address is covered
1439 * by the route. This is for handling the case where 2 or more
1440 * interfaces have the same prefix. An incoming packet arrives
1441 * on one interface and the corresponding outgoing packet leaves
1442 * another interface.
1443 */
1444 if ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) {
1445 struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp);
1446 struct in_addr dst_addr, mask_addr;
1447
1448 if (ia == NULL)
1449 return (EINVAL);
1450
1451 /*
1452 * ifaof_ifpforaddr() returns _best matching_ IFA.
1453 * It is possible that ifa prefix does not cover our address.
1454 * Explicitly verify and fail if that's the case.
1455 */
1456 dst_addr = IA_SIN(ia)->sin_addr;
1457 mask_addr.s_addr = htonl(ia->ia_subnetmask);
1458
1459 if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr))
1460 return (EINVAL);
1461 }
1462
1463 return (0);
1464 }
1465
1466 static inline uint32_t
in_lltable_hash_dst(const struct in_addr dst,uint32_t hsize)1467 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1468 {
1469
1470 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1471 }
1472
1473 static uint32_t
in_lltable_hash(const struct llentry * lle,uint32_t hsize)1474 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1475 {
1476
1477 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1478 }
1479
1480 static void
in_lltable_fill_sa_entry(const struct llentry * lle,struct sockaddr * sa)1481 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1482 {
1483 struct sockaddr_in *sin;
1484
1485 sin = (struct sockaddr_in *)sa;
1486 bzero(sin, sizeof(*sin));
1487 sin->sin_family = AF_INET;
1488 sin->sin_len = sizeof(*sin);
1489 sin->sin_addr = lle->r_l3addr.addr4;
1490 }
1491
1492 static inline struct llentry *
in_lltable_find_dst(struct lltable * llt,struct in_addr dst)1493 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1494 {
1495 struct llentry *lle;
1496 struct llentries *lleh;
1497 u_int hashidx;
1498
1499 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1500 lleh = &llt->lle_head[hashidx];
1501 CK_LIST_FOREACH(lle, lleh, lle_next) {
1502 if (lle->la_flags & LLE_DELETED)
1503 continue;
1504 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1505 break;
1506 }
1507
1508 return (lle);
1509 }
1510
1511 static void
in_lltable_delete_entry(struct lltable * llt,struct llentry * lle)1512 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1513 {
1514
1515 lle->la_flags |= LLE_DELETED;
1516 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1517 #ifdef DIAGNOSTIC
1518 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1519 #endif
1520 llentry_free(lle);
1521 }
1522
1523 static struct llentry *
in_lltable_alloc(struct lltable * llt,u_int flags,const struct sockaddr * l3addr)1524 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1525 {
1526 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1527 struct ifnet *ifp = llt->llt_ifp;
1528 struct llentry *lle;
1529 char linkhdr[LLE_MAX_LINKHDR];
1530 size_t linkhdrsize;
1531 int lladdr_off;
1532
1533 KASSERT(l3addr->sa_family == AF_INET,
1534 ("sin_family %d", l3addr->sa_family));
1535
1536 /*
1537 * A route that covers the given address must have
1538 * been installed 1st because we are doing a resolution,
1539 * verify this.
1540 */
1541 if (!(flags & LLE_IFADDR) &&
1542 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1543 return (NULL);
1544
1545 lle = in_lltable_new(sin->sin_addr, flags);
1546 if (lle == NULL) {
1547 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1548 return (NULL);
1549 }
1550 lle->la_flags = flags;
1551 if (flags & LLE_STATIC)
1552 lle->r_flags |= RLLE_VALID;
1553 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1554 linkhdrsize = LLE_MAX_LINKHDR;
1555 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1556 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1557 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1558 return (NULL);
1559 }
1560 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1561 lladdr_off);
1562 lle->la_flags |= LLE_STATIC;
1563 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1564 }
1565
1566 return (lle);
1567 }
1568
1569 /*
1570 * Return NULL if not found or marked for deletion.
1571 * If found return lle read locked.
1572 */
1573 static struct llentry *
in_lltable_lookup(struct lltable * llt,u_int flags,const struct sockaddr * l3addr)1574 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1575 {
1576 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1577 struct llentry *lle;
1578
1579 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1580 KASSERT(l3addr->sa_family == AF_INET,
1581 ("sin_family %d", l3addr->sa_family));
1582 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1583 (LLE_UNLOCKED | LLE_EXCLUSIVE),
1584 ("wrong lle request flags: %#x", flags));
1585
1586 lle = in_lltable_find_dst(llt, sin->sin_addr);
1587 if (lle == NULL)
1588 return (NULL);
1589 if (flags & LLE_UNLOCKED)
1590 return (lle);
1591
1592 if (flags & LLE_EXCLUSIVE)
1593 LLE_WLOCK(lle);
1594 else
1595 LLE_RLOCK(lle);
1596
1597 /*
1598 * If the afdata lock is not held, the LLE may have been unlinked while
1599 * we were blocked on the LLE lock. Check for this case.
1600 */
1601 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1602 if (flags & LLE_EXCLUSIVE)
1603 LLE_WUNLOCK(lle);
1604 else
1605 LLE_RUNLOCK(lle);
1606 return (NULL);
1607 }
1608 return (lle);
1609 }
1610
1611 static int
in_lltable_dump_entry(struct lltable * llt,struct llentry * lle,struct sysctl_req * wr)1612 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1613 struct sysctl_req *wr)
1614 {
1615 struct ifnet *ifp = llt->llt_ifp;
1616 /* XXX stack use */
1617 struct {
1618 struct rt_msghdr rtm;
1619 struct sockaddr_in sin;
1620 struct sockaddr_dl sdl;
1621 } arpc;
1622 struct sockaddr_dl *sdl;
1623 int error;
1624
1625 bzero(&arpc, sizeof(arpc));
1626 /* skip deleted entries */
1627 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1628 return (0);
1629 /* Skip if jailed and not a valid IP of the prison. */
1630 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1631 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1632 return (0);
1633 /*
1634 * produce a msg made of:
1635 * struct rt_msghdr;
1636 * struct sockaddr_in; (IPv4)
1637 * struct sockaddr_dl;
1638 */
1639 arpc.rtm.rtm_msglen = sizeof(arpc);
1640 arpc.rtm.rtm_version = RTM_VERSION;
1641 arpc.rtm.rtm_type = RTM_GET;
1642 arpc.rtm.rtm_flags = RTF_UP;
1643 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1644
1645 /* publish */
1646 if (lle->la_flags & LLE_PUB)
1647 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1648
1649 sdl = &arpc.sdl;
1650 sdl->sdl_family = AF_LINK;
1651 sdl->sdl_len = sizeof(*sdl);
1652 sdl->sdl_index = ifp->if_index;
1653 sdl->sdl_type = ifp->if_type;
1654 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1655 sdl->sdl_alen = ifp->if_addrlen;
1656 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1657 } else {
1658 sdl->sdl_alen = 0;
1659 bzero(LLADDR(sdl), ifp->if_addrlen);
1660 }
1661
1662 arpc.rtm.rtm_rmx.rmx_expire =
1663 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1664 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1665 if (lle->la_flags & LLE_STATIC)
1666 arpc.rtm.rtm_flags |= RTF_STATIC;
1667 if (lle->la_flags & LLE_IFADDR)
1668 arpc.rtm.rtm_flags |= RTF_PINNED;
1669 arpc.rtm.rtm_index = ifp->if_index;
1670 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1671
1672 return (error);
1673 }
1674
1675 static struct lltable *
in_lltattach(struct ifnet * ifp)1676 in_lltattach(struct ifnet *ifp)
1677 {
1678 struct lltable *llt;
1679
1680 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1681 llt->llt_af = AF_INET;
1682 llt->llt_ifp = ifp;
1683
1684 llt->llt_lookup = in_lltable_lookup;
1685 llt->llt_alloc_entry = in_lltable_alloc;
1686 llt->llt_delete_entry = in_lltable_delete_entry;
1687 llt->llt_dump_entry = in_lltable_dump_entry;
1688 llt->llt_hash = in_lltable_hash;
1689 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1690 llt->llt_free_entry = in_lltable_free_entry;
1691 llt->llt_match_prefix = in_lltable_match_prefix;
1692 llt->llt_mark_used = llentry_mark_used;
1693 lltable_link(llt);
1694
1695 return (llt);
1696 }
1697
1698 struct lltable *
in_lltable_get(struct ifnet * ifp)1699 in_lltable_get(struct ifnet *ifp)
1700 {
1701 struct lltable *llt = NULL;
1702
1703 void *afdata_ptr = ifp->if_afdata[AF_INET];
1704 if (afdata_ptr != NULL)
1705 llt = ((struct in_ifinfo *)afdata_ptr)->ii_llt;
1706 return (llt);
1707 }
1708
1709 void *
in_domifattach(struct ifnet * ifp)1710 in_domifattach(struct ifnet *ifp)
1711 {
1712 struct in_ifinfo *ii;
1713
1714 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1715
1716 ii->ii_llt = in_lltattach(ifp);
1717 ii->ii_igmp = igmp_domifattach(ifp);
1718
1719 return (ii);
1720 }
1721
1722 void
in_domifdetach(struct ifnet * ifp,void * aux)1723 in_domifdetach(struct ifnet *ifp, void *aux)
1724 {
1725 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1726
1727 igmp_domifdetach(ifp);
1728 lltable_free(ii->ii_llt);
1729 free(ii, M_IFADDR);
1730 }
1731