[package] update busybox to 1.18.4, patch from Peter Wagner
[openwrt.git] / package / busybox / patches / 241-udhcpc-oversized_packets.patch
1 diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c
2 index 2b7528c..94e53dc 100644
3 --- a/networking/udhcp/packet.c
4 +++ b/networking/udhcp/packet.c
5 @@ -165,6 +165,11 @@ uint16_t FAST_FUNC udhcp_checksum(void *addr, int count)
6         return ~sum;
7  }
8  
9 +int udhcp_get_payload_len(struct dhcp_packet *dhcp_pkt)
10 +{
11 +       return sizeof(struct dhcp_packet) - DHCP_OPTIONS_BUFSIZE + udhcp_end_option(dhcp_pkt->options) + sizeof(dhcp_pkt->options[0]);
12 +}
13 +
14  /* Construct a ip/udp header for a packet, send packet */
15  int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
16                 uint32_t source_nip, int source_port,
17 @@ -173,10 +178,10 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
18  {
19         struct sockaddr_ll dest_sll;
20         struct ip_udp_dhcp_packet packet;
21 -       unsigned padding;
22         int fd;
23         int result = -1;
24         const char *msg;
25 +       int p_len = udhcp_get_payload_len(dhcp_pkt);
26  
27         fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
28         if (fd < 0) {
29 @@ -185,8 +190,8 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
30         }
31  
32         memset(&dest_sll, 0, sizeof(dest_sll));
33 -       memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data));
34 -       packet.data = *dhcp_pkt; /* struct copy */
35 +       memset(&packet, 0, sizeof(packet));
36 +       memcpy(&(packet.data), dhcp_pkt, p_len);
37  
38         dest_sll.sll_family = AF_PACKET;
39         dest_sll.sll_protocol = htons(ETH_P_IP);
40 @@ -199,36 +204,24 @@ int FAST_FUNC udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
41                 goto ret_close;
42         }
43  
44 -       /* We were sending full-sized DHCP packets (zero padded),
45 -        * but some badly configured servers were seen dropping them.
46 -        * Apparently they drop all DHCP packets >576 *ethernet* octets big,
47 -        * whereas they may only drop packets >576 *IP* octets big
48 -        * (which for typical Ethernet II means 590 octets: 6+6+2 + 576).
49 -        *
50 -        * In order to work with those buggy servers,
51 -        * we truncate packets after end option byte.
52 -        */
53 -       padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options);
54 -
55         packet.ip.protocol = IPPROTO_UDP;
56         packet.ip.saddr = source_nip;
57         packet.ip.daddr = dest_nip;
58         packet.udp.source = htons(source_port);
59         packet.udp.dest = htons(dest_port);
60 -       /* size, excluding IP header: */
61 -       packet.udp.len = htons(UDP_DHCP_SIZE - padding);
62 -       /* for UDP checksumming, ip.len is set to UDP packet len */
63 +       p_len += sizeof(packet.udp);
64 +       packet.udp.len = htons(p_len);
65         packet.ip.tot_len = packet.udp.len;
66 -       packet.udp.check = udhcp_checksum(&packet, IP_UDP_DHCP_SIZE - padding);
67 -       /* but for sending, it is set to IP packet len */
68 -       packet.ip.tot_len = htons(IP_UDP_DHCP_SIZE - padding);
69 +       p_len += sizeof(packet.ip);
70 +       packet.udp.check = udhcp_checksum(&packet, p_len);
71 +       packet.ip.tot_len = htons(p_len);
72         packet.ip.ihl = sizeof(packet.ip) >> 2;
73         packet.ip.version = IPVERSION;
74         packet.ip.ttl = IPDEFTTL;
75         packet.ip.check = udhcp_checksum(&packet.ip, sizeof(packet.ip));
76  
77         udhcp_dump_packet(dhcp_pkt);
78 -       result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0,
79 +       result = sendto(fd, &packet, p_len, /*flags:*/ 0,
80                         (struct sockaddr *) &dest_sll, sizeof(dest_sll));
81         msg = "sendto";
82   ret_close:
83 @@ -246,7 +239,6 @@ int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
84                 uint32_t dest_nip, int dest_port)
85  {
86         struct sockaddr_in client;
87 -       unsigned padding;
88         int fd;
89         int result = -1;
90         const char *msg;
91 @@ -277,9 +269,7 @@ int FAST_FUNC udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
92         }
93  
94         udhcp_dump_packet(dhcp_pkt);
95 -
96 -       padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options);
97 -       result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding);
98 +       result = safe_write(fd, dhcp_pkt, udhcp_get_payload_len(dhcp_pkt));
99         msg = "write";
100   ret_close:
101         close(fd);