gateway_client.c 17 KB

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