Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1 | /* dhcpd.c |
| 2 | * |
| 3 | * udhcp DHCP client |
| 4 | * |
| 5 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 6 | * |
| 7 | * Converted to busybox by Glenn McGrath August 2002 |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <sys/time.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/file.h> |
| 28 | #include <unistd.h> |
| 29 | #include <getopt.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <netinet/in.h> |
| 33 | #include <arpa/inet.h> |
| 34 | #include <signal.h> |
| 35 | #include <time.h> |
| 36 | #include <string.h> |
| 37 | #include <sys/ioctl.h> |
| 38 | #include <net/if.h> |
| 39 | #include <errno.h> |
| 40 | #include <netinet/ip.h> |
| 41 | #include <netinet/udp.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/wait.h> |
| 44 | |
| 45 | #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1 |
| 46 | #include <netpacket/packet.h> |
| 47 | #include <net/ethernet.h> |
| 48 | #else |
| 49 | #include <asm/types.h> |
| 50 | #include <linux/if_packet.h> |
| 51 | #include <linux/if_ether.h> |
| 52 | #endif |
| 53 | #include "libbb.h" |
| 54 | |
| 55 | static int state; |
| 56 | static unsigned long requested_ip; /* = 0 */ |
| 57 | static unsigned long server_addr; |
| 58 | static unsigned long timeout; |
| 59 | static int packet_num; /* = 0 */ |
| 60 | static int fd_main; |
Glenn L McGrath | d9768d7 | 2002-08-21 11:44:31 +0000 | [diff] [blame] | 61 | /* #define DEBUG */ |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 62 | |
| 63 | #define VERSION "0.9.7" |
| 64 | |
| 65 | #define LISTEN_NONE 0 |
| 66 | #define LISTEN_KERNEL 1 |
| 67 | #define LISTEN_RAW 2 |
| 68 | static int listen_mode; |
| 69 | |
| 70 | #define DEFAULT_SCRIPT "/usr/share/udhcpc/default.script" |
| 71 | |
| 72 | #define DHCP_END 0xFF |
| 73 | #define TYPE_MASK 0x0F |
| 74 | #define BROADCAST_FLAG 0x8000 |
| 75 | |
| 76 | #define SERVER_PORT 67 |
| 77 | |
| 78 | #define DHCP_MAGIC 0x63825363 |
| 79 | |
| 80 | #define BOOTREQUEST 1 |
| 81 | #define BOOTREPLY 2 |
| 82 | |
| 83 | #define ETH_10MB 1 |
| 84 | #define ETH_10MB_LEN 6 |
| 85 | |
| 86 | #define OPTION_FIELD 0 |
| 87 | #define FILE_FIELD 1 |
| 88 | #define SNAME_FIELD 2 |
| 89 | |
| 90 | #define INIT_SELECTING 0 |
| 91 | #define REQUESTING 1 |
| 92 | #define BOUND 2 |
| 93 | #define RENEWING 3 |
| 94 | #define REBINDING 4 |
| 95 | #define INIT_REBOOT 5 |
| 96 | #define RENEW_REQUESTED 6 |
| 97 | #define RELEASED 7 |
| 98 | |
| 99 | #define CLIENT_PORT 68 |
| 100 | |
| 101 | #define DHCPDISCOVER 1 |
| 102 | #define DHCPOFFER 2 |
| 103 | #define DHCPREQUEST 3 |
| 104 | #define DHCPDECLINE 4 |
| 105 | #define DHCPACK 5 |
| 106 | #define DHCPNAK 6 |
| 107 | #define DHCPRELEASE 7 |
| 108 | #define DHCPINFORM 8 |
| 109 | |
| 110 | /* DHCP option codes (partial list) */ |
| 111 | #define DHCP_PADDING 0x00 |
| 112 | #define DHCP_SUBNET 0x01 |
| 113 | #define DHCP_TIME_OFFSET 0x02 |
| 114 | #define DHCP_ROUTER 0x03 |
| 115 | #define DHCP_TIME_SERVER 0x04 |
| 116 | #define DHCP_NAME_SERVER 0x05 |
| 117 | #define DHCP_DNS_SERVER 0x06 |
| 118 | #define DHCP_LOG_SERVER 0x07 |
| 119 | #define DHCP_COOKIE_SERVER 0x08 |
| 120 | #define DHCP_LPR_SERVER 0x09 |
| 121 | #define DHCP_HOST_NAME 0x0c |
| 122 | #define DHCP_BOOT_SIZE 0x0d |
| 123 | #define DHCP_DOMAIN_NAME 0x0f |
| 124 | #define DHCP_SWAP_SERVER 0x10 |
| 125 | #define DHCP_ROOT_PATH 0x11 |
| 126 | #define DHCP_IP_TTL 0x17 |
| 127 | #define DHCP_MTU 0x1a |
| 128 | #define DHCP_BROADCAST 0x1c |
| 129 | #define DHCP_NTP_SERVER 0x2a |
| 130 | #define DHCP_WINS_SERVER 0x2c |
| 131 | #define DHCP_REQUESTED_IP 0x32 |
| 132 | #define DHCP_LEASE_TIME 0x33 |
| 133 | #define DHCP_OPTION_OVER 0x34 |
| 134 | #define DHCP_MESSAGE_TYPE 0x35 |
| 135 | #define DHCP_SERVER_ID 0x36 |
| 136 | #define DHCP_PARAM_REQ 0x37 |
| 137 | #define DHCP_MESSAGE 0x38 |
| 138 | #define DHCP_MAX_SIZE 0x39 |
| 139 | #define DHCP_T1 0x3a |
| 140 | #define DHCP_T2 0x3b |
| 141 | #define DHCP_VENDOR 0x3c |
| 142 | #define DHCP_CLIENT_ID 0x3d |
| 143 | |
| 144 | /* miscellaneous defines */ |
| 145 | #define MAC_BCAST_ADDR (unsigned char *) "\xff\xff\xff\xff\xff\xff" |
| 146 | #define OPT_CODE 0 |
| 147 | #define OPT_LEN 1 |
| 148 | #define OPT_DATA 2 |
| 149 | |
| 150 | enum { |
| 151 | OPTION_IP=1, |
| 152 | OPTION_IP_PAIR, |
| 153 | OPTION_STRING, |
| 154 | OPTION_BOOLEAN, |
| 155 | OPTION_U8, |
| 156 | OPTION_U16, |
| 157 | OPTION_S16, |
| 158 | OPTION_U32, |
| 159 | OPTION_S32 |
| 160 | }; |
| 161 | |
| 162 | #define OPTION_REQ 0x10 /* have the client request this option */ |
| 163 | #define OPTION_LIST 0x20 /* There can be a list of 1 or more of these */ |
| 164 | |
| 165 | #ifdef SYSLOG |
| 166 | # define LOG(level, str, args...) do { printf(str, ## args); \ |
| 167 | printf("\n"); \ |
| 168 | syslog(level, str, ## args); } while(0) |
| 169 | # define OPEN_LOG(name) openlog(name, 0, 0) |
| 170 | #define CLOSE_LOG() closelog() |
| 171 | #else |
| 172 | # define LOG_EMERG "EMERGENCY!" |
| 173 | # define LOG_ALERT "ALERT!" |
| 174 | # define LOG_CRIT "critical!" |
| 175 | # define LOG_WARNING "warning" |
| 176 | # define LOG_ERR "error" |
| 177 | # define LOG_INFO "info" |
| 178 | # define LOG_DEBUG "debug" |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 179 | # define LOG(level, str, args...) do { printf("%s, " str "\n", level, ## args); } while(0) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 180 | # define OPEN_LOG(name) do {;} while(0) |
| 181 | #define CLOSE_LOG() do {;} while(0) |
| 182 | #endif |
| 183 | |
| 184 | #ifdef DEBUG |
| 185 | # undef DEBUG |
| 186 | # define DEBUG(level, str, args...) LOG(level, str, ## args) |
| 187 | # define DEBUGGING |
| 188 | #else |
| 189 | # define DEBUG(level, str, args...) do {;} while(0) |
| 190 | #endif |
| 191 | |
| 192 | struct client_config_t { |
| 193 | char foreground; /* Do not fork */ |
| 194 | char quit_after_lease; /* Quit after obtaining lease */ |
| 195 | char abort_if_no_lease; /* Abort if no lease */ |
| 196 | char *interface; /* The name of the interface to use */ |
| 197 | char *pidfile; /* Optionally store the process ID */ |
| 198 | char *script; /* User script to run at dhcp events */ |
| 199 | unsigned char *clientid; /* Optional client id to use */ |
| 200 | unsigned char *hostname; /* Optional hostname to use */ |
| 201 | int ifindex; /* Index number of the interface to use */ |
| 202 | unsigned char arp[6]; /* Our arp address */ |
| 203 | }; |
| 204 | extern struct client_config_t client_config; |
| 205 | |
| 206 | struct dhcpMessage { |
| 207 | u_int8_t op; |
| 208 | u_int8_t htype; |
| 209 | u_int8_t hlen; |
| 210 | u_int8_t hops; |
| 211 | u_int32_t xid; |
| 212 | u_int16_t secs; |
| 213 | u_int16_t flags; |
| 214 | u_int32_t ciaddr; |
| 215 | u_int32_t yiaddr; |
| 216 | u_int32_t siaddr; |
| 217 | u_int32_t giaddr; |
| 218 | u_int8_t chaddr[16]; |
| 219 | u_int8_t sname[64]; |
| 220 | u_int8_t file[128]; |
| 221 | u_int32_t cookie; |
| 222 | u_int8_t options[308]; /* 312 - cookie */ |
| 223 | }; |
| 224 | |
| 225 | struct client_config_t client_config = { |
| 226 | /* Default options. */ |
| 227 | abort_if_no_lease: 0, |
| 228 | foreground: 0, |
| 229 | quit_after_lease: 0, |
| 230 | interface: "eth0", |
| 231 | pidfile: NULL, |
| 232 | script: DEFAULT_SCRIPT, |
| 233 | clientid: NULL, |
| 234 | hostname: NULL, |
| 235 | ifindex: 0, |
| 236 | arp: "\0\0\0\0\0\0", /* appease gcc-3.0 */ |
| 237 | }; |
| 238 | |
| 239 | struct dhcp_option { |
| 240 | char name[10]; |
| 241 | char flags; |
| 242 | unsigned char code; |
| 243 | }; |
| 244 | |
| 245 | struct udp_dhcp_packet { |
| 246 | struct iphdr ip; |
| 247 | struct udphdr udp; |
| 248 | struct dhcpMessage data; |
| 249 | }; |
| 250 | |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 251 | static const struct dhcp_option options[] = { |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 252 | /* name[10] flags code */ |
| 253 | {"subnet", OPTION_IP | OPTION_REQ, 0x01}, |
| 254 | {"timezone", OPTION_S32, 0x02}, |
| 255 | {"router", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03}, |
| 256 | {"timesvr", OPTION_IP | OPTION_LIST, 0x04}, |
| 257 | {"namesvr", OPTION_IP | OPTION_LIST, 0x05}, |
| 258 | {"dns", OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06}, |
| 259 | {"logsvr", OPTION_IP | OPTION_LIST, 0x07}, |
| 260 | {"cookiesvr", OPTION_IP | OPTION_LIST, 0x08}, |
| 261 | {"lprsvr", OPTION_IP | OPTION_LIST, 0x09}, |
| 262 | {"hostname", OPTION_STRING | OPTION_REQ, 0x0c}, |
| 263 | {"bootsize", OPTION_U16, 0x0d}, |
| 264 | {"domain", OPTION_STRING | OPTION_REQ, 0x0f}, |
| 265 | {"swapsvr", OPTION_IP, 0x10}, |
| 266 | {"rootpath", OPTION_STRING, 0x11}, |
| 267 | {"ipttl", OPTION_U8, 0x17}, |
| 268 | {"mtu", OPTION_U16, 0x1a}, |
| 269 | {"broadcast", OPTION_IP | OPTION_REQ, 0x1c}, |
| 270 | {"ntpsrv", OPTION_IP | OPTION_LIST, 0x2a}, |
| 271 | {"wins", OPTION_IP | OPTION_LIST, 0x2c}, |
| 272 | {"requestip", OPTION_IP, 0x32}, |
| 273 | {"lease", OPTION_U32, 0x33}, |
| 274 | {"dhcptype", OPTION_U8, 0x35}, |
| 275 | {"serverid", OPTION_IP, 0x36}, |
| 276 | {"message", OPTION_STRING, 0x38}, |
| 277 | {"tftp", OPTION_STRING, 0x42}, |
| 278 | {"bootfile", OPTION_STRING, 0x43}, |
| 279 | {"", 0x00, 0x00} |
| 280 | }; |
| 281 | |
| 282 | /* Lengths of the different option types */ |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 283 | static const unsigned char option_lengths[] = { |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 284 | [OPTION_IP] = 4, |
| 285 | [OPTION_IP_PAIR] = 8, |
| 286 | [OPTION_BOOLEAN] = 1, |
| 287 | [OPTION_STRING] = 1, |
| 288 | [OPTION_U8] = 1, |
| 289 | [OPTION_U16] = 2, |
| 290 | [OPTION_S16] = 2, |
| 291 | [OPTION_U32] = 4, |
| 292 | [OPTION_S32] = 4 |
| 293 | }; |
| 294 | |
| 295 | /* get a rough idea of how long an option will be (rounding up...) */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 296 | static const unsigned char max_option_length[] = { |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 297 | [OPTION_IP] = sizeof("255.255.255.255 "), |
| 298 | [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2, |
| 299 | [OPTION_STRING] = 1, |
| 300 | [OPTION_BOOLEAN] = sizeof("yes "), |
| 301 | [OPTION_U8] = sizeof("255 "), |
| 302 | [OPTION_U16] = sizeof("65535 "), |
| 303 | [OPTION_S16] = sizeof("-32768 "), |
| 304 | [OPTION_U32] = sizeof("4294967295 "), |
| 305 | [OPTION_S32] = sizeof("-2147483684 "), |
| 306 | }; |
| 307 | |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 308 | /* return the position of the 'end' option (no bounds checking) */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 309 | static int end_option(unsigned char *optionptr) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 310 | { |
| 311 | int i = 0; |
| 312 | |
| 313 | while (optionptr[i] != DHCP_END) { |
| 314 | if (optionptr[i] == DHCP_PADDING) i++; |
| 315 | else i += optionptr[i + OPT_LEN] + 2; |
| 316 | } |
| 317 | return i; |
| 318 | } |
| 319 | |
| 320 | /* add an option string to the options (an option string contains an option code, |
| 321 | * length, then data) */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 322 | static int add_option_string(unsigned char *optionptr, unsigned char *string) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 323 | { |
| 324 | int end = end_option(optionptr); |
| 325 | |
| 326 | /* end position + string length + option code/length + end option */ |
| 327 | if (end + string[OPT_LEN] + 2 + 1 >= 308) { |
| 328 | LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]); |
| 329 | return 0; |
| 330 | } |
| 331 | DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]); |
| 332 | memcpy(optionptr + end, string, string[OPT_LEN] + 2); |
| 333 | optionptr[end + string[OPT_LEN] + 2] = DHCP_END; |
| 334 | return string[OPT_LEN] + 2; |
| 335 | } |
| 336 | |
| 337 | /* add a one to four byte option to a packet */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 338 | static int add_simple_option(unsigned char *optionptr, unsigned char code, u_int32_t data) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 339 | { |
| 340 | char length = 0; |
| 341 | int i; |
| 342 | unsigned char option[2 + 4]; |
| 343 | unsigned char *u8; |
| 344 | u_int16_t *u16; |
| 345 | u_int32_t *u32; |
| 346 | u_int32_t aligned; |
| 347 | u8 = (unsigned char *) &aligned; |
| 348 | u16 = (u_int16_t *) &aligned; |
| 349 | u32 = &aligned; |
| 350 | |
| 351 | for (i = 0; options[i].code; i++) |
| 352 | if (options[i].code == code) { |
| 353 | length = option_lengths[options[i].flags & TYPE_MASK]; |
| 354 | } |
| 355 | |
| 356 | if (!length) { |
| 357 | DEBUG(LOG_ERR, "Could not add option 0x%02x", code); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | option[OPT_CODE] = code; |
| 362 | option[OPT_LEN] = length; |
| 363 | |
| 364 | switch (length) { |
| 365 | case 1: *u8 = data; break; |
| 366 | case 2: *u16 = data; break; |
| 367 | case 4: *u32 = data; break; |
| 368 | } |
| 369 | |
| 370 | memcpy(option + 2, &aligned, length); |
| 371 | return add_option_string(optionptr, option); |
| 372 | } |
| 373 | #if 0 |
| 374 | void init_header(struct dhcpMessage *packet, char type) |
| 375 | { |
| 376 | memset(packet, 0, sizeof(struct dhcpMessage)); |
| 377 | switch (type) { |
| 378 | case DHCPDISCOVER: |
| 379 | case DHCPREQUEST: |
| 380 | case DHCPRELEASE: |
| 381 | case DHCPINFORM: |
| 382 | packet->op = BOOTREQUEST; |
| 383 | break; |
| 384 | case DHCPOFFER: |
| 385 | case DHCPACK: |
| 386 | case DHCPNAK: |
| 387 | packet->op = BOOTREPLY; |
| 388 | } |
| 389 | packet->htype = ETH_10MB; |
| 390 | packet->hlen = ETH_10MB_LEN; |
| 391 | packet->cookie = htonl(DHCP_MAGIC); |
| 392 | packet->options[0] = DHCP_END; |
| 393 | add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type); |
| 394 | } |
| 395 | #endif |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 396 | static u_int16_t checksum(void *addr, int count) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 397 | { |
| 398 | /* Compute Internet Checksum for "count" bytes |
| 399 | * beginning at location "addr". |
| 400 | */ |
| 401 | register int32_t sum = 0; |
| 402 | u_int16_t *source = (u_int16_t *) addr; |
| 403 | |
| 404 | while( count > 1 ) { |
| 405 | /* This is the inner loop */ |
| 406 | sum += *source++; |
| 407 | count -= 2; |
| 408 | } |
| 409 | |
| 410 | /* Add left-over byte, if any */ |
| 411 | if( count > 0 ) |
| 412 | sum += * (unsigned char *) source; |
| 413 | |
| 414 | /* Fold 32-bit sum to 16 bits */ |
| 415 | while (sum>>16) |
| 416 | sum = (sum & 0xffff) + (sum >> 16); |
| 417 | |
| 418 | return ~sum; |
| 419 | } |
| 420 | |
| 421 | /* Constuct a ip/udp header for a packet, and specify the source and dest hardware address */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 422 | static int raw_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port, |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 423 | u_int32_t dest_ip, int dest_port, unsigned char *dest_arp, int ifindex) |
| 424 | { |
| 425 | int l_fd; |
| 426 | int result; |
| 427 | struct sockaddr_ll dest; |
| 428 | struct udp_dhcp_packet packet; |
| 429 | |
| 430 | if ((l_fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { |
| 431 | DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]); |
| 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | memset(&dest, 0, sizeof(dest)); |
| 436 | memset(&packet, 0, sizeof(packet)); |
| 437 | |
| 438 | dest.sll_family = AF_PACKET; |
| 439 | dest.sll_protocol = htons(ETH_P_IP); |
| 440 | dest.sll_ifindex = ifindex; |
| 441 | dest.sll_halen = 6; |
| 442 | memcpy(dest.sll_addr, dest_arp, 6); |
| 443 | if (bind(l_fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) { |
| 444 | DEBUG(LOG_ERR, "bind call failed: %s", sys_errlist[errno]); |
| 445 | close(l_fd); |
| 446 | return -1; |
| 447 | } |
| 448 | |
| 449 | packet.ip.protocol = IPPROTO_UDP; |
| 450 | packet.ip.saddr = source_ip; |
| 451 | packet.ip.daddr = dest_ip; |
| 452 | packet.udp.source = htons(source_port); |
| 453 | packet.udp.dest = htons(dest_port); |
| 454 | packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */ |
| 455 | packet.ip.tot_len = packet.udp.len; |
| 456 | memcpy(&(packet.data), payload, sizeof(struct dhcpMessage)); |
| 457 | packet.udp.check = checksum(&packet, sizeof(struct udp_dhcp_packet)); |
| 458 | |
| 459 | packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet)); |
| 460 | packet.ip.ihl = sizeof(packet.ip) >> 2; |
| 461 | packet.ip.version = IPVERSION; |
| 462 | packet.ip.ttl = IPDEFTTL; |
| 463 | packet.ip.check = checksum(&(packet.ip), sizeof(packet.ip)); |
| 464 | |
| 465 | result = sendto(l_fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest)); |
| 466 | if (result <= 0) { |
| 467 | DEBUG(LOG_ERR, "write on socket failed: %s", sys_errlist[errno]); |
| 468 | } |
| 469 | close(l_fd); |
| 470 | return result; |
| 471 | } |
| 472 | |
| 473 | /* Let the kernel do all the work for packet generation */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 474 | static int kernel_packet(struct dhcpMessage *payload, u_int32_t source_ip, int source_port, |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 475 | u_int32_t dest_ip, int dest_port) |
| 476 | { |
| 477 | int n = 1; |
| 478 | int l_fd, result; |
| 479 | struct sockaddr_in client; |
| 480 | |
| 481 | if ((l_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 482 | return -1; |
| 483 | |
| 484 | if (setsockopt(l_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) |
| 485 | return -1; |
| 486 | |
| 487 | memset(&client, 0, sizeof(client)); |
| 488 | client.sin_family = AF_INET; |
| 489 | client.sin_port = htons(source_port); |
| 490 | client.sin_addr.s_addr = source_ip; |
| 491 | |
| 492 | if (bind(l_fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) |
| 493 | return -1; |
| 494 | |
| 495 | memset(&client, 0, sizeof(client)); |
| 496 | client.sin_family = AF_INET; |
| 497 | client.sin_port = htons(dest_port); |
| 498 | client.sin_addr.s_addr = dest_ip; |
| 499 | |
| 500 | if (connect(l_fd, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1) |
| 501 | return -1; |
| 502 | |
| 503 | result = write(l_fd, payload, sizeof(struct dhcpMessage)); |
| 504 | close(l_fd); |
| 505 | return result; |
| 506 | } |
| 507 | |
| 508 | /* initialize a packet with the proper defaults */ |
| 509 | static void init_packet(struct dhcpMessage *packet, char type) |
| 510 | { |
| 511 | struct vendor { |
| 512 | char vendor, length; |
| 513 | char str[sizeof("udhcp "VERSION)]; |
| 514 | } vendor_id = { DHCP_VENDOR, sizeof("udhcp "VERSION) - 1, "udhcp "VERSION}; |
| 515 | |
| 516 | // init_header(packet, type); |
| 517 | memset(packet, 0, sizeof(struct dhcpMessage)); |
| 518 | switch (type) { |
| 519 | case DHCPDISCOVER: |
| 520 | case DHCPREQUEST: |
| 521 | case DHCPRELEASE: |
| 522 | case DHCPINFORM: |
| 523 | packet->op = BOOTREQUEST; |
| 524 | break; |
| 525 | case DHCPOFFER: |
| 526 | case DHCPACK: |
| 527 | case DHCPNAK: |
| 528 | packet->op = BOOTREPLY; |
| 529 | } |
| 530 | packet->htype = ETH_10MB; |
| 531 | packet->hlen = ETH_10MB_LEN; |
| 532 | packet->cookie = htonl(DHCP_MAGIC); |
| 533 | packet->options[0] = DHCP_END; |
| 534 | add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type); |
| 535 | |
| 536 | memcpy(packet->chaddr, client_config.arp, 6); |
| 537 | add_option_string(packet->options, client_config.clientid); |
| 538 | if (client_config.hostname) add_option_string(packet->options, client_config.hostname); |
| 539 | add_option_string(packet->options, (unsigned char *) &vendor_id); |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /* Add a paramater request list for stubborn DHCP servers. Pull the data |
| 544 | * from the struct in options.c. Don't do bounds checking here because it |
| 545 | * goes towards the head of the packet. */ |
| 546 | static void add_requests(struct dhcpMessage *packet) |
| 547 | { |
| 548 | int end = end_option(packet->options); |
| 549 | int i, len = 0; |
| 550 | |
| 551 | packet->options[end + OPT_CODE] = DHCP_PARAM_REQ; |
| 552 | for (i = 0; options[i].code; i++) |
| 553 | if (options[i].flags & OPTION_REQ) |
| 554 | packet->options[end + OPT_DATA + len++] = options[i].code; |
| 555 | packet->options[end + OPT_LEN] = len; |
| 556 | packet->options[end + OPT_DATA + len] = DHCP_END; |
| 557 | |
| 558 | } |
| 559 | |
| 560 | /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 561 | static inline int send_discover(unsigned long xid, unsigned long requested) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 562 | { |
| 563 | struct dhcpMessage packet; |
| 564 | |
| 565 | init_packet(&packet, DHCPDISCOVER); |
| 566 | packet.xid = xid; |
| 567 | if (requested) |
| 568 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); |
| 569 | |
| 570 | add_requests(&packet); |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 571 | DEBUG(LOG_DEBUG, "Sending discover..."); |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 572 | return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
| 573 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 574 | } |
| 575 | |
| 576 | /* Broadcasts a DHCP request message */ |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 577 | static inline int send_selecting(unsigned long xid, unsigned long server, unsigned long requested) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 578 | { |
| 579 | struct dhcpMessage packet; |
| 580 | struct in_addr addr; |
| 581 | |
| 582 | init_packet(&packet, DHCPREQUEST); |
| 583 | packet.xid = xid; |
| 584 | |
| 585 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); |
| 586 | add_simple_option(packet.options, DHCP_SERVER_ID, server); |
| 587 | |
| 588 | add_requests(&packet); |
| 589 | addr.s_addr = requested; |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 590 | DEBUG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr)); |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 591 | return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
| 592 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 593 | } |
| 594 | |
| 595 | |
| 596 | /* Unicasts or broadcasts a DHCP renew message */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 597 | static int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 598 | { |
| 599 | struct dhcpMessage packet; |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 600 | |
| 601 | init_packet(&packet, DHCPREQUEST); |
| 602 | packet.xid = xid; |
| 603 | packet.ciaddr = ciaddr; |
| 604 | |
| 605 | add_requests(&packet); |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 606 | DEBUG(LOG_DEBUG, "Sending renew..."); |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 607 | if (server) |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 608 | return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); |
| 609 | return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 610 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* Create a random xid */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 614 | static unsigned long random_xid(void) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 615 | { |
| 616 | static int initialized; |
| 617 | if (!initialized) { |
| 618 | srand(time(0)); |
| 619 | initialized++; |
| 620 | } |
| 621 | return rand(); |
| 622 | } |
| 623 | #if 0 |
| 624 | /* Unicasts a DHCP release message */ |
| 625 | int send_release(unsigned long server, unsigned long ciaddr) |
| 626 | { |
| 627 | struct dhcpMessage packet; |
| 628 | |
| 629 | init_packet(&packet, DHCPRELEASE); |
| 630 | packet.xid = random_xid(); |
| 631 | packet.ciaddr = ciaddr; |
| 632 | |
| 633 | add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr); |
| 634 | add_simple_option(packet.options, DHCP_SERVER_ID, server); |
| 635 | |
| 636 | LOG(LOG_DEBUG, "Sending release..."); |
| 637 | return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); |
| 638 | } |
| 639 | #endif |
| 640 | |
| 641 | /* just a little helper */ |
| 642 | static void change_mode(int new_mode) |
| 643 | { |
| 644 | DEBUG(LOG_INFO, "entering %s listen mode", |
| 645 | new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); |
| 646 | close(fd_main); |
| 647 | fd_main = -1; |
| 648 | listen_mode = new_mode; |
| 649 | } |
| 650 | |
| 651 | |
| 652 | /* SIGUSR1 handler (renew) */ |
| 653 | static void renew_requested(int sig) |
| 654 | { |
| 655 | sig = 0; |
| 656 | LOG(LOG_INFO, "Received SIGUSR1"); |
| 657 | if (state == BOUND || state == RENEWING || state == REBINDING || |
| 658 | state == RELEASED) { |
| 659 | change_mode(LISTEN_KERNEL); |
| 660 | packet_num = 0; |
| 661 | state = RENEW_REQUESTED; |
| 662 | } |
| 663 | |
| 664 | if (state == RELEASED) { |
| 665 | change_mode(LISTEN_RAW); |
| 666 | state = INIT_SELECTING; |
| 667 | } |
| 668 | |
| 669 | /* Kill any timeouts because the user wants this to hurry along */ |
| 670 | timeout = 0; |
| 671 | } |
| 672 | |
| 673 | /* get an option with bounds checking (warning, not aligned). */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 674 | static unsigned char *get_option(struct dhcpMessage *packet, int code) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 675 | { |
| 676 | int i, length; |
| 677 | unsigned char *optionptr; |
| 678 | int over = 0, done = 0, curr = OPTION_FIELD; |
| 679 | |
| 680 | optionptr = packet->options; |
| 681 | i = 0; |
| 682 | length = 308; |
| 683 | while (!done) { |
| 684 | if (i >= length) { |
| 685 | LOG(LOG_WARNING, "bogus packet, option fields too long."); |
| 686 | return NULL; |
| 687 | } |
| 688 | if (optionptr[i + OPT_CODE] == code) { |
| 689 | if (i + 1 + optionptr[i + OPT_LEN] >= length) { |
| 690 | LOG(LOG_WARNING, "bogus packet, option fields too long."); |
| 691 | return NULL; |
| 692 | } |
| 693 | return optionptr + i + 2; |
| 694 | } |
| 695 | switch (optionptr[i + OPT_CODE]) { |
| 696 | case DHCP_PADDING: |
| 697 | i++; |
| 698 | break; |
| 699 | case DHCP_OPTION_OVER: |
| 700 | if (i + 1 + optionptr[i + OPT_LEN] >= length) { |
| 701 | LOG(LOG_WARNING, "bogus packet, option fields too long."); |
| 702 | return NULL; |
| 703 | } |
| 704 | over = optionptr[i + 3]; |
| 705 | i += optionptr[OPT_LEN] + 2; |
| 706 | break; |
| 707 | case DHCP_END: |
| 708 | if (curr == OPTION_FIELD && over & FILE_FIELD) { |
| 709 | optionptr = packet->file; |
| 710 | i = 0; |
| 711 | length = 128; |
| 712 | curr = FILE_FIELD; |
| 713 | } else if (curr == FILE_FIELD && over & SNAME_FIELD) { |
| 714 | optionptr = packet->sname; |
| 715 | i = 0; |
| 716 | length = 64; |
| 717 | curr = SNAME_FIELD; |
| 718 | } else done = 1; |
| 719 | break; |
| 720 | default: |
| 721 | i += optionptr[OPT_LEN + i] + 2; |
| 722 | } |
| 723 | } |
| 724 | return NULL; |
| 725 | } |
| 726 | #if 0 |
| 727 | static int upper_length(int length, struct dhcp_option *option) |
| 728 | { |
| 729 | return max_option_length[option->flags & TYPE_MASK] * |
| 730 | (length / option_lengths[option->flags & TYPE_MASK]); |
| 731 | } |
| 732 | #endif |
| 733 | |
| 734 | static int sprintip(char *dest, char *pre, unsigned char *ip) { |
| 735 | return sprintf(dest, "%s%d.%d.%d.%d ", pre, ip[0], ip[1], ip[2], ip[3]); |
| 736 | } |
| 737 | |
| 738 | |
| 739 | /* Fill dest with the text of option 'option'. */ |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 740 | extern inline void fill_options(char *dest, unsigned char *option, const struct dhcp_option *type_p) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 741 | { |
| 742 | int type, optlen; |
| 743 | u_int16_t val_u16; |
| 744 | int16_t val_s16; |
| 745 | u_int32_t val_u32; |
| 746 | int32_t val_s32; |
| 747 | int len = option[OPT_LEN - 2]; |
| 748 | |
| 749 | dest += sprintf(dest, "%s=", type_p->name); |
| 750 | |
| 751 | type = type_p->flags & TYPE_MASK; |
| 752 | optlen = option_lengths[type]; |
| 753 | for(;;) { |
| 754 | switch (type) { |
| 755 | case OPTION_IP_PAIR: |
| 756 | dest += sprintip(dest, "", option); |
| 757 | *(dest++) = '/'; |
| 758 | option += 4; |
| 759 | optlen = 4; |
| 760 | case OPTION_IP: /* Works regardless of host byte order. */ |
| 761 | dest += sprintip(dest, "", option); |
| 762 | break; |
| 763 | case OPTION_BOOLEAN: |
| 764 | dest += sprintf(dest, *option ? "yes " : "no "); |
| 765 | break; |
| 766 | case OPTION_U8: |
| 767 | dest += sprintf(dest, "%u ", *option); |
| 768 | break; |
| 769 | case OPTION_U16: |
| 770 | memcpy(&val_u16, option, 2); |
| 771 | dest += sprintf(dest, "%u ", ntohs(val_u16)); |
| 772 | break; |
| 773 | case OPTION_S16: |
| 774 | memcpy(&val_s16, option, 2); |
| 775 | dest += sprintf(dest, "%d ", ntohs(val_s16)); |
| 776 | break; |
| 777 | case OPTION_U32: |
| 778 | memcpy(&val_u32, option, 4); |
| 779 | dest += sprintf(dest, "%lu ", (unsigned long) ntohl(val_u32)); |
| 780 | break; |
| 781 | case OPTION_S32: |
| 782 | memcpy(&val_s32, option, 4); |
| 783 | dest += sprintf(dest, "%ld ", (long) ntohl(val_s32)); |
| 784 | break; |
| 785 | case OPTION_STRING: |
| 786 | memcpy(dest, option, len); |
| 787 | dest[len] = '\0'; |
| 788 | return; /* Short circuit this case */ |
| 789 | } |
| 790 | option += optlen; |
| 791 | len -= optlen; |
| 792 | if (len <= 0) break; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | static char *find_env(const char *prefix, char *defaultstr) |
| 797 | { |
| 798 | extern char **environ; |
| 799 | char **ptr; |
| 800 | const int len = strlen(prefix); |
| 801 | |
| 802 | for (ptr = environ; *ptr != NULL; ptr++) { |
| 803 | if (strncmp(prefix, *ptr, len) == 0) |
| 804 | return *ptr; |
| 805 | } |
| 806 | return defaultstr; |
| 807 | } |
| 808 | |
| 809 | /* put all the paramaters into an environment */ |
| 810 | static char **fill_envp(struct dhcpMessage *packet) |
| 811 | { |
| 812 | /* supported options are easily added here */ |
| 813 | int num_options = 0; |
| 814 | int i, j; |
| 815 | char **envp; |
| 816 | unsigned char *temp; |
| 817 | char over = 0; |
| 818 | |
| 819 | if (packet == NULL) |
| 820 | num_options = 0; |
| 821 | else { |
| 822 | for (i = 0; options[i].code; i++) |
| 823 | if (get_option(packet, options[i].code)) |
| 824 | num_options++; |
| 825 | if (packet->siaddr) num_options++; |
| 826 | if ((temp = get_option(packet, DHCP_OPTION_OVER))) |
| 827 | over = *temp; |
| 828 | if (!(over & FILE_FIELD) && packet->file[0]) num_options++; |
| 829 | if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++; |
| 830 | } |
| 831 | |
| 832 | envp = xmalloc((num_options + 5) * sizeof(char *)); |
| 833 | envp[0] = xmalloc(sizeof("interface=") + strlen(client_config.interface)); |
| 834 | sprintf(envp[0], "interface=%s", client_config.interface); |
| 835 | envp[1] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin"); |
| 836 | envp[2] = find_env("HOME", "HOME=/"); |
| 837 | |
| 838 | if (packet == NULL) { |
| 839 | envp[3] = NULL; |
| 840 | return envp; |
| 841 | } |
| 842 | |
| 843 | envp[3] = xmalloc(sizeof("ip=255.255.255.255")); |
| 844 | sprintip(envp[3], "ip=", (unsigned char *) &packet->yiaddr); |
| 845 | for (i = 0, j = 4; options[i].code; i++) { |
| 846 | if ((temp = get_option(packet, options[i].code))) { |
| 847 | envp[j] = xmalloc(max_option_length[(&options[i])->flags & TYPE_MASK] * (temp[OPT_LEN - 2] / option_lengths[(&options[i])->flags & TYPE_MASK]) + strlen((&options[i])->name) + 2); |
| 848 | // envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], &options[i]) + strlen(options[i].name) + 2); |
| 849 | fill_options(envp[j], temp, &options[i]); |
| 850 | j++; |
| 851 | } |
| 852 | } |
| 853 | if (packet->siaddr) { |
| 854 | envp[j] = xmalloc(sizeof("siaddr=255.255.255.255")); |
| 855 | sprintip(envp[j++], "siaddr=", (unsigned char *) &packet->yiaddr); |
| 856 | } |
| 857 | if (!(over & FILE_FIELD) && packet->file[0]) { |
| 858 | /* watch out for invalid packets */ |
| 859 | packet->file[sizeof(packet->file) - 1] = '\0'; |
| 860 | envp[j] = xmalloc(sizeof("boot_file=") + strlen(packet->file)); |
| 861 | sprintf(envp[j++], "boot_file=%s", packet->file); |
| 862 | } |
| 863 | if (!(over & SNAME_FIELD) && packet->sname[0]) { |
| 864 | /* watch out for invalid packets */ |
| 865 | packet->sname[sizeof(packet->sname) - 1] = '\0'; |
| 866 | envp[j] = xmalloc(sizeof("sname=") + strlen(packet->sname)); |
| 867 | sprintf(envp[j++], "sname=%s", packet->sname); |
| 868 | } |
| 869 | envp[j] = NULL; |
| 870 | return envp; |
| 871 | } |
| 872 | |
| 873 | /* Call a script with a par file and env vars */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 874 | static void run_script(struct dhcpMessage *packet, const char *name) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 875 | { |
| 876 | int pid; |
| 877 | char **envp; |
| 878 | |
| 879 | if (client_config.script == NULL) |
| 880 | return; |
| 881 | |
| 882 | /* call script */ |
| 883 | pid = fork(); |
| 884 | if (pid) { |
| 885 | waitpid(pid, NULL, 0); |
| 886 | return; |
| 887 | } else if (pid == 0) { |
| 888 | envp = fill_envp(packet); |
| 889 | |
| 890 | /* close fd's? */ |
| 891 | |
| 892 | /* exec script */ |
| 893 | DEBUG(LOG_INFO, "execle'ing %s", client_config.script); |
| 894 | execle(client_config.script, client_config.script, |
| 895 | name, NULL, envp); |
| 896 | LOG(LOG_ERR, "script %s failed: %s", |
| 897 | client_config.script, sys_errlist[errno]); |
| 898 | exit(1); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | /* SIGUSR2 handler (release) */ |
| 903 | static void release_requested(int sig) |
| 904 | { |
| 905 | sig = 0; |
| 906 | LOG(LOG_INFO, "Received SIGUSR2"); |
| 907 | /* send release packet */ |
| 908 | if (state == BOUND || state == RENEWING || state == REBINDING) { |
| 909 | struct dhcpMessage packet; |
| 910 | |
| 911 | init_packet(&packet, DHCPRELEASE); |
| 912 | packet.xid = random_xid(); |
| 913 | packet.ciaddr = requested_ip; |
| 914 | |
| 915 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested_ip); |
| 916 | add_simple_option(packet.options, DHCP_SERVER_ID, server_addr); |
| 917 | |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 918 | DEBUG(LOG_DEBUG, "Sending release..."); |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 919 | kernel_packet(&packet, requested_ip, CLIENT_PORT, server_addr, SERVER_PORT); |
| 920 | run_script(NULL, "deconfig"); |
| 921 | } |
| 922 | |
| 923 | change_mode(LISTEN_NONE); |
| 924 | state = RELEASED; |
| 925 | timeout = 0x7fffffff; |
| 926 | } |
| 927 | |
| 928 | |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 929 | static int pidfile_acquire(char *pidfile) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 930 | { |
| 931 | int pid_fd; |
| 932 | if (pidfile == NULL) return -1; |
| 933 | |
| 934 | pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644); |
| 935 | if (pid_fd < 0) { |
| 936 | LOG(LOG_ERR, "Unable to open pidfile %s: %s\n", |
| 937 | pidfile, strerror(errno)); |
| 938 | } else { |
| 939 | lockf(pid_fd, F_LOCK, 0); |
| 940 | } |
| 941 | |
| 942 | return pid_fd; |
| 943 | } |
| 944 | |
| 945 | |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 946 | static void pidfile_write_release(int pid_fd) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 947 | { |
| 948 | FILE *out; |
| 949 | |
| 950 | if (pid_fd < 0) return; |
| 951 | |
| 952 | if ((out = fdopen(pid_fd, "w")) != NULL) { |
| 953 | fprintf(out, "%d\n", getpid()); |
| 954 | fclose(out); |
| 955 | } |
| 956 | lockf(pid_fd, F_UNLCK, 0); |
| 957 | close(pid_fd); |
| 958 | } |
| 959 | |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 960 | /* Exit and cleanup */ |
| 961 | static void exit_client(int retval) |
| 962 | { |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 963 | unlink(client_config.pidfile); |
| 964 | if (client_config.pidfile) unlink(client_config.pidfile); |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 965 | CLOSE_LOG(); |
| 966 | exit(retval); |
| 967 | } |
| 968 | |
| 969 | |
| 970 | /* SIGTERM handler */ |
| 971 | static void terminate(int sig) |
| 972 | { |
| 973 | sig = 0; |
| 974 | LOG(LOG_INFO, "Received SIGTERM"); |
| 975 | exit_client(0); |
| 976 | } |
| 977 | |
| 978 | |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 979 | extern inline int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 980 | { |
| 981 | int l_fd; |
| 982 | struct ifreq ifr; |
| 983 | struct sockaddr_in *s_in; |
| 984 | |
| 985 | memset(&ifr, 0, sizeof(struct ifreq)); |
| 986 | if((l_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) { |
| 987 | ifr.ifr_addr.sa_family = AF_INET; |
| 988 | strcpy(ifr.ifr_name, interface); |
| 989 | |
| 990 | if (addr) { |
| 991 | if (ioctl(l_fd, SIOCGIFADDR, &ifr) == 0) { |
| 992 | s_in = (struct sockaddr_in *) &ifr.ifr_addr; |
| 993 | *addr = s_in->sin_addr.s_addr; |
| 994 | DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(s_in->sin_addr)); |
| 995 | } else { |
| 996 | LOG(LOG_ERR, "SIOCGIFADDR failed!: %s", sys_errlist[errno]); |
| 997 | return -1; |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | if (ioctl(l_fd, SIOCGIFINDEX, &ifr) == 0) { |
| 1002 | DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex); |
| 1003 | *ifindex = ifr.ifr_ifindex; |
| 1004 | } else { |
| 1005 | LOG(LOG_ERR, "SIOCGIFINDEX failed!: %s", sys_errlist[errno]); |
| 1006 | return -1; |
| 1007 | } |
| 1008 | if (ioctl(l_fd, SIOCGIFHWADDR, &ifr) == 0) { |
| 1009 | memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); |
| 1010 | DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", |
| 1011 | arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); |
| 1012 | } else { |
| 1013 | LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %s", sys_errlist[errno]); |
| 1014 | return -1; |
| 1015 | } |
| 1016 | } else { |
| 1017 | LOG(LOG_ERR, "socket failed!: %s", sys_errlist[errno]); |
| 1018 | return -1; |
| 1019 | } |
| 1020 | close(l_fd); |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 1025 | extern inline int listen_socket(unsigned int ip, int port, char *inf) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1026 | { |
| 1027 | struct ifreq interface; |
| 1028 | int l_fd; |
| 1029 | struct sockaddr_in addr; |
| 1030 | int n = 1; |
| 1031 | |
| 1032 | DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s\n", ip, port, inf); |
| 1033 | if ((l_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { |
| 1034 | DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]); |
| 1035 | return -1; |
| 1036 | } |
| 1037 | |
| 1038 | memset(&addr, 0, sizeof(addr)); |
| 1039 | addr.sin_family = AF_INET; |
| 1040 | addr.sin_port = htons(port); |
| 1041 | addr.sin_addr.s_addr = ip; |
| 1042 | |
| 1043 | if (setsockopt(l_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) { |
| 1044 | close(l_fd); |
| 1045 | return -1; |
| 1046 | } |
| 1047 | if (setsockopt(l_fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) { |
| 1048 | close(l_fd); |
| 1049 | return -1; |
| 1050 | } |
| 1051 | |
| 1052 | strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ); |
| 1053 | if (setsockopt(l_fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) { |
| 1054 | close(l_fd); |
| 1055 | return -1; |
| 1056 | } |
| 1057 | |
| 1058 | if (bind(l_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) { |
| 1059 | close(l_fd); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
| 1063 | return l_fd; |
| 1064 | } |
| 1065 | |
| 1066 | |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 1067 | static int raw_socket(int ifindex) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1068 | { |
| 1069 | int l_fd; |
| 1070 | struct sockaddr_ll sock; |
| 1071 | |
| 1072 | DEBUG(LOG_INFO, "Opening raw socket on ifindex %d\n", ifindex); |
| 1073 | if ((l_fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { |
| 1074 | DEBUG(LOG_ERR, "socket call failed: %s", sys_errlist[errno]); |
| 1075 | return -1; |
| 1076 | } |
| 1077 | |
| 1078 | sock.sll_family = AF_PACKET; |
| 1079 | sock.sll_protocol = htons(ETH_P_IP); |
| 1080 | sock.sll_ifindex = ifindex; |
| 1081 | if (bind(l_fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) { |
| 1082 | DEBUG(LOG_ERR, "bind call failed: %s", sys_errlist[errno]); |
| 1083 | close(l_fd); |
| 1084 | return -1; |
| 1085 | } |
| 1086 | |
| 1087 | return l_fd; |
| 1088 | |
| 1089 | } |
| 1090 | |
| 1091 | /* read a packet from socket fd, return -1 on read error, -2 on packet error */ |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 1092 | static int get_packet(struct dhcpMessage *packet, int l_fd) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1093 | { |
| 1094 | int bytes; |
| 1095 | int i; |
| 1096 | const char broken_vendors[][8] = { |
| 1097 | "MSFT 98", |
| 1098 | "" |
| 1099 | }; |
| 1100 | char unsigned *vendor; |
| 1101 | |
| 1102 | memset(packet, 0, sizeof(struct dhcpMessage)); |
| 1103 | bytes = read(l_fd, packet, sizeof(struct dhcpMessage)); |
| 1104 | if (bytes < 0) { |
| 1105 | DEBUG(LOG_INFO, "couldn't read on listening socket, ignoring"); |
| 1106 | return -1; |
| 1107 | } |
| 1108 | |
| 1109 | if (ntohl(packet->cookie) != DHCP_MAGIC) { |
| 1110 | LOG(LOG_ERR, "received bogus message, ignoring"); |
| 1111 | return -2; |
| 1112 | } |
| 1113 | DEBUG(LOG_INFO, "Received a packet"); |
| 1114 | |
| 1115 | if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) { |
| 1116 | for (i = 0; broken_vendors[i][0]; i++) { |
| 1117 | if (vendor[OPT_LEN - 2] == (unsigned char) strlen(broken_vendors[i]) && |
| 1118 | !strncmp(vendor, broken_vendors[i], vendor[OPT_LEN - 2])) { |
| 1119 | DEBUG(LOG_INFO, "broken client (%s), forcing broadcast", |
| 1120 | broken_vendors[i]); |
| 1121 | packet->flags |= htons(BROADCAST_FLAG); |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | |
| 1127 | return bytes; |
| 1128 | } |
| 1129 | |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 1130 | extern inline int get_raw_packet(struct dhcpMessage *payload, int l_fd) |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1131 | { |
| 1132 | int bytes; |
| 1133 | struct udp_dhcp_packet packet; |
| 1134 | u_int32_t source, dest; |
| 1135 | u_int16_t check; |
| 1136 | |
| 1137 | memset(&packet, 0, sizeof(struct udp_dhcp_packet)); |
| 1138 | bytes = read(l_fd, &packet, sizeof(struct udp_dhcp_packet)); |
| 1139 | if (bytes < 0) { |
| 1140 | DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring"); |
| 1141 | usleep(500000); /* possible down interface, looping condition */ |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
| 1145 | if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) { |
| 1146 | DEBUG(LOG_INFO, "message too short, ignoring"); |
| 1147 | return -1; |
| 1148 | } |
| 1149 | |
| 1150 | if (bytes < ntohs(packet.ip.tot_len)) { |
| 1151 | DEBUG(LOG_INFO, "Truncated packet"); |
| 1152 | return -1; |
| 1153 | } |
| 1154 | |
| 1155 | /* ignore any extra garbage bytes */ |
| 1156 | bytes = ntohs(packet.ip.tot_len); |
| 1157 | |
| 1158 | /* Make sure its the right packet for us, and that it passes sanity checks */ |
| 1159 | if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION || |
| 1160 | packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) || |
| 1161 | bytes > (int) sizeof(struct udp_dhcp_packet) || |
| 1162 | ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) { |
| 1163 | DEBUG(LOG_INFO, "unrelated/bogus packet"); |
| 1164 | return -1; |
| 1165 | } |
| 1166 | |
| 1167 | /* check IP checksum */ |
| 1168 | check = packet.ip.check; |
| 1169 | packet.ip.check = 0; |
| 1170 | if (check != checksum(&(packet.ip), sizeof(packet.ip))) { |
| 1171 | DEBUG(LOG_INFO, "bad IP header checksum, ignoring"); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | |
| 1175 | /* verify the UDP checksum by replacing the header with a psuedo header */ |
| 1176 | source = packet.ip.saddr; |
| 1177 | dest = packet.ip.daddr; |
| 1178 | check = packet.udp.check; |
| 1179 | packet.udp.check = 0; |
| 1180 | memset(&packet.ip, 0, sizeof(packet.ip)); |
| 1181 | |
| 1182 | packet.ip.protocol = IPPROTO_UDP; |
| 1183 | packet.ip.saddr = source; |
| 1184 | packet.ip.daddr = dest; |
| 1185 | packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */ |
| 1186 | if (check && check != checksum(&packet, bytes)) { |
| 1187 | DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring"); |
| 1188 | return -1; |
| 1189 | } |
| 1190 | |
| 1191 | memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp))); |
| 1192 | |
| 1193 | if (ntohl(payload->cookie) != DHCP_MAGIC) { |
| 1194 | LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring"); |
| 1195 | return -1; |
| 1196 | } |
| 1197 | DEBUG(LOG_INFO, "oooooh!!! got some!"); |
| 1198 | return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | int udhcpc_main(int argc, char *argv[]) |
| 1203 | { |
| 1204 | unsigned char *temp, *message; |
| 1205 | unsigned long t1 = 0, t2 = 0, xid = 0; |
| 1206 | unsigned long start = 0, lease; |
| 1207 | fd_set rfds; |
| 1208 | int retval; |
| 1209 | struct timeval tv; |
| 1210 | int c, len; |
| 1211 | struct dhcpMessage packet; |
| 1212 | struct in_addr temp_addr; |
| 1213 | int pid_fd; |
| 1214 | time_t now; |
| 1215 | |
| 1216 | static struct option l_options[] = { |
| 1217 | {"clientid", required_argument, 0, 'c'}, |
| 1218 | {"foreground", no_argument, 0, 'f'}, |
| 1219 | {"hostname", required_argument, 0, 'H'}, |
| 1220 | {"help", no_argument, 0, 'h'}, |
| 1221 | {"interface", required_argument, 0, 'i'}, |
| 1222 | {"now", no_argument, 0, 'n'}, |
| 1223 | {"pidfile", required_argument, 0, 'p'}, |
| 1224 | {"quit", no_argument, 0, 'q'}, |
| 1225 | {"request", required_argument, 0, 'r'}, |
| 1226 | {"script", required_argument, 0, 's'}, |
| 1227 | {"version", no_argument, 0, 'v'}, |
| 1228 | {0, 0, 0, 0} |
| 1229 | }; |
| 1230 | |
| 1231 | /* get options */ |
| 1232 | while (1) { |
| 1233 | int option_index = 0; |
| 1234 | c = getopt_long(argc, argv, "c:fH:hi:np:qr:s:v", l_options, &option_index); |
| 1235 | if (c == -1) break; |
| 1236 | |
| 1237 | switch (c) { |
| 1238 | case 'c': |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 1239 | len = strlen(optarg); |
| 1240 | if (len > 255) len = 255; |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1241 | if (client_config.clientid) free(client_config.clientid); |
| 1242 | client_config.clientid = xmalloc(len + 2); |
| 1243 | client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID; |
| 1244 | client_config.clientid[OPT_LEN] = len; |
| 1245 | client_config.clientid[OPT_DATA] = '\0'; |
| 1246 | strncpy(client_config.clientid + 3, optarg, len - 1); |
| 1247 | break; |
| 1248 | case 'f': |
| 1249 | client_config.foreground = 1; |
| 1250 | break; |
| 1251 | case 'H': |
Aaron Lehmann | a95e99e | 2002-08-21 12:44:54 +0000 | [diff] [blame] | 1252 | len = strlen(optarg); |
| 1253 | if (len > 255) len = 255; |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1254 | if (client_config.hostname) free(client_config.hostname); |
| 1255 | client_config.hostname = xmalloc(len + 2); |
| 1256 | client_config.hostname[OPT_CODE] = DHCP_HOST_NAME; |
| 1257 | client_config.hostname[OPT_LEN] = len; |
| 1258 | strncpy(client_config.hostname + 2, optarg, len); |
| 1259 | break; |
| 1260 | case 'h': |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 1261 | puts( |
| 1262 | "Usage: udhcpcd [OPTIONS]\n\n" |
| 1263 | " -c, --clientid=CLIENTID Client identifier\n" |
| 1264 | " -H, --hostname=HOSTNAME Client hostname\n" |
| 1265 | " -f, --foreground Do not fork after getting lease\n" |
| 1266 | " -i, --interface=INTERFACE Interface to use (default: eth0)\n" |
| 1267 | " -n, --now Exit with failure if lease cannot be\n" |
| 1268 | " immediately negotiated.\n" |
| 1269 | " -p, --pidfile=file Store process ID of daemon in file\n" |
| 1270 | " -q, --quit Quit after obtaining lease\n" |
| 1271 | " -r, --request=IP IP address to request (default: none)\n" |
| 1272 | " -s, --script=file Run file at dhcp events (default:\n" |
| 1273 | " " DEFAULT_SCRIPT ")\n" |
| 1274 | " -v, --version Display version" |
| 1275 | ); |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1276 | return 0; |
| 1277 | case 'i': |
| 1278 | client_config.interface = optarg; |
| 1279 | break; |
| 1280 | case 'n': |
| 1281 | client_config.abort_if_no_lease = 1; |
| 1282 | break; |
| 1283 | case 'p': |
| 1284 | client_config.pidfile = optarg; |
| 1285 | break; |
| 1286 | case 'q': |
| 1287 | client_config.quit_after_lease = 1; |
| 1288 | break; |
| 1289 | case 'r': |
| 1290 | requested_ip = inet_addr(optarg); |
| 1291 | break; |
| 1292 | case 's': |
| 1293 | client_config.script = optarg; |
| 1294 | break; |
| 1295 | case 'v': |
| 1296 | printf("udhcpcd, version %s\n\n", VERSION); |
| 1297 | exit_client(0); |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | OPEN_LOG("udhcpc"); |
| 1303 | LOG(LOG_INFO, "udhcp client (v%s) started", VERSION); |
| 1304 | |
| 1305 | pid_fd = pidfile_acquire(client_config.pidfile); |
| 1306 | pidfile_write_release(pid_fd); |
| 1307 | |
| 1308 | if (read_interface(client_config.interface, &client_config.ifindex, NULL, client_config.arp) < 0) { |
| 1309 | exit_client(1); |
| 1310 | } |
| 1311 | |
| 1312 | if (!client_config.clientid) { |
| 1313 | client_config.clientid = xmalloc(6 + 3); |
| 1314 | client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID; |
| 1315 | client_config.clientid[OPT_LEN] = 7; |
| 1316 | client_config.clientid[OPT_DATA] = 1; |
| 1317 | memcpy(client_config.clientid + 3, client_config.arp, 6); |
| 1318 | } |
| 1319 | |
| 1320 | /* setup signal handlers */ |
| 1321 | signal(SIGUSR1, renew_requested); |
| 1322 | signal(SIGUSR2, release_requested); |
| 1323 | signal(SIGTERM, terminate); |
| 1324 | |
| 1325 | state = INIT_SELECTING; |
| 1326 | run_script(NULL, "deconfig"); |
| 1327 | change_mode(LISTEN_RAW); |
| 1328 | |
| 1329 | for (;;) { |
| 1330 | tv.tv_sec = timeout - time(0); |
| 1331 | tv.tv_usec = 0; |
| 1332 | FD_ZERO(&rfds); |
| 1333 | |
| 1334 | if (listen_mode != LISTEN_NONE && fd_main < 0) { |
| 1335 | if (listen_mode == LISTEN_KERNEL) { |
| 1336 | fd_main = listen_socket(INADDR_ANY, CLIENT_PORT, client_config.interface); |
| 1337 | } else { |
| 1338 | fd_main = raw_socket(client_config.ifindex); |
| 1339 | } |
| 1340 | if (fd_main < 0) { |
| 1341 | LOG(LOG_ERR, "FATAL: couldn't listen on socket, %s", sys_errlist[errno]); |
| 1342 | exit_client(0); |
| 1343 | } |
| 1344 | } |
| 1345 | if (fd_main >= 0) { |
| 1346 | FD_SET(fd_main, &rfds); |
| 1347 | } |
| 1348 | |
| 1349 | if (tv.tv_sec > 0) { |
| 1350 | DEBUG(LOG_INFO, "Waiting on select...\n"); |
| 1351 | retval = select(fd_main + 1, &rfds, NULL, NULL, &tv); |
| 1352 | } else { |
| 1353 | retval = 0; /* If we already timed out, fall through */ |
| 1354 | } |
| 1355 | |
| 1356 | now = time(0); |
| 1357 | if (retval == 0) { |
| 1358 | /* timeout dropped to zero */ |
| 1359 | switch (state) { |
| 1360 | case INIT_SELECTING: |
| 1361 | if (packet_num < 3) { |
| 1362 | if (packet_num == 0) { |
| 1363 | xid = random_xid(); |
| 1364 | } |
| 1365 | /* send discover packet */ |
| 1366 | send_discover(xid, requested_ip); /* broadcast */ |
| 1367 | |
| 1368 | timeout = now + ((packet_num == 2) ? 10 : 2); |
| 1369 | packet_num++; |
| 1370 | } else { |
| 1371 | if (client_config.abort_if_no_lease) { |
| 1372 | LOG(LOG_INFO, "No lease, failing."); |
| 1373 | exit_client(1); |
| 1374 | } |
| 1375 | /* wait to try again */ |
| 1376 | packet_num = 0; |
| 1377 | timeout = now + 60; |
| 1378 | } |
| 1379 | break; |
| 1380 | case RENEW_REQUESTED: |
| 1381 | case REQUESTING: |
| 1382 | if (packet_num < 3) { |
| 1383 | /* send request packet */ |
| 1384 | if (state == RENEW_REQUESTED) { |
| 1385 | send_renew(xid, server_addr, requested_ip); /* unicast */ |
| 1386 | } else { |
| 1387 | send_selecting(xid, server_addr, requested_ip); /* broadcast */ |
| 1388 | } |
| 1389 | timeout = now + ((packet_num == 2) ? 10 : 2); |
| 1390 | packet_num++; |
| 1391 | } else { |
| 1392 | /* timed out, go back to init state */ |
| 1393 | state = INIT_SELECTING; |
| 1394 | timeout = now; |
| 1395 | packet_num = 0; |
| 1396 | change_mode(LISTEN_RAW); |
| 1397 | } |
| 1398 | break; |
| 1399 | case BOUND: |
| 1400 | /* Lease is starting to run out, time to enter renewing state */ |
| 1401 | state = RENEWING; |
| 1402 | change_mode(LISTEN_KERNEL); |
| 1403 | DEBUG(LOG_INFO, "Entering renew state"); |
| 1404 | /* fall right through */ |
| 1405 | case RENEWING: |
| 1406 | /* Either set a new T1, or enter REBINDING state */ |
| 1407 | if ((t2 - t1) <= (lease / 14400 + 1)) { |
| 1408 | /* timed out, enter rebinding state */ |
| 1409 | state = REBINDING; |
| 1410 | timeout = now + (t2 - t1); |
| 1411 | DEBUG(LOG_INFO, "Entering rebinding state"); |
| 1412 | } else { |
| 1413 | /* send a request packet */ |
| 1414 | send_renew(xid, server_addr, requested_ip); /* unicast */ |
| 1415 | |
| 1416 | t1 = (t2 - t1) / 2 + t1; |
| 1417 | timeout = t1 + start; |
| 1418 | } |
| 1419 | break; |
| 1420 | case REBINDING: |
| 1421 | /* Either set a new T2, or enter INIT state */ |
| 1422 | if ((lease - t2) <= (lease / 14400 + 1)) { |
| 1423 | /* timed out, enter init state */ |
| 1424 | state = INIT_SELECTING; |
| 1425 | LOG(LOG_INFO, "Lease lost, entering init state"); |
| 1426 | run_script(NULL, "deconfig"); |
| 1427 | timeout = now; |
| 1428 | packet_num = 0; |
| 1429 | change_mode(LISTEN_RAW); |
| 1430 | } else { |
| 1431 | /* send a request packet */ |
| 1432 | send_renew(xid, 0, requested_ip); /* broadcast */ |
| 1433 | |
| 1434 | t2 = (lease - t2) / 2 + t2; |
| 1435 | timeout = t2 + start; |
| 1436 | } |
| 1437 | break; |
| 1438 | case RELEASED: |
| 1439 | /* yah, I know, *you* say it would never happen */ |
| 1440 | timeout = 0x7fffffff; |
| 1441 | break; |
| 1442 | } |
| 1443 | } else if (retval > 0 && listen_mode != LISTEN_NONE && FD_ISSET(fd_main, &rfds)) { |
| 1444 | /* a packet is ready, read it */ |
| 1445 | |
| 1446 | if (listen_mode == LISTEN_KERNEL) { |
| 1447 | len = get_packet(&packet, fd_main); |
| 1448 | } else { |
| 1449 | len = get_raw_packet(&packet, fd_main); |
| 1450 | } |
| 1451 | if (len == -1 && errno != EINTR) { |
| 1452 | DEBUG(LOG_INFO, "error on read, %s, reopening socket", sys_errlist[errno]); |
| 1453 | change_mode(listen_mode); /* just close and reopen */ |
| 1454 | } |
| 1455 | if (len < 0) { |
| 1456 | continue; |
| 1457 | } |
| 1458 | |
| 1459 | if (packet.xid != xid) { |
| 1460 | DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)", |
| 1461 | (unsigned long) packet.xid, xid); |
| 1462 | continue; |
| 1463 | } |
| 1464 | |
| 1465 | if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) { |
| 1466 | DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring"); |
| 1467 | continue; |
| 1468 | } |
| 1469 | |
| 1470 | switch (state) { |
| 1471 | case INIT_SELECTING: |
| 1472 | /* Must be a DHCPOFFER to one of our xid's */ |
| 1473 | if (*message == DHCPOFFER) { |
| 1474 | if ((temp = get_option(&packet, DHCP_SERVER_ID))) { |
| 1475 | memcpy(&server_addr, temp, 4); |
| 1476 | xid = packet.xid; |
| 1477 | requested_ip = packet.yiaddr; |
| 1478 | |
| 1479 | /* enter requesting state */ |
| 1480 | state = REQUESTING; |
| 1481 | timeout = now; |
| 1482 | packet_num = 0; |
| 1483 | } else { |
| 1484 | DEBUG(LOG_ERR, "No server ID in message"); |
| 1485 | } |
| 1486 | } |
| 1487 | break; |
| 1488 | case RENEW_REQUESTED: |
| 1489 | case REQUESTING: |
| 1490 | case RENEWING: |
| 1491 | case REBINDING: |
| 1492 | if (*message == DHCPACK) { |
| 1493 | if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) { |
| 1494 | LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease"); |
| 1495 | lease = 60 * 60; |
| 1496 | } else { |
| 1497 | memcpy(&lease, temp, 4); |
| 1498 | lease = ntohl(lease); |
| 1499 | } |
| 1500 | |
| 1501 | /* enter bound state */ |
| 1502 | t1 = lease / 2; |
| 1503 | |
| 1504 | /* little fixed point for n * .875 */ |
| 1505 | t2 = (lease * 0x7) >> 3; |
| 1506 | temp_addr.s_addr = packet.yiaddr; |
| 1507 | LOG(LOG_INFO, "Lease of %s obtained, lease time %ld", inet_ntoa(temp_addr), lease); |
| 1508 | start = now; |
| 1509 | timeout = t1 + start; |
| 1510 | requested_ip = packet.yiaddr; |
| 1511 | run_script(&packet, ((state == RENEWING || state == REBINDING) ? "renew" : "bound")); |
| 1512 | |
| 1513 | state = BOUND; |
| 1514 | change_mode(LISTEN_NONE); |
Aaron Lehmann | 1ff58b6 | 2002-08-21 11:21:19 +0000 | [diff] [blame] | 1515 | { |
| 1516 | int pid_fd2; |
| 1517 | if (client_config.quit_after_lease) { |
| 1518 | exit_client(0); |
| 1519 | } else if (!client_config.foreground) { |
| 1520 | pid_fd2 = pidfile_acquire(client_config.pidfile); /* hold lock during fork. */ |
| 1521 | if (daemon(0, 0) == -1) { |
| 1522 | perror("fork"); |
| 1523 | exit_client(1); |
| 1524 | } |
| 1525 | client_config.foreground = 1; /* Do not fork again. */ |
| 1526 | pidfile_write_release(pid_fd2); |
| 1527 | } |
| 1528 | } |
Glenn L McGrath | 8eb0dc1 | 2002-08-21 10:27:58 +0000 | [diff] [blame] | 1529 | } |
| 1530 | else if (*message == DHCPNAK) { |
| 1531 | /* return to init state */ |
| 1532 | LOG(LOG_INFO, "Received DHCP NAK"); |
| 1533 | run_script(&packet, "nak"); |
| 1534 | if (state != REQUESTING) { |
| 1535 | run_script(NULL, "deconfig"); |
| 1536 | } |
| 1537 | state = INIT_SELECTING; |
| 1538 | timeout = now; |
| 1539 | requested_ip = 0; |
| 1540 | packet_num = 0; |
| 1541 | change_mode(LISTEN_RAW); |
| 1542 | sleep(3); /* avoid excessive network traffic */ |
| 1543 | } |
| 1544 | break; |
| 1545 | /* case BOUND, RELEASED: - ignore all packets */ |
| 1546 | } |
| 1547 | } |
| 1548 | else if (retval == -1 && errno == EINTR) { |
| 1549 | /* a signal was caught */ |
| 1550 | |
| 1551 | } else { |
| 1552 | /* An error occured */ |
| 1553 | DEBUG(LOG_ERR, "Error on select"); |
| 1554 | } |
| 1555 | |
| 1556 | } |
| 1557 | return 0; |
| 1558 | } |