totemnet.c 40 KB

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