totemnet.c 39 KB

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