routing.c 35 KB

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