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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* dhcpcd-dbus
* Copyright 2009-2014 Roy Marples <roy@marples.name>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "config.h"
#include "dhcpcd-dbus.h"
#include "eloop.h"
#include "wpa.h"
#include "wpa-dbus.h"
#ifndef SUN_LEN
#define SUN_LEN(su) \
(sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
#endif
struct if_sock {
char *iface;
int cmd_fd;
char *cmd_path;
int ctrl_fd;
char *ctrl_path;
int attached;
struct if_sock *next;
};
static struct if_sock *socks;
static void wpa_init(void *arg);
static int
_wpa_open(const char *iface, char **path)
{
static int counter;
int fd;
socklen_t len;
struct sockaddr_un sun;
if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
return -1;
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
snprintf(sun.sun_path, sizeof(sun.sun_path),
"/tmp/" PACKAGE "-wpa-%d.%d", getpid(), counter++);
*path = strdup(sun.sun_path);
len = (socklen_t)SUN_LEN(&sun);
if (bind(fd, (struct sockaddr *)&sun, len) == -1) {
close(fd);
unlink(*path);
free(*path);
*path = NULL;
return -1;
}
snprintf(sun.sun_path, sizeof(sun.sun_path),
WPA_CTRL_DIR "/%s", iface);
len = (socklen_t)SUN_LEN(&sun);
if (connect(fd, (struct sockaddr *)&sun, len) == 0) {
set_nonblock(fd);
return fd;
}
close(fd);
unlink(*path);
free(*path);
*path = NULL;
return -1;
}
static struct if_sock *
find_if_sock(const char *iface)
{
struct if_sock *ifs;
for (ifs = socks; ifs; ifs = ifs->next) {
if (strcmp(ifs->iface, iface) == 0)
return ifs;
}
return NULL;
}
static ssize_t
_wpa_cmd(int fd, const char *cmd, char *buffer, size_t len)
{
int retval;
ssize_t bytes;
struct pollfd pfd;
if (buffer)
*buffer = '\0';
bytes = write(fd, cmd, strlen(cmd));
if (bytes == -1 || bytes == 0)
return -1;
if (buffer == NULL || len == 0)
return 0;
pfd.fd = fd;
pfd.events = POLLIN | POLLHUP;
pfd.revents = 0;
retval = poll(&pfd, 1, 2000);
if (retval == -1) {
syslog(LOG_ERR, "poll: %m");
return -1;
}
if (retval == 0 || !(pfd.revents & (POLLIN | POLLHUP)))
return -1;
bytes = read(fd, buffer, len == 1 ? 1 : len - 1);
if (bytes != -1)
buffer[bytes] = '\0';
return bytes;
}
ssize_t
wpa_cmd(const char *iface, const char *cmd, char *buffer, size_t len)
{
struct if_sock *ifs;
ifs = find_if_sock(iface);
if (ifs == NULL)
return -1;
return _wpa_cmd(ifs->cmd_fd, cmd, buffer, len);
}
static int
attach_detach(struct if_sock *ifs, int attach)
{
char buffer[10];
if (ifs->attached == attach)
return 0;
if (attach > 0) {
if (_wpa_cmd(ifs->ctrl_fd, "ATTACH", buffer, sizeof(buffer)) == -1)
return -1;
if (strcmp(buffer, "OK\n") != 0)
return -1;
} else {
if (_wpa_cmd(ifs->ctrl_fd, "DETACH", NULL, 0) == -1)
return -1;
}
ifs->attached = attach;
return 0;
}
static struct if_sock *
wpa_open(const char *iface)
{
struct if_sock *ifs = NULL;
int cmd_fd = -1, ctrl_fd = -1;
char *cmd_path = NULL, *ctrl_path = NULL;
cmd_fd = _wpa_open(iface, &cmd_path);
if (cmd_fd == -1)
goto fail;
ctrl_fd = _wpa_open(iface, &ctrl_path);
if (ctrl_fd == -1)
goto fail;
ifs = malloc(sizeof(*ifs));
if (ifs == NULL)
goto fail;
ifs->iface = strdup(iface);
if (ifs->iface == NULL)
goto fail;
ifs->cmd_fd = cmd_fd;
ifs->cmd_path = cmd_path;
ifs->ctrl_fd = ctrl_fd;
ifs->ctrl_path = ctrl_path;
ifs->next = socks;
ifs->attached = 0;
socks = ifs;
return ifs;
fail:
if (cmd_fd != -1)
close(cmd_fd);
if (ctrl_fd != -1)
close(ctrl_fd);
free(cmd_path);
free(ctrl_path);
free(ifs);
return NULL;
}
int
wpa_close(const char *iface)
{
struct if_sock *ifs, *ifn, *ifl;
int retval;
ifl = NULL;
retval = 0;
for (ifs = socks; ifs && (ifn = ifs->next, 1); ifs = ifn) {
if (iface == NULL || strcmp(ifs->iface, iface) == 0) {
attach_detach(ifs, -1);
delete_event(ifs->ctrl_fd);
delete_timeout(NULL, ifs);
retval |= shutdown(ifs->ctrl_fd, SHUT_RDWR);
retval |= shutdown(ifs->cmd_fd, SHUT_RDWR);
free(ifs->iface);
unlink(ifs->cmd_path);
free(ifs->cmd_path);
unlink(ifs->ctrl_path);
free(ifs->ctrl_path);
if (ifl == NULL)
socks = ifs->next;
else
ifl->next = ifs->next;
free(ifs);
if (iface != NULL)
break;
}
}
return retval;
}
static ssize_t
read_event(const struct if_sock *ifs)
{
char buffer[256], *p;
size_t bytes;
bytes = (size_t)read(ifs->ctrl_fd, buffer, sizeof(buffer));
if ((ssize_t)bytes == -1 || bytes == 0)
return (ssize_t)bytes;
buffer[bytes] = '\0';
bytes = strlen(buffer);
if (buffer[bytes - 1] == ' ')
buffer[--bytes] = '\0';
for (p = buffer + 1; *p != '\0'; p++)
if (*p == '>') {
p++;
break;
}
if (strcmp(p, "CTRL-EVENT-SCAN-RESULTS") == 0)
wpa_dbus_signal_scan_results(ifs->iface);
return (ssize_t)bytes;
}
static void
handle_wpa(void *ifs)
{
read_event((const struct if_sock *)ifs);
}
static void
ping(void *arg)
{
char buffer[20];
struct if_sock *ifs;
struct dhcpcd_config *c;
ifs = (struct if_sock *)arg;
if (_wpa_cmd(ifs->cmd_fd, "PING", buffer, sizeof(buffer)) > 0 &&
strncmp(buffer, "PONG\n", 5) == 0) {
add_timeout_sec(1, ping, ifs);
return;
}
syslog(LOG_ERR, "lost connection to wpa_supplicant on interface %s",
ifs->iface);
c = dhcpcd_get_config(ifs->iface, "link");
wpa_close(c->iface);
add_timeout_sec(1, wpa_init, (void *)UNCONST(c->iface));
}
static void
wpa_init(void *arg)
{
const char *iface;
struct if_sock *ifs;
iface = (const char *)arg;
ifs = wpa_open(iface);
if (ifs == NULL) {
add_timeout_sec(1, wpa_init, arg);
return;
}
if (attach_detach(ifs, 1) != 0) {
wpa_close(iface);
add_timeout_sec(1, wpa_init, arg);
return;
}
add_event(ifs->ctrl_fd, handle_wpa, ifs);
add_timeout_sec(1, ping, ifs);
syslog(LOG_INFO, "connected to wpa_supplicant on interface %s", iface);
wpa_dbus_signal_scan_results(iface);
}
int
wpa_configure(const struct dhcpcd_config *c)
{
const char *p;
p = dhcpcd_get_value(c, "ifwireless=");
if (p == NULL || *p == '\0' || *p == '0')
return 0;
p = dhcpcd_get_value(c, "reason=");
if (p == NULL)
return 0;
if (strcmp(p, "STOP") == 0 ||
strcmp(p, "RELEASE") == 0)
return wpa_close(c->iface);
if (find_if_sock(c->iface) == NULL)
wpa_init((void *)UNCONST(c->iface));
if (find_if_sock(c->iface) == NULL)
return 0;
return -1;
}
|