routing.c 35 KB

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