dhcp: Invalidate and Release on Network Removal
[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_set_local(ipconfig, NULL);
105         __connman_ipconfig_set_broadcast(ipconfig, NULL);
106         __connman_ipconfig_set_gateway(ipconfig, NULL);
107         __connman_ipconfig_set_prefixlen(ipconfig, 0);
108
109         if (dhcp->callback != NULL && callback)
110                 dhcp->callback(dhcp->network, FALSE);
111
112         dhcp_free(dhcp);
113 }
114
115 static void dhcp_valid(struct connman_dhcp *dhcp)
116 {
117         if (dhcp->callback != NULL)
118                 dhcp->callback(dhcp->network, TRUE);
119 }
120
121 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
122 {
123         struct connman_dhcp *dhcp = user_data;
124
125         DBG("No lease available");
126
127         dhcp_invalidate(dhcp, TRUE);
128 }
129
130 static void lease_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
131 {
132         struct connman_dhcp *dhcp = user_data;
133
134         DBG("Lease lost");
135
136         dhcp_invalidate(dhcp, TRUE);
137 }
138
139 static void ipv4ll_lost_cb(GDHCPClient *dhcp_client, gpointer user_data)
140 {
141         struct connman_dhcp *dhcp = user_data;
142
143         DBG("Lease lost");
144
145         dhcp_invalidate(dhcp, TRUE);
146 }
147
148
149 static gboolean compare_string_arrays(char **array_a, char **array_b)
150 {
151         int i;
152
153         if (array_a == NULL || array_b == NULL)
154                 return FALSE;
155
156         if (g_strv_length(array_a) != g_strv_length(array_b))
157                 return FALSE;
158
159         for (i = 0; array_a[i] != NULL &&
160                              array_b[i] != NULL; i++) {
161                 if (g_strcmp0(array_a[i], array_b[i]) != 0)
162                         return FALSE;
163         }
164
165         return TRUE;
166 }
167
168 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
169 {
170         struct connman_dhcp *dhcp = user_data;
171         GList *list, *option = NULL;
172         char *address, *netmask = NULL, *gateway = NULL;
173         const char *c_address, *c_gateway;
174         char *domainname = NULL, *hostname = NULL;
175         char **nameservers, *timeserver = NULL, *pac = NULL;
176         int ns_entries;
177         struct connman_ipconfig *ipconfig;
178         struct connman_service *service;
179         unsigned char prefixlen, c_prefixlen;
180         gboolean ip_change;
181         int i;
182
183         DBG("Lease available");
184
185         service = __connman_service_lookup_from_network(dhcp->network);
186         if (service == NULL) {
187                 connman_error("Can not lookup service");
188                 return;
189         }
190
191         ipconfig = __connman_service_get_ip4config(service);
192         if (ipconfig == NULL) {
193                 connman_error("Could not lookup ipconfig");
194                 return;
195         }
196
197         c_address = __connman_ipconfig_get_local(ipconfig);
198         c_gateway = __connman_ipconfig_get_gateway(ipconfig);
199         c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);
200
201         address = g_dhcp_client_get_address(dhcp_client);
202
203         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
204         if (option != NULL)
205                 netmask = g_strdup(option->data);
206
207         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
208         if (option != NULL)
209                 gateway = g_strdup(option->data);
210
211         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
212
213         DBG("c_address %s", c_address);
214
215         if (address != NULL && c_address != NULL &&
216                                         g_strcmp0(address, c_address) != 0)
217                 ip_change = TRUE;
218         else if (gateway != NULL && c_gateway != NULL &&
219                                         g_strcmp0(gateway, c_gateway) != 0)
220                 ip_change = TRUE;
221         else if (prefixlen != c_prefixlen)
222                 ip_change = TRUE;
223         else if (c_address == NULL || c_gateway == NULL)
224                 ip_change = TRUE;
225         else
226                 ip_change = FALSE;
227
228         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
229         for (ns_entries = 0, list = option; list; list = list->next)
230                 ns_entries += 1;
231         nameservers = g_try_new0(char *, ns_entries + 1);
232         if (nameservers != NULL) {
233                 for (i = 0, list = option; list; list = list->next, i++)
234                         nameservers[i] = g_strdup(list->data);
235                 nameservers[ns_entries] = NULL;
236         }
237
238         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
239         if (option != NULL)
240                 domainname = g_strdup(option->data);
241
242         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
243         if (option != NULL)
244                 hostname = g_strdup(option->data);
245
246         option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
247         if (option != NULL)
248                 timeserver = g_strdup(option->data);
249
250         option = g_dhcp_client_get_option(dhcp_client, 252);
251         if (option != NULL)
252                 pac = g_strdup(option->data);
253
254         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
255
256         if (ip_change == TRUE) {
257                 __connman_ipconfig_set_local(ipconfig, address);
258                 __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
259                 __connman_ipconfig_set_gateway(ipconfig, gateway);
260         }
261
262         if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
263                 if (dhcp->nameservers != NULL) {
264                         for (i = 0; dhcp->nameservers[i] != NULL; i++) {
265                                 __connman_service_nameserver_remove(service,
266                                                         dhcp->nameservers[i]);
267                         }
268                         g_strfreev(dhcp->nameservers);
269                 }
270
271                 dhcp->nameservers = nameservers;
272
273                 for (i = 0; dhcp->nameservers[i] != NULL; i++) {
274                         __connman_service_nameserver_append(service,
275                                                         dhcp->nameservers[i]);
276                 }
277         }
278
279         if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
280                 if (dhcp->timeserver != NULL) {
281                         __connman_service_timeserver_remove(service,
282                                                         dhcp->timeserver);
283                         g_free(dhcp->timeserver);
284                 }
285
286                 dhcp->timeserver = timeserver;
287
288                 if (dhcp->timeserver != NULL)
289                         __connman_service_timeserver_append(service,
290                                                         dhcp->timeserver);
291         }
292
293         if (g_strcmp0(pac, dhcp->pac) != 0) {
294                 g_free(dhcp->pac);
295                 dhcp->pac = pac;
296
297                 __connman_service_set_pac(service, dhcp->pac);
298         }
299
300         __connman_service_set_domainname(service, domainname);
301
302         if (domainname != NULL)
303                 __connman_utsname_set_domainname(domainname);
304
305         if (hostname != NULL)
306                 __connman_utsname_set_hostname(hostname);
307
308         if (ip_change == TRUE)
309                 dhcp_valid(dhcp);
310
311         __connman_6to4_probe(service);
312
313         g_free(address);
314         g_free(netmask);
315         g_free(gateway);
316         g_free(domainname);
317         g_free(hostname);
318 }
319
320 static void ipv4ll_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
321 {
322         struct connman_dhcp *dhcp = user_data;
323         char *address, *netmask;
324         struct connman_service *service;
325         struct connman_ipconfig *ipconfig;
326         unsigned char prefixlen;
327
328         DBG("IPV4LL available");
329
330         service = __connman_service_lookup_from_network(dhcp->network);
331         if (service == NULL)
332                 return;
333
334         ipconfig = __connman_service_get_ip4config(service);
335         if (ipconfig == NULL)
336                 return;
337
338         address = g_dhcp_client_get_address(dhcp_client);
339         netmask = g_dhcp_client_get_netmask(dhcp_client);
340
341         prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
342
343         connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);
344         __connman_ipconfig_set_local(ipconfig, address);
345         __connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
346         __connman_ipconfig_set_gateway(ipconfig, NULL);
347
348         dhcp_valid(dhcp);
349
350         g_free(address);
351         g_free(netmask);
352 }
353
354 static void dhcp_debug(const char *str, void *data)
355 {
356         connman_info("%s: %s\n", (const char *) data, str);
357 }
358
359 static int dhcp_request(struct connman_dhcp *dhcp)
360 {
361         GDHCPClient *dhcp_client;
362         GDHCPClientError error;
363         const char *hostname;
364         int index;
365
366         DBG("dhcp %p", dhcp);
367
368         index = connman_network_get_index(dhcp->network);
369
370         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
371         if (error != G_DHCP_CLIENT_ERROR_NONE)
372                 return -EINVAL;
373
374         if (getenv("CONNMAN_DHCP_DEBUG"))
375                 g_dhcp_client_set_debug(dhcp_client, dhcp_debug, "DHCP");
376
377         hostname = connman_utsname_get_hostname();
378         if (hostname != NULL)
379                 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, hostname);
380
381         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
382         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
383         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
384         g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
385         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
386         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
387         g_dhcp_client_set_request(dhcp_client, 252);
388
389         g_dhcp_client_register_event(dhcp_client,
390                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
391                                                 lease_available_cb, dhcp);
392
393         g_dhcp_client_register_event(dhcp_client,
394                         G_DHCP_CLIENT_EVENT_IPV4LL_AVAILABLE,
395                                                 ipv4ll_available_cb, dhcp);
396
397         g_dhcp_client_register_event(dhcp_client,
398                         G_DHCP_CLIENT_EVENT_LEASE_LOST, lease_lost_cb, dhcp);
399
400         g_dhcp_client_register_event(dhcp_client,
401                         G_DHCP_CLIENT_EVENT_IPV4LL_LOST, ipv4ll_lost_cb, dhcp);
402
403         g_dhcp_client_register_event(dhcp_client,
404                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, dhcp);
405
406         dhcp->dhcp_client = dhcp_client;
407
408         return g_dhcp_client_start(dhcp_client);
409 }
410
411 static int dhcp_release(struct connman_dhcp *dhcp)
412 {
413         DBG("dhcp %p", dhcp);
414
415         if (dhcp->dhcp_client == NULL)
416                 return 0;
417
418         g_dhcp_client_stop(dhcp->dhcp_client);
419         g_dhcp_client_unref(dhcp->dhcp_client);
420
421         dhcp->dhcp_client = NULL;
422
423         return 0;
424 }
425
426 static void remove_network(gpointer user_data)
427 {
428         struct connman_dhcp *dhcp = user_data;
429
430         DBG("dhcp %p", dhcp);
431
432         dhcp_invalidate(dhcp, FALSE);
433         dhcp_release(dhcp);
434
435         g_free(dhcp);
436 }
437
438 int __connman_dhcp_start(struct connman_network *network, dhcp_cb callback)
439 {
440         struct connman_dhcp *dhcp;
441
442         DBG("");
443
444         dhcp = g_try_new0(struct connman_dhcp, 1);
445         if (dhcp == NULL)
446                 return -ENOMEM;
447
448         dhcp->network = network;
449         dhcp->callback = callback;
450
451         g_hash_table_replace(network_table, network, dhcp);
452
453         return dhcp_request(dhcp);
454 }
455
456 void __connman_dhcp_stop(struct connman_network *network)
457 {
458         DBG("");
459
460         g_hash_table_remove(network_table, network);
461 }
462
463 int __connman_dhcp_init(void)
464 {
465         DBG("");
466
467         network_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
468                                                         NULL, remove_network);
469
470         return 0;
471 }
472
473 void __connman_dhcp_cleanup(void)
474 {
475         DBG("");
476
477         g_hash_table_destroy(network_table);
478 }