vis.c 25 KB

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