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.
8 #include <netinet/in.h>
9 #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
10 #include <netpacket/packet.h>
11 #include <net/ethernet.h>
13 #include <asm/types.h>
14 #include <linux/if_packet.h>
15 #include <linux/if_ether.h>
21 void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
23 memset(packet, 0, sizeof(*packet));
24 packet->op = BOOTREQUEST; /* if client to a server */
29 packet->op = BOOTREPLY; /* if server to client */
31 packet->htype = 1; /* ethernet */
33 packet->cookie = htonl(DHCP_MAGIC);
35 packet->options[0] = DHCP_END;
36 udhcp_add_simple_option(packet->options, DHCP_MESSAGE_TYPE, type);
39 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
40 void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
42 char buf[sizeof(packet->chaddr)*2 + 1];
75 //, packet->chaddr[16]
81 *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
82 bb_info_msg(" chaddr %s", buf);
86 /* Read a packet from socket fd, return -1 on read error, -2 on packet error */
87 int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
90 unsigned char *vendor;
92 memset(packet, 0, sizeof(*packet));
93 bytes = safe_read(fd, packet, sizeof(*packet));
95 log1("Packet read error, ignoring");
96 return bytes; /* returns -1 */
99 if (packet->cookie != htonl(DHCP_MAGIC)) {
100 bb_info_msg("Packet with bad magic, ignoring");
103 log1("Received a packet");
104 udhcp_dump_packet(packet);
106 if (packet->op == BOOTREQUEST) {
107 vendor = udhcp_get_option(packet, DHCP_VENDOR);
110 static const char broken_vendors[][8] = {
115 for (i = 0; broken_vendors[i][0]; i++) {
116 if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)strlen(broken_vendors[i])
117 && strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - OPT_DATA]) == 0
119 log1("Broken client (%s), forcing broadcast replies",
121 packet->flags |= htons(BROADCAST_FLAG);
125 if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)(sizeof("MSFT 98")-1)
126 && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
128 log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
129 packet->flags |= htons(BROADCAST_FLAG);
138 uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
140 /* Compute Internet Checksum for "count" bytes
141 * beginning at location "addr".
144 uint16_t *source = (uint16_t *) addr;
147 /* This is the inner loop */
152 /* Add left-over byte, if any */
154 /* Make sure that the left-over byte is added correctly both
155 * with little and big endian hosts */
157 *(uint8_t*)&tmp = *(uint8_t*)source;
160 /* Fold 32-bit sum to 16 bits */
162 sum = (sum & 0xffff) + (sum >> 16);
167 /* Construct a ip/udp header for a packet, send packet */
168 int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
169 uint32_t source_ip, int source_port,
170 uint32_t dest_ip, int dest_port, const uint8_t *dest_arp,
173 struct sockaddr_ll dest;
174 struct ip_udp_dhcp_packet packet;
180 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),
184 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
190 memset(&dest, 0, sizeof(dest));
191 memset(&packet, 0, sizeof(packet));
192 packet.data = *dhcp_pkt; /* struct copy */
194 dest.sll_family = AF_PACKET;
195 dest.sll_protocol = htons(ETH_P_IP);
196 dest.sll_ifindex = ifindex;
198 memcpy(dest.sll_addr, dest_arp, 6);
199 if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
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);
209 /* size, excluding IP header: */
210 packet.udp.len = htons(UPD_DHCP_SIZE);
211 /* for UDP checksumming, ip.len is set to UDP packet len */
212 packet.ip.tot_len = packet.udp.len;
213 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);
216 packet.ip.ihl = sizeof(packet.ip) >> 2;
217 packet.ip.version = IPVERSION;
218 packet.ip.ttl = IPDEFTTL;
219 packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
221 /* 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[udhcp_end_option(packet.data.options)]
225 udhcp_dump_packet(dhcp_pkt);
226 result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
227 (struct sockaddr *) &dest, sizeof(dest));
231 /* FIXME: and if result >= 0 but != IP_UPD_DHCP_SIZE? */
234 bb_perror_msg(msg, "PACKET");
239 /* Let the kernel do all the work for packet generation */
240 int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
241 uint32_t source_ip, int source_port,
242 uint32_t dest_ip, int dest_port)
244 struct sockaddr_in client;
250 DHCP_SIZE = sizeof(struct dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS,
253 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
258 setsockopt_reuseaddr(fd);
260 memset(&client, 0, sizeof(client));
261 client.sin_family = AF_INET;
262 client.sin_port = htons(source_port);
263 client.sin_addr.s_addr = source_ip;
264 if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
269 memset(&client, 0, sizeof(client));
270 client.sin_family = AF_INET;
271 client.sin_port = htons(dest_port);
272 client.sin_addr.s_addr = dest_ip;
273 if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
278 /* Currently we send full-sized DHCP packets (see above) */
279 udhcp_dump_packet(dhcp_pkt);
280 result = safe_write(fd, dhcp_pkt, DHCP_SIZE);
284 /* FIXME: and if result >= 0 but != DHCP_SIZE? */
287 bb_perror_msg(msg, "UDP");