routing.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /* Copyright (C) 2007-2012 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_node *node;
  38. struct hlist_head *head;
  39. struct batadv_orig_node *orig_node;
  40. unsigned long *word;
  41. uint32_t i;
  42. size_t word_index;
  43. uint8_t *w;
  44. for (i = 0; i < hash->size; i++) {
  45. head = &hash->table[i];
  46. rcu_read_lock();
  47. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  48. spin_lock_bh(&orig_node->ogm_cnt_lock);
  49. word_index = hard_iface->if_num * BATADV_NUM_WORDS;
  50. word = &(orig_node->bcast_own[word_index]);
  51. batadv_bit_get_packet(bat_priv, word, 1, 0);
  52. w = &orig_node->bcast_own_sum[hard_iface->if_num];
  53. *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
  54. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  55. }
  56. rcu_read_unlock();
  57. }
  58. }
  59. static void _batadv_update_route(struct batadv_priv *bat_priv,
  60. struct batadv_orig_node *orig_node,
  61. struct batadv_neigh_node *neigh_node)
  62. {
  63. struct batadv_neigh_node *curr_router;
  64. curr_router = batadv_orig_node_get_router(orig_node);
  65. /* route deleted */
  66. if ((curr_router) && (!neigh_node)) {
  67. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  68. "Deleting route towards: %pM\n", orig_node->orig);
  69. batadv_tt_global_del_orig(bat_priv, orig_node,
  70. "Deleted route towards originator");
  71. /* route added */
  72. } else if ((!curr_router) && (neigh_node)) {
  73. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  74. "Adding route towards: %pM (via %pM)\n",
  75. orig_node->orig, neigh_node->addr);
  76. /* route changed */
  77. } else if (neigh_node && curr_router) {
  78. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  79. "Changing route towards: %pM (now via %pM - was via %pM)\n",
  80. orig_node->orig, neigh_node->addr,
  81. curr_router->addr);
  82. }
  83. if (curr_router)
  84. batadv_neigh_node_free_ref(curr_router);
  85. /* increase refcount of new best neighbor */
  86. if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
  87. neigh_node = NULL;
  88. spin_lock_bh(&orig_node->neigh_list_lock);
  89. rcu_assign_pointer(orig_node->router, neigh_node);
  90. spin_unlock_bh(&orig_node->neigh_list_lock);
  91. /* decrease refcount of previous best neighbor */
  92. if (curr_router)
  93. batadv_neigh_node_free_ref(curr_router);
  94. }
  95. void batadv_update_route(struct batadv_priv *bat_priv,
  96. struct batadv_orig_node *orig_node,
  97. struct batadv_neigh_node *neigh_node)
  98. {
  99. struct batadv_neigh_node *router = NULL;
  100. if (!orig_node)
  101. goto out;
  102. router = batadv_orig_node_get_router(orig_node);
  103. if (router != neigh_node)
  104. _batadv_update_route(bat_priv, orig_node, neigh_node);
  105. out:
  106. if (router)
  107. batadv_neigh_node_free_ref(router);
  108. }
  109. /* caller must hold the neigh_list_lock */
  110. void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
  111. struct batadv_neigh_node *neigh_node)
  112. {
  113. /* this neighbor is not part of our candidate list */
  114. if (list_empty(&neigh_node->bonding_list))
  115. goto out;
  116. list_del_rcu(&neigh_node->bonding_list);
  117. INIT_LIST_HEAD(&neigh_node->bonding_list);
  118. batadv_neigh_node_free_ref(neigh_node);
  119. atomic_dec(&orig_node->bond_candidates);
  120. out:
  121. return;
  122. }
  123. void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
  124. struct batadv_neigh_node *neigh_node)
  125. {
  126. struct hlist_node *node;
  127. struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
  128. uint8_t interference_candidate = 0;
  129. spin_lock_bh(&orig_node->neigh_list_lock);
  130. /* only consider if it has the same primary address ... */
  131. if (!batadv_compare_eth(orig_node->orig,
  132. neigh_node->orig_node->primary_addr))
  133. goto candidate_del;
  134. router = batadv_orig_node_get_router(orig_node);
  135. if (!router)
  136. goto candidate_del;
  137. /* ... and is good enough to be considered */
  138. if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
  139. goto candidate_del;
  140. /* check if we have another candidate with the same mac address or
  141. * interface. If we do, we won't select this candidate because of
  142. * possible interference.
  143. */
  144. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  145. &orig_node->neigh_list, list) {
  146. if (tmp_neigh_node == neigh_node)
  147. continue;
  148. /* we only care if the other candidate is even
  149. * considered as candidate.
  150. */
  151. if (list_empty(&tmp_neigh_node->bonding_list))
  152. continue;
  153. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  154. (batadv_compare_eth(neigh_node->addr,
  155. tmp_neigh_node->addr))) {
  156. interference_candidate = 1;
  157. break;
  158. }
  159. }
  160. /* don't care further if it is an interference candidate */
  161. if (interference_candidate)
  162. goto candidate_del;
  163. /* this neighbor already is part of our candidate list */
  164. if (!list_empty(&neigh_node->bonding_list))
  165. goto out;
  166. if (!atomic_inc_not_zero(&neigh_node->refcount))
  167. goto out;
  168. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  169. atomic_inc(&orig_node->bond_candidates);
  170. goto out;
  171. candidate_del:
  172. batadv_bonding_candidate_del(orig_node, neigh_node);
  173. out:
  174. spin_unlock_bh(&orig_node->neigh_list_lock);
  175. if (router)
  176. batadv_neigh_node_free_ref(router);
  177. }
  178. /* copy primary address for bonding */
  179. void
  180. batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
  181. struct batadv_orig_node *orig_neigh_node,
  182. const struct batadv_ogm_packet *batman_ogm_packet)
  183. {
  184. if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
  185. return;
  186. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  187. }
  188. /* checks whether the host restarted and is in the protection time.
  189. * returns:
  190. * 0 if the packet is to be accepted
  191. * 1 if the packet is to be ignored.
  192. */
  193. int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
  194. unsigned long *last_reset)
  195. {
  196. if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
  197. seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
  198. if (!batadv_has_timed_out(*last_reset,
  199. BATADV_RESET_PROTECTION_MS))
  200. return 1;
  201. *last_reset = jiffies;
  202. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  203. "old packet received, start protection\n");
  204. }
  205. return 0;
  206. }
  207. bool batadv_check_management_packet(struct sk_buff *skb,
  208. struct batadv_hard_iface *hard_iface,
  209. int header_len)
  210. {
  211. struct ethhdr *ethhdr;
  212. /* drop packet if it has not necessary minimum size */
  213. if (unlikely(!pskb_may_pull(skb, header_len)))
  214. return false;
  215. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  216. /* packet with broadcast indication but unicast recipient */
  217. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  218. return false;
  219. /* packet with broadcast sender address */
  220. if (is_broadcast_ether_addr(ethhdr->h_source))
  221. return false;
  222. /* create a copy of the skb, if needed, to modify it. */
  223. if (skb_cow(skb, 0) < 0)
  224. return false;
  225. /* keep skb linear */
  226. if (skb_linearize(skb) < 0)
  227. return false;
  228. return true;
  229. }
  230. static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
  231. struct sk_buff *skb, size_t icmp_len)
  232. {
  233. struct batadv_hard_iface *primary_if = NULL;
  234. struct batadv_orig_node *orig_node = NULL;
  235. struct batadv_icmp_packet_rr *icmp_packet;
  236. int ret = NET_RX_DROP;
  237. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  238. /* add data to device queue */
  239. if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
  240. batadv_socket_receive_packet(icmp_packet, icmp_len);
  241. goto out;
  242. }
  243. primary_if = batadv_primary_if_get_selected(bat_priv);
  244. if (!primary_if)
  245. goto out;
  246. /* answer echo request (ping) */
  247. /* get routing information */
  248. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  249. if (!orig_node)
  250. goto out;
  251. /* create a copy of the skb, if needed, to modify it. */
  252. if (skb_cow(skb, ETH_HLEN) < 0)
  253. goto out;
  254. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  255. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  256. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  257. icmp_packet->msg_type = BATADV_ECHO_REPLY;
  258. icmp_packet->header.ttl = BATADV_TTL;
  259. if (batadv_send_skb_to_orig(skb, orig_node, NULL))
  260. ret = NET_RX_SUCCESS;
  261. out:
  262. if (primary_if)
  263. batadv_hardif_free_ref(primary_if);
  264. if (orig_node)
  265. batadv_orig_node_free_ref(orig_node);
  266. return ret;
  267. }
  268. static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
  269. struct sk_buff *skb)
  270. {
  271. struct batadv_hard_iface *primary_if = NULL;
  272. struct batadv_orig_node *orig_node = NULL;
  273. struct batadv_icmp_packet *icmp_packet;
  274. int ret = NET_RX_DROP;
  275. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  276. /* send TTL exceeded if packet is an echo request (traceroute) */
  277. if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
  278. pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
  279. icmp_packet->orig, icmp_packet->dst);
  280. goto out;
  281. }
  282. primary_if = batadv_primary_if_get_selected(bat_priv);
  283. if (!primary_if)
  284. goto out;
  285. /* get routing information */
  286. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  287. if (!orig_node)
  288. goto out;
  289. /* create a copy of the skb, if needed, to modify it. */
  290. if (skb_cow(skb, ETH_HLEN) < 0)
  291. goto out;
  292. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  293. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  294. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  295. icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
  296. icmp_packet->header.ttl = BATADV_TTL;
  297. if (batadv_send_skb_to_orig(skb, orig_node, NULL))
  298. ret = NET_RX_SUCCESS;
  299. out:
  300. if (primary_if)
  301. batadv_hardif_free_ref(primary_if);
  302. if (orig_node)
  303. batadv_orig_node_free_ref(orig_node);
  304. return ret;
  305. }
  306. int batadv_recv_icmp_packet(struct sk_buff *skb,
  307. struct batadv_hard_iface *recv_if)
  308. {
  309. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  310. struct batadv_icmp_packet_rr *icmp_packet;
  311. struct ethhdr *ethhdr;
  312. struct batadv_orig_node *orig_node = NULL;
  313. int hdr_size = sizeof(struct batadv_icmp_packet);
  314. int ret = NET_RX_DROP;
  315. /* we truncate all incoming icmp packets if they don't match our size */
  316. if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
  317. hdr_size = sizeof(struct batadv_icmp_packet_rr);
  318. /* drop packet if it has not necessary minimum size */
  319. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  320. goto out;
  321. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  322. /* packet with unicast indication but broadcast recipient */
  323. if (is_broadcast_ether_addr(ethhdr->h_dest))
  324. goto out;
  325. /* packet with broadcast sender address */
  326. if (is_broadcast_ether_addr(ethhdr->h_source))
  327. goto out;
  328. /* not for me */
  329. if (!batadv_is_my_mac(ethhdr->h_dest))
  330. goto out;
  331. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  332. /* add record route information if not full */
  333. if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
  334. (icmp_packet->rr_cur < BATADV_RR_LEN)) {
  335. memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
  336. ethhdr->h_dest, ETH_ALEN);
  337. icmp_packet->rr_cur++;
  338. }
  339. /* packet for me */
  340. if (batadv_is_my_mac(icmp_packet->dst))
  341. return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
  342. /* TTL exceeded */
  343. if (icmp_packet->header.ttl < 2)
  344. return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
  345. /* get routing information */
  346. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
  347. if (!orig_node)
  348. goto out;
  349. /* create a copy of the skb, if needed, to modify it. */
  350. if (skb_cow(skb, ETH_HLEN) < 0)
  351. goto out;
  352. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  353. /* decrement ttl */
  354. icmp_packet->header.ttl--;
  355. /* route it */
  356. if (batadv_send_skb_to_orig(skb, orig_node, recv_if))
  357. ret = NET_RX_SUCCESS;
  358. out:
  359. if (orig_node)
  360. batadv_orig_node_free_ref(orig_node);
  361. return ret;
  362. }
  363. /* In the bonding case, send the packets in a round
  364. * robin fashion over the remaining interfaces.
  365. *
  366. * This method rotates the bonding list and increases the
  367. * returned router's refcount.
  368. */
  369. static struct batadv_neigh_node *
  370. batadv_find_bond_router(struct batadv_orig_node *primary_orig,
  371. const struct batadv_hard_iface *recv_if)
  372. {
  373. struct batadv_neigh_node *tmp_neigh_node;
  374. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  375. rcu_read_lock();
  376. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  377. bonding_list) {
  378. if (!first_candidate)
  379. first_candidate = tmp_neigh_node;
  380. /* recv_if == NULL on the first node. */
  381. if (tmp_neigh_node->if_incoming == recv_if)
  382. continue;
  383. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  384. continue;
  385. router = tmp_neigh_node;
  386. break;
  387. }
  388. /* use the first candidate if nothing was found. */
  389. if (!router && first_candidate &&
  390. atomic_inc_not_zero(&first_candidate->refcount))
  391. router = first_candidate;
  392. if (!router)
  393. goto out;
  394. /* selected should point to the next element
  395. * after the current router
  396. */
  397. spin_lock_bh(&primary_orig->neigh_list_lock);
  398. /* this is a list_move(), which unfortunately
  399. * does not exist as rcu version
  400. */
  401. list_del_rcu(&primary_orig->bond_list);
  402. list_add_rcu(&primary_orig->bond_list,
  403. &router->bonding_list);
  404. spin_unlock_bh(&primary_orig->neigh_list_lock);
  405. out:
  406. rcu_read_unlock();
  407. return router;
  408. }
  409. /* Interface Alternating: Use the best of the
  410. * remaining candidates which are not using
  411. * this interface.
  412. *
  413. * Increases the returned router's refcount
  414. */
  415. static struct batadv_neigh_node *
  416. batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
  417. const struct batadv_hard_iface *recv_if)
  418. {
  419. struct batadv_neigh_node *tmp_neigh_node;
  420. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  421. rcu_read_lock();
  422. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  423. bonding_list) {
  424. if (!first_candidate)
  425. first_candidate = tmp_neigh_node;
  426. /* recv_if == NULL on the first node. */
  427. if (tmp_neigh_node->if_incoming == recv_if)
  428. continue;
  429. if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
  430. continue;
  431. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  432. continue;
  433. /* decrement refcount of previously selected router */
  434. if (router)
  435. batadv_neigh_node_free_ref(router);
  436. /* we found a better router (or at least one valid router) */
  437. router = tmp_neigh_node;
  438. }
  439. /* use the first candidate if nothing was found. */
  440. if (!router && first_candidate &&
  441. atomic_inc_not_zero(&first_candidate->refcount))
  442. router = first_candidate;
  443. rcu_read_unlock();
  444. return router;
  445. }
  446. static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
  447. {
  448. struct ethhdr *ethhdr;
  449. /* drop packet if it has not necessary minimum size */
  450. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  451. return -1;
  452. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  453. /* packet with unicast indication but broadcast recipient */
  454. if (is_broadcast_ether_addr(ethhdr->h_dest))
  455. return -1;
  456. /* packet with broadcast sender address */
  457. if (is_broadcast_ether_addr(ethhdr->h_source))
  458. return -1;
  459. /* not for me */
  460. if (!batadv_is_my_mac(ethhdr->h_dest))
  461. return -1;
  462. return 0;
  463. }
  464. int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  465. {
  466. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  467. struct batadv_tt_query_packet *tt_query;
  468. uint16_t tt_size;
  469. int hdr_size = sizeof(*tt_query);
  470. char tt_flag;
  471. size_t packet_size;
  472. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  473. return NET_RX_DROP;
  474. /* I could need to modify it */
  475. if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
  476. goto out;
  477. tt_query = (struct batadv_tt_query_packet *)skb->data;
  478. switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
  479. case BATADV_TT_REQUEST:
  480. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
  481. /* If we cannot provide an answer the tt_request is
  482. * forwarded
  483. */
  484. if (!batadv_send_tt_response(bat_priv, tt_query)) {
  485. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  486. tt_flag = 'F';
  487. else
  488. tt_flag = '.';
  489. batadv_dbg(BATADV_DBG_TT, bat_priv,
  490. "Routing TT_REQUEST to %pM [%c]\n",
  491. tt_query->dst,
  492. tt_flag);
  493. return batadv_route_unicast_packet(skb, recv_if);
  494. }
  495. break;
  496. case BATADV_TT_RESPONSE:
  497. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
  498. if (batadv_is_my_mac(tt_query->dst)) {
  499. /* packet needs to be linearized to access the TT
  500. * changes
  501. */
  502. if (skb_linearize(skb) < 0)
  503. goto out;
  504. /* skb_linearize() possibly changed skb->data */
  505. tt_query = (struct batadv_tt_query_packet *)skb->data;
  506. tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
  507. /* Ensure we have all the claimed data */
  508. packet_size = sizeof(struct batadv_tt_query_packet);
  509. packet_size += tt_size;
  510. if (unlikely(skb_headlen(skb) < packet_size))
  511. goto out;
  512. batadv_handle_tt_response(bat_priv, tt_query);
  513. } else {
  514. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  515. tt_flag = 'F';
  516. else
  517. tt_flag = '.';
  518. batadv_dbg(BATADV_DBG_TT, bat_priv,
  519. "Routing TT_RESPONSE to %pM [%c]\n",
  520. tt_query->dst,
  521. tt_flag);
  522. return batadv_route_unicast_packet(skb, recv_if);
  523. }
  524. break;
  525. }
  526. out:
  527. /* returning NET_RX_DROP will make the caller function kfree the skb */
  528. return NET_RX_DROP;
  529. }
  530. int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  531. {
  532. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  533. struct batadv_roam_adv_packet *roam_adv_packet;
  534. struct batadv_orig_node *orig_node;
  535. if (batadv_check_unicast_packet(skb, 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(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(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(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(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(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(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(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(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(ethhdr->h_dest))
  1007. return NET_RX_DROP;
  1008. /* ignore own packets */
  1009. if (batadv_is_my_mac(vis_packet->vis_orig))
  1010. return NET_RX_DROP;
  1011. if (batadv_is_my_mac(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. }