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_network *network;
51 GDHCPClient *ipv4ll_client;
52 GDHCPClient *dhcp_client;
53 char *ipv4ll_debug_prefix;
54 char *dhcp_debug_prefix;
57 static GHashTable *network_table;
58 static bool ipv4ll_running;
60 static void dhcp_free(struct connman_dhcp *dhcp)
62 g_strfreev(dhcp->nameservers);
63 g_strfreev(dhcp->timeservers);
66 dhcp->nameservers = NULL;
67 dhcp->timeservers = NULL;
74 * dhcp_invalidate: Invalidate an existing DHCP lease
75 * @dhcp: pointer to the DHCP lease to invalidate.
76 * @callback: flag indicating whether or not to invoke the client callback
79 * Invalidates an existing DHCP lease, optionally invoking the client
80 * callback. The caller may wish to avoid the client callback invocation
81 * when the invocation of that callback might otherwise unnecessarily upset
82 * service state due to the IP configuration change implied by this
85 static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
87 struct connman_service *service;
88 struct connman_ipconfig *ipconfig;
91 DBG("dhcp %p callback %u", dhcp, callback);
96 service = connman_service_lookup_from_network(dhcp->network);
100 ipconfig = __connman_service_get_ip4config(service);
104 __connman_6to4_remove(ipconfig);
106 __connman_service_set_domainname(service, NULL);
107 __connman_service_set_pac(service, NULL);
109 if (dhcp->timeservers) {
110 for (i = 0; dhcp->timeservers[i]; i++) {
111 __connman_service_timeserver_remove(service,
112 dhcp->timeservers[i]);
116 if (dhcp->nameservers) {
117 for (i = 0; dhcp->nameservers[i]; i++) {
118 __connman_service_nameserver_remove(service,
119 dhcp->nameservers[i], false);
123 __connman_ipconfig_set_dhcp_address(ipconfig,
124 __connman_ipconfig_get_local(ipconfig));
125 DBG("last address %s", __connman_ipconfig_get_dhcp_address(ipconfig));
127 __connman_ipconfig_address_remove(ipconfig);
129 __connman_ipconfig_set_local(ipconfig, NULL);
130 __connman_ipconfig_set_broadcast(ipconfig, NULL);
131 __connman_ipconfig_set_gateway(ipconfig, NULL);
132 __connman_ipconfig_set_prefixlen(ipconfig, 0);
134 if (dhcp->callback && callback)
135 dhcp->callback(dhcp->network, false, NULL);
138 static void dhcp_valid(struct connman_dhcp *dhcp)
141 dhcp->callback(dhcp->network, true, NULL);
144 static void dhcp_debug(const char *str, void *data)
146 connman_info("%s: %s", (const char *) data, str);
149 static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
151 if (!dhcp->ipv4ll_client)
154 g_dhcp_client_stop(dhcp->ipv4ll_client);
155 g_dhcp_client_unref(dhcp->ipv4ll_client);
156 dhcp->ipv4ll_client = NULL;
157 ipv4ll_running = false;
159 g_free(dhcp->ipv4ll_debug_prefix);
160 dhcp->ipv4ll_debug_prefix = NULL;
163 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
164 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);
166 static int ipv4ll_start_client(struct connman_dhcp *dhcp)
168 GDHCPClient *ipv4ll_client;
169 GDHCPClientError error;
170 const char *hostname;
174 if (dhcp->ipv4ll_client)
177 index = connman_network_get_index(dhcp->network);
179 ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
180 if (error != G_DHCP_CLIENT_ERROR_NONE)
183 if (getenv("CONNMAN_DHCP_DEBUG")) {
184 dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
186 g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
187 dhcp->ipv4ll_debug_prefix);
190 g_dhcp_client_set_id(ipv4ll_client);
192 hostname = connman_utsname_get_hostname();
194 g_dhcp_client_set_send(ipv4ll_client, G_DHCP_HOST_NAME,
197 g_dhcp_client_register_event(ipv4ll_client,
198 G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
200 g_dhcp_client_register_event(ipv4ll_client,
201 G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
202 ipv4ll_available_cb, dhcp);
204 dhcp->ipv4ll_client = ipv4ll_client;
206 err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
208 ipv4ll_stop_client(dhcp);
212 ipv4ll_running = true;
216 static gboolean dhcp_retry_cb(gpointer user_data)
218 struct connman_dhcp *dhcp = user_data;
219 struct connman_service *service;
220 struct connman_ipconfig *ipconfig;
224 service = connman_service_lookup_from_network(dhcp->network);
225 ipconfig = __connman_service_get_ip4config(service);
227 g_dhcp_client_start(dhcp->dhcp_client,
228 __connman_ipconfig_get_dhcp_address(ipconfig));
233 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
235 struct connman_dhcp *dhcp = user_data;
238 DBG("No lease available ipv4ll %d client %p", ipv4ll_running,
239 dhcp->ipv4ll_client);
241 dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
247 err = ipv4ll_start_client(dhcp);
249 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
251 /* Only notify upper layer if we have a problem */
252 dhcp_invalidate(dhcp, !ipv4ll_running);
255 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
257 struct connman_dhcp *dhcp = user_data;
261 /* Upper layer will decide what to do, e.g. nothing or retry. */
262 dhcp_invalidate(dhcp, true);
265 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
267 struct connman_dhcp *dhcp = user_data;
271 ipv4ll_stop_client(dhcp);
274 * Since we lost our IPv4LL configuration we might as notify
277 dhcp_invalidate(dhcp, true);
280 static bool compare_string_arrays(char **array_a, char **array_b)
284 if (!array_a || !array_b)
287 if (g_strv_length(array_a) != g_strv_length(array_b))
290 for (i = 0; array_a[i] &&
292 if (g_strcmp0(array_a[i], array_b[i]) != 0)
299 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
301 struct connman_dhcp *dhcp = user_data;
302 GList *list, *option = NULL;
303 char *address, *netmask = NULL, *gateway = NULL;
304 const char *c_address, *c_gateway;
305 char **nameservers, **timeservers, *pac = NULL;
307 struct connman_ipconfig *ipconfig;
308 struct connman_service *service;
309 unsigned char prefixlen, c_prefixlen;
313 DBG("Lease available");
315 if (dhcp->ipv4ll_client) {
316 ipv4ll_stop_client(dhcp);
317 dhcp_invalidate(dhcp, false);
320 service = connman_service_lookup_from_network(dhcp->network);
322 connman_error("Can not lookup service");
326 ipconfig = __connman_service_get_ip4config(service);
328 connman_error("Could not lookup ipconfig");
332 c_address = __connman_ipconfig_get_local(ipconfig);
333 c_gateway = __connman_ipconfig_get_gateway(ipconfig);
334 c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
336 address = g_dhcp_client_get_address(dhcp_client);
338 __connman_ipconfig_set_dhcp_address(ipconfig, address);
339 DBG("last address %s", address);
341 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
343 netmask = g_strdup(option->data);
345 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
347 gateway = g_strdup(option->data);
349 prefixlen = __connman_ipaddress_netmask_prefix_len(netmask);
350 if (prefixlen == 255)
351 connman_warn("netmask: %s is invalid", netmask);
353 DBG("c_address %s", c_address);
355 if (address && c_address && g_strcmp0(address, c_address) != 0)
357 else if (gateway && c_gateway && g_strcmp0(gateway, c_gateway) != 0)
359 else if (prefixlen != c_prefixlen)
361 else if (!c_address || !c_gateway)
366 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
367 ns_entries = g_list_length(option);
368 nameservers = g_try_new0(char *, ns_entries + 1);
370 for (i = 0, list = option; list; list = list->next, i++)
371 nameservers[i] = g_strdup(list->data);
372 nameservers[ns_entries] = NULL;
375 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
377 __connman_service_set_domainname(service, option->data);
379 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
381 __connman_service_set_hostname(service, option->data);
383 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
384 ns_entries = g_list_length(option);
385 timeservers = g_try_new0(char *, ns_entries + 1);
387 for (i = 0, list = option; list; list = list->next, i++)
388 timeservers[i] = g_strdup(list->data);
389 timeservers[ns_entries] = NULL;
392 option = g_dhcp_client_get_option(dhcp_client, 252);
394 pac = g_strdup(option->data);
396 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
399 __connman_ipconfig_set_local(ipconfig, address);
400 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
401 __connman_ipconfig_set_gateway(ipconfig, gateway);
404 if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
405 if (dhcp->nameservers) {
406 for (i = 0; dhcp->nameservers[i]; i++) {
407 __connman_service_nameserver_remove(service,
408 dhcp->nameservers[i], false);
410 g_strfreev(dhcp->nameservers);
413 dhcp->nameservers = nameservers;
415 for (i = 0; dhcp->nameservers &&
416 dhcp->nameservers[i]; i++) {
417 __connman_service_nameserver_append(service,
418 dhcp->nameservers[i], false);
421 g_strfreev(nameservers);
424 if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
425 if (dhcp->timeservers) {
426 for (i = 0; dhcp->timeservers[i]; i++) {
427 __connman_service_timeserver_remove(service,
428 dhcp->timeservers[i]);
430 g_strfreev(dhcp->timeservers);
433 dhcp->timeservers = timeservers;
435 for (i = 0; dhcp->timeservers &&
436 dhcp->timeservers[i]; i++) {
437 __connman_service_timeserver_append(service,
438 dhcp->timeservers[i]);
441 g_strfreev(timeservers);
444 if (g_strcmp0(pac, dhcp->pac) != 0) {
448 __connman_service_set_pac(service, dhcp->pac);
454 __connman_6to4_probe(service);
461 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
463 struct connman_dhcp *dhcp = user_data;
464 char *address, *netmask;
465 struct connman_service *service;
466 struct connman_ipconfig *ipconfig;
467 unsigned char prefixlen;
469 DBG("IPV4LL available");
471 service = connman_service_lookup_from_network(dhcp->network);
475 ipconfig = __connman_service_get_ip4config(service);
479 address = g_dhcp_client_get_address(ipv4ll_client);
480 netmask = g_dhcp_client_get_netmask(ipv4ll_client);
482 prefixlen = __connman_ipaddress_netmask_prefix_len(netmask);
484 __connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
485 __connman_ipconfig_set_local(ipconfig, address);
486 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
487 __connman_ipconfig_set_gateway(ipconfig, NULL);
495 static int dhcp_initialize(struct connman_dhcp *dhcp)
497 struct connman_service *service;
498 GDHCPClient *dhcp_client;
499 GDHCPClientError error;
500 const char *hostname;
503 DBG("dhcp %p", dhcp);
505 index = connman_network_get_index(dhcp->network);
507 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
508 if (error != G_DHCP_CLIENT_ERROR_NONE)
511 if (getenv("CONNMAN_DHCP_DEBUG")) {
512 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
514 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
515 dhcp->dhcp_debug_prefix);
518 g_dhcp_client_set_id(dhcp_client);
520 service = connman_service_lookup_from_network(dhcp->network);
522 hostname = __connman_service_get_hostname(service);
524 hostname = connman_utsname_get_hostname();
527 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
529 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
530 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
531 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
532 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
533 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
534 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
535 g_dhcp_client_set_request(dhcp_client, 252);
537 g_dhcp_client_register_event(dhcp_client,
538 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
539 lease_available_cb, dhcp);
541 g_dhcp_client_register_event(dhcp_client,
542 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
544 g_dhcp_client_register_event(dhcp_client,
545 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
547 dhcp->dhcp_client = dhcp_client;
552 static int dhcp_release(struct connman_dhcp *dhcp)
554 DBG("dhcp %p", dhcp);
556 if (dhcp->timeout > 0)
557 g_source_remove(dhcp->timeout);
559 if (dhcp->dhcp_client) {
560 g_dhcp_client_stop(dhcp->dhcp_client);
561 g_dhcp_client_unref(dhcp->dhcp_client);
564 dhcp->dhcp_client = NULL;
566 g_free(dhcp->dhcp_debug_prefix);
567 dhcp->dhcp_debug_prefix = NULL;
569 ipv4ll_stop_client(dhcp);
574 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
576 struct connman_service *service;
577 struct connman_ipconfig *ipconfig;
578 const char *last_addr = NULL;
579 struct connman_dhcp *dhcp;
583 service = connman_service_lookup_from_network(network);
587 ipconfig = __connman_service_get_ip4config(service);
589 last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
591 dhcp = g_hash_table_lookup(network_table, network);
594 dhcp = g_try_new0(struct connman_dhcp, 1);
598 dhcp->network = network;
599 connman_network_ref(network);
601 g_hash_table_insert(network_table, network, dhcp);
603 dhcp_initialize(dhcp);
606 dhcp->callback = callback;
608 return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
611 void __connman_dhcp_stop(struct connman_network *network)
613 struct connman_dhcp *dhcp;
615 DBG("network_table %p network %p", network_table, network);
620 dhcp = g_hash_table_lookup(network_table, network);
622 g_hash_table_remove(network_table, network);
623 connman_network_unref(network);
625 dhcp_invalidate(dhcp, false);
630 int __connman_dhcp_init(void)
634 network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
640 void __connman_dhcp_cleanup(void)
644 g_hash_table_destroy(network_table);
645 network_table = NULL;