totemudp.c 58 KB

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