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
;
124 fn
->wifi_start_sending_offloaded_packet
= wifi_start_sending_offloaded_packet
;
125 fn
->wifi_stop_sending_offloaded_packet
= wifi_stop_sending_offloaded_packet
;
129 wifi_error
wifi_initialize(wifi_handle
*handle
)
133 ALOGI("Initializing wifi");
134 hal_info
*info
= (hal_info
*)malloc(sizeof(hal_info
));
136 ALOGE("Could not allocate hal_info");
137 return WIFI_ERROR_UNKNOWN
;
140 memset(info
, 0, sizeof(*info
));
142 ALOGI("Creating socket");
143 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, info
->cleanup_socks
) == -1) {
144 ALOGE("Could not create cleanup sockets");
146 return WIFI_ERROR_UNKNOWN
;
149 struct nl_sock
*cmd_sock
= wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT
);
150 if (cmd_sock
== NULL
) {
151 ALOGE("Could not create handle");
153 return WIFI_ERROR_UNKNOWN
;
156 struct nl_sock
*event_sock
= wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT
);
157 if (event_sock
== NULL
) {
158 ALOGE("Could not create handle");
159 nl_socket_free(cmd_sock
);
161 return WIFI_ERROR_UNKNOWN
;
164 struct nl_cb
*cb
= nl_socket_get_cb(event_sock
);
166 ALOGE("Could not create handle");
167 nl_socket_free(cmd_sock
);
168 nl_socket_free(event_sock
);
170 return WIFI_ERROR_UNKNOWN
;
173 // ALOGI("cb->refcnt = %d", cb->cb_refcnt);
174 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, internal_no_seq_check
, info
);
175 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, internal_valid_message_handler
, info
);
178 info
->cmd_sock
= cmd_sock
;
179 info
->event_sock
= event_sock
;
180 info
->clean_up
= false;
181 info
->in_event_loop
= false;
183 info
->event_cb
= (cb_info
*)malloc(sizeof(cb_info
) * DEFAULT_EVENT_CB_SIZE
);
184 info
->alloc_event_cb
= DEFAULT_EVENT_CB_SIZE
;
185 info
->num_event_cb
= 0;
187 info
->cmd
= (cmd_info
*)malloc(sizeof(cmd_info
) * DEFAULT_CMD_SIZE
);
188 info
->alloc_cmd
= DEFAULT_CMD_SIZE
;
191 info
->nl80211_family_id
= genl_ctrl_resolve(cmd_sock
, "nl80211");
192 if (info
->nl80211_family_id
< 0) {
193 ALOGE("Could not resolve nl80211 familty id");
194 nl_socket_free(cmd_sock
);
195 nl_socket_free(event_sock
);
197 return WIFI_ERROR_UNKNOWN
;
200 pthread_mutex_init(&info
->cb_lock
, NULL
);
202 *handle
= (wifi_handle
) info
;
203 ALOGD("wifi_initialize, handle = %p\n", handle
);
204 ALOGD("wifi_initialize, *handle = %p\n", *handle
);
205 ALOGD("wifi_initialize, info = %p\n", info
);
206 ALOGD("wifi_initialize, *info = %pn", *info
);
207 wifi_add_membership(*handle
, "scan");
208 wifi_add_membership(*handle
, "mlme");
209 wifi_add_membership(*handle
, "regulatory");
210 wifi_add_membership(*handle
, "vendor");
212 wifi_init_interfaces(*handle
);
213 ALOGD("Found %d interfaces", info
->num_interfaces
);
216 ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR
);
220 static int wifi_add_membership(wifi_handle handle
, const char *group
)
222 hal_info
*info
= getHalInfo(handle
);
224 int id
= wifi_get_multicast_id(handle
, "nl80211", group
);
226 ALOGE("Could not find group %s", group
);
230 int ret
= nl_socket_add_membership(info
->event_sock
, id
);
232 ALOGE("Could not add membership to group %s", group
);
235 ALOGI("Successfully added membership for group %s", group
);
239 static void internal_cleaned_up_handler(wifi_handle handle
)
241 hal_info
*info
= getHalInfo(handle
);
242 wifi_cleaned_up_handler cleaned_up_handler
= info
->cleaned_up_handler
;
244 if (info
->cmd_sock
!= 0) {
245 close(info
->cleanup_socks
[0]);
246 close(info
->cleanup_socks
[1]);
247 nl_socket_free(info
->cmd_sock
);
248 nl_socket_free(info
->event_sock
);
249 info
->cmd_sock
= NULL
;
250 info
->event_sock
= NULL
;
253 (*cleaned_up_handler
)(handle
);
254 pthread_mutex_destroy(&info
->cb_lock
);
257 ALOGI("Internal cleanup completed");
260 void wifi_cleanup(wifi_handle handle
, wifi_cleaned_up_handler handler
)
262 hal_info
*info
= getHalInfo(handle
);
265 info
->cleaned_up_handler
= handler
;
266 if (write(info
->cleanup_socks
[0], "Exit", 4) < 1) {
267 ALOGE("could not write to the cleanup socket");
269 // Listen to the response
270 // Hopefully we dont get errors or get hung up
271 // Not much can be done in that case, but assume that
272 // it has rx'ed the Exit message to exit the thread.
273 // As a fallback set the cleanup flag to TRUE
274 memset(buf
, 0, sizeof(buf
));
275 int result
= read(info
->cleanup_socks
[0], buf
, sizeof(buf
));
276 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result
, errno
);
277 if (strncmp(buf
, "Done", 4) == 0) {
278 ALOGE("Event processing terminated");
280 ALOGD("Rx'ed %s", buf
);
283 info
->clean_up
= true;
284 pthread_mutex_lock(&info
->cb_lock
);
286 int bad_commands
= 0;
288 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
289 cb_info
*cbi
= &(info
->event_cb
[i
]);
290 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
293 while (info
->num_cmd
> bad_commands
) {
294 int num_cmd
= info
->num_cmd
;
295 cmd_info
*cmdi
= &(info
->cmd
[bad_commands
]);
296 WifiCommand
*cmd
= cmdi
->cmd
;
298 pthread_mutex_unlock(&info
->cb_lock
);
300 pthread_mutex_lock(&info
->cb_lock
);
301 /* release reference added when command is saved */
303 if (num_cmd
== info
->num_cmd
) {
309 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
310 cb_info
*cbi
= &(info
->event_cb
[i
]);
311 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
312 ALOGE("Leaked command %p", cmd
);
314 pthread_mutex_unlock(&info
->cb_lock
);
315 internal_cleaned_up_handler(handle
);
318 static int internal_pollin_handler(wifi_handle handle
)
320 hal_info
*info
= getHalInfo(handle
);
321 ALOGI("even_loop info = %p", info
);
322 struct nl_cb
*cb
= nl_socket_get_cb(info
->event_sock
);
323 int res
= nl_recvmsgs(info
->event_sock
, cb
);
324 ALOGD("nl_recvmsgs returned %d", res
);
329 /* Run event handler */
330 void wifi_event_loop(wifi_handle handle
)
332 hal_info
*info
= getHalInfo(handle
);
333 ALOGI("even_loop info = %p", info
);
334 ALOGI("even_loop info = %p", handle
);
335 if (info
->in_event_loop
) {
338 info
->in_event_loop
= true;
342 memset(&pfd
[0], 0, sizeof(pollfd
) * 2);
344 pfd
[0].fd
= nl_socket_get_fd(info
->event_sock
);
345 pfd
[0].events
= POLLIN
;
346 pfd
[1].fd
= info
->cleanup_socks
[1];
347 pfd
[1].events
= POLLIN
;
352 int timeout
= -1; /* Infinite timeout */
355 int result
= poll(pfd
, 2, timeout
);
357 } else if (pfd
[0].revents
& POLLERR
) {
358 ALOGE("POLL Error; error no = %d", errno
);
359 int result2
= read(pfd
[0].fd
, buf
, sizeof(buf
));
360 ALOGE("Read after POLL returned %d, error no = %d", result2
, errno
);
361 } else if (pfd
[0].revents
& POLLHUP
) {
362 ALOGE("Remote side hung up");
364 } else if (pfd
[0].revents
& POLLIN
) {
365 internal_pollin_handler(handle
);
366 } else if (pfd
[1].revents
& POLLIN
) {
367 memset(buf
, 0, sizeof(buf
));
368 int result2
= read(pfd
[1].fd
, buf
, sizeof(buf
));
369 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result2
, errno
);
370 if (strncmp(buf
, "Exit", 4) == 0) {
371 ALOGD("Got a signal to exit!!!");
372 if (write(pfd
[1].fd
, "Done", 4) < 1) {
373 ALOGE("could not write to the cleanup socket");
377 ALOGD("Rx'ed %s on the cleanup socket\n", buf
);
380 ALOGE("Unknown event - %0x, %0x", pfd
[0].revents
, pfd
[1].revents
);
382 } while (!info
->clean_up
);
383 ALOGI("Exit %s", __FUNCTION__
);
386 ///////////////////////////////////////////////////////////////////////////////////////
388 static int internal_no_seq_check(struct nl_msg
*msg
, void *arg
)
393 static int internal_valid_message_handler(nl_msg
*msg
, void *arg
)
395 wifi_handle handle
= (wifi_handle
)arg
;
396 hal_info
*info
= getHalInfo(handle
);
397 ALOGI("even_loop info = %p", handle
);
398 ALOGD("internal_valid_message_handler, info = %p", info
);
400 WifiEvent
event(msg
);
401 int res
= event
.parse();
403 ALOGE("Failed to parse event: %d", res
);
407 int cmd
= event
.get_cmd();
408 uint32_t vendor_id
= 0;
411 if (cmd
== NL80211_CMD_VENDOR
) {
412 vendor_id
= event
.get_u32(NL80211_ATTR_VENDOR_ID
);
413 subcmd
= event
.get_u32(NL80211_ATTR_VENDOR_SUBCMD
);
414 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
415 event
.get_cmdString(), vendor_id
, subcmd
);
417 ALOGI("event received %s", event
.get_cmdString());
420 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
423 bool dispatched
= false;
425 pthread_mutex_lock(&info
->cb_lock
);
427 ALOGI("Number of events %d", info
->num_event_cb
);
429 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
430 if (cmd
== info
->event_cb
[i
].nl_cmd
) {
431 if (cmd
== NL80211_CMD_VENDOR
432 && ((vendor_id
!= info
->event_cb
[i
].vendor_id
)
433 || (subcmd
!= info
->event_cb
[i
].vendor_subcmd
)))
435 /* event for a different vendor, ignore it */
439 cb_info
*cbi
= &(info
->event_cb
[i
]);
440 nl_recvmsg_msg_cb_t cb_func
= cbi
->cb_func
;
441 void *cb_arg
= cbi
->cb_arg
;
442 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
447 pthread_mutex_unlock(&info
->cb_lock
);
449 (*cb_func
)(msg
, cb_arg
);
458 pthread_mutex_unlock(&info
->cb_lock
);
462 ///////////////////////////////////////////////////////////////////////////////////////
464 class GetMulticastIdCommand
: public WifiCommand
471 GetMulticastIdCommand(wifi_handle handle
, const char *name
, const char *group
)
472 : WifiCommand(handle
, 0)
483 virtual int create() {
484 int nlctrlFamily
= genl_ctrl_resolve(mInfo
->cmd_sock
, "nlctrl");
485 ALOGI("ctrl family = %d", nlctrlFamily
);
486 int ret
= mMsg
.create(nlctrlFamily
, CTRL_CMD_GETFAMILY
, 0, 0);
490 ret
= mMsg
.put_string(CTRL_ATTR_FAMILY_NAME
, mName
);
494 virtual int handleResponse(WifiEvent
& reply
) {
496 ALOGE("handling reponse in %s", __func__
);
498 struct nlattr
**tb
= reply
.attributes();
499 struct genlmsghdr
*gnlh
= reply
.header();
500 struct nlattr
*mcgrp
= NULL
;
503 if (!tb
[CTRL_ATTR_MCAST_GROUPS
]) {
504 ALOGE("No multicast groups found");
507 ALOGE("Multicast groups attr size = %d", nla_len(tb
[CTRL_ATTR_MCAST_GROUPS
]));
510 for_each_attr(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
512 ALOGE("Processing group");
513 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
514 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, (nlattr
*)nla_data(mcgrp
),
515 nla_len(mcgrp
), NULL
);
516 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] || !tb2
[CTRL_ATTR_MCAST_GRP_ID
]) {
520 char *grpName
= (char *)nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
521 int grpNameLen
= nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
523 ALOGE("Found group name %s", grpName
);
525 if (strncmp(grpName
, mGroup
, grpNameLen
) != 0)
528 mId
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
537 class SetPnoMacAddrOuiCommand
: public WifiCommand
{
542 feature_set
*feature_matrix
;
546 SetPnoMacAddrOuiCommand(wifi_interface_handle handle
, oui scan_oui
)
547 : WifiCommand(handle
, 0)
552 int createRequest(WifiRequest
& request
, int subcmd
, byte
*scan_oui
) {
553 int result
= request
.create(GOOGLE_OUI
, subcmd
);
558 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
559 result
= request
.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
, scan_oui
, DOT11_OUI_LEN
);
564 request
.attr_end(data
);
570 ALOGD("Sending mac address OUI");
571 WifiRequest
request(familyId(), ifaceId());
572 int result
= createRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI
, mOui
);
573 if (result
!= WIFI_SUCCESS
) {
574 ALOGE("failed to create request; result = %d", result
);
578 result
= requestResponse(request
);
579 if (result
!= WIFI_SUCCESS
) {
580 ALOGE("failed to set scanning mac OUI; result = %d", result
);
586 virtual int handleResponse(WifiEvent
& reply
) {
587 ALOGD("Request complete!");
588 /* Nothing to do on response! */
593 class SetNodfsCommand
: public WifiCommand
{
598 SetNodfsCommand(wifi_interface_handle handle
, u32 nodfs
)
599 : WifiCommand(handle
, 0) {
602 virtual int create() {
605 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS
);
607 ALOGE("Can't create message to send to driver - %d", ret
);
611 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
612 ret
= mMsg
.put_u32(ATTR_NODFS_VALUE
, mNoDfs
);
622 static int wifi_get_multicast_id(wifi_handle handle
, const char *name
, const char *group
)
624 GetMulticastIdCommand
cmd(handle
, name
, group
);
625 int res
= cmd
.requestResponse();
632 /////////////////////////////////////////////////////////////////////////
634 static bool is_wifi_interface(const char *name
)
636 if (strncmp(name
, "wlan", 4) != 0 && strncmp(name
, "p2p", 3) != 0) {
637 /* not a wifi interface; ignore it */
644 static int get_interface(const char *name
, interface_info
*info
)
646 strcpy(info
->name
, name
);
647 info
->id
= if_nametoindex(name
);
648 ALOGI("found an interface : %s, id = %d", name
, info
->id
);
652 wifi_error
wifi_init_interfaces(wifi_handle handle
)
654 hal_info
*info
= (hal_info
*)handle
;
655 ALOGD("wifi_init_interfaces, info = %p", info
);
659 DIR *d
= opendir("/sys/class/net");
661 return WIFI_ERROR_UNKNOWN
;
664 while ((de
= readdir(d
))) {
665 if (de
->d_name
[0] == '.')
667 if (is_wifi_interface(de
->d_name
) ) {
674 d
= opendir("/sys/class/net");
676 return WIFI_ERROR_UNKNOWN
;
678 info
->interfaces
= (interface_info
**)malloc(sizeof(interface_info
*) * n
);
681 while ((de
= readdir(d
))) {
682 if (de
->d_name
[0] == '.')
684 if (is_wifi_interface(de
->d_name
)) {
685 interface_info
*ifinfo
= (interface_info
*)malloc(sizeof(interface_info
));
686 if (get_interface(de
->d_name
, ifinfo
) != WIFI_SUCCESS
) {
690 ifinfo
->handle
= handle
;
691 info
->interfaces
[i
] = ifinfo
;
698 info
->num_interfaces
= n
;
702 wifi_error
wifi_get_ifaces(wifi_handle handle
, int *num
, wifi_interface_handle
**interfaces
)
704 hal_info
*info
= (hal_info
*)handle
;
706 *interfaces
= (wifi_interface_handle
*)info
->interfaces
;
707 *num
= info
->num_interfaces
;
712 wifi_error
wifi_get_iface_name(wifi_interface_handle handle
, char *name
, size_t size
)
714 interface_info
*info
= (interface_info
*)handle
;
715 strcpy(name
, info
->name
);
719 wifi_error
wifi_get_supported_feature_set(wifi_interface_handle handle
, feature_set
*set
)
721 return WIFI_ERROR_NOT_SUPPORTED
;
724 wifi_error
wifi_get_concurrency_matrix(wifi_interface_handle handle
, int set_size_max
,
725 feature_set set
[], int *set_size
)
727 return WIFI_ERROR_NOT_SUPPORTED
;
730 wifi_error
wifi_set_scanning_mac_oui(wifi_interface_handle handle
, oui scan_oui
)
732 SetPnoMacAddrOuiCommand
command(handle
, scan_oui
);
733 return (wifi_error
)command
.start();
737 wifi_error
wifi_set_nodfs_flag(wifi_interface_handle handle
, u32 nodfs
)
739 SetNodfsCommand
command(handle
, nodfs
);
740 return (wifi_error
) command
.requestResponse();
743 /////////////////////////////////////////////////////////////////////////////