pn_dev.c 7.2 KB

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