totemnet.c 39 KB

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