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