totemudp.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 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 <pthread.h>
  37. #include <sys/mman.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <sys/socket.h>
  41. #include <netdb.h>
  42. #include <sys/un.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/param.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <sys/time.h>
  55. #include <sys/poll.h>
  56. #include <limits.h>
  57. #include <corosync/sq.h>
  58. #include <corosync/swab.h>
  59. #include <corosync/list.h>
  60. #include <qb/qbdefs.h>
  61. #include <qb/qbloop.h>
  62. #define LOGSYS_UTILS_ONLY 1
  63. #include <corosync/logsys.h>
  64. #include "totemudp.h"
  65. #include "util.h"
  66. #include <nss.h>
  67. #include <pk11pub.h>
  68. #include <pkcs11.h>
  69. #include <prerror.h>
  70. #ifndef MSG_NOSIGNAL
  71. #define MSG_NOSIGNAL 0
  72. #endif
  73. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  74. #define NETIF_STATE_REPORT_UP 1
  75. #define NETIF_STATE_REPORT_DOWN 2
  76. #define BIND_STATE_UNBOUND 0
  77. #define BIND_STATE_REGULAR 1
  78. #define BIND_STATE_LOOPBACK 2
  79. #define MESSAGE_TYPE_MCAST 1
  80. #define HMAC_HASH_SIZE 20
  81. struct security_header {
  82. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  83. unsigned char salt[16]; /* random number */
  84. char msg[0];
  85. } __attribute__((packed));
  86. struct totemudp_socket {
  87. int mcast_recv;
  88. int mcast_send;
  89. int token;
  90. };
  91. struct totemudp_instance {
  92. PK11SymKey *nss_sym_key;
  93. PK11SymKey *nss_sym_key_sign;
  94. unsigned char totemudp_private_key[1024];
  95. unsigned int totemudp_private_key_len;
  96. qb_loop_t *totemudp_poll_handle;
  97. struct totem_interface *totem_interface;
  98. int netif_state_report;
  99. int netif_bind_state;
  100. void *context;
  101. void (*totemudp_deliver_fn) (
  102. void *context,
  103. const void *msg,
  104. unsigned int msg_len);
  105. void (*totemudp_iface_change_fn) (
  106. void *context,
  107. const struct totem_ip_address *iface_address);
  108. void (*totemudp_target_set_completed) (void *context);
  109. /*
  110. * Function and data used to log messages
  111. */
  112. int totemudp_log_level_security;
  113. int totemudp_log_level_error;
  114. int totemudp_log_level_warning;
  115. int totemudp_log_level_notice;
  116. int totemudp_log_level_debug;
  117. int totemudp_subsys_id;
  118. void (*totemudp_log_printf) (
  119. int level,
  120. int subsys,
  121. const char *function,
  122. const char *file,
  123. int line,
  124. const char *format,
  125. ...)__attribute__((format(printf, 6, 7)));
  126. void *udp_context;
  127. char iov_buffer[FRAME_SIZE_MAX];
  128. char iov_buffer_flush[FRAME_SIZE_MAX];
  129. struct iovec totemudp_iov_recv;
  130. struct iovec totemudp_iov_recv_flush;
  131. struct totemudp_socket totemudp_sockets;
  132. struct totem_ip_address mcast_address;
  133. int stats_sent;
  134. int stats_recv;
  135. int stats_delv;
  136. int stats_remcasts;
  137. int stats_orf_token;
  138. struct timeval stats_tv_start;
  139. struct totem_ip_address my_id;
  140. int firstrun;
  141. qb_loop_timer_handle timer_netif_check_timeout;
  142. unsigned int my_memb_entries;
  143. int flushing;
  144. struct totem_config *totem_config;
  145. struct totem_ip_address token_target;
  146. };
  147. struct work_item {
  148. const void *msg;
  149. unsigned int msg_len;
  150. struct totemudp_instance *instance;
  151. };
  152. static int totemudp_build_sockets (
  153. struct totemudp_instance *instance,
  154. struct totem_ip_address *bindnet_address,
  155. struct totem_ip_address *mcastaddress,
  156. struct totemudp_socket *sockets,
  157. struct totem_ip_address *bound_to);
  158. static struct totem_ip_address localhost;
  159. static void totemudp_instance_initialize (struct totemudp_instance *instance)
  160. {
  161. memset (instance, 0, sizeof (struct totemudp_instance));
  162. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  163. instance->totemudp_iov_recv.iov_base = instance->iov_buffer;
  164. instance->totemudp_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  165. instance->totemudp_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  166. instance->totemudp_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  167. /*
  168. * There is always atleast 1 processor
  169. */
  170. instance->my_memb_entries = 1;
  171. }
  172. #define log_printf(level, format, args...) \
  173. do { \
  174. instance->totemudp_log_printf ( \
  175. level, instance->totemudp_subsys_id, \
  176. __FUNCTION__, __FILE__, __LINE__, \
  177. (const char *)format, ##args); \
  178. } while (0);
  179. #define LOGSYS_PERROR(err_num, level, fmt, args...) \
  180. do { \
  181. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  182. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  183. instance->totemudp_log_printf ( \
  184. level, instance->totemudp_subsys_id, \
  185. __FUNCTION__, __FILE__, __LINE__, \
  186. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  187. } while(0)
  188. static unsigned char *copy_from_iovec(
  189. const struct iovec *iov,
  190. unsigned int iov_len,
  191. size_t *buf_size)
  192. {
  193. int i;
  194. size_t bufptr;
  195. size_t buflen = 0;
  196. unsigned char *newbuf;
  197. for (i=0; i<iov_len; i++)
  198. buflen += iov[i].iov_len;
  199. newbuf = malloc(buflen);
  200. if (!newbuf)
  201. return NULL;
  202. bufptr=0;
  203. for (i=0; i<iov_len; i++) {
  204. memcpy(newbuf+bufptr, iov[i].iov_base, iov[i].iov_len);
  205. bufptr += iov[i].iov_len;
  206. }
  207. *buf_size = buflen;
  208. return newbuf;
  209. }
  210. static void copy_to_iovec(
  211. struct iovec *iov,
  212. unsigned int iov_len,
  213. const unsigned char *buf,
  214. size_t buf_size)
  215. {
  216. int i;
  217. size_t copylen;
  218. size_t bufptr = 0;
  219. bufptr=0;
  220. for (i=0; i<iov_len; i++) {
  221. copylen = iov[i].iov_len;
  222. if (bufptr + copylen > buf_size) {
  223. copylen = buf_size - bufptr;
  224. }
  225. memcpy(iov[i].iov_base, buf+bufptr, copylen);
  226. bufptr += copylen;
  227. if (iov[i].iov_len != copylen) {
  228. iov[i].iov_len = copylen;
  229. return;
  230. }
  231. }
  232. }
  233. static void init_nss_crypto(
  234. struct totemudp_instance *instance)
  235. {
  236. PK11SlotInfo* aes_slot = NULL;
  237. PK11SlotInfo* sha1_slot = NULL;
  238. SECItem key_item;
  239. SECStatus rv;
  240. log_printf(instance->totemudp_log_level_notice,
  241. "Initializing transmit/receive security: NSS AES256CBC/SHA1HMAC (mode %u).", TOTEM_CRYPTO_AES256);
  242. rv = NSS_NoDB_Init(".");
  243. if (rv != SECSuccess)
  244. {
  245. log_printf(instance->totemudp_log_level_security, "NSS initialization failed (err %d)",
  246. PR_GetError());
  247. goto out;
  248. }
  249. aes_slot = PK11_GetBestSlot(instance->totem_config->crypto_crypt_type, NULL);
  250. if (aes_slot == NULL)
  251. {
  252. log_printf(instance->totemudp_log_level_security, "Unable to find security slot (err %d)",
  253. PR_GetError());
  254. goto out;
  255. }
  256. sha1_slot = PK11_GetBestSlot(CKM_SHA_1_HMAC, NULL);
  257. if (sha1_slot == NULL)
  258. {
  259. log_printf(instance->totemudp_log_level_security, "Unable to find security slot (err %d)",
  260. PR_GetError());
  261. goto out;
  262. }
  263. /*
  264. * Make the private key into a SymKey that we can use
  265. */
  266. key_item.type = siBuffer;
  267. key_item.data = instance->totem_config->private_key;
  268. key_item.len = 32; /* Use 256 bits */
  269. instance->nss_sym_key = PK11_ImportSymKey(aes_slot,
  270. instance->totem_config->crypto_crypt_type,
  271. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  272. &key_item, NULL);
  273. if (instance->nss_sym_key == NULL)
  274. {
  275. log_printf(instance->totemudp_log_level_security, "Failure to import key into NSS (err %d)",
  276. PR_GetError());
  277. goto out;
  278. }
  279. instance->nss_sym_key_sign = PK11_ImportSymKey(sha1_slot,
  280. CKM_SHA_1_HMAC,
  281. PK11_OriginUnwrap, CKA_SIGN,
  282. &key_item, NULL);
  283. if (instance->nss_sym_key_sign == NULL) {
  284. log_printf(instance->totemudp_log_level_security, "Failure to import key into NSS (err %d)",
  285. PR_GetError());
  286. goto out;
  287. }
  288. out:
  289. return;
  290. }
  291. static int encrypt_and_sign_nss (
  292. struct totemudp_instance *instance,
  293. unsigned char *buf,
  294. size_t *buf_len,
  295. const struct iovec *iovec,
  296. unsigned int iov_len)
  297. {
  298. PK11Context* enc_context = NULL;
  299. SECStatus rv1, rv2;
  300. int tmp1_outlen;
  301. unsigned int tmp2_outlen;
  302. unsigned char *inbuf;
  303. unsigned char *data;
  304. unsigned char *outdata;
  305. size_t datalen;
  306. SECItem no_params;
  307. SECItem iv_item;
  308. struct security_header *header;
  309. SECItem *nss_sec_param;
  310. unsigned char nss_iv_data[16];
  311. SECStatus rv;
  312. no_params.type = siBuffer;
  313. no_params.data = 0;
  314. no_params.len = 0;
  315. tmp1_outlen = tmp2_outlen = 0;
  316. inbuf = copy_from_iovec(iovec, iov_len, &datalen);
  317. if (!inbuf) {
  318. log_printf(instance->totemudp_log_level_security, "malloc error copying buffer from iovec");
  319. return -1;
  320. }
  321. data = inbuf + sizeof (struct security_header);
  322. datalen -= sizeof (struct security_header);
  323. outdata = buf + sizeof (struct security_header);
  324. header = (struct security_header *)buf;
  325. rv = PK11_GenerateRandom (
  326. nss_iv_data,
  327. sizeof (nss_iv_data));
  328. if (rv != SECSuccess) {
  329. log_printf(instance->totemudp_log_level_security,
  330. "Failure to generate a random number %d",
  331. PR_GetError());
  332. }
  333. memcpy(header->salt, nss_iv_data, sizeof(nss_iv_data));
  334. iv_item.type = siBuffer;
  335. iv_item.data = nss_iv_data;
  336. iv_item.len = sizeof (nss_iv_data);
  337. nss_sec_param = PK11_ParamFromIV (
  338. instance->totem_config->crypto_crypt_type,
  339. &iv_item);
  340. if (nss_sec_param == NULL) {
  341. log_printf(instance->totemudp_log_level_security,
  342. "Failure to set up PKCS11 param (err %d)",
  343. PR_GetError());
  344. free (inbuf);
  345. return (-1);
  346. }
  347. /*
  348. * Create cipher context for encryption
  349. */
  350. enc_context = PK11_CreateContextBySymKey (
  351. instance->totem_config->crypto_crypt_type,
  352. CKA_ENCRYPT,
  353. instance->nss_sym_key,
  354. nss_sec_param);
  355. if (!enc_context) {
  356. char err[1024];
  357. PR_GetErrorText(err);
  358. err[PR_GetErrorTextLength()] = 0;
  359. log_printf(instance->totemudp_log_level_security,
  360. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s",
  361. instance->totem_config->crypto_crypt_type,
  362. PR_GetError(), err);
  363. free(inbuf);
  364. return -1;
  365. }
  366. rv1 = PK11_CipherOp(enc_context, outdata,
  367. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct security_header),
  368. data, datalen);
  369. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  370. FRAME_SIZE_MAX - tmp1_outlen);
  371. PK11_DestroyContext(enc_context, PR_TRUE);
  372. *buf_len = tmp1_outlen + tmp2_outlen;
  373. free(inbuf);
  374. // memcpy(&outdata[*buf_len], nss_iv_data, sizeof(nss_iv_data));
  375. if (rv1 != SECSuccess || rv2 != SECSuccess)
  376. goto out;
  377. /* Now do the digest */
  378. enc_context = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
  379. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  380. if (!enc_context) {
  381. char err[1024];
  382. PR_GetErrorText(err);
  383. err[PR_GetErrorTextLength()] = 0;
  384. log_printf(instance->totemudp_log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s",
  385. PR_GetError(), err);
  386. return -1;
  387. }
  388. PK11_DigestBegin(enc_context);
  389. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_len + 16);
  390. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  391. PK11_DestroyContext(enc_context, PR_TRUE);
  392. if (rv1 != SECSuccess || rv2 != SECSuccess)
  393. goto out;
  394. *buf_len = *buf_len + sizeof(struct security_header);
  395. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  396. return 0;
  397. out:
  398. return -1;
  399. }
  400. static int authenticate_and_decrypt_nss (
  401. struct totemudp_instance *instance,
  402. struct iovec *iov,
  403. unsigned int iov_len)
  404. {
  405. PK11Context* enc_context = NULL;
  406. SECStatus rv1, rv2;
  407. int tmp1_outlen;
  408. unsigned int tmp2_outlen;
  409. unsigned char outbuf[FRAME_SIZE_MAX];
  410. unsigned char digest[HMAC_HASH_SIZE];
  411. unsigned char *outdata;
  412. int result_len;
  413. unsigned char *data;
  414. unsigned char *inbuf;
  415. size_t datalen;
  416. struct security_header *header = (struct security_header *)iov[0].iov_base;
  417. SECItem no_params;
  418. SECItem ivdata;
  419. no_params.type = siBuffer;
  420. no_params.data = 0;
  421. no_params.len = 0;
  422. tmp1_outlen = tmp2_outlen = 0;
  423. if (iov_len > 1) {
  424. inbuf = copy_from_iovec(iov, iov_len, &datalen);
  425. if (!inbuf) {
  426. log_printf(instance->totemudp_log_level_security, "malloc error copying buffer from iovec");
  427. return -1;
  428. }
  429. }
  430. else {
  431. inbuf = (unsigned char *)iov[0].iov_base;
  432. datalen = iov[0].iov_len;
  433. }
  434. data = inbuf + sizeof (struct security_header) - 16;
  435. datalen = datalen - sizeof (struct security_header) + 16;
  436. outdata = outbuf + sizeof (struct security_header);
  437. /* Check the digest */
  438. enc_context = PK11_CreateContextBySymKey (
  439. CKM_SHA_1_HMAC, CKA_SIGN,
  440. instance->nss_sym_key_sign,
  441. &no_params);
  442. if (!enc_context) {
  443. char err[1024];
  444. PR_GetErrorText(err);
  445. err[PR_GetErrorTextLength()] = 0;
  446. log_printf(instance->totemudp_log_level_security, "PK11_CreateContext failed (check digest) err %d: %s",
  447. PR_GetError(), err);
  448. free (inbuf);
  449. return -1;
  450. }
  451. PK11_DigestBegin(enc_context);
  452. rv1 = PK11_DigestOp(enc_context, data, datalen);
  453. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  454. PK11_DestroyContext(enc_context, PR_TRUE);
  455. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  456. log_printf(instance->totemudp_log_level_security, "Digest check failed");
  457. return -1;
  458. }
  459. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  460. log_printf(instance->totemudp_log_level_error, "Digest does not match");
  461. return -1;
  462. }
  463. /*
  464. * Get rid of salt
  465. */
  466. data += 16;
  467. datalen -= 16;
  468. /* Create cipher context for decryption */
  469. ivdata.type = siBuffer;
  470. ivdata.data = header->salt;
  471. ivdata.len = sizeof(header->salt);
  472. enc_context = PK11_CreateContextBySymKey(
  473. instance->totem_config->crypto_crypt_type,
  474. CKA_DECRYPT,
  475. instance->nss_sym_key, &ivdata);
  476. if (!enc_context) {
  477. log_printf(instance->totemudp_log_level_security,
  478. "PK11_CreateContext (decrypt) failed (err %d)",
  479. PR_GetError());
  480. return -1;
  481. }
  482. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  483. sizeof(outbuf) - sizeof (struct security_header),
  484. data, datalen);
  485. if (rv1 != SECSuccess) {
  486. log_printf(instance->totemudp_log_level_security,
  487. "PK11_CipherOp (decrypt) failed (err %d)",
  488. PR_GetError());
  489. }
  490. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  491. sizeof(outbuf) - tmp1_outlen);
  492. PK11_DestroyContext(enc_context, PR_TRUE);
  493. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct security_header);
  494. /* Copy it back to the buffer */
  495. copy_to_iovec(iov, iov_len, outbuf, result_len);
  496. if (iov_len > 1)
  497. free(inbuf);
  498. if (rv1 != SECSuccess || rv2 != SECSuccess)
  499. return -1;
  500. return 0;
  501. }
  502. static int encrypt_and_sign_worker (
  503. struct totemudp_instance *instance,
  504. unsigned char *buf,
  505. size_t *buf_len,
  506. const struct iovec *iovec,
  507. unsigned int iov_len)
  508. {
  509. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_AES256) {
  510. return encrypt_and_sign_nss(instance, buf, buf_len, iovec, iov_len);
  511. }
  512. return -1;
  513. }
  514. static int authenticate_and_decrypt (
  515. struct totemudp_instance *instance,
  516. struct iovec *iov,
  517. unsigned int iov_len)
  518. {
  519. unsigned char type;
  520. unsigned char *endbuf = (unsigned char *)iov[iov_len-1].iov_base;
  521. int res = -1;
  522. /*
  523. * Get the encryption type and remove it from the buffer
  524. */
  525. type = endbuf[iov[iov_len-1].iov_len-1];
  526. iov[iov_len-1].iov_len -= 1;
  527. if (type == TOTEM_CRYPTO_AES256) {
  528. res = authenticate_and_decrypt_nss(instance, iov, iov_len);
  529. }
  530. return res;
  531. }
  532. static void init_crypto(
  533. struct totemudp_instance *instance)
  534. {
  535. init_nss_crypto(instance);
  536. }
  537. int totemudp_crypto_set (
  538. void *udp_context,
  539. unsigned int type)
  540. {
  541. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  542. int res = 0;
  543. /*
  544. * Validate crypto algorithm
  545. */
  546. switch (type) {
  547. case TOTEM_CRYPTO_AES256:
  548. log_printf(instance->totemudp_log_level_security,
  549. "Transmit security set to: NSS AES256CBC/SHA1HMAC (mode %u)", type);
  550. break;
  551. default:
  552. res = -1;
  553. break;
  554. }
  555. return (res);
  556. }
  557. static inline void ucast_sendmsg (
  558. struct totemudp_instance *instance,
  559. struct totem_ip_address *system_to,
  560. const void *msg,
  561. unsigned int msg_len)
  562. {
  563. struct msghdr msg_ucast;
  564. int res = 0;
  565. size_t buf_len;
  566. unsigned char sheader[sizeof (struct security_header)];
  567. unsigned char encrypt_data[FRAME_SIZE_MAX];
  568. struct iovec iovec_encrypt[2];
  569. const struct iovec *iovec_sendmsg;
  570. struct sockaddr_storage sockaddr;
  571. struct iovec iovec;
  572. unsigned int iov_len;
  573. int addrlen;
  574. if (instance->totem_config->secauth == 1) {
  575. iovec_encrypt[0].iov_base = (void *)sheader;
  576. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  577. iovec_encrypt[1].iov_base = (void *)msg;
  578. iovec_encrypt[1].iov_len = msg_len;
  579. /*
  580. * Encrypt and digest the message
  581. */
  582. encrypt_and_sign_worker (
  583. instance,
  584. encrypt_data,
  585. &buf_len,
  586. iovec_encrypt,
  587. 2);
  588. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  589. iovec_encrypt[0].iov_base = (void *)encrypt_data;
  590. iovec_encrypt[0].iov_len = buf_len;
  591. iovec_sendmsg = &iovec_encrypt[0];
  592. iov_len = 1;
  593. } else {
  594. iovec.iov_base = (void *)msg;
  595. iovec.iov_len = msg_len;
  596. iovec_sendmsg = &iovec;
  597. iov_len = 1;
  598. }
  599. /*
  600. * Build unicast message
  601. */
  602. totemip_totemip_to_sockaddr_convert(system_to,
  603. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  604. msg_ucast.msg_name = &sockaddr;
  605. msg_ucast.msg_namelen = addrlen;
  606. msg_ucast.msg_iov = (void *) iovec_sendmsg;
  607. msg_ucast.msg_iovlen = iov_len;
  608. #if !defined(COROSYNC_SOLARIS)
  609. msg_ucast.msg_control = 0;
  610. msg_ucast.msg_controllen = 0;
  611. msg_ucast.msg_flags = 0;
  612. #else
  613. msg_ucast.msg_accrights = NULL;
  614. msg_ucast.msg_accrightslen = 0;
  615. #endif
  616. /*
  617. * Transmit unicast message
  618. * An error here is recovered by totemsrp
  619. */
  620. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
  621. MSG_NOSIGNAL);
  622. if (res < 0) {
  623. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  624. "sendmsg(ucast) failed (non-critical)");
  625. }
  626. }
  627. static inline void mcast_sendmsg (
  628. struct totemudp_instance *instance,
  629. const void *msg,
  630. unsigned int msg_len)
  631. {
  632. struct msghdr msg_mcast;
  633. int res = 0;
  634. size_t buf_len;
  635. unsigned char sheader[sizeof (struct security_header)];
  636. unsigned char encrypt_data[FRAME_SIZE_MAX];
  637. struct iovec iovec_encrypt[2];
  638. struct iovec iovec;
  639. const struct iovec *iovec_sendmsg;
  640. struct sockaddr_storage sockaddr;
  641. unsigned int iov_len;
  642. int addrlen;
  643. if (instance->totem_config->secauth == 1) {
  644. iovec_encrypt[0].iov_base = (void *)sheader;
  645. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  646. iovec_encrypt[1].iov_base = (void *)msg;
  647. iovec_encrypt[1].iov_len = msg_len;
  648. /*
  649. * Encrypt and digest the message
  650. */
  651. encrypt_and_sign_worker (
  652. instance,
  653. encrypt_data,
  654. &buf_len,
  655. iovec_encrypt,
  656. 2);
  657. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  658. iovec_encrypt[0].iov_base = (void *)encrypt_data;
  659. iovec_encrypt[0].iov_len = buf_len;
  660. iovec_sendmsg = &iovec_encrypt[0];
  661. iov_len = 1;
  662. } else {
  663. iovec.iov_base = (void *)msg;
  664. iovec.iov_len = msg_len;
  665. iovec_sendmsg = &iovec;
  666. iov_len = 1;
  667. }
  668. /*
  669. * Build multicast message
  670. */
  671. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  672. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  673. msg_mcast.msg_name = &sockaddr;
  674. msg_mcast.msg_namelen = addrlen;
  675. msg_mcast.msg_iov = (void *) iovec_sendmsg;
  676. msg_mcast.msg_iovlen = iov_len;
  677. #if !defined(COROSYNC_SOLARIS)
  678. msg_mcast.msg_control = 0;
  679. msg_mcast.msg_controllen = 0;
  680. msg_mcast.msg_flags = 0;
  681. #else
  682. msg_mcast.msg_accrights = NULL;
  683. msg_mcast.msg_accrightslen = 0;
  684. #endif
  685. /*
  686. * Transmit multicast message
  687. * An error here is recovered by totemsrp
  688. */
  689. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  690. MSG_NOSIGNAL);
  691. if (res < 0) {
  692. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  693. "sendmsg(mcast) failed (non-critical)");
  694. }
  695. }
  696. int totemudp_finalize (
  697. void *udp_context)
  698. {
  699. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  700. int res = 0;
  701. if (instance->totemudp_sockets.mcast_recv > 0) {
  702. close (instance->totemudp_sockets.mcast_recv);
  703. qb_loop_poll_del (instance->totemudp_poll_handle,
  704. instance->totemudp_sockets.mcast_recv);
  705. }
  706. if (instance->totemudp_sockets.mcast_send > 0) {
  707. close (instance->totemudp_sockets.mcast_send);
  708. }
  709. if (instance->totemudp_sockets.token > 0) {
  710. close (instance->totemudp_sockets.token);
  711. qb_loop_poll_del (instance->totemudp_poll_handle,
  712. instance->totemudp_sockets.token);
  713. }
  714. return (res);
  715. }
  716. /*
  717. * Only designed to work with a message with one iov
  718. */
  719. static int net_deliver_fn (
  720. int fd,
  721. int revents,
  722. void *data)
  723. {
  724. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  725. struct msghdr msg_recv;
  726. struct iovec *iovec;
  727. struct sockaddr_storage system_from;
  728. int bytes_received;
  729. int res = 0;
  730. unsigned char *msg_offset;
  731. unsigned int size_delv;
  732. char *message_type;
  733. if (instance->flushing == 1) {
  734. iovec = &instance->totemudp_iov_recv_flush;
  735. } else {
  736. iovec = &instance->totemudp_iov_recv;
  737. }
  738. /*
  739. * Receive datagram
  740. */
  741. msg_recv.msg_name = &system_from;
  742. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  743. msg_recv.msg_iov = iovec;
  744. msg_recv.msg_iovlen = 1;
  745. #if !defined(COROSYNC_SOLARIS)
  746. msg_recv.msg_control = 0;
  747. msg_recv.msg_controllen = 0;
  748. msg_recv.msg_flags = 0;
  749. #else
  750. msg_recv.msg_accrights = NULL;
  751. msg_recv.msg_accrightslen = 0;
  752. #endif
  753. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  754. if (bytes_received == -1) {
  755. return (0);
  756. } else {
  757. instance->stats_recv += bytes_received;
  758. }
  759. if ((instance->totem_config->secauth == 1) &&
  760. (bytes_received < sizeof (struct security_header))) {
  761. log_printf (instance->totemudp_log_level_security, "Received message is too short... ignoring %d.", bytes_received);
  762. return (0);
  763. }
  764. iovec->iov_len = bytes_received;
  765. if (instance->totem_config->secauth == 1) {
  766. /*
  767. * Authenticate and if authenticated, decrypt datagram
  768. */
  769. res = authenticate_and_decrypt (instance, iovec, 1);
  770. if (res == -1) {
  771. log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.");
  772. log_printf (instance->totemudp_log_level_security,
  773. "Invalid packet data");
  774. iovec->iov_len = FRAME_SIZE_MAX;
  775. return 0;
  776. }
  777. msg_offset = (unsigned char *)iovec->iov_base +
  778. sizeof (struct security_header);
  779. size_delv = bytes_received - sizeof (struct security_header);
  780. } else {
  781. msg_offset = (void *)iovec->iov_base;
  782. size_delv = bytes_received;
  783. }
  784. /*
  785. * Drop all non-mcast messages (more specifically join
  786. * messages should be dropped)
  787. */
  788. message_type = (char *)msg_offset;
  789. if (instance->flushing == 1 && *message_type != MESSAGE_TYPE_MCAST) {
  790. iovec->iov_len = FRAME_SIZE_MAX;
  791. return (0);
  792. }
  793. /*
  794. * Handle incoming message
  795. */
  796. instance->totemudp_deliver_fn (
  797. instance->context,
  798. msg_offset,
  799. size_delv);
  800. iovec->iov_len = FRAME_SIZE_MAX;
  801. return (0);
  802. }
  803. static int netif_determine (
  804. struct totemudp_instance *instance,
  805. struct totem_ip_address *bindnet,
  806. struct totem_ip_address *bound_to,
  807. int *interface_up,
  808. int *interface_num)
  809. {
  810. int res;
  811. res = totemip_iface_check (bindnet, bound_to,
  812. interface_up, interface_num,
  813. instance->totem_config->clear_node_high_bit);
  814. return (res);
  815. }
  816. /*
  817. * If the interface is up, the sockets for totem are built. If the interface is down
  818. * this function is requeued in the timer list to retry building the sockets later.
  819. */
  820. static void timer_function_netif_check_timeout (
  821. void *data)
  822. {
  823. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  824. int interface_up;
  825. int interface_num;
  826. struct totem_ip_address *bind_address;
  827. /*
  828. * Build sockets for every interface
  829. */
  830. netif_determine (instance,
  831. &instance->totem_interface->bindnet,
  832. &instance->totem_interface->boundto,
  833. &interface_up, &interface_num);
  834. /*
  835. * If the network interface isn't back up and we are already
  836. * in loopback mode, add timer to check again and return
  837. */
  838. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  839. interface_up == 0) ||
  840. (instance->my_memb_entries == 1 &&
  841. instance->netif_bind_state == BIND_STATE_REGULAR &&
  842. interface_up == 1)) {
  843. qb_loop_timer_add (instance->totemudp_poll_handle,
  844. QB_LOOP_MED,
  845. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  846. (void *)instance,
  847. timer_function_netif_check_timeout,
  848. &instance->timer_netif_check_timeout);
  849. /*
  850. * Add a timer to check for a downed regular interface
  851. */
  852. return;
  853. }
  854. if (instance->totemudp_sockets.mcast_recv > 0) {
  855. close (instance->totemudp_sockets.mcast_recv);
  856. qb_loop_poll_del (instance->totemudp_poll_handle,
  857. instance->totemudp_sockets.mcast_recv);
  858. }
  859. if (instance->totemudp_sockets.mcast_send > 0) {
  860. close (instance->totemudp_sockets.mcast_send);
  861. }
  862. if (instance->totemudp_sockets.token > 0) {
  863. close (instance->totemudp_sockets.token);
  864. qb_loop_poll_del (instance->totemudp_poll_handle,
  865. instance->totemudp_sockets.token);
  866. }
  867. if (interface_up == 0) {
  868. /*
  869. * Interface is not up
  870. */
  871. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  872. bind_address = &localhost;
  873. /*
  874. * Add a timer to retry building interfaces and request memb_gather_enter
  875. */
  876. qb_loop_timer_add (instance->totemudp_poll_handle,
  877. QB_LOOP_MED,
  878. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  879. (void *)instance,
  880. timer_function_netif_check_timeout,
  881. &instance->timer_netif_check_timeout);
  882. } else {
  883. /*
  884. * Interface is up
  885. */
  886. instance->netif_bind_state = BIND_STATE_REGULAR;
  887. bind_address = &instance->totem_interface->bindnet;
  888. }
  889. /*
  890. * Create and bind the multicast and unicast sockets
  891. */
  892. (void)totemudp_build_sockets (instance,
  893. &instance->mcast_address,
  894. bind_address,
  895. &instance->totemudp_sockets,
  896. &instance->totem_interface->boundto);
  897. qb_loop_poll_add (
  898. instance->totemudp_poll_handle,
  899. QB_LOOP_MED,
  900. instance->totemudp_sockets.mcast_recv,
  901. POLLIN, instance, net_deliver_fn);
  902. qb_loop_poll_add (
  903. instance->totemudp_poll_handle,
  904. QB_LOOP_MED,
  905. instance->totemudp_sockets.token,
  906. POLLIN, instance, net_deliver_fn);
  907. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  908. /*
  909. * This reports changes in the interface to the user and totemsrp
  910. */
  911. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  912. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  913. log_printf (instance->totemudp_log_level_notice,
  914. "The network interface [%s] is now up.",
  915. totemip_print (&instance->totem_interface->boundto));
  916. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  917. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  918. }
  919. /*
  920. * Add a timer to check for interface going down in single membership
  921. */
  922. if (instance->my_memb_entries == 1) {
  923. qb_loop_timer_add (instance->totemudp_poll_handle,
  924. QB_LOOP_MED,
  925. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  926. (void *)instance,
  927. timer_function_netif_check_timeout,
  928. &instance->timer_netif_check_timeout);
  929. }
  930. } else {
  931. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  932. log_printf (instance->totemudp_log_level_notice,
  933. "The network interface is down.");
  934. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  935. }
  936. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  937. }
  938. }
  939. /* Set the socket priority to INTERACTIVE to ensure
  940. that our messages don't get queued behind anything else */
  941. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  942. {
  943. #ifdef SO_PRIORITY
  944. int prio = 6; /* TC_PRIO_INTERACTIVE */
  945. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  946. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  947. }
  948. #endif
  949. }
  950. static int totemudp_build_sockets_ip (
  951. struct totemudp_instance *instance,
  952. struct totem_ip_address *mcast_address,
  953. struct totem_ip_address *bindnet_address,
  954. struct totemudp_socket *sockets,
  955. struct totem_ip_address *bound_to,
  956. int interface_num)
  957. {
  958. struct sockaddr_storage sockaddr;
  959. struct ipv6_mreq mreq6;
  960. struct ip_mreq mreq;
  961. struct sockaddr_storage mcast_ss, boundto_ss;
  962. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  963. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  964. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  965. unsigned int sendbuf_size;
  966. unsigned int recvbuf_size;
  967. unsigned int optlen = sizeof (sendbuf_size);
  968. int addrlen;
  969. int res;
  970. int flag;
  971. /*
  972. * Create multicast recv socket
  973. */
  974. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  975. if (sockets->mcast_recv == -1) {
  976. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  977. "socket() failed");
  978. return (-1);
  979. }
  980. totemip_nosigpipe (sockets->mcast_recv);
  981. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  982. if (res == -1) {
  983. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  984. "Could not set non-blocking operation on multicast socket");
  985. return (-1);
  986. }
  987. /*
  988. * Force reuse
  989. */
  990. flag = 1;
  991. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  992. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  993. "setsockopt(SO_REUSEADDR) failed");
  994. return (-1);
  995. }
  996. /*
  997. * Bind to multicast socket used for multicast receives
  998. */
  999. totemip_totemip_to_sockaddr_convert(mcast_address,
  1000. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1001. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  1002. if (res == -1) {
  1003. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1004. "Unable to bind the socket to receive multicast packets");
  1005. return (-1);
  1006. }
  1007. /*
  1008. * Setup mcast send socket
  1009. */
  1010. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1011. if (sockets->mcast_send == -1) {
  1012. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1013. "socket() failed");
  1014. return (-1);
  1015. }
  1016. totemip_nosigpipe (sockets->mcast_send);
  1017. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  1018. if (res == -1) {
  1019. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1020. "Could not set non-blocking operation on multicast socket");
  1021. return (-1);
  1022. }
  1023. /*
  1024. * Force reuse
  1025. */
  1026. flag = 1;
  1027. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1028. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1029. "setsockopt(SO_REUSEADDR) failed");
  1030. return (-1);
  1031. }
  1032. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  1033. &sockaddr, &addrlen);
  1034. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  1035. if (res == -1) {
  1036. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1037. "Unable to bind the socket to send multicast packets");
  1038. return (-1);
  1039. }
  1040. /*
  1041. * Setup unicast socket
  1042. */
  1043. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1044. if (sockets->token == -1) {
  1045. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1046. "socket() failed");
  1047. return (-1);
  1048. }
  1049. totemip_nosigpipe (sockets->token);
  1050. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  1051. if (res == -1) {
  1052. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1053. "Could not set non-blocking operation on token socket");
  1054. return (-1);
  1055. }
  1056. /*
  1057. * Force reuse
  1058. */
  1059. flag = 1;
  1060. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1061. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1062. "setsockopt(SO_REUSEADDR) failed");
  1063. return (-1);
  1064. }
  1065. /*
  1066. * Bind to unicast socket used for token send/receives
  1067. * This has the side effect of binding to the correct interface
  1068. */
  1069. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1070. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  1071. if (res == -1) {
  1072. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1073. "Unable to bind UDP unicast socket");
  1074. return (-1);
  1075. }
  1076. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1077. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1078. /*
  1079. * Set buffer sizes to avoid overruns
  1080. */
  1081. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  1082. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  1083. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  1084. if (res == 0) {
  1085. log_printf (instance->totemudp_log_level_debug,
  1086. "Receive multicast socket recv buffer size (%d bytes).", recvbuf_size);
  1087. }
  1088. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  1089. if (res == 0) {
  1090. log_printf (instance->totemudp_log_level_debug,
  1091. "Transmit multicast socket send buffer size (%d bytes).", sendbuf_size);
  1092. }
  1093. /*
  1094. * Join group membership on socket
  1095. */
  1096. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  1097. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  1098. if (instance->totem_config->broadcast_use == 1) {
  1099. unsigned int broadcast = 1;
  1100. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  1101. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1102. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1103. "setting broadcast option failed");
  1104. return (-1);
  1105. }
  1106. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  1107. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1108. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1109. "setting broadcast option failed");
  1110. return (-1);
  1111. }
  1112. } else {
  1113. switch (bindnet_address->family) {
  1114. case AF_INET:
  1115. memset(&mreq, 0, sizeof(mreq));
  1116. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  1117. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  1118. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  1119. &mreq, sizeof (mreq));
  1120. if (res == -1) {
  1121. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1122. "join ipv4 multicast group failed");
  1123. return (-1);
  1124. }
  1125. break;
  1126. case AF_INET6:
  1127. memset(&mreq6, 0, sizeof(mreq6));
  1128. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  1129. mreq6.ipv6mr_interface = interface_num;
  1130. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  1131. &mreq6, sizeof (mreq6));
  1132. if (res == -1) {
  1133. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1134. "join ipv6 multicast group failed");
  1135. return (-1);
  1136. }
  1137. break;
  1138. }
  1139. }
  1140. /*
  1141. * Turn on multicast loopback
  1142. */
  1143. flag = 1;
  1144. switch ( bindnet_address->family ) {
  1145. case AF_INET:
  1146. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  1147. &flag, sizeof (flag));
  1148. break;
  1149. case AF_INET6:
  1150. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  1151. &flag, sizeof (flag));
  1152. }
  1153. if (res == -1) {
  1154. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1155. "Unable to turn on multicast loopback");
  1156. return (-1);
  1157. }
  1158. /*
  1159. * Set multicast packets TTL
  1160. */
  1161. flag = instance->totem_interface->ttl;
  1162. if (bindnet_address->family == AF_INET6) {
  1163. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  1164. &flag, sizeof (flag));
  1165. if (res == -1) {
  1166. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1167. "set mcast v6 TTL failed");
  1168. return (-1);
  1169. }
  1170. } else {
  1171. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  1172. &flag, sizeof(flag));
  1173. if (res == -1) {
  1174. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1175. "set mcast v4 TTL failed");
  1176. return (-1);
  1177. }
  1178. }
  1179. /*
  1180. * Bind to a specific interface for multicast send and receive
  1181. */
  1182. switch ( bindnet_address->family ) {
  1183. case AF_INET:
  1184. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  1185. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1186. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1187. "cannot select interface for multicast packets (send)");
  1188. return (-1);
  1189. }
  1190. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  1191. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1192. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1193. "cannot select interface for multicast packets (recv)");
  1194. return (-1);
  1195. }
  1196. break;
  1197. case AF_INET6:
  1198. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1199. &interface_num, sizeof (interface_num)) < 0) {
  1200. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1201. "cannot select interface for multicast packets (send v6)");
  1202. return (-1);
  1203. }
  1204. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1205. &interface_num, sizeof (interface_num)) < 0) {
  1206. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1207. "cannot select interface for multicast packets (recv v6)");
  1208. return (-1);
  1209. }
  1210. break;
  1211. }
  1212. return 0;
  1213. }
  1214. static int totemudp_build_sockets (
  1215. struct totemudp_instance *instance,
  1216. struct totem_ip_address *mcast_address,
  1217. struct totem_ip_address *bindnet_address,
  1218. struct totemudp_socket *sockets,
  1219. struct totem_ip_address *bound_to)
  1220. {
  1221. int interface_num;
  1222. int interface_up;
  1223. int res;
  1224. /*
  1225. * Determine the ip address bound to and the interface name
  1226. */
  1227. res = netif_determine (instance,
  1228. bindnet_address,
  1229. bound_to,
  1230. &interface_up,
  1231. &interface_num);
  1232. if (res == -1) {
  1233. return (-1);
  1234. }
  1235. totemip_copy(&instance->my_id, bound_to);
  1236. res = totemudp_build_sockets_ip (instance, mcast_address,
  1237. bindnet_address, sockets, bound_to, interface_num);
  1238. /* We only send out of the token socket */
  1239. totemudp_traffic_control_set(instance, sockets->token);
  1240. return res;
  1241. }
  1242. /*
  1243. * Totem Network interface - also does encryption/decryption
  1244. * depends on poll abstraction, POSIX, IPV4
  1245. */
  1246. /*
  1247. * Create an instance
  1248. */
  1249. int totemudp_initialize (
  1250. qb_loop_t *poll_handle,
  1251. void **udp_context,
  1252. struct totem_config *totem_config,
  1253. int interface_no,
  1254. void *context,
  1255. void (*deliver_fn) (
  1256. void *context,
  1257. const void *msg,
  1258. unsigned int msg_len),
  1259. void (*iface_change_fn) (
  1260. void *context,
  1261. const struct totem_ip_address *iface_address),
  1262. void (*target_set_completed) (
  1263. void *context))
  1264. {
  1265. struct totemudp_instance *instance;
  1266. instance = malloc (sizeof (struct totemudp_instance));
  1267. if (instance == NULL) {
  1268. return (-1);
  1269. }
  1270. totemudp_instance_initialize (instance);
  1271. instance->totem_config = totem_config;
  1272. /*
  1273. * Configure logging
  1274. */
  1275. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1276. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1277. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1278. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1279. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1280. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1281. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1282. /*
  1283. * Initialize random number generator for later use to generate salt
  1284. */
  1285. memcpy (instance->totemudp_private_key, totem_config->private_key,
  1286. totem_config->private_key_len);
  1287. instance->totemudp_private_key_len = totem_config->private_key_len;
  1288. init_crypto(instance);
  1289. /*
  1290. * Initialize local variables for totemudp
  1291. */
  1292. instance->totem_interface = &totem_config->interfaces[interface_no];
  1293. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1294. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1295. instance->totemudp_poll_handle = poll_handle;
  1296. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1297. instance->context = context;
  1298. instance->totemudp_deliver_fn = deliver_fn;
  1299. instance->totemudp_iface_change_fn = iface_change_fn;
  1300. instance->totemudp_target_set_completed = target_set_completed;
  1301. totemip_localhost (instance->mcast_address.family, &localhost);
  1302. localhost.nodeid = instance->totem_config->node_id;
  1303. /*
  1304. * RRP layer isn't ready to receive message because it hasn't
  1305. * initialized yet. Add short timer to check the interfaces.
  1306. */
  1307. qb_loop_timer_add (instance->totemudp_poll_handle,
  1308. QB_LOOP_MED,
  1309. 100*QB_TIME_NS_IN_MSEC,
  1310. (void *)instance,
  1311. timer_function_netif_check_timeout,
  1312. &instance->timer_netif_check_timeout);
  1313. *udp_context = instance;
  1314. return (0);
  1315. }
  1316. void *totemudp_buffer_alloc (void)
  1317. {
  1318. return malloc (FRAME_SIZE_MAX);
  1319. }
  1320. void totemudp_buffer_release (void *ptr)
  1321. {
  1322. return free (ptr);
  1323. }
  1324. int totemudp_processor_count_set (
  1325. void *udp_context,
  1326. int processor_count)
  1327. {
  1328. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1329. int res = 0;
  1330. instance->my_memb_entries = processor_count;
  1331. qb_loop_timer_del (instance->totemudp_poll_handle,
  1332. instance->timer_netif_check_timeout);
  1333. if (processor_count == 1) {
  1334. qb_loop_timer_add (instance->totemudp_poll_handle,
  1335. QB_LOOP_MED,
  1336. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1337. (void *)instance,
  1338. timer_function_netif_check_timeout,
  1339. &instance->timer_netif_check_timeout);
  1340. }
  1341. return (res);
  1342. }
  1343. int totemudp_recv_flush (void *udp_context)
  1344. {
  1345. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1346. struct pollfd ufd;
  1347. int nfds;
  1348. int res = 0;
  1349. instance->flushing = 1;
  1350. do {
  1351. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1352. ufd.events = POLLIN;
  1353. nfds = poll (&ufd, 1, 0);
  1354. if (nfds == 1 && ufd.revents & POLLIN) {
  1355. net_deliver_fn (instance->totemudp_sockets.mcast_recv,
  1356. ufd.revents, instance);
  1357. }
  1358. } while (nfds == 1);
  1359. instance->flushing = 0;
  1360. return (res);
  1361. }
  1362. int totemudp_send_flush (void *udp_context)
  1363. {
  1364. return 0;
  1365. }
  1366. int totemudp_token_send (
  1367. void *udp_context,
  1368. const void *msg,
  1369. unsigned int msg_len)
  1370. {
  1371. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1372. int res = 0;
  1373. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1374. return (res);
  1375. }
  1376. int totemudp_mcast_flush_send (
  1377. void *udp_context,
  1378. const void *msg,
  1379. unsigned int msg_len)
  1380. {
  1381. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1382. int res = 0;
  1383. mcast_sendmsg (instance, msg, msg_len);
  1384. return (res);
  1385. }
  1386. int totemudp_mcast_noflush_send (
  1387. void *udp_context,
  1388. const void *msg,
  1389. unsigned int msg_len)
  1390. {
  1391. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1392. int res = 0;
  1393. mcast_sendmsg (instance, msg, msg_len);
  1394. return (res);
  1395. }
  1396. extern int totemudp_iface_check (void *udp_context)
  1397. {
  1398. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1399. int res = 0;
  1400. timer_function_netif_check_timeout (instance);
  1401. return (res);
  1402. }
  1403. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1404. {
  1405. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1406. if (totem_config->secauth == 1) {
  1407. totem_config->net_mtu -= sizeof (struct security_header) +
  1408. UDPIP_HEADER_SIZE;
  1409. } else {
  1410. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1411. }
  1412. }
  1413. const char *totemudp_iface_print (void *udp_context) {
  1414. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1415. const char *ret_char;
  1416. ret_char = totemip_print (&instance->my_id);
  1417. return (ret_char);
  1418. }
  1419. int totemudp_iface_get (
  1420. void *udp_context,
  1421. struct totem_ip_address *addr)
  1422. {
  1423. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1424. int res = 0;
  1425. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1426. return (res);
  1427. }
  1428. int totemudp_token_target_set (
  1429. void *udp_context,
  1430. const struct totem_ip_address *token_target)
  1431. {
  1432. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1433. int res = 0;
  1434. memcpy (&instance->token_target, token_target,
  1435. sizeof (struct totem_ip_address));
  1436. instance->totemudp_target_set_completed (instance->context);
  1437. return (res);
  1438. }
  1439. extern int totemudp_recv_mcast_empty (
  1440. void *udp_context)
  1441. {
  1442. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1443. unsigned int res;
  1444. struct sockaddr_storage system_from;
  1445. struct msghdr msg_recv;
  1446. struct pollfd ufd;
  1447. int nfds;
  1448. int msg_processed = 0;
  1449. /*
  1450. * Receive datagram
  1451. */
  1452. msg_recv.msg_name = &system_from;
  1453. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1454. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1455. msg_recv.msg_iovlen = 1;
  1456. #if !defined(COROSYNC_SOLARIS)
  1457. msg_recv.msg_control = 0;
  1458. msg_recv.msg_controllen = 0;
  1459. msg_recv.msg_flags = 0;
  1460. #else
  1461. msg_recv.msg_accrights = NULL;
  1462. msg_recv.msg_accrightslen = 0;
  1463. #endif
  1464. do {
  1465. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1466. ufd.events = POLLIN;
  1467. nfds = poll (&ufd, 1, 0);
  1468. if (nfds == 1 && ufd.revents & POLLIN) {
  1469. res = recvmsg (instance->totemudp_sockets.mcast_recv, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1470. if (res != -1) {
  1471. msg_processed = 1;
  1472. } else {
  1473. msg_processed = -1;
  1474. }
  1475. }
  1476. } while (nfds == 1);
  1477. return (msg_processed);
  1478. }