"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 3 | * udhcp server |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au> |
| 5 | * Chris Trew <ctrew@moreton.com.au> |
| 6 | * |
| 7 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 8 | * |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 22 | */ |
Denys Vlasenko | f7683cd | 2016-11-23 18:54:59 +0100 | [diff] [blame^] | 23 | //applet:IF_UDHCPD(APPLET(udhcpd, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 24 | |
| 25 | //kbuild:lib-$(CONFIG_UDHCPD) += common.o packet.o signalpipe.o socket.o |
| 26 | //kbuild:lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o |
| 27 | //kbuild:lib-$(CONFIG_FEATURE_UDHCP_RFC3397) += domain_codec.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 28 | |
| 29 | //usage:#define udhcpd_trivial_usage |
Denys Vlasenko | 7b5d5c1 | 2013-03-14 02:18:52 +0100 | [diff] [blame] | 30 | //usage: "[-fS] [-I ADDR]" IF_FEATURE_UDHCP_PORT(" [-P N]") " [CONFFILE]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 31 | //usage:#define udhcpd_full_usage "\n\n" |
| 32 | //usage: "DHCP server\n" |
| 33 | //usage: "\n -f Run in foreground" |
| 34 | //usage: "\n -S Log to syslog too" |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 35 | //usage: "\n -I ADDR Local address" |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 36 | //usage: "\n -a MSEC Timeout for ARP ping (default 2000)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 37 | //usage: IF_FEATURE_UDHCP_PORT( |
| 38 | //usage: "\n -P N Use port N (default 67)" |
| 39 | //usage: ) |
| 40 | |
Denys Vlasenko | 2bf2931 | 2016-10-04 00:37:50 +0200 | [diff] [blame] | 41 | #include <netinet/ether.h> |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 42 | #include <syslog.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 43 | #include "common.h" |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 44 | #include "dhcpc.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 45 | #include "dhcpd.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 46 | |
Denys Vlasenko | a85740c | 2016-10-04 00:51:38 +0200 | [diff] [blame] | 47 | /* globals */ |
| 48 | struct dyn_lease *g_leases; |
| 49 | /* struct server_config_t server_config is in bb_common_bufsiz1 */ |
| 50 | |
Denys Vlasenko | d2ae66c | 2016-10-04 00:43:14 +0200 | [diff] [blame] | 51 | /* Takes the address of the pointer to the static_leases linked list, |
| 52 | * address to a 6 byte mac address, |
| 53 | * 4 byte IP address */ |
| 54 | static void add_static_lease(struct static_lease **st_lease_pp, |
| 55 | uint8_t *mac, |
| 56 | uint32_t nip) |
| 57 | { |
| 58 | struct static_lease *st_lease; |
| 59 | |
| 60 | /* Find the tail of the list */ |
| 61 | while ((st_lease = *st_lease_pp) != NULL) { |
| 62 | st_lease_pp = &st_lease->next; |
| 63 | } |
| 64 | |
| 65 | /* Add new node */ |
| 66 | *st_lease_pp = st_lease = xzalloc(sizeof(*st_lease)); |
| 67 | memcpy(st_lease->mac, mac, 6); |
| 68 | st_lease->nip = nip; |
| 69 | /*st_lease->next = NULL;*/ |
| 70 | } |
| 71 | |
| 72 | /* Find static lease IP by mac */ |
| 73 | static uint32_t get_static_nip_by_mac(struct static_lease *st_lease, void *mac) |
| 74 | { |
| 75 | while (st_lease) { |
| 76 | if (memcmp(st_lease->mac, mac, 6) == 0) |
| 77 | return st_lease->nip; |
| 78 | st_lease = st_lease->next; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Denys Vlasenko | a85740c | 2016-10-04 00:51:38 +0200 | [diff] [blame] | 84 | static int is_nip_reserved(struct static_lease *st_lease, uint32_t nip) |
| 85 | { |
| 86 | while (st_lease) { |
| 87 | if (st_lease->nip == nip) |
| 88 | return 1; |
| 89 | st_lease = st_lease->next; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Denys Vlasenko | d2ae66c | 2016-10-04 00:43:14 +0200 | [diff] [blame] | 95 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 |
| 96 | /* Print out static leases just to check what's going on */ |
| 97 | /* Takes the address of the pointer to the static_leases linked list */ |
| 98 | static void log_static_leases(struct static_lease **st_lease_pp) |
| 99 | { |
| 100 | struct static_lease *cur; |
| 101 | |
| 102 | if (dhcp_verbose < 2) |
| 103 | return; |
| 104 | |
| 105 | cur = *st_lease_pp; |
| 106 | while (cur) { |
| 107 | bb_error_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x", |
| 108 | cur->mac[0], cur->mac[1], cur->mac[2], |
| 109 | cur->mac[3], cur->mac[4], cur->mac[5], |
| 110 | cur->nip |
| 111 | ); |
| 112 | cur = cur->next; |
| 113 | } |
| 114 | } |
| 115 | #else |
| 116 | # define log_static_leases(st_lease_pp) ((void)0) |
| 117 | #endif |
| 118 | |
Denys Vlasenko | a85740c | 2016-10-04 00:51:38 +0200 | [diff] [blame] | 119 | /* Find the oldest expired lease, NULL if there are no expired leases */ |
| 120 | static struct dyn_lease *oldest_expired_lease(void) |
| 121 | { |
| 122 | struct dyn_lease *oldest_lease = NULL; |
| 123 | leasetime_t oldest_time = time(NULL); |
| 124 | unsigned i; |
| 125 | |
| 126 | /* Unexpired leases have g_leases[i].expires >= current time |
| 127 | * and therefore can't ever match */ |
| 128 | for (i = 0; i < server_config.max_leases; i++) { |
| 129 | if (g_leases[i].expires == 0 /* empty entry */ |
| 130 | || g_leases[i].expires < oldest_time |
| 131 | ) { |
| 132 | oldest_time = g_leases[i].expires; |
| 133 | oldest_lease = &g_leases[i]; |
| 134 | } |
| 135 | } |
| 136 | return oldest_lease; |
| 137 | } |
| 138 | |
| 139 | /* Clear out all leases with matching nonzero chaddr OR yiaddr. |
| 140 | * If chaddr == NULL, this is a conflict lease. |
| 141 | */ |
| 142 | static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr) |
| 143 | { |
| 144 | unsigned i; |
| 145 | |
| 146 | for (i = 0; i < server_config.max_leases; i++) { |
| 147 | if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0) |
| 148 | || (yiaddr && g_leases[i].lease_nip == yiaddr) |
| 149 | ) { |
| 150 | memset(&g_leases[i], 0, sizeof(g_leases[i])); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* Add a lease into the table, clearing out any old ones. |
| 156 | * If chaddr == NULL, this is a conflict lease. |
| 157 | */ |
| 158 | static struct dyn_lease *add_lease( |
| 159 | const uint8_t *chaddr, uint32_t yiaddr, |
| 160 | leasetime_t leasetime, |
| 161 | const char *hostname, int hostname_len) |
| 162 | { |
| 163 | struct dyn_lease *oldest; |
| 164 | |
| 165 | /* clean out any old ones */ |
| 166 | clear_leases(chaddr, yiaddr); |
| 167 | |
| 168 | oldest = oldest_expired_lease(); |
| 169 | |
| 170 | if (oldest) { |
| 171 | memset(oldest, 0, sizeof(*oldest)); |
| 172 | if (hostname) { |
| 173 | char *p; |
| 174 | |
| 175 | hostname_len++; /* include NUL */ |
| 176 | if (hostname_len > sizeof(oldest->hostname)) |
| 177 | hostname_len = sizeof(oldest->hostname); |
| 178 | p = safe_strncpy(oldest->hostname, hostname, hostname_len); |
| 179 | /* |
| 180 | * Sanitization (s/bad_char/./g). |
| 181 | * The intent is not to allow only "DNS-valid" hostnames, |
| 182 | * but merely make dumpleases output safe for shells to use. |
| 183 | * We accept "0-9A-Za-z._-", all other chars turn to dots. |
| 184 | */ |
| 185 | while (*p) { |
| 186 | if (!isalnum(*p) && *p != '-' && *p != '_') |
| 187 | *p = '.'; |
| 188 | p++; |
| 189 | } |
| 190 | } |
| 191 | if (chaddr) |
| 192 | memcpy(oldest->lease_mac, chaddr, 6); |
| 193 | oldest->lease_nip = yiaddr; |
| 194 | oldest->expires = time(NULL) + leasetime; |
| 195 | } |
| 196 | |
| 197 | return oldest; |
| 198 | } |
| 199 | |
| 200 | /* True if a lease has expired */ |
| 201 | static int is_expired_lease(struct dyn_lease *lease) |
| 202 | { |
| 203 | return (lease->expires < (leasetime_t) time(NULL)); |
| 204 | } |
| 205 | |
| 206 | /* Find the first lease that matches MAC, NULL if no match */ |
| 207 | static struct dyn_lease *find_lease_by_mac(const uint8_t *mac) |
| 208 | { |
| 209 | unsigned i; |
| 210 | |
| 211 | for (i = 0; i < server_config.max_leases; i++) |
| 212 | if (memcmp(g_leases[i].lease_mac, mac, 6) == 0) |
| 213 | return &g_leases[i]; |
| 214 | |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | /* Find the first lease that matches IP, NULL is no match */ |
| 219 | static struct dyn_lease *find_lease_by_nip(uint32_t nip) |
| 220 | { |
| 221 | unsigned i; |
| 222 | |
| 223 | for (i = 0; i < server_config.max_leases; i++) |
| 224 | if (g_leases[i].lease_nip == nip) |
| 225 | return &g_leases[i]; |
| 226 | |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | /* Check if the IP is taken; if it is, add it to the lease table */ |
| 231 | static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigned arpping_ms) |
| 232 | { |
| 233 | struct in_addr temp; |
| 234 | int r; |
| 235 | |
| 236 | r = arpping(nip, safe_mac, |
| 237 | server_config.server_nip, |
| 238 | server_config.server_mac, |
| 239 | server_config.interface, |
| 240 | arpping_ms); |
| 241 | if (r) |
| 242 | return r; |
| 243 | |
| 244 | temp.s_addr = nip; |
| 245 | bb_error_msg("%s belongs to someone, reserving it for %u seconds", |
| 246 | inet_ntoa(temp), (unsigned)server_config.conflict_time); |
| 247 | add_lease(NULL, nip, server_config.conflict_time, NULL, 0); |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* Find a new usable (we think) address */ |
| 252 | static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arpping_ms) |
| 253 | { |
| 254 | uint32_t addr; |
| 255 | struct dyn_lease *oldest_lease = NULL; |
| 256 | |
| 257 | #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC |
| 258 | uint32_t stop; |
| 259 | unsigned i, hash; |
| 260 | |
| 261 | /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good |
| 262 | * dispersal even with similarly-valued "strings". |
| 263 | */ |
| 264 | hash = 0; |
| 265 | for (i = 0; i < 6; i++) |
| 266 | hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash; |
| 267 | |
| 268 | /* pick a seed based on hwaddr then iterate until we find a free address. */ |
| 269 | addr = server_config.start_ip |
| 270 | + (hash % (1 + server_config.end_ip - server_config.start_ip)); |
| 271 | stop = addr; |
| 272 | #else |
| 273 | addr = server_config.start_ip; |
| 274 | #define stop (server_config.end_ip + 1) |
| 275 | #endif |
| 276 | do { |
| 277 | uint32_t nip; |
| 278 | struct dyn_lease *lease; |
| 279 | |
| 280 | /* ie, 192.168.55.0 */ |
| 281 | if ((addr & 0xff) == 0) |
| 282 | goto next_addr; |
| 283 | /* ie, 192.168.55.255 */ |
| 284 | if ((addr & 0xff) == 0xff) |
| 285 | goto next_addr; |
| 286 | nip = htonl(addr); |
| 287 | /* skip our own address */ |
| 288 | if (nip == server_config.server_nip) |
| 289 | goto next_addr; |
| 290 | /* is this a static lease addr? */ |
| 291 | if (is_nip_reserved(server_config.static_leases, nip)) |
| 292 | goto next_addr; |
| 293 | |
| 294 | lease = find_lease_by_nip(nip); |
| 295 | if (!lease) { |
| 296 | //TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping! |
| 297 | if (nobody_responds_to_arp(nip, safe_mac, arpping_ms)) |
| 298 | return nip; |
| 299 | } else { |
| 300 | if (!oldest_lease || lease->expires < oldest_lease->expires) |
| 301 | oldest_lease = lease; |
| 302 | } |
| 303 | |
| 304 | next_addr: |
| 305 | addr++; |
| 306 | #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC |
| 307 | if (addr > server_config.end_ip) |
| 308 | addr = server_config.start_ip; |
| 309 | #endif |
| 310 | } while (addr != stop); |
| 311 | |
| 312 | if (oldest_lease |
| 313 | && is_expired_lease(oldest_lease) |
| 314 | && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac, arpping_ms) |
| 315 | ) { |
| 316 | return oldest_lease->lease_nip; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Denys Vlasenko | d2ae66c | 2016-10-04 00:43:14 +0200 | [diff] [blame] | 322 | /* On these functions, make sure your datatype matches */ |
Denys Vlasenko | 2bf2931 | 2016-10-04 00:37:50 +0200 | [diff] [blame] | 323 | static int FAST_FUNC read_str(const char *line, void *arg) |
| 324 | { |
| 325 | char **dest = arg; |
| 326 | |
| 327 | free(*dest); |
| 328 | *dest = xstrdup(line); |
| 329 | return 1; |
| 330 | } |
| 331 | |
| 332 | static int FAST_FUNC read_u32(const char *line, void *arg) |
| 333 | { |
| 334 | *(uint32_t*)arg = bb_strtou32(line, NULL, 10); |
| 335 | return errno == 0; |
| 336 | } |
| 337 | |
| 338 | static int FAST_FUNC read_staticlease(const char *const_line, void *arg) |
| 339 | { |
| 340 | char *line; |
| 341 | char *mac_string; |
| 342 | char *ip_string; |
| 343 | struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */ |
| 344 | uint32_t nip; |
| 345 | |
| 346 | /* Read mac */ |
| 347 | line = (char *) const_line; |
| 348 | mac_string = strtok_r(line, " \t", &line); |
| 349 | if (!mac_string || !ether_aton_r(mac_string, &mac_bytes)) |
| 350 | return 0; |
| 351 | |
| 352 | /* Read ip */ |
| 353 | ip_string = strtok_r(NULL, " \t", &line); |
| 354 | if (!ip_string || !udhcp_str2nip(ip_string, &nip)) |
| 355 | return 0; |
| 356 | |
| 357 | add_static_lease(arg, (uint8_t*) &mac_bytes, nip); |
| 358 | |
| 359 | log_static_leases(arg); |
| 360 | |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | struct config_keyword { |
| 365 | const char *keyword; |
| 366 | int (*handler)(const char *line, void *var) FAST_FUNC; |
| 367 | unsigned ofs; |
| 368 | const char *def; |
| 369 | }; |
| 370 | |
| 371 | #define OFS(field) offsetof(struct server_config_t, field) |
| 372 | |
| 373 | static const struct config_keyword keywords[] = { |
| 374 | /* keyword handler variable address default */ |
| 375 | {"start" , udhcp_str2nip , OFS(start_ip ), "192.168.0.20"}, |
| 376 | {"end" , udhcp_str2nip , OFS(end_ip ), "192.168.0.254"}, |
| 377 | {"interface" , read_str , OFS(interface ), "eth0"}, |
| 378 | /* Avoid "max_leases value not sane" warning by setting default |
| 379 | * to default_end_ip - default_start_ip + 1: */ |
| 380 | {"max_leases" , read_u32 , OFS(max_leases ), "235"}, |
| 381 | {"auto_time" , read_u32 , OFS(auto_time ), "7200"}, |
| 382 | {"decline_time" , read_u32 , OFS(decline_time ), "3600"}, |
| 383 | {"conflict_time", read_u32 , OFS(conflict_time), "3600"}, |
| 384 | {"offer_time" , read_u32 , OFS(offer_time ), "60"}, |
| 385 | {"min_lease" , read_u32 , OFS(min_lease_sec), "60"}, |
| 386 | {"lease_file" , read_str , OFS(lease_file ), LEASES_FILE}, |
| 387 | {"pidfile" , read_str , OFS(pidfile ), "/var/run/udhcpd.pid"}, |
| 388 | {"siaddr" , udhcp_str2nip , OFS(siaddr_nip ), "0.0.0.0"}, |
| 389 | /* keywords with no defaults must be last! */ |
| 390 | {"option" , udhcp_str2optset, OFS(options ), ""}, |
| 391 | {"opt" , udhcp_str2optset, OFS(options ), ""}, |
| 392 | {"notify_file" , read_str , OFS(notify_file ), NULL}, |
| 393 | {"sname" , read_str , OFS(sname ), NULL}, |
| 394 | {"boot_file" , read_str , OFS(boot_file ), NULL}, |
| 395 | {"static_lease" , read_staticlease, OFS(static_leases), ""}, |
| 396 | }; |
| 397 | enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; |
| 398 | |
| 399 | static NOINLINE void read_config(const char *file) |
| 400 | { |
| 401 | parser_t *parser; |
| 402 | const struct config_keyword *k; |
| 403 | unsigned i; |
| 404 | char *token[2]; |
| 405 | |
| 406 | for (i = 0; i < KWS_WITH_DEFAULTS; i++) |
| 407 | keywords[i].handler(keywords[i].def, (char*)&server_config + keywords[i].ofs); |
| 408 | |
| 409 | parser = config_open(file); |
| 410 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { |
| 411 | for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) { |
| 412 | if (strcasecmp(token[0], k->keyword) == 0) { |
| 413 | if (!k->handler(token[1], (char*)&server_config + k->ofs)) { |
| 414 | bb_error_msg("can't parse line %u in %s", |
| 415 | parser->lineno, file); |
| 416 | /* reset back to the default value */ |
| 417 | k->handler(k->def, (char*)&server_config + k->ofs); |
| 418 | } |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | config_close(parser); |
| 424 | |
| 425 | server_config.start_ip = ntohl(server_config.start_ip); |
| 426 | server_config.end_ip = ntohl(server_config.end_ip); |
| 427 | } |
| 428 | |
| 429 | static void write_leases(void) |
| 430 | { |
| 431 | int fd; |
| 432 | unsigned i; |
| 433 | leasetime_t curr; |
| 434 | int64_t written_at; |
| 435 | |
| 436 | fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); |
| 437 | if (fd < 0) |
| 438 | return; |
| 439 | |
| 440 | curr = written_at = time(NULL); |
| 441 | |
| 442 | written_at = SWAP_BE64(written_at); |
| 443 | full_write(fd, &written_at, sizeof(written_at)); |
| 444 | |
| 445 | for (i = 0; i < server_config.max_leases; i++) { |
| 446 | leasetime_t tmp_time; |
| 447 | |
| 448 | if (g_leases[i].lease_nip == 0) |
| 449 | continue; |
| 450 | |
| 451 | /* Screw with the time in the struct, for easier writing */ |
| 452 | tmp_time = g_leases[i].expires; |
| 453 | |
| 454 | g_leases[i].expires -= curr; |
| 455 | if ((signed_leasetime_t) g_leases[i].expires < 0) |
| 456 | g_leases[i].expires = 0; |
| 457 | g_leases[i].expires = htonl(g_leases[i].expires); |
| 458 | |
| 459 | /* No error check. If the file gets truncated, |
| 460 | * we lose some leases on restart. Oh well. */ |
| 461 | full_write(fd, &g_leases[i], sizeof(g_leases[i])); |
| 462 | |
| 463 | /* Then restore it when done */ |
| 464 | g_leases[i].expires = tmp_time; |
| 465 | } |
| 466 | close(fd); |
| 467 | |
| 468 | if (server_config.notify_file) { |
| 469 | char *argv[3]; |
| 470 | argv[0] = server_config.notify_file; |
| 471 | argv[1] = server_config.lease_file; |
| 472 | argv[2] = NULL; |
| 473 | spawn_and_wait(argv); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | static NOINLINE void read_leases(const char *file) |
| 478 | { |
| 479 | struct dyn_lease lease; |
| 480 | int64_t written_at, time_passed; |
| 481 | int fd; |
| 482 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 483 | unsigned i = 0; |
| 484 | #endif |
| 485 | |
| 486 | fd = open_or_warn(file, O_RDONLY); |
| 487 | if (fd < 0) |
| 488 | return; |
| 489 | |
| 490 | if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) |
| 491 | goto ret; |
| 492 | written_at = SWAP_BE64(written_at); |
| 493 | |
| 494 | time_passed = time(NULL) - written_at; |
| 495 | /* Strange written_at, or lease file from old version of udhcpd |
| 496 | * which had no "written_at" field? */ |
| 497 | if ((uint64_t)time_passed > 12 * 60 * 60) |
| 498 | goto ret; |
| 499 | |
| 500 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { |
| 501 | uint32_t y = ntohl(lease.lease_nip); |
| 502 | if (y >= server_config.start_ip && y <= server_config.end_ip) { |
| 503 | signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; |
| 504 | uint32_t static_nip; |
| 505 | |
| 506 | if (expires <= 0) |
| 507 | /* We keep expired leases: add_lease() will add |
| 508 | * a lease with 0 seconds remaining. |
| 509 | * Fewer IP address changes this way for mass reboot scenario. |
| 510 | */ |
| 511 | expires = 0; |
| 512 | |
| 513 | /* Check if there is a different static lease for this IP or MAC */ |
| 514 | static_nip = get_static_nip_by_mac(server_config.static_leases, lease.lease_mac); |
| 515 | if (static_nip) { |
| 516 | /* NB: we do not add lease even if static_nip == lease.lease_nip. |
| 517 | */ |
| 518 | continue; |
| 519 | } |
| 520 | if (is_nip_reserved(server_config.static_leases, lease.lease_nip)) |
| 521 | continue; |
| 522 | |
| 523 | /* NB: add_lease takes "relative time", IOW, |
| 524 | * lease duration, not lease deadline. */ |
| 525 | if (add_lease(lease.lease_mac, lease.lease_nip, |
| 526 | expires, |
| 527 | lease.hostname, sizeof(lease.hostname) |
| 528 | ) == 0 |
| 529 | ) { |
| 530 | bb_error_msg("too many leases while loading %s", file); |
| 531 | break; |
| 532 | } |
| 533 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 534 | i++; |
| 535 | #endif |
| 536 | } |
| 537 | } |
| 538 | log1("read %d leases", i); |
| 539 | ret: |
| 540 | close(fd); |
| 541 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 542 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 543 | /* Send a packet to a specific mac address and ip address by creating our own ip packet */ |
| 544 | static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 545 | { |
| 546 | const uint8_t *chaddr; |
| 547 | uint32_t ciaddr; |
| 548 | |
| 549 | // Was: |
| 550 | //if (force_broadcast) { /* broadcast */ } |
| 551 | //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ } |
| 552 | //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ } |
| 553 | //else { /* unicast to dhcp_pkt->yiaddr */ } |
| 554 | // But this is wrong: yiaddr is _our_ idea what client's IP is |
| 555 | // (for example, from lease file). Client may not know that, |
| 556 | // and may not have UDP socket listening on that IP! |
| 557 | // We should never unicast to dhcp_pkt->yiaddr! |
| 558 | // dhcp_pkt->ciaddr, OTOH, comes from client's request packet, |
| 559 | // and can be used. |
| 560 | |
| 561 | if (force_broadcast |
| 562 | || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 563 | || dhcp_pkt->ciaddr == 0 |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 564 | ) { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 565 | log1("broadcasting packet to client"); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 566 | ciaddr = INADDR_BROADCAST; |
| 567 | chaddr = MAC_BCAST_ADDR; |
| 568 | } else { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 569 | log1("unicasting packet to client ciaddr"); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 570 | ciaddr = dhcp_pkt->ciaddr; |
| 571 | chaddr = dhcp_pkt->chaddr; |
| 572 | } |
| 573 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 574 | udhcp_send_raw_packet(dhcp_pkt, |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 575 | /*src*/ server_config.server_nip, SERVER_PORT, |
| 576 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, |
| 577 | server_config.ifindex); |
| 578 | } |
| 579 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 580 | /* Send a packet to gateway_nip using the kernel ip stack */ |
| 581 | static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt) |
| 582 | { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 583 | log1("forwarding packet to relay"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 584 | |
| 585 | udhcp_send_kernel_packet(dhcp_pkt, |
| 586 | server_config.server_nip, SERVER_PORT, |
| 587 | dhcp_pkt->gateway_nip, SERVER_PORT); |
| 588 | } |
| 589 | |
| 590 | static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 591 | { |
| 592 | if (dhcp_pkt->gateway_nip) |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 593 | send_packet_to_relay(dhcp_pkt); |
| 594 | else |
| 595 | send_packet_to_client(dhcp_pkt, force_broadcast); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type) |
| 599 | { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 600 | /* Sets op, htype, hlen, cookie fields |
| 601 | * and adds DHCP_MESSAGE_TYPE option */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 602 | udhcp_init_header(packet, type); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 603 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 604 | packet->xid = oldpacket->xid; |
| 605 | memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr)); |
| 606 | packet->flags = oldpacket->flags; |
| 607 | packet->gateway_nip = oldpacket->gateway_nip; |
| 608 | packet->ciaddr = oldpacket->ciaddr; |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 609 | udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 610 | } |
| 611 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 612 | /* Fill options field, siaddr_nip, and sname and boot_file fields. |
| 613 | * TODO: teach this code to use overload option. |
| 614 | */ |
| 615 | static void add_server_options(struct dhcp_packet *packet) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 616 | { |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 617 | struct option_set *curr = server_config.options; |
| 618 | |
| 619 | while (curr) { |
| 620 | if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 621 | udhcp_add_binary_option(packet, curr->data); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 622 | curr = curr->next; |
| 623 | } |
| 624 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 625 | packet->siaddr_nip = server_config.siaddr_nip; |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 626 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 627 | if (server_config.sname) |
| 628 | strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1); |
| 629 | if (server_config.boot_file) |
| 630 | strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1); |
| 631 | } |
| 632 | |
| 633 | static uint32_t select_lease_time(struct dhcp_packet *packet) |
| 634 | { |
| 635 | uint32_t lease_time_sec = server_config.max_lease_sec; |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 636 | uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 637 | if (lease_time_opt) { |
| 638 | move_from_unaligned32(lease_time_sec, lease_time_opt); |
| 639 | lease_time_sec = ntohl(lease_time_sec); |
| 640 | if (lease_time_sec > server_config.max_lease_sec) |
| 641 | lease_time_sec = server_config.max_lease_sec; |
| 642 | if (lease_time_sec < server_config.min_lease_sec) |
| 643 | lease_time_sec = server_config.min_lease_sec; |
| 644 | } |
| 645 | return lease_time_sec; |
| 646 | } |
| 647 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 648 | /* We got a DHCP DISCOVER. Send an OFFER. */ |
Denys Vlasenko | 0bb35e1 | 2010-10-21 12:33:10 +0200 | [diff] [blame] | 649 | /* NOINLINE: limit stack usage in caller */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 650 | static NOINLINE void send_offer(struct dhcp_packet *oldpacket, |
| 651 | uint32_t static_lease_nip, |
| 652 | struct dyn_lease *lease, |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 653 | uint8_t *requested_ip_opt, |
| 654 | unsigned arpping_ms) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 655 | { |
| 656 | struct dhcp_packet packet; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 657 | uint32_t lease_time_sec; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 658 | struct in_addr addr; |
| 659 | |
| 660 | init_packet(&packet, oldpacket, DHCPOFFER); |
| 661 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 662 | /* If it is a static lease, use its IP */ |
| 663 | packet.yiaddr = static_lease_nip; |
| 664 | /* Else: */ |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 665 | if (!static_lease_nip) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 666 | /* We have no static lease for client's chaddr */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 667 | uint32_t req_nip; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 668 | const char *p_host_name; |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 669 | |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 670 | if (lease) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 671 | /* We have a dynamic lease for client's chaddr. |
| 672 | * Reuse its IP (even if lease is expired). |
| 673 | * Note that we ignore requested IP in this case. |
| 674 | */ |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 675 | packet.yiaddr = lease->lease_nip; |
| 676 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 677 | /* Or: if client has requested an IP */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 678 | else if (requested_ip_opt != NULL |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 679 | /* (read IP) */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 680 | && (move_from_unaligned32(req_nip, requested_ip_opt), 1) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 681 | /* and the IP is in the lease range */ |
| 682 | && ntohl(req_nip) >= server_config.start_ip |
| 683 | && ntohl(req_nip) <= server_config.end_ip |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 684 | /* and */ |
| 685 | && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */ |
| 686 | || is_expired_lease(lease) /* or is taken, but expired */ |
| 687 | ) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 688 | ) { |
| 689 | packet.yiaddr = req_nip; |
| 690 | } |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 691 | else { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 692 | /* Otherwise, find a free IP */ |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 693 | packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr, arpping_ms); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | if (!packet.yiaddr) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 697 | bb_error_msg("no free IP addresses. OFFER abandoned"); |
| 698 | return; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 699 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 700 | /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 701 | p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 702 | lease = add_lease(packet.chaddr, packet.yiaddr, |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 703 | server_config.offer_time, |
| 704 | p_host_name, |
| 705 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 706 | ); |
| 707 | if (!lease) { |
| 708 | bb_error_msg("no free IP addresses. OFFER abandoned"); |
| 709 | return; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 710 | } |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 711 | } |
| 712 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 713 | lease_time_sec = select_lease_time(oldpacket); |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 714 | udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 715 | add_server_options(&packet); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 716 | |
| 717 | addr.s_addr = packet.yiaddr; |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 718 | bb_error_msg("sending OFFER of %s", inet_ntoa(addr)); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 719 | /* send_packet emits error message itself if it detects failure */ |
| 720 | send_packet(&packet, /*force_bcast:*/ 0); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Denys Vlasenko | 0bb35e1 | 2010-10-21 12:33:10 +0200 | [diff] [blame] | 723 | /* NOINLINE: limit stack usage in caller */ |
| 724 | static NOINLINE void send_NAK(struct dhcp_packet *oldpacket) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 725 | { |
| 726 | struct dhcp_packet packet; |
| 727 | |
| 728 | init_packet(&packet, oldpacket, DHCPNAK); |
| 729 | |
Denys Vlasenko | 16efe19 | 2016-03-30 18:44:52 +0200 | [diff] [blame] | 730 | log1("sending %s", "NAK"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 731 | send_packet(&packet, /*force_bcast:*/ 1); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 732 | } |
| 733 | |
Denys Vlasenko | 0bb35e1 | 2010-10-21 12:33:10 +0200 | [diff] [blame] | 734 | /* NOINLINE: limit stack usage in caller */ |
| 735 | static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 736 | { |
| 737 | struct dhcp_packet packet; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 738 | uint32_t lease_time_sec; |
| 739 | struct in_addr addr; |
| 740 | const char *p_host_name; |
| 741 | |
| 742 | init_packet(&packet, oldpacket, DHCPACK); |
| 743 | packet.yiaddr = yiaddr; |
| 744 | |
| 745 | lease_time_sec = select_lease_time(oldpacket); |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 746 | udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 747 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 748 | add_server_options(&packet); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 749 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 750 | addr.s_addr = yiaddr; |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 751 | bb_error_msg("sending ACK to %s", inet_ntoa(addr)); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 752 | send_packet(&packet, /*force_bcast:*/ 0); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 753 | |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 754 | p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 755 | add_lease(packet.chaddr, packet.yiaddr, |
| 756 | lease_time_sec, |
| 757 | p_host_name, |
| 758 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 |
| 759 | ); |
| 760 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { |
| 761 | /* rewrite the file with leases at every new acceptance */ |
| 762 | write_leases(); |
| 763 | } |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Denys Vlasenko | 0bb35e1 | 2010-10-21 12:33:10 +0200 | [diff] [blame] | 766 | /* NOINLINE: limit stack usage in caller */ |
| 767 | static NOINLINE void send_inform(struct dhcp_packet *oldpacket) |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 768 | { |
| 769 | struct dhcp_packet packet; |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 770 | |
Denys Vlasenko | f8fcc18 | 2010-04-04 22:36:34 +0200 | [diff] [blame] | 771 | /* "If a client has obtained a network address through some other means |
| 772 | * (e.g., manual configuration), it may use a DHCPINFORM request message |
| 773 | * to obtain other local configuration parameters. Servers receiving a |
| 774 | * DHCPINFORM message construct a DHCPACK message with any local |
| 775 | * configuration parameters appropriate for the client without: |
| 776 | * allocating a new address, checking for an existing binding, filling |
| 777 | * in 'yiaddr' or including lease time parameters. The servers SHOULD |
| 778 | * unicast the DHCPACK reply to the address given in the 'ciaddr' field |
| 779 | * of the DHCPINFORM message. |
| 780 | * ... |
| 781 | * The server responds to a DHCPINFORM message by sending a DHCPACK |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 782 | * message directly to the address given in the 'ciaddr' field |
| 783 | * of the DHCPINFORM message. The server MUST NOT send a lease |
| 784 | * expiration time to the client and SHOULD NOT fill in 'yiaddr'." |
| 785 | */ |
Denys Vlasenko | f8fcc18 | 2010-04-04 22:36:34 +0200 | [diff] [blame] | 786 | //TODO: do a few sanity checks: is ciaddr set? |
| 787 | //Better yet: is ciaddr == IP source addr? |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 788 | init_packet(&packet, oldpacket, DHCPACK); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 789 | add_server_options(&packet); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 790 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 791 | send_packet(&packet, /*force_bcast:*/ 0); |
Denys Vlasenko | 8a7c166 | 2010-03-20 03:48:11 +0100 | [diff] [blame] | 792 | } |
| 793 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 794 | int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 795 | int udhcpd_main(int argc UNUSED_PARAM, char **argv) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 796 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 797 | int server_socket = -1, retval, max_sock; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 798 | uint8_t *state; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 799 | unsigned timeout_end; |
| 800 | unsigned num_ips; |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 801 | unsigned opt; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 802 | struct option_set *option; |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 803 | char *str_I = str_I; |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 804 | const char *str_a = "2000"; |
| 805 | unsigned arpping_ms; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 806 | IF_FEATURE_UDHCP_PORT(char *str_P;) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 807 | |
Denys Vlasenko | df70a43 | 2016-04-21 18:54:36 +0200 | [diff] [blame] | 808 | setup_common_bufsiz(); |
| 809 | |
| 810 | IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;) |
| 811 | IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 812 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 813 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 |
| 814 | opt_complementary = "vv"; |
| 815 | #endif |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 816 | opt = getopt32(argv, "fSI:va:" |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 817 | IF_FEATURE_UDHCP_PORT("P:") |
| 818 | , &str_I |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 819 | , &str_a |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 820 | IF_FEATURE_UDHCP_PORT(, &str_P) |
Leonid Lisovskiy | 6c9c0a1 | 2011-10-18 00:35:47 +0200 | [diff] [blame] | 821 | IF_UDHCP_VERBOSE(, &dhcp_verbose) |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 822 | ); |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 823 | if (!(opt & 1)) { /* no -f */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 824 | bb_daemonize_or_rexec(0, argv); |
Denis Vlasenko | a19e649 | 2009-03-11 14:40:00 +0000 | [diff] [blame] | 825 | logmode = LOGMODE_NONE; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 826 | } |
Mike Frysinger | 6db1373 | 2010-06-04 13:24:50 -0400 | [diff] [blame] | 827 | /* update argv after the possible vfork+exec in daemonize */ |
| 828 | argv += optind; |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 829 | if (opt & 2) { /* -S */ |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 830 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 831 | logmode |= LOGMODE_SYSLOG; |
| 832 | } |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 833 | if (opt & 4) { /* -I */ |
| 834 | len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET); |
| 835 | server_config.server_nip = lsa->u.sin.sin_addr.s_addr; |
| 836 | free(lsa); |
| 837 | } |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 838 | #if ENABLE_FEATURE_UDHCP_PORT |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 839 | if (opt & 32) { /* -P */ |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 840 | SERVER_PORT = xatou16(str_P); |
| 841 | CLIENT_PORT = SERVER_PORT + 1; |
| 842 | } |
| 843 | #endif |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 844 | arpping_ms = xatou(str_a); |
| 845 | |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 846 | /* Would rather not do read_config before daemonization - |
| 847 | * otherwise NOMMU machines will parse config twice */ |
Denis Vlasenko | 9f7b92a | 2007-08-15 20:03:36 +0000 | [diff] [blame] | 848 | read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 849 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 850 | /* Make sure fd 0,1,2 are open */ |
| 851 | bb_sanitize_stdio(); |
| 852 | /* Equivalent of doing a fflush after every \n */ |
| 853 | setlinebuf(stdout); |
| 854 | |
| 855 | /* Create pidfile */ |
| 856 | write_pidfile(server_config.pidfile); |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 857 | /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 858 | |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 859 | bb_error_msg("started, v"BB_VER); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 860 | |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 861 | option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME); |
Denys Vlasenko | 2e7aa92 | 2010-03-21 02:22:07 +0100 | [diff] [blame] | 862 | server_config.max_lease_sec = DEFAULT_LEASE_TIME; |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 863 | if (option) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 864 | move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA); |
| 865 | server_config.max_lease_sec = ntohl(server_config.max_lease_sec); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 866 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 867 | |
| 868 | /* Sanity check */ |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 869 | num_ips = server_config.end_ip - server_config.start_ip + 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 870 | if (server_config.max_leases > num_ips) { |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 871 | bb_error_msg("max_leases=%u is too big, setting to %u", |
| 872 | (unsigned)server_config.max_leases, num_ips); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 873 | server_config.max_leases = num_ips; |
| 874 | } |
| 875 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 876 | g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0])); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 877 | read_leases(server_config.lease_file); |
| 878 | |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 879 | if (udhcp_read_interface(server_config.interface, |
| 880 | &server_config.ifindex, |
Denys Vlasenko | e3f5b73 | 2013-03-13 22:27:37 +0100 | [diff] [blame] | 881 | (server_config.server_nip == 0 ? &server_config.server_nip : NULL), |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 882 | server_config.server_mac) |
| 883 | ) { |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 884 | retval = 1; |
| 885 | goto ret; |
| 886 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 887 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 888 | /* Setup the signal pipe */ |
| 889 | udhcp_sp_setup(); |
| 890 | |
Denys Vlasenko | 71045cc | 2012-07-24 17:21:26 +0200 | [diff] [blame] | 891 | continue_with_autotime: |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 892 | timeout_end = monotonic_sec() + server_config.auto_time; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 893 | while (1) { /* loop until universe collapses */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 894 | fd_set rfds; |
| 895 | struct dhcp_packet packet; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 896 | int bytes; |
| 897 | struct timeval tv; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 898 | uint8_t *server_id_opt; |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 899 | uint8_t *requested_ip_opt; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 900 | uint32_t requested_nip = requested_nip; /* for compiler */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 901 | uint32_t static_lease_nip; |
| 902 | struct dyn_lease *lease, fake_lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 903 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 904 | if (server_socket < 0) { |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 905 | server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 906 | server_config.interface); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 907 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 908 | |
| 909 | max_sock = udhcp_sp_fd_set(&rfds, server_socket); |
| 910 | if (server_config.auto_time) { |
Denys Vlasenko | 936c401 | 2015-01-27 21:59:40 +0100 | [diff] [blame] | 911 | /* cast to signed is essential if tv_sec is wider than int */ |
| 912 | tv.tv_sec = (int)(timeout_end - monotonic_sec()); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 913 | tv.tv_usec = 0; |
| 914 | } |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 915 | retval = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 916 | if (!server_config.auto_time || tv.tv_sec > 0) { |
| 917 | retval = select(max_sock + 1, &rfds, NULL, NULL, |
| 918 | server_config.auto_time ? &tv : NULL); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 919 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 920 | if (retval == 0) { |
| 921 | write_leases(); |
Denys Vlasenko | 71045cc | 2012-07-24 17:21:26 +0200 | [diff] [blame] | 922 | goto continue_with_autotime; |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 923 | } |
| 924 | if (retval < 0 && errno != EINTR) { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 925 | log1("error on select"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 926 | continue; |
| 927 | } |
| 928 | |
| 929 | switch (udhcp_sp_read(&rfds)) { |
| 930 | case SIGUSR1: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 931 | bb_error_msg("received %s", "SIGUSR1"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 932 | write_leases(); |
| 933 | /* why not just reset the timeout, eh */ |
Denys Vlasenko | 71045cc | 2012-07-24 17:21:26 +0200 | [diff] [blame] | 934 | goto continue_with_autotime; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 935 | case SIGTERM: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 936 | bb_error_msg("received %s", "SIGTERM"); |
Denys Vlasenko | 71045cc | 2012-07-24 17:21:26 +0200 | [diff] [blame] | 937 | write_leases(); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 938 | goto ret0; |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 939 | case 0: /* no signal: read a packet */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 940 | break; |
| 941 | default: /* signal or error (probably EINTR): back to select */ |
| 942 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 945 | bytes = udhcp_recv_kernel_packet(&packet, server_socket); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 946 | if (bytes < 0) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 947 | /* bytes can also be -2 ("bad packet data") */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 948 | if (bytes == -1 && errno != EINTR) { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 949 | log1("read error: %s, reopening socket", strerror(errno)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 950 | close(server_socket); |
| 951 | server_socket = -1; |
| 952 | } |
| 953 | continue; |
| 954 | } |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 955 | if (packet.hlen != 6) { |
| 956 | bb_error_msg("MAC length != 6, ignoring packet"); |
| 957 | continue; |
| 958 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 959 | if (packet.op != BOOTREQUEST) { |
| 960 | bb_error_msg("not a REQUEST, ignoring packet"); |
| 961 | continue; |
| 962 | } |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 963 | state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 964 | if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) { |
| 965 | bb_error_msg("no or bad message type option, ignoring packet"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 966 | continue; |
| 967 | } |
| 968 | |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 969 | /* Get SERVER_ID if present */ |
| 970 | server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID); |
| 971 | if (server_id_opt) { |
Denys Vlasenko | 713d241 | 2010-11-28 21:51:44 +0100 | [diff] [blame] | 972 | uint32_t server_id_network_order; |
| 973 | move_from_unaligned32(server_id_network_order, server_id_opt); |
| 974 | if (server_id_network_order != server_config.server_nip) { |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 975 | /* client talks to somebody else */ |
| 976 | log1("server ID doesn't match, ignoring"); |
| 977 | continue; |
| 978 | } |
| 979 | } |
| 980 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 981 | /* Look for a static/dynamic lease */ |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 982 | static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr); |
| 983 | if (static_lease_nip) { |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 984 | bb_error_msg("found static lease: %x", static_lease_nip); |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 985 | memcpy(&fake_lease.lease_mac, &packet.chaddr, 6); |
Denys Vlasenko | a953987 | 2010-03-20 03:49:27 +0100 | [diff] [blame] | 986 | fake_lease.lease_nip = static_lease_nip; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 987 | fake_lease.expires = 0; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 988 | lease = &fake_lease; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 989 | } else { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 990 | lease = find_lease_by_mac(packet.chaddr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 993 | /* Get REQUESTED_IP if present */ |
| 994 | requested_ip_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP); |
| 995 | if (requested_ip_opt) { |
| 996 | move_from_unaligned32(requested_nip, requested_ip_opt); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 997 | } |
| 998 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 999 | switch (state[0]) { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1000 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1001 | case DHCPDISCOVER: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 1002 | log1("received %s", "DISCOVER"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1003 | |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 1004 | send_offer(&packet, static_lease_nip, lease, requested_ip_opt, arpping_ms); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1005 | break; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 1006 | |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1007 | case DHCPREQUEST: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 1008 | log1("received %s", "REQUEST"); |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 1009 | /* RFC 2131: |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1010 | |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 1011 | o DHCPREQUEST generated during SELECTING state: |
| 1012 | |
| 1013 | Client inserts the address of the selected server in 'server |
| 1014 | identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be |
| 1015 | filled in with the yiaddr value from the chosen DHCPOFFER. |
| 1016 | |
| 1017 | Note that the client may choose to collect several DHCPOFFER |
| 1018 | messages and select the "best" offer. The client indicates its |
| 1019 | selection by identifying the offering server in the DHCPREQUEST |
| 1020 | message. If the client receives no acceptable offers, the client |
| 1021 | may choose to try another DHCPDISCOVER message. Therefore, the |
| 1022 | servers may not receive a specific DHCPREQUEST from which they can |
| 1023 | decide whether or not the client has accepted the offer. |
| 1024 | |
| 1025 | o DHCPREQUEST generated during INIT-REBOOT state: |
| 1026 | |
| 1027 | 'server identifier' MUST NOT be filled in, 'requested IP address' |
| 1028 | option MUST be filled in with client's notion of its previously |
| 1029 | assigned address. 'ciaddr' MUST be zero. The client is seeking to |
| 1030 | verify a previously allocated, cached configuration. Server SHOULD |
| 1031 | send a DHCPNAK message to the client if the 'requested IP address' |
| 1032 | is incorrect, or is on the wrong network. |
| 1033 | |
| 1034 | Determining whether a client in the INIT-REBOOT state is on the |
| 1035 | correct network is done by examining the contents of 'giaddr', the |
| 1036 | 'requested IP address' option, and a database lookup. If the DHCP |
| 1037 | server detects that the client is on the wrong net (i.e., the |
| 1038 | result of applying the local subnet mask or remote subnet mask (if |
| 1039 | 'giaddr' is not zero) to 'requested IP address' option value |
| 1040 | doesn't match reality), then the server SHOULD send a DHCPNAK |
| 1041 | message to the client. |
| 1042 | |
| 1043 | If the network is correct, then the DHCP server should check if |
| 1044 | the client's notion of its IP address is correct. If not, then the |
| 1045 | server SHOULD send a DHCPNAK message to the client. If the DHCP |
| 1046 | server has no record of this client, then it MUST remain silent, |
| 1047 | and MAY output a warning to the network administrator. This |
| 1048 | behavior is necessary for peaceful coexistence of non- |
| 1049 | communicating DHCP servers on the same wire. |
| 1050 | |
| 1051 | If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on |
| 1052 | the same subnet as the server. The server MUST broadcast the |
| 1053 | DHCPNAK message to the 0xffffffff broadcast address because the |
| 1054 | client may not have a correct network address or subnet mask, and |
| 1055 | the client may not be answering ARP requests. |
| 1056 | |
| 1057 | If 'giaddr' is set in the DHCPREQUEST message, the client is on a |
| 1058 | different subnet. The server MUST set the broadcast bit in the |
| 1059 | DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the |
| 1060 | client, because the client may not have a correct network address |
| 1061 | or subnet mask, and the client may not be answering ARP requests. |
| 1062 | |
| 1063 | o DHCPREQUEST generated during RENEWING state: |
| 1064 | |
| 1065 | 'server identifier' MUST NOT be filled in, 'requested IP address' |
| 1066 | option MUST NOT be filled in, 'ciaddr' MUST be filled in with |
| 1067 | client's IP address. In this situation, the client is completely |
| 1068 | configured, and is trying to extend its lease. This message will |
| 1069 | be unicast, so no relay agents will be involved in its |
| 1070 | transmission. Because 'giaddr' is therefore not filled in, the |
| 1071 | DHCP server will trust the value in 'ciaddr', and use it when |
| 1072 | replying to the client. |
| 1073 | |
| 1074 | A client MAY choose to renew or extend its lease prior to T1. The |
| 1075 | server may choose not to extend the lease (as a policy decision by |
| 1076 | the network administrator), but should return a DHCPACK message |
| 1077 | regardless. |
| 1078 | |
| 1079 | o DHCPREQUEST generated during REBINDING state: |
| 1080 | |
| 1081 | 'server identifier' MUST NOT be filled in, 'requested IP address' |
| 1082 | option MUST NOT be filled in, 'ciaddr' MUST be filled in with |
| 1083 | client's IP address. In this situation, the client is completely |
| 1084 | configured, and is trying to extend its lease. This message MUST |
| 1085 | be broadcast to the 0xffffffff IP broadcast address. The DHCP |
| 1086 | server SHOULD check 'ciaddr' for correctness before replying to |
| 1087 | the DHCPREQUEST. |
| 1088 | |
| 1089 | The DHCPREQUEST from a REBINDING client is intended to accommodate |
| 1090 | sites that have multiple DHCP servers and a mechanism for |
| 1091 | maintaining consistency among leases managed by multiple servers. |
| 1092 | A DHCP server MAY extend a client's lease only if it has local |
| 1093 | administrative authority to do so. |
| 1094 | */ |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 1095 | if (!requested_ip_opt) { |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 1096 | requested_nip = packet.ciaddr; |
| 1097 | if (requested_nip == 0) { |
| 1098 | log1("no requested IP and no ciaddr, ignoring"); |
| 1099 | break; |
| 1100 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1101 | } |
| 1102 | if (lease && requested_nip == lease->lease_nip) { |
Denys Vlasenko | 53f72bb | 2010-03-21 06:46:09 +0100 | [diff] [blame] | 1103 | /* client requested or configured IP matches the lease. |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1104 | * ACK it, and bump lease expiration time. */ |
| 1105 | send_ACK(&packet, lease->lease_nip); |
| 1106 | break; |
| 1107 | } |
Denys Vlasenko | 713d241 | 2010-11-28 21:51:44 +0100 | [diff] [blame] | 1108 | /* No lease for this MAC, or lease IP != requested IP */ |
| 1109 | |
| 1110 | if (server_id_opt /* client is in SELECTING state */ |
| 1111 | || requested_ip_opt /* client is in INIT-REBOOT state */ |
| 1112 | ) { |
| 1113 | /* "No, we don't have this IP for you" */ |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1114 | send_NAK(&packet); |
Denys Vlasenko | 713d241 | 2010-11-28 21:51:44 +0100 | [diff] [blame] | 1115 | } /* else: client is in RENEWING or REBINDING, do not answer */ |
| 1116 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1117 | break; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1118 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1119 | case DHCPDECLINE: |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1120 | /* RFC 2131: |
| 1121 | * "If the server receives a DHCPDECLINE message, |
| 1122 | * the client has discovered through some other means |
| 1123 | * that the suggested network address is already |
| 1124 | * in use. The server MUST mark the network address |
| 1125 | * as not available and SHOULD notify the local |
| 1126 | * sysadmin of a possible configuration problem." |
| 1127 | * |
| 1128 | * SERVER_ID must be present, |
| 1129 | * REQUESTED_IP must be present, |
| 1130 | * chaddr must be filled in, |
| 1131 | * ciaddr must be 0 (we do not check this) |
| 1132 | */ |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 1133 | log1("received %s", "DECLINE"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1134 | if (server_id_opt |
Denys Vlasenko | fa5e295 | 2010-11-28 01:10:51 +0100 | [diff] [blame] | 1135 | && requested_ip_opt |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1136 | && lease /* chaddr matches this lease */ |
| 1137 | && requested_nip == lease->lease_nip |
| 1138 | ) { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 1139 | memset(lease->lease_mac, 0, sizeof(lease->lease_mac)); |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 1140 | lease->expires = time(NULL) + server_config.decline_time; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1141 | } |
| 1142 | break; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1143 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1144 | case DHCPRELEASE: |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1145 | /* "Upon receipt of a DHCPRELEASE message, the server |
| 1146 | * marks the network address as not allocated." |
| 1147 | * |
| 1148 | * SERVER_ID must be present, |
| 1149 | * REQUESTED_IP must not be present (we do not check this), |
| 1150 | * chaddr must be filled in, |
| 1151 | * ciaddr must be filled in |
| 1152 | */ |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 1153 | log1("received %s", "RELEASE"); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1154 | if (server_id_opt |
| 1155 | && lease /* chaddr matches this lease */ |
| 1156 | && packet.ciaddr == lease->lease_nip |
| 1157 | ) { |
Denis Vlasenko | 04158e0 | 2009-02-02 10:48:06 +0000 | [diff] [blame] | 1158 | lease->expires = time(NULL); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1159 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1160 | break; |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 1161 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1162 | case DHCPINFORM: |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 1163 | log1("received %s", "INFORM"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1164 | send_inform(&packet); |
| 1165 | break; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1166 | } |
| 1167 | } |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 1168 | ret0: |
| 1169 | retval = 0; |
| 1170 | ret: |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 1171 | /*if (server_config.pidfile) - server_config.pidfile is never NULL */ |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 1172 | remove_pidfile(server_config.pidfile); |
| 1173 | return retval; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1174 | } |