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