Fix IO handling for the control channel
[connman] / plugins / supplicant.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  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 <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <net/if.h>
35
36 #include <glib.h>
37
38 #include "supplicant.h"
39
40 struct supplicant_task {
41         GPid pid;
42         int ifindex;
43         char *ifname;
44         struct connman_iface *iface;
45         int socket;
46         GIOChannel *channel;
47 };
48
49 static GSList *tasks = NULL;
50
51 static struct supplicant_task *find_task(int ifindex)
52 {
53         GSList *list;
54
55         for (list = tasks; list; list = list->next) {
56                 struct supplicant_task *task = list->data;
57
58                 if (task->ifindex == ifindex) 
59                         return task;
60         }
61
62         return NULL;
63 }
64
65 static int exec_cmd(struct supplicant_task *task, char *cmd)
66 {
67         write(task->socket, cmd, strlen(cmd));
68
69         return 0;
70 }
71
72 static gboolean control_event(GIOChannel *chan,
73                                 GIOCondition cond, gpointer data)
74 {
75         struct supplicant_task *task = data;
76         char buf[256];
77         gsize len;
78         GIOError err;
79
80         if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
81                 return FALSE;
82
83         memset(buf, 0, sizeof(buf));
84
85         err = g_io_channel_read(chan, buf, sizeof(buf), &len);
86         if (err) {
87                 if (err == G_IO_ERROR_AGAIN)
88                         return TRUE;
89                 return FALSE;
90         }
91
92         if (buf[0] != '<')
93                 return TRUE;
94
95         printf("[SUPPLICANT] %s\n", buf + 3);
96
97         if (g_str_has_prefix(buf + 3, "CTRL-EVENT-CONNECTED") == TRUE) {
98                 printf("[SUPPLICANT] connected\n");
99                 connman_iface_update(task->iface,
100                                         CONNMAN_IFACE_STATE_CARRIER);
101         }
102
103         if (g_str_has_prefix(buf + 3, "CTRL-EVENT-DISCONNECTED") == TRUE) {
104                 printf("[SUPPLICANT] disconnected\n");
105         }
106
107         if (g_str_has_prefix(buf + 3, "CTRL-EVENT-TERMINATING") == TRUE) {
108                 printf("[SUPPLICANT] terminating\n");
109         }
110
111         return TRUE;
112 }
113
114 static int open_control(struct supplicant_task *task)
115 {
116         struct sockaddr_un addr;
117         int sk;
118
119         printf("[SUPPLICANT] open control for %s\n", task->ifname);
120
121         sk = socket(PF_UNIX, SOCK_DGRAM, 0);
122         if (sk < 0)
123                 return -1;
124
125         memset(&addr, 0, sizeof(addr));
126         addr.sun_family = AF_UNIX;
127         snprintf(addr.sun_path, sizeof(addr.sun_path),
128                                         "%s/%s.cli", STATEDIR, task->ifname);
129         //unlink(addr.sun_path);
130
131         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
132                 close(sk);
133                 return -1;
134         }
135
136         memset(&addr, 0, sizeof(addr));
137         addr.sun_family = AF_UNIX;
138         snprintf(addr.sun_path, sizeof(addr.sun_path),
139                                         "%s/%s", STATEDIR, task->ifname);
140
141         if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
142                 close(sk);
143                 return -1;
144         }
145
146         task->socket = sk;
147
148         task->channel = g_io_channel_unix_new(sk);
149         g_io_channel_set_close_on_unref(task->channel, TRUE);
150
151         g_io_add_watch(task->channel,
152                         G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
153                                                 control_event, task);
154
155         exec_cmd(task, "ATTACH");
156         exec_cmd(task, "ADD_NETWORK");
157
158         return 0;
159 }
160
161 int __supplicant_start(struct connman_iface *iface)
162 {
163         struct ifreq ifr;
164         struct supplicant_task *task;
165         char *argv[9];
166         int sk, err;
167
168         sk = socket(PF_INET, SOCK_DGRAM, 0);
169         if (sk < 0)
170                 return -EIO;
171
172         memset(&ifr, 0, sizeof(ifr));
173         ifr.ifr_ifindex = iface->index;
174
175         err = ioctl(sk, SIOCGIFNAME, &ifr);
176
177         close(sk);
178
179         if (err < 0)
180                 return -EIO;
181
182         printf("[SUPPLICANT] start %s\n", ifr.ifr_name);
183
184         task = g_try_new0(struct supplicant_task, 1);
185         if (task == NULL)
186                 return -ENOMEM;
187
188         task->ifindex = iface->index;
189         task->ifname = strdup(ifr.ifr_name);
190         task->iface = iface;
191
192         if (task->ifname == NULL) {
193                 g_free(task);
194                 return -ENOMEM;
195         }
196
197         argv[0] = "/sbin/wpa_supplicant";
198         argv[1] = "-qq";
199         argv[2] = "-C";
200         argv[3] = STATEDIR;
201         argv[4] = "-D";
202         argv[5] = "wext";
203         argv[6] = "-i";
204         argv[7] = task->ifname;
205         argv[8] = NULL;
206
207         if (g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
208                                 NULL, NULL, &task->pid, NULL) == FALSE) {
209                 printf("Failed to spawn wpa_supplicant\n");
210                 return -1;
211         }
212
213         tasks = g_slist_append(tasks, task);
214
215         printf("[SUPPLICANT] executed with pid %d\n", task->pid);
216
217         sleep(1);
218
219         task->socket = -1;
220
221         if (open_control(task) < 0)
222                 printf("[SUPPLICANT] control failed\n");
223
224         return 0;
225 }
226
227 int __supplicant_stop(struct connman_iface *iface)
228 {
229         struct supplicant_task *task;
230         char pathname[PATH_MAX];
231
232         task = find_task(iface->index);
233         if (task == NULL)
234                 return -ENODEV;
235
236         printf("[SUPPLICANT] stop %s\n", task->ifname);
237
238         tasks = g_slist_remove(tasks, task);
239
240         exec_cmd(task, "DISABLE_NETWORK 0");
241         exec_cmd(task, "DETACH");
242
243         sleep(1);
244
245         kill(task->pid, SIGTERM);
246
247         g_io_channel_shutdown(task->channel, TRUE, NULL);
248         g_io_channel_unref(task->channel);
249
250         snprintf(pathname, sizeof(pathname),
251                                         "%s/%s.cli", STATEDIR, task->ifname);
252         unlink(pathname);
253
254         free(task->ifname);
255
256         g_free(task);
257
258         return 0;
259 }
260
261 int __supplicant_connect(struct connman_iface *iface,
262                                 const char *network, const char *passphrase)
263 {
264         struct supplicant_task *task;
265         char cmd[128];
266
267         task = find_task(iface->index);
268         if (task == NULL)
269                 return -ENODEV;
270
271         printf("[SUPPLICANT] connect %s\n", task->ifname);
272
273         exec_cmd(task, "DISABLE_NETWORK 0");
274
275         sprintf(cmd, "SET_NETWORK 0 ssid \"%s\"", network);
276         exec_cmd(task, cmd);
277
278         if (passphrase && strlen(passphrase) > 0) {
279                 exec_cmd(task, "SET_NETWORK 0 proto RSN WPA");
280                 exec_cmd(task, "SET_NETWORK 0 key_mgmt WPA-PSK");
281
282                 sprintf(cmd, "SET_NETWORK 0 psk \"%s\"", passphrase);
283                 exec_cmd(task, cmd);
284         } else {
285                 exec_cmd(task, "SET_NETWORK 0 proto RSN WPA");
286                 exec_cmd(task, "SET_NETWORK 0 key_mgmt NONE");
287         }
288
289         exec_cmd(task, "ENABLE_NETWORK 0");
290
291         return 0;
292 }