routing.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /*
  2. * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "routing.h"
  23. #include "send.h"
  24. #include "soft-interface.h"
  25. #include "hard-interface.h"
  26. #include "icmp_socket.h"
  27. #include "translation-table.h"
  28. #include "originator.h"
  29. #include "vis.h"
  30. #include "unicast.h"
  31. #include "bat_ogm.h"
  32. void slide_own_bcast_window(struct hard_iface *hard_iface)
  33. {
  34. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  35. struct hashtable_t *hash = bat_priv->orig_hash;
  36. struct hlist_node *node;
  37. struct hlist_head *head;
  38. struct orig_node *orig_node;
  39. unsigned long *word;
  40. uint32_t i;
  41. size_t word_index;
  42. for (i = 0; i < hash->size; i++) {
  43. head = &hash->table[i];
  44. rcu_read_lock();
  45. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  46. spin_lock_bh(&orig_node->ogm_cnt_lock);
  47. word_index = hard_iface->if_num * NUM_WORDS;
  48. word = &(orig_node->bcast_own[word_index]);
  49. bit_get_packet(bat_priv, word, 1, 0);
  50. orig_node->bcast_own_sum[hard_iface->if_num] =
  51. bit_packet_count(word);
  52. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  53. }
  54. rcu_read_unlock();
  55. }
  56. }
  57. static void _update_route(struct bat_priv *bat_priv,
  58. struct orig_node *orig_node,
  59. struct neigh_node *neigh_node)
  60. {
  61. struct neigh_node *curr_router;
  62. curr_router = orig_node_get_router(orig_node);
  63. /* route deleted */
  64. if ((curr_router) && (!neigh_node)) {
  65. bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
  66. orig_node->orig);
  67. tt_global_del_orig(bat_priv, orig_node,
  68. "Deleted route towards originator");
  69. /* route added */
  70. } else if ((!curr_router) && (neigh_node)) {
  71. bat_dbg(DBG_ROUTES, bat_priv,
  72. "Adding route towards: %pM (via %pM)\n",
  73. orig_node->orig, neigh_node->addr);
  74. /* route changed */
  75. } else if (neigh_node && curr_router) {
  76. bat_dbg(DBG_ROUTES, bat_priv,
  77. "Changing route towards: %pM "
  78. "(now via %pM - was via %pM)\n",
  79. orig_node->orig, neigh_node->addr,
  80. curr_router->addr);
  81. }
  82. if (curr_router)
  83. 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. neigh_node_free_ref(curr_router);
  93. }
  94. void update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
  95. struct neigh_node *neigh_node)
  96. {
  97. struct neigh_node *router = NULL;
  98. if (!orig_node)
  99. goto out;
  100. router = orig_node_get_router(orig_node);
  101. if (router != neigh_node)
  102. _update_route(bat_priv, orig_node, neigh_node);
  103. out:
  104. if (router)
  105. neigh_node_free_ref(router);
  106. }
  107. /* caller must hold the neigh_list_lock */
  108. void bonding_candidate_del(struct orig_node *orig_node,
  109. struct neigh_node *neigh_node)
  110. {
  111. /* this neighbor is not part of our candidate list */
  112. if (list_empty(&neigh_node->bonding_list))
  113. goto out;
  114. list_del_rcu(&neigh_node->bonding_list);
  115. INIT_LIST_HEAD(&neigh_node->bonding_list);
  116. neigh_node_free_ref(neigh_node);
  117. atomic_dec(&orig_node->bond_candidates);
  118. out:
  119. return;
  120. }
  121. void bonding_candidate_add(struct orig_node *orig_node,
  122. struct neigh_node *neigh_node)
  123. {
  124. struct hlist_node *node;
  125. struct neigh_node *tmp_neigh_node, *router = NULL;
  126. uint8_t interference_candidate = 0;
  127. spin_lock_bh(&orig_node->neigh_list_lock);
  128. /* only consider if it has the same primary address ... */
  129. if (!compare_eth(orig_node->orig,
  130. neigh_node->orig_node->primary_addr))
  131. goto candidate_del;
  132. router = orig_node_get_router(orig_node);
  133. if (!router)
  134. goto candidate_del;
  135. /* ... and is good enough to be considered */
  136. if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
  137. goto candidate_del;
  138. /**
  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. if (list_empty(&tmp_neigh_node->bonding_list))
  150. continue;
  151. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  152. (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
  153. interference_candidate = 1;
  154. break;
  155. }
  156. }
  157. /* don't care further if it is an interference candidate */
  158. if (interference_candidate)
  159. goto candidate_del;
  160. /* this neighbor already is part of our candidate list */
  161. if (!list_empty(&neigh_node->bonding_list))
  162. goto out;
  163. if (!atomic_inc_not_zero(&neigh_node->refcount))
  164. goto out;
  165. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  166. atomic_inc(&orig_node->bond_candidates);
  167. goto out;
  168. candidate_del:
  169. bonding_candidate_del(orig_node, neigh_node);
  170. out:
  171. spin_unlock_bh(&orig_node->neigh_list_lock);
  172. if (router)
  173. neigh_node_free_ref(router);
  174. }
  175. /* copy primary address for bonding */
  176. void bonding_save_primary(const struct orig_node *orig_node,
  177. struct orig_node *orig_neigh_node,
  178. const struct batman_ogm_packet *batman_ogm_packet)
  179. {
  180. if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
  181. return;
  182. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  183. }
  184. /* checks whether the host restarted and is in the protection time.
  185. * returns:
  186. * 0 if the packet is to be accepted
  187. * 1 if the packet is to be ignored.
  188. */
  189. int window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
  190. unsigned long *last_reset)
  191. {
  192. if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
  193. || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
  194. if (time_after(jiffies, *last_reset +
  195. msecs_to_jiffies(RESET_PROTECTION_MS))) {
  196. *last_reset = jiffies;
  197. bat_dbg(DBG_BATMAN, bat_priv,
  198. "old packet received, start protection\n");
  199. return 0;
  200. } else
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. int recv_bat_ogm_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
  206. {
  207. struct ethhdr *ethhdr;
  208. /* drop packet if it has not necessary minimum size */
  209. if (unlikely(!pskb_may_pull(skb, BATMAN_OGM_LEN)))
  210. return NET_RX_DROP;
  211. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  212. /* packet with broadcast indication but unicast recipient */
  213. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  214. return NET_RX_DROP;
  215. /* packet with broadcast sender address */
  216. if (is_broadcast_ether_addr(ethhdr->h_source))
  217. return NET_RX_DROP;
  218. /* create a copy of the skb, if needed, to modify it. */
  219. if (skb_cow(skb, 0) < 0)
  220. return NET_RX_DROP;
  221. /* keep skb linear */
  222. if (skb_linearize(skb) < 0)
  223. return NET_RX_DROP;
  224. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  225. bat_ogm_receive(ethhdr, skb->data, skb_headlen(skb), hard_iface);
  226. kfree_skb(skb);
  227. return NET_RX_SUCCESS;
  228. }
  229. static int recv_my_icmp_packet(struct bat_priv *bat_priv,
  230. struct sk_buff *skb, size_t icmp_len)
  231. {
  232. struct hard_iface *primary_if = NULL;
  233. struct orig_node *orig_node = NULL;
  234. struct neigh_node *router = NULL;
  235. struct icmp_packet_rr *icmp_packet;
  236. int ret = NET_RX_DROP;
  237. icmp_packet = (struct icmp_packet_rr *)skb->data;
  238. /* add data to device queue */
  239. if (icmp_packet->msg_type != ECHO_REQUEST) {
  240. bat_socket_receive_packet(icmp_packet, icmp_len);
  241. goto out;
  242. }
  243. primary_if = 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 = orig_hash_find(bat_priv, icmp_packet->orig);
  249. if (!orig_node)
  250. goto out;
  251. router = 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, sizeof(struct ethhdr)) < 0)
  256. goto out;
  257. icmp_packet = (struct 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 = ECHO_REPLY;
  261. icmp_packet->ttl = TTL;
  262. send_skb_packet(skb, router->if_incoming, router->addr);
  263. ret = NET_RX_SUCCESS;
  264. out:
  265. if (primary_if)
  266. hardif_free_ref(primary_if);
  267. if (router)
  268. neigh_node_free_ref(router);
  269. if (orig_node)
  270. orig_node_free_ref(orig_node);
  271. return ret;
  272. }
  273. static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
  274. struct sk_buff *skb)
  275. {
  276. struct hard_iface *primary_if = NULL;
  277. struct orig_node *orig_node = NULL;
  278. struct neigh_node *router = NULL;
  279. struct icmp_packet *icmp_packet;
  280. int ret = NET_RX_DROP;
  281. icmp_packet = (struct icmp_packet *)skb->data;
  282. /* send TTL exceeded if packet is an echo request (traceroute) */
  283. if (icmp_packet->msg_type != ECHO_REQUEST) {
  284. pr_debug("Warning - can't forward icmp packet from %pM to "
  285. "%pM: ttl exceeded\n", icmp_packet->orig,
  286. icmp_packet->dst);
  287. goto out;
  288. }
  289. primary_if = primary_if_get_selected(bat_priv);
  290. if (!primary_if)
  291. goto out;
  292. /* get routing information */
  293. orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
  294. if (!orig_node)
  295. goto out;
  296. router = 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, sizeof(struct ethhdr)) < 0)
  301. goto out;
  302. icmp_packet = (struct 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 = TTL_EXCEEDED;
  306. icmp_packet->ttl = TTL;
  307. send_skb_packet(skb, router->if_incoming, router->addr);
  308. ret = NET_RX_SUCCESS;
  309. out:
  310. if (primary_if)
  311. hardif_free_ref(primary_if);
  312. if (router)
  313. neigh_node_free_ref(router);
  314. if (orig_node)
  315. orig_node_free_ref(orig_node);
  316. return ret;
  317. }
  318. int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  319. {
  320. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  321. struct icmp_packet_rr *icmp_packet;
  322. struct ethhdr *ethhdr;
  323. struct orig_node *orig_node = NULL;
  324. struct neigh_node *router = NULL;
  325. int hdr_size = sizeof(struct icmp_packet);
  326. int ret = NET_RX_DROP;
  327. /**
  328. * we truncate all incoming icmp packets if they don't match our size
  329. */
  330. if (skb->len >= sizeof(struct icmp_packet_rr))
  331. hdr_size = sizeof(struct icmp_packet_rr);
  332. /* drop packet if it has not necessary minimum size */
  333. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  334. goto out;
  335. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  336. /* packet with unicast indication but broadcast recipient */
  337. if (is_broadcast_ether_addr(ethhdr->h_dest))
  338. goto out;
  339. /* packet with broadcast sender address */
  340. if (is_broadcast_ether_addr(ethhdr->h_source))
  341. goto out;
  342. /* not for me */
  343. if (!is_my_mac(ethhdr->h_dest))
  344. goto out;
  345. icmp_packet = (struct icmp_packet_rr *)skb->data;
  346. /* add record route information if not full */
  347. if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
  348. (icmp_packet->rr_cur < BAT_RR_LEN)) {
  349. memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
  350. ethhdr->h_dest, ETH_ALEN);
  351. icmp_packet->rr_cur++;
  352. }
  353. /* packet for me */
  354. if (is_my_mac(icmp_packet->dst))
  355. return recv_my_icmp_packet(bat_priv, skb, hdr_size);
  356. /* TTL exceeded */
  357. if (icmp_packet->ttl < 2)
  358. return recv_icmp_ttl_exceeded(bat_priv, skb);
  359. /* get routing information */
  360. orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
  361. if (!orig_node)
  362. goto out;
  363. router = orig_node_get_router(orig_node);
  364. if (!router)
  365. goto out;
  366. /* create a copy of the skb, if needed, to modify it. */
  367. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  368. goto out;
  369. icmp_packet = (struct icmp_packet_rr *)skb->data;
  370. /* decrement ttl */
  371. icmp_packet->ttl--;
  372. /* route it */
  373. send_skb_packet(skb, router->if_incoming, router->addr);
  374. ret = NET_RX_SUCCESS;
  375. out:
  376. if (router)
  377. neigh_node_free_ref(router);
  378. if (orig_node)
  379. orig_node_free_ref(orig_node);
  380. return ret;
  381. }
  382. /* In the bonding case, send the packets in a round
  383. * robin fashion over the remaining interfaces.
  384. *
  385. * This method rotates the bonding list and increases the
  386. * returned router's refcount. */
  387. static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
  388. const struct hard_iface *recv_if)
  389. {
  390. struct neigh_node *tmp_neigh_node;
  391. struct 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. spin_lock_bh(&primary_orig->neigh_list_lock);
  414. /* this is a list_move(), which unfortunately
  415. * does not exist as rcu version */
  416. list_del_rcu(&primary_orig->bond_list);
  417. list_add_rcu(&primary_orig->bond_list,
  418. &router->bonding_list);
  419. spin_unlock_bh(&primary_orig->neigh_list_lock);
  420. out:
  421. rcu_read_unlock();
  422. return router;
  423. }
  424. /* Interface Alternating: Use the best of the
  425. * remaining candidates which are not using
  426. * this interface.
  427. *
  428. * Increases the returned router's refcount */
  429. static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
  430. const struct hard_iface *recv_if)
  431. {
  432. struct neigh_node *tmp_neigh_node;
  433. struct neigh_node *router = NULL, *first_candidate = NULL;
  434. rcu_read_lock();
  435. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  436. bonding_list) {
  437. if (!first_candidate)
  438. first_candidate = tmp_neigh_node;
  439. /* recv_if == NULL on the first node. */
  440. if (tmp_neigh_node->if_incoming == recv_if)
  441. continue;
  442. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  443. continue;
  444. /* if we don't have a router yet
  445. * or this one is better, choose it. */
  446. if ((!router) ||
  447. (tmp_neigh_node->tq_avg > router->tq_avg)) {
  448. /* decrement refcount of
  449. * previously selected router */
  450. if (router)
  451. neigh_node_free_ref(router);
  452. router = tmp_neigh_node;
  453. atomic_inc_not_zero(&router->refcount);
  454. }
  455. neigh_node_free_ref(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. int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
  465. {
  466. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  467. struct tt_query_packet *tt_query;
  468. struct ethhdr *ethhdr;
  469. /* drop packet if it has not necessary minimum size */
  470. if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
  471. goto out;
  472. /* I could need to modify it */
  473. if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
  474. goto out;
  475. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  476. /* packet with unicast indication but broadcast recipient */
  477. if (is_broadcast_ether_addr(ethhdr->h_dest))
  478. goto out;
  479. /* packet with broadcast sender address */
  480. if (is_broadcast_ether_addr(ethhdr->h_source))
  481. goto out;
  482. tt_query = (struct tt_query_packet *)skb->data;
  483. tt_query->tt_data = ntohs(tt_query->tt_data);
  484. switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
  485. case TT_REQUEST:
  486. /* If we cannot provide an answer the tt_request is
  487. * forwarded */
  488. if (!send_tt_response(bat_priv, tt_query)) {
  489. bat_dbg(DBG_TT, bat_priv,
  490. "Routing TT_REQUEST to %pM [%c]\n",
  491. tt_query->dst,
  492. (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
  493. tt_query->tt_data = htons(tt_query->tt_data);
  494. return route_unicast_packet(skb, recv_if);
  495. }
  496. break;
  497. case TT_RESPONSE:
  498. if (is_my_mac(tt_query->dst)) {
  499. /* packet needs to be linearized to access the TT
  500. * changes */
  501. if (skb_linearize(skb) < 0)
  502. goto out;
  503. handle_tt_response(bat_priv, tt_query);
  504. } else {
  505. bat_dbg(DBG_TT, bat_priv,
  506. "Routing TT_RESPONSE to %pM [%c]\n",
  507. tt_query->dst,
  508. (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
  509. tt_query->tt_data = htons(tt_query->tt_data);
  510. return route_unicast_packet(skb, recv_if);
  511. }
  512. break;
  513. }
  514. out:
  515. /* returning NET_RX_DROP will make the caller function kfree the skb */
  516. return NET_RX_DROP;
  517. }
  518. int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
  519. {
  520. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  521. struct roam_adv_packet *roam_adv_packet;
  522. struct orig_node *orig_node;
  523. struct ethhdr *ethhdr;
  524. /* drop packet if it has not necessary minimum size */
  525. if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
  526. goto out;
  527. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  528. /* packet with unicast indication but broadcast recipient */
  529. if (is_broadcast_ether_addr(ethhdr->h_dest))
  530. goto out;
  531. /* packet with broadcast sender address */
  532. if (is_broadcast_ether_addr(ethhdr->h_source))
  533. goto out;
  534. roam_adv_packet = (struct roam_adv_packet *)skb->data;
  535. if (!is_my_mac(roam_adv_packet->dst))
  536. return route_unicast_packet(skb, recv_if);
  537. orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
  538. if (!orig_node)
  539. goto out;
  540. bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
  541. "(client %pM)\n", roam_adv_packet->src,
  542. roam_adv_packet->client);
  543. tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  544. atomic_read(&orig_node->last_ttvn) + 1, true, false);
  545. /* Roaming phase starts: I have new information but the ttvn has not
  546. * been incremented yet. This flag will make me check all the incoming
  547. * packets for the correct destination. */
  548. bat_priv->tt_poss_change = true;
  549. orig_node_free_ref(orig_node);
  550. out:
  551. /* returning NET_RX_DROP will make the caller function kfree the skb */
  552. return NET_RX_DROP;
  553. }
  554. /* find a suitable router for this originator, and use
  555. * bonding if possible. increases the found neighbors
  556. * refcount.*/
  557. struct neigh_node *find_router(struct bat_priv *bat_priv,
  558. struct orig_node *orig_node,
  559. const struct hard_iface *recv_if)
  560. {
  561. struct orig_node *primary_orig_node;
  562. struct orig_node *router_orig;
  563. struct neigh_node *router;
  564. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  565. int bonding_enabled;
  566. if (!orig_node)
  567. return NULL;
  568. router = orig_node_get_router(orig_node);
  569. if (!router)
  570. goto err;
  571. /* without bonding, the first node should
  572. * always choose the default router. */
  573. bonding_enabled = atomic_read(&bat_priv->bonding);
  574. rcu_read_lock();
  575. /* select default router to output */
  576. router_orig = router->orig_node;
  577. if (!router_orig)
  578. goto err_unlock;
  579. if ((!recv_if) && (!bonding_enabled))
  580. goto return_router;
  581. /* if we have something in the primary_addr, we can search
  582. * for a potential bonding candidate. */
  583. if (compare_eth(router_orig->primary_addr, zero_mac))
  584. goto return_router;
  585. /* find the orig_node which has the primary interface. might
  586. * even be the same as our router_orig in many cases */
  587. if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
  588. primary_orig_node = router_orig;
  589. } else {
  590. primary_orig_node = orig_hash_find(bat_priv,
  591. router_orig->primary_addr);
  592. if (!primary_orig_node)
  593. goto return_router;
  594. orig_node_free_ref(primary_orig_node);
  595. }
  596. /* with less than 2 candidates, we can't do any
  597. * bonding and prefer the original router. */
  598. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  599. goto return_router;
  600. /* all nodes between should choose a candidate which
  601. * is is not on the interface where the packet came
  602. * in. */
  603. neigh_node_free_ref(router);
  604. if (bonding_enabled)
  605. router = find_bond_router(primary_orig_node, recv_if);
  606. else
  607. router = find_ifalter_router(primary_orig_node, recv_if);
  608. return_router:
  609. if (router && router->if_incoming->if_status != IF_ACTIVE)
  610. goto err_unlock;
  611. rcu_read_unlock();
  612. return router;
  613. err_unlock:
  614. rcu_read_unlock();
  615. err:
  616. if (router)
  617. neigh_node_free_ref(router);
  618. return NULL;
  619. }
  620. static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
  621. {
  622. struct ethhdr *ethhdr;
  623. /* drop packet if it has not necessary minimum size */
  624. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  625. return -1;
  626. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  627. /* packet with unicast indication but broadcast recipient */
  628. if (is_broadcast_ether_addr(ethhdr->h_dest))
  629. return -1;
  630. /* packet with broadcast sender address */
  631. if (is_broadcast_ether_addr(ethhdr->h_source))
  632. return -1;
  633. /* not for me */
  634. if (!is_my_mac(ethhdr->h_dest))
  635. return -1;
  636. return 0;
  637. }
  638. int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  639. {
  640. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  641. struct orig_node *orig_node = NULL;
  642. struct neigh_node *neigh_node = NULL;
  643. struct unicast_packet *unicast_packet;
  644. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  645. int ret = NET_RX_DROP;
  646. struct sk_buff *new_skb;
  647. unicast_packet = (struct unicast_packet *)skb->data;
  648. /* TTL exceeded */
  649. if (unicast_packet->ttl < 2) {
  650. pr_debug("Warning - can't forward unicast packet from %pM to "
  651. "%pM: ttl exceeded\n", ethhdr->h_source,
  652. unicast_packet->dest);
  653. goto out;
  654. }
  655. /* get routing information */
  656. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  657. if (!orig_node)
  658. goto out;
  659. /* find_router() increases neigh_nodes refcount if found. */
  660. neigh_node = find_router(bat_priv, orig_node, recv_if);
  661. if (!neigh_node)
  662. goto out;
  663. /* create a copy of the skb, if needed, to modify it. */
  664. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  665. goto out;
  666. unicast_packet = (struct unicast_packet *)skb->data;
  667. if (unicast_packet->packet_type == BAT_UNICAST &&
  668. atomic_read(&bat_priv->fragmentation) &&
  669. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  670. ret = frag_send_skb(skb, bat_priv,
  671. neigh_node->if_incoming, neigh_node->addr);
  672. goto out;
  673. }
  674. if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
  675. frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
  676. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  677. if (ret == NET_RX_DROP)
  678. goto out;
  679. /* packet was buffered for late merge */
  680. if (!new_skb) {
  681. ret = NET_RX_SUCCESS;
  682. goto out;
  683. }
  684. skb = new_skb;
  685. unicast_packet = (struct unicast_packet *)skb->data;
  686. }
  687. /* decrement ttl */
  688. unicast_packet->ttl--;
  689. /* route it */
  690. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  691. ret = NET_RX_SUCCESS;
  692. out:
  693. if (neigh_node)
  694. neigh_node_free_ref(neigh_node);
  695. if (orig_node)
  696. orig_node_free_ref(orig_node);
  697. return ret;
  698. }
  699. static int check_unicast_ttvn(struct bat_priv *bat_priv,
  700. struct sk_buff *skb) {
  701. uint8_t curr_ttvn;
  702. struct orig_node *orig_node;
  703. struct ethhdr *ethhdr;
  704. struct hard_iface *primary_if;
  705. struct unicast_packet *unicast_packet;
  706. bool tt_poss_change;
  707. /* I could need to modify it */
  708. if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
  709. return 0;
  710. unicast_packet = (struct unicast_packet *)skb->data;
  711. if (is_my_mac(unicast_packet->dest)) {
  712. tt_poss_change = bat_priv->tt_poss_change;
  713. curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  714. } else {
  715. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  716. if (!orig_node)
  717. return 0;
  718. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  719. tt_poss_change = orig_node->tt_poss_change;
  720. orig_node_free_ref(orig_node);
  721. }
  722. /* Check whether I have to reroute the packet */
  723. if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
  724. /* Linearize the skb before accessing it */
  725. if (skb_linearize(skb) < 0)
  726. return 0;
  727. ethhdr = (struct ethhdr *)(skb->data +
  728. sizeof(struct unicast_packet));
  729. orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
  730. if (!orig_node) {
  731. if (!is_my_client(bat_priv, ethhdr->h_dest))
  732. return 0;
  733. primary_if = primary_if_get_selected(bat_priv);
  734. if (!primary_if)
  735. return 0;
  736. memcpy(unicast_packet->dest,
  737. primary_if->net_dev->dev_addr, ETH_ALEN);
  738. hardif_free_ref(primary_if);
  739. } else {
  740. memcpy(unicast_packet->dest, orig_node->orig,
  741. ETH_ALEN);
  742. curr_ttvn = (uint8_t)
  743. atomic_read(&orig_node->last_ttvn);
  744. orig_node_free_ref(orig_node);
  745. }
  746. bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
  747. "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
  748. "%pM\n", unicast_packet->ttvn, curr_ttvn,
  749. ethhdr->h_dest, unicast_packet->dest);
  750. unicast_packet->ttvn = curr_ttvn;
  751. }
  752. return 1;
  753. }
  754. int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  755. {
  756. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  757. struct unicast_packet *unicast_packet;
  758. int hdr_size = sizeof(*unicast_packet);
  759. if (check_unicast_packet(skb, hdr_size) < 0)
  760. return NET_RX_DROP;
  761. if (!check_unicast_ttvn(bat_priv, skb))
  762. return NET_RX_DROP;
  763. unicast_packet = (struct unicast_packet *)skb->data;
  764. /* packet for me */
  765. if (is_my_mac(unicast_packet->dest)) {
  766. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  767. return NET_RX_SUCCESS;
  768. }
  769. return route_unicast_packet(skb, recv_if);
  770. }
  771. int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  772. {
  773. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  774. struct unicast_frag_packet *unicast_packet;
  775. int hdr_size = sizeof(*unicast_packet);
  776. struct sk_buff *new_skb = NULL;
  777. int ret;
  778. if (check_unicast_packet(skb, hdr_size) < 0)
  779. return NET_RX_DROP;
  780. if (!check_unicast_ttvn(bat_priv, skb))
  781. return NET_RX_DROP;
  782. unicast_packet = (struct unicast_frag_packet *)skb->data;
  783. /* packet for me */
  784. if (is_my_mac(unicast_packet->dest)) {
  785. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  786. if (ret == NET_RX_DROP)
  787. return NET_RX_DROP;
  788. /* packet was buffered for late merge */
  789. if (!new_skb)
  790. return NET_RX_SUCCESS;
  791. interface_rx(recv_if->soft_iface, new_skb, recv_if,
  792. sizeof(struct unicast_packet));
  793. return NET_RX_SUCCESS;
  794. }
  795. return route_unicast_packet(skb, recv_if);
  796. }
  797. int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  798. {
  799. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  800. struct orig_node *orig_node = NULL;
  801. struct bcast_packet *bcast_packet;
  802. struct ethhdr *ethhdr;
  803. int hdr_size = sizeof(*bcast_packet);
  804. int ret = NET_RX_DROP;
  805. int32_t seq_diff;
  806. /* drop packet if it has not necessary minimum size */
  807. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  808. goto out;
  809. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  810. /* packet with broadcast indication but unicast recipient */
  811. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  812. goto out;
  813. /* packet with broadcast sender address */
  814. if (is_broadcast_ether_addr(ethhdr->h_source))
  815. goto out;
  816. /* ignore broadcasts sent by myself */
  817. if (is_my_mac(ethhdr->h_source))
  818. goto out;
  819. bcast_packet = (struct bcast_packet *)skb->data;
  820. /* ignore broadcasts originated by myself */
  821. if (is_my_mac(bcast_packet->orig))
  822. goto out;
  823. if (bcast_packet->ttl < 2)
  824. goto out;
  825. orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
  826. if (!orig_node)
  827. goto out;
  828. spin_lock_bh(&orig_node->bcast_seqno_lock);
  829. /* check whether the packet is a duplicate */
  830. if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  831. ntohl(bcast_packet->seqno)))
  832. goto spin_unlock;
  833. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  834. /* check whether the packet is old and the host just restarted. */
  835. if (window_protected(bat_priv, seq_diff,
  836. &orig_node->bcast_seqno_reset))
  837. goto spin_unlock;
  838. /* mark broadcast in flood history, update window position
  839. * if required. */
  840. if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  841. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  842. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  843. /* rebroadcast packet */
  844. add_bcast_packet_to_list(bat_priv, skb, 1);
  845. /* broadcast for me */
  846. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  847. ret = NET_RX_SUCCESS;
  848. goto out;
  849. spin_unlock:
  850. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  851. out:
  852. if (orig_node)
  853. orig_node_free_ref(orig_node);
  854. return ret;
  855. }
  856. int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  857. {
  858. struct vis_packet *vis_packet;
  859. struct ethhdr *ethhdr;
  860. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  861. int hdr_size = sizeof(*vis_packet);
  862. /* keep skb linear */
  863. if (skb_linearize(skb) < 0)
  864. return NET_RX_DROP;
  865. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  866. return NET_RX_DROP;
  867. vis_packet = (struct vis_packet *)skb->data;
  868. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  869. /* not for me */
  870. if (!is_my_mac(ethhdr->h_dest))
  871. return NET_RX_DROP;
  872. /* ignore own packets */
  873. if (is_my_mac(vis_packet->vis_orig))
  874. return NET_RX_DROP;
  875. if (is_my_mac(vis_packet->sender_orig))
  876. return NET_RX_DROP;
  877. switch (vis_packet->vis_type) {
  878. case VIS_TYPE_SERVER_SYNC:
  879. receive_server_sync_packet(bat_priv, vis_packet,
  880. skb_headlen(skb));
  881. break;
  882. case VIS_TYPE_CLIENT_UPDATE:
  883. receive_client_update_packet(bat_priv, vis_packet,
  884. skb_headlen(skb));
  885. break;
  886. default: /* ignore unknown packet */
  887. break;
  888. }
  889. /* We take a copy of the data in the packet, so we should
  890. always free the skbuf. */
  891. return NET_RX_DROP;
  892. }