routing.c 33 KB

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