routing.c 29 KB

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