diff options
Diffstat (limited to 'networking/udhcp/dhcpd.c')
-rw-r--r-- | networking/udhcp/dhcpd.c | 186 |
1 files changed, 93 insertions, 93 deletions
diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 058f86bca..3e08ec011 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c | |||
@@ -37,6 +37,8 @@ | |||
37 | //usage: IF_FEATURE_UDHCP_PORT( | 37 | //usage: IF_FEATURE_UDHCP_PORT( |
38 | //usage: "\n -P N Use port N (default 67)" | 38 | //usage: "\n -P N Use port N (default 67)" |
39 | //usage: ) | 39 | //usage: ) |
40 | //usage: "\nSignals:" | ||
41 | //usage: "\n USR1 Update lease file" | ||
40 | 42 | ||
41 | #include <netinet/ether.h> | 43 | #include <netinet/ether.h> |
42 | #include <syslog.h> | 44 | #include <syslog.h> |
@@ -46,7 +48,7 @@ | |||
46 | 48 | ||
47 | /* globals */ | 49 | /* globals */ |
48 | #define g_leases ((struct dyn_lease*)ptr_to_globals) | 50 | #define g_leases ((struct dyn_lease*)ptr_to_globals) |
49 | /* struct server_config_t server_config is in bb_common_bufsiz1 */ | 51 | /* struct server_data_t server_data is in bb_common_bufsiz1 */ |
50 | 52 | ||
51 | struct static_lease { | 53 | struct static_lease { |
52 | struct static_lease *next; | 54 | struct static_lease *next; |
@@ -100,7 +102,7 @@ static void add_static_lease(struct static_lease **st_lease_pp, | |||
100 | /* Find static lease IP by mac */ | 102 | /* Find static lease IP by mac */ |
101 | static uint32_t get_static_nip_by_mac(void *mac) | 103 | static uint32_t get_static_nip_by_mac(void *mac) |
102 | { | 104 | { |
103 | struct static_lease *st_lease = server_config.static_leases; | 105 | struct static_lease *st_lease = server_data.static_leases; |
104 | 106 | ||
105 | while (st_lease) { | 107 | while (st_lease) { |
106 | if (memcmp(st_lease->mac, mac, 6) == 0) | 108 | if (memcmp(st_lease->mac, mac, 6) == 0) |
@@ -113,7 +115,7 @@ static uint32_t get_static_nip_by_mac(void *mac) | |||
113 | 115 | ||
114 | static int is_nip_reserved_as_static(uint32_t nip) | 116 | static int is_nip_reserved_as_static(uint32_t nip) |
115 | { | 117 | { |
116 | struct static_lease *st_lease = server_config.static_leases; | 118 | struct static_lease *st_lease = server_data.static_leases; |
117 | 119 | ||
118 | while (st_lease) { | 120 | while (st_lease) { |
119 | if (st_lease->nip == nip) | 121 | if (st_lease->nip == nip) |
@@ -133,7 +135,7 @@ static struct dyn_lease *oldest_expired_lease(void) | |||
133 | 135 | ||
134 | /* Unexpired leases have g_leases[i].expires >= current time | 136 | /* Unexpired leases have g_leases[i].expires >= current time |
135 | * and therefore can't ever match */ | 137 | * and therefore can't ever match */ |
136 | for (i = 0; i < server_config.max_leases; i++) { | 138 | for (i = 0; i < server_data.max_leases; i++) { |
137 | if (g_leases[i].expires == 0 /* empty entry */ | 139 | if (g_leases[i].expires == 0 /* empty entry */ |
138 | || g_leases[i].expires < oldest_time | 140 | || g_leases[i].expires < oldest_time |
139 | ) { | 141 | ) { |
@@ -151,7 +153,7 @@ static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr) | |||
151 | { | 153 | { |
152 | unsigned i; | 154 | unsigned i; |
153 | 155 | ||
154 | for (i = 0; i < server_config.max_leases; i++) { | 156 | for (i = 0; i < server_data.max_leases; i++) { |
155 | if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0) | 157 | if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0) |
156 | || (yiaddr && g_leases[i].lease_nip == yiaddr) | 158 | || (yiaddr && g_leases[i].lease_nip == yiaddr) |
157 | ) { | 159 | ) { |
@@ -216,7 +218,7 @@ static struct dyn_lease *find_lease_by_mac(const uint8_t *mac) | |||
216 | { | 218 | { |
217 | unsigned i; | 219 | unsigned i; |
218 | 220 | ||
219 | for (i = 0; i < server_config.max_leases; i++) | 221 | for (i = 0; i < server_data.max_leases; i++) |
220 | if (memcmp(g_leases[i].lease_mac, mac, 6) == 0) | 222 | if (memcmp(g_leases[i].lease_mac, mac, 6) == 0) |
221 | return &g_leases[i]; | 223 | return &g_leases[i]; |
222 | 224 | ||
@@ -228,7 +230,7 @@ static struct dyn_lease *find_lease_by_nip(uint32_t nip) | |||
228 | { | 230 | { |
229 | unsigned i; | 231 | unsigned i; |
230 | 232 | ||
231 | for (i = 0; i < server_config.max_leases; i++) | 233 | for (i = 0; i < server_data.max_leases; i++) |
232 | if (g_leases[i].lease_nip == nip) | 234 | if (g_leases[i].lease_nip == nip) |
233 | return &g_leases[i]; | 235 | return &g_leases[i]; |
234 | 236 | ||
@@ -242,17 +244,17 @@ static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigne | |||
242 | int r; | 244 | int r; |
243 | 245 | ||
244 | r = arpping(nip, safe_mac, | 246 | r = arpping(nip, safe_mac, |
245 | server_config.server_nip, | 247 | server_data.server_nip, |
246 | server_config.server_mac, | 248 | server_data.server_mac, |
247 | server_config.interface, | 249 | server_data.interface, |
248 | arpping_ms); | 250 | arpping_ms); |
249 | if (r) | 251 | if (r) |
250 | return r; | 252 | return r; |
251 | 253 | ||
252 | temp.s_addr = nip; | 254 | temp.s_addr = nip; |
253 | bb_info_msg("%s belongs to someone, reserving it for %u seconds", | 255 | bb_info_msg("%s belongs to someone, reserving it for %u seconds", |
254 | inet_ntoa(temp), (unsigned)server_config.conflict_time); | 256 | inet_ntoa(temp), (unsigned)server_data.conflict_time); |
255 | add_lease(NULL, nip, server_config.conflict_time, NULL, 0); | 257 | add_lease(NULL, nip, server_data.conflict_time, NULL, 0); |
256 | return 0; | 258 | return 0; |
257 | } | 259 | } |
258 | 260 | ||
@@ -274,12 +276,12 @@ static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arppi | |||
274 | hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash; | 276 | hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash; |
275 | 277 | ||
276 | /* pick a seed based on hwaddr then iterate until we find a free address. */ | 278 | /* pick a seed based on hwaddr then iterate until we find a free address. */ |
277 | addr = server_config.start_ip | 279 | addr = server_data.start_ip |
278 | + (hash % (1 + server_config.end_ip - server_config.start_ip)); | 280 | + (hash % (1 + server_data.end_ip - server_data.start_ip)); |
279 | stop = addr; | 281 | stop = addr; |
280 | #else | 282 | #else |
281 | addr = server_config.start_ip; | 283 | addr = server_data.start_ip; |
282 | #define stop (server_config.end_ip + 1) | 284 | #define stop (server_data.end_ip + 1) |
283 | #endif | 285 | #endif |
284 | do { | 286 | do { |
285 | uint32_t nip; | 287 | uint32_t nip; |
@@ -293,7 +295,7 @@ static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arppi | |||
293 | goto next_addr; | 295 | goto next_addr; |
294 | nip = htonl(addr); | 296 | nip = htonl(addr); |
295 | /* skip our own address */ | 297 | /* skip our own address */ |
296 | if (nip == server_config.server_nip) | 298 | if (nip == server_data.server_nip) |
297 | goto next_addr; | 299 | goto next_addr; |
298 | /* is this a static lease addr? */ | 300 | /* is this a static lease addr? */ |
299 | if (is_nip_reserved_as_static(nip)) | 301 | if (is_nip_reserved_as_static(nip)) |
@@ -312,8 +314,8 @@ static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arppi | |||
312 | next_addr: | 314 | next_addr: |
313 | addr++; | 315 | addr++; |
314 | #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC | 316 | #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC |
315 | if (addr > server_config.end_ip) | 317 | if (addr > server_data.end_ip) |
316 | addr = server_config.start_ip; | 318 | addr = server_data.start_ip; |
317 | #endif | 319 | #endif |
318 | } while (addr != stop); | 320 | } while (addr != stop); |
319 | 321 | ||
@@ -386,7 +388,7 @@ struct config_keyword { | |||
386 | const char *def; | 388 | const char *def; |
387 | }; | 389 | }; |
388 | 390 | ||
389 | #define OFS(field) offsetof(struct server_config_t, field) | 391 | #define OFS(field) offsetof(struct server_data_t, field) |
390 | 392 | ||
391 | static const struct config_keyword keywords[] = { | 393 | static const struct config_keyword keywords[] = { |
392 | /* keyword handler variable address default */ | 394 | /* keyword handler variable address default */ |
@@ -422,17 +424,17 @@ static NOINLINE void read_config(const char *file) | |||
422 | char *token[2]; | 424 | char *token[2]; |
423 | 425 | ||
424 | for (i = 0; i < KWS_WITH_DEFAULTS; i++) | 426 | for (i = 0; i < KWS_WITH_DEFAULTS; i++) |
425 | keywords[i].handler(keywords[i].def, (char*)&server_config + keywords[i].ofs); | 427 | keywords[i].handler(keywords[i].def, (char*)&server_data + keywords[i].ofs); |
426 | 428 | ||
427 | parser = config_open(file); | 429 | parser = config_open(file); |
428 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { | 430 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { |
429 | for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) { | 431 | for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) { |
430 | if (strcasecmp(token[0], k->keyword) == 0) { | 432 | if (strcasecmp(token[0], k->keyword) == 0) { |
431 | if (!k->handler(token[1], (char*)&server_config + k->ofs)) { | 433 | if (!k->handler(token[1], (char*)&server_data + k->ofs)) { |
432 | bb_error_msg("can't parse line %u in %s", | 434 | bb_error_msg("can't parse line %u in %s", |
433 | parser->lineno, file); | 435 | parser->lineno, file); |
434 | /* reset back to the default value */ | 436 | /* reset back to the default value */ |
435 | k->handler(k->def, (char*)&server_config + k->ofs); | 437 | k->handler(k->def, (char*)&server_data + k->ofs); |
436 | } | 438 | } |
437 | break; | 439 | break; |
438 | } | 440 | } |
@@ -440,8 +442,8 @@ static NOINLINE void read_config(const char *file) | |||
440 | } | 442 | } |
441 | config_close(parser); | 443 | config_close(parser); |
442 | 444 | ||
443 | server_config.start_ip = ntohl(server_config.start_ip); | 445 | server_data.start_ip = ntohl(server_data.start_ip); |
444 | server_config.end_ip = ntohl(server_config.end_ip); | 446 | server_data.end_ip = ntohl(server_data.end_ip); |
445 | } | 447 | } |
446 | 448 | ||
447 | static void write_leases(void) | 449 | static void write_leases(void) |
@@ -451,7 +453,7 @@ static void write_leases(void) | |||
451 | leasetime_t curr; | 453 | leasetime_t curr; |
452 | int64_t written_at; | 454 | int64_t written_at; |
453 | 455 | ||
454 | fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); | 456 | fd = open_or_warn(server_data.lease_file, O_WRONLY|O_CREAT|O_TRUNC); |
455 | if (fd < 0) | 457 | if (fd < 0) |
456 | return; | 458 | return; |
457 | 459 | ||
@@ -460,7 +462,7 @@ static void write_leases(void) | |||
460 | written_at = SWAP_BE64(written_at); | 462 | written_at = SWAP_BE64(written_at); |
461 | full_write(fd, &written_at, sizeof(written_at)); | 463 | full_write(fd, &written_at, sizeof(written_at)); |
462 | 464 | ||
463 | for (i = 0; i < server_config.max_leases; i++) { | 465 | for (i = 0; i < server_data.max_leases; i++) { |
464 | leasetime_t tmp_time; | 466 | leasetime_t tmp_time; |
465 | 467 | ||
466 | if (g_leases[i].lease_nip == 0) | 468 | if (g_leases[i].lease_nip == 0) |
@@ -483,10 +485,10 @@ static void write_leases(void) | |||
483 | } | 485 | } |
484 | close(fd); | 486 | close(fd); |
485 | 487 | ||
486 | if (server_config.notify_file) { | 488 | if (server_data.notify_file) { |
487 | char *argv[3]; | 489 | char *argv[3]; |
488 | argv[0] = server_config.notify_file; | 490 | argv[0] = server_data.notify_file; |
489 | argv[1] = server_config.lease_file; | 491 | argv[1] = server_data.lease_file; |
490 | argv[2] = NULL; | 492 | argv[2] = NULL; |
491 | spawn_and_wait(argv); | 493 | spawn_and_wait(argv); |
492 | } | 494 | } |
@@ -517,7 +519,7 @@ static NOINLINE void read_leases(const char *file) | |||
517 | 519 | ||
518 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { | 520 | while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { |
519 | uint32_t y = ntohl(lease.lease_nip); | 521 | uint32_t y = ntohl(lease.lease_nip); |
520 | if (y >= server_config.start_ip && y <= server_config.end_ip) { | 522 | if (y >= server_data.start_ip && y <= server_data.end_ip) { |
521 | signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; | 523 | signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; |
522 | uint32_t static_nip; | 524 | uint32_t static_nip; |
523 | 525 | ||
@@ -580,28 +582,28 @@ static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadc | |||
580 | || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) | 582 | || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) |
581 | || dhcp_pkt->ciaddr == 0 | 583 | || dhcp_pkt->ciaddr == 0 |
582 | ) { | 584 | ) { |
583 | log1("broadcasting packet to client"); | 585 | log1s("broadcasting packet to client"); |
584 | ciaddr = INADDR_BROADCAST; | 586 | ciaddr = INADDR_BROADCAST; |
585 | chaddr = MAC_BCAST_ADDR; | 587 | chaddr = MAC_BCAST_ADDR; |
586 | } else { | 588 | } else { |
587 | log1("unicasting packet to client ciaddr"); | 589 | log1s("unicasting packet to client ciaddr"); |
588 | ciaddr = dhcp_pkt->ciaddr; | 590 | ciaddr = dhcp_pkt->ciaddr; |
589 | chaddr = dhcp_pkt->chaddr; | 591 | chaddr = dhcp_pkt->chaddr; |
590 | } | 592 | } |
591 | 593 | ||
592 | udhcp_send_raw_packet(dhcp_pkt, | 594 | udhcp_send_raw_packet(dhcp_pkt, |
593 | /*src*/ server_config.server_nip, SERVER_PORT, | 595 | /*src*/ server_data.server_nip, SERVER_PORT, |
594 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, | 596 | /*dst*/ ciaddr, CLIENT_PORT, chaddr, |
595 | server_config.ifindex); | 597 | server_data.ifindex); |
596 | } | 598 | } |
597 | 599 | ||
598 | /* Send a packet to gateway_nip using the kernel ip stack */ | 600 | /* Send a packet to gateway_nip using the kernel ip stack */ |
599 | static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt) | 601 | static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt) |
600 | { | 602 | { |
601 | log1("forwarding packet to relay"); | 603 | log1s("forwarding packet to relay"); |
602 | 604 | ||
603 | udhcp_send_kernel_packet(dhcp_pkt, | 605 | udhcp_send_kernel_packet(dhcp_pkt, |
604 | server_config.server_nip, SERVER_PORT, | 606 | server_data.server_nip, SERVER_PORT, |
605 | dhcp_pkt->gateway_nip, SERVER_PORT); | 607 | dhcp_pkt->gateway_nip, SERVER_PORT); |
606 | } | 608 | } |
607 | 609 | ||
@@ -633,7 +635,7 @@ static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacke | |||
633 | packet->flags = oldpacket->flags; | 635 | packet->flags = oldpacket->flags; |
634 | packet->gateway_nip = oldpacket->gateway_nip; | 636 | packet->gateway_nip = oldpacket->gateway_nip; |
635 | packet->ciaddr = oldpacket->ciaddr; | 637 | packet->ciaddr = oldpacket->ciaddr; |
636 | udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip); | 638 | udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_data.server_nip); |
637 | } | 639 | } |
638 | 640 | ||
639 | /* Fill options field, siaddr_nip, and sname and boot_file fields. | 641 | /* Fill options field, siaddr_nip, and sname and boot_file fields. |
@@ -646,7 +648,7 @@ static void add_server_options(struct dhcp_packet *packet) | |||
646 | 648 | ||
647 | client_hostname_opt = NULL; | 649 | client_hostname_opt = NULL; |
648 | if (packet->yiaddr) { /* if we aren't from send_inform()... */ | 650 | if (packet->yiaddr) { /* if we aren't from send_inform()... */ |
649 | struct static_lease *st_lease = server_config.static_leases; | 651 | struct static_lease *st_lease = server_data.static_leases; |
650 | while (st_lease) { | 652 | while (st_lease) { |
651 | if (st_lease->nip == packet->yiaddr) { | 653 | if (st_lease->nip == packet->yiaddr) { |
652 | if (st_lease->opt[0] != 0) | 654 | if (st_lease->opt[0] != 0) |
@@ -657,7 +659,7 @@ static void add_server_options(struct dhcp_packet *packet) | |||
657 | } | 659 | } |
658 | } | 660 | } |
659 | 661 | ||
660 | config_opts = server_config.options; | 662 | config_opts = server_data.options; |
661 | while (config_opts) { | 663 | while (config_opts) { |
662 | if (config_opts->data[OPT_CODE] != DHCP_LEASE_TIME) { | 664 | if (config_opts->data[OPT_CODE] != DHCP_LEASE_TIME) { |
663 | /* ^^^^ | 665 | /* ^^^^ |
@@ -684,25 +686,25 @@ static void add_server_options(struct dhcp_packet *packet) | |||
684 | if (client_hostname_opt) | 686 | if (client_hostname_opt) |
685 | udhcp_add_binary_option(packet, client_hostname_opt); | 687 | udhcp_add_binary_option(packet, client_hostname_opt); |
686 | 688 | ||
687 | packet->siaddr_nip = server_config.siaddr_nip; | 689 | packet->siaddr_nip = server_data.siaddr_nip; |
688 | 690 | ||
689 | if (server_config.sname) | 691 | if (server_data.sname) |
690 | strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1); | 692 | strncpy((char*)packet->sname, server_data.sname, sizeof(packet->sname) - 1); |
691 | if (server_config.boot_file) | 693 | if (server_data.boot_file) |
692 | strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1); | 694 | strncpy((char*)packet->file, server_data.boot_file, sizeof(packet->file) - 1); |
693 | } | 695 | } |
694 | 696 | ||
695 | static uint32_t select_lease_time(struct dhcp_packet *packet) | 697 | static uint32_t select_lease_time(struct dhcp_packet *packet) |
696 | { | 698 | { |
697 | uint32_t lease_time_sec = server_config.max_lease_sec; | 699 | uint32_t lease_time_sec = server_data.max_lease_sec; |
698 | uint8_t *lease_time_opt = udhcp_get_option32(packet, DHCP_LEASE_TIME); | 700 | uint8_t *lease_time_opt = udhcp_get_option32(packet, DHCP_LEASE_TIME); |
699 | if (lease_time_opt) { | 701 | if (lease_time_opt) { |
700 | move_from_unaligned32(lease_time_sec, lease_time_opt); | 702 | move_from_unaligned32(lease_time_sec, lease_time_opt); |
701 | lease_time_sec = ntohl(lease_time_sec); | 703 | lease_time_sec = ntohl(lease_time_sec); |
702 | if (lease_time_sec > server_config.max_lease_sec) | 704 | if (lease_time_sec > server_data.max_lease_sec) |
703 | lease_time_sec = server_config.max_lease_sec; | 705 | lease_time_sec = server_data.max_lease_sec; |
704 | if (lease_time_sec < server_config.min_lease_sec) | 706 | if (lease_time_sec < server_data.min_lease_sec) |
705 | lease_time_sec = server_config.min_lease_sec; | 707 | lease_time_sec = server_data.min_lease_sec; |
706 | } | 708 | } |
707 | return lease_time_sec; | 709 | return lease_time_sec; |
708 | } | 710 | } |
@@ -737,8 +739,8 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket, | |||
737 | /* Or: if client has requested an IP */ | 739 | /* Or: if client has requested an IP */ |
738 | else if (requested_nip != 0 | 740 | else if (requested_nip != 0 |
739 | /* and the IP is in the lease range */ | 741 | /* and the IP is in the lease range */ |
740 | && ntohl(requested_nip) >= server_config.start_ip | 742 | && ntohl(requested_nip) >= server_data.start_ip |
741 | && ntohl(requested_nip) <= server_config.end_ip | 743 | && ntohl(requested_nip) <= server_data.end_ip |
742 | /* and */ | 744 | /* and */ |
743 | && ( !(lease = find_lease_by_nip(requested_nip)) /* is not already taken */ | 745 | && ( !(lease = find_lease_by_nip(requested_nip)) /* is not already taken */ |
744 | || is_expired_lease(lease) /* or is taken, but expired */ | 746 | || is_expired_lease(lease) /* or is taken, but expired */ |
@@ -752,18 +754,18 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket, | |||
752 | } | 754 | } |
753 | 755 | ||
754 | if (!packet.yiaddr) { | 756 | if (!packet.yiaddr) { |
755 | bb_error_msg("no free IP addresses. OFFER abandoned"); | 757 | bb_simple_error_msg("no free IP addresses. OFFER abandoned"); |
756 | return; | 758 | return; |
757 | } | 759 | } |
758 | /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */ | 760 | /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */ |
759 | p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); | 761 | p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); |
760 | lease = add_lease(packet.chaddr, packet.yiaddr, | 762 | lease = add_lease(packet.chaddr, packet.yiaddr, |
761 | server_config.offer_time, | 763 | server_data.offer_time, |
762 | p_host_name, | 764 | p_host_name, |
763 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 | 765 | p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 |
764 | ); | 766 | ); |
765 | if (!lease) { | 767 | if (!lease) { |
766 | bb_error_msg("no free IP addresses. OFFER abandoned"); | 768 | bb_simple_error_msg("no free IP addresses. OFFER abandoned"); |
767 | return; | 769 | return; |
768 | } | 770 | } |
769 | } | 771 | } |
@@ -863,6 +865,10 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
863 | IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;) | 865 | IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;) |
864 | IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) | 866 | IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;) |
865 | 867 | ||
868 | /* Make sure fd 0,1,2 are open */ | ||
869 | /* Setup the signal pipe on fds 3,4 - must be before openlog() */ | ||
870 | udhcp_sp_setup(); | ||
871 | |||
866 | opt = getopt32(argv, "^" | 872 | opt = getopt32(argv, "^" |
867 | "fSI:va:"IF_FEATURE_UDHCP_PORT("P:") | 873 | "fSI:va:"IF_FEATURE_UDHCP_PORT("P:") |
868 | "\0" | 874 | "\0" |
@@ -886,7 +892,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
886 | } | 892 | } |
887 | if (opt & 4) { /* -I */ | 893 | if (opt & 4) { /* -I */ |
888 | len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET); | 894 | len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET); |
889 | server_config.server_nip = lsa->u.sin.sin_addr.s_addr; | 895 | server_data.server_nip = lsa->u.sin.sin_addr.s_addr; |
890 | free(lsa); | 896 | free(lsa); |
891 | } | 897 | } |
892 | #if ENABLE_FEATURE_UDHCP_PORT | 898 | #if ENABLE_FEATURE_UDHCP_PORT |
@@ -901,52 +907,46 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
901 | * otherwise NOMMU machines will parse config twice */ | 907 | * otherwise NOMMU machines will parse config twice */ |
902 | read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE); | 908 | read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE); |
903 | /* prevent poll timeout overflow */ | 909 | /* prevent poll timeout overflow */ |
904 | if (server_config.auto_time > INT_MAX / 1000) | 910 | if (server_data.auto_time > INT_MAX / 1000) |
905 | server_config.auto_time = INT_MAX / 1000; | 911 | server_data.auto_time = INT_MAX / 1000; |
906 | |||
907 | /* Make sure fd 0,1,2 are open */ | ||
908 | bb_sanitize_stdio(); | ||
909 | 912 | ||
910 | /* Create pidfile */ | 913 | /* Create pidfile */ |
911 | write_pidfile(server_config.pidfile); | 914 | write_pidfile(server_data.pidfile); |
912 | /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */ | 915 | /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */ |
913 | 916 | ||
914 | bb_info_msg("started, v"BB_VER); | 917 | bb_simple_info_msg("started, v"BB_VER); |
915 | 918 | ||
916 | option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME); | 919 | option = udhcp_find_option(server_data.options, DHCP_LEASE_TIME); |
917 | server_config.max_lease_sec = DEFAULT_LEASE_TIME; | 920 | server_data.max_lease_sec = DEFAULT_LEASE_TIME; |
918 | if (option) { | 921 | if (option) { |
919 | move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA); | 922 | move_from_unaligned32(server_data.max_lease_sec, option->data + OPT_DATA); |
920 | server_config.max_lease_sec = ntohl(server_config.max_lease_sec); | 923 | server_data.max_lease_sec = ntohl(server_data.max_lease_sec); |
921 | } | 924 | } |
922 | 925 | ||
923 | /* Sanity check */ | 926 | /* Sanity check */ |
924 | num_ips = server_config.end_ip - server_config.start_ip + 1; | 927 | num_ips = server_data.end_ip - server_data.start_ip + 1; |
925 | if (server_config.max_leases > num_ips) { | 928 | if (server_data.max_leases > num_ips) { |
926 | bb_error_msg("max_leases=%u is too big, setting to %u", | 929 | bb_error_msg("max_leases=%u is too big, setting to %u", |
927 | (unsigned)server_config.max_leases, num_ips); | 930 | (unsigned)server_data.max_leases, num_ips); |
928 | server_config.max_leases = num_ips; | 931 | server_data.max_leases = num_ips; |
929 | } | 932 | } |
930 | 933 | ||
931 | /* this sets g_leases */ | 934 | /* this sets g_leases */ |
932 | SET_PTR_TO_GLOBALS(xzalloc(server_config.max_leases * sizeof(g_leases[0]))); | 935 | SET_PTR_TO_GLOBALS(xzalloc(server_data.max_leases * sizeof(g_leases[0]))); |
933 | 936 | ||
934 | read_leases(server_config.lease_file); | 937 | read_leases(server_data.lease_file); |
935 | 938 | ||
936 | if (udhcp_read_interface(server_config.interface, | 939 | if (udhcp_read_interface(server_data.interface, |
937 | &server_config.ifindex, | 940 | &server_data.ifindex, |
938 | (server_config.server_nip == 0 ? &server_config.server_nip : NULL), | 941 | (server_data.server_nip == 0 ? &server_data.server_nip : NULL), |
939 | server_config.server_mac) | 942 | server_data.server_mac) |
940 | ) { | 943 | ) { |
941 | retval = 1; | 944 | retval = 1; |
942 | goto ret; | 945 | goto ret; |
943 | } | 946 | } |
944 | 947 | ||
945 | /* Setup the signal pipe */ | ||
946 | udhcp_sp_setup(); | ||
947 | |||
948 | continue_with_autotime: | 948 | continue_with_autotime: |
949 | timeout_end = monotonic_sec() + server_config.auto_time; | 949 | timeout_end = monotonic_sec() + server_data.auto_time; |
950 | while (1) { /* loop until universe collapses */ | 950 | while (1) { /* loop until universe collapses */ |
951 | struct pollfd pfds[2]; | 951 | struct pollfd pfds[2]; |
952 | struct dhcp_packet packet; | 952 | struct dhcp_packet packet; |
@@ -960,14 +960,14 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
960 | 960 | ||
961 | if (server_socket < 0) { | 961 | if (server_socket < 0) { |
962 | server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, | 962 | server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, |
963 | server_config.interface); | 963 | server_data.interface); |
964 | } | 964 | } |
965 | 965 | ||
966 | udhcp_sp_fd_set(pfds, server_socket); | 966 | udhcp_sp_fd_set(pfds, server_socket); |
967 | 967 | ||
968 | new_tv: | 968 | new_tv: |
969 | tv = -1; | 969 | tv = -1; |
970 | if (server_config.auto_time) { | 970 | if (server_data.auto_time) { |
971 | tv = timeout_end - monotonic_sec(); | 971 | tv = timeout_end - monotonic_sec(); |
972 | if (tv <= 0) { | 972 | if (tv <= 0) { |
973 | write_leases: | 973 | write_leases: |
@@ -985,7 +985,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
985 | if (errno == EINTR) | 985 | if (errno == EINTR) |
986 | goto new_tv; | 986 | goto new_tv; |
987 | /* < 0 and not EINTR: should not happen */ | 987 | /* < 0 and not EINTR: should not happen */ |
988 | bb_perror_msg_and_die("poll"); | 988 | bb_simple_perror_msg_and_die("poll"); |
989 | } | 989 | } |
990 | 990 | ||
991 | if (pfds[0].revents) switch (udhcp_sp_read()) { | 991 | if (pfds[0].revents) switch (udhcp_sp_read()) { |
@@ -1019,16 +1019,16 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
1019 | continue; | 1019 | continue; |
1020 | } | 1020 | } |
1021 | if (packet.hlen != 6) { | 1021 | if (packet.hlen != 6) { |
1022 | bb_info_msg("MAC length != 6, ignoring packet"); | 1022 | bb_info_msg("MAC length != 6%s", ", ignoring packet"); |
1023 | continue; | 1023 | continue; |
1024 | } | 1024 | } |
1025 | if (packet.op != BOOTREQUEST) { | 1025 | if (packet.op != BOOTREQUEST) { |
1026 | bb_info_msg("not a REQUEST, ignoring packet"); | 1026 | bb_info_msg("not a REQUEST%s", ", ignoring packet"); |
1027 | continue; | 1027 | continue; |
1028 | } | 1028 | } |
1029 | state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE); | 1029 | state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE); |
1030 | if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) { | 1030 | if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) { |
1031 | bb_info_msg("no or bad message type option, ignoring packet"); | 1031 | bb_info_msg("no or bad message type option%s", ", ignoring packet"); |
1032 | continue; | 1032 | continue; |
1033 | } | 1033 | } |
1034 | 1034 | ||
@@ -1037,9 +1037,9 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) | |||
1037 | if (server_id_opt) { | 1037 | if (server_id_opt) { |
1038 | uint32_t server_id_network_order; | 1038 | uint32_t server_id_network_order; |
1039 | move_from_unaligned32(server_id_network_order, server_id_opt); | 1039 | move_from_unaligned32(server_id_network_order, server_id_opt); |
1040 | if (server_id_network_order != server_config.server_nip) { | 1040 | if (server_id_network_order != server_data.server_nip) { |
1041 | /* client talks to somebody else */ | 1041 | /* client talks to somebody else */ |
1042 | log1("server ID doesn't match, ignoring"); | 1042 | log1("server ID doesn't match%s", ", ignoring"); |
1043 | continue; | 1043 | continue; |
1044 | } | 1044 | } |
1045 | } | 1045 | } |
@@ -1162,7 +1162,7 @@ o DHCPREQUEST generated during REBINDING state: | |||
1162 | if (!requested_ip_opt) { | 1162 | if (!requested_ip_opt) { |
1163 | requested_nip = packet.ciaddr; | 1163 | requested_nip = packet.ciaddr; |
1164 | if (requested_nip == 0) { | 1164 | if (requested_nip == 0) { |
1165 | log1("no requested IP and no ciaddr, ignoring"); | 1165 | log1("no requested IP and no ciaddr%s", ", ignoring"); |
1166 | break; | 1166 | break; |
1167 | } | 1167 | } |
1168 | } | 1168 | } |
@@ -1204,7 +1204,7 @@ o DHCPREQUEST generated during REBINDING state: | |||
1204 | && requested_nip == lease->lease_nip | 1204 | && requested_nip == lease->lease_nip |
1205 | ) { | 1205 | ) { |
1206 | memset(lease->lease_mac, 0, sizeof(lease->lease_mac)); | 1206 | memset(lease->lease_mac, 0, sizeof(lease->lease_mac)); |
1207 | lease->expires = time(NULL) + server_config.decline_time; | 1207 | lease->expires = time(NULL) + server_data.decline_time; |
1208 | } | 1208 | } |
1209 | break; | 1209 | break; |
1210 | 1210 | ||
@@ -1235,7 +1235,7 @@ o DHCPREQUEST generated during REBINDING state: | |||
1235 | ret0: | 1235 | ret0: |
1236 | retval = 0; | 1236 | retval = 0; |
1237 | ret: | 1237 | ret: |
1238 | /*if (server_config.pidfile) - server_config.pidfile is never NULL */ | 1238 | /*if (server_data.pidfile) - server_data.pidfile is never NULL */ |
1239 | remove_pidfile(server_config.pidfile); | 1239 | remove_pidfile(server_data.pidfile); |
1240 | return retval; | 1240 | return retval; |
1241 | } | 1241 | } |