cfg.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. #include <linux/ieee80211.h>
  9. #include <linux/nl80211.h>
  10. #include <linux/rtnetlink.h>
  11. #include <net/net_namespace.h>
  12. #include <linux/rcupdate.h>
  13. #include <net/cfg80211.h>
  14. #include "ieee80211_i.h"
  15. #include "cfg.h"
  16. #include "rate.h"
  17. #include "mesh.h"
  18. static bool nl80211_type_check(enum nl80211_iftype type)
  19. {
  20. switch (type) {
  21. case NL80211_IFTYPE_ADHOC:
  22. case NL80211_IFTYPE_STATION:
  23. case NL80211_IFTYPE_MONITOR:
  24. #ifdef CONFIG_MAC80211_MESH
  25. case NL80211_IFTYPE_MESH_POINT:
  26. #endif
  27. case NL80211_IFTYPE_WDS:
  28. return true;
  29. default:
  30. return false;
  31. }
  32. }
  33. static int ieee80211_add_iface(struct wiphy *wiphy, char *name,
  34. enum nl80211_iftype type, u32 *flags,
  35. struct vif_params *params)
  36. {
  37. struct ieee80211_local *local = wiphy_priv(wiphy);
  38. struct net_device *dev;
  39. struct ieee80211_sub_if_data *sdata;
  40. int err;
  41. if (!nl80211_type_check(type))
  42. return -EINVAL;
  43. err = ieee80211_if_add(local, name, &dev, type, params);
  44. if (err || type != NL80211_IFTYPE_MONITOR || !flags)
  45. return err;
  46. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  47. sdata->u.mntr_flags = *flags;
  48. return 0;
  49. }
  50. static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex)
  51. {
  52. struct net_device *dev;
  53. struct ieee80211_sub_if_data *sdata;
  54. /* we're under RTNL */
  55. dev = __dev_get_by_index(&init_net, ifindex);
  56. if (!dev)
  57. return -ENODEV;
  58. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  59. ieee80211_if_remove(sdata);
  60. return 0;
  61. }
  62. static int ieee80211_change_iface(struct wiphy *wiphy, int ifindex,
  63. enum nl80211_iftype type, u32 *flags,
  64. struct vif_params *params)
  65. {
  66. struct net_device *dev;
  67. struct ieee80211_sub_if_data *sdata;
  68. int ret;
  69. /* we're under RTNL */
  70. dev = __dev_get_by_index(&init_net, ifindex);
  71. if (!dev)
  72. return -ENODEV;
  73. if (!nl80211_type_check(type))
  74. return -EINVAL;
  75. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  76. ret = ieee80211_if_change_type(sdata, type);
  77. if (ret)
  78. return ret;
  79. if (netif_running(sdata->dev))
  80. return -EBUSY;
  81. if (ieee80211_vif_is_mesh(&sdata->vif) && params->mesh_id_len)
  82. ieee80211_sdata_set_mesh_id(sdata,
  83. params->mesh_id_len,
  84. params->mesh_id);
  85. if (sdata->vif.type != NL80211_IFTYPE_MONITOR || !flags)
  86. return 0;
  87. sdata->u.mntr_flags = *flags;
  88. return 0;
  89. }
  90. static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
  91. u8 key_idx, u8 *mac_addr,
  92. struct key_params *params)
  93. {
  94. struct ieee80211_sub_if_data *sdata;
  95. struct sta_info *sta = NULL;
  96. enum ieee80211_key_alg alg;
  97. struct ieee80211_key *key;
  98. int err;
  99. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  100. switch (params->cipher) {
  101. case WLAN_CIPHER_SUITE_WEP40:
  102. case WLAN_CIPHER_SUITE_WEP104:
  103. alg = ALG_WEP;
  104. break;
  105. case WLAN_CIPHER_SUITE_TKIP:
  106. alg = ALG_TKIP;
  107. break;
  108. case WLAN_CIPHER_SUITE_CCMP:
  109. alg = ALG_CCMP;
  110. break;
  111. default:
  112. return -EINVAL;
  113. }
  114. key = ieee80211_key_alloc(alg, key_idx, params->key_len, params->key);
  115. if (!key)
  116. return -ENOMEM;
  117. rcu_read_lock();
  118. if (mac_addr) {
  119. sta = sta_info_get(sdata->local, mac_addr);
  120. if (!sta) {
  121. ieee80211_key_free(key);
  122. err = -ENOENT;
  123. goto out_unlock;
  124. }
  125. }
  126. ieee80211_key_link(key, sdata, sta);
  127. err = 0;
  128. out_unlock:
  129. rcu_read_unlock();
  130. return err;
  131. }
  132. static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
  133. u8 key_idx, u8 *mac_addr)
  134. {
  135. struct ieee80211_sub_if_data *sdata;
  136. struct sta_info *sta;
  137. int ret;
  138. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  139. rcu_read_lock();
  140. if (mac_addr) {
  141. ret = -ENOENT;
  142. sta = sta_info_get(sdata->local, mac_addr);
  143. if (!sta)
  144. goto out_unlock;
  145. if (sta->key) {
  146. ieee80211_key_free(sta->key);
  147. WARN_ON(sta->key);
  148. ret = 0;
  149. }
  150. goto out_unlock;
  151. }
  152. if (!sdata->keys[key_idx]) {
  153. ret = -ENOENT;
  154. goto out_unlock;
  155. }
  156. ieee80211_key_free(sdata->keys[key_idx]);
  157. WARN_ON(sdata->keys[key_idx]);
  158. ret = 0;
  159. out_unlock:
  160. rcu_read_unlock();
  161. return ret;
  162. }
  163. static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
  164. u8 key_idx, u8 *mac_addr, void *cookie,
  165. void (*callback)(void *cookie,
  166. struct key_params *params))
  167. {
  168. struct ieee80211_sub_if_data *sdata;
  169. struct sta_info *sta = NULL;
  170. u8 seq[6] = {0};
  171. struct key_params params;
  172. struct ieee80211_key *key;
  173. u32 iv32;
  174. u16 iv16;
  175. int err = -ENOENT;
  176. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  177. rcu_read_lock();
  178. if (mac_addr) {
  179. sta = sta_info_get(sdata->local, mac_addr);
  180. if (!sta)
  181. goto out;
  182. key = sta->key;
  183. } else
  184. key = sdata->keys[key_idx];
  185. if (!key)
  186. goto out;
  187. memset(&params, 0, sizeof(params));
  188. switch (key->conf.alg) {
  189. case ALG_TKIP:
  190. params.cipher = WLAN_CIPHER_SUITE_TKIP;
  191. iv32 = key->u.tkip.tx.iv32;
  192. iv16 = key->u.tkip.tx.iv16;
  193. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
  194. sdata->local->ops->get_tkip_seq)
  195. sdata->local->ops->get_tkip_seq(
  196. local_to_hw(sdata->local),
  197. key->conf.hw_key_idx,
  198. &iv32, &iv16);
  199. seq[0] = iv16 & 0xff;
  200. seq[1] = (iv16 >> 8) & 0xff;
  201. seq[2] = iv32 & 0xff;
  202. seq[3] = (iv32 >> 8) & 0xff;
  203. seq[4] = (iv32 >> 16) & 0xff;
  204. seq[5] = (iv32 >> 24) & 0xff;
  205. params.seq = seq;
  206. params.seq_len = 6;
  207. break;
  208. case ALG_CCMP:
  209. params.cipher = WLAN_CIPHER_SUITE_CCMP;
  210. seq[0] = key->u.ccmp.tx_pn[5];
  211. seq[1] = key->u.ccmp.tx_pn[4];
  212. seq[2] = key->u.ccmp.tx_pn[3];
  213. seq[3] = key->u.ccmp.tx_pn[2];
  214. seq[4] = key->u.ccmp.tx_pn[1];
  215. seq[5] = key->u.ccmp.tx_pn[0];
  216. params.seq = seq;
  217. params.seq_len = 6;
  218. break;
  219. case ALG_WEP:
  220. if (key->conf.keylen == 5)
  221. params.cipher = WLAN_CIPHER_SUITE_WEP40;
  222. else
  223. params.cipher = WLAN_CIPHER_SUITE_WEP104;
  224. break;
  225. }
  226. params.key = key->conf.key;
  227. params.key_len = key->conf.keylen;
  228. callback(cookie, &params);
  229. err = 0;
  230. out:
  231. rcu_read_unlock();
  232. return err;
  233. }
  234. static int ieee80211_config_default_key(struct wiphy *wiphy,
  235. struct net_device *dev,
  236. u8 key_idx)
  237. {
  238. struct ieee80211_sub_if_data *sdata;
  239. rcu_read_lock();
  240. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  241. ieee80211_set_default_key(sdata, key_idx);
  242. rcu_read_unlock();
  243. return 0;
  244. }
  245. static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
  246. {
  247. struct ieee80211_sub_if_data *sdata = sta->sdata;
  248. sinfo->filled = STATION_INFO_INACTIVE_TIME |
  249. STATION_INFO_RX_BYTES |
  250. STATION_INFO_TX_BYTES;
  251. sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
  252. sinfo->rx_bytes = sta->rx_bytes;
  253. sinfo->tx_bytes = sta->tx_bytes;
  254. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  255. #ifdef CONFIG_MAC80211_MESH
  256. sinfo->filled |= STATION_INFO_LLID |
  257. STATION_INFO_PLID |
  258. STATION_INFO_PLINK_STATE;
  259. sinfo->llid = le16_to_cpu(sta->llid);
  260. sinfo->plid = le16_to_cpu(sta->plid);
  261. sinfo->plink_state = sta->plink_state;
  262. #endif
  263. }
  264. }
  265. static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
  266. int idx, u8 *mac, struct station_info *sinfo)
  267. {
  268. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  269. struct sta_info *sta;
  270. int ret = -ENOENT;
  271. rcu_read_lock();
  272. sta = sta_info_get_by_idx(local, idx, dev);
  273. if (sta) {
  274. ret = 0;
  275. memcpy(mac, sta->sta.addr, ETH_ALEN);
  276. sta_set_sinfo(sta, sinfo);
  277. }
  278. rcu_read_unlock();
  279. return ret;
  280. }
  281. static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
  282. u8 *mac, struct station_info *sinfo)
  283. {
  284. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  285. struct sta_info *sta;
  286. int ret = -ENOENT;
  287. rcu_read_lock();
  288. /* XXX: verify sta->dev == dev */
  289. sta = sta_info_get(local, mac);
  290. if (sta) {
  291. ret = 0;
  292. sta_set_sinfo(sta, sinfo);
  293. }
  294. rcu_read_unlock();
  295. return ret;
  296. }
  297. /*
  298. * This handles both adding a beacon and setting new beacon info
  299. */
  300. static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
  301. struct beacon_parameters *params)
  302. {
  303. struct beacon_data *new, *old;
  304. int new_head_len, new_tail_len;
  305. int size;
  306. int err = -EINVAL;
  307. old = sdata->u.ap.beacon;
  308. /* head must not be zero-length */
  309. if (params->head && !params->head_len)
  310. return -EINVAL;
  311. /*
  312. * This is a kludge. beacon interval should really be part
  313. * of the beacon information.
  314. */
  315. if (params->interval) {
  316. sdata->local->hw.conf.beacon_int = params->interval;
  317. if (ieee80211_hw_config(sdata->local))
  318. return -EINVAL;
  319. /*
  320. * We updated some parameter so if below bails out
  321. * it's not an error.
  322. */
  323. err = 0;
  324. }
  325. /* Need to have a beacon head if we don't have one yet */
  326. if (!params->head && !old)
  327. return err;
  328. /* sorry, no way to start beaconing without dtim period */
  329. if (!params->dtim_period && !old)
  330. return err;
  331. /* new or old head? */
  332. if (params->head)
  333. new_head_len = params->head_len;
  334. else
  335. new_head_len = old->head_len;
  336. /* new or old tail? */
  337. if (params->tail || !old)
  338. /* params->tail_len will be zero for !params->tail */
  339. new_tail_len = params->tail_len;
  340. else
  341. new_tail_len = old->tail_len;
  342. size = sizeof(*new) + new_head_len + new_tail_len;
  343. new = kzalloc(size, GFP_KERNEL);
  344. if (!new)
  345. return -ENOMEM;
  346. /* start filling the new info now */
  347. /* new or old dtim period? */
  348. if (params->dtim_period)
  349. new->dtim_period = params->dtim_period;
  350. else
  351. new->dtim_period = old->dtim_period;
  352. /*
  353. * pointers go into the block we allocated,
  354. * memory is | beacon_data | head | tail |
  355. */
  356. new->head = ((u8 *) new) + sizeof(*new);
  357. new->tail = new->head + new_head_len;
  358. new->head_len = new_head_len;
  359. new->tail_len = new_tail_len;
  360. /* copy in head */
  361. if (params->head)
  362. memcpy(new->head, params->head, new_head_len);
  363. else
  364. memcpy(new->head, old->head, new_head_len);
  365. /* copy in optional tail */
  366. if (params->tail)
  367. memcpy(new->tail, params->tail, new_tail_len);
  368. else
  369. if (old)
  370. memcpy(new->tail, old->tail, new_tail_len);
  371. rcu_assign_pointer(sdata->u.ap.beacon, new);
  372. synchronize_rcu();
  373. kfree(old);
  374. return ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
  375. }
  376. static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
  377. struct beacon_parameters *params)
  378. {
  379. struct ieee80211_sub_if_data *sdata;
  380. struct beacon_data *old;
  381. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  382. if (sdata->vif.type != NL80211_IFTYPE_AP)
  383. return -EINVAL;
  384. old = sdata->u.ap.beacon;
  385. if (old)
  386. return -EALREADY;
  387. return ieee80211_config_beacon(sdata, params);
  388. }
  389. static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
  390. struct beacon_parameters *params)
  391. {
  392. struct ieee80211_sub_if_data *sdata;
  393. struct beacon_data *old;
  394. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  395. if (sdata->vif.type != NL80211_IFTYPE_AP)
  396. return -EINVAL;
  397. old = sdata->u.ap.beacon;
  398. if (!old)
  399. return -ENOENT;
  400. return ieee80211_config_beacon(sdata, params);
  401. }
  402. static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
  403. {
  404. struct ieee80211_sub_if_data *sdata;
  405. struct beacon_data *old;
  406. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  407. if (sdata->vif.type != NL80211_IFTYPE_AP)
  408. return -EINVAL;
  409. old = sdata->u.ap.beacon;
  410. if (!old)
  411. return -ENOENT;
  412. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  413. synchronize_rcu();
  414. kfree(old);
  415. return ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
  416. }
  417. /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
  418. struct iapp_layer2_update {
  419. u8 da[ETH_ALEN]; /* broadcast */
  420. u8 sa[ETH_ALEN]; /* STA addr */
  421. __be16 len; /* 6 */
  422. u8 dsap; /* 0 */
  423. u8 ssap; /* 0 */
  424. u8 control;
  425. u8 xid_info[3];
  426. } __attribute__ ((packed));
  427. static void ieee80211_send_layer2_update(struct sta_info *sta)
  428. {
  429. struct iapp_layer2_update *msg;
  430. struct sk_buff *skb;
  431. /* Send Level 2 Update Frame to update forwarding tables in layer 2
  432. * bridge devices */
  433. skb = dev_alloc_skb(sizeof(*msg));
  434. if (!skb)
  435. return;
  436. msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
  437. /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
  438. * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
  439. memset(msg->da, 0xff, ETH_ALEN);
  440. memcpy(msg->sa, sta->sta.addr, ETH_ALEN);
  441. msg->len = htons(6);
  442. msg->dsap = 0;
  443. msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
  444. msg->control = 0xaf; /* XID response lsb.1111F101.
  445. * F=0 (no poll command; unsolicited frame) */
  446. msg->xid_info[0] = 0x81; /* XID format identifier */
  447. msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
  448. msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
  449. skb->dev = sta->sdata->dev;
  450. skb->protocol = eth_type_trans(skb, sta->sdata->dev);
  451. memset(skb->cb, 0, sizeof(skb->cb));
  452. netif_rx(skb);
  453. }
  454. static void sta_apply_parameters(struct ieee80211_local *local,
  455. struct sta_info *sta,
  456. struct station_parameters *params)
  457. {
  458. u32 rates;
  459. int i, j;
  460. struct ieee80211_supported_band *sband;
  461. struct ieee80211_sub_if_data *sdata = sta->sdata;
  462. /*
  463. * FIXME: updating the flags is racy when this function is
  464. * called from ieee80211_change_station(), this will
  465. * be resolved in a future patch.
  466. */
  467. if (params->station_flags & STATION_FLAG_CHANGED) {
  468. spin_lock_bh(&sta->lock);
  469. sta->flags &= ~WLAN_STA_AUTHORIZED;
  470. if (params->station_flags & STATION_FLAG_AUTHORIZED)
  471. sta->flags |= WLAN_STA_AUTHORIZED;
  472. sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
  473. if (params->station_flags & STATION_FLAG_SHORT_PREAMBLE)
  474. sta->flags |= WLAN_STA_SHORT_PREAMBLE;
  475. sta->flags &= ~WLAN_STA_WME;
  476. if (params->station_flags & STATION_FLAG_WME)
  477. sta->flags |= WLAN_STA_WME;
  478. spin_unlock_bh(&sta->lock);
  479. }
  480. /*
  481. * FIXME: updating the following information is racy when this
  482. * function is called from ieee80211_change_station().
  483. * However, all this information should be static so
  484. * maybe we should just reject attemps to change it.
  485. */
  486. if (params->aid) {
  487. sta->sta.aid = params->aid;
  488. if (sta->sta.aid > IEEE80211_MAX_AID)
  489. sta->sta.aid = 0; /* XXX: should this be an error? */
  490. }
  491. if (params->listen_interval >= 0)
  492. sta->listen_interval = params->listen_interval;
  493. if (params->supported_rates) {
  494. rates = 0;
  495. sband = local->hw.wiphy->bands[local->oper_channel->band];
  496. for (i = 0; i < params->supported_rates_len; i++) {
  497. int rate = (params->supported_rates[i] & 0x7f) * 5;
  498. for (j = 0; j < sband->n_bitrates; j++) {
  499. if (sband->bitrates[j].bitrate == rate)
  500. rates |= BIT(j);
  501. }
  502. }
  503. sta->sta.supp_rates[local->oper_channel->band] = rates;
  504. }
  505. if (params->ht_capa) {
  506. ieee80211_ht_cap_ie_to_ht_info(params->ht_capa,
  507. &sta->sta.ht_info);
  508. }
  509. if (ieee80211_vif_is_mesh(&sdata->vif) && params->plink_action) {
  510. switch (params->plink_action) {
  511. case PLINK_ACTION_OPEN:
  512. mesh_plink_open(sta);
  513. break;
  514. case PLINK_ACTION_BLOCK:
  515. mesh_plink_block(sta);
  516. break;
  517. }
  518. }
  519. }
  520. static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
  521. u8 *mac, struct station_parameters *params)
  522. {
  523. struct ieee80211_local *local = wiphy_priv(wiphy);
  524. struct sta_info *sta;
  525. struct ieee80211_sub_if_data *sdata;
  526. int err;
  527. /* Prevent a race with changing the rate control algorithm */
  528. if (!netif_running(dev))
  529. return -ENETDOWN;
  530. if (params->vlan) {
  531. sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
  532. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  533. sdata->vif.type != NL80211_IFTYPE_AP)
  534. return -EINVAL;
  535. } else
  536. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  537. if (compare_ether_addr(mac, dev->dev_addr) == 0)
  538. return -EINVAL;
  539. if (is_multicast_ether_addr(mac))
  540. return -EINVAL;
  541. sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
  542. if (!sta)
  543. return -ENOMEM;
  544. sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
  545. sta_apply_parameters(local, sta, params);
  546. rate_control_rate_init(sta);
  547. rcu_read_lock();
  548. err = sta_info_insert(sta);
  549. if (err) {
  550. /* STA has been freed */
  551. rcu_read_unlock();
  552. return err;
  553. }
  554. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  555. sdata->vif.type == NL80211_IFTYPE_AP)
  556. ieee80211_send_layer2_update(sta);
  557. rcu_read_unlock();
  558. return 0;
  559. }
  560. static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
  561. u8 *mac)
  562. {
  563. struct ieee80211_local *local = wiphy_priv(wiphy);
  564. struct ieee80211_sub_if_data *sdata;
  565. struct sta_info *sta;
  566. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  567. if (mac) {
  568. rcu_read_lock();
  569. /* XXX: get sta belonging to dev */
  570. sta = sta_info_get(local, mac);
  571. if (!sta) {
  572. rcu_read_unlock();
  573. return -ENOENT;
  574. }
  575. sta_info_unlink(&sta);
  576. rcu_read_unlock();
  577. sta_info_destroy(sta);
  578. } else
  579. sta_info_flush(local, sdata);
  580. return 0;
  581. }
  582. static int ieee80211_change_station(struct wiphy *wiphy,
  583. struct net_device *dev,
  584. u8 *mac,
  585. struct station_parameters *params)
  586. {
  587. struct ieee80211_local *local = wiphy_priv(wiphy);
  588. struct sta_info *sta;
  589. struct ieee80211_sub_if_data *vlansdata;
  590. rcu_read_lock();
  591. /* XXX: get sta belonging to dev */
  592. sta = sta_info_get(local, mac);
  593. if (!sta) {
  594. rcu_read_unlock();
  595. return -ENOENT;
  596. }
  597. if (params->vlan && params->vlan != sta->sdata->dev) {
  598. vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
  599. if (vlansdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  600. vlansdata->vif.type != NL80211_IFTYPE_AP) {
  601. rcu_read_unlock();
  602. return -EINVAL;
  603. }
  604. sta->sdata = vlansdata;
  605. ieee80211_send_layer2_update(sta);
  606. }
  607. sta_apply_parameters(local, sta, params);
  608. rcu_read_unlock();
  609. return 0;
  610. }
  611. #ifdef CONFIG_MAC80211_MESH
  612. static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
  613. u8 *dst, u8 *next_hop)
  614. {
  615. struct ieee80211_local *local = wiphy_priv(wiphy);
  616. struct ieee80211_sub_if_data *sdata;
  617. struct mesh_path *mpath;
  618. struct sta_info *sta;
  619. int err;
  620. if (!netif_running(dev))
  621. return -ENETDOWN;
  622. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  623. if (sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  624. return -ENOTSUPP;
  625. rcu_read_lock();
  626. sta = sta_info_get(local, next_hop);
  627. if (!sta) {
  628. rcu_read_unlock();
  629. return -ENOENT;
  630. }
  631. err = mesh_path_add(dst, sdata);
  632. if (err) {
  633. rcu_read_unlock();
  634. return err;
  635. }
  636. mpath = mesh_path_lookup(dst, sdata);
  637. if (!mpath) {
  638. rcu_read_unlock();
  639. return -ENXIO;
  640. }
  641. mesh_path_fix_nexthop(mpath, sta);
  642. rcu_read_unlock();
  643. return 0;
  644. }
  645. static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
  646. u8 *dst)
  647. {
  648. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  649. if (dst)
  650. return mesh_path_del(dst, sdata);
  651. mesh_path_flush(sdata);
  652. return 0;
  653. }
  654. static int ieee80211_change_mpath(struct wiphy *wiphy,
  655. struct net_device *dev,
  656. u8 *dst, u8 *next_hop)
  657. {
  658. struct ieee80211_local *local = wiphy_priv(wiphy);
  659. struct ieee80211_sub_if_data *sdata;
  660. struct mesh_path *mpath;
  661. struct sta_info *sta;
  662. if (!netif_running(dev))
  663. return -ENETDOWN;
  664. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  665. if (sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  666. return -ENOTSUPP;
  667. rcu_read_lock();
  668. sta = sta_info_get(local, next_hop);
  669. if (!sta) {
  670. rcu_read_unlock();
  671. return -ENOENT;
  672. }
  673. mpath = mesh_path_lookup(dst, sdata);
  674. if (!mpath) {
  675. rcu_read_unlock();
  676. return -ENOENT;
  677. }
  678. mesh_path_fix_nexthop(mpath, sta);
  679. rcu_read_unlock();
  680. return 0;
  681. }
  682. static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
  683. struct mpath_info *pinfo)
  684. {
  685. if (mpath->next_hop)
  686. memcpy(next_hop, mpath->next_hop->sta.addr, ETH_ALEN);
  687. else
  688. memset(next_hop, 0, ETH_ALEN);
  689. pinfo->filled = MPATH_INFO_FRAME_QLEN |
  690. MPATH_INFO_DSN |
  691. MPATH_INFO_METRIC |
  692. MPATH_INFO_EXPTIME |
  693. MPATH_INFO_DISCOVERY_TIMEOUT |
  694. MPATH_INFO_DISCOVERY_RETRIES |
  695. MPATH_INFO_FLAGS;
  696. pinfo->frame_qlen = mpath->frame_queue.qlen;
  697. pinfo->dsn = mpath->dsn;
  698. pinfo->metric = mpath->metric;
  699. if (time_before(jiffies, mpath->exp_time))
  700. pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
  701. pinfo->discovery_timeout =
  702. jiffies_to_msecs(mpath->discovery_timeout);
  703. pinfo->discovery_retries = mpath->discovery_retries;
  704. pinfo->flags = 0;
  705. if (mpath->flags & MESH_PATH_ACTIVE)
  706. pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
  707. if (mpath->flags & MESH_PATH_RESOLVING)
  708. pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
  709. if (mpath->flags & MESH_PATH_DSN_VALID)
  710. pinfo->flags |= NL80211_MPATH_FLAG_DSN_VALID;
  711. if (mpath->flags & MESH_PATH_FIXED)
  712. pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
  713. if (mpath->flags & MESH_PATH_RESOLVING)
  714. pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
  715. pinfo->flags = mpath->flags;
  716. }
  717. static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
  718. u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
  719. {
  720. struct ieee80211_sub_if_data *sdata;
  721. struct mesh_path *mpath;
  722. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  723. if (sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  724. return -ENOTSUPP;
  725. rcu_read_lock();
  726. mpath = mesh_path_lookup(dst, sdata);
  727. if (!mpath) {
  728. rcu_read_unlock();
  729. return -ENOENT;
  730. }
  731. memcpy(dst, mpath->dst, ETH_ALEN);
  732. mpath_set_pinfo(mpath, next_hop, pinfo);
  733. rcu_read_unlock();
  734. return 0;
  735. }
  736. static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
  737. int idx, u8 *dst, u8 *next_hop,
  738. struct mpath_info *pinfo)
  739. {
  740. struct ieee80211_sub_if_data *sdata;
  741. struct mesh_path *mpath;
  742. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  743. if (sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  744. return -ENOTSUPP;
  745. rcu_read_lock();
  746. mpath = mesh_path_lookup_by_idx(idx, sdata);
  747. if (!mpath) {
  748. rcu_read_unlock();
  749. return -ENOENT;
  750. }
  751. memcpy(dst, mpath->dst, ETH_ALEN);
  752. mpath_set_pinfo(mpath, next_hop, pinfo);
  753. rcu_read_unlock();
  754. return 0;
  755. }
  756. #endif
  757. static int ieee80211_change_bss(struct wiphy *wiphy,
  758. struct net_device *dev,
  759. struct bss_parameters *params)
  760. {
  761. struct ieee80211_sub_if_data *sdata;
  762. u32 changed = 0;
  763. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  764. if (sdata->vif.type != NL80211_IFTYPE_AP)
  765. return -EINVAL;
  766. if (params->use_cts_prot >= 0) {
  767. sdata->bss_conf.use_cts_prot = params->use_cts_prot;
  768. changed |= BSS_CHANGED_ERP_CTS_PROT;
  769. }
  770. if (params->use_short_preamble >= 0) {
  771. sdata->bss_conf.use_short_preamble =
  772. params->use_short_preamble;
  773. changed |= BSS_CHANGED_ERP_PREAMBLE;
  774. }
  775. if (params->use_short_slot_time >= 0) {
  776. sdata->bss_conf.use_short_slot =
  777. params->use_short_slot_time;
  778. changed |= BSS_CHANGED_ERP_SLOT;
  779. }
  780. ieee80211_bss_info_change_notify(sdata, changed);
  781. return 0;
  782. }
  783. struct cfg80211_ops mac80211_config_ops = {
  784. .add_virtual_intf = ieee80211_add_iface,
  785. .del_virtual_intf = ieee80211_del_iface,
  786. .change_virtual_intf = ieee80211_change_iface,
  787. .add_key = ieee80211_add_key,
  788. .del_key = ieee80211_del_key,
  789. .get_key = ieee80211_get_key,
  790. .set_default_key = ieee80211_config_default_key,
  791. .add_beacon = ieee80211_add_beacon,
  792. .set_beacon = ieee80211_set_beacon,
  793. .del_beacon = ieee80211_del_beacon,
  794. .add_station = ieee80211_add_station,
  795. .del_station = ieee80211_del_station,
  796. .change_station = ieee80211_change_station,
  797. .get_station = ieee80211_get_station,
  798. .dump_station = ieee80211_dump_station,
  799. #ifdef CONFIG_MAC80211_MESH
  800. .add_mpath = ieee80211_add_mpath,
  801. .del_mpath = ieee80211_del_mpath,
  802. .change_mpath = ieee80211_change_mpath,
  803. .get_mpath = ieee80211_get_mpath,
  804. .dump_mpath = ieee80211_dump_mpath,
  805. #endif
  806. .change_bss = ieee80211_change_bss,
  807. };