routing.c 35 KB

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