1 /*
2  * WPA Supplicant / UNIX domain socket -based control interface
3  * Copyright (c) 2004-2020, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef CTRL_IFACE_H
10 #define CTRL_IFACE_H
11 
12 #ifdef CONFIG_CTRL_IFACE
13 
14 #ifndef CTRL_IFACE_MAX_LEN
15 #ifdef __ZEPHYR__
16 #define CTRL_IFACE_MAX_LEN 1024
17 #else
18 #define CTRL_IFACE_MAX_LEN 4096
19 #endif /* __ZEPHYR__ */
20 
21 #endif /* CTRL_IFACE_MAX_LEN */
22 
23 /* Shared functions from ctrl_iface.c; to be called by ctrl_iface backends */
24 
25 /**
26  * wpa_supplicant_ctrl_iface_process - Process ctrl_iface command
27  * @wpa_s: Pointer to wpa_supplicant data
28  * @buf: Received command buffer (nul terminated string)
29  * @resp_len: Variable to be set to the response length
30  * Returns: Response (*resp_len bytes) or %NULL on failure
31  *
32  * Control interface backends call this function when receiving a message that
33  * they do not process internally, i.e., anything else than ATTACH, DETACH,
34  * and LEVEL. The return response value is then sent to the external program
35  * that sent the command. Caller is responsible for freeing the buffer after
36  * this. If %NULL is returned, *resp_len can be set to two special values:
37  * 1 = send "FAIL\n" response, 2 = send "OK\n" response. If *resp_len has any
38  * other value, no response is sent.
39  */
40 char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
41 					 char *buf, size_t *resp_len);
42 
43 /**
44  * wpa_supplicant_global_ctrl_iface_process - Process global ctrl_iface command
45  * @global: Pointer to global data from wpa_supplicant_init()
46  * @buf: Received command buffer (nul terminated string)
47  * @resp_len: Variable to be set to the response length
48  * Returns: Response (*resp_len bytes) or %NULL on failure
49  *
50  * Control interface backends call this function when receiving a message from
51  * the global ctrl_iface connection. The return response value is then sent to
52  * the external program that sent the command. Caller is responsible for
53  * freeing the buffer after this. If %NULL is returned, *resp_len can be set to
54  * two special values: 1 = send "FAIL\n" response, 2 = send "OK\n" response. If
55  * *resp_len has any other value, no response is sent.
56  */
57 char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
58 						char *buf, size_t *resp_len);
59 
60 
61 /* Functions that each ctrl_iface backend must implement */
62 
63 /**
64  * wpa_supplicant_ctrl_iface_init - Initialize control interface
65  * @wpa_s: Pointer to wpa_supplicant data
66  * Returns: Pointer to private data on success, %NULL on failure
67  *
68  * Initialize the control interface and start receiving commands from external
69  * programs.
70  *
71  * Required to be implemented in each control interface backend.
72  */
73 struct ctrl_iface_priv *
74 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s);
75 
76 /**
77  * wpa_supplicant_ctrl_iface_deinit - Deinitialize control interface
78  * @wpa_s: Pointer to wpa_supplicant data
79  * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
80  *
81  * Deinitialize the control interface that was initialized with
82  * wpa_supplicant_ctrl_iface_init() and any data related to the wpa_s instance.
83  * @priv may be %NULL if the control interface has not yet been initialized.
84  *
85  * Required to be implemented in each control interface backend.
86  */
87 void wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant *wpa_s,
88 				      struct ctrl_iface_priv *priv);
89 
90 /**
91  * wpa_supplicant_ctrl_iface_wait - Wait for ctrl_iface monitor
92  * @priv: Pointer to private data from wpa_supplicant_ctrl_iface_init()
93  *
94  * Wait until the first message from an external program using the control
95  * interface is received. This function can be used to delay normal startup
96  * processing to allow control interface programs to attach with
97  * %wpa_supplicant before normal operations are started.
98  *
99  * Required to be implemented in each control interface backend.
100  */
101 void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv);
102 
103 /**
104  * wpa_supplicant_global_ctrl_iface_init - Initialize global control interface
105  * @global: Pointer to global data from wpa_supplicant_init()
106  * Returns: Pointer to private data on success, %NULL on failure
107  *
108  * Initialize the global control interface and start receiving commands from
109  * external programs.
110  *
111  * Required to be implemented in each control interface backend.
112  */
113 struct ctrl_iface_global_priv *
114 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global);
115 
116 /**
117  * wpa_supplicant_global_ctrl_iface_deinit - Deinitialize global ctrl interface
118  * @priv: Pointer to private data from wpa_supplicant_global_ctrl_iface_init()
119  *
120  * Deinitialize the global control interface that was initialized with
121  * wpa_supplicant_global_ctrl_iface_init().
122  *
123  * Required to be implemented in each control interface backend.
124  */
125 void wpa_supplicant_global_ctrl_iface_deinit(
126 	struct ctrl_iface_global_priv *priv);
127 
128 void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s);
129 
130 int wpas_ctrl_cmd_debug_level(const char *cmd);
131 
132 #else /* CONFIG_CTRL_IFACE */
133 
134 static inline struct ctrl_iface_priv *
wpa_supplicant_ctrl_iface_init(struct wpa_supplicant * wpa_s)135 wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
136 {
137 	return (void *) -1;
138 }
139 
140 static inline void
wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant * wpa_s,struct ctrl_iface_priv * priv)141 wpa_supplicant_ctrl_iface_deinit(struct wpa_supplicant *wpa_s,
142 				 struct ctrl_iface_priv *priv)
143 {
144 }
145 
146 static inline void
wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv * priv,int level,char * buf,size_t len)147 wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv, int level,
148 			       char *buf, size_t len)
149 {
150 }
151 
152 static inline void
wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv * priv)153 wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv)
154 {
155 }
156 
157 static inline struct ctrl_iface_global_priv *
wpa_supplicant_global_ctrl_iface_init(struct wpa_global * global)158 wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
159 {
160 	return (void *) 1;
161 }
162 
163 static inline void
wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv * priv)164 wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
165 {
166 }
167 
wpas_ctrl_radio_work_flush(struct wpa_supplicant * wpa_s)168 static inline void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
169 {
170 }
171 
172 #endif /* CONFIG_CTRL_IFACE */
173 
174 #endif /* CTRL_IFACE_H */
175