Add macosx fix from #1407
[openwrt.git] / package / busybox / patches / 241-udhcpc-oversized_packets.patch
1 diff -ruN busybox-1.3.1-old/networking/udhcp/packet.c busybox-1.3.1/networking/udhcp/packet.c
2 --- busybox-1.3.1-old/networking/udhcp/packet.c 2006-12-27 05:52:33.000000000 +0100
3 +++ busybox-1.3.1/networking/udhcp/packet.c     2006-12-28 05:38:36.000000000 +0100
4 @@ -107,6 +107,10 @@
5         return ~sum;
6  }
7  
8 +int udhcp_get_payload_len(struct dhcpMessage *payload)
9 +{
10 +       return sizeof(struct dhcpMessage) - MAX_OPTIONS_LEN + end_option(payload->options) + sizeof(payload->options[0]);
11 +}
12  
13  /* Construct a ip/udp header for a packet, and specify the source and dest hardware address */
14  void BUG_sizeof_struct_udp_dhcp_packet_must_be_576(void);
15 @@ -118,6 +122,7 @@
16         int result;
17         struct sockaddr_ll dest;
18         struct udp_dhcp_packet packet;
19 +       int p_len = udhcp_get_payload_len(payload);
20  
21         fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
22         if (fd < 0) {
23 @@ -127,6 +132,7 @@
24  
25         memset(&dest, 0, sizeof(dest));
26         memset(&packet, 0, sizeof(packet));
27 +       memcpy(&(packet.data), payload, p_len);
28  
29         dest.sll_family = AF_PACKET;
30         dest.sll_protocol = htons(ETH_P_IP);
31 @@ -144,12 +150,13 @@
32         packet.ip.daddr = dest_ip;
33         packet.udp.source = htons(source_port);
34         packet.udp.dest = htons(dest_port);
35 -       packet.udp.len = htons(sizeof(packet.udp) + sizeof(struct dhcpMessage)); /* cheat on the psuedo-header */
36 +       p_len += sizeof(packet.udp);
37 +       packet.udp.len = htons(p_len);
38         packet.ip.tot_len = packet.udp.len;
39 -       memcpy(&(packet.data), payload, sizeof(struct dhcpMessage));
40 -       packet.udp.check = udhcp_checksum(&packet, sizeof(struct udp_dhcp_packet));
41 +       p_len += sizeof(packet.ip);
42 +       packet.udp.check = udhcp_checksum(&packet, p_len);
43  
44 -       packet.ip.tot_len = htons(sizeof(struct udp_dhcp_packet));
45 +       packet.ip.tot_len = htons(p_len);
46         packet.ip.ihl = sizeof(packet.ip) >> 2;
47         packet.ip.version = IPVERSION;
48         packet.ip.ttl = IPDEFTTL;
49 @@ -158,7 +165,7 @@
50         if (sizeof(struct udp_dhcp_packet) != 576)
51                 BUG_sizeof_struct_udp_dhcp_packet_must_be_576();
52  
53 -       result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0,
54 +       result = sendto(fd, &packet, p_len, 0,
55                         (struct sockaddr *) &dest, sizeof(dest));
56         if (result <= 0) {
57                 bb_perror_msg("sendto");
58 @@ -205,7 +212,7 @@
59                 return -1;
60         }
61  
62 -       result = write(fd, payload, sizeof(struct dhcpMessage));
63 +       result = write(fd, payload, udhcp_get_payload_len(payload));
64         close(fd);
65         return result;
66  }
67 diff -ruN busybox-1.3.1-old/networking/udhcp/common.h busybox-1.3.1/networking/udhcp/common.h
68 --- busybox-1.3.1-old/networking/udhcp/common.h 2006-12-27 05:52:33.000000000 +0100
69 +++ busybox-1.3.1/networking/udhcp/common.h     2006-12-28 05:17:06.000000000 +0100
70 @@ -26,6 +26,8 @@
71  #include <netinet/udp.h>
72  #include <netinet/ip.h>
73  
74 +#define MAX_OPTIONS_LEN  308
75 +
76  struct dhcpMessage {
77         uint8_t op;
78         uint8_t htype;
79 @@ -42,7 +44,7 @@
80         uint8_t sname[64];
81         uint8_t file[128];
82         uint32_t cookie;
83 -       uint8_t options[308]; /* 312 - cookie */
84 +       uint8_t options[MAX_OPTIONS_LEN]; /* 312 - cookie */
85  };
86  
87  struct udp_dhcp_packet {