vis.c 25 KB

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