4
0

totemnet.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc.
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@mvista.com)
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <assert.h>
  36. #include <pthread.h>
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <sys/un.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/param.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <signal.h>
  53. #include <sched.h>
  54. #include <time.h>
  55. #include <sys/time.h>
  56. #include <sys/poll.h>
  57. #include "aispoll.h"
  58. #include "totemnet.h"
  59. #include "wthread.h"
  60. #include "../include/queue.h"
  61. #include "../include/sq.h"
  62. #include "../include/list.h"
  63. #include "../include/hdb.h"
  64. #include "swab.h"
  65. #include "crypto.h"
  66. #ifdef OPENAIS_SOLARIS
  67. #define MSG_NOSIGNAL 0
  68. #endif
  69. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  70. #define NETIF_STATE_REPORT_UP 1
  71. #define NETIF_STATE_REPORT_DOWN 2
  72. #define BIND_STATE_UNBOUND 0
  73. #define BIND_STATE_REGULAR 1
  74. #define BIND_STATE_LOOPBACK 2
  75. #define HMAC_HASH_SIZE 20
  76. struct security_header {
  77. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  78. unsigned char salt[16]; /* random number */
  79. char msg[0];
  80. } __attribute__((packed));
  81. struct totemnet_mcast_thread_state {
  82. unsigned char iobuf[FRAME_SIZE_MAX];
  83. prng_state prng_state;
  84. };
  85. struct totemnet_socket {
  86. int mcast_recv;
  87. int mcast_send;
  88. int token;
  89. };
  90. struct totemnet_instance {
  91. hmac_state totemnet_hmac_state;
  92. prng_state totemnet_prng_state;
  93. unsigned char totemnet_private_key[1024];
  94. unsigned int totemnet_private_key_len;
  95. poll_handle totemnet_poll_handle;
  96. struct totem_interface *totem_interface;
  97. int netif_state_report;
  98. int netif_bind_state;
  99. struct worker_thread_group worker_thread_group;
  100. void *context;
  101. void (*totemnet_deliver_fn) (
  102. void *context,
  103. void *msg,
  104. int msg_len);
  105. void (*totemnet_iface_change_fn) (
  106. void *context,
  107. struct totem_ip_address *iface_address);
  108. /*
  109. * Function and data used to log messages
  110. */
  111. int totemnet_log_level_security;
  112. int totemnet_log_level_error;
  113. int totemnet_log_level_warning;
  114. int totemnet_log_level_notice;
  115. int totemnet_log_level_debug;
  116. void (*totemnet_log_printf) (char *file, int line, int level, char *format, ...) __attribute__((format(printf, 4, 5)));
  117. totemnet_handle handle;
  118. char iov_buffer[FRAME_SIZE_MAX];
  119. char iov_buffer_flush[FRAME_SIZE_MAX];
  120. struct iovec totemnet_iov_recv;
  121. struct iovec totemnet_iov_recv_flush;
  122. struct totemnet_socket totemnet_sockets;
  123. struct totem_ip_address mcast_address;
  124. int stats_sent;
  125. int stats_recv;
  126. int stats_delv;
  127. int stats_remcasts;
  128. int stats_orf_token;
  129. struct timeval stats_tv_start;
  130. struct totem_ip_address my_id;
  131. int firstrun;
  132. poll_timer_handle timer_netif_check_timeout;
  133. unsigned int my_memb_entries;
  134. int flushing;
  135. struct totem_config *totem_config;
  136. struct totem_ip_address token_target;
  137. };
  138. struct work_item {
  139. struct iovec iovec[20];
  140. int iov_len;
  141. struct totemnet_instance *instance;
  142. };
  143. static void netif_down_check (struct totemnet_instance *instance);
  144. static int totemnet_build_sockets (
  145. struct totemnet_instance *instance,
  146. struct totem_ip_address *bindnet_address,
  147. struct totem_ip_address *mcastaddress,
  148. struct totemnet_socket *sockets,
  149. struct totem_ip_address *bound_to);
  150. static struct totem_ip_address localhost;
  151. /*
  152. * All instances in one database
  153. */
  154. static struct hdb_handle_database totemnet_instance_database = {
  155. .handle_count = 0,
  156. .handles = 0,
  157. .iterator = 0,
  158. .mutex = PTHREAD_MUTEX_INITIALIZER
  159. };
  160. static void totemnet_instance_initialize (struct totemnet_instance *instance)
  161. {
  162. memset (instance, 0, sizeof (struct totemnet_instance));
  163. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  164. instance->totemnet_iov_recv.iov_base = instance->iov_buffer;
  165. instance->totemnet_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  166. instance->totemnet_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  167. instance->totemnet_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  168. /*
  169. * There is always atleast 1 processor
  170. */
  171. instance->my_memb_entries = 1;
  172. }
  173. #define log_printf(level, format, args...) \
  174. instance->totemnet_log_printf (__FILE__, __LINE__, level, format, ##args)
  175. static int authenticate_and_decrypt (
  176. struct totemnet_instance *instance,
  177. struct iovec *iov)
  178. {
  179. unsigned char keys[48];
  180. struct security_header *header =
  181. (struct security_header *)iov[0].iov_base;
  182. prng_state keygen_prng_state;
  183. prng_state stream_prng_state;
  184. unsigned char *hmac_key = &keys[32];
  185. unsigned char *cipher_key = &keys[16];
  186. unsigned char *initial_vector = &keys[0];
  187. unsigned char digest_comparison[HMAC_HASH_SIZE];
  188. unsigned long len;
  189. /*
  190. * Generate MAC, CIPHER, IV keys from private key
  191. */
  192. memset (keys, 0, sizeof (keys));
  193. sober128_start (&keygen_prng_state);
  194. sober128_add_entropy (instance->totemnet_private_key,
  195. instance->totemnet_private_key_len, &keygen_prng_state);
  196. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  197. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  198. /*
  199. * Setup stream cipher
  200. */
  201. sober128_start (&stream_prng_state);
  202. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  203. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  204. /*
  205. * Authenticate contents of message
  206. */
  207. hmac_init (&instance->totemnet_hmac_state, DIGEST_SHA1, hmac_key, 16);
  208. hmac_process (&instance->totemnet_hmac_state,
  209. (unsigned char *)iov->iov_base + HMAC_HASH_SIZE,
  210. iov->iov_len - HMAC_HASH_SIZE);
  211. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  212. assert (HMAC_HASH_SIZE >= len);
  213. hmac_done (&instance->totemnet_hmac_state, digest_comparison, &len);
  214. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  215. log_printf (instance->totemnet_log_level_security, "Received message has invalid digest... ignoring.\n");
  216. return (-1);
  217. }
  218. /*
  219. * Decrypt the contents of the message with the cipher key
  220. */
  221. assert(iov->iov_len >= sizeof (struct security_header));
  222. sober128_read (
  223. (unsigned char *)iov->iov_base + sizeof (struct security_header),
  224. (unsigned long)iov->iov_len - sizeof (struct security_header),
  225. &stream_prng_state);
  226. return (0);
  227. }
  228. static void encrypt_and_sign_worker (
  229. struct totemnet_instance *instance,
  230. unsigned char *buf,
  231. int *buf_len,
  232. struct iovec *iovec,
  233. int iov_len,
  234. prng_state *prng_state_in)
  235. {
  236. int i;
  237. unsigned char *addr;
  238. unsigned char keys[48];
  239. struct security_header *header;
  240. unsigned char *hmac_key = &keys[32];
  241. unsigned char *cipher_key = &keys[16];
  242. unsigned char *initial_vector = &keys[0];
  243. unsigned long len;
  244. int outlen = 0;
  245. hmac_state hmac_state;
  246. prng_state keygen_prng_state;
  247. prng_state stream_prng_state;
  248. header = (struct security_header *)buf;
  249. addr = buf + sizeof (struct security_header);
  250. memset (keys, 0, sizeof (keys));
  251. memset (header->salt, 0, sizeof (header->salt));
  252. /*
  253. * Generate MAC, CIPHER, IV keys from private key
  254. */
  255. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  256. sober128_start (&keygen_prng_state);
  257. sober128_add_entropy (instance->totemnet_private_key,
  258. instance->totemnet_private_key_len,
  259. &keygen_prng_state);
  260. sober128_add_entropy (header->salt, sizeof (header->salt),
  261. &keygen_prng_state);
  262. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  263. /*
  264. * Setup stream cipher
  265. */
  266. sober128_start (&stream_prng_state);
  267. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  268. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  269. outlen = sizeof (struct security_header);
  270. /*
  271. * Copy remainder of message, then encrypt it
  272. */
  273. for (i = 1; i < iov_len; i++) {
  274. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  275. addr += iovec[i].iov_len;
  276. outlen += iovec[i].iov_len;
  277. }
  278. /*
  279. * Encrypt message by XORing stream cipher data
  280. */
  281. sober128_read (buf + sizeof (struct security_header),
  282. outlen - sizeof (struct security_header),
  283. &stream_prng_state);
  284. memset (&hmac_state, 0, sizeof (hmac_state));
  285. /*
  286. * Sign the contents of the message with the hmac key and store signature in message
  287. */
  288. hmac_init (&hmac_state, DIGEST_SHA1, hmac_key, 16);
  289. hmac_process (&hmac_state,
  290. buf + HMAC_HASH_SIZE,
  291. outlen - HMAC_HASH_SIZE);
  292. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  293. hmac_done (&hmac_state, header->hash_digest, &len);
  294. *buf_len = outlen;
  295. }
  296. static inline void ucast_sendmsg (
  297. struct totemnet_instance *instance,
  298. struct totem_ip_address *system_to,
  299. struct iovec *iovec_in,
  300. int iov_len_in)
  301. {
  302. struct msghdr msg_ucast;
  303. int res = 0;
  304. int buf_len;
  305. unsigned char sheader[sizeof (struct security_header)];
  306. unsigned char encrypt_data[FRAME_SIZE_MAX];
  307. struct iovec iovec_encrypt[20];
  308. struct iovec *iovec_sendmsg;
  309. struct sockaddr_storage sockaddr;
  310. int iov_len;
  311. int addrlen;
  312. if (instance->totem_config->secauth == 1) {
  313. iovec_encrypt[0].iov_base = (char *)sheader;
  314. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  315. memcpy (&iovec_encrypt[1], &iovec_in[0],
  316. sizeof (struct iovec) * iov_len_in);
  317. /*
  318. * Encrypt and digest the message
  319. */
  320. encrypt_and_sign_worker (
  321. instance,
  322. encrypt_data,
  323. &buf_len,
  324. iovec_encrypt,
  325. iov_len_in + 1,
  326. &instance->totemnet_prng_state);
  327. iovec_encrypt[0].iov_base = (char *)encrypt_data;
  328. iovec_encrypt[0].iov_len = buf_len;
  329. iovec_sendmsg = &iovec_encrypt[0];
  330. iov_len = 1;
  331. } else {
  332. iovec_sendmsg = iovec_in;
  333. iov_len = iov_len_in;
  334. }
  335. /*
  336. * Build unicast message
  337. */
  338. totemip_totemip_to_sockaddr_convert(system_to,
  339. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  340. msg_ucast.msg_name = &sockaddr;
  341. msg_ucast.msg_namelen = addrlen;
  342. msg_ucast.msg_iov = iovec_sendmsg;
  343. msg_ucast.msg_iovlen = iov_len;
  344. #ifndef OPENAIS_SOLARIS
  345. msg_ucast.msg_control = 0;
  346. msg_ucast.msg_controllen = 0;
  347. msg_ucast.msg_flags = 0;
  348. #else
  349. msg_ucast.msg_accrights = NULL;
  350. msg_ucast.msg_accrightslen = 0;
  351. #endif
  352. /*
  353. * Transmit multicast message
  354. * An error here is recovered by totemsrp
  355. */
  356. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_ucast,
  357. MSG_NOSIGNAL);
  358. }
  359. static inline void mcast_sendmsg (
  360. struct totemnet_instance *instance,
  361. struct iovec *iovec_in,
  362. int iov_len_in)
  363. {
  364. struct msghdr msg_mcast;
  365. int res = 0;
  366. int buf_len;
  367. unsigned char sheader[sizeof (struct security_header)];
  368. unsigned char encrypt_data[FRAME_SIZE_MAX];
  369. struct iovec iovec_encrypt[20];
  370. struct iovec *iovec_sendmsg;
  371. struct sockaddr_storage sockaddr;
  372. int iov_len;
  373. int addrlen;
  374. if (instance->totem_config->secauth == 1) {
  375. iovec_encrypt[0].iov_base = (char *)sheader;
  376. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  377. memcpy (&iovec_encrypt[1], &iovec_in[0],
  378. sizeof (struct iovec) * iov_len_in);
  379. /*
  380. * Encrypt and digest the message
  381. */
  382. encrypt_and_sign_worker (
  383. instance,
  384. encrypt_data,
  385. &buf_len,
  386. iovec_encrypt,
  387. iov_len_in + 1,
  388. &instance->totemnet_prng_state);
  389. iovec_encrypt[0].iov_base = (char *)encrypt_data;
  390. iovec_encrypt[0].iov_len = buf_len;
  391. iovec_sendmsg = &iovec_encrypt[0];
  392. iov_len = 1;
  393. } else {
  394. iovec_sendmsg = iovec_in;
  395. iov_len = iov_len_in;
  396. }
  397. /*
  398. * Build multicast message
  399. */
  400. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  401. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  402. msg_mcast.msg_name = &sockaddr;
  403. msg_mcast.msg_namelen = addrlen;
  404. msg_mcast.msg_iov = iovec_sendmsg;
  405. msg_mcast.msg_iovlen = iov_len;
  406. #ifndef OPENAIS_SOLARIS
  407. msg_mcast.msg_control = 0;
  408. msg_mcast.msg_controllen = 0;
  409. msg_mcast.msg_flags = 0;
  410. #else
  411. msg_mcast.msg_accrights = NULL;
  412. msg_mcast.msg_accrightslen = 0;
  413. #endif
  414. /*
  415. * Transmit multicast message
  416. * An error here is recovered by totemsrp
  417. */
  418. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  419. MSG_NOSIGNAL);
  420. }
  421. static void totemnet_mcast_thread_state_constructor (
  422. void *totemnet_mcast_thread_state_in)
  423. {
  424. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  425. (struct totemnet_mcast_thread_state *)totemnet_mcast_thread_state_in;
  426. memset (totemnet_mcast_thread_state, 0,
  427. sizeof (totemnet_mcast_thread_state));
  428. rng_make_prng (128, PRNG_SOBER,
  429. &totemnet_mcast_thread_state->prng_state, NULL);
  430. }
  431. static void totemnet_mcast_worker_fn (void *thread_state, void *work_item_in)
  432. {
  433. struct work_item *work_item = (struct work_item *)work_item_in;
  434. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  435. (struct totemnet_mcast_thread_state *)thread_state;
  436. struct totemnet_instance *instance = work_item->instance;
  437. struct msghdr msg_mcast;
  438. unsigned char sheader[sizeof (struct security_header)];
  439. int res = 0;
  440. int buf_len;
  441. struct iovec iovec_encrypted;
  442. struct iovec *iovec_sendmsg;
  443. struct sockaddr_storage sockaddr;
  444. unsigned int iovs;
  445. int addrlen;
  446. if (instance->totem_config->secauth == 1) {
  447. memmove (&work_item->iovec[1], &work_item->iovec[0],
  448. work_item->iov_len * sizeof (struct iovec));
  449. work_item->iovec[0].iov_base = (char *)sheader;
  450. work_item->iovec[0].iov_len = sizeof (struct security_header);
  451. /*
  452. * Encrypt and digest the message
  453. */
  454. encrypt_and_sign_worker (
  455. instance,
  456. totemnet_mcast_thread_state->iobuf, &buf_len,
  457. work_item->iovec, work_item->iov_len + 1,
  458. &totemnet_mcast_thread_state->prng_state);
  459. iovec_sendmsg = &iovec_encrypted;
  460. iovec_sendmsg->iov_base = (char *)totemnet_mcast_thread_state->iobuf;
  461. iovec_sendmsg->iov_len = buf_len;
  462. iovs = 1;
  463. } else {
  464. iovec_sendmsg = work_item->iovec;
  465. iovs = work_item->iov_len;
  466. }
  467. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  468. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  469. msg_mcast.msg_name = &sockaddr;
  470. msg_mcast.msg_namelen = addrlen;
  471. msg_mcast.msg_iov = iovec_sendmsg;
  472. msg_mcast.msg_iovlen = iovs;
  473. #ifndef OPENAIS_SOLARIS
  474. msg_mcast.msg_control = 0;
  475. msg_mcast.msg_controllen = 0;
  476. msg_mcast.msg_flags = 0;
  477. #else
  478. msg_mcast.msg_accrights = NULL;
  479. msg_mcast.msg_accrightslen = 0;
  480. #endif
  481. /*
  482. * Transmit multicast message
  483. * An error here is recovered by totemnet
  484. */
  485. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  486. MSG_NOSIGNAL);
  487. if (res > 0) {
  488. instance->stats_sent += res;
  489. }
  490. }
  491. int totemnet_finalize (
  492. totemnet_handle handle)
  493. {
  494. struct totemnet_instance *instance;
  495. int res = 0;
  496. res = hdb_handle_get (&totemnet_instance_database, handle,
  497. (void *)&instance);
  498. if (res != 0) {
  499. res = ENOENT;
  500. goto error_exit;
  501. }
  502. worker_thread_group_exit (&instance->worker_thread_group);
  503. hdb_handle_put (&totemnet_instance_database, handle);
  504. error_exit:
  505. return (res);
  506. }
  507. /*
  508. * Only designed to work with a message with one iov
  509. */
  510. static int net_deliver_fn (
  511. poll_handle handle,
  512. int fd,
  513. int revents,
  514. void *data)
  515. {
  516. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  517. struct msghdr msg_recv;
  518. struct iovec *iovec;
  519. struct security_header *security_header;
  520. struct sockaddr_storage system_from;
  521. int bytes_received;
  522. int res = 0;
  523. char *msg_offset;
  524. unsigned int size_delv;
  525. if (instance->flushing == 1) {
  526. iovec = &instance->totemnet_iov_recv_flush;
  527. } else {
  528. iovec = &instance->totemnet_iov_recv;
  529. }
  530. /*
  531. * Receive datagram
  532. */
  533. msg_recv.msg_name = &system_from;
  534. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  535. msg_recv.msg_iov = iovec;
  536. msg_recv.msg_iovlen = 1;
  537. #ifndef OPENAIS_SOLARIS
  538. msg_recv.msg_control = 0;
  539. msg_recv.msg_controllen = 0;
  540. msg_recv.msg_flags = 0;
  541. #else
  542. msg_recv.msg_accrights = NULL;
  543. msg_recv.msg_accrightslen = 0;
  544. #endif
  545. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  546. if (bytes_received == -1) {
  547. return (0);
  548. } else {
  549. instance->stats_recv += bytes_received;
  550. }
  551. if ((instance->totem_config->secauth == 1) &&
  552. (bytes_received < sizeof (struct security_header))) {
  553. log_printf (instance->totemnet_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  554. return (0);
  555. }
  556. security_header = (struct security_header *)iovec->iov_base;
  557. iovec->iov_len = bytes_received;
  558. if (instance->totem_config->secauth == 1) {
  559. /*
  560. * Authenticate and if authenticated, decrypt datagram
  561. */
  562. res = authenticate_and_decrypt (instance, iovec);
  563. if (res == -1) {
  564. log_printf (instance->totemnet_log_level_security,
  565. "Invalid packet data\n");
  566. iovec->iov_len = FRAME_SIZE_MAX;
  567. return 0;
  568. }
  569. msg_offset = iovec->iov_base +
  570. sizeof (struct security_header);
  571. size_delv = bytes_received - sizeof (struct security_header);
  572. } else {
  573. msg_offset = iovec->iov_base;
  574. size_delv = bytes_received;
  575. }
  576. /*
  577. * Handle incoming message
  578. */
  579. instance->totemnet_deliver_fn (
  580. instance->context,
  581. msg_offset,
  582. size_delv);
  583. iovec->iov_len = FRAME_SIZE_MAX;
  584. return (0);
  585. }
  586. static int netif_determine (
  587. struct totemnet_instance *instance,
  588. struct totem_ip_address *bindnet,
  589. struct totem_ip_address *bound_to,
  590. int *interface_up,
  591. int *interface_num)
  592. {
  593. int res;
  594. res = totemip_iface_check (bindnet, bound_to,
  595. interface_up, interface_num);
  596. /*
  597. * If the desired binding is to an IPV4 network and nodeid isn't
  598. * specified, retrieve the node id from this_ip network address
  599. *
  600. * IPV6 networks must have a node ID specified since the node id
  601. * field is only 32 bits.
  602. */
  603. if (bound_to->family == AF_INET && bound_to->nodeid == 0) {
  604. bound_to->nodeid = totemip_compute_nodeid_from_addr(bound_to);
  605. }
  606. return (res);
  607. }
  608. /*
  609. * If the interface is up, the sockets for totem are built. If the interface is down
  610. * this function is requeued in the timer list to retry building the sockets later.
  611. */
  612. static void timer_function_netif_check_timeout (
  613. void *data)
  614. {
  615. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  616. int res;
  617. int interface_up;
  618. int interface_num;
  619. struct totem_ip_address *bind_address;
  620. /*
  621. * Build sockets for every interface
  622. */
  623. netif_determine (instance,
  624. &instance->totem_interface->bindnet,
  625. &instance->totem_interface->boundto,
  626. &interface_up, &interface_num);
  627. /*
  628. * If the network interface isn't back up and we are already
  629. * in loopback mode, add timer to check again and return
  630. */
  631. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  632. interface_up == 0) ||
  633. (instance->my_memb_entries == 1 &&
  634. instance->netif_bind_state == BIND_STATE_REGULAR &&
  635. interface_up == 1)) {
  636. poll_timer_add (instance->totemnet_poll_handle,
  637. instance->totem_config->downcheck_timeout,
  638. (void *)instance,
  639. timer_function_netif_check_timeout,
  640. &instance->timer_netif_check_timeout);
  641. /*
  642. * Add a timer to check for a downed regular interface
  643. */
  644. return;
  645. }
  646. if (instance->totemnet_sockets.mcast_recv > 0) {
  647. close (instance->totemnet_sockets.mcast_recv);
  648. poll_dispatch_delete (instance->totemnet_poll_handle,
  649. instance->totemnet_sockets.mcast_recv);
  650. }
  651. if (instance->totemnet_sockets.mcast_send > 0) {
  652. close (instance->totemnet_sockets.mcast_send);
  653. }
  654. if (instance->totemnet_sockets.token > 0) {
  655. close (instance->totemnet_sockets.token);
  656. poll_dispatch_delete (instance->totemnet_poll_handle,
  657. instance->totemnet_sockets.token);
  658. }
  659. if (interface_up == 0) {
  660. /*
  661. * Interface is not up
  662. */
  663. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  664. bind_address = &localhost;
  665. /*
  666. * Add a timer to retry building interfaces and request memb_gather_enter
  667. */
  668. poll_timer_add (instance->totemnet_poll_handle,
  669. instance->totem_config->downcheck_timeout,
  670. (void *)instance,
  671. timer_function_netif_check_timeout,
  672. &instance->timer_netif_check_timeout);
  673. } else {
  674. /*
  675. * Interface is up
  676. */
  677. instance->netif_bind_state = BIND_STATE_REGULAR;
  678. bind_address = &instance->totem_interface->bindnet;
  679. }
  680. /*
  681. * Create and bind the multicast and unicast sockets
  682. */
  683. res = totemnet_build_sockets (instance,
  684. &instance->mcast_address,
  685. bind_address,
  686. &instance->totemnet_sockets,
  687. &instance->totem_interface->boundto);
  688. poll_dispatch_add (
  689. instance->totemnet_poll_handle,
  690. instance->totemnet_sockets.mcast_recv,
  691. POLLIN, instance, net_deliver_fn);
  692. poll_dispatch_add (
  693. instance->totemnet_poll_handle,
  694. instance->totemnet_sockets.token,
  695. POLLIN, instance, net_deliver_fn);
  696. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  697. /*
  698. * This reports changes in the interface to the user and totemsrp
  699. */
  700. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  701. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  702. log_printf (instance->totemnet_log_level_notice,
  703. "The network interface [%s] is now up.\n",
  704. totemip_print (&instance->totem_interface->boundto));
  705. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  706. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  707. }
  708. /*
  709. * Add a timer to check for interface going down in single membership
  710. */
  711. if (instance->my_memb_entries == 1) {
  712. poll_timer_add (instance->totemnet_poll_handle,
  713. instance->totem_config->downcheck_timeout,
  714. (void *)instance,
  715. timer_function_netif_check_timeout,
  716. &instance->timer_netif_check_timeout);
  717. }
  718. } else {
  719. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  720. log_printf (instance->totemnet_log_level_notice,
  721. "The network interface is down.\n");
  722. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  723. }
  724. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  725. }
  726. }
  727. /*
  728. * Check if an interface is down and reconfigure
  729. * totemnet waiting for it to come back up
  730. */
  731. static void netif_down_check (struct totemnet_instance *instance)
  732. {
  733. timer_function_netif_check_timeout (instance);
  734. }
  735. /* Set the socket priority to INTERACTIVE to ensure
  736. that our messages don't get queued behind anything else */
  737. static void totemnet_traffic_control_set(struct totemnet_instance *instance, int sock)
  738. {
  739. #ifdef SO_PRIORITY
  740. int prio = 6; /* TC_PRIO_INTERACTIVE */
  741. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int)))
  742. log_printf (instance->totemnet_log_level_warning, "Could not set traffic priority. (%s)\n", strerror (errno));
  743. #endif
  744. }
  745. static int totemnet_build_sockets_ip (
  746. struct totemnet_instance *instance,
  747. struct totem_ip_address *mcast_address,
  748. struct totem_ip_address *bindnet_address,
  749. struct totemnet_socket *sockets,
  750. struct totem_ip_address *bound_to,
  751. int interface_num)
  752. {
  753. struct sockaddr_storage sockaddr;
  754. struct ipv6_mreq mreq6;
  755. struct ip_mreq mreq;
  756. struct sockaddr_storage mcast_ss, boundto_ss;
  757. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  758. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  759. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  760. unsigned int sendbuf_size;
  761. unsigned int recvbuf_size;
  762. unsigned int optlen = sizeof (sendbuf_size);
  763. int addrlen;
  764. int res;
  765. int flag;
  766. /*
  767. * Create multicast recv socket
  768. */
  769. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  770. if (sockets->mcast_recv == -1) {
  771. perror ("socket");
  772. return (-1);
  773. }
  774. totemip_nosigpipe (sockets->mcast_recv);
  775. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  776. if (res == -1) {
  777. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  778. return (-1);
  779. }
  780. /*
  781. * Force reuse
  782. */
  783. flag = 1;
  784. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  785. perror("setsockopt reuseaddr");
  786. return (-1);
  787. }
  788. /*
  789. * Bind to multicast socket used for multicast receives
  790. */
  791. totemip_totemip_to_sockaddr_convert(mcast_address,
  792. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  793. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  794. if (res == -1) {
  795. perror ("bind mcast recv socket failed");
  796. return (-1);
  797. }
  798. /*
  799. * Setup mcast send socket
  800. */
  801. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  802. if (sockets->mcast_send == -1) {
  803. perror ("socket");
  804. return (-1);
  805. }
  806. totemip_nosigpipe (sockets->mcast_send);
  807. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  808. if (res == -1) {
  809. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  810. return (-1);
  811. }
  812. /*
  813. * Force reuse
  814. */
  815. flag = 1;
  816. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  817. perror("setsockopt reuseaddr");
  818. return (-1);
  819. }
  820. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  821. &sockaddr, &addrlen);
  822. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  823. if (res == -1) {
  824. perror ("bind mcast send socket failed");
  825. return (-1);
  826. }
  827. /*
  828. * Setup unicast socket
  829. */
  830. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  831. if (sockets->token == -1) {
  832. perror ("socket2");
  833. return (-1);
  834. }
  835. totemip_nosigpipe (sockets->token);
  836. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  837. if (res == -1) {
  838. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on token socket: %s\n", strerror (errno));
  839. return (-1);
  840. }
  841. /*
  842. * Force reuse
  843. */
  844. flag = 1;
  845. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  846. perror("setsockopt reuseaddr");
  847. return (-1);
  848. }
  849. /*
  850. * Bind to unicast socket used for token send/receives
  851. * This has the side effect of binding to the correct interface
  852. */
  853. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  854. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  855. if (res == -1) {
  856. perror ("bind token socket failed");
  857. return (-1);
  858. }
  859. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  860. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  861. /*
  862. * Set buffer sizes to avoid overruns
  863. */
  864. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  865. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  866. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  867. if (res == 0) {
  868. log_printf (instance->totemnet_log_level_notice,
  869. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  870. }
  871. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  872. if (res == 0) {
  873. log_printf (instance->totemnet_log_level_notice,
  874. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  875. }
  876. /*
  877. * Join group membership on socket
  878. */
  879. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  880. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  881. switch ( bindnet_address->family ) {
  882. case AF_INET:
  883. memset(&mreq, 0, sizeof(mreq));
  884. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  885. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  886. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  887. &mreq, sizeof (mreq));
  888. if (res == -1) {
  889. perror ("join ipv4 multicast group failed");
  890. return (-1);
  891. }
  892. break;
  893. case AF_INET6:
  894. memset(&mreq6, 0, sizeof(mreq6));
  895. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  896. mreq6.ipv6mr_interface = interface_num;
  897. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  898. &mreq6, sizeof (mreq6));
  899. if (res == -1) {
  900. perror ("join ipv6 multicast group failed");
  901. return (-1);
  902. }
  903. break;
  904. }
  905. #ifndef OPENAIS_SOLARIS
  906. /*
  907. * Turn on multicast loopback
  908. */
  909. flag = 1;
  910. switch ( bindnet_address->family ) {
  911. case AF_INET:
  912. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_LOOP,
  913. &flag, sizeof (flag));
  914. break;
  915. case AF_INET6:
  916. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  917. &flag, sizeof (flag));
  918. }
  919. if (res == -1) {
  920. perror ("turn off loopback");
  921. return (-1);
  922. }
  923. #endif
  924. /*
  925. * Set multicast packets TTL
  926. */
  927. if ( bindnet_address->family == AF_INET6 )
  928. {
  929. flag = 255;
  930. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  931. &flag, sizeof (flag));
  932. if (res == -1) {
  933. perror ("setp mcast hops");
  934. return (-1);
  935. }
  936. }
  937. /*
  938. * Bind to a specific interface for multicast send and receive
  939. */
  940. switch ( bindnet_address->family ) {
  941. case AF_INET:
  942. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  943. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  944. perror ("cannot select interface");
  945. return (-1);
  946. }
  947. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  948. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  949. perror ("cannot select interface");
  950. return (-1);
  951. }
  952. break;
  953. case AF_INET6:
  954. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  955. &interface_num, sizeof (interface_num)) < 0) {
  956. perror ("cannot select interface");
  957. return (-1);
  958. }
  959. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  960. &interface_num, sizeof (interface_num)) < 0) {
  961. perror ("cannot select interface");
  962. return (-1);
  963. }
  964. break;
  965. }
  966. return 0;
  967. }
  968. static int totemnet_build_sockets (
  969. struct totemnet_instance *instance,
  970. struct totem_ip_address *mcast_address,
  971. struct totem_ip_address *bindnet_address,
  972. struct totemnet_socket *sockets,
  973. struct totem_ip_address *bound_to)
  974. {
  975. int interface_num;
  976. int interface_up;
  977. int res;
  978. /*
  979. * Determine the ip address bound to and the interface name
  980. */
  981. res = netif_determine (instance,
  982. bindnet_address,
  983. bound_to,
  984. &interface_up,
  985. &interface_num);
  986. if (res == -1) {
  987. return (-1);
  988. }
  989. totemip_copy(&instance->my_id, bound_to);
  990. res = totemnet_build_sockets_ip (instance, mcast_address,
  991. bindnet_address, sockets, bound_to, interface_num);
  992. /* We only send out of the token socket */
  993. totemnet_traffic_control_set(instance, sockets->token);
  994. return res;
  995. }
  996. /*
  997. * Totem Network interface - also does encryption/decryption
  998. * depends on poll abstraction, POSIX, IPV4
  999. */
  1000. /*
  1001. * Create an instance
  1002. */
  1003. int totemnet_initialize (
  1004. poll_handle poll_handle,
  1005. totemnet_handle *handle,
  1006. struct totem_config *totem_config,
  1007. int interface_no,
  1008. void *context,
  1009. void (*deliver_fn) (
  1010. void *context,
  1011. void *msg,
  1012. int msg_len),
  1013. void (*iface_change_fn) (
  1014. void *context,
  1015. struct totem_ip_address *iface_address))
  1016. {
  1017. struct totemnet_instance *instance;
  1018. unsigned int res;
  1019. res = hdb_handle_create (&totemnet_instance_database,
  1020. sizeof (struct totemnet_instance), handle);
  1021. if (res != 0) {
  1022. goto error_exit;
  1023. }
  1024. res = hdb_handle_get (&totemnet_instance_database, *handle,
  1025. (void *)&instance);
  1026. if (res != 0) {
  1027. goto error_destroy;
  1028. }
  1029. totemnet_instance_initialize (instance);
  1030. instance->totem_config = totem_config;
  1031. /*
  1032. * Configure logging
  1033. */
  1034. instance->totemnet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1035. instance->totemnet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1036. instance->totemnet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1037. instance->totemnet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1038. instance->totemnet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1039. instance->totemnet_log_printf = totem_config->totem_logging_configuration.log_printf;
  1040. /*
  1041. * Initialize random number generator for later use to generate salt
  1042. */
  1043. memcpy (instance->totemnet_private_key, totem_config->private_key,
  1044. totem_config->private_key_len);
  1045. instance->totemnet_private_key_len = totem_config->private_key_len;
  1046. rng_make_prng (128, PRNG_SOBER, &instance->totemnet_prng_state, NULL);
  1047. /*
  1048. * Initialize local variables for totemnet
  1049. */
  1050. instance->totem_interface = &totem_config->interfaces[interface_no];
  1051. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1052. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1053. /*
  1054. * If threaded send requested, initialize thread group data structure
  1055. */
  1056. if (totem_config->threads) {
  1057. worker_thread_group_init (
  1058. &instance->worker_thread_group,
  1059. totem_config->threads, 128,
  1060. sizeof (struct work_item),
  1061. sizeof (struct totemnet_mcast_thread_state),
  1062. totemnet_mcast_thread_state_constructor,
  1063. totemnet_mcast_worker_fn);
  1064. }
  1065. instance->totemnet_poll_handle = poll_handle;
  1066. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1067. instance->context = context;
  1068. instance->totemnet_deliver_fn = deliver_fn;
  1069. instance->totemnet_iface_change_fn = iface_change_fn;
  1070. instance->handle = *handle;
  1071. rng_make_prng (128, PRNG_SOBER, &instance->totemnet_prng_state, NULL);
  1072. totemip_localhost (instance->mcast_address.family, &localhost);
  1073. netif_down_check (instance);
  1074. error_exit:
  1075. hdb_handle_put (&totemnet_instance_database, *handle);
  1076. return (0);
  1077. error_destroy:
  1078. hdb_handle_destroy (&totemnet_instance_database, *handle);
  1079. return (-1);
  1080. }
  1081. int totemnet_processor_count_set (
  1082. totemnet_handle handle,
  1083. int processor_count)
  1084. {
  1085. struct totemnet_instance *instance;
  1086. int res = 0;
  1087. res = hdb_handle_get (&totemnet_instance_database, handle,
  1088. (void *)&instance);
  1089. if (res != 0) {
  1090. res = ENOENT;
  1091. goto error_exit;
  1092. }
  1093. instance->my_memb_entries = processor_count;
  1094. poll_timer_delete (instance->totemnet_poll_handle,
  1095. instance->timer_netif_check_timeout);
  1096. if (processor_count == 1) {
  1097. poll_timer_add (instance->totemnet_poll_handle,
  1098. instance->totem_config->downcheck_timeout,
  1099. (void *)instance,
  1100. timer_function_netif_check_timeout,
  1101. &instance->timer_netif_check_timeout);
  1102. }
  1103. hdb_handle_put (&totemnet_instance_database, handle);
  1104. error_exit:
  1105. return (res);
  1106. }
  1107. int totemnet_recv_flush (totemnet_handle handle)
  1108. {
  1109. struct totemnet_instance *instance;
  1110. struct pollfd ufd;
  1111. int nfds;
  1112. int res = 0;
  1113. res = hdb_handle_get (&totemnet_instance_database, handle,
  1114. (void *)&instance);
  1115. if (res != 0) {
  1116. res = ENOENT;
  1117. goto error_exit;
  1118. }
  1119. instance->flushing = 1;
  1120. do {
  1121. ufd.fd = instance->totemnet_sockets.mcast_recv;
  1122. ufd.events = POLLIN;
  1123. nfds = poll (&ufd, 1, 0);
  1124. if (nfds == 1 && ufd.revents & POLLIN) {
  1125. net_deliver_fn (0, instance->totemnet_sockets.mcast_recv,
  1126. ufd.revents, instance);
  1127. }
  1128. } while (nfds == 1);
  1129. instance->flushing = 0;
  1130. hdb_handle_put (&totemnet_instance_database, handle);
  1131. error_exit:
  1132. return (res);
  1133. }
  1134. int totemnet_send_flush (totemnet_handle handle)
  1135. {
  1136. struct totemnet_instance *instance;
  1137. int res = 0;
  1138. res = hdb_handle_get (&totemnet_instance_database, handle,
  1139. (void *)&instance);
  1140. if (res != 0) {
  1141. res = ENOENT;
  1142. goto error_exit;
  1143. }
  1144. worker_thread_group_wait (&instance->worker_thread_group);
  1145. hdb_handle_put (&totemnet_instance_database, handle);
  1146. error_exit:
  1147. return (res);
  1148. }
  1149. int totemnet_token_send (
  1150. totemnet_handle handle,
  1151. struct iovec *iovec,
  1152. int iov_len)
  1153. {
  1154. struct totemnet_instance *instance;
  1155. int res = 0;
  1156. res = hdb_handle_get (&totemnet_instance_database, handle,
  1157. (void *)&instance);
  1158. if (res != 0) {
  1159. res = ENOENT;
  1160. goto error_exit;
  1161. }
  1162. ucast_sendmsg (instance, &instance->token_target, iovec, iov_len);
  1163. hdb_handle_put (&totemnet_instance_database, handle);
  1164. error_exit:
  1165. return (res);
  1166. }
  1167. int totemnet_mcast_flush_send (
  1168. totemnet_handle handle,
  1169. struct iovec *iovec,
  1170. unsigned int iov_len)
  1171. {
  1172. struct totemnet_instance *instance;
  1173. int res = 0;
  1174. res = hdb_handle_get (&totemnet_instance_database, handle,
  1175. (void *)&instance);
  1176. if (res != 0) {
  1177. res = ENOENT;
  1178. goto error_exit;
  1179. }
  1180. mcast_sendmsg (instance, iovec, iov_len);
  1181. hdb_handle_put (&totemnet_instance_database, handle);
  1182. error_exit:
  1183. return (res);
  1184. }
  1185. int totemnet_mcast_noflush_send (
  1186. totemnet_handle handle,
  1187. struct iovec *iovec,
  1188. unsigned int iov_len)
  1189. {
  1190. struct totemnet_instance *instance;
  1191. struct work_item work_item;
  1192. int res = 0;
  1193. res = hdb_handle_get (&totemnet_instance_database, handle,
  1194. (void *)&instance);
  1195. if (res != 0) {
  1196. res = ENOENT;
  1197. goto error_exit;
  1198. }
  1199. if (instance->totem_config->threads) {
  1200. memcpy (&work_item.iovec[0], iovec, iov_len * sizeof (struct iovec));
  1201. work_item.iov_len = iov_len;
  1202. work_item.instance = instance;
  1203. worker_thread_group_work_add (&instance->worker_thread_group,
  1204. &work_item);
  1205. } else {
  1206. mcast_sendmsg (instance, iovec, iov_len);
  1207. }
  1208. hdb_handle_put (&totemnet_instance_database, handle);
  1209. error_exit:
  1210. return (res);
  1211. }
  1212. extern int totemnet_iface_check (totemnet_handle handle)
  1213. {
  1214. struct totemnet_instance *instance;
  1215. int res = 0;
  1216. res = hdb_handle_get (&totemnet_instance_database, handle,
  1217. (void *)&instance);
  1218. if (res != 0) {
  1219. res = ENOENT;
  1220. goto error_exit;
  1221. }
  1222. timer_function_netif_check_timeout (instance);
  1223. hdb_handle_put (&totemnet_instance_database, handle);
  1224. error_exit:
  1225. return (res);
  1226. }
  1227. extern void totemnet_net_mtu_adjust (struct totem_config *totem_config)
  1228. {
  1229. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1230. if (totem_config->secauth == 1) {
  1231. totem_config->net_mtu -= sizeof (struct security_header) +
  1232. UDPIP_HEADER_SIZE;
  1233. } else {
  1234. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1235. }
  1236. }
  1237. char *totemnet_iface_print (totemnet_handle handle) {
  1238. struct totemnet_instance *instance;
  1239. int res = 0;
  1240. char *ret_char;
  1241. res = hdb_handle_get (&totemnet_instance_database, handle,
  1242. (void *)&instance);
  1243. if (res != 0) {
  1244. ret_char = "Invalid totemnet handle";
  1245. goto error_exit;
  1246. }
  1247. ret_char = (char *)totemip_print (&instance->my_id);
  1248. hdb_handle_put (&totemnet_instance_database, handle);
  1249. error_exit:
  1250. return (ret_char);
  1251. }
  1252. int totemnet_iface_get (
  1253. totemnet_handle handle,
  1254. struct totem_ip_address *addr)
  1255. {
  1256. struct totemnet_instance *instance;
  1257. unsigned int res;
  1258. res = hdb_handle_get (&totemnet_instance_database, handle,
  1259. (void *)&instance);
  1260. if (res != 0) {
  1261. goto error_exit;
  1262. }
  1263. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1264. hdb_handle_put (&totemnet_instance_database, handle);
  1265. error_exit:
  1266. return (res);
  1267. }
  1268. int totemnet_token_target_set (
  1269. totemnet_handle handle,
  1270. struct totem_ip_address *token_target)
  1271. {
  1272. struct totemnet_instance *instance;
  1273. unsigned int res;
  1274. res = hdb_handle_get (&totemnet_instance_database, handle,
  1275. (void *)&instance);
  1276. if (res != 0) {
  1277. goto error_exit;
  1278. }
  1279. memcpy (&instance->token_target, token_target,
  1280. sizeof (struct totem_ip_address));
  1281. hdb_handle_put (&totemnet_instance_database, handle);
  1282. error_exit:
  1283. return (res);
  1284. }