dhcp.c 18 KB
Newer Older
1 2 3 4
/*
 *
 *  Connection Manager
 *
5
 *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
29 30 31 32 33
#include <net/ethernet.h>

#ifndef IPV6_MIN_MTU
#define IPV6_MIN_MTU 1280
#endif
34 35 36 37 38 39 40 41 42

#include <connman/ipconfig.h>
#include <include/setting.h>

#include <gdhcp/gdhcp.h>

#include <glib.h>

#include "connman.h"
43
#include "wakeup_timer.h"
44

45 46
#define RATE_LIMIT_INTERVAL	60	/* delay between successive attempts */

47
struct connman_dhcp {
48
	struct connman_ipconfig *ipconfig;
49 50
	struct connman_network *network;
	dhcp_cb callback;
51
	gpointer user_data;
52 53 54 55 56

	char **nameservers;
	char **timeservers;
	char *pac;

57 58 59
	unsigned int timeout;

	GDHCPClient *ipv4ll_client;
60
	GDHCPClient *dhcp_client;
61 62
	char *ipv4ll_debug_prefix;
	char *dhcp_debug_prefix;
63 64
};

65
static GHashTable *ipconfig_table;
66
static bool ipv4ll_running;
67 68 69 70 71 72 73 74 75 76

static void dhcp_free(struct connman_dhcp *dhcp)
{
	g_strfreev(dhcp->nameservers);
	g_strfreev(dhcp->timeservers);
	g_free(dhcp->pac);

	dhcp->nameservers = NULL;
	dhcp->timeservers = NULL;
	dhcp->pac = NULL;
77 78

	g_free(dhcp);
79 80
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94
static void ipv4ll_stop_client(struct connman_dhcp *dhcp)
{
	if (!dhcp->ipv4ll_client)
		return;

	g_dhcp_client_stop(dhcp->ipv4ll_client);
	g_dhcp_client_unref(dhcp->ipv4ll_client);
	dhcp->ipv4ll_client = NULL;
	ipv4ll_running = false;

	g_free(dhcp->ipv4ll_debug_prefix);
	dhcp->ipv4ll_debug_prefix = NULL;
}

95
static bool apply_dhcp_invalidate_on_network(struct connman_dhcp *dhcp)
96 97 98 99
{
	struct connman_service *service;
	int i;

100 101
	if (!dhcp->network)
		return true;
102 103

	service = connman_service_lookup_from_network(dhcp->network);
104 105 106 107
	if (!service) {
		connman_error("Can not lookup service");
		return false;
	}
108 109

	__connman_service_set_domainname(service, NULL);
110
	__connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig, NULL);
111

112 113
	if (dhcp->timeservers) {
		for (i = 0; dhcp->timeservers[i]; i++) {
114 115 116
			__connman_service_timeserver_remove(service,
							dhcp->timeservers[i]);
		}
117 118
		g_strfreev(dhcp->timeservers);
		dhcp->timeservers = NULL;
119
	}
120 121
	if (dhcp->nameservers) {
		for (i = 0; dhcp->nameservers[i]; i++) {
122
			__connman_service_nameserver_remove(service,
123
						dhcp->nameservers[i], false);
124
		}
125 126
		g_strfreev(dhcp->nameservers);
		dhcp->nameservers = NULL;
127 128
	}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	return true;
}

/**
 * dhcp_invalidate: Invalidate an existing DHCP lease
 * @dhcp: pointer to the DHCP lease to invalidate.
 * @callback: flag indicating whether or not to invoke the client callback
 *            if present.
 *
 * Invalidates an existing DHCP lease, optionally invoking the client
 * callback. The caller may wish to avoid the client callback invocation
 * when the invocation of that callback might otherwise unnecessarily upset
 * service state due to the IP configuration change implied by this
 * invalidation.
 */
