int __ni_dhcp4_fsm_discover(ni_dhcp4_device_t *dev, int scan_offers) { ni_addrconf_lease_t *lease; int rv; ni_info("%s: Initiating DHCPv4 discovery (ifindex %d)", dev->ifname, dev->link.ifindex); /* If we already have a lease, try asking for the same. * If not, create a dummy lease with NULL fields. * Note: if DISCOVER for the old lease times out, * we should fall back to asking for anything. */ if ((lease = dev->lease) == NULL) lease = ni_addrconf_lease_new(NI_ADDRCONF_DHCP, AF_INET); lease->uuid = dev->config->uuid; dev->fsm.state = NI_DHCP4_STATE_SELECTING; rv = ni_dhcp4_device_send_message(dev, DHCP4_DISCOVER, lease); dev->dhcp4.accept_any_offer = 1; ni_debug_dhcp("valid lease: %d; have prefs: %d", ni_addrconf_lease_is_valid(dev->lease), ni_dhcp4_config_have_server_preference()); if (ni_addrconf_lease_is_valid(dev->lease) || (scan_offers && ni_dhcp4_config_have_server_preference())) { ni_dhcp4_fsm_set_timeout(dev, dev->config->initial_discovery_timeout); dev->dhcp4.accept_any_offer = 0; } else { ni_dhcp4_fsm_set_timeout(dev, dev->config->request_timeout); } ni_dhcp4_device_drop_best_offer(dev); if (lease != dev->lease) ni_addrconf_lease_free(lease); return rv; }
/* * Reload an old lease from file, and see whether we can reuse it. * This is used during restart of wickedd. */ int ni_dhcp4_recover_lease(ni_dhcp4_device_t *dev) { ni_addrconf_lease_t *lease; ni_sockaddr_t addr; if (dev->lease) return 1; lease = ni_addrconf_lease_file_read(dev->ifname, NI_ADDRCONF_DHCP, AF_INET); if (!lease) return -1; if (!ni_addrconf_lease_is_valid(dev->lease)) { ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP, "%s: discarding existing lease, not granted", dev->ifname); goto discard; } /* We cannot renew/rebind/reboot without it */ ni_sockaddr_set_ipv4(&addr, lease->dhcp4.server_id, 0); if (!ni_sockaddr_is_ipv4_specified(&addr)) { ni_debug_verbose(NI_LOG_DEBUG1, NI_TRACE_DHCP, "%s: discarding existing lease, no server-id", dev->ifname); goto discard; } ni_dhcp4_device_set_lease(dev, lease); return 0; discard: ni_addrconf_lease_free(lease); return -1; }
/* * Process a request to reconfigure the device (ie rebind a lease, or discover * a new lease). */ int ni_dhcp4_acquire(ni_dhcp4_device_t *dev, const ni_dhcp4_request_t *info) { ni_dhcp4_config_t *config; const char *classid; size_t len; int rv; if ((rv = ni_dhcp4_device_refresh(dev)) < 0) return rv; config = xcalloc(1, sizeof(*config)); config->dry_run = info->dry_run; config->resend_timeout = NI_DHCP4_RESEND_TIMEOUT_INIT; config->request_timeout = info->acquire_timeout?: NI_DHCP4_REQUEST_TIMEOUT; config->initial_discovery_timeout = NI_DHCP4_DISCOVERY_TIMEOUT; config->uuid = info->uuid; config->flags = info->flags; config->update = info->update; config->route_priority = info->route_priority; config->start_delay = info->start_delay; config->recover_lease = info->recover_lease; config->release_lease = info->release_lease; config->max_lease_time = ni_dhcp4_config_max_lease_time(); if (config->max_lease_time == 0) config->max_lease_time = ~0U; if (info->lease_time && info->lease_time < config->max_lease_time) config->max_lease_time = info->lease_time; if ((len = ni_string_len(info->hostname)) > 0) { if (ni_check_domain_name(info->hostname, len, 0)) { strncpy(config->hostname, info->hostname, sizeof(config->hostname) - 1); } else { ni_debug_dhcp("Discarded request to use suspect hostname: '%s'", ni_print_suspect(info->hostname, len)); } } if (info->clientid) { ni_dhcp4_parse_client_id(&config->client_id, dev->system.hwaddr.type, info->clientid); } else { /* Set client ID from interface hwaddr */ ni_dhcp4_set_client_id(&config->client_id, &dev->system.hwaddr); } if ((classid = info->vendor_class) == NULL) classid = ni_dhcp4_config_vendor_class(); if (classid) strncpy(config->classid, classid, sizeof(config->classid) - 1); config->doflags = DHCP4_DO_DEFAULT; config->doflags |= ni_dhcp4_do_bits(info->update); if (ni_debug & NI_TRACE_DHCP) { ni_trace("Received request:"); ni_trace(" acquire-timeout %u", config->request_timeout); ni_trace(" lease-time %u", config->max_lease_time); ni_trace(" start-delay %u", config->start_delay); ni_trace(" hostname %s", config->hostname[0]? config->hostname : "<none>"); ni_trace(" vendor-class %s", config->classid[0]? config->classid : "<none>"); ni_trace(" client-id %s", ni_print_hex(config->client_id.data, config->client_id.len)); ni_trace(" uuid %s", ni_uuid_print(&config->uuid)); ni_trace(" update-flags %s", __ni_dhcp4_print_doflags(config->doflags)); ni_trace(" recover_lease %s", config->recover_lease ? "true" : "false"); ni_trace(" release_lease %s", config->release_lease ? "true" : "false"); } ni_dhcp4_device_set_config(dev, config); if (!dev->lease) ni_dhcp4_recover_lease(dev); if (dev->lease) { if (!ni_addrconf_lease_is_valid(dev->lease) || (config->client_id.len && !ni_opaque_eq(&config->client_id, &dev->lease->dhcp4.client_id))) { ni_debug_dhcp("%s: lease doesn't match request", dev->ifname); ni_dhcp4_device_drop_lease(dev); dev->notify = 1; } } ni_note("%s: Request to acquire DHCPv4 lease with UUID %s", dev->ifname, ni_uuid_print(&config->uuid)); if (ni_dhcp4_device_start(dev) < 0) return -1; return 1; }