secure-streams-serialize.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2019 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. *
  25. * In the case Secure Streams protocol needs to pass through a buffer,
  26. * or a streamed connection, the protocol metadata must be serialized. This
  27. * file provides internal apis to perform the serialization and deserialization
  28. * in and out of an lws_dsh fifo-type buffer.
  29. */
  30. #include <private-lib-core.h>
  31. typedef enum {
  32. RPAR_TYPE,
  33. RPAR_LEN_MSB,
  34. RPAR_LEN_LSB,
  35. RPAR_FLAG_B3,
  36. RPAR_FLAG_B2,
  37. RPAR_FLAG_B1,
  38. RPAR_FLAG_B0,
  39. RPAR_LATA3,
  40. RPAR_LATA2,
  41. RPAR_LATA1,
  42. RPAR_LATA0,
  43. RPAR_LATB7,
  44. RPAR_LATB6,
  45. RPAR_LATB5,
  46. RPAR_LATB4,
  47. RPAR_LATB3,
  48. RPAR_LATB2,
  49. RPAR_LATB1,
  50. RPAR_LATB0,
  51. RPAR_RIDESHARE_LEN,
  52. RPAR_RIDESHARE,
  53. RPAR_RESULT_CREATION_RIDESHARE,
  54. RPAR_METADATA_NAMELEN,
  55. RPAR_METADATA_NAME,
  56. RPAR_METADATA_VALUE,
  57. RPAR_PAYLOAD,
  58. RPAR_RX_TXCR_UPDATE,
  59. RPAR_STREAMTYPE,
  60. RPAR_INITTXC0,
  61. RPAR_TXCR0,
  62. RPAR_TIMEOUT0,
  63. RPAR_PAYLEN0,
  64. RPAR_RESULT_CREATION,
  65. RPAR_STATEINDEX,
  66. RPAR_ORD3,
  67. RPAR_ORD2,
  68. RPAR_ORD1,
  69. RPAR_ORD0,
  70. } rx_parser_t;
  71. #if defined(_DEBUG)
  72. static const char *sn[] = {
  73. "unset",
  74. "LPCSPROX_WAIT_INITIAL_TX",
  75. "LPCSPROX_REPORTING_FAIL",
  76. "LPCSPROX_REPORTING_OK",
  77. "LPCSPROX_OPERATIONAL",
  78. "LPCSPROX_DESTROYED",
  79. "LPCSCLI_SENDING_INITIAL_TX",
  80. "LPCSCLI_WAITING_CREATE_RESULT",
  81. "LPCSCLI_LOCAL_CONNECTED",
  82. "LPCSCLI_ONWARD_CONNECT",
  83. "LPCSCLI_OPERATIONAL",
  84. };
  85. #endif
  86. void
  87. lws_ss_serialize_state_transition(lws_ss_conn_states_t *state, int new_state)
  88. {
  89. #if defined(_DEBUG)
  90. lwsl_info("%s: %s -> %s\n", __func__, sn[*state], sn[new_state]);
  91. #endif
  92. *state = new_state;
  93. }
  94. /*
  95. * event loop received something and is queueing it for the foreign side of
  96. * the dsh to consume later as serialized rx
  97. */
  98. int
  99. lws_ss_serialize_rx_payload(struct lws_dsh *dsh, const uint8_t *buf,
  100. size_t len, int flags, const char *rsp)
  101. {
  102. lws_usec_t us = lws_now_usecs();
  103. uint8_t pre[128];
  104. int est = 19, l = 0;
  105. if (flags & LWSSS_FLAG_RIDESHARE) {
  106. /*
  107. * We should have the rideshare name if we have been told it's
  108. * on a non-default rideshare
  109. */
  110. assert(rsp);
  111. if (!rsp)
  112. return 1;
  113. l = strlen(rsp);
  114. est += 1 + l;
  115. } else
  116. assert(!rsp);
  117. // lwsl_user("%s: len %d, flags: %d\n", __func__, (int)len, flags);
  118. // lwsl_hexdump_info(buf, len);
  119. pre[0] = LWSSS_SER_RXPRE_RX_PAYLOAD;
  120. lws_ser_wu16be(&pre[1], len + est - 3);
  121. lws_ser_wu32be(&pre[3], flags);
  122. lws_ser_wu32be(&pre[7], 0); /* write will compute latency here... */
  123. lws_ser_wu64be(&pre[11], us); /* ... and set this to the write time */
  124. /*
  125. * If we are on a non-default rideshare, append the non-default name to
  126. * the headers of the payload part, 1-byte length first
  127. */
  128. if (flags & LWSSS_FLAG_RIDESHARE) {
  129. pre[19] = (uint8_t)l;
  130. memcpy(&pre[20], rsp, l);
  131. }
  132. if (lws_dsh_alloc_tail(dsh, KIND_SS_TO_P, pre, est, buf, len)) {
  133. lwsl_err("%s: unable to alloc in dsh 1\n", __func__);
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. /*
  139. * event loop is consuming dsh-buffered, already-serialized tx from the
  140. * foreign side
  141. */
  142. int
  143. lws_ss_deserialize_tx_payload(struct lws_dsh *dsh, struct lws *wsi,
  144. lws_ss_tx_ordinal_t ord, uint8_t *buf,
  145. size_t *len, int *flags)
  146. {
  147. uint8_t *p;
  148. size_t si;
  149. if (lws_dsh_get_head(dsh, KIND_C_TO_P, (void **)&p, &si)) {
  150. *len = 0;
  151. return 0;
  152. }
  153. /*
  154. * The packet in the dsh has a proxying serialization header, process
  155. * and strip it so we just forward the payload
  156. */
  157. if (*len <= si - 23 || si < 23) {
  158. /*
  159. * What comes out of the dsh needs to fit in the tx buffer...
  160. * we have arrangements at the proxy rx of the client UDS to
  161. * chop chunks larger than 1380 into seuqential lumps of 1380
  162. */
  163. lwsl_err("%s: *len = %d, si = %d\n", __func__, (int)*len, (int)si);
  164. assert(0);
  165. return 1;
  166. }
  167. if (p[0] != LWSSS_SER_TXPRE_TX_PAYLOAD) {
  168. assert(0);
  169. return 1;
  170. }
  171. *len = lws_ser_ru16be(&p[1]) - (23 - 3);
  172. if (*len != si - 23) {
  173. /*
  174. * We cannot accept any length that doesn't reflect the actual
  175. * length of what came in from the dsh, either something nasty
  176. * happened with truncation or we are being attacked
  177. */
  178. assert(0);
  179. return 1;
  180. }
  181. memcpy(buf, p + 23, si - 23);
  182. *flags = lws_ser_ru32be(&p[3]);
  183. #if defined(LWS_WITH_DETAILED_LATENCY)
  184. if (wsi && wsi->a.context->detailed_latency_cb) {
  185. /*
  186. * use the proxied latency information to compute the client
  187. * and our delays, and apply to wsi.
  188. *
  189. * + 7 u32 us held at client before written
  190. * +11 u32 us taken for transit to proxy
  191. * +15 u64 ustime when proxy got packet from client
  192. */
  193. lws_usec_t us = lws_now_usecs();
  194. wsi->detlat.acc_size = wsi->detlat.req_size = si - 23;
  195. wsi->detlat.latencies[LAT_DUR_PROXY_CLIENT_REQ_TO_WRITE] =
  196. lws_ser_ru32be(&p[7]);
  197. wsi->detlat.latencies[LAT_DUR_PROXY_CLIENT_WRITE_TO_PROXY_RX] =
  198. lws_ser_ru32be(&p[11]);
  199. wsi->detlat.latencies[LAT_DUR_PROXY_RX_TO_ONWARD_TX] =
  200. us - lws_ser_ru64be(&p[15]);
  201. wsi->detlat.latencies[LAT_DUR_USERCB] = 0;
  202. }
  203. #endif
  204. // lwsl_user("%s: len %d, flags: %d\n", __func__, (int)*len, *flags);
  205. // lwsl_hexdump_info(buf, *len);
  206. lws_dsh_free((void **)&p);
  207. return 0;
  208. }
  209. /*
  210. * event loop side is issuing state, serialize and put it in the dbuf for
  211. * the foreign side to consume later
  212. */
  213. int
  214. lws_ss_serialize_state(struct lws_dsh *dsh, lws_ss_constate_t state,
  215. lws_ss_tx_ordinal_t ack)
  216. {
  217. uint8_t pre[12];
  218. int n = 4;
  219. lwsl_info("%s: %s, ord 0x%x\n", __func__, lws_ss_state_name(state),
  220. (unsigned int)ack);
  221. pre[0] = LWSSS_SER_RXPRE_CONNSTATE;
  222. pre[1] = 0;
  223. if (state > 255) {
  224. pre[2] = 8;
  225. lws_ser_wu32be(&pre[3], state);
  226. n = 7;
  227. } else {
  228. pre[2] = 5;
  229. pre[3] = (uint8_t)state;
  230. }
  231. lws_ser_wu32be(&pre[n], ack);
  232. if (lws_dsh_alloc_tail(dsh, KIND_SS_TO_P, pre, n + 4, NULL, 0)) {
  233. lwsl_err("%s: unable to alloc in dsh 2\n", __func__);
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. /*
  239. * event loop side was told about remote peer tx credit window update, serialize
  240. * and put it in the dbuf for the foreign side to consume later
  241. */
  242. int
  243. lws_ss_serialize_txcr(struct lws_dsh *dsh, int txcr)
  244. {
  245. uint8_t pre[7];
  246. lwsl_info("%s: %d\n", __func__, txcr);
  247. pre[0] = LWSSS_SER_RXPRE_TXCR_UPDATE;
  248. pre[1] = 0;
  249. pre[2] = 4;
  250. lws_ser_wu32be(&pre[3], txcr);
  251. if (lws_dsh_alloc_tail(dsh, KIND_SS_TO_P, pre, 7, NULL, 0)) {
  252. lwsl_err("%s: unable to alloc in dsh 2\n", __func__);
  253. return 1;
  254. }
  255. return 0;
  256. }
  257. /*
  258. * event loop side is consuming serialized data from the client via dsh, parse
  259. * it using a bytewise parser for the serialization header(s)...
  260. * it's possibly coalesced
  261. *
  262. * client: pss is pointing to the start of userdata. We can use
  263. * pss_to_sspc_h(_pss, _ssi) to convert that to a pointer to the sspc
  264. * handle
  265. *
  266. * proxy: pss is pointing to &conn->ss, a pointer to the ss handle
  267. *
  268. * Returns one of
  269. *
  270. * LWSSSSRET_OK
  271. * LWSSSSRET_DISCONNECT_ME
  272. * LWSSSSRET_DESTROY_ME
  273. */
  274. /* convert userdata ptr _pss to handle pointer, allowing for any layout in
  275. * userdata */
  276. #define client_pss_to_sspc_h(_pss, _ssi) (*((lws_sspc_handle_t **) \
  277. ((uint8_t *)_pss) + _ssi->handle_offset))
  278. /* client pss to sspc userdata */
  279. #define client_pss_to_userdata(_pss) ((void *)_pss)
  280. /* proxy convert pss to ss handle */
  281. #define proxy_pss_to_ss_h(_pss) (*_pss)
  282. /* convert userdata ptr _pss to handle pointer, allowing for any layout in
  283. * userdata */
  284. #define client_pss_to_sspc_h(_pss, _ssi) (*((lws_sspc_handle_t **) \
  285. ((uint8_t *)_pss) + _ssi->handle_offset))
  286. /* client pss to sspc userdata */
  287. #define client_pss_to_userdata(_pss) ((void *)_pss)
  288. /* proxy convert pss to ss handle */
  289. #define proxy_pss_to_ss_h(_pss) (*_pss)
  290. int
  291. lws_ss_deserialize_parse(struct lws_ss_serialization_parser *par,
  292. struct lws_context *context,
  293. struct lws_dsh *dsh, const uint8_t *cp, size_t len,
  294. lws_ss_conn_states_t *state, void *parconn,
  295. lws_ss_handle_t **pss, lws_ss_info_t *ssi, char client)
  296. {
  297. lws_ss_metadata_t *pm;
  298. lws_sspc_handle_t *h;
  299. uint8_t pre[23];
  300. lws_usec_t us;
  301. uint32_t flags;
  302. uint8_t *p;
  303. int n;
  304. while (len--) {
  305. switch (par->ps) {
  306. case RPAR_TYPE:
  307. par->type = *cp++;
  308. par->ps++;
  309. break;
  310. case RPAR_LEN_MSB: /* this is remaining frame length */
  311. par->rem = (*cp++) << 8;
  312. par->ps++;
  313. break;
  314. case RPAR_LEN_LSB:
  315. par->rem |= *cp++;
  316. switch (par->type) {
  317. /* event loop side */
  318. case LWSSS_SER_TXPRE_TX_PAYLOAD:
  319. if (client)
  320. goto hangup;
  321. if (*state != LPCSPROX_OPERATIONAL)
  322. goto hangup;
  323. par->ps = RPAR_FLAG_B3;
  324. break;
  325. case LWSSS_SER_TXPRE_DESTROYING:
  326. if (client)
  327. goto hangup;
  328. par->ps = RPAR_TYPE;
  329. lwsl_notice("%s: DESTROYING\n", __func__);
  330. goto hangup;
  331. case LWSSS_SER_TXPRE_ONWARD_CONNECT:
  332. if (client)
  333. goto hangup;
  334. if (*state != LPCSPROX_OPERATIONAL)
  335. goto hangup;
  336. par->ps = RPAR_TYPE;
  337. lwsl_notice("%s: LWSSS_SER_TXPRE_ONWARD_CONNECT\n", __func__);
  338. if (proxy_pss_to_ss_h(pss) &&
  339. !proxy_pss_to_ss_h(pss)->wsi)
  340. _lws_ss_client_connect(
  341. proxy_pss_to_ss_h(pss), 0);
  342. break;
  343. case LWSSS_SER_TXPRE_STREAMTYPE:
  344. if (client)
  345. goto hangup;
  346. if (*state != LPCSPROX_WAIT_INITIAL_TX)
  347. goto hangup;
  348. if (par->rem < 4)
  349. goto hangup;
  350. par->ctr = 0;
  351. par->ps = RPAR_INITTXC0;
  352. break;
  353. case LWSSS_SER_TXPRE_METADATA:
  354. if (client)
  355. goto hangup;
  356. if (par->rem < 3)
  357. goto hangup;
  358. par->ctr = 0;
  359. par->ps = RPAR_METADATA_NAMELEN;
  360. break;
  361. case LWSSS_SER_TXPRE_TXCR_UPDATE:
  362. par->ps = RPAR_TXCR0;
  363. par->ctr = 0;
  364. break;
  365. case LWSSS_SER_TXPRE_TIMEOUT_UPDATE:
  366. if (client)
  367. goto hangup;
  368. if (par->rem != 4)
  369. goto hangup;
  370. par->ps = RPAR_TIMEOUT0;
  371. par->ctr = 0;
  372. break;
  373. case LWSSS_SER_TXPRE_PAYLOAD_LENGTH_HINT:
  374. if (client)
  375. goto hangup;
  376. if (par->rem != 4)
  377. goto hangup;
  378. par->ps = RPAR_PAYLEN0;
  379. par->ctr = 0;
  380. break;
  381. /* client side */
  382. case LWSSS_SER_RXPRE_RX_PAYLOAD:
  383. if (!client)
  384. goto hangup;
  385. if (*state != LPCSCLI_OPERATIONAL &&
  386. *state != LPCSCLI_LOCAL_CONNECTED)
  387. goto hangup;
  388. par->rideshare[0] = '\0';
  389. par->ps = RPAR_FLAG_B3;
  390. break;
  391. case LWSSS_SER_RXPRE_CREATE_RESULT:
  392. if (!client)
  393. goto hangup;
  394. if (*state != LPCSCLI_WAITING_CREATE_RESULT)
  395. goto hangup;
  396. if (par->rem < 1)
  397. goto hangup;
  398. par->ps = RPAR_RESULT_CREATION;
  399. break;
  400. case LWSSS_SER_RXPRE_CONNSTATE:
  401. if (!client)
  402. goto hangup;
  403. if (*state != LPCSCLI_LOCAL_CONNECTED &&
  404. *state != LPCSCLI_OPERATIONAL)
  405. goto hangup;
  406. if (par->rem < 5 || par->rem > 8)
  407. goto hangup;
  408. par->ps = RPAR_STATEINDEX;
  409. par->ctr = 0;
  410. break;
  411. case LWSSS_SER_RXPRE_TXCR_UPDATE:
  412. par->ctr = 0;
  413. par->ps = RPAR_RX_TXCR_UPDATE;
  414. break;
  415. default:
  416. lwsl_notice("%s: bad type 0x%x\n", __func__,
  417. par->type);
  418. goto hangup;
  419. }
  420. break;
  421. case RPAR_FLAG_B3:
  422. case RPAR_FLAG_B2:
  423. case RPAR_FLAG_B1:
  424. case RPAR_FLAG_B0:
  425. par->flags <<= 8;
  426. par->flags |= *cp++;
  427. par->ps++;
  428. if (!par->rem--)
  429. goto hangup;
  430. break;
  431. case RPAR_LATA3:
  432. case RPAR_LATA2:
  433. case RPAR_LATA1:
  434. case RPAR_LATA0:
  435. par->usd_phandling <<= 8;
  436. par->usd_phandling |= *cp++;
  437. par->ps++;
  438. if (!par->rem--)
  439. goto hangup;
  440. break;
  441. case RPAR_LATB7:
  442. case RPAR_LATB6:
  443. case RPAR_LATB5:
  444. case RPAR_LATB4:
  445. case RPAR_LATB3:
  446. case RPAR_LATB2:
  447. case RPAR_LATB1:
  448. case RPAR_LATB0:
  449. par->ust_pwait <<= 8;
  450. par->ust_pwait |= *cp++;
  451. par->ps++;
  452. par->frag1 = 1;
  453. if (!par->rem--)
  454. goto hangup;
  455. if (par->ps == RPAR_RIDESHARE_LEN &&
  456. !(par->flags & LWSSS_FLAG_RIDESHARE))
  457. par->ps = RPAR_PAYLOAD;
  458. if (par->rem)
  459. break;
  460. /* fallthru - handle 0-length payload */
  461. if (!(par->flags & LWSSS_FLAG_RIDESHARE))
  462. goto payload_ff;
  463. goto hangup;
  464. /*
  465. * Inbound rideshare info is provided on the RX packet
  466. * itself
  467. */
  468. case RPAR_RIDESHARE_LEN:
  469. par->slen = *cp++;
  470. par->ctr = 0;
  471. par->ps++;
  472. if (par->rem-- < par->slen)
  473. goto hangup;
  474. break;
  475. case RPAR_RIDESHARE:
  476. par->rideshare[par->ctr++] = *cp++;
  477. if (!par->rem--)
  478. goto hangup;
  479. if (par->ctr != par->slen)
  480. break;
  481. par->ps = RPAR_PAYLOAD;
  482. if (par->rem)
  483. break;
  484. /* fallthru - handle 0-length payload */
  485. case RPAR_PAYLOAD:
  486. payload_ff:
  487. n = (int)len + 1;
  488. if (n > par->rem)
  489. n = par->rem;
  490. /*
  491. * We get called with a serialized buffer of a size
  492. * chosen by the client. We can only create dsh entries
  493. * with up to 1380 payload, to guarantee we can emit
  494. * them on the onward connection atomically.
  495. *
  496. * If 1380 isn't enough to cover what was handed to us,
  497. * we'll stop at 1380 and go around again and create
  498. * more dsh entries for the rest, with their own
  499. * headers.
  500. */
  501. if (n > 1380)
  502. n = 1380;
  503. /*
  504. * Since we're in the business of fragmenting client
  505. * serialized payloads at 1380, we have to deal with
  506. * refragmenting the SOM / EOM flags that covered the
  507. * whole client serialized packet, so they apply to
  508. * each dsh entry we split it into correctly
  509. */
  510. flags = par->flags & LWSSS_FLAG_RELATED_START;
  511. if (par->frag1)
  512. /*
  513. * Only set the first time we came to this
  514. * state after deserialization of the header
  515. */
  516. flags |= par->flags &
  517. (LWSSS_FLAG_SOM | LWSSS_FLAG_POLL);
  518. if (par->rem == n)
  519. /*
  520. * We are going to complete the advertised
  521. * payload length from the client on this dsh,
  522. * so give him the EOM type flags if any
  523. */
  524. flags |= par->flags & (LWSSS_FLAG_EOM |
  525. LWSSS_FLAG_RELATED_END);
  526. par->frag1 = 0;
  527. us = lws_now_usecs();
  528. if (!client) {
  529. /*
  530. * Proxy - we received some serialized tx from
  531. * the client.
  532. *
  533. * The header for buffering private to the
  534. * proxy is 23 bytes vs 19, so we can hold the
  535. * current time when it was buffered
  536. * additionally
  537. */
  538. lwsl_info("%s: C2P RX: len %d\n", __func__,
  539. (int)n);
  540. p = pre;
  541. pre[0] = LWSSS_SER_TXPRE_TX_PAYLOAD;
  542. lws_ser_wu16be(&p[1], n + 23 - 3);
  543. lws_ser_wu32be(&p[3], flags);
  544. /* us held at client before written */
  545. lws_ser_wu32be(&p[7], par->usd_phandling);
  546. /* us taken for transit to proxy */
  547. lws_ser_wu32be(&p[11], us - par->ust_pwait);
  548. /* time used later to find proxy hold time */
  549. lws_ser_wu64be(&p[15], us);
  550. if (lws_dsh_alloc_tail(dsh, KIND_C_TO_P, pre,
  551. 23, cp, n)) {
  552. lwsl_err("%s: unable to alloc in dsh 3\n",
  553. __func__);
  554. return LWSSSSRET_DISCONNECT_ME;
  555. }
  556. if (proxy_pss_to_ss_h(pss))
  557. lws_ss_request_tx(
  558. proxy_pss_to_ss_h(pss));
  559. } else {
  560. /*
  561. * Client receives some RX from proxy
  562. *
  563. * Pass whatever payload we have to ss user
  564. */
  565. lwsl_info("%s: P2C RX: len %d\n", __func__,
  566. (int)n);
  567. h = lws_container_of(par, lws_sspc_handle_t,
  568. parser);
  569. h->txc.peer_tx_cr_est -= n;
  570. if (client_pss_to_sspc_h(pss, ssi))
  571. /* we still have an sspc handle */
  572. ssi->rx(client_pss_to_userdata(pss),
  573. (uint8_t *)cp, n, flags);
  574. #if defined(LWS_WITH_DETAILED_LATENCY)
  575. if (lws_det_lat_active(context)) {
  576. lws_detlat_t d;
  577. d.type = LDLT_READ;
  578. d.acc_size = d.req_size = n;
  579. d.latencies[LAT_DUR_USERCB] =
  580. lws_now_usecs() - us;
  581. d.latencies[LAT_DUR_PROXY_CLIENT_REQ_TO_WRITE] =
  582. par->usd_phandling;
  583. d.latencies[LAT_DUR_PROXY_CLIENT_WRITE_TO_PROXY_RX] =
  584. us - par->ust_pwait;
  585. lws_det_lat_cb(context, &d);
  586. }
  587. #endif
  588. }
  589. if (n) {
  590. cp += n;
  591. par->rem -= n;
  592. len = (len + 1) - n;
  593. /*
  594. * if we didn't consume it all, we'll come
  595. * around again and produce more dsh entries up
  596. * to 1380 each until it is gone
  597. */
  598. }
  599. if (!par->rem)
  600. par->ps = RPAR_TYPE;
  601. break;
  602. case RPAR_RX_TXCR_UPDATE:
  603. if (!--par->rem && par->ctr != 3)
  604. goto hangup;
  605. par->temp32 = (par->temp32 << 8) | *cp++;
  606. if (++par->ctr < 4)
  607. break;
  608. /*
  609. * Proxy is telling us remote endpoint is allowing us
  610. * par->temp32 more bytes tx credit to write to it
  611. */
  612. h = lws_container_of(par, lws_sspc_handle_t, parser);
  613. h->txc.tx_cr += par->temp32;
  614. lwsl_info("%s: RX_PEER_TXCR: %d\n", __func__, par->temp32);
  615. lws_sspc_request_tx(h); /* in case something waiting */
  616. par->ctr = 0;
  617. par->ps = RPAR_TYPE;
  618. break;
  619. case RPAR_INITTXC0:
  620. if (!--par->rem)
  621. goto hangup;
  622. par->temp32 = (par->temp32 << 8) | *cp++;
  623. if (++par->ctr < 4)
  624. break;
  625. par->txcr_out = par->temp32;
  626. par->ctr = 0;
  627. par->ps = RPAR_STREAMTYPE;
  628. break;
  629. /*
  630. * These are the client adjusting our / the remote peer ability
  631. * to send back to him. He's sending a signed u32 BE
  632. */
  633. case RPAR_TXCR0:
  634. par->temp32 = (par->temp32 << 8) | *cp++;
  635. if (++par->ctr < 4) {
  636. if (!--par->rem)
  637. goto hangup;
  638. break;
  639. }
  640. if (--par->rem)
  641. goto hangup;
  642. if (!client) {
  643. /*
  644. * We're the proxy, being told by the client
  645. * that it wants to allow more tx from the peer
  646. * on the onward connection towards it.
  647. */
  648. #if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT)
  649. if (proxy_pss_to_ss_h(pss) &&
  650. proxy_pss_to_ss_h(pss)->wsi) {
  651. lws_wsi_tx_credit(
  652. proxy_pss_to_ss_h(pss)->wsi,
  653. LWSTXCR_PEER_TO_US,
  654. par->temp32);
  655. lwsl_notice("%s: proxy RX_PEER_TXCR: +%d (est %d)\n",
  656. __func__, par->temp32,
  657. proxy_pss_to_ss_h(pss)->wsi->
  658. txc.peer_tx_cr_est);
  659. lws_ss_request_tx(proxy_pss_to_ss_h(pss));
  660. } else
  661. #endif
  662. lwsl_info("%s: dropping TXCR\n", __func__);
  663. } else {
  664. /*
  665. * We're the client, being told by the proxy
  666. * about tx credit being given to us from the
  667. * remote peer, allowing the client to write to
  668. * it.
  669. */
  670. h = lws_container_of(par, lws_sspc_handle_t,
  671. parser);
  672. h->txc.tx_cr += par->temp32;
  673. lwsl_info("%s: client RX_PEER_TXCR: %d\n",
  674. __func__, par->temp32);
  675. lws_sspc_request_tx(h); /* in case something waiting */
  676. }
  677. par->ps = RPAR_TYPE;
  678. break;
  679. case RPAR_TIMEOUT0:
  680. par->temp32 = (par->temp32 << 8) | *cp++;
  681. if (++par->ctr < 4) {
  682. if (!--par->rem)
  683. goto hangup;
  684. break;
  685. }
  686. if (--par->rem)
  687. goto hangup;
  688. /*
  689. * Proxy...
  690. *
  691. * *pss may have gone away asynchronously inbetweentimes
  692. */
  693. if (proxy_pss_to_ss_h(pss)) {
  694. if ((unsigned int)par->temp32 == 0xffffffff) {
  695. lwsl_notice("%s: cancel ss timeout\n",
  696. __func__);
  697. lws_ss_cancel_timeout(
  698. proxy_pss_to_ss_h(pss));
  699. } else {
  700. if (!par->temp32)
  701. par->temp32 =
  702. proxy_pss_to_ss_h(pss)->
  703. policy->timeout_ms;
  704. lwsl_notice("%s: set ss timeout for +%ums\n",
  705. __func__, par->temp32);
  706. lws_ss_start_timeout(
  707. proxy_pss_to_ss_h(pss),
  708. par->temp32);
  709. }
  710. }
  711. par->ps = RPAR_TYPE;
  712. break;
  713. case RPAR_PAYLEN0:
  714. /*
  715. * It's the length from lws_ss_request_tx_len() being
  716. * passed up to the proxy
  717. */
  718. par->temp32 = (par->temp32 << 8) | *cp++;
  719. if (++par->ctr < 4) {
  720. if (!--par->rem)
  721. goto hangup;
  722. break;
  723. }
  724. if (--par->rem)
  725. goto hangup;
  726. lwsl_notice("%s: set payload len %u\n", __func__,
  727. par->temp32);
  728. if (proxy_pss_to_ss_h(pss))
  729. lws_ss_request_tx_len(proxy_pss_to_ss_h(pss),
  730. par->temp32);
  731. par->ps = RPAR_TYPE;
  732. break;
  733. case RPAR_METADATA_NAMELEN:
  734. if (!--par->rem)
  735. goto hangup;
  736. par->slen = *cp++;
  737. if (par->slen >= sizeof(par->metadata_name) - 1)
  738. goto hangup;
  739. par->ctr = 0;
  740. par->ps++;
  741. break;
  742. case RPAR_METADATA_NAME:
  743. if (!--par->rem)
  744. goto hangup;
  745. par->metadata_name[par->ctr++] = *cp++;
  746. if (par->ctr != par->slen)
  747. break;
  748. par->metadata_name[par->ctr] = '\0';
  749. par->ps = RPAR_METADATA_VALUE;
  750. /* only proxy side can receive these */
  751. if (!proxy_pss_to_ss_h(pss))
  752. goto hangup;
  753. /*
  754. * This is the policy's metadata list for the given
  755. * name
  756. */
  757. pm = lws_ss_policy_metadata(proxy_pss_to_ss_h(pss)->policy,
  758. par->metadata_name);
  759. if (!pm) {
  760. lwsl_err("%s: metadata %s not in proxy policy\n",
  761. __func__, par->metadata_name);
  762. goto hangup;
  763. }
  764. par->ssmd = &proxy_pss_to_ss_h(pss)->metadata[pm->length];
  765. if (par->ssmd->value_on_lws_heap)
  766. lws_free_set_NULL(par->ssmd->value);
  767. par->ssmd->value_on_lws_heap = 0;
  768. par->ssmd->value = lws_malloc(par->rem + 1, "metadata");
  769. if (!par->ssmd->value) {
  770. lwsl_err("%s: OOM mdv\n", __func__);
  771. goto hangup;
  772. }
  773. par->ssmd->length = par->rem;
  774. /* mark it as needing cleanup */
  775. par->ssmd->value_on_lws_heap = 1;
  776. par->ctr = 0;
  777. break;
  778. case RPAR_METADATA_VALUE:
  779. ((uint8_t *)(par->ssmd->value))[par->ctr++] = *cp++;
  780. if (--par->rem)
  781. break;
  782. /* we think we got all the value */
  783. lwsl_info("%s: RPAR_METADATA_VALUE for %s (len %d)\n",
  784. __func__, par->ssmd->name,
  785. (int)par->ssmd->length);
  786. lwsl_hexdump_info(par->ssmd->value, par->ssmd->length);
  787. par->ps = RPAR_TYPE;
  788. break;
  789. case RPAR_STREAMTYPE:
  790. /* only the proxy can get these */
  791. if (client)
  792. goto hangup;
  793. if (par->ctr == sizeof(par->streamtype) - 1)
  794. goto hangup;
  795. /*
  796. * We can only expect to get this if we ourselves are
  797. * in the state that we're waiting for it. If it comes
  798. * later it's a protocol error.
  799. */
  800. if (*state != LPCSPROX_WAIT_INITIAL_TX)
  801. goto hangup;
  802. /*
  803. * We're the proxy, creating an SS on behalf of a
  804. * client
  805. */
  806. par->streamtype[par->ctr++] = *cp++;
  807. if (--par->rem)
  808. break;
  809. par->ps = RPAR_TYPE;
  810. par->streamtype[par->ctr] = '\0';
  811. lwsl_notice("%s: creating proxied ss '%s', txcr %d\n",
  812. __func__, par->streamtype, par->txcr_out);
  813. ssi->streamtype = par->streamtype;
  814. if (par->txcr_out) // !!!
  815. ssi->manual_initial_tx_credit = par->txcr_out;
  816. /*
  817. * Even for a synthetic SS proxing action like _lws_smd,
  818. * we create an actual SS in the proxy representing the
  819. * connection
  820. */
  821. ssi->flags |= LWSSSINFLAGS_PROXIED;
  822. if (lws_ss_create(context, 0, ssi, parconn, pss,
  823. NULL, NULL)) {
  824. /*
  825. * We're unable to create the onward secure
  826. * stream he asked for... schedule a chance to
  827. * inform him
  828. */
  829. lwsl_err("%s: create '%s' fail\n",
  830. __func__, par->streamtype);
  831. *state = LPCSPROX_REPORTING_FAIL;
  832. } else {
  833. lwsl_debug("%s: create '%s' OK\n",
  834. __func__, par->streamtype);
  835. *state = LPCSPROX_REPORTING_OK;
  836. }
  837. if (*pss) {
  838. (*pss)->being_serialized = 1;
  839. #if defined(LWS_WITH_SYS_SMD)
  840. if ((*pss)->policy != &pol_smd)
  841. /*
  842. * In SMD case we overloaded the
  843. * initial credit to be the class mask
  844. */
  845. #endif
  846. {
  847. lwsl_info("%s: Created SS initial credit %d\n",
  848. __func__, par->txcr_out);
  849. (*pss)->info.manual_initial_tx_credit = par->txcr_out;
  850. }
  851. }
  852. /* parent needs to schedule write on client conn */
  853. break;
  854. /* clientside states */
  855. case RPAR_RESULT_CREATION:
  856. if (*cp++) {
  857. lwsl_err("%s: stream creation failed\n",
  858. __func__);
  859. goto hangup;
  860. }
  861. /*
  862. * Client
  863. */
  864. lws_ss_serialize_state_transition(state,
  865. LPCSCLI_LOCAL_CONNECTED);
  866. h = lws_container_of(par, lws_sspc_handle_t, parser);
  867. /*
  868. * This is telling us that the streamtype could be (and
  869. * was) created at the proxy. It's not telling us that
  870. * the onward peer connection could be connected.
  871. *
  872. * We'll get a proxied state() coming later that informs
  873. * us about the situation with that.
  874. *
  875. * However at this point, we should choose to inform
  876. * the client that his stream was created... we will
  877. * later get a proxied CREATING state from the peer
  878. * but we should do it now and suppress the later one.
  879. *
  880. * The reason is he may set metadata in CREATING, and
  881. * we will try to do writeables to sync the stream to
  882. * master and ultimately bring up the onward connection now
  883. * now we are in LOCAL_CONNECTED. We need to do the
  884. * CREATING now so we'll know the metadata to sync.
  885. */
  886. h->creating_cb_done = 1;
  887. n = ssi->state(client_pss_to_userdata(pss),
  888. NULL, LWSSSCS_CREATING, 0);
  889. switch (n) {
  890. case LWSSSSRET_OK:
  891. break;
  892. case LWSSSSRET_DISCONNECT_ME:
  893. goto hangup;
  894. case LWSSSSRET_DESTROY_ME:
  895. return LWSSSSRET_DESTROY_ME;
  896. }
  897. if (h->cwsi)
  898. lws_callback_on_writable(h->cwsi);
  899. par->rsl_pos = 0;
  900. par->rsl_idx = 0;
  901. memset(&h->rideshare_ofs[0], 0, sizeof(h->rideshare_ofs[0]));
  902. h->rideshare_list[0] = '\0';
  903. h->rsidx = 0;
  904. if (!--par->rem)
  905. par->ps = RPAR_TYPE;
  906. else {
  907. par->ps = RPAR_RESULT_CREATION_RIDESHARE;
  908. if (par->rem >= sizeof(h->rideshare_list))
  909. goto hangup;
  910. }
  911. break;
  912. case RPAR_RESULT_CREATION_RIDESHARE:
  913. h = lws_container_of(par, lws_sspc_handle_t, parser);
  914. if (*cp == ',') {
  915. cp++;
  916. h->rideshare_list[par->rsl_pos++] = '\0';
  917. if (par->rsl_idx == LWS_ARRAY_SIZE(h->rideshare_ofs))
  918. goto hangup;
  919. h->rideshare_ofs[++par->rsl_idx] = par->rsl_pos;
  920. } else
  921. h->rideshare_list[par->rsl_pos++] = *cp++;
  922. if (!--par->rem)
  923. par->ps = RPAR_TYPE;
  924. break;
  925. case RPAR_STATEINDEX:
  926. par->ctr = (par->ctr << 8) | (*cp++);
  927. if (--par->rem == 4)
  928. par->ps = RPAR_ORD3;
  929. break;
  930. case RPAR_ORD3:
  931. par->flags = (*cp++) << 24;
  932. par->ps++;
  933. break;
  934. case RPAR_ORD2:
  935. par->flags |= (*cp++) << 16;
  936. par->ps++;
  937. break;
  938. case RPAR_ORD1:
  939. par->flags |= (*cp++) << 8;
  940. par->ps++;
  941. break;
  942. case RPAR_ORD0:
  943. par->flags |= *cp++;
  944. par->ps++;
  945. par->ps = RPAR_TYPE;
  946. /*
  947. * Client received a proxied state change
  948. */
  949. if (!client_pss_to_sspc_h(pss, ssi))
  950. /*
  951. * Since we're being informed we need to have
  952. * a stream to inform. Assume whatever set this
  953. * to NULL has started to close it.
  954. */
  955. break;
  956. switch (par->ctr) {
  957. case LWSSSCS_DISCONNECTED:
  958. case LWSSSCS_UNREACHABLE:
  959. case LWSSSCS_AUTH_FAILED:
  960. lws_ss_serialize_state_transition(state,
  961. LPCSCLI_LOCAL_CONNECTED);
  962. client_pss_to_sspc_h(pss, ssi)->conn_req_state =
  963. LWSSSPC_ONW_NONE;
  964. break;
  965. case LWSSSCS_CONNECTED:
  966. lwsl_info("%s: CONNECTED %s\n", __func__,
  967. ssi->streamtype);
  968. if (*state == LPCSCLI_OPERATIONAL)
  969. /*
  970. * Don't allow to see connected more
  971. * than once for one connection
  972. */
  973. goto swallow;
  974. lws_ss_serialize_state_transition(state,
  975. LPCSCLI_OPERATIONAL);
  976. client_pss_to_sspc_h(pss, ssi)->conn_req_state =
  977. LWSSSPC_ONW_CONN;
  978. break;
  979. case LWSSSCS_TIMEOUT:
  980. break;
  981. default:
  982. break;
  983. }
  984. if (par->ctr < 0)
  985. goto hangup;
  986. #if defined(_DEBUG)
  987. lwsl_info("%s: forwarding proxied state %s\n",
  988. __func__, lws_ss_state_name(par->ctr));
  989. #endif
  990. if (par->ctr == LWSSSCS_CREATING) {
  991. if (h->creating_cb_done)
  992. /*
  993. * We have told him he's CREATING when
  994. * we heard we had linked up to the
  995. * proxy, so suppress the remote
  996. * CREATING so that he only sees it once
  997. */
  998. break;
  999. h->creating_cb_done = 1;
  1000. }
  1001. n = ssi->state(client_pss_to_userdata(pss),
  1002. NULL, par->ctr, par->flags);
  1003. switch (n) {
  1004. case LWSSSSRET_OK:
  1005. break;
  1006. case LWSSSSRET_DISCONNECT_ME:
  1007. goto hangup;
  1008. case LWSSSSRET_DESTROY_ME:
  1009. return LWSSSSRET_DESTROY_ME;
  1010. }
  1011. swallow:
  1012. break;
  1013. default:
  1014. goto hangup;
  1015. }
  1016. }
  1017. return LWSSSSRET_OK;
  1018. hangup:
  1019. return LWSSSSRET_DISCONNECT_ME;
  1020. }