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
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
36 #include <linux/if_tunnel.h>
37 #include <linux/netlink.h>
38 #include <linux/rtnetlink.h>
39 #include <sys/ioctl.h>
43 #include <connman/log.h>
44 #include <connman/ipconfig.h>
45 #include "gweb/gweb.h"
47 static int tunnel_created;
48 static int tunnel_pending;
49 static char *tunnel_ip_address;
51 static guint web_request_id;
52 static unsigned int newlink_watch;
53 static unsigned int newlink_flags;
54 static int newlink_timeout_id;
56 #define STATUS_URL "http://ipv6.connman.net/online/status.html"
59 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
62 static int tunnel_create(struct in_addr *addr)
64 struct ip_tunnel_parm p;
69 /* ip tunnel add tun6to4 mode sit remote any local 1.2.3.4 ttl 64 */
71 memset(&p, 0, sizeof(struct ip_tunnel_parm));
72 memset(&ifr, 0, sizeof(struct ifreq));
76 p.iph.frag_off = htons(IP_DF);
77 p.iph.protocol = IPPROTO_IPV6;
78 p.iph.saddr = addr->s_addr;
80 strncpy(p.name, "tun6to4", sizeof(p.name) - 1);
82 strncpy(ifr.ifr_name, "sit0", sizeof(ifr.ifr_name) - 1);
83 ifr.ifr_ifru.ifru_data = (void *)&p;
84 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
87 ret = ioctl(fd, SIOCADDTUNNEL, &ifr);
89 connman_error("add tunnel %s failed: %s", ifr.ifr_name,
96 static void tunnel_destroy(void)
98 struct ip_tunnel_parm p;
103 if (tunnel_created == 0)
106 /* ip tunnel del tun6to4 */
108 memset(&p, 0, sizeof(struct ip_tunnel_parm));
109 memset(&ifr, 0, sizeof(struct ifreq));
113 p.iph.protocol = IPPROTO_IPV6;
114 strncpy(p.name, "tun6to4", sizeof(p.name) - 1);
116 strncpy(ifr.ifr_name, "tun6to4", sizeof(ifr.ifr_name) - 1);
117 ifr.ifr_ifru.ifru_data = (void *)&p;
118 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
120 connman_error("socket failed: %s", strerror(errno));
124 ret = ioctl(fd, SIOCDELTUNNEL, &ifr);
126 connman_error("del tunnel %s failed: %s", ifr.ifr_name,
134 g_free(tunnel_ip_address);
135 tunnel_ip_address = NULL;
138 static int tunnel_add_route(void)
140 struct __connman_inet_rtnl_handle rth;
141 struct in6_addr addr6;
145 /* ip -6 route add ::/0 via ::192.88.99.1 dev tun6to4 metric 1 */
147 index = if_nametoindex("tun6to4");
149 DBG("Can not find device tun6to4");
153 memset(&rth, 0, sizeof(rth));
155 rth.req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
156 rth.req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
157 rth.req.n.nlmsg_type = RTM_NEWROUTE;
158 rth.req.u.r.rt.rtm_family = AF_INET6;
159 rth.req.u.r.rt.rtm_table = RT_TABLE_MAIN;
160 rth.req.u.r.rt.rtm_protocol = RTPROT_BOOT;
161 rth.req.u.r.rt.rtm_scope = RT_SCOPE_UNIVERSE;
162 rth.req.u.r.rt.rtm_type = RTN_UNICAST;
163 rth.req.u.r.rt.rtm_dst_len = 0;
165 inet_pton(AF_INET6, "::192.88.99.1", &addr6);
167 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), RTA_GATEWAY,
169 __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req), RTA_OIF,
171 __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req),
174 ret = __connman_inet_rtnl_open(&rth);
178 ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
181 __connman_inet_rtnl_close(&rth);
185 static int tunnel_set_addr(unsigned int a, unsigned int b,
186 unsigned int c, unsigned int d)
188 struct __connman_inet_rtnl_handle rth;
189 struct in6_addr addr6;
193 /* ip -6 addr add dev tun6to4 2002:0102:0304::1/64 */
195 memset(&rth, 0, sizeof(rth));
197 rth.req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
198 rth.req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
199 rth.req.n.nlmsg_type = RTM_NEWADDR;
200 rth.req.u.i.ifa.ifa_family = AF_INET6;
201 rth.req.u.i.ifa.ifa_prefixlen = 64;
202 rth.req.u.i.ifa.ifa_index = if_nametoindex("tun6to4");
203 if (rth.req.u.i.ifa.ifa_index == 0) {
204 connman_error("Can not find device tun6to4");
209 ip6addr = g_strdup_printf("2002:%02x%02x:%02x%02x::1", a, b, c, d);
210 inet_pton(AF_INET6, ip6addr, &addr6);
211 DBG("ipv6 address %s", ip6addr);
214 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_LOCAL,
216 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_ADDRESS,
219 ret = __connman_inet_rtnl_open(&rth);
223 ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
226 __connman_inet_rtnl_close(&rth);
230 static gboolean unref_web(gpointer user_data)
236 static bool web_result(GWebResult *result, gpointer user_data)
240 if (web_request_id == 0)
243 status = g_web_result_get_status(result);
245 DBG("status %u", status);
247 if (status >= 400 && status < 500)
254 g_timeout_add_seconds(1, unref_web, NULL);
259 static void web_debug(const char *str, void *data)
261 connman_info("%s: %s\n", (const char *) data, str);
264 static gboolean newlink_timeout(gpointer user_data)
267 * Stop if the timeout has been cancelled already by tun_newlink()
269 if (newlink_timeout_id == 0)
274 if (newlink_watch != 0) {
275 connman_rtnl_remove_watch(newlink_watch);
281 if (web_request_id == 0)
284 newlink_timeout_id = 0;
289 static void tun_newlink(unsigned flags, unsigned change, void *user_data)
291 int index = GPOINTER_TO_INT(user_data);
293 if ((newlink_flags & IFF_UP) == (flags & IFF_UP)) {
294 newlink_flags = flags;
298 if (flags & IFF_UP) {
300 * We try to verify that connectivity through tunnel works ok.
302 if (newlink_timeout_id > 0) {
303 g_source_remove(newlink_timeout_id);
304 newlink_timeout_id = 0;
307 web = g_web_new(index);
313 g_web_set_accept(web, NULL);
314 g_web_set_user_agent(web, "ConnMan/%s", VERSION);
315 g_web_set_close_connection(web, TRUE);
317 if (getenv("CONNMAN_WEB_DEBUG"))
318 g_web_set_debug(web, web_debug, "6to4");
320 web_request_id = g_web_request_get(web, STATUS_URL,
321 web_result, NULL, NULL);
323 newlink_timeout(NULL);
326 newlink_flags = flags;
329 static int init_6to4(struct in_addr *ip4addr)
331 unsigned int a, b, c, d;
337 addr = ntohl(ip4addr->s_addr);
339 a = (addr & 0xff000000) >> 24;
340 b = (addr & 0x00ff0000) >> 16;
341 c = (addr & 0x0000ff00) >> 8;
342 d = addr & 0x000000ff;
344 ret = tunnel_create(ip4addr);
350 ret = connman_inet_setup_tunnel("tun6to4", 1472);
354 ret = tunnel_set_addr(a, b, c, d);
358 ret = tunnel_add_route();
362 if_index = connman_inet_ifindex("tun6to4");
366 newlink_watch = connman_rtnl_add_newlink_watch(if_index,
367 tun_newlink, GINT_TO_POINTER(if_index));
369 newlink_timeout_id = g_timeout_add_seconds(1, newlink_timeout, NULL);
378 static void receive_rs_reply(struct nd_router_advert *reply,
379 unsigned int length, void *user_data)
381 char *address = user_data;
382 struct in_addr ip4addr;
384 DBG("reply %p len %d address %s", reply, length, address);
386 /* We try to create tunnel if autoconfiguration did not work i.e.,
387 * we did not receive any reply to router solicitation message.
389 if (!reply && inet_aton(address, &ip4addr) != 0)
395 int __connman_6to4_probe(struct connman_service *service)
397 struct connman_ipconfig *ip4config, *ip6config;
398 enum connman_ipconfig_method method;
400 struct in_addr ip4addr;
406 DBG("service %p", service);
408 if (tunnel_created || tunnel_pending)
414 ip4config = __connman_service_get_ip4config(service);
418 ip6config = __connman_service_get_ip6config(service);
422 method = __connman_ipconfig_get_method(ip6config);
423 if (method != CONNMAN_IPCONFIG_METHOD_AUTO)
426 address = __connman_ipconfig_get_local(ip4config);
430 if (inet_aton(address, &ip4addr) == 0)
433 addr = ntohl(ip4addr.s_addr);
435 a = (addr & 0xff000000) >> 24;
436 b = (addr & 0x00ff0000) >> 16;
438 /* 6to4 tunnel is only usable if we have a public IPv4 address */
439 if (a == 10 || (a == 192 && b == 168) ||
440 (a == 172 && (b >= 16 && b <= 31)))
443 index = __connman_ipconfig_get_index(ip4config);
444 ip_address = g_strdup(address);
447 g_free(tunnel_ip_address);
448 tunnel_ip_address = g_strdup(address);
450 return __connman_inet_ipv6_send_rs(index, 2, receive_rs_reply,
454 void __connman_6to4_remove(struct connman_ipconfig *ip4config)
458 DBG("tunnel ip address %s", tunnel_ip_address);
463 address = __connman_ipconfig_get_local(ip4config);
467 if (g_strcmp0(address, tunnel_ip_address) != 0)
474 int __connman_6to4_check(struct connman_ipconfig *ip4config)
478 if (!ip4config || tunnel_created == 0 ||
482 DBG("tunnel ip address %s", tunnel_ip_address);
484 address = __connman_ipconfig_get_local(ip4config);
488 if (g_strcmp0(address, tunnel_ip_address) == 0)