dhcpd.c 36 KB

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