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)
125 struct dhcp_packet packet;
127 uint32_t lease_time_sec = server_config.max_lease_sec;
128 uint32_t static_lease_ip;
130 const char *p_host_name;
131 struct option_set *curr;
134 init_packet(&packet, oldpacket, DHCPOFFER);
136 static_lease_ip = get_static_nip_by_mac(server_config.static_leases, oldpacket->chaddr);
138 /* ADDME: if static, short circuit */
139 if (!static_lease_ip) {
140 struct dyn_lease *lease;
142 lease = find_lease_by_mac(oldpacket->chaddr);
143 /* The client is in our lease/offered table */
145 signed_leasetime_t tmp = lease->expires - time(NULL);
147 lease_time_sec = tmp;
148 packet.yiaddr = lease->lease_nip;
150 /* Or the client has requested an IP */
151 else if ((req_ip_opt = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL
153 && (move_from_unaligned32(req_nip, req_ip_opt), 1)
154 /* and the IP is in the lease range */
155 && ntohl(req_nip) >= server_config.start_ip
156 && ntohl(req_nip) <= server_config.end_ip
157 /* and is not already taken/offered */
158 && (!(lease = find_lease_by_nip(req_nip))
159 /* or its taken, but expired */
160 || is_expired_lease(lease))
162 packet.yiaddr = req_nip;
164 /* Otherwise, find a free IP */
166 packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr);
169 if (!packet.yiaddr) {
170 bb_error_msg("no IP addresses to give - OFFER abandoned");
173 p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME);
174 if (add_lease(packet.chaddr, packet.yiaddr,
175 server_config.offer_time,
177 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
180 bb_error_msg("lease pool is full - OFFER abandoned");
183 lease_time_sec = select_lease_time(oldpacket);
185 /* It is a static lease... use it */
186 packet.yiaddr = static_lease_ip;
189 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
191 curr = server_config.options;
193 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
194 add_option_string(packet.options, curr->data);
198 add_bootp_options(&packet);
200 addr.s_addr = packet.yiaddr;
201 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
202 return send_packet(&packet, 0);
205 static int send_NAK(struct dhcp_packet *oldpacket)
207 struct dhcp_packet packet;
209 init_packet(&packet, oldpacket, DHCPNAK);
212 return send_packet(&packet, 1);
215 static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
217 struct dhcp_packet packet;
218 struct option_set *curr;
219 uint32_t lease_time_sec;
221 const char *p_host_name;
223 init_packet(&packet, oldpacket, DHCPACK);
224 packet.yiaddr = yiaddr;
226 lease_time_sec = select_lease_time(oldpacket);
228 add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
230 curr = server_config.options;
232 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
233 add_option_string(packet.options, curr->data);
237 add_bootp_options(&packet);
239 addr.s_addr = packet.yiaddr;
240 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
242 if (send_packet(&packet, 0) < 0)
245 p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME);
246 add_lease(packet.chaddr, packet.yiaddr,
249 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
251 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
252 /* rewrite the file with leases at every new acceptance */
259 static int send_inform(struct dhcp_packet *oldpacket)
261 struct dhcp_packet packet;
262 struct option_set *curr;
264 init_packet(&packet, oldpacket, DHCPACK);
266 curr = server_config.options;
268 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
269 add_option_string(packet.options, curr->data);
273 add_bootp_options(&packet);
275 return send_packet(&packet, 0);
280 struct dyn_lease *g_leases;
281 /* struct server_config_t server_config is in bb_common_bufsiz1 */
284 int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
285 int udhcpd_main(int argc UNUSED_PARAM, char **argv)
288 int server_socket = -1, retval, max_sock;
289 struct dhcp_packet packet;
291 uint32_t static_lease_ip;
292 unsigned timeout_end;
295 struct option_set *option;
296 struct dyn_lease *lease, fake_lease;
297 IF_FEATURE_UDHCP_PORT(char *str_P;)
299 #if ENABLE_FEATURE_UDHCP_PORT
304 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
305 opt_complementary = "vv";
307 opt = getopt32(argv, "fSv"
308 IF_FEATURE_UDHCP_PORT("P:", &str_P)
309 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
314 if (!(opt & 1)) { /* no -f */
315 bb_daemonize_or_rexec(0, argv);
316 logmode = LOGMODE_NONE;
318 if (opt & 2) { /* -S */
319 openlog(applet_name, LOG_PID, LOG_DAEMON);
320 logmode |= LOGMODE_SYSLOG;
322 #if ENABLE_FEATURE_UDHCP_PORT
323 if (opt & 4) { /* -P */
324 SERVER_PORT = xatou16(str_P);
325 CLIENT_PORT = SERVER_PORT + 1;
328 /* Would rather not do read_config before daemonization -
329 * otherwise NOMMU machines will parse config twice */
330 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
332 /* Make sure fd 0,1,2 are open */
334 /* Equivalent of doing a fflush after every \n */
338 write_pidfile(server_config.pidfile);
339 /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
341 bb_info_msg("%s (v"BB_VER") started", applet_name);
343 option = find_option(server_config.options, DHCP_LEASE_TIME);
344 server_config.max_lease_sec = LEASE_TIME;
346 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
347 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
351 num_ips = server_config.end_ip - server_config.start_ip + 1;
352 if (server_config.max_leases > num_ips) {
353 bb_error_msg("max_leases=%u is too big, setting to %u",
354 (unsigned)server_config.max_leases, num_ips);
355 server_config.max_leases = num_ips;
358 g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
359 read_leases(server_config.lease_file);
361 if (udhcp_read_interface(server_config.interface,
362 &server_config.ifindex,
363 &server_config.server_nip,
364 server_config.server_mac)
370 /* Setup the signal pipe */
373 timeout_end = monotonic_sec() + server_config.auto_time;
374 while (1) { /* loop until universe collapses */
378 if (server_socket < 0) {
379 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
380 server_config.interface);
383 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
384 if (server_config.auto_time) {
385 tv.tv_sec = timeout_end - monotonic_sec();
389 if (!server_config.auto_time || tv.tv_sec > 0) {
390 retval = select(max_sock + 1, &rfds, NULL, NULL,
391 server_config.auto_time ? &tv : NULL);
395 timeout_end = monotonic_sec() + server_config.auto_time;
398 if (retval < 0 && errno != EINTR) {
399 log1("Error on select");
403 switch (udhcp_sp_read(&rfds)) {
405 bb_info_msg("Received a SIGUSR1");
407 /* why not just reset the timeout, eh */
408 timeout_end = monotonic_sec() + server_config.auto_time;
411 bb_info_msg("Received a SIGTERM");
413 case 0: /* no signal: read a packet */
415 default: /* signal or error (probably EINTR): back to select */
419 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
421 /* bytes can also be -2 ("bad packet data") */
422 if (bytes == -1 && errno != EINTR) {
423 log1("Read error: %s, reopening socket", strerror(errno));
424 close(server_socket);
430 if (packet.hlen != 6) {
431 bb_error_msg("MAC length != 6, ignoring packet");
435 state = get_option(&packet, DHCP_MESSAGE_TYPE);
437 bb_error_msg("no message type option, ignoring packet");
441 /* Look for a static lease */
442 static_lease_ip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
443 if (static_lease_ip) {
444 bb_info_msg("Found static lease: %x", static_lease_ip);
446 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
447 fake_lease.lease_nip = static_lease_ip;
448 fake_lease.expires = 0;
452 lease = find_lease_by_mac(packet.chaddr);
457 log1("Received DISCOVER");
459 if (send_offer(&packet) < 0) {
460 bb_error_msg("send OFFER failed");
464 uint8_t *server_id_opt, *requested_opt;
465 uint32_t server_id_net = server_id_net; /* for compiler */
466 uint32_t requested_nip = requested_nip; /* for compiler */
468 log1("Received REQUEST");
470 requested_opt = get_option(&packet, DHCP_REQUESTED_IP);
471 server_id_opt = get_option(&packet, DHCP_SERVER_ID);
473 move_from_unaligned32(requested_nip, requested_opt);
475 move_from_unaligned32(server_id_net, server_id_opt);
479 /* SELECTING State */
480 if (server_id_net == server_config.server_nip
482 && requested_nip == lease->lease_nip
484 send_ACK(&packet, lease->lease_nip);
486 } else if (requested_opt) {
487 /* INIT-REBOOT State */
488 if (lease->lease_nip == requested_nip)
489 send_ACK(&packet, lease->lease_nip);
492 } else if (lease->lease_nip == packet.ciaddr) {
493 /* RENEWING or REBINDING State */
494 send_ACK(&packet, lease->lease_nip);
495 } else { /* don't know what to do!!!! */
499 /* what to do if we have no record of the client */
500 } else if (server_id_opt) {
501 /* SELECTING State */
503 } else if (requested_opt) {
504 /* INIT-REBOOT State */
505 lease = find_lease_by_nip(requested_nip);
507 if (is_expired_lease(lease)) {
508 /* probably best if we drop this lease */
509 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
511 /* make some contention for this address */
515 uint32_t r = ntohl(requested_nip);
516 if (r < server_config.start_ip
517 || r > server_config.end_ip
521 /* else remain silent */
525 /* RENEWING or REBINDING State */
530 log1("Received DECLINE");
532 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
533 lease->expires = time(NULL) + server_config.decline_time;
537 log1("Received RELEASE");
539 lease->expires = time(NULL);
542 log1("Received INFORM");
543 send_inform(&packet);
546 bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
552 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
553 remove_pidfile(server_config.pidfile);