totemnet.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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. res = bind (sockets->mcast, (struct sockaddr *)&sockaddr_in_test,
  853. sizeof (struct sockaddr_in));
  854. if (res == -1) {
  855. perror ("bind failed");
  856. return (-1);
  857. }
  858. /*
  859. * Setup unicast socket
  860. */
  861. sockets->token = socket (AF_INET, SOCK_DGRAM, 0);
  862. if (sockets->token == -1) {
  863. perror ("socket2");
  864. return (-1);
  865. }
  866. /*
  867. * Bind to unicast socket used for token send/receives
  868. * This has the side effect of binding to the correct interface
  869. */
  870. sockaddr_in_test.sin_family = AF_INET;
  871. sockaddr_in_test.sin_addr.s_addr = bound_to->sin_addr.s_addr;
  872. sockaddr_in_test.sin_port = sockaddr_mcast->sin_port;
  873. res = bind (sockets->token, (struct sockaddr *)&sockaddr_in_test,
  874. sizeof (struct sockaddr_in));
  875. if (res == -1) {
  876. perror ("bind2 failed");
  877. return (-1);
  878. }
  879. #ifdef CONFIG_USE_BROADCAST
  880. /* This config option doesn't work */
  881. {
  882. int on = 1;
  883. setsockopt (sockets->mcast, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof (on));
  884. }
  885. #else
  886. /*
  887. * Join group membership on socket
  888. */
  889. mreq.imr_multiaddr.s_addr = sockaddr_mcast->sin_addr.s_addr;
  890. mreq.imr_interface.s_addr = bound_to->sin_addr.s_addr;
  891. res = setsockopt (sockets->mcast, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  892. &mreq, sizeof (mreq));
  893. if (res == -1) {
  894. perror ("join multicast group failed");
  895. return (-1);
  896. }
  897. #endif
  898. /*
  899. * Turn on multicast loopback
  900. */
  901. flag = 1;
  902. res = setsockopt (sockets->mcast, IPPROTO_IP, IP_MULTICAST_LOOP,
  903. &flag, sizeof (flag));
  904. if (res == -1) {
  905. perror ("turn off loopback");
  906. return (-1);
  907. }
  908. return (0);
  909. }
  910. /*
  911. * Totem Network interface - also does encryption/decryption
  912. * depends on poll abstraction, POSIX, IPV4
  913. */
  914. /*
  915. * Create an instance
  916. */
  917. int totemnet_initialize (
  918. poll_handle poll_handle,
  919. totemnet_handle *handle,
  920. struct totem_config *totem_config,
  921. int interface_no,
  922. void *context,
  923. void (*deliver_fn) (
  924. void *context,
  925. struct in_addr *system_from,
  926. void *msg,
  927. int msg_len),
  928. void (*iface_change_fn) (
  929. void *context,
  930. struct sockaddr_in *iface_sockaddr_in))
  931. {
  932. SaAisErrorT error;
  933. struct totemnet_instance *instance;
  934. memset (&sockaddr_in_test, 0, sizeof (struct sockaddr_in));
  935. error = saHandleCreate (&totemnet_instance_database,
  936. sizeof (struct totemnet_instance), handle);
  937. if (error != SA_OK) {
  938. goto error_exit;
  939. }
  940. error = saHandleInstanceGet (&totemnet_instance_database, *handle,
  941. (void *)&instance);
  942. if (error != SA_OK) {
  943. goto error_destroy;
  944. }
  945. totemnet_instance_initialize (instance);
  946. instance->totem_config = totem_config;
  947. /*
  948. * Configure logging
  949. */
  950. instance->totemnet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  951. instance->totemnet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  952. instance->totemnet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  953. instance->totemnet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  954. instance->totemnet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  955. instance->totemnet_log_printf = totem_config->totem_logging_configuration.log_printf;
  956. memcpy (&instance->sockaddr_in_mcast, &totem_config->mcast_addr,
  957. sizeof (struct sockaddr_in));
  958. memcpy (&instance->in_addr_mcast, &totem_config->mcast_addr.sin_addr,
  959. sizeof (struct in_addr));
  960. /*
  961. * Initialize random number generator for later use to generate salt
  962. */
  963. memcpy (instance->totemnet_private_key, totem_config->private_key,
  964. totem_config->private_key_len);
  965. instance->totemnet_private_key_len = totem_config->private_key_len;
  966. rng_make_prng (128, PRNG_SOBER, &instance->totemnet_prng_state, NULL);
  967. /*
  968. * Initialize local variables for totemnet
  969. */
  970. memcpy (&instance->sockaddr_in_mcast, &totem_config->mcast_addr,
  971. sizeof (struct sockaddr_in));
  972. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  973. /*
  974. * If threaded send requested, initialize thread group data structure
  975. */
  976. if (totem_config->threads) {
  977. worker_thread_group_init (
  978. &instance->worker_thread_group,
  979. totem_config->threads, 128,
  980. sizeof (struct work_item),
  981. sizeof (struct totemnet_mcast_thread_state),
  982. totemnet_mcast_thread_state_constructor,
  983. totemnet_mcast_worker_fn);
  984. }
  985. instance->totemnet_interface = &totem_config->interfaces[interface_no];
  986. instance->totemnet_poll_handle = poll_handle;
  987. instance->context = context;
  988. instance->totemnet_deliver_fn = deliver_fn;
  989. instance->totemnet_iface_change_fn = iface_change_fn;
  990. instance->handle = *handle;
  991. rng_make_prng (128, PRNG_SOBER, &instance->totemnet_prng_state, NULL);
  992. netif_down_check (instance);
  993. error_exit:
  994. saHandleInstancePut (&totemnet_instance_database, *handle);
  995. return (0);
  996. error_destroy:
  997. saHandleDestroy (&totemnet_instance_database, *handle);
  998. return (-1);
  999. }
  1000. int totemnet_processor_count_set (
  1001. totemnet_handle handle,
  1002. int processor_count)
  1003. {
  1004. SaAisErrorT error;
  1005. struct totemnet_instance *instance;
  1006. int res = 0;
  1007. error = saHandleInstanceGet (&totemnet_instance_database, handle,
  1008. (void *)&instance);
  1009. if (error != SA_OK) {
  1010. res = ENOENT;
  1011. goto error_exit;
  1012. }
  1013. instance->my_memb_entries = processor_count;
  1014. saHandleInstancePut (&totemnet_instance_database, handle);
  1015. error_exit:
  1016. return (res);
  1017. }
  1018. int totemnet_recv_flush (totemnet_handle handle)
  1019. {
  1020. SaAisErrorT error;
  1021. struct totemnet_instance *instance;
  1022. struct pollfd ufd;
  1023. int nfds;
  1024. int res = 0;
  1025. unsigned int prio;
  1026. error = saHandleInstanceGet (&totemnet_instance_database, handle,
  1027. (void *)&instance);
  1028. if (error != SA_OK) {
  1029. res = ENOENT;
  1030. goto error_exit;
  1031. }
  1032. instance->flushing = 1;
  1033. do {
  1034. ufd.fd = instance->totemnet_sockets.mcast;
  1035. ufd.events = POLLIN;
  1036. nfds = poll (&ufd, 1, 0);
  1037. if (nfds == 1 && ufd.revents & POLLIN) {
  1038. net_deliver_fn (0, instance->totemnet_sockets.mcast,
  1039. ufd.revents, instance, &prio);
  1040. }
  1041. } while (nfds == 1);
  1042. instance->flushing = 0;
  1043. saHandleInstancePut (&totemnet_instance_database, handle);
  1044. error_exit:
  1045. return (res);
  1046. }
  1047. int totemnet_send_flush (totemnet_handle handle)
  1048. {
  1049. SaAisErrorT error;
  1050. struct totemnet_instance *instance;
  1051. int res = 0;
  1052. error = saHandleInstanceGet (&totemnet_instance_database, handle,
  1053. (void *)&instance);
  1054. if (error != SA_OK) {
  1055. res = ENOENT;
  1056. goto error_exit;
  1057. }
  1058. worker_thread_group_wait (&instance->worker_thread_group);
  1059. saHandleInstancePut (&totemnet_instance_database, handle);
  1060. error_exit:
  1061. return (res);
  1062. }
  1063. int totemnet_token_send (
  1064. totemnet_handle handle,
  1065. struct in_addr *system_to,
  1066. void *msg,
  1067. int msg_len)
  1068. {
  1069. SaAisErrorT error;
  1070. struct totemnet_instance *instance;
  1071. int res = 0;
  1072. error = saHandleInstanceGet (&totemnet_instance_database, handle,
  1073. (void *)&instance);
  1074. if (error != SA_OK) {
  1075. res = ENOENT;
  1076. goto error_exit;
  1077. }
  1078. totemnet_msg_send (instance, system_to, msg, msg_len);
  1079. saHandleInstancePut (&totemnet_instance_database, handle);
  1080. error_exit:
  1081. return (res);
  1082. }
  1083. int totemnet_mcast_flush_send (
  1084. totemnet_handle handle,
  1085. void *msg,
  1086. int msg_len)
  1087. {
  1088. SaAisErrorT error;
  1089. struct totemnet_instance *instance;
  1090. int res = 0;
  1091. error = saHandleInstanceGet (&totemnet_instance_database, handle,
  1092. (void *)&instance);
  1093. if (error != SA_OK) {
  1094. res = ENOENT;
  1095. goto error_exit;
  1096. }
  1097. totemnet_msg_send (instance, 0, msg, msg_len);
  1098. saHandleInstancePut (&totemnet_instance_database, handle);
  1099. error_exit:
  1100. return (res);
  1101. }
  1102. int totemnet_mcast_noflush_send (
  1103. totemnet_handle handle,
  1104. struct iovec *iovec,
  1105. int iov_len)
  1106. {
  1107. SaAisErrorT error;
  1108. struct totemnet_instance *instance;
  1109. struct work_item work_item;
  1110. int res = 0;
  1111. error = saHandleInstanceGet (&totemnet_instance_database, handle,
  1112. (void *)&instance);
  1113. if (error != SA_OK) {
  1114. res = ENOENT;
  1115. goto error_exit;
  1116. }
  1117. if (instance->totem_config->threads) {
  1118. memcpy (&work_item.iovec[0], iovec, iov_len * sizeof (struct iovec));
  1119. work_item.iov_len = iov_len;
  1120. work_item.instance = instance;
  1121. worker_thread_group_work_add (&instance->worker_thread_group,
  1122. &work_item);
  1123. } else {
  1124. totemnet_iovec_send (instance, iovec, iov_len);
  1125. }
  1126. saHandleInstancePut (&totemnet_instance_database, handle);
  1127. error_exit:
  1128. return (res);
  1129. }
  1130. extern int totemnet_iface_check (totemnet_handle handle)
  1131. {
  1132. SaAisErrorT error;
  1133. struct totemnet_instance *instance;
  1134. int res = 0;
  1135. error = saHandleInstanceGet (&totemnet_instance_database, handle,
  1136. (void *)&instance);
  1137. if (error != SA_OK) {
  1138. res = ENOENT;
  1139. goto error_exit;
  1140. }
  1141. timer_function_netif_check_timeout (instance);
  1142. saHandleInstancePut (&totemnet_instance_database, handle);
  1143. error_exit:
  1144. return (res);
  1145. }
  1146. extern void totemnet_net_mtu_adjust (struct totem_config *totem_config)
  1147. {
  1148. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1149. if (totem_config->secauth == 1) {
  1150. totem_config->net_mtu -= sizeof (struct security_header) +
  1151. UDPIP_HEADER_SIZE;
  1152. } else {
  1153. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1154. }
  1155. }