4
0

totemudp.c 53 KB

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