1 --- a/networking/udhcp/packet.c
2 +++ b/networking/udhcp/packet.c
3 @@ -165,6 +165,11 @@ uint16_t FAST_FUNC udhcp_checksum(void *
7 +int udhcp_get_payload_len(struct dhcp_packet *dhcp_pkt)
9 + return sizeof(struct dhcp_packet) - DHCP_OPTIONS_BUFSIZE + udhcp_end_option(dhcp_pkt->options) + sizeof(dhcp_pkt->options[0]);
12 /* Construct a ip/udp header for a packet, send packet */
13 int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
14 uint32_t source_nip, int source_port,
15 @@ -173,10 +178,10 @@ int FAST_FUNC udhcp_send_raw_packet(stru
17 struct sockaddr_ll dest_sll;
18 struct ip_udp_dhcp_packet packet;
23 + int p_len = udhcp_get_payload_len(dhcp_pkt);
25 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
27 @@ -185,8 +190,8 @@ int FAST_FUNC udhcp_send_raw_packet(stru
30 memset(&dest_sll, 0, sizeof(dest_sll));
31 - memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
32 - packet.data = *dhcp_pkt; /* struct copy */
33 + memset(&packet, 0, sizeof(packet));
34 + memcpy(&(packet.data), dhcp_pkt, p_len);
36 dest_sll.sll_family = AF_PACKET;
37 dest_sll.sll_protocol = htons(ETH_P_IP);
38 @@ -199,36 +204,24 @@ int FAST_FUNC udhcp_send_raw_packet(stru
42 - /* We were sending full-sized DHCP packets (zero padded),
43 - * but some badly configured servers were seen dropping them.
44 - * Apparently they drop all DHCP packets >576 *ethernet* octets big,
45 - * whereas they may only drop packets >576 *IP* octets big
46 - * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
48 - * In order to work with those buggy servers,
49 - * we truncate packets after end option byte.
51 - padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
53 packet.ip.protocol = IPPROTO_UDP;
54 packet.ip.saddr = source_nip;
55 packet.ip.daddr = dest_nip;
56 packet.udp.source = htons(source_port);
57 packet.udp.dest = htons(dest_port);
58 - /* size, excluding IP header: */
59 - packet.udp.len = htons(UPD_DHCP_SIZE - padding);
60 - /* for UDP checksumming, ip.len is set to UDP packet len */
61 + p_len += sizeof(packet.udp);
62 + packet.udp.len = htons(p_len);
63 packet.ip.tot_len = packet.udp.len;
64 - packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE - padding);
65 - /* but for sending, it is set to IP packet len */
66 - packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE - padding);
67 + p_len += sizeof(packet.ip);
68 + packet.udp.check = udhcp_checksum(&packet, p_len);
69 + packet.ip.tot_len = htons(p_len);
70 packet.ip.ihl = sizeof(packet.ip) >> 2;
71 packet.ip.version = IPVERSION;
72 packet.ip.ttl = IPDEFTTL;
73 packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
75 udhcp_dump_packet(dhcp_pkt);
76 - result = sendto(fd, &packet, IP_UPD_DHCP_SIZE - padding, /*flags:*/ 0,
77 + result = sendto(fd, &packet, p_len, /*flags:*/ 0,
78 (struct sockaddr *) &dest_sll, sizeof(dest_sll));
81 @@ -246,7 +239,6 @@ int FAST_FUNC udhcp_send_kernel_packet(s
82 uint32_t dest_nip, int dest_port)
84 struct sockaddr_in client;
89 @@ -277,9 +269,7 @@ int FAST_FUNC udhcp_send_kernel_packet(s
92 udhcp_dump_packet(dhcp_pkt);
94 - padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
95 - result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
96 + result = safe_write(fd, dhcp_pkt, udhcp_get_payload_len(dhcp_pkt));