bat_v_ogm.c 27 KB

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