Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | /* clientpacket.c |
| 2 | * |
| 3 | * Packet generation and dispatching functions for the DHCP client. |
| 4 | * |
| 5 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <features.h> |
| 25 | #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1 |
| 26 | #include <netpacket/packet.h> |
| 27 | #include <net/ethernet.h> |
| 28 | #else |
| 29 | #include <asm/types.h> |
| 30 | #include <linux/if_packet.h> |
| 31 | #include <linux/if_ether.h> |
| 32 | #endif |
| 33 | #include <stdlib.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
| 36 | #include <netinet/in.h> |
| 37 | #include <arpa/inet.h> |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 38 | #include <fcntl.h> |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | #include "dhcpd.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 42 | #include "options.h" |
| 43 | #include "dhcpc.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 44 | #include "common.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | /* Create a random xid */ |
| 48 | unsigned long random_xid(void) |
| 49 | { |
| 50 | static int initialized; |
| 51 | if (!initialized) { |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 52 | int fd; |
| 53 | unsigned long seed; |
| 54 | |
| 55 | fd = open("/dev/urandom", 0); |
| 56 | if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 57 | LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m"); |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 58 | seed = time(0); |
| 59 | } |
| 60 | if (fd >= 0) close(fd); |
| 61 | srand(seed); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 62 | initialized++; |
| 63 | } |
| 64 | return rand(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /* initialize a packet with the proper defaults */ |
| 69 | static void init_packet(struct dhcpMessage *packet, char type) |
| 70 | { |
| 71 | struct vendor { |
| 72 | char vendor, length; |
| 73 | char str[sizeof("udhcp "VERSION)]; |
| 74 | } vendor_id = { DHCP_VENDOR, sizeof("udhcp "VERSION) - 1, "udhcp "VERSION}; |
| 75 | |
| 76 | init_header(packet, type); |
| 77 | memcpy(packet->chaddr, client_config.arp, 6); |
| 78 | add_option_string(packet->options, client_config.clientid); |
| 79 | if (client_config.hostname) add_option_string(packet->options, client_config.hostname); |
| 80 | add_option_string(packet->options, (unsigned char *) &vendor_id); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* Add a paramater request list for stubborn DHCP servers. Pull the data |
| 85 | * from the struct in options.c. Don't do bounds checking here because it |
| 86 | * goes towards the head of the packet. */ |
| 87 | static void add_requests(struct dhcpMessage *packet) |
| 88 | { |
| 89 | int end = end_option(packet->options); |
| 90 | int i, len = 0; |
| 91 | |
| 92 | packet->options[end + OPT_CODE] = DHCP_PARAM_REQ; |
| 93 | for (i = 0; options[i].code; i++) |
| 94 | if (options[i].flags & OPTION_REQ) |
| 95 | packet->options[end + OPT_DATA + len++] = options[i].code; |
| 96 | packet->options[end + OPT_LEN] = len; |
| 97 | packet->options[end + OPT_DATA + len] = DHCP_END; |
| 98 | |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ |
| 103 | int send_discover(unsigned long xid, unsigned long requested) |
| 104 | { |
| 105 | struct dhcpMessage packet; |
| 106 | |
| 107 | init_packet(&packet, DHCPDISCOVER); |
| 108 | packet.xid = xid; |
| 109 | if (requested) |
| 110 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); |
| 111 | |
| 112 | add_requests(&packet); |
| 113 | LOG(LOG_DEBUG, "Sending discover..."); |
| 114 | return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
| 115 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /* Broadcasts a DHCP request message */ |
| 120 | int send_selecting(unsigned long xid, unsigned long server, unsigned long requested) |
| 121 | { |
| 122 | struct dhcpMessage packet; |
| 123 | struct in_addr addr; |
| 124 | |
| 125 | init_packet(&packet, DHCPREQUEST); |
| 126 | packet.xid = xid; |
| 127 | |
| 128 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); |
| 129 | add_simple_option(packet.options, DHCP_SERVER_ID, server); |
| 130 | |
| 131 | add_requests(&packet); |
| 132 | addr.s_addr = requested; |
| 133 | LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr)); |
| 134 | return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
| 135 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /* Unicasts or broadcasts a DHCP renew message */ |
| 140 | int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr) |
| 141 | { |
| 142 | struct dhcpMessage packet; |
| 143 | int ret = 0; |
| 144 | |
| 145 | init_packet(&packet, DHCPREQUEST); |
| 146 | packet.xid = xid; |
| 147 | packet.ciaddr = ciaddr; |
| 148 | |
| 149 | add_requests(&packet); |
| 150 | LOG(LOG_DEBUG, "Sending renew..."); |
| 151 | if (server) |
| 152 | ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); |
| 153 | else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
| 154 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /* Unicasts a DHCP release message */ |
| 160 | int send_release(unsigned long server, unsigned long ciaddr) |
| 161 | { |
| 162 | struct dhcpMessage packet; |
| 163 | |
| 164 | init_packet(&packet, DHCPRELEASE); |
| 165 | packet.xid = random_xid(); |
| 166 | packet.ciaddr = ciaddr; |
| 167 | |
| 168 | add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr); |
| 169 | add_simple_option(packet.options, DHCP_SERVER_ID, server); |
| 170 | |
| 171 | LOG(LOG_DEBUG, "Sending release..."); |
| 172 | return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* return -1 on errors that are fatal for the socket, -2 for those that aren't */ |
| 177 | int get_raw_packet(struct dhcpMessage *payload, int fd) |
| 178 | { |
| 179 | int bytes; |
| 180 | struct udp_dhcp_packet packet; |
| 181 | u_int32_t source, dest; |
| 182 | u_int16_t check; |
| 183 | |
| 184 | memset(&packet, 0, sizeof(struct udp_dhcp_packet)); |
| 185 | bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet)); |
| 186 | if (bytes < 0) { |
| 187 | DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring"); |
| 188 | usleep(500000); /* possible down interface, looping condition */ |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) { |
| 193 | DEBUG(LOG_INFO, "message too short, ignoring"); |
| 194 | return -2; |
| 195 | } |
| 196 | |
| 197 | if (bytes < ntohs(packet.ip.tot_len)) { |
| 198 | DEBUG(LOG_INFO, "Truncated packet"); |
| 199 | return -2; |
| 200 | } |
| 201 | |
| 202 | /* ignore any extra garbage bytes */ |
| 203 | bytes = ntohs(packet.ip.tot_len); |
| 204 | |
| 205 | /* Make sure its the right packet for us, and that it passes sanity checks */ |
| 206 | if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION || |
| 207 | packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) || |
| 208 | bytes > (int) sizeof(struct udp_dhcp_packet) || |
| 209 | ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) { |
| 210 | DEBUG(LOG_INFO, "unrelated/bogus packet"); |
| 211 | return -2; |
| 212 | } |
| 213 | |
| 214 | /* check IP checksum */ |
| 215 | check = packet.ip.check; |
| 216 | packet.ip.check = 0; |
| 217 | if (check != checksum(&(packet.ip), sizeof(packet.ip))) { |
| 218 | DEBUG(LOG_INFO, "bad IP header checksum, ignoring"); |
| 219 | return -1; |
| 220 | } |
| 221 | |
| 222 | /* verify the UDP checksum by replacing the header with a psuedo header */ |
| 223 | source = packet.ip.saddr; |
| 224 | dest = packet.ip.daddr; |
| 225 | check = packet.udp.check; |
| 226 | packet.udp.check = 0; |
| 227 | memset(&packet.ip, 0, sizeof(packet.ip)); |
| 228 | |
| 229 | packet.ip.protocol = IPPROTO_UDP; |
| 230 | packet.ip.saddr = source; |
| 231 | packet.ip.daddr = dest; |
| 232 | packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */ |
| 233 | if (check && check != checksum(&packet, bytes)) { |
| 234 | DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring"); |
| 235 | return -2; |
| 236 | } |
| 237 | |
| 238 | memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp))); |
| 239 | |
| 240 | if (ntohl(payload->cookie) != DHCP_MAGIC) { |
| 241 | LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring"); |
| 242 | return -2; |
| 243 | } |
| 244 | DEBUG(LOG_INFO, "oooooh!!! got some!"); |
| 245 | return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); |
| 246 | |
| 247 | } |