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