gateway_client.c 21 KB

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