totemnet.c 38 KB

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