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@redhat.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 "swab.h"
  61. #include "../include/queue.h"
  62. #include "../include/sq.h"
  63. #include "../include/list.h"
  64. #include "../include/hdb.h"
  65. #include "swab.h"
  66. #include "crypto.h"
  67. #ifdef OPENAIS_SOLARIS
  68. #define MSG_NOSIGNAL 0
  69. #endif
  70. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  71. #define NETIF_STATE_REPORT_UP 1
  72. #define NETIF_STATE_REPORT_DOWN 2
  73. #define BIND_STATE_UNBOUND 0
  74. #define BIND_STATE_REGULAR 1
  75. #define BIND_STATE_LOOPBACK 2
  76. #define HMAC_HASH_SIZE 20
  77. struct security_header {
  78. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  79. unsigned char salt[16]; /* random number */
  80. char msg[0];
  81. } __attribute__((packed));
  82. struct totemnet_mcast_thread_state {
  83. unsigned char iobuf[FRAME_SIZE_MAX];
  84. prng_state prng_state;
  85. };
  86. struct totemnet_socket {
  87. int mcast_recv;
  88. int mcast_send;
  89. int token;
  90. };
  91. struct totemnet_instance {
  92. hmac_state totemnet_hmac_state;
  93. prng_state totemnet_prng_state;
  94. unsigned char totemnet_private_key[1024];
  95. unsigned int totemnet_private_key_len;
  96. poll_handle totemnet_poll_handle;
  97. struct totem_interface *totem_interface;
  98. int netif_state_report;
  99. int netif_bind_state;
  100. struct worker_thread_group worker_thread_group;
  101. void *context;
  102. void (*totemnet_deliver_fn) (
  103. void *context,
  104. void *msg,
  105. int msg_len);
  106. void (*totemnet_iface_change_fn) (
  107. void *context,
  108. struct totem_ip_address *iface_address);
  109. /*
  110. * Function and data used to log messages
  111. */
  112. int totemnet_log_level_security;
  113. int totemnet_log_level_error;
  114. int totemnet_log_level_warning;
  115. int totemnet_log_level_notice;
  116. int totemnet_log_level_debug;
  117. void (*totemnet_log_printf) (char *file, int line, int level, char *format, ...) __attribute__((format(printf, 4, 5)));
  118. totemnet_handle handle;
  119. char iov_buffer[FRAME_SIZE_MAX];
  120. char iov_buffer_flush[FRAME_SIZE_MAX];
  121. struct iovec totemnet_iov_recv;
  122. struct iovec totemnet_iov_recv_flush;
  123. struct totemnet_socket totemnet_sockets;
  124. struct totem_ip_address mcast_address;
  125. int stats_sent;
  126. int stats_recv;
  127. int stats_delv;
  128. int stats_remcasts;
  129. int stats_orf_token;
  130. struct timeval stats_tv_start;
  131. struct totem_ip_address my_id;
  132. int firstrun;
  133. poll_timer_handle timer_netif_check_timeout;
  134. unsigned int my_memb_entries;
  135. int flushing;
  136. struct totem_config *totem_config;
  137. struct totem_ip_address token_target;
  138. };
  139. struct work_item {
  140. struct iovec iovec[20];
  141. int iov_len;
  142. struct totemnet_instance *instance;
  143. };
  144. static void netif_down_check (struct totemnet_instance *instance);
  145. static int totemnet_build_sockets (
  146. struct totemnet_instance *instance,
  147. struct totem_ip_address *bindnet_address,
  148. struct totem_ip_address *mcastaddress,
  149. struct totemnet_socket *sockets,
  150. struct totem_ip_address *bound_to);
  151. static struct totem_ip_address localhost;
  152. /*
  153. * All instances in one database
  154. */
  155. static struct hdb_handle_database totemnet_instance_database = {
  156. .handle_count = 0,
  157. .handles = 0,
  158. .iterator = 0,
  159. .mutex = PTHREAD_MUTEX_INITIALIZER
  160. };
  161. static void totemnet_instance_initialize (struct totemnet_instance *instance)
  162. {
  163. memset (instance, 0, sizeof (struct totemnet_instance));
  164. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  165. instance->totemnet_iov_recv.iov_base = instance->iov_buffer;
  166. instance->totemnet_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  167. instance->totemnet_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  168. instance->totemnet_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  169. /*
  170. * There is always atleast 1 processor
  171. */
  172. instance->my_memb_entries = 1;
  173. }
  174. #define log_printf(level, format, args...) \
  175. instance->totemnet_log_printf (__FILE__, __LINE__, level, format, ##args)
  176. static int authenticate_and_decrypt (
  177. struct totemnet_instance *instance,
  178. struct iovec *iov)
  179. {
  180. unsigned char keys[48];
  181. struct security_header *header =
  182. (struct security_header *)iov[0].iov_base;
  183. prng_state keygen_prng_state;
  184. prng_state stream_prng_state;
  185. unsigned char *hmac_key = &keys[32];
  186. unsigned char *cipher_key = &keys[16];
  187. unsigned char *initial_vector = &keys[0];
  188. unsigned char digest_comparison[HMAC_HASH_SIZE];
  189. unsigned long len;
  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. return (-1);
  218. }
  219. /*
  220. * Decrypt the contents of the message with the cipher key
  221. */
  222. assert(iov->iov_len >= sizeof (struct security_header));
  223. sober128_read (
  224. (unsigned char *)iov->iov_base + sizeof (struct security_header),
  225. (unsigned long)iov->iov_len - sizeof (struct security_header),
  226. &stream_prng_state);
  227. return (0);
  228. }
  229. static void encrypt_and_sign_worker (
  230. struct totemnet_instance *instance,
  231. unsigned char *buf,
  232. int *buf_len,
  233. struct iovec *iovec,
  234. int iov_len,
  235. prng_state *prng_state_in)
  236. {
  237. int i;
  238. unsigned char *addr;
  239. unsigned char keys[48];
  240. struct security_header *header;
  241. unsigned char *hmac_key = &keys[32];
  242. unsigned char *cipher_key = &keys[16];
  243. unsigned char *initial_vector = &keys[0];
  244. unsigned long len;
  245. int outlen = 0;
  246. hmac_state hmac_state;
  247. prng_state keygen_prng_state;
  248. prng_state stream_prng_state;
  249. header = (struct security_header *)buf;
  250. addr = buf + sizeof (struct security_header);
  251. memset (keys, 0, sizeof (keys));
  252. memset (header->salt, 0, sizeof (header->salt));
  253. /*
  254. * Generate MAC, CIPHER, IV keys from private key
  255. */
  256. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  257. sober128_start (&keygen_prng_state);
  258. sober128_add_entropy (instance->totemnet_private_key,
  259. instance->totemnet_private_key_len,
  260. &keygen_prng_state);
  261. sober128_add_entropy (header->salt, sizeof (header->salt),
  262. &keygen_prng_state);
  263. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  264. /*
  265. * Setup stream cipher
  266. */
  267. sober128_start (&stream_prng_state);
  268. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  269. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  270. outlen = sizeof (struct security_header);
  271. /*
  272. * Copy remainder of message, then encrypt it
  273. */
  274. for (i = 1; i < iov_len; i++) {
  275. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  276. addr += iovec[i].iov_len;
  277. outlen += iovec[i].iov_len;
  278. }
  279. /*
  280. * Encrypt message by XORing stream cipher data
  281. */
  282. sober128_read (buf + sizeof (struct security_header),
  283. outlen - sizeof (struct security_header),
  284. &stream_prng_state);
  285. memset (&hmac_state, 0, sizeof (hmac_state));
  286. /*
  287. * Sign the contents of the message with the hmac key and store signature in message
  288. */
  289. hmac_init (&hmac_state, DIGEST_SHA1, hmac_key, 16);
  290. hmac_process (&hmac_state,
  291. buf + HMAC_HASH_SIZE,
  292. outlen - HMAC_HASH_SIZE);
  293. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  294. hmac_done (&hmac_state, header->hash_digest, &len);
  295. *buf_len = outlen;
  296. }
  297. static inline void ucast_sendmsg (
  298. struct totemnet_instance *instance,
  299. struct totem_ip_address *system_to,
  300. struct iovec *iovec_in,
  301. int iov_len_in)
  302. {
  303. struct msghdr msg_ucast;
  304. int res = 0;
  305. int buf_len;
  306. unsigned char sheader[sizeof (struct security_header)];
  307. unsigned char encrypt_data[FRAME_SIZE_MAX];
  308. struct iovec iovec_encrypt[20];
  309. struct iovec *iovec_sendmsg;
  310. struct sockaddr_storage sockaddr;
  311. int iov_len;
  312. int addrlen;
  313. if (instance->totem_config->secauth == 1) {
  314. iovec_encrypt[0].iov_base = (char *)sheader;
  315. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  316. memcpy (&iovec_encrypt[1], &iovec_in[0],
  317. sizeof (struct iovec) * iov_len_in);
  318. /*
  319. * Encrypt and digest the message
  320. */
  321. encrypt_and_sign_worker (
  322. instance,
  323. encrypt_data,
  324. &buf_len,
  325. iovec_encrypt,
  326. iov_len_in + 1,
  327. &instance->totemnet_prng_state);
  328. iovec_encrypt[0].iov_base = (char *)encrypt_data;
  329. iovec_encrypt[0].iov_len = buf_len;
  330. iovec_sendmsg = &iovec_encrypt[0];
  331. iov_len = 1;
  332. } else {
  333. iovec_sendmsg = iovec_in;
  334. iov_len = iov_len_in;
  335. }
  336. /*
  337. * Build unicast message
  338. */
  339. totemip_totemip_to_sockaddr_convert(system_to,
  340. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  341. msg_ucast.msg_name = &sockaddr;
  342. msg_ucast.msg_namelen = addrlen;
  343. msg_ucast.msg_iov = iovec_sendmsg;
  344. msg_ucast.msg_iovlen = iov_len;
  345. #ifndef OPENAIS_SOLARIS
  346. msg_ucast.msg_control = 0;
  347. msg_ucast.msg_controllen = 0;
  348. msg_ucast.msg_flags = 0;
  349. #else
  350. msg_ucast.msg_accrights = NULL;
  351. msg_ucast.msg_accrightslen = 0;
  352. #endif
  353. /*
  354. * Transmit multicast message
  355. * An error here is recovered by totemsrp
  356. */
  357. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_ucast,
  358. MSG_NOSIGNAL);
  359. }
  360. static inline void mcast_sendmsg (
  361. struct totemnet_instance *instance,
  362. struct iovec *iovec_in,
  363. int iov_len_in)
  364. {
  365. struct msghdr msg_mcast;
  366. int res = 0;
  367. int buf_len;
  368. unsigned char sheader[sizeof (struct security_header)];
  369. unsigned char encrypt_data[FRAME_SIZE_MAX];
  370. struct iovec iovec_encrypt[20];
  371. struct iovec *iovec_sendmsg;
  372. struct sockaddr_storage sockaddr;
  373. int iov_len;
  374. int addrlen;
  375. if (instance->totem_config->secauth == 1) {
  376. iovec_encrypt[0].iov_base = (char *)sheader;
  377. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  378. memcpy (&iovec_encrypt[1], &iovec_in[0],
  379. sizeof (struct iovec) * iov_len_in);
  380. /*
  381. * Encrypt and digest the message
  382. */
  383. encrypt_and_sign_worker (
  384. instance,
  385. encrypt_data,
  386. &buf_len,
  387. iovec_encrypt,
  388. iov_len_in + 1,
  389. &instance->totemnet_prng_state);
  390. iovec_encrypt[0].iov_base = (char *)encrypt_data;
  391. iovec_encrypt[0].iov_len = buf_len;
  392. iovec_sendmsg = &iovec_encrypt[0];
  393. iov_len = 1;
  394. } else {
  395. iovec_sendmsg = iovec_in;
  396. iov_len = iov_len_in;
  397. }
  398. /*
  399. * Build multicast message
  400. */
  401. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  402. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  403. msg_mcast.msg_name = &sockaddr;
  404. msg_mcast.msg_namelen = addrlen;
  405. msg_mcast.msg_iov = iovec_sendmsg;
  406. msg_mcast.msg_iovlen = iov_len;
  407. #ifndef OPENAIS_SOLARIS
  408. msg_mcast.msg_control = 0;
  409. msg_mcast.msg_controllen = 0;
  410. msg_mcast.msg_flags = 0;
  411. #else
  412. msg_mcast.msg_accrights = NULL;
  413. msg_mcast.msg_accrightslen = 0;
  414. #endif
  415. /*
  416. * Transmit multicast message
  417. * An error here is recovered by totemsrp
  418. */
  419. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  420. MSG_NOSIGNAL);
  421. }
  422. static void totemnet_mcast_thread_state_constructor (
  423. void *totemnet_mcast_thread_state_in)
  424. {
  425. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  426. (struct totemnet_mcast_thread_state *)totemnet_mcast_thread_state_in;
  427. memset (totemnet_mcast_thread_state, 0,
  428. sizeof (totemnet_mcast_thread_state));
  429. rng_make_prng (128, PRNG_SOBER,
  430. &totemnet_mcast_thread_state->prng_state, NULL);
  431. }
  432. static void totemnet_mcast_worker_fn (void *thread_state, void *work_item_in)
  433. {
  434. struct work_item *work_item = (struct work_item *)work_item_in;
  435. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  436. (struct totemnet_mcast_thread_state *)thread_state;
  437. struct totemnet_instance *instance = work_item->instance;
  438. struct msghdr msg_mcast;
  439. unsigned char sheader[sizeof (struct security_header)];
  440. int res = 0;
  441. int buf_len;
  442. struct iovec iovec_encrypted;
  443. struct iovec *iovec_sendmsg;
  444. struct sockaddr_storage sockaddr;
  445. unsigned int iovs;
  446. int addrlen;
  447. if (instance->totem_config->secauth == 1) {
  448. memmove (&work_item->iovec[1], &work_item->iovec[0],
  449. work_item->iov_len * sizeof (struct iovec));
  450. work_item->iovec[0].iov_base = (char *)sheader;
  451. work_item->iovec[0].iov_len = sizeof (struct security_header);
  452. /*
  453. * Encrypt and digest the message
  454. */
  455. encrypt_and_sign_worker (
  456. instance,
  457. totemnet_mcast_thread_state->iobuf, &buf_len,
  458. work_item->iovec, work_item->iov_len + 1,
  459. &totemnet_mcast_thread_state->prng_state);
  460. iovec_sendmsg = &iovec_encrypted;
  461. iovec_sendmsg->iov_base = (char *)totemnet_mcast_thread_state->iobuf;
  462. iovec_sendmsg->iov_len = buf_len;
  463. iovs = 1;
  464. } else {
  465. iovec_sendmsg = work_item->iovec;
  466. iovs = work_item->iov_len;
  467. }
  468. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  469. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  470. msg_mcast.msg_name = &sockaddr;
  471. msg_mcast.msg_namelen = addrlen;
  472. msg_mcast.msg_iov = iovec_sendmsg;
  473. msg_mcast.msg_iovlen = iovs;
  474. #ifndef OPENAIS_SOLARIS
  475. msg_mcast.msg_control = 0;
  476. msg_mcast.msg_controllen = 0;
  477. msg_mcast.msg_flags = 0;
  478. #else
  479. msg_mcast.msg_accrights = NULL;
  480. msg_mcast.msg_accrightslen = 0;
  481. #endif
  482. /*
  483. * Transmit multicast message
  484. * An error here is recovered by totemnet
  485. */
  486. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  487. MSG_NOSIGNAL);
  488. if (res > 0) {
  489. instance->stats_sent += res;
  490. }
  491. }
  492. int totemnet_finalize (
  493. totemnet_handle handle)
  494. {
  495. struct totemnet_instance *instance;
  496. int res = 0;
  497. res = hdb_handle_get (&totemnet_instance_database, handle,
  498. (void *)&instance);
  499. if (res != 0) {
  500. res = ENOENT;
  501. goto error_exit;
  502. }
  503. worker_thread_group_exit (&instance->worker_thread_group);
  504. hdb_handle_put (&totemnet_instance_database, handle);
  505. error_exit:
  506. return (res);
  507. }
  508. /*
  509. * Only designed to work with a message with one iov
  510. */
  511. static int net_deliver_fn (
  512. poll_handle handle,
  513. int fd,
  514. int revents,
  515. void *data)
  516. {
  517. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  518. struct msghdr msg_recv;
  519. struct iovec *iovec;
  520. struct security_header *security_header;
  521. struct sockaddr_storage system_from;
  522. int bytes_received;
  523. int res = 0;
  524. char *msg_offset;
  525. unsigned int size_delv;
  526. if (instance->flushing == 1) {
  527. iovec = &instance->totemnet_iov_recv_flush;
  528. } else {
  529. iovec = &instance->totemnet_iov_recv;
  530. }
  531. /*
  532. * Receive datagram
  533. */
  534. msg_recv.msg_name = &system_from;
  535. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  536. msg_recv.msg_iov = iovec;
  537. msg_recv.msg_iovlen = 1;
  538. #ifndef OPENAIS_SOLARIS
  539. msg_recv.msg_control = 0;
  540. msg_recv.msg_controllen = 0;
  541. msg_recv.msg_flags = 0;
  542. #else
  543. msg_recv.msg_accrights = NULL;
  544. msg_recv.msg_accrightslen = 0;
  545. #endif
  546. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  547. if (bytes_received == -1) {
  548. return (0);
  549. } else {
  550. instance->stats_recv += bytes_received;
  551. }
  552. if ((instance->totem_config->secauth == 1) &&
  553. (bytes_received < sizeof (struct security_header))) {
  554. log_printf (instance->totemnet_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  555. return (0);
  556. }
  557. security_header = (struct security_header *)iovec->iov_base;
  558. iovec->iov_len = bytes_received;
  559. if (instance->totem_config->secauth == 1) {
  560. /*
  561. * Authenticate and if authenticated, decrypt datagram
  562. */
  563. res = authenticate_and_decrypt (instance, iovec);
  564. if (res == -1) {
  565. log_printf (instance->totemnet_log_level_security,
  566. "Invalid packet data\n");
  567. iovec->iov_len = FRAME_SIZE_MAX;
  568. return 0;
  569. }
  570. msg_offset = iovec->iov_base +
  571. sizeof (struct security_header);
  572. size_delv = bytes_received - sizeof (struct security_header);
  573. } else {
  574. msg_offset = iovec->iov_base;
  575. size_delv = bytes_received;
  576. }
  577. /*
  578. * Handle incoming message
  579. */
  580. instance->totemnet_deliver_fn (
  581. instance->context,
  582. msg_offset,
  583. size_delv);
  584. iovec->iov_len = FRAME_SIZE_MAX;
  585. return (0);
  586. }
  587. static int netif_determine (
  588. struct totemnet_instance *instance,
  589. struct totem_ip_address *bindnet,
  590. struct totem_ip_address *bound_to,
  591. int *interface_up,
  592. int *interface_num)
  593. {
  594. int res;
  595. res = totemip_iface_check (bindnet, bound_to,
  596. interface_up, interface_num);
  597. /*
  598. * If the desired binding is to an IPV4 network and nodeid isn't
  599. * specified, retrieve the node id from this_ip network address
  600. *
  601. * IPV6 networks must have a node ID specified since the node id
  602. * field is only 32 bits.
  603. */
  604. if (bound_to->family == AF_INET && bound_to->nodeid == 0) {
  605. bound_to->nodeid = totemip_compute_nodeid_from_addr(bound_to);
  606. }
  607. return (res);
  608. }
  609. /*
  610. * If the interface is up, the sockets for totem are built. If the interface is down
  611. * this function is requeued in the timer list to retry building the sockets later.
  612. */
  613. static void timer_function_netif_check_timeout (
  614. void *data)
  615. {
  616. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  617. int res;
  618. int interface_up;
  619. int interface_num;
  620. struct totem_ip_address *bind_address;
  621. /*
  622. * Build sockets for every interface
  623. */
  624. netif_determine (instance,
  625. &instance->totem_interface->bindnet,
  626. &instance->totem_interface->boundto,
  627. &interface_up, &interface_num);
  628. /*
  629. * If the network interface isn't back up and we are already
  630. * in loopback mode, add timer to check again and return
  631. */
  632. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  633. interface_up == 0) ||
  634. (instance->my_memb_entries == 1 &&
  635. instance->netif_bind_state == BIND_STATE_REGULAR &&
  636. interface_up == 1)) {
  637. poll_timer_add (instance->totemnet_poll_handle,
  638. instance->totem_config->downcheck_timeout,
  639. (void *)instance,
  640. timer_function_netif_check_timeout,
  641. &instance->timer_netif_check_timeout);
  642. /*
  643. * Add a timer to check for a downed regular interface
  644. */
  645. return;
  646. }
  647. if (instance->totemnet_sockets.mcast_recv > 0) {
  648. close (instance->totemnet_sockets.mcast_recv);
  649. poll_dispatch_delete (instance->totemnet_poll_handle,
  650. instance->totemnet_sockets.mcast_recv);
  651. }
  652. if (instance->totemnet_sockets.mcast_send > 0) {
  653. close (instance->totemnet_sockets.mcast_send);
  654. }
  655. if (instance->totemnet_sockets.token > 0) {
  656. close (instance->totemnet_sockets.token);
  657. poll_dispatch_delete (instance->totemnet_poll_handle,
  658. instance->totemnet_sockets.token);
  659. }
  660. if (interface_up == 0) {
  661. /*
  662. * Interface is not up
  663. */
  664. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  665. bind_address = &localhost;
  666. /*
  667. * Add a timer to retry building interfaces and request memb_gather_enter
  668. */
  669. poll_timer_add (instance->totemnet_poll_handle,
  670. instance->totem_config->downcheck_timeout,
  671. (void *)instance,
  672. timer_function_netif_check_timeout,
  673. &instance->timer_netif_check_timeout);
  674. } else {
  675. /*
  676. * Interface is up
  677. */
  678. instance->netif_bind_state = BIND_STATE_REGULAR;
  679. bind_address = &instance->totem_interface->bindnet;
  680. }
  681. /*
  682. * Create and bind the multicast and unicast sockets
  683. */
  684. res = totemnet_build_sockets (instance,
  685. &instance->mcast_address,
  686. bind_address,
  687. &instance->totemnet_sockets,
  688. &instance->totem_interface->boundto);
  689. poll_dispatch_add (
  690. instance->totemnet_poll_handle,
  691. instance->totemnet_sockets.mcast_recv,
  692. POLLIN, instance, net_deliver_fn);
  693. poll_dispatch_add (
  694. instance->totemnet_poll_handle,
  695. instance->totemnet_sockets.token,
  696. POLLIN, instance, net_deliver_fn);
  697. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  698. /*
  699. * This reports changes in the interface to the user and totemsrp
  700. */
  701. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  702. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  703. log_printf (instance->totemnet_log_level_notice,
  704. "The network interface [%s] is now up.\n",
  705. totemip_print (&instance->totem_interface->boundto));
  706. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  707. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  708. }
  709. /*
  710. * Add a timer to check for interface going down in single membership
  711. */
  712. if (instance->my_memb_entries == 1) {
  713. poll_timer_add (instance->totemnet_poll_handle,
  714. instance->totem_config->downcheck_timeout,
  715. (void *)instance,
  716. timer_function_netif_check_timeout,
  717. &instance->timer_netif_check_timeout);
  718. }
  719. } else {
  720. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  721. log_printf (instance->totemnet_log_level_notice,
  722. "The network interface is down.\n");
  723. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  724. }
  725. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  726. }
  727. }
  728. /*
  729. * Check if an interface is down and reconfigure
  730. * totemnet waiting for it to come back up
  731. */
  732. static void netif_down_check (struct totemnet_instance *instance)
  733. {
  734. timer_function_netif_check_timeout (instance);
  735. }
  736. /* Set the socket priority to INTERACTIVE to ensure
  737. that our messages don't get queued behind anything else */
  738. static void totemnet_traffic_control_set(struct totemnet_instance *instance, int sock)
  739. {
  740. #ifdef SO_PRIORITY
  741. int prio = 6; /* TC_PRIO_INTERACTIVE */
  742. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int)))
  743. log_printf (instance->totemnet_log_level_warning, "Could not set traffic priority. (%s)\n", strerror (errno));
  744. #endif
  745. }
  746. static int totemnet_build_sockets_ip (
  747. struct totemnet_instance *instance,
  748. struct totem_ip_address *mcast_address,
  749. struct totem_ip_address *bindnet_address,
  750. struct totemnet_socket *sockets,
  751. struct totem_ip_address *bound_to,
  752. int interface_num)
  753. {
  754. struct sockaddr_storage sockaddr;
  755. struct ipv6_mreq mreq6;
  756. struct ip_mreq mreq;
  757. struct sockaddr_storage mcast_ss, boundto_ss;
  758. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  759. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  760. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  761. unsigned int sendbuf_size;
  762. unsigned int recvbuf_size;
  763. unsigned int optlen = sizeof (sendbuf_size);
  764. int addrlen;
  765. int res;
  766. int flag;
  767. /*
  768. * Create multicast recv socket
  769. */
  770. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  771. if (sockets->mcast_recv == -1) {
  772. perror ("socket");
  773. return (-1);
  774. }
  775. totemip_nosigpipe (sockets->mcast_recv);
  776. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  777. if (res == -1) {
  778. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  779. return (-1);
  780. }
  781. /*
  782. * Force reuse
  783. */
  784. flag = 1;
  785. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  786. perror("setsockopt reuseaddr");
  787. return (-1);
  788. }
  789. /*
  790. * Bind to multicast socket used for multicast receives
  791. */
  792. totemip_totemip_to_sockaddr_convert(mcast_address,
  793. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  794. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  795. if (res == -1) {
  796. perror ("bind mcast recv socket failed");
  797. return (-1);
  798. }
  799. /*
  800. * Setup mcast send socket
  801. */
  802. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  803. if (sockets->mcast_send == -1) {
  804. perror ("socket");
  805. return (-1);
  806. }
  807. totemip_nosigpipe (sockets->mcast_send);
  808. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  809. if (res == -1) {
  810. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  811. return (-1);
  812. }
  813. /*
  814. * Force reuse
  815. */
  816. flag = 1;
  817. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  818. perror("setsockopt reuseaddr");
  819. return (-1);
  820. }
  821. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  822. &sockaddr, &addrlen);
  823. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  824. if (res == -1) {
  825. perror ("bind mcast send socket failed");
  826. return (-1);
  827. }
  828. /*
  829. * Setup unicast socket
  830. */
  831. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  832. if (sockets->token == -1) {
  833. perror ("socket2");
  834. return (-1);
  835. }
  836. totemip_nosigpipe (sockets->token);
  837. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  838. if (res == -1) {
  839. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on token socket: %s\n", strerror (errno));
  840. return (-1);
  841. }
  842. /*
  843. * Force reuse
  844. */
  845. flag = 1;
  846. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  847. perror("setsockopt reuseaddr");
  848. return (-1);
  849. }
  850. /*
  851. * Bind to unicast socket used for token send/receives
  852. * This has the side effect of binding to the correct interface
  853. */
  854. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  855. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  856. if (res == -1) {
  857. perror ("bind token socket failed");
  858. return (-1);
  859. }
  860. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  861. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  862. /*
  863. * Set buffer sizes to avoid overruns
  864. */
  865. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  866. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  867. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  868. if (res == 0) {
  869. log_printf (instance->totemnet_log_level_notice,
  870. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  871. }
  872. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  873. if (res == 0) {
  874. log_printf (instance->totemnet_log_level_notice,
  875. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  876. }
  877. /*
  878. * Join group membership on socket
  879. */
  880. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  881. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  882. switch ( bindnet_address->family ) {
  883. case AF_INET:
  884. memset(&mreq, 0, sizeof(mreq));
  885. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  886. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  887. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  888. &mreq, sizeof (mreq));
  889. if (res == -1) {
  890. perror ("join ipv4 multicast group failed");
  891. return (-1);
  892. }
  893. break;
  894. case AF_INET6:
  895. memset(&mreq6, 0, sizeof(mreq6));
  896. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  897. mreq6.ipv6mr_interface = interface_num;
  898. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  899. &mreq6, sizeof (mreq6));
  900. if (res == -1) {
  901. perror ("join ipv6 multicast group failed");
  902. return (-1);
  903. }
  904. break;
  905. }
  906. #ifndef OPENAIS_SOLARIS
  907. /*
  908. * Turn on multicast loopback
  909. */
  910. flag = 1;
  911. switch ( bindnet_address->family ) {
  912. case AF_INET:
  913. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_LOOP,
  914. &flag, sizeof (flag));
  915. break;
  916. case AF_INET6:
  917. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  918. &flag, sizeof (flag));
  919. }
  920. if (res == -1) {
  921. perror ("turn off loopback");
  922. return (-1);
  923. }
  924. #endif
  925. /*
  926. * Set multicast packets TTL
  927. */
  928. if ( bindnet_address->family == AF_INET6 )
  929. {
  930. flag = 255;
  931. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  932. &flag, sizeof (flag));
  933. if (res == -1) {
  934. perror ("setp mcast hops");
  935. return (-1);
  936. }
  937. }
  938. #ifndef OPENAIS_SOLARIS
  939. /*
  940. * Bind to a specific interface for multicast send and receive
  941. */
  942. switch ( bindnet_address->family ) {
  943. case AF_INET:
  944. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  945. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  946. perror ("cannot select interface");
  947. return (-1);
  948. }
  949. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  950. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  951. perror ("cannot select interface");
  952. return (-1);
  953. }
  954. break;
  955. case AF_INET6:
  956. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  957. &interface_num, sizeof (interface_num)) < 0) {
  958. perror ("cannot select interface");
  959. return (-1);
  960. }
  961. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  962. &interface_num, sizeof (interface_num)) < 0) {
  963. perror ("cannot select interface");
  964. return (-1);
  965. }
  966. break;
  967. }
  968. #endif
  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. }