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 | */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 21 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 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 | 76729b8 | 2003-12-16 20:44:15 +0000 | [diff] [blame] | 42 | #include "clientpacket.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 43 | #include "options.h" |
| 44 | #include "dhcpc.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 45 | #include "common.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | /* Create a random xid */ |
| 49 | unsigned long random_xid(void) |
| 50 | { |
| 51 | static int initialized; |
| 52 | if (!initialized) { |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 53 | int fd; |
| 54 | unsigned long seed; |
| 55 | |
| 56 | fd = open("/dev/urandom", 0); |
| 57 | if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 58 | LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m"); |
Russ Dill | f5ecd43 | 2002-10-31 19:21:27 +0000 | [diff] [blame] | 59 | seed = time(0); |
| 60 | } |
| 61 | if (fd >= 0) close(fd); |
| 62 | srand(seed); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 63 | initialized++; |
| 64 | } |
| 65 | return rand(); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /* initialize a packet with the proper defaults */ |
| 70 | static void init_packet(struct dhcpMessage *packet, char type) |
| 71 | { |
| 72 | struct vendor { |
| 73 | char vendor, length; |
| 74 | char str[sizeof("udhcp "VERSION)]; |
| 75 | } vendor_id = { DHCP_VENDOR, sizeof("udhcp "VERSION) - 1, "udhcp "VERSION}; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 76 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 77 | init_header(packet, type); |
| 78 | memcpy(packet->chaddr, client_config.arp, 6); |
| 79 | add_option_string(packet->options, client_config.clientid); |
| 80 | if (client_config.hostname) add_option_string(packet->options, client_config.hostname); |
Mike Frysinger | d824853 | 2004-12-06 14:59:45 +0000 | [diff] [blame] | 81 | if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn); |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 82 | add_option_string(packet->options, (uint8_t *) &vendor_id); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 86 | /* Add a parameter request list for stubborn DHCP servers. Pull the data |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 87 | * from the struct in options.c. Don't do bounds checking here because it |
| 88 | * goes towards the head of the packet. */ |
| 89 | static void add_requests(struct dhcpMessage *packet) |
| 90 | { |
| 91 | int end = end_option(packet->options); |
| 92 | int i, len = 0; |
| 93 | |
| 94 | packet->options[end + OPT_CODE] = DHCP_PARAM_REQ; |
Russ Dill | 4a9e34c | 2003-12-15 22:09:36 +0000 | [diff] [blame] | 95 | for (i = 0; dhcp_options[i].code; i++) |
| 96 | if (dhcp_options[i].flags & OPTION_REQ) |
| 97 | packet->options[end + OPT_DATA + len++] = dhcp_options[i].code; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 98 | packet->options[end + OPT_LEN] = len; |
| 99 | packet->options[end + OPT_DATA + len] = DHCP_END; |
| 100 | |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */ |
| 105 | int send_discover(unsigned long xid, unsigned long requested) |
| 106 | { |
| 107 | struct dhcpMessage packet; |
| 108 | |
| 109 | init_packet(&packet, DHCPDISCOVER); |
| 110 | packet.xid = xid; |
| 111 | if (requested) |
| 112 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); |
| 113 | |
| 114 | add_requests(&packet); |
| 115 | LOG(LOG_DEBUG, "Sending discover..."); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 116 | return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 117 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /* Broadcasts a DHCP request message */ |
| 122 | int send_selecting(unsigned long xid, unsigned long server, unsigned long requested) |
| 123 | { |
| 124 | struct dhcpMessage packet; |
| 125 | struct in_addr addr; |
| 126 | |
| 127 | init_packet(&packet, DHCPREQUEST); |
| 128 | packet.xid = xid; |
| 129 | |
| 130 | add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); |
| 131 | add_simple_option(packet.options, DHCP_SERVER_ID, server); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 132 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 133 | add_requests(&packet); |
| 134 | addr.s_addr = requested; |
| 135 | LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 136 | return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 137 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /* Unicasts or broadcasts a DHCP renew message */ |
| 142 | int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr) |
| 143 | { |
| 144 | struct dhcpMessage packet; |
| 145 | int ret = 0; |
| 146 | |
| 147 | init_packet(&packet, DHCPREQUEST); |
| 148 | packet.xid = xid; |
| 149 | packet.ciaddr = ciaddr; |
| 150 | |
| 151 | add_requests(&packet); |
| 152 | LOG(LOG_DEBUG, "Sending renew..."); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 153 | if (server) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 154 | ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); |
| 155 | else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, |
| 156 | SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); |
| 157 | return ret; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 158 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 159 | |
| 160 | |
| 161 | /* Unicasts a DHCP release message */ |
| 162 | int send_release(unsigned long server, unsigned long ciaddr) |
| 163 | { |
| 164 | struct dhcpMessage packet; |
| 165 | |
| 166 | init_packet(&packet, DHCPRELEASE); |
| 167 | packet.xid = random_xid(); |
| 168 | packet.ciaddr = ciaddr; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 169 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 170 | add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr); |
| 171 | add_simple_option(packet.options, DHCP_SERVER_ID, server); |
| 172 | |
| 173 | LOG(LOG_DEBUG, "Sending release..."); |
| 174 | return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /* return -1 on errors that are fatal for the socket, -2 for those that aren't */ |
| 179 | int get_raw_packet(struct dhcpMessage *payload, int fd) |
| 180 | { |
| 181 | int bytes; |
| 182 | struct udp_dhcp_packet packet; |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 183 | uint32_t source, dest; |
| 184 | uint16_t check; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 185 | |
| 186 | memset(&packet, 0, sizeof(struct udp_dhcp_packet)); |
| 187 | bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet)); |
| 188 | if (bytes < 0) { |
| 189 | DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring"); |
| 190 | usleep(500000); /* possible down interface, looping condition */ |
| 191 | return -1; |
| 192 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 193 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 194 | if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) { |
| 195 | DEBUG(LOG_INFO, "message too short, ignoring"); |
| 196 | return -2; |
| 197 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 198 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 199 | if (bytes < ntohs(packet.ip.tot_len)) { |
| 200 | DEBUG(LOG_INFO, "Truncated packet"); |
| 201 | return -2; |
| 202 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 203 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 204 | /* ignore any extra garbage bytes */ |
| 205 | bytes = ntohs(packet.ip.tot_len); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 206 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 207 | /* Make sure its the right packet for us, and that it passes sanity checks */ |
| 208 | if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION || |
| 209 | packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) || |
| 210 | bytes > (int) sizeof(struct udp_dhcp_packet) || |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 211 | ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) { |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 212 | DEBUG(LOG_INFO, "unrelated/bogus packet"); |
| 213 | return -2; |
| 214 | } |
| 215 | |
| 216 | /* check IP checksum */ |
| 217 | check = packet.ip.check; |
| 218 | packet.ip.check = 0; |
| 219 | if (check != checksum(&(packet.ip), sizeof(packet.ip))) { |
| 220 | DEBUG(LOG_INFO, "bad IP header checksum, ignoring"); |
| 221 | return -1; |
| 222 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 223 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 224 | /* verify the UDP checksum by replacing the header with a psuedo header */ |
| 225 | source = packet.ip.saddr; |
| 226 | dest = packet.ip.daddr; |
| 227 | check = packet.udp.check; |
| 228 | packet.udp.check = 0; |
| 229 | memset(&packet.ip, 0, sizeof(packet.ip)); |
| 230 | |
| 231 | packet.ip.protocol = IPPROTO_UDP; |
| 232 | packet.ip.saddr = source; |
| 233 | packet.ip.daddr = dest; |
| 234 | packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */ |
| 235 | if (check && check != checksum(&packet, bytes)) { |
| 236 | DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring"); |
| 237 | return -2; |
| 238 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 239 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 240 | memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp))); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 241 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 242 | if (ntohl(payload->cookie) != DHCP_MAGIC) { |
| 243 | LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring"); |
| 244 | return -2; |
| 245 | } |
| 246 | DEBUG(LOG_INFO, "oooooh!!! got some!"); |
| 247 | return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 248 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 249 | } |