5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
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.
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.
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
30 #include <connman/ipconfig.h>
31 #include <include/setting.h>
33 #include <gdhcp/gdhcp.h>
39 #define RATE_LIMIT_INTERVAL 60 /* delay between successive attempts */
42 struct connman_ipconfig *ipconfig;
43 struct connman_network *network;
53 GDHCPClient *ipv4ll_client;
54 GDHCPClient *dhcp_client;
55 char *ipv4ll_debug_prefix;
56 char *dhcp_debug_prefix;
59 static GHashTable *ipconfig_table;
60 static bool ipv4ll_running;
62 static void dhcp_free(struct connman_dhcp *dhcp)
64 g_strfreev(dhcp->nameservers);
65 g_strfreev(dhcp->timeservers);
68 dhcp->nameservers = NULL;
69 dhcp->timeservers = NULL;
75 static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
77 if (!dhcp->ipv4ll_client)
80 g_dhcp_client_stop(dhcp->ipv4ll_client);
81 g_dhcp_client_unref(dhcp->ipv4ll_client);
82 dhcp->ipv4ll_client = NULL;
83 ipv4ll_running = false;
85 g_free(dhcp->ipv4ll_debug_prefix);
86 dhcp->ipv4ll_debug_prefix = NULL;
89 static bool apply_dhcp_invalidate_on_network(struct connman_dhcp *dhcp)
91 struct connman_service *service;
97 service = connman_service_lookup_from_network(dhcp->network);
99 connman_error("Can not lookup service");
103 __connman_service_set_domainname(service, NULL);
104 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig, NULL);
106 if (dhcp->timeservers) {
107 for (i = 0; dhcp->timeservers[i]; i++) {
108 __connman_service_timeserver_remove(service,
109 dhcp->timeservers[i]);
112 if (dhcp->nameservers) {
113 for (i = 0; dhcp->nameservers[i]; i++) {
114 #if defined TIZEN_EXT
115 __connman_service_nameserver_remove(service,
116 dhcp->nameservers[i], false,
117 CONNMAN_IPCONFIG_TYPE_IPV4);
119 __connman_service_nameserver_remove(service,
120 dhcp->nameservers[i], false);
129 * dhcp_invalidate: Invalidate an existing DHCP lease
130 * @dhcp: pointer to the DHCP lease to invalidate.
131 * @callback: flag indicating whether or not to invoke the client callback
134 * Invalidates an existing DHCP lease, optionally invoking the client
135 * callback. The caller may wish to avoid the client callback invocation
136 * when the invocation of that callback might otherwise unnecessarily upset
137 * service state due to the IP configuration change implied by this
140 static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
142 DBG("dhcp %p callback %u", dhcp, callback);
147 __connman_6to4_remove(dhcp->ipconfig);
149 if (!apply_dhcp_invalidate_on_network(dhcp))
152 __connman_ipconfig_set_dhcp_address(dhcp->ipconfig,
153 __connman_ipconfig_get_local(dhcp->ipconfig));
154 DBG("last address %s",
155 __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
157 __connman_ipconfig_address_remove(dhcp->ipconfig);
159 __connman_ipconfig_set_local(dhcp->ipconfig, NULL);
160 __connman_ipconfig_set_broadcast(dhcp->ipconfig, NULL);
161 __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
162 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, 0);
164 if (dhcp->callback && callback)
165 dhcp->callback(dhcp->ipconfig, dhcp->network,
166 false, dhcp->user_data);
169 static void dhcp_valid(struct connman_dhcp *dhcp)
172 dhcp->callback(dhcp->ipconfig, dhcp->network,
173 true, dhcp->user_data);
176 static void dhcp_debug(const char *str, void *data)
178 connman_info("%s: %s", (const char *) data, str);
181 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
182 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);
184 static int ipv4ll_start_client(struct connman_dhcp *dhcp)
186 GDHCPClient *ipv4ll_client;
187 GDHCPClientError error;
188 const char *hostname;
192 if (dhcp->ipv4ll_client)
195 index = __connman_ipconfig_get_index(dhcp->ipconfig);
197 ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
198 if (error != G_DHCP_CLIENT_ERROR_NONE)
201 #if !defined TIZEN_EXT
202 if (getenv("CONNMAN_DHCP_DEBUG")) {
204 dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
206 g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
207 dhcp->ipv4ll_debug_prefix);
208 #if !defined TIZEN_EXT
212 g_dhcp_client_set_id(ipv4ll_client);
215 hostname = connman_utsname_get_hostname();
217 g_dhcp_client_set_send(ipv4ll_client,
218 G_DHCP_HOST_NAME, hostname);
221 g_dhcp_client_register_event(ipv4ll_client,
222 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
224 g_dhcp_client_register_event(ipv4ll_client,
225 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
226 ipv4ll_available_cb, dhcp);
228 dhcp->ipv4ll_client = ipv4ll_client;
230 err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
232 ipv4ll_stop_client(dhcp);
236 ipv4ll_running = true;
240 static gboolean dhcp_retry_cb(gpointer user_data)
242 struct connman_dhcp *dhcp = user_data;
246 g_dhcp_client_start(dhcp->dhcp_client,
247 __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
252 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
254 struct connman_dhcp *dhcp = user_data;
257 DBG("No lease available ipv4ll %d client %p", ipv4ll_running,
258 dhcp->ipv4ll_client);
260 dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
266 err = ipv4ll_start_client(dhcp);
268 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
270 /* Only notify upper layer if we have a problem */
271 dhcp_invalidate(dhcp, !ipv4ll_running);
274 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
276 struct connman_dhcp *dhcp = user_data;
280 /* Upper layer will decide what to do, e.g. nothing or retry. */
281 dhcp_invalidate(dhcp, true);
284 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
286 struct connman_dhcp *dhcp = user_data;
290 ipv4ll_stop_client(dhcp);
293 * Since we lost our IPv4LL configuration we might as notify
296 dhcp_invalidate(dhcp, true);
299 static bool compare_string_arrays(char **array_a, char **array_b)
303 if (!array_a || !array_b)
306 if (g_strv_length(array_a) != g_strv_length(array_b))
309 for (i = 0; array_a[i] &&
311 if (g_strcmp0(array_a[i], array_b[i]) != 0)
318 static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
319 struct connman_dhcp *dhcp)
321 char **nameservers, **timeservers, *pac = NULL;
322 struct connman_service *service;
323 GList *list, *option = NULL;
330 service = connman_service_lookup_from_network(dhcp->network);
332 connman_error("Can not lookup service");
336 option = g_dhcp_client_get_option(dhcp_client, 252);
338 pac = g_strdup(option->data);
340 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
341 ns_entries = g_list_length(option);
342 nameservers = g_try_new0(char *, ns_entries + 1);
344 for (i = 0, list = option;list; list = list->next, i++)
345 nameservers[i] = g_strdup(list->data);
346 nameservers[ns_entries] = NULL;
349 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
351 __connman_service_set_domainname(service, option->data);
353 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
355 __connman_service_set_hostname(service, option->data);
357 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
358 ns_entries = g_list_length(option);
359 timeservers = g_try_new0(char *, ns_entries + 1);
361 for (i = 0, list = option; list; list = list->next, i++)
362 timeservers[i] = g_strdup(list->data);
363 timeservers[ns_entries] = NULL;
366 if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
367 if (dhcp->nameservers) {
368 #if defined TIZEN_EXT
369 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
370 __connman_service_nameserver_remove(service,
371 dhcp->nameservers[i], false,
372 CONNMAN_IPCONFIG_TYPE_IPV4);
375 for (i = 0; dhcp->nameservers[i]; i++) {
376 __connman_service_nameserver_remove(service,
377 dhcp->nameservers[i], false);
380 g_strfreev(dhcp->nameservers);
383 dhcp->nameservers = nameservers;
385 for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
386 #if defined TIZEN_EXT
387 __connman_service_nameserver_append(service,
388 dhcp->nameservers[i], false,
389 CONNMAN_IPCONFIG_TYPE_IPV4);
391 __connman_service_nameserver_append(service,
392 dhcp->nameservers[i], false);
396 g_strfreev(nameservers);
399 if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
400 if (dhcp->timeservers) {
401 for (i = 0; dhcp->timeservers[i]; i++) {
402 __connman_service_timeserver_remove(service,
403 dhcp->timeservers[i]);
405 g_strfreev(dhcp->timeservers);
408 dhcp->timeservers = timeservers;
410 for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
411 __connman_service_timeserver_append(service,
412 dhcp->timeservers[i]);
415 g_strfreev(timeservers);
418 if (g_strcmp0(pac, dhcp->pac) != 0) {
422 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
426 if (connman_setting_get_bool("Enable6to4"))
427 __connman_6to4_probe(service);
432 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
434 struct connman_dhcp *dhcp = user_data;
435 GList *option = NULL;
436 char *address, *netmask = NULL, *gateway = NULL;
437 const char *c_address, *c_gateway;
438 unsigned char prefixlen, c_prefixlen;
441 DBG("Lease available");
443 if (dhcp->ipv4ll_client) {
444 ipv4ll_stop_client(dhcp);
445 dhcp_invalidate(dhcp, false);
448 c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
449 c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
450 c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
452 address = g_dhcp_client_get_address(dhcp_client);
454 __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
455 DBG("last address %s", address);
457 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
459 netmask = g_strdup(option->data);
461 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
463 gateway = g_strdup(option->data);
465 prefixlen = connman_ipaddress_calc_netmask_len(netmask);
466 if (prefixlen == 255)
467 connman_warn("netmask: %s is invalid", netmask);
469 DBG("c_address %s", c_address);
471 if (g_strcmp0(address, c_address))
473 else if (g_strcmp0(gateway, c_gateway))
475 else if (prefixlen != c_prefixlen)
480 __connman_ipconfig_set_method(dhcp->ipconfig,
481 CONNMAN_IPCONFIG_METHOD_DHCP);
483 __connman_ipconfig_set_local(dhcp->ipconfig, address);
484 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
485 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
488 if (!apply_lease_available_on_network(dhcp_client, dhcp))
500 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
502 struct connman_dhcp *dhcp = user_data;
503 char *address, *netmask;
504 unsigned char prefixlen;
506 DBG("IPV4LL available");
508 address = g_dhcp_client_get_address(ipv4ll_client);
509 netmask = g_dhcp_client_get_netmask(ipv4ll_client);
511 prefixlen = connman_ipaddress_calc_netmask_len(netmask);
513 __connman_ipconfig_set_method(dhcp->ipconfig,
514 CONNMAN_IPCONFIG_METHOD_DHCP);
515 __connman_ipconfig_set_local(dhcp->ipconfig, address);
516 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
517 __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
525 static int dhcp_initialize(struct connman_dhcp *dhcp)
527 GDHCPClient *dhcp_client;
528 GDHCPClientError error;
531 DBG("dhcp %p", dhcp);
533 index = __connman_ipconfig_get_index(dhcp->ipconfig);
535 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
536 if (error != G_DHCP_CLIENT_ERROR_NONE)
537 #if defined TIZEN_EXT
539 DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
542 #if defined TIZEN_EXT
546 #if !defined TIZEN_EXT
547 if (getenv("CONNMAN_DHCP_DEBUG")) {
549 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
551 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
552 dhcp->dhcp_debug_prefix);
553 #if !defined TIZEN_EXT
557 g_dhcp_client_set_id(dhcp_client);
560 struct connman_service *service;
561 const char *hostname;
563 service = connman_service_lookup_from_network(dhcp->network);
565 hostname = __connman_service_get_hostname(service);
567 hostname = connman_utsname_get_hostname();
570 g_dhcp_client_set_send(dhcp_client,
571 G_DHCP_HOST_NAME, hostname);
573 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
574 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
575 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
576 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
577 g_dhcp_client_set_request(dhcp_client, 252);
580 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
581 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
583 g_dhcp_client_register_event(dhcp_client,
584 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
585 lease_available_cb, dhcp);
587 g_dhcp_client_register_event(dhcp_client,
588 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
590 g_dhcp_client_register_event(dhcp_client,
591 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
593 dhcp->dhcp_client = dhcp_client;
598 static int dhcp_release(struct connman_dhcp *dhcp)
600 DBG("dhcp %p", dhcp);
602 if (dhcp->timeout > 0) {
603 g_source_remove(dhcp->timeout);
607 if (dhcp->dhcp_client) {
608 g_dhcp_client_stop(dhcp->dhcp_client);
609 g_dhcp_client_unref(dhcp->dhcp_client);
612 dhcp->dhcp_client = NULL;
614 g_free(dhcp->dhcp_debug_prefix);
615 dhcp->dhcp_debug_prefix = NULL;
617 ipv4ll_stop_client(dhcp);
622 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
624 struct connman_dhcp *dhcp;
626 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
630 return g_dhcp_client_get_server_address(dhcp->dhcp_client);
633 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
634 struct connman_network *network, dhcp_cb callback,
637 #if !defined TIZEN_EXT
638 const char *last_addr = NULL;
640 struct connman_dhcp *dhcp;
646 struct connman_service *service;
648 service = connman_service_lookup_from_network(network);
653 #if !defined TIZEN_EXT
654 last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
657 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
660 dhcp = g_try_new0(struct connman_dhcp, 1);
664 dhcp->ipconfig = ipconfig;
665 __connman_ipconfig_ref(ipconfig);
668 dhcp->network = network;
669 connman_network_ref(network);
672 err = dhcp_initialize(dhcp);
676 connman_network_unref(network);
681 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
684 dhcp->callback = callback;
685 dhcp->user_data = user_data;
687 #if defined TIZEN_EXT
688 DBG("Start DHCP with DHCPDISCOVER request");
690 return g_dhcp_client_start(dhcp->dhcp_client, NULL);
692 return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
697 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
699 struct connman_dhcp *dhcp;
701 DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
706 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
708 g_hash_table_remove(ipconfig_table, ipconfig);
709 __connman_ipconfig_unref(ipconfig);
711 connman_network_unref(dhcp->network);
713 dhcp_invalidate(dhcp, false);
718 int __connman_dhcp_init(void)
722 ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
728 void __connman_dhcp_cleanup(void)
732 g_hash_table_destroy(ipconfig_table);
733 ipconfig_table = NULL;
735 dhcp_cleanup_random();