4 #include <sys/socket.h>
5 #include <netlink/genl/genl.h>
6 #include <netlink/genl/family.h>
7 #include <netlink/genl/ctrl.h>
8 #include <linux/rtnetlink.h>
9 #include <netpacket/packet.h>
10 #include <linux/filter.h>
11 #include <linux/errqueue.h>
13 #include <linux/pkt_sched.h>
14 #include <netlink/object-api.h>
15 #include <netlink/netlink.h>
16 #include <netlink/socket.h>
17 #include <netlink/attr.h>
18 #include <netlink/handlers.h>
19 #include <netlink/msg.h>
26 #define LOG_TAG "WifiHAL"
28 #include <utils/Log.h>
32 #include "cpp_bindings.h"
35 #define WIFI_HAL_CMD_SOCK_PORT 644
36 #define WIFI_HAL_EVENT_SOCK_PORT 645
39 #define FEATURE_SET_MATRIX 1
40 #define ATTR_NODFS_VALUE 3
42 static void internal_event_handler(wifi_handle handle
, int events
);
43 static int internal_no_seq_check(nl_msg
*msg
, void *arg
);
44 static int internal_valid_message_handler(nl_msg
*msg
, void *arg
);
45 static int wifi_get_multicast_id(wifi_handle handle
, const char *name
, const char *group
);
46 static int wifi_add_membership(wifi_handle handle
, const char *group
);
47 static wifi_error
wifi_init_interfaces(wifi_handle handle
);
49 typedef enum wifi_attr
{
50 ANDR_WIFI_ATTRIBUTE_NUM_FEATURE_SET
,
51 ANDR_WIFI_ATTRIBUTE_FEATURE_SET
,
52 ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
55 /* Initialize/Cleanup */
57 void wifi_socket_set_local_port(struct nl_sock
*sock
, uint32_t port
)
59 uint32_t pid
= getpid() & 0x3FFFFF;
60 nl_socket_set_local_port(sock
, pid
+ (port
<< 22));
63 static nl_sock
* wifi_create_nl_socket(int port
)
65 ALOGI("Creating socket");
66 struct nl_sock
*sock
= nl_socket_alloc();
68 ALOGE("Could not create handle");
72 wifi_socket_set_local_port(sock
, port
);
74 struct sockaddr
*addr
= NULL
;
75 // ALOGI("sizeof(sockaddr) = %d, sizeof(sockaddr_nl) = %d", sizeof(*addr), sizeof(*addr_nl));
77 ALOGI("Connecting socket");
78 if (nl_connect(sock
, NETLINK_GENERIC
)) {
79 ALOGE("Could not connect handle");
84 ALOGI("Making socket nonblocking");
86 if (nl_socket_set_nonblocking(sock)) {
87 ALOGE("Could make socket non-blocking");
96 /*initialize function pointer table with Broadcom HHAL API*/
97 wifi_error
init_wifi_vendor_hal_func_table(wifi_hal_fn
*fn
)
100 return WIFI_ERROR_UNKNOWN
;
102 fn
->wifi_initialize
= wifi_initialize
;
103 fn
->wifi_cleanup
= wifi_cleanup
;
104 fn
->wifi_event_loop
= wifi_event_loop
;
105 fn
->wifi_get_supported_feature_set
= wifi_get_supported_feature_set
;
106 fn
->wifi_get_concurrency_matrix
= wifi_get_concurrency_matrix
;
107 fn
->wifi_set_scanning_mac_oui
= wifi_set_scanning_mac_oui
;
108 fn
->wifi_get_ifaces
= wifi_get_ifaces
;
109 fn
->wifi_get_iface_name
= wifi_get_iface_name
;
110 fn
->wifi_start_gscan
= wifi_start_gscan
;
111 fn
->wifi_stop_gscan
= wifi_stop_gscan
;
112 fn
->wifi_get_cached_gscan_results
= wifi_get_cached_gscan_results
;
113 fn
->wifi_set_bssid_hotlist
= wifi_set_bssid_hotlist
;
114 fn
->wifi_reset_bssid_hotlist
= wifi_reset_bssid_hotlist
;
115 fn
->wifi_set_significant_change_handler
= wifi_set_significant_change_handler
;
116 fn
->wifi_reset_significant_change_handler
= wifi_reset_significant_change_handler
;
117 fn
->wifi_get_gscan_capabilities
= wifi_get_gscan_capabilities
;
118 fn
->wifi_get_link_stats
= wifi_get_link_stats
;
119 fn
->wifi_get_valid_channels
= wifi_get_valid_channels
;
120 fn
->wifi_rtt_range_request
= wifi_rtt_range_request
;
121 fn
->wifi_rtt_range_cancel
= wifi_rtt_range_cancel
;
122 fn
->wifi_get_rtt_capabilities
= wifi_get_rtt_capabilities
;
123 fn
->wifi_set_nodfs_flag
= wifi_set_nodfs_flag
;
127 wifi_error
wifi_initialize(wifi_handle
*handle
)
131 ALOGI("Initializing wifi");
132 hal_info
*info
= (hal_info
*)malloc(sizeof(hal_info
));
134 ALOGE("Could not allocate hal_info");
135 return WIFI_ERROR_UNKNOWN
;
138 memset(info
, 0, sizeof(*info
));
140 ALOGI("Creating socket");
141 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, info
->cleanup_socks
) == -1) {
142 ALOGE("Could not create cleanup sockets");
144 return WIFI_ERROR_UNKNOWN
;
147 struct nl_sock
*cmd_sock
= wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT
);
148 if (cmd_sock
== NULL
) {
149 ALOGE("Could not create handle");
151 return WIFI_ERROR_UNKNOWN
;
154 struct nl_sock
*event_sock
= wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT
);
155 if (event_sock
== NULL
) {
156 ALOGE("Could not create handle");
157 nl_socket_free(cmd_sock
);
159 return WIFI_ERROR_UNKNOWN
;
162 struct nl_cb
*cb
= nl_socket_get_cb(event_sock
);
164 ALOGE("Could not create handle");
165 nl_socket_free(cmd_sock
);
166 nl_socket_free(event_sock
);
168 return WIFI_ERROR_UNKNOWN
;
171 // ALOGI("cb->refcnt = %d", cb->cb_refcnt);
172 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, internal_no_seq_check
, info
);
173 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, internal_valid_message_handler
, info
);
176 info
->cmd_sock
= cmd_sock
;
177 info
->event_sock
= event_sock
;
178 info
->clean_up
= false;
179 info
->in_event_loop
= false;
181 info
->event_cb
= (cb_info
*)malloc(sizeof(cb_info
) * DEFAULT_EVENT_CB_SIZE
);
182 info
->alloc_event_cb
= DEFAULT_EVENT_CB_SIZE
;
183 info
->num_event_cb
= 0;
185 info
->cmd
= (cmd_info
*)malloc(sizeof(cmd_info
) * DEFAULT_CMD_SIZE
);
186 info
->alloc_cmd
= DEFAULT_CMD_SIZE
;
189 info
->nl80211_family_id
= genl_ctrl_resolve(cmd_sock
, "nl80211");
190 if (info
->nl80211_family_id
< 0) {
191 ALOGE("Could not resolve nl80211 familty id");
192 nl_socket_free(cmd_sock
);
193 nl_socket_free(event_sock
);
195 return WIFI_ERROR_UNKNOWN
;
198 pthread_mutex_init(&info
->cb_lock
, NULL
);
200 *handle
= (wifi_handle
) info
;
201 ALOGD("wifi_initialize, handle = %p\n", handle
);
202 ALOGD("wifi_initialize, *handle = %p\n", *handle
);
203 ALOGD("wifi_initialize, info = %p\n", info
);
204 ALOGD("wifi_initialize, *info = %pn", *info
);
205 wifi_add_membership(*handle
, "scan");
206 wifi_add_membership(*handle
, "mlme");
207 wifi_add_membership(*handle
, "regulatory");
208 wifi_add_membership(*handle
, "vendor");
210 wifi_init_interfaces(*handle
);
211 ALOGD("Found %d interfaces", info
->num_interfaces
);
214 ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR
);
218 static int wifi_add_membership(wifi_handle handle
, const char *group
)
220 hal_info
*info
= getHalInfo(handle
);
222 int id
= wifi_get_multicast_id(handle
, "nl80211", group
);
224 ALOGE("Could not find group %s", group
);
228 int ret
= nl_socket_add_membership(info
->event_sock
, id
);
230 ALOGE("Could not add membership to group %s", group
);
233 ALOGI("Successfully added membership for group %s", group
);
237 static void internal_cleaned_up_handler(wifi_handle handle
)
239 hal_info
*info
= getHalInfo(handle
);
240 wifi_cleaned_up_handler cleaned_up_handler
= info
->cleaned_up_handler
;
242 if (info
->cmd_sock
!= 0) {
243 close(info
->cleanup_socks
[0]);
244 close(info
->cleanup_socks
[1]);
245 nl_socket_free(info
->cmd_sock
);
246 nl_socket_free(info
->event_sock
);
247 info
->cmd_sock
= NULL
;
248 info
->event_sock
= NULL
;
251 (*cleaned_up_handler
)(handle
);
252 pthread_mutex_destroy(&info
->cb_lock
);
255 ALOGI("Internal cleanup completed");
258 void wifi_cleanup(wifi_handle handle
, wifi_cleaned_up_handler handler
)
260 hal_info
*info
= getHalInfo(handle
);
263 info
->cleaned_up_handler
= handler
;
264 if (write(info
->cleanup_socks
[0], "Exit", 4) < 1) {
265 ALOGE("could not write to the cleanup socket");
267 // Listen to the response
268 // Hopefully we dont get errors or get hung up
269 // Not much can be done in that case, but assume that
270 // it has rx'ed the Exit message to exit the thread.
271 // As a fallback set the cleanup flag to TRUE
272 memset(buf
, 0, sizeof(buf
));
273 int result
= read(info
->cleanup_socks
[0], buf
, sizeof(buf
));
274 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result
, errno
);
275 if (strncmp(buf
, "Done", 4) == 0) {
276 ALOGE("Event processing terminated");
278 ALOGD("Rx'ed %s", buf
);
281 info
->clean_up
= true;
282 pthread_mutex_lock(&info
->cb_lock
);
284 int bad_commands
= 0;
286 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
287 cb_info
*cbi
= &(info
->event_cb
[i
]);
288 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
291 while (info
->num_cmd
> bad_commands
) {
292 int num_cmd
= info
->num_cmd
;
293 cmd_info
*cmdi
= &(info
->cmd
[bad_commands
]);
294 WifiCommand
*cmd
= cmdi
->cmd
;
296 pthread_mutex_unlock(&info
->cb_lock
);
298 pthread_mutex_lock(&info
->cb_lock
);
299 /* release reference added when command is saved */
301 if (num_cmd
== info
->num_cmd
) {
307 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
308 cb_info
*cbi
= &(info
->event_cb
[i
]);
309 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
310 ALOGE("Leaked command %p", cmd
);
312 pthread_mutex_unlock(&info
->cb_lock
);
313 internal_cleaned_up_handler(handle
);
316 static int internal_pollin_handler(wifi_handle handle
)
318 hal_info
*info
= getHalInfo(handle
);
319 ALOGI("even_loop info = %p", info
);
320 struct nl_cb
*cb
= nl_socket_get_cb(info
->event_sock
);
321 int res
= nl_recvmsgs(info
->event_sock
, cb
);
322 ALOGD("nl_recvmsgs returned %d", res
);
327 /* Run event handler */
328 void wifi_event_loop(wifi_handle handle
)
330 hal_info
*info
= getHalInfo(handle
);
331 ALOGI("even_loop info = %p", info
);
332 ALOGI("even_loop info = %p", handle
);
333 if (info
->in_event_loop
) {
336 info
->in_event_loop
= true;
340 memset(&pfd
[0], 0, sizeof(pollfd
) * 2);
342 pfd
[0].fd
= nl_socket_get_fd(info
->event_sock
);
343 pfd
[0].events
= POLLIN
;
344 pfd
[1].fd
= info
->cleanup_socks
[1];
345 pfd
[1].events
= POLLIN
;
350 int timeout
= -1; /* Infinite timeout */
353 int result
= poll(pfd
, 2, timeout
);
355 } else if (pfd
[0].revents
& POLLERR
) {
356 ALOGE("POLL Error; error no = %d", errno
);
357 int result2
= read(pfd
[0].fd
, buf
, sizeof(buf
));
358 ALOGE("Read after POLL returned %d, error no = %d", result2
, errno
);
359 } else if (pfd
[0].revents
& POLLHUP
) {
360 ALOGE("Remote side hung up");
362 } else if (pfd
[0].revents
& POLLIN
) {
363 internal_pollin_handler(handle
);
364 } else if (pfd
[1].revents
& POLLIN
) {
365 memset(buf
, 0, sizeof(buf
));
366 int result2
= read(pfd
[1].fd
, buf
, sizeof(buf
));
367 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result2
, errno
);
368 if (strncmp(buf
, "Exit", 4) == 0) {
369 ALOGD("Got a signal to exit!!!");
370 if (write(pfd
[1].fd
, "Done", 4) < 1) {
371 ALOGE("could not write to the cleanup socket");
375 ALOGD("Rx'ed %s on the cleanup socket\n", buf
);
378 ALOGE("Unknown event - %0x, %0x", pfd
[0].revents
, pfd
[1].revents
);
380 } while (!info
->clean_up
);
381 ALOGI("Exit %s", __FUNCTION__
);
384 ///////////////////////////////////////////////////////////////////////////////////////
386 static int internal_no_seq_check(struct nl_msg
*msg
, void *arg
)
391 static int internal_valid_message_handler(nl_msg
*msg
, void *arg
)
393 wifi_handle handle
= (wifi_handle
)arg
;
394 hal_info
*info
= getHalInfo(handle
);
395 ALOGI("even_loop info = %p", handle
);
396 ALOGD("internal_valid_message_handler, info = %p", info
);
398 WifiEvent
event(msg
);
399 int res
= event
.parse();
401 ALOGE("Failed to parse event: %d", res
);
405 int cmd
= event
.get_cmd();
406 uint32_t vendor_id
= 0;
409 if (cmd
== NL80211_CMD_VENDOR
) {
410 vendor_id
= event
.get_u32(NL80211_ATTR_VENDOR_ID
);
411 subcmd
= event
.get_u32(NL80211_ATTR_VENDOR_SUBCMD
);
412 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
413 event
.get_cmdString(), vendor_id
, subcmd
);
415 ALOGI("event received %s", event
.get_cmdString());
418 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
421 bool dispatched
= false;
423 pthread_mutex_lock(&info
->cb_lock
);
425 ALOGI("Number of events %d", info
->num_event_cb
);
427 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
428 if (cmd
== info
->event_cb
[i
].nl_cmd
) {
429 if (cmd
== NL80211_CMD_VENDOR
430 && ((vendor_id
!= info
->event_cb
[i
].vendor_id
)
431 || (subcmd
!= info
->event_cb
[i
].vendor_subcmd
)))
433 /* event for a different vendor, ignore it */
437 cb_info
*cbi
= &(info
->event_cb
[i
]);
438 nl_recvmsg_msg_cb_t cb_func
= cbi
->cb_func
;
439 void *cb_arg
= cbi
->cb_arg
;
440 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
445 pthread_mutex_unlock(&info
->cb_lock
);
447 (*cb_func
)(msg
, cb_arg
);
456 pthread_mutex_unlock(&info
->cb_lock
);
460 ///////////////////////////////////////////////////////////////////////////////////////
462 class GetMulticastIdCommand
: public WifiCommand
469 GetMulticastIdCommand(wifi_handle handle
, const char *name
, const char *group
)
470 : WifiCommand(handle
, 0)
481 virtual int create() {
482 int nlctrlFamily
= genl_ctrl_resolve(mInfo
->cmd_sock
, "nlctrl");
483 ALOGI("ctrl family = %d", nlctrlFamily
);
484 int ret
= mMsg
.create(nlctrlFamily
, CTRL_CMD_GETFAMILY
, 0, 0);
488 ret
= mMsg
.put_string(CTRL_ATTR_FAMILY_NAME
, mName
);
492 virtual int handleResponse(WifiEvent
& reply
) {
494 ALOGE("handling reponse in %s", __func__
);
496 struct nlattr
**tb
= reply
.attributes();
497 struct genlmsghdr
*gnlh
= reply
.header();
498 struct nlattr
*mcgrp
= NULL
;
501 if (!tb
[CTRL_ATTR_MCAST_GROUPS
]) {
502 ALOGE("No multicast groups found");
505 ALOGE("Multicast groups attr size = %d", nla_len(tb
[CTRL_ATTR_MCAST_GROUPS
]));
508 for_each_attr(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
510 ALOGE("Processing group");
511 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
512 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, (nlattr
*)nla_data(mcgrp
),
513 nla_len(mcgrp
), NULL
);
514 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] || !tb2
[CTRL_ATTR_MCAST_GRP_ID
]) {
518 char *grpName
= (char *)nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
519 int grpNameLen
= nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
521 ALOGE("Found group name %s", grpName
);
523 if (strncmp(grpName
, mGroup
, grpNameLen
) != 0)
526 mId
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
535 class SetPnoMacAddrOuiCommand
: public WifiCommand
{
540 feature_set
*feature_matrix
;
544 SetPnoMacAddrOuiCommand(wifi_interface_handle handle
, oui scan_oui
)
545 : WifiCommand(handle
, 0)
550 int createRequest(WifiRequest
& request
, int subcmd
, byte
*scan_oui
) {
551 int result
= request
.create(GOOGLE_OUI
, subcmd
);
556 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
557 result
= request
.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
, scan_oui
, DOT11_OUI_LEN
);
562 request
.attr_end(data
);
568 ALOGD("Sending mac address OUI");
569 WifiRequest
request(familyId(), ifaceId());
570 int result
= createRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI
, mOui
);
571 if (result
!= WIFI_SUCCESS
) {
572 ALOGE("failed to create request; result = %d", result
);
576 result
= requestResponse(request
);
577 if (result
!= WIFI_SUCCESS
) {
578 ALOGE("failed to set scanning mac OUI; result = %d", result
);
584 virtual int handleResponse(WifiEvent
& reply
) {
585 ALOGD("Request complete!");
586 /* Nothing to do on response! */
591 class SetNodfsCommand
: public WifiCommand
{
596 SetNodfsCommand(wifi_interface_handle handle
, u32 nodfs
)
597 : WifiCommand(handle
, 0) {
600 virtual int create() {
603 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS
);
605 ALOGE("Can't create message to send to driver - %d", ret
);
609 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
610 ret
= mMsg
.put_u32(ATTR_NODFS_VALUE
, mNoDfs
);
620 static int wifi_get_multicast_id(wifi_handle handle
, const char *name
, const char *group
)
622 GetMulticastIdCommand
cmd(handle
, name
, group
);
623 int res
= cmd
.requestResponse();
630 /////////////////////////////////////////////////////////////////////////
632 static bool is_wifi_interface(const char *name
)
634 if (strncmp(name
, "wlan", 4) != 0 && strncmp(name
, "p2p", 3) != 0) {
635 /* not a wifi interface; ignore it */
642 static int get_interface(const char *name
, interface_info
*info
)
644 strcpy(info
->name
, name
);
645 info
->id
= if_nametoindex(name
);
646 ALOGI("found an interface : %s, id = %d", name
, info
->id
);
650 wifi_error
wifi_init_interfaces(wifi_handle handle
)
652 hal_info
*info
= (hal_info
*)handle
;
653 ALOGD("wifi_init_interfaces, info = %p", info
);
657 DIR *d
= opendir("/sys/class/net");
659 return WIFI_ERROR_UNKNOWN
;
662 while ((de
= readdir(d
))) {
663 if (de
->d_name
[0] == '.')
665 if (is_wifi_interface(de
->d_name
) ) {
672 d
= opendir("/sys/class/net");
674 return WIFI_ERROR_UNKNOWN
;
676 info
->interfaces
= (interface_info
**)malloc(sizeof(interface_info
*) * n
);
679 while ((de
= readdir(d
))) {
680 if (de
->d_name
[0] == '.')
682 if (is_wifi_interface(de
->d_name
)) {
683 interface_info
*ifinfo
= (interface_info
*)malloc(sizeof(interface_info
));
684 if (get_interface(de
->d_name
, ifinfo
) != WIFI_SUCCESS
) {
688 ifinfo
->handle
= handle
;
689 info
->interfaces
[i
] = ifinfo
;
696 info
->num_interfaces
= n
;
700 wifi_error
wifi_get_ifaces(wifi_handle handle
, int *num
, wifi_interface_handle
**interfaces
)
702 hal_info
*info
= (hal_info
*)handle
;
704 *interfaces
= (wifi_interface_handle
*)info
->interfaces
;
705 *num
= info
->num_interfaces
;
710 wifi_error
wifi_get_iface_name(wifi_interface_handle handle
, char *name
, size_t size
)
712 interface_info
*info
= (interface_info
*)handle
;
713 strcpy(name
, info
->name
);
717 wifi_error
wifi_get_supported_feature_set(wifi_interface_handle handle
, feature_set
*set
)
719 return WIFI_ERROR_NOT_SUPPORTED
;
722 wifi_error
wifi_get_concurrency_matrix(wifi_interface_handle handle
, int set_size_max
,
723 feature_set set
[], int *set_size
)
725 return WIFI_ERROR_NOT_SUPPORTED
;
728 wifi_error
wifi_set_scanning_mac_oui(wifi_interface_handle handle
, oui scan_oui
)
730 SetPnoMacAddrOuiCommand
command(handle
, scan_oui
);
731 return (wifi_error
)command
.start();
735 wifi_error
wifi_set_nodfs_flag(wifi_interface_handle handle
, u32 nodfs
)
737 SetNodfsCommand
command(handle
, nodfs
);
738 return (wifi_error
) command
.requestResponse();
741 /////////////////////////////////////////////////////////////////////////////