Imported Upstream version 1.35
[platform/upstream/connman.git] / src / dnsproxy.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2014  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 <fcntl.h>
36 #include <netdb.h>
37 #include <resolv.h>
38 #include <gweb/gresolv.h>
39
40 #include <glib.h>
41
42 #include "connman.h"
43
44 #define debug(fmt...) do { } while (0)
45
46 #if __BYTE_ORDER == __LITTLE_ENDIAN
47 struct domain_hdr {
48         uint16_t id;
49         uint8_t rd:1;
50         uint8_t tc:1;
51         uint8_t aa:1;
52         uint8_t opcode:4;
53         uint8_t qr:1;
54         uint8_t rcode:4;
55         uint8_t z:3;
56         uint8_t ra:1;
57         uint16_t qdcount;
58         uint16_t ancount;
59         uint16_t nscount;
60         uint16_t arcount;
61 } __attribute__ ((packed));
62 #elif __BYTE_ORDER == __BIG_ENDIAN
63 struct domain_hdr {
64         uint16_t id;
65         uint8_t qr:1;
66         uint8_t opcode:4;
67         uint8_t aa:1;
68         uint8_t tc:1;
69         uint8_t rd:1;
70         uint8_t ra:1;
71         uint8_t z:3;
72         uint8_t rcode:4;
73         uint16_t qdcount;
74         uint16_t ancount;
75         uint16_t nscount;
76         uint16_t arcount;
77 } __attribute__ ((packed));
78 #else
79 #error "Unknown byte order"
80 #endif
81
82 struct partial_reply {
83         uint16_t len;
84         uint16_t received;
85         unsigned char buf[];
86 };
87
88 struct server_data {
89         int index;
90         GList *domains;
91         char *server;
92         struct sockaddr *server_addr;
93         socklen_t server_addr_len;
94         int protocol;
95         GIOChannel *channel;
96         guint watch;
97         guint timeout;
98         bool enabled;
99         bool connected;
100         struct partial_reply *incoming_reply;
101 };
102
103 struct request_data {
104         union {
105                 struct sockaddr_in6 __sin6; /* Only for the length */
106                 struct sockaddr sa;
107         };
108         socklen_t sa_len;
109         int client_sk;
110         int protocol;
111         int family;
112         guint16 srcid;
113         guint16 dstid;
114         guint16 altid;
115         guint timeout;
116         guint watch;
117         guint numserv;
118         guint numresp;
119         gpointer request;
120         gsize request_len;
121         gpointer name;
122         gpointer resp;
123         gsize resplen;
124         struct listener_data *ifdata;
125         bool append_domain;
126 };
127
128 struct listener_data {
129         int index;
130
131         GIOChannel *udp4_listener_channel;
132         GIOChannel *tcp4_listener_channel;
133         guint udp4_listener_watch;
134         guint tcp4_listener_watch;
135
136         GIOChannel *udp6_listener_channel;
137         GIOChannel *tcp6_listener_channel;
138         guint udp6_listener_watch;
139         guint tcp6_listener_watch;
140 };
141
142 /*
143  * The TCP client requires some extra handling as we need to
144  * be prepared to receive also partial DNS requests.
145  */
146 struct tcp_partial_client_data {
147         int family;
148         struct listener_data *ifdata;
149         GIOChannel *channel;
150         guint watch;
151         unsigned char *buf;
152         unsigned int buf_end;
153         guint timeout;
154 };
155
156 struct cache_data {
157         time_t inserted;
158         time_t valid_until;
159         time_t cache_until;
160         int timeout;
161         uint16_t type;
162         uint16_t answers;
163         unsigned int data_len;
164         unsigned char *data; /* contains DNS header + body */
165 };
166
167 struct cache_entry {
168         char *key;
169         bool want_refresh;
170         int hits;
171         struct cache_data *ipv4;
172         struct cache_data *ipv6;
173 };
174
175 struct domain_question {
176         uint16_t type;
177         uint16_t class;
178 } __attribute__ ((packed));
179
180 struct domain_rr {
181         uint16_t type;
182         uint16_t class;
183         uint32_t ttl;
184         uint16_t rdlen;
185 } __attribute__ ((packed));
186
187 /*
188  * Max length of the DNS TCP packet.
189  */
190 #define TCP_MAX_BUF_LEN 4096
191
192 /*
193  * We limit how long the cached DNS entry stays in the cache.
194  * By default the TTL (time-to-live) of the DNS response is used
195  * when setting the cache entry life time. The value is in seconds.
196  */
197 #define MAX_CACHE_TTL (60 * 30)
198 /*
199  * Also limit the other end, cache at least for 30 seconds.
200  */
201 #define MIN_CACHE_TTL (30)
202
203 /*
204  * We limit the cache size to some sane value so that cached data does
205  * not occupy too much memory. Each cached entry occupies on average
206  * about 100 bytes memory (depending on DNS name length).
207  * Example: caching www.connman.net uses 97 bytes memory.
208  * The value is the max amount of cached DNS responses (count).
209  */
210 #define MAX_CACHE_SIZE 256
211
212 static int cache_size;
213 static GHashTable *cache;
214 static int cache_refcount;
215 static GSList *server_list = NULL;
216 static GSList *request_list = NULL;
217 static GHashTable *listener_table = NULL;
218 static time_t next_refresh;
219 static GHashTable *partial_tcp_req_table;
220 static guint cache_timer = 0;
221
222 static guint16 get_id(void)
223 {
224         uint64_t rand;
225
226         __connman_util_get_random(&rand);
227
228         return rand;
229 }
230
231 static int protocol_offset(int protocol)
232 {
233         switch (protocol) {
234         case IPPROTO_UDP:
235                 return 0;
236
237         case IPPROTO_TCP:
238                 return 2;
239
240         default:
241                 return -EINVAL;
242         }
243
244 }
245
246 /*
247  * There is a power and efficiency benefit to have entries
248  * in our cache expire at the same time. To this extend,
249  * we round down the cache valid time to common boundaries.
250  */
251 static time_t round_down_ttl(time_t end_time, int ttl)
252 {
253         if (ttl < 15)
254                 return end_time;
255
256         /* Less than 5 minutes, round to 10 second boundary */
257         if (ttl < 300) {
258                 end_time = end_time / 10;
259                 end_time = end_time * 10;
260         } else { /* 5 or more minutes, round to 30 seconds */
261                 end_time = end_time / 30;
262                 end_time = end_time * 30;
263         }
264         return end_time;
265 }
266
267 static struct request_data *find_request(guint16 id)
268 {
269         GSList *list;
270
271         for (list = request_list; list; list = list->next) {
272                 struct request_data *req = list->data;
273
274                 if (req->dstid == id || req->altid == id)
275                         return req;
276         }
277
278         return NULL;
279 }
280
281 static struct server_data *find_server(int index,
282                                         const char *server,
283                                                 int protocol)
284 {
285         GSList *list;
286
287         debug("index %d server %s proto %d", index, server, protocol);
288
289         for (list = server_list; list; list = list->next) {
290                 struct server_data *data = list->data;
291
292                 if (index < 0 && data->index < 0 &&
293                                 g_str_equal(data->server, server) &&
294                                 data->protocol == protocol)
295                         return data;
296
297                 if (index < 0 ||
298                                 data->index < 0 || !data->server)
299                         continue;
300
301                 if (data->index == index &&
302                                 g_str_equal(data->server, server) &&
303                                 data->protocol == protocol)
304                         return data;
305         }
306
307         return NULL;
308 }
309
310 /* we can keep using the same resolve's */
311 static GResolv *ipv4_resolve;
312 static GResolv *ipv6_resolve;
313
314 static void dummy_resolve_func(GResolvResultStatus status,
315                                         char **results, gpointer user_data)
316 {
317 }
318
319 /*
320  * Refresh a DNS entry, but also age the hit count a bit */
321 static void refresh_dns_entry(struct cache_entry *entry, char *name)
322 {
323         int age = 1;
324
325         if (!ipv4_resolve) {
326                 ipv4_resolve = g_resolv_new(0);
327                 g_resolv_set_address_family(ipv4_resolve, AF_INET);
328                 g_resolv_add_nameserver(ipv4_resolve, "127.0.0.1", 53, 0);
329         }
330
331         if (!ipv6_resolve) {
332                 ipv6_resolve = g_resolv_new(0);
333                 g_resolv_set_address_family(ipv6_resolve, AF_INET6);
334                 g_resolv_add_nameserver(ipv6_resolve, "::1", 53, 0);
335         }
336
337         if (!entry->ipv4) {
338                 debug("Refreshing A record for %s", name);
339                 g_resolv_lookup_hostname(ipv4_resolve, name,
340                                         dummy_resolve_func, NULL);
341                 age = 4;
342         }
343
344         if (!entry->ipv6) {
345                 debug("Refreshing AAAA record for %s", name);
346                 g_resolv_lookup_hostname(ipv6_resolve, name,
347                                         dummy_resolve_func, NULL);
348                 age = 4;
349         }
350
351         entry->hits -= age;
352         if (entry->hits < 0)
353                 entry->hits = 0;
354 }
355
356 static int dns_name_length(unsigned char *buf)
357 {
358         if ((buf[0] & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* compressed name */
359                 return 2;
360         return strlen((char *)buf) + 1;
361 }
362
363 static void update_cached_ttl(unsigned char *buf, int len, int new_ttl)
364 {
365         unsigned char *c;
366         uint16_t w;
367         int l;
368
369         /* skip the header */
370         c = buf + 12;
371         len -= 12;
372
373         /* skip the query, which is a name and 2 16 bit words */
374         l = dns_name_length(c);
375         c += l;
376         len -= l;
377         c += 4;
378         len -= 4;
379
380         /* now we get the answer records */
381
382         while (len > 0) {
383                 /* first a name */
384                 l = dns_name_length(c);
385                 c += l;
386                 len -= l;
387                 if (len < 0)
388                         break;
389                 /* then type + class, 2 bytes each */
390                 c += 4;
391                 len -= 4;
392                 if (len < 0)
393                         break;
394
395                 /* now the 4 byte TTL field */
396                 c[0] = new_ttl >> 24 & 0xff;
397                 c[1] = new_ttl >> 16 & 0xff;
398                 c[2] = new_ttl >> 8 & 0xff;
399                 c[3] = new_ttl & 0xff;
400                 c += 4;
401                 len -= 4;
402                 if (len < 0)
403                         break;
404
405                 /* now the 2 byte rdlen field */
406                 w = c[0] << 8 | c[1];
407                 c += w + 2;
408                 len -= w + 2;
409         }
410 }
411
412 static void send_cached_response(int sk, unsigned char *buf, int len,
413                                 const struct sockaddr *to, socklen_t tolen,
414                                 int protocol, int id, uint16_t answers, int ttl)
415 {
416         struct domain_hdr *hdr;
417         unsigned char *ptr = buf;
418         int err, offset, dns_len, adj_len = len - 2;
419
420         /*
421          * The cached packet contains always the TCP offset (two bytes)
422          * so skip them for UDP.
423          */
424         switch (protocol) {
425         case IPPROTO_UDP:
426                 ptr += 2;
427                 len -= 2;
428                 dns_len = len;
429                 offset = 0;
430                 break;
431         case IPPROTO_TCP:
432                 offset = 2;
433                 dns_len = ptr[0] * 256 + ptr[1];
434                 break;
435         default:
436                 return;
437         }
438
439         if (len < 12)
440                 return;
441
442         hdr = (void *) (ptr + offset);
443
444         hdr->id = id;
445         hdr->qr = 1;
446         hdr->rcode = ns_r_noerror;
447         hdr->ancount = htons(answers);
448         hdr->nscount = 0;
449         hdr->arcount = 0;
450
451         /* if this is a negative reply, we are authorative */
452         if (answers == 0)
453                 hdr->aa = 1;
454         else
455                 update_cached_ttl((unsigned char *)hdr, adj_len, ttl);
456
457         debug("sk %d id 0x%04x answers %d ptr %p length %d dns %d",
458                 sk, hdr->id, answers, ptr, len, dns_len);
459
460         err = sendto(sk, ptr, len, MSG_NOSIGNAL, to, tolen);
461         if (err < 0) {
462                 connman_error("Cannot send cached DNS response: %s",
463                                 strerror(errno));
464                 return;
465         }
466
467         if (err != len || (dns_len != (len - 2) && protocol == IPPROTO_TCP) ||
468                                 (dns_len != len && protocol == IPPROTO_UDP))
469                 debug("Packet length mismatch, sent %d wanted %d dns %d",
470                         err, len, dns_len);
471 }
472
473 static void send_response(int sk, unsigned char *buf, int len,
474                                 const struct sockaddr *to, socklen_t tolen,
475                                 int protocol)
476 {
477         struct domain_hdr *hdr;
478         int err, offset = protocol_offset(protocol);
479
480         debug("sk %d", sk);
481
482         if (offset < 0)
483                 return;
484
485         if (len < 12)
486                 return;
487
488         hdr = (void *) (buf + offset);
489
490         debug("id 0x%04x qr %d opcode %d", hdr->id, hdr->qr, hdr->opcode);
491
492         hdr->qr = 1;
493         hdr->rcode = ns_r_servfail;
494
495         hdr->ancount = 0;
496         hdr->nscount = 0;
497         hdr->arcount = 0;
498
499         err = sendto(sk, buf, len, MSG_NOSIGNAL, to, tolen);
500         if (err < 0) {
501                 connman_error("Failed to send DNS response to %d: %s",
502                                 sk, strerror(errno));
503                 return;
504         }
505 }
506
507 static int get_req_udp_socket(struct request_data *req)
508 {
509         GIOChannel *channel;
510
511         if (req->family == AF_INET)
512                 channel = req->ifdata->udp4_listener_channel;
513         else
514                 channel = req->ifdata->udp6_listener_channel;
515
516         if (!channel)
517                 return -1;
518
519         return g_io_channel_unix_get_fd(channel);
520 }
521
522 static void destroy_request_data(struct request_data *req)
523 {
524         if (req->timeout > 0)
525                 g_source_remove(req->timeout);
526
527         g_free(req->resp);
528         g_free(req->request);
529         g_free(req->name);
530         g_free(req);
531 }
532
533 static gboolean request_timeout(gpointer user_data)
534 {
535         struct request_data *req = user_data;
536         struct sockaddr *sa;
537         int sk;
538
539         if (!req)
540                 return FALSE;
541
542         debug("id 0x%04x", req->srcid);
543
544         request_list = g_slist_remove(request_list, req);
545
546         if (req->protocol == IPPROTO_UDP) {
547                 sk = get_req_udp_socket(req);
548                 sa = &req->sa;
549         } else if (req->protocol == IPPROTO_TCP) {
550                 sk = req->client_sk;
551                 sa = NULL;
552         } else
553                 goto out;
554
555         if (req->resplen > 0 && req->resp) {
556                 /*
557                  * Here we have received at least one reply (probably telling
558                  * "not found" result), so send that back to client instead
559                  * of more fatal server failed error.
560                  */
561                 if (sk >= 0)
562                         sendto(sk, req->resp, req->resplen, MSG_NOSIGNAL,
563                                 sa, req->sa_len);
564
565         } else if (req->request) {
566                 /*
567                  * There was not reply from server at all.
568                  */
569                 struct domain_hdr *hdr;
570
571                 hdr = (void *)(req->request + protocol_offset(req->protocol));
572                 hdr->id = req->srcid;
573
574                 if (sk >= 0)
575                         send_response(sk, req->request, req->request_len,
576                                 sa, req->sa_len, req->protocol);
577         }
578
579         /*
580          * We cannot leave TCP client hanging so just kick it out
581          * if we get a request timeout from server.
582          */
583         if (req->protocol == IPPROTO_TCP) {
584                 debug("client %d removed", req->client_sk);
585                 g_hash_table_remove(partial_tcp_req_table,
586                                 GINT_TO_POINTER(req->client_sk));
587         }
588
589 out:
590         req->timeout = 0;
591         destroy_request_data(req);
592
593         return FALSE;
594 }
595
596 static int append_query(unsigned char *buf, unsigned int size,
597                                 const char *query, const char *domain)
598 {
599         unsigned char *ptr = buf;
600         int len;
601
602         debug("query %s domain %s", query, domain);
603
604         while (query) {
605                 const char *tmp;
606
607                 tmp = strchr(query, '.');
608                 if (!tmp) {
609                         len = strlen(query);
610                         if (len == 0)
611                                 break;
612                         *ptr = len;
613                         memcpy(ptr + 1, query, len);
614                         ptr += len + 1;
615                         break;
616                 }
617
618                 *ptr = tmp - query;
619                 memcpy(ptr + 1, query, tmp - query);
620                 ptr += tmp - query + 1;
621
622                 query = tmp + 1;
623         }
624
625         while (domain) {
626                 const char *tmp;
627
628                 tmp = strchr(domain, '.');
629                 if (!tmp) {
630                         len = strlen(domain);
631                         if (len == 0)
632                                 break;
633                         *ptr = len;
634                         memcpy(ptr + 1, domain, len);
635                         ptr += len + 1;
636                         break;
637                 }
638
639                 *ptr = tmp - domain;
640                 memcpy(ptr + 1, domain, tmp - domain);
641                 ptr += tmp - domain + 1;
642
643                 domain = tmp + 1;
644         }
645
646         *ptr++ = 0x00;
647
648         return ptr - buf;
649 }
650
651 static bool cache_check_is_valid(struct cache_data *data,
652                                 time_t current_time)
653 {
654         if (!data)
655                 return false;
656
657         if (data->cache_until < current_time)
658                 return false;
659
660         return true;
661 }
662
663 /*
664  * remove stale cached entries so that they can be refreshed
665  */
666 static void cache_enforce_validity(struct cache_entry *entry)
667 {
668         time_t current_time = time(NULL);
669
670         if (!cache_check_is_valid(entry->ipv4, current_time)
671                                                         && entry->ipv4) {
672                 debug("cache timeout \"%s\" type A", entry->key);
673                 g_free(entry->ipv4->data);
674                 g_free(entry->ipv4);
675                 entry->ipv4 = NULL;
676
677         }
678
679         if (!cache_check_is_valid(entry->ipv6, current_time)
680                                                         && entry->ipv6) {
681                 debug("cache timeout \"%s\" type AAAA", entry->key);
682                 g_free(entry->ipv6->data);
683                 g_free(entry->ipv6);
684                 entry->ipv6 = NULL;
685         }
686 }
687
688 static uint16_t cache_check_validity(char *question, uint16_t type,
689                                 struct cache_entry *entry)
690 {
691         time_t current_time = time(NULL);
692         bool want_refresh = false;
693
694         /*
695          * if we have a popular entry, we want a refresh instead of
696          * total destruction of the entry.
697          */
698         if (entry->hits > 2)
699                 want_refresh = true;
700
701         cache_enforce_validity(entry);
702
703         switch (type) {
704         case 1:         /* IPv4 */
705                 if (!cache_check_is_valid(entry->ipv4, current_time)) {
706                         debug("cache %s \"%s\" type A", entry->ipv4 ?
707                                         "timeout" : "entry missing", question);
708
709                         if (want_refresh)
710                                 entry->want_refresh = true;
711
712                         /*
713                          * We do not remove cache entry if there is still
714                          * valid IPv6 entry found in the cache.
715                          */
716                         if (!cache_check_is_valid(entry->ipv6, current_time) && !want_refresh) {
717                                 g_hash_table_remove(cache, question);
718                                 type = 0;
719                         }
720                 }
721                 break;
722
723         case 28:        /* IPv6 */
724                 if (!cache_check_is_valid(entry->ipv6, current_time)) {
725                         debug("cache %s \"%s\" type AAAA", entry->ipv6 ?
726                                         "timeout" : "entry missing", question);
727
728                         if (want_refresh)
729                                 entry->want_refresh = true;
730
731                         if (!cache_check_is_valid(entry->ipv4, current_time) && !want_refresh) {
732                                 g_hash_table_remove(cache, question);
733                                 type = 0;
734                         }
735                 }
736                 break;
737         }
738
739         return type;
740 }
741
742 static void cache_element_destroy(gpointer value)
743 {
744         struct cache_entry *entry = value;
745
746         if (!entry)
747                 return;
748
749         if (entry->ipv4) {
750                 g_free(entry->ipv4->data);
751                 g_free(entry->ipv4);
752         }
753
754         if (entry->ipv6) {
755                 g_free(entry->ipv6->data);
756                 g_free(entry->ipv6);
757         }
758
759         g_free(entry->key);
760         g_free(entry);
761
762         if (--cache_size < 0)
763                 cache_size = 0;
764 }
765
766 static gboolean try_remove_cache(gpointer user_data)
767 {
768         cache_timer = 0;
769
770         if (__sync_fetch_and_sub(&cache_refcount, 1) == 1) {
771                 debug("No cache users, removing it.");
772
773                 g_hash_table_destroy(cache);
774                 cache = NULL;
775         }
776
777         return FALSE;
778 }
779
780 static void create_cache(void)
781 {
782         if (__sync_fetch_and_add(&cache_refcount, 1) == 0)
783                 cache = g_hash_table_new_full(g_str_hash,
784                                         g_str_equal,
785                                         NULL,
786                                         cache_element_destroy);
787 }
788
789 static struct cache_entry *cache_check(gpointer request, int *qtype, int proto)
790 {
791         char *question;
792         struct cache_entry *entry;
793         struct domain_question *q;
794         uint16_t type;
795         int offset, proto_offset;
796
797         if (!request)
798                 return NULL;
799
800         proto_offset = protocol_offset(proto);
801         if (proto_offset < 0)
802                 return NULL;
803
804         question = request + proto_offset + 12;
805
806         offset = strlen(question) + 1;
807         q = (void *) (question + offset);
808         type = ntohs(q->type);
809
810         /* We only cache either A (1) or AAAA (28) requests */
811         if (type != 1 && type != 28)
812                 return NULL;
813
814         if (!cache) {
815                 create_cache();
816                 return NULL;
817         }
818
819         entry = g_hash_table_lookup(cache, question);
820         if (!entry)
821                 return NULL;
822
823         type = cache_check_validity(question, type, entry);
824         if (type == 0)
825                 return NULL;
826
827         *qtype = type;
828         return entry;
829 }
830
831 /*
832  * Get a label/name from DNS resource record. The function decompresses the
833  * label if necessary. The function does not convert the name to presentation
834  * form. This means that the result string will contain label lengths instead
835  * of dots between labels. We intentionally do not want to convert to dotted
836  * format so that we can cache the wire format string directly.
837  */
838 static int get_name(int counter,
839                 unsigned char *pkt, unsigned char *start, unsigned char *max,
840                 unsigned char *output, int output_max, int *output_len,
841                 unsigned char **end, char *name, size_t max_name, int *name_len)
842 {
843         unsigned char *p;
844
845         /* Limit recursion to 10 (this means up to 10 labels in domain name) */
846         if (counter > 10)
847                 return -EINVAL;
848
849         p = start;
850         while (*p) {
851                 if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
852                         uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
853
854                         if (offset >= max - pkt)
855                                 return -ENOBUFS;
856
857                         if (!*end)
858                                 *end = p + 2;
859
860                         return get_name(counter + 1, pkt, pkt + offset, max,
861                                         output, output_max, output_len, end,
862                                         name, max_name, name_len);
863                 } else {
864                         unsigned label_len = *p;
865
866                         if (pkt + label_len > max)
867                                 return -ENOBUFS;
868
869                         if (*output_len > output_max)
870                                 return -ENOBUFS;
871
872                         if ((*name_len + 1 + label_len + 1) > max_name)
873                                 return -ENOBUFS;
874
875                         /*
876                          * We need the original name in order to check
877                          * if this answer is the correct one.
878                          */
879                         name[(*name_len)++] = label_len;
880                         memcpy(name + *name_len, p + 1, label_len + 1);
881                         *name_len += label_len;
882
883                         /* We compress the result */
884                         output[0] = NS_CMPRSFLGS;
885                         output[1] = 0x0C;
886                         *output_len = 2;
887
888                         p += label_len + 1;
889
890                         if (!*end)
891                                 *end = p;
892
893                         if (p >= max)
894                                 return -ENOBUFS;
895                 }
896         }
897
898         return 0;
899 }
900
901 static int parse_rr(unsigned char *buf, unsigned char *start,
902                         unsigned char *max,
903                         unsigned char *response, unsigned int *response_size,
904                         uint16_t *type, uint16_t *class, int *ttl, int *rdlen,
905                         unsigned char **end,
906                         char *name, size_t max_name)
907 {
908         struct domain_rr *rr;
909         int err, offset;
910         int name_len = 0, output_len = 0, max_rsp = *response_size;
911
912         err = get_name(0, buf, start, max, response, max_rsp,
913                         &output_len, end, name, max_name, &name_len);
914         if (err < 0)
915                 return err;
916
917         offset = output_len;
918
919         if ((unsigned int) offset > *response_size)
920                 return -ENOBUFS;
921
922         rr = (void *) (*end);
923
924         if (!rr)
925                 return -EINVAL;
926
927         *type = ntohs(rr->type);
928         *class = ntohs(rr->class);
929         *ttl = ntohl(rr->ttl);
930         *rdlen = ntohs(rr->rdlen);
931
932         if (*ttl < 0)
933                 return -EINVAL;
934
935         memcpy(response + offset, *end, sizeof(struct domain_rr));
936
937         offset += sizeof(struct domain_rr);
938         *end += sizeof(struct domain_rr);
939
940         if ((unsigned int) (offset + *rdlen) > *response_size)
941                 return -ENOBUFS;
942
943         memcpy(response + offset, *end, *rdlen);
944
945         *end += *rdlen;
946
947         *response_size = offset + *rdlen;
948
949         return 0;
950 }
951
952 static bool check_alias(GSList *aliases, char *name)
953 {
954         GSList *list;
955
956         if (aliases) {
957                 for (list = aliases; list; list = list->next) {
958                         int len = strlen((char *)list->data);
959                         if (strncmp((char *)list->data, name, len) == 0)
960                                 return true;
961                 }
962         }
963
964         return false;
965 }
966
967 static int parse_response(unsigned char *buf, int buflen,
968                         char *question, int qlen,
969                         uint16_t *type, uint16_t *class, int *ttl,
970                         unsigned char *response, unsigned int *response_len,
971                         uint16_t *answers)
972 {
973         struct domain_hdr *hdr = (void *) buf;
974         struct domain_question *q;
975         unsigned char *ptr;
976         uint16_t qdcount = ntohs(hdr->qdcount);
977         uint16_t ancount = ntohs(hdr->ancount);
978         int err, i;
979         uint16_t qtype, qclass;
980         unsigned char *next = NULL;
981         unsigned int maxlen = *response_len;
982         GSList *aliases = NULL, *list;
983         char name[NS_MAXDNAME + 1];
984
985         if (buflen < 12)
986                 return -EINVAL;
987
988         debug("qr %d qdcount %d", hdr->qr, qdcount);
989
990         /* We currently only cache responses where question count is 1 */
991         if (hdr->qr != 1 || qdcount != 1)
992                 return -EINVAL;
993
994         ptr = buf + sizeof(struct domain_hdr);
995
996         strncpy(question, (char *) ptr, qlen);
997         qlen = strlen(question);
998         ptr += qlen + 1; /* skip \0 */
999
1000         q = (void *) ptr;
1001         qtype = ntohs(q->type);
1002
1003         /* We cache only A and AAAA records */
1004         if (qtype != 1 && qtype != 28)
1005                 return -ENOMSG;
1006
1007         qclass = ntohs(q->class);
1008
1009         ptr += 2 + 2; /* ptr points now to answers */
1010
1011         err = -ENOMSG;
1012         *response_len = 0;
1013         *answers = 0;
1014
1015         memset(name, 0, sizeof(name));
1016
1017         /*
1018          * We have a bunch of answers (like A, AAAA, CNAME etc) to
1019          * A or AAAA question. We traverse the answers and parse the
1020          * resource records. Only A and AAAA records are cached, all
1021          * the other records in answers are skipped.
1022          */
1023         for (i = 0; i < ancount; i++) {
1024                 /*
1025                  * Get one address at a time to this buffer.
1026                  * The max size of the answer is
1027                  *   2 (pointer) + 2 (type) + 2 (class) +
1028                  *   4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
1029                  * for A or AAAA record.
1030                  * For CNAME the size can be bigger.
1031                  */
1032                 unsigned char rsp[NS_MAXCDNAME];
1033                 unsigned int rsp_len = sizeof(rsp) - 1;
1034                 int ret, rdlen;
1035
1036                 memset(rsp, 0, sizeof(rsp));
1037
1038                 ret = parse_rr(buf, ptr, buf + buflen, rsp, &rsp_len,
1039                         type, class, ttl, &rdlen, &next, name,
1040                         sizeof(name) - 1);
1041                 if (ret != 0) {
1042                         err = ret;
1043                         goto out;
1044                 }
1045
1046                 /*
1047                  * Now rsp contains compressed or uncompressed resource
1048                  * record. Next we check if this record answers the question.
1049                  * The name var contains the uncompressed label.
1050                  * One tricky bit is the CNAME records as they alias
1051                  * the name we might be interested in.
1052                  */
1053
1054                 /*
1055                  * Go to next answer if the class is not the one we are
1056                  * looking for.
1057                  */
1058                 if (*class != qclass) {
1059                         ptr = next;
1060                         next = NULL;
1061                         continue;
1062                 }
1063
1064                 /*
1065                  * Try to resolve aliases also, type is CNAME(5).
1066                  * This is important as otherwise the aliased names would not
1067                  * be cached at all as the cache would not contain the aliased
1068                  * question.
1069                  *
1070                  * If any CNAME is found in DNS packet, then we cache the alias
1071                  * IP address instead of the question (as the server
1072                  * said that question has only an alias).
1073                  * This means in practice that if e.g., ipv6.google.com is
1074                  * queried, DNS server returns CNAME of that name which is
1075                  * ipv6.l.google.com. We then cache the address of the CNAME
1076                  * but return the question name to client. So the alias
1077                  * status of the name is not saved in cache and thus not
1078                  * returned to the client. We do not return DNS packets from
1079                  * cache to client saying that ipv6.google.com is an alias to
1080                  * ipv6.l.google.com but we return instead a DNS packet that
1081                  * says ipv6.google.com has address xxx which is in fact the
1082                  * address of ipv6.l.google.com. For caching purposes this
1083                  * should not cause any issues.
1084                  */
1085                 if (*type == 5 && strncmp(question, name, qlen) == 0) {
1086                         /*
1087                          * So now the alias answered the question. This is
1088                          * not very useful from caching point of view as
1089                          * the following A or AAAA records will not match the
1090                          * question. We need to find the real A/AAAA record
1091                          * of the alias and cache that.
1092                          */
1093                         unsigned char *end = NULL;
1094                         int name_len = 0, output_len = 0;
1095
1096                         memset(rsp, 0, sizeof(rsp));
1097                         rsp_len = sizeof(rsp) - 1;
1098
1099                         /*
1100                          * Alias is in rdata part of the message,
1101                          * and next-rdlen points to it. So we need to get
1102                          * the real name of the alias.
1103                          */
1104                         ret = get_name(0, buf, next - rdlen, buf + buflen,
1105                                         rsp, rsp_len, &output_len, &end,
1106                                         name, sizeof(name) - 1, &name_len);
1107                         if (ret != 0) {
1108                                 /* just ignore the error at this point */
1109                                 ptr = next;
1110                                 next = NULL;
1111                                 continue;
1112                         }
1113
1114                         /*
1115                          * We should now have the alias of the entry we might
1116                          * want to cache. Just remember it for a while.
1117                          * We check the alias list when we have parsed the
1118                          * A or AAAA record.
1119                          */
1120                         aliases = g_slist_prepend(aliases, g_strdup(name));
1121
1122                         ptr = next;
1123                         next = NULL;
1124                         continue;
1125                 }
1126
1127                 if (*type == qtype) {
1128                         /*
1129                          * We found correct type (A or AAAA)
1130                          */
1131                         if (check_alias(aliases, name) ||
1132                                 (!aliases && strncmp(question, name,
1133                                                         qlen) == 0)) {
1134                                 /*
1135                                  * We found an alias or the name of the rr
1136                                  * matches the question. If so, we append
1137                                  * the compressed label to the cache.
1138                                  * The end result is a response buffer that
1139                                  * will contain one or more cached and
1140                                  * compressed resource records.
1141                                  */
1142                                 if (*response_len + rsp_len > maxlen) {
1143                                         err = -ENOBUFS;
1144                                         goto out;
1145                                 }
1146                                 memcpy(response + *response_len, rsp, rsp_len);
1147                                 *response_len += rsp_len;
1148                                 (*answers)++;
1149                                 err = 0;
1150                         }
1151                 }
1152
1153                 ptr = next;
1154                 next = NULL;
1155         }
1156
1157 out:
1158         for (list = aliases; list; list = list->next)
1159                 g_free(list->data);
1160         g_slist_free(aliases);
1161
1162         return err;
1163 }
1164
1165 struct cache_timeout {
1166         time_t current_time;
1167         int max_timeout;
1168         int try_harder;
1169 };
1170
1171 static gboolean cache_check_entry(gpointer key, gpointer value,
1172                                         gpointer user_data)
1173 {
1174         struct cache_timeout *data = user_data;
1175         struct cache_entry *entry = value;
1176         int max_timeout;
1177
1178         /* Scale the number of hits by half as part of cache aging */
1179
1180         entry->hits /= 2;
1181
1182         /*
1183          * If either IPv4 or IPv6 cached entry has expired, we
1184          * remove both from the cache.
1185          */
1186
1187         if (entry->ipv4 && entry->ipv4->timeout > 0) {
1188                 max_timeout = entry->ipv4->cache_until;
1189                 if (max_timeout > data->max_timeout)
1190                         data->max_timeout = max_timeout;
1191
1192                 if (entry->ipv4->cache_until < data->current_time)
1193                         return TRUE;
1194         }
1195
1196         if (entry->ipv6 && entry->ipv6->timeout > 0) {
1197                 max_timeout = entry->ipv6->cache_until;
1198                 if (max_timeout > data->max_timeout)
1199                         data->max_timeout = max_timeout;
1200
1201                 if (entry->ipv6->cache_until < data->current_time)
1202                         return TRUE;
1203         }
1204
1205         /*
1206          * if we're asked to try harder, also remove entries that have
1207          * few hits
1208          */
1209         if (data->try_harder && entry->hits < 4)
1210                 return TRUE;
1211
1212         return FALSE;
1213 }
1214
1215 static void cache_cleanup(void)
1216 {
1217         static int max_timeout;
1218         struct cache_timeout data;
1219         int count = 0;
1220
1221         data.current_time = time(NULL);
1222         data.max_timeout = 0;
1223         data.try_harder = 0;
1224
1225         /*
1226          * In the first pass, we only remove entries that have timed out.
1227          * We use a cache of the first time to expire to do this only
1228          * when it makes sense.
1229          */
1230         if (max_timeout <= data.current_time) {
1231                 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1232                                                 &data);
1233         }
1234         debug("removed %d in the first pass", count);
1235
1236         /*
1237          * In the second pass, if the first pass turned up blank,
1238          * we also expire entries with a low hit count,
1239          * while aging the hit count at the same time.
1240          */
1241         data.try_harder = 1;
1242         if (count == 0)
1243                 count = g_hash_table_foreach_remove(cache, cache_check_entry,
1244                                                 &data);
1245
1246         if (count == 0)
1247                 /*
1248                  * If we could not remove anything, then remember
1249                  * what is the max timeout and do nothing if we
1250                  * have not yet reached it. This will prevent
1251                  * constant traversal of the cache if it is full.
1252                  */
1253                 max_timeout = data.max_timeout;
1254         else
1255                 max_timeout = 0;
1256 }
1257
1258 static gboolean cache_invalidate_entry(gpointer key, gpointer value,
1259                                         gpointer user_data)
1260 {
1261         struct cache_entry *entry = value;
1262
1263         /* first, delete any expired elements */
1264         cache_enforce_validity(entry);
1265
1266         /* if anything is not expired, mark the entry for refresh */
1267         if (entry->hits > 0 && (entry->ipv4 || entry->ipv6))
1268                 entry->want_refresh = true;
1269
1270         /* delete the cached data */
1271         if (entry->ipv4) {
1272                 g_free(entry->ipv4->data);
1273                 g_free(entry->ipv4);
1274                 entry->ipv4 = NULL;
1275         }
1276
1277         if (entry->ipv6) {
1278                 g_free(entry->ipv6->data);
1279                 g_free(entry->ipv6);
1280                 entry->ipv6 = NULL;
1281         }
1282
1283         /* keep the entry if we want it refreshed, delete it otherwise */
1284         if (entry->want_refresh)
1285                 return FALSE;
1286         else
1287                 return TRUE;
1288 }
1289
1290 /*
1291  * cache_invalidate is called from places where the DNS landscape
1292  * has changed, say because connections are added or we entered a VPN.
1293  * The logic is to wipe all cache data, but mark all non-expired
1294  * parts of the cache for refresh rather than deleting the whole cache.
1295  */
1296 static void cache_invalidate(void)
1297 {
1298         debug("Invalidating the DNS cache %p", cache);
1299
1300         if (!cache)
1301                 return;
1302
1303         g_hash_table_foreach_remove(cache, cache_invalidate_entry, NULL);
1304 }
1305
1306 static void cache_refresh_entry(struct cache_entry *entry)
1307 {
1308
1309         cache_enforce_validity(entry);
1310
1311         if (entry->hits > 2 && !entry->ipv4)
1312                 entry->want_refresh = true;
1313         if (entry->hits > 2 && !entry->ipv6)
1314                 entry->want_refresh = true;
1315
1316         if (entry->want_refresh) {
1317                 char *c;
1318                 char dns_name[NS_MAXDNAME + 1];
1319                 entry->want_refresh = false;
1320
1321                 /* turn a DNS name into a hostname with dots */
1322                 strncpy(dns_name, entry->key, NS_MAXDNAME);
1323                 c = dns_name;
1324                 while (c && *c) {
1325                         int jump;
1326                         jump = *c;
1327                         *c = '.';
1328                         c += jump + 1;
1329                 }
1330                 debug("Refreshing %s\n", dns_name);
1331                 /* then refresh the hostname */
1332                 refresh_dns_entry(entry, &dns_name[1]);
1333         }
1334 }
1335
1336 static void cache_refresh_iterator(gpointer key, gpointer value,
1337                                         gpointer user_data)
1338 {
1339         struct cache_entry *entry = value;
1340
1341         cache_refresh_entry(entry);
1342 }
1343
1344 static void cache_refresh(void)
1345 {
1346         if (!cache)
1347                 return;
1348
1349         g_hash_table_foreach(cache, cache_refresh_iterator, NULL);
1350 }
1351
1352 static int reply_query_type(unsigned char *msg, int len)
1353 {
1354         unsigned char *c;
1355         int l;
1356         int type;
1357
1358         /* skip the header */
1359         c = msg + sizeof(struct domain_hdr);
1360         len -= sizeof(struct domain_hdr);
1361
1362         if (len < 0)
1363                 return 0;
1364
1365         /* now the query, which is a name and 2 16 bit words */
1366         l = dns_name_length(c);
1367         c += l;
1368         type = c[0] << 8 | c[1];
1369
1370         return type;
1371 }
1372
1373 static int cache_update(struct server_data *srv, unsigned char *msg,
1374                         unsigned int msg_len)
1375 {
1376         int offset = protocol_offset(srv->protocol);
1377         int err, qlen, ttl = 0;
1378         uint16_t answers = 0, type = 0, class = 0;
1379         struct domain_hdr *hdr = (void *)(msg + offset);
1380         struct domain_question *q;
1381         struct cache_entry *entry;
1382         struct cache_data *data;
1383         char question[NS_MAXDNAME + 1];
1384         unsigned char response[NS_MAXDNAME + 1];
1385         unsigned char *ptr;
1386         unsigned int rsplen;
1387         bool new_entry = true;
1388         time_t current_time;
1389
1390         if (cache_size >= MAX_CACHE_SIZE) {
1391                 cache_cleanup();
1392                 if (cache_size >= MAX_CACHE_SIZE)
1393                         return 0;
1394         }
1395
1396         current_time = time(NULL);
1397
1398         /* don't do a cache refresh more than twice a minute */
1399         if (next_refresh < current_time) {
1400                 cache_refresh();
1401                 next_refresh = current_time + 30;
1402         }
1403
1404         if (offset < 0)
1405                 return 0;
1406
1407         debug("offset %d hdr %p msg %p rcode %d", offset, hdr, msg, hdr->rcode);
1408
1409         /* Continue only if response code is 0 (=ok) */
1410         if (hdr->rcode != ns_r_noerror)
1411                 return 0;
1412
1413         if (!cache)
1414                 create_cache();
1415
1416         rsplen = sizeof(response) - 1;
1417         question[sizeof(question) - 1] = '\0';
1418
1419         err = parse_response(msg + offset, msg_len - offset,
1420                                 question, sizeof(question) - 1,
1421                                 &type, &class, &ttl,
1422                                 response, &rsplen, &answers);
1423
1424         /*
1425          * special case: if we do a ipv6 lookup and get no result
1426          * for a record that's already in our ipv4 cache.. we want
1427          * to cache the negative response.
1428          */
1429         if ((err == -ENOMSG || err == -ENOBUFS) &&
1430                         reply_query_type(msg + offset,
1431                                         msg_len - offset) == 28) {
1432                 entry = g_hash_table_lookup(cache, question);
1433                 if (entry && entry->ipv4 && !entry->ipv6) {
1434                         int cache_offset = 0;
1435
1436                         data = g_try_new(struct cache_data, 1);
1437                         if (!data)
1438                                 return -ENOMEM;
1439                         data->inserted = entry->ipv4->inserted;
1440                         data->type = type;
1441                         data->answers = ntohs(hdr->ancount);
1442                         data->timeout = entry->ipv4->timeout;
1443                         if (srv->protocol == IPPROTO_UDP)
1444                                 cache_offset = 2;
1445                         data->data_len = msg_len + cache_offset;
1446                         data->data = ptr = g_malloc(data->data_len);
1447                         ptr[0] = (data->data_len - 2) / 256;
1448                         ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1449                         if (srv->protocol == IPPROTO_UDP)
1450                                 ptr += 2;
1451                         data->valid_until = entry->ipv4->valid_until;
1452                         data->cache_until = entry->ipv4->cache_until;
1453                         memcpy(ptr, msg, msg_len);
1454                         entry->ipv6 = data;
1455                         /*
1456                          * we will get a "hit" when we serve the response
1457                          * out of the cache
1458                          */
1459                         entry->hits--;
1460                         if (entry->hits < 0)
1461                                 entry->hits = 0;
1462                         return 0;
1463                 }
1464         }
1465
1466         if (err < 0 || ttl == 0)
1467                 return 0;
1468
1469         qlen = strlen(question);
1470
1471         /*
1472          * If the cache contains already data, check if the
1473          * type of the cached data is the same and do not add
1474          * to cache if data is already there.
1475          * This is needed so that we can cache both A and AAAA
1476          * records for the same name.
1477          */
1478         entry = g_hash_table_lookup(cache, question);
1479         if (!entry) {
1480                 entry = g_try_new(struct cache_entry, 1);
1481                 if (!entry)
1482                         return -ENOMEM;
1483
1484                 data = g_try_new(struct cache_data, 1);
1485                 if (!data) {
1486                         g_free(entry);
1487                         return -ENOMEM;
1488                 }
1489
1490                 entry->key = g_strdup(question);
1491                 entry->ipv4 = entry->ipv6 = NULL;
1492                 entry->want_refresh = false;
1493                 entry->hits = 0;
1494
1495                 if (type == 1)
1496                         entry->ipv4 = data;
1497                 else
1498                         entry->ipv6 = data;
1499         } else {
1500                 if (type == 1 && entry->ipv4)
1501                         return 0;
1502
1503                 if (type == 28 && entry->ipv6)
1504                         return 0;
1505
1506                 data = g_try_new(struct cache_data, 1);
1507                 if (!data)
1508                         return -ENOMEM;
1509
1510                 if (type == 1)
1511                         entry->ipv4 = data;
1512                 else
1513                         entry->ipv6 = data;
1514
1515                 /*
1516                  * compensate for the hit we'll get for serving
1517                  * the response out of the cache
1518                  */
1519                 entry->hits--;
1520                 if (entry->hits < 0)
1521                         entry->hits = 0;
1522
1523                 new_entry = false;
1524         }
1525
1526         if (ttl < MIN_CACHE_TTL)
1527                 ttl = MIN_CACHE_TTL;
1528
1529         data->inserted = current_time;
1530         data->type = type;
1531         data->answers = answers;
1532         data->timeout = ttl;
1533         /*
1534          * The "2" in start of the length is the TCP offset. We allocate it
1535          * here even for UDP packet because it simplifies the sending
1536          * of cached packet.
1537          */
1538         data->data_len = 2 + 12 + qlen + 1 + 2 + 2 + rsplen;
1539         data->data = ptr = g_malloc(data->data_len);
1540         data->valid_until = current_time + ttl;
1541
1542         /*
1543          * Restrict the cached DNS record TTL to some sane value
1544          * in order to prevent data staying in the cache too long.
1545          */
1546         if (ttl > MAX_CACHE_TTL)
1547                 ttl = MAX_CACHE_TTL;
1548
1549         data->cache_until = round_down_ttl(current_time + ttl, ttl);
1550
1551         if (!data->data) {
1552                 g_free(entry->key);
1553                 g_free(data);
1554                 g_free(entry);
1555                 return -ENOMEM;
1556         }
1557
1558         /*
1559          * We cache the two extra bytes at the start of the message
1560          * in a TCP packet. When sending UDP packet, we skip the first
1561          * two bytes. This way we do not need to know the format
1562          * (UDP/TCP) of the cached message.
1563          */
1564         if (srv->protocol == IPPROTO_UDP)
1565                 memcpy(ptr + 2, msg, offset + 12);
1566         else
1567                 memcpy(ptr, msg, offset + 12);
1568
1569         ptr[0] = (data->data_len - 2) / 256;
1570         ptr[1] = (data->data_len - 2) - ptr[0] * 256;
1571         if (srv->protocol == IPPROTO_UDP)
1572                 ptr += 2;
1573
1574         memcpy(ptr + offset + 12, question, qlen + 1); /* copy also the \0 */
1575
1576         q = (void *) (ptr + offset + 12 + qlen + 1);
1577         q->type = htons(type);
1578         q->class = htons(class);
1579         memcpy(ptr + offset + 12 + qlen + 1 + sizeof(struct domain_question),
1580                 response, rsplen);
1581
1582         if (new_entry) {
1583                 g_hash_table_replace(cache, entry->key, entry);
1584                 cache_size++;
1585         }
1586
1587         debug("cache %d %squestion \"%s\" type %d ttl %d size %zd packet %u "
1588                                                                 "dns len %u",
1589                 cache_size, new_entry ? "new " : "old ",
1590                 question, type, ttl,
1591                 sizeof(*entry) + sizeof(*data) + data->data_len + qlen,
1592                 data->data_len,
1593                 srv->protocol == IPPROTO_TCP ?
1594                         (unsigned int)(data->data[0] * 256 + data->data[1]) :
1595                         data->data_len);
1596
1597         return 0;
1598 }
1599
1600 static int ns_resolv(struct server_data *server, struct request_data *req,
1601                                 gpointer request, gpointer name)
1602 {
1603         GList *list;
1604         int sk, err, type = 0;
1605         char *dot, *lookup = (char *) name;
1606         struct cache_entry *entry;
1607
1608         entry = cache_check(request, &type, req->protocol);
1609         if (entry) {
1610                 int ttl_left = 0;
1611                 struct cache_data *data;
1612
1613                 debug("cache hit %s type %s", lookup, type == 1 ? "A" : "AAAA");
1614                 if (type == 1)
1615                         data = entry->ipv4;
1616                 else
1617                         data = entry->ipv6;
1618
1619                 if (data) {
1620                         ttl_left = data->valid_until - time(NULL);
1621                         entry->hits++;
1622                 }
1623
1624                 if (data && req->protocol == IPPROTO_TCP) {
1625                         send_cached_response(req->client_sk, data->data,
1626                                         data->data_len, NULL, 0, IPPROTO_TCP,
1627                                         req->srcid, data->answers, ttl_left);
1628                         return 1;
1629                 }
1630
1631                 if (data && req->protocol == IPPROTO_UDP) {
1632                         int udp_sk = get_req_udp_socket(req);
1633
1634                         if (udp_sk < 0)
1635                                 return -EIO;
1636
1637                         send_cached_response(udp_sk, data->data,
1638                                 data->data_len, &req->sa, req->sa_len,
1639                                 IPPROTO_UDP, req->srcid, data->answers,
1640                                 ttl_left);
1641                         return 1;
1642                 }
1643         }
1644
1645         sk = g_io_channel_unix_get_fd(server->channel);
1646
1647         err = sendto(sk, request, req->request_len, MSG_NOSIGNAL,
1648                         server->server_addr, server->server_addr_len);
1649         if (err < 0) {
1650                 debug("Cannot send message to server %s sock %d "
1651                         "protocol %d (%s/%d)",
1652                         server->server, sk, server->protocol,
1653                         strerror(errno), errno);
1654                 return -EIO;
1655         }
1656
1657         req->numserv++;
1658
1659         /* If we have more than one dot, we don't add domains */
1660         dot = strchr(lookup, '.');
1661         if (dot && dot != lookup + strlen(lookup) - 1)
1662                 return 0;
1663
1664         if (server->domains && server->domains->data)
1665                 req->append_domain = true;
1666
1667         for (list = server->domains; list; list = list->next) {
1668                 char *domain;
1669                 unsigned char alt[1024];
1670                 struct domain_hdr *hdr = (void *) &alt;
1671                 int altlen, domlen, offset;
1672
1673                 domain = list->data;
1674
1675                 if (!domain)
1676                         continue;
1677
1678                 offset = protocol_offset(server->protocol);
1679                 if (offset < 0)
1680                         return offset;
1681
1682                 domlen = strlen(domain) + 1;
1683                 if (domlen < 5)
1684                         return -EINVAL;
1685
1686                 alt[offset] = req->altid & 0xff;
1687                 alt[offset + 1] = req->altid >> 8;
1688
1689                 memcpy(alt + offset + 2, request + offset + 2, 10);
1690                 hdr->qdcount = htons(1);
1691
1692                 altlen = append_query(alt + offset + 12, sizeof(alt) - 12,
1693                                         name, domain);
1694                 if (altlen < 0)
1695                         return -EINVAL;
1696
1697                 altlen += 12;
1698
1699                 memcpy(alt + offset + altlen,
1700                         request + offset + altlen - domlen,
1701                                 req->request_len - altlen - offset + domlen);
1702
1703                 if (server->protocol == IPPROTO_TCP) {
1704                         int req_len = req->request_len + domlen - 2;
1705
1706                         alt[0] = (req_len >> 8) & 0xff;
1707                         alt[1] = req_len & 0xff;
1708                 }
1709
1710                 debug("req %p dstid 0x%04x altid 0x%04x", req, req->dstid,
1711                                 req->altid);
1712
1713                 err = send(sk, alt, req->request_len + domlen, MSG_NOSIGNAL);
1714                 if (err < 0)
1715                         return -EIO;
1716
1717                 req->numserv++;
1718         }
1719
1720         return 0;
1721 }
1722
1723 static char *convert_label(char *start, char *end, char *ptr, char *uptr,
1724                         int remaining_len, int *used_comp, int *used_uncomp)
1725 {
1726         int pos, comp_pos;
1727         char name[NS_MAXLABEL];
1728
1729         pos = dn_expand((u_char *)start, (u_char *)end, (u_char *)ptr,
1730                         name, NS_MAXLABEL);
1731         if (pos < 0) {
1732                 debug("uncompress error [%d/%s]", errno, strerror(errno));
1733                 goto out;
1734         }
1735
1736         /*
1737          * We need to compress back the name so that we get back to internal
1738          * label presentation.
1739          */
1740         comp_pos = dn_comp(name, (u_char *)uptr, remaining_len, NULL, NULL);
1741         if (comp_pos < 0) {
1742                 debug("compress error [%d/%s]", errno, strerror(errno));
1743                 goto out;
1744         }
1745
1746         *used_comp = pos;
1747         *used_uncomp = comp_pos;
1748
1749         return ptr;
1750
1751 out:
1752         return NULL;
1753 }
1754
1755 static char *uncompress(int16_t field_count, char *start, char *end,
1756                         char *ptr, char *uncompressed, int uncomp_len,
1757                         char **uncompressed_ptr)
1758 {
1759         char *uptr = *uncompressed_ptr; /* position in result buffer */
1760
1761         debug("count %d ptr %p end %p uptr %p", field_count, ptr, end, uptr);
1762
1763         while (field_count-- > 0 && ptr < end) {
1764                 int dlen;               /* data field length */
1765                 int ulen;               /* uncompress length */
1766                 int pos;                /* position in compressed string */
1767                 char name[NS_MAXLABEL]; /* tmp label */
1768                 uint16_t dns_type, dns_class;
1769                 int comp_pos;
1770
1771                 if (!convert_label(start, end, ptr, name, NS_MAXLABEL,
1772                                         &pos, &comp_pos))
1773                         goto out;
1774
1775                 /*
1776                  * Copy the uncompressed resource record, type, class and \0 to
1777                  * tmp buffer.
1778                  */
1779
1780                 ulen = strlen(name);
1781                 strncpy(uptr, name, uncomp_len - (uptr - uncompressed));
1782
1783                 debug("pos %d ulen %d left %d name %s", pos, ulen,
1784                         (int)(uncomp_len - (uptr - uncompressed)), uptr);
1785
1786                 uptr += ulen;
1787                 *uptr++ = '\0';
1788
1789                 ptr += pos;
1790
1791                 /*
1792                  * We copy also the fixed portion of the result (type, class,
1793                  * ttl, address length and the address)
1794                  */
1795                 memcpy(uptr, ptr, NS_RRFIXEDSZ);
1796
1797                 dns_type = uptr[0] << 8 | uptr[1];
1798                 dns_class = uptr[2] << 8 | uptr[3];
1799
1800                 if (dns_class != ns_c_in)
1801                         goto out;
1802
1803                 ptr += NS_RRFIXEDSZ;
1804                 uptr += NS_RRFIXEDSZ;
1805
1806                 /*
1807                  * Then the variable portion of the result (data length).
1808                  * Typically this portion is also compressed
1809                  * so we need to uncompress it also when necessary.
1810                  */
1811                 if (dns_type == ns_t_cname) {
1812                         if (!convert_label(start, end, ptr, uptr,
1813                                         uncomp_len - (uptr - uncompressed),
1814                                                 &pos, &comp_pos))
1815                                 goto out;
1816
1817                         uptr[-2] = comp_pos << 8;
1818                         uptr[-1] = comp_pos & 0xff;
1819
1820                         uptr += comp_pos;
1821                         ptr += pos;
1822
1823                 } else if (dns_type == ns_t_a || dns_type == ns_t_aaaa) {
1824                         dlen = uptr[-2] << 8 | uptr[-1];
1825
1826                         if (ptr + dlen > end) {
1827                                 debug("data len %d too long", dlen);
1828                                 goto out;
1829                         }
1830
1831                         memcpy(uptr, ptr, dlen);
1832                         uptr += dlen;
1833                         ptr += dlen;
1834
1835                 } else if (dns_type == ns_t_soa) {
1836                         int total_len = 0;
1837                         char *len_ptr;
1838
1839                         /* Primary name server expansion */
1840                         if (!convert_label(start, end, ptr, uptr,
1841                                         uncomp_len - (uptr - uncompressed),
1842                                                 &pos, &comp_pos))
1843                                 goto out;
1844
1845                         total_len += comp_pos;
1846                         len_ptr = &uptr[-2];
1847                         ptr += pos;
1848                         uptr += comp_pos;
1849
1850                         /* Responsible authority's mailbox */
1851                         if (!convert_label(start, end, ptr, uptr,
1852                                         uncomp_len - (uptr - uncompressed),
1853                                                 &pos, &comp_pos))
1854                                 goto out;
1855
1856                         total_len += comp_pos;
1857                         ptr += pos;
1858                         uptr += comp_pos;
1859
1860                         /*
1861                          * Copy rest of the soa fields (serial number,
1862                          * refresh interval, retry interval, expiration
1863                          * limit and minimum ttl). They are 20 bytes long.
1864                          */
1865                         memcpy(uptr, ptr, 20);
1866                         uptr += 20;
1867                         ptr += 20;
1868                         total_len += 20;
1869
1870                         /*
1871                          * Finally fix the length of the data part
1872                          */
1873                         len_ptr[0] = total_len << 8;
1874                         len_ptr[1] = total_len & 0xff;
1875                 }
1876
1877                 *uncompressed_ptr = uptr;
1878         }
1879
1880         return ptr;
1881
1882 out:
1883         return NULL;
1884 }
1885
1886 static int strip_domains(char *name, char *answers, int maxlen)
1887 {
1888         uint16_t data_len;
1889         int name_len = strlen(name);
1890         char *ptr, *start = answers, *end = answers + maxlen;
1891
1892         while (maxlen > 0) {
1893                 ptr = strstr(answers, name);
1894                 if (ptr) {
1895                         char *domain = ptr + name_len;
1896
1897                         if (*domain) {
1898                                 int domain_len = strlen(domain);
1899
1900                                 memmove(answers + name_len,
1901                                         domain + domain_len,
1902                                         end - (domain + domain_len));
1903
1904                                 end -= domain_len;
1905                                 maxlen -= domain_len;
1906                         }
1907                 }
1908
1909                 answers += strlen(answers) + 1;
1910                 answers += 2 + 2 + 4;  /* skip type, class and ttl fields */
1911
1912                 data_len = answers[0] << 8 | answers[1];
1913                 answers += 2; /* skip the length field */
1914
1915                 if (answers + data_len > end)
1916                         return -EINVAL;
1917
1918                 answers += data_len;
1919                 maxlen -= answers - ptr;
1920         }
1921
1922         return end - start;
1923 }
1924
1925 static int forward_dns_reply(unsigned char *reply, int reply_len, int protocol,
1926                                 struct server_data *data)
1927 {
1928         struct domain_hdr *hdr;
1929         struct request_data *req;
1930         int dns_id, sk, err, offset = protocol_offset(protocol);
1931
1932         if (offset < 0)
1933                 return offset;
1934
1935         hdr = (void *)(reply + offset);
1936         dns_id = reply[offset] | reply[offset + 1] << 8;
1937
1938         debug("Received %d bytes (id 0x%04x)", reply_len, dns_id);
1939
1940         req = find_request(dns_id);
1941         if (!req)
1942                 return -EINVAL;
1943
1944         debug("req %p dstid 0x%04x altid 0x%04x rcode %d",
1945                         req, req->dstid, req->altid, hdr->rcode);
1946
1947         reply[offset] = req->srcid & 0xff;
1948         reply[offset + 1] = req->srcid >> 8;
1949
1950         req->numresp++;
1951
1952         if (hdr->rcode == ns_r_noerror || !req->resp) {
1953                 unsigned char *new_reply = NULL;
1954
1955                 /*
1956                  * If the domain name was append
1957                  * remove it before forwarding the reply.
1958                  * If there were more than one question, then this
1959                  * domain name ripping can be hairy so avoid that
1960                  * and bail out in that that case.
1961                  *
1962                  * The reason we are doing this magic is that if the
1963                  * user's DNS client tries to resolv hostname without
1964                  * domain part, it also expects to get the result without
1965                  * a domain name part.
1966                  */
1967                 if (req->append_domain && ntohs(hdr->qdcount) == 1) {
1968                         uint16_t domain_len = 0;
1969                         uint16_t header_len;
1970                         uint16_t dns_type, dns_class;
1971                         uint8_t host_len, dns_type_pos;
1972                         char uncompressed[NS_MAXDNAME], *uptr;
1973                         char *ptr, *eom = (char *)reply + reply_len;
1974
1975                         /*
1976                          * ptr points to the first char of the hostname.
1977                          * ->hostname.domain.net
1978                          */
1979                         header_len = offset + sizeof(struct domain_hdr);
1980                         ptr = (char *)reply + header_len;
1981
1982                         host_len = *ptr;
1983                         if (host_len > 0)
1984                                 domain_len = strnlen(ptr + 1 + host_len,
1985                                                 reply_len - header_len);
1986
1987                         /*
1988                          * If the query type is anything other than A or AAAA,
1989                          * then bail out and pass the message as is.
1990                          * We only want to deal with IPv4 or IPv6 addresses.
1991                          */
1992                         dns_type_pos = host_len + 1 + domain_len + 1;
1993
1994                         dns_type = ptr[dns_type_pos] << 8 |
1995                                                         ptr[dns_type_pos + 1];
1996                         dns_class = ptr[dns_type_pos + 2] << 8 |
1997                                                         ptr[dns_type_pos + 3];
1998                         if (dns_type != ns_t_a && dns_type != ns_t_aaaa &&
1999                                         dns_class != ns_c_in) {
2000                                 debug("Pass msg dns type %d class %d",
2001                                         dns_type, dns_class);
2002                                 goto pass;
2003                         }
2004
2005                         /*
2006                          * Remove the domain name and replace it by the end
2007                          * of reply. Check if the domain is really there
2008                          * before trying to copy the data. We also need to
2009                          * uncompress the answers if necessary.
2010                          * The domain_len can be 0 because if the original
2011                          * query did not contain a domain name, then we are
2012                          * sending two packets, first without the domain name
2013                          * and the second packet with domain name.
2014                          * The append_domain is set to true even if we sent
2015                          * the first packet without domain name. In this
2016                          * case we end up in this branch.
2017                          */
2018                         if (domain_len > 0) {
2019                                 int len = host_len + 1;
2020                                 int new_len, fixed_len;
2021                                 char *answers;
2022
2023                                 /*
2024                                  * First copy host (without domain name) into
2025                                  * tmp buffer.
2026                                  */
2027                                 uptr = &uncompressed[0];
2028                                 memcpy(uptr, ptr, len);
2029
2030                                 uptr[len] = '\0'; /* host termination */
2031                                 uptr += len + 1;
2032
2033                                 /*
2034                                  * Copy type and class fields of the question.
2035                                  */
2036                                 ptr += len + domain_len + 1;
2037                                 memcpy(uptr, ptr, NS_QFIXEDSZ);
2038
2039                                 /*
2040                                  * ptr points to answers after this
2041                                  */
2042                                 ptr += NS_QFIXEDSZ;
2043                                 uptr += NS_QFIXEDSZ;
2044                                 answers = uptr;
2045                                 fixed_len = answers - uncompressed;
2046
2047                                 /*
2048                                  * We then uncompress the result to buffer
2049                                  * so that we can rip off the domain name
2050                                  * part from the question. First answers,
2051                                  * then name server (authority) information,
2052                                  * and finally additional record info.
2053                                  */
2054
2055                                 ptr = uncompress(ntohs(hdr->ancount),
2056                                                 (char *)reply + offset, eom,
2057                                                 ptr, uncompressed, NS_MAXDNAME,
2058                                                 &uptr);
2059                                 if (!ptr)
2060                                         goto out;
2061
2062                                 ptr = uncompress(ntohs(hdr->nscount),
2063                                                 (char *)reply + offset, eom,
2064                                                 ptr, uncompressed, NS_MAXDNAME,
2065                                                 &uptr);
2066                                 if (!ptr)
2067                                         goto out;
2068
2069                                 ptr = uncompress(ntohs(hdr->arcount),
2070                                                 (char *)reply + offset, eom,
2071                                                 ptr, uncompressed, NS_MAXDNAME,
2072                                                 &uptr);
2073                                 if (!ptr)
2074                                         goto out;
2075
2076                                 /*
2077                                  * The uncompressed buffer now contains almost
2078                                  * valid response. Final step is to get rid of
2079                                  * the domain name because at least glibc
2080                                  * gethostbyname() implementation does extra
2081                                  * checks and expects to find an answer without
2082                                  * domain name if we asked a query without
2083                                  * domain part. Note that glibc getaddrinfo()
2084                                  * works differently and accepts FQDN in answer
2085                                  */
2086                                 new_len = strip_domains(uncompressed, answers,
2087                                                         uptr - answers);
2088                                 if (new_len < 0) {
2089                                         debug("Corrupted packet");
2090                                         return -EINVAL;
2091                                 }
2092
2093                                 /*
2094                                  * Because we have now uncompressed the answers
2095                                  * we might have to create a bigger buffer to
2096                                  * hold all that data.
2097                                  */
2098
2099                                 reply_len = header_len + new_len + fixed_len;
2100
2101                                 new_reply = g_try_malloc(reply_len);
2102                                 if (!new_reply)
2103                                         return -ENOMEM;
2104
2105                                 memcpy(new_reply, reply, header_len);
2106                                 memcpy(new_reply + header_len, uncompressed,
2107                                         new_len + fixed_len);
2108
2109                                 reply = new_reply;
2110                         }
2111                 }
2112
2113         pass:
2114                 g_free(req->resp);
2115                 req->resplen = 0;
2116
2117                 req->resp = g_try_malloc(reply_len);
2118                 if (!req->resp)
2119                         return -ENOMEM;
2120
2121                 memcpy(req->resp, reply, reply_len);
2122                 req->resplen = reply_len;
2123
2124                 cache_update(data, reply, reply_len);
2125
2126                 g_free(new_reply);
2127         }
2128
2129 out:
2130         if (req->numresp < req->numserv) {
2131                 if (hdr->rcode > ns_r_noerror) {
2132                         return -EINVAL;
2133                 } else if (hdr->ancount == 0 && req->append_domain) {
2134                         return -EINVAL;
2135                 }
2136         }
2137
2138         request_list = g_slist_remove(request_list, req);
2139
2140         if (protocol == IPPROTO_UDP) {
2141                 sk = get_req_udp_socket(req);
2142                 if (sk < 0) {
2143                         errno = -EIO;
2144                         err = -EIO;
2145                 } else
2146                         err = sendto(sk, req->resp, req->resplen, 0,
2147                                 &req->sa, req->sa_len);
2148         } else {
2149                 sk = req->client_sk;
2150                 err = send(sk, req->resp, req->resplen, MSG_NOSIGNAL);
2151         }
2152
2153         if (err < 0)
2154                 debug("Cannot send msg, sk %d proto %d errno %d/%s", sk,
2155                         protocol, errno, strerror(errno));
2156         else
2157                 debug("proto %d sent %d bytes to %d", protocol, err, sk);
2158
2159         destroy_request_data(req);
2160
2161         return err;
2162 }
2163
2164 static void server_destroy_socket(struct server_data *data)
2165 {
2166         debug("index %d server %s proto %d", data->index,
2167                                         data->server, data->protocol);
2168
2169         if (data->watch > 0) {
2170                 g_source_remove(data->watch);
2171                 data->watch = 0;
2172         }
2173
2174         if (data->timeout > 0) {
2175                 g_source_remove(data->timeout);
2176                 data->timeout = 0;
2177         }
2178
2179         if (data->channel) {
2180                 g_io_channel_shutdown(data->channel, TRUE, NULL);
2181                 g_io_channel_unref(data->channel);
2182                 data->channel = NULL;
2183         }
2184
2185         g_free(data->incoming_reply);
2186         data->incoming_reply = NULL;
2187 }
2188
2189 static void destroy_server(struct server_data *server)
2190 {
2191         debug("index %d server %s sock %d", server->index, server->server,
2192                         server->channel ?
2193                         g_io_channel_unix_get_fd(server->channel): -1);
2194
2195         server_list = g_slist_remove(server_list, server);
2196         server_destroy_socket(server);
2197
2198         if (server->protocol == IPPROTO_UDP && server->enabled)
2199                 debug("Removing DNS server %s", server->server);
2200
2201         g_free(server->server);
2202         g_list_free_full(server->domains, g_free);
2203         g_free(server->server_addr);
2204
2205         /*
2206          * We do not remove cache right away but delay it few seconds.
2207          * The idea is that when IPv6 DNS server is added via RDNSS, it has a
2208          * lifetime. When the lifetime expires we decrease the refcount so it
2209          * is possible that the cache is then removed. Because a new DNS server
2210          * is usually created almost immediately we would then loose the cache
2211          * without any good reason. The small delay allows the new RDNSS to
2212          * create a new DNS server instance and the refcount does not go to 0.
2213          */
2214         if (cache && !cache_timer)
2215                 cache_timer = g_timeout_add_seconds(3, try_remove_cache, NULL);
2216
2217         g_free(server);
2218 }
2219
2220 static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
2221                                                         gpointer user_data)
2222 {
2223         unsigned char buf[4096];
2224         int sk, err, len;
2225         struct server_data *data = user_data;
2226
2227         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2228                 connman_error("Error with UDP server %s", data->server);
2229                 server_destroy_socket(data);
2230                 return FALSE;
2231         }
2232
2233         sk = g_io_channel_unix_get_fd(channel);
2234
2235         len = recv(sk, buf, sizeof(buf), 0);
2236         if (len < 12)
2237                 return TRUE;
2238
2239         err = forward_dns_reply(buf, len, IPPROTO_UDP, data);
2240         if (err < 0)
2241                 return TRUE;
2242
2243         return TRUE;
2244 }
2245
2246 static gboolean tcp_server_event(GIOChannel *channel, GIOCondition condition,
2247                                                         gpointer user_data)
2248 {
2249         int sk;
2250         struct server_data *server = user_data;
2251
2252         sk = g_io_channel_unix_get_fd(channel);
2253         if (sk == 0)
2254                 return FALSE;
2255
2256         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
2257                 GSList *list;
2258 hangup:
2259                 debug("TCP server channel closed, sk %d", sk);
2260
2261                 /*
2262                  * Discard any partial response which is buffered; better
2263                  * to get a proper response from a working server.
2264                  */
2265                 g_free(server->incoming_reply);
2266                 server->incoming_reply = NULL;
2267
2268                 list = request_list;
2269                 while (list) {
2270                         struct request_data *req = list->data;
2271                         struct domain_hdr *hdr;
2272                         list = list->next;
2273
2274                         if (req->protocol == IPPROTO_UDP)
2275                                 continue;
2276
2277                         if (!req->request)
2278                                 continue;
2279
2280                         /*
2281                          * If we're not waiting for any further response
2282                          * from another name server, then we send an error
2283                          * response to the client.
2284                          */
2285                         if (req->numserv && --(req->numserv))
2286                                 continue;
2287
2288                         hdr = (void *) (req->request + 2);
2289                         hdr->id = req->srcid;
2290                         send_response(req->client_sk, req->request,
2291                                 req->request_len, NULL, 0, IPPROTO_TCP);
2292
2293                         request_list = g_slist_remove(request_list, req);
2294                 }
2295
2296                 destroy_server(server);
2297
2298                 return FALSE;
2299         }
2300
2301         if ((condition & G_IO_OUT) && !server->connected) {
2302                 GSList *list;
2303                 GList *domains;
2304                 bool no_request_sent = true;
2305                 struct server_data *udp_server;
2306
2307                 udp_server = find_server(server->index, server->server,
2308                                                                 IPPROTO_UDP);
2309                 if (udp_server) {
2310                         for (domains = udp_server->domains; domains;
2311                                                 domains = domains->next) {
2312                                 char *dom = domains->data;
2313
2314                                 debug("Adding domain %s to %s",
2315                                                 dom, server->server);
2316
2317                                 server->domains = g_list_append(server->domains,
2318                                                                 g_strdup(dom));
2319                         }
2320                 }
2321
2322                 server->connected = true;
2323                 server_list = g_slist_append(server_list, server);
2324
2325                 if (server->timeout > 0) {
2326                         g_source_remove(server->timeout);
2327                         server->timeout = 0;
2328                 }
2329
2330                 for (list = request_list; list; ) {
2331                         struct request_data *req = list->data;
2332                         int status;
2333
2334                         if (req->protocol == IPPROTO_UDP) {
2335                                 list = list->next;
2336                                 continue;
2337                         }
2338
2339                         debug("Sending req %s over TCP", (char *)req->name);
2340
2341                         status = ns_resolv(server, req,
2342                                                 req->request, req->name);
2343                         if (status > 0) {
2344                                 /*
2345                                  * A cached result was sent,
2346                                  * so the request can be released
2347                                  */
2348                                 list = list->next;
2349                                 request_list = g_slist_remove(request_list, req);
2350                                 destroy_request_data(req);
2351                                 continue;
2352                         }
2353
2354                         if (status < 0) {
2355                                 list = list->next;
2356                                 continue;
2357                         }
2358
2359                         no_request_sent = false;
2360
2361                         if (req->timeout > 0)
2362                                 g_source_remove(req->timeout);
2363
2364                         req->timeout = g_timeout_add_seconds(30,
2365                                                 request_timeout, req);
2366                         list = list->next;
2367                 }
2368
2369                 if (no_request_sent) {
2370                         destroy_server(server);
2371                         return FALSE;
2372                 }
2373
2374         } else if (condition & G_IO_IN) {
2375                 struct partial_reply *reply = server->incoming_reply;
2376                 int bytes_recv;
2377
2378                 if (!reply) {
2379                         unsigned char reply_len_buf[2];
2380                         uint16_t reply_len;
2381
2382                         bytes_recv = recv(sk, reply_len_buf, 2, MSG_PEEK);
2383                         if (!bytes_recv) {
2384                                 goto hangup;
2385                         } else if (bytes_recv < 0) {
2386                                 if (errno == EAGAIN || errno == EWOULDBLOCK)
2387                                         return TRUE;
2388
2389                                 connman_error("DNS proxy error %s",
2390                                                 strerror(errno));
2391                                 goto hangup;
2392                         } else if (bytes_recv < 2)
2393                                 return TRUE;
2394
2395                         reply_len = reply_len_buf[1] | reply_len_buf[0] << 8;
2396                         reply_len += 2;
2397
2398                         debug("TCP reply %d bytes from %d", reply_len, sk);
2399
2400                         reply = g_try_malloc(sizeof(*reply) + reply_len + 2);
2401                         if (!reply)
2402                                 return TRUE;
2403
2404                         reply->len = reply_len;
2405                         reply->received = 0;
2406
2407                         server->incoming_reply = reply;
2408                 }
2409
2410                 while (reply->received < reply->len) {
2411                         bytes_recv = recv(sk, reply->buf + reply->received,
2412                                         reply->len - reply->received, 0);
2413                         if (!bytes_recv) {
2414                                 connman_error("DNS proxy TCP disconnect");
2415                                 break;
2416                         } else if (bytes_recv < 0) {
2417                                 if (errno == EAGAIN || errno == EWOULDBLOCK)
2418                                         return TRUE;
2419
2420                                 connman_error("DNS proxy error %s",
2421                                                 strerror(errno));
2422                                 break;
2423                         }
2424                         reply->received += bytes_recv;
2425                 }
2426
2427                 forward_dns_reply(reply->buf, reply->received, IPPROTO_TCP,
2428                                         server);
2429
2430                 g_free(reply);
2431                 server->incoming_reply = NULL;
2432
2433                 destroy_server(server);
2434
2435                 return FALSE;
2436         }
2437
2438         return TRUE;
2439 }
2440
2441 static gboolean tcp_idle_timeout(gpointer user_data)
2442 {
2443         struct server_data *server = user_data;
2444
2445         debug("");
2446
2447         if (!server)
2448                 return FALSE;
2449
2450         destroy_server(server);
2451
2452         return FALSE;
2453 }
2454
2455 static int server_create_socket(struct server_data *data)
2456 {
2457         int sk, err;
2458         char *interface;
2459
2460         debug("index %d server %s proto %d", data->index,
2461                                         data->server, data->protocol);
2462
2463         sk = socket(data->server_addr->sa_family,
2464                 data->protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM,
2465                 data->protocol);
2466         if (sk < 0) {
2467                 err = errno;
2468                 connman_error("Failed to create server %s socket",
2469                                                         data->server);
2470                 server_destroy_socket(data);
2471                 return -err;
2472         }
2473
2474         debug("sk %d", sk);
2475
2476         interface = connman_inet_ifname(data->index);
2477         if (interface) {
2478                 if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
2479                                         interface,
2480                                         strlen(interface) + 1) < 0) {
2481                         err = errno;
2482                         connman_error("Failed to bind server %s "
2483                                                 "to interface %s",
2484                                                 data->server, interface);
2485                         close(sk);
2486                         server_destroy_socket(data);
2487                         g_free(interface);
2488                         return -err;
2489                 }
2490                 g_free(interface);
2491         }
2492
2493         data->channel = g_io_channel_unix_new(sk);
2494         if (!data->channel) {
2495                 connman_error("Failed to create server %s channel",
2496                                                         data->server);
2497                 close(sk);
2498                 server_destroy_socket(data);
2499                 return -ENOMEM;
2500         }
2501
2502         g_io_channel_set_close_on_unref(data->channel, TRUE);
2503
2504         if (data->protocol == IPPROTO_TCP) {
2505                 g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
2506                 data->watch = g_io_add_watch(data->channel,
2507                         G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
2508                                                 tcp_server_event, data);
2509                 data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
2510                                                                 data);
2511         } else
2512                 data->watch = g_io_add_watch(data->channel,
2513                         G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
2514                                                 udp_server_event, data);
2515
2516         if (connect(sk, data->server_addr, data->server_addr_len) < 0) {
2517                 err = errno;
2518
2519                 if ((data->protocol == IPPROTO_TCP && errno != EINPROGRESS) ||
2520                                 data->protocol == IPPROTO_UDP) {
2521
2522                         connman_error("Failed to connect to server %s",
2523                                                                 data->server);
2524                         server_destroy_socket(data);
2525                         return -err;
2526                 }
2527         }
2528
2529         create_cache();
2530
2531         return 0;
2532 }
2533
2534 static void enable_fallback(bool enable)
2535 {
2536         GSList *list;
2537
2538         for (list = server_list; list; list = list->next) {
2539                 struct server_data *data = list->data;
2540
2541                 if (data->index != -1)
2542                         continue;
2543
2544                 if (enable)
2545                         DBG("Enabling fallback DNS server %s", data->server);
2546                 else
2547                         DBG("Disabling fallback DNS server %s", data->server);
2548
2549                 data->enabled = enable;
2550         }
2551 }
2552
2553 static struct server_data *create_server(int index,
2554                                         const char *domain, const char *server,
2555                                         int protocol)
2556 {
2557         struct server_data *data;
2558         struct addrinfo hints, *rp;
2559         int ret;
2560
2561         DBG("index %d server %s", index, server);
2562
2563         data = g_try_new0(struct server_data, 1);
2564         if (!data) {
2565                 connman_error("Failed to allocate server %s data", server);
2566                 return NULL;
2567         }
2568
2569         data->index = index;
2570         if (domain)
2571                 data->domains = g_list_append(data->domains, g_strdup(domain));
2572         data->server = g_strdup(server);
2573         data->protocol = protocol;
2574
2575         memset(&hints, 0, sizeof(hints));
2576
2577         switch (protocol) {
2578         case IPPROTO_UDP:
2579                 hints.ai_socktype = SOCK_DGRAM;
2580                 break;
2581
2582         case IPPROTO_TCP:
2583                 hints.ai_socktype = SOCK_STREAM;
2584                 break;
2585
2586         default:
2587                 destroy_server(data);
2588                 return NULL;
2589         }
2590         hints.ai_family = AF_UNSPEC;
2591         hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST;
2592
2593         ret = getaddrinfo(data->server, "53", &hints, &rp);
2594         if (ret) {
2595                 connman_error("Failed to parse server %s address: %s\n",
2596                               data->server, gai_strerror(ret));
2597                 destroy_server(data);
2598                 return NULL;
2599         }
2600
2601         /* Do not blindly copy this code elsewhere; it doesn't loop over the
2602            results using ->ai_next as it should. That's OK in *this* case
2603            because it was a numeric lookup; we *know* there's only one. */
2604
2605         data->server_addr_len = rp->ai_addrlen;
2606
2607         switch (rp->ai_family) {
2608         case AF_INET:
2609                 data->server_addr = (struct sockaddr *)
2610                                         g_try_new0(struct sockaddr_in, 1);
2611                 break;
2612         case AF_INET6:
2613                 data->server_addr = (struct sockaddr *)
2614                                         g_try_new0(struct sockaddr_in6, 1);
2615                 break;
2616         default:
2617                 connman_error("Wrong address family %d", rp->ai_family);
2618                 break;
2619         }
2620         if (!data->server_addr) {
2621                 freeaddrinfo(rp);
2622                 destroy_server(data);
2623                 return NULL;
2624         }
2625         memcpy(data->server_addr, rp->ai_addr, rp->ai_addrlen);
2626         freeaddrinfo(rp);
2627
2628         if (server_create_socket(data) != 0) {
2629                 destroy_server(data);
2630                 return NULL;
2631         }
2632
2633         if (protocol == IPPROTO_UDP) {
2634                 if (__connman_service_index_is_default(data->index) ||
2635                                 __connman_service_index_is_split_routing(
2636                                                                 data->index)) {
2637                         data->enabled = true;
2638                         DBG("Adding DNS server %s", data->server);
2639
2640                         enable_fallback(false);
2641                 }
2642
2643                 server_list = g_slist_append(server_list, data);
2644         }
2645
2646         return data;
2647 }
2648
2649 static bool resolv(struct request_data *req,
2650                                 gpointer request, gpointer name)
2651 {
2652         GSList *list;
2653
2654         for (list = server_list; list; list = list->next) {
2655                 struct server_data *data = list->data;
2656
2657                 if (data->protocol == IPPROTO_TCP) {
2658                         DBG("server %s ignored proto TCP", data->server);
2659                         continue;
2660                 }
2661
2662                 debug("server %s enabled %d", data->server, data->enabled);
2663
2664                 if (!data->enabled)
2665                         continue;
2666
2667                 if (!data->channel && data->protocol == IPPROTO_UDP) {
2668                         if (server_create_socket(data) < 0) {
2669                                 DBG("socket creation failed while resolving");
2670                                 continue;
2671                         }
2672                 }
2673
2674                 if (ns_resolv(data, req, request, name) > 0)
2675                         return true;
2676         }
2677
2678         return false;
2679 }
2680
2681 static void update_domain(int index, const char *domain, bool append)
2682 {
2683         GSList *list;
2684
2685         DBG("index %d domain %s", index, domain);
2686
2687         if (!domain)
2688                 return;
2689
2690         for (list = server_list; list; list = list->next) {
2691                 struct server_data *data = list->data;
2692                 GList *dom_list;
2693                 char *dom;
2694                 bool dom_found = false;
2695
2696                 if (data->index < 0)
2697                         continue;
2698
2699                 if (data->index != index)
2700                         continue;
2701
2702                 for (dom_list = data->domains; dom_list;
2703                                 dom_list = dom_list->next) {
2704                         dom = dom_list->data;
2705
2706                         if (g_str_equal(dom, domain)) {
2707                                 dom_found = true;
2708                                 break;
2709                         }
2710                 }
2711
2712                 if (!dom_found && append) {
2713                         data->domains =
2714                                 g_list_append(data->domains, g_strdup(domain));
2715                 } else if (dom_found && !append) {
2716                         data->domains =
2717                                 g_list_remove(data->domains, dom);
2718                         g_free(dom);
2719                 }
2720         }
2721 }
2722
2723 static void append_domain(int index, const char *domain)
2724 {
2725         update_domain(index, domain, true);
2726 }
2727
2728 static void remove_domain(int index, const char *domain)
2729 {
2730         update_domain(index, domain, false);
2731 }
2732
2733 static void flush_requests(struct server_data *server)
2734 {
2735         GSList *list;
2736
2737         list = request_list;
2738         while (list) {
2739                 struct request_data *req = list->data;
2740
2741                 list = list->next;
2742
2743                 if (ns_resolv(server, req, req->request, req->name)) {
2744                         /*
2745                          * A cached result was sent,
2746                          * so the request can be released
2747                          */
2748                         request_list =
2749                                 g_slist_remove(request_list, req);
2750                         destroy_request_data(req);
2751                         continue;
2752                 }
2753
2754                 if (req->timeout > 0)
2755                         g_source_remove(req->timeout);
2756
2757                 req->timeout = g_timeout_add_seconds(5, request_timeout, req);
2758         }
2759 }
2760
2761 int __connman_dnsproxy_append(int index, const char *domain,
2762                                                         const char *server)
2763 {
2764         struct server_data *data;
2765
2766         DBG("index %d server %s", index, server);
2767
2768         if (!server && !domain)
2769                 return -EINVAL;
2770
2771         if (!server) {
2772                 append_domain(index, domain);
2773
2774                 return 0;
2775         }
2776
2777         if (g_str_equal(server, "127.0.0.1"))
2778                 return -ENODEV;
2779
2780         if (g_str_equal(server, "::1"))
2781                 return -ENODEV;
2782
2783         data = find_server(index, server, IPPROTO_UDP);
2784         if (data) {
2785                 append_domain(index, domain);
2786                 return 0;
2787         }
2788
2789         data = create_server(index, domain, server, IPPROTO_UDP);
2790         if (!data)
2791                 return -EIO;
2792
2793         flush_requests(data);
2794
2795         return 0;
2796 }
2797
2798 static void remove_server(int index, const char *domain,
2799                         const char *server, int protocol)
2800 {
2801         struct server_data *data;
2802         GSList *list;
2803
2804         data = find_server(index, server, protocol);
2805         if (!data)
2806                 return;
2807
2808         destroy_server(data);
2809
2810         for (list = server_list; list; list = list->next) {
2811                 struct server_data *data = list->data;
2812
2813                 if (data->index != -1 && data->enabled == true)
2814                         return;
2815         }
2816
2817         enable_fallback(true);
2818 }
2819
2820 int __connman_dnsproxy_remove(int index, const char *domain,
2821                                                         const char *server)
2822 {
2823         DBG("index %d server %s", index, server);
2824
2825         if (!server && !domain)
2826                 return -EINVAL;
2827
2828         if (!server) {
2829                 remove_domain(index, domain);
2830
2831                 return 0;
2832         }
2833
2834         if (g_str_equal(server, "127.0.0.1"))
2835                 return -ENODEV;
2836
2837         if (g_str_equal(server, "::1"))
2838                 return -ENODEV;
2839
2840         remove_server(index, domain, server, IPPROTO_UDP);
2841         remove_server(index, domain, server, IPPROTO_TCP);
2842
2843         return 0;
2844 }
2845
2846 static void dnsproxy_offline_mode(bool enabled)
2847 {
2848         GSList *list;
2849
2850         DBG("enabled %d", enabled);
2851
2852         for (list = server_list; list; list = list->next) {
2853                 struct server_data *data = list->data;
2854
2855                 if (!enabled) {
2856                         DBG("Enabling DNS server %s", data->server);
2857                         data->enabled = true;
2858                         cache_invalidate();
2859                         cache_refresh();
2860                 } else {
2861                         DBG("Disabling DNS server %s", data->server);
2862                         data->enabled = false;
2863                         cache_invalidate();
2864                 }
2865         }
2866 }
2867
2868 static void dnsproxy_default_changed(struct connman_service *service)
2869 {
2870         bool server_enabled = false;
2871         GSList *list;
2872         int index;
2873
2874         DBG("service %p", service);
2875
2876         /* DNS has changed, invalidate the cache */
2877         cache_invalidate();
2878
2879         if (!service) {
2880                 /* When no services are active, then disable DNS proxying */
2881                 dnsproxy_offline_mode(true);
2882                 return;
2883         }
2884
2885         index = __connman_service_get_index(service);
2886         if (index < 0)
2887                 return;
2888
2889         for (list = server_list; list; list = list->next) {
2890                 struct server_data *data = list->data;
2891
2892                 if (data->index == index) {
2893                         DBG("Enabling DNS server %s", data->server);
2894                         data->enabled = true;
2895                         server_enabled = true;
2896                 } else {
2897                         DBG("Disabling DNS server %s", data->server);
2898                         data->enabled = false;
2899                 }
2900         }
2901
2902         if (!server_enabled)
2903                 enable_fallback(true);
2904
2905         cache_refresh();
2906 }
2907
2908 static struct connman_notifier dnsproxy_notifier = {
2909         .name                   = "dnsproxy",
2910         .default_changed        = dnsproxy_default_changed,
2911         .offline_mode           = dnsproxy_offline_mode,
2912 };
2913
2914 static unsigned char opt_edns0_type[2] = { 0x00, 0x29 };
2915
2916 static int parse_request(unsigned char *buf, int len,
2917                                         char *name, unsigned int size)
2918 {
2919         struct domain_hdr *hdr = (void *) buf;
2920         uint16_t qdcount = ntohs(hdr->qdcount);
2921         uint16_t arcount = ntohs(hdr->arcount);
2922         unsigned char *ptr;
2923         char *last_label = NULL;
2924         unsigned int remain, used = 0;
2925
2926         if (len < 12)
2927                 return -EINVAL;
2928
2929         debug("id 0x%04x qr %d opcode %d qdcount %d arcount %d",
2930                                         hdr->id, hdr->qr, hdr->opcode,
2931                                                         qdcount, arcount);
2932
2933         if (hdr->qr != 0 || qdcount != 1)
2934                 return -EINVAL;
2935
2936         name[0] = '\0';
2937
2938         ptr = buf + sizeof(struct domain_hdr);
2939         remain = len - sizeof(struct domain_hdr);
2940
2941         while (remain > 0) {
2942                 uint8_t label_len = *ptr;
2943
2944                 if (label_len == 0x00) {
2945                         last_label = (char *) (ptr + 1);
2946                         break;
2947                 }
2948
2949                 if (used + label_len + 1 > size)
2950                         return -ENOBUFS;
2951
2952                 strncat(name, (char *) (ptr + 1), label_len);
2953                 strcat(name, ".");
2954
2955                 used += label_len + 1;
2956
2957                 ptr += label_len + 1;
2958                 remain -= label_len + 1;
2959         }
2960
2961         if (last_label && arcount && remain >= 9 && last_label[4] == 0 &&
2962                                 !memcmp(last_label + 5, opt_edns0_type, 2)) {
2963                 uint16_t edns0_bufsize;
2964
2965                 edns0_bufsize = last_label[7] << 8 | last_label[8];
2966
2967                 debug("EDNS0 buffer size %u", edns0_bufsize);
2968
2969                 /* This is an evil hack until full TCP support has been
2970                  * implemented.
2971                  *
2972                  * Somtimes the EDNS0 request gets send with a too-small
2973                  * buffer size. Since glibc doesn't seem to crash when it
2974                  * gets a response biffer then it requested, just bump
2975                  * the buffer size up to 4KiB.
2976                  */
2977                 if (edns0_bufsize < 0x1000) {
2978                         last_label[7] = 0x10;
2979                         last_label[8] = 0x00;
2980                 }
2981         }
2982
2983         debug("query %s", name);
2984
2985         return 0;
2986 }
2987
2988 static void client_reset(struct tcp_partial_client_data *client)
2989 {
2990         if (!client)
2991                 return;
2992
2993         if (client->channel) {
2994                 debug("client %d closing",
2995                         g_io_channel_unix_get_fd(client->channel));
2996
2997                 g_io_channel_unref(client->channel);
2998                 client->channel = NULL;
2999         }
3000
3001         if (client->watch > 0) {
3002                 g_source_remove(client->watch);
3003                 client->watch = 0;
3004         }
3005
3006         if (client->timeout > 0) {
3007                 g_source_remove(client->timeout);
3008                 client->timeout = 0;
3009         }
3010
3011         g_free(client->buf);
3012         client->buf = NULL;
3013
3014         client->buf_end = 0;
3015 }
3016
3017 static unsigned int get_msg_len(unsigned char *buf)
3018 {
3019         return buf[0]<<8 | buf[1];
3020 }
3021
3022 static bool read_tcp_data(struct tcp_partial_client_data *client,
3023                                 void *client_addr, socklen_t client_addr_len,
3024                                 int read_len)
3025 {
3026         char query[TCP_MAX_BUF_LEN];
3027         struct request_data *req;
3028         int client_sk, err;
3029         unsigned int msg_len;
3030         GSList *list;
3031         bool waiting_for_connect = false;
3032         int qtype = 0;
3033         struct cache_entry *entry;
3034
3035         client_sk = g_io_channel_unix_get_fd(client->channel);
3036
3037         if (read_len == 0) {
3038                 debug("client %d closed, pending %d bytes",
3039                         client_sk, client->buf_end);
3040                 g_hash_table_remove(partial_tcp_req_table,
3041                                         GINT_TO_POINTER(client_sk));
3042                 return false;
3043         }
3044
3045         debug("client %d received %d bytes", client_sk, read_len);
3046
3047         client->buf_end += read_len;
3048
3049         if (client->buf_end < 2)
3050                 return true;
3051
3052         msg_len = get_msg_len(client->buf);
3053         if (msg_len > TCP_MAX_BUF_LEN) {
3054                 debug("client %d sent too much data %d", client_sk, msg_len);
3055                 g_hash_table_remove(partial_tcp_req_table,
3056                                         GINT_TO_POINTER(client_sk));
3057                 return false;
3058         }
3059
3060 read_another:
3061         debug("client %d msg len %d end %d past end %d", client_sk, msg_len,
3062                 client->buf_end, client->buf_end - (msg_len + 2));
3063
3064         if (client->buf_end < (msg_len + 2)) {
3065                 debug("client %d still missing %d bytes",
3066                         client_sk,
3067                         msg_len + 2 - client->buf_end);
3068                 return true;
3069         }
3070
3071         debug("client %d all data %d received", client_sk, msg_len);
3072
3073         err = parse_request(client->buf + 2, msg_len,
3074                         query, sizeof(query));
3075         if (err < 0 || (g_slist_length(server_list) == 0)) {
3076                 send_response(client_sk, client->buf, msg_len + 2,
3077                         NULL, 0, IPPROTO_TCP);
3078                 return true;
3079         }
3080
3081         req = g_try_new0(struct request_data, 1);
3082         if (!req)
3083                 return true;
3084
3085         memcpy(&req->sa, client_addr, client_addr_len);
3086         req->sa_len = client_addr_len;
3087         req->client_sk = client_sk;
3088         req->protocol = IPPROTO_TCP;
3089         req->family = client->family;
3090
3091         req->srcid = client->buf[2] | (client->buf[3] << 8);
3092         req->dstid = get_id();
3093         req->altid = get_id();
3094         req->request_len = msg_len + 2;
3095
3096         client->buf[2] = req->dstid & 0xff;
3097         client->buf[3] = req->dstid >> 8;
3098
3099         req->numserv = 0;
3100         req->ifdata = client->ifdata;
3101         req->append_domain = false;
3102
3103         /*
3104          * Check if the answer is found in the cache before
3105          * creating sockets to the server.
3106          */
3107         entry = cache_check(client->buf, &qtype, IPPROTO_TCP);
3108         if (entry) {
3109                 int ttl_left = 0;
3110                 struct cache_data *data;
3111
3112                 debug("cache hit %s type %s", query, qtype == 1 ? "A" : "AAAA");
3113                 if (qtype == 1)
3114                         data = entry->ipv4;
3115                 else
3116                         data = entry->ipv6;
3117
3118                 if (data) {
3119                         ttl_left = data->valid_until - time(NULL);
3120                         entry->hits++;
3121
3122                         send_cached_response(client_sk, data->data,
3123                                         data->data_len, NULL, 0, IPPROTO_TCP,
3124                                         req->srcid, data->answers, ttl_left);
3125
3126                         g_free(req);
3127                         goto out;
3128                 } else
3129                         debug("data missing, ignoring cache for this query");
3130         }
3131
3132         for (list = server_list; list; list = list->next) {
3133                 struct server_data *data = list->data;
3134
3135                 if (data->protocol != IPPROTO_UDP || !data->enabled)
3136                         continue;
3137
3138                 if (!create_server(data->index, NULL, data->server,
3139                                         IPPROTO_TCP))
3140                         continue;
3141
3142                 waiting_for_connect = true;
3143         }
3144
3145         if (!waiting_for_connect) {
3146                 /* No server is waiting for connect */
3147                 send_response(client_sk, client->buf,
3148                         req->request_len, NULL, 0, IPPROTO_TCP);
3149                 g_free(req);
3150                 return true;
3151         }
3152
3153         /*
3154          * The server is not connected yet.
3155          * Copy the relevant buffers.
3156          * The request will actually be sent once we're
3157          * properly connected over TCP to the nameserver.
3158          */
3159         req->request = g_try_malloc0(req->request_len);
3160         if (!req->request) {
3161                 send_response(client_sk, client->buf,
3162                         req->request_len, NULL, 0, IPPROTO_TCP);
3163                 g_free(req);
3164                 goto out;
3165         }
3166         memcpy(req->request, client->buf, req->request_len);
3167
3168         req->name = g_try_malloc0(sizeof(query));
3169         if (!req->name) {
3170                 send_response(client_sk, client->buf,
3171                         req->request_len, NULL, 0, IPPROTO_TCP);
3172                 g_free(req->request);
3173                 g_free(req);
3174                 goto out;
3175         }
3176         memcpy(req->name, query, sizeof(query));
3177
3178         req->timeout = g_timeout_add_seconds(30, request_timeout, req);
3179
3180         request_list = g_slist_append(request_list, req);
3181
3182 out:
3183         if (client->buf_end > (msg_len + 2)) {
3184                 debug("client %d buf %p -> %p end %d len %d new %d",
3185                         client_sk,
3186                         client->buf + msg_len + 2,
3187                         client->buf, client->buf_end,
3188                         TCP_MAX_BUF_LEN - client->buf_end,
3189                         client->buf_end - (msg_len + 2));
3190                 memmove(client->buf, client->buf + msg_len + 2,
3191                         TCP_MAX_BUF_LEN - client->buf_end);
3192                 client->buf_end = client->buf_end - (msg_len + 2);
3193
3194                 /*
3195                  * If we have a full message waiting, just read it
3196                  * immediately.
3197                  */
3198                 msg_len = get_msg_len(client->buf);
3199                 if ((msg_len + 2) == client->buf_end) {
3200                         debug("client %d reading another %d bytes", client_sk,
3201                                                                 msg_len + 2);
3202                         goto read_another;
3203                 }
3204         } else {
3205                 debug("client %d clearing reading buffer", client_sk);
3206
3207                 client->buf_end = 0;
3208                 memset(client->buf, 0, TCP_MAX_BUF_LEN);
3209
3210                 /*
3211                  * We received all the packets from client so we must also
3212                  * remove the timeout handler here otherwise we might get
3213                  * timeout while waiting the results from server.
3214                  */
3215                 g_source_remove(client->timeout);
3216                 client->timeout = 0;
3217         }
3218
3219         return true;
3220 }
3221
3222 static gboolean tcp_client_event(GIOChannel *channel, GIOCondition condition,
3223                                 gpointer user_data)
3224 {
3225         struct tcp_partial_client_data *client = user_data;
3226         struct sockaddr_in6 client_addr6;
3227         socklen_t client_addr6_len = sizeof(client_addr6);
3228         struct sockaddr_in client_addr4;
3229         socklen_t client_addr4_len = sizeof(client_addr4);
3230         void *client_addr;
3231         socklen_t *client_addr_len;
3232         int len, client_sk;
3233
3234         client_sk = g_io_channel_unix_get_fd(channel);
3235
3236         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3237                 g_hash_table_remove(partial_tcp_req_table,
3238                                         GINT_TO_POINTER(client_sk));
3239
3240                 connman_error("Error with TCP client %d channel", client_sk);
3241                 return FALSE;
3242         }
3243
3244         switch (client->family) {
3245         case AF_INET:
3246                 client_addr = &client_addr4;
3247                 client_addr_len = &client_addr4_len;
3248                 break;
3249         case AF_INET6:
3250                 client_addr = &client_addr6;
3251                 client_addr_len = &client_addr6_len;
3252                 break;
3253         default:
3254                 g_hash_table_remove(partial_tcp_req_table,
3255                                         GINT_TO_POINTER(client_sk));
3256                 connman_error("client %p corrupted", client);
3257                 return FALSE;
3258         }
3259
3260         len = recvfrom(client_sk, client->buf + client->buf_end,
3261                         TCP_MAX_BUF_LEN - client->buf_end, 0,
3262                         client_addr, client_addr_len);
3263         if (len < 0) {
3264                 if (errno == EAGAIN || errno == EWOULDBLOCK)
3265                         return TRUE;
3266
3267                 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3268                         strerror(errno));
3269                 g_hash_table_remove(partial_tcp_req_table,
3270                                         GINT_TO_POINTER(client_sk));
3271                 return FALSE;
3272         }
3273
3274         return read_tcp_data(client, client_addr, *client_addr_len, len);
3275 }
3276
3277 static gboolean client_timeout(gpointer user_data)
3278 {
3279         struct tcp_partial_client_data *client = user_data;
3280         int sock;
3281
3282         sock = g_io_channel_unix_get_fd(client->channel);
3283
3284         debug("client %d timeout pending %d bytes", sock, client->buf_end);
3285
3286         g_hash_table_remove(partial_tcp_req_table, GINT_TO_POINTER(sock));
3287
3288         return FALSE;
3289 }
3290
3291 static bool tcp_listener_event(GIOChannel *channel, GIOCondition condition,
3292                                 struct listener_data *ifdata, int family,
3293                                 guint *listener_watch)
3294 {
3295         int sk, client_sk, len;
3296         unsigned int msg_len;
3297         struct tcp_partial_client_data *client;
3298         struct sockaddr_in6 client_addr6;
3299         socklen_t client_addr6_len = sizeof(client_addr6);
3300         struct sockaddr_in client_addr4;
3301         socklen_t client_addr4_len = sizeof(client_addr4);
3302         void *client_addr;
3303         socklen_t *client_addr_len;
3304         struct timeval tv;
3305         fd_set readfds;
3306
3307         debug("condition 0x%02x channel %p ifdata %p family %d",
3308                 condition, channel, ifdata, family);
3309
3310         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3311                 if (*listener_watch > 0)
3312                         g_source_remove(*listener_watch);
3313                 *listener_watch = 0;
3314
3315                 connman_error("Error with TCP listener channel");
3316
3317                 return false;
3318         }
3319
3320         sk = g_io_channel_unix_get_fd(channel);
3321
3322         if (family == AF_INET) {
3323                 client_addr = &client_addr4;
3324                 client_addr_len = &client_addr4_len;
3325         } else {
3326                 client_addr = &client_addr6;
3327                 client_addr_len = &client_addr6_len;
3328         }
3329
3330         tv.tv_sec = tv.tv_usec = 0;
3331         FD_ZERO(&readfds);
3332         FD_SET(sk, &readfds);
3333
3334         select(sk + 1, &readfds, NULL, NULL, &tv);
3335         if (FD_ISSET(sk, &readfds)) {
3336                 client_sk = accept(sk, client_addr, client_addr_len);
3337                 debug("client %d accepted", client_sk);
3338         } else {
3339                 debug("No data to read from master %d, waiting.", sk);
3340                 return true;
3341         }
3342
3343         if (client_sk < 0) {
3344                 connman_error("Accept failure on TCP listener");
3345                 *listener_watch = 0;
3346                 return false;
3347         }
3348
3349         fcntl(client_sk, F_SETFL, O_NONBLOCK);
3350
3351         client = g_hash_table_lookup(partial_tcp_req_table,
3352                                         GINT_TO_POINTER(client_sk));
3353         if (!client) {
3354                 client = g_try_new0(struct tcp_partial_client_data, 1);
3355                 if (!client) {
3356                         close(client_sk);
3357                         return false;
3358                 }
3359
3360                 g_hash_table_insert(partial_tcp_req_table,
3361                                         GINT_TO_POINTER(client_sk),
3362                                         client);
3363
3364                 client->channel = g_io_channel_unix_new(client_sk);
3365                 g_io_channel_set_close_on_unref(client->channel, TRUE);
3366
3367                 client->watch = g_io_add_watch(client->channel,
3368                                                 G_IO_IN, tcp_client_event,
3369                                                 (gpointer)client);
3370
3371                 client->ifdata = ifdata;
3372
3373                 debug("client %d created %p", client_sk, client);
3374         } else {
3375                 debug("client %d already exists %p", client_sk, client);
3376         }
3377
3378         if (!client->buf) {
3379                 client->buf = g_try_malloc(TCP_MAX_BUF_LEN);
3380                 if (!client->buf)
3381                         return false;
3382         }
3383         memset(client->buf, 0, TCP_MAX_BUF_LEN);
3384         client->buf_end = 0;
3385         client->family = family;
3386
3387         if (client->timeout == 0)
3388                 client->timeout = g_timeout_add_seconds(2, client_timeout,
3389                                                         client);
3390
3391         /*
3392          * Check how much data there is. If all is there, then we can
3393          * proceed normally, otherwise read the bits until everything
3394          * is received or timeout occurs.
3395          */
3396         len = recv(client_sk, client->buf, TCP_MAX_BUF_LEN, 0);
3397         if (len < 0) {
3398                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
3399                         debug("client %d no data to read, waiting", client_sk);
3400                         return true;
3401                 }
3402
3403                 debug("client %d cannot read errno %d/%s", client_sk, -errno,
3404                         strerror(errno));
3405                 g_hash_table_remove(partial_tcp_req_table,
3406                                         GINT_TO_POINTER(client_sk));
3407                 return true;
3408         }
3409
3410         if (len < 2) {
3411                 debug("client %d not enough data to read, waiting", client_sk);
3412                 client->buf_end += len;
3413                 return true;
3414         }
3415
3416         msg_len = get_msg_len(client->buf);
3417         if (msg_len > TCP_MAX_BUF_LEN) {
3418                 debug("client %d invalid message length %u ignoring packet",
3419                         client_sk, msg_len);
3420                 g_hash_table_remove(partial_tcp_req_table,
3421                                         GINT_TO_POINTER(client_sk));
3422                 return true;
3423         }
3424
3425         /*
3426          * The packet length bytes do not contain the total message length,
3427          * that is the reason to -2 below.
3428          */
3429         if (msg_len != (unsigned int)(len - 2)) {
3430                 debug("client %d sent %d bytes but expecting %u pending %d",
3431                         client_sk, len, msg_len + 2, msg_len + 2 - len);
3432
3433                 client->buf_end += len;
3434                 return true;
3435         }
3436
3437         return read_tcp_data(client, client_addr, *client_addr_len, len);
3438 }
3439
3440 static gboolean tcp4_listener_event(GIOChannel *channel, GIOCondition condition,
3441                                 gpointer user_data)
3442 {
3443         struct listener_data *ifdata = user_data;
3444
3445         return tcp_listener_event(channel, condition, ifdata, AF_INET,
3446                                 &ifdata->tcp4_listener_watch);
3447 }
3448
3449 static gboolean tcp6_listener_event(GIOChannel *channel, GIOCondition condition,
3450                                 gpointer user_data)
3451 {
3452         struct listener_data *ifdata = user_data;
3453
3454         return tcp_listener_event(channel, condition, user_data, AF_INET6,
3455                                 &ifdata->tcp6_listener_watch);
3456 }
3457
3458 static bool udp_listener_event(GIOChannel *channel, GIOCondition condition,
3459                                 struct listener_data *ifdata, int family,
3460                                 guint *listener_watch)
3461 {
3462         unsigned char buf[768];
3463         char query[512];
3464         struct request_data *req;
3465         struct sockaddr_in6 client_addr6;
3466         socklen_t client_addr6_len = sizeof(client_addr6);
3467         struct sockaddr_in client_addr4;
3468         socklen_t client_addr4_len = sizeof(client_addr4);
3469         void *client_addr;
3470         socklen_t *client_addr_len;
3471         int sk, err, len;
3472
3473         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
3474                 connman_error("Error with UDP listener channel");
3475                 *listener_watch = 0;
3476                 return false;
3477         }
3478
3479         sk = g_io_channel_unix_get_fd(channel);
3480
3481         if (family == AF_INET) {
3482                 client_addr = &client_addr4;
3483                 client_addr_len = &client_addr4_len;
3484         } else {
3485                 client_addr = &client_addr6;
3486                 client_addr_len = &client_addr6_len;
3487         }
3488
3489         memset(client_addr, 0, *client_addr_len);
3490         len = recvfrom(sk, buf, sizeof(buf), 0, client_addr, client_addr_len);
3491         if (len < 2)
3492                 return true;
3493
3494         debug("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
3495
3496         err = parse_request(buf, len, query, sizeof(query));
3497         if (err < 0 || (g_slist_length(server_list) == 0)) {
3498                 send_response(sk, buf, len, client_addr,
3499                                 *client_addr_len, IPPROTO_UDP);
3500                 return true;
3501         }
3502
3503         req = g_try_new0(struct request_data, 1);
3504         if (!req)
3505                 return true;
3506
3507         memcpy(&req->sa, client_addr, *client_addr_len);
3508         req->sa_len = *client_addr_len;
3509         req->client_sk = 0;
3510         req->protocol = IPPROTO_UDP;
3511         req->family = family;
3512
3513         req->srcid = buf[0] | (buf[1] << 8);
3514         req->dstid = get_id();
3515         req->altid = get_id();
3516         req->request_len = len;
3517
3518         buf[0] = req->dstid & 0xff;
3519         buf[1] = req->dstid >> 8;
3520
3521         req->numserv = 0;
3522         req->ifdata = ifdata;
3523         req->append_domain = false;
3524
3525         if (resolv(req, buf, query)) {
3526                 /* a cached result was sent, so the request can be released */
3527                 g_free(req);
3528                 return true;
3529         }
3530
3531         req->name = g_strdup(query);
3532         req->request = g_malloc(len);
3533         memcpy(req->request, buf, len);
3534         req->timeout = g_timeout_add_seconds(5, request_timeout, req);
3535         request_list = g_slist_append(request_list, req);
3536
3537         return true;
3538 }
3539
3540 static gboolean udp4_listener_event(GIOChannel *channel, GIOCondition condition,
3541                                 gpointer user_data)
3542 {
3543         struct listener_data *ifdata = user_data;
3544
3545         return udp_listener_event(channel, condition, ifdata, AF_INET,
3546                                 &ifdata->udp4_listener_watch);
3547 }
3548
3549 static gboolean udp6_listener_event(GIOChannel *channel, GIOCondition condition,
3550                                 gpointer user_data)
3551 {
3552         struct listener_data *ifdata = user_data;
3553
3554         return udp_listener_event(channel, condition, user_data, AF_INET6,
3555                                 &ifdata->udp6_listener_watch);
3556 }
3557
3558 static GIOChannel *get_listener(int family, int protocol, int index)
3559 {
3560         GIOChannel *channel;
3561         const char *proto;
3562         union {
3563                 struct sockaddr sa;
3564                 struct sockaddr_in6 sin6;
3565                 struct sockaddr_in sin;
3566         } s;
3567         socklen_t slen;
3568         int sk, type;
3569         char *interface;
3570
3571         debug("family %d protocol %d index %d", family, protocol, index);
3572
3573         switch (protocol) {
3574         case IPPROTO_UDP:
3575                 proto = "UDP";
3576                 type = SOCK_DGRAM | SOCK_CLOEXEC;
3577                 break;
3578
3579         case IPPROTO_TCP:
3580                 proto = "TCP";
3581                 type = SOCK_STREAM | SOCK_CLOEXEC;
3582                 break;
3583
3584         default:
3585                 return NULL;
3586         }
3587
3588         sk = socket(family, type, protocol);
3589         if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
3590                 connman_error("No IPv6 support");
3591                 return NULL;
3592         }
3593
3594         if (sk < 0) {
3595                 connman_error("Failed to create %s listener socket", proto);
3596                 return NULL;
3597         }
3598
3599         interface = connman_inet_ifname(index);
3600         if (!interface || setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
3601                                         interface,
3602                                         strlen(interface) + 1) < 0) {
3603                 connman_error("Failed to bind %s listener interface "
3604                         "for %s (%d/%s)",
3605                         proto, family == AF_INET ? "IPv4" : "IPv6",
3606                         -errno, strerror(errno));
3607                 close(sk);
3608                 g_free(interface);
3609                 return NULL;
3610         }
3611         g_free(interface);
3612
3613         if (family == AF_INET6) {
3614                 memset(&s.sin6, 0, sizeof(s.sin6));
3615                 s.sin6.sin6_family = AF_INET6;
3616                 s.sin6.sin6_port = htons(53);
3617                 slen = sizeof(s.sin6);
3618
3619                 if (__connman_inet_get_interface_address(index,
3620                                                 AF_INET6,
3621                                                 &s.sin6.sin6_addr) < 0) {
3622                         /* So we could not find suitable IPv6 address for
3623                          * the interface. This could happen if we have
3624                          * disabled IPv6 for the interface.
3625                          */
3626                         close(sk);
3627                         return NULL;
3628                 }
3629
3630         } else if (family == AF_INET) {
3631                 memset(&s.sin, 0, sizeof(s.sin));
3632                 s.sin.sin_family = AF_INET;
3633                 s.sin.sin_port = htons(53);
3634                 slen = sizeof(s.sin);
3635
3636                 if (__connman_inet_get_interface_address(index,
3637                                                 AF_INET,
3638                                                 &s.sin.sin_addr) < 0) {
3639                         close(sk);
3640                         return NULL;
3641                 }
3642         } else {
3643                 close(sk);
3644                 return NULL;
3645         }
3646
3647         if (bind(sk, &s.sa, slen) < 0) {
3648                 connman_error("Failed to bind %s listener socket", proto);
3649                 close(sk);
3650                 return NULL;
3651         }
3652
3653         if (protocol == IPPROTO_TCP) {
3654
3655                 if (listen(sk, 10) < 0) {
3656                         connman_error("Failed to listen on TCP socket %d/%s",
3657                                 -errno, strerror(errno));
3658                         close(sk);
3659                         return NULL;
3660                 }
3661
3662                 fcntl(sk, F_SETFL, O_NONBLOCK);
3663         }
3664
3665         channel = g_io_channel_unix_new(sk);
3666         if (!channel) {
3667                 connman_error("Failed to create %s listener channel", proto);
3668                 close(sk);
3669                 return NULL;
3670         }
3671
3672         g_io_channel_set_close_on_unref(channel, TRUE);
3673
3674         return channel;
3675 }
3676
3677 #define UDP_IPv4_FAILED 0x01
3678 #define TCP_IPv4_FAILED 0x02
3679 #define UDP_IPv6_FAILED 0x04
3680 #define TCP_IPv6_FAILED 0x08
3681 #define UDP_FAILED (UDP_IPv4_FAILED | UDP_IPv6_FAILED)
3682 #define TCP_FAILED (TCP_IPv4_FAILED | TCP_IPv6_FAILED)
3683 #define IPv6_FAILED (UDP_IPv6_FAILED | TCP_IPv6_FAILED)
3684 #define IPv4_FAILED (UDP_IPv4_FAILED | TCP_IPv4_FAILED)
3685
3686 static int create_dns_listener(int protocol, struct listener_data *ifdata)
3687 {
3688         int ret = 0;
3689
3690         if (protocol == IPPROTO_TCP) {
3691                 ifdata->tcp4_listener_channel = get_listener(AF_INET, protocol,
3692                                                         ifdata->index);
3693                 if (ifdata->tcp4_listener_channel)
3694                         ifdata->tcp4_listener_watch =
3695                                 g_io_add_watch(ifdata->tcp4_listener_channel,
3696                                         G_IO_IN, tcp4_listener_event,
3697                                         (gpointer)ifdata);
3698                 else
3699                         ret |= TCP_IPv4_FAILED;
3700
3701                 ifdata->tcp6_listener_channel = get_listener(AF_INET6, protocol,
3702                                                         ifdata->index);
3703                 if (ifdata->tcp6_listener_channel)
3704                         ifdata->tcp6_listener_watch =
3705                                 g_io_add_watch(ifdata->tcp6_listener_channel,
3706                                         G_IO_IN, tcp6_listener_event,
3707                                         (gpointer)ifdata);
3708                 else
3709                         ret |= TCP_IPv6_FAILED;
3710         } else {
3711                 ifdata->udp4_listener_channel = get_listener(AF_INET, protocol,
3712                                                         ifdata->index);
3713                 if (ifdata->udp4_listener_channel)
3714                         ifdata->udp4_listener_watch =
3715                                 g_io_add_watch(ifdata->udp4_listener_channel,
3716                                         G_IO_IN, udp4_listener_event,
3717                                         (gpointer)ifdata);
3718                 else
3719                         ret |= UDP_IPv4_FAILED;
3720
3721                 ifdata->udp6_listener_channel = get_listener(AF_INET6, protocol,
3722                                                         ifdata->index);
3723                 if (ifdata->udp6_listener_channel)
3724                         ifdata->udp6_listener_watch =
3725                                 g_io_add_watch(ifdata->udp6_listener_channel,
3726                                         G_IO_IN, udp6_listener_event,
3727                                         (gpointer)ifdata);
3728                 else
3729                         ret |= UDP_IPv6_FAILED;
3730         }
3731
3732         return ret;
3733 }
3734
3735 static void destroy_udp_listener(struct listener_data *ifdata)
3736 {
3737         DBG("index %d", ifdata->index);
3738
3739         if (ifdata->udp4_listener_watch > 0)
3740                 g_source_remove(ifdata->udp4_listener_watch);
3741
3742         if (ifdata->udp6_listener_watch > 0)
3743                 g_source_remove(ifdata->udp6_listener_watch);
3744
3745         if (ifdata->udp4_listener_channel)
3746                 g_io_channel_unref(ifdata->udp4_listener_channel);
3747         if (ifdata->udp6_listener_channel)
3748                 g_io_channel_unref(ifdata->udp6_listener_channel);
3749 }
3750
3751 static void destroy_tcp_listener(struct listener_data *ifdata)
3752 {
3753         DBG("index %d", ifdata->index);
3754
3755         if (ifdata->tcp4_listener_watch > 0)
3756                 g_source_remove(ifdata->tcp4_listener_watch);
3757         if (ifdata->tcp6_listener_watch > 0)
3758                 g_source_remove(ifdata->tcp6_listener_watch);
3759
3760         if (ifdata->tcp4_listener_channel)
3761                 g_io_channel_unref(ifdata->tcp4_listener_channel);
3762         if (ifdata->tcp6_listener_channel)
3763                 g_io_channel_unref(ifdata->tcp6_listener_channel);
3764 }
3765
3766 static int create_listener(struct listener_data *ifdata)
3767 {
3768         int err, index;
3769
3770         err = create_dns_listener(IPPROTO_UDP, ifdata);
3771         if ((err & UDP_FAILED) == UDP_FAILED)
3772                 return -EIO;
3773
3774         err |= create_dns_listener(IPPROTO_TCP, ifdata);
3775         if ((err & TCP_FAILED) == TCP_FAILED) {
3776                 destroy_udp_listener(ifdata);
3777                 return -EIO;
3778         }
3779
3780         index = connman_inet_ifindex("lo");
3781         if (ifdata->index == index) {
3782                 if ((err & IPv6_FAILED) != IPv6_FAILED)
3783                         __connman_resolvfile_append(index, NULL, "::1");
3784
3785                 if ((err & IPv4_FAILED) != IPv4_FAILED)
3786                         __connman_resolvfile_append(index, NULL, "127.0.0.1");
3787         }
3788
3789         return 0;
3790 }
3791
3792 static void destroy_listener(struct listener_data *ifdata)
3793 {
3794         int index;
3795         GSList *list;
3796
3797         index = connman_inet_ifindex("lo");
3798         if (ifdata->index == index) {
3799                 __connman_resolvfile_remove(index, NULL, "127.0.0.1");
3800                 __connman_resolvfile_remove(index, NULL, "::1");
3801         }
3802
3803         for (list = request_list; list; list = list->next) {
3804                 struct request_data *req = list->data;
3805
3806                 debug("Dropping request (id 0x%04x -> 0x%04x)",
3807                                                 req->srcid, req->dstid);
3808                 destroy_request_data(req);
3809                 list->data = NULL;
3810         }
3811
3812         g_slist_free(request_list);
3813         request_list = NULL;
3814
3815         destroy_tcp_listener(ifdata);
3816         destroy_udp_listener(ifdata);
3817 }
3818
3819 int __connman_dnsproxy_add_listener(int index)
3820 {
3821         struct listener_data *ifdata;
3822         int err;
3823
3824         DBG("index %d", index);
3825
3826         if (index < 0)
3827                 return -EINVAL;
3828
3829         if (!listener_table)
3830                 return -ENOENT;
3831
3832         if (g_hash_table_lookup(listener_table, GINT_TO_POINTER(index)))
3833                 return 0;
3834
3835         ifdata = g_try_new0(struct listener_data, 1);
3836         if (!ifdata)
3837                 return -ENOMEM;
3838
3839         ifdata->index = index;
3840         ifdata->udp4_listener_channel = NULL;
3841         ifdata->udp4_listener_watch = 0;
3842         ifdata->tcp4_listener_channel = NULL;
3843         ifdata->tcp4_listener_watch = 0;
3844         ifdata->udp6_listener_channel = NULL;
3845         ifdata->udp6_listener_watch = 0;
3846         ifdata->tcp6_listener_channel = NULL;
3847         ifdata->tcp6_listener_watch = 0;
3848
3849         err = create_listener(ifdata);
3850         if (err < 0) {
3851                 connman_error("Couldn't create listener for index %d err %d",
3852                                 index, err);
3853                 g_free(ifdata);
3854                 return err;
3855         }
3856         g_hash_table_insert(listener_table, GINT_TO_POINTER(ifdata->index),
3857                         ifdata);
3858         return 0;
3859 }
3860
3861 void __connman_dnsproxy_remove_listener(int index)
3862 {
3863         struct listener_data *ifdata;
3864
3865         DBG("index %d", index);
3866
3867         if (!listener_table)
3868                 return;
3869
3870         ifdata = g_hash_table_lookup(listener_table, GINT_TO_POINTER(index));
3871         if (!ifdata)
3872                 return;
3873
3874         destroy_listener(ifdata);
3875
3876         g_hash_table_remove(listener_table, GINT_TO_POINTER(index));
3877 }
3878
3879 static void remove_listener(gpointer key, gpointer value, gpointer user_data)
3880 {
3881         int index = GPOINTER_TO_INT(key);
3882         struct listener_data *ifdata = value;
3883
3884         DBG("index %d", index);
3885
3886         destroy_listener(ifdata);
3887 }
3888
3889 static void free_partial_reqs(gpointer value)
3890 {
3891         struct tcp_partial_client_data *data = value;
3892
3893         client_reset(data);
3894         g_free(data);
3895 }
3896
3897 int __connman_dnsproxy_init(void)
3898 {
3899         int err, index;
3900
3901         DBG("");
3902
3903         listener_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
3904                                                         NULL, g_free);
3905
3906         partial_tcp_req_table = g_hash_table_new_full(g_direct_hash,
3907                                                         g_direct_equal,
3908                                                         NULL,
3909                                                         free_partial_reqs);
3910
3911         index = connman_inet_ifindex("lo");
3912         err = __connman_dnsproxy_add_listener(index);
3913         if (err < 0)
3914                 return err;
3915
3916         err = connman_notifier_register(&dnsproxy_notifier);
3917         if (err < 0)
3918                 goto destroy;
3919
3920         return 0;
3921
3922 destroy:
3923         __connman_dnsproxy_remove_listener(index);
3924         g_hash_table_destroy(listener_table);
3925         g_hash_table_destroy(partial_tcp_req_table);
3926
3927         return err;
3928 }
3929
3930 void __connman_dnsproxy_cleanup(void)
3931 {
3932         DBG("");
3933
3934         if (cache_timer) {
3935                 g_source_remove(cache_timer);
3936                 cache_timer = 0;
3937         }
3938
3939         if (cache) {
3940                 g_hash_table_destroy(cache);
3941                 cache = NULL;
3942         }
3943
3944         connman_notifier_unregister(&dnsproxy_notifier);
3945
3946         g_hash_table_foreach(listener_table, remove_listener, NULL);
3947
3948         g_hash_table_destroy(listener_table);
3949
3950         g_hash_table_destroy(partial_tcp_req_table);
3951 }