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