Updated connman to version 1.35
[platform/upstream/connman.git] / src / dnsproxy.c
old mode 100644 (file)
new mode 100755 (executable)
index 40b4f15..3fa7bf4
 
 #include "connman.h"
 
+#if defined TIZEN_EXT
+#include <sys/smack.h>
+#include <systemd/sd-daemon.h>
+#endif
+
 #define debug(fmt...) do { } while (0)
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
@@ -194,7 +199,11 @@ struct domain_rr {
  * 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.
  */
+#if defined TIZEN_EXT
+#define MAX_CACHE_TTL (60 * 60)
+#else
 #define MAX_CACHE_TTL (60 * 30)
+#endif
 /*
  * Also limit the other end, cache at least for 30 seconds.
  */
@@ -213,12 +222,22 @@ static int cache_size;
 static GHashTable *cache;
 static int cache_refcount;
 static GSList *server_list = NULL;
+#if defined TIZEN_EXT
+static GSList *server_list_sec = NULL;
+#endif
 static GSList *request_list = NULL;
 static GHashTable *listener_table = NULL;
 static time_t next_refresh;
 static GHashTable *partial_tcp_req_table;
 static guint cache_timer = 0;
 
+#if defined TIZEN_EXT
+static void destroy_server_sec(struct server_data *server);
+static struct server_data *create_server_sec(int index,
+               const char *domain, const char *server,
+               int protocol);
+#endif
+
 static guint16 get_id(void)
 {
        uint64_t rand;
@@ -1642,6 +1661,31 @@ static int ns_resolv(struct server_data *server, struct request_data *req,
                }
        }
 
+#if defined TIZEN_EXT
+       if (server->protocol == IPPROTO_UDP) {
+               GList *domains;
+               struct server_data *new_server = NULL;
+
+               new_server = create_server_sec(server->index, NULL,
+                                               server->server, IPPROTO_UDP);
+
+               if (new_server != NULL) {
+                       for (domains = server->domains; domains;
+                                               domains = domains->next) {
+                               char *dom = domains->data;
+
+                               DBG("Adding domain %s to %s",
+                                               dom, new_server->server);
+
+                               new_server->domains = g_list_append(
+                                               new_server->domains,
+                                                       g_strdup(dom));
+                       }
+
+                       server = new_server;
+               }
+       }
+#endif
        sk = g_io_channel_unix_get_fd(server->channel);
 
        err = sendto(sk, request, req->request_len, MSG_NOSIGNAL,
@@ -2240,6 +2284,19 @@ static gboolean udp_server_event(GIOChannel *channel, GIOCondition condition,
        if (err < 0)
                return TRUE;
 
+#if defined TIZEN_EXT
+       GSList *list;
+
+       for (list = server_list_sec; list; list = list->next) {
+               struct server_data *new_data = list->data;
+
+               if (new_data == data) {
+                       destroy_server_sec(data);
+                       return TRUE;
+               }
+       }
+#endif
+
        return TRUE;
 }
 
@@ -2550,6 +2607,177 @@ static void enable_fallback(bool enable)
        }
 }
 
