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_info_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 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_info_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. #if ENABLE_FEATURE_UDHCP_PORT
  273. SERVER_PORT = 67;
  274. CLIENT_PORT = 68;
  275. #endif
  276. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  277. opt_complementary = "vv";
  278. #endif
  279. opt = getopt32(argv, "fSI:va:"
  280. IF_FEATURE_UDHCP_PORT("P:")
  281. , &str_I
  282. , &str_a
  283. IF_FEATURE_UDHCP_PORT(, &str_P)
  284. IF_UDHCP_VERBOSE(, &dhcp_verbose)
  285. );
  286. if (!(opt & 1)) { /* no -f */
  287. bb_daemonize_or_rexec(0, argv);
  288. logmode = LOGMODE_NONE;
  289. }
  290. /* update argv after the possible vfork+exec in daemonize */
  291. argv += optind;
  292. if (opt & 2) { /* -S */
  293. openlog(applet_name, LOG_PID, LOG_DAEMON);
  294. logmode |= LOGMODE_SYSLOG;
  295. }
  296. if (opt & 4) { /* -I */
  297. len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET);
  298. server_config.server_nip = lsa->u.sin.sin_addr.s_addr;
  299. free(lsa);
  300. }
  301. #if ENABLE_FEATURE_UDHCP_PORT
  302. if (opt & 32) { /* -P */
  303. SERVER_PORT = xatou16(str_P);
  304. CLIENT_PORT = SERVER_PORT + 1;
  305. }
  306. #endif
  307. arpping_ms = xatou(str_a);
  308. /* Would rather not do read_config before daemonization -
  309. * otherwise NOMMU machines will parse config twice */
  310. read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
  311. /* Make sure fd 0,1,2 are open */
  312. bb_sanitize_stdio();
  313. /* Equivalent of doing a fflush after every \n */
  314. setlinebuf(stdout);
  315. /* Create pidfile */
  316. write_pidfile(server_config.pidfile);
  317. /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
  318. bb_info_msg("%s (v"BB_VER") started", applet_name);
  319. option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
  320. server_config.max_lease_sec = DEFAULT_LEASE_TIME;
  321. if (option) {
  322. move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
  323. server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
  324. }
  325. /* Sanity check */
  326. num_ips = server_config.end_ip - server_config.start_ip + 1;
  327. if (server_config.max_leases > num_ips) {
  328. bb_error_msg("max_leases=%u is too big, setting to %u",
  329. (unsigned)server_config.max_leases, num_ips);
  330. server_config.max_leases = num_ips;
  331. }
  332. g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
  333. read_leases(server_config.lease_file);
  334. if (udhcp_read_interface(server_config.interface,
  335. &server_config.ifindex,
  336. (server_config.server_nip == 0 ? &server_config.server_nip : NULL),
  337. server_config.server_mac)
  338. ) {
  339. retval = 1;
  340. goto ret;
  341. }
  342. /* Setup the signal pipe */
  343. udhcp_sp_setup();
  344. continue_with_autotime:
  345. timeout_end = monotonic_sec() + server_config.auto_time;
  346. while (1) { /* loop until universe collapses */
  347. fd_set rfds;
  348. struct dhcp_packet packet;
  349. int bytes;
  350. struct timeval tv;
  351. uint8_t *server_id_opt;
  352. uint8_t *requested_ip_opt;
  353. uint32_t requested_nip = requested_nip; /* for compiler */
  354. uint32_t static_lease_nip;
  355. struct dyn_lease *lease, fake_lease;
  356. if (server_socket < 0) {
  357. server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
  358. server_config.interface);
  359. }
  360. max_sock = udhcp_sp_fd_set(&rfds, server_socket);
  361. if (server_config.auto_time) {
  362. /* cast to signed is essential if tv_sec is wider than int */
  363. tv.tv_sec = (int)(timeout_end - monotonic_sec());
  364. tv.tv_usec = 0;
  365. }
  366. retval = 0;
  367. if (!server_config.auto_time || tv.tv_sec > 0) {
  368. retval = select(max_sock + 1, &rfds, NULL, NULL,
  369. server_config.auto_time ? &tv : NULL);
  370. }
  371. if (retval == 0) {
  372. write_leases();
  373. goto continue_with_autotime;
  374. }
  375. if (retval < 0 && errno != EINTR) {
  376. log1("Error on select");
  377. continue;
  378. }
  379. switch (udhcp_sp_read(&rfds)) {
  380. case SIGUSR1:
  381. bb_info_msg("Received SIGUSR1");
  382. write_leases();
  383. /* why not just reset the timeout, eh */
  384. goto continue_with_autotime;
  385. case SIGTERM:
  386. bb_info_msg("Received SIGTERM");
  387. write_leases();
  388. goto ret0;
  389. case 0: /* no signal: read a packet */
  390. break;
  391. default: /* signal or error (probably EINTR): back to select */
  392. continue;
  393. }
  394. bytes = udhcp_recv_kernel_packet(&packet, server_socket);
  395. if (bytes < 0) {
  396. /* bytes can also be -2 ("bad packet data") */
  397. if (bytes == -1 && errno != EINTR) {
  398. log1("Read error: %s, reopening socket", strerror(errno));
  399. close(server_socket);
  400. server_socket = -1;
  401. }
  402. continue;
  403. }
  404. if (packet.hlen != 6) {
  405. bb_error_msg("MAC length != 6, ignoring packet");
  406. continue;
  407. }
  408. if (packet.op != BOOTREQUEST) {
  409. bb_error_msg("not a REQUEST, ignoring packet");
  410. continue;
  411. }
  412. state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
  413. if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
  414. bb_error_msg("no or bad message type option, ignoring packet");
  415. continue;
  416. }
  417. /* Get SERVER_ID if present */
  418. server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID);
  419. if (server_id_opt) {
  420. uint32_t server_id_network_order;
  421. move_from_unaligned32(server_id_network_order, server_id_opt);
  422. if (server_id_network_order != server_config.server_nip) {
  423. /* client talks to somebody else */
  424. log1("server ID doesn't match, ignoring");
  425. continue;
  426. }
  427. }
  428. /* Look for a static/dynamic lease */
  429. static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
  430. if (static_lease_nip) {
  431. bb_info_msg("Found static lease: %x", static_lease_nip);
  432. memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
  433. fake_lease.lease_nip = static_lease_nip;
  434. fake_lease.expires = 0;
  435. lease = &fake_lease;
  436. } else {
  437. lease = find_lease_by_mac(packet.chaddr);
  438. }
  439. /* Get REQUESTED_IP if present */
  440. requested_ip_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP);
  441. if (requested_ip_opt) {
  442. move_from_unaligned32(requested_nip, requested_ip_opt);
  443. }
  444. switch (state[0]) {
  445. case DHCPDISCOVER:
  446. log1("Received DISCOVER");
  447. send_offer(&packet, static_lease_nip, lease, requested_ip_opt, arpping_ms);
  448. break;
  449. case DHCPREQUEST:
  450. log1("Received REQUEST");
  451. /* RFC 2131:
  452. o DHCPREQUEST generated during SELECTING state:
  453. Client inserts the address of the selected server in 'server
  454. identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
  455. filled in with the yiaddr value from the chosen DHCPOFFER.
  456. Note that the client may choose to collect several DHCPOFFER
  457. messages and select the "best" offer. The client indicates its
  458. selection by identifying the offering server in the DHCPREQUEST
  459. message. If the client receives no acceptable offers, the client
  460. may choose to try another DHCPDISCOVER message. Therefore, the
  461. servers may not receive a specific DHCPREQUEST from which they can
  462. decide whether or not the client has accepted the offer.
  463. o DHCPREQUEST generated during INIT-REBOOT state:
  464. 'server identifier' MUST NOT be filled in, 'requested IP address'
  465. option MUST be filled in with client's notion of its previously
  466. assigned address. 'ciaddr' MUST be zero. The client is seeking to
  467. verify a previously allocated, cached configuration. Server SHOULD
  468. send a DHCPNAK message to the client if the 'requested IP address'
  469. is incorrect, or is on the wrong network.
  470. Determining whether a client in the INIT-REBOOT state is on the
  471. correct network is done by examining the contents of 'giaddr', the
  472. 'requested IP address' option, and a database lookup. If the DHCP
  473. server detects that the client is on the wrong net (i.e., the
  474. result of applying the local subnet mask or remote subnet mask (if
  475. 'giaddr' is not zero) to 'requested IP address' option value
  476. doesn't match reality), then the server SHOULD send a DHCPNAK
  477. message to the client.
  478. If the network is correct, then the DHCP server should check if
  479. the client's notion of its IP address is correct. If not, then the
  480. server SHOULD send a DHCPNAK message to the client. If the DHCP
  481. server has no record of this client, then it MUST remain silent,
  482. and MAY output a warning to the network administrator. This
  483. behavior is necessary for peaceful coexistence of non-
  484. communicating DHCP servers on the same wire.
  485. If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
  486. the same subnet as the server. The server MUST broadcast the
  487. DHCPNAK message to the 0xffffffff broadcast address because the
  488. client may not have a correct network address or subnet mask, and
  489. the client may not be answering ARP requests.
  490. If 'giaddr' is set in the DHCPREQUEST message, the client is on a
  491. different subnet. The server MUST set the broadcast bit in the
  492. DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
  493. client, because the client may not have a correct network address
  494. or subnet mask, and the client may not be answering ARP requests.
  495. o DHCPREQUEST generated during RENEWING state:
  496. 'server identifier' MUST NOT be filled in, 'requested IP address'
  497. option MUST NOT be filled in, 'ciaddr' MUST be filled in with
  498. client's IP address. In this situation, the client is completely
  499. configured, and is trying to extend its lease. This message will
  500. be unicast, so no relay agents will be involved in its
  501. transmission. Because 'giaddr' is therefore not filled in, the
  502. DHCP server will trust the value in 'ciaddr', and use it when
  503. replying to the client.
  504. A client MAY choose to renew or extend its lease prior to T1. The
  505. server may choose not to extend the lease (as a policy decision by
  506. the network administrator), but should return a DHCPACK message
  507. regardless.
  508. o DHCPREQUEST generated during REBINDING state:
  509. 'server identifier' MUST NOT be filled in, 'requested IP address'
  510. option MUST NOT be filled in, 'ciaddr' MUST be filled in with
  511. client's IP address. In this situation, the client is completely
  512. configured, and is trying to extend its lease. This message MUST
  513. be broadcast to the 0xffffffff IP broadcast address. The DHCP
  514. server SHOULD check 'ciaddr' for correctness before replying to
  515. the DHCPREQUEST.
  516. The DHCPREQUEST from a REBINDING client is intended to accommodate
  517. sites that have multiple DHCP servers and a mechanism for
  518. maintaining consistency among leases managed by multiple servers.
  519. A DHCP server MAY extend a client's lease only if it has local
  520. administrative authority to do so.
  521. */
  522. if (!requested_ip_opt) {
  523. requested_nip = packet.ciaddr;
  524. if (requested_nip == 0) {
  525. log1("no requested IP and no ciaddr, ignoring");
  526. break;
  527. }
  528. }
  529. if (lease && requested_nip == lease->lease_nip) {
  530. /* client requested or configured IP matches the lease.
  531. * ACK it, and bump lease expiration time. */
  532. send_ACK(&packet, lease->lease_nip);
  533. break;
  534. }
  535. /* No lease for this MAC, or lease IP != requested IP */
  536. if (server_id_opt /* client is in SELECTING state */
  537. || requested_ip_opt /* client is in INIT-REBOOT state */
  538. ) {
  539. /* "No, we don't have this IP for you" */
  540. send_NAK(&packet);
  541. } /* else: client is in RENEWING or REBINDING, do not answer */
  542. break;
  543. case DHCPDECLINE:
  544. /* RFC 2131:
  545. * "If the server receives a DHCPDECLINE message,
  546. * the client has discovered through some other means
  547. * that the suggested network address is already
  548. * in use. The server MUST mark the network address
  549. * as not available and SHOULD notify the local
  550. * sysadmin of a possible configuration problem."
  551. *
  552. * SERVER_ID must be present,
  553. * REQUESTED_IP must be present,
  554. * chaddr must be filled in,
  555. * ciaddr must be 0 (we do not check this)
  556. */
  557. log1("Received DECLINE");
  558. if (server_id_opt
  559. && requested_ip_opt
  560. && lease /* chaddr matches this lease */
  561. && requested_nip == lease->lease_nip
  562. ) {
  563. memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
  564. lease->expires = time(NULL) + server_config.decline_time;
  565. }
  566. break;
  567. case DHCPRELEASE:
  568. /* "Upon receipt of a DHCPRELEASE message, the server
  569. * marks the network address as not allocated."
  570. *
  571. * SERVER_ID must be present,
  572. * REQUESTED_IP must not be present (we do not check this),
  573. * chaddr must be filled in,
  574. * ciaddr must be filled in
  575. */
  576. log1("Received RELEASE");
  577. if (server_id_opt
  578. && lease /* chaddr matches this lease */
  579. && packet.ciaddr == lease->lease_nip
  580. ) {
  581. lease->expires = time(NULL);
  582. }
  583. break;
  584. case DHCPINFORM:
  585. log1("Received INFORM");
  586. send_inform(&packet);
  587. break;
  588. }
  589. }
  590. ret0:
  591. retval = 0;
  592. ret:
  593. /*if (server_config.pidfile) - server_config.pidfile is never NULL */
  594. remove_pidfile(server_config.pidfile);
  595. return retval;
  596. }