vis.c 26 KB

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