4
0

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