totemudpu.c 44 KB

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