routing.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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 "hash.h"
  25. #include "soft-interface.h"
  26. #include "hard-interface.h"
  27. #include "icmp_socket.h"
  28. #include "translation-table.h"
  29. #include "originator.h"
  30. #include "ring_buffer.h"
  31. #include "vis.h"
  32. #include "aggregation.h"
  33. #include "gateway_common.h"
  34. #include "gateway_client.h"
  35. #include "unicast.h"
  36. void slide_own_bcast_window(struct hard_iface *hard_iface)
  37. {
  38. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  39. struct hashtable_t *hash = bat_priv->orig_hash;
  40. struct hlist_node *node;
  41. struct hlist_head *head;
  42. struct orig_node *orig_node;
  43. unsigned long *word;
  44. int i;
  45. size_t word_index;
  46. for (i = 0; i < hash->size; i++) {
  47. head = &hash->table[i];
  48. rcu_read_lock();
  49. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  50. spin_lock_bh(&orig_node->ogm_cnt_lock);
  51. word_index = hard_iface->if_num * NUM_WORDS;
  52. word = &(orig_node->bcast_own[word_index]);
  53. bit_get_packet(bat_priv, word, 1, 0);
  54. orig_node->bcast_own_sum[hard_iface->if_num] =
  55. bit_packet_count(word);
  56. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  57. }
  58. rcu_read_unlock();
  59. }
  60. }
  61. static void update_TT(struct bat_priv *bat_priv, struct orig_node *orig_node,
  62. const unsigned char *tt_buff, int tt_buff_len)
  63. {
  64. if ((tt_buff_len != orig_node->tt_buff_len) ||
  65. ((tt_buff_len > 0) &&
  66. (orig_node->tt_buff_len > 0) &&
  67. (memcmp(orig_node->tt_buff, tt_buff, tt_buff_len) != 0))) {
  68. if (orig_node->tt_buff_len > 0)
  69. tt_global_del_orig(bat_priv, orig_node,
  70. "originator changed tt");
  71. if ((tt_buff_len > 0) && (tt_buff))
  72. tt_global_add_orig(bat_priv, orig_node,
  73. tt_buff, tt_buff_len);
  74. }
  75. }
  76. static void update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
  77. struct neigh_node *neigh_node,
  78. const unsigned char *tt_buff, int tt_buff_len)
  79. {
  80. struct neigh_node *curr_router;
  81. curr_router = orig_node_get_router(orig_node);
  82. /* route deleted */
  83. if ((curr_router) && (!neigh_node)) {
  84. bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
  85. orig_node->orig);
  86. tt_global_del_orig(bat_priv, orig_node,
  87. "originator timed out");
  88. /* route added */
  89. } else if ((!curr_router) && (neigh_node)) {
  90. bat_dbg(DBG_ROUTES, bat_priv,
  91. "Adding route towards: %pM (via %pM)\n",
  92. orig_node->orig, neigh_node->addr);
  93. tt_global_add_orig(bat_priv, orig_node,
  94. tt_buff, tt_buff_len);
  95. /* route changed */
  96. } else if (neigh_node && curr_router) {
  97. bat_dbg(DBG_ROUTES, bat_priv,
  98. "Changing route towards: %pM "
  99. "(now via %pM - was via %pM)\n",
  100. orig_node->orig, neigh_node->addr,
  101. curr_router->addr);
  102. }
  103. if (curr_router)
  104. neigh_node_free_ref(curr_router);
  105. /* increase refcount of new best neighbor */
  106. if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
  107. neigh_node = NULL;
  108. spin_lock_bh(&orig_node->neigh_list_lock);
  109. rcu_assign_pointer(orig_node->router, neigh_node);
  110. spin_unlock_bh(&orig_node->neigh_list_lock);
  111. /* decrease refcount of previous best neighbor */
  112. if (curr_router)
  113. neigh_node_free_ref(curr_router);
  114. }
  115. void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
  116. struct neigh_node *neigh_node, const unsigned char *tt_buff,
  117. int tt_buff_len)
  118. {
  119. struct neigh_node *router = NULL;
  120. if (!orig_node)
  121. goto out;
  122. router = orig_node_get_router(orig_node);
  123. if (router != neigh_node)
  124. update_route(bat_priv, orig_node, neigh_node,
  125. tt_buff, tt_buff_len);
  126. /* may be just TT changed */
  127. else
  128. update_TT(bat_priv, orig_node, tt_buff, tt_buff_len);
  129. out:
  130. if (router)
  131. neigh_node_free_ref(router);
  132. }
  133. static int is_bidirectional_neigh(struct orig_node *orig_node,
  134. struct orig_node *orig_neigh_node,
  135. struct batman_packet *batman_packet,
  136. struct hard_iface *if_incoming)
  137. {
  138. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  139. struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
  140. struct hlist_node *node;
  141. uint8_t total_count;
  142. uint8_t orig_eq_count, neigh_rq_count, tq_own;
  143. int tq_asym_penalty, ret = 0;
  144. /* find corresponding one hop neighbor */
  145. rcu_read_lock();
  146. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  147. &orig_neigh_node->neigh_list, list) {
  148. if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
  149. continue;
  150. if (tmp_neigh_node->if_incoming != if_incoming)
  151. continue;
  152. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  153. continue;
  154. neigh_node = tmp_neigh_node;
  155. break;
  156. }
  157. rcu_read_unlock();
  158. if (!neigh_node)
  159. neigh_node = create_neighbor(orig_neigh_node,
  160. orig_neigh_node,
  161. orig_neigh_node->orig,
  162. if_incoming);
  163. if (!neigh_node)
  164. goto out;
  165. /* if orig_node is direct neighbour update neigh_node last_valid */
  166. if (orig_node == orig_neigh_node)
  167. neigh_node->last_valid = jiffies;
  168. orig_node->last_valid = jiffies;
  169. /* find packet count of corresponding one hop neighbor */
  170. spin_lock_bh(&orig_node->ogm_cnt_lock);
  171. orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
  172. neigh_rq_count = neigh_node->real_packet_count;
  173. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  174. /* pay attention to not get a value bigger than 100 % */
  175. total_count = (orig_eq_count > neigh_rq_count ?
  176. neigh_rq_count : orig_eq_count);
  177. /* if we have too few packets (too less data) we set tq_own to zero */
  178. /* if we receive too few packets it is not considered bidirectional */
  179. if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
  180. (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
  181. tq_own = 0;
  182. else
  183. /* neigh_node->real_packet_count is never zero as we
  184. * only purge old information when getting new
  185. * information */
  186. tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
  187. /*
  188. * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
  189. * affect the nearly-symmetric links only a little, but
  190. * punishes asymmetric links more. This will give a value
  191. * between 0 and TQ_MAX_VALUE
  192. */
  193. tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
  194. (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
  195. (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
  196. (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
  197. (TQ_LOCAL_WINDOW_SIZE *
  198. TQ_LOCAL_WINDOW_SIZE *
  199. TQ_LOCAL_WINDOW_SIZE);
  200. batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
  201. (TQ_MAX_VALUE * TQ_MAX_VALUE));
  202. bat_dbg(DBG_BATMAN, bat_priv,
  203. "bidirectional: "
  204. "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
  205. "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
  206. "total tq: %3i\n",
  207. orig_node->orig, orig_neigh_node->orig, total_count,
  208. neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
  209. /* if link has the minimum required transmission quality
  210. * consider it bidirectional */
  211. if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
  212. ret = 1;
  213. out:
  214. if (neigh_node)
  215. neigh_node_free_ref(neigh_node);
  216. return ret;
  217. }
  218. /* caller must hold the neigh_list_lock */
  219. void bonding_candidate_del(struct orig_node *orig_node,
  220. struct neigh_node *neigh_node)
  221. {
  222. /* this neighbor is not part of our candidate list */
  223. if (list_empty(&neigh_node->bonding_list))
  224. goto out;
  225. list_del_rcu(&neigh_node->bonding_list);
  226. INIT_LIST_HEAD(&neigh_node->bonding_list);
  227. neigh_node_free_ref(neigh_node);
  228. atomic_dec(&orig_node->bond_candidates);
  229. out:
  230. return;
  231. }
  232. static void bonding_candidate_add(struct orig_node *orig_node,
  233. struct neigh_node *neigh_node)
  234. {
  235. struct hlist_node *node;
  236. struct neigh_node *tmp_neigh_node, *router = NULL;
  237. uint8_t interference_candidate = 0;
  238. spin_lock_bh(&orig_node->neigh_list_lock);
  239. /* only consider if it has the same primary address ... */
  240. if (!compare_eth(orig_node->orig,
  241. neigh_node->orig_node->primary_addr))
  242. goto candidate_del;
  243. router = orig_node_get_router(orig_node);
  244. if (!router)
  245. goto candidate_del;
  246. /* ... and is good enough to be considered */
  247. if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
  248. goto candidate_del;
  249. /**
  250. * check if we have another candidate with the same mac address or
  251. * interface. If we do, we won't select this candidate because of
  252. * possible interference.
  253. */
  254. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  255. &orig_node->neigh_list, list) {
  256. if (tmp_neigh_node == neigh_node)
  257. continue;
  258. /* we only care if the other candidate is even
  259. * considered as candidate. */
  260. if (list_empty(&tmp_neigh_node->bonding_list))
  261. continue;
  262. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  263. (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
  264. interference_candidate = 1;
  265. break;
  266. }
  267. }
  268. /* don't care further if it is an interference candidate */
  269. if (interference_candidate)
  270. goto candidate_del;
  271. /* this neighbor already is part of our candidate list */
  272. if (!list_empty(&neigh_node->bonding_list))
  273. goto out;
  274. if (!atomic_inc_not_zero(&neigh_node->refcount))
  275. goto out;
  276. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  277. atomic_inc(&orig_node->bond_candidates);
  278. goto out;
  279. candidate_del:
  280. bonding_candidate_del(orig_node, neigh_node);
  281. out:
  282. spin_unlock_bh(&orig_node->neigh_list_lock);
  283. if (router)
  284. neigh_node_free_ref(router);
  285. }
  286. /* copy primary address for bonding */
  287. static void bonding_save_primary(const struct orig_node *orig_node,
  288. struct orig_node *orig_neigh_node,
  289. const struct batman_packet *batman_packet)
  290. {
  291. if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
  292. return;
  293. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  294. }
  295. static void update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
  296. const struct ethhdr *ethhdr,
  297. const struct batman_packet *batman_packet,
  298. struct hard_iface *if_incoming,
  299. const unsigned char *tt_buff, int tt_buff_len,
  300. int is_duplicate)
  301. {
  302. struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
  303. struct neigh_node *router = NULL;
  304. struct orig_node *orig_node_tmp;
  305. struct hlist_node *node;
  306. int tmp_tt_buff_len;
  307. uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
  308. bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
  309. "Searching and updating originator entry of received packet\n");
  310. rcu_read_lock();
  311. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  312. &orig_node->neigh_list, list) {
  313. if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
  314. (tmp_neigh_node->if_incoming == if_incoming) &&
  315. atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
  316. if (neigh_node)
  317. neigh_node_free_ref(neigh_node);
  318. neigh_node = tmp_neigh_node;
  319. continue;
  320. }
  321. if (is_duplicate)
  322. continue;
  323. spin_lock_bh(&tmp_neigh_node->tq_lock);
  324. ring_buffer_set(tmp_neigh_node->tq_recv,
  325. &tmp_neigh_node->tq_index, 0);
  326. tmp_neigh_node->tq_avg =
  327. ring_buffer_avg(tmp_neigh_node->tq_recv);
  328. spin_unlock_bh(&tmp_neigh_node->tq_lock);
  329. }
  330. if (!neigh_node) {
  331. struct orig_node *orig_tmp;
  332. orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
  333. if (!orig_tmp)
  334. goto unlock;
  335. neigh_node = create_neighbor(orig_node, orig_tmp,
  336. ethhdr->h_source, if_incoming);
  337. orig_node_free_ref(orig_tmp);
  338. if (!neigh_node)
  339. goto unlock;
  340. } else
  341. bat_dbg(DBG_BATMAN, bat_priv,
  342. "Updating existing last-hop neighbor of originator\n");
  343. rcu_read_unlock();
  344. orig_node->flags = batman_packet->flags;
  345. neigh_node->last_valid = jiffies;
  346. spin_lock_bh(&neigh_node->tq_lock);
  347. ring_buffer_set(neigh_node->tq_recv,
  348. &neigh_node->tq_index,
  349. batman_packet->tq);
  350. neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
  351. spin_unlock_bh(&neigh_node->tq_lock);
  352. if (!is_duplicate) {
  353. orig_node->last_ttl = batman_packet->ttl;
  354. neigh_node->last_ttl = batman_packet->ttl;
  355. }
  356. bonding_candidate_add(orig_node, neigh_node);
  357. tmp_tt_buff_len = (tt_buff_len > batman_packet->num_tt * ETH_ALEN ?
  358. batman_packet->num_tt * ETH_ALEN : tt_buff_len);
  359. /* if this neighbor already is our next hop there is nothing
  360. * to change */
  361. router = orig_node_get_router(orig_node);
  362. if (router == neigh_node)
  363. goto update_tt;
  364. /* if this neighbor does not offer a better TQ we won't consider it */
  365. if (router && (router->tq_avg > neigh_node->tq_avg))
  366. goto update_tt;
  367. /* if the TQ is the same and the link not more symetric we
  368. * won't consider it either */
  369. if (router && (neigh_node->tq_avg == router->tq_avg)) {
  370. orig_node_tmp = router->orig_node;
  371. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  372. bcast_own_sum_orig =
  373. orig_node_tmp->bcast_own_sum[if_incoming->if_num];
  374. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  375. orig_node_tmp = neigh_node->orig_node;
  376. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  377. bcast_own_sum_neigh =
  378. orig_node_tmp->bcast_own_sum[if_incoming->if_num];
  379. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  380. if (bcast_own_sum_orig >= bcast_own_sum_neigh)
  381. goto update_tt;
  382. }
  383. update_routes(bat_priv, orig_node, neigh_node,
  384. tt_buff, tmp_tt_buff_len);
  385. goto update_gw;
  386. update_tt:
  387. update_routes(bat_priv, orig_node, router,
  388. tt_buff, tmp_tt_buff_len);
  389. update_gw:
  390. if (orig_node->gw_flags != batman_packet->gw_flags)
  391. gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
  392. orig_node->gw_flags = batman_packet->gw_flags;
  393. /* restart gateway selection if fast or late switching was enabled */
  394. if ((orig_node->gw_flags) &&
  395. (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
  396. (atomic_read(&bat_priv->gw_sel_class) > 2))
  397. gw_check_election(bat_priv, orig_node);
  398. goto out;
  399. unlock:
  400. rcu_read_unlock();
  401. out:
  402. if (neigh_node)
  403. neigh_node_free_ref(neigh_node);
  404. if (router)
  405. neigh_node_free_ref(router);
  406. }
  407. /* checks whether the host restarted and is in the protection time.
  408. * returns:
  409. * 0 if the packet is to be accepted
  410. * 1 if the packet is to be ignored.
  411. */
  412. static int window_protected(struct bat_priv *bat_priv,
  413. int32_t seq_num_diff,
  414. unsigned long *last_reset)
  415. {
  416. if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
  417. || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
  418. if (time_after(jiffies, *last_reset +
  419. msecs_to_jiffies(RESET_PROTECTION_MS))) {
  420. *last_reset = jiffies;
  421. bat_dbg(DBG_BATMAN, bat_priv,
  422. "old packet received, start protection\n");
  423. return 0;
  424. } else
  425. return 1;
  426. }
  427. return 0;
  428. }
  429. /* processes a batman packet for all interfaces, adjusts the sequence number and
  430. * finds out whether it is a duplicate.
  431. * returns:
  432. * 1 the packet is a duplicate
  433. * 0 the packet has not yet been received
  434. * -1 the packet is old and has been received while the seqno window
  435. * was protected. Caller should drop it.
  436. */
  437. static int count_real_packets(const struct ethhdr *ethhdr,
  438. const struct batman_packet *batman_packet,
  439. const struct hard_iface *if_incoming)
  440. {
  441. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  442. struct orig_node *orig_node;
  443. struct neigh_node *tmp_neigh_node;
  444. struct hlist_node *node;
  445. int is_duplicate = 0;
  446. int32_t seq_diff;
  447. int need_update = 0;
  448. int set_mark, ret = -1;
  449. orig_node = get_orig_node(bat_priv, batman_packet->orig);
  450. if (!orig_node)
  451. return 0;
  452. spin_lock_bh(&orig_node->ogm_cnt_lock);
  453. seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
  454. /* signalize caller that the packet is to be dropped. */
  455. if (window_protected(bat_priv, seq_diff,
  456. &orig_node->batman_seqno_reset))
  457. goto out;
  458. rcu_read_lock();
  459. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  460. &orig_node->neigh_list, list) {
  461. is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
  462. orig_node->last_real_seqno,
  463. batman_packet->seqno);
  464. if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
  465. (tmp_neigh_node->if_incoming == if_incoming))
  466. set_mark = 1;
  467. else
  468. set_mark = 0;
  469. /* if the window moved, set the update flag. */
  470. need_update |= bit_get_packet(bat_priv,
  471. tmp_neigh_node->real_bits,
  472. seq_diff, set_mark);
  473. tmp_neigh_node->real_packet_count =
  474. bit_packet_count(tmp_neigh_node->real_bits);
  475. }
  476. rcu_read_unlock();
  477. if (need_update) {
  478. bat_dbg(DBG_BATMAN, bat_priv,
  479. "updating last_seqno: old %d, new %d\n",
  480. orig_node->last_real_seqno, batman_packet->seqno);
  481. orig_node->last_real_seqno = batman_packet->seqno;
  482. }
  483. ret = is_duplicate;
  484. out:
  485. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  486. orig_node_free_ref(orig_node);
  487. return ret;
  488. }
  489. void receive_bat_packet(const struct ethhdr *ethhdr,
  490. struct batman_packet *batman_packet,
  491. const unsigned char *tt_buff, int tt_buff_len,
  492. struct hard_iface *if_incoming)
  493. {
  494. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  495. struct hard_iface *hard_iface;
  496. struct orig_node *orig_neigh_node, *orig_node;
  497. struct neigh_node *router = NULL, *router_router = NULL;
  498. struct neigh_node *orig_neigh_router = NULL;
  499. int has_directlink_flag;
  500. int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
  501. int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
  502. int is_duplicate;
  503. uint32_t if_incoming_seqno;
  504. /* Silently drop when the batman packet is actually not a
  505. * correct packet.
  506. *
  507. * This might happen if a packet is padded (e.g. Ethernet has a
  508. * minimum frame length of 64 byte) and the aggregation interprets
  509. * it as an additional length.
  510. *
  511. * TODO: A more sane solution would be to have a bit in the
  512. * batman_packet to detect whether the packet is the last
  513. * packet in an aggregation. Here we expect that the padding
  514. * is always zero (or not 0x01)
  515. */
  516. if (batman_packet->packet_type != BAT_PACKET)
  517. return;
  518. /* could be changed by schedule_own_packet() */
  519. if_incoming_seqno = atomic_read(&if_incoming->seqno);
  520. has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
  521. is_single_hop_neigh = (compare_eth(ethhdr->h_source,
  522. batman_packet->orig) ? 1 : 0);
  523. bat_dbg(DBG_BATMAN, bat_priv,
  524. "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
  525. "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
  526. "TTL %d, V %d, IDF %d)\n",
  527. ethhdr->h_source, if_incoming->net_dev->name,
  528. if_incoming->net_dev->dev_addr, batman_packet->orig,
  529. batman_packet->prev_sender, batman_packet->seqno,
  530. batman_packet->tq, batman_packet->ttl, batman_packet->version,
  531. has_directlink_flag);
  532. rcu_read_lock();
  533. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  534. if (hard_iface->if_status != IF_ACTIVE)
  535. continue;
  536. if (hard_iface->soft_iface != if_incoming->soft_iface)
  537. continue;
  538. if (compare_eth(ethhdr->h_source,
  539. hard_iface->net_dev->dev_addr))
  540. is_my_addr = 1;
  541. if (compare_eth(batman_packet->orig,
  542. hard_iface->net_dev->dev_addr))
  543. is_my_orig = 1;
  544. if (compare_eth(batman_packet->prev_sender,
  545. hard_iface->net_dev->dev_addr))
  546. is_my_oldorig = 1;
  547. if (is_broadcast_ether_addr(ethhdr->h_source))
  548. is_broadcast = 1;
  549. }
  550. rcu_read_unlock();
  551. if (batman_packet->version != COMPAT_VERSION) {
  552. bat_dbg(DBG_BATMAN, bat_priv,
  553. "Drop packet: incompatible batman version (%i)\n",
  554. batman_packet->version);
  555. return;
  556. }
  557. if (is_my_addr) {
  558. bat_dbg(DBG_BATMAN, bat_priv,
  559. "Drop packet: received my own broadcast (sender: %pM"
  560. ")\n",
  561. ethhdr->h_source);
  562. return;
  563. }
  564. if (is_broadcast) {
  565. bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
  566. "ignoring all packets with broadcast source addr (sender: %pM"
  567. ")\n", ethhdr->h_source);
  568. return;
  569. }
  570. if (is_my_orig) {
  571. unsigned long *word;
  572. int offset;
  573. orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
  574. if (!orig_neigh_node)
  575. return;
  576. /* neighbor has to indicate direct link and it has to
  577. * come via the corresponding interface */
  578. /* save packet seqno for bidirectional check */
  579. if (has_directlink_flag &&
  580. compare_eth(if_incoming->net_dev->dev_addr,
  581. batman_packet->orig)) {
  582. offset = if_incoming->if_num * NUM_WORDS;
  583. spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
  584. word = &(orig_neigh_node->bcast_own[offset]);
  585. bit_mark(word,
  586. if_incoming_seqno - batman_packet->seqno - 2);
  587. orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
  588. bit_packet_count(word);
  589. spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
  590. }
  591. bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
  592. "originator packet from myself (via neighbor)\n");
  593. orig_node_free_ref(orig_neigh_node);
  594. return;
  595. }
  596. if (is_my_oldorig) {
  597. bat_dbg(DBG_BATMAN, bat_priv,
  598. "Drop packet: ignoring all rebroadcast echos (sender: "
  599. "%pM)\n", ethhdr->h_source);
  600. return;
  601. }
  602. orig_node = get_orig_node(bat_priv, batman_packet->orig);
  603. if (!orig_node)
  604. return;
  605. is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
  606. if (is_duplicate == -1) {
  607. bat_dbg(DBG_BATMAN, bat_priv,
  608. "Drop packet: packet within seqno protection time "
  609. "(sender: %pM)\n", ethhdr->h_source);
  610. goto out;
  611. }
  612. if (batman_packet->tq == 0) {
  613. bat_dbg(DBG_BATMAN, bat_priv,
  614. "Drop packet: originator packet with tq equal 0\n");
  615. goto out;
  616. }
  617. router = orig_node_get_router(orig_node);
  618. if (router)
  619. router_router = orig_node_get_router(router->orig_node);
  620. /* avoid temporary routing loops */
  621. if (router && router_router &&
  622. (compare_eth(router->addr, batman_packet->prev_sender)) &&
  623. !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
  624. (compare_eth(router->addr, router_router->addr))) {
  625. bat_dbg(DBG_BATMAN, bat_priv,
  626. "Drop packet: ignoring all rebroadcast packets that "
  627. "may make me loop (sender: %pM)\n", ethhdr->h_source);
  628. goto out;
  629. }
  630. /* if sender is a direct neighbor the sender mac equals
  631. * originator mac */
  632. orig_neigh_node = (is_single_hop_neigh ?
  633. orig_node :
  634. get_orig_node(bat_priv, ethhdr->h_source));
  635. if (!orig_neigh_node)
  636. goto out;
  637. orig_neigh_router = orig_node_get_router(orig_neigh_node);
  638. /* drop packet if sender is not a direct neighbor and if we
  639. * don't route towards it */
  640. if (!is_single_hop_neigh && (!orig_neigh_router)) {
  641. bat_dbg(DBG_BATMAN, bat_priv,
  642. "Drop packet: OGM via unknown neighbor!\n");
  643. goto out_neigh;
  644. }
  645. is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
  646. batman_packet, if_incoming);
  647. bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
  648. /* update ranking if it is not a duplicate or has the same
  649. * seqno and similar ttl as the non-duplicate */
  650. if (is_bidirectional &&
  651. (!is_duplicate ||
  652. ((orig_node->last_real_seqno == batman_packet->seqno) &&
  653. (orig_node->last_ttl - 3 <= batman_packet->ttl))))
  654. update_orig(bat_priv, orig_node, ethhdr, batman_packet,
  655. if_incoming, tt_buff, tt_buff_len, is_duplicate);
  656. /* is single hop (direct) neighbor */
  657. if (is_single_hop_neigh) {
  658. /* mark direct link on incoming interface */
  659. schedule_forward_packet(orig_node, ethhdr, batman_packet,
  660. 1, tt_buff_len, if_incoming);
  661. bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
  662. "rebroadcast neighbor packet with direct link flag\n");
  663. goto out_neigh;
  664. }
  665. /* multihop originator */
  666. if (!is_bidirectional) {
  667. bat_dbg(DBG_BATMAN, bat_priv,
  668. "Drop packet: not received via bidirectional link\n");
  669. goto out_neigh;
  670. }
  671. if (is_duplicate) {
  672. bat_dbg(DBG_BATMAN, bat_priv,
  673. "Drop packet: duplicate packet received\n");
  674. goto out_neigh;
  675. }
  676. bat_dbg(DBG_BATMAN, bat_priv,
  677. "Forwarding packet: rebroadcast originator packet\n");
  678. schedule_forward_packet(orig_node, ethhdr, batman_packet,
  679. 0, tt_buff_len, if_incoming);
  680. out_neigh:
  681. if ((orig_neigh_node) && (!is_single_hop_neigh))
  682. orig_node_free_ref(orig_neigh_node);
  683. out:
  684. if (router)
  685. neigh_node_free_ref(router);
  686. if (router_router)
  687. neigh_node_free_ref(router_router);
  688. if (orig_neigh_router)
  689. neigh_node_free_ref(orig_neigh_router);
  690. orig_node_free_ref(orig_node);
  691. }
  692. int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
  693. {
  694. struct ethhdr *ethhdr;
  695. /* drop packet if it has not necessary minimum size */
  696. if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
  697. return NET_RX_DROP;
  698. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  699. /* packet with broadcast indication but unicast recipient */
  700. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  701. return NET_RX_DROP;
  702. /* packet with broadcast sender address */
  703. if (is_broadcast_ether_addr(ethhdr->h_source))
  704. return NET_RX_DROP;
  705. /* create a copy of the skb, if needed, to modify it. */
  706. if (skb_cow(skb, 0) < 0)
  707. return NET_RX_DROP;
  708. /* keep skb linear */
  709. if (skb_linearize(skb) < 0)
  710. return NET_RX_DROP;
  711. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  712. receive_aggr_bat_packet(ethhdr,
  713. skb->data,
  714. skb_headlen(skb),
  715. hard_iface);
  716. kfree_skb(skb);
  717. return NET_RX_SUCCESS;
  718. }
  719. static int recv_my_icmp_packet(struct bat_priv *bat_priv,
  720. struct sk_buff *skb, size_t icmp_len)
  721. {
  722. struct hard_iface *primary_if = NULL;
  723. struct orig_node *orig_node = NULL;
  724. struct neigh_node *router = NULL;
  725. struct icmp_packet_rr *icmp_packet;
  726. int ret = NET_RX_DROP;
  727. icmp_packet = (struct icmp_packet_rr *)skb->data;
  728. /* add data to device queue */
  729. if (icmp_packet->msg_type != ECHO_REQUEST) {
  730. bat_socket_receive_packet(icmp_packet, icmp_len);
  731. goto out;
  732. }
  733. primary_if = primary_if_get_selected(bat_priv);
  734. if (!primary_if)
  735. goto out;
  736. /* answer echo request (ping) */
  737. /* get routing information */
  738. orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
  739. if (!orig_node)
  740. goto out;
  741. router = orig_node_get_router(orig_node);
  742. if (!router)
  743. goto out;
  744. /* create a copy of the skb, if needed, to modify it. */
  745. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  746. goto out;
  747. icmp_packet = (struct icmp_packet_rr *)skb->data;
  748. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  749. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  750. icmp_packet->msg_type = ECHO_REPLY;
  751. icmp_packet->ttl = TTL;
  752. send_skb_packet(skb, router->if_incoming, router->addr);
  753. ret = NET_RX_SUCCESS;
  754. out:
  755. if (primary_if)
  756. hardif_free_ref(primary_if);
  757. if (router)
  758. neigh_node_free_ref(router);
  759. if (orig_node)
  760. orig_node_free_ref(orig_node);
  761. return ret;
  762. }
  763. static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
  764. struct sk_buff *skb)
  765. {
  766. struct hard_iface *primary_if = NULL;
  767. struct orig_node *orig_node = NULL;
  768. struct neigh_node *router = NULL;
  769. struct icmp_packet *icmp_packet;
  770. int ret = NET_RX_DROP;
  771. icmp_packet = (struct icmp_packet *)skb->data;
  772. /* send TTL exceeded if packet is an echo request (traceroute) */
  773. if (icmp_packet->msg_type != ECHO_REQUEST) {
  774. pr_debug("Warning - can't forward icmp packet from %pM to "
  775. "%pM: ttl exceeded\n", icmp_packet->orig,
  776. icmp_packet->dst);
  777. goto out;
  778. }
  779. primary_if = primary_if_get_selected(bat_priv);
  780. if (!primary_if)
  781. goto out;
  782. /* get routing information */
  783. orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
  784. if (!orig_node)
  785. goto out;
  786. router = orig_node_get_router(orig_node);
  787. if (!router)
  788. goto out;
  789. /* create a copy of the skb, if needed, to modify it. */
  790. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  791. goto out;
  792. icmp_packet = (struct icmp_packet *)skb->data;
  793. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  794. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  795. icmp_packet->msg_type = TTL_EXCEEDED;
  796. icmp_packet->ttl = TTL;
  797. send_skb_packet(skb, router->if_incoming, router->addr);
  798. ret = NET_RX_SUCCESS;
  799. out:
  800. if (primary_if)
  801. hardif_free_ref(primary_if);
  802. if (router)
  803. neigh_node_free_ref(router);
  804. if (orig_node)
  805. orig_node_free_ref(orig_node);
  806. return ret;
  807. }
  808. int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  809. {
  810. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  811. struct icmp_packet_rr *icmp_packet;
  812. struct ethhdr *ethhdr;
  813. struct orig_node *orig_node = NULL;
  814. struct neigh_node *router = NULL;
  815. int hdr_size = sizeof(struct icmp_packet);
  816. int ret = NET_RX_DROP;
  817. /**
  818. * we truncate all incoming icmp packets if they don't match our size
  819. */
  820. if (skb->len >= sizeof(struct icmp_packet_rr))
  821. hdr_size = sizeof(struct icmp_packet_rr);
  822. /* drop packet if it has not necessary minimum size */
  823. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  824. goto out;
  825. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  826. /* packet with unicast indication but broadcast recipient */
  827. if (is_broadcast_ether_addr(ethhdr->h_dest))
  828. goto out;
  829. /* packet with broadcast sender address */
  830. if (is_broadcast_ether_addr(ethhdr->h_source))
  831. goto out;
  832. /* not for me */
  833. if (!is_my_mac(ethhdr->h_dest))
  834. goto out;
  835. icmp_packet = (struct icmp_packet_rr *)skb->data;
  836. /* add record route information if not full */
  837. if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
  838. (icmp_packet->rr_cur < BAT_RR_LEN)) {
  839. memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
  840. ethhdr->h_dest, ETH_ALEN);
  841. icmp_packet->rr_cur++;
  842. }
  843. /* packet for me */
  844. if (is_my_mac(icmp_packet->dst))
  845. return recv_my_icmp_packet(bat_priv, skb, hdr_size);
  846. /* TTL exceeded */
  847. if (icmp_packet->ttl < 2)
  848. return recv_icmp_ttl_exceeded(bat_priv, skb);
  849. /* get routing information */
  850. orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
  851. if (!orig_node)
  852. goto out;
  853. router = orig_node_get_router(orig_node);
  854. if (!router)
  855. goto out;
  856. /* create a copy of the skb, if needed, to modify it. */
  857. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  858. goto out;
  859. icmp_packet = (struct icmp_packet_rr *)skb->data;
  860. /* decrement ttl */
  861. icmp_packet->ttl--;
  862. /* route it */
  863. send_skb_packet(skb, router->if_incoming, router->addr);
  864. ret = NET_RX_SUCCESS;
  865. out:
  866. if (router)
  867. neigh_node_free_ref(router);
  868. if (orig_node)
  869. orig_node_free_ref(orig_node);
  870. return ret;
  871. }
  872. /* In the bonding case, send the packets in a round
  873. * robin fashion over the remaining interfaces.
  874. *
  875. * This method rotates the bonding list and increases the
  876. * returned router's refcount. */
  877. static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
  878. const struct hard_iface *recv_if)
  879. {
  880. struct neigh_node *tmp_neigh_node;
  881. struct neigh_node *router = NULL, *first_candidate = NULL;
  882. rcu_read_lock();
  883. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  884. bonding_list) {
  885. if (!first_candidate)
  886. first_candidate = tmp_neigh_node;
  887. /* recv_if == NULL on the first node. */
  888. if (tmp_neigh_node->if_incoming == recv_if)
  889. continue;
  890. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  891. continue;
  892. router = tmp_neigh_node;
  893. break;
  894. }
  895. /* use the first candidate if nothing was found. */
  896. if (!router && first_candidate &&
  897. atomic_inc_not_zero(&first_candidate->refcount))
  898. router = first_candidate;
  899. if (!router)
  900. goto out;
  901. /* selected should point to the next element
  902. * after the current router */
  903. spin_lock_bh(&primary_orig->neigh_list_lock);
  904. /* this is a list_move(), which unfortunately
  905. * does not exist as rcu version */
  906. list_del_rcu(&primary_orig->bond_list);
  907. list_add_rcu(&primary_orig->bond_list,
  908. &router->bonding_list);
  909. spin_unlock_bh(&primary_orig->neigh_list_lock);
  910. out:
  911. rcu_read_unlock();
  912. return router;
  913. }
  914. /* Interface Alternating: Use the best of the
  915. * remaining candidates which are not using
  916. * this interface.
  917. *
  918. * Increases the returned router's refcount */
  919. static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
  920. const struct hard_iface *recv_if)
  921. {
  922. struct neigh_node *tmp_neigh_node;
  923. struct neigh_node *router = NULL, *first_candidate = NULL;
  924. rcu_read_lock();
  925. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  926. bonding_list) {
  927. if (!first_candidate)
  928. first_candidate = tmp_neigh_node;
  929. /* recv_if == NULL on the first node. */
  930. if (tmp_neigh_node->if_incoming == recv_if)
  931. continue;
  932. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  933. continue;
  934. /* if we don't have a router yet
  935. * or this one is better, choose it. */
  936. if ((!router) ||
  937. (tmp_neigh_node->tq_avg > router->tq_avg)) {
  938. /* decrement refcount of
  939. * previously selected router */
  940. if (router)
  941. neigh_node_free_ref(router);
  942. router = tmp_neigh_node;
  943. atomic_inc_not_zero(&router->refcount);
  944. }
  945. neigh_node_free_ref(tmp_neigh_node);
  946. }
  947. /* use the first candidate if nothing was found. */
  948. if (!router && first_candidate &&
  949. atomic_inc_not_zero(&first_candidate->refcount))
  950. router = first_candidate;
  951. rcu_read_unlock();
  952. return router;
  953. }
  954. /* find a suitable router for this originator, and use
  955. * bonding if possible. increases the found neighbors
  956. * refcount.*/
  957. struct neigh_node *find_router(struct bat_priv *bat_priv,
  958. struct orig_node *orig_node,
  959. const struct hard_iface *recv_if)
  960. {
  961. struct orig_node *primary_orig_node;
  962. struct orig_node *router_orig;
  963. struct neigh_node *router;
  964. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  965. int bonding_enabled;
  966. if (!orig_node)
  967. return NULL;
  968. router = orig_node_get_router(orig_node);
  969. if (!router)
  970. goto err;
  971. /* without bonding, the first node should
  972. * always choose the default router. */
  973. bonding_enabled = atomic_read(&bat_priv->bonding);
  974. rcu_read_lock();
  975. /* select default router to output */
  976. router_orig = router->orig_node;
  977. if (!router_orig)
  978. goto err_unlock;
  979. if ((!recv_if) && (!bonding_enabled))
  980. goto return_router;
  981. /* if we have something in the primary_addr, we can search
  982. * for a potential bonding candidate. */
  983. if (compare_eth(router_orig->primary_addr, zero_mac))
  984. goto return_router;
  985. /* find the orig_node which has the primary interface. might
  986. * even be the same as our router_orig in many cases */
  987. if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
  988. primary_orig_node = router_orig;
  989. } else {
  990. primary_orig_node = orig_hash_find(bat_priv,
  991. router_orig->primary_addr);
  992. if (!primary_orig_node)
  993. goto return_router;
  994. orig_node_free_ref(primary_orig_node);
  995. }
  996. /* with less than 2 candidates, we can't do any
  997. * bonding and prefer the original router. */
  998. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  999. goto return_router;
  1000. /* all nodes between should choose a candidate which
  1001. * is is not on the interface where the packet came
  1002. * in. */
  1003. neigh_node_free_ref(router);
  1004. if (bonding_enabled)
  1005. router = find_bond_router(primary_orig_node, recv_if);
  1006. else
  1007. router = find_ifalter_router(primary_orig_node, recv_if);
  1008. return_router:
  1009. if (router && router->if_incoming->if_status != IF_ACTIVE)
  1010. goto err_unlock;
  1011. rcu_read_unlock();
  1012. return router;
  1013. err_unlock:
  1014. rcu_read_unlock();
  1015. err:
  1016. if (router)
  1017. neigh_node_free_ref(router);
  1018. return NULL;
  1019. }
  1020. static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
  1021. {
  1022. struct ethhdr *ethhdr;
  1023. /* drop packet if it has not necessary minimum size */
  1024. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1025. return -1;
  1026. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1027. /* packet with unicast indication but broadcast recipient */
  1028. if (is_broadcast_ether_addr(ethhdr->h_dest))
  1029. return -1;
  1030. /* packet with broadcast sender address */
  1031. if (is_broadcast_ether_addr(ethhdr->h_source))
  1032. return -1;
  1033. /* not for me */
  1034. if (!is_my_mac(ethhdr->h_dest))
  1035. return -1;
  1036. return 0;
  1037. }
  1038. int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1039. {
  1040. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1041. struct orig_node *orig_node = NULL;
  1042. struct neigh_node *neigh_node = NULL;
  1043. struct unicast_packet *unicast_packet;
  1044. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1045. int ret = NET_RX_DROP;
  1046. struct sk_buff *new_skb;
  1047. unicast_packet = (struct unicast_packet *)skb->data;
  1048. /* TTL exceeded */
  1049. if (unicast_packet->ttl < 2) {
  1050. pr_debug("Warning - can't forward unicast packet from %pM to "
  1051. "%pM: ttl exceeded\n", ethhdr->h_source,
  1052. unicast_packet->dest);
  1053. goto out;
  1054. }
  1055. /* get routing information */
  1056. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  1057. if (!orig_node)
  1058. goto out;
  1059. /* find_router() increases neigh_nodes refcount if found. */
  1060. neigh_node = find_router(bat_priv, orig_node, recv_if);
  1061. if (!neigh_node)
  1062. goto out;
  1063. /* create a copy of the skb, if needed, to modify it. */
  1064. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  1065. goto out;
  1066. unicast_packet = (struct unicast_packet *)skb->data;
  1067. if (unicast_packet->packet_type == BAT_UNICAST &&
  1068. atomic_read(&bat_priv->fragmentation) &&
  1069. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  1070. ret = frag_send_skb(skb, bat_priv,
  1071. neigh_node->if_incoming, neigh_node->addr);
  1072. goto out;
  1073. }
  1074. if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
  1075. frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
  1076. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  1077. if (ret == NET_RX_DROP)
  1078. goto out;
  1079. /* packet was buffered for late merge */
  1080. if (!new_skb) {
  1081. ret = NET_RX_SUCCESS;
  1082. goto out;
  1083. }
  1084. skb = new_skb;
  1085. unicast_packet = (struct unicast_packet *)skb->data;
  1086. }
  1087. /* decrement ttl */
  1088. unicast_packet->ttl--;
  1089. /* route it */
  1090. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1091. ret = NET_RX_SUCCESS;
  1092. out:
  1093. if (neigh_node)
  1094. neigh_node_free_ref(neigh_node);
  1095. if (orig_node)
  1096. orig_node_free_ref(orig_node);
  1097. return ret;
  1098. }
  1099. int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1100. {
  1101. struct unicast_packet *unicast_packet;
  1102. int hdr_size = sizeof(*unicast_packet);
  1103. if (check_unicast_packet(skb, hdr_size) < 0)
  1104. return NET_RX_DROP;
  1105. unicast_packet = (struct unicast_packet *)skb->data;
  1106. /* packet for me */
  1107. if (is_my_mac(unicast_packet->dest)) {
  1108. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  1109. return NET_RX_SUCCESS;
  1110. }
  1111. return route_unicast_packet(skb, recv_if);
  1112. }
  1113. int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1114. {
  1115. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1116. struct unicast_frag_packet *unicast_packet;
  1117. int hdr_size = sizeof(*unicast_packet);
  1118. struct sk_buff *new_skb = NULL;
  1119. int ret;
  1120. if (check_unicast_packet(skb, hdr_size) < 0)
  1121. return NET_RX_DROP;
  1122. unicast_packet = (struct unicast_frag_packet *)skb->data;
  1123. /* packet for me */
  1124. if (is_my_mac(unicast_packet->dest)) {
  1125. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  1126. if (ret == NET_RX_DROP)
  1127. return NET_RX_DROP;
  1128. /* packet was buffered for late merge */
  1129. if (!new_skb)
  1130. return NET_RX_SUCCESS;
  1131. interface_rx(recv_if->soft_iface, new_skb, recv_if,
  1132. sizeof(struct unicast_packet));
  1133. return NET_RX_SUCCESS;
  1134. }
  1135. return route_unicast_packet(skb, recv_if);
  1136. }
  1137. int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1138. {
  1139. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1140. struct orig_node *orig_node = NULL;
  1141. struct bcast_packet *bcast_packet;
  1142. struct ethhdr *ethhdr;
  1143. int hdr_size = sizeof(*bcast_packet);
  1144. int ret = NET_RX_DROP;
  1145. int32_t seq_diff;
  1146. /* drop packet if it has not necessary minimum size */
  1147. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1148. goto out;
  1149. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1150. /* packet with broadcast indication but unicast recipient */
  1151. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  1152. goto out;
  1153. /* packet with broadcast sender address */
  1154. if (is_broadcast_ether_addr(ethhdr->h_source))
  1155. goto out;
  1156. /* ignore broadcasts sent by myself */
  1157. if (is_my_mac(ethhdr->h_source))
  1158. goto out;
  1159. bcast_packet = (struct bcast_packet *)skb->data;
  1160. /* ignore broadcasts originated by myself */
  1161. if (is_my_mac(bcast_packet->orig))
  1162. goto out;
  1163. if (bcast_packet->ttl < 2)
  1164. goto out;
  1165. orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
  1166. if (!orig_node)
  1167. goto out;
  1168. spin_lock_bh(&orig_node->bcast_seqno_lock);
  1169. /* check whether the packet is a duplicate */
  1170. if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  1171. ntohl(bcast_packet->seqno)))
  1172. goto spin_unlock;
  1173. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  1174. /* check whether the packet is old and the host just restarted. */
  1175. if (window_protected(bat_priv, seq_diff,
  1176. &orig_node->bcast_seqno_reset))
  1177. goto spin_unlock;
  1178. /* mark broadcast in flood history, update window position
  1179. * if required. */
  1180. if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  1181. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  1182. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1183. /* rebroadcast packet */
  1184. add_bcast_packet_to_list(bat_priv, skb);
  1185. /* broadcast for me */
  1186. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  1187. ret = NET_RX_SUCCESS;
  1188. goto out;
  1189. spin_unlock:
  1190. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1191. out:
  1192. if (orig_node)
  1193. orig_node_free_ref(orig_node);
  1194. return ret;
  1195. }
  1196. int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1197. {
  1198. struct vis_packet *vis_packet;
  1199. struct ethhdr *ethhdr;
  1200. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1201. int hdr_size = sizeof(*vis_packet);
  1202. /* keep skb linear */
  1203. if (skb_linearize(skb) < 0)
  1204. return NET_RX_DROP;
  1205. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1206. return NET_RX_DROP;
  1207. vis_packet = (struct vis_packet *)skb->data;
  1208. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1209. /* not for me */
  1210. if (!is_my_mac(ethhdr->h_dest))
  1211. return NET_RX_DROP;
  1212. /* ignore own packets */
  1213. if (is_my_mac(vis_packet->vis_orig))
  1214. return NET_RX_DROP;
  1215. if (is_my_mac(vis_packet->sender_orig))
  1216. return NET_RX_DROP;
  1217. switch (vis_packet->vis_type) {
  1218. case VIS_TYPE_SERVER_SYNC:
  1219. receive_server_sync_packet(bat_priv, vis_packet,
  1220. skb_headlen(skb));
  1221. break;
  1222. case VIS_TYPE_CLIENT_UPDATE:
  1223. receive_client_update_packet(bat_priv, vis_packet,
  1224. skb_headlen(skb));
  1225. break;
  1226. default: /* ignore unknown packet */
  1227. break;
  1228. }
  1229. /* We take a copy of the data in the packet, so we should
  1230. always free the skbuf. */
  1231. return NET_RX_DROP;
  1232. }