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