bat_v_ogm.c 26 KB

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