totemudpu.c 45 KB

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