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