totemudp.c 51 KB

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