Merge "Modified logic to process each VSIE of all vendors." into tizen
[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 #if defined TIZEN_EXT
477         int dhcp_lease_duration = g_dhcp_client_get_dhcp_lease_duration(dhcp_client);
478 #endif
479
480         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
481         if (option)
482                 netmask = g_strdup(option->data);
483
484         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
485         if (option)
486                 gateway = g_strdup(option->data);
487
488         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
489         if (prefixlen == 255)
490                 connman_warn("netmask: %s is invalid", netmask);
491
492         DBG("c_address %s", c_address);
493
494         if (g_strcmp0(address, c_address)) {
495                 ip_change = true;
496                 if (c_address) {
497                         /* Remove old ip address */
498                         __connman_ipconfig_address_remove(dhcp->ipconfig);
499                 }
500         }
501         if (g_strcmp0(gateway, c_gateway)) {
502                 ip_change = true;
503                 if (c_gateway) {
504                         /* Remove gateway ip address */
505                         __connman_ipconfig_gateway_remove(dhcp->ipconfig);
506                 }
507         } else if (prefixlen != c_prefixlen)
508                 ip_change = true;
509
510         __connman_ipconfig_set_method(dhcp->ipconfig,
511                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
512
513 #if defined TIZEN_EXT
514         __connman_ipconfig_set_dhcp_lease_duration(dhcp->ipconfig, dhcp_lease_duration);
515 #endif
516
517         if (ip_change) {
518                 __connman_ipconfig_set_local(dhcp->ipconfig, address);
519                 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
520                 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
521         }
522
523         if (!apply_lease_available_on_network(dhcp_client, dhcp))
524                 goto done;
525
526         if (ip_change)
527                 dhcp_valid(dhcp);
528
529 done:
530         g_free(address);
531         g_free(netmask);
532         g_free(gateway);
533 }
534
535 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
536 {
537         struct connman_dhcp *dhcp = user_data;
538                 char *address, *netmask;
539                 unsigned char prefixlen;
540
541         DBG("IPV4LL available");
542
543         address = g_dhcp_client_get_address(ipv4ll_client);
544         netmask = g_dhcp_client_get_netmask(ipv4ll_client);
545
546         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
547
548         __connman_ipconfig_set_method(dhcp->ipconfig,
549                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
550         __connman_ipconfig_set_local(dhcp->ipconfig, address);
551         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
552         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
553
554         dhcp_valid(dhcp);
555
556         g_free(address);
557         g_free(netmask);
558 }
559
560 static int dhcp_initialize(struct connman_dhcp *dhcp)
561 {
562         GDHCPClient *dhcp_client;
563         GDHCPClientError error;
564         int index;
565
566         DBG("dhcp %p", dhcp);
567
568         index = __connman_ipconfig_get_index(dhcp->ipconfig);
569
570         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
571         if (error != G_DHCP_CLIENT_ERROR_NONE)
572 #if defined TIZEN_EXT
573         {
574                 DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
575 #endif
576                 return -EINVAL;
577 #if defined TIZEN_EXT
578         }
579 #endif
580
581 #if !defined TIZEN_EXT
582         if (getenv("CONNMAN_DHCP_DEBUG")) {
583 #endif
584                 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
585                                                         index);
586                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
587                                         dhcp->dhcp_debug_prefix);
588 #if !defined TIZEN_EXT
589         }
590 #endif
591
592         g_dhcp_client_set_id(dhcp_client);
593
594         if (dhcp->network) {
595                 struct connman_service *service;
596                 const char *hostname;
597
598                 service = connman_service_lookup_from_network(dhcp->network);
599
600                 hostname = __connman_service_get_hostname(service);
601                 if (!hostname)
602                         hostname = connman_utsname_get_hostname();
603
604                 if (hostname)
605                         g_dhcp_client_set_send(dhcp_client,
606                                                 G_DHCP_HOST_NAME, hostname);
607
608                 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
609                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
610                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
611                 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
612                 g_dhcp_client_set_request(dhcp_client, 252);
613         }
614
615         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
616         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
617
618         g_dhcp_client_register_event(dhcp_client,
619                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
620                                                 lease_available_cb, dhcp);
621
622         g_dhcp_client_register_event(dhcp_client,
623                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
624
625         g_dhcp_client_register_event(dhcp_client,
626                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
627
628         dhcp->dhcp_client = dhcp_client;
629
630         return 0;
631 }
632
633 static int dhcp_release(struct connman_dhcp *dhcp)
634 {
635         DBG("dhcp %p", dhcp);
636
637         if (dhcp->timeout > 0) {
638                 g_source_remove(dhcp->timeout);
639                 dhcp->timeout = 0;
640         }
641
642         if (dhcp->dhcp_client) {
643                 g_dhcp_client_stop(dhcp->dhcp_client);
644                 g_dhcp_client_unref(dhcp->dhcp_client);
645         }
646
647         dhcp->dhcp_client = NULL;
648
649         g_free(dhcp->dhcp_debug_prefix);
650         dhcp->dhcp_debug_prefix = NULL;
651
652         ipv4ll_stop_client(dhcp);
653
654         return 0;
655 }
656
657 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
658 {
659         struct connman_dhcp *dhcp;
660
661         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
662         if (!dhcp)
663                 return NULL;
664
665         return g_dhcp_client_get_server_address(dhcp->dhcp_client);
666 }
667
668 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
669                         struct connman_network *network, dhcp_cb callback,
670                         gpointer user_data)
671 {
672 #if !defined TIZEN_EXT
673         const char *last_addr = NULL;
674 #endif
675         struct connman_dhcp *dhcp;
676         int err;
677
678         DBG("");
679
680         if (network) {
681                 struct connman_service *service;
682
683                 service = connman_service_lookup_from_network(network);
684                 if (!service)
685                         return -EINVAL;
686         }
687
688 #if !defined TIZEN_EXT
689         last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
690 #endif
691
692         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
693         if (!dhcp) {
694
695                 dhcp = g_try_new0(struct connman_dhcp, 1);
696                 if (!dhcp)
697                         return -ENOMEM;
698
699                 dhcp->ipconfig = ipconfig;
700                 __connman_ipconfig_ref(ipconfig);
701
702                 if (network) {
703                         dhcp->network = network;
704                         connman_network_ref(network);
705                 }
706
707                 err = dhcp_initialize(dhcp);
708
709                 if (err < 0) {
710                         if (network)
711                                 connman_network_unref(network);
712                         g_free(dhcp);
713                         return err;
714                 }
715
716                 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
717         }
718
719         dhcp->callback = callback;
720         dhcp->user_data = user_data;
721
722 #if defined TIZEN_EXT
723         DBG("Start DHCP with DHCPDISCOVER request");
724
725         return g_dhcp_client_start(dhcp->dhcp_client, NULL);
726 #else
727         return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
728 #endif
729 }
730
731 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
732 {
733         struct connman_dhcp *dhcp;
734
735         DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
736
737         if (!ipconfig_table)
738                 return;
739
740         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
741         if (dhcp) {
742                 g_hash_table_remove(ipconfig_table, ipconfig);
743                 __connman_ipconfig_unref(ipconfig);
744                 if (dhcp->network)
745                         connman_network_unref(dhcp->network);
746                 dhcp_release(dhcp);
747                 dhcp_invalidate(dhcp, false);
748                 dhcp_free(dhcp);
749         }
750 }
751
752 int __connman_dhcp_init(void)
753 {
754         DBG("");
755
756         ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
757                                                                 NULL, NULL);
758
759         return 0;
760 }
761
762 void __connman_dhcp_cleanup(void)
763 {
764         DBG("");
765
766         g_hash_table_destroy(ipconfig_table);
767         ipconfig_table = NULL;
768
769         dhcp_cleanup_random();
770 }