cfg.c 22 KB

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