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