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