Updated connman to version 1.35
[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 #include <net/ethernet.h>
30
31 #ifndef IPV6_MIN_MTU
32 #define IPV6_MIN_MTU 1280
33 #endif
34
35 #include <connman/ipconfig.h>
36 #include <include/setting.h>
37
38 #include <gdhcp/gdhcp.h>
39
40 #include <glib.h>
41
42 #include "connman.h"
43
44 #define RATE_LIMIT_INTERVAL     60      /* delay between successive attempts */
45
46 struct connman_dhcp {
47         struct connman_ipconfig *ipconfig;
48         struct connman_network *network;
49         dhcp_cb callback;
50         gpointer user_data;
51
52         char **nameservers;
53         char **timeservers;
54         char *pac;
55
56         unsigned int timeout;
57
58         GDHCPClient *ipv4ll_client;
59         GDHCPClient *dhcp_client;
60         char *ipv4ll_debug_prefix;
61         char *dhcp_debug_prefix;
62
63         bool ipv4ll_running;
64 };
65
66 static GHashTable *ipconfig_table;
67
68 static void dhcp_free(struct connman_dhcp *dhcp)
69 {
70 #if defined TIZEN_EXT
71         DBG("dhcp_free [%p]", dhcp);
72 #endif
73         g_strfreev(dhcp->nameservers);
74         g_strfreev(dhcp->timeservers);
75         g_free(dhcp->pac);
76
77         dhcp->nameservers = NULL;
78         dhcp->timeservers = NULL;
79         dhcp->pac = NULL;
80
81         g_free(dhcp);
82 #if defined TIZEN_EXT
83         dhcp = NULL;
84 #endif
85 }
86
87 static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
88 {
89 #if defined TIZEN_EXT
90         DBG("dhcp [%p] ipv4ll_client [%p]", dhcp, dhcp->ipv4ll_client);
91 #endif
92         if (!dhcp->ipv4ll_client)
93                 return;
94
95         g_dhcp_client_stop(dhcp->ipv4ll_client);
96         g_dhcp_client_unref(dhcp->ipv4ll_client);
97         dhcp->ipv4ll_client = NULL;
98         dhcp->ipv4ll_running = false;
99
100         g_free(dhcp->ipv4ll_debug_prefix);
101         dhcp->ipv4ll_debug_prefix = NULL;
102 }
103
104 static bool apply_dhcp_invalidate_on_network(struct connman_dhcp *dhcp)
105 {
106         struct connman_service *service;
107         int i;
108
109         if (!dhcp->network)
110                 return true;
111
112         service = connman_service_lookup_from_network(dhcp->network);
113         if (!service) {
114                 connman_error("Can not lookup service");
115                 return false;
116         }
117
118         __connman_service_set_domainname(service, NULL);
119         __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig, NULL);
120
121         if (dhcp->timeservers) {
122                 for (i = 0; dhcp->timeservers[i]; i++) {
123                         __connman_service_timeserver_remove(service,
124                                                         dhcp->timeservers[i]);
125                 }
126                 g_strfreev(dhcp->timeservers);
127                 dhcp->timeservers = NULL;
128         }
129         if (dhcp->nameservers) {
130                 for (i = 0; dhcp->nameservers[i]; i++) {
131 #if defined TIZEN_EXT
132                         __connman_service_nameserver_remove(service,
133                                         dhcp->nameservers[i], false,
134                                         CONNMAN_IPCONFIG_TYPE_IPV4);
135 #else
136                         __connman_service_nameserver_remove(service,
137                                         dhcp->nameservers[i], false);
138 #endif
139                 }
140                 g_strfreev(dhcp->nameservers);
141                 dhcp->nameservers = NULL;
142         }
143
144         return true;
145 }
146
147 /**
148  * dhcp_invalidate: Invalidate an existing DHCP lease
149  * @dhcp: pointer to the DHCP lease to invalidate.
150  * @callback: flag indicating whether or not to invoke the client callback
151  *            if present.
152  *
153  * Invalidates an existing DHCP lease, optionally invoking the client
154  * callback. The caller may wish to avoid the client callback invocation
155  * when the invocation of that callback might otherwise unnecessarily upset
156  * service state due to the IP configuration change implied by this
157  * invalidation.
158  */
159 static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
160 {
161         DBG("dhcp %p callback %u", dhcp, callback);
162
163         if (!dhcp)
164                 return;
165
166         __connman_6to4_remove(dhcp->ipconfig);
167
168         if (!apply_dhcp_invalidate_on_network(dhcp))
169                 return;
170
171         __connman_ipconfig_set_dhcp_address(dhcp->ipconfig,
172                                 __connman_ipconfig_get_local(dhcp->ipconfig));
173         DBG("last address %s",
174                         __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
175
176         __connman_ipconfig_address_remove(dhcp->ipconfig);
177
178         __connman_ipconfig_set_local(dhcp->ipconfig, NULL);
179         __connman_ipconfig_set_broadcast(dhcp->ipconfig, NULL);
180         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
181         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, 0);
182
183         if (dhcp->callback && callback)
184                 dhcp->callback(dhcp->ipconfig, dhcp->network,
185                                                 false, dhcp->user_data);
186 }
187
188 static void dhcp_valid(struct connman_dhcp *dhcp)
189 {
190         if (dhcp->callback)
191                 dhcp->callback(dhcp->ipconfig, dhcp->network,
192                                                 true, dhcp->user_data);
193 }
194
195 static void dhcp_debug(const char *str, void *data)
196 {
197         connman_info("%s: %s", (const char *) data, str);
198 }
199
200 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
201 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);
202
203 static int ipv4ll_start_client(struct connman_dhcp *dhcp)
204 {
205         GDHCPClient *ipv4ll_client;
206         GDHCPClientError error;
207         const char *hostname;
208         int index;
209         int err;
210
211 #if defined TIZEN_EXT
212         DBG("dhcp %p", dhcp);
213 #endif
214
215         if (dhcp->ipv4ll_client)
216                 return -EALREADY;
217
218         index = __connman_ipconfig_get_index(dhcp->ipconfig);
219
220         ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
221         if (error != G_DHCP_CLIENT_ERROR_NONE)
222                 return -EINVAL;
223
224 #if !defined TIZEN_EXT
225         if (getenv("CONNMAN_DHCP_DEBUG")) {
226 #endif
227                 dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
228                                                         index);
229                 g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
230                                         dhcp->ipv4ll_debug_prefix);
231 #if !defined TIZEN_EXT
232         }
233 #endif
234
235         g_dhcp_client_set_id(ipv4ll_client);
236
237         if (dhcp->network) {
238                 hostname = connman_utsname_get_hostname();
239                 if (hostname)
240                         g_dhcp_client_set_send(ipv4ll_client,
241                                                 G_DHCP_HOST_NAME, hostname);
242         }
243
244         g_dhcp_client_register_event(ipv4ll_client,
245                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
246
247         g_dhcp_client_register_event(ipv4ll_client,
248                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
249                                                 ipv4ll_available_cb, dhcp);
250
251         dhcp->ipv4ll_client = ipv4ll_client;
252
253         err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
254         if (err < 0) {
255                 ipv4ll_stop_client(dhcp);
256                 return err;
257         }
258
259         dhcp->ipv4ll_running = true;
260         return 0;
261 }
262
263 static gboolean dhcp_retry_cb(gpointer user_data)
264 {
265         struct connman_dhcp *dhcp = user_data;
266
267         dhcp->timeout = 0;
268 #if defined TIZEN_EXT
269         DBG("dhcp %p", dhcp);
270         DBG("dhcp->timeout %d", dhcp->timeout);
271 #endif
272         g_dhcp_client_start(dhcp->dhcp_client,
273                         __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
274
275         return FALSE;
276 }
277
278 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
279 {
280         struct connman_dhcp *dhcp = user_data;
281         int err;
282
283         DBG("No lease available ipv4ll %d client %p", dhcp->ipv4ll_running,
284                 dhcp->ipv4ll_client);
285
286         if (dhcp->timeout > 0)
287                 g_source_remove(dhcp->timeout);
288
289         dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
290                                                 dhcp_retry_cb,
291                                                 dhcp);
292         if (dhcp->ipv4ll_running)
293                 return;
294
295         err = ipv4ll_start_client(dhcp);
296         if (err < 0)
297                 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
298
299         /* Only notify upper layer if we have a problem */
300         dhcp_invalidate(dhcp, !dhcp->ipv4ll_running);
301 }
302
303 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
304 {
305         struct connman_dhcp *dhcp = user_data;
306
307         DBG("Lease lost");
308
309         /* Upper layer will decide what to do, e.g. nothing or retry. */
310         dhcp_invalidate(dhcp, true);
311 }
312
313 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
314 {
315         struct connman_dhcp *dhcp = user_data;
316
317         DBG("Lease lost");
318
319         ipv4ll_stop_client(dhcp);
320
321         /*
322          * Since we lost our IPv4LL configuration we might as notify
323          * the upper layers.
324          */
325         dhcp_invalidate(dhcp, true);
326 }
327
328 static bool compare_string_arrays(char **array_a, char **array_b)
329 {
330         int i;
331
332         if (!array_a || !array_b)
333                 return false;
334
335         if (g_strv_length(array_a) != g_strv_length(array_b))
336                 return false;
337
338         for (i = 0; array_a[i] &&
339                              array_b[i]; i++) {
340                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
341                         return false;
342         }
343
344         return true;
345 }
346
347 static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
348                                                 struct connman_dhcp *dhcp)
349 {
350         char **nameservers, **timeservers, *pac = NULL;
351         struct connman_service *service;
352         GList *list, *option = NULL;
353         int ns_entries;
354         int i;
355
356         if (!dhcp->network)
357                 return true;
358
359         service = connman_service_lookup_from_network(dhcp->network);
360         if (!service) {
361                 connman_error("Can not lookup service");
362                 return false;
363         }
364
365         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_MTU);
366         if (option && option->data) {
367                 int mtu, index, err;
368
369                 mtu = atoi(option->data);
370
371                 if (mtu >= IPV6_MIN_MTU && mtu <= ETH_DATA_LEN) {
372                         index = __connman_ipconfig_get_index(dhcp->ipconfig);
373                         err = connman_inet_set_mtu(index, mtu);
374
375                         DBG("MTU %d index %d err %d", mtu, index, err);
376                 }
377         }
378
379         option = g_dhcp_client_get_option(dhcp_client, 252);
380         if (option)
381                 pac = g_strdup(option->data);
382
383         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
384         ns_entries = g_list_length(option);
385         nameservers = g_try_new0(char *, ns_entries + 1);
386         if (nameservers) {
387                 for (i = 0, list = option;list; list = list->next, i++)
388                         nameservers[i] = g_strdup(list->data);
389                 nameservers[ns_entries] = NULL;
390         }
391
392         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
393         if (option)
394                 __connman_service_set_domainname(service, option->data);
395
396         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
397         if (option)
398                 __connman_service_set_hostname(service, option->data);
399
400         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
401         ns_entries = g_list_length(option);
402         timeservers = g_try_new0(char *, ns_entries + 1);
403         if (timeservers) {
404                 for (i = 0, list = option; list; list = list->next, i++)
405                         timeservers[i] = g_strdup(list->data);
406                 timeservers[ns_entries] = NULL;
407         }
408
409         if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
410                 if (dhcp->nameservers) {
411 #if defined TIZEN_EXT
412                         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
413                                 __connman_service_nameserver_remove(service,
414                                                 dhcp->nameservers[i], false,
415                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
416                         }
417 #else
418                         for (i = 0; dhcp->nameservers[i]; i++) {
419                                 __connman_service_nameserver_remove(service,
420                                                 dhcp->nameservers[i], false);
421                         }
422 #endif
423                         g_strfreev(dhcp->nameservers);
424                 }
425
426                 dhcp->nameservers = nameservers;
427
428                 for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
429 #if defined TIZEN_EXT
430                         __connman_service_nameserver_append(service,
431                                                 dhcp->nameservers[i], false,
432                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
433 #else
434                         __connman_service_nameserver_append(service,
435                                                 dhcp->nameservers[i], false);
436 #endif
437                 }
438         } else {
439                 g_strfreev(nameservers);
440         }
441
442         if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
443                 if (dhcp->timeservers) {
444                         for (i = 0; dhcp->timeservers[i]; i++) {
445                                 __connman_service_timeserver_remove(service,
446                                                         dhcp->timeservers[i]);
447                         }
448                         g_strfreev(dhcp->timeservers);
449                 }
450
451                 dhcp->timeservers = timeservers;
452
453                 for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
454                         __connman_service_timeserver_append(service,
455                                                         dhcp->timeservers[i]);
456                 }
457         } else {
458                 g_strfreev(timeservers);
459         }
460
461         if (g_strcmp0(pac, dhcp->pac) != 0) {
462                 g_free(dhcp->pac);
463                 dhcp->pac = pac;
464
465                 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
466                                                                 dhcp->pac);
467         }
468
469         if (connman_setting_get_bool("Enable6to4"))
470                 __connman_6to4_probe(service);
471
472         return true;
473 }
474
475 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
476 {
477         struct connman_dhcp *dhcp = user_data;
478         GList *option = NULL;
479         enum connman_ipconfig_method old_method;
480         char *address, *netmask = NULL, *gateway = NULL;
481         const char *c_address, *c_gateway;
482         unsigned char prefixlen, c_prefixlen;
483         bool ip_change = false;
484
485         DBG("Lease available");
486
487         if (dhcp->ipv4ll_client) {
488                 ipv4ll_stop_client(dhcp);
489                 dhcp_invalidate(dhcp, false);
490         }
491
492         c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
493         c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
494         c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
495
496         address = g_dhcp_client_get_address(dhcp_client);
497
498         __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
499         DBG("last address %s", address);
500
501 #if defined TIZEN_EXT
502         int dhcp_lease_duration = g_dhcp_client_get_dhcp_lease_duration(dhcp_client);
503 #endif
504
505         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
506         if (option)
507                 netmask = g_strdup(option->data);
508
509         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
510         if (option)
511                 gateway = g_strdup(option->data);
512
513         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
514         if (prefixlen == 255)
515                 connman_warn("netmask: %s is invalid", netmask);
516
517         DBG("c_address %s", c_address);
518
519         if (g_strcmp0(address, c_address)) {
520                 ip_change = true;
521                 if (c_address) {
522                         /* Remove old ip address */
523                         __connman_ipconfig_address_remove(dhcp->ipconfig);
524                 }
525         }
526         if (g_strcmp0(gateway, c_gateway)) {
527                 ip_change = true;
528                 if (c_gateway) {
529                         /* Remove gateway ip address */
530                         __connman_ipconfig_gateway_remove(dhcp->ipconfig);
531                 }
532         } else if (prefixlen != c_prefixlen)
533                 ip_change = true;
534
535         old_method = __connman_ipconfig_get_method(dhcp->ipconfig);
536         __connman_ipconfig_set_method(dhcp->ipconfig,
537                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
538
539 #if defined TIZEN_EXT
540         __connman_ipconfig_set_dhcp_lease_duration(dhcp->ipconfig, dhcp_lease_duration);
541 #endif
542
543         /*
544          * Notify IPv4.Configuration's method moved back to DHCP.
545          *
546          * This is the case ConnMan initially set an address by using
547          * IPv4LL because DHCP failed but now we got an address from DHCP.
548          */
549         if (old_method == CONNMAN_IPCONFIG_METHOD_AUTO) {
550                 struct connman_service *service =
551                         connman_service_lookup_from_network(dhcp->network);
552
553                 if (service)
554                         __connman_service_notify_ipv4_configuration(service);
555         }
556
557         if (ip_change) {
558                 __connman_ipconfig_set_local(dhcp->ipconfig, address);
559                 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
560                 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
561         }
562
563         if (!apply_lease_available_on_network(dhcp_client, dhcp))
564                 goto done;
565
566         if (ip_change)
567                 dhcp_valid(dhcp);
568
569 done:
570         g_free(address);
571         g_free(netmask);
572         g_free(gateway);
573 }
574
575 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
576 {
577         struct connman_dhcp *dhcp = user_data;
578         enum connman_ipconfig_method old_method;
579         char *address, *netmask;
580         unsigned char prefixlen;
581
582         DBG("IPV4LL available");
583
584         address = g_dhcp_client_get_address(ipv4ll_client);
585         netmask = g_dhcp_client_get_netmask(ipv4ll_client);
586
587         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
588
589         old_method = __connman_ipconfig_get_method(dhcp->ipconfig);
590         __connman_ipconfig_set_method(dhcp->ipconfig,
591                                                 CONNMAN_IPCONFIG_METHOD_AUTO);
592
593         /*
594          * Notify IPv4.Configuration's method is AUTO now.
595          *
596          * This is the case DHCP failed thus ConnMan used IPv4LL to get an
597          * address. Set IPv4.Configuration method to AUTO allows user to
598          * ask for a DHCP address by setting the method again to DHCP.
599          */
600         if (old_method == CONNMAN_IPCONFIG_METHOD_DHCP) {
601                 struct connman_service *service =
602                         connman_service_lookup_from_network(dhcp->network);
603
604                 if (service)
605                         __connman_service_notify_ipv4_configuration(service);
606         }
607
608         __connman_ipconfig_set_local(dhcp->ipconfig, address);
609         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
610         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
611
612         dhcp_valid(dhcp);
613
614         g_free(address);
615         g_free(netmask);
616 }
617
618 static int dhcp_initialize(struct connman_dhcp *dhcp)
619 {
620         GDHCPClient *dhcp_client;
621         GDHCPClientError error;
622         int index;
623         const char *vendor_class_id;
624
625         DBG("dhcp %p", dhcp);
626
627         index = __connman_ipconfig_get_index(dhcp->ipconfig);
628
629         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
630         if (error != G_DHCP_CLIENT_ERROR_NONE)
631 #if defined TIZEN_EXT
632         {
633                 DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
634 #endif
635                 return -EINVAL;
636 #if defined TIZEN_EXT
637         }
638 #endif
639
640 #if !defined TIZEN_EXT
641         if (getenv("CONNMAN_DHCP_DEBUG")) {
642 #endif
643                 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
644                                                         index);
645                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
646                                         dhcp->dhcp_debug_prefix);
647 #if !defined TIZEN_EXT
648         }
649 #endif
650
651         g_dhcp_client_set_id(dhcp_client);
652
653         if (dhcp->network) {
654                 struct connman_service *service;
655                 const char *hostname;
656
657                 service = connman_service_lookup_from_network(dhcp->network);
658
659                 hostname = __connman_service_get_hostname(service);
660                 if (!hostname)
661                         hostname = connman_utsname_get_hostname();
662
663                 if (hostname)
664                         g_dhcp_client_set_send(dhcp_client,
665                                                 G_DHCP_HOST_NAME, hostname);
666
667                 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
668                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
669                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
670                 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
671                 g_dhcp_client_set_request(dhcp_client, 252);
672                 g_dhcp_client_set_request(dhcp_client, G_DHCP_MTU);
673         }
674
675         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
676         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
677
678         vendor_class_id = connman_option_get_string("VendorClassID");
679         if (vendor_class_id)
680                 g_dhcp_client_set_send(dhcp_client, G_DHCP_VENDOR_CLASS_ID,
681                                         vendor_class_id);
682
683         g_dhcp_client_register_event(dhcp_client,
684                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
685                                                 lease_available_cb, dhcp);
686
687         g_dhcp_client_register_event(dhcp_client,
688                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
689
690         g_dhcp_client_register_event(dhcp_client,
691                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
692
693         dhcp->dhcp_client = dhcp_client;
694
695         return 0;
696 }
697
698 static int dhcp_release(struct connman_dhcp *dhcp)
699 {
700         DBG("dhcp %p", dhcp);
701
702         if (dhcp->timeout > 0) {
703                 g_source_remove(dhcp->timeout);
704                 dhcp->timeout = 0;
705         }
706
707         if (dhcp->dhcp_client) {
708                 g_dhcp_client_stop(dhcp->dhcp_client);
709                 g_dhcp_client_unref(dhcp->dhcp_client);
710         }
711
712         dhcp->dhcp_client = NULL;
713
714         g_free(dhcp->dhcp_debug_prefix);
715         dhcp->dhcp_debug_prefix = NULL;
716
717         ipv4ll_stop_client(dhcp);
718
719         return 0;
720 }
721
722 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
723 {
724         struct connman_dhcp *dhcp;
725
726         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
727         if (!dhcp)
728                 return NULL;
729
730         return g_dhcp_client_get_server_address(dhcp->dhcp_client);
731 }
732
733 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
734                         struct connman_network *network, dhcp_cb callback,
735                         gpointer user_data)
736 {
737 #if !defined TIZEN_EXT
738         const char *last_addr = NULL;
739 #endif
740         struct connman_dhcp *dhcp;
741         int err;
742
743         DBG("");
744
745         if (network) {
746                 struct connman_service *service;
747
748                 service = connman_service_lookup_from_network(network);
749                 if (!service)
750                         return -EINVAL;
751         }
752
753 #if !defined TIZEN_EXT
754         last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
755 #endif
756
757         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
758         if (!dhcp) {
759
760                 dhcp = g_try_new0(struct connman_dhcp, 1);
761                 if (!dhcp)
762                         return -ENOMEM;
763
764                 dhcp->ipconfig = ipconfig;
765                 __connman_ipconfig_ref(ipconfig);
766
767                 if (network) {
768                         dhcp->network = network;
769                         connman_network_ref(network);
770                 }
771
772                 err = dhcp_initialize(dhcp);
773
774                 if (err < 0) {
775                         if (network)
776                                 connman_network_unref(network);
777                         g_free(dhcp);
778                         return err;
779                 }
780
781                 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
782         }
783
784         dhcp->callback = callback;
785         dhcp->user_data = user_data;
786
787 #if defined TIZEN_EXT
788         DBG("Start DHCP with DHCPDISCOVER request");
789
790         return g_dhcp_client_start(dhcp->dhcp_client, NULL);
791 #else
792         return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
793 #endif
794 }
795
796 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
797 {
798         struct connman_dhcp *dhcp;
799
800         DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
801
802         if (!ipconfig_table)
803                 return;
804
805         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
806         if (dhcp) {
807                 g_hash_table_remove(ipconfig_table, ipconfig);
808                 __connman_ipconfig_unref(ipconfig);
809                 if (dhcp->network)
810                         connman_network_unref(dhcp->network);
811                 dhcp_release(dhcp);
812                 dhcp_invalidate(dhcp, false);
813                 dhcp_free(dhcp);
814         }
815 }
816
817 int __connman_dhcp_init(void)
818 {
819         DBG("");
820
821         ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
822                                                                 NULL, NULL);
823
824         return 0;
825 }
826
827 void __connman_dhcp_cleanup(void)
828 {
829         DBG("");
830
831         g_hash_table_destroy(ipconfig_table);
832         ipconfig_table = NULL;
833
834         dhcp_cleanup_random();
835 }