5 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
6 * Copyright (C) Alexey Kuznetsov et al. from iproute2 package.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
34 #include <linux/if_tunnel.h>
35 #include <linux/netlink.h>
36 #include <linux/rtnetlink.h>
37 #include <sys/ioctl.h>
41 #include <connman/log.h>
42 #include <connman/ipconfig.h>
43 #include "gweb/gweb.h"
45 static int tunnel_created;
46 static int tunnel_pending;
47 static char *tunnel_ip_address;
49 static guint web_request_id;
51 #define STATUS_URL "http://ipv6.google.com/"
53 #define NLMSG_TAIL(nmsg) \
54 ((struct rtattr *) (((void *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
57 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
62 struct sockaddr_nl local;
63 struct sockaddr_nl peer;
68 static int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
70 int len = RTA_LENGTH(4);
72 if (NLMSG_ALIGN(n->nlmsg_len) + len > (unsigned int)maxlen) {
73 DBG("Error! max allowed bound %d exceeded", maxlen);
79 memcpy(RTA_DATA(rta), &data, 4);
80 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
85 static int addattr_l(struct nlmsghdr *n, int maxlen, int type,
86 const void *data, int alen)
88 int len = RTA_LENGTH(alen);
91 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) >
92 (unsigned int)maxlen) {
93 DBG("addattr_l message exceeded bound of %d", maxlen);
99 memcpy(RTA_DATA(rta), data, alen);
100 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
105 static void rtnl_close(struct rtnl_handle *rth)
113 static int rtnl_open(struct rtnl_handle *rth)
118 memset(rth, 0, sizeof(*rth));
120 rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
122 connman_error("Can not open netlink socket: %s",
127 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf,
128 sizeof(sndbuf)) < 0) {
129 connman_error("SO_SNDBUF: %s", strerror(errno));
133 memset(&rth->local, 0, sizeof(rth->local));
134 rth->local.nl_family = AF_NETLINK;
135 rth->local.nl_groups = 0;
137 if (bind(rth->fd, (struct sockaddr *)&rth->local,
138 sizeof(rth->local)) < 0) {
139 connman_error("Can not bind netlink socket: %s",
143 addr_len = sizeof(rth->local);
144 if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
146 connman_error("Can not getsockname: %s", strerror(errno));
149 if (addr_len != sizeof(rth->local)) {
150 connman_error("Wrong address length %d", addr_len);
153 if (rth->local.nl_family != AF_NETLINK) {
154 connman_error("Wrong address family %d", rth->local.nl_family);
157 rth->seq = time(NULL);
162 static int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n)
164 struct sockaddr_nl nladdr;
166 .iov_base = (void *)n,
167 .iov_len = n->nlmsg_len
169 struct msghdr msg = {
171 .msg_namelen = sizeof(nladdr),
178 memset(&nladdr, 0, sizeof(nladdr));
179 nladdr.nl_family = AF_NETLINK;
181 n->nlmsg_seq = seq = ++rtnl->seq;
182 n->nlmsg_flags |= NLM_F_ACK;
184 err = sendmsg(rtnl->fd, &msg, 0);
186 connman_error("Can not talk to rtnetlink");
193 static int tunnel_create(struct in_addr *addr)
195 struct ip_tunnel_parm p;
200 /* ip tunnel add tun6to4 mode sit remote any local 1.2.3.4 ttl 64 */
202 memset(&p, 0, sizeof(struct ip_tunnel_parm));
203 memset(&ifr, 0, sizeof(struct ifreq));
207 p.iph.frag_off = htons(IP_DF);
208 p.iph.protocol = IPPROTO_IPV6;
209 p.iph.saddr = addr->s_addr;
211 strncpy(p.name, "tun6to4", IFNAMSIZ);
213 strncpy(ifr.ifr_name, "sit0", IFNAMSIZ);
214 ifr.ifr_ifru.ifru_data = (void *)&p;
215 fd = socket(AF_INET, SOCK_DGRAM, 0);
216 ret = ioctl(fd, SIOCADDTUNNEL, &ifr);
218 connman_error("add tunnel %s failed: %s", ifr.ifr_name,
225 static void tunnel_destroy()
227 struct ip_tunnel_parm p;
232 if (tunnel_created == 0)
235 /* ip tunnel del tun6to4 */
237 memset(&p, 0, sizeof(struct ip_tunnel_parm));
238 memset(&ifr, 0, sizeof(struct ifreq));
242 p.iph.protocol = IPPROTO_IPV6;
243 strncpy(p.name, "tun6to4", IFNAMSIZ);
245 strncpy(ifr.ifr_name, "tun6to4", IFNAMSIZ);
246 ifr.ifr_ifru.ifru_data = (void *)&p;
247 fd = socket(AF_INET, SOCK_DGRAM, 0);
249 connman_error("socket failed: %s", strerror(errno));
253 ret = ioctl(fd, SIOCDELTUNNEL, &ifr);
255 connman_error("del tunnel %s failed: %s", ifr.ifr_name,
263 g_free(tunnel_ip_address);
264 tunnel_ip_address = NULL;
267 static int tunnel_add_route()
269 struct rtnl_handle rth;
270 struct in6_addr addr6;
280 /* ip -6 route add ::/0 via ::192.88.99.1 dev tun6to4 metric 1 */
282 index = if_nametoindex("tun6to4");
284 DBG("Can not find device tun6to4");
288 memset(&req, 0, sizeof(req));
290 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
291 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
292 req.n.nlmsg_type = RTM_NEWROUTE;
293 req.r.rtm_family = AF_INET6;
294 req.r.rtm_table = RT_TABLE_MAIN;
295 req.r.rtm_protocol = RTPROT_BOOT;
296 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
297 req.r.rtm_type = RTN_UNICAST;
298 req.r.rtm_dst_len = 0;
300 inet_pton(AF_INET6, "::192.88.99.1", &addr6);
302 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr6.s6_addr, 16);
303 addattr32(&req.n, sizeof(req), RTA_OIF, index);
304 addattr32(&req.n, sizeof(req), RTA_PRIORITY, 1);
306 ret = rtnl_open(&rth);
310 ret = rtnl_talk(&rth, &req.n);
317 static int tunnel_set_addr(unsigned int a, unsigned int b,
318 unsigned int c, unsigned int d)
320 struct rtnl_handle rth;
321 struct in6_addr addr6;
327 struct ifaddrmsg ifa;
331 /* ip -6 addr add dev tun6to4 2002:0102:0304::1/64 */
333 memset(&req, 0, sizeof(req));
335 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
336 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
337 req.n.nlmsg_type = RTM_NEWADDR;
338 req.ifa.ifa_family = AF_INET6;
339 req.ifa.ifa_prefixlen = 64;
340 req.ifa.ifa_index = if_nametoindex("tun6to4");
341 if (req.ifa.ifa_index == 0) {
342 connman_error("Can not find device tun6to4");
347 ip6addr = g_strdup_printf("2002:%02x%02x:%02x%02x::1", a, b, c, d);
348 inet_pton(AF_INET6, ip6addr, &addr6);
349 DBG("ipv6 address %s", ip6addr);
352 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &addr6.s6_addr, 16);
353 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &addr6.s6_addr, 16);
355 ret = rtnl_open(&rth);
359 ret = rtnl_talk(&rth, &req.n);
366 static gboolean unref_web(gpointer user_data)
372 static gboolean web_result(GWebResult *result, gpointer user_data)
376 if (web_request_id == 0)
379 status = g_web_result_get_status(result);
381 DBG("status %u", status);
383 if (status >= 400 && status < 500)
390 g_timeout_add_seconds(1, unref_web, NULL);
395 static int init_6to4(struct in_addr *ip4addr)
397 unsigned int a, b, c, d;
403 addr = ntohl(ip4addr->s_addr);
405 a = (addr & 0xff000000) >> 24;
406 b = (addr & 0x00ff0000) >> 16;
407 c = (addr & 0x0000ff00) >> 8;
408 d = addr & 0x000000ff;
410 ret = tunnel_create(ip4addr);
416 ret = connman_inet_setup_tunnel("tun6to4", 1472);
420 ret = tunnel_set_addr(a, b, c, d);
424 ret = tunnel_add_route();
428 /* We try to verify that connectivity through tunnel works ok.
434 g_web_set_accept(web, NULL);
435 g_web_set_user_agent(web, "ConnMan/%s", VERSION);
436 g_web_set_close_connection(web, TRUE);
438 web_request_id = g_web_request_get(web, STATUS_URL, web_result, NULL);
447 static void receive_rs_reply(struct nd_router_advert *reply, void *user_data)
449 char *address = user_data;
450 struct in_addr ip4addr;
452 DBG("reply %p address %s", reply, address);
454 /* We try to create tunnel if autoconfiguration did not work i.e.,
455 * we did not receive any reply to router solicitation message.
457 if (reply == NULL && inet_aton(address, &ip4addr) != 0)
463 int __connman_6to4_probe(struct connman_service *service)
465 struct connman_ipconfig *ip4config, *ip6config;
466 enum connman_ipconfig_method method;
467 unsigned int a, b, c, d;
468 struct in_addr ip4addr;
474 DBG("service %p", service);
476 if (tunnel_created || tunnel_pending)
482 ip4config = __connman_service_get_ip4config(service);
483 if (ip4config == NULL)
486 ip6config = __connman_service_get_ip6config(service);
487 if (ip6config == NULL)
490 method = __connman_ipconfig_get_method(ip6config);
491 if (method != CONNMAN_IPCONFIG_METHOD_AUTO)
494 address = __connman_ipconfig_get_local(ip4config);
498 if (inet_aton(address, &ip4addr) == 0)
501 addr = ntohl(ip4addr.s_addr);
503 a = (addr & 0xff000000) >> 24;
504 b = (addr & 0x00ff0000) >> 16;
505 c = (addr & 0x0000ff00) >> 8;
506 d = addr & 0x000000ff;
508 /* 6to4 tunnel is only usable if we have a public IPv4 address */
509 if (a == 10 || (a == 192 && b == 168) ||
510 (a == 172 && (b >= 16 && b <= 31)))
513 index = connman_ipconfig_get_index(ip4config);
514 ip_address = g_strdup(address);
517 g_free(tunnel_ip_address);
518 tunnel_ip_address = g_strdup(address);
520 return __connman_inet_ipv6_send_rs(index, 2, receive_rs_reply,
524 void __connman_6to4_remove(struct connman_ipconfig *ip4config)
528 DBG("tunnel ip address %s", tunnel_ip_address);
530 if (ip4config == NULL)
533 address = __connman_ipconfig_get_local(ip4config);
537 if (g_strcmp0(address, tunnel_ip_address) != 0)
544 int __connman_6to4_check(struct connman_ipconfig *ip4config)
548 if (ip4config == NULL || tunnel_created == 0 ||
552 DBG("tunnel ip address %s", tunnel_ip_address);
554 address = __connman_ipconfig_get_local(ip4config);
558 if (g_strcmp0(address, tunnel_ip_address) == 0)