technology: return already enabled when tethering is enabled
[framework/connectivity/connman.git] / src / 6to4.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2011  Nokia Corporation. All rights reserved.
6  *  Copyright (C) Alexey Kuznetsov et al. from iproute2 package.
7  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <net/if.h>
35 #include <linux/ip.h>
36 #include <linux/if_tunnel.h>
37 #include <linux/netlink.h>
38 #include <linux/rtnetlink.h>
39 #include <sys/ioctl.h>
40 #include <unistd.h>
41
42 #include "connman.h"
43 #include <connman/log.h>
44 #include <connman/ipconfig.h>
45 #include "gweb/gweb.h"
46
47 static int tunnel_created;
48 static int tunnel_pending;
49 static char *tunnel_ip_address;
50 static GWeb *web;
51 static guint web_request_id;
52 static unsigned int newlink_watch;
53 static unsigned int newlink_flags;
54 static int newlink_timeout_id;
55
56 #define STATUS_URL "http://ipv6.connman.net/online/status.html"
57
58 #ifndef IP_DF
59 #define IP_DF           0x4000          /* Flag: "Don't Fragment"       */
60 #endif
61
62 static int tunnel_create(struct in_addr *addr)
63 {
64         struct ip_tunnel_parm p;
65         struct ifreq ifr;
66         int fd = -1;
67         int ret;
68
69         /* ip tunnel add tun6to4 mode sit remote any local 1.2.3.4 ttl 64 */
70
71         memset(&p, 0, sizeof(struct ip_tunnel_parm));
72         memset(&ifr, 0, sizeof(struct ifreq));
73
74         p.iph.version = 4;
75         p.iph.ihl = 5;
76         p.iph.frag_off = htons(IP_DF);
77         p.iph.protocol = IPPROTO_IPV6;
78         p.iph.saddr = addr->s_addr;
79         p.iph.ttl = 64;
80         strncpy(p.name, "tun6to4", IFNAMSIZ);
81
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);
86         if (ret)
87                 connman_error("add tunnel %s failed: %s", ifr.ifr_name,
88                                                         strerror(errno));
89         close(fd);
90
91         return ret;
92 }
93
94 static void tunnel_destroy()
95 {
96         struct ip_tunnel_parm p;
97         struct ifreq ifr;
98         int fd = -1;
99         int ret;
100
101         if (tunnel_created == 0)
102                 return;
103
104         /* ip tunnel del tun6to4 */
105
106         memset(&p, 0, sizeof(struct ip_tunnel_parm));
107         memset(&ifr, 0, sizeof(struct ifreq));
108
109         p.iph.version = 4;
110         p.iph.ihl = 5;
111         p.iph.protocol = IPPROTO_IPV6;
112         strncpy(p.name, "tun6to4", IFNAMSIZ);
113
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);
117         if (fd < 0) {
118                 connman_error("socket failed: %s", strerror(errno));
119                 return;
120         }
121
122         ret = ioctl(fd, SIOCDELTUNNEL, &ifr);
123         if (ret)
124                 connman_error("del tunnel %s failed: %s", ifr.ifr_name,
125                                                         strerror(errno));
126         else
127                 tunnel_created = 0;
128
129         tunnel_pending = 0;
130         close(fd);
131
132         g_free(tunnel_ip_address);
133         tunnel_ip_address = NULL;
134 }
135
136 static int tunnel_add_route()
137 {
138         struct __connman_inet_rtnl_handle rth;
139         struct in6_addr addr6;
140         int index;
141         int ret = 0;
142
143         /* ip -6 route add ::/0 via ::192.88.99.1 dev tun6to4 metric 1 */
144
145         index = if_nametoindex("tun6to4");
146         if (index == 0) {
147                 DBG("Can not find device tun6to4");
148                 return -1;
149         }
150
151         memset(&rth, 0, sizeof(rth));
152
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;
162
163         inet_pton(AF_INET6, "::192.88.99.1", &addr6);
164
165         __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), RTA_GATEWAY,
166                                         &addr6.s6_addr, 16);
167         __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req), RTA_OIF,
168                                         index);
169         __connman_inet_rtnl_addattr32(&rth.req.n, sizeof(rth.req),
170                                         RTA_PRIORITY, 1);
171
172         ret = __connman_inet_rtnl_open(&rth);
173         if (ret < 0)
174                 goto done;
175
176         ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
177
178 done:
179         __connman_inet_rtnl_close(&rth);
180         return ret;
181 }
182
183 static int tunnel_set_addr(unsigned int a, unsigned int b,
184                         unsigned int c, unsigned int d)
185 {
186         struct __connman_inet_rtnl_handle rth;
187         struct in6_addr addr6;
188         char *ip6addr;
189         int ret;
190
191         /* ip -6 addr add dev tun6to4 2002:0102:0304::1/64 */
192
193         memset(&rth, 0, sizeof(rth));
194
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");
203                 ret = -1;
204                 goto done;
205         }
206
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);
210         g_free(ip6addr);
211
212         __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_LOCAL,
213                                         &addr6.s6_addr, 16);
214         __connman_inet_rtnl_addattr_l(&rth.req.n, sizeof(rth.req), IFA_ADDRESS,
215                                         &addr6.s6_addr, 16);
216
217         ret = __connman_inet_rtnl_open(&rth);
218         if (ret < 0)
219                 goto done;
220
221         ret = __connman_inet_rtnl_send(&rth, &rth.req.n);
222
223 done:
224         __connman_inet_rtnl_close(&rth);
225         return ret;
226 }
227
228 static gboolean unref_web(gpointer user_data)
229 {
230         g_web_unref(web);
231         return FALSE;
232 }
233
234 static gboolean web_result(GWebResult *result, gpointer user_data)
235 {
236         guint16 status;
237
238         if (web_request_id == 0)
239                 return FALSE;
240
241         status = g_web_result_get_status(result);
242
243         DBG("status %u", status);
244
245         if (status >= 400 && status < 500)
246                 tunnel_destroy();
247         else
248                 tunnel_pending = 0;
249
250         web_request_id = 0;
251
252         g_timeout_add_seconds(1, unref_web, NULL);
253
254         return FALSE;
255 }
256
257 static void web_debug(const char *str, void *data)
258 {
259         connman_info("%s: %s\n", (const char *) data, str);
260 }
261
262 static gboolean newlink_timeout(gpointer user_data)
263 {
264         /*
265          * Stop if the timeout has been cancelled already by tun_newlink()
266          */
267         if (newlink_timeout_id == 0)
268                 return FALSE;
269
270         DBG("");
271
272         if (newlink_watch != 0) {
273                 connman_rtnl_remove_watch(newlink_watch);
274                 newlink_watch = 0;
275         }
276
277         newlink_flags = 0;
278
279         if (web_request_id == 0)
280                 tunnel_destroy();
281
282         newlink_timeout_id = 0;
283
284         return FALSE;
285 }
286
287 static void tun_newlink(unsigned flags, unsigned change, void *user_data)
288 {
289         int index = GPOINTER_TO_INT(user_data);
290
291         if ((newlink_flags & IFF_UP) == (flags & IFF_UP)) {
292                 newlink_flags = flags;
293                 return;
294         }
295
296         if (flags & IFF_UP) {
297                 /*
298                  * We try to verify that connectivity through tunnel works ok.
299                  */
300                 if (newlink_timeout_id > 0) {
301                         g_source_remove(newlink_timeout_id);
302                         newlink_timeout_id = 0;
303                 }
304
305                 web = g_web_new(index);
306                 if (web == NULL) {
307                         tunnel_destroy();
308                         return;
309                 }
310
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);
314
315                 if (getenv("CONNMAN_WEB_DEBUG"))
316                         g_web_set_debug(web, web_debug, "6to4");
317
318                 web_request_id = g_web_request_get(web, STATUS_URL,
319                                 web_result, NULL,  NULL);
320
321                 newlink_timeout(NULL);
322         }
323
324         newlink_flags = flags;
325 }
326
327 static int init_6to4(struct in_addr *ip4addr)
328 {
329         unsigned int a, b, c, d;
330         int ret, if_index;
331         in_addr_t addr;
332
333         DBG("");
334
335         addr = ntohl(ip4addr->s_addr);
336
337         a = (addr & 0xff000000) >> 24;
338         b = (addr & 0x00ff0000) >> 16;
339         c = (addr & 0x0000ff00) >> 8;
340         d = addr & 0x000000ff;
341
342         ret = tunnel_create(ip4addr);
343         if (ret)
344                 return -1;
345
346         tunnel_created = 1;
347
348         ret = connman_inet_setup_tunnel("tun6to4", 1472);
349         if (ret)
350                 goto error;
351
352         ret = tunnel_set_addr(a, b, c, d);
353         if (ret)
354                 goto error;
355
356         ret = tunnel_add_route();
357         if (ret)
358                 goto error;
359
360         if_index = connman_inet_ifindex("tun6to4");
361         if (if_index < 0)
362                 goto error;
363
364         newlink_watch = connman_rtnl_add_newlink_watch(if_index,
365                                 tun_newlink, GINT_TO_POINTER(if_index));
366
367         newlink_timeout_id = g_timeout_add_seconds(1, newlink_timeout, NULL);
368
369         return 0;
370
371 error:
372         tunnel_destroy();
373         return -1;
374 }
375
376 static void receive_rs_reply(struct nd_router_advert *reply,
377                         unsigned int length, void *user_data)
378 {
379         char *address = user_data;
380         struct in_addr ip4addr;
381
382         DBG("reply %p len %d address %s", reply, length, address);
383
384         /* We try to create tunnel if autoconfiguration did not work i.e.,
385          * we did not receive any reply to router solicitation message.
386          */
387         if (reply == NULL && inet_aton(address, &ip4addr) != 0)
388                 init_6to4(&ip4addr);
389
390         g_free(address);
391 }
392
393 int __connman_6to4_probe(struct connman_service *service)
394 {
395         struct connman_ipconfig *ip4config, *ip6config;
396         enum connman_ipconfig_method method;
397         unsigned int a, b;
398         struct in_addr ip4addr;
399         in_addr_t addr;
400         const char *address;
401         char *ip_address;
402         int index;
403
404         DBG("service %p", service);
405
406         if (tunnel_created || tunnel_pending)
407                 return 0;
408
409         if (service == NULL)
410                 return -1;
411
412         ip4config = __connman_service_get_ip4config(service);
413         if (ip4config == NULL)
414                 return -1;
415
416         ip6config = __connman_service_get_ip6config(service);
417         if (ip6config == NULL)
418                 return -1;
419
420         method = __connman_ipconfig_get_method(ip6config);
421         if (method != CONNMAN_IPCONFIG_METHOD_AUTO)
422                 return -1;
423
424         address = __connman_ipconfig_get_local(ip4config);
425         if (address == NULL)
426                 return -1;
427
428         if (inet_aton(address, &ip4addr) == 0)
429                 return -1;
430
431         addr = ntohl(ip4addr.s_addr);
432
433         a = (addr & 0xff000000) >> 24;
434         b = (addr & 0x00ff0000) >> 16;
435
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)))
439                 return -1;
440
441         index = __connman_ipconfig_get_index(ip4config);
442         ip_address = g_strdup(address);
443         tunnel_pending = 1;
444
445         g_free(tunnel_ip_address);
446         tunnel_ip_address = g_strdup(address);
447
448         return __connman_inet_ipv6_send_rs(index, 2, receive_rs_reply,
449                                                         ip_address);
450 }
451
452 void __connman_6to4_remove(struct connman_ipconfig *ip4config)
453 {
454         const char *address;
455
456         DBG("tunnel ip address %s", tunnel_ip_address);
457
458         if (ip4config == NULL)
459                 return;
460
461         address = __connman_ipconfig_get_local(ip4config);
462         if (address == NULL)
463                 return;
464
465         if (g_strcmp0(address, tunnel_ip_address) != 0)
466                 return;
467
468         if (tunnel_created)
469                 tunnel_destroy();
470 }
471
472 int __connman_6to4_check(struct connman_ipconfig *ip4config)
473 {
474         const char *address;
475
476         if (ip4config == NULL || tunnel_created == 0 ||
477                                         tunnel_pending == 1)
478                 return -1;
479
480         DBG("tunnel ip address %s", tunnel_ip_address);
481
482         address = __connman_ipconfig_get_local(ip4config);
483         if (address == NULL)
484                 return -1;
485
486         if (g_strcmp0(address, tunnel_ip_address) == 0)
487                 return 1;
488
489         return 0;
490 }