static void dhcp_invalidate(struct connman_dhcp *dhcp, bool callback)
{
	DBG("dhcp %p callback %u", dhcp, callback);

	if (!dhcp)
		return;

	__connman_6to4_remove(dhcp->ipconfig);
152

153 154 155 156 157 158 159
	if (!apply_dhcp_invalidate_on_network(dhcp))
		return;

	__connman_ipconfig_set_dhcp_address(dhcp->ipconfig,
				__connman_ipconfig_get_local(dhcp->ipconfig));
	DBG("last address %s",
			__connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
160

161 162 163 164 165 166
	__connman_ipconfig_address_remove(dhcp->ipconfig);

	__connman_ipconfig_set_local(dhcp->ipconfig, NULL);
	__connman_ipconfig_set_broadcast(dhcp->ipconfig, NULL);
	__connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
	__connman_ipconfig_set_prefixlen(dhcp->ipconfig, 0);
167

168 169 170 171 172 173 174 175
	if (dhcp->timeout > 0) {
		g_source_remove(dhcp->timeout);
		dhcp->timeout = 0;
	}

	if (ipv4ll_running)
		ipv4ll_stop_client(dhcp);

176
	if (dhcp->callback && callback)
177 178
		dhcp->callback(dhcp->ipconfig, dhcp->network,
						false, dhcp->user_data);
179 180 181 182
}

static void dhcp_valid(struct connman_dhcp *dhcp)
{
183
	if (dhcp->callback)
184 185
		dhcp->callback(dhcp->ipconfig, dhcp->network,
						true, dhcp->user_data);
186 187 188 189
}

static void dhcp_debug(const char *str, void *data)
{
190
	DBG("%s: %s", (const char *) data, str);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data);
static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data);

static int ipv4ll_start_client(struct connman_dhcp *dhcp)
{
	GDHCPClient *ipv4ll_client;
	GDHCPClientError error;
	const char *hostname;
	int index;
	int err;

	if (dhcp->ipv4ll_client)
		return -EALREADY;

207
	index = __connman_ipconfig_get_index(dhcp->ipconfig);
208 209 210 211 212 213 214 215 216 217 218 219 220 221

	ipv4ll_client = g_dhcp_client_new(G_DHCP_IPV4LL, index, &error);
	if (error != G_DHCP_CLIENT_ERROR_NONE)
		return -EINVAL;

	if (getenv("CONNMAN_DHCP_DEBUG")) {
		dhcp->ipv4ll_debug_prefix = g_strdup_printf("IPv4LL index %d",
							index);
		g_dhcp_client_set_debug(ipv4ll_client, dhcp_debug,
					dhcp->ipv4ll_debug_prefix);
	}

	g_dhcp_client_set_id(ipv4ll_client);

222 223 224 225 226 227
	if (dhcp->network) {
		hostname = connman_utsname_get_hostname();
		if (hostname)
			g_dhcp_client_set_send(ipv4ll_client,
						G_DHCP_HOST_NAME, hostname);
	}
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

	g_dhcp_client_register_event(ipv4ll_client,
			G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);

	g_dhcp_client_register_event(ipv4ll_client,
			G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
						ipv4ll_available_cb, dhcp);

	dhcp->ipv4ll_client = ipv4ll_client;

	err = g_dhcp_client_start(dhcp->ipv4ll_client, NULL);
	if (err < 0) {
		ipv4ll_stop_client(dhcp);
		return err;
	}

	ipv4ll_running = true;
	return 0;
}

static gboolean dhcp_retry_cb(gpointer user_data)
{
	struct connman_dhcp *dhcp = user_data;

	dhcp->timeout = 0;

	g_dhcp_client_start(dhcp->dhcp_client,
255
			__connman_ipconfig_get_dhcp_address(dhcp->ipconfig));
256 257

	return FALSE;
258 259 260 261 262
}

static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
	struct connman_dhcp *dhcp = user_data;
263 264 265 266 267
	int err;

	DBG("No lease available ipv4ll %d client %p", ipv4ll_running,
		dhcp->ipv4ll_client);

