7ca19d096dfd81d8d77f92228f8ecab48f54bca2
[platform/upstream/connman.git] / src / dhcp.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <connman/ipconfig.h>
31 #include <include/setting.h>
32
33 #include <gdhcp/gdhcp.h>
34
35 #include <glib.h>
36
37 #include "connman.h"
38
39 #define RATE_LIMIT_INTERVAL     60      /* delay between successive attempts */
40
41 struct connman_dhcp {
42         struct connman_ipconfig *ipconfig;
43         struct connman_network *network;
44         dhcp_cb callback;
45         gpointer user_data;
46
47         char **nameservers;
48         char **timeservers;
49         char *pac;
50
51         unsigned int timeout;
52
53         GDHCPClient *ipv4ll_client;
54         GDHCPClient *dhcp_client;
55         char *ipv4ll_debug_prefix;
56         char *dhcp_debug_prefix;
57 };
58
59 static GHashTable *ipconfig_table;
60 static bool ipv4ll_running;
61
62 static void dhcp_free(struct connman_dhcp *dhcp)
63 {
64         g_strfreev(dhcp->nameservers);
65         g_strfreev(dhcp->timeservers);
66         g_free(dhcp->pac);
67
68         dhcp->nameservers = NULL;
69         dhcp->timeservers = NULL;
70         dhcp->pac = NULL;
71
72         g_free(dhcp);
73 }
74
75 static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
76 {
77         if (!dhcp->ipv4ll_client)
78                 return;
79
80         g_dhcp_client_stop(dhcp->ipv4ll_client);
81         g_dhcp_client_unref(dhcp->ipv4ll_client);
82         dhcp->ipv4ll_client = NULL;
83         ipv4ll_running = false;
84
85         g_free(dhcp->ipv4ll_debug_prefix);
86         dhcp->ipv4ll_debug_prefix = NULL;
87 }
88
89 static bool apply_dhcp_invalidate_on_network(struct connman_dhcp *dhcp)
90 {
91         struct connman_service *service;
92         int i;
93
94         if (!dhcp->network)
95                 return true;
96
97         service = connman_service_lookup_from_network(dhcp->network);
98         if (!service) {
99                 connman_error("Can not lookup service");
100                 return false;
101         }
102
103         __connman_service_set_domainname(service, NULL);
104         __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig, NULL);
105
106         if (dhcp->timeservers) {
107                 for (i = 0; dhcp->timeservers[i]; i++) {
108                         __connman_service_timeserver_remove(service,
109                                                         dhcp->timeservers[i]);
110                 }
111         }
112         if (dhcp->nameservers) {
113                 for (i = 0; dhcp->nameservers[i]; i++) {
114                         __connman_service_nameserver_remove(service,
115                                                 dhcp->nameservers[i], false);
116                 }
117         }
118
119         return true;
120 }
121
122 /**
123  * dhcp_invalidate: Invalidate an existing DHCP lease
124  * @dhcp: pointer to the DHCP lease to invalidate.
125  * @callback: flag indicating whether or not to invoke the client callback
126  *            if present.
127  *
128  * Invalidates an existing DHCP lease, optionally invoking the client
129  * callback. The caller may wish to avoid the client callback invocation
130  * when the invocation of that callback might otherwise unnecessarily upset
131  * service state due to the IP configuration change implied by this
132  * invalidation.
133  */
134 static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
135 {
136         DBG("dhcp %p callback %u", dhcp, callback);
137
138         if (!dhcp)
139                 return;
140
141         __connman_6to4_remove(dhcp->ipconfig);
142
143         if (!apply_dhcp_invalidate_on_network(dhcp))
144                 return;
145
146         __connman_ipconfig_set_dhcp_address(dhcp->ipconfig,
147                                 __connman_ipconfig_get_local(dhcp->ipconfig));
148         DBG("last address %s",
149                         __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
150
151         __connman_ipconfig_address_remove(dhcp->ipconfig);
152
153         __connman_ipconfig_set_local(dhcp->ipconfig, NULL);
154         __connman_ipconfig_set_broadcast(dhcp->ipconfig, NULL);
155         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
156         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, 0);
157
158         if (dhcp->callback && callback)
159                 dhcp->callback(dhcp->ipconfig, dhcp->network,
160                                                 false, dhcp->user_data);
161 }
162
163 static void dhcp_valid(struct connman_dhcp *dhcp)
164 {
165         if (dhcp->callback)
166                 dhcp->callback(dhcp->ipconfig, dhcp->network,
167                                                 true, dhcp->user_data);
168 }
169
170 static void dhcp_debug(const char *str, void *data)
171 {
172         connman_info("%s: %s", (const char *) data, str);
173 }
174
175 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
176 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);
177
178 static int ipv4ll_start_client(struct connman_dhcp *dhcp)
179 {
180         GDHCPClient *ipv4ll_client;
181         GDHCPClientError error;
182         const char *hostname;
183         int index;
184         int err;
185
186         if (dhcp->ipv4ll_client)
187                 return -EALREADY;
188
189         index = __connman_ipconfig_get_index(dhcp->ipconfig);
190
191         ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
192         if (error != G_DHCP_CLIENT_ERROR_NONE)
193                 return -EINVAL;
194
195 #if !defined TIZEN_EXT
196         if (getenv("CONNMAN_DHCP_DEBUG")) {
197 #endif
198                 dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
199                                                         index);
200                 g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
201                                         dhcp->ipv4ll_debug_prefix);
202 #if !defined TIZEN_EXT
203         }
204 #endif
205
206         g_dhcp_client_set_id(ipv4ll_client);
207
208         if (dhcp->network) {
209                 hostname = connman_utsname_get_hostname();
210                 if (hostname)
211                         g_dhcp_client_set_send(ipv4ll_client,
212                                                 G_DHCP_HOST_NAME, hostname);
213         }
214
215         g_dhcp_client_register_event(ipv4ll_client,
216                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
217
218         g_dhcp_client_register_event(ipv4ll_client,
219                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
220                                                 ipv4ll_available_cb, dhcp);
221
222         dhcp->ipv4ll_client = ipv4ll_client;
223
224         err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
225         if (err < 0) {
226                 ipv4ll_stop_client(dhcp);
227                 return err;
228         }
229
230         ipv4ll_running = true;
231         return 0;
232 }
233
234 static gboolean dhcp_retry_cb(gpointer user_data)
235 {
236         struct connman_dhcp *dhcp = user_data;
237
238         dhcp->timeout = 0;
239
240         g_dhcp_client_start(dhcp->dhcp_client,
241                         __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
242
243         return FALSE;
244 }
245
246 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
247 {
248         struct connman_dhcp *dhcp = user_data;
249         int err;
250
251         DBG("No lease available ipv4ll %d client %p", ipv4ll_running,
252                 dhcp->ipv4ll_client);
253
254         dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
255                                                 dhcp_retry_cb,
256                                                 dhcp);
257         if (ipv4ll_running)
258                 return;
259
260         err = ipv4ll_start_client(dhcp);
261         if (err < 0)
262                 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
263
264         /* Only notify upper layer if we have a problem */
265         dhcp_invalidate(dhcp, !ipv4ll_running);
266 }
267
268 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
269 {
270         struct connman_dhcp *dhcp = user_data;
271
272         DBG("Lease lost");
273
274         /* Upper layer will decide what to do, e.g. nothing or retry. */
275         dhcp_invalidate(dhcp, true);
276 }
277
278 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
279 {
280         struct connman_dhcp *dhcp = user_data;
281
282         DBG("Lease lost");
283
284         ipv4ll_stop_client(dhcp);
285
286         /*
287          * Since we lost our IPv4LL configuration we might as notify
288          * the upper layers.
289          */
290         dhcp_invalidate(dhcp, true);
291 }
292
293 static bool compare_string_arrays(char **array_a, char **array_b)
294 {
295         int i;
296
297         if (!array_a || !array_b)
298                 return false;
299
300         if (g_strv_length(array_a) != g_strv_length(array_b))
301                 return false;
302
303         for (i = 0; array_a[i] &&
304                              array_b[i]; i++) {
305                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
306                         return false;
307         }
308
309         return true;
310 }
311
312 static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
313                                                 struct connman_dhcp *dhcp)
314 {
315         char **nameservers, **timeservers, *pac = NULL;
316         struct connman_service *service;
317         GList *list, *option = NULL;
318         int ns_entries;
319         int i;
320
321         if (!dhcp->network)
322                 return true;
323
324         service = connman_service_lookup_from_network(dhcp->network);
325         if (!service) {
326                 connman_error("Can not lookup service");
327                 return false;
328         }
329
330         option = g_dhcp_client_get_option(dhcp_client, 252);
331         if (option)
332                 pac = g_strdup(option->data);
333
334         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
335         ns_entries = g_list_length(option);
336         nameservers = g_try_new0(char *, ns_entries + 1);
337         if (nameservers) {
338                 for (i = 0, list = option;list; list = list->next, i++)
339                         nameservers[i] = g_strdup(list->data);
340                 nameservers[ns_entries] = NULL;
341         }
342
343         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
344         if (option)
345                 __connman_service_set_domainname(service, option->data);
346
347         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
348         if (option)
349                 __connman_service_set_hostname(service, option->data);
350
351         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
352         ns_entries = g_list_length(option);
353         timeservers = g_try_new0(char *, ns_entries + 1);
354         if (timeservers) {
355                 for (i = 0, list = option; list; list = list->next, i++)
356                         timeservers[i] = g_strdup(list->data);
357                 timeservers[ns_entries] = NULL;
358         }
359
360         if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
361                 if (dhcp->nameservers) {
362                         for (i = 0; dhcp->nameservers[i]; i++) {
363                                 __connman_service_nameserver_remove(service,
364                                                 dhcp->nameservers[i], false);
365                         }
366                         g_strfreev(dhcp->nameservers);
367                 }
368
369                 dhcp->nameservers = nameservers;
370
371                 for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
372                         __connman_service_nameserver_append(service,
373                                                 dhcp->nameservers[i], false);
374                 }
375         } else {
376                 g_strfreev(nameservers);
377         }
378
379         if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
380                 if (dhcp->timeservers) {
381                         for (i = 0; dhcp->timeservers[i]; i++) {
382                                 __connman_service_timeserver_remove(service,
383                                                         dhcp->timeservers[i]);
384                         }
385                         g_strfreev(dhcp->timeservers);
386                 }
387
388                 dhcp->timeservers = timeservers;
389
390                 for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
391                         __connman_service_timeserver_append(service,
392                                                         dhcp->timeservers[i]);
393                 }
394         } else {
395                 g_strfreev(timeservers);
396         }
397
398         if (g_strcmp0(pac, dhcp->pac) != 0) {
399                 g_free(dhcp->pac);
400                 dhcp->pac = pac;
401
402                 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
403                                                                 dhcp->pac);
404         }
405
406         if (connman_setting_get_bool("Enable6to4"))
407                 __connman_6to4_probe(service);
408
409         return true;
410 }
411
412 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
413 {
414         struct connman_dhcp *dhcp = user_data;
415         GList *option = NULL;
416         char *address, *netmask = NULL, *gateway = NULL;
417         const char *c_address, *c_gateway;
418         unsigned char prefixlen, c_prefixlen;
419         bool ip_change;
420
421         DBG("Lease available");
422
423         if (dhcp->ipv4ll_client) {
424                 ipv4ll_stop_client(dhcp);
425                 dhcp_invalidate(dhcp, false);
426         }
427
428         c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
429         c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
430         c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
431
432         address = g_dhcp_client_get_address(dhcp_client);
433
434         __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
435         DBG("last address %s", address);
436
437         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
438         if (option)
439                 netmask = g_strdup(option->data);
440
441         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
442         if (option)
443                 gateway = g_strdup(option->data);
444
445         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
446         if (prefixlen == 255)
447                 connman_warn("netmask: %s is invalid", netmask);
448
449         DBG("c_address %s", c_address);
450
451         if (g_strcmp0(address, c_address))
452                 ip_change = true;
453         else if (g_strcmp0(gateway, c_gateway))
454                 ip_change = true;
455         else if (prefixlen != c_prefixlen)
456                 ip_change = true;
457         else
458                 ip_change = false;
459
460         __connman_ipconfig_set_method(dhcp->ipconfig,
461                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
462         if (ip_change) {
463                 __connman_ipconfig_set_local(dhcp->ipconfig, address);
464                 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
465                 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
466         }
467
468         if (!apply_lease_available_on_network(dhcp_client, dhcp))
469                 goto done;
470
471         if (ip_change)
472                 dhcp_valid(dhcp);
473
474 done:
475         g_free(address);
476         g_free(netmask);
477         g_free(gateway);
478 }
479
480 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
481 {
482         struct connman_dhcp *dhcp = user_data;
483                 char *address, *netmask;
484                 unsigned char prefixlen;
485
486         DBG("IPV4LL available");
487
488         address = g_dhcp_client_get_address(ipv4ll_client);
489         netmask = g_dhcp_client_get_netmask(ipv4ll_client);
490
491         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
492
493         __connman_ipconfig_set_method(dhcp->ipconfig,
494                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
495         __connman_ipconfig_set_local(dhcp->ipconfig, address);
496         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
497         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
498
499         dhcp_valid(dhcp);
500
501         g_free(address);
502         g_free(netmask);
503 }
504
505 static int dhcp_initialize(struct connman_dhcp *dhcp)
506 {
507         GDHCPClient *dhcp_client;
508         GDHCPClientError error;
509         int index;
510
511         DBG("dhcp %p", dhcp);
512
513         index = __connman_ipconfig_get_index(dhcp->ipconfig);
514
515         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
516         if (error != G_DHCP_CLIENT_ERROR_NONE)
517 #if defined TIZEN_EXT
518         {
519                 DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
520 #endif
521                 return -EINVAL;
522 #if defined TIZEN_EXT
523         }
524 #endif
525
526 #if !defined TIZEN_EXT
527         if (getenv("CONNMAN_DHCP_DEBUG")) {
528 #endif
529                 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
530                                                         index);
531                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
532                                         dhcp->dhcp_debug_prefix);
533 #if !defined TIZEN_EXT
534         }
535 #endif
536
537         g_dhcp_client_set_id(dhcp_client);
538
539         if (dhcp->network) {
540                 struct connman_service *service;
541                 const char *hostname;
542
543                 service = connman_service_lookup_from_network(dhcp->network);
544
545                 hostname = __connman_service_get_hostname(service);
546                 if (!hostname)
547                         hostname = connman_utsname_get_hostname();
548
549                 if (hostname)
550                         g_dhcp_client_set_send(dhcp_client,
551                                                 G_DHCP_HOST_NAME, hostname);
552
553                 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
554                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
555                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
556                 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
557                 g_dhcp_client_set_request(dhcp_client, 252);
558         }
559
560         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
561         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
562
563         g_dhcp_client_register_event(dhcp_client,
564                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
565                                                 lease_available_cb, dhcp);
566
567         g_dhcp_client_register_event(dhcp_client,
568                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
569
570         g_dhcp_client_register_event(dhcp_client,
571                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
572
573         dhcp->dhcp_client = dhcp_client;
574
575         return 0;
576 }
577
578 static int dhcp_release(struct connman_dhcp *dhcp)
579 {
580         DBG("dhcp %p", dhcp);
581
582         if (dhcp->timeout > 0) {
583                 g_source_remove(dhcp->timeout);
584                 dhcp->timeout = 0;
585         }
586
587         if (dhcp->dhcp_client) {
588                 g_dhcp_client_stop(dhcp->dhcp_client);
589                 g_dhcp_client_unref(dhcp->dhcp_client);
590         }
591
592         dhcp->dhcp_client = NULL;
593
594         g_free(dhcp->dhcp_debug_prefix);
595         dhcp->dhcp_debug_prefix = NULL;
596
597         ipv4ll_stop_client(dhcp);
598
599         return 0;
600 }
601
602 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
603 {
604         struct connman_dhcp *dhcp;
605
606         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
607         if (!dhcp)
608                 return NULL;
609
610         return g_dhcp_client_get_server_address(dhcp->dhcp_client);
611 }
612
613 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
614                         struct connman_network *network, dhcp_cb callback,
615                         gpointer user_data)
616 {
617         const char *last_addr = NULL;
618         struct connman_dhcp *dhcp;
619         int err;
620
621         DBG("");
622
623         if (network) {
624                 struct connman_service *service;
625
626                 service = connman_service_lookup_from_network(network);
627                 if (!service)
628                         return -EINVAL;
629         }
630
631         last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
632
633         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
634         if (!dhcp) {
635
636                 dhcp = g_try_new0(struct connman_dhcp, 1);
637                 if (!dhcp)
638                         return -ENOMEM;
639
640                 dhcp->ipconfig = ipconfig;
641                 __connman_ipconfig_ref(ipconfig);
642
643                 if (network) {
644                         dhcp->network = network;
645                         connman_network_ref(network);
646                 }
647
648                 err = dhcp_initialize(dhcp);
649
650                 if (err < 0) {
651                         if (network)
652                                 connman_network_unref(network);
653                         g_free(dhcp);
654                         return err;
655                 }
656
657                 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
658         }
659
660         dhcp->callback = callback;
661         dhcp->user_data = user_data;
662
663         return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
664 }
665
666 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
667 {
668         struct connman_dhcp *dhcp;
669
670         DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
671
672         if (!ipconfig_table)
673                 return;
674
675         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
676         if (dhcp) {
677                 g_hash_table_remove(ipconfig_table, ipconfig);
678                 __connman_ipconfig_unref(ipconfig);
679                 if (dhcp->network)
680                         connman_network_unref(dhcp->network);
681                 dhcp_release(dhcp);
682                 dhcp_invalidate(dhcp, false);
683                 dhcp_free(dhcp);
684         }
685 }
686
687 int __connman_dhcp_init(void)
688 {
689         DBG("");
690
691         ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
692                                                                 NULL, NULL);
693
694         return 0;
695 }
696
697 void __connman_dhcp_cleanup(void)
698 {
699         DBG("");
700
701         g_hash_table_destroy(ipconfig_table);
702         ipconfig_table = NULL;
703
704         dhcp_cleanup_random();
705 }