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