totemnet.c 39 KB

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