gateway_client.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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_client.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/byteorder/generic.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/fs.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/if_vlan.h>
  25. #include <linux/in.h>
  26. #include <linux/ip.h>
  27. #include <linux/ipv6.h>
  28. #include <linux/kernel.h>
  29. #include <linux/list.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/rculist.h>
  32. #include <linux/rcupdate.h>
  33. #include <linux/seq_file.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/slab.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/stddef.h>
  38. #include <linux/udp.h>
  39. #include "gateway_common.h"
  40. #include "hard-interface.h"
  41. #include "originator.h"
  42. #include "packet.h"
  43. #include "routing.h"
  44. #include "sysfs.h"
  45. #include "translation-table.h"
  46. /* These are the offsets of the "hw type" and "hw address length" in the dhcp
  47. * packet starting at the beginning of the dhcp header
  48. */
  49. #define BATADV_DHCP_HTYPE_OFFSET 1
  50. #define BATADV_DHCP_HLEN_OFFSET 2
  51. /* Value of htype representing Ethernet */
  52. #define BATADV_DHCP_HTYPE_ETHERNET 0x01
  53. /* This is the offset of the "chaddr" field in the dhcp packet starting at the
  54. * beginning of the dhcp header
  55. */
  56. #define BATADV_DHCP_CHADDR_OFFSET 28
  57. static void batadv_gw_node_free_ref(struct batadv_gw_node *gw_node)
  58. {
  59. if (atomic_dec_and_test(&gw_node->refcount)) {
  60. batadv_orig_node_free_ref(gw_node->orig_node);
  61. kfree_rcu(gw_node, rcu);
  62. }
  63. }
  64. static struct batadv_gw_node *
  65. batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
  66. {
  67. struct batadv_gw_node *gw_node;
  68. rcu_read_lock();
  69. gw_node = rcu_dereference(bat_priv->gw.curr_gw);
  70. if (!gw_node)
  71. goto out;
  72. if (!atomic_inc_not_zero(&gw_node->refcount))
  73. gw_node = NULL;
  74. out:
  75. rcu_read_unlock();
  76. return gw_node;
  77. }
  78. struct batadv_orig_node *
  79. batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
  80. {
  81. struct batadv_gw_node *gw_node;
  82. struct batadv_orig_node *orig_node = NULL;
  83. gw_node = batadv_gw_get_selected_gw_node(bat_priv);
  84. if (!gw_node)
  85. goto out;
  86. rcu_read_lock();
  87. orig_node = gw_node->orig_node;
  88. if (!orig_node)
  89. goto unlock;
  90. if (!atomic_inc_not_zero(&orig_node->refcount))
  91. orig_node = NULL;
  92. unlock:
  93. rcu_read_unlock();
  94. out:
  95. if (gw_node)
  96. batadv_gw_node_free_ref(gw_node);
  97. return orig_node;
  98. }
  99. static void batadv_gw_select(struct batadv_priv *bat_priv,
  100. struct batadv_gw_node *new_gw_node)
  101. {
  102. struct batadv_gw_node *curr_gw_node;
  103. spin_lock_bh(&bat_priv->gw.list_lock);
  104. if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
  105. new_gw_node = NULL;
  106. curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
  107. rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
  108. if (curr_gw_node)
  109. batadv_gw_node_free_ref(curr_gw_node);
  110. spin_unlock_bh(&bat_priv->gw.list_lock);
  111. }
  112. /**
  113. * batadv_gw_reselect - force a gateway reselection
  114. * @bat_priv: the bat priv with all the soft interface information
  115. *
  116. * Set a flag to remind the GW component to perform a new gateway reselection.
  117. * However this function does not ensure that the current gateway is going to be
  118. * deselected. The reselection mechanism may elect the same gateway once again.
  119. *
  120. * This means that invoking batadv_gw_reselect() does not guarantee a gateway
  121. * change and therefore a uevent is not necessarily expected.
  122. */
  123. void batadv_gw_reselect(struct batadv_priv *bat_priv)
  124. {
  125. atomic_set(&bat_priv->gw.reselect, 1);
  126. }
  127. static struct batadv_gw_node *
  128. batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
  129. {
  130. struct batadv_neigh_node *router;
  131. struct batadv_neigh_ifinfo *router_ifinfo;
  132. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  133. u64 max_gw_factor = 0;
  134. u64 tmp_gw_factor = 0;
  135. u8 max_tq = 0;
  136. u8 tq_avg;
  137. struct batadv_orig_node *orig_node;
  138. rcu_read_lock();
  139. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
  140. orig_node = gw_node->orig_node;
  141. router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  142. if (!router)
  143. continue;
  144. router_ifinfo = batadv_neigh_ifinfo_get(router,
  145. BATADV_IF_DEFAULT);
  146. if (!router_ifinfo)
  147. goto next;
  148. if (!atomic_inc_not_zero(&gw_node->refcount))
  149. goto next;
  150. tq_avg = router_ifinfo->bat_iv.tq_avg;
  151. switch (atomic_read(&bat_priv->gw_sel_class)) {
  152. case 1: /* fast connection */
  153. tmp_gw_factor = tq_avg * tq_avg;
  154. tmp_gw_factor *= gw_node->bandwidth_down;
  155. tmp_gw_factor *= 100 * 100;
  156. tmp_gw_factor >>= 18;
  157. if ((tmp_gw_factor > max_gw_factor) ||
  158. ((tmp_gw_factor == max_gw_factor) &&
  159. (tq_avg > max_tq))) {
  160. if (curr_gw)
  161. batadv_gw_node_free_ref(curr_gw);
  162. curr_gw = gw_node;
  163. atomic_inc(&curr_gw->refcount);
  164. }
  165. break;
  166. default: /* 2: stable connection (use best statistic)
  167. * 3: fast-switch (use best statistic but change as
  168. * soon as a better gateway appears)
  169. * XX: late-switch (use best statistic but change as
  170. * soon as a better gateway appears which has
  171. * $routing_class more tq points)
  172. */
  173. if (tq_avg > max_tq) {
  174. if (curr_gw)
  175. batadv_gw_node_free_ref(curr_gw);
  176. curr_gw = gw_node;
  177. atomic_inc(&curr_gw->refcount);
  178. }
  179. break;
  180. }
  181. if (tq_avg > max_tq)
  182. max_tq = tq_avg;
  183. if (tmp_gw_factor > max_gw_factor)
  184. max_gw_factor = tmp_gw_factor;
  185. batadv_gw_node_free_ref(gw_node);
  186. next:
  187. batadv_neigh_node_free_ref(router);
  188. if (router_ifinfo)
  189. batadv_neigh_ifinfo_free_ref(router_ifinfo);
  190. }
  191. rcu_read_unlock();
  192. return curr_gw;
  193. }
  194. /**
  195. * batadv_gw_check_client_stop - check if client mode has been switched off
  196. * @bat_priv: the bat priv with all the soft interface information
  197. *
  198. * This function assumes the caller has checked that the gw state *is actually
  199. * changing*. This function is not supposed to be called when there is no state
  200. * change.
  201. */
  202. void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
  203. {
  204. struct batadv_gw_node *curr_gw;
  205. if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
  206. return;
  207. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  208. if (!curr_gw)
  209. return;
  210. /* deselect the current gateway so that next time that client mode is
  211. * enabled a proper GW_ADD event can be sent
  212. */
  213. batadv_gw_select(bat_priv, NULL);
  214. /* if batman-adv is switching the gw client mode off and a gateway was
  215. * already selected, send a DEL uevent
  216. */
  217. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
  218. batadv_gw_node_free_ref(curr_gw);
  219. }
  220. void batadv_gw_election(struct batadv_priv *bat_priv)
  221. {
  222. struct batadv_gw_node *curr_gw = NULL;
  223. struct batadv_gw_node *next_gw = NULL;
  224. struct batadv_neigh_node *router = NULL;
  225. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  226. char gw_addr[18] = { '\0' };
  227. if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
  228. goto out;
  229. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  230. if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
  231. goto out;
  232. next_gw = batadv_gw_get_best_gw_node(bat_priv);
  233. if (curr_gw == next_gw)
  234. goto out;
  235. if (next_gw) {
  236. sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
  237. router = batadv_orig_router_get(next_gw->orig_node,
  238. BATADV_IF_DEFAULT);
  239. if (!router) {
  240. batadv_gw_reselect(bat_priv);
  241. goto out;
  242. }
  243. router_ifinfo = batadv_neigh_ifinfo_get(router,
  244. BATADV_IF_DEFAULT);
  245. if (!router_ifinfo) {
  246. batadv_gw_reselect(bat_priv);
  247. goto out;
  248. }
  249. }
  250. if ((curr_gw) && (!next_gw)) {
  251. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  252. "Removing selected gateway - no gateway in range\n");
  253. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
  254. NULL);
  255. } else if ((!curr_gw) && (next_gw)) {
  256. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  257. "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
  258. next_gw->orig_node->orig,
  259. next_gw->bandwidth_down / 10,
  260. next_gw->bandwidth_down % 10,
  261. next_gw->bandwidth_up / 10,
  262. next_gw->bandwidth_up % 10,
  263. router_ifinfo->bat_iv.tq_avg);
  264. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
  265. gw_addr);
  266. } else {
  267. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  268. "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
  269. next_gw->orig_node->orig,
  270. next_gw->bandwidth_down / 10,
  271. next_gw->bandwidth_down % 10,
  272. next_gw->bandwidth_up / 10,
  273. next_gw->bandwidth_up % 10,
  274. router_ifinfo->bat_iv.tq_avg);
  275. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
  276. gw_addr);
  277. }
  278. batadv_gw_select(bat_priv, next_gw);
  279. out:
  280. if (curr_gw)
  281. batadv_gw_node_free_ref(curr_gw);
  282. if (next_gw)
  283. batadv_gw_node_free_ref(next_gw);
  284. if (router)
  285. batadv_neigh_node_free_ref(router);
  286. if (router_ifinfo)
  287. batadv_neigh_ifinfo_free_ref(router_ifinfo);
  288. }
  289. void batadv_gw_check_election(struct batadv_priv *bat_priv,
  290. struct batadv_orig_node *orig_node)
  291. {
  292. struct batadv_neigh_ifinfo *router_orig_tq = NULL;
  293. struct batadv_neigh_ifinfo *router_gw_tq = NULL;
  294. struct batadv_orig_node *curr_gw_orig;
  295. struct batadv_neigh_node *router_gw = NULL;
  296. struct batadv_neigh_node *router_orig = NULL;
  297. u8 gw_tq_avg, orig_tq_avg;
  298. curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
  299. if (!curr_gw_orig)
  300. goto reselect;
  301. router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
  302. if (!router_gw)
  303. goto reselect;
  304. router_gw_tq = batadv_neigh_ifinfo_get(router_gw,
  305. BATADV_IF_DEFAULT);
  306. if (!router_gw_tq)
  307. goto reselect;
  308. /* this node already is the gateway */
  309. if (curr_gw_orig == orig_node)
  310. goto out;
  311. router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  312. if (!router_orig)
  313. goto out;
  314. router_orig_tq = batadv_neigh_ifinfo_get(router_orig,
  315. BATADV_IF_DEFAULT);
  316. if (!router_orig_tq)
  317. goto out;
  318. gw_tq_avg = router_gw_tq->bat_iv.tq_avg;
  319. orig_tq_avg = router_orig_tq->bat_iv.tq_avg;
  320. /* the TQ value has to be better */
  321. if (orig_tq_avg < gw_tq_avg)
  322. goto out;
  323. /* if the routing class is greater than 3 the value tells us how much
  324. * greater the TQ value of the new gateway must be
  325. */
  326. if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
  327. (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
  328. goto out;
  329. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  330. "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
  331. gw_tq_avg, orig_tq_avg);
  332. reselect:
  333. batadv_gw_reselect(bat_priv);
  334. out:
  335. if (curr_gw_orig)
  336. batadv_orig_node_free_ref(curr_gw_orig);
  337. if (router_gw)
  338. batadv_neigh_node_free_ref(router_gw);
  339. if (router_orig)
  340. batadv_neigh_node_free_ref(router_orig);
  341. if (router_gw_tq)
  342. batadv_neigh_ifinfo_free_ref(router_gw_tq);
  343. if (router_orig_tq)
  344. batadv_neigh_ifinfo_free_ref(router_orig_tq);
  345. }
  346. /**
  347. * batadv_gw_node_add - add gateway node to list of available gateways
  348. * @bat_priv: the bat priv with all the soft interface information
  349. * @orig_node: originator announcing gateway capabilities
  350. * @gateway: announced bandwidth information
  351. */
  352. static void batadv_gw_node_add(struct batadv_priv *bat_priv,
  353. struct batadv_orig_node *orig_node,
  354. struct batadv_tvlv_gateway_data *gateway)
  355. {
  356. struct batadv_gw_node *gw_node;
  357. if (gateway->bandwidth_down == 0)
  358. return;
  359. if (!atomic_inc_not_zero(&orig_node->refcount))
  360. return;
  361. gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
  362. if (!gw_node) {
  363. batadv_orig_node_free_ref(orig_node);
  364. return;
  365. }
  366. INIT_HLIST_NODE(&gw_node->list);
  367. gw_node->orig_node = orig_node;
  368. gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
  369. gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
  370. atomic_set(&gw_node->refcount, 1);
  371. spin_lock_bh(&bat_priv->gw.list_lock);
  372. hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
  373. spin_unlock_bh(&bat_priv->gw.list_lock);
  374. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  375. "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
  376. orig_node->orig,
  377. ntohl(gateway->bandwidth_down) / 10,
  378. ntohl(gateway->bandwidth_down) % 10,
  379. ntohl(gateway->bandwidth_up) / 10,
  380. ntohl(gateway->bandwidth_up) % 10);
  381. }
  382. /**
  383. * batadv_gw_node_get - retrieve gateway node from list of available gateways
  384. * @bat_priv: the bat priv with all the soft interface information
  385. * @orig_node: originator announcing gateway capabilities
  386. *
  387. * Returns gateway node if found or NULL otherwise.
  388. */
  389. static struct batadv_gw_node *
  390. batadv_gw_node_get(struct batadv_priv *bat_priv,
  391. struct batadv_orig_node *orig_node)
  392. {
  393. struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
  394. rcu_read_lock();
  395. hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
  396. if (gw_node_tmp->orig_node != orig_node)
  397. continue;
  398. if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
  399. continue;
  400. gw_node = gw_node_tmp;
  401. break;
  402. }
  403. rcu_read_unlock();
  404. return gw_node;
  405. }
  406. /**
  407. * batadv_gw_node_update - update list of available gateways with changed
  408. * bandwidth information
  409. * @bat_priv: the bat priv with all the soft interface information
  410. * @orig_node: originator announcing gateway capabilities
  411. * @gateway: announced bandwidth information
  412. */
  413. void batadv_gw_node_update(struct batadv_priv *bat_priv,
  414. struct batadv_orig_node *orig_node,
  415. struct batadv_tvlv_gateway_data *gateway)
  416. {
  417. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  418. gw_node = batadv_gw_node_get(bat_priv, orig_node);
  419. if (!gw_node) {
  420. batadv_gw_node_add(bat_priv, orig_node, gateway);
  421. goto out;
  422. }
  423. if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
  424. (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
  425. goto out;
  426. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  427. "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
  428. orig_node->orig,
  429. gw_node->bandwidth_down / 10,
  430. gw_node->bandwidth_down % 10,
  431. gw_node->bandwidth_up / 10,
  432. gw_node->bandwidth_up % 10,
  433. ntohl(gateway->bandwidth_down) / 10,
  434. ntohl(gateway->bandwidth_down) % 10,
  435. ntohl(gateway->bandwidth_up) / 10,
  436. ntohl(gateway->bandwidth_up) % 10);
  437. gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
  438. gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
  439. if (ntohl(gateway->bandwidth_down) == 0) {
  440. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  441. "Gateway %pM removed from gateway list\n",
  442. orig_node->orig);
  443. /* Note: We don't need a NULL check here, since curr_gw never
  444. * gets dereferenced.
  445. */
  446. spin_lock_bh(&bat_priv->gw.list_lock);
  447. hlist_del_init_rcu(&gw_node->list);
  448. spin_unlock_bh(&bat_priv->gw.list_lock);
  449. batadv_gw_node_free_ref(gw_node);
  450. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  451. if (gw_node == curr_gw)
  452. batadv_gw_reselect(bat_priv);
  453. if (curr_gw)
  454. batadv_gw_node_free_ref(curr_gw);
  455. }
  456. out:
  457. if (gw_node)
  458. batadv_gw_node_free_ref(gw_node);
  459. }
  460. void batadv_gw_node_delete(struct batadv_priv *bat_priv,
  461. struct batadv_orig_node *orig_node)
  462. {
  463. struct batadv_tvlv_gateway_data gateway;
  464. gateway.bandwidth_down = 0;
  465. gateway.bandwidth_up = 0;
  466. batadv_gw_node_update(bat_priv, orig_node, &gateway);
  467. }
  468. void batadv_gw_node_free(struct batadv_priv *bat_priv)
  469. {
  470. struct batadv_gw_node *gw_node;
  471. struct hlist_node *node_tmp;
  472. spin_lock_bh(&bat_priv->gw.list_lock);
  473. hlist_for_each_entry_safe(gw_node, node_tmp,
  474. &bat_priv->gw.list, list) {
  475. hlist_del_init_rcu(&gw_node->list);
  476. batadv_gw_node_free_ref(gw_node);
  477. }
  478. spin_unlock_bh(&bat_priv->gw.list_lock);
  479. }
  480. /* fails if orig_node has no router */
  481. static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
  482. struct seq_file *seq,
  483. const struct batadv_gw_node *gw_node)
  484. {
  485. struct batadv_gw_node *curr_gw;
  486. struct batadv_neigh_node *router;
  487. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  488. int ret = -1;
  489. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  490. if (!router)
  491. goto out;
  492. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  493. if (!router_ifinfo)
  494. goto out;
  495. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  496. seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
  497. (curr_gw == gw_node ? "=>" : " "),
  498. gw_node->orig_node->orig,
  499. router_ifinfo->bat_iv.tq_avg, router->addr,
  500. router->if_incoming->net_dev->name,
  501. gw_node->bandwidth_down / 10,
  502. gw_node->bandwidth_down % 10,
  503. gw_node->bandwidth_up / 10,
  504. gw_node->bandwidth_up % 10);
  505. ret = seq_has_overflowed(seq) ? -1 : 0;
  506. if (curr_gw)
  507. batadv_gw_node_free_ref(curr_gw);
  508. out:
  509. if (router_ifinfo)
  510. batadv_neigh_ifinfo_free_ref(router_ifinfo);
  511. if (router)
  512. batadv_neigh_node_free_ref(router);
  513. return ret;
  514. }
  515. int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
  516. {
  517. struct net_device *net_dev = (struct net_device *)seq->private;
  518. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  519. struct batadv_hard_iface *primary_if;
  520. struct batadv_gw_node *gw_node;
  521. int gw_count = 0;
  522. primary_if = batadv_seq_print_text_primary_if_get(seq);
  523. if (!primary_if)
  524. goto out;
  525. seq_printf(seq,
  526. " %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
  527. "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
  528. BATADV_SOURCE_VERSION, primary_if->net_dev->name,
  529. primary_if->net_dev->dev_addr, net_dev->name);
  530. rcu_read_lock();
  531. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
  532. /* fails if orig_node has no router */
  533. if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
  534. continue;
  535. gw_count++;
  536. }
  537. rcu_read_unlock();
  538. if (gw_count == 0)
  539. seq_puts(seq, "No gateways in range ...\n");
  540. out:
  541. if (primary_if)
  542. batadv_hardif_free_ref(primary_if);
  543. return 0;
  544. }
  545. /**
  546. * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
  547. * @skb: the packet to check
  548. * @header_len: a pointer to the batman-adv header size
  549. * @chaddr: buffer where the client address will be stored. Valid
  550. * only if the function returns BATADV_DHCP_TO_CLIENT
  551. *
  552. * Returns:
  553. * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
  554. * while parsing it
  555. * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
  556. * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
  557. *
  558. * This function may re-allocate the data buffer of the skb passed as argument.
  559. */
  560. enum batadv_dhcp_recipient
  561. batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
  562. u8 *chaddr)
  563. {
  564. enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
  565. struct ethhdr *ethhdr;
  566. struct iphdr *iphdr;
  567. struct ipv6hdr *ipv6hdr;
  568. struct udphdr *udphdr;
  569. struct vlan_ethhdr *vhdr;
  570. int chaddr_offset;
  571. __be16 proto;
  572. u8 *p;
  573. /* check for ethernet header */
  574. if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
  575. return BATADV_DHCP_NO;
  576. ethhdr = eth_hdr(skb);
  577. proto = ethhdr->h_proto;
  578. *header_len += ETH_HLEN;
  579. /* check for initial vlan header */
  580. if (proto == htons(ETH_P_8021Q)) {
  581. if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
  582. return BATADV_DHCP_NO;
  583. vhdr = vlan_eth_hdr(skb);
  584. proto = vhdr->h_vlan_encapsulated_proto;
  585. *header_len += VLAN_HLEN;
  586. }
  587. /* check for ip header */
  588. switch (proto) {
  589. case htons(ETH_P_IP):
  590. if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
  591. return BATADV_DHCP_NO;
  592. iphdr = (struct iphdr *)(skb->data + *header_len);
  593. *header_len += iphdr->ihl * 4;
  594. /* check for udp header */
  595. if (iphdr->protocol != IPPROTO_UDP)
  596. return BATADV_DHCP_NO;
  597. break;
  598. case htons(ETH_P_IPV6):
  599. if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
  600. return BATADV_DHCP_NO;
  601. ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
  602. *header_len += sizeof(*ipv6hdr);
  603. /* check for udp header */
  604. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  605. return BATADV_DHCP_NO;
  606. break;
  607. default:
  608. return BATADV_DHCP_NO;
  609. }
  610. if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
  611. return BATADV_DHCP_NO;
  612. udphdr = (struct udphdr *)(skb->data + *header_len);
  613. *header_len += sizeof(*udphdr);
  614. /* check for bootp port */
  615. switch (proto) {
  616. case htons(ETH_P_IP):
  617. if (udphdr->dest == htons(67))
  618. ret = BATADV_DHCP_TO_SERVER;
  619. else if (udphdr->source == htons(67))
  620. ret = BATADV_DHCP_TO_CLIENT;
  621. break;
  622. case htons(ETH_P_IPV6):
  623. if (udphdr->dest == htons(547))
  624. ret = BATADV_DHCP_TO_SERVER;
  625. else if (udphdr->source == htons(547))
  626. ret = BATADV_DHCP_TO_CLIENT;
  627. break;
  628. }
  629. chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
  630. /* store the client address if the message is going to a client */
  631. if (ret == BATADV_DHCP_TO_CLIENT &&
  632. pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
  633. /* check if the DHCP packet carries an Ethernet DHCP */
  634. p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
  635. if (*p != BATADV_DHCP_HTYPE_ETHERNET)
  636. return BATADV_DHCP_NO;
  637. /* check if the DHCP packet carries a valid Ethernet address */
  638. p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
  639. if (*p != ETH_ALEN)
  640. return BATADV_DHCP_NO;
  641. ether_addr_copy(chaddr, skb->data + chaddr_offset);
  642. }
  643. return ret;
  644. }
  645. /**
  646. * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
  647. * @bat_priv: the bat priv with all the soft interface information
  648. * @skb: the outgoing packet
  649. *
  650. * Check if the skb is a DHCP request and if it is sent to the current best GW
  651. * server. Due to topology changes it may be the case that the GW server
  652. * previously selected is not the best one anymore.
  653. *
  654. * Returns true if the packet destination is unicast and it is not the best gw,
  655. * false otherwise.
  656. *
  657. * This call might reallocate skb data.
  658. * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
  659. */
  660. bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
  661. struct sk_buff *skb)
  662. {
  663. struct batadv_neigh_node *neigh_curr = NULL;
  664. struct batadv_neigh_node *neigh_old = NULL;
  665. struct batadv_orig_node *orig_dst_node = NULL;
  666. struct batadv_gw_node *gw_node = NULL;
  667. struct batadv_gw_node *curr_gw = NULL;
  668. struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
  669. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  670. bool out_of_range = false;
  671. u8 curr_tq_avg;
  672. unsigned short vid;
  673. vid = batadv_get_vid(skb, 0);
  674. if (is_multicast_ether_addr(ethhdr->h_dest))
  675. goto out;
  676. orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
  677. ethhdr->h_dest, vid);
  678. if (!orig_dst_node)
  679. goto out;
  680. gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
  681. if (!gw_node)
  682. goto out;
  683. switch (atomic_read(&bat_priv->gw_mode)) {
  684. case BATADV_GW_MODE_SERVER:
  685. /* If we are a GW then we are our best GW. We can artificially
  686. * set the tq towards ourself as the maximum value
  687. */
  688. curr_tq_avg = BATADV_TQ_MAX_VALUE;
  689. break;
  690. case BATADV_GW_MODE_CLIENT:
  691. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  692. if (!curr_gw)
  693. goto out;
  694. /* packet is going to our gateway */
  695. if (curr_gw->orig_node == orig_dst_node)
  696. goto out;
  697. /* If the dhcp packet has been sent to a different gw,
  698. * we have to evaluate whether the old gw is still
  699. * reliable enough
  700. */
  701. neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
  702. NULL);
  703. if (!neigh_curr)
  704. goto out;
  705. curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
  706. BATADV_IF_DEFAULT);
  707. if (!curr_ifinfo)
  708. goto out;
  709. curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
  710. batadv_neigh_ifinfo_free_ref(curr_ifinfo);
  711. break;
  712. case BATADV_GW_MODE_OFF:
  713. default:
  714. goto out;
  715. }
  716. neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
  717. if (!neigh_old)
  718. goto out;
  719. old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
  720. if (!old_ifinfo)
  721. goto out;
  722. if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
  723. out_of_range = true;
  724. batadv_neigh_ifinfo_free_ref(old_ifinfo);
  725. out:
  726. if (orig_dst_node)
  727. batadv_orig_node_free_ref(orig_dst_node);
  728. if (curr_gw)
  729. batadv_gw_node_free_ref(curr_gw);
  730. if (gw_node)
  731. batadv_gw_node_free_ref(gw_node);
  732. if (neigh_old)
  733. batadv_neigh_node_free_ref(neigh_old);
  734. if (neigh_curr)
  735. batadv_neigh_node_free_ref(neigh_curr);
  736. return out_of_range;
  737. }