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