pn_dev.c 10 KB

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