summaryrefslogtreecommitdiff
path: root/plugins/supplicant.c
blob: de74ce56c3aacdd8c625a8c0df3adcfbc70d0707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <net/if.h>

#include <glib.h>

#include "supplicant.h"

struct supplicant_task {
	GPid pid;
	int ifindex;
	char *ifname;
	struct connman_iface *iface;
	int socket;
	GIOChannel *channel;
};

static GSList *tasks = NULL;

static struct supplicant_task *find_task(int ifindex)
{
	GSList *list;

	for (list = tasks; list; list = list->next) {
		struct supplicant_task *task = list->data;

		if (task->ifindex == ifindex) 
			return task;
	}

	return NULL;
}

static int exec_cmd(struct supplicant_task *task, char *cmd)
{
	return write(task->socket, cmd, strlen(cmd));
}

static gboolean control_event(GIOChannel *chan,
				GIOCondition cond, gpointer data)
{
	struct supplicant_task *task = data;
	char buf[256];
	gsize len;
	GIOError err;

	if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
		return FALSE;

	memset(buf, 0, sizeof(buf));

	err = g_io_channel_read(chan, buf, sizeof(buf), &len);
	if (err) {
		if (err == G_IO_ERROR_AGAIN)
			return TRUE;
		return FALSE;
	}

	if (buf[0] != '<')
		return TRUE;

	printf("[SUPPLICANT] %s\n", buf + 3);

	if (g_str_has_prefix(buf + 3, "CTRL-EVENT-CONNECTED") == TRUE) {
		printf("[SUPPLICANT] connected\n");
		connman_iface_update(task->iface,
					CONNMAN_IFACE_STATE_CARRIER);
	}

	if (g_str_has_prefix(buf + 3, "CTRL-EVENT-DISCONNECTED") == TRUE) {
		printf("[SUPPLICANT] disconnected\n");
	}

	if (g_str_has_prefix(buf + 3, "CTRL-EVENT-TERMINATING") == TRUE) {
		printf("[SUPPLICANT] terminating\n");
	}

	return TRUE;
}

static int open_control(struct supplicant_task *task)
{
	struct sockaddr_un addr;
	int sk;

	printf("[SUPPLICANT] open control for %s\n", task->ifname);

	sk = socket(PF_UNIX, SOCK_DGRAM, 0);
	if (sk < 0)
		return -1;

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	snprintf(addr.sun_path, sizeof(addr.sun_path),
					"%s/%s.cli", STATEDIR, task->ifname);
	//unlink(addr.sun_path);

	if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		close(sk);
		return -1;
	}

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	snprintf(addr.sun_path, sizeof(addr.sun_path),
					"%s/%s", STATEDIR, task->ifname);

	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		close(sk);
		return -1;
	}

	task->socket = sk;

	task->channel = g_io_channel_unix_new(sk);
	g_io_channel_set_close_on_unref(task->channel, TRUE);

	g_io_add_watch(task->channel,
			G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
						control_event, task);

	exec_cmd(task, "ATTACH");
	exec_cmd(task, "ADD_NETWORK");

	return 0;
}

int __supplicant_start(struct connman_iface *iface)
{
	struct ifreq ifr;
	struct supplicant_task *task;
	char *argv[9];
	int sk, err;

	sk = socket(PF_INET, SOCK_DGRAM, 0);
	if (sk < 0)
		return -EIO;

	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_ifindex = iface->index;

	err = ioctl(sk, SIOCGIFNAME, &ifr);

	close(sk);

	if (err < 0)
		return -EIO;

	printf("[SUPPLICANT] start %s\n", ifr.ifr_name);

	task = g_try_new0(struct supplicant_task, 1);
	if (task == NULL)
		return -ENOMEM;

	task->ifindex = iface->index;
	task->ifname = strdup(ifr.ifr_name);
	task->iface = iface;

	if (task->ifname == NULL) {
		g_free(task);
		return -ENOMEM;
	}

	argv[0] = "/sbin/wpa_supplicant";
	argv[1] = "-qq";
	argv[2] = "-C";
	argv[3] = STATEDIR;
	argv[4] = "-D";
	argv[5] = "wext";
	argv[6] = "-i";
	argv[7] = task->ifname;
	argv[8] = NULL;

	if (g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
				NULL, NULL, &task->pid, NULL) == FALSE) {
		printf("Failed to spawn wpa_supplicant\n");
		return -1;
	}

	tasks = g_slist_append(tasks, task);

	printf("[SUPPLICANT] executed with pid %d\n", task->pid);

	sleep(1);

	task->socket = -1;

	if (open_control(task) < 0)
		printf("[SUPPLICANT] control failed\n");

	return 0;
}

int __supplicant_stop(struct connman_iface *iface)
{
	struct supplicant_task *task;
	char pathname[PATH_MAX];

	task = find_task(iface->index);
	if (task == NULL)
		return -ENODEV;

	printf("[SUPPLICANT] stop %s\n", task->ifname);

	tasks = g_slist_remove(tasks, task);

	exec_cmd(task, "DISABLE_NETWORK 0");
	exec_cmd(task, "DETACH");

	sleep(1);

	kill(task->pid, SIGTERM);

	g_io_channel_shutdown(task->channel, TRUE, NULL);
	g_io_channel_unref(task->channel);

	snprintf(pathname, sizeof(pathname),
					"%s/%s.cli", STATEDIR, task->ifname);
	unlink(pathname);

	free(task->ifname);

	g_free(task);

	return 0;
}

int __supplicant_connect(struct connman_iface *iface,
				const char *network, const char *passphrase)
{
	struct supplicant_task *task;
	char cmd[128];

	task = find_task(iface->index);
	if (task == NULL)
		return -ENODEV;

	printf("[SUPPLICANT] connect %s\n", task->ifname);

	exec_cmd(task, "DISABLE_NETWORK 0");

	sprintf(cmd, "SET_NETWORK 0 ssid \"%s\"", network);
	exec_cmd(task, cmd);

	if (passphrase && strlen(passphrase) > 0) {
		exec_cmd(task, "SET_NETWORK 0 proto RSN WPA");
		exec_cmd(task, "SET_NETWORK 0 key_mgmt WPA-PSK");

		sprintf(cmd, "SET_NETWORK 0 psk \"%s\"", passphrase);
		exec_cmd(task, cmd);
	} else {
		exec_cmd(task, "SET_NETWORK 0 proto RSN WPA");
		exec_cmd(task, "SET_NETWORK 0 key_mgmt NONE");
	}

	exec_cmd(task, "ENABLE_NETWORK 0");

	return 0;
}