totemudp.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  1. /*
  2. * Copyright (c) 2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <assert.h>
  36. #include <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/list.h>
  59. #include <corosync/hdb.h>
  60. #include <corosync/swab.h>
  61. #include <corosync/totem/coropoll.h>
  62. #define LOGSYS_UTILS_ONLY 1
  63. #include <corosync/engine/logsys.h>
  64. #include "totemudp.h"
  65. #include "wthread.h"
  66. #include "crypto.h"
  67. #ifdef HAVE_LIBNSS
  68. #include <nss.h>
  69. #include <pk11pub.h>
  70. #include <pkcs11.h>
  71. #include <prerror.h>
  72. #endif
  73. #ifndef MSG_NOSIGNAL
  74. #define MSG_NOSIGNAL 0
  75. #endif
  76. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  77. #define NETIF_STATE_REPORT_UP 1
  78. #define NETIF_STATE_REPORT_DOWN 2
  79. #define BIND_STATE_UNBOUND 0
  80. #define BIND_STATE_REGULAR 1
  81. #define BIND_STATE_LOOPBACK 2
  82. #define HMAC_HASH_SIZE 20
  83. struct security_header {
  84. unsigned char hash_digest[HMAC_HASH_SIZE]; /* The hash *MUST* be first in the data structure */
  85. unsigned char salt[16]; /* random number */
  86. char msg[0];
  87. } __attribute__((packed));
  88. struct totemudp_mcast_thread_state {
  89. unsigned char iobuf[FRAME_SIZE_MAX];
  90. prng_state prng_state;
  91. };
  92. struct totemudp_socket {
  93. int mcast_recv;
  94. int mcast_send;
  95. int token;
  96. };
  97. struct totemudp_instance {
  98. hmac_state totemudp_hmac_state;
  99. prng_state totemudp_prng_state;
  100. #ifdef HAVE_LIBNSS
  101. PK11SymKey *nss_sym_key;
  102. PK11SymKey *nss_sym_key_sign;
  103. #endif
  104. unsigned char totemudp_private_key[1024];
  105. unsigned int totemudp_private_key_len;
  106. hdb_handle_t totemudp_poll_handle;
  107. struct totem_interface *totem_interface;
  108. int netif_state_report;
  109. int netif_bind_state;
  110. struct worker_thread_group worker_thread_group;
  111. void *context;
  112. void (*totemudp_deliver_fn) (
  113. void *context,
  114. const void *msg,
  115. unsigned int msg_len);
  116. void (*totemudp_iface_change_fn) (
  117. void *context,
  118. const struct totem_ip_address *iface_address);
  119. /*
  120. * Function and data used to log messages
  121. */
  122. int totemudp_log_level_security;
  123. int totemudp_log_level_error;
  124. int totemudp_log_level_warning;
  125. int totemudp_log_level_notice;
  126. int totemudp_log_level_debug;
  127. int totemudp_subsys_id;
  128. void (*totemudp_log_printf) (
  129. unsigned int rec_ident,
  130. const char *function,
  131. const char *file,
  132. int line,
  133. const char *format,
  134. ...)__attribute__((format(printf, 5, 6)));
  135. void *udp_context;
  136. char iov_buffer[FRAME_SIZE_MAX];
  137. char iov_buffer_flush[FRAME_SIZE_MAX];
  138. struct iovec totemudp_iov_recv;
  139. struct iovec totemudp_iov_recv_flush;
  140. struct totemudp_socket totemudp_sockets;
  141. struct totem_ip_address mcast_address;
  142. int stats_sent;
  143. int stats_recv;
  144. int stats_delv;
  145. int stats_remcasts;
  146. int stats_orf_token;
  147. struct timeval stats_tv_start;
  148. struct totem_ip_address my_id;
  149. int firstrun;
  150. poll_timer_handle timer_netif_check_timeout;
  151. unsigned int my_memb_entries;
  152. int flushing;
  153. struct totem_config *totem_config;
  154. struct totem_ip_address token_target;
  155. };
  156. struct work_item {
  157. const void *msg;
  158. unsigned int msg_len;
  159. struct totemudp_instance *instance;
  160. };
  161. static int totemudp_build_sockets (
  162. struct totemudp_instance *instance,
  163. struct totem_ip_address *bindnet_address,
  164. struct totem_ip_address *mcastaddress,
  165. struct totemudp_socket *sockets,
  166. struct totem_ip_address *bound_to);
  167. static struct totem_ip_address localhost;
  168. static void totemudp_instance_initialize (struct totemudp_instance *instance)
  169. {
  170. memset (instance, 0, sizeof (struct totemudp_instance));
  171. instance->netif_state_report = NETIF_STATE_REPORT_UP | NETIF_STATE_REPORT_DOWN;
  172. instance->totemudp_iov_recv.iov_base = instance->iov_buffer;
  173. instance->totemudp_iov_recv.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  174. instance->totemudp_iov_recv_flush.iov_base = instance->iov_buffer_flush;
  175. instance->totemudp_iov_recv_flush.iov_len = FRAME_SIZE_MAX; //sizeof (instance->iov_buffer);
  176. /*
  177. * There is always atleast 1 processor
  178. */
  179. instance->my_memb_entries = 1;
  180. }
  181. #define log_printf(level, format, args...) \
  182. do { \
  183. instance->totemudp_log_printf ( \
  184. LOGSYS_ENCODE_RECID(level, \
  185. instance->totemudp_subsys_id, \
  186. LOGSYS_RECID_LOG), \
  187. __FUNCTION__, __FILE__, __LINE__, \
  188. (const char *)format, ##args); \
  189. } while (0);
  190. static int authenticate_and_decrypt_sober (
  191. struct totemudp_instance *instance,
  192. struct iovec *iov,
  193. unsigned int iov_len)
  194. {
  195. unsigned char keys[48];
  196. struct security_header *header = (struct security_header *)iov[0].iov_base;
  197. prng_state keygen_prng_state;
  198. prng_state stream_prng_state;
  199. unsigned char *hmac_key = &keys[32];
  200. unsigned char *cipher_key = &keys[16];
  201. unsigned char *initial_vector = &keys[0];
  202. unsigned char digest_comparison[HMAC_HASH_SIZE];
  203. unsigned long len;
  204. /*
  205. * Generate MAC, CIPHER, IV keys from private key
  206. */
  207. memset (keys, 0, sizeof (keys));
  208. sober128_start (&keygen_prng_state);
  209. sober128_add_entropy (instance->totemudp_private_key,
  210. instance->totemudp_private_key_len, &keygen_prng_state);
  211. sober128_add_entropy (header->salt, sizeof (header->salt), &keygen_prng_state);
  212. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  213. /*
  214. * Setup stream cipher
  215. */
  216. sober128_start (&stream_prng_state);
  217. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  218. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  219. /*
  220. * Authenticate contents of message
  221. */
  222. hmac_init (&instance->totemudp_hmac_state, DIGEST_SHA1, hmac_key, 16);
  223. hmac_process (&instance->totemudp_hmac_state,
  224. (unsigned char *)iov->iov_base + HMAC_HASH_SIZE,
  225. iov->iov_len - HMAC_HASH_SIZE);
  226. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  227. assert (HMAC_HASH_SIZE >= len);
  228. hmac_done (&instance->totemudp_hmac_state, digest_comparison, &len);
  229. if (memcmp (digest_comparison, header->hash_digest, len) != 0) {
  230. return (-1);
  231. }
  232. /*
  233. * Decrypt the contents of the message with the cipher key
  234. */
  235. sober128_read ((unsigned char*)iov->iov_base +
  236. sizeof (struct security_header),
  237. iov->iov_len - sizeof (struct security_header),
  238. &stream_prng_state);
  239. return (0);
  240. }
  241. static void init_sober_crypto(
  242. struct totemudp_instance *instance)
  243. {
  244. log_printf(instance->totemudp_log_level_notice,
  245. "Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).\n");
  246. rng_make_prng (128, PRNG_SOBER, &instance->totemudp_prng_state, NULL);
  247. }
  248. #ifdef HAVE_LIBNSS
  249. static unsigned char *copy_from_iovec(
  250. const struct iovec *iov,
  251. unsigned int iov_len,
  252. size_t *buf_size)
  253. {
  254. int i;
  255. size_t bufptr;
  256. size_t buflen = 0;
  257. unsigned char *newbuf;
  258. for (i=0; i<iov_len; i++)
  259. buflen += iov[i].iov_len;
  260. newbuf = malloc(buflen);
  261. if (!newbuf)
  262. return NULL;
  263. bufptr=0;
  264. for (i=0; i<iov_len; i++) {
  265. memcpy(newbuf+bufptr, iov[i].iov_base, iov[i].iov_len);
  266. bufptr += iov[i].iov_len;
  267. }
  268. *buf_size = buflen;
  269. return newbuf;
  270. }
  271. static void copy_to_iovec(
  272. struct iovec *iov,
  273. unsigned int iov_len,
  274. const unsigned char *buf,
  275. size_t buf_size)
  276. {
  277. int i;
  278. size_t copylen;
  279. size_t bufptr = 0;
  280. bufptr=0;
  281. for (i=0; i<iov_len; i++) {
  282. copylen = iov[i].iov_len;
  283. if (bufptr + copylen > buf_size) {
  284. copylen = buf_size - bufptr;
  285. }
  286. memcpy(iov[i].iov_base, buf+bufptr, copylen);
  287. bufptr += copylen;
  288. if (iov[i].iov_len != copylen) {
  289. iov[i].iov_len = copylen;
  290. return;
  291. }
  292. }
  293. }
  294. static void init_nss_crypto(
  295. struct totemudp_instance *instance)
  296. {
  297. PK11SlotInfo* aes_slot = NULL;
  298. PK11SlotInfo* sha1_slot = NULL;
  299. SECItem key_item;
  300. SECStatus rv;
  301. log_printf(instance->totemudp_log_level_notice,
  302. "Initializing transmit/receive security: NSS AES128CBC/SHA1HMAC (mode 1).\n");
  303. rv = NSS_NoDB_Init(".");
  304. if (rv != SECSuccess)
  305. {
  306. log_printf(instance->totemudp_log_level_security, "NSS initialization failed (err %d)\n",
  307. PR_GetError());
  308. goto out;
  309. }
  310. aes_slot = PK11_GetBestSlot(instance->totem_config->crypto_crypt_type, NULL);
  311. if (aes_slot == NULL)
  312. {
  313. log_printf(instance->totemudp_log_level_security, "Unable to find security slot (err %d)\n",
  314. PR_GetError());
  315. goto out;
  316. }
  317. sha1_slot = PK11_GetBestSlot(CKM_SHA_1_HMAC, NULL);
  318. if (sha1_slot == NULL)
  319. {
  320. log_printf(instance->totemudp_log_level_security, "Unable to find security slot (err %d)\n",
  321. PR_GetError());
  322. goto out;
  323. }
  324. /*
  325. * Make the private key into a SymKey that we can use
  326. */
  327. key_item.type = siBuffer;
  328. key_item.data = instance->totem_config->private_key;
  329. key_item.len = 32; /* Use 128 bits */
  330. instance->nss_sym_key = PK11_ImportSymKey(aes_slot,
  331. instance->totem_config->crypto_crypt_type,
  332. PK11_OriginUnwrap, CKA_ENCRYPT|CKA_DECRYPT,
  333. &key_item, NULL);
  334. if (instance->nss_sym_key == NULL)
  335. {
  336. log_printf(instance->totemudp_log_level_security, "Failure to import key into NSS (err %d)\n",
  337. PR_GetError());
  338. goto out;
  339. }
  340. instance->nss_sym_key_sign = PK11_ImportSymKey(sha1_slot,
  341. CKM_SHA_1_HMAC,
  342. PK11_OriginUnwrap, CKA_SIGN,
  343. &key_item, NULL);
  344. if (instance->nss_sym_key_sign == NULL) {
  345. log_printf(instance->totemudp_log_level_security, "Failure to import key into NSS (err %d)\n",
  346. PR_GetError());
  347. goto out;
  348. }
  349. out:
  350. return;
  351. }
  352. static int encrypt_and_sign_nss (
  353. struct totemudp_instance *instance,
  354. unsigned char *buf,
  355. size_t *buf_len,
  356. const struct iovec *iovec,
  357. unsigned int iov_len)
  358. {
  359. PK11Context* enc_context = NULL;
  360. SECStatus rv1, rv2;
  361. int tmp1_outlen;
  362. unsigned int tmp2_outlen;
  363. unsigned char *inbuf;
  364. unsigned char *data;
  365. unsigned char *outdata;
  366. size_t datalen;
  367. SECItem no_params;
  368. SECItem iv_item;
  369. struct security_header *header;
  370. SECItem *nss_sec_param;
  371. unsigned char nss_iv_data[16];
  372. SECStatus rv;
  373. no_params.type = siBuffer;
  374. no_params.data = 0;
  375. no_params.len = 0;
  376. tmp1_outlen = tmp2_outlen = 0;
  377. inbuf = copy_from_iovec(iovec, iov_len, &datalen);
  378. if (!inbuf) {
  379. log_printf(instance->totemudp_log_level_security, "malloc error copying buffer from iovec\n");
  380. return -1;
  381. }
  382. data = inbuf + sizeof (struct security_header);
  383. datalen -= sizeof (struct security_header);
  384. outdata = buf + sizeof (struct security_header);
  385. header = (struct security_header *)buf;
  386. rv = PK11_GenerateRandom (
  387. nss_iv_data,
  388. sizeof (nss_iv_data));
  389. if (rv != SECSuccess) {
  390. log_printf(instance->totemudp_log_level_security,
  391. "Failure to generate a random number %d\n",
  392. PR_GetError());
  393. }
  394. memcpy(header->salt, nss_iv_data, sizeof(nss_iv_data));
  395. iv_item.type = siBuffer;
  396. iv_item.data = nss_iv_data;
  397. iv_item.len = sizeof (nss_iv_data);
  398. nss_sec_param = PK11_ParamFromIV (
  399. instance->totem_config->crypto_crypt_type,
  400. &iv_item);
  401. if (nss_sec_param == NULL) {
  402. log_printf(instance->totemudp_log_level_security,
  403. "Failure to set up PKCS11 param (err %d)\n",
  404. PR_GetError());
  405. goto out;
  406. }
  407. /*
  408. * Create cipher context for encryption
  409. */
  410. enc_context = PK11_CreateContextBySymKey (
  411. instance->totem_config->crypto_crypt_type,
  412. CKA_ENCRYPT,
  413. instance->nss_sym_key,
  414. nss_sec_param);
  415. if (!enc_context) {
  416. char err[1024];
  417. PR_GetErrorText(err);
  418. err[PR_GetErrorTextLength()] = 0;
  419. log_printf(instance->totemudp_log_level_security,
  420. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s\n",
  421. instance->totem_config->crypto_crypt_type,
  422. PR_GetError(), err);
  423. return -1;
  424. }
  425. rv1 = PK11_CipherOp(enc_context, outdata,
  426. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct security_header),
  427. data, datalen);
  428. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  429. FRAME_SIZE_MAX - tmp1_outlen);
  430. PK11_DestroyContext(enc_context, PR_TRUE);
  431. *buf_len = tmp1_outlen + tmp2_outlen;
  432. free(inbuf);
  433. // memcpy(&outdata[*buf_len], nss_iv_data, sizeof(nss_iv_data));
  434. if (rv1 != SECSuccess || rv2 != SECSuccess)
  435. goto out;
  436. /* Now do the digest */
  437. enc_context = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
  438. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  439. if (!enc_context) {
  440. char err[1024];
  441. PR_GetErrorText(err);
  442. err[PR_GetErrorTextLength()] = 0;
  443. log_printf(instance->totemudp_log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s\n",
  444. PR_GetError(), err);
  445. return -1;
  446. }
  447. PK11_DigestBegin(enc_context);
  448. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_len + 16);
  449. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  450. PK11_DestroyContext(enc_context, PR_TRUE);
  451. if (rv1 != SECSuccess || rv2 != SECSuccess)
  452. goto out;
  453. *buf_len = *buf_len + sizeof(struct security_header);
  454. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  455. return 0;
  456. out:
  457. return -1;
  458. }
  459. static int authenticate_and_decrypt_nss (
  460. struct totemudp_instance *instance,
  461. struct iovec *iov,
  462. unsigned int iov_len)
  463. {
  464. PK11Context* enc_context = NULL;
  465. SECStatus rv1, rv2;
  466. int tmp1_outlen;
  467. unsigned int tmp2_outlen;
  468. unsigned char outbuf[FRAME_SIZE_MAX];
  469. unsigned char digest[HMAC_HASH_SIZE];
  470. unsigned char *outdata;
  471. int result_len;
  472. unsigned char *data;
  473. unsigned char *inbuf;
  474. size_t datalen;
  475. struct security_header *header = (struct security_header *)iov[0].iov_base;
  476. SECItem no_params;
  477. SECItem ivdata;
  478. no_params.type = siBuffer;
  479. no_params.data = 0;
  480. no_params.len = 0;
  481. tmp1_outlen = tmp2_outlen = 0;
  482. if (iov_len > 1) {
  483. inbuf = copy_from_iovec(iov, iov_len, &datalen);
  484. if (!inbuf) {
  485. log_printf(instance->totemudp_log_level_security, "malloc error copying buffer from iovec\n");
  486. return -1;
  487. }
  488. }
  489. else {
  490. inbuf = (unsigned char *)iov[0].iov_base;
  491. datalen = iov[0].iov_len;
  492. }
  493. data = inbuf + sizeof (struct security_header) - 16;
  494. datalen = datalen - sizeof (struct security_header) + 16;
  495. outdata = outbuf + sizeof (struct security_header);
  496. /* Check the digest */
  497. enc_context = PK11_CreateContextBySymKey (
  498. CKM_SHA_1_HMAC, CKA_SIGN,
  499. instance->nss_sym_key_sign,
  500. &no_params);
  501. if (!enc_context) {
  502. char err[1024];
  503. PR_GetErrorText(err);
  504. err[PR_GetErrorTextLength()] = 0;
  505. log_printf(instance->totemudp_log_level_security, "PK11_CreateContext failed (check digest) err %d: %s\n",
  506. PR_GetError(), err);
  507. return -1;
  508. }
  509. PK11_DigestBegin(enc_context);
  510. rv1 = PK11_DigestOp(enc_context, data, datalen);
  511. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  512. PK11_DestroyContext(enc_context, PR_TRUE);
  513. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  514. log_printf(instance->totemudp_log_level_security, "Digest check failed\n");
  515. return -1;
  516. }
  517. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  518. log_printf(instance->totemudp_log_level_error, "Digest does not match\n");
  519. return -1;
  520. }
  521. /*
  522. * Get rid of salt
  523. */
  524. data += 16;
  525. datalen -= 16;
  526. /* Create cipher context for decryption */
  527. ivdata.type = siBuffer;
  528. ivdata.data = header->salt;
  529. ivdata.len = sizeof(header->salt);
  530. enc_context = PK11_CreateContextBySymKey(
  531. instance->totem_config->crypto_crypt_type,
  532. CKA_DECRYPT,
  533. instance->nss_sym_key, &ivdata);
  534. if (!enc_context) {
  535. log_printf(instance->totemudp_log_level_security,
  536. "PK11_CreateContext (decrypt) failed (err %d)\n",
  537. PR_GetError());
  538. return -1;
  539. }
  540. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  541. sizeof(outbuf) - sizeof (struct security_header),
  542. data, datalen);
  543. if (rv1 != SECSuccess) {
  544. log_printf(instance->totemudp_log_level_security,
  545. "PK11_CipherOp (decrypt) failed (err %d)\n",
  546. PR_GetError());
  547. }
  548. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  549. sizeof(outbuf) - tmp1_outlen);
  550. PK11_DestroyContext(enc_context, PR_TRUE);
  551. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct security_header);
  552. /* Copy it back to the buffer */
  553. copy_to_iovec(iov, iov_len, outbuf, result_len);
  554. if (iov_len > 1)
  555. free(inbuf);
  556. if (rv1 != SECSuccess || rv2 != SECSuccess)
  557. return -1;
  558. return 0;
  559. }
  560. #endif
  561. static int encrypt_and_sign_sober (
  562. struct totemudp_instance *instance,
  563. unsigned char *buf,
  564. size_t *buf_len,
  565. const struct iovec *iovec,
  566. unsigned int iov_len)
  567. {
  568. int i;
  569. unsigned char *addr;
  570. unsigned char keys[48];
  571. struct security_header *header;
  572. unsigned char *hmac_key = &keys[32];
  573. unsigned char *cipher_key = &keys[16];
  574. unsigned char *initial_vector = &keys[0];
  575. unsigned long len;
  576. size_t outlen = 0;
  577. hmac_state hmac_st;
  578. prng_state keygen_prng_state;
  579. prng_state stream_prng_state;
  580. prng_state *prng_state_in = &instance->totemudp_prng_state;
  581. header = (struct security_header *)buf;
  582. addr = buf + sizeof (struct security_header);
  583. memset (keys, 0, sizeof (keys));
  584. memset (header->salt, 0, sizeof (header->salt));
  585. /*
  586. * Generate MAC, CIPHER, IV keys from private key
  587. */
  588. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  589. sober128_start (&keygen_prng_state);
  590. sober128_add_entropy (instance->totemudp_private_key,
  591. instance->totemudp_private_key_len,
  592. &keygen_prng_state);
  593. sober128_add_entropy (header->salt, sizeof (header->salt),
  594. &keygen_prng_state);
  595. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  596. /*
  597. * Setup stream cipher
  598. */
  599. sober128_start (&stream_prng_state);
  600. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  601. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  602. outlen = sizeof (struct security_header);
  603. /*
  604. * Copy remainder of message, then encrypt it
  605. */
  606. for (i = 1; i < iov_len; i++) {
  607. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  608. addr += iovec[i].iov_len;
  609. outlen += iovec[i].iov_len;
  610. }
  611. /*
  612. * Encrypt message by XORing stream cipher data
  613. */
  614. sober128_read (buf + sizeof (struct security_header),
  615. outlen - sizeof (struct security_header),
  616. &stream_prng_state);
  617. memset (&hmac_st, 0, sizeof (hmac_st));
  618. /*
  619. * Sign the contents of the message with the hmac key and store signature in message
  620. */
  621. hmac_init (&hmac_st, DIGEST_SHA1, hmac_key, 16);
  622. hmac_process (&hmac_st,
  623. buf + HMAC_HASH_SIZE,
  624. outlen - HMAC_HASH_SIZE);
  625. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  626. hmac_done (&hmac_st, header->hash_digest, &len);
  627. *buf_len = outlen;
  628. return 0;
  629. }
  630. static int encrypt_and_sign_worker (
  631. struct totemudp_instance *instance,
  632. unsigned char *buf,
  633. size_t *buf_len,
  634. const struct iovec *iovec,
  635. unsigned int iov_len)
  636. {
  637. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_SOBER ||
  638. instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  639. return encrypt_and_sign_sober(instance, buf, buf_len, iovec, iov_len);
  640. #ifdef HAVE_LIBNSS
  641. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_NSS)
  642. return encrypt_and_sign_nss(instance, buf, buf_len, iovec, iov_len);
  643. #endif
  644. return -1;
  645. }
  646. static int authenticate_and_decrypt (
  647. struct totemudp_instance *instance,
  648. struct iovec *iov,
  649. unsigned int iov_len)
  650. {
  651. unsigned char type;
  652. unsigned char *endbuf = (unsigned char *)iov[iov_len-1].iov_base;
  653. int res = -1;
  654. /*
  655. * Get the encryption type and remove it from the buffer
  656. */
  657. type = endbuf[iov[iov_len-1].iov_len-1];
  658. iov[iov_len-1].iov_len -= 1;
  659. if (type == TOTEM_CRYPTO_SOBER)
  660. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  661. /*
  662. * Only try higher crypto options if NEW has been requested
  663. */
  664. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  665. #ifdef HAVE_LIBNSS
  666. if (type == TOTEM_CRYPTO_NSS)
  667. res = authenticate_and_decrypt_nss(instance, iov, iov_len);
  668. #endif
  669. }
  670. /*
  671. * If it failed, then try decrypting the whole packet as it might be
  672. * from aisexec
  673. */
  674. if (res == -1) {
  675. iov[iov_len-1].iov_len += 1;
  676. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  677. }
  678. return res;
  679. }
  680. static void init_crypto(
  681. struct totemudp_instance *instance)
  682. {
  683. /*
  684. * If we are expecting NEW crypto type then initialise all available
  685. * crypto options. For OLD then we only need SOBER128.
  686. */
  687. init_sober_crypto(instance);
  688. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  689. return;
  690. #ifdef HAVE_LIBNSS
  691. init_nss_crypto(instance);
  692. #endif
  693. }
  694. int totemudp_crypto_set (
  695. void *udp_context,
  696. unsigned int type)
  697. {
  698. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  699. int res = 0;
  700. /*
  701. * Can't set crypto type if OLD is selected
  702. */
  703. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD) {
  704. res = -1;
  705. } else {
  706. /*
  707. * Validate crypto algorithm
  708. */
  709. switch (type) {
  710. case TOTEM_CRYPTO_SOBER:
  711. log_printf(instance->totemudp_log_level_security,
  712. "Transmit security set to: libtomcrypt SOBER128/SHA1HMAC (mode 0)");
  713. break;
  714. case TOTEM_CRYPTO_NSS:
  715. log_printf(instance->totemudp_log_level_security,
  716. "Transmit security set to: NSS AES128CBC/SHA1HMAC (mode 1)");
  717. break;
  718. default:
  719. res = -1;
  720. break;
  721. }
  722. }
  723. return (res);
  724. }
  725. static inline void ucast_sendmsg (
  726. struct totemudp_instance *instance,
  727. struct totem_ip_address *system_to,
  728. const void *msg,
  729. unsigned int msg_len)
  730. {
  731. struct msghdr msg_ucast;
  732. int res = 0;
  733. size_t buf_len;
  734. unsigned char sheader[sizeof (struct security_header)];
  735. unsigned char encrypt_data[FRAME_SIZE_MAX];
  736. struct iovec iovec_encrypt[2];
  737. const struct iovec *iovec_sendmsg;
  738. struct sockaddr_storage sockaddr;
  739. struct iovec iovec;
  740. unsigned int iov_len;
  741. int addrlen;
  742. if (instance->totem_config->secauth == 1) {
  743. iovec_encrypt[0].iov_base = (void *)sheader;
  744. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  745. iovec_encrypt[1].iov_base = (void *)msg;
  746. iovec_encrypt[1].iov_len = msg_len;
  747. /*
  748. * Encrypt and digest the message
  749. */
  750. encrypt_and_sign_worker (
  751. instance,
  752. encrypt_data,
  753. &buf_len,
  754. iovec_encrypt,
  755. 2);
  756. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  757. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  758. }
  759. else {
  760. encrypt_data[buf_len++] = 0;
  761. }
  762. iovec_encrypt[0].iov_base = (void *)encrypt_data;
  763. iovec_encrypt[0].iov_len = buf_len;
  764. iovec_sendmsg = &iovec_encrypt[0];
  765. iov_len = 1;
  766. } else {
  767. iovec.iov_base = (void *)msg;
  768. iovec.iov_len = msg_len;
  769. iovec_sendmsg = &iovec;
  770. iov_len = 1;
  771. }
  772. /*
  773. * Build unicast message
  774. */
  775. totemip_totemip_to_sockaddr_convert(system_to,
  776. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  777. msg_ucast.msg_name = &sockaddr;
  778. msg_ucast.msg_namelen = addrlen;
  779. msg_ucast.msg_iov = (void *) iovec_sendmsg;
  780. msg_ucast.msg_iovlen = iov_len;
  781. #if !defined(COROSYNC_SOLARIS)
  782. msg_ucast.msg_control = 0;
  783. msg_ucast.msg_controllen = 0;
  784. msg_ucast.msg_flags = 0;
  785. #else
  786. msg_ucast.msg_accrights = NULL;
  787. msg_ucast.msg_accrightslen = 0;
  788. #endif
  789. /*
  790. * Transmit multicast message
  791. * An error here is recovered by totemsrp
  792. */
  793. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
  794. MSG_NOSIGNAL);
  795. }
  796. static inline void mcast_sendmsg (
  797. struct totemudp_instance *instance,
  798. const void *msg,
  799. unsigned int msg_len)
  800. {
  801. struct msghdr msg_mcast;
  802. int res = 0;
  803. size_t buf_len;
  804. unsigned char sheader[sizeof (struct security_header)];
  805. unsigned char encrypt_data[FRAME_SIZE_MAX];
  806. struct iovec iovec_encrypt[2];
  807. struct iovec iovec;
  808. const struct iovec *iovec_sendmsg;
  809. struct sockaddr_storage sockaddr;
  810. unsigned int iov_len;
  811. int addrlen;
  812. if (instance->totem_config->secauth == 1) {
  813. iovec_encrypt[0].iov_base = (void *)sheader;
  814. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  815. iovec_encrypt[1].iov_base = (void *)msg;
  816. iovec_encrypt[1].iov_len = msg_len;
  817. /*
  818. * Encrypt and digest the message
  819. */
  820. encrypt_and_sign_worker (
  821. instance,
  822. encrypt_data,
  823. &buf_len,
  824. iovec_encrypt,
  825. 2);
  826. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  827. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  828. }
  829. else {
  830. encrypt_data[buf_len++] = 0;
  831. }
  832. iovec_encrypt[0].iov_base = (void *)encrypt_data;
  833. iovec_encrypt[0].iov_len = buf_len;
  834. iovec_sendmsg = &iovec_encrypt[0];
  835. iov_len = 1;
  836. } else {
  837. iovec.iov_base = (void *)msg;
  838. iovec.iov_len = msg_len;
  839. iovec_sendmsg = &iovec;
  840. iov_len = 1;
  841. }
  842. /*
  843. * Build multicast message
  844. */
  845. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  846. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  847. msg_mcast.msg_name = &sockaddr;
  848. msg_mcast.msg_namelen = addrlen;
  849. msg_mcast.msg_iov = (void *) iovec_sendmsg;
  850. msg_mcast.msg_iovlen = iov_len;
  851. #if !defined(COROSYNC_SOLARIS)
  852. msg_mcast.msg_control = 0;
  853. msg_mcast.msg_controllen = 0;
  854. msg_mcast.msg_flags = 0;
  855. #else
  856. msg_mcast.msg_accrights = NULL;
  857. msg_mcast.msg_accrightslen = 0;
  858. #endif
  859. /*
  860. * Transmit multicast message
  861. * An error here is recovered by totemsrp
  862. */
  863. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  864. MSG_NOSIGNAL);
  865. }
  866. static void totemudp_mcast_thread_state_constructor (
  867. void *totemudp_mcast_thread_state_in)
  868. {
  869. struct totemudp_mcast_thread_state *totemudp_mcast_thread_state =
  870. (struct totemudp_mcast_thread_state *)totemudp_mcast_thread_state_in;
  871. memset (totemudp_mcast_thread_state, 0,
  872. sizeof (totemudp_mcast_thread_state));
  873. rng_make_prng (128, PRNG_SOBER,
  874. &totemudp_mcast_thread_state->prng_state, NULL);
  875. }
  876. static void totemudp_mcast_worker_fn (void *thread_state, void *work_item_in)
  877. {
  878. struct work_item *work_item = (struct work_item *)work_item_in;
  879. struct totemudp_mcast_thread_state *totemudp_mcast_thread_state =
  880. (struct totemudp_mcast_thread_state *)thread_state;
  881. struct totemudp_instance *instance = work_item->instance;
  882. struct msghdr msg_mcast;
  883. unsigned char sheader[sizeof (struct security_header)];
  884. int res = 0;
  885. size_t buf_len;
  886. struct iovec iovec_enc[2];
  887. struct iovec iovec;
  888. struct sockaddr_storage sockaddr;
  889. int addrlen;
  890. if (instance->totem_config->secauth == 1) {
  891. iovec_enc[0].iov_base = (void *)sheader;
  892. iovec_enc[0].iov_len = sizeof (struct security_header);
  893. iovec_enc[1].iov_base = (void *)work_item->msg;
  894. iovec_enc[1].iov_len = work_item->msg_len;
  895. /*
  896. * Encrypt and digest the message
  897. */
  898. encrypt_and_sign_worker (
  899. instance,
  900. totemudp_mcast_thread_state->iobuf,
  901. &buf_len,
  902. iovec_enc, 2);
  903. iovec.iov_base = (void *)totemudp_mcast_thread_state->iobuf;
  904. iovec.iov_len = buf_len;
  905. } else {
  906. iovec.iov_base = (void *)work_item->msg;
  907. iovec.iov_len = work_item->msg_len;
  908. }
  909. totemip_totemip_to_sockaddr_convert(&instance->mcast_address,
  910. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  911. msg_mcast.msg_name = &sockaddr;
  912. msg_mcast.msg_namelen = addrlen;
  913. msg_mcast.msg_iov = &iovec;
  914. msg_mcast.msg_iovlen = 1;
  915. #if !defined(COROSYNC_SOLARIS)
  916. msg_mcast.msg_control = 0;
  917. msg_mcast.msg_controllen = 0;
  918. msg_mcast.msg_flags = 0;
  919. #else
  920. msg_mcast.msg_accrights = NULL;
  921. msg_mcast.msg_accrightslen = 0;
  922. #endif
  923. /*
  924. * Transmit multicast message
  925. * An error here is recovered by totemudp
  926. */
  927. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_mcast,
  928. MSG_NOSIGNAL);
  929. }
  930. int totemudp_finalize (
  931. void *udp_context)
  932. {
  933. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  934. int res = 0;
  935. worker_thread_group_exit (&instance->worker_thread_group);
  936. if (instance->totemudp_sockets.mcast_recv > 0) {
  937. close (instance->totemudp_sockets.mcast_recv);
  938. poll_dispatch_delete (instance->totemudp_poll_handle,
  939. instance->totemudp_sockets.mcast_recv);
  940. }
  941. if (instance->totemudp_sockets.mcast_send > 0) {
  942. close (instance->totemudp_sockets.mcast_send);
  943. }
  944. if (instance->totemudp_sockets.token > 0) {
  945. close (instance->totemudp_sockets.token);
  946. poll_dispatch_delete (instance->totemudp_poll_handle,
  947. instance->totemudp_sockets.token);
  948. }
  949. return (res);
  950. }
  951. /*
  952. * Only designed to work with a message with one iov
  953. */
  954. static int net_deliver_fn (
  955. hdb_handle_t handle,
  956. int fd,
  957. int revents,
  958. void *data)
  959. {
  960. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  961. struct msghdr msg_recv;
  962. struct iovec *iovec;
  963. struct security_header *security_header;
  964. struct sockaddr_storage system_from;
  965. int bytes_received;
  966. int res = 0;
  967. unsigned char *msg_offset;
  968. unsigned int size_delv;
  969. if (instance->flushing == 1) {
  970. iovec = &instance->totemudp_iov_recv_flush;
  971. } else {
  972. iovec = &instance->totemudp_iov_recv;
  973. }
  974. /*
  975. * Receive datagram
  976. */
  977. msg_recv.msg_name = &system_from;
  978. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  979. msg_recv.msg_iov = iovec;
  980. msg_recv.msg_iovlen = 1;
  981. #if !defined(COROSYNC_SOLARIS)
  982. msg_recv.msg_control = 0;
  983. msg_recv.msg_controllen = 0;
  984. msg_recv.msg_flags = 0;
  985. #else
  986. msg_recv.msg_accrights = NULL;
  987. msg_recv.msg_accrightslen = 0;
  988. #endif
  989. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  990. if (bytes_received == -1) {
  991. return (0);
  992. } else {
  993. instance->stats_recv += bytes_received;
  994. }
  995. if ((instance->totem_config->secauth == 1) &&
  996. (bytes_received < sizeof (struct security_header))) {
  997. log_printf (instance->totemudp_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  998. return (0);
  999. }
  1000. security_header = (struct security_header *)iovec->iov_base;
  1001. iovec->iov_len = bytes_received;
  1002. if (instance->totem_config->secauth == 1) {
  1003. /*
  1004. * Authenticate and if authenticated, decrypt datagram
  1005. */
  1006. res = authenticate_and_decrypt (instance, iovec, 1);
  1007. if (res == -1) {
  1008. log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.\n");
  1009. log_printf (instance->totemudp_log_level_security,
  1010. "Invalid packet data\n");
  1011. iovec->iov_len = FRAME_SIZE_MAX;
  1012. return 0;
  1013. }
  1014. msg_offset = (unsigned char *)iovec->iov_base +
  1015. sizeof (struct security_header);
  1016. size_delv = bytes_received - sizeof (struct security_header);
  1017. } else {
  1018. msg_offset = (void *)iovec->iov_base;
  1019. size_delv = bytes_received;
  1020. }
  1021. /*
  1022. * Handle incoming message
  1023. */
  1024. instance->totemudp_deliver_fn (
  1025. instance->context,
  1026. msg_offset,
  1027. size_delv);
  1028. iovec->iov_len = FRAME_SIZE_MAX;
  1029. return (0);
  1030. }
  1031. static int netif_determine (
  1032. struct totemudp_instance *instance,
  1033. struct totem_ip_address *bindnet,
  1034. struct totem_ip_address *bound_to,
  1035. int *interface_up,
  1036. int *interface_num)
  1037. {
  1038. int res;
  1039. res = totemip_iface_check (bindnet, bound_to,
  1040. interface_up, interface_num,
  1041. instance->totem_config->clear_node_high_bit);
  1042. return (res);
  1043. }
  1044. /*
  1045. * If the interface is up, the sockets for totem are built. If the interface is down
  1046. * this function is requeued in the timer list to retry building the sockets later.
  1047. */
  1048. static void timer_function_netif_check_timeout (
  1049. void *data)
  1050. {
  1051. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  1052. int res;
  1053. int interface_up;
  1054. int interface_num;
  1055. struct totem_ip_address *bind_address;
  1056. /*
  1057. * Build sockets for every interface
  1058. */
  1059. netif_determine (instance,
  1060. &instance->totem_interface->bindnet,
  1061. &instance->totem_interface->boundto,
  1062. &interface_up, &interface_num);
  1063. /*
  1064. * If the network interface isn't back up and we are already
  1065. * in loopback mode, add timer to check again and return
  1066. */
  1067. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  1068. interface_up == 0) ||
  1069. (instance->my_memb_entries == 1 &&
  1070. instance->netif_bind_state == BIND_STATE_REGULAR &&
  1071. interface_up == 1)) {
  1072. poll_timer_add (instance->totemudp_poll_handle,
  1073. instance->totem_config->downcheck_timeout,
  1074. (void *)instance,
  1075. timer_function_netif_check_timeout,
  1076. &instance->timer_netif_check_timeout);
  1077. /*
  1078. * Add a timer to check for a downed regular interface
  1079. */
  1080. return;
  1081. }
  1082. if (instance->totemudp_sockets.mcast_recv > 0) {
  1083. close (instance->totemudp_sockets.mcast_recv);
  1084. poll_dispatch_delete (instance->totemudp_poll_handle,
  1085. instance->totemudp_sockets.mcast_recv);
  1086. }
  1087. if (instance->totemudp_sockets.mcast_send > 0) {
  1088. close (instance->totemudp_sockets.mcast_send);
  1089. }
  1090. if (instance->totemudp_sockets.token > 0) {
  1091. close (instance->totemudp_sockets.token);
  1092. poll_dispatch_delete (instance->totemudp_poll_handle,
  1093. instance->totemudp_sockets.token);
  1094. }
  1095. if (interface_up == 0) {
  1096. /*
  1097. * Interface is not up
  1098. */
  1099. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1100. bind_address = &localhost;
  1101. /*
  1102. * Add a timer to retry building interfaces and request memb_gather_enter
  1103. */
  1104. poll_timer_add (instance->totemudp_poll_handle,
  1105. instance->totem_config->downcheck_timeout,
  1106. (void *)instance,
  1107. timer_function_netif_check_timeout,
  1108. &instance->timer_netif_check_timeout);
  1109. } else {
  1110. /*
  1111. * Interface is up
  1112. */
  1113. instance->netif_bind_state = BIND_STATE_REGULAR;
  1114. bind_address = &instance->totem_interface->bindnet;
  1115. }
  1116. /*
  1117. * Create and bind the multicast and unicast sockets
  1118. */
  1119. res = totemudp_build_sockets (instance,
  1120. &instance->mcast_address,
  1121. bind_address,
  1122. &instance->totemudp_sockets,
  1123. &instance->totem_interface->boundto);
  1124. poll_dispatch_add (
  1125. instance->totemudp_poll_handle,
  1126. instance->totemudp_sockets.mcast_recv,
  1127. POLLIN, instance, net_deliver_fn);
  1128. poll_dispatch_add (
  1129. instance->totemudp_poll_handle,
  1130. instance->totemudp_sockets.token,
  1131. POLLIN, instance, net_deliver_fn);
  1132. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  1133. /*
  1134. * This reports changes in the interface to the user and totemsrp
  1135. */
  1136. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1137. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  1138. log_printf (instance->totemudp_log_level_notice,
  1139. "The network interface [%s] is now up.\n",
  1140. totemip_print (&instance->totem_interface->boundto));
  1141. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  1142. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  1143. }
  1144. /*
  1145. * Add a timer to check for interface going down in single membership
  1146. */
  1147. if (instance->my_memb_entries == 1) {
  1148. poll_timer_add (instance->totemudp_poll_handle,
  1149. instance->totem_config->downcheck_timeout,
  1150. (void *)instance,
  1151. timer_function_netif_check_timeout,
  1152. &instance->timer_netif_check_timeout);
  1153. }
  1154. } else {
  1155. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  1156. log_printf (instance->totemudp_log_level_notice,
  1157. "The network interface is down.\n");
  1158. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  1159. }
  1160. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  1161. }
  1162. }
  1163. /* Set the socket priority to INTERACTIVE to ensure
  1164. that our messages don't get queued behind anything else */
  1165. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  1166. {
  1167. #ifdef SO_PRIORITY
  1168. int prio = 6; /* TC_PRIO_INTERACTIVE */
  1169. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int)))
  1170. log_printf (instance->totemudp_log_level_warning, "Could not set traffic priority. (%s)\n", strerror (errno));
  1171. #endif
  1172. }
  1173. static int totemudp_build_sockets_ip (
  1174. struct totemudp_instance *instance,
  1175. struct totem_ip_address *mcast_address,
  1176. struct totem_ip_address *bindnet_address,
  1177. struct totemudp_socket *sockets,
  1178. struct totem_ip_address *bound_to,
  1179. int interface_num)
  1180. {
  1181. struct sockaddr_storage sockaddr;
  1182. struct ipv6_mreq mreq6;
  1183. struct ip_mreq mreq;
  1184. struct sockaddr_storage mcast_ss, boundto_ss;
  1185. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  1186. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  1187. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  1188. unsigned int sendbuf_size;
  1189. unsigned int recvbuf_size;
  1190. unsigned int optlen = sizeof (sendbuf_size);
  1191. int addrlen;
  1192. int res;
  1193. int flag;
  1194. /*
  1195. * Create multicast recv socket
  1196. */
  1197. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1198. if (sockets->mcast_recv == -1) {
  1199. perror ("socket");
  1200. return (-1);
  1201. }
  1202. totemip_nosigpipe (sockets->mcast_recv);
  1203. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  1204. if (res == -1) {
  1205. log_printf (instance->totemudp_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  1206. return (-1);
  1207. }
  1208. /*
  1209. * Force reuse
  1210. */
  1211. flag = 1;
  1212. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1213. perror("setsockopt reuseaddr");
  1214. return (-1);
  1215. }
  1216. /*
  1217. * Bind to multicast socket used for multicast receives
  1218. */
  1219. totemip_totemip_to_sockaddr_convert(mcast_address,
  1220. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1221. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  1222. if (res == -1) {
  1223. perror ("bind mcast recv socket failed");
  1224. return (-1);
  1225. }
  1226. /*
  1227. * Setup mcast send socket
  1228. */
  1229. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1230. if (sockets->mcast_send == -1) {
  1231. perror ("socket");
  1232. return (-1);
  1233. }
  1234. totemip_nosigpipe (sockets->mcast_send);
  1235. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  1236. if (res == -1) {
  1237. log_printf (instance->totemudp_log_level_warning, "Could not set non-blocking operation on multicast socket: %s\n", strerror (errno));
  1238. return (-1);
  1239. }
  1240. /*
  1241. * Force reuse
  1242. */
  1243. flag = 1;
  1244. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1245. perror("setsockopt reuseaddr");
  1246. return (-1);
  1247. }
  1248. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  1249. &sockaddr, &addrlen);
  1250. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  1251. if (res == -1) {
  1252. perror ("bind mcast send socket failed");
  1253. return (-1);
  1254. }
  1255. /*
  1256. * Setup unicast socket
  1257. */
  1258. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1259. if (sockets->token == -1) {
  1260. perror ("socket2");
  1261. return (-1);
  1262. }
  1263. totemip_nosigpipe (sockets->token);
  1264. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  1265. if (res == -1) {
  1266. log_printf (instance->totemudp_log_level_warning, "Could not set non-blocking operation on token socket: %s\n", strerror (errno));
  1267. return (-1);
  1268. }
  1269. /*
  1270. * Force reuse
  1271. */
  1272. flag = 1;
  1273. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1274. perror("setsockopt reuseaddr");
  1275. return (-1);
  1276. }
  1277. /*
  1278. * Bind to unicast socket used for token send/receives
  1279. * This has the side effect of binding to the correct interface
  1280. */
  1281. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1282. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  1283. if (res == -1) {
  1284. perror ("bind token socket failed");
  1285. return (-1);
  1286. }
  1287. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1288. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1289. /*
  1290. * Set buffer sizes to avoid overruns
  1291. */
  1292. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  1293. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  1294. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  1295. if (res == 0) {
  1296. log_printf (instance->totemudp_log_level_debug,
  1297. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  1298. }
  1299. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  1300. if (res == 0) {
  1301. log_printf (instance->totemudp_log_level_debug,
  1302. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  1303. }
  1304. /*
  1305. * Join group membership on socket
  1306. */
  1307. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  1308. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  1309. if (instance->totem_config->broadcast_use == 1) {
  1310. unsigned int broadcast = 1;
  1311. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  1312. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1313. perror("setting broadcast option");
  1314. return (-1);
  1315. }
  1316. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  1317. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1318. perror("setting broadcast option");
  1319. return (-1);
  1320. }
  1321. } else {
  1322. switch (bindnet_address->family) {
  1323. case AF_INET:
  1324. memset(&mreq, 0, sizeof(mreq));
  1325. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  1326. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  1327. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  1328. &mreq, sizeof (mreq));
  1329. if (res == -1) {
  1330. perror ("join ipv4 multicast group failed");
  1331. return (-1);
  1332. }
  1333. break;
  1334. case AF_INET6:
  1335. memset(&mreq6, 0, sizeof(mreq6));
  1336. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  1337. mreq6.ipv6mr_interface = interface_num;
  1338. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  1339. &mreq6, sizeof (mreq6));
  1340. if (res == -1) {
  1341. perror ("join ipv6 multicast group failed");
  1342. return (-1);
  1343. }
  1344. break;
  1345. }
  1346. }
  1347. /*
  1348. * Turn on multicast loopback
  1349. */
  1350. flag = 1;
  1351. switch ( bindnet_address->family ) {
  1352. case AF_INET:
  1353. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  1354. &flag, sizeof (flag));
  1355. break;
  1356. case AF_INET6:
  1357. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  1358. &flag, sizeof (flag));
  1359. }
  1360. if (res == -1) {
  1361. perror ("turn off loopback");
  1362. return (-1);
  1363. }
  1364. /*
  1365. * Set multicast packets TTL
  1366. */
  1367. if ( bindnet_address->family == AF_INET6 )
  1368. {
  1369. flag = 255;
  1370. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  1371. &flag, sizeof (flag));
  1372. if (res == -1) {
  1373. perror ("setp mcast hops");
  1374. return (-1);
  1375. }
  1376. }
  1377. /*
  1378. * Bind to a specific interface for multicast send and receive
  1379. */
  1380. switch ( bindnet_address->family ) {
  1381. case AF_INET:
  1382. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  1383. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1384. perror ("cannot select interface");
  1385. return (-1);
  1386. }
  1387. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  1388. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1389. perror ("cannot select interface");
  1390. return (-1);
  1391. }
  1392. break;
  1393. case AF_INET6:
  1394. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1395. &interface_num, sizeof (interface_num)) < 0) {
  1396. perror ("cannot select interface");
  1397. return (-1);
  1398. }
  1399. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1400. &interface_num, sizeof (interface_num)) < 0) {
  1401. perror ("cannot select interface");
  1402. return (-1);
  1403. }
  1404. break;
  1405. }
  1406. return 0;
  1407. }
  1408. static int totemudp_build_sockets (
  1409. struct totemudp_instance *instance,
  1410. struct totem_ip_address *mcast_address,
  1411. struct totem_ip_address *bindnet_address,
  1412. struct totemudp_socket *sockets,
  1413. struct totem_ip_address *bound_to)
  1414. {
  1415. int interface_num;
  1416. int interface_up;
  1417. int res;
  1418. /*
  1419. * Determine the ip address bound to and the interface name
  1420. */
  1421. res = netif_determine (instance,
  1422. bindnet_address,
  1423. bound_to,
  1424. &interface_up,
  1425. &interface_num);
  1426. if (res == -1) {
  1427. return (-1);
  1428. }
  1429. totemip_copy(&instance->my_id, bound_to);
  1430. res = totemudp_build_sockets_ip (instance, mcast_address,
  1431. bindnet_address, sockets, bound_to, interface_num);
  1432. /* We only send out of the token socket */
  1433. totemudp_traffic_control_set(instance, sockets->token);
  1434. return res;
  1435. }
  1436. /*
  1437. * Totem Network interface - also does encryption/decryption
  1438. * depends on poll abstraction, POSIX, IPV4
  1439. */
  1440. /*
  1441. * Create an instance
  1442. */
  1443. int totemudp_initialize (
  1444. hdb_handle_t poll_handle,
  1445. void **udp_context,
  1446. struct totem_config *totem_config,
  1447. int interface_no,
  1448. void *context,
  1449. void (*deliver_fn) (
  1450. void *context,
  1451. const void *msg,
  1452. unsigned int msg_len),
  1453. void (*iface_change_fn) (
  1454. void *context,
  1455. const struct totem_ip_address *iface_address))
  1456. {
  1457. struct totemudp_instance *instance;
  1458. instance = malloc (sizeof (struct totemudp_instance));
  1459. if (instance == NULL) {
  1460. return (-1);
  1461. }
  1462. totemudp_instance_initialize (instance);
  1463. instance->totem_config = totem_config;
  1464. /*
  1465. * Configure logging
  1466. */
  1467. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1468. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1469. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1470. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1471. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1472. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1473. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1474. /*
  1475. * Initialize random number generator for later use to generate salt
  1476. */
  1477. memcpy (instance->totemudp_private_key, totem_config->private_key,
  1478. totem_config->private_key_len);
  1479. instance->totemudp_private_key_len = totem_config->private_key_len;
  1480. init_crypto(instance);
  1481. /*
  1482. * Initialize local variables for totemudp
  1483. */
  1484. instance->totem_interface = &totem_config->interfaces[interface_no];
  1485. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1486. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1487. /*
  1488. * If threaded send requested, initialize thread group data structure
  1489. */
  1490. if (totem_config->threads) {
  1491. worker_thread_group_init (
  1492. &instance->worker_thread_group,
  1493. totem_config->threads, 128,
  1494. sizeof (struct work_item),
  1495. sizeof (struct totemudp_mcast_thread_state),
  1496. totemudp_mcast_thread_state_constructor,
  1497. totemudp_mcast_worker_fn);
  1498. }
  1499. instance->totemudp_poll_handle = poll_handle;
  1500. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1501. instance->context = context;
  1502. instance->totemudp_deliver_fn = deliver_fn;
  1503. instance->totemudp_iface_change_fn = iface_change_fn;
  1504. totemip_localhost (instance->mcast_address.family, &localhost);
  1505. /*
  1506. * RRP layer isn't ready to receive message because it hasn't
  1507. * initialized yet. Add short timer to check the interfaces.
  1508. */
  1509. poll_timer_add (instance->totemudp_poll_handle,
  1510. 100,
  1511. (void *)instance,
  1512. timer_function_netif_check_timeout,
  1513. &instance->timer_netif_check_timeout);
  1514. *udp_context = instance;
  1515. return (0);
  1516. }
  1517. int totemudp_processor_count_set (
  1518. void *udp_context,
  1519. int processor_count)
  1520. {
  1521. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1522. int res = 0;
  1523. instance->my_memb_entries = processor_count;
  1524. poll_timer_delete (instance->totemudp_poll_handle,
  1525. instance->timer_netif_check_timeout);
  1526. if (processor_count == 1) {
  1527. poll_timer_add (instance->totemudp_poll_handle,
  1528. instance->totem_config->downcheck_timeout,
  1529. (void *)instance,
  1530. timer_function_netif_check_timeout,
  1531. &instance->timer_netif_check_timeout);
  1532. }
  1533. return (res);
  1534. }
  1535. int totemudp_recv_flush (void *udp_context)
  1536. {
  1537. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1538. struct pollfd ufd;
  1539. int nfds;
  1540. int res = 0;
  1541. instance->flushing = 1;
  1542. do {
  1543. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1544. ufd.events = POLLIN;
  1545. nfds = poll (&ufd, 1, 0);
  1546. if (nfds == 1 && ufd.revents & POLLIN) {
  1547. net_deliver_fn (0, instance->totemudp_sockets.mcast_recv,
  1548. ufd.revents, instance);
  1549. }
  1550. } while (nfds == 1);
  1551. instance->flushing = 0;
  1552. return (res);
  1553. }
  1554. int totemudp_send_flush (void *udp_context)
  1555. {
  1556. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1557. int res = 0;
  1558. worker_thread_group_wait (&instance->worker_thread_group);
  1559. return (res);
  1560. }
  1561. int totemudp_token_send (
  1562. void *udp_context,
  1563. const void *msg,
  1564. unsigned int msg_len)
  1565. {
  1566. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1567. int res = 0;
  1568. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1569. return (res);
  1570. }
  1571. int totemudp_mcast_flush_send (
  1572. void *udp_context,
  1573. const void *msg,
  1574. unsigned int msg_len)
  1575. {
  1576. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1577. int res = 0;
  1578. mcast_sendmsg (instance, msg, msg_len);
  1579. return (res);
  1580. }
  1581. int totemudp_mcast_noflush_send (
  1582. void *udp_context,
  1583. const void *msg,
  1584. unsigned int msg_len)
  1585. {
  1586. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1587. struct work_item work_item;
  1588. int res = 0;
  1589. if (instance->totem_config->threads) {
  1590. work_item.msg = msg;
  1591. work_item.msg_len = msg_len;
  1592. work_item.instance = instance;
  1593. worker_thread_group_work_add (&instance->worker_thread_group,
  1594. &work_item);
  1595. } else {
  1596. mcast_sendmsg (instance, msg, msg_len);
  1597. }
  1598. return (res);
  1599. }
  1600. extern int totemudp_iface_check (void *udp_context)
  1601. {
  1602. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1603. int res = 0;
  1604. timer_function_netif_check_timeout (instance);
  1605. return (res);
  1606. }
  1607. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1608. {
  1609. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1610. if (totem_config->secauth == 1) {
  1611. totem_config->net_mtu -= sizeof (struct security_header) +
  1612. UDPIP_HEADER_SIZE;
  1613. } else {
  1614. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1615. }
  1616. }
  1617. const char *totemudp_iface_print (void *udp_context) {
  1618. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1619. const char *ret_char;
  1620. ret_char = totemip_print (&instance->my_id);
  1621. return (ret_char);
  1622. }
  1623. int totemudp_iface_get (
  1624. void *udp_context,
  1625. struct totem_ip_address *addr)
  1626. {
  1627. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1628. int res = 0;
  1629. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1630. return (res);
  1631. }
  1632. int totemudp_token_target_set (
  1633. void *udp_context,
  1634. const struct totem_ip_address *token_target)
  1635. {
  1636. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1637. int res = 0;
  1638. memcpy (&instance->token_target, token_target,
  1639. sizeof (struct totem_ip_address));
  1640. return (res);
  1641. }
  1642. extern int totemudp_recv_mcast_empty (
  1643. void *udp_context)
  1644. {
  1645. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1646. unsigned int res;
  1647. struct sockaddr_storage system_from;
  1648. struct msghdr msg_recv;
  1649. struct pollfd ufd;
  1650. int nfds;
  1651. int msg_processed = 0;
  1652. /*
  1653. * Receive datagram
  1654. */
  1655. msg_recv.msg_name = &system_from;
  1656. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1657. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1658. msg_recv.msg_iovlen = 1;
  1659. #if !defined(COROSYNC_SOLARIS)
  1660. msg_recv.msg_control = 0;
  1661. msg_recv.msg_controllen = 0;
  1662. msg_recv.msg_flags = 0;
  1663. #else
  1664. msg_recv.msg_accrights = NULL;
  1665. msg_recv.msg_accrightslen = 0;
  1666. #endif
  1667. do {
  1668. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1669. ufd.events = POLLIN;
  1670. nfds = poll (&ufd, 1, 0);
  1671. if (nfds == 1 && ufd.revents & POLLIN) {
  1672. res = recvmsg (instance->totemudp_sockets.mcast_recv, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1673. if (res != -1) {
  1674. msg_processed = 1;
  1675. } else {
  1676. msg_processed = -1;
  1677. }
  1678. }
  1679. } while (nfds == 1);
  1680. return (msg_processed);
  1681. }