dhcp: truncate packets instead of padding them to 574 bytes. closes bug 1849
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 30 May 2010 22:45:09 +0000 (00:45 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 30 May 2010 22:45:09 +0000 (00:45 +0200)
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>
networking/udhcp/packet.c

index c01fb7d7cbefc85ce49d4af35bf628699ddc0c8c..4badc9cbcbc8aa73d8dd7ea975fa599fc1cef4ec 100644 (file)
@@ -173,6 +173,7 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
 {
        struct sockaddr_ll dest_sll;
        struct ip_udp_dhcp_packet packet;
+       unsigned padding;
        int fd;
        int result = -1;
        const char *msg;
@@ -198,34 +199,40 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
                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.saddr = source_nip;
        packet.ip.daddr = dest_nip;
        packet.udp.source = htons(source_port);
        packet.udp.dest = htons(dest_port);
        /* 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 */
        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 */
-       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.version = IPVERSION;
        packet.ip.ttl = IPDEFTTL;
        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);
-       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));
        msg = "sendto";
  ret_close:
        close(fd);
-       /* FIXME: and if result >= 0 but != IP_UPD_DHCP_SIZE? */
        if (result < 0) {
  ret_msg:
                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)
 {
        struct sockaddr_in client;
+       unsigned padding;
        int fd;
        int result = -1;
        const char *msg;
@@ -268,13 +276,13 @@ int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
                goto ret_close;
        }
 
-       /* Currently we send full-sized DHCP packets (see above) */
        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";
  ret_close:
        close(fd);
-       /* FIXME: and if result >= 0 but != DHCP_SIZE? */
        if (result < 0) {
  ret_msg:
                bb_perror_msg(msg, "UDP");