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