bat_v_ogm.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2013-2018 B.A.T.M.A.N. contributors:
  3. *
  4. * Antonio Quartulli
  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 "bat_v_ogm.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/jiffies.h>
  27. #include <linux/kernel.h>
  28. #include <linux/kref.h>
  29. #include <linux/list.h>
  30. #include <linux/lockdep.h>
  31. #include <linux/mutex.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/random.h>
  34. #include <linux/rculist.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/slab.h>
  38. #include <linux/stddef.h>
  39. #include <linux/string.h>
  40. #include <linux/types.h>
  41. #include <linux/workqueue.h>
  42. #include <uapi/linux/batadv_packet.h>
  43. #include "bat_algo.h"
  44. #include "hard-interface.h"
  45. #include "hash.h"
  46. #include "log.h"
  47. #include "originator.h"
  48. #include "routing.h"
  49. #include "send.h"
  50. #include "translation-table.h"
  51. #include "tvlv.h"
  52. /**
  53. * batadv_v_ogm_orig_get() - retrieve and possibly create an originator node
  54. * @bat_priv: the bat priv with all the soft interface information
  55. * @addr: the address of the originator
  56. *
  57. * Return: the orig_node corresponding to the specified address. If such object
  58. * does not exist it is allocated here. In case of allocation failure returns
  59. * NULL.
  60. */
  61. struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
  62. const u8 *addr)
  63. {
  64. struct batadv_orig_node *orig_node;
  65. int hash_added;
  66. orig_node = batadv_orig_hash_find(bat_priv, addr);
  67. if (orig_node)
  68. return orig_node;
  69. orig_node = batadv_orig_node_new(bat_priv, addr);
  70. if (!orig_node)
  71. return NULL;
  72. kref_get(&orig_node->refcount);
  73. hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
  74. batadv_choose_orig, orig_node,
  75. &orig_node->hash_entry);
  76. if (hash_added != 0) {
  77. /* remove refcnt for newly created orig_node and hash entry */
  78. batadv_orig_node_put(orig_node);
  79. batadv_orig_node_put(orig_node);
  80. orig_node = NULL;
  81. }
  82. return orig_node;
  83. }
  84. /**
  85. * batadv_v_ogm_start_timer() - restart the OGM sending timer
  86. * @bat_priv: the bat priv with all the soft interface information
  87. */
  88. static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
  89. {
  90. unsigned long msecs;
  91. /* this function may be invoked in different contexts (ogm rescheduling
  92. * or hard_iface activation), but the work timer should not be reset
  93. */
  94. if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
  95. return;
  96. msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
  97. msecs += prandom_u32() % (2 * BATADV_JITTER);
  98. queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
  99. msecs_to_jiffies(msecs));
  100. }
  101. /**
  102. * batadv_v_ogm_send_to_if() - send a batman ogm using a given interface
  103. * @skb: the OGM to send
  104. * @hard_iface: the interface to use to send the OGM
  105. */
  106. static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
  107. struct batadv_hard_iface *hard_iface)
  108. {
  109. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  110. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  111. return;
  112. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
  113. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
  114. skb->len + ETH_HLEN);
  115. batadv_send_broadcast_skb(skb, hard_iface);
  116. }
  117. /**
  118. * batadv_v_ogm_send_softif() - periodic worker broadcasting the own OGM
  119. * @bat_priv: the bat priv with all the soft interface information
  120. */
  121. static void batadv_v_ogm_send_softif(struct batadv_priv *bat_priv)
  122. {
  123. struct batadv_hard_iface *hard_iface;
  124. struct batadv_ogm2_packet *ogm_packet;
  125. struct sk_buff *skb, *skb_tmp;
  126. unsigned char *ogm_buff;
  127. int ogm_buff_len;
  128. u16 tvlv_len = 0;
  129. int ret;
  130. lockdep_assert_held(&bat_priv->bat_v.ogm_buff_mutex);
  131. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  132. goto out;
  133. ogm_buff = bat_priv->bat_v.ogm_buff;
  134. ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
  135. /* tt changes have to be committed before the tvlv data is
  136. * appended as it may alter the tt tvlv container
  137. */
  138. batadv_tt_local_commit_changes(bat_priv);
  139. tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
  140. &ogm_buff_len,
  141. BATADV_OGM2_HLEN);
  142. bat_priv->bat_v.ogm_buff = ogm_buff;
  143. bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
  144. skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
  145. if (!skb)
  146. goto reschedule;
  147. skb_reserve(skb, ETH_HLEN);
  148. skb_put_data(skb, ogm_buff, ogm_buff_len);
  149. ogm_packet = (struct batadv_ogm2_packet *)skb->data;
  150. ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
  151. atomic_inc(&bat_priv->bat_v.ogm_seqno);
  152. ogm_packet->tvlv_len = htons(tvlv_len);
  153. /* broadcast on every interface */
  154. rcu_read_lock();
  155. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  156. if (hard_iface->soft_iface != bat_priv->soft_iface)
  157. continue;
  158. if (!kref_get_unless_zero(&hard_iface->refcount))
  159. continue;
  160. ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
  161. if (ret) {
  162. char *type;
  163. switch (ret) {
  164. case BATADV_HARDIF_BCAST_NORECIPIENT:
  165. type = "no neighbor";
  166. break;
  167. case BATADV_HARDIF_BCAST_DUPFWD:
  168. type = "single neighbor is source";
  169. break;
  170. case BATADV_HARDIF_BCAST_DUPORIG:
  171. type = "single neighbor is originator";
  172. break;
  173. default:
  174. type = "unknown";
  175. }
  176. batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
  177. hard_iface->net_dev->name, type);
  178. batadv_hardif_put(hard_iface);
  179. continue;
  180. }
  181. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  182. "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
  183. ogm_packet->orig, ntohl(ogm_packet->seqno),
  184. ntohl(ogm_packet->throughput), ogm_packet->ttl,
  185. hard_iface->net_dev->name,
  186. hard_iface->net_dev->dev_addr);
  187. /* this skb gets consumed by batadv_v_ogm_send_to_if() */
  188. skb_tmp = skb_clone(skb, GFP_ATOMIC);
  189. if (!skb_tmp) {
  190. batadv_hardif_put(hard_iface);
  191. break;
  192. }
  193. batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
  194. batadv_hardif_put(hard_iface);
  195. }
  196. rcu_read_unlock();
  197. consume_skb(skb);
  198. reschedule:
  199. batadv_v_ogm_start_timer(bat_priv);
  200. out:
  201. return;
  202. }
  203. /**
  204. * batadv_v_ogm_send() - periodic worker broadcasting the own OGM
  205. * @work: work queue item
  206. */
  207. static void batadv_v_ogm_send(struct work_struct *work)
  208. {
  209. struct batadv_priv_bat_v *bat_v;
  210. struct batadv_priv *bat_priv;
  211. bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
  212. bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
  213. mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
  214. batadv_v_ogm_send_softif(bat_priv);
  215. mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
  216. }
  217. /**
  218. * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V
  219. * @hard_iface: the interface to prepare
  220. *
  221. * Takes care of scheduling own OGM sending routine for this interface.
  222. *
  223. * Return: 0 on success or a negative error code otherwise
  224. */
  225. int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
  226. {
  227. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  228. batadv_v_ogm_start_timer(bat_priv);
  229. return 0;
  230. }
  231. /**
  232. * batadv_v_ogm_primary_iface_set() - set a new primary interface
  233. * @primary_iface: the new primary interface
  234. */
  235. void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
  236. {
  237. struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
  238. struct batadv_ogm2_packet *ogm_packet;
  239. mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
  240. if (!bat_priv->bat_v.ogm_buff)
  241. goto unlock;
  242. ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
  243. ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
  244. unlock:
  245. mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
  246. }
  247. /**
  248. * batadv_v_forward_penalty() - apply a penalty to the throughput metric
  249. * forwarded with B.A.T.M.A.N. V OGMs
  250. * @bat_priv: the bat priv with all the soft interface information
  251. * @if_incoming: the interface where the OGM has been received
  252. * @if_outgoing: the interface where the OGM has to be forwarded to
  253. * @throughput: the current throughput
  254. *
  255. * Apply a penalty on the current throughput metric value based on the
  256. * characteristic of the interface where the OGM has been received. The return
  257. * value is computed as follows:
  258. * - throughput * 50% if the incoming and outgoing interface are the
  259. * same WiFi interface and the throughput is above
  260. * 1MBit/s
  261. * - throughput if the outgoing interface is the default
  262. * interface (i.e. this OGM is processed for the
  263. * internal table and not forwarded)
  264. * - throughput * hop penalty otherwise
  265. *
  266. * Return: the penalised throughput metric.
  267. */
  268. static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
  269. struct batadv_hard_iface *if_incoming,
  270. struct batadv_hard_iface *if_outgoing,
  271. u32 throughput)
  272. {
  273. int hop_penalty = atomic_read(&bat_priv->hop_penalty);
  274. int hop_penalty_max = BATADV_TQ_MAX_VALUE;
  275. /* Don't apply hop penalty in default originator table. */
  276. if (if_outgoing == BATADV_IF_DEFAULT)
  277. return throughput;
  278. /* Forwarding on the same WiFi interface cuts the throughput in half
  279. * due to the store & forward characteristics of WIFI.
  280. * Very low throughput values are the exception.
  281. */
  282. if (throughput > 10 &&
  283. if_incoming == if_outgoing &&
  284. !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
  285. return throughput / 2;
  286. /* hop penalty of 255 equals 100% */
  287. return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
  288. }
  289. /**
  290. * batadv_v_ogm_forward() - check conditions and forward an OGM to the given
  291. * outgoing interface
  292. * @bat_priv: the bat priv with all the soft interface information
  293. * @ogm_received: previously received OGM to be forwarded
  294. * @orig_node: the originator which has been updated
  295. * @neigh_node: the neigh_node through with the OGM has been received
  296. * @if_incoming: the interface on which this OGM was received on
  297. * @if_outgoing: the interface to which the OGM has to be forwarded to
  298. *
  299. * Forward an OGM to an interface after having altered the throughput metric and
  300. * the TTL value contained in it. The original OGM isn't modified.
  301. */
  302. static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
  303. const struct batadv_ogm2_packet *ogm_received,
  304. struct batadv_orig_node *orig_node,
  305. struct batadv_neigh_node *neigh_node,
  306. struct batadv_hard_iface *if_incoming,
  307. struct batadv_hard_iface *if_outgoing)
  308. {
  309. struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
  310. struct batadv_orig_ifinfo *orig_ifinfo = NULL;
  311. struct batadv_neigh_node *router = NULL;
  312. struct batadv_ogm2_packet *ogm_forward;
  313. unsigned char *skb_buff;
  314. struct sk_buff *skb;
  315. size_t packet_len;
  316. u16 tvlv_len;
  317. /* only forward for specific interfaces, not for the default one. */
  318. if (if_outgoing == BATADV_IF_DEFAULT)
  319. goto out;
  320. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  321. if (!orig_ifinfo)
  322. goto out;
  323. /* acquire possibly updated router */
  324. router = batadv_orig_router_get(orig_node, if_outgoing);
  325. /* strict rule: forward packets coming from the best next hop only */
  326. if (neigh_node != router)
  327. goto out;
  328. /* don't forward the same seqno twice on one interface */
  329. if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
  330. goto out;
  331. orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
  332. if (ogm_received->ttl <= 1) {
  333. batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
  334. goto out;
  335. }
  336. neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  337. if (!neigh_ifinfo)
  338. goto out;
  339. tvlv_len = ntohs(ogm_received->tvlv_len);
  340. packet_len = BATADV_OGM2_HLEN + tvlv_len;
  341. skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
  342. ETH_HLEN + packet_len);
  343. if (!skb)
  344. goto out;
  345. skb_reserve(skb, ETH_HLEN);
  346. skb_buff = skb_put_data(skb, ogm_received, packet_len);
  347. /* apply forward penalty */
  348. ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
  349. ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
  350. ogm_forward->ttl--;
  351. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  352. "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
  353. if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
  354. ogm_forward->ttl, if_incoming->net_dev->name);
  355. batadv_v_ogm_send_to_if(skb, if_outgoing);
  356. out:
  357. if (orig_ifinfo)
  358. batadv_orig_ifinfo_put(orig_ifinfo);
  359. if (router)
  360. batadv_neigh_node_put(router);
  361. if (neigh_ifinfo)
  362. batadv_neigh_ifinfo_put(neigh_ifinfo);
  363. }
  364. /**
  365. * batadv_v_ogm_metric_update() - update route metric based on OGM
  366. * @bat_priv: the bat priv with all the soft interface information
  367. * @ogm2: OGM2 structure
  368. * @orig_node: Originator structure for which the OGM has been received
  369. * @neigh_node: the neigh_node through with the OGM has been received
  370. * @if_incoming: the interface where this packet was received
  371. * @if_outgoing: the interface for which the packet should be considered
  372. *
  373. * Return:
  374. * 1 if the OGM is new,
  375. * 0 if it is not new but valid,
  376. * <0 on error (e.g. old OGM)
  377. */
  378. static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
  379. const struct batadv_ogm2_packet *ogm2,
  380. struct batadv_orig_node *orig_node,
  381. struct batadv_neigh_node *neigh_node,
  382. struct batadv_hard_iface *if_incoming,
  383. struct batadv_hard_iface *if_outgoing)
  384. {
  385. struct batadv_orig_ifinfo *orig_ifinfo;
  386. struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
  387. bool protection_started = false;
  388. int ret = -EINVAL;
  389. u32 path_throughput;
  390. s32 seq_diff;
  391. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  392. if (!orig_ifinfo)
  393. goto out;
  394. seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
  395. if (!hlist_empty(&orig_node->neigh_list) &&
  396. batadv_window_protected(bat_priv, seq_diff,
  397. BATADV_OGM_MAX_AGE,
  398. &orig_ifinfo->batman_seqno_reset,
  399. &protection_started)) {
  400. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  401. "Drop packet: packet within window protection time from %pM\n",
  402. ogm2->orig);
  403. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  404. "Last reset: %ld, %ld\n",
  405. orig_ifinfo->batman_seqno_reset, jiffies);
  406. goto out;
  407. }
  408. /* drop packets with old seqnos, however accept the first packet after
  409. * a host has been rebooted.
  410. */
  411. if (seq_diff < 0 && !protection_started)
  412. goto out;
  413. neigh_node->last_seen = jiffies;
  414. orig_node->last_seen = jiffies;
  415. orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
  416. orig_ifinfo->last_ttl = ogm2->ttl;
  417. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
  418. if (!neigh_ifinfo)
  419. goto out;
  420. path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
  421. if_outgoing,
  422. ntohl(ogm2->throughput));
  423. neigh_ifinfo->bat_v.throughput = path_throughput;
  424. neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
  425. neigh_ifinfo->last_ttl = ogm2->ttl;
  426. if (seq_diff > 0 || protection_started)
  427. ret = 1;
  428. else
  429. ret = 0;
  430. out:
  431. if (orig_ifinfo)
  432. batadv_orig_ifinfo_put(orig_ifinfo);
  433. if (neigh_ifinfo)
  434. batadv_neigh_ifinfo_put(neigh_ifinfo);
  435. return ret;
  436. }
  437. /**
  438. * batadv_v_ogm_route_update() - update routes based on OGM
  439. * @bat_priv: the bat priv with all the soft interface information
  440. * @ethhdr: the Ethernet header of the OGM2
  441. * @ogm2: OGM2 structure
  442. * @orig_node: Originator structure for which the OGM has been received
  443. * @neigh_node: the neigh_node through with the OGM has been received
  444. * @if_incoming: the interface where this packet was received
  445. * @if_outgoing: the interface for which the packet should be considered
  446. *
  447. * Return: true if the packet should be forwarded, false otherwise
  448. */
  449. static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
  450. const struct ethhdr *ethhdr,
  451. const struct batadv_ogm2_packet *ogm2,
  452. struct batadv_orig_node *orig_node,
  453. struct batadv_neigh_node *neigh_node,
  454. struct batadv_hard_iface *if_incoming,
  455. struct batadv_hard_iface *if_outgoing)
  456. {
  457. struct batadv_neigh_node *router = NULL;
  458. struct batadv_orig_node *orig_neigh_node;
  459. struct batadv_neigh_node *orig_neigh_router = NULL;
  460. struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
  461. u32 router_throughput, neigh_throughput;
  462. u32 router_last_seqno;
  463. u32 neigh_last_seqno;
  464. s32 neigh_seq_diff;
  465. bool forward = false;
  466. orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
  467. if (!orig_neigh_node)
  468. goto out;
  469. orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
  470. if_outgoing);
  471. /* drop packet if sender is not a direct neighbor and if we
  472. * don't route towards it
  473. */
  474. router = batadv_orig_router_get(orig_node, if_outgoing);
  475. if (router && router->orig_node != orig_node && !orig_neigh_router) {
  476. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  477. "Drop packet: OGM via unknown neighbor!\n");
  478. goto out;
  479. }
  480. /* Mark the OGM to be considered for forwarding, and update routes
  481. * if needed.
  482. */
  483. forward = true;
  484. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  485. "Searching and updating originator entry of received packet\n");
  486. /* if this neighbor already is our next hop there is nothing
  487. * to change
  488. */
  489. if (router == neigh_node)
  490. goto out;
  491. /* don't consider neighbours with worse throughput.
  492. * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
  493. * the last received seqno from our best next hop.
  494. */
  495. if (router) {
  496. router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
  497. neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  498. /* if these are not allocated, something is wrong. */
  499. if (!router_ifinfo || !neigh_ifinfo)
  500. goto out;
  501. neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
  502. router_last_seqno = router_ifinfo->bat_v.last_seqno;
  503. neigh_seq_diff = neigh_last_seqno - router_last_seqno;
  504. router_throughput = router_ifinfo->bat_v.throughput;
  505. neigh_throughput = neigh_ifinfo->bat_v.throughput;
  506. if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF &&
  507. router_throughput >= neigh_throughput)
  508. goto out;
  509. }
  510. batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
  511. out:
  512. if (router)
  513. batadv_neigh_node_put(router);
  514. if (orig_neigh_router)
  515. batadv_neigh_node_put(orig_neigh_router);
  516. if (orig_neigh_node)
  517. batadv_orig_node_put(orig_neigh_node);
  518. if (router_ifinfo)
  519. batadv_neigh_ifinfo_put(router_ifinfo);
  520. if (neigh_ifinfo)
  521. batadv_neigh_ifinfo_put(neigh_ifinfo);
  522. return forward;
  523. }
  524. /**
  525. * batadv_v_ogm_process_per_outif() - process a batman v OGM for an outgoing if
  526. * @bat_priv: the bat priv with all the soft interface information
  527. * @ethhdr: the Ethernet header of the OGM2
  528. * @ogm2: OGM2 structure
  529. * @orig_node: Originator structure for which the OGM has been received
  530. * @neigh_node: the neigh_node through with the OGM has been received
  531. * @if_incoming: the interface where this packet was received
  532. * @if_outgoing: the interface for which the packet should be considered
  533. */
  534. static void
  535. batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
  536. const struct ethhdr *ethhdr,
  537. const struct batadv_ogm2_packet *ogm2,
  538. struct batadv_orig_node *orig_node,
  539. struct batadv_neigh_node *neigh_node,
  540. struct batadv_hard_iface *if_incoming,
  541. struct batadv_hard_iface *if_outgoing)
  542. {
  543. int seqno_age;
  544. bool forward;
  545. /* first, update the metric with according sanity checks */
  546. seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
  547. neigh_node, if_incoming,
  548. if_outgoing);
  549. /* outdated sequence numbers are to be discarded */
  550. if (seqno_age < 0)
  551. return;
  552. /* only unknown & newer OGMs contain TVLVs we are interested in */
  553. if (seqno_age > 0 && if_outgoing == BATADV_IF_DEFAULT)
  554. batadv_tvlv_containers_process(bat_priv, true, orig_node,
  555. NULL, NULL,
  556. (unsigned char *)(ogm2 + 1),
  557. ntohs(ogm2->tvlv_len));
  558. /* if the metric update went through, update routes if needed */
  559. forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
  560. neigh_node, if_incoming,
  561. if_outgoing);
  562. /* if the routes have been processed correctly, check and forward */
  563. if (forward)
  564. batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
  565. if_incoming, if_outgoing);
  566. }
  567. /**
  568. * batadv_v_ogm_aggr_packet() - checks if there is another OGM aggregated
  569. * @buff_pos: current position in the skb
  570. * @packet_len: total length of the skb
  571. * @ogm2_packet: potential OGM2 in buffer
  572. *
  573. * Return: true if there is enough space for another OGM, false otherwise.
  574. */
  575. static bool
  576. batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
  577. const struct batadv_ogm2_packet *ogm2_packet)
  578. {
  579. int next_buff_pos = 0;
  580. /* check if there is enough space for the header */
  581. next_buff_pos += buff_pos + sizeof(*ogm2_packet);
  582. if (next_buff_pos > packet_len)
  583. return false;
  584. /* check if there is enough space for the optional TVLV */
  585. next_buff_pos += ntohs(ogm2_packet->tvlv_len);
  586. return (next_buff_pos <= packet_len) &&
  587. (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
  588. }
  589. /**
  590. * batadv_v_ogm_process() - process an incoming batman v OGM
  591. * @skb: the skb containing the OGM
  592. * @ogm_offset: offset to the OGM which should be processed (for aggregates)
  593. * @if_incoming: the interface where this packet was receved
  594. */
  595. static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
  596. struct batadv_hard_iface *if_incoming)
  597. {
  598. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  599. struct ethhdr *ethhdr;
  600. struct batadv_orig_node *orig_node = NULL;
  601. struct batadv_hardif_neigh_node *hardif_neigh = NULL;
  602. struct batadv_neigh_node *neigh_node = NULL;
  603. struct batadv_hard_iface *hard_iface;
  604. struct batadv_ogm2_packet *ogm_packet;
  605. u32 ogm_throughput, link_throughput, path_throughput;
  606. int ret;
  607. ethhdr = eth_hdr(skb);
  608. ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
  609. ogm_throughput = ntohl(ogm_packet->throughput);
  610. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  611. "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, throughput %u, TTL %u, V %u, tvlv_len %u)\n",
  612. ethhdr->h_source, if_incoming->net_dev->name,
  613. if_incoming->net_dev->dev_addr, ogm_packet->orig,
  614. ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
  615. ogm_packet->version, ntohs(ogm_packet->tvlv_len));
  616. /* If the throughput metric is 0, immediately drop the packet. No need
  617. * to create orig_node / neigh_node for an unusable route.
  618. */
  619. if (ogm_throughput == 0) {
  620. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  621. "Drop packet: originator packet with throughput metric of 0\n");
  622. return;
  623. }
  624. /* require ELP packets be to received from this neighbor first */
  625. hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
  626. if (!hardif_neigh) {
  627. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  628. "Drop packet: OGM via unknown neighbor!\n");
  629. goto out;
  630. }
  631. orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
  632. if (!orig_node)
  633. return;
  634. neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
  635. ethhdr->h_source);
  636. if (!neigh_node)
  637. goto out;
  638. /* Update the received throughput metric to match the link
  639. * characteristic:
  640. * - If this OGM traveled one hop so far (emitted by single hop
  641. * neighbor) the path throughput metric equals the link throughput.
  642. * - For OGMs traversing more than hop the path throughput metric is
  643. * the smaller of the path throughput and the link throughput.
  644. */
  645. link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
  646. path_throughput = min_t(u32, link_throughput, ogm_throughput);
  647. ogm_packet->throughput = htonl(path_throughput);
  648. batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
  649. neigh_node, if_incoming,
  650. BATADV_IF_DEFAULT);
  651. rcu_read_lock();
  652. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  653. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  654. continue;
  655. if (hard_iface->soft_iface != bat_priv->soft_iface)
  656. continue;
  657. if (!kref_get_unless_zero(&hard_iface->refcount))
  658. continue;
  659. ret = batadv_hardif_no_broadcast(hard_iface,
  660. ogm_packet->orig,
  661. hardif_neigh->orig);
  662. if (ret) {
  663. char *type;
  664. switch (ret) {
  665. case BATADV_HARDIF_BCAST_NORECIPIENT:
  666. type = "no neighbor";
  667. break;
  668. case BATADV_HARDIF_BCAST_DUPFWD:
  669. type = "single neighbor is source";
  670. break;
  671. case BATADV_HARDIF_BCAST_DUPORIG:
  672. type = "single neighbor is originator";
  673. break;
  674. default:
  675. type = "unknown";
  676. }
  677. batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n",
  678. ogm_packet->orig, hard_iface->net_dev->name,
  679. type);
  680. batadv_hardif_put(hard_iface);
  681. continue;
  682. }
  683. batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
  684. orig_node, neigh_node,
  685. if_incoming, hard_iface);
  686. batadv_hardif_put(hard_iface);
  687. }
  688. rcu_read_unlock();
  689. out:
  690. if (orig_node)
  691. batadv_orig_node_put(orig_node);
  692. if (neigh_node)
  693. batadv_neigh_node_put(neigh_node);
  694. if (hardif_neigh)
  695. batadv_hardif_neigh_put(hardif_neigh);
  696. }
  697. /**
  698. * batadv_v_ogm_packet_recv() - OGM2 receiving handler
  699. * @skb: the received OGM
  700. * @if_incoming: the interface where this OGM has been received
  701. *
  702. * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
  703. * (without freeing the skb) on failure
  704. */
  705. int batadv_v_ogm_packet_recv(struct sk_buff *skb,
  706. struct batadv_hard_iface *if_incoming)
  707. {
  708. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  709. struct batadv_ogm2_packet *ogm_packet;
  710. struct ethhdr *ethhdr = eth_hdr(skb);
  711. int ogm_offset;
  712. u8 *packet_pos;
  713. int ret = NET_RX_DROP;
  714. /* did we receive a OGM2 packet on an interface that does not have
  715. * B.A.T.M.A.N. V enabled ?
  716. */
  717. if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
  718. goto free_skb;
  719. if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
  720. goto free_skb;
  721. if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
  722. goto free_skb;
  723. ogm_packet = (struct batadv_ogm2_packet *)skb->data;
  724. if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
  725. goto free_skb;
  726. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
  727. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
  728. skb->len + ETH_HLEN);
  729. ogm_offset = 0;
  730. ogm_packet = (struct batadv_ogm2_packet *)skb->data;
  731. while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
  732. ogm_packet)) {
  733. batadv_v_ogm_process(skb, ogm_offset, if_incoming);
  734. ogm_offset += BATADV_OGM2_HLEN;
  735. ogm_offset += ntohs(ogm_packet->tvlv_len);
  736. packet_pos = skb->data + ogm_offset;
  737. ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
  738. }
  739. ret = NET_RX_SUCCESS;
  740. free_skb:
  741. if (ret == NET_RX_SUCCESS)
  742. consume_skb(skb);
  743. else
  744. kfree_skb(skb);
  745. return ret;
  746. }
  747. /**
  748. * batadv_v_ogm_init() - initialise the OGM2 engine
  749. * @bat_priv: the bat priv with all the soft interface information
  750. *
  751. * Return: 0 on success or a negative error code in case of failure
  752. */
  753. int batadv_v_ogm_init(struct batadv_priv *bat_priv)
  754. {
  755. struct batadv_ogm2_packet *ogm_packet;
  756. unsigned char *ogm_buff;
  757. u32 random_seqno;
  758. bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
  759. ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
  760. if (!ogm_buff)
  761. return -ENOMEM;
  762. bat_priv->bat_v.ogm_buff = ogm_buff;
  763. ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
  764. ogm_packet->packet_type = BATADV_OGM2;
  765. ogm_packet->version = BATADV_COMPAT_VERSION;
  766. ogm_packet->ttl = BATADV_TTL;
  767. ogm_packet->flags = BATADV_NO_FLAGS;
  768. ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
  769. /* randomize initial seqno to avoid collision */
  770. get_random_bytes(&random_seqno, sizeof(random_seqno));
  771. atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
  772. INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
  773. mutex_init(&bat_priv->bat_v.ogm_buff_mutex);
  774. return 0;
  775. }
  776. /**
  777. * batadv_v_ogm_free() - free OGM private resources
  778. * @bat_priv: the bat priv with all the soft interface information
  779. */
  780. void batadv_v_ogm_free(struct batadv_priv *bat_priv)
  781. {
  782. cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
  783. mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
  784. kfree(bat_priv->bat_v.ogm_buff);
  785. bat_priv->bat_v.ogm_buff = NULL;
  786. bat_priv->bat_v.ogm_buff_len = 0;
  787. mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
  788. }