bat_algo.c 5.2 KB

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