vis.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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 BATADV_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 void batadv_vis_data_read_prim_sec(struct seq_file *seq,
  122. const struct hlist_head *if_list)
  123. {
  124. struct if_list_entry *entry;
  125. struct hlist_node *pos;
  126. hlist_for_each_entry(entry, pos, if_list, list) {
  127. if (entry->primary)
  128. seq_printf(seq, "PRIMARY, ");
  129. else
  130. seq_printf(seq, "SEC %pM, ", entry->addr);
  131. }
  132. }
  133. /* read an entry */
  134. static ssize_t batadv_vis_data_read_entry(struct seq_file *seq,
  135. const struct vis_info_entry *entry,
  136. const uint8_t *src, bool primary)
  137. {
  138. if (primary && entry->quality == 0)
  139. return seq_printf(seq, "TT %pM, ", entry->dest);
  140. else if (batadv_compare_eth(entry->src, src))
  141. return seq_printf(seq, "TQ %pM %d, ", entry->dest,
  142. entry->quality);
  143. return 0;
  144. }
  145. static void batadv_vis_data_insert_interfaces(struct hlist_head *list,
  146. struct vis_packet *packet,
  147. struct vis_info_entry *entries)
  148. {
  149. int i;
  150. for (i = 0; i < packet->entries; i++) {
  151. if (entries[i].quality == 0)
  152. continue;
  153. if (batadv_compare_eth(entries[i].src, packet->vis_orig))
  154. continue;
  155. batadv_vis_data_insert_interface(entries[i].src, list, false);
  156. }
  157. }
  158. static void batadv_vis_data_read_entries(struct seq_file *seq,
  159. struct hlist_head *list,
  160. struct vis_packet *packet,
  161. struct vis_info_entry *entries)
  162. {
  163. int i;
  164. struct if_list_entry *entry;
  165. struct hlist_node *pos;
  166. hlist_for_each_entry(entry, pos, list, list) {
  167. seq_printf(seq, "%pM,", entry->addr);
  168. for (i = 0; i < packet->entries; i++)
  169. batadv_vis_data_read_entry(seq, &entries[i],
  170. entry->addr, entry->primary);
  171. /* add primary/secondary records */
  172. if (batadv_compare_eth(entry->addr, packet->vis_orig))
  173. batadv_vis_data_read_prim_sec(seq, list);
  174. seq_printf(seq, "\n");
  175. }
  176. }
  177. static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
  178. const struct hlist_head *head)
  179. {
  180. struct hlist_node *node;
  181. struct vis_info *info;
  182. struct vis_packet *packet;
  183. uint8_t *entries_pos;
  184. struct vis_info_entry *entries;
  185. struct if_list_entry *entry;
  186. struct hlist_node *pos, *n;
  187. HLIST_HEAD(vis_if_list);
  188. hlist_for_each_entry_rcu(info, node, head, hash_entry) {
  189. packet = (struct vis_packet *)info->skb_packet->data;
  190. entries_pos = (uint8_t *)packet + sizeof(*packet);
  191. entries = (struct vis_info_entry *)entries_pos;
  192. batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
  193. true);
  194. batadv_vis_data_insert_interfaces(&vis_if_list, packet,
  195. entries);
  196. batadv_vis_data_read_entries(seq, &vis_if_list, packet,
  197. entries);
  198. hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
  199. hlist_del(&entry->list);
  200. kfree(entry);
  201. }
  202. }
  203. }
  204. int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
  205. {
  206. struct hard_iface *primary_if;
  207. struct hlist_head *head;
  208. struct net_device *net_dev = (struct net_device *)seq->private;
  209. struct bat_priv *bat_priv = netdev_priv(net_dev);
  210. struct hashtable_t *hash = bat_priv->vis_hash;
  211. uint32_t i;
  212. int ret = 0;
  213. int vis_server = atomic_read(&bat_priv->vis_mode);
  214. primary_if = batadv_primary_if_get_selected(bat_priv);
  215. if (!primary_if)
  216. goto out;
  217. if (vis_server == VIS_TYPE_CLIENT_UPDATE)
  218. goto out;
  219. spin_lock_bh(&bat_priv->vis_hash_lock);
  220. for (i = 0; i < hash->size; i++) {
  221. head = &hash->table[i];
  222. batadv_vis_seq_print_text_bucket(seq, head);
  223. }
  224. spin_unlock_bh(&bat_priv->vis_hash_lock);
  225. out:
  226. if (primary_if)
  227. batadv_hardif_free_ref(primary_if);
  228. return ret;
  229. }
  230. /* add the info packet to the send list, if it was not
  231. * already linked in.
  232. */
  233. static void batadv_send_list_add(struct bat_priv *bat_priv,
  234. struct vis_info *info)
  235. {
  236. if (list_empty(&info->send_list)) {
  237. kref_get(&info->refcount);
  238. list_add_tail(&info->send_list, &bat_priv->vis_send_list);
  239. }
  240. }
  241. /* delete the info packet from the send list, if it was
  242. * linked in.
  243. */
  244. static void batadv_send_list_del(struct vis_info *info)
  245. {
  246. if (!list_empty(&info->send_list)) {
  247. list_del_init(&info->send_list);
  248. kref_put(&info->refcount, batadv_free_info);
  249. }
  250. }
  251. /* tries to add one entry to the receive list. */
  252. static void batadv_recv_list_add(struct bat_priv *bat_priv,
  253. struct list_head *recv_list, const char *mac)
  254. {
  255. struct recvlist_node *entry;
  256. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  257. if (!entry)
  258. return;
  259. memcpy(entry->mac, mac, ETH_ALEN);
  260. spin_lock_bh(&bat_priv->vis_list_lock);
  261. list_add_tail(&entry->list, recv_list);
  262. spin_unlock_bh(&bat_priv->vis_list_lock);
  263. }
  264. /* returns 1 if this mac is in the recv_list */
  265. static int batadv_recv_list_is_in(struct bat_priv *bat_priv,
  266. const struct list_head *recv_list,
  267. const char *mac)
  268. {
  269. const struct recvlist_node *entry;
  270. spin_lock_bh(&bat_priv->vis_list_lock);
  271. list_for_each_entry(entry, recv_list, list) {
  272. if (batadv_compare_eth(entry->mac, mac)) {
  273. spin_unlock_bh(&bat_priv->vis_list_lock);
  274. return 1;
  275. }
  276. }
  277. spin_unlock_bh(&bat_priv->vis_list_lock);
  278. return 0;
  279. }
  280. /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
  281. * broken.. ). vis hash must be locked outside. is_new is set when the packet
  282. * is newer than old entries in the hash.
  283. */
  284. static struct vis_info *batadv_add_packet(struct bat_priv *bat_priv,
  285. struct vis_packet *vis_packet,
  286. int vis_info_len, int *is_new,
  287. int make_broadcast)
  288. {
  289. struct vis_info *info, *old_info;
  290. struct vis_packet *search_packet, *old_packet;
  291. struct vis_info search_elem;
  292. struct vis_packet *packet;
  293. int hash_added;
  294. *is_new = 0;
  295. /* sanity check */
  296. if (!bat_priv->vis_hash)
  297. return NULL;
  298. /* see if the packet is already in vis_hash */
  299. search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
  300. if (!search_elem.skb_packet)
  301. return NULL;
  302. search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
  303. sizeof(*search_packet));
  304. memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
  305. old_info = batadv_vis_hash_find(bat_priv, &search_elem);
  306. kfree_skb(search_elem.skb_packet);
  307. if (old_info) {
  308. old_packet = (struct vis_packet *)old_info->skb_packet->data;
  309. if (!batadv_seq_after(ntohl(vis_packet->seqno),
  310. ntohl(old_packet->seqno))) {
  311. if (old_packet->seqno == vis_packet->seqno) {
  312. batadv_recv_list_add(bat_priv,
  313. &old_info->recv_list,
  314. vis_packet->sender_orig);
  315. return old_info;
  316. } else {
  317. /* newer packet is already in hash. */
  318. return NULL;
  319. }
  320. }
  321. /* remove old entry */
  322. batadv_hash_remove(bat_priv->vis_hash, batadv_vis_info_cmp,
  323. batadv_vis_info_choose, old_info);
  324. batadv_send_list_del(old_info);
  325. kref_put(&old_info->refcount, batadv_free_info);
  326. }
  327. info = kmalloc(sizeof(*info), GFP_ATOMIC);
  328. if (!info)
  329. return NULL;
  330. info->skb_packet = dev_alloc_skb(sizeof(*packet) + vis_info_len +
  331. ETH_HLEN);
  332. if (!info->skb_packet) {
  333. kfree(info);
  334. return NULL;
  335. }
  336. skb_reserve(info->skb_packet, ETH_HLEN);
  337. packet = (struct vis_packet *)skb_put(info->skb_packet, sizeof(*packet)
  338. + vis_info_len);
  339. kref_init(&info->refcount);
  340. INIT_LIST_HEAD(&info->send_list);
  341. INIT_LIST_HEAD(&info->recv_list);
  342. info->first_seen = jiffies;
  343. info->bat_priv = bat_priv;
  344. memcpy(packet, vis_packet, sizeof(*packet) + vis_info_len);
  345. /* initialize and add new packet. */
  346. *is_new = 1;
  347. /* Make it a broadcast packet, if required */
  348. if (make_broadcast)
  349. memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
  350. /* repair if entries is longer than packet. */
  351. if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
  352. packet->entries = vis_info_len / sizeof(struct vis_info_entry);
  353. batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
  354. /* try to add it */
  355. hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
  356. batadv_vis_info_choose, info,
  357. &info->hash_entry);
  358. if (hash_added != 0) {
  359. /* did not work (for some reason) */
  360. kref_put(&info->refcount, batadv_free_info);
  361. info = NULL;
  362. }
  363. return info;
  364. }
  365. /* handle the server sync packet, forward if needed. */
  366. void batadv_receive_server_sync_packet(struct bat_priv *bat_priv,
  367. struct vis_packet *vis_packet,
  368. int vis_info_len)
  369. {
  370. struct vis_info *info;
  371. int is_new, make_broadcast;
  372. int vis_server = atomic_read(&bat_priv->vis_mode);
  373. make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
  374. spin_lock_bh(&bat_priv->vis_hash_lock);
  375. info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
  376. &is_new, make_broadcast);
  377. if (!info)
  378. goto end;
  379. /* only if we are server ourselves and packet is newer than the one in
  380. * hash.
  381. */
  382. if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
  383. batadv_send_list_add(bat_priv, info);
  384. end:
  385. spin_unlock_bh(&bat_priv->vis_hash_lock);
  386. }
  387. /* handle an incoming client update packet and schedule forward if needed. */
  388. void batadv_receive_client_update_packet(struct bat_priv *bat_priv,
  389. struct vis_packet *vis_packet,
  390. int vis_info_len)
  391. {
  392. struct vis_info *info;
  393. struct vis_packet *packet;
  394. int is_new;
  395. int vis_server = atomic_read(&bat_priv->vis_mode);
  396. int are_target = 0;
  397. /* clients shall not broadcast. */
  398. if (is_broadcast_ether_addr(vis_packet->target_orig))
  399. return;
  400. /* Are we the target for this VIS packet? */
  401. if (vis_server == VIS_TYPE_SERVER_SYNC &&
  402. batadv_is_my_mac(vis_packet->target_orig))
  403. are_target = 1;
  404. spin_lock_bh(&bat_priv->vis_hash_lock);
  405. info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
  406. &is_new, are_target);
  407. if (!info)
  408. goto end;
  409. /* note that outdated packets will be dropped at this point. */
  410. packet = (struct vis_packet *)info->skb_packet->data;
  411. /* send only if we're the target server or ... */
  412. if (are_target && is_new) {
  413. packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
  414. batadv_send_list_add(bat_priv, info);
  415. /* ... we're not the recipient (and thus need to forward). */
  416. } else if (!batadv_is_my_mac(packet->target_orig)) {
  417. batadv_send_list_add(bat_priv, info);
  418. }
  419. end:
  420. spin_unlock_bh(&bat_priv->vis_hash_lock);
  421. }
  422. /* Walk the originators and find the VIS server with the best tq. Set the packet
  423. * address to its address and return the best_tq.
  424. *
  425. * Must be called with the originator hash locked
  426. */
  427. static int batadv_find_best_vis_server(struct bat_priv *bat_priv,
  428. struct vis_info *info)
  429. {
  430. struct hashtable_t *hash = bat_priv->orig_hash;
  431. struct neigh_node *router;
  432. struct hlist_node *node;
  433. struct hlist_head *head;
  434. struct orig_node *orig_node;
  435. struct vis_packet *packet;
  436. int best_tq = -1;
  437. uint32_t i;
  438. packet = (struct vis_packet *)info->skb_packet->data;
  439. for (i = 0; i < hash->size; i++) {
  440. head = &hash->table[i];
  441. rcu_read_lock();
  442. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  443. router = batadv_orig_node_get_router(orig_node);
  444. if (!router)
  445. continue;
  446. if ((orig_node->flags & VIS_SERVER) &&
  447. (router->tq_avg > best_tq)) {
  448. best_tq = router->tq_avg;
  449. memcpy(packet->target_orig, orig_node->orig,
  450. ETH_ALEN);
  451. }
  452. batadv_neigh_node_free_ref(router);
  453. }
  454. rcu_read_unlock();
  455. }
  456. return best_tq;
  457. }
  458. /* Return true if the vis packet is full. */
  459. static bool batadv_vis_packet_full(const struct vis_info *info)
  460. {
  461. const struct vis_packet *packet;
  462. size_t num_items;
  463. packet = (struct vis_packet *)info->skb_packet->data;
  464. num_items = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry);
  465. if (num_items < packet->entries + 1)
  466. return true;
  467. return false;
  468. }
  469. /* generates a packet of own vis data,
  470. * returns 0 on success, -1 if no packet could be generated
  471. */
  472. static int batadv_generate_vis_packet(struct bat_priv *bat_priv)
  473. {
  474. struct hashtable_t *hash = bat_priv->orig_hash;
  475. struct hlist_node *node;
  476. struct hlist_head *head;
  477. struct orig_node *orig_node;
  478. struct neigh_node *router;
  479. struct vis_info *info = bat_priv->my_vis_info;
  480. struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
  481. struct vis_info_entry *entry;
  482. struct tt_common_entry *tt_common_entry;
  483. int best_tq = -1;
  484. uint32_t i;
  485. info->first_seen = jiffies;
  486. packet->vis_type = atomic_read(&bat_priv->vis_mode);
  487. memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
  488. packet->header.ttl = TTL;
  489. packet->seqno = htonl(ntohl(packet->seqno) + 1);
  490. packet->entries = 0;
  491. skb_trim(info->skb_packet, sizeof(*packet));
  492. if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
  493. best_tq = batadv_find_best_vis_server(bat_priv, info);
  494. if (best_tq < 0)
  495. return best_tq;
  496. }
  497. for (i = 0; i < hash->size; i++) {
  498. head = &hash->table[i];
  499. rcu_read_lock();
  500. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  501. router = batadv_orig_node_get_router(orig_node);
  502. if (!router)
  503. continue;
  504. if (!batadv_compare_eth(router->addr, orig_node->orig))
  505. goto next;
  506. if (router->if_incoming->if_status != IF_ACTIVE)
  507. goto next;
  508. if (router->tq_avg < 1)
  509. goto next;
  510. /* fill one entry into buffer. */
  511. entry = (struct vis_info_entry *)
  512. skb_put(info->skb_packet, sizeof(*entry));
  513. memcpy(entry->src,
  514. router->if_incoming->net_dev->dev_addr,
  515. ETH_ALEN);
  516. memcpy(entry->dest, orig_node->orig, ETH_ALEN);
  517. entry->quality = router->tq_avg;
  518. packet->entries++;
  519. next:
  520. batadv_neigh_node_free_ref(router);
  521. if (batadv_vis_packet_full(info))
  522. goto unlock;
  523. }
  524. rcu_read_unlock();
  525. }
  526. hash = bat_priv->tt_local_hash;
  527. for (i = 0; i < hash->size; i++) {
  528. head = &hash->table[i];
  529. rcu_read_lock();
  530. hlist_for_each_entry_rcu(tt_common_entry, node, head,
  531. hash_entry) {
  532. entry = (struct vis_info_entry *)
  533. skb_put(info->skb_packet,
  534. sizeof(*entry));
  535. memset(entry->src, 0, ETH_ALEN);
  536. memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
  537. entry->quality = 0; /* 0 means TT */
  538. packet->entries++;
  539. if (batadv_vis_packet_full(info))
  540. goto unlock;
  541. }
  542. rcu_read_unlock();
  543. }
  544. return 0;
  545. unlock:
  546. rcu_read_unlock();
  547. return 0;
  548. }
  549. /* free old vis packets. Must be called with this vis_hash_lock
  550. * held
  551. */
  552. static void batadv_purge_vis_packets(struct bat_priv *bat_priv)
  553. {
  554. uint32_t i;
  555. struct hashtable_t *hash = bat_priv->vis_hash;
  556. struct hlist_node *node, *node_tmp;
  557. struct hlist_head *head;
  558. struct vis_info *info;
  559. for (i = 0; i < hash->size; i++) {
  560. head = &hash->table[i];
  561. hlist_for_each_entry_safe(info, node, node_tmp,
  562. head, hash_entry) {
  563. /* never purge own data. */
  564. if (info == bat_priv->my_vis_info)
  565. continue;
  566. if (batadv_has_timed_out(info->first_seen,
  567. BATADV_VIS_TIMEOUT)) {
  568. hlist_del(node);
  569. batadv_send_list_del(info);
  570. kref_put(&info->refcount, batadv_free_info);
  571. }
  572. }
  573. }
  574. }
  575. static void batadv_broadcast_vis_packet(struct bat_priv *bat_priv,
  576. struct vis_info *info)
  577. {
  578. struct neigh_node *router;
  579. struct hashtable_t *hash = bat_priv->orig_hash;
  580. struct hlist_node *node;
  581. struct hlist_head *head;
  582. struct orig_node *orig_node;
  583. struct vis_packet *packet;
  584. struct sk_buff *skb;
  585. struct hard_iface *hard_iface;
  586. uint8_t dstaddr[ETH_ALEN];
  587. uint32_t i;
  588. packet = (struct vis_packet *)info->skb_packet->data;
  589. /* send to all routers in range. */
  590. for (i = 0; i < hash->size; i++) {
  591. head = &hash->table[i];
  592. rcu_read_lock();
  593. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  594. /* if it's a vis server and reachable, send it. */
  595. if (!(orig_node->flags & VIS_SERVER))
  596. continue;
  597. router = batadv_orig_node_get_router(orig_node);
  598. if (!router)
  599. continue;
  600. /* don't send it if we already received the packet from
  601. * this node.
  602. */
  603. if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
  604. orig_node->orig)) {
  605. batadv_neigh_node_free_ref(router);
  606. continue;
  607. }
  608. memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
  609. hard_iface = router->if_incoming;
  610. memcpy(dstaddr, router->addr, ETH_ALEN);
  611. batadv_neigh_node_free_ref(router);
  612. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  613. if (skb)
  614. batadv_send_skb_packet(skb, hard_iface,
  615. dstaddr);
  616. }
  617. rcu_read_unlock();
  618. }
  619. }
  620. static void batadv_unicast_vis_packet(struct bat_priv *bat_priv,
  621. struct vis_info *info)
  622. {
  623. struct orig_node *orig_node;
  624. struct neigh_node *router = NULL;
  625. struct sk_buff *skb;
  626. struct vis_packet *packet;
  627. packet = (struct vis_packet *)info->skb_packet->data;
  628. orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
  629. if (!orig_node)
  630. goto out;
  631. router = batadv_orig_node_get_router(orig_node);
  632. if (!router)
  633. goto out;
  634. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  635. if (skb)
  636. batadv_send_skb_packet(skb, router->if_incoming, router->addr);
  637. out:
  638. if (router)
  639. batadv_neigh_node_free_ref(router);
  640. if (orig_node)
  641. batadv_orig_node_free_ref(orig_node);
  642. }
  643. /* only send one vis packet. called from batadv_send_vis_packets() */
  644. static void batadv_send_vis_packet(struct bat_priv *bat_priv,
  645. struct vis_info *info)
  646. {
  647. struct hard_iface *primary_if;
  648. struct vis_packet *packet;
  649. primary_if = batadv_primary_if_get_selected(bat_priv);
  650. if (!primary_if)
  651. goto out;
  652. packet = (struct vis_packet *)info->skb_packet->data;
  653. if (packet->header.ttl < 2) {
  654. pr_debug("Error - can't send vis packet: ttl exceeded\n");
  655. goto out;
  656. }
  657. memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  658. packet->header.ttl--;
  659. if (is_broadcast_ether_addr(packet->target_orig))
  660. batadv_broadcast_vis_packet(bat_priv, info);
  661. else
  662. batadv_unicast_vis_packet(bat_priv, info);
  663. packet->header.ttl++; /* restore TTL */
  664. out:
  665. if (primary_if)
  666. batadv_hardif_free_ref(primary_if);
  667. }
  668. /* called from timer; send (and maybe generate) vis packet. */
  669. static void batadv_send_vis_packets(struct work_struct *work)
  670. {
  671. struct delayed_work *delayed_work =
  672. container_of(work, struct delayed_work, work);
  673. struct bat_priv *bat_priv =
  674. container_of(delayed_work, struct bat_priv, vis_work);
  675. struct vis_info *info;
  676. spin_lock_bh(&bat_priv->vis_hash_lock);
  677. batadv_purge_vis_packets(bat_priv);
  678. if (batadv_generate_vis_packet(bat_priv) == 0) {
  679. /* schedule if generation was successful */
  680. batadv_send_list_add(bat_priv, bat_priv->my_vis_info);
  681. }
  682. while (!list_empty(&bat_priv->vis_send_list)) {
  683. info = list_first_entry(&bat_priv->vis_send_list,
  684. typeof(*info), send_list);
  685. kref_get(&info->refcount);
  686. spin_unlock_bh(&bat_priv->vis_hash_lock);
  687. batadv_send_vis_packet(bat_priv, info);
  688. spin_lock_bh(&bat_priv->vis_hash_lock);
  689. batadv_send_list_del(info);
  690. kref_put(&info->refcount, batadv_free_info);
  691. }
  692. spin_unlock_bh(&bat_priv->vis_hash_lock);
  693. batadv_start_vis_timer(bat_priv);
  694. }
  695. /* init the vis server. this may only be called when if_list is already
  696. * initialized (e.g. bat0 is initialized, interfaces have been added)
  697. */
  698. int batadv_vis_init(struct bat_priv *bat_priv)
  699. {
  700. struct vis_packet *packet;
  701. int hash_added;
  702. unsigned int len;
  703. if (bat_priv->vis_hash)
  704. return 0;
  705. spin_lock_bh(&bat_priv->vis_hash_lock);
  706. bat_priv->vis_hash = batadv_hash_new(256);
  707. if (!bat_priv->vis_hash) {
  708. pr_err("Can't initialize vis_hash\n");
  709. goto err;
  710. }
  711. bat_priv->my_vis_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
  712. if (!bat_priv->my_vis_info)
  713. goto err;
  714. len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE + ETH_HLEN;
  715. bat_priv->my_vis_info->skb_packet = dev_alloc_skb(len);
  716. if (!bat_priv->my_vis_info->skb_packet)
  717. goto free_info;
  718. skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
  719. packet = (struct vis_packet *)skb_put(bat_priv->my_vis_info->skb_packet,
  720. sizeof(*packet));
  721. /* prefill the vis info */
  722. bat_priv->my_vis_info->first_seen = jiffies -
  723. msecs_to_jiffies(VIS_INTERVAL);
  724. INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
  725. INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
  726. kref_init(&bat_priv->my_vis_info->refcount);
  727. bat_priv->my_vis_info->bat_priv = bat_priv;
  728. packet->header.version = BATADV_COMPAT_VERSION;
  729. packet->header.packet_type = BAT_VIS;
  730. packet->header.ttl = TTL;
  731. packet->seqno = 0;
  732. packet->entries = 0;
  733. INIT_LIST_HEAD(&bat_priv->vis_send_list);
  734. hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
  735. batadv_vis_info_choose,
  736. bat_priv->my_vis_info,
  737. &bat_priv->my_vis_info->hash_entry);
  738. if (hash_added != 0) {
  739. pr_err("Can't add own vis packet into hash\n");
  740. /* not in hash, need to remove it manually. */
  741. kref_put(&bat_priv->my_vis_info->refcount, batadv_free_info);
  742. goto err;
  743. }
  744. spin_unlock_bh(&bat_priv->vis_hash_lock);
  745. batadv_start_vis_timer(bat_priv);
  746. return 0;
  747. free_info:
  748. kfree(bat_priv->my_vis_info);
  749. bat_priv->my_vis_info = NULL;
  750. err:
  751. spin_unlock_bh(&bat_priv->vis_hash_lock);
  752. batadv_vis_quit(bat_priv);
  753. return -ENOMEM;
  754. }
  755. /* Decrease the reference count on a hash item info */
  756. static void batadv_free_info_ref(struct hlist_node *node, void *arg)
  757. {
  758. struct vis_info *info;
  759. info = container_of(node, struct vis_info, hash_entry);
  760. batadv_send_list_del(info);
  761. kref_put(&info->refcount, batadv_free_info);
  762. }
  763. /* shutdown vis-server */
  764. void batadv_vis_quit(struct bat_priv *bat_priv)
  765. {
  766. if (!bat_priv->vis_hash)
  767. return;
  768. cancel_delayed_work_sync(&bat_priv->vis_work);
  769. spin_lock_bh(&bat_priv->vis_hash_lock);
  770. /* properly remove, kill timers ... */
  771. batadv_hash_delete(bat_priv->vis_hash, batadv_free_info_ref, NULL);
  772. bat_priv->vis_hash = NULL;
  773. bat_priv->my_vis_info = NULL;
  774. spin_unlock_bh(&bat_priv->vis_hash_lock);
  775. }
  776. /* schedule packets for (re)transmission */
  777. static void batadv_start_vis_timer(struct bat_priv *bat_priv)
  778. {
  779. INIT_DELAYED_WORK(&bat_priv->vis_work, batadv_send_vis_packets);
  780. queue_delayed_work(batadv_event_workqueue, &bat_priv->vis_work,
  781. msecs_to_jiffies(VIS_INTERVAL));
  782. }