Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * WPA Supplicant - driver interaction with BSD net80211 layer |
| 3 | * Copyright (c) 2004, Sam Leffler <sam@errno.com> |
| 4 | * Copyright (c) 2004, 2Wire, Inc |
| 5 | * |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 6 | * This software may be distributed under the terms of the BSD license. |
| 7 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "includes.h" |
| 11 | #include <sys/ioctl.h> |
| 12 | #include <sys/sysctl.h> |
| 13 | |
| 14 | #include "common.h" |
| 15 | #include "driver.h" |
| 16 | #include "eloop.h" |
| 17 | #include "common/ieee802_11_defs.h" |
| 18 | #include "common/wpa_common.h" |
| 19 | |
| 20 | #include <net/if.h> |
| 21 | #include <net/if_media.h> |
| 22 | |
| 23 | #ifdef __NetBSD__ |
| 24 | #include <net/if_ether.h> |
| 25 | #else |
| 26 | #include <net/ethernet.h> |
| 27 | #endif |
| 28 | #include <net/route.h> |
| 29 | |
| 30 | #ifdef __DragonFly__ |
| 31 | #include <netproto/802_11/ieee80211_ioctl.h> |
| 32 | #include <netproto/802_11/ieee80211_dragonfly.h> |
| 33 | #else /* __DragonFly__ */ |
| 34 | #ifdef __GLIBC__ |
| 35 | #include <netinet/ether.h> |
| 36 | #endif /* __GLIBC__ */ |
| 37 | #include <net80211/ieee80211.h> |
| 38 | #include <net80211/ieee80211_ioctl.h> |
| 39 | #include <net80211/ieee80211_crypto.h> |
| 40 | #endif /* __DragonFly__ || __GLIBC__ */ |
| 41 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 42 | #include <net80211/ieee80211_freebsd.h> |
| 43 | #endif |
| 44 | #if __NetBSD__ |
| 45 | #include <net80211/ieee80211_netbsd.h> |
| 46 | #endif |
| 47 | |
| 48 | #include "l2_packet/l2_packet.h" |
| 49 | |
| 50 | struct bsd_driver_data { |
| 51 | struct hostapd_data *hapd; /* back pointer */ |
| 52 | |
| 53 | int sock; /* open socket for 802.11 ioctls */ |
| 54 | struct l2_packet_data *sock_xmit;/* raw packet xmit socket */ |
| 55 | int route; /* routing socket for events */ |
| 56 | char ifname[IFNAMSIZ+1]; /* interface name */ |
| 57 | unsigned int ifindex; /* interface index */ |
| 58 | void *ctx; |
| 59 | struct wpa_driver_capa capa; /* driver capability */ |
| 60 | int is_ap; /* Access point mode */ |
| 61 | int prev_roaming; /* roaming state to restore on deinit */ |
| 62 | int prev_privacy; /* privacy state to restore on deinit */ |
| 63 | int prev_wpa; /* wpa state to restore on deinit */ |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 64 | enum ieee80211_opmode opmode; /* operation mode */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /* Generic functions for hostapd and wpa_supplicant */ |
| 68 | |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 69 | static enum ieee80211_opmode |
| 70 | get80211opmode(struct bsd_driver_data *drv) |
| 71 | { |
| 72 | struct ifmediareq ifmr; |
| 73 | |
| 74 | (void) memset(&ifmr, 0, sizeof(ifmr)); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 75 | (void) os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name)); |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 76 | |
| 77 | if (ioctl(drv->sock, SIOCGIFMEDIA, (caddr_t)&ifmr) >= 0) { |
| 78 | if (ifmr.ifm_current & IFM_IEEE80211_ADHOC) { |
| 79 | if (ifmr.ifm_current & IFM_FLAG0) |
| 80 | return IEEE80211_M_AHDEMO; |
| 81 | else |
| 82 | return IEEE80211_M_IBSS; |
| 83 | } |
| 84 | if (ifmr.ifm_current & IFM_IEEE80211_HOSTAP) |
| 85 | return IEEE80211_M_HOSTAP; |
| 86 | if (ifmr.ifm_current & IFM_IEEE80211_MONITOR) |
| 87 | return IEEE80211_M_MONITOR; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame^] | 88 | #ifdef IEEE80211_M_MBSS |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 89 | if (ifmr.ifm_current & IFM_IEEE80211_MBSS) |
| 90 | return IEEE80211_M_MBSS; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame^] | 91 | #endif /* IEEE80211_M_MBSS */ |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 92 | } |
| 93 | return IEEE80211_M_STA; |
| 94 | } |
| 95 | |
| 96 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 97 | static int |
| 98 | bsd_set80211(void *priv, int op, int val, const void *arg, int arg_len) |
| 99 | { |
| 100 | struct bsd_driver_data *drv = priv; |
| 101 | struct ieee80211req ireq; |
| 102 | |
| 103 | os_memset(&ireq, 0, sizeof(ireq)); |
| 104 | os_strlcpy(ireq.i_name, drv->ifname, sizeof(ireq.i_name)); |
| 105 | ireq.i_type = op; |
| 106 | ireq.i_val = val; |
| 107 | ireq.i_data = (void *) arg; |
| 108 | ireq.i_len = arg_len; |
| 109 | |
| 110 | if (ioctl(drv->sock, SIOCS80211, &ireq) < 0) { |
| 111 | wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, val=%u, " |
| 112 | "arg_len=%u]: %s", op, val, arg_len, |
| 113 | strerror(errno)); |
| 114 | return -1; |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int |
| 120 | bsd_get80211(void *priv, struct ieee80211req *ireq, int op, void *arg, |
| 121 | int arg_len) |
| 122 | { |
| 123 | struct bsd_driver_data *drv = priv; |
| 124 | |
| 125 | os_memset(ireq, 0, sizeof(*ireq)); |
| 126 | os_strlcpy(ireq->i_name, drv->ifname, sizeof(ireq->i_name)); |
| 127 | ireq->i_type = op; |
| 128 | ireq->i_len = arg_len; |
| 129 | ireq->i_data = arg; |
| 130 | |
| 131 | if (ioctl(drv->sock, SIOCG80211, ireq) < 0) { |
| 132 | wpa_printf(MSG_ERROR, "ioctl[SIOCS80211, op=%u, " |
| 133 | "arg_len=%u]: %s", op, arg_len, strerror(errno)); |
| 134 | return -1; |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int |
| 140 | get80211var(struct bsd_driver_data *drv, int op, void *arg, int arg_len) |
| 141 | { |
| 142 | struct ieee80211req ireq; |
| 143 | |
| 144 | if (bsd_get80211(drv, &ireq, op, arg, arg_len) < 0) |
| 145 | return -1; |
| 146 | return ireq.i_len; |
| 147 | } |
| 148 | |
| 149 | static int |
| 150 | set80211var(struct bsd_driver_data *drv, int op, const void *arg, int arg_len) |
| 151 | { |
| 152 | return bsd_set80211(drv, op, 0, arg, arg_len); |
| 153 | } |
| 154 | |
| 155 | static int |
| 156 | set80211param(struct bsd_driver_data *drv, int op, int arg) |
| 157 | { |
| 158 | return bsd_set80211(drv, op, arg, NULL, 0); |
| 159 | } |
| 160 | |
| 161 | static int |
| 162 | bsd_get_ssid(void *priv, u8 *ssid, int len) |
| 163 | { |
| 164 | struct bsd_driver_data *drv = priv; |
| 165 | #ifdef SIOCG80211NWID |
| 166 | struct ieee80211_nwid nwid; |
| 167 | struct ifreq ifr; |
| 168 | |
| 169 | os_memset(&ifr, 0, sizeof(ifr)); |
| 170 | os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); |
| 171 | ifr.ifr_data = (void *)&nwid; |
| 172 | if (ioctl(drv->sock, SIOCG80211NWID, &ifr) < 0 || |
| 173 | nwid.i_len > IEEE80211_NWID_LEN) |
| 174 | return -1; |
| 175 | os_memcpy(ssid, nwid.i_nwid, nwid.i_len); |
| 176 | return nwid.i_len; |
| 177 | #else |
| 178 | return get80211var(drv, IEEE80211_IOC_SSID, ssid, IEEE80211_NWID_LEN); |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | static int |
| 183 | bsd_set_ssid(void *priv, const u8 *ssid, int ssid_len) |
| 184 | { |
| 185 | struct bsd_driver_data *drv = priv; |
| 186 | #ifdef SIOCS80211NWID |
| 187 | struct ieee80211_nwid nwid; |
| 188 | struct ifreq ifr; |
| 189 | |
| 190 | os_memcpy(nwid.i_nwid, ssid, ssid_len); |
| 191 | nwid.i_len = ssid_len; |
| 192 | os_memset(&ifr, 0, sizeof(ifr)); |
| 193 | os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); |
| 194 | ifr.ifr_data = (void *)&nwid; |
| 195 | return ioctl(drv->sock, SIOCS80211NWID, &ifr); |
| 196 | #else |
| 197 | return set80211var(drv, IEEE80211_IOC_SSID, ssid, ssid_len); |
| 198 | #endif |
| 199 | } |
| 200 | |
| 201 | static int |
| 202 | bsd_get_if_media(void *priv) |
| 203 | { |
| 204 | struct bsd_driver_data *drv = priv; |
| 205 | struct ifmediareq ifmr; |
| 206 | |
| 207 | os_memset(&ifmr, 0, sizeof(ifmr)); |
| 208 | os_strlcpy(ifmr.ifm_name, drv->ifname, sizeof(ifmr.ifm_name)); |
| 209 | |
| 210 | if (ioctl(drv->sock, SIOCGIFMEDIA, &ifmr) < 0) { |
| 211 | wpa_printf(MSG_ERROR, "%s: SIOCGIFMEDIA %s", __func__, |
| 212 | strerror(errno)); |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | return ifmr.ifm_current; |
| 217 | } |
| 218 | |
| 219 | static int |
| 220 | bsd_set_if_media(void *priv, int media) |
| 221 | { |
| 222 | struct bsd_driver_data *drv = priv; |
| 223 | struct ifreq ifr; |
| 224 | |
| 225 | os_memset(&ifr, 0, sizeof(ifr)); |
| 226 | os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); |
| 227 | ifr.ifr_media = media; |
| 228 | |
| 229 | if (ioctl(drv->sock, SIOCSIFMEDIA, &ifr) < 0) { |
| 230 | wpa_printf(MSG_ERROR, "%s: SIOCSIFMEDIA %s", __func__, |
| 231 | strerror(errno)); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static int |
| 239 | bsd_set_mediaopt(void *priv, uint32_t mask, uint32_t mode) |
| 240 | { |
| 241 | int media = bsd_get_if_media(priv); |
| 242 | |
| 243 | if (media < 0) |
| 244 | return -1; |
| 245 | media &= ~mask; |
| 246 | media |= mode; |
| 247 | if (bsd_set_if_media(priv, media) < 0) |
| 248 | return -1; |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static int |
| 253 | bsd_del_key(void *priv, const u8 *addr, int key_idx) |
| 254 | { |
| 255 | struct ieee80211req_del_key wk; |
| 256 | |
| 257 | os_memset(&wk, 0, sizeof(wk)); |
| 258 | if (addr == NULL) { |
| 259 | wpa_printf(MSG_DEBUG, "%s: key_idx=%d", __func__, key_idx); |
| 260 | wk.idk_keyix = key_idx; |
| 261 | } else { |
| 262 | wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, |
| 263 | MAC2STR(addr)); |
| 264 | os_memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN); |
| 265 | wk.idk_keyix = (u_int8_t) IEEE80211_KEYIX_NONE; /* XXX */ |
| 266 | } |
| 267 | |
| 268 | return set80211var(priv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk)); |
| 269 | } |
| 270 | |
| 271 | static int |
| 272 | bsd_send_mlme_param(void *priv, const u8 op, const u16 reason, const u8 *addr) |
| 273 | { |
| 274 | struct ieee80211req_mlme mlme; |
| 275 | |
| 276 | os_memset(&mlme, 0, sizeof(mlme)); |
| 277 | mlme.im_op = op; |
| 278 | mlme.im_reason = reason; |
| 279 | os_memcpy(mlme.im_macaddr, addr, IEEE80211_ADDR_LEN); |
| 280 | return set80211var(priv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)); |
| 281 | } |
| 282 | |
| 283 | static int |
| 284 | bsd_ctrl_iface(void *priv, int enable) |
| 285 | { |
| 286 | struct bsd_driver_data *drv = priv; |
| 287 | struct ifreq ifr; |
| 288 | |
| 289 | os_memset(&ifr, 0, sizeof(ifr)); |
| 290 | os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name)); |
| 291 | |
| 292 | if (ioctl(drv->sock, SIOCGIFFLAGS, &ifr) < 0) { |
| 293 | perror("ioctl[SIOCGIFFLAGS]"); |
| 294 | return -1; |
| 295 | } |
| 296 | |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 297 | if (enable) { |
| 298 | if (ifr.ifr_flags & IFF_UP) |
| 299 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 300 | ifr.ifr_flags |= IFF_UP; |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 301 | } else { |
| 302 | if (!(ifr.ifr_flags & IFF_UP)) |
| 303 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 304 | ifr.ifr_flags &= ~IFF_UP; |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 305 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 306 | |
| 307 | if (ioctl(drv->sock, SIOCSIFFLAGS, &ifr) < 0) { |
| 308 | perror("ioctl[SIOCSIFFLAGS]"); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 315 | #ifdef HOSTAPD |
| 316 | static int |
| 317 | bsd_commit(void *priv) |
| 318 | { |
| 319 | return bsd_ctrl_iface(priv, 1); |
| 320 | } |
| 321 | #endif /* HOSTAPD */ |
| 322 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 323 | static int |
| 324 | bsd_set_key(const char *ifname, void *priv, enum wpa_alg alg, |
| 325 | const unsigned char *addr, int key_idx, int set_tx, const u8 *seq, |
| 326 | size_t seq_len, const u8 *key, size_t key_len) |
| 327 | { |
| 328 | struct ieee80211req_key wk; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame^] | 329 | #ifdef IEEE80211_KEY_NOREPLAY |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 330 | struct bsd_driver_data *drv = priv; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame^] | 331 | #endif /* IEEE80211_KEY_NOREPLAY */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 332 | |
| 333 | wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d " |
| 334 | "seq_len=%zu key_len=%zu", __func__, alg, addr, key_idx, |
| 335 | set_tx, seq_len, key_len); |
| 336 | |
| 337 | if (alg == WPA_ALG_NONE) { |
| 338 | #ifndef HOSTAPD |
| 339 | if (addr == NULL || is_broadcast_ether_addr(addr)) |
| 340 | return bsd_del_key(priv, NULL, key_idx); |
| 341 | else |
| 342 | #endif /* HOSTAPD */ |
| 343 | return bsd_del_key(priv, addr, key_idx); |
| 344 | } |
| 345 | |
| 346 | os_memset(&wk, 0, sizeof(wk)); |
| 347 | switch (alg) { |
| 348 | case WPA_ALG_WEP: |
| 349 | wk.ik_type = IEEE80211_CIPHER_WEP; |
| 350 | break; |
| 351 | case WPA_ALG_TKIP: |
| 352 | wk.ik_type = IEEE80211_CIPHER_TKIP; |
| 353 | break; |
| 354 | case WPA_ALG_CCMP: |
| 355 | wk.ik_type = IEEE80211_CIPHER_AES_CCM; |
| 356 | break; |
| 357 | default: |
| 358 | wpa_printf(MSG_ERROR, "%s: unknown alg=%d", __func__, alg); |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | wk.ik_flags = IEEE80211_KEY_RECV; |
| 363 | if (set_tx) |
| 364 | wk.ik_flags |= IEEE80211_KEY_XMIT; |
| 365 | |
| 366 | if (addr == NULL) { |
| 367 | os_memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); |
| 368 | wk.ik_keyix = key_idx; |
| 369 | } else { |
| 370 | os_memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); |
| 371 | /* |
| 372 | * Deduce whether group/global or unicast key by checking |
| 373 | * the address (yech). Note also that we can only mark global |
| 374 | * keys default; doing this for a unicast key is an error. |
| 375 | */ |
| 376 | if (is_broadcast_ether_addr(addr)) { |
| 377 | wk.ik_flags |= IEEE80211_KEY_GROUP; |
| 378 | wk.ik_keyix = key_idx; |
| 379 | } else { |
| 380 | wk.ik_keyix = key_idx == 0 ? IEEE80211_KEYIX_NONE : |
| 381 | key_idx; |
| 382 | } |
| 383 | } |
| 384 | if (wk.ik_keyix != IEEE80211_KEYIX_NONE && set_tx) |
| 385 | wk.ik_flags |= IEEE80211_KEY_DEFAULT; |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 386 | #ifndef HOSTAPD |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame^] | 387 | #ifdef IEEE80211_KEY_NOREPLAY |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 388 | /* |
| 389 | * Ignore replay failures in IBSS and AHDEMO mode. |
| 390 | */ |
| 391 | if (drv->opmode == IEEE80211_M_IBSS || |
| 392 | drv->opmode == IEEE80211_M_AHDEMO) |
| 393 | wk.ik_flags |= IEEE80211_KEY_NOREPLAY; |
Dmitry Shmidt | 04f534e | 2013-12-09 15:50:16 -0800 | [diff] [blame^] | 394 | #endif /* IEEE80211_KEY_NOREPLAY */ |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 395 | #endif /* HOSTAPD */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 396 | wk.ik_keylen = key_len; |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 397 | if (seq) { |
| 398 | #ifdef WORDS_BIGENDIAN |
| 399 | /* |
| 400 | * wk.ik_keyrsc is in host byte order (big endian), need to |
| 401 | * swap it to match with the byte order used in WPA. |
| 402 | */ |
| 403 | int i; |
| 404 | u8 *keyrsc = (u8 *) &wk.ik_keyrsc; |
| 405 | for (i = 0; i < seq_len; i++) |
| 406 | keyrsc[WPA_KEY_RSC_LEN - i - 1] = seq[i]; |
| 407 | #else /* WORDS_BIGENDIAN */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 408 | os_memcpy(&wk.ik_keyrsc, seq, seq_len); |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 409 | #endif /* WORDS_BIGENDIAN */ |
| 410 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 411 | os_memcpy(wk.ik_keydata, key, key_len); |
| 412 | |
| 413 | return set80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)); |
| 414 | } |
| 415 | |
| 416 | static int |
| 417 | bsd_configure_wpa(void *priv, struct wpa_bss_params *params) |
| 418 | { |
| 419 | #ifndef IEEE80211_IOC_APPIE |
| 420 | static const char *ciphernames[] = |
| 421 | { "WEP", "TKIP", "AES-OCB", "AES-CCM", "CKIP", "NONE" }; |
| 422 | int v; |
| 423 | |
| 424 | switch (params->wpa_group) { |
| 425 | case WPA_CIPHER_CCMP: |
| 426 | v = IEEE80211_CIPHER_AES_CCM; |
| 427 | break; |
| 428 | case WPA_CIPHER_TKIP: |
| 429 | v = IEEE80211_CIPHER_TKIP; |
| 430 | break; |
| 431 | case WPA_CIPHER_WEP104: |
| 432 | v = IEEE80211_CIPHER_WEP; |
| 433 | break; |
| 434 | case WPA_CIPHER_WEP40: |
| 435 | v = IEEE80211_CIPHER_WEP; |
| 436 | break; |
| 437 | case WPA_CIPHER_NONE: |
| 438 | v = IEEE80211_CIPHER_NONE; |
| 439 | break; |
| 440 | default: |
| 441 | printf("Unknown group key cipher %u\n", |
| 442 | params->wpa_group); |
| 443 | return -1; |
| 444 | } |
| 445 | wpa_printf(MSG_DEBUG, "%s: group key cipher=%s (%u)", |
| 446 | __func__, ciphernames[v], v); |
| 447 | if (set80211param(priv, IEEE80211_IOC_MCASTCIPHER, v)) { |
| 448 | printf("Unable to set group key cipher to %u (%s)\n", |
| 449 | v, ciphernames[v]); |
| 450 | return -1; |
| 451 | } |
| 452 | if (v == IEEE80211_CIPHER_WEP) { |
| 453 | /* key length is done only for specific ciphers */ |
| 454 | v = (params->wpa_group == WPA_CIPHER_WEP104 ? 13 : 5); |
| 455 | if (set80211param(priv, IEEE80211_IOC_MCASTKEYLEN, v)) { |
| 456 | printf("Unable to set group key length to %u\n", v); |
| 457 | return -1; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | v = 0; |
| 462 | if (params->wpa_pairwise & WPA_CIPHER_CCMP) |
| 463 | v |= 1<<IEEE80211_CIPHER_AES_CCM; |
| 464 | if (params->wpa_pairwise & WPA_CIPHER_TKIP) |
| 465 | v |= 1<<IEEE80211_CIPHER_TKIP; |
| 466 | if (params->wpa_pairwise & WPA_CIPHER_NONE) |
| 467 | v |= 1<<IEEE80211_CIPHER_NONE; |
| 468 | wpa_printf(MSG_DEBUG, "%s: pairwise key ciphers=0x%x", __func__, v); |
| 469 | if (set80211param(priv, IEEE80211_IOC_UCASTCIPHERS, v)) { |
| 470 | printf("Unable to set pairwise key ciphers to 0x%x\n", v); |
| 471 | return -1; |
| 472 | } |
| 473 | |
| 474 | wpa_printf(MSG_DEBUG, "%s: key management algorithms=0x%x", |
| 475 | __func__, params->wpa_key_mgmt); |
| 476 | if (set80211param(priv, IEEE80211_IOC_KEYMGTALGS, |
| 477 | params->wpa_key_mgmt)) { |
| 478 | printf("Unable to set key management algorithms to 0x%x\n", |
| 479 | params->wpa_key_mgmt); |
| 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | v = 0; |
| 484 | if (params->rsn_preauth) |
| 485 | v |= BIT(0); |
| 486 | wpa_printf(MSG_DEBUG, "%s: rsn capabilities=0x%x", |
| 487 | __func__, params->rsn_preauth); |
| 488 | if (set80211param(priv, IEEE80211_IOC_RSNCAPS, v)) { |
| 489 | printf("Unable to set RSN capabilities to 0x%x\n", v); |
| 490 | return -1; |
| 491 | } |
| 492 | #endif /* IEEE80211_IOC_APPIE */ |
| 493 | |
| 494 | wpa_printf(MSG_DEBUG, "%s: enable WPA= 0x%x", __func__, params->wpa); |
| 495 | if (set80211param(priv, IEEE80211_IOC_WPA, params->wpa)) { |
| 496 | printf("Unable to set WPA to %u\n", params->wpa); |
| 497 | return -1; |
| 498 | } |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int |
| 503 | bsd_set_ieee8021x(void *priv, struct wpa_bss_params *params) |
| 504 | { |
| 505 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, params->enabled); |
| 506 | |
| 507 | if (!params->enabled) { |
| 508 | /* XXX restore state */ |
| 509 | return set80211param(priv, IEEE80211_IOC_AUTHMODE, |
| 510 | IEEE80211_AUTH_AUTO); |
| 511 | } |
| 512 | if (!params->wpa && !params->ieee802_1x) { |
| 513 | wpa_printf(MSG_ERROR, "%s: No 802.1X or WPA enabled", |
| 514 | __func__); |
| 515 | return -1; |
| 516 | } |
| 517 | if (params->wpa && bsd_configure_wpa(priv, params) != 0) { |
| 518 | wpa_printf(MSG_ERROR, "%s: Failed to configure WPA state", |
| 519 | __func__); |
| 520 | return -1; |
| 521 | } |
| 522 | if (set80211param(priv, IEEE80211_IOC_AUTHMODE, |
| 523 | (params->wpa ? IEEE80211_AUTH_WPA : IEEE80211_AUTH_8021X))) { |
| 524 | wpa_printf(MSG_ERROR, "%s: Failed to enable WPA/802.1X", |
| 525 | __func__); |
| 526 | return -1; |
| 527 | } |
| 528 | return bsd_ctrl_iface(priv, 1); |
| 529 | } |
| 530 | |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 531 | #ifdef HOSTAPD |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 532 | static int |
| 533 | bsd_set_sta_authorized(void *priv, const u8 *addr, |
| 534 | int total_flags, int flags_or, int flags_and) |
| 535 | { |
| 536 | int authorized = -1; |
| 537 | |
| 538 | /* For now, only support setting Authorized flag */ |
| 539 | if (flags_or & WPA_STA_AUTHORIZED) |
| 540 | authorized = 1; |
| 541 | if (!(flags_and & WPA_STA_AUTHORIZED)) |
| 542 | authorized = 0; |
| 543 | |
| 544 | if (authorized < 0) |
| 545 | return 0; |
| 546 | |
| 547 | return bsd_send_mlme_param(priv, authorized ? |
| 548 | IEEE80211_MLME_AUTHORIZE : |
| 549 | IEEE80211_MLME_UNAUTHORIZE, 0, addr); |
| 550 | } |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 551 | #endif /* HOSTAPD */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 552 | |
| 553 | static void |
| 554 | bsd_new_sta(void *priv, void *ctx, u8 addr[IEEE80211_ADDR_LEN]) |
| 555 | { |
| 556 | struct ieee80211req_wpaie ie; |
| 557 | int ielen = 0; |
| 558 | u8 *iebuf = NULL; |
| 559 | |
| 560 | /* |
| 561 | * Fetch and validate any negotiated WPA/RSN parameters. |
| 562 | */ |
| 563 | memset(&ie, 0, sizeof(ie)); |
| 564 | memcpy(ie.wpa_macaddr, addr, IEEE80211_ADDR_LEN); |
| 565 | if (get80211var(priv, IEEE80211_IOC_WPAIE, &ie, sizeof(ie)) < 0) { |
| 566 | printf("Failed to get WPA/RSN information element.\n"); |
| 567 | goto no_ie; |
| 568 | } |
| 569 | iebuf = ie.wpa_ie; |
| 570 | ielen = ie.wpa_ie[1]; |
| 571 | if (ielen == 0) |
| 572 | iebuf = NULL; |
| 573 | else |
| 574 | ielen += 2; |
| 575 | |
| 576 | no_ie: |
| 577 | drv_event_assoc(ctx, addr, iebuf, ielen, 0); |
| 578 | } |
| 579 | |
| 580 | static int |
| 581 | bsd_send_eapol(void *priv, const u8 *addr, const u8 *data, size_t data_len, |
| 582 | int encrypt, const u8 *own_addr, u32 flags) |
| 583 | { |
| 584 | struct bsd_driver_data *drv = priv; |
| 585 | |
| 586 | wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", data, data_len); |
| 587 | |
| 588 | return l2_packet_send(drv->sock_xmit, addr, ETH_P_EAPOL, data, |
| 589 | data_len); |
| 590 | } |
| 591 | |
| 592 | static int |
| 593 | bsd_set_freq(void *priv, struct hostapd_freq_params *freq) |
| 594 | { |
| 595 | struct bsd_driver_data *drv = priv; |
| 596 | #ifdef SIOCS80211CHANNEL |
| 597 | struct ieee80211chanreq creq; |
| 598 | #endif /* SIOCS80211CHANNEL */ |
| 599 | u32 mode; |
| 600 | int channel = freq->channel; |
| 601 | |
| 602 | if (channel < 14) { |
| 603 | mode = |
| 604 | #ifdef CONFIG_IEEE80211N |
| 605 | freq->ht_enabled ? IFM_IEEE80211_11NG : |
| 606 | #endif /* CONFIG_IEEE80211N */ |
| 607 | IFM_IEEE80211_11G; |
| 608 | } else if (channel == 14) { |
| 609 | mode = IFM_IEEE80211_11B; |
| 610 | } else { |
| 611 | mode = |
| 612 | #ifdef CONFIG_IEEE80211N |
| 613 | freq->ht_enabled ? IFM_IEEE80211_11NA : |
| 614 | #endif /* CONFIG_IEEE80211N */ |
| 615 | IFM_IEEE80211_11A; |
| 616 | } |
| 617 | if (bsd_set_mediaopt(drv, IFM_MMASK, mode) < 0) { |
| 618 | wpa_printf(MSG_ERROR, "%s: failed to set modulation mode", |
| 619 | __func__); |
| 620 | return -1; |
| 621 | } |
| 622 | |
| 623 | #ifdef SIOCS80211CHANNEL |
| 624 | os_memset(&creq, 0, sizeof(creq)); |
| 625 | os_strlcpy(creq.i_name, drv->ifname, sizeof(creq.i_name)); |
| 626 | creq.i_channel = (u_int16_t)channel; |
| 627 | return ioctl(drv->sock, SIOCS80211CHANNEL, &creq); |
| 628 | #else /* SIOCS80211CHANNEL */ |
| 629 | return set80211param(priv, IEEE80211_IOC_CHANNEL, channel); |
| 630 | #endif /* SIOCS80211CHANNEL */ |
| 631 | } |
| 632 | |
| 633 | static int |
| 634 | bsd_set_opt_ie(void *priv, const u8 *ie, size_t ie_len) |
| 635 | { |
| 636 | #ifdef IEEE80211_IOC_APPIE |
| 637 | wpa_printf(MSG_DEBUG, "%s: set WPA+RSN ie (len %lu)", __func__, |
| 638 | (unsigned long)ie_len); |
| 639 | return bsd_set80211(priv, IEEE80211_IOC_APPIE, IEEE80211_APPIE_WPA, |
| 640 | ie, ie_len); |
| 641 | #endif /* IEEE80211_IOC_APPIE */ |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | static int |
| 646 | rtbuf_len(void) |
| 647 | { |
| 648 | size_t len; |
| 649 | |
| 650 | int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET, NET_RT_DUMP, 0}; |
| 651 | |
| 652 | if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { |
| 653 | wpa_printf(MSG_WARNING, "%s failed: %s\n", __func__, |
| 654 | strerror(errno)); |
| 655 | len = 2048; |
| 656 | } |
| 657 | |
| 658 | return len; |
| 659 | } |
| 660 | |
| 661 | #ifdef HOSTAPD |
| 662 | |
| 663 | /* |
| 664 | * Avoid conflicts with hostapd definitions by undefining couple of defines |
| 665 | * from net80211 header files. |
| 666 | */ |
| 667 | #undef RSN_VERSION |
| 668 | #undef WPA_VERSION |
| 669 | #undef WPA_OUI_TYPE |
| 670 | |
| 671 | static int bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, |
| 672 | int reason_code); |
| 673 | |
| 674 | static const char * |
| 675 | ether_sprintf(const u8 *addr) |
| 676 | { |
| 677 | static char buf[sizeof(MACSTR)]; |
| 678 | |
| 679 | if (addr != NULL) |
| 680 | snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr)); |
| 681 | else |
| 682 | snprintf(buf, sizeof(buf), MACSTR, 0,0,0,0,0,0); |
| 683 | return buf; |
| 684 | } |
| 685 | |
| 686 | static int |
| 687 | bsd_set_privacy(void *priv, int enabled) |
| 688 | { |
| 689 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); |
| 690 | |
| 691 | return set80211param(priv, IEEE80211_IOC_PRIVACY, enabled); |
| 692 | } |
| 693 | |
| 694 | static int |
| 695 | bsd_get_seqnum(const char *ifname, void *priv, const u8 *addr, int idx, |
| 696 | u8 *seq) |
| 697 | { |
| 698 | struct ieee80211req_key wk; |
| 699 | |
| 700 | wpa_printf(MSG_DEBUG, "%s: addr=%s idx=%d", |
| 701 | __func__, ether_sprintf(addr), idx); |
| 702 | |
| 703 | memset(&wk, 0, sizeof(wk)); |
| 704 | if (addr == NULL) |
| 705 | memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN); |
| 706 | else |
| 707 | memcpy(wk.ik_macaddr, addr, IEEE80211_ADDR_LEN); |
| 708 | wk.ik_keyix = idx; |
| 709 | |
| 710 | if (get80211var(priv, IEEE80211_IOC_WPAKEY, &wk, sizeof(wk)) < 0) { |
| 711 | printf("Failed to get encryption.\n"); |
| 712 | return -1; |
| 713 | } |
| 714 | |
| 715 | #ifdef WORDS_BIGENDIAN |
| 716 | { |
| 717 | /* |
| 718 | * wk.ik_keytsc is in host byte order (big endian), need to |
| 719 | * swap it to match with the byte order used in WPA. |
| 720 | */ |
| 721 | int i; |
| 722 | u8 tmp[WPA_KEY_RSC_LEN]; |
| 723 | memcpy(tmp, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); |
| 724 | for (i = 0; i < WPA_KEY_RSC_LEN; i++) { |
| 725 | seq[i] = tmp[WPA_KEY_RSC_LEN - i - 1]; |
| 726 | } |
| 727 | } |
| 728 | #else /* WORDS_BIGENDIAN */ |
| 729 | memcpy(seq, &wk.ik_keytsc, sizeof(wk.ik_keytsc)); |
| 730 | #endif /* WORDS_BIGENDIAN */ |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | |
| 735 | static int |
| 736 | bsd_flush(void *priv) |
| 737 | { |
| 738 | u8 allsta[IEEE80211_ADDR_LEN]; |
| 739 | |
| 740 | memset(allsta, 0xff, IEEE80211_ADDR_LEN); |
| 741 | return bsd_sta_deauth(priv, NULL, allsta, IEEE80211_REASON_AUTH_LEAVE); |
| 742 | } |
| 743 | |
| 744 | |
| 745 | static int |
| 746 | bsd_read_sta_driver_data(void *priv, struct hostap_sta_driver_data *data, |
| 747 | const u8 *addr) |
| 748 | { |
| 749 | struct ieee80211req_sta_stats stats; |
| 750 | |
| 751 | memcpy(stats.is_u.macaddr, addr, IEEE80211_ADDR_LEN); |
| 752 | if (get80211var(priv, IEEE80211_IOC_STA_STATS, &stats, sizeof(stats)) |
| 753 | > 0) { |
| 754 | /* XXX? do packets counts include non-data frames? */ |
| 755 | data->rx_packets = stats.is_stats.ns_rx_data; |
| 756 | data->rx_bytes = stats.is_stats.ns_rx_bytes; |
| 757 | data->tx_packets = stats.is_stats.ns_tx_data; |
| 758 | data->tx_bytes = stats.is_stats.ns_tx_bytes; |
| 759 | } |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static int |
| 764 | bsd_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, int reason_code) |
| 765 | { |
| 766 | return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code, |
| 767 | addr); |
| 768 | } |
| 769 | |
| 770 | static int |
| 771 | bsd_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr, |
| 772 | int reason_code) |
| 773 | { |
| 774 | return bsd_send_mlme_param(priv, IEEE80211_MLME_DISASSOC, reason_code, |
| 775 | addr); |
| 776 | } |
| 777 | |
| 778 | static void |
| 779 | bsd_wireless_event_receive(int sock, void *ctx, void *sock_ctx) |
| 780 | { |
| 781 | struct bsd_driver_data *drv = ctx; |
| 782 | char *buf; |
| 783 | struct if_announcemsghdr *ifan; |
| 784 | struct rt_msghdr *rtm; |
| 785 | struct ieee80211_michael_event *mic; |
| 786 | struct ieee80211_join_event *join; |
| 787 | struct ieee80211_leave_event *leave; |
| 788 | int n, len; |
| 789 | union wpa_event_data data; |
| 790 | |
| 791 | len = rtbuf_len(); |
| 792 | |
| 793 | buf = os_malloc(len); |
| 794 | if (buf == NULL) { |
| 795 | wpa_printf(MSG_ERROR, "%s os_malloc() failed\n", __func__); |
| 796 | return; |
| 797 | } |
| 798 | |
| 799 | n = read(sock, buf, len); |
| 800 | if (n < 0) { |
| 801 | if (errno != EINTR && errno != EAGAIN) |
| 802 | wpa_printf(MSG_ERROR, "%s read() failed: %s\n", |
| 803 | __func__, strerror(errno)); |
| 804 | os_free(buf); |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | rtm = (struct rt_msghdr *) buf; |
| 809 | if (rtm->rtm_version != RTM_VERSION) { |
| 810 | wpa_printf(MSG_DEBUG, "Invalid routing message version=%d", |
| 811 | rtm->rtm_version); |
| 812 | os_free(buf); |
| 813 | return; |
| 814 | } |
| 815 | ifan = (struct if_announcemsghdr *) rtm; |
| 816 | switch (rtm->rtm_type) { |
| 817 | case RTM_IEEE80211: |
| 818 | switch (ifan->ifan_what) { |
| 819 | case RTM_IEEE80211_ASSOC: |
| 820 | case RTM_IEEE80211_REASSOC: |
| 821 | case RTM_IEEE80211_DISASSOC: |
| 822 | case RTM_IEEE80211_SCAN: |
| 823 | break; |
| 824 | case RTM_IEEE80211_LEAVE: |
| 825 | leave = (struct ieee80211_leave_event *) &ifan[1]; |
| 826 | drv_event_disassoc(drv->hapd, leave->iev_addr); |
| 827 | break; |
| 828 | case RTM_IEEE80211_JOIN: |
| 829 | #ifdef RTM_IEEE80211_REJOIN |
| 830 | case RTM_IEEE80211_REJOIN: |
| 831 | #endif |
| 832 | join = (struct ieee80211_join_event *) &ifan[1]; |
| 833 | bsd_new_sta(drv, drv->hapd, join->iev_addr); |
| 834 | break; |
| 835 | case RTM_IEEE80211_REPLAY: |
| 836 | /* ignore */ |
| 837 | break; |
| 838 | case RTM_IEEE80211_MICHAEL: |
| 839 | mic = (struct ieee80211_michael_event *) &ifan[1]; |
| 840 | wpa_printf(MSG_DEBUG, |
| 841 | "Michael MIC failure wireless event: " |
| 842 | "keyix=%u src_addr=" MACSTR, mic->iev_keyix, |
| 843 | MAC2STR(mic->iev_src)); |
| 844 | os_memset(&data, 0, sizeof(data)); |
| 845 | data.michael_mic_failure.unicast = 1; |
| 846 | data.michael_mic_failure.src = mic->iev_src; |
| 847 | wpa_supplicant_event(drv->hapd, |
| 848 | EVENT_MICHAEL_MIC_FAILURE, &data); |
| 849 | break; |
| 850 | } |
| 851 | break; |
| 852 | } |
| 853 | os_free(buf); |
| 854 | } |
| 855 | |
| 856 | static void |
| 857 | handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len) |
| 858 | { |
| 859 | struct bsd_driver_data *drv = ctx; |
| 860 | drv_event_eapol_rx(drv->hapd, src_addr, buf, len); |
| 861 | } |
| 862 | |
| 863 | static void * |
| 864 | bsd_init(struct hostapd_data *hapd, struct wpa_init_params *params) |
| 865 | { |
| 866 | struct bsd_driver_data *drv; |
| 867 | |
| 868 | drv = os_zalloc(sizeof(struct bsd_driver_data)); |
| 869 | if (drv == NULL) { |
| 870 | printf("Could not allocate memory for bsd driver data\n"); |
| 871 | goto bad; |
| 872 | } |
| 873 | |
| 874 | drv->hapd = hapd; |
| 875 | drv->sock = socket(PF_INET, SOCK_DGRAM, 0); |
| 876 | if (drv->sock < 0) { |
| 877 | perror("socket[PF_INET,SOCK_DGRAM]"); |
| 878 | goto bad; |
| 879 | } |
| 880 | os_strlcpy(drv->ifname, params->ifname, sizeof(drv->ifname)); |
| 881 | |
| 882 | drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL, |
| 883 | handle_read, drv, 0); |
| 884 | if (drv->sock_xmit == NULL) |
| 885 | goto bad; |
| 886 | if (l2_packet_get_own_addr(drv->sock_xmit, params->own_addr)) |
| 887 | goto bad; |
| 888 | |
| 889 | /* mark down during setup */ |
| 890 | if (bsd_ctrl_iface(drv, 0) < 0) |
| 891 | goto bad; |
| 892 | |
| 893 | drv->route = socket(PF_ROUTE, SOCK_RAW, 0); |
| 894 | if (drv->route < 0) { |
| 895 | perror("socket(PF_ROUTE,SOCK_RAW)"); |
| 896 | goto bad; |
| 897 | } |
| 898 | eloop_register_read_sock(drv->route, bsd_wireless_event_receive, drv, |
| 899 | NULL); |
| 900 | |
| 901 | if (bsd_set_mediaopt(drv, IFM_OMASK, IFM_IEEE80211_HOSTAP) < 0) { |
| 902 | wpa_printf(MSG_ERROR, "%s: failed to set operation mode", |
| 903 | __func__); |
| 904 | goto bad; |
| 905 | } |
| 906 | |
| 907 | return drv; |
| 908 | bad: |
| 909 | if (drv->sock_xmit != NULL) |
| 910 | l2_packet_deinit(drv->sock_xmit); |
| 911 | if (drv->sock >= 0) |
| 912 | close(drv->sock); |
| 913 | if (drv != NULL) |
| 914 | os_free(drv); |
| 915 | return NULL; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | static void |
| 920 | bsd_deinit(void *priv) |
| 921 | { |
| 922 | struct bsd_driver_data *drv = priv; |
| 923 | |
| 924 | if (drv->route >= 0) { |
| 925 | eloop_unregister_read_sock(drv->route); |
| 926 | close(drv->route); |
| 927 | } |
| 928 | bsd_ctrl_iface(drv, 0); |
| 929 | if (drv->sock >= 0) |
| 930 | close(drv->sock); |
| 931 | if (drv->sock_xmit != NULL) |
| 932 | l2_packet_deinit(drv->sock_xmit); |
| 933 | os_free(drv); |
| 934 | } |
| 935 | |
| 936 | #else /* HOSTAPD */ |
| 937 | |
| 938 | static int |
| 939 | get80211param(struct bsd_driver_data *drv, int op) |
| 940 | { |
| 941 | struct ieee80211req ireq; |
| 942 | |
| 943 | if (bsd_get80211(drv, &ireq, op, NULL, 0) < 0) |
| 944 | return -1; |
| 945 | return ireq.i_val; |
| 946 | } |
| 947 | |
| 948 | static int |
| 949 | wpa_driver_bsd_get_bssid(void *priv, u8 *bssid) |
| 950 | { |
| 951 | struct bsd_driver_data *drv = priv; |
| 952 | #ifdef SIOCG80211BSSID |
| 953 | struct ieee80211_bssid bs; |
| 954 | |
| 955 | os_strlcpy(bs.i_name, drv->ifname, sizeof(bs.i_name)); |
| 956 | if (ioctl(drv->sock, SIOCG80211BSSID, &bs) < 0) |
| 957 | return -1; |
| 958 | os_memcpy(bssid, bs.i_bssid, sizeof(bs.i_bssid)); |
| 959 | return 0; |
| 960 | #else |
| 961 | return get80211var(drv, IEEE80211_IOC_BSSID, |
| 962 | bssid, IEEE80211_ADDR_LEN) < 0 ? -1 : 0; |
| 963 | #endif |
| 964 | } |
| 965 | |
| 966 | static int |
| 967 | wpa_driver_bsd_get_ssid(void *priv, u8 *ssid) |
| 968 | { |
| 969 | struct bsd_driver_data *drv = priv; |
| 970 | return bsd_get_ssid(drv, ssid, 0); |
| 971 | } |
| 972 | |
| 973 | static int |
| 974 | wpa_driver_bsd_set_wpa_ie(struct bsd_driver_data *drv, const u8 *wpa_ie, |
| 975 | size_t wpa_ie_len) |
| 976 | { |
| 977 | #ifdef IEEE80211_IOC_APPIE |
| 978 | return bsd_set_opt_ie(drv, wpa_ie, wpa_ie_len); |
| 979 | #else /* IEEE80211_IOC_APPIE */ |
| 980 | return set80211var(drv, IEEE80211_IOC_OPTIE, wpa_ie, wpa_ie_len); |
| 981 | #endif /* IEEE80211_IOC_APPIE */ |
| 982 | } |
| 983 | |
| 984 | static int |
| 985 | wpa_driver_bsd_set_wpa_internal(void *priv, int wpa, int privacy) |
| 986 | { |
| 987 | int ret = 0; |
| 988 | |
| 989 | wpa_printf(MSG_DEBUG, "%s: wpa=%d privacy=%d", |
| 990 | __FUNCTION__, wpa, privacy); |
| 991 | |
| 992 | if (!wpa && wpa_driver_bsd_set_wpa_ie(priv, NULL, 0) < 0) |
| 993 | ret = -1; |
| 994 | if (set80211param(priv, IEEE80211_IOC_PRIVACY, privacy) < 0) |
| 995 | ret = -1; |
| 996 | if (set80211param(priv, IEEE80211_IOC_WPA, wpa) < 0) |
| 997 | ret = -1; |
| 998 | |
| 999 | return ret; |
| 1000 | } |
| 1001 | |
| 1002 | static int |
| 1003 | wpa_driver_bsd_set_wpa(void *priv, int enabled) |
| 1004 | { |
| 1005 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __FUNCTION__, enabled); |
| 1006 | |
| 1007 | return wpa_driver_bsd_set_wpa_internal(priv, enabled ? 3 : 0, enabled); |
| 1008 | } |
| 1009 | |
| 1010 | static int |
| 1011 | wpa_driver_bsd_set_countermeasures(void *priv, int enabled) |
| 1012 | { |
| 1013 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); |
| 1014 | return set80211param(priv, IEEE80211_IOC_COUNTERMEASURES, enabled); |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | static int |
| 1019 | wpa_driver_bsd_set_drop_unencrypted(void *priv, int enabled) |
| 1020 | { |
| 1021 | wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled); |
| 1022 | return set80211param(priv, IEEE80211_IOC_DROPUNENCRYPTED, enabled); |
| 1023 | } |
| 1024 | |
| 1025 | static int |
| 1026 | wpa_driver_bsd_deauthenticate(void *priv, const u8 *addr, int reason_code) |
| 1027 | { |
| 1028 | return bsd_send_mlme_param(priv, IEEE80211_MLME_DEAUTH, reason_code, |
| 1029 | addr); |
| 1030 | } |
| 1031 | |
| 1032 | static int |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1033 | wpa_driver_bsd_set_auth_alg(void *priv, int auth_alg) |
| 1034 | { |
| 1035 | int authmode; |
| 1036 | |
| 1037 | if ((auth_alg & WPA_AUTH_ALG_OPEN) && |
| 1038 | (auth_alg & WPA_AUTH_ALG_SHARED)) |
| 1039 | authmode = IEEE80211_AUTH_AUTO; |
| 1040 | else if (auth_alg & WPA_AUTH_ALG_SHARED) |
| 1041 | authmode = IEEE80211_AUTH_SHARED; |
| 1042 | else |
| 1043 | authmode = IEEE80211_AUTH_OPEN; |
| 1044 | |
| 1045 | return set80211param(priv, IEEE80211_IOC_AUTHMODE, authmode); |
| 1046 | } |
| 1047 | |
| 1048 | static void |
| 1049 | handle_read(void *ctx, const u8 *src_addr, const u8 *buf, size_t len) |
| 1050 | { |
| 1051 | struct bsd_driver_data *drv = ctx; |
| 1052 | |
| 1053 | drv_event_eapol_rx(drv->ctx, src_addr, buf, len); |
| 1054 | } |
| 1055 | |
| 1056 | static int |
| 1057 | wpa_driver_bsd_associate(void *priv, struct wpa_driver_associate_params *params) |
| 1058 | { |
| 1059 | struct bsd_driver_data *drv = priv; |
| 1060 | struct ieee80211req_mlme mlme; |
| 1061 | u32 mode; |
| 1062 | int privacy; |
| 1063 | int ret = 0; |
| 1064 | |
| 1065 | wpa_printf(MSG_DEBUG, |
| 1066 | "%s: ssid '%.*s' wpa ie len %u pairwise %u group %u key mgmt %u" |
| 1067 | , __func__ |
| 1068 | , (unsigned int) params->ssid_len, params->ssid |
| 1069 | , (unsigned int) params->wpa_ie_len |
| 1070 | , params->pairwise_suite |
| 1071 | , params->group_suite |
| 1072 | , params->key_mgmt_suite |
| 1073 | ); |
| 1074 | |
| 1075 | switch (params->mode) { |
| 1076 | case IEEE80211_MODE_INFRA: |
| 1077 | mode = 0 /* STA */; |
| 1078 | break; |
| 1079 | case IEEE80211_MODE_IBSS: |
| 1080 | mode = IFM_IEEE80211_IBSS; |
| 1081 | break; |
| 1082 | case IEEE80211_MODE_AP: |
| 1083 | mode = IFM_IEEE80211_HOSTAP; |
| 1084 | break; |
| 1085 | default: |
| 1086 | wpa_printf(MSG_ERROR, "%s: unknown operation mode", __func__); |
| 1087 | return -1; |
| 1088 | } |
| 1089 | if (bsd_set_mediaopt(drv, IFM_OMASK, mode) < 0) { |
| 1090 | wpa_printf(MSG_ERROR, "%s: failed to set operation mode", |
| 1091 | __func__); |
| 1092 | return -1; |
| 1093 | } |
| 1094 | |
| 1095 | if (params->mode == IEEE80211_MODE_AP) { |
| 1096 | drv->sock_xmit = l2_packet_init(drv->ifname, NULL, ETH_P_EAPOL, |
| 1097 | handle_read, drv, 0); |
| 1098 | if (drv->sock_xmit == NULL) |
| 1099 | return -1; |
| 1100 | drv->is_ap = 1; |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | if (wpa_driver_bsd_set_drop_unencrypted(drv, params->drop_unencrypted) |
| 1105 | < 0) |
| 1106 | ret = -1; |
| 1107 | if (wpa_driver_bsd_set_auth_alg(drv, params->auth_alg) < 0) |
| 1108 | ret = -1; |
| 1109 | /* XXX error handling is wrong but unclear what to do... */ |
| 1110 | if (wpa_driver_bsd_set_wpa_ie(drv, params->wpa_ie, params->wpa_ie_len) < 0) |
| 1111 | return -1; |
| 1112 | |
| 1113 | privacy = !(params->pairwise_suite == CIPHER_NONE && |
| 1114 | params->group_suite == CIPHER_NONE && |
| 1115 | params->key_mgmt_suite == KEY_MGMT_NONE && |
| 1116 | params->wpa_ie_len == 0); |
| 1117 | wpa_printf(MSG_DEBUG, "%s: set PRIVACY %u", __func__, privacy); |
| 1118 | |
| 1119 | if (set80211param(drv, IEEE80211_IOC_PRIVACY, privacy) < 0) |
| 1120 | return -1; |
| 1121 | |
| 1122 | if (params->wpa_ie_len && |
| 1123 | set80211param(drv, IEEE80211_IOC_WPA, |
| 1124 | params->wpa_ie[0] == WLAN_EID_RSN ? 2 : 1) < 0) |
| 1125 | return -1; |
| 1126 | |
| 1127 | os_memset(&mlme, 0, sizeof(mlme)); |
| 1128 | mlme.im_op = IEEE80211_MLME_ASSOC; |
| 1129 | if (params->ssid != NULL) |
| 1130 | os_memcpy(mlme.im_ssid, params->ssid, params->ssid_len); |
| 1131 | mlme.im_ssid_len = params->ssid_len; |
| 1132 | if (params->bssid != NULL) |
| 1133 | os_memcpy(mlme.im_macaddr, params->bssid, IEEE80211_ADDR_LEN); |
| 1134 | if (set80211var(drv, IEEE80211_IOC_MLME, &mlme, sizeof(mlme)) < 0) |
| 1135 | return -1; |
| 1136 | return ret; |
| 1137 | } |
| 1138 | |
| 1139 | static int |
| 1140 | wpa_driver_bsd_scan(void *priv, struct wpa_driver_scan_params *params) |
| 1141 | { |
| 1142 | struct bsd_driver_data *drv = priv; |
| 1143 | #ifdef IEEE80211_IOC_SCAN_MAX_SSID |
| 1144 | struct ieee80211_scan_req sr; |
| 1145 | int i; |
| 1146 | #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ |
| 1147 | |
| 1148 | if (bsd_set_mediaopt(drv, IFM_OMASK, 0 /* STA */) < 0) { |
| 1149 | wpa_printf(MSG_ERROR, "%s: failed to set operation mode", |
| 1150 | __func__); |
| 1151 | return -1; |
| 1152 | } |
| 1153 | |
| 1154 | if (set80211param(drv, IEEE80211_IOC_ROAMING, |
| 1155 | IEEE80211_ROAMING_MANUAL) < 0) { |
| 1156 | wpa_printf(MSG_ERROR, "%s: failed to set " |
| 1157 | "wpa_supplicant-based roaming: %s", __func__, |
| 1158 | strerror(errno)); |
| 1159 | return -1; |
| 1160 | } |
| 1161 | |
| 1162 | if (wpa_driver_bsd_set_wpa(drv, 1) < 0) { |
| 1163 | wpa_printf(MSG_ERROR, "%s: failed to set wpa: %s", __func__, |
| 1164 | strerror(errno)); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | |
| 1168 | /* NB: interface must be marked UP to do a scan */ |
| 1169 | if (bsd_ctrl_iface(drv, 1) < 0) |
| 1170 | return -1; |
| 1171 | |
| 1172 | #ifdef IEEE80211_IOC_SCAN_MAX_SSID |
| 1173 | os_memset(&sr, 0, sizeof(sr)); |
| 1174 | sr.sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE | |
| 1175 | IEEE80211_IOC_SCAN_NOJOIN; |
| 1176 | sr.sr_duration = IEEE80211_IOC_SCAN_FOREVER; |
| 1177 | if (params->num_ssids > 0) { |
| 1178 | sr.sr_nssid = params->num_ssids; |
| 1179 | #if 0 |
| 1180 | /* Boundary check is done by upper layer */ |
| 1181 | if (sr.sr_nssid > IEEE80211_IOC_SCAN_MAX_SSID) |
| 1182 | sr.sr_nssid = IEEE80211_IOC_SCAN_MAX_SSID; |
| 1183 | #endif |
| 1184 | |
| 1185 | /* NB: check scan cache first */ |
| 1186 | sr.sr_flags |= IEEE80211_IOC_SCAN_CHECK; |
| 1187 | } |
| 1188 | for (i = 0; i < sr.sr_nssid; i++) { |
| 1189 | sr.sr_ssid[i].len = params->ssids[i].ssid_len; |
| 1190 | os_memcpy(sr.sr_ssid[i].ssid, params->ssids[i].ssid, |
| 1191 | sr.sr_ssid[i].len); |
| 1192 | } |
| 1193 | |
| 1194 | /* NB: net80211 delivers a scan complete event so no need to poll */ |
| 1195 | return set80211var(drv, IEEE80211_IOC_SCAN_REQ, &sr, sizeof(sr)); |
| 1196 | #else /* IEEE80211_IOC_SCAN_MAX_SSID */ |
| 1197 | /* set desired ssid before scan */ |
| 1198 | if (bsd_set_ssid(drv, params->ssids[0].ssid, |
| 1199 | params->ssids[0].ssid_len) < 0) |
| 1200 | return -1; |
| 1201 | |
| 1202 | /* NB: net80211 delivers a scan complete event so no need to poll */ |
| 1203 | return set80211param(drv, IEEE80211_IOC_SCAN_REQ, 0); |
| 1204 | #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ |
| 1205 | } |
| 1206 | |
| 1207 | static void |
| 1208 | wpa_driver_bsd_event_receive(int sock, void *ctx, void *sock_ctx) |
| 1209 | { |
| 1210 | struct bsd_driver_data *drv = sock_ctx; |
| 1211 | char *buf; |
| 1212 | struct if_announcemsghdr *ifan; |
| 1213 | struct if_msghdr *ifm; |
| 1214 | struct rt_msghdr *rtm; |
| 1215 | union wpa_event_data event; |
| 1216 | struct ieee80211_michael_event *mic; |
| 1217 | struct ieee80211_leave_event *leave; |
| 1218 | struct ieee80211_join_event *join; |
| 1219 | int n, len; |
| 1220 | |
| 1221 | len = rtbuf_len(); |
| 1222 | |
| 1223 | buf = os_malloc(len); |
| 1224 | if (buf == NULL) { |
| 1225 | wpa_printf(MSG_ERROR, "%s os_malloc() failed\n", __func__); |
| 1226 | return; |
| 1227 | } |
| 1228 | |
| 1229 | n = read(sock, buf, len); |
| 1230 | if (n < 0) { |
| 1231 | if (errno != EINTR && errno != EAGAIN) |
| 1232 | wpa_printf(MSG_ERROR, "%s read() failed: %s\n", |
| 1233 | __func__, strerror(errno)); |
| 1234 | os_free(buf); |
| 1235 | return; |
| 1236 | } |
| 1237 | |
| 1238 | rtm = (struct rt_msghdr *) buf; |
| 1239 | if (rtm->rtm_version != RTM_VERSION) { |
| 1240 | wpa_printf(MSG_DEBUG, "Invalid routing message version=%d", |
| 1241 | rtm->rtm_version); |
| 1242 | os_free(buf); |
| 1243 | return; |
| 1244 | } |
| 1245 | os_memset(&event, 0, sizeof(event)); |
| 1246 | switch (rtm->rtm_type) { |
| 1247 | case RTM_IFANNOUNCE: |
| 1248 | ifan = (struct if_announcemsghdr *) rtm; |
| 1249 | if (ifan->ifan_index != drv->ifindex) |
| 1250 | break; |
| 1251 | os_strlcpy(event.interface_status.ifname, drv->ifname, |
| 1252 | sizeof(event.interface_status.ifname)); |
| 1253 | switch (ifan->ifan_what) { |
| 1254 | case IFAN_DEPARTURE: |
| 1255 | event.interface_status.ievent = EVENT_INTERFACE_REMOVED; |
| 1256 | default: |
| 1257 | os_free(buf); |
| 1258 | return; |
| 1259 | } |
| 1260 | wpa_printf(MSG_DEBUG, "RTM_IFANNOUNCE: Interface '%s' %s", |
| 1261 | event.interface_status.ifname, |
| 1262 | ifan->ifan_what == IFAN_DEPARTURE ? |
| 1263 | "removed" : "added"); |
| 1264 | wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event); |
| 1265 | break; |
| 1266 | case RTM_IEEE80211: |
| 1267 | ifan = (struct if_announcemsghdr *) rtm; |
| 1268 | if (ifan->ifan_index != drv->ifindex) |
| 1269 | break; |
| 1270 | switch (ifan->ifan_what) { |
| 1271 | case RTM_IEEE80211_ASSOC: |
| 1272 | case RTM_IEEE80211_REASSOC: |
| 1273 | if (drv->is_ap) |
| 1274 | break; |
| 1275 | wpa_supplicant_event(ctx, EVENT_ASSOC, NULL); |
| 1276 | break; |
| 1277 | case RTM_IEEE80211_DISASSOC: |
| 1278 | if (drv->is_ap) |
| 1279 | break; |
| 1280 | wpa_supplicant_event(ctx, EVENT_DISASSOC, NULL); |
| 1281 | break; |
| 1282 | case RTM_IEEE80211_SCAN: |
| 1283 | if (drv->is_ap) |
| 1284 | break; |
| 1285 | wpa_supplicant_event(ctx, EVENT_SCAN_RESULTS, NULL); |
| 1286 | break; |
| 1287 | case RTM_IEEE80211_LEAVE: |
| 1288 | leave = (struct ieee80211_leave_event *) &ifan[1]; |
| 1289 | drv_event_disassoc(ctx, leave->iev_addr); |
| 1290 | break; |
| 1291 | case RTM_IEEE80211_JOIN: |
| 1292 | #ifdef RTM_IEEE80211_REJOIN |
| 1293 | case RTM_IEEE80211_REJOIN: |
| 1294 | #endif |
| 1295 | join = (struct ieee80211_join_event *) &ifan[1]; |
| 1296 | bsd_new_sta(drv, ctx, join->iev_addr); |
| 1297 | break; |
| 1298 | case RTM_IEEE80211_REPLAY: |
| 1299 | /* ignore */ |
| 1300 | break; |
| 1301 | case RTM_IEEE80211_MICHAEL: |
| 1302 | mic = (struct ieee80211_michael_event *) &ifan[1]; |
| 1303 | wpa_printf(MSG_DEBUG, |
| 1304 | "Michael MIC failure wireless event: " |
| 1305 | "keyix=%u src_addr=" MACSTR, mic->iev_keyix, |
| 1306 | MAC2STR(mic->iev_src)); |
| 1307 | |
| 1308 | os_memset(&event, 0, sizeof(event)); |
| 1309 | event.michael_mic_failure.unicast = |
| 1310 | !IEEE80211_IS_MULTICAST(mic->iev_dst); |
| 1311 | wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, |
| 1312 | &event); |
| 1313 | break; |
| 1314 | } |
| 1315 | break; |
| 1316 | case RTM_IFINFO: |
| 1317 | ifm = (struct if_msghdr *) rtm; |
| 1318 | if (ifm->ifm_index != drv->ifindex) |
| 1319 | break; |
| 1320 | if ((rtm->rtm_flags & RTF_UP) == 0) { |
| 1321 | os_strlcpy(event.interface_status.ifname, drv->ifname, |
| 1322 | sizeof(event.interface_status.ifname)); |
| 1323 | event.interface_status.ievent = EVENT_INTERFACE_REMOVED; |
| 1324 | wpa_printf(MSG_DEBUG, "RTM_IFINFO: Interface '%s' DOWN", |
| 1325 | event.interface_status.ifname); |
| 1326 | wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event); |
| 1327 | } |
| 1328 | break; |
| 1329 | } |
| 1330 | os_free(buf); |
| 1331 | } |
| 1332 | |
| 1333 | static void |
| 1334 | wpa_driver_bsd_add_scan_entry(struct wpa_scan_results *res, |
| 1335 | struct ieee80211req_scan_result *sr) |
| 1336 | { |
| 1337 | struct wpa_scan_res *result, **tmp; |
| 1338 | size_t extra_len; |
| 1339 | u8 *pos; |
| 1340 | |
| 1341 | extra_len = 2 + sr->isr_ssid_len; |
| 1342 | extra_len += 2 + sr->isr_nrates; |
| 1343 | extra_len += 3; /* ERP IE */ |
| 1344 | extra_len += sr->isr_ie_len; |
| 1345 | |
| 1346 | result = os_zalloc(sizeof(*result) + extra_len); |
| 1347 | if (result == NULL) |
| 1348 | return; |
| 1349 | os_memcpy(result->bssid, sr->isr_bssid, ETH_ALEN); |
| 1350 | result->freq = sr->isr_freq; |
| 1351 | result->beacon_int = sr->isr_intval; |
| 1352 | result->caps = sr->isr_capinfo; |
| 1353 | result->qual = sr->isr_rssi; |
| 1354 | result->noise = sr->isr_noise; |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 1355 | /* |
| 1356 | * the rssi value reported by the kernel is in 0.5dB steps relative to |
| 1357 | * the reported noise floor. see ieee80211_node.h for details. |
| 1358 | */ |
| 1359 | result->level = sr->isr_rssi / 2 + sr->isr_noise; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1360 | |
| 1361 | pos = (u8 *)(result + 1); |
| 1362 | |
| 1363 | *pos++ = WLAN_EID_SSID; |
| 1364 | *pos++ = sr->isr_ssid_len; |
| 1365 | os_memcpy(pos, sr + 1, sr->isr_ssid_len); |
| 1366 | pos += sr->isr_ssid_len; |
| 1367 | |
| 1368 | /* |
| 1369 | * Deal all rates as supported rate. |
| 1370 | * Because net80211 doesn't report extended supported rate or not. |
| 1371 | */ |
| 1372 | *pos++ = WLAN_EID_SUPP_RATES; |
| 1373 | *pos++ = sr->isr_nrates; |
| 1374 | os_memcpy(pos, sr->isr_rates, sr->isr_nrates); |
| 1375 | pos += sr->isr_nrates; |
| 1376 | |
| 1377 | *pos++ = WLAN_EID_ERP_INFO; |
| 1378 | *pos++ = 1; |
| 1379 | *pos++ = sr->isr_erp; |
| 1380 | |
| 1381 | os_memcpy(pos, (u8 *)(sr + 1) + sr->isr_ssid_len, sr->isr_ie_len); |
| 1382 | pos += sr->isr_ie_len; |
| 1383 | |
| 1384 | result->ie_len = pos - (u8 *)(result + 1); |
| 1385 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1386 | tmp = os_realloc_array(res->res, res->num + 1, |
| 1387 | sizeof(struct wpa_scan_res *)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1388 | if (tmp == NULL) { |
| 1389 | os_free(result); |
| 1390 | return; |
| 1391 | } |
| 1392 | tmp[res->num++] = result; |
| 1393 | res->res = tmp; |
| 1394 | } |
| 1395 | |
| 1396 | struct wpa_scan_results * |
| 1397 | wpa_driver_bsd_get_scan_results2(void *priv) |
| 1398 | { |
| 1399 | struct ieee80211req_scan_result *sr; |
| 1400 | struct wpa_scan_results *res; |
| 1401 | int len, rest; |
| 1402 | uint8_t buf[24*1024], *pos; |
| 1403 | |
| 1404 | len = get80211var(priv, IEEE80211_IOC_SCAN_RESULTS, buf, 24*1024); |
| 1405 | if (len < 0) |
| 1406 | return NULL; |
| 1407 | |
| 1408 | res = os_zalloc(sizeof(*res)); |
| 1409 | if (res == NULL) |
| 1410 | return NULL; |
| 1411 | |
| 1412 | pos = buf; |
| 1413 | rest = len; |
| 1414 | while (rest >= sizeof(struct ieee80211req_scan_result)) { |
| 1415 | sr = (struct ieee80211req_scan_result *)pos; |
| 1416 | wpa_driver_bsd_add_scan_entry(res, sr); |
| 1417 | pos += sr->isr_len; |
| 1418 | rest -= sr->isr_len; |
| 1419 | } |
| 1420 | |
| 1421 | wpa_printf(MSG_DEBUG, "Received %d bytes of scan results (%lu BSSes)", |
| 1422 | len, (unsigned long)res->num); |
| 1423 | |
| 1424 | return res; |
| 1425 | } |
| 1426 | |
| 1427 | static int wpa_driver_bsd_capa(struct bsd_driver_data *drv) |
| 1428 | { |
| 1429 | #ifdef IEEE80211_IOC_DEVCAPS |
| 1430 | /* kernel definitions copied from net80211/ieee80211_var.h */ |
| 1431 | #define IEEE80211_CIPHER_WEP 0 |
| 1432 | #define IEEE80211_CIPHER_TKIP 1 |
| 1433 | #define IEEE80211_CIPHER_AES_CCM 3 |
| 1434 | #define IEEE80211_CRYPTO_WEP (1<<IEEE80211_CIPHER_WEP) |
| 1435 | #define IEEE80211_CRYPTO_TKIP (1<<IEEE80211_CIPHER_TKIP) |
| 1436 | #define IEEE80211_CRYPTO_AES_CCM (1<<IEEE80211_CIPHER_AES_CCM) |
| 1437 | #define IEEE80211_C_HOSTAP 0x00000400 /* CAPABILITY: HOSTAP avail */ |
| 1438 | #define IEEE80211_C_WPA1 0x00800000 /* CAPABILITY: WPA1 avail */ |
| 1439 | #define IEEE80211_C_WPA2 0x01000000 /* CAPABILITY: WPA2 avail */ |
| 1440 | struct ieee80211_devcaps_req devcaps; |
| 1441 | |
| 1442 | if (get80211var(drv, IEEE80211_IOC_DEVCAPS, &devcaps, |
| 1443 | sizeof(devcaps)) < 0) { |
| 1444 | wpa_printf(MSG_ERROR, "failed to IEEE80211_IOC_DEVCAPS: %s", |
| 1445 | strerror(errno)); |
| 1446 | return -1; |
| 1447 | } |
| 1448 | |
| 1449 | wpa_printf(MSG_DEBUG, "%s: drivercaps=0x%08x,cryptocaps=0x%08x", |
| 1450 | __func__, devcaps.dc_drivercaps, devcaps.dc_cryptocaps); |
| 1451 | |
| 1452 | if (devcaps.dc_drivercaps & IEEE80211_C_WPA1) |
| 1453 | drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | |
| 1454 | WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK; |
| 1455 | if (devcaps.dc_drivercaps & IEEE80211_C_WPA2) |
| 1456 | drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | |
| 1457 | WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; |
| 1458 | |
| 1459 | if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_WEP) |
| 1460 | drv->capa.enc |= WPA_DRIVER_CAPA_ENC_WEP40 | |
| 1461 | WPA_DRIVER_CAPA_ENC_WEP104; |
| 1462 | if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_TKIP) |
| 1463 | drv->capa.enc |= WPA_DRIVER_CAPA_ENC_TKIP; |
| 1464 | if (devcaps.dc_cryptocaps & IEEE80211_CRYPTO_AES_CCM) |
| 1465 | drv->capa.enc |= WPA_DRIVER_CAPA_ENC_CCMP; |
| 1466 | |
| 1467 | if (devcaps.dc_drivercaps & IEEE80211_C_HOSTAP) |
| 1468 | drv->capa.flags |= WPA_DRIVER_FLAGS_AP; |
| 1469 | #undef IEEE80211_CIPHER_WEP |
| 1470 | #undef IEEE80211_CIPHER_TKIP |
| 1471 | #undef IEEE80211_CIPHER_AES_CCM |
| 1472 | #undef IEEE80211_CRYPTO_WEP |
| 1473 | #undef IEEE80211_CRYPTO_TKIP |
| 1474 | #undef IEEE80211_CRYPTO_AES_CCM |
| 1475 | #undef IEEE80211_C_HOSTAP |
| 1476 | #undef IEEE80211_C_WPA1 |
| 1477 | #undef IEEE80211_C_WPA2 |
| 1478 | #else /* IEEE80211_IOC_DEVCAPS */ |
| 1479 | /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */ |
| 1480 | drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | |
| 1481 | WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK | |
| 1482 | WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | |
| 1483 | WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; |
| 1484 | drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 | |
| 1485 | WPA_DRIVER_CAPA_ENC_WEP104 | |
| 1486 | WPA_DRIVER_CAPA_ENC_TKIP | |
| 1487 | WPA_DRIVER_CAPA_ENC_CCMP; |
| 1488 | drv->capa.flags |= WPA_DRIVER_FLAGS_AP; |
| 1489 | #endif /* IEEE80211_IOC_DEVCAPS */ |
| 1490 | #ifdef IEEE80211_IOC_SCAN_MAX_SSID |
| 1491 | drv->capa.max_scan_ssids = IEEE80211_IOC_SCAN_MAX_SSID; |
| 1492 | #else /* IEEE80211_IOC_SCAN_MAX_SSID */ |
| 1493 | drv->capa.max_scan_ssids = 1; |
| 1494 | #endif /* IEEE80211_IOC_SCAN_MAX_SSID */ |
| 1495 | drv->capa.auth = WPA_DRIVER_AUTH_OPEN | |
| 1496 | WPA_DRIVER_AUTH_SHARED | |
| 1497 | WPA_DRIVER_AUTH_LEAP; |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | static void * |
| 1502 | wpa_driver_bsd_init(void *ctx, const char *ifname) |
| 1503 | { |
| 1504 | #define GETPARAM(drv, param, v) \ |
| 1505 | (((v) = get80211param(drv, param)) != -1) |
| 1506 | struct bsd_driver_data *drv; |
| 1507 | |
| 1508 | drv = os_zalloc(sizeof(*drv)); |
| 1509 | if (drv == NULL) |
| 1510 | return NULL; |
| 1511 | /* |
| 1512 | * NB: We require the interface name be mappable to an index. |
| 1513 | * This implies we do not support having wpa_supplicant |
| 1514 | * wait for an interface to appear. This seems ok; that |
| 1515 | * doesn't belong here; it's really the job of devd. |
| 1516 | */ |
| 1517 | drv->ifindex = if_nametoindex(ifname); |
| 1518 | if (drv->ifindex == 0) { |
| 1519 | wpa_printf(MSG_DEBUG, "%s: interface %s does not exist", |
| 1520 | __func__, ifname); |
| 1521 | goto fail1; |
| 1522 | } |
| 1523 | drv->sock = socket(PF_INET, SOCK_DGRAM, 0); |
| 1524 | if (drv->sock < 0) |
| 1525 | goto fail1; |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 1526 | |
| 1527 | os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname)); |
| 1528 | /* Down interface during setup. */ |
| 1529 | if (bsd_ctrl_iface(drv, 0) < 0) |
| 1530 | goto fail; |
| 1531 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1532 | drv->route = socket(PF_ROUTE, SOCK_RAW, 0); |
| 1533 | if (drv->route < 0) |
| 1534 | goto fail; |
| 1535 | eloop_register_read_sock(drv->route, |
| 1536 | wpa_driver_bsd_event_receive, ctx, drv); |
| 1537 | |
| 1538 | drv->ctx = ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1539 | |
| 1540 | if (!GETPARAM(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming)) { |
| 1541 | wpa_printf(MSG_DEBUG, "%s: failed to get roaming state: %s", |
| 1542 | __func__, strerror(errno)); |
| 1543 | goto fail; |
| 1544 | } |
| 1545 | if (!GETPARAM(drv, IEEE80211_IOC_PRIVACY, drv->prev_privacy)) { |
| 1546 | wpa_printf(MSG_DEBUG, "%s: failed to get privacy state: %s", |
| 1547 | __func__, strerror(errno)); |
| 1548 | goto fail; |
| 1549 | } |
| 1550 | if (!GETPARAM(drv, IEEE80211_IOC_WPA, drv->prev_wpa)) { |
| 1551 | wpa_printf(MSG_DEBUG, "%s: failed to get wpa state: %s", |
| 1552 | __func__, strerror(errno)); |
| 1553 | goto fail; |
| 1554 | } |
| 1555 | |
| 1556 | if (wpa_driver_bsd_capa(drv)) |
| 1557 | goto fail; |
| 1558 | |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 1559 | drv->opmode = get80211opmode(drv); |
| 1560 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1561 | return drv; |
| 1562 | fail: |
| 1563 | close(drv->sock); |
| 1564 | fail1: |
| 1565 | os_free(drv); |
| 1566 | return NULL; |
| 1567 | #undef GETPARAM |
| 1568 | } |
| 1569 | |
| 1570 | static void |
| 1571 | wpa_driver_bsd_deinit(void *priv) |
| 1572 | { |
| 1573 | struct bsd_driver_data *drv = priv; |
| 1574 | |
| 1575 | wpa_driver_bsd_set_wpa(drv, 0); |
| 1576 | eloop_unregister_read_sock(drv->route); |
| 1577 | |
| 1578 | /* NB: mark interface down */ |
| 1579 | bsd_ctrl_iface(drv, 0); |
| 1580 | |
| 1581 | wpa_driver_bsd_set_wpa_internal(drv, drv->prev_wpa, drv->prev_privacy); |
| 1582 | if (set80211param(drv, IEEE80211_IOC_ROAMING, drv->prev_roaming) < 0) |
| 1583 | wpa_printf(MSG_DEBUG, "%s: failed to restore roaming state", |
| 1584 | __func__); |
| 1585 | |
| 1586 | if (drv->sock_xmit != NULL) |
| 1587 | l2_packet_deinit(drv->sock_xmit); |
| 1588 | (void) close(drv->route); /* ioctl socket */ |
| 1589 | (void) close(drv->sock); /* event socket */ |
| 1590 | os_free(drv); |
| 1591 | } |
| 1592 | |
| 1593 | static int |
| 1594 | wpa_driver_bsd_get_capa(void *priv, struct wpa_driver_capa *capa) |
| 1595 | { |
| 1596 | struct bsd_driver_data *drv = priv; |
| 1597 | |
| 1598 | os_memcpy(capa, &drv->capa, sizeof(*capa)); |
| 1599 | return 0; |
| 1600 | } |
| 1601 | #endif /* HOSTAPD */ |
| 1602 | |
| 1603 | |
| 1604 | const struct wpa_driver_ops wpa_driver_bsd_ops = { |
| 1605 | .name = "bsd", |
| 1606 | .desc = "BSD 802.11 support", |
| 1607 | #ifdef HOSTAPD |
| 1608 | .hapd_init = bsd_init, |
| 1609 | .hapd_deinit = bsd_deinit, |
| 1610 | .set_privacy = bsd_set_privacy, |
| 1611 | .get_seqnum = bsd_get_seqnum, |
| 1612 | .flush = bsd_flush, |
| 1613 | .read_sta_data = bsd_read_sta_driver_data, |
| 1614 | .sta_disassoc = bsd_sta_disassoc, |
| 1615 | .sta_deauth = bsd_sta_deauth, |
Dmitry Shmidt | 5393a0f | 2013-08-08 11:23:34 -0700 | [diff] [blame] | 1616 | .sta_set_flags = bsd_set_sta_authorized, |
| 1617 | .commit = bsd_commit, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1618 | #else /* HOSTAPD */ |
| 1619 | .init = wpa_driver_bsd_init, |
| 1620 | .deinit = wpa_driver_bsd_deinit, |
| 1621 | .get_bssid = wpa_driver_bsd_get_bssid, |
| 1622 | .get_ssid = wpa_driver_bsd_get_ssid, |
| 1623 | .set_countermeasures = wpa_driver_bsd_set_countermeasures, |
| 1624 | .scan2 = wpa_driver_bsd_scan, |
| 1625 | .get_scan_results2 = wpa_driver_bsd_get_scan_results2, |
| 1626 | .deauthenticate = wpa_driver_bsd_deauthenticate, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1627 | .associate = wpa_driver_bsd_associate, |
| 1628 | .get_capa = wpa_driver_bsd_get_capa, |
| 1629 | #endif /* HOSTAPD */ |
| 1630 | .set_freq = bsd_set_freq, |
| 1631 | .set_key = bsd_set_key, |
| 1632 | .set_ieee8021x = bsd_set_ieee8021x, |
| 1633 | .hapd_set_ssid = bsd_set_ssid, |
| 1634 | .hapd_get_ssid = bsd_get_ssid, |
| 1635 | .hapd_send_eapol = bsd_send_eapol, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1636 | .set_generic_elem = bsd_set_opt_ie, |
| 1637 | }; |