mib.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright 2007-2012 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Written by:
  18. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  19. * Sergey Lapin <slapin@ossfans.org>
  20. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  21. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  22. */
  23. #include <linux/if_arp.h>
  24. #include <net/mac802154.h>
  25. #include <net/ieee802154_netdev.h>
  26. #include <net/wpan-phy.h>
  27. #include "mac802154.h"
  28. struct phy_chan_notify_work {
  29. struct work_struct work;
  30. struct net_device *dev;
  31. };
  32. struct hw_addr_filt_notify_work {
  33. struct work_struct work;
  34. struct net_device *dev;
  35. unsigned long changed;
  36. };
  37. static struct mac802154_priv *mac802154_slave_get_priv(struct net_device *dev)
  38. {
  39. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  40. BUG_ON(dev->type != ARPHRD_IEEE802154);
  41. return priv->hw;
  42. }
  43. static void hw_addr_notify(struct work_struct *work)
  44. {
  45. struct hw_addr_filt_notify_work *nw = container_of(work,
  46. struct hw_addr_filt_notify_work, work);
  47. struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
  48. int res;
  49. res = hw->ops->set_hw_addr_filt(&hw->hw,
  50. &hw->hw.hw_filt,
  51. nw->changed);
  52. if (res)
  53. pr_debug("failed changed mask %lx\n", nw->changed);
  54. kfree(nw);
  55. }
  56. static void set_hw_addr_filt(struct net_device *dev, unsigned long changed)
  57. {
  58. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  59. struct hw_addr_filt_notify_work *work;
  60. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  61. if (!work)
  62. return;
  63. INIT_WORK(&work->work, hw_addr_notify);
  64. work->dev = dev;
  65. work->changed = changed;
  66. queue_work(priv->hw->dev_workqueue, &work->work);
  67. }
  68. void mac802154_dev_set_short_addr(struct net_device *dev, __le16 val)
  69. {
  70. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  71. BUG_ON(dev->type != ARPHRD_IEEE802154);
  72. spin_lock_bh(&priv->mib_lock);
  73. priv->short_addr = val;
  74. spin_unlock_bh(&priv->mib_lock);
  75. if ((priv->hw->ops->set_hw_addr_filt) &&
  76. (priv->hw->hw.hw_filt.short_addr != priv->short_addr)) {
  77. priv->hw->hw.hw_filt.short_addr = priv->short_addr;
  78. set_hw_addr_filt(dev, IEEE802515_AFILT_SADDR_CHANGED);
  79. }
  80. }
  81. __le16 mac802154_dev_get_short_addr(const struct net_device *dev)
  82. {
  83. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  84. __le16 ret;
  85. BUG_ON(dev->type != ARPHRD_IEEE802154);
  86. spin_lock_bh(&priv->mib_lock);
  87. ret = priv->short_addr;
  88. spin_unlock_bh(&priv->mib_lock);
  89. return ret;
  90. }
  91. void mac802154_dev_set_ieee_addr(struct net_device *dev)
  92. {
  93. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  94. struct mac802154_priv *mac = priv->hw;
  95. priv->extended_addr = ieee802154_devaddr_from_raw(dev->dev_addr);
  96. if (mac->ops->set_hw_addr_filt &&
  97. mac->hw.hw_filt.ieee_addr != priv->extended_addr) {
  98. mac->hw.hw_filt.ieee_addr = priv->extended_addr;
  99. set_hw_addr_filt(dev, IEEE802515_AFILT_IEEEADDR_CHANGED);
  100. }
  101. }
  102. __le16 mac802154_dev_get_pan_id(const struct net_device *dev)
  103. {
  104. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  105. __le16 ret;
  106. BUG_ON(dev->type != ARPHRD_IEEE802154);
  107. spin_lock_bh(&priv->mib_lock);
  108. ret = priv->pan_id;
  109. spin_unlock_bh(&priv->mib_lock);
  110. return ret;
  111. }
  112. void mac802154_dev_set_pan_id(struct net_device *dev, __le16 val)
  113. {
  114. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  115. BUG_ON(dev->type != ARPHRD_IEEE802154);
  116. spin_lock_bh(&priv->mib_lock);
  117. priv->pan_id = val;
  118. spin_unlock_bh(&priv->mib_lock);
  119. if ((priv->hw->ops->set_hw_addr_filt) &&
  120. (priv->hw->hw.hw_filt.pan_id != priv->pan_id)) {
  121. priv->hw->hw.hw_filt.pan_id = priv->pan_id;
  122. set_hw_addr_filt(dev, IEEE802515_AFILT_PANID_CHANGED);
  123. }
  124. }
  125. u8 mac802154_dev_get_dsn(const struct net_device *dev)
  126. {
  127. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  128. BUG_ON(dev->type != ARPHRD_IEEE802154);
  129. return priv->dsn++;
  130. }
  131. static void phy_chan_notify(struct work_struct *work)
  132. {
  133. struct phy_chan_notify_work *nw = container_of(work,
  134. struct phy_chan_notify_work, work);
  135. struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
  136. struct mac802154_sub_if_data *priv = netdev_priv(nw->dev);
  137. int res;
  138. mutex_lock(&priv->hw->phy->pib_lock);
  139. res = hw->ops->set_channel(&hw->hw, priv->page, priv->chan);
  140. if (res) {
  141. pr_debug("set_channel failed\n");
  142. } else {
  143. priv->hw->phy->current_channel = priv->chan;
  144. priv->hw->phy->current_page = priv->page;
  145. }
  146. mutex_unlock(&priv->hw->phy->pib_lock);
  147. kfree(nw);
  148. }
  149. void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan)
  150. {
  151. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  152. struct phy_chan_notify_work *work;
  153. BUG_ON(dev->type != ARPHRD_IEEE802154);
  154. spin_lock_bh(&priv->mib_lock);
  155. priv->page = page;
  156. priv->chan = chan;
  157. spin_unlock_bh(&priv->mib_lock);
  158. mutex_lock(&priv->hw->phy->pib_lock);
  159. if (priv->hw->phy->current_channel != priv->chan ||
  160. priv->hw->phy->current_page != priv->page) {
  161. mutex_unlock(&priv->hw->phy->pib_lock);
  162. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  163. if (!work)
  164. return;
  165. INIT_WORK(&work->work, phy_chan_notify);
  166. work->dev = dev;
  167. queue_work(priv->hw->dev_workqueue, &work->work);
  168. } else {
  169. mutex_unlock(&priv->hw->phy->pib_lock);
  170. }
  171. }
  172. int mac802154_get_params(struct net_device *dev,
  173. struct ieee802154_llsec_params *params)
  174. {
  175. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  176. int res;
  177. BUG_ON(dev->type != ARPHRD_IEEE802154);
  178. mutex_lock(&priv->sec_mtx);
  179. res = mac802154_llsec_get_params(&priv->sec, params);
  180. mutex_unlock(&priv->sec_mtx);
  181. return res;
  182. }
  183. int mac802154_set_params(struct net_device *dev,
  184. const struct ieee802154_llsec_params *params,
  185. int changed)
  186. {
  187. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  188. int res;
  189. BUG_ON(dev->type != ARPHRD_IEEE802154);
  190. mutex_lock(&priv->sec_mtx);
  191. res = mac802154_llsec_set_params(&priv->sec, params, changed);
  192. mutex_unlock(&priv->sec_mtx);
  193. return res;
  194. }
  195. int mac802154_add_key(struct net_device *dev,
  196. const struct ieee802154_llsec_key_id *id,
  197. const struct ieee802154_llsec_key *key)
  198. {
  199. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  200. int res;
  201. BUG_ON(dev->type != ARPHRD_IEEE802154);
  202. mutex_lock(&priv->sec_mtx);
  203. res = mac802154_llsec_key_add(&priv->sec, id, key);
  204. mutex_unlock(&priv->sec_mtx);
  205. return res;
  206. }
  207. int mac802154_del_key(struct net_device *dev,
  208. const struct ieee802154_llsec_key_id *id)
  209. {
  210. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  211. int res;
  212. BUG_ON(dev->type != ARPHRD_IEEE802154);
  213. mutex_lock(&priv->sec_mtx);
  214. res = mac802154_llsec_key_del(&priv->sec, id);
  215. mutex_unlock(&priv->sec_mtx);
  216. return res;
  217. }
  218. int mac802154_add_dev(struct net_device *dev,
  219. const struct ieee802154_llsec_device *llsec_dev)
  220. {
  221. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  222. int res;
  223. BUG_ON(dev->type != ARPHRD_IEEE802154);
  224. mutex_lock(&priv->sec_mtx);
  225. res = mac802154_llsec_dev_add(&priv->sec, llsec_dev);
  226. mutex_unlock(&priv->sec_mtx);
  227. return res;
  228. }
  229. int mac802154_del_dev(struct net_device *dev, __le64 dev_addr)
  230. {
  231. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  232. int res;
  233. BUG_ON(dev->type != ARPHRD_IEEE802154);
  234. mutex_lock(&priv->sec_mtx);
  235. res = mac802154_llsec_dev_del(&priv->sec, dev_addr);
  236. mutex_unlock(&priv->sec_mtx);
  237. return res;
  238. }
  239. int mac802154_add_devkey(struct net_device *dev,
  240. __le64 device_addr,
  241. const struct ieee802154_llsec_device_key *key)
  242. {
  243. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  244. int res;
  245. BUG_ON(dev->type != ARPHRD_IEEE802154);
  246. mutex_lock(&priv->sec_mtx);
  247. res = mac802154_llsec_devkey_add(&priv->sec, device_addr, key);
  248. mutex_unlock(&priv->sec_mtx);
  249. return res;
  250. }
  251. int mac802154_del_devkey(struct net_device *dev,
  252. __le64 device_addr,
  253. const struct ieee802154_llsec_device_key *key)
  254. {
  255. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  256. int res;
  257. BUG_ON(dev->type != ARPHRD_IEEE802154);
  258. mutex_lock(&priv->sec_mtx);
  259. res = mac802154_llsec_devkey_del(&priv->sec, device_addr, key);
  260. mutex_unlock(&priv->sec_mtx);
  261. return res;
  262. }
  263. int mac802154_add_seclevel(struct net_device *dev,
  264. const struct ieee802154_llsec_seclevel *sl)
  265. {
  266. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  267. int res;
  268. BUG_ON(dev->type != ARPHRD_IEEE802154);
  269. mutex_lock(&priv->sec_mtx);
  270. res = mac802154_llsec_seclevel_add(&priv->sec, sl);
  271. mutex_unlock(&priv->sec_mtx);
  272. return res;
  273. }
  274. int mac802154_del_seclevel(struct net_device *dev,
  275. const struct ieee802154_llsec_seclevel *sl)
  276. {
  277. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  278. int res;
  279. BUG_ON(dev->type != ARPHRD_IEEE802154);
  280. mutex_lock(&priv->sec_mtx);
  281. res = mac802154_llsec_seclevel_del(&priv->sec, sl);
  282. mutex_unlock(&priv->sec_mtx);
  283. return res;
  284. }
  285. void mac802154_lock_table(struct net_device *dev)
  286. {
  287. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  288. BUG_ON(dev->type != ARPHRD_IEEE802154);
  289. mutex_lock(&priv->sec_mtx);
  290. }
  291. void mac802154_get_table(struct net_device *dev,
  292. struct ieee802154_llsec_table **t)
  293. {
  294. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  295. BUG_ON(dev->type != ARPHRD_IEEE802154);
  296. *t = &priv->sec.table;
  297. }
  298. void mac802154_unlock_table(struct net_device *dev)
  299. {
  300. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  301. BUG_ON(dev->type != ARPHRD_IEEE802154);
  302. mutex_unlock(&priv->sec_mtx);
  303. }