totemudp.c 51 KB

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