gateway_client.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * Copyright (C) 2009-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "bat_sysfs.h"
  23. #include "gateway_client.h"
  24. #include "gateway_common.h"
  25. #include "hard-interface.h"
  26. #include "originator.h"
  27. #include "translation-table.h"
  28. #include "routing.h"
  29. #include <linux/ip.h>
  30. #include <linux/ipv6.h>
  31. #include <linux/udp.h>
  32. #include <linux/if_vlan.h>
  33. /* This is the offset of the options field in a dhcp packet starting at
  34. * the beginning of the dhcp header */
  35. #define DHCP_OPTIONS_OFFSET 240
  36. #define DHCP_REQUEST 3
  37. static void gw_node_free_ref(struct gw_node *gw_node)
  38. {
  39. if (atomic_dec_and_test(&gw_node->refcount))
  40. kfree_rcu(gw_node, rcu);
  41. }
  42. static struct gw_node *gw_get_selected_gw_node(struct bat_priv *bat_priv)
  43. {
  44. struct gw_node *gw_node;
  45. rcu_read_lock();
  46. gw_node = rcu_dereference(bat_priv->curr_gw);
  47. if (!gw_node)
  48. goto out;
  49. if (!atomic_inc_not_zero(&gw_node->refcount))
  50. gw_node = NULL;
  51. out:
  52. rcu_read_unlock();
  53. return gw_node;
  54. }
  55. struct orig_node *batadv_gw_get_selected_orig(struct bat_priv *bat_priv)
  56. {
  57. struct gw_node *gw_node;
  58. struct orig_node *orig_node = NULL;
  59. gw_node = gw_get_selected_gw_node(bat_priv);
  60. if (!gw_node)
  61. goto out;
  62. rcu_read_lock();
  63. orig_node = gw_node->orig_node;
  64. if (!orig_node)
  65. goto unlock;
  66. if (!atomic_inc_not_zero(&orig_node->refcount))
  67. orig_node = NULL;
  68. unlock:
  69. rcu_read_unlock();
  70. out:
  71. if (gw_node)
  72. gw_node_free_ref(gw_node);
  73. return orig_node;
  74. }
  75. static void gw_select(struct bat_priv *bat_priv, struct gw_node *new_gw_node)
  76. {
  77. struct gw_node *curr_gw_node;
  78. spin_lock_bh(&bat_priv->gw_list_lock);
  79. if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
  80. new_gw_node = NULL;
  81. curr_gw_node = rcu_dereference_protected(bat_priv->curr_gw, 1);
  82. rcu_assign_pointer(bat_priv->curr_gw, new_gw_node);
  83. if (curr_gw_node)
  84. gw_node_free_ref(curr_gw_node);
  85. spin_unlock_bh(&bat_priv->gw_list_lock);
  86. }
  87. void batadv_gw_deselect(struct bat_priv *bat_priv)
  88. {
  89. atomic_set(&bat_priv->gw_reselect, 1);
  90. }
  91. static struct gw_node *gw_get_best_gw_node(struct bat_priv *bat_priv)
  92. {
  93. struct neigh_node *router;
  94. struct hlist_node *node;
  95. struct gw_node *gw_node, *curr_gw = NULL;
  96. uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
  97. uint8_t max_tq = 0;
  98. int down, up;
  99. struct orig_node *orig_node;
  100. rcu_read_lock();
  101. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  102. if (gw_node->deleted)
  103. continue;
  104. orig_node = gw_node->orig_node;
  105. router = orig_node_get_router(orig_node);
  106. if (!router)
  107. continue;
  108. if (!atomic_inc_not_zero(&gw_node->refcount))
  109. goto next;
  110. switch (atomic_read(&bat_priv->gw_sel_class)) {
  111. case 1: /* fast connection */
  112. batadv_gw_bandwidth_to_kbit(orig_node->gw_flags,
  113. &down, &up);
  114. tmp_gw_factor = (router->tq_avg * router->tq_avg *
  115. down * 100 * 100) /
  116. (TQ_LOCAL_WINDOW_SIZE *
  117. TQ_LOCAL_WINDOW_SIZE * 64);
  118. if ((tmp_gw_factor > max_gw_factor) ||
  119. ((tmp_gw_factor == max_gw_factor) &&
  120. (router->tq_avg > max_tq))) {
  121. if (curr_gw)
  122. gw_node_free_ref(curr_gw);
  123. curr_gw = gw_node;
  124. atomic_inc(&curr_gw->refcount);
  125. }
  126. break;
  127. default: /**
  128. * 2: stable connection (use best statistic)
  129. * 3: fast-switch (use best statistic but change as
  130. * soon as a better gateway appears)
  131. * XX: late-switch (use best statistic but change as
  132. * soon as a better gateway appears which has
  133. * $routing_class more tq points)
  134. **/
  135. if (router->tq_avg > max_tq) {
  136. if (curr_gw)
  137. gw_node_free_ref(curr_gw);
  138. curr_gw = gw_node;
  139. atomic_inc(&curr_gw->refcount);
  140. }
  141. break;
  142. }
  143. if (router->tq_avg > max_tq)
  144. max_tq = router->tq_avg;
  145. if (tmp_gw_factor > max_gw_factor)
  146. max_gw_factor = tmp_gw_factor;
  147. gw_node_free_ref(gw_node);
  148. next:
  149. neigh_node_free_ref(router);
  150. }
  151. rcu_read_unlock();
  152. return curr_gw;
  153. }
  154. void batadv_gw_election(struct bat_priv *bat_priv)
  155. {
  156. struct gw_node *curr_gw = NULL, *next_gw = NULL;
  157. struct neigh_node *router = NULL;
  158. char gw_addr[18] = { '\0' };
  159. /**
  160. * The batman daemon checks here if we already passed a full originator
  161. * cycle in order to make sure we don't choose the first gateway we
  162. * hear about. This check is based on the daemon's uptime which we
  163. * don't have.
  164. **/
  165. if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
  166. goto out;
  167. if (!atomic_dec_not_zero(&bat_priv->gw_reselect))
  168. goto out;
  169. curr_gw = gw_get_selected_gw_node(bat_priv);
  170. next_gw = gw_get_best_gw_node(bat_priv);
  171. if (curr_gw == next_gw)
  172. goto out;
  173. if (next_gw) {
  174. sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
  175. router = orig_node_get_router(next_gw->orig_node);
  176. if (!router) {
  177. batadv_gw_deselect(bat_priv);
  178. goto out;
  179. }
  180. }
  181. if ((curr_gw) && (!next_gw)) {
  182. bat_dbg(DBG_BATMAN, bat_priv,
  183. "Removing selected gateway - no gateway in range\n");
  184. batadv_throw_uevent(bat_priv, UEV_GW, UEV_DEL, NULL);
  185. } else if ((!curr_gw) && (next_gw)) {
  186. bat_dbg(DBG_BATMAN, bat_priv,
  187. "Adding route to gateway %pM (gw_flags: %i, tq: %i)\n",
  188. next_gw->orig_node->orig, next_gw->orig_node->gw_flags,
  189. router->tq_avg);
  190. batadv_throw_uevent(bat_priv, UEV_GW, UEV_ADD, gw_addr);
  191. } else {
  192. bat_dbg(DBG_BATMAN, bat_priv,
  193. "Changing route to gateway %pM (gw_flags: %i, tq: %i)\n",
  194. next_gw->orig_node->orig, next_gw->orig_node->gw_flags,
  195. router->tq_avg);
  196. batadv_throw_uevent(bat_priv, UEV_GW, UEV_CHANGE, gw_addr);
  197. }
  198. gw_select(bat_priv, next_gw);
  199. out:
  200. if (curr_gw)
  201. gw_node_free_ref(curr_gw);
  202. if (next_gw)
  203. gw_node_free_ref(next_gw);
  204. if (router)
  205. neigh_node_free_ref(router);
  206. }
  207. void batadv_gw_check_election(struct bat_priv *bat_priv,
  208. struct orig_node *orig_node)
  209. {
  210. struct orig_node *curr_gw_orig;
  211. struct neigh_node *router_gw = NULL, *router_orig = NULL;
  212. uint8_t gw_tq_avg, orig_tq_avg;
  213. curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
  214. if (!curr_gw_orig)
  215. goto deselect;
  216. router_gw = orig_node_get_router(curr_gw_orig);
  217. if (!router_gw)
  218. goto deselect;
  219. /* this node already is the gateway */
  220. if (curr_gw_orig == orig_node)
  221. goto out;
  222. router_orig = orig_node_get_router(orig_node);
  223. if (!router_orig)
  224. goto out;
  225. gw_tq_avg = router_gw->tq_avg;
  226. orig_tq_avg = router_orig->tq_avg;
  227. /* the TQ value has to be better */
  228. if (orig_tq_avg < gw_tq_avg)
  229. goto out;
  230. /**
  231. * if the routing class is greater than 3 the value tells us how much
  232. * greater the TQ value of the new gateway must be
  233. **/
  234. if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
  235. (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
  236. goto out;
  237. bat_dbg(DBG_BATMAN, bat_priv,
  238. "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
  239. gw_tq_avg, orig_tq_avg);
  240. deselect:
  241. batadv_gw_deselect(bat_priv);
  242. out:
  243. if (curr_gw_orig)
  244. orig_node_free_ref(curr_gw_orig);
  245. if (router_gw)
  246. neigh_node_free_ref(router_gw);
  247. if (router_orig)
  248. neigh_node_free_ref(router_orig);
  249. return;
  250. }
  251. static void gw_node_add(struct bat_priv *bat_priv,
  252. struct orig_node *orig_node, uint8_t new_gwflags)
  253. {
  254. struct gw_node *gw_node;
  255. int down, up;
  256. gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
  257. if (!gw_node)
  258. return;
  259. INIT_HLIST_NODE(&gw_node->list);
  260. gw_node->orig_node = orig_node;
  261. atomic_set(&gw_node->refcount, 1);
  262. spin_lock_bh(&bat_priv->gw_list_lock);
  263. hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
  264. spin_unlock_bh(&bat_priv->gw_list_lock);
  265. batadv_gw_bandwidth_to_kbit(new_gwflags, &down, &up);
  266. bat_dbg(DBG_BATMAN, bat_priv,
  267. "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n",
  268. orig_node->orig, new_gwflags,
  269. (down > 2048 ? down / 1024 : down),
  270. (down > 2048 ? "MBit" : "KBit"),
  271. (up > 2048 ? up / 1024 : up),
  272. (up > 2048 ? "MBit" : "KBit"));
  273. }
  274. void batadv_gw_node_update(struct bat_priv *bat_priv,
  275. struct orig_node *orig_node, uint8_t new_gwflags)
  276. {
  277. struct hlist_node *node;
  278. struct gw_node *gw_node, *curr_gw;
  279. /**
  280. * Note: We don't need a NULL check here, since curr_gw never gets
  281. * dereferenced. If curr_gw is NULL we also should not exit as we may
  282. * have this gateway in our list (duplication check!) even though we
  283. * have no currently selected gateway.
  284. */
  285. curr_gw = gw_get_selected_gw_node(bat_priv);
  286. rcu_read_lock();
  287. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  288. if (gw_node->orig_node != orig_node)
  289. continue;
  290. bat_dbg(DBG_BATMAN, bat_priv,
  291. "Gateway class of originator %pM changed from %i to %i\n",
  292. orig_node->orig, gw_node->orig_node->gw_flags,
  293. new_gwflags);
  294. gw_node->deleted = 0;
  295. if (new_gwflags == NO_FLAGS) {
  296. gw_node->deleted = jiffies;
  297. bat_dbg(DBG_BATMAN, bat_priv,
  298. "Gateway %pM removed from gateway list\n",
  299. orig_node->orig);
  300. if (gw_node == curr_gw)
  301. goto deselect;
  302. }
  303. goto unlock;
  304. }
  305. if (new_gwflags == NO_FLAGS)
  306. goto unlock;
  307. gw_node_add(bat_priv, orig_node, new_gwflags);
  308. goto unlock;
  309. deselect:
  310. batadv_gw_deselect(bat_priv);
  311. unlock:
  312. rcu_read_unlock();
  313. if (curr_gw)
  314. gw_node_free_ref(curr_gw);
  315. }
  316. void batadv_gw_node_delete(struct bat_priv *bat_priv,
  317. struct orig_node *orig_node)
  318. {
  319. batadv_gw_node_update(bat_priv, orig_node, 0);
  320. }
  321. void batadv_gw_node_purge(struct bat_priv *bat_priv)
  322. {
  323. struct gw_node *gw_node, *curr_gw;
  324. struct hlist_node *node, *node_tmp;
  325. unsigned long timeout = msecs_to_jiffies(2 * PURGE_TIMEOUT);
  326. int do_deselect = 0;
  327. curr_gw = gw_get_selected_gw_node(bat_priv);
  328. spin_lock_bh(&bat_priv->gw_list_lock);
  329. hlist_for_each_entry_safe(gw_node, node, node_tmp,
  330. &bat_priv->gw_list, list) {
  331. if (((!gw_node->deleted) ||
  332. (time_before(jiffies, gw_node->deleted + timeout))) &&
  333. atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
  334. continue;
  335. if (curr_gw == gw_node)
  336. do_deselect = 1;
  337. hlist_del_rcu(&gw_node->list);
  338. gw_node_free_ref(gw_node);
  339. }
  340. spin_unlock_bh(&bat_priv->gw_list_lock);
  341. /* gw_deselect() needs to acquire the gw_list_lock */
  342. if (do_deselect)
  343. batadv_gw_deselect(bat_priv);
  344. if (curr_gw)
  345. gw_node_free_ref(curr_gw);
  346. }
  347. /**
  348. * fails if orig_node has no router
  349. */
  350. static int _write_buffer_text(struct bat_priv *bat_priv, struct seq_file *seq,
  351. const struct gw_node *gw_node)
  352. {
  353. struct gw_node *curr_gw;
  354. struct neigh_node *router;
  355. int down, up, ret = -1;
  356. batadv_gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
  357. router = orig_node_get_router(gw_node->orig_node);
  358. if (!router)
  359. goto out;
  360. curr_gw = gw_get_selected_gw_node(bat_priv);
  361. ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
  362. (curr_gw == gw_node ? "=>" : " "),
  363. gw_node->orig_node->orig,
  364. router->tq_avg, router->addr,
  365. router->if_incoming->net_dev->name,
  366. gw_node->orig_node->gw_flags,
  367. (down > 2048 ? down / 1024 : down),
  368. (down > 2048 ? "MBit" : "KBit"),
  369. (up > 2048 ? up / 1024 : up),
  370. (up > 2048 ? "MBit" : "KBit"));
  371. neigh_node_free_ref(router);
  372. if (curr_gw)
  373. gw_node_free_ref(curr_gw);
  374. out:
  375. return ret;
  376. }
  377. int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
  378. {
  379. struct net_device *net_dev = (struct net_device *)seq->private;
  380. struct bat_priv *bat_priv = netdev_priv(net_dev);
  381. struct hard_iface *primary_if;
  382. struct gw_node *gw_node;
  383. struct hlist_node *node;
  384. int gw_count = 0, ret = 0;
  385. primary_if = primary_if_get_selected(bat_priv);
  386. if (!primary_if) {
  387. ret = seq_printf(seq,
  388. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  389. net_dev->name);
  390. goto out;
  391. }
  392. if (primary_if->if_status != IF_ACTIVE) {
  393. ret = seq_printf(seq,
  394. "BATMAN mesh %s disabled - primary interface not active\n",
  395. net_dev->name);
  396. goto out;
  397. }
  398. seq_printf(seq,
  399. " %-12s (%s/%i) %17s [%10s]: gw_class ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
  400. "Gateway", "#", TQ_MAX_VALUE, "Nexthop", "outgoingIF",
  401. SOURCE_VERSION, primary_if->net_dev->name,
  402. primary_if->net_dev->dev_addr, net_dev->name);
  403. rcu_read_lock();
  404. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  405. if (gw_node->deleted)
  406. continue;
  407. /* fails if orig_node has no router */
  408. if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
  409. continue;
  410. gw_count++;
  411. }
  412. rcu_read_unlock();
  413. if (gw_count == 0)
  414. seq_printf(seq, "No gateways in range ...\n");
  415. out:
  416. if (primary_if)
  417. hardif_free_ref(primary_if);
  418. return ret;
  419. }
  420. static bool is_type_dhcprequest(struct sk_buff *skb, int header_len)
  421. {
  422. int ret = false;
  423. unsigned char *p;
  424. int pkt_len;
  425. if (skb_linearize(skb) < 0)
  426. goto out;
  427. pkt_len = skb_headlen(skb);
  428. if (pkt_len < header_len + DHCP_OPTIONS_OFFSET + 1)
  429. goto out;
  430. p = skb->data + header_len + DHCP_OPTIONS_OFFSET;
  431. pkt_len -= header_len + DHCP_OPTIONS_OFFSET + 1;
  432. /* Access the dhcp option lists. Each entry is made up by:
  433. * - octet 1: option type
  434. * - octet 2: option data len (only if type != 255 and 0)
  435. * - octet 3: option data */
  436. while (*p != 255 && !ret) {
  437. /* p now points to the first octet: option type */
  438. if (*p == 53) {
  439. /* type 53 is the message type option.
  440. * Jump the len octet and go to the data octet */
  441. if (pkt_len < 2)
  442. goto out;
  443. p += 2;
  444. /* check if the message type is what we need */
  445. if (*p == DHCP_REQUEST)
  446. ret = true;
  447. break;
  448. } else if (*p == 0) {
  449. /* option type 0 (padding), just go forward */
  450. if (pkt_len < 1)
  451. goto out;
  452. pkt_len--;
  453. p++;
  454. } else {
  455. /* This is any other option. So we get the length... */
  456. if (pkt_len < 1)
  457. goto out;
  458. pkt_len--;
  459. p++;
  460. /* ...and then we jump over the data */
  461. if (pkt_len < 1 + (*p))
  462. goto out;
  463. pkt_len -= 1 + (*p);
  464. p += 1 + (*p);
  465. }
  466. }
  467. out:
  468. return ret;
  469. }
  470. bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
  471. {
  472. struct ethhdr *ethhdr;
  473. struct iphdr *iphdr;
  474. struct ipv6hdr *ipv6hdr;
  475. struct udphdr *udphdr;
  476. /* check for ethernet header */
  477. if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
  478. return false;
  479. ethhdr = (struct ethhdr *)skb->data;
  480. *header_len += ETH_HLEN;
  481. /* check for initial vlan header */
  482. if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
  483. if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
  484. return false;
  485. ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
  486. *header_len += VLAN_HLEN;
  487. }
  488. /* check for ip header */
  489. switch (ntohs(ethhdr->h_proto)) {
  490. case ETH_P_IP:
  491. if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
  492. return false;
  493. iphdr = (struct iphdr *)(skb->data + *header_len);
  494. *header_len += iphdr->ihl * 4;
  495. /* check for udp header */
  496. if (iphdr->protocol != IPPROTO_UDP)
  497. return false;
  498. break;
  499. case ETH_P_IPV6:
  500. if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
  501. return false;
  502. ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
  503. *header_len += sizeof(*ipv6hdr);
  504. /* check for udp header */
  505. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  506. return false;
  507. break;
  508. default:
  509. return false;
  510. }
  511. if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
  512. return false;
  513. udphdr = (struct udphdr *)(skb->data + *header_len);
  514. *header_len += sizeof(*udphdr);
  515. /* check for bootp port */
  516. if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
  517. (ntohs(udphdr->dest) != 67))
  518. return false;
  519. if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
  520. (ntohs(udphdr->dest) != 547))
  521. return false;
  522. return true;
  523. }
  524. bool batadv_gw_out_of_range(struct bat_priv *bat_priv,
  525. struct sk_buff *skb, struct ethhdr *ethhdr)
  526. {
  527. struct neigh_node *neigh_curr = NULL, *neigh_old = NULL;
  528. struct orig_node *orig_dst_node = NULL;
  529. struct gw_node *curr_gw = NULL;
  530. bool ret, out_of_range = false;
  531. unsigned int header_len = 0;
  532. uint8_t curr_tq_avg;
  533. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  534. if (!ret)
  535. goto out;
  536. orig_dst_node = transtable_search(bat_priv, ethhdr->h_source,
  537. ethhdr->h_dest);
  538. if (!orig_dst_node)
  539. goto out;
  540. if (!orig_dst_node->gw_flags)
  541. goto out;
  542. ret = is_type_dhcprequest(skb, header_len);
  543. if (!ret)
  544. goto out;
  545. switch (atomic_read(&bat_priv->gw_mode)) {
  546. case GW_MODE_SERVER:
  547. /* If we are a GW then we are our best GW. We can artificially
  548. * set the tq towards ourself as the maximum value */
  549. curr_tq_avg = TQ_MAX_VALUE;
  550. break;
  551. case GW_MODE_CLIENT:
  552. curr_gw = gw_get_selected_gw_node(bat_priv);
  553. if (!curr_gw)
  554. goto out;
  555. /* packet is going to our gateway */
  556. if (curr_gw->orig_node == orig_dst_node)
  557. goto out;
  558. /* If the dhcp packet has been sent to a different gw,
  559. * we have to evaluate whether the old gw is still
  560. * reliable enough */
  561. neigh_curr = find_router(bat_priv, curr_gw->orig_node, NULL);
  562. if (!neigh_curr)
  563. goto out;
  564. curr_tq_avg = neigh_curr->tq_avg;
  565. break;
  566. case GW_MODE_OFF:
  567. default:
  568. goto out;
  569. }
  570. neigh_old = find_router(bat_priv, orig_dst_node, NULL);
  571. if (!neigh_old)
  572. goto out;
  573. if (curr_tq_avg - neigh_old->tq_avg > GW_THRESHOLD)
  574. out_of_range = true;
  575. out:
  576. if (orig_dst_node)
  577. orig_node_free_ref(orig_dst_node);
  578. if (curr_gw)
  579. gw_node_free_ref(curr_gw);
  580. if (neigh_old)
  581. neigh_node_free_ref(neigh_old);
  582. if (neigh_curr)
  583. neigh_node_free_ref(neigh_curr);
  584. return out_of_range;
  585. }