routing.c 36 KB

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