"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 3 | * Packet ops |
| 4 | * |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 8 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 10 | #include "dhcpd.h" |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 11 | #include <netinet/in.h> |
| 12 | #include <netinet/if_ether.h> |
| 13 | #include <netpacket/packet.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 14 | |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 15 | void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 16 | { |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 17 | memset(packet, 0, sizeof(*packet)); |
Denis Vlasenko | 739e30f | 2008-09-26 23:45:20 +0000 | [diff] [blame] | 18 | packet->op = BOOTREQUEST; /* if client to a server */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 19 | switch (type) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 20 | case DHCPOFFER: |
| 21 | case DHCPACK: |
| 22 | case DHCPNAK: |
Denis Vlasenko | 739e30f | 2008-09-26 23:45:20 +0000 | [diff] [blame] | 23 | packet->op = BOOTREPLY; /* if server to client */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 24 | } |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 25 | packet->htype = 1; /* ethernet */ |
| 26 | packet->hlen = 6; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 27 | packet->cookie = htonl(DHCP_MAGIC); |
Denys Vlasenko | c7dc79e | 2010-03-21 06:15:28 +0100 | [diff] [blame] | 28 | if (DHCP_END != 0) |
| 29 | packet->options[0] = DHCP_END; |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 30 | udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Denys Vlasenko | 7a76eba | 2009-06-19 13:51:29 +0200 | [diff] [blame] | 33 | #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 34 | void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet) |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 35 | { |
| 36 | char buf[sizeof(packet->chaddr)*2 + 1]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 37 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 38 | if (dhcp_verbose < 2) |
| 39 | return; |
| 40 | |
| 41 | bb_info_msg( |
| 42 | //" op %x" |
| 43 | //" htype %x" |
| 44 | " hlen %x" |
| 45 | //" hops %x" |
| 46 | " xid %x" |
| 47 | //" secs %x" |
| 48 | //" flags %x" |
| 49 | " ciaddr %x" |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 50 | " yiaddr %x" |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 51 | " siaddr %x" |
| 52 | " giaddr %x" |
| 53 | //" chaddr %s" |
| 54 | //" sname %s" |
| 55 | //" file %s" |
| 56 | //" cookie %x" |
| 57 | //" options %s" |
| 58 | //, packet->op |
| 59 | //, packet->htype |
| 60 | , packet->hlen |
| 61 | //, packet->hops |
| 62 | , packet->xid |
| 63 | //, packet->secs |
| 64 | //, packet->flags |
| 65 | , packet->ciaddr |
| 66 | , packet->yiaddr |
| 67 | , packet->siaddr_nip |
| 68 | , packet->gateway_nip |
| 69 | //, packet->chaddr[16] |
| 70 | //, packet->sname[64] |
| 71 | //, packet->file[128] |
| 72 | //, packet->cookie |
| 73 | //, packet->options[] |
| 74 | ); |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 75 | *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0'; |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 76 | bb_info_msg(" chaddr %s", buf); |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | /* Read a packet from socket fd, return -1 on read error, -2 on packet error */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 81 | int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 82 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 83 | int bytes; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 84 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 85 | memset(packet, 0, sizeof(*packet)); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 86 | bytes = safe_read(fd, packet, sizeof(*packet)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 87 | if (bytes < 0) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 88 | log1("Packet read error, ignoring"); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 89 | return bytes; /* returns -1 */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Denys Vlasenko | 3b46fcb | 2011-11-05 01:26:18 +0100 | [diff] [blame^] | 92 | if (bytes < offsetof(struct dhcp_packet, options) |
| 93 | || packet->cookie != htonl(DHCP_MAGIC) |
| 94 | ) { |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 95 | bb_info_msg("Packet with bad magic, ignoring"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 96 | return -2; |
| 97 | } |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 98 | log1("Received a packet"); |
| 99 | udhcp_dump_packet(packet); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 100 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 101 | return bytes; |
| 102 | } |
| 103 | |
Denis Vlasenko | 6198792 | 2007-12-21 16:32:30 +0000 | [diff] [blame] | 104 | /* Construct a ip/udp header for a packet, send packet */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 105 | int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 106 | uint32_t source_nip, int source_port, |
| 107 | uint32_t dest_nip, int dest_port, const uint8_t *dest_arp, |
Denis Vlasenko | c321b51 | 2008-09-26 16:29:12 +0000 | [diff] [blame] | 108 | int ifindex) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 109 | { |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 110 | struct sockaddr_ll dest_sll; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 111 | struct ip_udp_dhcp_packet packet; |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 112 | unsigned padding; |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 113 | int fd; |
| 114 | int result = -1; |
| 115 | const char *msg; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 117 | fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); |
| 118 | if (fd < 0) { |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 119 | msg = "socket(%s)"; |
| 120 | goto ret_msg; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 123 | memset(&dest_sll, 0, sizeof(dest_sll)); |
| 124 | memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data)); |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 125 | packet.data = *dhcp_pkt; /* struct copy */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 126 | |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 127 | dest_sll.sll_family = AF_PACKET; |
| 128 | dest_sll.sll_protocol = htons(ETH_P_IP); |
| 129 | dest_sll.sll_ifindex = ifindex; |
| 130 | dest_sll.sll_halen = 6; |
| 131 | memcpy(dest_sll.sll_addr, dest_arp, 6); |
| 132 | |
| 133 | if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) { |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 134 | msg = "bind(%s)"; |
| 135 | goto ret_close; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 138 | /* We were sending full-sized DHCP packets (zero padded), |
| 139 | * but some badly configured servers were seen dropping them. |
| 140 | * Apparently they drop all DHCP packets >576 *ethernet* octets big, |
| 141 | * whereas they may only drop packets >576 *IP* octets big |
| 142 | * (which for typical Ethernet II means 590 octets: 6+6+2 + 576). |
| 143 | * |
| 144 | * In order to work with those buggy servers, |
| 145 | * we truncate packets after end option byte. |
| 146 | */ |
| 147 | padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options); |
| 148 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 149 | packet.ip.protocol = IPPROTO_UDP; |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 150 | packet.ip.saddr = source_nip; |
| 151 | packet.ip.daddr = dest_nip; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 152 | packet.udp.source = htons(source_port); |
| 153 | packet.udp.dest = htons(dest_port); |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 154 | /* size, excluding IP header: */ |
Denys Vlasenko | 2c3b71a | 2010-10-20 18:04:36 +0200 | [diff] [blame] | 155 | packet.udp.len = htons(UDP_DHCP_SIZE - padding); |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 156 | /* for UDP checksumming, ip.len is set to UDP packet len */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 157 | packet.ip.tot_len = packet.udp.len; |
Baruch Siach | e8f3633 | 2011-09-07 17:52:37 +0200 | [diff] [blame] | 158 | packet.udp.check = inet_cksum((uint16_t *)&packet, |
| 159 | IP_UDP_DHCP_SIZE - padding); |
Denis Vlasenko | fff145d | 2007-12-20 21:11:38 +0000 | [diff] [blame] | 160 | /* but for sending, it is set to IP packet len */ |
Denys Vlasenko | 2c3b71a | 2010-10-20 18:04:36 +0200 | [diff] [blame] | 161 | packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 162 | packet.ip.ihl = sizeof(packet.ip) >> 2; |
| 163 | packet.ip.version = IPVERSION; |
| 164 | packet.ip.ttl = IPDEFTTL; |
Baruch Siach | e8f3633 | 2011-09-07 17:52:37 +0200 | [diff] [blame] | 165 | packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 166 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 167 | udhcp_dump_packet(dhcp_pkt); |
Denys Vlasenko | 2c3b71a | 2010-10-20 18:04:36 +0200 | [diff] [blame] | 168 | result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0, |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 169 | (struct sockaddr *) &dest_sll, sizeof(dest_sll)); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 170 | msg = "sendto"; |
| 171 | ret_close: |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 172 | close(fd); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 173 | if (result < 0) { |
| 174 | ret_msg: |
| 175 | bb_perror_msg(msg, "PACKET"); |
| 176 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 177 | return result; |
| 178 | } |
| 179 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 180 | /* Let the kernel do all the work for packet generation */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 181 | int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt, |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 182 | uint32_t source_nip, int source_port, |
| 183 | uint32_t dest_nip, int dest_port) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 184 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 185 | struct sockaddr_in client; |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 186 | unsigned padding; |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 187 | int fd; |
| 188 | int result = -1; |
| 189 | const char *msg; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 190 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 191 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 192 | if (fd < 0) { |
| 193 | msg = "socket(%s)"; |
| 194 | goto ret_msg; |
| 195 | } |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 196 | setsockopt_reuseaddr(fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 197 | |
| 198 | memset(&client, 0, sizeof(client)); |
| 199 | client.sin_family = AF_INET; |
| 200 | client.sin_port = htons(source_port); |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 201 | client.sin_addr.s_addr = source_nip; |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 202 | if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) { |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 203 | msg = "bind(%s)"; |
| 204 | goto ret_close; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | memset(&client, 0, sizeof(client)); |
| 208 | client.sin_family = AF_INET; |
| 209 | client.sin_port = htons(dest_port); |
Denys Vlasenko | b7d19cc | 2010-05-30 23:41:23 +0200 | [diff] [blame] | 210 | client.sin_addr.s_addr = dest_nip; |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 211 | if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) { |
| 212 | msg = "connect"; |
| 213 | goto ret_close; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 216 | udhcp_dump_packet(dhcp_pkt); |
Denys Vlasenko | b8b72f0 | 2010-05-31 00:45:09 +0200 | [diff] [blame] | 217 | |
| 218 | padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options); |
| 219 | result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 220 | msg = "write"; |
| 221 | ret_close: |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 222 | close(fd); |
Denis Vlasenko | 8e5b6f5 | 2007-12-24 17:32:22 +0000 | [diff] [blame] | 223 | if (result < 0) { |
| 224 | ret_msg: |
| 225 | bb_perror_msg(msg, "UDP"); |
| 226 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 227 | return result; |
| 228 | } |