routing.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "routing.h"
  21. #include "send.h"
  22. #include "soft-interface.h"
  23. #include "hard-interface.h"
  24. #include "icmp_socket.h"
  25. #include "translation-table.h"
  26. #include "originator.h"
  27. #include "vis.h"
  28. #include "unicast.h"
  29. #include "bridge_loop_avoidance.h"
  30. static int batadv_route_unicast_packet(struct sk_buff *skb,
  31. struct batadv_hard_iface *recv_if);
  32. void batadv_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
  33. {
  34. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  35. struct batadv_hashtable *hash = bat_priv->orig_hash;
  36. struct hlist_node *node;
  37. struct hlist_head *head;
  38. struct batadv_orig_node *orig_node;
  39. unsigned long *word;
  40. uint32_t i;
  41. size_t word_index;
  42. uint8_t *w;
  43. for (i = 0; i < hash->size; i++) {
  44. head = &hash->table[i];
  45. rcu_read_lock();
  46. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  47. spin_lock_bh(&orig_node->ogm_cnt_lock);
  48. word_index = hard_iface->if_num * BATADV_NUM_WORDS;
  49. word = &(orig_node->bcast_own[word_index]);
  50. batadv_bit_get_packet(bat_priv, word, 1, 0);
  51. w = &orig_node->bcast_own_sum[hard_iface->if_num];
  52. *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
  53. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  54. }
  55. rcu_read_unlock();
  56. }
  57. }
  58. static void _batadv_update_route(struct batadv_priv *bat_priv,
  59. struct batadv_orig_node *orig_node,
  60. struct batadv_neigh_node *neigh_node)
  61. {
  62. struct batadv_neigh_node *curr_router;
  63. curr_router = batadv_orig_node_get_router(orig_node);
  64. /* route deleted */
  65. if ((curr_router) && (!neigh_node)) {
  66. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  67. "Deleting route towards: %pM\n", orig_node->orig);
  68. batadv_tt_global_del_orig(bat_priv, orig_node,
  69. "Deleted route towards originator");
  70. /* route added */
  71. } else if ((!curr_router) && (neigh_node)) {
  72. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  73. "Adding route towards: %pM (via %pM)\n",
  74. orig_node->orig, neigh_node->addr);
  75. /* route changed */
  76. } else if (neigh_node && curr_router) {
  77. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  78. "Changing route towards: %pM (now via %pM - was via %pM)\n",
  79. orig_node->orig, neigh_node->addr,
  80. curr_router->addr);
  81. }
  82. if (curr_router)
  83. batadv_neigh_node_free_ref(curr_router);
  84. /* increase refcount of new best neighbor */
  85. if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
  86. neigh_node = NULL;
  87. spin_lock_bh(&orig_node->neigh_list_lock);
  88. rcu_assign_pointer(orig_node->router, neigh_node);
  89. spin_unlock_bh(&orig_node->neigh_list_lock);
  90. /* decrease refcount of previous best neighbor */
  91. if (curr_router)
  92. batadv_neigh_node_free_ref(curr_router);
  93. }
  94. void batadv_update_route(struct batadv_priv *bat_priv,
  95. struct batadv_orig_node *orig_node,
  96. struct batadv_neigh_node *neigh_node)
  97. {
  98. struct batadv_neigh_node *router = NULL;
  99. if (!orig_node)
  100. goto out;
  101. router = batadv_orig_node_get_router(orig_node);
  102. if (router != neigh_node)
  103. _batadv_update_route(bat_priv, orig_node, neigh_node);
  104. out:
  105. if (router)
  106. batadv_neigh_node_free_ref(router);
  107. }
  108. /* caller must hold the neigh_list_lock */
  109. void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
  110. struct batadv_neigh_node *neigh_node)
  111. {
  112. /* this neighbor is not part of our candidate list */
  113. if (list_empty(&neigh_node->bonding_list))
  114. goto out;
  115. list_del_rcu(&neigh_node->bonding_list);
  116. INIT_LIST_HEAD(&neigh_node->bonding_list);
  117. batadv_neigh_node_free_ref(neigh_node);
  118. atomic_dec(&orig_node->bond_candidates);
  119. out:
  120. return;
  121. }
  122. void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
  123. struct batadv_neigh_node *neigh_node)
  124. {
  125. struct hlist_node *node;
  126. struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
  127. uint8_t interference_candidate = 0;
  128. spin_lock_bh(&orig_node->neigh_list_lock);
  129. /* only consider if it has the same primary address ... */
  130. if (!batadv_compare_eth(orig_node->orig,
  131. neigh_node->orig_node->primary_addr))
  132. goto candidate_del;
  133. router = batadv_orig_node_get_router(orig_node);
  134. if (!router)
  135. goto candidate_del;
  136. /* ... and is good enough to be considered */
  137. if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
  138. goto candidate_del;
  139. /* check if we have another candidate with the same mac address or
  140. * interface. If we do, we won't select this candidate because of
  141. * possible interference.
  142. */
  143. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  144. &orig_node->neigh_list, list) {
  145. if (tmp_neigh_node == neigh_node)
  146. continue;
  147. /* we only care if the other candidate is even
  148. * considered as candidate.
  149. */
  150. if (list_empty(&tmp_neigh_node->bonding_list))
  151. continue;
  152. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  153. (batadv_compare_eth(neigh_node->addr,
  154. tmp_neigh_node->addr))) {
  155. interference_candidate = 1;
  156. break;
  157. }
  158. }
  159. /* don't care further if it is an interference candidate */
  160. if (interference_candidate)
  161. goto candidate_del;
  162. /* this neighbor already is part of our candidate list */
  163. if (!list_empty(&neigh_node->bonding_list))
  164. goto out;
  165. if (!atomic_inc_not_zero(&neigh_node->refcount))
  166. goto out;
  167. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  168. atomic_inc(&orig_node->bond_candidates);
  169. goto out;
  170. candidate_del:
  171. batadv_bonding_candidate_del(orig_node, neigh_node);
  172. out:
  173. spin_unlock_bh(&orig_node->neigh_list_lock);
  174. if (router)
  175. batadv_neigh_node_free_ref(router);
  176. }
  177. /* copy primary address for bonding */
  178. void
  179. batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
  180. struct batadv_orig_node *orig_neigh_node,
  181. const struct batadv_ogm_packet *batman_ogm_packet)
  182. {
  183. if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
  184. return;
  185. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  186. }
  187. /* checks whether the host restarted and is in the protection time.
  188. * returns:
  189. * 0 if the packet is to be accepted
  190. * 1 if the packet is to be ignored.
  191. */
  192. int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
  193. unsigned long *last_reset)
  194. {
  195. if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
  196. seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
  197. if (!batadv_has_timed_out(*last_reset,
  198. BATADV_RESET_PROTECTION_MS))
  199. return 1;
  200. *last_reset = jiffies;
  201. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  202. "old packet received, start protection\n");
  203. }
  204. return 0;
  205. }
  206. bool batadv_check_management_packet(struct sk_buff *skb,
  207. struct batadv_hard_iface *hard_iface,
  208. int header_len)
  209. {
  210. struct ethhdr *ethhdr;
  211. /* drop packet if it has not necessary minimum size */
  212. if (unlikely(!pskb_may_pull(skb, header_len)))
  213. return false;
  214. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  215. /* packet with broadcast indication but unicast recipient */
  216. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  217. return false;
  218. /* packet with broadcast sender address */
  219. if (is_broadcast_ether_addr(ethhdr->h_source))
  220. return false;
  221. /* create a copy of the skb, if needed, to modify it. */
  222. if (skb_cow(skb, 0) < 0)
  223. return false;
  224. /* keep skb linear */
  225. if (skb_linearize(skb) < 0)
  226. return false;
  227. return true;
  228. }
  229. static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
  230. struct sk_buff *skb, size_t icmp_len)
  231. {
  232. struct batadv_hard_iface *primary_if = NULL;
  233. struct batadv_orig_node *orig_node = NULL;
  234. struct batadv_neigh_node *router = NULL;
  235. struct batadv_icmp_packet_rr *icmp_packet;
  236. int ret = NET_RX_DROP;
  237. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  238. /* add data to device queue */
  239. if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
  240. batadv_socket_receive_packet(icmp_packet, icmp_len);
  241. goto out;
  242. }
  243. primary_if = batadv_primary_if_get_selected(bat_priv);
  244. if (!primary_if)
  245. goto out;
  246. /* answer echo request (ping) */
  247. /* get routing information */
  248. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  249. if (!orig_node)
  250. goto out;
  251. router = batadv_orig_node_get_router(orig_node);
  252. if (!router)
  253. goto out;
  254. /* create a copy of the skb, if needed, to modify it. */
  255. if (skb_cow(skb, ETH_HLEN) < 0)
  256. goto out;
  257. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  258. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  259. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  260. icmp_packet->msg_type = BATADV_ECHO_REPLY;
  261. icmp_packet->header.ttl = BATADV_TTL;
  262. batadv_send_skb_packet(skb, router->if_incoming, router->addr);
  263. ret = NET_RX_SUCCESS;
  264. out:
  265. if (primary_if)
  266. batadv_hardif_free_ref(primary_if);
  267. if (router)
  268. batadv_neigh_node_free_ref(router);
  269. if (orig_node)
  270. batadv_orig_node_free_ref(orig_node);
  271. return ret;
  272. }
  273. static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
  274. struct sk_buff *skb)
  275. {
  276. struct batadv_hard_iface *primary_if = NULL;
  277. struct batadv_orig_node *orig_node = NULL;
  278. struct batadv_neigh_node *router = NULL;
  279. struct batadv_icmp_packet *icmp_packet;
  280. int ret = NET_RX_DROP;
  281. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  282. /* send TTL exceeded if packet is an echo request (traceroute) */
  283. if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
  284. pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
  285. icmp_packet->orig, icmp_packet->dst);
  286. goto out;
  287. }
  288. primary_if = batadv_primary_if_get_selected(bat_priv);
  289. if (!primary_if)
  290. goto out;
  291. /* get routing information */
  292. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  293. if (!orig_node)
  294. goto out;
  295. router = batadv_orig_node_get_router(orig_node);
  296. if (!router)
  297. goto out;
  298. /* create a copy of the skb, if needed, to modify it. */
  299. if (skb_cow(skb, ETH_HLEN) < 0)
  300. goto out;
  301. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  302. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  303. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  304. icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
  305. icmp_packet->header.ttl = BATADV_TTL;
  306. batadv_send_skb_packet(skb, router->if_incoming, router->addr);
  307. ret = NET_RX_SUCCESS;
  308. out:
  309. if (primary_if)
  310. batadv_hardif_free_ref(primary_if);
  311. if (router)
  312. batadv_neigh_node_free_ref(router);
  313. if (orig_node)
  314. batadv_orig_node_free_ref(orig_node);
  315. return ret;
  316. }
  317. int batadv_recv_icmp_packet(struct sk_buff *skb,
  318. struct batadv_hard_iface *recv_if)
  319. {
  320. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  321. struct batadv_icmp_packet_rr *icmp_packet;
  322. struct ethhdr *ethhdr;
  323. struct batadv_orig_node *orig_node = NULL;
  324. struct batadv_neigh_node *router = NULL;
  325. int hdr_size = sizeof(struct batadv_icmp_packet);
  326. int ret = NET_RX_DROP;
  327. /* we truncate all incoming icmp packets if they don't match our size */
  328. if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
  329. hdr_size = sizeof(struct batadv_icmp_packet_rr);
  330. /* drop packet if it has not necessary minimum size */
  331. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  332. goto out;
  333. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  334. /* packet with unicast indication but broadcast recipient */
  335. if (is_broadcast_ether_addr(ethhdr->h_dest))
  336. goto out;
  337. /* packet with broadcast sender address */
  338. if (is_broadcast_ether_addr(ethhdr->h_source))
  339. goto out;
  340. /* not for me */
  341. if (!batadv_is_my_mac(ethhdr->h_dest))
  342. goto out;
  343. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  344. /* add record route information if not full */
  345. if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
  346. (icmp_packet->rr_cur < BATADV_RR_LEN)) {
  347. memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
  348. ethhdr->h_dest, ETH_ALEN);
  349. icmp_packet->rr_cur++;
  350. }
  351. /* packet for me */
  352. if (batadv_is_my_mac(icmp_packet->dst))
  353. return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
  354. /* TTL exceeded */
  355. if (icmp_packet->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, icmp_packet->dst);
  359. if (!orig_node)
  360. goto out;
  361. router = batadv_orig_node_get_router(orig_node);
  362. if (!router)
  363. goto out;
  364. /* create a copy of the skb, if needed, to modify it. */
  365. if (skb_cow(skb, ETH_HLEN) < 0)
  366. goto out;
  367. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  368. /* decrement ttl */
  369. icmp_packet->header.ttl--;
  370. /* route it */
  371. batadv_send_skb_packet(skb, router->if_incoming, router->addr);
  372. ret = NET_RX_SUCCESS;
  373. out:
  374. if (router)
  375. batadv_neigh_node_free_ref(router);
  376. if (orig_node)
  377. batadv_orig_node_free_ref(orig_node);
  378. return ret;
  379. }
  380. /* In the bonding case, send the packets in a round
  381. * robin fashion over the remaining interfaces.
  382. *
  383. * This method rotates the bonding list and increases the
  384. * returned router's refcount.
  385. */
  386. static struct batadv_neigh_node *
  387. batadv_find_bond_router(struct batadv_orig_node *primary_orig,
  388. const struct batadv_hard_iface *recv_if)
  389. {
  390. struct batadv_neigh_node *tmp_neigh_node;
  391. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  392. rcu_read_lock();
  393. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  394. bonding_list) {
  395. if (!first_candidate)
  396. first_candidate = tmp_neigh_node;
  397. /* recv_if == NULL on the first node. */
  398. if (tmp_neigh_node->if_incoming == recv_if)
  399. continue;
  400. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  401. continue;
  402. router = tmp_neigh_node;
  403. break;
  404. }
  405. /* use the first candidate if nothing was found. */
  406. if (!router && first_candidate &&
  407. atomic_inc_not_zero(&first_candidate->refcount))
  408. router = first_candidate;
  409. if (!router)
  410. goto out;
  411. /* selected should point to the next element
  412. * after the current router
  413. */
  414. spin_lock_bh(&primary_orig->neigh_list_lock);
  415. /* this is a list_move(), which unfortunately
  416. * does not exist as rcu version
  417. */
  418. list_del_rcu(&primary_orig->bond_list);
  419. list_add_rcu(&primary_orig->bond_list,
  420. &router->bonding_list);
  421. spin_unlock_bh(&primary_orig->neigh_list_lock);
  422. out:
  423. rcu_read_unlock();
  424. return router;
  425. }
  426. /* Interface Alternating: Use the best of the
  427. * remaining candidates which are not using
  428. * this interface.
  429. *
  430. * Increases the returned router's refcount
  431. */
  432. static struct batadv_neigh_node *
  433. batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
  434. const struct batadv_hard_iface *recv_if)
  435. {
  436. struct batadv_neigh_node *tmp_neigh_node;
  437. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  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 (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  447. continue;
  448. /* if we don't have a router yet
  449. * or this one is better, choose it.
  450. */
  451. if ((!router) ||
  452. (tmp_neigh_node->tq_avg > router->tq_avg)) {
  453. /* decrement refcount of
  454. * previously selected router
  455. */
  456. if (router)
  457. batadv_neigh_node_free_ref(router);
  458. router = tmp_neigh_node;
  459. atomic_inc_not_zero(&router->refcount);
  460. }
  461. batadv_neigh_node_free_ref(tmp_neigh_node);
  462. }
  463. /* use the first candidate if nothing was found. */
  464. if (!router && first_candidate &&
  465. atomic_inc_not_zero(&first_candidate->refcount))
  466. router = first_candidate;
  467. rcu_read_unlock();
  468. return router;
  469. }
  470. int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  471. {
  472. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  473. struct batadv_tt_query_packet *tt_query;
  474. uint16_t tt_size;
  475. struct ethhdr *ethhdr;
  476. char tt_flag;
  477. size_t packet_size;
  478. /* drop packet if it has not necessary minimum size */
  479. if (unlikely(!pskb_may_pull(skb,
  480. sizeof(struct batadv_tt_query_packet))))
  481. goto out;
  482. /* I could need to modify it */
  483. if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
  484. goto out;
  485. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  486. /* packet with unicast indication but broadcast recipient */
  487. if (is_broadcast_ether_addr(ethhdr->h_dest))
  488. goto out;
  489. /* packet with broadcast sender address */
  490. if (is_broadcast_ether_addr(ethhdr->h_source))
  491. goto out;
  492. tt_query = (struct batadv_tt_query_packet *)skb->data;
  493. switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
  494. case BATADV_TT_REQUEST:
  495. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
  496. /* If we cannot provide an answer the tt_request is
  497. * forwarded
  498. */
  499. if (!batadv_send_tt_response(bat_priv, tt_query)) {
  500. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  501. tt_flag = 'F';
  502. else
  503. tt_flag = '.';
  504. batadv_dbg(BATADV_DBG_TT, bat_priv,
  505. "Routing TT_REQUEST to %pM [%c]\n",
  506. tt_query->dst,
  507. tt_flag);
  508. return batadv_route_unicast_packet(skb, recv_if);
  509. }
  510. break;
  511. case BATADV_TT_RESPONSE:
  512. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
  513. if (batadv_is_my_mac(tt_query->dst)) {
  514. /* packet needs to be linearized to access the TT
  515. * changes
  516. */
  517. if (skb_linearize(skb) < 0)
  518. goto out;
  519. /* skb_linearize() possibly changed skb->data */
  520. tt_query = (struct batadv_tt_query_packet *)skb->data;
  521. tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
  522. /* Ensure we have all the claimed data */
  523. packet_size = sizeof(struct batadv_tt_query_packet);
  524. packet_size += tt_size;
  525. if (unlikely(skb_headlen(skb) < packet_size))
  526. goto out;
  527. batadv_handle_tt_response(bat_priv, tt_query);
  528. } else {
  529. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  530. tt_flag = 'F';
  531. else
  532. tt_flag = '.';
  533. batadv_dbg(BATADV_DBG_TT, bat_priv,
  534. "Routing TT_RESPONSE to %pM [%c]\n",
  535. tt_query->dst,
  536. tt_flag);
  537. return batadv_route_unicast_packet(skb, recv_if);
  538. }
  539. break;
  540. }
  541. out:
  542. /* returning NET_RX_DROP will make the caller function kfree the skb */
  543. return NET_RX_DROP;
  544. }
  545. int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  546. {
  547. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  548. struct batadv_roam_adv_packet *roam_adv_packet;
  549. struct batadv_orig_node *orig_node;
  550. struct ethhdr *ethhdr;
  551. /* drop packet if it has not necessary minimum size */
  552. if (unlikely(!pskb_may_pull(skb,
  553. sizeof(struct batadv_roam_adv_packet))))
  554. goto out;
  555. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  556. /* packet with unicast indication but broadcast recipient */
  557. if (is_broadcast_ether_addr(ethhdr->h_dest))
  558. goto out;
  559. /* packet with broadcast sender address */
  560. if (is_broadcast_ether_addr(ethhdr->h_source))
  561. goto out;
  562. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
  563. roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
  564. if (!batadv_is_my_mac(roam_adv_packet->dst))
  565. return batadv_route_unicast_packet(skb, recv_if);
  566. /* check if it is a backbone gateway. we don't accept
  567. * roaming advertisement from it, as it has the same
  568. * entries as we have.
  569. */
  570. if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
  571. goto out;
  572. orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
  573. if (!orig_node)
  574. goto out;
  575. batadv_dbg(BATADV_DBG_TT, bat_priv,
  576. "Received ROAMING_ADV from %pM (client %pM)\n",
  577. roam_adv_packet->src, roam_adv_packet->client);
  578. batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  579. BATADV_TT_CLIENT_ROAM,
  580. atomic_read(&orig_node->last_ttvn) + 1);
  581. /* Roaming phase starts: I have new information but the ttvn has not
  582. * been incremented yet. This flag will make me check all the incoming
  583. * packets for the correct destination.
  584. */
  585. bat_priv->tt_poss_change = true;
  586. batadv_orig_node_free_ref(orig_node);
  587. out:
  588. /* returning NET_RX_DROP will make the caller function kfree the skb */
  589. return NET_RX_DROP;
  590. }
  591. /* find a suitable router for this originator, and use
  592. * bonding if possible. increases the found neighbors
  593. * refcount.
  594. */
  595. struct batadv_neigh_node *
  596. batadv_find_router(struct batadv_priv *bat_priv,
  597. struct batadv_orig_node *orig_node,
  598. const struct batadv_hard_iface *recv_if)
  599. {
  600. struct batadv_orig_node *primary_orig_node;
  601. struct batadv_orig_node *router_orig;
  602. struct batadv_neigh_node *router;
  603. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  604. int bonding_enabled;
  605. uint8_t *primary_addr;
  606. if (!orig_node)
  607. return NULL;
  608. router = batadv_orig_node_get_router(orig_node);
  609. if (!router)
  610. goto err;
  611. /* without bonding, the first node should
  612. * always choose the default router.
  613. */
  614. bonding_enabled = atomic_read(&bat_priv->bonding);
  615. rcu_read_lock();
  616. /* select default router to output */
  617. router_orig = router->orig_node;
  618. if (!router_orig)
  619. goto err_unlock;
  620. if ((!recv_if) && (!bonding_enabled))
  621. goto return_router;
  622. primary_addr = router_orig->primary_addr;
  623. /* if we have something in the primary_addr, we can search
  624. * for a potential bonding candidate.
  625. */
  626. if (batadv_compare_eth(primary_addr, zero_mac))
  627. goto return_router;
  628. /* find the orig_node which has the primary interface. might
  629. * even be the same as our router_orig in many cases
  630. */
  631. if (batadv_compare_eth(primary_addr, router_orig->orig)) {
  632. primary_orig_node = router_orig;
  633. } else {
  634. primary_orig_node = batadv_orig_hash_find(bat_priv,
  635. primary_addr);
  636. if (!primary_orig_node)
  637. goto return_router;
  638. batadv_orig_node_free_ref(primary_orig_node);
  639. }
  640. /* with less than 2 candidates, we can't do any
  641. * bonding and prefer the original router.
  642. */
  643. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  644. goto return_router;
  645. /* all nodes between should choose a candidate which
  646. * is is not on the interface where the packet came
  647. * in.
  648. */
  649. batadv_neigh_node_free_ref(router);
  650. if (bonding_enabled)
  651. router = batadv_find_bond_router(primary_orig_node, recv_if);
  652. else
  653. router = batadv_find_ifalter_router(primary_orig_node, recv_if);
  654. return_router:
  655. if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
  656. goto err_unlock;
  657. rcu_read_unlock();
  658. return router;
  659. err_unlock:
  660. rcu_read_unlock();
  661. err:
  662. if (router)
  663. batadv_neigh_node_free_ref(router);
  664. return NULL;
  665. }
  666. static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
  667. {
  668. struct ethhdr *ethhdr;
  669. /* drop packet if it has not necessary minimum size */
  670. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  671. return -1;
  672. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  673. /* packet with unicast indication but broadcast recipient */
  674. if (is_broadcast_ether_addr(ethhdr->h_dest))
  675. return -1;
  676. /* packet with broadcast sender address */
  677. if (is_broadcast_ether_addr(ethhdr->h_source))
  678. return -1;
  679. /* not for me */
  680. if (!batadv_is_my_mac(ethhdr->h_dest))
  681. return -1;
  682. return 0;
  683. }
  684. static int batadv_route_unicast_packet(struct sk_buff *skb,
  685. struct batadv_hard_iface *recv_if)
  686. {
  687. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  688. struct batadv_orig_node *orig_node = NULL;
  689. struct batadv_neigh_node *neigh_node = NULL;
  690. struct batadv_unicast_packet *unicast_packet;
  691. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  692. int ret = NET_RX_DROP;
  693. struct sk_buff *new_skb;
  694. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  695. /* TTL exceeded */
  696. if (unicast_packet->header.ttl < 2) {
  697. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  698. ethhdr->h_source, unicast_packet->dest);
  699. goto out;
  700. }
  701. /* get routing information */
  702. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  703. if (!orig_node)
  704. goto out;
  705. /* find_router() increases neigh_nodes refcount if found. */
  706. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  707. if (!neigh_node)
  708. goto out;
  709. /* create a copy of the skb, if needed, to modify it. */
  710. if (skb_cow(skb, ETH_HLEN) < 0)
  711. goto out;
  712. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  713. if (unicast_packet->header.packet_type == BATADV_UNICAST &&
  714. atomic_read(&bat_priv->fragmentation) &&
  715. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  716. ret = batadv_frag_send_skb(skb, bat_priv,
  717. neigh_node->if_incoming,
  718. neigh_node->addr);
  719. goto out;
  720. }
  721. if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
  722. batadv_frag_can_reassemble(skb,
  723. neigh_node->if_incoming->net_dev->mtu)) {
  724. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  725. if (ret == NET_RX_DROP)
  726. goto out;
  727. /* packet was buffered for late merge */
  728. if (!new_skb) {
  729. ret = NET_RX_SUCCESS;
  730. goto out;
  731. }
  732. skb = new_skb;
  733. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  734. }
  735. /* decrement ttl */
  736. unicast_packet->header.ttl--;
  737. /* Update stats counter */
  738. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  739. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  740. skb->len + ETH_HLEN);
  741. /* route it */
  742. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  743. ret = NET_RX_SUCCESS;
  744. out:
  745. if (neigh_node)
  746. batadv_neigh_node_free_ref(neigh_node);
  747. if (orig_node)
  748. batadv_orig_node_free_ref(orig_node);
  749. return ret;
  750. }
  751. static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  752. struct sk_buff *skb) {
  753. uint8_t curr_ttvn;
  754. struct batadv_orig_node *orig_node;
  755. struct ethhdr *ethhdr;
  756. struct batadv_hard_iface *primary_if;
  757. struct batadv_unicast_packet *unicast_packet;
  758. bool tt_poss_change;
  759. int is_old_ttvn;
  760. /* I could need to modify it */
  761. if (skb_cow(skb, sizeof(struct batadv_unicast_packet)) < 0)
  762. return 0;
  763. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  764. if (batadv_is_my_mac(unicast_packet->dest)) {
  765. tt_poss_change = bat_priv->tt_poss_change;
  766. curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  767. } else {
  768. orig_node = batadv_orig_hash_find(bat_priv,
  769. unicast_packet->dest);
  770. if (!orig_node)
  771. return 0;
  772. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  773. tt_poss_change = orig_node->tt_poss_change;
  774. batadv_orig_node_free_ref(orig_node);
  775. }
  776. /* Check whether I have to reroute the packet */
  777. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  778. if (is_old_ttvn || tt_poss_change) {
  779. /* check if there is enough data before accessing it */
  780. if (pskb_may_pull(skb, sizeof(struct batadv_unicast_packet) +
  781. ETH_HLEN) < 0)
  782. return 0;
  783. ethhdr = (struct ethhdr *)(skb->data +
  784. sizeof(struct batadv_unicast_packet));
  785. /* we don't have an updated route for this client, so we should
  786. * not try to reroute the packet!!
  787. */
  788. if (batadv_tt_global_client_is_roaming(bat_priv,
  789. ethhdr->h_dest))
  790. return 1;
  791. orig_node = batadv_transtable_search(bat_priv, NULL,
  792. ethhdr->h_dest);
  793. if (!orig_node) {
  794. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
  795. return 0;
  796. primary_if = batadv_primary_if_get_selected(bat_priv);
  797. if (!primary_if)
  798. return 0;
  799. memcpy(unicast_packet->dest,
  800. primary_if->net_dev->dev_addr, ETH_ALEN);
  801. batadv_hardif_free_ref(primary_if);
  802. } else {
  803. memcpy(unicast_packet->dest, orig_node->orig,
  804. ETH_ALEN);
  805. curr_ttvn = (uint8_t)
  806. atomic_read(&orig_node->last_ttvn);
  807. batadv_orig_node_free_ref(orig_node);
  808. }
  809. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  810. "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
  811. unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
  812. unicast_packet->dest);
  813. unicast_packet->ttvn = curr_ttvn;
  814. }
  815. return 1;
  816. }
  817. int batadv_recv_unicast_packet(struct sk_buff *skb,
  818. struct batadv_hard_iface *recv_if)
  819. {
  820. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  821. struct batadv_unicast_packet *unicast_packet;
  822. int hdr_size = sizeof(*unicast_packet);
  823. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  824. return NET_RX_DROP;
  825. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  826. return NET_RX_DROP;
  827. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  828. /* packet for me */
  829. if (batadv_is_my_mac(unicast_packet->dest)) {
  830. batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
  831. hdr_size);
  832. return NET_RX_SUCCESS;
  833. }
  834. return batadv_route_unicast_packet(skb, recv_if);
  835. }
  836. int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
  837. struct batadv_hard_iface *recv_if)
  838. {
  839. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  840. struct batadv_unicast_frag_packet *unicast_packet;
  841. int hdr_size = sizeof(*unicast_packet);
  842. struct sk_buff *new_skb = NULL;
  843. int ret;
  844. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  845. return NET_RX_DROP;
  846. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  847. return NET_RX_DROP;
  848. unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
  849. /* packet for me */
  850. if (batadv_is_my_mac(unicast_packet->dest)) {
  851. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  852. if (ret == NET_RX_DROP)
  853. return NET_RX_DROP;
  854. /* packet was buffered for late merge */
  855. if (!new_skb)
  856. return NET_RX_SUCCESS;
  857. batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
  858. sizeof(struct batadv_unicast_packet));
  859. return NET_RX_SUCCESS;
  860. }
  861. return batadv_route_unicast_packet(skb, recv_if);
  862. }
  863. int batadv_recv_bcast_packet(struct sk_buff *skb,
  864. struct batadv_hard_iface *recv_if)
  865. {
  866. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  867. struct batadv_orig_node *orig_node = NULL;
  868. struct batadv_bcast_packet *bcast_packet;
  869. struct ethhdr *ethhdr;
  870. int hdr_size = sizeof(*bcast_packet);
  871. int ret = NET_RX_DROP;
  872. int32_t seq_diff;
  873. /* drop packet if it has not necessary minimum size */
  874. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  875. goto out;
  876. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  877. /* packet with broadcast indication but unicast recipient */
  878. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  879. goto out;
  880. /* packet with broadcast sender address */
  881. if (is_broadcast_ether_addr(ethhdr->h_source))
  882. goto out;
  883. /* ignore broadcasts sent by myself */
  884. if (batadv_is_my_mac(ethhdr->h_source))
  885. goto out;
  886. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  887. /* ignore broadcasts originated by myself */
  888. if (batadv_is_my_mac(bcast_packet->orig))
  889. goto out;
  890. if (bcast_packet->header.ttl < 2)
  891. goto out;
  892. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  893. if (!orig_node)
  894. goto out;
  895. spin_lock_bh(&orig_node->bcast_seqno_lock);
  896. /* check whether the packet is a duplicate */
  897. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  898. ntohl(bcast_packet->seqno)))
  899. goto spin_unlock;
  900. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  901. /* check whether the packet is old and the host just restarted. */
  902. if (batadv_window_protected(bat_priv, seq_diff,
  903. &orig_node->bcast_seqno_reset))
  904. goto spin_unlock;
  905. /* mark broadcast in flood history, update window position
  906. * if required.
  907. */
  908. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  909. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  910. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  911. /* check whether this has been sent by another originator before */
  912. if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
  913. goto out;
  914. /* rebroadcast packet */
  915. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  916. /* don't hand the broadcast up if it is from an originator
  917. * from the same backbone.
  918. */
  919. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  920. goto out;
  921. /* broadcast for me */
  922. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  923. ret = NET_RX_SUCCESS;
  924. goto out;
  925. spin_unlock:
  926. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  927. out:
  928. if (orig_node)
  929. batadv_orig_node_free_ref(orig_node);
  930. return ret;
  931. }
  932. int batadv_recv_vis_packet(struct sk_buff *skb,
  933. struct batadv_hard_iface *recv_if)
  934. {
  935. struct batadv_vis_packet *vis_packet;
  936. struct ethhdr *ethhdr;
  937. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  938. int hdr_size = sizeof(*vis_packet);
  939. /* keep skb linear */
  940. if (skb_linearize(skb) < 0)
  941. return NET_RX_DROP;
  942. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  943. return NET_RX_DROP;
  944. vis_packet = (struct batadv_vis_packet *)skb->data;
  945. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  946. /* not for me */
  947. if (!batadv_is_my_mac(ethhdr->h_dest))
  948. return NET_RX_DROP;
  949. /* ignore own packets */
  950. if (batadv_is_my_mac(vis_packet->vis_orig))
  951. return NET_RX_DROP;
  952. if (batadv_is_my_mac(vis_packet->sender_orig))
  953. return NET_RX_DROP;
  954. switch (vis_packet->vis_type) {
  955. case BATADV_VIS_TYPE_SERVER_SYNC:
  956. batadv_receive_server_sync_packet(bat_priv, vis_packet,
  957. skb_headlen(skb));
  958. break;
  959. case BATADV_VIS_TYPE_CLIENT_UPDATE:
  960. batadv_receive_client_update_packet(bat_priv, vis_packet,
  961. skb_headlen(skb));
  962. break;
  963. default: /* ignore unknown packet */
  964. break;
  965. }
  966. /* We take a copy of the data in the packet, so we should
  967. * always free the skbuf.
  968. */
  969. return NET_RX_DROP;
  970. }