5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
6 * Copyright (C) 2003-2005 Go-Core Project
7 * Copyright (C) 2003-2006 Helsinki University of Technology
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <linux/sockios.h>
39 #include <arpa/inet.h>
40 #include <net/route.h>
41 #include <net/ethernet.h>
43 #include <net/if_arp.h>
44 #include <netinet/icmp6.h>
46 #include <linux/if_tun.h>
50 #define NLMSG_TAIL(nmsg) \
51 ((struct rtattr *) (((uint8_t*) (nmsg)) + \
52 NLMSG_ALIGN((nmsg)->nlmsg_len)))
54 int __connman_inet_rtnl_addattr_l(struct nlmsghdr *n, size_t max_length,
55 int type, const void *data, size_t data_length)
60 length = RTA_LENGTH(data_length);
62 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length) > max_length)
67 rta->rta_len = length;
68 memcpy(RTA_DATA(rta), data, data_length);
69 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length);
74 int __connman_inet_modify_address(int cmd, int flags,
75 int index, int family,
78 unsigned char prefixlen,
79 const char *broadcast)
81 uint8_t request[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
82 NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
83 RTA_LENGTH(sizeof(struct in6_addr)) +
84 RTA_LENGTH(sizeof(struct in6_addr))];
86 struct nlmsghdr *header;
87 struct sockaddr_nl nl_addr;
88 struct ifaddrmsg *ifaddrmsg;
89 struct in6_addr ipv6_addr;
90 struct in_addr ipv4_addr, ipv4_dest, ipv4_bcast;
93 DBG("cmd %#x flags %#x index %d family %d address %s peer %s "
94 "prefixlen %hhu broadcast %s", cmd, flags, index, family,
95 address, peer, prefixlen, broadcast);
100 if (family != AF_INET && family != AF_INET6)
103 memset(&request, 0, sizeof(request));
105 header = (struct nlmsghdr *)request;
106 header->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
107 header->nlmsg_type = cmd;
108 header->nlmsg_flags = NLM_F_REQUEST | flags;
109 header->nlmsg_seq = 1;
111 ifaddrmsg = NLMSG_DATA(header);
112 ifaddrmsg->ifa_family = family;
113 ifaddrmsg->ifa_prefixlen = prefixlen;
114 ifaddrmsg->ifa_flags = IFA_F_PERMANENT;
115 ifaddrmsg->ifa_scope = RT_SCOPE_UNIVERSE;
116 ifaddrmsg->ifa_index = index;
118 if (family == AF_INET) {
119 if (inet_pton(AF_INET, address, &ipv4_addr) < 1)
122 if (broadcast != NULL)
123 inet_pton(AF_INET, broadcast, &ipv4_bcast);
125 ipv4_bcast.s_addr = ipv4_addr.s_addr |
126 htonl(0xfffffffflu >> prefixlen);
129 if (inet_pton(AF_INET, peer, &ipv4_dest) < 1)
132 err = __connman_inet_rtnl_addattr_l(header,
141 err = __connman_inet_rtnl_addattr_l(header,
149 err = __connman_inet_rtnl_addattr_l(header,
157 } else if (family == AF_INET6) {
158 if (inet_pton(AF_INET6, address, &ipv6_addr) < 1)
161 err = __connman_inet_rtnl_addattr_l(header,
170 sk = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
174 memset(&nl_addr, 0, sizeof(nl_addr));
175 nl_addr.nl_family = AF_NETLINK;
177 if ((err = sendto(sk, request, header->nlmsg_len, 0,
178 (struct sockaddr *) &nl_addr, sizeof(nl_addr))) < 0)
189 int connman_inet_ifindex(const char *name)
197 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
201 memset(&ifr, 0, sizeof(ifr));
202 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
204 err = ioctl(sk, SIOCGIFINDEX, &ifr);
211 return ifr.ifr_ifindex;
214 char *connman_inet_ifname(int index)
222 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
226 memset(&ifr, 0, sizeof(ifr));
227 ifr.ifr_ifindex = index;
229 err = ioctl(sk, SIOCGIFNAME, &ifr);
236 return strdup(ifr.ifr_name);
239 short int connman_inet_ifflags(int index)
244 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
248 memset(&ifr, 0, sizeof(ifr));
249 ifr.ifr_ifindex = index;
251 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
256 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
269 int connman_inet_ifup(int index)
274 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
278 memset(&ifr, 0, sizeof(ifr));
279 ifr.ifr_ifindex = index;
281 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
286 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
291 if (ifr.ifr_flags & IFF_UP) {
296 ifr.ifr_flags |= IFF_UP;
298 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
311 int connman_inet_ifdown(int index)
316 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
320 memset(&ifr, 0, sizeof(ifr));
321 ifr.ifr_ifindex = index;
323 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
328 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
333 if (!(ifr.ifr_flags & IFF_UP)) {
338 ifr.ifr_flags &= ~IFF_UP;
340 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
351 static char *index2addr(int index)
354 struct ether_addr eth;
361 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
365 memset(&ifr, 0, sizeof(ifr));
366 ifr.ifr_ifindex = index;
368 err = ioctl(sk, SIOCGIFNAME, &ifr);
371 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
382 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
383 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
384 eth.ether_addr_octet[0],
385 eth.ether_addr_octet[1],
386 eth.ether_addr_octet[2],
387 eth.ether_addr_octet[3],
388 eth.ether_addr_octet[4],
389 eth.ether_addr_octet[5]);
394 static char *index2ident(int index, const char *prefix)
397 struct ether_addr eth;
404 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
408 memset(&ifr, 0, sizeof(ifr));
409 ifr.ifr_ifindex = index;
411 err = ioctl(sk, SIOCGIFNAME, &ifr);
414 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
421 len = prefix ? strlen(prefix) + 18 : 18;
427 memcpy(ð, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
428 snprintf(str, len, "%s%02x%02x%02x%02x%02x%02x",
429 prefix ? prefix : "",
430 eth.ether_addr_octet[0],
431 eth.ether_addr_octet[1],
432 eth.ether_addr_octet[2],
433 eth.ether_addr_octet[3],
434 eth.ether_addr_octet[4],
435 eth.ether_addr_octet[5]);
440 connman_bool_t connman_inet_is_cfg80211(int index)
442 connman_bool_t result = FALSE;
443 char phy80211_path[PATH_MAX];
448 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
452 memset(&ifr, 0, sizeof(ifr));
453 ifr.ifr_ifindex = index;
455 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0)
458 snprintf(phy80211_path, PATH_MAX,
459 "/sys/class/net/%s/phy80211", ifr.ifr_name);
461 if (stat(phy80211_path, &st) == 0 && (st.st_mode & S_IFDIR))
470 struct connman_device *connman_inet_create_device(int index)
472 enum connman_device_type type;
473 struct connman_device *device;
474 char *devname, *ident = NULL;
475 char *addr = NULL, *name = NULL;
480 devname = connman_inet_ifname(index);
484 if (__connman_device_isfiltered(devname) == TRUE) {
485 connman_info("Ignoring interface %s (filtered)", devname);
490 type = __connman_rtnl_get_device_type(index);
493 case CONNMAN_DEVICE_TYPE_UNKNOWN:
494 connman_info("Ignoring interface %s (type unknown)", devname);
497 case CONNMAN_DEVICE_TYPE_ETHERNET:
498 case CONNMAN_DEVICE_TYPE_GADGET:
499 case CONNMAN_DEVICE_TYPE_WIFI:
500 case CONNMAN_DEVICE_TYPE_WIMAX:
501 name = index2ident(index, "");
502 addr = index2addr(index);
504 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
505 case CONNMAN_DEVICE_TYPE_CELLULAR:
506 case CONNMAN_DEVICE_TYPE_GPS:
507 case CONNMAN_DEVICE_TYPE_VENDOR:
508 name = strdup(devname);
512 device = connman_device_create(name, type);
517 case CONNMAN_DEVICE_TYPE_UNKNOWN:
518 case CONNMAN_DEVICE_TYPE_VENDOR:
519 case CONNMAN_DEVICE_TYPE_GPS:
521 case CONNMAN_DEVICE_TYPE_ETHERNET:
522 case CONNMAN_DEVICE_TYPE_GADGET:
523 ident = index2ident(index, NULL);
525 case CONNMAN_DEVICE_TYPE_WIFI:
526 case CONNMAN_DEVICE_TYPE_WIMAX:
527 ident = index2ident(index, NULL);
529 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
531 case CONNMAN_DEVICE_TYPE_CELLULAR:
532 ident = index2ident(index, NULL);
536 connman_device_set_index(device, index);
537 connman_device_set_interface(device, devname);
540 connman_device_set_ident(device, ident);
544 connman_device_set_string(device, "Address", addr);
555 struct in6_addr ifr6_addr;
556 __u32 ifr6_prefixlen;
557 unsigned int ifr6_ifindex;
560 int connman_inet_set_ipv6_address(int index,
561 struct connman_ipaddress *ipaddress)
564 unsigned char prefix_len;
567 if (ipaddress->local == NULL)
570 prefix_len = ipaddress->prefixlen;
571 address = ipaddress->local;
573 DBG("index %d address %s prefix_len %d", index, address, prefix_len);
575 err = __connman_inet_modify_address(RTM_NEWADDR,
576 NLM_F_REPLACE | NLM_F_ACK, index, AF_INET6,
577 address, NULL, prefix_len, NULL);
579 connman_error("%s: %s", __func__, strerror(-err));
586 int connman_inet_set_address(int index, struct connman_ipaddress *ipaddress)
589 unsigned char prefix_len;
590 const char *address, *broadcast, *peer;
592 if (ipaddress->local == NULL)
595 prefix_len = ipaddress->prefixlen;
596 address = ipaddress->local;
597 broadcast = ipaddress->broadcast;
598 peer = ipaddress->peer;
600 DBG("index %d address %s prefix_len %d", index, address, prefix_len);
602 err = __connman_inet_modify_address(RTM_NEWADDR,
603 NLM_F_REPLACE | NLM_F_ACK, index, AF_INET,
604 address, peer, prefix_len, broadcast);
606 connman_error("%s: %s", __func__, strerror(-err));
613 int connman_inet_clear_ipv6_address(int index, const char *address,
618 DBG("index %d address %s prefix_len %d", index, address, prefix_len);
620 err = __connman_inet_modify_address(RTM_DELADDR, 0, index, AF_INET6,
621 address, NULL, prefix_len, NULL);
623 connman_error("%s: %s", __func__, strerror(-err));
630 int connman_inet_clear_address(int index, struct connman_ipaddress *ipaddress)
633 unsigned char prefix_len;
634 const char *address, *broadcast, *peer;
636 prefix_len = ipaddress->prefixlen;
637 address = ipaddress->local;
638 broadcast = ipaddress->broadcast;
639 peer = ipaddress->peer;
641 DBG("index %d address %s prefix_len %d", index, address, prefix_len);
643 err = __connman_inet_modify_address(RTM_DELADDR, 0, index, AF_INET,
644 address, peer, prefix_len, broadcast);
646 connman_error("%s: %s", __func__, strerror(-err));
653 int connman_inet_add_host_route(int index, const char *host,
656 return connman_inet_add_network_route(index, host, gateway, NULL);
659 int connman_inet_del_host_route(int index, const char *host)
661 return connman_inet_del_network_route(index, host);
664 int connman_inet_add_network_route(int index, const char *host,
670 struct sockaddr_in addr;
673 DBG("index %d host %s gateway %s netmask %s", index,
674 host, gateway, netmask);
676 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
680 memset(&ifr, 0, sizeof(ifr));
681 ifr.ifr_ifindex = index;
683 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
688 DBG("ifname %s", ifr.ifr_name);
690 memset(&rt, 0, sizeof(rt));
691 rt.rt_flags = RTF_UP;
693 rt.rt_flags |= RTF_GATEWAY;
695 rt.rt_flags |= RTF_HOST;
697 memset(&addr, 0, sizeof(addr));
698 addr.sin_family = AF_INET;
699 addr.sin_addr.s_addr = inet_addr(host);
700 memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
702 memset(&addr, 0, sizeof(addr));
703 addr.sin_family = AF_INET;
705 addr.sin_addr.s_addr = inet_addr(gateway);
707 addr.sin_addr.s_addr = INADDR_ANY;
708 memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway));
710 memset(&addr, 0, sizeof(addr));
711 addr.sin_family = AF_INET;
712 addr.sin_addr.s_addr = INADDR_ANY;
714 addr.sin_addr.s_addr = inet_addr(netmask);
716 addr.sin_addr.s_addr = INADDR_ANY;
717 memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask));
719 rt.rt_dev = ifr.ifr_name;
721 err = ioctl(sk, SIOCADDRT, &rt);
723 connman_error("Adding host route failed (%s)",
731 int connman_inet_del_network_route(int index, const char *host)
735 struct sockaddr_in addr;
738 DBG("index %d host %s", index, host);
740 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
744 memset(&ifr, 0, sizeof(ifr));
745 ifr.ifr_ifindex = index;
747 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
752 DBG("ifname %s", ifr.ifr_name);
754 memset(&rt, 0, sizeof(rt));
755 rt.rt_flags = RTF_UP | RTF_HOST;
757 memset(&addr, 0, sizeof(addr));
758 addr.sin_family = AF_INET;
759 addr.sin_addr.s_addr = inet_addr(host);
760 memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
762 rt.rt_dev = ifr.ifr_name;
764 err = ioctl(sk, SIOCDELRT, &rt);
766 connman_error("Deleting host route failed (%s)",
774 int connman_inet_del_ipv6_network_route(int index, const char *host,
775 unsigned char prefix_len)
780 DBG("index %d host %s", index, host);
785 memset(&rt, 0, sizeof(rt));
787 rt.rtmsg_dst_len = prefix_len;
789 err = inet_pton(AF_INET6, host, &rt.rtmsg_dst);
793 rt.rtmsg_flags = RTF_UP | RTF_HOST;
796 rt.rtmsg_ifindex = index;
798 sk = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
804 err = ioctl(sk, SIOCDELRT, &rt);
808 connman_error("Del IPv6 host route error (%s)",
814 int connman_inet_del_ipv6_host_route(int index, const char *host)
816 return connman_inet_del_ipv6_network_route(index, host, 128);
819 int connman_inet_add_ipv6_network_route(int index, const char *host,
821 unsigned char prefix_len)
826 DBG("index %d host %s gateway %s", index, host, gateway);
831 memset(&rt, 0, sizeof(rt));
833 rt.rtmsg_dst_len = prefix_len;
835 err = inet_pton(AF_INET6, host, &rt.rtmsg_dst);
839 rt.rtmsg_flags = RTF_UP | RTF_HOST;
841 if (gateway != NULL) {
842 rt.rtmsg_flags |= RTF_GATEWAY;
843 inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway);
847 rt.rtmsg_ifindex = index;
849 sk = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
855 err = ioctl(sk, SIOCADDRT, &rt);
859 connman_error("Set IPv6 host route error (%s)",
865 int connman_inet_add_ipv6_host_route(int index, const char *host,
868 return connman_inet_add_ipv6_network_route(index, host, gateway, 128);
871 int connman_inet_set_ipv6_gateway_address(int index, const char *gateway)
876 DBG("index %d gateway %s", index, gateway);
881 memset(&rt, 0, sizeof(rt));
883 err = inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway);
887 rt.rtmsg_flags = RTF_UP | RTF_GATEWAY;
889 rt.rtmsg_dst_len = 0;
890 rt.rtmsg_ifindex = index;
892 sk = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
898 err = ioctl(sk, SIOCADDRT, &rt);
902 connman_error("Set default IPv6 gateway error (%s)",
908 int connman_inet_clear_ipv6_gateway_address(int index, const char *gateway)
913 DBG("index %d gateway %s", index, gateway);
918 memset(&rt, 0, sizeof(rt));
920 err = inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway);
924 rt.rtmsg_flags = RTF_UP | RTF_GATEWAY;
926 rt.rtmsg_dst_len = 0;
927 rt.rtmsg_ifindex = index;
929 sk = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
935 err = ioctl(sk, SIOCDELRT, &rt);
939 connman_error("Clear default IPv6 gateway error (%s)",
945 int connman_inet_set_gateway_address(int index, const char *gateway)
949 struct sockaddr_in addr;
952 DBG("index %d gateway %s", index, gateway);
954 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
958 memset(&ifr, 0, sizeof(ifr));
959 ifr.ifr_ifindex = index;
961 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
966 DBG("ifname %s", ifr.ifr_name);
968 memset(&rt, 0, sizeof(rt));
969 rt.rt_flags = RTF_UP | RTF_GATEWAY;
971 memset(&addr, 0, sizeof(addr));
972 addr.sin_family = AF_INET;
973 addr.sin_addr.s_addr = INADDR_ANY;
974 memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
976 memset(&addr, 0, sizeof(addr));
977 addr.sin_family = AF_INET;
978 addr.sin_addr.s_addr = inet_addr(gateway);
979 memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway));
981 memset(&addr, 0, sizeof(addr));
982 addr.sin_family = AF_INET;
983 addr.sin_addr.s_addr = INADDR_ANY;
984 memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask));
986 err = ioctl(sk, SIOCADDRT, &rt);
988 connman_error("Setting default gateway route failed (%s)",
996 int connman_inet_set_gateway_interface(int index)
1000 struct sockaddr_in addr;
1003 DBG("index %d", index);
1005 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1009 memset(&ifr, 0, sizeof(ifr));
1010 ifr.ifr_ifindex = index;
1012 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
1017 DBG("ifname %s", ifr.ifr_name);
1019 memset(&rt, 0, sizeof(rt));
1020 rt.rt_flags = RTF_UP;
1022 memset(&addr, 0, sizeof(addr));
1023 addr.sin_family = AF_INET;
1024 addr.sin_addr.s_addr = INADDR_ANY;
1026 memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask));
1027 memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
1028 memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway));
1030 rt.rt_dev = ifr.ifr_name;
1032 err = ioctl(sk, SIOCADDRT, &rt);
1034 connman_error("Setting default interface route failed (%s)",
1041 int connman_inet_set_ipv6_gateway_interface(int index)
1045 struct sockaddr_in6 addr;
1046 const struct in6_addr any = IN6ADDR_ANY_INIT;
1049 DBG("index %d", index);
1051 sk = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1055 memset(&ifr, 0, sizeof(ifr));
1056 ifr.ifr_ifindex = index;
1058 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
1063 DBG("ifname %s", ifr.ifr_name);
1065 memset(&rt, 0, sizeof(rt));
1066 rt.rt_flags = RTF_UP;
1068 memset(&addr, 0, sizeof(addr));
1069 addr.sin6_family = AF_INET6;
1070 addr.sin6_addr = any;
1072 memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask));
1073 memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
1074 memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway));
1076 rt.rt_dev = ifr.ifr_name;
1078 err = ioctl(sk, SIOCADDRT, &rt);
1080 connman_error("Setting default interface route failed (%s)",
1087 int connman_inet_clear_gateway_address(int index, const char *gateway)
1091 struct sockaddr_in addr;
1094 DBG("index %d gateway %s", index, gateway);
1096 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1100 memset(&ifr, 0, sizeof(ifr));
1101 ifr.ifr_ifindex = index;
1103 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
1108 DBG("ifname %s", ifr.ifr_name);
1110 memset(&rt, 0, sizeof(rt));
1111 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1113 memset(&addr, 0, sizeof(addr));
1114 addr.sin_family = AF_INET;
1115 addr.sin_addr.s_addr = INADDR_ANY;
1116 memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
1118 memset(&addr, 0, sizeof(addr));
1119 addr.sin_family = AF_INET;
1120 addr.sin_addr.s_addr = inet_addr(gateway);
1121 memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway));
1123 memset(&addr, 0, sizeof(addr));
1124 addr.sin_family = AF_INET;
1125 addr.sin_addr.s_addr = INADDR_ANY;
1126 memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask));
1128 err = ioctl(sk, SIOCDELRT, &rt);
1130 connman_error("Removing default gateway route failed (%s)",
1138 int connman_inet_clear_gateway_interface(int index)
1142 struct sockaddr_in addr;
1145 DBG("index %d", index);
1147 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1151 memset(&ifr, 0, sizeof(ifr));
1152 ifr.ifr_ifindex = index;
1154 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
1159 DBG("ifname %s", ifr.ifr_name);
1161 memset(&rt, 0, sizeof(rt));
1162 rt.rt_flags = RTF_UP;
1164 memset(&addr, 0, sizeof(addr));
1165 addr.sin_family = AF_INET;
1166 addr.sin_addr.s_addr = INADDR_ANY;
1168 memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask));
1169 memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
1170 memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway));
1172 rt.rt_dev = ifr.ifr_name;
1174 err = ioctl(sk, SIOCDELRT, &rt);
1176 connman_error("Removing default interface route failed (%s)",
1183 int connman_inet_clear_ipv6_gateway_interface(int index)
1187 struct sockaddr_in6 addr;
1188 const struct in6_addr any = IN6ADDR_ANY_INIT;
1191 DBG("index %d", index);
1193 sk = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1197 memset(&ifr, 0, sizeof(ifr));
1198 ifr.ifr_ifindex = index;
1200 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
1205 DBG("ifname %s", ifr.ifr_name);
1207 memset(&rt, 0, sizeof(rt));
1208 rt.rt_flags = RTF_UP;
1210 memset(&addr, 0, sizeof(addr));
1211 addr.sin6_family = AF_INET6;
1212 addr.sin6_addr = any;
1214 memcpy(&rt.rt_genmask, &addr, sizeof(rt.rt_genmask));
1215 memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
1216 memcpy(&rt.rt_gateway, &addr, sizeof(rt.rt_gateway));
1218 rt.rt_dev = ifr.ifr_name;
1220 err = ioctl(sk, SIOCDELRT, &rt);
1222 connman_error("Removing default interface route failed (%s)",
1229 connman_bool_t connman_inet_compare_subnet(int index, const char *host)
1232 struct in_addr _host_addr;
1233 in_addr_t host_addr, netmask_addr, if_addr;
1234 struct sockaddr_in *netmask, *addr;
1237 DBG("host %s", host);
1242 if (inet_aton(host, &_host_addr) == 0)
1244 host_addr = _host_addr.s_addr;
1246 sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1250 memset(&ifr, 0, sizeof(ifr));
1251 ifr.ifr_ifindex = index;
1253 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
1258 if (ioctl(sk, SIOCGIFNETMASK, &ifr) < 0) {
1263 netmask = (struct sockaddr_in *)&ifr.ifr_netmask;
1264 netmask_addr = netmask->sin_addr.s_addr;
1266 if (ioctl(sk, SIOCGIFADDR, &ifr) < 0) {
1273 addr = (struct sockaddr_in *)&ifr.ifr_addr;
1274 if_addr = addr->sin_addr.s_addr;
1276 return ((if_addr & netmask_addr) == (host_addr & netmask_addr));
1279 int connman_inet_remove_from_bridge(int index, const char *bridge)
1287 sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
1291 memset(&ifr, 0, sizeof(ifr));
1292 strncpy(ifr.ifr_name, bridge, IFNAMSIZ - 1);
1293 ifr.ifr_ifindex = index;
1295 err = ioctl(sk, SIOCBRDELIF, &ifr);
1300 connman_error("Remove interface from bridge error %s",
1308 int connman_inet_add_to_bridge(int index, const char *bridge)
1316 sk = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
1320 memset(&ifr, 0, sizeof(ifr));
1321 strncpy(ifr.ifr_name, bridge, IFNAMSIZ - 1);
1322 ifr.ifr_ifindex = index;
1324 err = ioctl(sk, SIOCBRADDIF, &ifr);
1329 connman_error("Add interface to bridge error %s",
1337 int connman_inet_set_mtu(int index, int mtu)
1342 sk = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1346 memset(&ifr, 0, sizeof(ifr));
1347 ifr.ifr_ifindex = index;
1349 err = ioctl(sk, SIOCGIFNAME, &ifr);
1352 err = ioctl(sk, SIOCSIFMTU, &ifr);
1359 int connman_inet_setup_tunnel(char *tunnel, int mtu)
1369 sk = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1373 index = if_nametoindex(tunnel);
1375 err = connman_inet_set_mtu(index, mtu);
1381 memset(&ifr, 0, sizeof(ifr));
1382 strncpy(ifr.ifr_name, tunnel, IFNAMSIZ);
1383 err = ioctl(sk, SIOCGIFFLAGS, &ifr);
1390 if ((ifr.ifr_flags ^ flags) & mask) {
1391 ifr.ifr_flags &= ~mask;
1392 ifr.ifr_flags |= mask & flags;
1393 err = ioctl(sk, SIOCSIFFLAGS, &ifr);
1395 connman_error("SIOCSIFFLAGS failed: %s",
1404 int connman_inet_create_tunnel(char **iface)
1409 fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
1412 connman_error("Failed to open /dev/net/tun: %s",
1417 memset(&ifr, 0, sizeof(ifr));
1418 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
1420 for (i = 0; i < 256; i++) {
1421 sprintf(ifr.ifr_name, "tun%d", i);
1423 if (!ioctl(fd, TUNSETIFF, (void *)&ifr))
1428 connman_error("Failed to find available tun device");
1433 *iface = g_strdup(ifr.ifr_name);
1439 GIOChannel *channel;
1440 __connman_inet_rs_cb_t callback;
1441 struct sockaddr_in6 addr;
1447 #define CMSG_BUF_LEN 512
1448 #define IN6ADDR_ALL_NODES_MC_INIT \
1449 { { { 0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0,0x1 } } } /* ff02::1 */
1450 #define IN6ADDR_ALL_ROUTERS_MC_INIT \
1451 { { { 0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0,0x2 } } } /* ff02::2 */
1453 static const struct in6_addr in6addr_all_nodes_mc = IN6ADDR_ALL_NODES_MC_INIT;
1454 static const struct in6_addr in6addr_all_routers_mc =
1455 IN6ADDR_ALL_ROUTERS_MC_INIT;
1457 static void rs_cleanup(struct rs_cb_data *data)
1459 if (data->channel != NULL) {
1460 g_io_channel_shutdown(data->channel, TRUE, NULL);
1461 g_io_channel_unref(data->channel);
1462 data->channel = NULL;
1465 if (data->rs_timeout > 0)
1466 g_source_remove(data->rs_timeout);
1468 if (data->watch_id > 0)
1469 g_source_remove(data->watch_id);
1474 static gboolean rs_timeout_cb(gpointer user_data)
1476 struct rs_cb_data *data = user_data;
1478 DBG("user data %p", user_data);
1483 if (data->callback != NULL)
1484 data->callback(NULL, 0, data->user_data);
1486 data->rs_timeout = 0;
1491 static int icmpv6_recv(int fd, gpointer user_data)
1495 unsigned char chdr[CMSG_BUF_LEN];
1496 unsigned char buf[1540];
1497 struct rs_cb_data *data = user_data;
1498 struct nd_router_advert *hdr;
1499 struct sockaddr_in6 saddr;
1504 iov.iov_len = sizeof(buf);
1507 mhdr.msg_name = (void *)&saddr;
1508 mhdr.msg_namelen = sizeof(struct sockaddr_in6);
1509 mhdr.msg_iov = &iov;
1510 mhdr.msg_iovlen = 1;
1511 mhdr.msg_control = (void *)chdr;
1512 mhdr.msg_controllen = CMSG_BUF_LEN;
1514 len = recvmsg(fd, &mhdr, 0);
1516 data->callback(NULL, 0, data->user_data);
1521 hdr = (struct nd_router_advert *)buf;
1522 DBG("code %d len %zd hdr %zd", hdr->nd_ra_code, len,
1523 sizeof(struct nd_router_advert));
1524 if (hdr->nd_ra_code != 0)
1527 data->callback(hdr, len, data->user_data);
1533 static gboolean icmpv6_event(GIOChannel *chan, GIOCondition cond,
1540 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
1543 fd = g_io_channel_unix_get_fd(chan);
1544 ret = icmpv6_recv(fd, data);
1551 /* Adapted from RFC 1071 "C" Implementation Example */
1552 static uint16_t csum(const void *phdr, const void *data, socklen_t datalen)
1554 register unsigned long sum = 0;
1559 /* caller must make sure datalen is even */
1561 addr = (uint16_t *)phdr;
1562 for (i = 0; i < 20; i++)
1566 addr = (uint16_t *)data;
1574 sum = (sum & 0xffff) + (sum >> 16);
1576 return (uint16_t)~sum;
1579 static int ndisc_send_unspec(int type, int oif, const struct in6_addr *dest)
1582 struct in6_addr src;
1583 struct in6_addr dst;
1585 uint8_t reserved[3];
1592 struct icmp6_hdr icmp;
1593 struct nd_neighbor_solicit ns;
1594 struct nd_router_solicit rs;
1599 struct cmsghdr *cmsg;
1600 struct in6_pktinfo *pinfo;
1601 struct sockaddr_in6 dst;
1602 char cbuf[CMSG_SPACE(sizeof(*pinfo))];
1604 int fd, datalen, ret;
1608 fd = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_RAW);
1612 memset(&frame, 0, sizeof(frame));
1613 memset(&dst, 0, sizeof(dst));
1615 datalen = sizeof(frame.i.rs); /* 8, csum() safe */
1616 dst.sin6_addr = *dest;
1618 /* Fill in the IPv6 header */
1619 frame.ip.ip6_vfc = 0x60;
1620 frame.ip.ip6_plen = htons(datalen);
1621 frame.ip.ip6_nxt = IPPROTO_ICMPV6;
1622 frame.ip.ip6_hlim = 255;
1623 frame.ip.ip6_dst = dst.sin6_addr;
1624 /* all other fields are already set to zero */
1626 /* Prepare pseudo header for csum */
1627 memset(&phdr, 0, sizeof(phdr));
1628 phdr.dst = dst.sin6_addr;
1629 phdr.plen = htonl(datalen);
1630 phdr.nxt = IPPROTO_ICMPV6;
1632 /* Fill in remaining ICMP header fields */
1633 frame.i.icmp.icmp6_type = type;
1634 frame.i.icmp.icmp6_cksum = csum(&phdr, &frame.i, datalen);
1636 iov.iov_base = &frame;
1637 iov.iov_len = sizeof(frame.ip) + datalen;
1639 dst.sin6_family = AF_INET6;
1640 msgh.msg_name = &dst;
1641 msgh.msg_namelen = sizeof(dst);
1642 msgh.msg_iov = &iov;
1643 msgh.msg_iovlen = 1;
1646 memset(cbuf, 0, CMSG_SPACE(sizeof(*pinfo)));
1647 cmsg = (struct cmsghdr *)cbuf;
1648 pinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
1649 pinfo->ipi6_ifindex = oif;
1651 cmsg->cmsg_len = CMSG_LEN(sizeof(*pinfo));
1652 cmsg->cmsg_level = IPPROTO_IPV6;
1653 cmsg->cmsg_type = IPV6_PKTINFO;
1654 msgh.msg_control = cmsg;
1655 msgh.msg_controllen = cmsg->cmsg_len;
1657 ret = sendmsg(fd, &msgh, 0);
1663 static inline void ipv6_addr_set(struct in6_addr *addr,
1664 uint32_t w1, uint32_t w2,
1665 uint32_t w3, uint32_t w4)
1667 addr->s6_addr32[0] = w1;
1668 addr->s6_addr32[1] = w2;
1669 addr->s6_addr32[2] = w3;
1670 addr->s6_addr32[3] = w4;
1673 static inline void ipv6_addr_solict_mult(const struct in6_addr *addr,
1674 struct in6_addr *solicited)
1676 ipv6_addr_set(solicited, htonl(0xFF020000), 0, htonl(0x1),
1677 htonl(0xFF000000) | addr->s6_addr32[3]);
1680 static int if_mc_group(int sock, int ifindex, const struct in6_addr *mc_addr,
1683 unsigned int val = 0;
1684 struct ipv6_mreq mreq;
1687 memset(&mreq, 0, sizeof(mreq));
1688 mreq.ipv6mr_interface = ifindex;
1689 mreq.ipv6mr_multiaddr = *mc_addr;
1691 ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1697 return setsockopt(sock, IPPROTO_IPV6, cmd, &mreq, sizeof(mreq));
1700 int __connman_inet_ipv6_send_rs(int index, int timeout,
1701 __connman_inet_rs_cb_t callback, void *user_data)
1703 struct rs_cb_data *data;
1704 struct icmp6_filter filter;
1705 struct in6_addr solicit;
1706 struct in6_addr dst = in6addr_all_routers_mc;
1714 data = g_try_malloc0(sizeof(struct rs_cb_data));
1718 data->callback = callback;
1719 data->user_data = user_data;
1720 data->rs_timeout = g_timeout_add_seconds(timeout, rs_timeout_cb, data);
1722 sk = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
1726 ICMP6_FILTER_SETBLOCKALL(&filter);
1727 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
1729 setsockopt(sk, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
1730 sizeof(struct icmp6_filter));
1732 ipv6_addr_solict_mult(&dst, &solicit);
1733 if_mc_group(sk, index, &in6addr_all_nodes_mc, IPV6_JOIN_GROUP);
1734 if_mc_group(sk, index, &solicit, IPV6_JOIN_GROUP);
1736 data->channel = g_io_channel_unix_new(sk);
1737 g_io_channel_set_close_on_unref(data->channel, TRUE);
1739 g_io_channel_set_encoding(data->channel, NULL, NULL);
1740 g_io_channel_set_buffered(data->channel, FALSE);
1742 data->watch_id = g_io_add_watch(data->channel,
1743 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
1744 icmpv6_event, data);
1746 ndisc_send_unspec(ND_ROUTER_SOLICIT, index, &dst);
1751 GSList *__connman_inet_ipv6_get_prefixes(struct nd_router_advert *hdr,
1752 unsigned int length)
1754 GSList *prefixes = NULL;
1758 if (length <= sizeof(struct nd_router_advert))
1761 len = length - sizeof(struct nd_router_advert);
1762 pos = (uint8_t *)hdr + sizeof(struct nd_router_advert);
1765 struct nd_opt_prefix_info *pinfo;
1766 char prefix_str[INET6_ADDRSTRLEN+1], *str;
1773 optlen = pos[1] << 3;
1774 if (optlen == 0 || optlen > len)
1778 case ND_OPT_PREFIX_INFORMATION:
1779 pinfo = (struct nd_opt_prefix_info *)pos;
1780 prefix = inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1781 prefix_str, INET6_ADDRSTRLEN);
1785 str = g_strdup_printf("%s/%d", prefix,
1786 pinfo->nd_opt_pi_prefix_len);
1787 prefixes = g_slist_append(prefixes, str);
1789 DBG("prefix %s", str);
1801 static int get_dest_addr(int family, int index, char *buf, int len)
1807 sk = socket(family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1811 memset(&ifr, 0, sizeof(ifr));
1812 ifr.ifr_ifindex = index;
1814 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
1815 DBG("SIOCGIFNAME (%d/%s)", errno, strerror(errno));
1820 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
1821 DBG("SIOCGIFFLAGS (%d/%s)", errno, strerror(errno));
1826 if ((ifr.ifr_flags & IFF_POINTOPOINT) == 0) {
1832 DBG("index %d %s", index, ifr.ifr_name);
1834 if (ioctl(sk, SIOCGIFDSTADDR, &ifr) < 0) {
1835 connman_error("Get destination address failed (%s)",
1845 addr = &((struct sockaddr_in *)&ifr.ifr_dstaddr)->sin_addr;
1848 addr = &((struct sockaddr_in6 *)&ifr.ifr_dstaddr)->sin6_addr;
1855 if (inet_ntop(family, addr, buf, len) == NULL) {
1856 DBG("error %d/%s", errno, strerror(errno));
1863 int connman_inet_get_dest_addr(int index, char **dest)
1865 char addr[INET_ADDRSTRLEN];
1868 ret = get_dest_addr(PF_INET, index, addr, INET_ADDRSTRLEN);
1872 *dest = g_strdup(addr);
1874 DBG("destination %s", *dest);
1879 int connman_inet_ipv6_get_dest_addr(int index, char **dest)
1881 char addr[INET6_ADDRSTRLEN];
1884 ret = get_dest_addr(PF_INET6, index, addr, INET6_ADDRSTRLEN);
1888 *dest = g_strdup(addr);
1890 DBG("destination %s", *dest);
1895 int __connman_inet_rtnl_open(struct __connman_inet_rtnl_handle *rth)
1898 int rcvbuf = 1024 * 4;
1900 rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
1902 connman_error("Can not open netlink socket: %s",
1907 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf,
1908 sizeof(sndbuf)) < 0) {
1909 connman_error("SO_SNDBUF: %s", strerror(errno));
1913 if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf,
1914 sizeof(rcvbuf)) < 0) {
1915 connman_error("SO_RCVBUF: %s", strerror(errno));
1919 memset(&rth->local, 0, sizeof(rth->local));
1920 rth->local.nl_family = AF_NETLINK;
1921 rth->local.nl_groups = 0;
1923 if (bind(rth->fd, (struct sockaddr *)&rth->local,
1924 sizeof(rth->local)) < 0) {
1925 connman_error("Can not bind netlink socket: %s",
1930 rth->seq = time(NULL);
1932 DBG("fd %d", rth->fd);
1937 struct inet_rtnl_cb_data {
1938 GIOChannel *channel;
1939 __connman_inet_rtnl_cb_t callback;
1942 struct __connman_inet_rtnl_handle *rtnl;
1946 static void inet_rtnl_cleanup(struct inet_rtnl_cb_data *data)
1948 struct __connman_inet_rtnl_handle *rth = data->rtnl;
1950 if (data->channel != NULL) {
1951 g_io_channel_shutdown(data->channel, TRUE, NULL);
1952 g_io_channel_unref(data->channel);
1953 data->channel = NULL;
1956 DBG("data %p", data);
1958 if (data->rtnl_timeout > 0)
1959 g_source_remove(data->rtnl_timeout);
1961 if (data->watch_id > 0)
1962 g_source_remove(data->watch_id);
1965 __connman_inet_rtnl_close(rth);
1972 static gboolean inet_rtnl_timeout_cb(gpointer user_data)
1974 struct inet_rtnl_cb_data *data = user_data;
1976 DBG("user data %p", user_data);
1981 if (data->callback != NULL)
1982 data->callback(NULL, data->user_data);
1984 data->rtnl_timeout = 0;
1985 inet_rtnl_cleanup(data);
1989 static int inet_rtnl_recv(GIOChannel *chan, gpointer user_data)
1991 struct inet_rtnl_cb_data *rtnl_data = user_data;
1992 struct __connman_inet_rtnl_handle *rth = rtnl_data->rtnl;
1993 struct nlmsghdr *h = NULL;
1994 unsigned char buf[4096];
1999 memset(buf, 0, sizeof(buf));
2001 status = g_io_channel_read_chars(chan, (gchar *) buf,
2002 sizeof(buf), &len, NULL);
2004 DBG("status %d", status);
2007 case G_IO_STATUS_NORMAL:
2009 case G_IO_STATUS_AGAIN:
2016 struct nlmsgerr *err;
2020 if (!NLMSG_OK(h, len)) {
2025 if (h->nlmsg_seq != rth->seq) {
2027 DBG("skip %d/%d len %d", rth->seq,
2028 h->nlmsg_seq, h->nlmsg_len);
2030 len -= h->nlmsg_len;
2031 ptr += h->nlmsg_len;
2035 switch (h->nlmsg_type) {
2041 err = (struct nlmsgerr *)NLMSG_DATA(h);
2042 connman_error("RTNETLINK answers %s (%d)",
2043 strerror(-err->error), -err->error);
2050 if (h->nlmsg_seq == rth->seq) {
2051 DBG("received %d seq %d", h->nlmsg_len, h->nlmsg_seq);
2053 rtnl_data->callback(h, rtnl_data->user_data);
2055 if (rtnl_data->rtnl_timeout > 0) {
2056 g_source_remove(rtnl_data->rtnl_timeout);
2057 rtnl_data->rtnl_timeout = 0;
2060 __connman_inet_rtnl_close(rth);
2067 static gboolean inet_rtnl_event(GIOChannel *chan, GIOCondition cond,
2074 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
2077 ret = inet_rtnl_recv(chan, user_data);
2084 int __connman_inet_rtnl_talk(struct __connman_inet_rtnl_handle *rtnl,
2085 struct nlmsghdr *n, int timeout,
2086 __connman_inet_rtnl_cb_t callback, void *user_data)
2088 struct sockaddr_nl nladdr;
2089 struct inet_rtnl_cb_data *data;
2093 memset(&nladdr, 0, sizeof(nladdr));
2094 nladdr.nl_family = AF_NETLINK;
2096 n->nlmsg_seq = seq = ++rtnl->seq;
2098 if (callback != NULL) {
2099 data = g_try_malloc0(sizeof(struct inet_rtnl_cb_data));
2103 data->callback = callback;
2104 data->user_data = user_data;
2106 data->rtnl_timeout = g_timeout_add_seconds(timeout,
2107 inet_rtnl_timeout_cb, data);
2109 data->channel = g_io_channel_unix_new(rtnl->fd);
2110 g_io_channel_set_close_on_unref(data->channel, TRUE);
2112 g_io_channel_set_encoding(data->channel, NULL, NULL);
2113 g_io_channel_set_buffered(data->channel, FALSE);
2115 data->watch_id = g_io_add_watch(data->channel,
2116 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
2117 inet_rtnl_event, data);
2119 n->nlmsg_flags |= NLM_F_ACK;
2121 err = sendto(rtnl->fd, &rtnl->req.n, rtnl->req.n.nlmsg_len, 0,
2122 (struct sockaddr *) &nladdr, sizeof(nladdr));
2123 DBG("handle %p len %d err %d", rtnl, rtnl->req.n.nlmsg_len, err);
2125 connman_error("Can not talk to rtnetlink");
2129 if ((unsigned int)err != rtnl->req.n.nlmsg_len) {
2130 connman_error("Sent %d bytes, msg truncated", err);
2137 void __connman_inet_rtnl_close(struct __connman_inet_rtnl_handle *rth)
2139 DBG("handle %p", rth);
2147 int __connman_inet_rtnl_addattr32(struct nlmsghdr *n, size_t maxlen, int type,
2150 int len = RTA_LENGTH(4);
2153 if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) {
2154 DBG("Error! max allowed bound %d exceeded", maxlen);
2157 rta = NLMSG_TAIL(n);
2158 rta->rta_type = type;
2160 memcpy(RTA_DATA(rta), &data, 4);
2161 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;