vis.c 25 KB

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