cfg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License version 2
  3. * as published by the Free Software Foundation.
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * Authors:
  11. * Alexander Aring <aar@pengutronix.de>
  12. *
  13. * Based on: net/mac80211/cfg.c
  14. */
  15. #include <net/rtnetlink.h>
  16. #include <net/cfg802154.h>
  17. #include "ieee802154_i.h"
  18. #include "driver-ops.h"
  19. #include "cfg.h"
  20. static struct net_device *
  21. ieee802154_add_iface_deprecated(struct wpan_phy *wpan_phy,
  22. const char *name,
  23. unsigned char name_assign_type, int type)
  24. {
  25. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  26. struct net_device *dev;
  27. rtnl_lock();
  28. dev = ieee802154_if_add(local, name, name_assign_type, type,
  29. cpu_to_le64(0x0000000000000000ULL));
  30. rtnl_unlock();
  31. return dev;
  32. }
  33. static void ieee802154_del_iface_deprecated(struct wpan_phy *wpan_phy,
  34. struct net_device *dev)
  35. {
  36. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  37. ieee802154_if_remove(sdata);
  38. }
  39. #ifdef CONFIG_PM
  40. static int ieee802154_suspend(struct wpan_phy *wpan_phy)
  41. {
  42. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  43. if (!local->open_count)
  44. goto suspend;
  45. ieee802154_stop_queue(&local->hw);
  46. synchronize_net();
  47. /* stop hardware - this must stop RX */
  48. ieee802154_stop_device(local);
  49. suspend:
  50. local->suspended = true;
  51. return 0;
  52. }
  53. static int ieee802154_resume(struct wpan_phy *wpan_phy)
  54. {
  55. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  56. int ret;
  57. /* nothing to do if HW shouldn't run */
  58. if (!local->open_count)
  59. goto wake_up;
  60. /* restart hardware */
  61. ret = drv_start(local);
  62. if (ret)
  63. return ret;
  64. wake_up:
  65. ieee802154_wake_queue(&local->hw);
  66. local->suspended = false;
  67. return 0;
  68. }
  69. #else
  70. #define ieee802154_suspend NULL
  71. #define ieee802154_resume NULL
  72. #endif
  73. static int
  74. ieee802154_add_iface(struct wpan_phy *phy, const char *name,
  75. unsigned char name_assign_type,
  76. enum nl802154_iftype type, __le64 extended_addr)
  77. {
  78. struct ieee802154_local *local = wpan_phy_priv(phy);
  79. struct net_device *err;
  80. err = ieee802154_if_add(local, name, name_assign_type, type,
  81. extended_addr);
  82. return PTR_ERR_OR_ZERO(err);
  83. }
  84. static int
  85. ieee802154_del_iface(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
  86. {
  87. ieee802154_if_remove(IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev));
  88. return 0;
  89. }
  90. static int
  91. ieee802154_set_channel(struct wpan_phy *wpan_phy, u8 page, u8 channel)
  92. {
  93. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  94. int ret;
  95. ASSERT_RTNL();
  96. if (wpan_phy->current_page == page &&
  97. wpan_phy->current_channel == channel)
  98. return 0;
  99. ret = drv_set_channel(local, page, channel);
  100. if (!ret) {
  101. wpan_phy->current_page = page;
  102. wpan_phy->current_channel = channel;
  103. }
  104. return ret;
  105. }
  106. static int
  107. ieee802154_set_cca_mode(struct wpan_phy *wpan_phy,
  108. const struct wpan_phy_cca *cca)
  109. {
  110. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  111. int ret;
  112. ASSERT_RTNL();
  113. if (wpan_phy_cca_cmp(&wpan_phy->cca, cca))
  114. return 0;
  115. ret = drv_set_cca_mode(local, cca);
  116. if (!ret)
  117. wpan_phy->cca = *cca;
  118. return ret;
  119. }
  120. static int
  121. ieee802154_set_cca_ed_level(struct wpan_phy *wpan_phy, s32 ed_level)
  122. {
  123. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  124. int ret;
  125. ASSERT_RTNL();
  126. if (wpan_phy->cca_ed_level == ed_level)
  127. return 0;
  128. ret = drv_set_cca_ed_level(local, ed_level);
  129. if (!ret)
  130. wpan_phy->cca_ed_level = ed_level;
  131. return ret;
  132. }
  133. static int
  134. ieee802154_set_tx_power(struct wpan_phy *wpan_phy, s32 power)
  135. {
  136. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  137. int ret;
  138. ASSERT_RTNL();
  139. if (wpan_phy->transmit_power == power)
  140. return 0;
  141. ret = drv_set_tx_power(local, power);
  142. if (!ret)
  143. wpan_phy->transmit_power = power;
  144. return ret;
  145. }
  146. static int
  147. ieee802154_set_pan_id(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  148. __le16 pan_id)
  149. {
  150. int ret;
  151. ASSERT_RTNL();
  152. if (wpan_dev->pan_id == pan_id)
  153. return 0;
  154. ret = mac802154_wpan_update_llsec(wpan_dev->netdev);
  155. if (!ret)
  156. wpan_dev->pan_id = pan_id;
  157. return ret;
  158. }
  159. static int
  160. ieee802154_set_backoff_exponent(struct wpan_phy *wpan_phy,
  161. struct wpan_dev *wpan_dev,
  162. u8 min_be, u8 max_be)
  163. {
  164. ASSERT_RTNL();
  165. wpan_dev->min_be = min_be;
  166. wpan_dev->max_be = max_be;
  167. return 0;
  168. }
  169. static int
  170. ieee802154_set_short_addr(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  171. __le16 short_addr)
  172. {
  173. ASSERT_RTNL();
  174. wpan_dev->short_addr = short_addr;
  175. return 0;
  176. }
  177. static int
  178. ieee802154_set_max_csma_backoffs(struct wpan_phy *wpan_phy,
  179. struct wpan_dev *wpan_dev,
  180. u8 max_csma_backoffs)
  181. {
  182. ASSERT_RTNL();
  183. wpan_dev->csma_retries = max_csma_backoffs;
  184. return 0;
  185. }
  186. static int
  187. ieee802154_set_max_frame_retries(struct wpan_phy *wpan_phy,
  188. struct wpan_dev *wpan_dev,
  189. s8 max_frame_retries)
  190. {
  191. ASSERT_RTNL();
  192. wpan_dev->frame_retries = max_frame_retries;
  193. return 0;
  194. }
  195. static int
  196. ieee802154_set_lbt_mode(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  197. bool mode)
  198. {
  199. ASSERT_RTNL();
  200. wpan_dev->lbt = mode;
  201. return 0;
  202. }
  203. static int
  204. ieee802154_set_ackreq_default(struct wpan_phy *wpan_phy,
  205. struct wpan_dev *wpan_dev, bool ackreq)
  206. {
  207. ASSERT_RTNL();
  208. wpan_dev->ackreq = ackreq;
  209. return 0;
  210. }
  211. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  212. static void
  213. ieee802154_get_llsec_table(struct wpan_phy *wpan_phy,
  214. struct wpan_dev *wpan_dev,
  215. struct ieee802154_llsec_table **table)
  216. {
  217. struct net_device *dev = wpan_dev->netdev;
  218. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  219. *table = &sdata->sec.table;
  220. }
  221. static void
  222. ieee802154_lock_llsec_table(struct wpan_phy *wpan_phy,
  223. struct wpan_dev *wpan_dev)
  224. {
  225. struct net_device *dev = wpan_dev->netdev;
  226. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  227. mutex_lock(&sdata->sec_mtx);
  228. }
  229. static void
  230. ieee802154_unlock_llsec_table(struct wpan_phy *wpan_phy,
  231. struct wpan_dev *wpan_dev)
  232. {
  233. struct net_device *dev = wpan_dev->netdev;
  234. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  235. mutex_unlock(&sdata->sec_mtx);
  236. }
  237. static int
  238. ieee802154_set_llsec_params(struct wpan_phy *wpan_phy,
  239. struct wpan_dev *wpan_dev,
  240. const struct ieee802154_llsec_params *params,
  241. int changed)
  242. {
  243. struct net_device *dev = wpan_dev->netdev;
  244. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  245. int res;
  246. mutex_lock(&sdata->sec_mtx);
  247. res = mac802154_llsec_set_params(&sdata->sec, params, changed);
  248. mutex_unlock(&sdata->sec_mtx);
  249. return res;
  250. }
  251. static int
  252. ieee802154_get_llsec_params(struct wpan_phy *wpan_phy,
  253. struct wpan_dev *wpan_dev,
  254. struct ieee802154_llsec_params *params)
  255. {
  256. struct net_device *dev = wpan_dev->netdev;
  257. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  258. int res;
  259. mutex_lock(&sdata->sec_mtx);
  260. res = mac802154_llsec_get_params(&sdata->sec, params);
  261. mutex_unlock(&sdata->sec_mtx);
  262. return res;
  263. }
  264. static int
  265. ieee802154_add_llsec_key(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  266. const struct ieee802154_llsec_key_id *id,
  267. const struct ieee802154_llsec_key *key)
  268. {
  269. struct net_device *dev = wpan_dev->netdev;
  270. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  271. int res;
  272. mutex_lock(&sdata->sec_mtx);
  273. res = mac802154_llsec_key_add(&sdata->sec, id, key);
  274. mutex_unlock(&sdata->sec_mtx);
  275. return res;
  276. }
  277. static int
  278. ieee802154_del_llsec_key(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  279. const struct ieee802154_llsec_key_id *id)
  280. {
  281. struct net_device *dev = wpan_dev->netdev;
  282. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  283. int res;
  284. mutex_lock(&sdata->sec_mtx);
  285. res = mac802154_llsec_key_del(&sdata->sec, id);
  286. mutex_unlock(&sdata->sec_mtx);
  287. return res;
  288. }
  289. static int
  290. ieee802154_add_seclevel(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  291. const struct ieee802154_llsec_seclevel *sl)
  292. {
  293. struct net_device *dev = wpan_dev->netdev;
  294. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  295. int res;
  296. mutex_lock(&sdata->sec_mtx);
  297. res = mac802154_llsec_seclevel_add(&sdata->sec, sl);
  298. mutex_unlock(&sdata->sec_mtx);
  299. return res;
  300. }
  301. static int
  302. ieee802154_del_seclevel(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  303. const struct ieee802154_llsec_seclevel *sl)
  304. {
  305. struct net_device *dev = wpan_dev->netdev;
  306. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  307. int res;
  308. mutex_lock(&sdata->sec_mtx);
  309. res = mac802154_llsec_seclevel_del(&sdata->sec, sl);
  310. mutex_unlock(&sdata->sec_mtx);
  311. return res;
  312. }
  313. static int
  314. ieee802154_add_device(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  315. const struct ieee802154_llsec_device *dev_desc)
  316. {
  317. struct net_device *dev = wpan_dev->netdev;
  318. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  319. int res;
  320. mutex_lock(&sdata->sec_mtx);
  321. res = mac802154_llsec_dev_add(&sdata->sec, dev_desc);
  322. mutex_unlock(&sdata->sec_mtx);
  323. return res;
  324. }
  325. static int
  326. ieee802154_del_device(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  327. __le64 extended_addr)
  328. {
  329. struct net_device *dev = wpan_dev->netdev;
  330. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  331. int res;
  332. mutex_lock(&sdata->sec_mtx);
  333. res = mac802154_llsec_dev_del(&sdata->sec, extended_addr);
  334. mutex_unlock(&sdata->sec_mtx);
  335. return res;
  336. }
  337. static int
  338. ieee802154_add_devkey(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  339. __le64 extended_addr,
  340. const struct ieee802154_llsec_device_key *key)
  341. {
  342. struct net_device *dev = wpan_dev->netdev;
  343. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  344. int res;
  345. mutex_lock(&sdata->sec_mtx);
  346. res = mac802154_llsec_devkey_add(&sdata->sec, extended_addr, key);
  347. mutex_unlock(&sdata->sec_mtx);
  348. return res;
  349. }
  350. static int
  351. ieee802154_del_devkey(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  352. __le64 extended_addr,
  353. const struct ieee802154_llsec_device_key *key)
  354. {
  355. struct net_device *dev = wpan_dev->netdev;
  356. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  357. int res;
  358. mutex_lock(&sdata->sec_mtx);
  359. res = mac802154_llsec_devkey_del(&sdata->sec, extended_addr, key);
  360. mutex_unlock(&sdata->sec_mtx);
  361. return res;
  362. }
  363. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  364. const struct cfg802154_ops mac802154_config_ops = {
  365. .add_virtual_intf_deprecated = ieee802154_add_iface_deprecated,
  366. .del_virtual_intf_deprecated = ieee802154_del_iface_deprecated,
  367. .suspend = ieee802154_suspend,
  368. .resume = ieee802154_resume,
  369. .add_virtual_intf = ieee802154_add_iface,
  370. .del_virtual_intf = ieee802154_del_iface,
  371. .set_channel = ieee802154_set_channel,
  372. .set_cca_mode = ieee802154_set_cca_mode,
  373. .set_cca_ed_level = ieee802154_set_cca_ed_level,
  374. .set_tx_power = ieee802154_set_tx_power,
  375. .set_pan_id = ieee802154_set_pan_id,
  376. .set_short_addr = ieee802154_set_short_addr,
  377. .set_backoff_exponent = ieee802154_set_backoff_exponent,
  378. .set_max_csma_backoffs = ieee802154_set_max_csma_backoffs,
  379. .set_max_frame_retries = ieee802154_set_max_frame_retries,
  380. .set_lbt_mode = ieee802154_set_lbt_mode,
  381. .set_ackreq_default = ieee802154_set_ackreq_default,
  382. #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  383. .get_llsec_table = ieee802154_get_llsec_table,
  384. .lock_llsec_table = ieee802154_lock_llsec_table,
  385. .unlock_llsec_table = ieee802154_unlock_llsec_table,
  386. /* TODO above */
  387. .set_llsec_params = ieee802154_set_llsec_params,
  388. .get_llsec_params = ieee802154_get_llsec_params,
  389. .add_llsec_key = ieee802154_add_llsec_key,
  390. .del_llsec_key = ieee802154_del_llsec_key,
  391. .add_seclevel = ieee802154_add_seclevel,
  392. .del_seclevel = ieee802154_del_seclevel,
  393. .add_device = ieee802154_add_device,
  394. .del_device = ieee802154_del_device,
  395. .add_devkey = ieee802154_add_devkey,
  396. .del_devkey = ieee802154_del_devkey,
  397. #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  398. };