vis.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 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. packet = (struct vis_packet *)info->skb_packet->data;
  463. if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
  464. < packet->entries + 1)
  465. return true;
  466. return false;
  467. }
  468. /* generates a packet of own vis data,
  469. * returns 0 on success, -1 if no packet could be generated
  470. */
  471. static int batadv_generate_vis_packet(struct bat_priv *bat_priv)
  472. {
  473. struct hashtable_t *hash = bat_priv->orig_hash;
  474. struct hlist_node *node;
  475. struct hlist_head *head;
  476. struct orig_node *orig_node;
  477. struct neigh_node *router;
  478. struct vis_info *info = bat_priv->my_vis_info;
  479. struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
  480. struct vis_info_entry *entry;
  481. struct tt_common_entry *tt_common_entry;
  482. int best_tq = -1;
  483. uint32_t i;
  484. info->first_seen = jiffies;
  485. packet->vis_type = atomic_read(&bat_priv->vis_mode);
  486. memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
  487. packet->header.ttl = TTL;
  488. packet->seqno = htonl(ntohl(packet->seqno) + 1);
  489. packet->entries = 0;
  490. skb_trim(info->skb_packet, sizeof(*packet));
  491. if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
  492. best_tq = batadv_find_best_vis_server(bat_priv, info);
  493. if (best_tq < 0)
  494. return best_tq;
  495. }
  496. for (i = 0; i < hash->size; i++) {
  497. head = &hash->table[i];
  498. rcu_read_lock();
  499. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  500. router = batadv_orig_node_get_router(orig_node);
  501. if (!router)
  502. continue;
  503. if (!batadv_compare_eth(router->addr, orig_node->orig))
  504. goto next;
  505. if (router->if_incoming->if_status != IF_ACTIVE)
  506. goto next;
  507. if (router->tq_avg < 1)
  508. goto next;
  509. /* fill one entry into buffer. */
  510. entry = (struct vis_info_entry *)
  511. skb_put(info->skb_packet, sizeof(*entry));
  512. memcpy(entry->src,
  513. router->if_incoming->net_dev->dev_addr,
  514. ETH_ALEN);
  515. memcpy(entry->dest, orig_node->orig, ETH_ALEN);
  516. entry->quality = router->tq_avg;
  517. packet->entries++;
  518. next:
  519. batadv_neigh_node_free_ref(router);
  520. if (batadv_vis_packet_full(info))
  521. goto unlock;
  522. }
  523. rcu_read_unlock();
  524. }
  525. hash = bat_priv->tt_local_hash;
  526. for (i = 0; i < hash->size; i++) {
  527. head = &hash->table[i];
  528. rcu_read_lock();
  529. hlist_for_each_entry_rcu(tt_common_entry, node, head,
  530. hash_entry) {
  531. entry = (struct vis_info_entry *)
  532. skb_put(info->skb_packet,
  533. sizeof(*entry));
  534. memset(entry->src, 0, ETH_ALEN);
  535. memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
  536. entry->quality = 0; /* 0 means TT */
  537. packet->entries++;
  538. if (batadv_vis_packet_full(info))
  539. goto unlock;
  540. }
  541. rcu_read_unlock();
  542. }
  543. return 0;
  544. unlock:
  545. rcu_read_unlock();
  546. return 0;
  547. }
  548. /* free old vis packets. Must be called with this vis_hash_lock
  549. * held
  550. */
  551. static void batadv_purge_vis_packets(struct bat_priv *bat_priv)
  552. {
  553. uint32_t i;
  554. struct hashtable_t *hash = bat_priv->vis_hash;
  555. struct hlist_node *node, *node_tmp;
  556. struct hlist_head *head;
  557. struct vis_info *info;
  558. for (i = 0; i < hash->size; i++) {
  559. head = &hash->table[i];
  560. hlist_for_each_entry_safe(info, node, node_tmp,
  561. head, hash_entry) {
  562. /* never purge own data. */
  563. if (info == bat_priv->my_vis_info)
  564. continue;
  565. if (batadv_has_timed_out(info->first_seen,
  566. VIS_TIMEOUT)) {
  567. hlist_del(node);
  568. batadv_send_list_del(info);
  569. kref_put(&info->refcount, batadv_free_info);
  570. }
  571. }
  572. }
  573. }
  574. static void batadv_broadcast_vis_packet(struct bat_priv *bat_priv,
  575. struct vis_info *info)
  576. {
  577. struct neigh_node *router;
  578. struct hashtable_t *hash = bat_priv->orig_hash;
  579. struct hlist_node *node;
  580. struct hlist_head *head;
  581. struct orig_node *orig_node;
  582. struct vis_packet *packet;
  583. struct sk_buff *skb;
  584. struct hard_iface *hard_iface;
  585. uint8_t dstaddr[ETH_ALEN];
  586. uint32_t i;
  587. packet = (struct vis_packet *)info->skb_packet->data;
  588. /* send to all routers in range. */
  589. for (i = 0; i < hash->size; i++) {
  590. head = &hash->table[i];
  591. rcu_read_lock();
  592. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  593. /* if it's a vis server and reachable, send it. */
  594. if (!(orig_node->flags & VIS_SERVER))
  595. continue;
  596. router = batadv_orig_node_get_router(orig_node);
  597. if (!router)
  598. continue;
  599. /* don't send it if we already received the packet from
  600. * this node.
  601. */
  602. if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
  603. orig_node->orig)) {
  604. batadv_neigh_node_free_ref(router);
  605. continue;
  606. }
  607. memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
  608. hard_iface = router->if_incoming;
  609. memcpy(dstaddr, router->addr, ETH_ALEN);
  610. batadv_neigh_node_free_ref(router);
  611. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  612. if (skb)
  613. batadv_send_skb_packet(skb, hard_iface,
  614. dstaddr);
  615. }
  616. rcu_read_unlock();
  617. }
  618. }
  619. static void batadv_unicast_vis_packet(struct bat_priv *bat_priv,
  620. struct vis_info *info)
  621. {
  622. struct orig_node *orig_node;
  623. struct neigh_node *router = NULL;
  624. struct sk_buff *skb;
  625. struct vis_packet *packet;
  626. packet = (struct vis_packet *)info->skb_packet->data;
  627. orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
  628. if (!orig_node)
  629. goto out;
  630. router = batadv_orig_node_get_router(orig_node);
  631. if (!router)
  632. goto out;
  633. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  634. if (skb)
  635. batadv_send_skb_packet(skb, router->if_incoming, router->addr);
  636. out:
  637. if (router)
  638. batadv_neigh_node_free_ref(router);
  639. if (orig_node)
  640. batadv_orig_node_free_ref(orig_node);
  641. }
  642. /* only send one vis packet. called from batadv_send_vis_packets() */
  643. static void batadv_send_vis_packet(struct bat_priv *bat_priv,
  644. struct vis_info *info)
  645. {
  646. struct hard_iface *primary_if;
  647. struct vis_packet *packet;
  648. primary_if = batadv_primary_if_get_selected(bat_priv);
  649. if (!primary_if)
  650. goto out;
  651. packet = (struct vis_packet *)info->skb_packet->data;
  652. if (packet->header.ttl < 2) {
  653. pr_debug("Error - can't send vis packet: ttl exceeded\n");
  654. goto out;
  655. }
  656. memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  657. packet->header.ttl--;
  658. if (is_broadcast_ether_addr(packet->target_orig))
  659. batadv_broadcast_vis_packet(bat_priv, info);
  660. else
  661. batadv_unicast_vis_packet(bat_priv, info);
  662. packet->header.ttl++; /* restore TTL */
  663. out:
  664. if (primary_if)
  665. batadv_hardif_free_ref(primary_if);
  666. }
  667. /* called from timer; send (and maybe generate) vis packet. */
  668. static void batadv_send_vis_packets(struct work_struct *work)
  669. {
  670. struct delayed_work *delayed_work =
  671. container_of(work, struct delayed_work, work);
  672. struct bat_priv *bat_priv =
  673. container_of(delayed_work, struct bat_priv, vis_work);
  674. struct vis_info *info;
  675. spin_lock_bh(&bat_priv->vis_hash_lock);
  676. batadv_purge_vis_packets(bat_priv);
  677. if (batadv_generate_vis_packet(bat_priv) == 0) {
  678. /* schedule if generation was successful */
  679. batadv_send_list_add(bat_priv, bat_priv->my_vis_info);
  680. }
  681. while (!list_empty(&bat_priv->vis_send_list)) {
  682. info = list_first_entry(&bat_priv->vis_send_list,
  683. typeof(*info), send_list);
  684. kref_get(&info->refcount);
  685. spin_unlock_bh(&bat_priv->vis_hash_lock);
  686. batadv_send_vis_packet(bat_priv, info);
  687. spin_lock_bh(&bat_priv->vis_hash_lock);
  688. batadv_send_list_del(info);
  689. kref_put(&info->refcount, batadv_free_info);
  690. }
  691. spin_unlock_bh(&bat_priv->vis_hash_lock);
  692. batadv_start_vis_timer(bat_priv);
  693. }
  694. /* init the vis server. this may only be called when if_list is already
  695. * initialized (e.g. bat0 is initialized, interfaces have been added)
  696. */
  697. int batadv_vis_init(struct bat_priv *bat_priv)
  698. {
  699. struct vis_packet *packet;
  700. int hash_added;
  701. if (bat_priv->vis_hash)
  702. return 0;
  703. spin_lock_bh(&bat_priv->vis_hash_lock);
  704. bat_priv->vis_hash = batadv_hash_new(256);
  705. if (!bat_priv->vis_hash) {
  706. pr_err("Can't initialize vis_hash\n");
  707. goto err;
  708. }
  709. bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
  710. if (!bat_priv->my_vis_info)
  711. goto err;
  712. bat_priv->my_vis_info->skb_packet = dev_alloc_skb(sizeof(*packet) +
  713. MAX_VIS_PACKET_SIZE +
  714. ETH_HLEN);
  715. if (!bat_priv->my_vis_info->skb_packet)
  716. goto free_info;
  717. skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
  718. packet = (struct vis_packet *)skb_put(bat_priv->my_vis_info->skb_packet,
  719. sizeof(*packet));
  720. /* prefill the vis info */
  721. bat_priv->my_vis_info->first_seen = jiffies -
  722. msecs_to_jiffies(VIS_INTERVAL);
  723. INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
  724. INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
  725. kref_init(&bat_priv->my_vis_info->refcount);
  726. bat_priv->my_vis_info->bat_priv = bat_priv;
  727. packet->header.version = COMPAT_VERSION;
  728. packet->header.packet_type = BAT_VIS;
  729. packet->header.ttl = TTL;
  730. packet->seqno = 0;
  731. packet->entries = 0;
  732. INIT_LIST_HEAD(&bat_priv->vis_send_list);
  733. hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
  734. batadv_vis_info_choose,
  735. bat_priv->my_vis_info,
  736. &bat_priv->my_vis_info->hash_entry);
  737. if (hash_added != 0) {
  738. pr_err("Can't add own vis packet into hash\n");
  739. /* not in hash, need to remove it manually. */
  740. kref_put(&bat_priv->my_vis_info->refcount, batadv_free_info);
  741. goto err;
  742. }
  743. spin_unlock_bh(&bat_priv->vis_hash_lock);
  744. batadv_start_vis_timer(bat_priv);
  745. return 0;
  746. free_info:
  747. kfree(bat_priv->my_vis_info);
  748. bat_priv->my_vis_info = NULL;
  749. err:
  750. spin_unlock_bh(&bat_priv->vis_hash_lock);
  751. batadv_vis_quit(bat_priv);
  752. return -ENOMEM;
  753. }
  754. /* Decrease the reference count on a hash item info */
  755. static void batadv_free_info_ref(struct hlist_node *node, void *arg)
  756. {
  757. struct vis_info *info;
  758. info = container_of(node, struct vis_info, hash_entry);
  759. batadv_send_list_del(info);
  760. kref_put(&info->refcount, batadv_free_info);
  761. }
  762. /* shutdown vis-server */
  763. void batadv_vis_quit(struct bat_priv *bat_priv)
  764. {
  765. if (!bat_priv->vis_hash)
  766. return;
  767. cancel_delayed_work_sync(&bat_priv->vis_work);
  768. spin_lock_bh(&bat_priv->vis_hash_lock);
  769. /* properly remove, kill timers ... */
  770. batadv_hash_delete(bat_priv->vis_hash, batadv_free_info_ref, NULL);
  771. bat_priv->vis_hash = NULL;
  772. bat_priv->my_vis_info = NULL;
  773. spin_unlock_bh(&bat_priv->vis_hash_lock);
  774. }
  775. /* schedule packets for (re)transmission */
  776. static void batadv_start_vis_timer(struct bat_priv *bat_priv)
  777. {
  778. INIT_DELAYED_WORK(&bat_priv->vis_work, batadv_send_vis_packets);
  779. queue_delayed_work(batadv_event_workqueue, &bat_priv->vis_work,
  780. msecs_to_jiffies(VIS_INTERVAL));
  781. }