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