1 /* vi: set sw=4 ts=4: */
4 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
5 * Chris Trew <ctrew@moreton.com.au>
7 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
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.
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.
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.
31 /* send a packet to gateway_nip using the kernel ip stack */
32 static int send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
34 log1("Forwarding packet to relay");
36 return udhcp_send_kernel_packet(dhcp_pkt,
37 server_config.server_nip, SERVER_PORT,
38 dhcp_pkt->gateway_nip, SERVER_PORT);
41 /* send a packet to a specific mac address and ip address by creating our own ip packet */
42 static int send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
44 const uint8_t *chaddr;
48 //if (force_broadcast) { /* broadcast */ }
49 //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
50 //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
51 //else { /* unicast to dhcp_pkt->yiaddr */ }
52 // But this is wrong: yiaddr is _our_ idea what client's IP is
53 // (for example, from lease file). Client may not know that,
54 // and may not have UDP socket listening on that IP!
55 // We should never unicast to dhcp_pkt->yiaddr!
56 // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
60 || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
63 log1("Broadcasting packet to client");
64 ciaddr = INADDR_BROADCAST;
65 chaddr = MAC_BCAST_ADDR;
67 log1("Unicasting packet to client ciaddr");
68 ciaddr = dhcp_pkt->ciaddr;
69 chaddr = dhcp_pkt->chaddr;
72 return udhcp_send_raw_packet(dhcp_pkt,
73 /*src*/ server_config.server_nip, SERVER_PORT,
74 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
75 server_config.ifindex);
78 /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
79 static int send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
81 if (dhcp_pkt->gateway_nip)
82 return send_packet_to_relay(dhcp_pkt);
83 return send_packet_to_client(dhcp_pkt, force_broadcast);
86 static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
88 udhcp_init_header(packet, type);
89 packet->xid = oldpacket->xid;
90 memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
91 packet->flags = oldpacket->flags;
92 packet->gateway_nip = oldpacket->gateway_nip;
93 packet->ciaddr = oldpacket->ciaddr;
94 add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip);
97 /* add in the bootp options */
98 static void add_bootp_options(struct dhcp_packet *packet)
100 packet->siaddr_nip = server_config.siaddr_nip;
101 if (server_config.sname)
102 strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
103 if (server_config.boot_file)
104 strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
107 static uint32_t select_lease_time(struct dhcp_packet *packet)
109 uint32_t lease_time_sec = server_config.max_lease_sec;
110 uint8_t *lease_time_opt = get_option(packet, DHCP_LEASE_TIME);
111 if (lease_time_opt) {
112 move_from_unaligned32(lease_time_sec, lease_time_opt);
113 lease_time_sec = ntohl(lease_time_sec);
114 if (lease_time_sec > server_config.max_lease_sec)
115 lease_time_sec = server_config.max_lease_sec;
116 if (lease_time_sec < server_config.min_lease_sec)
117 lease_time_sec = server_config.min_lease_sec;
119 return lease_time_sec;
122 /* send a DHCP OFFER to a DHCP DISCOVER */
123 static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease)
125 struct dhcp_packet packet;
127 uint32_t lease_time_sec = server_config.max_lease_sec;
129 const char *p_host_name;
130 struct option_set *curr;
133 init_packet(&packet, oldpacket, DHCPOFFER);
135 /* ADDME: if static, short circuit */
136 if (!static_lease_nip) {
137 /* The client is in our lease/offered table */
139 packet.yiaddr = lease->lease_nip;
141 /* Or the client has requested an IP */
142 else if ((req_ip_opt = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL
144 && (move_from_unaligned32(req_nip, req_ip_opt), 1)
145 /* and the IP is in the lease range */
146 && ntohl(req_nip) >= server_config.start_ip
147 && ntohl(req_nip) <= server_config.end_ip
148 /* and is not already taken/offered */
149 && (!(lease = find_lease_by_nip(req_nip))
150 /* or its taken, but expired */
151 || is_expired_lease(lease))
153 packet.yiaddr = req_nip;
155 /* Otherwise, find a free IP */
157 packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr);
160 if (!packet.yiaddr) {
161 bb_error_msg("no IP addresses to give - OFFER abandoned");
164 p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME);
165 if (add_lease(packet.chaddr, packet.yiaddr,
166 server_config.offer_time,
168 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
171 bb_error_msg("lease pool is full - OFFER abandoned");
174 lease_time_sec = select_lease_time(oldpacket);
176 /* It is a static lease... use it */
177 packet.yiaddr = static_lease_nip;
180 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
182 curr = server_config.options;
184 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
185 add_option_string(packet.options, curr->data);
189 add_bootp_options(&packet);
191 addr.s_addr = packet.yiaddr;
192 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
193 return send_packet(&packet, 0);
196 static int send_NAK(struct dhcp_packet *oldpacket)
198 struct dhcp_packet packet;
200 init_packet(&packet, oldpacket, DHCPNAK);
203 return send_packet(&packet, 1);
206 static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
208 struct dhcp_packet packet;
209 struct option_set *curr;
210 uint32_t lease_time_sec;
212 const char *p_host_name;
214 init_packet(&packet, oldpacket, DHCPACK);
215 packet.yiaddr = yiaddr;
217 lease_time_sec = select_lease_time(oldpacket);
219 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
221 curr = server_config.options;
223 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
224 add_option_string(packet.options, curr->data);
228 add_bootp_options(&packet);
230 addr.s_addr = packet.yiaddr;
231 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
233 if (send_packet(&packet, 0) < 0)
236 p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME);
237 add_lease(packet.chaddr, packet.yiaddr,
240 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
242 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
243 /* rewrite the file with leases at every new acceptance */
250 static int send_inform(struct dhcp_packet *oldpacket)
252 struct dhcp_packet packet;
253 struct option_set *curr;
255 init_packet(&packet, oldpacket, DHCPACK);
257 curr = server_config.options;
259 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
260 add_option_string(packet.options, curr->data);
264 add_bootp_options(&packet);
266 return send_packet(&packet, 0);
271 struct dyn_lease *g_leases;
272 /* struct server_config_t server_config is in bb_common_bufsiz1 */
275 int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
276 int udhcpd_main(int argc UNUSED_PARAM, char **argv)
279 int server_socket = -1, retval, max_sock;
280 struct dhcp_packet packet;
282 uint32_t static_lease_nip;
283 unsigned timeout_end;
286 struct option_set *option;
287 struct dyn_lease *lease, fake_lease;
288 IF_FEATURE_UDHCP_PORT(char *str_P;)
290 #if ENABLE_FEATURE_UDHCP_PORT
295 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
296 opt_complementary = "vv";
298 opt = getopt32(argv, "fSv"
299 IF_FEATURE_UDHCP_PORT("P:", &str_P)
300 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
305 if (!(opt & 1)) { /* no -f */
306 bb_daemonize_or_rexec(0, argv);
307 logmode = LOGMODE_NONE;
309 if (opt & 2) { /* -S */
310 openlog(applet_name, LOG_PID, LOG_DAEMON);
311 logmode |= LOGMODE_SYSLOG;
313 #if ENABLE_FEATURE_UDHCP_PORT
314 if (opt & 4) { /* -P */
315 SERVER_PORT = xatou16(str_P);
316 CLIENT_PORT = SERVER_PORT + 1;
319 /* Would rather not do read_config before daemonization -
320 * otherwise NOMMU machines will parse config twice */
321 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
323 /* Make sure fd 0,1,2 are open */
325 /* Equivalent of doing a fflush after every \n */
329 write_pidfile(server_config.pidfile);
330 /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
332 bb_info_msg("%s (v"BB_VER") started", applet_name);
334 option = find_option(server_config.options, DHCP_LEASE_TIME);
335 server_config.max_lease_sec = LEASE_TIME;
337 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
338 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
342 num_ips = server_config.end_ip - server_config.start_ip + 1;
343 if (server_config.max_leases > num_ips) {
344 bb_error_msg("max_leases=%u is too big, setting to %u",
345 (unsigned)server_config.max_leases, num_ips);
346 server_config.max_leases = num_ips;
349 g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
350 read_leases(server_config.lease_file);
352 if (udhcp_read_interface(server_config.interface,
353 &server_config.ifindex,
354 &server_config.server_nip,
355 server_config.server_mac)
361 /* Setup the signal pipe */
364 timeout_end = monotonic_sec() + server_config.auto_time;
365 while (1) { /* loop until universe collapses */
369 if (server_socket < 0) {
370 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
371 server_config.interface);
374 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
375 if (server_config.auto_time) {
376 tv.tv_sec = timeout_end - monotonic_sec();
380 if (!server_config.auto_time || tv.tv_sec > 0) {
381 retval = select(max_sock + 1, &rfds, NULL, NULL,
382 server_config.auto_time ? &tv : NULL);
386 timeout_end = monotonic_sec() + server_config.auto_time;
389 if (retval < 0 && errno != EINTR) {
390 log1("Error on select");
394 switch (udhcp_sp_read(&rfds)) {
396 bb_info_msg("Received a SIGUSR1");
398 /* why not just reset the timeout, eh */
399 timeout_end = monotonic_sec() + server_config.auto_time;
402 bb_info_msg("Received a SIGTERM");
404 case 0: /* no signal: read a packet */
406 default: /* signal or error (probably EINTR): back to select */
410 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
412 /* bytes can also be -2 ("bad packet data") */
413 if (bytes == -1 && errno != EINTR) {
414 log1("Read error: %s, reopening socket", strerror(errno));
415 close(server_socket);
421 if (packet.hlen != 6) {
422 bb_error_msg("MAC length != 6, ignoring packet");
426 state = get_option(&packet, DHCP_MESSAGE_TYPE);
428 bb_error_msg("no message type option, ignoring packet");
432 /* Look for a static lease */
433 static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
434 if (static_lease_nip) {
435 bb_info_msg("Found static lease: %x", static_lease_nip);
437 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
438 fake_lease.lease_nip = static_lease_nip;
439 fake_lease.expires = 0;
443 lease = find_lease_by_mac(packet.chaddr);
448 log1("Received DISCOVER");
450 if (send_offer(&packet, static_lease_nip, lease) < 0) {
451 bb_error_msg("send OFFER failed");
455 uint8_t *server_id_opt, *requested_opt;
456 uint32_t server_id_net = server_id_net; /* for compiler */
457 uint32_t requested_nip = requested_nip; /* for compiler */
459 log1("Received REQUEST");
461 requested_opt = get_option(&packet, DHCP_REQUESTED_IP);
462 server_id_opt = get_option(&packet, DHCP_SERVER_ID);
464 move_from_unaligned32(requested_nip, requested_opt);
466 move_from_unaligned32(server_id_net, server_id_opt);
470 /* SELECTING State */
471 if (server_id_net == server_config.server_nip
473 && requested_nip == lease->lease_nip
475 send_ACK(&packet, lease->lease_nip);
477 } else if (requested_opt) {
478 /* INIT-REBOOT State */
479 if (lease->lease_nip == requested_nip)
480 send_ACK(&packet, lease->lease_nip);
483 } else if (lease->lease_nip == packet.ciaddr) {
484 /* RENEWING or REBINDING State */
485 send_ACK(&packet, lease->lease_nip);
486 } else { /* don't know what to do!!!! */
490 /* what to do if we have no record of the client */
491 } else if (server_id_opt) {
492 /* SELECTING State */
494 } else if (requested_opt) {
495 /* INIT-REBOOT State */
496 lease = find_lease_by_nip(requested_nip);
498 if (is_expired_lease(lease)) {
499 /* probably best if we drop this lease */
500 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
502 /* make some contention for this address */
506 uint32_t r = ntohl(requested_nip);
507 if (r < server_config.start_ip
508 || r > server_config.end_ip
512 /* else remain silent */
516 /* RENEWING or REBINDING State */
521 log1("Received DECLINE");
523 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
524 lease->expires = time(NULL) + server_config.decline_time;
528 log1("Received RELEASE");
530 lease->expires = time(NULL);
533 log1("Received INFORM");
534 send_inform(&packet);
537 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
543 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
544 remove_pidfile(server_config.pidfile);