pn_dev.c 6.5 KB

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