vis.c 25 KB

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