Merge "Imported Upstream connman version 1.38" into tizen
[platform/upstream/connman.git] / src / rtnl.c
index a46aa28..7c8c68b 100644 (file)
 #define ARPHDR_PHONET_PIPE (821)
 #endif
 
+#if defined TIZEN_EXT
+#ifndef ARPHDR_RMNET
+#define ARPHDR_RMNET (530)
+#endif
+#endif
+
 #define print(arg...) do { if (0) connman_info(arg); } while (0)
 //#define print(arg...) connman_info(arg)
 
@@ -94,6 +100,7 @@ static bool ether_blacklisted(const char *name)
        return false;
 }
 
+#if !defined TIZEN_EXT
 static bool wext_interface(char *ifname)
 {
        struct iwreq wrq;
@@ -115,6 +122,30 @@ static bool wext_interface(char *ifname)
 
        return true;
 }
+#endif
+
+#if defined TIZEN_EXT
+static bool __connman_rtnl_is_cellular_device(const char *name)
+{
+       char **pattern;
+       char **cellular_interfaces;
+
+       cellular_interfaces =
+                       connman_setting_get_string_list(
+                                       "NetworkCellularInterfaceList");
+       if (!cellular_interfaces)
+               return false;
+
+       for (pattern = cellular_interfaces; *pattern; pattern++) {
+               if (g_str_has_prefix(name, *pattern)) {
+                       DBG("Cellular interface: %s", name);
+                       return true;
+               }
+       }
+
+       return false;
+}
+#endif
 
 static void read_uevent(struct interface_data *interface)
 {
@@ -124,9 +155,19 @@ static void read_uevent(struct interface_data *interface)
 
        name = connman_inet_ifname(interface->index);
 
+#if defined TIZEN_EXT
+       if (__connman_rtnl_is_cellular_device(name)) {
+               interface->service_type = CONNMAN_SERVICE_TYPE_CELLULAR;
+               interface->device_type = CONNMAN_DEVICE_TYPE_CELLULAR;
+               g_free(name);
+               return;
+       }
+#endif
+
        if (ether_blacklisted(name)) {
                interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
                interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
+               goto out;
        } else {
                interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
                interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
@@ -170,7 +211,12 @@ static void read_uevent(struct interface_data *interface)
                } else if (strcmp(line + 8, "gadget") == 0) {
                        interface->service_type = CONNMAN_SERVICE_TYPE_GADGET;
                        interface->device_type = CONNMAN_DEVICE_TYPE_GADGET;
-
+               } else if (strcmp(line + 8, "vlan") == 0) {
+                       interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
+                       interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
+               } else if (strcmp(line + 8, "bond") == 0) {
+                       interface->service_type = CONNMAN_SERVICE_TYPE_ETHERNET;
+                       interface->device_type = CONNMAN_DEVICE_TYPE_ETHERNET;
                } else {
                        interface->service_type = CONNMAN_SERVICE_TYPE_UNKNOWN;
                        interface->device_type = CONNMAN_DEVICE_TYPE_UNKNOWN;
@@ -182,6 +228,8 @@ static void read_uevent(struct interface_data *interface)
        if (found_devtype)
                goto out;
 
+#if !defined TIZEN_EXT
+       /* TIZEN does not use old wext interface */
        /* We haven't got a DEVTYPE, let's check if it's a wireless device */
        if (wext_interface(name)) {
                interface->service_type = CONNMAN_SERVICE_TYPE_WIFI;
@@ -189,6 +237,7 @@ static void read_uevent(struct interface_data *interface)
 
                connman_error("%s runs an unsupported 802.11 driver", name);
        }
+#endif
 
 out:
        g_free(name);
@@ -414,6 +463,28 @@ static void process_newlink(unsigned short type, int index, unsigned flags,
        if (!extract_link(msg, bytes, &address, &ifname, &mtu, &operstate, &stats))
                return;
 
+#if defined TIZEN_EXT_WIFI_MESH
+       /* Do not accept Wi-Fi Mesh interface */
+       if (g_strrstr(ifname, "mesh") != NULL) {
+               DBG("Newlink event for Wi-Fi Mesh interface ignored");
+               return;
+       }
+
+       /* Do not accept Wi-Fi WLAN1 interface "dedicated for softAP */
+       if (!g_strcmp0(ifname, "wlan1")) {
+               DBG("Newlink event for Wi-Fi WLAN1 interface ignored");
+               return;
+       }
+#endif
+
+#if defined TIZEN_EXT
+       /* Do not accept Wi-Fi P2P interface */
+       if (g_strrstr(ifname, "p2p") != NULL) {
+               DBG("Newlink event for Wi-Fi P2P interface ignored");
+               return;
+       }
+#endif
+
        snprintf(ident, 13, "%02x%02x%02x%02x%02x%02x",
                                                address.ether_addr_octet[0],
                                                address.ether_addr_octet[1],
@@ -436,12 +507,25 @@ static void process_newlink(unsigned short type, int index, unsigned flags,
                return;
        }
 
+#ifdef TIZEN_EXT
+       if (TIZEN_TV_EXT && g_strcmp0(ident, "eeeeeeeeeeee") == 0) {
+               DBG("Newlink event with Dummy MAC. Ignored!");
+               return;
+       }
+#endif
+
        switch (type) {
        case ARPHRD_ETHER:
        case ARPHRD_LOOPBACK:
        case ARPHDR_PHONET_PIPE:
        case ARPHRD_PPP:
        case ARPHRD_NONE:
+#if defined TIZEN_EXT
+/*
+ * Description: ARPHDR_RMNET for QC modem using QMI
+ */
+       case ARPHDR_RMNET:
+#endif
                __connman_ipconfig_newlink(index, type, flags,
                                                        str, mtu, &stats);
                break;
@@ -466,7 +550,28 @@ static void process_newlink(unsigned short type, int index, unsigned flags,
 
                if (type == ARPHRD_ETHER)
                        read_uevent(interface);
-       } else
+#if defined TIZEN_EXT
+               if (type == ARPHRD_PPP || type == ARPHDR_RMNET)
+                       read_uevent(interface);
+
+       } else if (g_strcmp0(interface->ident, ident) != 0) {
+               /* If an original address is built-in physical device,
+                * it's hardly get an address at a initial creation
+                */
+               __connman_technology_remove_interface(interface->service_type,
+                               interface->index, interface->ident);
+
+               g_free(interface->ident);
+               interface->ident = g_strdup(ident);
+
+               __connman_technology_add_interface(interface->service_type,
+                               interface->index, interface->ident);
+
+               interface = NULL;
+#endif
+       } else if (type == ARPHRD_ETHER && interface->device_type == CONNMAN_DEVICE_TYPE_UNKNOWN)
+               read_uevent(interface);
+       else
                interface = NULL;
 
        for (list = rtnl_list; list; list = list->next) {
@@ -486,14 +591,15 @@ static void process_newlink(unsigned short type, int index, unsigned flags,
                __connman_technology_add_interface(interface->service_type,
                        interface->index, interface->ident);
 
-       for (list = watch_list; list; list = list->next) {
+       list = watch_list;
+       while (list) {
+               GSList *next = list->next;
                struct watch_data *watch = list->data;
 
-               if (watch->index != index)
-                       continue;
-
-               if (watch->newlink)
+               if (watch->index == index && watch->newlink)
                        watch->newlink(flags, change, watch->user_data);
+
+               list = next;
        }
 }
 
@@ -527,6 +633,13 @@ static void process_dellink(unsigned short type, int index, unsigned flags,
        case ARPHDR_PHONET_PIPE:
        case ARPHRD_PPP:
        case ARPHRD_NONE:
+#if defined TIZEN_EXT
+       /*
+        * Description: SLP requires ARPHRD_PPP for PPP type device
+        *              ARPHDR_RMNET for QC modem using QMI
+        */
+       case ARPHDR_RMNET:
+#endif
                __connman_ipconfig_dellink(index, &stats);
                break;
        }
@@ -612,17 +725,18 @@ static void process_newaddr(unsigned char family, unsigned char prefixlen,
        if (!inet_ntop(family, src, ip_string, INET6_ADDRSTRLEN))
                return;
 
-       __connman_ipconfig_newaddr(index, family, label,
-                                       prefixlen, ip_string);
-
-       if (family == AF_INET6) {
-               /*
-                * Re-create RDNSS configured servers if there are any
-                * for this interface. This is done because we might
-                * have now properly configured interface with proper
-                * autoconfigured address.
-                */
-               __connman_resolver_redo_servers(index);
+       if (__connman_ipconfig_newaddr(index, family, label,
+                                       prefixlen, ip_string) >= 0) {
+               if (family == AF_INET6) {
+                       /*
+                        * Re-create RDNSS configured servers if there
+                        * are any for this interface. This is done
+                        * because we might have now properly
+                        * configured interface with proper
+                        * autoconfigured address.
+                        */
+                       __connman_resolver_redo_servers(index);
+               }
        }
 }
 
@@ -1083,6 +1197,8 @@ static void rtnl_route(struct nlmsghdr *hdr)
 
 static bool is_route_rtmsg(struct rtmsg *msg)
 {
+       if (msg->rtm_flags & RTM_F_CLONED)
+               return false;
 
        if (msg->rtm_table != RT_TABLE_MAIN)
                return false;
@@ -1219,6 +1335,37 @@ static void rtnl_newnduseropt(struct nlmsghdr *hdr)
        if (index < 0)
                return;
 
+#if defined TIZEN_EXT
+       struct connman_service *service;
+       enum connman_service_state state;
+       enum connman_dnsconfig_method ipv6_dns_method;
+
+       service = __connman_service_lookup_from_index(index);
+       if (!service) {
+               DBG("Invalid service");
+               return;
+       }
+
+       DBG("service: %p index: %d\n", service, index);
+
+       if (connman_setting_get_bool("SingleConnectedTechnology") == TRUE) {
+               state = __connman_service_ipconfig_get_state(service, CONNMAN_IPCONFIG_TYPE_IPV6);
+               if (state != CONNMAN_SERVICE_STATE_ASSOCIATION &&
+                               state != CONNMAN_SERVICE_STATE_CONFIGURATION &&
+                               state != CONNMAN_SERVICE_STATE_READY &&
+                               state != CONNMAN_SERVICE_STATE_ONLINE) {
+                       DBG("Service state[%d] is not connecting/connected", state);
+                       return;
+               }
+       }
+
+       ipv6_dns_method = connman_service_get_ipv6_dns_method(service);
+       if (ipv6_dns_method != CONNMAN_DNSCONFIG_METHOD_DHCP) {
+               DBG("IPv6 DNS method is not Auto ignore RA!!! [DNS method: %d]", ipv6_dns_method);
+               return;
+       }
+#endif
+
        for (opt = (void *)&msg[1];
                        msglen > 0;
                        msglen -= opt->nd_opt_len * 8,
@@ -1229,7 +1376,12 @@ static void rtnl_newnduseropt(struct nlmsghdr *hdr)
 
                if (opt->nd_opt_type == 25) { /* ND_OPT_RDNSS */
                        char buf[40];
+#if defined TIZEN_EXT
+                       struct connman_service *service;
 
+                       service = __connman_service_lookup_from_index(index);
+                       DBG("service: %p\n",service);
+#endif
                        servers = rtnl_nd_opt_rdnss(opt, &lifetime,
                                                                &nr_servers);
                        for (i = 0; i < nr_servers; i++) {
@@ -1237,6 +1389,14 @@ static void rtnl_newnduseropt(struct nlmsghdr *hdr)
                                                                sizeof(buf)))
                                        continue;
 
+#if defined TIZEN_EXT
+                               __connman_service_nameserver_remove(service,
+                                               buf, false,
+                                               CONNMAN_IPCONFIG_TYPE_IPV6);
+                               __connman_service_nameserver_append(service,
+                                               buf, false,
+                                               CONNMAN_IPCONFIG_TYPE_IPV6);
+#endif
                                connman_resolver_append_lifetime(index,
                                                        NULL, buf, lifetime);
                        }
@@ -1291,6 +1451,7 @@ static const char *type2string(uint16_t type)
 }
 
 static GIOChannel *channel = NULL;
+static guint channel_watch = 0;
 
 struct rtnl_request {
        struct nlmsghdr hdr;
@@ -1365,8 +1526,6 @@ static int process_response(guint32 seq)
 
 static void rtnl_message(void *buf, size_t len)
 {
-       DBG("buf %p len %zd", buf, len);
-
        while (len > 0) {
                struct nlmsghdr *hdr = buf;
                struct nlmsgerr *err;
@@ -1374,7 +1533,7 @@ static void rtnl_message(void *buf, size_t len)
                if (!NLMSG_OK(hdr, len))
                        break;
 
-               DBG("%s len %d type %d flags 0x%04x seq %d pid %d",
+               DBG("%s len %u type %u flags 0x%04x seq %u pid %u",
                                        type2string(hdr->nlmsg_type),
                                        hdr->nlmsg_len, hdr->nlmsg_type,
                                        hdr->nlmsg_flags, hdr->nlmsg_seq,
@@ -1428,8 +1587,15 @@ static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data
        ssize_t status;
        int fd;
 
+#if defined TIZEN_EXT
+       if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+               __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
+               return FALSE;
+       }
+#else /* TIZEN_EXT */
        if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
                return FALSE;
+#endif /* TIZEN_EXT */
 
        memset(buf, 0, sizeof(buf));
        memset(&nladdr, 0, sizeof(nladdr));
@@ -1442,11 +1608,21 @@ static gboolean netlink_event(GIOChannel *chan, GIOCondition cond, gpointer data
                if (errno == EINTR || errno == EAGAIN)
                        return TRUE;
 
+#if defined TIZEN_EXT
+               __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
+#endif /* TIZEN_EXT */
                return FALSE;
        }
 
+#if defined TIZEN_EXT
+       if (status == 0) {
+               __connman_rtnl_init(GIO_SOCKET_RETRY_COUNT);
+               return FALSE;
+       }
+#else /* TIZEN_EXT */
        if (status == 0)
                return FALSE;
+#endif /* TIZEN_EXT */
 
        if (nladdr.nl_pid != 0) { /* not sent by kernel, ignore */
                DBG("Received msg from %u, ignoring it", nladdr.nl_pid);
@@ -1590,19 +1766,34 @@ int __connman_rtnl_request_update(void)
        return send_getlink();
 }
 
+#if defined TIZEN_EXT
+int __connman_rtnl_init(int retry_count)
+#else /* TIZEN_EXT */
 int __connman_rtnl_init(void)
+#endif /* TIZEN_EXT */
 {
        struct sockaddr_nl addr;
        int sk;
 
+#if defined TIZEN_EXT
+       if (retry_count < 0)
+               return -1;
+
+       DBG("retry_count %d", retry_count);
+#else /* TIZEN_EXT */
        DBG("");
+#endif /* TIZEN_EXT */
 
        interface_list = g_hash_table_new_full(g_direct_hash, g_direct_equal,
                                                        NULL, free_interface);
 
        sk = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
        if (sk < 0)
+#if defined TIZEN_EXT
+               return __connman_rtnl_init(retry_count - 1);
+#else /* TIZEN_EXT */
                return -1;
+#endif /* TIZEN_EXT */
 
        memset(&addr, 0, sizeof(addr));
        addr.nl_family = AF_NETLINK;
@@ -1612,7 +1803,11 @@ int __connman_rtnl_init(void)
 
        if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
                close(sk);
+#if defined TIZEN_EXT
+               return __connman_rtnl_init(retry_count - 1);
+#else /* TIZEN_EXT */
                return -1;
+#endif /* TIZEN_EXT */
        }
 
        channel = g_io_channel_unix_new(sk);
@@ -1621,8 +1816,9 @@ int __connman_rtnl_init(void)
        g_io_channel_set_encoding(channel, NULL, NULL);
        g_io_channel_set_buffered(channel, FALSE);
 
-       g_io_add_watch(channel, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
-                                                       netlink_event, NULL);
+       channel_watch = g_io_add_watch(channel,
+                               G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
+                               netlink_event, NULL);
 
        return 0;
 }
@@ -1672,6 +1868,11 @@ void __connman_rtnl_cleanup(void)
        g_slist_free(request_list);
        request_list = NULL;
 
+       if (channel_watch) {
+               g_source_remove(channel_watch);
+               channel_watch = 0;
+       }
+
        g_io_channel_shutdown(channel, TRUE, NULL);
        g_io_channel_unref(channel);