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