diff options
Diffstat (limited to 'networking/udhcp/packet.c')
-rw-r--r-- | networking/udhcp/packet.c | 34 |
1 files changed, 3 insertions, 31 deletions
diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c index 66b42c5e1..4d5ff0676 100644 --- a/networking/udhcp/packet.c +++ b/networking/udhcp/packet.c | |||
@@ -129,35 +129,6 @@ int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd) | |||
129 | return bytes; | 129 | return bytes; |
130 | } | 130 | } |
131 | 131 | ||
132 | uint16_t FAST_FUNC udhcp_checksum(void *addr, int count) | ||
133 | { | ||
134 | /* Compute Internet Checksum for "count" bytes | ||
135 | * beginning at location "addr". | ||
136 | */ | ||
137 | int32_t sum = 0; | ||
138 | uint16_t *source = (uint16_t *) addr; | ||
139 | |||
140 | while (count > 1) { | ||
141 | /* This is the inner loop */ | ||
142 | sum += *source++; | ||
143 | count -= 2; | ||
144 | } | ||
145 | |||
146 | /* Add left-over byte, if any */ | ||
147 | if (count > 0) { | ||
148 | /* Make sure that the left-over byte is added correctly both | ||
149 | * with little and big endian hosts */ | ||
150 | uint16_t tmp = 0; | ||
151 | *(uint8_t*)&tmp = *(uint8_t*)source; | ||
152 | sum += tmp; | ||
153 | } | ||
154 | /* Fold 32-bit sum to 16 bits */ | ||
155 | while (sum >> 16) | ||
156 | sum = (sum & 0xffff) + (sum >> 16); | ||
157 | |||
158 | return ~sum; | ||
159 | } | ||
160 | |||
161 | /* Construct a ip/udp header for a packet, send packet */ | 132 | /* Construct a ip/udp header for a packet, send packet */ |
162 | int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, | 133 | int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, |
163 | uint32_t source_nip, int source_port, | 134 | uint32_t source_nip, int source_port, |
@@ -212,13 +183,14 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, | |||
212 | packet.udp.len = htons(UDP_DHCP_SIZE - padding); | 183 | packet.udp.len = htons(UDP_DHCP_SIZE - padding); |
213 | /* for UDP checksumming, ip.len is set to UDP packet len */ | 184 | /* for UDP checksumming, ip.len is set to UDP packet len */ |
214 | packet.ip.tot_len = packet.udp.len; | 185 | packet.ip.tot_len = packet.udp.len; |
215 | packet.udp.check = udhcp_checksum(&packet, IP_UDP_DHCP_SIZE - padding); | 186 | packet.udp.check = inet_cksum((uint16_t *)&packet, |
187 | IP_UDP_DHCP_SIZE - padding); | ||
216 | /* but for sending, it is set to IP packet len */ | 188 | /* but for sending, it is set to IP packet len */ |
217 | packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding); | 189 | packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding); |
218 | packet.ip.ihl = sizeof(packet.ip) >> 2; | 190 | packet.ip.ihl = sizeof(packet.ip) >> 2; |
219 | packet.ip.version = IPVERSION; | 191 | packet.ip.version = IPVERSION; |
220 | packet.ip.ttl = IPDEFTTL; | 192 | packet.ip.ttl = IPDEFTTL; |
221 | packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip)); | 193 | packet.ip.check = inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip)); |
222 | 194 | ||
223 | udhcp_dump_packet(dhcp_pkt); | 195 | udhcp_dump_packet(dhcp_pkt); |
224 | result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0, | 196 | result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0, |