gateway_client.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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->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. /* The batman daemon checks here if we already passed a full originator
  185. * cycle in order to make sure we don't choose the first gateway we
  186. * hear about. This check is based on the daemon's uptime which we
  187. * don't have.
  188. */
  189. if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
  190. goto out;
  191. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  192. if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
  193. goto out;
  194. next_gw = batadv_gw_get_best_gw_node(bat_priv);
  195. if (curr_gw == next_gw)
  196. goto out;
  197. if (next_gw) {
  198. sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
  199. router = batadv_orig_node_get_router(next_gw->orig_node);
  200. if (!router) {
  201. batadv_gw_deselect(bat_priv);
  202. goto out;
  203. }
  204. }
  205. if ((curr_gw) && (!next_gw)) {
  206. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  207. "Removing selected gateway - no gateway in range\n");
  208. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
  209. NULL);
  210. } else if ((!curr_gw) && (next_gw)) {
  211. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  212. "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
  213. next_gw->orig_node->orig,
  214. next_gw->bandwidth_down / 10,
  215. next_gw->bandwidth_down % 10,
  216. next_gw->bandwidth_up / 10,
  217. next_gw->bandwidth_up % 10, router->tq_avg);
  218. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
  219. gw_addr);
  220. } else {
  221. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  222. "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
  223. next_gw->orig_node->orig,
  224. next_gw->bandwidth_down / 10,
  225. next_gw->bandwidth_down % 10,
  226. next_gw->bandwidth_up / 10,
  227. next_gw->bandwidth_up % 10, router->tq_avg);
  228. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
  229. gw_addr);
  230. }
  231. batadv_gw_select(bat_priv, next_gw);
  232. out:
  233. if (curr_gw)
  234. batadv_gw_node_free_ref(curr_gw);
  235. if (next_gw)
  236. batadv_gw_node_free_ref(next_gw);
  237. if (router)
  238. batadv_neigh_node_free_ref(router);
  239. }
  240. void batadv_gw_check_election(struct batadv_priv *bat_priv,
  241. struct batadv_orig_node *orig_node)
  242. {
  243. struct batadv_orig_node *curr_gw_orig;
  244. struct batadv_neigh_node *router_gw = NULL, *router_orig = NULL;
  245. uint8_t gw_tq_avg, orig_tq_avg;
  246. curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
  247. if (!curr_gw_orig)
  248. goto deselect;
  249. router_gw = batadv_orig_node_get_router(curr_gw_orig);
  250. if (!router_gw)
  251. goto deselect;
  252. /* this node already is the gateway */
  253. if (curr_gw_orig == orig_node)
  254. goto out;
  255. router_orig = batadv_orig_node_get_router(orig_node);
  256. if (!router_orig)
  257. goto out;
  258. gw_tq_avg = router_gw->tq_avg;
  259. orig_tq_avg = router_orig->tq_avg;
  260. /* the TQ value has to be better */
  261. if (orig_tq_avg < gw_tq_avg)
  262. goto out;
  263. /* if the routing class is greater than 3 the value tells us how much
  264. * greater the TQ value of the new gateway must be
  265. */
  266. if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
  267. (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
  268. goto out;
  269. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  270. "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
  271. gw_tq_avg, orig_tq_avg);
  272. deselect:
  273. batadv_gw_deselect(bat_priv);
  274. out:
  275. if (curr_gw_orig)
  276. batadv_orig_node_free_ref(curr_gw_orig);
  277. if (router_gw)
  278. batadv_neigh_node_free_ref(router_gw);
  279. if (router_orig)
  280. batadv_neigh_node_free_ref(router_orig);
  281. return;
  282. }
  283. /**
  284. * batadv_gw_node_add - add gateway node to list of available gateways
  285. * @bat_priv: the bat priv with all the soft interface information
  286. * @orig_node: originator announcing gateway capabilities
  287. * @gateway: announced bandwidth information
  288. */
  289. static void batadv_gw_node_add(struct batadv_priv *bat_priv,
  290. struct batadv_orig_node *orig_node,
  291. struct batadv_tvlv_gateway_data *gateway)
  292. {
  293. struct batadv_gw_node *gw_node;
  294. if (gateway->bandwidth_down == 0)
  295. return;
  296. gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
  297. if (!gw_node)
  298. return;
  299. INIT_HLIST_NODE(&gw_node->list);
  300. gw_node->orig_node = orig_node;
  301. atomic_set(&gw_node->refcount, 1);
  302. spin_lock_bh(&bat_priv->gw.list_lock);
  303. hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
  304. spin_unlock_bh(&bat_priv->gw.list_lock);
  305. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  306. "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
  307. orig_node->orig,
  308. ntohl(gateway->bandwidth_down) / 10,
  309. ntohl(gateway->bandwidth_down) % 10,
  310. ntohl(gateway->bandwidth_up) / 10,
  311. ntohl(gateway->bandwidth_up) % 10);
  312. }
  313. /**
  314. * batadv_gw_node_get - retrieve gateway node from list of available gateways
  315. * @bat_priv: the bat priv with all the soft interface information
  316. * @orig_node: originator announcing gateway capabilities
  317. *
  318. * Returns gateway node if found or NULL otherwise.
  319. */
  320. static struct batadv_gw_node *
  321. batadv_gw_node_get(struct batadv_priv *bat_priv,
  322. struct batadv_orig_node *orig_node)
  323. {
  324. struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
  325. rcu_read_lock();
  326. hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
  327. if (gw_node_tmp->orig_node != orig_node)
  328. continue;
  329. if (gw_node_tmp->deleted)
  330. continue;
  331. if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
  332. continue;
  333. gw_node = gw_node_tmp;
  334. break;
  335. }
  336. rcu_read_unlock();
  337. return gw_node;
  338. }
  339. /**
  340. * batadv_gw_node_update - update list of available gateways with changed
  341. * bandwidth information
  342. * @bat_priv: the bat priv with all the soft interface information
  343. * @orig_node: originator announcing gateway capabilities
  344. * @gateway: announced bandwidth information
  345. */
  346. void batadv_gw_node_update(struct batadv_priv *bat_priv,
  347. struct batadv_orig_node *orig_node,
  348. struct batadv_tvlv_gateway_data *gateway)
  349. {
  350. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  351. gw_node = batadv_gw_node_get(bat_priv, orig_node);
  352. if (!gw_node) {
  353. batadv_gw_node_add(bat_priv, orig_node, gateway);
  354. goto out;
  355. }
  356. if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
  357. (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
  358. goto out;
  359. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  360. "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
  361. orig_node->orig,
  362. gw_node->bandwidth_down / 10,
  363. gw_node->bandwidth_down % 10,
  364. gw_node->bandwidth_up / 10,
  365. gw_node->bandwidth_up % 10,
  366. ntohl(gateway->bandwidth_down) / 10,
  367. ntohl(gateway->bandwidth_down) % 10,
  368. ntohl(gateway->bandwidth_up) / 10,
  369. ntohl(gateway->bandwidth_up) % 10);
  370. gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
  371. gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
  372. gw_node->deleted = 0;
  373. if (ntohl(gateway->bandwidth_down) == 0) {
  374. gw_node->deleted = jiffies;
  375. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  376. "Gateway %pM removed from gateway list\n",
  377. orig_node->orig);
  378. /* Note: We don't need a NULL check here, since curr_gw never
  379. * gets dereferenced.
  380. */
  381. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  382. if (gw_node == curr_gw)
  383. batadv_gw_deselect(bat_priv);
  384. }
  385. out:
  386. if (curr_gw)
  387. batadv_gw_node_free_ref(curr_gw);
  388. if (gw_node)
  389. batadv_gw_node_free_ref(gw_node);
  390. }
  391. void batadv_gw_node_delete(struct batadv_priv *bat_priv,
  392. struct batadv_orig_node *orig_node)
  393. {
  394. struct batadv_tvlv_gateway_data gateway;
  395. gateway.bandwidth_down = 0;
  396. gateway.bandwidth_up = 0;
  397. batadv_gw_node_update(bat_priv, orig_node, &gateway);
  398. }
  399. void batadv_gw_node_purge(struct batadv_priv *bat_priv)
  400. {
  401. struct batadv_gw_node *gw_node, *curr_gw;
  402. struct hlist_node *node_tmp;
  403. unsigned long timeout = msecs_to_jiffies(2 * BATADV_PURGE_TIMEOUT);
  404. int do_deselect = 0;
  405. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  406. spin_lock_bh(&bat_priv->gw.list_lock);
  407. hlist_for_each_entry_safe(gw_node, node_tmp,
  408. &bat_priv->gw.list, list) {
  409. if (((!gw_node->deleted) ||
  410. (time_before(jiffies, gw_node->deleted + timeout))) &&
  411. atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE)
  412. continue;
  413. if (curr_gw == gw_node)
  414. do_deselect = 1;
  415. hlist_del_rcu(&gw_node->list);
  416. batadv_gw_node_free_ref(gw_node);
  417. }
  418. spin_unlock_bh(&bat_priv->gw.list_lock);
  419. /* gw_deselect() needs to acquire the gw_list_lock */
  420. if (do_deselect)
  421. batadv_gw_deselect(bat_priv);
  422. if (curr_gw)
  423. batadv_gw_node_free_ref(curr_gw);
  424. }
  425. /* fails if orig_node has no router */
  426. static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
  427. struct seq_file *seq,
  428. const struct batadv_gw_node *gw_node)
  429. {
  430. struct batadv_gw_node *curr_gw;
  431. struct batadv_neigh_node *router;
  432. int ret = -1;
  433. router = batadv_orig_node_get_router(gw_node->orig_node);
  434. if (!router)
  435. goto out;
  436. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  437. ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
  438. (curr_gw == gw_node ? "=>" : " "),
  439. gw_node->orig_node->orig,
  440. router->tq_avg, router->addr,
  441. router->if_incoming->net_dev->name,
  442. gw_node->bandwidth_down / 10,
  443. gw_node->bandwidth_down % 10,
  444. gw_node->bandwidth_up / 10,
  445. gw_node->bandwidth_up % 10);
  446. batadv_neigh_node_free_ref(router);
  447. if (curr_gw)
  448. batadv_gw_node_free_ref(curr_gw);
  449. out:
  450. return ret;
  451. }
  452. int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
  453. {
  454. struct net_device *net_dev = (struct net_device *)seq->private;
  455. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  456. struct batadv_hard_iface *primary_if;
  457. struct batadv_gw_node *gw_node;
  458. int gw_count = 0;
  459. primary_if = batadv_seq_print_text_primary_if_get(seq);
  460. if (!primary_if)
  461. goto out;
  462. seq_printf(seq,
  463. " %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
  464. "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
  465. BATADV_SOURCE_VERSION, primary_if->net_dev->name,
  466. primary_if->net_dev->dev_addr, net_dev->name);
  467. rcu_read_lock();
  468. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
  469. if (gw_node->deleted)
  470. continue;
  471. /* fails if orig_node has no router */
  472. if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
  473. continue;
  474. gw_count++;
  475. }
  476. rcu_read_unlock();
  477. if (gw_count == 0)
  478. seq_puts(seq, "No gateways in range ...\n");
  479. out:
  480. if (primary_if)
  481. batadv_hardif_free_ref(primary_if);
  482. return 0;
  483. }
  484. /* this call might reallocate skb data */
  485. static bool batadv_is_type_dhcprequest(struct sk_buff *skb, int header_len)
  486. {
  487. int ret = false;
  488. unsigned char *p;
  489. int pkt_len;
  490. if (skb_linearize(skb) < 0)
  491. goto out;
  492. pkt_len = skb_headlen(skb);
  493. if (pkt_len < header_len + BATADV_DHCP_OPTIONS_OFFSET + 1)
  494. goto out;
  495. p = skb->data + header_len + BATADV_DHCP_OPTIONS_OFFSET;
  496. pkt_len -= header_len + BATADV_DHCP_OPTIONS_OFFSET + 1;
  497. /* Access the dhcp option lists. Each entry is made up by:
  498. * - octet 1: option type
  499. * - octet 2: option data len (only if type != 255 and 0)
  500. * - octet 3: option data
  501. */
  502. while (*p != 255 && !ret) {
  503. /* p now points to the first octet: option type */
  504. if (*p == 53) {
  505. /* type 53 is the message type option.
  506. * Jump the len octet and go to the data octet
  507. */
  508. if (pkt_len < 2)
  509. goto out;
  510. p += 2;
  511. /* check if the message type is what we need */
  512. if (*p == BATADV_DHCP_REQUEST)
  513. ret = true;
  514. break;
  515. } else if (*p == 0) {
  516. /* option type 0 (padding), just go forward */
  517. if (pkt_len < 1)
  518. goto out;
  519. pkt_len--;
  520. p++;
  521. } else {
  522. /* This is any other option. So we get the length... */
  523. if (pkt_len < 1)
  524. goto out;
  525. pkt_len--;
  526. p++;
  527. /* ...and then we jump over the data */
  528. if (pkt_len < 1 + (*p))
  529. goto out;
  530. pkt_len -= 1 + (*p);
  531. p += 1 + (*p);
  532. }
  533. }
  534. out:
  535. return ret;
  536. }
  537. /* this call might reallocate skb data */
  538. bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
  539. {
  540. struct ethhdr *ethhdr;
  541. struct iphdr *iphdr;
  542. struct ipv6hdr *ipv6hdr;
  543. struct udphdr *udphdr;
  544. struct vlan_ethhdr *vhdr;
  545. __be16 proto;
  546. /* check for ethernet header */
  547. if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
  548. return false;
  549. ethhdr = (struct ethhdr *)skb->data;
  550. proto = ethhdr->h_proto;
  551. *header_len += ETH_HLEN;
  552. /* check for initial vlan header */
  553. if (proto == htons(ETH_P_8021Q)) {
  554. if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
  555. return false;
  556. vhdr = (struct vlan_ethhdr *)skb->data;
  557. proto = vhdr->h_vlan_encapsulated_proto;
  558. *header_len += VLAN_HLEN;
  559. }
  560. /* check for ip header */
  561. switch (proto) {
  562. case htons(ETH_P_IP):
  563. if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
  564. return false;
  565. iphdr = (struct iphdr *)(skb->data + *header_len);
  566. *header_len += iphdr->ihl * 4;
  567. /* check for udp header */
  568. if (iphdr->protocol != IPPROTO_UDP)
  569. return false;
  570. break;
  571. case htons(ETH_P_IPV6):
  572. if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
  573. return false;
  574. ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
  575. *header_len += sizeof(*ipv6hdr);
  576. /* check for udp header */
  577. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  578. return false;
  579. break;
  580. default:
  581. return false;
  582. }
  583. if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
  584. return false;
  585. /* skb->data might have been reallocated by pskb_may_pull() */
  586. ethhdr = (struct ethhdr *)skb->data;
  587. if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
  588. ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
  589. udphdr = (struct udphdr *)(skb->data + *header_len);
  590. *header_len += sizeof(*udphdr);
  591. /* check for bootp port */
  592. if ((proto == htons(ETH_P_IP)) &&
  593. (udphdr->dest != htons(67)))
  594. return false;
  595. if ((proto == htons(ETH_P_IPV6)) &&
  596. (udphdr->dest != htons(547)))
  597. return false;
  598. return true;
  599. }
  600. /* this call might reallocate skb data */
  601. bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
  602. struct sk_buff *skb)
  603. {
  604. struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
  605. struct batadv_orig_node *orig_dst_node = NULL;
  606. struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
  607. struct ethhdr *ethhdr;
  608. bool ret, out_of_range = false;
  609. unsigned int header_len = 0;
  610. uint8_t curr_tq_avg;
  611. ret = batadv_gw_is_dhcp_target(skb, &header_len);
  612. if (!ret)
  613. goto out;
  614. ethhdr = (struct ethhdr *)skb->data;
  615. orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
  616. ethhdr->h_dest);
  617. if (!orig_dst_node)
  618. goto out;
  619. gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
  620. if (!gw_node->bandwidth_down == 0)
  621. goto out;
  622. ret = batadv_is_type_dhcprequest(skb, header_len);
  623. if (!ret)
  624. goto out;
  625. switch (atomic_read(&bat_priv->gw_mode)) {
  626. case BATADV_GW_MODE_SERVER:
  627. /* If we are a GW then we are our best GW. We can artificially
  628. * set the tq towards ourself as the maximum value
  629. */
  630. curr_tq_avg = BATADV_TQ_MAX_VALUE;
  631. break;
  632. case BATADV_GW_MODE_CLIENT:
  633. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  634. if (!curr_gw)
  635. goto out;
  636. /* packet is going to our gateway */
  637. if (curr_gw->orig_node == orig_dst_node)
  638. goto out;
  639. /* If the dhcp packet has been sent to a different gw,
  640. * we have to evaluate whether the old gw is still
  641. * reliable enough
  642. */
  643. neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
  644. NULL);
  645. if (!neigh_curr)
  646. goto out;
  647. curr_tq_avg = neigh_curr->tq_avg;
  648. break;
  649. case BATADV_GW_MODE_OFF:
  650. default:
  651. goto out;
  652. }
  653. neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
  654. if (!neigh_old)
  655. goto out;
  656. if (curr_tq_avg - neigh_old->tq_avg > BATADV_GW_THRESHOLD)
  657. out_of_range = true;
  658. out:
  659. if (orig_dst_node)
  660. batadv_orig_node_free_ref(orig_dst_node);
  661. if (curr_gw)
  662. batadv_gw_node_free_ref(curr_gw);
  663. if (gw_node)
  664. batadv_gw_node_free_ref(gw_node);
  665. if (neigh_old)
  666. batadv_neigh_node_free_ref(neigh_old);
  667. if (neigh_curr)
  668. batadv_neigh_node_free_ref(neigh_curr);
  669. return out_of_range;
  670. }