3 * Copyright 2009-2014 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/socket.h>
47 (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
50 #define CLAMP(x, low, high) \
51 (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
54 wpa_open(const char *ifname, char **path)
59 struct sockaddr_un sun;
61 if ((fd = socket(AF_UNIX,
62 SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) == -1)
64 memset(&sun, 0, sizeof(sun));
65 sun.sun_family = AF_UNIX;
66 snprintf(sun.sun_path, sizeof(sun.sun_path),
67 "/tmp/libdhcpcd-wpa-%d.%d", getpid(), counter++);
68 *path = strdup(sun.sun_path);
69 len = (socklen_t)SUN_LEN(&sun);
70 if (bind(fd, (struct sockaddr *)&sun, len) == -1) {
77 snprintf(sun.sun_path, sizeof(sun.sun_path),
78 WPA_CTRL_DIR "/%s", ifname);
79 len = (socklen_t)SUN_LEN(&sun);
80 if (connect(fd, (struct sockaddr *)&sun, len) == -1) {
92 wpa_cmd(int fd, const char *cmd, char *buffer, size_t len)
100 bytes = write(fd, cmd, strlen(cmd));
101 if (bytes == -1 || bytes == 0)
103 if (buffer == NULL || len == 0)
106 pfd.events = POLLIN | POLLHUP;
108 retval = poll(&pfd, 1, 2000);
111 if (retval == 0 || !(pfd.revents & (POLLIN | POLLHUP)))
114 bytes = read(fd, buffer, len == 1 ? 1 : len - 1);
116 buffer[bytes] = '\0';
121 dhcpcd_wpa_command(DHCPCD_WPA *wpa, const char *cmd)
126 bytes = wpa_cmd(wpa->command_fd, cmd, buf, sizeof(buf));
127 return (bytes == -1 || bytes == 0 ||
128 strcmp(buf, "OK\n")) ? false : true;
132 dhcpcd_wpa_command_arg(DHCPCD_WPA *wpa, const char *cmd, const char *arg)
136 cmdlen = strlen(cmd);
137 nlen = cmdlen + strlen(arg) + 2;
138 if (!dhcpcd_realloc(wpa->con, nlen))
140 strlcpy(wpa->con->buf, cmd, wpa->con->buflen);
141 wpa->con->buf[cmdlen] = ' ';
142 strlcpy(wpa->con->buf + cmdlen + 1, arg, wpa->con->buflen - 1 - cmdlen);
143 return dhcpcd_wpa_command(wpa, wpa->con->buf);
147 dhcpcd_attach_detach(DHCPCD_WPA *wpa, bool attach)
152 if (wpa->attached == attach)
155 bytes = wpa_cmd(wpa->listen_fd, attach > 0 ? "ATTACH" : "DETACH",
157 if (bytes == -1 || bytes == 0 || strcmp(buf, "OK\n"))
160 wpa->attached = attach;
165 dhcpcd_wpa_scan(DHCPCD_WPA *wpa)
168 return dhcpcd_wpa_command(wpa, "SCAN");
172 dhcpcd_wi_scans_free(DHCPCD_WI_SCAN *wis)
184 dhcpcd_strtoi(int *val, const char *s)
188 l = strtol(s, NULL, 0);
189 if (l >= INT_MIN && l <= INT_MAX)
196 dhcpcd_wpa_hex2num(char c)
199 if (c >= '0' && c <= '9')
201 if (c >= 'a' && c <= 'f')
203 if (c >= 'A' && c <= 'F')
209 dhcpcd_wpa_hex2byte(const char *src)
213 if ((h = dhcpcd_wpa_hex2num(*src++)) == -1 ||
214 (l = dhcpcd_wpa_hex2num(*src)) == -1)
220 dhcpcd_wpa_decode_ssid(char *dst, size_t dlen, const char *src)
244 case '"': *dst++ = esc; break;
245 case 'n': *dst++ = '\n'; break;
246 case 'r': *dst++ = '\r'; break;
247 case 't': *dst++ = '\t'; break;
248 case 'e': *dst++ = '\033'; break;
250 if (src[0] == '\0' || src[1] == '\0') {
254 if ((xb = dhcpcd_wpa_hex2byte(src)) == -1)
259 default: errno = EINVAL; return -1;
261 default: *dst++ = c; break;
272 static DHCPCD_WI_SCAN *
273 dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
277 DHCPCD_WI_SCAN *wis, *w, *l;
278 char *s, *p, buf[32];
279 char wssid[sizeof(w->ssid)];
282 if (!dhcpcd_realloc(wpa->con, 2048))
285 for (i = 0; i < 1000; i++) {
286 snprintf(buf, sizeof(buf), "BSS %zu", i);
287 bytes = wpa_cmd(wpa->command_fd, buf,
288 wpa->con->buf, wpa->con->buflen);
289 if (bytes == 0 || bytes == -1 ||
290 strncmp(wpa->con->buf, "FAIL", 4) == 0)
293 w = calloc(1, sizeof(*w));
298 while ((s = strsep(&p, "\n"))) {
301 if (strncmp(s, "bssid=", 6) == 0)
302 strlcpy(w->bssid, s + 6, sizeof(w->bssid));
303 else if (strncmp(s, "freq=", 5) == 0)
304 dhcpcd_strtoi(&w->frequency, s + 5);
305 // else if (strncmp(s, "beacon_int=", 11) == 0)
307 else if (strncmp(s, "qual=", 5) == 0)
308 dhcpcd_strtoi(&w->quality.value, s + 5);
309 else if (strncmp(s, "noise=", 6) == 0)
310 dhcpcd_strtoi(&w->noise.value, s + 6);
311 else if (strncmp(s, "level=", 6) == 0)
312 dhcpcd_strtoi(&w->level.value, s + 6);
313 else if (strncmp(s, "flags=", 6) == 0)
314 strlcpy(w->wpa_flags, s + 6,
315 sizeof(w->wpa_flags));
316 else if (strncmp(s, "ssid=", 5) == 0) {
317 /* Decode it from \xNN to \NNN
318 * so we're consistent */
319 dl = dhcpcd_wpa_decode_ssid(wssid,
320 sizeof(wssid), s + 5);
323 dl = dhcpcd_encode_string_escape(w->ssid,
324 sizeof(w->ssid), wssid, (size_t)dl);
340 if ((proto = strstr(w->wpa_flags, "[WPA-")) ||
341 (proto = strstr(w->wpa_flags, "[WPA2-")) ||
342 (proto = strstr(w->wpa_flags, "[RSN-")))
344 const char *endp, *psk;
346 w->flags = WSF_WPA | WSF_SECURE;
347 endp = strchr(proto, ']');
348 if ((psk = strstr(proto, "-PSK]")) ||
349 (psk = strstr(proto, "-PSK-")) ||
350 (psk = strstr(proto, "-PSK+")))
356 if (strstr(w->wpa_flags, "[WEP]"))
357 w->flags = WSF_WEP | WSF_PSK | WSF_SECURE;
359 w->strength.value = w->level.value;
360 if (w->strength.value > 110 && w->strength.value < 256)
361 /* Convert WEXT level to dBm */
362 w->strength.value -= 256;
364 if (w->strength.value < 0) {
367 abs(CLAMP(w->strength.value, -100, -40) + 40);
369 100 - ((100 * w->strength.value) / 60);
371 /* Assume quality percentage */
372 w->strength.value = CLAMP(w->strength.value, 0, 100);
379 dhcpcd_wi_scan_compare(DHCPCD_WI_SCAN *a, DHCPCD_WI_SCAN *b)
383 /* Fist sort non case sensitive, then case sensitive */
384 cmp = strcasecmp(a->ssid, b->ssid);
386 cmp = strcmp(a->ssid, b->ssid);
388 /* If still the same, return strongest first */
390 cmp = b->strength.value - a->strength.value;
396 * This function is copyright 2001 Simon Tatham.
398 * Permission is hereby granted, free of charge, to any person
399 * obtaining a copy of this software and associated documentation
400 * files (the "Software"), to deal in the Software without
401 * restriction, including without limitation the rights to use,
402 * copy, modify, merge, publish, distribute, sublicense, and/or
403 * sell copies of the Software, and to permit persons to whom the
404 * Software is furnished to do so, subject to the following
407 * The above copyright notice and this permission notice shall be
408 * included in all copies or substantial portions of the Software.
410 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
411 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
412 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
413 * NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR
414 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
415 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
416 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
419 static DHCPCD_WI_SCAN *
420 dhcpcd_wi_scans_sort(DHCPCD_WI_SCAN *list)
422 DHCPCD_WI_SCAN *p, *q, *e, *tail;
423 size_t insize, nmerges, psize, qsize, i;
425 /* Silly special case: if `list' was passed in as NULL, return
426 * NULL immediately. */
435 nmerges = 0; /* count number of merges we do in this pass */
438 nmerges++; /* there exists a merge to be done */
439 /* step `insize' places along from p */
442 for (i = 0; i < insize; i++) {
449 /* if q hasn't fallen off end,
450 * we have two lists to merge */
453 /* now we have two lists; merge them */
454 while (psize > 0 || (qsize > 0 && q)) {
455 /* decide whether next element of merge comes
458 /* p is empty; e must come from q. */
459 e = q; q = q->next; qsize--;
460 } else if (qsize == 0 || !q) {
461 /* q is empty; e must come from p. */
462 e = p; p = p->next; psize--;
463 } else if (dhcpcd_wi_scan_compare(p, q) <= 0) {
464 /* First element of p is lower
465 * (or same); e must come from p. */
466 e = p; p = p->next; psize--;
468 /* First element of q is lower;
469 * e must come from q. */
470 e = q; q = q->next; qsize--;
473 /* add the next element to the merged list */
481 /* now p has stepped `insize' places along,
487 /* If we have done only one merge, we're finished. */
488 if (nmerges <= 1) /* allow for nmerges==0, the empty list */
491 /* Otherwise repeat, merging lists twice the size */
497 dhcpcd_wi_scans(DHCPCD_IF *i)
500 DHCPCD_WI_SCAN *wis, *w, *n, *p;
502 DHCPCD_WI_HIST *h, *hl;
504 wpa = dhcpcd_wpa_find(i->con, i->ifname);
507 wis = dhcpcd_wpa_scans_read(wpa);
509 /* Sort the resultant list alphabetically and then by strength */
510 wis = dhcpcd_wi_scans_sort(wis);
513 for (w = wis; w && (n = w->next, 1); w = n) {
514 /* Currently we don't support non SSID broadcasting APs */
515 if (*w->ssid == '\0') {
523 /* Strip duplicated SSIDs, only show the strongest */
524 if (p && strcmp(p->ssid, w->ssid) == 0) {
529 /* Remember this as the previos next time */
534 w->quality.average = w->quality.value;
535 w->noise.average = w->noise.value;
536 w->level.average = w->level.value;
537 w->strength.average = w->strength.value;
539 for (h = wpa->con->wi_history; h; h = h->next) {
540 if (strcmp(h->ifname, i->ifname) == 0 &&
541 strcmp(h->bssid, wis->bssid) == 0)
543 w->quality.average += h->quality;
544 w->noise.average += h->noise;
545 w->level.average += h->level;
546 w->strength.average += h->strength;
547 if (++nh == DHCPCD_WI_HIST_MAX) {
557 w->quality.average /= nh;
558 w->noise.average /= nh;
559 w->level.average /= nh;
560 w->strength.average /= nh;
562 h = malloc(sizeof(*h));
564 strlcpy(h->ifname, i->ifname, sizeof(h->ifname));
565 strlcpy(h->bssid, w->bssid, sizeof(h->bssid));
566 h->quality = w->quality.value;
567 h->noise = w->noise.value;
568 h->level = w->level.value;
569 h->strength = w->strength.value;
570 h->next = wpa->con->wi_history;
571 wpa->con->wi_history = h;
579 dhcpcd_wpa_reconfigure(DHCPCD_WPA *wpa)
582 return dhcpcd_wpa_command(wpa, "RECONFIGURE");
586 dhcpcd_wpa_reassociate(DHCPCD_WPA *wpa)
589 return dhcpcd_wpa_command(wpa, "REASSOCIATE");
593 dhcpcd_wpa_disconnect(DHCPCD_WPA *wpa)
596 return dhcpcd_wpa_command(wpa, "DISCONNECT");
600 dhcpcd_wpa_config_write(DHCPCD_WPA *wpa)
603 return dhcpcd_wpa_command(wpa, "SAVE_CONFIG");
607 dhcpcd_wpa_network(DHCPCD_WPA *wpa, const char *cmd, int id)
611 len = strlen(cmd) + 32;
612 if (!dhcpcd_realloc(wpa->con, len))
614 snprintf(wpa->con->buf, wpa->con->buflen, "%s %d", cmd, id);
615 return dhcpcd_wpa_command(wpa, wpa->con->buf);
619 dhcpcd_wpa_network_disable(DHCPCD_WPA *wpa, int id)
622 return dhcpcd_wpa_network(wpa, "DISABLE_NETWORK", id);
626 dhcpcd_wpa_network_enable(DHCPCD_WPA *wpa, int id)
629 return dhcpcd_wpa_network(wpa, "ENABLE_NETWORK", id);
633 dhcpcd_wpa_network_select(DHCPCD_WPA *wpa, int id)
636 return dhcpcd_wpa_network(wpa, "SELECT_NETWORK", id);
640 dhcpcd_wpa_network_remove(DHCPCD_WPA *wpa, int id)
643 return dhcpcd_wpa_network(wpa, "REMOVE_NETWORK", id);
647 dhcpcd_wpa_network_get(DHCPCD_WPA *wpa, int id, const char *param)
651 if (!dhcpcd_realloc(wpa->con, 2048))
653 snprintf(wpa->con->buf, wpa->con->buflen, "GET_NETWORK %d %s",
655 bytes = wpa_cmd(wpa->command_fd, wpa->con->buf,
656 wpa->con->buf, wpa->con->buflen);
657 if (bytes == 0 || bytes == -1)
659 if (strcmp(wpa->con->buf, "FAIL\n") == 0) {
663 return wpa->con->buf;
667 dhcpcd_wpa_network_set(DHCPCD_WPA *wpa, int id,
668 const char *param, const char *value)
672 len = strlen("SET_NETWORK") + 32 + strlen(param) + strlen(value) + 3;
673 if (!dhcpcd_realloc(wpa->con, len))
675 snprintf(wpa->con->buf, wpa->con->buflen, "SET_NETWORK %d %s %s",
677 return dhcpcd_wpa_command(wpa, wpa->con->buf);
681 dhcpcd_wpa_network_find(DHCPCD_WPA *wpa, const char *fssid)
683 ssize_t bytes, dl, tl;
685 char *s, *t, *ssid, *bssid, *flags;
686 char dssid[IF_SSIDSIZE], tssid[IF_SSIDSIZE];
689 dhcpcd_realloc(wpa->con, 2048);
690 bytes = wpa_cmd(wpa->command_fd, "LIST_NETWORKS",
691 wpa->con->buf, wpa->con->buflen);
692 if (bytes == 0 || bytes == -1)
697 s = strchr(wpa->con->buf, '\n');
700 while ((t = strsep(&s, "\b\n"))) {
703 ssid = strchr(t, '\t');
707 bssid = strchr(ssid, '\t');
711 flags = strchr(bssid, '\t');
715 l = strtol(t, NULL, 0);
716 if (l < 0 || l > INT_MAX) {
721 /* Decode the wpa_supplicant SSID into raw chars and
722 * then encode into our octal escaped string to
724 dl = dhcpcd_wpa_decode_ssid(dssid, sizeof(dssid), ssid);
727 tl = dhcpcd_encode_string_escape(tssid,
728 sizeof(tssid), dssid, (size_t)dl);
731 if ((size_t)tl == fl && memcmp(tssid, fssid, (size_t)tl) == 0)
739 dhcpcd_wpa_network_new(DHCPCD_WPA *wpa)
744 dhcpcd_realloc(wpa->con, 32);
745 bytes = wpa_cmd(wpa->command_fd, "ADD_NETWORK",
746 wpa->con->buf, sizeof(wpa->con->buf));
747 if (bytes == 0 || bytes == -1)
749 l = strtol(wpa->con->buf, NULL, 0);
750 if (l < 0 || l > INT_MAX) {
757 static const char hexchrs[] = "0123456789abcdef";
759 dhcpcd_wpa_network_find_new(DHCPCD_WPA *wpa, const char *ssid)
762 char dssid[IF_SSIDSIZE], essid[IF_SSIDSIZE], *ep;
766 id = dhcpcd_wpa_network_find(wpa, ssid);
770 dl = dhcpcd_decode_string_escape(dssid, sizeof(dssid), ssid);
774 for (i = 0; i < dl; i++) {
775 if (!isascii((int)dssid[i]) && !isprint((int)dssid[i]))
781 /* Non standard characters found! Encode as hex string */
785 c = (unsigned char)*dp++;
786 *ep++ = hexchrs[(c & 0xf0) >> 4];
787 *ep++ = hexchrs[(c & 0x0f)];
793 while (*++dp != '\0');
798 id = dhcpcd_wpa_network_new(wpa);
800 dhcpcd_wpa_network_set(wpa, id, "ssid", essid);
805 dhcpcd_wpa_close(DHCPCD_WPA *wpa)
810 if (wpa->command_fd == -1 || !wpa->open)
814 dhcpcd_attach_detach(wpa, false);
815 shutdown(wpa->command_fd, SHUT_RDWR);
816 shutdown(wpa->listen_fd, SHUT_RDWR);
818 if (wpa->con->wpa_status_cb)
819 wpa->con->wpa_status_cb(wpa, "down",
820 wpa->con->wpa_status_context);
822 close(wpa->command_fd);
823 wpa->command_fd = -1;
824 close(wpa->listen_fd);
826 unlink(wpa->command_path);
827 free(wpa->command_path);
828 wpa->command_path = NULL;
829 unlink(wpa->listen_path);
830 free(wpa->listen_path);
831 wpa->listen_path = NULL;
835 dhcpcd_wpa_find(DHCPCD_CONNECTION *con, const char *ifname)
839 for (wpa = con->wpa; wpa; wpa = wpa->next) {
840 if (strcmp(wpa->ifname, ifname) == 0)
848 dhcpcd_wpa_new(DHCPCD_CONNECTION *con, const char *ifname)
852 wpa = dhcpcd_wpa_find(con, ifname);
856 wpa = malloc(sizeof(*wpa));
861 strlcpy(wpa->ifname, ifname, sizeof(wpa->ifname));
862 wpa->command_fd = wpa->listen_fd = -1;
863 wpa->command_path = wpa->listen_path = NULL;
864 wpa->next = con->wpa;
870 dhcpcd_wpa_connection(DHCPCD_WPA *wpa)
878 dhcpcd_wpa_if(DHCPCD_WPA *wpa)
881 return dhcpcd_get_if(wpa->con, wpa->ifname, "link");
885 dhcpcd_wpa_open(DHCPCD_WPA *wpa)
887 int cmd_fd, list_fd = -1;
888 char *cmd_path = NULL, *list_path = NULL;
890 if (wpa->listen_fd != -1) {
895 return wpa->listen_fd;
898 cmd_fd = wpa_open(wpa->ifname, &cmd_path);
902 list_fd = wpa_open(wpa->ifname, &list_path);
907 wpa->attached = false;
908 wpa->command_fd = cmd_fd;
909 wpa->command_path = cmd_path;
910 wpa->listen_fd = list_fd;
911 wpa->listen_path = list_path;
912 if (!dhcpcd_attach_detach(wpa, true)) {
913 dhcpcd_wpa_close(wpa);
917 if (wpa->con->wi_scanresults_cb)
918 wpa->con->wi_scanresults_cb(wpa,
919 wpa->con->wi_scanresults_context);
921 return wpa->listen_fd;
937 dhcpcd_wpa_get_fd(DHCPCD_WPA *wpa)
941 return wpa->open ? wpa->listen_fd : -1;
945 dhcpcd_wpa_set_scan_callback(DHCPCD_CONNECTION *con,
946 void (*cb)(DHCPCD_WPA *, void *), void *context)
950 con->wi_scanresults_cb = cb;
951 con->wi_scanresults_context = context;
955 void dhcpcd_wpa_set_status_callback(DHCPCD_CONNECTION * con,
956 void (*cb)(DHCPCD_WPA *, const char *, void *), void *context)
960 con->wpa_status_cb = cb;
961 con->wpa_status_context = context;
965 dhcpcd_wpa_dispatch(DHCPCD_WPA *wpa)
967 char buffer[256], *p;
971 bytes = (size_t)read(wpa->listen_fd, buffer, sizeof(buffer));
972 if ((ssize_t)bytes == -1 || bytes == 0) {
973 dhcpcd_wpa_close(wpa);
977 buffer[bytes] = '\0';
978 bytes = strlen(buffer);
979 if (buffer[bytes - 1] == ' ')
980 buffer[--bytes] = '\0';
981 for (p = buffer + 1; *p != '\0'; p++)
986 if (strcmp(p, "CTRL-EVENT-SCAN-RESULTS") == 0 &&
987 wpa->con->wi_scanresults_cb)
988 wpa->con->wi_scanresults_cb(wpa,
989 wpa->con->wi_scanresults_context);
994 dhcpcd_wpa_if_event(DHCPCD_IF *i)
999 if (strcmp(i->type, "link") == 0) {
1000 if (strcmp(i->reason, "STOPPED") == 0 ||
1001 strcmp(i->reason, "DEPARTED") == 0)
1003 wpa = dhcpcd_wpa_find(i->con, i->ifname);
1005 dhcpcd_wpa_close(wpa);
1006 } else if (i->wireless && i->con->wpa_started) {
1007 wpa = dhcpcd_wpa_new(i->con, i->ifname);
1008 if (wpa && wpa->listen_fd == -1)
1009 dhcpcd_wpa_open(wpa);
1015 dhcpcd_wpa_start(DHCPCD_CONNECTION *con)
1020 con->wpa_started = true;
1022 for (i = con->interfaces; i; i = i->next)
1023 dhcpcd_wpa_if_event(i);
1027 dhcpcd_wpa_configure_psk1(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
1029 const char *mgmt, *var;
1035 if (!dhcpcd_wpa_disconnect(wpa))
1036 return DHCPCD_WPA_ERR_DISCONN;
1038 /* reload the configuration so that when we don't save
1039 * the disabled networks to the config file. */
1040 if (!dhcpcd_wpa_reconfigure(wpa))
1041 return DHCPCD_WPA_ERR_RECONF;
1043 id = dhcpcd_wpa_network_find_new(wpa, s->ssid);
1045 return DHCPCD_WPA_ERR;
1047 if (s->flags & WSF_WEP) {
1050 } else if (s->flags & WSF_WPA) {
1052 if (s->flags & WSF_PSK)
1061 if (!dhcpcd_wpa_network_set(wpa, id, "key_mgmt", mgmt))
1062 return DHCPCD_WPA_ERR_SET;
1066 psk_len = strlen(psk);
1069 npsk = malloc(psk_len + 3);
1071 return DHCPCD_WPA_ERR;
1074 memcpy(npsk + 1, psk, psk_len);
1075 npsk[psk_len + 1] = '"';
1076 npsk[psk_len + 2] = '\0';
1077 r = dhcpcd_wpa_network_set(wpa, id, var, npsk);
1080 return DHCPCD_WPA_ERR_SET_PSK;
1083 if (!dhcpcd_wpa_network_enable(wpa, id))
1084 return DHCPCD_WPA_ERR_ENABLE;
1085 if (dhcpcd_wpa_config_write(wpa))
1086 retval = DHCPCD_WPA_SUCCESS;
1088 retval = DHCPCD_WPA_ERR_WRITE;
1089 /* Selecting a network disbales the others.
1090 * This should not be saved. */
1091 if (!dhcpcd_wpa_network_select(wpa, id))
1092 return DHCPCD_WPA_ERR_SELECT;
1097 dhcpcd_wpa_configure_psk(DHCPCD_WPA *wpa, DHCPCD_WI_SCAN *s, const char *psk)
1104 retval = dhcpcd_wpa_configure_psk1(wpa, s, psk);
1105 /* Always reassociate regardless of error */
1106 if (!dhcpcd_wpa_reassociate(wpa)) {
1107 if (retval == DHCPCD_WPA_SUCCESS)
1108 retval = DHCPCD_WPA_ERR_ASSOC;