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 ALOGI("Connecting socket");
75 if (nl_connect(sock
, NETLINK_GENERIC
)) {
76 ALOGE("Could not connect handle");
84 /* Initialize HAL function pointer table */
85 wifi_error
init_wifi_vendor_hal_func_table(wifi_hal_fn
*fn
)
88 return WIFI_ERROR_UNKNOWN
;
90 fn
->wifi_initialize
= wifi_initialize
;
91 fn
->wifi_cleanup
= wifi_cleanup
;
92 fn
->wifi_event_loop
= wifi_event_loop
;
93 fn
->wifi_get_supported_feature_set
= wifi_get_supported_feature_set
;
94 fn
->wifi_get_concurrency_matrix
= wifi_get_concurrency_matrix
;
95 fn
->wifi_set_scanning_mac_oui
= wifi_set_scanning_mac_oui
;
96 fn
->wifi_get_ifaces
= wifi_get_ifaces
;
97 fn
->wifi_get_iface_name
= wifi_get_iface_name
;
98 fn
->wifi_start_gscan
= wifi_start_gscan
;
99 fn
->wifi_stop_gscan
= wifi_stop_gscan
;
100 fn
->wifi_get_cached_gscan_results
= wifi_get_cached_gscan_results
;
101 fn
->wifi_set_bssid_hotlist
= wifi_set_bssid_hotlist
;
102 fn
->wifi_reset_bssid_hotlist
= wifi_reset_bssid_hotlist
;
103 fn
->wifi_set_significant_change_handler
= wifi_set_significant_change_handler
;
104 fn
->wifi_reset_significant_change_handler
= wifi_reset_significant_change_handler
;
105 fn
->wifi_get_gscan_capabilities
= wifi_get_gscan_capabilities
;
106 fn
->wifi_get_link_stats
= wifi_get_link_stats
;
107 fn
->wifi_get_valid_channels
= wifi_get_valid_channels
;
108 fn
->wifi_rtt_range_request
= wifi_rtt_range_request
;
109 fn
->wifi_rtt_range_cancel
= wifi_rtt_range_cancel
;
110 fn
->wifi_get_rtt_capabilities
= wifi_get_rtt_capabilities
;
111 fn
->wifi_set_nodfs_flag
= wifi_set_nodfs_flag
;
112 fn
->wifi_start_sending_offloaded_packet
= wifi_start_sending_offloaded_packet
;
113 fn
->wifi_stop_sending_offloaded_packet
= wifi_stop_sending_offloaded_packet
;
114 fn
->wifi_set_bssid_blacklist
= wifi_set_bssid_blacklist
;
118 wifi_error
wifi_initialize(wifi_handle
*handle
)
122 ALOGI("Initializing wifi");
123 hal_info
*info
= (hal_info
*)malloc(sizeof(hal_info
));
125 ALOGE("Could not allocate hal_info");
126 return WIFI_ERROR_UNKNOWN
;
129 memset(info
, 0, sizeof(*info
));
131 ALOGI("Creating socket");
132 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, info
->cleanup_socks
) == -1) {
133 ALOGE("Could not create cleanup sockets");
135 return WIFI_ERROR_UNKNOWN
;
138 struct nl_sock
*cmd_sock
= wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT
);
139 if (cmd_sock
== NULL
) {
140 ALOGE("Could not create handle");
142 return WIFI_ERROR_UNKNOWN
;
145 struct nl_sock
*event_sock
= wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT
);
146 if (event_sock
== NULL
) {
147 ALOGE("Could not create handle");
148 nl_socket_free(cmd_sock
);
150 return WIFI_ERROR_UNKNOWN
;
153 struct nl_cb
*cb
= nl_socket_get_cb(event_sock
);
155 ALOGE("Could not create handle");
156 nl_socket_free(cmd_sock
);
157 nl_socket_free(event_sock
);
159 return WIFI_ERROR_UNKNOWN
;
162 // ALOGI("cb->refcnt = %d", cb->cb_refcnt);
163 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, internal_no_seq_check
, info
);
164 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, internal_valid_message_handler
, info
);
167 info
->cmd_sock
= cmd_sock
;
168 info
->event_sock
= event_sock
;
169 info
->clean_up
= false;
170 info
->in_event_loop
= false;
172 info
->event_cb
= (cb_info
*)malloc(sizeof(cb_info
) * DEFAULT_EVENT_CB_SIZE
);
173 info
->alloc_event_cb
= DEFAULT_EVENT_CB_SIZE
;
174 info
->num_event_cb
= 0;
176 info
->cmd
= (cmd_info
*)malloc(sizeof(cmd_info
) * DEFAULT_CMD_SIZE
);
177 info
->alloc_cmd
= DEFAULT_CMD_SIZE
;
180 info
->nl80211_family_id
= genl_ctrl_resolve(cmd_sock
, "nl80211");
181 if (info
->nl80211_family_id
< 0) {
182 ALOGE("Could not resolve nl80211 familty id");
183 nl_socket_free(cmd_sock
);
184 nl_socket_free(event_sock
);
186 return WIFI_ERROR_UNKNOWN
;
189 pthread_mutex_init(&info
->cb_lock
, NULL
);
191 *handle
= (wifi_handle
) info
;
192 ALOGD("wifi_initialize, handle = %p\n", handle
);
193 ALOGD("wifi_initialize, *handle = %p\n", *handle
);
194 ALOGD("wifi_initialize, info = %p\n", info
);
195 ALOGD("wifi_initialize, *info = %pn", *info
);
196 wifi_add_membership(*handle
, "scan");
197 wifi_add_membership(*handle
, "mlme");
198 wifi_add_membership(*handle
, "regulatory");
199 wifi_add_membership(*handle
, "vendor");
201 wifi_init_interfaces(*handle
);
202 ALOGD("Found %d interfaces", info
->num_interfaces
);
205 ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR
);
209 static int wifi_add_membership(wifi_handle handle
, const char *group
)
211 hal_info
*info
= getHalInfo(handle
);
213 int id
= wifi_get_multicast_id(handle
, "nl80211", group
);
215 ALOGE("Could not find group %s", group
);
219 int ret
= nl_socket_add_membership(info
->event_sock
, id
);
221 ALOGE("Could not add membership to group %s", group
);
224 ALOGI("Successfully added membership for group %s", group
);
228 static void internal_cleaned_up_handler(wifi_handle handle
)
230 hal_info
*info
= getHalInfo(handle
);
231 wifi_cleaned_up_handler cleaned_up_handler
= info
->cleaned_up_handler
;
233 if (info
->cmd_sock
!= 0) {
234 close(info
->cleanup_socks
[0]);
235 close(info
->cleanup_socks
[1]);
236 nl_socket_free(info
->cmd_sock
);
237 nl_socket_free(info
->event_sock
);
238 info
->cmd_sock
= NULL
;
239 info
->event_sock
= NULL
;
242 (*cleaned_up_handler
)(handle
);
243 pthread_mutex_destroy(&info
->cb_lock
);
246 ALOGI("Internal cleanup completed");
249 void wifi_cleanup(wifi_handle handle
, wifi_cleaned_up_handler handler
)
251 hal_info
*info
= getHalInfo(handle
);
254 info
->cleaned_up_handler
= handler
;
255 if (write(info
->cleanup_socks
[0], "Exit", 4) < 1) {
256 ALOGE("could not write to the cleanup socket");
258 // Listen to the response
259 // Hopefully we dont get errors or get hung up
260 // Not much can be done in that case, but assume that
261 // it has rx'ed the Exit message to exit the thread.
262 // As a fallback set the cleanup flag to TRUE
263 memset(buf
, 0, sizeof(buf
));
264 int result
= read(info
->cleanup_socks
[0], buf
, sizeof(buf
));
265 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result
, errno
);
266 if (strncmp(buf
, "Done", 4) == 0) {
267 ALOGE("Event processing terminated");
269 ALOGD("Rx'ed %s", buf
);
272 info
->clean_up
= true;
273 pthread_mutex_lock(&info
->cb_lock
);
275 int bad_commands
= 0;
277 while (info
->num_cmd
> bad_commands
) {
278 int num_cmd
= info
->num_cmd
;
279 cmd_info
*cmdi
= &(info
->cmd
[bad_commands
]);
280 WifiCommand
*cmd
= cmdi
->cmd
;
282 pthread_mutex_unlock(&info
->cb_lock
);
284 pthread_mutex_lock(&info
->cb_lock
);
285 /* release reference added when command is saved */
287 if (num_cmd
== info
->num_cmd
) {
293 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
294 cb_info
*cbi
= &(info
->event_cb
[i
]);
295 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
296 ALOGE("Leaked command %p", cmd
);
298 pthread_mutex_unlock(&info
->cb_lock
);
299 internal_cleaned_up_handler(handle
);
302 static int internal_pollin_handler(wifi_handle handle
)
304 hal_info
*info
= getHalInfo(handle
);
305 ALOGI("even_loop info = %p", info
);
306 struct nl_cb
*cb
= nl_socket_get_cb(info
->event_sock
);
307 int res
= nl_recvmsgs(info
->event_sock
, cb
);
308 ALOGD("nl_recvmsgs returned %d", res
);
313 /* Run event handler */
314 void wifi_event_loop(wifi_handle handle
)
316 hal_info
*info
= getHalInfo(handle
);
317 ALOGI("even_loop info = %p", info
);
318 ALOGI("even_loop info = %p", handle
);
319 if (info
->in_event_loop
) {
322 info
->in_event_loop
= true;
326 memset(&pfd
[0], 0, sizeof(pollfd
) * 2);
328 pfd
[0].fd
= nl_socket_get_fd(info
->event_sock
);
329 pfd
[0].events
= POLLIN
;
330 pfd
[1].fd
= info
->cleanup_socks
[1];
331 pfd
[1].events
= POLLIN
;
336 int timeout
= -1; /* Infinite timeout */
339 int result
= poll(pfd
, 2, timeout
);
341 } else if (pfd
[0].revents
& POLLERR
) {
342 ALOGE("POLL Error; error no = %d", errno
);
343 int result2
= read(pfd
[0].fd
, buf
, sizeof(buf
));
344 ALOGE("Read after POLL returned %d, error no = %d", result2
, errno
);
345 } else if (pfd
[0].revents
& POLLHUP
) {
346 ALOGE("Remote side hung up");
348 } else if (pfd
[0].revents
& POLLIN
) {
349 internal_pollin_handler(handle
);
350 } else if (pfd
[1].revents
& POLLIN
) {
351 memset(buf
, 0, sizeof(buf
));
352 int result2
= read(pfd
[1].fd
, buf
, sizeof(buf
));
353 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result2
, errno
);
354 if (strncmp(buf
, "Exit", 4) == 0) {
355 ALOGD("Got a signal to exit!!!");
356 if (write(pfd
[1].fd
, "Done", 4) < 1) {
357 ALOGE("could not write to the cleanup socket");
361 ALOGD("Rx'ed %s on the cleanup socket\n", buf
);
364 ALOGE("Unknown event - %0x, %0x", pfd
[0].revents
, pfd
[1].revents
);
366 } while (!info
->clean_up
);
367 ALOGI("Exit %s", __FUNCTION__
);
370 ///////////////////////////////////////////////////////////////////////////////////////
372 static int internal_no_seq_check(struct nl_msg
*msg
, void *arg
)
377 static int internal_valid_message_handler(nl_msg
*msg
, void *arg
)
379 wifi_handle handle
= (wifi_handle
)arg
;
380 hal_info
*info
= getHalInfo(handle
);
381 ALOGI("even_loop info = %p", handle
);
382 ALOGD("internal_valid_message_handler, info = %p", info
);
384 WifiEvent
event(msg
);
385 int res
= event
.parse();
387 ALOGE("Failed to parse event: %d", res
);
391 int cmd
= event
.get_cmd();
392 uint32_t vendor_id
= 0;
395 if (cmd
== NL80211_CMD_VENDOR
) {
396 vendor_id
= event
.get_u32(NL80211_ATTR_VENDOR_ID
);
397 subcmd
= event
.get_u32(NL80211_ATTR_VENDOR_SUBCMD
);
398 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
399 event
.get_cmdString(), vendor_id
, subcmd
);
401 ALOGI("event received %s", event
.get_cmdString());
404 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
407 pthread_mutex_lock(&info
->cb_lock
);
409 ALOGI("Number of events %d", info
->num_event_cb
);
411 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
412 if (cmd
== info
->event_cb
[i
].nl_cmd
) {
413 if (cmd
== NL80211_CMD_VENDOR
414 && ((vendor_id
!= info
->event_cb
[i
].vendor_id
)
415 || (subcmd
!= info
->event_cb
[i
].vendor_subcmd
)))
417 /* event for a different vendor, ignore it */
421 cb_info
*cbi
= &(info
->event_cb
[i
]);
422 nl_recvmsg_msg_cb_t cb_func
= cbi
->cb_func
;
423 void *cb_arg
= cbi
->cb_arg
;
424 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
429 pthread_mutex_unlock(&info
->cb_lock
);
431 (*cb_func
)(msg
, cb_arg
);
440 pthread_mutex_unlock(&info
->cb_lock
);
444 ///////////////////////////////////////////////////////////////////////////////////////
446 class GetMulticastIdCommand
: public WifiCommand
453 GetMulticastIdCommand(wifi_handle handle
, const char *name
, const char *group
)
454 : WifiCommand(handle
, 0)
465 virtual int create() {
466 int nlctrlFamily
= genl_ctrl_resolve(mInfo
->cmd_sock
, "nlctrl");
467 ALOGI("ctrl family = %d", nlctrlFamily
);
468 int ret
= mMsg
.create(nlctrlFamily
, CTRL_CMD_GETFAMILY
, 0, 0);
472 ret
= mMsg
.put_string(CTRL_ATTR_FAMILY_NAME
, mName
);
476 virtual int handleResponse(WifiEvent
& reply
) {
478 ALOGE("handling reponse in %s", __func__
);
480 struct nlattr
**tb
= reply
.attributes();
481 struct nlattr
*mcgrp
= NULL
;
484 if (!tb
[CTRL_ATTR_MCAST_GROUPS
]) {
485 ALOGE("No multicast groups found");
488 ALOGE("Multicast groups attr size = %d", nla_len(tb
[CTRL_ATTR_MCAST_GROUPS
]));
491 for_each_attr(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
493 ALOGE("Processing group");
494 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
495 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, (nlattr
*)nla_data(mcgrp
),
496 nla_len(mcgrp
), NULL
);
497 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] || !tb2
[CTRL_ATTR_MCAST_GRP_ID
]) {
501 char *grpName
= (char *)nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
502 int grpNameLen
= nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
504 ALOGE("Found group name %s", grpName
);
506 if (strncmp(grpName
, mGroup
, grpNameLen
) != 0)
509 mId
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
518 class SetPnoMacAddrOuiCommand
: public WifiCommand
{
523 feature_set
*feature_matrix
;
527 SetPnoMacAddrOuiCommand(wifi_interface_handle handle
, oui scan_oui
)
528 : WifiCommand(handle
, 0)
533 int createRequest(WifiRequest
& request
, int subcmd
, byte
*scan_oui
) {
534 int result
= request
.create(GOOGLE_OUI
, subcmd
);
539 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
540 result
= request
.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
, scan_oui
, DOT11_OUI_LEN
);
545 request
.attr_end(data
);
551 ALOGD("Sending mac address OUI");
552 WifiRequest
request(familyId(), ifaceId());
553 int result
= createRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI
, mOui
);
554 if (result
!= WIFI_SUCCESS
) {
555 ALOGE("failed to create request; result = %d", result
);
559 result
= requestResponse(request
);
560 if (result
!= WIFI_SUCCESS
) {
561 ALOGE("failed to set scanning mac OUI; result = %d", result
);
567 virtual int handleResponse(WifiEvent
& reply
) {
568 ALOGD("Request complete!");
569 /* Nothing to do on response! */
574 class SetNodfsCommand
: public WifiCommand
{
579 SetNodfsCommand(wifi_interface_handle handle
, u32 nodfs
)
580 : WifiCommand(handle
, 0) {
583 virtual int create() {
586 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS
);
588 ALOGE("Can't create message to send to driver - %d", ret
);
592 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
593 ret
= mMsg
.put_u32(ATTR_NODFS_VALUE
, mNoDfs
);
603 static int wifi_get_multicast_id(wifi_handle handle
, const char *name
, const char *group
)
605 GetMulticastIdCommand
cmd(handle
, name
, group
);
606 int res
= cmd
.requestResponse();
613 /////////////////////////////////////////////////////////////////////////
615 static bool is_wifi_interface(const char *name
)
617 if (strncmp(name
, "wlan", 4) != 0 && strncmp(name
, "p2p", 3) != 0) {
618 /* not a wifi interface; ignore it */
625 static int get_interface(const char *name
, interface_info
*info
)
627 strcpy(info
->name
, name
);
628 info
->id
= if_nametoindex(name
);
629 ALOGI("found an interface : %s, id = %d", name
, info
->id
);
633 wifi_error
wifi_init_interfaces(wifi_handle handle
)
635 hal_info
*info
= (hal_info
*)handle
;
636 ALOGD("wifi_init_interfaces, info = %p", info
);
640 DIR *d
= opendir("/sys/class/net");
642 return WIFI_ERROR_UNKNOWN
;
645 while ((de
= readdir(d
))) {
646 if (de
->d_name
[0] == '.')
648 if (is_wifi_interface(de
->d_name
) ) {
655 d
= opendir("/sys/class/net");
657 return WIFI_ERROR_UNKNOWN
;
659 info
->interfaces
= (interface_info
**)malloc(sizeof(interface_info
*) * n
);
662 while ((de
= readdir(d
))) {
663 if (de
->d_name
[0] == '.')
665 if (is_wifi_interface(de
->d_name
)) {
666 interface_info
*ifinfo
= (interface_info
*)malloc(sizeof(interface_info
));
667 if (get_interface(de
->d_name
, ifinfo
) != WIFI_SUCCESS
) {
671 ifinfo
->handle
= handle
;
672 info
->interfaces
[i
] = ifinfo
;
679 info
->num_interfaces
= n
;
683 wifi_error
wifi_get_ifaces(wifi_handle handle
, int *num
, wifi_interface_handle
**interfaces
)
685 hal_info
*info
= (hal_info
*)handle
;
687 *interfaces
= (wifi_interface_handle
*)info
->interfaces
;
688 *num
= info
->num_interfaces
;
693 wifi_error
wifi_get_iface_name(wifi_interface_handle handle
, char *name
, size_t size
)
695 interface_info
*info
= (interface_info
*)handle
;
696 strcpy(name
, info
->name
);
700 wifi_error
wifi_get_supported_feature_set(wifi_interface_handle handle
, feature_set
*set
)
702 return WIFI_ERROR_NOT_SUPPORTED
;
705 wifi_error
wifi_get_concurrency_matrix(wifi_interface_handle handle
, int set_size_max
,
706 feature_set set
[], int *set_size
)
708 return WIFI_ERROR_NOT_SUPPORTED
;
711 wifi_error
wifi_set_scanning_mac_oui(wifi_interface_handle handle
, oui scan_oui
)
713 SetPnoMacAddrOuiCommand
command(handle
, scan_oui
);
714 return (wifi_error
)command
.start();
718 wifi_error
wifi_set_nodfs_flag(wifi_interface_handle handle
, u32 nodfs
)
720 SetNodfsCommand
command(handle
, nodfs
);
721 return (wifi_error
) command
.requestResponse();
724 /////////////////////////////////////////////////////////////////////////////