"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 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 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 11 | #include <getopt.h> |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 12 | #include <syslog.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 14 | /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */ |
| 15 | #define WANT_PIDFILE 1 |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 16 | #include "common.h" |
| 17 | #include "dhcpd.h" |
| 18 | #include "dhcpc.h" |
| 19 | #include "options.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 20 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | 239369b | 2006-09-07 17:05:44 +0000 | [diff] [blame] | 22 | /* Something is definitely wrong here. IPv4 addresses |
| 23 | * in variables of type long?? BTW, we use inet_ntoa() |
| 24 | * in the code. Manpage says that struct in_addr has a member of type long (!) |
| 25 | * which holds IPv4 address, and the struct is passed by value (!!) |
| 26 | */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 27 | static int timeout; /* = 0. Must be signed */ |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 28 | static uint32_t requested_ip; /* = 0 */ |
Denis Vlasenko | 74c9d23 | 2007-01-18 15:42:00 +0000 | [diff] [blame] | 29 | static uint32_t server_addr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 30 | static int packet_num; /* = 0 */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 31 | static int sockfd = -1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 32 | |
| 33 | #define LISTEN_NONE 0 |
| 34 | #define LISTEN_KERNEL 1 |
| 35 | #define LISTEN_RAW 2 |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 36 | static smallint listen_mode; |
| 37 | |
| 38 | static smallint state; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | deabacd | 2007-09-30 17:55:43 +0000 | [diff] [blame] | 40 | /* struct client_config_t client_config is in bb_common_bufsiz1 */ |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 41 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 42 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 43 | /* just a little helper */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 44 | static void change_listen_mode(int new_mode) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 45 | { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 46 | DEBUG("entering %s listen mode", |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 47 | new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 48 | if (sockfd >= 0) { |
| 49 | close(sockfd); |
| 50 | sockfd = -1; |
| 51 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 52 | listen_mode = new_mode; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* perform a renew */ |
| 57 | static void perform_renew(void) |
| 58 | { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 59 | bb_info_msg("Performing a DHCP renew"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 60 | switch (state) { |
| 61 | case BOUND: |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 62 | change_listen_mode(LISTEN_KERNEL); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 63 | case RENEWING: |
| 64 | case REBINDING: |
| 65 | state = RENEW_REQUESTED; |
| 66 | break; |
| 67 | case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 68 | udhcp_run_script(NULL, "deconfig"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 69 | case REQUESTING: |
| 70 | case RELEASED: |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 71 | change_listen_mode(LISTEN_RAW); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 72 | state = INIT_SELECTING; |
| 73 | break; |
| 74 | case INIT_SELECTING: |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | /* start things over */ |
| 79 | packet_num = 0; |
| 80 | |
| 81 | /* Kill any timeouts because the user wants this to hurry along */ |
| 82 | timeout = 0; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* perform a release */ |
| 87 | static void perform_release(void) |
| 88 | { |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 89 | char buffer[sizeof("255.255.255.255")]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 90 | struct in_addr temp_addr; |
| 91 | |
| 92 | /* send release packet */ |
| 93 | if (state == BOUND || state == RENEWING || state == REBINDING) { |
| 94 | temp_addr.s_addr = server_addr; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 95 | strcpy(buffer, inet_ntoa(temp_addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 96 | temp_addr.s_addr = requested_ip; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 97 | bb_info_msg("Unicasting a release of %s to %s", |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 98 | inet_ntoa(temp_addr), buffer); |
| 99 | send_release(server_addr, requested_ip); /* unicast */ |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 100 | udhcp_run_script(NULL, "deconfig"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 101 | } |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 102 | bb_info_msg("Entering released state"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 104 | change_listen_mode(LISTEN_NONE); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 105 | state = RELEASED; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 106 | timeout = INT_MAX; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | static void client_background(void) |
| 111 | { |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 112 | #if !BB_MMU |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 113 | bb_error_msg("cannot background in uclinux (yet)"); |
| 114 | /* ... mainly because udhcpc calls client_background() |
| 115 | * in _the _middle _of _udhcpc _run_, not at the start! |
| 116 | * If that will be properly disabled for NOMMU, client_background() |
| 117 | * will work on NOMMU too */ |
| 118 | #else |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 119 | bb_daemonize(0); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 120 | logmode &= ~LOGMODE_STDIO; |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 121 | /* rewrite pidfile, as our pid is different now */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 122 | write_pidfile(client_config.pidfile); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 123 | #endif |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 124 | /* Do not fork again. */ |
| 125 | client_config.foreground = 1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 126 | client_config.background_if_no_lease = 0; |
| 127 | } |
| 128 | |
| 129 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 130 | static uint8_t* alloc_dhcp_option(int code, const char *str, int extra) |
| 131 | { |
| 132 | uint8_t *storage; |
| 133 | int len = strlen(str); |
| 134 | if (len > 255) len = 255; |
| 135 | storage = xzalloc(len + extra + OPT_DATA); |
| 136 | storage[OPT_CODE] = code; |
| 137 | storage[OPT_LEN] = len + extra; |
| 138 | memcpy(storage + extra + OPT_DATA, str, len); |
| 139 | return storage; |
| 140 | } |
| 141 | |
| 142 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 143 | int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 144 | int udhcpc_main(int argc, char **argv) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 145 | { |
| 146 | uint8_t *temp, *message; |
Paul Fox | 49cce2b | 2007-11-07 16:01:28 +0000 | [diff] [blame] | 147 | char *str_c, *str_V, *str_h, *str_F, *str_r, *str_T, *str_A, *str_t; |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 148 | int tryagain_timeout = 60; |
| 149 | int discover_timeout = 3; |
| 150 | int discover_retries = 3; |
| 151 | #if ENABLE_FEATURE_UDHCPC_ARPING |
| 152 | int decline_wait = 10; |
| 153 | char *str_W; |
| 154 | #endif |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 155 | uint32_t xid = 0; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 156 | uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 157 | unsigned t1 = 0, t2 = 0; /* what a wonderful names */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 158 | unsigned timestamp_got_lease = 0; /* for gcc */ |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 159 | unsigned opt; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 160 | int max_fd; |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 161 | int retval; |
| 162 | int len; |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 163 | struct timeval tv; |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 164 | struct in_addr temp_addr; |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 165 | struct dhcpMessage packet; |
| 166 | fd_set rfds; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 168 | enum { |
| 169 | OPT_c = 1 << 0, |
| 170 | OPT_C = 1 << 1, |
| 171 | OPT_V = 1 << 2, |
| 172 | OPT_f = 1 << 3, |
| 173 | OPT_b = 1 << 4, |
| 174 | OPT_H = 1 << 5, |
| 175 | OPT_h = 1 << 6, |
| 176 | OPT_F = 1 << 7, |
| 177 | OPT_i = 1 << 8, |
| 178 | OPT_n = 1 << 9, |
| 179 | OPT_p = 1 << 10, |
| 180 | OPT_q = 1 << 11, |
| 181 | OPT_R = 1 << 12, |
| 182 | OPT_r = 1 << 13, |
| 183 | OPT_s = 1 << 14, |
| 184 | OPT_T = 1 << 15, |
| 185 | OPT_t = 1 << 16, |
| 186 | OPT_v = 1 << 17, |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 187 | OPT_S = 1 << 18, |
Paul Fox | 49cce2b | 2007-11-07 16:01:28 +0000 | [diff] [blame] | 188 | OPT_A = 1 << 19, |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 189 | #if ENABLE_FEATURE_UDHCPC_ARPING |
| 190 | OPT_a = 1 << 20, |
| 191 | OPT_W = 1 << 21, |
| 192 | #endif |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 193 | }; |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 194 | #if ENABLE_GETOPT_LONG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 195 | static const char udhcpc_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 196 | "clientid\0" Required_argument "c" |
| 197 | "clientid-none\0" No_argument "C" |
| 198 | "vendorclass\0" Required_argument "V" |
| 199 | "foreground\0" No_argument "f" |
| 200 | "background\0" No_argument "b" |
| 201 | "hostname\0" Required_argument "H" |
| 202 | "hostname\0" Required_argument "h" |
| 203 | "fqdn\0" Required_argument "F" |
| 204 | "interface\0" Required_argument "i" |
| 205 | "now\0" No_argument "n" |
| 206 | "pidfile\0" Required_argument "p" |
| 207 | "quit\0" No_argument "q" |
| 208 | "release\0" No_argument "R" |
| 209 | "request\0" Required_argument "r" |
| 210 | "script\0" Required_argument "s" |
| 211 | "timeout\0" Required_argument "T" |
| 212 | "version\0" No_argument "v" |
| 213 | "retries\0" Required_argument "t" |
Paul Fox | 49cce2b | 2007-11-07 16:01:28 +0000 | [diff] [blame] | 214 | "tryagain\0" Required_argument "A" |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 215 | "syslog\0" No_argument "S" |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 216 | #if ENABLE_FEATURE_UDHCPC_ARPING |
| 217 | "arping\0" No_argument "a" |
| 218 | "wait\0" Required_argument "W" |
| 219 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 220 | ; |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 221 | #endif |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 222 | /* Default options. */ |
| 223 | client_config.interface = "eth0"; |
| 224 | client_config.script = DEFAULT_SCRIPT; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 225 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 226 | /* Parse command line */ |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 227 | opt_complementary = "c--C:C--c" // mutually exclusive |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 228 | ":hH:Hh"; // -h and -H are the same |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 229 | #if ENABLE_GETOPT_LONG |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 230 | applet_long_options = udhcpc_longopts; |
Denis Vlasenko | c61852a | 2006-11-29 11:09:43 +0000 | [diff] [blame] | 231 | #endif |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 232 | opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:" |
| 233 | USE_FEATURE_UDHCPC_ARPING("aW:") |
| 234 | , &str_c, &str_V, &str_h, &str_h, &str_F, |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 235 | &client_config.interface, &client_config.pidfile, &str_r, |
Paul Fox | 49cce2b | 2007-11-07 16:01:28 +0000 | [diff] [blame] | 236 | &client_config.script, &str_T, &str_t, &str_A |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 237 | USE_FEATURE_UDHCPC_ARPING(, &str_W) |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 238 | ); |
| 239 | |
| 240 | if (opt & OPT_c) |
| 241 | client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0); |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 242 | //if (opt & OPT_C) |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 243 | if (opt & OPT_V) |
| 244 | client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0); |
| 245 | if (opt & OPT_f) |
| 246 | client_config.foreground = 1; |
| 247 | if (opt & OPT_b) |
| 248 | client_config.background_if_no_lease = 1; |
| 249 | if (opt & OPT_h) |
| 250 | client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0); |
| 251 | if (opt & OPT_F) { |
| 252 | client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3); |
| 253 | /* Flags: 0000NEOS |
| 254 | S: 1 => Client requests Server to update A RR in DNS as well as PTR |
| 255 | O: 1 => Server indicates to client that DNS has been updated regardless |
| 256 | E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com" |
| 257 | N: 1 => Client requests Server to not update DNS |
| 258 | */ |
| 259 | client_config.fqdn[OPT_DATA + 0] = 0x1; |
| 260 | /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */ |
| 261 | /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */ |
| 262 | } |
| 263 | // if (opt & OPT_i) client_config.interface = ... |
| 264 | if (opt & OPT_n) |
| 265 | client_config.abort_if_no_lease = 1; |
| 266 | // if (opt & OPT_p) client_config.pidfile = ... |
| 267 | if (opt & OPT_q) |
| 268 | client_config.quit_after_lease = 1; |
| 269 | if (opt & OPT_R) |
| 270 | client_config.release_on_quit = 1; |
| 271 | if (opt & OPT_r) |
| 272 | requested_ip = inet_addr(str_r); |
| 273 | // if (opt & OPT_s) client_config.script = ... |
| 274 | if (opt & OPT_T) |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 275 | discover_timeout = xatoi_u(str_T); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 276 | if (opt & OPT_t) |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 277 | discover_retries = xatoi_u(str_t); |
Paul Fox | 49cce2b | 2007-11-07 16:01:28 +0000 | [diff] [blame] | 278 | if (opt & OPT_A) |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 279 | tryagain_timeout = xatoi_u(str_A); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 280 | if (opt & OPT_v) { |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 281 | puts("version "BB_VER); |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 282 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Denis Vlasenko | 3d17d2b | 2007-08-14 16:45:29 +0000 | [diff] [blame] | 285 | if (opt & OPT_S) { |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 286 | openlog(applet_name, LOG_PID, LOG_LOCAL0); |
| 287 | logmode |= LOGMODE_SYSLOG; |
| 288 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 289 | |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 290 | #if ENABLE_FEATURE_UDHCPC_ARPING |
| 291 | if (opt & OPT_W) |
| 292 | decline_wait = xatou_range(str_W, 10, INT_MAX); |
| 293 | #endif |
| 294 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 295 | if (read_interface(client_config.interface, &client_config.ifindex, |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 296 | NULL, client_config.arp)) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 297 | return 1; |
| 298 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 299 | /* Make sure fd 0,1,2 are open */ |
| 300 | bb_sanitize_stdio(); |
| 301 | /* Equivalent of doing a fflush after every \n */ |
| 302 | setlinebuf(stdout); |
| 303 | |
| 304 | /* Create pidfile */ |
| 305 | write_pidfile(client_config.pidfile); |
| 306 | /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */ |
| 307 | |
| 308 | /* Goes to stdout and possibly syslog */ |
Denis Vlasenko | def8898 | 2007-10-07 17:06:01 +0000 | [diff] [blame] | 309 | bb_info_msg("%s (v"BB_VER") started", applet_name); |
Denis Vlasenko | af1c843 | 2007-03-26 13:22:35 +0000 | [diff] [blame] | 310 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 311 | /* if not set, and not suppressed, setup the default client ID */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 312 | if (!client_config.clientid && !(opt & OPT_C)) { |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 313 | client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 314 | client_config.clientid[OPT_DATA] = 1; |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 315 | memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Denis Vlasenko | e2d3ded | 2006-11-27 23:43:28 +0000 | [diff] [blame] | 318 | if (!client_config.vendorclass) |
| 319 | client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 320 | |
| 321 | /* setup the signal pipe */ |
| 322 | udhcp_sp_setup(); |
| 323 | |
| 324 | state = INIT_SELECTING; |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 325 | udhcp_run_script(NULL, "deconfig"); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 326 | change_listen_mode(LISTEN_RAW); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 327 | |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 328 | /* Main event loop. select() waits on signal pipe and possibly |
| 329 | * on sockfd. |
| 330 | * "continue" statements in code below jump to the top of the loop. |
| 331 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 332 | for (;;) { |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 333 | tv.tv_sec = timeout; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 334 | tv.tv_usec = 0; |
| 335 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 336 | if (listen_mode != LISTEN_NONE && sockfd < 0) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 337 | if (listen_mode == LISTEN_KERNEL) |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 338 | sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 339 | else |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 340 | sockfd = raw_socket(client_config.ifindex); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 341 | } |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 342 | max_fd = udhcp_sp_fd_set(&rfds, sockfd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 344 | retval = 0; /* If we already timed out, fall through, else... */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 345 | if (tv.tv_sec > 0) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 346 | DEBUG("Waiting on select..."); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 347 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 348 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 349 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 350 | if (retval < 0) { |
| 351 | /* EINTR? signal was caught, don't panic */ |
| 352 | if (errno != EINTR) { |
| 353 | /* Else: an error occured, panic! */ |
| 354 | bb_perror_msg_and_die("select"); |
| 355 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 356 | continue; |
| 357 | } |
| 358 | |
| 359 | /* If timeout dropped to zero, time to become active: |
| 360 | * resend discover/renew/whatever |
| 361 | */ |
| 362 | if (retval == 0) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 363 | switch (state) { |
| 364 | case INIT_SELECTING: |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 365 | if (packet_num < discover_retries) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 366 | if (packet_num == 0) |
| 367 | xid = random_xid(); |
| 368 | |
| 369 | /* send discover packet */ |
| 370 | send_discover(xid, requested_ip); /* broadcast */ |
| 371 | |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 372 | timeout = discover_timeout; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 373 | packet_num++; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 374 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 375 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 376 | udhcp_run_script(NULL, "leasefail"); |
| 377 | if (client_config.background_if_no_lease) { |
| 378 | bb_info_msg("No lease, forking to background"); |
| 379 | client_background(); |
| 380 | } else if (client_config.abort_if_no_lease) { |
| 381 | bb_info_msg("No lease, failing"); |
| 382 | retval = 1; |
| 383 | goto ret; |
| 384 | } |
| 385 | /* wait to try again */ |
| 386 | timeout = tryagain_timeout; |
| 387 | packet_num = 0; |
| 388 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 389 | case RENEW_REQUESTED: |
| 390 | case REQUESTING: |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 391 | if (packet_num < discover_retries) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 392 | /* send request packet */ |
| 393 | if (state == RENEW_REQUESTED) |
| 394 | send_renew(xid, server_addr, requested_ip); /* unicast */ |
| 395 | else send_selecting(xid, server_addr, requested_ip); /* broadcast */ |
| 396 | |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 397 | timeout = ((packet_num == 2) ? 10 : 2); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 398 | packet_num++; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 399 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 400 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 401 | /* timed out, go back to init state */ |
| 402 | if (state == RENEW_REQUESTED) |
| 403 | udhcp_run_script(NULL, "deconfig"); |
| 404 | change_listen_mode(LISTEN_RAW); |
| 405 | state = INIT_SELECTING; |
| 406 | timeout = 0; |
| 407 | packet_num = 0; |
| 408 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 409 | case BOUND: |
| 410 | /* Lease is starting to run out, time to enter renewing state */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 411 | change_listen_mode(LISTEN_KERNEL); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 412 | DEBUG("Entering renew state"); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 413 | state = RENEWING; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 414 | /* fall right through */ |
| 415 | case RENEWING: |
| 416 | /* Either set a new T1, or enter REBINDING state */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 417 | if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 418 | /* send a request packet */ |
| 419 | send_renew(xid, server_addr, requested_ip); /* unicast */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 420 | t1 += (t2 - t1) / 2; |
| 421 | timeout = t1 - ((int)monotonic_sec() - timestamp_got_lease); |
| 422 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 423 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 424 | /* Timed out, enter rebinding state */ |
| 425 | DEBUG("Entering rebinding state"); |
| 426 | state = REBINDING; |
| 427 | timeout = (t2 - t1); |
| 428 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 429 | case REBINDING: |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 430 | /* Lease is *really* about to run out, |
| 431 | * try to find DHCP server using broadcast */ |
| 432 | if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 433 | /* send a request packet */ |
| 434 | send_renew(xid, 0, requested_ip); /* broadcast */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 435 | t2 += (lease_seconds - t2) / 2; |
| 436 | timeout = t2 - ((int)monotonic_sec() - timestamp_got_lease); |
| 437 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 438 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 439 | /* Timed out, enter init state */ |
| 440 | bb_info_msg("Lease lost, entering init state"); |
| 441 | udhcp_run_script(NULL, "deconfig"); |
| 442 | change_listen_mode(LISTEN_RAW); |
| 443 | state = INIT_SELECTING; |
| 444 | timeout = 0; |
| 445 | packet_num = 0; |
| 446 | continue; |
| 447 | /* case RELEASED: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 448 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 449 | /* yah, I know, *you* say it would never happen */ |
| 450 | timeout = INT_MAX; |
| 451 | continue; /* back to main loop */ |
| 452 | } |
| 453 | |
| 454 | /* select() didn't timeout, something did happen. */ |
| 455 | /* Is is a packet? */ |
| 456 | if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) { |
| 457 | /* A packet is ready, read it */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 458 | |
| 459 | if (listen_mode == LISTEN_KERNEL) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 460 | len = udhcp_get_packet(&packet, sockfd); |
| 461 | else len = get_raw_packet(&packet, sockfd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 462 | |
| 463 | if (len == -1 && errno != EINTR) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 464 | DEBUG("error on read, %s, reopening socket", strerror(errno)); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 465 | change_listen_mode(listen_mode); /* just close and reopen */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 466 | } |
| 467 | if (len < 0) continue; |
| 468 | |
| 469 | if (packet.xid != xid) { |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 470 | DEBUG("Ignoring XID %x (our xid is %x)", |
| 471 | (unsigned)packet.xid, (unsigned)xid); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 472 | continue; |
| 473 | } |
| 474 | |
| 475 | /* Ignore packets that aren't for us */ |
| 476 | if (memcmp(packet.chaddr, client_config.arp, 6)) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 477 | DEBUG("Packet does not have our chaddr - ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 478 | continue; |
| 479 | } |
| 480 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 481 | message = get_option(&packet, DHCP_MESSAGE_TYPE); |
| 482 | if (message == NULL) { |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 483 | bb_error_msg("cannot get message type from packet - ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 484 | continue; |
| 485 | } |
| 486 | |
| 487 | switch (state) { |
| 488 | case INIT_SELECTING: |
| 489 | /* Must be a DHCPOFFER to one of our xid's */ |
| 490 | if (*message == DHCPOFFER) { |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 491 | /* TODO: why we don't just fetch server's IP from IP header? */ |
Denis Vlasenko | 239369b | 2006-09-07 17:05:44 +0000 | [diff] [blame] | 492 | temp = get_option(&packet, DHCP_SERVER_ID); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 493 | if (!temp) { |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 494 | bb_error_msg("no server ID in message"); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 495 | continue; |
| 496 | /* still selecting - this server looks bad */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 497 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 498 | /* can be misaligned, thus memcpy */ |
| 499 | memcpy(&server_addr, temp, 4); |
| 500 | xid = packet.xid; |
| 501 | requested_ip = packet.yiaddr; |
| 502 | |
| 503 | /* enter requesting state */ |
| 504 | state = REQUESTING; |
| 505 | timeout = 0; |
| 506 | packet_num = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 507 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 508 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 509 | case RENEW_REQUESTED: |
| 510 | case REQUESTING: |
| 511 | case RENEWING: |
| 512 | case REBINDING: |
| 513 | if (*message == DHCPACK) { |
Denis Vlasenko | 239369b | 2006-09-07 17:05:44 +0000 | [diff] [blame] | 514 | temp = get_option(&packet, DHCP_LEASE_TIME); |
| 515 | if (!temp) { |
Denis Vlasenko | a6dbb08 | 2006-10-12 19:29:44 +0000 | [diff] [blame] | 516 | bb_error_msg("no lease time with ACK, using 1 hour lease"); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 517 | lease_seconds = 60 * 60; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 518 | } else { |
Denis Vlasenko | 74c9d23 | 2007-01-18 15:42:00 +0000 | [diff] [blame] | 519 | /* can be misaligned, thus memcpy */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 520 | memcpy(&lease_seconds, temp, 4); |
| 521 | lease_seconds = ntohl(lease_seconds); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 522 | } |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 523 | #if ENABLE_FEATURE_UDHCPC_ARPING |
| 524 | if (opt & OPT_a) { |
| 525 | if (!arpping(packet.yiaddr, |
| 526 | (uint32_t) 0, |
| 527 | client_config.arp, |
| 528 | client_config.interface) |
| 529 | ) { |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 530 | bb_info_msg("offered address is in use " |
| 531 | "(got ARP reply), declining"); |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 532 | send_decline(xid, server_addr); |
| 533 | |
| 534 | if (state != REQUESTING) |
| 535 | udhcp_run_script(NULL, "deconfig"); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 536 | change_listen_mode(LISTEN_RAW); |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 537 | state = INIT_SELECTING; |
| 538 | requested_ip = 0; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 539 | timeout = decline_wait; |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 540 | packet_num = 0; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 541 | continue; /* back to main loop */ |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 545 | /* enter bound state */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 546 | t1 = lease_seconds / 2; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 547 | |
| 548 | /* little fixed point for n * .875 */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 549 | t2 = (lease_seconds * 7) >> 3; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 550 | temp_addr.s_addr = packet.yiaddr; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 551 | bb_info_msg("Lease of %s obtained, lease time %u", |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 552 | inet_ntoa(temp_addr), (unsigned)lease_seconds); |
| 553 | timestamp_got_lease = monotonic_sec(); |
| 554 | timeout = t1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 555 | requested_ip = packet.yiaddr; |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 556 | udhcp_run_script(&packet, |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 557 | ((state == RENEWING || state == REBINDING) ? "renew" : "bound")); |
| 558 | |
| 559 | state = BOUND; |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 560 | change_listen_mode(LISTEN_NONE); |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 561 | if (client_config.quit_after_lease) { |
| 562 | if (client_config.release_on_quit) |
| 563 | perform_release(); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 564 | goto ret0; |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 565 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 566 | if (!client_config.foreground) |
| 567 | client_background(); |
| 568 | |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 569 | continue; /* back to main loop */ |
| 570 | } |
| 571 | if (*message == DHCPNAK) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 572 | /* return to init state */ |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 573 | bb_info_msg("Received DHCP NAK"); |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 574 | udhcp_run_script(&packet, "nak"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 575 | if (state != REQUESTING) |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 576 | udhcp_run_script(NULL, "deconfig"); |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 577 | change_listen_mode(LISTEN_RAW); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 578 | sleep(3); /* avoid excessive network traffic */ |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 579 | state = INIT_SELECTING; |
| 580 | requested_ip = 0; |
| 581 | timeout = 0; |
| 582 | packet_num = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 583 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 584 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 585 | /* case BOUND, RELEASED: - ignore all packets */ |
| 586 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 587 | continue; /* back to main loop */ |
| 588 | } |
| 589 | |
| 590 | /* select() didn't timeout, something did happen. |
| 591 | * But it wasn't a packet. It's a signal pipe then. */ |
| 592 | { |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 593 | int signo = udhcp_sp_read(&rfds); |
| 594 | switch (signo) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 595 | case SIGUSR1: |
| 596 | perform_renew(); |
| 597 | break; |
| 598 | case SIGUSR2: |
| 599 | perform_release(); |
| 600 | break; |
| 601 | case SIGTERM: |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 602 | bb_info_msg("Received SIGTERM"); |
Denis Vlasenko | e175ff2 | 2006-09-26 17:41:00 +0000 | [diff] [blame] | 603 | if (client_config.release_on_quit) |
| 604 | perform_release(); |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 605 | goto ret0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 606 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 607 | } |
Denis Vlasenko | 9cdfd14 | 2007-11-22 01:00:00 +0000 | [diff] [blame^] | 608 | } /* for (;;) - main loop ends */ |
| 609 | |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 610 | ret0: |
| 611 | retval = 0; |
| 612 | ret: |
Denis Vlasenko | bb23c06 | 2007-08-15 20:05:37 +0000 | [diff] [blame] | 613 | /*if (client_config.pidfile) - remove_pidfile has it's own check */ |
Denis Vlasenko | 6e6d331 | 2007-05-03 23:39:35 +0000 | [diff] [blame] | 614 | remove_pidfile(client_config.pidfile); |
| 615 | return retval; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 616 | } |