totemudp.c 52 KB

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