routing.c 33 KB

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