technology: return already enabled when tethering is enabled
[framework/connectivity/connman.git] / gdhcp / common.c
1 /*
2  *  DHCP library with GLib integration
3  *
4  *  Copyright (C) 2007-2012  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 <stdio.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <sys/ioctl.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <endian.h>
33 #include <net/if_arp.h>
34 #include <linux/if.h>
35 #include <netpacket/packet.h>
36 #include <net/ethernet.h>
37 #include <arpa/inet.h>
38
39 #include "gdhcp.h"
40 #include "common.h"
41
42 static const DHCPOption client_options[] = {
43         { OPTION_IP,                    0x01 }, /* subnet-mask */
44         { OPTION_IP | OPTION_LIST,      0x03 }, /* routers */
45         { OPTION_IP | OPTION_LIST,      0x06 }, /* domain-name-servers */
46         { OPTION_STRING,                0x0c }, /* hostname */
47         { OPTION_STRING,                0x0f }, /* domain-name */
48         { OPTION_IP | OPTION_LIST,      0x2a }, /* ntp-servers */
49         { OPTION_U32,                   0x33 }, /* dhcp-lease-time */
50         /* Options below will not be exposed to user */
51         { OPTION_IP,                    0x32 }, /* requested-ip */
52         { OPTION_U8,                    0x35 }, /* message-type */
53         { OPTION_U32,                   0x36 }, /* server-id */
54         { OPTION_U16,                   0x39 }, /* max-size */
55         { OPTION_STRING,                0x3c }, /* vendor */
56         { OPTION_STRING,                0x3d }, /* client-id */
57         { OPTION_STRING,                0xfc }, /* UNOFFICIAL proxy-pac */
58         { OPTION_UNKNOWN,               0x00 },
59 };
60
61 GDHCPOptionType dhcp_get_code_type(uint8_t code)
62 {
63         int i;
64
65         for (i = 0; client_options[i].code; i++) {
66                 if (client_options[i].code == code)
67                         return client_options[i].type;
68         }
69
70         return OPTION_UNKNOWN;
71 }
72
73 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code)
74 {
75         int len, rem;
76         uint8_t *optionptr;
77         uint8_t overload = 0;
78
79         /* option bytes: [code][len][data1][data2]..[dataLEN] */
80         optionptr = packet->options;
81         rem = sizeof(packet->options);
82
83         while (1) {
84                 if (rem <= 0)
85                         /* Bad packet, malformed option field */
86                         return NULL;
87
88                 if (optionptr[OPT_CODE] == DHCP_PADDING) {
89                         rem--;
90                         optionptr++;
91
92                         continue;
93                 }
94
95                 if (optionptr[OPT_CODE] == DHCP_END) {
96                         if (overload & FILE_FIELD) {
97                                 overload &= ~FILE_FIELD;
98
99                                 optionptr = packet->file;
100                                 rem = sizeof(packet->file);
101
102                                 continue;
103                         } else if (overload & SNAME_FIELD) {
104                                 overload &= ~SNAME_FIELD;
105
106                                 optionptr = packet->sname;
107                                 rem = sizeof(packet->sname);
108
109                                 continue;
110                         }
111
112                         break;
113                 }
114
115                 len = 2 + optionptr[OPT_LEN];
116
117                 rem -= len;
118                 if (rem < 0)
119                         continue; /* complain and return NULL */
120
121                 if (optionptr[OPT_CODE] == code)
122                         return optionptr + OPT_DATA;
123
124                 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD)
125                         overload |= optionptr[OPT_DATA];
126
127                 optionptr += len;
128         }
129
130         return NULL;
131 }
132
133 int dhcp_end_option(uint8_t *optionptr)
134 {
135         int i = 0;
136
137         while (optionptr[i] != DHCP_END) {
138                 if (optionptr[i] != DHCP_PADDING)
139                         i += optionptr[i + OPT_LEN] + OPT_DATA - 1;
140
141                 i++;
142         }
143
144         return i;
145 }
146
147 uint8_t *dhcpv6_get_option(struct dhcpv6_packet *packet, uint16_t pkt_len,
148                         int code, uint16_t *option_len, int *option_count)
149 {
150         int rem, count = 0;
151         uint8_t *optionptr, *found = NULL;
152         uint16_t opt_code, opt_len, len;
153
154         optionptr = packet->options;
155         rem = pkt_len - 1 - 3;
156
157         if (rem <= 0)
158                 goto bad_packet;
159
160         while (1) {
161                 opt_code = optionptr[0] << 8 | optionptr[1];
162                 opt_len = len = optionptr[2] << 8 | optionptr[3];
163                 len += 2 + 2; /* skip code and len */
164
165                 if (len < 4)
166                         goto bad_packet;
167
168                 rem -= len;
169                 if (rem < 0)
170                         break;
171
172                 if (opt_code == code) {
173                         if (option_len != NULL)
174                                 *option_len = opt_len;
175                         if (rem == 0)
176                                 found = NULL;
177                         else
178                                 found = optionptr + 2 + 2;
179                         count++;
180                 }
181
182                 if (rem == 0)
183                         break;
184
185                 optionptr += len;
186         }
187
188         if (option_count != NULL)
189                 *option_count = count;
190
191         return found;
192
193 bad_packet:
194         if (option_len != NULL)
195                 *option_len = 0;
196         if (option_count != NULL)
197                 *option_count = 0;
198         return NULL;
199 }
200
201 uint8_t *dhcpv6_get_sub_option(unsigned char *option, uint16_t max_len,
202                         uint16_t *option_code, uint16_t *option_len)
203 {
204         int rem;
205         uint16_t code, len;
206
207         rem = max_len - 2 - 2;
208
209         if (rem <= 0)
210                 /* Bad option */
211                 return NULL;
212
213         code = option[0] << 8 | option[1];
214         len = option[2] << 8 | option[3];
215
216         rem -= len;
217         if (rem < 0)
218                 return NULL;
219
220         *option_code = code;
221         *option_len = len;
222
223         return &option[4];
224 }
225
226 /*
227  * Add an option (supplied in binary form) to the options.
228  * Option format: [code][len][data1][data2]..[dataLEN]
229  */
230 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
231 {
232         unsigned len;
233         uint8_t *optionptr = packet->options;
234         unsigned end = dhcp_end_option(optionptr);
235
236         len = OPT_DATA + addopt[OPT_LEN];
237
238         /* end position + (option code/length + addopt length) + end option */
239         if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE)
240                 /* option did not fit into the packet */
241                 return;
242
243         memcpy(optionptr + end, addopt, len);
244
245         optionptr[end + len] = DHCP_END;
246 }
247
248 /*
249  * Add an option (supplied in binary form) to the options.
250  * Option format: [code][len][data1][data2]..[dataLEN]
251  */
252 void dhcpv6_add_binary_option(struct dhcpv6_packet *packet, uint16_t max_len,
253                                 uint16_t *pkt_len, uint8_t *addopt)
254 {
255         unsigned len;
256         uint8_t *optionptr = packet->options;
257
258         len = 2 + 2 + (addopt[2] << 8 | addopt[3]);
259
260         /* end position + (option code/length + addopt length) */
261         if (*pkt_len + len >= max_len)
262                 /* option did not fit into the packet */
263                 return;
264
265         memcpy(optionptr + *pkt_len, addopt, len);
266         *pkt_len += len;
267 }
268
269 void dhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code,
270                                                         uint32_t data)
271 {
272         uint8_t option[6], len;
273         GDHCPOptionType type = dhcp_get_code_type(code);
274
275         if (type == OPTION_UNKNOWN)
276                 return;
277
278         option[OPT_CODE] = code;
279
280         len = dhcp_option_lengths[type & OPTION_TYPE_MASK];
281         option[OPT_LEN] = len;
282
283 #if __BYTE_ORDER == __BIG_ENDIAN
284         data <<= 8 * (4 - len);
285 #endif
286
287         dhcp_put_unaligned(data, (uint32_t *)(option + OPT_DATA));
288         dhcp_add_binary_option(packet, option);
289
290         return;
291 }
292
293 void dhcp_init_header(struct dhcp_packet *packet, char type)
294 {
295         memset(packet, 0, sizeof(*packet));
296
297         packet->op = BOOTREQUEST;
298
299         switch (type) {
300         case DHCPOFFER:
301         case DHCPACK:
302         case DHCPNAK:
303                 packet->op = BOOTREPLY;
304         }
305
306         packet->htype = 1;
307         packet->hlen = 6;
308         packet->cookie = htonl(DHCP_MAGIC);
309         packet->options[0] = DHCP_END;
310
311         dhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type);
312 }
313
314 void dhcpv6_init_header(struct dhcpv6_packet *packet, uint8_t type)
315 {
316         int id;
317
318         memset(packet, 0, sizeof(*packet));
319
320         packet->message = type;
321
322         id = random();
323
324         packet->transaction_id[0] = (id >> 16) & 0xff;
325         packet->transaction_id[1] = (id >> 8) & 0xff;
326         packet->transaction_id[2] = id & 0xff;
327 }
328
329 static gboolean check_vendor(uint8_t  *option_vendor, const char *vendor)
330 {
331         uint8_t vendor_length = sizeof(vendor) - 1;
332
333         if (option_vendor[OPT_LEN - OPT_DATA] != vendor_length)
334                 return FALSE;
335
336         if (memcmp(option_vendor, vendor, vendor_length) != 0)
337                 return FALSE;
338
339         return TRUE;
340 }
341
342 static void check_broken_vendor(struct dhcp_packet *packet)
343 {
344         uint8_t *vendor;
345
346         if (packet->op != BOOTREQUEST)
347                 return;
348
349         vendor = dhcp_get_option(packet, DHCP_VENDOR);
350         if (vendor == NULL)
351                 return;
352
353         if (check_vendor(vendor, "MSFT 98") == TRUE)
354                 packet->flags |= htons(BROADCAST_FLAG);
355 }
356
357 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd)
358 {
359         int n;
360
361         memset(packet, 0, sizeof(*packet));
362
363         n = read(fd, packet, sizeof(*packet));
364         if (n < 0)
365                 return -errno;
366
367         if (packet->cookie != htonl(DHCP_MAGIC))
368                 return -EPROTO;
369
370         check_broken_vendor(packet);
371
372         return n;
373 }
374
375 int dhcpv6_recv_l3_packet(struct dhcpv6_packet **packet, unsigned char *buf,
376                         int buf_len, int fd)
377 {
378         int n;
379
380         n = read(fd, buf, buf_len);
381         if (n < 0)
382                 return -errno;
383
384         *packet = (struct dhcpv6_packet *)buf;
385
386         return n;
387 }
388
389 /* TODO: Use glib checksum */
390 uint16_t dhcp_checksum(void *addr, int count)
391 {
392         /*
393          * Compute Internet Checksum for "count" bytes
394          * beginning at location "addr".
395          */
396         int32_t sum = 0;
397         uint16_t *source = (uint16_t *) addr;
398
399         while (count > 1)  {
400                 /*  This is the inner loop */
401                 sum += *source++;
402                 count -= 2;
403         }
404
405         /*  Add left-over byte, if any */
406         if (count > 0) {
407                 /* Make sure that the left-over byte is added correctly both
408                  * with little and big endian hosts */
409                 uint16_t tmp = 0;
410                 *(uint8_t *) &tmp = *(uint8_t *) source;
411                 sum += tmp;
412         }
413         /*  Fold 32-bit sum to 16 bits */
414         while (sum >> 16)
415                 sum = (sum & 0xffff) + (sum >> 16);
416
417         return ~sum;
418 }
419
420 #define IN6ADDR_ALL_DHCP_RELAY_AGENTS_AND_SERVERS_MC_INIT \
421         { { { 0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0x1,0,0x2 } } } /* ff02::1:2 */
422 static const struct in6_addr in6addr_all_dhcp_relay_agents_and_servers_mc =
423         IN6ADDR_ALL_DHCP_RELAY_AGENTS_AND_SERVERS_MC_INIT;
424
425 /* from netinet/in.h */
426 struct in6_pktinfo {
427         struct in6_addr ipi6_addr;  /* src/dst IPv6 address */
428         unsigned int ipi6_ifindex;  /* send/recv interface index */
429 };
430
431 int dhcpv6_send_packet(int index, struct dhcpv6_packet *dhcp_pkt, int len)
432 {
433         struct msghdr m;
434         struct iovec v;
435         struct in6_pktinfo *pktinfo;
436         struct cmsghdr *cmsg;
437         int fd, ret;
438         struct sockaddr_in6 dst;
439         void *control_buf;
440         size_t control_buf_len;
441
442         fd = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
443         if (fd < 0)
444                 return -errno;
445
446         memset(&dst, 0, sizeof(dst));
447         dst.sin6_family = AF_INET6;
448         dst.sin6_port = htons(DHCPV6_SERVER_PORT);
449
450         dst.sin6_addr = in6addr_all_dhcp_relay_agents_and_servers_mc;
451
452         control_buf_len = CMSG_SPACE(sizeof(struct in6_pktinfo));
453         control_buf = g_try_malloc0(control_buf_len);
454         if (control_buf == NULL) {
455                 close(fd);
456                 return -ENOMEM;
457         }
458
459         memset(&m, 0, sizeof(m));
460         memset(&v, 0, sizeof(v));
461
462         m.msg_name = &dst;
463         m.msg_namelen = sizeof(dst);
464
465         v.iov_base = (char *)dhcp_pkt;
466         v.iov_len = len;
467         m.msg_iov = &v;
468         m.msg_iovlen = 1;
469
470         m.msg_control = control_buf;
471         m.msg_controllen = control_buf_len;
472         cmsg = CMSG_FIRSTHDR(&m);
473         cmsg->cmsg_level = IPPROTO_IPV6;
474         cmsg->cmsg_type = IPV6_PKTINFO;
475         cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
476
477         pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
478         memset(pktinfo, 0, sizeof(*pktinfo));
479         pktinfo->ipi6_ifindex = index;
480         m.msg_controllen = cmsg->cmsg_len;
481
482         ret = sendmsg(fd, &m, 0);
483         if (ret < 0)
484                 perror("DHCPv6 msg send failed");
485
486         g_free(control_buf);
487         close(fd);
488
489         return ret;
490 }
491
492 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
493                 uint32_t source_ip, int source_port, uint32_t dest_ip,
494                         int dest_port, const uint8_t *dest_arp, int ifindex)
495 {
496         struct sockaddr_ll dest;
497         struct ip_udp_dhcp_packet packet;
498         int fd, n;
499
500         enum {
501                 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) -
502                                                 EXTEND_FOR_BUGGY_SERVERS,
503                 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE -
504                                 offsetof(struct ip_udp_dhcp_packet, udp),
505         };
506
507         fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
508         if (fd < 0)
509                 return -errno;
510
511         memset(&dest, 0, sizeof(dest));
512         memset(&packet, 0, sizeof(packet));
513         packet.data = *dhcp_pkt;
514
515         dest.sll_family = AF_PACKET;
516         dest.sll_protocol = htons(ETH_P_IP);
517         dest.sll_ifindex = ifindex;
518         dest.sll_halen = 6;
519         memcpy(dest.sll_addr, dest_arp, 6);
520         if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
521                 close(fd);
522                 return -errno;
523         }
524
525         packet.ip.protocol = IPPROTO_UDP;
526         packet.ip.saddr = source_ip;
527         packet.ip.daddr = dest_ip;
528         packet.udp.source = htons(source_port);
529         packet.udp.dest = htons(dest_port);
530         /* size, excluding IP header: */
531         packet.udp.len = htons(UPD_DHCP_SIZE);
532         /* for UDP checksumming, ip.len is set to UDP packet len */
533         packet.ip.tot_len = packet.udp.len;
534         packet.udp.check = dhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
535         /* but for sending, it is set to IP packet len */
536         packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
537         packet.ip.ihl = sizeof(packet.ip) >> 2;
538         packet.ip.version = IPVERSION;
539         packet.ip.ttl = IPDEFTTL;
540         packet.ip.check = dhcp_checksum(&packet.ip, sizeof(packet.ip));
541
542         /*
543          * Currently we send full-sized DHCP packets (zero padded).
544          * If you need to change this: last byte of the packet is
545          * packet.data.options[dhcp_end_option(packet.data.options)]
546          */
547         n = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
548                         (struct sockaddr *) &dest, sizeof(dest));
549         close(fd);
550
551         if (n < 0)
552                 return -errno;
553
554         return n;
555 }
556
557 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
558                                 uint32_t source_ip, int source_port,
559                                 uint32_t dest_ip, int dest_port)
560 {
561         struct sockaddr_in client;
562         int fd, n, opt = 1;
563
564         enum {
565                 DHCP_SIZE = sizeof(struct dhcp_packet) -
566                                         EXTEND_FOR_BUGGY_SERVERS,
567         };
568
569         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
570         if (fd < 0)
571                 return -errno;
572
573         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
574
575         memset(&client, 0, sizeof(client));
576         client.sin_family = AF_INET;
577         client.sin_port = htons(source_port);
578         client.sin_addr.s_addr = source_ip;
579         if (bind(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
580                 close(fd);
581                 return -errno;
582         }
583
584         memset(&client, 0, sizeof(client));
585         client.sin_family = AF_INET;
586         client.sin_port = htons(dest_port);
587         client.sin_addr.s_addr = dest_ip;
588         if (connect(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
589                 close(fd);
590                 return -errno;
591         }
592
593         n = write(fd, dhcp_pkt, DHCP_SIZE);
594
595         close(fd);
596
597         if (n < 0)
598                 return -errno;
599
600         return n;
601 }
602
603 int dhcp_l3_socket(int port, const char *interface, int family)
604 {
605         int fd, opt = 1, len;
606         struct sockaddr_in addr4;
607         struct sockaddr_in6 addr6;
608         struct sockaddr *addr;
609
610         fd = socket(family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
611         if (fd < 0)
612                 return -errno;
613
614         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
615
616         if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
617                                 interface, strlen(interface) + 1) < 0) {
618                 close(fd);
619                 return -1;
620         }
621
622         if (family == AF_INET) {
623                 memset(&addr4, 0, sizeof(addr4));
624                 addr4.sin_family = family;
625                 addr4.sin_port = htons(port);
626                 addr = (struct sockaddr *)&addr4;
627                 len = sizeof(addr4);
628         } else if (family == AF_INET6) {
629                 memset(&addr6, 0, sizeof(addr6));
630                 addr6.sin6_family = family;
631                 addr6.sin6_port = htons(port);
632                 addr = (struct sockaddr *)&addr6;
633                 len = sizeof(addr6);
634         } else {
635                 close(fd);
636                 return -EINVAL;
637         }
638
639         if (bind(fd, addr, len) != 0) {
640                 close(fd);
641                 return -1;
642         }
643
644         return fd;
645 }
646
647 char *get_interface_name(int index)
648 {
649         struct ifreq ifr;
650         int sk, err;
651
652         if (index < 0)
653                 return NULL;
654
655         sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
656         if (sk < 0) {
657                 perror("Open socket error");
658                 return NULL;
659         }
660
661         memset(&ifr, 0, sizeof(ifr));
662         ifr.ifr_ifindex = index;
663
664         err = ioctl(sk, SIOCGIFNAME, &ifr);
665         if (err < 0) {
666                 perror("Get interface name error");
667                 close(sk);
668                 return NULL;
669         }
670
671         close(sk);
672
673         return g_strdup(ifr.ifr_name);
674 }
675
676 gboolean interface_is_up(int index)
677 {
678         int sk, err;
679         struct ifreq ifr;
680         gboolean ret = FALSE;
681
682         sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
683         if (sk < 0) {
684                 perror("Open socket error");
685                 return FALSE;
686         }
687
688         memset(&ifr, 0, sizeof(ifr));
689         ifr.ifr_ifindex = index;
690
691         err = ioctl(sk, SIOCGIFNAME, &ifr);
692         if (err < 0) {
693                 perror("Get interface name error");
694                 goto done;
695         }
696
697         err = ioctl(sk, SIOCGIFFLAGS, &ifr);
698         if (err < 0) {
699                 perror("Get interface flags error");
700                 goto done;
701         }
702
703         if (ifr.ifr_flags & IFF_UP)
704                 ret = TRUE;
705
706 done:
707         close(sk);
708
709         return ret;
710 }