pn_dev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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_routes {
  35. struct mutex lock;
  36. struct net_device *table[64];
  37. };
  38. struct phonet_net {
  39. struct phonet_device_list pndevs;
  40. struct phonet_routes routes;
  41. };
  42. int phonet_net_id;
  43. struct phonet_device_list *phonet_device_list(struct net *net)
  44. {
  45. struct phonet_net *pnn = net_generic(net, phonet_net_id);
  46. return &pnn->pndevs;
  47. }
  48. /* Allocate new Phonet device. */
  49. static struct phonet_device *__phonet_device_alloc(struct net_device *dev)
  50. {
  51. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  52. struct phonet_device *pnd = kmalloc(sizeof(*pnd), GFP_ATOMIC);
  53. if (pnd == NULL)
  54. return NULL;
  55. pnd->netdev = dev;
  56. bitmap_zero(pnd->addrs, 64);
  57. list_add(&pnd->list, &pndevs->list);
  58. return pnd;
  59. }
  60. static struct phonet_device *__phonet_get(struct net_device *dev)
  61. {
  62. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  63. struct phonet_device *pnd;
  64. list_for_each_entry(pnd, &pndevs->list, list) {
  65. if (pnd->netdev == dev)
  66. return pnd;
  67. }
  68. return NULL;
  69. }
  70. static void phonet_device_destroy(struct net_device *dev)
  71. {
  72. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  73. struct phonet_device *pnd;
  74. ASSERT_RTNL();
  75. spin_lock_bh(&pndevs->lock);
  76. pnd = __phonet_get(dev);
  77. if (pnd)
  78. list_del(&pnd->list);
  79. spin_unlock_bh(&pndevs->lock);
  80. if (pnd) {
  81. u8 addr;
  82. for (addr = find_first_bit(pnd->addrs, 64); addr < 64;
  83. addr = find_next_bit(pnd->addrs, 64, 1+addr))
  84. phonet_address_notify(RTM_DELADDR, dev, addr);
  85. kfree(pnd);
  86. }
  87. }
  88. struct net_device *phonet_device_get(struct net *net)
  89. {
  90. struct phonet_device_list *pndevs = phonet_device_list(net);
  91. struct phonet_device *pnd;
  92. struct net_device *dev = NULL;
  93. spin_lock_bh(&pndevs->lock);
  94. list_for_each_entry(pnd, &pndevs->list, list) {
  95. dev = pnd->netdev;
  96. BUG_ON(!dev);
  97. if ((dev->reg_state == NETREG_REGISTERED) &&
  98. ((pnd->netdev->flags & IFF_UP)) == IFF_UP)
  99. break;
  100. dev = NULL;
  101. }
  102. if (dev)
  103. dev_hold(dev);
  104. spin_unlock_bh(&pndevs->lock);
  105. return dev;
  106. }
  107. int phonet_address_add(struct net_device *dev, u8 addr)
  108. {
  109. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  110. struct phonet_device *pnd;
  111. int err = 0;
  112. spin_lock_bh(&pndevs->lock);
  113. /* Find or create Phonet-specific device data */
  114. pnd = __phonet_get(dev);
  115. if (pnd == NULL)
  116. pnd = __phonet_device_alloc(dev);
  117. if (unlikely(pnd == NULL))
  118. err = -ENOMEM;
  119. else if (test_and_set_bit(addr >> 2, pnd->addrs))
  120. err = -EEXIST;
  121. spin_unlock_bh(&pndevs->lock);
  122. return err;
  123. }
  124. int phonet_address_del(struct net_device *dev, u8 addr)
  125. {
  126. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  127. struct phonet_device *pnd;
  128. int err = 0;
  129. spin_lock_bh(&pndevs->lock);
  130. pnd = __phonet_get(dev);
  131. if (!pnd || !test_and_clear_bit(addr >> 2, pnd->addrs))
  132. err = -EADDRNOTAVAIL;
  133. else if (bitmap_empty(pnd->addrs, 64)) {
  134. list_del(&pnd->list);
  135. kfree(pnd);
  136. }
  137. spin_unlock_bh(&pndevs->lock);
  138. return err;
  139. }
  140. /* Gets a source address toward a destination, through a interface. */
  141. u8 phonet_address_get(struct net_device *dev, u8 daddr)
  142. {
  143. struct phonet_device_list *pndevs = phonet_device_list(dev_net(dev));
  144. struct phonet_device *pnd;
  145. u8 saddr;
  146. spin_lock_bh(&pndevs->lock);
  147. pnd = __phonet_get(dev);
  148. if (pnd) {
  149. BUG_ON(bitmap_empty(pnd->addrs, 64));
  150. /* Use same source address as destination, if possible */
  151. if (test_bit(daddr >> 2, pnd->addrs))
  152. saddr = daddr;
  153. else
  154. saddr = find_first_bit(pnd->addrs, 64) << 2;
  155. } else
  156. saddr = PN_NO_ADDR;
  157. spin_unlock_bh(&pndevs->lock);
  158. if (saddr == PN_NO_ADDR) {
  159. /* Fallback to another device */
  160. struct net_device *def_dev;
  161. def_dev = phonet_device_get(dev_net(dev));
  162. if (def_dev) {
  163. if (def_dev != dev)
  164. saddr = phonet_address_get(def_dev, daddr);
  165. dev_put(def_dev);
  166. }
  167. }
  168. return saddr;
  169. }
  170. int phonet_address_lookup(struct net *net, u8 addr)
  171. {
  172. struct phonet_device_list *pndevs = phonet_device_list(net);
  173. struct phonet_device *pnd;
  174. int err = -EADDRNOTAVAIL;
  175. spin_lock_bh(&pndevs->lock);
  176. list_for_each_entry(pnd, &pndevs->list, list) {
  177. /* Don't allow unregistering devices! */
  178. if ((pnd->netdev->reg_state != NETREG_REGISTERED) ||
  179. ((pnd->netdev->flags & IFF_UP)) != IFF_UP)
  180. continue;
  181. if (test_bit(addr >> 2, pnd->addrs)) {
  182. err = 0;
  183. goto found;
  184. }
  185. }
  186. found:
  187. spin_unlock_bh(&pndevs->lock);
  188. return err;
  189. }
  190. /* automatically configure a Phonet device, if supported */
  191. static int phonet_device_autoconf(struct net_device *dev)
  192. {
  193. struct if_phonet_req req;
  194. int ret;
  195. if (!dev->netdev_ops->ndo_do_ioctl)
  196. return -EOPNOTSUPP;
  197. ret = dev->netdev_ops->ndo_do_ioctl(dev, (struct ifreq *)&req,
  198. SIOCPNGAUTOCONF);
  199. if (ret < 0)
  200. return ret;
  201. ASSERT_RTNL();
  202. ret = phonet_address_add(dev, req.ifr_phonet_autoconf.device);
  203. if (ret)
  204. return ret;
  205. phonet_address_notify(RTM_NEWADDR, dev,
  206. req.ifr_phonet_autoconf.device);
  207. return 0;
  208. }
  209. static void phonet_route_autodel(struct net_device *dev)
  210. {
  211. struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
  212. unsigned i;
  213. DECLARE_BITMAP(deleted, 64);
  214. /* Remove left-over Phonet routes */
  215. bitmap_zero(deleted, 64);
  216. mutex_lock(&pnn->routes.lock);
  217. for (i = 0; i < 64; i++)
  218. if (dev == pnn->routes.table[i]) {
  219. rcu_assign_pointer(pnn->routes.table[i], NULL);
  220. set_bit(i, deleted);
  221. }
  222. mutex_unlock(&pnn->routes.lock);
  223. if (bitmap_empty(deleted, 64))
  224. return; /* short-circuit RCU */
  225. synchronize_rcu();
  226. for (i = find_first_bit(deleted, 64); i < 64;
  227. i = find_next_bit(deleted, 64, i + 1)) {
  228. rtm_phonet_notify(RTM_DELROUTE, dev, i);
  229. dev_put(dev);
  230. }
  231. }
  232. /* notify Phonet of device events */
  233. static int phonet_device_notify(struct notifier_block *me, unsigned long what,
  234. void *arg)
  235. {
  236. struct net_device *dev = arg;
  237. switch (what) {
  238. case NETDEV_REGISTER:
  239. if (dev->type == ARPHRD_PHONET)
  240. phonet_device_autoconf(dev);
  241. break;
  242. case NETDEV_UNREGISTER:
  243. phonet_device_destroy(dev);
  244. phonet_route_autodel(dev);
  245. break;
  246. }
  247. return 0;
  248. }
  249. static struct notifier_block phonet_device_notifier = {
  250. .notifier_call = phonet_device_notify,
  251. .priority = 0,
  252. };
  253. /* Per-namespace Phonet devices handling */
  254. static int phonet_init_net(struct net *net)
  255. {
  256. struct phonet_net *pnn = kzalloc(sizeof(*pnn), GFP_KERNEL);
  257. if (!pnn)
  258. return -ENOMEM;
  259. if (!proc_net_fops_create(net, "phonet", 0, &pn_sock_seq_fops)) {
  260. kfree(pnn);
  261. return -ENOMEM;
  262. }
  263. INIT_LIST_HEAD(&pnn->pndevs.list);
  264. spin_lock_init(&pnn->pndevs.lock);
  265. mutex_init(&pnn->routes.lock);
  266. net_assign_generic(net, phonet_net_id, pnn);
  267. return 0;
  268. }
  269. static void phonet_exit_net(struct net *net)
  270. {
  271. struct phonet_net *pnn = net_generic(net, phonet_net_id);
  272. struct net_device *dev;
  273. unsigned i;
  274. rtnl_lock();
  275. for_each_netdev(net, dev)
  276. phonet_device_destroy(dev);
  277. for (i = 0; i < 64; i++) {
  278. dev = pnn->routes.table[i];
  279. if (dev) {
  280. rtm_phonet_notify(RTM_DELROUTE, dev, i);
  281. dev_put(dev);
  282. }
  283. }
  284. rtnl_unlock();
  285. proc_net_remove(net, "phonet");
  286. kfree(pnn);
  287. }
  288. static struct pernet_operations phonet_net_ops = {
  289. .init = phonet_init_net,
  290. .exit = phonet_exit_net,
  291. };
  292. /* Initialize Phonet devices list */
  293. int __init phonet_device_init(void)
  294. {
  295. int err = register_pernet_gen_device(&phonet_net_id, &phonet_net_ops);
  296. if (err)
  297. return err;
  298. register_netdevice_notifier(&phonet_device_notifier);
  299. err = phonet_netlink_register();
  300. if (err)
  301. phonet_device_exit();
  302. return err;
  303. }
  304. void phonet_device_exit(void)
  305. {
  306. rtnl_unregister_all(PF_PHONET);
  307. unregister_netdevice_notifier(&phonet_device_notifier);
  308. unregister_pernet_gen_device(phonet_net_id, &phonet_net_ops);
  309. }
  310. int phonet_route_add(struct net_device *dev, u8 daddr)
  311. {
  312. struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
  313. struct phonet_routes *routes = &pnn->routes;
  314. int err = -EEXIST;
  315. daddr = daddr >> 2;
  316. mutex_lock(&routes->lock);
  317. if (routes->table[daddr] == NULL) {
  318. rcu_assign_pointer(routes->table[daddr], dev);
  319. dev_hold(dev);
  320. err = 0;
  321. }
  322. mutex_unlock(&routes->lock);
  323. return err;
  324. }
  325. int phonet_route_del(struct net_device *dev, u8 daddr)
  326. {
  327. struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
  328. struct phonet_routes *routes = &pnn->routes;
  329. daddr = daddr >> 2;
  330. mutex_lock(&routes->lock);
  331. if (dev == routes->table[daddr])
  332. rcu_assign_pointer(routes->table[daddr], NULL);
  333. else
  334. dev = NULL;
  335. mutex_unlock(&routes->lock);
  336. if (!dev)
  337. return -ENOENT;
  338. synchronize_rcu();
  339. dev_put(dev);
  340. return 0;
  341. }
  342. struct net_device *phonet_route_get(struct net *net, u8 daddr)
  343. {
  344. struct phonet_net *pnn = net_generic(net, phonet_net_id);
  345. struct phonet_routes *routes = &pnn->routes;
  346. struct net_device *dev;
  347. ASSERT_RTNL(); /* no need to hold the device */
  348. daddr >>= 2;
  349. rcu_read_lock();
  350. dev = rcu_dereference(routes->table[daddr]);
  351. rcu_read_unlock();
  352. return dev;
  353. }
  354. struct net_device *phonet_route_output(struct net *net, u8 daddr)
  355. {
  356. struct phonet_net *pnn = net_generic(net, phonet_net_id);
  357. struct phonet_routes *routes = &pnn->routes;
  358. struct net_device *dev;
  359. daddr >>= 2;
  360. rcu_read_lock();
  361. dev = rcu_dereference(routes->table[daddr]);
  362. if (dev)
  363. dev_hold(dev);
  364. rcu_read_unlock();
  365. if (!dev)
  366. dev = phonet_device_get(net); /* Default route */
  367. return dev;
  368. }