gateway_client.c 19 KB

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