Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | /* dhcpc.c |
| 2 | * |
| 3 | * udhcp DHCP client |
| 4 | * |
| 5 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 21 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 22 | #include <sys/time.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 23 | #include <sys/file.h> |
| 24 | #include <unistd.h> |
| 25 | #include <getopt.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <netinet/in.h> |
| 29 | #include <arpa/inet.h> |
| 30 | #include <signal.h> |
| 31 | #include <time.h> |
| 32 | #include <string.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <net/if.h> |
| 35 | #include <errno.h> |
| 36 | |
| 37 | #include "dhcpd.h" |
| 38 | #include "dhcpc.h" |
| 39 | #include "options.h" |
| 40 | #include "clientpacket.h" |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 41 | #include "clientsocket.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 42 | #include "script.h" |
| 43 | #include "socket.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 44 | #include "common.h" |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 45 | #include "signalpipe.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 46 | |
| 47 | static int state; |
| 48 | static unsigned long requested_ip; /* = 0 */ |
| 49 | static unsigned long server_addr; |
| 50 | static unsigned long timeout; |
| 51 | static int packet_num; /* = 0 */ |
Russ Dill | 6393d69 | 2003-01-21 22:39:34 +0000 | [diff] [blame] | 52 | static int fd = -1; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 53 | |
| 54 | #define LISTEN_NONE 0 |
| 55 | #define LISTEN_KERNEL 1 |
| 56 | #define LISTEN_RAW 2 |
| 57 | static int listen_mode; |
| 58 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 59 | struct client_config_t client_config = { |
| 60 | /* Default options. */ |
Mike Frysinger | 3cc01a8 | 2005-04-16 04:17:39 +0000 | [diff] [blame] | 61 | .abort_if_no_lease = 0, |
| 62 | .foreground = 0, |
| 63 | .quit_after_lease = 0, |
| 64 | .background_if_no_lease = 0, |
| 65 | .interface = "eth0", |
| 66 | .pidfile = NULL, |
| 67 | .script = DEFAULT_SCRIPT, |
| 68 | .clientid = NULL, |
| 69 | .hostname = NULL, |
| 70 | .fqdn = NULL, |
| 71 | .ifindex = 0, |
| 72 | .arp = "\0\0\0\0\0\0", /* appease gcc-3.0 */ |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 75 | #ifndef IN_BUSYBOX |
| 76 | static void __attribute__ ((noreturn)) show_usage(void) |
| 77 | { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 78 | printf( |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 79 | "Usage: udhcpc [OPTIONS]\n\n" |
| 80 | " -c, --clientid=CLIENTID Client identifier\n" |
| 81 | " -H, --hostname=HOSTNAME Client hostname\n" |
| 82 | " -h Alias for -H\n" |
Mike Frysinger | d824853 | 2004-12-06 14:59:45 +0000 | [diff] [blame] | 83 | " -F, --fqdn=FQDN Client fully qualified domain name\n" |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 84 | " -f, --foreground Do not fork after getting lease\n" |
| 85 | " -b, --background Fork to background if lease cannot be\n" |
| 86 | " immediately negotiated.\n" |
| 87 | " -i, --interface=INTERFACE Interface to use (default: eth0)\n" |
| 88 | " -n, --now Exit with failure if lease cannot be\n" |
| 89 | " immediately negotiated.\n" |
| 90 | " -p, --pidfile=file Store process ID of daemon in file\n" |
| 91 | " -q, --quit Quit after obtaining lease\n" |
| 92 | " -r, --request=IP IP address to request (default: none)\n" |
| 93 | " -s, --script=file Run file at dhcp events (default:\n" |
| 94 | " " DEFAULT_SCRIPT ")\n" |
| 95 | " -v, --version Display version\n" |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 96 | ); |
| 97 | exit(0); |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 98 | } |
| 99 | #else |
| 100 | #define show_usage bb_show_usage |
| 101 | extern void show_usage(void) __attribute__ ((noreturn)); |
| 102 | #endif |
| 103 | |
| 104 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 105 | /* just a little helper */ |
| 106 | static void change_mode(int new_mode) |
| 107 | { |
| 108 | DEBUG(LOG_INFO, "entering %s listen mode", |
| 109 | new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); |
Russ Dill | 6393d69 | 2003-01-21 22:39:34 +0000 | [diff] [blame] | 110 | if (fd >= 0) close(fd); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 111 | fd = -1; |
| 112 | listen_mode = new_mode; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* perform a renew */ |
| 117 | static void perform_renew(void) |
| 118 | { |
| 119 | LOG(LOG_INFO, "Performing a DHCP renew"); |
| 120 | switch (state) { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 121 | case BOUND: |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 122 | change_mode(LISTEN_KERNEL); |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 123 | case RENEWING: |
| 124 | case REBINDING: |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 125 | state = RENEW_REQUESTED; |
| 126 | break; |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 127 | case RENEW_REQUESTED: /* impatient are we? fine, square 1 */ |
| 128 | run_script(NULL, "deconfig"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 129 | case REQUESTING: |
| 130 | case RELEASED: |
| 131 | change_mode(LISTEN_RAW); |
| 132 | state = INIT_SELECTING; |
| 133 | break; |
| 134 | case INIT_SELECTING: |
Eric Andersen | 1b2e7c3 | 2003-06-20 09:20:28 +0000 | [diff] [blame] | 135 | break; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* start things over */ |
| 139 | packet_num = 0; |
| 140 | |
| 141 | /* Kill any timeouts because the user wants this to hurry along */ |
| 142 | timeout = 0; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /* perform a release */ |
| 147 | static void perform_release(void) |
| 148 | { |
| 149 | char buffer[16]; |
| 150 | struct in_addr temp_addr; |
| 151 | |
| 152 | /* send release packet */ |
| 153 | if (state == BOUND || state == RENEWING || state == REBINDING) { |
| 154 | temp_addr.s_addr = server_addr; |
| 155 | sprintf(buffer, "%s", inet_ntoa(temp_addr)); |
| 156 | temp_addr.s_addr = requested_ip; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 157 | LOG(LOG_INFO, "Unicasting a release of %s to %s", |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 158 | inet_ntoa(temp_addr), buffer); |
| 159 | send_release(server_addr, requested_ip); /* unicast */ |
| 160 | run_script(NULL, "deconfig"); |
| 161 | } |
| 162 | LOG(LOG_INFO, "Entering released state"); |
| 163 | |
| 164 | change_mode(LISTEN_NONE); |
| 165 | state = RELEASED; |
| 166 | timeout = 0x7fffffff; |
| 167 | } |
| 168 | |
| 169 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 170 | static void client_background(void) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 171 | { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 172 | background(client_config.pidfile); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 173 | client_config.foreground = 1; /* Do not fork again. */ |
Russ Dill | 6393d69 | 2003-01-21 22:39:34 +0000 | [diff] [blame] | 174 | client_config.background_if_no_lease = 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 178 | #ifdef COMBINED_BINARY |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 179 | int udhcpc_main(int argc, char *argv[]) |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 180 | #else |
| 181 | int main(int argc, char *argv[]) |
| 182 | #endif |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 183 | { |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 184 | uint8_t *temp, *message; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 185 | unsigned long t1 = 0, t2 = 0, xid = 0; |
| 186 | unsigned long start = 0, lease; |
| 187 | fd_set rfds; |
| 188 | int retval; |
| 189 | struct timeval tv; |
| 190 | int c, len; |
| 191 | struct dhcpMessage packet; |
| 192 | struct in_addr temp_addr; |
Russ Dill | 309c7b7 | 2004-05-19 08:29:05 +0000 | [diff] [blame] | 193 | long now; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 194 | int max_fd; |
| 195 | int sig; |
| 196 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 197 | static const struct option arg_options[] = { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 198 | {"clientid", required_argument, 0, 'c'}, |
| 199 | {"foreground", no_argument, 0, 'f'}, |
| 200 | {"background", no_argument, 0, 'b'}, |
| 201 | {"hostname", required_argument, 0, 'H'}, |
Mike Frysinger | d824853 | 2004-12-06 14:59:45 +0000 | [diff] [blame] | 202 | {"hostname", required_argument, 0, 'h'}, |
| 203 | {"fqdn", required_argument, 0, 'F'}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 204 | {"interface", required_argument, 0, 'i'}, |
| 205 | {"now", no_argument, 0, 'n'}, |
| 206 | {"pidfile", required_argument, 0, 'p'}, |
| 207 | {"quit", no_argument, 0, 'q'}, |
| 208 | {"request", required_argument, 0, 'r'}, |
| 209 | {"script", required_argument, 0, 's'}, |
| 210 | {"version", no_argument, 0, 'v'}, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 211 | {0, 0, 0, 0} |
| 212 | }; |
| 213 | |
| 214 | /* get options */ |
| 215 | while (1) { |
| 216 | int option_index = 0; |
Mike Frysinger | d824853 | 2004-12-06 14:59:45 +0000 | [diff] [blame] | 217 | c = getopt_long(argc, argv, "c:fbH:h:F:i:np:qr:s:v", arg_options, &option_index); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 218 | if (c == -1) break; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 219 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 220 | switch (c) { |
| 221 | case 'c': |
| 222 | len = strlen(optarg) > 255 ? 255 : strlen(optarg); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame] | 223 | if (client_config.clientid) free(client_config.clientid); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 224 | client_config.clientid = xmalloc(len + 2); |
| 225 | client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID; |
| 226 | client_config.clientid[OPT_LEN] = len; |
| 227 | client_config.clientid[OPT_DATA] = '\0'; |
| 228 | strncpy(client_config.clientid + OPT_DATA, optarg, len); |
| 229 | break; |
| 230 | case 'f': |
| 231 | client_config.foreground = 1; |
| 232 | break; |
| 233 | case 'b': |
| 234 | client_config.background_if_no_lease = 1; |
| 235 | break; |
| 236 | case 'h': |
| 237 | case 'H': |
| 238 | len = strlen(optarg) > 255 ? 255 : strlen(optarg); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame] | 239 | if (client_config.hostname) free(client_config.hostname); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 240 | client_config.hostname = xmalloc(len + 2); |
| 241 | client_config.hostname[OPT_CODE] = DHCP_HOST_NAME; |
| 242 | client_config.hostname[OPT_LEN] = len; |
| 243 | strncpy(client_config.hostname + 2, optarg, len); |
| 244 | break; |
Mike Frysinger | d824853 | 2004-12-06 14:59:45 +0000 | [diff] [blame] | 245 | case 'F': |
| 246 | len = strlen(optarg) > 255 ? 255 : strlen(optarg); |
| 247 | if (client_config.fqdn) free(client_config.fqdn); |
| 248 | client_config.fqdn = xmalloc(len + 5); |
| 249 | client_config.fqdn[OPT_CODE] = DHCP_FQDN; |
| 250 | client_config.fqdn[OPT_LEN] = len + 3; |
| 251 | /* Flags: 0000NEOS |
| 252 | S: 1 => Client requests Server to update A RR in DNS as well as PTR |
| 253 | O: 1 => Server indicates to client that DNS has been updated regardless |
| 254 | E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com" |
| 255 | N: 1 => Client requests Server to not update DNS |
| 256 | */ |
| 257 | client_config.fqdn[OPT_LEN + 1] = 0x1; |
| 258 | client_config.fqdn[OPT_LEN + 2] = 0; |
| 259 | client_config.fqdn[OPT_LEN + 3] = 0; |
| 260 | strncpy(client_config.fqdn + 5, optarg, len); |
| 261 | break; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 262 | case 'i': |
| 263 | client_config.interface = optarg; |
| 264 | break; |
| 265 | case 'n': |
| 266 | client_config.abort_if_no_lease = 1; |
| 267 | break; |
| 268 | case 'p': |
| 269 | client_config.pidfile = optarg; |
| 270 | break; |
| 271 | case 'q': |
| 272 | client_config.quit_after_lease = 1; |
| 273 | break; |
| 274 | case 'r': |
| 275 | requested_ip = inet_addr(optarg); |
| 276 | break; |
| 277 | case 's': |
| 278 | client_config.script = optarg; |
| 279 | break; |
| 280 | case 'v': |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 281 | printf("udhcpcd, version %s\n\n", VERSION); |
| 282 | return 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 283 | break; |
| 284 | default: |
Russ Dill | 4b77aca | 2003-12-16 02:28:20 +0000 | [diff] [blame] | 285 | show_usage(); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 289 | /* Start the log, sanitize fd's, and write a pid file */ |
| 290 | start_log_and_pid("udhcpc", client_config.pidfile); |
| 291 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 292 | if (read_interface(client_config.interface, &client_config.ifindex, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 293 | NULL, client_config.arp) < 0) |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 294 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 295 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 296 | if (!client_config.clientid) { |
| 297 | client_config.clientid = xmalloc(6 + 3); |
| 298 | client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID; |
| 299 | client_config.clientid[OPT_LEN] = 7; |
| 300 | client_config.clientid[OPT_DATA] = 1; |
| 301 | memcpy(client_config.clientid + 3, client_config.arp, 6); |
| 302 | } |
| 303 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 304 | /* setup the signal pipe */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 305 | udhcp_sp_setup(); |
| 306 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 307 | state = INIT_SELECTING; |
| 308 | run_script(NULL, "deconfig"); |
| 309 | change_mode(LISTEN_RAW); |
| 310 | |
| 311 | for (;;) { |
| 312 | |
Rob Landley | 918f2ab | 2005-05-04 02:15:23 +0000 | [diff] [blame] | 313 | tv.tv_sec = timeout - uptime(); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 314 | tv.tv_usec = 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 315 | |
| 316 | if (listen_mode != LISTEN_NONE && fd < 0) { |
| 317 | if (listen_mode == LISTEN_KERNEL) |
| 318 | fd = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface); |
| 319 | else |
| 320 | fd = raw_socket(client_config.ifindex); |
| 321 | if (fd < 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 322 | LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m"); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 323 | return 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 326 | max_fd = udhcp_sp_fd_set(&rfds, fd); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 327 | |
| 328 | if (tv.tv_sec > 0) { |
Glenn L McGrath | 8ce8f9b | 2003-08-29 15:19:44 +0000 | [diff] [blame] | 329 | DEBUG(LOG_INFO, "Waiting on select..."); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 330 | retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); |
| 331 | } else retval = 0; /* If we already timed out, fall through */ |
| 332 | |
Rob Landley | 918f2ab | 2005-05-04 02:15:23 +0000 | [diff] [blame] | 333 | now = uptime(); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 334 | if (retval == 0) { |
| 335 | /* timeout dropped to zero */ |
| 336 | switch (state) { |
| 337 | case INIT_SELECTING: |
| 338 | if (packet_num < 3) { |
| 339 | if (packet_num == 0) |
| 340 | xid = random_xid(); |
| 341 | |
| 342 | /* send discover packet */ |
| 343 | send_discover(xid, requested_ip); /* broadcast */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 344 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 345 | timeout = now + ((packet_num == 2) ? 4 : 2); |
| 346 | packet_num++; |
| 347 | } else { |
Glenn L McGrath | d9461f8 | 2003-09-01 04:08:36 +0000 | [diff] [blame] | 348 | run_script(NULL, "leasefail"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 349 | if (client_config.background_if_no_lease) { |
| 350 | LOG(LOG_INFO, "No lease, forking to background."); |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 351 | client_background(); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 352 | } else if (client_config.abort_if_no_lease) { |
| 353 | LOG(LOG_INFO, "No lease, failing."); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 354 | return 1; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +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 < 3) { |
| 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 */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 368 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 369 | timeout = now + ((packet_num == 2) ? 10 : 2); |
| 370 | packet_num++; |
| 371 | } else { |
| 372 | /* timed out, go back to init state */ |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 373 | if (state == RENEW_REQUESTED) run_script(NULL, "deconfig"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 374 | state = INIT_SELECTING; |
| 375 | timeout = now; |
| 376 | packet_num = 0; |
| 377 | change_mode(LISTEN_RAW); |
| 378 | } |
| 379 | break; |
| 380 | case BOUND: |
| 381 | /* Lease is starting to run out, time to enter renewing state */ |
| 382 | state = RENEWING; |
| 383 | change_mode(LISTEN_KERNEL); |
| 384 | DEBUG(LOG_INFO, "Entering renew state"); |
| 385 | /* fall right through */ |
| 386 | case RENEWING: |
| 387 | /* Either set a new T1, or enter REBINDING state */ |
| 388 | if ((t2 - t1) <= (lease / 14400 + 1)) { |
| 389 | /* timed out, enter rebinding state */ |
| 390 | state = REBINDING; |
| 391 | timeout = now + (t2 - t1); |
| 392 | DEBUG(LOG_INFO, "Entering rebinding state"); |
| 393 | } else { |
| 394 | /* send a request packet */ |
| 395 | send_renew(xid, server_addr, requested_ip); /* unicast */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 396 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 397 | t1 = (t2 - t1) / 2 + t1; |
| 398 | timeout = t1 + start; |
| 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; |
| 406 | LOG(LOG_INFO, "Lease lost, entering init state"); |
| 407 | run_script(NULL, "deconfig"); |
| 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 */ |
| 414 | |
| 415 | t2 = (lease - t2) / 2 + t2; |
| 416 | timeout = t2 + start; |
| 417 | } |
| 418 | break; |
| 419 | case RELEASED: |
| 420 | /* yah, I know, *you* say it would never happen */ |
| 421 | timeout = 0x7fffffff; |
| 422 | break; |
| 423 | } |
| 424 | } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd, &rfds)) { |
| 425 | /* a packet is ready, read it */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 426 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 427 | if (listen_mode == LISTEN_KERNEL) |
| 428 | len = get_packet(&packet, fd); |
| 429 | else len = get_raw_packet(&packet, fd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 430 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 431 | if (len == -1 && errno != EINTR) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 432 | DEBUG(LOG_INFO, "error on read, %m, reopening socket"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 433 | change_mode(listen_mode); /* just close and reopen */ |
| 434 | } |
| 435 | if (len < 0) continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 436 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 437 | if (packet.xid != xid) { |
| 438 | DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)", |
| 439 | (unsigned long) packet.xid, xid); |
| 440 | continue; |
| 441 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 442 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 443 | if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) { |
| 444 | DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring"); |
| 445 | continue; |
| 446 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 447 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 448 | switch (state) { |
| 449 | case INIT_SELECTING: |
| 450 | /* Must be a DHCPOFFER to one of our xid's */ |
| 451 | if (*message == DHCPOFFER) { |
| 452 | if ((temp = get_option(&packet, DHCP_SERVER_ID))) { |
| 453 | memcpy(&server_addr, temp, 4); |
| 454 | xid = packet.xid; |
| 455 | requested_ip = packet.yiaddr; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 456 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 457 | /* enter requesting state */ |
| 458 | state = REQUESTING; |
| 459 | timeout = now; |
| 460 | packet_num = 0; |
| 461 | } else { |
| 462 | DEBUG(LOG_ERR, "No server ID in message"); |
| 463 | } |
| 464 | } |
| 465 | break; |
| 466 | case RENEW_REQUESTED: |
| 467 | case REQUESTING: |
| 468 | case RENEWING: |
| 469 | case REBINDING: |
| 470 | if (*message == DHCPACK) { |
| 471 | if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) { |
| 472 | LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease"); |
| 473 | lease = 60 * 60; |
| 474 | } else { |
| 475 | memcpy(&lease, temp, 4); |
| 476 | lease = ntohl(lease); |
| 477 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 478 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 479 | /* enter bound state */ |
| 480 | t1 = lease / 2; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 481 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 482 | /* little fixed point for n * .875 */ |
| 483 | t2 = (lease * 0x7) >> 3; |
| 484 | temp_addr.s_addr = packet.yiaddr; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 485 | LOG(LOG_INFO, "Lease of %s obtained, lease time %ld", |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 486 | inet_ntoa(temp_addr), lease); |
| 487 | start = now; |
| 488 | timeout = t1 + start; |
| 489 | requested_ip = packet.yiaddr; |
| 490 | run_script(&packet, |
| 491 | ((state == RENEWING || state == REBINDING) ? "renew" : "bound")); |
| 492 | |
| 493 | state = BOUND; |
| 494 | change_mode(LISTEN_NONE); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 495 | if (client_config.quit_after_lease) |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 496 | return 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 497 | if (!client_config.foreground) |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 498 | client_background(); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 499 | |
| 500 | } else if (*message == DHCPNAK) { |
| 501 | /* return to init state */ |
| 502 | LOG(LOG_INFO, "Received DHCP NAK"); |
| 503 | run_script(&packet, "nak"); |
| 504 | if (state != REQUESTING) |
| 505 | run_script(NULL, "deconfig"); |
| 506 | state = INIT_SELECTING; |
| 507 | timeout = now; |
| 508 | requested_ip = 0; |
| 509 | packet_num = 0; |
| 510 | change_mode(LISTEN_RAW); |
| 511 | sleep(3); /* avoid excessive network traffic */ |
| 512 | } |
| 513 | break; |
| 514 | /* case BOUND, RELEASED: - ignore all packets */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 515 | } |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 516 | } else if (retval > 0 && (sig = udhcp_sp_read(&rfds))) { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 517 | switch (sig) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 518 | case SIGUSR1: |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 519 | perform_renew(); |
| 520 | break; |
| 521 | case SIGUSR2: |
| 522 | perform_release(); |
| 523 | break; |
| 524 | case SIGTERM: |
| 525 | LOG(LOG_INFO, "Received SIGTERM"); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 526 | return 0; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 527 | } |
| 528 | } else if (retval == -1 && errno == EINTR) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 529 | /* a signal was caught */ |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 530 | } else { |
| 531 | /* An error occured */ |
| 532 | DEBUG(LOG_ERR, "Error on select"); |
| 533 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 534 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 535 | } |
| 536 | return 0; |
| 537 | } |