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