routing.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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(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(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. * @skb: packet to check
  448. * @hdr_size: size of header to pull
  449. *
  450. * Check for short header and bad addresses in given packet. Returns negative
  451. * value when check fails and 0 otherwise. The negative value depends on the
  452. * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
  453. * and -EREMOTE for non-local (other host) destination.
  454. */
  455. static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
  456. {
  457. struct ethhdr *ethhdr;
  458. /* drop packet if it has not necessary minimum size */
  459. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  460. return -ENODATA;
  461. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  462. /* packet with unicast indication but broadcast recipient */
  463. if (is_broadcast_ether_addr(ethhdr->h_dest))
  464. return -EBADR;
  465. /* packet with broadcast sender address */
  466. if (is_broadcast_ether_addr(ethhdr->h_source))
  467. return -EBADR;
  468. /* not for me */
  469. if (!batadv_is_my_mac(ethhdr->h_dest))
  470. return -EREMOTE;
  471. return 0;
  472. }
  473. int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  474. {
  475. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  476. struct batadv_tt_query_packet *tt_query;
  477. uint16_t tt_size;
  478. int hdr_size = sizeof(*tt_query);
  479. char tt_flag;
  480. size_t packet_size;
  481. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  482. return NET_RX_DROP;
  483. /* I could need to modify it */
  484. if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
  485. goto out;
  486. tt_query = (struct batadv_tt_query_packet *)skb->data;
  487. switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
  488. case BATADV_TT_REQUEST:
  489. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
  490. /* If we cannot provide an answer the tt_request is
  491. * forwarded
  492. */
  493. if (!batadv_send_tt_response(bat_priv, tt_query)) {
  494. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  495. tt_flag = 'F';
  496. else
  497. tt_flag = '.';
  498. batadv_dbg(BATADV_DBG_TT, bat_priv,
  499. "Routing TT_REQUEST to %pM [%c]\n",
  500. tt_query->dst,
  501. tt_flag);
  502. return batadv_route_unicast_packet(skb, recv_if);
  503. }
  504. break;
  505. case BATADV_TT_RESPONSE:
  506. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
  507. if (batadv_is_my_mac(tt_query->dst)) {
  508. /* packet needs to be linearized to access the TT
  509. * changes
  510. */
  511. if (skb_linearize(skb) < 0)
  512. goto out;
  513. /* skb_linearize() possibly changed skb->data */
  514. tt_query = (struct batadv_tt_query_packet *)skb->data;
  515. tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
  516. /* Ensure we have all the claimed data */
  517. packet_size = sizeof(struct batadv_tt_query_packet);
  518. packet_size += tt_size;
  519. if (unlikely(skb_headlen(skb) < packet_size))
  520. goto out;
  521. batadv_handle_tt_response(bat_priv, tt_query);
  522. } else {
  523. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  524. tt_flag = 'F';
  525. else
  526. tt_flag = '.';
  527. batadv_dbg(BATADV_DBG_TT, bat_priv,
  528. "Routing TT_RESPONSE to %pM [%c]\n",
  529. tt_query->dst,
  530. tt_flag);
  531. return batadv_route_unicast_packet(skb, recv_if);
  532. }
  533. break;
  534. }
  535. out:
  536. /* returning NET_RX_DROP will make the caller function kfree the skb */
  537. return NET_RX_DROP;
  538. }
  539. int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  540. {
  541. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  542. struct batadv_roam_adv_packet *roam_adv_packet;
  543. struct batadv_orig_node *orig_node;
  544. if (batadv_check_unicast_packet(skb, sizeof(*roam_adv_packet)) < 0)
  545. goto out;
  546. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
  547. roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
  548. if (!batadv_is_my_mac(roam_adv_packet->dst))
  549. return batadv_route_unicast_packet(skb, recv_if);
  550. /* check if it is a backbone gateway. we don't accept
  551. * roaming advertisement from it, as it has the same
  552. * entries as we have.
  553. */
  554. if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
  555. goto out;
  556. orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
  557. if (!orig_node)
  558. goto out;
  559. batadv_dbg(BATADV_DBG_TT, bat_priv,
  560. "Received ROAMING_ADV from %pM (client %pM)\n",
  561. roam_adv_packet->src, roam_adv_packet->client);
  562. batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  563. BATADV_TT_CLIENT_ROAM,
  564. atomic_read(&orig_node->last_ttvn) + 1);
  565. batadv_orig_node_free_ref(orig_node);
  566. out:
  567. /* returning NET_RX_DROP will make the caller function kfree the skb */
  568. return NET_RX_DROP;
  569. }
  570. /* find a suitable router for this originator, and use
  571. * bonding if possible. increases the found neighbors
  572. * refcount.
  573. */
  574. struct batadv_neigh_node *
  575. batadv_find_router(struct batadv_priv *bat_priv,
  576. struct batadv_orig_node *orig_node,
  577. const struct batadv_hard_iface *recv_if)
  578. {
  579. struct batadv_orig_node *primary_orig_node;
  580. struct batadv_orig_node *router_orig;
  581. struct batadv_neigh_node *router;
  582. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  583. int bonding_enabled;
  584. uint8_t *primary_addr;
  585. if (!orig_node)
  586. return NULL;
  587. router = batadv_orig_node_get_router(orig_node);
  588. if (!router)
  589. goto err;
  590. /* without bonding, the first node should
  591. * always choose the default router.
  592. */
  593. bonding_enabled = atomic_read(&bat_priv->bonding);
  594. rcu_read_lock();
  595. /* select default router to output */
  596. router_orig = router->orig_node;
  597. if (!router_orig)
  598. goto err_unlock;
  599. if ((!recv_if) && (!bonding_enabled))
  600. goto return_router;
  601. primary_addr = router_orig->primary_addr;
  602. /* if we have something in the primary_addr, we can search
  603. * for a potential bonding candidate.
  604. */
  605. if (batadv_compare_eth(primary_addr, zero_mac))
  606. goto return_router;
  607. /* find the orig_node which has the primary interface. might
  608. * even be the same as our router_orig in many cases
  609. */
  610. if (batadv_compare_eth(primary_addr, router_orig->orig)) {
  611. primary_orig_node = router_orig;
  612. } else {
  613. primary_orig_node = batadv_orig_hash_find(bat_priv,
  614. primary_addr);
  615. if (!primary_orig_node)
  616. goto return_router;
  617. batadv_orig_node_free_ref(primary_orig_node);
  618. }
  619. /* with less than 2 candidates, we can't do any
  620. * bonding and prefer the original router.
  621. */
  622. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  623. goto return_router;
  624. /* all nodes between should choose a candidate which
  625. * is is not on the interface where the packet came
  626. * in.
  627. */
  628. batadv_neigh_node_free_ref(router);
  629. if (bonding_enabled)
  630. router = batadv_find_bond_router(primary_orig_node, recv_if);
  631. else
  632. router = batadv_find_ifalter_router(primary_orig_node, recv_if);
  633. return_router:
  634. if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
  635. goto err_unlock;
  636. rcu_read_unlock();
  637. return router;
  638. err_unlock:
  639. rcu_read_unlock();
  640. err:
  641. if (router)
  642. batadv_neigh_node_free_ref(router);
  643. return NULL;
  644. }
  645. static int batadv_route_unicast_packet(struct sk_buff *skb,
  646. struct batadv_hard_iface *recv_if)
  647. {
  648. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  649. struct batadv_orig_node *orig_node = NULL;
  650. struct batadv_neigh_node *neigh_node = NULL;
  651. struct batadv_unicast_packet *unicast_packet;
  652. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  653. int ret = NET_RX_DROP;
  654. struct sk_buff *new_skb;
  655. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  656. /* TTL exceeded */
  657. if (unicast_packet->header.ttl < 2) {
  658. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  659. ethhdr->h_source, unicast_packet->dest);
  660. goto out;
  661. }
  662. /* get routing information */
  663. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  664. if (!orig_node)
  665. goto out;
  666. /* find_router() increases neigh_nodes refcount if found. */
  667. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  668. if (!neigh_node)
  669. goto out;
  670. /* create a copy of the skb, if needed, to modify it. */
  671. if (skb_cow(skb, ETH_HLEN) < 0)
  672. goto out;
  673. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  674. if (unicast_packet->header.packet_type == BATADV_UNICAST &&
  675. atomic_read(&bat_priv->fragmentation) &&
  676. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  677. ret = batadv_frag_send_skb(skb, bat_priv,
  678. neigh_node->if_incoming,
  679. neigh_node->addr);
  680. goto out;
  681. }
  682. if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
  683. batadv_frag_can_reassemble(skb,
  684. neigh_node->if_incoming->net_dev->mtu)) {
  685. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  686. if (ret == NET_RX_DROP)
  687. goto out;
  688. /* packet was buffered for late merge */
  689. if (!new_skb) {
  690. ret = NET_RX_SUCCESS;
  691. goto out;
  692. }
  693. skb = new_skb;
  694. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  695. }
  696. /* decrement ttl */
  697. unicast_packet->header.ttl--;
  698. /* network code packet if possible */
  699. if (batadv_nc_skb_forward(skb, neigh_node, ethhdr)) {
  700. ret = NET_RX_SUCCESS;
  701. } else if (batadv_send_skb_to_orig(skb, orig_node, recv_if)) {
  702. ret = NET_RX_SUCCESS;
  703. /* Update stats counter */
  704. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  705. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  706. skb->len + ETH_HLEN);
  707. }
  708. out:
  709. if (neigh_node)
  710. batadv_neigh_node_free_ref(neigh_node);
  711. if (orig_node)
  712. batadv_orig_node_free_ref(orig_node);
  713. return ret;
  714. }
  715. /**
  716. * batadv_reroute_unicast_packet - update the unicast header for re-routing
  717. * @bat_priv: the bat priv with all the soft interface information
  718. * @unicast_packet: the unicast header to be updated
  719. * @dst_addr: the payload destination
  720. *
  721. * Search the translation table for dst_addr and update the unicast header with
  722. * the new corresponding information (originator address where the destination
  723. * client currently is and its known TTVN)
  724. *
  725. * Returns true if the packet header has been updated, false otherwise
  726. */
  727. static bool
  728. batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
  729. struct batadv_unicast_packet *unicast_packet,
  730. uint8_t *dst_addr)
  731. {
  732. struct batadv_orig_node *orig_node = NULL;
  733. struct batadv_hard_iface *primary_if = NULL;
  734. bool ret = false;
  735. uint8_t *orig_addr, orig_ttvn;
  736. if (batadv_is_my_client(bat_priv, dst_addr)) {
  737. primary_if = batadv_primary_if_get_selected(bat_priv);
  738. if (!primary_if)
  739. goto out;
  740. orig_addr = primary_if->net_dev->dev_addr;
  741. orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  742. } else {
  743. orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
  744. if (!orig_node)
  745. goto out;
  746. if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
  747. goto out;
  748. orig_addr = orig_node->orig;
  749. orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  750. }
  751. /* update the packet header */
  752. memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
  753. unicast_packet->ttvn = orig_ttvn;
  754. ret = true;
  755. out:
  756. if (primary_if)
  757. batadv_hardif_free_ref(primary_if);
  758. if (orig_node)
  759. batadv_orig_node_free_ref(orig_node);
  760. return ret;
  761. }
  762. static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  763. struct sk_buff *skb) {
  764. uint8_t curr_ttvn, old_ttvn;
  765. struct batadv_orig_node *orig_node;
  766. struct ethhdr *ethhdr;
  767. struct batadv_hard_iface *primary_if;
  768. struct batadv_unicast_packet *unicast_packet;
  769. int is_old_ttvn;
  770. /* check if there is enough data before accessing it */
  771. if (pskb_may_pull(skb, sizeof(*unicast_packet) + ETH_HLEN) < 0)
  772. return 0;
  773. /* create a copy of the skb (in case of for re-routing) to modify it. */
  774. if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
  775. return 0;
  776. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  777. ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet));
  778. /* check if the destination client was served by this node and it is now
  779. * roaming. In this case, it means that the node has got a ROAM_ADV
  780. * message and that it knows the new destination in the mesh to re-route
  781. * the packet to
  782. */
  783. if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
  784. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  785. ethhdr->h_dest))
  786. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
  787. bat_priv,
  788. "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
  789. unicast_packet->dest,
  790. ethhdr->h_dest);
  791. /* at this point the mesh destination should have been
  792. * substituted with the originator address found in the global
  793. * table. If not, let the packet go untouched anyway because
  794. * there is nothing the node can do
  795. */
  796. return 1;
  797. }
  798. /* retrieve the TTVN known by this node for the packet destination. This
  799. * value is used later to check if the node which sent (or re-routed
  800. * last time) the packet had an updated information or not
  801. */
  802. curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  803. if (!batadv_is_my_mac(unicast_packet->dest)) {
  804. orig_node = batadv_orig_hash_find(bat_priv,
  805. unicast_packet->dest);
  806. /* if it is not possible to find the orig_node representing the
  807. * destination, the packet can immediately be dropped as it will
  808. * not be possible to deliver it
  809. */
  810. if (!orig_node)
  811. return 0;
  812. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  813. batadv_orig_node_free_ref(orig_node);
  814. }
  815. /* check if the TTVN contained in the packet is fresher than what the
  816. * node knows
  817. */
  818. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  819. if (!is_old_ttvn)
  820. return 1;
  821. old_ttvn = unicast_packet->ttvn;
  822. /* the packet was forged based on outdated network information. Its
  823. * destination can possibly be updated and forwarded towards the new
  824. * target host
  825. */
  826. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  827. ethhdr->h_dest)) {
  828. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
  829. "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
  830. unicast_packet->dest, ethhdr->h_dest,
  831. old_ttvn, curr_ttvn);
  832. return 1;
  833. }
  834. /* the packet has not been re-routed: either the destination is
  835. * currently served by this node or there is no destination at all and
  836. * it is possible to drop the packet
  837. */
  838. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
  839. return 0;
  840. /* update the header in order to let the packet be delivered to this
  841. * node's soft interface
  842. */
  843. primary_if = batadv_primary_if_get_selected(bat_priv);
  844. if (!primary_if)
  845. return 0;
  846. memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
  847. batadv_hardif_free_ref(primary_if);
  848. unicast_packet->ttvn = curr_ttvn;
  849. return 1;
  850. }
  851. int batadv_recv_unicast_packet(struct sk_buff *skb,
  852. struct batadv_hard_iface *recv_if)
  853. {
  854. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  855. struct batadv_unicast_packet *unicast_packet;
  856. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  857. uint8_t *orig_addr;
  858. struct batadv_orig_node *orig_node = NULL;
  859. int check, hdr_size = sizeof(*unicast_packet);
  860. bool is4addr;
  861. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  862. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  863. is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
  864. /* the caller function should have already pulled 2 bytes */
  865. if (is4addr)
  866. hdr_size = sizeof(*unicast_4addr_packet);
  867. /* function returns -EREMOTE for promiscuous packets */
  868. check = batadv_check_unicast_packet(skb, hdr_size);
  869. /* Even though the packet is not for us, we might save it to use for
  870. * decoding a later received coded packet
  871. */
  872. if (check == -EREMOTE)
  873. batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
  874. if (check < 0)
  875. return NET_RX_DROP;
  876. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  877. return NET_RX_DROP;
  878. /* packet for me */
  879. if (batadv_is_my_mac(unicast_packet->dest)) {
  880. if (is4addr) {
  881. batadv_dat_inc_counter(bat_priv,
  882. unicast_4addr_packet->subtype);
  883. orig_addr = unicast_4addr_packet->src;
  884. orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
  885. }
  886. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
  887. hdr_size))
  888. goto rx_success;
  889. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
  890. hdr_size))
  891. goto rx_success;
  892. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  893. orig_node);
  894. rx_success:
  895. if (orig_node)
  896. batadv_orig_node_free_ref(orig_node);
  897. return NET_RX_SUCCESS;
  898. }
  899. return batadv_route_unicast_packet(skb, recv_if);
  900. }
  901. int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
  902. struct batadv_hard_iface *recv_if)
  903. {
  904. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  905. struct batadv_unicast_frag_packet *unicast_packet;
  906. int hdr_size = sizeof(*unicast_packet);
  907. struct sk_buff *new_skb = NULL;
  908. int ret;
  909. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  910. return NET_RX_DROP;
  911. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  912. return NET_RX_DROP;
  913. unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
  914. /* packet for me */
  915. if (batadv_is_my_mac(unicast_packet->dest)) {
  916. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  917. if (ret == NET_RX_DROP)
  918. return NET_RX_DROP;
  919. /* packet was buffered for late merge */
  920. if (!new_skb)
  921. return NET_RX_SUCCESS;
  922. if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb,
  923. hdr_size))
  924. goto rx_success;
  925. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb,
  926. hdr_size))
  927. goto rx_success;
  928. batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
  929. sizeof(struct batadv_unicast_packet), NULL);
  930. rx_success:
  931. return NET_RX_SUCCESS;
  932. }
  933. return batadv_route_unicast_packet(skb, recv_if);
  934. }
  935. int batadv_recv_bcast_packet(struct sk_buff *skb,
  936. struct batadv_hard_iface *recv_if)
  937. {
  938. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  939. struct batadv_orig_node *orig_node = NULL;
  940. struct batadv_bcast_packet *bcast_packet;
  941. struct ethhdr *ethhdr;
  942. int hdr_size = sizeof(*bcast_packet);
  943. int ret = NET_RX_DROP;
  944. int32_t seq_diff;
  945. /* drop packet if it has not necessary minimum size */
  946. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  947. goto out;
  948. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  949. /* packet with broadcast indication but unicast recipient */
  950. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  951. goto out;
  952. /* packet with broadcast sender address */
  953. if (is_broadcast_ether_addr(ethhdr->h_source))
  954. goto out;
  955. /* ignore broadcasts sent by myself */
  956. if (batadv_is_my_mac(ethhdr->h_source))
  957. goto out;
  958. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  959. /* ignore broadcasts originated by myself */
  960. if (batadv_is_my_mac(bcast_packet->orig))
  961. goto out;
  962. if (bcast_packet->header.ttl < 2)
  963. goto out;
  964. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  965. if (!orig_node)
  966. goto out;
  967. spin_lock_bh(&orig_node->bcast_seqno_lock);
  968. /* check whether the packet is a duplicate */
  969. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  970. ntohl(bcast_packet->seqno)))
  971. goto spin_unlock;
  972. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  973. /* check whether the packet is old and the host just restarted. */
  974. if (batadv_window_protected(bat_priv, seq_diff,
  975. &orig_node->bcast_seqno_reset))
  976. goto spin_unlock;
  977. /* mark broadcast in flood history, update window position
  978. * if required.
  979. */
  980. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  981. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  982. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  983. /* check whether this has been sent by another originator before */
  984. if (batadv_bla_check_bcast_duplist(bat_priv, skb))
  985. goto out;
  986. /* rebroadcast packet */
  987. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  988. /* don't hand the broadcast up if it is from an originator
  989. * from the same backbone.
  990. */
  991. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  992. goto out;
  993. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
  994. goto rx_success;
  995. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
  996. goto rx_success;
  997. /* broadcast for me */
  998. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  999. orig_node);
  1000. rx_success:
  1001. ret = NET_RX_SUCCESS;
  1002. goto out;
  1003. spin_unlock:
  1004. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1005. out:
  1006. if (orig_node)
  1007. batadv_orig_node_free_ref(orig_node);
  1008. return ret;
  1009. }
  1010. int batadv_recv_vis_packet(struct sk_buff *skb,
  1011. struct batadv_hard_iface *recv_if)
  1012. {
  1013. struct batadv_vis_packet *vis_packet;
  1014. struct ethhdr *ethhdr;
  1015. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1016. int hdr_size = sizeof(*vis_packet);
  1017. /* keep skb linear */
  1018. if (skb_linearize(skb) < 0)
  1019. return NET_RX_DROP;
  1020. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1021. return NET_RX_DROP;
  1022. vis_packet = (struct batadv_vis_packet *)skb->data;
  1023. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1024. /* not for me */
  1025. if (!batadv_is_my_mac(ethhdr->h_dest))
  1026. return NET_RX_DROP;
  1027. /* ignore own packets */
  1028. if (batadv_is_my_mac(vis_packet->vis_orig))
  1029. return NET_RX_DROP;
  1030. if (batadv_is_my_mac(vis_packet->sender_orig))
  1031. return NET_RX_DROP;
  1032. switch (vis_packet->vis_type) {
  1033. case BATADV_VIS_TYPE_SERVER_SYNC:
  1034. batadv_receive_server_sync_packet(bat_priv, vis_packet,
  1035. skb_headlen(skb));
  1036. break;
  1037. case BATADV_VIS_TYPE_CLIENT_UPDATE:
  1038. batadv_receive_client_update_packet(bat_priv, vis_packet,
  1039. skb_headlen(skb));
  1040. break;
  1041. default: /* ignore unknown packet */
  1042. break;
  1043. }
  1044. /* We take a copy of the data in the packet, so we should
  1045. * always free the skbuf.
  1046. */
  1047. return NET_RX_DROP;
  1048. }