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