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