blob: e2c8e6eadbc3d5613dda2abca4ecf842138771d4 [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 */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +00008
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
Mike Frysinger7031f622006-05-08 03:20:50 +000011#include <netpacket/packet.h>
12#include <net/ethernet.h>
13#else
14#include <asm/types.h>
15#include <linux/if_packet.h>
16#include <linux/if_ether.h>
17#endif
Mike Frysinger7031f622006-05-08 03:20:50 +000018
19#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000020#include "dhcpd.h"
21#include "options.h"
22
23
Denis Vlasenkof1980f62008-09-26 09:34:59 +000024void FAST_FUNC udhcp_init_header(struct dhcpMessage *packet, char type)
Mike Frysinger7031f622006-05-08 03:20:50 +000025{
26 memset(packet, 0, sizeof(struct dhcpMessage));
Denis Vlasenko739e30f2008-09-26 23:45:20 +000027 packet->op = BOOTREQUEST; /* if client to a server */
Mike Frysinger7031f622006-05-08 03:20:50 +000028 switch (type) {
Mike Frysinger7031f622006-05-08 03:20:50 +000029 case DHCPOFFER:
30 case DHCPACK:
31 case DHCPNAK:
Denis Vlasenko739e30f2008-09-26 23:45:20 +000032 packet->op = BOOTREPLY; /* if server to client */
Mike Frysinger7031f622006-05-08 03:20:50 +000033 }
34 packet->htype = ETH_10MB;
35 packet->hlen = ETH_10MB_LEN;
36 packet->cookie = htonl(DHCP_MAGIC);
37 packet->options[0] = DHCP_END;
38 add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
39}
40
41
42/* read a packet from socket fd, return -1 on read error, -2 on packet error */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000043int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd)
Mike Frysinger7031f622006-05-08 03:20:50 +000044{
Mike Frysinger7031f622006-05-08 03:20:50 +000045 int bytes;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000046 unsigned char *vendor;
Mike Frysinger7031f622006-05-08 03:20:50 +000047
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000048 memset(packet, 0, sizeof(*packet));
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000049 bytes = safe_read(fd, packet, sizeof(*packet));
Mike Frysinger7031f622006-05-08 03:20:50 +000050 if (bytes < 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000051 DEBUG("cannot read on listening socket, ignoring");
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000052 return bytes; /* returns -1 */
Mike Frysinger7031f622006-05-08 03:20:50 +000053 }
54
Denis Vlasenko6884f662007-11-23 00:08:54 +000055 if (packet->cookie != htonl(DHCP_MAGIC)) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000056 bb_error_msg("received bogus message, ignoring");
Mike Frysinger7031f622006-05-08 03:20:50 +000057 return -2;
58 }
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000059 DEBUG("Received a packet");
Mike Frysinger7031f622006-05-08 03:20:50 +000060
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000061 if (packet->op == BOOTREQUEST) {
62 vendor = get_option(packet, DHCP_VENDOR);
63 if (vendor) {
64#if 0
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +000065 static const char broken_vendors[][8] = {
66 "MSFT 98",
67 ""
68 };
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000069 int i;
70 for (i = 0; broken_vendors[i][0]; i++) {
71 if (vendor[OPT_LEN - 2] == (uint8_t)strlen(broken_vendors[i])
72 && !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])
73 ) {
Denis Vlasenko739e30f2008-09-26 23:45:20 +000074 DEBUG("broken client (%s), forcing broadcast replies",
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000075 broken_vendors[i]);
76 packet->flags |= htons(BROADCAST_FLAG);
77 }
78 }
79#else
80 if (vendor[OPT_LEN - 2] == (uint8_t)(sizeof("MSFT 98")-1)
81 && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000082 ) {
Denis Vlasenko739e30f2008-09-26 23:45:20 +000083 DEBUG("broken client (%s), forcing broadcast replies", "MSFT 98");
Mike Frysinger7031f622006-05-08 03:20:50 +000084 packet->flags |= htons(BROADCAST_FLAG);
85 }
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000086#endif
Mike Frysinger7031f622006-05-08 03:20:50 +000087 }
88 }
89
90 return bytes;
91}
92
93
Denis Vlasenkof1980f62008-09-26 09:34:59 +000094uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
Mike Frysinger7031f622006-05-08 03:20:50 +000095{
96 /* Compute Internet Checksum for "count" bytes
97 * beginning at location "addr".
98 */
"Robert P. J. Day"68229832006-07-01 13:08:46 +000099 int32_t sum = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000100 uint16_t *source = (uint16_t *) addr;
101
102 while (count > 1) {
103 /* This is the inner loop */
104 sum += *source++;
105 count -= 2;
106 }
107
108 /* Add left-over byte, if any */
109 if (count > 0) {
110 /* Make sure that the left-over byte is added correctly both
111 * with little and big endian hosts */
112 uint16_t tmp = 0;
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000113 *(uint8_t*)&tmp = *(uint8_t*)source;
Mike Frysinger7031f622006-05-08 03:20:50 +0000114 sum += tmp;
115 }
116 /* Fold 32-bit sum to 16 bits */
117 while (sum >> 16)
118 sum = (sum & 0xffff) + (sum >> 16);
119
120 return ~sum;
121}
122
123
Denis Vlasenko61987922007-12-21 16:32:30 +0000124/* Construct a ip/udp header for a packet, send packet */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000125int FAST_FUNC udhcp_send_raw_packet(struct dhcpMessage *payload,
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000126 uint32_t source_ip, int source_port,
Denis Vlasenkoc321b512008-09-26 16:29:12 +0000127 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp,
128 int ifindex)
Mike Frysinger7031f622006-05-08 03:20:50 +0000129{
Mike Frysinger7031f622006-05-08 03:20:50 +0000130 struct sockaddr_ll dest;
131 struct udp_dhcp_packet packet;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000132 int fd;
133 int result = -1;
134 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000135
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000136 enum {
137 IP_UPD_DHCP_SIZE = sizeof(struct udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
138 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE - offsetof(struct udp_dhcp_packet, udp),
139 };
140
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000141 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
142 if (fd < 0) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000143 msg = "socket(%s)";
144 goto ret_msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000145 }
146
147 memset(&dest, 0, sizeof(dest));
148 memset(&packet, 0, sizeof(packet));
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000149 packet.data = *payload; /* struct copy */
Mike Frysinger7031f622006-05-08 03:20:50 +0000150
151 dest.sll_family = AF_PACKET;
152 dest.sll_protocol = htons(ETH_P_IP);
153 dest.sll_ifindex = ifindex;
154 dest.sll_halen = 6;
155 memcpy(dest.sll_addr, dest_arp, 6);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000156 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
157 msg = "bind(%s)";
158 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000159 }
160
161 packet.ip.protocol = IPPROTO_UDP;
162 packet.ip.saddr = source_ip;
163 packet.ip.daddr = dest_ip;
164 packet.udp.source = htons(source_port);
165 packet.udp.dest = htons(dest_port);
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000166 /* size, excluding IP header: */
167 packet.udp.len = htons(UPD_DHCP_SIZE);
168 /* for UDP checksumming, ip.len is set to UDP packet len */
Mike Frysinger7031f622006-05-08 03:20:50 +0000169 packet.ip.tot_len = packet.udp.len;
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000170 packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
171 /* but for sending, it is set to IP packet len */
172 packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
Mike Frysinger7031f622006-05-08 03:20:50 +0000173 packet.ip.ihl = sizeof(packet.ip) >> 2;
174 packet.ip.version = IPVERSION;
175 packet.ip.ttl = IPDEFTTL;
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000176 packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
Mike Frysinger7031f622006-05-08 03:20:50 +0000177
Denis Vlasenko61987922007-12-21 16:32:30 +0000178 /* Currently we send full-sized DHCP packets (zero padded).
179 * If you need to change this: last byte of the packet is
180 * packet.data.options[end_option(packet.data.options)]
181 */
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000182 result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
183 (struct sockaddr *) &dest, sizeof(dest));
184 msg = "sendto";
185 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000186 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000187 if (result < 0) {
188 ret_msg:
189 bb_perror_msg(msg, "PACKET");
190 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000191 return result;
192}
193
194
195/* Let the kernel do all the work for packet generation */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000196int FAST_FUNC udhcp_send_kernel_packet(struct dhcpMessage *payload,
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000197 uint32_t source_ip, int source_port,
198 uint32_t dest_ip, int dest_port)
Mike Frysinger7031f622006-05-08 03:20:50 +0000199{
Mike Frysinger7031f622006-05-08 03:20:50 +0000200 struct sockaddr_in client;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000201 int fd;
202 int result = -1;
203 const char *msg;
Mike Frysinger7031f622006-05-08 03:20:50 +0000204
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000205 enum {
206 DHCP_SIZE = sizeof(struct dhcpMessage) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
207 };
208
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000209 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000210 if (fd < 0) {
211 msg = "socket(%s)";
212 goto ret_msg;
213 }
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000214 setsockopt_reuseaddr(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +0000215
216 memset(&client, 0, sizeof(client));
217 client.sin_family = AF_INET;
218 client.sin_port = htons(source_port);
219 client.sin_addr.s_addr = source_ip;
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000220 if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000221 msg = "bind(%s)";
222 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000223 }
224
225 memset(&client, 0, sizeof(client));
226 client.sin_family = AF_INET;
227 client.sin_port = htons(dest_port);
228 client.sin_addr.s_addr = dest_ip;
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000229 if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
230 msg = "connect";
231 goto ret_close;
Mike Frysinger7031f622006-05-08 03:20:50 +0000232 }
233
Denis Vlasenko61987922007-12-21 16:32:30 +0000234 /* Currently we send full-sized DHCP packets (see above) */
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000235 result = safe_write(fd, payload, DHCP_SIZE);
236 msg = "write";
237 ret_close:
Mike Frysinger7031f622006-05-08 03:20:50 +0000238 close(fd);
Denis Vlasenko8e5b6f52007-12-24 17:32:22 +0000239 if (result < 0) {
240 ret_msg:
241 bb_perror_msg(msg, "UDP");
242 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000243 return result;
244}