l3mdev.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * net/l3mdev/l3mdev.c - L3 master device implementation
  3. * Copyright (c) 2015 Cumulus Networks
  4. * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/netdevice.h>
  12. #include <net/l3mdev.h>
  13. /**
  14. * l3mdev_master_ifindex - get index of L3 master device
  15. * @dev: targeted interface
  16. */
  17. int l3mdev_master_ifindex_rcu(const struct net_device *dev)
  18. {
  19. int ifindex = 0;
  20. if (!dev)
  21. return 0;
  22. if (netif_is_l3_master(dev)) {
  23. ifindex = dev->ifindex;
  24. } else if (netif_is_l3_slave(dev)) {
  25. struct net_device *master;
  26. struct net_device *_dev = (struct net_device *)dev;
  27. /* netdev_master_upper_dev_get_rcu calls
  28. * list_first_or_null_rcu to walk the upper dev list.
  29. * list_first_or_null_rcu does not handle a const arg. We aren't
  30. * making changes, just want the master device from that list so
  31. * typecast to remove the const
  32. */
  33. master = netdev_master_upper_dev_get_rcu(_dev);
  34. if (master)
  35. ifindex = master->ifindex;
  36. }
  37. return ifindex;
  38. }
  39. EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu);
  40. /**
  41. * l3mdev_fib_table - get FIB table id associated with an L3
  42. * master interface
  43. * @dev: targeted interface
  44. */
  45. u32 l3mdev_fib_table_rcu(const struct net_device *dev)
  46. {
  47. u32 tb_id = 0;
  48. if (!dev)
  49. return 0;
  50. if (netif_is_l3_master(dev)) {
  51. if (dev->l3mdev_ops->l3mdev_fib_table)
  52. tb_id = dev->l3mdev_ops->l3mdev_fib_table(dev);
  53. } else if (netif_is_l3_slave(dev)) {
  54. /* Users of netdev_master_upper_dev_get_rcu need non-const,
  55. * but current inet_*type functions take a const
  56. */
  57. struct net_device *_dev = (struct net_device *) dev;
  58. const struct net_device *master;
  59. master = netdev_master_upper_dev_get_rcu(_dev);
  60. if (master &&
  61. master->l3mdev_ops->l3mdev_fib_table)
  62. tb_id = master->l3mdev_ops->l3mdev_fib_table(master);
  63. }
  64. return tb_id;
  65. }
  66. EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu);
  67. u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
  68. {
  69. struct net_device *dev;
  70. u32 tb_id = 0;
  71. if (!ifindex)
  72. return 0;
  73. rcu_read_lock();
  74. dev = dev_get_by_index_rcu(net, ifindex);
  75. if (dev)
  76. tb_id = l3mdev_fib_table_rcu(dev);
  77. rcu_read_unlock();
  78. return tb_id;
  79. }
  80. EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index);