mib.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. int mac802154_get_params(struct net_device *dev,
  172. struct ieee802154_llsec_params *params)
  173. {
  174. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  175. int res;
  176. BUG_ON(dev->type != ARPHRD_IEEE802154);
  177. mutex_lock(&priv->sec_mtx);
  178. res = mac802154_llsec_get_params(&priv->sec, params);
  179. mutex_unlock(&priv->sec_mtx);
  180. return res;
  181. }
  182. int mac802154_set_params(struct net_device *dev,
  183. const struct ieee802154_llsec_params *params,
  184. int changed)
  185. {
  186. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  187. int res;
  188. BUG_ON(dev->type != ARPHRD_IEEE802154);
  189. mutex_lock(&priv->sec_mtx);
  190. res = mac802154_llsec_set_params(&priv->sec, params, changed);
  191. mutex_unlock(&priv->sec_mtx);
  192. return res;
  193. }
  194. int mac802154_add_key(struct net_device *dev,
  195. const struct ieee802154_llsec_key_id *id,
  196. const struct ieee802154_llsec_key *key)
  197. {
  198. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  199. int res;
  200. BUG_ON(dev->type != ARPHRD_IEEE802154);
  201. mutex_lock(&priv->sec_mtx);
  202. res = mac802154_llsec_key_add(&priv->sec, id, key);
  203. mutex_unlock(&priv->sec_mtx);
  204. return res;
  205. }
  206. int mac802154_del_key(struct net_device *dev,
  207. const struct ieee802154_llsec_key_id *id)
  208. {
  209. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  210. int res;
  211. BUG_ON(dev->type != ARPHRD_IEEE802154);
  212. mutex_lock(&priv->sec_mtx);
  213. res = mac802154_llsec_key_del(&priv->sec, id);
  214. mutex_unlock(&priv->sec_mtx);
  215. return res;
  216. }
  217. int mac802154_add_dev(struct net_device *dev,
  218. const struct ieee802154_llsec_device *llsec_dev)
  219. {
  220. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  221. int res;
  222. BUG_ON(dev->type != ARPHRD_IEEE802154);
  223. mutex_lock(&priv->sec_mtx);
  224. res = mac802154_llsec_dev_add(&priv->sec, llsec_dev);
  225. mutex_unlock(&priv->sec_mtx);
  226. return res;
  227. }
  228. int mac802154_del_dev(struct net_device *dev, __le64 dev_addr)
  229. {
  230. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  231. int res;
  232. BUG_ON(dev->type != ARPHRD_IEEE802154);
  233. mutex_lock(&priv->sec_mtx);
  234. res = mac802154_llsec_dev_del(&priv->sec, dev_addr);
  235. mutex_unlock(&priv->sec_mtx);
  236. return res;
  237. }
  238. int mac802154_add_devkey(struct net_device *dev,
  239. __le64 device_addr,
  240. const struct ieee802154_llsec_device_key *key)
  241. {
  242. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  243. int res;
  244. BUG_ON(dev->type != ARPHRD_IEEE802154);
  245. mutex_lock(&priv->sec_mtx);
  246. res = mac802154_llsec_devkey_add(&priv->sec, device_addr, key);
  247. mutex_unlock(&priv->sec_mtx);
  248. return res;
  249. }
  250. int mac802154_del_devkey(struct net_device *dev,
  251. __le64 device_addr,
  252. const struct ieee802154_llsec_device_key *key)
  253. {
  254. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  255. int res;
  256. BUG_ON(dev->type != ARPHRD_IEEE802154);
  257. mutex_lock(&priv->sec_mtx);
  258. res = mac802154_llsec_devkey_del(&priv->sec, device_addr, key);
  259. mutex_unlock(&priv->sec_mtx);
  260. return res;
  261. }
  262. int mac802154_add_seclevel(struct net_device *dev,
  263. const struct ieee802154_llsec_seclevel *sl)
  264. {
  265. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  266. int res;
  267. BUG_ON(dev->type != ARPHRD_IEEE802154);
  268. mutex_lock(&priv->sec_mtx);
  269. res = mac802154_llsec_seclevel_add(&priv->sec, sl);
  270. mutex_unlock(&priv->sec_mtx);
  271. return res;
  272. }
  273. int mac802154_del_seclevel(struct net_device *dev,
  274. const struct ieee802154_llsec_seclevel *sl)
  275. {
  276. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  277. int res;
  278. BUG_ON(dev->type != ARPHRD_IEEE802154);
  279. mutex_lock(&priv->sec_mtx);
  280. res = mac802154_llsec_seclevel_del(&priv->sec, sl);
  281. mutex_unlock(&priv->sec_mtx);
  282. return res;
  283. }
  284. void mac802154_lock_table(struct net_device *dev)
  285. {
  286. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  287. BUG_ON(dev->type != ARPHRD_IEEE802154);
  288. mutex_lock(&priv->sec_mtx);
  289. }
  290. void mac802154_get_table(struct net_device *dev,
  291. struct ieee802154_llsec_table **t)
  292. {
  293. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  294. BUG_ON(dev->type != ARPHRD_IEEE802154);
  295. *t = &priv->sec.table;
  296. }
  297. void mac802154_unlock_table(struct net_device *dev)
  298. {
  299. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  300. BUG_ON(dev->type != ARPHRD_IEEE802154);
  301. mutex_unlock(&priv->sec_mtx);
  302. }