routing.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
  471. {
  472. struct ethhdr *ethhdr;
  473. /* drop packet if it has not necessary minimum size */
  474. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  475. return -1;
  476. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  477. /* packet with unicast indication but broadcast recipient */
  478. if (is_broadcast_ether_addr(ethhdr->h_dest))
  479. return -1;
  480. /* packet with broadcast sender address */
  481. if (is_broadcast_ether_addr(ethhdr->h_source))
  482. return -1;
  483. /* not for me */
  484. if (!batadv_is_my_mac(ethhdr->h_dest))
  485. return -1;
  486. return 0;
  487. }
  488. int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  489. {
  490. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  491. struct batadv_tt_query_packet *tt_query;
  492. uint16_t tt_size;
  493. int hdr_size = sizeof(*tt_query);
  494. char tt_flag;
  495. size_t packet_size;
  496. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  497. return NET_RX_DROP;
  498. /* I could need to modify it */
  499. if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
  500. goto out;
  501. tt_query = (struct batadv_tt_query_packet *)skb->data;
  502. switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
  503. case BATADV_TT_REQUEST:
  504. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
  505. /* If we cannot provide an answer the tt_request is
  506. * forwarded
  507. */
  508. if (!batadv_send_tt_response(bat_priv, tt_query)) {
  509. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  510. tt_flag = 'F';
  511. else
  512. tt_flag = '.';
  513. batadv_dbg(BATADV_DBG_TT, bat_priv,
  514. "Routing TT_REQUEST to %pM [%c]\n",
  515. tt_query->dst,
  516. tt_flag);
  517. return batadv_route_unicast_packet(skb, recv_if);
  518. }
  519. break;
  520. case BATADV_TT_RESPONSE:
  521. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
  522. if (batadv_is_my_mac(tt_query->dst)) {
  523. /* packet needs to be linearized to access the TT
  524. * changes
  525. */
  526. if (skb_linearize(skb) < 0)
  527. goto out;
  528. /* skb_linearize() possibly changed skb->data */
  529. tt_query = (struct batadv_tt_query_packet *)skb->data;
  530. tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
  531. /* Ensure we have all the claimed data */
  532. packet_size = sizeof(struct batadv_tt_query_packet);
  533. packet_size += tt_size;
  534. if (unlikely(skb_headlen(skb) < packet_size))
  535. goto out;
  536. batadv_handle_tt_response(bat_priv, tt_query);
  537. } else {
  538. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  539. tt_flag = 'F';
  540. else
  541. tt_flag = '.';
  542. batadv_dbg(BATADV_DBG_TT, bat_priv,
  543. "Routing TT_RESPONSE to %pM [%c]\n",
  544. tt_query->dst,
  545. tt_flag);
  546. return batadv_route_unicast_packet(skb, recv_if);
  547. }
  548. break;
  549. }
  550. out:
  551. /* returning NET_RX_DROP will make the caller function kfree the skb */
  552. return NET_RX_DROP;
  553. }
  554. int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  555. {
  556. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  557. struct batadv_roam_adv_packet *roam_adv_packet;
  558. struct batadv_orig_node *orig_node;
  559. struct ethhdr *ethhdr;
  560. /* drop packet if it has not necessary minimum size */
  561. if (unlikely(!pskb_may_pull(skb,
  562. sizeof(struct batadv_roam_adv_packet))))
  563. goto out;
  564. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  565. /* packet with unicast indication but broadcast recipient */
  566. if (is_broadcast_ether_addr(ethhdr->h_dest))
  567. goto out;
  568. /* packet with broadcast sender address */
  569. if (is_broadcast_ether_addr(ethhdr->h_source))
  570. goto out;
  571. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
  572. roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
  573. if (!batadv_is_my_mac(roam_adv_packet->dst))
  574. return batadv_route_unicast_packet(skb, recv_if);
  575. /* check if it is a backbone gateway. we don't accept
  576. * roaming advertisement from it, as it has the same
  577. * entries as we have.
  578. */
  579. if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
  580. goto out;
  581. orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
  582. if (!orig_node)
  583. goto out;
  584. batadv_dbg(BATADV_DBG_TT, bat_priv,
  585. "Received ROAMING_ADV from %pM (client %pM)\n",
  586. roam_adv_packet->src, roam_adv_packet->client);
  587. batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  588. BATADV_TT_CLIENT_ROAM,
  589. atomic_read(&orig_node->last_ttvn) + 1);
  590. /* Roaming phase starts: I have new information but the ttvn has not
  591. * been incremented yet. This flag will make me check all the incoming
  592. * packets for the correct destination.
  593. */
  594. bat_priv->tt.poss_change = true;
  595. batadv_orig_node_free_ref(orig_node);
  596. out:
  597. /* returning NET_RX_DROP will make the caller function kfree the skb */
  598. return NET_RX_DROP;
  599. }
  600. /* find a suitable router for this originator, and use
  601. * bonding if possible. increases the found neighbors
  602. * refcount.
  603. */
  604. struct batadv_neigh_node *
  605. batadv_find_router(struct batadv_priv *bat_priv,
  606. struct batadv_orig_node *orig_node,
  607. const struct batadv_hard_iface *recv_if)
  608. {
  609. struct batadv_orig_node *primary_orig_node;
  610. struct batadv_orig_node *router_orig;
  611. struct batadv_neigh_node *router;
  612. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  613. int bonding_enabled;
  614. uint8_t *primary_addr;
  615. if (!orig_node)
  616. return NULL;
  617. router = batadv_orig_node_get_router(orig_node);
  618. if (!router)
  619. goto err;
  620. /* without bonding, the first node should
  621. * always choose the default router.
  622. */
  623. bonding_enabled = atomic_read(&bat_priv->bonding);
  624. rcu_read_lock();
  625. /* select default router to output */
  626. router_orig = router->orig_node;
  627. if (!router_orig)
  628. goto err_unlock;
  629. if ((!recv_if) && (!bonding_enabled))
  630. goto return_router;
  631. primary_addr = router_orig->primary_addr;
  632. /* if we have something in the primary_addr, we can search
  633. * for a potential bonding candidate.
  634. */
  635. if (batadv_compare_eth(primary_addr, zero_mac))
  636. goto return_router;
  637. /* find the orig_node which has the primary interface. might
  638. * even be the same as our router_orig in many cases
  639. */
  640. if (batadv_compare_eth(primary_addr, router_orig->orig)) {
  641. primary_orig_node = router_orig;
  642. } else {
  643. primary_orig_node = batadv_orig_hash_find(bat_priv,
  644. primary_addr);
  645. if (!primary_orig_node)
  646. goto return_router;
  647. batadv_orig_node_free_ref(primary_orig_node);
  648. }
  649. /* with less than 2 candidates, we can't do any
  650. * bonding and prefer the original router.
  651. */
  652. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  653. goto return_router;
  654. /* all nodes between should choose a candidate which
  655. * is is not on the interface where the packet came
  656. * in.
  657. */
  658. batadv_neigh_node_free_ref(router);
  659. if (bonding_enabled)
  660. router = batadv_find_bond_router(primary_orig_node, recv_if);
  661. else
  662. router = batadv_find_ifalter_router(primary_orig_node, recv_if);
  663. return_router:
  664. if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
  665. goto err_unlock;
  666. rcu_read_unlock();
  667. return router;
  668. err_unlock:
  669. rcu_read_unlock();
  670. err:
  671. if (router)
  672. batadv_neigh_node_free_ref(router);
  673. return NULL;
  674. }
  675. static int batadv_route_unicast_packet(struct sk_buff *skb,
  676. struct batadv_hard_iface *recv_if)
  677. {
  678. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  679. struct batadv_orig_node *orig_node = NULL;
  680. struct batadv_neigh_node *neigh_node = NULL;
  681. struct batadv_unicast_packet *unicast_packet;
  682. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  683. int ret = NET_RX_DROP;
  684. struct sk_buff *new_skb;
  685. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  686. /* TTL exceeded */
  687. if (unicast_packet->header.ttl < 2) {
  688. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  689. ethhdr->h_source, unicast_packet->dest);
  690. goto out;
  691. }
  692. /* get routing information */
  693. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  694. if (!orig_node)
  695. goto out;
  696. /* find_router() increases neigh_nodes refcount if found. */
  697. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  698. if (!neigh_node)
  699. goto out;
  700. /* create a copy of the skb, if needed, to modify it. */
  701. if (skb_cow(skb, ETH_HLEN) < 0)
  702. goto out;
  703. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  704. if (unicast_packet->header.packet_type == BATADV_UNICAST &&
  705. atomic_read(&bat_priv->fragmentation) &&
  706. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  707. ret = batadv_frag_send_skb(skb, bat_priv,
  708. neigh_node->if_incoming,
  709. neigh_node->addr);
  710. goto out;
  711. }
  712. if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
  713. batadv_frag_can_reassemble(skb,
  714. neigh_node->if_incoming->net_dev->mtu)) {
  715. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  716. if (ret == NET_RX_DROP)
  717. goto out;
  718. /* packet was buffered for late merge */
  719. if (!new_skb) {
  720. ret = NET_RX_SUCCESS;
  721. goto out;
  722. }
  723. skb = new_skb;
  724. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  725. }
  726. /* decrement ttl */
  727. unicast_packet->header.ttl--;
  728. /* Update stats counter */
  729. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  730. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  731. skb->len + ETH_HLEN);
  732. /* route it */
  733. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  734. ret = NET_RX_SUCCESS;
  735. out:
  736. if (neigh_node)
  737. batadv_neigh_node_free_ref(neigh_node);
  738. if (orig_node)
  739. batadv_orig_node_free_ref(orig_node);
  740. return ret;
  741. }
  742. static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  743. struct sk_buff *skb) {
  744. uint8_t curr_ttvn;
  745. struct batadv_orig_node *orig_node;
  746. struct ethhdr *ethhdr;
  747. struct batadv_hard_iface *primary_if;
  748. struct batadv_unicast_packet *unicast_packet;
  749. bool tt_poss_change;
  750. int is_old_ttvn;
  751. /* I could need to modify it */
  752. if (skb_cow(skb, sizeof(struct batadv_unicast_packet)) < 0)
  753. return 0;
  754. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  755. if (batadv_is_my_mac(unicast_packet->dest)) {
  756. tt_poss_change = bat_priv->tt.poss_change;
  757. curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  758. } else {
  759. orig_node = batadv_orig_hash_find(bat_priv,
  760. unicast_packet->dest);
  761. if (!orig_node)
  762. return 0;
  763. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  764. tt_poss_change = orig_node->tt_poss_change;
  765. batadv_orig_node_free_ref(orig_node);
  766. }
  767. /* Check whether I have to reroute the packet */
  768. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  769. if (is_old_ttvn || tt_poss_change) {
  770. /* check if there is enough data before accessing it */
  771. if (pskb_may_pull(skb, sizeof(struct batadv_unicast_packet) +
  772. ETH_HLEN) < 0)
  773. return 0;
  774. ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet));
  775. /* we don't have an updated route for this client, so we should
  776. * not try to reroute the packet!!
  777. */
  778. if (batadv_tt_global_client_is_roaming(bat_priv,
  779. ethhdr->h_dest))
  780. return 1;
  781. orig_node = batadv_transtable_search(bat_priv, NULL,
  782. ethhdr->h_dest);
  783. if (!orig_node) {
  784. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
  785. return 0;
  786. primary_if = batadv_primary_if_get_selected(bat_priv);
  787. if (!primary_if)
  788. return 0;
  789. memcpy(unicast_packet->dest,
  790. primary_if->net_dev->dev_addr, ETH_ALEN);
  791. batadv_hardif_free_ref(primary_if);
  792. } else {
  793. memcpy(unicast_packet->dest, orig_node->orig,
  794. ETH_ALEN);
  795. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  796. batadv_orig_node_free_ref(orig_node);
  797. }
  798. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  799. "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
  800. unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
  801. unicast_packet->dest);
  802. unicast_packet->ttvn = curr_ttvn;
  803. }
  804. return 1;
  805. }
  806. int batadv_recv_unicast_packet(struct sk_buff *skb,
  807. struct batadv_hard_iface *recv_if)
  808. {
  809. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  810. struct batadv_unicast_packet *unicast_packet;
  811. int hdr_size = sizeof(*unicast_packet);
  812. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  813. return NET_RX_DROP;
  814. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  815. return NET_RX_DROP;
  816. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  817. /* packet for me */
  818. if (batadv_is_my_mac(unicast_packet->dest)) {
  819. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  820. NULL);
  821. return NET_RX_SUCCESS;
  822. }
  823. return batadv_route_unicast_packet(skb, recv_if);
  824. }
  825. int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
  826. struct batadv_hard_iface *recv_if)
  827. {
  828. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  829. struct batadv_unicast_frag_packet *unicast_packet;
  830. int hdr_size = sizeof(*unicast_packet);
  831. struct sk_buff *new_skb = NULL;
  832. int ret;
  833. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  834. return NET_RX_DROP;
  835. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  836. return NET_RX_DROP;
  837. unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
  838. /* packet for me */
  839. if (batadv_is_my_mac(unicast_packet->dest)) {
  840. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  841. if (ret == NET_RX_DROP)
  842. return NET_RX_DROP;
  843. /* packet was buffered for late merge */
  844. if (!new_skb)
  845. return NET_RX_SUCCESS;
  846. batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
  847. sizeof(struct batadv_unicast_packet), NULL);
  848. return NET_RX_SUCCESS;
  849. }
  850. return batadv_route_unicast_packet(skb, recv_if);
  851. }
  852. int batadv_recv_bcast_packet(struct sk_buff *skb,
  853. struct batadv_hard_iface *recv_if)
  854. {
  855. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  856. struct batadv_orig_node *orig_node = NULL;
  857. struct batadv_bcast_packet *bcast_packet;
  858. struct ethhdr *ethhdr;
  859. int hdr_size = sizeof(*bcast_packet);
  860. int ret = NET_RX_DROP;
  861. int32_t seq_diff;
  862. /* drop packet if it has not necessary minimum size */
  863. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  864. goto out;
  865. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  866. /* packet with broadcast indication but unicast recipient */
  867. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  868. goto out;
  869. /* packet with broadcast sender address */
  870. if (is_broadcast_ether_addr(ethhdr->h_source))
  871. goto out;
  872. /* ignore broadcasts sent by myself */
  873. if (batadv_is_my_mac(ethhdr->h_source))
  874. goto out;
  875. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  876. /* ignore broadcasts originated by myself */
  877. if (batadv_is_my_mac(bcast_packet->orig))
  878. goto out;
  879. if (bcast_packet->header.ttl < 2)
  880. goto out;
  881. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  882. if (!orig_node)
  883. goto out;
  884. spin_lock_bh(&orig_node->bcast_seqno_lock);
  885. /* check whether the packet is a duplicate */
  886. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  887. ntohl(bcast_packet->seqno)))
  888. goto spin_unlock;
  889. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  890. /* check whether the packet is old and the host just restarted. */
  891. if (batadv_window_protected(bat_priv, seq_diff,
  892. &orig_node->bcast_seqno_reset))
  893. goto spin_unlock;
  894. /* mark broadcast in flood history, update window position
  895. * if required.
  896. */
  897. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  898. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  899. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  900. /* check whether this has been sent by another originator before */
  901. if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
  902. goto out;
  903. /* rebroadcast packet */
  904. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  905. /* don't hand the broadcast up if it is from an originator
  906. * from the same backbone.
  907. */
  908. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  909. goto out;
  910. /* broadcast for me */
  911. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  912. orig_node);
  913. ret = NET_RX_SUCCESS;
  914. goto out;
  915. spin_unlock:
  916. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  917. out:
  918. if (orig_node)
  919. batadv_orig_node_free_ref(orig_node);
  920. return ret;
  921. }
  922. int batadv_recv_vis_packet(struct sk_buff *skb,
  923. struct batadv_hard_iface *recv_if)
  924. {
  925. struct batadv_vis_packet *vis_packet;
  926. struct ethhdr *ethhdr;
  927. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  928. int hdr_size = sizeof(*vis_packet);
  929. /* keep skb linear */
  930. if (skb_linearize(skb) < 0)
  931. return NET_RX_DROP;
  932. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  933. return NET_RX_DROP;
  934. vis_packet = (struct batadv_vis_packet *)skb->data;
  935. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  936. /* not for me */
  937. if (!batadv_is_my_mac(ethhdr->h_dest))
  938. return NET_RX_DROP;
  939. /* ignore own packets */
  940. if (batadv_is_my_mac(vis_packet->vis_orig))
  941. return NET_RX_DROP;
  942. if (batadv_is_my_mac(vis_packet->sender_orig))
  943. return NET_RX_DROP;
  944. switch (vis_packet->vis_type) {
  945. case BATADV_VIS_TYPE_SERVER_SYNC:
  946. batadv_receive_server_sync_packet(bat_priv, vis_packet,
  947. skb_headlen(skb));
  948. break;
  949. case BATADV_VIS_TYPE_CLIENT_UPDATE:
  950. batadv_receive_client_update_packet(bat_priv, vis_packet,
  951. skb_headlen(skb));
  952. break;
  953. default: /* ignore unknown packet */
  954. break;
  955. }
  956. /* We take a copy of the data in the packet, so we should
  957. * always free the skbuf.
  958. */
  959. return NET_RX_DROP;
  960. }