4
0

totemudpu.c 44 KB

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