pn_dev.c 6.3 KB

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