1 /* vi: set sw=4 ts=4: */
5 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
7 * Licensed under GPLv2, see file LICENSE in this source tree.
11 #include <netinet/in.h>
12 #include <netinet/if_ether.h>
13 #include <netpacket/packet.h>
15 void FAST_FUNC udhcp_init_header(struct dhcp_packet *packet, char type)
17 memset(packet, 0, sizeof(*packet));
18 packet->op = BOOTREQUEST; /* if client to a server */
23 packet->op = BOOTREPLY; /* if server to client */
25 packet->htype = 1; /* ethernet */
27 packet->cookie = htonl(DHCP_MAGIC);
29 packet->options[0] = DHCP_END;
30 udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
33 #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
34 void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet)
36 char buf[sizeof(packet->chaddr)*2 + 1];
69 //, packet->chaddr[16]
75 *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
76 bb_info_msg(" chaddr %s", buf);
80 /* Read a packet from socket fd, return -1 on read error, -2 on packet error */
81 int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd)
84 unsigned char *vendor;
86 memset(packet, 0, sizeof(*packet));
87 bytes = safe_read(fd, packet, sizeof(*packet));
89 log1("Packet read error, ignoring");
90 return bytes; /* returns -1 */
93 if (packet->cookie != htonl(DHCP_MAGIC)) {
94 bb_info_msg("Packet with bad magic, ignoring");
97 log1("Received a packet");
98 udhcp_dump_packet(packet);
100 if (packet->op == BOOTREQUEST) {
101 vendor = udhcp_get_option(packet, DHCP_VENDOR);
104 static const char broken_vendors[][8] = {
109 for (i = 0; broken_vendors[i][0]; i++) {
110 if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)strlen(broken_vendors[i])
111 && strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - OPT_DATA]) == 0
113 log1("Broken client (%s), forcing broadcast replies",
115 packet->flags |= htons(BROADCAST_FLAG);
119 if (vendor[OPT_LEN - OPT_DATA] == (uint8_t)(sizeof("MSFT 98")-1)
120 && memcmp(vendor, "MSFT 98", sizeof("MSFT 98")-1) == 0
122 log1("Broken client (%s), forcing broadcast replies", "MSFT 98");
123 packet->flags |= htons(BROADCAST_FLAG);
132 uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
134 /* Compute Internet Checksum for "count" bytes
135 * beginning at location "addr".
138 uint16_t *source = (uint16_t *) addr;
141 /* This is the inner loop */
146 /* Add left-over byte, if any */
148 /* Make sure that the left-over byte is added correctly both
149 * with little and big endian hosts */
151 *(uint8_t*)&tmp = *(uint8_t*)source;
154 /* Fold 32-bit sum to 16 bits */
156 sum = (sum & 0xffff) + (sum >> 16);
161 /* Construct a ip/udp header for a packet, send packet */
162 int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
163 uint32_t source_nip, int source_port,
164 uint32_t dest_nip, int dest_port, const uint8_t *dest_arp,
167 struct sockaddr_ll dest_sll;
168 struct ip_udp_dhcp_packet packet;
174 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
180 memset(&dest_sll, 0, sizeof(dest_sll));
181 memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
182 packet.data = *dhcp_pkt; /* struct copy */
184 dest_sll.sll_family = AF_PACKET;
185 dest_sll.sll_protocol = htons(ETH_P_IP);
186 dest_sll.sll_ifindex = ifindex;
187 dest_sll.sll_halen = 6;
188 memcpy(dest_sll.sll_addr, dest_arp, 6);
190 if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
195 /* We were sending full-sized DHCP packets (zero padded),
196 * but some badly configured servers were seen dropping them.
197 * Apparently they drop all DHCP packets >576 *ethernet* octets big,
198 * whereas they may only drop packets >576 *IP* octets big
199 * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
201 * In order to work with those buggy servers,
202 * we truncate packets after end option byte.
204 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
206 packet.ip.protocol = IPPROTO_UDP;
207 packet.ip.saddr = source_nip;
208 packet.ip.daddr = dest_nip;
209 packet.udp.source = htons(source_port);
210 packet.udp.dest = htons(dest_port);
211 /* size, excluding IP header: */
212 packet.udp.len = htons(UDP_DHCP_SIZE - padding);
213 /* for UDP checksumming, ip.len is set to UDP packet len */
214 packet.ip.tot_len = packet.udp.len;
215 packet.udp.check = udhcp_checksum(&packet, IP_UDP_DHCP_SIZE - padding);
216 /* but for sending, it is set to IP packet len */
217 packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
218 packet.ip.ihl = sizeof(packet.ip) >> 2;
219 packet.ip.version = IPVERSION;
220 packet.ip.ttl = IPDEFTTL;
221 packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
223 udhcp_dump_packet(dhcp_pkt);
224 result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
225 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
231 bb_perror_msg(msg, "PACKET");
236 /* Let the kernel do all the work for packet generation */
237 int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
238 uint32_t source_nip, int source_port,
239 uint32_t dest_nip, int dest_port)
241 struct sockaddr_in client;
247 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
252 setsockopt_reuseaddr(fd);
254 memset(&client, 0, sizeof(client));
255 client.sin_family = AF_INET;
256 client.sin_port = htons(source_port);
257 client.sin_addr.s_addr = source_nip;
258 if (bind(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
263 memset(&client, 0, sizeof(client));
264 client.sin_family = AF_INET;
265 client.sin_port = htons(dest_port);
266 client.sin_addr.s_addr = dest_nip;
267 if (connect(fd, (struct sockaddr *)&client, sizeof(client)) == -1) {
272 udhcp_dump_packet(dhcp_pkt);
274 padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
275 result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
281 bb_perror_msg(msg, "UDP");