Base Code merged to SPIN 2.4
[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 (getenv("CONNMAN_DHCP_DEBUG")) {
196                 dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
197                                                         index);
198                 g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
199                                         dhcp->ipv4ll_debug_prefix);
200         }
201
202         g_dhcp_client_set_id(ipv4ll_client);
203
204         if (dhcp->network) {
205                 hostname = connman_utsname_get_hostname();
206                 if (hostname)
207                         g_dhcp_client_set_send(ipv4ll_client,
208                                                 G_DHCP_HOST_NAME, hostname);
209         }
210
211         g_dhcp_client_register_event(ipv4ll_client,
212                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
213
214         g_dhcp_client_register_event(ipv4ll_client,
215                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
216                                                 ipv4ll_available_cb, dhcp);
217
218         dhcp->ipv4ll_client = ipv4ll_client;
219
220         err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
221         if (err < 0) {
222                 ipv4ll_stop_client(dhcp);
223                 return err;
224         }
225
226         ipv4ll_running = true;
227         return 0;
228 }
229
230 static gboolean dhcp_retry_cb(gpointer user_data)
231 {
232         struct connman_dhcp *dhcp = user_data;
233
234         dhcp->timeout = 0;
235
236         g_dhcp_client_start(dhcp->dhcp_client,
237                         __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
238
239         return FALSE;
240 }
241
242 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
243 {
244         struct connman_dhcp *dhcp = user_data;
245         int err;
246
247         DBG("No lease available ipv4ll %d client %p", ipv4ll_running,
248                 dhcp->ipv4ll_client);
249
250         dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
251                                                 dhcp_retry_cb,
252                                                 dhcp);
253         if (ipv4ll_running)
254                 return;
255
256         err = ipv4ll_start_client(dhcp);
257         if (err < 0)
258                 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
259
260         /* Only notify upper layer if we have a problem */
261         dhcp_invalidate(dhcp, !ipv4ll_running);
262 }
263
264 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
265 {
266         struct connman_dhcp *dhcp = user_data;
267
268         DBG("Lease lost");
269
270         /* Upper layer will decide what to do, e.g. nothing or retry. */
271         dhcp_invalidate(dhcp, true);
272 }
273
274 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
275 {
276         struct connman_dhcp *dhcp = user_data;
277
278         DBG("Lease lost");
279
280         ipv4ll_stop_client(dhcp);
281
282         /*
283          * Since we lost our IPv4LL configuration we might as notify
284          * the upper layers.
285          */
286         dhcp_invalidate(dhcp, true);
287 }
288
289 static bool compare_string_arrays(char **array_a, char **array_b)
290 {
291         int i;
292
293         if (!array_a || !array_b)
294                 return false;
295
296         if (g_strv_length(array_a) != g_strv_length(array_b))
297                 return false;
298
299         for (i = 0; array_a[i] &&
300                              array_b[i]; i++) {
301                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
302                         return false;
303         }
304
305         return true;
306 }
307
308 static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
309                                                 struct connman_dhcp *dhcp)
310 {
311         char **nameservers, **timeservers, *pac = NULL;
312         struct connman_service *service;
313         GList *list, *option = NULL;
314         int ns_entries;
315         int i;
316
317         if (!dhcp->network)
318                 return true;
319
320         service = connman_service_lookup_from_network(dhcp->network);
321         if (!service) {
322                 connman_error("Can not lookup service");
323                 return false;
324         }
325
326         option = g_dhcp_client_get_option(dhcp_client, 252);
327         if (option)
328                 pac = g_strdup(option->data);
329
330         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
331         ns_entries = g_list_length(option);
332         nameservers = g_try_new0(char *, ns_entries + 1);
333         if (nameservers) {
334                 for (i = 0, list = option;list; list = list->next, i++)
335                         nameservers[i] = g_strdup(list->data);
336                 nameservers[ns_entries] = NULL;
337         }
338
339         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
340         if (option)
341                 __connman_service_set_domainname(service, option->data);
342
343         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
344         if (option)
345                 __connman_service_set_hostname(service, option->data);
346
347         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
348         ns_entries = g_list_length(option);
349         timeservers = g_try_new0(char *, ns_entries + 1);
350         if (timeservers) {
351                 for (i = 0, list = option; list; list = list->next, i++)
352                         timeservers[i] = g_strdup(list->data);
353                 timeservers[ns_entries] = NULL;
354         }
355
356         if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
357                 if (dhcp->nameservers) {
358                         for (i = 0; dhcp->nameservers[i]; i++) {
359                                 __connman_service_nameserver_remove(service,
360                                                 dhcp->nameservers[i], false);
361                         }
362                         g_strfreev(dhcp->nameservers);
363                 }
364
365                 dhcp->nameservers = nameservers;
366
367                 for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
368                         __connman_service_nameserver_append(service,
369                                                 dhcp->nameservers[i], false);
370                 }
371         } else {
372                 g_strfreev(nameservers);
373         }
374
375         if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
376                 if (dhcp->timeservers) {
377                         for (i = 0; dhcp->timeservers[i]; i++) {
378                                 __connman_service_timeserver_remove(service,
379                                                         dhcp->timeservers[i]);
380                         }
381                         g_strfreev(dhcp->timeservers);
382                 }
383
384                 dhcp->timeservers = timeservers;
385
386                 for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
387                         __connman_service_timeserver_append(service,
388                                                         dhcp->timeservers[i]);
389                 }
390         } else {
391                 g_strfreev(timeservers);
392         }
393
394         if (g_strcmp0(pac, dhcp->pac) != 0) {
395                 g_free(dhcp->pac);
396                 dhcp->pac = pac;
397
398                 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
399                                                                 dhcp->pac);
400         }
401
402         if (connman_setting_get_bool("Enable6to4"))
403                 __connman_6to4_probe(service);
404
405         return true;
406 }
407
408 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
409 {
410         struct connman_dhcp *dhcp = user_data;
411         GList *option = NULL;
412         char *address, *netmask = NULL, *gateway = NULL;
413         const char *c_address, *c_gateway;
414         unsigned char prefixlen, c_prefixlen;
415         bool ip_change;
416
417         DBG("Lease available");
418
419         if (dhcp->ipv4ll_client) {
420                 ipv4ll_stop_client(dhcp);
421                 dhcp_invalidate(dhcp, false);
422         }
423
424         c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
425         c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
426         c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
427
428         address = g_dhcp_client_get_address(dhcp_client);
429
430         __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
431         DBG("last address %s", address);
432
433         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
434         if (option)
435                 netmask = g_strdup(option->data);
436
437         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
438         if (option)
439                 gateway = g_strdup(option->data);
440
441         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
442         if (prefixlen == 255)
443                 connman_warn("netmask: %s is invalid", netmask);
444
445         DBG("c_address %s", c_address);
446
447         if (g_strcmp0(address, c_address))
448                 ip_change = true;
449         else if (g_strcmp0(gateway, c_gateway))
450                 ip_change = true;
451         else if (prefixlen != c_prefixlen)
452                 ip_change = true;
453         else
454                 ip_change = false;
455
456         __connman_ipconfig_set_method(dhcp->ipconfig,
457                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
458         if (ip_change) {
459                 __connman_ipconfig_set_local(dhcp->ipconfig, address);
460                 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
461                 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
462         }
463
464         if (!apply_lease_available_on_network(dhcp_client, dhcp))
465                 return;
466
467         if (ip_change)
468                 dhcp_valid(dhcp);
469
470         g_free(address);
471         g_free(netmask);
472         g_free(gateway);
473 }
474
475 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
476 {
477         struct connman_dhcp *dhcp = user_data;
478         char *address, *netmask;
479         unsigned char prefixlen;
480
481         DBG("IPV4LL available");
482
483         address = g_dhcp_client_get_address(ipv4ll_client);
484         netmask = g_dhcp_client_get_netmask(ipv4ll_client);
485
486         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
487
488         __connman_ipconfig_set_method(dhcp->ipconfig,
489                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
490         __connman_ipconfig_set_local(dhcp->ipconfig, address);
491         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
492         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
493
494         dhcp_valid(dhcp);
495
496         g_free(address);
497         g_free(netmask);
498 }
499
500 static int dhcp_initialize(struct connman_dhcp *dhcp)
501 {
502         GDHCPClient *dhcp_client;
503         GDHCPClientError error;
504         int index;
505
506         DBG("dhcp %p", dhcp);
507
508         index = __connman_ipconfig_get_index(dhcp->ipconfig);
509
510         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
511         if (error != G_DHCP_CLIENT_ERROR_NONE)
512                 return -EINVAL;
513
514         if (getenv("CONNMAN_DHCP_DEBUG")) {
515                 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
516                                                         index);
517                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
518                                         dhcp->dhcp_debug_prefix);
519         }
520
521         g_dhcp_client_set_id(dhcp_client);
522
523         if (dhcp->network) {
524                 struct connman_service *service;
525                 const char *hostname;
526
527                 service = connman_service_lookup_from_network(dhcp->network);
528
529                 hostname = __connman_service_get_hostname(service);
530                 if (!hostname)
531                         hostname = connman_utsname_get_hostname();
532
533                 if (hostname)
534                         g_dhcp_client_set_send(dhcp_client,
535                                                 G_DHCP_HOST_NAME, hostname);
536
537                 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
538                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
539                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
540                 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
541                 g_dhcp_client_set_request(dhcp_client, 252);
542         }
543
544         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
545         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
546
547         g_dhcp_client_register_event(dhcp_client,
548                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
549                                                 lease_available_cb, dhcp);
550
551         g_dhcp_client_register_event(dhcp_client,
552                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
553
554         g_dhcp_client_register_event(dhcp_client,
555                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
556
557         dhcp->dhcp_client = dhcp_client;
558
559         return 0;
560 }
561
562 static int dhcp_release(struct connman_dhcp *dhcp)
563 {
564         DBG("dhcp %p", dhcp);
565
566         if (dhcp->timeout > 0) {
567                 g_source_remove(dhcp->timeout);
568                 dhcp->timeout = 0;
569         }
570
571         if (dhcp->dhcp_client) {
572                 g_dhcp_client_stop(dhcp->dhcp_client);
573                 g_dhcp_client_unref(dhcp->dhcp_client);
574         }
575
576         dhcp->dhcp_client = NULL;
577
578         g_free(dhcp->dhcp_debug_prefix);
579         dhcp->dhcp_debug_prefix = NULL;
580
581         ipv4ll_stop_client(dhcp);
582
583         return 0;
584 }
585
586 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
587 {
588         struct connman_dhcp *dhcp;
589
590         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
591         if (!dhcp)
592                 return NULL;
593
594         return g_dhcp_client_get_server_address(dhcp->dhcp_client);
595 }
596
597 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
598                         struct connman_network *network, dhcp_cb callback,
599                         gpointer user_data)
600 {
601         const char *last_addr = NULL;
602         struct connman_dhcp *dhcp;
603         int err;
604
605         DBG("");
606
607         if (network) {
608                 struct connman_service *service;
609
610                 service = connman_service_lookup_from_network(network);
611                 if (!service)
612                         return -EINVAL;
613         }
614
615         last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
616
617         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
618         if (!dhcp) {
619
620                 dhcp = g_try_new0(struct connman_dhcp, 1);
621                 if (!dhcp)
622                         return -ENOMEM;
623
624                 dhcp->ipconfig = ipconfig;
625                 __connman_ipconfig_ref(ipconfig);
626
627                 if (network) {
628                         dhcp->network = network;
629                         connman_network_ref(network);
630                 }
631
632                 err = dhcp_initialize(dhcp);
633
634                 if (err < 0) {
635                         if (network)
636                                 connman_network_unref(network);
637                         g_free(dhcp);
638                         return err;
639                 }
640
641                 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
642         }
643
644         dhcp->callback = callback;
645         dhcp->user_data = user_data;
646
647         return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
648 }
649
650 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
651 {
652         struct connman_dhcp *dhcp;
653
654         DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
655
656         if (!ipconfig_table)
657                 return;
658
659         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
660         if (dhcp) {
661                 g_hash_table_remove(ipconfig_table, ipconfig);
662                 __connman_ipconfig_unref(ipconfig);
663                 if (dhcp->network)
664                         connman_network_unref(dhcp->network);
665                 dhcp_release(dhcp);
666                 dhcp_invalidate(dhcp, false);
667                 dhcp_free(dhcp);
668         }
669 }
670
671 int __connman_dhcp_init(void)
672 {
673         DBG("");
674
675         ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
676                                                                 NULL, NULL);
677
678         return 0;
679 }
680
681 void __connman_dhcp_cleanup(void)
682 {
683         DBG("");
684
685         g_hash_table_destroy(ipconfig_table);
686         ipconfig_table = NULL;
687
688         dhcp_cleanup_random();
689 }