routing.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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. void slide_own_bcast_window(struct hard_iface *hard_iface)
  32. {
  33. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  34. struct hashtable_t *hash = bat_priv->orig_hash;
  35. struct hlist_node *node;
  36. struct hlist_head *head;
  37. struct orig_node *orig_node;
  38. unsigned long *word;
  39. uint32_t i;
  40. size_t word_index;
  41. for (i = 0; i < hash->size; i++) {
  42. head = &hash->table[i];
  43. rcu_read_lock();
  44. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  45. spin_lock_bh(&orig_node->ogm_cnt_lock);
  46. word_index = hard_iface->if_num * NUM_WORDS;
  47. word = &(orig_node->bcast_own[word_index]);
  48. bit_get_packet(bat_priv, word, 1, 0);
  49. orig_node->bcast_own_sum[hard_iface->if_num] =
  50. bit_packet_count(word);
  51. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  52. }
  53. rcu_read_unlock();
  54. }
  55. }
  56. static void _update_route(struct bat_priv *bat_priv,
  57. struct orig_node *orig_node,
  58. struct neigh_node *neigh_node)
  59. {
  60. struct neigh_node *curr_router;
  61. curr_router = orig_node_get_router(orig_node);
  62. /* route deleted */
  63. if ((curr_router) && (!neigh_node)) {
  64. bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
  65. orig_node->orig);
  66. tt_global_del_orig(bat_priv, orig_node,
  67. "Deleted route towards originator");
  68. /* route added */
  69. } else if ((!curr_router) && (neigh_node)) {
  70. bat_dbg(DBG_ROUTES, bat_priv,
  71. "Adding route towards: %pM (via %pM)\n",
  72. orig_node->orig, neigh_node->addr);
  73. /* route changed */
  74. } else if (neigh_node && curr_router) {
  75. bat_dbg(DBG_ROUTES, bat_priv,
  76. "Changing route towards: %pM "
  77. "(now via %pM - was via %pM)\n",
  78. orig_node->orig, neigh_node->addr,
  79. curr_router->addr);
  80. }
  81. if (curr_router)
  82. neigh_node_free_ref(curr_router);
  83. /* increase refcount of new best neighbor */
  84. if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
  85. neigh_node = NULL;
  86. spin_lock_bh(&orig_node->neigh_list_lock);
  87. rcu_assign_pointer(orig_node->router, neigh_node);
  88. spin_unlock_bh(&orig_node->neigh_list_lock);
  89. /* decrease refcount of previous best neighbor */
  90. if (curr_router)
  91. neigh_node_free_ref(curr_router);
  92. }
  93. void update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
  94. struct neigh_node *neigh_node)
  95. {
  96. struct neigh_node *router = NULL;
  97. if (!orig_node)
  98. goto out;
  99. router = orig_node_get_router(orig_node);
  100. if (router != neigh_node)
  101. _update_route(bat_priv, orig_node, neigh_node);
  102. out:
  103. if (router)
  104. neigh_node_free_ref(router);
  105. }
  106. /* caller must hold the neigh_list_lock */
  107. void bonding_candidate_del(struct orig_node *orig_node,
  108. struct neigh_node *neigh_node)
  109. {
  110. /* this neighbor is not part of our candidate list */
  111. if (list_empty(&neigh_node->bonding_list))
  112. goto out;
  113. list_del_rcu(&neigh_node->bonding_list);
  114. INIT_LIST_HEAD(&neigh_node->bonding_list);
  115. neigh_node_free_ref(neigh_node);
  116. atomic_dec(&orig_node->bond_candidates);
  117. out:
  118. return;
  119. }
  120. void bonding_candidate_add(struct orig_node *orig_node,
  121. struct neigh_node *neigh_node)
  122. {
  123. struct hlist_node *node;
  124. struct neigh_node *tmp_neigh_node, *router = NULL;
  125. uint8_t interference_candidate = 0;
  126. spin_lock_bh(&orig_node->neigh_list_lock);
  127. /* only consider if it has the same primary address ... */
  128. if (!compare_eth(orig_node->orig,
  129. neigh_node->orig_node->primary_addr))
  130. goto candidate_del;
  131. router = orig_node_get_router(orig_node);
  132. if (!router)
  133. goto candidate_del;
  134. /* ... and is good enough to be considered */
  135. if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
  136. goto candidate_del;
  137. /**
  138. * check if we have another candidate with the same mac address or
  139. * interface. If we do, we won't select this candidate because of
  140. * possible interference.
  141. */
  142. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  143. &orig_node->neigh_list, list) {
  144. if (tmp_neigh_node == neigh_node)
  145. continue;
  146. /* we only care if the other candidate is even
  147. * considered as candidate. */
  148. if (list_empty(&tmp_neigh_node->bonding_list))
  149. continue;
  150. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  151. (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
  152. interference_candidate = 1;
  153. break;
  154. }
  155. }
  156. /* don't care further if it is an interference candidate */
  157. if (interference_candidate)
  158. goto candidate_del;
  159. /* this neighbor already is part of our candidate list */
  160. if (!list_empty(&neigh_node->bonding_list))
  161. goto out;
  162. if (!atomic_inc_not_zero(&neigh_node->refcount))
  163. goto out;
  164. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  165. atomic_inc(&orig_node->bond_candidates);
  166. goto out;
  167. candidate_del:
  168. bonding_candidate_del(orig_node, neigh_node);
  169. out:
  170. spin_unlock_bh(&orig_node->neigh_list_lock);
  171. if (router)
  172. neigh_node_free_ref(router);
  173. }
  174. /* copy primary address for bonding */
  175. void bonding_save_primary(const struct orig_node *orig_node,
  176. struct orig_node *orig_neigh_node,
  177. const struct batman_ogm_packet *batman_ogm_packet)
  178. {
  179. if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
  180. return;
  181. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  182. }
  183. /* checks whether the host restarted and is in the protection time.
  184. * returns:
  185. * 0 if the packet is to be accepted
  186. * 1 if the packet is to be ignored.
  187. */
  188. int window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
  189. unsigned long *last_reset)
  190. {
  191. if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
  192. || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
  193. if (time_after(jiffies, *last_reset +
  194. msecs_to_jiffies(RESET_PROTECTION_MS))) {
  195. *last_reset = jiffies;
  196. bat_dbg(DBG_BATMAN, bat_priv,
  197. "old packet received, start protection\n");
  198. return 0;
  199. } else
  200. return 1;
  201. }
  202. return 0;
  203. }
  204. int recv_bat_ogm_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
  205. {
  206. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  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. bat_priv->bat_algo_ops->bat_ogm_receive(hard_iface, skb);
  225. kfree_skb(skb);
  226. return NET_RX_SUCCESS;
  227. }
  228. static int recv_my_icmp_packet(struct bat_priv *bat_priv,
  229. struct sk_buff *skb, size_t icmp_len)
  230. {
  231. struct hard_iface *primary_if = NULL;
  232. struct orig_node *orig_node = NULL;
  233. struct neigh_node *router = NULL;
  234. struct icmp_packet_rr *icmp_packet;
  235. int ret = NET_RX_DROP;
  236. icmp_packet = (struct icmp_packet_rr *)skb->data;
  237. /* add data to device queue */
  238. if (icmp_packet->msg_type != ECHO_REQUEST) {
  239. bat_socket_receive_packet(icmp_packet, icmp_len);
  240. goto out;
  241. }
  242. primary_if = primary_if_get_selected(bat_priv);
  243. if (!primary_if)
  244. goto out;
  245. /* answer echo request (ping) */
  246. /* get routing information */
  247. orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
  248. if (!orig_node)
  249. goto out;
  250. router = orig_node_get_router(orig_node);
  251. if (!router)
  252. goto out;
  253. /* create a copy of the skb, if needed, to modify it. */
  254. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  255. goto out;
  256. icmp_packet = (struct icmp_packet_rr *)skb->data;
  257. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  258. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  259. icmp_packet->msg_type = ECHO_REPLY;
  260. icmp_packet->header.ttl = TTL;
  261. send_skb_packet(skb, router->if_incoming, router->addr);
  262. ret = NET_RX_SUCCESS;
  263. out:
  264. if (primary_if)
  265. hardif_free_ref(primary_if);
  266. if (router)
  267. neigh_node_free_ref(router);
  268. if (orig_node)
  269. orig_node_free_ref(orig_node);
  270. return ret;
  271. }
  272. static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
  273. struct sk_buff *skb)
  274. {
  275. struct hard_iface *primary_if = NULL;
  276. struct orig_node *orig_node = NULL;
  277. struct neigh_node *router = NULL;
  278. struct icmp_packet *icmp_packet;
  279. int ret = NET_RX_DROP;
  280. icmp_packet = (struct icmp_packet *)skb->data;
  281. /* send TTL exceeded if packet is an echo request (traceroute) */
  282. if (icmp_packet->msg_type != ECHO_REQUEST) {
  283. pr_debug("Warning - can't forward icmp packet from %pM to "
  284. "%pM: ttl exceeded\n", icmp_packet->orig,
  285. icmp_packet->dst);
  286. goto out;
  287. }
  288. primary_if = primary_if_get_selected(bat_priv);
  289. if (!primary_if)
  290. goto out;
  291. /* get routing information */
  292. orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
  293. if (!orig_node)
  294. goto out;
  295. router = 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, sizeof(struct ethhdr)) < 0)
  300. goto out;
  301. icmp_packet = (struct 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 = TTL_EXCEEDED;
  305. icmp_packet->header.ttl = TTL;
  306. send_skb_packet(skb, router->if_incoming, router->addr);
  307. ret = NET_RX_SUCCESS;
  308. out:
  309. if (primary_if)
  310. hardif_free_ref(primary_if);
  311. if (router)
  312. neigh_node_free_ref(router);
  313. if (orig_node)
  314. orig_node_free_ref(orig_node);
  315. return ret;
  316. }
  317. int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  318. {
  319. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  320. struct icmp_packet_rr *icmp_packet;
  321. struct ethhdr *ethhdr;
  322. struct orig_node *orig_node = NULL;
  323. struct neigh_node *router = NULL;
  324. int hdr_size = sizeof(struct icmp_packet);
  325. int ret = NET_RX_DROP;
  326. /**
  327. * we truncate all incoming icmp packets if they don't match our size
  328. */
  329. if (skb->len >= sizeof(struct icmp_packet_rr))
  330. hdr_size = sizeof(struct 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 (!is_my_mac(ethhdr->h_dest))
  343. goto out;
  344. icmp_packet = (struct icmp_packet_rr *)skb->data;
  345. /* add record route information if not full */
  346. if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
  347. (icmp_packet->rr_cur < BAT_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 (is_my_mac(icmp_packet->dst))
  354. return recv_my_icmp_packet(bat_priv, skb, hdr_size);
  355. /* TTL exceeded */
  356. if (icmp_packet->header.ttl < 2)
  357. return recv_icmp_ttl_exceeded(bat_priv, skb);
  358. /* get routing information */
  359. orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
  360. if (!orig_node)
  361. goto out;
  362. router = 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, sizeof(struct ethhdr)) < 0)
  367. goto out;
  368. icmp_packet = (struct icmp_packet_rr *)skb->data;
  369. /* decrement ttl */
  370. icmp_packet->header.ttl--;
  371. /* route it */
  372. send_skb_packet(skb, router->if_incoming, router->addr);
  373. ret = NET_RX_SUCCESS;
  374. out:
  375. if (router)
  376. neigh_node_free_ref(router);
  377. if (orig_node)
  378. 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. static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
  387. const struct hard_iface *recv_if)
  388. {
  389. struct neigh_node *tmp_neigh_node;
  390. struct neigh_node *router = NULL, *first_candidate = NULL;
  391. rcu_read_lock();
  392. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  393. bonding_list) {
  394. if (!first_candidate)
  395. first_candidate = tmp_neigh_node;
  396. /* recv_if == NULL on the first node. */
  397. if (tmp_neigh_node->if_incoming == recv_if)
  398. continue;
  399. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  400. continue;
  401. router = tmp_neigh_node;
  402. break;
  403. }
  404. /* use the first candidate if nothing was found. */
  405. if (!router && first_candidate &&
  406. atomic_inc_not_zero(&first_candidate->refcount))
  407. router = first_candidate;
  408. if (!router)
  409. goto out;
  410. /* selected should point to the next element
  411. * after the current router */
  412. spin_lock_bh(&primary_orig->neigh_list_lock);
  413. /* this is a list_move(), which unfortunately
  414. * does not exist as rcu version */
  415. list_del_rcu(&primary_orig->bond_list);
  416. list_add_rcu(&primary_orig->bond_list,
  417. &router->bonding_list);
  418. spin_unlock_bh(&primary_orig->neigh_list_lock);
  419. out:
  420. rcu_read_unlock();
  421. return router;
  422. }
  423. /* Interface Alternating: Use the best of the
  424. * remaining candidates which are not using
  425. * this interface.
  426. *
  427. * Increases the returned router's refcount */
  428. static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
  429. const struct hard_iface *recv_if)
  430. {
  431. struct neigh_node *tmp_neigh_node;
  432. struct neigh_node *router = NULL, *first_candidate = NULL;
  433. rcu_read_lock();
  434. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  435. bonding_list) {
  436. if (!first_candidate)
  437. first_candidate = tmp_neigh_node;
  438. /* recv_if == NULL on the first node. */
  439. if (tmp_neigh_node->if_incoming == recv_if)
  440. continue;
  441. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  442. continue;
  443. /* if we don't have a router yet
  444. * or this one is better, choose it. */
  445. if ((!router) ||
  446. (tmp_neigh_node->tq_avg > router->tq_avg)) {
  447. /* decrement refcount of
  448. * previously selected router */
  449. if (router)
  450. neigh_node_free_ref(router);
  451. router = tmp_neigh_node;
  452. atomic_inc_not_zero(&router->refcount);
  453. }
  454. neigh_node_free_ref(tmp_neigh_node);
  455. }
  456. /* use the first candidate if nothing was found. */
  457. if (!router && first_candidate &&
  458. atomic_inc_not_zero(&first_candidate->refcount))
  459. router = first_candidate;
  460. rcu_read_unlock();
  461. return router;
  462. }
  463. int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
  464. {
  465. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  466. struct tt_query_packet *tt_query;
  467. uint16_t tt_len;
  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. tt_len = tt_query->tt_data * sizeof(struct tt_change);
  504. /* Ensure we have all the claimed data */
  505. if (unlikely(skb_headlen(skb) <
  506. sizeof(struct tt_query_packet) + tt_len))
  507. goto out;
  508. handle_tt_response(bat_priv, tt_query);
  509. } else {
  510. bat_dbg(DBG_TT, bat_priv,
  511. "Routing TT_RESPONSE to %pM [%c]\n",
  512. tt_query->dst,
  513. (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
  514. tt_query->tt_data = htons(tt_query->tt_data);
  515. return route_unicast_packet(skb, recv_if);
  516. }
  517. break;
  518. }
  519. out:
  520. /* returning NET_RX_DROP will make the caller function kfree the skb */
  521. return NET_RX_DROP;
  522. }
  523. int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
  524. {
  525. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  526. struct roam_adv_packet *roam_adv_packet;
  527. struct orig_node *orig_node;
  528. struct ethhdr *ethhdr;
  529. /* drop packet if it has not necessary minimum size */
  530. if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
  531. goto out;
  532. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  533. /* packet with unicast indication but broadcast recipient */
  534. if (is_broadcast_ether_addr(ethhdr->h_dest))
  535. goto out;
  536. /* packet with broadcast sender address */
  537. if (is_broadcast_ether_addr(ethhdr->h_source))
  538. goto out;
  539. roam_adv_packet = (struct roam_adv_packet *)skb->data;
  540. if (!is_my_mac(roam_adv_packet->dst))
  541. return route_unicast_packet(skb, recv_if);
  542. orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
  543. if (!orig_node)
  544. goto out;
  545. bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
  546. "(client %pM)\n", roam_adv_packet->src,
  547. roam_adv_packet->client);
  548. tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  549. atomic_read(&orig_node->last_ttvn) + 1, true, false);
  550. /* Roaming phase starts: I have new information but the ttvn has not
  551. * been incremented yet. This flag will make me check all the incoming
  552. * packets for the correct destination. */
  553. bat_priv->tt_poss_change = true;
  554. orig_node_free_ref(orig_node);
  555. out:
  556. /* returning NET_RX_DROP will make the caller function kfree the skb */
  557. return NET_RX_DROP;
  558. }
  559. /* find a suitable router for this originator, and use
  560. * bonding if possible. increases the found neighbors
  561. * refcount.*/
  562. struct neigh_node *find_router(struct bat_priv *bat_priv,
  563. struct orig_node *orig_node,
  564. const struct hard_iface *recv_if)
  565. {
  566. struct orig_node *primary_orig_node;
  567. struct orig_node *router_orig;
  568. struct neigh_node *router;
  569. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  570. int bonding_enabled;
  571. if (!orig_node)
  572. return NULL;
  573. router = orig_node_get_router(orig_node);
  574. if (!router)
  575. goto err;
  576. /* without bonding, the first node should
  577. * always choose the default router. */
  578. bonding_enabled = atomic_read(&bat_priv->bonding);
  579. rcu_read_lock();
  580. /* select default router to output */
  581. router_orig = router->orig_node;
  582. if (!router_orig)
  583. goto err_unlock;
  584. if ((!recv_if) && (!bonding_enabled))
  585. goto return_router;
  586. /* if we have something in the primary_addr, we can search
  587. * for a potential bonding candidate. */
  588. if (compare_eth(router_orig->primary_addr, zero_mac))
  589. goto return_router;
  590. /* find the orig_node which has the primary interface. might
  591. * even be the same as our router_orig in many cases */
  592. if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
  593. primary_orig_node = router_orig;
  594. } else {
  595. primary_orig_node = orig_hash_find(bat_priv,
  596. router_orig->primary_addr);
  597. if (!primary_orig_node)
  598. goto return_router;
  599. orig_node_free_ref(primary_orig_node);
  600. }
  601. /* with less than 2 candidates, we can't do any
  602. * bonding and prefer the original router. */
  603. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  604. goto return_router;
  605. /* all nodes between should choose a candidate which
  606. * is is not on the interface where the packet came
  607. * in. */
  608. neigh_node_free_ref(router);
  609. if (bonding_enabled)
  610. router = find_bond_router(primary_orig_node, recv_if);
  611. else
  612. router = find_ifalter_router(primary_orig_node, recv_if);
  613. return_router:
  614. if (router && router->if_incoming->if_status != IF_ACTIVE)
  615. goto err_unlock;
  616. rcu_read_unlock();
  617. return router;
  618. err_unlock:
  619. rcu_read_unlock();
  620. err:
  621. if (router)
  622. neigh_node_free_ref(router);
  623. return NULL;
  624. }
  625. static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
  626. {
  627. struct ethhdr *ethhdr;
  628. /* drop packet if it has not necessary minimum size */
  629. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  630. return -1;
  631. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  632. /* packet with unicast indication but broadcast recipient */
  633. if (is_broadcast_ether_addr(ethhdr->h_dest))
  634. return -1;
  635. /* packet with broadcast sender address */
  636. if (is_broadcast_ether_addr(ethhdr->h_source))
  637. return -1;
  638. /* not for me */
  639. if (!is_my_mac(ethhdr->h_dest))
  640. return -1;
  641. return 0;
  642. }
  643. int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  644. {
  645. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  646. struct orig_node *orig_node = NULL;
  647. struct neigh_node *neigh_node = NULL;
  648. struct unicast_packet *unicast_packet;
  649. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  650. int ret = NET_RX_DROP;
  651. struct sk_buff *new_skb;
  652. unicast_packet = (struct unicast_packet *)skb->data;
  653. /* TTL exceeded */
  654. if (unicast_packet->header.ttl < 2) {
  655. pr_debug("Warning - can't forward unicast packet from %pM to "
  656. "%pM: ttl exceeded\n", ethhdr->h_source,
  657. unicast_packet->dest);
  658. goto out;
  659. }
  660. /* get routing information */
  661. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  662. if (!orig_node)
  663. goto out;
  664. /* find_router() increases neigh_nodes refcount if found. */
  665. neigh_node = find_router(bat_priv, orig_node, recv_if);
  666. if (!neigh_node)
  667. goto out;
  668. /* create a copy of the skb, if needed, to modify it. */
  669. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  670. goto out;
  671. unicast_packet = (struct unicast_packet *)skb->data;
  672. if (unicast_packet->header.packet_type == BAT_UNICAST &&
  673. atomic_read(&bat_priv->fragmentation) &&
  674. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  675. ret = frag_send_skb(skb, bat_priv,
  676. neigh_node->if_incoming, neigh_node->addr);
  677. goto out;
  678. }
  679. if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
  680. frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
  681. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  682. if (ret == NET_RX_DROP)
  683. goto out;
  684. /* packet was buffered for late merge */
  685. if (!new_skb) {
  686. ret = NET_RX_SUCCESS;
  687. goto out;
  688. }
  689. skb = new_skb;
  690. unicast_packet = (struct unicast_packet *)skb->data;
  691. }
  692. /* decrement ttl */
  693. unicast_packet->header.ttl--;
  694. /* route it */
  695. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  696. ret = NET_RX_SUCCESS;
  697. out:
  698. if (neigh_node)
  699. neigh_node_free_ref(neigh_node);
  700. if (orig_node)
  701. orig_node_free_ref(orig_node);
  702. return ret;
  703. }
  704. static int check_unicast_ttvn(struct bat_priv *bat_priv,
  705. struct sk_buff *skb) {
  706. uint8_t curr_ttvn;
  707. struct orig_node *orig_node;
  708. struct ethhdr *ethhdr;
  709. struct hard_iface *primary_if;
  710. struct unicast_packet *unicast_packet;
  711. bool tt_poss_change;
  712. /* I could need to modify it */
  713. if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
  714. return 0;
  715. unicast_packet = (struct unicast_packet *)skb->data;
  716. if (is_my_mac(unicast_packet->dest)) {
  717. tt_poss_change = bat_priv->tt_poss_change;
  718. curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  719. } else {
  720. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  721. if (!orig_node)
  722. return 0;
  723. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  724. tt_poss_change = orig_node->tt_poss_change;
  725. orig_node_free_ref(orig_node);
  726. }
  727. /* Check whether I have to reroute the packet */
  728. if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
  729. /* Linearize the skb before accessing it */
  730. if (skb_linearize(skb) < 0)
  731. return 0;
  732. ethhdr = (struct ethhdr *)(skb->data +
  733. sizeof(struct unicast_packet));
  734. orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
  735. if (!orig_node) {
  736. if (!is_my_client(bat_priv, ethhdr->h_dest))
  737. return 0;
  738. primary_if = primary_if_get_selected(bat_priv);
  739. if (!primary_if)
  740. return 0;
  741. memcpy(unicast_packet->dest,
  742. primary_if->net_dev->dev_addr, ETH_ALEN);
  743. hardif_free_ref(primary_if);
  744. } else {
  745. memcpy(unicast_packet->dest, orig_node->orig,
  746. ETH_ALEN);
  747. curr_ttvn = (uint8_t)
  748. atomic_read(&orig_node->last_ttvn);
  749. orig_node_free_ref(orig_node);
  750. }
  751. bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
  752. "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
  753. "%pM\n", unicast_packet->ttvn, curr_ttvn,
  754. ethhdr->h_dest, unicast_packet->dest);
  755. unicast_packet->ttvn = curr_ttvn;
  756. }
  757. return 1;
  758. }
  759. int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  760. {
  761. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  762. struct unicast_packet *unicast_packet;
  763. int hdr_size = sizeof(*unicast_packet);
  764. if (check_unicast_packet(skb, hdr_size) < 0)
  765. return NET_RX_DROP;
  766. if (!check_unicast_ttvn(bat_priv, skb))
  767. return NET_RX_DROP;
  768. unicast_packet = (struct unicast_packet *)skb->data;
  769. /* packet for me */
  770. if (is_my_mac(unicast_packet->dest)) {
  771. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  772. return NET_RX_SUCCESS;
  773. }
  774. return route_unicast_packet(skb, recv_if);
  775. }
  776. int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  777. {
  778. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  779. struct unicast_frag_packet *unicast_packet;
  780. int hdr_size = sizeof(*unicast_packet);
  781. struct sk_buff *new_skb = NULL;
  782. int ret;
  783. if (check_unicast_packet(skb, hdr_size) < 0)
  784. return NET_RX_DROP;
  785. if (!check_unicast_ttvn(bat_priv, skb))
  786. return NET_RX_DROP;
  787. unicast_packet = (struct unicast_frag_packet *)skb->data;
  788. /* packet for me */
  789. if (is_my_mac(unicast_packet->dest)) {
  790. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  791. if (ret == NET_RX_DROP)
  792. return NET_RX_DROP;
  793. /* packet was buffered for late merge */
  794. if (!new_skb)
  795. return NET_RX_SUCCESS;
  796. interface_rx(recv_if->soft_iface, new_skb, recv_if,
  797. sizeof(struct unicast_packet));
  798. return NET_RX_SUCCESS;
  799. }
  800. return route_unicast_packet(skb, recv_if);
  801. }
  802. int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  803. {
  804. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  805. struct orig_node *orig_node = NULL;
  806. struct bcast_packet *bcast_packet;
  807. struct ethhdr *ethhdr;
  808. int hdr_size = sizeof(*bcast_packet);
  809. int ret = NET_RX_DROP;
  810. int32_t seq_diff;
  811. /* drop packet if it has not necessary minimum size */
  812. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  813. goto out;
  814. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  815. /* packet with broadcast indication but unicast recipient */
  816. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  817. goto out;
  818. /* packet with broadcast sender address */
  819. if (is_broadcast_ether_addr(ethhdr->h_source))
  820. goto out;
  821. /* ignore broadcasts sent by myself */
  822. if (is_my_mac(ethhdr->h_source))
  823. goto out;
  824. bcast_packet = (struct bcast_packet *)skb->data;
  825. /* ignore broadcasts originated by myself */
  826. if (is_my_mac(bcast_packet->orig))
  827. goto out;
  828. if (bcast_packet->header.ttl < 2)
  829. goto out;
  830. orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
  831. if (!orig_node)
  832. goto out;
  833. spin_lock_bh(&orig_node->bcast_seqno_lock);
  834. /* check whether the packet is a duplicate */
  835. if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  836. ntohl(bcast_packet->seqno)))
  837. goto spin_unlock;
  838. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  839. /* check whether the packet is old and the host just restarted. */
  840. if (window_protected(bat_priv, seq_diff,
  841. &orig_node->bcast_seqno_reset))
  842. goto spin_unlock;
  843. /* mark broadcast in flood history, update window position
  844. * if required. */
  845. if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  846. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  847. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  848. /* rebroadcast packet */
  849. add_bcast_packet_to_list(bat_priv, skb, 1);
  850. /* broadcast for me */
  851. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  852. ret = NET_RX_SUCCESS;
  853. goto out;
  854. spin_unlock:
  855. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  856. out:
  857. if (orig_node)
  858. orig_node_free_ref(orig_node);
  859. return ret;
  860. }
  861. int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  862. {
  863. struct vis_packet *vis_packet;
  864. struct ethhdr *ethhdr;
  865. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  866. int hdr_size = sizeof(*vis_packet);
  867. /* keep skb linear */
  868. if (skb_linearize(skb) < 0)
  869. return NET_RX_DROP;
  870. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  871. return NET_RX_DROP;
  872. vis_packet = (struct vis_packet *)skb->data;
  873. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  874. /* not for me */
  875. if (!is_my_mac(ethhdr->h_dest))
  876. return NET_RX_DROP;
  877. /* ignore own packets */
  878. if (is_my_mac(vis_packet->vis_orig))
  879. return NET_RX_DROP;
  880. if (is_my_mac(vis_packet->sender_orig))
  881. return NET_RX_DROP;
  882. switch (vis_packet->vis_type) {
  883. case VIS_TYPE_SERVER_SYNC:
  884. receive_server_sync_packet(bat_priv, vis_packet,
  885. skb_headlen(skb));
  886. break;
  887. case VIS_TYPE_CLIENT_UPDATE:
  888. receive_client_update_packet(bat_priv, vis_packet,
  889. skb_headlen(skb));
  890. break;
  891. default: /* ignore unknown packet */
  892. break;
  893. }
  894. /* We take a copy of the data in the packet, so we should
  895. always free the skbuf. */
  896. return NET_RX_DROP;
  897. }