totemudpu.c 43 KB

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