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