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
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
35 #include <linux/if_tunnel.h>
36 #include <linux/netlink.h>
37 #include <linux/rtnetlink.h>
38 #include <sys/ioctl.h>
42 #include <connman/log.h>
43 #include <connman/ipconfig.h>
44 #include "gweb/gweb.h"
46 static int tunnel_created;
47 static int tunnel_pending;
48 static char *tunnel_ip_address;
50 static guint web_request_id;
52 #define STATUS_URL "http://ipv6.connman.net/online/status.html"
55 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
58 static int tunnel_create(struct in_addr *addr)
60 struct ip_tunnel_parm p;
65 /* ip tunnel add tun6to4 mode sit remote any local 1.2.3.4 ttl 64 */
67 memset(&p, 0, sizeof(struct ip_tunnel_parm));
68 memset(&ifr, 0, sizeof(struct ifreq));
72 p.iph.frag_off = htons(IP_DF);
73 p.iph.protocol = IPPROTO_IPV6;
74 p.iph.saddr = addr->s_addr;
76 strncpy(p.name, "tun6to4", IFNAMSIZ);
78 strncpy(ifr.ifr_name, "sit0", IFNAMSIZ);
79 ifr.ifr_ifru.ifru_data = (void *)&p;
80 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
81 ret = ioctl(fd, SIOCADDTUNNEL, &ifr);
83 connman_error("add tunnel %s failed: %s", ifr.ifr_name,
90 static void tunnel_destroy()
92 struct ip_tunnel_parm p;
97 if (tunnel_created == 0)
100 /* ip tunnel del tun6to4 */
102 memset(&p, 0, sizeof(struct ip_tunnel_parm));
103 memset(&ifr, 0, sizeof(struct ifreq));
107 p.iph.protocol = IPPROTO_IPV6;
108 strncpy(p.name, "tun6to4", IFNAMSIZ);
110 strncpy(ifr.ifr_name, "tun6to4", IFNAMSIZ);
111 ifr.ifr_ifru.ifru_data = (void *)&p;
112 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
114 connman_error("socket failed: %s", strerror(errno));
118 ret = ioctl(fd, SIOCDELTUNNEL, &ifr);
120 connman_error("del tunnel %s failed: %s", ifr.ifr_name,
128 g_free(tunnel_ip_address);
129 tunnel_ip_address = NULL;
132 static int tunnel_add_route()
134 struct __connman_inet_rtnl_handle rth;
135 struct in6_addr addr6;
139 /* ip -6 route add ::/0 via ::192.88.99.1 dev tun6to4 metric 1 */
141 index = if_nametoindex("tun6to4");
143 DBG("Can not find device tun6to4");
147 memset(&rth, 0, sizeof(rth));
149 rth.req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
150 rth.req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
151 rth.req.n.nlmsg_type = RTM_NEWROUTE;
152 rth.req.u.r.rt.rtm_family = AF_INET6;
153 rth.req.u.r.rt.rtm_table = RT_TABLE_MAIN;
154 rth.req.u.r.rt.rtm_protocol = RTPROT_BOOT;
155 rth.req.u.r.rt.rtm_scope = RT_SCOPE_UNIVERSE;
156 rth.req.u.r.rt.rtm_type = RTN_UNICAST;
157 rth.req.u.r.rt.rtm_dst_len = 0;
159 inet_pton(AF_INET6, "::192.88.99.1", &addr6);
161 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), RTA_GATEWAY,
163 __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req), RTA_OIF,
165 __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req),
168 ret = __connman_inet_rtnl_open(&rth);
172 ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
175 __connman_inet_rtnl_close(&rth);
179 static int tunnel_set_addr(unsigned int a, unsigned int b,
180 unsigned int c, unsigned int d)
182 struct __connman_inet_rtnl_handle rth;
183 struct in6_addr addr6;
187 /* ip -6 addr add dev tun6to4 2002:0102:0304::1/64 */
189 memset(&rth, 0, sizeof(rth));
191 rth.req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
192 rth.req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
193 rth.req.n.nlmsg_type = RTM_NEWADDR;
194 rth.req.u.i.ifa.ifa_family = AF_INET6;
195 rth.req.u.i.ifa.ifa_prefixlen = 64;
196 rth.req.u.i.ifa.ifa_index = if_nametoindex("tun6to4");
197 if (rth.req.u.i.ifa.ifa_index == 0) {
198 connman_error("Can not find device tun6to4");
203 ip6addr = g_strdup_printf("2002:%02x%02x:%02x%02x::1", a, b, c, d);
204 inet_pton(AF_INET6, ip6addr, &addr6);
205 DBG("ipv6 address %s", ip6addr);
208 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_LOCAL,
210 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_ADDRESS,
213 ret = __connman_inet_rtnl_open(&rth);
217 ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
220 __connman_inet_rtnl_close(&rth);
224 static gboolean unref_web(gpointer user_data)
230 static gboolean web_result(GWebResult *result, gpointer user_data)
234 if (web_request_id == 0)
237 status = g_web_result_get_status(result);
239 DBG("status %u", status);
241 if (status >= 400 && status < 500)
248 g_timeout_add_seconds(1, unref_web, NULL);
253 static int init_6to4(struct in_addr *ip4addr)
255 unsigned int a, b, c, d;
261 addr = ntohl(ip4addr->s_addr);
263 a = (addr & 0xff000000) >> 24;
264 b = (addr & 0x00ff0000) >> 16;
265 c = (addr & 0x0000ff00) >> 8;
266 d = addr & 0x000000ff;
268 ret = tunnel_create(ip4addr);
274 ret = connman_inet_setup_tunnel("tun6to4", 1472);
278 ret = tunnel_set_addr(a, b, c, d);
282 ret = tunnel_add_route();
286 if_index = connman_inet_ifindex("tun6to4");
290 /* We try to verify that connectivity through tunnel works ok.
292 web = g_web_new(if_index);
296 g_web_set_accept(web, NULL);
297 g_web_set_user_agent(web, "ConnMan/%s", VERSION);
298 g_web_set_close_connection(web, TRUE);
300 web_request_id = g_web_request_get(web, STATUS_URL, web_result, NULL);
309 static void receive_rs_reply(struct nd_router_advert *reply,
310 unsigned int length, void *user_data)
312 char *address = user_data;
313 struct in_addr ip4addr;
315 DBG("reply %p len %d address %s", reply, length, address);
317 /* We try to create tunnel if autoconfiguration did not work i.e.,
318 * we did not receive any reply to router solicitation message.
320 if (reply == NULL && inet_aton(address, &ip4addr) != 0)
326 int __connman_6to4_probe(struct connman_service *service)
328 struct connman_ipconfig *ip4config, *ip6config;
329 enum connman_ipconfig_method method;
331 struct in_addr ip4addr;
337 DBG("service %p", service);
339 if (tunnel_created || tunnel_pending)
345 ip4config = __connman_service_get_ip4config(service);
346 if (ip4config == NULL)
349 ip6config = __connman_service_get_ip6config(service);
350 if (ip6config == NULL)
353 method = __connman_ipconfig_get_method(ip6config);
354 if (method != CONNMAN_IPCONFIG_METHOD_AUTO)
357 address = __connman_ipconfig_get_local(ip4config);
361 if (inet_aton(address, &ip4addr) == 0)
364 addr = ntohl(ip4addr.s_addr);
366 a = (addr & 0xff000000) >> 24;
367 b = (addr & 0x00ff0000) >> 16;
369 /* 6to4 tunnel is only usable if we have a public IPv4 address */
370 if (a == 10 || (a == 192 && b == 168) ||
371 (a == 172 && (b >= 16 && b <= 31)))
374 index = __connman_ipconfig_get_index(ip4config);
375 ip_address = g_strdup(address);
378 g_free(tunnel_ip_address);
379 tunnel_ip_address = g_strdup(address);
381 return __connman_inet_ipv6_send_rs(index, 2, receive_rs_reply,
385 void __connman_6to4_remove(struct connman_ipconfig *ip4config)
389 DBG("tunnel ip address %s", tunnel_ip_address);
391 if (ip4config == NULL)
394 address = __connman_ipconfig_get_local(ip4config);
398 if (g_strcmp0(address, tunnel_ip_address) != 0)
405 int __connman_6to4_check(struct connman_ipconfig *ip4config)
409 if (ip4config == NULL || tunnel_created == 0 ||
413 DBG("tunnel ip address %s", tunnel_ip_address);
415 address = __connman_ipconfig_get_local(ip4config);
419 if (g_strcmp0(address, tunnel_ip_address) == 0)