+#if defined TIZEN_EXT
+
+static void destroy_server_sec(struct server_data *server)
+{
+       GList *list;
+       int fd;
+
+       if (server->channel)
+               fd = g_io_channel_unix_get_fd(server->channel);
+       else
+               fd = -1;
+
+       DBG("index %d server %s sock %d", server->index, server->server, fd);
+
+       server_list_sec = g_slist_remove(server_list_sec, server);
+
+       if (fd > 0)
+               close(fd);
+
+       server_destroy_socket(server);
+
+       if (server->protocol == IPPROTO_UDP && server->enabled)
+               DBG("Removing DNS server %s", server->server);
+
+       g_free(server->server);
+       for (list = server->domains; list; list = list->next) {
+               char *domain = list->data;
+
+               server->domains = g_list_remove(server->domains, domain);
+               g_free(domain);
+       }
+       g_free(server->server_addr);
+
+       /*
+        * 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.
+        */
+       /* TODO: Need to check this */
+       /* g_timeout_add_seconds(3, try_remove_cache, NULL); */
+
+       g_free(server);
+}
+
+static void destroy_all_server_sec()
+{
+       GSList *list;
+
+       DBG("remove all dns server");
+
+       for (list = server_list_sec; list; list = list->next) {
+               struct server_data *server = list->data;
+               destroy_server_sec(server);
+       }
+       server_list_sec = NULL;
+}
+
+static gboolean sec_udp_idle_timeout(gpointer user_data)
+{
+       struct server_data *server = user_data;
+
+       DBG("");
+
+       if (server == NULL)
+               return FALSE;
+
+       destroy_server_sec(server);
+
+       return FALSE;
+}
+
+static struct server_data *create_server_sec(int index,
+                                       const char *domain, const char *server,
+                                       int protocol)
+{
+       struct server_data *data;
+       struct addrinfo hints, *rp;
+       int ret;
+
+       DBG("index %d server %s", index, server);
+
+       data = g_try_new0(struct server_data, 1);
+       if (data == NULL) {
+               connman_error("Failed to allocate server %s data", server);
+               return NULL;
+       }
+
+       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_sec(data);
+               return NULL;
+       }
+       hints.ai_family = AF_UNSPEC;
+       hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST;
+
+       ret = getaddrinfo(data->server, "53", &hints, &rp);
+       if (ret) {
+               connman_error("Failed to parse server %s address: %s\n",
+                               data->server, gai_strerror(ret));
+               freeaddrinfo(rp);
+               destroy_server_sec(data);
+               return NULL;
+       }
+
+       /* 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_sec(data);
+               return NULL;
+       }
+       memcpy(data->server_addr, rp->ai_addr, rp->ai_addrlen);
+       freeaddrinfo(rp);
+
+       if (server_create_socket(data) != 0) {
+               destroy_server_sec(data);
+               return NULL;
+       }
+
+       if (protocol == IPPROTO_UDP) {
+               /* Enable new servers by default */
+               data->enabled = TRUE;
+               DBG("Adding DNS server %s", data->server);
+
+               data->timeout = g_timeout_add_seconds(30, sec_udp_idle_timeout,
+                                                               data);
+
+               server_list_sec = g_slist_append(server_list_sec, data);
+       }
+
+       return data;
+}
+#endif
+
 static struct server_data *create_server(int index,
                                        const char *domain, const char *server,
                                        int protocol)
@@ -2840,6 +3068,10 @@ int __connman_dnsproxy_remove(int index, const char *domain,
        remove_server(index, domain, server, IPPROTO_UDP);
        remove_server(index, domain, server, IPPROTO_TCP);
 
+#if defined TIZEN_EXT
+       destroy_all_server_sec();
+#endif
+
        return 0;
 }
 
@@ -3288,6 +3520,23 @@ static gboolean client_timeout(gpointer user_data)
        return FALSE;
 }
 
+#if defined TIZEN_EXT
+static void recover_listener(GIOChannel *channel, struct listener_data *ifdata)
+{
+       int sk, index;
+
+       index = ifdata->index;
+
+       sk = g_io_channel_unix_get_fd(channel);
+       close(sk);
+
+       __connman_dnsproxy_remove_listener(index);
+
+       if (__connman_dnsproxy_add_listener(index) == 0)
+               DBG("listener %d successfully recovered", index);
+}
+#endif
+
 static bool tcp_listener_event(GIOChannel *channel, GIOCondition condition,
                                struct listener_data *ifdata, int family,
                                guint *listener_watch)
@@ -3308,11 +3557,17 @@ static bool tcp_listener_event(GIOChannel *channel, GIOCondition condition,
                condition, channel, ifdata, family);
 
        if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
+#if defined TIZEN_EXT
+               connman_error("Error %d with TCP listener channel", condition);
+
+               recover_listener(channel, ifdata);
+#else
                if (*listener_watch > 0)
                        g_source_remove(*listener_watch);
                *listener_watch = 0;
 
                connman_error("Error with TCP listener channel");
+#endif
 
                return false;
        }
