vis.c 25 KB

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