"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* clientpacket.c |
| 3 | * |
| 4 | * Packet generation and dispatching functions for the DHCP client. |
| 5 | * |
| 6 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 7 | * |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <string.h> |
| 12 | #include <sys/socket.h> |
| 13 | #include <features.h> |
| 14 | #if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION |
| 15 | #include <netpacket/packet.h> |
| 16 | #include <net/ethernet.h> |
| 17 | #else |
| 18 | #include <asm/types.h> |
| 19 | #include <linux/if_packet.h> |
| 20 | #include <linux/if_ether.h> |
| 21 | #endif |
| 22 | #include <stdlib.h> |
| 23 | #include <time.h> |
| 24 | #include <unistd.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <arpa/inet.h> |
| 27 | #include <fcntl.h> |
| 28 | |
| 29 | |
| 30 | #include "dhcpd.h" |
| 31 | #include "clientpacket.h" |
| 32 | #include "options.h" |
| 33 | #include "dhcpc.h" |
| 34 | #include "common.h" |
| 35 | |
| 36 | |
| 37 | /* Create a random xid */ |
| 38 | unsigned long random_xid(void) |
| 39 | { |
| 40 | static int initialized; |
| 41 | if (!initialized) { |
| 42 | int fd; |
| 43 | unsigned long seed; |
| 44 | |
| 45 | fd = open("/dev/urandom", 0); |
| 46 | if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 47 | bb_info_msg("Could not load seed " |
| 48 | "from /dev/urandom: %s", strerror(errno)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 49 | seed = time(0); |
| 50 | } |
| 51 | if (fd >= 0) close(fd); |
| 52 | srand(seed); |
| 53 | initialized++; |
| 54 | } |
| 55 | return rand(); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /* initialize a packet with the proper defaults */ |
| 60 | static void init_packet(struct dhcpMessage *packet, char type) |
| 61 | { |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 62 | udhcp_init_header(packet, type); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 63 | memcpy(packet->chaddr, client_config.arp, 6); |
| 64 | if (client_config.clientid) |
| 65 | add_option_string(packet->options, client_config.clientid); |
| 66 | if (client_config.hostname) add_option_string(packet->options, client_config.hostname); |
| 67 | if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn); |
| 68 | add_option_string(packet->options, client_config.vendorclass); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /* Add a parameter request list for stubborn DHCP servers. Pull the data |
| 73 | * from the struct in options.c. Don't do bounds checking here because it |
| 74 | * goes towards the head of the packet. */ |
| 75 | static void add_requests(struct dhcpMessage *packet) |
| 76 | { |
| 77 | int end = end_option(packet->options); |
| 78 | int i, len = 0; |
| 79 | |
| 80 | packet->options[end + OPT_CODE] = DHCP_PARAM_REQ; |
| 81 | for (i = 0; dhcp_options[i].code; i++) |
| 82 | if (dhcp_options[i].flags & OPTION_REQ) |
| 83 | packet->options[end + OPT_DATA + len++] = dhcp_options[i].code; |
| 84 | packet->options[end + OPT_LEN] = len; |
| 85 | packet->options[end + OPT_DATA + len] = DHCP_END; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ |
| 91 | int send_discover(unsigned long xid, unsigned long requested) |
| 92 | { |
| 93 | struct dhcpMessage packet; |
| 94 | |
| 95 | init_packet(&packet, DHCPDISCOVER); |
| 96 | packet.xid = xid; |
| 97 | if (requested) |
| 98 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); |
| 99 | |
| 100 | add_requests(&packet); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 101 | bb_info_msg("Sending discover..."); |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 102 | return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 103 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* Broadcasts a DHCP request message */ |
| 108 | int send_selecting(unsigned long xid, unsigned long server, unsigned long requested) |
| 109 | { |
| 110 | struct dhcpMessage packet; |
| 111 | struct in_addr addr; |
| 112 | |
| 113 | init_packet(&packet, DHCPREQUEST); |
| 114 | packet.xid = xid; |
| 115 | |
| 116 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); |
| 117 | add_simple_option(packet.options, DHCP_SERVER_ID, server); |
| 118 | |
| 119 | add_requests(&packet); |
| 120 | addr.s_addr = requested; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 121 | bb_info_msg("Sending select for %s...", inet_ntoa(addr)); |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 122 | return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 123 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /* Unicasts or broadcasts a DHCP renew message */ |
| 128 | int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr) |
| 129 | { |
| 130 | struct dhcpMessage packet; |
| 131 | int ret = 0; |
| 132 | |
| 133 | init_packet(&packet, DHCPREQUEST); |
| 134 | packet.xid = xid; |
| 135 | packet.ciaddr = ciaddr; |
| 136 | |
| 137 | add_requests(&packet); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 138 | bb_info_msg("Sending renew..."); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 139 | if (server) |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 140 | ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); |
| 141 | else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 142 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* Unicasts a DHCP release message */ |
| 148 | int send_release(unsigned long server, unsigned long ciaddr) |
| 149 | { |
| 150 | struct dhcpMessage packet; |
| 151 | |
| 152 | init_packet(&packet, DHCPRELEASE); |
| 153 | packet.xid = random_xid(); |
| 154 | packet.ciaddr = ciaddr; |
| 155 | |
| 156 | add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr); |
| 157 | add_simple_option(packet.options, DHCP_SERVER_ID, server); |
| 158 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 159 | bb_info_msg("Sending release..."); |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 160 | return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
| 164 | /* return -1 on errors that are fatal for the socket, -2 for those that aren't */ |
| 165 | int get_raw_packet(struct dhcpMessage *payload, int fd) |
| 166 | { |
| 167 | int bytes; |
| 168 | struct udp_dhcp_packet packet; |
| 169 | uint32_t source, dest; |
| 170 | uint16_t check; |
| 171 | |
| 172 | memset(&packet, 0, sizeof(struct udp_dhcp_packet)); |
| 173 | bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet)); |
| 174 | if (bytes < 0) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 175 | DEBUG("Couldn't read on raw listening socket - ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 176 | usleep(500000); /* possible down interface, looping condition */ |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 181 | DEBUG("Message too short, ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 182 | return -2; |
| 183 | } |
| 184 | |
| 185 | if (bytes < ntohs(packet.ip.tot_len)) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 186 | DEBUG("Truncated packet"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 187 | return -2; |
| 188 | } |
| 189 | |
| 190 | /* ignore any extra garbage bytes */ |
| 191 | bytes = ntohs(packet.ip.tot_len); |
| 192 | |
| 193 | /* Make sure its the right packet for us, and that it passes sanity checks */ |
| 194 | if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION || |
| 195 | packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) || |
| 196 | bytes > (int) sizeof(struct udp_dhcp_packet) || |
| 197 | ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 198 | DEBUG("Unrelated/bogus packet"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 199 | return -2; |
| 200 | } |
| 201 | |
| 202 | /* check IP checksum */ |
| 203 | check = packet.ip.check; |
| 204 | packet.ip.check = 0; |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 205 | if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 206 | DEBUG("bad IP header checksum, ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | /* verify the UDP checksum by replacing the header with a psuedo header */ |
| 211 | source = packet.ip.saddr; |
| 212 | dest = packet.ip.daddr; |
| 213 | check = packet.udp.check; |
| 214 | packet.udp.check = 0; |
| 215 | memset(&packet.ip, 0, sizeof(packet.ip)); |
| 216 | |
| 217 | packet.ip.protocol = IPPROTO_UDP; |
| 218 | packet.ip.saddr = source; |
| 219 | packet.ip.daddr = dest; |
| 220 | packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */ |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 221 | if (check && check != udhcp_checksum(&packet, bytes)) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 222 | bb_error_msg("Packet with bad UDP checksum received, ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 223 | return -2; |
| 224 | } |
| 225 | |
| 226 | memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp))); |
| 227 | |
| 228 | if (ntohl(payload->cookie) != DHCP_MAGIC) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 229 | bb_error_msg("Received bogus message (bad magic) - ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 230 | return -2; |
| 231 | } |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 232 | DEBUG("oooooh!!! got some!"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 233 | return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); |
| 234 | |
| 235 | } |