bat_v_ogm.c 33 KB

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