dhcpd.c 21 KB

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