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
29 #include <net/ethernet.h>
32 #define IPV6_MIN_MTU 1280
35 #include <connman/ipconfig.h>
36 #include <include/setting.h>
38 #include <gdhcp/gdhcp.h>
44 #define RATE_LIMIT_INTERVAL 60 /* delay between successive attempts */
47 struct connman_ipconfig *ipconfig;
48 struct connman_network *network;
58 GDHCPClient *ipv4ll_client;
59 GDHCPClient *dhcp_client;
60 char *ipv4ll_debug_prefix;
61 char *dhcp_debug_prefix;
66 static GHashTable *ipconfig_table;
68 static void dhcp_free(struct connman_dhcp *dhcp)
70 g_strfreev(dhcp->nameservers);
71 g_strfreev(dhcp->timeservers);
74 dhcp->nameservers = NULL;
75 dhcp->timeservers = NULL;
81 static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
83 if (!dhcp->ipv4ll_client)
86 g_dhcp_client_stop(dhcp->ipv4ll_client);
87 g_dhcp_client_unref(dhcp->ipv4ll_client);
88 dhcp->ipv4ll_client = NULL;
89 dhcp->ipv4ll_running = false;
91 g_free(dhcp->ipv4ll_debug_prefix);
92 dhcp->ipv4ll_debug_prefix = NULL;
95 static bool apply_dhcp_invalidate_on_network(struct connman_dhcp *dhcp)
97 struct connman_service *service;
103 service = connman_service_lookup_from_network(dhcp->network);
105 connman_error("Can not lookup service");
109 __connman_service_set_domainname(service, NULL);
110 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig, NULL);
112 if (dhcp->timeservers) {
113 for (i = 0; dhcp->timeservers[i]; i++) {
114 __connman_service_timeserver_remove(service,
115 dhcp->timeservers[i]);
117 g_strfreev(dhcp->timeservers);
118 dhcp->timeservers = NULL;
120 if (dhcp->nameservers) {
121 for (i = 0; dhcp->nameservers[i]; i++) {
122 __connman_service_nameserver_remove(service,
123 dhcp->nameservers[i], false);
125 g_strfreev(dhcp->nameservers);
126 dhcp->nameservers = NULL;
133 * dhcp_invalidate: Invalidate an existing DHCP lease
134 * @dhcp: pointer to the DHCP lease to invalidate.
135 * @callback: flag indicating whether or not to invoke the client callback
138 * Invalidates an existing DHCP lease, optionally invoking the client
139 * callback. The caller may wish to avoid the client callback invocation
140 * when the invocation of that callback might otherwise unnecessarily upset
141 * service state due to the IP configuration change implied by this
144 static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
146 DBG("dhcp %p callback %u", dhcp, callback);
151 __connman_6to4_remove(dhcp->ipconfig);
153 if (!apply_dhcp_invalidate_on_network(dhcp))
156 __connman_ipconfig_set_dhcp_address(dhcp->ipconfig,
157 __connman_ipconfig_get_local(dhcp->ipconfig));
158 DBG("last address %s",
159 __connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
161 __connman_ipconfig_address_remove(dhcp->ipconfig);
163 __connman_ipconfig_set_local(dhcp->ipconfig, NULL);
164 __connman_ipconfig_set_broadcast(dhcp->ipconfig, NULL);
165 __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
166 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, 0);
168 if (dhcp->callback && callback)
169 dhcp->callback(dhcp->ipconfig, dhcp->network,
170 false, dhcp->user_data);
173 static void dhcp_valid(struct connman_dhcp *dhcp)
176 dhcp->callback(dhcp->ipconfig, dhcp->network,
177 true, dhcp->user_data);
180 static void dhcp_debug(const char *str, void *data)
182 connman_info("%s: %s", (const char *) data, str);
185 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
186 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);
188 static int ipv4ll_start_client(struct connman_dhcp *dhcp)
190 GDHCPClient *ipv4ll_client;
191 GDHCPClientError error;
192 const char *hostname;
196 if (dhcp->ipv4ll_client)
199 index = __connman_ipconfig_get_index(dhcp->ipconfig);
201 ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
202 if (error != G_DHCP_CLIENT_ERROR_NONE)
205 if (getenv("CONNMAN_DHCP_DEBUG")) {
206 dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
208 g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
209 dhcp->ipv4ll_debug_prefix);
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 dhcp->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", dhcp->ipv4ll_running,
258 dhcp->ipv4ll_client);
260 if (dhcp->timeout > 0)
261 g_source_remove(dhcp->timeout);
263 dhcp->timeout = g_timeout_add_seconds(RATE_LIMIT_INTERVAL,
266 if (dhcp->ipv4ll_running)
269 err = ipv4ll_start_client(dhcp);
271 DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
273 /* Only notify upper layer if we have a problem */
274 dhcp_invalidate(dhcp, !dhcp->ipv4ll_running);
277 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
279 struct connman_dhcp *dhcp = user_data;
283 /* Upper layer will decide what to do, e.g. nothing or retry. */
284 dhcp_invalidate(dhcp, true);
287 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
289 struct connman_dhcp *dhcp = user_data;
293 ipv4ll_stop_client(dhcp);
296 * Since we lost our IPv4LL configuration we might as notify
299 dhcp_invalidate(dhcp, true);
302 static bool compare_string_arrays(char **array_a, char **array_b)
306 if (!array_a || !array_b)
309 if (g_strv_length(array_a) != g_strv_length(array_b))
312 for (i = 0; array_a[i] &&
314 if (g_strcmp0(array_a[i], array_b[i]) != 0)
321 static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
322 struct connman_dhcp *dhcp)
324 char **nameservers, **timeservers, *pac = NULL;
325 struct connman_service *service;
326 GList *list, *option = NULL;
333 service = connman_service_lookup_from_network(dhcp->network);
335 connman_error("Can not lookup service");
339 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_MTU);
340 if (option && option->data) {
343 mtu = atoi(option->data);
345 if (mtu >= IPV6_MIN_MTU && mtu <= ETH_DATA_LEN) {
346 index = __connman_ipconfig_get_index(dhcp->ipconfig);
347 err = connman_inet_set_mtu(index, mtu);
349 DBG("MTU %d index %d err %d", mtu, index, err);
353 option = g_dhcp_client_get_option(dhcp_client, 252);
355 pac = g_strdup(option->data);
357 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
358 ns_entries = g_list_length(option);
359 nameservers = g_try_new0(char *, ns_entries + 1);
361 for (i = 0, list = option;list; list = list->next, i++)
362 nameservers[i] = g_strdup(list->data);
363 nameservers[ns_entries] = NULL;
366 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
368 __connman_service_set_domainname(service, option->data);
370 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
372 __connman_service_set_hostname(service, option->data);
374 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
375 ns_entries = g_list_length(option);
376 timeservers = g_try_new0(char *, ns_entries + 1);
378 for (i = 0, list = option; list; list = list->next, i++)
379 timeservers[i] = g_strdup(list->data);
380 timeservers[ns_entries] = NULL;
383 if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
384 if (dhcp->nameservers) {
385 for (i = 0; dhcp->nameservers[i]; i++) {
386 __connman_service_nameserver_remove(service,
387 dhcp->nameservers[i], false);
389 g_strfreev(dhcp->nameservers);
392 dhcp->nameservers = nameservers;
394 for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
395 __connman_service_nameserver_append(service,
396 dhcp->nameservers[i], false);
399 g_strfreev(nameservers);
402 if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
403 if (dhcp->timeservers) {
404 for (i = 0; dhcp->timeservers[i]; i++) {
405 __connman_service_timeserver_remove(service,
406 dhcp->timeservers[i]);
408 g_strfreev(dhcp->timeservers);
411 dhcp->timeservers = timeservers;
413 for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
414 __connman_service_timeserver_append(service,
415 dhcp->timeservers[i]);
418 g_strfreev(timeservers);
421 if (g_strcmp0(pac, dhcp->pac) != 0) {
425 __connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
429 if (connman_setting_get_bool("Enable6to4"))
430 __connman_6to4_probe(service);
435 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
437 struct connman_dhcp *dhcp = user_data;
438 GList *option = NULL;
439 enum connman_ipconfig_method old_method;
440 char *address, *netmask = NULL, *gateway = NULL;
441 const char *c_address, *c_gateway;
442 unsigned char prefixlen, c_prefixlen;
443 bool ip_change = false;
445 DBG("Lease available");
447 if (dhcp->ipv4ll_client) {
448 ipv4ll_stop_client(dhcp);
449 dhcp_invalidate(dhcp, false);
452 c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
453 c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
454 c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);
456 address = g_dhcp_client_get_address(dhcp_client);
458 __connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
459 DBG("last address %s", address);
461 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
463 netmask = g_strdup(option->data);
465 option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
467 gateway = g_strdup(option->data);
469 prefixlen = connman_ipaddress_calc_netmask_len(netmask);
470 if (prefixlen == 255)
471 connman_warn("netmask: %s is invalid", netmask);
473 DBG("c_address %s", c_address);
475 if (g_strcmp0(address, c_address)) {
478 /* Remove old ip address */
479 __connman_ipconfig_address_remove(dhcp->ipconfig);
482 if (g_strcmp0(gateway, c_gateway)) {
485 /* Remove gateway ip address */
486 __connman_ipconfig_gateway_remove(dhcp->ipconfig);
488 } else if (prefixlen != c_prefixlen)
491 old_method = __connman_ipconfig_get_method(dhcp->ipconfig);
492 __connman_ipconfig_set_method(dhcp->ipconfig,
493 CONNMAN_IPCONFIG_METHOD_DHCP);
496 * Notify IPv4.Configuration's method moved back to DHCP.
498 * This is the case ConnMan initially set an address by using
499 * IPv4LL because DHCP failed but now we got an address from DHCP.
501 if (old_method == CONNMAN_IPCONFIG_METHOD_AUTO) {
502 struct connman_service *service =
503 connman_service_lookup_from_network(dhcp->network);
506 __connman_service_notify_ipv4_configuration(service);
510 __connman_ipconfig_set_local(dhcp->ipconfig, address);
511 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
512 __connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
515 if (!apply_lease_available_on_network(dhcp_client, dhcp))
527 static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
529 struct connman_dhcp *dhcp = user_data;
530 enum connman_ipconfig_method old_method;
531 char *address, *netmask;
532 unsigned char prefixlen;
534 DBG("IPV4LL available");
536 address = g_dhcp_client_get_address(ipv4ll_client);
537 netmask = g_dhcp_client_get_netmask(ipv4ll_client);
539 prefixlen = connman_ipaddress_calc_netmask_len(netmask);
541 old_method = __connman_ipconfig_get_method(dhcp->ipconfig);
542 __connman_ipconfig_set_method(dhcp->ipconfig,
543 CONNMAN_IPCONFIG_METHOD_AUTO);
546 * Notify IPv4.Configuration's method is AUTO now.
548 * This is the case DHCP failed thus ConnMan used IPv4LL to get an
549 * address. Set IPv4.Configuration method to AUTO allows user to
550 * ask for a DHCP address by setting the method again to DHCP.
552 if (old_method == CONNMAN_IPCONFIG_METHOD_DHCP) {
553 struct connman_service *service =
554 connman_service_lookup_from_network(dhcp->network);
557 __connman_service_notify_ipv4_configuration(service);
560 __connman_ipconfig_set_local(dhcp->ipconfig, address);
561 __connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
562 __connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
570 static int dhcp_initialize(struct connman_dhcp *dhcp)
572 GDHCPClient *dhcp_client;
573 GDHCPClientError error;
575 const char *vendor_class_id;
577 DBG("dhcp %p", dhcp);
579 index = __connman_ipconfig_get_index(dhcp->ipconfig);
581 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
582 if (error != G_DHCP_CLIENT_ERROR_NONE)
585 if (getenv("CONNMAN_DHCP_DEBUG")) {
586 dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
588 g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
589 dhcp->dhcp_debug_prefix);
592 g_dhcp_client_set_id(dhcp_client);
595 struct connman_service *service;
596 const char *hostname;
598 service = connman_service_lookup_from_network(dhcp->network);
600 hostname = __connman_service_get_hostname(service);
602 hostname = connman_utsname_get_hostname();
605 g_dhcp_client_set_send(dhcp_client,
606 G_DHCP_HOST_NAME, hostname);
608 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
609 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
610 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
611 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
612 g_dhcp_client_set_request(dhcp_client, 252);
613 g_dhcp_client_set_request(dhcp_client, G_DHCP_MTU);
616 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
617 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
619 vendor_class_id = connman_option_get_string("VendorClassID");
621 g_dhcp_client_set_send(dhcp_client, G_DHCP_VENDOR_CLASS_ID,
624 g_dhcp_client_register_event(dhcp_client,
625 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
626 lease_available_cb, dhcp);
628 g_dhcp_client_register_event(dhcp_client,
629 G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
631 g_dhcp_client_register_event(dhcp_client,
632 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
634 dhcp->dhcp_client = dhcp_client;
639 static int dhcp_release(struct connman_dhcp *dhcp)
641 DBG("dhcp %p", dhcp);
643 if (dhcp->timeout > 0) {
644 g_source_remove(dhcp->timeout);
648 if (dhcp->dhcp_client) {
649 g_dhcp_client_stop(dhcp->dhcp_client);
650 g_dhcp_client_unref(dhcp->dhcp_client);
653 dhcp->dhcp_client = NULL;
655 g_free(dhcp->dhcp_debug_prefix);
656 dhcp->dhcp_debug_prefix = NULL;
658 ipv4ll_stop_client(dhcp);
663 char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
665 struct connman_dhcp *dhcp;
667 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
671 return g_dhcp_client_get_server_address(dhcp->dhcp_client);
674 int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
675 struct connman_network *network, dhcp_cb callback,
678 const char *last_addr = NULL;
679 struct connman_dhcp *dhcp;
685 struct connman_service *service;
687 service = connman_service_lookup_from_network(network);
692 last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
694 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
697 dhcp = g_try_new0(struct connman_dhcp, 1);
701 dhcp->ipconfig = ipconfig;
702 __connman_ipconfig_ref(ipconfig);
705 dhcp->network = network;
706 connman_network_ref(network);
709 err = dhcp_initialize(dhcp);
713 connman_network_unref(network);
718 g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
721 dhcp->callback = callback;
722 dhcp->user_data = user_data;
724 return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
727 void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
729 struct connman_dhcp *dhcp;
731 DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
736 dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
738 g_hash_table_remove(ipconfig_table, ipconfig);
739 __connman_ipconfig_unref(ipconfig);
741 connman_network_unref(dhcp->network);
743 dhcp_invalidate(dhcp, false);
748 int __connman_dhcp_init(void)
752 ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
758 void __connman_dhcp_cleanup(void)
762 g_hash_table_destroy(ipconfig_table);
763 ipconfig_table = NULL;
765 dhcp_cleanup_random();