vis.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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_puts(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_puts(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 = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
  337. if (!info->skb_packet) {
  338. kfree(info);
  339. return NULL;
  340. }
  341. info->skb_packet->priority = TC_PRIO_CONTROL;
  342. skb_reserve(info->skb_packet, ETH_HLEN);
  343. packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
  344. kref_init(&info->refcount);
  345. INIT_LIST_HEAD(&info->send_list);
  346. INIT_LIST_HEAD(&info->recv_list);
  347. info->first_seen = jiffies;
  348. info->bat_priv = bat_priv;
  349. memcpy(packet, vis_packet, len);
  350. /* initialize and add new packet. */
  351. *is_new = 1;
  352. /* Make it a broadcast packet, if required */
  353. if (make_broadcast)
  354. memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
  355. /* repair if entries is longer than packet. */
  356. max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry);
  357. if (packet->entries > max_entries)
  358. packet->entries = max_entries;
  359. batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
  360. /* try to add it */
  361. hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
  362. batadv_vis_info_choose, info,
  363. &info->hash_entry);
  364. if (hash_added != 0) {
  365. /* did not work (for some reason) */
  366. kref_put(&info->refcount, batadv_free_info);
  367. info = NULL;
  368. }
  369. return info;
  370. }
  371. /* handle the server sync packet, forward if needed. */
  372. void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
  373. struct batadv_vis_packet *vis_packet,
  374. int vis_info_len)
  375. {
  376. struct batadv_vis_info *info;
  377. int is_new, make_broadcast;
  378. int vis_server = atomic_read(&bat_priv->vis_mode);
  379. make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
  380. spin_lock_bh(&bat_priv->vis.hash_lock);
  381. info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
  382. &is_new, make_broadcast);
  383. if (!info)
  384. goto end;
  385. /* only if we are server ourselves and packet is newer than the one in
  386. * hash.
  387. */
  388. if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
  389. batadv_send_list_add(bat_priv, info);
  390. end:
  391. spin_unlock_bh(&bat_priv->vis.hash_lock);
  392. }
  393. /* handle an incoming client update packet and schedule forward if needed. */
  394. void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
  395. struct batadv_vis_packet *vis_packet,
  396. int vis_info_len)
  397. {
  398. struct batadv_vis_info *info;
  399. struct batadv_vis_packet *packet;
  400. int is_new;
  401. int vis_server = atomic_read(&bat_priv->vis_mode);
  402. int are_target = 0;
  403. /* clients shall not broadcast. */
  404. if (is_broadcast_ether_addr(vis_packet->target_orig))
  405. return;
  406. /* Are we the target for this VIS packet? */
  407. if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC &&
  408. batadv_is_my_mac(bat_priv, vis_packet->target_orig))
  409. are_target = 1;
  410. spin_lock_bh(&bat_priv->vis.hash_lock);
  411. info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
  412. &is_new, are_target);
  413. if (!info)
  414. goto end;
  415. /* note that outdated packets will be dropped at this point. */
  416. packet = (struct batadv_vis_packet *)info->skb_packet->data;
  417. /* send only if we're the target server or ... */
  418. if (are_target && is_new) {
  419. packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
  420. batadv_send_list_add(bat_priv, info);
  421. /* ... we're not the recipient (and thus need to forward). */
  422. } else if (!batadv_is_my_mac(bat_priv, packet->target_orig)) {
  423. batadv_send_list_add(bat_priv, info);
  424. }
  425. end:
  426. spin_unlock_bh(&bat_priv->vis.hash_lock);
  427. }
  428. /* Walk the originators and find the VIS server with the best tq. Set the packet
  429. * address to its address and return the best_tq.
  430. *
  431. * Must be called with the originator hash locked
  432. */
  433. static int batadv_find_best_vis_server(struct batadv_priv *bat_priv,
  434. struct batadv_vis_info *info)
  435. {
  436. struct batadv_hashtable *hash = bat_priv->orig_hash;
  437. struct batadv_neigh_node *router;
  438. struct hlist_head *head;
  439. struct batadv_orig_node *orig_node;
  440. struct batadv_vis_packet *packet;
  441. int best_tq = -1;
  442. uint32_t i;
  443. packet = (struct batadv_vis_packet *)info->skb_packet->data;
  444. for (i = 0; i < hash->size; i++) {
  445. head = &hash->table[i];
  446. rcu_read_lock();
  447. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  448. router = batadv_orig_node_get_router(orig_node);
  449. if (!router)
  450. continue;
  451. if ((orig_node->flags & BATADV_VIS_SERVER) &&
  452. (router->tq_avg > best_tq)) {
  453. best_tq = router->tq_avg;
  454. memcpy(packet->target_orig, orig_node->orig,
  455. ETH_ALEN);
  456. }
  457. batadv_neigh_node_free_ref(router);
  458. }
  459. rcu_read_unlock();
  460. }
  461. return best_tq;
  462. }
  463. /* Return true if the vis packet is full. */
  464. static bool batadv_vis_packet_full(const struct batadv_vis_info *info)
  465. {
  466. const struct batadv_vis_packet *packet;
  467. size_t num;
  468. packet = (struct batadv_vis_packet *)info->skb_packet->data;
  469. num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry);
  470. if (num < packet->entries + 1)
  471. return true;
  472. return false;
  473. }
  474. /* generates a packet of own vis data,
  475. * returns 0 on success, -1 if no packet could be generated
  476. */
  477. static int batadv_generate_vis_packet(struct batadv_priv *bat_priv)
  478. {
  479. struct batadv_hashtable *hash = bat_priv->orig_hash;
  480. struct hlist_head *head;
  481. struct batadv_orig_node *orig_node;
  482. struct batadv_neigh_node *router;
  483. struct batadv_vis_info *info = bat_priv->vis.my_info;
  484. struct batadv_vis_packet *packet;
  485. struct batadv_vis_info_entry *entry;
  486. struct batadv_tt_common_entry *tt_common_entry;
  487. uint8_t *packet_pos;
  488. int best_tq = -1;
  489. uint32_t i;
  490. info->first_seen = jiffies;
  491. packet = (struct batadv_vis_packet *)info->skb_packet->data;
  492. packet->vis_type = atomic_read(&bat_priv->vis_mode);
  493. memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
  494. packet->header.ttl = BATADV_TTL;
  495. packet->seqno = htonl(ntohl(packet->seqno) + 1);
  496. packet->entries = 0;
  497. packet->reserved = 0;
  498. skb_trim(info->skb_packet, sizeof(*packet));
  499. if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
  500. best_tq = batadv_find_best_vis_server(bat_priv, info);
  501. if (best_tq < 0)
  502. return best_tq;
  503. }
  504. for (i = 0; i < hash->size; i++) {
  505. head = &hash->table[i];
  506. rcu_read_lock();
  507. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  508. router = batadv_orig_node_get_router(orig_node);
  509. if (!router)
  510. continue;
  511. if (!batadv_compare_eth(router->addr, orig_node->orig))
  512. goto next;
  513. if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
  514. goto next;
  515. if (router->tq_avg < 1)
  516. goto next;
  517. /* fill one entry into buffer. */
  518. packet_pos = skb_put(info->skb_packet, sizeof(*entry));
  519. entry = (struct batadv_vis_info_entry *)packet_pos;
  520. memcpy(entry->src,
  521. router->if_incoming->net_dev->dev_addr,
  522. ETH_ALEN);
  523. memcpy(entry->dest, orig_node->orig, ETH_ALEN);
  524. entry->quality = router->tq_avg;
  525. packet->entries++;
  526. next:
  527. batadv_neigh_node_free_ref(router);
  528. if (batadv_vis_packet_full(info))
  529. goto unlock;
  530. }
  531. rcu_read_unlock();
  532. }
  533. hash = bat_priv->tt.local_hash;
  534. for (i = 0; i < hash->size; i++) {
  535. head = &hash->table[i];
  536. rcu_read_lock();
  537. hlist_for_each_entry_rcu(tt_common_entry, head,
  538. hash_entry) {
  539. packet_pos = skb_put(info->skb_packet, sizeof(*entry));
  540. entry = (struct batadv_vis_info_entry *)packet_pos;
  541. memset(entry->src, 0, ETH_ALEN);
  542. memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
  543. entry->quality = 0; /* 0 means TT */
  544. packet->entries++;
  545. if (batadv_vis_packet_full(info))
  546. goto unlock;
  547. }
  548. rcu_read_unlock();
  549. }
  550. return 0;
  551. unlock:
  552. rcu_read_unlock();
  553. return 0;
  554. }
  555. /* free old vis packets. Must be called with this vis_hash_lock
  556. * held
  557. */
  558. static void batadv_purge_vis_packets(struct batadv_priv *bat_priv)
  559. {
  560. uint32_t i;
  561. struct batadv_hashtable *hash = bat_priv->vis.hash;
  562. struct hlist_node *node_tmp;
  563. struct hlist_head *head;
  564. struct batadv_vis_info *info;
  565. for (i = 0; i < hash->size; i++) {
  566. head = &hash->table[i];
  567. hlist_for_each_entry_safe(info, node_tmp,
  568. head, hash_entry) {
  569. /* never purge own data. */
  570. if (info == bat_priv->vis.my_info)
  571. continue;
  572. if (batadv_has_timed_out(info->first_seen,
  573. BATADV_VIS_TIMEOUT)) {
  574. hlist_del(&info->hash_entry);
  575. batadv_send_list_del(info);
  576. kref_put(&info->refcount, batadv_free_info);
  577. }
  578. }
  579. }
  580. }
  581. static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv,
  582. struct batadv_vis_info *info)
  583. {
  584. struct batadv_hashtable *hash = bat_priv->orig_hash;
  585. struct hlist_head *head;
  586. struct batadv_orig_node *orig_node;
  587. struct batadv_vis_packet *packet;
  588. struct sk_buff *skb;
  589. uint32_t i, res;
  590. packet = (struct batadv_vis_packet *)info->skb_packet->data;
  591. /* send to all routers in range. */
  592. for (i = 0; i < hash->size; i++) {
  593. head = &hash->table[i];
  594. rcu_read_lock();
  595. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  596. /* if it's a vis server and reachable, send it. */
  597. if (!(orig_node->flags & BATADV_VIS_SERVER))
  598. continue;
  599. /* don't send it if we already received the packet from
  600. * this node.
  601. */
  602. if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
  603. orig_node->orig))
  604. continue;
  605. memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
  606. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  607. if (!skb)
  608. continue;
  609. res = batadv_send_skb_to_orig(skb, orig_node, NULL);
  610. if (res == NET_XMIT_DROP)
  611. kfree_skb(skb);
  612. }
  613. rcu_read_unlock();
  614. }
  615. }
  616. static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv,
  617. struct batadv_vis_info *info)
  618. {
  619. struct batadv_orig_node *orig_node;
  620. struct sk_buff *skb;
  621. struct batadv_vis_packet *packet;
  622. packet = (struct batadv_vis_packet *)info->skb_packet->data;
  623. orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
  624. if (!orig_node)
  625. goto out;
  626. skb = skb_clone(info->skb_packet, GFP_ATOMIC);
  627. if (!skb)
  628. goto out;
  629. if (batadv_send_skb_to_orig(skb, orig_node, NULL) == NET_XMIT_DROP)
  630. kfree_skb(skb);
  631. out:
  632. if (orig_node)
  633. batadv_orig_node_free_ref(orig_node);
  634. }
  635. /* only send one vis packet. called from batadv_send_vis_packets() */
  636. static void batadv_send_vis_packet(struct batadv_priv *bat_priv,
  637. struct batadv_vis_info *info)
  638. {
  639. struct batadv_hard_iface *primary_if;
  640. struct batadv_vis_packet *packet;
  641. primary_if = batadv_primary_if_get_selected(bat_priv);
  642. if (!primary_if)
  643. goto out;
  644. packet = (struct batadv_vis_packet *)info->skb_packet->data;
  645. if (packet->header.ttl < 2) {
  646. pr_debug("Error - can't send vis packet: ttl exceeded\n");
  647. goto out;
  648. }
  649. memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  650. packet->header.ttl--;
  651. if (is_broadcast_ether_addr(packet->target_orig))
  652. batadv_broadcast_vis_packet(bat_priv, info);
  653. else
  654. batadv_unicast_vis_packet(bat_priv, info);
  655. packet->header.ttl++; /* restore TTL */
  656. out:
  657. if (primary_if)
  658. batadv_hardif_free_ref(primary_if);
  659. }
  660. /* called from timer; send (and maybe generate) vis packet. */
  661. static void batadv_send_vis_packets(struct work_struct *work)
  662. {
  663. struct delayed_work *delayed_work;
  664. struct batadv_priv *bat_priv;
  665. struct batadv_priv_vis *priv_vis;
  666. struct batadv_vis_info *info;
  667. delayed_work = container_of(work, struct delayed_work, work);
  668. priv_vis = container_of(delayed_work, struct batadv_priv_vis, work);
  669. bat_priv = container_of(priv_vis, struct batadv_priv, vis);
  670. spin_lock_bh(&bat_priv->vis.hash_lock);
  671. batadv_purge_vis_packets(bat_priv);
  672. if (batadv_generate_vis_packet(bat_priv) == 0) {
  673. /* schedule if generation was successful */
  674. batadv_send_list_add(bat_priv, bat_priv->vis.my_info);
  675. }
  676. while (!list_empty(&bat_priv->vis.send_list)) {
  677. info = list_first_entry(&bat_priv->vis.send_list,
  678. typeof(*info), send_list);
  679. kref_get(&info->refcount);
  680. spin_unlock_bh(&bat_priv->vis.hash_lock);
  681. batadv_send_vis_packet(bat_priv, info);
  682. spin_lock_bh(&bat_priv->vis.hash_lock);
  683. batadv_send_list_del(info);
  684. kref_put(&info->refcount, batadv_free_info);
  685. }
  686. spin_unlock_bh(&bat_priv->vis.hash_lock);
  687. queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
  688. msecs_to_jiffies(BATADV_VIS_INTERVAL));
  689. }
  690. /* init the vis server. this may only be called when if_list is already
  691. * initialized (e.g. bat0 is initialized, interfaces have been added)
  692. */
  693. int batadv_vis_init(struct batadv_priv *bat_priv)
  694. {
  695. struct batadv_vis_packet *packet;
  696. int hash_added;
  697. unsigned int len;
  698. unsigned long first_seen;
  699. struct sk_buff *tmp_skb;
  700. if (bat_priv->vis.hash)
  701. return 0;
  702. spin_lock_bh(&bat_priv->vis.hash_lock);
  703. bat_priv->vis.hash = batadv_hash_new(256);
  704. if (!bat_priv->vis.hash) {
  705. pr_err("Can't initialize vis_hash\n");
  706. goto err;
  707. }
  708. batadv_hash_set_lock_class(bat_priv->vis.hash,
  709. &batadv_vis_hash_lock_class_key);
  710. bat_priv->vis.my_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
  711. if (!bat_priv->vis.my_info)
  712. goto err;
  713. len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE + ETH_HLEN;
  714. bat_priv->vis.my_info->skb_packet = netdev_alloc_skb_ip_align(NULL,
  715. len);
  716. if (!bat_priv->vis.my_info->skb_packet)
  717. goto free_info;
  718. bat_priv->vis.my_info->skb_packet->priority = TC_PRIO_CONTROL;
  719. skb_reserve(bat_priv->vis.my_info->skb_packet, ETH_HLEN);
  720. tmp_skb = bat_priv->vis.my_info->skb_packet;
  721. packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
  722. /* prefill the vis info */
  723. first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
  724. bat_priv->vis.my_info->first_seen = first_seen;
  725. INIT_LIST_HEAD(&bat_priv->vis.my_info->recv_list);
  726. INIT_LIST_HEAD(&bat_priv->vis.my_info->send_list);
  727. kref_init(&bat_priv->vis.my_info->refcount);
  728. bat_priv->vis.my_info->bat_priv = bat_priv;
  729. packet->header.version = BATADV_COMPAT_VERSION;
  730. packet->header.packet_type = BATADV_VIS;
  731. packet->header.ttl = BATADV_TTL;
  732. packet->seqno = 0;
  733. packet->reserved = 0;
  734. packet->entries = 0;
  735. INIT_LIST_HEAD(&bat_priv->vis.send_list);
  736. hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
  737. batadv_vis_info_choose,
  738. bat_priv->vis.my_info,
  739. &bat_priv->vis.my_info->hash_entry);
  740. if (hash_added != 0) {
  741. pr_err("Can't add own vis packet into hash\n");
  742. /* not in hash, need to remove it manually. */
  743. kref_put(&bat_priv->vis.my_info->refcount, batadv_free_info);
  744. goto err;
  745. }
  746. spin_unlock_bh(&bat_priv->vis.hash_lock);
  747. INIT_DELAYED_WORK(&bat_priv->vis.work, batadv_send_vis_packets);
  748. queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
  749. msecs_to_jiffies(BATADV_VIS_INTERVAL));
  750. return 0;
  751. free_info:
  752. kfree(bat_priv->vis.my_info);
  753. bat_priv->vis.my_info = NULL;
  754. err:
  755. spin_unlock_bh(&bat_priv->vis.hash_lock);
  756. batadv_vis_quit(bat_priv);
  757. return -ENOMEM;
  758. }
  759. /* Decrease the reference count on a hash item info */
  760. static void batadv_free_info_ref(struct hlist_node *node, void *arg)
  761. {
  762. struct batadv_vis_info *info;
  763. info = container_of(node, struct batadv_vis_info, hash_entry);
  764. batadv_send_list_del(info);
  765. kref_put(&info->refcount, batadv_free_info);
  766. }
  767. /* shutdown vis-server */
  768. void batadv_vis_quit(struct batadv_priv *bat_priv)
  769. {
  770. if (!bat_priv->vis.hash)
  771. return;
  772. cancel_delayed_work_sync(&bat_priv->vis.work);
  773. spin_lock_bh(&bat_priv->vis.hash_lock);
  774. /* properly remove, kill timers ... */
  775. batadv_hash_delete(bat_priv->vis.hash, batadv_free_info_ref, NULL);
  776. bat_priv->vis.hash = NULL;
  777. bat_priv->vis.my_info = NULL;
  778. spin_unlock_bh(&bat_priv->vis.hash_lock);
  779. }