298
222
const char *params,
299
223
char *buf, size_t buflen)
225
char *pos, *end, tmp[30];
226
int res, verbose, ret;
304
228
verbose = strcmp(params, "-VERBOSE") == 0;
306
230
end = buf + buflen;
307
pos += snprintf(pos, end - pos, "bssid=" MACSTR "\n",
308
MAC2STR(wpa_s->bssid));
309
if (wpa_s->current_ssid) {
310
pos += snprintf(pos, end - pos, "ssid=%s\n",
311
wpa_ssid_txt(wpa_s->current_ssid->ssid,
312
wpa_s->current_ssid->ssid_len));
314
pos += snprintf(pos, end - pos,
315
"pairwise_cipher=%s\n"
319
wpa_cipher_txt(wpa_s->pairwise_cipher),
320
wpa_cipher_txt(wpa_s->group_cipher),
321
wpa_key_mgmt_txt(wpa_s->key_mgmt, wpa_s->proto),
322
wpa_state_txt(wpa_s->wpa_state));
324
res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos, verbose);
328
if (wpa_s->preauth_eapol) {
329
pos += snprintf(pos, end - pos, "Pre-authentication "
330
"EAPOL state machines:\n");
331
res = eapol_sm_get_status(wpa_s->preauth_eapol,
332
pos, end - pos, verbose);
231
if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
232
struct wpa_ssid *ssid = wpa_s->current_ssid;
233
ret = snprintf(pos, end - pos, "bssid=" MACSTR "\n",
234
MAC2STR(wpa_s->bssid));
235
if (ret < 0 || ret >= end - pos)
239
u8 *_ssid = ssid->ssid;
240
size_t ssid_len = ssid->ssid_len;
241
u8 ssid_buf[MAX_SSID_LEN];
243
int res = wpa_drv_get_ssid(wpa_s, ssid_buf);
250
ret = snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
251
wpa_ssid_txt(_ssid, ssid_len),
253
if (ret < 0 || ret >= end - pos)
258
ret = snprintf(pos, end - pos, "id_str=%s\n",
260
if (ret < 0 || ret >= end - pos)
266
pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
268
ret = snprintf(pos, end - pos, "wpa_state=%s\n",
269
wpa_supplicant_state_txt(wpa_s->wpa_state));
270
if (ret < 0 || ret >= end - pos)
275
l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
276
ret = snprintf(pos, end - pos, "ip_address=%s\n", tmp);
277
if (ret < 0 || ret >= end - pos)
282
if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
283
wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
284
res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
341
static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
344
struct wpa_supplicant *wpa_s = eloop_ctx;
347
CTRL_IFACE_SOCK from;
348
socklen_t fromlen = sizeof(from);
290
res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
298
static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
303
struct wpa_ssid *ssid;
306
/* cmd: "<network id> <BSSID>" */
307
pos = strchr(cmd, ' ');
312
wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
313
if (hwaddr_aton(pos, bssid)) {
314
wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
318
ssid = wpa_config_get_network(wpa_s->conf, id);
320
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
325
memcpy(ssid->bssid, bssid, ETH_ALEN);
327
memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0;
334
static int wpa_supplicant_ctrl_iface_list_networks(
335
struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
338
struct wpa_ssid *ssid;
343
ret = snprintf(pos, end - pos, "network id / ssid / bssid / flags\n");
344
if (ret < 0 || ret >= end - pos)
348
ssid = wpa_s->conf->ssid;
350
ret = snprintf(pos, end - pos, "%d\t%s",
352
wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
353
if (ret < 0 || ret >= end - pos)
356
if (ssid->bssid_set) {
357
ret = snprintf(pos, end - pos, "\t" MACSTR,
358
MAC2STR(ssid->bssid));
360
ret = snprintf(pos, end - pos, "\tany");
362
if (ret < 0 || ret >= end - pos)
365
ret = snprintf(pos, end - pos, "\t%s%s",
366
ssid == wpa_s->current_ssid ? "[CURRENT]" : "",
367
ssid->disabled ? "[DISABLED]" : "");
368
if (ret < 0 || ret >= end - pos)
371
ret = snprintf(pos, end - pos, "\n");
372
if (ret < 0 || ret >= end - pos)
383
static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
386
ret = snprintf(pos, end - pos, "-");
387
if (ret < 0 || ret >= end - pos)
390
if (cipher & WPA_CIPHER_NONE) {
391
ret = snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
392
if (ret < 0 || ret >= end - pos)
397
if (cipher & WPA_CIPHER_WEP40) {
398
ret = snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
399
if (ret < 0 || ret >= end - pos)
404
if (cipher & WPA_CIPHER_WEP104) {
405
ret = snprintf(pos, end - pos, "%sWEP104", first ? "" : "+");
406
if (ret < 0 || ret >= end - pos)
411
if (cipher & WPA_CIPHER_TKIP) {
412
ret = snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
413
if (ret < 0 || ret >= end - pos)
418
if (cipher & WPA_CIPHER_CCMP) {
419
ret = snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
420
if (ret < 0 || ret >= end - pos)
429
static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
430
const u8 *ie, size_t ie_len)
432
struct wpa_ie_data data;
435
ret = snprintf(pos, end - pos, "[%s-", proto);
436
if (ret < 0 || ret >= end - pos)
440
if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
441
ret = snprintf(pos, end - pos, "?]");
442
if (ret < 0 || ret >= end - pos)
449
if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
450
ret = snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
451
if (ret < 0 || ret >= end - pos)
456
if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
457
ret = snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
458
if (ret < 0 || ret >= end - pos)
463
if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
464
ret = snprintf(pos, end - pos, "%sNone", first ? "" : "+");
465
if (ret < 0 || ret >= end - pos)
471
pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
473
if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
474
ret = snprintf(pos, end - pos, "-preauth");
475
if (ret < 0 || ret >= end - pos)
480
ret = snprintf(pos, end - pos, "]");
481
if (ret < 0 || ret >= end - pos)
489
static int wpa_supplicant_ctrl_iface_scan_results(
490
struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
493
struct wpa_scan_result *res;
496
if (wpa_s->scan_results == NULL &&
497
wpa_supplicant_get_scan_results(wpa_s) < 0)
502
ret = snprintf(pos, end - pos, "bssid / frequency / signal level / "
504
if (ret < 0 || ret >= end - pos)
508
for (i = 0; i < wpa_s->num_scan_results; i++) {
509
res = &wpa_s->scan_results[i];
510
ret = snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
511
MAC2STR(res->bssid), res->freq, res->level);
512
if (ret < 0 || ret >= end - pos)
515
if (res->wpa_ie_len) {
516
pos = wpa_supplicant_ie_txt(pos, end, "WPA",
520
if (res->rsn_ie_len) {
521
pos = wpa_supplicant_ie_txt(pos, end, "WPA2",
525
if (!res->wpa_ie_len && !res->rsn_ie_len &&
526
res->caps & IEEE80211_CAP_PRIVACY) {
527
ret = snprintf(pos, end - pos, "[WEP]");
528
if (ret < 0 || ret >= end - pos)
532
if (res->caps & IEEE80211_CAP_IBSS) {
533
ret = snprintf(pos, end - pos, "[IBSS]");
534
if (ret < 0 || ret >= end - pos)
539
ret = snprintf(pos, end - pos, "\t%s",
540
wpa_ssid_txt(res->ssid, res->ssid_len));
541
if (ret < 0 || ret >= end - pos)
545
ret = snprintf(pos, end - pos, "\n");
546
if (ret < 0 || ret >= end - pos)
555
static int wpa_supplicant_ctrl_iface_select_network(
556
struct wpa_supplicant *wpa_s, char *cmd)
559
struct wpa_ssid *ssid;
561
/* cmd: "<network id>" or "any" */
562
if (strcmp(cmd, "any") == 0) {
563
wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
564
ssid = wpa_s->conf->ssid;
569
wpa_s->reassociate = 1;
570
wpa_supplicant_req_scan(wpa_s, 0, 0);
575
wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
577
ssid = wpa_config_get_network(wpa_s->conf, id);
579
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
584
if (ssid != wpa_s->current_ssid && wpa_s->current_ssid)
585
wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
587
/* Mark all other networks disabled and trigger reassociation */
588
ssid = wpa_s->conf->ssid;
590
ssid->disabled = id != ssid->id;
593
wpa_s->reassociate = 1;
594
wpa_supplicant_req_scan(wpa_s, 0, 0);
600
static int wpa_supplicant_ctrl_iface_enable_network(
601
struct wpa_supplicant *wpa_s, char *cmd)
604
struct wpa_ssid *ssid;
606
/* cmd: "<network id>" */
608
wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
610
ssid = wpa_config_get_network(wpa_s->conf, id);
612
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
617
if (wpa_s->current_ssid == NULL && ssid->disabled) {
619
* Try to reassociate since there is no current configuration
620
* and a new network was made available. */
621
wpa_s->reassociate = 1;
622
wpa_supplicant_req_scan(wpa_s, 0, 0);
630
static int wpa_supplicant_ctrl_iface_disable_network(
631
struct wpa_supplicant *wpa_s, char *cmd)
634
struct wpa_ssid *ssid;
636
/* cmd: "<network id>" */
638
wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
640
ssid = wpa_config_get_network(wpa_s->conf, id);
642
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
647
if (ssid == wpa_s->current_ssid)
648
wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
655
static int wpa_supplicant_ctrl_iface_add_network(
656
struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
658
struct wpa_ssid *ssid;
661
wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
663
ssid = wpa_config_add_network(wpa_s->conf);
667
wpa_config_set_network_defaults(ssid);
669
ret = snprintf(buf, buflen, "%d\n", ssid->id);
670
if (ret < 0 || (size_t) ret >= buflen)
676
static int wpa_supplicant_ctrl_iface_remove_network(
677
struct wpa_supplicant *wpa_s, char *cmd)
680
struct wpa_ssid *ssid;
682
/* cmd: "<network id>" */
684
wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
686
ssid = wpa_config_get_network(wpa_s->conf, id);
688
wpa_config_remove_network(wpa_s->conf, id) < 0) {
689
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
694
if (ssid == wpa_s->current_ssid)
695
wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
701
static int wpa_supplicant_ctrl_iface_set_network(
702
struct wpa_supplicant *wpa_s, char *cmd)
705
struct wpa_ssid *ssid;
708
/* cmd: "<network id> <variable name> <value>" */
709
name = strchr(cmd, ' ');
714
value = strchr(name, ' ');
720
wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
722
wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
723
(u8 *) value, strlen(value));
725
ssid = wpa_config_get_network(wpa_s->conf, id);
727
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
732
if (wpa_config_set(ssid, name, value, 0) < 0) {
733
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
734
"variable '%s'", name);
738
if ((strcmp(name, "psk") == 0 && value[0] == '"' && ssid->ssid_len) ||
739
(strcmp(name, "ssid") == 0 && ssid->passphrase))
740
wpa_config_update_psk(ssid);
746
static int wpa_supplicant_ctrl_iface_get_network(
747
struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
750
struct wpa_ssid *ssid;
753
/* cmd: "<network id> <variable name>" */
754
name = strchr(cmd, ' ');
755
if (name == NULL || buflen == 0)
760
wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
763
ssid = wpa_config_get_network(wpa_s->conf, id);
765
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
770
value = wpa_config_get(ssid, name);
772
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
773
"variable '%s'", name);
777
snprintf(buf, buflen, "%s", value);
778
buf[buflen - 1] = '\0';
786
static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
790
if (!wpa_s->conf->update_config) {
791
wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
792
"to update configuration (update_config=0)");
796
ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
798
wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
799
"update configuration");
801
wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
809
static int wpa_supplicant_ctrl_iface_get_capability(
810
struct wpa_supplicant *wpa_s, const char *_field, char *buf,
813
struct wpa_driver_capa capa;
814
int res, first = 1, ret;
815
char *pos, *end, *strict;
818
/* Determine whether or not strict checking was requested */
819
snprintf(field, sizeof(field), "%s", _field);
820
field[sizeof(field) - 1] = '\0';
821
strict = strchr(field, ' ');
822
if (strict != NULL) {
824
if (strcmp(strict, "strict") != 0) {
830
wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
831
field, strict ? strict : "");
833
if (strcmp(field, "eap") == 0) {
834
return eap_get_names(buf, buflen);
837
res = wpa_drv_get_capa(wpa_s, &capa);
842
if (strcmp(field, "pairwise") == 0) {
846
ret = snprintf(buf, buflen, "CCMP TKIP NONE");
847
if (ret < 0 || (size_t) ret >= buflen)
852
if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
853
ret = snprintf(pos, end - pos, "%sCCMP",
855
if (ret < 0 || ret >= end - pos)
861
if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
862
ret = snprintf(pos, end - pos, "%sTKIP",
864
if (ret < 0 || ret >= end - pos)
870
if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
871
ret = snprintf(pos, end - pos, "%sNONE",
873
if (ret < 0 || ret >= end - pos)
882
if (strcmp(field, "group") == 0) {
886
ret = snprintf(buf, buflen, "CCMP TKIP WEP104 WEP40");
887
if (ret < 0 || (size_t) ret >= buflen)
892
if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
893
ret = snprintf(pos, end - pos, "%sCCMP",
895
if (ret < 0 || ret >= end - pos)
901
if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
902
ret = snprintf(pos, end - pos, "%sTKIP",
904
if (ret < 0 || ret >= end - pos)
910
if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
911
ret = snprintf(pos, end - pos, "%sWEP104",
913
if (ret < 0 || ret >= end - pos)
919
if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
920
ret = snprintf(pos, end - pos, "%sWEP40",
922
if (ret < 0 || ret >= end - pos)
931
if (strcmp(field, "key_mgmt") == 0) {
935
ret = snprintf(buf, buflen, "WPA-PSK WPA-EAP "
936
"IEEE8021X WPA-NONE NONE");
937
if (ret < 0 || (size_t) ret >= buflen)
942
ret = snprintf(pos, end - pos, "NONE IEEE8021X");
943
if (ret < 0 || ret >= end - pos)
947
if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
948
WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
949
ret = snprintf(pos, end - pos, " WPA-EAP");
950
if (ret < 0 || ret >= end - pos)
955
if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
956
WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
957
ret = snprintf(pos, end - pos, " WPA-PSK");
958
if (ret < 0 || ret >= end - pos)
963
if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
964
ret = snprintf(pos, end - pos, " WPA-NONE");
965
if (ret < 0 || ret >= end - pos)
973
if (strcmp(field, "proto") == 0) {
977
ret = snprintf(buf, buflen, "RSN WPA");
978
if (ret < 0 || (size_t) ret >= buflen)
983
if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
984
WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
985
ret = snprintf(pos, end - pos, "%sRSN",
987
if (ret < 0 || ret >= end - pos)
993
if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
994
WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
995
ret = snprintf(pos, end - pos, "%sWPA",
997
if (ret < 0 || ret >= end - pos)
1006
if (strcmp(field, "auth_alg") == 0) {
1010
ret = snprintf(buf, buflen, "OPEN SHARED LEAP");
1011
if (ret < 0 || (size_t) ret >= buflen)
1016
if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
1017
ret = snprintf(pos, end - pos, "%sOPEN",
1019
if (ret < 0 || ret >= end - pos)
1025
if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
1026
ret = snprintf(pos, end - pos, "%sSHARED",
1028
if (ret < 0 || ret >= end - pos)
1034
if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
1035
ret = snprintf(pos, end - pos, "%sLEAP",
1037
if (ret < 0 || ret >= end - pos)
1046
wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
1053
static int wpa_supplicant_ctrl_iface_ap_scan(
1054
struct wpa_supplicant *wpa_s, char *cmd)
1056
int ap_scan = atoi(cmd);
1058
if (ap_scan < 0 || ap_scan > 2)
1060
wpa_s->conf->ap_scan = ap_scan;
1065
char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
1066
char *buf, size_t *resp_len)
350
1069
const int reply_size = 2048;
352
int new_attached = 0, ctrl_rsp = 0;
354
res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
355
(struct sockaddr *) &from, &fromlen);
357
perror("recvfrom(ctrl_iface)");
361
if (strncmp(buf, "CTRL-RSP-", 9) == 0) {
1073
if (strncmp(buf, WPA_CTRL_RSP, strlen(WPA_CTRL_RSP)) == 0 ||
1074
strncmp(buf, "SET_NETWORK ", 12) == 0) {
362
1075
wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
1076
(const u8 *) buf, strlen(buf));
365
wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
1078
wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
1079
(const u8 *) buf, strlen(buf));
368
1082
reply = malloc(reply_size);
369
1083
if (reply == NULL) {
370
sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
375
1088
memcpy(reply, "OK\n", 3);
437
1199
memcpy(reply, "FAIL\n", 5);
440
sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
444
eapol_sm_notify_ctrl_attached(wpa_s->eapol);
446
1204
eapol_sm_notify_ctrl_response(wpa_s->eapol);
450
static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
455
if (wpa_s->conf->ctrl_interface == NULL)
458
len = strlen(wpa_s->conf->ctrl_interface) + strlen(wpa_s->ifname) + 2;
463
snprintf(buf, len, "%s/%s",
464
wpa_s->conf->ctrl_interface, wpa_s->ifname);
467
/* Windows/WinPcap uses interface names that are not suitable
468
* as a file name - convert invalid chars to underscores */
476
#endif /* __CYGWIN__ */
481
int wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
483
CTRL_IFACE_SOCK addr;
487
wpa_s->ctrl_sock = -1;
489
if (wpa_s->conf->ctrl_interface == NULL)
492
#ifdef CONFIG_CTRL_IFACE_UDP
493
s = socket(PF_INET, SOCK_DGRAM, 0);
495
perror("socket(PF_INET)");
499
memset(&addr, 0, sizeof(addr));
500
addr.sin_family = AF_INET;
501
addr.sin_addr.s_addr = htonl((127 << 24) | 1);
502
addr.sin_port = htons(9877);
503
if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
504
perror("bind(AF_UNIX)");
507
#else /* CONFIG_CTRL_IFACE_UDP */
508
if (mkdir(wpa_s->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
509
if (errno == EEXIST) {
510
wpa_printf(MSG_DEBUG, "Using existing control "
511
"interface directory.");
513
perror("mkdir[ctrl_interface]");
518
if (wpa_s->conf->ctrl_interface_gid_set &&
519
chown(wpa_s->conf->ctrl_interface, 0,
520
wpa_s->conf->ctrl_interface_gid) < 0) {
521
perror("chown[ctrl_interface]");
525
if (strlen(wpa_s->conf->ctrl_interface) + 1 + strlen(wpa_s->ifname) >=
526
sizeof(addr.sun_path))
529
s = socket(PF_UNIX, SOCK_DGRAM, 0);
531
perror("socket(PF_UNIX)");
535
memset(&addr, 0, sizeof(addr));
536
addr.sun_family = AF_UNIX;
537
fname = wpa_supplicant_ctrl_iface_path(wpa_s);
540
strncpy(addr.sun_path, fname, sizeof(addr.sun_path));
541
if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
542
perror("bind(PF_UNIX)");
543
if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
544
wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
545
" allow connections - assuming it was left"
546
"over from forced program termination");
547
if (unlink(fname) < 0) {
548
perror("unlink[ctrl_iface]");
549
wpa_printf(MSG_ERROR, "Could not unlink "
550
"existing ctrl_iface socket '%s'",
554
if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
556
perror("bind(PF_UNIX)");
559
wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
560
"ctrl_iface socket '%s'", fname);
562
wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
563
"be in use - cannot override it");
564
wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
565
"not used anymore", fname);
572
if (wpa_s->conf->ctrl_interface_gid_set &&
573
chown(fname, 0, wpa_s->conf->ctrl_interface_gid) < 0) {
574
perror("chown[ctrl_interface/ifname]");
578
if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
579
perror("chmod[ctrl_interface/ifname]");
583
#endif /* CONFIG_CTRL_IFACE_UDP */
585
wpa_s->ctrl_sock = s;
586
eloop_register_read_sock(s, wpa_supplicant_ctrl_iface_receive, wpa_s,
602
void wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant *wpa_s)
604
struct wpa_ctrl_dst *dst, *prev;
606
if (wpa_s->ctrl_sock > -1) {
608
eloop_unregister_read_sock(wpa_s->ctrl_sock);
609
close(wpa_s->ctrl_sock);
610
wpa_s->ctrl_sock = -1;
611
fname = wpa_supplicant_ctrl_iface_path(wpa_s);
616
if (rmdir(wpa_s->conf->ctrl_interface) < 0) {
617
if (errno == ENOTEMPTY) {
618
wpa_printf(MSG_DEBUG, "Control interface "
619
"directory not empty - leaving it "
622
perror("rmdir[ctrl_interface]");
627
dst = wpa_s->ctrl_dst;
636
void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s, int level,
637
char *buf, size_t len)
639
struct wpa_ctrl_dst *dst, *next;
642
#ifdef CONFIG_CTRL_IFACE_UDP
646
dst = wpa_s->ctrl_dst;
647
if (wpa_s->ctrl_sock < 0 || dst == NULL)
650
snprintf(levelstr, sizeof(levelstr), "<%d>", level);
652
llen = strlen(levelstr);
653
sbuf = malloc(llen + len);
657
memcpy(sbuf, levelstr, llen);
658
memcpy(sbuf + llen, buf, len);
663
if (level >= dst->debug_level) {
664
wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d",
665
inet_ntoa(dst->addr.sin_addr),
666
ntohs(dst->addr.sin_port));
667
if (sendto(wpa_s->ctrl_sock, sbuf, llen + len, 0,
668
(struct sockaddr *) &dst->addr,
669
sizeof(dst->addr)) < 0) {
670
fprintf(stderr, "CTRL_IFACE monitor[%d]: ",
674
if (dst->errors > 10) {
675
wpa_supplicant_ctrl_iface_detach(
686
#else /* CONFIG_CTRL_IFACE_UDP */
690
dst = wpa_s->ctrl_dst;
691
if (wpa_s->ctrl_sock < 0 || dst == NULL)
694
snprintf(levelstr, sizeof(levelstr), "<%d>", level);
695
io[0].iov_base = levelstr;
696
io[0].iov_len = strlen(levelstr);
697
io[1].iov_base = buf;
699
memset(&msg, 0, sizeof(msg));
706
if (level >= dst->debug_level) {
707
wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
708
(u8 *) dst->addr.sun_path, dst->addrlen);
709
msg.msg_name = &dst->addr;
710
msg.msg_namelen = dst->addrlen;
711
if (sendmsg(wpa_s->ctrl_sock, &msg, 0) < 0) {
712
fprintf(stderr, "CTRL_IFACE monitor[%d]: ",
716
if (dst->errors > 10) {
717
wpa_supplicant_ctrl_iface_detach(
727
#endif /* CONFIG_CTRL_IFACE_UDP */
1206
*resp_len = reply_len;
1211
static int wpa_supplicant_global_iface_add(struct wpa_global *global,
1214
struct wpa_interface iface;
1218
* <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
1219
* TAB<bridge_ifname>
1221
wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
1223
memset(&iface, 0, sizeof(iface));
1226
iface.ifname = pos = cmd;
1227
pos = strchr(pos, '\t');
1230
if (iface.ifname[0] == '\0')
1235
iface.confname = pos;
1236
pos = strchr(pos, '\t');
1239
if (iface.confname[0] == '\0')
1240
iface.confname = NULL;
1245
pos = strchr(pos, '\t');
1248
if (iface.driver[0] == '\0')
1249
iface.driver = NULL;
1253
iface.ctrl_interface = pos;
1254
pos = strchr(pos, '\t');
1257
if (iface.ctrl_interface[0] == '\0')
1258
iface.ctrl_interface = NULL;
1262
iface.driver_param = pos;
1263
pos = strchr(pos, '\t');
1266
if (iface.driver_param[0] == '\0')
1267
iface.driver_param = NULL;
1271
iface.bridge_ifname = pos;
1272
pos = strchr(pos, '\t');
1275
if (iface.bridge_ifname[0] == '\0')
1276
iface.bridge_ifname = NULL;
1281
if (wpa_supplicant_get_iface(global, iface.ifname))
1284
return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
1288
static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
1291
struct wpa_supplicant *wpa_s;
1293
wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
1295
wpa_s = wpa_supplicant_get_iface(global, cmd);
1298
return wpa_supplicant_remove_iface(global, wpa_s);
1302
static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
1307
struct wpa_supplicant *wpa_s;
1309
wpa_s = global->ifaces;
1314
res = snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
1315
if (res < 0 || res >= end - pos) {
1320
wpa_s = wpa_s->next;
1326
char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
1327
char *buf, size_t *resp_len)
1330
const int reply_size = 2048;
1333
wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
1334
(const u8 *) buf, strlen(buf));
1336
reply = malloc(reply_size);
1337
if (reply == NULL) {
1342
memcpy(reply, "OK\n", 3);
1345
if (strcmp(buf, "PING") == 0) {
1346
memcpy(reply, "PONG\n", 5);
1348
} else if (strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
1349
if (wpa_supplicant_global_iface_add(global, buf + 14))
1351
} else if (strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
1352
if (wpa_supplicant_global_iface_remove(global, buf + 17))
1354
} else if (strcmp(buf, "INTERFACES") == 0) {
1355
reply_len = wpa_supplicant_global_iface_interfaces(
1356
global, reply, reply_size);
1358
memcpy(reply, "UNKNOWN COMMAND\n", 16);
1362
if (reply_len < 0) {
1363
memcpy(reply, "FAIL\n", 5);
1367
*resp_len = reply_len;