totemudp.c 52 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 <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. free (inbuf);
  407. return (-1);
  408. }
  409. /*
  410. * Create cipher context for encryption
  411. */
  412. enc_context = PK11_CreateContextBySymKey (
  413. instance->totem_config->crypto_crypt_type,
  414. CKA_ENCRYPT,
  415. instance->nss_sym_key,
  416. nss_sec_param);
  417. if (!enc_context) {
  418. char err[1024];
  419. PR_GetErrorText(err);
  420. err[PR_GetErrorTextLength()] = 0;
  421. log_printf(instance->totemudp_log_level_security,
  422. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s\n",
  423. instance->totem_config->crypto_crypt_type,
  424. PR_GetError(), err);
  425. free(inbuf);
  426. return -1;
  427. }
  428. rv1 = PK11_CipherOp(enc_context, outdata,
  429. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct security_header),
  430. data, datalen);
  431. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  432. FRAME_SIZE_MAX - tmp1_outlen);
  433. PK11_DestroyContext(enc_context, PR_TRUE);
  434. *buf_len = tmp1_outlen + tmp2_outlen;
  435. free(inbuf);
  436. // memcpy(&outdata[*buf_len], nss_iv_data, sizeof(nss_iv_data));
  437. if (rv1 != SECSuccess || rv2 != SECSuccess)
  438. goto out;
  439. /* Now do the digest */
  440. enc_context = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
  441. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  442. if (!enc_context) {
  443. char err[1024];
  444. PR_GetErrorText(err);
  445. err[PR_GetErrorTextLength()] = 0;
  446. log_printf(instance->totemudp_log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s\n",
  447. PR_GetError(), err);
  448. return -1;
  449. }
  450. PK11_DigestBegin(enc_context);
  451. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_len + 16);
  452. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  453. PK11_DestroyContext(enc_context, PR_TRUE);
  454. if (rv1 != SECSuccess || rv2 != SECSuccess)
  455. goto out;
  456. *buf_len = *buf_len + sizeof(struct security_header);
  457. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  458. return 0;
  459. out:
  460. return -1;
  461. }
  462. static int authenticate_and_decrypt_nss (
  463. struct totemudp_instance *instance,
  464. struct iovec *iov,
  465. unsigned int iov_len)
  466. {
  467. PK11Context* enc_context = NULL;
  468. SECStatus rv1, rv2;
  469. int tmp1_outlen;
  470. unsigned int tmp2_outlen;
  471. unsigned char outbuf[FRAME_SIZE_MAX];
  472. unsigned char digest[HMAC_HASH_SIZE];
  473. unsigned char *outdata;
  474. int result_len;
  475. unsigned char *data;
  476. unsigned char *inbuf;
  477. size_t datalen;
  478. struct security_header *header = (struct security_header *)iov[0].iov_base;
  479. SECItem no_params;
  480. SECItem ivdata;
  481. no_params.type = siBuffer;
  482. no_params.data = 0;
  483. no_params.len = 0;
  484. tmp1_outlen = tmp2_outlen = 0;
  485. if (iov_len > 1) {
  486. inbuf = copy_from_iovec(iov, iov_len, &datalen);
  487. if (!inbuf) {
  488. log_printf(instance->totemudp_log_level_security, "malloc error copying buffer from iovec\n");
  489. return -1;
  490. }
  491. }
  492. else {
  493. inbuf = (unsigned char *)iov[0].iov_base;
  494. datalen = iov[0].iov_len;
  495. }
  496. data = inbuf + sizeof (struct security_header) - 16;
  497. datalen = datalen - sizeof (struct security_header) + 16;
  498. outdata = outbuf + sizeof (struct security_header);
  499. /* Check the digest */
  500. enc_context = PK11_CreateContextBySymKey (
  501. CKM_SHA_1_HMAC, CKA_SIGN,
  502. instance->nss_sym_key_sign,
  503. &no_params);
  504. if (!enc_context) {
  505. char err[1024];
  506. PR_GetErrorText(err);
  507. err[PR_GetErrorTextLength()] = 0;
  508. log_printf(instance->totemudp_log_level_security, "PK11_CreateContext failed (check digest) err %d: %s\n",
  509. PR_GetError(), err);
  510. free (inbuf);
  511. return -1;
  512. }
  513. PK11_DigestBegin(enc_context);
  514. rv1 = PK11_DigestOp(enc_context, data, datalen);
  515. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  516. PK11_DestroyContext(enc_context, PR_TRUE);
  517. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  518. log_printf(instance->totemudp_log_level_security, "Digest check failed\n");
  519. return -1;
  520. }
  521. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  522. log_printf(instance->totemudp_log_level_error, "Digest does not match\n");
  523. return -1;
  524. }
  525. /*
  526. * Get rid of salt
  527. */
  528. data += 16;
  529. datalen -= 16;
  530. /* Create cipher context for decryption */
  531. ivdata.type = siBuffer;
  532. ivdata.data = header->salt;
  533. ivdata.len = sizeof(header->salt);
  534. enc_context = PK11_CreateContextBySymKey(
  535. instance->totem_config->crypto_crypt_type,
  536. CKA_DECRYPT,
  537. instance->nss_sym_key, &ivdata);
  538. if (!enc_context) {
  539. log_printf(instance->totemudp_log_level_security,
  540. "PK11_CreateContext (decrypt) failed (err %d)\n",
  541. PR_GetError());
  542. return -1;
  543. }
  544. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  545. sizeof(outbuf) - sizeof (struct security_header),
  546. data, datalen);
  547. if (rv1 != SECSuccess) {
  548. log_printf(instance->totemudp_log_level_security,
  549. "PK11_CipherOp (decrypt) failed (err %d)\n",
  550. PR_GetError());
  551. }
  552. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  553. sizeof(outbuf) - tmp1_outlen);
  554. PK11_DestroyContext(enc_context, PR_TRUE);
  555. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct security_header);
  556. /* Copy it back to the buffer */
  557. copy_to_iovec(iov, iov_len, outbuf, result_len);
  558. if (iov_len > 1)
  559. free(inbuf);
  560. if (rv1 != SECSuccess || rv2 != SECSuccess)
  561. return -1;
  562. return 0;
  563. }
  564. #endif
  565. static int encrypt_and_sign_sober (
  566. struct totemudp_instance *instance,
  567. unsigned char *buf,
  568. size_t *buf_len,
  569. const struct iovec *iovec,
  570. unsigned int iov_len)
  571. {
  572. int i;
  573. unsigned char *addr;
  574. unsigned char keys[48];
  575. struct security_header *header;
  576. unsigned char *hmac_key = &keys[32];
  577. unsigned char *cipher_key = &keys[16];
  578. unsigned char *initial_vector = &keys[0];
  579. unsigned long len;
  580. size_t outlen = 0;
  581. hmac_state hmac_st;
  582. prng_state keygen_prng_state;
  583. prng_state stream_prng_state;
  584. prng_state *prng_state_in = &instance->totemudp_prng_state;
  585. header = (struct security_header *)buf;
  586. addr = buf + sizeof (struct security_header);
  587. memset (keys, 0, sizeof (keys));
  588. memset (header->salt, 0, sizeof (header->salt));
  589. /*
  590. * Generate MAC, CIPHER, IV keys from private key
  591. */
  592. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  593. sober128_start (&keygen_prng_state);
  594. sober128_add_entropy (instance->totemudp_private_key,
  595. instance->totemudp_private_key_len,
  596. &keygen_prng_state);
  597. sober128_add_entropy (header->salt, sizeof (header->salt),
  598. &keygen_prng_state);
  599. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  600. /*
  601. * Setup stream cipher
  602. */
  603. sober128_start (&stream_prng_state);
  604. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  605. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  606. outlen = sizeof (struct security_header);
  607. /*
  608. * Copy remainder of message, then encrypt it
  609. */
  610. for (i = 1; i < iov_len; i++) {
  611. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  612. addr += iovec[i].iov_len;
  613. outlen += iovec[i].iov_len;
  614. }
  615. /*
  616. * Encrypt message by XORing stream cipher data
  617. */
  618. sober128_read (buf + sizeof (struct security_header),
  619. outlen - sizeof (struct security_header),
  620. &stream_prng_state);
  621. memset (&hmac_st, 0, sizeof (hmac_st));
  622. /*
  623. * Sign the contents of the message with the hmac key and store signature in message
  624. */
  625. hmac_init (&hmac_st, DIGEST_SHA1, hmac_key, 16);
  626. hmac_process (&hmac_st,
  627. buf + HMAC_HASH_SIZE,
  628. outlen - HMAC_HASH_SIZE);
  629. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  630. hmac_done (&hmac_st, header->hash_digest, &len);
  631. *buf_len = outlen;
  632. return 0;
  633. }
  634. static int encrypt_and_sign_worker (
  635. struct totemudp_instance *instance,
  636. unsigned char *buf,
  637. size_t *buf_len,
  638. const struct iovec *iovec,
  639. unsigned int iov_len)
  640. {
  641. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_SOBER ||
  642. instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  643. return encrypt_and_sign_sober(instance, buf, buf_len, iovec, iov_len);
  644. #ifdef HAVE_LIBNSS
  645. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_NSS)
  646. return encrypt_and_sign_nss(instance, buf, buf_len, iovec, iov_len);
  647. #endif
  648. return -1;
  649. }
  650. static int authenticate_and_decrypt (
  651. struct totemudp_instance *instance,
  652. struct iovec *iov,
  653. unsigned int iov_len)
  654. {
  655. unsigned char type;
  656. unsigned char *endbuf = (unsigned char *)iov[iov_len-1].iov_base;
  657. int res = -1;
  658. /*
  659. * Get the encryption type and remove it from the buffer
  660. */
  661. type = endbuf[iov[iov_len-1].iov_len-1];
  662. iov[iov_len-1].iov_len -= 1;
  663. if (type == TOTEM_CRYPTO_SOBER)
  664. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  665. /*
  666. * Only try higher crypto options if NEW has been requested
  667. */
  668. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  669. #ifdef HAVE_LIBNSS
  670. if (type == TOTEM_CRYPTO_NSS)
  671. res = authenticate_and_decrypt_nss(instance, iov, iov_len);
  672. #endif
  673. }
  674. /*
  675. * If it failed, then try decrypting the whole packet as it might be
  676. * from aisexec
  677. */
  678. if (res == -1) {
  679. iov[iov_len-1].iov_len += 1;
  680. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  681. }
  682. return res;
  683. }
  684. static void init_crypto(
  685. struct totemudp_instance *instance)
  686. {
  687. /*
  688. * If we are expecting NEW crypto type then initialise all available
  689. * crypto options. For OLD then we only need SOBER128.
  690. */
  691. init_sober_crypto(instance);
  692. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  693. return;
  694. #ifdef HAVE_LIBNSS
  695. init_nss_crypto(instance);
  696. #endif
  697. }
  698. int totemudp_crypto_set (
  699. void *udp_context,
  700. unsigned int type)
  701. {
  702. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  703. int res = 0;
  704. /*
  705. * Can't set crypto type if OLD is selected
  706. */
  707. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD) {
  708. res = -1;
  709. } else {
  710. /*
  711. * Validate crypto algorithm
  712. */
  713. switch (type) {
  714. case TOTEM_CRYPTO_SOBER:
  715. log_printf(instance->totemudp_log_level_security,
  716. "Transmit security set to: libtomcrypt SOBER128/SHA1HMAC (mode 0)");
  717. break;
  718. case TOTEM_CRYPTO_NSS:
  719. log_printf(instance->totemudp_log_level_security,
  720. "Transmit security set to: NSS AES128CBC/SHA1HMAC (mode 1)");
  721. break;
  722. default:
  723. res = -1;
  724. break;
  725. }
  726. }
  727. return (res);
  728. }
  729. static inline void ucast_sendmsg (
  730. struct totemudp_instance *instance,
  731. struct totem_ip_address *system_to,
  732. const void *msg,
  733. unsigned int msg_len)
  734. {
  735. struct msghdr msg_ucast;
  736. int res = 0;
  737. size_t buf_len;
  738. unsigned char sheader[sizeof (struct security_header)];
  739. unsigned char encrypt_data[FRAME_SIZE_MAX];
  740. struct iovec iovec_encrypt[2];
  741. const struct iovec *iovec_sendmsg;
  742. struct sockaddr_storage sockaddr;
  743. struct iovec iovec;
  744. unsigned int iov_len;
  745. int addrlen;
  746. if (instance->totem_config->secauth == 1) {
  747. iovec_encrypt[0].iov_base = (void *)sheader;
  748. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  749. iovec_encrypt[1].iov_base = (void *)msg;
  750. iovec_encrypt[1].iov_len = msg_len;
  751. /*
  752. * Encrypt and digest the message
  753. */
  754. encrypt_and_sign_worker (
  755. instance,
  756. encrypt_data,
  757. &buf_len,
  758. iovec_encrypt,
  759. 2);
  760. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  761. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  762. }
  763. else {
  764. encrypt_data[buf_len++] = 0;
  765. }
  766. iovec_encrypt[0].iov_base = (void *)encrypt_data;
  767. iovec_encrypt[0].iov_len = buf_len;
  768. iovec_sendmsg = &iovec_encrypt[0];
  769. iov_len = 1;
  770. } else {
  771. iovec.iov_base = (void *)msg;
  772. iovec.iov_len = msg_len;
  773. iovec_sendmsg = &iovec;
  774. iov_len = 1;
  775. }
  776. /*
  777. * Build unicast message
  778. */
  779. totemip_totemip_to_sockaddr_convert(system_to,
  780. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  781. msg_ucast.msg_name = &sockaddr;
  782. msg_ucast.msg_namelen = addrlen;
  783. msg_ucast.msg_iov = (void *) iovec_sendmsg;
  784. msg_ucast.msg_iovlen = iov_len;
  785. #if !defined(COROSYNC_SOLARIS)
  786. msg_ucast.msg_control = 0;
  787. msg_ucast.msg_controllen = 0;
  788. msg_ucast.msg_flags = 0;
  789. #else
  790. msg_ucast.msg_accrights = NULL;
  791. msg_ucast.msg_accrightslen = 0;
  792. #endif
  793. /*
  794. * Transmit unicast message
  795. * An error here is recovered by totemsrp
  796. */
  797. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
  798. MSG_NOSIGNAL);
  799. if (res < 0) {
  800. char error_str[100];
  801. strerror_r (errno, error_str, sizeof(error_str));
  802. log_printf (instance->totemudp_log_level_debug,
  803. "sendmsg(ucast) failed (non-critical): %s\n", error_str);
  804. }
  805. }
  806. static inline void mcast_sendmsg (
  807. struct totemudp_instance *instance,
  808. const void *msg,
  809. unsigned int msg_len)
  810. {
  811. struct msghdr msg_mcast;
  812. int res = 0;
  813. size_t buf_len;
  814. unsigned char sheader[sizeof (struct security_header)];
  815. unsigned char encrypt_data[FRAME_SIZE_MAX];
  816. struct iovec iovec_encrypt[2];
  817. struct iovec iovec;
  818. const struct iovec *iovec_sendmsg;
  819. struct sockaddr_storage sockaddr;
  820. unsigned int iov_len;
  821. int addrlen;
  822. if (instance->totem_config->secauth == 1) {
  823. iovec_encrypt[0].iov_base = (void *)sheader;
  824. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  825. iovec_encrypt[1].iov_base = (void *)msg;
  826. iovec_encrypt[1].iov_len = msg_len;
  827. /*
  828. * Encrypt and digest the message
  829. */
  830. encrypt_and_sign_worker (
  831. instance,
  832. encrypt_data,
  833. &buf_len,
  834. iovec_encrypt,
  835. 2);
  836. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  837. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  838. }
  839. else {
  840. encrypt_data[buf_len++] = 0;
  841. }
  842. iovec_encrypt[0].iov_base = (void *)encrypt_data;
  843. iovec_encrypt[0].iov_len = buf_len;
  844. iovec_sendmsg = &iovec_encrypt[0];
  845. iov_len = 1;
  846. } else {
  847. iovec.iov_base = (void *)msg;
  848. iovec.iov_len = msg_len;
  849. iovec_sendmsg = &iovec;
  850. iov_len = 1;
  851. }
  852. /*
  853. * Build multicast message
  854. */
  855. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  856. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  857. msg_mcast.msg_name = &sockaddr;
  858. msg_mcast.msg_namelen = addrlen;
  859. msg_mcast.msg_iov = (void *) iovec_sendmsg;
  860. msg_mcast.msg_iovlen = iov_len;
  861. #if !defined(COROSYNC_SOLARIS)
  862. msg_mcast.msg_control = 0;
  863. msg_mcast.msg_controllen = 0;
  864. msg_mcast.msg_flags = 0;
  865. #else
  866. msg_mcast.msg_accrights = NULL;
  867. msg_mcast.msg_accrightslen = 0;
  868. #endif
  869. /*
  870. * Transmit multicast message
  871. * An error here is recovered by totemsrp
  872. */
  873. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  874. MSG_NOSIGNAL);
  875. if (res < 0) {
  876. char error_str[100];
  877. strerror_r (errno, error_str, sizeof(error_str));
  878. log_printf (instance->totemudp_log_level_debug,
  879. "sendmsg(mcast) failed (non-critical): %s\n", error_str);
  880. }
  881. }
  882. static void totemudp_mcast_thread_state_constructor (
  883. void *totemudp_mcast_thread_state_in)
  884. {
  885. struct totemudp_mcast_thread_state *totemudp_mcast_thread_state =
  886. (struct totemudp_mcast_thread_state *)totemudp_mcast_thread_state_in;
  887. memset (totemudp_mcast_thread_state, 0,
  888. sizeof (totemudp_mcast_thread_state));
  889. rng_make_prng (128, PRNG_SOBER,
  890. &totemudp_mcast_thread_state->prng_state, NULL);
  891. }
  892. static void totemudp_mcast_worker_fn (void *thread_state, void *work_item_in)
  893. {
  894. struct work_item *work_item = (struct work_item *)work_item_in;
  895. struct totemudp_mcast_thread_state *totemudp_mcast_thread_state =
  896. (struct totemudp_mcast_thread_state *)thread_state;
  897. struct totemudp_instance *instance = work_item->instance;
  898. struct msghdr msg_mcast;
  899. unsigned char sheader[sizeof (struct security_header)];
  900. int res = 0;
  901. size_t buf_len;
  902. struct iovec iovec_enc[2];
  903. struct iovec iovec;
  904. struct sockaddr_storage sockaddr;
  905. int addrlen;
  906. if (instance->totem_config->secauth == 1) {
  907. iovec_enc[0].iov_base = (void *)sheader;
  908. iovec_enc[0].iov_len = sizeof (struct security_header);
  909. iovec_enc[1].iov_base = (void *)work_item->msg;
  910. iovec_enc[1].iov_len = work_item->msg_len;
  911. /*
  912. * Encrypt and digest the message
  913. */
  914. encrypt_and_sign_worker (
  915. instance,
  916. totemudp_mcast_thread_state->iobuf,
  917. &buf_len,
  918. iovec_enc, 2);
  919. iovec.iov_base = (void *)totemudp_mcast_thread_state->iobuf;
  920. iovec.iov_len = buf_len;
  921. } else {
  922. iovec.iov_base = (void *)work_item->msg;
  923. iovec.iov_len = work_item->msg_len;
  924. }
  925. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  926. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  927. msg_mcast.msg_name = &sockaddr;
  928. msg_mcast.msg_namelen = addrlen;
  929. msg_mcast.msg_iov = &iovec;
  930. msg_mcast.msg_iovlen = 1;
  931. #if !defined(COROSYNC_SOLARIS)
  932. msg_mcast.msg_control = 0;
  933. msg_mcast.msg_controllen = 0;
  934. msg_mcast.msg_flags = 0;
  935. #else
  936. msg_mcast.msg_accrights = NULL;
  937. msg_mcast.msg_accrightslen = 0;
  938. #endif
  939. /*
  940. * Transmit multicast message
  941. * An error here is recovered by totemudp
  942. */
  943. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  944. MSG_NOSIGNAL);
  945. if (res < 0) {
  946. char error_str[100];
  947. strerror_r (errno, error_str, sizeof(error_str));
  948. log_printf (instance->totemudp_log_level_debug,
  949. "sendmsg(mcast) failed (non-critical): %s\n", error_str);
  950. }
  951. }
  952. int totemudp_finalize (
  953. void *udp_context)
  954. {
  955. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  956. int res = 0;
  957. worker_thread_group_exit (&instance->worker_thread_group);
  958. if (instance->totemudp_sockets.mcast_recv > 0) {
  959. close (instance->totemudp_sockets.mcast_recv);
  960. poll_dispatch_delete (instance->totemudp_poll_handle,
  961. instance->totemudp_sockets.mcast_recv);
  962. }
  963. if (instance->totemudp_sockets.mcast_send > 0) {
  964. close (instance->totemudp_sockets.mcast_send);
  965. }
  966. if (instance->totemudp_sockets.token > 0) {
  967. close (instance->totemudp_sockets.token);
  968. poll_dispatch_delete (instance->totemudp_poll_handle,
  969. instance->totemudp_sockets.token);
  970. }
  971. return (res);
  972. }
  973. /*
  974. * Only designed to work with a message with one iov
  975. */
  976. static int net_deliver_fn (
  977. hdb_handle_t handle,
  978. int fd,
  979. int revents,
  980. void *data)
  981. {
  982. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  983. struct msghdr msg_recv;
  984. struct iovec *iovec;
  985. struct security_header *security_header;
  986. struct sockaddr_storage system_from;
  987. int bytes_received;
  988. int res = 0;
  989. unsigned char *msg_offset;
  990. unsigned int size_delv;
  991. if (instance->flushing == 1) {
  992. iovec = &instance->totemudp_iov_recv_flush;
  993. } else {
  994. iovec = &instance->totemudp_iov_recv;
  995. }
  996. /*
  997. * Receive datagram
  998. */
  999. msg_recv.msg_name = &system_from;
  1000. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1001. msg_recv.msg_iov = iovec;
  1002. msg_recv.msg_iovlen = 1;
  1003. #if !defined(COROSYNC_SOLARIS)
  1004. msg_recv.msg_control = 0;
  1005. msg_recv.msg_controllen = 0;
  1006. msg_recv.msg_flags = 0;
  1007. #else
  1008. msg_recv.msg_accrights = NULL;
  1009. msg_recv.msg_accrightslen = 0;
  1010. #endif
  1011. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1012. if (bytes_received == -1) {
  1013. return (0);
  1014. } else {
  1015. instance->stats_recv += bytes_received;
  1016. }
  1017. if ((instance->totem_config->secauth == 1) &&
  1018. (bytes_received < sizeof (struct security_header))) {
  1019. log_printf (instance->totemudp_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  1020. return (0);
  1021. }
  1022. security_header = (struct security_header *)iovec->iov_base;
  1023. iovec->iov_len = bytes_received;
  1024. if (instance->totem_config->secauth == 1) {
  1025. /*
  1026. * Authenticate and if authenticated, decrypt datagram
  1027. */
  1028. res = authenticate_and_decrypt (instance, iovec, 1);
  1029. if (res == -1) {
  1030. log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.\n");
  1031. log_printf (instance->totemudp_log_level_security,
  1032. "Invalid packet data\n");
  1033. iovec->iov_len = FRAME_SIZE_MAX;
  1034. return 0;
  1035. }
  1036. msg_offset = (unsigned char *)iovec->iov_base +
  1037. sizeof (struct security_header);
  1038. size_delv = bytes_received - sizeof (struct security_header);
  1039. } else {
  1040. msg_offset = (void *)iovec->iov_base;
  1041. size_delv = bytes_received;
  1042. }
  1043. /*
  1044. * Handle incoming message
  1045. */
  1046. instance->totemudp_deliver_fn (
  1047. instance->context,
  1048. msg_offset,
  1049. size_delv);
  1050. iovec->iov_len = FRAME_SIZE_MAX;
  1051. return (0);
  1052. }
  1053. static int netif_determine (
  1054. struct totemudp_instance *instance,
  1055. struct totem_ip_address *bindnet,
  1056. struct totem_ip_address *bound_to,
  1057. int *interface_up,
  1058. int *interface_num)
  1059. {
  1060. int res;
  1061. res = totemip_iface_check (bindnet, bound_to,
  1062. interface_up, interface_num,
  1063. instance->totem_config->clear_node_high_bit);
  1064. return (res);
  1065. }
  1066. /*
  1067. * If the interface is up, the sockets for totem are built. If the interface is down
  1068. * this function is requeued in the timer list to retry building the sockets later.
  1069. */
  1070. static void timer_function_netif_check_timeout (
  1071. void *data)
  1072. {
  1073. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  1074. int res;
  1075. int interface_up;
  1076. int interface_num;
  1077. struct totem_ip_address *bind_address;
  1078. /*
  1079. * Build sockets for every interface
  1080. */
  1081. netif_determine (instance,
  1082. &instance->totem_interface->bindnet,
  1083. &instance->totem_interface->boundto,
  1084. &interface_up, &interface_num);
  1085. /*
  1086. * If the network interface isn't back up and we are already
  1087. * in loopback mode, add timer to check again and return
  1088. */
  1089. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  1090. interface_up == 0) ||
  1091. (instance->my_memb_entries == 1 &&
  1092. instance->netif_bind_state == BIND_STATE_REGULAR &&
  1093. interface_up == 1)) {
  1094. poll_timer_add (instance->totemudp_poll_handle,
  1095. instance->totem_config->downcheck_timeout,
  1096. (void *)instance,
  1097. timer_function_netif_check_timeout,
  1098. &instance->timer_netif_check_timeout);
  1099. /*
  1100. * Add a timer to check for a downed regular interface
  1101. */
  1102. return;
  1103. }
  1104. if (instance->totemudp_sockets.mcast_recv > 0) {
  1105. close (instance->totemudp_sockets.mcast_recv);
  1106. poll_dispatch_delete (instance->totemudp_poll_handle,
  1107. instance->totemudp_sockets.mcast_recv);
  1108. }
  1109. if (instance->totemudp_sockets.mcast_send > 0) {
  1110. close (instance->totemudp_sockets.mcast_send);
  1111. }
  1112. if (instance->totemudp_sockets.token > 0) {
  1113. close (instance->totemudp_sockets.token);
  1114. poll_dispatch_delete (instance->totemudp_poll_handle,
  1115. instance->totemudp_sockets.token);
  1116. }
  1117. if (interface_up == 0) {
  1118. /*
  1119. * Interface is not up
  1120. */
  1121. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1122. bind_address = &localhost;
  1123. /*
  1124. * Add a timer to retry building interfaces and request memb_gather_enter
  1125. */
  1126. poll_timer_add (instance->totemudp_poll_handle,
  1127. instance->totem_config->downcheck_timeout,
  1128. (void *)instance,
  1129. timer_function_netif_check_timeout,
  1130. &instance->timer_netif_check_timeout);
  1131. } else {
  1132. /*
  1133. * Interface is up
  1134. */
  1135. instance->netif_bind_state = BIND_STATE_REGULAR;
  1136. bind_address = &instance->totem_interface->bindnet;
  1137. }
  1138. /*
  1139. * Create and bind the multicast and unicast sockets
  1140. */
  1141. res = totemudp_build_sockets (instance,
  1142. &instance->mcast_address,
  1143. bind_address,
  1144. &instance->totemudp_sockets,
  1145. &instance->totem_interface->boundto);
  1146. poll_dispatch_add (
  1147. instance->totemudp_poll_handle,
  1148. instance->totemudp_sockets.mcast_recv,
  1149. POLLIN, instance, net_deliver_fn);
  1150. poll_dispatch_add (
  1151. instance->totemudp_poll_handle,
  1152. instance->totemudp_sockets.token,
  1153. POLLIN, instance, net_deliver_fn);
  1154. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  1155. /*
  1156. * This reports changes in the interface to the user and totemsrp
  1157. */
  1158. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1159. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  1160. log_printf (instance->totemudp_log_level_notice,
  1161. "The network interface [%s] is now up.\n",
  1162. totemip_print (&instance->totem_interface->boundto));
  1163. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  1164. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  1165. }
  1166. /*
  1167. * Add a timer to check for interface going down in single membership
  1168. */
  1169. if (instance->my_memb_entries == 1) {
  1170. poll_timer_add (instance->totemudp_poll_handle,
  1171. instance->totem_config->downcheck_timeout,
  1172. (void *)instance,
  1173. timer_function_netif_check_timeout,
  1174. &instance->timer_netif_check_timeout);
  1175. }
  1176. } else {
  1177. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  1178. log_printf (instance->totemudp_log_level_notice,
  1179. "The network interface is down.\n");
  1180. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  1181. }
  1182. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  1183. }
  1184. }
  1185. /* Set the socket priority to INTERACTIVE to ensure
  1186. that our messages don't get queued behind anything else */
  1187. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  1188. {
  1189. #ifdef SO_PRIORITY
  1190. int prio = 6; /* TC_PRIO_INTERACTIVE */
  1191. char error_str[100];
  1192. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  1193. strerror_r (errno, error_str, 100);
  1194. log_printf (instance->totemudp_log_level_warning,
  1195. "Could not set traffic priority. (%s)\n", error_str);
  1196. }
  1197. #endif
  1198. }
  1199. static int totemudp_build_sockets_ip (
  1200. struct totemudp_instance *instance,
  1201. struct totem_ip_address *mcast_address,
  1202. struct totem_ip_address *bindnet_address,
  1203. struct totemudp_socket *sockets,
  1204. struct totem_ip_address *bound_to,
  1205. int interface_num)
  1206. {
  1207. struct sockaddr_storage sockaddr;
  1208. struct ipv6_mreq mreq6;
  1209. struct ip_mreq mreq;
  1210. struct sockaddr_storage mcast_ss, boundto_ss;
  1211. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  1212. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  1213. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  1214. unsigned int sendbuf_size;
  1215. unsigned int recvbuf_size;
  1216. unsigned int optlen = sizeof (sendbuf_size);
  1217. int addrlen;
  1218. int res;
  1219. int flag;
  1220. /*
  1221. * Create multicast recv socket
  1222. */
  1223. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1224. if (sockets->mcast_recv == -1) {
  1225. perror ("socket");
  1226. return (-1);
  1227. }
  1228. totemip_nosigpipe (sockets->mcast_recv);
  1229. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  1230. if (res == -1) {
  1231. char error_str[100];
  1232. strerror_r (errno, error_str, 100);
  1233. log_printf (instance->totemudp_log_level_warning,
  1234. "Could not set non-blocking operation on multicast socket: %s\n", error_str);
  1235. return (-1);
  1236. }
  1237. /*
  1238. * Force reuse
  1239. */
  1240. flag = 1;
  1241. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1242. perror("setsockopt reuseaddr");
  1243. return (-1);
  1244. }
  1245. /*
  1246. * Bind to multicast socket used for multicast receives
  1247. */
  1248. totemip_totemip_to_sockaddr_convert(mcast_address,
  1249. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1250. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  1251. if (res == -1) {
  1252. perror ("bind mcast recv socket failed");
  1253. return (-1);
  1254. }
  1255. /*
  1256. * Setup mcast send socket
  1257. */
  1258. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1259. if (sockets->mcast_send == -1) {
  1260. perror ("socket");
  1261. return (-1);
  1262. }
  1263. totemip_nosigpipe (sockets->mcast_send);
  1264. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  1265. if (res == -1) {
  1266. char error_str[100];
  1267. strerror_r (errno, error_str, 100);
  1268. log_printf (instance->totemudp_log_level_warning,
  1269. "Could not set non-blocking operation on multicast socket: %s\n", error_str);
  1270. return (-1);
  1271. }
  1272. /*
  1273. * Force reuse
  1274. */
  1275. flag = 1;
  1276. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1277. perror("setsockopt reuseaddr");
  1278. return (-1);
  1279. }
  1280. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  1281. &sockaddr, &addrlen);
  1282. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  1283. if (res == -1) {
  1284. perror ("bind mcast send socket failed");
  1285. return (-1);
  1286. }
  1287. /*
  1288. * Setup unicast socket
  1289. */
  1290. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1291. if (sockets->token == -1) {
  1292. perror ("socket2");
  1293. return (-1);
  1294. }
  1295. totemip_nosigpipe (sockets->token);
  1296. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  1297. if (res == -1) {
  1298. char error_str[100];
  1299. strerror_r (errno, error_str, 100);
  1300. log_printf (instance->totemudp_log_level_warning,
  1301. "Could not set non-blocking operation on token socket: %s\n", error_str);
  1302. return (-1);
  1303. }
  1304. /*
  1305. * Force reuse
  1306. */
  1307. flag = 1;
  1308. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1309. perror("setsockopt reuseaddr");
  1310. return (-1);
  1311. }
  1312. /*
  1313. * Bind to unicast socket used for token send/receives
  1314. * This has the side effect of binding to the correct interface
  1315. */
  1316. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1317. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  1318. if (res == -1) {
  1319. perror ("bind token socket failed");
  1320. return (-1);
  1321. }
  1322. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1323. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1324. /*
  1325. * Set buffer sizes to avoid overruns
  1326. */
  1327. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  1328. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  1329. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  1330. if (res == 0) {
  1331. log_printf (instance->totemudp_log_level_debug,
  1332. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  1333. }
  1334. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  1335. if (res == 0) {
  1336. log_printf (instance->totemudp_log_level_debug,
  1337. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  1338. }
  1339. /*
  1340. * Join group membership on socket
  1341. */
  1342. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  1343. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  1344. if (instance->totem_config->broadcast_use == 1) {
  1345. unsigned int broadcast = 1;
  1346. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  1347. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1348. perror("setting broadcast option");
  1349. return (-1);
  1350. }
  1351. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  1352. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1353. perror("setting broadcast option");
  1354. return (-1);
  1355. }
  1356. } else {
  1357. switch (bindnet_address->family) {
  1358. case AF_INET:
  1359. memset(&mreq, 0, sizeof(mreq));
  1360. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  1361. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  1362. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  1363. &mreq, sizeof (mreq));
  1364. if (res == -1) {
  1365. perror ("join ipv4 multicast group failed");
  1366. return (-1);
  1367. }
  1368. break;
  1369. case AF_INET6:
  1370. memset(&mreq6, 0, sizeof(mreq6));
  1371. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  1372. mreq6.ipv6mr_interface = interface_num;
  1373. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  1374. &mreq6, sizeof (mreq6));
  1375. if (res == -1) {
  1376. perror ("join ipv6 multicast group failed");
  1377. return (-1);
  1378. }
  1379. break;
  1380. }
  1381. }
  1382. /*
  1383. * Turn on multicast loopback
  1384. */
  1385. flag = 1;
  1386. switch ( bindnet_address->family ) {
  1387. case AF_INET:
  1388. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  1389. &flag, sizeof (flag));
  1390. break;
  1391. case AF_INET6:
  1392. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  1393. &flag, sizeof (flag));
  1394. }
  1395. if (res == -1) {
  1396. perror ("turn off loopback");
  1397. return (-1);
  1398. }
  1399. /*
  1400. * Set multicast packets TTL
  1401. */
  1402. if ( bindnet_address->family == AF_INET6 )
  1403. {
  1404. flag = 255;
  1405. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  1406. &flag, sizeof (flag));
  1407. if (res == -1) {
  1408. perror ("setp mcast hops");
  1409. return (-1);
  1410. }
  1411. }
  1412. /*
  1413. * Bind to a specific interface for multicast send and receive
  1414. */
  1415. switch ( bindnet_address->family ) {
  1416. case AF_INET:
  1417. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  1418. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1419. perror ("cannot select interface");
  1420. return (-1);
  1421. }
  1422. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  1423. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1424. perror ("cannot select interface");
  1425. return (-1);
  1426. }
  1427. break;
  1428. case AF_INET6:
  1429. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1430. &interface_num, sizeof (interface_num)) < 0) {
  1431. perror ("cannot select interface");
  1432. return (-1);
  1433. }
  1434. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1435. &interface_num, sizeof (interface_num)) < 0) {
  1436. perror ("cannot select interface");
  1437. return (-1);
  1438. }
  1439. break;
  1440. }
  1441. return 0;
  1442. }
  1443. static int totemudp_build_sockets (
  1444. struct totemudp_instance *instance,
  1445. struct totem_ip_address *mcast_address,
  1446. struct totem_ip_address *bindnet_address,
  1447. struct totemudp_socket *sockets,
  1448. struct totem_ip_address *bound_to)
  1449. {
  1450. int interface_num;
  1451. int interface_up;
  1452. int res;
  1453. /*
  1454. * Determine the ip address bound to and the interface name
  1455. */
  1456. res = netif_determine (instance,
  1457. bindnet_address,
  1458. bound_to,
  1459. &interface_up,
  1460. &interface_num);
  1461. if (res == -1) {
  1462. return (-1);
  1463. }
  1464. totemip_copy(&instance->my_id, bound_to);
  1465. res = totemudp_build_sockets_ip (instance, mcast_address,
  1466. bindnet_address, sockets, bound_to, interface_num);
  1467. /* We only send out of the token socket */
  1468. totemudp_traffic_control_set(instance, sockets->token);
  1469. return res;
  1470. }
  1471. /*
  1472. * Totem Network interface - also does encryption/decryption
  1473. * depends on poll abstraction, POSIX, IPV4
  1474. */
  1475. /*
  1476. * Create an instance
  1477. */
  1478. int totemudp_initialize (
  1479. hdb_handle_t poll_handle,
  1480. void **udp_context,
  1481. struct totem_config *totem_config,
  1482. int interface_no,
  1483. void *context,
  1484. void (*deliver_fn) (
  1485. void *context,
  1486. const void *msg,
  1487. unsigned int msg_len),
  1488. void (*iface_change_fn) (
  1489. void *context,
  1490. const struct totem_ip_address *iface_address),
  1491. void (*target_set_completed) (
  1492. void *context))
  1493. {
  1494. struct totemudp_instance *instance;
  1495. instance = malloc (sizeof (struct totemudp_instance));
  1496. if (instance == NULL) {
  1497. return (-1);
  1498. }
  1499. totemudp_instance_initialize (instance);
  1500. instance->totem_config = totem_config;
  1501. /*
  1502. * Configure logging
  1503. */
  1504. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1505. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1506. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1507. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1508. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1509. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1510. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1511. /*
  1512. * Initialize random number generator for later use to generate salt
  1513. */
  1514. memcpy (instance->totemudp_private_key, totem_config->private_key,
  1515. totem_config->private_key_len);
  1516. instance->totemudp_private_key_len = totem_config->private_key_len;
  1517. init_crypto(instance);
  1518. /*
  1519. * Initialize local variables for totemudp
  1520. */
  1521. instance->totem_interface = &totem_config->interfaces[interface_no];
  1522. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1523. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1524. /*
  1525. * If threaded send requested, initialize thread group data structure
  1526. */
  1527. if (totem_config->threads) {
  1528. worker_thread_group_init (
  1529. &instance->worker_thread_group,
  1530. totem_config->threads, 128,
  1531. sizeof (struct work_item),
  1532. sizeof (struct totemudp_mcast_thread_state),
  1533. totemudp_mcast_thread_state_constructor,
  1534. totemudp_mcast_worker_fn);
  1535. }
  1536. instance->totemudp_poll_handle = poll_handle;
  1537. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1538. instance->context = context;
  1539. instance->totemudp_deliver_fn = deliver_fn;
  1540. instance->totemudp_iface_change_fn = iface_change_fn;
  1541. instance->totemudp_target_set_completed = target_set_completed;
  1542. totemip_localhost (instance->mcast_address.family, &localhost);
  1543. localhost.nodeid = instance->totem_config->node_id;
  1544. /*
  1545. * RRP layer isn't ready to receive message because it hasn't
  1546. * initialized yet. Add short timer to check the interfaces.
  1547. */
  1548. poll_timer_add (instance->totemudp_poll_handle,
  1549. 100,
  1550. (void *)instance,
  1551. timer_function_netif_check_timeout,
  1552. &instance->timer_netif_check_timeout);
  1553. *udp_context = instance;
  1554. return (0);
  1555. }
  1556. int totemudp_processor_count_set (
  1557. void *udp_context,
  1558. int processor_count)
  1559. {
  1560. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1561. int res = 0;
  1562. instance->my_memb_entries = processor_count;
  1563. poll_timer_delete (instance->totemudp_poll_handle,
  1564. instance->timer_netif_check_timeout);
  1565. if (processor_count == 1) {
  1566. poll_timer_add (instance->totemudp_poll_handle,
  1567. instance->totem_config->downcheck_timeout,
  1568. (void *)instance,
  1569. timer_function_netif_check_timeout,
  1570. &instance->timer_netif_check_timeout);
  1571. }
  1572. return (res);
  1573. }
  1574. int totemudp_recv_flush (void *udp_context)
  1575. {
  1576. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1577. struct pollfd ufd;
  1578. int nfds;
  1579. int res = 0;
  1580. instance->flushing = 1;
  1581. do {
  1582. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1583. ufd.events = POLLIN;
  1584. nfds = poll (&ufd, 1, 0);
  1585. if (nfds == 1 && ufd.revents & POLLIN) {
  1586. net_deliver_fn (0, instance->totemudp_sockets.mcast_recv,
  1587. ufd.revents, instance);
  1588. }
  1589. } while (nfds == 1);
  1590. instance->flushing = 0;
  1591. return (res);
  1592. }
  1593. int totemudp_send_flush (void *udp_context)
  1594. {
  1595. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1596. int res = 0;
  1597. worker_thread_group_wait (&instance->worker_thread_group);
  1598. return (res);
  1599. }
  1600. int totemudp_token_send (
  1601. void *udp_context,
  1602. const void *msg,
  1603. unsigned int msg_len)
  1604. {
  1605. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1606. int res = 0;
  1607. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1608. return (res);
  1609. }
  1610. int totemudp_mcast_flush_send (
  1611. void *udp_context,
  1612. const void *msg,
  1613. unsigned int msg_len)
  1614. {
  1615. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1616. int res = 0;
  1617. mcast_sendmsg (instance, msg, msg_len);
  1618. return (res);
  1619. }
  1620. int totemudp_mcast_noflush_send (
  1621. void *udp_context,
  1622. const void *msg,
  1623. unsigned int msg_len)
  1624. {
  1625. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1626. struct work_item work_item;
  1627. int res = 0;
  1628. if (instance->totem_config->threads) {
  1629. work_item.msg = msg;
  1630. work_item.msg_len = msg_len;
  1631. work_item.instance = instance;
  1632. worker_thread_group_work_add (&instance->worker_thread_group,
  1633. &work_item);
  1634. } else {
  1635. mcast_sendmsg (instance, msg, msg_len);
  1636. }
  1637. return (res);
  1638. }
  1639. extern int totemudp_iface_check (void *udp_context)
  1640. {
  1641. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1642. int res = 0;
  1643. timer_function_netif_check_timeout (instance);
  1644. return (res);
  1645. }
  1646. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1647. {
  1648. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1649. if (totem_config->secauth == 1) {
  1650. totem_config->net_mtu -= sizeof (struct security_header) +
  1651. UDPIP_HEADER_SIZE;
  1652. } else {
  1653. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1654. }
  1655. }
  1656. const char *totemudp_iface_print (void *udp_context) {
  1657. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1658. const char *ret_char;
  1659. ret_char = totemip_print (&instance->my_id);
  1660. return (ret_char);
  1661. }
  1662. int totemudp_iface_get (
  1663. void *udp_context,
  1664. struct totem_ip_address *addr)
  1665. {
  1666. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1667. int res = 0;
  1668. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1669. return (res);
  1670. }
  1671. int totemudp_token_target_set (
  1672. void *udp_context,
  1673. const struct totem_ip_address *token_target)
  1674. {
  1675. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1676. int res = 0;
  1677. memcpy (&instance->token_target, token_target,
  1678. sizeof (struct totem_ip_address));
  1679. instance->totemudp_target_set_completed (instance->context);
  1680. return (res);
  1681. }
  1682. extern int totemudp_recv_mcast_empty (
  1683. void *udp_context)
  1684. {
  1685. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1686. unsigned int res;
  1687. struct sockaddr_storage system_from;
  1688. struct msghdr msg_recv;
  1689. struct pollfd ufd;
  1690. int nfds;
  1691. int msg_processed = 0;
  1692. /*
  1693. * Receive datagram
  1694. */
  1695. msg_recv.msg_name = &system_from;
  1696. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1697. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1698. msg_recv.msg_iovlen = 1;
  1699. #if !defined(COROSYNC_SOLARIS)
  1700. msg_recv.msg_control = 0;
  1701. msg_recv.msg_controllen = 0;
  1702. msg_recv.msg_flags = 0;
  1703. #else
  1704. msg_recv.msg_accrights = NULL;
  1705. msg_recv.msg_accrightslen = 0;
  1706. #endif
  1707. do {
  1708. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1709. ufd.events = POLLIN;
  1710. nfds = poll (&ufd, 1, 0);
  1711. if (nfds == 1 && ufd.revents & POLLIN) {
  1712. res = recvmsg (instance->totemudp_sockets.mcast_recv, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1713. if (res != -1) {
  1714. msg_processed = 1;
  1715. } else {
  1716. msg_processed = -1;
  1717. }
  1718. }
  1719. } while (nfds == 1);
  1720. return (msg_processed);
  1721. }