1 | --- a/networking/udhcp/packet.c |
2 | +++ b/networking/udhcp/packet.c |
3 | @@ -164,6 +164,11 @@ uint16_t FAST_FUNC udhcp_checksum(void * |
4 | return ~sum; |
5 | } |
6 | |
7 | +int udhcp_get_payload_len(struct dhcp_packet *dhcp_pkt) |
8 | +{ |
9 | + return sizeof(struct dhcp_packet) - DHCP_OPTIONS_BUFSIZE + end_option(dhcp_pkt->options) + sizeof(dhcp_pkt->options[0]); |
10 | +} |
11 | + |
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_ip, int source_port, |
15 | @@ -175,11 +180,7 @@ int FAST_FUNC udhcp_send_raw_packet(stru |
16 | int fd; |
17 | int result = -1; |
18 | const char *msg; |
19 | - |
20 | - enum { |
21 | - IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, |
22 | - UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE - offsetof(struct ip_udp_dhcp_packet, udp), |
23 | - }; |
24 | + int p_len = udhcp_get_payload_len(dhcp_pkt); |
25 | |
26 | fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); |
27 | if (fd < 0) { |
28 | @@ -189,7 +190,7 @@ int FAST_FUNC udhcp_send_raw_packet(stru |
29 | |
30 | memset(&dest, 0, sizeof(dest)); |
31 | memset(&packet, 0, sizeof(packet)); |
32 | - packet.data = *dhcp_pkt; /* struct copy */ |
33 | + memcpy(&(packet.data), dhcp_pkt, p_len); |
34 | |
35 | dest.sll_family = AF_PACKET; |
36 | dest.sll_protocol = htons(ETH_P_IP); |
37 | @@ -206,24 +207,19 @@ int FAST_FUNC udhcp_send_raw_packet(stru |
38 | packet.ip.daddr = dest_ip; |
39 | packet.udp.source = htons(source_port); |
40 | packet.udp.dest = htons(dest_port); |
41 | - /* size, excluding IP header: */ |
42 | - packet.udp.len = htons(UPD_DHCP_SIZE); |
43 | - /* for UDP checksumming, ip.len is set to UDP packet len */ |
44 | + p_len += sizeof(packet.udp); |
45 | + packet.udp.len = htons(p_len); |
46 | packet.ip.tot_len = packet.udp.len; |
47 | - packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE); |
48 | - /* but for sending, it is set to IP packet len */ |
49 | - packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE); |
50 | + p_len += sizeof(packet.ip); |
51 | + packet.udp.check = udhcp_checksum(&packet, p_len); |
52 | + packet.ip.tot_len = htons(p_len); |
53 | packet.ip.ihl = sizeof(packet.ip) >> 2; |
54 | packet.ip.version = IPVERSION; |
55 | packet.ip.ttl = IPDEFTTL; |
56 | packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip)); |
57 | |
58 | - /* Currently we send full-sized DHCP packets (zero padded). |
59 | - * If you need to change this: last byte of the packet is |
60 | - * packet.data.options[end_option(packet.data.options)] |
61 | - */ |
62 | udhcp_dump_packet(dhcp_pkt); |
63 | - result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0, |
64 | + result = sendto(fd, &packet, p_len, 0, |
65 | (struct sockaddr *) &dest, sizeof(dest)); |
66 | msg = "sendto"; |
67 | ret_close: |
68 | @@ -245,10 +241,6 @@ int FAST_FUNC udhcp_send_kernel_packet(s |
69 | int result = -1; |
70 | const char *msg; |
71 | |
72 | - enum { |
73 | - DHCP_SIZE = sizeof(struct dhcp_packet) - CONFIG_UDHCPC_SLACK_FOR_BUGGY_SERVERS, |
74 | - }; |
75 | - |
76 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
77 | if (fd < 0) { |
78 | msg = "socket(%s)"; |
79 | @@ -274,9 +266,8 @@ int FAST_FUNC udhcp_send_kernel_packet(s |
80 | goto ret_close; |
81 | } |
82 | |
83 | - /* Currently we send full-sized DHCP packets (see above) */ |
84 | udhcp_dump_packet(dhcp_pkt); |
85 | - result = safe_write(fd, dhcp_pkt, DHCP_SIZE); |
86 | + result = safe_write(fd, dhcp_pkt, udhcp_get_payload_len(dhcp_pkt)); |
87 | msg = "write"; |
88 | ret_close: |
89 | close(fd); |
90 | |