vis.c 26 KB

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