Imported Upstream version 1.40
[platform/upstream/connman.git] / src / connection.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6  *  Copyright (C) 2011-2014  BMW Car IT GmbH.
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 <string.h>
29 #include <net/if.h>
30
31 #include <gdbus.h>
32
33 #include "connman.h"
34
35 struct gateway_config {
36         bool active;
37         char *gateway;
38
39         /* VPN extra data */
40         bool vpn;
41         char *vpn_ip;
42         int vpn_phy_index;
43         char *vpn_phy_ip;
44 };
45
46 struct gateway_data {
47         int index;
48         struct connman_service *service;
49         struct gateway_config *ipv4_gateway;
50         struct gateway_config *ipv6_gateway;
51         bool default_checked;
52 };
53
54 static GHashTable *gateway_hash = NULL;
55
56 static struct gateway_config *find_gateway(int index, const char *gateway)
57 {
58         GHashTableIter iter;
59         gpointer value, key;
60
61         if (!gateway)
62                 return NULL;
63
64         g_hash_table_iter_init(&iter, gateway_hash);
65
66         while (g_hash_table_iter_next(&iter, &key, &value)) {
67                 struct gateway_data *data = value;
68
69                 if (data->ipv4_gateway && data->index == index &&
70                                 g_str_equal(data->ipv4_gateway->gateway,
71                                         gateway))
72                         return data->ipv4_gateway;
73
74                 if (data->ipv6_gateway && data->index == index &&
75                                 g_str_equal(data->ipv6_gateway->gateway,
76                                         gateway))
77                         return data->ipv6_gateway;
78         }
79
80         return NULL;
81 }
82
83 static struct gateway_data *lookup_gateway_data(struct gateway_config *config)
84 {
85         GHashTableIter iter;
86         gpointer value, key;
87
88         if (!config)
89                 return NULL;
90
91         g_hash_table_iter_init(&iter, gateway_hash);
92
93         while (g_hash_table_iter_next(&iter, &key, &value)) {
94                 struct gateway_data *data = value;
95
96                 if (data->ipv4_gateway &&
97                                 data->ipv4_gateway == config)
98                         return data;
99
100                 if (data->ipv6_gateway &&
101                                 data->ipv6_gateway == config)
102                         return data;
103         }
104
105         return NULL;
106 }
107
108 static struct gateway_data *find_vpn_gateway(int index, const char *gateway)
109 {
110         GHashTableIter iter;
111         gpointer value, key;
112
113         if (!gateway)
114                 return NULL;
115
116         g_hash_table_iter_init(&iter, gateway_hash);
117
118         while (g_hash_table_iter_next(&iter, &key, &value)) {
119                 struct gateway_data *data = value;
120
121                 if (data->ipv4_gateway && data->index == index &&
122                                 g_str_equal(data->ipv4_gateway->gateway,
123                                         gateway))
124                         return data;
125
126                 if (data->ipv6_gateway && data->index == index &&
127                                 g_str_equal(data->ipv6_gateway->gateway,
128                                         gateway))
129                         return data;
130         }
131
132         return NULL;
133 }
134
135 struct get_gateway_params {
136         char *vpn_gateway;
137         int vpn_index;
138 };
139
140 static void get_gateway_cb(const char *gateway, int index, void *user_data)
141 {
142         struct gateway_config *config;
143         struct gateway_data *data;
144         struct get_gateway_params *params = user_data;
145         int family;
146
147         if (index < 0)
148                 goto out;
149
150         DBG("phy index %d phy gw %s vpn index %d vpn gw %s", index, gateway,
151                 params->vpn_index, params->vpn_gateway);
152
153         data = find_vpn_gateway(params->vpn_index, params->vpn_gateway);
154         if (!data) {
155                 DBG("Cannot find VPN link route, index %d addr %s",
156                         params->vpn_index, params->vpn_gateway);
157                 goto out;
158         }
159
160         family = connman_inet_check_ipaddress(params->vpn_gateway);
161
162         if (family == AF_INET)
163                 config = data->ipv4_gateway;
164         else if (family == AF_INET6)
165                 config = data->ipv6_gateway;
166         else
167                 goto out;
168
169         config->vpn_phy_index = index;
170
171         DBG("vpn %s phy index %d", config->vpn_ip, config->vpn_phy_index);
172
173 out:
174         g_free(params->vpn_gateway);
175         g_free(params);
176 }
177
178 static void set_vpn_routes(struct gateway_data *new_gateway,
179                         struct connman_service *service,
180                         const char *gateway,
181                         enum connman_ipconfig_type type,
182                         const char *peer,
183                         struct gateway_data *active_gateway)
184 {
185         struct gateway_config *config;
186         struct connman_ipconfig *ipconfig;
187         char *dest;
188
189         DBG("new %p service %p gw %s type %d peer %s active %p",
190                 new_gateway, service, gateway, type, peer, active_gateway);
191
192         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
193                 ipconfig = __connman_service_get_ip4config(service);
194                 config = new_gateway->ipv4_gateway;
195         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
196                 ipconfig = __connman_service_get_ip6config(service);
197                 config = new_gateway->ipv6_gateway;
198         } else
199                 return;
200
201         if (config) {
202                 int index = __connman_ipconfig_get_index(ipconfig);
203                 struct get_gateway_params *params;
204
205                 config->vpn = true;
206                 if (peer)
207                         config->vpn_ip = g_strdup(peer);
208                 else if (gateway)
209                         config->vpn_ip = g_strdup(gateway);
210
211                 params = g_try_malloc(sizeof(struct get_gateway_params));
212                 if (!params)
213                         return;
214
215                 params->vpn_index = index;
216                 params->vpn_gateway = g_strdup(gateway);
217
218                 /*
219                  * Find the gateway that is serving the VPN link
220                  */
221                 __connman_inet_get_route(gateway, get_gateway_cb, params);
222         }
223
224         if (!active_gateway)
225                 return;
226
227         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
228                 /*
229                  * Special route to VPN server via gateway. This
230                  * is needed so that we can access hosts behind
231                  * the VPN. The route might already exist depending
232                  * on network topology.
233                  */
234                 if (!active_gateway->ipv4_gateway)
235                         return;
236
237
238                 /*
239                  * If VPN server is on same subnet as we are, skip adding
240                  * route.
241                  */
242                 if (connman_inet_compare_subnet(active_gateway->index,
243                                                                 gateway))
244                         return;
245
246                 DBG("active gw %s", active_gateway->ipv4_gateway->gateway);
247
248                 if (g_strcmp0(active_gateway->ipv4_gateway->gateway,
249                                                         "0.0.0.0") != 0)
250                         dest = active_gateway->ipv4_gateway->gateway;
251                 else
252                         dest = NULL;
253
254                 connman_inet_add_host_route(active_gateway->index, gateway,
255                                                                         dest);
256
257         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
258
259                 if (!active_gateway->ipv6_gateway)
260                         return;
261
262                 if (connman_inet_compare_ipv6_subnet(active_gateway->index,
263                                                                 gateway))
264                         return;
265
266                 DBG("active gw %s", active_gateway->ipv6_gateway->gateway);
267
268                 if (g_strcmp0(active_gateway->ipv6_gateway->gateway,
269                                                                 "::") != 0)
270                         dest = active_gateway->ipv6_gateway->gateway;
271                 else
272                         dest = NULL;
273
274                 connman_inet_add_ipv6_host_route(active_gateway->index,
275                                                                 gateway, dest);
276         }
277 }
278
279 static int del_routes(struct gateway_data *data,
280                         enum connman_ipconfig_type type)
281 {
282         int status4 = 0, status6 = 0;
283         bool do_ipv4 = false, do_ipv6 = false;
284
285         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
286                 do_ipv4 = true;
287         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
288                 do_ipv6 = true;
289         else
290                 do_ipv4 = do_ipv6 = true;
291
292         if (do_ipv4 && data->ipv4_gateway) {
293                 if (data->ipv4_gateway->vpn) {
294                         status4 = connman_inet_clear_gateway_address(
295                                                 data->index,
296                                                 data->ipv4_gateway->vpn_ip);
297
298                 } else if (g_strcmp0(data->ipv4_gateway->gateway,
299                                                         "0.0.0.0") == 0) {
300                         status4 = connman_inet_clear_gateway_interface(
301                                                                 data->index);
302                 } else {
303                         connman_inet_del_host_route(data->index,
304                                                 data->ipv4_gateway->gateway);
305                         status4 = connman_inet_clear_gateway_address(
306                                                 data->index,
307                                                 data->ipv4_gateway->gateway);
308                 }
309         }
310
311         if (do_ipv6 && data->ipv6_gateway) {
312                 if (data->ipv6_gateway->vpn) {
313                         status6 = connman_inet_clear_ipv6_gateway_address(
314                                                 data->index,
315                                                 data->ipv6_gateway->vpn_ip);
316
317                 } else if (g_strcmp0(data->ipv6_gateway->gateway, "::") == 0) {
318                         status6 = connman_inet_clear_ipv6_gateway_interface(
319                                                                 data->index);
320                 } else {
321                         connman_inet_del_ipv6_host_route(data->index,
322                                                 data->ipv6_gateway->gateway);
323                         status6 = connman_inet_clear_ipv6_gateway_address(
324                                                 data->index,
325                                                 data->ipv6_gateway->gateway);
326                 }
327         }
328
329         return (status4 < 0 ? status4 : status6);
330 }
331
332 static int disable_gateway(struct gateway_data *data,
333                         enum connman_ipconfig_type type)
334 {
335         bool active = false;
336
337         if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
338                 if (data->ipv4_gateway)
339                         active = data->ipv4_gateway->active;
340         } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
341                 if (data->ipv6_gateway)
342                         active = data->ipv6_gateway->active;
343         } else
344                 active = true;
345
346         DBG("type %d active %d", type, active);
347
348         if (active)
349                 return del_routes(data, type);
350
351         return 0;
352 }
353
354 static struct gateway_data *add_gateway(struct connman_service *service,
355                                         int index, const char *gateway,
356                                         enum connman_ipconfig_type type)
357 {
358         struct gateway_data *data, *old;
359         struct gateway_config *config;
360
361         if (!gateway || strlen(gateway) == 0)
362                 return NULL;
363
364         data = g_try_new0(struct gateway_data, 1);
365         if (!data)
366                 return NULL;
367
368         data->index = index;
369
370         config = g_try_new0(struct gateway_config, 1);
371         if (!config) {
372                 g_free(data);
373                 return NULL;
374         }
375
376         config->gateway = g_strdup(gateway);
377         config->vpn_ip = NULL;
378         config->vpn_phy_ip = NULL;
379         config->vpn = false;
380         config->vpn_phy_index = -1;
381         config->active = false;
382
383         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
384                 data->ipv4_gateway = config;
385         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
386                 data->ipv6_gateway = config;
387         else {
388                 g_free(config->gateway);
389                 g_free(config);
390                 g_free(data);
391                 return NULL;
392         }
393
394         data->service = service;
395
396         /*
397          * If the service is already in the hash, then we
398          * must not replace it blindly but disable the gateway
399          * of the type we are replacing and take the other type
400          * from old gateway settings.
401          */
402         old = g_hash_table_lookup(gateway_hash, service);
403         if (old) {
404                 DBG("Replacing gw %p ipv4 %p ipv6 %p", old,
405                         old->ipv4_gateway, old->ipv6_gateway);
406                 disable_gateway(old, type);
407                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
408                         data->ipv6_gateway = old->ipv6_gateway;
409                         old->ipv6_gateway = NULL;
410                 } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
411                         data->ipv4_gateway = old->ipv4_gateway;
412                         old->ipv4_gateway = NULL;
413                 }
414         }
415
416         connman_service_ref(data->service);
417         g_hash_table_replace(gateway_hash, service, data);
418
419         return data;
420 }
421
422 static void set_default_gateway(struct gateway_data *data,
423                                 enum connman_ipconfig_type type)
424 {
425         int index;
426         int status4 = 0, status6 = 0;
427         bool do_ipv4 = false, do_ipv6 = false;
428
429         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
430                 do_ipv4 = true;
431         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
432                 do_ipv6 = true;
433         else
434                 do_ipv4 = do_ipv6 = true;
435
436         DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
437                                                 data->ipv6_gateway);
438
439         if (do_ipv4 && data->ipv4_gateway &&
440                                         data->ipv4_gateway->vpn) {
441                 connman_inet_set_gateway_interface(data->index);
442                 data->ipv4_gateway->active = true;
443
444                 DBG("set %p index %d vpn %s index %d phy %s",
445                         data, data->index, data->ipv4_gateway->vpn_ip,
446                         data->ipv4_gateway->vpn_phy_index,
447                         data->ipv4_gateway->vpn_phy_ip);
448
449                 __connman_service_indicate_default(data->service);
450
451                 return;
452         }
453
454         if (do_ipv6 && data->ipv6_gateway &&
455                                         data->ipv6_gateway->vpn) {
456                 connman_inet_set_ipv6_gateway_interface(data->index);
457                 data->ipv6_gateway->active = true;
458
459                 DBG("set %p index %d vpn %s index %d phy %s",
460                         data, data->index, data->ipv6_gateway->vpn_ip,
461                         data->ipv6_gateway->vpn_phy_index,
462                         data->ipv6_gateway->vpn_phy_ip);
463
464                 __connman_service_indicate_default(data->service);
465
466                 return;
467         }
468
469         index = __connman_service_get_index(data->service);
470
471         if (do_ipv4 && data->ipv4_gateway &&
472                         g_strcmp0(data->ipv4_gateway->gateway,
473                                                         "0.0.0.0") == 0) {
474                 if (connman_inet_set_gateway_interface(index) < 0)
475                         return;
476                 data->ipv4_gateway->active = true;
477                 goto done;
478         }
479
480         if (do_ipv6 && data->ipv6_gateway &&
481                         g_strcmp0(data->ipv6_gateway->gateway,
482                                                         "::") == 0) {
483                 if (connman_inet_set_ipv6_gateway_interface(index) < 0)
484                         return;
485                 data->ipv6_gateway->active = true;
486                 goto done;
487         }
488
489         if (do_ipv6 && data->ipv6_gateway)
490                 status6 = __connman_inet_add_default_to_table(RT_TABLE_MAIN,
491                                         index, data->ipv6_gateway->gateway);
492
493         if (do_ipv4 && data->ipv4_gateway)
494                 status4 = __connman_inet_add_default_to_table(RT_TABLE_MAIN,
495                                         index, data->ipv4_gateway->gateway);
496
497         if (status4 < 0 || status6 < 0)
498                 return;
499
500 done:
501         __connman_service_indicate_default(data->service);
502 }
503
504 static void unset_default_gateway(struct gateway_data *data,
505                                 enum connman_ipconfig_type type)
506 {
507         int index;
508         bool do_ipv4 = false, do_ipv6 = false;
509
510         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
511                 do_ipv4 = true;
512         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
513                 do_ipv6 = true;
514         else
515                 do_ipv4 = do_ipv6 = true;
516
517         DBG("type %d gateway ipv4 %p ipv6 %p", type, data->ipv4_gateway,
518                                                 data->ipv6_gateway);
519
520         if (do_ipv4 && data->ipv4_gateway &&
521                                         data->ipv4_gateway->vpn) {
522                 connman_inet_clear_gateway_interface(data->index);
523                 data->ipv4_gateway->active = false;
524
525                 DBG("unset %p index %d vpn %s index %d phy %s",
526                         data, data->index, data->ipv4_gateway->vpn_ip,
527                         data->ipv4_gateway->vpn_phy_index,
528                         data->ipv4_gateway->vpn_phy_ip);
529
530                 return;
531         }
532
533         if (do_ipv6 && data->ipv6_gateway &&
534                                         data->ipv6_gateway->vpn) {
535                 connman_inet_clear_ipv6_gateway_interface(data->index);
536                 data->ipv6_gateway->active = false;
537
538                 DBG("unset %p index %d vpn %s index %d phy %s",
539                         data, data->index, data->ipv6_gateway->vpn_ip,
540                         data->ipv6_gateway->vpn_phy_index,
541                         data->ipv6_gateway->vpn_phy_ip);
542
543                 return;
544         }
545
546         index = __connman_service_get_index(data->service);
547
548         if (do_ipv4 && data->ipv4_gateway &&
549                         g_strcmp0(data->ipv4_gateway->gateway,
550                                                         "0.0.0.0") == 0) {
551                 connman_inet_clear_gateway_interface(index);
552                 data->ipv4_gateway->active = false;
553                 return;
554         }
555
556         if (do_ipv6 && data->ipv6_gateway &&
557                         g_strcmp0(data->ipv6_gateway->gateway,
558                                                         "::") == 0) {
559                 connman_inet_clear_ipv6_gateway_interface(index);
560                 data->ipv6_gateway->active = false;
561                 return;
562         }
563
564         if (do_ipv6 && data->ipv6_gateway)
565                 connman_inet_clear_ipv6_gateway_address(index,
566                                                 data->ipv6_gateway->gateway);
567
568         if (do_ipv4 && data->ipv4_gateway)
569                 connman_inet_clear_gateway_address(index,
570                                                 data->ipv4_gateway->gateway);
571 }
572
573 static struct gateway_data *find_default_gateway(void)
574 {
575         struct connman_service *service;
576
577         service = connman_service_get_default();
578         if (!service)
579                 return NULL;
580
581         return g_hash_table_lookup(gateway_hash, service);
582 }
583
584 static bool choose_default_gateway(struct gateway_data *data,
585                                         struct gateway_data *candidate)
586 {
587         bool downgraded = false;
588
589         /*
590          * If the current default is not active, then we mark
591          * this one as default. If the other one is already active
592          * we mark this one as non default.
593          */
594         if (data->ipv4_gateway && candidate->ipv4_gateway) {
595
596                 if (!candidate->ipv4_gateway->active) {
597                         DBG("ipv4 downgrading %p", candidate);
598                         unset_default_gateway(candidate,
599                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
600                 }
601
602                 if (candidate->ipv4_gateway->active &&
603                                 __connman_service_compare(candidate->service,
604                                                         data->service) < 0) {
605                         DBG("ipv4 downgrading this %p", data);
606                         unset_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV4);
607                         downgraded = true;
608                 }
609         }
610
611         if (data->ipv6_gateway && candidate->ipv6_gateway) {
612                 if (!candidate->ipv6_gateway->active) {
613                         DBG("ipv6 downgrading %p", candidate);
614                         unset_default_gateway(candidate,
615                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
616                 }
617
618                 if (candidate->ipv6_gateway->active &&
619                         __connman_service_compare(candidate->service,
620                                                 data->service) < 0) {
621                         DBG("ipv6 downgrading this %p", data);
622                         unset_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV6);
623                         downgraded = true;
624                 }
625         }
626
627         return downgraded;
628 }
629
630 static void connection_newgateway(int index, const char *gateway)
631 {
632         struct gateway_config *config;
633         struct gateway_data *data;
634         GHashTableIter iter;
635         gpointer value, key;
636         bool found = false;
637
638         DBG("index %d gateway %s", index, gateway);
639
640         config = find_gateway(index, gateway);
641         if (!config)
642                 return;
643
644         config->active = true;
645
646         /*
647          * It is possible that we have two default routes atm
648          * if there are two gateways waiting rtnl activation at the
649          * same time.
650          */
651         data = lookup_gateway_data(config);
652         if (!data)
653                 return;
654
655         if (data->default_checked)
656                 return;
657
658         /*
659          * The next checks are only done once, otherwise setting
660          * the default gateway could lead into rtnl forever loop.
661          */
662
663         g_hash_table_iter_init(&iter, gateway_hash);
664
665         while (g_hash_table_iter_next(&iter, &key, &value)) {
666                 struct gateway_data *candidate = value;
667
668                 if (candidate == data)
669                         continue;
670
671                 found = choose_default_gateway(data, candidate);
672                 if (found)
673                         break;
674         }
675
676         if (!found) {
677                 if (data->ipv4_gateway)
678                         set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV4);
679
680                 if (data->ipv6_gateway)
681                         set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_IPV6);
682         }
683
684         data->default_checked = true;
685 }
686
687 static void remove_gateway(gpointer user_data)
688 {
689         struct gateway_data *data = user_data;
690
691         DBG("gateway ipv4 %p ipv6 %p", data->ipv4_gateway, data->ipv6_gateway);
692
693         if (data->ipv4_gateway) {
694                 g_free(data->ipv4_gateway->gateway);
695                 g_free(data->ipv4_gateway->vpn_ip);
696                 g_free(data->ipv4_gateway->vpn_phy_ip);
697                 g_free(data->ipv4_gateway);
698         }
699
700         if (data->ipv6_gateway) {
701                 g_free(data->ipv6_gateway->gateway);
702                 g_free(data->ipv6_gateway->vpn_ip);
703                 g_free(data->ipv6_gateway->vpn_phy_ip);
704                 g_free(data->ipv6_gateway);
705         }
706
707         connman_service_unref(data->service);
708
709         g_free(data);
710 }
711
712 static void connection_delgateway(int index, const char *gateway)
713 {
714         struct gateway_config *config;
715         struct gateway_data *data;
716
717         DBG("index %d gateway %s", index, gateway);
718
719         config = find_gateway(index, gateway);
720         if (config)
721                 config->active = false;
722
723         data = find_default_gateway();
724         if (data)
725                 set_default_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
726 }
727
728 static struct connman_rtnl connection_rtnl = {
729         .name           = "connection",
730         .newgateway     = connection_newgateway,
731         .delgateway     = connection_delgateway,
732 };
733
734 static struct gateway_data *find_active_gateway(void)
735 {
736         GHashTableIter iter;
737         gpointer value, key;
738
739         DBG("");
740
741         g_hash_table_iter_init(&iter, gateway_hash);
742
743         while (g_hash_table_iter_next(&iter, &key, &value)) {
744                 struct gateway_data *data = value;
745
746                 if (data->ipv4_gateway &&
747                                 data->ipv4_gateway->active)
748                         return data;
749
750                 if (data->ipv6_gateway &&
751                                 data->ipv6_gateway->active)
752                         return data;
753         }
754
755         return NULL;
756 }
757
758 static void add_host_route(int family, int index, const char *gateway,
759                         enum connman_service_type service_type)
760 {
761         switch (family) {
762         case AF_INET:
763                 if (g_strcmp0(gateway, "0.0.0.0") != 0) {
764                         /*
765                          * We must not set route to the phy dev gateway in
766                          * VPN link. The packets to VPN link might be routed
767                          * back to itself and not routed into phy link gateway.
768                          */
769                         if (service_type != CONNMAN_SERVICE_TYPE_VPN)
770                                 connman_inet_add_host_route(index, gateway,
771                                                                         NULL);
772                 } else {
773                         /*
774                          * Add host route to P-t-P link so that services can
775                          * be moved around and we can have some link to P-t-P
776                          * network (although those P-t-P links have limited
777                          * usage if default route is not directed to them)
778                          */
779                         char *dest;
780                         if (connman_inet_get_dest_addr(index, &dest) == 0) {
781                                 connman_inet_add_host_route(index, dest, NULL);
782                                 g_free(dest);
783                         }
784                 }
785                 break;
786
787         case AF_INET6:
788                 if (g_strcmp0(gateway, "::") != 0) {
789                         if (service_type != CONNMAN_SERVICE_TYPE_VPN)
790                                 connman_inet_add_ipv6_host_route(index,
791                                                                 gateway, NULL);
792                 } else {
793                         /* P-t-P link, add route to destination */
794                         char *dest;
795                         if (connman_inet_ipv6_get_dest_addr(index,
796                                                                 &dest) == 0) {
797                                 connman_inet_add_ipv6_host_route(index, dest,
798                                                                 NULL);
799                                 g_free(dest);
800                         }
801                 }
802                 break;
803         }
804 }
805
806 int __connman_connection_gateway_add(struct connman_service *service,
807                                         const char *gateway,
808                                         enum connman_ipconfig_type type,
809                                         const char *peer)
810 {
811         struct gateway_data *active_gateway = NULL;
812         struct gateway_data *new_gateway = NULL;
813         enum connman_ipconfig_type type4 = CONNMAN_IPCONFIG_TYPE_UNKNOWN,
814                 type6 = CONNMAN_IPCONFIG_TYPE_UNKNOWN;
815         enum connman_service_type service_type =
816                                         connman_service_get_type(service);
817         int index;
818
819         index = __connman_service_get_index(service);
820
821         /*
822          * If gateway is NULL, it's a point to point link and the default
823          * gateway for ipv4 is 0.0.0.0 and for ipv6 is ::, meaning the
824          * interface
825          */
826         if (!gateway && type == CONNMAN_IPCONFIG_TYPE_IPV4)
827                 gateway = "0.0.0.0";
828
829         if (!gateway && type == CONNMAN_IPCONFIG_TYPE_IPV6)
830                 gateway = "::";
831
832         DBG("service %p index %d gateway %s vpn ip %s type %d",
833                 service, index, gateway, peer, type);
834
835         new_gateway = add_gateway(service, index, gateway, type);
836         if (!new_gateway)
837                 return -EINVAL;
838
839         active_gateway = find_active_gateway();
840
841         DBG("active %p index %d new %p", active_gateway,
842                 active_gateway ? active_gateway->index : -1, new_gateway);
843
844         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
845                                 new_gateway->ipv4_gateway) {
846                 add_host_route(AF_INET, index, gateway, service_type);
847                 __connman_service_nameserver_add_routes(service,
848                                         new_gateway->ipv4_gateway->gateway);
849                 type4 = CONNMAN_IPCONFIG_TYPE_IPV4;
850         }
851
852         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
853                                 new_gateway->ipv6_gateway) {
854                 add_host_route(AF_INET6, index, gateway, service_type);
855                 __connman_service_nameserver_add_routes(service,
856                                         new_gateway->ipv6_gateway->gateway);
857                 type6 = CONNMAN_IPCONFIG_TYPE_IPV6;
858         }
859
860         if (service_type == CONNMAN_SERVICE_TYPE_VPN) {
861
862                 set_vpn_routes(new_gateway, service, gateway, type, peer,
863                                                         active_gateway);
864
865         } else {
866                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
867                                         new_gateway->ipv4_gateway)
868                         new_gateway->ipv4_gateway->vpn = false;
869
870                 if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
871                                         new_gateway->ipv6_gateway)
872                         new_gateway->ipv6_gateway->vpn = false;
873         }
874
875         if (!active_gateway) {
876                 set_default_gateway(new_gateway, type);
877                 goto done;
878         }
879
880         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
881                                 new_gateway->ipv4_gateway &&
882                                 new_gateway->ipv4_gateway->vpn) {
883                 if (!__connman_service_is_split_routing(new_gateway->service))
884                         connman_inet_clear_gateway_address(
885                                         active_gateway->index,
886                                         active_gateway->ipv4_gateway->gateway);
887         }
888
889         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
890                                 new_gateway->ipv6_gateway &&
891                                 new_gateway->ipv6_gateway->vpn) {
892                 if (!__connman_service_is_split_routing(new_gateway->service))
893                         connman_inet_clear_ipv6_gateway_address(
894                                         active_gateway->index,
895                                         active_gateway->ipv6_gateway->gateway);
896         }
897
898 done:
899         if (type4 == CONNMAN_IPCONFIG_TYPE_IPV4)
900                 __connman_service_ipconfig_indicate_state(service,
901                                                 CONNMAN_SERVICE_STATE_READY,
902                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
903
904         if (type6 == CONNMAN_IPCONFIG_TYPE_IPV6)
905                 __connman_service_ipconfig_indicate_state(service,
906                                                 CONNMAN_SERVICE_STATE_READY,
907                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
908         return 0;
909 }
910
911 void __connman_connection_gateway_remove(struct connman_service *service,
912                                         enum connman_ipconfig_type type)
913 {
914         struct gateway_data *data = NULL;
915         bool set_default4 = false, set_default6 = false;
916         bool do_ipv4 = false, do_ipv6 = false;
917         int err;
918
919         DBG("service %p type %d", service, type);
920
921         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
922                 do_ipv4 = true;
923         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
924                 do_ipv6 = true;
925         else
926                 do_ipv4 = do_ipv6 = true;
927
928         __connman_service_nameserver_del_routes(service, type);
929
930         data = g_hash_table_lookup(gateway_hash, service);
931         if (!data)
932                 return;
933
934         if (do_ipv4 && data->ipv4_gateway)
935                 set_default4 = data->ipv4_gateway->vpn;
936
937         if (do_ipv6 && data->ipv6_gateway)
938                 set_default6 = data->ipv6_gateway->vpn;
939
940         DBG("ipv4 gateway %s ipv6 gateway %s vpn %d/%d",
941                 data->ipv4_gateway ? data->ipv4_gateway->gateway : "<null>",
942                 data->ipv6_gateway ? data->ipv6_gateway->gateway : "<null>",
943                 set_default4, set_default6);
944
945         if (do_ipv4 && data->ipv4_gateway &&
946                         data->ipv4_gateway->vpn && data->index >= 0)
947                 connman_inet_del_host_route(data->ipv4_gateway->vpn_phy_index,
948                                                 data->ipv4_gateway->gateway);
949
950         if (do_ipv6 && data->ipv6_gateway &&
951                         data->ipv6_gateway->vpn && data->index >= 0)
952                 connman_inet_del_ipv6_host_route(
953                                         data->ipv6_gateway->vpn_phy_index,
954                                                 data->ipv6_gateway->gateway);
955
956         err = disable_gateway(data, type);
957
958         /*
959          * We remove the service from the hash only if all the gateway
960          * settings are to be removed.
961          */
962         if (do_ipv4 == do_ipv6 ||
963                 (data->ipv4_gateway && !data->ipv6_gateway
964                         && do_ipv4) ||
965                 (data->ipv6_gateway && !data->ipv4_gateway
966                         && do_ipv6)) {
967                 g_hash_table_remove(gateway_hash, service);
968         } else
969                 DBG("Not yet removing gw ipv4 %p/%d ipv6 %p/%d",
970                         data->ipv4_gateway, do_ipv4,
971                         data->ipv6_gateway, do_ipv6);
972
973         /* with vpn this will be called after the network was deleted,
974          * we need to call set_default here because we will not receive any
975          * gateway delete notification.
976          * We hit the same issue if remove_gateway() fails.
977          */
978         if (set_default4 || set_default6 || err < 0) {
979                 data = find_default_gateway();
980                 if (data)
981                         set_default_gateway(data, type);
982         }
983 }
984
985 bool __connman_connection_update_gateway(void)
986 {
987         struct gateway_data *default_gateway;
988         bool updated = false;
989         GHashTableIter iter;
990         gpointer value, key;
991
992         if (!gateway_hash)
993                 return updated;
994
995         default_gateway = find_default_gateway();
996
997         DBG("default %p", default_gateway);
998
999         /*
1000          * There can be multiple active gateways so we need to
1001          * check them all.
1002          */
1003         g_hash_table_iter_init(&iter, gateway_hash);
1004
1005         while (g_hash_table_iter_next(&iter, &key, &value)) {
1006                 struct gateway_data *active_gateway = value;
1007
1008                 if (active_gateway == default_gateway)
1009                         continue;
1010
1011                 if (active_gateway->ipv4_gateway &&
1012                                 active_gateway->ipv4_gateway->active) {
1013
1014                         unset_default_gateway(active_gateway,
1015                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
1016                         updated = true;
1017                 }
1018
1019                 if (active_gateway->ipv6_gateway &&
1020                                 active_gateway->ipv6_gateway->active) {
1021
1022                         unset_default_gateway(active_gateway,
1023                                                 CONNMAN_IPCONFIG_TYPE_IPV6);
1024                         updated = true;
1025                 }
1026         }
1027
1028         /*
1029          * Set default gateway if it has been updated or if it has not been
1030          * set as active yet.
1031          */
1032         if (default_gateway) {
1033                 if (default_gateway->ipv4_gateway &&
1034                         (updated || !default_gateway->ipv4_gateway->active))
1035                         set_default_gateway(default_gateway,
1036                                         CONNMAN_IPCONFIG_TYPE_IPV4);
1037
1038                 if (default_gateway->ipv6_gateway &&
1039                         (updated || !default_gateway->ipv6_gateway->active))
1040                         set_default_gateway(default_gateway,
1041                                         CONNMAN_IPCONFIG_TYPE_IPV6);
1042         }
1043
1044         return updated;
1045 }
1046
1047 int __connman_connection_get_vpn_index(int phy_index)
1048 {
1049         GHashTableIter iter;
1050         gpointer value, key;
1051
1052         g_hash_table_iter_init(&iter, gateway_hash);
1053
1054         while (g_hash_table_iter_next(&iter, &key, &value)) {
1055                 struct gateway_data *data = value;
1056
1057                 if (data->ipv4_gateway &&
1058                                 data->ipv4_gateway->vpn_phy_index == phy_index)
1059                         return data->index;
1060
1061                 if (data->ipv6_gateway &&
1062                                 data->ipv6_gateway->vpn_phy_index == phy_index)
1063                         return data->index;
1064         }
1065
1066         return -1;
1067 }
1068
1069 int __connman_connection_get_vpn_phy_index(int vpn_index)
1070 {
1071         GHashTableIter iter;
1072         gpointer value, key;
1073
1074         g_hash_table_iter_init(&iter, gateway_hash);
1075
1076         while (g_hash_table_iter_next(&iter, &key, &value)) {
1077                 struct gateway_data *data = value;
1078
1079                 if (data->index != vpn_index)
1080                         continue;
1081
1082                 if (data->ipv4_gateway)
1083                         return data->ipv4_gateway->vpn_phy_index;
1084
1085                 if (data->ipv6_gateway)
1086                         return data->ipv6_gateway->vpn_phy_index;
1087         }
1088
1089         return -1;
1090 }
1091
1092 int __connman_connection_init(void)
1093 {
1094         int err;
1095
1096         DBG("");
1097
1098         gateway_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1099                                                         NULL, remove_gateway);
1100
1101         err = connman_rtnl_register(&connection_rtnl);
1102         if (err < 0)
1103                 connman_error("Failed to setup RTNL gateway driver");
1104
1105         return err;
1106 }
1107
1108 void __connman_connection_cleanup(void)
1109 {
1110         GHashTableIter iter;
1111         gpointer value, key;
1112
1113         DBG("");
1114
1115         connman_rtnl_unregister(&connection_rtnl);
1116
1117         g_hash_table_iter_init(&iter, gateway_hash);
1118
1119         while (g_hash_table_iter_next(&iter, &key, &value)) {
1120                 struct gateway_data *data = value;
1121
1122                 disable_gateway(data, CONNMAN_IPCONFIG_TYPE_ALL);
1123         }
1124
1125         g_hash_table_destroy(gateway_hash);
1126         gateway_hash = NULL;
1127 }