2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
28 #include <net/route.h>
29 #include <arpa/inet.h>
32 #include <sys/ioctl.h>
34 #include <vconf-keys.h>
35 #include <tzplatform_config.h>
36 #include <system_info.h>
38 #include <linux/netlink.h>
39 #include <linux/rtnetlink.h>
44 #include "wifi-state.h"
47 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
48 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
49 #define MAC_INFO_FILEPATH tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
50 #define MAC_ADDRESS_FILEPATH "/sys/class/net/wlan0/address"
51 #define MAC_ADDRESS_MAX_LEN 18
52 #define HEADED_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-headed.so"
53 #define TELEPHONY_PLUGIN_FILEPATH "/usr/lib/net-config-plugin-telephony.so"
61 } netconfig_inet_prefix_s;
65 struct sockaddr_nl local;
66 struct sockaddr_nl peer;
70 netconfig_rtnl_s rth = { .fd = -1 };
72 static gboolean netconfig_device_picker_test = FALSE;
73 static int mdnsd_ref_count = 0;
77 } dnssd_conn_destroy_data;
79 static gboolean netconfig_plugin_headed_enabled = FALSE;
80 static gboolean netconfig_plugin_telephony_enabled = FALSE;
81 static void *handle_headed;
82 static void *handle_telephony;
83 static struct netconfig_headed_plugin_t *headed_plugin;
84 static struct netconfig_telephony_plugin_t *telephony_plugin;
86 static bool is_feature_checked[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
87 static bool feature_supported[NETCONFIG_SUPPORTED_FEATURE_MAX] = {0, };
89 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
96 length = strlen(passphrase);
98 if (g_str_has_suffix(service, "psk") == TRUE) {
100 for (int i = 0; i < 64; i++)
101 if (!isxdigit((unsigned char)passphrase[i]))
103 } else if (length < 8 || length > 63)
105 } else if (g_str_has_suffix(service, "wep") == TRUE) {
106 if (length == 10 || length == 26) {
107 for (int i = 0; i < length; i++)
108 if (!isxdigit((unsigned char)passphrase[i]))
110 } else if (length != 5 && length != 13)
117 GKeyFile *netconfig_keyfile_load(const char *pathname)
119 GKeyFile *keyfile = NULL;
120 GError *error = NULL;
122 keyfile = g_key_file_new();
123 if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
124 DBG("Unable to open %s, error %s", pathname, error->message);
127 g_key_file_free(keyfile);
131 DBG("loaded keyfile %s", pathname);
135 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
138 GError *error = NULL;
139 gchar *keydata = NULL;
140 gchar *needle = NULL, *directory = NULL;
142 directory = g_strdup(pathname);
143 needle = g_strrstr(directory, "/");
148 if (directory == NULL || (*directory) == '\0') {
150 ERR("directory is NULL");
154 if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
155 if (g_mkdir_with_parents(directory,
156 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
158 ERR("failed to make directory");
164 keydata = g_key_file_to_data(keyfile, &size, &error);
165 if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
166 ERR("Unable to save %s, error %s", pathname, error->message);
170 chmod(pathname, S_IRUSR | S_IWUSR);
171 DBG("Successfully saved keyfile %s", pathname);
176 void netconfig_start_timer_seconds(guint secs,
177 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
181 if (callback == NULL) {
182 ERR("callback function is NULL");
186 if ((timer_id != NULL && *timer_id != 0)) {
187 ERR("timer already is registered");
191 t_id = g_timeout_add_seconds(secs, callback, user_data);
194 ERR("Can't add timer");
198 if (timer_id != NULL)
202 void netconfig_start_timer(guint msecs,
203 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
207 INFO("Register timer with callback pointer (%p)", callback);
209 if (callback == NULL) {
210 ERR("callback function is NULL");
214 if ((timer_id != NULL && *timer_id != 0)) {
215 ERR("timer already is registered");
219 t_id = g_timeout_add(msecs, callback, user_data);
222 ERR("Can't add timer");
226 if (timer_id != NULL)
230 void netconfig_stop_timer(guint *timer_id)
232 if (timer_id == NULL) {
233 ERR("timer is NULL");
237 if (*timer_id != 0) {
238 g_source_remove(*timer_id);
243 static gboolean __netconfig_test_device_picker()
245 char *favorite_wifi_service = NULL;
247 favorite_wifi_service = wifi_get_favorite_service();
248 if (favorite_wifi_service != NULL) {
249 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
250 g_free(favorite_wifi_service);
257 static void __netconfig_pop_device_picker(void)
259 if (!netconfig_plugin_headed_enabled)
265 headed_plugin->pop_device_picker();
268 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
270 if (__netconfig_test_device_picker() == TRUE)
271 __netconfig_pop_device_picker();
276 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
278 static guint netconfig_wifi_device_picker_service_timer = 0;
280 if (is_set_method != TRUE)
281 return netconfig_wifi_device_picker_service_timer;
283 if (netconfig_wifi_device_picker_service_timer != timer_id)
284 netconfig_wifi_device_picker_service_timer = timer_id;
286 return netconfig_wifi_device_picker_service_timer;
289 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
291 __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
294 static guint __netconfig_wifi_device_picker_get_timer_id(void)
296 return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
299 void netconfig_wifi_enable_device_picker_test(void)
301 netconfig_device_picker_test = TRUE;
304 void netconfig_wifi_device_picker_service_start(void)
306 const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
309 if (netconfig_device_picker_test == TRUE)
310 netconfig_device_picker_test = FALSE;
316 netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
317 if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
320 DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
321 netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
323 __netconfig_wifi_device_picker_set_timer_id(timer_id);
326 void netconfig_wifi_device_picker_service_stop(void)
330 timer_id = __netconfig_wifi_device_picker_get_timer_id();
334 DBG("Clear device picker timer with timer_id %d", timer_id);
336 netconfig_stop_timer(&timer_id);
338 __netconfig_wifi_device_picker_set_timer_id(timer_id);
341 gboolean netconfig_is_wifi_direct_on(void)
343 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT))
346 int wifi_direct_state = 0;
348 netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
350 DBG("Wi-Fi direct mode %d", wifi_direct_state);
351 return (wifi_direct_state != 0) ? TRUE : FALSE;
354 gboolean netconfig_is_wifi_tethering_on(void)
356 if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_TETHERING)) {
357 int wifi_tethering_state = 0;
359 netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
360 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
361 if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
362 || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
363 DBG("Mobile AP is on");
368 DBG("Mobile AP is off");
372 gboolean netconfig_interface_up(const char *ifname)
377 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
381 memset(&ifr, 0, sizeof(ifr));
382 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
384 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
389 ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
390 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
397 DBG("Successfully activated wireless interface %s", ifname);
401 gboolean netconfig_interface_down(const char *ifname)
406 fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
410 memset(&ifr, 0, sizeof(ifr));
411 g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
413 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
418 ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
419 if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
426 DBG("Successfully de-activated wireless interface %s", ifname);
430 int netconfig_execute_file(const char *file_path,
431 char *const args[], char *const envs[])
437 register unsigned int index = 0;
438 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
440 while (args[index] != NULL) {
441 DBG("%s", args[index]);
445 if (!(pid = fork())) {
446 DBG("pid(%d), ppid (%d)", getpid(), getppid());
447 DBG("Inside child, exec (%s) command", file_path);
450 if (execve(file_path, args, envs) == -1) {
451 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
452 DBG("Fail to execute command (%s)", error_buf);
455 } else if (pid > 0) {
456 if (waitpid(pid, &status, 0) == -1)
457 DBG("wait pid (%u) status (%d)", pid, status);
459 if (WIFEXITED(status)) {
460 rv = WEXITSTATUS(status);
461 DBG("exited, status=%d", rv);
462 } else if (WIFSIGNALED(status)) {
463 DBG("killed by signal %d", WTERMSIG(status));
464 } else if (WIFSTOPPED(status)) {
465 DBG("stopped by signal %d", WSTOPSIG(status));
466 } else if (WIFCONTINUED(status)) {
473 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
474 DBG("failed to fork(%s)", error_buf);
478 int netconfig_execute_cmd(const char *cmd)
487 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
490 DBG("command: %s", cmd);
492 args = g_strsplit_set(cmd, " ", -1);
494 if (!(pid = fork())) {
495 DBG("pid(%d), ppid (%d)", getpid(), getppid());
498 if (execv(args[0], args) == -1) {
499 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
500 DBG("Fail to execute command (%s)", error_buf);
504 } else if (pid > 0) {
505 if (waitpid(pid, &status, 0) == -1)
506 DBG("wait pid (%u) status (%d)", pid, status);
508 if (WIFEXITED(status)) {
509 rv = WEXITSTATUS(status);
510 DBG("exited, status=%d", rv);
511 } else if (WIFSIGNALED(status)) {
512 DBG("killed by signal %d", WTERMSIG(status));
513 } else if (WIFSTOPPED(status)) {
514 DBG("stopped by signal %d", WSTOPSIG(status));
515 } else if (WIFCONTINUED(status)) {
523 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
524 DBG("failed to fork(%s)", error_buf);
530 static void on_clat_handler()
535 clat_pid = waitpid(-1, &state, WNOHANG);
537 DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
540 int netconfig_execute_clatd(const char *file_path, char *const args[])
545 register unsigned int index = 0;
546 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
548 struct sigaction act;
551 act.sa_handler = on_clat_handler;
552 sigemptyset(&act.sa_mask);
555 state = sigaction(SIGCHLD, &act, 0);
557 DBG("sigaction() : %d");
561 while (args[index] != NULL) {
562 DBG("%s", args[index]);
566 if (!(pid = fork())) {
567 DBG("pid(%d), ppid (%d)", getpid(), getppid());
568 DBG("Inside child, exec (%s) command", file_path);
571 if (execvp(file_path, args) == -1) {
572 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
573 ERR("Fail to execute command (%s)", error_buf);
576 } else if (pid > 0) {
577 ERR("Success to launch clatd");
581 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
582 DBG("failed to fork(%s)", error_buf);
586 static void no_wait_signal_handler()
591 child_pid = waitpid(-1, &state, WNOHANG);
593 DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
596 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
601 register unsigned int index = 0;
603 struct sigaction act;
605 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
607 act.sa_handler = no_wait_signal_handler;
608 sigemptyset(&act.sa_mask);
611 state = sigaction(SIGCHLD, &act, 0);
613 DBG("sigaction() : %d");
617 while (args[index] != NULL) {
618 DBG("%s", args[index]);
622 if (!(pid = fork())) {
623 DBG("pid(%d), ppid (%d)", getpid(), getppid());
624 DBG("Inside child, exec (%s) command", file_path);
627 if (execvp(file_path, args) == -1) {
628 ERR("Fail to execute command (%s)",
629 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
632 } else if (pid > 0) {
633 ERR("Successfully launched child process");
637 DBG("failed to fork(%s)",
638 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
642 int __netconfig_get_interface_index(const char *interface_name)
647 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
649 if (interface_name == NULL) {
650 DBG("Inteface name is NULL");
655 sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
657 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
658 DBG("Failed to create socket : %s", error_buf);
662 memset(&ifr, 0, sizeof(ifr));
663 strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
664 result = ioctl(sock, SIOCGIFINDEX, &ifr);
668 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
669 DBG("Failed to get ifr index: %s", error_buf);
673 return ifr.ifr_ifindex;
676 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
680 struct sockaddr_in addr_in;
682 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
684 memset(&ifr, 0, sizeof(ifr));
686 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
688 if (ifr.ifr_ifindex < 0)
691 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
693 memset(&rt, 0, sizeof(rt));
695 rt.rt_flags = RTF_UP | RTF_HOST;
696 memset(&addr_in, 0, sizeof(struct sockaddr_in));
697 addr_in.sin_family = address_family;
698 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
699 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
701 memset(&addr_in, 0, sizeof(struct sockaddr_in));
702 addr_in.sin_family = address_family;
703 addr_in.sin_addr.s_addr = INADDR_ANY;
704 memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
706 memset(&addr_in, 0, sizeof(struct sockaddr_in));
707 addr_in.sin_family = AF_INET;
708 addr_in.sin_addr.s_addr = inet_addr(subnet);
709 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
711 rt.rt_dev = ifr.ifr_name;
714 sock = socket(PF_INET, SOCK_DGRAM, 0);
717 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
718 DBG("Failed to create socket : %s", error_buf);
722 if (ioctl(sock, SIOCADDRT, &rt) < 0) {
723 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
724 DBG("Failed to set route address : %s", error_buf);
734 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
738 struct sockaddr_in addr_in;
740 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
742 memset(&ifr, 0, sizeof(ifr));
743 ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
745 if (ifr.ifr_ifindex < 0)
748 strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
750 memset(&rt, 0, sizeof(rt));
752 rt.rt_flags = RTF_UP;
753 memset(&addr_in, 0, sizeof(struct sockaddr_in));
754 addr_in.sin_family = address_family;
755 addr_in.sin_addr.s_addr = inet_addr(ip_addr);
756 memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
758 memset(&addr_in, 0, sizeof(struct sockaddr_in));
759 addr_in.sin_family = address_family;
760 addr_in.sin_addr.s_addr = inet_addr(subnet);
761 memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
762 rt.rt_dev = ifr.ifr_name;
765 sock = socket(PF_INET, SOCK_DGRAM, 0);
768 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
769 DBG("Failed to create socket : %s", error_buf);
773 if (ioctl(sock, SIOCDELRT, &rt) < 0) {
774 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
775 DBG("Failed to set route address : %s", error_buf);
785 static int __netconfig_rtnl_talk(netconfig_rtnl_s *rtnl, struct nlmsghdr *n, pid_t peer,
786 unsigned groups, struct nlmsghdr *answer)
789 struct sockaddr_nl nladdr;
791 .iov_base = (void*)n,
792 .iov_len = n->nlmsg_len
794 struct msghdr msg = {
796 .msg_namelen = sizeof(nladdr),
803 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
805 memset(&nladdr, 0, sizeof(nladdr));
806 nladdr.nl_family = AF_NETLINK;
807 nladdr.nl_pid = peer;
808 nladdr.nl_groups = groups;
810 n->nlmsg_seq = seq = ++rtnl->seq;
813 n->nlmsg_flags |= NLM_F_ACK;
815 status = sendmsg(rtnl->fd, &msg, 0);
817 DBG("failed to send message to kernel, status: %d", status);
821 memset(buf, 0, sizeof(buf));
826 iov.iov_len = sizeof(buf);
827 status = recvmsg(rtnl->fd, &msg, 0);
828 DBG("status: %d", status);
831 if (errno == EINTR || errno == EAGAIN)
833 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
834 DBG("netlink receive error %s (%d)", error_buf, errno);
838 DBG("EOF on netlink");
841 if (msg.msg_namelen != sizeof(nladdr)) {
842 DBG("sender address length == %d", msg.msg_namelen);
845 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
846 int len = h->nlmsg_len;
847 int l = len - sizeof(*h);
849 if (l < 0 || len > status) {
850 if (msg.msg_flags & MSG_TRUNC) {
851 DBG("truncated message");
854 DBG("malformed message: len=%d", len);
858 if (nladdr.nl_pid != peer ||
859 h->nlmsg_pid != rtnl->local.nl_pid ||
860 h->nlmsg_seq != seq) {
861 /** Don't forget to skip that message. */
862 status -= NLMSG_ALIGN(len);
863 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
867 if (h->nlmsg_type == NLMSG_ERROR) {
868 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
869 if (l < sizeof(struct nlmsgerr)) {
870 DBG("Error truncated message");
874 memcpy(answer, h, h->nlmsg_len);
879 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
880 DBG("RTNETLINK answers: %s", error_buf);
885 memcpy(answer, h, h->nlmsg_len);
889 DBG("Unexpected reply");
891 status -= NLMSG_ALIGN(len);
892 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
894 if (msg.msg_flags & MSG_TRUNC) {
895 DBG("Message truncated");
899 DBG("Remnant of size %d", status);
905 static int __netconfig_get_prefix(netconfig_inet_prefix_s *dst, char *arg, int family)
907 if (family != AF_UNSPEC && family != AF_INET6) {
908 DBG("Error: invalid address family.");
912 memset(dst, 0, sizeof(*dst));
914 if (strchr(arg, ':')) {
915 dst->family = AF_INET6;
916 if (inet_pton(AF_INET6, arg, dst->data) <= 0) {
917 DBG("Error: invalid ipv6 address.");
927 static int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
930 int len = RTA_LENGTH(alen);
933 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
934 DBG("Error message exceeded bound of %d", maxlen);
938 rta->rta_type = type;
940 memcpy(RTA_DATA(rta), data, alen);
941 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
946 static int __netconfig_iproute_modify(int cmd, unsigned flags, char *interface, char *ip_addr)
956 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
958 memset(&req, 0, sizeof(req));
960 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
961 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
962 req.n.nlmsg_type = cmd;
963 req.r.rtm_family = AF_INET6;
965 if (cmd != RTM_DELROUTE) {
966 req.r.rtm_protocol = RTPROT_BOOT;
967 req.r.rtm_type = RTN_UNICAST;
970 fd = socket(AF_INET6, SOCK_DGRAM, 0);
972 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
973 DBG("Failed to create socket : %s", error_buf);
977 memset(&ifr, 0, sizeof(ifr));
978 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
979 ioctl(fd, SIOCGIFINDEX, &ifr);
980 idx = ifr.ifr_ifindex;
983 if (ifr.ifr_ifindex == 0) {
984 DBG("Cannot find device %s", interface);
988 netconfig_inet_prefix_s dst;
990 ret = __netconfig_get_prefix(&dst, ip_addr, req.r.rtm_family);
994 req.r.rtm_dst_len = dst.bitlen;
996 ret = addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
1001 ret = addattr_l(&req.n, sizeof(req), RTA_OIF, &idx, sizeof(uint32_t));
1005 if (__netconfig_rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) {
1006 DBG("__netconfig_rtnl_talk failed");
1013 static void __netconfig_rtnl_close()
1021 static int __netconfig_rtnl_open(netconfig_rtnl_s *rth, unsigned subscriptions,
1026 int rcvbuf = 1024 * 1024;
1027 char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
1029 memset(rth, 0, sizeof(*rth));
1031 rth->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
1033 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
1034 DBG("Failed to open netlink socket: %s", error_buf);
1038 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) < 0) {
1039 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
1040 DBG("Failed to set option(SO_SNDBUF) on socket [Error: %s]", error_buf);
1044 if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) < 0) {
1045 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
1046 DBG("Failed to set option(SO_RCVBUF) on socket [Error: %s]", error_buf);
1050 memset(&rth->local, 0, sizeof(rth->local));
1051 rth->local.nl_family = AF_NETLINK;
1052 rth->local.nl_groups = subscriptions;
1054 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
1055 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
1056 DBG("Failed to bind netlink socket [Error: %s]", error_buf);
1060 addr_len = sizeof(rth->local);
1061 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
1062 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
1063 DBG("Failed to getsockname [Error: %s]", error_buf);
1066 if (addr_len != sizeof(rth->local)) {
1067 DBG("Wrong address length %d", addr_len);
1070 if (rth->local.nl_family != AF_NETLINK) {
1071 DBG("Wrong address family %d", rth->local.nl_family);
1074 rth->seq = time(NULL);
1079 int netconfig_add_route_ipv6(gchar *interface, gchar *gateway)
1081 int ret = __netconfig_rtnl_open(&rth, 0, NETLINK_ROUTE);
1083 DBG("Failed to open rtnl socket");
1087 ret = __netconfig_iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL, interface, gateway);
1089 DBG("Failed to modify ipv6 route.");
1090 __netconfig_rtnl_close();
1094 __netconfig_rtnl_close();
1098 int netconfig_del_route_ipv6(gchar *interface, gchar *gateway)
1100 int ret = __netconfig_rtnl_open(&rth, 0, NETLINK_ROUTE);
1102 DBG("Failed to open rtnl socket");
1106 ret = __netconfig_iproute_modify(RTM_DELROUTE, NLM_F_CREATE|NLM_F_EXCL, interface, gateway);
1108 DBG("Failed to delete ipv6 route.");
1109 __netconfig_rtnl_close();
1113 __netconfig_rtnl_close();
1117 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
1119 if (!netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT)) {
1120 wifi_complete_launch_direct(wifi, context);
1125 DBG("Launch Wi-Fi direct daemon");
1127 const char *path = "/usr/bin/wifi-direct-server.sh";
1128 char *const args[] = { "wifi-direct-server.sh", "start", NULL };
1129 char *const envs[] = { NULL };
1131 ret = netconfig_execute_file(path, args, envs);
1133 ERR("Failed to launch Wi-Fi direct daemon");
1134 netconfig_error_wifi_direct_failed(context);
1138 wifi_complete_launch_direct(wifi, context);
1142 int execute_mdnsd_script(char* op)
1144 const char *path = "/usr/bin/mdnsresponder-server.sh";
1145 char *const args[] = { "mdnsresponder-server.sh", op, NULL };
1146 char *const envs[] = { NULL };
1148 return netconfig_execute_file(path, args, envs);
1151 static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
1152 const gchar *Name, const gchar *path, const gchar *interface,
1153 const gchar *sig, GVariant *param, gpointer user_data)
1158 dnssd_conn_destroy_data *data = user_data;
1159 GDBusConnection *connection = NULL;
1160 connection = netdbus_get_connection();
1165 g_variant_get(param, "(sss)", &name, &old, &new);
1167 if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
1168 DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
1171 g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
1172 if (mdnsd_ref_count == 0) {
1173 if (execute_mdnsd_script("stop") < 0)
1174 ERR("Failed to stop mdnsresponder daemon");
1180 g_free(data->conn_name);
1185 static void register_dnssd_conn_destroy_signal(gchar *name)
1187 dnssd_conn_destroy_data *data;
1188 GDBusConnection *connection = NULL;
1189 connection = netdbus_get_connection();
1191 if (connection == NULL) {
1192 ERR("Failed to get GDbus Connection");
1196 data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
1199 ERR("Out of Memory!");
1203 data->conn_name = g_strdup(name);
1205 data->conn_id = g_dbus_connection_signal_subscribe(connection,
1206 DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
1207 "NameOwnerChanged", NULL, name,
1208 G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
1213 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
1216 DBG("Launch mdnsresponder daemon");
1218 if (execute_mdnsd_script("start") < 0) {
1219 ERR("Failed to launch mdnsresponder daemon");
1220 netconfig_error_invalid_parameter(context);
1225 register_dnssd_conn_destroy_signal(name);
1226 DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
1228 network_complete_launch_mdns(object, context);
1232 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
1234 if (!netconfig_plugin_headed_enabled)
1240 return headed_plugin->send_notification_to_net_popup(noti, ssid);
1243 int netconfig_send_message_to_net_popup(const char *title,
1244 const char *content, const char *type, const char *ssid)
1246 if (!netconfig_plugin_headed_enabled)
1252 return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
1255 int netconfig_send_restriction_to_net_popup(const char *title,
1256 const char *type, const char *restriction)
1258 if (!netconfig_plugin_headed_enabled)
1264 return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
1267 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
1269 if (!netconfig_plugin_headed_enabled)
1275 headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1278 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1280 if (!netconfig_plugin_headed_enabled)
1286 headed_plugin->pop_wifi_connected_poppup(ssid);
1289 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1291 if (!netconfig_plugin_telephony_enabled)
1294 if (!telephony_plugin)
1297 telephony_plugin->get_telephony_network_type(svctype, pstype);
1300 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1302 if (!netconfig_plugin_telephony_enabled)
1305 if (!telephony_plugin)
1308 return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1311 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1312 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1314 if (!netconfig_plugin_telephony_enabled)
1315 return NETCONFIG_ERROR_INTERNAL;
1317 if (!telephony_plugin)
1318 return NETCONFIG_ERROR_INTERNAL;
1320 return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1323 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1324 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1326 if (!netconfig_plugin_telephony_enabled)
1329 if (!telephony_plugin)
1332 return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1335 gboolean netconfig_tapi_check_sim_state(void)
1337 if (!netconfig_plugin_telephony_enabled)
1340 if (!telephony_plugin)
1343 return telephony_plugin->tapi_check_sim_state();
1346 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1347 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1349 if (!netconfig_plugin_telephony_enabled)
1352 if (!telephony_plugin)
1355 return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1358 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1359 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1361 if (!netconfig_plugin_telephony_enabled)
1364 if (!telephony_plugin)
1367 return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1370 void netconfig_set_vconf_int(const char * key, int value)
1374 DBG("[%s: %d]", key, value);
1376 ret = vconf_set_int(key, value);
1377 if (ret != VCONF_OK)
1378 ERR("Failed to set");
1381 void netconfig_set_vconf_str(const char * key, const char * value)
1385 DBG("[%s: %s]", key, value);
1387 ret = vconf_set_str(key, value);
1388 if (ret != VCONF_OK)
1389 ERR("Failed to set");
1392 int netconfig_vconf_get_int(const char * key, int *value)
1396 ret = vconf_get_int(key, value);
1397 if (ret != VCONF_OK) {
1398 ERR("Failed to get vconfkey [%s] value", key);
1405 int netconfig_vconf_get_bool(const char * key, int *value)
1409 ret = vconf_get_bool(key, value);
1410 if (ret != VCONF_OK) {
1411 ERR("Failed to get vconfkey [%s] value", key);
1418 char* netconfig_get_env(const char *key)
1421 char buf[256], *entry = NULL, *value = NULL, *last;
1427 fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1431 while (fgets(buf, sizeof(buf), fp)) {
1433 entry = strtok_r(entry, "=", &last);
1435 if (strstr(entry, key)) {
1436 entry = strtok_r(NULL, "\n", &last);
1438 len = strlen(entry);
1439 value = (char*)malloc(len+1);
1440 g_strlcpy(value, entry, len+1);
1442 value = (char*)malloc(sizeof(char));
1443 g_strlcpy(value, "\n", sizeof(char));
1454 void netconfig_set_mac_address_from_file(void)
1457 char mac_str[MAC_ADDRESS_MAX_LEN];
1458 gchar *mac_lower_str = NULL;
1461 file = fopen(MAC_INFO_FILEPATH, "r");
1463 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1464 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1466 ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1470 if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1471 ERR("Fail to read mac address");
1476 mac_len = strlen(mac_str);
1478 ERR("mac.info is empty");
1483 mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1484 netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1486 g_free(mac_lower_str);
1490 tizen_profile_t _get_tizen_profile()
1492 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1493 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1497 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1498 switch (*profileName) {
1501 profile = TIZEN_PROFILE_MOBILE;
1505 profile = TIZEN_PROFILE_WEARABLE;
1509 profile = TIZEN_PROFILE_TV;
1513 profile = TIZEN_PROFILE_IVI;
1515 default: // common or unknown ==> ALL ARE COMMON.
1516 profile = TIZEN_PROFILE_COMMON;
1523 void netconfig_plugin_init()
1525 handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1526 if (!handle_headed) {
1527 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1529 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1530 if (!headed_plugin) {
1531 ERR("Can't load symbol: %s", dlerror());
1532 dlclose(handle_headed);
1534 netconfig_plugin_headed_enabled = TRUE;
1538 handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1539 if (!handle_telephony) {
1540 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1542 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1543 if (!telephony_plugin) {
1544 ERR("Can't load symbol: %s", dlerror());
1545 dlclose(handle_telephony);
1547 netconfig_plugin_telephony_enabled = TRUE;
1552 void netconfig_plugin_deinit()
1554 if (netconfig_plugin_headed_enabled) {
1555 netconfig_plugin_headed_enabled = FALSE;
1556 dlclose(handle_headed);
1559 if (netconfig_plugin_telephony_enabled) {
1560 netconfig_plugin_telephony_enabled = FALSE;
1561 dlclose(handle_telephony);
1565 gboolean netconfig_get_headed_plugin_flag()
1567 return netconfig_plugin_headed_enabled;
1570 gboolean netconfig_get_telephony_plugin_flag()
1572 return netconfig_plugin_telephony_enabled;
1575 bool netconfig_check_feature_supported(netconfig_supported_feature_e feature)
1577 const char *key = NULL;
1579 if (!is_feature_checked[feature]) {
1581 case NETCONFIG_SUPPORTED_FEATURE_ETHERNET:
1582 key = ETHERNET_FEATURE;
1584 case NETCONFIG_SUPPORTED_FEATURE_TETHERING:
1585 key = TETHERING_FEATURE;
1587 case NETCONFIG_SUPPORTED_FEATURE_WIFI_DIRECT:
1588 key = WIFI_DIRECT_FEATURE;
1591 ERR("Uknown feature");
1595 if (system_info_get_platform_bool(key, &feature_supported[feature]) < 0) {
1596 ERR("Get feature is failed");
1599 is_feature_checked[feature] = true;
1601 return feature_supported[feature];