gateway_client.c 18 KB

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