vis.c 26 KB

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