3
0

dhcpd.c 22 KB

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