dhcpc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* dhcpc.c
  2. *
  3. * udhcp DHCP client
  4. *
  5. * Russ Dill <Russ.Dill@asu.edu> July 2001
  6. *
  7. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. #include <sys/time.h>
  10. #include <sys/file.h>
  11. #include <unistd.h>
  12. #include <getopt.h>
  13. #include <stdlib.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17. #include <signal.h>
  18. #include <time.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <net/if.h>
  22. #include <errno.h>
  23. #include "common.h"
  24. #include "dhcpd.h"
  25. #include "dhcpc.h"
  26. #include "options.h"
  27. #include "clientpacket.h"
  28. #include "clientsocket.h"
  29. #include "script.h"
  30. #include "socket.h"
  31. #include "signalpipe.h"
  32. static int state;
  33. static unsigned long requested_ip; /* = 0 */
  34. static unsigned long server_addr;
  35. static unsigned long timeout;
  36. static int packet_num; /* = 0 */
  37. static int fd = -1;
  38. #define LISTEN_NONE 0
  39. #define LISTEN_KERNEL 1
  40. #define LISTEN_RAW 2
  41. static int listen_mode;
  42. struct client_config_t client_config = {
  43. /* Default options. */
  44. .abort_if_no_lease = 0,
  45. .foreground = 0,
  46. .quit_after_lease = 0,
  47. .background_if_no_lease = 0,
  48. .interface = "eth0",
  49. .pidfile = NULL,
  50. .script = DEFAULT_SCRIPT,
  51. .clientid = NULL,
  52. .vendorclass = NULL,
  53. .hostname = NULL,
  54. .fqdn = NULL,
  55. .ifindex = 0,
  56. .retries = 3,
  57. .timeout = 3,
  58. .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */
  59. };
  60. #ifndef IN_BUSYBOX
  61. static void ATTRIBUTE_NORETURN show_usage(void)
  62. {
  63. printf(
  64. "Usage: udhcpc [OPTIONS]\n\n"
  65. " -c, --clientid=CLIENTID Set client identifier - type is first char\n"
  66. " -C, --clientid-none Suppress default client identifier\n"
  67. " -V, --vendorclass=CLASSID Set vendor class identifier\n"
  68. " -H, --hostname=HOSTNAME Client hostname\n"
  69. " -h Alias for -H\n"
  70. " -F, --fqdn=FQDN Client fully qualified domain name\n"
  71. " -f, --foreground Do not fork after getting lease\n"
  72. " -b, --background Fork to background if lease cannot be\n"
  73. " immediately negotiated.\n"
  74. " -i, --interface=INTERFACE Interface to use (default: eth0)\n"
  75. " -n, --now Exit with failure if lease cannot be\n"
  76. " immediately negotiated.\n"
  77. " -p, --pidfile=file Store process ID of daemon in file\n"
  78. " -q, --quit Quit after obtaining lease\n"
  79. " -r, --request=IP IP address to request (default: none)\n"
  80. " -s, --script=file Run file at dhcp events (default:\n"
  81. " " DEFAULT_SCRIPT ")\n"
  82. " -T, --timeout=seconds Try to get the lease for the amount of\n"
  83. " seconds (default: 3)\n"
  84. " -t, --retries=NUM Send up to NUM request packets\n"
  85. " -v, --version Display version\n"
  86. );
  87. exit(0);
  88. }
  89. #else
  90. #define show_usage bb_show_usage
  91. extern void show_usage(void) ATTRIBUTE_NORETURN;
  92. #endif
  93. /* just a little helper */
  94. static void change_mode(int new_mode)
  95. {
  96. DEBUG(LOG_INFO, "entering %s listen mode",
  97. new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
  98. if (fd >= 0) close(fd);
  99. fd = -1;
  100. listen_mode = new_mode;
  101. }
  102. /* perform a renew */
  103. static void perform_renew(void)
  104. {
  105. LOG(LOG_INFO, "Performing a DHCP renew");
  106. switch (state) {
  107. case BOUND:
  108. change_mode(LISTEN_KERNEL);
  109. case RENEWING:
  110. case REBINDING:
  111. state = RENEW_REQUESTED;
  112. break;
  113. case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
  114. run_script(NULL, "deconfig");
  115. case REQUESTING:
  116. case RELEASED:
  117. change_mode(LISTEN_RAW);
  118. state = INIT_SELECTING;
  119. break;
  120. case INIT_SELECTING:
  121. break;
  122. }
  123. /* start things over */
  124. packet_num = 0;
  125. /* Kill any timeouts because the user wants this to hurry along */
  126. timeout = 0;
  127. }
  128. /* perform a release */
  129. static void perform_release(void)
  130. {
  131. char buffer[16];
  132. struct in_addr temp_addr;
  133. /* send release packet */
  134. if (state == BOUND || state == RENEWING || state == REBINDING) {
  135. temp_addr.s_addr = server_addr;
  136. sprintf(buffer, "%s", inet_ntoa(temp_addr));
  137. temp_addr.s_addr = requested_ip;
  138. LOG(LOG_INFO, "Unicasting a release of %s to %s",
  139. inet_ntoa(temp_addr), buffer);
  140. send_release(server_addr, requested_ip); /* unicast */
  141. run_script(NULL, "deconfig");
  142. }
  143. LOG(LOG_INFO, "Entering released state");
  144. change_mode(LISTEN_NONE);
  145. state = RELEASED;
  146. timeout = 0x7fffffff;
  147. }
  148. static void client_background(void)
  149. {
  150. background(client_config.pidfile);
  151. client_config.foreground = 1; /* Do not fork again. */
  152. client_config.background_if_no_lease = 0;
  153. }
  154. #ifdef COMBINED_BINARY
  155. int udhcpc_main(int argc, char *argv[])
  156. #else
  157. int main(int argc, char *argv[])
  158. #endif
  159. {
  160. uint8_t *temp, *message;
  161. unsigned long t1 = 0, t2 = 0, xid = 0;
  162. unsigned long start = 0, lease;
  163. fd_set rfds;
  164. int retval;
  165. struct timeval tv;
  166. int c, len;
  167. struct dhcpMessage packet;
  168. struct in_addr temp_addr;
  169. long now;
  170. int max_fd;
  171. int sig;
  172. int no_clientid = 0;
  173. static const struct option arg_options[] = {
  174. {"clientid", required_argument, 0, 'c'},
  175. {"clientid-none", no_argument, 0, 'C'},
  176. {"vendorclass", required_argument, 0, 'V'},
  177. {"foreground", no_argument, 0, 'f'},
  178. {"background", no_argument, 0, 'b'},
  179. {"hostname", required_argument, 0, 'H'},
  180. {"hostname", required_argument, 0, 'h'},
  181. {"fqdn", required_argument, 0, 'F'},
  182. {"interface", required_argument, 0, 'i'},
  183. {"now", no_argument, 0, 'n'},
  184. {"pidfile", required_argument, 0, 'p'},
  185. {"quit", no_argument, 0, 'q'},
  186. {"request", required_argument, 0, 'r'},
  187. {"script", required_argument, 0, 's'},
  188. {"timeout", required_argument, 0, 'T'},
  189. {"version", no_argument, 0, 'v'},
  190. {"retries", required_argument, 0, 't'},
  191. {0, 0, 0, 0}
  192. };
  193. /* get options */
  194. while (1) {
  195. int option_index = 0;
  196. c = getopt_long(argc, argv, "c:CV:fbH:h:F:i:np:qr:s:T:t:v", arg_options, &option_index);
  197. if (c == -1) break;
  198. switch (c) {
  199. case 'c':
  200. if (no_clientid) show_usage();
  201. len = strlen(optarg) > 255 ? 255 : strlen(optarg);
  202. free(client_config.clientid);
  203. client_config.clientid = xmalloc(len + 2);
  204. client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
  205. client_config.clientid[OPT_LEN] = len;
  206. client_config.clientid[OPT_DATA] = '\0';
  207. strncpy((char*)client_config.clientid + OPT_DATA, optarg, len);
  208. break;
  209. case 'C':
  210. if (client_config.clientid) show_usage();
  211. no_clientid = 1;
  212. break;
  213. case 'V':
  214. len = strlen(optarg) > 255 ? 255 : strlen(optarg);
  215. free(client_config.vendorclass);
  216. client_config.vendorclass = xmalloc(len + 2);
  217. client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
  218. client_config.vendorclass[OPT_LEN] = len;
  219. strncpy((char*)client_config.vendorclass + OPT_DATA, optarg, len);
  220. break;
  221. case 'f':
  222. client_config.foreground = 1;
  223. break;
  224. case 'b':
  225. client_config.background_if_no_lease = 1;
  226. break;
  227. case 'h':
  228. case 'H':
  229. len = strlen(optarg) > 255 ? 255 : strlen(optarg);
  230. free(client_config.hostname);
  231. client_config.hostname = xmalloc(len + 2);
  232. client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
  233. client_config.hostname[OPT_LEN] = len;
  234. strncpy((char*)client_config.hostname + 2, optarg, len);
  235. break;
  236. case 'F':
  237. len = strlen(optarg) > 255 ? 255 : strlen(optarg);
  238. free(client_config.fqdn);
  239. client_config.fqdn = xmalloc(len + 5);
  240. client_config.fqdn[OPT_CODE] = DHCP_FQDN;
  241. client_config.fqdn[OPT_LEN] = len + 3;
  242. /* Flags: 0000NEOS
  243. S: 1 => Client requests Server to update A RR in DNS as well as PTR
  244. O: 1 => Server indicates to client that DNS has been updated regardless
  245. E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
  246. N: 1 => Client requests Server to not update DNS
  247. */
  248. client_config.fqdn[OPT_LEN + 1] = 0x1;
  249. client_config.fqdn[OPT_LEN + 2] = 0;
  250. client_config.fqdn[OPT_LEN + 3] = 0;
  251. strncpy((char*)client_config.fqdn + 5, optarg, len);
  252. break;
  253. case 'i':
  254. client_config.interface = optarg;
  255. break;
  256. case 'n':
  257. client_config.abort_if_no_lease = 1;
  258. break;
  259. case 'p':
  260. client_config.pidfile = optarg;
  261. break;
  262. case 'q':
  263. client_config.quit_after_lease = 1;
  264. break;
  265. case 'r':
  266. requested_ip = inet_addr(optarg);
  267. break;
  268. case 's':
  269. client_config.script = optarg;
  270. break;
  271. case 'T':
  272. client_config.timeout = atoi(optarg);
  273. break;
  274. case 't':
  275. client_config.retries = atoi(optarg);
  276. break;
  277. case 'v':
  278. printf("udhcpcd, version %s\n\n", VERSION);
  279. return 0;
  280. break;
  281. default:
  282. show_usage();
  283. }
  284. }
  285. /* Start the log, sanitize fd's, and write a pid file */
  286. start_log_and_pid("udhcpc", client_config.pidfile);
  287. if (read_interface(client_config.interface, &client_config.ifindex,
  288. NULL, client_config.arp) < 0)
  289. return 1;
  290. /* if not set, and not suppressed, setup the default client ID */
  291. if (!client_config.clientid && !no_clientid) {
  292. client_config.clientid = xmalloc(6 + 3);
  293. client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
  294. client_config.clientid[OPT_LEN] = 7;
  295. client_config.clientid[OPT_DATA] = 1;
  296. memcpy(client_config.clientid + 3, client_config.arp, 6);
  297. }
  298. if (!client_config.vendorclass) {
  299. client_config.vendorclass = xmalloc(sizeof("udhcp "VERSION) + 2);
  300. client_config.vendorclass[OPT_CODE] = DHCP_VENDOR;
  301. client_config.vendorclass[OPT_LEN] = sizeof("udhcp "VERSION) - 1;
  302. client_config.vendorclass[OPT_DATA] = 1;
  303. memcpy(&client_config.vendorclass[OPT_DATA],
  304. "udhcp "VERSION, sizeof("udhcp "VERSION) - 1);
  305. }
  306. /* setup the signal pipe */
  307. udhcp_sp_setup();
  308. state = INIT_SELECTING;
  309. run_script(NULL, "deconfig");
  310. change_mode(LISTEN_RAW);
  311. for (;;) {
  312. tv.tv_sec = timeout - uptime();
  313. tv.tv_usec = 0;
  314. if (listen_mode != LISTEN_NONE && fd < 0) {
  315. if (listen_mode == LISTEN_KERNEL)
  316. fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface);
  317. else
  318. fd = raw_socket(client_config.ifindex);
  319. if (fd < 0) {
  320. LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
  321. return 0;
  322. }
  323. }
  324. max_fd = udhcp_sp_fd_set(&rfds, fd);
  325. if (tv.tv_sec > 0) {
  326. DEBUG(LOG_INFO, "Waiting on select...");
  327. retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
  328. } else retval = 0; /* If we already timed out, fall through */
  329. now = uptime();
  330. if (retval == 0) {
  331. /* timeout dropped to zero */
  332. switch (state) {
  333. case INIT_SELECTING:
  334. if (packet_num < client_config.retries) {
  335. if (packet_num == 0)
  336. xid = random_xid();
  337. /* send discover packet */
  338. send_discover(xid, requested_ip); /* broadcast */
  339. timeout = now + client_config.timeout;
  340. packet_num++;
  341. } else {
  342. run_script(NULL, "leasefail");
  343. if (client_config.background_if_no_lease) {
  344. LOG(LOG_INFO, "No lease, forking to background.");
  345. client_background();
  346. } else if (client_config.abort_if_no_lease) {
  347. LOG(LOG_INFO, "No lease, failing.");
  348. return 1;
  349. }
  350. /* wait to try again */
  351. packet_num = 0;
  352. timeout = now + 60;
  353. }
  354. break;
  355. case RENEW_REQUESTED:
  356. case REQUESTING:
  357. if (packet_num < client_config.retries) {
  358. /* send request packet */
  359. if (state == RENEW_REQUESTED)
  360. send_renew(xid, server_addr, requested_ip); /* unicast */
  361. else send_selecting(xid, server_addr, requested_ip); /* broadcast */
  362. timeout = now + ((packet_num == 2) ? 10 : 2);
  363. packet_num++;
  364. } else {
  365. /* timed out, go back to init state */
  366. if (state == RENEW_REQUESTED) run_script(NULL, "deconfig");
  367. state = INIT_SELECTING;
  368. timeout = now;
  369. packet_num = 0;
  370. change_mode(LISTEN_RAW);
  371. }
  372. break;
  373. case BOUND:
  374. /* Lease is starting to run out, time to enter renewing state */
  375. state = RENEWING;
  376. change_mode(LISTEN_KERNEL);
  377. DEBUG(LOG_INFO, "Entering renew state");
  378. /* fall right through */
  379. case RENEWING:
  380. /* Either set a new T1, or enter REBINDING state */
  381. if ((t2 - t1) <= (lease / 14400 + 1)) {
  382. /* timed out, enter rebinding state */
  383. state = REBINDING;
  384. timeout = now + (t2 - t1);
  385. DEBUG(LOG_INFO, "Entering rebinding state");
  386. } else {
  387. /* send a request packet */
  388. send_renew(xid, server_addr, requested_ip); /* unicast */
  389. t1 = (t2 - t1) / 2 + t1;
  390. timeout = t1 + start;
  391. }
  392. break;
  393. case REBINDING:
  394. /* Either set a new T2, or enter INIT state */
  395. if ((lease - t2) <= (lease / 14400 + 1)) {
  396. /* timed out, enter init state */
  397. state = INIT_SELECTING;
  398. LOG(LOG_INFO, "Lease lost, entering init state");
  399. run_script(NULL, "deconfig");
  400. timeout = now;
  401. packet_num = 0;
  402. change_mode(LISTEN_RAW);
  403. } else {
  404. /* send a request packet */
  405. send_renew(xid, 0, requested_ip); /* broadcast */
  406. t2 = (lease - t2) / 2 + t2;
  407. timeout = t2 + start;
  408. }
  409. break;
  410. case RELEASED:
  411. /* yah, I know, *you* say it would never happen */
  412. timeout = 0x7fffffff;
  413. break;
  414. }
  415. } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) {
  416. /* a packet is ready, read it */
  417. if (listen_mode == LISTEN_KERNEL)
  418. len = get_packet(&packet, fd);
  419. else len = get_raw_packet(&packet, fd);
  420. if (len == -1 && errno != EINTR) {
  421. DEBUG(LOG_INFO, "error on read, %m, reopening socket");
  422. change_mode(listen_mode); /* just close and reopen */
  423. }
  424. if (len < 0) continue;
  425. if (packet.xid != xid) {
  426. DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)",
  427. (unsigned long) packet.xid, xid);
  428. continue;
  429. }
  430. /* Ignore packets that aren't for us */
  431. if (memcmp(packet.chaddr, client_config.arp, 6)) {
  432. DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring");
  433. continue;
  434. }
  435. if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
  436. DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring");
  437. continue;
  438. }
  439. switch (state) {
  440. case INIT_SELECTING:
  441. /* Must be a DHCPOFFER to one of our xid's */
  442. if (*message == DHCPOFFER) {
  443. if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
  444. memcpy(&server_addr, temp, 4);
  445. xid = packet.xid;
  446. requested_ip = packet.yiaddr;
  447. /* enter requesting state */
  448. state = REQUESTING;
  449. timeout = now;
  450. packet_num = 0;
  451. } else {
  452. DEBUG(LOG_ERR, "No server ID in message");
  453. }
  454. }
  455. break;
  456. case RENEW_REQUESTED:
  457. case REQUESTING:
  458. case RENEWING:
  459. case REBINDING:
  460. if (*message == DHCPACK) {
  461. if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
  462. LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease");
  463. lease = 60 * 60;
  464. } else {
  465. memcpy(&lease, temp, 4);
  466. lease = ntohl(lease);
  467. }
  468. /* enter bound state */
  469. t1 = lease / 2;
  470. /* little fixed point for n * .875 */
  471. t2 = (lease * 0x7) >> 3;
  472. temp_addr.s_addr = packet.yiaddr;
  473. LOG(LOG_INFO, "Lease of %s obtained, lease time %ld",
  474. inet_ntoa(temp_addr), lease);
  475. start = now;
  476. timeout = t1 + start;
  477. requested_ip = packet.yiaddr;
  478. run_script(&packet,
  479. ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
  480. state = BOUND;
  481. change_mode(LISTEN_NONE);
  482. if (client_config.quit_after_lease)
  483. return 0;
  484. if (!client_config.foreground)
  485. client_background();
  486. } else if (*message == DHCPNAK) {
  487. /* return to init state */
  488. LOG(LOG_INFO, "Received DHCP NAK");
  489. run_script(&packet, "nak");
  490. if (state != REQUESTING)
  491. run_script(NULL, "deconfig");
  492. state = INIT_SELECTING;
  493. timeout = now;
  494. requested_ip = 0;
  495. packet_num = 0;
  496. change_mode(LISTEN_RAW);
  497. sleep(3); /* avoid excessive network traffic */
  498. }
  499. break;
  500. /* case BOUND, RELEASED: - ignore all packets */
  501. }
  502. } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) {
  503. switch (sig) {
  504. case SIGUSR1:
  505. perform_renew();
  506. break;
  507. case SIGUSR2:
  508. perform_release();
  509. break;
  510. case SIGTERM:
  511. LOG(LOG_INFO, "Received SIGTERM");
  512. return 0;
  513. }
  514. } else if (retval == -1 && errno == EINTR) {
  515. /* a signal was caught */
  516. } else {
  517. /* An error occured */
  518. DEBUG(LOG_ERR, "Error on select");
  519. }
  520. }
  521. return 0;
  522. }