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