gateway_client.c 17 KB

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