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)
65 DBG("dhcp_free [%p]", dhcp);
67 g_strfreev(dhcp->nameservers);
68 g_strfreev(dhcp->timeservers);
71 dhcp->nameservers = NULL;
72 dhcp->timeservers = NULL;
81 static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
84 DBG("dhcp [%p] ipv4ll_client [%p]", dhcp, dhcp->ipv4ll_client);
86 if (!dhcp->ipv4ll_client)
89 g_dhcp_client_stop(dhcp->ipv4ll_client);
90 g_dhcp_client_unref(dhcp->ipv4ll_client);
91 dhcp->ipv4ll_client = NULL;
92 ipv4ll_running = false;
94 g_free(dhcp->ipv4ll_debug_prefix);
95 dhcp->ipv4ll_debug_prefix = NULL;
98 static bool apply_dhcp_invalidate_on_network(struct connman_dhcp *dhcp)
100 struct connman_service *service;
106 service = connman_service_lookup_from_network(dhcp->network);
108 connman_error("Can not lookup service");
112 __connman_service_set_domainname(service, NULL);
113 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig, NULL);
115 if (dhcp->timeservers) {
116 for (i = 0; dhcp->timeservers[i]; i++) {
117 __connman_service_timeserver_remove(service,
118 dhcp->timeservers[i]);
121 if (dhcp->nameservers) {
122 for (i = 0; dhcp->nameservers[i]; i++) {
123 #if defined TIZEN_EXT
124 __connman_service_nameserver_remove(service,
125 dhcp->nameservers[i], false,
126 CONNMAN_IPCONFIG_TYPE_IPV4);
128 __connman_service_nameserver_remove(service,
129 dhcp->nameservers[i], false);
138 * dhcp_invalidate: Invalidate an existing DHCP lease
139 * @dhcp: pointer to the DHCP lease to invalidate.
140 * @callback: flag indicating whether or not to invoke the client callback
143 * Invalidates an existing DHCP lease, optionally invoking the client
144 * callback. The caller may wish to avoid the client callback invocation
145 * when the invocation of that callback might otherwise unnecessarily upset
146 * service state due to the IP configuration change implied by this
149 static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
151 DBG("dhcp %p callback %u", dhcp, callback);
156 __connman_6to4_remove(dhcp->ipconfig);
158 if (!apply_dhcp_invalidate_on_network(dhcp))
161 __connman_ipconfig_set_dhcp_address(dhcp->ipconfig,
162 __connman_ipconfig_get_local(dhcp->ipconfig));
163 DBG("last address %s",
164 __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
166 __connman_ipconfig_address_remove(dhcp->ipconfig);
168 __connman_ipconfig_set_local(dhcp->ipconfig, NULL);
169 __connman_ipconfig_set_broadcast(dhcp->ipconfig, NULL);
170 __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
171 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, 0);
173 if (dhcp->callback && callback)
174 dhcp->callback(dhcp->ipconfig, dhcp->network,
175 false, dhcp->user_data);
178 static void dhcp_valid(struct connman_dhcp *dhcp)
181 dhcp->callback(dhcp->ipconfig, dhcp->network,
182 true, dhcp->user_data);
185 static void dhcp_debug(const char *str, void *data)
187 connman_info("%s: %s", (const char *) data, str);
190 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
191 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);
193 static int ipv4ll_start_client(struct connman_dhcp *dhcp)
195 GDHCPClient *ipv4ll_client;
196 GDHCPClientError error;
197 const char *hostname;
201 #if defined TIZEN_EXT
202 DBG("dhcp %p", dhcp);
205 if (dhcp->ipv4ll_client)
208 index = __connman_ipconfig_get_index(dhcp->ipconfig);
210 ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
211 if (error != G_DHCP_CLIENT_ERROR_NONE)
214 #if !defined TIZEN_EXT
215 if (getenv("CONNMAN_DHCP_DEBUG")) {
217 dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
219 g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
220 dhcp->ipv4ll_debug_prefix);
221 #if !defined TIZEN_EXT
225 g_dhcp_client_set_id(ipv4ll_client);
228 hostname = connman_utsname_get_hostname();
230 g_dhcp_client_set_send(ipv4ll_client,
231 G_DHCP_HOST_NAME, hostname);
234 g_dhcp_client_register_event(ipv4ll_client,
235 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
237 g_dhcp_client_register_event(ipv4ll_client,
238 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
239 ipv4ll_available_cb, dhcp);
241 dhcp->ipv4ll_client = ipv4ll_client;
243 err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
245 ipv4ll_stop_client(dhcp);
249 ipv4ll_running = true;
253 static gboolean dhcp_retry_cb(gpointer user_data)
255 struct connman_dhcp *dhcp = user_data;
258 #if defined TIZEN_EXT
259 DBG("dhcp %p", dhcp);
260 DBG("dhcp->timeout %d", dhcp->timeout);
262 g_dhcp_client_start(dhcp->dhcp_client,
263 __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
268 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
270 struct connman_dhcp *dhcp = user_data;
273 DBG("No lease available ipv4ll %d client %p", ipv4ll_running,
274 dhcp->ipv4ll_client);
276 if (dhcp->timeout > 0)
277 g_source_remove(dhcp->timeout);
279 dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
285 err = ipv4ll_start_client(dhcp);
287 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
289 /* Only notify upper layer if we have a problem */
290 dhcp_invalidate(dhcp, !ipv4ll_running);
293 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
295 struct connman_dhcp *dhcp = user_data;
299 /* Upper layer will decide what to do, e.g. nothing or retry. */
300 dhcp_invalidate(dhcp, true);
303 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
305 struct connman_dhcp *dhcp = user_data;
309 ipv4ll_stop_client(dhcp);
312 * Since we lost our IPv4LL configuration we might as notify
315 dhcp_invalidate(dhcp, true);
318 static bool compare_string_arrays(char **array_a, char **array_b)
322 if (!array_a || !array_b)
325 if (g_strv_length(array_a) != g_strv_length(array_b))
328 for (i = 0; array_a[i] &&
330 if (g_strcmp0(array_a[i], array_b[i]) != 0)
337 static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
338 struct connman_dhcp *dhcp)
340 char **nameservers, **timeservers, *pac = NULL;
341 struct connman_service *service;
342 GList *list, *option = NULL;
349 service = connman_service_lookup_from_network(dhcp->network);
351 connman_error("Can not lookup service");
355 option = g_dhcp_client_get_option(dhcp_client, 252);
357 pac = g_strdup(option->data);
359 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
360 ns_entries = g_list_length(option);
361 nameservers = g_try_new0(char *, ns_entries + 1);
363 for (i = 0, list = option;list; list = list->next, i++)
364 nameservers[i] = g_strdup(list->data);
365 nameservers[ns_entries] = NULL;
368 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
370 __connman_service_set_domainname(service, option->data);
372 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
374 __connman_service_set_hostname(service, option->data);
376 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
377 ns_entries = g_list_length(option);
378 timeservers = g_try_new0(char *, ns_entries + 1);
380 for (i = 0, list = option; list; list = list->next, i++)
381 timeservers[i] = g_strdup(list->data);
382 timeservers[ns_entries] = NULL;
385 if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
386 if (dhcp->nameservers) {
387 #if defined TIZEN_EXT
388 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
389 __connman_service_nameserver_remove(service,
390 dhcp->nameservers[i], false,
391 CONNMAN_IPCONFIG_TYPE_IPV4);
394 for (i = 0; dhcp->nameservers[i]; i++) {
395 __connman_service_nameserver_remove(service,
396 dhcp->nameservers[i], false);
399 g_strfreev(dhcp->nameservers);
402 dhcp->nameservers = nameservers;
404 for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
405 #if defined TIZEN_EXT
406 __connman_service_nameserver_append(service,
407 dhcp->nameservers[i], false,
408 CONNMAN_IPCONFIG_TYPE_IPV4);
410 __connman_service_nameserver_append(service,
411 dhcp->nameservers[i], false);
415 g_strfreev(nameservers);
418 if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
419 if (dhcp->timeservers) {
420 for (i = 0; dhcp->timeservers[i]; i++) {
421 __connman_service_timeserver_remove(service,
422 dhcp->timeservers[i]);
424 g_strfreev(dhcp->timeservers);
427 dhcp->timeservers = timeservers;
429 for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
430 __connman_service_timeserver_append(service,
431 dhcp->timeservers[i]);
434 g_strfreev(timeservers);
437 if (g_strcmp0(pac, dhcp->pac) != 0) {
441 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
445 if (connman_setting_get_bool("Enable6to4"))
446 __connman_6to4_probe(service);
451 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
453 struct connman_dhcp *dhcp = user_data;
454 GList *option = NULL;
455 char *address, *netmask = NULL, *gateway = NULL;
456 const char *c_address, *c_gateway;
457 unsigned char prefixlen, c_prefixlen;
458 bool ip_change = false;
460 DBG("Lease available");
462 if (dhcp->ipv4ll_client) {
463 ipv4ll_stop_client(dhcp);
464 dhcp_invalidate(dhcp, false);
467 c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
468 c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
469 c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
471 address = g_dhcp_client_get_address(dhcp_client);
473 __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
474 DBG("last address %s", address);
476 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
478 netmask = g_strdup(option->data);
480 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
482 gateway = g_strdup(option->data);
484 prefixlen = connman_ipaddress_calc_netmask_len(netmask);
485 if (prefixlen == 255)
486 connman_warn("netmask: %s is invalid", netmask);
488 DBG("c_address %s", c_address);
490 if (g_strcmp0(address, c_address)) {
493 /* Remove old ip address */
494 __connman_ipconfig_address_remove(dhcp->ipconfig);
497 if (g_strcmp0(gateway, c_gateway)) {
500 /* Remove gateway ip address */
501 __connman_ipconfig_gateway_remove(dhcp->ipconfig);
503 } else if (prefixlen != c_prefixlen)
506 __connman_ipconfig_set_method(dhcp->ipconfig,
507 CONNMAN_IPCONFIG_METHOD_DHCP);
509 __connman_ipconfig_set_local(dhcp->ipconfig, address);
510 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
511 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
514 if (!apply_lease_available_on_network(dhcp_client, dhcp))
526 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
528 struct connman_dhcp *dhcp = user_data;
529 char *address, *netmask;
530 unsigned char prefixlen;
532 DBG("IPV4LL available");
534 address = g_dhcp_client_get_address(ipv4ll_client);
535 netmask = g_dhcp_client_get_netmask(ipv4ll_client);
537 prefixlen = connman_ipaddress_calc_netmask_len(netmask);
539 __connman_ipconfig_set_method(dhcp->ipconfig,
540 CONNMAN_IPCONFIG_METHOD_DHCP);
541 __connman_ipconfig_set_local(dhcp->ipconfig, address);
542 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
543 __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
551 static int dhcp_initialize(struct connman_dhcp *dhcp)
553 GDHCPClient *dhcp_client;
554 GDHCPClientError error;
557 DBG("dhcp %p", dhcp);
559 index = __connman_ipconfig_get_index(dhcp->ipconfig);
561 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
562 if (error != G_DHCP_CLIENT_ERROR_NONE)
563 #if defined TIZEN_EXT
565 DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
568 #if defined TIZEN_EXT
572 #if !defined TIZEN_EXT
573 if (getenv("CONNMAN_DHCP_DEBUG")) {
575 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
577 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
578 dhcp->dhcp_debug_prefix);
579 #if !defined TIZEN_EXT
583 g_dhcp_client_set_id(dhcp_client);
586 struct connman_service *service;
587 const char *hostname;
589 service = connman_service_lookup_from_network(dhcp->network);
591 hostname = __connman_service_get_hostname(service);
593 hostname = connman_utsname_get_hostname();
596 g_dhcp_client_set_send(dhcp_client,
597 G_DHCP_HOST_NAME, hostname);
599 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
600 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
601 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
602 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
603 g_dhcp_client_set_request(dhcp_client, 252);
606 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
607 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
609 g_dhcp_client_register_event(dhcp_client,
610 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
611 lease_available_cb, dhcp);
613 g_dhcp_client_register_event(dhcp_client,
614 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
616 g_dhcp_client_register_event(dhcp_client,
617 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
619 dhcp->dhcp_client = dhcp_client;
624 static int dhcp_release(struct connman_dhcp *dhcp)
626 DBG("dhcp %p", dhcp);
628 if (dhcp->timeout > 0) {
629 g_source_remove(dhcp->timeout);
633 if (dhcp->dhcp_client) {
634 g_dhcp_client_stop(dhcp->dhcp_client);
635 g_dhcp_client_unref(dhcp->dhcp_client);
638 dhcp->dhcp_client = NULL;
640 g_free(dhcp->dhcp_debug_prefix);
641 dhcp->dhcp_debug_prefix = NULL;
643 ipv4ll_stop_client(dhcp);
648 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
650 struct connman_dhcp *dhcp;
652 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
656 return g_dhcp_client_get_server_address(dhcp->dhcp_client);
659 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
660 struct connman_network *network, dhcp_cb callback,
663 #if !defined TIZEN_EXT
664 const char *last_addr = NULL;
666 struct connman_dhcp *dhcp;
672 struct connman_service *service;
674 service = connman_service_lookup_from_network(network);
679 #if !defined TIZEN_EXT
680 last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
683 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
686 dhcp = g_try_new0(struct connman_dhcp, 1);
690 dhcp->ipconfig = ipconfig;
691 __connman_ipconfig_ref(ipconfig);
694 dhcp->network = network;
695 connman_network_ref(network);
698 err = dhcp_initialize(dhcp);
702 connman_network_unref(network);
707 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
710 dhcp->callback = callback;
711 dhcp->user_data = user_data;
713 #if defined TIZEN_EXT
714 DBG("Start DHCP with DHCPDISCOVER request");
716 return g_dhcp_client_start(dhcp->dhcp_client, NULL);
718 return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
722 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
724 struct connman_dhcp *dhcp;
726 DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
731 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
733 g_hash_table_remove(ipconfig_table, ipconfig);
734 __connman_ipconfig_unref(ipconfig);
736 connman_network_unref(dhcp->network);
738 dhcp_invalidate(dhcp, false);
743 int __connman_dhcp_init(void)
747 ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
753 void __connman_dhcp_cleanup(void)
757 g_hash_table_destroy(ipconfig_table);
758 ipconfig_table = NULL;
760 dhcp_cleanup_random();