3
0

dhcpd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. static void send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease)
  120. {
  121. struct dhcp_packet packet;
  122. uint32_t lease_time_sec;
  123. struct in_addr addr;
  124. init_packet(&packet, oldpacket, DHCPOFFER);
  125. /* If it is a static lease, use its IP */
  126. packet.yiaddr = static_lease_nip;
  127. /* Else: */
  128. if (!static_lease_nip) {
  129. /* We have no static lease for client's chaddr */
  130. uint32_t req_nip;
  131. uint8_t *req_ip_opt;
  132. const char *p_host_name;
  133. if (lease) {
  134. /* We have a dynamic lease for client's chaddr.
  135. * Reuse its IP (even if lease is expired).
  136. * Note that we ignore requested IP in this case.
  137. */
  138. packet.yiaddr = lease->lease_nip;
  139. }
  140. /* Or: if client has requested an IP */
  141. else if ((req_ip_opt = udhcp_get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL
  142. /* (read IP) */
  143. && (move_from_unaligned32(req_nip, req_ip_opt), 1)
  144. /* and the IP is in the lease range */
  145. && ntohl(req_nip) >= server_config.start_ip
  146. && ntohl(req_nip) <= server_config.end_ip
  147. /* and */
  148. && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
  149. || is_expired_lease(lease) /* or is taken, but expired */
  150. )
  151. ) {
  152. packet.yiaddr = req_nip;
  153. }
  154. else {
  155. /* Otherwise, find a free IP */
  156. packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr);
  157. }
  158. if (!packet.yiaddr) {
  159. bb_error_msg("no free IP addresses. OFFER abandoned");
  160. return;
  161. }
  162. /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
  163. p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
  164. lease = add_lease(packet.chaddr, packet.yiaddr,
  165. server_config.offer_time,
  166. p_host_name,
  167. p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
  168. );
  169. if (!lease) {
  170. bb_error_msg("no free IP addresses. OFFER abandoned");
  171. return;
  172. }
  173. }
  174. lease_time_sec = select_lease_time(oldpacket);
  175. udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
  176. add_server_options(&packet);
  177. addr.s_addr = packet.yiaddr;
  178. bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
  179. /* send_packet emits error message itself if it detects failure */
  180. send_packet(&packet, /*force_bcast:*/ 0);
  181. }
  182. static void send_NAK(struct dhcp_packet *oldpacket)
  183. {
  184. struct dhcp_packet packet;
  185. init_packet(&packet, oldpacket, DHCPNAK);
  186. log1("Sending NAK");
  187. send_packet(&packet, /*force_bcast:*/ 1);
  188. }
  189. static void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
  190. {
  191. struct dhcp_packet packet;
  192. uint32_t lease_time_sec;
  193. struct in_addr addr;
  194. const char *p_host_name;
  195. init_packet(&packet, oldpacket, DHCPACK);
  196. packet.yiaddr = yiaddr;
  197. lease_time_sec = select_lease_time(oldpacket);
  198. udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
  199. add_server_options(&packet);
  200. addr.s_addr = yiaddr;
  201. bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
  202. send_packet(&packet, /*force_bcast:*/ 0);
  203. p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
  204. add_lease(packet.chaddr, packet.yiaddr,
  205. lease_time_sec,
  206. p_host_name,
  207. p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
  208. );
  209. if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
  210. /* rewrite the file with leases at every new acceptance */
  211. write_leases();
  212. }
  213. }
  214. static void send_inform(struct dhcp_packet *oldpacket)
  215. {
  216. struct dhcp_packet packet;
  217. /* "If a client has obtained a network address through some other means
  218. * (e.g., manual configuration), it may use a DHCPINFORM request message
  219. * to obtain other local configuration parameters. Servers receiving a
  220. * DHCPINFORM message construct a DHCPACK message with any local
  221. * configuration parameters appropriate for the client without:
  222. * allocating a new address, checking for an existing binding, filling
  223. * in 'yiaddr' or including lease time parameters. The servers SHOULD
  224. * unicast the DHCPACK reply to the address given in the 'ciaddr' field
  225. * of the DHCPINFORM message.
  226. * ...
  227. * The server responds to a DHCPINFORM message by sending a DHCPACK
  228. * message directly to the address given in the 'ciaddr' field
  229. * of the DHCPINFORM message. The server MUST NOT send a lease
  230. * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
  231. */
  232. //TODO: do a few sanity checks: is ciaddr set?
  233. //Better yet: is ciaddr == IP source addr?
  234. init_packet(&packet, oldpacket, DHCPACK);
  235. add_server_options(&packet);
  236. send_packet(&packet, /*force_bcast:*/ 0);
  237. }
  238. /* globals */
  239. struct dyn_lease *g_leases;
  240. /* struct server_config_t server_config is in bb_common_bufsiz1 */
  241. int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  242. int udhcpd_main(int argc UNUSED_PARAM, char **argv)
  243. {
  244. fd_set rfds;
  245. int server_socket = -1, retval, max_sock;
  246. struct dhcp_packet packet;
  247. uint8_t *state;
  248. uint32_t static_lease_nip;
  249. unsigned timeout_end;
  250. unsigned num_ips;
  251. unsigned opt;
  252. struct option_set *option;
  253. struct dyn_lease *lease, fake_lease;
  254. IF_FEATURE_UDHCP_PORT(char *str_P;)
  255. #if ENABLE_FEATURE_UDHCP_PORT
  256. SERVER_PORT = 67;
  257. CLIENT_PORT = 68;
  258. #endif
  259. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  260. opt_complementary = "vv";
  261. #endif
  262. opt = getopt32(argv, "fSv"
  263. IF_FEATURE_UDHCP_PORT("P:", &str_P)
  264. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  265. , &dhcp_verbose
  266. #endif
  267. );
  268. argv += optind;
  269. if (!(opt & 1)) { /* no -f */
  270. bb_daemonize_or_rexec(0, argv);
  271. logmode = LOGMODE_NONE;
  272. }
  273. if (opt & 2) { /* -S */
  274. openlog(applet_name, LOG_PID, LOG_DAEMON);
  275. logmode |= LOGMODE_SYSLOG;
  276. }
  277. #if ENABLE_FEATURE_UDHCP_PORT
  278. if (opt & 8) { /* -P */
  279. SERVER_PORT = xatou16(str_P);
  280. CLIENT_PORT = SERVER_PORT + 1;
  281. }
  282. #endif
  283. /* Would rather not do read_config before daemonization -
  284. * otherwise NOMMU machines will parse config twice */
  285. read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
  286. /* Make sure fd 0,1,2 are open */
  287. bb_sanitize_stdio();
  288. /* Equivalent of doing a fflush after every \n */
  289. setlinebuf(stdout);
  290. /* Create pidfile */
  291. write_pidfile(server_config.pidfile);
  292. /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
  293. bb_info_msg("%s (v"BB_VER") started", applet_name);
  294. option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
  295. server_config.max_lease_sec = DEFAULT_LEASE_TIME;
  296. if (option) {
  297. move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
  298. server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
  299. }
  300. /* Sanity check */
  301. num_ips = server_config.end_ip - server_config.start_ip + 1;
  302. if (server_config.max_leases > num_ips) {
  303. bb_error_msg("max_leases=%u is too big, setting to %u",
  304. (unsigned)server_config.max_leases, num_ips);
  305. server_config.max_leases = num_ips;
  306. }
  307. g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
  308. read_leases(server_config.lease_file);
  309. if (udhcp_read_interface(server_config.interface,
  310. &server_config.ifindex,
  311. &server_config.server_nip,
  312. server_config.server_mac)
  313. ) {
  314. retval = 1;
  315. goto ret;
  316. }
  317. /* Setup the signal pipe */
  318. udhcp_sp_setup();
  319. timeout_end = monotonic_sec() + server_config.auto_time;
  320. while (1) { /* loop until universe collapses */
  321. int bytes;
  322. struct timeval tv;
  323. uint8_t *server_id_opt;
  324. uint8_t *requested_opt;
  325. uint32_t requested_nip = requested_nip; /* for compiler */
  326. if (server_socket < 0) {
  327. server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
  328. server_config.interface);
  329. }
  330. max_sock = udhcp_sp_fd_set(&rfds, server_socket);
  331. if (server_config.auto_time) {
  332. tv.tv_sec = timeout_end - monotonic_sec();
  333. tv.tv_usec = 0;
  334. }
  335. retval = 0;
  336. if (!server_config.auto_time || tv.tv_sec > 0) {
  337. retval = select(max_sock + 1, &rfds, NULL, NULL,
  338. server_config.auto_time ? &tv : NULL);
  339. }
  340. if (retval == 0) {
  341. write_leases();
  342. timeout_end = monotonic_sec() + server_config.auto_time;
  343. continue;
  344. }
  345. if (retval < 0 && errno != EINTR) {
  346. log1("Error on select");
  347. continue;
  348. }
  349. switch (udhcp_sp_read(&rfds)) {
  350. case SIGUSR1:
  351. bb_info_msg("Received SIGUSR1");
  352. write_leases();
  353. /* why not just reset the timeout, eh */
  354. timeout_end = monotonic_sec() + server_config.auto_time;
  355. continue;
  356. case SIGTERM:
  357. bb_info_msg("Received SIGTERM");
  358. goto ret0;
  359. case 0: /* no signal: read a packet */
  360. break;
  361. default: /* signal or error (probably EINTR): back to select */
  362. continue;
  363. }
  364. bytes = udhcp_recv_kernel_packet(&packet, server_socket);
  365. if (bytes < 0) {
  366. /* bytes can also be -2 ("bad packet data") */
  367. if (bytes == -1 && errno != EINTR) {
  368. log1("Read error: %s, reopening socket", strerror(errno));
  369. close(server_socket);
  370. server_socket = -1;
  371. }
  372. continue;
  373. }
  374. if (packet.hlen != 6) {
  375. bb_error_msg("MAC length != 6, ignoring packet");
  376. continue;
  377. }
  378. if (packet.op != BOOTREQUEST) {
  379. bb_error_msg("not a REQUEST, ignoring packet");
  380. continue;
  381. }
  382. state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
  383. if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
  384. bb_error_msg("no or bad message type option, ignoring packet");
  385. continue;
  386. }
  387. /* Look for a static/dynamic lease */
  388. static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
  389. if (static_lease_nip) {
  390. bb_info_msg("Found static lease: %x", static_lease_nip);
  391. memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
  392. fake_lease.lease_nip = static_lease_nip;
  393. fake_lease.expires = 0;
  394. lease = &fake_lease;
  395. } else {
  396. lease = find_lease_by_mac(packet.chaddr);
  397. }
  398. /* Get REQUESTED_IP and SERVER_ID if present */
  399. server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID);
  400. if (server_id_opt) {
  401. uint32_t server_id_net;
  402. move_from_unaligned32(server_id_net, server_id_opt);
  403. if (server_id_net != server_config.server_nip) {
  404. /* client talks to somebody else */
  405. log1("server ID doesn't match, ignoring");
  406. continue;
  407. }
  408. }
  409. requested_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP);
  410. if (requested_opt) {
  411. move_from_unaligned32(requested_nip, requested_opt);
  412. }
  413. switch (state[0]) {
  414. case DHCPDISCOVER:
  415. log1("Received DISCOVER");
  416. send_offer(&packet, static_lease_nip, lease);
  417. break;
  418. case DHCPREQUEST:
  419. log1("Received REQUEST");
  420. /* RFC 2131:
  421. o DHCPREQUEST generated during SELECTING state:
  422. Client inserts the address of the selected server in 'server
  423. identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
  424. filled in with the yiaddr value from the chosen DHCPOFFER.
  425. Note that the client may choose to collect several DHCPOFFER
  426. messages and select the "best" offer. The client indicates its
  427. selection by identifying the offering server in the DHCPREQUEST
  428. message. If the client receives no acceptable offers, the client
  429. may choose to try another DHCPDISCOVER message. Therefore, the
  430. servers may not receive a specific DHCPREQUEST from which they can
  431. decide whether or not the client has accepted the offer.
  432. o DHCPREQUEST generated during INIT-REBOOT state:
  433. 'server identifier' MUST NOT be filled in, 'requested IP address'
  434. option MUST be filled in with client's notion of its previously
  435. assigned address. 'ciaddr' MUST be zero. The client is seeking to
  436. verify a previously allocated, cached configuration. Server SHOULD
  437. send a DHCPNAK message to the client if the 'requested IP address'
  438. is incorrect, or is on the wrong network.
  439. Determining whether a client in the INIT-REBOOT state is on the
  440. correct network is done by examining the contents of 'giaddr', the
  441. 'requested IP address' option, and a database lookup. If the DHCP
  442. server detects that the client is on the wrong net (i.e., the
  443. result of applying the local subnet mask or remote subnet mask (if
  444. 'giaddr' is not zero) to 'requested IP address' option value
  445. doesn't match reality), then the server SHOULD send a DHCPNAK
  446. message to the client.
  447. If the network is correct, then the DHCP server should check if
  448. the client's notion of its IP address is correct. If not, then the
  449. server SHOULD send a DHCPNAK message to the client. If the DHCP
  450. server has no record of this client, then it MUST remain silent,
  451. and MAY output a warning to the network administrator. This
  452. behavior is necessary for peaceful coexistence of non-
  453. communicating DHCP servers on the same wire.
  454. If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
  455. the same subnet as the server. The server MUST broadcast the
  456. DHCPNAK message to the 0xffffffff broadcast address because the
  457. client may not have a correct network address or subnet mask, and
  458. the client may not be answering ARP requests.
  459. If 'giaddr' is set in the DHCPREQUEST message, the client is on a
  460. different subnet. The server MUST set the broadcast bit in the
  461. DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
  462. client, because the client may not have a correct network address
  463. or subnet mask, and the client may not be answering ARP requests.
  464. o DHCPREQUEST generated during RENEWING state:
  465. 'server identifier' MUST NOT be filled in, 'requested IP address'
  466. option MUST NOT be filled in, 'ciaddr' MUST be filled in with
  467. client's IP address. In this situation, the client is completely
  468. configured, and is trying to extend its lease. This message will
  469. be unicast, so no relay agents will be involved in its
  470. transmission. Because 'giaddr' is therefore not filled in, the
  471. DHCP server will trust the value in 'ciaddr', and use it when
  472. replying to the client.
  473. A client MAY choose to renew or extend its lease prior to T1. The
  474. server may choose not to extend the lease (as a policy decision by
  475. the network administrator), but should return a DHCPACK message
  476. regardless.
  477. o DHCPREQUEST generated during REBINDING state:
  478. 'server identifier' MUST NOT be filled in, 'requested IP address'
  479. option MUST NOT be filled in, 'ciaddr' MUST be filled in with
  480. client's IP address. In this situation, the client is completely
  481. configured, and is trying to extend its lease. This message MUST
  482. be broadcast to the 0xffffffff IP broadcast address. The DHCP
  483. server SHOULD check 'ciaddr' for correctness before replying to
  484. the DHCPREQUEST.
  485. The DHCPREQUEST from a REBINDING client is intended to accommodate
  486. sites that have multiple DHCP servers and a mechanism for
  487. maintaining consistency among leases managed by multiple servers.
  488. A DHCP server MAY extend a client's lease only if it has local
  489. administrative authority to do so.
  490. */
  491. if (!requested_opt) {
  492. requested_nip = packet.ciaddr;
  493. if (requested_nip == 0) {
  494. log1("no requested IP and no ciaddr, ignoring");
  495. break;
  496. }
  497. }
  498. if (lease && requested_nip == lease->lease_nip) {
  499. /* client requested or configured IP matches the lease.
  500. * ACK it, and bump lease expiration time. */
  501. send_ACK(&packet, lease->lease_nip);
  502. break;
  503. }
  504. if (server_id_opt) {
  505. /* client was talking specifically to us.
  506. * "No, we don't have this IP for you". */
  507. send_NAK(&packet);
  508. }
  509. break;
  510. case DHCPDECLINE:
  511. /* RFC 2131:
  512. * "If the server receives a DHCPDECLINE message,
  513. * the client has discovered through some other means
  514. * that the suggested network address is already
  515. * in use. The server MUST mark the network address
  516. * as not available and SHOULD notify the local
  517. * sysadmin of a possible configuration problem."
  518. *
  519. * SERVER_ID must be present,
  520. * REQUESTED_IP must be present,
  521. * chaddr must be filled in,
  522. * ciaddr must be 0 (we do not check this)
  523. */
  524. log1("Received DECLINE");
  525. if (server_id_opt
  526. && requested_opt
  527. && lease /* chaddr matches this lease */
  528. && requested_nip == lease->lease_nip
  529. ) {
  530. memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
  531. lease->expires = time(NULL) + server_config.decline_time;
  532. }
  533. break;
  534. case DHCPRELEASE:
  535. /* "Upon receipt of a DHCPRELEASE message, the server
  536. * marks the network address as not allocated."
  537. *
  538. * SERVER_ID must be present,
  539. * REQUESTED_IP must not be present (we do not check this),
  540. * chaddr must be filled in,
  541. * ciaddr must be filled in
  542. */
  543. log1("Received RELEASE");
  544. if (server_id_opt
  545. && lease /* chaddr matches this lease */
  546. && packet.ciaddr == lease->lease_nip
  547. ) {
  548. lease->expires = time(NULL);
  549. }
  550. break;
  551. case DHCPINFORM:
  552. log1("Received INFORM");
  553. send_inform(&packet);
  554. break;
  555. }
  556. }
  557. ret0:
  558. retval = 0;
  559. ret:
  560. /*if (server_config.pidfile) - server_config.pidfile is never NULL */
  561. remove_pidfile(server_config.pidfile);
  562. return retval;
  563. }