Professional Documents
Culture Documents
Dnsproxy
Dnsproxy
*
* Connection Manager
*
* Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <resolv.h>
#include <gweb/gresolv.h>
#include <glib.h>
#include "connman.h"
struct partial_reply {
uint16_t len;
uint16_t received;
unsigned char buf[];
};
struct server_data {
int index;
GList *domains;
char *server;
struct sockaddr *server_addr;
socklen_t server_addr_len;
int protocol;
GIOChannel *channel;
guint watch;
guint timeout;
gboolean enabled;
gboolean connected;
struct partial_reply *incoming_reply;
};
struct request_data {
union {
struct sockaddr_in6 __sin6; /* Only for the length */
struct sockaddr sa;
};
socklen_t sa_len;
int client_sk;
int protocol;
int family;
guint16 srcid;
guint16 dstid;
guint16 altid;
guint timeout;
guint watch;
guint numserv;
guint numresp;
gpointer request;
gsize request_len;
gpointer name;
gpointer resp;
gsize resplen;
struct listener_data *ifdata;
gboolean append_domain;
};
struct listener_data {
int index;
GIOChannel *udp4_listener_channel;
GIOChannel *tcp4_listener_channel;
guint udp4_listener_watch;
guint tcp4_listener_watch;
GIOChannel *udp6_listener_channel;
GIOChannel *tcp6_listener_channel;
guint udp6_listener_watch;
guint tcp6_listener_watch;
};
/*
* The TCP client requires some extra handling as we need to
* be prepared to receive also partial DNS requests.
*/
struct tcp_partial_client_data {
int family;
struct listener_data *ifdata;
GIOChannel *channel;
guint watch;
unsigned char *buf;
unsigned int buf_end;
guint timeout;
};
struct cache_data {
time_t inserted;
time_t valid_until;
time_t cache_until;
int timeout;
uint16_t type;
uint16_t answers;
unsigned int data_len;
unsigned char *data; /* contains DNS header + body */
};
struct cache_entry {
char *key;
int want_refresh;
int hits;
struct cache_data *ipv4;
struct cache_data *ipv6;
};
struct domain_question {
uint16_t type;
uint16_t class;
} __attribute__ ((packed));
struct domain_rr {
uint16_t type;
uint16_t class;
uint32_t ttl;
uint16_t rdlen;
} __attribute__ ((packed));
/*
* Max length of the DNS TCP packet.
*/
#define TCP_MAX_BUF_LEN 4096
/*
* We limit how long the cached DNS entry stays in the cache.
* By default the TTL (time-to-live) of the DNS response is used
* when setting the cache entry life time. The value is in seconds.
*/
#define MAX_CACHE_TTL (60 * 30)
/*
* Also limit the other end, cache at least for 30 seconds.
*/
#define MIN_CACHE_TTL (30)
/*
* We limit the cache size to some sane value so that cached data does
* not occupy too much memory. Each cached entry occupies on average
* about 100 bytes memory (depending on DNS name length).
* Example: caching www.connman.net uses 97 bytes memory.
* The value is the max amount of cached DNS responses (count).
*/
#define MAX_CACHE_SIZE 256
case IPPROTO_TCP:
return 2;
default:
return -EINVAL;
}
}
/*
* There is a power and efficiency benefit to have entries
* in our cache expire at the same time. To this extend,
* we round down the cache valid time to common boundaries.
*/
static time_t round_down_ttl(time_t end_time, int ttl)
{
if (ttl < 15)
return end_time;
return NULL;
}
if (index < 0 ||
data->index < 0 || data->server == NULL)
continue;
return NULL;
}
/*
* Refresh a DNS entry, but also age the hit count a bit */
static void refresh_dns_entry(struct cache_entry *entry, char *name)
{
int age = 1;
if (ipv4_resolve == NULL) {
ipv4_resolve = g_resolv_new(0);
g_resolv_set_address_family(ipv4_resolve, AF_INET);
g_resolv_add_nameserver(ipv4_resolve, "127.0.0.1", 53, 0);
}
if (ipv6_resolve == NULL) {
ipv6_resolve = g_resolv_new(0);
g_resolv_set_address_family(ipv6_resolve, AF_INET6);
g_resolv_add_nameserver(ipv6_resolve, "::1", 53, 0);
}
if (entry->ipv4 == NULL) {
DBG("Refresing A record for %s", name);
g_resolv_lookup_hostname(ipv4_resolve, name,
dummy_resolve_func, NULL);
age = 4;
}
if (entry->ipv6 == NULL) {
DBG("Refresing AAAA record for %s", name);
g_resolv_lookup_hostname(ipv6_resolve, name,
dummy_resolve_func, NULL);
age = 4;
}
entry->hits -= age;
if (entry->hits < 0)
entry->hits = 0;
}
/*
* The cached packet contains always the TCP offset (two bytes)
* so skip them for UDP.
*/
switch (protocol) {
case IPPROTO_UDP:
ptr += 2;
len -= 2;
dns_len = len;
offset = 0;
break;
case IPPROTO_TCP:
offset = 2;
dns_len = ptr[0] * 256 + ptr[1];
break;
default:
return;
}
hdr->id = id;
hdr->qr = 1;
hdr->rcode = 0;
hdr->ancount = htons(answers);
hdr->nscount = 0;
hdr->arcount = 0;
hdr->qr = 1;
hdr->rcode = 2;
hdr->ancount = 0;
hdr->nscount = 0;
hdr->arcount = 0;
if (req->family == AF_INET)
channel = req->ifdata->udp4_listener_channel;
else
channel = req->ifdata->udp6_listener_channel;
if (channel == NULL)
return -1;
return g_io_channel_unix_get_fd(channel);
}
g_free(req->resp);
g_free(req->request);
g_free(req->name);
g_free(req);
}
if (req == NULL)
return FALSE;
DBG("id 0x%04x", req->srcid);
if (req->protocol == IPPROTO_UDP) {
sk = get_req_udp_socket(req);
if (sk < 0)
return FALSE;
if (req->protocol == IPPROTO_TCP) {
hdr = (void *) (req->request + 2);
hdr->id = req->srcid;
send_response(req->client_sk, req->request,
req->request_len, NULL, 0, IPPROTO_TCP);
sk = get_req_udp_socket(req);
if (sk >= 0)
send_response(sk, req->request,
req->request_len, &req->sa,
req->sa_len, IPPROTO_UDP);
}
}
/*
* We cannot leave TCP client hanging so just kick it out
* if we get a request timeout from server.
*/
if (req->protocol == IPPROTO_TCP) {
DBG("client %d removed", req->client_sk);
g_hash_table_remove(partial_tcp_req_table,
GINT_TO_POINTER(req->client_sk));
}
req->timeout = 0;
destroy_request_data(req);
return FALSE;
}
query = tmp + 1;
}
domain = tmp + 1;
}
*ptr++ = 0x00;
return TRUE;
}
/*
* remove stale cached entries so that they can be refreshed
*/
static void cache_enforce_validity(struct cache_entry *entry)
{
time_t current_time = time(NULL);
/*
* if we have a popular entry, we want a refresh instead of
* total destruction of the entry.
*/
if (entry->hits > 2)
want_refresh = 1;
cache_enforce_validity(entry);
switch (type) {
case 1: /* IPv4 */
if (cache_check_is_valid(entry->ipv4, current_time) == FALSE) {
DBG("cache %s \"%s\" type A", entry->ipv4 ?
"timeout" : "entry missing", question);
if (want_refresh)
entry->want_refresh = 1;
/*
* We do not remove cache entry if there is still
* valid IPv6 entry found in the cache.
*/
if (cache_check_is_valid(entry->ipv6, current_time)
== FALSE && want_refresh == FALSE) {
g_hash_table_remove(cache, question);
type = 0;
}
}
break;
if (want_refresh)
entry->want_refresh = 1;
if (cache_check_is_valid(entry->ipv4, current_time)
== FALSE && want_refresh == FALSE) {
g_hash_table_remove(cache, question);
type = 0;
}
}
break;
}
return type;
}
if (entry == NULL)
return;
if (entry->ipv4 != NULL) {
g_free(entry->ipv4->data);
g_free(entry->ipv4);
}
if (entry->ipv6 != NULL) {
g_free(entry->ipv6->data);
g_free(entry->ipv6);
}
g_free(entry->key);
g_free(entry);
if (--cache_size < 0)
cache_size = 0;
}
g_hash_table_destroy(cache);
cache = NULL;
}
return FALSE;
}
if (request == NULL)
return NULL;
proto_offset = protocol_offset(proto);
if (proto_offset < 0)
return NULL;
offset = strlen(question) + 1;
q = (void *) (question + offset);
type = ntohs(q->type);
if (cache == NULL) {
create_cache();
return NULL;
}
*qtype = type;
return entry;
}
/*
* Get a label/name from DNS resource record. The function decompresses the
* label if necessary. The function does not convert the name to presentation
* form. This means that the result string will contain label lengths instead
* of dots between labels. We intentionally do not want to convert to dotted
* format so that we can cache the wire format string directly.
*/
static int get_name(int counter,
unsigned char *pkt, unsigned char *start, unsigned char *max,
unsigned char *output, int output_max, int *output_len,
unsigned char **end, char *name, int *name_len)
{
unsigned char *p;
p = start;
while (*p) {
if ((*p & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
uint16_t offset = (*p & 0x3F) * 256 + *(p + 1);
if (*end == NULL)
*end = p + 2;
/*
* We need the original name in order to check
* if this answer is the correct one.
*/
name[(*name_len)++] = label_len;
memcpy(name + *name_len, p + 1, label_len + 1);
*name_len += label_len;
p += label_len + 1;
if (*end == NULL)
*end = p;
if (p >= max)
return -ENOBUFS;
}
}
return 0;
}
offset = output_len;
rr = (void *) (*end);
if (rr == NULL)
return -EINVAL;
*type = ntohs(rr->type);
*class = ntohs(rr->class);
*ttl = ntohl(rr->ttl);
*rdlen = ntohs(rr->rdlen);
if (*ttl < 0)
return -EINVAL;
*end += *rdlen;
return 0;
}
if (aliases != NULL) {
for (list = aliases; list; list = list->next) {
int len = strlen((char *)list->data);
if (strncmp((char *)list->data, name, len) == 0)
return TRUE;
}
}
return FALSE;
}
q = (void *) ptr;
qtype = ntohs(q->type);
qclass = ntohs(q->class);
ptr += 2 + 2; /* ptr points now to answers */
err = -ENOMSG;
*response_len = 0;
*answers = 0;
/*
* We have a bunch of answers (like A, AAAA, CNAME etc) to
* A or AAAA question. We traverse the answers and parse the
* resource records. Only A and AAAA records are cached, all
* the other records in answers are skipped.
*/
for (i = 0; i < ancount; i++) {
/*
* Get one address at a time to this buffer.
* The max size of the answer is
* 2 (pointer) + 2 (type) + 2 (class) +
* 4 (ttl) + 2 (rdlen) + addr (16 or 4) = 28
* for A or AAAA record.
* For CNAME the size can be bigger.
*/
unsigned char rsp[NS_MAXCDNAME];
unsigned int rsp_len = sizeof(rsp) - 1;
int ret, rdlen;
memset(rsp, 0, sizeof(rsp));
/*
* Now rsp contains compressed or uncompressed resource
* record. Next we check if this record answers the question.
* The name var contains the uncompressed label.
* One tricky bit is the CNAME records as they alias
* the name we might be interested in.
*/
/*
* Go to next answer if the class is not the one we are
* looking for.
*/
if (*class != qclass) {
ptr = next;
next = NULL;
continue;
}
/*
* Try to resolve aliases also, type is CNAME(5).
* This is important as otherwise the aliased names would not
* be cached at all as the cache would not contain the aliased
* question.
*
* If any CNAME is found in DNS packet, then we cache the alias
* IP address instead of the question (as the server
* said that question has only an alias).
* This means in practice that if e.g., ipv6.google.com is
* queried, DNS server returns CNAME of that name which is
* ipv6.l.google.com. We then cache the address of the CNAME
* but return the question name to client. So the alias
* status of the name is not saved in cache and thus not
* returned to the client. We do not return DNS packets from
* cache to client saying that ipv6.google.com is an alias to
* ipv6.l.google.com but we return instead a DNS packet that
* says ipv6.google.com has address xxx which is in fact the
* address of ipv6.l.google.com. For caching purposes this
* should not cause any issues.
*/
if (*type == 5 && strncmp(question, name, qlen) == 0) {
/*
* So now the alias answered the question. This is
* not very useful from caching point of view as
* the following A or AAAA records will not match the
* question. We need to find the real A/AAAA record
* of the alias and cache that.
*/
unsigned char *end = NULL;
int name_len = 0, output_len = 0;
memset(rsp, 0, sizeof(rsp));
rsp_len = sizeof(rsp) - 1;
/*
* Alias is in rdata part of the message,
* and next-rdlen points to it. So we need to get
* the real name of the alias.
*/
ret = get_name(0, buf, next - rdlen, buf + buflen,
rsp, rsp_len, &output_len, &end,
name, &name_len);
if (ret != 0) {
/* just ignore the error at this point */
ptr = next;
next = NULL;
continue;
}
/*
* We should now have the alias of the entry we might
* want to cache. Just remember it for a while.
* We check the alias list when we have parsed the
* A or AAAA record.
*/
aliases = g_slist_prepend(aliases, g_strdup(name));
ptr = next;
next = NULL;
continue;
}
if (*type == qtype) {
/*
* We found correct type (A or AAAA)
*/
if (check_alias(aliases, name) == TRUE ||
(aliases == NULL && strncmp(question, name,
qlen) == 0)) {
/*
* We found an alias or the name of the rr
* matches the question. If so, we append
* the compressed label to the cache.
* The end result is a response buffer that
* will contain one or more cached and
* compressed resource records.
*/
if (*response_len + rsp_len > maxlen) {
err = -ENOBUFS;
goto out;
}
memcpy(response + *response_len, rsp, rsp_len);
*response_len += rsp_len;
(*answers)++;
err = 0;
}
}
ptr = next;
next = NULL;
}
out:
for (list = aliases; list; list = list->next)
g_free(list->data);
g_slist_free(aliases);
return err;
}
struct cache_timeout {
time_t current_time;
int max_timeout;
int try_harder;
};
entry->hits /= 2;
/*
* If either IPv4 or IPv6 cached entry has expired, we
* remove both from the cache.
*/
/*
* if we're asked to try harder, also remove entries that have
* few hits
*/
if (data->try_harder && entry->hits < 4)
return TRUE;
return FALSE;
}
data.current_time = time(NULL);
data.max_timeout = 0;
data.try_harder = 0;
/*
* In the first pass, we only remove entries that have timed out.
* We use a cache of the first time to expire to do this only
* when it makes sense.
*/
if (max_timeout <= data.current_time) {
count = g_hash_table_foreach_remove(cache, cache_check_entry,
&data);
}
DBG("removed %d in the first pass", count);
/*
* In the second pass, if the first pass turned up blank,
* we also expire entries with a low hit count,
* while aging the hit count at the same time.
*/
data.try_harder = 1;
if (count == 0)
count = g_hash_table_foreach_remove(cache, cache_check_entry,
&data);
if (count == 0)
/*
* If we could not remove anything, then remember
* what is the max timeout and do nothing if we
* have not yet reached it. This will prevent
* constant traversal of the cache if it is full.
*/
max_timeout = data.max_timeout;
else
max_timeout = 0;
}
if (entry->ipv6) {
g_free(entry->ipv6->data);
g_free(entry->ipv6);
entry->ipv6 = NULL;
}
/*
* cache_invalidate is called from places where the DNS landscape
* has changed, say because connections are added or we entered a VPN.
* The logic is to wipe all cache data, but mark all non-expired
* parts of the cache for refresh rather than deleting the whole cache.
*/
static void cache_invalidate(void)
{
DBG("Invalidating the DNS cache %p", cache);
if (cache == NULL)
return;
cache_enforce_validity(entry);
if (entry->want_refresh) {
char *c;
char dns_name[NS_MAXDNAME + 1];
entry->want_refresh = 0;
cache_refresh_entry(entry);
}
if (len < 0)
return 0;
/* now the query, which is a name and 2 16 bit words */
l = dns_name_length(c) + 1;
c += l;
w = (uint16_t *) c;
type = ntohs(*w);
return type;
}
current_time = time(NULL);
if (offset < 0)
return 0;
rsplen = sizeof(response) - 1;
question[sizeof(question) - 1] = '\0';
/*
* special case: if we do a ipv6 lookup and get no result
* for a record that's already in our ipv4 cache.. we want
* to cache the negative response.
*/
if ((err == -ENOMSG || err == -ENOBUFS) &&
reply_query_type(msg + offset,
msg_len - offset) == 28) {
if (cache == NULL) {
create_cache();
entry = NULL;
} else
entry = g_hash_table_lookup(cache, question);
if (entry && entry->ipv4 && entry->ipv6 == NULL) {
int cache_offset = 0;
qlen = strlen(question);
/*
* If the cache contains already data, check if the
* type of the cached data is the same and do not add
* to cache if data is already there.
* This is needed so that we can cache both A and AAAA
* records for the same name.
*/
entry = g_hash_table_lookup(cache, question);
if (entry == NULL) {
entry = g_try_new(struct cache_entry, 1);
if (entry == NULL)
return -ENOMEM;
entry->key = g_strdup(question);
entry->ipv4 = entry->ipv6 = NULL;
entry->want_refresh = 0;
entry->hits = 0;
if (type == 1)
entry->ipv4 = data;
else
entry->ipv6 = data;
} else {
if (type == 1 && entry->ipv4 != NULL)
return 0;
if (type == 1)
entry->ipv4 = data;
else
entry->ipv6 = data;
/*
* compensate for the hit we'll get for serving
* the response out of the cache
*/
entry->hits--;
if (entry->hits < 0)
entry->hits = 0;
new_entry = FALSE;
}
data->inserted = current_time;
data->type = type;
data->answers = answers;
data->timeout = ttl;
/*
* The "2" in start of the length is the TCP offset. We allocate it
* here even for UDP packet because it simplifies the sending
* of cached packet.
*/
data->data_len = 2 + 12 + qlen + 1 + 2 + 2 + rsplen;
data->data = ptr = g_malloc(data->data_len);
data->valid_until = current_time + ttl;
/*
* Restrict the cached DNS record TTL to some sane value
* in order to prevent data staying in the cache too long.
*/
if (ttl > MAX_CACHE_TTL)
ttl = MAX_CACHE_TTL;
if (data->data == NULL) {
g_free(entry->key);
g_free(data);
g_free(entry);
return -ENOMEM;
}
/*
* We cache the two extra bytes at the start of the message
* in a TCP packet. When sending UDP packet, we skip the first
* two bytes. This way we do not need to know the format
* (UDP/TCP) of the cached message.
*/
if (srv->protocol == IPPROTO_UDP)
memcpy(ptr + 2, msg, offset + 12);
else
memcpy(ptr, msg, offset + 12);
if (new_entry == TRUE) {
g_hash_table_replace(cache, entry->key, entry);
cache_size++;
}
return 0;
}
static int ns_resolv(struct server_data *server, struct request_data *req,
gpointer request, gpointer name)
{
GList *list;
int sk, err, type = 0;
char *dot, *lookup = (char *) name;
struct cache_entry *entry;
if (data) {
ttl_left = data->valid_until - time(NULL);
entry->hits++;
}
send_cached_response(udp_sk, data->data,
data->data_len, &req->sa, req->sa_len,
IPPROTO_UDP, req->srcid, data->answers,
ttl_left);
return 1;
}
}
sk = g_io_channel_unix_get_fd(server->channel);
req->numserv++;
domain = list->data;
if (domain == NULL)
continue;
offset = protocol_offset(server->protocol);
if (offset < 0)
return offset;
domlen = strlen(domain) + 1;
if (domlen < 5)
return -EINVAL;
altlen += 12;
if (server->protocol == IPPROTO_TCP) {
int req_len = req->request_len + domlen - 2;
req->numserv++;
}
return 0;
}
if (offset < 0)
return offset;
req = find_request(dns_id);
if (req == NULL)
return -EINVAL;
req->numresp++;
/*
* If the domain name was append
* remove it before forwarding the reply.
*/
if (req->append_domain == TRUE) {
unsigned int domain_len = 0;
unsigned char *ptr;
uint8_t host_len;
unsigned int header_len;
/*
* ptr points to the first char of the hostname.
* ->hostname.domain.net
*/
header_len = offset + sizeof(struct domain_hdr);
ptr = reply + header_len;
host_len = *ptr;
if (host_len > 0)
domain_len = strnlen((const char *)ptr + 1 +
host_len,
reply_len - header_len);
/*
* Remove the domain name and replace it by the end
* of reply. Check if the domain is really there
* before trying to copy the data. The domain_len can
* be 0 because if the original query did not contain
* a domain name, then we are sending two packets,
* first without the domain name and the second packet
* with domain name. The append_domain is set to true
* even if we sent the first packet without domain
* name. In this case we end up in this branch.
*/
if (domain_len > 0) {
/*
* Note that we must use memmove() here,
* because the memory areas can overlap.
*/
memmove(ptr + host_len + 1,
ptr + host_len + domain_len + 1,
reply_len - header_len - domain_len);
g_free(req->resp);
req->resplen = 0;
req->resp = g_try_malloc(reply_len);
if (req->resp == NULL)
return -ENOMEM;
if (protocol == IPPROTO_UDP) {
sk = get_req_udp_socket(req);
err = sendto(sk, req->resp, req->resplen, 0,
&req->sa, req->sa_len);
} else {
sk = req->client_sk;
err = send(sk, req->resp, req->resplen, MSG_NOSIGNAL);
}
if (err < 0)
DBG("Cannot send msg, sk %d proto %d errno %d/%s", sk,
protocol, errno, strerror(errno));
else
DBG("proto %d sent %d bytes to %d", protocol, err, sk);
destroy_request_data(req);
return err;
}
static void server_destroy_socket(struct server_data *data)
{
DBG("index %d server %s proto %d", data->index,
data->server, data->protocol);
if (data->watch > 0) {
g_source_remove(data->watch);
data->watch = 0;
}
if (data->timeout > 0) {
g_source_remove(data->timeout);
data->timeout = 0;
}
if (data->channel != NULL) {
g_io_channel_shutdown(data->channel, TRUE, NULL);
g_io_channel_unref(data->channel);
data->channel = NULL;
}
g_free(data->incoming_reply);
data->incoming_reply = NULL;
}
g_free(server->server);
for (list = server->domains; list; list = list->next) {
char *domain = list->data;
/*
* We do not remove cache right away but delay it few seconds.
* The idea is that when IPv6 DNS server is added via RDNSS, it has a
* lifetime. When the lifetime expires we decrease the refcount so it
* is possible that the cache is then removed. Because a new DNS server
* is usually created almost immediately we would then loose the cache
* without any good reason. The small delay allows the new RDNSS to
* create a new DNS server instance and the refcount does not go to 0.
*/
g_timeout_add_seconds(3, try_remove_cache, NULL);
g_free(server);
}
sk = g_io_channel_unix_get_fd(channel);
return TRUE;
}
sk = g_io_channel_unix_get_fd(channel);
if (sk == 0)
return FALSE;
/*
* Discard any partial response which is buffered; better
* to get a proper response from a working server.
*/
g_free(server->incoming_reply);
server->incoming_reply = NULL;
if (req->protocol == IPPROTO_UDP)
continue;
if (req->request == NULL)
continue;
/*
* If we're not waiting for any further response
* from another name server, then we send an error
* response to the client.
*/
if (req->numserv && --(req->numserv))
continue;
destroy_server(server);
return FALSE;
}
server->domains = g_list_append(server->domains,
g_strdup(dom));
}
}
server->connected = TRUE;
server_list = g_slist_append(server_list, server);
if (server->timeout > 0) {
g_source_remove(server->timeout);
server->timeout = 0;
}
if (req->protocol == IPPROTO_UDP) {
list = list->next;
continue;
}
if (status < 0) {
list = list->next;
continue;
}
no_request_sent = FALSE;
if (req->timeout > 0)
g_source_remove(req->timeout);
req->timeout = g_timeout_add_seconds(30,
request_timeout, req);
list = list->next;
}
if (no_request_sent == TRUE) {
destroy_server(server);
return FALSE;
}
if (!reply) {
unsigned char reply_len_buf[2];
uint16_t reply_len;
reply->len = reply_len;
reply->received = 0;
server->incoming_reply = reply;
}
g_free(reply);
server->incoming_reply = NULL;
destroy_server(server);
return FALSE;
}
return TRUE;
}
DBG("");
if (server == NULL)
return FALSE;
destroy_server(server);
return FALSE;
}
static int server_create_socket(struct server_data *data)
{
int sk, err;
char *interface;
sk = socket(data->server_addr->sa_family,
data->protocol == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM,
data->protocol);
if (sk < 0) {
err = errno;
connman_error("Failed to create server %s socket",
data->server);
server_destroy_socket(data);
return -err;
}
interface = connman_inet_ifname(data->index);
if (interface != NULL) {
if (setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
interface,
strlen(interface) + 1) < 0) {
err = errno;
connman_error("Failed to bind server %s "
"to interface %s",
data->server, interface);
close(sk);
server_destroy_socket(data);
g_free(interface);
return -err;
}
g_free(interface);
}
data->channel = g_io_channel_unix_new(sk);
if (data->channel == NULL) {
connman_error("Failed to create server %s channel",
data->server);
close(sk);
server_destroy_socket(data);
return -ENOMEM;
}
g_io_channel_set_close_on_unref(data->channel, TRUE);
if (data->protocol == IPPROTO_TCP) {
g_io_channel_set_flags(data->channel, G_IO_FLAG_NONBLOCK, NULL);
data->watch = g_io_add_watch(data->channel,
G_IO_OUT | G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
tcp_server_event, data);
data->timeout = g_timeout_add_seconds(30, tcp_idle_timeout,
data);
} else
data->watch = g_io_add_watch(data->channel,
G_IO_IN | G_IO_NVAL | G_IO_ERR | G_IO_HUP,
udp_server_event, data);
create_cache();
return 0;
}
data->index = index;
if (domain)
data->domains = g_list_append(data->domains, g_strdup(domain));
data->server = g_strdup(server);
data->protocol = protocol;
memset(&hints, 0, sizeof(hints));
switch (protocol) {
case IPPROTO_UDP:
hints.ai_socktype = SOCK_DGRAM;
break;
case IPPROTO_TCP:
hints.ai_socktype = SOCK_STREAM;
break;
default:
destroy_server(data);
return NULL;
}
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST;
/* Do not blindly copy this code elsewhere; it doesn't loop over the
results using ->ai_next as it should. That's OK in *this* case
because it was a numeric lookup; we *know* there's only one. */
data->server_addr_len = rp->ai_addrlen;
switch (rp->ai_family) {
case AF_INET:
data->server_addr = (struct sockaddr *)
g_try_new0(struct sockaddr_in, 1);
break;
case AF_INET6:
data->server_addr = (struct sockaddr *)
g_try_new0(struct sockaddr_in6, 1);
break;
default:
connman_error("Wrong address family %d", rp->ai_family);
break;
}
if (data->server_addr == NULL) {
freeaddrinfo(rp);
destroy_server(data);
return NULL;
}
memcpy(data->server_addr, rp->ai_addr, rp->ai_addrlen);
freeaddrinfo(rp);
if (server_create_socket(data) != 0) {
destroy_server(data);
return NULL;
}
if (protocol == IPPROTO_UDP) {
/* Enable new servers by default */
data->enabled = TRUE;
DBG("Adding DNS server %s", data->server);
return data;
}
if (data->protocol == IPPROTO_TCP) {
DBG("server %s ignored proto TCP", data->server);
continue;
}
if (data->enabled == FALSE)
continue;
return FALSE;
}
if (domain == NULL)
return;
if (data->index < 0)
continue;
if (data->index != index)
continue;
if (g_str_equal(dom, domain)) {
dom_found = TRUE;
break;
}
}
if (dom_found == FALSE) {
data->domains =
g_list_append(data->domains, g_strdup(domain));
}
}
}
if (server == NULL) {
append_domain(index, domain);
return 0;
}
return 0;
}
destroy_server(data);
}
if (server == NULL)
return -EINVAL;
if (g_str_equal(server, "127.0.0.1") == TRUE)
return -ENODEV;
return 0;
}
void __connman_dnsproxy_flush(void)
{
GSList *list;
list = request_list;
while (list) {
struct request_data *req = list->data;
list = list->next;
if (req->timeout > 0)
g_source_remove(req->timeout);
req->timeout = g_timeout_add_seconds(5, request_timeout, req);
}
}
if (enabled == FALSE) {
DBG("Enabling DNS server %s", data->server);
data->enabled = TRUE;
cache_invalidate();
cache_refresh();
} else {
DBG("Disabling DNS server %s", data->server);
data->enabled = FALSE;
cache_invalidate();
}
}
}
if (service == NULL) {
/* When no services are active, then disable DNS proxying */
dnsproxy_offline_mode(TRUE);
return;
}
index = __connman_service_get_index(service);
if (index < 0)
return;
if (data->index == index) {
DBG("Enabling DNS server %s", data->server);
data->enabled = TRUE;
} else {
DBG("Disabling DNS server %s", data->server);
data->enabled = FALSE;
}
}
cache_refresh();
}
if (hdr->qr != 0 || qdcount != 1)
return -EINVAL;
name[0] = '\0';
if (label_len == 0x00) {
last_label = (char *) (ptr + 1);
break;
}
used += label_len + 1;
ptr += label_len + 1;
remain -= label_len + 1;
}
return 0;
}
static void client_reset(struct tcp_partial_client_data *client)
{
if (client == NULL)
return;
if (client->channel != NULL) {
DBG("client %d closing",
g_io_channel_unix_get_fd(client->channel));
g_io_channel_unref(client->channel);
client->channel = NULL;
}
if (client->watch > 0) {
g_source_remove(client->watch);
client->watch = 0;
}
if (client->timeout > 0) {
g_source_remove(client->timeout);
client->timeout = 0;
}
g_free(client->buf);
client->buf = NULL;
client->buf_end = 0;
}
client_sk = g_io_channel_unix_get_fd(client->channel);
if (read_len == 0) {
DBG("client %d closed, pending %d bytes",
client_sk, client->buf_end);
g_hash_table_remove(partial_tcp_req_table,
GINT_TO_POINTER(client_sk));
return FALSE;
}
client->buf_end += read_len;
if (client->buf_end < 2)
return TRUE;
msg_len = get_msg_len(client->buf);
if (msg_len > TCP_MAX_BUF_LEN) {
DBG("client %d sent too much data %d", client_sk, msg_len);
g_hash_table_remove(partial_tcp_req_table,
GINT_TO_POINTER(client_sk));
return FALSE;
}
read_another:
DBG("client %d msg len %d end %d past end %d", client_sk, msg_len,
client->buf_end, client->buf_end - (msg_len + 2));
req->numserv = 0;
req->ifdata = client->ifdata;
req->append_domain = FALSE;
/*
* Check if the answer is found in the cache before
* creating sockets to the server.
*/
entry = cache_check(client->buf, &qtype, IPPROTO_TCP);
if (entry != NULL) {
int ttl_left = 0;
struct cache_data *data;
if (data != NULL) {
ttl_left = data->valid_until - time(NULL);
entry->hits++;
send_cached_response(client_sk, data->data,
data->data_len, NULL, 0, IPPROTO_TCP,
req->srcid, data->answers, ttl_left);
g_free(req);
goto out;
} else
DBG("data missing, ignoring cache for this query");
}
if(create_server(data->index, NULL,
data->server, IPPROTO_TCP) == NULL)
continue;
waiting_for_connect = TRUE;
}
if (waiting_for_connect == FALSE) {
/* No server is waiting for connect */
send_response(client_sk, client->buf,
req->request_len, NULL, 0, IPPROTO_TCP);
g_free(req);
return TRUE;
}
/*
* The server is not connected yet.
* Copy the relevant buffers.
* The request will actually be sent once we're
* properly connected over TCP to the nameserver.
*/
req->request = g_try_malloc0(req->request_len);
if (req->request == NULL) {
send_response(client_sk, client->buf,
req->request_len, NULL, 0, IPPROTO_TCP);
g_free(req);
goto out;
}
memcpy(req->request, client->buf, req->request_len);
req->name = g_try_malloc0(sizeof(query));
if (req->name == NULL) {
send_response(client_sk, client->buf,
req->request_len, NULL, 0, IPPROTO_TCP);
g_free(req->request);
g_free(req);
goto out;
}
memcpy(req->name, query, sizeof(query));
out:
if (client->buf_end > (msg_len + 2)) {
DBG("client %d buf %p -> %p end %d len %d new %d",
client_sk,
client->buf + msg_len + 2,
client->buf, client->buf_end,
TCP_MAX_BUF_LEN - client->buf_end,
client->buf_end - (msg_len + 2));
memmove(client->buf, client->buf + msg_len + 2,
TCP_MAX_BUF_LEN - client->buf_end);
client->buf_end = client->buf_end - (msg_len + 2);
/*
* If we have a full message waiting, just read it
* immediately.
*/
msg_len = get_msg_len(client->buf);
if ((msg_len + 2) == client->buf_end) {
DBG("client %d reading another %d bytes", client_sk,
msg_len + 2);
goto read_another;
}
} else {
DBG("client %d clearing reading buffer", client_sk);
client->buf_end = 0;
memset(client->buf, 0, TCP_MAX_BUF_LEN);
/*
* We received all the packets from client so we must also
* remove the timeout handler here otherwise we might get
* timeout while waiting the results from server.
*/
g_source_remove(client->timeout);
client->timeout = 0;
}
return TRUE;
}
client_sk = g_io_channel_unix_get_fd(channel);
switch (client->family) {
case AF_INET:
client_addr = &client_addr4;
client_addr_len = &client_addr4_len;
break;
case AF_INET6:
client_addr = &client_addr6;
client_addr_len = &client_addr6_len;
break;
default:
g_hash_table_remove(partial_tcp_req_table,
GINT_TO_POINTER(client_sk));
connman_error("client %p corrupted", client);
return FALSE;
}
sock = g_io_channel_unix_get_fd(client->channel);
DBG("client %d timeout pending %d bytes", sock, client->buf_end);
g_hash_table_remove(partial_tcp_req_table, GINT_TO_POINTER(sock));
return FALSE;
}
return FALSE;
}
sk = g_io_channel_unix_get_fd(channel);
if (family == AF_INET) {
client_addr = &client_addr4;
client_addr_len = &client_addr4_len;
} else {
client_addr = &client_addr6;
client_addr_len = &client_addr6_len;
}
tv.tv_sec = tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(sk, &readfds);
client = g_hash_table_lookup(partial_tcp_req_table,
GINT_TO_POINTER(client_sk));
if (client == NULL) {
client = g_try_new0(struct tcp_partial_client_data, 1);
if (client == NULL)
return FALSE;
g_hash_table_insert(partial_tcp_req_table,
GINT_TO_POINTER(client_sk),
client);
client->channel = g_io_channel_unix_new(client_sk);
g_io_channel_set_close_on_unref(client->channel, TRUE);
client->watch = g_io_add_watch(client->channel,
G_IO_IN, tcp_client_event,
(gpointer)client);
client->ifdata = ifdata;
if (client->buf == NULL) {
client->buf = g_try_malloc(TCP_MAX_BUF_LEN);
if (client->buf == NULL)
return FALSE;
}
memset(client->buf, 0, TCP_MAX_BUF_LEN);
client->buf_end = 0;
client->family = family;
if (client->timeout == 0)
client->timeout = g_timeout_add_seconds(2, client_timeout,
client);
/*
* Check how much data there is. If all is there, then we can
* proceed normally, otherwise read the bits until everything
* is received or timeout occurs.
*/
len = recv(client_sk, client->buf, TCP_MAX_BUF_LEN, 0);
if (len < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
DBG("client %d no data to read, waiting", client_sk);
return TRUE;
}
if (len < 2) {
DBG("client %d not enough data to read, waiting", client_sk);
client->buf_end += len;
return TRUE;
}
msg_len = get_msg_len(client->buf);
if (msg_len > TCP_MAX_BUF_LEN) {
DBG("client %d invalid message length %u ignoring packet",
client_sk, msg_len);
g_hash_table_remove(partial_tcp_req_table,
GINT_TO_POINTER(client_sk));
return TRUE;
}
/*
* The packet length bytes do not contain the total message length,
* that is the reason to -2 below.
*/
if (msg_len != (unsigned int)(len - 2)) {
DBG("client %d sent %d bytes but expecting %u pending %d",
client_sk, len, msg_len + 2, msg_len + 2 - len);
client->buf_end += len;
return TRUE;
}
sk = g_io_channel_unix_get_fd(channel);
if (family == AF_INET) {
client_addr = &client_addr4;
client_addr_len = &client_addr4_len;
} else {
client_addr = &client_addr6;
client_addr_len = &client_addr6_len;
}
memset(client_addr, 0, *client_addr_len);
len = recvfrom(sk, buf, sizeof(buf), 0, client_addr, client_addr_len);
if (len < 2)
return TRUE;
req->numserv = 0;
req->ifdata = ifdata;
req->append_domain = FALSE;
return TRUE;
}
switch (protocol) {
case IPPROTO_UDP:
proto = "UDP";
type = SOCK_DGRAM | SOCK_CLOEXEC;
break;
case IPPROTO_TCP:
proto = "TCP";
type = SOCK_STREAM | SOCK_CLOEXEC;
break;
default:
return NULL;
}
if (sk < 0) {
connman_error("Failed to create %s listener socket", proto);
return NULL;
}
interface = connman_inet_ifname(index);
if (interface == NULL || setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
interface,
strlen(interface) + 1) < 0) {
connman_error("Failed to bind %s listener interface "
"for %s (%d/%s)",
proto, family == AF_INET ? "IPv4" : "IPv6",
-errno, strerror(errno));
close(sk);
g_free(interface);
return NULL;
}
g_free(interface);
if (family == AF_INET6) {
memset(&s.sin6, 0, sizeof(s.sin6));
s.sin6.sin6_family = AF_INET6;
s.sin6.sin6_port = htons(53);
slen = sizeof(s.sin6);
if (__connman_inet_get_interface_address(index,
AF_INET6,
&s.sin6.sin6_addr) < 0) {
/* So we could not find suitable IPv6 address for
* the interface. This could happen if we have
* disabled IPv6 for the interface.
*/
close(sk);
return NULL;
}
if (__connman_inet_get_interface_address(index,
AF_INET,
&s.sin.sin_addr) < 0) {
close(sk);
return NULL;
}
} else {
close(sk);
return NULL;
}
if (protocol == IPPROTO_TCP) {
channel = g_io_channel_unix_new(sk);
if (channel == NULL) {
connman_error("Failed to create %s listener channel", proto);
close(sk);
return NULL;
}
g_io_channel_set_close_on_unref(channel, TRUE);
return channel;
}
if (protocol == IPPROTO_TCP) {
ifdata->tcp4_listener_channel = get_listener(AF_INET, protocol,
ifdata->index);
if (ifdata->tcp4_listener_channel != NULL)
ifdata->tcp4_listener_watch =
g_io_add_watch(ifdata->tcp4_listener_channel,
G_IO_IN, tcp4_listener_event,
(gpointer)ifdata);
else
ret |= TCP_IPv4_FAILED;
return ret;
}
if (ifdata->udp4_listener_watch > 0)
g_source_remove(ifdata->udp4_listener_watch);
if (ifdata->udp6_listener_watch > 0)
g_source_remove(ifdata->udp6_listener_watch);
if (ifdata->udp4_listener_channel != NULL)
g_io_channel_unref(ifdata->udp4_listener_channel);
if (ifdata->udp6_listener_channel != NULL)
g_io_channel_unref(ifdata->udp6_listener_channel);
}
if (ifdata->tcp4_listener_watch > 0)
g_source_remove(ifdata->tcp4_listener_watch);
if (ifdata->tcp6_listener_watch > 0)
g_source_remove(ifdata->tcp6_listener_watch);
if (ifdata->tcp4_listener_channel != NULL)
g_io_channel_unref(ifdata->tcp4_listener_channel);
if (ifdata->tcp6_listener_channel != NULL)
g_io_channel_unref(ifdata->tcp6_listener_channel);
}
index = connman_inet_ifindex("lo");
if (ifdata->index == index) {
if ((err & IPv6_FAILED) != IPv6_FAILED)
__connman_resolvfile_append(index, NULL, "::1");
return 0;
}
index = connman_inet_ifindex("lo");
if (ifdata->index == index) {
__connman_resolvfile_remove(index, NULL, "127.0.0.1");
__connman_resolvfile_remove(index, NULL, "::1");
}
g_slist_free(request_list);
request_list = NULL;
destroy_tcp_listener(ifdata);
destroy_udp_listener(ifdata);
}
if (index < 0)
return -EINVAL;
if (listener_table == NULL)
return -ENOENT;
ifdata->index = index;
ifdata->udp4_listener_channel = NULL;
ifdata->udp4_listener_watch = 0;
ifdata->tcp4_listener_channel = NULL;
ifdata->tcp4_listener_watch = 0;
ifdata->udp6_listener_channel = NULL;
ifdata->udp6_listener_watch = 0;
ifdata->tcp6_listener_channel = NULL;
ifdata->tcp6_listener_watch = 0;
err = create_listener(ifdata);
if (err < 0) {
connman_error("Couldn't create listener for index %d err %d",
index, err);
g_free(ifdata);
return err;
}
g_hash_table_insert(listener_table, GINT_TO_POINTER(ifdata->index),
ifdata);
return 0;
}
if (listener_table == NULL)
return;
destroy_listener(ifdata);
g_hash_table_remove(listener_table, GINT_TO_POINTER(index));
}
static void remove_listener(gpointer key, gpointer value, gpointer user_data)
{
int index = GPOINTER_TO_INT(key);
struct listener_data *ifdata = value;
destroy_listener(ifdata);
}
client_reset(data);
g_free(data);
}
int __connman_dnsproxy_init(void)
{
int err, index;
DBG("");
srandom(time(NULL));
partial_tcp_req_table = g_hash_table_new_full(g_direct_hash,
g_direct_equal,
NULL,
free_partial_reqs);
index = connman_inet_ifindex("lo");
err = __connman_dnsproxy_add_listener(index);
if (err < 0)
return err;
err = connman_notifier_register(&dnsproxy_notifier);
if (err < 0)
goto destroy;
return 0;
destroy:
__connman_dnsproxy_remove_listener(index);
g_hash_table_destroy(listener_table);
g_hash_table_destroy(partial_tcp_req_table);
return err;
}
void __connman_dnsproxy_cleanup(void)
{
DBG("");
connman_notifier_unregister(&dnsproxy_notifier);
g_hash_table_foreach(listener_table, remove_listener, NULL);
g_hash_table_destroy(listener_table);
g_hash_table_destroy(partial_tcp_req_table);
}