e595162c6b4c5fbb259e73f85561b445e4a9e9dd
[openwrt.git] / package / busybox / patches / 950-partial-checksum.patch
1 diff -ur busybox-1.19.4.orig/networking/udhcp/dhcpc.c busybox-1.19.4/networking/udhcp/dhcpc.c
2 --- busybox-1.19.4.orig/networking/udhcp/dhcpc.c        2012-09-06 22:33:53.476998721 +0400
3 +++ busybox-1.19.4/networking/udhcp/dhcpc.c     2012-09-07 01:09:46.693372304 +0400
4 @@ -26,8 +26,8 @@
5  #include "dhcpc.h"
6  
7  #include <netinet/if_ether.h>
8 -#include <netpacket/packet.h>
9  #include <linux/filter.h>
10 +#include <linux/if_packet.h>
11  
12  /* struct client_config_t client_config is in bb_common_bufsiz1 */
13  
14 @@ -784,17 +784,41 @@
15  static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd)
16  {
17         int bytes;
18 +       int nocsum = 0;
19         struct ip_udp_dhcp_packet packet;
20         uint16_t check;
21 +       unsigned char cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
22 +       struct iovec iov = {
23 +               .iov_base = &packet,
24 +               .iov_len = sizeof(packet),
25 +       };
26 +       struct msghdr msg = {
27 +               .msg_iov = &iov,
28 +               .msg_iovlen = 1,
29 +               .msg_control = cmsgbuf,
30 +               .msg_controllen = sizeof(cmsgbuf),
31 +       };
32 +       struct cmsghdr *cmsg;
33  
34         memset(&packet, 0, sizeof(packet));
35 -       bytes = safe_read(fd, &packet, sizeof(packet));
36 +       do {
37 +               bytes = recvmsg(fd, &msg, 0);
38 +       } while (bytes < 0 && errno == EINTR);
39 +
40         if (bytes < 0) {
41                 log1("Packet read error, ignoring");
42                 /* NB: possible down interface, etc. Caller should pause. */
43                 return bytes; /* returns -1 */
44         }
45  
46 +       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
47 +               if (cmsg->cmsg_level == SOL_PACKET &&
48 +                       cmsg->cmsg_type == PACKET_AUXDATA) {
49 +                       struct tpacket_auxdata *aux = (void *)CMSG_DATA(cmsg);
50 +                       nocsum = aux->tp_status & TP_STATUS_CSUMNOTREADY;
51 +               }
52 +       }
53 +
54         if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) {
55                 log1("Packet is too short, ignoring");
56                 return -2;
57 @@ -834,7 +858,7 @@
58         packet.ip.tot_len = packet.udp.len; /* yes, this is needed */
59         check = packet.udp.check;
60         packet.udp.check = 0;
61 -       if (check && check != udhcp_checksum(&packet, bytes)) {
62 +       if (!nocsum && check && check != udhcp_checksum(&packet, bytes)) {
63                 log1("Packet with bad UDP checksum received, ignoring");
64                 return -2;
65         }
66 @@ -880,6 +904,7 @@
67  {
68         int fd;
69         struct sockaddr_ll sock;
70 +       int val;
71  
72         /*
73          * Comment:
74 @@ -946,6 +971,13 @@
75                         log1("Attached filter to raw socket fd %d", fd); // log?
76         }
77  
78 +       val = 1;
79 +       if (setsockopt(fd, SOL_PACKET, PACKET_AUXDATA, &val,
80 +                       sizeof(val)) < 0) {
81 +               if (errno != ENOPROTOOPT)
82 +                       log1("Failed to set auxiliary packet data for socket fd %d", fd);
83 +       }
84 +
85         log1("Created raw socket");
86  
87         return fd;