totemudp.c 58 KB

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