dhcp: truncate packets instead of padding them to 574 bytes. closes bug 1849

function                                             old     new   delta
udhcp_send_raw_packet                                411     456     +45
udhcp_send_kernel_packet                             259     271     +12

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2010-05-31 00:45:09 +02:00
parent e58c6e2e13
commit b8b72f02f0

View File

@ -173,6 +173,7 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
{ {
struct sockaddr_ll dest_sll; struct sockaddr_ll dest_sll;
struct ip_udp_dhcp_packet packet; struct ip_udp_dhcp_packet packet;
unsigned padding;
int fd; int fd;
int result = -1; int result = -1;
const char *msg; const char *msg;
@ -198,34 +199,40 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
goto ret_close; goto ret_close;
} }
/* We were sending full-sized DHCP packets (zero padded),
* but some badly configured servers were seen dropping them.
* Apparently they drop all DHCP packets >576 *ethernet* octets big,
* whereas they may only drop packets >576 *IP* octets big
* (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
*
* In order to work with those buggy servers,
* we truncate packets after end option byte.
*/
padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
packet.ip.protocol = IPPROTO_UDP; packet.ip.protocol = IPPROTO_UDP;
packet.ip.saddr = source_nip; packet.ip.saddr = source_nip;
packet.ip.daddr = dest_nip; packet.ip.daddr = dest_nip;
packet.udp.source = htons(source_port); packet.udp.source = htons(source_port);
packet.udp.dest = htons(dest_port); packet.udp.dest = htons(dest_port);
/* size, excluding IP header: */ /* size, excluding IP header: */
packet.udp.len = htons(UPD_DHCP_SIZE); packet.udp.len = htons(UPD_DHCP_SIZE - padding);
/* for UDP checksumming, ip.len is set to UDP packet len */ /* for UDP checksumming, ip.len is set to UDP packet len */
packet.ip.tot_len = packet.udp.len; packet.ip.tot_len = packet.udp.len;
packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE); packet.udp.check = udhcp_checksum(&packet, IP_UPD_DHCP_SIZE - padding);
/* but for sending, it is set to IP packet len */ /* but for sending, it is set to IP packet len */
packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE); packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE - padding);
packet.ip.ihl = sizeof(packet.ip) >> 2; packet.ip.ihl = sizeof(packet.ip) >> 2;
packet.ip.version = IPVERSION; packet.ip.version = IPVERSION;
packet.ip.ttl = IPDEFTTL; packet.ip.ttl = IPDEFTTL;
packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip)); packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
/* Currently we send full-sized DHCP packets (zero padded).
* If you need to change this: last byte of the packet is
* packet.data.options[udhcp_end_option(packet.data.options)]
*/
udhcp_dump_packet(dhcp_pkt); udhcp_dump_packet(dhcp_pkt);
result = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0, result = sendto(fd, &packet, IP_UPD_DHCP_SIZE - padding, /*flags:*/ 0,
(struct sockaddr *) &dest_sll, sizeof(dest_sll)); (struct sockaddr *) &dest_sll, sizeof(dest_sll));
msg = "sendto"; msg = "sendto";
ret_close: ret_close:
close(fd); close(fd);
/* FIXME: and if result >= 0 but != IP_UPD_DHCP_SIZE? */
if (result < 0) { if (result < 0) {
ret_msg: ret_msg:
bb_perror_msg(msg, "PACKET"); bb_perror_msg(msg, "PACKET");
@ -239,6 +246,7 @@ int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
uint32_t dest_nip, int dest_port) uint32_t dest_nip, int dest_port)
{ {
struct sockaddr_in client; struct sockaddr_in client;
unsigned padding;
int fd; int fd;
int result = -1; int result = -1;
const char *msg; const char *msg;
@ -268,13 +276,13 @@ int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
goto ret_close; goto ret_close;
} }
/* Currently we send full-sized DHCP packets (see above) */
udhcp_dump_packet(dhcp_pkt); udhcp_dump_packet(dhcp_pkt);
result = safe_write(fd, dhcp_pkt, DHCP_SIZE);
padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
msg = "write"; msg = "write";
ret_close: ret_close:
close(fd); close(fd);
/* FIXME: and if result >= 0 but != DHCP_SIZE? */
if (result < 0) { if (result < 0) {
ret_msg: ret_msg:
bb_perror_msg(msg, "UDP"); bb_perror_msg(msg, "UDP");