probe-req.c (7736B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2020 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <sys/types.h> 31 #include <sys/socket.h> 32 #include <sys/ioctl.h> 33 #include <net/if.h> 34 #include <netinet/in.h> 35 #include <unistd.h> 36 37 #include <ell/ell.h> 38 39 #include "linux/nl80211.h" 40 41 #include "src/mpdu.h" 42 #include "src/nl80211util.h" 43 44 static struct l_genl *genl; 45 static struct l_genl_family *nl80211; 46 static int exit_status; 47 static uint64_t wdev_id; 48 static uint8_t wdev_addr[6]; 49 static uint32_t freq; 50 51 static const uint8_t probe_req_body[] = { 52 /* SSID */ 53 0x00, 0x07, 'D', 'I', 'R', 'E', 'C', 'T', '-', 54 /* Supported Rates */ 55 0x01, 0x08, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c, 56 /* DS Parameter Set */ 57 0x03, 0x01, 0x00, 58 /* HT Capabilities */ 59 0x2d, 0x1a, 0xef, 0x11, 0x17, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 60 0x00, 0x00, 0x00, 0x2c, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 61 0x00, 0x00, 0x00, 0x00, 62 /* WPS */ 63 0xdd, 0x6c, 0x00, 0x50, 0xf2, 0x04, 64 /* > Version */ 65 0x10, 0x4a, 0x00, 0x01, 0x10, 66 /* > Request Type */ 67 0x10, 0x3a, 0x00, 0x01, 0x00, 68 /* > Config Methods */ 69 0x10, 0x08, 0x00, 0x02, 0x13, 0x80, 70 /* > UUID E */ 71 0x10, 0x47, 0x00, 0x10, 0x46, 0x92, 0x49, 0x6f, 0xce, 0x1e, 0x5f, 0xd1, 72 0xa5, 0x45, 0x9b, 0x1c, 0xa5, 0xde, 0xb9, 0x41, 73 /* > Primary Device Type */ 74 0x10, 0x54, 0x00, 0x08, 0x00, 0x01, 0x00, 0x50, 0xf2, 0x04, 0x00, 0x01, 75 /* > RF Bands */ 76 0x10, 0x3c, 0x00, 0x01, 0x01, 77 /* > Association State */ 78 0x10, 0x02, 0x00, 0x02, 0x00, 0x00, 79 /* > Configuration Error */ 80 0x10, 0x09, 0x00, 0x02, 0x00, 0x00, 81 /* > Device Password ID */ 82 0x10, 0x12, 0x00, 0x02, 0x00, 0x00, 83 /* > Manufacturer */ 84 0x10, 0x21, 0x00, 0x01, 0x20, 85 /* > Model Name */ 86 0x10, 0x23, 0x00, 0x01, 0x20, 87 /* > Model Numbers */ 88 0x10, 0x24, 0x00, 0x01, 0x20, 89 /* > Device Name */ 90 0x10, 0x11, 0x00, 0x04, 't', 'e', 's', 't', 91 /* > Vendor Extension > Version2 */ 92 0x10, 0x49, 0x00, 0x06, 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20, 93 /* P2P */ 94 0xdd, 0x11, 0x50, 0x6f, 0x9a, 0x09, 95 /* > P2P Capability */ 96 0x02, 0x02, 0x00, 0x04, 0x00, 97 /* > Listen Channel */ 98 0x06, 0x05, 0x00, 'X', 'X', 0x04, 0x51, 0x01, 99 }; 100 101 static void frame_cb(struct l_genl_msg *msg, void *user_data) 102 { 103 int err = l_genl_msg_get_error(msg); 104 105 if (err < 0) { 106 l_error("CMD_FRAME failed: %s (%i)", strerror(-err), -err); 107 exit_status = EXIT_FAILURE; 108 } else { 109 l_info("Frame queued"); 110 exit_status = EXIT_SUCCESS; 111 } 112 113 l_main_quit(); 114 } 115 116 static void get_interface_callback(struct l_genl_msg *msg, void *user_data) 117 { 118 uint32_t ifindex; 119 uint32_t iftype; 120 const char *ifname; 121 const uint8_t *ifaddr; 122 uint64_t cur_wdev_id; 123 struct ifreq ifr; 124 int sock; 125 int r; 126 127 /* 128 * For now hoose the first interface with iftype station, require it 129 * to be UP and have an ifindex. 130 */ 131 132 if (wdev_id) 133 return; 134 135 if (nl80211_parse_attrs(msg, NL80211_ATTR_IFINDEX, &ifindex, 136 NL80211_ATTR_WDEV, &cur_wdev_id, 137 NL80211_ATTR_IFTYPE, &iftype, 138 NL80211_ATTR_IFNAME, &ifname, 139 NL80211_ATTR_MAC, &ifaddr, 140 NL80211_ATTR_UNSPEC) < 0) 141 return; 142 143 if (iftype != NL80211_IFTYPE_STATION) 144 return; 145 146 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); 147 if (sock == -1) 148 return; 149 150 memset(&ifr, 0, sizeof(ifr)); 151 l_strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); 152 r = ioctl(sock, SIOCGIFFLAGS, &ifr); 153 close(sock); 154 155 /* IFF_RUNNING not required */ 156 if (r == -1 || !(ifr.ifr_flags & IFF_UP)) 157 return; 158 159 l_info("Selected interface %s", ifname); 160 wdev_id = cur_wdev_id; 161 memcpy(wdev_addr, ifaddr, 6); 162 } 163 164 static void get_interface_done(void *user_data) 165 { 166 struct l_genl_msg *msg; 167 uint8_t frame_buf[256] __attribute__ ((aligned)); 168 struct mmpdu_header *hdr = (void *) frame_buf; 169 static const uint8_t bcast_addr[6] = 170 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 171 size_t frame_len; 172 173 if (!wdev_id) { 174 l_error("No suitable interface found"); 175 exit_status = EXIT_FAILURE; 176 l_main_quit(); 177 return; 178 } 179 180 memset(frame_buf, 0, sizeof(*hdr)); 181 hdr->fc.protocol_version = 0; 182 hdr->fc.type = MPDU_TYPE_MANAGEMENT; 183 hdr->fc.subtype = MPDU_MANAGEMENT_SUBTYPE_PROBE_REQUEST; 184 memcpy(hdr->address_1, bcast_addr, 6); /* DA */ 185 memcpy(hdr->address_2, wdev_addr, 6); /* SA */ 186 memcpy(hdr->address_3, bcast_addr, 6); /* BSSID */ 187 frame_len = (uint8_t *) mmpdu_body(hdr) - (uint8_t *) hdr; 188 189 memcpy((void *) mmpdu_body(hdr), probe_req_body, sizeof(probe_req_body)); 190 frame_len += sizeof(probe_req_body); 191 192 msg = l_genl_msg_new_sized(NL80211_CMD_FRAME, 128 + frame_len); 193 l_genl_msg_append_attr(msg, NL80211_ATTR_WDEV, 8, &wdev_id); 194 195 if (freq) { 196 l_genl_msg_append_attr(msg, NL80211_ATTR_WIPHY_FREQ, 4, &freq); 197 l_genl_msg_append_attr(msg, NL80211_ATTR_OFFCHANNEL_TX_OK, 0, 198 NULL); 199 } 200 201 l_genl_msg_append_attr(msg, NL80211_ATTR_FRAME, frame_len, frame_buf); 202 l_genl_msg_append_attr(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK, 0, NULL); 203 l_genl_msg_append_attr(msg, NL80211_ATTR_TX_NO_CCK_RATE, 0, NULL); 204 205 if (!l_genl_family_send(nl80211, msg, frame_cb, user_data, NULL)) { 206 l_error("l_genl_family_send failed"); 207 exit_status = EXIT_FAILURE; 208 l_main_quit(); 209 return; 210 } 211 } 212 213 static void dump_interfaces(void) 214 { 215 struct l_genl_msg *msg; 216 217 msg = l_genl_msg_new(NL80211_CMD_GET_INTERFACE); 218 if (!l_genl_family_dump(nl80211, msg, get_interface_callback, 219 NULL, get_interface_done)) { 220 l_genl_msg_unref(msg); 221 l_error("Getting nl80211 interface information failed"); 222 exit_status = EXIT_FAILURE; 223 l_main_quit(); 224 return; 225 } 226 } 227 228 static void family_discovered(const struct l_genl_family_info *info, 229 void *user_data) 230 { 231 if (!strcmp(l_genl_family_info_get_name(info), NL80211_GENL_NAME)) 232 nl80211 = l_genl_family_new(genl, NL80211_GENL_NAME); 233 } 234 235 static void discovery_done(void *user_data) 236 { 237 if (!nl80211) { 238 l_error("nl80211 doesn't exist.\n" 239 "Load it manually using modprobe cfg80211"); 240 goto quit; 241 } 242 243 dump_interfaces(); 244 return; 245 246 quit: 247 exit_status = EXIT_FAILURE; 248 l_main_quit(); 249 } 250 251 int main(int argc, char *argv[]) 252 { 253 if (argc >= 2) { 254 char *endp; 255 256 if (!strcmp(argv[1], "-h")) { 257 fprintf(stderr, 258 "Usage: %s [<frequency>]\n\n" 259 "Send out a broadcast Probe Request frame. " 260 "A wireless interface must be UP. If a " 261 "frequency is not given, the frame is " 262 "transmitted on the current channel.\n", 263 argv[0]); 264 return EXIT_SUCCESS; 265 } 266 267 freq = strtol(argv[1], &endp, 0); 268 269 if (*endp != '\0') { 270 fprintf(stderr, "Can't parse '%s'\n", endp); 271 return EXIT_FAILURE; 272 } 273 } 274 275 if (!l_main_init()) 276 return EXIT_FAILURE; 277 278 l_log_set_stderr(); 279 exit_status = EXIT_FAILURE; 280 281 genl = l_genl_new(); 282 if (!genl) { 283 l_error("Failed to initialize generic netlink"); 284 goto done; 285 } 286 287 if (!l_genl_discover_families(genl, family_discovered, NULL, 288 discovery_done)) { 289 l_error("Unable to start family discovery"); 290 l_genl_unref(genl); 291 goto done; 292 } 293 294 l_main_run(); 295 296 l_genl_family_free(nl80211); 297 l_genl_unref(genl); 298 299 done: 300 l_main_exit(); 301 302 return exit_status; 303 }