vis.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /* Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
  2. *
  3. * Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "send.h"
  21. #include "translation-table.h"
  22. #include "vis.h"
  23. #include "soft-interface.h"
  24. #include "hard-interface.h"
  25. #include "hash.h"
  26. #include "originator.h"
  27. #define MAX_VIS_PACKET_SIZE 1000
  28. static void batadv_start_vis_timer(struct bat_priv *bat_priv);
  29. /* free the info */
  30. static void batadv_free_info(struct kref *ref)
  31. {
  32. struct vis_info *info = container_of(ref, struct vis_info, refcount);
  33. struct bat_priv *bat_priv = info->bat_priv;
  34. struct recvlist_node *entry, *tmp;
  35. list_del_init(&info->send_list);
  36. spin_lock_bh(&bat_priv->vis_list_lock);
  37. list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
  38. list_del(&entry->list);
  39. kfree(entry);
  40. }
  41. spin_unlock_bh(&bat_priv->vis_list_lock);
  42. kfree_skb(info->skb_packet);
  43. kfree(info);
  44. }
  45. /* Compare two vis packets, used by the hashing algorithm */
  46. static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
  47. {
  48. const struct vis_info *d1, *d2;
  49. const struct vis_packet *p1, *p2;
  50. d1 = container_of(node, struct vis_info, hash_entry);
  51. d2 = data2;
  52. p1 = (struct vis_packet *)d1->skb_packet->data;
  53. p2 = (struct vis_packet *)d2->skb_packet->data;
  54. return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
  55. }
  56. /* hash function to choose an entry in a hash table of given size
  57. * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
  58. */
  59. static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
  60. {
  61. const struct vis_info *vis_info = data;
  62. const struct vis_packet *packet;
  63. const unsigned char *key;
  64. uint32_t hash = 0;
  65. size_t i;
  66. packet = (struct vis_packet *)vis_info->skb_packet->data;
  67. key = packet->vis_orig;
  68. for (i = 0; i < ETH_ALEN; i++) {
  69. hash += key[i];
  70. hash += (hash << 10);
  71. hash ^= (hash >> 6);
  72. }
  73. hash += (hash << 3);
  74. hash ^= (hash >> 11);
  75. hash += (hash << 15);
  76. return hash % size;
  77. }
  78. static struct vis_info *batadv_vis_hash_find(struct bat_priv *bat_priv,
  79. const void *data)
  80. {
  81. struct hashtable_t *hash = bat_priv->vis_hash;
  82. struct hlist_head *head;
  83. struct hlist_node *node;
  84. struct vis_info *vis_info, *vis_info_tmp = NULL;
  85. uint32_t index;
  86. if (!hash)
  87. return NULL;
  88. index = batadv_vis_info_choose(data, hash->size);
  89. head = &hash->table[index];
  90. rcu_read_lock();
  91. hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
  92. if (!batadv_vis_info_cmp(node, data))
  93. continue;
  94. vis_info_tmp = vis_info;
  95. break;
  96. }
  97. rcu_read_unlock();
  98. return vis_info_tmp;
  99. }
  100. /* insert interface to the list of interfaces of one originator, if it
  101. * does not already exist in the list
  102. */
  103. static void batadv_vis_data_insert_interface(const uint8_t *interface,
  104. struct hlist_head *if_list,
  105. bool primary)
  106. {
  107. struct if_list_entry *entry;
  108. struct hlist_node *pos;
  109. hlist_for_each_entry(entry, pos, if_list, list) {
  110. if (batadv_compare_eth(entry->addr, interface))
  111. return;
  112. }
  113. /* it's a new address, add it to the list */
  114. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  115. if (!entry)
  116. return;
  117. memcpy(entry->addr, interface, ETH_ALEN);
  118. entry->primary = primary;
  119. hlist_add_head(&entry->list, if_list);
  120. }
  121. static ssize_t batadv_vis_prim_sec(char *buff, const struct hlist_head *if_list)
  122. {
  123. struct if_list_entry *entry;
  124. struct hlist_node *pos;
  125. size_t len = 0;
  126. hlist_for_each_entry(entry, pos, if_list, list) {
  127. if (entry->primary)
  128. len += sprintf(buff + len, "PRIMARY, ");
  129. else
  130. len += sprintf(buff + len, "SEC %pM, ", entry->addr);
  131. }
  132. return len;
  133. }
  134. static size_t batadv_vis_cnt_prim_sec(struct hlist_head *if_list)
  135. {
  136. struct if_list_entry *entry;
  137. struct hlist_node *pos;
  138. size_t count = 0;
  139. hlist_for_each_entry(entry, pos, if_list, list) {
  140. if (entry->primary)
  141. count += 9;
  142. else
  143. count += 23;
  144. }
  145. return count;
  146. }
  147. /* read an entry */
  148. static ssize_t batadv_vis_data_read_entry(char *buff,
  149. const struct vis_info_entry *entry,
  150. const uint8_t *src, bool primary)
  151. {
  152. /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
  153. if (primary && entry->quality == 0)
  154. return sprintf(buff, "TT %pM, ", entry->dest);
  155. else if (batadv_compare_eth(entry->src, src))
  156. return sprintf(buff, "TQ %pM %d, ", entry->dest,
  157. entry->quality);
  158. return 0;
  159. }
  160. int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
  161. {
  162. struct hard_iface *primary_if;
  163. struct hlist_node *node;
  164. struct hlist_head *head;
  165. struct vis_info *info;
  166. struct vis_packet *packet;
  167. struct vis_info_entry *entries;
  168. struct net_device *net_dev = (struct net_device *)seq->private;
  169. struct bat_priv *bat_priv = netdev_priv(net_dev);
  170. struct hashtable_t *hash = bat_priv->vis_hash;
  171. HLIST_HEAD(vis_if_list);
  172. struct if_list_entry *entry;
  173. struct hlist_node *pos, *n;
  174. uint32_t i;
  175. int j, ret = 0;
  176. int vis_server = atomic_read(&bat_priv->vis_mode);
  177. size_t buff_pos, buf_size;
  178. char *buff;
  179. primary_if = batadv_primary_if_get_selected(bat_priv);
  180. if (!primary_if)
  181. goto out;
  182. if (vis_server == VIS_TYPE_CLIENT_UPDATE)
  183. goto out;
  184. buf_size = 1;
  185. /* Estimate length */
  186. spin_lock_bh(&bat_priv->vis_hash_lock);
  187. for (i = 0; i < hash->size; i++) {
  188. head = &hash->table[i];
  189. rcu_read_lock();
  190. hlist_for_each_entry_rcu(info, node, head, hash_entry) {
  191. packet = (struct vis_packet *)info->skb_packet->data;
  192. entries = (struct vis_info_entry *)
  193. ((char *)packet + sizeof(*packet));
  194. batadv_vis_data_insert_interface(packet->vis_orig,
  195. &vis_if_list, true);
  196. for (j = 0; j < packet->entries; j++) {
  197. if (entries[j].quality == 0)
  198. continue;
  199. if (batadv_compare_eth(entries[j].src,
  200. packet->vis_orig))
  201. continue;
  202. batadv_vis_data_insert_interface(entries[j].src,
  203. &vis_if_list,
  204. false);
  205. }
  206. hlist_for_each_entry(entry, pos, &vis_if_list, list) {
  207. buf_size += 18 + 26 * packet->entries;
  208. /* add primary/secondary records */
  209. if (batadv_compare_eth(entry->addr,
  210. packet->vis_orig))
  211. buf_size +=
  212. batadv_vis_cnt_prim_sec(&vis_if_list);
  213. buf_size += 1;
  214. }
  215. hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
  216. list) {
  217. hlist_del(&entry->list);
  218. kfree(entry);
  219. }
  220. }
  221. rcu_read_unlock();
  222. }
  223. buff = kmalloc(buf_size, GFP_ATOMIC);
  224. if (!buff) {
  225. spin_unlock_bh(&bat_priv->vis_hash_lock);
  226. ret = -ENOMEM;
  227. goto out;
  228. }
  229. buff[0] = '\0';
  230. buff_pos = 0;
  231. for (i = 0; i < hash->size; i++) {
  232. head = &hash->table[i];
  233. rcu_read_lock();
  234. hlist_for_each_entry_rcu(info, node, head, hash_entry) {
  235. packet = (struct vis_packet *)info->skb_packet->data;
  236. entries = (struct vis_info_entry *)
  237. ((char *)packet + sizeof(*packet));
  238. batadv_vis_data_insert_interface(packet->vis_orig,
  239. &vis_if_list, true);
  240. for (j = 0; j < packet->entries; j++) {
  241. if (entries[j].quality == 0)
  242. continue;
  243. if (batadv_compare_eth(entries[j].src,
  244. packet->vis_orig))
  245. continue;
  246. batadv_vis_data_insert_interface(entries[j].src,
  247. &vis_if_list,
  248. false);
  249. }
  250. hlist_for_each_entry(entry, pos, &vis_if_list, list) {
  251. buff_pos += sprintf(buff + buff_pos, "%pM,",
  252. entry->addr);
  253. for (j = 0; j < packet->entries; j++)
  254. buff_pos += batadv_vis_data_read_entry(
  255. buff + buff_pos,
  256. &entries[j],
  257. entry->addr,
  258. entry->primary);
  259. /* add primary/secondary records */
  260. if (batadv_compare_eth(entry->addr,
  261. packet->vis_orig))
  262. buff_pos +=
  263. batadv_vis_prim_sec(buff + buff_pos,
  264. &vis_if_list);
  265. buff_pos += sprintf(buff + buff_pos, "\n");
  266. }
  267. hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
  268. list) {
  269. hlist_del(&entry->list);
  270. kfree(entry);
  271. }
  272. }
  273. rcu_read_unlock();
  274. }
  275. spin_unlock_bh(&bat_priv->vis_hash_lock);
  276. seq_printf(seq, "%s", buff);
  277. kfree(buff);
  278. out:
  279. if (primary_if)
  280. batadv_hardif_free_ref(primary_if);
  281. return ret;
  282. }
  283. /* add the info packet to the send list, if it was not
  284. * already linked in.
  285. */
  286. static void batadv_send_list_add(struct bat_priv *bat_priv,
  287. struct vis_info *info)
  288. {
  289. if (list_empty(&info->send_list)) {
  290. kref_get(&info->refcount);
  291. list_add_tail(&info->send_list, &bat_priv->vis_send_list);
  292. }
  293. }
  294. /* delete the info packet from the send list, if it was
  295. * linked in.
  296. */
  297. static void batadv_send_list_del(struct vis_info *info)
  298. {
  299. if (!list_empty(&info->send_list)) {
  300. list_del_init(&info->send_list);
  301. kref_put(&info->refcount, batadv_free_info);
  302. }
  303. }
  304. /* tries to add one entry to the receive list. */
  305. static void batadv_recv_list_add(struct bat_priv *bat_priv,
  306. struct list_head *recv_list, const char *mac)
  307. {
  308. struct recvlist_node *entry;
  309. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  310. if (!entry)
  311. return;
  312. memcpy(entry->mac, mac, ETH_ALEN);
  313. spin_lock_bh(&bat_priv->vis_list_lock);
  314. list_add_tail(&entry->list, recv_list);
  315. spin_unlock_bh(&bat_priv->vis_list_lock);
  316. }
  317. /* returns 1 if this mac is in the recv_list */
  318. static int batadv_recv_list_is_in(struct bat_priv *bat_priv,
  319. const struct list_head *recv_list,
  320. const char *mac)
  321. {
  322. const struct recvlist_node *entry;
  323. spin_lock_bh(&bat_priv->vis_list_lock);
  324. list_for_each_entry(entry, recv_list, list) {
  325. if (batadv_compare_eth(entry->mac, mac)) {
  326. spin_unlock_bh(&bat_priv->vis_list_lock);
  327. return 1;
  328. }
  329. }
  330. spin_unlock_bh(&bat_priv->vis_list_lock);
  331. return 0;
  332. }
  333. /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
  334. * broken.. ). vis hash must be locked outside. is_new is set when the packet
  335. * is newer than old entries in the hash.
  336. */
  337. static struct vis_info *batadv_add_packet(struct bat_priv *bat_priv,
  338. struct vis_packet *vis_packet,
  339. int vis_info_len, int *is_new,
  340. int make_broadcast)
  341. {
  342. struct vis_info *info, *old_info;
  343. struct vis_packet *search_packet, *old_packet;
  344. struct vis_info search_elem;
  345. struct vis_packet *packet;
  346. int hash_added;
  347. *is_new = 0;
  348. /* sanity check */
  349. if (!bat_priv->vis_hash)
  350. return NULL;
  351. /* see if the packet is already in vis_hash */
  352. search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
  353. if (!search_elem.skb_packet)
  354. return NULL;
  355. search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
  356. sizeof(*search_packet));
  357. memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
  358. old_info = batadv_vis_hash_find(bat_priv, &search_elem);
  359. kfree_skb(search_elem.skb_packet);
  360. if (old_info) {
  361. old_packet = (struct vis_packet *)old_info->skb_packet->data;
  362. if (!batadv_seq_after(ntohl(vis_packet->seqno),
  363. ntohl(old_packet->seqno))) {
  364. if (old_packet->seqno == vis_packet->seqno) {
  365. batadv_recv_list_add(bat_priv,
  366. &old_info->recv_list,
  367. vis_packet->sender_orig);
  368. return old_info;
  369. } else {
  370. /* newer packet is already in hash. */
  371. return NULL;
  372. }
  373. }
  374. /* remove old entry */
  375. batadv_hash_remove(bat_priv->vis_hash, batadv_vis_info_cmp,
  376. batadv_vis_info_choose, old_info);
  377. batadv_send_list_del(old_info);
  378. kref_put(&old_info->refcount, batadv_free_info);
  379. }
  380. info = kmalloc(sizeof(*info), GFP_ATOMIC);
  381. if (!info)
  382. return NULL;
  383. info->skb_packet = dev_alloc_skb(sizeof(*packet) + vis_info_len +
  384. ETH_HLEN);
  385. if (!info->skb_packet) {
  386. kfree(info);
  387. return NULL;
  388. }
  389. skb_reserve(info->skb_packet, ETH_HLEN);
  390. packet = (struct vis_packet *)skb_put(info->skb_packet, sizeof(*packet)
  391. + vis_info_len);
  392. kref_init(&info->refcount);
  393. INIT_LIST_HEAD(&info->send_list);
  394. INIT_LIST_HEAD(&info->recv_list);
  395. info->first_seen = jiffies;
  396. info->bat_priv = bat_priv;
  397. memcpy(packet, vis_packet, sizeof(*packet) + vis_info_len);
  398. /* initialize and add new packet. */
  399. *is_new = 1;
  400. /* Make it a broadcast packet, if required */
  401. if (make_broadcast)
  402. memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
  403. /* repair if entries is longer than packet. */
  404. if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
  405. packet->entries = vis_info_len / sizeof(struct vis_info_entry);
  406. batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
  407. /* try to add it */
  408. hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
  409. batadv_vis_info_choose, info,
  410. &info->hash_entry);
  411. if (hash_added != 0) {
  412. /* did not work (for some reason) */
  413. kref_put(&info->refcount, batadv_free_info);
  414. info = NULL;
  415. }
  416. return info;
  417. }
  418. /* handle the server sync packet, forward if needed. */
  419. void batadv_receive_server_sync_packet(struct bat_priv *bat_priv,
  420. struct vis_packet *vis_packet,
  421. int vis_info_len)
  422. {
  423. struct vis_info *info;
  424. int is_new, make_broadcast;
  425. int vis_server = atomic_read(&bat_priv->vis_mode);
  426. make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
  427. spin_lock_bh(&bat_priv->vis_hash_lock);
  428. info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
  429. &is_new, make_broadcast);
  430. if (!info)
  431. goto end;
  432. /* only if we are server ourselves and packet is newer than the one in
  433. * hash.
  434. */
  435. if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
  436. batadv_send_list_add(bat_priv, info);
  437. end:
  438. spin_unlock_bh(&bat_priv->vis_hash_lock);
  439. }
  440. /* handle an incoming client update packet and schedule forward if needed. */
  441. void batadv_receive_client_update_packet(struct bat_priv *bat_priv,
  442. struct vis_packet *vis_packet,
  443. int vis_info_len)
  444. {
  445. struct vis_info *info;
  446. struct vis_packet *packet;
  447. int is_new;
  448. int vis_server = atomic_read(&bat_priv->vis_mode);
  449. int are_target = 0;
  450. /* clients shall not broadcast. */
  451. if (is_broadcast_ether_addr(vis_packet->target_orig))
  452. return;
  453. /* Are we the target for this VIS packet? */
  454. if (vis_server == VIS_TYPE_SERVER_SYNC &&
  455. batadv_is_my_mac(vis_packet->target_orig))
  456. are_target = 1;
  457. spin_lock_bh(&bat_priv->vis_hash_lock);
  458. info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
  459. &is_new, are_target);
  460. if (!info)
  461. goto end;
  462. /* note that outdated packets will be dropped at this point. */
  463. packet = (struct vis_packet *)info->skb_packet->data;
  464. /* send only if we're the target server or ... */
  465. if (are_target && is_new) {
  466. packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
  467. batadv_send_list_add(bat_priv, info);
  468. /* ... we're not the recipient (and thus need to forward). */
  469. } else if (!batadv_is_my_mac(packet->target_orig)) {
  470. batadv_send_list_add(bat_priv, info);
  471. }
  472. end:
  473. spin_unlock_bh(&bat_priv->vis_hash_lock);
  474. }
  475. /* Walk the originators and find the VIS server with the best tq. Set the packet
  476. * address to its address and return the best_tq.
  477. *
  478. * Must be called with the originator hash locked
  479. */
  480. static int batadv_find_best_vis_server(struct bat_priv *bat_priv,
  481. struct vis_info *info)
  482. {
  483. struct hashtable_t *hash = bat_priv->orig_hash;
  484. struct neigh_node *router;
  485. struct hlist_node *node;
  486. struct hlist_head *head;
  487. struct orig_node *orig_node;
  488. struct vis_packet *packet;
  489. int best_tq = -1;
  490. uint32_t i;
  491. packet = (struct vis_packet *)info->skb_packet->data;
  492. for (i = 0; i < hash->size; i++) {
  493. head = &hash->table[i];
  494. rcu_read_lock();
  495. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  496. router = batadv_orig_node_get_router(orig_node);
  497. if (!router)
  498. continue;
  499. if ((orig_node->flags & VIS_SERVER) &&
  500. (router->tq_avg > best_tq)) {
  501. best_tq = router->tq_avg;
  502. memcpy(packet->target_orig, orig_node->orig,
  503. ETH_ALEN);
  504. }
  505. batadv_neigh_node_free_ref(router);
  506. }
  507. rcu_read_unlock();
  508. }
  509. return best_tq;
  510. }
  511. /* Return true if the vis packet is full. */
  512. static bool batadv_vis_packet_full(const struct vis_info *info)
  513. {
  514. const struct vis_packet *packet;
  515. packet = (struct vis_packet *)info->skb_packet->data;
  516. if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
  517. < packet->entries + 1)
  518. return true;
  519. return false;
  520. }
  521. /* generates a packet of own vis data,
  522. * returns 0 on success, -1 if no packet could be generated
  523. */
  524. static int batadv_generate_vis_packet(struct bat_priv *bat_priv)
  525. {
  526. struct hashtable_t *hash = bat_priv->orig_hash;
  527. struct hlist_node *node;
  528. struct hlist_head *head;
  529. struct orig_node *orig_node;
  530. struct neigh_node *router;
  531. struct vis_info *info = bat_priv->my_vis_info;
  532. struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
  533. struct vis_info_entry *entry;
  534. struct tt_common_entry *tt_common_entry;
  535. int best_tq = -1;
  536. uint32_t i;
  537. info->first_seen = jiffies;
  538. packet->vis_type = atomic_read(&bat_priv->vis_mode);
  539. memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
  540. packet->header.ttl = TTL;
  541. packet->seqno = htonl(ntohl(packet->seqno) + 1);
  542. packet->entries = 0;
  543. skb_trim(info->skb_packet, sizeof(*packet));
  544. if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
  545. best_tq = batadv_find_best_vis_server(bat_priv, info);
  546. if (best_tq < 0)
  547. return best_tq;
  548. }
  549. for (i = 0; i < hash->size; i++) {
  550. head = &hash->table[i];
  551. rcu_read_lock();
  552. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  553. router = batadv_orig_node_get_router(orig_node);
  554. if (!router)
  555. continue;
  556. if (!batadv_compare_eth(router->addr, orig_node->orig))
  557. goto next;
  558. if (router->if_incoming->if_status != IF_ACTIVE)
  559. goto next;
  560. if (router->tq_avg < 1)
  561. goto next;
  562. /* fill one entry into buffer. */
  563. entry = (struct vis_info_entry *)
  564. skb_put(info->skb_packet, sizeof(*entry));
  565. memcpy(entry->src,
  566. router->if_incoming->net_dev->dev_addr,
  567. ETH_ALEN);
  568. memcpy(entry->dest, orig_node->orig, ETH_ALEN);
  569. entry->quality = router->tq_avg;
  570. packet->entries++;
  571. next:
  572. batadv_neigh_node_free_ref(router);
  573. if (batadv_vis_packet_full(info))
  574. goto unlock;
  575. }
  576. rcu_read_unlock();
  577. }
  578. hash = bat_priv->tt_local_hash;
  579. for (i = 0; i < hash->size; i++) {
  580. head = &hash->table[i];
  581. rcu_read_lock();
  582. hlist_for_each_entry_rcu(tt_common_entry, node, head,
  583. hash_entry) {
  584. entry = (struct vis_info_entry *)
  585. skb_put(info->skb_packet,
  586. sizeof(*entry));
  587. memset(entry->src, 0, ETH_ALEN);
  588. memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
  589. entry->quality = 0; /* 0 means TT */
  590. packet->entries++;
  591. if (batadv_vis_packet_full(info))
  592. goto unlock;
  593. }
  594. rcu_read_unlock();
  595. }
  596. return 0;
  597. unlock:
  598. rcu_read_unlock();
  599. return 0;
  600. }
  601. /* free old vis packets. Must be called with this vis_hash_lock
  602. * held
  603. */
  604. static void batadv_purge_vis_packets(struct bat_priv *bat_priv)
  605. {
  606. uint32_t i;
  607. struct hashtable_t *hash = bat_priv->vis_hash;
  608. struct hlist_node *node, *node_tmp;
  609. struct hlist_head *head;
  610. struct vis_info *info;
  611. for (i = 0; i < hash->size; i++) {
  612. head = &hash->table[i];
  613. hlist_for_each_entry_safe(info, node, node_tmp,
  614. head, hash_entry) {
  615. /* never purge own data. */
  616. if (info == bat_priv->my_vis_info)
  617. continue;
  618. if (batadv_has_timed_out(info->first_seen,
  619. VIS_TIMEOUT)) {
  620. hlist_del(node);
  621. batadv_send_list_del(info);
  622. kref_put(&info->refcount, batadv_free_info);
  623. }
  624. }
  625. }
  626. }
  627. static void batadv_broadcast_vis_packet(struct bat_priv *bat_priv,
  628. struct vis_info *info)
  629. {
  630. struct neigh_node *router;
  631. struct hashtable_t *hash = bat_priv->orig_hash;
  632. struct hlist_node *node;
  633. struct hlist_head *head;
  634. struct orig_node *orig_node;
  635. struct vis_packet *packet;
  636. struct sk_buff *skb;
  637. struct hard_iface *hard_iface;
  638. uint8_t dstaddr[ETH_ALEN];
  639. uint32_t i;
  640. packet = (struct vis_packet *)info->skb_packet->data;
  641. /* send to all routers in range. */
  642. for (i = 0; i < hash->size; i++) {
  643. head = &hash->table[i];
  644. rcu_read_lock();
  645. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  646. /* if it's a vis server and reachable, send it. */
  647. if (!(orig_node->flags & VIS_SERVER))
  648. continue;
  649. router = batadv_orig_node_get_router(orig_node);
  650. if (!router)
  651. continue;
  652. /* don't send it if we already received the packet from
  653. * this node.
  654. */
  655. if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
  656. orig_node->orig)) {
  657. batadv_neigh_node_free_ref(router);
  658. continue;
  659. }
  660. memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
  661. hard_iface = router->if_incoming;
  662. memcpy(dstaddr, router->addr, ETH_ALEN);
  663. batadv_neigh_node_free_ref(router);
  664. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  665. if (skb)
  666. batadv_send_skb_packet(skb, hard_iface,
  667. dstaddr);
  668. }
  669. rcu_read_unlock();
  670. }
  671. }
  672. static void batadv_unicast_vis_packet(struct bat_priv *bat_priv,
  673. struct vis_info *info)
  674. {
  675. struct orig_node *orig_node;
  676. struct neigh_node *router = NULL;
  677. struct sk_buff *skb;
  678. struct vis_packet *packet;
  679. packet = (struct vis_packet *)info->skb_packet->data;
  680. orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
  681. if (!orig_node)
  682. goto out;
  683. router = batadv_orig_node_get_router(orig_node);
  684. if (!router)
  685. goto out;
  686. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  687. if (skb)
  688. batadv_send_skb_packet(skb, router->if_incoming, router->addr);
  689. out:
  690. if (router)
  691. batadv_neigh_node_free_ref(router);
  692. if (orig_node)
  693. batadv_orig_node_free_ref(orig_node);
  694. }
  695. /* only send one vis packet. called from batadv_send_vis_packets() */
  696. static void batadv_send_vis_packet(struct bat_priv *bat_priv,
  697. struct vis_info *info)
  698. {
  699. struct hard_iface *primary_if;
  700. struct vis_packet *packet;
  701. primary_if = batadv_primary_if_get_selected(bat_priv);
  702. if (!primary_if)
  703. goto out;
  704. packet = (struct vis_packet *)info->skb_packet->data;
  705. if (packet->header.ttl < 2) {
  706. pr_debug("Error - can't send vis packet: ttl exceeded\n");
  707. goto out;
  708. }
  709. memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  710. packet->header.ttl--;
  711. if (is_broadcast_ether_addr(packet->target_orig))
  712. batadv_broadcast_vis_packet(bat_priv, info);
  713. else
  714. batadv_unicast_vis_packet(bat_priv, info);
  715. packet->header.ttl++; /* restore TTL */
  716. out:
  717. if (primary_if)
  718. batadv_hardif_free_ref(primary_if);
  719. }
  720. /* called from timer; send (and maybe generate) vis packet. */
  721. static void batadv_send_vis_packets(struct work_struct *work)
  722. {
  723. struct delayed_work *delayed_work =
  724. container_of(work, struct delayed_work, work);
  725. struct bat_priv *bat_priv =
  726. container_of(delayed_work, struct bat_priv, vis_work);
  727. struct vis_info *info;
  728. spin_lock_bh(&bat_priv->vis_hash_lock);
  729. batadv_purge_vis_packets(bat_priv);
  730. if (batadv_generate_vis_packet(bat_priv) == 0) {
  731. /* schedule if generation was successful */
  732. batadv_send_list_add(bat_priv, bat_priv->my_vis_info);
  733. }
  734. while (!list_empty(&bat_priv->vis_send_list)) {
  735. info = list_first_entry(&bat_priv->vis_send_list,
  736. typeof(*info), send_list);
  737. kref_get(&info->refcount);
  738. spin_unlock_bh(&bat_priv->vis_hash_lock);
  739. batadv_send_vis_packet(bat_priv, info);
  740. spin_lock_bh(&bat_priv->vis_hash_lock);
  741. batadv_send_list_del(info);
  742. kref_put(&info->refcount, batadv_free_info);
  743. }
  744. spin_unlock_bh(&bat_priv->vis_hash_lock);
  745. batadv_start_vis_timer(bat_priv);
  746. }
  747. /* init the vis server. this may only be called when if_list is already
  748. * initialized (e.g. bat0 is initialized, interfaces have been added)
  749. */
  750. int batadv_vis_init(struct bat_priv *bat_priv)
  751. {
  752. struct vis_packet *packet;
  753. int hash_added;
  754. if (bat_priv->vis_hash)
  755. return 0;
  756. spin_lock_bh(&bat_priv->vis_hash_lock);
  757. bat_priv->vis_hash = batadv_hash_new(256);
  758. if (!bat_priv->vis_hash) {
  759. pr_err("Can't initialize vis_hash\n");
  760. goto err;
  761. }
  762. bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
  763. if (!bat_priv->my_vis_info)
  764. goto err;
  765. bat_priv->my_vis_info->skb_packet = dev_alloc_skb(sizeof(*packet) +
  766. MAX_VIS_PACKET_SIZE +
  767. ETH_HLEN);
  768. if (!bat_priv->my_vis_info->skb_packet)
  769. goto free_info;
  770. skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
  771. packet = (struct vis_packet *)skb_put(bat_priv->my_vis_info->skb_packet,
  772. sizeof(*packet));
  773. /* prefill the vis info */
  774. bat_priv->my_vis_info->first_seen = jiffies -
  775. msecs_to_jiffies(VIS_INTERVAL);
  776. INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
  777. INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
  778. kref_init(&bat_priv->my_vis_info->refcount);
  779. bat_priv->my_vis_info->bat_priv = bat_priv;
  780. packet->header.version = COMPAT_VERSION;
  781. packet->header.packet_type = BAT_VIS;
  782. packet->header.ttl = TTL;
  783. packet->seqno = 0;
  784. packet->entries = 0;
  785. INIT_LIST_HEAD(&bat_priv->vis_send_list);
  786. hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
  787. batadv_vis_info_choose,
  788. bat_priv->my_vis_info,
  789. &bat_priv->my_vis_info->hash_entry);
  790. if (hash_added != 0) {
  791. pr_err("Can't add own vis packet into hash\n");
  792. /* not in hash, need to remove it manually. */
  793. kref_put(&bat_priv->my_vis_info->refcount, batadv_free_info);
  794. goto err;
  795. }
  796. spin_unlock_bh(&bat_priv->vis_hash_lock);
  797. batadv_start_vis_timer(bat_priv);
  798. return 0;
  799. free_info:
  800. kfree(bat_priv->my_vis_info);
  801. bat_priv->my_vis_info = NULL;
  802. err:
  803. spin_unlock_bh(&bat_priv->vis_hash_lock);
  804. batadv_vis_quit(bat_priv);
  805. return -ENOMEM;
  806. }
  807. /* Decrease the reference count on a hash item info */
  808. static void batadv_free_info_ref(struct hlist_node *node, void *arg)
  809. {
  810. struct vis_info *info;
  811. info = container_of(node, struct vis_info, hash_entry);
  812. batadv_send_list_del(info);
  813. kref_put(&info->refcount, batadv_free_info);
  814. }
  815. /* shutdown vis-server */
  816. void batadv_vis_quit(struct bat_priv *bat_priv)
  817. {
  818. if (!bat_priv->vis_hash)
  819. return;
  820. cancel_delayed_work_sync(&bat_priv->vis_work);
  821. spin_lock_bh(&bat_priv->vis_hash_lock);
  822. /* properly remove, kill timers ... */
  823. batadv_hash_delete(bat_priv->vis_hash, batadv_free_info_ref, NULL);
  824. bat_priv->vis_hash = NULL;
  825. bat_priv->my_vis_info = NULL;
  826. spin_unlock_bh(&bat_priv->vis_hash_lock);
  827. }
  828. /* schedule packets for (re)transmission */
  829. static void batadv_start_vis_timer(struct bat_priv *bat_priv)
  830. {
  831. INIT_DELAYED_WORK(&bat_priv->vis_work, batadv_send_vis_packets);
  832. queue_delayed_work(batadv_event_workqueue, &bat_priv->vis_work,
  833. msecs_to_jiffies(VIS_INTERVAL));
  834. }