4
0

totemudpu.c 45 KB

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