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