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
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
34 #include <linux/if_tunnel.h>
35 #include <linux/netlink.h>
36 #include <linux/rtnetlink.h>
37 #include <sys/ioctl.h>
41 #include <connman/log.h>
42 #include <connman/ipconfig.h>
44 static int tunnel_created;
45 static int tunnel_pending;
46 static char *tunnel_ip_address;
48 #define NLMSG_TAIL(nmsg) \
49 ((struct rtattr *) (((void *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
52 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
57 struct sockaddr_nl local;
58 struct sockaddr_nl peer;
63 static int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
65 int len = RTA_LENGTH(4);
67 if (NLMSG_ALIGN(n->nlmsg_len) + len > (unsigned int)maxlen) {
68 DBG("Error! max allowed bound %d exceeded", maxlen);
74 memcpy(RTA_DATA(rta), &data, 4);
75 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
80 static int addattr_l(struct nlmsghdr *n, int maxlen, int type,
81 const void *data, int alen)
83 int len = RTA_LENGTH(alen);
86 if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) >
87 (unsigned int)maxlen) {
88 DBG("addattr_l message exceeded bound of %d", maxlen);
94 memcpy(RTA_DATA(rta), data, alen);
95 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
100 static void rtnl_close(struct rtnl_handle *rth)
108 static int rtnl_open(struct rtnl_handle *rth)
113 memset(rth, 0, sizeof(*rth));
115 rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
117 connman_error("Can not open netlink socket: %s",
122 if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF, &sndbuf,
123 sizeof(sndbuf)) < 0) {
124 connman_error("SO_SNDBUF: %s", strerror(errno));
128 memset(&rth->local, 0, sizeof(rth->local));
129 rth->local.nl_family = AF_NETLINK;
130 rth->local.nl_groups = 0;
132 if (bind(rth->fd, (struct sockaddr *)&rth->local,
133 sizeof(rth->local)) < 0) {
134 connman_error("Can not bind netlink socket: %s",
138 addr_len = sizeof(rth->local);
139 if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
141 connman_error("Can not getsockname: %s", strerror(errno));
144 if (addr_len != sizeof(rth->local)) {
145 connman_error("Wrong address length %d", addr_len);
148 if (rth->local.nl_family != AF_NETLINK) {
149 connman_error("Wrong address family %d", rth->local.nl_family);
152 rth->seq = time(NULL);
157 static int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n)
159 struct sockaddr_nl nladdr;
161 .iov_base = (void *)n,
162 .iov_len = n->nlmsg_len
164 struct msghdr msg = {
166 .msg_namelen = sizeof(nladdr),
173 memset(&nladdr, 0, sizeof(nladdr));
174 nladdr.nl_family = AF_NETLINK;
176 n->nlmsg_seq = seq = ++rtnl->seq;
177 n->nlmsg_flags |= NLM_F_ACK;
179 err = sendmsg(rtnl->fd, &msg, 0);
181 connman_error("Can not talk to rtnetlink");
188 static int tunnel_create(struct in_addr *addr)
190 struct ip_tunnel_parm p;
195 /* ip tunnel add tun6to4 mode sit remote any local 1.2.3.4 ttl 64 */
197 memset(&p, 0, sizeof(struct ip_tunnel_parm));
198 memset(&ifr, 0, sizeof(struct ifreq));
202 p.iph.frag_off = htons(IP_DF);
203 p.iph.protocol = IPPROTO_IPV6;
204 p.iph.saddr = addr->s_addr;
206 strncpy(p.name, "tun6to4", IFNAMSIZ);
208 strncpy(ifr.ifr_name, "sit0", IFNAMSIZ);
209 ifr.ifr_ifru.ifru_data = (void *)&p;
210 fd = socket(AF_INET, SOCK_DGRAM, 0);
211 ret = ioctl(fd, SIOCADDTUNNEL, &ifr);
213 connman_error("add tunnel %s failed: %s", ifr.ifr_name,
220 static void tunnel_destroy()
222 struct ip_tunnel_parm p;
227 if (tunnel_created == 0)
230 /* ip tunnel del tun6to4 */
232 memset(&p, 0, sizeof(struct ip_tunnel_parm));
233 memset(&ifr, 0, sizeof(struct ifreq));
237 p.iph.protocol = IPPROTO_IPV6;
238 strncpy(p.name, "tun6to4", IFNAMSIZ);
240 strncpy(ifr.ifr_name, "tun6to4", IFNAMSIZ);
241 ifr.ifr_ifru.ifru_data = (void *)&p;
242 fd = socket(AF_INET, SOCK_DGRAM, 0);
244 connman_error("socket failed: %s", strerror(errno));
248 ret = ioctl(fd, SIOCDELTUNNEL, &ifr);
250 connman_error("del tunnel %s failed: %s", ifr.ifr_name,
258 g_free(tunnel_ip_address);
259 tunnel_ip_address = NULL;
262 static int tunnel_add_route()
264 struct rtnl_handle rth;
265 struct in6_addr addr6;
275 /* ip -6 route add ::/0 via ::192.88.99.1 dev tun6to4 metric 1 */
277 index = if_nametoindex("tun6to4");
279 DBG("Can not find device tun6to4");
283 memset(&req, 0, sizeof(req));
285 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
286 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
287 req.n.nlmsg_type = RTM_NEWROUTE;
288 req.r.rtm_family = AF_INET6;
289 req.r.rtm_table = RT_TABLE_MAIN;
290 req.r.rtm_protocol = RTPROT_BOOT;
291 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
292 req.r.rtm_type = RTN_UNICAST;
293 req.r.rtm_dst_len = 0;
295 inet_pton(AF_INET6, "::192.88.99.1", &addr6);
297 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr6.s6_addr, 16);
298 addattr32(&req.n, sizeof(req), RTA_OIF, index);
299 addattr32(&req.n, sizeof(req), RTA_PRIORITY, 1);
301 ret = rtnl_open(&rth);
305 ret = rtnl_talk(&rth, &req.n);
312 static int tunnel_set_addr(unsigned int a, unsigned int b,
313 unsigned int c, unsigned int d)
315 struct rtnl_handle rth;
316 struct in6_addr addr6;
322 struct ifaddrmsg ifa;
326 /* ip -6 addr add dev tun6to4 2002:0102:0304::1/64 */
328 memset(&req, 0, sizeof(req));
330 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
331 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
332 req.n.nlmsg_type = RTM_NEWADDR;
333 req.ifa.ifa_family = AF_INET6;
334 req.ifa.ifa_prefixlen = 64;
335 req.ifa.ifa_index = if_nametoindex("tun6to4");
336 if (req.ifa.ifa_index == 0) {
337 connman_error("Can not find device tun6to4");
342 ip6addr = g_strdup_printf("2002:%02x%02x:%02x%02x::1", a, b, c, d);
343 inet_pton(AF_INET6, ip6addr, &addr6);
344 DBG("ipv6 address %s", ip6addr);
347 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &addr6.s6_addr, 16);
348 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &addr6.s6_addr, 16);
350 ret = rtnl_open(&rth);
354 ret = rtnl_talk(&rth, &req.n);
361 static int init_6to4(struct in_addr *ip4addr)
363 unsigned int a, b, c, d;
369 addr = ntohl(ip4addr->s_addr);
371 a = (addr & 0xff000000) >> 24;
372 b = (addr & 0x00ff0000) >> 16;
373 c = (addr & 0x0000ff00) >> 8;
374 d = addr & 0x000000ff;
376 ret = tunnel_create(ip4addr);
382 ret = connman_inet_setup_tunnel("tun6to4", 1472);
386 ret = tunnel_set_addr(a, b, c, d);
390 ret = tunnel_add_route();
402 static void receive_rs_reply(struct nd_router_advert *reply, void *user_data)
404 char *address = user_data;
405 struct in_addr ip4addr;
407 DBG("reply %p address %s", reply, address);
409 /* We try to create tunnel if autoconfiguration did not work i.e.,
410 * we did not receive any reply to router solicitation message.
412 if (reply == NULL && inet_aton(address, &ip4addr) != 0)
418 int __connman_6to4_probe(struct connman_service *service)
420 struct connman_ipconfig *ip4config, *ip6config;
421 enum connman_ipconfig_method method;
422 unsigned int a, b, c, d;
423 struct in_addr ip4addr;
429 DBG("service %p", service);
431 if (tunnel_created || tunnel_pending)
437 ip4config = __connman_service_get_ip4config(service);
438 if (ip4config == NULL)
441 ip6config = __connman_service_get_ip6config(service);
442 if (ip6config == NULL)
445 method = __connman_ipconfig_get_method(ip6config);
446 if (method != CONNMAN_IPCONFIG_METHOD_AUTO)
449 address = __connman_ipconfig_get_local(ip4config);
453 if (inet_aton(address, &ip4addr) == 0)
456 addr = ntohl(ip4addr.s_addr);
458 a = (addr & 0xff000000) >> 24;
459 b = (addr & 0x00ff0000) >> 16;
460 c = (addr & 0x0000ff00) >> 8;
461 d = addr & 0x000000ff;
463 /* 6to4 tunnel is only usable if we have a public IPv4 address */
464 if (a == 10 || (a == 192 && b == 168) ||
465 (a == 172 && (b >= 16 && b <= 31)))
468 index = connman_ipconfig_get_index(ip4config);
469 ip_address = g_strdup(address);
472 g_free(tunnel_ip_address);
473 tunnel_ip_address = g_strdup(address);
475 return __connman_inet_ipv6_send_rs(index, 2, receive_rs_reply,
479 void __connman_6to4_remove(struct connman_ipconfig *ip4config)
483 DBG("tunnel ip address %s", tunnel_ip_address);
485 if (ip4config == NULL)
488 address = __connman_ipconfig_get_local(ip4config);
492 if (g_strcmp0(address, tunnel_ip_address) != 0)