totemnet.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  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) (char *file, int line, 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. #define log_printf(level, format, args...) \
  169. instance->totemnet_log_printf (__FILE__, __LINE__, level, format, ##args)
  170. static int authenticate_and_decrypt (
  171. struct totemnet_instance *instance,
  172. struct iovec *iov)
  173. {
  174. unsigned char keys[48];
  175. struct security_header *header = iov[0].iov_base;
  176. prng_state keygen_prng_state;
  177. prng_state stream_prng_state;
  178. unsigned char *hmac_key = &keys[32];
  179. unsigned char *cipher_key = &keys[16];
  180. unsigned char *initial_vector = &keys[0];
  181. unsigned char digest_comparison[HMAC_HASH_SIZE];
  182. unsigned long len;
  183. int res = 0;
  184. /*
  185. * Generate MAC, CIPHER, IV keys from private key
  186. */
  187. memset (keys, 0, sizeof (keys));
  188. sober128_start (&keygen_prng_state);
  189. sober128_add_entropy (instance->totemnet_private_key,
  190. instance->totemnet_private_key_len, &keygen_prng_state);
  191. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  192. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  193. /*
  194. * Setup stream cipher
  195. */
  196. sober128_start (&stream_prng_state);
  197. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  198. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  199. /*
  200. * Authenticate contents of message
  201. */
  202. hmac_init (&instance->totemnet_hmac_state, DIGEST_SHA1, hmac_key, 16);
  203. hmac_process (&instance->totemnet_hmac_state,
  204. iov->iov_base + HMAC_HASH_SIZE,
  205. iov->iov_len - HMAC_HASH_SIZE);
  206. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  207. assert (HMAC_HASH_SIZE >= len);
  208. hmac_done (&instance->totemnet_hmac_state, digest_comparison, &len);
  209. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  210. log_printf (instance->totemnet_log_level_security, "Received message has invalid digest... ignoring.\n");
  211. res = -1;
  212. return (-1);
  213. }
  214. /*
  215. * Decrypt the contents of the message with the cipher key
  216. */
  217. sober128_read (iov->iov_base + sizeof (struct security_header),
  218. iov->iov_len - sizeof (struct security_header),
  219. &stream_prng_state);
  220. return (res);
  221. return (0);
  222. }
  223. static void encrypt_and_sign_worker (
  224. struct totemnet_instance *instance,
  225. unsigned char *buf,
  226. int *buf_len,
  227. struct iovec *iovec,
  228. int iov_len,
  229. prng_state *prng_state_in)
  230. {
  231. int i;
  232. unsigned char *addr;
  233. unsigned char keys[48];
  234. struct security_header *header;
  235. unsigned char *hmac_key = &keys[32];
  236. unsigned char *cipher_key = &keys[16];
  237. unsigned char *initial_vector = &keys[0];
  238. unsigned long len;
  239. int outlen = 0;
  240. hmac_state hmac_state;
  241. prng_state keygen_prng_state;
  242. prng_state stream_prng_state;
  243. header = (struct security_header *)buf;
  244. addr = buf + sizeof (struct security_header);
  245. memset (keys, 0, sizeof (keys));
  246. memset (header->salt, 0, sizeof (header->salt));
  247. /*
  248. * Generate MAC, CIPHER, IV keys from private key
  249. */
  250. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  251. sober128_start (&keygen_prng_state);
  252. sober128_add_entropy (instance->totemnet_private_key,
  253. instance->totemnet_private_key_len,
  254. &keygen_prng_state);
  255. sober128_add_entropy (header->salt, sizeof (header->salt),
  256. &keygen_prng_state);
  257. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  258. /*
  259. * Setup stream cipher
  260. */
  261. sober128_start (&stream_prng_state);
  262. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  263. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  264. outlen = sizeof (struct security_header);
  265. /*
  266. * Copy remainder of message, then encrypt it
  267. */
  268. for (i = 1; i < iov_len; i++) {
  269. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  270. addr += iovec[i].iov_len;
  271. outlen += iovec[i].iov_len;
  272. }
  273. /*
  274. * Encrypt message by XORing stream cipher data
  275. */
  276. sober128_read (buf + sizeof (struct security_header),
  277. outlen - sizeof (struct security_header),
  278. &stream_prng_state);
  279. memset (&hmac_state, 0, sizeof (hmac_state));
  280. /*
  281. * Sign the contents of the message with the hmac key and store signature in message
  282. */
  283. hmac_init (&hmac_state, DIGEST_SHA1, hmac_key, 16);
  284. hmac_process (&hmac_state,
  285. buf + HMAC_HASH_SIZE,
  286. outlen - HMAC_HASH_SIZE);
  287. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  288. hmac_done (&hmac_state, header->hash_digest, &len);
  289. *buf_len = outlen;
  290. }
  291. static inline void ucast_sendmsg (
  292. struct totemnet_instance *instance,
  293. struct totem_ip_address *system_to,
  294. struct iovec *iovec_in,
  295. int iov_len_in)
  296. {
  297. struct msghdr msg_ucast;
  298. int res = 0;
  299. int buf_len;
  300. unsigned char sheader[sizeof (struct security_header)];
  301. unsigned char encrypt_data[FRAME_SIZE_MAX];
  302. struct iovec iovec_encrypt[20];
  303. struct iovec *iovec_sendmsg;
  304. struct sockaddr_storage sockaddr;
  305. int iov_len;
  306. int addrlen;
  307. if (instance->totem_config->secauth == 1) {
  308. iovec_encrypt[0].iov_base = sheader;
  309. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  310. memcpy (&iovec_encrypt[1], &iovec_in[0],
  311. sizeof (struct iovec) * iov_len_in);
  312. /*
  313. * Encrypt and digest the message
  314. */
  315. encrypt_and_sign_worker (
  316. instance,
  317. encrypt_data,
  318. &buf_len,
  319. iovec_encrypt,
  320. iov_len_in + 1,
  321. &instance->totemnet_prng_state);
  322. iovec_encrypt[0].iov_base = encrypt_data;
  323. iovec_encrypt[0].iov_len = buf_len;
  324. iovec_sendmsg = &iovec_encrypt[0];
  325. iov_len = 1;
  326. } else {
  327. iovec_sendmsg = iovec_in;
  328. iov_len = iov_len_in;
  329. }
  330. /*
  331. * Build unicast message
  332. */
  333. totemip_totemip_to_sockaddr_convert(system_to,
  334. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  335. msg_ucast.msg_name = &sockaddr;
  336. msg_ucast.msg_namelen = addrlen;
  337. msg_ucast.msg_iov = iovec_sendmsg;
  338. msg_ucast.msg_iovlen = iov_len;
  339. msg_ucast.msg_control = 0;
  340. msg_ucast.msg_controllen = 0;
  341. msg_ucast.msg_flags = 0;
  342. /*
  343. * Transmit multicast message
  344. * An error here is recovered by totemsrp
  345. */
  346. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_ucast,
  347. MSG_NOSIGNAL);
  348. }
  349. static inline void mcast_sendmsg (
  350. struct totemnet_instance *instance,
  351. struct iovec *iovec_in,
  352. int iov_len_in)
  353. {
  354. struct msghdr msg_mcast;
  355. int res = 0;
  356. int buf_len;
  357. unsigned char sheader[sizeof (struct security_header)];
  358. unsigned char encrypt_data[FRAME_SIZE_MAX];
  359. struct iovec iovec_encrypt[20];
  360. struct iovec *iovec_sendmsg;
  361. struct sockaddr_storage sockaddr;
  362. int iov_len;
  363. int addrlen;
  364. if (instance->totem_config->secauth == 1) {
  365. iovec_encrypt[0].iov_base = sheader;
  366. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  367. memcpy (&iovec_encrypt[1], &iovec_in[0],
  368. sizeof (struct iovec) * iov_len_in);
  369. /*
  370. * Encrypt and digest the message
  371. */
  372. encrypt_and_sign_worker (
  373. instance,
  374. encrypt_data,
  375. &buf_len,
  376. iovec_encrypt,
  377. iov_len_in + 1,
  378. &instance->totemnet_prng_state);
  379. iovec_encrypt[0].iov_base = encrypt_data;
  380. iovec_encrypt[0].iov_len = buf_len;
  381. iovec_sendmsg = &iovec_encrypt[0];
  382. iov_len = 1;
  383. } else {
  384. iovec_sendmsg = iovec_in;
  385. iov_len = iov_len_in;
  386. }
  387. /*
  388. * Build multicast message
  389. */
  390. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  391. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  392. msg_mcast.msg_name = &sockaddr;
  393. msg_mcast.msg_namelen = addrlen;
  394. msg_mcast.msg_iov = iovec_sendmsg;
  395. msg_mcast.msg_iovlen = iov_len;
  396. msg_mcast.msg_control = 0;
  397. msg_mcast.msg_controllen = 0;
  398. msg_mcast.msg_flags = 0;
  399. /*
  400. * Transmit multicast message
  401. * An error here is recovered by totemsrp
  402. */
  403. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  404. MSG_NOSIGNAL);
  405. }
  406. static void totemnet_mcast_thread_state_constructor (
  407. void *totemnet_mcast_thread_state_in)
  408. {
  409. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  410. (struct totemnet_mcast_thread_state *)totemnet_mcast_thread_state_in;
  411. memset (totemnet_mcast_thread_state, 0,
  412. sizeof (totemnet_mcast_thread_state));
  413. rng_make_prng (128, PRNG_SOBER,
  414. &totemnet_mcast_thread_state->prng_state, NULL);
  415. }
  416. static void totemnet_mcast_worker_fn (void *thread_state, void *work_item_in)
  417. {
  418. struct work_item *work_item = (struct work_item *)work_item_in;
  419. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  420. (struct totemnet_mcast_thread_state *)thread_state;
  421. struct totemnet_instance *instance = work_item->instance;
  422. struct msghdr msg_mcast;
  423. unsigned char sheader[sizeof (struct security_header)];
  424. int res = 0;
  425. int buf_len;
  426. struct iovec iovec_encrypted;
  427. struct iovec *iovec_sendmsg;
  428. struct sockaddr_storage sockaddr;
  429. unsigned int iovs;
  430. int addrlen;
  431. if (instance->totem_config->secauth == 1) {
  432. memmove (&work_item->iovec[1], &work_item->iovec[0],
  433. work_item->iov_len * sizeof (struct iovec));
  434. work_item->iovec[0].iov_base = sheader;
  435. work_item->iovec[0].iov_len = sizeof (struct security_header);
  436. /*
  437. * Encrypt and digest the message
  438. */
  439. encrypt_and_sign_worker (
  440. instance,
  441. totemnet_mcast_thread_state->iobuf, &buf_len,
  442. work_item->iovec, work_item->iov_len + 1,
  443. &totemnet_mcast_thread_state->prng_state);
  444. iovec_sendmsg = &iovec_encrypted;
  445. iovec_sendmsg->iov_base = totemnet_mcast_thread_state->iobuf;
  446. iovec_sendmsg->iov_len = buf_len;
  447. iovs = 1;
  448. } else {
  449. iovec_sendmsg = work_item->iovec;
  450. iovs = work_item->iov_len;
  451. }
  452. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  453. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  454. msg_mcast.msg_name = &sockaddr;
  455. msg_mcast.msg_namelen = addrlen;
  456. msg_mcast.msg_iov = iovec_sendmsg;
  457. msg_mcast.msg_iovlen = iovs;
  458. msg_mcast.msg_control = 0;
  459. msg_mcast.msg_controllen = 0;
  460. msg_mcast.msg_flags = 0;
  461. /*
  462. * Transmit multicast message
  463. * An error here is recovered by totemnet
  464. */
  465. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  466. MSG_NOSIGNAL);
  467. if (res > 0) {
  468. instance->stats_sent += res;
  469. }
  470. }
  471. int totemnet_finalize (
  472. totemnet_handle handle)
  473. {
  474. struct totemnet_instance *instance;
  475. int res = 0;
  476. res = hdb_handle_get (&totemnet_instance_database, handle,
  477. (void *)&instance);
  478. if (res != 0) {
  479. res = ENOENT;
  480. goto error_exit;
  481. }
  482. worker_thread_group_exit (&instance->worker_thread_group);
  483. hdb_handle_put (&totemnet_instance_database, handle);
  484. error_exit:
  485. return (res);
  486. }
  487. /*
  488. * Only designed to work with a message with one iov
  489. */
  490. static int net_deliver_fn (
  491. poll_handle handle,
  492. int fd,
  493. int revents,
  494. void *data,
  495. unsigned int *prio)
  496. {
  497. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  498. struct msghdr msg_recv;
  499. struct iovec *iovec;
  500. struct security_header *security_header;
  501. struct sockaddr_storage system_from;
  502. struct totem_ip_address from_address;
  503. int bytes_received;
  504. int res = 0;
  505. unsigned char *msg_offset;
  506. unsigned int size_delv;
  507. *prio = UINT_MAX;
  508. if (instance->flushing == 1) {
  509. iovec = &instance->totemnet_iov_recv_flush;
  510. } else {
  511. iovec = &instance->totemnet_iov_recv;
  512. }
  513. /*
  514. * Receive datagram
  515. */
  516. msg_recv.msg_name = &system_from;
  517. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  518. msg_recv.msg_iov = iovec;
  519. msg_recv.msg_iovlen = 1;
  520. msg_recv.msg_control = 0;
  521. msg_recv.msg_controllen = 0;
  522. msg_recv.msg_flags = 0;
  523. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  524. if (bytes_received == -1) {
  525. return (0);
  526. } else {
  527. instance->stats_recv += bytes_received;
  528. }
  529. if ((instance->totem_config->secauth == 1) &&
  530. (bytes_received < sizeof (struct security_header))) {
  531. log_printf (instance->totemnet_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  532. return (0);
  533. }
  534. totemip_sockaddr_to_totemip_convert(&system_from, &from_address);
  535. security_header = (struct security_header *)iovec->iov_base;
  536. iovec->iov_len = bytes_received;
  537. if (instance->totem_config->secauth == 1) {
  538. /*
  539. * Authenticate and if authenticated, decrypt datagram
  540. */
  541. res = authenticate_and_decrypt (instance, iovec);
  542. if (res == -1) {
  543. log_printf (instance->totemnet_log_level_security,
  544. "Invalid packet data\n");
  545. iovec->iov_len = FRAME_SIZE_MAX;
  546. return 0;
  547. }
  548. msg_offset = iovec->iov_base +
  549. sizeof (struct security_header);
  550. size_delv = bytes_received - sizeof (struct security_header);
  551. } else {
  552. msg_offset = iovec->iov_base;
  553. size_delv = bytes_received;
  554. }
  555. /*
  556. * Handle incoming message
  557. */
  558. instance->totemnet_deliver_fn (
  559. instance->context,
  560. &from_address,
  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, UINT_MAX);
  672. poll_dispatch_add (
  673. instance->totemnet_poll_handle,
  674. instance->totemnet_sockets.token,
  675. POLLIN, instance, net_deliver_fn, UINT_MAX);
  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_send, 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. struct totem_ip_address *system_from,
  990. void *msg,
  991. int msg_len),
  992. void (*iface_change_fn) (
  993. void *context,
  994. struct totem_ip_address *iface_address))
  995. {
  996. struct totemnet_instance *instance;
  997. unsigned int res;
  998. res = hdb_handle_create (&totemnet_instance_database,
  999. sizeof (struct totemnet_instance), handle);
  1000. if (res != 0) {
  1001. goto error_exit;
  1002. }
  1003. res = hdb_handle_get (&totemnet_instance_database, *handle,
  1004. (void *)&instance);
  1005. if (res != 0) {
  1006. goto error_destroy;
  1007. }
  1008. totemnet_instance_initialize (instance);
  1009. instance->totem_config = totem_config;
  1010. /*
  1011. * Configure logging
  1012. */
  1013. instance->totemnet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1014. instance->totemnet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1015. instance->totemnet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1016. instance->totemnet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1017. instance->totemnet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  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. unsigned int prio;
  1093. res = hdb_handle_get (&totemnet_instance_database, handle,
  1094. (void *)&instance);
  1095. if (res != 0) {
  1096. res = ENOENT;
  1097. goto error_exit;
  1098. }
  1099. instance->flushing = 1;
  1100. do {
  1101. ufd.fd = instance->totemnet_sockets.mcast_recv;
  1102. ufd.events = POLLIN;
  1103. nfds = poll (&ufd, 1, 0);
  1104. if (nfds == 1 && ufd.revents & POLLIN) {
  1105. net_deliver_fn (0, instance->totemnet_sockets.mcast_recv,
  1106. ufd.revents, instance, &prio);
  1107. }
  1108. } while (nfds == 1);
  1109. instance->flushing = 0;
  1110. hdb_handle_put (&totemnet_instance_database, handle);
  1111. error_exit:
  1112. return (res);
  1113. }
  1114. int totemnet_send_flush (totemnet_handle handle)
  1115. {
  1116. struct totemnet_instance *instance;
  1117. int res = 0;
  1118. res = hdb_handle_get (&totemnet_instance_database, handle,
  1119. (void *)&instance);
  1120. if (res != 0) {
  1121. res = ENOENT;
  1122. goto error_exit;
  1123. }
  1124. worker_thread_group_wait (&instance->worker_thread_group);
  1125. hdb_handle_put (&totemnet_instance_database, handle);
  1126. error_exit:
  1127. return (res);
  1128. }
  1129. int totemnet_token_send (
  1130. totemnet_handle handle,
  1131. struct totem_ip_address *system_to,
  1132. struct iovec *iovec,
  1133. int iov_len)
  1134. {
  1135. struct totemnet_instance *instance;
  1136. int res = 0;
  1137. res = hdb_handle_get (&totemnet_instance_database, handle,
  1138. (void *)&instance);
  1139. if (res != 0) {
  1140. res = ENOENT;
  1141. goto error_exit;
  1142. }
  1143. ucast_sendmsg (instance, system_to, iovec, iov_len);
  1144. hdb_handle_put (&totemnet_instance_database, handle);
  1145. error_exit:
  1146. return (res);
  1147. }
  1148. int totemnet_mcast_flush_send (
  1149. totemnet_handle handle,
  1150. struct iovec *iovec,
  1151. unsigned int iov_len)
  1152. {
  1153. struct totemnet_instance *instance;
  1154. int res = 0;
  1155. res = hdb_handle_get (&totemnet_instance_database, handle,
  1156. (void *)&instance);
  1157. if (res != 0) {
  1158. res = ENOENT;
  1159. goto error_exit;
  1160. }
  1161. mcast_sendmsg (instance, iovec, iov_len);
  1162. hdb_handle_put (&totemnet_instance_database, handle);
  1163. error_exit:
  1164. return (res);
  1165. }
  1166. int totemnet_mcast_noflush_send (
  1167. totemnet_handle handle,
  1168. struct iovec *iovec,
  1169. unsigned int iov_len)
  1170. {
  1171. struct totemnet_instance *instance;
  1172. struct work_item work_item;
  1173. int res = 0;
  1174. res = hdb_handle_get (&totemnet_instance_database, handle,
  1175. (void *)&instance);
  1176. if (res != 0) {
  1177. res = ENOENT;
  1178. goto error_exit;
  1179. }
  1180. if (instance->totem_config->threads) {
  1181. memcpy (&work_item.iovec[0], iovec, iov_len * sizeof (struct iovec));
  1182. work_item.iov_len = iov_len;
  1183. work_item.instance = instance;
  1184. worker_thread_group_work_add (&instance->worker_thread_group,
  1185. &work_item);
  1186. } else {
  1187. mcast_sendmsg (instance, iovec, iov_len);
  1188. }
  1189. hdb_handle_put (&totemnet_instance_database, handle);
  1190. error_exit:
  1191. return (res);
  1192. }
  1193. extern int totemnet_iface_check (totemnet_handle handle)
  1194. {
  1195. struct totemnet_instance *instance;
  1196. int res = 0;
  1197. res = hdb_handle_get (&totemnet_instance_database, handle,
  1198. (void *)&instance);
  1199. if (res != 0) {
  1200. res = ENOENT;
  1201. goto error_exit;
  1202. }
  1203. timer_function_netif_check_timeout (instance);
  1204. hdb_handle_put (&totemnet_instance_database, handle);
  1205. error_exit:
  1206. return (res);
  1207. }
  1208. extern void totemnet_net_mtu_adjust (struct totem_config *totem_config)
  1209. {
  1210. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1211. if (totem_config->secauth == 1) {
  1212. totem_config->net_mtu -= sizeof (struct security_header) +
  1213. UDPIP_HEADER_SIZE;
  1214. } else {
  1215. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1216. }
  1217. }