totemnet.c 51 KB

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