vis.c 25 KB

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