totemnet.c 37 KB

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