pn_dev.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/netns/generic.h>
  31. #include <net/phonet/pn_dev.h>
  32. struct phonet_net {
  33. struct phonet_device_list pndevs;
  34. };
  35. int phonet_net_id;
  36. struct phonet_device_list *phonet_device_list(struct net *net)
  37. {
  38. struct phonet_net *pnn = net_generic(net, phonet_net_id);
  39. return &pnn->pndevs;
  40. }
  41. /* Allocate new Phonet device. */
  42. static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
  43. {
  44. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  45. struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
  46. if (pnd == NULL)
  47. return NULL;
  48. pnd->netdev = dev;
  49. bitmap_zero(pnd->addrs, 64);
  50. list_add(&pnd->list, &pndevs->list);
  51. return pnd;
  52. }
  53. static struct phonet_device *__phonet_get(struct net_device *dev)
  54. {
  55. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  56. struct phonet_device *pnd;
  57. list_for_each_entry(pnd, &pndevs->list, list) {
  58. if (pnd->netdev == dev)
  59. return pnd;
  60. }
  61. return NULL;
  62. }
  63. static void __phonet_device_free(struct phonet_device *pnd)
  64. {
  65. list_del(&pnd->list);
  66. kfree(pnd);
  67. }
  68. struct net_device *phonet_device_get(struct net *net)
  69. {
  70. struct phonet_device_list *pndevs = phonet_device_list(net);
  71. struct phonet_device *pnd;
  72. struct net_device *dev;
  73. spin_lock_bh(&pndevs->lock);
  74. list_for_each_entry(pnd, &pndevs->list, list) {
  75. dev = pnd->netdev;
  76. BUG_ON(!dev);
  77. if ((dev->reg_state == NETREG_REGISTERED) &&
  78. ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
  79. break;
  80. dev = NULL;
  81. }
  82. if (dev)
  83. dev_hold(dev);
  84. spin_unlock_bh(&pndevs->lock);
  85. return dev;
  86. }
  87. int phonet_address_add(struct net_device *dev, u8 addr)
  88. {
  89. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  90. struct phonet_device *pnd;
  91. int err = 0;
  92. spin_lock_bh(&pndevs->lock);
  93. /* Find or create Phonet-specific device data */
  94. pnd = __phonet_get(dev);
  95. if (pnd == NULL)
  96. pnd = __phonet_device_alloc(dev);
  97. if (unlikely(pnd == NULL))
  98. err = -ENOMEM;
  99. else if (test_and_set_bit(addr >> 2, pnd->addrs))
  100. err = -EEXIST;
  101. spin_unlock_bh(&pndevs->lock);
  102. return err;
  103. }
  104. int phonet_address_del(struct net_device *dev, u8 addr)
  105. {
  106. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  107. struct phonet_device *pnd;
  108. int err = 0;
  109. spin_lock_bh(&pndevs->lock);
  110. pnd = __phonet_get(dev);
  111. if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
  112. err = -EADDRNOTAVAIL;
  113. else if (bitmap_empty(pnd->addrs, 64))
  114. __phonet_device_free(pnd);
  115. spin_unlock_bh(&pndevs->lock);
  116. return err;
  117. }
  118. /* Gets a source address toward a destination, through a interface. */
  119. u8 phonet_address_get(struct net_device *dev, u8 addr)
  120. {
  121. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  122. struct phonet_device *pnd;
  123. spin_lock_bh(&pndevs->lock);
  124. pnd = __phonet_get(dev);
  125. if (pnd) {
  126. BUG_ON(bitmap_empty(pnd->addrs, 64));
  127. /* Use same source address as destination, if possible */
  128. if (!test_bit(addr >> 2, pnd->addrs))
  129. addr = find_first_bit(pnd->addrs, 64) << 2;
  130. } else
  131. addr = PN_NO_ADDR;
  132. spin_unlock_bh(&pndevs->lock);
  133. return addr;
  134. }
  135. int phonet_address_lookup(struct net *net, u8 addr)
  136. {
  137. struct phonet_device_list *pndevs = phonet_device_list(net);
  138. struct phonet_device *pnd;
  139. int err = -EADDRNOTAVAIL;
  140. spin_lock_bh(&pndevs->lock);
  141. list_for_each_entry(pnd, &pndevs->list, list) {
  142. /* Don't allow unregistering devices! */
  143. if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
  144. ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
  145. continue;
  146. if (test_bit(addr >> 2, pnd->addrs)) {
  147. err = 0;
  148. goto found;
  149. }
  150. }
  151. found:
  152. spin_unlock_bh(&pndevs->lock);
  153. return err;
  154. }
  155. /* notify Phonet of device events */
  156. static int phonet_device_notify(struct notifier_block *me, unsigned long what,
  157. void *arg)
  158. {
  159. struct net_device *dev = arg;
  160. if (what == NETDEV_UNREGISTER) {
  161. struct phonet_device_list *pndevs;
  162. struct phonet_device *pnd;
  163. /* Destroy phonet-specific device data */
  164. pndevs = phonet_device_list(dev_net(dev));
  165. spin_lock_bh(&pndevs->lock);
  166. pnd = __phonet_get(dev);
  167. if (pnd)
  168. __phonet_device_free(pnd);
  169. spin_unlock_bh(&pndevs->lock);
  170. }
  171. return 0;
  172. }
  173. static struct notifier_block phonet_device_notifier = {
  174. .notifier_call = phonet_device_notify,
  175. .priority = 0,
  176. };
  177. /* Per-namespace Phonet devices handling */
  178. static int phonet_init_net(struct net *net)
  179. {
  180. struct phonet_net *pnn = kmalloc(sizeof(*pnn), GFP_KERNEL);
  181. if (!pnn)
  182. return -ENOMEM;
  183. INIT_LIST_HEAD(&pnn->pndevs.list);
  184. spin_lock_init(&pnn->pndevs.lock);
  185. net_assign_generic(net, phonet_net_id, pnn);
  186. return 0;
  187. }
  188. static void phonet_exit_net(struct net *net)
  189. {
  190. struct phonet_net *pnn = net_generic(net, phonet_net_id);
  191. struct phonet_device *pnd, *n;
  192. list_for_each_entry_safe(pnd, n, &pnn->pndevs.list, list)
  193. __phonet_device_free(pnd);
  194. kfree(pnn);
  195. }
  196. static struct pernet_operations phonet_net_ops = {
  197. .init = phonet_init_net,
  198. .exit = phonet_exit_net,
  199. };
  200. /* Initialize Phonet devices list */
  201. int __init phonet_device_init(void)
  202. {
  203. int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
  204. if (err)
  205. return err;
  206. register_netdevice_notifier(&phonet_device_notifier);
  207. err = phonet_netlink_register();
  208. if (err)
  209. phonet_device_exit();
  210. return err;
  211. }
  212. void phonet_device_exit(void)
  213. {
  214. rtnl_unregister_all(PF_PHONET);
  215. unregister_netdevice_notifier(&phonet_device_notifier);
  216. unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
  217. }