268 269 270
	if (dhcp->timeout > 0)
		g_source_remove(dhcp->timeout);

271
	dhcp->timeout = connman_wakeup_timer_add_seconds(RATE_LIMIT_INTERVAL,
272
						dhcp_retry_cb,
273
						dhcp);
274 275
	if (ipv4ll_running)
		return;
276

277 278 279
	err = ipv4ll_start_client(dhcp);
	if (err < 0)
		DBG("Cannot start ipv4ll client (%d/%s)", err, strerror(-err));
280

281 282
	/* Only notify upper layer if we have a problem */
	dhcp_invalidate(dhcp, !ipv4ll_running);
283 284 285 286 287 288 289 290
}

static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
	struct connman_dhcp *dhcp = user_data;

	DBG("Lease lost");

291 292
	/* Upper layer will decide what to do, e.g. nothing or retry. */
	dhcp_invalidate(dhcp, true);
293 294 295 296 297 298 299 300
}

static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
	struct connman_dhcp *dhcp = user_data;

	DBG("Lease lost");

301
	ipv4ll_stop_client(dhcp);
302

303 304 305 306 307 308
	/*
	 * Since we lost our IPv4LL configuration we might as notify
	 * the upper layers.
	 */
	dhcp_invalidate(dhcp, true);
}
309

310
static bool compare_string_arrays(char **array_a, char **array_b)
311 312 313
{
	int i;

314 315
	if (!array_a || !array_b)
		return false;
316 317

	if (g_strv_length(array_a) != g_strv_length(array_b))
318
		return false;
319

320 321
	for (i = 0; array_a[i] &&
			     array_b[i]; i++) {
322
		if (g_strcmp0(array_a[i], array_b[i]) != 0)
323
			return false;
324 325
	}

326
	return true;
327 328
}

329 330
static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
						struct connman_dhcp *dhcp)
331 332 333
{
	char **nameservers, **timeservers, *pac = NULL;
	struct connman_service *service;
334 335
	GList *list, *option = NULL;
	int ns_entries;
336 337
	int i;

338 339 340
	if (!dhcp->network)
		return true;

341
	service = connman_service_lookup_from_network(dhcp->network);
342
	if (!service) {
343
		connman_error("Can not lookup service");
344
		return false;
345 346
	}

347 348 349 350 351 352 353 354 355 356 357 358 359 360
	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_MTU);
	if (option && option->data) {
		int mtu, index, err;

		mtu = atoi(option->data);

		if (mtu >= IPV6_MIN_MTU && mtu <= ETH_DATA_LEN) {
			index = __connman_ipconfig_get_index(dhcp->ipconfig);
			err = connman_inet_set_mtu(index, mtu);

			DBG("MTU %d index %d err %d", mtu, index, err);
		}
	}

361
	option = g_dhcp_client_get_option(dhcp_client, 252);
362
	if (option)
363
		pac = g_strdup(option->data);
364 365 366 367

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
	ns_entries = g_list_length(option);
	nameservers = g_try_new0(char *, ns_entries + 1);
368
	if (nameservers) {
369
		for (i = 0, list = option;list; list = list->next, i++)
370 371 372 373 374
			nameservers[i] = g_strdup(list->data);
		nameservers[ns_entries] = NULL;
	}

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
375 376 377 378 379 380
	if (option)
		__connman_service_set_domainname(service, option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
	if (option)
		__connman_service_set_hostname(service, option->data);
381 382 383 384

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
	ns_entries = g_list_length(option);
	timeservers = g_try_new0(char *, ns_entries + 1);
385
	if (timeservers) {
386 387 388 389 390
		for (i = 0, list = option; list; list = list->next, i++)
			timeservers[i] = g_strdup(list->data);
		timeservers[ns_entries] = NULL;
	}

391 392 393
	if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
		if (dhcp->nameservers) {
			for (i = 0; dhcp->nameservers[i]; i++) {
394
				__connman_service_nameserver_remove(service,
395
						dhcp->nameservers[i], false);
396 397 398 399 400 401
			}
			g_strfreev(dhcp->nameservers);
		}

		dhcp->nameservers = nameservers;

402
		for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
403
			__connman_service_nameserver_append(service,
404
						dhcp->nameservers[i], false);
405 406 407 408 409
		}
	} else {
		g_strfreev(nameservers);
	}

