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", IFNAMSIZ);
82 strncpy(ifr.ifr_name, "sit0", IFNAMSIZ);
83 ifr.ifr_ifru.ifru_data = (void *)&p;
84 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
85 ret = ioctl(fd, SIOCADDTUNNEL, &ifr);
87 connman_error("add tunnel %s failed: %s", ifr.ifr_name,
94 static void tunnel_destroy()
96 struct ip_tunnel_parm p;
101 if (tunnel_created == 0)
104 /* ip tunnel del tun6to4 */
106 memset(&p, 0, sizeof(struct ip_tunnel_parm));
107 memset(&ifr, 0, sizeof(struct ifreq));
111 p.iph.protocol = IPPROTO_IPV6;
112 strncpy(p.name, "tun6to4", IFNAMSIZ);
114 strncpy(ifr.ifr_name, "tun6to4", IFNAMSIZ);
115 ifr.ifr_ifru.ifru_data = (void *)&p;
116 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
118 connman_error("socket failed: %s", strerror(errno));
122 ret = ioctl(fd, SIOCDELTUNNEL, &ifr);
124 connman_error("del tunnel %s failed: %s", ifr.ifr_name,
132 g_free(tunnel_ip_address);
133 tunnel_ip_address = NULL;
136 static int tunnel_add_route()
138 struct __connman_inet_rtnl_handle rth;
139 struct in6_addr addr6;
143 /* ip -6 route add ::/0 via ::192.88.99.1 dev tun6to4 metric 1 */
145 index = if_nametoindex("tun6to4");
147 DBG("Can not find device tun6to4");
151 memset(&rth, 0, sizeof(rth));
153 rth.req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
154 rth.req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
155 rth.req.n.nlmsg_type = RTM_NEWROUTE;
156 rth.req.u.r.rt.rtm_family = AF_INET6;
157 rth.req.u.r.rt.rtm_table = RT_TABLE_MAIN;
158 rth.req.u.r.rt.rtm_protocol = RTPROT_BOOT;
159 rth.req.u.r.rt.rtm_scope = RT_SCOPE_UNIVERSE;
160 rth.req.u.r.rt.rtm_type = RTN_UNICAST;
161 rth.req.u.r.rt.rtm_dst_len = 0;
163 inet_pton(AF_INET6, "::192.88.99.1", &addr6);
165 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), RTA_GATEWAY,
167 __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req), RTA_OIF,
169 __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req),
172 ret = __connman_inet_rtnl_open(&rth);
176 ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
179 __connman_inet_rtnl_close(&rth);
183 static int tunnel_set_addr(unsigned int a, unsigned int b,
184 unsigned int c, unsigned int d)
186 struct __connman_inet_rtnl_handle rth;
187 struct in6_addr addr6;
191 /* ip -6 addr add dev tun6to4 2002:0102:0304::1/64 */
193 memset(&rth, 0, sizeof(rth));
195 rth.req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
196 rth.req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
197 rth.req.n.nlmsg_type = RTM_NEWADDR;
198 rth.req.u.i.ifa.ifa_family = AF_INET6;
199 rth.req.u.i.ifa.ifa_prefixlen = 64;
200 rth.req.u.i.ifa.ifa_index = if_nametoindex("tun6to4");
201 if (rth.req.u.i.ifa.ifa_index == 0) {
202 connman_error("Can not find device tun6to4");
207 ip6addr = g_strdup_printf("2002:%02x%02x:%02x%02x::1", a, b, c, d);
208 inet_pton(AF_INET6, ip6addr, &addr6);
209 DBG("ipv6 address %s", ip6addr);
212 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_LOCAL,
214 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_ADDRESS,
217 ret = __connman_inet_rtnl_open(&rth);
221 ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
224 __connman_inet_rtnl_close(&rth);
228 static gboolean unref_web(gpointer user_data)
234 static gboolean web_result(GWebResult *result, gpointer user_data)
238 if (web_request_id == 0)
241 status = g_web_result_get_status(result);
243 DBG("status %u", status);
245 if (status >= 400 && status < 500)
252 g_timeout_add_seconds(1, unref_web, NULL);
257 static void web_debug(const char *str, void *data)
259 connman_info("%s: %s\n", (const char *) data, str);
262 static gboolean newlink_timeout(gpointer user_data)
265 * Stop if the timeout has been cancelled already by tun_newlink()
267 if (newlink_timeout_id == 0)
272 if (newlink_watch != 0) {
273 connman_rtnl_remove_watch(newlink_watch);
279 if (web_request_id == 0)
282 newlink_timeout_id = 0;
287 static void tun_newlink(unsigned flags, unsigned change, void *user_data)
289 int index = GPOINTER_TO_INT(user_data);
291 if ((newlink_flags & IFF_UP) == (flags & IFF_UP)) {
292 newlink_flags = flags;
296 if (flags & IFF_UP) {
298 * We try to verify that connectivity through tunnel works ok.
300 if (newlink_timeout_id > 0) {
301 g_source_remove(newlink_timeout_id);
302 newlink_timeout_id = 0;
305 web = g_web_new(index);
311 g_web_set_accept(web, NULL);
312 g_web_set_user_agent(web, "ConnMan/%s", VERSION);
313 g_web_set_close_connection(web, TRUE);
315 if (getenv("CONNMAN_WEB_DEBUG"))
316 g_web_set_debug(web, web_debug, "6to4");
318 web_request_id = g_web_request_get(web, STATUS_URL,
321 newlink_timeout(NULL);
324 newlink_flags = flags;
327 static int init_6to4(struct in_addr *ip4addr)
329 unsigned int a, b, c, d;
335 addr = ntohl(ip4addr->s_addr);
337 a = (addr & 0xff000000) >> 24;
338 b = (addr & 0x00ff0000) >> 16;
339 c = (addr & 0x0000ff00) >> 8;
340 d = addr & 0x000000ff;
342 ret = tunnel_create(ip4addr);
348 ret = connman_inet_setup_tunnel("tun6to4", 1472);
352 ret = tunnel_set_addr(a, b, c, d);
356 ret = tunnel_add_route();
360 if_index = connman_inet_ifindex("tun6to4");
364 newlink_watch = connman_rtnl_add_newlink_watch(if_index,
365 tun_newlink, GINT_TO_POINTER(if_index));
367 newlink_timeout_id = g_timeout_add_seconds(1, newlink_timeout, NULL);
376 static void receive_rs_reply(struct nd_router_advert *reply,
377 unsigned int length, void *user_data)
379 char *address = user_data;
380 struct in_addr ip4addr;
382 DBG("reply %p len %d address %s", reply, length, address);
384 /* We try to create tunnel if autoconfiguration did not work i.e.,
385 * we did not receive any reply to router solicitation message.
387 if (reply == NULL && inet_aton(address, &ip4addr) != 0)
393 int __connman_6to4_probe(struct connman_service *service)
395 struct connman_ipconfig *ip4config, *ip6config;
396 enum connman_ipconfig_method method;
398 struct in_addr ip4addr;
404 DBG("service %p", service);
406 if (tunnel_created || tunnel_pending)
412 ip4config = __connman_service_get_ip4config(service);
413 if (ip4config == NULL)
416 ip6config = __connman_service_get_ip6config(service);
417 if (ip6config == NULL)
420 method = __connman_ipconfig_get_method(ip6config);
421 if (method != CONNMAN_IPCONFIG_METHOD_AUTO)
424 address = __connman_ipconfig_get_local(ip4config);
428 if (inet_aton(address, &ip4addr) == 0)
431 addr = ntohl(ip4addr.s_addr);
433 a = (addr & 0xff000000) >> 24;
434 b = (addr & 0x00ff0000) >> 16;
436 /* 6to4 tunnel is only usable if we have a public IPv4 address */
437 if (a == 10 || (a == 192 && b == 168) ||
438 (a == 172 && (b >= 16 && b <= 31)))
441 index = __connman_ipconfig_get_index(ip4config);
442 ip_address = g_strdup(address);
445 g_free(tunnel_ip_address);
446 tunnel_ip_address = g_strdup(address);
448 return __connman_inet_ipv6_send_rs(index, 2, receive_rs_reply,
452 void __connman_6to4_remove(struct connman_ipconfig *ip4config)
456 DBG("tunnel ip address %s", tunnel_ip_address);
458 if (ip4config == NULL)
461 address = __connman_ipconfig_get_local(ip4config);
465 if (g_strcmp0(address, tunnel_ip_address) != 0)
472 int __connman_6to4_check(struct connman_ipconfig *ip4config)
476 if (ip4config == NULL || tunnel_created == 0 ||
480 DBG("tunnel ip address %s", tunnel_ip_address);
482 address = __connman_ipconfig_get_local(ip4config);
486 if (g_strcmp0(address, tunnel_ip_address) == 0)