bat_algo.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 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/skbuff.h>
  13. #include <linux/stddef.h>
  14. #include <linux/string.h>
  15. #include <linux/types.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. /**
  32. * batadv_algo_get() - Search for algorithm with specific name
  33. * @name: algorithm name to find
  34. *
  35. * Return: Pointer to batadv_algo_ops on success, NULL otherwise
  36. */
  37. struct batadv_algo_ops *batadv_algo_get(const char *name)
  38. {
  39. struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  40. hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
  41. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  42. continue;
  43. bat_algo_ops = bat_algo_ops_tmp;
  44. break;
  45. }
  46. return bat_algo_ops;
  47. }
  48. /**
  49. * batadv_algo_register() - Register callbacks for a mesh algorithm
  50. * @bat_algo_ops: mesh algorithm callbacks to add
  51. *
  52. * Return: 0 on success or negative error number in case of failure
  53. */
  54. int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
  55. {
  56. struct batadv_algo_ops *bat_algo_ops_tmp;
  57. bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
  58. if (bat_algo_ops_tmp) {
  59. pr_info("Trying to register already registered routing algorithm: %s\n",
  60. bat_algo_ops->name);
  61. return -EEXIST;
  62. }
  63. /* all algorithms must implement all ops (for now) */
  64. if (!bat_algo_ops->iface.enable ||
  65. !bat_algo_ops->iface.disable ||
  66. !bat_algo_ops->iface.update_mac ||
  67. !bat_algo_ops->iface.primary_set ||
  68. !bat_algo_ops->neigh.cmp ||
  69. !bat_algo_ops->neigh.is_similar_or_better) {
  70. pr_info("Routing algo '%s' does not implement required ops\n",
  71. bat_algo_ops->name);
  72. return -EINVAL;
  73. }
  74. INIT_HLIST_NODE(&bat_algo_ops->list);
  75. hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  76. return 0;
  77. }
  78. /**
  79. * batadv_algo_select() - Select algorithm of mesh interface
  80. * @bat_priv: the bat priv with all the mesh interface information
  81. * @name: name of the algorithm to select
  82. *
  83. * The algorithm callbacks for the mesh interface will be set when the algorithm
  84. * with the correct name was found. Any previous selected algorithm will not be
  85. * deinitialized and the new selected algorithm will also not be initialized.
  86. * It is therefore not allowed to call batadv_algo_select outside the creation
  87. * function of the mesh interface.
  88. *
  89. * Return: 0 on success or negative error number in case of failure
  90. */
  91. int batadv_algo_select(struct batadv_priv *bat_priv, const char *name)
  92. {
  93. struct batadv_algo_ops *bat_algo_ops;
  94. bat_algo_ops = batadv_algo_get(name);
  95. if (!bat_algo_ops)
  96. return -EINVAL;
  97. bat_priv->algo_ops = bat_algo_ops;
  98. return 0;
  99. }
  100. static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  101. {
  102. struct batadv_algo_ops *bat_algo_ops;
  103. char *algo_name = (char *)val;
  104. size_t name_len = strlen(algo_name);
  105. if (name_len > 0 && algo_name[name_len - 1] == '\n')
  106. algo_name[name_len - 1] = '\0';
  107. bat_algo_ops = batadv_algo_get(algo_name);
  108. if (!bat_algo_ops) {
  109. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  110. return -EINVAL;
  111. }
  112. return param_set_copystring(algo_name, kp);
  113. }
  114. static const struct kernel_param_ops batadv_param_ops_ra = {
  115. .set = batadv_param_set_ra,
  116. .get = param_get_string,
  117. };
  118. static struct kparam_string batadv_param_string_ra = {
  119. .maxlen = sizeof(batadv_routing_algo),
  120. .string = batadv_routing_algo,
  121. };
  122. module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  123. 0644);
  124. /**
  125. * batadv_algo_dump_entry() - fill in information about one supported routing
  126. * algorithm
  127. * @msg: netlink message to be sent back
  128. * @portid: Port to reply to
  129. * @seq: Sequence number of message
  130. * @bat_algo_ops: Algorithm to be dumped
  131. *
  132. * Return: Error number, or 0 on success
  133. */
  134. static int batadv_algo_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  135. struct batadv_algo_ops *bat_algo_ops)
  136. {
  137. void *hdr;
  138. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  139. NLM_F_MULTI, BATADV_CMD_GET_ROUTING_ALGOS);
  140. if (!hdr)
  141. return -EMSGSIZE;
  142. if (nla_put_string(msg, BATADV_ATTR_ALGO_NAME, bat_algo_ops->name))
  143. goto nla_put_failure;
  144. genlmsg_end(msg, hdr);
  145. return 0;
  146. nla_put_failure:
  147. genlmsg_cancel(msg, hdr);
  148. return -EMSGSIZE;
  149. }
  150. /**
  151. * batadv_algo_dump() - fill in information about supported routing
  152. * algorithms
  153. * @msg: netlink message to be sent back
  154. * @cb: Parameters to the netlink request
  155. *
  156. * Return: Length of reply message.
  157. */
  158. int batadv_algo_dump(struct sk_buff *msg, struct netlink_callback *cb)
  159. {
  160. int portid = NETLINK_CB(cb->skb).portid;
  161. struct batadv_algo_ops *bat_algo_ops;
  162. int skip = cb->args[0];
  163. int i = 0;
  164. hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  165. if (i++ < skip)
  166. continue;
  167. if (batadv_algo_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
  168. bat_algo_ops)) {
  169. i--;
  170. break;
  171. }
  172. }
  173. cb->args[0] = i;
  174. return msg->len;
  175. }