3
0

dhcpd.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. //applet:IF_UDHCPD(APPLET(udhcpd, BB_DIR_USR_SBIN, BB_SUID_DROP))
  24. //kbuild:lib-$(CONFIG_UDHCPD) += common.o packet.o signalpipe.o socket.o
  25. //kbuild:lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o
  26. //kbuild:lib-$(CONFIG_FEATURE_UDHCP_RFC3397) += domain_codec.o
  27. //usage:#define udhcpd_trivial_usage
  28. //usage: "[-fS] [-I ADDR]" IF_FEATURE_UDHCP_PORT(" [-P N]") " [CONFFILE]"
  29. //usage:#define udhcpd_full_usage "\n\n"
  30. //usage: "DHCP server\n"
  31. //usage: "\n -f Run in foreground"
  32. //usage: "\n -S Log to syslog too"
  33. //usage: "\n -I ADDR Local address"
  34. //usage: "\n -a MSEC Timeout for ARP ping (default 2000)"
  35. //usage: IF_FEATURE_UDHCP_PORT(
  36. //usage: "\n -P N Use port N (default 67)"
  37. //usage: )
  38. //usage: "\nSignals:"
  39. //usage: "\n USR1 Update lease file"
  40. #include <netinet/ether.h>
  41. #include <syslog.h>
  42. #include "common.h"
  43. #include "dhcpc.h"
  44. #include "dhcpd.h"
  45. /* globals */
  46. #define g_leases ((struct dyn_lease*)ptr_to_globals)
  47. /* struct server_data_t server_data is in bb_common_bufsiz1 */
  48. struct static_lease {
  49. struct static_lease *next;
  50. uint32_t nip;
  51. uint8_t mac[6];
  52. uint8_t opt[1];
  53. };
  54. /* Takes the address of the pointer to the static_leases linked list,
  55. * address to a 6 byte mac address,
  56. * 4 byte IP address */
  57. static void add_static_lease(struct static_lease **st_lease_pp,
  58. uint8_t *mac,
  59. uint32_t nip,
  60. const char *opts)
  61. {
  62. struct static_lease *st_lease;
  63. unsigned optlen;
  64. optlen = (opts ? 1+1+strnlen(opts, 120) : 0);
  65. /* Find the tail of the list */
  66. while ((st_lease = *st_lease_pp) != NULL) {
  67. st_lease_pp = &st_lease->next;
  68. }
  69. /* Add new node */
  70. *st_lease_pp = st_lease = xzalloc(sizeof(*st_lease) + optlen);
  71. memcpy(st_lease->mac, mac, 6);
  72. st_lease->nip = nip;
  73. /*st_lease->next = NULL;*/
  74. if (optlen) {
  75. st_lease->opt[OPT_CODE] = DHCP_HOST_NAME;
  76. optlen -= 2;
  77. st_lease->opt[OPT_LEN] = optlen;
  78. memcpy(&st_lease->opt[OPT_DATA], opts, optlen);
  79. }
  80. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
  81. /* Print out static leases just to check what's going on */
  82. if (dhcp_verbose >= 2) {
  83. bb_info_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x",
  84. st_lease->mac[0], st_lease->mac[1], st_lease->mac[2],
  85. st_lease->mac[3], st_lease->mac[4], st_lease->mac[5],
  86. st_lease->nip
  87. );
  88. }
  89. #endif
  90. }
  91. /* Find static lease IP by mac */
  92. static uint32_t get_static_nip_by_mac(void *mac)
  93. {
  94. struct static_lease *st_lease = server_data.static_leases;
  95. while (st_lease) {
  96. if (memcmp(st_lease->mac, mac, 6) == 0)
  97. return st_lease->nip;
  98. st_lease = st_lease->next;
  99. }
  100. return 0;
  101. }
  102. static int is_nip_reserved_as_static(uint32_t nip)
  103. {
  104. struct static_lease *st_lease = server_data.static_leases;
  105. while (st_lease) {
  106. if (st_lease->nip == nip)
  107. return 1;
  108. st_lease = st_lease->next;
  109. }
  110. return 0;
  111. }
  112. /* Find the oldest expired lease, NULL if there are no expired leases */
  113. static struct dyn_lease *oldest_expired_lease(void)
  114. {
  115. struct dyn_lease *oldest_lease = NULL;
  116. leasetime_t oldest_time = time(NULL);
  117. unsigned i;
  118. /* Unexpired leases have g_leases[i].expires >= current time
  119. * and therefore can't ever match */
  120. for (i = 0; i < server_data.max_leases; i++) {
  121. if (g_leases[i].expires == 0 /* empty entry */
  122. || g_leases[i].expires < oldest_time
  123. ) {
  124. oldest_time = g_leases[i].expires;
  125. oldest_lease = &g_leases[i];
  126. }
  127. }
  128. return oldest_lease;
  129. }
  130. /* Clear out all leases with matching nonzero chaddr OR yiaddr.
  131. * If chaddr == NULL, this is a conflict lease.
  132. */
  133. static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
  134. {
  135. unsigned i;
  136. for (i = 0; i < server_data.max_leases; i++) {
  137. if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
  138. || (yiaddr && g_leases[i].lease_nip == yiaddr)
  139. ) {
  140. memset(&g_leases[i], 0, sizeof(g_leases[i]));
  141. }
  142. }
  143. }
  144. /* Add a lease into the table, clearing out any old ones.
  145. * If chaddr == NULL, this is a conflict lease.
  146. */
  147. static struct dyn_lease *add_lease(
  148. const uint8_t *chaddr, uint32_t yiaddr,
  149. leasetime_t leasetime,
  150. const char *hostname, int hostname_len)
  151. {
  152. struct dyn_lease *oldest;
  153. /* clean out any old ones */
  154. clear_leases(chaddr, yiaddr);
  155. oldest = oldest_expired_lease();
  156. if (oldest) {
  157. memset(oldest, 0, sizeof(*oldest));
  158. if (hostname) {
  159. char *p;
  160. hostname_len++; /* include NUL */
  161. if (hostname_len > sizeof(oldest->hostname))
  162. hostname_len = sizeof(oldest->hostname);
  163. p = safe_strncpy(oldest->hostname, hostname, hostname_len);
  164. /*
  165. * Sanitization (s/bad_char/./g).
  166. * The intent is not to allow only "DNS-valid" hostnames,
  167. * but merely make dumpleases output safe for shells to use.
  168. * We accept "0-9A-Za-z._-", all other chars turn to dots.
  169. */
  170. while (*p) {
  171. if (!isalnum(*p) && *p != '-' && *p != '_')
  172. *p = '.';
  173. p++;
  174. }
  175. }
  176. if (chaddr)
  177. memcpy(oldest->lease_mac, chaddr, 6);
  178. oldest->lease_nip = yiaddr;
  179. oldest->expires = time(NULL) + leasetime;
  180. }
  181. return oldest;
  182. }
  183. /* True if a lease has expired */
  184. static int is_expired_lease(struct dyn_lease *lease)
  185. {
  186. return (lease->expires < (leasetime_t) time(NULL));
  187. }
  188. /* Find the first lease that matches MAC, NULL if no match */
  189. static struct dyn_lease *find_lease_by_mac(const uint8_t *mac)
  190. {
  191. unsigned i;
  192. for (i = 0; i < server_data.max_leases; i++)
  193. if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
  194. return &g_leases[i];
  195. return NULL;
  196. }
  197. /* Find the first lease that matches IP, NULL is no match */
  198. static struct dyn_lease *find_lease_by_nip(uint32_t nip)
  199. {
  200. unsigned i;
  201. for (i = 0; i < server_data.max_leases; i++)
  202. if (g_leases[i].lease_nip == nip)
  203. return &g_leases[i];
  204. return NULL;
  205. }
  206. /* Check if the IP is taken; if it is, add it to the lease table */
  207. static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigned arpping_ms)
  208. {
  209. struct in_addr temp;
  210. int r;
  211. r = arpping(nip, safe_mac,
  212. server_data.server_nip,
  213. server_data.server_mac,
  214. server_data.interface,
  215. arpping_ms);
  216. if (r)
  217. return r;
  218. temp.s_addr = nip;
  219. bb_info_msg("%s belongs to someone, reserving it for %u seconds",
  220. inet_ntoa(temp), (unsigned)server_data.conflict_time);
  221. add_lease(NULL, nip, server_data.conflict_time, NULL, 0);
  222. return 0;
  223. }
  224. /* Find a new usable (we think) address */
  225. static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arpping_ms)
  226. {
  227. uint32_t addr;
  228. struct dyn_lease *oldest_lease = NULL;
  229. #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
  230. uint32_t stop;
  231. unsigned i, hash;
  232. /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good
  233. * dispersal even with similarly-valued "strings".
  234. */
  235. hash = 0;
  236. for (i = 0; i < 6; i++)
  237. hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash;
  238. /* pick a seed based on hwaddr then iterate until we find a free address. */
  239. addr = server_data.start_ip
  240. + (hash % (1 + server_data.end_ip - server_data.start_ip));
  241. stop = addr;
  242. #else
  243. addr = server_data.start_ip;
  244. #define stop (server_data.end_ip + 1)
  245. #endif
  246. do {
  247. uint32_t nip;
  248. struct dyn_lease *lease;
  249. /* ie, 192.168.55.0 */
  250. if ((addr & 0xff) == 0)
  251. goto next_addr;
  252. /* ie, 192.168.55.255 */
  253. if ((addr & 0xff) == 0xff)
  254. goto next_addr;
  255. nip = htonl(addr);
  256. /* skip our own address */
  257. if (nip == server_data.server_nip)
  258. goto next_addr;
  259. /* is this a static lease addr? */
  260. if (is_nip_reserved_as_static(nip))
  261. goto next_addr;
  262. lease = find_lease_by_nip(nip);
  263. if (!lease) {
  264. //TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
  265. if (nobody_responds_to_arp(nip, safe_mac, arpping_ms))
  266. return nip;
  267. } else {
  268. if (!oldest_lease || lease->expires < oldest_lease->expires)
  269. oldest_lease = lease;
  270. }
  271. next_addr:
  272. addr++;
  273. #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
  274. if (addr > server_data.end_ip)
  275. addr = server_data.start_ip;
  276. #endif
  277. } while (addr != stop);
  278. if (oldest_lease
  279. && is_expired_lease(oldest_lease)
  280. && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac, arpping_ms)
  281. ) {
  282. return oldest_lease->lease_nip;
  283. }
  284. return 0;
  285. }
  286. /* On these functions, make sure your datatype matches */
  287. static int FAST_FUNC read_str(const char *line, void *arg)
  288. {
  289. char **dest = arg;
  290. free(*dest);
  291. *dest = xstrdup(line);
  292. return 1;
  293. }
  294. static int FAST_FUNC read_u32(const char *line, void *arg)
  295. {
  296. *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
  297. return errno == 0;
  298. }
  299. static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
  300. {
  301. char *line;
  302. char *mac_string;
  303. char *ip_string;
  304. char *opts;
  305. struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */
  306. uint32_t nip;
  307. /* Read mac */
  308. line = (char *) const_line;
  309. mac_string = strtok_r(line, " \t", &line);
  310. if (!mac_string || !ether_aton_r(mac_string, &mac_bytes))
  311. return 0;
  312. /* Read ip */
  313. ip_string = strtok_r(NULL, " \t", &line);
  314. if (!ip_string || !udhcp_str2nip(ip_string, &nip))
  315. return 0;
  316. opts = strtok_r(NULL, " \t", &line);
  317. /* opts might be NULL, that's not an error */
  318. add_static_lease(arg, (uint8_t*) &mac_bytes, nip, opts);
  319. return 1;
  320. }
  321. static int FAST_FUNC read_optset(const char *line, void *arg)
  322. {
  323. return udhcp_str2optset(line, arg,
  324. dhcp_optflags, dhcp_option_strings,
  325. /*dhcpv6:*/ 0
  326. );
  327. }
  328. struct config_keyword {
  329. const char *keyword;
  330. int (*handler)(const char *line, void *var) FAST_FUNC;
  331. unsigned ofs;
  332. const char *def;
  333. };
  334. #define OFS(field) offsetof(struct server_data_t, field)
  335. static const struct config_keyword keywords[] = {
  336. /* keyword handler variable address default */
  337. {"start" , udhcp_str2nip , OFS(start_ip ), "192.168.0.20"},
  338. {"end" , udhcp_str2nip , OFS(end_ip ), "192.168.0.254"},
  339. {"interface" , read_str , OFS(interface ), "eth0"},
  340. /* Avoid "max_leases value not sane" warning by setting default
  341. * to default_end_ip - default_start_ip + 1: */
  342. {"max_leases" , read_u32 , OFS(max_leases ), "235"},
  343. {"auto_time" , read_u32 , OFS(auto_time ), "7200"},
  344. {"decline_time" , read_u32 , OFS(decline_time ), "3600"},
  345. {"conflict_time", read_u32 , OFS(conflict_time), "3600"},
  346. {"offer_time" , read_u32 , OFS(offer_time ), "60"},
  347. {"min_lease" , read_u32 , OFS(min_lease_sec), "60"},
  348. {"lease_file" , read_str , OFS(lease_file ), LEASES_FILE},
  349. {"pidfile" , read_str , OFS(pidfile ), "/var/run/udhcpd.pid"},
  350. {"siaddr" , udhcp_str2nip , OFS(siaddr_nip ), "0.0.0.0"},
  351. /* keywords with no defaults must be last! */
  352. {"option" , read_optset , OFS(options ), ""},
  353. {"opt" , read_optset , OFS(options ), ""},
  354. {"notify_file" , read_str , OFS(notify_file ), NULL},
  355. {"sname" , read_str , OFS(sname ), NULL},
  356. {"boot_file" , read_str , OFS(boot_file ), NULL},
  357. {"static_lease" , read_staticlease, OFS(static_leases), ""},
  358. };
  359. enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
  360. static NOINLINE void read_config(const char *file)
  361. {
  362. parser_t *parser;
  363. const struct config_keyword *k;
  364. unsigned i;
  365. char *token[2];
  366. for (i = 0; i < KWS_WITH_DEFAULTS; i++)
  367. keywords[i].handler(keywords[i].def, (char*)&server_data + keywords[i].ofs);
  368. parser = config_open(file);
  369. while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
  370. for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
  371. if (strcasecmp(token[0], k->keyword) == 0) {
  372. if (!k->handler(token[1], (char*)&server_data + k->ofs)) {
  373. bb_error_msg("can't parse line %u in %s",
  374. parser->lineno, file);
  375. /* reset back to the default value */
  376. k->handler(k->def, (char*)&server_data + k->ofs);
  377. }
  378. break;
  379. }
  380. }
  381. }
  382. config_close(parser);
  383. server_data.start_ip = ntohl(server_data.start_ip);
  384. server_data.end_ip = ntohl(server_data.end_ip);
  385. }
  386. static void write_leases(void)
  387. {
  388. int fd;
  389. unsigned i;
  390. leasetime_t curr;
  391. int64_t written_at;
  392. fd = open_or_warn(server_data.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
  393. if (fd < 0)
  394. return;
  395. curr = written_at = time(NULL);
  396. written_at = SWAP_BE64(written_at);
  397. full_write(fd, &written_at, sizeof(written_at));
  398. for (i = 0; i < server_data.max_leases; i++) {
  399. leasetime_t tmp_time;
  400. if (g_leases[i].lease_nip == 0)
  401. continue;
  402. /* Screw with the time in the struct, for easier writing */
  403. tmp_time = g_leases[i].expires;
  404. g_leases[i].expires -= curr;
  405. if ((signed_leasetime_t) g_leases[i].expires < 0)
  406. g_leases[i].expires = 0;
  407. g_leases[i].expires = htonl(g_leases[i].expires);
  408. /* No error check. If the file gets truncated,
  409. * we lose some leases on restart. Oh well. */
  410. full_write(fd, &g_leases[i], sizeof(g_leases[i]));
  411. /* Then restore it when done */
  412. g_leases[i].expires = tmp_time;
  413. }
  414. close(fd);
  415. if (server_data.notify_file) {
  416. char *argv[3];
  417. argv[0] = server_data.notify_file;
  418. argv[1] = server_data.lease_file;
  419. argv[2] = NULL;
  420. spawn_and_wait(argv);
  421. }
  422. }
  423. static NOINLINE void read_leases(const char *file)
  424. {
  425. struct dyn_lease lease;
  426. int64_t written_at, time_passed;
  427. int fd;
  428. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  429. unsigned i = 0;
  430. #endif
  431. fd = open_or_warn(file, O_RDONLY);
  432. if (fd < 0)
  433. return;
  434. if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
  435. goto ret;
  436. written_at = SWAP_BE64(written_at);
  437. time_passed = time(NULL) - written_at;
  438. /* Strange written_at, or lease file from old version of udhcpd
  439. * which had no "written_at" field? */
  440. if ((uint64_t)time_passed > 12 * 60 * 60)
  441. goto ret;
  442. while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
  443. uint32_t y = ntohl(lease.lease_nip);
  444. if (y >= server_data.start_ip && y <= server_data.end_ip) {
  445. signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
  446. uint32_t static_nip;
  447. if (expires <= 0)
  448. /* We keep expired leases: add_lease() will add
  449. * a lease with 0 seconds remaining.
  450. * Fewer IP address changes this way for mass reboot scenario.
  451. */
  452. expires = 0;
  453. /* Check if there is a different static lease for this IP or MAC */
  454. static_nip = get_static_nip_by_mac(lease.lease_mac);
  455. if (static_nip) {
  456. /* NB: we do not add lease even if static_nip == lease.lease_nip.
  457. */
  458. continue;
  459. }
  460. if (is_nip_reserved_as_static(lease.lease_nip))
  461. continue;
  462. /* NB: add_lease takes "relative time", IOW,
  463. * lease duration, not lease deadline. */
  464. if (add_lease(lease.lease_mac, lease.lease_nip,
  465. expires,
  466. lease.hostname, sizeof(lease.hostname)
  467. ) == 0
  468. ) {
  469. bb_error_msg("too many leases while loading %s", file);
  470. break;
  471. }
  472. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  473. i++;
  474. #endif
  475. }
  476. }
  477. log1("read %d leases", i);
  478. ret:
  479. close(fd);
  480. }
  481. /* Send a packet to a specific mac address and ip address by creating our own ip packet */
  482. static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
  483. {
  484. const uint8_t *chaddr;
  485. uint32_t ciaddr;
  486. // Was:
  487. //if (force_broadcast) { /* broadcast */ }
  488. //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
  489. //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
  490. //else { /* unicast to dhcp_pkt->yiaddr */ }
  491. // But this is wrong: yiaddr is _our_ idea what client's IP is
  492. // (for example, from lease file). Client may not know that,
  493. // and may not have UDP socket listening on that IP!
  494. // We should never unicast to dhcp_pkt->yiaddr!
  495. // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
  496. // and can be used.
  497. if (force_broadcast
  498. || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
  499. || dhcp_pkt->ciaddr == 0
  500. ) {
  501. log1("broadcasting packet to client");
  502. ciaddr = INADDR_BROADCAST;
  503. chaddr = MAC_BCAST_ADDR;
  504. } else {
  505. log1("unicasting packet to client ciaddr");
  506. ciaddr = dhcp_pkt->ciaddr;
  507. chaddr = dhcp_pkt->chaddr;
  508. }
  509. udhcp_send_raw_packet(dhcp_pkt,
  510. /*src*/ server_data.server_nip, SERVER_PORT,
  511. /*dst*/ ciaddr, CLIENT_PORT, chaddr,
  512. server_data.ifindex);
  513. }
  514. /* Send a packet to gateway_nip using the kernel ip stack */
  515. static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
  516. {
  517. log1("forwarding packet to relay");
  518. udhcp_send_kernel_packet(dhcp_pkt,
  519. server_data.server_nip, SERVER_PORT,
  520. dhcp_pkt->gateway_nip, SERVER_PORT);
  521. }
  522. static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
  523. {
  524. if (dhcp_pkt->gateway_nip)
  525. send_packet_to_relay(dhcp_pkt);
  526. else
  527. send_packet_to_client(dhcp_pkt, force_broadcast);
  528. }
  529. static void send_packet_verbose(struct dhcp_packet *dhcp_pkt, const char *fmt)
  530. {
  531. struct in_addr addr;
  532. addr.s_addr = dhcp_pkt->yiaddr;
  533. bb_info_msg(fmt, inet_ntoa(addr));
  534. /* send_packet emits error message itself if it detects failure */
  535. send_packet(dhcp_pkt, /*force_bcast:*/ 0);
  536. }
  537. static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
  538. {
  539. /* Sets op, htype, hlen, cookie fields
  540. * and adds DHCP_MESSAGE_TYPE option */
  541. udhcp_init_header(packet, type);
  542. packet->xid = oldpacket->xid;
  543. memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
  544. packet->flags = oldpacket->flags;
  545. packet->gateway_nip = oldpacket->gateway_nip;
  546. packet->ciaddr = oldpacket->ciaddr;
  547. udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_data.server_nip);
  548. }
  549. /* Fill options field, siaddr_nip, and sname and boot_file fields.
  550. * TODO: teach this code to use overload option.
  551. */
  552. static void add_server_options(struct dhcp_packet *packet)
  553. {
  554. struct option_set *config_opts;
  555. uint8_t *client_hostname_opt;
  556. client_hostname_opt = NULL;
  557. if (packet->yiaddr) { /* if we aren't from send_inform()... */
  558. struct static_lease *st_lease = server_data.static_leases;
  559. while (st_lease) {
  560. if (st_lease->nip == packet->yiaddr) {
  561. if (st_lease->opt[0] != 0)
  562. client_hostname_opt = st_lease->opt;
  563. break;
  564. }
  565. st_lease = st_lease->next;
  566. }
  567. }
  568. config_opts = server_data.options;
  569. while (config_opts) {
  570. if (config_opts->data[OPT_CODE] != DHCP_LEASE_TIME) {
  571. /* ^^^^
  572. * DHCP_LEASE_TIME is already filled, or in case of
  573. * send_inform(), should not be filled at all.
  574. */
  575. if (config_opts->data[OPT_CODE] != DHCP_HOST_NAME
  576. || !client_hostname_opt
  577. ) {
  578. /* Why "!client_hostname_opt":
  579. * add hostname only if client has no hostname
  580. * on its static lease line.
  581. * (Not that "opt hostname HOST"
  582. * makes much sense in udhcpd.conf,
  583. * that'd give all clients the same hostname,
  584. * but it's a valid configuration).
  585. */
  586. udhcp_add_binary_option(packet, config_opts->data);
  587. }
  588. }
  589. config_opts = config_opts->next;
  590. }
  591. if (client_hostname_opt)
  592. udhcp_add_binary_option(packet, client_hostname_opt);
  593. packet->siaddr_nip = server_data.siaddr_nip;
  594. if (server_data.sname)
  595. strncpy((char*)packet->sname, server_data.sname, sizeof(packet->sname) - 1);
  596. if (server_data.boot_file)
  597. strncpy((char*)packet->file, server_data.boot_file, sizeof(packet->file) - 1);
  598. }
  599. static uint32_t select_lease_time(struct dhcp_packet *packet)
  600. {
  601. uint32_t lease_time_sec = server_data.max_lease_sec;
  602. uint8_t *lease_time_opt = udhcp_get_option32(packet, DHCP_LEASE_TIME);
  603. if (lease_time_opt) {
  604. move_from_unaligned32(lease_time_sec, lease_time_opt);
  605. lease_time_sec = ntohl(lease_time_sec);
  606. if (lease_time_sec > server_data.max_lease_sec)
  607. lease_time_sec = server_data.max_lease_sec;
  608. if (lease_time_sec < server_data.min_lease_sec)
  609. lease_time_sec = server_data.min_lease_sec;
  610. }
  611. return lease_time_sec;
  612. }
  613. /* We got a DHCP DISCOVER. Send an OFFER. */
  614. /* NOINLINE: limit stack usage in caller */
  615. static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
  616. uint32_t static_lease_nip,
  617. struct dyn_lease *lease,
  618. uint32_t requested_nip,
  619. unsigned arpping_ms)
  620. {
  621. struct dhcp_packet packet;
  622. uint32_t lease_time_sec;
  623. init_packet(&packet, oldpacket, DHCPOFFER);
  624. /* If it is a static lease, use its IP */
  625. packet.yiaddr = static_lease_nip;
  626. /* Else: */
  627. if (!static_lease_nip) {
  628. /* We have no static lease for client's chaddr */
  629. const char *p_host_name;
  630. if (lease) {
  631. /* We have a dynamic lease for client's chaddr.
  632. * Reuse its IP (even if lease is expired).
  633. * Note that we ignore requested IP in this case.
  634. */
  635. packet.yiaddr = lease->lease_nip;
  636. }
  637. /* Or: if client has requested an IP */
  638. else if (requested_nip != 0
  639. /* and the IP is in the lease range */
  640. && ntohl(requested_nip) >= server_data.start_ip
  641. && ntohl(requested_nip) <= server_data.end_ip
  642. /* and */
  643. && ( !(lease = find_lease_by_nip(requested_nip)) /* is not already taken */
  644. || is_expired_lease(lease) /* or is taken, but expired */
  645. )
  646. ) {
  647. packet.yiaddr = requested_nip;
  648. }
  649. else {
  650. /* Otherwise, find a free IP */
  651. packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr, arpping_ms);
  652. }
  653. if (!packet.yiaddr) {
  654. bb_error_msg("no free IP addresses. OFFER abandoned");
  655. return;
  656. }
  657. /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
  658. p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
  659. lease = add_lease(packet.chaddr, packet.yiaddr,
  660. server_data.offer_time,
  661. p_host_name,
  662. p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
  663. );
  664. if (!lease) {
  665. bb_error_msg("no free IP addresses. OFFER abandoned");
  666. return;
  667. }
  668. }
  669. lease_time_sec = select_lease_time(oldpacket);
  670. udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
  671. add_server_options(&packet);
  672. /* send_packet emits error message itself if it detects failure */
  673. send_packet_verbose(&packet, "sending OFFER to %s");
  674. }
  675. /* NOINLINE: limit stack usage in caller */
  676. static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
  677. {
  678. struct dhcp_packet packet;
  679. init_packet(&packet, oldpacket, DHCPNAK);
  680. log1("sending %s", "NAK");
  681. send_packet(&packet, /*force_bcast:*/ 1);
  682. }
  683. /* NOINLINE: limit stack usage in caller */
  684. static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
  685. {
  686. struct dhcp_packet packet;
  687. uint32_t lease_time_sec;
  688. const char *p_host_name;
  689. init_packet(&packet, oldpacket, DHCPACK);
  690. packet.yiaddr = yiaddr;
  691. lease_time_sec = select_lease_time(oldpacket);
  692. udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
  693. add_server_options(&packet);
  694. send_packet_verbose(&packet, "sending ACK to %s");
  695. p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
  696. add_lease(packet.chaddr, packet.yiaddr,
  697. lease_time_sec,
  698. p_host_name,
  699. p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
  700. );
  701. if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
  702. /* rewrite the file with leases at every new acceptance */
  703. write_leases();
  704. }
  705. }
  706. /* NOINLINE: limit stack usage in caller */
  707. static NOINLINE void send_inform(struct dhcp_packet *oldpacket)
  708. {
  709. struct dhcp_packet packet;
  710. /* "If a client has obtained a network address through some other means
  711. * (e.g., manual configuration), it may use a DHCPINFORM request message
  712. * to obtain other local configuration parameters. Servers receiving a
  713. * DHCPINFORM message construct a DHCPACK message with any local
  714. * configuration parameters appropriate for the client without:
  715. * allocating a new address, checking for an existing binding, filling
  716. * in 'yiaddr' or including lease time parameters. The servers SHOULD
  717. * unicast the DHCPACK reply to the address given in the 'ciaddr' field
  718. * of the DHCPINFORM message.
  719. * ...
  720. * The server responds to a DHCPINFORM message by sending a DHCPACK
  721. * message directly to the address given in the 'ciaddr' field
  722. * of the DHCPINFORM message. The server MUST NOT send a lease
  723. * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
  724. */
  725. //TODO: do a few sanity checks: is ciaddr set?
  726. //Better yet: is ciaddr == IP source addr?
  727. init_packet(&packet, oldpacket, DHCPACK);
  728. add_server_options(&packet);
  729. send_packet(&packet, /*force_bcast:*/ 0);
  730. // or maybe? send_packet_verbose(&packet, "sending ACK to %s");
  731. }
  732. int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  733. int udhcpd_main(int argc UNUSED_PARAM, char **argv)
  734. {
  735. int server_socket = -1, retval;
  736. uint8_t *state;
  737. unsigned timeout_end;
  738. unsigned num_ips;
  739. unsigned opt;
  740. struct option_set *option;
  741. char *str_I = str_I;
  742. const char *str_a = "2000";
  743. unsigned arpping_ms;
  744. IF_FEATURE_UDHCP_PORT(char *str_P;)
  745. setup_common_bufsiz();
  746. IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;)
  747. IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
  748. /* Make sure fd 0,1,2 are open */
  749. /* Setup the signal pipe on fds 3,4 - must be before openlog() */
  750. udhcp_sp_setup();
  751. opt = getopt32(argv, "^"
  752. "fSI:va:"IF_FEATURE_UDHCP_PORT("P:")
  753. "\0"
  754. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  755. "vv"
  756. #endif
  757. , &str_I
  758. , &str_a
  759. IF_FEATURE_UDHCP_PORT(, &str_P)
  760. IF_UDHCP_VERBOSE(, &dhcp_verbose)
  761. );
  762. if (!(opt & 1)) { /* no -f */
  763. bb_daemonize_or_rexec(0, argv);
  764. logmode = LOGMODE_NONE;
  765. }
  766. /* update argv after the possible vfork+exec in daemonize */
  767. argv += optind;
  768. if (opt & 2) { /* -S */
  769. openlog(applet_name, LOG_PID, LOG_DAEMON);
  770. logmode |= LOGMODE_SYSLOG;
  771. }
  772. if (opt & 4) { /* -I */
  773. len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET);
  774. server_data.server_nip = lsa->u.sin.sin_addr.s_addr;
  775. free(lsa);
  776. }
  777. #if ENABLE_FEATURE_UDHCP_PORT
  778. if (opt & 32) { /* -P */
  779. SERVER_PORT = xatou16(str_P);
  780. CLIENT_PORT = SERVER_PORT + 1;
  781. }
  782. #endif
  783. arpping_ms = xatou(str_a);
  784. /* Would rather not do read_config before daemonization -
  785. * otherwise NOMMU machines will parse config twice */
  786. read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
  787. /* prevent poll timeout overflow */
  788. if (server_data.auto_time > INT_MAX / 1000)
  789. server_data.auto_time = INT_MAX / 1000;
  790. /* Create pidfile */
  791. write_pidfile(server_data.pidfile);
  792. /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
  793. bb_info_msg("started, v"BB_VER);
  794. option = udhcp_find_option(server_data.options, DHCP_LEASE_TIME);
  795. server_data.max_lease_sec = DEFAULT_LEASE_TIME;
  796. if (option) {
  797. move_from_unaligned32(server_data.max_lease_sec, option->data + OPT_DATA);
  798. server_data.max_lease_sec = ntohl(server_data.max_lease_sec);
  799. }
  800. /* Sanity check */
  801. num_ips = server_data.end_ip - server_data.start_ip + 1;
  802. if (server_data.max_leases > num_ips) {
  803. bb_error_msg("max_leases=%u is too big, setting to %u",
  804. (unsigned)server_data.max_leases, num_ips);
  805. server_data.max_leases = num_ips;
  806. }
  807. /* this sets g_leases */
  808. SET_PTR_TO_GLOBALS(xzalloc(server_data.max_leases * sizeof(g_leases[0])));
  809. read_leases(server_data.lease_file);
  810. if (udhcp_read_interface(server_data.interface,
  811. &server_data.ifindex,
  812. (server_data.server_nip == 0 ? &server_data.server_nip : NULL),
  813. server_data.server_mac)
  814. ) {
  815. retval = 1;
  816. goto ret;
  817. }
  818. continue_with_autotime:
  819. timeout_end = monotonic_sec() + server_data.auto_time;
  820. while (1) { /* loop until universe collapses */
  821. struct pollfd pfds[2];
  822. struct dhcp_packet packet;
  823. int bytes;
  824. int tv;
  825. uint8_t *server_id_opt;
  826. uint8_t *requested_ip_opt;
  827. uint32_t requested_nip;
  828. uint32_t static_lease_nip;
  829. struct dyn_lease *lease, fake_lease;
  830. if (server_socket < 0) {
  831. server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
  832. server_data.interface);
  833. }
  834. udhcp_sp_fd_set(pfds, server_socket);
  835. new_tv:
  836. tv = -1;
  837. if (server_data.auto_time) {
  838. tv = timeout_end - monotonic_sec();
  839. if (tv <= 0) {
  840. write_leases:
  841. write_leases();
  842. goto continue_with_autotime;
  843. }
  844. tv *= 1000;
  845. }
  846. /* Block here waiting for either signal or packet */
  847. retval = poll(pfds, 2, tv);
  848. if (retval <= 0) {
  849. if (retval == 0)
  850. goto write_leases;
  851. if (errno == EINTR)
  852. goto new_tv;
  853. /* < 0 and not EINTR: should not happen */
  854. bb_perror_msg_and_die("poll");
  855. }
  856. if (pfds[0].revents) switch (udhcp_sp_read()) {
  857. case SIGUSR1:
  858. bb_info_msg("received %s", "SIGUSR1");
  859. write_leases();
  860. /* why not just reset the timeout, eh */
  861. goto continue_with_autotime;
  862. case SIGTERM:
  863. bb_info_msg("received %s", "SIGTERM");
  864. write_leases();
  865. goto ret0;
  866. }
  867. /* Is it a packet? */
  868. if (!pfds[1].revents)
  869. continue; /* no */
  870. /* Note: we do not block here, we block on poll() instead.
  871. * Blocking here would prevent SIGTERM from working:
  872. * socket read inside this call is restarted on caught signals.
  873. */
  874. bytes = udhcp_recv_kernel_packet(&packet, server_socket);
  875. if (bytes < 0) {
  876. /* bytes can also be -2 ("bad packet data") */
  877. if (bytes == -1 && errno != EINTR) {
  878. log1("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
  879. close(server_socket);
  880. server_socket = -1;
  881. }
  882. continue;
  883. }
  884. if (packet.hlen != 6) {
  885. bb_info_msg("MAC length != 6, ignoring packet");
  886. continue;
  887. }
  888. if (packet.op != BOOTREQUEST) {
  889. bb_info_msg("not a REQUEST, ignoring packet");
  890. continue;
  891. }
  892. state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
  893. if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
  894. bb_info_msg("no or bad message type option, ignoring packet");
  895. continue;
  896. }
  897. /* Get SERVER_ID if present */
  898. server_id_opt = udhcp_get_option32(&packet, DHCP_SERVER_ID);
  899. if (server_id_opt) {
  900. uint32_t server_id_network_order;
  901. move_from_unaligned32(server_id_network_order, server_id_opt);
  902. if (server_id_network_order != server_data.server_nip) {
  903. /* client talks to somebody else */
  904. log1("server ID doesn't match, ignoring");
  905. continue;
  906. }
  907. }
  908. /* Look for a static/dynamic lease */
  909. static_lease_nip = get_static_nip_by_mac(&packet.chaddr);
  910. if (static_lease_nip) {
  911. bb_info_msg("found static lease: %x", static_lease_nip);
  912. memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
  913. fake_lease.lease_nip = static_lease_nip;
  914. fake_lease.expires = 0;
  915. lease = &fake_lease;
  916. } else {
  917. lease = find_lease_by_mac(packet.chaddr);
  918. }
  919. /* Get REQUESTED_IP if present */
  920. requested_nip = 0;
  921. requested_ip_opt = udhcp_get_option32(&packet, DHCP_REQUESTED_IP);
  922. if (requested_ip_opt) {
  923. move_from_unaligned32(requested_nip, requested_ip_opt);
  924. }
  925. switch (state[0]) {
  926. case DHCPDISCOVER:
  927. log1("received %s", "DISCOVER");
  928. send_offer(&packet, static_lease_nip, lease, requested_nip, arpping_ms);
  929. break;
  930. case DHCPREQUEST:
  931. log1("received %s", "REQUEST");
  932. /* RFC 2131:
  933. o DHCPREQUEST generated during SELECTING state:
  934. Client inserts the address of the selected server in 'server
  935. identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
  936. filled in with the yiaddr value from the chosen DHCPOFFER.
  937. Note that the client may choose to collect several DHCPOFFER
  938. messages and select the "best" offer. The client indicates its
  939. selection by identifying the offering server in the DHCPREQUEST
  940. message. If the client receives no acceptable offers, the client
  941. may choose to try another DHCPDISCOVER message. Therefore, the
  942. servers may not receive a specific DHCPREQUEST from which they can
  943. decide whether or not the client has accepted the offer.
  944. o DHCPREQUEST generated during INIT-REBOOT state:
  945. 'server identifier' MUST NOT be filled in, 'requested IP address'
  946. option MUST be filled in with client's notion of its previously
  947. assigned address. 'ciaddr' MUST be zero. The client is seeking to
  948. verify a previously allocated, cached configuration. Server SHOULD
  949. send a DHCPNAK message to the client if the 'requested IP address'
  950. is incorrect, or is on the wrong network.
  951. Determining whether a client in the INIT-REBOOT state is on the
  952. correct network is done by examining the contents of 'giaddr', the
  953. 'requested IP address' option, and a database lookup. If the DHCP
  954. server detects that the client is on the wrong net (i.e., the
  955. result of applying the local subnet mask or remote subnet mask (if
  956. 'giaddr' is not zero) to 'requested IP address' option value
  957. doesn't match reality), then the server SHOULD send a DHCPNAK
  958. message to the client.
  959. If the network is correct, then the DHCP server should check if
  960. the client's notion of its IP address is correct. If not, then the
  961. server SHOULD send a DHCPNAK message to the client. If the DHCP
  962. server has no record of this client, then it MUST remain silent,
  963. and MAY output a warning to the network administrator. This
  964. behavior is necessary for peaceful coexistence of non-
  965. communicating DHCP servers on the same wire.
  966. If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
  967. the same subnet as the server. The server MUST broadcast the
  968. DHCPNAK message to the 0xffffffff broadcast address because the
  969. client may not have a correct network address or subnet mask, and
  970. the client may not be answering ARP requests.
  971. If 'giaddr' is set in the DHCPREQUEST message, the client is on a
  972. different subnet. The server MUST set the broadcast bit in the
  973. DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
  974. client, because the client may not have a correct network address
  975. or subnet mask, and the client may not be answering ARP requests.
  976. o DHCPREQUEST generated during RENEWING state:
  977. 'server identifier' MUST NOT be filled in, 'requested IP address'
  978. option MUST NOT be filled in, 'ciaddr' MUST be filled in with
  979. client's IP address. In this situation, the client is completely
  980. configured, and is trying to extend its lease. This message will
  981. be unicast, so no relay agents will be involved in its
  982. transmission. Because 'giaddr' is therefore not filled in, the
  983. DHCP server will trust the value in 'ciaddr', and use it when
  984. replying to the client.
  985. A client MAY choose to renew or extend its lease prior to T1. The
  986. server may choose not to extend the lease (as a policy decision by
  987. the network administrator), but should return a DHCPACK message
  988. regardless.
  989. o DHCPREQUEST generated during REBINDING state:
  990. 'server identifier' MUST NOT be filled in, 'requested IP address'
  991. option MUST NOT be filled in, 'ciaddr' MUST be filled in with
  992. client's IP address. In this situation, the client is completely
  993. configured, and is trying to extend its lease. This message MUST
  994. be broadcast to the 0xffffffff IP broadcast address. The DHCP
  995. server SHOULD check 'ciaddr' for correctness before replying to
  996. the DHCPREQUEST.
  997. The DHCPREQUEST from a REBINDING client is intended to accommodate
  998. sites that have multiple DHCP servers and a mechanism for
  999. maintaining consistency among leases managed by multiple servers.
  1000. A DHCP server MAY extend a client's lease only if it has local
  1001. administrative authority to do so.
  1002. */
  1003. if (!requested_ip_opt) {
  1004. requested_nip = packet.ciaddr;
  1005. if (requested_nip == 0) {
  1006. log1("no requested IP and no ciaddr, ignoring");
  1007. break;
  1008. }
  1009. }
  1010. if (lease && requested_nip == lease->lease_nip) {
  1011. /* client requested or configured IP matches the lease.
  1012. * ACK it, and bump lease expiration time. */
  1013. send_ACK(&packet, lease->lease_nip);
  1014. break;
  1015. }
  1016. /* No lease for this MAC, or lease IP != requested IP */
  1017. if (server_id_opt /* client is in SELECTING state */
  1018. || requested_ip_opt /* client is in INIT-REBOOT state */
  1019. ) {
  1020. /* "No, we don't have this IP for you" */
  1021. send_NAK(&packet);
  1022. } /* else: client is in RENEWING or REBINDING, do not answer */
  1023. break;
  1024. case DHCPDECLINE:
  1025. /* RFC 2131:
  1026. * "If the server receives a DHCPDECLINE message,
  1027. * the client has discovered through some other means
  1028. * that the suggested network address is already
  1029. * in use. The server MUST mark the network address
  1030. * as not available and SHOULD notify the local
  1031. * sysadmin of a possible configuration problem."
  1032. *
  1033. * SERVER_ID must be present,
  1034. * REQUESTED_IP must be present,
  1035. * chaddr must be filled in,
  1036. * ciaddr must be 0 (we do not check this)
  1037. */
  1038. log1("received %s", "DECLINE");
  1039. if (server_id_opt
  1040. && requested_ip_opt
  1041. && lease /* chaddr matches this lease */
  1042. && requested_nip == lease->lease_nip
  1043. ) {
  1044. memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
  1045. lease->expires = time(NULL) + server_data.decline_time;
  1046. }
  1047. break;
  1048. case DHCPRELEASE:
  1049. /* "Upon receipt of a DHCPRELEASE message, the server
  1050. * marks the network address as not allocated."
  1051. *
  1052. * SERVER_ID must be present,
  1053. * REQUESTED_IP must not be present (we do not check this),
  1054. * chaddr must be filled in,
  1055. * ciaddr must be filled in
  1056. */
  1057. log1("received %s", "RELEASE");
  1058. if (server_id_opt
  1059. && lease /* chaddr matches this lease */
  1060. && packet.ciaddr == lease->lease_nip
  1061. ) {
  1062. lease->expires = time(NULL);
  1063. }
  1064. break;
  1065. case DHCPINFORM:
  1066. log1("received %s", "INFORM");
  1067. send_inform(&packet);
  1068. break;
  1069. }
  1070. }
  1071. ret0:
  1072. retval = 0;
  1073. ret:
  1074. /*if (server_data.pidfile) - server_data.pidfile is never NULL */
  1075. remove_pidfile(server_data.pidfile);
  1076. return retval;
  1077. }