fib_notifier.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <linux/rtnetlink.h>
  2. #include <linux/notifier.h>
  3. #include <linux/rcupdate.h>
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <net/net_namespace.h>
  8. #include <net/fib_notifier.h>
  9. static ATOMIC_NOTIFIER_HEAD(fib_chain);
  10. int call_fib_notifier(struct notifier_block *nb, struct net *net,
  11. enum fib_event_type event_type,
  12. struct fib_notifier_info *info)
  13. {
  14. info->net = net;
  15. return nb->notifier_call(nb, event_type, info);
  16. }
  17. EXPORT_SYMBOL(call_fib_notifier);
  18. int call_fib_notifiers(struct net *net, enum fib_event_type event_type,
  19. struct fib_notifier_info *info)
  20. {
  21. info->net = net;
  22. return atomic_notifier_call_chain(&fib_chain, event_type, info);
  23. }
  24. EXPORT_SYMBOL(call_fib_notifiers);
  25. static unsigned int fib_seq_sum(void)
  26. {
  27. struct fib_notifier_ops *ops;
  28. unsigned int fib_seq = 0;
  29. struct net *net;
  30. rtnl_lock();
  31. for_each_net(net) {
  32. list_for_each_entry(ops, &net->fib_notifier_ops, list) {
  33. if (!try_module_get(ops->owner))
  34. continue;
  35. fib_seq += ops->fib_seq_read(net);
  36. module_put(ops->owner);
  37. }
  38. }
  39. rtnl_unlock();
  40. return fib_seq;
  41. }
  42. static int fib_net_dump(struct net *net, struct notifier_block *nb)
  43. {
  44. struct fib_notifier_ops *ops;
  45. list_for_each_entry_rcu(ops, &net->fib_notifier_ops, list) {
  46. int err;
  47. if (!try_module_get(ops->owner))
  48. continue;
  49. err = ops->fib_dump(net, nb);
  50. module_put(ops->owner);
  51. if (err)
  52. return err;
  53. }
  54. return 0;
  55. }
  56. static bool fib_dump_is_consistent(struct notifier_block *nb,
  57. void (*cb)(struct notifier_block *nb),
  58. unsigned int fib_seq)
  59. {
  60. atomic_notifier_chain_register(&fib_chain, nb);
  61. if (fib_seq == fib_seq_sum())
  62. return true;
  63. atomic_notifier_chain_unregister(&fib_chain, nb);
  64. if (cb)
  65. cb(nb);
  66. return false;
  67. }
  68. #define FIB_DUMP_MAX_RETRIES 5
  69. int register_fib_notifier(struct notifier_block *nb,
  70. void (*cb)(struct notifier_block *nb))
  71. {
  72. int retries = 0;
  73. int err;
  74. do {
  75. unsigned int fib_seq = fib_seq_sum();
  76. struct net *net;
  77. rcu_read_lock();
  78. for_each_net_rcu(net) {
  79. err = fib_net_dump(net, nb);
  80. if (err)
  81. goto err_fib_net_dump;
  82. }
  83. rcu_read_unlock();
  84. if (fib_dump_is_consistent(nb, cb, fib_seq))
  85. return 0;
  86. } while (++retries < FIB_DUMP_MAX_RETRIES);
  87. return -EBUSY;
  88. err_fib_net_dump:
  89. rcu_read_unlock();
  90. return err;
  91. }
  92. EXPORT_SYMBOL(register_fib_notifier);
  93. int unregister_fib_notifier(struct notifier_block *nb)
  94. {
  95. return atomic_notifier_chain_unregister(&fib_chain, nb);
  96. }
  97. EXPORT_SYMBOL(unregister_fib_notifier);
  98. static int __fib_notifier_ops_register(struct fib_notifier_ops *ops,
  99. struct net *net)
  100. {
  101. struct fib_notifier_ops *o;
  102. list_for_each_entry(o, &net->fib_notifier_ops, list)
  103. if (ops->family == o->family)
  104. return -EEXIST;
  105. list_add_tail_rcu(&ops->list, &net->fib_notifier_ops);
  106. return 0;
  107. }
  108. struct fib_notifier_ops *
  109. fib_notifier_ops_register(const struct fib_notifier_ops *tmpl, struct net *net)
  110. {
  111. struct fib_notifier_ops *ops;
  112. int err;
  113. ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
  114. if (!ops)
  115. return ERR_PTR(-ENOMEM);
  116. err = __fib_notifier_ops_register(ops, net);
  117. if (err)
  118. goto err_register;
  119. return ops;
  120. err_register:
  121. kfree(ops);
  122. return ERR_PTR(err);
  123. }
  124. EXPORT_SYMBOL(fib_notifier_ops_register);
  125. void fib_notifier_ops_unregister(struct fib_notifier_ops *ops)
  126. {
  127. list_del_rcu(&ops->list);
  128. kfree_rcu(ops, rcu);
  129. }
  130. EXPORT_SYMBOL(fib_notifier_ops_unregister);
  131. static int __net_init fib_notifier_net_init(struct net *net)
  132. {
  133. INIT_LIST_HEAD(&net->fib_notifier_ops);
  134. return 0;
  135. }
  136. static struct pernet_operations fib_notifier_net_ops = {
  137. .init = fib_notifier_net_init,
  138. };
  139. static int __init fib_notifier_init(void)
  140. {
  141. return register_pernet_subsys(&fib_notifier_net_ops);
  142. }
  143. subsys_initcall(fib_notifier_init);