routing.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. uint16_t tt_len;
  469. struct ethhdr *ethhdr;
  470. /* drop packet if it has not necessary minimum size */
  471. if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
  472. goto out;
  473. /* I could need to modify it */
  474. if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
  475. goto out;
  476. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  477. /* packet with unicast indication but broadcast recipient */
  478. if (is_broadcast_ether_addr(ethhdr->h_dest))
  479. goto out;
  480. /* packet with broadcast sender address */
  481. if (is_broadcast_ether_addr(ethhdr->h_source))
  482. goto out;
  483. tt_query = (struct tt_query_packet *)skb->data;
  484. tt_query->tt_data = ntohs(tt_query->tt_data);
  485. switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
  486. case TT_REQUEST:
  487. /* If we cannot provide an answer the tt_request is
  488. * forwarded */
  489. if (!send_tt_response(bat_priv, tt_query)) {
  490. bat_dbg(DBG_TT, bat_priv,
  491. "Routing TT_REQUEST to %pM [%c]\n",
  492. tt_query->dst,
  493. (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
  494. tt_query->tt_data = htons(tt_query->tt_data);
  495. return route_unicast_packet(skb, recv_if);
  496. }
  497. break;
  498. case TT_RESPONSE:
  499. if (is_my_mac(tt_query->dst)) {
  500. /* packet needs to be linearized to access the TT
  501. * changes */
  502. if (skb_linearize(skb) < 0)
  503. goto out;
  504. tt_len = tt_query->tt_data * sizeof(struct tt_change);
  505. /* Ensure we have all the claimed data */
  506. if (unlikely(skb_headlen(skb) <
  507. sizeof(struct tt_query_packet) +
  508. tt_len))
  509. goto out;
  510. handle_tt_response(bat_priv, tt_query);
  511. } else {
  512. bat_dbg(DBG_TT, bat_priv,
  513. "Routing TT_RESPONSE to %pM [%c]\n",
  514. tt_query->dst,
  515. (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
  516. tt_query->tt_data = htons(tt_query->tt_data);
  517. return route_unicast_packet(skb, recv_if);
  518. }
  519. break;
  520. }
  521. out:
  522. /* returning NET_RX_DROP will make the caller function kfree the skb */
  523. return NET_RX_DROP;
  524. }
  525. int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
  526. {
  527. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  528. struct roam_adv_packet *roam_adv_packet;
  529. struct orig_node *orig_node;
  530. struct ethhdr *ethhdr;
  531. /* drop packet if it has not necessary minimum size */
  532. if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
  533. goto out;
  534. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  535. /* packet with unicast indication but broadcast recipient */
  536. if (is_broadcast_ether_addr(ethhdr->h_dest))
  537. goto out;
  538. /* packet with broadcast sender address */
  539. if (is_broadcast_ether_addr(ethhdr->h_source))
  540. goto out;
  541. roam_adv_packet = (struct roam_adv_packet *)skb->data;
  542. if (!is_my_mac(roam_adv_packet->dst))
  543. return route_unicast_packet(skb, recv_if);
  544. orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
  545. if (!orig_node)
  546. goto out;
  547. bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
  548. "(client %pM)\n", roam_adv_packet->src,
  549. roam_adv_packet->client);
  550. tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  551. atomic_read(&orig_node->last_ttvn) + 1, true, false);
  552. /* Roaming phase starts: I have new information but the ttvn has not
  553. * been incremented yet. This flag will make me check all the incoming
  554. * packets for the correct destination. */
  555. bat_priv->tt_poss_change = true;
  556. orig_node_free_ref(orig_node);
  557. out:
  558. /* returning NET_RX_DROP will make the caller function kfree the skb */
  559. return NET_RX_DROP;
  560. }
  561. /* find a suitable router for this originator, and use
  562. * bonding if possible. increases the found neighbors
  563. * refcount.*/
  564. struct neigh_node *find_router(struct bat_priv *bat_priv,
  565. struct orig_node *orig_node,
  566. const struct hard_iface *recv_if)
  567. {
  568. struct orig_node *primary_orig_node;
  569. struct orig_node *router_orig;
  570. struct neigh_node *router;
  571. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  572. int bonding_enabled;
  573. if (!orig_node)
  574. return NULL;
  575. router = orig_node_get_router(orig_node);
  576. if (!router)
  577. goto err;
  578. /* without bonding, the first node should
  579. * always choose the default router. */
  580. bonding_enabled = atomic_read(&bat_priv->bonding);
  581. rcu_read_lock();
  582. /* select default router to output */
  583. router_orig = router->orig_node;
  584. if (!router_orig)
  585. goto err_unlock;
  586. if ((!recv_if) && (!bonding_enabled))
  587. goto return_router;
  588. /* if we have something in the primary_addr, we can search
  589. * for a potential bonding candidate. */
  590. if (compare_eth(router_orig->primary_addr, zero_mac))
  591. goto return_router;
  592. /* find the orig_node which has the primary interface. might
  593. * even be the same as our router_orig in many cases */
  594. if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
  595. primary_orig_node = router_orig;
  596. } else {
  597. primary_orig_node = orig_hash_find(bat_priv,
  598. router_orig->primary_addr);
  599. if (!primary_orig_node)
  600. goto return_router;
  601. orig_node_free_ref(primary_orig_node);
  602. }
  603. /* with less than 2 candidates, we can't do any
  604. * bonding and prefer the original router. */
  605. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  606. goto return_router;
  607. /* all nodes between should choose a candidate which
  608. * is is not on the interface where the packet came
  609. * in. */
  610. neigh_node_free_ref(router);
  611. if (bonding_enabled)
  612. router = find_bond_router(primary_orig_node, recv_if);
  613. else
  614. router = find_ifalter_router(primary_orig_node, recv_if);
  615. return_router:
  616. if (router && router->if_incoming->if_status != IF_ACTIVE)
  617. goto err_unlock;
  618. rcu_read_unlock();
  619. return router;
  620. err_unlock:
  621. rcu_read_unlock();
  622. err:
  623. if (router)
  624. neigh_node_free_ref(router);
  625. return NULL;
  626. }
  627. static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
  628. {
  629. struct ethhdr *ethhdr;
  630. /* drop packet if it has not necessary minimum size */
  631. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  632. return -1;
  633. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  634. /* packet with unicast indication but broadcast recipient */
  635. if (is_broadcast_ether_addr(ethhdr->h_dest))
  636. return -1;
  637. /* packet with broadcast sender address */
  638. if (is_broadcast_ether_addr(ethhdr->h_source))
  639. return -1;
  640. /* not for me */
  641. if (!is_my_mac(ethhdr->h_dest))
  642. return -1;
  643. return 0;
  644. }
  645. int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  646. {
  647. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  648. struct orig_node *orig_node = NULL;
  649. struct neigh_node *neigh_node = NULL;
  650. struct unicast_packet *unicast_packet;
  651. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  652. int ret = NET_RX_DROP;
  653. struct sk_buff *new_skb;
  654. unicast_packet = (struct unicast_packet *)skb->data;
  655. /* TTL exceeded */
  656. if (unicast_packet->ttl < 2) {
  657. pr_debug("Warning - can't forward unicast packet from %pM to "
  658. "%pM: ttl exceeded\n", ethhdr->h_source,
  659. unicast_packet->dest);
  660. goto out;
  661. }
  662. /* get routing information */
  663. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  664. if (!orig_node)
  665. goto out;
  666. /* find_router() increases neigh_nodes refcount if found. */
  667. neigh_node = find_router(bat_priv, orig_node, recv_if);
  668. if (!neigh_node)
  669. goto out;
  670. /* create a copy of the skb, if needed, to modify it. */
  671. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  672. goto out;
  673. unicast_packet = (struct unicast_packet *)skb->data;
  674. if (unicast_packet->packet_type == BAT_UNICAST &&
  675. atomic_read(&bat_priv->fragmentation) &&
  676. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  677. ret = frag_send_skb(skb, bat_priv,
  678. neigh_node->if_incoming, neigh_node->addr);
  679. goto out;
  680. }
  681. if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
  682. frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
  683. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  684. if (ret == NET_RX_DROP)
  685. goto out;
  686. /* packet was buffered for late merge */
  687. if (!new_skb) {
  688. ret = NET_RX_SUCCESS;
  689. goto out;
  690. }
  691. skb = new_skb;
  692. unicast_packet = (struct unicast_packet *)skb->data;
  693. }
  694. /* decrement ttl */
  695. unicast_packet->ttl--;
  696. /* route it */
  697. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  698. ret = NET_RX_SUCCESS;
  699. out:
  700. if (neigh_node)
  701. neigh_node_free_ref(neigh_node);
  702. if (orig_node)
  703. orig_node_free_ref(orig_node);
  704. return ret;
  705. }
  706. static int check_unicast_ttvn(struct bat_priv *bat_priv,
  707. struct sk_buff *skb) {
  708. uint8_t curr_ttvn;
  709. struct orig_node *orig_node;
  710. struct ethhdr *ethhdr;
  711. struct hard_iface *primary_if;
  712. struct unicast_packet *unicast_packet;
  713. bool tt_poss_change;
  714. /* I could need to modify it */
  715. if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
  716. return 0;
  717. unicast_packet = (struct unicast_packet *)skb->data;
  718. if (is_my_mac(unicast_packet->dest)) {
  719. tt_poss_change = bat_priv->tt_poss_change;
  720. curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  721. } else {
  722. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  723. if (!orig_node)
  724. return 0;
  725. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  726. tt_poss_change = orig_node->tt_poss_change;
  727. orig_node_free_ref(orig_node);
  728. }
  729. /* Check whether I have to reroute the packet */
  730. if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
  731. /* Linearize the skb before accessing it */
  732. if (skb_linearize(skb) < 0)
  733. return 0;
  734. ethhdr = (struct ethhdr *)(skb->data +
  735. sizeof(struct unicast_packet));
  736. orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
  737. if (!orig_node) {
  738. if (!is_my_client(bat_priv, ethhdr->h_dest))
  739. return 0;
  740. primary_if = primary_if_get_selected(bat_priv);
  741. if (!primary_if)
  742. return 0;
  743. memcpy(unicast_packet->dest,
  744. primary_if->net_dev->dev_addr, ETH_ALEN);
  745. hardif_free_ref(primary_if);
  746. } else {
  747. memcpy(unicast_packet->dest, orig_node->orig,
  748. ETH_ALEN);
  749. curr_ttvn = (uint8_t)
  750. atomic_read(&orig_node->last_ttvn);
  751. orig_node_free_ref(orig_node);
  752. }
  753. bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
  754. "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
  755. "%pM\n", unicast_packet->ttvn, curr_ttvn,
  756. ethhdr->h_dest, unicast_packet->dest);
  757. unicast_packet->ttvn = curr_ttvn;
  758. }
  759. return 1;
  760. }
  761. int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  762. {
  763. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  764. struct unicast_packet *unicast_packet;
  765. int hdr_size = sizeof(*unicast_packet);
  766. if (check_unicast_packet(skb, hdr_size) < 0)
  767. return NET_RX_DROP;
  768. if (!check_unicast_ttvn(bat_priv, skb))
  769. return NET_RX_DROP;
  770. unicast_packet = (struct unicast_packet *)skb->data;
  771. /* packet for me */
  772. if (is_my_mac(unicast_packet->dest)) {
  773. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  774. return NET_RX_SUCCESS;
  775. }
  776. return route_unicast_packet(skb, recv_if);
  777. }
  778. int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  779. {
  780. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  781. struct unicast_frag_packet *unicast_packet;
  782. int hdr_size = sizeof(*unicast_packet);
  783. struct sk_buff *new_skb = NULL;
  784. int ret;
  785. if (check_unicast_packet(skb, hdr_size) < 0)
  786. return NET_RX_DROP;
  787. if (!check_unicast_ttvn(bat_priv, skb))
  788. return NET_RX_DROP;
  789. unicast_packet = (struct unicast_frag_packet *)skb->data;
  790. /* packet for me */
  791. if (is_my_mac(unicast_packet->dest)) {
  792. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  793. if (ret == NET_RX_DROP)
  794. return NET_RX_DROP;
  795. /* packet was buffered for late merge */
  796. if (!new_skb)
  797. return NET_RX_SUCCESS;
  798. interface_rx(recv_if->soft_iface, new_skb, recv_if,
  799. sizeof(struct unicast_packet));
  800. return NET_RX_SUCCESS;
  801. }
  802. return route_unicast_packet(skb, recv_if);
  803. }
  804. int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  805. {
  806. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  807. struct orig_node *orig_node = NULL;
  808. struct bcast_packet *bcast_packet;
  809. struct ethhdr *ethhdr;
  810. int hdr_size = sizeof(*bcast_packet);
  811. int ret = NET_RX_DROP;
  812. int32_t seq_diff;
  813. /* drop packet if it has not necessary minimum size */
  814. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  815. goto out;
  816. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  817. /* packet with broadcast indication but unicast recipient */
  818. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  819. goto out;
  820. /* packet with broadcast sender address */
  821. if (is_broadcast_ether_addr(ethhdr->h_source))
  822. goto out;
  823. /* ignore broadcasts sent by myself */
  824. if (is_my_mac(ethhdr->h_source))
  825. goto out;
  826. bcast_packet = (struct bcast_packet *)skb->data;
  827. /* ignore broadcasts originated by myself */
  828. if (is_my_mac(bcast_packet->orig))
  829. goto out;
  830. if (bcast_packet->ttl < 2)
  831. goto out;
  832. orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
  833. if (!orig_node)
  834. goto out;
  835. spin_lock_bh(&orig_node->bcast_seqno_lock);
  836. /* check whether the packet is a duplicate */
  837. if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  838. ntohl(bcast_packet->seqno)))
  839. goto spin_unlock;
  840. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  841. /* check whether the packet is old and the host just restarted. */
  842. if (window_protected(bat_priv, seq_diff,
  843. &orig_node->bcast_seqno_reset))
  844. goto spin_unlock;
  845. /* mark broadcast in flood history, update window position
  846. * if required. */
  847. if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  848. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  849. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  850. /* rebroadcast packet */
  851. add_bcast_packet_to_list(bat_priv, skb, 1);
  852. /* broadcast for me */
  853. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  854. ret = NET_RX_SUCCESS;
  855. goto out;
  856. spin_unlock:
  857. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  858. out:
  859. if (orig_node)
  860. orig_node_free_ref(orig_node);
  861. return ret;
  862. }
  863. int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  864. {
  865. struct vis_packet *vis_packet;
  866. struct ethhdr *ethhdr;
  867. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  868. int hdr_size = sizeof(*vis_packet);
  869. /* keep skb linear */
  870. if (skb_linearize(skb) < 0)
  871. return NET_RX_DROP;
  872. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  873. return NET_RX_DROP;
  874. vis_packet = (struct vis_packet *)skb->data;
  875. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  876. /* not for me */
  877. if (!is_my_mac(ethhdr->h_dest))
  878. return NET_RX_DROP;
  879. /* ignore own packets */
  880. if (is_my_mac(vis_packet->vis_orig))
  881. return NET_RX_DROP;
  882. if (is_my_mac(vis_packet->sender_orig))
  883. return NET_RX_DROP;
  884. switch (vis_packet->vis_type) {
  885. case VIS_TYPE_SERVER_SYNC:
  886. receive_server_sync_packet(bat_priv, vis_packet,
  887. skb_headlen(skb));
  888. break;
  889. case VIS_TYPE_CLIENT_UPDATE:
  890. receive_client_update_packet(bat_priv, vis_packet,
  891. skb_headlen(skb));
  892. break;
  893. default: /* ignore unknown packet */
  894. break;
  895. }
  896. /* We take a copy of the data in the packet, so we should
  897. always free the skbuf. */
  898. return NET_RX_DROP;
  899. }