bat_algo.c 6.1 KB

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