Imported Upstream version 1.29
[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         __connman_6to4_probe(service);
403
404         return true;
405 }
406
407 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
408 {
409         struct connman_dhcp *dhcp = user_data;
410         GList *option = NULL;
411         char *address, *netmask = NULL, *gateway = NULL;
412         const char *c_address, *c_gateway;
413         unsigned char prefixlen, c_prefixlen;
414         bool ip_change;
415
416         DBG("Lease available");
417
418         if (dhcp->ipv4ll_client) {
419                 ipv4ll_stop_client(dhcp);
420                 dhcp_invalidate(dhcp, false);
421         }
422
423         c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
424         c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
425         c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
426
427         address = g_dhcp_client_get_address(dhcp_client);
428
429         __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
430         DBG("last address %s", address);
431
432         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
433         if (option)
434                 netmask = g_strdup(option->data);
435
436         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
437         if (option)
438                 gateway = g_strdup(option->data);
439
440         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
441         if (prefixlen == 255)
442                 connman_warn("netmask: %s is invalid", netmask);
443
444         DBG("c_address %s", c_address);
445
446         if (g_strcmp0(address, c_address))
447                 ip_change = true;
448         else if (g_strcmp0(gateway, c_gateway))
449                 ip_change = true;
450         else if (prefixlen != c_prefixlen)
451                 ip_change = true;
452         else
453                 ip_change = false;
454
455         __connman_ipconfig_set_method(dhcp->ipconfig,
456                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
457         if (ip_change) {
458                 __connman_ipconfig_set_local(dhcp->ipconfig, address);
459                 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
460                 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
461         }
462
463         if (!apply_lease_available_on_network(dhcp_client, dhcp))
464                 return;
465
466         if (ip_change)
467                 dhcp_valid(dhcp);
468
469         g_free(address);
470         g_free(netmask);
471         g_free(gateway);
472 }
473
474 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
475 {
476         struct connman_dhcp *dhcp = user_data;
477         char *address, *netmask;
478         unsigned char prefixlen;
479
480         DBG("IPV4LL available");
481
482         address = g_dhcp_client_get_address(ipv4ll_client);
483         netmask = g_dhcp_client_get_netmask(ipv4ll_client);
484
485         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
486
487         __connman_ipconfig_set_method(dhcp->ipconfig,
488                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
489         __connman_ipconfig_set_local(dhcp->ipconfig, address);
490         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
491         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
492
493         dhcp_valid(dhcp);
494
495         g_free(address);
496         g_free(netmask);
497 }
498
499 static int dhcp_initialize(struct connman_dhcp *dhcp)
500 {
501         GDHCPClient *dhcp_client;
502         GDHCPClientError error;
503         int index;
504
505         DBG("dhcp %p", dhcp);
506
507         index = __connman_ipconfig_get_index(dhcp->ipconfig);
508
509         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
510         if (error != G_DHCP_CLIENT_ERROR_NONE)
511                 return -EINVAL;
512
513         if (getenv("CONNMAN_DHCP_DEBUG")) {
514                 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
515                                                         index);
516                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
517                                         dhcp->dhcp_debug_prefix);
518         }
519
520         g_dhcp_client_set_id(dhcp_client);
521
522         if (dhcp->network) {
523                 struct connman_service *service;
524                 const char *hostname;
525
526                 service = connman_service_lookup_from_network(dhcp->network);
527
528                 hostname = __connman_service_get_hostname(service);
529                 if (!hostname)
530                         hostname = connman_utsname_get_hostname();
531
532                 if (hostname)
533                         g_dhcp_client_set_send(dhcp_client,
534                                                 G_DHCP_HOST_NAME, hostname);
535
536                 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
537                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
538                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
539                 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
540                 g_dhcp_client_set_request(dhcp_client, 252);
541         }
542
543         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
544         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
545
546         g_dhcp_client_register_event(dhcp_client,
547                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
548                                                 lease_available_cb, dhcp);
549
550         g_dhcp_client_register_event(dhcp_client,
551                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
552
553         g_dhcp_client_register_event(dhcp_client,
554                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
555
556         dhcp->dhcp_client = dhcp_client;
557
558         return 0;
559 }
560
561 static int dhcp_release(struct connman_dhcp *dhcp)
562 {
563         DBG("dhcp %p", dhcp);
564
565         if (dhcp->timeout > 0) {
566                 g_source_remove(dhcp->timeout);
567                 dhcp->timeout = 0;
568         }
569
570         if (dhcp->dhcp_client) {
571                 g_dhcp_client_stop(dhcp->dhcp_client);
572                 g_dhcp_client_unref(dhcp->dhcp_client);
573         }
574
575         dhcp->dhcp_client = NULL;
576
577         g_free(dhcp->dhcp_debug_prefix);
578         dhcp->dhcp_debug_prefix = NULL;
579
580         ipv4ll_stop_client(dhcp);
581
582         return 0;
583 }
584
585 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
586 {
587         struct connman_dhcp *dhcp;
588
589         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
590         if (!dhcp)
591                 return NULL;
592
593         return g_dhcp_client_get_server_address(dhcp->dhcp_client);
594 }
595
596 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
597                         struct connman_network *network, dhcp_cb callback,
598                         gpointer user_data)
599 {
600         const char *last_addr = NULL;
601         struct connman_dhcp *dhcp;
602         int err;
603
604         DBG("");
605
606         if (network) {
607                 struct connman_service *service;
608
609                 service = connman_service_lookup_from_network(network);
610                 if (!service)
611                         return -EINVAL;
612         }
613
614         last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
615
616         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
617         if (!dhcp) {
618
619                 dhcp = g_try_new0(struct connman_dhcp, 1);
620                 if (!dhcp)
621                         return -ENOMEM;
622
623                 dhcp->ipconfig = ipconfig;
624                 __connman_ipconfig_ref(ipconfig);
625
626                 if (network) {
627                         dhcp->network = network;
628                         connman_network_ref(network);
629                 }
630
631                 err = dhcp_initialize(dhcp);
632
633                 if (err < 0) {
634                         if (network)
635                                 connman_network_unref(network);
636                         g_free(dhcp);
637                         return err;
638                 }
639
640                 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
641         }
642
643         dhcp->callback = callback;
644         dhcp->user_data = user_data;
645
646         return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
647 }
648
649 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
650 {
651         struct connman_dhcp *dhcp;
652
653         DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
654
655         if (!ipconfig_table)
656                 return;
657
658         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
659         if (dhcp) {
660                 g_hash_table_remove(ipconfig_table, ipconfig);
661                 __connman_ipconfig_unref(ipconfig);
662                 if (dhcp->network)
663                         connman_network_unref(dhcp->network);
664                 dhcp_release(dhcp);
665                 dhcp_invalidate(dhcp, false);
666                 dhcp_free(dhcp);
667         }
668 }
669
670 int __connman_dhcp_init(void)
671 {
672         DBG("");
673
674         ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
675                                                                 NULL, NULL);
676
677         return 0;
678 }
679
680 void __connman_dhcp_cleanup(void)
681 {
682         DBG("");
683
684         g_hash_table_destroy(ipconfig_table);
685         ipconfig_table = NULL;
686
687         dhcp_cleanup_random();
688 }