totemnet.c 35 KB

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