totemnet.c 51 KB

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