pn_dev.c 10 KB

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