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