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
;
117 wifi_error
wifi_initialize(wifi_handle
*handle
)
121 ALOGI("Initializing wifi");
122 hal_info
*info
= (hal_info
*)malloc(sizeof(hal_info
));
124 ALOGE("Could not allocate hal_info");
125 return WIFI_ERROR_UNKNOWN
;
128 memset(info
, 0, sizeof(*info
));
130 ALOGI("Creating socket");
131 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, info
->cleanup_socks
) == -1) {
132 ALOGE("Could not create cleanup sockets");
134 return WIFI_ERROR_UNKNOWN
;
137 struct nl_sock
*cmd_sock
= wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT
);
138 if (cmd_sock
== NULL
) {
139 ALOGE("Could not create handle");
141 return WIFI_ERROR_UNKNOWN
;
144 struct nl_sock
*event_sock
= wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT
);
145 if (event_sock
== NULL
) {
146 ALOGE("Could not create handle");
147 nl_socket_free(cmd_sock
);
149 return WIFI_ERROR_UNKNOWN
;
152 struct nl_cb
*cb
= nl_socket_get_cb(event_sock
);
154 ALOGE("Could not create handle");
155 nl_socket_free(cmd_sock
);
156 nl_socket_free(event_sock
);
158 return WIFI_ERROR_UNKNOWN
;
161 // ALOGI("cb->refcnt = %d", cb->cb_refcnt);
162 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, internal_no_seq_check
, info
);
163 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, internal_valid_message_handler
, info
);
166 info
->cmd_sock
= cmd_sock
;
167 info
->event_sock
= event_sock
;
168 info
->clean_up
= false;
169 info
->in_event_loop
= false;
171 info
->event_cb
= (cb_info
*)malloc(sizeof(cb_info
) * DEFAULT_EVENT_CB_SIZE
);
172 info
->alloc_event_cb
= DEFAULT_EVENT_CB_SIZE
;
173 info
->num_event_cb
= 0;
175 info
->cmd
= (cmd_info
*)malloc(sizeof(cmd_info
) * DEFAULT_CMD_SIZE
);
176 info
->alloc_cmd
= DEFAULT_CMD_SIZE
;
179 info
->nl80211_family_id
= genl_ctrl_resolve(cmd_sock
, "nl80211");
180 if (info
->nl80211_family_id
< 0) {
181 ALOGE("Could not resolve nl80211 familty id");
182 nl_socket_free(cmd_sock
);
183 nl_socket_free(event_sock
);
185 return WIFI_ERROR_UNKNOWN
;
188 pthread_mutex_init(&info
->cb_lock
, NULL
);
190 *handle
= (wifi_handle
) info
;
191 ALOGD("wifi_initialize, handle = %p\n", handle
);
192 ALOGD("wifi_initialize, *handle = %p\n", *handle
);
193 ALOGD("wifi_initialize, info = %p\n", info
);
194 ALOGD("wifi_initialize, *info = %pn", *info
);
195 wifi_add_membership(*handle
, "scan");
196 wifi_add_membership(*handle
, "mlme");
197 wifi_add_membership(*handle
, "regulatory");
198 wifi_add_membership(*handle
, "vendor");
200 wifi_init_interfaces(*handle
);
201 ALOGD("Found %d interfaces", info
->num_interfaces
);
204 ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR
);
208 static int wifi_add_membership(wifi_handle handle
, const char *group
)
210 hal_info
*info
= getHalInfo(handle
);
212 int id
= wifi_get_multicast_id(handle
, "nl80211", group
);
214 ALOGE("Could not find group %s", group
);
218 int ret
= nl_socket_add_membership(info
->event_sock
, id
);
220 ALOGE("Could not add membership to group %s", group
);
223 ALOGI("Successfully added membership for group %s", group
);
227 static void internal_cleaned_up_handler(wifi_handle handle
)
229 hal_info
*info
= getHalInfo(handle
);
230 wifi_cleaned_up_handler cleaned_up_handler
= info
->cleaned_up_handler
;
232 if (info
->cmd_sock
!= 0) {
233 close(info
->cleanup_socks
[0]);
234 close(info
->cleanup_socks
[1]);
235 nl_socket_free(info
->cmd_sock
);
236 nl_socket_free(info
->event_sock
);
237 info
->cmd_sock
= NULL
;
238 info
->event_sock
= NULL
;
241 (*cleaned_up_handler
)(handle
);
242 pthread_mutex_destroy(&info
->cb_lock
);
245 ALOGI("Internal cleanup completed");
248 void wifi_cleanup(wifi_handle handle
, wifi_cleaned_up_handler handler
)
250 hal_info
*info
= getHalInfo(handle
);
253 info
->cleaned_up_handler
= handler
;
254 if (write(info
->cleanup_socks
[0], "Exit", 4) < 1) {
255 ALOGE("could not write to the cleanup socket");
257 // Listen to the response
258 // Hopefully we dont get errors or get hung up
259 // Not much can be done in that case, but assume that
260 // it has rx'ed the Exit message to exit the thread.
261 // As a fallback set the cleanup flag to TRUE
262 memset(buf
, 0, sizeof(buf
));
263 int result
= read(info
->cleanup_socks
[0], buf
, sizeof(buf
));
264 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result
, errno
);
265 if (strncmp(buf
, "Done", 4) == 0) {
266 ALOGE("Event processing terminated");
268 ALOGD("Rx'ed %s", buf
);
271 info
->clean_up
= true;
272 pthread_mutex_lock(&info
->cb_lock
);
274 int bad_commands
= 0;
276 while (info
->num_cmd
> bad_commands
) {
277 int num_cmd
= info
->num_cmd
;
278 cmd_info
*cmdi
= &(info
->cmd
[bad_commands
]);
279 WifiCommand
*cmd
= cmdi
->cmd
;
281 pthread_mutex_unlock(&info
->cb_lock
);
283 pthread_mutex_lock(&info
->cb_lock
);
284 /* release reference added when command is saved */
286 if (num_cmd
== info
->num_cmd
) {
292 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
293 cb_info
*cbi
= &(info
->event_cb
[i
]);
294 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
295 ALOGE("Leaked command %p", cmd
);
297 pthread_mutex_unlock(&info
->cb_lock
);
298 internal_cleaned_up_handler(handle
);
301 static int internal_pollin_handler(wifi_handle handle
)
303 hal_info
*info
= getHalInfo(handle
);
304 ALOGI("even_loop info = %p", info
);
305 struct nl_cb
*cb
= nl_socket_get_cb(info
->event_sock
);
306 int res
= nl_recvmsgs(info
->event_sock
, cb
);
307 ALOGD("nl_recvmsgs returned %d", res
);
312 /* Run event handler */
313 void wifi_event_loop(wifi_handle handle
)
315 hal_info
*info
= getHalInfo(handle
);
316 ALOGI("even_loop info = %p", info
);
317 ALOGI("even_loop info = %p", handle
);
318 if (info
->in_event_loop
) {
321 info
->in_event_loop
= true;
325 memset(&pfd
[0], 0, sizeof(pollfd
) * 2);
327 pfd
[0].fd
= nl_socket_get_fd(info
->event_sock
);
328 pfd
[0].events
= POLLIN
;
329 pfd
[1].fd
= info
->cleanup_socks
[1];
330 pfd
[1].events
= POLLIN
;
335 int timeout
= -1; /* Infinite timeout */
338 int result
= poll(pfd
, 2, timeout
);
340 } else if (pfd
[0].revents
& POLLERR
) {
341 ALOGE("POLL Error; error no = %d", errno
);
342 int result2
= read(pfd
[0].fd
, buf
, sizeof(buf
));
343 ALOGE("Read after POLL returned %d, error no = %d", result2
, errno
);
344 } else if (pfd
[0].revents
& POLLHUP
) {
345 ALOGE("Remote side hung up");
347 } else if (pfd
[0].revents
& POLLIN
) {
348 internal_pollin_handler(handle
);
349 } else if (pfd
[1].revents
& POLLIN
) {
350 memset(buf
, 0, sizeof(buf
));
351 int result2
= read(pfd
[1].fd
, buf
, sizeof(buf
));
352 ALOGE("%s: Read after POLL returned %d, error no = %d", __FUNCTION__
, result2
, errno
);
353 if (strncmp(buf
, "Exit", 4) == 0) {
354 ALOGD("Got a signal to exit!!!");
355 if (write(pfd
[1].fd
, "Done", 4) < 1) {
356 ALOGE("could not write to the cleanup socket");
360 ALOGD("Rx'ed %s on the cleanup socket\n", buf
);
363 ALOGE("Unknown event - %0x, %0x", pfd
[0].revents
, pfd
[1].revents
);
365 } while (!info
->clean_up
);
366 ALOGI("Exit %s", __FUNCTION__
);
369 ///////////////////////////////////////////////////////////////////////////////////////
371 static int internal_no_seq_check(struct nl_msg
*msg
, void *arg
)
376 static int internal_valid_message_handler(nl_msg
*msg
, void *arg
)
378 wifi_handle handle
= (wifi_handle
)arg
;
379 hal_info
*info
= getHalInfo(handle
);
380 ALOGI("even_loop info = %p", handle
);
381 ALOGD("internal_valid_message_handler, info = %p", info
);
383 WifiEvent
event(msg
);
384 int res
= event
.parse();
386 ALOGE("Failed to parse event: %d", res
);
390 int cmd
= event
.get_cmd();
391 uint32_t vendor_id
= 0;
394 if (cmd
== NL80211_CMD_VENDOR
) {
395 vendor_id
= event
.get_u32(NL80211_ATTR_VENDOR_ID
);
396 subcmd
= event
.get_u32(NL80211_ATTR_VENDOR_SUBCMD
);
397 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
398 event
.get_cmdString(), vendor_id
, subcmd
);
400 ALOGI("event received %s", event
.get_cmdString());
403 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
406 pthread_mutex_lock(&info
->cb_lock
);
408 ALOGI("Number of events %d", info
->num_event_cb
);
410 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
411 if (cmd
== info
->event_cb
[i
].nl_cmd
) {
412 if (cmd
== NL80211_CMD_VENDOR
413 && ((vendor_id
!= info
->event_cb
[i
].vendor_id
)
414 || (subcmd
!= info
->event_cb
[i
].vendor_subcmd
)))
416 /* event for a different vendor, ignore it */
420 cb_info
*cbi
= &(info
->event_cb
[i
]);
421 nl_recvmsg_msg_cb_t cb_func
= cbi
->cb_func
;
422 void *cb_arg
= cbi
->cb_arg
;
423 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
428 pthread_mutex_unlock(&info
->cb_lock
);
430 (*cb_func
)(msg
, cb_arg
);
439 pthread_mutex_unlock(&info
->cb_lock
);
443 ///////////////////////////////////////////////////////////////////////////////////////
445 class GetMulticastIdCommand
: public WifiCommand
452 GetMulticastIdCommand(wifi_handle handle
, const char *name
, const char *group
)
453 : WifiCommand(handle
, 0)
464 virtual int create() {
465 int nlctrlFamily
= genl_ctrl_resolve(mInfo
->cmd_sock
, "nlctrl");
466 ALOGI("ctrl family = %d", nlctrlFamily
);
467 int ret
= mMsg
.create(nlctrlFamily
, CTRL_CMD_GETFAMILY
, 0, 0);
471 ret
= mMsg
.put_string(CTRL_ATTR_FAMILY_NAME
, mName
);
475 virtual int handleResponse(WifiEvent
& reply
) {
477 ALOGE("handling reponse in %s", __func__
);
479 struct nlattr
**tb
= reply
.attributes();
480 struct nlattr
*mcgrp
= NULL
;
483 if (!tb
[CTRL_ATTR_MCAST_GROUPS
]) {
484 ALOGE("No multicast groups found");
487 ALOGE("Multicast groups attr size = %d", nla_len(tb
[CTRL_ATTR_MCAST_GROUPS
]));
490 for_each_attr(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
492 ALOGE("Processing group");
493 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
494 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, (nlattr
*)nla_data(mcgrp
),
495 nla_len(mcgrp
), NULL
);
496 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] || !tb2
[CTRL_ATTR_MCAST_GRP_ID
]) {
500 char *grpName
= (char *)nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
501 int grpNameLen
= nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
503 ALOGE("Found group name %s", grpName
);
505 if (strncmp(grpName
, mGroup
, grpNameLen
) != 0)
508 mId
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
517 class SetPnoMacAddrOuiCommand
: public WifiCommand
{
522 feature_set
*feature_matrix
;
526 SetPnoMacAddrOuiCommand(wifi_interface_handle handle
, oui scan_oui
)
527 : WifiCommand(handle
, 0)
532 int createRequest(WifiRequest
& request
, int subcmd
, byte
*scan_oui
) {
533 int result
= request
.create(GOOGLE_OUI
, subcmd
);
538 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
539 result
= request
.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
, scan_oui
, DOT11_OUI_LEN
);
544 request
.attr_end(data
);
550 ALOGD("Sending mac address OUI");
551 WifiRequest
request(familyId(), ifaceId());
552 int result
= createRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI
, mOui
);
553 if (result
!= WIFI_SUCCESS
) {
554 ALOGE("failed to create request; result = %d", result
);
558 result
= requestResponse(request
);
559 if (result
!= WIFI_SUCCESS
) {
560 ALOGE("failed to set scanning mac OUI; result = %d", result
);
566 virtual int handleResponse(WifiEvent
& reply
) {
567 ALOGD("Request complete!");
568 /* Nothing to do on response! */
573 class SetNodfsCommand
: public WifiCommand
{
578 SetNodfsCommand(wifi_interface_handle handle
, u32 nodfs
)
579 : WifiCommand(handle
, 0) {
582 virtual int create() {
585 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS
);
587 ALOGE("Can't create message to send to driver - %d", ret
);
591 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
592 ret
= mMsg
.put_u32(ATTR_NODFS_VALUE
, mNoDfs
);
602 static int wifi_get_multicast_id(wifi_handle handle
, const char *name
, const char *group
)
604 GetMulticastIdCommand
cmd(handle
, name
, group
);
605 int res
= cmd
.requestResponse();
612 /////////////////////////////////////////////////////////////////////////
614 static bool is_wifi_interface(const char *name
)
616 if (strncmp(name
, "wlan", 4) != 0 && strncmp(name
, "p2p", 3) != 0) {
617 /* not a wifi interface; ignore it */
624 static int get_interface(const char *name
, interface_info
*info
)
626 strcpy(info
->name
, name
);
627 info
->id
= if_nametoindex(name
);
628 ALOGI("found an interface : %s, id = %d", name
, info
->id
);
632 wifi_error
wifi_init_interfaces(wifi_handle handle
)
634 hal_info
*info
= (hal_info
*)handle
;
635 ALOGD("wifi_init_interfaces, info = %p", info
);
639 DIR *d
= opendir("/sys/class/net");
641 return WIFI_ERROR_UNKNOWN
;
644 while ((de
= readdir(d
))) {
645 if (de
->d_name
[0] == '.')
647 if (is_wifi_interface(de
->d_name
) ) {
654 d
= opendir("/sys/class/net");
656 return WIFI_ERROR_UNKNOWN
;
658 info
->interfaces
= (interface_info
**)malloc(sizeof(interface_info
*) * n
);
661 while ((de
= readdir(d
))) {
662 if (de
->d_name
[0] == '.')
664 if (is_wifi_interface(de
->d_name
)) {
665 interface_info
*ifinfo
= (interface_info
*)malloc(sizeof(interface_info
));
666 if (get_interface(de
->d_name
, ifinfo
) != WIFI_SUCCESS
) {
670 ifinfo
->handle
= handle
;
671 info
->interfaces
[i
] = ifinfo
;
678 info
->num_interfaces
= n
;
682 wifi_error
wifi_get_ifaces(wifi_handle handle
, int *num
, wifi_interface_handle
**interfaces
)
684 hal_info
*info
= (hal_info
*)handle
;
686 *interfaces
= (wifi_interface_handle
*)info
->interfaces
;
687 *num
= info
->num_interfaces
;
692 wifi_error
wifi_get_iface_name(wifi_interface_handle handle
, char *name
, size_t size
)
694 interface_info
*info
= (interface_info
*)handle
;
695 strcpy(name
, info
->name
);
699 wifi_error
wifi_get_supported_feature_set(wifi_interface_handle handle
, feature_set
*set
)
701 return WIFI_ERROR_NOT_SUPPORTED
;
704 wifi_error
wifi_get_concurrency_matrix(wifi_interface_handle handle
, int set_size_max
,
705 feature_set set
[], int *set_size
)
707 return WIFI_ERROR_NOT_SUPPORTED
;
710 wifi_error
wifi_set_scanning_mac_oui(wifi_interface_handle handle
, oui scan_oui
)
712 SetPnoMacAddrOuiCommand
command(handle
, scan_oui
);
713 return (wifi_error
)command
.start();
717 wifi_error
wifi_set_nodfs_flag(wifi_interface_handle handle
, u32 nodfs
)
719 SetNodfsCommand
command(handle
, nodfs
);
720 return (wifi_error
) command
.requestResponse();
723 /////////////////////////////////////////////////////////////////////////////