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