[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[2725] | 2 | /*
|
---|
| 3 | * Packet ops
|
---|
| 4 | *
|
---|
| 5 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
|
---|
| 6 | *
|
---|
| 7 | * Licensed under GPLv2, see file LICENSE in this source tree.
|
---|
| 8 | */
|
---|
[821] | 9 | #include <netinet/in.h>
|
---|
[1765] | 10 | #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
|
---|
[2725] | 11 | # include <netpacket/packet.h>
|
---|
| 12 | # include <net/ethernet.h>
|
---|
[821] | 13 | #else
|
---|
[2725] | 14 | # include <asm/types.h>
|
---|
| 15 | # include <linux/if_packet.h>
|
---|
| 16 | # include <linux/if_ether.h>
|
---|
[821] | 17 | #endif
|
---|
| 18 |
|
---|
| 19 | #include "common.h"
|
---|
| 20 | #include "dhcpd.h"
|
---|
| 21 |
|
---|
[2725] | 22 | void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
|
---|
[821] | 23 | {
|
---|
[2725] | 24 | memset(packet, 0, sizeof(*packet));
|
---|
| 25 | packet->op = BOOTREQUEST; /* if client to a server */
|
---|
[821] | 26 | switch (type) {
|
---|
| 27 | case DHCPOFFER:
|
---|
| 28 | case DHCPACK:
|
---|
| 29 | case DHCPNAK:
|
---|
[2725] | 30 | packet->op = BOOTREPLY; /* if server to client */
|
---|
[821] | 31 | }
|
---|
[2725] | 32 | packet->htype = 1; /* ethernet */
|
---|
| 33 | packet->hlen = 6;
|
---|
[821] | 34 | packet->cookie = htonl(DHCP_MAGIC);
|
---|
[2725] | 35 | if (DHCP_END != 0)
|
---|
| 36 | packet->options[0] = DHCP_END;
|
---|
| 37 | udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
|
---|
[821] | 38 | }
|
---|
| 39 |
|
---|
[2725] | 40 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
|
---|
| 41 | void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
|
---|
| 42 | {
|
---|
| 43 | char buf[sizeof(packet->chaddr)*2 + 1];
|
---|
[821] | 44 |
|
---|
[2725] | 45 | if (dhcp_verbose < 2)
|
---|
| 46 | return;
|
---|
| 47 |
|
---|
| 48 | bb_info_msg(
|
---|
| 49 | //" op %x"
|
---|
| 50 | //" htype %x"
|
---|
| 51 | " hlen %x"
|
---|
| 52 | //" hops %x"
|
---|
| 53 | " xid %x"
|
---|
| 54 | //" secs %x"
|
---|
| 55 | //" flags %x"
|
---|
| 56 | " ciaddr %x"
|
---|
| 57 | " yiaddr %x"
|
---|
| 58 | " siaddr %x"
|
---|
| 59 | " giaddr %x"
|
---|
| 60 | //" chaddr %s"
|
---|
| 61 | //" sname %s"
|
---|
| 62 | //" file %s"
|
---|
| 63 | //" cookie %x"
|
---|
| 64 | //" options %s"
|
---|
| 65 | //, packet->op
|
---|
| 66 | //, packet->htype
|
---|
| 67 | , packet->hlen
|
---|
| 68 | //, packet->hops
|
---|
| 69 | , packet->xid
|
---|
| 70 | //, packet->secs
|
---|
| 71 | //, packet->flags
|
---|
| 72 | , packet->ciaddr
|
---|
| 73 | , packet->yiaddr
|
---|
| 74 | , packet->siaddr_nip
|
---|
| 75 | , packet->gateway_nip
|
---|
| 76 | //, packet->chaddr[16]
|
---|
| 77 | //, packet->sname[64]
|
---|
| 78 | //, packet->file[128]
|
---|
| 79 | //, packet->cookie
|
---|
| 80 | //, packet->options[]
|
---|
| 81 | );
|
---|
| 82 | *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
|
---|
| 83 | bb_info_msg(" chaddr %s", buf);
|
---|
| 84 | }
|
---|
| 85 | #endif
|
---|
| 86 |
|
---|
| 87 | /* Read a packet from socket fd, return -1 on read error, -2 on packet error */
|
---|
| 88 | int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
|
---|
[821] | 89 | {
|
---|
| 90 | int bytes;
|
---|
[1765] | 91 | unsigned char *vendor;
|
---|
[821] | 92 |
|
---|
[1765] | 93 | memset(packet, 0, sizeof(*packet));
|
---|
[2725] | 94 | bytes = safe_read(fd, packet, sizeof(*packet));
|
---|
[821] | 95 | if (bytes < 0) {
|
---|
[2725] | 96 | log1("Packet read error, ignoring");
|
---|
| 97 | return bytes; /* returns -1 */
|
---|
[821] | 98 | }
|
---|
| 99 |
|
---|
[2725] | 100 | if (packet->cookie != htonl(DHCP_MAGIC)) {
|
---|
| 101 | bb_info_msg("Packet with bad magic, ignoring");
|
---|
[821] | 102 | return -2;
|
---|
| 103 | }
|
---|
[2725] | 104 | log1("Received a packet");
|
---|
| 105 | udhcp_dump_packet(packet);
|
---|
[821] | 106 |
|
---|
[1765] | 107 | if (packet->op == BOOTREQUEST) {
|
---|
[2725] | 108 | vendor = udhcp_get_option(packet, DHCP_VENDOR);
|
---|
[1765] | 109 | if (vendor) {
|
---|
| 110 | #if 0
|
---|
[2725] | 111 | static const char broken_vendors[][8] = {
|
---|
| 112 | "MSFT 98",
|
---|
| 113 | ""
|
---|
| 114 | };
|
---|
[1765] | 115 | int i;
|
---|
| 116 | for (i = 0; broken_vendors[i][0]; i++) {
|
---|
[2725] | 117 | if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)strlen(broken_vendors[i])
|
---|
| 118 | && strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - OPT_DATA]) == 0
|
---|
[1765] | 119 | ) {
|
---|
[2725] | 120 | log1("Broken client (%s), forcing broadcast replies",
|
---|
[1765] | 121 | broken_vendors[i]);
|
---|
| 122 | packet->flags |= htons(BROADCAST_FLAG);
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | #else
|
---|
[2725] | 126 | if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)(sizeof("MSFT 98")-1)
|
---|
[1765] | 127 | && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
|
---|
| 128 | ) {
|
---|
[2725] | 129 | log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
|
---|
[821] | 130 | packet->flags |= htons(BROADCAST_FLAG);
|
---|
| 131 | }
|
---|
[1765] | 132 | #endif
|
---|
[821] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | return bytes;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[2725] | 139 | uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
|
---|
[821] | 140 | {
|
---|
| 141 | /* Compute Internet Checksum for "count" bytes
|
---|
[2725] | 142 | * beginning at location "addr".
|
---|
[821] | 143 | */
|
---|
[1765] | 144 | int32_t sum = 0;
|
---|
[821] | 145 | uint16_t *source = (uint16_t *) addr;
|
---|
| 146 |
|
---|
| 147 | while (count > 1) {
|
---|
| 148 | /* This is the inner loop */
|
---|
| 149 | sum += *source++;
|
---|
| 150 | count -= 2;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /* Add left-over byte, if any */
|
---|
| 154 | if (count > 0) {
|
---|
| 155 | /* Make sure that the left-over byte is added correctly both
|
---|
| 156 | * with little and big endian hosts */
|
---|
| 157 | uint16_t tmp = 0;
|
---|
[2725] | 158 | *(uint8_t*)&tmp = *(uint8_t*)source;
|
---|
[821] | 159 | sum += tmp;
|
---|
| 160 | }
|
---|
| 161 | /* Fold 32-bit sum to 16 bits */
|
---|
| 162 | while (sum >> 16)
|
---|
| 163 | sum = (sum & 0xffff) + (sum >> 16);
|
---|
| 164 |
|
---|
| 165 | return ~sum;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[2725] | 168 | /* Construct a ip/udp header for a packet, send packet */
|
---|
| 169 | int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
|
---|
| 170 | uint32_t source_nip, int source_port,
|
---|
| 171 | uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
|
---|
| 172 | int ifindex)
|
---|
[821] | 173 | {
|
---|
[2725] | 174 | struct sockaddr_ll dest_sll;
|
---|
| 175 | struct ip_udp_dhcp_packet packet;
|
---|
| 176 | unsigned padding;
|
---|
[821] | 177 | int fd;
|
---|
[2725] | 178 | int result = -1;
|
---|
| 179 | const char *msg;
|
---|
[821] | 180 |
|
---|
[1765] | 181 | fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
|
---|
| 182 | if (fd < 0) {
|
---|
[2725] | 183 | msg = "socket(%s)";
|
---|
| 184 | goto ret_msg;
|
---|
[821] | 185 | }
|
---|
| 186 |
|
---|
[2725] | 187 | memset(&dest_sll, 0, sizeof(dest_sll));
|
---|
| 188 | memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
|
---|
| 189 | packet.data = *dhcp_pkt; /* struct copy */
|
---|
[821] | 190 |
|
---|
[2725] | 191 | dest_sll.sll_family = AF_PACKET;
|
---|
| 192 | dest_sll.sll_protocol = htons(ETH_P_IP);
|
---|
| 193 | dest_sll.sll_ifindex = ifindex;
|
---|
| 194 | dest_sll.sll_halen = 6;
|
---|
| 195 | memcpy(dest_sll.sll_addr, dest_arp, 6);
|
---|
| 196 |
|
---|
| 197 | if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
|
---|
| 198 | msg = "bind(%s)";
|
---|
| 199 | goto ret_close;
|
---|
[821] | 200 | }
|
---|
| 201 |
|
---|
[2725] | 202 | /* We were sending full-sized DHCP packets (zero padded),
|
---|
| 203 | * but some badly configured servers were seen dropping them.
|
---|
| 204 | * Apparently they drop all DHCP packets >576 *ethernet* octets big,
|
---|
| 205 | * whereas they may only drop packets >576 *IP* octets big
|
---|
| 206 | * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
|
---|
| 207 | *
|
---|
| 208 | * In order to work with those buggy servers,
|
---|
| 209 | * we truncate packets after end option byte.
|
---|
| 210 | */
|
---|
| 211 | padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
|
---|
| 212 |
|
---|
[821] | 213 | packet.ip.protocol = IPPROTO_UDP;
|
---|
[2725] | 214 | packet.ip.saddr = source_nip;
|
---|
| 215 | packet.ip.daddr = dest_nip;
|
---|
[821] | 216 | packet.udp.source = htons(source_port);
|
---|
| 217 | packet.udp.dest = htons(dest_port);
|
---|
[2725] | 218 | /* size, excluding IP header: */
|
---|
| 219 | packet.udp.len = htons(UDP_DHCP_SIZE - padding);
|
---|
| 220 | /* for UDP checksumming, ip.len is set to UDP packet len */
|
---|
[821] | 221 | packet.ip.tot_len = packet.udp.len;
|
---|
[2725] | 222 | packet.udp.check = udhcp_checksum(&packet, IP_UDP_DHCP_SIZE - padding);
|
---|
| 223 | /* but for sending, it is set to IP packet len */
|
---|
| 224 | packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
|
---|
[821] | 225 | packet.ip.ihl = sizeof(packet.ip) >> 2;
|
---|
| 226 | packet.ip.version = IPVERSION;
|
---|
| 227 | packet.ip.ttl = IPDEFTTL;
|
---|
[2725] | 228 | packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
|
---|
[821] | 229 |
|
---|
[2725] | 230 | udhcp_dump_packet(dhcp_pkt);
|
---|
| 231 | result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
|
---|
| 232 | (struct sockaddr *) &dest_sll, sizeof(dest_sll));
|
---|
| 233 | msg = "sendto";
|
---|
| 234 | ret_close:
|
---|
| 235 | close(fd);
|
---|
| 236 | if (result < 0) {
|
---|
| 237 | ret_msg:
|
---|
| 238 | bb_perror_msg(msg, "PACKET");
|
---|
[821] | 239 | }
|
---|
| 240 | return result;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | /* Let the kernel do all the work for packet generation */
|
---|
[2725] | 244 | int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
|
---|
| 245 | uint32_t source_nip, int source_port,
|
---|
| 246 | uint32_t dest_nip, int dest_port)
|
---|
[821] | 247 | {
|
---|
| 248 | struct sockaddr_in client;
|
---|
[2725] | 249 | unsigned padding;
|
---|
| 250 | int fd;
|
---|
| 251 | int result = -1;
|
---|
| 252 | const char *msg;
|
---|
[821] | 253 |
|
---|
[1765] | 254 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
---|
[2725] | 255 | if (fd < 0) {
|
---|
| 256 | msg = "socket(%s)";
|
---|
| 257 | goto ret_msg;
|
---|
| 258 | }
|
---|
[1765] | 259 | setsockopt_reuseaddr(fd);
|
---|
[821] | 260 |
|
---|
| 261 | memset(&client, 0, sizeof(client));
|
---|
| 262 | client.sin_family = AF_INET;
|
---|
| 263 | client.sin_port = htons(source_port);
|
---|
[2725] | 264 | client.sin_addr.s_addr = source_nip;
|
---|
[1765] | 265 | if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
|
---|
[2725] | 266 | msg = "bind(%s)";
|
---|
| 267 | goto ret_close;
|
---|
[821] | 268 | }
|
---|
| 269 |
|
---|
| 270 | memset(&client, 0, sizeof(client));
|
---|
| 271 | client.sin_family = AF_INET;
|
---|
| 272 | client.sin_port = htons(dest_port);
|
---|
[2725] | 273 | client.sin_addr.s_addr = dest_nip;
|
---|
| 274 | if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
|
---|
| 275 | msg = "connect";
|
---|
| 276 | goto ret_close;
|
---|
[821] | 277 | }
|
---|
| 278 |
|
---|
[2725] | 279 | udhcp_dump_packet(dhcp_pkt);
|
---|
| 280 |
|
---|
| 281 | padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
|
---|
| 282 | result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
|
---|
| 283 | msg = "write";
|
---|
| 284 | ret_close:
|
---|
[821] | 285 | close(fd);
|
---|
[2725] | 286 | if (result < 0) {
|
---|
| 287 | ret_msg:
|
---|
| 288 | bb_perror_msg(msg, "UDP");
|
---|
| 289 | }
|
---|
[821] | 290 | return result;
|
---|
| 291 | }
|
---|