bat_v_ogm.c 33 KB

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