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