1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2020 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 IPV4LL 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 61 ipv4ll_pickaddr(struct interface *ifp) 62 { 63 struct in_addr addr = { .s_addr = 0 }; 64 struct ipv4ll_state *state; 65 66 state = IPV4LL_STATE(ifp); 67 setstate(state->randomstate); 68 69 do { 70 long r; 71 72 again: 73 /* RFC 3927 Section 2.1 states that the first 256 and 74 * last 256 addresses are reserved for future use. 75 * See ipv4ll_start for why we don't use arc4random. */ 76 /* coverity[dont_call] */ 77 r = random(); 78 addr.s_addr = ntohl(LINKLOCAL_ADDR | 79 ((uint32_t)(r % 0xFD00) + 0x0100)); 80 81 /* No point using a failed address */ 82 if (IN_ARE_ADDR_EQUAL(&addr, &state->pickedaddr)) 83 goto again; 84 /* Ensure we don't have the address on another interface */ 85 } while (ipv4_findaddr(ifp->ctx, &addr) != NULL); 86 87 /* Restore the original random state */ 88 setstate(ifp->ctx->randomstate); 89 state->pickedaddr = addr; 90 } 91 92 int 93 ipv4ll_subnetroute(rb_tree_t *routes, struct interface *ifp) 94 { 95 struct ipv4ll_state *state; 96 struct rt *rt; 97 struct in_addr in; 98 99 assert(ifp != NULL); 100 if ((state = IPV4LL_STATE(ifp)) == NULL || 101 state->addr == NULL) 102 return 0; 103 104 if ((rt = rt_new(ifp)) == NULL) 105 return -1; 106 107 in.s_addr = state->addr->addr.s_addr & state->addr->mask.s_addr; 108 sa_in_init(&rt->rt_dest, &in); 109 in.s_addr = state->addr->mask.s_addr; 110 sa_in_init(&rt->rt_netmask, &in); 111 in.s_addr = INADDR_ANY; 112 sa_in_init(&rt->rt_gateway, &in); 113 sa_in_init(&rt->rt_ifa, &state->addr->addr); 114 rt->rt_dflags |= RTDF_IPV4LL; 115 return rt_proto_add(routes, rt) ? 1 : 0; 116 } 117 118 int 119 ipv4ll_defaultroute(rb_tree_t *routes, struct interface *ifp) 120 { 121 struct ipv4ll_state *state; 122 struct rt *rt; 123 struct in_addr in; 124 125 assert(ifp != NULL); 126 if ((state = IPV4LL_STATE(ifp)) == NULL || 127 state->addr == NULL) 128 return 0; 129 130 if ((rt = rt_new(ifp)) == NULL) 131 return -1; 132 133 in.s_addr = INADDR_ANY; 134 sa_in_init(&rt->rt_dest, &in); 135 sa_in_init(&rt->rt_netmask, &in); 136 sa_in_init(&rt->rt_gateway, &in); 137 sa_in_init(&rt->rt_ifa, &state->addr->addr); 138 rt->rt_dflags |= RTDF_IPV4LL; 139 #ifdef HAVE_ROUTE_METRIC 140 rt->rt_metric += 10000; 141 #endif 142 return rt_proto_add(routes, rt) ? 1 : 0; 143 } 144 145 ssize_t 146 ipv4ll_env(FILE *fp, const char *prefix, const struct interface *ifp) 147 { 148 const struct ipv4ll_state *state; 149 const char *pf = prefix == NULL ? "" : "_"; 150 struct in_addr netnum; 151 152 assert(ifp != NULL); 153 if ((state = IPV4LL_CSTATE(ifp)) == NULL || state->addr == NULL) 154 return 0; 155 156 /* Emulate a DHCP environment */ 157 if (efprintf(fp, "%s%sip_address=%s", 158 prefix, pf, inet_ntoa(state->addr->addr)) == -1) 159 return -1; 160 if (efprintf(fp, "%s%ssubnet_mask=%s", 161 prefix, pf, inet_ntoa(state->addr->mask)) == -1) 162 return -1; 163 if (efprintf(fp, "%s%ssubnet_cidr=%d", 164 prefix, pf, inet_ntocidr(state->addr->mask)) == -1) 165 return -1; 166 if (efprintf(fp, "%s%sbroadcast_address=%s", 167 prefix, pf, inet_ntoa(state->addr->brd)) == -1) 168 return -1; 169 netnum.s_addr = state->addr->addr.s_addr & state->addr->mask.s_addr; 170 if (efprintf(fp, "%s%snetwork_number=%s", 171 prefix, pf, inet_ntoa(netnum)) == -1) 172 return -1; 173 return 5; 174 } 175 176 static void 177 ipv4ll_announced_arp(struct arp_state *astate) 178 { 179 struct ipv4ll_state *state = IPV4LL_STATE(astate->iface); 180 181 state->conflicts = 0; 182 #ifdef KERNEL_RFC5227 183 arp_free(astate); 184 #endif 185 } 186 187 #ifndef KERNEL_RFC5227 188 /* This is the callback by ARP freeing */ 189 static void 190 ipv4ll_free_arp(struct arp_state *astate) 191 { 192 struct ipv4ll_state *state; 193 194 state = IPV4LL_STATE(astate->iface); 195 if (state != NULL && state->arp == astate) 196 state->arp = NULL; 197 } 198 199 /* This is us freeing any ARP state */ 200 static void 201 ipv4ll_freearp(struct interface *ifp) 202 { 203 struct ipv4ll_state *state; 204 205 state = IPV4LL_STATE(ifp); 206 if (state == NULL || state->arp == NULL) 207 return; 208 209 eloop_timeout_delete(ifp->ctx->eloop, NULL, state->arp); 210 arp_free(state->arp); 211 state->arp = NULL; 212 } 213 #else 214 #define ipv4ll_freearp(ifp) 215 #endif 216 217 static void 218 ipv4ll_not_found(struct interface *ifp) 219 { 220 struct ipv4ll_state *state; 221 struct ipv4_addr *ia; 222 struct arp_state *astate; 223 224 state = IPV4LL_STATE(ifp); 225 ia = ipv4_iffindaddr(ifp, &state->pickedaddr, &inaddr_llmask); 226 #ifdef IN_IFF_NOTREADY 227 if (ia == NULL || ia->addr_flags & IN_IFF_NOTREADY) 228 #endif 229 loginfox("%s: using IPv4LL address %s", 230 ifp->name, inet_ntoa(state->pickedaddr)); 231 if (ia == NULL) { 232 if (ifp->ctx->options & DHCPCD_TEST) 233 goto test; 234 ia = ipv4_addaddr(ifp, &state->pickedaddr, 235 &inaddr_llmask, &inaddr_llbcast, 236 DHCP_INFINITE_LIFETIME, DHCP_INFINITE_LIFETIME); 237 } 238 if (ia == NULL) 239 return; 240 #ifdef IN_IFF_NOTREADY 241 if (ia->addr_flags & IN_IFF_NOTREADY) 242 return; 243 logdebugx("%s: DAD completed for %s", ifp->name, ia->saddr); 244 #endif 245 246 test: 247 state->addr = ia; 248 state->down = false; 249 if (ifp->ctx->options & DHCPCD_TEST) { 250 script_runreason(ifp, "TEST"); 251 eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS); 252 return; 253 } 254 rt_build(ifp->ctx, AF_INET); 255 astate = arp_announceaddr(ifp->ctx, &ia->addr); 256 if (astate != NULL) 257 astate->announced_cb = ipv4ll_announced_arp; 258 script_runreason(ifp, "IPV4LL"); 259 dhcpcd_daemonise(ifp->ctx); 260 } 261 262 static void 263 ipv4ll_found(struct interface *ifp) 264 { 265 struct ipv4ll_state *state = IPV4LL_STATE(ifp); 266 267 ipv4ll_freearp(ifp); 268 if (++state->conflicts == MAX_CONFLICTS) 269 logerrx("%s: failed to acquire an IPv4LL address", 270 ifp->name); 271 ipv4ll_pickaddr(ifp); 272 eloop_timeout_add_sec(ifp->ctx->eloop, 273 state->conflicts >= MAX_CONFLICTS ? 274 RATE_LIMIT_INTERVAL : PROBE_WAIT, 275 ipv4ll_start, ifp); 276 } 277 278 static void 279 ipv4ll_defend_failed(struct interface *ifp) 280 { 281 struct ipv4ll_state *state = IPV4LL_STATE(ifp); 282 283 ipv4ll_freearp(ifp); 284 ipv4_deladdr(state->addr, 1); 285 state->addr = NULL; 286 rt_build(ifp->ctx, AF_INET); 287 script_runreason(ifp, "IPV4LL"); 288 ipv4ll_pickaddr(ifp); 289 ipv4ll_start(ifp); 290 } 291 292 #ifndef KERNEL_RFC5227 293 static void 294 ipv4ll_not_found_arp(struct arp_state *astate) 295 { 296 297 ipv4ll_not_found(astate->iface); 298 } 299 300 static void 301 ipv4ll_found_arp(struct arp_state *astate, __unused const struct arp_msg *amsg) 302 { 303 304 ipv4ll_found(astate->iface); 305 } 306 307 static void 308 ipv4ll_defend_failed_arp(struct arp_state *astate) 309 { 310 311 ipv4ll_defend_failed(astate->iface); 312 } 313 #endif 314 315 void 316 ipv4ll_start(void *arg) 317 { 318 struct interface *ifp = arg; 319 struct ipv4ll_state *state; 320 struct ipv4_addr *ia; 321 bool repick; 322 #ifndef KERNEL_RFC5227 323 struct arp_state *astate; 324 #endif 325 326 if ((state = IPV4LL_STATE(ifp)) == NULL) { 327 ifp->if_data[IF_DATA_IPV4LL] = calloc(1, sizeof(*state)); 328 if ((state = IPV4LL_STATE(ifp)) == NULL) { 329 logerr(__func__); 330 return; 331 } 332 } 333 334 /* RFC 3927 Section 2.1 states that the random number generator 335 * SHOULD be seeded with a value derived from persistent information 336 * such as the IEEE 802 MAC address so that it usually picks 337 * the same address without persistent storage. */ 338 if (!state->seeded) { 339 unsigned int seed; 340 char *orig; 341 342 if (sizeof(seed) > ifp->hwlen) { 343 seed = 0; 344 memcpy(&seed, ifp->hwaddr, ifp->hwlen); 345 } else 346 memcpy(&seed, ifp->hwaddr + ifp->hwlen - sizeof(seed), 347 sizeof(seed)); 348 /* coverity[dont_call] */ 349 orig = initstate(seed, 350 state->randomstate, sizeof(state->randomstate)); 351 352 /* Save the original state. */ 353 if (ifp->ctx->randomstate == NULL) 354 ifp->ctx->randomstate = orig; 355 356 /* Set back the original state until we need the seeded one. */ 357 setstate(ifp->ctx->randomstate); 358 state->seeded = true; 359 } 360 361 /* Find the previosuly used address. */ 362 if (state->pickedaddr.s_addr != INADDR_ANY) 363 ia = ipv4_iffindaddr(ifp, &state->pickedaddr, NULL); 364 else 365 ia = NULL; 366 367 /* Find an existing IPv4LL address and ensure we can work with it. */ 368 if (ia == NULL) 369 ia = ipv4_iffindlladdr(ifp); 370 371 repick = false; 372 #ifdef IN_IFF_DUPLICATED 373 if (ia != NULL && ia->addr_flags & IN_IFF_DUPLICATED) { 374 state->pickedaddr = ia->addr; /* So it's not picked again. */ 375 repick = true; 376 ipv4_deladdr(ia, 0); 377 ia = NULL; 378 } 379 #endif 380 381 state->addr = ia; 382 state->down = true; 383 if (ia != NULL) { 384 state->pickedaddr = ia->addr; 385 #ifdef IN_IFF_TENTATIVE 386 if (ia->addr_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED)) { 387 loginfox("%s: waiting for DAD to complete on %s", 388 ifp->name, inet_ntoa(ia->addr)); 389 return; 390 } 391 #endif 392 #ifdef IN_IFF_DUPLICATED 393 loginfox("%s: using IPv4LL address %s", ifp->name, ia->saddr); 394 #endif 395 } else { 396 loginfox("%s: probing for an IPv4LL address", ifp->name); 397 if (repick || state->pickedaddr.s_addr == INADDR_ANY) 398 ipv4ll_pickaddr(ifp); 399 } 400 401 #ifdef KERNEL_RFC5227 402 ipv4ll_not_found(ifp); 403 #else 404 ipv4ll_freearp(ifp); 405 state->arp = astate = arp_new(ifp, &state->pickedaddr); 406 if (state->arp == NULL) 407 return; 408 409 astate->found_cb = ipv4ll_found_arp; 410 astate->not_found_cb = ipv4ll_not_found_arp; 411 astate->announced_cb = ipv4ll_announced_arp; 412 astate->defend_failed_cb = ipv4ll_defend_failed_arp; 413 astate->free_cb = ipv4ll_free_arp; 414 arp_probe(astate); 415 #endif 416 } 417 418 void 419 ipv4ll_drop(struct interface *ifp) 420 { 421 struct ipv4ll_state *state; 422 bool dropped = false; 423 struct ipv4_state *istate; 424 425 assert(ifp != NULL); 426 427 ipv4ll_freearp(ifp); 428 429 if ((ifp->options->options & DHCPCD_NODROP) == DHCPCD_NODROP) 430 return; 431 432 state = IPV4LL_STATE(ifp); 433 if (state && state->addr != NULL) { 434 ipv4_deladdr(state->addr, 1); 435 state->addr = NULL; 436 dropped = true; 437 } 438 439 /* Free any other link local addresses that might exist. */ 440 if ((istate = IPV4_STATE(ifp)) != NULL) { 441 struct ipv4_addr *ia, *ian; 442 443 TAILQ_FOREACH_SAFE(ia, &istate->addrs, next, ian) { 444 if (IN_LINKLOCAL(ntohl(ia->addr.s_addr))) { 445 ipv4_deladdr(ia, 0); 446 dropped = true; 447 } 448 } 449 } 450 451 if (dropped) { 452 rt_build(ifp->ctx, AF_INET); 453 script_runreason(ifp, "IPV4LL"); 454 } 455 } 456 457 void 458 ipv4ll_reset(struct interface *ifp) 459 { 460 struct ipv4ll_state *state = IPV4LL_STATE(ifp); 461 462 if (state == NULL) 463 return; 464 ipv4ll_freearp(ifp); 465 state->pickedaddr.s_addr = INADDR_ANY; 466 state->seeded = false; 467 } 468 469 void 470 ipv4ll_free(struct interface *ifp) 471 { 472 473 assert(ifp != NULL); 474 475 ipv4ll_freearp(ifp); 476 free(IPV4LL_STATE(ifp)); 477 ifp->if_data[IF_DATA_IPV4LL] = NULL; 478 } 479 480 /* This may cause issues in BSD systems, where running as a single dhcpcd 481 * daemon would solve this issue easily. */ 482 #ifdef HAVE_ROUTE_METRIC 483 int 484 ipv4ll_recvrt(__unused int cmd, const struct rt *rt) 485 { 486 struct dhcpcd_ctx *ctx; 487 struct interface *ifp; 488 489 /* Only interested in default route changes. */ 490 if (sa_is_unspecified(&rt->rt_dest)) 491 return 0; 492 493 /* If any interface is running IPv4LL, rebuild our routing table. */ 494 ctx = rt->rt_ifp->ctx; 495 TAILQ_FOREACH(ifp, ctx->ifaces, next) { 496 if (IPV4LL_STATE_RUNNING(ifp)) { 497 rt_build(ctx, AF_INET); 498 break; 499 } 500 } 501 502 return 0; 503 } 504 #endif 505 506 struct ipv4_addr * 507 ipv4ll_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid) 508 { 509 struct interface *ifp; 510 struct ipv4ll_state *state; 511 512 ifp = ia->iface; 513 state = IPV4LL_STATE(ifp); 514 if (state == NULL) 515 return ia; 516 517 if (cmd == RTM_DELADDR && 518 state->addr != NULL && 519 IN_ARE_ADDR_EQUAL(&state->addr->addr, &ia->addr)) 520 { 521 loginfox("%s: pid %d deleted IP address %s", 522 ifp->name, pid, ia->saddr); 523 ipv4ll_defend_failed(ifp); 524 return ia; 525 } 526 527 #ifdef IN_IFF_DUPLICATED 528 if (cmd != RTM_NEWADDR) 529 return ia; 530 if (!IN_ARE_ADDR_EQUAL(&state->pickedaddr, &ia->addr)) 531 return ia; 532 if (!(ia->addr_flags & IN_IFF_NOTUSEABLE)) 533 ipv4ll_not_found(ifp); 534 else if (ia->addr_flags & IN_IFF_DUPLICATED) { 535 logerrx("%s: DAD detected %s", ifp->name, ia->saddr); 536 ipv4ll_freearp(ifp); 537 ipv4_deladdr(ia, 1); 538 state->addr = NULL; 539 rt_build(ifp->ctx, AF_INET); 540 ipv4ll_found(ifp); 541 return NULL; 542 } 543 #endif 544 545 return ia; 546 } 547