totemudp.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  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 <corosync/hdb.h>
  61. #include <qb/qbdefs.h>
  62. #include <qb/qbloop.h>
  63. #define LOGSYS_UTILS_ONLY 1
  64. #include <corosync/engine/logsys.h>
  65. #include "totemudp.h"
  66. #include "crypto.h"
  67. #include "util.h"
  68. #ifdef HAVE_LIBNSS
  69. #include <nss.h>
  70. #include <pk11pub.h>
  71. #include <pkcs11.h>
  72. #include <prerror.h>
  73. #endif
  74. #ifndef MSG_NOSIGNAL
  75. #define MSG_NOSIGNAL 0
  76. #endif
  77. #define MCAST_SOCKET_BUFFER_SIZE (TRANSMITS_ALLOWED * FRAME_SIZE_MAX)
  78. #define NETIF_STATE_REPORT_UP 1
  79. #define NETIF_STATE_REPORT_DOWN 2
  80. #define BIND_STATE_UNBOUND 0
  81. #define BIND_STATE_REGULAR 1
  82. #define BIND_STATE_LOOPBACK 2
  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. 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. qb_loop_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. free (inbuf);
  407. return (-1);
  408. }
  409. /*
  410. * Create cipher context for encryption
  411. */
  412. enc_context = PK11_CreateContextBySymKey (
  413. instance->totem_config->crypto_crypt_type,
  414. CKA_ENCRYPT,
  415. instance->nss_sym_key,
  416. nss_sec_param);
  417. if (!enc_context) {
  418. char err[1024];
  419. PR_GetErrorText(err);
  420. err[PR_GetErrorTextLength()] = 0;
  421. log_printf(instance->totemudp_log_level_security,
  422. "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s\n",
  423. instance->totem_config->crypto_crypt_type,
  424. PR_GetError(), err);
  425. free(inbuf);
  426. return -1;
  427. }
  428. rv1 = PK11_CipherOp(enc_context, outdata,
  429. &tmp1_outlen, FRAME_SIZE_MAX - sizeof(struct security_header),
  430. data, datalen);
  431. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  432. FRAME_SIZE_MAX - tmp1_outlen);
  433. PK11_DestroyContext(enc_context, PR_TRUE);
  434. *buf_len = tmp1_outlen + tmp2_outlen;
  435. free(inbuf);
  436. // memcpy(&outdata[*buf_len], nss_iv_data, sizeof(nss_iv_data));
  437. if (rv1 != SECSuccess || rv2 != SECSuccess)
  438. goto out;
  439. /* Now do the digest */
  440. enc_context = PK11_CreateContextBySymKey(CKM_SHA_1_HMAC,
  441. CKA_SIGN, instance->nss_sym_key_sign, &no_params);
  442. if (!enc_context) {
  443. char err[1024];
  444. PR_GetErrorText(err);
  445. err[PR_GetErrorTextLength()] = 0;
  446. log_printf(instance->totemudp_log_level_security, "encrypt: PK11_CreateContext failed (digest) err %d: %s\n",
  447. PR_GetError(), err);
  448. return -1;
  449. }
  450. PK11_DigestBegin(enc_context);
  451. rv1 = PK11_DigestOp(enc_context, outdata - 16, *buf_len + 16);
  452. rv2 = PK11_DigestFinal(enc_context, header->hash_digest, &tmp2_outlen, sizeof(header->hash_digest));
  453. PK11_DestroyContext(enc_context, PR_TRUE);
  454. if (rv1 != SECSuccess || rv2 != SECSuccess)
  455. goto out;
  456. *buf_len = *buf_len + sizeof(struct security_header);
  457. SECITEM_FreeItem(nss_sec_param, PR_TRUE);
  458. return 0;
  459. out:
  460. return -1;
  461. }
  462. static int authenticate_and_decrypt_nss (
  463. struct totemudp_instance *instance,
  464. struct iovec *iov,
  465. unsigned int iov_len)
  466. {
  467. PK11Context* enc_context = NULL;
  468. SECStatus rv1, rv2;
  469. int tmp1_outlen;
  470. unsigned int tmp2_outlen;
  471. unsigned char outbuf[FRAME_SIZE_MAX];
  472. unsigned char digest[HMAC_HASH_SIZE];
  473. unsigned char *outdata;
  474. int result_len;
  475. unsigned char *data;
  476. unsigned char *inbuf;
  477. size_t datalen;
  478. struct security_header *header = (struct security_header *)iov[0].iov_base;
  479. SECItem no_params;
  480. SECItem ivdata;
  481. no_params.type = siBuffer;
  482. no_params.data = 0;
  483. no_params.len = 0;
  484. tmp1_outlen = tmp2_outlen = 0;
  485. if (iov_len > 1) {
  486. inbuf = copy_from_iovec(iov, iov_len, &datalen);
  487. if (!inbuf) {
  488. log_printf(instance->totemudp_log_level_security, "malloc error copying buffer from iovec\n");
  489. return -1;
  490. }
  491. }
  492. else {
  493. inbuf = (unsigned char *)iov[0].iov_base;
  494. datalen = iov[0].iov_len;
  495. }
  496. data = inbuf + sizeof (struct security_header) - 16;
  497. datalen = datalen - sizeof (struct security_header) + 16;
  498. outdata = outbuf + sizeof (struct security_header);
  499. /* Check the digest */
  500. enc_context = PK11_CreateContextBySymKey (
  501. CKM_SHA_1_HMAC, CKA_SIGN,
  502. instance->nss_sym_key_sign,
  503. &no_params);
  504. if (!enc_context) {
  505. char err[1024];
  506. PR_GetErrorText(err);
  507. err[PR_GetErrorTextLength()] = 0;
  508. log_printf(instance->totemudp_log_level_security, "PK11_CreateContext failed (check digest) err %d: %s\n",
  509. PR_GetError(), err);
  510. free (inbuf);
  511. return -1;
  512. }
  513. PK11_DigestBegin(enc_context);
  514. rv1 = PK11_DigestOp(enc_context, data, datalen);
  515. rv2 = PK11_DigestFinal(enc_context, digest, &tmp2_outlen, sizeof(digest));
  516. PK11_DestroyContext(enc_context, PR_TRUE);
  517. if (rv1 != SECSuccess || rv2 != SECSuccess) {
  518. log_printf(instance->totemudp_log_level_security, "Digest check failed\n");
  519. return -1;
  520. }
  521. if (memcmp(digest, header->hash_digest, tmp2_outlen) != 0) {
  522. log_printf(instance->totemudp_log_level_error, "Digest does not match\n");
  523. return -1;
  524. }
  525. /*
  526. * Get rid of salt
  527. */
  528. data += 16;
  529. datalen -= 16;
  530. /* Create cipher context for decryption */
  531. ivdata.type = siBuffer;
  532. ivdata.data = header->salt;
  533. ivdata.len = sizeof(header->salt);
  534. enc_context = PK11_CreateContextBySymKey(
  535. instance->totem_config->crypto_crypt_type,
  536. CKA_DECRYPT,
  537. instance->nss_sym_key, &ivdata);
  538. if (!enc_context) {
  539. log_printf(instance->totemudp_log_level_security,
  540. "PK11_CreateContext (decrypt) failed (err %d)\n",
  541. PR_GetError());
  542. return -1;
  543. }
  544. rv1 = PK11_CipherOp(enc_context, outdata, &tmp1_outlen,
  545. sizeof(outbuf) - sizeof (struct security_header),
  546. data, datalen);
  547. if (rv1 != SECSuccess) {
  548. log_printf(instance->totemudp_log_level_security,
  549. "PK11_CipherOp (decrypt) failed (err %d)\n",
  550. PR_GetError());
  551. }
  552. rv2 = PK11_DigestFinal(enc_context, outdata + tmp1_outlen, &tmp2_outlen,
  553. sizeof(outbuf) - tmp1_outlen);
  554. PK11_DestroyContext(enc_context, PR_TRUE);
  555. result_len = tmp1_outlen + tmp2_outlen + sizeof (struct security_header);
  556. /* Copy it back to the buffer */
  557. copy_to_iovec(iov, iov_len, outbuf, result_len);
  558. if (iov_len > 1)
  559. free(inbuf);
  560. if (rv1 != SECSuccess || rv2 != SECSuccess)
  561. return -1;
  562. return 0;
  563. }
  564. #endif
  565. static int encrypt_and_sign_sober (
  566. struct totemudp_instance *instance,
  567. unsigned char *buf,
  568. size_t *buf_len,
  569. const struct iovec *iovec,
  570. unsigned int iov_len)
  571. {
  572. int i;
  573. unsigned char *addr;
  574. unsigned char keys[48];
  575. struct security_header *header;
  576. unsigned char *hmac_key = &keys[32];
  577. unsigned char *cipher_key = &keys[16];
  578. unsigned char *initial_vector = &keys[0];
  579. unsigned long len;
  580. size_t outlen = 0;
  581. hmac_state hmac_st;
  582. prng_state keygen_prng_state;
  583. prng_state stream_prng_state;
  584. prng_state *prng_state_in = &instance->totemudp_prng_state;
  585. header = (struct security_header *)buf;
  586. addr = buf + sizeof (struct security_header);
  587. memset (keys, 0, sizeof (keys));
  588. memset (header->salt, 0, sizeof (header->salt));
  589. /*
  590. * Generate MAC, CIPHER, IV keys from private key
  591. */
  592. sober128_read (header->salt, sizeof (header->salt), prng_state_in);
  593. sober128_start (&keygen_prng_state);
  594. sober128_add_entropy (instance->totemudp_private_key,
  595. instance->totemudp_private_key_len,
  596. &keygen_prng_state);
  597. sober128_add_entropy (header->salt, sizeof (header->salt),
  598. &keygen_prng_state);
  599. sober128_read (keys, sizeof (keys), &keygen_prng_state);
  600. /*
  601. * Setup stream cipher
  602. */
  603. sober128_start (&stream_prng_state);
  604. sober128_add_entropy (cipher_key, 16, &stream_prng_state);
  605. sober128_add_entropy (initial_vector, 16, &stream_prng_state);
  606. outlen = sizeof (struct security_header);
  607. /*
  608. * Copy remainder of message, then encrypt it
  609. */
  610. for (i = 1; i < iov_len; i++) {
  611. memcpy (addr, iovec[i].iov_base, iovec[i].iov_len);
  612. addr += iovec[i].iov_len;
  613. outlen += iovec[i].iov_len;
  614. }
  615. /*
  616. * Encrypt message by XORing stream cipher data
  617. */
  618. sober128_read (buf + sizeof (struct security_header),
  619. outlen - sizeof (struct security_header),
  620. &stream_prng_state);
  621. memset (&hmac_st, 0, sizeof (hmac_st));
  622. /*
  623. * Sign the contents of the message with the hmac key and store signature in message
  624. */
  625. hmac_init (&hmac_st, DIGEST_SHA1, hmac_key, 16);
  626. hmac_process (&hmac_st,
  627. buf + HMAC_HASH_SIZE,
  628. outlen - HMAC_HASH_SIZE);
  629. len = hash_descriptor[DIGEST_SHA1]->hashsize;
  630. hmac_done (&hmac_st, header->hash_digest, &len);
  631. *buf_len = outlen;
  632. return 0;
  633. }
  634. static int encrypt_and_sign_worker (
  635. struct totemudp_instance *instance,
  636. unsigned char *buf,
  637. size_t *buf_len,
  638. const struct iovec *iovec,
  639. unsigned int iov_len)
  640. {
  641. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_SOBER ||
  642. instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  643. return encrypt_and_sign_sober(instance, buf, buf_len, iovec, iov_len);
  644. #ifdef HAVE_LIBNSS
  645. if (instance->totem_config->crypto_type == TOTEM_CRYPTO_NSS)
  646. return encrypt_and_sign_nss(instance, buf, buf_len, iovec, iov_len);
  647. #endif
  648. return -1;
  649. }
  650. static int authenticate_and_decrypt (
  651. struct totemudp_instance *instance,
  652. struct iovec *iov,
  653. unsigned int iov_len)
  654. {
  655. unsigned char type;
  656. unsigned char *endbuf = (unsigned char *)iov[iov_len-1].iov_base;
  657. int res = -1;
  658. /*
  659. * Get the encryption type and remove it from the buffer
  660. */
  661. type = endbuf[iov[iov_len-1].iov_len-1];
  662. iov[iov_len-1].iov_len -= 1;
  663. if (type == TOTEM_CRYPTO_SOBER)
  664. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  665. /*
  666. * Only try higher crypto options if NEW has been requested
  667. */
  668. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  669. #ifdef HAVE_LIBNSS
  670. if (type == TOTEM_CRYPTO_NSS)
  671. res = authenticate_and_decrypt_nss(instance, iov, iov_len);
  672. #endif
  673. }
  674. /*
  675. * If it failed, then try decrypting the whole packet as it might be
  676. * from aisexec
  677. */
  678. if (res == -1) {
  679. iov[iov_len-1].iov_len += 1;
  680. res = authenticate_and_decrypt_sober(instance, iov, iov_len);
  681. }
  682. return res;
  683. }
  684. static void init_crypto(
  685. struct totemudp_instance *instance)
  686. {
  687. /*
  688. * If we are expecting NEW crypto type then initialise all available
  689. * crypto options. For OLD then we only need SOBER128.
  690. */
  691. init_sober_crypto(instance);
  692. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD)
  693. return;
  694. #ifdef HAVE_LIBNSS
  695. init_nss_crypto(instance);
  696. #endif
  697. }
  698. int totemudp_crypto_set (
  699. void *udp_context,
  700. unsigned int type)
  701. {
  702. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  703. int res = 0;
  704. /*
  705. * Can't set crypto type if OLD is selected
  706. */
  707. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_OLD) {
  708. res = -1;
  709. } else {
  710. /*
  711. * Validate crypto algorithm
  712. */
  713. switch (type) {
  714. case TOTEM_CRYPTO_SOBER:
  715. log_printf(instance->totemudp_log_level_security,
  716. "Transmit security set to: libtomcrypt SOBER128/SHA1HMAC (mode 0)");
  717. break;
  718. case TOTEM_CRYPTO_NSS:
  719. log_printf(instance->totemudp_log_level_security,
  720. "Transmit security set to: NSS AES128CBC/SHA1HMAC (mode 1)");
  721. break;
  722. default:
  723. res = -1;
  724. break;
  725. }
  726. }
  727. return (res);
  728. }
  729. static inline void ucast_sendmsg (
  730. struct totemudp_instance *instance,
  731. struct totem_ip_address *system_to,
  732. const void *msg,
  733. unsigned int msg_len)
  734. {
  735. struct msghdr msg_ucast;
  736. int res = 0;
  737. size_t buf_len;
  738. unsigned char sheader[sizeof (struct security_header)];
  739. unsigned char encrypt_data[FRAME_SIZE_MAX];
  740. struct iovec iovec_encrypt[2];
  741. const struct iovec *iovec_sendmsg;
  742. struct sockaddr_storage sockaddr;
  743. struct iovec iovec;
  744. unsigned int iov_len;
  745. int addrlen;
  746. if (instance->totem_config->secauth == 1) {
  747. iovec_encrypt[0].iov_base = (void *)sheader;
  748. iovec_encrypt[0].iov_len = sizeof (struct security_header);
  749. iovec_encrypt[1].iov_base = (void *)msg;
  750. iovec_encrypt[1].iov_len = msg_len;
  751. /*
  752. * Encrypt and digest the message
  753. */
  754. encrypt_and_sign_worker (
  755. instance,
  756. encrypt_data,
  757. &buf_len,
  758. iovec_encrypt,
  759. 2);
  760. if (instance->totem_config->crypto_accept == TOTEM_CRYPTO_ACCEPT_NEW) {
  761. encrypt_data[buf_len++] = instance->totem_config->crypto_type;
  762. }
  763. else {
  764. encrypt_data[buf_len++] = 0;
  765. }
  766. iovec_encrypt[0].iov_base = (void *)encrypt_data;
  767. iovec_encrypt[0].iov_len = buf_len;
  768. iovec_sendmsg = &iovec_encrypt[0];
  769. iov_len = 1;
  770. } else {
  771. iovec.iov_base = (void *)msg;
  772. iovec.iov_len = msg_len;
  773. iovec_sendmsg = &iovec;
  774. iov_len = 1;
  775. }
  776. /*
  777. * Build unicast message
  778. */
  779. totemip_totemip_to_sockaddr_convert(system_to,
  780. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  781. msg_ucast.msg_name = &sockaddr;
  782. msg_ucast.msg_namelen = addrlen;
  783. msg_ucast.msg_iov = (void *) iovec_sendmsg;
  784. msg_ucast.msg_iovlen = iov_len;
  785. #if !defined(COROSYNC_SOLARIS)
  786. msg_ucast.msg_control = 0;
  787. msg_ucast.msg_controllen = 0;
  788. msg_ucast.msg_flags = 0;
  789. #else
  790. msg_ucast.msg_accrights = NULL;
  791. msg_ucast.msg_accrightslen = 0;
  792. #endif
  793. /*
  794. * Transmit unicast message
  795. * An error here is recovered by totemsrp
  796. */
  797. res = sendmsg (instance->totemudp_sockets.mcast_send, &msg_ucast,
  798. MSG_NOSIGNAL);
  799. if (res < 0) {
  800. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  801. "sendmsg(ucast) failed (non-critical)");
  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. LOGSYS_PERROR (errno, instance->totemudp_log_level_debug,
  875. "sendmsg(mcast) failed (non-critical)");
  876. }
  877. }
  878. int totemudp_finalize (
  879. void *udp_context)
  880. {
  881. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  882. int res = 0;
  883. if (instance->totemudp_sockets.mcast_recv > 0) {
  884. close (instance->totemudp_sockets.mcast_recv);
  885. qb_loop_poll_del (instance->totemudp_poll_handle,
  886. instance->totemudp_sockets.mcast_recv);
  887. }
  888. if (instance->totemudp_sockets.mcast_send > 0) {
  889. close (instance->totemudp_sockets.mcast_send);
  890. }
  891. if (instance->totemudp_sockets.token > 0) {
  892. close (instance->totemudp_sockets.token);
  893. qb_loop_poll_del (instance->totemudp_poll_handle,
  894. instance->totemudp_sockets.token);
  895. }
  896. return (res);
  897. }
  898. /*
  899. * Only designed to work with a message with one iov
  900. */
  901. static int net_deliver_fn (
  902. int fd,
  903. int revents,
  904. void *data)
  905. {
  906. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  907. struct msghdr msg_recv;
  908. struct iovec *iovec;
  909. struct sockaddr_storage system_from;
  910. int bytes_received;
  911. int res = 0;
  912. unsigned char *msg_offset;
  913. unsigned int size_delv;
  914. if (instance->flushing == 1) {
  915. iovec = &instance->totemudp_iov_recv_flush;
  916. } else {
  917. iovec = &instance->totemudp_iov_recv;
  918. }
  919. /*
  920. * Receive datagram
  921. */
  922. msg_recv.msg_name = &system_from;
  923. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  924. msg_recv.msg_iov = iovec;
  925. msg_recv.msg_iovlen = 1;
  926. #if !defined(COROSYNC_SOLARIS)
  927. msg_recv.msg_control = 0;
  928. msg_recv.msg_controllen = 0;
  929. msg_recv.msg_flags = 0;
  930. #else
  931. msg_recv.msg_accrights = NULL;
  932. msg_recv.msg_accrightslen = 0;
  933. #endif
  934. bytes_received = recvmsg (fd, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  935. if (bytes_received == -1) {
  936. return (0);
  937. } else {
  938. instance->stats_recv += bytes_received;
  939. }
  940. if ((instance->totem_config->secauth == 1) &&
  941. (bytes_received < sizeof (struct security_header))) {
  942. log_printf (instance->totemudp_log_level_security, "Received message is too short... ignoring %d.\n", bytes_received);
  943. return (0);
  944. }
  945. iovec->iov_len = bytes_received;
  946. if (instance->totem_config->secauth == 1) {
  947. /*
  948. * Authenticate and if authenticated, decrypt datagram
  949. */
  950. res = authenticate_and_decrypt (instance, iovec, 1);
  951. if (res == -1) {
  952. log_printf (instance->totemudp_log_level_security, "Received message has invalid digest... ignoring.\n");
  953. log_printf (instance->totemudp_log_level_security,
  954. "Invalid packet data\n");
  955. iovec->iov_len = FRAME_SIZE_MAX;
  956. return 0;
  957. }
  958. msg_offset = (unsigned char *)iovec->iov_base +
  959. sizeof (struct security_header);
  960. size_delv = bytes_received - sizeof (struct security_header);
  961. } else {
  962. msg_offset = (void *)iovec->iov_base;
  963. size_delv = bytes_received;
  964. }
  965. /*
  966. * Handle incoming message
  967. */
  968. instance->totemudp_deliver_fn (
  969. instance->context,
  970. msg_offset,
  971. size_delv);
  972. iovec->iov_len = FRAME_SIZE_MAX;
  973. return (0);
  974. }
  975. static int netif_determine (
  976. struct totemudp_instance *instance,
  977. struct totem_ip_address *bindnet,
  978. struct totem_ip_address *bound_to,
  979. int *interface_up,
  980. int *interface_num)
  981. {
  982. int res;
  983. res = totemip_iface_check (bindnet, bound_to,
  984. interface_up, interface_num,
  985. instance->totem_config->clear_node_high_bit);
  986. return (res);
  987. }
  988. /*
  989. * If the interface is up, the sockets for totem are built. If the interface is down
  990. * this function is requeued in the timer list to retry building the sockets later.
  991. */
  992. static void timer_function_netif_check_timeout (
  993. void *data)
  994. {
  995. struct totemudp_instance *instance = (struct totemudp_instance *)data;
  996. int interface_up;
  997. int interface_num;
  998. struct totem_ip_address *bind_address;
  999. /*
  1000. * Build sockets for every interface
  1001. */
  1002. netif_determine (instance,
  1003. &instance->totem_interface->bindnet,
  1004. &instance->totem_interface->boundto,
  1005. &interface_up, &interface_num);
  1006. /*
  1007. * If the network interface isn't back up and we are already
  1008. * in loopback mode, add timer to check again and return
  1009. */
  1010. if ((instance->netif_bind_state == BIND_STATE_LOOPBACK &&
  1011. interface_up == 0) ||
  1012. (instance->my_memb_entries == 1 &&
  1013. instance->netif_bind_state == BIND_STATE_REGULAR &&
  1014. interface_up == 1)) {
  1015. qb_loop_timer_add (instance->totemudp_poll_handle,
  1016. QB_LOOP_MED,
  1017. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1018. (void *)instance,
  1019. timer_function_netif_check_timeout,
  1020. &instance->timer_netif_check_timeout);
  1021. /*
  1022. * Add a timer to check for a downed regular interface
  1023. */
  1024. return;
  1025. }
  1026. if (instance->totemudp_sockets.mcast_recv > 0) {
  1027. close (instance->totemudp_sockets.mcast_recv);
  1028. qb_loop_poll_del (instance->totemudp_poll_handle,
  1029. instance->totemudp_sockets.mcast_recv);
  1030. }
  1031. if (instance->totemudp_sockets.mcast_send > 0) {
  1032. close (instance->totemudp_sockets.mcast_send);
  1033. }
  1034. if (instance->totemudp_sockets.token > 0) {
  1035. close (instance->totemudp_sockets.token);
  1036. qb_loop_poll_del (instance->totemudp_poll_handle,
  1037. instance->totemudp_sockets.token);
  1038. }
  1039. if (interface_up == 0) {
  1040. /*
  1041. * Interface is not up
  1042. */
  1043. instance->netif_bind_state = BIND_STATE_LOOPBACK;
  1044. bind_address = &localhost;
  1045. /*
  1046. * Add a timer to retry building interfaces and request memb_gather_enter
  1047. */
  1048. qb_loop_timer_add (instance->totemudp_poll_handle,
  1049. QB_LOOP_MED,
  1050. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1051. (void *)instance,
  1052. timer_function_netif_check_timeout,
  1053. &instance->timer_netif_check_timeout);
  1054. } else {
  1055. /*
  1056. * Interface is up
  1057. */
  1058. instance->netif_bind_state = BIND_STATE_REGULAR;
  1059. bind_address = &instance->totem_interface->bindnet;
  1060. }
  1061. /*
  1062. * Create and bind the multicast and unicast sockets
  1063. */
  1064. (void)totemudp_build_sockets (instance,
  1065. &instance->mcast_address,
  1066. bind_address,
  1067. &instance->totemudp_sockets,
  1068. &instance->totem_interface->boundto);
  1069. qb_loop_poll_add (
  1070. instance->totemudp_poll_handle,
  1071. QB_LOOP_MED,
  1072. instance->totemudp_sockets.mcast_recv,
  1073. POLLIN, instance, net_deliver_fn);
  1074. qb_loop_poll_add (
  1075. instance->totemudp_poll_handle,
  1076. QB_LOOP_MED,
  1077. instance->totemudp_sockets.token,
  1078. POLLIN, instance, net_deliver_fn);
  1079. totemip_copy (&instance->my_id, &instance->totem_interface->boundto);
  1080. /*
  1081. * This reports changes in the interface to the user and totemsrp
  1082. */
  1083. if (instance->netif_bind_state == BIND_STATE_REGULAR) {
  1084. if (instance->netif_state_report & NETIF_STATE_REPORT_UP) {
  1085. log_printf (instance->totemudp_log_level_notice,
  1086. "The network interface [%s] is now up.\n",
  1087. totemip_print (&instance->totem_interface->boundto));
  1088. instance->netif_state_report = NETIF_STATE_REPORT_DOWN;
  1089. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  1090. }
  1091. /*
  1092. * Add a timer to check for interface going down in single membership
  1093. */
  1094. if (instance->my_memb_entries == 1) {
  1095. qb_loop_timer_add (instance->totemudp_poll_handle,
  1096. QB_LOOP_MED,
  1097. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1098. (void *)instance,
  1099. timer_function_netif_check_timeout,
  1100. &instance->timer_netif_check_timeout);
  1101. }
  1102. } else {
  1103. if (instance->netif_state_report & NETIF_STATE_REPORT_DOWN) {
  1104. log_printf (instance->totemudp_log_level_notice,
  1105. "The network interface is down.\n");
  1106. instance->totemudp_iface_change_fn (instance->context, &instance->my_id);
  1107. }
  1108. instance->netif_state_report = NETIF_STATE_REPORT_UP;
  1109. }
  1110. }
  1111. /* Set the socket priority to INTERACTIVE to ensure
  1112. that our messages don't get queued behind anything else */
  1113. static void totemudp_traffic_control_set(struct totemudp_instance *instance, int sock)
  1114. {
  1115. #ifdef SO_PRIORITY
  1116. int prio = 6; /* TC_PRIO_INTERACTIVE */
  1117. if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(int))) {
  1118. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning, "Could not set traffic priority");
  1119. }
  1120. #endif
  1121. }
  1122. static int totemudp_build_sockets_ip (
  1123. struct totemudp_instance *instance,
  1124. struct totem_ip_address *mcast_address,
  1125. struct totem_ip_address *bindnet_address,
  1126. struct totemudp_socket *sockets,
  1127. struct totem_ip_address *bound_to,
  1128. int interface_num)
  1129. {
  1130. struct sockaddr_storage sockaddr;
  1131. struct ipv6_mreq mreq6;
  1132. struct ip_mreq mreq;
  1133. struct sockaddr_storage mcast_ss, boundto_ss;
  1134. struct sockaddr_in6 *mcast_sin6 = (struct sockaddr_in6 *)&mcast_ss;
  1135. struct sockaddr_in *mcast_sin = (struct sockaddr_in *)&mcast_ss;
  1136. struct sockaddr_in *boundto_sin = (struct sockaddr_in *)&boundto_ss;
  1137. unsigned int sendbuf_size;
  1138. unsigned int recvbuf_size;
  1139. unsigned int optlen = sizeof (sendbuf_size);
  1140. int addrlen;
  1141. int res;
  1142. int flag;
  1143. /*
  1144. * Create multicast recv socket
  1145. */
  1146. sockets->mcast_recv = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1147. if (sockets->mcast_recv == -1) {
  1148. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1149. "socket() failed");
  1150. return (-1);
  1151. }
  1152. totemip_nosigpipe (sockets->mcast_recv);
  1153. res = fcntl (sockets->mcast_recv, F_SETFL, O_NONBLOCK);
  1154. if (res == -1) {
  1155. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1156. "Could not set non-blocking operation on multicast socket");
  1157. return (-1);
  1158. }
  1159. /*
  1160. * Force reuse
  1161. */
  1162. flag = 1;
  1163. if ( setsockopt(sockets->mcast_recv, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1164. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1165. "setsockopt(SO_REUSEADDR) failed");
  1166. return (-1);
  1167. }
  1168. /*
  1169. * Bind to multicast socket used for multicast receives
  1170. */
  1171. totemip_totemip_to_sockaddr_convert(mcast_address,
  1172. instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1173. res = bind (sockets->mcast_recv, (struct sockaddr *)&sockaddr, addrlen);
  1174. if (res == -1) {
  1175. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1176. "Unable to bind the socket to receive multicast packets");
  1177. return (-1);
  1178. }
  1179. /*
  1180. * Setup mcast send socket
  1181. */
  1182. sockets->mcast_send = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1183. if (sockets->mcast_send == -1) {
  1184. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1185. "socket() failed");
  1186. return (-1);
  1187. }
  1188. totemip_nosigpipe (sockets->mcast_send);
  1189. res = fcntl (sockets->mcast_send, F_SETFL, O_NONBLOCK);
  1190. if (res == -1) {
  1191. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1192. "Could not set non-blocking operation on multicast socket");
  1193. return (-1);
  1194. }
  1195. /*
  1196. * Force reuse
  1197. */
  1198. flag = 1;
  1199. if ( setsockopt(sockets->mcast_send, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1200. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1201. "setsockopt(SO_REUSEADDR) failed");
  1202. return (-1);
  1203. }
  1204. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port - 1,
  1205. &sockaddr, &addrlen);
  1206. res = bind (sockets->mcast_send, (struct sockaddr *)&sockaddr, addrlen);
  1207. if (res == -1) {
  1208. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1209. "Unable to bind the socket to send multicast packets");
  1210. return (-1);
  1211. }
  1212. /*
  1213. * Setup unicast socket
  1214. */
  1215. sockets->token = socket (bindnet_address->family, SOCK_DGRAM, 0);
  1216. if (sockets->token == -1) {
  1217. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1218. "socket() failed");
  1219. return (-1);
  1220. }
  1221. totemip_nosigpipe (sockets->token);
  1222. res = fcntl (sockets->token, F_SETFL, O_NONBLOCK);
  1223. if (res == -1) {
  1224. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1225. "Could not set non-blocking operation on token socket");
  1226. return (-1);
  1227. }
  1228. /*
  1229. * Force reuse
  1230. */
  1231. flag = 1;
  1232. if ( setsockopt(sockets->token, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof (flag)) < 0) {
  1233. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1234. "setsockopt(SO_REUSEADDR) failed");
  1235. return (-1);
  1236. }
  1237. /*
  1238. * Bind to unicast socket used for token send/receives
  1239. * This has the side effect of binding to the correct interface
  1240. */
  1241. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &sockaddr, &addrlen);
  1242. res = bind (sockets->token, (struct sockaddr *)&sockaddr, addrlen);
  1243. if (res == -1) {
  1244. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1245. "Unable to bind UDP unicast socket");
  1246. return (-1);
  1247. }
  1248. recvbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1249. sendbuf_size = MCAST_SOCKET_BUFFER_SIZE;
  1250. /*
  1251. * Set buffer sizes to avoid overruns
  1252. */
  1253. res = setsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, optlen);
  1254. res = setsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, optlen);
  1255. res = getsockopt (sockets->mcast_recv, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, &optlen);
  1256. if (res == 0) {
  1257. log_printf (instance->totemudp_log_level_debug,
  1258. "Receive multicast socket recv buffer size (%d bytes).\n", recvbuf_size);
  1259. }
  1260. res = getsockopt (sockets->mcast_send, SOL_SOCKET, SO_SNDBUF, &sendbuf_size, &optlen);
  1261. if (res == 0) {
  1262. log_printf (instance->totemudp_log_level_debug,
  1263. "Transmit multicast socket send buffer size (%d bytes).\n", sendbuf_size);
  1264. }
  1265. /*
  1266. * Join group membership on socket
  1267. */
  1268. totemip_totemip_to_sockaddr_convert(mcast_address, instance->totem_interface->ip_port, &mcast_ss, &addrlen);
  1269. totemip_totemip_to_sockaddr_convert(bound_to, instance->totem_interface->ip_port, &boundto_ss, &addrlen);
  1270. if (instance->totem_config->broadcast_use == 1) {
  1271. unsigned int broadcast = 1;
  1272. if ((setsockopt(sockets->mcast_recv, SOL_SOCKET,
  1273. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1274. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1275. "setting broadcast option failed");
  1276. return (-1);
  1277. }
  1278. if ((setsockopt(sockets->mcast_send, SOL_SOCKET,
  1279. SO_BROADCAST, &broadcast, sizeof (broadcast))) == -1) {
  1280. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1281. "setting broadcast option failed");
  1282. return (-1);
  1283. }
  1284. } else {
  1285. switch (bindnet_address->family) {
  1286. case AF_INET:
  1287. memset(&mreq, 0, sizeof(mreq));
  1288. mreq.imr_multiaddr.s_addr = mcast_sin->sin_addr.s_addr;
  1289. mreq.imr_interface.s_addr = boundto_sin->sin_addr.s_addr;
  1290. res = setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  1291. &mreq, sizeof (mreq));
  1292. if (res == -1) {
  1293. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1294. "join ipv4 multicast group failed");
  1295. return (-1);
  1296. }
  1297. break;
  1298. case AF_INET6:
  1299. memset(&mreq6, 0, sizeof(mreq6));
  1300. memcpy(&mreq6.ipv6mr_multiaddr, &mcast_sin6->sin6_addr, sizeof(struct in6_addr));
  1301. mreq6.ipv6mr_interface = interface_num;
  1302. res = setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  1303. &mreq6, sizeof (mreq6));
  1304. if (res == -1) {
  1305. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1306. "join ipv6 multicast group failed");
  1307. return (-1);
  1308. }
  1309. break;
  1310. }
  1311. }
  1312. /*
  1313. * Turn on multicast loopback
  1314. */
  1315. flag = 1;
  1316. switch ( bindnet_address->family ) {
  1317. case AF_INET:
  1318. res = setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_LOOP,
  1319. &flag, sizeof (flag));
  1320. break;
  1321. case AF_INET6:
  1322. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  1323. &flag, sizeof (flag));
  1324. }
  1325. if (res == -1) {
  1326. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1327. "Unable to turn on multicast loopback");
  1328. return (-1);
  1329. }
  1330. /*
  1331. * Set multicast packets TTL
  1332. */
  1333. flag = instance->totem_interface->ttl;
  1334. if (bindnet_address->family == AF_INET6) {
  1335. res = setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  1336. &flag, sizeof (flag));
  1337. if (res == -1) {
  1338. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1339. "set mcast v6 TTL failed");
  1340. return (-1);
  1341. }
  1342. } else {
  1343. res = setsockopt(sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_TTL,
  1344. &flag, sizeof(flag));
  1345. if (res == -1) {
  1346. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1347. "set mcast v4 TTL failed");
  1348. return (-1);
  1349. }
  1350. }
  1351. /*
  1352. * Bind to a specific interface for multicast send and receive
  1353. */
  1354. switch ( bindnet_address->family ) {
  1355. case AF_INET:
  1356. if (setsockopt (sockets->mcast_send, IPPROTO_IP, IP_MULTICAST_IF,
  1357. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1358. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1359. "cannot select interface for multicast packets (send)");
  1360. return (-1);
  1361. }
  1362. if (setsockopt (sockets->mcast_recv, IPPROTO_IP, IP_MULTICAST_IF,
  1363. &boundto_sin->sin_addr, sizeof (boundto_sin->sin_addr)) < 0) {
  1364. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1365. "cannot select interface for multicast packets (recv)");
  1366. return (-1);
  1367. }
  1368. break;
  1369. case AF_INET6:
  1370. if (setsockopt (sockets->mcast_send, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1371. &interface_num, sizeof (interface_num)) < 0) {
  1372. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1373. "cannot select interface for multicast packets (send v6)");
  1374. return (-1);
  1375. }
  1376. if (setsockopt (sockets->mcast_recv, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  1377. &interface_num, sizeof (interface_num)) < 0) {
  1378. LOGSYS_PERROR (errno, instance->totemudp_log_level_warning,
  1379. "cannot select interface for multicast packets (recv v6)");
  1380. return (-1);
  1381. }
  1382. break;
  1383. }
  1384. return 0;
  1385. }
  1386. static int totemudp_build_sockets (
  1387. struct totemudp_instance *instance,
  1388. struct totem_ip_address *mcast_address,
  1389. struct totem_ip_address *bindnet_address,
  1390. struct totemudp_socket *sockets,
  1391. struct totem_ip_address *bound_to)
  1392. {
  1393. int interface_num;
  1394. int interface_up;
  1395. int res;
  1396. /*
  1397. * Determine the ip address bound to and the interface name
  1398. */
  1399. res = netif_determine (instance,
  1400. bindnet_address,
  1401. bound_to,
  1402. &interface_up,
  1403. &interface_num);
  1404. if (res == -1) {
  1405. return (-1);
  1406. }
  1407. totemip_copy(&instance->my_id, bound_to);
  1408. res = totemudp_build_sockets_ip (instance, mcast_address,
  1409. bindnet_address, sockets, bound_to, interface_num);
  1410. /* We only send out of the token socket */
  1411. totemudp_traffic_control_set(instance, sockets->token);
  1412. return res;
  1413. }
  1414. /*
  1415. * Totem Network interface - also does encryption/decryption
  1416. * depends on poll abstraction, POSIX, IPV4
  1417. */
  1418. /*
  1419. * Create an instance
  1420. */
  1421. int totemudp_initialize (
  1422. qb_loop_t *poll_handle,
  1423. void **udp_context,
  1424. struct totem_config *totem_config,
  1425. int interface_no,
  1426. void *context,
  1427. void (*deliver_fn) (
  1428. void *context,
  1429. const void *msg,
  1430. unsigned int msg_len),
  1431. void (*iface_change_fn) (
  1432. void *context,
  1433. const struct totem_ip_address *iface_address),
  1434. void (*target_set_completed) (
  1435. void *context))
  1436. {
  1437. struct totemudp_instance *instance;
  1438. instance = malloc (sizeof (struct totemudp_instance));
  1439. if (instance == NULL) {
  1440. return (-1);
  1441. }
  1442. totemudp_instance_initialize (instance);
  1443. instance->totem_config = totem_config;
  1444. /*
  1445. * Configure logging
  1446. */
  1447. instance->totemudp_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  1448. instance->totemudp_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  1449. instance->totemudp_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  1450. instance->totemudp_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  1451. instance->totemudp_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  1452. instance->totemudp_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  1453. instance->totemudp_log_printf = totem_config->totem_logging_configuration.log_printf;
  1454. /*
  1455. * Initialize random number generator for later use to generate salt
  1456. */
  1457. memcpy (instance->totemudp_private_key, totem_config->private_key,
  1458. totem_config->private_key_len);
  1459. instance->totemudp_private_key_len = totem_config->private_key_len;
  1460. init_crypto(instance);
  1461. /*
  1462. * Initialize local variables for totemudp
  1463. */
  1464. instance->totem_interface = &totem_config->interfaces[interface_no];
  1465. totemip_copy (&instance->mcast_address, &instance->totem_interface->mcast_addr);
  1466. memset (instance->iov_buffer, 0, FRAME_SIZE_MAX);
  1467. instance->totemudp_poll_handle = poll_handle;
  1468. instance->totem_interface->bindnet.nodeid = instance->totem_config->node_id;
  1469. instance->context = context;
  1470. instance->totemudp_deliver_fn = deliver_fn;
  1471. instance->totemudp_iface_change_fn = iface_change_fn;
  1472. instance->totemudp_target_set_completed = target_set_completed;
  1473. totemip_localhost (instance->mcast_address.family, &localhost);
  1474. localhost.nodeid = instance->totem_config->node_id;
  1475. /*
  1476. * RRP layer isn't ready to receive message because it hasn't
  1477. * initialized yet. Add short timer to check the interfaces.
  1478. */
  1479. qb_loop_timer_add (instance->totemudp_poll_handle,
  1480. QB_LOOP_MED,
  1481. 100*QB_TIME_NS_IN_MSEC,
  1482. (void *)instance,
  1483. timer_function_netif_check_timeout,
  1484. &instance->timer_netif_check_timeout);
  1485. *udp_context = instance;
  1486. return (0);
  1487. }
  1488. void *totemudp_buffer_alloc (void)
  1489. {
  1490. return malloc (FRAME_SIZE_MAX);
  1491. }
  1492. void totemudp_buffer_release (void *ptr)
  1493. {
  1494. return free (ptr);
  1495. }
  1496. int totemudp_processor_count_set (
  1497. void *udp_context,
  1498. int processor_count)
  1499. {
  1500. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1501. int res = 0;
  1502. instance->my_memb_entries = processor_count;
  1503. qb_loop_timer_del (instance->totemudp_poll_handle,
  1504. instance->timer_netif_check_timeout);
  1505. if (processor_count == 1) {
  1506. qb_loop_timer_add (instance->totemudp_poll_handle,
  1507. QB_LOOP_MED,
  1508. instance->totem_config->downcheck_timeout*QB_TIME_NS_IN_MSEC,
  1509. (void *)instance,
  1510. timer_function_netif_check_timeout,
  1511. &instance->timer_netif_check_timeout);
  1512. }
  1513. return (res);
  1514. }
  1515. int totemudp_recv_flush (void *udp_context)
  1516. {
  1517. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1518. struct pollfd ufd;
  1519. int nfds;
  1520. int res = 0;
  1521. instance->flushing = 1;
  1522. do {
  1523. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1524. ufd.events = POLLIN;
  1525. nfds = poll (&ufd, 1, 0);
  1526. if (nfds == 1 && ufd.revents & POLLIN) {
  1527. net_deliver_fn (instance->totemudp_sockets.mcast_recv,
  1528. ufd.revents, instance);
  1529. }
  1530. } while (nfds == 1);
  1531. instance->flushing = 0;
  1532. return (res);
  1533. }
  1534. int totemudp_send_flush (void *udp_context)
  1535. {
  1536. return 0;
  1537. }
  1538. int totemudp_token_send (
  1539. void *udp_context,
  1540. const void *msg,
  1541. unsigned int msg_len)
  1542. {
  1543. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1544. int res = 0;
  1545. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1546. return (res);
  1547. }
  1548. int totemudp_mcast_flush_send (
  1549. void *udp_context,
  1550. const void *msg,
  1551. unsigned int msg_len)
  1552. {
  1553. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1554. int res = 0;
  1555. mcast_sendmsg (instance, msg, msg_len);
  1556. return (res);
  1557. }
  1558. int totemudp_mcast_noflush_send (
  1559. void *udp_context,
  1560. const void *msg,
  1561. unsigned int msg_len)
  1562. {
  1563. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1564. int res = 0;
  1565. mcast_sendmsg (instance, msg, msg_len);
  1566. return (res);
  1567. }
  1568. extern int totemudp_iface_check (void *udp_context)
  1569. {
  1570. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1571. int res = 0;
  1572. timer_function_netif_check_timeout (instance);
  1573. return (res);
  1574. }
  1575. extern void totemudp_net_mtu_adjust (void *udp_context, struct totem_config *totem_config)
  1576. {
  1577. #define UDPIP_HEADER_SIZE (20 + 8) /* 20 bytes for ip 8 bytes for udp */
  1578. if (totem_config->secauth == 1) {
  1579. totem_config->net_mtu -= sizeof (struct security_header) +
  1580. UDPIP_HEADER_SIZE;
  1581. } else {
  1582. totem_config->net_mtu -= UDPIP_HEADER_SIZE;
  1583. }
  1584. }
  1585. const char *totemudp_iface_print (void *udp_context) {
  1586. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1587. const char *ret_char;
  1588. ret_char = totemip_print (&instance->my_id);
  1589. return (ret_char);
  1590. }
  1591. int totemudp_iface_get (
  1592. void *udp_context,
  1593. struct totem_ip_address *addr)
  1594. {
  1595. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1596. int res = 0;
  1597. memcpy (addr, &instance->my_id, sizeof (struct totem_ip_address));
  1598. return (res);
  1599. }
  1600. int totemudp_token_target_set (
  1601. void *udp_context,
  1602. const struct totem_ip_address *token_target)
  1603. {
  1604. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1605. int res = 0;
  1606. memcpy (&instance->token_target, token_target,
  1607. sizeof (struct totem_ip_address));
  1608. instance->totemudp_target_set_completed (instance->context);
  1609. return (res);
  1610. }
  1611. extern int totemudp_recv_mcast_empty (
  1612. void *udp_context)
  1613. {
  1614. struct totemudp_instance *instance = (struct totemudp_instance *)udp_context;
  1615. unsigned int res;
  1616. struct sockaddr_storage system_from;
  1617. struct msghdr msg_recv;
  1618. struct pollfd ufd;
  1619. int nfds;
  1620. int msg_processed = 0;
  1621. /*
  1622. * Receive datagram
  1623. */
  1624. msg_recv.msg_name = &system_from;
  1625. msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
  1626. msg_recv.msg_iov = &instance->totemudp_iov_recv_flush;
  1627. msg_recv.msg_iovlen = 1;
  1628. #if !defined(COROSYNC_SOLARIS)
  1629. msg_recv.msg_control = 0;
  1630. msg_recv.msg_controllen = 0;
  1631. msg_recv.msg_flags = 0;
  1632. #else
  1633. msg_recv.msg_accrights = NULL;
  1634. msg_recv.msg_accrightslen = 0;
  1635. #endif
  1636. do {
  1637. ufd.fd = instance->totemudp_sockets.mcast_recv;
  1638. ufd.events = POLLIN;
  1639. nfds = poll (&ufd, 1, 0);
  1640. if (nfds == 1 && ufd.revents & POLLIN) {
  1641. res = recvmsg (instance->totemudp_sockets.mcast_recv, &msg_recv, MSG_NOSIGNAL | MSG_DONTWAIT);
  1642. if (res != -1) {
  1643. msg_processed = 1;
  1644. } else {
  1645. msg_processed = -1;
  1646. }
  1647. }
  1648. } while (nfds == 1);
  1649. return (msg_processed);
  1650. }