410 411 412
	if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
		if (dhcp->timeservers) {
			for (i = 0; dhcp->timeservers[i]; i++) {
413 414 415 416 417 418 419 420
				__connman_service_timeserver_remove(service,
							dhcp->timeservers[i]);
			}
			g_strfreev(dhcp->timeservers);
		}

		dhcp->timeservers = timeservers;

421
		for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
422 423 424 425 426 427 428 429 430 431 432
			__connman_service_timeserver_append(service,
							dhcp->timeservers[i]);
		}
	} else {
		g_strfreev(timeservers);
	}

	if (g_strcmp0(pac, dhcp->pac) != 0) {
		g_free(dhcp->pac);
		dhcp->pac = pac;

433 434 435 436
		__connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
								dhcp->pac);
	}

437 438
	if (connman_setting_get_bool("Enable6to4"))
		__connman_6to4_probe(service);
439 440 441 442 443 444 445 446 447 448 449

	return true;
}

static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
	struct connman_dhcp *dhcp = user_data;
	GList *option = NULL;
	char *address, *netmask = NULL, *gateway = NULL;
	const char *c_address, *c_gateway;
	unsigned char prefixlen, c_prefixlen;
450
	bool ip_change = false;
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

	DBG("Lease available");

	if (dhcp->ipv4ll_client) {
		ipv4ll_stop_client(dhcp);
		dhcp_invalidate(dhcp, false);
	}

	c_address = __connman_ipconfig_get_local(dhcp->ipconfig);
	c_gateway = __connman_ipconfig_get_gateway(dhcp->ipconfig);
	c_prefixlen = __connman_ipconfig_get_prefixlen(dhcp->ipconfig);

	address = g_dhcp_client_get_address(dhcp_client);

	__connman_ipconfig_set_dhcp_address(dhcp->ipconfig, address);
	DBG("last address %s", address);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
	if (option)
		netmask = g_strdup(option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
	if (option)
		gateway = g_strdup(option->data);

	prefixlen = connman_ipaddress_calc_netmask_len(netmask);
	if (prefixlen == 255)
		connman_warn("netmask: %s is invalid", netmask);

	DBG("c_address %s", c_address);

482
	if (g_strcmp0(address, c_address)) {
483
		ip_change = true;
484 485 486 487 488 489
		if (c_address) {
			/* Remove old ip address */
			__connman_ipconfig_address_remove(dhcp->ipconfig);
		}
	}
	if (g_strcmp0(gateway, c_gateway)) {
490
		ip_change = true;
491 492 493 494 495
		if (c_gateway) {
			/* Remove gateway ip address */
			__connman_ipconfig_gateway_remove(dhcp->ipconfig);
		}
	} else if (prefixlen != c_prefixlen)
496 497 498 499 500 501 502 503
		ip_change = true;

	__connman_ipconfig_set_method(dhcp->ipconfig,
						CONNMAN_IPCONFIG_METHOD_DHCP);
	if (ip_change) {
		__connman_ipconfig_set_local(dhcp->ipconfig, address);
		__connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
		__connman_ipconfig_set_gateway(dhcp->ipconfig, gateway);
504 505
	}

506
	if (!apply_lease_available_on_network(dhcp_client, dhcp))
Saurav Babu's avatar
Saurav Babu committed
507
		goto done;
508

509
	if (ip_change)
510 511
		dhcp_valid(dhcp);

Saurav Babu's avatar
Saurav Babu committed
512
done:
513 514 515 516 517
	g_free(address);
	g_free(netmask);
	g_free(gateway);
}

