routing.c 37 KB

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