612d02af30df07c38e35e93aa0c346da7a8fa183
[connman] / plugins / dhclient.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/wait.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <net/if.h>
37
38 #include <glib.h>
39 #include <gdbus.h>
40
41 #include <connman/plugin.h>
42 #include <connman/dhcp.h>
43
44 #define DHCLIENT_INTF "org.isc.dhclient"
45 #define DHCLIENT_PATH "/org/isc/dhclient"
46
47 static const char *busname;
48
49 struct dhclient_task {
50         GPid pid;
51         int ifindex;
52         char *ifname;
53         struct connman_iface *iface;
54 };
55
56 static GSList *tasks = NULL;
57
58 static struct dhclient_task *find_task_by_pid(GPid pid)
59 {
60         GSList *list;
61
62         for (list = tasks; list; list = list->next) {
63                 struct dhclient_task *task = list->data;
64
65                 if (task->pid == pid)
66                         return task;
67         }
68
69         return NULL;
70 }
71
72 static struct dhclient_task *find_task_by_index(int index)
73 {
74         GSList *list;
75
76         for (list = tasks; list; list = list->next) {
77                 struct dhclient_task *task = list->data;
78
79                 if (task->ifindex == index)
80                         return task;
81         }
82
83         return NULL;
84 }
85
86 static void kill_task(struct dhclient_task *task)
87 {
88         if (task->pid > 0)
89                 kill(task->pid, SIGTERM);
90 }
91
92 static void task_died(GPid pid, gint status, gpointer data)
93 {
94         struct dhclient_task *task = data;
95         char pathname[PATH_MAX];
96
97         if (WIFEXITED(status))
98                 printf("[DHCP] exit status %d for %s\n",
99                                         WEXITSTATUS(status), task->ifname);
100         else
101                 printf("[DHCP] signal %d killed %s\n",
102                                         WTERMSIG(status), task->ifname);
103
104         g_spawn_close_pid(pid);
105         task->pid = 0;
106
107         tasks = g_slist_remove(tasks, task);
108
109         snprintf(pathname, sizeof(pathname) - 1,
110                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
111         unlink(pathname);
112
113         snprintf(pathname, sizeof(pathname) - 1,
114                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
115         unlink(pathname);
116
117         free(task->ifname);
118
119         g_free(task);
120 }
121
122 static void task_setup(gpointer data)
123 {
124         struct dhclient_task *task = data;
125
126         printf("[DHCP] setup %s\n", task->ifname);
127 }
128
129 static int dhclient_request(struct connman_iface *iface)
130 {
131         struct ifreq ifr;
132         struct dhclient_task *task;
133         char *argv[16], *envp[1], address[128], pidfile[PATH_MAX];
134         char leases[PATH_MAX], config[PATH_MAX], script[PATH_MAX];
135         int sk, err;
136
137         sk = socket(PF_INET, SOCK_DGRAM, 0);
138         if (sk < 0)
139                 return -EIO;
140
141         memset(&ifr, 0, sizeof(ifr));
142         ifr.ifr_ifindex = iface->index;
143
144         err = ioctl(sk, SIOCGIFNAME, &ifr);
145
146         close(sk);
147
148         if (err < 0)
149                 return -EIO;
150
151         task = g_try_new0(struct dhclient_task, 1);
152         if (task == NULL)
153                 return -ENOMEM;
154
155         task->ifindex = iface->index;
156         task->ifname = strdup(ifr.ifr_name);
157         task->iface = iface;
158
159         if (task->ifname == NULL) {
160                 g_free(task);
161                 return -ENOMEM;
162         }
163
164         printf("[DHCP] request %s\n", task->ifname);
165
166         snprintf(address, sizeof(address) - 1, "BUSNAME=%s", busname);
167         snprintf(pidfile, sizeof(pidfile) - 1,
168                         "%s/dhclient.%s.pid", STATEDIR, task->ifname);
169         snprintf(leases, sizeof(leases) - 1,
170                         "%s/dhclient.%s.leases", STATEDIR, task->ifname);
171         snprintf(config, sizeof(config) - 1, "%s/dhclient.conf", SCRIPTDIR);
172         snprintf(script, sizeof(script) - 1, "%s/dhclient-script", SCRIPTDIR);
173
174         argv[0] = DHCLIENT;
175         argv[1] = "-d";
176         argv[2] = "-q";
177         argv[3] = "-n";
178         argv[4] = "-e";
179         argv[5] = address;
180         argv[6] = "-pf";
181         argv[7] = pidfile;
182         argv[8] = "-lf";
183         argv[9] = leases;
184         argv[10] = "-cf";
185         argv[11] = config;
186         argv[12] = "-sf";
187         argv[13] = script;
188         argv[14] = task->ifname;
189         argv[15] = NULL;
190
191         envp[0] = NULL;
192
193         if (g_spawn_async(NULL, argv, envp, G_SPAWN_DO_NOT_REAP_CHILD,
194                                 task_setup, task, &task->pid, NULL) == FALSE) {
195                 printf("Failed to spawn dhclient\n");
196                 return -1;
197         }
198
199         tasks = g_slist_append(tasks, task);
200
201         g_child_watch_add(task->pid, task_died, task);
202
203         printf("[DHCP] executed %s with pid %d\n", DHCLIENT, task->pid);
204
205         return 0;
206 }
207
208 static int dhclient_release(struct connman_iface *iface)
209 {
210         struct dhclient_task *task;
211
212         task = find_task_by_index(iface->index);
213         if (task == NULL)
214                 return -ENODEV;
215
216         printf("[DHCP] release %s\n", task->ifname);
217
218         tasks = g_slist_remove(tasks, task);
219
220         kill_task(task);
221
222         return 0;
223 }
224
225 static struct connman_dhcp_driver dhclient_driver = {
226         .name           = "dhclient",
227         .request        = dhclient_request,
228         .release        = dhclient_release,
229 };
230
231 static DBusHandlerResult dhclient_filter(DBusConnection *conn,
232                                                 DBusMessage *msg, void *data)
233 {
234         DBusMessageIter iter, dict;
235         dbus_uint32_t pid;
236         struct dhclient_task *task;
237         struct connman_ipv4 ipv4;
238         const char *text, *key, *value;
239
240         if (dbus_message_is_method_call(msg, DHCLIENT_INTF, "notify") == FALSE)
241                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
242
243         memset(&ipv4, 0, sizeof(ipv4));
244
245         dbus_message_iter_init(msg, &iter);
246
247         dbus_message_iter_get_basic(&iter, &pid);
248         dbus_message_iter_next(&iter);
249
250         dbus_message_iter_get_basic(&iter, &text);
251         dbus_message_iter_next(&iter);
252
253         printf("[DHCP] change %d to %s\n", pid, text);
254
255         task = find_task_by_pid(pid);
256         if (task == NULL)
257                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
258
259         dbus_message_iter_recurse(&iter, &dict);
260
261         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
262                 DBusMessageIter entry;
263
264                 dbus_message_iter_recurse(&dict, &entry);
265                 dbus_message_iter_get_basic(&entry, &key);
266                 dbus_message_iter_next(&entry);
267                 dbus_message_iter_get_basic(&entry, &value);
268
269                 printf("[DHCP] %s = %s\n", key, value);
270
271                 if (strcmp(key, "new_ip_address") == 0)
272                         inet_aton(value, &ipv4.address);
273
274                 if (strcmp(key, "new_subnet_mask") == 0)
275                         inet_aton(value, &ipv4.netmask);
276
277                 if (strcmp(key, "new_routers") == 0)
278                         inet_aton(value, &ipv4.gateway);
279
280                 if (strcmp(key, "new_network_number") == 0)
281                         inet_aton(value, &ipv4.network);
282
283                 if (strcmp(key, "new_broadcast_address") == 0)
284                         inet_aton(value, &ipv4.broadcast);
285
286                 if (strcmp(key, "new_domain_name_servers") == 0)
287                         inet_aton(value, &ipv4.nameserver);
288
289                 dbus_message_iter_next(&dict);
290         }
291
292         if (strcmp(text, "PREINIT") == 0)
293                 connman_dhcp_update(task->iface,
294                                         CONNMAN_DHCP_STATE_INIT, &ipv4);
295         else if (strcmp(text, "BOUND") == 0 || strcmp(text, "REBOOT") == 0)
296                 connman_dhcp_update(task->iface,
297                                         CONNMAN_DHCP_STATE_BOUND, &ipv4);
298         else if (strcmp(text, "RENEW") == 0 || strcmp(text, "REBIND") == 0)
299                 connman_dhcp_update(task->iface,
300                                         CONNMAN_DHCP_STATE_RENEW, &ipv4);
301         else
302                 connman_dhcp_update(task->iface,
303                                         CONNMAN_DHCP_STATE_FAILED, NULL);
304
305         return DBUS_HANDLER_RESULT_HANDLED;
306 }
307
308 static DBusConnection *connection;
309
310 static int plugin_init(void)
311 {
312         gchar *filter;
313
314         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
315
316         busname = dbus_bus_get_unique_name(connection);
317
318         busname = "org.freedesktop.connman";
319
320         dbus_connection_add_filter(connection, dhclient_filter, NULL, NULL);
321
322         filter = g_strdup_printf("interface=%s,path=%s",
323                                                 DHCLIENT_INTF, DHCLIENT_PATH);
324
325         dbus_bus_add_match(connection, filter, NULL);
326
327         g_free(filter);
328
329         connman_dhcp_register(&dhclient_driver);
330
331         return 0;
332 }
333
334 static void plugin_exit(void)
335 {
336         GSList *list;
337
338         for (list = tasks; list; list = list->next) {
339                 struct dhclient_task *task = list->data;
340
341                 printf("[DHCP] killing process %d\n", task->pid);
342
343                 kill_task(task);
344         }
345
346         g_slist_free(tasks);
347
348         connman_dhcp_unregister(&dhclient_driver);
349
350         dbus_connection_unref(connection);
351 }
352
353 CONNMAN_PLUGIN_DEFINE("dhclient", "ISC DHCP client plugin", VERSION,
354                                                 plugin_init, plugin_exit)