blob: 90410cbc06ff19500aeb55634b19b78d81f7f7f4 [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/*
3 * packet.c -- packet ops
4 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
5 *
6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
7 */
Mike Frysinger7031f622006-05-08 03:20:50 +00008#include <netinet/in.h>
Denis Vlasenko83e5d6f2006-12-18 21:49:06 +00009#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
Mike Frysinger7031f622006-05-08 03:20:50 +000010#include <netpacket/packet.h>
11#include <net/ethernet.h>
12#else
13#include <asm/types.h>
14#include <linux/if_packet.h>
15#include <linux/if_ether.h>
16#endif
Mike Frysinger7031f622006-05-08 03:20:50 +000017
18#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000019#include "dhcpd.h"
20#include "options.h"
21
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 Vlasenko31af3d52009-06-17 11:57:09 +020024 memset(packet, 0, sizeof(struct dhcp_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 }
32 packet->htype = ETH_10MB;
33 packet->hlen = ETH_10MB_LEN;
34 packet->cookie = htonl(DHCP_MAGIC);
35 packet->options[0] = DHCP_END;
36 add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
37}
38
Denys Vlasenko7a76eba2009-06-19 13:51:29 +020039#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
Denys Vlasenko31af3d52009-06-17 11:57:09 +020040void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020041{
42 char buf[sizeof(packet->chaddr)*2 + 1];
Mike Frysinger7031f622006-05-08 03:20:50 +000043
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020044 if (dhcp_verbose < 2)
45 return;
46
47 bb_info_msg(
48 //" op %x"
49 //" htype %x"
50 " hlen %x"
51 //" hops %x"
52 " xid %x"
53 //" secs %x"
54 //" flags %x"
55 " ciaddr %x"
Denys Vlasenko9038d6f2009-07-15 20:02:19 +020056 " yiaddr %x"
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020057 " siaddr %x"
58 " giaddr %x"
59 //" chaddr %s"
60 //" sname %s"
61 //" file %s"
62 //" cookie %x"
63 //" options %s"
64 //, packet->op
65 //, packet->htype
66 , packet->hlen
67 //, packet->hops
68 , packet->xid
69 //, packet->secs
70 //, packet->flags
71 , packet->ciaddr
72 , packet->yiaddr
73 , packet->siaddr_nip
74 , packet->gateway_nip
75 //, packet->chaddr[16]
76 //, packet->sname[64]
77 //, packet->file[128]
78 //, packet->cookie
79 //, packet->options[]
80 );
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020081 *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020082 bb_info_msg(" chaddr %s", buf);
83}
84#endif
85
86/* Read a packet from socket fd, return -1 on read error, -2 on packet error */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020087int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
Mike Frysinger7031f622006-05-08 03:20:50 +000088{
Mike Frysinger7031f622006-05-08 03:20:50 +000089 int bytes;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000090 unsigned char *vendor;
Mike Frysinger7031f622006-05-08 03:20:50 +000091
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000092 memset(packet, 0, sizeof(*packet));
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000093 bytes = safe_read(fd, packet, sizeof(*packet));
Mike Frysinger7031f622006-05-08 03:20:50 +000094 if (bytes < 0) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020095 log1("Packet read error, ignoring");
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000096 return bytes; /* returns -1 */
Mike Frysinger7031f622006-05-08 03:20:50 +000097 }
98
Denis Vlasenko6884f662007-11-23 00:08:54 +000099 if (packet->cookie != htonl(DHCP_MAGIC)) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200100 bb_info_msg("Packet with bad magic, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +0000101 return -2;
102 }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200103 log1("Received a packet");
104 udhcp_dump_packet(packet);
Mike Frysinger7031f622006-05-08 03:20:50 +0000105
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000106 if (packet->op == BOOTREQUEST) {
107 vendor = get_option(packet, DHCP_VENDOR);
108 if (vendor) {
109#if 0
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000110 static const char broken_vendors[][8] = {
111 "MSFT 98",
112 ""
113 };
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000114 int i;
115 for (i = 0; broken_vendors[i][0]; i++) {
116 if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i])
117 && !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])
118 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200119 log1("Broken client (%s), forcing broadcast replies",
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000120 broken_vendors[i]);
121 packet->flags |= htons(BROADCAST_FLAG);
122 }
123 }
124#else
125 if (vendor[OPT_LEN - 2] == (uint8_t)(sizeof("MSFT 98")-1)
126 && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000127 ) {
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200128 log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
Mike Frysinger7031f622006-05-08 03:20:50 +0000129 packet->flags |= htons(BROADCAST_FLAG);
130 }
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000131#endif
Mike Frysinger7031f622006-05-08 03:20:50 +0000132 }
133 }
134
135 return bytes;
136}
137
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000138uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
Mike Frysinger7031f622006-05-08 03:20:50 +0000139{
140 /* Compute Internet Checksum for "count" bytes
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200141 * beginning at location "addr".
Mike Frysinger7031f622006-05-08 03:20:50 +0000142 */
"Robert P. J. Day"68229832006-07-01 13:08:46 +0000143 int32_t sum = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000144 uint16_t *source = (uint16_t *) addr;
145
146 while (count > 1) {
147 /* This is the inner loop */
148 sum += *source++;
149 count -= 2;
150 }
151
152 /* Add left-over byte, if any */
153 if (count > 0) {
154 /* Make sure that the left-over byte is added correctly both
155 * with little and big endian hosts */
156 uint16_t tmp = 0;
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000157 *(uint8_t*)&tmp = *(uint8_t*)source;
Mike Frysinger7031f622006-05-08 03:20:50 +0000158 sum += tmp;
159 }
160 /* Fold 32-bit sum to 16 bits */
161 while (sum >> 16)
162 sum = (sum & 0xffff) + (sum >> 16);
163
164 return ~sum;
165}
166
Denis Vlasenko61987922007-12-21 16:32:30 +0000167/* Construct a ip/udp header for a packet, send packet */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200168int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000169 uint32_t source_ip, int source_port,
Denis Vlasenkoc321b512008-09-26 16:29:12 +0000170 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp,
171 int ifindex)
Mike Frysinger7031f622006-05-08 03:20:50 +0000172{
Mike Frysinger7031f622006-05-08 03:20:50 +0000173 struct sockaddr_ll dest;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200174 struct ip_udp_dhcp_packet packet;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000175 int fd;
176 int result = -1;
177 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000178
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000179 enum {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200180 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
181 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE - offsetof(struct ip_udp_dhcp_packet, udp),
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000182 };
183
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000184 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
185 if (fd < 0) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000186 msg = "socket(%s)";
187 goto ret_msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000188 }
189
190 memset(&dest, 0, sizeof(dest));
191 memset(&packet, 0, sizeof(packet));
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200192 packet.data = *dhcp_pkt; /* struct copy */
Mike Frysinger7031f622006-05-08 03:20:50 +0000193
194 dest.sll_family = AF_PACKET;
195 dest.sll_protocol = htons(ETH_P_IP);
196 dest.sll_ifindex = ifindex;
197 dest.sll_halen = 6;
198 memcpy(dest.sll_addr, dest_arp, 6);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000199 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
200 msg = "bind(%s)";
201 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000202 }
203
204 packet.ip.protocol = IPPROTO_UDP;
205 packet.ip.saddr = source_ip;
206 packet.ip.daddr = dest_ip;
207 packet.udp.source = htons(source_port);
208 packet.udp.dest = htons(dest_port);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000209 /* size, excluding IP header: */
210 packet.udp.len = htons(UPD_DHCP_SIZE);
211 /* for UDP checksumming, ip.len is set to UDP packet len */
Mike Frysinger7031f622006-05-08 03:20:50 +0000212 packet.ip.tot_len = packet.udp.len;
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000213 packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
214 /* but for sending, it is set to IP packet len */
215 packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000216 packet.ip.ihl = sizeof(packet.ip) >> 2;
217 packet.ip.version = IPVERSION;
218 packet.ip.ttl = IPDEFTTL;
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000219 packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
Mike Frysinger7031f622006-05-08 03:20:50 +0000220
Denis Vlasenko61987922007-12-21 16:32:30 +0000221 /* Currently we send full-sized DHCP packets (zero padded).
222 * If you need to change this: last byte of the packet is
223 * packet.data.options[end_option(packet.data.options)]
224 */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200225 udhcp_dump_packet(dhcp_pkt);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000226 result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
227 (struct sockaddr *) &dest, sizeof(dest));
228 msg = "sendto";
229 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000230 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000231 if (result < 0) {
232 ret_msg:
233 bb_perror_msg(msg, "PACKET");
234 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000235 return result;
236}
237
Mike Frysinger7031f622006-05-08 03:20:50 +0000238/* Let the kernel do all the work for packet generation */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200239int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000240 uint32_t source_ip, int source_port,
241 uint32_t dest_ip, int dest_port)
Mike Frysinger7031f622006-05-08 03:20:50 +0000242{
Mike Frysinger7031f622006-05-08 03:20:50 +0000243 struct sockaddr_in client;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000244 int fd;
245 int result = -1;
246 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000247
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000248 enum {
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200249 DHCP_SIZE = sizeof(struct dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000250 };
251
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000252 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000253 if (fd < 0) {
254 msg = "socket(%s)";
255 goto ret_msg;
256 }
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000257 setsockopt_reuseaddr(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000258
259 memset(&client, 0, sizeof(client));
260 client.sin_family = AF_INET;
261 client.sin_port = htons(source_port);
262 client.sin_addr.s_addr = source_ip;
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000263 if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000264 msg = "bind(%s)";
265 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000266 }
267
268 memset(&client, 0, sizeof(client));
269 client.sin_family = AF_INET;
270 client.sin_port = htons(dest_port);
271 client.sin_addr.s_addr = dest_ip;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000272 if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
273 msg = "connect";
274 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000275 }
276
Denis Vlasenko61987922007-12-21 16:32:30 +0000277 /* Currently we send full-sized DHCP packets (see above) */
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200278 udhcp_dump_packet(dhcp_pkt);
279 result = safe_write(fd, dhcp_pkt, DHCP_SIZE);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000280 msg = "write";
281 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000282 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000283 if (result < 0) {
284 ret_msg:
285 bb_perror_msg(msg, "UDP");
286 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000287 return result;
288}