pn_dev.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * File: pn_dev.c
  3. *
  4. * Phonet network device
  5. *
  6. * Copyright (C) 2008 Nokia Corporation.
  7. *
  8. * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
  9. * Original author: Sakari Ailus <sakari.ailus@nokia.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * version 2 as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. * 02110-1301 USA
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/net.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/phonet.h>
  29. #include <net/sock.h>
  30. #include <net/phonet/pn_dev.h>
  31. /* when accessing, remember to lock with spin_lock(&pndevs.lock); */
  32. struct phonet_device_list pndevs = {
  33. .list = LIST_HEAD_INIT(pndevs.list),
  34. .lock = __SPIN_LOCK_UNLOCKED(pndevs.lock),
  35. };
  36. /* Allocate new Phonet device. */
  37. static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
  38. {
  39. struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
  40. if (pnd == NULL)
  41. return NULL;
  42. pnd->netdev = dev;
  43. bitmap_zero(pnd->addrs, 64);
  44. list_add(&pnd->list, &pndevs.list);
  45. return pnd;
  46. }
  47. static struct phonet_device *__phonet_get(struct net_device *dev)
  48. {
  49. struct phonet_device *pnd;
  50. list_for_each_entry(pnd, &pndevs.list, list) {
  51. if (pnd->netdev == dev)
  52. return pnd;
  53. }
  54. return NULL;
  55. }
  56. static void __phonet_device_free(struct phonet_device *pnd)
  57. {
  58. list_del(&pnd->list);
  59. kfree(pnd);
  60. }
  61. struct net_device *phonet_device_get(struct net *net)
  62. {
  63. struct phonet_device *pnd;
  64. struct net_device *dev;
  65. spin_lock_bh(&pndevs.lock);
  66. list_for_each_entry(pnd, &pndevs.list, list) {
  67. dev = pnd->netdev;
  68. BUG_ON(!dev);
  69. if (dev_net(dev) == net &&
  70. (dev->reg_state == NETREG_REGISTERED) &&
  71. ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
  72. break;
  73. dev = NULL;
  74. }
  75. if (dev)
  76. dev_hold(dev);
  77. spin_unlock_bh(&pndevs.lock);
  78. return dev;
  79. }
  80. int phonet_address_add(struct net_device *dev, u8 addr)
  81. {
  82. struct phonet_device *pnd;
  83. int err = 0;
  84. spin_lock_bh(&pndevs.lock);
  85. /* Find or create Phonet-specific device data */
  86. pnd = __phonet_get(dev);
  87. if (pnd == NULL)
  88. pnd = __phonet_device_alloc(dev);
  89. if (unlikely(pnd == NULL))
  90. err = -ENOMEM;
  91. else if (test_and_set_bit(addr >> 2, pnd->addrs))
  92. err = -EEXIST;
  93. spin_unlock_bh(&pndevs.lock);
  94. return err;
  95. }
  96. int phonet_address_del(struct net_device *dev, u8 addr)
  97. {
  98. struct phonet_device *pnd;
  99. int err = 0;
  100. spin_lock_bh(&pndevs.lock);
  101. pnd = __phonet_get(dev);
  102. if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
  103. err = -EADDRNOTAVAIL;
  104. if (bitmap_empty(pnd->addrs, 64))
  105. __phonet_device_free(pnd);
  106. spin_unlock_bh(&pndevs.lock);
  107. return err;
  108. }
  109. /* Gets a source address toward a destination, through a interface. */
  110. u8 phonet_address_get(struct net_device *dev, u8 addr)
  111. {
  112. struct phonet_device *pnd;
  113. spin_lock_bh(&pndevs.lock);
  114. pnd = __phonet_get(dev);
  115. if (pnd) {
  116. BUG_ON(bitmap_empty(pnd->addrs, 64));
  117. /* Use same source address as destination, if possible */
  118. if (!test_bit(addr >> 2, pnd->addrs))
  119. addr = find_first_bit(pnd->addrs, 64) << 2;
  120. } else
  121. addr = PN_NO_ADDR;
  122. spin_unlock_bh(&pndevs.lock);
  123. return addr;
  124. }
  125. int phonet_address_lookup(u8 addr)
  126. {
  127. struct phonet_device *pnd;
  128. spin_lock_bh(&pndevs.lock);
  129. list_for_each_entry(pnd, &pndevs.list, list) {
  130. /* Don't allow unregistering devices! */
  131. if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
  132. ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
  133. continue;
  134. if (test_bit(addr >> 2, pnd->addrs)) {
  135. spin_unlock_bh(&pndevs.lock);
  136. return 0;
  137. }
  138. }
  139. spin_unlock_bh(&pndevs.lock);
  140. return -EADDRNOTAVAIL;
  141. }
  142. /* notify Phonet of device events */
  143. static int phonet_device_notify(struct notifier_block *me, unsigned long what,
  144. void *arg)
  145. {
  146. struct net_device *dev = arg;
  147. if (what == NETDEV_UNREGISTER) {
  148. struct phonet_device *pnd;
  149. /* Destroy phonet-specific device data */
  150. spin_lock_bh(&pndevs.lock);
  151. pnd = __phonet_get(dev);
  152. if (pnd)
  153. __phonet_device_free(pnd);
  154. spin_unlock_bh(&pndevs.lock);
  155. }
  156. return 0;
  157. }
  158. static struct notifier_block phonet_device_notifier = {
  159. .notifier_call = phonet_device_notify,
  160. .priority = 0,
  161. };
  162. /* Initialize Phonet devices list */
  163. void phonet_device_init(void)
  164. {
  165. register_netdevice_notifier(&phonet_device_notifier);
  166. }
  167. void phonet_device_exit(void)
  168. {
  169. struct phonet_device *pnd, *n;
  170. rtnl_unregister_all(PF_PHONET);
  171. rtnl_lock();
  172. spin_lock_bh(&pndevs.lock);
  173. list_for_each_entry_safe(pnd, n, &pndevs.list, list)
  174. __phonet_device_free(pnd);
  175. spin_unlock_bh(&pndevs.lock);
  176. rtnl_unlock();
  177. unregister_netdevice_notifier(&phonet_device_notifier);
  178. }