vis.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "send.h"
  23. #include "translation-table.h"
  24. #include "vis.h"
  25. #include "soft-interface.h"
  26. #include "hard-interface.h"
  27. #include "hash.h"
  28. #include "originator.h"
  29. #define MAX_VIS_PACKET_SIZE 1000
  30. static void start_vis_timer(struct bat_priv *bat_priv);
  31. /* free the info */
  32. static void free_info(struct kref *ref)
  33. {
  34. struct vis_info *info = container_of(ref, struct vis_info, refcount);
  35. struct bat_priv *bat_priv = info->bat_priv;
  36. struct recvlist_node *entry, *tmp;
  37. list_del_init(&info->send_list);
  38. spin_lock_bh(&bat_priv->vis_list_lock);
  39. list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
  40. list_del(&entry->list);
  41. kfree(entry);
  42. }
  43. spin_unlock_bh(&bat_priv->vis_list_lock);
  44. kfree_skb(info->skb_packet);
  45. kfree(info);
  46. }
  47. /* Compare two vis packets, used by the hashing algorithm */
  48. static int vis_info_cmp(const struct hlist_node *node, const void *data2)
  49. {
  50. const struct vis_info *d1, *d2;
  51. const struct vis_packet *p1, *p2;
  52. d1 = container_of(node, struct vis_info, hash_entry);
  53. d2 = data2;
  54. p1 = (struct vis_packet *)d1->skb_packet->data;
  55. p2 = (struct vis_packet *)d2->skb_packet->data;
  56. return compare_eth(p1->vis_orig, p2->vis_orig);
  57. }
  58. /* hash function to choose an entry in a hash table of given size */
  59. /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
  60. static uint32_t vis_info_choose(const void *data, uint32_t size)
  61. {
  62. const struct vis_info *vis_info = data;
  63. const struct vis_packet *packet;
  64. const unsigned char *key;
  65. uint32_t hash = 0;
  66. size_t i;
  67. packet = (struct vis_packet *)vis_info->skb_packet->data;
  68. key = packet->vis_orig;
  69. for (i = 0; i < ETH_ALEN; i++) {
  70. hash += key[i];
  71. hash += (hash << 10);
  72. hash ^= (hash >> 6);
  73. }
  74. hash += (hash << 3);
  75. hash ^= (hash >> 11);
  76. hash += (hash << 15);
  77. return hash % size;
  78. }
  79. static struct vis_info *vis_hash_find(struct bat_priv *bat_priv,
  80. const void *data)
  81. {
  82. struct hashtable_t *hash = bat_priv->vis_hash;
  83. struct hlist_head *head;
  84. struct hlist_node *node;
  85. struct vis_info *vis_info, *vis_info_tmp = NULL;
  86. uint32_t index;
  87. if (!hash)
  88. return NULL;
  89. index = vis_info_choose(data, hash->size);
  90. head = &hash->table[index];
  91. rcu_read_lock();
  92. hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
  93. if (!vis_info_cmp(node, data))
  94. continue;
  95. vis_info_tmp = vis_info;
  96. break;
  97. }
  98. rcu_read_unlock();
  99. return vis_info_tmp;
  100. }
  101. /* insert interface to the list of interfaces of one originator, if it
  102. * does not already exist in the list */
  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 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 = 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. 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. static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info)
  285. {
  286. if (list_empty(&info->send_list)) {
  287. kref_get(&info->refcount);
  288. list_add_tail(&info->send_list, &bat_priv->vis_send_list);
  289. }
  290. }
  291. /* delete the info packet from the send list, if it was
  292. * linked in. */
  293. static void send_list_del(struct vis_info *info)
  294. {
  295. if (!list_empty(&info->send_list)) {
  296. list_del_init(&info->send_list);
  297. kref_put(&info->refcount, free_info);
  298. }
  299. }
  300. /* tries to add one entry to the receive list. */
  301. static void recv_list_add(struct bat_priv *bat_priv,
  302. struct list_head *recv_list, const char *mac)
  303. {
  304. struct recvlist_node *entry;
  305. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  306. if (!entry)
  307. return;
  308. memcpy(entry->mac, mac, ETH_ALEN);
  309. spin_lock_bh(&bat_priv->vis_list_lock);
  310. list_add_tail(&entry->list, recv_list);
  311. spin_unlock_bh(&bat_priv->vis_list_lock);
  312. }
  313. /* returns 1 if this mac is in the recv_list */
  314. static int recv_list_is_in(struct bat_priv *bat_priv,
  315. const struct list_head *recv_list, const char *mac)
  316. {
  317. const struct recvlist_node *entry;
  318. spin_lock_bh(&bat_priv->vis_list_lock);
  319. list_for_each_entry(entry, recv_list, list) {
  320. if (compare_eth(entry->mac, mac)) {
  321. spin_unlock_bh(&bat_priv->vis_list_lock);
  322. return 1;
  323. }
  324. }
  325. spin_unlock_bh(&bat_priv->vis_list_lock);
  326. return 0;
  327. }
  328. /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
  329. * broken.. ). vis hash must be locked outside. is_new is set when the packet
  330. * is newer than old entries in the hash. */
  331. static struct vis_info *add_packet(struct bat_priv *bat_priv,
  332. struct vis_packet *vis_packet,
  333. int vis_info_len, int *is_new,
  334. int make_broadcast)
  335. {
  336. struct vis_info *info, *old_info;
  337. struct vis_packet *search_packet, *old_packet;
  338. struct vis_info search_elem;
  339. struct vis_packet *packet;
  340. int hash_added;
  341. *is_new = 0;
  342. /* sanity check */
  343. if (!bat_priv->vis_hash)
  344. return NULL;
  345. /* see if the packet is already in vis_hash */
  346. search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
  347. if (!search_elem.skb_packet)
  348. return NULL;
  349. search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
  350. sizeof(*search_packet));
  351. memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
  352. old_info = vis_hash_find(bat_priv, &search_elem);
  353. kfree_skb(search_elem.skb_packet);
  354. if (old_info) {
  355. old_packet = (struct vis_packet *)old_info->skb_packet->data;
  356. if (!seq_after(ntohl(vis_packet->seqno),
  357. ntohl(old_packet->seqno))) {
  358. if (old_packet->seqno == vis_packet->seqno) {
  359. recv_list_add(bat_priv, &old_info->recv_list,
  360. vis_packet->sender_orig);
  361. return old_info;
  362. } else {
  363. /* newer packet is already in hash. */
  364. return NULL;
  365. }
  366. }
  367. /* remove old entry */
  368. hash_remove(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
  369. old_info);
  370. send_list_del(old_info);
  371. kref_put(&old_info->refcount, free_info);
  372. }
  373. info = kmalloc(sizeof(*info), GFP_ATOMIC);
  374. if (!info)
  375. return NULL;
  376. info->skb_packet = dev_alloc_skb(sizeof(*packet) + vis_info_len +
  377. ETH_HLEN);
  378. if (!info->skb_packet) {
  379. kfree(info);
  380. return NULL;
  381. }
  382. skb_reserve(info->skb_packet, ETH_HLEN);
  383. packet = (struct vis_packet *)skb_put(info->skb_packet, sizeof(*packet)
  384. + vis_info_len);
  385. kref_init(&info->refcount);
  386. INIT_LIST_HEAD(&info->send_list);
  387. INIT_LIST_HEAD(&info->recv_list);
  388. info->first_seen = jiffies;
  389. info->bat_priv = bat_priv;
  390. memcpy(packet, vis_packet, sizeof(*packet) + vis_info_len);
  391. /* initialize and add new packet. */
  392. *is_new = 1;
  393. /* Make it a broadcast packet, if required */
  394. if (make_broadcast)
  395. memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
  396. /* repair if entries is longer than packet. */
  397. if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
  398. packet->entries = vis_info_len / sizeof(struct vis_info_entry);
  399. recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
  400. /* try to add it */
  401. hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
  402. info, &info->hash_entry);
  403. if (hash_added != 0) {
  404. /* did not work (for some reason) */
  405. kref_put(&info->refcount, free_info);
  406. info = NULL;
  407. }
  408. return info;
  409. }
  410. /* handle the server sync packet, forward if needed. */
  411. void receive_server_sync_packet(struct bat_priv *bat_priv,
  412. struct vis_packet *vis_packet,
  413. int vis_info_len)
  414. {
  415. struct vis_info *info;
  416. int is_new, make_broadcast;
  417. int vis_server = atomic_read(&bat_priv->vis_mode);
  418. make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
  419. spin_lock_bh(&bat_priv->vis_hash_lock);
  420. info = add_packet(bat_priv, vis_packet, vis_info_len,
  421. &is_new, make_broadcast);
  422. if (!info)
  423. goto end;
  424. /* only if we are server ourselves and packet is newer than the one in
  425. * hash.*/
  426. if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
  427. send_list_add(bat_priv, info);
  428. end:
  429. spin_unlock_bh(&bat_priv->vis_hash_lock);
  430. }
  431. /* handle an incoming client update packet and schedule forward if needed. */
  432. void receive_client_update_packet(struct bat_priv *bat_priv,
  433. struct vis_packet *vis_packet,
  434. int vis_info_len)
  435. {
  436. struct vis_info *info;
  437. struct vis_packet *packet;
  438. int is_new;
  439. int vis_server = atomic_read(&bat_priv->vis_mode);
  440. int are_target = 0;
  441. /* clients shall not broadcast. */
  442. if (is_broadcast_ether_addr(vis_packet->target_orig))
  443. return;
  444. /* Are we the target for this VIS packet? */
  445. if (vis_server == VIS_TYPE_SERVER_SYNC &&
  446. is_my_mac(vis_packet->target_orig))
  447. are_target = 1;
  448. spin_lock_bh(&bat_priv->vis_hash_lock);
  449. info = add_packet(bat_priv, vis_packet, vis_info_len,
  450. &is_new, are_target);
  451. if (!info)
  452. goto end;
  453. /* note that outdated packets will be dropped at this point. */
  454. packet = (struct vis_packet *)info->skb_packet->data;
  455. /* send only if we're the target server or ... */
  456. if (are_target && is_new) {
  457. packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
  458. send_list_add(bat_priv, info);
  459. /* ... we're not the recipient (and thus need to forward). */
  460. } else if (!is_my_mac(packet->target_orig)) {
  461. send_list_add(bat_priv, info);
  462. }
  463. end:
  464. spin_unlock_bh(&bat_priv->vis_hash_lock);
  465. }
  466. /* Walk the originators and find the VIS server with the best tq. Set the packet
  467. * address to its address and return the best_tq.
  468. *
  469. * Must be called with the originator hash locked */
  470. static int find_best_vis_server(struct bat_priv *bat_priv,
  471. struct vis_info *info)
  472. {
  473. struct hashtable_t *hash = bat_priv->orig_hash;
  474. struct neigh_node *router;
  475. struct hlist_node *node;
  476. struct hlist_head *head;
  477. struct orig_node *orig_node;
  478. struct vis_packet *packet;
  479. int best_tq = -1;
  480. uint32_t i;
  481. packet = (struct vis_packet *)info->skb_packet->data;
  482. for (i = 0; i < hash->size; i++) {
  483. head = &hash->table[i];
  484. rcu_read_lock();
  485. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  486. router = batadv_orig_node_get_router(orig_node);
  487. if (!router)
  488. continue;
  489. if ((orig_node->flags & VIS_SERVER) &&
  490. (router->tq_avg > best_tq)) {
  491. best_tq = router->tq_avg;
  492. memcpy(packet->target_orig, orig_node->orig,
  493. ETH_ALEN);
  494. }
  495. batadv_neigh_node_free_ref(router);
  496. }
  497. rcu_read_unlock();
  498. }
  499. return best_tq;
  500. }
  501. /* Return true if the vis packet is full. */
  502. static bool vis_packet_full(const struct vis_info *info)
  503. {
  504. const struct vis_packet *packet;
  505. packet = (struct vis_packet *)info->skb_packet->data;
  506. if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
  507. < packet->entries + 1)
  508. return true;
  509. return false;
  510. }
  511. /* generates a packet of own vis data,
  512. * returns 0 on success, -1 if no packet could be generated */
  513. static int generate_vis_packet(struct bat_priv *bat_priv)
  514. {
  515. struct hashtable_t *hash = bat_priv->orig_hash;
  516. struct hlist_node *node;
  517. struct hlist_head *head;
  518. struct orig_node *orig_node;
  519. struct neigh_node *router;
  520. struct vis_info *info = bat_priv->my_vis_info;
  521. struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
  522. struct vis_info_entry *entry;
  523. struct tt_common_entry *tt_common_entry;
  524. int best_tq = -1;
  525. uint32_t i;
  526. info->first_seen = jiffies;
  527. packet->vis_type = atomic_read(&bat_priv->vis_mode);
  528. memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
  529. packet->header.ttl = TTL;
  530. packet->seqno = htonl(ntohl(packet->seqno) + 1);
  531. packet->entries = 0;
  532. skb_trim(info->skb_packet, sizeof(*packet));
  533. if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
  534. best_tq = find_best_vis_server(bat_priv, info);
  535. if (best_tq < 0)
  536. return best_tq;
  537. }
  538. for (i = 0; i < hash->size; i++) {
  539. head = &hash->table[i];
  540. rcu_read_lock();
  541. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  542. router = batadv_orig_node_get_router(orig_node);
  543. if (!router)
  544. continue;
  545. if (!compare_eth(router->addr, orig_node->orig))
  546. goto next;
  547. if (router->if_incoming->if_status != IF_ACTIVE)
  548. goto next;
  549. if (router->tq_avg < 1)
  550. goto next;
  551. /* fill one entry into buffer. */
  552. entry = (struct vis_info_entry *)
  553. skb_put(info->skb_packet, sizeof(*entry));
  554. memcpy(entry->src,
  555. router->if_incoming->net_dev->dev_addr,
  556. ETH_ALEN);
  557. memcpy(entry->dest, orig_node->orig, ETH_ALEN);
  558. entry->quality = router->tq_avg;
  559. packet->entries++;
  560. next:
  561. batadv_neigh_node_free_ref(router);
  562. if (vis_packet_full(info))
  563. goto unlock;
  564. }
  565. rcu_read_unlock();
  566. }
  567. hash = bat_priv->tt_local_hash;
  568. for (i = 0; i < hash->size; i++) {
  569. head = &hash->table[i];
  570. rcu_read_lock();
  571. hlist_for_each_entry_rcu(tt_common_entry, node, head,
  572. hash_entry) {
  573. entry = (struct vis_info_entry *)
  574. skb_put(info->skb_packet,
  575. sizeof(*entry));
  576. memset(entry->src, 0, ETH_ALEN);
  577. memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
  578. entry->quality = 0; /* 0 means TT */
  579. packet->entries++;
  580. if (vis_packet_full(info))
  581. goto unlock;
  582. }
  583. rcu_read_unlock();
  584. }
  585. return 0;
  586. unlock:
  587. rcu_read_unlock();
  588. return 0;
  589. }
  590. /* free old vis packets. Must be called with this vis_hash_lock
  591. * held */
  592. static void purge_vis_packets(struct bat_priv *bat_priv)
  593. {
  594. uint32_t i;
  595. struct hashtable_t *hash = bat_priv->vis_hash;
  596. struct hlist_node *node, *node_tmp;
  597. struct hlist_head *head;
  598. struct vis_info *info;
  599. for (i = 0; i < hash->size; i++) {
  600. head = &hash->table[i];
  601. hlist_for_each_entry_safe(info, node, node_tmp,
  602. head, hash_entry) {
  603. /* never purge own data. */
  604. if (info == bat_priv->my_vis_info)
  605. continue;
  606. if (has_timed_out(info->first_seen, VIS_TIMEOUT)) {
  607. hlist_del(node);
  608. send_list_del(info);
  609. kref_put(&info->refcount, free_info);
  610. }
  611. }
  612. }
  613. }
  614. static void broadcast_vis_packet(struct bat_priv *bat_priv,
  615. struct vis_info *info)
  616. {
  617. struct neigh_node *router;
  618. struct hashtable_t *hash = bat_priv->orig_hash;
  619. struct hlist_node *node;
  620. struct hlist_head *head;
  621. struct orig_node *orig_node;
  622. struct vis_packet *packet;
  623. struct sk_buff *skb;
  624. struct hard_iface *hard_iface;
  625. uint8_t dstaddr[ETH_ALEN];
  626. uint32_t i;
  627. packet = (struct vis_packet *)info->skb_packet->data;
  628. /* send to all routers in range. */
  629. for (i = 0; i < hash->size; i++) {
  630. head = &hash->table[i];
  631. rcu_read_lock();
  632. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  633. /* if it's a vis server and reachable, send it. */
  634. if (!(orig_node->flags & VIS_SERVER))
  635. continue;
  636. router = batadv_orig_node_get_router(orig_node);
  637. if (!router)
  638. continue;
  639. /* don't send it if we already received the packet from
  640. * this node. */
  641. if (recv_list_is_in(bat_priv, &info->recv_list,
  642. orig_node->orig)) {
  643. batadv_neigh_node_free_ref(router);
  644. continue;
  645. }
  646. memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
  647. hard_iface = router->if_incoming;
  648. memcpy(dstaddr, router->addr, ETH_ALEN);
  649. batadv_neigh_node_free_ref(router);
  650. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  651. if (skb)
  652. send_skb_packet(skb, hard_iface, dstaddr);
  653. }
  654. rcu_read_unlock();
  655. }
  656. }
  657. static void unicast_vis_packet(struct bat_priv *bat_priv,
  658. struct vis_info *info)
  659. {
  660. struct orig_node *orig_node;
  661. struct neigh_node *router = NULL;
  662. struct sk_buff *skb;
  663. struct vis_packet *packet;
  664. packet = (struct vis_packet *)info->skb_packet->data;
  665. orig_node = orig_hash_find(bat_priv, packet->target_orig);
  666. if (!orig_node)
  667. goto out;
  668. router = batadv_orig_node_get_router(orig_node);
  669. if (!router)
  670. goto out;
  671. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  672. if (skb)
  673. send_skb_packet(skb, router->if_incoming, router->addr);
  674. out:
  675. if (router)
  676. batadv_neigh_node_free_ref(router);
  677. if (orig_node)
  678. batadv_orig_node_free_ref(orig_node);
  679. }
  680. /* only send one vis packet. called from send_vis_packets() */
  681. static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
  682. {
  683. struct hard_iface *primary_if;
  684. struct vis_packet *packet;
  685. primary_if = primary_if_get_selected(bat_priv);
  686. if (!primary_if)
  687. goto out;
  688. packet = (struct vis_packet *)info->skb_packet->data;
  689. if (packet->header.ttl < 2) {
  690. pr_debug("Error - can't send vis packet: ttl exceeded\n");
  691. goto out;
  692. }
  693. memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  694. packet->header.ttl--;
  695. if (is_broadcast_ether_addr(packet->target_orig))
  696. broadcast_vis_packet(bat_priv, info);
  697. else
  698. unicast_vis_packet(bat_priv, info);
  699. packet->header.ttl++; /* restore TTL */
  700. out:
  701. if (primary_if)
  702. hardif_free_ref(primary_if);
  703. }
  704. /* called from timer; send (and maybe generate) vis packet. */
  705. static void send_vis_packets(struct work_struct *work)
  706. {
  707. struct delayed_work *delayed_work =
  708. container_of(work, struct delayed_work, work);
  709. struct bat_priv *bat_priv =
  710. container_of(delayed_work, struct bat_priv, vis_work);
  711. struct vis_info *info;
  712. spin_lock_bh(&bat_priv->vis_hash_lock);
  713. purge_vis_packets(bat_priv);
  714. if (generate_vis_packet(bat_priv) == 0) {
  715. /* schedule if generation was successful */
  716. send_list_add(bat_priv, bat_priv->my_vis_info);
  717. }
  718. while (!list_empty(&bat_priv->vis_send_list)) {
  719. info = list_first_entry(&bat_priv->vis_send_list,
  720. typeof(*info), send_list);
  721. kref_get(&info->refcount);
  722. spin_unlock_bh(&bat_priv->vis_hash_lock);
  723. send_vis_packet(bat_priv, info);
  724. spin_lock_bh(&bat_priv->vis_hash_lock);
  725. send_list_del(info);
  726. kref_put(&info->refcount, free_info);
  727. }
  728. spin_unlock_bh(&bat_priv->vis_hash_lock);
  729. start_vis_timer(bat_priv);
  730. }
  731. /* init the vis server. this may only be called when if_list is already
  732. * initialized (e.g. bat0 is initialized, interfaces have been added) */
  733. int vis_init(struct bat_priv *bat_priv)
  734. {
  735. struct vis_packet *packet;
  736. int hash_added;
  737. if (bat_priv->vis_hash)
  738. return 0;
  739. spin_lock_bh(&bat_priv->vis_hash_lock);
  740. bat_priv->vis_hash = batadv_hash_new(256);
  741. if (!bat_priv->vis_hash) {
  742. pr_err("Can't initialize vis_hash\n");
  743. goto err;
  744. }
  745. bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
  746. if (!bat_priv->my_vis_info)
  747. goto err;
  748. bat_priv->my_vis_info->skb_packet = dev_alloc_skb(sizeof(*packet) +
  749. MAX_VIS_PACKET_SIZE +
  750. ETH_HLEN);
  751. if (!bat_priv->my_vis_info->skb_packet)
  752. goto free_info;
  753. skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
  754. packet = (struct vis_packet *)skb_put(bat_priv->my_vis_info->skb_packet,
  755. sizeof(*packet));
  756. /* prefill the vis info */
  757. bat_priv->my_vis_info->first_seen = jiffies -
  758. msecs_to_jiffies(VIS_INTERVAL);
  759. INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
  760. INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
  761. kref_init(&bat_priv->my_vis_info->refcount);
  762. bat_priv->my_vis_info->bat_priv = bat_priv;
  763. packet->header.version = COMPAT_VERSION;
  764. packet->header.packet_type = BAT_VIS;
  765. packet->header.ttl = TTL;
  766. packet->seqno = 0;
  767. packet->entries = 0;
  768. INIT_LIST_HEAD(&bat_priv->vis_send_list);
  769. hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
  770. bat_priv->my_vis_info,
  771. &bat_priv->my_vis_info->hash_entry);
  772. if (hash_added != 0) {
  773. pr_err("Can't add own vis packet into hash\n");
  774. /* not in hash, need to remove it manually. */
  775. kref_put(&bat_priv->my_vis_info->refcount, free_info);
  776. goto err;
  777. }
  778. spin_unlock_bh(&bat_priv->vis_hash_lock);
  779. start_vis_timer(bat_priv);
  780. return 0;
  781. free_info:
  782. kfree(bat_priv->my_vis_info);
  783. bat_priv->my_vis_info = NULL;
  784. err:
  785. spin_unlock_bh(&bat_priv->vis_hash_lock);
  786. vis_quit(bat_priv);
  787. return -ENOMEM;
  788. }
  789. /* Decrease the reference count on a hash item info */
  790. static void free_info_ref(struct hlist_node *node, void *arg)
  791. {
  792. struct vis_info *info;
  793. info = container_of(node, struct vis_info, hash_entry);
  794. send_list_del(info);
  795. kref_put(&info->refcount, free_info);
  796. }
  797. /* shutdown vis-server */
  798. void vis_quit(struct bat_priv *bat_priv)
  799. {
  800. if (!bat_priv->vis_hash)
  801. return;
  802. cancel_delayed_work_sync(&bat_priv->vis_work);
  803. spin_lock_bh(&bat_priv->vis_hash_lock);
  804. /* properly remove, kill timers ... */
  805. hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
  806. bat_priv->vis_hash = NULL;
  807. bat_priv->my_vis_info = NULL;
  808. spin_unlock_bh(&bat_priv->vis_hash_lock);
  809. }
  810. /* schedule packets for (re)transmission */
  811. static void start_vis_timer(struct bat_priv *bat_priv)
  812. {
  813. INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
  814. queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work,
  815. msecs_to_jiffies(VIS_INTERVAL));
  816. }