gateway_client.c 24 KB

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