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;
53 #define STATUS_URL "http://ipv6.connman.net/online/status.html"
56 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
59 static int tunnel_create(struct in_addr *addr)
61 struct ip_tunnel_parm p;
66 /* ip tunnel add tun6to4 mode sit remote any local 1.2.3.4 ttl 64 */
68 memset(&p, 0, sizeof(struct ip_tunnel_parm));
69 memset(&ifr, 0, sizeof(struct ifreq));
73 p.iph.frag_off = htons(IP_DF);
74 p.iph.protocol = IPPROTO_IPV6;
75 p.iph.saddr = addr->s_addr;
77 strncpy(p.name, "tun6to4", IFNAMSIZ);
79 strncpy(ifr.ifr_name, "sit0", IFNAMSIZ);
80 ifr.ifr_ifru.ifru_data = (void *)&p;
81 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
82 ret = ioctl(fd, SIOCADDTUNNEL, &ifr);
84 connman_error("add tunnel %s failed: %s", ifr.ifr_name,
91 static void tunnel_destroy()
93 struct ip_tunnel_parm p;
98 if (tunnel_created == 0)
101 /* ip tunnel del tun6to4 */
103 memset(&p, 0, sizeof(struct ip_tunnel_parm));
104 memset(&ifr, 0, sizeof(struct ifreq));
108 p.iph.protocol = IPPROTO_IPV6;
109 strncpy(p.name, "tun6to4", IFNAMSIZ);
111 strncpy(ifr.ifr_name, "tun6to4", IFNAMSIZ);
112 ifr.ifr_ifru.ifru_data = (void *)&p;
113 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
115 connman_error("socket failed: %s", strerror(errno));
119 ret = ioctl(fd, SIOCDELTUNNEL, &ifr);
121 connman_error("del tunnel %s failed: %s", ifr.ifr_name,
129 g_free(tunnel_ip_address);
130 tunnel_ip_address = NULL;
133 static int tunnel_add_route()
135 struct __connman_inet_rtnl_handle rth;
136 struct in6_addr addr6;
140 /* ip -6 route add ::/0 via ::192.88.99.1 dev tun6to4 metric 1 */
142 index = if_nametoindex("tun6to4");
144 DBG("Can not find device tun6to4");
148 memset(&rth, 0, sizeof(rth));
150 rth.req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
151 rth.req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
152 rth.req.n.nlmsg_type = RTM_NEWROUTE;
153 rth.req.u.r.rt.rtm_family = AF_INET6;
154 rth.req.u.r.rt.rtm_table = RT_TABLE_MAIN;
155 rth.req.u.r.rt.rtm_protocol = RTPROT_BOOT;
156 rth.req.u.r.rt.rtm_scope = RT_SCOPE_UNIVERSE;
157 rth.req.u.r.rt.rtm_type = RTN_UNICAST;
158 rth.req.u.r.rt.rtm_dst_len = 0;
160 inet_pton(AF_INET6, "::192.88.99.1", &addr6);
162 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), RTA_GATEWAY,
164 __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req), RTA_OIF,
166 __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req),
169 ret = __connman_inet_rtnl_open(&rth);
173 ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
176 __connman_inet_rtnl_close(&rth);
180 static int tunnel_set_addr(unsigned int a, unsigned int b,
181 unsigned int c, unsigned int d)
183 struct __connman_inet_rtnl_handle rth;
184 struct in6_addr addr6;
188 /* ip -6 addr add dev tun6to4 2002:0102:0304::1/64 */
190 memset(&rth, 0, sizeof(rth));
192 rth.req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
193 rth.req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
194 rth.req.n.nlmsg_type = RTM_NEWADDR;
195 rth.req.u.i.ifa.ifa_family = AF_INET6;
196 rth.req.u.i.ifa.ifa_prefixlen = 64;
197 rth.req.u.i.ifa.ifa_index = if_nametoindex("tun6to4");
198 if (rth.req.u.i.ifa.ifa_index == 0) {
199 connman_error("Can not find device tun6to4");
204 ip6addr = g_strdup_printf("2002:%02x%02x:%02x%02x::1", a, b, c, d);
205 inet_pton(AF_INET6, ip6addr, &addr6);
206 DBG("ipv6 address %s", ip6addr);
209 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_LOCAL,
211 __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_ADDRESS,
214 ret = __connman_inet_rtnl_open(&rth);
218 ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
221 __connman_inet_rtnl_close(&rth);
225 static gboolean unref_web(gpointer user_data)
231 static gboolean web_result(GWebResult *result, gpointer user_data)
235 if (web_request_id == 0)
238 status = g_web_result_get_status(result);
240 DBG("status %u", status);
242 if (status >= 400 && status < 500)
249 g_timeout_add_seconds(1, unref_web, NULL);
254 static void web_debug(const char *str, void *data)
256 connman_info("%s: %s\n", (const char *) data, str);
259 static int init_6to4(struct in_addr *ip4addr)
261 unsigned int a, b, c, d;
267 addr = ntohl(ip4addr->s_addr);
269 a = (addr & 0xff000000) >> 24;
270 b = (addr & 0x00ff0000) >> 16;
271 c = (addr & 0x0000ff00) >> 8;
272 d = addr & 0x000000ff;
274 ret = tunnel_create(ip4addr);
280 ret = connman_inet_setup_tunnel("tun6to4", 1472);
284 ret = tunnel_set_addr(a, b, c, d);
288 ret = tunnel_add_route();
292 if_index = connman_inet_ifindex("tun6to4");
296 /* We try to verify that connectivity through tunnel works ok.
298 web = g_web_new(if_index);
302 g_web_set_accept(web, NULL);
303 g_web_set_user_agent(web, "ConnMan/%s", VERSION);
304 g_web_set_close_connection(web, TRUE);
306 if (getenv("CONNMAN_WEB_DEBUG"))
307 g_web_set_debug(web, web_debug, "6to4");
309 web_request_id = g_web_request_get(web, STATUS_URL, web_result, NULL);
318 static void receive_rs_reply(struct nd_router_advert *reply,
319 unsigned int length, void *user_data)
321 char *address = user_data;
322 struct in_addr ip4addr;
324 DBG("reply %p len %d address %s", reply, length, address);
326 /* We try to create tunnel if autoconfiguration did not work i.e.,
327 * we did not receive any reply to router solicitation message.
329 if (reply == NULL && inet_aton(address, &ip4addr) != 0)
335 int __connman_6to4_probe(struct connman_service *service)
337 struct connman_ipconfig *ip4config, *ip6config;
338 enum connman_ipconfig_method method;
340 struct in_addr ip4addr;
346 DBG("service %p", service);
348 if (tunnel_created || tunnel_pending)
354 ip4config = __connman_service_get_ip4config(service);
355 if (ip4config == NULL)
358 ip6config = __connman_service_get_ip6config(service);
359 if (ip6config == NULL)
362 method = __connman_ipconfig_get_method(ip6config);
363 if (method != CONNMAN_IPCONFIG_METHOD_AUTO)
366 address = __connman_ipconfig_get_local(ip4config);
370 if (inet_aton(address, &ip4addr) == 0)
373 addr = ntohl(ip4addr.s_addr);
375 a = (addr & 0xff000000) >> 24;
376 b = (addr & 0x00ff0000) >> 16;
378 /* 6to4 tunnel is only usable if we have a public IPv4 address */
379 if (a == 10 || (a == 192 && b == 168) ||
380 (a == 172 && (b >= 16 && b <= 31)))
383 index = __connman_ipconfig_get_index(ip4config);
384 ip_address = g_strdup(address);
387 g_free(tunnel_ip_address);
388 tunnel_ip_address = g_strdup(address);
390 return __connman_inet_ipv6_send_rs(index, 2, receive_rs_reply,
394 void __connman_6to4_remove(struct connman_ipconfig *ip4config)
398 DBG("tunnel ip address %s", tunnel_ip_address);
400 if (ip4config == NULL)
403 address = __connman_ipconfig_get_local(ip4config);
407 if (g_strcmp0(address, tunnel_ip_address) != 0)
414 int __connman_6to4_check(struct connman_ipconfig *ip4config)
418 if (ip4config == NULL || tunnel_created == 0 ||
422 DBG("tunnel ip address %s", tunnel_ip_address);
424 address = __connman_ipconfig_get_local(ip4config);
428 if (g_strcmp0(address, tunnel_ip_address) == 0)