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"
54 #define NLMSG_TAIL(nmsg) \
55 ((struct rtattr *) (((void *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
58 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
63 struct sockaddr_nl local;
64 struct sockaddr_nl peer;
69 static int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
71 int len = RTA_LENGTH(4);
73 if (NLMSG_ALIGN(n->nlmsg_len) + len > (unsigned int)maxlen) {
74 DBG("Error! max allowed bound %d exceeded", maxlen);
80 memcpy(RTA_DATA(rta), &data, 4);
81 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
86 static int addattr_l(struct nlmsghdr *n, int maxlen, int type,
87 const void *data, int alen)
89 int len = RTA_LENGTH(alen);
92 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) >
93 (unsigned int)maxlen) {
94 DBG("addattr_l message exceeded bound of %d", maxlen);
100 memcpy(RTA_DATA(rta), data, alen);
101 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
106 static void rtnl_close(struct rtnl_handle *rth)
114 static int rtnl_open(struct rtnl_handle *rth)
119 memset(rth, 0, sizeof(*rth));
121 rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
123 connman_error("Can not open netlink socket: %s",
128 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf,
129 sizeof(sndbuf)) < 0) {
130 connman_error("SO_SNDBUF: %s", strerror(errno));
134 memset(&rth->local, 0, sizeof(rth->local));
135 rth->local.nl_family = AF_NETLINK;
136 rth->local.nl_groups = 0;
138 if (bind(rth->fd, (struct sockaddr *)&rth->local,
139 sizeof(rth->local)) < 0) {
140 connman_error("Can not bind netlink socket: %s",
144 addr_len = sizeof(rth->local);
145 if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
147 connman_error("Can not getsockname: %s", strerror(errno));
150 if (addr_len != sizeof(rth->local)) {
151 connman_error("Wrong address length %d", addr_len);
154 if (rth->local.nl_family != AF_NETLINK) {
155 connman_error("Wrong address family %d", rth->local.nl_family);
158 rth->seq = time(NULL);
163 static int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n)
165 struct sockaddr_nl nladdr;
167 .iov_base = (void *)n,
168 .iov_len = n->nlmsg_len
170 struct msghdr msg = {
172 .msg_namelen = sizeof(nladdr),
179 memset(&nladdr, 0, sizeof(nladdr));
180 nladdr.nl_family = AF_NETLINK;
182 n->nlmsg_seq = seq = ++rtnl->seq;
183 n->nlmsg_flags |= NLM_F_ACK;
185 err = sendmsg(rtnl->fd, &msg, 0);
187 connman_error("Can not talk to rtnetlink");
194 static int tunnel_create(struct in_addr *addr)
196 struct ip_tunnel_parm p;
201 /* ip tunnel add tun6to4 mode sit remote any local 1.2.3.4 ttl 64 */
203 memset(&p, 0, sizeof(struct ip_tunnel_parm));
204 memset(&ifr, 0, sizeof(struct ifreq));
208 p.iph.frag_off = htons(IP_DF);
209 p.iph.protocol = IPPROTO_IPV6;
210 p.iph.saddr = addr->s_addr;
212 strncpy(p.name, "tun6to4", IFNAMSIZ);
214 strncpy(ifr.ifr_name, "sit0", IFNAMSIZ);
215 ifr.ifr_ifru.ifru_data = (void *)&p;
216 fd = socket(AF_INET, SOCK_DGRAM, 0);
217 ret = ioctl(fd, SIOCADDTUNNEL, &ifr);
219 connman_error("add tunnel %s failed: %s", ifr.ifr_name,
226 static void tunnel_destroy()
228 struct ip_tunnel_parm p;
233 if (tunnel_created == 0)
236 /* ip tunnel del tun6to4 */
238 memset(&p, 0, sizeof(struct ip_tunnel_parm));
239 memset(&ifr, 0, sizeof(struct ifreq));
243 p.iph.protocol = IPPROTO_IPV6;
244 strncpy(p.name, "tun6to4", IFNAMSIZ);
246 strncpy(ifr.ifr_name, "tun6to4", IFNAMSIZ);
247 ifr.ifr_ifru.ifru_data = (void *)&p;
248 fd = socket(AF_INET, SOCK_DGRAM, 0);
250 connman_error("socket failed: %s", strerror(errno));
254 ret = ioctl(fd, SIOCDELTUNNEL, &ifr);
256 connman_error("del tunnel %s failed: %s", ifr.ifr_name,
264 g_free(tunnel_ip_address);
265 tunnel_ip_address = NULL;
268 static int tunnel_add_route()
270 struct rtnl_handle rth;
271 struct in6_addr addr6;
281 /* ip -6 route add ::/0 via ::192.88.99.1 dev tun6to4 metric 1 */
283 index = if_nametoindex("tun6to4");
285 DBG("Can not find device tun6to4");
289 memset(&req, 0, sizeof(req));
291 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
292 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
293 req.n.nlmsg_type = RTM_NEWROUTE;
294 req.r.rtm_family = AF_INET6;
295 req.r.rtm_table = RT_TABLE_MAIN;
296 req.r.rtm_protocol = RTPROT_BOOT;
297 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
298 req.r.rtm_type = RTN_UNICAST;
299 req.r.rtm_dst_len = 0;
301 inet_pton(AF_INET6, "::192.88.99.1", &addr6);
303 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr6.s6_addr, 16);
304 addattr32(&req.n, sizeof(req), RTA_OIF, index);
305 addattr32(&req.n, sizeof(req), RTA_PRIORITY, 1);
307 ret = rtnl_open(&rth);
311 ret = rtnl_talk(&rth, &req.n);
318 static int tunnel_set_addr(unsigned int a, unsigned int b,
319 unsigned int c, unsigned int d)
321 struct rtnl_handle rth;
322 struct in6_addr addr6;
328 struct ifaddrmsg ifa;
332 /* ip -6 addr add dev tun6to4 2002:0102:0304::1/64 */
334 memset(&req, 0, sizeof(req));
336 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
337 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
338 req.n.nlmsg_type = RTM_NEWADDR;
339 req.ifa.ifa_family = AF_INET6;
340 req.ifa.ifa_prefixlen = 64;
341 req.ifa.ifa_index = if_nametoindex("tun6to4");
342 if (req.ifa.ifa_index == 0) {
343 connman_error("Can not find device tun6to4");
348 ip6addr = g_strdup_printf("2002:%02x%02x:%02x%02x::1", a, b, c, d);
349 inet_pton(AF_INET6, ip6addr, &addr6);
350 DBG("ipv6 address %s", ip6addr);
353 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &addr6.s6_addr, 16);
354 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &addr6.s6_addr, 16);
356 ret = rtnl_open(&rth);
360 ret = rtnl_talk(&rth, &req.n);
367 static gboolean unref_web(gpointer user_data)
373 static gboolean web_result(GWebResult *result, gpointer user_data)
377 if (web_request_id == 0)
380 status = g_web_result_get_status(result);
382 DBG("status %u", status);
384 if (status >= 400 && status < 500)
391 g_timeout_add_seconds(1, unref_web, NULL);
396 static int init_6to4(struct in_addr *ip4addr)
398 unsigned int a, b, c, d;
404 addr = ntohl(ip4addr->s_addr);
406 a = (addr & 0xff000000) >> 24;
407 b = (addr & 0x00ff0000) >> 16;
408 c = (addr & 0x0000ff00) >> 8;
409 d = addr & 0x000000ff;
411 ret = tunnel_create(ip4addr);
417 ret = connman_inet_setup_tunnel("tun6to4", 1472);
421 ret = tunnel_set_addr(a, b, c, d);
425 ret = tunnel_add_route();
429 /* We try to verify that connectivity through tunnel works ok.
435 g_web_set_accept(web, NULL);
436 g_web_set_user_agent(web, "ConnMan/%s", VERSION);
437 g_web_set_close_connection(web, TRUE);
439 web_request_id = g_web_request_get(web, STATUS_URL, web_result, NULL);
448 static void receive_rs_reply(struct nd_router_advert *reply, void *user_data)
450 char *address = user_data;
451 struct in_addr ip4addr;
453 DBG("reply %p address %s", reply, address);
455 /* We try to create tunnel if autoconfiguration did not work i.e.,
456 * we did not receive any reply to router solicitation message.
458 if (reply == NULL && inet_aton(address, &ip4addr) != 0)
464 int __connman_6to4_probe(struct connman_service *service)
466 struct connman_ipconfig *ip4config, *ip6config;
467 enum connman_ipconfig_method method;
469 struct in_addr ip4addr;
475 DBG("service %p", service);
477 if (tunnel_created || tunnel_pending)
483 ip4config = __connman_service_get_ip4config(service);
484 if (ip4config == NULL)
487 ip6config = __connman_service_get_ip6config(service);
488 if (ip6config == NULL)
491 method = __connman_ipconfig_get_method(ip6config);
492 if (method != CONNMAN_IPCONFIG_METHOD_AUTO)
495 address = __connman_ipconfig_get_local(ip4config);
499 if (inet_aton(address, &ip4addr) == 0)
502 addr = ntohl(ip4addr.s_addr);
504 a = (addr & 0xff000000) >> 24;
505 b = (addr & 0x00ff0000) >> 16;
507 /* 6to4 tunnel is only usable if we have a public IPv4 address */
508 if (a == 10 || (a == 192 && b == 168) ||
509 (a == 172 && (b >= 16 && b <= 31)))
512 index = connman_ipconfig_get_index(ip4config);
513 ip_address = g_strdup(address);
516 g_free(tunnel_ip_address);
517 tunnel_ip_address = g_strdup(address);
519 return __connman_inet_ipv6_send_rs(index, 2, receive_rs_reply,
523 void __connman_6to4_remove(struct connman_ipconfig *ip4config)
527 DBG("tunnel ip address %s", tunnel_ip_address);
529 if (ip4config == NULL)
532 address = __connman_ipconfig_get_local(ip4config);
536 if (g_strcmp0(address, tunnel_ip_address) != 0)
543 int __connman_6to4_check(struct connman_ipconfig *ip4config)
547 if (ip4config == NULL || tunnel_created == 0 ||
551 DBG("tunnel ip address %s", tunnel_ip_address);
553 address = __connman_ipconfig_get_local(ip4config);
557 if (g_strcmp0(address, tunnel_ip_address) == 0)