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