dhcpc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* vi: set sw=4 ts=4: */
  2. /* dhcpc.c
  3. *
  4. * udhcp DHCP client
  5. *
  6. * Russ Dill <Russ.Dill@asu.edu> July 2001
  7. *
  8. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  9. */
  10. #include <getopt.h>
  11. #include "common.h"
  12. #include "dhcpd.h"
  13. #include "dhcpc.h"
  14. #include "options.h"
  15. static int state;
  16. /* Something is definitely wrong here. IPv4 addresses
  17. * in variables of type long?? BTW, we use inet_ntoa()
  18. * in the code. Manpage says that struct in_addr has a member of type long (!)
  19. * which holds IPv4 address, and the struct is passed by value (!!)
  20. */
  21. static unsigned long requested_ip; /* = 0 */
  22. static uint32_t server_addr;
  23. static unsigned long timeout;
  24. static int packet_num; /* = 0 */
  25. static int fd = -1;
  26. #define LISTEN_NONE 0
  27. #define LISTEN_KERNEL 1
  28. #define LISTEN_RAW 2
  29. static int listen_mode;
  30. struct client_config_t client_config;
  31. /* just a little helper */
  32. static void change_mode(int new_mode)
  33. {
  34. DEBUG("entering %s listen mode",
  35. new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
  36. if (fd >= 0) close(fd);
  37. fd = -1;
  38. listen_mode = new_mode;
  39. }
  40. /* perform a renew */
  41. static void perform_renew(void)
  42. {
  43. bb_info_msg("Performing a DHCP renew");
  44. switch (state) {
  45. case BOUND:
  46. change_mode(LISTEN_KERNEL);
  47. case RENEWING:
  48. case REBINDING:
  49. state = RENEW_REQUESTED;
  50. break;
  51. case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
  52. udhcp_run_script(NULL, "deconfig");
  53. case REQUESTING:
  54. case RELEASED:
  55. change_mode(LISTEN_RAW);
  56. state = INIT_SELECTING;
  57. break;
  58. case INIT_SELECTING:
  59. break;
  60. }
  61. /* start things over */
  62. packet_num = 0;
  63. /* Kill any timeouts because the user wants this to hurry along */
  64. timeout = 0;
  65. }
  66. /* perform a release */
  67. static void perform_release(void)
  68. {
  69. char buffer[16];
  70. struct in_addr temp_addr;
  71. /* send release packet */
  72. if (state == BOUND || state == RENEWING || state == REBINDING) {
  73. temp_addr.s_addr = server_addr;
  74. sprintf(buffer, "%s", inet_ntoa(temp_addr));
  75. temp_addr.s_addr = requested_ip;
  76. bb_info_msg("Unicasting a release of %s to %s",
  77. inet_ntoa(temp_addr), buffer);
  78. send_release(server_addr, requested_ip); /* unicast */
  79. udhcp_run_script(NULL, "deconfig");
  80. }
  81. bb_info_msg("Entering released state");
  82. change_mode(LISTEN_NONE);
  83. state = RELEASED;
  84. timeout = 0x7fffffff;
  85. }
  86. static void client_background(void)
  87. {
  88. udhcp_background(client_config.pidfile);
  89. client_config.foreground = 1; /* Do not fork again. */
  90. client_config.background_if_no_lease = 0;
  91. }
  92. static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
  93. {
  94. uint8_t *storage;
  95. int len = strlen(str);
  96. if (len > 255) len = 255;
  97. storage = xzalloc(len + extra + OPT_DATA);
  98. storage[OPT_CODE] = code;
  99. storage[OPT_LEN] = len + extra;
  100. memcpy(storage + extra + OPT_DATA, str, len);
  101. return storage;
  102. }
  103. int udhcpc_main(int argc, char *argv[])
  104. {
  105. uint8_t *temp, *message;
  106. char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_t;
  107. unsigned long t1 = 0, t2 = 0, xid = 0;
  108. unsigned long start = 0, lease = 0;
  109. long now;
  110. unsigned opt;
  111. int max_fd;
  112. int sig;
  113. int retval;
  114. int len;
  115. int no_clientid = 0;
  116. fd_set rfds;
  117. struct timeval tv;
  118. struct dhcpMessage packet;
  119. struct in_addr temp_addr;
  120. enum {
  121. OPT_c = 1 << 0,
  122. OPT_C = 1 << 1,
  123. OPT_V = 1 << 2,
  124. OPT_f = 1 << 3,
  125. OPT_b = 1 << 4,
  126. OPT_H = 1 << 5,
  127. OPT_h = 1 << 6,
  128. OPT_F = 1 << 7,
  129. OPT_i = 1 << 8,
  130. OPT_n = 1 << 9,
  131. OPT_p = 1 << 10,
  132. OPT_q = 1 << 11,
  133. OPT_R = 1 << 12,
  134. OPT_r = 1 << 13,
  135. OPT_s = 1 << 14,
  136. OPT_T = 1 << 15,
  137. OPT_t = 1 << 16,
  138. OPT_v = 1 << 17,
  139. };
  140. #if ENABLE_GETOPT_LONG
  141. static const struct option arg_options[] = {
  142. { "clientid", required_argument, 0, 'c' },
  143. { "clientid-none", no_argument, 0, 'C' },
  144. { "vendorclass", required_argument, 0, 'V' },
  145. { "foreground", no_argument, 0, 'f' },
  146. { "background", no_argument, 0, 'b' },
  147. { "hostname", required_argument, 0, 'H' },
  148. { "hostname", required_argument, 0, 'h' },
  149. { "fqdn", required_argument, 0, 'F' },
  150. { "interface", required_argument, 0, 'i' },
  151. { "now", no_argument, 0, 'n' },
  152. { "pidfile", required_argument, 0, 'p' },
  153. { "quit", no_argument, 0, 'q' },
  154. { "release", no_argument, 0, 'R' },
  155. { "request", required_argument, 0, 'r' },
  156. { "script", required_argument, 0, 's' },
  157. { "timeout", required_argument, 0, 'T' },
  158. { "version", no_argument, 0, 'v' },
  159. { "retries", required_argument, 0, 't' },
  160. { 0, 0, 0, 0 }
  161. };
  162. #endif
  163. /* Default options. */
  164. client_config.interface = "eth0";
  165. client_config.script = DEFAULT_SCRIPT;
  166. client_config.retries = 3;
  167. client_config.timeout = 3;
  168. /* Parse command line */
  169. opt_complementary = "?:c--C:C--c" // mutually exclusive
  170. ":hH:Hh"; // -h and -H are the same
  171. #if ENABLE_GETOPT_LONG
  172. applet_long_options = arg_options;
  173. #endif
  174. opt = getopt32(argc, argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:v",
  175. &str_c, &str_V, &str_h, &str_h, &str_F,
  176. &client_config.interface, &client_config.pidfile, &str_r,
  177. &client_config.script, &str_T, &str_t
  178. );
  179. if (opt & OPT_c)
  180. client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
  181. if (opt & OPT_C)
  182. no_clientid = 1;
  183. if (opt & OPT_V)
  184. client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
  185. if (opt & OPT_f)
  186. client_config.foreground = 1;
  187. if (opt & OPT_b)
  188. client_config.background_if_no_lease = 1;
  189. if (opt & OPT_h)
  190. client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
  191. if (opt & OPT_F) {
  192. client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
  193. /* Flags: 0000NEOS
  194. S: 1 => Client requests Server to update A RR in DNS as well as PTR
  195. O: 1 => Server indicates to client that DNS has been updated regardless
  196. E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
  197. N: 1 => Client requests Server to not update DNS
  198. */
  199. client_config.fqdn[OPT_DATA + 0] = 0x1;
  200. /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
  201. /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
  202. }
  203. // if (opt & OPT_i) client_config.interface = ...
  204. if (opt & OPT_n)
  205. client_config.abort_if_no_lease = 1;
  206. // if (opt & OPT_p) client_config.pidfile = ...
  207. if (opt & OPT_q)
  208. client_config.quit_after_lease = 1;
  209. if (opt & OPT_R)
  210. client_config.release_on_quit = 1;
  211. if (opt & OPT_r)
  212. requested_ip = inet_addr(str_r);
  213. // if (opt & OPT_s) client_config.script = ...
  214. if (opt & OPT_T)
  215. client_config.timeout = xatoi_u(str_T);
  216. if (opt & OPT_t)
  217. client_config.retries = xatoi_u(str_t);
  218. if (opt & OPT_v) {
  219. printf("version %s\n\n", BB_VER);
  220. return 0;
  221. }
  222. /* Start the log, sanitize fd's, and write a pid file */
  223. udhcp_start_log_and_pid(client_config.pidfile);
  224. if (read_interface(client_config.interface, &client_config.ifindex,
  225. NULL, client_config.arp) < 0)
  226. return 1;
  227. /* if not set, and not suppressed, setup the default client ID */
  228. if (!client_config.clientid && !no_clientid) {
  229. client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
  230. client_config.clientid[OPT_DATA] = 1;
  231. memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
  232. }
  233. if (!client_config.vendorclass)
  234. client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
  235. /* setup the signal pipe */
  236. udhcp_sp_setup();
  237. state = INIT_SELECTING;
  238. udhcp_run_script(NULL, "deconfig");
  239. change_mode(LISTEN_RAW);
  240. for (;;) {
  241. tv.tv_sec = timeout - uptime();
  242. tv.tv_usec = 0;
  243. if (listen_mode != LISTEN_NONE && fd < 0) {
  244. if (listen_mode == LISTEN_KERNEL)
  245. fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
  246. else
  247. fd = raw_socket(client_config.ifindex);
  248. }
  249. max_fd = udhcp_sp_fd_set(&rfds, fd);
  250. if (tv.tv_sec > 0) {
  251. DEBUG("Waiting on select...");
  252. retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
  253. } else retval = 0; /* If we already timed out, fall through */
  254. now = uptime();
  255. if (retval == 0) {
  256. /* timeout dropped to zero */
  257. switch (state) {
  258. case INIT_SELECTING:
  259. if (packet_num < client_config.retries) {
  260. if (packet_num == 0)
  261. xid = random_xid();
  262. /* send discover packet */
  263. send_discover(xid, requested_ip); /* broadcast */
  264. timeout = now + client_config.timeout;
  265. packet_num++;
  266. } else {
  267. udhcp_run_script(NULL, "leasefail");
  268. if (client_config.background_if_no_lease) {
  269. bb_info_msg("No lease, forking to background");
  270. client_background();
  271. } else if (client_config.abort_if_no_lease) {
  272. bb_info_msg("No lease, failing");
  273. return 1;
  274. }
  275. /* wait to try again */
  276. packet_num = 0;
  277. timeout = now + 60;
  278. }
  279. break;
  280. case RENEW_REQUESTED:
  281. case REQUESTING:
  282. if (packet_num < client_config.retries) {
  283. /* send request packet */
  284. if (state == RENEW_REQUESTED)
  285. send_renew(xid, server_addr, requested_ip); /* unicast */
  286. else send_selecting(xid, server_addr, requested_ip); /* broadcast */
  287. timeout = now + ((packet_num == 2) ? 10 : 2);
  288. packet_num++;
  289. } else {
  290. /* timed out, go back to init state */
  291. if (state == RENEW_REQUESTED) udhcp_run_script(NULL, "deconfig");
  292. state = INIT_SELECTING;
  293. timeout = now;
  294. packet_num = 0;
  295. change_mode(LISTEN_RAW);
  296. }
  297. break;
  298. case BOUND:
  299. /* Lease is starting to run out, time to enter renewing state */
  300. state = RENEWING;
  301. change_mode(LISTEN_KERNEL);
  302. DEBUG("Entering renew state");
  303. /* fall right through */
  304. case RENEWING:
  305. /* Either set a new T1, or enter REBINDING state */
  306. if ((t2 - t1) <= (lease / 14400 + 1)) {
  307. /* timed out, enter rebinding state */
  308. state = REBINDING;
  309. timeout = now + (t2 - t1);
  310. DEBUG("Entering rebinding state");
  311. } else {
  312. /* send a request packet */
  313. send_renew(xid, server_addr, requested_ip); /* unicast */
  314. t1 = (t2 - t1) / 2 + t1;
  315. timeout = t1 + start;
  316. }
  317. break;
  318. case REBINDING:
  319. /* Either set a new T2, or enter INIT state */
  320. if ((lease - t2) <= (lease / 14400 + 1)) {
  321. /* timed out, enter init state */
  322. state = INIT_SELECTING;
  323. bb_info_msg("Lease lost, entering init state");
  324. udhcp_run_script(NULL, "deconfig");
  325. timeout = now;
  326. packet_num = 0;
  327. change_mode(LISTEN_RAW);
  328. } else {
  329. /* send a request packet */
  330. send_renew(xid, 0, requested_ip); /* broadcast */
  331. t2 = (lease - t2) / 2 + t2;
  332. timeout = t2 + start;
  333. }
  334. break;
  335. case RELEASED:
  336. /* yah, I know, *you* say it would never happen */
  337. timeout = 0x7fffffff;
  338. break;
  339. }
  340. } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
  341. /* a packet is ready, read it */
  342. if (listen_mode == LISTEN_KERNEL)
  343. len = udhcp_get_packet(&packet, fd);
  344. else len = get_raw_packet(&packet, fd);
  345. if (len == -1 && errno != EINTR) {
  346. DEBUG("error on read, %s, reopening socket", strerror(errno));
  347. change_mode(listen_mode); /* just close and reopen */
  348. }
  349. if (len < 0) continue;
  350. if (packet.xid != xid) {
  351. DEBUG("Ignoring XID %lx (our xid is %lx)",
  352. (unsigned long) packet.xid, xid);
  353. continue;
  354. }
  355. /* Ignore packets that aren't for us */
  356. if (memcmp(packet.chaddr, client_config.arp, 6)) {
  357. DEBUG("Packet does not have our chaddr - ignoring");
  358. continue;
  359. }
  360. if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
  361. bb_error_msg("cannot get option from packet - ignoring");
  362. continue;
  363. }
  364. switch (state) {
  365. case INIT_SELECTING:
  366. /* Must be a DHCPOFFER to one of our xid's */
  367. if (*message == DHCPOFFER) {
  368. temp = get_option(&packet, DHCP_SERVER_ID);
  369. if (temp) {
  370. /* can be misaligned, thus memcpy */
  371. memcpy(&server_addr, temp, 4);
  372. xid = packet.xid;
  373. requested_ip = packet.yiaddr;
  374. /* enter requesting state */
  375. state = REQUESTING;
  376. timeout = now;
  377. packet_num = 0;
  378. } else {
  379. bb_error_msg("no server ID in message");
  380. }
  381. }
  382. break;
  383. case RENEW_REQUESTED:
  384. case REQUESTING:
  385. case RENEWING:
  386. case REBINDING:
  387. if (*message == DHCPACK) {
  388. temp = get_option(&packet, DHCP_LEASE_TIME);
  389. if (!temp) {
  390. bb_error_msg("no lease time with ACK, using 1 hour lease");
  391. lease = 60 * 60;
  392. } else {
  393. /* can be misaligned, thus memcpy */
  394. memcpy(&lease, temp, 4);
  395. lease = ntohl(lease);
  396. }
  397. /* enter bound state */
  398. t1 = lease / 2;
  399. /* little fixed point for n * .875 */
  400. t2 = (lease * 0x7) >> 3;
  401. temp_addr.s_addr = packet.yiaddr;
  402. bb_info_msg("Lease of %s obtained, lease time %ld",
  403. inet_ntoa(temp_addr), lease);
  404. start = now;
  405. timeout = t1 + start;
  406. requested_ip = packet.yiaddr;
  407. udhcp_run_script(&packet,
  408. ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
  409. state = BOUND;
  410. change_mode(LISTEN_NONE);
  411. if (client_config.quit_after_lease) {
  412. if (client_config.release_on_quit)
  413. perform_release();
  414. return 0;
  415. }
  416. if (!client_config.foreground)
  417. client_background();
  418. } else if (*message == DHCPNAK) {
  419. /* return to init state */
  420. bb_info_msg("Received DHCP NAK");
  421. udhcp_run_script(&packet, "nak");
  422. if (state != REQUESTING)
  423. udhcp_run_script(NULL, "deconfig");
  424. state = INIT_SELECTING;
  425. timeout = now;
  426. requested_ip = 0;
  427. packet_num = 0;
  428. change_mode(LISTEN_RAW);
  429. sleep(3); /* avoid excessive network traffic */
  430. }
  431. break;
  432. /* case BOUND, RELEASED: - ignore all packets */
  433. }
  434. } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
  435. switch (sig) {
  436. case SIGUSR1:
  437. perform_renew();
  438. break;
  439. case SIGUSR2:
  440. perform_release();
  441. break;
  442. case SIGTERM:
  443. bb_info_msg("Received SIGTERM");
  444. if (client_config.release_on_quit)
  445. perform_release();
  446. return 0;
  447. }
  448. } else if (retval == -1 && errno == EINTR) {
  449. /* a signal was caught */
  450. } else {
  451. /* An error occured */
  452. bb_perror_msg("select");
  453. }
  454. }
  455. return 0;
  456. }