gateway_common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Copyright (C) 2009-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "gateway_common.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/byteorder/generic.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/math64.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/stddef.h>
  26. #include <linux/string.h>
  27. #include "gateway_client.h"
  28. #include "log.h"
  29. #include "packet.h"
  30. #include "tvlv.h"
  31. /**
  32. * batadv_parse_throughput - parse supplied string buffer to extract throughput
  33. * information
  34. * @net_dev: the soft interface net device
  35. * @buff: string buffer to parse
  36. * @description: text shown when throughput string cannot be parsed
  37. * @throughput: pointer holding the returned throughput information
  38. *
  39. * Return: false on parse error and true otherwise.
  40. */
  41. bool batadv_parse_throughput(struct net_device *net_dev, char *buff,
  42. const char *description, u32 *throughput)
  43. {
  44. enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
  45. u64 lthroughput;
  46. char *tmp_ptr;
  47. int ret;
  48. if (strlen(buff) > 4) {
  49. tmp_ptr = buff + strlen(buff) - 4;
  50. if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
  51. bw_unit_type = BATADV_BW_UNIT_MBIT;
  52. if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
  53. (bw_unit_type == BATADV_BW_UNIT_MBIT))
  54. *tmp_ptr = '\0';
  55. }
  56. ret = kstrtou64(buff, 10, &lthroughput);
  57. if (ret) {
  58. batadv_err(net_dev,
  59. "Invalid throughput speed for %s: %s\n",
  60. description, buff);
  61. return false;
  62. }
  63. switch (bw_unit_type) {
  64. case BATADV_BW_UNIT_MBIT:
  65. /* prevent overflow */
  66. if (U64_MAX / 10 < lthroughput) {
  67. batadv_err(net_dev,
  68. "Throughput speed for %s too large: %s\n",
  69. description, buff);
  70. return false;
  71. }
  72. lthroughput *= 10;
  73. break;
  74. case BATADV_BW_UNIT_KBIT:
  75. default:
  76. lthroughput = div_u64(lthroughput, 100);
  77. break;
  78. }
  79. if (lthroughput > U32_MAX) {
  80. batadv_err(net_dev,
  81. "Throughput speed for %s too large: %s\n",
  82. description, buff);
  83. return false;
  84. }
  85. *throughput = lthroughput;
  86. return true;
  87. }
  88. /**
  89. * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
  90. * and upload bandwidth information
  91. * @net_dev: the soft interface net device
  92. * @buff: string buffer to parse
  93. * @down: pointer holding the returned download bandwidth information
  94. * @up: pointer holding the returned upload bandwidth information
  95. *
  96. * Return: false on parse error and true otherwise.
  97. */
  98. static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
  99. u32 *down, u32 *up)
  100. {
  101. char *slash_ptr;
  102. bool ret;
  103. slash_ptr = strchr(buff, '/');
  104. if (slash_ptr)
  105. *slash_ptr = 0;
  106. ret = batadv_parse_throughput(net_dev, buff, "download gateway speed",
  107. down);
  108. if (!ret)
  109. return false;
  110. /* we also got some upload info */
  111. if (slash_ptr) {
  112. ret = batadv_parse_throughput(net_dev, slash_ptr + 1,
  113. "upload gateway speed", up);
  114. if (!ret)
  115. return false;
  116. }
  117. return true;
  118. }
  119. /**
  120. * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
  121. * setting change
  122. * @bat_priv: the bat priv with all the soft interface information
  123. */
  124. void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
  125. {
  126. struct batadv_tvlv_gateway_data gw;
  127. u32 down, up;
  128. char gw_mode;
  129. gw_mode = atomic_read(&bat_priv->gw.mode);
  130. switch (gw_mode) {
  131. case BATADV_GW_MODE_OFF:
  132. case BATADV_GW_MODE_CLIENT:
  133. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
  134. break;
  135. case BATADV_GW_MODE_SERVER:
  136. down = atomic_read(&bat_priv->gw.bandwidth_down);
  137. up = atomic_read(&bat_priv->gw.bandwidth_up);
  138. gw.bandwidth_down = htonl(down);
  139. gw.bandwidth_up = htonl(up);
  140. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
  141. &gw, sizeof(gw));
  142. break;
  143. }
  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. batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
  221. NULL, BATADV_TVLV_GW, 1,
  222. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  223. }
  224. /**
  225. * batadv_gw_free - free the gateway handling internals
  226. * @bat_priv: the bat priv with all the soft interface information
  227. */
  228. void batadv_gw_free(struct batadv_priv *bat_priv)
  229. {
  230. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
  231. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
  232. }