options.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Multipath TCP
  3. *
  4. * Copyright (c) 2017 - 2019, Intel Corporation.
  5. */
  6. #define pr_fmt(fmt) "MPTCP: " fmt
  7. #include <linux/kernel.h>
  8. #include <crypto/sha2.h>
  9. #include <net/tcp.h>
  10. #include <net/mptcp.h>
  11. #include "protocol.h"
  12. #include "mib.h"
  13. #include <trace/events/mptcp.h>
  14. static bool mptcp_cap_flag_sha256(u8 flags)
  15. {
  16. return (flags & MPTCP_CAP_FLAG_MASK) == MPTCP_CAP_HMAC_SHA256;
  17. }
  18. static void mptcp_parse_option(const struct sk_buff *skb,
  19. const unsigned char *ptr, int opsize,
  20. struct mptcp_options_received *mp_opt)
  21. {
  22. u8 subtype = *ptr >> 4;
  23. int expected_opsize;
  24. u16 subopt;
  25. u8 version;
  26. u8 flags;
  27. u8 i;
  28. switch (subtype) {
  29. case MPTCPOPT_MP_CAPABLE:
  30. /* strict size checking */
  31. if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
  32. if (skb->len > tcp_hdr(skb)->doff << 2)
  33. expected_opsize = TCPOLEN_MPTCP_MPC_ACK_DATA;
  34. else
  35. expected_opsize = TCPOLEN_MPTCP_MPC_ACK;
  36. subopt = OPTION_MPTCP_MPC_ACK;
  37. } else {
  38. if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) {
  39. expected_opsize = TCPOLEN_MPTCP_MPC_SYNACK;
  40. subopt = OPTION_MPTCP_MPC_SYNACK;
  41. } else {
  42. expected_opsize = TCPOLEN_MPTCP_MPC_SYN;
  43. subopt = OPTION_MPTCP_MPC_SYN;
  44. }
  45. }
  46. /* Cfr RFC 8684 Section 3.3.0:
  47. * If a checksum is present but its use had
  48. * not been negotiated in the MP_CAPABLE handshake, the receiver MUST
  49. * close the subflow with a RST, as it is not behaving as negotiated.
  50. * If a checksum is not present when its use has been negotiated, the
  51. * receiver MUST close the subflow with a RST, as it is considered
  52. * broken
  53. * We parse even option with mismatching csum presence, so that
  54. * later in subflow_data_ready we can trigger the reset.
  55. */
  56. if (opsize != expected_opsize &&
  57. (expected_opsize != TCPOLEN_MPTCP_MPC_ACK_DATA ||
  58. opsize != TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM))
  59. break;
  60. /* try to be gentle vs future versions on the initial syn */
  61. version = *ptr++ & MPTCP_VERSION_MASK;
  62. if (opsize != TCPOLEN_MPTCP_MPC_SYN) {
  63. if (version != MPTCP_SUPPORTED_VERSION)
  64. break;
  65. } else if (version < MPTCP_SUPPORTED_VERSION) {
  66. break;
  67. }
  68. flags = *ptr++;
  69. if (!mptcp_cap_flag_sha256(flags) ||
  70. (flags & MPTCP_CAP_EXTENSIBILITY))
  71. break;
  72. /* RFC 6824, Section 3.1:
  73. * "For the Checksum Required bit (labeled "A"), if either
  74. * host requires the use of checksums, checksums MUST be used.
  75. * In other words, the only way for checksums not to be used
  76. * is if both hosts in their SYNs set A=0."
  77. */
  78. if (flags & MPTCP_CAP_CHECKSUM_REQD)
  79. mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
  80. mp_opt->deny_join_id0 = !!(flags & MPTCP_CAP_DENY_JOIN_ID0);
  81. mp_opt->suboptions |= subopt;
  82. if (opsize >= TCPOLEN_MPTCP_MPC_SYNACK) {
  83. mp_opt->sndr_key = get_unaligned_be64(ptr);
  84. ptr += 8;
  85. }
  86. if (opsize >= TCPOLEN_MPTCP_MPC_ACK) {
  87. mp_opt->rcvr_key = get_unaligned_be64(ptr);
  88. ptr += 8;
  89. }
  90. if (opsize >= TCPOLEN_MPTCP_MPC_ACK_DATA) {
  91. /* Section 3.1.:
  92. * "the data parameters in a MP_CAPABLE are semantically
  93. * equivalent to those in a DSS option and can be used
  94. * interchangeably."
  95. */
  96. mp_opt->suboptions |= OPTION_MPTCP_DSS;
  97. mp_opt->use_map = 1;
  98. mp_opt->mpc_map = 1;
  99. mp_opt->data_len = get_unaligned_be16(ptr);
  100. ptr += 2;
  101. }
  102. if (opsize == TCPOLEN_MPTCP_MPC_ACK_DATA_CSUM) {
  103. mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
  104. mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
  105. ptr += 2;
  106. }
  107. pr_debug("MP_CAPABLE version=%x, flags=%x, optlen=%d sndr=%llu, rcvr=%llu len=%d csum=%u\n",
  108. version, flags, opsize, mp_opt->sndr_key,
  109. mp_opt->rcvr_key, mp_opt->data_len, mp_opt->csum);
  110. break;
  111. case MPTCPOPT_MP_JOIN:
  112. if (opsize == TCPOLEN_MPTCP_MPJ_SYN) {
  113. mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYN;
  114. mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
  115. mp_opt->join_id = *ptr++;
  116. mp_opt->token = get_unaligned_be32(ptr);
  117. ptr += 4;
  118. mp_opt->nonce = get_unaligned_be32(ptr);
  119. ptr += 4;
  120. pr_debug("MP_JOIN bkup=%u, id=%u, token=%u, nonce=%u\n",
  121. mp_opt->backup, mp_opt->join_id,
  122. mp_opt->token, mp_opt->nonce);
  123. } else if (opsize == TCPOLEN_MPTCP_MPJ_SYNACK) {
  124. mp_opt->suboptions |= OPTION_MPTCP_MPJ_SYNACK;
  125. mp_opt->backup = *ptr++ & MPTCPOPT_BACKUP;
  126. mp_opt->join_id = *ptr++;
  127. mp_opt->thmac = get_unaligned_be64(ptr);
  128. ptr += 8;
  129. mp_opt->nonce = get_unaligned_be32(ptr);
  130. ptr += 4;
  131. pr_debug("MP_JOIN bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
  132. mp_opt->backup, mp_opt->join_id,
  133. mp_opt->thmac, mp_opt->nonce);
  134. } else if (opsize == TCPOLEN_MPTCP_MPJ_ACK) {
  135. mp_opt->suboptions |= OPTION_MPTCP_MPJ_ACK;
  136. ptr += 2;
  137. memcpy(mp_opt->hmac, ptr, MPTCPOPT_HMAC_LEN);
  138. pr_debug("MP_JOIN hmac\n");
  139. }
  140. break;
  141. case MPTCPOPT_DSS:
  142. pr_debug("DSS\n");
  143. ptr++;
  144. flags = (*ptr++) & MPTCP_DSS_FLAG_MASK;
  145. mp_opt->data_fin = (flags & MPTCP_DSS_DATA_FIN) != 0;
  146. mp_opt->dsn64 = (flags & MPTCP_DSS_DSN64) != 0;
  147. mp_opt->use_map = (flags & MPTCP_DSS_HAS_MAP) != 0;
  148. mp_opt->ack64 = (flags & MPTCP_DSS_ACK64) != 0;
  149. mp_opt->use_ack = (flags & MPTCP_DSS_HAS_ACK);
  150. pr_debug("data_fin=%d dsn64=%d use_map=%d ack64=%d use_ack=%d\n",
  151. mp_opt->data_fin, mp_opt->dsn64,
  152. mp_opt->use_map, mp_opt->ack64,
  153. mp_opt->use_ack);
  154. expected_opsize = TCPOLEN_MPTCP_DSS_BASE;
  155. if (mp_opt->use_ack) {
  156. if (mp_opt->ack64)
  157. expected_opsize += TCPOLEN_MPTCP_DSS_ACK64;
  158. else
  159. expected_opsize += TCPOLEN_MPTCP_DSS_ACK32;
  160. }
  161. if (mp_opt->use_map) {
  162. if (mp_opt->dsn64)
  163. expected_opsize += TCPOLEN_MPTCP_DSS_MAP64;
  164. else
  165. expected_opsize += TCPOLEN_MPTCP_DSS_MAP32;
  166. }
  167. /* Always parse any csum presence combination, we will enforce
  168. * RFC 8684 Section 3.3.0 checks later in subflow_data_ready
  169. */
  170. if (opsize != expected_opsize &&
  171. opsize != expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM)
  172. break;
  173. mp_opt->suboptions |= OPTION_MPTCP_DSS;
  174. if (mp_opt->use_ack) {
  175. if (mp_opt->ack64) {
  176. mp_opt->data_ack = get_unaligned_be64(ptr);
  177. ptr += 8;
  178. } else {
  179. mp_opt->data_ack = get_unaligned_be32(ptr);
  180. ptr += 4;
  181. }
  182. pr_debug("data_ack=%llu\n", mp_opt->data_ack);
  183. }
  184. if (mp_opt->use_map) {
  185. if (mp_opt->dsn64) {
  186. mp_opt->data_seq = get_unaligned_be64(ptr);
  187. ptr += 8;
  188. } else {
  189. mp_opt->data_seq = get_unaligned_be32(ptr);
  190. ptr += 4;
  191. }
  192. mp_opt->subflow_seq = get_unaligned_be32(ptr);
  193. ptr += 4;
  194. mp_opt->data_len = get_unaligned_be16(ptr);
  195. ptr += 2;
  196. if (opsize == expected_opsize + TCPOLEN_MPTCP_DSS_CHECKSUM) {
  197. mp_opt->suboptions |= OPTION_MPTCP_CSUMREQD;
  198. mp_opt->csum = get_unaligned((__force __sum16 *)ptr);
  199. ptr += 2;
  200. }
  201. pr_debug("data_seq=%llu subflow_seq=%u data_len=%u csum=%d:%u\n",
  202. mp_opt->data_seq, mp_opt->subflow_seq,
  203. mp_opt->data_len, !!(mp_opt->suboptions & OPTION_MPTCP_CSUMREQD),
  204. mp_opt->csum);
  205. }
  206. break;
  207. case MPTCPOPT_ADD_ADDR:
  208. mp_opt->echo = (*ptr++) & MPTCP_ADDR_ECHO;
  209. if (!mp_opt->echo) {
  210. if (opsize == TCPOLEN_MPTCP_ADD_ADDR ||
  211. opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT)
  212. mp_opt->addr.family = AF_INET;
  213. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  214. else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6 ||
  215. opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT)
  216. mp_opt->addr.family = AF_INET6;
  217. #endif
  218. else
  219. break;
  220. } else {
  221. if (opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE ||
  222. opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT)
  223. mp_opt->addr.family = AF_INET;
  224. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  225. else if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE ||
  226. opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT)
  227. mp_opt->addr.family = AF_INET6;
  228. #endif
  229. else
  230. break;
  231. }
  232. mp_opt->suboptions |= OPTION_MPTCP_ADD_ADDR;
  233. mp_opt->addr.id = *ptr++;
  234. mp_opt->addr.port = 0;
  235. mp_opt->ahmac = 0;
  236. if (mp_opt->addr.family == AF_INET) {
  237. memcpy((u8 *)&mp_opt->addr.addr.s_addr, (u8 *)ptr, 4);
  238. ptr += 4;
  239. if (opsize == TCPOLEN_MPTCP_ADD_ADDR_PORT ||
  240. opsize == TCPOLEN_MPTCP_ADD_ADDR_BASE_PORT) {
  241. mp_opt->addr.port = htons(get_unaligned_be16(ptr));
  242. ptr += 2;
  243. }
  244. }
  245. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  246. else {
  247. memcpy(mp_opt->addr.addr6.s6_addr, (u8 *)ptr, 16);
  248. ptr += 16;
  249. if (opsize == TCPOLEN_MPTCP_ADD_ADDR6_PORT ||
  250. opsize == TCPOLEN_MPTCP_ADD_ADDR6_BASE_PORT) {
  251. mp_opt->addr.port = htons(get_unaligned_be16(ptr));
  252. ptr += 2;
  253. }
  254. }
  255. #endif
  256. if (!mp_opt->echo) {
  257. mp_opt->ahmac = get_unaligned_be64(ptr);
  258. ptr += 8;
  259. }
  260. pr_debug("ADD_ADDR%s: id=%d, ahmac=%llu, echo=%d, port=%d\n",
  261. (mp_opt->addr.family == AF_INET6) ? "6" : "",
  262. mp_opt->addr.id, mp_opt->ahmac, mp_opt->echo, ntohs(mp_opt->addr.port));
  263. break;
  264. case MPTCPOPT_RM_ADDR:
  265. if (opsize < TCPOLEN_MPTCP_RM_ADDR_BASE + 1 ||
  266. opsize > TCPOLEN_MPTCP_RM_ADDR_BASE + MPTCP_RM_IDS_MAX)
  267. break;
  268. ptr++;
  269. mp_opt->suboptions |= OPTION_MPTCP_RM_ADDR;
  270. mp_opt->rm_list.nr = opsize - TCPOLEN_MPTCP_RM_ADDR_BASE;
  271. for (i = 0; i < mp_opt->rm_list.nr; i++)
  272. mp_opt->rm_list.ids[i] = *ptr++;
  273. pr_debug("RM_ADDR: rm_list_nr=%d\n", mp_opt->rm_list.nr);
  274. break;
  275. case MPTCPOPT_MP_PRIO:
  276. if (opsize != TCPOLEN_MPTCP_PRIO)
  277. break;
  278. mp_opt->suboptions |= OPTION_MPTCP_PRIO;
  279. mp_opt->backup = *ptr++ & MPTCP_PRIO_BKUP;
  280. pr_debug("MP_PRIO: prio=%d\n", mp_opt->backup);
  281. break;
  282. case MPTCPOPT_MP_FASTCLOSE:
  283. if (opsize != TCPOLEN_MPTCP_FASTCLOSE)
  284. break;
  285. ptr += 2;
  286. mp_opt->rcvr_key = get_unaligned_be64(ptr);
  287. ptr += 8;
  288. mp_opt->suboptions |= OPTION_MPTCP_FASTCLOSE;
  289. pr_debug("MP_FASTCLOSE: recv_key=%llu\n", mp_opt->rcvr_key);
  290. break;
  291. case MPTCPOPT_RST:
  292. if (opsize != TCPOLEN_MPTCP_RST)
  293. break;
  294. if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST))
  295. break;
  296. mp_opt->suboptions |= OPTION_MPTCP_RST;
  297. flags = *ptr++;
  298. mp_opt->reset_transient = flags & MPTCP_RST_TRANSIENT;
  299. mp_opt->reset_reason = *ptr;
  300. pr_debug("MP_RST: transient=%u reason=%u\n",
  301. mp_opt->reset_transient, mp_opt->reset_reason);
  302. break;
  303. case MPTCPOPT_MP_FAIL:
  304. if (opsize != TCPOLEN_MPTCP_FAIL)
  305. break;
  306. ptr += 2;
  307. mp_opt->suboptions |= OPTION_MPTCP_FAIL;
  308. mp_opt->fail_seq = get_unaligned_be64(ptr);
  309. pr_debug("MP_FAIL: data_seq=%llu\n", mp_opt->fail_seq);
  310. break;
  311. default:
  312. break;
  313. }
  314. }
  315. void mptcp_get_options(const struct sk_buff *skb,
  316. struct mptcp_options_received *mp_opt)
  317. {
  318. const struct tcphdr *th = tcp_hdr(skb);
  319. const unsigned char *ptr;
  320. int length;
  321. /* Ensure that casting the whole status to u32 is efficient and safe */
  322. BUILD_BUG_ON(sizeof_field(struct mptcp_options_received, status) != sizeof(u32));
  323. BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct mptcp_options_received, status),
  324. sizeof(u32)));
  325. *(u32 *)&mp_opt->status = 0;
  326. length = (th->doff * 4) - sizeof(struct tcphdr);
  327. ptr = (const unsigned char *)(th + 1);
  328. while (length > 0) {
  329. int opcode = *ptr++;
  330. int opsize;
  331. switch (opcode) {
  332. case TCPOPT_EOL:
  333. return;
  334. case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
  335. length--;
  336. continue;
  337. default:
  338. if (length < 2)
  339. return;
  340. opsize = *ptr++;
  341. if (opsize < 2) /* "silly options" */
  342. return;
  343. if (opsize > length)
  344. return; /* don't parse partial options */
  345. if (opcode == TCPOPT_MPTCP)
  346. mptcp_parse_option(skb, ptr, opsize, mp_opt);
  347. ptr += opsize - 2;
  348. length -= opsize;
  349. }
  350. }
  351. }
  352. bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
  353. unsigned int *size, struct mptcp_out_options *opts)
  354. {
  355. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  356. /* we will use snd_isn to detect first pkt [re]transmission
  357. * in mptcp_established_options_mp()
  358. */
  359. subflow->snd_isn = TCP_SKB_CB(skb)->end_seq;
  360. if (subflow->request_mptcp) {
  361. opts->suboptions = OPTION_MPTCP_MPC_SYN;
  362. opts->csum_reqd = mptcp_is_checksum_enabled(sock_net(sk));
  363. opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
  364. *size = TCPOLEN_MPTCP_MPC_SYN;
  365. return true;
  366. } else if (subflow->request_join) {
  367. pr_debug("remote_token=%u, nonce=%u\n", subflow->remote_token,
  368. subflow->local_nonce);
  369. opts->suboptions = OPTION_MPTCP_MPJ_SYN;
  370. opts->join_id = subflow->local_id;
  371. opts->token = subflow->remote_token;
  372. opts->nonce = subflow->local_nonce;
  373. opts->backup = subflow->request_bkup;
  374. *size = TCPOLEN_MPTCP_MPJ_SYN;
  375. return true;
  376. }
  377. return false;
  378. }
  379. static void clear_3rdack_retransmission(struct sock *sk)
  380. {
  381. struct inet_connection_sock *icsk = inet_csk(sk);
  382. sk_stop_timer(sk, &icsk->icsk_delack_timer);
  383. icsk->icsk_ack.timeout = 0;
  384. icsk->icsk_ack.ato = 0;
  385. icsk->icsk_ack.pending &= ~(ICSK_ACK_SCHED | ICSK_ACK_TIMER);
  386. }
  387. static bool mptcp_established_options_mp(struct sock *sk, struct sk_buff *skb,
  388. bool snd_data_fin_enable,
  389. unsigned int *size,
  390. struct mptcp_out_options *opts)
  391. {
  392. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  393. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  394. struct mptcp_ext *mpext;
  395. unsigned int data_len;
  396. u8 len;
  397. /* When skb is not available, we better over-estimate the emitted
  398. * options len. A full DSS option (28 bytes) is longer than
  399. * TCPOLEN_MPTCP_MPC_ACK_DATA(22) or TCPOLEN_MPTCP_MPJ_ACK(24), so
  400. * tell the caller to defer the estimate to
  401. * mptcp_established_options_dss(), which will reserve enough space.
  402. */
  403. if (!skb)
  404. return false;
  405. /* MPC/MPJ needed only on 3rd ack packet, DATA_FIN and TCP shutdown take precedence */
  406. if (READ_ONCE(subflow->fully_established) || snd_data_fin_enable ||
  407. subflow->snd_isn != TCP_SKB_CB(skb)->seq ||
  408. sk->sk_state != TCP_ESTABLISHED)
  409. return false;
  410. if (subflow->mp_capable) {
  411. mpext = mptcp_get_ext(skb);
  412. data_len = mpext ? mpext->data_len : 0;
  413. /* we will check ops->data_len in mptcp_write_options() to
  414. * discriminate between TCPOLEN_MPTCP_MPC_ACK_DATA and
  415. * TCPOLEN_MPTCP_MPC_ACK
  416. */
  417. opts->data_len = data_len;
  418. opts->suboptions = OPTION_MPTCP_MPC_ACK;
  419. opts->sndr_key = subflow->local_key;
  420. opts->rcvr_key = subflow->remote_key;
  421. opts->csum_reqd = READ_ONCE(msk->csum_enabled);
  422. opts->allow_join_id0 = mptcp_allow_join_id0(sock_net(sk));
  423. /* Section 3.1.
  424. * The MP_CAPABLE option is carried on the SYN, SYN/ACK, and ACK
  425. * packets that start the first subflow of an MPTCP connection,
  426. * as well as the first packet that carries data
  427. */
  428. if (data_len > 0) {
  429. len = TCPOLEN_MPTCP_MPC_ACK_DATA;
  430. if (opts->csum_reqd) {
  431. /* we need to propagate more info to csum the pseudo hdr */
  432. opts->data_seq = mpext->data_seq;
  433. opts->subflow_seq = mpext->subflow_seq;
  434. opts->csum = mpext->csum;
  435. len += TCPOLEN_MPTCP_DSS_CHECKSUM;
  436. }
  437. *size = ALIGN(len, 4);
  438. } else {
  439. *size = TCPOLEN_MPTCP_MPC_ACK;
  440. }
  441. pr_debug("subflow=%p, local_key=%llu, remote_key=%llu map_len=%d\n",
  442. subflow, subflow->local_key, subflow->remote_key,
  443. data_len);
  444. return true;
  445. } else if (subflow->mp_join) {
  446. opts->suboptions = OPTION_MPTCP_MPJ_ACK;
  447. memcpy(opts->hmac, subflow->hmac, MPTCPOPT_HMAC_LEN);
  448. *size = TCPOLEN_MPTCP_MPJ_ACK;
  449. pr_debug("subflow=%p\n", subflow);
  450. /* we can use the full delegate action helper only from BH context
  451. * If we are in process context - sk is flushing the backlog at
  452. * socket lock release time - just set the appropriate flag, will
  453. * be handled by the release callback
  454. */
  455. if (sock_owned_by_user(sk))
  456. set_bit(MPTCP_DELEGATE_ACK, &subflow->delegated_status);
  457. else
  458. mptcp_subflow_delegate(subflow, MPTCP_DELEGATE_ACK);
  459. return true;
  460. }
  461. return false;
  462. }
  463. static void mptcp_write_data_fin(struct mptcp_subflow_context *subflow,
  464. struct sk_buff *skb, struct mptcp_ext *ext)
  465. {
  466. /* The write_seq value has already been incremented, so the actual
  467. * sequence number for the DATA_FIN is one less.
  468. */
  469. u64 data_fin_tx_seq = READ_ONCE(mptcp_sk(subflow->conn)->write_seq) - 1;
  470. if (!ext->use_map || !skb->len) {
  471. /* RFC6824 requires a DSS mapping with specific values
  472. * if DATA_FIN is set but no data payload is mapped
  473. */
  474. ext->data_fin = 1;
  475. ext->use_map = 1;
  476. ext->dsn64 = 1;
  477. ext->data_seq = data_fin_tx_seq;
  478. ext->subflow_seq = 0;
  479. ext->data_len = 1;
  480. } else if (ext->data_seq + ext->data_len == data_fin_tx_seq) {
  481. /* If there's an existing DSS mapping and it is the
  482. * final mapping, DATA_FIN consumes 1 additional byte of
  483. * mapping space.
  484. */
  485. ext->data_fin = 1;
  486. ext->data_len++;
  487. }
  488. }
  489. static bool mptcp_established_options_dss(struct sock *sk, struct sk_buff *skb,
  490. bool snd_data_fin_enable,
  491. unsigned int *size,
  492. struct mptcp_out_options *opts)
  493. {
  494. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  495. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  496. unsigned int dss_size = 0;
  497. struct mptcp_ext *mpext;
  498. unsigned int ack_size;
  499. bool ret = false;
  500. u64 ack_seq;
  501. opts->csum_reqd = READ_ONCE(msk->csum_enabled);
  502. mpext = skb ? mptcp_get_ext(skb) : NULL;
  503. if (!skb || (mpext && mpext->use_map) || snd_data_fin_enable) {
  504. unsigned int map_size = TCPOLEN_MPTCP_DSS_BASE + TCPOLEN_MPTCP_DSS_MAP64;
  505. if (mpext) {
  506. if (opts->csum_reqd)
  507. map_size += TCPOLEN_MPTCP_DSS_CHECKSUM;
  508. opts->ext_copy = *mpext;
  509. }
  510. dss_size = map_size;
  511. if (skb && snd_data_fin_enable)
  512. mptcp_write_data_fin(subflow, skb, &opts->ext_copy);
  513. opts->suboptions = OPTION_MPTCP_DSS;
  514. ret = true;
  515. }
  516. /* passive sockets msk will set the 'can_ack' after accept(), even
  517. * if the first subflow may have the already the remote key handy
  518. */
  519. opts->ext_copy.use_ack = 0;
  520. if (!READ_ONCE(msk->can_ack)) {
  521. *size = ALIGN(dss_size, 4);
  522. return ret;
  523. }
  524. ack_seq = READ_ONCE(msk->ack_seq);
  525. if (READ_ONCE(msk->use_64bit_ack)) {
  526. ack_size = TCPOLEN_MPTCP_DSS_ACK64;
  527. opts->ext_copy.data_ack = ack_seq;
  528. opts->ext_copy.ack64 = 1;
  529. } else {
  530. ack_size = TCPOLEN_MPTCP_DSS_ACK32;
  531. opts->ext_copy.data_ack32 = (uint32_t)ack_seq;
  532. opts->ext_copy.ack64 = 0;
  533. }
  534. opts->ext_copy.use_ack = 1;
  535. opts->suboptions = OPTION_MPTCP_DSS;
  536. /* Add kind/length/subtype/flag overhead if mapping is not populated */
  537. if (dss_size == 0)
  538. ack_size += TCPOLEN_MPTCP_DSS_BASE;
  539. dss_size += ack_size;
  540. *size = ALIGN(dss_size, 4);
  541. return true;
  542. }
  543. static u64 add_addr_generate_hmac(u64 key1, u64 key2,
  544. struct mptcp_addr_info *addr)
  545. {
  546. u16 port = ntohs(addr->port);
  547. u8 hmac[SHA256_DIGEST_SIZE];
  548. u8 msg[19];
  549. int i = 0;
  550. msg[i++] = addr->id;
  551. if (addr->family == AF_INET) {
  552. memcpy(&msg[i], &addr->addr.s_addr, 4);
  553. i += 4;
  554. }
  555. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  556. else if (addr->family == AF_INET6) {
  557. memcpy(&msg[i], &addr->addr6.s6_addr, 16);
  558. i += 16;
  559. }
  560. #endif
  561. msg[i++] = port >> 8;
  562. msg[i++] = port & 0xFF;
  563. mptcp_crypto_hmac_sha(key1, key2, msg, i, hmac);
  564. return get_unaligned_be64(&hmac[SHA256_DIGEST_SIZE - sizeof(u64)]);
  565. }
  566. static bool mptcp_established_options_add_addr(struct sock *sk, struct sk_buff *skb,
  567. unsigned int *size,
  568. unsigned int remaining,
  569. struct mptcp_out_options *opts)
  570. {
  571. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  572. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  573. bool drop_other_suboptions = false;
  574. unsigned int opt_size = *size;
  575. struct mptcp_addr_info addr;
  576. bool echo;
  577. int len;
  578. /* add addr will strip the existing options, be sure to avoid breaking
  579. * MPC/MPJ handshakes
  580. */
  581. if (!mptcp_pm_should_add_signal(msk) ||
  582. (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
  583. !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &addr,
  584. &echo, &drop_other_suboptions))
  585. return false;
  586. /*
  587. * Later on, mptcp_write_options() will enforce mutually exclusion with
  588. * DSS, bail out if such option is set and we can't drop it.
  589. */
  590. if (drop_other_suboptions)
  591. remaining += opt_size;
  592. else if (opts->suboptions & OPTION_MPTCP_DSS)
  593. return false;
  594. len = mptcp_add_addr_len(addr.family, echo, !!addr.port);
  595. if (remaining < len)
  596. return false;
  597. *size = len;
  598. if (drop_other_suboptions) {
  599. pr_debug("drop other suboptions\n");
  600. opts->suboptions = 0;
  601. /* note that e.g. DSS could have written into the memory
  602. * aliased by ahmac, we must reset the field here
  603. * to avoid appending the hmac even for ADD_ADDR echo
  604. * options
  605. */
  606. opts->ahmac = 0;
  607. *size -= opt_size;
  608. }
  609. opts->addr = addr;
  610. opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
  611. if (!echo) {
  612. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
  613. opts->ahmac = add_addr_generate_hmac(READ_ONCE(msk->local_key),
  614. READ_ONCE(msk->remote_key),
  615. &opts->addr);
  616. } else {
  617. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADDTX);
  618. }
  619. pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d\n",
  620. opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
  621. return true;
  622. }
  623. static bool mptcp_established_options_rm_addr(struct sock *sk,
  624. unsigned int *size,
  625. unsigned int remaining,
  626. struct mptcp_out_options *opts)
  627. {
  628. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  629. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  630. struct mptcp_rm_list rm_list;
  631. int i, len;
  632. if (!mptcp_pm_should_rm_signal(msk) ||
  633. !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
  634. return false;
  635. len = mptcp_rm_addr_len(&rm_list);
  636. if (len < 0)
  637. return false;
  638. if (remaining < len)
  639. return false;
  640. *size = len;
  641. opts->suboptions |= OPTION_MPTCP_RM_ADDR;
  642. opts->rm_list = rm_list;
  643. for (i = 0; i < opts->rm_list.nr; i++)
  644. pr_debug("rm_list_ids[%d]=%d\n", i, opts->rm_list.ids[i]);
  645. MPTCP_ADD_STATS(sock_net(sk), MPTCP_MIB_RMADDRTX, opts->rm_list.nr);
  646. return true;
  647. }
  648. static bool mptcp_established_options_mp_prio(struct sock *sk,
  649. unsigned int *size,
  650. unsigned int remaining,
  651. struct mptcp_out_options *opts)
  652. {
  653. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  654. /* can't send MP_PRIO with MPC, as they share the same option space:
  655. * 'backup'. Also it makes no sense at all
  656. */
  657. if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
  658. return false;
  659. /* account for the trailing 'nop' option */
  660. if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
  661. return false;
  662. *size = TCPOLEN_MPTCP_PRIO_ALIGN;
  663. opts->suboptions |= OPTION_MPTCP_PRIO;
  664. opts->backup = subflow->request_bkup;
  665. pr_debug("prio=%d\n", opts->backup);
  666. return true;
  667. }
  668. static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
  669. unsigned int *size,
  670. unsigned int remaining,
  671. struct mptcp_out_options *opts)
  672. {
  673. const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  674. if (remaining < TCPOLEN_MPTCP_RST)
  675. return false;
  676. *size = TCPOLEN_MPTCP_RST;
  677. opts->suboptions |= OPTION_MPTCP_RST;
  678. opts->reset_transient = subflow->reset_transient;
  679. opts->reset_reason = subflow->reset_reason;
  680. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
  681. return true;
  682. }
  683. static bool mptcp_established_options_fastclose(struct sock *sk,
  684. unsigned int *size,
  685. unsigned int remaining,
  686. struct mptcp_out_options *opts)
  687. {
  688. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  689. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  690. if (likely(!subflow->send_fastclose))
  691. return false;
  692. if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
  693. return false;
  694. *size = TCPOLEN_MPTCP_FASTCLOSE;
  695. opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
  696. opts->rcvr_key = READ_ONCE(msk->remote_key);
  697. pr_debug("FASTCLOSE key=%llu\n", opts->rcvr_key);
  698. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
  699. return true;
  700. }
  701. static bool mptcp_established_options_mp_fail(struct sock *sk,
  702. unsigned int *size,
  703. unsigned int remaining,
  704. struct mptcp_out_options *opts)
  705. {
  706. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  707. if (likely(!subflow->send_mp_fail))
  708. return false;
  709. if (remaining < TCPOLEN_MPTCP_FAIL)
  710. return false;
  711. *size = TCPOLEN_MPTCP_FAIL;
  712. opts->suboptions |= OPTION_MPTCP_FAIL;
  713. opts->fail_seq = subflow->map_seq;
  714. pr_debug("MP_FAIL fail_seq=%llu\n", opts->fail_seq);
  715. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
  716. return true;
  717. }
  718. bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
  719. unsigned int *size, unsigned int remaining,
  720. struct mptcp_out_options *opts)
  721. {
  722. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  723. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  724. unsigned int opt_size = 0;
  725. bool snd_data_fin;
  726. bool ret = false;
  727. opts->suboptions = 0;
  728. if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
  729. return false;
  730. if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
  731. if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
  732. mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
  733. *size += opt_size;
  734. remaining -= opt_size;
  735. }
  736. /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
  737. if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
  738. *size += opt_size;
  739. remaining -= opt_size;
  740. }
  741. return true;
  742. }
  743. snd_data_fin = mptcp_data_fin_enabled(msk);
  744. if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
  745. ret = true;
  746. else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
  747. unsigned int mp_fail_size;
  748. ret = true;
  749. if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
  750. remaining - opt_size, opts)) {
  751. *size += opt_size + mp_fail_size;
  752. remaining -= opt_size - mp_fail_size;
  753. return true;
  754. }
  755. }
  756. /* we reserved enough space for the above options, and exceeding the
  757. * TCP option space would be fatal
  758. */
  759. if (WARN_ON_ONCE(opt_size > remaining))
  760. return false;
  761. *size += opt_size;
  762. remaining -= opt_size;
  763. if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
  764. *size += opt_size;
  765. remaining -= opt_size;
  766. ret = true;
  767. } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
  768. *size += opt_size;
  769. remaining -= opt_size;
  770. ret = true;
  771. }
  772. if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
  773. *size += opt_size;
  774. remaining -= opt_size;
  775. ret = true;
  776. }
  777. return ret;
  778. }
  779. bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
  780. struct mptcp_out_options *opts)
  781. {
  782. struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
  783. if (subflow_req->mp_capable) {
  784. opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
  785. opts->sndr_key = subflow_req->local_key;
  786. opts->csum_reqd = subflow_req->csum_reqd;
  787. opts->allow_join_id0 = subflow_req->allow_join_id0;
  788. *size = TCPOLEN_MPTCP_MPC_SYNACK;
  789. pr_debug("subflow_req=%p, local_key=%llu\n",
  790. subflow_req, subflow_req->local_key);
  791. return true;
  792. } else if (subflow_req->mp_join) {
  793. opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
  794. opts->backup = subflow_req->request_bkup;
  795. opts->join_id = subflow_req->local_id;
  796. opts->thmac = subflow_req->thmac;
  797. opts->nonce = subflow_req->local_nonce;
  798. pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
  799. subflow_req, opts->backup, opts->join_id,
  800. opts->thmac, opts->nonce);
  801. *size = TCPOLEN_MPTCP_MPJ_SYNACK;
  802. return true;
  803. }
  804. return false;
  805. }
  806. static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
  807. struct mptcp_subflow_context *subflow,
  808. struct sk_buff *skb,
  809. struct mptcp_options_received *mp_opt)
  810. {
  811. /* here we can process OoO, in-window pkts, only in-sequence 4th ack
  812. * will make the subflow fully established
  813. */
  814. if (likely(READ_ONCE(subflow->fully_established))) {
  815. /* on passive sockets, check for 3rd ack retransmission
  816. * note that msk is always set by subflow_syn_recv_sock()
  817. * for mp_join subflows
  818. */
  819. if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
  820. TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
  821. subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
  822. !subflow->request_join)
  823. tcp_send_ack(ssk);
  824. goto check_notify;
  825. }
  826. /* we must process OoO packets before the first subflow is fully
  827. * established. OoO packets are instead a protocol violation
  828. * for MP_JOIN subflows as the peer must not send any data
  829. * before receiving the forth ack - cfr. RFC 8684 section 3.2.
  830. */
  831. if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
  832. if (subflow->mp_join)
  833. goto reset;
  834. if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
  835. goto set_fully_established;
  836. return subflow->mp_capable;
  837. }
  838. if (subflow->remote_key_valid &&
  839. (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
  840. ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) &&
  841. (!mp_opt->echo || subflow->mp_join)))) {
  842. /* subflows are fully established as soon as we get any
  843. * additional ack, including ADD_ADDR.
  844. */
  845. goto set_fully_established;
  846. }
  847. /* If the first established packet does not contain MP_CAPABLE + data
  848. * then fallback to TCP. Fallback scenarios requires a reset for
  849. * MP_JOIN subflows.
  850. */
  851. if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
  852. if (subflow->mp_join)
  853. goto reset;
  854. subflow->mp_capable = 0;
  855. if (!mptcp_try_fallback(ssk))
  856. goto reset;
  857. pr_fallback(msk);
  858. return false;
  859. }
  860. if (unlikely(!READ_ONCE(msk->pm.server_side)))
  861. pr_warn_once("bogus mpc option on established client sk");
  862. set_fully_established:
  863. if (mp_opt->deny_join_id0)
  864. WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
  865. mptcp_data_lock((struct sock *)msk);
  866. __mptcp_subflow_fully_established(msk, subflow, mp_opt);
  867. mptcp_data_unlock((struct sock *)msk);
  868. check_notify:
  869. /* if the subflow is not already linked into the conn_list, we can't
  870. * notify the PM: this subflow is still on the listener queue
  871. * and the PM possibly acquiring the subflow lock could race with
  872. * the listener close
  873. */
  874. if (likely(subflow->pm_notified) || list_empty(&subflow->node))
  875. return true;
  876. subflow->pm_notified = 1;
  877. if (subflow->mp_join) {
  878. clear_3rdack_retransmission(ssk);
  879. mptcp_pm_subflow_established(msk);
  880. } else {
  881. mptcp_pm_fully_established(msk, ssk);
  882. }
  883. return true;
  884. reset:
  885. mptcp_subflow_reset(ssk);
  886. return false;
  887. }
  888. u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
  889. {
  890. u32 old_seq32, cur_seq32;
  891. old_seq32 = (u32)old_seq;
  892. cur_seq32 = (u32)cur_seq;
  893. cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
  894. if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
  895. return cur_seq + (1LL << 32);
  896. /* reverse wrap could happen, too */
  897. if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
  898. return cur_seq - (1LL << 32);
  899. return cur_seq;
  900. }
  901. static void __mptcp_snd_una_update(struct mptcp_sock *msk, u64 new_snd_una)
  902. {
  903. msk->bytes_acked += new_snd_una - msk->snd_una;
  904. WRITE_ONCE(msk->snd_una, new_snd_una);
  905. }
  906. static void ack_update_msk(struct mptcp_sock *msk,
  907. struct sock *ssk,
  908. struct mptcp_options_received *mp_opt)
  909. {
  910. u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
  911. struct sock *sk = (struct sock *)msk;
  912. u64 old_snd_una;
  913. mptcp_data_lock(sk);
  914. /* avoid ack expansion on update conflict, to reduce the risk of
  915. * wrongly expanding to a future ack sequence number, which is way
  916. * more dangerous than missing an ack
  917. */
  918. old_snd_una = msk->snd_una;
  919. new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
  920. /* ACK for data not even sent yet? Ignore.*/
  921. if (unlikely(after64(new_snd_una, snd_nxt)))
  922. new_snd_una = old_snd_una;
  923. new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
  924. if (after64(new_wnd_end, msk->wnd_end))
  925. WRITE_ONCE(msk->wnd_end, new_wnd_end);
  926. /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
  927. if (after64(msk->wnd_end, snd_nxt))
  928. __mptcp_check_push(sk, ssk);
  929. if (after64(new_snd_una, old_snd_una)) {
  930. __mptcp_snd_una_update(msk, new_snd_una);
  931. __mptcp_data_acked(sk);
  932. }
  933. msk->last_ack_recv = tcp_jiffies32;
  934. mptcp_data_unlock(sk);
  935. trace_ack_update_msk(mp_opt->data_ack,
  936. old_snd_una, new_snd_una,
  937. new_wnd_end, READ_ONCE(msk->wnd_end));
  938. }
  939. bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
  940. {
  941. /* Skip if DATA_FIN was already received.
  942. * If updating simultaneously with the recvmsg loop, values
  943. * should match. If they mismatch, the peer is misbehaving and
  944. * we will prefer the most recent information.
  945. */
  946. if (READ_ONCE(msk->rcv_data_fin))
  947. return false;
  948. WRITE_ONCE(msk->rcv_data_fin_seq,
  949. mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
  950. WRITE_ONCE(msk->rcv_data_fin, 1);
  951. return true;
  952. }
  953. static bool add_addr_hmac_valid(struct mptcp_sock *msk,
  954. struct mptcp_options_received *mp_opt)
  955. {
  956. u64 hmac = 0;
  957. if (mp_opt->echo)
  958. return true;
  959. hmac = add_addr_generate_hmac(READ_ONCE(msk->remote_key),
  960. READ_ONCE(msk->local_key),
  961. &mp_opt->addr);
  962. pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
  963. msk, hmac, mp_opt->ahmac);
  964. return hmac == mp_opt->ahmac;
  965. }
  966. /* Return false in case of error (or subflow has been reset),
  967. * else return true.
  968. */
  969. bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
  970. {
  971. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  972. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  973. struct mptcp_options_received mp_opt;
  974. struct mptcp_ext *mpext;
  975. if (__mptcp_check_fallback(msk)) {
  976. /* Keep it simple and unconditionally trigger send data cleanup and
  977. * pending queue spooling. We will need to acquire the data lock
  978. * for more accurate checks, and once the lock is acquired, such
  979. * helpers are cheap.
  980. */
  981. mptcp_data_lock(subflow->conn);
  982. if (sk_stream_memory_free(sk))
  983. __mptcp_check_push(subflow->conn, sk);
  984. /* on fallback we just need to ignore the msk-level snd_una, as
  985. * this is really plain TCP
  986. */
  987. __mptcp_snd_una_update(msk, READ_ONCE(msk->snd_nxt));
  988. __mptcp_data_acked(subflow->conn);
  989. mptcp_data_unlock(subflow->conn);
  990. return true;
  991. }
  992. mptcp_get_options(skb, &mp_opt);
  993. /* The subflow can be in close state only if check_fully_established()
  994. * just sent a reset. If so, tell the caller to ignore the current packet.
  995. */
  996. if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
  997. return sk->sk_state != TCP_CLOSE;
  998. if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
  999. if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
  1000. READ_ONCE(msk->local_key) == mp_opt.rcvr_key) {
  1001. WRITE_ONCE(msk->rcv_fastclose, true);
  1002. mptcp_schedule_work((struct sock *)msk);
  1003. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
  1004. }
  1005. if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
  1006. add_addr_hmac_valid(msk, &mp_opt)) {
  1007. if (!mp_opt.echo) {
  1008. mptcp_pm_add_addr_received(sk, &mp_opt.addr);
  1009. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
  1010. } else {
  1011. mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
  1012. mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
  1013. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
  1014. }
  1015. if (mp_opt.addr.port)
  1016. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
  1017. }
  1018. if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
  1019. mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
  1020. if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
  1021. mptcp_pm_mp_prio_received(sk, mp_opt.backup);
  1022. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
  1023. }
  1024. if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
  1025. mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
  1026. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
  1027. }
  1028. if (mp_opt.suboptions & OPTION_MPTCP_RST) {
  1029. subflow->reset_seen = 1;
  1030. subflow->reset_reason = mp_opt.reset_reason;
  1031. subflow->reset_transient = mp_opt.reset_transient;
  1032. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
  1033. }
  1034. if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
  1035. return true;
  1036. }
  1037. /* we can't wait for recvmsg() to update the ack_seq, otherwise
  1038. * monodirectional flows will stuck
  1039. */
  1040. if (mp_opt.use_ack)
  1041. ack_update_msk(msk, sk, &mp_opt);
  1042. /* Zero-data-length packets are dropped by the caller and not
  1043. * propagated to the MPTCP layer, so the skb extension does not
  1044. * need to be allocated or populated. DATA_FIN information, if
  1045. * present, needs to be updated here before the skb is freed.
  1046. */
  1047. if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
  1048. if (mp_opt.data_fin && mp_opt.data_len == 1 &&
  1049. mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
  1050. mptcp_schedule_work((struct sock *)msk);
  1051. return true;
  1052. }
  1053. mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
  1054. if (!mpext)
  1055. return false;
  1056. memset(mpext, 0, sizeof(*mpext));
  1057. if (likely(mp_opt.use_map)) {
  1058. if (mp_opt.mpc_map) {
  1059. /* this is an MP_CAPABLE carrying MPTCP data
  1060. * we know this map the first chunk of data
  1061. */
  1062. mptcp_crypto_key_sha(subflow->remote_key, NULL,
  1063. &mpext->data_seq);
  1064. mpext->data_seq++;
  1065. mpext->subflow_seq = 1;
  1066. mpext->dsn64 = 1;
  1067. mpext->mpc_map = 1;
  1068. mpext->data_fin = 0;
  1069. } else {
  1070. mpext->data_seq = mp_opt.data_seq;
  1071. mpext->subflow_seq = mp_opt.subflow_seq;
  1072. mpext->dsn64 = mp_opt.dsn64;
  1073. mpext->data_fin = mp_opt.data_fin;
  1074. }
  1075. mpext->data_len = mp_opt.data_len;
  1076. mpext->use_map = 1;
  1077. mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
  1078. if (mpext->csum_reqd)
  1079. mpext->csum = mp_opt.csum;
  1080. }
  1081. return true;
  1082. }
  1083. static void mptcp_set_rwin(struct tcp_sock *tp, struct tcphdr *th)
  1084. {
  1085. const struct sock *ssk = (const struct sock *)tp;
  1086. struct mptcp_subflow_context *subflow;
  1087. u64 ack_seq, rcv_wnd_old, rcv_wnd_new;
  1088. struct mptcp_sock *msk;
  1089. u32 new_win;
  1090. u64 win;
  1091. subflow = mptcp_subflow_ctx(ssk);
  1092. msk = mptcp_sk(subflow->conn);
  1093. ack_seq = READ_ONCE(msk->ack_seq);
  1094. rcv_wnd_new = ack_seq + tp->rcv_wnd;
  1095. rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
  1096. if (after64(rcv_wnd_new, rcv_wnd_old)) {
  1097. u64 rcv_wnd;
  1098. for (;;) {
  1099. rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
  1100. if (rcv_wnd == rcv_wnd_old)
  1101. break;
  1102. rcv_wnd_old = rcv_wnd;
  1103. if (before64(rcv_wnd_new, rcv_wnd_old)) {
  1104. MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
  1105. goto raise_win;
  1106. }
  1107. MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
  1108. }
  1109. goto update_wspace;
  1110. }
  1111. if (rcv_wnd_new != rcv_wnd_old) {
  1112. raise_win:
  1113. win = rcv_wnd_old - ack_seq;
  1114. tp->rcv_wnd = min_t(u64, win, U32_MAX);
  1115. new_win = tp->rcv_wnd;
  1116. /* Make sure we do not exceed the maximum possible
  1117. * scaled window.
  1118. */
  1119. if (unlikely(th->syn))
  1120. new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
  1121. if (!tp->rx_opt.rcv_wscale &&
  1122. READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
  1123. new_win = min(new_win, MAX_TCP_WINDOW);
  1124. else
  1125. new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
  1126. /* RFC1323 scaling applied */
  1127. new_win >>= tp->rx_opt.rcv_wscale;
  1128. th->window = htons(new_win);
  1129. MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
  1130. }
  1131. update_wspace:
  1132. WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
  1133. }
  1134. __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
  1135. {
  1136. struct csum_pseudo_header header;
  1137. __wsum csum;
  1138. /* cfr RFC 8684 3.3.1.:
  1139. * the data sequence number used in the pseudo-header is
  1140. * always the 64-bit value, irrespective of what length is used in the
  1141. * DSS option itself.
  1142. */
  1143. header.data_seq = cpu_to_be64(data_seq);
  1144. header.subflow_seq = htonl(subflow_seq);
  1145. header.data_len = htons(data_len);
  1146. header.csum = 0;
  1147. csum = csum_partial(&header, sizeof(header), sum);
  1148. return csum_fold(csum);
  1149. }
  1150. static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
  1151. {
  1152. return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
  1153. ~csum_unfold(mpext->csum));
  1154. }
  1155. static void put_len_csum(u16 len, __sum16 csum, void *data)
  1156. {
  1157. __sum16 *sumptr = data + 2;
  1158. __be16 *ptr = data;
  1159. put_unaligned_be16(len, ptr);
  1160. put_unaligned(csum, sumptr);
  1161. }
  1162. void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
  1163. struct mptcp_out_options *opts)
  1164. {
  1165. const struct sock *ssk = (const struct sock *)tp;
  1166. struct mptcp_subflow_context *subflow;
  1167. /* Which options can be used together?
  1168. *
  1169. * X: mutually exclusive
  1170. * O: often used together
  1171. * C: can be used together in some cases
  1172. * P: could be used together but we prefer not to (optimisations)
  1173. *
  1174. * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
  1175. * ------|------|------|------|------|------|------|------|------|
  1176. * MPC |------|------|------|------|------|------|------|------|
  1177. * MPJ | X |------|------|------|------|------|------|------|
  1178. * DSS | X | X |------|------|------|------|------|------|
  1179. * ADD | X | X | P |------|------|------|------|------|
  1180. * RM | C | C | C | P |------|------|------|------|
  1181. * PRIO | X | C | C | C | C |------|------|------|
  1182. * FAIL | X | X | C | X | X | X |------|------|
  1183. * FC | X | X | X | X | X | X | X |------|
  1184. * RST | X | X | X | X | X | X | O | O |
  1185. * ------|------|------|------|------|------|------|------|------|
  1186. *
  1187. * The same applies in mptcp_established_options() function.
  1188. */
  1189. if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
  1190. struct mptcp_ext *mpext = &opts->ext_copy;
  1191. u8 len = TCPOLEN_MPTCP_DSS_BASE;
  1192. u8 flags = 0;
  1193. if (mpext->use_ack) {
  1194. flags = MPTCP_DSS_HAS_ACK;
  1195. if (mpext->ack64) {
  1196. len += TCPOLEN_MPTCP_DSS_ACK64;
  1197. flags |= MPTCP_DSS_ACK64;
  1198. } else {
  1199. len += TCPOLEN_MPTCP_DSS_ACK32;
  1200. }
  1201. }
  1202. if (mpext->use_map) {
  1203. len += TCPOLEN_MPTCP_DSS_MAP64;
  1204. /* Use only 64-bit mapping flags for now, add
  1205. * support for optional 32-bit mappings later.
  1206. */
  1207. flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
  1208. if (mpext->data_fin)
  1209. flags |= MPTCP_DSS_DATA_FIN;
  1210. if (opts->csum_reqd)
  1211. len += TCPOLEN_MPTCP_DSS_CHECKSUM;
  1212. }
  1213. *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
  1214. if (mpext->use_ack) {
  1215. if (mpext->ack64) {
  1216. put_unaligned_be64(mpext->data_ack, ptr);
  1217. ptr += 2;
  1218. } else {
  1219. put_unaligned_be32(mpext->data_ack32, ptr);
  1220. ptr += 1;
  1221. }
  1222. }
  1223. if (mpext->use_map) {
  1224. put_unaligned_be64(mpext->data_seq, ptr);
  1225. ptr += 2;
  1226. put_unaligned_be32(mpext->subflow_seq, ptr);
  1227. ptr += 1;
  1228. if (opts->csum_reqd) {
  1229. /* data_len == 0 is reserved for the infinite mapping,
  1230. * the checksum will also be set to 0.
  1231. */
  1232. put_len_csum(mpext->data_len,
  1233. (mpext->data_len ? mptcp_make_csum(mpext) : 0),
  1234. ptr);
  1235. } else {
  1236. put_unaligned_be32(mpext->data_len << 16 |
  1237. TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
  1238. }
  1239. ptr += 1;
  1240. }
  1241. /* We might need to add MP_FAIL options in rare cases */
  1242. if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
  1243. goto mp_fail;
  1244. } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
  1245. u8 len, flag = MPTCP_CAP_HMAC_SHA256;
  1246. if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
  1247. len = TCPOLEN_MPTCP_MPC_SYN;
  1248. } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
  1249. len = TCPOLEN_MPTCP_MPC_SYNACK;
  1250. } else if (opts->data_len) {
  1251. len = TCPOLEN_MPTCP_MPC_ACK_DATA;
  1252. if (opts->csum_reqd)
  1253. len += TCPOLEN_MPTCP_DSS_CHECKSUM;
  1254. } else {
  1255. len = TCPOLEN_MPTCP_MPC_ACK;
  1256. }
  1257. if (opts->csum_reqd)
  1258. flag |= MPTCP_CAP_CHECKSUM_REQD;
  1259. if (!opts->allow_join_id0)
  1260. flag |= MPTCP_CAP_DENY_JOIN_ID0;
  1261. *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
  1262. MPTCP_SUPPORTED_VERSION,
  1263. flag);
  1264. if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
  1265. opts->suboptions))
  1266. goto mp_capable_done;
  1267. put_unaligned_be64(opts->sndr_key, ptr);
  1268. ptr += 2;
  1269. if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
  1270. goto mp_capable_done;
  1271. put_unaligned_be64(opts->rcvr_key, ptr);
  1272. ptr += 2;
  1273. if (!opts->data_len)
  1274. goto mp_capable_done;
  1275. if (opts->csum_reqd) {
  1276. put_len_csum(opts->data_len,
  1277. __mptcp_make_csum(opts->data_seq,
  1278. opts->subflow_seq,
  1279. opts->data_len,
  1280. ~csum_unfold(opts->csum)),
  1281. ptr);
  1282. } else {
  1283. put_unaligned_be32(opts->data_len << 16 |
  1284. TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
  1285. }
  1286. ptr += 1;
  1287. /* MPC is additionally mutually exclusive with MP_PRIO */
  1288. goto mp_capable_done;
  1289. } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
  1290. if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
  1291. *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
  1292. TCPOLEN_MPTCP_MPJ_SYN,
  1293. opts->backup, opts->join_id);
  1294. put_unaligned_be32(opts->token, ptr);
  1295. ptr += 1;
  1296. put_unaligned_be32(opts->nonce, ptr);
  1297. ptr += 1;
  1298. } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
  1299. *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
  1300. TCPOLEN_MPTCP_MPJ_SYNACK,
  1301. opts->backup, opts->join_id);
  1302. put_unaligned_be64(opts->thmac, ptr);
  1303. ptr += 2;
  1304. put_unaligned_be32(opts->nonce, ptr);
  1305. ptr += 1;
  1306. } else {
  1307. *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
  1308. TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
  1309. memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
  1310. ptr += 5;
  1311. }
  1312. } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
  1313. u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
  1314. u8 echo = MPTCP_ADDR_ECHO;
  1315. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  1316. if (opts->addr.family == AF_INET6)
  1317. len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
  1318. #endif
  1319. if (opts->addr.port)
  1320. len += TCPOLEN_MPTCP_PORT_LEN;
  1321. if (opts->ahmac) {
  1322. len += sizeof(opts->ahmac);
  1323. echo = 0;
  1324. }
  1325. *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
  1326. len, echo, opts->addr.id);
  1327. if (opts->addr.family == AF_INET) {
  1328. memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
  1329. ptr += 1;
  1330. }
  1331. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  1332. else if (opts->addr.family == AF_INET6) {
  1333. memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
  1334. ptr += 4;
  1335. }
  1336. #endif
  1337. if (!opts->addr.port) {
  1338. if (opts->ahmac) {
  1339. put_unaligned_be64(opts->ahmac, ptr);
  1340. ptr += 2;
  1341. }
  1342. } else {
  1343. u16 port = ntohs(opts->addr.port);
  1344. if (opts->ahmac) {
  1345. u8 *bptr = (u8 *)ptr;
  1346. put_unaligned_be16(port, bptr);
  1347. bptr += 2;
  1348. put_unaligned_be64(opts->ahmac, bptr);
  1349. bptr += 8;
  1350. put_unaligned_be16(TCPOPT_NOP << 8 |
  1351. TCPOPT_NOP, bptr);
  1352. ptr += 3;
  1353. } else {
  1354. put_unaligned_be32(port << 16 |
  1355. TCPOPT_NOP << 8 |
  1356. TCPOPT_NOP, ptr);
  1357. ptr += 1;
  1358. }
  1359. }
  1360. } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
  1361. /* FASTCLOSE is mutually exclusive with others except RST */
  1362. *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
  1363. TCPOLEN_MPTCP_FASTCLOSE,
  1364. 0, 0);
  1365. put_unaligned_be64(opts->rcvr_key, ptr);
  1366. ptr += 2;
  1367. if (OPTION_MPTCP_RST & opts->suboptions)
  1368. goto mp_rst;
  1369. return;
  1370. } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
  1371. mp_fail:
  1372. /* MP_FAIL is mutually exclusive with others except RST */
  1373. subflow = mptcp_subflow_ctx(ssk);
  1374. subflow->send_mp_fail = 0;
  1375. *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
  1376. TCPOLEN_MPTCP_FAIL,
  1377. 0, 0);
  1378. put_unaligned_be64(opts->fail_seq, ptr);
  1379. ptr += 2;
  1380. if (OPTION_MPTCP_RST & opts->suboptions)
  1381. goto mp_rst;
  1382. return;
  1383. } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
  1384. mp_rst:
  1385. *ptr++ = mptcp_option(MPTCPOPT_RST,
  1386. TCPOLEN_MPTCP_RST,
  1387. opts->reset_transient,
  1388. opts->reset_reason);
  1389. return;
  1390. }
  1391. if (OPTION_MPTCP_PRIO & opts->suboptions) {
  1392. subflow = mptcp_subflow_ctx(ssk);
  1393. subflow->send_mp_prio = 0;
  1394. *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
  1395. TCPOLEN_MPTCP_PRIO,
  1396. opts->backup, TCPOPT_NOP);
  1397. MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
  1398. }
  1399. mp_capable_done:
  1400. if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
  1401. u8 i = 1;
  1402. *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
  1403. TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
  1404. 0, opts->rm_list.ids[0]);
  1405. while (i < opts->rm_list.nr) {
  1406. u8 id1, id2, id3, id4;
  1407. id1 = opts->rm_list.ids[i];
  1408. id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
  1409. id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
  1410. id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
  1411. put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
  1412. ptr += 1;
  1413. i += 4;
  1414. }
  1415. }
  1416. if (tp)
  1417. mptcp_set_rwin(tp, th);
  1418. }
  1419. __be32 mptcp_get_reset_option(const struct sk_buff *skb)
  1420. {
  1421. const struct mptcp_ext *ext = mptcp_get_ext(skb);
  1422. u8 flags, reason;
  1423. if (ext) {
  1424. flags = ext->reset_transient;
  1425. reason = ext->reset_reason;
  1426. return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
  1427. flags, reason);
  1428. }
  1429. return htonl(0u);
  1430. }
  1431. EXPORT_SYMBOL_GPL(mptcp_get_reset_option);