1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2019 Roy Marples <roy@marples.name> 5 * All rights reserved 6 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <arpa/inet.h> 30 31 #include <assert.h> 32 #include <errno.h> 33 #include <stdbool.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <unistd.h> 38 39 #define ELOOP_QUEUE 6 40 #include "config.h" 41 #include "arp.h" 42 #include "common.h" 43 #include "dhcp.h" 44 #include "eloop.h" 45 #include "if.h" 46 #include "if-options.h" 47 #include "ipv4.h" 48 #include "ipv4ll.h" 49 #include "logerr.h" 50 #include "sa.h" 51 #include "script.h" 52 53 static const struct in_addr inaddr_llmask = { 54 .s_addr = HTONL(LINKLOCAL_MASK) 55 }; 56 static const struct in_addr inaddr_llbcast = { 57 .s_addr = HTONL(LINKLOCAL_BCAST) 58 }; 59 60 static void ipv4ll_start1(struct interface *, struct arp_state *); 61 62 static in_addr_t 63 ipv4ll_pickaddr(struct interface *ifp) 64 { 65 struct in_addr addr; 66 struct ipv4ll_state *state; 67 68 state = IPV4LL_STATE(ifp); 69 setstate(state->randomstate); 70 71 do { 72 long r; 73 74 again: 75 /* RFC 3927 Section 2.1 states that the first 256 and 76 * last 256 addresses are reserved for future use. 77 * See ipv4ll_start for why we don't use arc4random. */ 78 /* coverity[dont_call] */ 79 r = random(); 80 addr.s_addr = ntohl(LINKLOCAL_ADDR | 81 ((uint32_t)(r % 0xFD00) + 0x0100)); 82 83 /* No point using a failed address */ 84 if (IN_ARE_ADDR_EQUAL(&addr, &state->pickedaddr)) 85 goto again; 86 /* Ensure we don't have the address on another interface */ 87 } while (ipv4_findaddr(ifp->ctx, &addr) != NULL); 88 89 /* Restore the original random state */ 90 setstate(ifp->ctx->randomstate); 91 return addr.s_addr; 92 } 93 94 int 95 ipv4ll_subnetroute(rb_tree_t *routes, struct interface *ifp) 96 { 97 struct ipv4ll_state *state; 98 struct rt *rt; 99 struct in_addr in; 100 101 assert(ifp != NULL); 102 if ((state = IPV4LL_STATE(ifp)) == NULL || 103 state->addr == NULL) 104 return 0; 105 106 if ((rt = rt_new(ifp)) == NULL) 107 return -1; 108 109 in.s_addr = state->addr->addr.s_addr & state->addr->mask.s_addr; 110 sa_in_init(&rt->rt_dest, &in); 111 in.s_addr = state->addr->mask.s_addr; 112 sa_in_init(&rt->rt_netmask, &in); 113 in.s_addr = INADDR_ANY; 114 sa_in_init(&rt->rt_gateway, &in); 115 sa_in_init(&rt->rt_ifa, &state->addr->addr); 116 return rt_proto_add(routes, rt) ? 1 : 0; 117 } 118 119 int 120 ipv4ll_defaultroute(rb_tree_t *routes, struct interface *ifp) 121 { 122 struct ipv4ll_state *state; 123 struct rt *rt; 124 struct in_addr in; 125 126 assert(ifp != NULL); 127 if ((state = IPV4LL_STATE(ifp)) == NULL || 128 state->addr == NULL) 129 return 0; 130 131 if ((rt = rt_new(ifp)) == NULL) 132 return -1; 133 134 in.s_addr = INADDR_ANY; 135 sa_in_init(&rt->rt_dest, &in); 136 sa_in_init(&rt->rt_netmask, &in); 137 sa_in_init(&rt->rt_gateway, &in); 138 sa_in_init(&rt->rt_ifa, &state->addr->addr); 139 return rt_proto_add(routes, rt) ? 1 : 0; 140 } 141 142 ssize_t 143 ipv4ll_env(FILE *fp, const char *prefix, const struct interface *ifp) 144 { 145 const struct ipv4ll_state *state; 146 const char *pf = prefix == NULL ? "" : "_"; 147 struct in_addr netnum; 148 149 assert(ifp != NULL); 150 if ((state = IPV4LL_CSTATE(ifp)) == NULL || state->addr == NULL) 151 return 0; 152 153 /* Emulate a DHCP environment */ 154 if (efprintf(fp, "%s%sip_address=%s", 155 prefix, pf, inet_ntoa(state->addr->addr)) == -1) 156 return -1; 157 if (efprintf(fp, "%s%ssubnet_mask=%s", 158 prefix, pf, inet_ntoa(state->addr->mask)) == -1) 159 return -1; 160 if (efprintf(fp, "%s%ssubnet_cidr=%d", 161 prefix, pf, inet_ntocidr(state->addr->mask)) == -1) 162 return -1; 163 if (efprintf(fp, "%s%sbroadcast_address=%s", 164 prefix, pf, inet_ntoa(state->addr->brd)) == -1) 165 return -1; 166 netnum.s_addr = state->addr->addr.s_addr & state->addr->mask.s_addr; 167 if (efprintf(fp, "%s%snetwork_number=%s", 168 prefix, pf, inet_ntoa(netnum)) == -1) 169 return -1; 170 return 5; 171 } 172 173 static void 174 ipv4ll_announced_arp(struct arp_state *astate) 175 { 176 struct ipv4ll_state *state = IPV4LL_STATE(astate->iface); 177 178 state->conflicts = 0; 179 #ifdef KERNEL_RFC5227 180 arp_free(astate); 181 #endif 182 } 183 184 static void 185 ipv4ll_arpfree(struct arp_state *astate) 186 { 187 struct ipv4ll_state *state; 188 189 state = IPV4LL_STATE(astate->iface); 190 if (state != NULL && state->arp == astate) 191 state->arp = NULL; 192 } 193 194 static void 195 ipv4ll_not_found(struct interface *ifp) 196 { 197 struct ipv4ll_state *state; 198 struct ipv4_addr *ia; 199 #ifdef KERNEL_RFC5227 200 struct arp_state *astate; 201 bool new_addr; 202 #endif 203 204 state = IPV4LL_STATE(ifp); 205 assert(state != NULL); 206 207 ia = ipv4_iffindaddr(ifp, &state->pickedaddr, &inaddr_llmask); 208 #ifdef KERNEL_RFC5227 209 new_addr = ia == NULL; 210 #endif 211 #ifdef IN_IFF_NOTREADY 212 if (ia == NULL || ia->addr_flags & IN_IFF_NOTREADY) 213 #endif 214 loginfox("%s: using IPv4LL address %s", 215 ifp->name, inet_ntoa(state->pickedaddr)); 216 if (ia == NULL) { 217 if (ifp->ctx->options & DHCPCD_TEST) 218 goto test; 219 ia = ipv4_addaddr(ifp, &state->pickedaddr, 220 &inaddr_llmask, &inaddr_llbcast, 221 DHCP_INFINITE_LIFETIME, DHCP_INFINITE_LIFETIME); 222 } 223 if (ia == NULL) 224 return; 225 #ifdef IN_IFF_NOTREADY 226 if (ia->addr_flags & IN_IFF_NOTREADY) 227 return; 228 logdebugx("%s: DAD completed for %s", ifp->name, ia->saddr); 229 #endif 230 test: 231 state->addr = ia; 232 state->down = false; 233 if (ifp->ctx->options & DHCPCD_TEST) { 234 script_runreason(ifp, "TEST"); 235 eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS); 236 return; 237 } 238 rt_build(ifp->ctx, AF_INET); 239 #ifdef KERNEL_RFC5227 240 if (!new_addr) { 241 astate = arp_new(ifp, &ia->addr); 242 if (astate != NULL) { 243 astate->announced_cb = ipv4ll_announced_arp; 244 astate->free_cb = ipv4ll_arpfree; 245 arp_announce(astate); 246 } 247 } 248 #else 249 arp_announce(state->arp); 250 #endif 251 script_runreason(ifp, "IPV4LL"); 252 dhcpcd_daemonise(ifp->ctx); 253 } 254 255 static void 256 ipv4ll_startifp(void *arg) 257 { 258 struct interface *ifp = arg; 259 struct ipv4ll_state *state; 260 261 state = IPV4LL_STATE(ifp); 262 ipv4ll_start1(ifp, state->arp); 263 } 264 265 static void 266 ipv4ll_found(struct interface *ifp) 267 { 268 struct ipv4ll_state *state = IPV4LL_STATE(ifp); 269 270 if (state->arp != NULL) 271 arp_cancel(state->arp); 272 if (++state->conflicts == MAX_CONFLICTS) 273 logerrx("%s: failed to acquire an IPv4LL address", 274 ifp->name); 275 state->pickedaddr.s_addr = ipv4ll_pickaddr(ifp); 276 eloop_timeout_add_sec(ifp->ctx->eloop, 277 state->conflicts >= MAX_CONFLICTS ? 278 RATE_LIMIT_INTERVAL : PROBE_WAIT, 279 ipv4ll_startifp, ifp); 280 } 281 282 static void 283 ipv4ll_defend_failed(struct interface *ifp) 284 { 285 struct ipv4ll_state *state = IPV4LL_STATE(ifp); 286 287 if (state->arp != NULL) 288 arp_cancel(state->arp); 289 ipv4_deladdr(state->addr, 1); 290 state->down = true; 291 state->addr = NULL; 292 rt_build(ifp->ctx, AF_INET); 293 script_runreason(ifp, "IPV4LL"); 294 state->pickedaddr.s_addr = ipv4ll_pickaddr(ifp); 295 ipv4ll_start1(ifp, state->arp); 296 } 297 298 #ifndef KERNEL_RFC5227 299 static void 300 ipv4ll_not_found_arp(struct arp_state *astate) 301 { 302 struct interface *ifp; 303 struct ipv4ll_state *state; 304 305 assert(astate != NULL); 306 assert(astate->iface != NULL); 307 308 ifp = astate->iface; 309 state = IPV4LL_STATE(ifp); 310 assert(state != NULL); 311 assert(state->arp == astate); 312 ipv4ll_not_found(ifp); 313 } 314 315 static void 316 ipv4ll_found_arp(struct arp_state *astate, __unused const struct arp_msg *amsg) 317 { 318 struct interface *ifp = astate->iface; 319 struct ipv4ll_state *state = IPV4LL_STATE(ifp); 320 321 assert(state->arp == astate); 322 ipv4ll_found(ifp); 323 } 324 325 static void 326 ipv4ll_defend_failed_arp(struct arp_state *astate) 327 { 328 struct ipv4ll_state *state = IPV4LL_STATE(astate->iface); 329 330 assert(state->arp == astate); 331 ipv4ll_defend_failed(astate->iface); 332 } 333 #endif 334 335 static void 336 ipv4ll_start1(struct interface *ifp, struct arp_state *astate) 337 { 338 struct ipv4ll_state *state; 339 struct ipv4_addr *ia; 340 bool repick; 341 342 assert(ifp != NULL); 343 if ((state = IPV4LL_STATE(ifp)) == NULL) { 344 ifp->if_data[IF_DATA_IPV4LL] = calloc(1, sizeof(*state)); 345 if ((state = IPV4LL_STATE(ifp)) == NULL) { 346 logerr(__func__); 347 return; 348 } 349 } 350 351 /* RFC 3927 Section 2.1 states that the random number generator 352 * SHOULD be seeded with a value derived from persistent information 353 * such as the IEEE 802 MAC address so that it usually picks 354 * the same address without persistent storage. */ 355 if (!state->seeded) { 356 unsigned int seed; 357 char *orig; 358 359 if (sizeof(seed) > ifp->hwlen) { 360 seed = 0; 361 memcpy(&seed, ifp->hwaddr, ifp->hwlen); 362 } else 363 memcpy(&seed, ifp->hwaddr + ifp->hwlen - sizeof(seed), 364 sizeof(seed)); 365 /* coverity[dont_call] */ 366 orig = initstate(seed, 367 state->randomstate, sizeof(state->randomstate)); 368 369 /* Save the original state. */ 370 if (ifp->ctx->randomstate == NULL) 371 ifp->ctx->randomstate = orig; 372 373 /* Set back the original state until we need the seeded one. */ 374 setstate(ifp->ctx->randomstate); 375 state->seeded = true; 376 } 377 378 #ifndef KERNEL_RFC5227 379 if (astate == NULL) { 380 if (state->arp != NULL) 381 return; 382 if ((astate = arp_new(ifp, NULL)) == NULL) 383 return; 384 astate->found_cb = ipv4ll_found_arp; 385 astate->not_found_cb = ipv4ll_not_found_arp; 386 astate->announced_cb = ipv4ll_announced_arp; 387 astate->defend_failed_cb = ipv4ll_defend_failed_arp; 388 astate->free_cb = ipv4ll_arpfree; 389 state->arp = astate; 390 } else 391 assert(state->arp == astate); 392 #else 393 UNUSED(astate); 394 #endif 395 396 state->down = true; 397 398 /* Find the previosuly used address. */ 399 if (state->pickedaddr.s_addr != INADDR_ANY) 400 ia = ipv4_iffindaddr(ifp, &state->pickedaddr, NULL); 401 else 402 ia = NULL; 403 404 /* Find an existing IPv4LL address and ensure we can work with it. */ 405 if (ia == NULL) 406 ia = ipv4_iffindlladdr(ifp); 407 408 repick = false; 409 #ifdef IN_IFF_DUPLICATED 410 if (ia != NULL && ia->addr_flags & IN_IFF_DUPLICATED) { 411 state->pickedaddr = ia->addr; /* So it's not picked again. */ 412 repick = true; 413 ipv4_deladdr(ia, 0); 414 ia = NULL; 415 } 416 #endif 417 418 state->addr = ia; 419 if (ia != NULL) { 420 state->pickedaddr = ia->addr; 421 #ifndef KERNEL_RFC5227 422 astate->addr = ia->addr; 423 #endif 424 #ifdef IN_IFF_TENTATIVE 425 if (ia->addr_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED)) { 426 loginfox("%s: waiting for DAD to complete on %s", 427 ifp->name, inet_ntoa(ia->addr)); 428 return; 429 } 430 #endif 431 #ifdef IN_IFF_DUPLICATED 432 loginfox("%s: using IPv4LL address %s", ifp->name, ia->saddr); 433 #endif 434 ipv4ll_not_found(ifp); 435 return; 436 } 437 438 loginfox("%s: probing for an IPv4LL address", ifp->name); 439 if (repick || state->pickedaddr.s_addr == INADDR_ANY) 440 state->pickedaddr.s_addr = ipv4ll_pickaddr(ifp); 441 #ifndef KERNEL_RFC5227 442 astate->addr = state->pickedaddr; 443 #endif 444 #ifdef IN_IFF_DUPLICATED 445 ipv4ll_not_found(ifp); 446 #else 447 arp_probe(astate); 448 #endif 449 } 450 451 void 452 ipv4ll_start(void *arg) 453 { 454 455 ipv4ll_start1(arg, NULL); 456 } 457 458 static void 459 ipv4ll_freearp(struct interface *ifp) 460 { 461 struct ipv4ll_state *state; 462 463 state = IPV4LL_STATE(ifp); 464 if (state == NULL || state->arp == NULL) 465 return; 466 467 eloop_timeout_delete(ifp->ctx->eloop, NULL, state->arp); 468 arp_free(state->arp); 469 } 470 471 void 472 ipv4ll_drop(struct interface *ifp) 473 { 474 struct ipv4ll_state *state; 475 bool dropped = false; 476 struct ipv4_state *istate; 477 478 assert(ifp != NULL); 479 480 ipv4ll_freearp(ifp); 481 482 if ((ifp->options->options & DHCPCD_NODROP) == DHCPCD_NODROP) 483 return; 484 485 state = IPV4LL_STATE(ifp); 486 if (state && state->addr != NULL) { 487 ipv4_deladdr(state->addr, 1); 488 state->addr = NULL; 489 dropped = true; 490 } 491 492 /* Free any other link local addresses that might exist. */ 493 if ((istate = IPV4_STATE(ifp)) != NULL) { 494 struct ipv4_addr *ia, *ian; 495 496 TAILQ_FOREACH_SAFE(ia, &istate->addrs, next, ian) { 497 if (IN_LINKLOCAL(ntohl(ia->addr.s_addr))) { 498 ipv4_deladdr(ia, 0); 499 dropped = true; 500 } 501 } 502 } 503 504 if (dropped) { 505 rt_build(ifp->ctx, AF_INET); 506 script_runreason(ifp, "IPV4LL"); 507 } 508 } 509 510 void 511 ipv4ll_reset(struct interface *ifp) 512 { 513 struct ipv4ll_state *state = IPV4LL_STATE(ifp); 514 515 if (state == NULL) 516 return; 517 state->pickedaddr.s_addr = INADDR_ANY; 518 state->seeded = false; 519 } 520 521 void 522 ipv4ll_free(struct interface *ifp) 523 { 524 525 assert(ifp != NULL); 526 527 ipv4ll_freearp(ifp); 528 free(IPV4LL_STATE(ifp)); 529 ifp->if_data[IF_DATA_IPV4LL] = NULL; 530 } 531 532 /* This may cause issues in BSD systems, where running as a single dhcpcd 533 * daemon would solve this issue easily. */ 534 #ifdef HAVE_ROUTE_METRIC 535 int 536 ipv4ll_recvrt(__unused int cmd, const struct rt *rt) 537 { 538 struct dhcpcd_ctx *ctx; 539 struct interface *ifp; 540 541 /* Only interested in default route changes. */ 542 if (sa_is_unspecified(&rt->rt_dest)) 543 return 0; 544 545 /* If any interface is running IPv4LL, rebuild our routing table. */ 546 ctx = rt->rt_ifp->ctx; 547 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 548 if (IPV4LL_STATE_RUNNING(ifp)) { 549 rt_build(ctx, AF_INET); 550 break; 551 } 552 } 553 554 return 0; 555 } 556 #endif 557 558 struct ipv4_addr * 559 ipv4ll_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid) 560 { 561 struct interface *ifp; 562 struct ipv4ll_state *state; 563 564 ifp = ia->iface; 565 state = IPV4LL_STATE(ifp); 566 if (state == NULL) 567 return ia; 568 569 if (cmd == RTM_DELADDR && 570 state->addr != NULL && 571 IN_ARE_ADDR_EQUAL(&state->addr->addr, &ia->addr)) 572 { 573 loginfox("%s: pid %d deleted IP address %s", 574 ifp->name, pid, ia->saddr); 575 ipv4ll_defend_failed(ifp); 576 return ia; 577 } 578 579 #ifdef IN_IFF_DUPLICATED 580 if (cmd != RTM_NEWADDR) 581 return ia; 582 if (!IN_ARE_ADDR_EQUAL(&state->pickedaddr, &ia->addr)) 583 return ia; 584 if (!(ia->addr_flags & IN_IFF_NOTUSEABLE)) 585 ipv4ll_not_found(ifp); 586 else if (ia->addr_flags & IN_IFF_DUPLICATED) { 587 logerrx("%s: DAD detected %s", ifp->name, ia->saddr); 588 #ifdef KERNEL_RFC5227 589 arp_freeaddr(ifp, &ia->addr); 590 #endif 591 ipv4_deladdr(ia, 1); 592 state->addr = NULL; 593 rt_build(ifp->ctx, AF_INET); 594 ipv4ll_found(ifp); 595 return NULL; 596 } 597 #endif 598 599 return ia; 600 } 601