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