cfg.c 22 KB

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