Fix floating point error for unhandled dhcp options
[framework/connectivity/connman.git] / gdhcp / common.c
1 /*
2  *  DHCP library with GLib integration
3  *
4  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <errno.h>
26 #include <unistd.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <endian.h>
30 #include <netpacket/packet.h>
31 #include <net/ethernet.h>
32
33 #include "gdhcp.h"
34 #include "common.h"
35
36 static const DHCPOption client_options[] = {
37         { OPTION_IP,                    0x01 }, /* subnet-mask */
38         { OPTION_IP | OPTION_LIST,      0x03 }, /* routers */
39         { OPTION_IP | OPTION_LIST,      0x06 }, /* domain-name-servers */
40         { OPTION_STRING,                0x0c }, /* hostname */
41         { OPTION_STRING,                0x0f }, /* domain-name */
42         { OPTION_IP | OPTION_LIST,      0x2a }, /* ntp-servers */
43         { OPTION_U32,                   0x33 }, /* dhcp-lease-time */
44         /* Options below will not be exposed to user */
45         { OPTION_IP,                    0x32 }, /* requested-ip */
46         { OPTION_U8,                    0x35 }, /* message-type */
47         { OPTION_U16,                   0x39 }, /* max-size */
48         { OPTION_STRING,                0x3c }, /* vendor */
49         { OPTION_STRING,                0x3d }, /* client-id */
50         { OPTION_UNKNOWN,               0x00 },
51 };
52
53 GDHCPOptionType dhcp_get_code_type(uint8_t code)
54 {
55         int i;
56
57         for (i = 0; client_options[i].code; i++) {
58                 if (client_options[i].code == code)
59                         return client_options[i].type;
60         }
61
62         return OPTION_UNKNOWN;
63 }
64
65 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code)
66 {
67         int len, rem;
68         uint8_t *optionptr;
69         uint8_t overload = 0;
70
71         /* option bytes: [code][len][data1][data2]..[dataLEN] */
72         optionptr = packet->options;
73         rem = sizeof(packet->options);
74
75         while (1) {
76                 if (rem <= 0)
77                         /* Bad packet, malformed option field */
78                         return NULL;
79
80                 if (optionptr[OPT_CODE] == DHCP_PADDING) {
81                         rem--;
82                         optionptr++;
83
84                         continue;
85                 }
86
87                 if (optionptr[OPT_CODE] == DHCP_END) {
88                         if (overload & FILE_FIELD) {
89                                 overload &= ~FILE_FIELD;
90
91                                 optionptr = packet->file;
92                                 rem = sizeof(packet->file);
93
94                                 continue;
95                         } else if (overload & SNAME_FIELD) {
96                                 overload &= ~SNAME_FIELD;
97
98                                 optionptr = packet->sname;
99                                 rem = sizeof(packet->sname);
100
101                                 continue;
102                         }
103
104                         break;
105                 }
106
107                 len = 2 + optionptr[OPT_LEN];
108
109                 rem -= len;
110                 if (rem < 0)
111                         continue; /* complain and return NULL */
112
113                 if (optionptr[OPT_CODE] == code)
114                         return optionptr + OPT_DATA;
115
116                 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD)
117                         overload |= optionptr[OPT_DATA];
118
119                 optionptr += len;
120         }
121
122         return NULL;
123 }
124
125 int dhcp_end_option(uint8_t *optionptr)
126 {
127         int i = 0;
128
129         while (optionptr[i] != DHCP_END) {
130                 if (optionptr[i] != DHCP_PADDING)
131                         i += optionptr[i + OPT_LEN] + OPT_DATA - 1;
132
133                 i++;
134         }
135
136         return i;
137 }
138
139 /*
140  * Add an option (supplied in binary form) to the options.
141  * Option format: [code][len][data1][data2]..[dataLEN]
142  */
143 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
144 {
145         unsigned len;
146         uint8_t *optionptr = packet->options;
147         unsigned end = dhcp_end_option(optionptr);
148
149         len = OPT_DATA + addopt[OPT_LEN];
150
151         /* end position + (option code/length + addopt length) + end option */
152         if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE)
153                 /* option did not fit into the packet */
154                 return;
155
156         memcpy(optionptr + end, addopt, len);
157
158         optionptr[end + len] = DHCP_END;
159 }
160
161 void dhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code,
162                                                         uint32_t data)
163 {
164         uint8_t option[6], len;
165         GDHCPOptionType type = dhcp_get_code_type(code);
166
167         if (type == OPTION_UNKNOWN)
168                 return;
169
170         option[OPT_CODE] = code;
171
172         len = dhcp_option_lengths[type & OPTION_TYPE_MASK];
173         option[OPT_LEN] = len;
174
175 #if __BYTE_ORDER == __BIG_ENDIAN
176         data <<= 8 * (4 - len);
177 #endif
178
179         dhcp_put_unaligned(data, (uint32_t *) &option[OPT_DATA]);
180         dhcp_add_binary_option(packet, option);
181
182         return;
183 }
184
185 void dhcp_init_header(struct dhcp_packet *packet, char type)
186 {
187         memset(packet, 0, sizeof(*packet));
188
189         packet->op = BOOTREQUEST;
190
191         switch (type) {
192         case DHCPOFFER:
193         case DHCPACK:
194         case DHCPNAK:
195                 packet->op = BOOTREPLY;
196         }
197
198         packet->htype = 1;
199         packet->hlen = 6;
200         packet->cookie = htonl(DHCP_MAGIC);
201         packet->options[0] = DHCP_END;
202
203         dhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
204 }
205
206 static gboolean check_vendor(uint8_t  *option_vendor, const char *vendor)
207 {
208         uint8_t vendor_length = sizeof(vendor) - 1;
209
210         if (option_vendor[OPT_LEN - OPT_DATA] != vendor_length)
211                 return FALSE;
212
213         if (memcmp(option_vendor, vendor, vendor_length) != 0)
214                 return FALSE;
215
216         return TRUE;
217 }
218
219 static void check_broken_vendor(struct dhcp_packet *packet)
220 {
221         uint8_t *vendor;
222
223         if (packet->op != BOOTREQUEST)
224                 return;
225
226         vendor = dhcp_get_option(packet, DHCP_VENDOR);
227         if (vendor == NULL)
228                 return;
229
230         if (check_vendor(vendor, "MSFT 98") == TRUE)
231                 packet->flags |= htons(BROADCAST_FLAG);
232 }
233
234 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd)
235 {
236         int n;
237
238         memset(packet, 0, sizeof(*packet));
239
240         n = read(fd, packet, sizeof(*packet));
241         if (n < 0)
242                 return -errno;
243
244         if (packet->cookie != htonl(DHCP_MAGIC))
245                 return -EPROTO;
246
247         check_broken_vendor(packet);
248
249         return n;
250 }
251
252 /* TODO: Use glib checksum */
253 uint16_t dhcp_checksum(void *addr, int count)
254 {
255         /*
256          * Compute Internet Checksum for "count" bytes
257          * beginning at location "addr".
258          */
259         int32_t sum = 0;
260         uint16_t *source = (uint16_t *) addr;
261
262         while (count > 1)  {
263                 /*  This is the inner loop */
264                 sum += *source++;
265                 count -= 2;
266         }
267
268         /*  Add left-over byte, if any */
269         if (count > 0) {
270                 /* Make sure that the left-over byte is added correctly both
271                  * with little and big endian hosts */
272                 uint16_t tmp = 0;
273                 *(uint8_t *) &tmp = *(uint8_t *) source;
274                 sum += tmp;
275         }
276         /*  Fold 32-bit sum to 16 bits */
277         while (sum >> 16)
278                 sum = (sum & 0xffff) + (sum >> 16);
279
280         return ~sum;
281 }
282
283 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
284                 uint32_t source_ip, int source_port, uint32_t dest_ip,
285                         int dest_port, const uint8_t *dest_arp, int ifindex)
286 {
287         struct sockaddr_ll dest;
288         struct ip_udp_dhcp_packet packet;
289         int fd, n;
290
291         enum {
292                 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) -
293                                                 EXTEND_FOR_BUGGY_SERVERS,
294                 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE -
295                                 offsetof(struct ip_udp_dhcp_packet, udp),
296         };
297
298         fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
299         if (fd < 0)
300                 return -errno;
301
302         memset(&dest, 0, sizeof(dest));
303         memset(&packet, 0, sizeof(packet));
304         packet.data = *dhcp_pkt;
305
306         dest.sll_family = AF_PACKET;
307         dest.sll_protocol = htons(ETH_P_IP);
308         dest.sll_ifindex = ifindex;
309         dest.sll_halen = 6;
310         memcpy(dest.sll_addr, dest_arp, 6);
311         if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
312                 close(fd);
313                 return -errno;
314         }
315
316         packet.ip.protocol = IPPROTO_UDP;
317         packet.ip.saddr = source_ip;
318         packet.ip.daddr = dest_ip;
319         packet.udp.source = htons(source_port);
320         packet.udp.dest = htons(dest_port);
321         /* size, excluding IP header: */
322         packet.udp.len = htons(UPD_DHCP_SIZE);
323         /* for UDP checksumming, ip.len is set to UDP packet len */
324         packet.ip.tot_len = packet.udp.len;
325         packet.udp.check = dhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
326         /* but for sending, it is set to IP packet len */
327         packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
328         packet.ip.ihl = sizeof(packet.ip) >> 2;
329         packet.ip.version = IPVERSION;
330         packet.ip.ttl = IPDEFTTL;
331         packet.ip.check = dhcp_checksum(&packet.ip, sizeof(packet.ip));
332
333         /*
334          * Currently we send full-sized DHCP packets (zero padded).
335          * If you need to change this: last byte of the packet is
336          * packet.data.options[dhcp_end_option(packet.data.options)]
337          */
338         n = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
339                         (struct sockaddr *) &dest, sizeof(dest));
340         if (n < 0)
341                 return -errno;
342
343         close(fd);
344
345         return n;
346 }
347
348 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
349                                 uint32_t source_ip, int source_port,
350                                 uint32_t dest_ip, int dest_port)
351 {
352         struct sockaddr_in client;
353         int fd, n, opt = 1;
354
355         enum {
356                 DHCP_SIZE = sizeof(struct dhcp_packet) -
357                                         EXTEND_FOR_BUGGY_SERVERS,
358         };
359
360         fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
361         if (fd < 0)
362                 return -errno;
363
364         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
365
366         memset(&client, 0, sizeof(client));
367         client.sin_family = AF_INET;
368         client.sin_port = htons(source_port);
369         client.sin_addr.s_addr = source_ip;
370         if (bind(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
371                 close(fd);
372                 return -errno;
373         }
374
375         memset(&client, 0, sizeof(client));
376         client.sin_family = AF_INET;
377         client.sin_port = htons(dest_port);
378         client.sin_addr.s_addr = dest_ip;
379         if (connect(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
380                 close(fd);
381                 return -errno;
382         }
383
384         n = write(fd, dhcp_pkt, DHCP_SIZE);
385
386         close(fd);
387
388         if (n < 0)
389                 return -errno;
390
391         return n;
392 }
393
394 int dhcp_l3_socket(int port, const char *interface)
395 {
396         int fd, opt = 1;
397         struct sockaddr_in addr;
398
399         fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
400
401         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
402
403         if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
404                                 interface, strlen(interface) + 1) < 0) {
405                 close(fd);
406                 return -1;
407         }
408
409         memset(&addr, 0, sizeof(addr));
410         addr.sin_family = AF_INET;
411         addr.sin_port = htons(port);
412         if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
413                 close(fd);
414                 return -1;
415         }
416
417         return fd;
418 }