dhcp: Call ipconfig_address_remove from dhcp_invalidate
[framework/connectivity/connman.git] / src / dhcp.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <connman/ipconfig.h>
31
32 #include <gdhcp/gdhcp.h>
33
34 #include <glib.h>
35
36 #include "connman.h"
37
38 struct connman_dhcp {
39         struct connman_network *network;
40         dhcp_cb callback;
41
42         char **nameservers;
43         char *timeserver;
44         char *pac;
45
46         GDHCPClient *dhcp_client;
47 };
48
49 static GHashTable *network_table;
50
51 static void dhcp_free(struct connman_dhcp *dhcp)
52 {
53         g_strfreev(dhcp->nameservers);
54         g_free(dhcp->timeserver);
55         g_free(dhcp->pac);
56
57         dhcp->nameservers = NULL;
58         dhcp->timeserver = NULL;
59         dhcp->pac = NULL;
60 }
61
62 /**
63  * dhcp_invalidate: Invalidate an existing DHCP lease
64  * @dhcp: pointer to the DHCP lease to invalidate.
65  * @callback: flag indicating whether or not to invoke the client callback
66  *            if present.
67  *
68  * Invalidates an existing DHCP lease, optionally invoking the client
69  * callback. The caller may wish to avoid the client callback invocation
70  * when the invocation of that callback might otherwise unnecessarily upset
71  * service state due to the IP configuration change implied by this
72  * invalidation.
73  */
74 static void dhcp_invalidate(struct connman_dhcp *dhcp, connman_bool_t callback)
75 {
76         struct connman_service *service;
77         struct connman_ipconfig *ipconfig;
78         int i;
79
80         DBG("dhcp %p callback %u", dhcp, callback);
81
82         if (dhcp == NULL)
83                 return;
84
85         service = __connman_service_lookup_from_network(dhcp->network);
86         if (service == NULL)
87                 return;
88
89         ipconfig = __connman_service_get_ip4config(service);
90         if (ipconfig == NULL)
91                 return;
92
93         __connman_6to4_remove(ipconfig);
94
95         __connman_service_set_domainname(service, NULL);
96         __connman_service_set_pac(service, NULL);
97         __connman_service_timeserver_remove(service, dhcp->timeserver);
98
99         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
100                 __connman_service_nameserver_remove(service,
101                                                 dhcp->nameservers[i]);
102         }
103
104         __connman_ipconfig_address_remove(ipconfig);
105
106         __connman_ipconfig_set_local(ipconfig, NULL);
107         __connman_ipconfig_set_broadcast(ipconfig, NULL);
108         __connman_ipconfig_set_gateway(ipconfig, NULL);
109         __connman_ipconfig_set_prefixlen(ipconfig, 0);
110
111         if (dhcp->callback != NULL && callback)
112                 dhcp->callback(dhcp->network, FALSE);
113
114         dhcp_free(dhcp);
115 }
116
117 static void dhcp_valid(struct connman_dhcp *dhcp)
118 {
119         if (dhcp->callback != NULL)
120                 dhcp->callback(dhcp->network, TRUE);
121 }
122
123 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
124 {
125         struct connman_dhcp *dhcp = user_data;
126
127         DBG("No lease available");
128
129         dhcp_invalidate(dhcp, TRUE);
130 }
131
132 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
133 {
134         struct connman_dhcp *dhcp = user_data;
135
136         DBG("Lease lost");
137
138         dhcp_invalidate(dhcp, TRUE);
139 }
140
141 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
142 {
143         struct connman_dhcp *dhcp = user_data;
144
145         DBG("Lease lost");
146
147         dhcp_invalidate(dhcp, TRUE);
148 }
149
150
151 static gboolean compare_string_arrays(char **array_a, char **array_b)
152 {
153         int i;
154
155         if (array_a == NULL || array_b == NULL)
156                 return FALSE;
157
158         if (g_strv_length(array_a) != g_strv_length(array_b))
159                 return FALSE;
160
161         for (i = 0; array_a[i] != NULL &&
162                              array_b[i] != NULL; i++) {
163                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
164                         return FALSE;
165         }
166
167         return TRUE;
168 }
169
170 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
171 {
172         struct connman_dhcp *dhcp = user_data;
173         GList *list, *option = NULL;
174         char *address, *netmask = NULL, *gateway = NULL;
175         const char *c_address, *c_gateway;
176         char *domainname = NULL, *hostname = NULL;
177         char **nameservers, *timeserver = NULL, *pac = NULL;
178         int ns_entries;
179         struct connman_ipconfig *ipconfig;
180         struct connman_service *service;
181         unsigned char prefixlen, c_prefixlen;
182         gboolean ip_change;
183         int i;
184
185         DBG("Lease available");
186
187         service = __connman_service_lookup_from_network(dhcp->network);
188         if (service == NULL) {
189                 connman_error("Can not lookup service");
190                 return;
191         }
192
193         ipconfig = __connman_service_get_ip4config(service);
194         if (ipconfig == NULL) {
195                 connman_error("Could not lookup ipconfig");
196                 return;
197         }
198
199         c_address = __connman_ipconfig_get_local(ipconfig);
200         c_gateway = __connman_ipconfig_get_gateway(ipconfig);
201         c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
202
203         address = g_dhcp_client_get_address(dhcp_client);
204
205         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
206         if (option != NULL)
207                 netmask = g_strdup(option->data);
208
209         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
210         if (option != NULL)
211                 gateway = g_strdup(option->data);
212
213         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
214
215         DBG("c_address %s", c_address);
216
217         if (address != NULL && c_address != NULL &&
218                                         g_strcmp0(address, c_address) != 0)
219                 ip_change = TRUE;
220         else if (gateway != NULL && c_gateway != NULL &&
221                                         g_strcmp0(gateway, c_gateway) != 0)
222                 ip_change = TRUE;
223         else if (prefixlen != c_prefixlen)
224                 ip_change = TRUE;
225         else if (c_address == NULL || c_gateway == NULL)
226                 ip_change = TRUE;
227         else
228                 ip_change = FALSE;
229
230         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
231         for (ns_entries = 0, list = option; list; list = list->next)
232                 ns_entries += 1;
233         nameservers = g_try_new0(char *, ns_entries + 1);
234         if (nameservers != NULL) {
235                 for (i = 0, list = option; list; list = list->next, i++)
236                         nameservers[i] = g_strdup(list->data);
237                 nameservers[ns_entries] = NULL;
238         }
239
240         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
241         if (option != NULL)
242                 domainname = g_strdup(option->data);
243
244         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
245         if (option != NULL)
246                 hostname = g_strdup(option->data);
247
248         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
249         if (option != NULL)
250                 timeserver = g_strdup(option->data);
251
252         option = g_dhcp_client_get_option(dhcp_client, 252);
253         if (option != NULL)
254                 pac = g_strdup(option->data);
255
256         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
257
258         if (ip_change == TRUE) {
259                 __connman_ipconfig_set_local(ipconfig, address);
260                 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
261                 __connman_ipconfig_set_gateway(ipconfig, gateway);
262         }
263
264         if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
265                 if (dhcp->nameservers != NULL) {
266                         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
267                                 __connman_service_nameserver_remove(service,
268                                                         dhcp->nameservers[i]);
269                         }
270                         g_strfreev(dhcp->nameservers);
271                 }
272
273                 dhcp->nameservers = nameservers;
274
275                 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
276                         __connman_service_nameserver_append(service,
277                                                         dhcp->nameservers[i]);
278                 }
279         }
280
281         if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
282                 if (dhcp->timeserver != NULL) {
283                         __connman_service_timeserver_remove(service,
284                                                         dhcp->timeserver);
285                         g_free(dhcp->timeserver);
286                 }
287
288                 dhcp->timeserver = timeserver;
289
290                 if (dhcp->timeserver != NULL)
291                         __connman_service_timeserver_append(service,
292                                                         dhcp->timeserver);
293         }
294
295         if (g_strcmp0(pac, dhcp->pac) != 0) {
296                 g_free(dhcp->pac);
297                 dhcp->pac = pac;
298
299                 __connman_service_set_pac(service, dhcp->pac);
300         }
301
302         __connman_service_set_domainname(service, domainname);
303
304         if (domainname != NULL)
305                 __connman_utsname_set_domainname(domainname);
306
307         if (hostname != NULL)
308                 __connman_utsname_set_hostname(hostname);
309
310         if (ip_change == TRUE)
311                 dhcp_valid(dhcp);
312
313         __connman_6to4_probe(service);
314
315         g_free(address);
316         g_free(netmask);
317         g_free(gateway);
318         g_free(domainname);
319         g_free(hostname);
320 }
321
322 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
323 {
324         struct connman_dhcp *dhcp = user_data;
325         char *address, *netmask;
326         struct connman_service *service;
327         struct connman_ipconfig *ipconfig;
328         unsigned char prefixlen;
329
330         DBG("IPV4LL available");
331
332         service = __connman_service_lookup_from_network(dhcp->network);
333         if (service == NULL)
334                 return;
335
336         ipconfig = __connman_service_get_ip4config(service);
337         if (ipconfig == NULL)
338                 return;
339
340         address = g_dhcp_client_get_address(dhcp_client);
341         netmask = g_dhcp_client_get_netmask(dhcp_client);
342
343         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
344
345         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
346         __connman_ipconfig_set_local(ipconfig, address);
347         __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
348         __connman_ipconfig_set_gateway(ipconfig, NULL);
349
350         dhcp_valid(dhcp);
351
352         g_free(address);
353         g_free(netmask);
354 }
355
356 static void dhcp_debug(const char *str, void *data)
357 {
358         connman_info("%s: %s\n", (const char *) data, str);
359 }
360
361 static int dhcp_request(struct connman_dhcp *dhcp)
362 {
363         GDHCPClient *dhcp_client;
364         GDHCPClientError error;
365         const char *hostname;
366         int index;
367
368         DBG("dhcp %p", dhcp);
369
370         index = connman_network_get_index(dhcp->network);
371
372         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
373         if (error != G_DHCP_CLIENT_ERROR_NONE)
374                 return -EINVAL;
375
376         if (getenv("CONNMAN_DHCP_DEBUG"))
377                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
378
379         hostname = connman_utsname_get_hostname();
380         if (hostname != NULL)
381                 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
382
383         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
384         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
385         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
386         g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
387         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
388         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
389         g_dhcp_client_set_request(dhcp_client, 252);
390
391         g_dhcp_client_register_event(dhcp_client,
392                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
393                                                 lease_available_cb, dhcp);
394
395         g_dhcp_client_register_event(dhcp_client,
396                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
397                                                 ipv4ll_available_cb, dhcp);
398
399         g_dhcp_client_register_event(dhcp_client,
400                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
401
402         g_dhcp_client_register_event(dhcp_client,
403                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
404
405         g_dhcp_client_register_event(dhcp_client,
406                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
407
408         dhcp->dhcp_client = dhcp_client;
409
410         return g_dhcp_client_start(dhcp_client);
411 }
412
413 static int dhcp_release(struct connman_dhcp *dhcp)
414 {
415         DBG("dhcp %p", dhcp);
416
417         if (dhcp->dhcp_client == NULL)
418                 return 0;
419
420         g_dhcp_client_stop(dhcp->dhcp_client);
421         g_dhcp_client_unref(dhcp->dhcp_client);
422
423         dhcp->dhcp_client = NULL;
424
425         return 0;
426 }
427
428 static void remove_network(gpointer user_data)
429 {
430         struct connman_dhcp *dhcp = user_data;
431
432         DBG("dhcp %p", dhcp);
433
434         dhcp_invalidate(dhcp, FALSE);
435         dhcp_release(dhcp);
436
437         g_free(dhcp);
438 }
439
440 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
441 {
442         struct connman_dhcp *dhcp;
443
444         DBG("");
445
446         dhcp = g_try_new0(struct connman_dhcp, 1);
447         if (dhcp == NULL)
448                 return -ENOMEM;
449
450         dhcp->network = network;
451         dhcp->callback = callback;
452
453         g_hash_table_replace(network_table, network, dhcp);
454
455         return dhcp_request(dhcp);
456 }
457
458 void __connman_dhcp_stop(struct connman_network *network)
459 {
460         DBG("");
461
462         g_hash_table_remove(network_table, network);
463 }
464
465 int __connman_dhcp_init(void)
466 {
467         DBG("");
468
469         network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
470                                                         NULL, remove_network);
471
472         return 0;
473 }
474
475 void __connman_dhcp_cleanup(void)
476 {
477         DBG("");
478
479         g_hash_table_destroy(network_table);
480 }