routing.c 41 KB

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