pn_dev.c 10 KB

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