totemudp.c 52 KB

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