routing.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "routing.h"
  21. #include "send.h"
  22. #include "soft-interface.h"
  23. #include "hard-interface.h"
  24. #include "icmp_socket.h"
  25. #include "translation-table.h"
  26. #include "originator.h"
  27. #include "vis.h"
  28. #include "unicast.h"
  29. #include "bridge_loop_avoidance.h"
  30. #include "distributed-arp-table.h"
  31. #include "network-coding.h"
  32. static int batadv_route_unicast_packet(struct sk_buff *skb,
  33. struct batadv_hard_iface *recv_if);
  34. void batadv_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
  35. {
  36. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  37. struct batadv_hashtable *hash = bat_priv->orig_hash;
  38. struct hlist_head *head;
  39. struct batadv_orig_node *orig_node;
  40. unsigned long *word;
  41. uint32_t i;
  42. size_t word_index;
  43. uint8_t *w;
  44. for (i = 0; i < hash->size; i++) {
  45. head = &hash->table[i];
  46. rcu_read_lock();
  47. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  48. spin_lock_bh(&orig_node->ogm_cnt_lock);
  49. word_index = hard_iface->if_num * BATADV_NUM_WORDS;
  50. word = &(orig_node->bcast_own[word_index]);
  51. batadv_bit_get_packet(bat_priv, word, 1, 0);
  52. w = &orig_node->bcast_own_sum[hard_iface->if_num];
  53. *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
  54. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  55. }
  56. rcu_read_unlock();
  57. }
  58. }
  59. static void _batadv_update_route(struct batadv_priv *bat_priv,
  60. struct batadv_orig_node *orig_node,
  61. struct batadv_neigh_node *neigh_node)
  62. {
  63. struct batadv_neigh_node *curr_router;
  64. curr_router = batadv_orig_node_get_router(orig_node);
  65. /* route deleted */
  66. if ((curr_router) && (!neigh_node)) {
  67. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  68. "Deleting route towards: %pM\n", orig_node->orig);
  69. batadv_tt_global_del_orig(bat_priv, orig_node,
  70. "Deleted route towards originator");
  71. /* route added */
  72. } else if ((!curr_router) && (neigh_node)) {
  73. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  74. "Adding route towards: %pM (via %pM)\n",
  75. orig_node->orig, neigh_node->addr);
  76. /* route changed */
  77. } else if (neigh_node && curr_router) {
  78. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  79. "Changing route towards: %pM (now via %pM - was via %pM)\n",
  80. orig_node->orig, neigh_node->addr,
  81. curr_router->addr);
  82. }
  83. if (curr_router)
  84. batadv_neigh_node_free_ref(curr_router);
  85. /* increase refcount of new best neighbor */
  86. if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
  87. neigh_node = NULL;
  88. spin_lock_bh(&orig_node->neigh_list_lock);
  89. rcu_assign_pointer(orig_node->router, neigh_node);
  90. spin_unlock_bh(&orig_node->neigh_list_lock);
  91. /* decrease refcount of previous best neighbor */
  92. if (curr_router)
  93. batadv_neigh_node_free_ref(curr_router);
  94. }
  95. void batadv_update_route(struct batadv_priv *bat_priv,
  96. struct batadv_orig_node *orig_node,
  97. struct batadv_neigh_node *neigh_node)
  98. {
  99. struct batadv_neigh_node *router = NULL;
  100. if (!orig_node)
  101. goto out;
  102. router = batadv_orig_node_get_router(orig_node);
  103. if (router != neigh_node)
  104. _batadv_update_route(bat_priv, orig_node, neigh_node);
  105. out:
  106. if (router)
  107. batadv_neigh_node_free_ref(router);
  108. }
  109. /* caller must hold the neigh_list_lock */
  110. void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
  111. struct batadv_neigh_node *neigh_node)
  112. {
  113. /* this neighbor is not part of our candidate list */
  114. if (list_empty(&neigh_node->bonding_list))
  115. goto out;
  116. list_del_rcu(&neigh_node->bonding_list);
  117. INIT_LIST_HEAD(&neigh_node->bonding_list);
  118. batadv_neigh_node_free_ref(neigh_node);
  119. atomic_dec(&orig_node->bond_candidates);
  120. out:
  121. return;
  122. }
  123. void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
  124. struct batadv_neigh_node *neigh_node)
  125. {
  126. struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
  127. uint8_t interference_candidate = 0;
  128. spin_lock_bh(&orig_node->neigh_list_lock);
  129. /* only consider if it has the same primary address ... */
  130. if (!batadv_compare_eth(orig_node->orig,
  131. neigh_node->orig_node->primary_addr))
  132. goto candidate_del;
  133. router = batadv_orig_node_get_router(orig_node);
  134. if (!router)
  135. goto candidate_del;
  136. /* ... and is good enough to be considered */
  137. if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
  138. goto candidate_del;
  139. /* check if we have another candidate with the same mac address or
  140. * interface. If we do, we won't select this candidate because of
  141. * possible interference.
  142. */
  143. hlist_for_each_entry_rcu(tmp_neigh_node,
  144. &orig_node->neigh_list, list) {
  145. if (tmp_neigh_node == neigh_node)
  146. continue;
  147. /* we only care if the other candidate is even
  148. * considered as candidate.
  149. */
  150. if (list_empty(&tmp_neigh_node->bonding_list))
  151. continue;
  152. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  153. (batadv_compare_eth(neigh_node->addr,
  154. tmp_neigh_node->addr))) {
  155. interference_candidate = 1;
  156. break;
  157. }
  158. }
  159. /* don't care further if it is an interference candidate */
  160. if (interference_candidate)
  161. goto candidate_del;
  162. /* this neighbor already is part of our candidate list */
  163. if (!list_empty(&neigh_node->bonding_list))
  164. goto out;
  165. if (!atomic_inc_not_zero(&neigh_node->refcount))
  166. goto out;
  167. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  168. atomic_inc(&orig_node->bond_candidates);
  169. goto out;
  170. candidate_del:
  171. batadv_bonding_candidate_del(orig_node, neigh_node);
  172. out:
  173. spin_unlock_bh(&orig_node->neigh_list_lock);
  174. if (router)
  175. batadv_neigh_node_free_ref(router);
  176. }
  177. /* copy primary address for bonding */
  178. void
  179. batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
  180. struct batadv_orig_node *orig_neigh_node,
  181. const struct batadv_ogm_packet *batman_ogm_packet)
  182. {
  183. if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
  184. return;
  185. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  186. }
  187. /* checks whether the host restarted and is in the protection time.
  188. * returns:
  189. * 0 if the packet is to be accepted
  190. * 1 if the packet is to be ignored.
  191. */
  192. int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
  193. unsigned long *last_reset)
  194. {
  195. if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
  196. seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
  197. if (!batadv_has_timed_out(*last_reset,
  198. BATADV_RESET_PROTECTION_MS))
  199. return 1;
  200. *last_reset = jiffies;
  201. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  202. "old packet received, start protection\n");
  203. }
  204. return 0;
  205. }
  206. bool batadv_check_management_packet(struct sk_buff *skb,
  207. struct batadv_hard_iface *hard_iface,
  208. int header_len)
  209. {
  210. struct ethhdr *ethhdr;
  211. /* drop packet if it has not necessary minimum size */
  212. if (unlikely(!pskb_may_pull(skb, header_len)))
  213. return false;
  214. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  215. /* packet with broadcast indication but unicast recipient */
  216. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  217. return false;
  218. /* packet with broadcast sender address */
  219. if (is_broadcast_ether_addr(ethhdr->h_source))
  220. return false;
  221. /* create a copy of the skb, if needed, to modify it. */
  222. if (skb_cow(skb, 0) < 0)
  223. return false;
  224. /* keep skb linear */
  225. if (skb_linearize(skb) < 0)
  226. return false;
  227. return true;
  228. }
  229. static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
  230. struct sk_buff *skb, size_t icmp_len)
  231. {
  232. struct batadv_hard_iface *primary_if = NULL;
  233. struct batadv_orig_node *orig_node = NULL;
  234. struct batadv_icmp_packet_rr *icmp_packet;
  235. int ret = NET_RX_DROP;
  236. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  237. /* add data to device queue */
  238. if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
  239. batadv_socket_receive_packet(icmp_packet, icmp_len);
  240. goto out;
  241. }
  242. primary_if = batadv_primary_if_get_selected(bat_priv);
  243. if (!primary_if)
  244. goto out;
  245. /* answer echo request (ping) */
  246. /* get routing information */
  247. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  248. if (!orig_node)
  249. goto out;
  250. /* create a copy of the skb, if needed, to modify it. */
  251. if (skb_cow(skb, ETH_HLEN) < 0)
  252. goto out;
  253. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  254. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  255. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  256. icmp_packet->msg_type = BATADV_ECHO_REPLY;
  257. icmp_packet->header.ttl = BATADV_TTL;
  258. if (batadv_send_skb_to_orig(skb, orig_node, NULL))
  259. ret = NET_RX_SUCCESS;
  260. out:
  261. if (primary_if)
  262. batadv_hardif_free_ref(primary_if);
  263. if (orig_node)
  264. batadv_orig_node_free_ref(orig_node);
  265. return ret;
  266. }
  267. static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
  268. struct sk_buff *skb)
  269. {
  270. struct batadv_hard_iface *primary_if = NULL;
  271. struct batadv_orig_node *orig_node = NULL;
  272. struct batadv_icmp_packet *icmp_packet;
  273. int ret = NET_RX_DROP;
  274. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  275. /* send TTL exceeded if packet is an echo request (traceroute) */
  276. if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
  277. pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
  278. icmp_packet->orig, icmp_packet->dst);
  279. goto out;
  280. }
  281. primary_if = batadv_primary_if_get_selected(bat_priv);
  282. if (!primary_if)
  283. goto out;
  284. /* get routing information */
  285. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  286. if (!orig_node)
  287. goto out;
  288. /* create a copy of the skb, if needed, to modify it. */
  289. if (skb_cow(skb, ETH_HLEN) < 0)
  290. goto out;
  291. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  292. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  293. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  294. icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
  295. icmp_packet->header.ttl = BATADV_TTL;
  296. if (batadv_send_skb_to_orig(skb, orig_node, NULL))
  297. ret = NET_RX_SUCCESS;
  298. out:
  299. if (primary_if)
  300. batadv_hardif_free_ref(primary_if);
  301. if (orig_node)
  302. batadv_orig_node_free_ref(orig_node);
  303. return ret;
  304. }
  305. int batadv_recv_icmp_packet(struct sk_buff *skb,
  306. struct batadv_hard_iface *recv_if)
  307. {
  308. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  309. struct batadv_icmp_packet_rr *icmp_packet;
  310. struct ethhdr *ethhdr;
  311. struct batadv_orig_node *orig_node = NULL;
  312. int hdr_size = sizeof(struct batadv_icmp_packet);
  313. int ret = NET_RX_DROP;
  314. /* we truncate all incoming icmp packets if they don't match our size */
  315. if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
  316. hdr_size = sizeof(struct batadv_icmp_packet_rr);
  317. /* drop packet if it has not necessary minimum size */
  318. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  319. goto out;
  320. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  321. /* packet with unicast indication but broadcast recipient */
  322. if (is_broadcast_ether_addr(ethhdr->h_dest))
  323. goto out;
  324. /* packet with broadcast sender address */
  325. if (is_broadcast_ether_addr(ethhdr->h_source))
  326. goto out;
  327. /* not for me */
  328. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  329. goto out;
  330. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  331. /* add record route information if not full */
  332. if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
  333. (icmp_packet->rr_cur < BATADV_RR_LEN)) {
  334. memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
  335. ethhdr->h_dest, ETH_ALEN);
  336. icmp_packet->rr_cur++;
  337. }
  338. /* packet for me */
  339. if (batadv_is_my_mac(bat_priv, icmp_packet->dst))
  340. return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
  341. /* TTL exceeded */
  342. if (icmp_packet->header.ttl < 2)
  343. return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
  344. /* get routing information */
  345. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
  346. if (!orig_node)
  347. goto out;
  348. /* create a copy of the skb, if needed, to modify it. */
  349. if (skb_cow(skb, ETH_HLEN) < 0)
  350. goto out;
  351. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  352. /* decrement ttl */
  353. icmp_packet->header.ttl--;
  354. /* route it */
  355. if (batadv_send_skb_to_orig(skb, orig_node, recv_if))
  356. ret = NET_RX_SUCCESS;
  357. out:
  358. if (orig_node)
  359. batadv_orig_node_free_ref(orig_node);
  360. return ret;
  361. }
  362. /* In the bonding case, send the packets in a round
  363. * robin fashion over the remaining interfaces.
  364. *
  365. * This method rotates the bonding list and increases the
  366. * returned router's refcount.
  367. */
  368. static struct batadv_neigh_node *
  369. batadv_find_bond_router(struct batadv_orig_node *primary_orig,
  370. const struct batadv_hard_iface *recv_if)
  371. {
  372. struct batadv_neigh_node *tmp_neigh_node;
  373. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  374. rcu_read_lock();
  375. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  376. bonding_list) {
  377. if (!first_candidate)
  378. first_candidate = tmp_neigh_node;
  379. /* recv_if == NULL on the first node. */
  380. if (tmp_neigh_node->if_incoming == recv_if)
  381. continue;
  382. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  383. continue;
  384. router = tmp_neigh_node;
  385. break;
  386. }
  387. /* use the first candidate if nothing was found. */
  388. if (!router && first_candidate &&
  389. atomic_inc_not_zero(&first_candidate->refcount))
  390. router = first_candidate;
  391. if (!router)
  392. goto out;
  393. /* selected should point to the next element
  394. * after the current router
  395. */
  396. spin_lock_bh(&primary_orig->neigh_list_lock);
  397. /* this is a list_move(), which unfortunately
  398. * does not exist as rcu version
  399. */
  400. list_del_rcu(&primary_orig->bond_list);
  401. list_add_rcu(&primary_orig->bond_list,
  402. &router->bonding_list);
  403. spin_unlock_bh(&primary_orig->neigh_list_lock);
  404. out:
  405. rcu_read_unlock();
  406. return router;
  407. }
  408. /* Interface Alternating: Use the best of the
  409. * remaining candidates which are not using
  410. * this interface.
  411. *
  412. * Increases the returned router's refcount
  413. */
  414. static struct batadv_neigh_node *
  415. batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
  416. const struct batadv_hard_iface *recv_if)
  417. {
  418. struct batadv_neigh_node *tmp_neigh_node;
  419. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  420. rcu_read_lock();
  421. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  422. bonding_list) {
  423. if (!first_candidate)
  424. first_candidate = tmp_neigh_node;
  425. /* recv_if == NULL on the first node. */
  426. if (tmp_neigh_node->if_incoming == recv_if)
  427. continue;
  428. if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
  429. continue;
  430. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  431. continue;
  432. /* decrement refcount of previously selected router */
  433. if (router)
  434. batadv_neigh_node_free_ref(router);
  435. /* we found a better router (or at least one valid router) */
  436. router = tmp_neigh_node;
  437. }
  438. /* use the first candidate if nothing was found. */
  439. if (!router && first_candidate &&
  440. atomic_inc_not_zero(&first_candidate->refcount))
  441. router = first_candidate;
  442. rcu_read_unlock();
  443. return router;
  444. }
  445. /**
  446. * batadv_check_unicast_packet - Check for malformed unicast packets
  447. * @bat_priv: the bat priv with all the soft interface information
  448. * @skb: packet to check
  449. * @hdr_size: size of header to pull
  450. *
  451. * Check for short header and bad addresses in given packet. Returns negative
  452. * value when check fails and 0 otherwise. The negative value depends on the
  453. * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
  454. * and -EREMOTE for non-local (other host) destination.
  455. */
  456. static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
  457. struct sk_buff *skb, int hdr_size)
  458. {
  459. struct ethhdr *ethhdr;
  460. /* drop packet if it has not necessary minimum size */
  461. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  462. return -ENODATA;
  463. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  464. /* packet with unicast indication but broadcast recipient */
  465. if (is_broadcast_ether_addr(ethhdr->h_dest))
  466. return -EBADR;
  467. /* packet with broadcast sender address */
  468. if (is_broadcast_ether_addr(ethhdr->h_source))
  469. return -EBADR;
  470. /* not for me */
  471. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  472. return -EREMOTE;
  473. return 0;
  474. }
  475. int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  476. {
  477. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  478. struct batadv_tt_query_packet *tt_query;
  479. uint16_t tt_size;
  480. int hdr_size = sizeof(*tt_query);
  481. char tt_flag;
  482. size_t packet_size;
  483. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  484. return NET_RX_DROP;
  485. /* I could need to modify it */
  486. if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
  487. goto out;
  488. tt_query = (struct batadv_tt_query_packet *)skb->data;
  489. switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
  490. case BATADV_TT_REQUEST:
  491. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
  492. /* If we cannot provide an answer the tt_request is
  493. * forwarded
  494. */
  495. if (!batadv_send_tt_response(bat_priv, tt_query)) {
  496. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  497. tt_flag = 'F';
  498. else
  499. tt_flag = '.';
  500. batadv_dbg(BATADV_DBG_TT, bat_priv,
  501. "Routing TT_REQUEST to %pM [%c]\n",
  502. tt_query->dst,
  503. tt_flag);
  504. return batadv_route_unicast_packet(skb, recv_if);
  505. }
  506. break;
  507. case BATADV_TT_RESPONSE:
  508. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
  509. if (batadv_is_my_mac(bat_priv, tt_query->dst)) {
  510. /* packet needs to be linearized to access the TT
  511. * changes
  512. */
  513. if (skb_linearize(skb) < 0)
  514. goto out;
  515. /* skb_linearize() possibly changed skb->data */
  516. tt_query = (struct batadv_tt_query_packet *)skb->data;
  517. tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
  518. /* Ensure we have all the claimed data */
  519. packet_size = sizeof(struct batadv_tt_query_packet);
  520. packet_size += tt_size;
  521. if (unlikely(skb_headlen(skb) < packet_size))
  522. goto out;
  523. batadv_handle_tt_response(bat_priv, tt_query);
  524. } else {
  525. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  526. tt_flag = 'F';
  527. else
  528. tt_flag = '.';
  529. batadv_dbg(BATADV_DBG_TT, bat_priv,
  530. "Routing TT_RESPONSE to %pM [%c]\n",
  531. tt_query->dst,
  532. tt_flag);
  533. return batadv_route_unicast_packet(skb, recv_if);
  534. }
  535. break;
  536. }
  537. out:
  538. /* returning NET_RX_DROP will make the caller function kfree the skb */
  539. return NET_RX_DROP;
  540. }
  541. int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  542. {
  543. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  544. struct batadv_roam_adv_packet *roam_adv_packet;
  545. struct batadv_orig_node *orig_node;
  546. if (batadv_check_unicast_packet(bat_priv, skb,
  547. sizeof(*roam_adv_packet)) < 0)
  548. goto out;
  549. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
  550. roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
  551. if (!batadv_is_my_mac(bat_priv, roam_adv_packet->dst))
  552. return batadv_route_unicast_packet(skb, recv_if);
  553. /* check if it is a backbone gateway. we don't accept
  554. * roaming advertisement from it, as it has the same
  555. * entries as we have.
  556. */
  557. if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
  558. goto out;
  559. orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
  560. if (!orig_node)
  561. goto out;
  562. batadv_dbg(BATADV_DBG_TT, bat_priv,
  563. "Received ROAMING_ADV from %pM (client %pM)\n",
  564. roam_adv_packet->src, roam_adv_packet->client);
  565. batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  566. BATADV_TT_CLIENT_ROAM,
  567. atomic_read(&orig_node->last_ttvn) + 1);
  568. batadv_orig_node_free_ref(orig_node);
  569. out:
  570. /* returning NET_RX_DROP will make the caller function kfree the skb */
  571. return NET_RX_DROP;
  572. }
  573. /* find a suitable router for this originator, and use
  574. * bonding if possible. increases the found neighbors
  575. * refcount.
  576. */
  577. struct batadv_neigh_node *
  578. batadv_find_router(struct batadv_priv *bat_priv,
  579. struct batadv_orig_node *orig_node,
  580. const struct batadv_hard_iface *recv_if)
  581. {
  582. struct batadv_orig_node *primary_orig_node;
  583. struct batadv_orig_node *router_orig;
  584. struct batadv_neigh_node *router;
  585. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  586. int bonding_enabled;
  587. uint8_t *primary_addr;
  588. if (!orig_node)
  589. return NULL;
  590. router = batadv_orig_node_get_router(orig_node);
  591. if (!router)
  592. goto err;
  593. /* without bonding, the first node should
  594. * always choose the default router.
  595. */
  596. bonding_enabled = atomic_read(&bat_priv->bonding);
  597. rcu_read_lock();
  598. /* select default router to output */
  599. router_orig = router->orig_node;
  600. if (!router_orig)
  601. goto err_unlock;
  602. if ((!recv_if) && (!bonding_enabled))
  603. goto return_router;
  604. primary_addr = router_orig->primary_addr;
  605. /* if we have something in the primary_addr, we can search
  606. * for a potential bonding candidate.
  607. */
  608. if (batadv_compare_eth(primary_addr, zero_mac))
  609. goto return_router;
  610. /* find the orig_node which has the primary interface. might
  611. * even be the same as our router_orig in many cases
  612. */
  613. if (batadv_compare_eth(primary_addr, router_orig->orig)) {
  614. primary_orig_node = router_orig;
  615. } else {
  616. primary_orig_node = batadv_orig_hash_find(bat_priv,
  617. primary_addr);
  618. if (!primary_orig_node)
  619. goto return_router;
  620. batadv_orig_node_free_ref(primary_orig_node);
  621. }
  622. /* with less than 2 candidates, we can't do any
  623. * bonding and prefer the original router.
  624. */
  625. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  626. goto return_router;
  627. /* all nodes between should choose a candidate which
  628. * is is not on the interface where the packet came
  629. * in.
  630. */
  631. batadv_neigh_node_free_ref(router);
  632. if (bonding_enabled)
  633. router = batadv_find_bond_router(primary_orig_node, recv_if);
  634. else
  635. router = batadv_find_ifalter_router(primary_orig_node, recv_if);
  636. return_router:
  637. if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
  638. goto err_unlock;
  639. rcu_read_unlock();
  640. return router;
  641. err_unlock:
  642. rcu_read_unlock();
  643. err:
  644. if (router)
  645. batadv_neigh_node_free_ref(router);
  646. return NULL;
  647. }
  648. static int batadv_route_unicast_packet(struct sk_buff *skb,
  649. struct batadv_hard_iface *recv_if)
  650. {
  651. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  652. struct batadv_orig_node *orig_node = NULL;
  653. struct batadv_neigh_node *neigh_node = NULL;
  654. struct batadv_unicast_packet *unicast_packet;
  655. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  656. int ret = NET_RX_DROP;
  657. struct sk_buff *new_skb;
  658. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  659. /* TTL exceeded */
  660. if (unicast_packet->header.ttl < 2) {
  661. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  662. ethhdr->h_source, unicast_packet->dest);
  663. goto out;
  664. }
  665. /* get routing information */
  666. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  667. if (!orig_node)
  668. goto out;
  669. /* find_router() increases neigh_nodes refcount if found. */
  670. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  671. if (!neigh_node)
  672. goto out;
  673. /* create a copy of the skb, if needed, to modify it. */
  674. if (skb_cow(skb, ETH_HLEN) < 0)
  675. goto out;
  676. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  677. if (unicast_packet->header.packet_type == BATADV_UNICAST &&
  678. atomic_read(&bat_priv->fragmentation) &&
  679. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  680. ret = batadv_frag_send_skb(skb, bat_priv,
  681. neigh_node->if_incoming,
  682. neigh_node->addr);
  683. goto out;
  684. }
  685. if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
  686. batadv_frag_can_reassemble(skb,
  687. neigh_node->if_incoming->net_dev->mtu)) {
  688. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  689. if (ret == NET_RX_DROP)
  690. goto out;
  691. /* packet was buffered for late merge */
  692. if (!new_skb) {
  693. ret = NET_RX_SUCCESS;
  694. goto out;
  695. }
  696. skb = new_skb;
  697. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  698. }
  699. /* decrement ttl */
  700. unicast_packet->header.ttl--;
  701. /* network code packet if possible */
  702. if (batadv_nc_skb_forward(skb, neigh_node, ethhdr)) {
  703. ret = NET_RX_SUCCESS;
  704. } else if (batadv_send_skb_to_orig(skb, orig_node, recv_if)) {
  705. ret = NET_RX_SUCCESS;
  706. /* Update stats counter */
  707. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  708. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  709. skb->len + ETH_HLEN);
  710. }
  711. out:
  712. if (neigh_node)
  713. batadv_neigh_node_free_ref(neigh_node);
  714. if (orig_node)
  715. batadv_orig_node_free_ref(orig_node);
  716. return ret;
  717. }
  718. /**
  719. * batadv_reroute_unicast_packet - update the unicast header for re-routing
  720. * @bat_priv: the bat priv with all the soft interface information
  721. * @unicast_packet: the unicast header to be updated
  722. * @dst_addr: the payload destination
  723. *
  724. * Search the translation table for dst_addr and update the unicast header with
  725. * the new corresponding information (originator address where the destination
  726. * client currently is and its known TTVN)
  727. *
  728. * Returns true if the packet header has been updated, false otherwise
  729. */
  730. static bool
  731. batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
  732. struct batadv_unicast_packet *unicast_packet,
  733. uint8_t *dst_addr)
  734. {
  735. struct batadv_orig_node *orig_node = NULL;
  736. struct batadv_hard_iface *primary_if = NULL;
  737. bool ret = false;
  738. uint8_t *orig_addr, orig_ttvn;
  739. if (batadv_is_my_client(bat_priv, dst_addr)) {
  740. primary_if = batadv_primary_if_get_selected(bat_priv);
  741. if (!primary_if)
  742. goto out;
  743. orig_addr = primary_if->net_dev->dev_addr;
  744. orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  745. } else {
  746. orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
  747. if (!orig_node)
  748. goto out;
  749. if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
  750. goto out;
  751. orig_addr = orig_node->orig;
  752. orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  753. }
  754. /* update the packet header */
  755. memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
  756. unicast_packet->ttvn = orig_ttvn;
  757. ret = true;
  758. out:
  759. if (primary_if)
  760. batadv_hardif_free_ref(primary_if);
  761. if (orig_node)
  762. batadv_orig_node_free_ref(orig_node);
  763. return ret;
  764. }
  765. static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  766. struct sk_buff *skb, int hdr_len) {
  767. uint8_t curr_ttvn, old_ttvn;
  768. struct batadv_orig_node *orig_node;
  769. struct ethhdr *ethhdr;
  770. struct batadv_hard_iface *primary_if;
  771. struct batadv_unicast_packet *unicast_packet;
  772. int is_old_ttvn;
  773. /* check if there is enough data before accessing it */
  774. if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
  775. return 0;
  776. /* create a copy of the skb (in case of for re-routing) to modify it. */
  777. if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
  778. return 0;
  779. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  780. ethhdr = (struct ethhdr *)(skb->data + hdr_len);
  781. /* check if the destination client was served by this node and it is now
  782. * roaming. In this case, it means that the node has got a ROAM_ADV
  783. * message and that it knows the new destination in the mesh to re-route
  784. * the packet to
  785. */
  786. if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
  787. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  788. ethhdr->h_dest))
  789. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
  790. bat_priv,
  791. "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
  792. unicast_packet->dest,
  793. ethhdr->h_dest);
  794. /* at this point the mesh destination should have been
  795. * substituted with the originator address found in the global
  796. * table. If not, let the packet go untouched anyway because
  797. * there is nothing the node can do
  798. */
  799. return 1;
  800. }
  801. /* retrieve the TTVN known by this node for the packet destination. This
  802. * value is used later to check if the node which sent (or re-routed
  803. * last time) the packet had an updated information or not
  804. */
  805. curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  806. if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  807. orig_node = batadv_orig_hash_find(bat_priv,
  808. unicast_packet->dest);
  809. /* if it is not possible to find the orig_node representing the
  810. * destination, the packet can immediately be dropped as it will
  811. * not be possible to deliver it
  812. */
  813. if (!orig_node)
  814. return 0;
  815. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  816. batadv_orig_node_free_ref(orig_node);
  817. }
  818. /* check if the TTVN contained in the packet is fresher than what the
  819. * node knows
  820. */
  821. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  822. if (!is_old_ttvn)
  823. return 1;
  824. old_ttvn = unicast_packet->ttvn;
  825. /* the packet was forged based on outdated network information. Its
  826. * destination can possibly be updated and forwarded towards the new
  827. * target host
  828. */
  829. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  830. ethhdr->h_dest)) {
  831. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
  832. "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
  833. unicast_packet->dest, ethhdr->h_dest,
  834. old_ttvn, curr_ttvn);
  835. return 1;
  836. }
  837. /* the packet has not been re-routed: either the destination is
  838. * currently served by this node or there is no destination at all and
  839. * it is possible to drop the packet
  840. */
  841. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
  842. return 0;
  843. /* update the header in order to let the packet be delivered to this
  844. * node's soft interface
  845. */
  846. primary_if = batadv_primary_if_get_selected(bat_priv);
  847. if (!primary_if)
  848. return 0;
  849. memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
  850. batadv_hardif_free_ref(primary_if);
  851. unicast_packet->ttvn = curr_ttvn;
  852. return 1;
  853. }
  854. int batadv_recv_unicast_packet(struct sk_buff *skb,
  855. struct batadv_hard_iface *recv_if)
  856. {
  857. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  858. struct batadv_unicast_packet *unicast_packet;
  859. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  860. uint8_t *orig_addr;
  861. struct batadv_orig_node *orig_node = NULL;
  862. int check, hdr_size = sizeof(*unicast_packet);
  863. bool is4addr;
  864. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  865. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  866. is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
  867. /* the caller function should have already pulled 2 bytes */
  868. if (is4addr)
  869. hdr_size = sizeof(*unicast_4addr_packet);
  870. /* function returns -EREMOTE for promiscuous packets */
  871. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  872. /* Even though the packet is not for us, we might save it to use for
  873. * decoding a later received coded packet
  874. */
  875. if (check == -EREMOTE)
  876. batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
  877. if (check < 0)
  878. return NET_RX_DROP;
  879. if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
  880. return NET_RX_DROP;
  881. /* packet for me */
  882. if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  883. if (is4addr) {
  884. batadv_dat_inc_counter(bat_priv,
  885. unicast_4addr_packet->subtype);
  886. orig_addr = unicast_4addr_packet->src;
  887. orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
  888. }
  889. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
  890. hdr_size))
  891. goto rx_success;
  892. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
  893. hdr_size))
  894. goto rx_success;
  895. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  896. orig_node);
  897. rx_success:
  898. if (orig_node)
  899. batadv_orig_node_free_ref(orig_node);
  900. return NET_RX_SUCCESS;
  901. }
  902. return batadv_route_unicast_packet(skb, recv_if);
  903. }
  904. int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
  905. struct batadv_hard_iface *recv_if)
  906. {
  907. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  908. struct batadv_unicast_frag_packet *unicast_packet;
  909. int hdr_size = sizeof(*unicast_packet);
  910. struct sk_buff *new_skb = NULL;
  911. int ret;
  912. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  913. return NET_RX_DROP;
  914. if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
  915. return NET_RX_DROP;
  916. unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
  917. /* packet for me */
  918. if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  919. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  920. if (ret == NET_RX_DROP)
  921. return NET_RX_DROP;
  922. /* packet was buffered for late merge */
  923. if (!new_skb)
  924. return NET_RX_SUCCESS;
  925. if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb,
  926. hdr_size))
  927. goto rx_success;
  928. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb,
  929. hdr_size))
  930. goto rx_success;
  931. batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
  932. sizeof(struct batadv_unicast_packet), NULL);
  933. rx_success:
  934. return NET_RX_SUCCESS;
  935. }
  936. return batadv_route_unicast_packet(skb, recv_if);
  937. }
  938. int batadv_recv_bcast_packet(struct sk_buff *skb,
  939. struct batadv_hard_iface *recv_if)
  940. {
  941. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  942. struct batadv_orig_node *orig_node = NULL;
  943. struct batadv_bcast_packet *bcast_packet;
  944. struct ethhdr *ethhdr;
  945. int hdr_size = sizeof(*bcast_packet);
  946. int ret = NET_RX_DROP;
  947. int32_t seq_diff;
  948. /* drop packet if it has not necessary minimum size */
  949. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  950. goto out;
  951. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  952. /* packet with broadcast indication but unicast recipient */
  953. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  954. goto out;
  955. /* packet with broadcast sender address */
  956. if (is_broadcast_ether_addr(ethhdr->h_source))
  957. goto out;
  958. /* ignore broadcasts sent by myself */
  959. if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
  960. goto out;
  961. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  962. /* ignore broadcasts originated by myself */
  963. if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
  964. goto out;
  965. if (bcast_packet->header.ttl < 2)
  966. goto out;
  967. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  968. if (!orig_node)
  969. goto out;
  970. spin_lock_bh(&orig_node->bcast_seqno_lock);
  971. /* check whether the packet is a duplicate */
  972. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  973. ntohl(bcast_packet->seqno)))
  974. goto spin_unlock;
  975. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  976. /* check whether the packet is old and the host just restarted. */
  977. if (batadv_window_protected(bat_priv, seq_diff,
  978. &orig_node->bcast_seqno_reset))
  979. goto spin_unlock;
  980. /* mark broadcast in flood history, update window position
  981. * if required.
  982. */
  983. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  984. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  985. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  986. /* check whether this has been sent by another originator before */
  987. if (batadv_bla_check_bcast_duplist(bat_priv, skb))
  988. goto out;
  989. /* rebroadcast packet */
  990. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  991. /* don't hand the broadcast up if it is from an originator
  992. * from the same backbone.
  993. */
  994. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  995. goto out;
  996. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
  997. goto rx_success;
  998. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
  999. goto rx_success;
  1000. /* broadcast for me */
  1001. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  1002. orig_node);
  1003. rx_success:
  1004. ret = NET_RX_SUCCESS;
  1005. goto out;
  1006. spin_unlock:
  1007. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1008. out:
  1009. if (orig_node)
  1010. batadv_orig_node_free_ref(orig_node);
  1011. return ret;
  1012. }
  1013. int batadv_recv_vis_packet(struct sk_buff *skb,
  1014. struct batadv_hard_iface *recv_if)
  1015. {
  1016. struct batadv_vis_packet *vis_packet;
  1017. struct ethhdr *ethhdr;
  1018. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1019. int hdr_size = sizeof(*vis_packet);
  1020. /* keep skb linear */
  1021. if (skb_linearize(skb) < 0)
  1022. return NET_RX_DROP;
  1023. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1024. return NET_RX_DROP;
  1025. vis_packet = (struct batadv_vis_packet *)skb->data;
  1026. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1027. /* not for me */
  1028. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  1029. return NET_RX_DROP;
  1030. /* ignore own packets */
  1031. if (batadv_is_my_mac(bat_priv, vis_packet->vis_orig))
  1032. return NET_RX_DROP;
  1033. if (batadv_is_my_mac(bat_priv, vis_packet->sender_orig))
  1034. return NET_RX_DROP;
  1035. switch (vis_packet->vis_type) {
  1036. case BATADV_VIS_TYPE_SERVER_SYNC:
  1037. batadv_receive_server_sync_packet(bat_priv, vis_packet,
  1038. skb_headlen(skb));
  1039. break;
  1040. case BATADV_VIS_TYPE_CLIENT_UPDATE:
  1041. batadv_receive_client_update_packet(bat_priv, vis_packet,
  1042. skb_headlen(skb));
  1043. break;
  1044. default: /* ignore unknown packet */
  1045. break;
  1046. }
  1047. /* We take a copy of the data in the packet, so we should
  1048. * always free the skbuf.
  1049. */
  1050. return NET_RX_DROP;
  1051. }