3
0

dhcpd.c 37 KB

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