totemnet.c 51 KB

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