1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Portions copyright (C) 2017 Broadcom Limited
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <stdint.h>
20 #include <fcntl.h>
21 #include <sys/socket.h>
22 #include <netlink/genl/genl.h>
23 #include <netlink/genl/family.h>
24 #include <netlink/genl/ctrl.h>
25 #include <linux/rtnetlink.h>
26 #include <netpacket/packet.h>
27 #include <linux/filter.h>
28 #include <linux/errqueue.h>
29
30 #include <linux/pkt_sched.h>
31 #include <netlink/object-api.h>
32 #include <netlink/netlink.h>
33 #include <netlink/socket.h>
34 #include <netlink/handlers.h>
35
36 #include "wifi_hal.h"
37 #include "common.h"
38 #include "cpp_bindings.h"
39
40 /* test mode flag for halutil only */
41 bool halutil_mode = false;
getIfaceInfo(wifi_interface_handle handle)42 interface_info *getIfaceInfo(wifi_interface_handle handle)
43 {
44 return (interface_info *)handle;
45 }
46
getWifiHandle(wifi_interface_handle handle)47 wifi_handle getWifiHandle(wifi_interface_handle handle)
48 {
49 return getIfaceInfo(handle)->handle;
50 }
51
getHalInfo(wifi_handle handle)52 hal_info *getHalInfo(wifi_handle handle)
53 {
54 return (hal_info *)handle;
55 }
56
getHalInfo(wifi_interface_handle handle)57 hal_info *getHalInfo(wifi_interface_handle handle)
58 {
59 return getHalInfo(getWifiHandle(handle));
60 }
61
getWifiHandle(hal_info * info)62 wifi_handle getWifiHandle(hal_info *info)
63 {
64 return (wifi_handle)info;
65 }
66
getIfaceHandle(interface_info * info)67 wifi_interface_handle getIfaceHandle(interface_info *info)
68 {
69 return (wifi_interface_handle)info;
70 }
71
wifi_register_handler(wifi_handle handle,int cmd,nl_recvmsg_msg_cb_t func,void * arg)72 wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg)
73 {
74 hal_info *info = (hal_info *)handle;
75
76 /* TODO: check for multiple handlers? */
77 pthread_mutex_lock(&info->cb_lock);
78
79 wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;
80
81 if (info->num_event_cb < info->alloc_event_cb) {
82 info->event_cb[info->num_event_cb].nl_cmd = cmd;
83 info->event_cb[info->num_event_cb].vendor_id = 0;
84 info->event_cb[info->num_event_cb].vendor_subcmd = 0;
85 info->event_cb[info->num_event_cb].cb_func = func;
86 info->event_cb[info->num_event_cb].cb_arg = arg;
87 ALOGV("Successfully added event handler %p:%p for command %d at %d",
88 arg, func, cmd, info->num_event_cb);
89 info->num_event_cb++;
90 result = WIFI_SUCCESS;
91 }
92
93 pthread_mutex_unlock(&info->cb_lock);
94 return result;
95 }
96
wifi_register_vendor_handler(wifi_handle handle,uint32_t id,int subcmd,nl_recvmsg_msg_cb_t func,void * arg)97 wifi_error wifi_register_vendor_handler(wifi_handle handle,
98 uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg)
99 {
100 hal_info *info = (hal_info *)handle;
101
102 /* TODO: check for multiple handlers? */
103 pthread_mutex_lock(&info->cb_lock);
104
105 wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;
106
107 if (info->num_event_cb < info->alloc_event_cb) {
108 info->event_cb[info->num_event_cb].nl_cmd = NL80211_CMD_VENDOR;
109 info->event_cb[info->num_event_cb].vendor_id = id;
110 info->event_cb[info->num_event_cb].vendor_subcmd = subcmd;
111 info->event_cb[info->num_event_cb].cb_func = func;
112 info->event_cb[info->num_event_cb].cb_arg = arg;
113 ALOGV("Added event handler %p:%p for vendor 0x%0x and subcmd 0x%0x at %d",
114 arg, func, id, subcmd, info->num_event_cb);
115 info->num_event_cb++;
116 result = WIFI_SUCCESS;
117 }
118
119 pthread_mutex_unlock(&info->cb_lock);
120 return result;
121 }
122
wifi_unregister_handler(wifi_handle handle,int cmd)123 void wifi_unregister_handler(wifi_handle handle, int cmd)
124 {
125 hal_info *info = (hal_info *)handle;
126
127 if (cmd == NL80211_CMD_VENDOR) {
128 ALOGE("Must use wifi_unregister_vendor_handler to remove vendor handlers");
129 return;
130 }
131
132 pthread_mutex_lock(&info->cb_lock);
133
134 for (int i = 0; i < info->num_event_cb; i++) {
135 if (info->event_cb[i].nl_cmd == cmd) {
136 ALOGV("Successfully removed event handler %p:%p for cmd = 0x%0x from %d",
137 info->event_cb[i].cb_arg, info->event_cb[i].cb_func, cmd, i);
138
139 memmove(&info->event_cb[i], &info->event_cb[i+1],
140 (info->num_event_cb - i - 1) * sizeof(cb_info));
141 info->num_event_cb--;
142 break;
143 }
144 }
145
146 pthread_mutex_unlock(&info->cb_lock);
147 }
148
wifi_unregister_vendor_handler(wifi_handle handle,uint32_t id,int subcmd)149 void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd)
150 {
151 hal_info *info = (hal_info *)handle;
152
153 pthread_mutex_lock(&info->cb_lock);
154
155 for (int i = 0; i < info->num_event_cb; i++) {
156
157 if (info->event_cb[i].nl_cmd == NL80211_CMD_VENDOR
158 && info->event_cb[i].vendor_id == id
159 && info->event_cb[i].vendor_subcmd == subcmd) {
160 ALOGV("Successfully removed event handler %p:%p for vendor 0x%0x, subcmd 0x%0x from %d",
161 info->event_cb[i].cb_arg, info->event_cb[i].cb_func, id, subcmd, i);
162 memmove(&info->event_cb[i], &info->event_cb[i+1],
163 (info->num_event_cb - i - 1) * sizeof(cb_info));
164 info->num_event_cb--;
165 break;
166 }
167 }
168
169 pthread_mutex_unlock(&info->cb_lock);
170 }
171
172
wifi_register_cmd(wifi_handle handle,int id,WifiCommand * cmd)173 wifi_error wifi_register_cmd(wifi_handle handle, int id, WifiCommand *cmd)
174 {
175 hal_info *info = (hal_info *)handle;
176
177 ALOGV("registering command %d", id);
178
179 wifi_error result = WIFI_ERROR_OUT_OF_MEMORY;
180
181 if (info->num_cmd < info->alloc_cmd) {
182 info->cmd[info->num_cmd].id = id;
183 info->cmd[info->num_cmd].cmd = cmd;
184 ALOGV("Successfully added command %d: %p at %d", id, cmd, info->num_cmd);
185 info->num_cmd++;
186 result = WIFI_SUCCESS;
187 } else {
188 ALOGE("Failed to add command %d: %p at %d, reached max limit %d",
189 id, cmd, info->num_cmd, info->alloc_cmd);
190 }
191
192 return result;
193 }
194
wifi_unregister_cmd(wifi_handle handle,int id)195 WifiCommand *wifi_unregister_cmd(wifi_handle handle, int id)
196 {
197 hal_info *info = (hal_info *)handle;
198
199 ALOGV("un-registering command %d", id);
200
201 WifiCommand *cmd = NULL;
202
203 for (int i = 0; i < info->num_cmd; i++) {
204 if (info->cmd[i].id == id) {
205 cmd = info->cmd[i].cmd;
206 memmove(&info->cmd[i], &info->cmd[i+1], (info->num_cmd - i - 1) * sizeof(cmd_info));
207 info->num_cmd--;
208 ALOGV("Successfully removed command %d: %p from %d", id, cmd, i);
209 break;
210 }
211 }
212
213 if (!cmd) {
214 ALOGI("Failed to remove command %d: %p", id, cmd);
215 }
216
217 return cmd;
218 }
219
wifi_get_cmd(wifi_handle handle,int id)220 WifiCommand *wifi_get_cmd(wifi_handle handle, int id)
221 {
222 hal_info *info = (hal_info *)handle;
223
224 WifiCommand *cmd = NULL;
225
226 for (int i = 0; i < info->num_cmd; i++) {
227 if (info->cmd[i].id == id) {
228 cmd = info->cmd[i].cmd;
229 break;
230 }
231 }
232
233 return cmd;
234 }
235
wifi_unregister_cmd(wifi_handle handle,WifiCommand * cmd)236 void wifi_unregister_cmd(wifi_handle handle, WifiCommand *cmd)
237 {
238 hal_info *info = (hal_info *)handle;
239
240 for (int i = 0; i < info->num_cmd; i++) {
241 if (info->cmd[i].cmd == cmd) {
242 int id = info->cmd[i].id;
243 memmove(&info->cmd[i], &info->cmd[i+1], (info->num_cmd - i - 1) * sizeof(cmd_info));
244 info->num_cmd--;
245 ALOGV("Successfully removed command %d: %p from %d", id, cmd, i);
246 break;
247 }
248 }
249 }
250
wifi_cancel_cmd(wifi_request_id id,wifi_interface_handle iface)251 wifi_error wifi_cancel_cmd(wifi_request_id id, wifi_interface_handle iface)
252 {
253 wifi_handle handle = getWifiHandle(iface);
254
255 WifiCommand *cmd = wifi_unregister_cmd(handle, id);
256 ALOGV("Cancel WifiCommand = %p", cmd);
257 if (cmd) {
258 cmd->cancel();
259 cmd->releaseRef();
260 return WIFI_SUCCESS;
261 }
262
263 return WIFI_ERROR_INVALID_ARGS;
264 }
265
set_hautil_mode(bool util_mode)266 void set_hautil_mode(bool util_mode)
267 {
268 halutil_mode = util_mode;
269 }
get_halutil_mode()270 bool get_halutil_mode()
271 {
272 return halutil_mode;
273 }
274