xref: /freebsd/contrib/wpa/src/common/wpa_ctrl.h (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*
2  * wpa_supplicant/hostapd control interface library
3  * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #ifndef WPA_CTRL_H
16 #define WPA_CTRL_H
17 
18 #ifdef  __cplusplus
19 extern "C" {
20 #endif
21 
22 /* wpa_supplicant control interface - fixed message prefixes */
23 
24 /** Interactive request for identity/password/pin */
25 #define WPA_CTRL_REQ "CTRL-REQ-"
26 
27 /** Response to identity/password/pin request */
28 #define WPA_CTRL_RSP "CTRL-RSP-"
29 
30 /* Event messages with fixed prefix */
31 /** Authentication completed successfully and data connection enabled */
32 #define WPA_EVENT_CONNECTED "CTRL-EVENT-CONNECTED "
33 /** Disconnected, data connection is not available */
34 #define WPA_EVENT_DISCONNECTED "CTRL-EVENT-DISCONNECTED "
35 /** wpa_supplicant is exiting */
36 #define WPA_EVENT_TERMINATING "CTRL-EVENT-TERMINATING "
37 /** Password change was completed successfully */
38 #define WPA_EVENT_PASSWORD_CHANGED "CTRL-EVENT-PASSWORD-CHANGED "
39 /** EAP-Request/Notification received */
40 #define WPA_EVENT_EAP_NOTIFICATION "CTRL-EVENT-EAP-NOTIFICATION "
41 /** EAP authentication started (EAP-Request/Identity received) */
42 #define WPA_EVENT_EAP_STARTED "CTRL-EVENT-EAP-STARTED "
43 /** EAP method proposed by the server */
44 #define WPA_EVENT_EAP_PROPOSED_METHOD "CTRL-EVENT-EAP-PROPOSED-METHOD "
45 /** EAP method selected */
46 #define WPA_EVENT_EAP_METHOD "CTRL-EVENT-EAP-METHOD "
47 /** EAP peer certificate from TLS */
48 #define WPA_EVENT_EAP_PEER_CERT "CTRL-EVENT-EAP-PEER-CERT "
49 /** EAP TLS certificate chain validation error */
50 #define WPA_EVENT_EAP_TLS_CERT_ERROR "CTRL-EVENT-EAP-TLS-CERT-ERROR "
51 /** EAP authentication completed successfully */
52 #define WPA_EVENT_EAP_SUCCESS "CTRL-EVENT-EAP-SUCCESS "
53 /** EAP authentication failed (EAP-Failure received) */
54 #define WPA_EVENT_EAP_FAILURE "CTRL-EVENT-EAP-FAILURE "
55 /** New scan results available */
56 #define WPA_EVENT_SCAN_RESULTS "CTRL-EVENT-SCAN-RESULTS "
57 /** A new BSS entry was added (followed by BSS entry id and BSSID) */
58 #define WPA_EVENT_BSS_ADDED "CTRL-EVENT-BSS-ADDED "
59 /** A BSS entry was removed (followed by BSS entry id and BSSID) */
60 #define WPA_EVENT_BSS_REMOVED "CTRL-EVENT-BSS-REMOVED "
61 
62 /** WPS overlap detected in PBC mode */
63 #define WPS_EVENT_OVERLAP "WPS-OVERLAP-DETECTED "
64 /** Available WPS AP with active PBC found in scan results */
65 #define WPS_EVENT_AP_AVAILABLE_PBC "WPS-AP-AVAILABLE-PBC "
66 /** Available WPS AP with recently selected PIN registrar found in scan results
67  */
68 #define WPS_EVENT_AP_AVAILABLE_PIN "WPS-AP-AVAILABLE-PIN "
69 /** Available WPS AP found in scan results */
70 #define WPS_EVENT_AP_AVAILABLE "WPS-AP-AVAILABLE "
71 /** A new credential received */
72 #define WPS_EVENT_CRED_RECEIVED "WPS-CRED-RECEIVED "
73 /** M2D received */
74 #define WPS_EVENT_M2D "WPS-M2D "
75 /** WPS registration failed after M2/M2D */
76 #define WPS_EVENT_FAIL "WPS-FAIL "
77 /** WPS registration completed successfully */
78 #define WPS_EVENT_SUCCESS "WPS-SUCCESS "
79 /** WPS enrollment attempt timed out and was terminated */
80 #define WPS_EVENT_TIMEOUT "WPS-TIMEOUT "
81 
82 #define WPS_EVENT_ENROLLEE_SEEN "WPS-ENROLLEE-SEEN "
83 
84 /* WPS ER events */
85 #define WPS_EVENT_ER_AP_ADD "WPS-ER-AP-ADD "
86 #define WPS_EVENT_ER_AP_REMOVE "WPS-ER-AP-REMOVE "
87 #define WPS_EVENT_ER_ENROLLEE_ADD "WPS-ER-ENROLLEE-ADD "
88 #define WPS_EVENT_ER_ENROLLEE_REMOVE "WPS-ER-ENROLLEE-REMOVE "
89 
90 /* hostapd control interface - fixed message prefixes */
91 #define WPS_EVENT_PIN_NEEDED "WPS-PIN-NEEDED "
92 #define WPS_EVENT_NEW_AP_SETTINGS "WPS-NEW-AP-SETTINGS "
93 #define WPS_EVENT_REG_SUCCESS "WPS-REG-SUCCESS "
94 #define WPS_EVENT_AP_SETUP_LOCKED "WPS-AP-SETUP-LOCKED "
95 #define WPS_EVENT_AP_SETUP_UNLOCKED "WPS-AP-SETUP-UNLOCKED "
96 #define WPS_EVENT_AP_PIN_ENABLED "WPS-AP-PIN-ENABLED "
97 #define WPS_EVENT_AP_PIN_DISABLED "WPS-AP-PIN-DISABLED "
98 #define AP_STA_CONNECTED "AP-STA-CONNECTED "
99 #define AP_STA_DISCONNECTED "AP-STA-DISCONNECTED "
100 
101 
102 /* wpa_supplicant/hostapd control interface access */
103 
104 /**
105  * wpa_ctrl_open - Open a control interface to wpa_supplicant/hostapd
106  * @ctrl_path: Path for UNIX domain sockets; ignored if UDP sockets are used.
107  * Returns: Pointer to abstract control interface data or %NULL on failure
108  *
109  * This function is used to open a control interface to wpa_supplicant/hostapd.
110  * ctrl_path is usually /var/run/wpa_supplicant or /var/run/hostapd. This path
111  * is configured in wpa_supplicant/hostapd and other programs using the control
112  * interface need to use matching path configuration.
113  */
114 struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path);
115 
116 
117 /**
118  * wpa_ctrl_close - Close a control interface to wpa_supplicant/hostapd
119  * @ctrl: Control interface data from wpa_ctrl_open()
120  *
121  * This function is used to close a control interface.
122  */
123 void wpa_ctrl_close(struct wpa_ctrl *ctrl);
124 
125 
126 /**
127  * wpa_ctrl_request - Send a command to wpa_supplicant/hostapd
128  * @ctrl: Control interface data from wpa_ctrl_open()
129  * @cmd: Command; usually, ASCII text, e.g., "PING"
130  * @cmd_len: Length of the cmd in bytes
131  * @reply: Buffer for the response
132  * @reply_len: Reply buffer length
133  * @msg_cb: Callback function for unsolicited messages or %NULL if not used
134  * Returns: 0 on success, -1 on error (send or receive failed), -2 on timeout
135  *
136  * This function is used to send commands to wpa_supplicant/hostapd. Received
137  * response will be written to reply and reply_len is set to the actual length
138  * of the reply. This function will block for up to two seconds while waiting
139  * for the reply. If unsolicited messages are received, the blocking time may
140  * be longer.
141  *
142  * msg_cb can be used to register a callback function that will be called for
143  * unsolicited messages received while waiting for the command response. These
144  * messages may be received if wpa_ctrl_request() is called at the same time as
145  * wpa_supplicant/hostapd is sending such a message. This can happen only if
146  * the program has used wpa_ctrl_attach() to register itself as a monitor for
147  * event messages. Alternatively to msg_cb, programs can register two control
148  * interface connections and use one of them for commands and the other one for
149  * receiving event messages, in other words, call wpa_ctrl_attach() only for
150  * the control interface connection that will be used for event messages.
151  */
152 int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
153 		     char *reply, size_t *reply_len,
154 		     void (*msg_cb)(char *msg, size_t len));
155 
156 
157 /**
158  * wpa_ctrl_attach - Register as an event monitor for the control interface
159  * @ctrl: Control interface data from wpa_ctrl_open()
160  * Returns: 0 on success, -1 on failure, -2 on timeout
161  *
162  * This function registers the control interface connection as a monitor for
163  * wpa_supplicant/hostapd events. After a success wpa_ctrl_attach() call, the
164  * control interface connection starts receiving event messages that can be
165  * read with wpa_ctrl_recv().
166  */
167 int wpa_ctrl_attach(struct wpa_ctrl *ctrl);
168 
169 
170 /**
171  * wpa_ctrl_detach - Unregister event monitor from the control interface
172  * @ctrl: Control interface data from wpa_ctrl_open()
173  * Returns: 0 on success, -1 on failure, -2 on timeout
174  *
175  * This function unregisters the control interface connection as a monitor for
176  * wpa_supplicant/hostapd events, i.e., cancels the registration done with
177  * wpa_ctrl_attach().
178  */
179 int wpa_ctrl_detach(struct wpa_ctrl *ctrl);
180 
181 
182 /**
183  * wpa_ctrl_recv - Receive a pending control interface message
184  * @ctrl: Control interface data from wpa_ctrl_open()
185  * @reply: Buffer for the message data
186  * @reply_len: Length of the reply buffer
187  * Returns: 0 on success, -1 on failure
188  *
189  * This function will receive a pending control interface message. This
190  * function will block if no messages are available. The received response will
191  * be written to reply and reply_len is set to the actual length of the reply.
192  * wpa_ctrl_recv() is only used for event messages, i.e., wpa_ctrl_attach()
193  * must have been used to register the control interface as an event monitor.
194  */
195 int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len);
196 
197 
198 /**
199  * wpa_ctrl_pending - Check whether there are pending event messages
200  * @ctrl: Control interface data from wpa_ctrl_open()
201  * Returns: 1 if there are pending messages, 0 if no, or -1 on error
202  *
203  * This function will check whether there are any pending control interface
204  * message available to be received with wpa_ctrl_recv(). wpa_ctrl_pending() is
205  * only used for event messages, i.e., wpa_ctrl_attach() must have been used to
206  * register the control interface as an event monitor.
207  */
208 int wpa_ctrl_pending(struct wpa_ctrl *ctrl);
209 
210 
211 /**
212  * wpa_ctrl_get_fd - Get file descriptor used by the control interface
213  * @ctrl: Control interface data from wpa_ctrl_open()
214  * Returns: File descriptor used for the connection
215  *
216  * This function can be used to get the file descriptor that is used for the
217  * control interface connection. The returned value can be used, e.g., with
218  * select() while waiting for multiple events.
219  *
220  * The returned file descriptor must not be used directly for sending or
221  * receiving packets; instead, the library functions wpa_ctrl_request() and
222  * wpa_ctrl_recv() must be used for this.
223  */
224 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl);
225 
226 #ifdef CONFIG_CTRL_IFACE_UDP
227 #define WPA_CTRL_IFACE_PORT 9877
228 #define WPA_GLOBAL_CTRL_IFACE_PORT 9878
229 #endif /* CONFIG_CTRL_IFACE_UDP */
230 
231 
232 #ifdef  __cplusplus
233 }
234 #endif
235 
236 #endif /* WPA_CTRL_H */
237