bat_algo.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "main.h"
  18. #include <linux/errno.h>
  19. #include <linux/list.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/netlink.h>
  22. #include <linux/printk.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/stddef.h>
  26. #include <linux/string.h>
  27. #include <net/genetlink.h>
  28. #include <net/netlink.h>
  29. #include <uapi/linux/batman_adv.h>
  30. #include "bat_algo.h"
  31. #include "netlink.h"
  32. char batadv_routing_algo[20] = "BATMAN_IV";
  33. static struct hlist_head batadv_algo_list;
  34. /**
  35. * batadv_algo_init - Initialize batman-adv algorithm management data structures
  36. */
  37. void batadv_algo_init(void)
  38. {
  39. INIT_HLIST_HEAD(&batadv_algo_list);
  40. }
  41. static struct batadv_algo_ops *batadv_algo_get(char *name)
  42. {
  43. struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  44. hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
  45. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  46. continue;
  47. bat_algo_ops = bat_algo_ops_tmp;
  48. break;
  49. }
  50. return bat_algo_ops;
  51. }
  52. int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
  53. {
  54. struct batadv_algo_ops *bat_algo_ops_tmp;
  55. bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
  56. if (bat_algo_ops_tmp) {
  57. pr_info("Trying to register already registered routing algorithm: %s\n",
  58. bat_algo_ops->name);
  59. return -EEXIST;
  60. }
  61. /* all algorithms must implement all ops (for now) */
  62. if (!bat_algo_ops->iface.enable ||
  63. !bat_algo_ops->iface.disable ||
  64. !bat_algo_ops->iface.update_mac ||
  65. !bat_algo_ops->iface.primary_set ||
  66. !bat_algo_ops->neigh.cmp ||
  67. !bat_algo_ops->neigh.is_similar_or_better) {
  68. pr_info("Routing algo '%s' does not implement required ops\n",
  69. bat_algo_ops->name);
  70. return -EINVAL;
  71. }
  72. INIT_HLIST_NODE(&bat_algo_ops->list);
  73. hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  74. return 0;
  75. }
  76. int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
  77. {
  78. struct batadv_algo_ops *bat_algo_ops;
  79. bat_algo_ops = batadv_algo_get(name);
  80. if (!bat_algo_ops)
  81. return -EINVAL;
  82. bat_priv->algo_ops = bat_algo_ops;
  83. return 0;
  84. }
  85. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  86. int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
  87. {
  88. struct batadv_algo_ops *bat_algo_ops;
  89. seq_puts(seq, "Available routing algorithms:\n");
  90. hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  91. seq_printf(seq, " * %s\n", bat_algo_ops->name);
  92. }
  93. return 0;
  94. }
  95. #endif
  96. static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  97. {
  98. struct batadv_algo_ops *bat_algo_ops;
  99. char *algo_name = (char *)val;
  100. size_t name_len = strlen(algo_name);
  101. if (name_len > 0 && algo_name[name_len - 1] == '\n')
  102. algo_name[name_len - 1] = '\0';
  103. bat_algo_ops = batadv_algo_get(algo_name);
  104. if (!bat_algo_ops) {
  105. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  106. return -EINVAL;
  107. }
  108. return param_set_copystring(algo_name, kp);
  109. }
  110. static const struct kernel_param_ops batadv_param_ops_ra = {
  111. .set = batadv_param_set_ra,
  112. .get = param_get_string,
  113. };
  114. static struct kparam_string batadv_param_string_ra = {
  115. .maxlen = sizeof(batadv_routing_algo),
  116. .string = batadv_routing_algo,
  117. };
  118. module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  119. 0644);
  120. /**
  121. * batadv_algo_dump_entry - fill in information about one supported routing
  122. * algorithm
  123. * @msg: netlink message to be sent back
  124. * @portid: Port to reply to
  125. * @seq: Sequence number of message
  126. * @bat_algo_ops: Algorithm to be dumped
  127. *
  128. * Return: Error number, or 0 on success
  129. */
  130. static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  131. struct batadv_algo_ops *bat_algo_ops)
  132. {
  133. void *hdr;
  134. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  135. NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS);
  136. if (!hdr)
  137. return -EMSGSIZE;
  138. if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name))
  139. goto nla_put_failure;
  140. genlmsg_end(msg, hdr);
  141. return 0;
  142. nla_put_failure:
  143. genlmsg_cancel(msg, hdr);
  144. return -EMSGSIZE;
  145. }
  146. /**
  147. * batadv_algo_dump - fill in information about supported routing
  148. * algorithms
  149. * @msg: netlink message to be sent back
  150. * @cb: Parameters to the netlink request
  151. *
  152. * Return: Length of reply message.
  153. */
  154. int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb)
  155. {
  156. int portid = NETLINK_CB(cb->skb).portid;
  157. struct batadv_algo_ops *bat_algo_ops;
  158. int skip = cb->args[0];
  159. int i = 0;
  160. hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  161. if (i++ < skip)
  162. continue;
  163. if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
  164. bat_algo_ops)) {
  165. i--;
  166. break;
  167. }
  168. }
  169. cb->args[0] = i;
  170. return msg->len;
  171. }