gateway_common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2009-2020 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  5. */
  6. #include "gateway_common.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/byteorder/generic.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/limits.h>
  13. #include <linux/math64.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/stddef.h>
  16. #include <linux/string.h>
  17. #include <uapi/linux/batadv_packet.h>
  18. #include <uapi/linux/batman_adv.h>
  19. #include "gateway_client.h"
  20. #include "log.h"
  21. #include "tvlv.h"
  22. /**
  23. * batadv_parse_throughput() - parse supplied string buffer to extract
  24. * throughput information
  25. * @net_dev: the soft interface net device
  26. * @buff: string buffer to parse
  27. * @description: text shown when throughput string cannot be parsed
  28. * @throughput: pointer holding the returned throughput information
  29. *
  30. * Return: false on parse error and true otherwise.
  31. */
  32. bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
  33. const char *description, u32 *throughput)
  34. {
  35. enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
  36. u64 lthroughput;
  37. char *tmp_ptr;
  38. int ret;
  39. if (strlen(buff) > 4) {
  40. tmp_ptr = buff + strlen(buff) - 4;
  41. if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
  42. bw_unit_type = BATADV_BW_UNIT_MBIT;
  43. if (strncasecmp(tmp_ptr, "kbit", 4) == 0 ||
  44. bw_unit_type == BATADV_BW_UNIT_MBIT)
  45. *tmp_ptr = '\0';
  46. }
  47. ret = kstrtou64(buff, 10, &lthroughput);
  48. if (ret) {
  49. batadv_err(net_dev,
  50. "Invalid throughput speed for %s: %s\n",
  51. description, buff);
  52. return false;
  53. }
  54. switch (bw_unit_type) {
  55. case BATADV_BW_UNIT_MBIT:
  56. /* prevent overflow */
  57. if (U64_MAX / 10 < lthroughput) {
  58. batadv_err(net_dev,
  59. "Throughput speed for %s too large: %s\n",
  60. description, buff);
  61. return false;
  62. }
  63. lthroughput *= 10;
  64. break;
  65. case BATADV_BW_UNIT_KBIT:
  66. default:
  67. lthroughput = div_u64(lthroughput, 100);
  68. break;
  69. }
  70. if (lthroughput > U32_MAX) {
  71. batadv_err(net_dev,
  72. "Throughput speed for %s too large: %s\n",
  73. description, buff);
  74. return false;
  75. }
  76. *throughput = lthroughput;
  77. return true;
  78. }
  79. /**
  80. * batadv_parse_gw_bandwidth() - parse supplied string buffer to extract
  81. * download and upload bandwidth information
  82. * @net_dev: the soft interface net device
  83. * @buff: string buffer to parse
  84. * @down: pointer holding the returned download bandwidth information
  85. * @up: pointer holding the returned upload bandwidth information
  86. *
  87. * Return: false on parse error and true otherwise.
  88. */
  89. static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
  90. u32 *down, u32 *up)
  91. {
  92. char *slash_ptr;
  93. bool ret;
  94. slash_ptr = strchr(buff, '/');
  95. if (slash_ptr)
  96. *slash_ptr = 0;
  97. ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
  98. down);
  99. if (!ret)
  100. return false;
  101. /* we also got some upload info */
  102. if (slash_ptr) {
  103. ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
  104. "upload gateway speed", up);
  105. if (!ret)
  106. return false;
  107. }
  108. return true;
  109. }
  110. /**
  111. * batadv_gw_tvlv_container_update() - update the gw tvlv container after
  112. * gateway setting change
  113. * @bat_priv: the bat priv with all the soft interface information
  114. */
  115. void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
  116. {
  117. struct batadv_tvlv_gateway_data gw;
  118. u32 down, up;
  119. char gw_mode;
  120. gw_mode = atomic_read(&bat_priv->gw.mode);
  121. switch (gw_mode) {
  122. case BATADV_GW_MODE_OFF:
  123. case BATADV_GW_MODE_CLIENT:
  124. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
  125. break;
  126. case BATADV_GW_MODE_SERVER:
  127. down = atomic_read(&bat_priv->gw.bandwidth_down);
  128. up = atomic_read(&bat_priv->gw.bandwidth_up);
  129. gw.bandwidth_down = htonl(down);
  130. gw.bandwidth_up = htonl(up);
  131. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
  132. &gw, sizeof(gw));
  133. break;
  134. }
  135. }
  136. /**
  137. * batadv_gw_bandwidth_set() - Parse and set download/upload gateway bandwidth
  138. * from supplied string buffer
  139. * @net_dev: netdev struct of the soft interface
  140. * @buff: the buffer containing the user data
  141. * @count: number of bytes in the buffer
  142. *
  143. * Return: 'count' on success or a negative error code in case of failure
  144. */
  145. ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
  146. size_t count)
  147. {
  148. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  149. u32 down_curr;
  150. u32 up_curr;
  151. u32 down_new = 0;
  152. u32 up_new = 0;
  153. bool ret;
  154. down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
  155. up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
  156. ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
  157. if (!ret)
  158. return -EINVAL;
  159. if (!down_new)
  160. down_new = 1;
  161. if (!up_new)
  162. up_new = down_new / 5;
  163. if (!up_new)
  164. up_new = 1;
  165. if (down_curr == down_new && up_curr == up_new)
  166. return count;
  167. batadv_gw_reselect(bat_priv);
  168. batadv_info(net_dev,
  169. "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
  170. down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
  171. down_new / 10, down_new % 10, up_new / 10, up_new % 10);
  172. atomic_set(&bat_priv->gw.bandwidth_down, down_new);
  173. atomic_set(&bat_priv->gw.bandwidth_up, up_new);
  174. batadv_gw_tvlv_container_update(bat_priv);
  175. return count;
  176. }
  177. /**
  178. * batadv_gw_tvlv_ogm_handler_v1() - process incoming gateway tvlv container
  179. * @bat_priv: the bat priv with all the soft interface information
  180. * @orig: the orig_node of the ogm
  181. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  182. * @tvlv_value: tvlv buffer containing the gateway data
  183. * @tvlv_value_len: tvlv buffer length
  184. */
  185. static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  186. struct batadv_orig_node *orig,
  187. u8 flags,
  188. void *tvlv_value, u16 tvlv_value_len)
  189. {
  190. struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
  191. /* only fetch the tvlv value if the handler wasn't called via the
  192. * CIFNOTFND flag and if there is data to fetch
  193. */
  194. if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND ||
  195. tvlv_value_len < sizeof(gateway)) {
  196. gateway.bandwidth_down = 0;
  197. gateway.bandwidth_up = 0;
  198. } else {
  199. gateway_ptr = tvlv_value;
  200. gateway.bandwidth_down = gateway_ptr->bandwidth_down;
  201. gateway.bandwidth_up = gateway_ptr->bandwidth_up;
  202. if (gateway.bandwidth_down == 0 ||
  203. gateway.bandwidth_up == 0) {
  204. gateway.bandwidth_down = 0;
  205. gateway.bandwidth_up = 0;
  206. }
  207. }
  208. batadv_gw_node_update(bat_priv, orig, &gateway);
  209. /* restart gateway selection */
  210. if (gateway.bandwidth_down != 0 &&
  211. atomic_read(&bat_priv->gw.mode) == BATADV_GW_MODE_CLIENT)
  212. batadv_gw_check_election(bat_priv, orig);
  213. }
  214. /**
  215. * batadv_gw_init() - initialise the gateway handling internals
  216. * @bat_priv: the bat priv with all the soft interface information
  217. */
  218. void batadv_gw_init(struct batadv_priv *bat_priv)
  219. {
  220. if (bat_priv->algo_ops->gw.init_sel_class)
  221. bat_priv->algo_ops->gw.init_sel_class(bat_priv);
  222. else
  223. atomic_set(&bat_priv->gw.sel_class, 1);
  224. batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
  225. NULL, BATADV_TVLV_GW, 1,
  226. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  227. }
  228. /**
  229. * batadv_gw_free() - free the gateway handling internals
  230. * @bat_priv: the bat priv with all the soft interface information
  231. */
  232. void batadv_gw_free(struct batadv_priv *bat_priv)
  233. {
  234. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
  235. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
  236. }