1 /*
2  * Copyright (c) 2019 The Linux Foundation. All rights reserved.
3  *
4  * wpa_supplicant/hostapd control interface library
5  * Copyright (c) 2004-2006, Jouni Malinen <[email protected]>
6  *
7  * This software may be distributed under the terms of the BSD license.
8  * redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name(s) of the above-listed copyright holder(s) nor the
20  * names of its contributors may be used to endorse or promote products
21  * derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef WIFIHAL_CTRL_H
37 #define WIFIHAL_CTRL_H
38 
39 #include <sys/un.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 
43 #include "stdlib.h"
44 
45 #ifdef ANDROID
46 #include <dirent.h>
47 #include <grp.h>
48 #include <pwd.h>
49 #include <sys/stat.h>
50 #include <sys/types.h>
51 #include <cutils/sockets.h>
52 #endif /* ANDROID */
53 
54 #ifdef  __cplusplus
55 extern "C" {
56 #endif
57 
58 /**
59  * struct wifihal_ctrl - Internal structure for control interface library
60  *
61  * This structure is used by the clients to interface with WiFi Hal
62  * library to store internal data. Programs using the library should not touch
63  * this data directly. They can only use the pointer to the data structure as
64  * an identifier for the control interface connection and use this as one of
65  * the arguments for most of the control interface library functions.
66  */
67 
68 struct wifihal_ctrl {
69     int s;
70     struct sockaddr_un local;
71     struct sockaddr_un dest;
72 };
73 
74 #ifndef CONFIG_CTRL_IFACE_CLIENT_DIR
75 #define CONFIG_CTRL_IFACE_CLIENT_DIR "/dev/socket/wifihal"
76 #endif /* CONFIG_CTRL_IFACE_CLIENT_DIR */
77 #ifndef CONFIG_CTRL_IFACE_CLIENT_PREFIX
78 #define CONFIG_CTRL_IFACE_CLIENT_PREFIX "wifihal_ctrl_cli_"
79 #endif /* CONFIG_CTRL_IFACE_CLIENT_PREFIX */
80 
81 #define DEFAULT_PAGE_SIZE         4096
82 
83 enum nl_family_type
84 {
85   //! gen netlink family
86   GENERIC_NL_FAMILY = 1,
87   //! Cld80211 family
88   CLD80211_FAMILY
89 };
90 
91 
92 enum wifihal_ctrl_cmd
93 {
94     /** attach monitor sock */
95     WIFIHAL_CTRL_MONITOR_ATTACH,
96     /** dettach monitor sock */
97     WIFIHAL_CTRL_MONITOR_DETTACH,
98     /** Send data over Netlink Sock */
99     WIFIHAL_CTRL_SEND_NL_DATA,
100 };
101 
102 //! WIFIHAL Control Request
103 typedef struct wifihal_ctrl_req_s {
104     //! ctrl command
105     uint32_t ctrl_cmd;
106     //! Family name
107     uint32_t family_name;
108     //! command ID
109     uint32_t cmd_id;
110     //! monitor sock len
111     uint32_t monsock_len;
112     //! monitor sock
113     struct sockaddr_un monsock;
114     //! data buff length
115     uint32_t data_len;
116     //! reserved
117     uint32_t reserved[4];
118     //! data
119     char data[0];
120 }wifihal_ctrl_req_t;
121 
122 
123 //! WIFIHAL Sync Response
124 typedef struct wifihal_ctrl_sync_rsp_s {
125     //! ctrl command
126     uint32_t ctrl_cmd;
127     //! Family name
128     uint32_t family_name;
129     //! command ID
130     uint32_t cmd_id;
131     //! status for the request
132     int status;
133     //! reserved
134     uint32_t reserved[4];
135 }wifihal_ctrl_sync_rsp_t;
136 
137 //! WIFIHAL Async Response
138 typedef struct wifihal_ctrl_event_s {
139     //! Family name
140     uint32_t family_name;
141     //! command ID
142     uint32_t cmd_id;
143     //! data buff length
144     uint32_t data_len;
145     //! reserved
146     uint32_t reserved;
147     //! data
148     char data[0];
149 }wifihal_ctrl_event_t;
150 
151 /* WiFi Hal control interface access */
152 
153 /**
154  * wifihal_ctrl_open - Open a control interface to WiFi-Hal
155  * @ctrl_path: Path for UNIX domain sockets; ignored if UDP sockets are used.
156  * Returns: Pointer to abstract control interface data or %NULL on failure
157  *
158  * This function is used to open a control interface to WiFi-Hal.
159  * ctrl_path is usually /var/run/wifihal. This path
160  * is configured in WiFi-Hal and other programs using the control
161  * interface need to use matching path configuration.
162  */
163 struct wifihal_ctrl * wifihal_ctrl_open(const char *ctrl_path);
164 
165 /**
166  * wifihal_ctrl_open2 - Open a control interface to wifihal
167  * @ctrl_path: Path for UNIX domain sockets; ignored if UDP sockets are used.
168  * @cli_path: Path for client UNIX domain sockets; ignored if UDP socket
169  *            is used.
170  * Returns: Pointer to abstract control interface data or %NULL on failure
171  *
172  * This function is used to open a control interface to wifihal
173  * when the socket path for client need to be specified explicitly. Default
174  * ctrl_path is usually /var/run/wifihal and client
175  * socket path is /tmp.
176  */
177 struct wifihal_ctrl * wifihal_ctrl_open2(const char *ctrl_path, const char *cli_path);
178 
179 
180 /**
181  * wifihal_ctrl_close - Close a control interface to wifihal
182  * @ctrl: Control interface data from wifihal_ctrl_open()
183  *
184  * This function is used to close a control interface.
185  */
186 void wifihal_ctrl_close(struct wifihal_ctrl *ctrl);
187 
188 
189 /**
190  * wifihal_ctrl_request - Send a command to wifihal
191  * @ctrl: Control interface data from wifihal_ctrl_open()
192  * @cmd: Command; usually, ASCII text, e.g., "PING"
193  * @cmd_len: Length of the cmd in bytes
194  * @reply: Buffer for the response
195  * @reply_len: Reply buffer length
196  * @msg_cb: Callback function for unsolicited messages or %NULL if not used
197  * Returns: 0 on success, -1 on error (send or receive failed), -2 on timeout
198  *
199  * This function is used to send commands to wifihal. Received
200  * response will be written to reply and reply_len is set to the actual length
201  * of the reply. This function will block for up to two seconds while waiting
202  * for the reply. If unsolicited messages are received, the blocking time may
203  * be longer.
204  *
205  * msg_cb can be used to register a callback function that will be called for
206  * unsolicited messages received while waiting for the command response. These
207  * messages may be received if wifihal_ctrl_request() is called at the same time as
208  * wifihal is sending such a message.
209  * FIXME : Change the comment below.
210  * This can happen only if
211  * the program has used wpa_ctrl_attach() to register itself as a monitor for
212  * event messages. Alternatively to msg_cb, programs can register two control
213  * interface connections and use one of them for commands and the other one for
214  * receiving event messages, in other words, call wpa_ctrl_attach() only for
215  * the control interface connection that will be used for event messages.
216  */
217 int wifihal_ctrl_request(struct wifihal_ctrl *ctrl, const char *cmd, size_t cmd_len,
218                          char *reply, size_t *reply_len);
219 
220 
221 #ifdef  __cplusplus
222 }
223 #endif
224 
225 #endif
226