totemudp.c 57 KB

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