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 wifi_error
wifi_initialize(wifi_handle
*handle
)
100 ALOGI("Initializing wifi");
101 hal_info
*info
= (hal_info
*)malloc(sizeof(hal_info
));
103 ALOGE("Could not allocate hal_info");
104 return WIFI_ERROR_UNKNOWN
;
107 memset(info
, 0, sizeof(*info
));
109 ALOGI("Creating socket");
110 struct nl_sock
*cmd_sock
= wifi_create_nl_socket(WIFI_HAL_CMD_SOCK_PORT
);
111 if (cmd_sock
== NULL
) {
112 ALOGE("Could not create handle");
113 return WIFI_ERROR_UNKNOWN
;
116 struct nl_sock
*event_sock
= wifi_create_nl_socket(WIFI_HAL_EVENT_SOCK_PORT
);
117 if (event_sock
== NULL
) {
118 ALOGE("Could not create handle");
119 nl_socket_free(cmd_sock
);
120 return WIFI_ERROR_UNKNOWN
;
123 struct nl_cb
*cb
= nl_socket_get_cb(event_sock
);
125 ALOGE("Could not create handle");
126 return WIFI_ERROR_UNKNOWN
;
129 // ALOGI("cb->refcnt = %d", cb->cb_refcnt);
130 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, internal_no_seq_check
, info
);
131 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, internal_valid_message_handler
, info
);
134 info
->cmd_sock
= cmd_sock
;
135 info
->event_sock
= event_sock
;
136 info
->clean_up
= false;
137 info
->in_event_loop
= false;
139 info
->event_cb
= (cb_info
*)malloc(sizeof(cb_info
) * DEFAULT_EVENT_CB_SIZE
);
140 info
->alloc_event_cb
= DEFAULT_EVENT_CB_SIZE
;
141 info
->num_event_cb
= 0;
143 info
->cmd
= (cmd_info
*)malloc(sizeof(cmd_info
) * DEFAULT_CMD_SIZE
);
144 info
->alloc_cmd
= DEFAULT_CMD_SIZE
;
147 info
->nl80211_family_id
= genl_ctrl_resolve(cmd_sock
, "nl80211");
148 if (info
->nl80211_family_id
< 0) {
149 ALOGE("Could not resolve nl80211 familty id");
150 nl_socket_free(cmd_sock
);
151 nl_socket_free(event_sock
);
153 return WIFI_ERROR_UNKNOWN
;
156 pthread_mutex_init(&info
->cb_lock
, NULL
);
158 *handle
= (wifi_handle
) info
;
159 ALOGD("wifi_initialize, handle = %p\n", handle
);
160 ALOGD("wifi_initialize, *handle = %p\n", *handle
);
161 ALOGD("wifi_initialize, info = %p\n", info
);
162 ALOGD("wifi_initialize, *info = %pn", *info
);
163 wifi_add_membership(*handle
, "scan");
164 wifi_add_membership(*handle
, "mlme");
165 wifi_add_membership(*handle
, "regulatory");
166 wifi_add_membership(*handle
, "vendor");
168 wifi_init_interfaces(*handle
);
169 ALOGD("Found %d interfaces", info
->num_interfaces
);
172 ALOGI("Initialized Wifi HAL Successfully; vendor cmd = %d", NL80211_CMD_VENDOR
);
176 static int wifi_add_membership(wifi_handle handle
, const char *group
)
178 hal_info
*info
= getHalInfo(handle
);
180 int id
= wifi_get_multicast_id(handle
, "nl80211", group
);
182 ALOGE("Could not find group %s", group
);
186 int ret
= nl_socket_add_membership(info
->event_sock
, id
);
188 ALOGE("Could not add membership to group %s", group
);
191 ALOGI("Successfully added membership for group %s", group
);
195 static void internal_cleaned_up_handler(wifi_handle handle
)
197 hal_info
*info
= getHalInfo(handle
);
198 wifi_cleaned_up_handler cleaned_up_handler
= info
->cleaned_up_handler
;
200 if (info
->cmd_sock
!= 0) {
201 nl_socket_free(info
->cmd_sock
);
202 nl_socket_free(info
->event_sock
);
203 info
->cmd_sock
= NULL
;
204 info
->event_sock
= NULL
;
207 (*cleaned_up_handler
)(handle
);
208 pthread_mutex_destroy(&info
->cb_lock
);
211 ALOGI("Internal cleanup completed");
214 void wifi_cleanup(wifi_handle handle
, wifi_cleaned_up_handler handler
)
216 hal_info
*info
= getHalInfo(handle
);
217 info
->cleaned_up_handler
= handler
;
218 info
->clean_up
= true;
220 ALOGI("Wifi cleanup completed");
223 static int internal_pollin_handler(wifi_handle handle
)
225 hal_info
*info
= getHalInfo(handle
);
226 ALOGI("even_loop info = %p", info
);
227 struct nl_cb
*cb
= nl_socket_get_cb(info
->event_sock
);
228 int res
= nl_recvmsgs(info
->event_sock
, cb
);
229 ALOGD("nl_recvmsgs returned %d", res
);
234 /* Run event handler */
235 void wifi_event_loop(wifi_handle handle
)
237 hal_info
*info
= getHalInfo(handle
);
238 ALOGI("even_loop info = %p", info
);
239 ALOGI("even_loop info = %p", handle
);
240 if (info
->in_event_loop
) {
243 info
->in_event_loop
= true;
247 memset(&pfd
, 0, sizeof(pfd
));
249 pfd
.fd
= nl_socket_get_fd(info
->event_sock
);
252 /* TODO: Add support for timeouts */
255 int timeout
= -1; /* Infinite timeout */
257 ALOGI("Polling socket");
258 int result
= poll(&pfd
, 1, -1);
260 ALOGE("Error polling socket");
261 } else if (pfd
.revents
& POLLERR
) {
262 ALOGE("POLL Error; error no = %d", errno
);
264 int result2
= read(pfd
.fd
, buf
, sizeof(buf
));
265 ALOGE("Read after POLL returned %d, error no = %d", result2
, errno
);
266 } else if (pfd
.revents
& POLLHUP
) {
267 ALOGE("Remote side hung up");
269 } else if (pfd
.revents
& POLLIN
) {
270 ALOGI("Found some events!!!");
271 internal_pollin_handler(handle
);
273 ALOGE("Unknown event - %0x", pfd
.revents
);
275 } while (!info
->clean_up
);
278 ALOGI("Cleaning up");
279 internal_cleaned_up_handler(handle
);
282 ///////////////////////////////////////////////////////////////////////////////////////
284 static int internal_no_seq_check(struct nl_msg
*msg
, void *arg
)
289 static int internal_valid_message_handler(nl_msg
*msg
, void *arg
)
291 wifi_handle handle
= (wifi_handle
)arg
;
292 hal_info
*info
= getHalInfo(handle
);
293 ALOGI("even_loop info = %p", handle
);
294 ALOGD("internal_valid_message_handler, info = %p", info
);
296 WifiEvent
event(msg
);
297 int res
= event
.parse();
299 ALOGE("Failed to parse event: %d", res
);
303 int cmd
= event
.get_cmd();
304 uint32_t vendor_id
= 0;
307 if (cmd
== NL80211_CMD_VENDOR
) {
308 vendor_id
= event
.get_u32(NL80211_ATTR_VENDOR_ID
);
309 subcmd
= event
.get_u32(NL80211_ATTR_VENDOR_SUBCMD
);
310 ALOGI("event received %s, vendor_id = 0x%0x, subcmd = 0x%0x",
311 event
.get_cmdString(), vendor_id
, subcmd
);
313 ALOGI("event received %s", event
.get_cmdString());
316 //ALOGI("event received %s, vendor_id = 0x%0x", event.get_cmdString(), vendor_id);
319 bool dispatched
= false;
321 pthread_mutex_lock(&info
->cb_lock
);
323 ALOGI("Number of events %d", info
->num_event_cb
);
325 for (int i
= 0; i
< info
->num_event_cb
; i
++) {
326 if (cmd
== info
->event_cb
[i
].nl_cmd
) {
327 if (cmd
== NL80211_CMD_VENDOR
328 && ((vendor_id
!= info
->event_cb
[i
].vendor_id
)
329 || (subcmd
!= info
->event_cb
[i
].vendor_subcmd
)))
331 /* event for a different vendor, ignore it */
335 cb_info
*cbi
= &(info
->event_cb
[i
]);
336 nl_recvmsg_msg_cb_t cb_func
= cbi
->cb_func
;
337 void *cb_arg
= cbi
->cb_arg
;
338 WifiCommand
*cmd
= (WifiCommand
*)cbi
->cb_arg
;
343 pthread_mutex_unlock(&info
->cb_lock
);
345 (*cb_func
)(msg
, cb_arg
);
354 pthread_mutex_unlock(&info
->cb_lock
);
358 ///////////////////////////////////////////////////////////////////////////////////////
360 class GetMulticastIdCommand
: public WifiCommand
367 GetMulticastIdCommand(wifi_handle handle
, const char *name
, const char *group
)
368 : WifiCommand(handle
, 0)
379 virtual int create() {
380 int nlctrlFamily
= genl_ctrl_resolve(mInfo
->cmd_sock
, "nlctrl");
381 ALOGI("ctrl family = %d", nlctrlFamily
);
382 int ret
= mMsg
.create(nlctrlFamily
, CTRL_CMD_GETFAMILY
, 0, 0);
386 ret
= mMsg
.put_string(CTRL_ATTR_FAMILY_NAME
, mName
);
390 virtual int handleResponse(WifiEvent
& reply
) {
392 ALOGE("handling reponse in %s", __func__
);
394 struct nlattr
**tb
= reply
.attributes();
395 struct genlmsghdr
*gnlh
= reply
.header();
396 struct nlattr
*mcgrp
= NULL
;
399 if (!tb
[CTRL_ATTR_MCAST_GROUPS
]) {
400 ALOGE("No multicast groups found");
403 ALOGE("Multicast groups attr size = %d", nla_len(tb
[CTRL_ATTR_MCAST_GROUPS
]));
406 for_each_attr(mcgrp
, tb
[CTRL_ATTR_MCAST_GROUPS
], i
) {
408 ALOGE("Processing group");
409 struct nlattr
*tb2
[CTRL_ATTR_MCAST_GRP_MAX
+ 1];
410 nla_parse(tb2
, CTRL_ATTR_MCAST_GRP_MAX
, (nlattr
*)nla_data(mcgrp
),
411 nla_len(mcgrp
), NULL
);
412 if (!tb2
[CTRL_ATTR_MCAST_GRP_NAME
] || !tb2
[CTRL_ATTR_MCAST_GRP_ID
]) {
416 char *grpName
= (char *)nla_data(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
417 int grpNameLen
= nla_len(tb2
[CTRL_ATTR_MCAST_GRP_NAME
]);
419 ALOGE("Found group name %s", grpName
);
421 if (strncmp(grpName
, mGroup
, grpNameLen
) != 0)
424 mId
= nla_get_u32(tb2
[CTRL_ATTR_MCAST_GRP_ID
]);
433 class SetPnoMacAddrOuiCommand
: public WifiCommand
{
438 feature_set
*feature_matrix
;
442 SetPnoMacAddrOuiCommand(wifi_interface_handle handle
, oui scan_oui
)
443 : WifiCommand(handle
, 0)
448 int createRequest(WifiRequest
& request
, int subcmd
, byte
*scan_oui
) {
449 int result
= request
.create(GOOGLE_OUI
, subcmd
);
454 nlattr
*data
= request
.attr_start(NL80211_ATTR_VENDOR_DATA
);
455 result
= request
.put(ANDR_WIFI_ATTRIBUTE_PNO_RANDOM_MAC_OUI
, scan_oui
, DOT11_OUI_LEN
);
460 request
.attr_end(data
);
466 ALOGD("Sending mac address OUI");
467 WifiRequest
request(familyId(), ifaceId());
468 int result
= createRequest(request
, SLSI_NL80211_VENDOR_SUBCMD_SET_GSCAN_OUI
, mOui
);
469 if (result
!= WIFI_SUCCESS
) {
470 ALOGE("failed to create request; result = %d", result
);
474 result
= requestResponse(request
);
475 if (result
!= WIFI_SUCCESS
) {
476 ALOGE("failed to set scanning mac OUI; result = %d", result
);
482 virtual int handleResponse(WifiEvent
& reply
) {
483 ALOGD("Request complete!");
484 /* Nothing to do on response! */
489 class SetNodfsCommand
: public WifiCommand
{
494 SetNodfsCommand(wifi_interface_handle handle
, u32 nodfs
)
495 : WifiCommand(handle
, 0) {
498 virtual int create() {
501 ret
= mMsg
.create(GOOGLE_OUI
, SLSI_NL80211_VENDOR_SUBCMD_SET_NODFS
);
503 ALOGE("Can't create message to send to driver - %d", ret
);
507 nlattr
*data
= mMsg
.attr_start(NL80211_ATTR_VENDOR_DATA
);
508 ret
= mMsg
.put_u32(ATTR_NODFS_VALUE
, mNoDfs
);
518 static int wifi_get_multicast_id(wifi_handle handle
, const char *name
, const char *group
)
520 GetMulticastIdCommand
cmd(handle
, name
, group
);
521 int res
= cmd
.requestResponse();
528 /////////////////////////////////////////////////////////////////////////
530 static bool is_wifi_interface(const char *name
)
532 if (strncmp(name
, "wlan", 4) != 0 && strncmp(name
, "p2p", 3) != 0) {
533 /* not a wifi interface; ignore it */
540 static int get_interface(const char *name
, interface_info
*info
)
542 strcpy(info
->name
, name
);
543 info
->id
= if_nametoindex(name
);
544 ALOGI("found an interface : %s, id = %d", name
, info
->id
);
548 wifi_error
wifi_init_interfaces(wifi_handle handle
)
550 hal_info
*info
= (hal_info
*)handle
;
551 ALOGD("wifi_init_interfaces, info = %p", info
);
555 DIR *d
= opendir("/sys/class/net");
557 return WIFI_ERROR_UNKNOWN
;
560 while ((de
= readdir(d
))) {
561 if (de
->d_name
[0] == '.')
563 if (is_wifi_interface(de
->d_name
) ) {
570 d
= opendir("/sys/class/net");
572 return WIFI_ERROR_UNKNOWN
;
574 info
->interfaces
= (interface_info
**)malloc(sizeof(interface_info
*) * n
);
577 while ((de
= readdir(d
))) {
578 if (de
->d_name
[0] == '.')
580 if (is_wifi_interface(de
->d_name
)) {
581 interface_info
*ifinfo
= (interface_info
*)malloc(sizeof(interface_info
));
582 if (get_interface(de
->d_name
, ifinfo
) != WIFI_SUCCESS
) {
586 ifinfo
->handle
= handle
;
587 info
->interfaces
[i
] = ifinfo
;
594 info
->num_interfaces
= n
;
598 wifi_error
wifi_get_ifaces(wifi_handle handle
, int *num
, wifi_interface_handle
**interfaces
)
600 hal_info
*info
= (hal_info
*)handle
;
602 *interfaces
= (wifi_interface_handle
*)info
->interfaces
;
603 *num
= info
->num_interfaces
;
608 wifi_error
wifi_get_iface_name(wifi_interface_handle handle
, char *name
, size_t size
)
610 interface_info
*info
= (interface_info
*)handle
;
611 strcpy(name
, info
->name
);
615 wifi_error
wifi_get_supported_feature_set(wifi_interface_handle handle
, feature_set
*set
)
617 return WIFI_ERROR_NOT_SUPPORTED
;
620 wifi_error
wifi_get_concurrency_matrix(wifi_interface_handle handle
, int set_size_max
,
621 feature_set set
[], int *set_size
)
623 return WIFI_ERROR_NOT_SUPPORTED
;
626 wifi_error
wifi_set_scanning_mac_oui(wifi_interface_handle handle
, oui scan_oui
)
628 SetPnoMacAddrOuiCommand
command(handle
, scan_oui
);
629 return (wifi_error
)command
.start();
633 wifi_error
wifi_set_nodfs_flag(wifi_interface_handle handle
, u32 nodfs
)
635 SetNodfsCommand
command(handle
, nodfs
);
636 return (wifi_error
) command
.requestResponse();
639 /////////////////////////////////////////////////////////////////////////////