totemudpu.c 38 KB

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