vis.c 25 KB

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