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