totemudp.c 51 KB

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