bat_v_ogm.c 27 KB

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