dhcpc.c 14 KB

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