totemudpu.c 44 KB

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