xref: /OK3568_Linux_fs/app/forlinx/quectelCM/libmnl/dhcp/packet.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2008, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/socket.h>
21 #include <sys/uio.h>
22 #include <linux/if_ether.h>
23 #include <linux/if_packet.h>
24 #include <netinet/in.h>
25 #include <netinet/ip.h>
26 #include <netinet/udp.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 
30 #include "dhcpmsg.h"
31 
32 int fatal();
33 
open_raw_socket(const char * ifname,uint8_t * hwaddr,int if_index)34 int open_raw_socket(const char *ifname __attribute__((unused)), uint8_t *hwaddr, int if_index)
35 {
36     int s;
37     struct sockaddr_ll bindaddr;
38 
39     if((s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
40         return fatal("socket(PF_PACKET)");
41     }
42 
43     memset(&bindaddr, 0, sizeof(bindaddr));
44     bindaddr.sll_family = AF_PACKET;
45     bindaddr.sll_protocol = htons(ETH_P_IP);
46     bindaddr.sll_halen = ETH_ALEN;
47     memcpy(bindaddr.sll_addr, hwaddr, ETH_ALEN);
48     bindaddr.sll_ifindex = if_index;
49 
50     if (bind(s, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
51         return fatal("Cannot bind raw socket to interface");
52     }
53 
54     return s;
55 }
56 
checksum(void * buffer,unsigned int count,uint32_t startsum)57 static uint32_t checksum(void *buffer, unsigned int count, uint32_t startsum)
58 {
59     uint16_t *up = (uint16_t *)buffer;
60     uint32_t sum = startsum;
61     uint32_t upper16;
62 
63     while (count > 1) {
64         sum += *up++;
65         count -= 2;
66     }
67     if (count > 0) {
68         sum += (uint16_t) *(uint8_t *)up;
69     }
70     while ((upper16 = (sum >> 16)) != 0) {
71         sum = (sum & 0xffff) + upper16;
72     }
73     return sum;
74 }
75 
finish_sum(uint32_t sum)76 static uint32_t finish_sum(uint32_t sum)
77 {
78     return ~sum & 0xffff;
79 }
80 
send_packet(int s,int if_index,struct dhcp_msg * msg,int size,uint32_t saddr,uint32_t daddr,uint32_t sport,uint32_t dport)81 int send_packet(int s, int if_index, struct dhcp_msg *msg, int size,
82                 uint32_t saddr, uint32_t daddr, uint32_t sport, uint32_t dport)
83 {
84     struct iphdr ip;
85     struct udphdr udp;
86     struct iovec iov[3];
87     uint32_t udpsum;
88     uint16_t temp;
89     struct msghdr msghdr;
90     struct sockaddr_ll destaddr;
91 
92     ip.version = IPVERSION;
93     ip.ihl = sizeof(ip) >> 2;
94     ip.tos = 0;
95     ip.tot_len = htons(sizeof(ip) + sizeof(udp) + size);
96     ip.id = 0;
97     ip.frag_off = 0;
98     ip.ttl = IPDEFTTL;
99     ip.protocol = IPPROTO_UDP;
100     ip.check = 0;
101     ip.saddr = saddr;
102     ip.daddr = daddr;
103     ip.check = finish_sum(checksum(&ip, sizeof(ip), 0));
104 
105     udp.source = htons(sport);
106     udp.dest = htons(dport);
107     udp.len = htons(sizeof(udp) + size);
108     udp.check = 0;
109 
110     /* Calculate checksum for pseudo header */
111     udpsum = checksum(&ip.saddr, sizeof(ip.saddr), 0);
112     udpsum = checksum(&ip.daddr, sizeof(ip.daddr), udpsum);
113     temp = htons(IPPROTO_UDP);
114     udpsum = checksum(&temp, sizeof(temp), udpsum);
115     temp = udp.len;
116     udpsum = checksum(&temp, sizeof(temp), udpsum);
117 
118     /* Add in the checksum for the udp header */
119     udpsum = checksum(&udp, sizeof(udp), udpsum);
120 
121     /* Add in the checksum for the data */
122     udpsum = checksum(msg, size, udpsum);
123     udp.check = finish_sum(udpsum);
124 
125     iov[0].iov_base = (char *)&ip;
126     iov[0].iov_len = sizeof(ip);
127     iov[1].iov_base = (char *)&udp;
128     iov[1].iov_len = sizeof(udp);
129     iov[2].iov_base = (char *)msg;
130     iov[2].iov_len = size;
131     memset(&destaddr, 0, sizeof(destaddr));
132     destaddr.sll_family = AF_PACKET;
133     destaddr.sll_protocol = htons(ETH_P_IP);
134     destaddr.sll_ifindex = if_index;
135     destaddr.sll_halen = ETH_ALEN;
136     memcpy(destaddr.sll_addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN);
137 
138     msghdr.msg_name = &destaddr;
139     msghdr.msg_namelen = sizeof(destaddr);
140     msghdr.msg_iov = iov;
141     msghdr.msg_iovlen = sizeof(iov) / sizeof(struct iovec);
142     msghdr.msg_flags = 0;
143     msghdr.msg_control = 0;
144     msghdr.msg_controllen = 0;
145     return sendmsg(s, &msghdr, 0);
146 }
147 
receive_packet(int s,struct dhcp_msg * msg)148 int receive_packet(int s, struct dhcp_msg *msg)
149 {
150     int nread;
151     int is_valid;
152     struct dhcp_packet {
153         struct iphdr ip;
154         struct udphdr udp;
155         struct dhcp_msg dhcp;
156     } packet;
157     int dhcp_size;
158     uint32_t sum;
159     uint16_t temp;
160     uint32_t saddr, daddr;
161 
162     nread = read(s, &packet, sizeof(packet));
163     if (nread < 0) {
164         return -1;
165     }
166     /*
167      * The raw packet interface gives us all packets received by the
168      * network interface. We need to filter out all packets that are
169      * not meant for us.
170      */
171     is_valid = 0;
172     if (nread < (int)(sizeof(struct iphdr) + sizeof(struct udphdr))) {
173 #if VERBOSE
174         ALOGD("Packet is too small (%d) to be a UDP datagram", nread);
175 #endif
176     } else if (packet.ip.version != IPVERSION || packet.ip.ihl != (sizeof(packet.ip) >> 2)) {
177 #if VERBOSE
178         ALOGD("Not a valid IP packet");
179 #endif
180     } else if (nread < ntohs(packet.ip.tot_len)) {
181 #if VERBOSE
182         ALOGD("Packet was truncated (read %d, needed %d)", nread, ntohs(packet.ip.tot_len));
183 #endif
184     } else if (packet.ip.protocol != IPPROTO_UDP) {
185 #if VERBOSE
186         ALOGD("IP protocol (%d) is not UDP", packet.ip.protocol);
187 #endif
188     } else if (packet.udp.dest != htons(PORT_BOOTP_CLIENT)) {
189 #if VERBOSE
190         ALOGD("UDP dest port (%d) is not DHCP client", ntohs(packet.udp.dest));
191 #endif
192     } else {
193         is_valid = 1;
194     }
195 
196     if (!is_valid) {
197         return -1;
198     }
199 
200     /* Seems like it's probably a valid DHCP packet */
201     /* validate IP header checksum */
202     sum = finish_sum(checksum(&packet.ip, sizeof(packet.ip), 0));
203     if (sum != 0) {
204         printf("IP header checksum failure (0x%x)\n", packet.ip.check);
205         return -1;
206     }
207     /*
208      * Validate the UDP checksum.
209      * Since we don't need the IP header anymore, we "borrow" it
210      * to construct the pseudo header used in the checksum calculation.
211      */
212     dhcp_size = ntohs(packet.udp.len) - sizeof(packet.udp);
213     /*
214      * check validity of dhcp_size.
215      * 1) cannot be negative or zero.
216      * 2) src buffer contains enough bytes to copy
217      * 3) cannot exceed destination buffer
218      */
219     if ((dhcp_size <= 0) ||
220         ((int)(nread - sizeof(struct iphdr) - sizeof(struct udphdr)) < dhcp_size) ||
221         ((int)sizeof(struct dhcp_msg) < dhcp_size)) {
222 #if VERBOSE
223         printf("Malformed Packet\n");
224 #endif
225         return -1;
226     }
227     saddr = packet.ip.saddr;
228     daddr = packet.ip.daddr;
229     nread = ntohs(packet.ip.tot_len);
230     memset(&packet.ip, 0, sizeof(packet.ip));
231     packet.ip.saddr = saddr;
232     packet.ip.daddr = daddr;
233     packet.ip.protocol = IPPROTO_UDP;
234     packet.ip.tot_len = packet.udp.len;
235     temp = packet.udp.check;
236     packet.udp.check = 0;
237     sum = finish_sum(checksum(&packet, nread, 0));
238     packet.udp.check = temp;
239     if (!sum)
240         sum = finish_sum(sum);
241     if (temp != sum) {
242         printf("UDP header checksum failure (0x%x should be 0x%x)\n", sum, temp);
243         return -1;
244     }
245     memcpy(msg, &packet.dhcp, dhcp_size);
246     return dhcp_size;
247 }
248