bat_algo.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. */
  6. #include "main.h"
  7. #include <linux/errno.h>
  8. #include <linux/list.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/netlink.h>
  11. #include <linux/printk.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/stddef.h>
  15. #include <linux/string.h>
  16. #include <net/genetlink.h>
  17. #include <net/netlink.h>
  18. #include <uapi/linux/batman_adv.h>
  19. #include "bat_algo.h"
  20. #include "netlink.h"
  21. char batadv_routing_algo[20] = "BATMAN_IV";
  22. static struct hlist_head batadv_algo_list;
  23. /**
  24. * batadv_algo_init() - Initialize batman-adv algorithm management data
  25. * structures
  26. */
  27. void batadv_algo_init(void)
  28. {
  29. INIT_HLIST_HEAD(&batadv_algo_list);
  30. }
  31. static struct batadv_algo_ops *batadv_algo_get(char *name)
  32. {
  33. struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  34. hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
  35. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  36. continue;
  37. bat_algo_ops = bat_algo_ops_tmp;
  38. break;
  39. }
  40. return bat_algo_ops;
  41. }
  42. /**
  43. * batadv_algo_register() - Register callbacks for a mesh algorithm
  44. * @bat_algo_ops: mesh algorithm callbacks to add
  45. *
  46. * Return: 0 on success or negative error number in case of failure
  47. */
  48. int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
  49. {
  50. struct batadv_algo_ops *bat_algo_ops_tmp;
  51. bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
  52. if (bat_algo_ops_tmp) {
  53. pr_info("Trying to register already registered routing algorithm: %s\n",
  54. bat_algo_ops->name);
  55. return -EEXIST;
  56. }
  57. /* all algorithms must implement all ops (for now) */
  58. if (!bat_algo_ops->iface.enable ||
  59. !bat_algo_ops->iface.disable ||
  60. !bat_algo_ops->iface.update_mac ||
  61. !bat_algo_ops->iface.primary_set ||
  62. !bat_algo_ops->neigh.cmp ||
  63. !bat_algo_ops->neigh.is_similar_or_better) {
  64. pr_info("Routing algo '%s' does not implement required ops\n",
  65. bat_algo_ops->name);
  66. return -EINVAL;
  67. }
  68. INIT_HLIST_NODE(&bat_algo_ops->list);
  69. hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  70. return 0;
  71. }
  72. /**
  73. * batadv_algo_select() - Select algorithm of soft interface
  74. * @bat_priv: the bat priv with all the soft interface information
  75. * @name: name of the algorithm to select
  76. *
  77. * The algorithm callbacks for the soft interface will be set when the algorithm
  78. * with the correct name was found. Any previous selected algorithm will not be
  79. * deinitialized and the new selected algorithm will also not be initialized.
  80. * It is therefore not allowed to call batadv_algo_select outside the creation
  81. * function of the soft interface.
  82. *
  83. * Return: 0 on success or negative error number in case of failure
  84. */
  85. int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
  86. {
  87. struct batadv_algo_ops *bat_algo_ops;
  88. bat_algo_ops = batadv_algo_get(name);
  89. if (!bat_algo_ops)
  90. return -EINVAL;
  91. bat_priv->algo_ops = bat_algo_ops;
  92. return 0;
  93. }
  94. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  95. /**
  96. * batadv_algo_seq_print_text() - Print the supported algorithms in a seq file
  97. * @seq: seq file to print on
  98. * @offset: not used
  99. *
  100. * Return: always 0
  101. */
  102. int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
  103. {
  104. struct batadv_algo_ops *bat_algo_ops;
  105. seq_puts(seq, "Available routing algorithms:\n");
  106. hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  107. seq_printf(seq, " * %s\n", bat_algo_ops->name);
  108. }
  109. return 0;
  110. }
  111. #endif
  112. static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  113. {
  114. struct batadv_algo_ops *bat_algo_ops;
  115. char *algo_name = (char *)val;
  116. size_t name_len = strlen(algo_name);
  117. if (name_len > 0 && algo_name[name_len - 1] == '\n')
  118. algo_name[name_len - 1] = '\0';
  119. bat_algo_ops = batadv_algo_get(algo_name);
  120. if (!bat_algo_ops) {
  121. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  122. return -EINVAL;
  123. }
  124. return param_set_copystring(algo_name, kp);
  125. }
  126. static const struct kernel_param_ops batadv_param_ops_ra = {
  127. .set = batadv_param_set_ra,
  128. .get = param_get_string,
  129. };
  130. static struct kparam_string batadv_param_string_ra = {
  131. .maxlen = sizeof(batadv_routing_algo),
  132. .string = batadv_routing_algo,
  133. };
  134. module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  135. 0644);
  136. /**
  137. * batadv_algo_dump_entry() - fill in information about one supported routing
  138. * algorithm
  139. * @msg: netlink message to be sent back
  140. * @portid: Port to reply to
  141. * @seq: Sequence number of message
  142. * @bat_algo_ops: Algorithm to be dumped
  143. *
  144. * Return: Error number, or 0 on success
  145. */
  146. static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  147. struct batadv_algo_ops *bat_algo_ops)
  148. {
  149. void *hdr;
  150. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  151. NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS);
  152. if (!hdr)
  153. return -EMSGSIZE;
  154. if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name))
  155. goto nla_put_failure;
  156. genlmsg_end(msg, hdr);
  157. return 0;
  158. nla_put_failure:
  159. genlmsg_cancel(msg, hdr);
  160. return -EMSGSIZE;
  161. }
  162. /**
  163. * batadv_algo_dump() - fill in information about supported routing
  164. * algorithms
  165. * @msg: netlink message to be sent back
  166. * @cb: Parameters to the netlink request
  167. *
  168. * Return: Length of reply message.
  169. */
  170. int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb)
  171. {
  172. int portid = NETLINK_CB(cb->skb).portid;
  173. struct batadv_algo_ops *bat_algo_ops;
  174. int skip = cb->args[0];
  175. int i = 0;
  176. hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  177. if (i++ < skip)
  178. continue;
  179. if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
  180. bat_algo_ops)) {
  181. i--;
  182. break;
  183. }
  184. }
  185. cb->args[0] = i;
  186. return msg->len;
  187. }