diff options
author | Baruch Siach <baruch@tkos.co.il> | 2011-09-07 17:52:37 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-09-07 17:55:40 +0200 |
commit | e8f36330d9bb27f9f7e66aa6f01ff92c07d86f62 (patch) | |
tree | 863018163a166cc690902d8027a3f04f9f812dd3 /networking/udhcp | |
parent | 8c84f7545cf08925edb23d94d9f6519b338267c6 (diff) | |
download | busybox-w32-e8f36330d9bb27f9f7e66aa6f01ff92c07d86f62.tar.gz busybox-w32-e8f36330d9bb27f9f7e66aa6f01ff92c07d86f62.tar.bz2 busybox-w32-e8f36330d9bb27f9f7e66aa6f01ff92c07d86f62.zip |
networking: consolidate the IP checksum code. -129 bytes.
Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/udhcp')
-rw-r--r-- | networking/udhcp/dhcpc.c | 4 | ||||
-rw-r--r-- | networking/udhcp/packet.c | 34 |
2 files changed, 5 insertions, 33 deletions
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 4d755e6b8..3be09f4d7 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c | |||
@@ -739,7 +739,7 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) | |||
739 | /* verify IP checksum */ | 739 | /* verify IP checksum */ |
740 | check = packet.ip.check; | 740 | check = packet.ip.check; |
741 | packet.ip.check = 0; | 741 | packet.ip.check = 0; |
742 | if (check != udhcp_checksum(&packet.ip, sizeof(packet.ip))) { | 742 | if (check != inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip))) { |
743 | log1("Bad IP header checksum, ignoring"); | 743 | log1("Bad IP header checksum, ignoring"); |
744 | return -2; | 744 | return -2; |
745 | } | 745 | } |
@@ -750,7 +750,7 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) | |||
750 | packet.ip.tot_len = packet.udp.len; /* yes, this is needed */ | 750 | packet.ip.tot_len = packet.udp.len; /* yes, this is needed */ |
751 | check = packet.udp.check; | 751 | check = packet.udp.check; |
752 | packet.udp.check = 0; | 752 | packet.udp.check = 0; |
753 | if (check && check != udhcp_checksum(&packet, bytes)) { | 753 | if (check && check != inet_cksum((uint16_t *)&packet, bytes)) { |
754 | log1("Packet with bad UDP checksum received, ignoring"); | 754 | log1("Packet with bad UDP checksum received, ignoring"); |
755 | return -2; | 755 | return -2; |
756 | } | 756 | } |
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, |