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