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