gateway_client.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. uint8_t tq_avg;
  102. struct batadv_orig_node *orig_node;
  103. gw_divisor = BATADV_TQ_LOCAL_WINDOW_SIZE * BATADV_TQ_LOCAL_WINDOW_SIZE;
  104. gw_divisor *= 64;
  105. rcu_read_lock();
  106. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
  107. if (gw_node->deleted)
  108. continue;
  109. orig_node = gw_node->orig_node;
  110. router = batadv_orig_node_get_router(orig_node);
  111. if (!router)
  112. continue;
  113. if (!atomic_inc_not_zero(&gw_node->refcount))
  114. goto next;
  115. tq_avg = router->bat_iv.tq_avg;
  116. switch (atomic_read(&bat_priv->gw_sel_class)) {
  117. case 1: /* fast connection */
  118. tmp_gw_factor = tq_avg * tq_avg;
  119. tmp_gw_factor *= gw_node->bandwidth_down;
  120. tmp_gw_factor *= 100 * 100;
  121. tmp_gw_factor /= gw_divisor;
  122. if ((tmp_gw_factor > max_gw_factor) ||
  123. ((tmp_gw_factor == max_gw_factor) &&
  124. (tq_avg > max_tq))) {
  125. if (curr_gw)
  126. batadv_gw_node_free_ref(curr_gw);
  127. curr_gw = gw_node;
  128. atomic_inc(&curr_gw->refcount);
  129. }
  130. break;
  131. default: /* 2: stable connection (use best statistic)
  132. * 3: fast-switch (use best statistic but change as
  133. * soon as a better gateway appears)
  134. * XX: late-switch (use best statistic but change as
  135. * soon as a better gateway appears which has
  136. * $routing_class more tq points)
  137. */
  138. if (tq_avg > max_tq) {
  139. if (curr_gw)
  140. batadv_gw_node_free_ref(curr_gw);
  141. curr_gw = gw_node;
  142. atomic_inc(&curr_gw->refcount);
  143. }
  144. break;
  145. }
  146. if (tq_avg > max_tq)
  147. max_tq = tq_avg;
  148. if (tmp_gw_factor > max_gw_factor)
  149. max_gw_factor = tmp_gw_factor;
  150. batadv_gw_node_free_ref(gw_node);
  151. next:
  152. batadv_neigh_node_free_ref(router);
  153. }
  154. rcu_read_unlock();
  155. return curr_gw;
  156. }
  157. /**
  158. * batadv_gw_check_client_stop - check if client mode has been switched off
  159. * @bat_priv: the bat priv with all the soft interface information
  160. *
  161. * This function assumes the caller has checked that the gw state *is actually
  162. * changing*. This function is not supposed to be called when there is no state
  163. * change.
  164. */
  165. void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
  166. {
  167. struct batadv_gw_node *curr_gw;
  168. if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
  169. return;
  170. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  171. if (!curr_gw)
  172. return;
  173. /* if batman-adv is switching the gw client mode off and a gateway was
  174. * already selected, send a DEL uevent
  175. */
  176. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
  177. batadv_gw_node_free_ref(curr_gw);
  178. }
  179. void batadv_gw_election(struct batadv_priv *bat_priv)
  180. {
  181. struct batadv_gw_node *curr_gw = NULL, *next_gw = NULL;
  182. struct batadv_neigh_node *router = NULL;
  183. char gw_addr[18] = { '\0' };
  184. if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
  185. goto out;
  186. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  187. if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
  188. goto out;
  189. next_gw = batadv_gw_get_best_gw_node(bat_priv);
  190. if (curr_gw == next_gw)
  191. goto out;
  192. if (next_gw) {
  193. sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
  194. router = batadv_orig_node_get_router(next_gw->orig_node);
  195. if (!router) {
  196. batadv_gw_deselect(bat_priv);
  197. goto out;
  198. }
  199. }
  200. if ((curr_gw) && (!next_gw)) {
  201. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  202. "Removing selected gateway - no gateway in range\n");
  203. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
  204. NULL);
  205. } else if ((!curr_gw) && (next_gw)) {
  206. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  207. "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
  208. next_gw->orig_node->orig,
  209. next_gw->bandwidth_down / 10,
  210. next_gw->bandwidth_down % 10,
  211. next_gw->bandwidth_up / 10,
  212. next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
  213. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
  214. gw_addr);
  215. } else {
  216. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  217. "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
  218. next_gw->orig_node->orig,
  219. next_gw->bandwidth_down / 10,
  220. next_gw->bandwidth_down % 10,
  221. next_gw->bandwidth_up / 10,
  222. next_gw->bandwidth_up % 10, router->bat_iv.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->bat_iv.tq_avg;
  254. orig_tq_avg = router_orig->bat_iv.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. /**
  279. * batadv_gw_node_add - add gateway node to list of available gateways
  280. * @bat_priv: the bat priv with all the soft interface information
  281. * @orig_node: originator announcing gateway capabilities
  282. * @gateway: announced bandwidth information
  283. */
  284. static void batadv_gw_node_add(struct batadv_priv *bat_priv,
  285. struct batadv_orig_node *orig_node,
  286. struct batadv_tvlv_gateway_data *gateway)
  287. {
  288. struct batadv_gw_node *gw_node;
  289. if (gateway->bandwidth_down == 0)
  290. return;
  291. gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
  292. if (!gw_node)
  293. return;
  294. INIT_HLIST_NODE(&gw_node->list);
  295. gw_node->orig_node = orig_node;
  296. atomic_set(&gw_node->refcount, 1);
  297. spin_lock_bh(&bat_priv->gw.list_lock);
  298. hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
  299. spin_unlock_bh(&bat_priv->gw.list_lock);
  300. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  301. "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
  302. orig_node->orig,
  303. ntohl(gateway->bandwidth_down) / 10,
  304. ntohl(gateway->bandwidth_down) % 10,
  305. ntohl(gateway->bandwidth_up) / 10,
  306. ntohl(gateway->bandwidth_up) % 10);
  307. }
  308. /**
  309. * batadv_gw_node_get - retrieve gateway node from list of available gateways
  310. * @bat_priv: the bat priv with all the soft interface information
  311. * @orig_node: originator announcing gateway capabilities
  312. *
  313. * Returns gateway node if found or NULL otherwise.
  314. */
  315. static struct batadv_gw_node *
  316. batadv_gw_node_get(struct batadv_priv *bat_priv,
  317. struct batadv_orig_node *orig_node)
  318. {
  319. struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
  320. rcu_read_lock();
  321. hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
  322. if (gw_node_tmp->orig_node != orig_node)
  323. continue;
  324. if (gw_node_tmp->deleted)
  325. continue;
  326. if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
  327. continue;
  328. gw_node = gw_node_tmp;
  329. break;
  330. }
  331. rcu_read_unlock();
  332. return gw_node;
  333. }
  334. /**
  335. * batadv_gw_node_update - update list of available gateways with changed
  336. * bandwidth information
  337. * @bat_priv: the bat priv with all the soft interface information
  338. * @orig_node: originator announcing gateway capabilities
  339. * @gateway: announced bandwidth information
  340. */
  341. void batadv_gw_node_update(struct batadv_priv *bat_priv,
  342. struct batadv_orig_node *orig_node,
  343. struct batadv_tvlv_gateway_data *gateway)
  344. {
  345. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  346. gw_node = batadv_gw_node_get(bat_priv, orig_node);
  347. if (!gw_node) {
  348. batadv_gw_node_add(bat_priv, orig_node, gateway);
  349. goto out;
  350. }
  351. if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
  352. (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
  353. goto out;
  354. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  355. "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
  356. orig_node->orig,
  357. gw_node->bandwidth_down / 10,
  358. gw_node->bandwidth_down % 10,
  359. gw_node->bandwidth_up / 10,
  360. gw_node->bandwidth_up % 10,
  361. ntohl(gateway->bandwidth_down) / 10,
  362. ntohl(gateway->bandwidth_down) % 10,
  363. ntohl(gateway->bandwidth_up) / 10,
  364. ntohl(gateway->bandwidth_up) % 10);
  365. gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
  366. gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
  367. gw_node->deleted = 0;
  368. if (ntohl(gateway->bandwidth_down) == 0) {
  369. gw_node->deleted = jiffies;
  370. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  371. "Gateway %pM removed from gateway list\n",
  372. orig_node->orig);
  373. /* Note: We don't need a NULL check here, since curr_gw never
  374. * gets dereferenced.
  375. */
  376. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  377. if (gw_node == curr_gw)
  378. batadv_gw_deselect(bat_priv);
  379. }
  380. out:
  381. if (curr_gw)
  382. batadv_gw_node_free_ref(curr_gw);
  383. if (gw_node)
  384. batadv_gw_node_free_ref(gw_node);
  385. }
  386. void batadv_gw_node_delete(struct batadv_priv *bat_priv,
  387. struct batadv_orig_node *orig_node)
  388. {
  389. struct batadv_tvlv_gateway_data gateway;
  390. gateway.bandwidth_down = 0;
  391. gateway.bandwidth_up = 0;
  392. batadv_gw_node_update(bat_priv, orig_node, &gateway);
  393. }
  394. void batadv_gw_node_purge(struct batadv_priv *bat_priv)
  395. {
  396. struct batadv_gw_node *gw_node, *curr_gw;
  397. struct hlist_node *node_tmp;
  398. unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
  399. int do_deselect = 0;
  400. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  401. spin_lock_bh(&bat_priv->gw.list_lock);
  402. hlist_for_each_entry_safe(gw_node, node_tmp,
  403. &bat_priv->gw.list, list) {
  404. if (((!gw_node->deleted) ||
  405. (time_before(jiffies, gw_node->deleted + timeout))) &&
  406. atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
  407. continue;
  408. if (curr_gw == gw_node)
  409. do_deselect = 1;
  410. hlist_del_rcu(&gw_node->list);
  411. batadv_gw_node_free_ref(gw_node);
  412. }
  413. spin_unlock_bh(&bat_priv->gw.list_lock);
  414. /* gw_deselect() needs to acquire the gw_list_lock */
  415. if (do_deselect)
  416. batadv_gw_deselect(bat_priv);
  417. if (curr_gw)
  418. batadv_gw_node_free_ref(curr_gw);
  419. }
  420. /* fails if orig_node has no router */
  421. static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
  422. struct seq_file *seq,
  423. const struct batadv_gw_node *gw_node)
  424. {
  425. struct batadv_gw_node *curr_gw;
  426. struct batadv_neigh_node *router;
  427. int ret = -1;
  428. router = batadv_orig_node_get_router(gw_node->orig_node);
  429. if (!router)
  430. goto out;
  431. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  432. ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
  433. (curr_gw == gw_node ? "=>" : " "),
  434. gw_node->orig_node->orig,
  435. router->bat_iv.tq_avg, router->addr,
  436. router->if_incoming->net_dev->name,
  437. gw_node->bandwidth_down / 10,
  438. gw_node->bandwidth_down % 10,
  439. gw_node->bandwidth_up / 10,
  440. gw_node->bandwidth_up % 10);
  441. batadv_neigh_node_free_ref(router);
  442. if (curr_gw)
  443. batadv_gw_node_free_ref(curr_gw);
  444. out:
  445. return ret;
  446. }
  447. int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
  448. {
  449. struct net_device *net_dev = (struct net_device *)seq->private;
  450. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  451. struct batadv_hard_iface *primary_if;
  452. struct batadv_gw_node *gw_node;
  453. int gw_count = 0;
  454. primary_if = batadv_seq_print_text_primary_if_get(seq);
  455. if (!primary_if)
  456. goto out;
  457. seq_printf(seq,
  458. " %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
  459. "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
  460. BATADV_SOURCE_VERSION, primary_if->net_dev->name,
  461. primary_if->net_dev->dev_addr, net_dev->name);
  462. rcu_read_lock();
  463. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
  464. if (gw_node->deleted)
  465. continue;
  466. /* fails if orig_node has no router */
  467. if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
  468. continue;
  469. gw_count++;
  470. }
  471. rcu_read_unlock();
  472. if (gw_count == 0)
  473. seq_puts(seq, "No gateways in range ...\n");
  474. out:
  475. if (primary_if)
  476. batadv_hardif_free_ref(primary_if);
  477. return 0;
  478. }
  479. /* this call might reallocate skb data */
  480. static bool batadv_is_type_dhcprequest(struct sk_buff *skb, int header_len)
  481. {
  482. int ret = false;
  483. unsigned char *p;
  484. int pkt_len;
  485. if (skb_linearize(skb) < 0)
  486. goto out;
  487. pkt_len = skb_headlen(skb);
  488. if (pkt_len < header_len + BATADV_DHCP_OPTIONS_OFFSET + 1)
  489. goto out;
  490. p = skb->data + header_len + BATADV_DHCP_OPTIONS_OFFSET;
  491. pkt_len -= header_len + BATADV_DHCP_OPTIONS_OFFSET + 1;
  492. /* Access the dhcp option lists. Each entry is made up by:
  493. * - octet 1: option type
  494. * - octet 2: option data len (only if type != 255 and 0)
  495. * - octet 3: option data
  496. */
  497. while (*p != 255 && !ret) {
  498. /* p now points to the first octet: option type */
  499. if (*p == 53) {
  500. /* type 53 is the message type option.
  501. * Jump the len octet and go to the data octet
  502. */
  503. if (pkt_len < 2)
  504. goto out;
  505. p += 2;
  506. /* check if the message type is what we need */
  507. if (*p == BATADV_DHCP_REQUEST)
  508. ret = true;
  509. break;
  510. } else if (*p == 0) {
  511. /* option type 0 (padding), just go forward */
  512. if (pkt_len < 1)
  513. goto out;
  514. pkt_len--;
  515. p++;
  516. } else {
  517. /* This is any other option. So we get the length... */
  518. if (pkt_len < 1)
  519. goto out;
  520. pkt_len--;
  521. p++;
  522. /* ...and then we jump over the data */
  523. if (pkt_len < 1 + (*p))
  524. goto out;
  525. pkt_len -= 1 + (*p);
  526. p += 1 + (*p);
  527. }
  528. }
  529. out:
  530. return ret;
  531. }
  532. /* this call might reallocate skb data */
  533. bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
  534. {
  535. struct ethhdr *ethhdr;
  536. struct iphdr *iphdr;
  537. struct ipv6hdr *ipv6hdr;
  538. struct udphdr *udphdr;
  539. struct vlan_ethhdr *vhdr;
  540. __be16 proto;
  541. /* check for ethernet header */
  542. if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
  543. return false;
  544. ethhdr = (struct ethhdr *)skb->data;
  545. proto = ethhdr->h_proto;
  546. *header_len += ETH_HLEN;
  547. /* check for initial vlan header */
  548. if (proto == htons(ETH_P_8021Q)) {
  549. if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
  550. return false;
  551. vhdr = (struct vlan_ethhdr *)skb->data;
  552. proto = vhdr->h_vlan_encapsulated_proto;
  553. *header_len += VLAN_HLEN;
  554. }
  555. /* check for ip header */
  556. switch (proto) {
  557. case htons(ETH_P_IP):
  558. if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
  559. return false;
  560. iphdr = (struct iphdr *)(skb->data + *header_len);
  561. *header_len += iphdr->ihl * 4;
  562. /* check for udp header */
  563. if (iphdr->protocol != IPPROTO_UDP)
  564. return false;
  565. break;
  566. case htons(ETH_P_IPV6):
  567. if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
  568. return false;
  569. ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
  570. *header_len += sizeof(*ipv6hdr);
  571. /* check for udp header */
  572. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  573. return false;
  574. break;
  575. default:
  576. return false;
  577. }
  578. if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
  579. return false;
  580. /* skb->data might have been reallocated by pskb_may_pull() */
  581. ethhdr = (struct ethhdr *)skb->data;
  582. if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
  583. ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
  584. udphdr = (struct udphdr *)(skb->data + *header_len);
  585. *header_len += sizeof(*udphdr);
  586. /* check for bootp port */
  587. if ((proto == htons(ETH_P_IP)) &&
  588. (udphdr->dest != htons(67)))
  589. return false;
  590. if ((proto == htons(ETH_P_IPV6)) &&
  591. (udphdr->dest != htons(547)))
  592. return false;
  593. return true;
  594. }
  595. /**
  596. * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
  597. * @bat_priv: the bat priv with all the soft interface information
  598. * @skb: the outgoing packet
  599. *
  600. * Check if the skb is a DHCP request and if it is sent to the current best GW
  601. * server. Due to topology changes it may be the case that the GW server
  602. * previously selected is not the best one anymore.
  603. *
  604. * Returns true if the packet destination is unicast and it is not the best gw,
  605. * false otherwise.
  606. *
  607. * This call might reallocate skb data.
  608. */
  609. bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
  610. struct sk_buff *skb)
  611. {
  612. struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
  613. struct batadv_orig_node *orig_dst_node = NULL;
  614. struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
  615. struct ethhdr *ethhdr;
  616. bool ret, out_of_range = false;
  617. unsigned int header_len = 0;
  618. uint8_t curr_tq_avg;
  619. unsigned short vid;
  620. vid = batadv_get_vid(skb, 0);
  621. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  622. if (!ret)
  623. goto out;
  624. ethhdr = (struct ethhdr *)skb->data;
  625. orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
  626. ethhdr->h_dest, vid);
  627. if (!orig_dst_node)
  628. goto out;
  629. gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
  630. if (!gw_node->bandwidth_down == 0)
  631. goto out;
  632. ret = batadv_is_type_dhcprequest(skb, header_len);
  633. if (!ret)
  634. goto out;
  635. switch (atomic_read(&bat_priv->gw_mode)) {
  636. case BATADV_GW_MODE_SERVER:
  637. /* If we are a GW then we are our best GW. We can artificially
  638. * set the tq towards ourself as the maximum value
  639. */
  640. curr_tq_avg = BATADV_TQ_MAX_VALUE;
  641. break;
  642. case BATADV_GW_MODE_CLIENT:
  643. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  644. if (!curr_gw)
  645. goto out;
  646. /* packet is going to our gateway */
  647. if (curr_gw->orig_node == orig_dst_node)
  648. goto out;
  649. /* If the dhcp packet has been sent to a different gw,
  650. * we have to evaluate whether the old gw is still
  651. * reliable enough
  652. */
  653. neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
  654. NULL);
  655. if (!neigh_curr)
  656. goto out;
  657. curr_tq_avg = neigh_curr->bat_iv.tq_avg;
  658. break;
  659. case BATADV_GW_MODE_OFF:
  660. default:
  661. goto out;
  662. }
  663. neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
  664. if (!neigh_old)
  665. goto out;
  666. if (curr_tq_avg - neigh_old->bat_iv.tq_avg > BATADV_GW_THRESHOLD)
  667. out_of_range = true;
  668. out:
  669. if (orig_dst_node)
  670. batadv_orig_node_free_ref(orig_dst_node);
  671. if (curr_gw)
  672. batadv_gw_node_free_ref(curr_gw);
  673. if (gw_node)
  674. batadv_gw_node_free_ref(gw_node);
  675. if (neigh_old)
  676. batadv_neigh_node_free_ref(neigh_old);
  677. if (neigh_curr)
  678. batadv_neigh_node_free_ref(neigh_curr);
  679. return out_of_range;
  680. }