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