Merge tag 'upstream/1.40' 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 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 (connman_setting_get_bool("EnableAutoIp") == false) {
289                 DBG("link-local address autoconfiguration is disabled.");
290                 if (dhcp->network)
291                         __connman_network_disconnect(dhcp->network);
292                 return;
293         }
294 #endif
295         if (dhcp->timeout > 0)
296                 g_source_remove(dhcp->timeout);
297
298         dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
299                                                 dhcp_retry_cb,
300                                                 dhcp);
301         if (dhcp->ipv4ll_running)
302                 return;
303
304         err = ipv4ll_start_client(dhcp);
305         if (err < 0)
306                 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
307
308         /* Only notify upper layer if we have a problem */
309         dhcp_invalidate(dhcp, !dhcp->ipv4ll_running);
310 }
311
312 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
313 {
314         struct connman_dhcp *dhcp = user_data;
315
316         DBG("Lease lost");
317
318         /* Upper layer will decide what to do, e.g. nothing or retry. */
319         dhcp_invalidate(dhcp, true);
320 }
321
322 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
323 {
324         struct connman_dhcp *dhcp = user_data;
325
326         DBG("Lease lost");
327
328         ipv4ll_stop_client(dhcp);
329
330         /*
331          * Since we lost our IPv4LL configuration we might as notify
332          * the upper layers.
333          */
334         dhcp_invalidate(dhcp, true);
335 }
336
337 static bool compare_string_arrays(char **array_a, char **array_b)
338 {
339         int i;
340
341         if (!array_a || !array_b)
342                 return false;
343
344         if (g_strv_length(array_a) != g_strv_length(array_b))
345                 return false;
346
347         for (i = 0; array_a[i] &&
348                              array_b[i]; i++) {
349                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
350                         return false;
351         }
352
353         return true;
354 }
355
356 static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
357                                                 struct connman_dhcp *dhcp)
358 {
359         char **nameservers, **timeservers, *pac = NULL;
360         struct connman_service *service;
361         GList *list, *option = NULL;
362         int ns_entries;
363         int i;
364
365         if (!dhcp->network)
366                 return true;
367
368         service = connman_service_lookup_from_network(dhcp->network);
369         if (!service) {
370                 connman_error("Can not lookup service");
371                 return false;
372         }
373
374         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_MTU);
375         if (option && option->data) {
376                 int mtu, index, err;
377
378                 mtu = atoi(option->data);
379
380                 if (mtu >= IPV6_MIN_MTU && mtu <= ETH_DATA_LEN) {
381                         index = __connman_ipconfig_get_index(dhcp->ipconfig);
382                         err = connman_inet_set_mtu(index, mtu);
383
384                         DBG("MTU %d index %d err %d", mtu, index, err);
385                 }
386         }
387
388         option = g_dhcp_client_get_option(dhcp_client, 252);
389         if (option)
390                 pac = g_strdup(option->data);
391
392         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
393         ns_entries = g_list_length(option);
394         nameservers = g_try_new0(char *, ns_entries + 1);
395         if (nameservers) {
396                 for (i = 0, list = option;list; list = list->next, i++)
397                         nameservers[i] = g_strdup(list->data);
398                 nameservers[ns_entries] = NULL;
399         }
400
401         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
402         if (option)
403                 __connman_service_set_domainname(service, option->data);
404
405         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
406         if (option)
407                 __connman_service_set_hostname(service, option->data);
408
409         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
410         ns_entries = g_list_length(option);
411         timeservers = g_try_new0(char *, ns_entries + 1);
412         if (timeservers) {
413                 for (i = 0, list = option; list; list = list->next, i++)
414                         timeservers[i] = g_strdup(list->data);
415                 timeservers[ns_entries] = NULL;
416         }
417
418         if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
419                 if (dhcp->nameservers) {
420 #if defined TIZEN_EXT
421                         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
422                                 __connman_service_nameserver_remove(service,
423                                                 dhcp->nameservers[i], false,
424                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
425                         }
426 #else
427                         for (i = 0; dhcp->nameservers[i]; i++) {
428                                 __connman_service_nameserver_remove(service,
429                                                 dhcp->nameservers[i], false);
430                         }
431 #endif
432                         g_strfreev(dhcp->nameservers);
433                 }
434
435                 dhcp->nameservers = nameservers;
436
437                 for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
438 #if defined TIZEN_EXT
439                         __connman_service_nameserver_append(service,
440                                                 dhcp->nameservers[i], false,
441                                                 CONNMAN_IPCONFIG_TYPE_IPV4);
442 #else
443                         __connman_service_nameserver_append(service,
444                                                 dhcp->nameservers[i], false);
445 #endif
446                 }
447         } else {
448                 g_strfreev(nameservers);
449         }
450
451         if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
452                 if (dhcp->timeservers) {
453                         for (i = 0; dhcp->timeservers[i]; i++) {
454                                 __connman_service_timeserver_remove(service,
455                                                         dhcp->timeservers[i]);
456                         }
457                         g_strfreev(dhcp->timeservers);
458                 }
459
460                 dhcp->timeservers = timeservers;
461
462                 for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
463                         __connman_service_timeserver_append(service,
464                                                         dhcp->timeservers[i]);
465                 }
466         } else {
467                 g_strfreev(timeservers);
468         }
469
470         if (g_strcmp0(pac, dhcp->pac) != 0) {
471                 g_free(dhcp->pac);
472                 dhcp->pac = pac;
473
474                 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
475                                                                 dhcp->pac);
476         }
477
478         if (connman_setting_get_bool("Enable6to4"))
479                 __connman_6to4_probe(service);
480
481         return true;
482 }
483
484 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
485 {
486         struct connman_dhcp *dhcp = user_data;
487         GList *option = NULL;
488         enum connman_ipconfig_method old_method;
489         char *address, *netmask = NULL, *gateway = NULL;
490         const char *c_address, *c_gateway;
491         unsigned char prefixlen, c_prefixlen;
492         bool ip_change = false;
493
494         DBG("Lease available");
495
496         if (dhcp->ipv4ll_client) {
497                 ipv4ll_stop_client(dhcp);
498                 dhcp_invalidate(dhcp, false);
499         }
500
501         c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
502         c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
503         c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
504
505         address = g_dhcp_client_get_address(dhcp_client);
506
507         __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
508         DBG("last address %s", address);
509
510 #if defined TIZEN_EXT
511         int dhcp_lease_duration = g_dhcp_client_get_dhcp_lease_duration(dhcp_client);
512 #endif
513
514         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
515         if (option)
516                 netmask = g_strdup(option->data);
517
518         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
519         if (option)
520                 gateway = g_strdup(option->data);
521
522         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
523         if (prefixlen == 255)
524                 connman_warn("netmask: %s is invalid", netmask);
525
526         DBG("c_address %s", c_address);
527
528         if (g_strcmp0(address, c_address)) {
529                 ip_change = true;
530                 if (c_address) {
531                         /* Remove old ip address */
532                         __connman_ipconfig_address_remove(dhcp->ipconfig);
533                 }
534         }
535         if (g_strcmp0(gateway, c_gateway)) {
536                 ip_change = true;
537                 if (c_gateway) {
538                         /* Remove gateway ip address */
539                         __connman_ipconfig_gateway_remove(dhcp->ipconfig);
540                 }
541         } else if (prefixlen != c_prefixlen)
542                 ip_change = true;
543
544         old_method = __connman_ipconfig_get_method(dhcp->ipconfig);
545         __connman_ipconfig_set_method(dhcp->ipconfig,
546                                                 CONNMAN_IPCONFIG_METHOD_DHCP);
547
548 #if defined TIZEN_EXT
549         __connman_ipconfig_set_dhcp_lease_duration(dhcp->ipconfig, dhcp_lease_duration);
550 #endif
551
552         /*
553          * Notify IPv4.Configuration's method moved back to DHCP.
554          *
555          * This is the case ConnMan initially set an address by using
556          * IPv4LL because DHCP failed but now we got an address from DHCP.
557          */
558         if (old_method == CONNMAN_IPCONFIG_METHOD_AUTO) {
559                 struct connman_service *service =
560                         connman_service_lookup_from_network(dhcp->network);
561
562                 if (service)
563                         __connman_service_notify_ipv4_configuration(service);
564         }
565
566         if (ip_change) {
567                 __connman_ipconfig_set_local(dhcp->ipconfig, address);
568                 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
569                 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
570         }
571
572         if (!apply_lease_available_on_network(dhcp_client, dhcp))
573                 goto done;
574
575         if (ip_change)
576                 dhcp_valid(dhcp);
577
578 done:
579         g_free(address);
580         g_free(netmask);
581         g_free(gateway);
582 }
583
584 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
585 {
586         struct connman_dhcp *dhcp = user_data;
587         enum connman_ipconfig_method old_method;
588         char *address, *netmask;
589         unsigned char prefixlen;
590
591         DBG("IPV4LL available");
592
593         address = g_dhcp_client_get_address(ipv4ll_client);
594         netmask = g_dhcp_client_get_netmask(ipv4ll_client);
595
596         prefixlen = connman_ipaddress_calc_netmask_len(netmask);
597
598         old_method = __connman_ipconfig_get_method(dhcp->ipconfig);
599         __connman_ipconfig_set_method(dhcp->ipconfig,
600                                                 CONNMAN_IPCONFIG_METHOD_AUTO);
601
602         /*
603          * Notify IPv4.Configuration's method is AUTO now.
604          *
605          * This is the case DHCP failed thus ConnMan used IPv4LL to get an
606          * address. Set IPv4.Configuration method to AUTO allows user to
607          * ask for a DHCP address by setting the method again to DHCP.
608          */
609         if (old_method == CONNMAN_IPCONFIG_METHOD_DHCP) {
610                 struct connman_service *service =
611                         connman_service_lookup_from_network(dhcp->network);
612
613                 if (service)
614                         __connman_service_notify_ipv4_configuration(service);
615         }
616
617         __connman_ipconfig_set_local(dhcp->ipconfig, address);
618         __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
619         __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
620
621         dhcp_valid(dhcp);
622
623         g_free(address);
624         g_free(netmask);
625 }
626
627 static int dhcp_initialize(struct connman_dhcp *dhcp)
628 {
629         GDHCPClient *dhcp_client;
630         GDHCPClientError error;
631         int index;
632         const char *vendor_class_id;
633
634         DBG("dhcp %p", dhcp);
635
636         index = __connman_ipconfig_get_index(dhcp->ipconfig);
637
638         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
639         if (error != G_DHCP_CLIENT_ERROR_NONE)
640 #if defined TIZEN_EXT
641         {
642                 DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
643 #endif
644                 return -EINVAL;
645 #if defined TIZEN_EXT
646         }
647 #endif
648
649 #if !defined TIZEN_EXT
650         if (getenv("CONNMAN_DHCP_DEBUG")) {
651 #endif
652                 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
653                                                         index);
654                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
655                                         dhcp->dhcp_debug_prefix);
656 #if !defined TIZEN_EXT
657         }
658 #endif
659
660         g_dhcp_client_set_id(dhcp_client);
661
662         if (dhcp->network) {
663                 struct connman_service *service;
664                 const char *hostname;
665
666                 service = connman_service_lookup_from_network(dhcp->network);
667
668                 hostname = __connman_service_get_hostname(service);
669                 if (!hostname)
670                         hostname = connman_utsname_get_hostname();
671
672                 if (hostname)
673                         g_dhcp_client_set_send(dhcp_client,
674                                                 G_DHCP_HOST_NAME, hostname);
675
676                 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
677                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
678                 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
679                 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
680                 g_dhcp_client_set_request(dhcp_client, 252);
681                 g_dhcp_client_set_request(dhcp_client, G_DHCP_MTU);
682         }
683
684         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
685         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
686
687         vendor_class_id = connman_setting_get_string("VendorClassID");
688         if (vendor_class_id)
689                 g_dhcp_client_set_send(dhcp_client, G_DHCP_VENDOR_CLASS_ID,
690                                         vendor_class_id);
691
692         g_dhcp_client_register_event(dhcp_client,
693                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
694                                                 lease_available_cb, dhcp);
695
696         g_dhcp_client_register_event(dhcp_client,
697                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
698
699         g_dhcp_client_register_event(dhcp_client,
700                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
701
702         dhcp->dhcp_client = dhcp_client;
703
704         return 0;
705 }
706
707 static int dhcp_release(struct connman_dhcp *dhcp)
708 {
709         DBG("dhcp %p", dhcp);
710
711         if (dhcp->timeout > 0) {
712                 g_source_remove(dhcp->timeout);
713                 dhcp->timeout = 0;
714         }
715
716         if (dhcp->dhcp_client) {
717                 g_dhcp_client_stop(dhcp->dhcp_client);
718                 g_dhcp_client_unref(dhcp->dhcp_client);
719         }
720
721         dhcp->dhcp_client = NULL;
722
723         g_free(dhcp->dhcp_debug_prefix);
724         dhcp->dhcp_debug_prefix = NULL;
725
726         ipv4ll_stop_client(dhcp);
727
728         return 0;
729 }
730
731 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
732 {
733         struct connman_dhcp *dhcp;
734
735         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
736         if (!dhcp)
737                 return NULL;
738
739         return g_dhcp_client_get_server_address(dhcp->dhcp_client);
740 }
741
742 #if defined TIZEN_EXT_WIFI_MESH
743 int __connman_mesh_dhcp_start(struct connman_ipconfig *ipconfig,
744                         dhcp_cb callback, gpointer user_data)
745 {
746         struct connman_dhcp *dhcp;
747         int err;
748
749         DBG("");
750
751         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
752         if (!dhcp) {
753
754                 dhcp = g_try_new0(struct connman_dhcp, 1);
755                 if (!dhcp)
756                         return -ENOMEM;
757
758                 dhcp->ipconfig = ipconfig;
759                 __connman_ipconfig_ref(ipconfig);
760
761                 err = dhcp_initialize(dhcp);
762
763                 if (err < 0) {
764                         g_free(dhcp);
765                         return err;
766                 }
767
768                 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
769         }
770
771         dhcp->callback = callback;
772         dhcp->user_data = user_data;
773         return g_dhcp_client_start(dhcp->dhcp_client, NULL);
774 }
775 #endif
776
777 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
778                         struct connman_network *network, dhcp_cb callback,
779                         gpointer user_data)
780 {
781 #if !defined TIZEN_EXT
782         const char *last_addr = NULL;
783 #endif
784         struct connman_dhcp *dhcp;
785         int err;
786
787         DBG("");
788
789         if (network) {
790                 struct connman_service *service;
791
792                 service = connman_service_lookup_from_network(network);
793                 if (!service)
794                         return -EINVAL;
795         }
796
797 #if !defined TIZEN_EXT
798         last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
799 #endif
800
801         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
802         if (!dhcp) {
803
804                 dhcp = g_try_new0(struct connman_dhcp, 1);
805                 if (!dhcp)
806                         return -ENOMEM;
807
808                 dhcp->ipconfig = ipconfig;
809                 __connman_ipconfig_ref(ipconfig);
810
811                 if (network) {
812                         dhcp->network = network;
813                         connman_network_ref(network);
814                 }
815
816                 err = dhcp_initialize(dhcp);
817
818                 if (err < 0) {
819                         if (network)
820                                 connman_network_unref(network);
821                         g_free(dhcp);
822                         return err;
823                 }
824
825                 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
826         }
827
828         dhcp->callback = callback;
829         dhcp->user_data = user_data;
830
831 #if defined TIZEN_EXT
832         DBG("Start DHCP with DHCPDISCOVER request");
833
834         return g_dhcp_client_start(dhcp->dhcp_client, NULL);
835 #else
836         return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
837 #endif
838 }
839
840 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
841 {
842         struct connman_dhcp *dhcp;
843
844         DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
845
846         if (!ipconfig_table)
847                 return;
848
849         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
850         if (dhcp) {
851                 g_hash_table_remove(ipconfig_table, ipconfig);
852                 __connman_ipconfig_unref(ipconfig);
853                 if (dhcp->network)
854                         connman_network_unref(dhcp->network);
855                 dhcp_release(dhcp);
856                 dhcp_invalidate(dhcp, false);
857                 dhcp_free(dhcp);
858         }
859 }
860
861 void __connman_dhcp_decline(struct connman_ipconfig *ipconfig)
862 {
863         struct connman_dhcp *dhcp;
864         const char *address;
865         struct in_addr addr;
866
867         DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
868
869         if (!ipconfig_table)
870                 return;
871
872         dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
873         if (dhcp) {
874                 address = __connman_ipconfig_get_local(ipconfig);
875                 if (!address)
876                         return;
877
878                 if (inet_pton(AF_INET, address, &addr) != 1)
879                         connman_error("Could not convert address %s", address);
880
881                 g_dhcp_client_decline(dhcp->dhcp_client, htonl(addr.s_addr));
882         }
883 }
884
885 int __connman_dhcp_init(void)
886 {
887         DBG("");
888
889         ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
890                                                                 NULL, NULL);
891
892         return 0;
893 }
894
895 void __connman_dhcp_cleanup(void)
896 {
897         DBG("");
898
899         g_hash_table_destroy(ipconfig_table);
900         ipconfig_table = NULL;
901 }