1 /* 2 * mac80211 configuration hooks for cfg80211 3 * 4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net> 5 * 6 * This file is GPLv2 as found in COPYING. 7 */ 8 9 #include <linux/ieee80211.h> 10 #include <linux/nl80211.h> 11 #include <linux/rtnetlink.h> 12 #include <net/net_namespace.h> 13 #include <linux/rcupdate.h> 14 #include <net/cfg80211.h> 15 #include "ieee80211_i.h" 16 #include "cfg.h" 17 #include "ieee80211_rate.h" 18 19 static enum ieee80211_if_types 20 nl80211_type_to_mac80211_type(enum nl80211_iftype type) 21 { 22 switch (type) { 23 case NL80211_IFTYPE_UNSPECIFIED: 24 return IEEE80211_IF_TYPE_STA; 25 case NL80211_IFTYPE_ADHOC: 26 return IEEE80211_IF_TYPE_IBSS; 27 case NL80211_IFTYPE_STATION: 28 return IEEE80211_IF_TYPE_STA; 29 case NL80211_IFTYPE_MONITOR: 30 return IEEE80211_IF_TYPE_MNTR; 31 default: 32 return IEEE80211_IF_TYPE_INVALID; 33 } 34 } 35 36 static int ieee80211_add_iface(struct wiphy *wiphy, char *name, 37 enum nl80211_iftype type, u32 *flags, 38 struct vif_params *params) 39 { 40 struct ieee80211_local *local = wiphy_priv(wiphy); 41 enum ieee80211_if_types itype; 42 struct net_device *dev; 43 struct ieee80211_sub_if_data *sdata; 44 int err; 45 46 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED)) 47 return -ENODEV; 48 49 itype = nl80211_type_to_mac80211_type(type); 50 if (itype == IEEE80211_IF_TYPE_INVALID) 51 return -EINVAL; 52 53 err = ieee80211_if_add(local->mdev, name, &dev, itype); 54 if (err || itype != IEEE80211_IF_TYPE_MNTR || !flags) 55 return err; 56 57 sdata = IEEE80211_DEV_TO_SUB_IF(dev); 58 sdata->u.mntr_flags = *flags; 59 return 0; 60 } 61 62 static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex) 63 { 64 struct ieee80211_local *local = wiphy_priv(wiphy); 65 struct net_device *dev; 66 char *name; 67 68 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED)) 69 return -ENODEV; 70 71 /* we're under RTNL */ 72 dev = __dev_get_by_index(&init_net, ifindex); 73 if (!dev) 74 return 0; 75 76 name = dev->name; 77 78 return ieee80211_if_remove(local->mdev, name, -1); 79 } 80 81 static int ieee80211_change_iface(struct wiphy *wiphy, int ifindex, 82 enum nl80211_iftype type, u32 *flags, 83 struct vif_params *params) 84 { 85 struct ieee80211_local *local = wiphy_priv(wiphy); 86 struct net_device *dev; 87 enum ieee80211_if_types itype; 88 struct ieee80211_sub_if_data *sdata; 89 90 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED)) 91 return -ENODEV; 92 93 /* we're under RTNL */ 94 dev = __dev_get_by_index(&init_net, ifindex); 95 if (!dev) 96 return -ENODEV; 97 98 if (netif_running(dev)) 99 return -EBUSY; 100 101 itype = nl80211_type_to_mac80211_type(type); 102 if (itype == IEEE80211_IF_TYPE_INVALID) 103 return -EINVAL; 104 105 sdata = IEEE80211_DEV_TO_SUB_IF(dev); 106 107 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN) 108 return -EOPNOTSUPP; 109 110 ieee80211_if_reinit(dev); 111 ieee80211_if_set_type(dev, itype); 112 113 if (sdata->vif.type != IEEE80211_IF_TYPE_MNTR || !flags) 114 return 0; 115 116 sdata->u.mntr_flags = *flags; 117 return 0; 118 } 119 120 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev, 121 u8 key_idx, u8 *mac_addr, 122 struct key_params *params) 123 { 124 struct ieee80211_sub_if_data *sdata; 125 struct sta_info *sta = NULL; 126 enum ieee80211_key_alg alg; 127 int ret; 128 struct ieee80211_key *key; 129 130 sdata = IEEE80211_DEV_TO_SUB_IF(dev); 131 132 switch (params->cipher) { 133 case WLAN_CIPHER_SUITE_WEP40: 134 case WLAN_CIPHER_SUITE_WEP104: 135 alg = ALG_WEP; 136 break; 137 case WLAN_CIPHER_SUITE_TKIP: 138 alg = ALG_TKIP; 139 break; 140 case WLAN_CIPHER_SUITE_CCMP: 141 alg = ALG_CCMP; 142 break; 143 default: 144 return -EINVAL; 145 } 146 147 key = ieee80211_key_alloc(alg, key_idx, params->key_len, params->key); 148 if (!key) 149 return -ENOMEM; 150 151 if (mac_addr) { 152 sta = sta_info_get(sdata->local, mac_addr); 153 if (!sta) { 154 ieee80211_key_free(key); 155 return -ENOENT; 156 } 157 } 158 159 ieee80211_key_link(key, sdata, sta); 160 161 ret = 0; 162 163 if (sta) 164 sta_info_put(sta); 165 166 return ret; 167 } 168 169 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev, 170 u8 key_idx, u8 *mac_addr) 171 { 172 struct ieee80211_sub_if_data *sdata; 173 struct sta_info *sta; 174 int ret; 175 struct ieee80211_key *key; 176 177 sdata = IEEE80211_DEV_TO_SUB_IF(dev); 178 179 if (mac_addr) { 180 sta = sta_info_get(sdata->local, mac_addr); 181 if (!sta) 182 return -ENOENT; 183 184 ret = 0; 185 if (sta->key) { 186 key = sta->key; 187 ieee80211_key_free(key); 188 WARN_ON(sta->key); 189 } else 190 ret = -ENOENT; 191 192 sta_info_put(sta); 193 return ret; 194 } 195 196 if (!sdata->keys[key_idx]) 197 return -ENOENT; 198 199 key = sdata->keys[key_idx]; 200 ieee80211_key_free(key); 201 WARN_ON(sdata->keys[key_idx]); 202 203 return 0; 204 } 205 206 static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev, 207 u8 key_idx, u8 *mac_addr, void *cookie, 208 void (*callback)(void *cookie, 209 struct key_params *params)) 210 { 211 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 212 struct sta_info *sta = NULL; 213 u8 seq[6] = {0}; 214 struct key_params params; 215 struct ieee80211_key *key; 216 u32 iv32; 217 u16 iv16; 218 int err = -ENOENT; 219 220 if (mac_addr) { 221 sta = sta_info_get(sdata->local, mac_addr); 222 if (!sta) 223 goto out; 224 225 key = sta->key; 226 } else 227 key = sdata->keys[key_idx]; 228 229 if (!key) 230 goto out; 231 232 memset(¶ms, 0, sizeof(params)); 233 234 switch (key->conf.alg) { 235 case ALG_TKIP: 236 params.cipher = WLAN_CIPHER_SUITE_TKIP; 237 238 iv32 = key->u.tkip.iv32; 239 iv16 = key->u.tkip.iv16; 240 241 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE && 242 sdata->local->ops->get_tkip_seq) 243 sdata->local->ops->get_tkip_seq( 244 local_to_hw(sdata->local), 245 key->conf.hw_key_idx, 246 &iv32, &iv16); 247 248 seq[0] = iv16 & 0xff; 249 seq[1] = (iv16 >> 8) & 0xff; 250 seq[2] = iv32 & 0xff; 251 seq[3] = (iv32 >> 8) & 0xff; 252 seq[4] = (iv32 >> 16) & 0xff; 253 seq[5] = (iv32 >> 24) & 0xff; 254 params.seq = seq; 255 params.seq_len = 6; 256 break; 257 case ALG_CCMP: 258 params.cipher = WLAN_CIPHER_SUITE_CCMP; 259 seq[0] = key->u.ccmp.tx_pn[5]; 260 seq[1] = key->u.ccmp.tx_pn[4]; 261 seq[2] = key->u.ccmp.tx_pn[3]; 262 seq[3] = key->u.ccmp.tx_pn[2]; 263 seq[4] = key->u.ccmp.tx_pn[1]; 264 seq[5] = key->u.ccmp.tx_pn[0]; 265 params.seq = seq; 266 params.seq_len = 6; 267 break; 268 case ALG_WEP: 269 if (key->conf.keylen == 5) 270 params.cipher = WLAN_CIPHER_SUITE_WEP40; 271 else 272 params.cipher = WLAN_CIPHER_SUITE_WEP104; 273 break; 274 } 275 276 params.key = key->conf.key; 277 params.key_len = key->conf.keylen; 278 279 callback(cookie, ¶ms); 280 err = 0; 281 282 out: 283 if (sta) 284 sta_info_put(sta); 285 return err; 286 } 287 288 static int ieee80211_config_default_key(struct wiphy *wiphy, 289 struct net_device *dev, 290 u8 key_idx) 291 { 292 struct ieee80211_sub_if_data *sdata; 293 294 sdata = IEEE80211_DEV_TO_SUB_IF(dev); 295 ieee80211_set_default_key(sdata, key_idx); 296 297 return 0; 298 } 299 300 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev, 301 u8 *mac, struct station_info *sinfo) 302 { 303 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); 304 struct sta_info *sta; 305 306 sta = sta_info_get(local, mac); 307 if (!sta) 308 return -ENOENT; 309 310 /* XXX: verify sta->dev == dev */ 311 312 sinfo->filled = STATION_INFO_INACTIVE_TIME | 313 STATION_INFO_RX_BYTES | 314 STATION_INFO_TX_BYTES; 315 316 sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx); 317 sinfo->rx_bytes = sta->rx_bytes; 318 sinfo->tx_bytes = sta->tx_bytes; 319 320 sta_info_put(sta); 321 322 return 0; 323 } 324 325 /* 326 * This handles both adding a beacon and setting new beacon info 327 */ 328 static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata, 329 struct beacon_parameters *params) 330 { 331 struct beacon_data *new, *old; 332 int new_head_len, new_tail_len; 333 int size; 334 int err = -EINVAL; 335 336 old = sdata->u.ap.beacon; 337 338 /* head must not be zero-length */ 339 if (params->head && !params->head_len) 340 return -EINVAL; 341 342 /* 343 * This is a kludge. beacon interval should really be part 344 * of the beacon information. 345 */ 346 if (params->interval) { 347 sdata->local->hw.conf.beacon_int = params->interval; 348 if (ieee80211_hw_config(sdata->local)) 349 return -EINVAL; 350 /* 351 * We updated some parameter so if below bails out 352 * it's not an error. 353 */ 354 err = 0; 355 } 356 357 /* Need to have a beacon head if we don't have one yet */ 358 if (!params->head && !old) 359 return err; 360 361 /* sorry, no way to start beaconing without dtim period */ 362 if (!params->dtim_period && !old) 363 return err; 364 365 /* new or old head? */ 366 if (params->head) 367 new_head_len = params->head_len; 368 else 369 new_head_len = old->head_len; 370 371 /* new or old tail? */ 372 if (params->tail || !old) 373 /* params->tail_len will be zero for !params->tail */ 374 new_tail_len = params->tail_len; 375 else 376 new_tail_len = old->tail_len; 377 378 size = sizeof(*new) + new_head_len + new_tail_len; 379 380 new = kzalloc(size, GFP_KERNEL); 381 if (!new) 382 return -ENOMEM; 383 384 /* start filling the new info now */ 385 386 /* new or old dtim period? */ 387 if (params->dtim_period) 388 new->dtim_period = params->dtim_period; 389 else 390 new->dtim_period = old->dtim_period; 391 392 /* 393 * pointers go into the block we allocated, 394 * memory is | beacon_data | head | tail | 395 */ 396 new->head = ((u8 *) new) + sizeof(*new); 397 new->tail = new->head + new_head_len; 398 new->head_len = new_head_len; 399 new->tail_len = new_tail_len; 400 401 /* copy in head */ 402 if (params->head) 403 memcpy(new->head, params->head, new_head_len); 404 else 405 memcpy(new->head, old->head, new_head_len); 406 407 /* copy in optional tail */ 408 if (params->tail) 409 memcpy(new->tail, params->tail, new_tail_len); 410 else 411 if (old) 412 memcpy(new->tail, old->tail, new_tail_len); 413 414 rcu_assign_pointer(sdata->u.ap.beacon, new); 415 416 synchronize_rcu(); 417 418 kfree(old); 419 420 return ieee80211_if_config_beacon(sdata->dev); 421 } 422 423 static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev, 424 struct beacon_parameters *params) 425 { 426 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 427 struct beacon_data *old; 428 429 if (sdata->vif.type != IEEE80211_IF_TYPE_AP) 430 return -EINVAL; 431 432 old = sdata->u.ap.beacon; 433 434 if (old) 435 return -EALREADY; 436 437 return ieee80211_config_beacon(sdata, params); 438 } 439 440 static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev, 441 struct beacon_parameters *params) 442 { 443 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 444 struct beacon_data *old; 445 446 if (sdata->vif.type != IEEE80211_IF_TYPE_AP) 447 return -EINVAL; 448 449 old = sdata->u.ap.beacon; 450 451 if (!old) 452 return -ENOENT; 453 454 return ieee80211_config_beacon(sdata, params); 455 } 456 457 static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev) 458 { 459 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 460 struct beacon_data *old; 461 462 if (sdata->vif.type != IEEE80211_IF_TYPE_AP) 463 return -EINVAL; 464 465 old = sdata->u.ap.beacon; 466 467 if (!old) 468 return -ENOENT; 469 470 rcu_assign_pointer(sdata->u.ap.beacon, NULL); 471 synchronize_rcu(); 472 kfree(old); 473 474 return ieee80211_if_config_beacon(dev); 475 } 476 477 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */ 478 struct iapp_layer2_update { 479 u8 da[ETH_ALEN]; /* broadcast */ 480 u8 sa[ETH_ALEN]; /* STA addr */ 481 __be16 len; /* 6 */ 482 u8 dsap; /* 0 */ 483 u8 ssap; /* 0 */ 484 u8 control; 485 u8 xid_info[3]; 486 } __attribute__ ((packed)); 487 488 static void ieee80211_send_layer2_update(struct sta_info *sta) 489 { 490 struct iapp_layer2_update *msg; 491 struct sk_buff *skb; 492 493 /* Send Level 2 Update Frame to update forwarding tables in layer 2 494 * bridge devices */ 495 496 skb = dev_alloc_skb(sizeof(*msg)); 497 if (!skb) 498 return; 499 msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg)); 500 501 /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID) 502 * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */ 503 504 memset(msg->da, 0xff, ETH_ALEN); 505 memcpy(msg->sa, sta->addr, ETH_ALEN); 506 msg->len = htons(6); 507 msg->dsap = 0; 508 msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */ 509 msg->control = 0xaf; /* XID response lsb.1111F101. 510 * F=0 (no poll command; unsolicited frame) */ 511 msg->xid_info[0] = 0x81; /* XID format identifier */ 512 msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */ 513 msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */ 514 515 skb->dev = sta->dev; 516 skb->protocol = eth_type_trans(skb, sta->dev); 517 memset(skb->cb, 0, sizeof(skb->cb)); 518 netif_rx(skb); 519 } 520 521 static void sta_apply_parameters(struct ieee80211_local *local, 522 struct sta_info *sta, 523 struct station_parameters *params) 524 { 525 u32 rates; 526 int i, j; 527 struct ieee80211_supported_band *sband; 528 529 if (params->station_flags & STATION_FLAG_CHANGED) { 530 sta->flags &= ~WLAN_STA_AUTHORIZED; 531 if (params->station_flags & STATION_FLAG_AUTHORIZED) 532 sta->flags |= WLAN_STA_AUTHORIZED; 533 534 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE; 535 if (params->station_flags & STATION_FLAG_SHORT_PREAMBLE) 536 sta->flags |= WLAN_STA_SHORT_PREAMBLE; 537 538 sta->flags &= ~WLAN_STA_WME; 539 if (params->station_flags & STATION_FLAG_WME) 540 sta->flags |= WLAN_STA_WME; 541 } 542 543 if (params->aid) { 544 sta->aid = params->aid; 545 if (sta->aid > IEEE80211_MAX_AID) 546 sta->aid = 0; /* XXX: should this be an error? */ 547 } 548 549 if (params->listen_interval >= 0) 550 sta->listen_interval = params->listen_interval; 551 552 if (params->supported_rates) { 553 rates = 0; 554 sband = local->hw.wiphy->bands[local->oper_channel->band]; 555 556 for (i = 0; i < params->supported_rates_len; i++) { 557 int rate = (params->supported_rates[i] & 0x7f) * 5; 558 for (j = 0; j < sband->n_bitrates; j++) { 559 if (sband->bitrates[j].bitrate == rate) 560 rates |= BIT(j); 561 } 562 } 563 sta->supp_rates[local->oper_channel->band] = rates; 564 } 565 } 566 567 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev, 568 u8 *mac, struct station_parameters *params) 569 { 570 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); 571 struct sta_info *sta; 572 struct ieee80211_sub_if_data *sdata; 573 574 /* Prevent a race with changing the rate control algorithm */ 575 if (!netif_running(dev)) 576 return -ENETDOWN; 577 578 if (params->vlan) { 579 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan); 580 581 if (sdata->vif.type != IEEE80211_IF_TYPE_VLAN || 582 sdata->vif.type != IEEE80211_IF_TYPE_AP) 583 return -EINVAL; 584 } else 585 sdata = IEEE80211_DEV_TO_SUB_IF(dev); 586 587 sta = sta_info_add(local, dev, mac, GFP_KERNEL); 588 if (IS_ERR(sta)) 589 return PTR_ERR(sta); 590 591 sta->dev = sdata->dev; 592 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN || 593 sdata->vif.type == IEEE80211_IF_TYPE_AP) 594 ieee80211_send_layer2_update(sta); 595 596 sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC; 597 598 sta_apply_parameters(local, sta, params); 599 600 rate_control_rate_init(sta, local); 601 602 sta_info_put(sta); 603 604 return 0; 605 } 606 607 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev, 608 u8 *mac) 609 { 610 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); 611 struct sta_info *sta; 612 613 if (mac) { 614 /* XXX: get sta belonging to dev */ 615 sta = sta_info_get(local, mac); 616 if (!sta) 617 return -ENOENT; 618 619 sta_info_free(sta); 620 sta_info_put(sta); 621 } else 622 sta_info_flush(local, dev); 623 624 return 0; 625 } 626 627 static int ieee80211_change_station(struct wiphy *wiphy, 628 struct net_device *dev, 629 u8 *mac, 630 struct station_parameters *params) 631 { 632 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); 633 struct sta_info *sta; 634 struct ieee80211_sub_if_data *vlansdata; 635 636 /* XXX: get sta belonging to dev */ 637 sta = sta_info_get(local, mac); 638 if (!sta) 639 return -ENOENT; 640 641 if (params->vlan && params->vlan != sta->dev) { 642 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan); 643 644 if (vlansdata->vif.type != IEEE80211_IF_TYPE_VLAN || 645 vlansdata->vif.type != IEEE80211_IF_TYPE_AP) 646 return -EINVAL; 647 648 sta->dev = params->vlan; 649 ieee80211_send_layer2_update(sta); 650 } 651 652 sta_apply_parameters(local, sta, params); 653 654 sta_info_put(sta); 655 656 return 0; 657 } 658 659 struct cfg80211_ops mac80211_config_ops = { 660 .add_virtual_intf = ieee80211_add_iface, 661 .del_virtual_intf = ieee80211_del_iface, 662 .change_virtual_intf = ieee80211_change_iface, 663 .add_key = ieee80211_add_key, 664 .del_key = ieee80211_del_key, 665 .get_key = ieee80211_get_key, 666 .set_default_key = ieee80211_config_default_key, 667 .add_beacon = ieee80211_add_beacon, 668 .set_beacon = ieee80211_set_beacon, 669 .del_beacon = ieee80211_del_beacon, 670 .add_station = ieee80211_add_station, 671 .del_station = ieee80211_del_station, 672 .change_station = ieee80211_change_station, 673 .get_station = ieee80211_get_station, 674 }; 675