return -EINVAL;
}
+ if (packet->ip.version != IPVERSION) {
+ log_dhcp_client(client, "ignoring packet: not IPv4");
+ return -EINVAL;
+ }
+
if (packet->ip.ihl < 5) {
log_dhcp_client(client, "ignoring packet: IPv4 IHL (%u words) invalid",
packet->ip.ihl);
return -EINVAL;
}
- if (dhcp_packet_checksum(&packet->ip, hdrlen)) {
- log_dhcp_client(client, "ignoring packet: invalid IP checksum");
- return -EINVAL;
- }
-
/* UDP */
if (packet->ip.protocol != IPPROTO_UDP) {
return -EINVAL;
}
+ if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) {
+ log_dhcp_client(client, "ignoring packet: to port %u, which "
+ "is not the DHCP client port (%u)",
+ be16toh(packet->udp.dest), DHCP_PORT_CLIENT);
+ return -EINVAL;
+ }
+
+ /* checksums - computing these is relatively expensive, so only do it
+ if all the other checks have passed
+ */
+
+ if (dhcp_packet_checksum(&packet->ip, hdrlen)) {
+ log_dhcp_client(client, "ignoring packet: invalid IP checksum");
+ return -EINVAL;
+ }
+
if (checksum && packet->udp.check) {
packet->ip.check = packet->udp.len;
packet->ip.ttl = 0;
}
}
- if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) {
- log_dhcp_client(client, "ignoring packet: to port %u, which "
- "is not the DHCP client port (%u)",
- be16toh(packet->udp.dest), DHCP_PORT_CLIENT);
- return -EINVAL;
- }
-
return 0;
}