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