blob: 550b1d689411b4b931d27fcd785dce934fa1072d [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* vi: set sw=4 ts=4: */
2/*
3 * udhcp server
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 *
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.
22 */
23
24//usage:#define udhcpd_trivial_usage
25//usage: "[-fS]" IF_FEATURE_UDHCP_PORT(" [-P N]") " [CONFFILE]"
26//usage:#define udhcpd_full_usage "\n\n"
27//usage: "DHCP server\n"
28//usage: "\n -f Run in foreground"
29//usage: "\n -S Log to syslog too"
30//usage: IF_FEATURE_UDHCP_PORT(
31//usage: "\n -P N Use port N (default 67)"
32//usage: )
33
34#include <syslog.h>
35#include "common.h"
36#include "dhcpc.h"
37#include "dhcpd.h"
38
39
40/* Send a packet to a specific mac address and ip address by creating our own ip packet */
41static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
42{
43 const uint8_t *chaddr;
44 uint32_t ciaddr;
45
46 // Was:
47 //if (force_broadcast) { /* broadcast */ }
48 //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
49 //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
50 //else { /* unicast to dhcp_pkt->yiaddr */ }
51 // But this is wrong: yiaddr is _our_ idea what client's IP is
52 // (for example, from lease file). Client may not know that,
53 // and may not have UDP socket listening on that IP!
54 // We should never unicast to dhcp_pkt->yiaddr!
55 // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
56 // and can be used.
57
58 if (force_broadcast
59 || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
60 || dhcp_pkt->ciaddr == 0
61 ) {
62 log1("Broadcasting packet to client");
63 ciaddr = INADDR_BROADCAST;
64 chaddr = MAC_BCAST_ADDR;
65 } else {
66 log1("Unicasting packet to client ciaddr");
67 ciaddr = dhcp_pkt->ciaddr;
68 chaddr = dhcp_pkt->chaddr;
69 }
70
71 udhcp_send_raw_packet(dhcp_pkt,
72 /*src*/ server_config.server_nip, SERVER_PORT,
73 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
74 server_config.ifindex);
75}
76
77/* Send a packet to gateway_nip using the kernel ip stack */
78static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
79{
80 log1("Forwarding packet to relay");
81
82 udhcp_send_kernel_packet(dhcp_pkt,
83 server_config.server_nip, SERVER_PORT,
84 dhcp_pkt->gateway_nip, SERVER_PORT);
85}
86
87static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
88{
89 if (dhcp_pkt->gateway_nip)
90 send_packet_to_relay(dhcp_pkt);
91 else
92 send_packet_to_client(dhcp_pkt, force_broadcast);
93}
94
95static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
96{
97 /* Sets op, htype, hlen, cookie fields
98 * and adds DHCP_MESSAGE_TYPE option */
99 udhcp_init_header(packet, type);
100
101 packet->xid = oldpacket->xid;
102 memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
103 packet->flags = oldpacket->flags;
104 packet->gateway_nip = oldpacket->gateway_nip;
105 packet->ciaddr = oldpacket->ciaddr;
106 udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
107}
108
109/* Fill options field, siaddr_nip, and sname and boot_file fields.
110 * TODO: teach this code to use overload option.
111 */
112static void add_server_options(struct dhcp_packet *packet)
113{
114 struct option_set *curr = server_config.options;
115
116 while (curr) {
117 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
118 udhcp_add_binary_option(packet, curr->data);
119 curr = curr->next;
120 }
121
122 packet->siaddr_nip = server_config.siaddr_nip;
123
124 if (server_config.sname)
125 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
126 if (server_config.boot_file)
127 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
128}
129
130static uint32_t select_lease_time(struct dhcp_packet *packet)
131{
132 uint32_t lease_time_sec = server_config.max_lease_sec;
133 uint8_t *lease_time_opt = udhcp_get_option32(packet, DHCP_LEASE_TIME);//CVE-2018-20679
134 if (lease_time_opt) {
135 move_from_unaligned32(lease_time_sec, lease_time_opt);
136 lease_time_sec = ntohl(lease_time_sec);
137 if (lease_time_sec > server_config.max_lease_sec)
138 lease_time_sec = server_config.max_lease_sec;
139 if (lease_time_sec < server_config.min_lease_sec)
140 lease_time_sec = server_config.min_lease_sec;
141 }
142 return lease_time_sec;
143}
144
145/* We got a DHCP DISCOVER. Send an OFFER. */
146/* NOINLINE: limit stack usage in caller */
147static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
148 uint32_t static_lease_nip,
149 struct dyn_lease *lease,
150 uint8_t *requested_ip_opt)
151{
152 struct dhcp_packet packet;
153 uint32_t lease_time_sec;
154 struct in_addr addr;
155
156 init_packet(&packet, oldpacket, DHCPOFFER);
157
158 /* If it is a static lease, use its IP */
159 packet.yiaddr = static_lease_nip;
160 /* Else: */
161 if (!static_lease_nip) {
162 /* We have no static lease for client's chaddr */
163 uint32_t req_nip;
164 const char *p_host_name;
165
166 if (lease) {
167 /* We have a dynamic lease for client's chaddr.
168 * Reuse its IP (even if lease is expired).
169 * Note that we ignore requested IP in this case.
170 */
171 packet.yiaddr = lease->lease_nip;
172 }
173 /* Or: if client has requested an IP */
174 else if (requested_ip_opt != NULL
175 /* (read IP) */
176 && (move_from_unaligned32(req_nip, requested_ip_opt), 1)
177 /* and the IP is in the lease range */
178 && ntohl(req_nip) >= server_config.start_ip
179 && ntohl(req_nip) <= server_config.end_ip
180 /* and */
181 && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
182 || is_expired_lease(lease) /* or is taken, but expired */
183 )
184 ) {
185 packet.yiaddr = req_nip;
186 }
187 else {
188 /* Otherwise, find a free IP */
189 packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr);
190 }
191
192 if (!packet.yiaddr) {
193 bb_error_msg("no free IP addresses. OFFER abandoned");
194 return;
195 }
196 /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
197 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
198 lease = add_lease(packet.chaddr, packet.yiaddr,
199 server_config.offer_time,
200 p_host_name,
201 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
202 );
203 if (!lease) {
204 bb_error_msg("no free IP addresses. OFFER abandoned");
205 return;
206 }
207 }
208
209 lease_time_sec = select_lease_time(oldpacket);
210 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
211 add_server_options(&packet);
212
213 addr.s_addr = packet.yiaddr;
214 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
215 /* send_packet emits error message itself if it detects failure */
216 send_packet(&packet, /*force_bcast:*/ 0);
217}
218
219/* NOINLINE: limit stack usage in caller */
220static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
221{
222 struct dhcp_packet packet;
223
224 init_packet(&packet, oldpacket, DHCPNAK);
225
226 log1("Sending NAK");
227 send_packet(&packet, /*force_bcast:*/ 1);
228}
229
230/* NOINLINE: limit stack usage in caller */
231static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
232{
233 struct dhcp_packet packet;
234 uint32_t lease_time_sec;
235 struct in_addr addr;
236 const char *p_host_name;
237
238 init_packet(&packet, oldpacket, DHCPACK);
239 packet.yiaddr = yiaddr;
240
241 lease_time_sec = select_lease_time(oldpacket);
242 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
243
244 add_server_options(&packet);
245
246 addr.s_addr = yiaddr;
247 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
248 send_packet(&packet, /*force_bcast:*/ 0);
249
250 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
251 add_lease(packet.chaddr, packet.yiaddr,
252 lease_time_sec,
253 p_host_name,
254 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
255 );
256 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
257 /* rewrite the file with leases at every new acceptance */
258 write_leases();
259 }
260}
261
262/* NOINLINE: limit stack usage in caller */
263static NOINLINE void send_inform(struct dhcp_packet *oldpacket)
264{
265 struct dhcp_packet packet;
266
267 /* "If a client has obtained a network address through some other means
268 * (e.g., manual configuration), it may use a DHCPINFORM request message
269 * to obtain other local configuration parameters. Servers receiving a
270 * DHCPINFORM message construct a DHCPACK message with any local
271 * configuration parameters appropriate for the client without:
272 * allocating a new address, checking for an existing binding, filling
273 * in 'yiaddr' or including lease time parameters. The servers SHOULD
274 * unicast the DHCPACK reply to the address given in the 'ciaddr' field
275 * of the DHCPINFORM message.
276 * ...
277 * The server responds to a DHCPINFORM message by sending a DHCPACK
278 * message directly to the address given in the 'ciaddr' field
279 * of the DHCPINFORM message. The server MUST NOT send a lease
280 * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
281 */
282//TODO: do a few sanity checks: is ciaddr set?
283//Better yet: is ciaddr == IP source addr?
284 init_packet(&packet, oldpacket, DHCPACK);
285 add_server_options(&packet);
286
287 send_packet(&packet, /*force_bcast:*/ 0);
288}
289
290
291/* globals */
292struct dyn_lease *g_leases;
293/* struct server_config_t server_config is in bb_common_bufsiz1 */
294extern int base_ip_on_mac = 0;
295int execute_cmd(const char *cmd, char *out_data, int out_len)
296{
297 int ret = -1;
298
299#if 1
300 FILE *fp;
301
302 fp = popen(cmd, "r");
303 if(NULL == fp) {
304 printf("popen execute[%s] fail, error:%s \n", cmd, strerror(errno));
305 return -1;
306 }
307
308 if(NULL != out_data){
309 while(fgets(out_data, out_len, fp) != NULL) {
310 if('\n' == out_data[strlen(out_data)-1]) {
311 out_data[strlen(out_data)-1] = '\0';
312 }
313 printf("command:[%s] out_data:[%s]\r\n", cmd, out_data);
314 }
315 }
316
317 ret = pclose(fp);
318 if(0 != ret) {
319 printf("Close [%s] out put point fail, ret = %d, error:%s \n", cmd, ret, strerror(errno));
320 return -1;
321 } else {
322 printf("command:[%s], child hork end status:[%d]\n", cmd, ret);
323 }
324
325 return 0;
326
327#else
328 ret = system(cmd);
329
330 if(-1 == ret) {
331 upi_log("System [%s] fail, error:%s \n", cmd, strerror(errno));
332 return -1;
333 }else{
334 printf("System [%s] fail, return:%d \n", cmd, ret);
335 }
336
337 return 0;
338#endif
339
340}
341
342int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
343int udhcpd_main(int argc UNUSED_PARAM, char **argv)
344{
345 int server_socket = -1, retval, max_sock;
346 uint8_t *state;
347 unsigned timeout_end;
348 unsigned num_ips;
349 unsigned opt;
350 struct option_set *option;
351 IF_FEATURE_UDHCP_PORT(char *str_P;)
352 char value[8] = {0};
353
354#if ENABLE_FEATURE_UDHCP_PORT
355 SERVER_PORT = 67;
356 CLIENT_PORT = 68;
357#endif
358
359#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
360 opt_complementary = "vv";
361#endif
362 if(execute_cmd("nv get base_ip_on_mac", value, sizeof(value)) == 0)
363 {
364 if(strcmp(value, "1") == 0)
365 {
366 base_ip_on_mac = 1;
367 }
368 }
369 opt = getopt32(argv, "fSv"
370 IF_FEATURE_UDHCP_PORT("P:", &str_P)
371 IF_UDHCP_VERBOSE(, &dhcp_verbose)
372 );
373 if (!(opt & 1)) { /* no -f */
374 bb_daemonize_or_rexec(0, argv);
375 logmode = LOGMODE_NONE;
376 }
377 /* update argv after the possible vfork+exec in daemonize */
378 argv += optind;
379 if (opt & 2) { /* -S */
380 openlog(applet_name, LOG_PID, LOG_DAEMON);
381 logmode |= LOGMODE_SYSLOG;
382 }
383#if ENABLE_FEATURE_UDHCP_PORT
384 if (opt & 8) { /* -P */
385 SERVER_PORT = xatou16(str_P);
386 CLIENT_PORT = SERVER_PORT + 1;
387 }
388#endif
389 /* Would rather not do read_config before daemonization -
390 * otherwise NOMMU machines will parse config twice */
391 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
392
393 /* Make sure fd 0,1,2 are open */
394 bb_sanitize_stdio();
395 /* Equivalent of doing a fflush after every \n */
396 setlinebuf(stdout);
397
398 /* Create pidfile */
399 write_pidfile(server_config.pidfile);
400 /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
401
402 bb_info_msg("%s (v"BB_VER") started", applet_name);
403
404 option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
405 server_config.max_lease_sec = DEFAULT_LEASE_TIME;
406 if (option) {
407 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
408 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
409 }
410
411 /* Sanity check */
412 num_ips = server_config.end_ip - server_config.start_ip + 1;
413 if (server_config.max_leases > num_ips) {
414 bb_error_msg("max_leases=%u is too big, setting to %u",
415 (unsigned)server_config.max_leases, num_ips);
416 server_config.max_leases = num_ips;
417 }
418
419 g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
420 read_leases(server_config.lease_file);
421
422 if (udhcp_read_interface(server_config.interface,
423 &server_config.ifindex,
424 &server_config.server_nip,
425 server_config.server_mac)
426 ) {
427 retval = 1;
428 goto ret;
429 }
430
431 /* Setup the signal pipe */
432 udhcp_sp_setup();
433
434 continue_with_autotime:
435 timeout_end = monotonic_sec() + server_config.auto_time;
436 while (1) { /* loop until universe collapses */
437 fd_set rfds;
438 struct dhcp_packet packet;
439 int bytes;
440 struct timeval tv;
441 uint8_t *server_id_opt;
442 uint8_t *requested_ip_opt;
443 uint32_t requested_nip = requested_nip; /* for compiler */
444 uint32_t static_lease_nip;
445 struct dyn_lease *lease, fake_lease;
446
447 if (server_socket < 0) {
448 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
449 server_config.interface);
450 }
451
452 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
453 if (server_config.auto_time) {
454 tv.tv_sec = timeout_end - monotonic_sec();
455 tv.tv_usec = 0;
456 }
457 retval = 0;
458 if (!server_config.auto_time || tv.tv_sec > 0) {
459 retval = select(max_sock + 1, &rfds, NULL, NULL,
460 server_config.auto_time ? &tv : NULL);
461 }
462 if (retval == 0) {
463 write_leases();
464 goto continue_with_autotime;
465 }
466 if (retval < 0 && errno != EINTR) {
467 log1("Error on select");
468 continue;
469 }
470
471 switch (udhcp_sp_read(&rfds)) {
472 case SIGUSR1:
473 bb_info_msg("Received SIGUSR1");
474 write_leases();
475 /* why not just reset the timeout, eh */
476 goto continue_with_autotime;
477 case SIGTERM:
478 bb_info_msg("Received SIGTERM");
479 write_leases();
480 goto ret0;
481 case 0: /* no signal: read a packet */
482 break;
483 default: /* signal or error (probably EINTR): back to select */
484 continue;
485 }
486
487 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
488 if (bytes < 0) {
489 /* bytes can also be -2 ("bad packet data") */
490 if (bytes == -1 && errno != EINTR) {
491 log1("Read error: %s, reopening socket", strerror(errno));
492 close(server_socket);
493 server_socket = -1;
494 }
495 continue;
496 }
497 if (packet.hlen != 6) {
498 bb_error_msg("MAC length != 6, ignoring packet");
499 continue;
500 }
501 if (packet.op != BOOTREQUEST) {
502 bb_error_msg("not a REQUEST, ignoring packet");
503 continue;
504 }
505 state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
506 if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
507 bb_error_msg("no or bad message type option, ignoring packet");
508 continue;
509 }
510
511 /* Get SERVER_ID if present */
512 server_id_opt = udhcp_get_option32(&packet, DHCP_SERVER_ID);//CVE-2018-20679
513 if (server_id_opt) {
514 uint32_t server_id_network_order;
515 move_from_unaligned32(server_id_network_order, server_id_opt);
516 if (server_id_network_order != server_config.server_nip) {
517 /* client talks to somebody else */
518 log1("server ID doesn't match, ignoring");
519 continue;
520 }
521 }
522
523 /* Look for a static/dynamic lease */
524 static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
525 if (static_lease_nip) {
526 bb_info_msg("Found static lease: %x", static_lease_nip);
527 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
528 fake_lease.lease_nip = static_lease_nip;
529 fake_lease.expires = 0;
530 lease = &fake_lease;
531 } else {
532 lease = find_lease_by_mac(packet.chaddr);
533 }
534
535 /* Get REQUESTED_IP if present */
536 requested_ip_opt = udhcp_get_option32(&packet, DHCP_REQUESTED_IP);//CVE-2018-20679
537 if (requested_ip_opt) {
538 move_from_unaligned32(requested_nip, requested_ip_opt);
539 }
540
541 switch (state[0]) {
542
543 case DHCPDISCOVER:
544 log1("Received DISCOVER");
545 //update br mac
546 udhcp_read_interface(server_config.interface,
547 &server_config.ifindex,
548 &server_config.server_nip,
549 server_config.server_mac);
550 send_offer(&packet, static_lease_nip, lease, requested_ip_opt);
551 break;
552
553 case DHCPREQUEST:
554 log1("Received REQUEST");
555/* RFC 2131:
556
557o DHCPREQUEST generated during SELECTING state:
558
559 Client inserts the address of the selected server in 'server
560 identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
561 filled in with the yiaddr value from the chosen DHCPOFFER.
562
563 Note that the client may choose to collect several DHCPOFFER
564 messages and select the "best" offer. The client indicates its
565 selection by identifying the offering server in the DHCPREQUEST
566 message. If the client receives no acceptable offers, the client
567 may choose to try another DHCPDISCOVER message. Therefore, the
568 servers may not receive a specific DHCPREQUEST from which they can
569 decide whether or not the client has accepted the offer.
570
571o DHCPREQUEST generated during INIT-REBOOT state:
572
573 'server identifier' MUST NOT be filled in, 'requested IP address'
574 option MUST be filled in with client's notion of its previously
575 assigned address. 'ciaddr' MUST be zero. The client is seeking to
576 verify a previously allocated, cached configuration. Server SHOULD
577 send a DHCPNAK message to the client if the 'requested IP address'
578 is incorrect, or is on the wrong network.
579
580 Determining whether a client in the INIT-REBOOT state is on the
581 correct network is done by examining the contents of 'giaddr', the
582 'requested IP address' option, and a database lookup. If the DHCP
583 server detects that the client is on the wrong net (i.e., the
584 result of applying the local subnet mask or remote subnet mask (if
585 'giaddr' is not zero) to 'requested IP address' option value
586 doesn't match reality), then the server SHOULD send a DHCPNAK
587 message to the client.
588
589 If the network is correct, then the DHCP server should check if
590 the client's notion of its IP address is correct. If not, then the
591 server SHOULD send a DHCPNAK message to the client. If the DHCP
592 server has no record of this client, then it MUST remain silent,
593 and MAY output a warning to the network administrator. This
594 behavior is necessary for peaceful coexistence of non-
595 communicating DHCP servers on the same wire.
596
597 If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
598 the same subnet as the server. The server MUST broadcast the
599 DHCPNAK message to the 0xffffffff broadcast address because the
600 client may not have a correct network address or subnet mask, and
601 the client may not be answering ARP requests.
602
603 If 'giaddr' is set in the DHCPREQUEST message, the client is on a
604 different subnet. The server MUST set the broadcast bit in the
605 DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
606 client, because the client may not have a correct network address
607 or subnet mask, and the client may not be answering ARP requests.
608
609o DHCPREQUEST generated during RENEWING state:
610
611 'server identifier' MUST NOT be filled in, 'requested IP address'
612 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
613 client's IP address. In this situation, the client is completely
614 configured, and is trying to extend its lease. This message will
615 be unicast, so no relay agents will be involved in its
616 transmission. Because 'giaddr' is therefore not filled in, the
617 DHCP server will trust the value in 'ciaddr', and use it when
618 replying to the client.
619
620 A client MAY choose to renew or extend its lease prior to T1. The
621 server may choose not to extend the lease (as a policy decision by
622 the network administrator), but should return a DHCPACK message
623 regardless.
624
625o DHCPREQUEST generated during REBINDING state:
626
627 'server identifier' MUST NOT be filled in, 'requested IP address'
628 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
629 client's IP address. In this situation, the client is completely
630 configured, and is trying to extend its lease. This message MUST
631 be broadcast to the 0xffffffff IP broadcast address. The DHCP
632 server SHOULD check 'ciaddr' for correctness before replying to
633 the DHCPREQUEST.
634
635 The DHCPREQUEST from a REBINDING client is intended to accommodate
636 sites that have multiple DHCP servers and a mechanism for
637 maintaining consistency among leases managed by multiple servers.
638 A DHCP server MAY extend a client's lease only if it has local
639 administrative authority to do so.
640*/
641 if (!requested_ip_opt) {
642 requested_nip = packet.ciaddr;
643 if (requested_nip == 0) {
644 log1("no requested IP and no ciaddr, ignoring");
645 break;
646 }
647 }
648 if (lease && requested_nip == lease->lease_nip) {
649 /* client requested or configured IP matches the lease.
650 * ACK it, and bump lease expiration time. */
651 send_ACK(&packet, lease->lease_nip);
652 break;
653 }
654 /* No lease for this MAC, or lease IP != requested IP */
655
656 if (server_id_opt /* client is in SELECTING state */
657 || requested_ip_opt /* client is in INIT-REBOOT state */
658 ) {
659 /* "No, we don't have this IP for you" */
660 send_NAK(&packet);
661 } /* else: client is in RENEWING or REBINDING, do not answer */
662
663 break;
664
665 case DHCPDECLINE:
666 /* RFC 2131:
667 * "If the server receives a DHCPDECLINE message,
668 * the client has discovered through some other means
669 * that the suggested network address is already
670 * in use. The server MUST mark the network address
671 * as not available and SHOULD notify the local
672 * sysadmin of a possible configuration problem."
673 *
674 * SERVER_ID must be present,
675 * REQUESTED_IP must be present,
676 * chaddr must be filled in,
677 * ciaddr must be 0 (we do not check this)
678 */
679 log1("Received DECLINE");
680 if (server_id_opt
681 && requested_ip_opt
682 && lease /* chaddr matches this lease */
683 && requested_nip == lease->lease_nip
684 ) {
685 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
686 lease->expires = time(NULL) + server_config.decline_time;
687 }
688 break;
689
690 case DHCPRELEASE:
691 /* "Upon receipt of a DHCPRELEASE message, the server
692 * marks the network address as not allocated."
693 *
694 * SERVER_ID must be present,
695 * REQUESTED_IP must not be present (we do not check this),
696 * chaddr must be filled in,
697 * ciaddr must be filled in
698 */
699 log1("Received RELEASE");
700 if (server_id_opt
701 && lease /* chaddr matches this lease */
702 && packet.ciaddr == lease->lease_nip
703 ) {
704 lease->expires = time(NULL);
705 }
706 break;
707
708 case DHCPINFORM:
709 log1("Received INFORM");
710 send_inform(&packet);
711 break;
712 }
713 }
714 ret0:
715 retval = 0;
716 ret:
717 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
718 remove_pidfile(server_config.pidfile);
719 return retval;
720}