blob: 1bfe12041ccd5c9ec6fcb02926e3dfd9eaad529c [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00002/*
Denys Vlasenko385b4562010-03-26 10:09:34 +01003 * Packet ops
4 *
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
6 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 */
Mike Frysinger7031f622006-05-08 03:20:50 +00009#include <netinet/in.h>
Denis Vlasenko83e5d6f2006-12-18 21:49:06 +000010#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
Denys Vlasenko385b4562010-03-26 10:09:34 +010011# include <netpacket/packet.h>
12# include <net/ethernet.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000013#else
Denys Vlasenko385b4562010-03-26 10:09:34 +010014# include <asm/types.h>
15# include <linux/if_packet.h>
16# include <linux/if_ether.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000017#endif
Mike Frysinger7031f622006-05-08 03:20:50 +000018
19#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000020#include "dhcpd.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000021
Denys Vlasenko31af3d52009-06-17 11:57:09 +020022void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
Mike Frysinger7031f622006-05-08 03:20:50 +000023{
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010024 memset(packet, 0, sizeof(*packet));
Denis Vlasenko739e30f2008-09-26 23:45:20 +000025 packet->op = BOOTREQUEST; /* if client to a server */
Mike Frysinger7031f622006-05-08 03:20:50 +000026 switch (type) {
Mike Frysinger7031f622006-05-08 03:20:50 +000027 case DHCPOFFER:
28 case DHCPACK:
29 case DHCPNAK:
Denis Vlasenko739e30f2008-09-26 23:45:20 +000030 packet->op = BOOTREPLY; /* if server to client */
Mike Frysinger7031f622006-05-08 03:20:50 +000031 }
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010032 packet->htype = 1; /* ethernet */
33 packet->hlen = 6;
Mike Frysinger7031f622006-05-08 03:20:50 +000034 packet->cookie = htonl(DHCP_MAGIC);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +010035 if (DHCP_END != 0)
36 packet->options[0] = DHCP_END;
Denys Vlasenko7724c762010-03-26 09:32:09 +010037 udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
Mike Frysinger7031f622006-05-08 03:20:50 +000038}
39
Denys Vlasenko7a76eba2009-06-19 13:51:29 +020040#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
Denys Vlasenko31af3d52009-06-17 11:57:09 +020041void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020042{
43 char buf[sizeof(packet->chaddr)*2 + 1];
Mike Frysinger7031f622006-05-08 03:20:50 +000044
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020045 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"
Denys Vlasenko9038d6f2009-07-15 20:02:19 +020057 " yiaddr %x"
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020058 " 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 );
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020082 *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020083 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 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020088int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
Mike Frysinger7031f622006-05-08 03:20:50 +000089{
Mike Frysinger7031f622006-05-08 03:20:50 +000090 int bytes;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000091 unsigned char *vendor;
Mike Frysinger7031f622006-05-08 03:20:50 +000092
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000093 memset(packet, 0, sizeof(*packet));
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000094 bytes = safe_read(fd, packet, sizeof(*packet));
Mike Frysinger7031f622006-05-08 03:20:50 +000095 if (bytes < 0) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020096 log1("Packet read error, ignoring");
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000097 return bytes; /* returns -1 */
Mike Frysinger7031f622006-05-08 03:20:50 +000098 }
99
Denis Vlasenko6884f662007-11-23 00:08:54 +0000100 if (packet->cookie != htonl(DHCP_MAGIC)) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200101 bb_info_msg("Packet with bad magic, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000102 return -2;
103 }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200104 log1("Received a packet");
105 udhcp_dump_packet(packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000106
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000107 if (packet->op == BOOTREQUEST) {
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100108 vendor = udhcp_get_option(packet, DHCP_VENDOR);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000109 if (vendor) {
110#if 0
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000111 static const char broken_vendors[][8] = {
112 "MSFT 98",
113 ""
114 };
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000115 int i;
116 for (i = 0; broken_vendors[i][0]; i++) {
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100117 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
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000119 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200120 log1("Broken client (%s), forcing broadcast replies",
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000121 broken_vendors[i]);
122 packet->flags |= htons(BROADCAST_FLAG);
123 }
124 }
125#else
Denys Vlasenko37a658c2010-03-23 15:43:08 +0100126 if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)(sizeof("MSFT 98")-1)
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000127 && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000128 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200129 log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
Mike Frysinger7031f622006-05-08 03:20:50 +0000130 packet->flags |= htons(BROADCAST_FLAG);
131 }
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000132#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000133 }
134 }
135
136 return bytes;
137}
138
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000139uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
Mike Frysinger7031f622006-05-08 03:20:50 +0000140{
141 /* Compute Internet Checksum for "count" bytes
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200142 * beginning at location "addr".
Mike Frysinger7031f622006-05-08 03:20:50 +0000143 */
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000144 int32_t sum = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000145 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;
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000158 *(uint8_t*)&tmp = *(uint8_t*)source;
Mike Frysinger7031f622006-05-08 03:20:50 +0000159 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
Denis Vlasenko61987922007-12-21 16:32:30 +0000168/* Construct a ip/udp header for a packet, send packet */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200169int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000170 uint32_t source_ip, int source_port,
Denis Vlasenkoc321b512008-09-26 16:29:12 +0000171 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp,
172 int ifindex)
Mike Frysinger7031f622006-05-08 03:20:50 +0000173{
Mike Frysinger7031f622006-05-08 03:20:50 +0000174 struct sockaddr_ll dest;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200175 struct ip_udp_dhcp_packet packet;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000176 int fd;
177 int result = -1;
178 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000179
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000180 enum {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200181 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
182 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE - offsetof(struct ip_udp_dhcp_packet, udp),
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000183 };
184
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000185 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
186 if (fd < 0) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000187 msg = "socket(%s)";
188 goto ret_msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000189 }
190
191 memset(&dest, 0, sizeof(dest));
192 memset(&packet, 0, sizeof(packet));
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200193 packet.data = *dhcp_pkt; /* struct copy */
Mike Frysinger7031f622006-05-08 03:20:50 +0000194
195 dest.sll_family = AF_PACKET;
196 dest.sll_protocol = htons(ETH_P_IP);
197 dest.sll_ifindex = ifindex;
198 dest.sll_halen = 6;
199 memcpy(dest.sll_addr, dest_arp, 6);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000200 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
201 msg = "bind(%s)";
202 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000203 }
204
205 packet.ip.protocol = IPPROTO_UDP;
206 packet.ip.saddr = source_ip;
207 packet.ip.daddr = dest_ip;
208 packet.udp.source = htons(source_port);
209 packet.udp.dest = htons(dest_port);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000210 /* size, excluding IP header: */
211 packet.udp.len = htons(UPD_DHCP_SIZE);
212 /* for UDP checksumming, ip.len is set to UDP packet len */
Mike Frysinger7031f622006-05-08 03:20:50 +0000213 packet.ip.tot_len = packet.udp.len;
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000214 packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
215 /* but for sending, it is set to IP packet len */
216 packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000217 packet.ip.ihl = sizeof(packet.ip) >> 2;
218 packet.ip.version = IPVERSION;
219 packet.ip.ttl = IPDEFTTL;
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000220 packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
Mike Frysinger7031f622006-05-08 03:20:50 +0000221
Denis Vlasenko61987922007-12-21 16:32:30 +0000222 /* Currently we send full-sized DHCP packets (zero padded).
223 * If you need to change this: last byte of the packet is
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100224 * packet.data.options[udhcp_end_option(packet.data.options)]
Denis Vlasenko61987922007-12-21 16:32:30 +0000225 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200226 udhcp_dump_packet(dhcp_pkt);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000227 result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
228 (struct sockaddr *) &dest, sizeof(dest));
229 msg = "sendto";
230 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000231 close(fd);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100232 /* FIXME: and if result >= 0 but != IP_UPD_DHCP_SIZE? */
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000233 if (result < 0) {
234 ret_msg:
235 bb_perror_msg(msg, "PACKET");
236 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000237 return result;
238}
239
Mike Frysinger7031f622006-05-08 03:20:50 +0000240/* Let the kernel do all the work for packet generation */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200241int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000242 uint32_t source_ip, int source_port,
243 uint32_t dest_ip, int dest_port)
Mike Frysinger7031f622006-05-08 03:20:50 +0000244{
Mike Frysinger7031f622006-05-08 03:20:50 +0000245 struct sockaddr_in client;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000246 int fd;
247 int result = -1;
248 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000249
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000250 enum {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200251 DHCP_SIZE = sizeof(struct dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000252 };
253
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000254 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000255 if (fd < 0) {
256 msg = "socket(%s)";
257 goto ret_msg;
258 }
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000259 setsockopt_reuseaddr(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000260
261 memset(&client, 0, sizeof(client));
262 client.sin_family = AF_INET;
263 client.sin_port = htons(source_port);
264 client.sin_addr.s_addr = source_ip;
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000265 if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000266 msg = "bind(%s)";
267 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000268 }
269
270 memset(&client, 0, sizeof(client));
271 client.sin_family = AF_INET;
272 client.sin_port = htons(dest_port);
273 client.sin_addr.s_addr = dest_ip;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000274 if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
275 msg = "connect";
276 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000277 }
278
Denis Vlasenko61987922007-12-21 16:32:30 +0000279 /* Currently we send full-sized DHCP packets (see above) */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200280 udhcp_dump_packet(dhcp_pkt);
281 result = safe_write(fd, dhcp_pkt, DHCP_SIZE);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000282 msg = "write";
283 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000284 close(fd);
Denys Vlasenkoc7dc79e2010-03-21 06:15:28 +0100285 /* FIXME: and if result >= 0 but != DHCP_SIZE? */
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000286 if (result < 0) {
287 ret_msg:
288 bb_perror_msg(msg, "UDP");
289 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000290 return result;
291}