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