totemnet.c 40 KB

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