1 /* vi: set sw=4 ts=4: */
3 * packet.c -- packet ops
4 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
9 #include <netinet/in.h>
10 #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
11 #include <netpacket/packet.h>
12 #include <net/ethernet.h>
14 #include <asm/types.h>
15 #include <linux/if_packet.h>
16 #include <linux/if_ether.h>
24 void FAST_FUNC udhcp_init_header(struct dhcpMessage *packet, char type)
26 memset(packet, 0, sizeof(struct dhcpMessage));
27 packet->op = BOOTREQUEST; /* if client to a server */
32 packet->op = BOOTREPLY; /* if server to client */
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);
42 /* read a packet from socket fd, return -1 on read error, -2 on packet error */
43 int FAST_FUNC udhcp_recv_kernel_packet(struct dhcpMessage *packet, int fd)
46 unsigned char *vendor;
48 memset(packet, 0, sizeof(*packet));
49 bytes = safe_read(fd, packet, sizeof(*packet));
51 DEBUG("cannot read on listening socket, ignoring");
52 return bytes; /* returns -1 */
55 if (packet->cookie != htonl(DHCP_MAGIC)) {
56 bb_error_msg("received bogus message, ignoring");
59 DEBUG("Received a packet");
61 if (packet->op == BOOTREQUEST) {
62 vendor = get_option(packet, DHCP_VENDOR);
65 static const char broken_vendors[][8] = {
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])
74 DEBUG("broken client (%s), forcing broadcast replies",
76 packet->flags |= htons(BROADCAST_FLAG);
80 if (vendor[OPT_LEN - 2] == (uint8_t)(sizeof("MSFT 98")-1)
81 && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
83 DEBUG("broken client (%s), forcing broadcast replies", "MSFT 98");
84 packet->flags |= htons(BROADCAST_FLAG);
94 uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
96 /* Compute Internet Checksum for "count" bytes
97 * beginning at location "addr".
100 uint16_t *source = (uint16_t *) addr;
103 /* This is the inner loop */
108 /* Add left-over byte, if any */
110 /* Make sure that the left-over byte is added correctly both
111 * with little and big endian hosts */
113 *(uint8_t*)&tmp = *(uint8_t*)source;
116 /* Fold 32-bit sum to 16 bits */
118 sum = (sum & 0xffff) + (sum >> 16);
124 /* Construct a ip/udp header for a packet, send packet */
125 int FAST_FUNC udhcp_send_raw_packet(struct dhcpMessage *payload,
126 uint32_t source_ip, int source_port,
127 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp,
130 struct sockaddr_ll dest;
131 struct udp_dhcp_packet packet;
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),
141 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
147 memset(&dest, 0, sizeof(dest));
148 memset(&packet, 0, sizeof(packet));
149 packet.data = *payload; /* struct copy */
151 dest.sll_family = AF_PACKET;
152 dest.sll_protocol = htons(ETH_P_IP);
153 dest.sll_ifindex = ifindex;
155 memcpy(dest.sll_addr, dest_arp, 6);
156 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
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);
166 /* size, excluding IP header: */
167 packet.udp.len = htons(UPD_DHCP_SIZE);
168 /* for UDP checksumming, ip.len is set to UDP packet len */
169 packet.ip.tot_len = packet.udp.len;
170 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);
173 packet.ip.ihl = sizeof(packet.ip) >> 2;
174 packet.ip.version = IPVERSION;
175 packet.ip.ttl = IPDEFTTL;
176 packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
178 /* 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)]
182 result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
183 (struct sockaddr *) &dest, sizeof(dest));
189 bb_perror_msg(msg, "PACKET");
195 /* Let the kernel do all the work for packet generation */
196 int FAST_FUNC udhcp_send_kernel_packet(struct dhcpMessage *payload,
197 uint32_t source_ip, int source_port,
198 uint32_t dest_ip, int dest_port)
200 struct sockaddr_in client;
206 DHCP_SIZE = sizeof(struct dhcpMessage) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
209 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
214 setsockopt_reuseaddr(fd);
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;
220 if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
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;
229 if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
234 /* Currently we send full-sized DHCP packets (see above) */
235 result = safe_write(fd, payload, DHCP_SIZE);
241 bb_perror_msg(msg, "UDP");