518
static void ipv4ll_available_cb(GDHCPClient *ipv4ll_client, gpointer user_data)
519 520 521 522 523 524 525
{
	struct connman_dhcp *dhcp = user_data;
	char *address, *netmask;
	unsigned char prefixlen;

	DBG("IPV4LL available");

526 527
	address = g_dhcp_client_get_address(ipv4ll_client);
	netmask = g_dhcp_client_get_netmask(ipv4ll_client);
528

529
	prefixlen = connman_ipaddress_calc_netmask_len(netmask);
530

531 532 533 534 535
	__connman_ipconfig_set_method(dhcp->ipconfig,
						CONNMAN_IPCONFIG_METHOD_DHCP);
	__connman_ipconfig_set_local(dhcp->ipconfig, address);
	__connman_ipconfig_set_prefixlen(dhcp->ipconfig, prefixlen);
	__connman_ipconfig_set_gateway(dhcp->ipconfig, NULL);
536 537 538 539 540 541 542

	dhcp_valid(dhcp);

	g_free(address);
	g_free(netmask);
}

543
static int dhcp_initialize(struct connman_dhcp *dhcp)
544 545 546 547
{
	GDHCPClient *dhcp_client;
	GDHCPClientError error;
	int index;
548
	const char *vendor_class_id;
549 550 551

	DBG("dhcp %p", dhcp);

552
	index = __connman_ipconfig_get_index(dhcp->ipconfig);
553 554 555 556 557

	dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
	if (error != G_DHCP_CLIENT_ERROR_NONE)
		return -EINVAL;

558 559 560 561 562 563
	if (getenv("CONNMAN_DHCP_DEBUG")) {
		dhcp->dhcp_debug_prefix = g_strdup_printf("DHCP index %d",
							index);
		g_dhcp_client_set_debug(dhcp_client, dhcp_debug,
					dhcp->dhcp_debug_prefix);
	}
564 565 566

	g_dhcp_client_set_id(dhcp_client);

567 568 569
	if (dhcp->network) {
		struct connman_service *service;
		const char *hostname;
570

571 572 573 574 575
		service = connman_service_lookup_from_network(dhcp->network);

		hostname = __connman_service_get_hostname(service);
		if (!hostname)
			hostname = connman_utsname_get_hostname();
576

577 578 579 580 581 582 583 584 585
		if (hostname)
			g_dhcp_client_set_send(dhcp_client,
						G_DHCP_HOST_NAME, hostname);

		g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
		g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
		g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
		g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
		g_dhcp_client_set_request(dhcp_client, 252);
586
		g_dhcp_client_set_request(dhcp_client, G_DHCP_MTU);
587
	}
588 589

	g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
590
	g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
591

592 593 594 595 596
	vendor_class_id = connman_option_get_string("VendorClassID");
	if (vendor_class_id)
		g_dhcp_client_set_send(dhcp_client, G_DHCP_VENDOR_CLASS_ID,
					vendor_class_id);

597 598 599 600 601 602 603 604 605 606 607 608
	g_dhcp_client_register_event(dhcp_client,
			G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
						lease_available_cb, dhcp);

	g_dhcp_client_register_event(dhcp_client,
			G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);

	g_dhcp_client_register_event(dhcp_client,
			G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);

	dhcp->dhcp_client = dhcp_client;

609
	return 0;
610 611 612 613 614 615
}

static int dhcp_release(struct connman_dhcp *dhcp)
{
	DBG("dhcp %p", dhcp);

616
	if (dhcp->timeout > 0) {
617
		g_source_remove(dhcp->timeout);
618 619
		dhcp->timeout = 0;
	}
620

621 622 623 624
	if (dhcp->dhcp_client) {
		g_dhcp_client_stop(dhcp->dhcp_client);
		g_dhcp_client_unref(dhcp->dhcp_client);
	}
625 626 627

	dhcp->dhcp_client = NULL;

628 629 630 631 632
	g_free(dhcp->dhcp_debug_prefix);
	dhcp->dhcp_debug_prefix = NULL;

	ipv4ll_stop_client(dhcp);

633 634 635
	return 0;
}

