1 /* $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org> 6 * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 #include <sys/cdefs.h> 22 #include "opt_wlan.h" 23 24 #include <sys/param.h> 25 #include <sys/lock.h> 26 #include <sys/mutex.h> 27 #include <sys/mbuf.h> 28 #include <sys/kernel.h> 29 #include <sys/socket.h> 30 #include <sys/systm.h> 31 #include <sys/malloc.h> 32 #include <sys/queue.h> 33 #include <sys/taskqueue.h> 34 #include <sys/bus.h> 35 #include <sys/endian.h> 36 37 #include <net/if.h> 38 #include <net/ethernet.h> 39 #include <net/if_media.h> 40 41 #include <net80211/ieee80211_var.h> 42 #include <net80211/ieee80211_radiotap.h> 43 44 #include <dev/rtwn/if_rtwnreg.h> 45 #include <dev/rtwn/if_rtwnvar.h> 46 47 #include <dev/rtwn/if_rtwn_cam.h> 48 #include <dev/rtwn/if_rtwn_debug.h> 49 #include <dev/rtwn/if_rtwn_task.h> 50 51 #include <dev/rtwn/rtl8192c/r92c_reg.h> 52 53 void 54 rtwn_init_cam(struct rtwn_softc *sc) 55 { 56 /* Invalidate all CAM entries. */ 57 rtwn_write_4(sc, R92C_CAMCMD, 58 R92C_CAMCMD_POLLING | R92C_CAMCMD_CLR); 59 } 60 61 static int 62 rtwn_cam_write(struct rtwn_softc *sc, uint32_t addr, uint32_t data) 63 { 64 int error; 65 66 error = rtwn_write_4(sc, R92C_CAMWRITE, data); 67 if (error != 0) 68 return (error); 69 error = rtwn_write_4(sc, R92C_CAMCMD, 70 R92C_CAMCMD_POLLING | R92C_CAMCMD_WRITE | 71 SM(R92C_CAMCMD_ADDR, addr)); 72 73 return (error); 74 } 75 76 void 77 rtwn_init_seccfg(struct rtwn_softc *sc) 78 { 79 uint16_t seccfg; 80 81 /* Select decryption / encryption flags. */ 82 seccfg = 0; 83 switch (sc->sc_hwcrypto) { 84 case RTWN_CRYPTO_SW: 85 break; /* nothing to do */ 86 case RTWN_CRYPTO_PAIR: 87 /* NB: TXUCKEY_DEF / RXUCKEY_DEF are required for RTL8192C */ 88 seccfg = R92C_SECCFG_TXUCKEY_DEF | R92C_SECCFG_RXUCKEY_DEF | 89 R92C_SECCFG_TXENC_ENA | R92C_SECCFG_RXDEC_ENA | 90 R92C_SECCFG_MC_SRCH_DIS; 91 break; 92 case RTWN_CRYPTO_FULL: 93 seccfg = R92C_SECCFG_TXUCKEY_DEF | R92C_SECCFG_RXUCKEY_DEF | 94 R92C_SECCFG_TXENC_ENA | R92C_SECCFG_RXDEC_ENA | 95 R92C_SECCFG_TXBCKEY_DEF | R92C_SECCFG_RXBCKEY_DEF; 96 break; 97 default: 98 KASSERT(0, ("%s: case %d was not handled\n", __func__, 99 sc->sc_hwcrypto)); 100 break; 101 } 102 103 RTWN_DPRINTF(sc, RTWN_DEBUG_KEY, "%s: seccfg %04X, hwcrypto %d\n", 104 __func__, seccfg, sc->sc_hwcrypto); 105 106 rtwn_write_2(sc, R92C_SECCFG, seccfg); 107 } 108 109 int 110 rtwn_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k, 111 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix) 112 { 113 struct rtwn_softc *sc = vap->iv_ic->ic_softc; 114 int i, start; 115 116 if (ieee80211_is_key_global(vap, k)) { 117 *keyix = ieee80211_crypto_get_key_wepidx(vap, k); 118 if (sc->sc_hwcrypto != RTWN_CRYPTO_FULL) 119 k->wk_flags |= IEEE80211_KEY_SWCRYPT; 120 else { 121 RTWN_LOCK(sc); 122 if (isset(sc->keys_bmap, *keyix)) { 123 device_printf(sc->sc_dev, 124 "%s: group key slot %d is already used!\n", 125 __func__, *keyix); 126 /* XXX recover? */ 127 RTWN_UNLOCK(sc); 128 return (0); 129 } 130 131 setbit(sc->keys_bmap, *keyix); 132 RTWN_UNLOCK(sc); 133 } 134 135 goto end; 136 } 137 138 start = sc->cam_entry_limit; 139 switch (sc->sc_hwcrypto) { 140 case RTWN_CRYPTO_SW: 141 k->wk_flags |= IEEE80211_KEY_SWCRYPT; 142 *keyix = 0; 143 goto end; 144 case RTWN_CRYPTO_PAIR: 145 /* all slots for pairwise keys. */ 146 start = 0; 147 RTWN_LOCK(sc); 148 if (sc->sc_flags & RTWN_FLAG_CAM_FIXED) 149 start = 4; 150 RTWN_UNLOCK(sc); 151 break; 152 case RTWN_CRYPTO_FULL: 153 /* first 4 - for group keys, others for pairwise. */ 154 start = 4; 155 break; 156 default: 157 KASSERT(0, ("%s: case %d was not handled!\n", 158 __func__, sc->sc_hwcrypto)); 159 break; 160 } 161 162 RTWN_LOCK(sc); 163 for (i = start; i < sc->cam_entry_limit; i++) { 164 if (isclr(sc->keys_bmap, i)) { 165 setbit(sc->keys_bmap, i); 166 *keyix = i; 167 break; 168 } 169 } 170 RTWN_UNLOCK(sc); 171 if (i == sc->cam_entry_limit) { 172 /* XXX check and remove keys with the same MAC address */ 173 k->wk_flags |= IEEE80211_KEY_SWCRYPT; 174 *keyix = 0; 175 } 176 177 end: 178 *rxkeyix = *keyix; 179 return (1); 180 } 181 182 static int 183 rtwn_key_set_cb0(struct rtwn_softc *sc, const struct ieee80211_key *k) 184 { 185 uint8_t algo, keyid; 186 int i, error; 187 188 if (sc->sc_hwcrypto == RTWN_CRYPTO_FULL && 189 k->wk_keyix < IEEE80211_WEP_NKID) 190 keyid = k->wk_keyix; 191 else 192 keyid = 0; 193 194 /* Map net80211 cipher to HW crypto algorithm. */ 195 switch (k->wk_cipher->ic_cipher) { 196 case IEEE80211_CIPHER_WEP: 197 if (k->wk_keylen < 8) 198 algo = R92C_CAM_ALGO_WEP40; 199 else 200 algo = R92C_CAM_ALGO_WEP104; 201 break; 202 case IEEE80211_CIPHER_TKIP: 203 algo = R92C_CAM_ALGO_TKIP; 204 break; 205 case IEEE80211_CIPHER_AES_CCM: 206 algo = R92C_CAM_ALGO_AES; 207 break; 208 default: 209 device_printf(sc->sc_dev, "%s: unknown cipher %u\n", 210 __func__, k->wk_cipher->ic_cipher); 211 return (EINVAL); 212 } 213 214 RTWN_DPRINTF(sc, RTWN_DEBUG_KEY, 215 "%s: keyix %u, keyid %u, algo %u/%u, flags %04X, len %u, " 216 "macaddr %s\n", __func__, k->wk_keyix, keyid, 217 k->wk_cipher->ic_cipher, algo, k->wk_flags, k->wk_keylen, 218 ether_sprintf(k->wk_macaddr)); 219 220 /* Clear high bits. */ 221 rtwn_cam_write(sc, R92C_CAM_CTL6(k->wk_keyix), 0); 222 rtwn_cam_write(sc, R92C_CAM_CTL7(k->wk_keyix), 0); 223 224 /* Write key. */ 225 for (i = 0; i < 4; i++) { 226 error = rtwn_cam_write(sc, R92C_CAM_KEY(k->wk_keyix, i), 227 le32dec(&k->wk_key[i * 4])); 228 if (error != 0) 229 goto fail; 230 } 231 232 /* Write CTL0 last since that will validate the CAM entry. */ 233 error = rtwn_cam_write(sc, R92C_CAM_CTL1(k->wk_keyix), 234 le32dec(&k->wk_macaddr[2])); 235 if (error != 0) 236 goto fail; 237 error = rtwn_cam_write(sc, R92C_CAM_CTL0(k->wk_keyix), 238 SM(R92C_CAM_ALGO, algo) | 239 SM(R92C_CAM_KEYID, keyid) | 240 SM(R92C_CAM_MACLO, le16dec(&k->wk_macaddr[0])) | 241 R92C_CAM_VALID); 242 if (error != 0) 243 goto fail; 244 245 return (0); 246 247 fail: 248 device_printf(sc->sc_dev, "%s fails, error %d\n", __func__, error); 249 return (error); 250 } 251 252 static void 253 rtwn_key_set_cb(struct rtwn_softc *sc, union sec_param *data) 254 { 255 const struct ieee80211_key *k = &data->key; 256 257 (void) rtwn_key_set_cb0(sc, k); 258 } 259 260 int 261 rtwn_init_static_keys(struct rtwn_softc *sc, struct rtwn_vap *rvp) 262 { 263 int i, error; 264 265 if (sc->sc_hwcrypto != RTWN_CRYPTO_FULL) 266 return (0); /* nothing to do */ 267 268 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 269 const struct ieee80211_key *k = rvp->keys[i]; 270 if (k != NULL) { 271 error = rtwn_key_set_cb0(sc, k); 272 if (error != 0) 273 return (error); 274 } 275 } 276 277 return (0); 278 } 279 280 static void 281 rtwn_key_del_cb(struct rtwn_softc *sc, union sec_param *data) 282 { 283 struct ieee80211_key *k = &data->key; 284 int i; 285 286 RTWN_DPRINTF(sc, RTWN_DEBUG_KEY, 287 "%s: keyix %u, flags %04X, macaddr %s\n", __func__, 288 k->wk_keyix, k->wk_flags, ether_sprintf(k->wk_macaddr)); 289 290 rtwn_cam_write(sc, R92C_CAM_CTL0(k->wk_keyix), 0); 291 rtwn_cam_write(sc, R92C_CAM_CTL1(k->wk_keyix), 0); 292 293 /* Clear key. */ 294 for (i = 0; i < 4; i++) 295 rtwn_cam_write(sc, R92C_CAM_KEY(k->wk_keyix, i), 0); 296 clrbit(sc->keys_bmap, k->wk_keyix); 297 } 298 299 static int 300 rtwn_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k, 301 int set) 302 { 303 struct rtwn_softc *sc = vap->iv_ic->ic_softc; 304 305 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) { 306 /* Not for us. */ 307 return (1); 308 } 309 310 if (ieee80211_is_key_global(vap, k)) { 311 if (sc->sc_hwcrypto == RTWN_CRYPTO_FULL) { 312 struct rtwn_vap *rvp = RTWN_VAP(vap); 313 314 RTWN_LOCK(sc); 315 rvp->keys[k->wk_keyix] = (set ? k : NULL); 316 if ((sc->sc_flags & RTWN_RUNNING) == 0) { 317 if (!set) 318 clrbit(sc->keys_bmap, k->wk_keyix); 319 RTWN_UNLOCK(sc); 320 return (1); 321 } 322 RTWN_UNLOCK(sc); 323 } 324 } 325 326 return (!rtwn_cmd_sleepable(sc, k, sizeof(*k), 327 set ? rtwn_key_set_cb : rtwn_key_del_cb)); 328 } 329 330 int 331 rtwn_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k) 332 { 333 return (rtwn_process_key(vap, k, 1)); 334 } 335 336 int 337 rtwn_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k) 338 { 339 return (rtwn_process_key(vap, k, 0)); 340 } 341