totemudpu.c 43 KB

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