vis.c 25 KB

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