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 __connman_service_nameserver_remove(service,
115 dhcp->nameservers[i], false);
123 * dhcp_invalidate: Invalidate an existing DHCP lease
124 * @dhcp: pointer to the DHCP lease to invalidate.
125 * @callback: flag indicating whether or not to invoke the client callback
128 * Invalidates an existing DHCP lease, optionally invoking the client
129 * callback. The caller may wish to avoid the client callback invocation
130 * when the invocation of that callback might otherwise unnecessarily upset
131 * service state due to the IP configuration change implied by this
134 static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
136 DBG("dhcp %p callback %u", dhcp, callback);
141 __connman_6to4_remove(dhcp->ipconfig);
143 if (!apply_dhcp_invalidate_on_network(dhcp))
146 __connman_ipconfig_set_dhcp_address(dhcp->ipconfig,
147 __connman_ipconfig_get_local(dhcp->ipconfig));
148 DBG("last address %s",
149 __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
151 __connman_ipconfig_address_remove(dhcp->ipconfig);
153 __connman_ipconfig_set_local(dhcp->ipconfig, NULL);
154 __connman_ipconfig_set_broadcast(dhcp->ipconfig, NULL);
155 __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
156 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, 0);
158 if (dhcp->callback && callback)
159 dhcp->callback(dhcp->ipconfig, dhcp->network,
160 false, dhcp->user_data);
163 static void dhcp_valid(struct connman_dhcp *dhcp)
166 dhcp->callback(dhcp->ipconfig, dhcp->network,
167 true, dhcp->user_data);
170 static void dhcp_debug(const char *str, void *data)
172 connman_info("%s: %s", (const char *) data, str);
175 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
176 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);
178 static int ipv4ll_start_client(struct connman_dhcp *dhcp)
180 GDHCPClient *ipv4ll_client;
181 GDHCPClientError error;
182 const char *hostname;
186 if (dhcp->ipv4ll_client)
189 index = __connman_ipconfig_get_index(dhcp->ipconfig);
191 ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
192 if (error != G_DHCP_CLIENT_ERROR_NONE)
195 #if !defined TIZEN_EXT
196 if (getenv("CONNMAN_DHCP_DEBUG")) {
198 dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
200 g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
201 dhcp->ipv4ll_debug_prefix);
202 #if !defined TIZEN_EXT
206 g_dhcp_client_set_id(ipv4ll_client);
209 hostname = connman_utsname_get_hostname();
211 g_dhcp_client_set_send(ipv4ll_client,
212 G_DHCP_HOST_NAME, hostname);
215 g_dhcp_client_register_event(ipv4ll_client,
216 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
218 g_dhcp_client_register_event(ipv4ll_client,
219 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
220 ipv4ll_available_cb, dhcp);
222 dhcp->ipv4ll_client = ipv4ll_client;
224 err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
226 ipv4ll_stop_client(dhcp);
230 ipv4ll_running = true;
234 static gboolean dhcp_retry_cb(gpointer user_data)
236 struct connman_dhcp *dhcp = user_data;
240 g_dhcp_client_start(dhcp->dhcp_client,
241 __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
246 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
248 struct connman_dhcp *dhcp = user_data;
251 DBG("No lease available ipv4ll %d client %p", ipv4ll_running,
252 dhcp->ipv4ll_client);
254 dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
260 err = ipv4ll_start_client(dhcp);
262 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
264 /* Only notify upper layer if we have a problem */
265 dhcp_invalidate(dhcp, !ipv4ll_running);
268 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
270 struct connman_dhcp *dhcp = user_data;
274 /* Upper layer will decide what to do, e.g. nothing or retry. */
275 dhcp_invalidate(dhcp, true);
278 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
280 struct connman_dhcp *dhcp = user_data;
284 ipv4ll_stop_client(dhcp);
287 * Since we lost our IPv4LL configuration we might as notify
290 dhcp_invalidate(dhcp, true);
293 static bool compare_string_arrays(char **array_a, char **array_b)
297 if (!array_a || !array_b)
300 if (g_strv_length(array_a) != g_strv_length(array_b))
303 for (i = 0; array_a[i] &&
305 if (g_strcmp0(array_a[i], array_b[i]) != 0)
312 static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
313 struct connman_dhcp *dhcp)
315 char **nameservers, **timeservers, *pac = NULL;
316 struct connman_service *service;
317 GList *list, *option = NULL;
324 service = connman_service_lookup_from_network(dhcp->network);
326 connman_error("Can not lookup service");
330 option = g_dhcp_client_get_option(dhcp_client, 252);
332 pac = g_strdup(option->data);
334 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
335 ns_entries = g_list_length(option);
336 nameservers = g_try_new0(char *, ns_entries + 1);
338 for (i = 0, list = option;list; list = list->next, i++)
339 nameservers[i] = g_strdup(list->data);
340 nameservers[ns_entries] = NULL;
343 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
345 __connman_service_set_domainname(service, option->data);
347 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
349 __connman_service_set_hostname(service, option->data);
351 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
352 ns_entries = g_list_length(option);
353 timeservers = g_try_new0(char *, ns_entries + 1);
355 for (i = 0, list = option; list; list = list->next, i++)
356 timeservers[i] = g_strdup(list->data);
357 timeservers[ns_entries] = NULL;
360 if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
361 if (dhcp->nameservers) {
362 for (i = 0; dhcp->nameservers[i]; i++) {
363 __connman_service_nameserver_remove(service,
364 dhcp->nameservers[i], false);
366 g_strfreev(dhcp->nameservers);
369 dhcp->nameservers = nameservers;
371 for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
372 __connman_service_nameserver_append(service,
373 dhcp->nameservers[i], false);
376 g_strfreev(nameservers);
379 if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
380 if (dhcp->timeservers) {
381 for (i = 0; dhcp->timeservers[i]; i++) {
382 __connman_service_timeserver_remove(service,
383 dhcp->timeservers[i]);
385 g_strfreev(dhcp->timeservers);
388 dhcp->timeservers = timeservers;
390 for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
391 __connman_service_timeserver_append(service,
392 dhcp->timeservers[i]);
395 g_strfreev(timeservers);
398 if (g_strcmp0(pac, dhcp->pac) != 0) {
402 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
406 if (connman_setting_get_bool("Enable6to4"))
407 __connman_6to4_probe(service);
412 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
414 struct connman_dhcp *dhcp = user_data;
415 GList *option = NULL;
416 char *address, *netmask = NULL, *gateway = NULL;
417 const char *c_address, *c_gateway;
418 unsigned char prefixlen, c_prefixlen;
421 DBG("Lease available");
423 if (dhcp->ipv4ll_client) {
424 ipv4ll_stop_client(dhcp);
425 dhcp_invalidate(dhcp, false);
428 c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
429 c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
430 c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
432 address = g_dhcp_client_get_address(dhcp_client);
434 __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
435 DBG("last address %s", address);
437 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
439 netmask = g_strdup(option->data);
441 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
443 gateway = g_strdup(option->data);
445 prefixlen = connman_ipaddress_calc_netmask_len(netmask);
446 if (prefixlen == 255)
447 connman_warn("netmask: %s is invalid", netmask);
449 DBG("c_address %s", c_address);
451 if (g_strcmp0(address, c_address))
453 else if (g_strcmp0(gateway, c_gateway))
455 else if (prefixlen != c_prefixlen)
460 __connman_ipconfig_set_method(dhcp->ipconfig,
461 CONNMAN_IPCONFIG_METHOD_DHCP);
463 __connman_ipconfig_set_local(dhcp->ipconfig, address);
464 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
465 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
468 if (!apply_lease_available_on_network(dhcp_client, dhcp))
480 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
482 struct connman_dhcp *dhcp = user_data;
483 char *address, *netmask;
484 unsigned char prefixlen;
486 DBG("IPV4LL available");
488 address = g_dhcp_client_get_address(ipv4ll_client);
489 netmask = g_dhcp_client_get_netmask(ipv4ll_client);
491 prefixlen = connman_ipaddress_calc_netmask_len(netmask);
493 __connman_ipconfig_set_method(dhcp->ipconfig,
494 CONNMAN_IPCONFIG_METHOD_DHCP);
495 __connman_ipconfig_set_local(dhcp->ipconfig, address);
496 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
497 __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
505 static int dhcp_initialize(struct connman_dhcp *dhcp)
507 GDHCPClient *dhcp_client;
508 GDHCPClientError error;
511 DBG("dhcp %p", dhcp);
513 index = __connman_ipconfig_get_index(dhcp->ipconfig);
515 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
516 if (error != G_DHCP_CLIENT_ERROR_NONE)
517 #if defined TIZEN_EXT
519 DBG("failed g_dhcp_client_new(%d), index(%d)", error, index);
522 #if defined TIZEN_EXT
526 #if !defined TIZEN_EXT
527 if (getenv("CONNMAN_DHCP_DEBUG")) {
529 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
531 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
532 dhcp->dhcp_debug_prefix);
533 #if !defined TIZEN_EXT
537 g_dhcp_client_set_id(dhcp_client);
540 struct connman_service *service;
541 const char *hostname;
543 service = connman_service_lookup_from_network(dhcp->network);
545 hostname = __connman_service_get_hostname(service);
547 hostname = connman_utsname_get_hostname();
550 g_dhcp_client_set_send(dhcp_client,
551 G_DHCP_HOST_NAME, hostname);
553 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
554 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
555 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
556 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
557 g_dhcp_client_set_request(dhcp_client, 252);
560 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
561 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
563 g_dhcp_client_register_event(dhcp_client,
564 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
565 lease_available_cb, dhcp);
567 g_dhcp_client_register_event(dhcp_client,
568 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
570 g_dhcp_client_register_event(dhcp_client,
571 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
573 dhcp->dhcp_client = dhcp_client;
578 static int dhcp_release(struct connman_dhcp *dhcp)
580 DBG("dhcp %p", dhcp);
582 if (dhcp->timeout > 0) {
583 g_source_remove(dhcp->timeout);
587 if (dhcp->dhcp_client) {
588 g_dhcp_client_stop(dhcp->dhcp_client);
589 g_dhcp_client_unref(dhcp->dhcp_client);
592 dhcp->dhcp_client = NULL;
594 g_free(dhcp->dhcp_debug_prefix);
595 dhcp->dhcp_debug_prefix = NULL;
597 ipv4ll_stop_client(dhcp);
602 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
604 struct connman_dhcp *dhcp;
606 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
610 return g_dhcp_client_get_server_address(dhcp->dhcp_client);
613 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
614 struct connman_network *network, dhcp_cb callback,
617 const char *last_addr = NULL;
618 struct connman_dhcp *dhcp;
624 struct connman_service *service;
626 service = connman_service_lookup_from_network(network);
631 last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
633 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
636 dhcp = g_try_new0(struct connman_dhcp, 1);
640 dhcp->ipconfig = ipconfig;
641 __connman_ipconfig_ref(ipconfig);
644 dhcp->network = network;
645 connman_network_ref(network);
648 err = dhcp_initialize(dhcp);
652 connman_network_unref(network);
657 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
660 dhcp->callback = callback;
661 dhcp->user_data = user_data;
663 return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
666 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
668 struct connman_dhcp *dhcp;
670 DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
675 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
677 g_hash_table_remove(ipconfig_table, ipconfig);
678 __connman_ipconfig_unref(ipconfig);
680 connman_network_unref(dhcp->network);
682 dhcp_invalidate(dhcp, false);
687 int __connman_dhcp_init(void)
691 ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
697 void __connman_dhcp_cleanup(void)
701 g_hash_table_destroy(ipconfig_table);
702 ipconfig_table = NULL;
704 dhcp_cleanup_random();