dhcp: don't fail assertions when a DHCP client is not available
[NetworkManager.git] / src / dhcp-manager / nm-dhcp-manager.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* nm-dhcp-manager.c - Handle the DHCP daemon for NetworkManager
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Copyright (C) 2005 - 2013 Red Hat, Inc.
19  * Copyright (C) 2006 - 2008 Novell, Inc.
20  *
21  */
22
23 #include "nm-default.h"
24
25 #include <sys/socket.h>
26 #include <sys/wait.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34
35 #include "nm-dhcp-manager.h"
36 #include "nm-dhcp-dhclient.h"
37 #include "nm-dhcp-dhcpcd.h"
38 #include "nm-dhcp-systemd.h"
39 #include "nm-config.h"
40 #include "NetworkManagerUtils.h"
41
42 #define DHCP_TIMEOUT 45 /* default DHCP timeout, in seconds */
43
44 /* default to installed helper, but can be modified for testing */
45 const char *nm_dhcp_helper_path = LIBEXECDIR "/nm-dhcp-helper";
46
47 typedef GSList * (*GetLeaseConfigFunc) (const char *iface, const char *uuid, gboolean ipv6);
48
49 typedef struct {
50         GType               client_type;
51         GHashTable *        clients;
52         char *              default_hostname;
53 } NMDhcpManagerPrivate;
54
55 #define NM_DHCP_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_MANAGER, NMDhcpManagerPrivate))
56
57 G_DEFINE_TYPE (NMDhcpManager, nm_dhcp_manager, G_TYPE_OBJECT)
58
59 /***************************************************/
60
61 typedef struct {
62         GType gtype;
63         const char *name;
64         NMDhcpClientGetPathFunc get_path_func;
65         NMDhcpClientGetLeaseConfigsFunc get_lease_configs_func;
66 } ClientDesc;
67
68 static GSList *client_descs = NULL;
69
70 void
71 _nm_dhcp_client_register (GType gtype,
72                           const char *name,
73                           NMDhcpClientGetPathFunc get_path_func,
74                           NMDhcpClientGetLeaseConfigsFunc get_lease_configs_func)
75 {
76         ClientDesc *desc;
77         GSList *iter;
78
79         g_return_if_fail (gtype != G_TYPE_INVALID);
80         g_return_if_fail (name != NULL);
81
82         for (iter = client_descs; iter; iter = iter->next) {
83                 desc = iter->data;
84                 g_return_if_fail (desc->gtype != gtype);
85                 g_return_if_fail (strcmp (desc->name, name) != 0);
86         }
87
88         desc = g_slice_new0 (ClientDesc);
89         desc->gtype = gtype;
90         desc->name = name;
91         desc->get_path_func = get_path_func;
92         desc->get_lease_configs_func = get_lease_configs_func;
93         client_descs = g_slist_prepend (client_descs, desc);
94 }
95
96 static ClientDesc *
97 find_client_desc (const char *name, GType gtype)
98 {
99         GSList *iter;
100
101         g_return_val_if_fail (name || gtype, NULL);
102
103         for (iter = client_descs; iter; iter = iter->next) {
104                 ClientDesc *desc = iter->data;
105
106                 if (name && strcmp (desc->name, name) != 0)
107                         continue;
108                 if (gtype && desc->gtype != gtype)
109                         continue;
110                 return desc;
111         }
112         return NULL;
113 }
114
115 static GType
116 is_client_enabled (const char *name)
117 {
118         ClientDesc *desc;
119
120         desc = find_client_desc (name, G_TYPE_INVALID);
121         if (desc && (!desc->get_path_func || desc->get_path_func()))
122                 return desc->gtype;
123
124         return G_TYPE_INVALID;
125 }
126
127 /***************************************************/
128
129 static NMDhcpClient *
130 get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6)
131 {
132         NMDhcpManagerPrivate *priv;
133         GHashTableIter iter;
134         gpointer value;
135
136         g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL);
137         g_return_val_if_fail (ifindex > 0, NULL);
138
139         priv = NM_DHCP_MANAGER_GET_PRIVATE (manager);
140
141         g_hash_table_iter_init (&iter, priv->clients);
142         while (g_hash_table_iter_next (&iter, NULL, &value)) {
143                 NMDhcpClient *candidate = NM_DHCP_CLIENT (value);
144
145                 if (   nm_dhcp_client_get_ifindex (candidate) == ifindex
146                     && nm_dhcp_client_get_ipv6 (candidate) == ip6)
147                         return candidate;
148         }
149
150         return NULL;
151 }
152
153 static void client_state_changed (NMDhcpClient *client,
154                                   NMDhcpState state,
155                                   GObject *ip_config,
156                                   GVariant *options,
157                                   const char *event_id,
158                                   NMDhcpManager *self);
159
160 static void
161 remove_client (NMDhcpManager *self, NMDhcpClient *client)
162 {
163         g_signal_handlers_disconnect_by_func (client, client_state_changed, self);
164
165         /* Stopping the client is left up to the controlling device
166          * explicitly since we may want to quit NetworkManager but not terminate
167          * the DHCP client.
168          */
169
170         g_hash_table_remove (NM_DHCP_MANAGER_GET_PRIVATE (self)->clients, client);
171 }
172
173 static void
174 client_state_changed (NMDhcpClient *client,
175                       NMDhcpState state,
176                       GObject *ip_config,
177                       GVariant *options,
178                       const char *event_id,
179                       NMDhcpManager *self)
180 {
181         if (state >= NM_DHCP_STATE_TIMEOUT)
182                 remove_client (self, client);
183 }
184
185 static NMDhcpClient *
186 client_start (NMDhcpManager *self,
187               const char *iface,
188               int ifindex,
189               const GByteArray *hwaddr,
190               const char *uuid,
191               guint32 priority,
192               gboolean ipv6,
193               const struct in6_addr *ipv6_ll_addr,
194               const char *dhcp_client_id,
195               guint32 timeout,
196               const char *dhcp_anycast_addr,
197               const char *hostname,
198               const char *fqdn,
199               gboolean info_only,
200               NMSettingIP6ConfigPrivacy privacy,
201               const char *last_ip4_address)
202 {
203         NMDhcpManagerPrivate *priv;
204         NMDhcpClient *client;
205         gboolean success = FALSE;
206
207         g_return_val_if_fail (self, NULL);
208         g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
209         g_return_val_if_fail (ifindex > 0, NULL);
210         g_return_val_if_fail (uuid != NULL, NULL);
211
212         priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
213
214         /* Ensure we have a usable DHCP client */
215         if (priv->client_type == G_TYPE_INVALID)
216                 return NULL;
217
218         /* Kill any old client instance */
219         client = get_client_for_ifindex (self, ifindex, ipv6);
220         if (client) {
221                 g_object_ref (client);
222                 remove_client (self, client);
223                 nm_dhcp_client_stop (client, FALSE);
224                 g_object_unref (client);
225         }
226
227         /* And make a new one */
228         client = g_object_new (priv->client_type,
229                                NM_DHCP_CLIENT_INTERFACE, iface,
230                                NM_DHCP_CLIENT_IFINDEX, ifindex,
231                                NM_DHCP_CLIENT_HWADDR, hwaddr,
232                                NM_DHCP_CLIENT_IPV6, ipv6,
233                                NM_DHCP_CLIENT_UUID, uuid,
234                                NM_DHCP_CLIENT_PRIORITY, priority,
235                                NM_DHCP_CLIENT_TIMEOUT, timeout ? timeout : DHCP_TIMEOUT,
236                                NULL);
237         g_hash_table_insert (NM_DHCP_MANAGER_GET_PRIVATE (self)->clients, client, g_object_ref (client));
238         g_signal_connect (client, NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED, G_CALLBACK (client_state_changed), self);
239
240         if (ipv6)
241                 success = nm_dhcp_client_start_ip6 (client, dhcp_anycast_addr, ipv6_ll_addr, hostname, info_only, privacy);
242         else
243                 success = nm_dhcp_client_start_ip4 (client, dhcp_client_id, dhcp_anycast_addr, hostname, fqdn, last_ip4_address);
244
245         if (!success) {
246                 remove_client (self, client);
247                 client = NULL;
248         }
249
250         return client;
251 }
252
253 static const char *
254 get_send_hostname (NMDhcpManager *self, const char *setting_hostname)
255 {
256         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
257
258         /* Always prefer the explicit dhcp-send-hostname if given */
259         return setting_hostname ? setting_hostname : priv->default_hostname;
260 }
261
262 /* Caller owns a reference to the NMDhcpClient on return */
263 NMDhcpClient *
264 nm_dhcp_manager_start_ip4 (NMDhcpManager *self,
265                            const char *iface,
266                            int ifindex,
267                            const GByteArray *hwaddr,
268                            const char *uuid,
269                            guint32 priority,
270                            gboolean send_hostname,
271                            const char *dhcp_hostname,
272                            const char *dhcp_fqdn,
273                            const char *dhcp_client_id,
274                            guint32 timeout,
275                            const char *dhcp_anycast_addr,
276                            const char *last_ip_address)
277 {
278         const char *hostname = NULL;
279         const char *fqdn = NULL;
280
281         g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
282
283         if (send_hostname) {
284                 hostname = get_send_hostname (self, dhcp_hostname);
285                 fqdn = dhcp_fqdn;
286         }
287         return client_start (self, iface, ifindex, hwaddr, uuid, priority, FALSE, NULL,
288                              dhcp_client_id, timeout, dhcp_anycast_addr, hostname,
289                              fqdn, FALSE, 0, last_ip_address);
290 }
291
292 /* Caller owns a reference to the NMDhcpClient on return */
293 NMDhcpClient *
294 nm_dhcp_manager_start_ip6 (NMDhcpManager *self,
295                            const char *iface,
296                            int ifindex,
297                            const GByteArray *hwaddr,
298                            const struct in6_addr *ll_addr,
299                            const char *uuid,
300                            guint32 priority,
301                            gboolean send_hostname,
302                            const char *dhcp_hostname,
303                            guint32 timeout,
304                            const char *dhcp_anycast_addr,
305                            gboolean info_only,
306                            NMSettingIP6ConfigPrivacy privacy)
307 {
308         const char *hostname = NULL;
309
310         g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
311
312         if (send_hostname)
313                 hostname = get_send_hostname (self, dhcp_hostname);
314         return client_start (self, iface, ifindex, hwaddr, uuid, priority, TRUE,
315                              ll_addr, NULL, timeout, dhcp_anycast_addr, hostname, NULL, info_only,
316                              privacy, NULL);
317 }
318
319 void
320 nm_dhcp_manager_set_default_hostname (NMDhcpManager *manager, const char *hostname)
321 {
322         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (manager);
323
324         g_clear_pointer (&priv->default_hostname, g_free);
325
326         /* Never send 'localhost'-type names to the DHCP server */
327         if (!nm_utils_is_specific_hostname (hostname))
328                 return;
329
330         priv->default_hostname = g_strdup (hostname);
331 }
332
333 GSList *
334 nm_dhcp_manager_get_lease_ip_configs (NMDhcpManager *self,
335                                       const char *iface,
336                                       int ifindex,
337                                       const char *uuid,
338                                       gboolean ipv6,
339                                       guint32 default_route_metric)
340 {
341         NMDhcpManagerPrivate *priv;
342         ClientDesc *desc;
343
344         g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
345         g_return_val_if_fail (iface != NULL, NULL);
346         g_return_val_if_fail (ifindex >= -1, NULL);
347         g_return_val_if_fail (uuid != NULL, NULL);
348
349         priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
350         if (priv->client_type == G_TYPE_INVALID)
351                 return NULL;
352
353         desc = find_client_desc (NULL, priv->client_type);
354         if (desc && desc->get_lease_configs_func)
355                 return desc->get_lease_configs_func (iface, ifindex, uuid, ipv6, default_route_metric);
356         return NULL;
357 }
358
359 /***************************************************/
360
361 NM_DEFINE_SINGLETON_GETTER (NMDhcpManager, nm_dhcp_manager_get, NM_TYPE_DHCP_MANAGER);
362
363 static void
364 nm_dhcp_manager_init (NMDhcpManager *self)
365 {
366         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
367         NMConfig *config = nm_config_get ();
368         const char *client;
369         GSList *iter;
370         GType type = G_TYPE_INVALID;
371
372         for (iter = client_descs; iter; iter = iter->next) {
373                 ClientDesc *desc = iter->data;
374
375                 nm_log_dbg (LOGD_DHCP, "Registered DHCP client '%s' (%s)",
376                             desc->name, g_type_name (desc->gtype));
377         }
378
379         /* Client-specific setup */
380         client = nm_config_get_dhcp_client (config);
381         if (nm_config_get_configure_and_quit (config)) {
382                 if (g_strcmp0 (client, "internal") != 0)
383                         nm_log_warn (LOGD_DHCP, "Using internal DHCP client since configure-and-quit is set.");
384                 client = "internal";
385         }
386
387         if (client)
388                 type = is_client_enabled (client);
389
390         if (type == G_TYPE_INVALID) {
391                 if (client)
392                         nm_log_warn (LOGD_DHCP, "DHCP client '%s' not available", client);
393
394                 type = is_client_enabled ("dhclient");
395                 if (type == G_TYPE_INVALID)
396                         type = is_client_enabled ("dhcpcd");
397                 if (type == G_TYPE_INVALID)
398                         type = is_client_enabled ("internal");
399         }
400
401         if (type == G_TYPE_INVALID)
402                 nm_log_warn (LOGD_DHCP, "No usable DHCP client found! DHCP configurations will fail");
403         else
404                 nm_log_info (LOGD_DHCP, "Using DHCP client '%s'", find_client_desc (NULL, type)->name);
405
406         priv->client_type = type;
407         priv->clients = g_hash_table_new_full (g_direct_hash, g_direct_equal,
408                                                NULL,
409                                                (GDestroyNotify) g_object_unref);
410 }
411
412 static void
413 dispose (GObject *object)
414 {
415         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (object);
416         GList *values, *iter;
417
418         if (priv->clients) {
419                 values = g_hash_table_get_values (priv->clients);
420                 for (iter = values; iter; iter = g_list_next (iter))
421                         remove_client (NM_DHCP_MANAGER (object), NM_DHCP_CLIENT (iter->data));
422                 g_list_free (values);
423         }
424
425         G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->dispose (object);
426 }
427
428 static void
429 finalize (GObject *object)
430 {
431         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (object);
432
433         g_free (priv->default_hostname);
434
435         if (priv->clients)
436                 g_hash_table_destroy (priv->clients);
437
438         G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->finalize (object);
439 }
440
441 static void
442 nm_dhcp_manager_class_init (NMDhcpManagerClass *manager_class)
443 {
444         GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
445
446         g_type_class_add_private (manager_class, sizeof (NMDhcpManagerPrivate));
447
448         /* virtual methods */
449         object_class->finalize = finalize;
450         object_class->dispose = dispose;
451 }