vis.c 26 KB

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