routing.c 36 KB

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