totemnet.c 52 KB

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