options.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  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. bool echo;
  576. int len;
  577. /* add addr will strip the existing options, be sure to avoid breaking
  578. * MPC/MPJ handshakes
  579. */
  580. if (!mptcp_pm_should_add_signal(msk) ||
  581. (opts->suboptions & (OPTION_MPTCP_MPJ_ACK | OPTION_MPTCP_MPC_ACK)) ||
  582. !mptcp_pm_add_addr_signal(msk, skb, opt_size, remaining, &opts->addr,
  583. &echo, &drop_other_suboptions))
  584. return false;
  585. /*
  586. * Later on, mptcp_write_options() will enforce mutually exclusion with
  587. * DSS, bail out if such option is set and we can't drop it.
  588. */
  589. if (drop_other_suboptions)
  590. remaining += opt_size;
  591. else if (opts->suboptions & OPTION_MPTCP_DSS)
  592. return false;
  593. len = mptcp_add_addr_len(opts->addr.family, echo, !!opts->addr.port);
  594. if (remaining < len)
  595. return false;
  596. *size = len;
  597. if (drop_other_suboptions) {
  598. pr_debug("drop other suboptions\n");
  599. opts->suboptions = 0;
  600. /* note that e.g. DSS could have written into the memory
  601. * aliased by ahmac, we must reset the field here
  602. * to avoid appending the hmac even for ADD_ADDR echo
  603. * options
  604. */
  605. opts->ahmac = 0;
  606. *size -= opt_size;
  607. }
  608. opts->suboptions |= OPTION_MPTCP_ADD_ADDR;
  609. if (!echo) {
  610. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDRTX);
  611. opts->ahmac = add_addr_generate_hmac(READ_ONCE(msk->local_key),
  612. READ_ONCE(msk->remote_key),
  613. &opts->addr);
  614. } else {
  615. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADDTX);
  616. }
  617. pr_debug("addr_id=%d, ahmac=%llu, echo=%d, port=%d\n",
  618. opts->addr.id, opts->ahmac, echo, ntohs(opts->addr.port));
  619. return true;
  620. }
  621. static bool mptcp_established_options_rm_addr(struct sock *sk,
  622. unsigned int *size,
  623. unsigned int remaining,
  624. struct mptcp_out_options *opts)
  625. {
  626. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  627. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  628. struct mptcp_rm_list rm_list;
  629. int i, len;
  630. if (!mptcp_pm_should_rm_signal(msk) ||
  631. !(mptcp_pm_rm_addr_signal(msk, remaining, &rm_list)))
  632. return false;
  633. len = mptcp_rm_addr_len(&rm_list);
  634. if (len < 0)
  635. return false;
  636. if (remaining < len)
  637. return false;
  638. *size = len;
  639. opts->suboptions |= OPTION_MPTCP_RM_ADDR;
  640. opts->rm_list = rm_list;
  641. for (i = 0; i < opts->rm_list.nr; i++)
  642. pr_debug("rm_list_ids[%d]=%d\n", i, opts->rm_list.ids[i]);
  643. MPTCP_ADD_STATS(sock_net(sk), MPTCP_MIB_RMADDRTX, opts->rm_list.nr);
  644. return true;
  645. }
  646. static bool mptcp_established_options_mp_prio(struct sock *sk,
  647. unsigned int *size,
  648. unsigned int remaining,
  649. struct mptcp_out_options *opts)
  650. {
  651. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  652. /* can't send MP_PRIO with MPC, as they share the same option space:
  653. * 'backup'. Also it makes no sense at all
  654. */
  655. if (!subflow->send_mp_prio || (opts->suboptions & OPTIONS_MPTCP_MPC))
  656. return false;
  657. /* account for the trailing 'nop' option */
  658. if (remaining < TCPOLEN_MPTCP_PRIO_ALIGN)
  659. return false;
  660. *size = TCPOLEN_MPTCP_PRIO_ALIGN;
  661. opts->suboptions |= OPTION_MPTCP_PRIO;
  662. opts->backup = subflow->request_bkup;
  663. pr_debug("prio=%d\n", opts->backup);
  664. return true;
  665. }
  666. static noinline bool mptcp_established_options_rst(struct sock *sk, struct sk_buff *skb,
  667. unsigned int *size,
  668. unsigned int remaining,
  669. struct mptcp_out_options *opts)
  670. {
  671. const struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  672. if (remaining < TCPOLEN_MPTCP_RST)
  673. return false;
  674. *size = TCPOLEN_MPTCP_RST;
  675. opts->suboptions |= OPTION_MPTCP_RST;
  676. opts->reset_transient = subflow->reset_transient;
  677. opts->reset_reason = subflow->reset_reason;
  678. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTTX);
  679. return true;
  680. }
  681. static bool mptcp_established_options_fastclose(struct sock *sk,
  682. unsigned int *size,
  683. unsigned int remaining,
  684. struct mptcp_out_options *opts)
  685. {
  686. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  687. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  688. if (likely(!subflow->send_fastclose))
  689. return false;
  690. if (remaining < TCPOLEN_MPTCP_FASTCLOSE)
  691. return false;
  692. *size = TCPOLEN_MPTCP_FASTCLOSE;
  693. opts->suboptions |= OPTION_MPTCP_FASTCLOSE;
  694. opts->rcvr_key = READ_ONCE(msk->remote_key);
  695. pr_debug("FASTCLOSE key=%llu\n", opts->rcvr_key);
  696. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSETX);
  697. return true;
  698. }
  699. static bool mptcp_established_options_mp_fail(struct sock *sk,
  700. unsigned int *size,
  701. unsigned int remaining,
  702. struct mptcp_out_options *opts)
  703. {
  704. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  705. if (likely(!subflow->send_mp_fail))
  706. return false;
  707. if (remaining < TCPOLEN_MPTCP_FAIL)
  708. return false;
  709. *size = TCPOLEN_MPTCP_FAIL;
  710. opts->suboptions |= OPTION_MPTCP_FAIL;
  711. opts->fail_seq = subflow->map_seq;
  712. pr_debug("MP_FAIL fail_seq=%llu\n", opts->fail_seq);
  713. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILTX);
  714. return true;
  715. }
  716. bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
  717. unsigned int *size, unsigned int remaining,
  718. struct mptcp_out_options *opts)
  719. {
  720. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  721. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  722. unsigned int opt_size = 0;
  723. bool snd_data_fin;
  724. bool ret = false;
  725. opts->suboptions = 0;
  726. if (unlikely(__mptcp_check_fallback(msk) && !mptcp_check_infinite_map(skb)))
  727. return false;
  728. if (unlikely(skb && TCP_SKB_CB(skb)->tcp_flags & TCPHDR_RST)) {
  729. if (mptcp_established_options_fastclose(sk, &opt_size, remaining, opts) ||
  730. mptcp_established_options_mp_fail(sk, &opt_size, remaining, opts)) {
  731. *size += opt_size;
  732. remaining -= opt_size;
  733. }
  734. /* MP_RST can be used with MP_FASTCLOSE and MP_FAIL if there is room */
  735. if (mptcp_established_options_rst(sk, skb, &opt_size, remaining, opts)) {
  736. *size += opt_size;
  737. remaining -= opt_size;
  738. }
  739. return true;
  740. }
  741. snd_data_fin = mptcp_data_fin_enabled(msk);
  742. if (mptcp_established_options_mp(sk, skb, snd_data_fin, &opt_size, opts))
  743. ret = true;
  744. else if (mptcp_established_options_dss(sk, skb, snd_data_fin, &opt_size, opts)) {
  745. unsigned int mp_fail_size;
  746. ret = true;
  747. if (mptcp_established_options_mp_fail(sk, &mp_fail_size,
  748. remaining - opt_size, opts)) {
  749. *size += opt_size + mp_fail_size;
  750. remaining -= opt_size - mp_fail_size;
  751. return true;
  752. }
  753. }
  754. /* we reserved enough space for the above options, and exceeding the
  755. * TCP option space would be fatal
  756. */
  757. if (WARN_ON_ONCE(opt_size > remaining))
  758. return false;
  759. *size += opt_size;
  760. remaining -= opt_size;
  761. if (mptcp_established_options_add_addr(sk, skb, &opt_size, remaining, opts)) {
  762. *size += opt_size;
  763. remaining -= opt_size;
  764. ret = true;
  765. } else if (mptcp_established_options_rm_addr(sk, &opt_size, remaining, opts)) {
  766. *size += opt_size;
  767. remaining -= opt_size;
  768. ret = true;
  769. }
  770. if (mptcp_established_options_mp_prio(sk, &opt_size, remaining, opts)) {
  771. *size += opt_size;
  772. remaining -= opt_size;
  773. ret = true;
  774. }
  775. return ret;
  776. }
  777. bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
  778. struct mptcp_out_options *opts)
  779. {
  780. struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
  781. if (subflow_req->mp_capable) {
  782. opts->suboptions = OPTION_MPTCP_MPC_SYNACK;
  783. opts->sndr_key = subflow_req->local_key;
  784. opts->csum_reqd = subflow_req->csum_reqd;
  785. opts->allow_join_id0 = subflow_req->allow_join_id0;
  786. *size = TCPOLEN_MPTCP_MPC_SYNACK;
  787. pr_debug("subflow_req=%p, local_key=%llu\n",
  788. subflow_req, subflow_req->local_key);
  789. return true;
  790. } else if (subflow_req->mp_join) {
  791. opts->suboptions = OPTION_MPTCP_MPJ_SYNACK;
  792. opts->backup = subflow_req->request_bkup;
  793. opts->join_id = subflow_req->local_id;
  794. opts->thmac = subflow_req->thmac;
  795. opts->nonce = subflow_req->local_nonce;
  796. pr_debug("req=%p, bkup=%u, id=%u, thmac=%llu, nonce=%u\n",
  797. subflow_req, opts->backup, opts->join_id,
  798. opts->thmac, opts->nonce);
  799. *size = TCPOLEN_MPTCP_MPJ_SYNACK;
  800. return true;
  801. }
  802. return false;
  803. }
  804. static bool check_fully_established(struct mptcp_sock *msk, struct sock *ssk,
  805. struct mptcp_subflow_context *subflow,
  806. struct sk_buff *skb,
  807. struct mptcp_options_received *mp_opt)
  808. {
  809. /* here we can process OoO, in-window pkts, only in-sequence 4th ack
  810. * will make the subflow fully established
  811. */
  812. if (likely(READ_ONCE(subflow->fully_established))) {
  813. /* on passive sockets, check for 3rd ack retransmission
  814. * note that msk is always set by subflow_syn_recv_sock()
  815. * for mp_join subflows
  816. */
  817. if (TCP_SKB_CB(skb)->seq == subflow->ssn_offset + 1 &&
  818. TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq &&
  819. subflow->mp_join && (mp_opt->suboptions & OPTIONS_MPTCP_MPJ) &&
  820. !subflow->request_join)
  821. tcp_send_ack(ssk);
  822. goto check_notify;
  823. }
  824. /* we must process OoO packets before the first subflow is fully
  825. * established. OoO packets are instead a protocol violation
  826. * for MP_JOIN subflows as the peer must not send any data
  827. * before receiving the forth ack - cfr. RFC 8684 section 3.2.
  828. */
  829. if (TCP_SKB_CB(skb)->seq != subflow->ssn_offset + 1) {
  830. if (subflow->mp_join)
  831. goto reset;
  832. if (subflow->is_mptfo && mp_opt->suboptions & OPTION_MPTCP_MPC_ACK)
  833. goto set_fully_established;
  834. return subflow->mp_capable;
  835. }
  836. if (subflow->remote_key_valid &&
  837. (((mp_opt->suboptions & OPTION_MPTCP_DSS) && mp_opt->use_ack) ||
  838. ((mp_opt->suboptions & OPTION_MPTCP_ADD_ADDR) &&
  839. (!mp_opt->echo || subflow->mp_join)))) {
  840. /* subflows are fully established as soon as we get any
  841. * additional ack, including ADD_ADDR.
  842. */
  843. goto set_fully_established;
  844. }
  845. /* If the first established packet does not contain MP_CAPABLE + data
  846. * then fallback to TCP. Fallback scenarios requires a reset for
  847. * MP_JOIN subflows.
  848. */
  849. if (!(mp_opt->suboptions & OPTIONS_MPTCP_MPC)) {
  850. if (subflow->mp_join)
  851. goto reset;
  852. subflow->mp_capable = 0;
  853. pr_fallback(msk);
  854. mptcp_do_fallback(ssk);
  855. return false;
  856. }
  857. if (mp_opt->deny_join_id0)
  858. WRITE_ONCE(msk->pm.remote_deny_join_id0, true);
  859. if (unlikely(!READ_ONCE(msk->pm.server_side)))
  860. pr_warn_once("bogus mpc option on established client sk");
  861. set_fully_established:
  862. mptcp_data_lock((struct sock *)msk);
  863. __mptcp_subflow_fully_established(msk, subflow, mp_opt);
  864. mptcp_data_unlock((struct sock *)msk);
  865. check_notify:
  866. /* if the subflow is not already linked into the conn_list, we can't
  867. * notify the PM: this subflow is still on the listener queue
  868. * and the PM possibly acquiring the subflow lock could race with
  869. * the listener close
  870. */
  871. if (likely(subflow->pm_notified) || list_empty(&subflow->node))
  872. return true;
  873. subflow->pm_notified = 1;
  874. if (subflow->mp_join) {
  875. clear_3rdack_retransmission(ssk);
  876. mptcp_pm_subflow_established(msk);
  877. } else {
  878. mptcp_pm_fully_established(msk, ssk);
  879. }
  880. return true;
  881. reset:
  882. mptcp_subflow_reset(ssk);
  883. return false;
  884. }
  885. u64 __mptcp_expand_seq(u64 old_seq, u64 cur_seq)
  886. {
  887. u32 old_seq32, cur_seq32;
  888. old_seq32 = (u32)old_seq;
  889. cur_seq32 = (u32)cur_seq;
  890. cur_seq = (old_seq & GENMASK_ULL(63, 32)) + cur_seq32;
  891. if (unlikely(cur_seq32 < old_seq32 && before(old_seq32, cur_seq32)))
  892. return cur_seq + (1LL << 32);
  893. /* reverse wrap could happen, too */
  894. if (unlikely(cur_seq32 > old_seq32 && after(old_seq32, cur_seq32)))
  895. return cur_seq - (1LL << 32);
  896. return cur_seq;
  897. }
  898. static void __mptcp_snd_una_update(struct mptcp_sock *msk, u64 new_snd_una)
  899. {
  900. msk->bytes_acked += new_snd_una - msk->snd_una;
  901. WRITE_ONCE(msk->snd_una, new_snd_una);
  902. }
  903. static void ack_update_msk(struct mptcp_sock *msk,
  904. struct sock *ssk,
  905. struct mptcp_options_received *mp_opt)
  906. {
  907. u64 new_wnd_end, new_snd_una, snd_nxt = READ_ONCE(msk->snd_nxt);
  908. struct sock *sk = (struct sock *)msk;
  909. u64 old_snd_una;
  910. mptcp_data_lock(sk);
  911. /* avoid ack expansion on update conflict, to reduce the risk of
  912. * wrongly expanding to a future ack sequence number, which is way
  913. * more dangerous than missing an ack
  914. */
  915. old_snd_una = msk->snd_una;
  916. new_snd_una = mptcp_expand_seq(old_snd_una, mp_opt->data_ack, mp_opt->ack64);
  917. /* ACK for data not even sent yet? Ignore.*/
  918. if (unlikely(after64(new_snd_una, snd_nxt)))
  919. new_snd_una = old_snd_una;
  920. new_wnd_end = new_snd_una + tcp_sk(ssk)->snd_wnd;
  921. if (after64(new_wnd_end, msk->wnd_end))
  922. WRITE_ONCE(msk->wnd_end, new_wnd_end);
  923. /* this assumes mptcp_incoming_options() is invoked after tcp_ack() */
  924. if (after64(msk->wnd_end, snd_nxt))
  925. __mptcp_check_push(sk, ssk);
  926. if (after64(new_snd_una, old_snd_una)) {
  927. __mptcp_snd_una_update(msk, new_snd_una);
  928. __mptcp_data_acked(sk);
  929. }
  930. msk->last_ack_recv = tcp_jiffies32;
  931. mptcp_data_unlock(sk);
  932. trace_ack_update_msk(mp_opt->data_ack,
  933. old_snd_una, new_snd_una,
  934. new_wnd_end, READ_ONCE(msk->wnd_end));
  935. }
  936. bool mptcp_update_rcv_data_fin(struct mptcp_sock *msk, u64 data_fin_seq, bool use_64bit)
  937. {
  938. /* Skip if DATA_FIN was already received.
  939. * If updating simultaneously with the recvmsg loop, values
  940. * should match. If they mismatch, the peer is misbehaving and
  941. * we will prefer the most recent information.
  942. */
  943. if (READ_ONCE(msk->rcv_data_fin))
  944. return false;
  945. WRITE_ONCE(msk->rcv_data_fin_seq,
  946. mptcp_expand_seq(READ_ONCE(msk->ack_seq), data_fin_seq, use_64bit));
  947. WRITE_ONCE(msk->rcv_data_fin, 1);
  948. return true;
  949. }
  950. static bool add_addr_hmac_valid(struct mptcp_sock *msk,
  951. struct mptcp_options_received *mp_opt)
  952. {
  953. u64 hmac = 0;
  954. if (mp_opt->echo)
  955. return true;
  956. hmac = add_addr_generate_hmac(READ_ONCE(msk->remote_key),
  957. READ_ONCE(msk->local_key),
  958. &mp_opt->addr);
  959. pr_debug("msk=%p, ahmac=%llu, mp_opt->ahmac=%llu\n",
  960. msk, hmac, mp_opt->ahmac);
  961. return hmac == mp_opt->ahmac;
  962. }
  963. /* Return false if a subflow has been reset, else return true */
  964. bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb)
  965. {
  966. struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
  967. struct mptcp_sock *msk = mptcp_sk(subflow->conn);
  968. struct mptcp_options_received mp_opt;
  969. struct mptcp_ext *mpext;
  970. if (__mptcp_check_fallback(msk)) {
  971. /* Keep it simple and unconditionally trigger send data cleanup and
  972. * pending queue spooling. We will need to acquire the data lock
  973. * for more accurate checks, and once the lock is acquired, such
  974. * helpers are cheap.
  975. */
  976. mptcp_data_lock(subflow->conn);
  977. if (sk_stream_memory_free(sk))
  978. __mptcp_check_push(subflow->conn, sk);
  979. /* on fallback we just need to ignore the msk-level snd_una, as
  980. * this is really plain TCP
  981. */
  982. __mptcp_snd_una_update(msk, READ_ONCE(msk->snd_nxt));
  983. __mptcp_data_acked(subflow->conn);
  984. mptcp_data_unlock(subflow->conn);
  985. return true;
  986. }
  987. mptcp_get_options(skb, &mp_opt);
  988. /* The subflow can be in close state only if check_fully_established()
  989. * just sent a reset. If so, tell the caller to ignore the current packet.
  990. */
  991. if (!check_fully_established(msk, sk, subflow, skb, &mp_opt))
  992. return sk->sk_state != TCP_CLOSE;
  993. if (unlikely(mp_opt.suboptions != OPTION_MPTCP_DSS)) {
  994. if ((mp_opt.suboptions & OPTION_MPTCP_FASTCLOSE) &&
  995. READ_ONCE(msk->local_key) == mp_opt.rcvr_key) {
  996. WRITE_ONCE(msk->rcv_fastclose, true);
  997. mptcp_schedule_work((struct sock *)msk);
  998. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFASTCLOSERX);
  999. }
  1000. if ((mp_opt.suboptions & OPTION_MPTCP_ADD_ADDR) &&
  1001. add_addr_hmac_valid(msk, &mp_opt)) {
  1002. if (!mp_opt.echo) {
  1003. mptcp_pm_add_addr_received(sk, &mp_opt.addr);
  1004. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ADDADDR);
  1005. } else {
  1006. mptcp_pm_add_addr_echoed(msk, &mp_opt.addr);
  1007. mptcp_pm_del_add_timer(msk, &mp_opt.addr, true);
  1008. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_ECHOADD);
  1009. }
  1010. if (mp_opt.addr.port)
  1011. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_PORTADD);
  1012. }
  1013. if (mp_opt.suboptions & OPTION_MPTCP_RM_ADDR)
  1014. mptcp_pm_rm_addr_received(msk, &mp_opt.rm_list);
  1015. if (mp_opt.suboptions & OPTION_MPTCP_PRIO) {
  1016. mptcp_pm_mp_prio_received(sk, mp_opt.backup);
  1017. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPPRIORX);
  1018. }
  1019. if (mp_opt.suboptions & OPTION_MPTCP_FAIL) {
  1020. mptcp_pm_mp_fail_received(sk, mp_opt.fail_seq);
  1021. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPFAILRX);
  1022. }
  1023. if (mp_opt.suboptions & OPTION_MPTCP_RST) {
  1024. subflow->reset_seen = 1;
  1025. subflow->reset_reason = mp_opt.reset_reason;
  1026. subflow->reset_transient = mp_opt.reset_transient;
  1027. MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_MPRSTRX);
  1028. }
  1029. if (!(mp_opt.suboptions & OPTION_MPTCP_DSS))
  1030. return true;
  1031. }
  1032. /* we can't wait for recvmsg() to update the ack_seq, otherwise
  1033. * monodirectional flows will stuck
  1034. */
  1035. if (mp_opt.use_ack)
  1036. ack_update_msk(msk, sk, &mp_opt);
  1037. /* Zero-data-length packets are dropped by the caller and not
  1038. * propagated to the MPTCP layer, so the skb extension does not
  1039. * need to be allocated or populated. DATA_FIN information, if
  1040. * present, needs to be updated here before the skb is freed.
  1041. */
  1042. if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
  1043. if (mp_opt.data_fin && mp_opt.data_len == 1 &&
  1044. mptcp_update_rcv_data_fin(msk, mp_opt.data_seq, mp_opt.dsn64))
  1045. mptcp_schedule_work((struct sock *)msk);
  1046. return true;
  1047. }
  1048. mpext = skb_ext_add(skb, SKB_EXT_MPTCP);
  1049. if (!mpext)
  1050. return true;
  1051. memset(mpext, 0, sizeof(*mpext));
  1052. if (likely(mp_opt.use_map)) {
  1053. if (mp_opt.mpc_map) {
  1054. /* this is an MP_CAPABLE carrying MPTCP data
  1055. * we know this map the first chunk of data
  1056. */
  1057. mptcp_crypto_key_sha(subflow->remote_key, NULL,
  1058. &mpext->data_seq);
  1059. mpext->data_seq++;
  1060. mpext->subflow_seq = 1;
  1061. mpext->dsn64 = 1;
  1062. mpext->mpc_map = 1;
  1063. mpext->data_fin = 0;
  1064. } else {
  1065. mpext->data_seq = mp_opt.data_seq;
  1066. mpext->subflow_seq = mp_opt.subflow_seq;
  1067. mpext->dsn64 = mp_opt.dsn64;
  1068. mpext->data_fin = mp_opt.data_fin;
  1069. }
  1070. mpext->data_len = mp_opt.data_len;
  1071. mpext->use_map = 1;
  1072. mpext->csum_reqd = !!(mp_opt.suboptions & OPTION_MPTCP_CSUMREQD);
  1073. if (mpext->csum_reqd)
  1074. mpext->csum = mp_opt.csum;
  1075. }
  1076. return true;
  1077. }
  1078. static void mptcp_set_rwin(struct tcp_sock *tp, struct tcphdr *th)
  1079. {
  1080. const struct sock *ssk = (const struct sock *)tp;
  1081. struct mptcp_subflow_context *subflow;
  1082. u64 ack_seq, rcv_wnd_old, rcv_wnd_new;
  1083. struct mptcp_sock *msk;
  1084. u32 new_win;
  1085. u64 win;
  1086. subflow = mptcp_subflow_ctx(ssk);
  1087. msk = mptcp_sk(subflow->conn);
  1088. ack_seq = READ_ONCE(msk->ack_seq);
  1089. rcv_wnd_new = ack_seq + tp->rcv_wnd;
  1090. rcv_wnd_old = atomic64_read(&msk->rcv_wnd_sent);
  1091. if (after64(rcv_wnd_new, rcv_wnd_old)) {
  1092. u64 rcv_wnd;
  1093. for (;;) {
  1094. rcv_wnd = atomic64_cmpxchg(&msk->rcv_wnd_sent, rcv_wnd_old, rcv_wnd_new);
  1095. if (rcv_wnd == rcv_wnd_old)
  1096. break;
  1097. rcv_wnd_old = rcv_wnd;
  1098. if (before64(rcv_wnd_new, rcv_wnd_old)) {
  1099. MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICTUPDATE);
  1100. goto raise_win;
  1101. }
  1102. MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDCONFLICT);
  1103. }
  1104. goto update_wspace;
  1105. }
  1106. if (rcv_wnd_new != rcv_wnd_old) {
  1107. raise_win:
  1108. win = rcv_wnd_old - ack_seq;
  1109. tp->rcv_wnd = min_t(u64, win, U32_MAX);
  1110. new_win = tp->rcv_wnd;
  1111. /* Make sure we do not exceed the maximum possible
  1112. * scaled window.
  1113. */
  1114. if (unlikely(th->syn))
  1115. new_win = min(new_win, 65535U) << tp->rx_opt.rcv_wscale;
  1116. if (!tp->rx_opt.rcv_wscale &&
  1117. READ_ONCE(sock_net(ssk)->ipv4.sysctl_tcp_workaround_signed_windows))
  1118. new_win = min(new_win, MAX_TCP_WINDOW);
  1119. else
  1120. new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
  1121. /* RFC1323 scaling applied */
  1122. new_win >>= tp->rx_opt.rcv_wscale;
  1123. th->window = htons(new_win);
  1124. MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_RCVWNDSHARED);
  1125. }
  1126. update_wspace:
  1127. WRITE_ONCE(msk->old_wspace, tp->rcv_wnd);
  1128. }
  1129. __sum16 __mptcp_make_csum(u64 data_seq, u32 subflow_seq, u16 data_len, __wsum sum)
  1130. {
  1131. struct csum_pseudo_header header;
  1132. __wsum csum;
  1133. /* cfr RFC 8684 3.3.1.:
  1134. * the data sequence number used in the pseudo-header is
  1135. * always the 64-bit value, irrespective of what length is used in the
  1136. * DSS option itself.
  1137. */
  1138. header.data_seq = cpu_to_be64(data_seq);
  1139. header.subflow_seq = htonl(subflow_seq);
  1140. header.data_len = htons(data_len);
  1141. header.csum = 0;
  1142. csum = csum_partial(&header, sizeof(header), sum);
  1143. return csum_fold(csum);
  1144. }
  1145. static __sum16 mptcp_make_csum(const struct mptcp_ext *mpext)
  1146. {
  1147. return __mptcp_make_csum(mpext->data_seq, mpext->subflow_seq, mpext->data_len,
  1148. ~csum_unfold(mpext->csum));
  1149. }
  1150. static void put_len_csum(u16 len, __sum16 csum, void *data)
  1151. {
  1152. __sum16 *sumptr = data + 2;
  1153. __be16 *ptr = data;
  1154. put_unaligned_be16(len, ptr);
  1155. put_unaligned(csum, sumptr);
  1156. }
  1157. void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
  1158. struct mptcp_out_options *opts)
  1159. {
  1160. const struct sock *ssk = (const struct sock *)tp;
  1161. struct mptcp_subflow_context *subflow;
  1162. /* Which options can be used together?
  1163. *
  1164. * X: mutually exclusive
  1165. * O: often used together
  1166. * C: can be used together in some cases
  1167. * P: could be used together but we prefer not to (optimisations)
  1168. *
  1169. * Opt: | MPC | MPJ | DSS | ADD | RM | PRIO | FAIL | FC |
  1170. * ------|------|------|------|------|------|------|------|------|
  1171. * MPC |------|------|------|------|------|------|------|------|
  1172. * MPJ | X |------|------|------|------|------|------|------|
  1173. * DSS | X | X |------|------|------|------|------|------|
  1174. * ADD | X | X | P |------|------|------|------|------|
  1175. * RM | C | C | C | P |------|------|------|------|
  1176. * PRIO | X | C | C | C | C |------|------|------|
  1177. * FAIL | X | X | C | X | X | X |------|------|
  1178. * FC | X | X | X | X | X | X | X |------|
  1179. * RST | X | X | X | X | X | X | O | O |
  1180. * ------|------|------|------|------|------|------|------|------|
  1181. *
  1182. * The same applies in mptcp_established_options() function.
  1183. */
  1184. if (likely(OPTION_MPTCP_DSS & opts->suboptions)) {
  1185. struct mptcp_ext *mpext = &opts->ext_copy;
  1186. u8 len = TCPOLEN_MPTCP_DSS_BASE;
  1187. u8 flags = 0;
  1188. if (mpext->use_ack) {
  1189. flags = MPTCP_DSS_HAS_ACK;
  1190. if (mpext->ack64) {
  1191. len += TCPOLEN_MPTCP_DSS_ACK64;
  1192. flags |= MPTCP_DSS_ACK64;
  1193. } else {
  1194. len += TCPOLEN_MPTCP_DSS_ACK32;
  1195. }
  1196. }
  1197. if (mpext->use_map) {
  1198. len += TCPOLEN_MPTCP_DSS_MAP64;
  1199. /* Use only 64-bit mapping flags for now, add
  1200. * support for optional 32-bit mappings later.
  1201. */
  1202. flags |= MPTCP_DSS_HAS_MAP | MPTCP_DSS_DSN64;
  1203. if (mpext->data_fin)
  1204. flags |= MPTCP_DSS_DATA_FIN;
  1205. if (opts->csum_reqd)
  1206. len += TCPOLEN_MPTCP_DSS_CHECKSUM;
  1207. }
  1208. *ptr++ = mptcp_option(MPTCPOPT_DSS, len, 0, flags);
  1209. if (mpext->use_ack) {
  1210. if (mpext->ack64) {
  1211. put_unaligned_be64(mpext->data_ack, ptr);
  1212. ptr += 2;
  1213. } else {
  1214. put_unaligned_be32(mpext->data_ack32, ptr);
  1215. ptr += 1;
  1216. }
  1217. }
  1218. if (mpext->use_map) {
  1219. put_unaligned_be64(mpext->data_seq, ptr);
  1220. ptr += 2;
  1221. put_unaligned_be32(mpext->subflow_seq, ptr);
  1222. ptr += 1;
  1223. if (opts->csum_reqd) {
  1224. /* data_len == 0 is reserved for the infinite mapping,
  1225. * the checksum will also be set to 0.
  1226. */
  1227. put_len_csum(mpext->data_len,
  1228. (mpext->data_len ? mptcp_make_csum(mpext) : 0),
  1229. ptr);
  1230. } else {
  1231. put_unaligned_be32(mpext->data_len << 16 |
  1232. TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
  1233. }
  1234. ptr += 1;
  1235. }
  1236. /* We might need to add MP_FAIL options in rare cases */
  1237. if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions))
  1238. goto mp_fail;
  1239. } else if (OPTIONS_MPTCP_MPC & opts->suboptions) {
  1240. u8 len, flag = MPTCP_CAP_HMAC_SHA256;
  1241. if (OPTION_MPTCP_MPC_SYN & opts->suboptions) {
  1242. len = TCPOLEN_MPTCP_MPC_SYN;
  1243. } else if (OPTION_MPTCP_MPC_SYNACK & opts->suboptions) {
  1244. len = TCPOLEN_MPTCP_MPC_SYNACK;
  1245. } else if (opts->data_len) {
  1246. len = TCPOLEN_MPTCP_MPC_ACK_DATA;
  1247. if (opts->csum_reqd)
  1248. len += TCPOLEN_MPTCP_DSS_CHECKSUM;
  1249. } else {
  1250. len = TCPOLEN_MPTCP_MPC_ACK;
  1251. }
  1252. if (opts->csum_reqd)
  1253. flag |= MPTCP_CAP_CHECKSUM_REQD;
  1254. if (!opts->allow_join_id0)
  1255. flag |= MPTCP_CAP_DENY_JOIN_ID0;
  1256. *ptr++ = mptcp_option(MPTCPOPT_MP_CAPABLE, len,
  1257. MPTCP_SUPPORTED_VERSION,
  1258. flag);
  1259. if (!((OPTION_MPTCP_MPC_SYNACK | OPTION_MPTCP_MPC_ACK) &
  1260. opts->suboptions))
  1261. goto mp_capable_done;
  1262. put_unaligned_be64(opts->sndr_key, ptr);
  1263. ptr += 2;
  1264. if (!((OPTION_MPTCP_MPC_ACK) & opts->suboptions))
  1265. goto mp_capable_done;
  1266. put_unaligned_be64(opts->rcvr_key, ptr);
  1267. ptr += 2;
  1268. if (!opts->data_len)
  1269. goto mp_capable_done;
  1270. if (opts->csum_reqd) {
  1271. put_len_csum(opts->data_len,
  1272. __mptcp_make_csum(opts->data_seq,
  1273. opts->subflow_seq,
  1274. opts->data_len,
  1275. ~csum_unfold(opts->csum)),
  1276. ptr);
  1277. } else {
  1278. put_unaligned_be32(opts->data_len << 16 |
  1279. TCPOPT_NOP << 8 | TCPOPT_NOP, ptr);
  1280. }
  1281. ptr += 1;
  1282. /* MPC is additionally mutually exclusive with MP_PRIO */
  1283. goto mp_capable_done;
  1284. } else if (OPTIONS_MPTCP_MPJ & opts->suboptions) {
  1285. if (OPTION_MPTCP_MPJ_SYN & opts->suboptions) {
  1286. *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
  1287. TCPOLEN_MPTCP_MPJ_SYN,
  1288. opts->backup, opts->join_id);
  1289. put_unaligned_be32(opts->token, ptr);
  1290. ptr += 1;
  1291. put_unaligned_be32(opts->nonce, ptr);
  1292. ptr += 1;
  1293. } else if (OPTION_MPTCP_MPJ_SYNACK & opts->suboptions) {
  1294. *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
  1295. TCPOLEN_MPTCP_MPJ_SYNACK,
  1296. opts->backup, opts->join_id);
  1297. put_unaligned_be64(opts->thmac, ptr);
  1298. ptr += 2;
  1299. put_unaligned_be32(opts->nonce, ptr);
  1300. ptr += 1;
  1301. } else {
  1302. *ptr++ = mptcp_option(MPTCPOPT_MP_JOIN,
  1303. TCPOLEN_MPTCP_MPJ_ACK, 0, 0);
  1304. memcpy(ptr, opts->hmac, MPTCPOPT_HMAC_LEN);
  1305. ptr += 5;
  1306. }
  1307. } else if (OPTION_MPTCP_ADD_ADDR & opts->suboptions) {
  1308. u8 len = TCPOLEN_MPTCP_ADD_ADDR_BASE;
  1309. u8 echo = MPTCP_ADDR_ECHO;
  1310. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  1311. if (opts->addr.family == AF_INET6)
  1312. len = TCPOLEN_MPTCP_ADD_ADDR6_BASE;
  1313. #endif
  1314. if (opts->addr.port)
  1315. len += TCPOLEN_MPTCP_PORT_LEN;
  1316. if (opts->ahmac) {
  1317. len += sizeof(opts->ahmac);
  1318. echo = 0;
  1319. }
  1320. *ptr++ = mptcp_option(MPTCPOPT_ADD_ADDR,
  1321. len, echo, opts->addr.id);
  1322. if (opts->addr.family == AF_INET) {
  1323. memcpy((u8 *)ptr, (u8 *)&opts->addr.addr.s_addr, 4);
  1324. ptr += 1;
  1325. }
  1326. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  1327. else if (opts->addr.family == AF_INET6) {
  1328. memcpy((u8 *)ptr, opts->addr.addr6.s6_addr, 16);
  1329. ptr += 4;
  1330. }
  1331. #endif
  1332. if (!opts->addr.port) {
  1333. if (opts->ahmac) {
  1334. put_unaligned_be64(opts->ahmac, ptr);
  1335. ptr += 2;
  1336. }
  1337. } else {
  1338. u16 port = ntohs(opts->addr.port);
  1339. if (opts->ahmac) {
  1340. u8 *bptr = (u8 *)ptr;
  1341. put_unaligned_be16(port, bptr);
  1342. bptr += 2;
  1343. put_unaligned_be64(opts->ahmac, bptr);
  1344. bptr += 8;
  1345. put_unaligned_be16(TCPOPT_NOP << 8 |
  1346. TCPOPT_NOP, bptr);
  1347. ptr += 3;
  1348. } else {
  1349. put_unaligned_be32(port << 16 |
  1350. TCPOPT_NOP << 8 |
  1351. TCPOPT_NOP, ptr);
  1352. ptr += 1;
  1353. }
  1354. }
  1355. } else if (unlikely(OPTION_MPTCP_FASTCLOSE & opts->suboptions)) {
  1356. /* FASTCLOSE is mutually exclusive with others except RST */
  1357. *ptr++ = mptcp_option(MPTCPOPT_MP_FASTCLOSE,
  1358. TCPOLEN_MPTCP_FASTCLOSE,
  1359. 0, 0);
  1360. put_unaligned_be64(opts->rcvr_key, ptr);
  1361. ptr += 2;
  1362. if (OPTION_MPTCP_RST & opts->suboptions)
  1363. goto mp_rst;
  1364. return;
  1365. } else if (unlikely(OPTION_MPTCP_FAIL & opts->suboptions)) {
  1366. mp_fail:
  1367. /* MP_FAIL is mutually exclusive with others except RST */
  1368. subflow = mptcp_subflow_ctx(ssk);
  1369. subflow->send_mp_fail = 0;
  1370. *ptr++ = mptcp_option(MPTCPOPT_MP_FAIL,
  1371. TCPOLEN_MPTCP_FAIL,
  1372. 0, 0);
  1373. put_unaligned_be64(opts->fail_seq, ptr);
  1374. ptr += 2;
  1375. if (OPTION_MPTCP_RST & opts->suboptions)
  1376. goto mp_rst;
  1377. return;
  1378. } else if (unlikely(OPTION_MPTCP_RST & opts->suboptions)) {
  1379. mp_rst:
  1380. *ptr++ = mptcp_option(MPTCPOPT_RST,
  1381. TCPOLEN_MPTCP_RST,
  1382. opts->reset_transient,
  1383. opts->reset_reason);
  1384. return;
  1385. }
  1386. if (OPTION_MPTCP_PRIO & opts->suboptions) {
  1387. subflow = mptcp_subflow_ctx(ssk);
  1388. subflow->send_mp_prio = 0;
  1389. *ptr++ = mptcp_option(MPTCPOPT_MP_PRIO,
  1390. TCPOLEN_MPTCP_PRIO,
  1391. opts->backup, TCPOPT_NOP);
  1392. MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
  1393. }
  1394. mp_capable_done:
  1395. if (OPTION_MPTCP_RM_ADDR & opts->suboptions) {
  1396. u8 i = 1;
  1397. *ptr++ = mptcp_option(MPTCPOPT_RM_ADDR,
  1398. TCPOLEN_MPTCP_RM_ADDR_BASE + opts->rm_list.nr,
  1399. 0, opts->rm_list.ids[0]);
  1400. while (i < opts->rm_list.nr) {
  1401. u8 id1, id2, id3, id4;
  1402. id1 = opts->rm_list.ids[i];
  1403. id2 = i + 1 < opts->rm_list.nr ? opts->rm_list.ids[i + 1] : TCPOPT_NOP;
  1404. id3 = i + 2 < opts->rm_list.nr ? opts->rm_list.ids[i + 2] : TCPOPT_NOP;
  1405. id4 = i + 3 < opts->rm_list.nr ? opts->rm_list.ids[i + 3] : TCPOPT_NOP;
  1406. put_unaligned_be32(id1 << 24 | id2 << 16 | id3 << 8 | id4, ptr);
  1407. ptr += 1;
  1408. i += 4;
  1409. }
  1410. }
  1411. if (tp)
  1412. mptcp_set_rwin(tp, th);
  1413. }
  1414. __be32 mptcp_get_reset_option(const struct sk_buff *skb)
  1415. {
  1416. const struct mptcp_ext *ext = mptcp_get_ext(skb);
  1417. u8 flags, reason;
  1418. if (ext) {
  1419. flags = ext->reset_transient;
  1420. reason = ext->reset_reason;
  1421. return mptcp_option(MPTCPOPT_RST, TCPOLEN_MPTCP_RST,
  1422. flags, reason);
  1423. }
  1424. return htonl(0u);
  1425. }
  1426. EXPORT_SYMBOL_GPL(mptcp_get_reset_option);