totemnet.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  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 <config.h>
  35. #include <assert.h>
  36. #include <pthread.h>
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <sys/un.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/param.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <sys/time.h>
  55. #include <sys/poll.h>
  56. #include <corosync/sq.h>
  57. #include <corosync/list.h>
  58. #include <corosync/hdb.h>
  59. #include <corosync/swab.h>
  60. #include <corosync/totem/coropoll.h>
  61. #include "totemnet.h"
  62. #include "wthread.h"
  63. #include "crypto.h"
  64. #ifdef HAVE_LIBNSS
  65. #include <nss.h>
  66. #include <pk11pub.h>
  67. #include <pkcs11.h>
  68. #include <prerror.h>
  69. #endif
  70. #ifndef MSG_NOSIGNAL
  71. #define MSG_NOSIGNAL 0
  72. #endif
  73. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  74. #define NETIF_STATE_REPORT_UP 1
  75. #define NETIF_STATE_REPORT_DOWN 2
  76. #define BIND_STATE_UNBOUND 0
  77. #define BIND_STATE_REGULAR 1
  78. #define BIND_STATE_LOOPBACK 2
  79. #define HMAC_HASH_SIZE 20
  80. struct security_header {
  81. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  82. unsigned char salt[16]; /* random number */
  83. char msg[0];
  84. } __attribute__((packed));
  85. struct totemnet_mcast_thread_state {
  86. unsigned char iobuf[FRAME_SIZE_MAX];
  87. prng_state prng_state;
  88. };
  89. struct totemnet_socket {
  90. int mcast_recv;
  91. int mcast_send;
  92. int token;
  93. };
  94. struct totemnet_instance {
  95. hmac_state totemnet_hmac_state;
  96. prng_state totemnet_prng_state;
  97. #ifdef HAVE_LIBNSS
  98. PK11SymKey *nss_sym_key;
  99. PK11SymKey *nss_sym_key_sign;
  100. #endif
  101. unsigned char totemnet_private_key[1024];
  102. unsigned int totemnet_private_key_len;
  103. hdb_handle_t totemnet_poll_handle;
  104. struct totem_interface *totem_interface;
  105. int netif_state_report;
  106. int netif_bind_state;
  107. struct worker_thread_group worker_thread_group;
  108. void *context;
  109. void (*totemnet_deliver_fn) (
  110. void *context,
  111. const void *msg,
  112. unsigned int msg_len);
  113. void (*totemnet_iface_change_fn) (
  114. void *context,
  115. const struct totem_ip_address *iface_address);
  116. /*
  117. * Function and data used to log messages
  118. */
  119. int totemnet_log_level_security;
  120. int totemnet_log_level_error;
  121. int totemnet_log_level_warning;
  122. int totemnet_log_level_notice;
  123. int totemnet_log_level_debug;
  124. int totemnet_subsys_id;
  125. void (*totemnet_log_printf) (int subsys,
  126. const char *function,
  127. const char *file,
  128. int line, unsigned int level,
  129. unsigned int tag,
  130. const char *format,
  131. ...)__attribute__((format(printf, 7, 8)));
  132. hdb_handle_t handle;
  133. char iov_buffer[FRAME_SIZE_MAX];
  134. char iov_buffer_flush[FRAME_SIZE_MAX];
  135. struct iovec totemnet_iov_recv;
  136. struct iovec totemnet_iov_recv_flush;
  137. struct totemnet_socket totemnet_sockets;
  138. struct totem_ip_address mcast_address;
  139. int stats_sent;
  140. int stats_recv;
  141. int stats_delv;
  142. int stats_remcasts;
  143. int stats_orf_token;
  144. struct timeval stats_tv_start;
  145. struct totem_ip_address my_id;
  146. int firstrun;
  147. poll_timer_handle timer_netif_check_timeout;
  148. unsigned int my_memb_entries;
  149. int flushing;
  150. struct totem_config *totem_config;
  151. struct totem_ip_address token_target;
  152. };
  153. struct work_item {
  154. const void *msg;
  155. unsigned int msg_len;
  156. struct totemnet_instance *instance;
  157. };
  158. static void netif_down_check (struct totemnet_instance *instance);
  159. static int totemnet_build_sockets (
  160. struct totemnet_instance *instance,
  161. struct totem_ip_address *bindnet_address,
  162. struct totem_ip_address *mcastaddress,
  163. struct totemnet_socket *sockets,
  164. struct totem_ip_address *bound_to);
  165. static struct totem_ip_address localhost;
  166. DECLARE_HDB_DATABASE (totemnet_instance_database,NULL);
  167. static void totemnet_instance_initialize (struct totemnet_instance *instance)
  168. {
  169. memset (instance, 0, sizeof (struct totemnet_instance));
  170. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  171. instance->totemnet_iov_recv.iov_base = instance->iov_buffer;
  172. instance->totemnet_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  173. instance->totemnet_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  174. instance->totemnet_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  175. /*
  176. * There is always atleast 1 processor
  177. */
  178. instance->my_memb_entries = 1;
  179. }
  180. #define log_printf(level, format, args...) \
  181. do { \
  182. instance->totemnet_log_printf (instance->totemnet_subsys_id, \
  183. __FUNCTION__, __FILE__, __LINE__, \
  184. level, 0, (const char *)format, ##args); \
  185. } while (0);
  186. static int authenticate_and_decrypt_sober (
  187. struct totemnet_instance *instance,
  188. struct iovec *iov,
  189. unsigned int iov_len)
  190. {
  191. unsigned char keys[48];
  192. struct security_header *header = iov[0].iov_base;
  193. prng_state keygen_prng_state;
  194. prng_state stream_prng_state;
  195. unsigned char *hmac_key = &keys[32];
  196. unsigned char *cipher_key = &keys[16];
  197. unsigned char *initial_vector = &keys[0];
  198. unsigned char digest_comparison[HMAC_HASH_SIZE];
  199. unsigned long len;
  200. /*
  201. * Generate MAC, CIPHER, IV keys from private key
  202. */
  203. memset (keys, 0, sizeof (keys));
  204. sober128_start (&keygen_prng_state);
  205. sober128_add_entropy (instance->totemnet_private_key,
  206. instance->totemnet_private_key_len, &keygen_prng_state);
  207. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  208. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  209. /*
  210. * Setup stream cipher
  211. */
  212. sober128_start (&stream_prng_state);
  213. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  214. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  215. /*
  216. * Authenticate contents of message
  217. */
  218. hmac_init (&instance->totemnet_hmac_state, DIGEST_SHA1, hmac_key, 16);
  219. hmac_process (&instance->totemnet_hmac_state,
  220. (unsigned char *)iov->iov_base + HMAC_HASH_SIZE,
  221. iov->iov_len - HMAC_HASH_SIZE);
  222. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  223. assert (HMAC_HASH_SIZE >= len);
  224. hmac_done (&instance->totemnet_hmac_state, digest_comparison, &len);
  225. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  226. return (-1);
  227. }
  228. /*
  229. * Decrypt the contents of the message with the cipher key
  230. */
  231. sober128_read ((unsigned char*)iov->iov_base +
  232. sizeof (struct security_header),
  233. iov->iov_len - sizeof (struct security_header),
  234. &stream_prng_state);
  235. return (0);
  236. }
  237. static void init_sober_crypto(
  238. struct totemnet_instance *instance)
  239. {
  240. log_printf(instance->totemnet_log_level_notice,
  241. "Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).\n");
  242. rng_make_prng (128, PRNG_SOBER, &instance->totemnet_prng_state, NULL);
  243. }
  244. #ifdef HAVE_LIBNSS
  245. static unsigned char *copy_from_iovec(
  246. const struct iovec *iov,
  247. unsigned int iov_len,
  248. size_t *buf_size)
  249. {
  250. int i;
  251. size_t bufptr;
  252. size_t buflen = 0;
  253. unsigned char *newbuf;
  254. for (i=0; i<iov_len; i++)
  255. buflen += iov[i].iov_len;
  256. newbuf = malloc(buflen);
  257. if (!newbuf)
  258. return NULL;
  259. bufptr=0;
  260. for (i=0; i<iov_len; i++) {
  261. memcpy(newbuf+bufptr, iov[i].iov_base, iov[i].iov_len);
  262. bufptr += iov[i].iov_len;
  263. }
  264. *buf_size = buflen;
  265. return newbuf;
  266. }
  267. static void copy_to_iovec(
  268. struct iovec *iov,
  269. unsigned int iov_len,
  270. const unsigned char *buf,
  271. size_t buf_size)
  272. {
  273. int i;
  274. size_t copylen;
  275. size_t bufptr = 0;
  276. bufptr=0;
  277. for (i=0; i<iov_len; i++) {
  278. copylen = iov[i].iov_len;
  279. if (bufptr + copylen > buf_size) {
  280. copylen = buf_size - bufptr;
  281. }
  282. memcpy(iov[i].iov_base, buf+bufptr, copylen);
  283. bufptr += copylen;
  284. if (iov[i].iov_len != copylen) {
  285. iov[i].iov_len = copylen;
  286. return;
  287. }
  288. }
  289. }
  290. static void init_nss_crypto(
  291. struct totemnet_instance *instance)
  292. {
  293. PK11SlotInfo* aes_slot = NULL;
  294. PK11SlotInfo* sha1_slot = NULL;
  295. SECItem key_item;
  296. SECStatus rv;
  297. log_printf(instance->totemnet_log_level_notice,
  298. "Initializing transmit/receive security: NSS AES128CBC/SHA1HMAC (mode 1).\n");
  299. rv = NSS_NoDB_Init(".");
  300. if (rv != SECSuccess)
  301. {
  302. log_printf(instance->totemnet_log_level_security, "NSS initialization failed (err %d)\n",
  303. PR_GetError());
  304. goto out;
  305. }
  306. aes_slot = PK11_GetBestSlot(instance->totem_config->crypto_crypt_type, NULL);
  307. if (aes_slot == NULL)
  308. {
  309. log_printf(instance->totemnet_log_level_security, "Unable to find security slot (err %d)\n",
  310. PR_GetError());
  311. goto out;
  312. }
  313. sha1_slot = PK11_GetBestSlot(CKM_SHA_1_HMAC, NULL);
  314. if (sha1_slot == NULL)
  315. {
  316. log_printf(instance->totemnet_log_level_security, "Unable to find security slot (err %d)\n",
  317. PR_GetError());
  318. goto out;
  319. }
  320. /*
  321. * Make the private key into a SymKey that we can use
  322. */
  323. key_item.type = siBuffer;
  324. key_item.data = instance->totem_config->private_key;
  325. key_item.len = 32; /* Use 128 bits */
  326. instance->nss_sym_key = PK11_ImportSymKey(aes_slot,
  327. instance->totem_config->crypto_crypt_type,
  328. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  329. &key_item, NULL);
  330. if (instance->nss_sym_key == NULL)
  331. {
  332. log_printf(instance->totemnet_log_level_security, "Failure to import key into NSS (err %d)\n",
  333. PR_GetError());
  334. goto out;
  335. }
  336. instance->nss_sym_key_sign = PK11_ImportSymKey(sha1_slot,
  337. CKM_SHA_1_HMAC,
  338. PK11_OriginUnwrap, CKA_SIGN,
  339. &key_item, NULL);
  340. if (instance->nss_sym_key_sign == NULL) {
  341. log_printf(instance->totemnet_log_level_security, "Failure to import key into NSS (err %d)\n",
  342. PR_GetError());
  343. goto out;
  344. }
  345. out:
  346. return;
  347. }
  348. static int encrypt_and_sign_nss (
  349. struct totemnet_instance *instance,
  350. unsigned char *buf,
  351. size_t *buf_len,
  352. const struct iovec *iovec,
  353. unsigned int iov_len)
  354. {
  355. PK11Context* enc_context = NULL;
  356. SECStatus rv1, rv2;
  357. int tmp1_outlen;
  358. unsigned int tmp2_outlen;
  359. unsigned char *inbuf;
  360. unsigned char *data;
  361. unsigned char *outdata;
  362. size_t datalen;
  363. SECItem no_params;
  364. SECItem iv_item;
  365. struct security_header *header;
  366. SECItem *nss_sec_param;
  367. unsigned char nss_iv_data[16];
  368. SECStatus rv;
  369. no_params.type = siBuffer;
  370. no_params.data = 0;
  371. no_params.len = 0;
  372. tmp1_outlen = tmp2_outlen = 0;
  373. inbuf = copy_from_iovec(iovec, iov_len, &datalen);
  374. if (!inbuf) {
  375. log_printf(instance->totemnet_log_level_security, "malloc error copying buffer from iovec\n");
  376. return -1;
  377. }
  378. data = inbuf + sizeof (struct security_header);
  379. datalen -= sizeof (struct security_header);
  380. outdata = buf + sizeof (struct security_header);
  381. header = (struct security_header *)buf;
  382. rv = PK11_GenerateRandom (
  383. nss_iv_data,
  384. sizeof (nss_iv_data));
  385. if (rv != SECSuccess) {
  386. log_printf(instance->totemnet_log_level_security,
  387. "Failure to generate a random number %d\n",
  388. PR_GetError());
  389. }
  390. memcpy(header->salt, nss_iv_data, sizeof(nss_iv_data));
  391. iv_item.type = siBuffer;
  392. iv_item.data = nss_iv_data;
  393. iv_item.len = sizeof (nss_iv_data);
  394. nss_sec_param = PK11_ParamFromIV (
  395. instance->totem_config->crypto_crypt_type,
  396. &iv_item);
  397. if (nss_sec_param == NULL) {
  398. log_printf(instance->totemnet_log_level_security,
  399. "Failure to set up PKCS11 param (err %d)\n",
  400. PR_GetError());
  401. goto out;
  402. }
  403. /*
  404. * Create cipher context for encryption
  405. */
  406. enc_context = PK11_CreateContextBySymKey (
  407. instance->totem_config->crypto_crypt_type,
  408. CKA_ENCRYPT,
  409. instance->nss_sym_key,
  410. nss_sec_param);
  411. if (!enc_context) {
  412. char err[1024];
  413. PR_GetErrorText(err);
  414. err[PR_GetErrorTextLength()] = 0;
  415. log_printf(instance->totemnet_log_level_security,
  416. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s\n",
  417. instance->totem_config->crypto_crypt_type,
  418. PR_GetError(), err);
  419. return -1;
  420. }
  421. rv1 = PK11_CipherOp(enc_context, outdata,
  422. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct security_header),
  423. data, datalen);
  424. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  425. FRAME_SIZE_MAX - tmp1_outlen);
  426. PK11_DestroyContext(enc_context, PR_TRUE);
  427. *buf_len = tmp1_outlen + tmp2_outlen;
  428. free(inbuf);
  429. // memcpy(&outdata[*buf_len], nss_iv_data, sizeof(nss_iv_data));
  430. if (rv1 != SECSuccess || rv2 != SECSuccess)
  431. goto out;
  432. /* Now do the digest */
  433. enc_context = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
  434. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  435. if (!enc_context) {
  436. char err[1024];
  437. PR_GetErrorText(err);
  438. err[PR_GetErrorTextLength()] = 0;
  439. log_printf(instance->totemnet_log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s\n",
  440. PR_GetError(), err);
  441. return -1;
  442. }
  443. PK11_DigestBegin(enc_context);
  444. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_len + 16);
  445. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  446. PK11_DestroyContext(enc_context, PR_TRUE);
  447. if (rv1 != SECSuccess || rv2 != SECSuccess)
  448. goto out;
  449. *buf_len = *buf_len + sizeof(struct security_header);
  450. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  451. return 0;
  452. out:
  453. return -1;
  454. }
  455. static int authenticate_and_decrypt_nss (
  456. struct totemnet_instance *instance,
  457. struct iovec *iov,
  458. unsigned int iov_len)
  459. {
  460. PK11Context* enc_context = NULL;
  461. SECStatus rv1, rv2;
  462. int tmp1_outlen;
  463. unsigned int tmp2_outlen;
  464. unsigned char outbuf[FRAME_SIZE_MAX];
  465. unsigned char digest[HMAC_HASH_SIZE];
  466. unsigned char *outdata;
  467. int result_len;
  468. unsigned char *data;
  469. unsigned char *inbuf;
  470. size_t datalen;
  471. struct security_header *header = iov[0].iov_base;
  472. SECItem no_params;
  473. SECItem ivdata;
  474. no_params.type = siBuffer;
  475. no_params.data = 0;
  476. no_params.len = 0;
  477. tmp1_outlen = tmp2_outlen = 0;
  478. if (iov_len > 1) {
  479. inbuf = copy_from_iovec(iov, iov_len, &datalen);
  480. if (!inbuf) {
  481. log_printf(instance->totemnet_log_level_security, "malloc error copying buffer from iovec\n");
  482. return -1;
  483. }
  484. }
  485. else {
  486. inbuf = iov[0].iov_base;
  487. datalen = iov[0].iov_len;
  488. }
  489. data = inbuf + sizeof (struct security_header) - 16;
  490. datalen = datalen - sizeof (struct security_header) + 16;
  491. outdata = outbuf + sizeof (struct security_header);
  492. /* Check the digest */
  493. enc_context = PK11_CreateContextBySymKey (
  494. CKM_SHA_1_HMAC, CKA_SIGN,
  495. instance->nss_sym_key_sign,
  496. &no_params);
  497. if (!enc_context) {
  498. char err[1024];
  499. PR_GetErrorText(err);
  500. err[PR_GetErrorTextLength()] = 0;
  501. log_printf(instance->totemnet_log_level_security, "PK11_CreateContext failed (check digest) err %d: %s\n",
  502. PR_GetError(), err);
  503. return -1;
  504. }
  505. PK11_DigestBegin(enc_context);
  506. rv1 = PK11_DigestOp(enc_context, data, datalen);
  507. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  508. PK11_DestroyContext(enc_context, PR_TRUE);
  509. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  510. log_printf(instance->totemnet_log_level_security, "Digest check failed\n");
  511. return -1;
  512. }
  513. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  514. log_printf(instance->totemnet_log_level_error, "Digest does not match\n");
  515. return -1;
  516. }
  517. /*
  518. * Get rid of salt
  519. */
  520. data += 16;
  521. datalen -= 16;
  522. /* Create cipher context for decryption */
  523. ivdata.type = siBuffer;
  524. ivdata.data = header->salt;
  525. ivdata.len = sizeof(header->salt);
  526. enc_context = PK11_CreateContextBySymKey(
  527. instance->totem_config->crypto_crypt_type,
  528. CKA_DECRYPT,
  529. instance->nss_sym_key, &ivdata);
  530. if (!enc_context) {
  531. log_printf(instance->totemnet_log_level_security,
  532. "PK11_CreateContext (decrypt) failed (err %d)\n",
  533. PR_GetError());
  534. return -1;
  535. }
  536. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  537. sizeof(outbuf) - sizeof (struct security_header),
  538. data, datalen);
  539. if (rv1 != SECSuccess) {
  540. log_printf(instance->totemnet_log_level_security,
  541. "PK11_CipherOp (decrypt) failed (err %d)\n",
  542. PR_GetError());
  543. }
  544. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  545. sizeof(outbuf) - tmp1_outlen);
  546. PK11_DestroyContext(enc_context, PR_TRUE);
  547. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct security_header);
  548. /* Copy it back to the buffer */
  549. copy_to_iovec(iov, iov_len, outbuf, result_len);
  550. if (iov_len > 1)
  551. free(inbuf);
  552. if (rv1 != SECSuccess || rv2 != SECSuccess)
  553. return -1;
  554. return 0;
  555. }
  556. #endif
  557. static int encrypt_and_sign_sober (
  558. struct totemnet_instance *instance,
  559. unsigned char *buf,
  560. size_t *buf_len,
  561. const struct iovec *iovec,
  562. unsigned int iov_len)
  563. {
  564. int i;
  565. unsigned char *addr;
  566. unsigned char keys[48];
  567. struct security_header *header;
  568. unsigned char *hmac_key = &keys[32];
  569. unsigned char *cipher_key = &keys[16];
  570. unsigned char *initial_vector = &keys[0];
  571. unsigned long len;
  572. size_t outlen = 0;
  573. hmac_state hmac_st;
  574. prng_state keygen_prng_state;
  575. prng_state stream_prng_state;
  576. prng_state *prng_state_in = &instance->totemnet_prng_state;
  577. header = (struct security_header *)buf;
  578. addr = buf + sizeof (struct security_header);
  579. memset (keys, 0, sizeof (keys));
  580. memset (header->salt, 0, sizeof (header->salt));
  581. /*
  582. * Generate MAC, CIPHER, IV keys from private key
  583. */
  584. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  585. sober128_start (&keygen_prng_state);
  586. sober128_add_entropy (instance->totemnet_private_key,
  587. instance->totemnet_private_key_len,
  588. &keygen_prng_state);
  589. sober128_add_entropy (header->salt, sizeof (header->salt),
  590. &keygen_prng_state);
  591. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  592. /*
  593. * Setup stream cipher
  594. */
  595. sober128_start (&stream_prng_state);
  596. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  597. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  598. outlen = sizeof (struct security_header);
  599. /*
  600. * Copy remainder of message, then encrypt it
  601. */
  602. for (i = 1; i < iov_len; i++) {
  603. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  604. addr += iovec[i].iov_len;
  605. outlen += iovec[i].iov_len;
  606. }
  607. /*
  608. * Encrypt message by XORing stream cipher data
  609. */
  610. sober128_read (buf + sizeof (struct security_header),
  611. outlen - sizeof (struct security_header),
  612. &stream_prng_state);
  613. memset (&hmac_st, 0, sizeof (hmac_st));
  614. /*
  615. * Sign the contents of the message with the hmac key and store signature in message
  616. */
  617. hmac_init (&hmac_st, DIGEST_SHA1, hmac_key, 16);
  618. hmac_process (&hmac_st,
  619. buf + HMAC_HASH_SIZE,
  620. outlen - HMAC_HASH_SIZE);
  621. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  622. hmac_done (&hmac_st, header->hash_digest, &len);
  623. *buf_len = outlen;
  624. return 0;
  625. }
  626. static int encrypt_and_sign_worker (
  627. struct totemnet_instance *instance,
  628. unsigned char *buf,
  629. size_t *buf_len,
  630. const struct iovec *iovec,
  631. unsigned int iov_len)
  632. {
  633. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_SOBER ||
  634. instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  635. return encrypt_and_sign_sober(instance, buf, buf_len, iovec, iov_len);
  636. #ifdef HAVE_LIBNSS
  637. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_NSS)
  638. return encrypt_and_sign_nss(instance, buf, buf_len, iovec, iov_len);
  639. #endif
  640. return -1;
  641. }
  642. static int authenticate_and_decrypt (
  643. struct totemnet_instance *instance,
  644. struct iovec *iov,
  645. unsigned int iov_len)
  646. {
  647. unsigned char type;
  648. unsigned char *endbuf = iov[iov_len-1].iov_base;
  649. int res = -1;
  650. /*
  651. * Get the encryption type and remove it from the buffer
  652. */
  653. type = endbuf[iov[iov_len-1].iov_len-1];
  654. iov[iov_len-1].iov_len -= 1;
  655. if (type == TOTEM_CRYPTO_SOBER)
  656. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  657. /*
  658. * Only try higher crypto options if NEW has been requested
  659. */
  660. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  661. #ifdef HAVE_LIBNSS
  662. if (type == TOTEM_CRYPTO_NSS)
  663. res = authenticate_and_decrypt_nss(instance, iov, iov_len);
  664. #endif
  665. }
  666. /*
  667. * If it failed, then try decrypting the whole packet as it might be
  668. * from aisexec
  669. */
  670. if (res == -1) {
  671. iov[iov_len-1].iov_len += 1;
  672. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  673. }
  674. return res;
  675. }
  676. static void init_crypto(
  677. struct totemnet_instance *instance)
  678. {
  679. /*
  680. * If we are expecting NEW crypto type then initialise all available
  681. * crypto options. For OLD then we only need SOBER128.
  682. */
  683. init_sober_crypto(instance);
  684. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  685. return;
  686. #ifdef HAVE_LIBNSS
  687. init_nss_crypto(instance);
  688. #endif
  689. }
  690. int totemnet_crypto_set (hdb_handle_t handle,
  691. unsigned int type)
  692. {
  693. struct totemnet_instance *instance;
  694. int res = 0;
  695. res = hdb_handle_get (&totemnet_instance_database, handle,
  696. (void *)&instance);
  697. if (res != 0) {
  698. res = ENOENT;
  699. goto error_exit;
  700. }
  701. /*
  702. * Can't set crypto type if OLD is selected
  703. */
  704. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD) {
  705. res = -1;
  706. } else {
  707. /*
  708. * Validate crypto algorithm
  709. */
  710. switch (type) {
  711. case TOTEM_CRYPTO_SOBER:
  712. log_printf(instance->totemnet_log_level_security,
  713. "Transmit security set to: libtomcrypt SOBER128/SHA1HMAC (mode 0)");
  714. break;
  715. case TOTEM_CRYPTO_NSS:
  716. log_printf(instance->totemnet_log_level_security,
  717. "Transmit security set to: NSS AES128CBC/SHA1HMAC (mode 1)");
  718. break;
  719. default:
  720. res = -1;
  721. break;
  722. }
  723. }
  724. hdb_handle_put (&totemnet_instance_database, handle);
  725. error_exit:
  726. return res;
  727. }
  728. static inline void ucast_sendmsg (
  729. struct totemnet_instance *instance,
  730. struct totem_ip_address *system_to,
  731. const void *msg,
  732. unsigned int msg_len)
  733. {
  734. struct msghdr msg_ucast;
  735. int res = 0;
  736. size_t buf_len;
  737. unsigned char sheader[sizeof (struct security_header)];
  738. unsigned char encrypt_data[FRAME_SIZE_MAX];
  739. struct iovec iovec_encrypt[2];
  740. const struct iovec *iovec_sendmsg;
  741. struct sockaddr_storage sockaddr;
  742. struct iovec iovec;
  743. unsigned int iov_len;
  744. int addrlen;
  745. if (instance->totem_config->secauth == 1) {
  746. iovec_encrypt[0].iov_base = sheader;
  747. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  748. iovec_encrypt[1].iov_base = (void *)msg;
  749. iovec_encrypt[1].iov_len = msg_len;
  750. /*
  751. * Encrypt and digest the message
  752. */
  753. encrypt_and_sign_worker (
  754. instance,
  755. encrypt_data,
  756. &buf_len,
  757. iovec_encrypt,
  758. 2);
  759. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  760. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  761. }
  762. else {
  763. encrypt_data[buf_len++] = 0;
  764. }
  765. iovec_encrypt[0].iov_base = encrypt_data;
  766. iovec_encrypt[0].iov_len = buf_len;
  767. iovec_sendmsg = &iovec_encrypt[0];
  768. iov_len = 1;
  769. } else {
  770. iovec.iov_base = (void *)msg;
  771. iovec.iov_len = msg_len;
  772. iovec_sendmsg = &iovec;
  773. iov_len = 1;
  774. }
  775. /*
  776. * Build unicast message
  777. */
  778. totemip_totemip_to_sockaddr_convert(system_to,
  779. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  780. msg_ucast.msg_name = &sockaddr;
  781. msg_ucast.msg_namelen = addrlen;
  782. msg_ucast.msg_iov = (void *) iovec_sendmsg;
  783. msg_ucast.msg_iovlen = iov_len;
  784. #if !defined(COROSYNC_SOLARIS)
  785. msg_ucast.msg_control = 0;
  786. msg_ucast.msg_controllen = 0;
  787. msg_ucast.msg_flags = 0;
  788. #else
  789. msg_ucast.msg_accrights = NULL;
  790. msg_ucast.msg_accrightslen = 0;
  791. #endif
  792. /*
  793. * Transmit multicast message
  794. * An error here is recovered by totemsrp
  795. */
  796. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_ucast,
  797. MSG_NOSIGNAL);
  798. }
  799. static inline void mcast_sendmsg (
  800. struct totemnet_instance *instance,
  801. const void *msg,
  802. unsigned int msg_len)
  803. {
  804. struct msghdr msg_mcast;
  805. int res = 0;
  806. size_t buf_len;
  807. unsigned char sheader[sizeof (struct security_header)];
  808. unsigned char encrypt_data[FRAME_SIZE_MAX];
  809. struct iovec iovec_encrypt[2];
  810. struct iovec iovec;
  811. const struct iovec *iovec_sendmsg;
  812. struct sockaddr_storage sockaddr;
  813. unsigned int iov_len;
  814. int addrlen;
  815. if (instance->totem_config->secauth == 1) {
  816. iovec_encrypt[0].iov_base = sheader;
  817. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  818. iovec_encrypt[1].iov_base = (void *)msg;
  819. iovec_encrypt[1].iov_len = msg_len;
  820. /*
  821. * Encrypt and digest the message
  822. */
  823. encrypt_and_sign_worker (
  824. instance,
  825. encrypt_data,
  826. &buf_len,
  827. iovec_encrypt,
  828. 2);
  829. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  830. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  831. }
  832. else {
  833. encrypt_data[buf_len++] = 0;
  834. }
  835. iovec_encrypt[0].iov_base = encrypt_data;
  836. iovec_encrypt[0].iov_len = buf_len;
  837. iovec_sendmsg = &iovec_encrypt[0];
  838. iov_len = 1;
  839. } else {
  840. iovec.iov_base = (void *)msg;
  841. iovec.iov_len = msg_len;
  842. iovec_sendmsg = &iovec;
  843. iov_len = 1;
  844. }
  845. /*
  846. * Build multicast message
  847. */
  848. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  849. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  850. msg_mcast.msg_name = &sockaddr;
  851. msg_mcast.msg_namelen = addrlen;
  852. msg_mcast.msg_iov = (void *) iovec_sendmsg;
  853. msg_mcast.msg_iovlen = iov_len;
  854. #if !defined(COROSYNC_SOLARIS)
  855. msg_mcast.msg_control = 0;
  856. msg_mcast.msg_controllen = 0;
  857. msg_mcast.msg_flags = 0;
  858. #else
  859. msg_mcast.msg_accrights = NULL;
  860. msg_mcast.msg_accrightslen = 0;
  861. #endif
  862. /*
  863. * Transmit multicast message
  864. * An error here is recovered by totemsrp
  865. */
  866. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  867. MSG_NOSIGNAL);
  868. }
  869. static void totemnet_mcast_thread_state_constructor (
  870. void *totemnet_mcast_thread_state_in)
  871. {
  872. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  873. (struct totemnet_mcast_thread_state *)totemnet_mcast_thread_state_in;
  874. memset (totemnet_mcast_thread_state, 0,
  875. sizeof (totemnet_mcast_thread_state));
  876. rng_make_prng (128, PRNG_SOBER,
  877. &totemnet_mcast_thread_state->prng_state, NULL);
  878. }
  879. static void totemnet_mcast_worker_fn (void *thread_state, void *work_item_in)
  880. {
  881. struct work_item *work_item = (struct work_item *)work_item_in;
  882. struct totemnet_mcast_thread_state *totemnet_mcast_thread_state =
  883. (struct totemnet_mcast_thread_state *)thread_state;
  884. struct totemnet_instance *instance = work_item->instance;
  885. struct msghdr msg_mcast;
  886. unsigned char sheader[sizeof (struct security_header)];
  887. int res = 0;
  888. size_t buf_len;
  889. struct iovec iovec_enc[2];
  890. struct iovec iovec;
  891. struct sockaddr_storage sockaddr;
  892. int addrlen;
  893. if (instance->totem_config->secauth == 1) {
  894. iovec_enc[0].iov_base = sheader;
  895. iovec_enc[0].iov_len = sizeof (struct security_header);
  896. iovec_enc[1].iov_base = (void *)work_item->msg;
  897. iovec_enc[1].iov_len = work_item->msg_len;
  898. /*
  899. * Encrypt and digest the message
  900. */
  901. encrypt_and_sign_worker (
  902. instance,
  903. totemnet_mcast_thread_state->iobuf,
  904. &buf_len,
  905. iovec_enc, 2);
  906. iovec.iov_base = totemnet_mcast_thread_state->iobuf;
  907. iovec.iov_len = buf_len;
  908. } else {
  909. iovec.iov_base = (void *)work_item->msg;
  910. iovec.iov_len = work_item->msg_len;
  911. }
  912. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  913. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  914. msg_mcast.msg_name = &sockaddr;
  915. msg_mcast.msg_namelen = addrlen;
  916. msg_mcast.msg_iov = &iovec;
  917. msg_mcast.msg_iovlen = 1;
  918. #if !defined(COROSYNC_SOLARIS)
  919. msg_mcast.msg_control = 0;
  920. msg_mcast.msg_controllen = 0;
  921. msg_mcast.msg_flags = 0;
  922. #else
  923. msg_mcast.msg_accrights = NULL;
  924. msg_mcast.msg_accrightslen = 0;
  925. #endif
  926. /*
  927. * Transmit multicast message
  928. * An error here is recovered by totemnet
  929. */
  930. res = sendmsg (instance->totemnet_sockets.mcast_send, &msg_mcast,
  931. MSG_NOSIGNAL);
  932. }
  933. int totemnet_finalize (
  934. hdb_handle_t handle)
  935. {
  936. struct totemnet_instance *instance;
  937. int res = 0;
  938. res = hdb_handle_get (&totemnet_instance_database, handle,
  939. (void *)&instance);
  940. if (res != 0) {
  941. res = ENOENT;
  942. goto error_exit;
  943. }
  944. worker_thread_group_exit (&instance->worker_thread_group);
  945. if (instance->totemnet_sockets.mcast_recv > 0) {
  946. close (instance->totemnet_sockets.mcast_recv);
  947. poll_dispatch_delete (instance->totemnet_poll_handle,
  948. instance->totemnet_sockets.mcast_recv);
  949. }
  950. if (instance->totemnet_sockets.mcast_send > 0) {
  951. close (instance->totemnet_sockets.mcast_send);
  952. }
  953. if (instance->totemnet_sockets.token > 0) {
  954. close (instance->totemnet_sockets.token);
  955. poll_dispatch_delete (instance->totemnet_poll_handle,
  956. instance->totemnet_sockets.token);
  957. }
  958. hdb_handle_put (&totemnet_instance_database, handle);
  959. error_exit:
  960. return (res);
  961. }
  962. /*
  963. * Only designed to work with a message with one iov
  964. */
  965. static int net_deliver_fn (
  966. hdb_handle_t handle,
  967. int fd,
  968. int revents,
  969. void *data)
  970. {
  971. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  972. struct msghdr msg_recv;
  973. struct iovec *iovec;
  974. struct security_header *security_header;
  975. struct sockaddr_storage system_from;
  976. int bytes_received;
  977. int res = 0;
  978. unsigned char *msg_offset;
  979. unsigned int size_delv;
  980. if (instance->flushing == 1) {
  981. iovec = &instance->totemnet_iov_recv_flush;
  982. } else {
  983. iovec = &instance->totemnet_iov_recv;
  984. }
  985. /*
  986. * Receive datagram
  987. */
  988. msg_recv.msg_name = &system_from;
  989. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  990. msg_recv.msg_iov = iovec;
  991. msg_recv.msg_iovlen = 1;
  992. #if !defined(COROSYNC_SOLARIS)
  993. msg_recv.msg_control = 0;
  994. msg_recv.msg_controllen = 0;
  995. msg_recv.msg_flags = 0;
  996. #else
  997. msg_recv.msg_accrights = NULL;
  998. msg_recv.msg_accrightslen = 0;
  999. #endif
  1000. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1001. if (bytes_received == -1) {
  1002. return (0);
  1003. } else {
  1004. instance->stats_recv += bytes_received;
  1005. }
  1006. if ((instance->totem_config->secauth == 1) &&
  1007. (bytes_received < sizeof (struct security_header))) {
  1008. log_printf (instance->totemnet_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  1009. return (0);
  1010. }
  1011. security_header = (struct security_header *)iovec->iov_base;
  1012. iovec->iov_len = bytes_received;
  1013. if (instance->totem_config->secauth == 1) {
  1014. /*
  1015. * Authenticate and if authenticated, decrypt datagram
  1016. */
  1017. res = authenticate_and_decrypt (instance, iovec, 1);
  1018. if (res == -1) {
  1019. log_printf (instance->totemnet_log_level_security, "Received message has invalid digest... ignoring.\n");
  1020. log_printf (instance->totemnet_log_level_security,
  1021. "Invalid packet data\n");
  1022. iovec->iov_len = FRAME_SIZE_MAX;
  1023. return 0;
  1024. }
  1025. msg_offset = (unsigned char *)iovec->iov_base +
  1026. sizeof (struct security_header);
  1027. size_delv = bytes_received - sizeof (struct security_header);
  1028. } else {
  1029. msg_offset = iovec->iov_base;
  1030. size_delv = bytes_received;
  1031. }
  1032. /*
  1033. * Handle incoming message
  1034. */
  1035. instance->totemnet_deliver_fn (
  1036. instance->context,
  1037. msg_offset,
  1038. size_delv);
  1039. iovec->iov_len = FRAME_SIZE_MAX;
  1040. return (0);
  1041. }
  1042. static int netif_determine (
  1043. struct totemnet_instance *instance,
  1044. struct totem_ip_address *bindnet,
  1045. struct totem_ip_address *bound_to,
  1046. int *interface_up,
  1047. int *interface_num)
  1048. {
  1049. int res;
  1050. res = totemip_iface_check (bindnet, bound_to,
  1051. interface_up, interface_num,
  1052. instance->totem_config->clear_node_high_bit);
  1053. return (res);
  1054. }
  1055. /*
  1056. * If the interface is up, the sockets for totem are built. If the interface is down
  1057. * this function is requeued in the timer list to retry building the sockets later.
  1058. */
  1059. static void timer_function_netif_check_timeout (
  1060. void *data)
  1061. {
  1062. struct totemnet_instance *instance = (struct totemnet_instance *)data;
  1063. int res;
  1064. int interface_up;
  1065. int interface_num;
  1066. struct totem_ip_address *bind_address;
  1067. /*
  1068. * Build sockets for every interface
  1069. */
  1070. netif_determine (instance,
  1071. &instance->totem_interface->bindnet,
  1072. &instance->totem_interface->boundto,
  1073. &interface_up, &interface_num);
  1074. /*
  1075. * If the network interface isn't back up and we are already
  1076. * in loopback mode, add timer to check again and return
  1077. */
  1078. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  1079. interface_up == 0) ||
  1080. (instance->my_memb_entries == 1 &&
  1081. instance->netif_bind_state == BIND_STATE_REGULAR &&
  1082. interface_up == 1)) {
  1083. poll_timer_add (instance->totemnet_poll_handle,
  1084. instance->totem_config->downcheck_timeout,
  1085. (void *)instance,
  1086. timer_function_netif_check_timeout,
  1087. &instance->timer_netif_check_timeout);
  1088. /*
  1089. * Add a timer to check for a downed regular interface
  1090. */
  1091. return;
  1092. }
  1093. if (instance->totemnet_sockets.mcast_recv > 0) {
  1094. close (instance->totemnet_sockets.mcast_recv);
  1095. poll_dispatch_delete (instance->totemnet_poll_handle,
  1096. instance->totemnet_sockets.mcast_recv);
  1097. }
  1098. if (instance->totemnet_sockets.mcast_send > 0) {
  1099. close (instance->totemnet_sockets.mcast_send);
  1100. }
  1101. if (instance->totemnet_sockets.token > 0) {
  1102. close (instance->totemnet_sockets.token);
  1103. poll_dispatch_delete (instance->totemnet_poll_handle,
  1104. instance->totemnet_sockets.token);
  1105. }
  1106. if (interface_up == 0) {
  1107. /*
  1108. * Interface is not up
  1109. */
  1110. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1111. bind_address = &localhost;
  1112. /*
  1113. * Add a timer to retry building interfaces and request memb_gather_enter
  1114. */
  1115. poll_timer_add (instance->totemnet_poll_handle,
  1116. instance->totem_config->downcheck_timeout,
  1117. (void *)instance,
  1118. timer_function_netif_check_timeout,
  1119. &instance->timer_netif_check_timeout);
  1120. } else {
  1121. /*
  1122. * Interface is up
  1123. */
  1124. instance->netif_bind_state = BIND_STATE_REGULAR;
  1125. bind_address = &instance->totem_interface->bindnet;
  1126. }
  1127. /*
  1128. * Create and bind the multicast and unicast sockets
  1129. */
  1130. res = totemnet_build_sockets (instance,
  1131. &instance->mcast_address,
  1132. bind_address,
  1133. &instance->totemnet_sockets,
  1134. &instance->totem_interface->boundto);
  1135. poll_dispatch_add (
  1136. instance->totemnet_poll_handle,
  1137. instance->totemnet_sockets.mcast_recv,
  1138. POLLIN, instance, net_deliver_fn);
  1139. poll_dispatch_add (
  1140. instance->totemnet_poll_handle,
  1141. instance->totemnet_sockets.token,
  1142. POLLIN, instance, net_deliver_fn);
  1143. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  1144. /*
  1145. * This reports changes in the interface to the user and totemsrp
  1146. */
  1147. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1148. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  1149. log_printf (instance->totemnet_log_level_notice,
  1150. "The network interface [%s] is now up.\n",
  1151. totemip_print (&instance->totem_interface->boundto));
  1152. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  1153. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  1154. }
  1155. /*
  1156. * Add a timer to check for interface going down in single membership
  1157. */
  1158. if (instance->my_memb_entries == 1) {
  1159. poll_timer_add (instance->totemnet_poll_handle,
  1160. instance->totem_config->downcheck_timeout,
  1161. (void *)instance,
  1162. timer_function_netif_check_timeout,
  1163. &instance->timer_netif_check_timeout);
  1164. }
  1165. } else {
  1166. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  1167. log_printf (instance->totemnet_log_level_notice,
  1168. "The network interface is down.\n");
  1169. instance->totemnet_iface_change_fn (instance->context, &instance->my_id);
  1170. }
  1171. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  1172. }
  1173. }
  1174. /*
  1175. * Check if an interface is down and reconfigure
  1176. * totemnet waiting for it to come back up
  1177. */
  1178. static void netif_down_check (struct totemnet_instance *instance)
  1179. {
  1180. timer_function_netif_check_timeout (instance);
  1181. }
  1182. /* Set the socket priority to INTERACTIVE to ensure
  1183. that our messages don't get queued behind anything else */
  1184. static void totemnet_traffic_control_set(struct totemnet_instance *instance, int sock)
  1185. {
  1186. #ifdef SO_PRIORITY
  1187. int prio = 6; /* TC_PRIO_INTERACTIVE */
  1188. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int)))
  1189. log_printf (instance->totemnet_log_level_warning, "Could not set traffic priority. (%s)\n", strerror (errno));
  1190. #endif
  1191. }
  1192. static int totemnet_build_sockets_ip (
  1193. struct totemnet_instance *instance,
  1194. struct totem_ip_address *mcast_address,
  1195. struct totem_ip_address *bindnet_address,
  1196. struct totemnet_socket *sockets,
  1197. struct totem_ip_address *bound_to,
  1198. int interface_num)
  1199. {
  1200. struct sockaddr_storage sockaddr;
  1201. struct ipv6_mreq mreq6;
  1202. struct ip_mreq mreq;
  1203. struct sockaddr_storage mcast_ss, boundto_ss;
  1204. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  1205. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  1206. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  1207. unsigned int sendbuf_size;
  1208. unsigned int recvbuf_size;
  1209. unsigned int optlen = sizeof (sendbuf_size);
  1210. int addrlen;
  1211. int res;
  1212. int flag;
  1213. /*
  1214. * Create multicast recv socket
  1215. */
  1216. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1217. if (sockets->mcast_recv == -1) {
  1218. perror ("socket");
  1219. return (-1);
  1220. }
  1221. totemip_nosigpipe (sockets->mcast_recv);
  1222. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  1223. if (res == -1) {
  1224. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  1225. return (-1);
  1226. }
  1227. /*
  1228. * Force reuse
  1229. */
  1230. flag = 1;
  1231. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1232. perror("setsockopt reuseaddr");
  1233. return (-1);
  1234. }
  1235. /*
  1236. * Bind to multicast socket used for multicast receives
  1237. */
  1238. totemip_totemip_to_sockaddr_convert(mcast_address,
  1239. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1240. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  1241. if (res == -1) {
  1242. perror ("bind mcast recv socket failed");
  1243. return (-1);
  1244. }
  1245. /*
  1246. * Setup mcast send socket
  1247. */
  1248. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1249. if (sockets->mcast_send == -1) {
  1250. perror ("socket");
  1251. return (-1);
  1252. }
  1253. totemip_nosigpipe (sockets->mcast_send);
  1254. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  1255. if (res == -1) {
  1256. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  1257. return (-1);
  1258. }
  1259. /*
  1260. * Force reuse
  1261. */
  1262. flag = 1;
  1263. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1264. perror("setsockopt reuseaddr");
  1265. return (-1);
  1266. }
  1267. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  1268. &sockaddr, &addrlen);
  1269. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  1270. if (res == -1) {
  1271. perror ("bind mcast send socket failed");
  1272. return (-1);
  1273. }
  1274. /*
  1275. * Setup unicast socket
  1276. */
  1277. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1278. if (sockets->token == -1) {
  1279. perror ("socket2");
  1280. return (-1);
  1281. }
  1282. totemip_nosigpipe (sockets->token);
  1283. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  1284. if (res == -1) {
  1285. log_printf (instance->totemnet_log_level_warning, "Could not set non-blocking operation on token socket: %s\n", strerror (errno));
  1286. return (-1);
  1287. }
  1288. /*
  1289. * Force reuse
  1290. */
  1291. flag = 1;
  1292. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1293. perror("setsockopt reuseaddr");
  1294. return (-1);
  1295. }
  1296. /*
  1297. * Bind to unicast socket used for token send/receives
  1298. * This has the side effect of binding to the correct interface
  1299. */
  1300. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1301. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  1302. if (res == -1) {
  1303. perror ("bind token socket failed");
  1304. return (-1);
  1305. }
  1306. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1307. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1308. /*
  1309. * Set buffer sizes to avoid overruns
  1310. */
  1311. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  1312. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  1313. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  1314. if (res == 0) {
  1315. log_printf (instance->totemnet_log_level_notice,
  1316. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  1317. }
  1318. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  1319. if (res == 0) {
  1320. log_printf (instance->totemnet_log_level_notice,
  1321. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  1322. }
  1323. /*
  1324. * Join group membership on socket
  1325. */
  1326. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  1327. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  1328. if (instance->totem_config->broadcast_use == 1) {
  1329. unsigned int broadcast = 1;
  1330. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  1331. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1332. perror("setting broadcast option");
  1333. return (-1);
  1334. }
  1335. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  1336. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1337. perror("setting broadcast option");
  1338. return (-1);
  1339. }
  1340. } else {
  1341. switch (bindnet_address->family) {
  1342. case AF_INET:
  1343. memset(&mreq, 0, sizeof(mreq));
  1344. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  1345. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  1346. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  1347. &mreq, sizeof (mreq));
  1348. if (res == -1) {
  1349. perror ("join ipv4 multicast group failed");
  1350. return (-1);
  1351. }
  1352. break;
  1353. case AF_INET6:
  1354. memset(&mreq6, 0, sizeof(mreq6));
  1355. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  1356. mreq6.ipv6mr_interface = interface_num;
  1357. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  1358. &mreq6, sizeof (mreq6));
  1359. if (res == -1) {
  1360. perror ("join ipv6 multicast group failed");
  1361. return (-1);
  1362. }
  1363. break;
  1364. }
  1365. }
  1366. /*
  1367. * Turn on multicast loopback
  1368. */
  1369. flag = 1;
  1370. switch ( bindnet_address->family ) {
  1371. case AF_INET:
  1372. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  1373. &flag, sizeof (flag));
  1374. break;
  1375. case AF_INET6:
  1376. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  1377. &flag, sizeof (flag));
  1378. }
  1379. if (res == -1) {
  1380. perror ("turn off loopback");
  1381. return (-1);
  1382. }
  1383. /*
  1384. * Set multicast packets TTL
  1385. */
  1386. if ( bindnet_address->family == AF_INET6 )
  1387. {
  1388. flag = 255;
  1389. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  1390. &flag, sizeof (flag));
  1391. if (res == -1) {
  1392. perror ("setp mcast hops");
  1393. return (-1);
  1394. }
  1395. }
  1396. /*
  1397. * Bind to a specific interface for multicast send and receive
  1398. */
  1399. switch ( bindnet_address->family ) {
  1400. case AF_INET:
  1401. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  1402. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1403. perror ("cannot select interface");
  1404. return (-1);
  1405. }
  1406. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  1407. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1408. perror ("cannot select interface");
  1409. return (-1);
  1410. }
  1411. break;
  1412. case AF_INET6:
  1413. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1414. &interface_num, sizeof (interface_num)) < 0) {
  1415. perror ("cannot select interface");
  1416. return (-1);
  1417. }
  1418. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1419. &interface_num, sizeof (interface_num)) < 0) {
  1420. perror ("cannot select interface");
  1421. return (-1);
  1422. }
  1423. break;
  1424. }
  1425. return 0;
  1426. }
  1427. static int totemnet_build_sockets (
  1428. struct totemnet_instance *instance,
  1429. struct totem_ip_address *mcast_address,
  1430. struct totem_ip_address *bindnet_address,
  1431. struct totemnet_socket *sockets,
  1432. struct totem_ip_address *bound_to)
  1433. {
  1434. int interface_num;
  1435. int interface_up;
  1436. int res;
  1437. /*
  1438. * Determine the ip address bound to and the interface name
  1439. */
  1440. res = netif_determine (instance,
  1441. bindnet_address,
  1442. bound_to,
  1443. &interface_up,
  1444. &interface_num);
  1445. if (res == -1) {
  1446. return (-1);
  1447. }
  1448. totemip_copy(&instance->my_id, bound_to);
  1449. res = totemnet_build_sockets_ip (instance, mcast_address,
  1450. bindnet_address, sockets, bound_to, interface_num);
  1451. /* We only send out of the token socket */
  1452. totemnet_traffic_control_set(instance, sockets->token);
  1453. return res;
  1454. }
  1455. /*
  1456. * Totem Network interface - also does encryption/decryption
  1457. * depends on poll abstraction, POSIX, IPV4
  1458. */
  1459. /*
  1460. * Create an instance
  1461. */
  1462. int totemnet_initialize (
  1463. hdb_handle_t poll_handle,
  1464. hdb_handle_t *handle,
  1465. struct totem_config *totem_config,
  1466. int interface_no,
  1467. void *context,
  1468. void (*deliver_fn) (
  1469. void *context,
  1470. const void *msg,
  1471. unsigned int msg_len),
  1472. void (*iface_change_fn) (
  1473. void *context,
  1474. const struct totem_ip_address *iface_address))
  1475. {
  1476. struct totemnet_instance *instance;
  1477. unsigned int res;
  1478. res = hdb_handle_create (&totemnet_instance_database,
  1479. sizeof (struct totemnet_instance), handle);
  1480. if (res != 0) {
  1481. goto error_exit;
  1482. }
  1483. res = hdb_handle_get (&totemnet_instance_database, *handle,
  1484. (void *)&instance);
  1485. if (res != 0) {
  1486. goto error_destroy;
  1487. }
  1488. totemnet_instance_initialize (instance);
  1489. instance->totem_config = totem_config;
  1490. /*
  1491. * Configure logging
  1492. */
  1493. instance->totemnet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1494. instance->totemnet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1495. instance->totemnet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1496. instance->totemnet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1497. instance->totemnet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1498. instance->totemnet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1499. instance->totemnet_log_printf = totem_config->totem_logging_configuration.log_printf;
  1500. /*
  1501. * Initialize random number generator for later use to generate salt
  1502. */
  1503. memcpy (instance->totemnet_private_key, totem_config->private_key,
  1504. totem_config->private_key_len);
  1505. instance->totemnet_private_key_len = totem_config->private_key_len;
  1506. init_crypto(instance);
  1507. /*
  1508. * Initialize local variables for totemnet
  1509. */
  1510. instance->totem_interface = &totem_config->interfaces[interface_no];
  1511. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1512. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1513. /*
  1514. * If threaded send requested, initialize thread group data structure
  1515. */
  1516. if (totem_config->threads) {
  1517. worker_thread_group_init (
  1518. &instance->worker_thread_group,
  1519. totem_config->threads, 128,
  1520. sizeof (struct work_item),
  1521. sizeof (struct totemnet_mcast_thread_state),
  1522. totemnet_mcast_thread_state_constructor,
  1523. totemnet_mcast_worker_fn);
  1524. }
  1525. instance->totemnet_poll_handle = poll_handle;
  1526. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1527. instance->context = context;
  1528. instance->totemnet_deliver_fn = deliver_fn;
  1529. instance->totemnet_iface_change_fn = iface_change_fn;
  1530. instance->handle = *handle;
  1531. totemip_localhost (instance->mcast_address.family, &localhost);
  1532. netif_down_check (instance);
  1533. error_exit:
  1534. hdb_handle_put (&totemnet_instance_database, *handle);
  1535. return (0);
  1536. error_destroy:
  1537. hdb_handle_destroy (&totemnet_instance_database, *handle);
  1538. return (-1);
  1539. }
  1540. int totemnet_processor_count_set (
  1541. hdb_handle_t handle,
  1542. int processor_count)
  1543. {
  1544. struct totemnet_instance *instance;
  1545. int res = 0;
  1546. res = hdb_handle_get (&totemnet_instance_database, handle,
  1547. (void *)&instance);
  1548. if (res != 0) {
  1549. res = ENOENT;
  1550. goto error_exit;
  1551. }
  1552. instance->my_memb_entries = processor_count;
  1553. poll_timer_delete (instance->totemnet_poll_handle,
  1554. instance->timer_netif_check_timeout);
  1555. if (processor_count == 1) {
  1556. poll_timer_add (instance->totemnet_poll_handle,
  1557. instance->totem_config->downcheck_timeout,
  1558. (void *)instance,
  1559. timer_function_netif_check_timeout,
  1560. &instance->timer_netif_check_timeout);
  1561. }
  1562. hdb_handle_put (&totemnet_instance_database, handle);
  1563. error_exit:
  1564. return (res);
  1565. }
  1566. int totemnet_recv_flush (hdb_handle_t handle)
  1567. {
  1568. struct totemnet_instance *instance;
  1569. struct pollfd ufd;
  1570. int nfds;
  1571. int res = 0;
  1572. res = hdb_handle_get (&totemnet_instance_database, handle,
  1573. (void *)&instance);
  1574. if (res != 0) {
  1575. res = ENOENT;
  1576. goto error_exit;
  1577. }
  1578. instance->flushing = 1;
  1579. do {
  1580. ufd.fd = instance->totemnet_sockets.mcast_recv;
  1581. ufd.events = POLLIN;
  1582. nfds = poll (&ufd, 1, 0);
  1583. if (nfds == 1 && ufd.revents & POLLIN) {
  1584. net_deliver_fn (0, instance->totemnet_sockets.mcast_recv,
  1585. ufd.revents, instance);
  1586. }
  1587. } while (nfds == 1);
  1588. instance->flushing = 0;
  1589. hdb_handle_put (&totemnet_instance_database, handle);
  1590. error_exit:
  1591. return (res);
  1592. }
  1593. int totemnet_send_flush (hdb_handle_t handle)
  1594. {
  1595. struct totemnet_instance *instance;
  1596. int res = 0;
  1597. res = hdb_handle_get (&totemnet_instance_database, handle,
  1598. (void *)&instance);
  1599. if (res != 0) {
  1600. res = ENOENT;
  1601. goto error_exit;
  1602. }
  1603. worker_thread_group_wait (&instance->worker_thread_group);
  1604. hdb_handle_put (&totemnet_instance_database, handle);
  1605. error_exit:
  1606. return (res);
  1607. }
  1608. int totemnet_token_send (
  1609. hdb_handle_t handle,
  1610. const void *msg,
  1611. unsigned int msg_len)
  1612. {
  1613. struct totemnet_instance *instance;
  1614. int res = 0;
  1615. res = hdb_handle_get (&totemnet_instance_database, handle,
  1616. (void *)&instance);
  1617. if (res != 0) {
  1618. res = ENOENT;
  1619. goto error_exit;
  1620. }
  1621. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1622. hdb_handle_put (&totemnet_instance_database, handle);
  1623. error_exit:
  1624. return (res);
  1625. }
  1626. int totemnet_mcast_flush_send (
  1627. hdb_handle_t handle,
  1628. const void *msg,
  1629. unsigned int msg_len)
  1630. {
  1631. struct totemnet_instance *instance;
  1632. int res = 0;
  1633. res = hdb_handle_get (&totemnet_instance_database, handle,
  1634. (void *)&instance);
  1635. if (res != 0) {
  1636. res = ENOENT;
  1637. goto error_exit;
  1638. }
  1639. mcast_sendmsg (instance, msg, msg_len);
  1640. hdb_handle_put (&totemnet_instance_database, handle);
  1641. error_exit:
  1642. return (res);
  1643. }
  1644. int totemnet_mcast_noflush_send (
  1645. hdb_handle_t handle,
  1646. const void *msg,
  1647. unsigned int msg_len)
  1648. {
  1649. struct totemnet_instance *instance;
  1650. struct work_item work_item;
  1651. int res = 0;
  1652. res = hdb_handle_get (&totemnet_instance_database, handle,
  1653. (void *)&instance);
  1654. if (res != 0) {
  1655. res = ENOENT;
  1656. goto error_exit;
  1657. }
  1658. if (instance->totem_config->threads) {
  1659. work_item.msg = msg;
  1660. work_item.msg_len = msg_len;
  1661. work_item.instance = instance;
  1662. worker_thread_group_work_add (&instance->worker_thread_group,
  1663. &work_item);
  1664. } else {
  1665. mcast_sendmsg (instance, msg, msg_len);
  1666. }
  1667. hdb_handle_put (&totemnet_instance_database, handle);
  1668. error_exit:
  1669. return (res);
  1670. }
  1671. extern int totemnet_iface_check (hdb_handle_t handle)
  1672. {
  1673. struct totemnet_instance *instance;
  1674. int res = 0;
  1675. res = hdb_handle_get (&totemnet_instance_database, handle,
  1676. (void *)&instance);
  1677. if (res != 0) {
  1678. res = ENOENT;
  1679. goto error_exit;
  1680. }
  1681. timer_function_netif_check_timeout (instance);
  1682. hdb_handle_put (&totemnet_instance_database, handle);
  1683. error_exit:
  1684. return (res);
  1685. }
  1686. extern void totemnet_net_mtu_adjust (struct totem_config *totem_config)
  1687. {
  1688. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1689. if (totem_config->secauth == 1) {
  1690. totem_config->net_mtu -= sizeof (struct security_header) +
  1691. UDPIP_HEADER_SIZE;
  1692. } else {
  1693. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1694. }
  1695. }
  1696. const char *totemnet_iface_print (hdb_handle_t handle) {
  1697. struct totemnet_instance *instance;
  1698. int res = 0;
  1699. const char *ret_char;
  1700. res = hdb_handle_get (&totemnet_instance_database, handle,
  1701. (void *)&instance);
  1702. if (res != 0) {
  1703. ret_char = "Invalid totemnet handle";
  1704. goto error_exit;
  1705. }
  1706. ret_char = totemip_print (&instance->my_id);
  1707. hdb_handle_put (&totemnet_instance_database, handle);
  1708. error_exit:
  1709. return (ret_char);
  1710. }
  1711. int totemnet_iface_get (
  1712. hdb_handle_t handle,
  1713. struct totem_ip_address *addr)
  1714. {
  1715. struct totemnet_instance *instance;
  1716. unsigned int res;
  1717. res = hdb_handle_get (&totemnet_instance_database, handle,
  1718. (void *)&instance);
  1719. if (res != 0) {
  1720. goto error_exit;
  1721. }
  1722. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1723. hdb_handle_put (&totemnet_instance_database, handle);
  1724. error_exit:
  1725. return (res);
  1726. }
  1727. int totemnet_token_target_set (
  1728. hdb_handle_t handle,
  1729. const struct totem_ip_address *token_target)
  1730. {
  1731. struct totemnet_instance *instance;
  1732. unsigned int res;
  1733. res = hdb_handle_get (&totemnet_instance_database, handle,
  1734. (void *)&instance);
  1735. if (res != 0) {
  1736. goto error_exit;
  1737. }
  1738. memcpy (&instance->token_target, token_target,
  1739. sizeof (struct totem_ip_address));
  1740. hdb_handle_put (&totemnet_instance_database, handle);
  1741. error_exit:
  1742. return (res);
  1743. }