gateway_client.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2009-2018 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "gateway_client.h"
  19. #include "main.h"
  20. #include <linux/atomic.h>
  21. #include <linux/byteorder/generic.h>
  22. #include <linux/errno.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/gfp.h>
  25. #include <linux/if_ether.h>
  26. #include <linux/if_vlan.h>
  27. #include <linux/in.h>
  28. #include <linux/ip.h>
  29. #include <linux/ipv6.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kref.h>
  32. #include <linux/list.h>
  33. #include <linux/lockdep.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/netlink.h>
  36. #include <linux/rculist.h>
  37. #include <linux/rcupdate.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/stddef.h>
  43. #include <linux/udp.h>
  44. #include <net/sock.h>
  45. #include <uapi/linux/batadv_packet.h>
  46. #include <uapi/linux/batman_adv.h>
  47. #include "gateway_common.h"
  48. #include "hard-interface.h"
  49. #include "log.h"
  50. #include "netlink.h"
  51. #include "originator.h"
  52. #include "routing.h"
  53. #include "soft-interface.h"
  54. #include "sysfs.h"
  55. #include "translation-table.h"
  56. /* These are the offsets of the "hw type" and "hw address length" in the dhcp
  57. * packet starting at the beginning of the dhcp header
  58. */
  59. #define BATADV_DHCP_HTYPE_OFFSET 1
  60. #define BATADV_DHCP_HLEN_OFFSET 2
  61. /* Value of htype representing Ethernet */
  62. #define BATADV_DHCP_HTYPE_ETHERNET 0x01
  63. /* This is the offset of the "chaddr" field in the dhcp packet starting at the
  64. * beginning of the dhcp header
  65. */
  66. #define BATADV_DHCP_CHADDR_OFFSET 28
  67. /**
  68. * batadv_gw_node_release() - release gw_node from lists and queue for free
  69. * after rcu grace period
  70. * @ref: kref pointer of the gw_node
  71. */
  72. static void batadv_gw_node_release(struct kref *ref)
  73. {
  74. struct batadv_gw_node *gw_node;
  75. gw_node = container_of(ref, struct batadv_gw_node, refcount);
  76. batadv_orig_node_put(gw_node->orig_node);
  77. kfree_rcu(gw_node, rcu);
  78. }
  79. /**
  80. * batadv_gw_node_put() - decrement the gw_node refcounter and possibly release
  81. * it
  82. * @gw_node: gateway node to free
  83. */
  84. void batadv_gw_node_put(struct batadv_gw_node *gw_node)
  85. {
  86. kref_put(&gw_node->refcount, batadv_gw_node_release);
  87. }
  88. /**
  89. * batadv_gw_get_selected_gw_node() - Get currently selected gateway
  90. * @bat_priv: the bat priv with all the soft interface information
  91. *
  92. * Return: selected gateway (with increased refcnt), NULL on errors
  93. */
  94. struct batadv_gw_node *
  95. batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
  96. {
  97. struct batadv_gw_node *gw_node;
  98. rcu_read_lock();
  99. gw_node = rcu_dereference(bat_priv->gw.curr_gw);
  100. if (!gw_node)
  101. goto out;
  102. if (!kref_get_unless_zero(&gw_node->refcount))
  103. gw_node = NULL;
  104. out:
  105. rcu_read_unlock();
  106. return gw_node;
  107. }
  108. /**
  109. * batadv_gw_get_selected_orig() - Get originator of currently selected gateway
  110. * @bat_priv: the bat priv with all the soft interface information
  111. *
  112. * Return: orig_node of selected gateway (with increased refcnt), NULL on errors
  113. */
  114. struct batadv_orig_node *
  115. batadv_gw_get_selected_orig(struct batadv_priv *bat_priv)
  116. {
  117. struct batadv_gw_node *gw_node;
  118. struct batadv_orig_node *orig_node = NULL;
  119. gw_node = batadv_gw_get_selected_gw_node(bat_priv);
  120. if (!gw_node)
  121. goto out;
  122. rcu_read_lock();
  123. orig_node = gw_node->orig_node;
  124. if (!orig_node)
  125. goto unlock;
  126. if (!kref_get_unless_zero(&orig_node->refcount))
  127. orig_node = NULL;
  128. unlock:
  129. rcu_read_unlock();
  130. out:
  131. if (gw_node)
  132. batadv_gw_node_put(gw_node);
  133. return orig_node;
  134. }
  135. static void batadv_gw_select(struct batadv_priv *bat_priv,
  136. struct batadv_gw_node *new_gw_node)
  137. {
  138. struct batadv_gw_node *curr_gw_node;
  139. spin_lock_bh(&bat_priv->gw.list_lock);
  140. if (new_gw_node)
  141. kref_get(&new_gw_node->refcount);
  142. curr_gw_node = rcu_dereference_protected(bat_priv->gw.curr_gw, 1);
  143. rcu_assign_pointer(bat_priv->gw.curr_gw, new_gw_node);
  144. if (curr_gw_node)
  145. batadv_gw_node_put(curr_gw_node);
  146. spin_unlock_bh(&bat_priv->gw.list_lock);
  147. }
  148. /**
  149. * batadv_gw_reselect() - force a gateway reselection
  150. * @bat_priv: the bat priv with all the soft interface information
  151. *
  152. * Set a flag to remind the GW component to perform a new gateway reselection.
  153. * However this function does not ensure that the current gateway is going to be
  154. * deselected. The reselection mechanism may elect the same gateway once again.
  155. *
  156. * This means that invoking batadv_gw_reselect() does not guarantee a gateway
  157. * change and therefore a uevent is not necessarily expected.
  158. */
  159. void batadv_gw_reselect(struct batadv_priv *bat_priv)
  160. {
  161. atomic_set(&bat_priv->gw.reselect, 1);
  162. }
  163. /**
  164. * batadv_gw_check_client_stop() - check if client mode has been switched off
  165. * @bat_priv: the bat priv with all the soft interface information
  166. *
  167. * This function assumes the caller has checked that the gw state *is actually
  168. * changing*. This function is not supposed to be called when there is no state
  169. * change.
  170. */
  171. void batadv_gw_check_client_stop(struct batadv_priv *bat_priv)
  172. {
  173. struct batadv_gw_node *curr_gw;
  174. if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
  175. return;
  176. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  177. if (!curr_gw)
  178. return;
  179. /* deselect the current gateway so that next time that client mode is
  180. * enabled a proper GW_ADD event can be sent
  181. */
  182. batadv_gw_select(bat_priv, NULL);
  183. /* if batman-adv is switching the gw client mode off and a gateway was
  184. * already selected, send a DEL uevent
  185. */
  186. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL, NULL);
  187. batadv_gw_node_put(curr_gw);
  188. }
  189. /**
  190. * batadv_gw_election() - Elect the best gateway
  191. * @bat_priv: the bat priv with all the soft interface information
  192. */
  193. void batadv_gw_election(struct batadv_priv *bat_priv)
  194. {
  195. struct batadv_gw_node *curr_gw = NULL;
  196. struct batadv_gw_node *next_gw = NULL;
  197. struct batadv_neigh_node *router = NULL;
  198. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  199. char gw_addr[18] = { '\0' };
  200. if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
  201. goto out;
  202. if (!bat_priv->algo_ops->gw.get_best_gw_node)
  203. goto out;
  204. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  205. if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
  206. goto out;
  207. /* if gw.reselect is set to 1 it means that a previous call to
  208. * gw.is_eligible() said that we have a new best GW, therefore it can
  209. * now be picked from the list and selected
  210. */
  211. next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
  212. if (curr_gw == next_gw)
  213. goto out;
  214. if (next_gw) {
  215. sprintf(gw_addr, "%pM", next_gw->orig_node->orig);
  216. router = batadv_orig_router_get(next_gw->orig_node,
  217. BATADV_IF_DEFAULT);
  218. if (!router) {
  219. batadv_gw_reselect(bat_priv);
  220. goto out;
  221. }
  222. router_ifinfo = batadv_neigh_ifinfo_get(router,
  223. BATADV_IF_DEFAULT);
  224. if (!router_ifinfo) {
  225. batadv_gw_reselect(bat_priv);
  226. goto out;
  227. }
  228. }
  229. if (curr_gw && !next_gw) {
  230. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  231. "Removing selected gateway - no gateway in range\n");
  232. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_DEL,
  233. NULL);
  234. } else if (!curr_gw && next_gw) {
  235. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  236. "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
  237. next_gw->orig_node->orig,
  238. next_gw->bandwidth_down / 10,
  239. next_gw->bandwidth_down % 10,
  240. next_gw->bandwidth_up / 10,
  241. next_gw->bandwidth_up % 10,
  242. router_ifinfo->bat_iv.tq_avg);
  243. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
  244. gw_addr);
  245. } else {
  246. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  247. "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
  248. next_gw->orig_node->orig,
  249. next_gw->bandwidth_down / 10,
  250. next_gw->bandwidth_down % 10,
  251. next_gw->bandwidth_up / 10,
  252. next_gw->bandwidth_up % 10,
  253. router_ifinfo->bat_iv.tq_avg);
  254. batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
  255. gw_addr);
  256. }
  257. batadv_gw_select(bat_priv, next_gw);
  258. out:
  259. if (curr_gw)
  260. batadv_gw_node_put(curr_gw);
  261. if (next_gw)
  262. batadv_gw_node_put(next_gw);
  263. if (router)
  264. batadv_neigh_node_put(router);
  265. if (router_ifinfo)
  266. batadv_neigh_ifinfo_put(router_ifinfo);
  267. }
  268. /**
  269. * batadv_gw_check_election() - Elect orig node as best gateway when eligible
  270. * @bat_priv: the bat priv with all the soft interface information
  271. * @orig_node: orig node which is to be checked
  272. */
  273. void batadv_gw_check_election(struct batadv_priv *bat_priv,
  274. struct batadv_orig_node *orig_node)
  275. {
  276. struct batadv_orig_node *curr_gw_orig;
  277. /* abort immediately if the routing algorithm does not support gateway
  278. * election
  279. */
  280. if (!bat_priv->algo_ops->gw.is_eligible)
  281. return;
  282. curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
  283. if (!curr_gw_orig)
  284. goto reselect;
  285. /* this node already is the gateway */
  286. if (curr_gw_orig == orig_node)
  287. goto out;
  288. if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
  289. orig_node))
  290. goto out;
  291. reselect:
  292. batadv_gw_reselect(bat_priv);
  293. out:
  294. if (curr_gw_orig)
  295. batadv_orig_node_put(curr_gw_orig);
  296. }
  297. /**
  298. * batadv_gw_node_add() - add gateway node to list of available gateways
  299. * @bat_priv: the bat priv with all the soft interface information
  300. * @orig_node: originator announcing gateway capabilities
  301. * @gateway: announced bandwidth information
  302. *
  303. * Has to be called with the appropriate locks being acquired
  304. * (gw.list_lock).
  305. */
  306. static void batadv_gw_node_add(struct batadv_priv *bat_priv,
  307. struct batadv_orig_node *orig_node,
  308. struct batadv_tvlv_gateway_data *gateway)
  309. {
  310. struct batadv_gw_node *gw_node;
  311. lockdep_assert_held(&bat_priv->gw.list_lock);
  312. if (gateway->bandwidth_down == 0)
  313. return;
  314. gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
  315. if (!gw_node)
  316. return;
  317. kref_init(&gw_node->refcount);
  318. INIT_HLIST_NODE(&gw_node->list);
  319. kref_get(&orig_node->refcount);
  320. gw_node->orig_node = orig_node;
  321. gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
  322. gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
  323. kref_get(&gw_node->refcount);
  324. hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.gateway_list);
  325. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  326. "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
  327. orig_node->orig,
  328. ntohl(gateway->bandwidth_down) / 10,
  329. ntohl(gateway->bandwidth_down) % 10,
  330. ntohl(gateway->bandwidth_up) / 10,
  331. ntohl(gateway->bandwidth_up) % 10);
  332. /* don't return reference to new gw_node */
  333. batadv_gw_node_put(gw_node);
  334. }
  335. /**
  336. * batadv_gw_node_get() - retrieve gateway node from list of available gateways
  337. * @bat_priv: the bat priv with all the soft interface information
  338. * @orig_node: originator announcing gateway capabilities
  339. *
  340. * Return: gateway node if found or NULL otherwise.
  341. */
  342. struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
  343. struct batadv_orig_node *orig_node)
  344. {
  345. struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
  346. rcu_read_lock();
  347. hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.gateway_list,
  348. list) {
  349. if (gw_node_tmp->orig_node != orig_node)
  350. continue;
  351. if (!kref_get_unless_zero(&gw_node_tmp->refcount))
  352. continue;
  353. gw_node = gw_node_tmp;
  354. break;
  355. }
  356. rcu_read_unlock();
  357. return gw_node;
  358. }
  359. /**
  360. * batadv_gw_node_update() - update list of available gateways with changed
  361. * bandwidth information
  362. * @bat_priv: the bat priv with all the soft interface information
  363. * @orig_node: originator announcing gateway capabilities
  364. * @gateway: announced bandwidth information
  365. */
  366. void batadv_gw_node_update(struct batadv_priv *bat_priv,
  367. struct batadv_orig_node *orig_node,
  368. struct batadv_tvlv_gateway_data *gateway)
  369. {
  370. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  371. spin_lock_bh(&bat_priv->gw.list_lock);
  372. gw_node = batadv_gw_node_get(bat_priv, orig_node);
  373. if (!gw_node) {
  374. batadv_gw_node_add(bat_priv, orig_node, gateway);
  375. spin_unlock_bh(&bat_priv->gw.list_lock);
  376. goto out;
  377. }
  378. spin_unlock_bh(&bat_priv->gw.list_lock);
  379. if (gw_node->bandwidth_down == ntohl(gateway->bandwidth_down) &&
  380. gw_node->bandwidth_up == ntohl(gateway->bandwidth_up))
  381. goto out;
  382. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  383. "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
  384. orig_node->orig,
  385. gw_node->bandwidth_down / 10,
  386. gw_node->bandwidth_down % 10,
  387. gw_node->bandwidth_up / 10,
  388. gw_node->bandwidth_up % 10,
  389. ntohl(gateway->bandwidth_down) / 10,
  390. ntohl(gateway->bandwidth_down) % 10,
  391. ntohl(gateway->bandwidth_up) / 10,
  392. ntohl(gateway->bandwidth_up) % 10);
  393. gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
  394. gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
  395. if (ntohl(gateway->bandwidth_down) == 0) {
  396. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  397. "Gateway %pM removed from gateway list\n",
  398. orig_node->orig);
  399. /* Note: We don't need a NULL check here, since curr_gw never
  400. * gets dereferenced.
  401. */
  402. spin_lock_bh(&bat_priv->gw.list_lock);
  403. if (!hlist_unhashed(&gw_node->list)) {
  404. hlist_del_init_rcu(&gw_node->list);
  405. batadv_gw_node_put(gw_node);
  406. }
  407. spin_unlock_bh(&bat_priv->gw.list_lock);
  408. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  409. if (gw_node == curr_gw)
  410. batadv_gw_reselect(bat_priv);
  411. if (curr_gw)
  412. batadv_gw_node_put(curr_gw);
  413. }
  414. out:
  415. if (gw_node)
  416. batadv_gw_node_put(gw_node);
  417. }
  418. /**
  419. * batadv_gw_node_delete() - Remove orig_node from gateway list
  420. * @bat_priv: the bat priv with all the soft interface information
  421. * @orig_node: orig node which is currently in process of being removed
  422. */
  423. void batadv_gw_node_delete(struct batadv_priv *bat_priv,
  424. struct batadv_orig_node *orig_node)
  425. {
  426. struct batadv_tvlv_gateway_data gateway;
  427. gateway.bandwidth_down = 0;
  428. gateway.bandwidth_up = 0;
  429. batadv_gw_node_update(bat_priv, orig_node, &gateway);
  430. }
  431. /**
  432. * batadv_gw_node_free() - Free gateway information from soft interface
  433. * @bat_priv: the bat priv with all the soft interface information
  434. */
  435. void batadv_gw_node_free(struct batadv_priv *bat_priv)
  436. {
  437. struct batadv_gw_node *gw_node;
  438. struct hlist_node *node_tmp;
  439. spin_lock_bh(&bat_priv->gw.list_lock);
  440. hlist_for_each_entry_safe(gw_node, node_tmp,
  441. &bat_priv->gw.gateway_list, list) {
  442. hlist_del_init_rcu(&gw_node->list);
  443. batadv_gw_node_put(gw_node);
  444. }
  445. spin_unlock_bh(&bat_priv->gw.list_lock);
  446. }
  447. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  448. /**
  449. * batadv_gw_client_seq_print_text() - Print the gateway table in a seq file
  450. * @seq: seq file to print on
  451. * @offset: not used
  452. *
  453. * Return: always 0
  454. */
  455. int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
  456. {
  457. struct net_device *net_dev = (struct net_device *)seq->private;
  458. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  459. struct batadv_hard_iface *primary_if;
  460. primary_if = batadv_seq_print_text_primary_if_get(seq);
  461. if (!primary_if)
  462. return 0;
  463. seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
  464. BATADV_SOURCE_VERSION, primary_if->net_dev->name,
  465. primary_if->net_dev->dev_addr, net_dev->name,
  466. bat_priv->algo_ops->name);
  467. batadv_hardif_put(primary_if);
  468. if (!bat_priv->algo_ops->gw.print) {
  469. seq_puts(seq,
  470. "No printing function for this routing protocol\n");
  471. return 0;
  472. }
  473. bat_priv->algo_ops->gw.print(bat_priv, seq);
  474. return 0;
  475. }
  476. #endif
  477. /**
  478. * batadv_gw_dump() - Dump gateways into a message
  479. * @msg: Netlink message to dump into
  480. * @cb: Control block containing additional options
  481. *
  482. * Return: Error code, or length of message
  483. */
  484. int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
  485. {
  486. struct batadv_hard_iface *primary_if = NULL;
  487. struct net *net = sock_net(cb->skb->sk);
  488. struct net_device *soft_iface;
  489. struct batadv_priv *bat_priv;
  490. int ifindex;
  491. int ret;
  492. ifindex = batadv_netlink_get_ifindex(cb->nlh,
  493. BATADV_ATTR_MESH_IFINDEX);
  494. if (!ifindex)
  495. return -EINVAL;
  496. soft_iface = dev_get_by_index(net, ifindex);
  497. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  498. ret = -ENODEV;
  499. goto out;
  500. }
  501. bat_priv = netdev_priv(soft_iface);
  502. primary_if = batadv_primary_if_get_selected(bat_priv);
  503. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  504. ret = -ENOENT;
  505. goto out;
  506. }
  507. if (!bat_priv->algo_ops->gw.dump) {
  508. ret = -EOPNOTSUPP;
  509. goto out;
  510. }
  511. bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
  512. ret = msg->len;
  513. out:
  514. if (primary_if)
  515. batadv_hardif_put(primary_if);
  516. if (soft_iface)
  517. dev_put(soft_iface);
  518. return ret;
  519. }
  520. /**
  521. * batadv_gw_dhcp_recipient_get() - check if a packet is a DHCP message
  522. * @skb: the packet to check
  523. * @header_len: a pointer to the batman-adv header size
  524. * @chaddr: buffer where the client address will be stored. Valid
  525. * only if the function returns BATADV_DHCP_TO_CLIENT
  526. *
  527. * This function may re-allocate the data buffer of the skb passed as argument.
  528. *
  529. * Return:
  530. * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
  531. * while parsing it
  532. * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
  533. * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
  534. */
  535. enum batadv_dhcp_recipient
  536. batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
  537. u8 *chaddr)
  538. {
  539. enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
  540. struct ethhdr *ethhdr;
  541. struct iphdr *iphdr;
  542. struct ipv6hdr *ipv6hdr;
  543. struct udphdr *udphdr;
  544. struct vlan_ethhdr *vhdr;
  545. int chaddr_offset;
  546. __be16 proto;
  547. u8 *p;
  548. /* check for ethernet header */
  549. if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
  550. return BATADV_DHCP_NO;
  551. ethhdr = eth_hdr(skb);
  552. proto = ethhdr->h_proto;
  553. *header_len += ETH_HLEN;
  554. /* check for initial vlan header */
  555. if (proto == htons(ETH_P_8021Q)) {
  556. if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
  557. return BATADV_DHCP_NO;
  558. vhdr = vlan_eth_hdr(skb);
  559. proto = vhdr->h_vlan_encapsulated_proto;
  560. *header_len += VLAN_HLEN;
  561. }
  562. /* check for ip header */
  563. switch (proto) {
  564. case htons(ETH_P_IP):
  565. if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
  566. return BATADV_DHCP_NO;
  567. iphdr = (struct iphdr *)(skb->data + *header_len);
  568. *header_len += iphdr->ihl * 4;
  569. /* check for udp header */
  570. if (iphdr->protocol != IPPROTO_UDP)
  571. return BATADV_DHCP_NO;
  572. break;
  573. case htons(ETH_P_IPV6):
  574. if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
  575. return BATADV_DHCP_NO;
  576. ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
  577. *header_len += sizeof(*ipv6hdr);
  578. /* check for udp header */
  579. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  580. return BATADV_DHCP_NO;
  581. break;
  582. default:
  583. return BATADV_DHCP_NO;
  584. }
  585. if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
  586. return BATADV_DHCP_NO;
  587. udphdr = (struct udphdr *)(skb->data + *header_len);
  588. *header_len += sizeof(*udphdr);
  589. /* check for bootp port */
  590. switch (proto) {
  591. case htons(ETH_P_IP):
  592. if (udphdr->dest == htons(67))
  593. ret = BATADV_DHCP_TO_SERVER;
  594. else if (udphdr->source == htons(67))
  595. ret = BATADV_DHCP_TO_CLIENT;
  596. break;
  597. case htons(ETH_P_IPV6):
  598. if (udphdr->dest == htons(547))
  599. ret = BATADV_DHCP_TO_SERVER;
  600. else if (udphdr->source == htons(547))
  601. ret = BATADV_DHCP_TO_CLIENT;
  602. break;
  603. }
  604. chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
  605. /* store the client address if the message is going to a client */
  606. if (ret == BATADV_DHCP_TO_CLIENT &&
  607. pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
  608. /* check if the DHCP packet carries an Ethernet DHCP */
  609. p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
  610. if (*p != BATADV_DHCP_HTYPE_ETHERNET)
  611. return BATADV_DHCP_NO;
  612. /* check if the DHCP packet carries a valid Ethernet address */
  613. p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
  614. if (*p != ETH_ALEN)
  615. return BATADV_DHCP_NO;
  616. ether_addr_copy(chaddr, skb->data + chaddr_offset);
  617. }
  618. return ret;
  619. }
  620. /**
  621. * batadv_gw_out_of_range() - check if the dhcp request destination is the best
  622. * gateway
  623. * @bat_priv: the bat priv with all the soft interface information
  624. * @skb: the outgoing packet
  625. *
  626. * Check if the skb is a DHCP request and if it is sent to the current best GW
  627. * server. Due to topology changes it may be the case that the GW server
  628. * previously selected is not the best one anymore.
  629. *
  630. * This call might reallocate skb data.
  631. * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
  632. *
  633. * Return: true if the packet destination is unicast and it is not the best gw,
  634. * false otherwise.
  635. */
  636. bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
  637. struct sk_buff *skb)
  638. {
  639. struct batadv_neigh_node *neigh_curr = NULL;
  640. struct batadv_neigh_node *neigh_old = NULL;
  641. struct batadv_orig_node *orig_dst_node = NULL;
  642. struct batadv_gw_node *gw_node = NULL;
  643. struct batadv_gw_node *curr_gw = NULL;
  644. struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
  645. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  646. bool out_of_range = false;
  647. u8 curr_tq_avg;
  648. unsigned short vid;
  649. vid = batadv_get_vid(skb, 0);
  650. if (is_multicast_ether_addr(ethhdr->h_dest))
  651. goto out;
  652. orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
  653. ethhdr->h_dest, vid);
  654. if (!orig_dst_node)
  655. goto out;
  656. gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
  657. if (!gw_node)
  658. goto out;
  659. switch (atomic_read(&bat_priv->gw.mode)) {
  660. case BATADV_GW_MODE_SERVER:
  661. /* If we are a GW then we are our best GW. We can artificially
  662. * set the tq towards ourself as the maximum value
  663. */
  664. curr_tq_avg = BATADV_TQ_MAX_VALUE;
  665. break;
  666. case BATADV_GW_MODE_CLIENT:
  667. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  668. if (!curr_gw)
  669. goto out;
  670. /* packet is going to our gateway */
  671. if (curr_gw->orig_node == orig_dst_node)
  672. goto out;
  673. /* If the dhcp packet has been sent to a different gw,
  674. * we have to evaluate whether the old gw is still
  675. * reliable enough
  676. */
  677. neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
  678. NULL);
  679. if (!neigh_curr)
  680. goto out;
  681. curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
  682. BATADV_IF_DEFAULT);
  683. if (!curr_ifinfo)
  684. goto out;
  685. curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
  686. batadv_neigh_ifinfo_put(curr_ifinfo);
  687. break;
  688. case BATADV_GW_MODE_OFF:
  689. default:
  690. goto out;
  691. }
  692. neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
  693. if (!neigh_old)
  694. goto out;
  695. old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
  696. if (!old_ifinfo)
  697. goto out;
  698. if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
  699. out_of_range = true;
  700. batadv_neigh_ifinfo_put(old_ifinfo);
  701. out:
  702. if (orig_dst_node)
  703. batadv_orig_node_put(orig_dst_node);
  704. if (curr_gw)
  705. batadv_gw_node_put(curr_gw);
  706. if (gw_node)
  707. batadv_gw_node_put(gw_node);
  708. if (neigh_old)
  709. batadv_neigh_node_put(neigh_old);
  710. if (neigh_curr)
  711. batadv_neigh_node_put(neigh_curr);
  712. return out_of_range;
  713. }