blob: 043220de9bdc24e3840cfc7ea76873355874fea9 [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 */
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +000023#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000024#include "common.h"
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000025#include "dhcpc.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000026#include "dhcpd.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000027
28
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010029/* Send a packet to a specific mac address and ip address by creating our own ip packet */
30static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010031{
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))
Denys Vlasenko53f72bb2010-03-21 06:46:09 +010049 || dhcp_pkt->ciaddr == 0
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010050 ) {
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
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010060 udhcp_send_raw_packet(dhcp_pkt,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010061 /*src*/ server_config.server_nip, SERVER_PORT,
62 /*dst*/ ciaddr, CLIENT_PORT, chaddr,
63 server_config.ifindex);
64}
65
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010066/* Send a packet to gateway_nip using the kernel ip stack */
67static 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
76static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010077{
78 if (dhcp_pkt->gateway_nip)
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010079 send_packet_to_relay(dhcp_pkt);
80 else
81 send_packet_to_client(dhcp_pkt, force_broadcast);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010082}
83
84static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
85{
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010086 /* Sets op, htype, hlen, cookie fields
87 * and adds DHCP_MESSAGE_TYPE option */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010088 udhcp_init_header(packet, type);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010089
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010090 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;
Denys Vlasenko7724c762010-03-26 09:32:09 +010095 udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +010096}
97
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010098/* Fill options field, siaddr_nip, and sname and boot_file fields.
99 * TODO: teach this code to use overload option.
100 */
101static void add_server_options(struct dhcp_packet *packet)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100102{
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100103 struct option_set *curr = server_config.options;
104
105 while (curr) {
106 if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
Denys Vlasenko7724c762010-03-26 09:32:09 +0100107 udhcp_add_binary_option(packet, curr->data);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100108 curr = curr->next;
109 }
110
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100111 packet->siaddr_nip = server_config.siaddr_nip;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100112
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100113 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
119static uint32_t select_lease_time(struct dhcp_packet *packet)
120{
121 uint32_t lease_time_sec = server_config.max_lease_sec;
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100122 uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100123 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
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100134/* We got a DHCP DISCOVER. Send an OFFER. */
135static void send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100136{
137 struct dhcp_packet packet;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100138 uint32_t lease_time_sec;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100139 struct in_addr addr;
140
141 init_packet(&packet, oldpacket, DHCPOFFER);
142
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100143 /* If it is a static lease, use its IP */
144 packet.yiaddr = static_lease_nip;
145 /* Else: */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100146 if (!static_lease_nip) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100147 /* We have no static lease for client's chaddr */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100148 uint32_t req_nip;
149 uint8_t *req_ip_opt;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100150 const char *p_host_name;
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100151
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100152 if (lease) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100153 /* We have a dynamic lease for client's chaddr.
154 * Reuse its IP (even if lease is expired).
155 * Note that we ignore requested IP in this case.
156 */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100157 packet.yiaddr = lease->lease_nip;
158 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100159 /* Or: if client has requested an IP */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100160 else if ((req_ip_opt = udhcp_get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100161 /* (read IP) */
162 && (move_from_unaligned32(req_nip, req_ip_opt), 1)
163 /* and the IP is in the lease range */
164 && ntohl(req_nip) >= server_config.start_ip
165 && ntohl(req_nip) <= server_config.end_ip
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100166 /* and */
167 && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
168 || is_expired_lease(lease) /* or is taken, but expired */
169 )
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100170 ) {
171 packet.yiaddr = req_nip;
172 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100173 else {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100174 /* Otherwise, find a free IP */
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100175 packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr);
176 }
177
178 if (!packet.yiaddr) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100179 bb_error_msg("no free IP addresses. OFFER abandoned");
180 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100181 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100182 /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100183 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100184 lease = add_lease(packet.chaddr, packet.yiaddr,
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100185 server_config.offer_time,
186 p_host_name,
187 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100188 );
189 if (!lease) {
190 bb_error_msg("no free IP addresses. OFFER abandoned");
191 return;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100192 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100193 }
194
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100195 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100196 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100197 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100198
199 addr.s_addr = packet.yiaddr;
200 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100201 /* send_packet emits error message itself if it detects failure */
202 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100203}
204
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100205static void send_NAK(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100206{
207 struct dhcp_packet packet;
208
209 init_packet(&packet, oldpacket, DHCPNAK);
210
211 log1("Sending NAK");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100212 send_packet(&packet, /*force_bcast:*/ 1);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100213}
214
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100215static void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100216{
217 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100218 uint32_t lease_time_sec;
219 struct in_addr addr;
220 const char *p_host_name;
221
222 init_packet(&packet, oldpacket, DHCPACK);
223 packet.yiaddr = yiaddr;
224
225 lease_time_sec = select_lease_time(oldpacket);
Denys Vlasenko7724c762010-03-26 09:32:09 +0100226 udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100227
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100228 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100229
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100230 addr.s_addr = yiaddr;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100231 bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100232 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100233
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100234 p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100235 add_lease(packet.chaddr, packet.yiaddr,
236 lease_time_sec,
237 p_host_name,
238 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
239 );
240 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
241 /* rewrite the file with leases at every new acceptance */
242 write_leases();
243 }
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100244}
245
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100246static void send_inform(struct dhcp_packet *oldpacket)
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100247{
248 struct dhcp_packet packet;
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100249
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200250 /* "If a client has obtained a network address through some other means
251 * (e.g., manual configuration), it may use a DHCPINFORM request message
252 * to obtain other local configuration parameters. Servers receiving a
253 * DHCPINFORM message construct a DHCPACK message with any local
254 * configuration parameters appropriate for the client without:
255 * allocating a new address, checking for an existing binding, filling
256 * in 'yiaddr' or including lease time parameters. The servers SHOULD
257 * unicast the DHCPACK reply to the address given in the 'ciaddr' field
258 * of the DHCPINFORM message.
259 * ...
260 * The server responds to a DHCPINFORM message by sending a DHCPACK
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100261 * message directly to the address given in the 'ciaddr' field
262 * of the DHCPINFORM message. The server MUST NOT send a lease
263 * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
264 */
Denys Vlasenkof8fcc182010-04-04 22:36:34 +0200265//TODO: do a few sanity checks: is ciaddr set?
266//Better yet: is ciaddr == IP source addr?
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100267 init_packet(&packet, oldpacket, DHCPACK);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100268 add_server_options(&packet);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100269
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100270 send_packet(&packet, /*force_bcast:*/ 0);
Denys Vlasenko8a7c1662010-03-20 03:48:11 +0100271}
272
273
Mike Frysinger7031f622006-05-08 03:20:50 +0000274/* globals */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200275struct dyn_lease *g_leases;
Denis Vlasenkodeabacd2007-09-30 17:55:43 +0000276/* struct server_config_t server_config is in bb_common_bufsiz1 */
Mike Frysinger7031f622006-05-08 03:20:50 +0000277
278
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000279int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000280int udhcpd_main(int argc UNUSED_PARAM, char **argv)
Mike Frysinger7031f622006-05-08 03:20:50 +0000281{
282 fd_set rfds;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000283 int server_socket = -1, retval, max_sock;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200284 struct dhcp_packet packet;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200285 uint8_t *state;
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100286 uint32_t static_lease_nip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000287 unsigned timeout_end;
288 unsigned num_ips;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000289 unsigned opt;
Mike Frysinger7031f622006-05-08 03:20:50 +0000290 struct option_set *option;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200291 struct dyn_lease *lease, fake_lease;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000292 IF_FEATURE_UDHCP_PORT(char *str_P;)
Mike Frysinger7031f622006-05-08 03:20:50 +0000293
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000294#if ENABLE_FEATURE_UDHCP_PORT
295 SERVER_PORT = 67;
296 CLIENT_PORT = 68;
297#endif
298
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200299#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
300 opt_complementary = "vv";
301#endif
302 opt = getopt32(argv, "fSv"
303 IF_FEATURE_UDHCP_PORT("P:", &str_P)
304#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
305 , &dhcp_verbose
306#endif
307 );
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000308 if (!(opt & 1)) { /* no -f */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000309 bb_daemonize_or_rexec(0, argv);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000310 logmode = LOGMODE_NONE;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000311 }
Mike Frysinger6db13732010-06-04 13:24:50 -0400312 /* update argv after the possible vfork+exec in daemonize */
313 argv += optind;
Denis Vlasenko3d17d2b2007-08-14 16:45:29 +0000314 if (opt & 2) { /* -S */
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000315 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000316 logmode |= LOGMODE_SYSLOG;
317 }
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000318#if ENABLE_FEATURE_UDHCP_PORT
Denys Vlasenko406bd142010-03-27 23:24:57 +0100319 if (opt & 8) { /* -P */
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +0000320 SERVER_PORT = xatou16(str_P);
321 CLIENT_PORT = SERVER_PORT + 1;
322 }
323#endif
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000324 /* Would rather not do read_config before daemonization -
325 * otherwise NOMMU machines will parse config twice */
Denis Vlasenko9f7b92a2007-08-15 20:03:36 +0000326 read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000327
Denis Vlasenko80edead2007-08-02 22:31:05 +0000328 /* Make sure fd 0,1,2 are open */
329 bb_sanitize_stdio();
330 /* Equivalent of doing a fflush after every \n */
331 setlinebuf(stdout);
332
333 /* Create pidfile */
334 write_pidfile(server_config.pidfile);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100335 /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000336
Denis Vlasenkodef88982007-10-07 17:06:01 +0000337 bb_info_msg("%s (v"BB_VER") started", applet_name);
Mike Frysinger7031f622006-05-08 03:20:50 +0000338
Denys Vlasenko7724c762010-03-26 09:32:09 +0100339 option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
Denys Vlasenko2e7aa922010-03-21 02:22:07 +0100340 server_config.max_lease_sec = DEFAULT_LEASE_TIME;
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000341 if (option) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200342 move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
343 server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000344 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000345
346 /* Sanity check */
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000347 num_ips = server_config.end_ip - server_config.start_ip + 1;
Mike Frysinger7031f622006-05-08 03:20:50 +0000348 if (server_config.max_leases > num_ips) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000349 bb_error_msg("max_leases=%u is too big, setting to %u",
350 (unsigned)server_config.max_leases, num_ips);
Mike Frysinger7031f622006-05-08 03:20:50 +0000351 server_config.max_leases = num_ips;
352 }
353
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200354 g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
Mike Frysinger7031f622006-05-08 03:20:50 +0000355 read_leases(server_config.lease_file);
356
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200357 if (udhcp_read_interface(server_config.interface,
358 &server_config.ifindex,
359 &server_config.server_nip,
360 server_config.server_mac)
361 ) {
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000362 retval = 1;
363 goto ret;
364 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000365
Mike Frysinger7031f622006-05-08 03:20:50 +0000366 /* Setup the signal pipe */
367 udhcp_sp_setup();
368
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000369 timeout_end = monotonic_sec() + server_config.auto_time;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000370 while (1) { /* loop until universe collapses */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000371 int bytes;
372 struct timeval tv;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100373 uint8_t *server_id_opt;
374 uint8_t *requested_opt;
375 uint32_t requested_nip = requested_nip; /* for compiler */
Mike Frysinger7031f622006-05-08 03:20:50 +0000376
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000377 if (server_socket < 0) {
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000378 server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000379 server_config.interface);
Denis Vlasenkoe2d3ded2006-11-27 23:43:28 +0000380 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000381
382 max_sock = udhcp_sp_fd_set(&rfds, server_socket);
383 if (server_config.auto_time) {
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000384 tv.tv_sec = timeout_end - monotonic_sec();
Mike Frysinger7031f622006-05-08 03:20:50 +0000385 tv.tv_usec = 0;
386 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000387 retval = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000388 if (!server_config.auto_time || tv.tv_sec > 0) {
389 retval = select(max_sock + 1, &rfds, NULL, NULL,
390 server_config.auto_time ? &tv : NULL);
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000391 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000392 if (retval == 0) {
393 write_leases();
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000394 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000395 continue;
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000396 }
397 if (retval < 0 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200398 log1("Error on select");
Mike Frysinger7031f622006-05-08 03:20:50 +0000399 continue;
400 }
401
402 switch (udhcp_sp_read(&rfds)) {
403 case SIGUSR1:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100404 bb_info_msg("Received SIGUSR1");
Mike Frysinger7031f622006-05-08 03:20:50 +0000405 write_leases();
406 /* why not just reset the timeout, eh */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000407 timeout_end = monotonic_sec() + server_config.auto_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000408 continue;
409 case SIGTERM:
Denys Vlasenko651a2692010-03-23 16:25:17 +0100410 bb_info_msg("Received SIGTERM");
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000411 goto ret0;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000412 case 0: /* no signal: read a packet */
413 break;
414 default: /* signal or error (probably EINTR): back to select */
415 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000416 }
417
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000418 bytes = udhcp_recv_kernel_packet(&packet, server_socket);
Denis Vlasenkoaf1c8432007-03-26 13:22:35 +0000419 if (bytes < 0) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000420 /* bytes can also be -2 ("bad packet data") */
Mike Frysinger7031f622006-05-08 03:20:50 +0000421 if (bytes == -1 && errno != EINTR) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200422 log1("Read error: %s, reopening socket", strerror(errno));
Mike Frysinger7031f622006-05-08 03:20:50 +0000423 close(server_socket);
424 server_socket = -1;
425 }
426 continue;
427 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200428 if (packet.hlen != 6) {
429 bb_error_msg("MAC length != 6, ignoring packet");
430 continue;
431 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100432 if (packet.op != BOOTREQUEST) {
433 bb_error_msg("not a REQUEST, ignoring packet");
434 continue;
435 }
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100436 state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100437 if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
438 bb_error_msg("no or bad message type option, ignoring packet");
Mike Frysinger7031f622006-05-08 03:20:50 +0000439 continue;
440 }
441
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100442 /* Look for a static/dynamic lease */
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100443 static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
444 if (static_lease_nip) {
445 bb_info_msg("Found static lease: %x", static_lease_nip);
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200446 memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
Denys Vlasenkoa9539872010-03-20 03:49:27 +0100447 fake_lease.lease_nip = static_lease_nip;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200448 fake_lease.expires = 0;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200449 lease = &fake_lease;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000450 } else {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200451 lease = find_lease_by_mac(packet.chaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +0000452 }
453
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100454 /* Get REQUESTED_IP and SERVER_ID if present */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100455 server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100456 if (server_id_opt) {
457 uint32_t server_id_net;
458 move_from_unaligned32(server_id_net, server_id_opt);
459 if (server_id_net != server_config.server_nip) {
460 /* client talks to somebody else */
461 log1("server ID doesn't match, ignoring");
462 continue;
463 }
464 }
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100465 requested_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100466 if (requested_opt) {
467 move_from_unaligned32(requested_nip, requested_opt);
468 }
469
Mike Frysinger7031f622006-05-08 03:20:50 +0000470 switch (state[0]) {
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100471
Mike Frysinger7031f622006-05-08 03:20:50 +0000472 case DHCPDISCOVER:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200473 log1("Received DISCOVER");
Mike Frysinger7031f622006-05-08 03:20:50 +0000474
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100475 send_offer(&packet, static_lease_nip, lease);
Mike Frysinger7031f622006-05-08 03:20:50 +0000476 break;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200477
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100478 case DHCPREQUEST:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200479 log1("Received REQUEST");
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100480/* RFC 2131:
Mike Frysinger7031f622006-05-08 03:20:50 +0000481
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100482o DHCPREQUEST generated during SELECTING state:
483
484 Client inserts the address of the selected server in 'server
485 identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
486 filled in with the yiaddr value from the chosen DHCPOFFER.
487
488 Note that the client may choose to collect several DHCPOFFER
489 messages and select the "best" offer. The client indicates its
490 selection by identifying the offering server in the DHCPREQUEST
491 message. If the client receives no acceptable offers, the client
492 may choose to try another DHCPDISCOVER message. Therefore, the
493 servers may not receive a specific DHCPREQUEST from which they can
494 decide whether or not the client has accepted the offer.
495
496o DHCPREQUEST generated during INIT-REBOOT state:
497
498 'server identifier' MUST NOT be filled in, 'requested IP address'
499 option MUST be filled in with client's notion of its previously
500 assigned address. 'ciaddr' MUST be zero. The client is seeking to
501 verify a previously allocated, cached configuration. Server SHOULD
502 send a DHCPNAK message to the client if the 'requested IP address'
503 is incorrect, or is on the wrong network.
504
505 Determining whether a client in the INIT-REBOOT state is on the
506 correct network is done by examining the contents of 'giaddr', the
507 'requested IP address' option, and a database lookup. If the DHCP
508 server detects that the client is on the wrong net (i.e., the
509 result of applying the local subnet mask or remote subnet mask (if
510 'giaddr' is not zero) to 'requested IP address' option value
511 doesn't match reality), then the server SHOULD send a DHCPNAK
512 message to the client.
513
514 If the network is correct, then the DHCP server should check if
515 the client's notion of its IP address is correct. If not, then the
516 server SHOULD send a DHCPNAK message to the client. If the DHCP
517 server has no record of this client, then it MUST remain silent,
518 and MAY output a warning to the network administrator. This
519 behavior is necessary for peaceful coexistence of non-
520 communicating DHCP servers on the same wire.
521
522 If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
523 the same subnet as the server. The server MUST broadcast the
524 DHCPNAK message to the 0xffffffff broadcast address because the
525 client may not have a correct network address or subnet mask, and
526 the client may not be answering ARP requests.
527
528 If 'giaddr' is set in the DHCPREQUEST message, the client is on a
529 different subnet. The server MUST set the broadcast bit in the
530 DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
531 client, because the client may not have a correct network address
532 or subnet mask, and the client may not be answering ARP requests.
533
534o DHCPREQUEST generated during RENEWING state:
535
536 'server identifier' MUST NOT be filled in, 'requested IP address'
537 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
538 client's IP address. In this situation, the client is completely
539 configured, and is trying to extend its lease. This message will
540 be unicast, so no relay agents will be involved in its
541 transmission. Because 'giaddr' is therefore not filled in, the
542 DHCP server will trust the value in 'ciaddr', and use it when
543 replying to the client.
544
545 A client MAY choose to renew or extend its lease prior to T1. The
546 server may choose not to extend the lease (as a policy decision by
547 the network administrator), but should return a DHCPACK message
548 regardless.
549
550o DHCPREQUEST generated during REBINDING state:
551
552 'server identifier' MUST NOT be filled in, 'requested IP address'
553 option MUST NOT be filled in, 'ciaddr' MUST be filled in with
554 client's IP address. In this situation, the client is completely
555 configured, and is trying to extend its lease. This message MUST
556 be broadcast to the 0xffffffff IP broadcast address. The DHCP
557 server SHOULD check 'ciaddr' for correctness before replying to
558 the DHCPREQUEST.
559
560 The DHCPREQUEST from a REBINDING client is intended to accommodate
561 sites that have multiple DHCP servers and a mechanism for
562 maintaining consistency among leases managed by multiple servers.
563 A DHCP server MAY extend a client's lease only if it has local
564 administrative authority to do so.
565*/
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100566 if (!requested_opt) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100567 requested_nip = packet.ciaddr;
568 if (requested_nip == 0) {
569 log1("no requested IP and no ciaddr, ignoring");
570 break;
571 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100572 }
573 if (lease && requested_nip == lease->lease_nip) {
Denys Vlasenko53f72bb2010-03-21 06:46:09 +0100574 /* client requested or configured IP matches the lease.
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100575 * ACK it, and bump lease expiration time. */
576 send_ACK(&packet, lease->lease_nip);
577 break;
578 }
579 if (server_id_opt) {
580 /* client was talking specifically to us.
581 * "No, we don't have this IP for you". */
582 send_NAK(&packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000583 }
584 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100585
Mike Frysinger7031f622006-05-08 03:20:50 +0000586 case DHCPDECLINE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100587 /* RFC 2131:
588 * "If the server receives a DHCPDECLINE message,
589 * the client has discovered through some other means
590 * that the suggested network address is already
591 * in use. The server MUST mark the network address
592 * as not available and SHOULD notify the local
593 * sysadmin of a possible configuration problem."
594 *
595 * SERVER_ID must be present,
596 * REQUESTED_IP must be present,
597 * chaddr must be filled in,
598 * ciaddr must be 0 (we do not check this)
599 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200600 log1("Received DECLINE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100601 if (server_id_opt
602 && requested_opt
603 && lease /* chaddr matches this lease */
604 && requested_nip == lease->lease_nip
605 ) {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200606 memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
Denis Vlasenko04158e02009-02-02 10:48:06 +0000607 lease->expires = time(NULL) + server_config.decline_time;
Mike Frysinger7031f622006-05-08 03:20:50 +0000608 }
609 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100610
Mike Frysinger7031f622006-05-08 03:20:50 +0000611 case DHCPRELEASE:
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100612 /* "Upon receipt of a DHCPRELEASE message, the server
613 * marks the network address as not allocated."
614 *
615 * SERVER_ID must be present,
616 * REQUESTED_IP must not be present (we do not check this),
617 * chaddr must be filled in,
618 * ciaddr must be filled in
619 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200620 log1("Received RELEASE");
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100621 if (server_id_opt
622 && lease /* chaddr matches this lease */
623 && packet.ciaddr == lease->lease_nip
624 ) {
Denis Vlasenko04158e02009-02-02 10:48:06 +0000625 lease->expires = time(NULL);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100626 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000627 break;
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100628
Mike Frysinger7031f622006-05-08 03:20:50 +0000629 case DHCPINFORM:
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200630 log1("Received INFORM");
Mike Frysinger7031f622006-05-08 03:20:50 +0000631 send_inform(&packet);
632 break;
Mike Frysinger7031f622006-05-08 03:20:50 +0000633 }
634 }
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000635 ret0:
636 retval = 0;
637 ret:
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000638 /*if (server_config.pidfile) - server_config.pidfile is never NULL */
Denis Vlasenko6e6d3312007-05-03 23:39:35 +0000639 remove_pidfile(server_config.pidfile);
640 return retval;
Mike Frysinger7031f622006-05-08 03:20:50 +0000641}