gateway_client.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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.gateway_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.gateway_list,
  322. list) {
  323. if (gw_node_tmp->orig_node != orig_node)
  324. continue;
  325. if (!kref_get_unless_zero(&gw_node_tmp->refcount))
  326. continue;
  327. gw_node = gw_node_tmp;
  328. break;
  329. }
  330. rcu_read_unlock();
  331. return gw_node;
  332. }
  333. /**
  334. * batadv_gw_node_update - update list of available gateways with changed
  335. * bandwidth information
  336. * @bat_priv: the bat priv with all the soft interface information
  337. * @orig_node: originator announcing gateway capabilities
  338. * @gateway: announced bandwidth information
  339. */
  340. void batadv_gw_node_update(struct batadv_priv *bat_priv,
  341. struct batadv_orig_node *orig_node,
  342. struct batadv_tvlv_gateway_data *gateway)
  343. {
  344. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  345. gw_node = batadv_gw_node_get(bat_priv, orig_node);
  346. if (!gw_node) {
  347. batadv_gw_node_add(bat_priv, orig_node, gateway);
  348. goto out;
  349. }
  350. if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
  351. (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
  352. goto out;
  353. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  354. "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
  355. orig_node->orig,
  356. gw_node->bandwidth_down / 10,
  357. gw_node->bandwidth_down % 10,
  358. gw_node->bandwidth_up / 10,
  359. gw_node->bandwidth_up % 10,
  360. ntohl(gateway->bandwidth_down) / 10,
  361. ntohl(gateway->bandwidth_down) % 10,
  362. ntohl(gateway->bandwidth_up) / 10,
  363. ntohl(gateway->bandwidth_up) % 10);
  364. gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
  365. gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
  366. if (ntohl(gateway->bandwidth_down) == 0) {
  367. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  368. "Gateway %pM removed from gateway list\n",
  369. orig_node->orig);
  370. /* Note: We don't need a NULL check here, since curr_gw never
  371. * gets dereferenced.
  372. */
  373. spin_lock_bh(&bat_priv->gw.list_lock);
  374. if (!hlist_unhashed(&gw_node->list)) {
  375. hlist_del_init_rcu(&gw_node->list);
  376. batadv_gw_node_put(gw_node);
  377. }
  378. spin_unlock_bh(&bat_priv->gw.list_lock);
  379. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  380. if (gw_node == curr_gw)
  381. batadv_gw_reselect(bat_priv);
  382. if (curr_gw)
  383. batadv_gw_node_put(curr_gw);
  384. }
  385. out:
  386. if (gw_node)
  387. batadv_gw_node_put(gw_node);
  388. }
  389. void batadv_gw_node_delete(struct batadv_priv *bat_priv,
  390. struct batadv_orig_node *orig_node)
  391. {
  392. struct batadv_tvlv_gateway_data gateway;
  393. gateway.bandwidth_down = 0;
  394. gateway.bandwidth_up = 0;
  395. batadv_gw_node_update(bat_priv, orig_node, &gateway);
  396. }
  397. void batadv_gw_node_free(struct batadv_priv *bat_priv)
  398. {
  399. struct batadv_gw_node *gw_node;
  400. struct hlist_node *node_tmp;
  401. spin_lock_bh(&bat_priv->gw.list_lock);
  402. hlist_for_each_entry_safe(gw_node, node_tmp,
  403. &bat_priv->gw.gateway_list, 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. }
  409. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  410. int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
  411. {
  412. struct net_device *net_dev = (struct net_device *)seq->private;
  413. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  414. struct batadv_hard_iface *primary_if;
  415. primary_if = batadv_seq_print_text_primary_if_get(seq);
  416. if (!primary_if)
  417. return 0;
  418. seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
  419. BATADV_SOURCE_VERSION, primary_if->net_dev->name,
  420. primary_if->net_dev->dev_addr, net_dev->name,
  421. bat_priv->algo_ops->name);
  422. batadv_hardif_put(primary_if);
  423. if (!bat_priv->algo_ops->gw.print) {
  424. seq_puts(seq,
  425. "No printing function for this routing protocol\n");
  426. return 0;
  427. }
  428. bat_priv->algo_ops->gw.print(bat_priv, seq);
  429. return 0;
  430. }
  431. #endif
  432. /**
  433. * batadv_gw_dump - Dump gateways into a message
  434. * @msg: Netlink message to dump into
  435. * @cb: Control block containing additional options
  436. *
  437. * Return: Error code, or length of message
  438. */
  439. int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
  440. {
  441. struct batadv_hard_iface *primary_if = NULL;
  442. struct net *net = sock_net(cb->skb->sk);
  443. struct net_device *soft_iface;
  444. struct batadv_priv *bat_priv;
  445. int ifindex;
  446. int ret;
  447. ifindex = batadv_netlink_get_ifindex(cb->nlh,
  448. BATADV_ATTR_MESH_IFINDEX);
  449. if (!ifindex)
  450. return -EINVAL;
  451. soft_iface = dev_get_by_index(net, ifindex);
  452. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  453. ret = -ENODEV;
  454. goto out;
  455. }
  456. bat_priv = netdev_priv(soft_iface);
  457. primary_if = batadv_primary_if_get_selected(bat_priv);
  458. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  459. ret = -ENOENT;
  460. goto out;
  461. }
  462. if (!bat_priv->algo_ops->gw.dump) {
  463. ret = -EOPNOTSUPP;
  464. goto out;
  465. }
  466. bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
  467. ret = msg->len;
  468. out:
  469. if (primary_if)
  470. batadv_hardif_put(primary_if);
  471. if (soft_iface)
  472. dev_put(soft_iface);
  473. return ret;
  474. }
  475. /**
  476. * batadv_gw_dhcp_recipient_get - check if a packet is a DHCP message
  477. * @skb: the packet to check
  478. * @header_len: a pointer to the batman-adv header size
  479. * @chaddr: buffer where the client address will be stored. Valid
  480. * only if the function returns BATADV_DHCP_TO_CLIENT
  481. *
  482. * This function may re-allocate the data buffer of the skb passed as argument.
  483. *
  484. * Return:
  485. * - BATADV_DHCP_NO if the packet is not a dhcp message or if there was an error
  486. * while parsing it
  487. * - BATADV_DHCP_TO_SERVER if this is a message going to the DHCP server
  488. * - BATADV_DHCP_TO_CLIENT if this is a message going to a DHCP client
  489. */
  490. enum batadv_dhcp_recipient
  491. batadv_gw_dhcp_recipient_get(struct sk_buff *skb, unsigned int *header_len,
  492. u8 *chaddr)
  493. {
  494. enum batadv_dhcp_recipient ret = BATADV_DHCP_NO;
  495. struct ethhdr *ethhdr;
  496. struct iphdr *iphdr;
  497. struct ipv6hdr *ipv6hdr;
  498. struct udphdr *udphdr;
  499. struct vlan_ethhdr *vhdr;
  500. int chaddr_offset;
  501. __be16 proto;
  502. u8 *p;
  503. /* check for ethernet header */
  504. if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
  505. return BATADV_DHCP_NO;
  506. ethhdr = eth_hdr(skb);
  507. proto = ethhdr->h_proto;
  508. *header_len += ETH_HLEN;
  509. /* check for initial vlan header */
  510. if (proto == htons(ETH_P_8021Q)) {
  511. if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
  512. return BATADV_DHCP_NO;
  513. vhdr = vlan_eth_hdr(skb);
  514. proto = vhdr->h_vlan_encapsulated_proto;
  515. *header_len += VLAN_HLEN;
  516. }
  517. /* check for ip header */
  518. switch (proto) {
  519. case htons(ETH_P_IP):
  520. if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
  521. return BATADV_DHCP_NO;
  522. iphdr = (struct iphdr *)(skb->data + *header_len);
  523. *header_len += iphdr->ihl * 4;
  524. /* check for udp header */
  525. if (iphdr->protocol != IPPROTO_UDP)
  526. return BATADV_DHCP_NO;
  527. break;
  528. case htons(ETH_P_IPV6):
  529. if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
  530. return BATADV_DHCP_NO;
  531. ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
  532. *header_len += sizeof(*ipv6hdr);
  533. /* check for udp header */
  534. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  535. return BATADV_DHCP_NO;
  536. break;
  537. default:
  538. return BATADV_DHCP_NO;
  539. }
  540. if (!pskb_may_pull(skb, *header_len + sizeof(*udphdr)))
  541. return BATADV_DHCP_NO;
  542. udphdr = (struct udphdr *)(skb->data + *header_len);
  543. *header_len += sizeof(*udphdr);
  544. /* check for bootp port */
  545. switch (proto) {
  546. case htons(ETH_P_IP):
  547. if (udphdr->dest == htons(67))
  548. ret = BATADV_DHCP_TO_SERVER;
  549. else if (udphdr->source == htons(67))
  550. ret = BATADV_DHCP_TO_CLIENT;
  551. break;
  552. case htons(ETH_P_IPV6):
  553. if (udphdr->dest == htons(547))
  554. ret = BATADV_DHCP_TO_SERVER;
  555. else if (udphdr->source == htons(547))
  556. ret = BATADV_DHCP_TO_CLIENT;
  557. break;
  558. }
  559. chaddr_offset = *header_len + BATADV_DHCP_CHADDR_OFFSET;
  560. /* store the client address if the message is going to a client */
  561. if (ret == BATADV_DHCP_TO_CLIENT &&
  562. pskb_may_pull(skb, chaddr_offset + ETH_ALEN)) {
  563. /* check if the DHCP packet carries an Ethernet DHCP */
  564. p = skb->data + *header_len + BATADV_DHCP_HTYPE_OFFSET;
  565. if (*p != BATADV_DHCP_HTYPE_ETHERNET)
  566. return BATADV_DHCP_NO;
  567. /* check if the DHCP packet carries a valid Ethernet address */
  568. p = skb->data + *header_len + BATADV_DHCP_HLEN_OFFSET;
  569. if (*p != ETH_ALEN)
  570. return BATADV_DHCP_NO;
  571. ether_addr_copy(chaddr, skb->data + chaddr_offset);
  572. }
  573. return ret;
  574. }
  575. /**
  576. * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
  577. * @bat_priv: the bat priv with all the soft interface information
  578. * @skb: the outgoing packet
  579. *
  580. * Check if the skb is a DHCP request and if it is sent to the current best GW
  581. * server. Due to topology changes it may be the case that the GW server
  582. * previously selected is not the best one anymore.
  583. *
  584. * This call might reallocate skb data.
  585. * Must be invoked only when the DHCP packet is going TO a DHCP SERVER.
  586. *
  587. * Return: true if the packet destination is unicast and it is not the best gw,
  588. * false otherwise.
  589. */
  590. bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
  591. struct sk_buff *skb)
  592. {
  593. struct batadv_neigh_node *neigh_curr = NULL;
  594. struct batadv_neigh_node *neigh_old = NULL;
  595. struct batadv_orig_node *orig_dst_node;
  596. struct batadv_gw_node *gw_node = NULL;
  597. struct batadv_gw_node *curr_gw = NULL;
  598. struct batadv_neigh_ifinfo *curr_ifinfo, *old_ifinfo;
  599. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  600. bool out_of_range = false;
  601. u8 curr_tq_avg;
  602. unsigned short vid;
  603. vid = batadv_get_vid(skb, 0);
  604. orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
  605. ethhdr->h_dest, vid);
  606. if (!orig_dst_node)
  607. goto out;
  608. gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
  609. if (!gw_node)
  610. goto out;
  611. switch (atomic_read(&bat_priv->gw.mode)) {
  612. case BATADV_GW_MODE_SERVER:
  613. /* If we are a GW then we are our best GW. We can artificially
  614. * set the tq towards ourself as the maximum value
  615. */
  616. curr_tq_avg = BATADV_TQ_MAX_VALUE;
  617. break;
  618. case BATADV_GW_MODE_CLIENT:
  619. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  620. if (!curr_gw)
  621. goto out;
  622. /* packet is going to our gateway */
  623. if (curr_gw->orig_node == orig_dst_node)
  624. goto out;
  625. /* If the dhcp packet has been sent to a different gw,
  626. * we have to evaluate whether the old gw is still
  627. * reliable enough
  628. */
  629. neigh_curr = batadv_find_router(bat_priv, curr_gw->orig_node,
  630. NULL);
  631. if (!neigh_curr)
  632. goto out;
  633. curr_ifinfo = batadv_neigh_ifinfo_get(neigh_curr,
  634. BATADV_IF_DEFAULT);
  635. if (!curr_ifinfo)
  636. goto out;
  637. curr_tq_avg = curr_ifinfo->bat_iv.tq_avg;
  638. batadv_neigh_ifinfo_put(curr_ifinfo);
  639. break;
  640. case BATADV_GW_MODE_OFF:
  641. default:
  642. goto out;
  643. }
  644. neigh_old = batadv_find_router(bat_priv, orig_dst_node, NULL);
  645. if (!neigh_old)
  646. goto out;
  647. old_ifinfo = batadv_neigh_ifinfo_get(neigh_old, BATADV_IF_DEFAULT);
  648. if (!old_ifinfo)
  649. goto out;
  650. if ((curr_tq_avg - old_ifinfo->bat_iv.tq_avg) > BATADV_GW_THRESHOLD)
  651. out_of_range = true;
  652. batadv_neigh_ifinfo_put(old_ifinfo);
  653. out:
  654. if (orig_dst_node)
  655. batadv_orig_node_put(orig_dst_node);
  656. if (curr_gw)
  657. batadv_gw_node_put(curr_gw);
  658. if (gw_node)
  659. batadv_gw_node_put(gw_node);
  660. if (neigh_old)
  661. batadv_neigh_node_put(neigh_old);
  662. if (neigh_curr)
  663. batadv_neigh_node_put(neigh_curr);
  664. return out_of_range;
  665. }