636 637 638 639 640 641 642 643 644 645 646
char *__connman_dhcp_get_server_address(struct connman_ipconfig *ipconfig)
{
	struct connman_dhcp *dhcp;

	dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
	if (!dhcp)
		return NULL;

	return g_dhcp_client_get_server_address(dhcp->dhcp_client);
}

647
int __connman_dhcp_start(struct connman_ipconfig *ipconfig,
648 649
			struct connman_network *network, dhcp_cb callback,
			gpointer user_data)
650
{
651
	const char *last_addr = NULL;
652
	struct connman_dhcp *dhcp;
653
	int err;
654 655 656

	DBG("");

657 658 659 660 661 662 663
	if (network) {
		struct connman_service *service;

		service = connman_service_lookup_from_network(network);
		if (!service)
			return -EINVAL;
	}
664

665
	last_addr = __connman_ipconfig_get_dhcp_address(ipconfig);
666

667
	dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
668
	if (!dhcp) {
669

670 671 672
		dhcp = g_try_new0(struct connman_dhcp, 1);
		if (!dhcp)
			return -ENOMEM;
673

674 675 676 677 678 679 680
		dhcp->ipconfig = ipconfig;
		__connman_ipconfig_ref(ipconfig);

		if (network) {
			dhcp->network = network;
			connman_network_ref(network);
		}
681

682
		err = dhcp_initialize(dhcp);
683

684
		if (err < 0) {
685 686
			if (network)
				connman_network_unref(network);
687 688 689 690
			g_free(dhcp);
			return err;
		}

691
		g_hash_table_insert(ipconfig_table, ipconfig, dhcp);
692 693 694
	}

	dhcp->callback = callback;
695
	dhcp->user_data = user_data;
696

697
	return g_dhcp_client_start(dhcp->dhcp_client, last_addr);
698 699
}

700
void __connman_dhcp_stop(struct connman_ipconfig *ipconfig)
701
{
702 703
	struct connman_dhcp *dhcp;

704
	DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);
705

706
	if (!ipconfig_table)
707 708
		return;

709
	dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
710
	if (dhcp) {
711 712 713 714
		g_hash_table_remove(ipconfig_table, ipconfig);
		__connman_ipconfig_unref(ipconfig);
		if (dhcp->network)
			connman_network_unref(dhcp->network);
715 716
		dhcp_release(dhcp);
		dhcp_invalidate(dhcp, false);
717
		dhcp_free(dhcp);
718
	}
719 720
}

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
void __connman_dhcp_decline(struct connman_ipconfig *ipconfig)
{
	struct connman_dhcp *dhcp;
	const char *address;
	struct in_addr addr;

	DBG("ipconfig_table %p ipconfig %p", ipconfig_table, ipconfig);

	if (!ipconfig_table)
		return;

	dhcp = g_hash_table_lookup(ipconfig_table, ipconfig);
	if (dhcp) {
		address = __connman_ipconfig_get_local(ipconfig);
		if (!address)
			return;

		if (inet_pton(AF_INET, address, &addr) != 1)
			connman_error("Could not convert address %s", address);

		g_dhcp_client_decline(dhcp->dhcp_client, htonl(addr.s_addr));
	}
}

745 746 747 748
int __connman_dhcp_init(void)
{
	DBG("");

749 750
	ipconfig_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
								NULL, NULL);
751 752 753 754 755 756 757 758

	return 0;
}

void __connman_dhcp_cleanup(void)
{
	DBG("");

759 760
	g_hash_table_destroy(ipconfig_table);
	ipconfig_table = NULL;
761
}