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