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