routing.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. */
  6. #include "routing.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/byteorder/generic.h>
  10. #include <linux/compiler.h>
  11. #include <linux/errno.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/if_ether.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/kref.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/printk.h>
  18. #include <linux/rculist.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/stddef.h>
  23. #include <uapi/linux/batadv_packet.h>
  24. #include "bitarray.h"
  25. #include "bridge_loop_avoidance.h"
  26. #include "distributed-arp-table.h"
  27. #include "fragmentation.h"
  28. #include "hard-interface.h"
  29. #include "log.h"
  30. #include "network-coding.h"
  31. #include "originator.h"
  32. #include "send.h"
  33. #include "soft-interface.h"
  34. #include "tp_meter.h"
  35. #include "translation-table.h"
  36. #include "tvlv.h"
  37. static int batadv_route_unicast_packet(struct sk_buff *skb,
  38. struct batadv_hard_iface *recv_if);
  39. /**
  40. * _batadv_update_route() - set the router for this originator
  41. * @bat_priv: the bat priv with all the soft interface information
  42. * @orig_node: orig node which is to be configured
  43. * @recv_if: the receive interface for which this route is set
  44. * @neigh_node: neighbor which should be the next router
  45. *
  46. * This function does not perform any error checks
  47. */
  48. static void _batadv_update_route(struct batadv_priv *bat_priv,
  49. struct batadv_orig_node *orig_node,
  50. struct batadv_hard_iface *recv_if,
  51. struct batadv_neigh_node *neigh_node)
  52. {
  53. struct batadv_orig_ifinfo *orig_ifinfo;
  54. struct batadv_neigh_node *curr_router;
  55. orig_ifinfo = batadv_orig_ifinfo_get(orig_node, recv_if);
  56. if (!orig_ifinfo)
  57. return;
  58. spin_lock_bh(&orig_node->neigh_list_lock);
  59. /* curr_router used earlier may not be the current orig_ifinfo->router
  60. * anymore because it was dereferenced outside of the neigh_list_lock
  61. * protected region. After the new best neighbor has replace the current
  62. * best neighbor the reference counter needs to decrease. Consequently,
  63. * the code needs to ensure the curr_router variable contains a pointer
  64. * to the replaced best neighbor.
  65. */
  66. /* increase refcount of new best neighbor */
  67. if (neigh_node)
  68. kref_get(&neigh_node->refcount);
  69. curr_router = rcu_replace_pointer(orig_ifinfo->router, neigh_node,
  70. true);
  71. spin_unlock_bh(&orig_node->neigh_list_lock);
  72. batadv_orig_ifinfo_put(orig_ifinfo);
  73. /* route deleted */
  74. if (curr_router && !neigh_node) {
  75. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  76. "Deleting route towards: %pM\n", orig_node->orig);
  77. batadv_tt_global_del_orig(bat_priv, orig_node, -1,
  78. "Deleted route towards originator");
  79. /* route added */
  80. } else if (!curr_router && neigh_node) {
  81. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  82. "Adding route towards: %pM (via %pM)\n",
  83. orig_node->orig, neigh_node->addr);
  84. /* route changed */
  85. } else if (neigh_node && curr_router) {
  86. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  87. "Changing route towards: %pM (now via %pM - was via %pM)\n",
  88. orig_node->orig, neigh_node->addr,
  89. curr_router->addr);
  90. }
  91. /* decrease refcount of previous best neighbor */
  92. batadv_neigh_node_put(curr_router);
  93. }
  94. /**
  95. * batadv_update_route() - set the router for this originator
  96. * @bat_priv: the bat priv with all the soft interface information
  97. * @orig_node: orig node which is to be configured
  98. * @recv_if: the receive interface for which this route is set
  99. * @neigh_node: neighbor which should be the next router
  100. */
  101. void batadv_update_route(struct batadv_priv *bat_priv,
  102. struct batadv_orig_node *orig_node,
  103. struct batadv_hard_iface *recv_if,
  104. struct batadv_neigh_node *neigh_node)
  105. {
  106. struct batadv_neigh_node *router = NULL;
  107. if (!orig_node)
  108. goto out;
  109. router = batadv_orig_router_get(orig_node, recv_if);
  110. if (router != neigh_node)
  111. _batadv_update_route(bat_priv, orig_node, recv_if, neigh_node);
  112. out:
  113. batadv_neigh_node_put(router);
  114. }
  115. /**
  116. * batadv_window_protected() - checks whether the host restarted and is in the
  117. * protection time.
  118. * @bat_priv: the bat priv with all the soft interface information
  119. * @seq_num_diff: difference between the current/received sequence number and
  120. * the last sequence number
  121. * @seq_old_max_diff: maximum age of sequence number not considered as restart
  122. * @last_reset: jiffies timestamp of the last reset, will be updated when reset
  123. * is detected
  124. * @protection_started: is set to true if the protection window was started,
  125. * doesn't change otherwise.
  126. *
  127. * Return:
  128. * false if the packet is to be accepted.
  129. * true if the packet is to be ignored.
  130. */
  131. bool batadv_window_protected(struct batadv_priv *bat_priv, s32 seq_num_diff,
  132. s32 seq_old_max_diff, unsigned long *last_reset,
  133. bool *protection_started)
  134. {
  135. if (seq_num_diff <= -seq_old_max_diff ||
  136. seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
  137. if (!batadv_has_timed_out(*last_reset,
  138. BATADV_RESET_PROTECTION_MS))
  139. return true;
  140. *last_reset = jiffies;
  141. if (protection_started)
  142. *protection_started = true;
  143. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  144. "old packet received, start protection\n");
  145. }
  146. return false;
  147. }
  148. /**
  149. * batadv_check_management_packet() - Check preconditions for management packets
  150. * @skb: incoming packet buffer
  151. * @hard_iface: incoming hard interface
  152. * @header_len: minimal header length of packet type
  153. *
  154. * Return: true when management preconditions are met, false otherwise
  155. */
  156. bool batadv_check_management_packet(struct sk_buff *skb,
  157. struct batadv_hard_iface *hard_iface,
  158. int header_len)
  159. {
  160. struct ethhdr *ethhdr;
  161. /* drop packet if it has not necessary minimum size */
  162. if (unlikely(!pskb_may_pull(skb, header_len)))
  163. return false;
  164. ethhdr = eth_hdr(skb);
  165. /* packet with broadcast indication but unicast recipient */
  166. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  167. return false;
  168. /* packet with invalid sender address */
  169. if (!is_valid_ether_addr(ethhdr->h_source))
  170. return false;
  171. /* create a copy of the skb, if needed, to modify it. */
  172. if (skb_cow(skb, 0) < 0)
  173. return false;
  174. /* keep skb linear */
  175. if (skb_linearize(skb) < 0)
  176. return false;
  177. return true;
  178. }
  179. /**
  180. * batadv_recv_my_icmp_packet() - receive an icmp packet locally
  181. * @bat_priv: the bat priv with all the soft interface information
  182. * @skb: icmp packet to process
  183. *
  184. * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  185. * otherwise.
  186. */
  187. static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
  188. struct sk_buff *skb)
  189. {
  190. struct batadv_hard_iface *primary_if = NULL;
  191. struct batadv_orig_node *orig_node = NULL;
  192. struct batadv_icmp_header *icmph;
  193. int res, ret = NET_RX_DROP;
  194. icmph = (struct batadv_icmp_header *)skb->data;
  195. switch (icmph->msg_type) {
  196. case BATADV_ECHO_REQUEST:
  197. /* answer echo request (ping) */
  198. primary_if = batadv_primary_if_get_selected(bat_priv);
  199. if (!primary_if)
  200. goto out;
  201. /* get routing information */
  202. orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
  203. if (!orig_node)
  204. goto out;
  205. /* create a copy of the skb, if needed, to modify it. */
  206. if (skb_cow(skb, ETH_HLEN) < 0)
  207. goto out;
  208. icmph = (struct batadv_icmp_header *)skb->data;
  209. ether_addr_copy(icmph->dst, icmph->orig);
  210. ether_addr_copy(icmph->orig, primary_if->net_dev->dev_addr);
  211. icmph->msg_type = BATADV_ECHO_REPLY;
  212. icmph->ttl = BATADV_TTL;
  213. res = batadv_send_skb_to_orig(skb, orig_node, NULL);
  214. if (res == NET_XMIT_SUCCESS)
  215. ret = NET_RX_SUCCESS;
  216. /* skb was consumed */
  217. skb = NULL;
  218. break;
  219. case BATADV_TP:
  220. if (!pskb_may_pull(skb, sizeof(struct batadv_icmp_tp_packet)))
  221. goto out;
  222. batadv_tp_meter_recv(bat_priv, skb);
  223. ret = NET_RX_SUCCESS;
  224. /* skb was consumed */
  225. skb = NULL;
  226. goto out;
  227. default:
  228. /* drop unknown type */
  229. goto out;
  230. }
  231. out:
  232. batadv_hardif_put(primary_if);
  233. batadv_orig_node_put(orig_node);
  234. kfree_skb(skb);
  235. return ret;
  236. }
  237. static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
  238. struct sk_buff *skb)
  239. {
  240. struct batadv_hard_iface *primary_if = NULL;
  241. struct batadv_orig_node *orig_node = NULL;
  242. struct batadv_icmp_packet *icmp_packet;
  243. int res, ret = NET_RX_DROP;
  244. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  245. /* send TTL exceeded if packet is an echo request (traceroute) */
  246. if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
  247. pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
  248. icmp_packet->orig, icmp_packet->dst);
  249. goto out;
  250. }
  251. primary_if = batadv_primary_if_get_selected(bat_priv);
  252. if (!primary_if)
  253. goto out;
  254. /* get routing information */
  255. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  256. if (!orig_node)
  257. goto out;
  258. /* create a copy of the skb, if needed, to modify it. */
  259. if (skb_cow(skb, ETH_HLEN) < 0)
  260. goto out;
  261. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  262. ether_addr_copy(icmp_packet->dst, icmp_packet->orig);
  263. ether_addr_copy(icmp_packet->orig, primary_if->net_dev->dev_addr);
  264. icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
  265. icmp_packet->ttl = BATADV_TTL;
  266. res = batadv_send_skb_to_orig(skb, orig_node, NULL);
  267. if (res == NET_RX_SUCCESS)
  268. ret = NET_XMIT_SUCCESS;
  269. /* skb was consumed */
  270. skb = NULL;
  271. out:
  272. batadv_hardif_put(primary_if);
  273. batadv_orig_node_put(orig_node);
  274. kfree_skb(skb);
  275. return ret;
  276. }
  277. /**
  278. * batadv_recv_icmp_packet() - Process incoming icmp packet
  279. * @skb: incoming packet buffer
  280. * @recv_if: incoming hard interface
  281. *
  282. * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
  283. */
  284. int batadv_recv_icmp_packet(struct sk_buff *skb,
  285. struct batadv_hard_iface *recv_if)
  286. {
  287. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  288. struct batadv_icmp_header *icmph;
  289. struct batadv_icmp_packet_rr *icmp_packet_rr;
  290. struct ethhdr *ethhdr;
  291. struct batadv_orig_node *orig_node = NULL;
  292. int hdr_size = sizeof(struct batadv_icmp_header);
  293. int res, ret = NET_RX_DROP;
  294. /* drop packet if it has not necessary minimum size */
  295. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  296. goto free_skb;
  297. ethhdr = eth_hdr(skb);
  298. /* packet with unicast indication but non-unicast recipient */
  299. if (!is_valid_ether_addr(ethhdr->h_dest))
  300. goto free_skb;
  301. /* packet with broadcast/multicast sender address */
  302. if (is_multicast_ether_addr(ethhdr->h_source))
  303. goto free_skb;
  304. /* not for me */
  305. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  306. goto free_skb;
  307. icmph = (struct batadv_icmp_header *)skb->data;
  308. /* add record route information if not full */
  309. if ((icmph->msg_type == BATADV_ECHO_REPLY ||
  310. icmph->msg_type == BATADV_ECHO_REQUEST) &&
  311. skb->len >= sizeof(struct batadv_icmp_packet_rr)) {
  312. if (skb_linearize(skb) < 0)
  313. goto free_skb;
  314. /* create a copy of the skb, if needed, to modify it. */
  315. if (skb_cow(skb, ETH_HLEN) < 0)
  316. goto free_skb;
  317. ethhdr = eth_hdr(skb);
  318. icmph = (struct batadv_icmp_header *)skb->data;
  319. icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
  320. if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
  321. goto free_skb;
  322. ether_addr_copy(icmp_packet_rr->rr[icmp_packet_rr->rr_cur],
  323. ethhdr->h_dest);
  324. icmp_packet_rr->rr_cur++;
  325. }
  326. /* packet for me */
  327. if (batadv_is_my_mac(bat_priv, icmph->dst))
  328. return batadv_recv_my_icmp_packet(bat_priv, skb);
  329. /* TTL exceeded */
  330. if (icmph->ttl < 2)
  331. return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
  332. /* get routing information */
  333. orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
  334. if (!orig_node)
  335. goto free_skb;
  336. /* create a copy of the skb, if needed, to modify it. */
  337. if (skb_cow(skb, ETH_HLEN) < 0)
  338. goto put_orig_node;
  339. icmph = (struct batadv_icmp_header *)skb->data;
  340. /* decrement ttl */
  341. icmph->ttl--;
  342. /* route it */
  343. res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
  344. if (res == NET_XMIT_SUCCESS)
  345. ret = NET_RX_SUCCESS;
  346. /* skb was consumed */
  347. skb = NULL;
  348. put_orig_node:
  349. batadv_orig_node_put(orig_node);
  350. free_skb:
  351. kfree_skb(skb);
  352. return ret;
  353. }
  354. /**
  355. * batadv_check_unicast_packet() - Check for malformed unicast packets
  356. * @bat_priv: the bat priv with all the soft interface information
  357. * @skb: packet to check
  358. * @hdr_size: size of header to pull
  359. *
  360. * Checks for short header and bad addresses in the given packet.
  361. *
  362. * Return: negative value when check fails and 0 otherwise. The negative value
  363. * depends on the reason: -ENODATA for bad header, -EBADR for broadcast
  364. * destination or source, and -EREMOTE for non-local (other host) destination.
  365. */
  366. static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
  367. struct sk_buff *skb, int hdr_size)
  368. {
  369. struct ethhdr *ethhdr;
  370. /* drop packet if it has not necessary minimum size */
  371. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  372. return -ENODATA;
  373. ethhdr = eth_hdr(skb);
  374. /* packet with unicast indication but non-unicast recipient */
  375. if (!is_valid_ether_addr(ethhdr->h_dest))
  376. return -EBADR;
  377. /* packet with broadcast/multicast sender address */
  378. if (is_multicast_ether_addr(ethhdr->h_source))
  379. return -EBADR;
  380. /* not for me */
  381. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  382. return -EREMOTE;
  383. return 0;
  384. }
  385. /**
  386. * batadv_last_bonding_get() - Get last_bonding_candidate of orig_node
  387. * @orig_node: originator node whose last bonding candidate should be retrieved
  388. *
  389. * Return: last bonding candidate of router or NULL if not found
  390. *
  391. * The object is returned with refcounter increased by 1.
  392. */
  393. static struct batadv_orig_ifinfo *
  394. batadv_last_bonding_get(struct batadv_orig_node *orig_node)
  395. {
  396. struct batadv_orig_ifinfo *last_bonding_candidate;
  397. spin_lock_bh(&orig_node->neigh_list_lock);
  398. last_bonding_candidate = orig_node->last_bonding_candidate;
  399. if (last_bonding_candidate)
  400. kref_get(&last_bonding_candidate->refcount);
  401. spin_unlock_bh(&orig_node->neigh_list_lock);
  402. return last_bonding_candidate;
  403. }
  404. /**
  405. * batadv_last_bonding_replace() - Replace last_bonding_candidate of orig_node
  406. * @orig_node: originator node whose bonding candidates should be replaced
  407. * @new_candidate: new bonding candidate or NULL
  408. */
  409. static void
  410. batadv_last_bonding_replace(struct batadv_orig_node *orig_node,
  411. struct batadv_orig_ifinfo *new_candidate)
  412. {
  413. struct batadv_orig_ifinfo *old_candidate;
  414. spin_lock_bh(&orig_node->neigh_list_lock);
  415. old_candidate = orig_node->last_bonding_candidate;
  416. if (new_candidate)
  417. kref_get(&new_candidate->refcount);
  418. orig_node->last_bonding_candidate = new_candidate;
  419. spin_unlock_bh(&orig_node->neigh_list_lock);
  420. batadv_orig_ifinfo_put(old_candidate);
  421. }
  422. /**
  423. * batadv_find_router() - find a suitable router for this originator
  424. * @bat_priv: the bat priv with all the soft interface information
  425. * @orig_node: the destination node
  426. * @recv_if: pointer to interface this packet was received on
  427. *
  428. * Return: the router which should be used for this orig_node on
  429. * this interface, or NULL if not available.
  430. */
  431. struct batadv_neigh_node *
  432. batadv_find_router(struct batadv_priv *bat_priv,
  433. struct batadv_orig_node *orig_node,
  434. struct batadv_hard_iface *recv_if)
  435. {
  436. struct batadv_algo_ops *bao = bat_priv->algo_ops;
  437. struct batadv_neigh_node *first_candidate_router = NULL;
  438. struct batadv_neigh_node *next_candidate_router = NULL;
  439. struct batadv_neigh_node *router, *cand_router = NULL;
  440. struct batadv_neigh_node *last_cand_router = NULL;
  441. struct batadv_orig_ifinfo *cand, *first_candidate = NULL;
  442. struct batadv_orig_ifinfo *next_candidate = NULL;
  443. struct batadv_orig_ifinfo *last_candidate;
  444. bool last_candidate_found = false;
  445. if (!orig_node)
  446. return NULL;
  447. router = batadv_orig_router_get(orig_node, recv_if);
  448. if (!router)
  449. return router;
  450. /* only consider bonding for recv_if == BATADV_IF_DEFAULT (first hop)
  451. * and if activated.
  452. */
  453. if (!(recv_if == BATADV_IF_DEFAULT && atomic_read(&bat_priv->bonding)))
  454. return router;
  455. /* bonding: loop through the list of possible routers found
  456. * for the various outgoing interfaces and find a candidate after
  457. * the last chosen bonding candidate (next_candidate). If no such
  458. * router is found, use the first candidate found (the previously
  459. * chosen bonding candidate might have been the last one in the list).
  460. * If this can't be found either, return the previously chosen
  461. * router - obviously there are no other candidates.
  462. */
  463. rcu_read_lock();
  464. last_candidate = batadv_last_bonding_get(orig_node);
  465. if (last_candidate)
  466. last_cand_router = rcu_dereference(last_candidate->router);
  467. hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
  468. /* acquire some structures and references ... */
  469. if (!kref_get_unless_zero(&cand->refcount))
  470. continue;
  471. cand_router = rcu_dereference(cand->router);
  472. if (!cand_router)
  473. goto next;
  474. if (!kref_get_unless_zero(&cand_router->refcount)) {
  475. cand_router = NULL;
  476. goto next;
  477. }
  478. /* alternative candidate should be good enough to be
  479. * considered
  480. */
  481. if (!bao->neigh.is_similar_or_better(cand_router,
  482. cand->if_outgoing, router,
  483. recv_if))
  484. goto next;
  485. /* don't use the same router twice */
  486. if (last_cand_router == cand_router)
  487. goto next;
  488. /* mark the first possible candidate */
  489. if (!first_candidate) {
  490. kref_get(&cand_router->refcount);
  491. kref_get(&cand->refcount);
  492. first_candidate = cand;
  493. first_candidate_router = cand_router;
  494. }
  495. /* check if the loop has already passed the previously selected
  496. * candidate ... this function should select the next candidate
  497. * AFTER the previously used bonding candidate.
  498. */
  499. if (!last_candidate || last_candidate_found) {
  500. next_candidate = cand;
  501. next_candidate_router = cand_router;
  502. break;
  503. }
  504. if (last_candidate == cand)
  505. last_candidate_found = true;
  506. next:
  507. /* free references */
  508. if (cand_router) {
  509. batadv_neigh_node_put(cand_router);
  510. cand_router = NULL;
  511. }
  512. batadv_orig_ifinfo_put(cand);
  513. }
  514. rcu_read_unlock();
  515. /* After finding candidates, handle the three cases:
  516. * 1) there is a next candidate, use that
  517. * 2) there is no next candidate, use the first of the list
  518. * 3) there is no candidate at all, return the default router
  519. */
  520. if (next_candidate) {
  521. batadv_neigh_node_put(router);
  522. kref_get(&next_candidate_router->refcount);
  523. router = next_candidate_router;
  524. batadv_last_bonding_replace(orig_node, next_candidate);
  525. } else if (first_candidate) {
  526. batadv_neigh_node_put(router);
  527. kref_get(&first_candidate_router->refcount);
  528. router = first_candidate_router;
  529. batadv_last_bonding_replace(orig_node, first_candidate);
  530. } else {
  531. batadv_last_bonding_replace(orig_node, NULL);
  532. }
  533. /* cleanup of candidates */
  534. if (first_candidate) {
  535. batadv_neigh_node_put(first_candidate_router);
  536. batadv_orig_ifinfo_put(first_candidate);
  537. }
  538. if (next_candidate) {
  539. batadv_neigh_node_put(next_candidate_router);
  540. batadv_orig_ifinfo_put(next_candidate);
  541. }
  542. batadv_orig_ifinfo_put(last_candidate);
  543. return router;
  544. }
  545. static int batadv_route_unicast_packet(struct sk_buff *skb,
  546. struct batadv_hard_iface *recv_if)
  547. {
  548. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  549. struct batadv_orig_node *orig_node = NULL;
  550. struct batadv_unicast_packet *unicast_packet;
  551. struct ethhdr *ethhdr = eth_hdr(skb);
  552. int res, hdr_len, ret = NET_RX_DROP;
  553. unsigned int len;
  554. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  555. /* TTL exceeded */
  556. if (unicast_packet->ttl < 2) {
  557. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  558. ethhdr->h_source, unicast_packet->dest);
  559. goto free_skb;
  560. }
  561. /* get routing information */
  562. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  563. if (!orig_node)
  564. goto free_skb;
  565. /* create a copy of the skb, if needed, to modify it. */
  566. if (skb_cow(skb, ETH_HLEN) < 0)
  567. goto put_orig_node;
  568. /* decrement ttl */
  569. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  570. unicast_packet->ttl--;
  571. switch (unicast_packet->packet_type) {
  572. case BATADV_UNICAST_4ADDR:
  573. hdr_len = sizeof(struct batadv_unicast_4addr_packet);
  574. break;
  575. case BATADV_UNICAST:
  576. hdr_len = sizeof(struct batadv_unicast_packet);
  577. break;
  578. default:
  579. /* other packet types not supported - yet */
  580. hdr_len = -1;
  581. break;
  582. }
  583. if (hdr_len > 0)
  584. batadv_skb_set_priority(skb, hdr_len);
  585. len = skb->len;
  586. res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
  587. /* translate transmit result into receive result */
  588. if (res == NET_XMIT_SUCCESS) {
  589. ret = NET_RX_SUCCESS;
  590. /* skb was transmitted and consumed */
  591. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  592. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  593. len + ETH_HLEN);
  594. }
  595. /* skb was consumed */
  596. skb = NULL;
  597. put_orig_node:
  598. batadv_orig_node_put(orig_node);
  599. free_skb:
  600. kfree_skb(skb);
  601. return ret;
  602. }
  603. /**
  604. * batadv_reroute_unicast_packet() - update the unicast header for re-routing
  605. * @bat_priv: the bat priv with all the soft interface information
  606. * @skb: unicast packet to process
  607. * @unicast_packet: the unicast header to be updated
  608. * @dst_addr: the payload destination
  609. * @vid: VLAN identifier
  610. *
  611. * Search the translation table for dst_addr and update the unicast header with
  612. * the new corresponding information (originator address where the destination
  613. * client currently is and its known TTVN)
  614. *
  615. * Return: true if the packet header has been updated, false otherwise
  616. */
  617. static bool
  618. batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
  619. struct batadv_unicast_packet *unicast_packet,
  620. u8 *dst_addr, unsigned short vid)
  621. {
  622. struct batadv_orig_node *orig_node = NULL;
  623. struct batadv_hard_iface *primary_if = NULL;
  624. bool ret = false;
  625. const u8 *orig_addr;
  626. u8 orig_ttvn;
  627. if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
  628. primary_if = batadv_primary_if_get_selected(bat_priv);
  629. if (!primary_if)
  630. goto out;
  631. orig_addr = primary_if->net_dev->dev_addr;
  632. orig_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
  633. } else {
  634. orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
  635. vid);
  636. if (!orig_node)
  637. goto out;
  638. if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
  639. goto out;
  640. orig_addr = orig_node->orig;
  641. orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
  642. }
  643. /* update the packet header */
  644. skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
  645. ether_addr_copy(unicast_packet->dest, orig_addr);
  646. unicast_packet->ttvn = orig_ttvn;
  647. skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
  648. ret = true;
  649. out:
  650. batadv_hardif_put(primary_if);
  651. batadv_orig_node_put(orig_node);
  652. return ret;
  653. }
  654. static bool batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  655. struct sk_buff *skb, int hdr_len)
  656. {
  657. struct batadv_unicast_packet *unicast_packet;
  658. struct batadv_hard_iface *primary_if;
  659. struct batadv_orig_node *orig_node;
  660. u8 curr_ttvn, old_ttvn;
  661. struct ethhdr *ethhdr;
  662. unsigned short vid;
  663. int is_old_ttvn;
  664. /* check if there is enough data before accessing it */
  665. if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
  666. return false;
  667. /* create a copy of the skb (in case of for re-routing) to modify it. */
  668. if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
  669. return false;
  670. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  671. vid = batadv_get_vid(skb, hdr_len);
  672. ethhdr = (struct ethhdr *)(skb->data + hdr_len);
  673. /* do not reroute multicast frames in a unicast header */
  674. if (is_multicast_ether_addr(ethhdr->h_dest))
  675. return true;
  676. /* check if the destination client was served by this node and it is now
  677. * roaming. In this case, it means that the node has got a ROAM_ADV
  678. * message and that it knows the new destination in the mesh to re-route
  679. * the packet to
  680. */
  681. if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
  682. if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
  683. ethhdr->h_dest, vid))
  684. batadv_dbg_ratelimited(BATADV_DBG_TT,
  685. bat_priv,
  686. "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
  687. unicast_packet->dest,
  688. ethhdr->h_dest);
  689. /* at this point the mesh destination should have been
  690. * substituted with the originator address found in the global
  691. * table. If not, let the packet go untouched anyway because
  692. * there is nothing the node can do
  693. */
  694. return true;
  695. }
  696. /* retrieve the TTVN known by this node for the packet destination. This
  697. * value is used later to check if the node which sent (or re-routed
  698. * last time) the packet had an updated information or not
  699. */
  700. curr_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
  701. if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  702. orig_node = batadv_orig_hash_find(bat_priv,
  703. unicast_packet->dest);
  704. /* if it is not possible to find the orig_node representing the
  705. * destination, the packet can immediately be dropped as it will
  706. * not be possible to deliver it
  707. */
  708. if (!orig_node)
  709. return false;
  710. curr_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
  711. batadv_orig_node_put(orig_node);
  712. }
  713. /* check if the TTVN contained in the packet is fresher than what the
  714. * node knows
  715. */
  716. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  717. if (!is_old_ttvn)
  718. return true;
  719. old_ttvn = unicast_packet->ttvn;
  720. /* the packet was forged based on outdated network information. Its
  721. * destination can possibly be updated and forwarded towards the new
  722. * target host
  723. */
  724. if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
  725. ethhdr->h_dest, vid)) {
  726. batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
  727. "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
  728. unicast_packet->dest, ethhdr->h_dest,
  729. old_ttvn, curr_ttvn);
  730. return true;
  731. }
  732. /* the packet has not been re-routed: either the destination is
  733. * currently served by this node or there is no destination at all and
  734. * it is possible to drop the packet
  735. */
  736. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
  737. return false;
  738. /* update the header in order to let the packet be delivered to this
  739. * node's soft interface
  740. */
  741. primary_if = batadv_primary_if_get_selected(bat_priv);
  742. if (!primary_if)
  743. return false;
  744. /* update the packet header */
  745. skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
  746. ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
  747. unicast_packet->ttvn = curr_ttvn;
  748. skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
  749. batadv_hardif_put(primary_if);
  750. return true;
  751. }
  752. /**
  753. * batadv_recv_unhandled_unicast_packet() - receive and process packets which
  754. * are in the unicast number space but not yet known to the implementation
  755. * @skb: unicast tvlv packet to process
  756. * @recv_if: pointer to interface this packet was received on
  757. *
  758. * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  759. * otherwise.
  760. */
  761. int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
  762. struct batadv_hard_iface *recv_if)
  763. {
  764. struct batadv_unicast_packet *unicast_packet;
  765. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  766. int check, hdr_size = sizeof(*unicast_packet);
  767. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  768. if (check < 0)
  769. goto free_skb;
  770. /* we don't know about this type, drop it. */
  771. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  772. if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
  773. goto free_skb;
  774. return batadv_route_unicast_packet(skb, recv_if);
  775. free_skb:
  776. kfree_skb(skb);
  777. return NET_RX_DROP;
  778. }
  779. /**
  780. * batadv_recv_unicast_packet() - Process incoming unicast packet
  781. * @skb: incoming packet buffer
  782. * @recv_if: incoming hard interface
  783. *
  784. * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
  785. */
  786. int batadv_recv_unicast_packet(struct sk_buff *skb,
  787. struct batadv_hard_iface *recv_if)
  788. {
  789. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  790. struct batadv_unicast_packet *unicast_packet;
  791. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  792. u8 *orig_addr, *orig_addr_gw;
  793. struct batadv_orig_node *orig_node = NULL, *orig_node_gw = NULL;
  794. int check, hdr_size = sizeof(*unicast_packet);
  795. enum batadv_subtype subtype;
  796. int ret = NET_RX_DROP;
  797. bool is4addr, is_gw;
  798. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  799. is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
  800. /* the caller function should have already pulled 2 bytes */
  801. if (is4addr)
  802. hdr_size = sizeof(*unicast_4addr_packet);
  803. /* function returns -EREMOTE for promiscuous packets */
  804. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  805. /* Even though the packet is not for us, we might save it to use for
  806. * decoding a later received coded packet
  807. */
  808. if (check == -EREMOTE)
  809. batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
  810. if (check < 0)
  811. goto free_skb;
  812. if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
  813. goto free_skb;
  814. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  815. /* packet for me */
  816. if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  817. /* If this is a unicast packet from another backgone gw,
  818. * drop it.
  819. */
  820. orig_addr_gw = eth_hdr(skb)->h_source;
  821. orig_node_gw = batadv_orig_hash_find(bat_priv, orig_addr_gw);
  822. if (orig_node_gw) {
  823. is_gw = batadv_bla_is_backbone_gw(skb, orig_node_gw,
  824. hdr_size);
  825. batadv_orig_node_put(orig_node_gw);
  826. if (is_gw) {
  827. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  828. "%s(): Dropped unicast pkt received from another backbone gw %pM.\n",
  829. __func__, orig_addr_gw);
  830. goto free_skb;
  831. }
  832. }
  833. if (is4addr) {
  834. unicast_4addr_packet =
  835. (struct batadv_unicast_4addr_packet *)skb->data;
  836. subtype = unicast_4addr_packet->subtype;
  837. batadv_dat_inc_counter(bat_priv, subtype);
  838. /* Only payload data should be considered for speedy
  839. * join. For example, DAT also uses unicast 4addr
  840. * types, but those packets should not be considered
  841. * for speedy join, since the clients do not actually
  842. * reside at the sending originator.
  843. */
  844. if (subtype == BATADV_P_DATA) {
  845. orig_addr = unicast_4addr_packet->src;
  846. orig_node = batadv_orig_hash_find(bat_priv,
  847. orig_addr);
  848. }
  849. }
  850. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
  851. hdr_size))
  852. goto rx_success;
  853. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
  854. hdr_size))
  855. goto rx_success;
  856. batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
  857. batadv_interface_rx(recv_if->soft_iface, skb, hdr_size,
  858. orig_node);
  859. rx_success:
  860. batadv_orig_node_put(orig_node);
  861. return NET_RX_SUCCESS;
  862. }
  863. ret = batadv_route_unicast_packet(skb, recv_if);
  864. /* skb was consumed */
  865. skb = NULL;
  866. free_skb:
  867. kfree_skb(skb);
  868. return ret;
  869. }
  870. /**
  871. * batadv_recv_unicast_tvlv() - receive and process unicast tvlv packets
  872. * @skb: unicast tvlv packet to process
  873. * @recv_if: pointer to interface this packet was received on
  874. *
  875. * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  876. * otherwise.
  877. */
  878. int batadv_recv_unicast_tvlv(struct sk_buff *skb,
  879. struct batadv_hard_iface *recv_if)
  880. {
  881. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  882. struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
  883. unsigned char *tvlv_buff;
  884. u16 tvlv_buff_len;
  885. int hdr_size = sizeof(*unicast_tvlv_packet);
  886. int ret = NET_RX_DROP;
  887. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  888. goto free_skb;
  889. /* the header is likely to be modified while forwarding */
  890. if (skb_cow(skb, hdr_size) < 0)
  891. goto free_skb;
  892. /* packet needs to be linearized to access the tvlv content */
  893. if (skb_linearize(skb) < 0)
  894. goto free_skb;
  895. unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
  896. tvlv_buff = (unsigned char *)(skb->data + hdr_size);
  897. tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
  898. if (tvlv_buff_len > skb->len - hdr_size)
  899. goto free_skb;
  900. ret = batadv_tvlv_containers_process(bat_priv, BATADV_UNICAST_TVLV,
  901. NULL, skb, tvlv_buff,
  902. tvlv_buff_len);
  903. if (ret != NET_RX_SUCCESS) {
  904. ret = batadv_route_unicast_packet(skb, recv_if);
  905. /* skb was consumed */
  906. skb = NULL;
  907. }
  908. free_skb:
  909. kfree_skb(skb);
  910. return ret;
  911. }
  912. /**
  913. * batadv_recv_frag_packet() - process received fragment
  914. * @skb: the received fragment
  915. * @recv_if: interface that the skb is received on
  916. *
  917. * This function does one of the three following things: 1) Forward fragment, if
  918. * the assembled packet will exceed our MTU; 2) Buffer fragment, if we still
  919. * lack further fragments; 3) Merge fragments, if we have all needed parts.
  920. *
  921. * Return: NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
  922. */
  923. int batadv_recv_frag_packet(struct sk_buff *skb,
  924. struct batadv_hard_iface *recv_if)
  925. {
  926. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  927. struct batadv_orig_node *orig_node_src = NULL;
  928. struct batadv_frag_packet *frag_packet;
  929. int ret = NET_RX_DROP;
  930. if (batadv_check_unicast_packet(bat_priv, skb,
  931. sizeof(*frag_packet)) < 0)
  932. goto free_skb;
  933. frag_packet = (struct batadv_frag_packet *)skb->data;
  934. orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
  935. if (!orig_node_src)
  936. goto free_skb;
  937. skb->priority = frag_packet->priority + 256;
  938. /* Route the fragment if it is not for us and too big to be merged. */
  939. if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
  940. batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
  941. /* skb was consumed */
  942. skb = NULL;
  943. ret = NET_RX_SUCCESS;
  944. goto put_orig_node;
  945. }
  946. batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
  947. batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
  948. /* Add fragment to buffer and merge if possible. */
  949. if (!batadv_frag_skb_buffer(&skb, orig_node_src))
  950. goto put_orig_node;
  951. /* Deliver merged packet to the appropriate handler, if it was
  952. * merged
  953. */
  954. if (skb) {
  955. batadv_batman_skb_recv(skb, recv_if->net_dev,
  956. &recv_if->batman_adv_ptype, NULL);
  957. /* skb was consumed */
  958. skb = NULL;
  959. }
  960. ret = NET_RX_SUCCESS;
  961. put_orig_node:
  962. batadv_orig_node_put(orig_node_src);
  963. free_skb:
  964. kfree_skb(skb);
  965. return ret;
  966. }
  967. /**
  968. * batadv_recv_bcast_packet() - Process incoming broadcast packet
  969. * @skb: incoming packet buffer
  970. * @recv_if: incoming hard interface
  971. *
  972. * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
  973. */
  974. int batadv_recv_bcast_packet(struct sk_buff *skb,
  975. struct batadv_hard_iface *recv_if)
  976. {
  977. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  978. struct batadv_orig_node *orig_node = NULL;
  979. struct batadv_bcast_packet *bcast_packet;
  980. struct ethhdr *ethhdr;
  981. int hdr_size = sizeof(*bcast_packet);
  982. s32 seq_diff;
  983. u32 seqno;
  984. int ret;
  985. /* drop packet if it has not necessary minimum size */
  986. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  987. goto free_skb;
  988. ethhdr = eth_hdr(skb);
  989. /* packet with broadcast indication but unicast recipient */
  990. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  991. goto free_skb;
  992. /* packet with broadcast/multicast sender address */
  993. if (is_multicast_ether_addr(ethhdr->h_source))
  994. goto free_skb;
  995. /* ignore broadcasts sent by myself */
  996. if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
  997. goto free_skb;
  998. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  999. /* ignore broadcasts originated by myself */
  1000. if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
  1001. goto free_skb;
  1002. if (bcast_packet->ttl-- < 2)
  1003. goto free_skb;
  1004. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  1005. if (!orig_node)
  1006. goto free_skb;
  1007. spin_lock_bh(&orig_node->bcast_seqno_lock);
  1008. seqno = ntohl(bcast_packet->seqno);
  1009. /* check whether the packet is a duplicate */
  1010. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  1011. seqno))
  1012. goto spin_unlock;
  1013. seq_diff = seqno - orig_node->last_bcast_seqno;
  1014. /* check whether the packet is old and the host just restarted. */
  1015. if (batadv_window_protected(bat_priv, seq_diff,
  1016. BATADV_BCAST_MAX_AGE,
  1017. &orig_node->bcast_seqno_reset, NULL))
  1018. goto spin_unlock;
  1019. /* mark broadcast in flood history, update window position
  1020. * if required.
  1021. */
  1022. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  1023. orig_node->last_bcast_seqno = seqno;
  1024. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1025. /* check whether this has been sent by another originator before */
  1026. if (batadv_bla_check_bcast_duplist(bat_priv, skb))
  1027. goto free_skb;
  1028. batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
  1029. /* rebroadcast packet */
  1030. ret = batadv_forw_bcast_packet(bat_priv, skb, 0, false);
  1031. if (ret == NETDEV_TX_BUSY)
  1032. goto free_skb;
  1033. /* don't hand the broadcast up if it is from an originator
  1034. * from the same backbone.
  1035. */
  1036. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  1037. goto free_skb;
  1038. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
  1039. goto rx_success;
  1040. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
  1041. goto rx_success;
  1042. batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
  1043. /* broadcast for me */
  1044. batadv_interface_rx(recv_if->soft_iface, skb, hdr_size, orig_node);
  1045. rx_success:
  1046. ret = NET_RX_SUCCESS;
  1047. goto out;
  1048. spin_unlock:
  1049. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1050. free_skb:
  1051. kfree_skb(skb);
  1052. ret = NET_RX_DROP;
  1053. out:
  1054. batadv_orig_node_put(orig_node);
  1055. return ret;
  1056. }
  1057. #ifdef CONFIG_BATMAN_ADV_MCAST
  1058. /**
  1059. * batadv_recv_mcast_packet() - process received batman-adv multicast packet
  1060. * @skb: the received batman-adv multicast packet
  1061. * @recv_if: interface that the skb is received on
  1062. *
  1063. * Parses the given, received batman-adv multicast packet. Depending on the
  1064. * contents of its TVLV forwards it and/or decapsulates it to hand it to the
  1065. * soft interface.
  1066. *
  1067. * Return: NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
  1068. */
  1069. int batadv_recv_mcast_packet(struct sk_buff *skb,
  1070. struct batadv_hard_iface *recv_if)
  1071. {
  1072. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1073. struct batadv_mcast_packet *mcast_packet;
  1074. int hdr_size = sizeof(*mcast_packet);
  1075. unsigned char *tvlv_buff;
  1076. int ret = NET_RX_DROP;
  1077. u16 tvlv_buff_len;
  1078. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  1079. goto free_skb;
  1080. /* create a copy of the skb, if needed, to modify it. */
  1081. if (skb_cow(skb, ETH_HLEN) < 0)
  1082. goto free_skb;
  1083. /* packet needs to be linearized to access the tvlv content */
  1084. if (skb_linearize(skb) < 0)
  1085. goto free_skb;
  1086. mcast_packet = (struct batadv_mcast_packet *)skb->data;
  1087. if (mcast_packet->ttl-- < 2)
  1088. goto free_skb;
  1089. tvlv_buff = (unsigned char *)(skb->data + hdr_size);
  1090. tvlv_buff_len = ntohs(mcast_packet->tvlv_len);
  1091. if (tvlv_buff_len > skb->len - hdr_size)
  1092. goto free_skb;
  1093. ret = batadv_tvlv_containers_process(bat_priv, BATADV_MCAST, NULL, skb,
  1094. tvlv_buff, tvlv_buff_len);
  1095. if (ret >= 0) {
  1096. batadv_inc_counter(bat_priv, BATADV_CNT_MCAST_RX);
  1097. batadv_add_counter(bat_priv, BATADV_CNT_MCAST_RX_BYTES,
  1098. skb->len + ETH_HLEN);
  1099. }
  1100. hdr_size += tvlv_buff_len;
  1101. if (ret == NET_RX_SUCCESS && (skb->len - hdr_size >= ETH_HLEN)) {
  1102. batadv_inc_counter(bat_priv, BATADV_CNT_MCAST_RX_LOCAL);
  1103. batadv_add_counter(bat_priv, BATADV_CNT_MCAST_RX_LOCAL_BYTES,
  1104. skb->len - hdr_size);
  1105. batadv_interface_rx(bat_priv->soft_iface, skb, hdr_size, NULL);
  1106. /* skb was consumed */
  1107. skb = NULL;
  1108. }
  1109. free_skb:
  1110. kfree_skb(skb);
  1111. return ret;
  1112. }
  1113. #endif /* CONFIG_BATMAN_ADV_MCAST */