blob: d248d2b6714127fb7a04bb10ebec8fa480620b65 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +01002/*
Denys Vlasenko385b4562010-03-26 10:09:34 +01003 * udhcp server
Mike Frysinger7031f622006-05-08 03:20:50 +00004 * 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 Vlasenko8a7c1662010-03-20 03:48:11 +01009 * 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 Frysinger7031f622006-05-08 03:20:50 +000022 */
Denys Vlasenkof7683cd2016-11-23 18:54:59 +010023//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 Orga5bc8c002011-04-11 03:29:49 +020028
29//usage:#define udhcpd_trivial_usage
Denys Vlasenko7b5d5c12013-03-14 02:18:52 +010030//usage: "[-fS] [-I ADDR]" IF_FEATURE_UDHCP_PORT(" [-P N]") " [CONFFILE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020031//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 Vlasenkoe3f5b732013-03-13 22:27:37 +010035//usage: "\n -I ADDR Local address"
Michel Stam9f412712014-10-30 11:59:04 +010036//usage: "\n -a MSEC Timeout for ARP ping (default 2000)"
Pere Orga5bc8c002011-04-11 03:29:49 +020037//usage: IF_FEATURE_UDHCP_PORT(
38//usage: "\n -P N Use port N (default 67)"
39//usage: )
40
Denys Vlasenko2bf29312016-10-04 00:37:50 +020041#include <netinet/ether.h>
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000042#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000043#include "common.h"
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000044#include "dhcpc.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000045#include "dhcpd.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000046
Denys Vlasenkoa85740c2016-10-04 00:51:38 +020047/* globals */
Denys Vlasenko06076492018-02-01 10:41:14 +010048#define g_leases ((struct dyn_lease*)ptr_to_globals)
Denys Vlasenkoa85740c2016-10-04 00:51:38 +020049/* struct server_config_t server_config is in bb_common_bufsiz1 */
50
Denys Vlasenkod2ae66c2016-10-04 00:43:14 +020051/* 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 */
54static 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 */
73static 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 Vlasenkoa85740c2016-10-04 00:51:38 +020084static 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 Vlasenkod2ae66c2016-10-04 00:43:14 +020095#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 */
98static 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) {
James Byrne253c4e72019-04-12 17:01:51 +0000107 bb_info_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x",
Denys Vlasenkod2ae66c2016-10-04 00:43:14 +0200108 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 Vlasenkoa85740c2016-10-04 00:51:38 +0200119/* Find the oldest expired lease, NULL if there are no expired leases */
120static 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 */
142static 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 */
158static 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 */
201static 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 */
207static 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 */
219static 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 */
231static 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;
James Byrne253c4e72019-04-12 17:01:51 +0000245 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
Denys Vlasenkoa85740c2016-10-04 00:51:38 +0200246 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 */
252static 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 Vlasenkod2ae66c2016-10-04 00:43:14 +0200322/* On these functions, make sure your datatype matches */
Denys Vlasenko2bf29312016-10-04 00:37:50 +0200323static 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
332static 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
338static 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
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200364static int FAST_FUNC read_optset(const char *line, void *arg) {
Denys Vlasenko60275972018-05-14 11:06:35 +0200365 return udhcp_str2optset(line, arg,
366 dhcp_optflags, dhcp_option_strings,
367 /*dhcpv6:*/ 0
368 );
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200369}
370
Denys Vlasenko2bf29312016-10-04 00:37:50 +0200371struct config_keyword {
372 const char *keyword;
373 int (*handler)(const char *line, void *var) FAST_FUNC;
374 unsigned ofs;
375 const char *def;
376};
377
378#define OFS(field) offsetof(struct server_config_t, field)
379
380static const struct config_keyword keywords[] = {
Denys Vlasenko3d27d432018-12-27 18:03:20 +0100381 /* keyword handler variable address default */
Denys Vlasenko2bf29312016-10-04 00:37:50 +0200382 {"start" , udhcp_str2nip , OFS(start_ip ), "192.168.0.20"},
383 {"end" , udhcp_str2nip , OFS(end_ip ), "192.168.0.254"},
384 {"interface" , read_str , OFS(interface ), "eth0"},
385 /* Avoid "max_leases value not sane" warning by setting default
386 * to default_end_ip - default_start_ip + 1: */
387 {"max_leases" , read_u32 , OFS(max_leases ), "235"},
388 {"auto_time" , read_u32 , OFS(auto_time ), "7200"},
389 {"decline_time" , read_u32 , OFS(decline_time ), "3600"},
390 {"conflict_time", read_u32 , OFS(conflict_time), "3600"},
391 {"offer_time" , read_u32 , OFS(offer_time ), "60"},
392 {"min_lease" , read_u32 , OFS(min_lease_sec), "60"},
393 {"lease_file" , read_str , OFS(lease_file ), LEASES_FILE},
394 {"pidfile" , read_str , OFS(pidfile ), "/var/run/udhcpd.pid"},
395 {"siaddr" , udhcp_str2nip , OFS(siaddr_nip ), "0.0.0.0"},
396 /* keywords with no defaults must be last! */
Denys Vlasenkoba4fbca2017-06-28 19:18:17 +0200397 {"option" , read_optset , OFS(options ), ""},
398 {"opt" , read_optset , OFS(options ), ""},
Denys Vlasenko2bf29312016-10-04 00:37:50 +0200399 {"notify_file" , read_str , OFS(notify_file ), NULL},
400 {"sname" , read_str , OFS(sname ), NULL},
401 {"boot_file" , read_str , OFS(boot_file ), NULL},
402 {"static_lease" , read_staticlease, OFS(static_leases), ""},
403};
404enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
405
406static NOINLINE void read_config(const char *file)
407{
408 parser_t *parser;
409 const struct config_keyword *k;
410 unsigned i;
411 char *token[2];
412
413 for (i = 0; i < KWS_WITH_DEFAULTS; i++)
414 keywords[i].handler(keywords[i].def, (char*)&server_config + keywords[i].ofs);
415
416 parser = config_open(file);
417 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
418 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
419 if (strcasecmp(token[0], k->keyword) == 0) {
420 if (!k->handler(token[1], (char*)&server_config + k->ofs)) {
421 bb_error_msg("can't parse line %u in %s",
422 parser->lineno, file);
423 /* reset back to the default value */
424 k->handler(k->def, (char*)&server_config + k->ofs);
425 }
426 break;
427 }
428 }
429 }
430 config_close(parser);
431
432 server_config.start_ip = ntohl(server_config.start_ip);
433 server_config.end_ip = ntohl(server_config.end_ip);
434}
435
436static void write_leases(void)
437{
438 int fd;
439 unsigned i;
440 leasetime_t curr;
441 int64_t written_at;
442
443 fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
444 if (fd < 0)
445 return;
446
447 curr = written_at = time(NULL);
448
449 written_at = SWAP_BE64(written_at);
450 full_write(fd, &written_at, sizeof(written_at));
451
452 for (i = 0; i < server_config.max_leases; i++) {
453 leasetime_t tmp_time;
454
455 if (g_leases[i].lease_nip == 0)
456 continue;
457
458 /* Screw with the time in the struct, for easier writing */
459 tmp_time = g_leases[i].expires;
460
461 g_leases[i].expires -= curr;
462 if ((signed_leasetime_t) g_leases[i].expires < 0)
463 g_leases[i].expires = 0;
464 g_leases[i].expires = htonl(g_leases[i].expires);
465
466 /* No error check. If the file gets truncated,
467 * we lose some leases on restart. Oh well. */
468 full_write(fd, &g_leases[i], sizeof(g_leases[i]));
469
470 /* Then restore it when done */
471 g_leases[i].expires = tmp_time;
472 }
473 close(fd);
474
475 if (server_config.notify_file) {
476 char *argv[3];
477 argv[0] = server_config.notify_file;
478 argv[1] = server_config.lease_file;
479 argv[2] = NULL;
480 spawn_and_wait(argv);
481 }
482}
483
484static NOINLINE void read_leases(const char *file)
485{
486 struct dyn_lease lease;
487 int64_t written_at, time_passed;
488 int fd;
489#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
490 unsigned i = 0;
491#endif
492
493 fd = open_or_warn(file, O_RDONLY);
494 if (fd < 0)
495 return;
496
497 if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
498 goto ret;
499 written_at = SWAP_BE64(written_at);
500
501 time_passed = time(NULL) - written_at;
502 /* Strange written_at, or lease file from old version of udhcpd
503 * which had no "written_at" field? */
504 if ((uint64_t)time_passed > 12 * 60 * 60)
505 goto ret;
506
507 while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
508 uint32_t y = ntohl(lease.lease_nip);
509 if (y >= server_config.start_ip && y <= server_config.end_ip) {
510 signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
511 uint32_t static_nip;
512
513 if (expires <= 0)
514 /* We keep expired leases: add_lease() will add
515 * a lease with 0 seconds remaining.
516 * Fewer IP address changes this way for mass reboot scenario.
517 */
518 expires = 0;
519
520 /* Check if there is a different static lease for this IP or MAC */
521 static_nip = get_static_nip_by_mac(server_config.static_leases, lease.lease_mac);
522 if (static_nip) {
523 /* NB: we do not add lease even if static_nip == lease.lease_nip.
524 */
525 continue;
526 }
527 if (is_nip_reserved(server_config.static_leases, lease.lease_nip))
528 continue;
529
530 /* NB: add_lease takes "relative time", IOW,
531 * lease duration, not lease deadline. */
532 if (add_lease(lease.lease_mac, lease.lease_nip,
533 expires,
534 lease.hostname, sizeof(lease.hostname)
535 ) == 0
536 ) {
537 bb_error_msg("too many leases while loading %s", file);
538 break;
539 }
540#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
541 i++;
542#endif
543 }
544 }
545 log1("read %d leases", i);
546 ret:
547 close(fd);
548}
Mike Frysinger7031f622006-05-08 03:20:50 +0000549
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100550/* Send a packet to a specific mac address and ip address by creating our own ip packet */
551static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100552{
553 const uint8_t *chaddr;
554 uint32_t ciaddr;
555
556 // Was:
557 //if (force_broadcast) { /* broadcast */ }
558 //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
559 //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
560 //else { /* unicast to dhcp_pkt->yiaddr */ }
561 // But this is wrong: yiaddr is _our_ idea what client's IP is
562 // (for example, from lease file). Client may not know that,
563 // and may not have UDP socket listening on that IP!
564 // We should never unicast to dhcp_pkt->yiaddr!
565 // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
566 // and can be used.
567
568 if (force_broadcast
569 || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100570 || dhcp_pkt->ciaddr == 0
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100571 ) {
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +0200572 log1("broadcasting packet to client");
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100573 ciaddr = INADDR_BROADCAST;
574 chaddr = MAC_BCAST_ADDR;
575 } else {
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +0200576 log1("unicasting packet to client ciaddr");
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100577 ciaddr = dhcp_pkt->ciaddr;
578 chaddr = dhcp_pkt->chaddr;
579 }
580
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100581 udhcp_send_raw_packet(dhcp_pkt,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100582 /*src*/ server_config.server_nip, SERVER_PORT,
583 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
584 server_config.ifindex);
585}
586
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100587/* Send a packet to gateway_nip using the kernel ip stack */
588static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
589{
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +0200590 log1("forwarding packet to relay");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100591
592 udhcp_send_kernel_packet(dhcp_pkt,
593 server_config.server_nip, SERVER_PORT,
Denys Vlasenko148788e2018-06-21 17:36:22 +0200594 dhcp_pkt->gateway_nip, SERVER_PORT);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100595}
596
597static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100598{
599 if (dhcp_pkt->gateway_nip)
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100600 send_packet_to_relay(dhcp_pkt);
601 else
602 send_packet_to_client(dhcp_pkt, force_broadcast);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100603}
604
605static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
606{
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100607 /* Sets op, htype, hlen, cookie fields
608 * and adds DHCP_MESSAGE_TYPE option */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100609 udhcp_init_header(packet, type);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100610
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100611 packet->xid = oldpacket->xid;
612 memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
613 packet->flags = oldpacket->flags;
614 packet->gateway_nip = oldpacket->gateway_nip;
615 packet->ciaddr = oldpacket->ciaddr;
Denys Vlasenko7724c762010-03-26 09:32:09 +0100616 udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100617}
618
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100619/* Fill options field, siaddr_nip, and sname and boot_file fields.
620 * TODO: teach this code to use overload option.
621 */
622static void add_server_options(struct dhcp_packet *packet)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100623{
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100624 struct option_set *curr = server_config.options;
625
626 while (curr) {
627 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
Denys Vlasenko7724c762010-03-26 09:32:09 +0100628 udhcp_add_binary_option(packet, curr->data);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100629 curr = curr->next;
630 }
631
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100632 packet->siaddr_nip = server_config.siaddr_nip;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100633
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100634 if (server_config.sname)
635 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
636 if (server_config.boot_file)
637 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
638}
639
640static uint32_t select_lease_time(struct dhcp_packet *packet)
641{
642 uint32_t lease_time_sec = server_config.max_lease_sec;
Denys Vlasenko6d3b4bb2018-12-17 18:07:18 +0100643 uint8_t *lease_time_opt = udhcp_get_option32(packet, DHCP_LEASE_TIME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100644 if (lease_time_opt) {
645 move_from_unaligned32(lease_time_sec, lease_time_opt);
646 lease_time_sec = ntohl(lease_time_sec);
647 if (lease_time_sec > server_config.max_lease_sec)
648 lease_time_sec = server_config.max_lease_sec;
649 if (lease_time_sec < server_config.min_lease_sec)
650 lease_time_sec = server_config.min_lease_sec;
651 }
652 return lease_time_sec;
653}
654
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100655/* We got a DHCP DISCOVER. Send an OFFER. */
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200656/* NOINLINE: limit stack usage in caller */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100657static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
658 uint32_t static_lease_nip,
659 struct dyn_lease *lease,
Michel Stam9f412712014-10-30 11:59:04 +0100660 uint8_t *requested_ip_opt,
661 unsigned arpping_ms)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100662{
663 struct dhcp_packet packet;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100664 uint32_t lease_time_sec;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100665 struct in_addr addr;
666
667 init_packet(&packet, oldpacket, DHCPOFFER);
668
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100669 /* If it is a static lease, use its IP */
670 packet.yiaddr = static_lease_nip;
671 /* Else: */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100672 if (!static_lease_nip) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100673 /* We have no static lease for client's chaddr */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100674 uint32_t req_nip;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100675 const char *p_host_name;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100676
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100677 if (lease) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100678 /* We have a dynamic lease for client's chaddr.
679 * Reuse its IP (even if lease is expired).
680 * Note that we ignore requested IP in this case.
681 */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100682 packet.yiaddr = lease->lease_nip;
683 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100684 /* Or: if client has requested an IP */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100685 else if (requested_ip_opt != NULL
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100686 /* (read IP) */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100687 && (move_from_unaligned32(req_nip, requested_ip_opt), 1)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100688 /* and the IP is in the lease range */
689 && ntohl(req_nip) >= server_config.start_ip
690 && ntohl(req_nip) <= server_config.end_ip
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100691 /* and */
692 && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
693 || is_expired_lease(lease) /* or is taken, but expired */
694 )
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100695 ) {
696 packet.yiaddr = req_nip;
697 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100698 else {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100699 /* Otherwise, find a free IP */
Michel Stam9f412712014-10-30 11:59:04 +0100700 packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr, arpping_ms);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100701 }
702
703 if (!packet.yiaddr) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100704 bb_error_msg("no free IP addresses. OFFER abandoned");
705 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100706 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100707 /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100708 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100709 lease = add_lease(packet.chaddr, packet.yiaddr,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100710 server_config.offer_time,
711 p_host_name,
712 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100713 );
714 if (!lease) {
715 bb_error_msg("no free IP addresses. OFFER abandoned");
716 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100717 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100718 }
719
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100720 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100721 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100722 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100723
724 addr.s_addr = packet.yiaddr;
James Byrne253c4e72019-04-12 17:01:51 +0000725 bb_info_msg("sending OFFER of %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100726 /* send_packet emits error message itself if it detects failure */
727 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100728}
729
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200730/* NOINLINE: limit stack usage in caller */
731static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100732{
733 struct dhcp_packet packet;
734
735 init_packet(&packet, oldpacket, DHCPNAK);
736
Denys Vlasenko16efe192016-03-30 18:44:52 +0200737 log1("sending %s", "NAK");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100738 send_packet(&packet, /*force_bcast:*/ 1);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100739}
740
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200741/* NOINLINE: limit stack usage in caller */
742static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100743{
744 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100745 uint32_t lease_time_sec;
746 struct in_addr addr;
747 const char *p_host_name;
748
749 init_packet(&packet, oldpacket, DHCPACK);
750 packet.yiaddr = yiaddr;
751
752 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100753 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100754
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100755 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100756
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100757 addr.s_addr = yiaddr;
James Byrne253c4e72019-04-12 17:01:51 +0000758 bb_info_msg("sending ACK to %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100759 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100760
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100761 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100762 add_lease(packet.chaddr, packet.yiaddr,
763 lease_time_sec,
764 p_host_name,
765 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
766 );
767 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
768 /* rewrite the file with leases at every new acceptance */
769 write_leases();
770 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100771}
772
Denys Vlasenko0bb35e12010-10-21 12:33:10 +0200773/* NOINLINE: limit stack usage in caller */
774static NOINLINE void send_inform(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100775{
776 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100777
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200778 /* "If a client has obtained a network address through some other means
779 * (e.g., manual configuration), it may use a DHCPINFORM request message
780 * to obtain other local configuration parameters. Servers receiving a
781 * DHCPINFORM message construct a DHCPACK message with any local
782 * configuration parameters appropriate for the client without:
783 * allocating a new address, checking for an existing binding, filling
784 * in 'yiaddr' or including lease time parameters. The servers SHOULD
785 * unicast the DHCPACK reply to the address given in the 'ciaddr' field
786 * of the DHCPINFORM message.
787 * ...
788 * The server responds to a DHCPINFORM message by sending a DHCPACK
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100789 * message directly to the address given in the 'ciaddr' field
790 * of the DHCPINFORM message. The server MUST NOT send a lease
791 * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
792 */
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200793//TODO: do a few sanity checks: is ciaddr set?
794//Better yet: is ciaddr == IP source addr?
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100795 init_packet(&packet, oldpacket, DHCPACK);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100796 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100797
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100798 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100799}
800
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000801int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000802int udhcpd_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000803{
Denys Vlasenko52a515d2017-02-16 23:25:44 +0100804 int server_socket = -1, retval;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200805 uint8_t *state;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000806 unsigned timeout_end;
807 unsigned num_ips;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000808 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000809 struct option_set *option;
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100810 char *str_I = str_I;
Michel Stam9f412712014-10-30 11:59:04 +0100811 const char *str_a = "2000";
812 unsigned arpping_ms;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000813 IF_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +0000814
Denys Vlasenkodf70a432016-04-21 18:54:36 +0200815 setup_common_bufsiz();
816
817 IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;)
818 IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000819
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200820 opt = getopt32(argv, "^"
821 "fSI:va:"IF_FEATURE_UDHCP_PORT("P:")
822 "\0"
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200823#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200824 "vv"
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200825#endif
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100826 , &str_I
Michel Stam9f412712014-10-30 11:59:04 +0100827 , &str_a
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100828 IF_FEATURE_UDHCP_PORT(, &str_P)
Leonid Lisovskiy6c9c0a12011-10-18 00:35:47 +0200829 IF_UDHCP_VERBOSE(, &dhcp_verbose)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200830 );
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000831 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000832 bb_daemonize_or_rexec(0, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000833 logmode = LOGMODE_NONE;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000834 }
Mike Frysinger6db13732010-06-04 13:24:50 -0400835 /* update argv after the possible vfork+exec in daemonize */
836 argv += optind;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000837 if (opt & 2) { /* -S */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000838 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000839 logmode |= LOGMODE_SYSLOG;
840 }
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100841 if (opt & 4) { /* -I */
842 len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET);
843 server_config.server_nip = lsa->u.sin.sin_addr.s_addr;
844 free(lsa);
845 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000846#if ENABLE_FEATURE_UDHCP_PORT
Michel Stam9f412712014-10-30 11:59:04 +0100847 if (opt & 32) { /* -P */
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000848 SERVER_PORT = xatou16(str_P);
849 CLIENT_PORT = SERVER_PORT + 1;
850 }
851#endif
Michel Stam9f412712014-10-30 11:59:04 +0100852 arpping_ms = xatou(str_a);
853
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000854 /* Would rather not do read_config before daemonization -
855 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +0000856 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Denys Vlasenko3d306bf2018-03-11 11:34:44 +0100857 /* prevent poll timeout overflow */
858 if (server_config.auto_time > INT_MAX / 1000)
859 server_config.auto_time = INT_MAX / 1000;
Mike Frysinger7031f622006-05-08 03:20:50 +0000860
Denis Vlasenko80edead2007-08-02 22:31:05 +0000861 /* Make sure fd 0,1,2 are open */
862 bb_sanitize_stdio();
Denis Vlasenko80edead2007-08-02 22:31:05 +0000863
864 /* Create pidfile */
865 write_pidfile(server_config.pidfile);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100866 /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000867
James Byrne253c4e72019-04-12 17:01:51 +0000868 bb_info_msg("started, v"BB_VER);
Mike Frysinger7031f622006-05-08 03:20:50 +0000869
Denys Vlasenko7724c762010-03-26 09:32:09 +0100870 option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
Denys Vlasenko2e7aa922010-03-21 02:22:07 +0100871 server_config.max_lease_sec = DEFAULT_LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000872 if (option) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200873 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
874 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000875 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000876
877 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000878 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000879 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000880 bb_error_msg("max_leases=%u is too big, setting to %u",
881 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +0000882 server_config.max_leases = num_ips;
883 }
884
Denys Vlasenko06076492018-02-01 10:41:14 +0100885 /* this sets g_leases */
886 SET_PTR_TO_GLOBALS(xzalloc(server_config.max_leases * sizeof(g_leases[0])));
887
Mike Frysinger7031f622006-05-08 03:20:50 +0000888 read_leases(server_config.lease_file);
889
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200890 if (udhcp_read_interface(server_config.interface,
891 &server_config.ifindex,
Denys Vlasenkoe3f5b732013-03-13 22:27:37 +0100892 (server_config.server_nip == 0 ? &server_config.server_nip : NULL),
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200893 server_config.server_mac)
894 ) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000895 retval = 1;
896 goto ret;
897 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000898
Mike Frysinger7031f622006-05-08 03:20:50 +0000899 /* Setup the signal pipe */
900 udhcp_sp_setup();
901
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200902 continue_with_autotime:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000903 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000904 while (1) { /* loop until universe collapses */
Denys Vlasenko52a515d2017-02-16 23:25:44 +0100905 struct pollfd pfds[2];
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100906 struct dhcp_packet packet;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000907 int bytes;
Denys Vlasenko52a515d2017-02-16 23:25:44 +0100908 int tv;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100909 uint8_t *server_id_opt;
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100910 uint8_t *requested_ip_opt;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100911 uint32_t requested_nip = requested_nip; /* for compiler */
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100912 uint32_t static_lease_nip;
913 struct dyn_lease *lease, fake_lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000914
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000915 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000916 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000917 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000918 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000919
Denys Vlasenko52a515d2017-02-16 23:25:44 +0100920 udhcp_sp_fd_set(pfds, server_socket);
Denys Vlasenko3d306bf2018-03-11 11:34:44 +0100921
922 new_tv:
923 tv = -1;
924 if (server_config.auto_time) {
925 tv = timeout_end - monotonic_sec();
926 if (tv <= 0) {
927 write_leases:
Denys Vlasenko3293bc12018-03-10 19:01:48 +0100928 write_leases();
929 goto continue_with_autotime;
930 }
Denys Vlasenko3d306bf2018-03-11 11:34:44 +0100931 tv *= 1000;
932 }
933
934 /* Block here waiting for either signal or packet */
935 retval = poll(pfds, 2, tv);
936 if (retval <= 0) {
937 if (retval == 0)
938 goto write_leases;
939 if (errno == EINTR)
940 goto new_tv;
Denys Vlasenko3293bc12018-03-10 19:01:48 +0100941 /* < 0 and not EINTR: should not happen */
942 bb_perror_msg_and_die("poll");
Mike Frysinger7031f622006-05-08 03:20:50 +0000943 }
944
Denys Vlasenko3293bc12018-03-10 19:01:48 +0100945 if (pfds[0].revents) switch (udhcp_sp_read()) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000946 case SIGUSR1:
James Byrne253c4e72019-04-12 17:01:51 +0000947 bb_info_msg("received %s", "SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000948 write_leases();
949 /* why not just reset the timeout, eh */
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200950 goto continue_with_autotime;
Mike Frysinger7031f622006-05-08 03:20:50 +0000951 case SIGTERM:
James Byrne253c4e72019-04-12 17:01:51 +0000952 bb_info_msg("received %s", "SIGTERM");
Denys Vlasenko71045cc2012-07-24 17:21:26 +0200953 write_leases();
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000954 goto ret0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000955 }
956
Denys Vlasenko3293bc12018-03-10 19:01:48 +0100957 /* Is it a packet? */
958 if (!pfds[1].revents)
959 continue; /* no */
960
961 /* Note: we do not block here, we block on poll() instead.
962 * Blocking here would prevent SIGTERM from working:
963 * socket read inside this call is restarted on caught signals.
964 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000965 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000966 if (bytes < 0) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000967 /* bytes can also be -2 ("bad packet data") */
Mike Frysinger7031f622006-05-08 03:20:50 +0000968 if (bytes == -1 && errno != EINTR) {
Denys Vlasenko6f97b302017-09-29 18:17:25 +0200969 log1("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
Mike Frysinger7031f622006-05-08 03:20:50 +0000970 close(server_socket);
971 server_socket = -1;
972 }
973 continue;
974 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200975 if (packet.hlen != 6) {
James Byrne253c4e72019-04-12 17:01:51 +0000976 bb_info_msg("MAC length != 6, ignoring packet");
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200977 continue;
978 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100979 if (packet.op != BOOTREQUEST) {
James Byrne253c4e72019-04-12 17:01:51 +0000980 bb_info_msg("not a REQUEST, ignoring packet");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100981 continue;
982 }
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100983 state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100984 if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
James Byrne253c4e72019-04-12 17:01:51 +0000985 bb_info_msg("no or bad message type option, ignoring packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000986 continue;
987 }
988
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100989 /* Get SERVER_ID if present */
Denys Vlasenko6d3b4bb2018-12-17 18:07:18 +0100990 server_id_opt = udhcp_get_option32(&packet, DHCP_SERVER_ID);
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100991 if (server_id_opt) {
Denys Vlasenko713d2412010-11-28 21:51:44 +0100992 uint32_t server_id_network_order;
993 move_from_unaligned32(server_id_network_order, server_id_opt);
994 if (server_id_network_order != server_config.server_nip) {
Denys Vlasenkofa5e2952010-11-28 01:10:51 +0100995 /* client talks to somebody else */
996 log1("server ID doesn't match, ignoring");
997 continue;
998 }
999 }
1000
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001001 /* Look for a static/dynamic lease */
Denys Vlasenkoa9539872010-03-20 03:49:27 +01001002 static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
1003 if (static_lease_nip) {
James Byrne253c4e72019-04-12 17:01:51 +00001004 bb_info_msg("found static lease: %x", static_lease_nip);
Denys Vlasenko6947d2c2009-06-17 13:24:03 +02001005 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
Denys Vlasenkoa9539872010-03-20 03:49:27 +01001006 fake_lease.lease_nip = static_lease_nip;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +02001007 fake_lease.expires = 0;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +02001008 lease = &fake_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +00001009 } else {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +02001010 lease = find_lease_by_mac(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +00001011 }
1012
Denys Vlasenkofa5e2952010-11-28 01:10:51 +01001013 /* Get REQUESTED_IP if present */
Denys Vlasenko6d3b4bb2018-12-17 18:07:18 +01001014 requested_ip_opt = udhcp_get_option32(&packet, DHCP_REQUESTED_IP);
Denys Vlasenkofa5e2952010-11-28 01:10:51 +01001015 if (requested_ip_opt) {
1016 move_from_unaligned32(requested_nip, requested_ip_opt);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001017 }
1018
Mike Frysinger7031f622006-05-08 03:20:50 +00001019 switch (state[0]) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001020
Mike Frysinger7031f622006-05-08 03:20:50 +00001021 case DHCPDISCOVER:
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +02001022 log1("received %s", "DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +00001023
Michel Stam9f412712014-10-30 11:59:04 +01001024 send_offer(&packet, static_lease_nip, lease, requested_ip_opt, arpping_ms);
Mike Frysinger7031f622006-05-08 03:20:50 +00001025 break;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +02001026
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001027 case DHCPREQUEST:
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +02001028 log1("received %s", "REQUEST");
Denys Vlasenko53f72bb2010-03-21 06:46:09 +01001029/* RFC 2131:
Mike Frysinger7031f622006-05-08 03:20:50 +00001030
Denys Vlasenko53f72bb2010-03-21 06:46:09 +01001031o DHCPREQUEST generated during SELECTING state:
1032
1033 Client inserts the address of the selected server in 'server
1034 identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
1035 filled in with the yiaddr value from the chosen DHCPOFFER.
1036
1037 Note that the client may choose to collect several DHCPOFFER
1038 messages and select the "best" offer. The client indicates its
1039 selection by identifying the offering server in the DHCPREQUEST
1040 message. If the client receives no acceptable offers, the client
1041 may choose to try another DHCPDISCOVER message. Therefore, the
1042 servers may not receive a specific DHCPREQUEST from which they can
1043 decide whether or not the client has accepted the offer.
1044
1045o DHCPREQUEST generated during INIT-REBOOT state:
1046
1047 'server identifier' MUST NOT be filled in, 'requested IP address'
1048 option MUST be filled in with client's notion of its previously
1049 assigned address. 'ciaddr' MUST be zero. The client is seeking to
1050 verify a previously allocated, cached configuration. Server SHOULD
1051 send a DHCPNAK message to the client if the 'requested IP address'
1052 is incorrect, or is on the wrong network.
1053
1054 Determining whether a client in the INIT-REBOOT state is on the
1055 correct network is done by examining the contents of 'giaddr', the
1056 'requested IP address' option, and a database lookup. If the DHCP
1057 server detects that the client is on the wrong net (i.e., the
1058 result of applying the local subnet mask or remote subnet mask (if
1059 'giaddr' is not zero) to 'requested IP address' option value
1060 doesn't match reality), then the server SHOULD send a DHCPNAK
1061 message to the client.
1062
1063 If the network is correct, then the DHCP server should check if
1064 the client's notion of its IP address is correct. If not, then the
1065 server SHOULD send a DHCPNAK message to the client. If the DHCP
1066 server has no record of this client, then it MUST remain silent,
1067 and MAY output a warning to the network administrator. This
1068 behavior is necessary for peaceful coexistence of non-
1069 communicating DHCP servers on the same wire.
1070
1071 If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
1072 the same subnet as the server. The server MUST broadcast the
1073 DHCPNAK message to the 0xffffffff broadcast address because the
1074 client may not have a correct network address or subnet mask, and
1075 the client may not be answering ARP requests.
1076
1077 If 'giaddr' is set in the DHCPREQUEST message, the client is on a
1078 different subnet. The server MUST set the broadcast bit in the
1079 DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
1080 client, because the client may not have a correct network address
1081 or subnet mask, and the client may not be answering ARP requests.
1082
1083o DHCPREQUEST generated during RENEWING state:
1084
1085 'server identifier' MUST NOT be filled in, 'requested IP address'
1086 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
1087 client's IP address. In this situation, the client is completely
1088 configured, and is trying to extend its lease. This message will
1089 be unicast, so no relay agents will be involved in its
1090 transmission. Because 'giaddr' is therefore not filled in, the
1091 DHCP server will trust the value in 'ciaddr', and use it when
1092 replying to the client.
1093
1094 A client MAY choose to renew or extend its lease prior to T1. The
1095 server may choose not to extend the lease (as a policy decision by
1096 the network administrator), but should return a DHCPACK message
1097 regardless.
1098
1099o DHCPREQUEST generated during REBINDING state:
1100
1101 'server identifier' MUST NOT be filled in, 'requested IP address'
1102 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
1103 client's IP address. In this situation, the client is completely
1104 configured, and is trying to extend its lease. This message MUST
1105 be broadcast to the 0xffffffff IP broadcast address. The DHCP
1106 server SHOULD check 'ciaddr' for correctness before replying to
1107 the DHCPREQUEST.
1108
1109 The DHCPREQUEST from a REBINDING client is intended to accommodate
1110 sites that have multiple DHCP servers and a mechanism for
1111 maintaining consistency among leases managed by multiple servers.
1112 A DHCP server MAY extend a client's lease only if it has local
1113 administrative authority to do so.
1114*/
Denys Vlasenkofa5e2952010-11-28 01:10:51 +01001115 if (!requested_ip_opt) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +01001116 requested_nip = packet.ciaddr;
1117 if (requested_nip == 0) {
1118 log1("no requested IP and no ciaddr, ignoring");
1119 break;
1120 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001121 }
1122 if (lease && requested_nip == lease->lease_nip) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +01001123 /* client requested or configured IP matches the lease.
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001124 * ACK it, and bump lease expiration time. */
1125 send_ACK(&packet, lease->lease_nip);
1126 break;
1127 }
Denys Vlasenko713d2412010-11-28 21:51:44 +01001128 /* No lease for this MAC, or lease IP != requested IP */
1129
1130 if (server_id_opt /* client is in SELECTING state */
1131 || requested_ip_opt /* client is in INIT-REBOOT state */
1132 ) {
1133 /* "No, we don't have this IP for you" */
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001134 send_NAK(&packet);
Denys Vlasenko713d2412010-11-28 21:51:44 +01001135 } /* else: client is in RENEWING or REBINDING, do not answer */
1136
Mike Frysinger7031f622006-05-08 03:20:50 +00001137 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001138
Mike Frysinger7031f622006-05-08 03:20:50 +00001139 case DHCPDECLINE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001140 /* RFC 2131:
1141 * "If the server receives a DHCPDECLINE message,
1142 * the client has discovered through some other means
1143 * that the suggested network address is already
1144 * in use. The server MUST mark the network address
1145 * as not available and SHOULD notify the local
1146 * sysadmin of a possible configuration problem."
1147 *
1148 * SERVER_ID must be present,
1149 * REQUESTED_IP must be present,
1150 * chaddr must be filled in,
1151 * ciaddr must be 0 (we do not check this)
1152 */
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +02001153 log1("received %s", "DECLINE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001154 if (server_id_opt
Denys Vlasenkofa5e2952010-11-28 01:10:51 +01001155 && requested_ip_opt
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001156 && lease /* chaddr matches this lease */
1157 && requested_nip == lease->lease_nip
1158 ) {
Denys Vlasenko31af3d52009-06-17 11:57:09 +02001159 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
Denis Vlasenko04158e02009-02-02 10:48:06 +00001160 lease->expires = time(NULL) + server_config.decline_time;
Mike Frysinger7031f622006-05-08 03:20:50 +00001161 }
1162 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001163
Mike Frysinger7031f622006-05-08 03:20:50 +00001164 case DHCPRELEASE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001165 /* "Upon receipt of a DHCPRELEASE message, the server
1166 * marks the network address as not allocated."
1167 *
1168 * SERVER_ID must be present,
1169 * REQUESTED_IP must not be present (we do not check this),
1170 * chaddr must be filled in,
1171 * ciaddr must be filled in
1172 */
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +02001173 log1("received %s", "RELEASE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001174 if (server_id_opt
1175 && lease /* chaddr matches this lease */
1176 && packet.ciaddr == lease->lease_nip
1177 ) {
Denis Vlasenko04158e02009-02-02 10:48:06 +00001178 lease->expires = time(NULL);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001179 }
Mike Frysinger7031f622006-05-08 03:20:50 +00001180 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +01001181
Mike Frysinger7031f622006-05-08 03:20:50 +00001182 case DHCPINFORM:
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +02001183 log1("received %s", "INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +00001184 send_inform(&packet);
1185 break;
Mike Frysinger7031f622006-05-08 03:20:50 +00001186 }
1187 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +00001188 ret0:
1189 retval = 0;
1190 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +00001191 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +00001192 remove_pidfile(server_config.pidfile);
1193 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +00001194}