gateway_common.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Copyright (C) 2009-2015 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/errno.h>
  21. #include <linux/byteorder/generic.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 "packet.h"
  29. /**
  30. * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
  31. * and upload bandwidth information
  32. * @net_dev: the soft interface net device
  33. * @buff: string buffer to parse
  34. * @down: pointer holding the returned download bandwidth information
  35. * @up: pointer holding the returned upload bandwidth information
  36. *
  37. * Returns false on parse error and true otherwise.
  38. */
  39. static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
  40. u32 *down, u32 *up)
  41. {
  42. enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
  43. char *slash_ptr, *tmp_ptr;
  44. u64 ldown, lup;
  45. int ret;
  46. slash_ptr = strchr(buff, '/');
  47. if (slash_ptr)
  48. *slash_ptr = 0;
  49. if (strlen(buff) > 4) {
  50. tmp_ptr = buff + strlen(buff) - 4;
  51. if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
  52. bw_unit_type = BATADV_BW_UNIT_MBIT;
  53. if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
  54. (bw_unit_type == BATADV_BW_UNIT_MBIT))
  55. *tmp_ptr = '\0';
  56. }
  57. ret = kstrtou64(buff, 10, &ldown);
  58. if (ret) {
  59. batadv_err(net_dev,
  60. "Download speed of gateway mode invalid: %s\n",
  61. buff);
  62. return false;
  63. }
  64. switch (bw_unit_type) {
  65. case BATADV_BW_UNIT_MBIT:
  66. /* prevent overflow */
  67. if (U64_MAX / 10 < ldown) {
  68. batadv_err(net_dev,
  69. "Download speed of gateway mode too large: %s\n",
  70. buff);
  71. return false;
  72. }
  73. ldown *= 10;
  74. break;
  75. case BATADV_BW_UNIT_KBIT:
  76. default:
  77. ldown = div_u64(ldown, 100);
  78. break;
  79. }
  80. if (U32_MAX < ldown) {
  81. batadv_err(net_dev,
  82. "Download speed of gateway mode too large: %s\n",
  83. buff);
  84. return false;
  85. }
  86. *down = ldown;
  87. /* we also got some upload info */
  88. if (slash_ptr) {
  89. bw_unit_type = BATADV_BW_UNIT_KBIT;
  90. if (strlen(slash_ptr + 1) > 4) {
  91. tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
  92. if (strncasecmp(tmp_ptr, "mbit", 4) == 0)
  93. bw_unit_type = BATADV_BW_UNIT_MBIT;
  94. if ((strncasecmp(tmp_ptr, "kbit", 4) == 0) ||
  95. (bw_unit_type == BATADV_BW_UNIT_MBIT))
  96. *tmp_ptr = '\0';
  97. }
  98. ret = kstrtou64(slash_ptr + 1, 10, &lup);
  99. if (ret) {
  100. batadv_err(net_dev,
  101. "Upload speed of gateway mode invalid: %s\n",
  102. slash_ptr + 1);
  103. return false;
  104. }
  105. switch (bw_unit_type) {
  106. case BATADV_BW_UNIT_MBIT:
  107. /* prevent overflow */
  108. if (U64_MAX / 10 < lup) {
  109. batadv_err(net_dev,
  110. "Upload speed of gateway mode too large: %s\n",
  111. slash_ptr + 1);
  112. return false;
  113. }
  114. lup *= 10;
  115. break;
  116. case BATADV_BW_UNIT_KBIT:
  117. default:
  118. lup = div_u64(lup, 100);
  119. break;
  120. }
  121. if (U32_MAX < lup) {
  122. batadv_err(net_dev,
  123. "Upload speed of gateway mode too large: %s\n",
  124. slash_ptr + 1);
  125. return false;
  126. }
  127. *up = lup;
  128. }
  129. return true;
  130. }
  131. /**
  132. * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
  133. * setting change
  134. * @bat_priv: the bat priv with all the soft interface information
  135. */
  136. void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
  137. {
  138. struct batadv_tvlv_gateway_data gw;
  139. u32 down, up;
  140. char gw_mode;
  141. gw_mode = atomic_read(&bat_priv->gw_mode);
  142. switch (gw_mode) {
  143. case BATADV_GW_MODE_OFF:
  144. case BATADV_GW_MODE_CLIENT:
  145. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
  146. break;
  147. case BATADV_GW_MODE_SERVER:
  148. down = atomic_read(&bat_priv->gw.bandwidth_down);
  149. up = atomic_read(&bat_priv->gw.bandwidth_up);
  150. gw.bandwidth_down = htonl(down);
  151. gw.bandwidth_up = htonl(up);
  152. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
  153. &gw, sizeof(gw));
  154. break;
  155. }
  156. }
  157. ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
  158. size_t count)
  159. {
  160. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  161. u32 down_curr;
  162. u32 up_curr;
  163. u32 down_new = 0;
  164. u32 up_new = 0;
  165. bool ret;
  166. down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
  167. up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
  168. ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
  169. if (!ret)
  170. return -EINVAL;
  171. if (!down_new)
  172. down_new = 1;
  173. if (!up_new)
  174. up_new = down_new / 5;
  175. if (!up_new)
  176. up_new = 1;
  177. if ((down_curr == down_new) && (up_curr == up_new))
  178. return count;
  179. batadv_gw_reselect(bat_priv);
  180. batadv_info(net_dev,
  181. "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
  182. down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
  183. down_new / 10, down_new % 10, up_new / 10, up_new % 10);
  184. atomic_set(&bat_priv->gw.bandwidth_down, down_new);
  185. atomic_set(&bat_priv->gw.bandwidth_up, up_new);
  186. batadv_gw_tvlv_container_update(bat_priv);
  187. return count;
  188. }
  189. /**
  190. * batadv_gw_tvlv_ogm_handler_v1 - process incoming gateway tvlv container
  191. * @bat_priv: the bat priv with all the soft interface information
  192. * @orig: the orig_node of the ogm
  193. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  194. * @tvlv_value: tvlv buffer containing the gateway data
  195. * @tvlv_value_len: tvlv buffer length
  196. */
  197. static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  198. struct batadv_orig_node *orig,
  199. u8 flags,
  200. void *tvlv_value, u16 tvlv_value_len)
  201. {
  202. struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
  203. /* only fetch the tvlv value if the handler wasn't called via the
  204. * CIFNOTFND flag and if there is data to fetch
  205. */
  206. if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) ||
  207. (tvlv_value_len < sizeof(gateway))) {
  208. gateway.bandwidth_down = 0;
  209. gateway.bandwidth_up = 0;
  210. } else {
  211. gateway_ptr = tvlv_value;
  212. gateway.bandwidth_down = gateway_ptr->bandwidth_down;
  213. gateway.bandwidth_up = gateway_ptr->bandwidth_up;
  214. if ((gateway.bandwidth_down == 0) ||
  215. (gateway.bandwidth_up == 0)) {
  216. gateway.bandwidth_down = 0;
  217. gateway.bandwidth_up = 0;
  218. }
  219. }
  220. batadv_gw_node_update(bat_priv, orig, &gateway);
  221. /* restart gateway selection if fast or late switching was enabled */
  222. if ((gateway.bandwidth_down != 0) &&
  223. (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
  224. (atomic_read(&bat_priv->gw_sel_class) > 2))
  225. batadv_gw_check_election(bat_priv, orig);
  226. }
  227. /**
  228. * batadv_gw_init - initialise the gateway handling internals
  229. * @bat_priv: the bat priv with all the soft interface information
  230. */
  231. void batadv_gw_init(struct batadv_priv *bat_priv)
  232. {
  233. batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
  234. NULL, BATADV_TVLV_GW, 1,
  235. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  236. }
  237. /**
  238. * batadv_gw_free - free the gateway handling internals
  239. * @bat_priv: the bat priv with all the soft interface information
  240. */
  241. void batadv_gw_free(struct batadv_priv *bat_priv)
  242. {
  243. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
  244. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
  245. }