gateway_client.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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 *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 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. 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. router = orig_node_get_router(gw_node->orig_node);
  104. if (!router)
  105. continue;
  106. if (!atomic_inc_not_zero(&gw_node->refcount))
  107. goto next;
  108. switch (atomic_read(&bat_priv->gw_sel_class)) {
  109. case 1: /* fast connection */
  110. gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags,
  111. &down, &up);
  112. tmp_gw_factor = (router->tq_avg * router->tq_avg *
  113. down * 100 * 100) /
  114. (TQ_LOCAL_WINDOW_SIZE *
  115. TQ_LOCAL_WINDOW_SIZE * 64);
  116. if ((tmp_gw_factor > max_gw_factor) ||
  117. ((tmp_gw_factor == max_gw_factor) &&
  118. (router->tq_avg > max_tq))) {
  119. if (curr_gw)
  120. gw_node_free_ref(curr_gw);
  121. curr_gw = gw_node;
  122. atomic_inc(&curr_gw->refcount);
  123. }
  124. break;
  125. default: /**
  126. * 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. neigh_node_free_ref(router);
  148. }
  149. rcu_read_unlock();
  150. return curr_gw;
  151. }
  152. void 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. /**
  158. * The batman daemon checks here if we already passed a full originator
  159. * cycle in order to make sure we don't choose the first gateway we
  160. * hear about. This check is based on the daemon's uptime which we
  161. * don't have.
  162. **/
  163. if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
  164. goto out;
  165. if (!atomic_dec_not_zero(&bat_priv->gw_reselect))
  166. goto out;
  167. curr_gw = gw_get_selected_gw_node(bat_priv);
  168. next_gw = gw_get_best_gw_node(bat_priv);
  169. if (curr_gw == next_gw)
  170. goto out;
  171. if (next_gw) {
  172. sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
  173. router = orig_node_get_router(next_gw->orig_node);
  174. if (!router) {
  175. gw_deselect(bat_priv);
  176. goto out;
  177. }
  178. }
  179. if ((curr_gw) && (!next_gw)) {
  180. bat_dbg(DBG_BATMAN, bat_priv,
  181. "Removing selected gateway - no gateway in range\n");
  182. throw_uevent(bat_priv, UEV_GW, UEV_DEL, NULL);
  183. } else if ((!curr_gw) && (next_gw)) {
  184. bat_dbg(DBG_BATMAN, bat_priv,
  185. "Adding route to gateway %pM (gw_flags: %i, tq: %i)\n",
  186. next_gw->orig_node->orig, next_gw->orig_node->gw_flags,
  187. router->tq_avg);
  188. throw_uevent(bat_priv, UEV_GW, UEV_ADD, gw_addr);
  189. } else {
  190. bat_dbg(DBG_BATMAN, bat_priv,
  191. "Changing route to gateway %pM (gw_flags: %i, tq: %i)\n",
  192. next_gw->orig_node->orig, next_gw->orig_node->gw_flags,
  193. router->tq_avg);
  194. throw_uevent(bat_priv, UEV_GW, UEV_CHANGE, gw_addr);
  195. }
  196. gw_select(bat_priv, next_gw);
  197. out:
  198. if (curr_gw)
  199. gw_node_free_ref(curr_gw);
  200. if (next_gw)
  201. gw_node_free_ref(next_gw);
  202. if (router)
  203. neigh_node_free_ref(router);
  204. }
  205. void gw_check_election(struct bat_priv *bat_priv, 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 = gw_get_selected_orig(bat_priv);
  211. if (!curr_gw_orig)
  212. goto deselect;
  213. router_gw = 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 = 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. /**
  228. * if the routing class is greater than 3 the value tells us how much
  229. * greater the TQ value of the new gateway must be
  230. **/
  231. if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
  232. (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
  233. goto out;
  234. bat_dbg(DBG_BATMAN, bat_priv,
  235. "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
  236. gw_tq_avg, orig_tq_avg);
  237. deselect:
  238. gw_deselect(bat_priv);
  239. out:
  240. if (curr_gw_orig)
  241. orig_node_free_ref(curr_gw_orig);
  242. if (router_gw)
  243. neigh_node_free_ref(router_gw);
  244. if (router_orig)
  245. neigh_node_free_ref(router_orig);
  246. return;
  247. }
  248. static void gw_node_add(struct bat_priv *bat_priv,
  249. struct orig_node *orig_node, uint8_t new_gwflags)
  250. {
  251. struct gw_node *gw_node;
  252. int down, up;
  253. gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
  254. if (!gw_node)
  255. return;
  256. INIT_HLIST_NODE(&gw_node->list);
  257. gw_node->orig_node = orig_node;
  258. atomic_set(&gw_node->refcount, 1);
  259. spin_lock_bh(&bat_priv->gw_list_lock);
  260. hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
  261. spin_unlock_bh(&bat_priv->gw_list_lock);
  262. gw_bandwidth_to_kbit(new_gwflags, &down, &up);
  263. bat_dbg(DBG_BATMAN, bat_priv,
  264. "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n",
  265. orig_node->orig, new_gwflags,
  266. (down > 2048 ? down / 1024 : down),
  267. (down > 2048 ? "MBit" : "KBit"),
  268. (up > 2048 ? up / 1024 : up),
  269. (up > 2048 ? "MBit" : "KBit"));
  270. }
  271. void gw_node_update(struct bat_priv *bat_priv,
  272. struct orig_node *orig_node, uint8_t new_gwflags)
  273. {
  274. struct hlist_node *node;
  275. struct gw_node *gw_node, *curr_gw;
  276. /**
  277. * Note: We don't need a NULL check here, since curr_gw never gets
  278. * dereferenced. If curr_gw is NULL we also should not exit as we may
  279. * have this gateway in our list (duplication check!) even though we
  280. * have no currently selected gateway.
  281. */
  282. curr_gw = gw_get_selected_gw_node(bat_priv);
  283. rcu_read_lock();
  284. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  285. if (gw_node->orig_node != orig_node)
  286. continue;
  287. bat_dbg(DBG_BATMAN, bat_priv,
  288. "Gateway class of originator %pM changed from %i to %i\n",
  289. orig_node->orig, gw_node->orig_node->gw_flags,
  290. new_gwflags);
  291. gw_node->deleted = 0;
  292. if (new_gwflags == NO_FLAGS) {
  293. gw_node->deleted = jiffies;
  294. bat_dbg(DBG_BATMAN, bat_priv,
  295. "Gateway %pM removed from gateway list\n",
  296. orig_node->orig);
  297. if (gw_node == curr_gw)
  298. goto deselect;
  299. }
  300. goto unlock;
  301. }
  302. if (new_gwflags == NO_FLAGS)
  303. goto unlock;
  304. gw_node_add(bat_priv, orig_node, new_gwflags);
  305. goto unlock;
  306. deselect:
  307. gw_deselect(bat_priv);
  308. unlock:
  309. rcu_read_unlock();
  310. if (curr_gw)
  311. gw_node_free_ref(curr_gw);
  312. }
  313. void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
  314. {
  315. gw_node_update(bat_priv, orig_node, 0);
  316. }
  317. void gw_node_purge(struct bat_priv *bat_priv)
  318. {
  319. struct gw_node *gw_node, *curr_gw;
  320. struct hlist_node *node, *node_tmp;
  321. unsigned long timeout = msecs_to_jiffies(2 * PURGE_TIMEOUT);
  322. int do_deselect = 0;
  323. curr_gw = gw_get_selected_gw_node(bat_priv);
  324. spin_lock_bh(&bat_priv->gw_list_lock);
  325. hlist_for_each_entry_safe(gw_node, node, node_tmp,
  326. &bat_priv->gw_list, list) {
  327. if (((!gw_node->deleted) ||
  328. (time_before(jiffies, gw_node->deleted + timeout))) &&
  329. atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
  330. continue;
  331. if (curr_gw == gw_node)
  332. do_deselect = 1;
  333. hlist_del_rcu(&gw_node->list);
  334. gw_node_free_ref(gw_node);
  335. }
  336. spin_unlock_bh(&bat_priv->gw_list_lock);
  337. /* gw_deselect() needs to acquire the gw_list_lock */
  338. if (do_deselect)
  339. gw_deselect(bat_priv);
  340. if (curr_gw)
  341. gw_node_free_ref(curr_gw);
  342. }
  343. /**
  344. * fails if orig_node has no router
  345. */
  346. static int _write_buffer_text(struct bat_priv *bat_priv, struct seq_file *seq,
  347. const struct gw_node *gw_node)
  348. {
  349. struct gw_node *curr_gw;
  350. struct neigh_node *router;
  351. int down, up, ret = -1;
  352. gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
  353. router = orig_node_get_router(gw_node->orig_node);
  354. if (!router)
  355. goto out;
  356. curr_gw = gw_get_selected_gw_node(bat_priv);
  357. ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
  358. (curr_gw == gw_node ? "=>" : " "),
  359. gw_node->orig_node->orig,
  360. router->tq_avg, router->addr,
  361. router->if_incoming->net_dev->name,
  362. gw_node->orig_node->gw_flags,
  363. (down > 2048 ? down / 1024 : down),
  364. (down > 2048 ? "MBit" : "KBit"),
  365. (up > 2048 ? up / 1024 : up),
  366. (up > 2048 ? "MBit" : "KBit"));
  367. neigh_node_free_ref(router);
  368. if (curr_gw)
  369. gw_node_free_ref(curr_gw);
  370. out:
  371. return ret;
  372. }
  373. int gw_client_seq_print_text(struct seq_file *seq, void *offset)
  374. {
  375. struct net_device *net_dev = (struct net_device *)seq->private;
  376. struct bat_priv *bat_priv = netdev_priv(net_dev);
  377. struct hard_iface *primary_if;
  378. struct gw_node *gw_node;
  379. struct hlist_node *node;
  380. int gw_count = 0, ret = 0;
  381. primary_if = primary_if_get_selected(bat_priv);
  382. if (!primary_if) {
  383. ret = seq_printf(seq,
  384. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  385. net_dev->name);
  386. goto out;
  387. }
  388. if (primary_if->if_status != IF_ACTIVE) {
  389. ret = seq_printf(seq,
  390. "BATMAN mesh %s disabled - primary interface not active\n",
  391. net_dev->name);
  392. goto out;
  393. }
  394. seq_printf(seq,
  395. " %-12s (%s/%i) %17s [%10s]: gw_class ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
  396. "Gateway", "#", TQ_MAX_VALUE, "Nexthop", "outgoingIF",
  397. SOURCE_VERSION, primary_if->net_dev->name,
  398. primary_if->net_dev->dev_addr, net_dev->name);
  399. rcu_read_lock();
  400. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  401. if (gw_node->deleted)
  402. continue;
  403. /* fails if orig_node has no router */
  404. if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
  405. continue;
  406. gw_count++;
  407. }
  408. rcu_read_unlock();
  409. if (gw_count == 0)
  410. seq_printf(seq, "No gateways in range ...\n");
  411. out:
  412. if (primary_if)
  413. hardif_free_ref(primary_if);
  414. return ret;
  415. }
  416. static bool is_type_dhcprequest(struct sk_buff *skb, int header_len)
  417. {
  418. int ret = false;
  419. unsigned char *p;
  420. int pkt_len;
  421. if (skb_linearize(skb) < 0)
  422. goto out;
  423. pkt_len = skb_headlen(skb);
  424. if (pkt_len < header_len + DHCP_OPTIONS_OFFSET + 1)
  425. goto out;
  426. p = skb->data + header_len + DHCP_OPTIONS_OFFSET;
  427. pkt_len -= header_len + DHCP_OPTIONS_OFFSET + 1;
  428. /* Access the dhcp option lists. Each entry is made up by:
  429. * - octet 1: option type
  430. * - octet 2: option data len (only if type != 255 and 0)
  431. * - octet 3: option data */
  432. while (*p != 255 && !ret) {
  433. /* p now points to the first octet: option type */
  434. if (*p == 53) {
  435. /* type 53 is the message type option.
  436. * Jump the len octet and go to the data octet */
  437. if (pkt_len < 2)
  438. goto out;
  439. p += 2;
  440. /* check if the message type is what we need */
  441. if (*p == DHCP_REQUEST)
  442. ret = true;
  443. break;
  444. } else if (*p == 0) {
  445. /* option type 0 (padding), just go forward */
  446. if (pkt_len < 1)
  447. goto out;
  448. pkt_len--;
  449. p++;
  450. } else {
  451. /* This is any other option. So we get the length... */
  452. if (pkt_len < 1)
  453. goto out;
  454. pkt_len--;
  455. p++;
  456. /* ...and then we jump over the data */
  457. if (pkt_len < *p)
  458. goto out;
  459. pkt_len -= *p;
  460. p += (*p);
  461. }
  462. }
  463. out:
  464. return ret;
  465. }
  466. bool gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
  467. {
  468. struct ethhdr *ethhdr;
  469. struct iphdr *iphdr;
  470. struct ipv6hdr *ipv6hdr;
  471. struct udphdr *udphdr;
  472. /* check for ethernet header */
  473. if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
  474. return false;
  475. ethhdr = (struct ethhdr *)skb->data;
  476. *header_len += ETH_HLEN;
  477. /* check for initial vlan header */
  478. if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
  479. if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
  480. return false;
  481. ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
  482. *header_len += VLAN_HLEN;
  483. }
  484. /* check for ip header */
  485. switch (ntohs(ethhdr->h_proto)) {
  486. case ETH_P_IP:
  487. if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
  488. return false;
  489. iphdr = (struct iphdr *)(skb->data + *header_len);
  490. *header_len += iphdr->ihl * 4;
  491. /* check for udp header */
  492. if (iphdr->protocol != IPPROTO_UDP)
  493. return false;
  494. break;
  495. case ETH_P_IPV6:
  496. if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
  497. return false;
  498. ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
  499. *header_len += sizeof(*ipv6hdr);
  500. /* check for udp header */
  501. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  502. return false;
  503. break;
  504. default:
  505. return false;
  506. }
  507. if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
  508. return false;
  509. udphdr = (struct udphdr *)(skb->data + *header_len);
  510. *header_len += sizeof(*udphdr);
  511. /* check for bootp port */
  512. if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
  513. (ntohs(udphdr->dest) != 67))
  514. return false;
  515. if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
  516. (ntohs(udphdr->dest) != 547))
  517. return false;
  518. return true;
  519. }
  520. bool gw_out_of_range(struct bat_priv *bat_priv,
  521. struct sk_buff *skb, struct ethhdr *ethhdr)
  522. {
  523. struct neigh_node *neigh_curr = NULL, *neigh_old = NULL;
  524. struct orig_node *orig_dst_node = NULL;
  525. struct gw_node *curr_gw = NULL;
  526. bool ret, out_of_range = false;
  527. unsigned int header_len = 0;
  528. uint8_t curr_tq_avg;
  529. ret = gw_is_dhcp_target(skb, &header_len);
  530. if (!ret)
  531. goto out;
  532. orig_dst_node = transtable_search(bat_priv, ethhdr->h_source,
  533. ethhdr->h_dest);
  534. if (!orig_dst_node)
  535. goto out;
  536. if (!orig_dst_node->gw_flags)
  537. goto out;
  538. ret = is_type_dhcprequest(skb, header_len);
  539. if (!ret)
  540. goto out;
  541. switch (atomic_read(&bat_priv->gw_mode)) {
  542. case GW_MODE_SERVER:
  543. /* If we are a GW then we are our best GW. We can artificially
  544. * set the tq towards ourself as the maximum value */
  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. neigh_curr = find_router(bat_priv, curr_gw->orig_node, NULL);
  558. if (!neigh_curr)
  559. goto out;
  560. curr_tq_avg = neigh_curr->tq_avg;
  561. break;
  562. case GW_MODE_OFF:
  563. default:
  564. goto out;
  565. }
  566. neigh_old = find_router(bat_priv, orig_dst_node, NULL);
  567. if (!neigh_old)
  568. goto out;
  569. if (curr_tq_avg - neigh_old->tq_avg > GW_THRESHOLD)
  570. out_of_range = true;
  571. out:
  572. if (orig_dst_node)
  573. orig_node_free_ref(orig_dst_node);
  574. if (curr_gw)
  575. gw_node_free_ref(curr_gw);
  576. if (neigh_old)
  577. neigh_node_free_ref(neigh_old);
  578. if (neigh_curr)
  579. neigh_node_free_ref(neigh_curr);
  580. return out_of_range;
  581. }