totemnet.c 38 KB

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