totemudpu.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  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 sockaddr_storage system_from;
  895. int bytes_received;
  896. int res = 0;
  897. unsigned char *msg_offset;
  898. unsigned int size_delv;
  899. iovec = &instance->totemudpu_iov_recv;
  900. /*
  901. * Receive datagram
  902. */
  903. msg_recv.msg_name = &system_from;
  904. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  905. msg_recv.msg_iov = iovec;
  906. msg_recv.msg_iovlen = 1;
  907. #if !defined(COROSYNC_SOLARIS)
  908. msg_recv.msg_control = 0;
  909. msg_recv.msg_controllen = 0;
  910. msg_recv.msg_flags = 0;
  911. #else
  912. msg_recv.msg_accrights = NULL;
  913. msg_recv.msg_accrightslen = 0;
  914. #endif
  915. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  916. if (bytes_received == -1) {
  917. return (0);
  918. } else {
  919. instance->stats_recv += bytes_received;
  920. }
  921. if ((instance->totem_config->secauth == 1) &&
  922. (bytes_received < sizeof (struct security_header))) {
  923. log_printf (instance->totemudpu_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  924. return (0);
  925. }
  926. iovec->iov_len = bytes_received;
  927. if (instance->totem_config->secauth == 1) {
  928. /*
  929. * Authenticate and if authenticated, decrypt datagram
  930. */
  931. res = authenticate_and_decrypt (instance, iovec, 1);
  932. if (res == -1) {
  933. log_printf (instance->totemudpu_log_level_security, "Received message has invalid digest... ignoring.\n");
  934. log_printf (instance->totemudpu_log_level_security,
  935. "Invalid packet data\n");
  936. iovec->iov_len = FRAME_SIZE_MAX;
  937. return 0;
  938. }
  939. msg_offset = (unsigned char *)iovec->iov_base +
  940. sizeof (struct security_header);
  941. size_delv = bytes_received - sizeof (struct security_header);
  942. } else {
  943. msg_offset = (void *)iovec->iov_base;
  944. size_delv = bytes_received;
  945. }
  946. /*
  947. * Handle incoming message
  948. */
  949. instance->totemudpu_deliver_fn (
  950. instance->context,
  951. msg_offset,
  952. size_delv);
  953. iovec->iov_len = FRAME_SIZE_MAX;
  954. return (0);
  955. }
  956. static int netif_determine (
  957. struct totemudpu_instance *instance,
  958. struct totem_ip_address *bindnet,
  959. struct totem_ip_address *bound_to,
  960. int *interface_up,
  961. int *interface_num)
  962. {
  963. int res;
  964. res = totemip_iface_check (bindnet, bound_to,
  965. interface_up, interface_num,
  966. instance->totem_config->clear_node_high_bit);
  967. return (res);
  968. }
  969. /*
  970. * If the interface is up, the sockets for totem are built. If the interface is down
  971. * this function is requeued in the timer list to retry building the sockets later.
  972. */
  973. static void timer_function_netif_check_timeout (
  974. void *data)
  975. {
  976. struct totemudpu_instance *instance = (struct totemudpu_instance *)data;
  977. int interface_up;
  978. int interface_num;
  979. struct totem_ip_address *bind_address;
  980. /*
  981. * Build sockets for every interface
  982. */
  983. netif_determine (instance,
  984. &instance->totem_interface->bindnet,
  985. &instance->totem_interface->boundto,
  986. &interface_up, &interface_num);
  987. /*
  988. * If the network interface isn't back up and we are already
  989. * in loopback mode, add timer to check again and return
  990. */
  991. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  992. interface_up == 0) ||
  993. (instance->my_memb_entries == 1 &&
  994. instance->netif_bind_state == BIND_STATE_REGULAR &&
  995. interface_up == 1)) {
  996. poll_timer_add (instance->totemudpu_poll_handle,
  997. instance->totem_config->downcheck_timeout,
  998. (void *)instance,
  999. timer_function_netif_check_timeout,
  1000. &instance->timer_netif_check_timeout);
  1001. /*
  1002. * Add a timer to check for a downed regular interface
  1003. */
  1004. return;
  1005. }
  1006. if (instance->token_socket > 0) {
  1007. close (instance->token_socket);
  1008. poll_dispatch_delete (instance->totemudpu_poll_handle,
  1009. instance->token_socket);
  1010. }
  1011. if (interface_up == 0) {
  1012. /*
  1013. * Interface is not up
  1014. */
  1015. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1016. bind_address = &localhost;
  1017. /*
  1018. * Add a timer to retry building interfaces and request memb_gather_enter
  1019. */
  1020. poll_timer_add (instance->totemudpu_poll_handle,
  1021. instance->totem_config->downcheck_timeout,
  1022. (void *)instance,
  1023. timer_function_netif_check_timeout,
  1024. &instance->timer_netif_check_timeout);
  1025. } else {
  1026. /*
  1027. * Interface is up
  1028. */
  1029. instance->netif_bind_state = BIND_STATE_REGULAR;
  1030. bind_address = &instance->totem_interface->bindnet;
  1031. }
  1032. /*
  1033. * Create and bind the multicast and unicast sockets
  1034. */
  1035. totemudpu_build_sockets (instance,
  1036. bind_address,
  1037. &instance->totem_interface->boundto);
  1038. poll_dispatch_add (
  1039. instance->totemudpu_poll_handle,
  1040. instance->token_socket,
  1041. POLLIN, instance, net_deliver_fn);
  1042. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  1043. /*
  1044. * This reports changes in the interface to the user and totemsrp
  1045. */
  1046. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1047. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  1048. log_printf (instance->totemudpu_log_level_notice,
  1049. "The network interface [%s] is now up.\n",
  1050. totemip_print (&instance->totem_interface->boundto));
  1051. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  1052. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  1053. }
  1054. /*
  1055. * Add a timer to check for interface going down in single membership
  1056. */
  1057. if (instance->my_memb_entries == 1) {
  1058. poll_timer_add (instance->totemudpu_poll_handle,
  1059. instance->totem_config->downcheck_timeout,
  1060. (void *)instance,
  1061. timer_function_netif_check_timeout,
  1062. &instance->timer_netif_check_timeout);
  1063. }
  1064. } else {
  1065. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  1066. log_printf (instance->totemudpu_log_level_notice,
  1067. "The network interface is down.\n");
  1068. instance->totemudpu_iface_change_fn (instance->context, &instance->my_id);
  1069. }
  1070. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  1071. }
  1072. }
  1073. /* Set the socket priority to INTERACTIVE to ensure
  1074. that our messages don't get queued behind anything else */
  1075. static void totemudpu_traffic_control_set(struct totemudpu_instance *instance, int sock)
  1076. {
  1077. #ifdef SO_PRIORITY
  1078. int prio = 6; /* TC_PRIO_INTERACTIVE */
  1079. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  1080. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1081. "Could not set traffic priority");
  1082. }
  1083. #endif
  1084. }
  1085. static int totemudpu_build_sockets_ip (
  1086. struct totemudpu_instance *instance,
  1087. struct totem_ip_address *bindnet_address,
  1088. struct totem_ip_address *bound_to,
  1089. int interface_num)
  1090. {
  1091. struct sockaddr_storage sockaddr;
  1092. int addrlen;
  1093. int res;
  1094. unsigned int recvbuf_size;
  1095. unsigned int optlen = sizeof (recvbuf_size);
  1096. /*
  1097. * Setup unicast socket
  1098. */
  1099. instance->token_socket = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1100. if (instance->token_socket == -1) {
  1101. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1102. "socket() failed");
  1103. return (-1);
  1104. }
  1105. totemip_nosigpipe (instance->token_socket);
  1106. res = fcntl (instance->token_socket, F_SETFL, O_NONBLOCK);
  1107. if (res == -1) {
  1108. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1109. "Could not set non-blocking operation on token socket");
  1110. return (-1);
  1111. }
  1112. /*
  1113. * Bind to unicast socket used for token send/receives
  1114. * This has the side effect of binding to the correct interface
  1115. */
  1116. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1117. res = bind (instance->token_socket, (struct sockaddr *)&sockaddr, addrlen);
  1118. if (res == -1) {
  1119. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1120. "bind token socket failed");
  1121. return (-1);
  1122. }
  1123. /*
  1124. * the token_socket can receive many messages. Allow a large number
  1125. * of receive messages on this socket
  1126. */
  1127. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1128. res = setsockopt (instance->token_socket, SOL_SOCKET, SO_RCVBUF,
  1129. &recvbuf_size, optlen);
  1130. if (res == -1) {
  1131. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1132. "Could not set recvbuf size");
  1133. }
  1134. return 0;
  1135. }
  1136. static int totemudpu_build_sockets (
  1137. struct totemudpu_instance *instance,
  1138. struct totem_ip_address *bindnet_address,
  1139. struct totem_ip_address *bound_to)
  1140. {
  1141. int interface_num;
  1142. int interface_up;
  1143. int res;
  1144. /*
  1145. * Determine the ip address bound to and the interface name
  1146. */
  1147. res = netif_determine (instance,
  1148. bindnet_address,
  1149. bound_to,
  1150. &interface_up,
  1151. &interface_num);
  1152. if (res == -1) {
  1153. return (-1);
  1154. }
  1155. totemip_copy(&instance->my_id, bound_to);
  1156. res = totemudpu_build_sockets_ip (instance,
  1157. bindnet_address, bound_to, interface_num);
  1158. /* We only send out of the token socket */
  1159. totemudpu_traffic_control_set(instance, instance->token_socket);
  1160. return res;
  1161. }
  1162. /*
  1163. * Totem Network interface - also does encryption/decryption
  1164. * depends on poll abstraction, POSIX, IPV4
  1165. */
  1166. /*
  1167. * Create an instance
  1168. */
  1169. int totemudpu_initialize (
  1170. hdb_handle_t poll_handle,
  1171. void **udpu_context,
  1172. struct totem_config *totem_config,
  1173. int interface_no,
  1174. void *context,
  1175. void (*deliver_fn) (
  1176. void *context,
  1177. const void *msg,
  1178. unsigned int msg_len),
  1179. void (*iface_change_fn) (
  1180. void *context,
  1181. const struct totem_ip_address *iface_address),
  1182. void (*target_set_completed) (
  1183. void *context))
  1184. {
  1185. struct totemudpu_instance *instance;
  1186. instance = malloc (sizeof (struct totemudpu_instance));
  1187. if (instance == NULL) {
  1188. return (-1);
  1189. }
  1190. totemudpu_instance_initialize (instance);
  1191. instance->totem_config = totem_config;
  1192. /*
  1193. * Configure logging
  1194. */
  1195. instance->totemudpu_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1196. instance->totemudpu_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1197. instance->totemudpu_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1198. instance->totemudpu_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1199. instance->totemudpu_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1200. instance->totemudpu_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1201. instance->totemudpu_log_printf = totem_config->totem_logging_configuration.log_printf;
  1202. /*
  1203. * Initialize random number generator for later use to generate salt
  1204. */
  1205. memcpy (instance->totemudpu_private_key, totem_config->private_key,
  1206. totem_config->private_key_len);
  1207. instance->totemudpu_private_key_len = totem_config->private_key_len;
  1208. init_crypto(instance);
  1209. /*
  1210. * Initialize local variables for totemudpu
  1211. */
  1212. instance->totem_interface = &totem_config->interfaces[interface_no];
  1213. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1214. instance->totemudpu_poll_handle = poll_handle;
  1215. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1216. instance->context = context;
  1217. instance->totemudpu_deliver_fn = deliver_fn;
  1218. instance->totemudpu_iface_change_fn = iface_change_fn;
  1219. instance->totemudpu_target_set_completed = target_set_completed;
  1220. totemip_localhost (AF_INET, &localhost);
  1221. localhost.nodeid = instance->totem_config->node_id;
  1222. /*
  1223. * RRP layer isn't ready to receive message because it hasn't
  1224. * initialized yet. Add short timer to check the interfaces.
  1225. */
  1226. poll_timer_add (instance->totemudpu_poll_handle,
  1227. 100,
  1228. (void *)instance,
  1229. timer_function_netif_check_timeout,
  1230. &instance->timer_netif_check_timeout);
  1231. *udpu_context = instance;
  1232. return (0);
  1233. }
  1234. void *totemudpu_buffer_alloc (void)
  1235. {
  1236. return malloc (FRAME_SIZE_MAX);
  1237. }
  1238. void totemudpu_buffer_release (void *ptr)
  1239. {
  1240. return free (ptr);
  1241. }
  1242. int totemudpu_processor_count_set (
  1243. void *udpu_context,
  1244. int processor_count)
  1245. {
  1246. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1247. int res = 0;
  1248. instance->my_memb_entries = processor_count;
  1249. poll_timer_delete (instance->totemudpu_poll_handle,
  1250. instance->timer_netif_check_timeout);
  1251. if (processor_count == 1) {
  1252. poll_timer_add (instance->totemudpu_poll_handle,
  1253. instance->totem_config->downcheck_timeout,
  1254. (void *)instance,
  1255. timer_function_netif_check_timeout,
  1256. &instance->timer_netif_check_timeout);
  1257. }
  1258. return (res);
  1259. }
  1260. int totemudpu_recv_flush (void *udpu_context)
  1261. {
  1262. int res = 0;
  1263. return (res);
  1264. }
  1265. int totemudpu_send_flush (void *udpu_context)
  1266. {
  1267. int res = 0;
  1268. return (res);
  1269. }
  1270. int totemudpu_token_send (
  1271. void *udpu_context,
  1272. const void *msg,
  1273. unsigned int msg_len)
  1274. {
  1275. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1276. int res = 0;
  1277. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1278. return (res);
  1279. }
  1280. int totemudpu_mcast_flush_send (
  1281. void *udpu_context,
  1282. const void *msg,
  1283. unsigned int msg_len)
  1284. {
  1285. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1286. int res = 0;
  1287. mcast_sendmsg (instance, msg, msg_len);
  1288. return (res);
  1289. }
  1290. int totemudpu_mcast_noflush_send (
  1291. void *udpu_context,
  1292. const void *msg,
  1293. unsigned int msg_len)
  1294. {
  1295. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1296. int res = 0;
  1297. mcast_sendmsg (instance, msg, msg_len);
  1298. return (res);
  1299. }
  1300. extern int totemudpu_iface_check (void *udpu_context)
  1301. {
  1302. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1303. int res = 0;
  1304. timer_function_netif_check_timeout (instance);
  1305. return (res);
  1306. }
  1307. extern void totemudpu_net_mtu_adjust (void *udpu_context, struct totem_config *totem_config)
  1308. {
  1309. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1310. if (totem_config->secauth == 1) {
  1311. totem_config->net_mtu -= sizeof (struct security_header) +
  1312. UDPIP_HEADER_SIZE;
  1313. } else {
  1314. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1315. }
  1316. }
  1317. const char *totemudpu_iface_print (void *udpu_context) {
  1318. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1319. const char *ret_char;
  1320. ret_char = totemip_print (&instance->my_id);
  1321. return (ret_char);
  1322. }
  1323. int totemudpu_iface_get (
  1324. void *udpu_context,
  1325. struct totem_ip_address *addr)
  1326. {
  1327. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1328. int res = 0;
  1329. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1330. return (res);
  1331. }
  1332. int totemudpu_token_target_set (
  1333. void *udpu_context,
  1334. const struct totem_ip_address *token_target)
  1335. {
  1336. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1337. int res = 0;
  1338. memcpy (&instance->token_target, token_target,
  1339. sizeof (struct totem_ip_address));
  1340. instance->totemudpu_target_set_completed (instance->context);
  1341. return (res);
  1342. }
  1343. extern int totemudpu_recv_mcast_empty (
  1344. void *udpu_context)
  1345. {
  1346. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1347. unsigned int res;
  1348. struct sockaddr_storage system_from;
  1349. struct msghdr msg_recv;
  1350. struct pollfd ufd;
  1351. int nfds;
  1352. int msg_processed = 0;
  1353. /*
  1354. * Receive datagram
  1355. */
  1356. msg_recv.msg_name = &system_from;
  1357. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1358. msg_recv.msg_iov = &instance->totemudpu_iov_recv;
  1359. msg_recv.msg_iovlen = 1;
  1360. #if !defined(COROSYNC_SOLARIS)
  1361. msg_recv.msg_control = 0;
  1362. msg_recv.msg_controllen = 0;
  1363. msg_recv.msg_flags = 0;
  1364. #else
  1365. msg_recv.msg_accrights = NULL;
  1366. msg_recv.msg_accrightslen = 0;
  1367. #endif
  1368. do {
  1369. ufd.fd = instance->token_socket;
  1370. ufd.events = POLLIN;
  1371. nfds = poll (&ufd, 1, 0);
  1372. if (nfds == 1 && ufd.revents & POLLIN) {
  1373. res = recvmsg (instance->token_socket, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1374. if (res != -1) {
  1375. msg_processed = 1;
  1376. } else {
  1377. msg_processed = -1;
  1378. }
  1379. }
  1380. } while (nfds == 1);
  1381. return (msg_processed);
  1382. }
  1383. int totemudpu_member_add (
  1384. void *udpu_context,
  1385. const struct totem_ip_address *member)
  1386. {
  1387. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1388. struct totemudpu_member *new_member;
  1389. int res;
  1390. unsigned int sendbuf_size;
  1391. unsigned int optlen = sizeof (sendbuf_size);
  1392. new_member = malloc (sizeof (struct totemudpu_member));
  1393. if (new_member == NULL) {
  1394. return (-1);
  1395. }
  1396. list_init (&new_member->list);
  1397. list_add_tail (&new_member->list, &instance->member_list);
  1398. memcpy (&new_member->member, member, sizeof (struct totem_ip_address));
  1399. new_member->fd = socket (member->family, SOCK_DGRAM, 0);
  1400. if (new_member->fd == -1) {
  1401. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1402. "Could not create socket for new member");
  1403. return (-1);
  1404. }
  1405. totemip_nosigpipe (new_member->fd);
  1406. res = fcntl (new_member->fd, F_SETFL, O_NONBLOCK);
  1407. if (res == -1) {
  1408. LOGSYS_PERROR (errno, instance->totemudpu_log_level_warning,
  1409. "Could not set non-blocking operation on token socket");
  1410. return (-1);
  1411. }
  1412. /*
  1413. * These sockets are used to send multicast messages, so their buffers
  1414. * should be large
  1415. */
  1416. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1417. res = setsockopt (new_member->fd, SOL_SOCKET, SO_SNDBUF,
  1418. &sendbuf_size, optlen);
  1419. if (res == -1) {
  1420. LOGSYS_PERROR (errno, instance->totemudpu_log_level_notice,
  1421. "Could not set sendbuf size");
  1422. }
  1423. return (0);
  1424. }
  1425. int totemudpu_member_remove (
  1426. void *udpu_context,
  1427. const struct totem_ip_address *token_target)
  1428. {
  1429. struct totemudpu_instance *instance = (struct totemudpu_instance *)udpu_context;
  1430. instance = NULL;
  1431. return (0);
  1432. }