2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "netsupplicant.h"
25 #include "wifi-config.h"
26 #include "wifi-netlink-scan.h"
27 #include <netlink/genl/genl.h>
28 #include <netlink/genl/family.h>
29 #include <netlink/genl/ctrl.h>
30 #include <netlink/msg.h>
31 #include <netlink/attr.h>
32 #include <netlink/netlink.h>
35 static GSList *bss_info_list = NULL;
36 static unsigned char samsung_oui[3] = {0x00, 0x16, 0x32};
38 void __netconfig_notify_netlink_scan_done(void)
40 GVariantBuilder *builder = NULL;
42 const char *prop_ssid = "ssid";
43 const char *prop_bssid = "bssid";
44 const char *prop_freq = "freq";
45 const char *prop_rssi = "rssi";
46 const char *prop_vsie = "vsie";
48 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
49 for (list = bss_info_list; list != NULL; list = list->next) {
50 struct bss_scan_info_t *bss_info = (struct bss_scan_info_t *)list->data;
53 char *bssid = (char *)bss_info->bssid;
54 char *ssid = (char *)bss_info->ssid;
55 char *vsie = (char *)bss_info->vsie;
56 int freq = (int)bss_info->freq;
57 int signal = (int)bss_info->signal;
59 g_variant_builder_add(builder, "{sv}", prop_ssid, g_variant_new_string(ssid));
60 g_variant_builder_add(builder, "{sv}", prop_bssid, g_variant_new_string(bssid));
61 g_variant_builder_add(builder, "{sv}", prop_freq, g_variant_new_int32(freq));
62 g_variant_builder_add(builder, "{sv}", prop_rssi, g_variant_new_int32(signal));
63 g_variant_builder_add(builder, "{sv}", prop_vsie, g_variant_new_string(vsie));
67 wifi_emit_netlink_scan_completed((Wifi *)get_wifi_object(), g_variant_builder_end(builder));
68 g_variant_builder_unref(builder);
70 if (bss_info_list != NULL)
71 g_slist_free_full(bss_info_list, g_free);
74 INFO("NetlinkScanCompleted");
79 static int ack_handler(struct nl_msg *msg, void *user_data)
86 static int finish_handler(struct nl_msg *msg, void *user_data)
93 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
101 static int no_seq_check(struct nl_msg *msg, void *user_data)
107 static int __netconfig_family_handler(struct nl_msg *msg, void *user_data)
109 /** Callback for NL_CB_VALID in multicast group */
110 struct netconfig_netlink_scan_handler_args *grp = user_data;
111 struct nlattr *tb[CTRL_ATTR_MAX + 1];
112 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
113 struct nlattr *mc_grp;
116 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
118 if (!tb[CTRL_ATTR_MCAST_GROUPS])
121 nla_for_each_nested(mc_grp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mc_grp) {
122 struct nlattr *tb_mc_grp[CTRL_ATTR_MCAST_GRP_MAX + 1];
124 nla_parse(tb_mc_grp, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mc_grp), nla_len(mc_grp), NULL);
126 if (!tb_mc_grp[CTRL_ATTR_MCAST_GRP_NAME] || !tb_mc_grp[CTRL_ATTR_MCAST_GRP_ID])
128 if (strncmp(nla_data(tb_mc_grp[CTRL_ATTR_MCAST_GRP_NAME]), grp->group,
129 nla_len(tb_mc_grp[CTRL_ATTR_MCAST_GRP_NAME])))
132 grp->id = nla_get_u32(tb_mc_grp[CTRL_ATTR_MCAST_GRP_ID]);
139 static int __netconfig_get_multicast_id(struct nl_sock *socket, const char *family, const char *group)
141 struct nl_msg *msg = NULL;
142 struct nl_cb *cb = NULL;
144 struct netconfig_netlink_scan_handler_args grp = { .group = group, .id = -ENOENT, };
150 cb = nl_cb_alloc(NL_CB_DEFAULT);
156 ctrl_id = genl_ctrl_resolve(socket, "nlctrl");
158 genlmsg_put(msg, 0, 0, ctrl_id, 0, 0, CTRL_CMD_GETFAMILY, 0);
161 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
163 ret = nl_send_auto_complete(socket, msg);
169 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &ret);
170 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &ret);
171 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __netconfig_family_handler, &grp);
174 nl_recvmsgs(socket, cb);
187 static void __netconfig_macaddress_str(char *bssid, unsigned char *user_data)
191 for (i = 0; i < 6; i++) {
193 snprintf(bssid, 3, "%02x", user_data[i]);
196 snprintf(bssid, 4, ":%02x", user_data[i]);
202 static void __netconfig_get_vsie(unsigned char *bss_element, int length, char **dst)
206 gboolean vsie_found = FALSE;
209 DBG("Vendor specific data not available");
213 /** Check for vendor specific information element */
214 for (i = 0; i < length; i++) {
215 if (bss_element[i] == 221) {
216 len = bss_element[i+1];
222 if (vsie_found && memcmp(bss_element+i+2, samsung_oui, 3) == 0) {
223 DBG("Vendor Specific IE found, len: %d", len);
224 *dst = g_try_malloc0(2*(len+2) + 1);
226 DBG("Failed to allocate memory");
232 for (j = i; j <= (i + len + 1); j++) {
233 snprintf(buf, 3, "%02x", bss_element[j]);
241 static void __netconfig_found_ap(unsigned char *bss_element, int length, char *str)
247 while (length >= 2 && length >= bss_element[1]) {
248 if (bss_element[0] == 0 && bss_element[1] <= 32) {
249 len = bss_element[1];
250 data = bss_element + 2;
251 for (i = 0; i < len; i++) {
252 if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
253 snprintf(&str[i], 2, "%c", data[i]);
254 else if (data[i] == ' ' && (i != 0 && i != len -1))
255 snprintf(&str[i], 2, "%c", ' ');
257 snprintf(&str[i], 3, "%.2x", data[i]);
261 length -= bss_element[1] + 2;
262 bss_element += bss_element[1] + 2;
266 static int __netconfig_netlink_scan_cb(struct nl_msg *msg, void *user_data)
268 /** Called by the kernel with a dump of the successful scan's data. Called for each SSID. */
269 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
270 char bssid[NETCONFIG_BSSID_LEN+1];
271 char ssid[NETCONFIG_SSID_LEN+1] = {0, };
273 struct nlattr *tb[NL80211_ATTR_MAX + 1];
274 struct nlattr *bss[NL80211_BSS_MAX + 1];
275 struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
276 [NL80211_BSS_FREQUENCY] = {.type = NLA_U32},
277 [NL80211_BSS_BSSID] = { },
278 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
279 [NL80211_BSS_SIGNAL_MBM] = {.type = NLA_U32},
282 /** Parse nl message and check error. */
283 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
284 if (!tb[NL80211_ATTR_BSS]) {
285 DBG("BSS info not available");
288 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy)) {
289 DBG("Failed to parse nested attributes");
292 if ((!bss[NL80211_BSS_BSSID]) || (!bss[NL80211_BSS_INFORMATION_ELEMENTS]))
295 /** Extract BSSID and AP info. */
296 __netconfig_macaddress_str(bssid, nla_data(bss[NL80211_BSS_BSSID]));
297 __netconfig_found_ap(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), ssid);
298 __netconfig_get_vsie(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), &vsie);
300 /** Create AP info list. */
301 if (ssid[0] != '\0') {
302 struct bss_scan_info_t *bss_info;
305 bss_info = g_try_new0(struct bss_scan_info_t, 1);
306 if (bss_info == NULL)
309 g_strlcpy(bss_info->bssid, bssid, strlen(bssid)+1);
310 g_strlcpy(bss_info->ssid, ssid, strlen(ssid)+1);
312 g_strlcpy(bss_info->vsie, vsie, strlen(vsie)+1);
315 bss_info->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
317 if (bss[NL80211_BSS_SIGNAL_MBM]) {
318 signal = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
319 signal /= 100; /** mBm to dBm */
320 bss_info->signal = signal;
322 DBG("%s %d %d %s [vsie: %s]", bss_info->bssid, bss_info->freq, bss_info->signal, bss_info->ssid, bss_info->vsie);
324 if (bss_info->ssid[0] == '\0')
327 bss_info_list = g_slist_append(bss_info_list, bss_info);
334 static int __netconfig_netlink_scan_reply(struct nl_msg *msg, void *user_data)
336 /** Called by the kernel when the scan is done or has been aborted. */
337 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
338 struct netconfig_netlink_scan_results *results = user_data;
340 if (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS) {
341 DBG("Received NL80211_CMD_NEW_SCAN_RESULTS in reply");
343 results->aborted = 0;
344 } else if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED) {
345 DBG("Received NL80211_CMD_SCAN_ABORTED in reply");
347 results->aborted = 1;
353 static int __netconfig_request_netlink_scan(struct nl_sock *socket,
354 int if_index, int id, GVariant *params)
356 struct netconfig_netlink_scan_results results = { .done = 0, .aborted = 0 };
357 struct nl_msg *msg = NULL;
358 struct nl_cb *cb = NULL;
359 struct nl_msg *ssids = NULL;
362 unsigned char ies[NETCONFIG_MAX_VSIE_LEN+1] = {0x00, };
367 gboolean ssid_found = FALSE;
368 int mcid = __netconfig_get_multicast_id(socket, "nl80211", "scan");
369 nl_socket_add_membership(socket, mcid);
373 DBG("Failed to allocate msg");
376 ssids = nlmsg_alloc();
378 DBG("Failed to allocate ssids");
382 cb = nl_cb_alloc(NL_CB_DEFAULT);
384 DBG("Failed to allocate callbacks");
390 /** Set nl message and callback functions. */
391 genlmsg_put(msg, 0, 0, id, 0, 0, NL80211_CMD_TRIGGER_SCAN, 0);
392 nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index);
394 g_variant_get(params, "a{sv}", &iter);
395 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
396 if (g_strcmp0(key, "SSID") == 0) {
397 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
398 char *ssid = g_strdup(g_variant_get_string(value, NULL));
400 DBG("ssid [%s]", ssid);
402 nla_put(ssids, 1, strlen(ssid), ssid);
405 } else if (g_strcmp0(key, "VSIE") == 0) {
406 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
407 char *vsie = g_strdup(g_variant_get_string(value, NULL));
408 int vsie_len = strlen(vsie);
409 DBG("vsie: %s vsie_len: %d", vsie, vsie_len);
411 ies_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
412 __netconfig_hex_str_to_bin(vsie, ies, ies_len);
417 g_variant_iter_free(iter);
420 nla_put(ssids, 1, 0, "");
421 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
424 if (ies[0] == NETCONFIG_VENDOR_SPECIFIC_ID && ies[1] >= 4) {
425 DBG("ies_len: %d ies: %02x %02x %02x %02x %02x %02x %02x", ies_len,
426 ies[0], ies[1], ies[2], ies[3], ies[4], ies[5], ies[6]);
427 nla_put(msg, NL80211_ATTR_IE, ies_len, ies);
431 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __netconfig_netlink_scan_reply, &results);
432 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
433 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
434 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
435 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
437 /** Send NL80211_CMD_TRIGGER_SCAN to start the scan. */
438 ret = nl_send_auto_complete(socket, msg);
439 DBG("Sent %d bytes to the kernel", ret);
443 ret = nl_recvmsgs(socket, cb);
446 DBG("nl_recvmsgs() ret: %d (%s)", ret, nl_geterror(-ret));
450 while (!results.done)
451 nl_recvmsgs(socket, cb);
453 if (results.aborted) {
459 /** Release memory */
462 nl_socket_drop_membership(socket, mcid);
466 static int __netconfig_initialize_nl80211(netconfig_nl_global *global)
470 global->if_index = __netconfig_get_interface_index(WIFI_IFNAME);
471 if (global->if_index < 0) {
472 DBG("Failed to get interface index");
476 global->socket = nl_socket_alloc();
477 if (!global->socket) {
478 DBG("Failed to allocate netlink socket.");
482 if (genl_connect(global->socket)) {
483 DBG("Failed to connect to generic netlink.");
488 global->id = genl_ctrl_resolve(global->socket, "nl80211");
489 if (global->id < 0) {
490 DBG("Failed to find the nl80211 driver");
498 nl_socket_free(global->socket);
502 static int __netconfig_initialize_nl_msg(netconfig_nl_global *global)
504 if (global == NULL) {
505 DBG("Invalid parameter.");
509 global->msg = nlmsg_alloc();
510 if (global->msg == NULL) {
511 DBG("Failed to allocate netlink message");
515 /* Set command into message */
516 genlmsg_put(global->msg, 0, 0, global->id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0);
517 nla_put_u32(global->msg, NL80211_ATTR_IFINDEX, global->if_index);
518 nl_socket_modify_cb(global->socket, NL_CB_VALID, NL_CB_CUSTOM, __netconfig_netlink_scan_cb, NULL);
523 int handle_netlink_scan(Wifi *wifi, GDBusMethodInvocation *context, GVariant *params)
526 netconfig_nl_global global = {
533 /** Initialize netlink socket */
534 int ret = __netconfig_initialize_nl80211(&global);
536 DBG("__netconfig_initialize_nl80211() failed, error %d", ret);
537 wifi_complete_netlink_scan(wifi, context);
541 /** Request NL80211_CMD_TRIGGER_SCAN to the kernel. */
542 ret = __netconfig_request_netlink_scan(global.socket, global.if_index, global.id, params);
544 DBG("__netconfig_request_netlink_scan() failed, error %d", ret);
545 wifi_complete_netlink_scan(wifi, context);
549 ret = __netconfig_initialize_nl_msg(&global);
551 DBG("__netconfig_initialize_nl_msg() failed, error %d", ret);
552 wifi_complete_netlink_scan(wifi, context);
556 ret = nl_send_auto_complete(global.socket, global.msg);
557 DBG("NL80211_CMD_GET_SCAN sent %d bytes to the kernel", ret);
559 /** Receive the kernel message. */
560 ret = nl_recvmsgs_default(global.socket);
561 nlmsg_free(global.msg);
563 DBG("nl_recvmsgs_default() failed. ret: %d (error: %s)", ret, nl_geterror(-ret));
564 wifi_complete_netlink_scan(wifi, context);
568 wifi_complete_netlink_scan(wifi, context);
569 __netconfig_notify_netlink_scan_done();