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