3
0

dhcpd.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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. }
  511. static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
  512. {
  513. if (dhcp_pkt->gateway_nip)
  514. send_packet_to_relay(dhcp_pkt);
  515. else
  516. send_packet_to_client(dhcp_pkt, force_broadcast);
  517. }
  518. static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
  519. {
  520. /* Sets op, htype, hlen, cookie fields
  521. * and adds DHCP_MESSAGE_TYPE option */
  522. udhcp_init_header(packet, type);
  523. packet->xid = oldpacket->xid;
  524. memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
  525. packet->flags = oldpacket->flags;
  526. packet->gateway_nip = oldpacket->gateway_nip;
  527. packet->ciaddr = oldpacket->ciaddr;
  528. udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
  529. }
  530. /* Fill options field, siaddr_nip, and sname and boot_file fields.
  531. * TODO: teach this code to use overload option.
  532. */
  533. static void add_server_options(struct dhcp_packet *packet)
  534. {
  535. struct option_set *curr = server_config.options;
  536. while (curr) {
  537. if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
  538. udhcp_add_binary_option(packet, curr->data);
  539. curr = curr->next;
  540. }
  541. packet->siaddr_nip = server_config.siaddr_nip;
  542. if (server_config.sname)
  543. strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
  544. if (server_config.boot_file)
  545. strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
  546. }
  547. static uint32_t select_lease_time(struct dhcp_packet *packet)
  548. {
  549. uint32_t lease_time_sec = server_config.max_lease_sec;
  550. uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME);
  551. if (lease_time_opt) {
  552. move_from_unaligned32(lease_time_sec, lease_time_opt);
  553. lease_time_sec = ntohl(lease_time_sec);
  554. if (lease_time_sec > server_config.max_lease_sec)
  555. lease_time_sec = server_config.max_lease_sec;
  556. if (lease_time_sec < server_config.min_lease_sec)
  557. lease_time_sec = server_config.min_lease_sec;
  558. }
  559. return lease_time_sec;
  560. }
  561. /* We got a DHCP DISCOVER. Send an OFFER. */
  562. /* NOINLINE: limit stack usage in caller */
  563. static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
  564. uint32_t static_lease_nip,
  565. struct dyn_lease *lease,
  566. uint8_t *requested_ip_opt,
  567. unsigned arpping_ms)
  568. {
  569. struct dhcp_packet packet;
  570. uint32_t lease_time_sec;
  571. struct in_addr addr;
  572. init_packet(&packet, oldpacket, DHCPOFFER);
  573. /* If it is a static lease, use its IP */
  574. packet.yiaddr = static_lease_nip;
  575. /* Else: */
  576. if (!static_lease_nip) {
  577. /* We have no static lease for client's chaddr */
  578. uint32_t req_nip;
  579. const char *p_host_name;
  580. if (lease) {
  581. /* We have a dynamic lease for client's chaddr.
  582. * Reuse its IP (even if lease is expired).
  583. * Note that we ignore requested IP in this case.
  584. */
  585. packet.yiaddr = lease->lease_nip;
  586. }
  587. /* Or: if client has requested an IP */
  588. else if (requested_ip_opt != NULL
  589. /* (read IP) */
  590. && (move_from_unaligned32(req_nip, requested_ip_opt), 1)
  591. /* and the IP is in the lease range */
  592. && ntohl(req_nip) >= server_config.start_ip
  593. && ntohl(req_nip) <= server_config.end_ip
  594. /* and */
  595. && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
  596. || is_expired_lease(lease) /* or is taken, but expired */
  597. )
  598. ) {
  599. packet.yiaddr = req_nip;
  600. }
  601. else {
  602. /* Otherwise, find a free IP */
  603. packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr, arpping_ms);
  604. }
  605. if (!packet.yiaddr) {
  606. bb_error_msg("no free IP addresses. OFFER abandoned");
  607. return;
  608. }
  609. /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
  610. p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
  611. lease = add_lease(packet.chaddr, packet.yiaddr,
  612. server_config.offer_time,
  613. p_host_name,
  614. p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
  615. );
  616. if (!lease) {
  617. bb_error_msg("no free IP addresses. OFFER abandoned");
  618. return;
  619. }
  620. }
  621. lease_time_sec = select_lease_time(oldpacket);
  622. udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
  623. add_server_options(&packet);
  624. addr.s_addr = packet.yiaddr;
  625. bb_error_msg("sending OFFER of %s", inet_ntoa(addr));
  626. /* send_packet emits error message itself if it detects failure */
  627. send_packet(&packet, /*force_bcast:*/ 0);
  628. }
  629. /* NOINLINE: limit stack usage in caller */
  630. static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
  631. {
  632. struct dhcp_packet packet;
  633. init_packet(&packet, oldpacket, DHCPNAK);
  634. log1("sending %s", "NAK");
  635. send_packet(&packet, /*force_bcast:*/ 1);
  636. }
  637. /* NOINLINE: limit stack usage in caller */
  638. static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
  639. {
  640. struct dhcp_packet packet;
  641. uint32_t lease_time_sec;
  642. struct in_addr addr;
  643. const char *p_host_name;
  644. init_packet(&packet, oldpacket, DHCPACK);
  645. packet.yiaddr = yiaddr;
  646. lease_time_sec = select_lease_time(oldpacket);
  647. udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
  648. add_server_options(&packet);
  649. addr.s_addr = yiaddr;
  650. bb_error_msg("sending ACK to %s", inet_ntoa(addr));
  651. send_packet(&packet, /*force_bcast:*/ 0);
  652. p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
  653. add_lease(packet.chaddr, packet.yiaddr,
  654. lease_time_sec,
  655. p_host_name,
  656. p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
  657. );
  658. if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
  659. /* rewrite the file with leases at every new acceptance */
  660. write_leases();
  661. }
  662. }
  663. /* NOINLINE: limit stack usage in caller */
  664. static NOINLINE void send_inform(struct dhcp_packet *oldpacket)
  665. {
  666. struct dhcp_packet packet;
  667. /* "If a client has obtained a network address through some other means
  668. * (e.g., manual configuration), it may use a DHCPINFORM request message
  669. * to obtain other local configuration parameters. Servers receiving a
  670. * DHCPINFORM message construct a DHCPACK message with any local
  671. * configuration parameters appropriate for the client without:
  672. * allocating a new address, checking for an existing binding, filling
  673. * in 'yiaddr' or including lease time parameters. The servers SHOULD
  674. * unicast the DHCPACK reply to the address given in the 'ciaddr' field
  675. * of the DHCPINFORM message.
  676. * ...
  677. * The server responds to a DHCPINFORM message by sending a DHCPACK
  678. * message directly to the address given in the 'ciaddr' field
  679. * of the DHCPINFORM message. The server MUST NOT send a lease
  680. * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
  681. */
  682. //TODO: do a few sanity checks: is ciaddr set?
  683. //Better yet: is ciaddr == IP source addr?
  684. init_packet(&packet, oldpacket, DHCPACK);
  685. add_server_options(&packet);
  686. send_packet(&packet, /*force_bcast:*/ 0);
  687. }
  688. int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  689. int udhcpd_main(int argc UNUSED_PARAM, char **argv)
  690. {
  691. int server_socket = -1, retval;
  692. uint8_t *state;
  693. unsigned timeout_end;
  694. unsigned num_ips;
  695. unsigned opt;
  696. struct option_set *option;
  697. char *str_I = str_I;
  698. const char *str_a = "2000";
  699. unsigned arpping_ms;
  700. IF_FEATURE_UDHCP_PORT(char *str_P;)
  701. setup_common_bufsiz();
  702. IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;)
  703. IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
  704. opt = getopt32(argv, "^"
  705. "fSI:va:"IF_FEATURE_UDHCP_PORT("P:")
  706. "\0"
  707. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  708. "vv"
  709. #endif
  710. , &str_I
  711. , &str_a
  712. IF_FEATURE_UDHCP_PORT(, &str_P)
  713. IF_UDHCP_VERBOSE(, &dhcp_verbose)
  714. );
  715. if (!(opt & 1)) { /* no -f */
  716. bb_daemonize_or_rexec(0, argv);
  717. logmode = LOGMODE_NONE;
  718. }
  719. /* update argv after the possible vfork+exec in daemonize */
  720. argv += optind;
  721. if (opt & 2) { /* -S */
  722. openlog(applet_name, LOG_PID, LOG_DAEMON);
  723. logmode |= LOGMODE_SYSLOG;
  724. }
  725. if (opt & 4) { /* -I */
  726. len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET);
  727. server_config.server_nip = lsa->u.sin.sin_addr.s_addr;
  728. free(lsa);
  729. }
  730. #if ENABLE_FEATURE_UDHCP_PORT
  731. if (opt & 32) { /* -P */
  732. SERVER_PORT = xatou16(str_P);
  733. CLIENT_PORT = SERVER_PORT + 1;
  734. }
  735. #endif
  736. arpping_ms = xatou(str_a);
  737. /* Would rather not do read_config before daemonization -
  738. * otherwise NOMMU machines will parse config twice */
  739. read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
  740. /* prevent poll timeout overflow */
  741. if (server_config.auto_time > INT_MAX / 1000)
  742. server_config.auto_time = INT_MAX / 1000;
  743. /* Make sure fd 0,1,2 are open */
  744. bb_sanitize_stdio();
  745. /* Create pidfile */
  746. write_pidfile(server_config.pidfile);
  747. /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
  748. bb_error_msg("started, v"BB_VER);
  749. option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
  750. server_config.max_lease_sec = DEFAULT_LEASE_TIME;
  751. if (option) {
  752. move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
  753. server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
  754. }
  755. /* Sanity check */
  756. num_ips = server_config.end_ip - server_config.start_ip + 1;
  757. if (server_config.max_leases > num_ips) {
  758. bb_error_msg("max_leases=%u is too big, setting to %u",
  759. (unsigned)server_config.max_leases, num_ips);
  760. server_config.max_leases = num_ips;
  761. }
  762. /* this sets g_leases */
  763. SET_PTR_TO_GLOBALS(xzalloc(server_config.max_leases * sizeof(g_leases[0])));
  764. read_leases(server_config.lease_file);
  765. if (udhcp_read_interface(server_config.interface,
  766. &server_config.ifindex,
  767. (server_config.server_nip == 0 ? &server_config.server_nip : NULL),
  768. server_config.server_mac)
  769. ) {
  770. retval = 1;
  771. goto ret;
  772. }
  773. /* Setup the signal pipe */
  774. udhcp_sp_setup();
  775. continue_with_autotime:
  776. timeout_end = monotonic_sec() + server_config.auto_time;
  777. while (1) { /* loop until universe collapses */
  778. struct pollfd pfds[2];
  779. struct dhcp_packet packet;
  780. int bytes;
  781. int tv;
  782. uint8_t *server_id_opt;
  783. uint8_t *requested_ip_opt;
  784. uint32_t requested_nip = requested_nip; /* for compiler */
  785. uint32_t static_lease_nip;
  786. struct dyn_lease *lease, fake_lease;
  787. if (server_socket < 0) {
  788. server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
  789. server_config.interface);
  790. }
  791. udhcp_sp_fd_set(pfds, server_socket);
  792. new_tv:
  793. tv = -1;
  794. if (server_config.auto_time) {
  795. tv = timeout_end - monotonic_sec();
  796. if (tv <= 0) {
  797. write_leases:
  798. write_leases();
  799. goto continue_with_autotime;
  800. }
  801. tv *= 1000;
  802. }
  803. /* Block here waiting for either signal or packet */
  804. retval = poll(pfds, 2, tv);
  805. if (retval <= 0) {
  806. if (retval == 0)
  807. goto write_leases;
  808. if (errno == EINTR)
  809. goto new_tv;
  810. /* < 0 and not EINTR: should not happen */
  811. bb_perror_msg_and_die("poll");
  812. }
  813. if (pfds[0].revents) switch (udhcp_sp_read()) {
  814. case SIGUSR1:
  815. bb_error_msg("received %s", "SIGUSR1");
  816. write_leases();
  817. /* why not just reset the timeout, eh */
  818. goto continue_with_autotime;
  819. case SIGTERM:
  820. bb_error_msg("received %s", "SIGTERM");
  821. write_leases();
  822. goto ret0;
  823. }
  824. /* Is it a packet? */
  825. if (!pfds[1].revents)
  826. continue; /* no */
  827. /* Note: we do not block here, we block on poll() instead.
  828. * Blocking here would prevent SIGTERM from working:
  829. * socket read inside this call is restarted on caught signals.
  830. */
  831. bytes = udhcp_recv_kernel_packet(&packet, server_socket);
  832. if (bytes < 0) {
  833. /* bytes can also be -2 ("bad packet data") */
  834. if (bytes == -1 && errno != EINTR) {
  835. log1("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
  836. close(server_socket);
  837. server_socket = -1;
  838. }
  839. continue;
  840. }
  841. if (packet.hlen != 6) {
  842. bb_error_msg("MAC length != 6, ignoring packet");
  843. continue;
  844. }
  845. if (packet.op != BOOTREQUEST) {
  846. bb_error_msg("not a REQUEST, ignoring packet");
  847. continue;
  848. }
  849. state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
  850. if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
  851. bb_error_msg("no or bad message type option, ignoring packet");
  852. continue;
  853. }
  854. /* Get SERVER_ID if present */
  855. server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID);
  856. if (server_id_opt) {
  857. uint32_t server_id_network_order;
  858. move_from_unaligned32(server_id_network_order, server_id_opt);
  859. if (server_id_network_order != server_config.server_nip) {
  860. /* client talks to somebody else */
  861. log1("server ID doesn't match, ignoring");
  862. continue;
  863. }
  864. }
  865. /* Look for a static/dynamic lease */
  866. static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
  867. if (static_lease_nip) {
  868. bb_error_msg("found static lease: %x", static_lease_nip);
  869. memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
  870. fake_lease.lease_nip = static_lease_nip;
  871. fake_lease.expires = 0;
  872. lease = &fake_lease;
  873. } else {
  874. lease = find_lease_by_mac(packet.chaddr);
  875. }
  876. /* Get REQUESTED_IP if present */
  877. requested_ip_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP);
  878. if (requested_ip_opt) {
  879. move_from_unaligned32(requested_nip, requested_ip_opt);
  880. }
  881. switch (state[0]) {
  882. case DHCPDISCOVER:
  883. log1("received %s", "DISCOVER");
  884. send_offer(&packet, static_lease_nip, lease, requested_ip_opt, arpping_ms);
  885. break;
  886. case DHCPREQUEST:
  887. log1("received %s", "REQUEST");
  888. /* RFC 2131:
  889. o DHCPREQUEST generated during SELECTING state:
  890. Client inserts the address of the selected server in 'server
  891. identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
  892. filled in with the yiaddr value from the chosen DHCPOFFER.
  893. Note that the client may choose to collect several DHCPOFFER
  894. messages and select the "best" offer. The client indicates its
  895. selection by identifying the offering server in the DHCPREQUEST
  896. message. If the client receives no acceptable offers, the client
  897. may choose to try another DHCPDISCOVER message. Therefore, the
  898. servers may not receive a specific DHCPREQUEST from which they can
  899. decide whether or not the client has accepted the offer.
  900. o DHCPREQUEST generated during INIT-REBOOT state:
  901. 'server identifier' MUST NOT be filled in, 'requested IP address'
  902. option MUST be filled in with client's notion of its previously
  903. assigned address. 'ciaddr' MUST be zero. The client is seeking to
  904. verify a previously allocated, cached configuration. Server SHOULD
  905. send a DHCPNAK message to the client if the 'requested IP address'
  906. is incorrect, or is on the wrong network.
  907. Determining whether a client in the INIT-REBOOT state is on the
  908. correct network is done by examining the contents of 'giaddr', the
  909. 'requested IP address' option, and a database lookup. If the DHCP
  910. server detects that the client is on the wrong net (i.e., the
  911. result of applying the local subnet mask or remote subnet mask (if
  912. 'giaddr' is not zero) to 'requested IP address' option value
  913. doesn't match reality), then the server SHOULD send a DHCPNAK
  914. message to the client.
  915. If the network is correct, then the DHCP server should check if
  916. the client's notion of its IP address is correct. If not, then the
  917. server SHOULD send a DHCPNAK message to the client. If the DHCP
  918. server has no record of this client, then it MUST remain silent,
  919. and MAY output a warning to the network administrator. This
  920. behavior is necessary for peaceful coexistence of non-
  921. communicating DHCP servers on the same wire.
  922. If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
  923. the same subnet as the server. The server MUST broadcast the
  924. DHCPNAK message to the 0xffffffff broadcast address because the
  925. client may not have a correct network address or subnet mask, and
  926. the client may not be answering ARP requests.
  927. If 'giaddr' is set in the DHCPREQUEST message, the client is on a
  928. different subnet. The server MUST set the broadcast bit in the
  929. DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
  930. client, because the client may not have a correct network address
  931. or subnet mask, and the client may not be answering ARP requests.
  932. o DHCPREQUEST generated during RENEWING state:
  933. 'server identifier' MUST NOT be filled in, 'requested IP address'
  934. option MUST NOT be filled in, 'ciaddr' MUST be filled in with
  935. client's IP address. In this situation, the client is completely
  936. configured, and is trying to extend its lease. This message will
  937. be unicast, so no relay agents will be involved in its
  938. transmission. Because 'giaddr' is therefore not filled in, the
  939. DHCP server will trust the value in 'ciaddr', and use it when
  940. replying to the client.
  941. A client MAY choose to renew or extend its lease prior to T1. The
  942. server may choose not to extend the lease (as a policy decision by
  943. the network administrator), but should return a DHCPACK message
  944. regardless.
  945. o DHCPREQUEST generated during REBINDING state:
  946. 'server identifier' MUST NOT be filled in, 'requested IP address'
  947. option MUST NOT be filled in, 'ciaddr' MUST be filled in with
  948. client's IP address. In this situation, the client is completely
  949. configured, and is trying to extend its lease. This message MUST
  950. be broadcast to the 0xffffffff IP broadcast address. The DHCP
  951. server SHOULD check 'ciaddr' for correctness before replying to
  952. the DHCPREQUEST.
  953. The DHCPREQUEST from a REBINDING client is intended to accommodate
  954. sites that have multiple DHCP servers and a mechanism for
  955. maintaining consistency among leases managed by multiple servers.
  956. A DHCP server MAY extend a client's lease only if it has local
  957. administrative authority to do so.
  958. */
  959. if (!requested_ip_opt) {
  960. requested_nip = packet.ciaddr;
  961. if (requested_nip == 0) {
  962. log1("no requested IP and no ciaddr, ignoring");
  963. break;
  964. }
  965. }
  966. if (lease && requested_nip == lease->lease_nip) {
  967. /* client requested or configured IP matches the lease.
  968. * ACK it, and bump lease expiration time. */
  969. send_ACK(&packet, lease->lease_nip);
  970. break;
  971. }
  972. /* No lease for this MAC, or lease IP != requested IP */
  973. if (server_id_opt /* client is in SELECTING state */
  974. || requested_ip_opt /* client is in INIT-REBOOT state */
  975. ) {
  976. /* "No, we don't have this IP for you" */
  977. send_NAK(&packet);
  978. } /* else: client is in RENEWING or REBINDING, do not answer */
  979. break;
  980. case DHCPDECLINE:
  981. /* RFC 2131:
  982. * "If the server receives a DHCPDECLINE message,
  983. * the client has discovered through some other means
  984. * that the suggested network address is already
  985. * in use. The server MUST mark the network address
  986. * as not available and SHOULD notify the local
  987. * sysadmin of a possible configuration problem."
  988. *
  989. * SERVER_ID must be present,
  990. * REQUESTED_IP must be present,
  991. * chaddr must be filled in,
  992. * ciaddr must be 0 (we do not check this)
  993. */
  994. log1("received %s", "DECLINE");
  995. if (server_id_opt
  996. && requested_ip_opt
  997. && lease /* chaddr matches this lease */
  998. && requested_nip == lease->lease_nip
  999. ) {
  1000. memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
  1001. lease->expires = time(NULL) + server_config.decline_time;
  1002. }
  1003. break;
  1004. case DHCPRELEASE:
  1005. /* "Upon receipt of a DHCPRELEASE message, the server
  1006. * marks the network address as not allocated."
  1007. *
  1008. * SERVER_ID must be present,
  1009. * REQUESTED_IP must not be present (we do not check this),
  1010. * chaddr must be filled in,
  1011. * ciaddr must be filled in
  1012. */
  1013. log1("received %s", "RELEASE");
  1014. if (server_id_opt
  1015. && lease /* chaddr matches this lease */
  1016. && packet.ciaddr == lease->lease_nip
  1017. ) {
  1018. lease->expires = time(NULL);
  1019. }
  1020. break;
  1021. case DHCPINFORM:
  1022. log1("received %s", "INFORM");
  1023. send_inform(&packet);
  1024. break;
  1025. }
  1026. }
  1027. ret0:
  1028. retval = 0;
  1029. ret:
  1030. /*if (server_config.pidfile) - server_config.pidfile is never NULL */
  1031. remove_pidfile(server_config.pidfile);
  1032. return retval;
  1033. }