@@ -3426,7 +3681,11 @@ static bool tcp_listener_event(GIOChannel *channel, GIOCondition condition,
         * The packet length bytes do not contain the total message length,
         * that is the reason to -2 below.
         */
+#if defined TIZEN_EXT
+       if (msg_len > (unsigned int)(len - 2)) {
+#else
        if (msg_len != (unsigned int)(len - 2)) {
+#endif
                debug("client %d sent %d bytes but expecting %u pending %d",
                        client_sk, len, msg_len + 2, msg_len + 2 - len);
 
@@ -3471,8 +3730,14 @@ static bool udp_listener_event(GIOChannel *channel, GIOCondition condition,
        int sk, err, len;
 
        if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
+#if defined TIZEN_EXT
+               connman_error("Error %d with UDP listener channel", condition);
+
+               recover_listener(channel, ifdata);
+#else
                connman_error("Error with UDP listener channel");
                *listener_watch = 0;
+#endif
                return false;
        }
 
@@ -3531,7 +3796,12 @@ static bool udp_listener_event(GIOChannel *channel, GIOCondition condition,
        req->name = g_strdup(query);
        req->request = g_malloc(len);
        memcpy(req->request, buf, len);
+#if defined TIZEN_EXT
+       DBG("req %p dstid 0x%04x altid 0x%04x", req, req->dstid, req->altid);
+       req->timeout = g_timeout_add_seconds(30, request_timeout, req);
+#else
        req->timeout = g_timeout_add_seconds(5, request_timeout, req);
+#endif
        request_list = g_slist_append(request_list, req);
 
        return true;
@@ -3559,14 +3829,24 @@ static GIOChannel *get_listener(int family, int protocol, int index)
 {
        GIOChannel *channel;
        const char *proto;
+#if !defined TIZEN_EXT
        union {
                struct sockaddr sa;
                struct sockaddr_in6 sin6;
                struct sockaddr_in sin;
        } s;
        socklen_t slen;
+#endif
        int sk, type;
+#if !defined TIZEN_EXT
        char *interface;
+#endif
+#if defined TIZEN_EXT
+       int option;
+       int sd_num = 0;
+       int rv;
+       int is_socket_inet = 0;
+#endif
 
        debug("family %d protocol %d index %d", family, protocol, index);
 
@@ -3584,7 +3864,33 @@ static GIOChannel *get_listener(int family, int protocol, int index)
        default:
                return NULL;
        }
+#if defined TIZEN_EXT
+       sd_num = sd_listen_fds(0);
+       DBG("socket type(%s) systemd number of fds(%d)", proto, sd_num);
+       if(sd_num < 1){
+               DBG("fail to get the fd from systemd");
+               return NULL;
+       }
 
+       if(protocol == IPPROTO_TCP)
+               type = SOCK_STREAM;
+       else
+               type = SOCK_DGRAM;
+
+       for(sk = SD_LISTEN_FDS_START; sk < SD_LISTEN_FDS_START+sd_num; ++sk){
+               rv = sd_is_socket_inet(sk, family, type, -1, 53);
+               if(rv > 0){
+                       DBG("socket fd (%d) is passed by systemd", sk);
+                       is_socket_inet = 1;
+                       break;
+               }
+       }
+
+       if (!is_socket_inet) {
+               DBG("socket fd is not matched what connman requests");
+               return NULL;
+       }
+#else
        sk = socket(family, type, protocol);
        if (sk < 0 && family == AF_INET6 && errno == EAFNOSUPPORT) {
                connman_error("No IPv6 support");
@@ -3596,6 +3902,9 @@ static GIOChannel *get_listener(int family, int protocol, int index)
                return NULL;
        }
 
+       /* ConnMan listens DNS from multiple interfaces
+        * E.g. various technology based and tethering interfaces
+        */
        interface = connman_inet_ifname(index);
        if (!interface || setsockopt(sk, SOL_SOCKET, SO_BINDTODEVICE,
                                        interface,
@@ -3632,7 +3941,6 @@ static GIOChannel *get_listener(int family, int protocol, int index)
                s.sin.sin_family = AF_INET;
                s.sin.sin_port = htons(53);
                slen = sizeof(s.sin);
-
                if (__connman_inet_get_interface_address(index,
                                                AF_INET,
                                                &s.sin.sin_addr) < 0) {
@@ -3643,22 +3951,30 @@ static GIOChannel *get_listener(int family, int protocol, int index)
                close(sk);
                return NULL;
        }
-
+#endif
+#if defined TIZEN_EXT
+       /* When ConnMan crashed,
+        * probably DNS listener cannot bind existing address */
+       option = 1;
+       setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
+#endif
+#if !defined TIZEN_EXT
        if (bind(sk, &s.sa, slen) < 0) {
                connman_error("Failed to bind %s listener socket", proto);
                close(sk);
                return NULL;
        }
+#endif
 
        if (protocol == IPPROTO_TCP) {
-
+#if !defined TIZEN_EXT
                if (listen(sk, 10) < 0) {
                        connman_error("Failed to listen on TCP socket %d/%s",
                                -errno, strerror(errno));
                        close(sk);
                        return NULL;
                }
-
+#endif
                fcntl(sk, F_SETFL, O_NONBLOCK);
        }
 
@@ -3691,40 +4007,68 @@ static int create_dns_listener(int protocol, struct listener_data *ifdata)
                ifdata->tcp4_listener_channel = get_listener(AF_INET, protocol,
                                                        ifdata->index);
                if (ifdata->tcp4_listener_channel)
+#if defined TIZEN_EXT
+                       ifdata->tcp4_listener_watch =
+                               g_io_add_watch(ifdata->tcp4_listener_channel,
+                                       G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+                                       tcp4_listener_event, (gpointer)ifdata);
+#else
                        ifdata->tcp4_listener_watch =
                                g_io_add_watch(ifdata->tcp4_listener_channel,
                                        G_IO_IN, tcp4_listener_event,
                                        (gpointer)ifdata);
+#endif
                else
                        ret |= TCP_IPv4_FAILED;
 
                ifdata->tcp6_listener_channel = get_listener(AF_INET6, protocol,
                                                        ifdata->index);
                if (ifdata->tcp6_listener_channel)
+#if defined TIZEN_EXT
+                       ifdata->tcp6_listener_watch =
+                               g_io_add_watch(ifdata->tcp6_listener_channel,
+                                       G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+                                       tcp6_listener_event, (gpointer)ifdata);
+#else
                        ifdata->tcp6_listener_watch =
                                g_io_add_watch(ifdata->tcp6_listener_channel,
                                        G_IO_IN, tcp6_listener_event,
                                        (gpointer)ifdata);
+#endif
                else
                        ret |= TCP_IPv6_FAILED;
        } else {
                ifdata->udp4_listener_channel = get_listener(AF_INET, protocol,
                                                        ifdata->index);
                if (ifdata->udp4_listener_channel)
+#if defined TIZEN_EXT
+                       ifdata->udp4_listener_watch =
+                               g_io_add_watch(ifdata->udp4_listener_channel,
+                                       G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+                                       udp4_listener_event, (gpointer)ifdata);
+#else
                        ifdata->udp4_listener_watch =
                                g_io_add_watch(ifdata->udp4_listener_channel,
                                        G_IO_IN, udp4_listener_event,
                                        (gpointer)ifdata);
+#endif
                else
                        ret |= UDP_IPv4_FAILED;
 
                ifdata->udp6_listener_channel = get_listener(AF_INET6, protocol,
                                                        ifdata->index);
                if (ifdata->udp6_listener_channel)
+#if defined TIZEN_EXT
+                       ifdata->udp6_listener_watch =
+                               g_io_add_watch(ifdata->udp6_listener_channel,
+                                       G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+                                       udp6_listener_event, (gpointer)ifdata);
+#else
                        ifdata->udp6_listener_watch =
                                g_io_add_watch(ifdata->udp6_listener_channel,
                                        G_IO_IN, udp6_listener_event,
                                        (gpointer)ifdata);
+#endif
                else
                        ret |= UDP_IPv6_FAILED;
        }