dnsproxy: Check the result of socket creation when resolving
[platform/upstream/connman.git] / src / dnsproxy.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <arpa/inet.h>
32 #include <netinet/in.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netdb.h>
36 #include <resolv.h>
37 #include <gweb/gresolv.h>
38
39 #include <glib.h>
40
41 #include "connman.h"
42
43 #if __BYTE_ORDER == __LITTLE_ENDIAN
44 struct domain_hdr {
45         uint16_t id;
46         uint8_t rd:1;
47         uint8_t tc:1;
48         uint8_t aa:1;
49         uint8_t opcode:4;
50         uint8_t qr:1;
51         uint8_t rcode:4;
52         uint8_t z:3;
53         uint8_t ra:1;
54         uint16_t qdcount;
55         uint16_t ancount;
56         uint16_t nscount;
57         uint16_t arcount;
58 } __attribute__ ((packed));
59 #elif __BYTE_ORDER == __BIG_ENDIAN
60 struct domain_hdr {
61         uint16_t id;
62         uint8_t qr:1;
63         uint8_t opcode:4;
64         uint8_t aa:1;
65         uint8_t tc:1;
66         uint8_t rd:1;
67         uint8_t ra:1;
68         uint8_t z:3;
69         uint8_t rcode:4;
70         uint16_t qdcount;
71         uint16_t ancount;
72         uint16_t nscount;
73         uint16_t arcount;
74 } __attribute__ ((packed));
75 #else
76 #error "Unknown byte order"
77 #endif
78
79 struct partial_reply {
80         uint16_t len;
81         uint16_t received;
82         unsigned char buf[];
83 };
84
85 struct server_data {
86         char *interface;
87         GList *domains;
88         char *server;
89         struct sockaddr *server_addr;
90         socklen_t server_addr_len;
91         int protocol;
92         GIOChannel *channel;
93         guint watch;
94         guint timeout;
95         gboolean enabled;
96         gboolean connected;
97         struct partial_reply *incoming_reply;
98 };
99
100 struct request_data {
101         union {
102                 struct sockaddr_in6 __sin6; /* Only for the length */
103                 struct sockaddr sa;
104         };
105         socklen_t sa_len;
106         int client_sk;
107         int protocol;
108         guint16 srcid;
109         guint16 dstid;
110         guint16 altid;
111         guint timeout;
112         guint watch;
113         guint numserv;
114         guint numresp;
115         gpointer request;
116         gsize request_len;
117         gpointer name;
118         gpointer resp;
119         gsize resplen;
120         struct listener_data *ifdata;
121         gboolean append_domain;
122 };
123
124 struct listener_data {
125         char *ifname;
126         GIOChannel *udp_listener_channel;
127         guint udp_listener_watch;
128         GIOChannel *tcp_listener_channel;
129         guint tcp_listener_watch;
130 };
131
132 struct cache_data {
133         time_t inserted;
134         time_t valid_until;
135         time_t cache_until;
136         int timeout;
137         uint16_t type;
138         uint16_t answers;
139         unsigned int data_len;
140         unsigned char *data; /* contains DNS header + body */
141 };
142
143 struct cache_entry {
144         char *key;
145         int want_refresh;
146         int hits;
147         struct cache_data *ipv4;
148         struct cache_data *ipv6;
149 };
150
151 struct domain_question {
152         uint16_t type;
153         uint16_t class;
154 } __attribute__ ((packed));
155
156 struct domain_rr {
157         uint16_t type;
158         uint16_t class;
159         uint32_t ttl;
160         uint16_t rdlen;
161 } __attribute__ ((packed));
162
163 /*
164  * We limit how long the cached DNS entry stays in the cache.
165  * By default the TTL (time-to-live) of the DNS response is used
166  * when setting the cache entry life time. The value is in seconds.
167  */
168 #define MAX_CACHE_TTL (60 * 30)
169 /*
170  * Also limit the other end, cache at least for 30 seconds.
171  */
172 #define MIN_CACHE_TTL (30)
173
174 /*
175  * We limit the cache size to some sane value so that cached data does
176  * not occupy too much memory. Each cached entry occupies on average
177  * about 100 bytes memory (depending on DNS name length).
178  * Example: caching www.connman.net uses 97 bytes memory.
179  * The value is the max amount of cached DNS responses (count).
180  */
181 #define MAX_CACHE_SIZE 256
182
183 static int cache_size;
184 static GHashTable *cache;
185 static int cache_refcount;
186 static GSList *server_list = NULL;
187 static GSList *request_list = NULL;
188 static GHashTable *listener_table = NULL;
189 static time_t next_refresh;
190
191 static guint16 get_id()
192 {
193         return random();
194 }
195
196 static int protocol_offset(int protocol)
197 {
198         switch (protocol) {
199         case IPPROTO_UDP:
200                 return 0;
201
202         case IPPROTO_TCP:
203                 return 2;
204
205         default:
206                 return -EINVAL;
207         }
208
209 }
210
211 /*
212  * There is a power and efficiency benefit to have entries
213  * in our cache expire at the same time. To this extend,
214  * we round down the cache valid time to common boundaries.
215  */
216 static time_t round_down_ttl(time_t end_time, int ttl)
217 {
218         if (ttl < 15)
219                 return end_time;
220
221         /* Less than 5 minutes, round to 10 second boundary */
222         if (ttl < 300) {
223                 end_time = end_time / 10;
224                 end_time = end_time * 10;
225         } else { /* 5 or more minutes, round to 30 seconds */
226                 end_time = end_time / 30;
227                 end_time = end_time * 30;
228         }
229         return end_time;
230 }
231
232 static struct request_data *find_request(guint16 id)
233 {
234         GSList *list;
235
236         for (list = request_list; list; list = list->next) {
237                 struct request_data *req = list->data;
238
239                 if (req->dstid == id || req->altid == id)
240                         return req;
241         }
242
243         return NULL;
244 }
245
246 static struct server_data *find_server(const char *interface,
247                                         const char *server,
248                                                 int protocol)
249 {
250         GSList *list;
251
252         DBG("interface %s server %s proto %d", interface, server, protocol);
253
254         for (list = server_list; list; list = list->next) {
255                 struct server_data *data = list->data;
256
257                 if (interface == NULL && data->interface == NULL &&
258                                 g_str_equal(data->server, server) == TRUE &&
259                                 data->protocol == protocol)
260                         return data;
261
262                 if (interface == NULL ||
263                                 data->interface == NULL || data->server == NULL)
264                         continue;
265
266                 if (g_str_equal(data->interface, interface) == TRUE &&
267                                 g_str_equal(data->server, server) == TRUE &&
268                                 data->protocol == protocol)
269                         return data;
270         }
271
272         return NULL;
273 }
274
275 /* we can keep using the same resolve's */
276 static GResolv *ipv4_resolve;
277 static GResolv *ipv6_resolve;
278
279 static void dummy_resolve_func(GResolvResultStatus status,
280                                         char **results, gpointer user_data)
281 {
282 }
283
284 /*
285  * Refresh a DNS entry, but also age the hit count a bit */
286 static void refresh_dns_entry(struct cache_entry *entry, char *name)
287 {
288         int age = 1;
289
290         if (ipv4_resolve == NULL) {
291                 ipv4_resolve = g_resolv_new(0);
292                 g_resolv_set_address_family(ipv4_resolve, AF_INET);
293                 g_resolv_add_nameserver(ipv4_resolve, "127.0.0.1", 53, 0);
294         }
295
296         if (ipv6_resolve == NULL) {
297                 ipv6_resolve = g_resolv_new(0);
298                 g_resolv_set_address_family(ipv6_resolve, AF_INET6);
299                 g_resolv_add_nameserver(ipv6_resolve, "127.0.0.1", 53, 0);
300         }
301
302         if (entry->ipv4 == NULL) {
303                 DBG("Refresing A record for %s", name);
304                 g_resolv_lookup_hostname(ipv4_resolve, name,
305                                         dummy_resolve_func, NULL);
306                 age = 4;
307         }
308
309         if (entry->ipv6 == NULL) {
310                 DBG("Refresing AAAA record for %s", name);
311                 g_resolv_lookup_hostname(ipv6_resolve, name,
312                                         dummy_resolve_func, NULL);
313                 age = 4;
314         }
315
316         entry->hits -= age;
317         if (entry->hits < 0)
318                 entry->hits = 0;
319 }
320
321 static int dns_name_length(unsigned char *buf)
322 {
323         if ((buf[0] & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* compressed name */
324                 return 2;
325         return strlen((char *)buf);
326 }
327
328 static void update_cached_ttl(unsigned char *buf, int len, int new_ttl)
329 {
330         unsigned char *c;
331         uint32_t *i;
332         uint16_t *w;
333         int l;
334
335         /* skip the header */
336         c = buf + 12;
337         len -= 12;
338
339         /* skip the query, which is a name and 2 16 bit words */
340         l = dns_name_length(c);
341         c += l;
342         len -= l;
343         c += 4;
344         len -= 4;
345
346         /* now we get the answer records */
347
348         while (len > 0) {
349                 /* first a name */
350                 l = dns_name_length(c);
351                 c += l;
352                 len -= l;
353                 if (len < 0)
354                         break;
355                 /* then type + class, 2 bytes each */
356                 c += 4;
357                 len -= 4;
358                 if (len < 0)
359                         break;
360
361                 /* now the 4 byte TTL field */
362                 i = (uint32_t *)c;
363                 *i = htonl(new_ttl);
364                 c += 4;
365                 len -= 4;
366                 if (len < 0)
367                         break;
368
369                 /* now the 2 byte rdlen field */
370                 w = (uint16_t *)c;
371                 c += ntohs(*w) + 2;
372                 len -= ntohs(*w) + 2;
373         }
374 }
375
376 static void send_cached_response(int sk, unsigned char *buf, int len,
377                                 const struct sockaddr *to, socklen_t tolen,
378                                 int protocol, int id, uint16_t answers, int ttl)
379 {
380         struct domain_hdr *hdr;
381         unsigned char *ptr = buf;
382         int err, offset, dns_len, adj_len = len - 2;
383
384         /*
385          * The cached packet contains always the TCP offset (two bytes)
386          * so skip them for UDP.
387          */
388         switch (protocol) {
389         case IPPROTO_UDP:
390                 ptr += 2;
391                 len -= 2;
392                 dns_len = len;
393                 offset = 0;
394                 break;
395         case IPPROTO_TCP:
396                 offset = 2;
397                 dns_len = ptr[0] * 256 + ptr[1];
398                 break;
399         default:
400                 return;
401         }
402
403         if (len < 12)
404                 return;
405
406         hdr = (void *) (ptr + offset);
407
408         hdr->id = id;
409         hdr->qr = 1;
410         hdr->rcode = 0;
411         hdr->ancount = htons(answers);
412         hdr->nscount = 0;
413         hdr->arcount = 0;
414
415         /* if this is a negative reply, we are authorative */
416         if (answers == 0)
417                 hdr->aa = 1;
418         else
419                 update_cached_ttl((unsigned char *)hdr, adj_len, ttl);
420
421         DBG("sk %d id 0x%04x answers %d ptr %p length %d dns %d",
422                 sk, hdr->id, answers, ptr, len, dns_len);
423
424         err = sendto(sk, ptr, len, MSG_NOSIGNAL, to, tolen);
425         if (err < 0) {
426                 connman_error("Cannot send cached DNS response: %s",
427                                 strerror(errno));
428                 return;
429         }
430
431         if (err != len || (dns_len != (len - 2) && protocol == IPPROTO_TCP) ||
432                                 (dns_len != len && protocol == IPPROTO_UDP))
433                 DBG("Packet length mismatch, sent %d wanted %d dns %d",
434                         err, len, dns_len);
435 }
436
437 static void send_response(int sk, unsigned char *buf, int len,
438                                 const struct sockaddr *to, socklen_t tolen,
439                                 int protocol)
440 {
441         struct domain_hdr *hdr;
442         int err, offset = protocol_offset(protocol);
443
444         DBG("sk %d", sk);
445
446         if (offset < 0)
447                 return;
448
449         if (len < 12)
450                 return;
451
452         hdr = (void *) (buf + offset);
453
454         DBG("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
455
456         hdr->qr = 1;
457         hdr->rcode = 2;
458
459         hdr->ancount = 0;
460         hdr->nscount = 0;
461         hdr->arcount = 0;
462
463         err = sendto(sk, buf, len, MSG_NOSIGNAL, to, tolen);
464         if (err < 0) {
465                 connman_error("Failed to send DNS response to %d: %s",
466                                 sk, strerror(errno));
467                 return;
468         }
469 }
470
471 static gboolean request_timeout(gpointer user_data)
472 {
473         struct request_data *req = user_data;
474         struct listener_data *ifdata;
475
476         DBG("id 0x%04x", req->srcid);
477
478         if (req == NULL)
479                 return FALSE;
480
481         ifdata = req->ifdata;
482
483         request_list = g_slist_remove(request_list, req);
484         req->numserv--;
485
486         if (req->resplen > 0 && req->resp != NULL) {
487                 int sk, err;
488
489                 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
490
491                 err = sendto(sk, req->resp, req->resplen, MSG_NOSIGNAL,
492                                                 &req->sa, req->sa_len);
493                 if (err < 0)
494                         return FALSE;
495         } else if (req->request && req->numserv == 0) {
496                 struct domain_hdr *hdr;
497
498                 if (req->protocol == IPPROTO_TCP) {
499                         hdr = (void *) (req->request + 2);
500                         hdr->id = req->srcid;
501                         send_response(req->client_sk, req->request,
502                                 req->request_len, NULL, 0, IPPROTO_TCP);
503
504                 } else if (req->protocol == IPPROTO_UDP) {
505                         int sk;
506
507                         hdr = (void *) (req->request);
508                         hdr->id = req->srcid;
509                         sk = g_io_channel_unix_get_fd(
510                                                 ifdata->udp_listener_channel);
511                         send_response(sk, req->request, req->request_len,
512                                         &req->sa, req->sa_len, IPPROTO_UDP);
513                 }
514         }
515
516         g_free(req->resp);
517         g_free(req);
518
519         return FALSE;
520 }
521
522 static int append_query(unsigned char *buf, unsigned int size,
523                                 const char *query, const char *domain)
524 {
525         unsigned char *ptr = buf;
526         int len;
527
528         DBG("query %s domain %s", query, domain);
529
530         while (query != NULL) {
531                 const char *tmp;
532
533                 tmp = strchr(query, '.');
534                 if (tmp == NULL) {
535                         len = strlen(query);
536                         if (len == 0)
537                                 break;
538                         *ptr = len;
539                         memcpy(ptr + 1, query, len);
540                         ptr += len + 1;
541                         break;
542                 }
543
544                 *ptr = tmp - query;
545                 memcpy(ptr + 1, query, tmp - query);
546                 ptr += tmp - query + 1;
547
548                 query = tmp + 1;
549         }
550
551         while (domain != NULL) {
552                 const char *tmp;
553
554                 tmp = strchr(domain, '.');
555                 if (tmp == NULL) {
556                         len = strlen(domain);
557                         if (len == 0)
558                                 break;
559                         *ptr = len;
560                         memcpy(ptr + 1, domain, len);
561                         ptr += len + 1;
562                         break;
563                 }
564
565                 *ptr = tmp - domain;
566                 memcpy(ptr + 1, domain, tmp - domain);
567                 ptr += tmp - domain + 1;
568
569                 domain = tmp + 1;
570         }
571
572         *ptr++ = 0x00;
573
574         return ptr - buf;
575 }
576
577 static gboolean cache_check_is_valid(struct cache_data *data,
578                                 time_t current_time)
579 {
580         if (data == NULL)
581                 return FALSE;
582
583         if (data->cache_until < current_time)
584                 return FALSE;
585
586         return TRUE;
587 }
588
589 /*
590  * remove stale cached entries so that they can be refreshed
591  */
592 static void cache_enforce_validity(struct cache_entry *entry)
593 {
594         time_t current_time = time(NULL);
595
596         if (cache_check_is_valid(entry->ipv4, current_time) == FALSE
597                                                         && entry->ipv4) {
598                 DBG("cache timeout \"%s\" type A", entry->key);
599                 g_free(entry->ipv4->data);
600                 g_free(entry->ipv4);
601                 entry->ipv4 = NULL;
602
603         }
604
605         if (cache_check_is_valid(entry->ipv6, current_time) == FALSE
606                                                         && entry->ipv6) {
607                 DBG("cache timeout \"%s\" type AAAA", entry->key);
608                 g_free(entry->ipv6->data);
609                 g_free(entry->ipv6);
610                 entry->ipv6 = NULL;
611         }
612 }
613
614 static uint16_t cache_check_validity(char *question, uint16_t type,
615                                 struct cache_entry *entry)
616 {
617         time_t current_time = time(NULL);
618         int want_refresh = 0;
619
620         /*
621          * if we have a popular entry, we want a refresh instead of
622          * total destruction of the entry.
623          */
624         if (entry->hits > 2)
625                 want_refresh = 1;
626
627         cache_enforce_validity(entry);
628
629         switch (type) {
630         case 1:         /* IPv4 */
631                 if (cache_check_is_valid(entry->ipv4, current_time) == FALSE) {
632                         DBG("cache %s \"%s\" type A", entry->ipv4 ?
633                                         "timeout" : "entry missing", question);
634
635                         if (want_refresh)
636                                 entry->want_refresh = 1;
637
638                         /*
639                          * We do not remove cache entry if there is still
640                          * valid IPv6 entry found in the cache.
641                          */
642                         if (cache_check_is_valid(entry->ipv6, current_time)
643                                         == FALSE && want_refresh == FALSE) {
644                                 g_hash_table_remove(cache, question);
645                                 type = 0;
646                         }
647                 }
648                 break;
649
650         case 28:        /* IPv6 */
651                 if (cache_check_is_valid(entry->ipv6, current_time) == FALSE) {
652                         DBG("cache %s \"%s\" type AAAA", entry->ipv6 ?
653                                         "timeout" : "entry missing", question);
654
655                         if (want_refresh)
656                                 entry->want_refresh = 1;
657
658                         if (cache_check_is_valid(entry->ipv4, current_time)
659                                         == FALSE && want_refresh == FALSE) {
660                                 g_hash_table_remove(cache, question);
661                                 type = 0;
662                         }
663                 }
664                 break;
665         }
666
667         return type;
668 }
669
670 static struct cache_entry *cache_check(gpointer request, int *qtype, int proto)
671 {
672         char *question;
673         struct cache_entry *entry;
674         struct domain_question *q;
675         uint16_t type;
676         int offset, proto_offset;
677
678         if (request == NULL)
679                 return NULL;
680
681         proto_offset = protocol_offset(proto);
682         if (proto_offset < 0)
683                 return NULL;
684
685         question = request + proto_offset + 12;
686
687         offset = strlen(question) + 1;
688         q = (void *) (question + offset);
689         type = ntohs(q->type);
690
691         /* We only cache either A (1) or AAAA (28) requests */
692         if (type != 1 && type != 28)
693                 return NULL;
694
695         entry = g_hash_table_lookup(cache, question);
696         if (entry == NULL)
697                 return NULL;
698
699         type = cache_check_validity(question, type, entry);
700         if (type == 0)
701                 return NULL;
702
703         *qtype = type;
704         return entry;
705 }
706
707 /*
708  * Get a label/name from DNS resource record. The function decompresses the
709  * label if necessary. The function does not convert the name to presentation
710  * form. This means that the result string will contain label lengths instead
711  * of dots between labels. We intentionally do not want to convert to dotted
712  * format so that we can cache the wire format string directly.
713  */
714 static int get_name(int counter,
715                 unsigned char *pkt, unsigned char *start, unsigned char *max,
716                 unsigned char *output, int output_max, int *output_len,
717                 unsigned char **end, char *name, int *name_len)
718 {
719         unsigned char *p;
720
721         /* Limit recursion to 10 (this means up to 10 labels in domain name) */
722         if (counter > 10)
723                 return -EINVAL;
724
725         p = start;
726         while (*p) {
727                 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
728                         uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
729
730                         if (offset >= max - pkt)
731                                 return -ENOBUFS;
732
733                         if (*end == NULL)
734                                 *end = p + 2;
735
736                         return get_name(counter + 1, pkt, pkt + offset, max,
737                                         output, output_max, output_len, end,
738                                         name, name_len);
739                 } else {
740                         unsigned label_len = *p;
741
742                         if (pkt + label_len > max)
743                                 return -ENOBUFS;
744
745                         if (*output_len > output_max)
746                                 return -ENOBUFS;
747
748                         /*
749                          * We need the original name in order to check
750                          * if this answer is the correct one.
751                          */
752                         name[(*name_len)++] = label_len;
753                         memcpy(name + *name_len, p + 1, label_len + 1);
754                         *name_len += label_len;
755
756                         /* We compress the result */
757                         output[0] = NS_CMPRSFLGS;
758                         output[1] = 0x0C;
759                         *output_len = 2;
760
761                         p += label_len + 1;
762
763                         if (*end == NULL)
764                                 *end = p;
765
766                         if (p >= max)
767                                 return -ENOBUFS;
768                 }
769         }
770
771         return 0;
772 }
773
774 static int parse_rr(unsigned char *buf, unsigned char *start,
775                         unsigned char *max,
776                         unsigned char *response, unsigned int *response_size,
777                         uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
778                         unsigned char **end,
779                         char *name)
780 {
781         struct domain_rr *rr;
782         int err, offset;
783         int name_len = 0, output_len = 0, max_rsp = *response_size;
784
785         err = get_name(0, buf, start, max, response, max_rsp,
786                 &output_len, end, name, &name_len);
787         if (err < 0)
788                 return err;
789
790         offset = output_len;
791
792         if ((unsigned int) offset > *response_size)
793                 return -ENOBUFS;
794
795         rr = (void *) (*end);
796
797         if (rr == NULL)
798                 return -EINVAL;
799
800         *type = ntohs(rr->type);
801         *class = ntohs(rr->class);
802         *ttl = ntohl(rr->ttl);
803         *rdlen = ntohs(rr->rdlen);
804
805         if (*ttl < 0)
806                 return -EINVAL;
807
808         memcpy(response + offset, *end, sizeof(struct domain_rr));
809
810         offset += sizeof(struct domain_rr);
811         *end += sizeof(struct domain_rr);
812
813         if ((unsigned int) (offset + *rdlen) > *response_size)
814                 return -ENOBUFS;
815
816         memcpy(response + offset, *end, *rdlen);
817
818         *end += *rdlen;
819
820         *response_size = offset + *rdlen;
821
822         return 0;
823 }
824
825 static gboolean check_alias(GSList *aliases, char *name)
826 {
827         GSList *list;
828
829         if (aliases != NULL) {
830                 for (list = aliases; list; list = list->next) {
831                         int len = strlen((char *)list->data);
832                         if (strncmp((char *)list->data, name, len) == 0)
833                                 return TRUE;
834                 }
835         }
836
837         return FALSE;
838 }
839
840 static int parse_response(unsigned char *buf, int buflen,
841                         char *question, int qlen,
842                         uint16_t *type, uint16_t *class, int *ttl,
843                         unsigned char *response, unsigned int *response_len,
844                         uint16_t *answers)
845 {
846         struct domain_hdr *hdr = (void *) buf;
847         struct domain_question *q;
848         unsigned char *ptr;
849         uint16_t qdcount = ntohs(hdr->qdcount);
850         uint16_t ancount = ntohs(hdr->ancount);
851         int err, i;
852         uint16_t qtype, qclass;
853         unsigned char *next = NULL;
854         unsigned int maxlen = *response_len;
855         GSList *aliases = NULL, *list;
856         char name[NS_MAXDNAME + 1];
857
858         if (buflen < 12)
859                 return -EINVAL;
860
861         DBG("qr %d qdcount %d", hdr->qr, qdcount);
862
863         /* We currently only cache responses where question count is 1 */
864         if (hdr->qr != 1 || qdcount != 1)
865                 return -EINVAL;
866
867         ptr = buf + sizeof(struct domain_hdr);
868
869         strncpy(question, (char *) ptr, qlen);
870         qlen = strlen(question);
871         ptr += qlen + 1; /* skip \0 */
872
873         q = (void *) ptr;
874         qtype = ntohs(q->type);
875
876         /* We cache only A and AAAA records */
877         if (qtype != 1 && qtype != 28)
878                 return -ENOMSG;
879
880         qclass = ntohs(q->class);
881
882         ptr += 2 + 2; /* ptr points now to answers */
883
884         err = -ENOMSG;
885         *response_len = 0;
886         *answers = 0;
887
888         /*
889          * We have a bunch of answers (like A, AAAA, CNAME etc) to
890          * A or AAAA question. We traverse the answers and parse the
891          * resource records. Only A and AAAA records are cached, all
892          * the other records in answers are skipped.
893          */
894         for (i = 0; i < ancount; i++) {
895                 /*
896                  * Get one address at a time to this buffer.
897                  * The max size of the answer is
898                  *   2 (pointer) + 2 (type) + 2 (class) +
899                  *   4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
900                  * for A or AAAA record.
901                  * For CNAME the size can be bigger.
902                  */
903                 unsigned char rsp[NS_MAXCDNAME];
904                 unsigned int rsp_len = sizeof(rsp) - 1;
905                 int ret, rdlen;
906
907                 memset(rsp, 0, sizeof(rsp));
908
909                 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
910                         type, class, ttl, &rdlen, &next, name);
911                 if (ret != 0) {
912                         err = ret;
913                         goto out;
914                 }
915
916                 /*
917                  * Now rsp contains compressed or uncompressed resource
918                  * record. Next we check if this record answers the question.
919                  * The name var contains the uncompressed label.
920                  * One tricky bit is the CNAME records as they alias
921                  * the name we might be interested in.
922                  */
923
924                 /*
925                  * Go to next answer if the class is not the one we are
926                  * looking for.
927                  */
928                 if (*class != qclass) {
929                         ptr = next;
930                         next = NULL;
931                         continue;
932                 }
933
934                 /*
935                  * Try to resolve aliases also, type is CNAME(5).
936                  * This is important as otherwise the aliased names would not
937                  * be cached at all as the cache would not contain the aliased
938                  * question.
939                  *
940                  * If any CNAME is found in DNS packet, then we cache the alias
941                  * IP address instead of the question (as the server
942                  * said that question has only an alias).
943                  * This means in practice that if e.g., ipv6.google.com is
944                  * queried, DNS server returns CNAME of that name which is
945                  * ipv6.l.google.com. We then cache the address of the CNAME
946                  * but return the question name to client. So the alias
947                  * status of the name is not saved in cache and thus not
948                  * returned to the client. We do not return DNS packets from
949                  * cache to client saying that ipv6.google.com is an alias to
950                  * ipv6.l.google.com but we return instead a DNS packet that
951                  * says ipv6.google.com has address xxx which is in fact the
952                  * address of ipv6.l.google.com. For caching purposes this
953                  * should not cause any issues.
954                  */
955                 if (*type == 5 && strncmp(question, name, qlen) == 0) {
956                         /*
957                          * So now the alias answered the question. This is
958                          * not very useful from caching point of view as
959                          * the following A or AAAA records will not match the
960                          * question. We need to find the real A/AAAA record
961                          * of the alias and cache that.
962                          */
963                         unsigned char *end = NULL;
964                         int name_len = 0, output_len;
965
966                         memset(rsp, 0, sizeof(rsp));
967                         rsp_len = sizeof(rsp) - 1;
968
969                         /*
970                          * Alias is in rdata part of the message,
971                          * and next-rdlen points to it. So we need to get
972                          * the real name of the alias.
973                          */
974                         ret = get_name(0, buf, next - rdlen, buf + buflen,
975                                         rsp, rsp_len, &output_len, &end,
976                                         name, &name_len);
977                         if (ret != 0) {
978                                 /* just ignore the error at this point */
979                                 ptr = next;
980                                 next = NULL;
981                                 continue;
982                         }
983
984                         /*
985                          * We should now have the alias of the entry we might
986                          * want to cache. Just remember it for a while.
987                          * We check the alias list when we have parsed the
988                          * A or AAAA record.
989                          */
990                         aliases = g_slist_prepend(aliases, g_strdup(name));
991
992                         ptr = next;
993                         next = NULL;
994                         continue;
995                 }
996
997                 if (*type == qtype) {
998                         /*
999                          * We found correct type (A or AAAA)
1000                          */
1001                         if (check_alias(aliases, name) == TRUE ||
1002                                 (aliases == NULL && strncmp(question, name,
1003                                                         qlen) == 0)) {
1004                                 /*
1005                                  * We found an alias or the name of the rr
1006                                  * matches the question. If so, we append
1007                                  * the compressed label to the cache.
1008                                  * The end result is a response buffer that
1009                                  * will contain one or more cached and
1010                                  * compressed resource records.
1011                                  */
1012                                 if (*response_len + rsp_len > maxlen) {
1013                                         err = -ENOBUFS;
1014                                         goto out;
1015                                 }
1016                                 memcpy(response + *response_len, rsp, rsp_len);
1017                                 *response_len += rsp_len;
1018                                 (*answers)++;
1019                                 err = 0;
1020                         }
1021                 }
1022
1023                 ptr = next;
1024                 next = NULL;
1025         }
1026
1027 out:
1028         for (list = aliases; list; list = list->next)
1029                 g_free(list->data);
1030         g_slist_free(aliases);
1031
1032         return err;
1033 }
1034
1035 struct cache_timeout {
1036         time_t current_time;
1037         int max_timeout;
1038         int try_harder;
1039 };
1040
1041 static gboolean cache_check_entry(gpointer key, gpointer value,
1042                                         gpointer user_data)
1043 {
1044         struct cache_timeout *data = user_data;
1045         struct cache_entry *entry = value;
1046         int max_timeout;
1047
1048         /* Scale the number of hits by half as part of cache aging */
1049
1050         entry->hits /= 2;
1051
1052         /*
1053          * If either IPv4 or IPv6 cached entry has expired, we
1054          * remove both from the cache.
1055          */
1056
1057         if (entry->ipv4 != NULL && entry->ipv4->timeout > 0) {
1058                 max_timeout = entry->ipv4->cache_until;
1059                 if (max_timeout > data->max_timeout)
1060                         data->max_timeout = max_timeout;
1061
1062                 if (entry->ipv4->cache_until < data->current_time)
1063                         return TRUE;
1064         }
1065
1066         if (entry->ipv6 != NULL && entry->ipv6->timeout > 0) {
1067                 max_timeout = entry->ipv6->cache_until;
1068                 if (max_timeout > data->max_timeout)
1069                         data->max_timeout = max_timeout;
1070
1071                 if (entry->ipv6->cache_until < data->current_time)
1072                         return TRUE;
1073         }
1074
1075         /*
1076          * if we're asked to try harder, also remove entries that have
1077          * few hits
1078          */
1079         if (data->try_harder && entry->hits < 4)
1080                 return TRUE;
1081
1082         return FALSE;
1083 }
1084
1085 static void cache_cleanup(void)
1086 {
1087         static int max_timeout;
1088         struct cache_timeout data;
1089         int count = 0;
1090
1091         data.current_time = time(NULL);
1092         data.max_timeout = 0;
1093         data.try_harder = 0;
1094
1095         /*
1096          * In the first pass, we only remove entries that have timed out.
1097          * We use a cache of the first time to expire to do this only
1098          * when it makes sense.
1099          */
1100         if (max_timeout <= data.current_time) {
1101                 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1102                                                 &data);
1103         }
1104         DBG("removed %d in the first pass", count);
1105
1106         /*
1107          * In the second pass, if the first pass turned up blank,
1108          * we also expire entries with a low hit count,
1109          * while aging the hit count at the same time.
1110          */
1111         data.try_harder = 1;
1112         if (count == 0)
1113                 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1114                                                 &data);
1115
1116         if (count == 0)
1117                 /*
1118                  * If we could not remove anything, then remember
1119                  * what is the max timeout and do nothing if we
1120                  * have not yet reached it. This will prevent
1121                  * constant traversal of the cache if it is full.
1122                  */
1123                 max_timeout = data.max_timeout;
1124         else
1125                 max_timeout = 0;
1126 }
1127
1128 static gboolean cache_invalidate_entry(gpointer key, gpointer value,
1129                                         gpointer user_data)
1130 {
1131         struct cache_entry *entry = value;
1132
1133         /* first, delete any expired elements */
1134         cache_enforce_validity(entry);
1135
1136         /* if anything is not expired, mark the entry for refresh */
1137         if (entry->hits > 0 && (entry->ipv4 || entry->ipv6))
1138                 entry->want_refresh = 1;
1139
1140         /* delete the cached data */
1141         if (entry->ipv4) {
1142                 g_free(entry->ipv4->data);
1143                 g_free(entry->ipv4);
1144                 entry->ipv4 = NULL;
1145         }
1146
1147         if (entry->ipv6) {
1148                 g_free(entry->ipv6->data);
1149                 g_free(entry->ipv6);
1150                 entry->ipv6 = NULL;
1151         }
1152
1153         /* keep the entry if we want it refreshed, delete it otherwise */
1154         if (entry->want_refresh)
1155                 return FALSE;
1156         else
1157                 return TRUE;
1158 }
1159
1160 /*
1161  * cache_invalidate is called from places where the DNS landscape
1162  * has changed, say because connections are added or we entered a VPN.
1163  * The logic is to wipe all cache data, but mark all non-expired
1164  * parts of the cache for refresh rather than deleting the whole cache.
1165  */
1166 static void cache_invalidate(void)
1167 {
1168         DBG("Invalidating the DNS cache %p", cache);
1169
1170         if (cache == NULL)
1171                 return;
1172
1173         g_hash_table_foreach_remove(cache, cache_invalidate_entry, NULL);
1174 }
1175
1176 static void cache_refresh_entry(struct cache_entry *entry)
1177 {
1178
1179         cache_enforce_validity(entry);
1180
1181         if (entry->hits > 2 && entry->ipv4 == NULL)
1182                 entry->want_refresh = 1;
1183         if (entry->hits > 2 && entry->ipv6 == NULL)
1184                 entry->want_refresh = 1;
1185
1186         if (entry->want_refresh) {
1187                 char *c;
1188                 char dns_name[NS_MAXDNAME + 1];
1189                 entry->want_refresh = 0;
1190
1191                 /* turn a DNS name into a hostname with dots */
1192                 strncpy(dns_name, entry->key, NS_MAXDNAME);
1193                 c = dns_name;
1194                 while (c && *c) {
1195                         int jump;
1196                         jump = *c;
1197                         *c = '.';
1198                         c += jump + 1;
1199                 }
1200                 DBG("Refreshing %s\n", dns_name);
1201                 /* then refresh the hostname */
1202                 refresh_dns_entry(entry, &dns_name[1]);
1203         }
1204 }
1205
1206 static void cache_refresh_iterator(gpointer key, gpointer value,
1207                                         gpointer user_data)
1208 {
1209         struct cache_entry *entry = value;
1210
1211         cache_refresh_entry(entry);
1212 }
1213
1214 static void cache_refresh(void)
1215 {
1216         if (cache == NULL)
1217                 return;
1218
1219         g_hash_table_foreach(cache, cache_refresh_iterator, NULL);
1220 }
1221
1222 static int reply_query_type(unsigned char *msg, int len)
1223 {
1224         unsigned char *c;
1225         uint16_t *w;
1226         int l;
1227         int type;
1228
1229         /* skip the header */
1230         c = msg + sizeof(struct domain_hdr);
1231         len -= sizeof(struct domain_hdr);
1232
1233         if (len < 0)
1234                 return 0;
1235
1236         /* now the query, which is a name and 2 16 bit words */
1237         l = dns_name_length(c) + 1;
1238         c += l;
1239         w = (uint16_t *) c;
1240         type = ntohs(*w);
1241
1242         return type;
1243 }
1244
1245 static int cache_update(struct server_data *srv, unsigned char *msg,
1246                         unsigned int msg_len)
1247 {
1248         int offset = protocol_offset(srv->protocol);
1249         int err, qlen, ttl = 0;
1250         uint16_t answers = 0, type = 0, class = 0;
1251         struct domain_hdr *hdr = (void *)(msg + offset);
1252         struct domain_question *q;
1253         struct cache_entry *entry;
1254         struct cache_data *data;
1255         char question[NS_MAXDNAME + 1];
1256         unsigned char response[NS_MAXDNAME + 1];
1257         unsigned char *ptr;
1258         unsigned int rsplen;
1259         gboolean new_entry = TRUE;
1260         time_t current_time;
1261
1262         if (cache_size >= MAX_CACHE_SIZE) {
1263                 cache_cleanup();
1264                 if (cache_size >= MAX_CACHE_SIZE)
1265                         return 0;
1266         }
1267
1268         current_time = time(NULL);
1269
1270         /* don't do a cache refresh more than twice a minute */
1271         if (next_refresh < current_time) {
1272                 cache_refresh();
1273                 next_refresh = current_time + 30;
1274         }
1275
1276         if (offset < 0)
1277                 return 0;
1278
1279         DBG("offset %d hdr %p msg %p rcode %d", offset, hdr, msg, hdr->rcode);
1280
1281         /* Continue only if response code is 0 (=ok) */
1282         if (hdr->rcode != 0)
1283                 return 0;
1284
1285         rsplen = sizeof(response) - 1;
1286         question[sizeof(question) - 1] = '\0';
1287
1288         err = parse_response(msg + offset, msg_len - offset,
1289                                 question, sizeof(question) - 1,
1290                                 &type, &class, &ttl,
1291                                 response, &rsplen, &answers);
1292
1293         /*
1294          * special case: if we do a ipv6 lookup and get no result
1295          * for a record that's already in our ipv4 cache.. we want
1296          * to cache the negative response.
1297          */
1298         if ((err == -ENOMSG || err == -ENOBUFS) &&
1299                         reply_query_type(msg + offset,
1300                                         msg_len - offset) == 28) {
1301                 entry = g_hash_table_lookup(cache, question);
1302                 if (entry && entry->ipv4 && entry->ipv6 == NULL) {
1303                         int cache_offset = 0;
1304
1305                         data = g_try_new(struct cache_data, 1);
1306                         if (data == NULL)
1307                                 return -ENOMEM;
1308                         data->inserted = entry->ipv4->inserted;
1309                         data->type = type;
1310                         data->answers = hdr->ancount;
1311                         data->timeout = entry->ipv4->timeout;
1312                         if (srv->protocol == IPPROTO_UDP)
1313                                 cache_offset = 2;
1314                         data->data_len = msg_len + cache_offset;
1315                         data->data = ptr = g_malloc(data->data_len);
1316                         ptr[0] = (data->data_len - 2) / 256;
1317                         ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1318                         if (srv->protocol == IPPROTO_UDP)
1319                                 ptr += 2;
1320                         data->valid_until = entry->ipv4->valid_until;
1321                         data->cache_until = entry->ipv4->cache_until;
1322                         memcpy(ptr, msg, msg_len);
1323                         entry->ipv6 = data;
1324                         /*
1325                          * we will get a "hit" when we serve the response
1326                          * out of the cache
1327                          */
1328                         entry->hits--;
1329                         if (entry->hits < 0)
1330                                 entry->hits = 0;
1331                         return 0;
1332                 }
1333         }
1334
1335         if (err < 0 || ttl == 0)
1336                 return 0;
1337
1338         qlen = strlen(question);
1339
1340         /*
1341          * If the cache contains already data, check if the
1342          * type of the cached data is the same and do not add
1343          * to cache if data is already there.
1344          * This is needed so that we can cache both A and AAAA
1345          * records for the same name.
1346          */
1347         entry = g_hash_table_lookup(cache, question);
1348         if (entry == NULL) {
1349                 entry = g_try_new(struct cache_entry, 1);
1350                 if (entry == NULL)
1351                         return -ENOMEM;
1352
1353                 data = g_try_new(struct cache_data, 1);
1354                 if (data == NULL) {
1355                         g_free(entry);
1356                         return -ENOMEM;
1357                 }
1358
1359                 entry->key = g_strdup(question);
1360                 entry->ipv4 = entry->ipv6 = NULL;
1361                 entry->want_refresh = 0;
1362                 entry->hits = 0;
1363
1364                 if (type == 1)
1365                         entry->ipv4 = data;
1366                 else
1367                         entry->ipv6 = data;
1368         } else {
1369                 if (type == 1 && entry->ipv4 != NULL)
1370                         return 0;
1371
1372                 if (type == 28 && entry->ipv6 != NULL)
1373                         return 0;
1374
1375                 data = g_try_new(struct cache_data, 1);
1376                 if (data == NULL)
1377                         return -ENOMEM;
1378
1379                 if (type == 1)
1380                         entry->ipv4 = data;
1381                 else
1382                         entry->ipv6 = data;
1383
1384                 /*
1385                  * compensate for the hit we'll get for serving
1386                  * the response out of the cache
1387                  */
1388                 entry->hits--;
1389                 if (entry->hits < 0)
1390                         entry->hits = 0;
1391
1392                 new_entry = FALSE;
1393         }
1394
1395         if (ttl < MIN_CACHE_TTL)
1396                 ttl = MIN_CACHE_TTL;
1397
1398         data->inserted = current_time;
1399         data->type = type;
1400         data->answers = answers;
1401         data->timeout = ttl;
1402         /*
1403          * The "2" in start of the length is the TCP offset. We allocate it
1404          * here even for UDP packet because it simplifies the sending
1405          * of cached packet.
1406          */
1407         data->data_len = 2 + 12 + qlen + 1 + 2 + 2 + rsplen;
1408         data->data = ptr = g_malloc(data->data_len);
1409         data->valid_until = current_time + ttl;
1410
1411         /*
1412          * Restrict the cached DNS record TTL to some sane value
1413          * in order to prevent data staying in the cache too long.
1414          */
1415         if (ttl > MAX_CACHE_TTL)
1416                 ttl = MAX_CACHE_TTL;
1417
1418         data->cache_until = round_down_ttl(current_time + ttl, ttl);
1419
1420         if (data->data == NULL) {
1421                 g_free(entry->key);
1422                 g_free(data);
1423                 g_free(entry);
1424                 return -ENOMEM;
1425         }
1426
1427         /*
1428          * We cache the two extra bytes at the start of the message
1429          * in a TCP packet. When sending UDP packet, we skip the first
1430          * two bytes. This way we do not need to know the format
1431          * (UDP/TCP) of the cached message.
1432          */
1433         ptr[0] = (data->data_len - 2) / 256;
1434         ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1435         if (srv->protocol == IPPROTO_UDP)
1436                 ptr += 2;
1437
1438         memcpy(ptr, msg, offset + 12);
1439         memcpy(ptr + offset + 12, question, qlen + 1); /* copy also the \0 */
1440
1441         q = (void *) (ptr + offset + 12 + qlen + 1);
1442         q->type = htons(type);
1443         q->class = htons(class);
1444         memcpy(ptr + offset + 12 + qlen + 1 + sizeof(struct domain_question),
1445                 response, rsplen);
1446
1447         if (new_entry == TRUE) {
1448                 g_hash_table_replace(cache, entry->key, entry);
1449                 cache_size++;
1450         }
1451
1452         DBG("cache %d %squestion \"%s\" type %d ttl %d size %zd packet %u "
1453                                                                 "dns len %u",
1454                 cache_size, new_entry ? "new " : "old ",
1455                 question, type, ttl,
1456                 sizeof(*entry) + sizeof(*data) + data->data_len + qlen,
1457                 data->data_len,
1458                 srv->protocol == IPPROTO_TCP ?
1459                         (unsigned int)(data->data[0] * 256 + data->data[1]) :
1460                         data->data_len);
1461
1462         return 0;
1463 }
1464
1465 static int ns_resolv(struct server_data *server, struct request_data *req,
1466                                 gpointer request, gpointer name)
1467 {
1468         GList *list;
1469         int sk, err, type = 0;
1470         char *dot, *lookup = (char *) name;
1471         struct cache_entry *entry;
1472
1473         entry = cache_check(request, &type, req->protocol);
1474         if (entry != NULL) {
1475                 int ttl_left = 0;
1476                 struct cache_data *data;
1477
1478                 DBG("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1479                 if (type == 1)
1480                         data = entry->ipv4;
1481                 else
1482                         data = entry->ipv6;
1483
1484                 if (data) {
1485                         ttl_left = data->valid_until - time(NULL);
1486                         entry->hits++;
1487                 }
1488
1489                 if (data != NULL && req->protocol == IPPROTO_TCP) {
1490                         send_cached_response(req->client_sk, data->data,
1491                                         data->data_len, NULL, 0, IPPROTO_TCP,
1492                                         req->srcid, data->answers, ttl_left);
1493                         return 1;
1494                 }
1495
1496                 if (data != NULL && req->protocol == IPPROTO_UDP) {
1497                         int udp_sk = g_io_channel_unix_get_fd(
1498                                         req->ifdata->udp_listener_channel);
1499
1500                         send_cached_response(udp_sk, data->data,
1501                                 data->data_len, &req->sa, req->sa_len,
1502                                 IPPROTO_UDP, req->srcid, data->answers,
1503                                 ttl_left);
1504                         return 1;
1505                 }
1506         }
1507
1508         sk = g_io_channel_unix_get_fd(server->channel);
1509
1510         err = sendto(sk, request, req->request_len, MSG_NOSIGNAL,
1511                         server->server_addr, server->server_addr_len);
1512         if (err < 0) {
1513                 DBG("Cannot send message to server %s sock %d "
1514                         "protocol %d (%s/%d)",
1515                         server->server, sk, server->protocol,
1516                         strerror(errno), errno);
1517                 return -EIO;
1518         }
1519
1520         req->numserv++;
1521
1522         /* If we have more than one dot, we don't add domains */
1523         dot = strchr(lookup, '.');
1524         if (dot != NULL && dot != lookup + strlen(lookup) - 1)
1525                 return 0;
1526
1527         if (server->domains != NULL && server->domains->data != NULL)
1528                 req->append_domain = TRUE;
1529
1530         for (list = server->domains; list; list = list->next) {
1531                 char *domain;
1532                 unsigned char alt[1024];
1533                 struct domain_hdr *hdr = (void *) &alt;
1534                 int altlen, domlen, offset;
1535
1536                 domain = list->data;
1537
1538                 if (domain == NULL)
1539                         continue;
1540
1541                 offset = protocol_offset(server->protocol);
1542                 if (offset < 0)
1543                         return offset;
1544
1545                 domlen = strlen(domain) + 1;
1546                 if (domlen < 5)
1547                         return -EINVAL;
1548
1549                 alt[offset] = req->altid & 0xff;
1550                 alt[offset + 1] = req->altid >> 8;
1551
1552                 memcpy(alt + offset + 2, request + offset + 2, 10);
1553                 hdr->qdcount = htons(1);
1554
1555                 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1556                                         name, domain);
1557                 if (altlen < 0)
1558                         return -EINVAL;
1559
1560                 altlen += 12;
1561
1562                 memcpy(alt + offset + altlen,
1563                         request + offset + altlen - domlen,
1564                                 req->request_len - altlen - offset + domlen);
1565
1566                 if (server->protocol == IPPROTO_TCP) {
1567                         int req_len = req->request_len + domlen - 2;
1568
1569                         alt[0] = (req_len >> 8) & 0xff;
1570                         alt[1] = req_len & 0xff;
1571                 }
1572
1573                 DBG("req %p dstid 0x%04x altid 0x%04x", req, req->dstid,
1574                                 req->altid);
1575
1576                 err = send(sk, alt, req->request_len + domlen, MSG_NOSIGNAL);
1577                 if (err < 0)
1578                         return -EIO;
1579
1580                 req->numserv++;
1581         }
1582
1583         return 0;
1584 }
1585
1586 static void destroy_request_data(struct request_data *req)
1587 {
1588         if (req->timeout > 0)
1589                 g_source_remove(req->timeout);
1590
1591         g_free(req->resp);
1592         g_free(req->request);
1593         g_free(req->name);
1594         g_free(req);
1595 }
1596
1597 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1598                                 struct server_data *data)
1599 {
1600         struct domain_hdr *hdr;
1601         struct request_data *req;
1602         int dns_id, sk, err, offset = protocol_offset(protocol);
1603         struct listener_data *ifdata;
1604
1605         if (offset < 0)
1606                 return offset;
1607
1608         hdr = (void *)(reply + offset);
1609         dns_id = reply[offset] | reply[offset + 1] << 8;
1610
1611         DBG("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1612
1613         req = find_request(dns_id);
1614         if (req == NULL)
1615                 return -EINVAL;
1616
1617         DBG("req %p dstid 0x%04x altid 0x%04x rcode %d",
1618                         req, req->dstid, req->altid, hdr->rcode);
1619
1620         ifdata = req->ifdata;
1621
1622         reply[offset] = req->srcid & 0xff;
1623         reply[offset + 1] = req->srcid >> 8;
1624
1625         req->numresp++;
1626
1627         if (hdr->rcode == 0 || req->resp == NULL) {
1628
1629                 /*
1630                  * If the domain name was append
1631                  * remove it before forwarding the reply.
1632                  */
1633                 if (req->append_domain == TRUE) {
1634                         unsigned int domain_len = 0;
1635                         unsigned char *ptr;
1636                         uint8_t host_len;
1637                         unsigned int header_len;
1638
1639                         /*
1640                          * ptr points to the first char of the hostname.
1641                          * ->hostname.domain.net
1642                          */
1643                         header_len = offset + sizeof(struct domain_hdr);
1644                         ptr = reply + header_len;
1645                         host_len = *ptr;
1646                         if (host_len > 0)
1647                                 domain_len = strnlen((const char *)ptr + 1 +
1648                                                 host_len,
1649                                                 reply_len - header_len);
1650
1651
1652                         DBG("host len %d domain len %d", host_len, domain_len);
1653
1654                         /*
1655                          * Remove the domain name and replace it by the end
1656                          * of reply. Check if the domain is really there
1657                          * before trying to copy the data. The domain_len can
1658                          * be 0 because if the original query did not contain
1659                          * a domain name, then we are sending two packets,
1660                          * first without the domain name and the second packet
1661                          * with domain name. The append_domain is set to true
1662                          * even if we sent the first packet without domain
1663                          * name. In this case we end up in this branch.
1664                          */
1665                         if (domain_len > 0) {
1666                                 /*
1667                                  * Note that we must use memmove() here,
1668                                  * because the memory areas can overlap.
1669                                  */
1670                                 memmove(ptr + host_len + 1,
1671                                         ptr + host_len + domain_len + 1,
1672                                         reply_len - header_len - domain_len);
1673
1674                                 reply_len = reply_len - domain_len;
1675                         }
1676                 }
1677
1678                 g_free(req->resp);
1679                 req->resplen = 0;
1680
1681                 req->resp = g_try_malloc(reply_len);
1682                 if (req->resp == NULL)
1683                         return -ENOMEM;
1684
1685                 memcpy(req->resp, reply, reply_len);
1686                 req->resplen = reply_len;
1687
1688                 cache_update(data, reply, reply_len);
1689         }
1690
1691         if (hdr->rcode > 0 && req->numresp < req->numserv)
1692                 return -EINVAL;
1693
1694         request_list = g_slist_remove(request_list, req);
1695
1696         if (protocol == IPPROTO_UDP) {
1697                 sk = g_io_channel_unix_get_fd(ifdata->udp_listener_channel);
1698                 err = sendto(sk, req->resp, req->resplen, 0,
1699                              &req->sa, req->sa_len);
1700         } else {
1701                 sk = req->client_sk;
1702                 err = send(sk, req->resp, req->resplen, MSG_NOSIGNAL);
1703                 close(sk);
1704         }
1705
1706         if (err < 0)
1707                 DBG("Cannot send msg, sk %d proto %d errno %d/%s", sk,
1708                         protocol, errno, strerror(errno));
1709         else
1710                 DBG("proto %d sent %d bytes to %d", protocol, err, sk);
1711
1712         destroy_request_data(req);
1713
1714         return err;
1715 }
1716
1717 static void cache_element_destroy(gpointer value)
1718 {
1719         struct cache_entry *entry = value;
1720
1721         if (entry == NULL)
1722                 return;
1723
1724         if (entry->ipv4 != NULL) {
1725                 g_free(entry->ipv4->data);
1726                 g_free(entry->ipv4);
1727         }
1728
1729         if (entry->ipv6 != NULL) {
1730                 g_free(entry->ipv6->data);
1731                 g_free(entry->ipv6);
1732         }
1733
1734         g_free(entry->key);
1735         g_free(entry);
1736
1737         if (--cache_size < 0)
1738                 cache_size = 0;
1739 }
1740
1741 static gboolean try_remove_cache(gpointer user_data)
1742 {
1743         if (__sync_fetch_and_sub(&cache_refcount, 1) == 1) {
1744                 DBG("No cache users, removing it.");
1745
1746                 g_hash_table_destroy(cache);
1747                 cache = NULL;
1748         }
1749
1750         return FALSE;
1751 }
1752
1753 static void server_destroy_socket(struct server_data *data)
1754 {
1755         DBG("interface %s server %s proto %d", data->interface,
1756                                         data->server, data->protocol);
1757
1758         if (data->watch > 0) {
1759                 g_source_remove(data->watch);
1760                 data->watch = 0;
1761         }
1762
1763         if (data->timeout > 0) {
1764                 g_source_remove(data->timeout);
1765                 data->timeout = 0;
1766         }
1767
1768         if (data->channel != NULL) {
1769                 g_io_channel_shutdown(data->channel, TRUE, NULL);
1770                 g_io_channel_unref(data->channel);
1771                 data->channel = NULL;
1772         }
1773
1774         g_free(data->incoming_reply);
1775         data->incoming_reply = NULL;
1776 }
1777
1778 static void destroy_server(struct server_data *server)
1779 {
1780         GList *list;
1781
1782         DBG("interface %s server %s sock %d", server->interface, server->server,
1783                         server->channel != NULL ?
1784                         g_io_channel_unix_get_fd(server->channel): -1);
1785
1786         server_list = g_slist_remove(server_list, server);
1787         server_destroy_socket(server);
1788
1789         if (server->protocol == IPPROTO_UDP && server->enabled)
1790                 DBG("Removing DNS server %s", server->server);
1791
1792         g_free(server->server);
1793         for (list = server->domains; list; list = list->next) {
1794                 char *domain = list->data;
1795
1796                 server->domains = g_list_remove(server->domains, domain);
1797                 g_free(domain);
1798         }
1799         g_free(server->interface);
1800         g_free(server->server_addr);
1801
1802         /*
1803          * We do not remove cache right away but delay it few seconds.
1804          * The idea is that when IPv6 DNS server is added via RDNSS, it has a
1805          * lifetime. When the lifetime expires we decrease the refcount so it
1806          * is possible that the cache is then removed. Because a new DNS server
1807          * is usually created almost immediately we would then loose the cache
1808          * without any good reason. The small delay allows the new RDNSS to
1809          * create a new DNS server instance and the refcount does not go to 0.
1810          */
1811         g_timeout_add_seconds(3, try_remove_cache, NULL);
1812
1813         g_free(server);
1814 }
1815
1816 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
1817                                                         gpointer user_data)
1818 {
1819         unsigned char buf[4096];
1820         int sk, err, len;
1821         struct server_data *data = user_data;
1822
1823         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1824                 connman_error("Error with UDP server %s", data->server);
1825                 server_destroy_socket(data);
1826                 return FALSE;
1827         }
1828
1829         sk = g_io_channel_unix_get_fd(channel);
1830
1831         len = recv(sk, buf, sizeof(buf), 0);
1832         if (len < 12)
1833                 return TRUE;
1834
1835         err = forward_dns_reply(buf, len, IPPROTO_UDP, data);
1836         if (err < 0)
1837                 return TRUE;
1838
1839         return TRUE;
1840 }
1841
1842 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
1843                                                         gpointer user_data)
1844 {
1845         int sk;
1846         struct server_data *server = user_data;
1847
1848         sk = g_io_channel_unix_get_fd(channel);
1849         if (sk == 0)
1850                 return FALSE;
1851
1852         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
1853                 GSList *list;
1854 hangup:
1855                 DBG("TCP server channel closed, sk %d", sk);
1856
1857                 /*
1858                  * Discard any partial response which is buffered; better
1859                  * to get a proper response from a working server.
1860                  */
1861                 g_free(server->incoming_reply);
1862                 server->incoming_reply = NULL;
1863
1864                 for (list = request_list; list; list = list->next) {
1865                         struct request_data *req = list->data;
1866                         struct domain_hdr *hdr;
1867
1868                         if (req->protocol == IPPROTO_UDP)
1869                                 continue;
1870
1871                         if (req->request == NULL)
1872                                 continue;
1873
1874                         /*
1875                          * If we're not waiting for any further response
1876                          * from another name server, then we send an error
1877                          * response to the client.
1878                          */
1879                         if (req->numserv && --(req->numserv))
1880                                 continue;
1881
1882                         hdr = (void *) (req->request + 2);
1883                         hdr->id = req->srcid;
1884                         send_response(req->client_sk, req->request,
1885                                 req->request_len, NULL, 0, IPPROTO_TCP);
1886
1887                         request_list = g_slist_remove(request_list, req);
1888                 }
1889
1890                 destroy_server(server);
1891
1892                 return FALSE;
1893         }
1894
1895         if ((condition & G_IO_OUT) && !server->connected) {
1896                 GSList *list;
1897                 GList *domains;
1898                 int no_request_sent = TRUE;
1899                 struct server_data *udp_server;
1900
1901                 udp_server = find_server(server->interface, server->server,
1902                                                                 IPPROTO_UDP);
1903                 if (udp_server != NULL) {
1904                         for (domains = udp_server->domains; domains;
1905                                                 domains = domains->next) {
1906                                 char *dom = domains->data;
1907
1908                                 DBG("Adding domain %s to %s",
1909                                                 dom, server->server);
1910
1911                                 server->domains = g_list_append(server->domains,
1912                                                                 g_strdup(dom));
1913                         }
1914                 }
1915
1916                 server->connected = TRUE;
1917                 server_list = g_slist_append(server_list, server);
1918
1919                 if (server->timeout > 0) {
1920                         g_source_remove(server->timeout);
1921                         server->timeout = 0;
1922                 }
1923
1924                 for (list = request_list; list; ) {
1925                         struct request_data *req = list->data;
1926                         int status;
1927
1928                         if (req->protocol == IPPROTO_UDP) {
1929                                 list = list->next;
1930                                 continue;
1931                         }
1932
1933                         DBG("Sending req %s over TCP", (char *)req->name);
1934
1935                         status = ns_resolv(server, req,
1936                                                 req->request, req->name);
1937                         if (status > 0) {
1938                                 /*
1939                                  * A cached result was sent,
1940                                  * so the request can be released
1941                                  */
1942                                 list = list->next;
1943                                 request_list = g_slist_remove(request_list, req);
1944                                 destroy_request_data(req);
1945                                 continue;
1946                         }
1947
1948                         if (status < 0) {
1949                                 list = list->next;
1950                                 continue;
1951                         }
1952
1953                         no_request_sent = FALSE;
1954
1955                         if (req->timeout > 0)
1956                                 g_source_remove(req->timeout);
1957
1958                         req->timeout = g_timeout_add_seconds(30,
1959                                                 request_timeout, req);
1960                         list = list->next;
1961                 }
1962
1963                 if (no_request_sent == TRUE) {
1964                         destroy_server(server);
1965                         return FALSE;
1966                 }
1967
1968         } else if (condition & G_IO_IN) {
1969                 struct partial_reply *reply = server->incoming_reply;
1970                 int bytes_recv;
1971
1972                 if (!reply) {
1973                         unsigned char reply_len_buf[2];
1974                         uint16_t reply_len;
1975
1976                         bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
1977                         if (!bytes_recv) {
1978                                 goto hangup;
1979                         } else if (bytes_recv < 0) {
1980                                 if (errno == EAGAIN || errno == EWOULDBLOCK)
1981                                         return TRUE;
1982
1983                                 connman_error("DNS proxy error %s",
1984                                                 strerror(errno));
1985                                 goto hangup;
1986                         } else if (bytes_recv < 2)
1987                                 return TRUE;
1988
1989                         reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
1990                         reply_len += 2;
1991
1992                         DBG("TCP reply %d bytes from %d", reply_len, sk);
1993
1994                         reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
1995                         if (!reply)
1996                                 return TRUE;
1997
1998                         reply->len = reply_len;
1999                         reply->received = 0;
2000
2001                         server->incoming_reply = reply;
2002                 }
2003
2004                 while (reply->received < reply->len) {
2005                         bytes_recv = recv(sk, reply->buf + reply->received,
2006                                         reply->len - reply->received, 0);
2007                         if (!bytes_recv) {
2008                                 connman_error("DNS proxy TCP disconnect");
2009                                 break;
2010                         } else if (bytes_recv < 0) {
2011                                 if (errno == EAGAIN || errno == EWOULDBLOCK)
2012                                         return TRUE;
2013
2014                                 connman_error("DNS proxy error %s",
2015                                                 strerror(errno));
2016                                 break;
2017                         }
2018                         reply->received += bytes_recv;
2019                 }
2020
2021                 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
2022                                         server);
2023
2024                 g_free(reply);
2025                 server->incoming_reply = NULL;
2026
2027                 destroy_server(server);
2028
2029                 return FALSE;
2030         }
2031
2032         return TRUE;
2033 }
2034
2035 static gboolean tcp_idle_timeout(gpointer user_data)
2036 {
2037         struct server_data *server = user_data;
2038
2039         DBG("");
2040
2041         if (server == NULL)
2042                 return FALSE;
2043
2044         destroy_server(server);
2045
2046         return FALSE;
2047 }
2048
2049 static int server_create_socket(struct server_data *data)
2050 {
2051         int sk, err;
2052
2053         DBG("interface %s server %s proto %d", data->interface,
2054                                         data->server, data->protocol);
2055
2056         sk = socket(data->server_addr->sa_family,
2057                 data->protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM,
2058                 data->protocol);
2059         if (sk < 0) {
2060                 err = errno;
2061                 connman_error("Failed to create server %s socket",
2062                                                         data->server);
2063                 server_destroy_socket(data);
2064                 return -err;
2065         }
2066
2067         DBG("sk %d", sk);
2068
2069         if (data->interface != NULL) {
2070                 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2071                                         data->interface,
2072                                         strlen(data->interface) + 1) < 0) {
2073                         err = errno;
2074                         connman_error("Failed to bind server %s "
2075                                                 "to interface %s",
2076                                                 data->server, data->interface);
2077                         close(sk);
2078                         server_destroy_socket(data);
2079                         return -err;
2080                 }
2081         }
2082
2083         data->channel = g_io_channel_unix_new(sk);
2084         if (data->channel == NULL) {
2085                 connman_error("Failed to create server %s channel",
2086                                                         data->server);
2087                 close(sk);
2088                 server_destroy_socket(data);
2089                 return -ENOMEM;
2090         }
2091
2092         g_io_channel_set_close_on_unref(data->channel, TRUE);
2093
2094         if (data->protocol == IPPROTO_TCP) {
2095                 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
2096                 data->watch = g_io_add_watch(data->channel,
2097                         G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
2098                                                 tcp_server_event, data);
2099                 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
2100                                                                 data);
2101         } else
2102                 data->watch = g_io_add_watch(data->channel,
2103                         G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
2104                                                 udp_server_event, data);
2105
2106         if (connect(sk, data->server_addr, data->server_addr_len) < 0) {
2107                 err = errno;
2108
2109                 if ((data->protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
2110                                 data->protocol == IPPROTO_UDP) {
2111
2112                         connman_error("Failed to connect to server %s",
2113                                                                 data->server);
2114                         server_destroy_socket(data);
2115                         return -err;
2116                 }
2117         }
2118
2119         if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
2120                 cache = g_hash_table_new_full(g_str_hash,
2121                                         g_str_equal,
2122                                         NULL,
2123                                         cache_element_destroy);
2124
2125         return 0;
2126 }
2127
2128 static struct server_data *create_server(const char *interface,
2129                                         const char *domain, const char *server,
2130                                         int protocol)
2131 {
2132         struct server_data *data;
2133         struct addrinfo hints, *rp;
2134         int ret;
2135
2136         DBG("interface %s server %s", interface, server);
2137
2138         data = g_try_new0(struct server_data, 1);
2139         if (data == NULL) {
2140                 connman_error("Failed to allocate server %s data", server);
2141                 return NULL;
2142         }
2143
2144         data->interface = g_strdup(interface);
2145         if (domain)
2146                 data->domains = g_list_append(data->domains, g_strdup(domain));
2147         data->server = g_strdup(server);
2148         data->protocol = protocol;
2149
2150         memset(&hints, 0, sizeof(hints));
2151
2152         switch (protocol) {
2153         case IPPROTO_UDP:
2154                 hints.ai_socktype = SOCK_DGRAM;
2155                 break;
2156
2157         case IPPROTO_TCP:
2158                 hints.ai_socktype = SOCK_STREAM;
2159                 break;
2160
2161         default:
2162                 destroy_server(data);
2163                 return NULL;
2164         }
2165         hints.ai_family = AF_UNSPEC;
2166         hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST;
2167
2168         ret = getaddrinfo(data->server, "53", &hints, &rp);
2169         if (ret) {
2170                 connman_error("Failed to parse server %s address: %s\n",
2171                               data->server, gai_strerror(ret));
2172                 destroy_server(data);
2173                 return NULL;
2174         }
2175
2176         /* Do not blindly copy this code elsewhere; it doesn't loop over the
2177            results using ->ai_next as it should. That's OK in *this* case
2178            because it was a numeric lookup; we *know* there's only one. */
2179
2180         data->server_addr_len = rp->ai_addrlen;
2181
2182         switch (rp->ai_family) {
2183         case AF_INET:
2184                 data->server_addr = (struct sockaddr *)
2185                                         g_try_new0(struct sockaddr_in, 1);
2186                 break;
2187         case AF_INET6:
2188                 data->server_addr = (struct sockaddr *)
2189                                         g_try_new0(struct sockaddr_in6, 1);
2190                 break;
2191         default:
2192                 connman_error("Wrong address family %d", rp->ai_family);
2193                 break;
2194         }
2195         if (data->server_addr == NULL) {
2196                 freeaddrinfo(rp);
2197                 destroy_server(data);
2198                 return NULL;
2199         }
2200         memcpy(data->server_addr, rp->ai_addr, rp->ai_addrlen);
2201         freeaddrinfo(rp);
2202
2203         if (server_create_socket(data) != 0) {
2204                 destroy_server(data);
2205                 return NULL;
2206         }
2207
2208         if (protocol == IPPROTO_UDP) {
2209                 /* Enable new servers by default */
2210                 data->enabled = TRUE;
2211                 DBG("Adding DNS server %s", data->server);
2212
2213                 server_list = g_slist_append(server_list, data);
2214         }
2215
2216         return data;
2217 }
2218
2219 static gboolean resolv(struct request_data *req,
2220                                 gpointer request, gpointer name)
2221 {
2222         GSList *list;
2223
2224         for (list = server_list; list; list = list->next) {
2225                 struct server_data *data = list->data;
2226
2227                 DBG("server %s enabled %d", data->server, data->enabled);
2228
2229                 if (data->enabled == FALSE)
2230                         continue;
2231
2232                 if (data->channel == NULL && data->protocol == IPPROTO_UDP) {
2233                         if (server_create_socket(data) < 0) {
2234                                 DBG("socket creation failed while resolving");
2235                                 continue;
2236                         }
2237                 }
2238
2239                 if (ns_resolv(data, req, request, name) > 0)
2240                         return TRUE;
2241         }
2242
2243         return FALSE;
2244 }
2245
2246 static void append_domain(const char *interface, const char *domain)
2247 {
2248         GSList *list;
2249
2250         DBG("interface %s domain %s", interface, domain);
2251
2252         if (domain == NULL)
2253                 return;
2254
2255         for (list = server_list; list; list = list->next) {
2256                 struct server_data *data = list->data;
2257                 GList *dom_list;
2258                 char *dom;
2259                 gboolean dom_found = FALSE;
2260
2261                 if (data->interface == NULL)
2262                         continue;
2263
2264                 if (g_str_equal(data->interface, interface) == FALSE)
2265                         continue;
2266
2267                 for (dom_list = data->domains; dom_list;
2268                                 dom_list = dom_list->next) {
2269                         dom = dom_list->data;
2270
2271                         if (g_str_equal(dom, domain)) {
2272                                 dom_found = TRUE;
2273                                 break;
2274                         }
2275                 }
2276
2277                 if (dom_found == FALSE) {
2278                         data->domains =
2279                                 g_list_append(data->domains, g_strdup(domain));
2280                 }
2281         }
2282 }
2283
2284 int __connman_dnsproxy_append(const char *interface, const char *domain,
2285                                                         const char *server)
2286 {
2287         struct server_data *data;
2288
2289         DBG("interface %s server %s", interface, server);
2290
2291         if (server == NULL && domain == NULL)
2292                 return -EINVAL;
2293
2294         if (server == NULL) {
2295                 append_domain(interface, domain);
2296
2297                 return 0;
2298         }
2299
2300         if (g_str_equal(server, "127.0.0.1") == TRUE)
2301                 return -ENODEV;
2302
2303         data = find_server(interface, server, IPPROTO_UDP);
2304         if (data != NULL) {
2305                 append_domain(interface, domain);
2306                 return 0;
2307         }
2308
2309         data = create_server(interface, domain, server, IPPROTO_UDP);
2310         if (data == NULL)
2311                 return -EIO;
2312
2313         return 0;
2314 }
2315
2316 static void remove_server(const char *interface, const char *domain,
2317                         const char *server, int protocol)
2318 {
2319         struct server_data *data;
2320
2321         data = find_server(interface, server, protocol);
2322         if (data == NULL)
2323                 return;
2324
2325         destroy_server(data);
2326 }
2327
2328 int __connman_dnsproxy_remove(const char *interface, const char *domain,
2329                                                         const char *server)
2330 {
2331         DBG("interface %s server %s", interface, server);
2332
2333         if (server == NULL)
2334                 return -EINVAL;
2335
2336         if (g_str_equal(server, "127.0.0.1") == TRUE)
2337                 return -ENODEV;
2338
2339         remove_server(interface, domain, server, IPPROTO_UDP);
2340         remove_server(interface, domain, server, IPPROTO_TCP);
2341
2342         return 0;
2343 }
2344
2345 void __connman_dnsproxy_flush(void)
2346 {
2347         GSList *list;
2348
2349         list = request_list;
2350         while (list) {
2351                 struct request_data *req = list->data;
2352
2353                 list = list->next;
2354
2355                 if (resolv(req, req->request, req->name) == TRUE) {
2356                         /*
2357                          * A cached result was sent,
2358                          * so the request can be released
2359                          */
2360                         request_list =
2361                                 g_slist_remove(request_list, req);
2362                         destroy_request_data(req);
2363                         continue;
2364                 }
2365
2366                 if (req->timeout > 0)
2367                         g_source_remove(req->timeout);
2368                 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2369         }
2370 }
2371
2372 static void dnsproxy_offline_mode(connman_bool_t enabled)
2373 {
2374         GSList *list;
2375
2376         DBG("enabled %d", enabled);
2377
2378         for (list = server_list; list; list = list->next) {
2379                 struct server_data *data = list->data;
2380
2381                 if (enabled == FALSE) {
2382                         DBG("Enabling DNS server %s", data->server);
2383                         data->enabled = TRUE;
2384                         cache_invalidate();
2385                         cache_refresh();
2386                 } else {
2387                         DBG("Disabling DNS server %s", data->server);
2388                         data->enabled = FALSE;
2389                         cache_invalidate();
2390                 }
2391         }
2392 }
2393
2394 static void dnsproxy_default_changed(struct connman_service *service)
2395 {
2396         GSList *list;
2397         char *interface;
2398
2399         DBG("service %p", service);
2400
2401         /* DNS has changed, invalidate the cache */
2402         cache_invalidate();
2403
2404         if (service == NULL) {
2405                 /* When no services are active, then disable DNS proxying */
2406                 dnsproxy_offline_mode(TRUE);
2407                 return;
2408         }
2409
2410         interface = connman_service_get_interface(service);
2411         if (interface == NULL)
2412                 return;
2413
2414         for (list = server_list; list; list = list->next) {
2415                 struct server_data *data = list->data;
2416
2417                 if (g_strcmp0(data->interface, interface) == 0) {
2418                         DBG("Enabling DNS server %s", data->server);
2419                         data->enabled = TRUE;
2420                 } else {
2421                         DBG("Disabling DNS server %s", data->server);
2422                         data->enabled = FALSE;
2423                 }
2424         }
2425
2426         g_free(interface);
2427         cache_refresh();
2428 }
2429
2430 static struct connman_notifier dnsproxy_notifier = {
2431         .name                   = "dnsproxy",
2432         .default_changed        = dnsproxy_default_changed,
2433         .offline_mode           = dnsproxy_offline_mode,
2434 };
2435
2436 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
2437
2438 static int parse_request(unsigned char *buf, int len,
2439                                         char *name, unsigned int size)
2440 {
2441         struct domain_hdr *hdr = (void *) buf;
2442         uint16_t qdcount = ntohs(hdr->qdcount);
2443         uint16_t arcount = ntohs(hdr->arcount);
2444         unsigned char *ptr;
2445         char *last_label = NULL;
2446         unsigned int remain, used = 0;
2447
2448         if (len < 12)
2449                 return -EINVAL;
2450
2451         DBG("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
2452                                         hdr->id, hdr->qr, hdr->opcode,
2453                                                         qdcount, arcount);
2454
2455         if (hdr->qr != 0 || qdcount != 1)
2456                 return -EINVAL;
2457
2458         name[0] = '\0';
2459
2460         ptr = buf + sizeof(struct domain_hdr);
2461         remain = len - sizeof(struct domain_hdr);
2462
2463         while (remain > 0) {
2464                 uint8_t label_len = *ptr;
2465
2466                 if (label_len == 0x00) {
2467                         last_label = (char *) (ptr + 1);
2468                         break;
2469                 }
2470
2471                 if (used + label_len + 1 > size)
2472                         return -ENOBUFS;
2473
2474                 strncat(name, (char *) (ptr + 1), label_len);
2475                 strcat(name, ".");
2476
2477                 used += label_len + 1;
2478
2479                 ptr += label_len + 1;
2480                 remain -= label_len + 1;
2481         }
2482
2483         if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
2484                                 !memcmp(last_label + 5, opt_edns0_type, 2)) {
2485                 uint16_t edns0_bufsize;
2486
2487                 edns0_bufsize = last_label[7] << 8 | last_label[8];
2488
2489                 DBG("EDNS0 buffer size %u", edns0_bufsize);
2490
2491                 /* This is an evil hack until full TCP support has been
2492                  * implemented.
2493                  *
2494                  * Somtimes the EDNS0 request gets send with a too-small
2495                  * buffer size. Since glibc doesn't seem to crash when it
2496                  * gets a response biffer then it requested, just bump
2497                  * the buffer size up to 4KiB.
2498                  */
2499                 if (edns0_bufsize < 0x1000) {
2500                         last_label[7] = 0x10;
2501                         last_label[8] = 0x00;
2502                 }
2503         }
2504
2505         DBG("query %s", name);
2506
2507         return 0;
2508 }
2509
2510 static gboolean tcp_listener_event(GIOChannel *channel, GIOCondition condition,
2511                                                         gpointer user_data)
2512 {
2513         unsigned char buf[768];
2514         char query[512];
2515         struct request_data *req;
2516         int sk, client_sk, len, err;
2517         struct sockaddr_in6 client_addr;
2518         socklen_t client_addr_len = sizeof(client_addr);
2519         GSList *list;
2520         struct listener_data *ifdata = user_data;
2521         int waiting_for_connect = FALSE, qtype = 0;
2522         struct cache_entry *entry;
2523
2524         DBG("condition 0x%x", condition);
2525
2526         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2527                 if (ifdata->tcp_listener_watch > 0)
2528                         g_source_remove(ifdata->tcp_listener_watch);
2529                 ifdata->tcp_listener_watch = 0;
2530
2531                 connman_error("Error with TCP listener channel");
2532
2533                 return FALSE;
2534         }
2535
2536         sk = g_io_channel_unix_get_fd(channel);
2537
2538         client_sk = accept(sk, (void *)&client_addr, &client_addr_len);
2539         if (client_sk < 0) {
2540                 connman_error("Accept failure on TCP listener");
2541                 ifdata->tcp_listener_watch = 0;
2542                 return FALSE;
2543         }
2544
2545         len = recv(client_sk, buf, sizeof(buf), 0);
2546         if (len < 2)
2547                 return TRUE;
2548
2549         DBG("Received %d bytes (id 0x%04x) from %d", len,
2550                 buf[2] | buf[3] << 8, client_sk);
2551
2552         err = parse_request(buf + 2, len - 2, query, sizeof(query));
2553         if (err < 0 || (g_slist_length(server_list) == 0)) {
2554                 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2555                 return TRUE;
2556         }
2557
2558         req = g_try_new0(struct request_data, 1);
2559         if (req == NULL)
2560                 return TRUE;
2561
2562         memcpy(&req->sa, &client_addr, client_addr_len);
2563         req->sa_len = client_addr_len;
2564         req->client_sk = client_sk;
2565         req->protocol = IPPROTO_TCP;
2566
2567         req->srcid = buf[2] | (buf[3] << 8);
2568         req->dstid = get_id();
2569         req->altid = get_id();
2570         req->request_len = len;
2571
2572         buf[2] = req->dstid & 0xff;
2573         buf[3] = req->dstid >> 8;
2574
2575         req->numserv = 0;
2576         req->ifdata = (struct listener_data *) ifdata;
2577         req->append_domain = FALSE;
2578
2579         /*
2580          * Check if the answer is found in the cache before
2581          * creating sockets to the server.
2582          */
2583         entry = cache_check(buf, &qtype, IPPROTO_TCP);
2584         if (entry != NULL) {
2585                 int ttl_left = 0;
2586                 struct cache_data *data;
2587
2588                 DBG("cache hit %s type %s", query, qtype == 1 ? "A" : "AAAA");
2589                 if (qtype == 1)
2590                         data = entry->ipv4;
2591                 else
2592                         data = entry->ipv6;
2593
2594                 if (data != NULL) {
2595                         ttl_left = data->valid_until - time(NULL);
2596                         entry->hits++;
2597
2598                         send_cached_response(client_sk, data->data,
2599                                         data->data_len, NULL, 0, IPPROTO_TCP,
2600                                         req->srcid, data->answers, ttl_left);
2601
2602                         g_free(req);
2603                         return TRUE;
2604                 } else
2605                         DBG("data missing, ignoring cache for this query");
2606         }
2607
2608         for (list = server_list; list; list = list->next) {
2609                 struct server_data *data = list->data;
2610
2611                 if (data->protocol != IPPROTO_UDP || data->enabled == FALSE)
2612                         continue;
2613
2614                 if(create_server(data->interface, NULL,
2615                                         data->server, IPPROTO_TCP) == NULL)
2616                         continue;
2617
2618                 waiting_for_connect = TRUE;
2619         }
2620
2621         if (waiting_for_connect == FALSE) {
2622                 /* No server is waiting for connect */
2623                 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2624                 g_free(req);
2625                 return TRUE;
2626         }
2627
2628         /*
2629          * The server is not connected yet.
2630          * Copy the relevant buffers.
2631          * The request will actually be sent once we're
2632          * properly connected over TCP to the nameserver.
2633          */
2634         req->request = g_try_malloc0(req->request_len);
2635         if (req->request == NULL) {
2636                 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2637                 g_free(req);
2638                 return TRUE;
2639         }
2640         memcpy(req->request, buf, req->request_len);
2641
2642         req->name = g_try_malloc0(sizeof(query));
2643         if (req->name == NULL) {
2644                 send_response(client_sk, buf, len, NULL, 0, IPPROTO_TCP);
2645                 g_free(req->request);
2646                 g_free(req);
2647                 return TRUE;
2648         }
2649         memcpy(req->name, query, sizeof(query));
2650
2651         req->timeout = g_timeout_add_seconds(30, request_timeout, req);
2652
2653         request_list = g_slist_append(request_list, req);
2654
2655         return TRUE;
2656 }
2657
2658 static gboolean udp_listener_event(GIOChannel *channel, GIOCondition condition,
2659                                                         gpointer user_data)
2660 {
2661         unsigned char buf[768];
2662         char query[512];
2663         struct request_data *req;
2664         struct sockaddr_in6 client_addr;
2665         socklen_t client_addr_len = sizeof(client_addr);
2666         int sk, err, len;
2667         struct listener_data *ifdata = user_data;
2668
2669         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2670                 connman_error("Error with UDP listener channel");
2671                 ifdata->udp_listener_watch = 0;
2672                 return FALSE;
2673         }
2674
2675         sk = g_io_channel_unix_get_fd(channel);
2676
2677         memset(&client_addr, 0, client_addr_len);
2678         len = recvfrom(sk, buf, sizeof(buf), 0, (void *)&client_addr,
2679                        &client_addr_len);
2680         if (len < 2)
2681                 return TRUE;
2682
2683         DBG("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
2684
2685         err = parse_request(buf, len, query, sizeof(query));
2686         if (err < 0 || (g_slist_length(server_list) == 0)) {
2687                 send_response(sk, buf, len, (void *)&client_addr,
2688                                 client_addr_len, IPPROTO_UDP);
2689                 return TRUE;
2690         }
2691
2692         req = g_try_new0(struct request_data, 1);
2693         if (req == NULL)
2694                 return TRUE;
2695
2696         memcpy(&req->sa, &client_addr, client_addr_len);
2697         req->sa_len = client_addr_len;
2698         req->client_sk = 0;
2699         req->protocol = IPPROTO_UDP;
2700
2701         req->srcid = buf[0] | (buf[1] << 8);
2702         req->dstid = get_id();
2703         req->altid = get_id();
2704         req->request_len = len;
2705
2706         buf[0] = req->dstid & 0xff;
2707         buf[1] = req->dstid >> 8;
2708
2709         req->numserv = 0;
2710         req->ifdata = (struct listener_data *) ifdata;
2711         req->append_domain = FALSE;
2712
2713         if (resolv(req, buf, query) == TRUE) {
2714                 /* a cached result was sent, so the request can be released */
2715                 g_free(req);
2716                 return TRUE;
2717         }
2718
2719         req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2720         request_list = g_slist_append(request_list, req);
2721
2722         return TRUE;
2723 }
2724
2725 static int create_dns_listener(int protocol, struct listener_data *ifdata)
2726 {
2727         GIOChannel *channel;
2728         const char *proto;
2729         union {
2730                 struct sockaddr sa;
2731                 struct sockaddr_in6 sin6;
2732                 struct sockaddr_in sin;
2733         } s;
2734         socklen_t slen;
2735         int sk, type, v6only = 0;
2736         int family = AF_INET6;
2737
2738
2739         DBG("interface %s", ifdata->ifname);
2740
2741         switch (protocol) {
2742         case IPPROTO_UDP:
2743                 proto = "UDP";
2744                 type = SOCK_DGRAM | SOCK_CLOEXEC;
2745                 break;
2746
2747         case IPPROTO_TCP:
2748                 proto = "TCP";
2749                 type = SOCK_STREAM | SOCK_CLOEXEC;
2750                 break;
2751
2752         default:
2753                 return -EINVAL;
2754         }
2755
2756         sk = socket(family, type, protocol);
2757         if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
2758                 connman_error("No IPv6 support; DNS proxy listening only on Legacy IP");
2759                 family = AF_INET;
2760                 sk = socket(family, type, protocol);
2761         }
2762         if (sk < 0) {
2763                 connman_error("Failed to create %s listener socket", proto);
2764                 return -EIO;
2765         }
2766
2767         if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2768                                         ifdata->ifname,
2769                                         strlen(ifdata->ifname) + 1) < 0) {
2770                 connman_error("Failed to bind %s listener interface", proto);
2771                 close(sk);
2772                 return -EIO;
2773         }
2774         /* Ensure it accepts Legacy IP connections too */
2775         if (family == AF_INET6 &&
2776                         setsockopt(sk, SOL_IPV6, IPV6_V6ONLY,
2777                                         &v6only, sizeof(v6only)) < 0) {
2778                 connman_error("Failed to clear V6ONLY on %s listener socket",
2779                               proto);
2780                 close(sk);
2781                 return -EIO;
2782         }
2783
2784         if (family == AF_INET) {
2785                 memset(&s.sin, 0, sizeof(s.sin));
2786                 s.sin.sin_family = AF_INET;
2787                 s.sin.sin_port = htons(53);
2788                 s.sin.sin_addr.s_addr = htonl(INADDR_ANY);
2789                 slen = sizeof(s.sin);
2790         } else {
2791                 memset(&s.sin6, 0, sizeof(s.sin6));
2792                 s.sin6.sin6_family = AF_INET6;
2793                 s.sin6.sin6_port = htons(53);
2794                 s.sin6.sin6_addr = in6addr_any;
2795                 slen = sizeof(s.sin6);
2796         }
2797
2798         if (bind(sk, &s.sa, slen) < 0) {
2799                 connman_error("Failed to bind %s listener socket", proto);
2800                 close(sk);
2801                 return -EIO;
2802         }
2803
2804         if (protocol == IPPROTO_TCP && listen(sk, 10) < 0) {
2805                 connman_error("Failed to listen on TCP socket");
2806                 close(sk);
2807                 return -EIO;
2808         }
2809
2810         channel = g_io_channel_unix_new(sk);
2811         if (channel == NULL) {
2812                 connman_error("Failed to create %s listener channel", proto);
2813                 close(sk);
2814                 return -EIO;
2815         }
2816
2817         g_io_channel_set_close_on_unref(channel, TRUE);
2818
2819         if (protocol == IPPROTO_TCP) {
2820                 ifdata->tcp_listener_channel = channel;
2821                 ifdata->tcp_listener_watch = g_io_add_watch(channel,
2822                                 G_IO_IN, tcp_listener_event, (gpointer) ifdata);
2823         } else {
2824                 ifdata->udp_listener_channel = channel;
2825                 ifdata->udp_listener_watch = g_io_add_watch(channel,
2826                                 G_IO_IN, udp_listener_event, (gpointer) ifdata);
2827         }
2828
2829         return 0;
2830 }
2831
2832 static void destroy_udp_listener(struct listener_data *ifdata)
2833 {
2834         DBG("interface %s", ifdata->ifname);
2835
2836         if (ifdata->udp_listener_watch > 0)
2837                 g_source_remove(ifdata->udp_listener_watch);
2838
2839         g_io_channel_unref(ifdata->udp_listener_channel);
2840 }
2841
2842 static void destroy_tcp_listener(struct listener_data *ifdata)
2843 {
2844         DBG("interface %s", ifdata->ifname);
2845
2846         if (ifdata->tcp_listener_watch > 0)
2847                 g_source_remove(ifdata->tcp_listener_watch);
2848
2849         g_io_channel_unref(ifdata->tcp_listener_channel);
2850 }
2851
2852 static int create_listener(struct listener_data *ifdata)
2853 {
2854         int err;
2855
2856         err = create_dns_listener(IPPROTO_UDP, ifdata);
2857         if (err < 0)
2858                 return err;
2859
2860         err = create_dns_listener(IPPROTO_TCP, ifdata);
2861         if (err < 0) {
2862                 destroy_udp_listener(ifdata);
2863                 return err;
2864         }
2865
2866         if (g_strcmp0(ifdata->ifname, "lo") == 0)
2867                 __connman_resolvfile_append("lo", NULL, "127.0.0.1");
2868
2869         return 0;
2870 }
2871
2872 static void destroy_listener(struct listener_data *ifdata)
2873 {
2874         GSList *list;
2875
2876         if (g_strcmp0(ifdata->ifname, "lo") == 0)
2877                 __connman_resolvfile_remove("lo", NULL, "127.0.0.1");
2878
2879         for (list = request_list; list; list = list->next) {
2880                 struct request_data *req = list->data;
2881
2882                 DBG("Dropping request (id 0x%04x -> 0x%04x)",
2883                                                 req->srcid, req->dstid);
2884                 destroy_request_data(req);
2885                 list->data = NULL;
2886         }
2887
2888         g_slist_free(request_list);
2889         request_list = NULL;
2890
2891         destroy_tcp_listener(ifdata);
2892         destroy_udp_listener(ifdata);
2893 }
2894
2895 int __connman_dnsproxy_add_listener(const char *interface)
2896 {
2897         struct listener_data *ifdata;
2898         int err;
2899
2900         DBG("interface %s", interface);
2901
2902         if (g_hash_table_lookup(listener_table, interface) != NULL)
2903                 return 0;
2904
2905         ifdata = g_try_new0(struct listener_data, 1);
2906         if (ifdata == NULL)
2907                 return -ENOMEM;
2908
2909         ifdata->ifname = g_strdup(interface);
2910         ifdata->udp_listener_channel = NULL;
2911         ifdata->udp_listener_watch = 0;
2912         ifdata->tcp_listener_channel = NULL;
2913         ifdata->tcp_listener_watch = 0;
2914
2915         err = create_listener(ifdata);
2916         if (err < 0) {
2917                 connman_error("Couldn't create listener for %s err %d",
2918                                 interface, err);
2919                 g_free(ifdata->ifname);
2920                 g_free(ifdata);
2921                 return err;
2922         }
2923         g_hash_table_insert(listener_table, ifdata->ifname, ifdata);
2924         return 0;
2925 }
2926
2927 void __connman_dnsproxy_remove_listener(const char *interface)
2928 {
2929         struct listener_data *ifdata;
2930
2931         DBG("interface %s", interface);
2932
2933         ifdata = g_hash_table_lookup(listener_table, interface);
2934         if (ifdata == NULL)
2935                 return;
2936
2937         destroy_listener(ifdata);
2938
2939         g_hash_table_remove(listener_table, interface);
2940 }
2941
2942 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
2943 {
2944         const char *interface = key;
2945         struct listener_data *ifdata = value;
2946
2947         DBG("interface %s", interface);
2948
2949         destroy_listener(ifdata);
2950 }
2951
2952 int __connman_dnsproxy_init(void)
2953 {
2954         int err;
2955
2956         DBG("");
2957
2958         srandom(time(NULL));
2959
2960         listener_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2961                                                         g_free, g_free);
2962         err = __connman_dnsproxy_add_listener("lo");
2963         if (err < 0)
2964                 return err;
2965
2966         err = connman_notifier_register(&dnsproxy_notifier);
2967         if (err < 0)
2968                 goto destroy;
2969
2970         return 0;
2971
2972 destroy:
2973         __connman_dnsproxy_remove_listener("lo");
2974         g_hash_table_destroy(listener_table);
2975
2976         return err;
2977 }
2978
2979 void __connman_dnsproxy_cleanup(void)
2980 {
2981         DBG("");
2982
2983         connman_notifier_unregister(&dnsproxy_notifier);
2984
2985         g_hash_table_foreach(listener_table, remove_listener, NULL);
2986
2987         g_hash_table_destroy(listener_table);
2988 }