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