Parse interface MTU values from DHCP results
[framework/connectivity/connman.git] / plugins / dhclient.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/wait.h>
29 #include <glib/gstdio.h>
30
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/dhcp.h>
34 #include <connman/dbus.h>
35 #include <connman/log.h>
36
37 #define DHCLIENT_INTF "org.isc.dhclient"
38 #define DHCLIENT_PATH "/org/isc/dhclient"
39
40 static const char *busname;
41
42 struct dhclient_task {
43         GPid pid;
44         gboolean killed;
45         int ifindex;
46         gchar *ifname;
47         struct dhclient_task *pending;
48         struct connman_dhcp *dhcp;
49 };
50
51 static GSList *task_list = NULL;
52
53 static struct dhclient_task *find_task_by_pid(GPid pid)
54 {
55         GSList *list;
56
57         for (list = task_list; list; list = list->next) {
58                 struct dhclient_task *task = list->data;
59
60                 if (task->pid == pid)
61                         return task;
62         }
63
64         return NULL;
65 }
66
67 static struct dhclient_task *find_task_by_index(int index)
68 {
69         GSList *list;
70
71         for (list = task_list; list; list = list->next) {
72                 struct dhclient_task *task = list->data;
73
74                 if (task->ifindex == index)
75                         return task;
76         }
77
78         return NULL;
79 }
80
81 static void kill_task(struct dhclient_task *task)
82 {
83         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
84
85         if (task->killed == TRUE)
86                 return;
87
88         if (task->pid > 0) {
89                 task->killed = TRUE;
90                 kill(task->pid, SIGTERM);
91         }
92 }
93
94 static void unlink_task(struct dhclient_task *task)
95 {
96         gchar *pathname;
97
98         DBG("task %p name %s pid %d", task, task->ifname, task->pid);
99
100         pathname = g_strdup_printf("%s/dhclient.%s.pid",
101                                                 STATEDIR, task->ifname);
102         g_unlink(pathname);
103         g_free(pathname);
104
105         pathname = g_strdup_printf("%s/dhclient.%s.leases",
106                                                 STATEDIR, task->ifname);
107         g_unlink(pathname);
108         g_free(pathname);
109 }
110
111 static int start_dhclient(struct dhclient_task *task);
112
113 static void task_died(GPid pid, gint status, gpointer data)
114 {
115         struct dhclient_task *task = data;
116
117         if (WIFEXITED(status))
118                 DBG("exit status %d for %s", WEXITSTATUS(status), task->ifname);
119         else
120                 DBG("signal %d killed %s", WTERMSIG(status), task->ifname);
121
122         g_spawn_close_pid(pid);
123         task->pid = 0;
124
125         task_list = g_slist_remove(task_list, task);
126
127         unlink_task(task);
128
129         if (task->pending != NULL)
130                 start_dhclient(task->pending);
131
132         connman_dhcp_unref(task->dhcp);
133
134         g_free(task->ifname);
135         g_free(task);
136 }
137
138 static void task_setup(gpointer data)
139 {
140         struct dhclient_task *task = data;
141
142         DBG("task %p name %s", task, task->ifname);
143
144         task->killed = FALSE;
145 }
146
147 static int start_dhclient(struct dhclient_task *task)
148 {
149         char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
150         char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
151
152         snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
153         snprintf(pidfile, sizeof(pidfile) - 1,
154                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
155         snprintf(leases, sizeof(leases) - 1,
156                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
157         snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
158         snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
159
160         argv[0] = DHCLIENT;
161         argv[1] = "-d";
162         argv[2] = "-q";
163         argv[3] = "-e";
164         argv[4] = address;
165         argv[5] = "-pf";
166         argv[6] = pidfile;
167         argv[7] = "-lf";
168         argv[8] = leases;
169         argv[9] = "-cf";
170         argv[10] = config;
171         argv[11] = "-sf";
172         argv[12] = script;
173         argv[13] = task->ifname;
174         argv[14] = "-n";
175         argv[15] = NULL;
176
177         envp[0] = NULL;
178
179         if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
180                                 task_setup, task, &task->pid, NULL) == FALSE) {
181                 connman_error("Failed to spawn dhclient");
182                 return -1;
183         }
184
185         task_list = g_slist_append(task_list, task);
186
187         g_child_watch_add(task->pid, task_died, task);
188
189         DBG("executed %s with pid %d", DHCLIENT, task->pid);
190
191         return 0;
192 }
193
194 static int dhclient_request(struct connman_dhcp *dhcp)
195 {
196         struct dhclient_task *task, *previous;
197
198         DBG("dhcp %p", dhcp);
199
200         if (access(DHCLIENT, X_OK) < 0)
201                 return -errno;
202
203         task = g_try_new0(struct dhclient_task, 1);
204         if (task == NULL)
205                 return -ENOMEM;
206
207         task->ifindex = connman_dhcp_get_index(dhcp);
208         task->ifname  = connman_dhcp_get_interface(dhcp);
209
210         if (task->ifname == NULL) {
211                 g_free(task);
212                 return -ENOMEM;
213         }
214
215         task->dhcp = connman_dhcp_ref(dhcp);
216
217         previous= find_task_by_index(task->ifindex);
218         if (previous != NULL) {
219                 previous->pending = task;
220                 kill_task(previous);
221                 return 0;
222         }
223
224         return start_dhclient(task);
225 }
226
227 static int dhclient_release(struct connman_dhcp *dhcp)
228 {
229         struct dhclient_task *task;
230         int index;
231
232         DBG("dhcp %p", dhcp);
233
234         index = connman_dhcp_get_index(dhcp);
235
236         task = find_task_by_index(index);
237         if (task == NULL)
238                 return -EINVAL;
239
240         DBG("release %s", task->ifname);
241
242         kill_task(task);
243
244         return 0;
245 }
246
247 static struct connman_dhcp_driver dhclient_driver = {
248         .name           = "dhclient",
249         .request        = dhclient_request,
250         .release        = dhclient_release,
251 };
252
253 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
254                                                 DBusMessage *msg, void *data)
255 {
256         DBusMessageIter iter, dict;
257         dbus_uint32_t pid;
258         struct dhclient_task *task;
259         const char *text, *key, *value;
260
261         if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
262                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
263
264         dbus_message_iter_init(msg, &iter);
265
266         dbus_message_iter_get_basic(&iter, &pid);
267         dbus_message_iter_next(&iter);
268
269         dbus_message_iter_get_basic(&iter, &text);
270         dbus_message_iter_next(&iter);
271
272         DBG("change %d to %s", pid, text);
273
274         task = find_task_by_pid(pid);
275
276         if (task == NULL)
277                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
278
279         dbus_message_iter_recurse(&iter, &dict);
280
281         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
282                 DBusMessageIter entry;
283
284                 dbus_message_iter_recurse(&dict, &entry);
285                 dbus_message_iter_get_basic(&entry, &key);
286                 dbus_message_iter_next(&entry);
287                 dbus_message_iter_get_basic(&entry, &value);
288
289                 DBG("%s = %s", key, value);
290
291                 if (g_strcmp0(key, "new_ip_address") == 0) {
292                         connman_dhcp_set_value(task->dhcp, "Address", value);
293                 } else if (g_strcmp0(key, "new_subnet_mask") == 0) {
294                         connman_dhcp_set_value(task->dhcp, "Netmask", value);
295                 } else if (g_strcmp0(key, "new_routers") == 0) {
296                         connman_dhcp_set_value(task->dhcp, "Gateway", value);
297                 } else if (g_strcmp0(key, "new_network_number") == 0) {
298                         connman_dhcp_set_value(task->dhcp, "Network", value);
299                 } else if (g_strcmp0(key, "new_broadcast_address") == 0) {
300                         connman_dhcp_set_value(task->dhcp, "Broadcast", value);
301                 } else if (g_strcmp0(key, "new_domain_name_servers") == 0) {
302                         connman_dhcp_set_value(task->dhcp, "Nameserver", value);
303                 } else if (g_ascii_strcasecmp(key, "new_domain_name") == 0) {
304                         connman_dhcp_set_value(task->dhcp, "Domainname", value);
305                 } else if (g_ascii_strcasecmp(key, "new_domain_search") == 0) {
306                 } else if (g_ascii_strcasecmp(key, "new_host_name") == 0) {
307                         connman_dhcp_set_value(task->dhcp, "Hostname", value);
308                 } else if (g_ascii_strcasecmp(key, "new_ntp_servers") == 0) {
309                         connman_dhcp_set_value(task->dhcp, "Timeserver", value);
310                 } else if (g_ascii_strcasecmp(key, "new_interface_mtu") == 0) {
311                         connman_dhcp_set_value(task->dhcp, "MTU", value);
312                 }
313
314                 dbus_message_iter_next(&dict);
315         }
316
317         if (g_ascii_strcasecmp(text, "PREINIT") == 0) {
318         } else if (g_ascii_strcasecmp(text, "BOUND") == 0 ||
319                                 g_ascii_strcasecmp(text, "REBOOT") == 0) {
320                 connman_dhcp_bound(task->dhcp);
321         } else if (g_ascii_strcasecmp(text, "RENEW") == 0 ||
322                                 g_ascii_strcasecmp(text, "REBIND") == 0) {
323                 connman_dhcp_renew(task->dhcp);
324         } else if (g_ascii_strcasecmp(text, "FAIL") == 0) {
325                 connman_dhcp_fail(task->dhcp);
326         } else {
327         }
328
329         return DBUS_HANDLER_RESULT_HANDLED;
330 }
331
332 static DBusConnection *connection;
333
334 static const char *dhclient_rule = "path=" DHCLIENT_PATH
335                                                 ",interface=" DHCLIENT_INTF;
336
337 static int dhclient_init(void)
338 {
339         int err;
340
341         connection = connman_dbus_get_connection();
342
343         busname = dbus_bus_get_unique_name(connection);
344         busname = CONNMAN_SERVICE;
345
346         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
347
348         dbus_bus_add_match(connection, dhclient_rule, NULL);
349
350         err = connman_dhcp_driver_register(&dhclient_driver);
351         if (err < 0) {
352                 dbus_connection_unref(connection);
353                 return err;
354         }
355
356         return 0;
357 }
358
359 static void dhclient_exit(void)
360 {
361         GSList *list;
362
363         for (list = task_list; list; list = list->next) {
364                 struct dhclient_task *task = list->data;
365
366                 DBG("killing process %d", task->pid);
367
368                 kill_task(task);
369                 unlink_task(task);
370         }
371
372         g_slist_free(task_list);
373
374         connman_dhcp_driver_unregister(&dhclient_driver);
375
376         dbus_bus_remove_match(connection, dhclient_rule, NULL);
377
378         dbus_connection_remove_filter(connection, dhclient_filter, NULL);
379
380         dbus_connection_unref(connection);
381 }
382
383 CONNMAN_PLUGIN_DEFINE(dhclient, "ISC DHCP client plugin", VERSION,
384                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, dhclient_init, dhclient_exit)