3
0

dhcpd.c 21 KB

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