feat.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/dccp/feat.c
  4. *
  5. * Feature negotiation for the DCCP protocol (RFC 4340, section 6)
  6. *
  7. * Copyright (c) 2008 Gerrit Renker <gerrit@erg.abdn.ac.uk>
  8. * Rewrote from scratch, some bits from earlier code by
  9. * Copyright (c) 2005 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
  10. *
  11. * ASSUMPTIONS
  12. * -----------
  13. * o Feature negotiation is coordinated with connection setup (as in TCP), wild
  14. * changes of parameters of an established connection are not supported.
  15. * o Changing non-negotiable (NN) values is supported in state OPEN/PARTOPEN.
  16. * o All currently known SP features have 1-byte quantities. If in the future
  17. * extensions of RFCs 4340..42 define features with item lengths larger than
  18. * one byte, a feature-specific extension of the code will be required.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include "ccid.h"
  23. #include "feat.h"
  24. /* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */
  25. unsigned long sysctl_dccp_sequence_window __read_mostly = 100;
  26. int sysctl_dccp_rx_ccid __read_mostly = 2,
  27. sysctl_dccp_tx_ccid __read_mostly = 2;
  28. /*
  29. * Feature activation handlers.
  30. *
  31. * These all use an u64 argument, to provide enough room for NN/SP features. At
  32. * this stage the negotiated values have been checked to be within their range.
  33. */
  34. static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
  35. {
  36. struct dccp_sock *dp = dccp_sk(sk);
  37. struct ccid *new_ccid = ccid_new(ccid, sk, rx);
  38. if (new_ccid == NULL)
  39. return -ENOMEM;
  40. if (rx) {
  41. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  42. dp->dccps_hc_rx_ccid = new_ccid;
  43. } else {
  44. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  45. dp->dccps_hc_tx_ccid = new_ccid;
  46. }
  47. return 0;
  48. }
  49. static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
  50. {
  51. struct dccp_sock *dp = dccp_sk(sk);
  52. if (rx) {
  53. dp->dccps_r_seq_win = seq_win;
  54. /* propagate changes to update SWL/SWH */
  55. dccp_update_gsr(sk, dp->dccps_gsr);
  56. } else {
  57. dp->dccps_l_seq_win = seq_win;
  58. /* propagate changes to update AWL */
  59. dccp_update_gss(sk, dp->dccps_gss);
  60. }
  61. return 0;
  62. }
  63. static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
  64. {
  65. if (rx)
  66. dccp_sk(sk)->dccps_r_ack_ratio = ratio;
  67. else
  68. dccp_sk(sk)->dccps_l_ack_ratio = ratio;
  69. return 0;
  70. }
  71. static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
  72. {
  73. struct dccp_sock *dp = dccp_sk(sk);
  74. if (rx) {
  75. if (enable && dp->dccps_hc_rx_ackvec == NULL) {
  76. dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
  77. if (dp->dccps_hc_rx_ackvec == NULL)
  78. return -ENOMEM;
  79. } else if (!enable) {
  80. dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
  81. dp->dccps_hc_rx_ackvec = NULL;
  82. }
  83. }
  84. return 0;
  85. }
  86. static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
  87. {
  88. if (!rx)
  89. dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
  90. return 0;
  91. }
  92. /*
  93. * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
  94. * `rx' holds when the sending peer informs about his partial coverage via a
  95. * ChangeR() option. In the other case, we are the sender and the receiver
  96. * announces its coverage via ChangeL() options. The policy here is to honour
  97. * such communication by enabling the corresponding partial coverage - but only
  98. * if it has not been set manually before; the warning here means that all
  99. * packets will be dropped.
  100. */
  101. static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
  102. {
  103. struct dccp_sock *dp = dccp_sk(sk);
  104. if (rx)
  105. dp->dccps_pcrlen = cscov;
  106. else {
  107. if (dp->dccps_pcslen == 0)
  108. dp->dccps_pcslen = cscov;
  109. else if (cscov > dp->dccps_pcslen)
  110. DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
  111. dp->dccps_pcslen, (u8)cscov);
  112. }
  113. return 0;
  114. }
  115. static const struct {
  116. u8 feat_num; /* DCCPF_xxx */
  117. enum dccp_feat_type rxtx; /* RX or TX */
  118. enum dccp_feat_type reconciliation; /* SP or NN */
  119. u8 default_value; /* as in 6.4 */
  120. int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
  121. /*
  122. * Lookup table for location and type of features (from RFC 4340/4342)
  123. * +--------------------------+----+-----+----+----+---------+-----------+
  124. * | Feature | Location | Reconc. | Initial | Section |
  125. * | | RX | TX | SP | NN | Value | Reference |
  126. * +--------------------------+----+-----+----+----+---------+-----------+
  127. * | DCCPF_CCID | | X | X | | 2 | 10 |
  128. * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
  129. * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
  130. * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
  131. * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
  132. * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
  133. * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
  134. * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
  135. * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
  136. * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
  137. * +--------------------------+----+-----+----+----+---------+-----------+
  138. */
  139. } dccp_feat_table[] = {
  140. { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
  141. { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
  142. { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
  143. { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
  144. { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
  145. { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
  146. { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
  147. { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
  148. { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
  149. { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
  150. };
  151. #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
  152. /**
  153. * dccp_feat_index - Hash function to map feature number into array position
  154. * @feat_num: feature to hash, one of %dccp_feature_numbers
  155. *
  156. * Returns consecutive array index or -1 if the feature is not understood.
  157. */
  158. static int dccp_feat_index(u8 feat_num)
  159. {
  160. /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
  161. if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
  162. return feat_num - 1;
  163. /*
  164. * Other features: add cases for new feature types here after adding
  165. * them to the above table.
  166. */
  167. switch (feat_num) {
  168. case DCCPF_SEND_LEV_RATE:
  169. return DCCP_FEAT_SUPPORTED_MAX - 1;
  170. }
  171. return -1;
  172. }
  173. static u8 dccp_feat_type(u8 feat_num)
  174. {
  175. int idx = dccp_feat_index(feat_num);
  176. if (idx < 0)
  177. return FEAT_UNKNOWN;
  178. return dccp_feat_table[idx].reconciliation;
  179. }
  180. static int dccp_feat_default_value(u8 feat_num)
  181. {
  182. int idx = dccp_feat_index(feat_num);
  183. /*
  184. * There are no default values for unknown features, so encountering a
  185. * negative index here indicates a serious problem somewhere else.
  186. */
  187. DCCP_BUG_ON(idx < 0);
  188. return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
  189. }
  190. /*
  191. * Debugging and verbose-printing section
  192. */
  193. static const char *dccp_feat_fname(const u8 feat)
  194. {
  195. static const char *const feature_names[] = {
  196. [DCCPF_RESERVED] = "Reserved",
  197. [DCCPF_CCID] = "CCID",
  198. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  199. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  200. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  201. [DCCPF_ACK_RATIO] = "Ack Ratio",
  202. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  203. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  204. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  205. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  206. };
  207. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  208. return feature_names[DCCPF_RESERVED];
  209. if (feat == DCCPF_SEND_LEV_RATE)
  210. return "Send Loss Event Rate";
  211. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  212. return "CCID-specific";
  213. return feature_names[feat];
  214. }
  215. static const char *const dccp_feat_sname[] = {
  216. "DEFAULT", "INITIALISING", "CHANGING", "UNSTABLE", "STABLE",
  217. };
  218. #ifdef CONFIG_IP_DCCP_DEBUG
  219. static const char *dccp_feat_oname(const u8 opt)
  220. {
  221. switch (opt) {
  222. case DCCPO_CHANGE_L: return "Change_L";
  223. case DCCPO_CONFIRM_L: return "Confirm_L";
  224. case DCCPO_CHANGE_R: return "Change_R";
  225. case DCCPO_CONFIRM_R: return "Confirm_R";
  226. }
  227. return NULL;
  228. }
  229. static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
  230. {
  231. u8 i, type = dccp_feat_type(feat_num);
  232. if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL))
  233. dccp_pr_debug_cat("(NULL)");
  234. else if (type == FEAT_SP)
  235. for (i = 0; i < val->sp.len; i++)
  236. dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]);
  237. else if (type == FEAT_NN)
  238. dccp_pr_debug_cat("%llu", (unsigned long long)val->nn);
  239. else
  240. dccp_pr_debug_cat("unknown type %u", type);
  241. }
  242. static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
  243. {
  244. u8 type = dccp_feat_type(feat_num);
  245. dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
  246. if (type == FEAT_NN)
  247. fval.nn = dccp_decode_value_var(list, len);
  248. dccp_feat_printval(feat_num, &fval);
  249. }
  250. static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
  251. {
  252. dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote",
  253. dccp_feat_fname(entry->feat_num));
  254. dccp_feat_printval(entry->feat_num, &entry->val);
  255. dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
  256. entry->needs_confirm ? "(Confirm pending)" : "");
  257. }
  258. #define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \
  259. dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\
  260. dccp_feat_printvals(feat, val, len); \
  261. dccp_pr_debug_cat(") %s\n", mandatory ? "!" : ""); } while (0)
  262. #define dccp_feat_print_fnlist(fn_list) { \
  263. const struct dccp_feat_entry *___entry; \
  264. \
  265. dccp_pr_debug("List Dump:\n"); \
  266. list_for_each_entry(___entry, fn_list, node) \
  267. dccp_feat_print_entry(___entry); \
  268. }
  269. #else /* ! CONFIG_IP_DCCP_DEBUG */
  270. #define dccp_feat_print_opt(opt, feat, val, len, mandatory)
  271. #define dccp_feat_print_fnlist(fn_list)
  272. #endif
  273. static int __dccp_feat_activate(struct sock *sk, const int idx,
  274. const bool is_local, dccp_feat_val const *fval)
  275. {
  276. bool rx;
  277. u64 val;
  278. if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
  279. return -1;
  280. if (dccp_feat_table[idx].activation_hdlr == NULL)
  281. return 0;
  282. if (fval == NULL) {
  283. val = dccp_feat_table[idx].default_value;
  284. } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
  285. if (fval->sp.vec == NULL) {
  286. /*
  287. * This can happen when an empty Confirm is sent
  288. * for an SP (i.e. known) feature. In this case
  289. * we would be using the default anyway.
  290. */
  291. DCCP_CRIT("Feature #%d undefined: using default", idx);
  292. val = dccp_feat_table[idx].default_value;
  293. } else {
  294. val = fval->sp.vec[0];
  295. }
  296. } else {
  297. val = fval->nn;
  298. }
  299. /* Location is RX if this is a local-RX or remote-TX feature */
  300. rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
  301. dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
  302. dccp_feat_fname(dccp_feat_table[idx].feat_num),
  303. fval ? "" : "default ", (unsigned long long)val);
  304. return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
  305. }
  306. /**
  307. * dccp_feat_activate - Activate feature value on socket
  308. * @sk: fully connected DCCP socket (after handshake is complete)
  309. * @feat_num: feature to activate, one of %dccp_feature_numbers
  310. * @local: whether local (1) or remote (0) @feat_num is meant
  311. * @fval: the value (SP or NN) to activate, or NULL to use the default value
  312. *
  313. * For general use this function is preferable over __dccp_feat_activate().
  314. */
  315. static int dccp_feat_activate(struct sock *sk, u8 feat_num, bool local,
  316. dccp_feat_val const *fval)
  317. {
  318. return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval);
  319. }
  320. /* Test for "Req'd" feature (RFC 4340, 6.4) */
  321. static inline int dccp_feat_must_be_understood(u8 feat_num)
  322. {
  323. return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
  324. feat_num == DCCPF_SEQUENCE_WINDOW;
  325. }
  326. /* copy constructor, fval must not already contain allocated memory */
  327. static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
  328. {
  329. fval->sp.len = len;
  330. if (fval->sp.len > 0) {
  331. fval->sp.vec = kmemdup(val, len, gfp_any());
  332. if (fval->sp.vec == NULL) {
  333. fval->sp.len = 0;
  334. return -ENOMEM;
  335. }
  336. }
  337. return 0;
  338. }
  339. static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
  340. {
  341. if (unlikely(val == NULL))
  342. return;
  343. if (dccp_feat_type(feat_num) == FEAT_SP)
  344. kfree(val->sp.vec);
  345. memset(val, 0, sizeof(*val));
  346. }
  347. static struct dccp_feat_entry *
  348. dccp_feat_clone_entry(struct dccp_feat_entry const *original)
  349. {
  350. struct dccp_feat_entry *new;
  351. u8 type = dccp_feat_type(original->feat_num);
  352. if (type == FEAT_UNKNOWN)
  353. return NULL;
  354. new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
  355. if (new == NULL)
  356. return NULL;
  357. if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
  358. original->val.sp.vec,
  359. original->val.sp.len)) {
  360. kfree(new);
  361. return NULL;
  362. }
  363. return new;
  364. }
  365. static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
  366. {
  367. if (entry != NULL) {
  368. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  369. kfree(entry);
  370. }
  371. }
  372. /*
  373. * List management functions
  374. *
  375. * Feature negotiation lists rely on and maintain the following invariants:
  376. * - each feat_num in the list is known, i.e. we know its type and default value
  377. * - each feat_num/is_local combination is unique (old entries are overwritten)
  378. * - SP values are always freshly allocated
  379. * - list is sorted in increasing order of feature number (faster lookup)
  380. */
  381. static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
  382. u8 feat_num, bool is_local)
  383. {
  384. struct dccp_feat_entry *entry;
  385. list_for_each_entry(entry, fn_list, node) {
  386. if (entry->feat_num == feat_num && entry->is_local == is_local)
  387. return entry;
  388. else if (entry->feat_num > feat_num)
  389. break;
  390. }
  391. return NULL;
  392. }
  393. /**
  394. * dccp_feat_entry_new - Central list update routine (called by all others)
  395. * @head: list to add to
  396. * @feat: feature number
  397. * @local: whether the local (1) or remote feature with number @feat is meant
  398. *
  399. * This is the only constructor and serves to ensure the above invariants.
  400. */
  401. static struct dccp_feat_entry *
  402. dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
  403. {
  404. struct dccp_feat_entry *entry;
  405. list_for_each_entry(entry, head, node)
  406. if (entry->feat_num == feat && entry->is_local == local) {
  407. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  408. return entry;
  409. } else if (entry->feat_num > feat) {
  410. head = &entry->node;
  411. break;
  412. }
  413. entry = kmalloc(sizeof(*entry), gfp_any());
  414. if (entry != NULL) {
  415. entry->feat_num = feat;
  416. entry->is_local = local;
  417. list_add_tail(&entry->node, head);
  418. }
  419. return entry;
  420. }
  421. /**
  422. * dccp_feat_push_change - Add/overwrite a Change option in the list
  423. * @fn_list: feature-negotiation list to update
  424. * @feat: one of %dccp_feature_numbers
  425. * @local: whether local (1) or remote (0) @feat_num is meant
  426. * @mandatory: whether to use Mandatory feature negotiation options
  427. * @fval: pointer to NN/SP value to be inserted (will be copied)
  428. */
  429. static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
  430. u8 mandatory, dccp_feat_val *fval)
  431. {
  432. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  433. if (new == NULL)
  434. return -ENOMEM;
  435. new->feat_num = feat;
  436. new->is_local = local;
  437. new->state = FEAT_INITIALISING;
  438. new->needs_confirm = false;
  439. new->empty_confirm = false;
  440. new->val = *fval;
  441. new->needs_mandatory = mandatory;
  442. return 0;
  443. }
  444. /**
  445. * dccp_feat_push_confirm - Add a Confirm entry to the FN list
  446. * @fn_list: feature-negotiation list to add to
  447. * @feat: one of %dccp_feature_numbers
  448. * @local: whether local (1) or remote (0) @feat_num is being confirmed
  449. * @fval: pointer to NN/SP value to be inserted or NULL
  450. *
  451. * Returns 0 on success, a Reset code for further processing otherwise.
  452. */
  453. static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
  454. dccp_feat_val *fval)
  455. {
  456. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  457. if (new == NULL)
  458. return DCCP_RESET_CODE_TOO_BUSY;
  459. new->feat_num = feat;
  460. new->is_local = local;
  461. new->state = FEAT_STABLE; /* transition in 6.6.2 */
  462. new->needs_confirm = true;
  463. new->empty_confirm = (fval == NULL);
  464. new->val.nn = 0; /* zeroes the whole structure */
  465. if (!new->empty_confirm)
  466. new->val = *fval;
  467. new->needs_mandatory = false;
  468. return 0;
  469. }
  470. static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
  471. {
  472. return dccp_feat_push_confirm(fn_list, feat, local, NULL);
  473. }
  474. static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
  475. {
  476. list_del(&entry->node);
  477. dccp_feat_entry_destructor(entry);
  478. }
  479. void dccp_feat_list_purge(struct list_head *fn_list)
  480. {
  481. struct dccp_feat_entry *entry, *next;
  482. list_for_each_entry_safe(entry, next, fn_list, node)
  483. dccp_feat_entry_destructor(entry);
  484. INIT_LIST_HEAD(fn_list);
  485. }
  486. EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
  487. /* generate @to as full clone of @from - @to must not contain any nodes */
  488. int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
  489. {
  490. struct dccp_feat_entry *entry, *new;
  491. INIT_LIST_HEAD(to);
  492. list_for_each_entry(entry, from, node) {
  493. new = dccp_feat_clone_entry(entry);
  494. if (new == NULL)
  495. goto cloning_failed;
  496. list_add_tail(&new->node, to);
  497. }
  498. return 0;
  499. cloning_failed:
  500. dccp_feat_list_purge(to);
  501. return -ENOMEM;
  502. }
  503. /**
  504. * dccp_feat_valid_nn_length - Enforce length constraints on NN options
  505. * @feat_num: feature to return length of, one of %dccp_feature_numbers
  506. *
  507. * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
  508. * incoming options are accepted as long as their values are valid.
  509. */
  510. static u8 dccp_feat_valid_nn_length(u8 feat_num)
  511. {
  512. if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
  513. return 2;
  514. if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
  515. return 6;
  516. return 0;
  517. }
  518. static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
  519. {
  520. switch (feat_num) {
  521. case DCCPF_ACK_RATIO:
  522. return val <= DCCPF_ACK_RATIO_MAX;
  523. case DCCPF_SEQUENCE_WINDOW:
  524. return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
  525. }
  526. return 0; /* feature unknown - so we can't tell */
  527. }
  528. /* check that SP values are within the ranges defined in RFC 4340 */
  529. static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
  530. {
  531. switch (feat_num) {
  532. case DCCPF_CCID:
  533. return val == DCCPC_CCID2 || val == DCCPC_CCID3;
  534. /* Type-check Boolean feature values: */
  535. case DCCPF_SHORT_SEQNOS:
  536. case DCCPF_ECN_INCAPABLE:
  537. case DCCPF_SEND_ACK_VECTOR:
  538. case DCCPF_SEND_NDP_COUNT:
  539. case DCCPF_DATA_CHECKSUM:
  540. case DCCPF_SEND_LEV_RATE:
  541. return val < 2;
  542. case DCCPF_MIN_CSUM_COVER:
  543. return val < 16;
  544. }
  545. return 0; /* feature unknown */
  546. }
  547. static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
  548. {
  549. if (sp_list == NULL || sp_len < 1)
  550. return 0;
  551. while (sp_len--)
  552. if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
  553. return 0;
  554. return 1;
  555. }
  556. /**
  557. * dccp_feat_insert_opts - Generate FN options from current list state
  558. * @skb: next sk_buff to be sent to the peer
  559. * @dp: for client during handshake and general negotiation
  560. * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
  561. */
  562. int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
  563. struct sk_buff *skb)
  564. {
  565. struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
  566. struct dccp_feat_entry *pos, *next;
  567. u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
  568. bool rpt;
  569. /* put entries into @skb in the order they appear in the list */
  570. list_for_each_entry_safe_reverse(pos, next, fn, node) {
  571. opt = dccp_feat_genopt(pos);
  572. type = dccp_feat_type(pos->feat_num);
  573. rpt = false;
  574. if (pos->empty_confirm) {
  575. len = 0;
  576. ptr = NULL;
  577. } else {
  578. if (type == FEAT_SP) {
  579. len = pos->val.sp.len;
  580. ptr = pos->val.sp.vec;
  581. rpt = pos->needs_confirm;
  582. } else if (type == FEAT_NN) {
  583. len = dccp_feat_valid_nn_length(pos->feat_num);
  584. ptr = nn_in_nbo;
  585. dccp_encode_value_var(pos->val.nn, ptr, len);
  586. } else {
  587. DCCP_BUG("unknown feature %u", pos->feat_num);
  588. return -1;
  589. }
  590. }
  591. dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
  592. if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
  593. return -1;
  594. if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
  595. return -1;
  596. if (skb->sk->sk_state == DCCP_OPEN &&
  597. (opt == DCCPO_CONFIRM_R || opt == DCCPO_CONFIRM_L)) {
  598. /*
  599. * Confirms don't get retransmitted (6.6.3) once the
  600. * connection is in state OPEN
  601. */
  602. dccp_feat_list_pop(pos);
  603. } else {
  604. /*
  605. * Enter CHANGING after transmitting the Change
  606. * option (6.6.2).
  607. */
  608. if (pos->state == FEAT_INITIALISING)
  609. pos->state = FEAT_CHANGING;
  610. }
  611. }
  612. return 0;
  613. }
  614. /**
  615. * __feat_register_nn - Register new NN value on socket
  616. * @fn: feature-negotiation list to register with
  617. * @feat: an NN feature from %dccp_feature_numbers
  618. * @mandatory: use Mandatory option if 1
  619. * @nn_val: value to register (restricted to 4 bytes)
  620. *
  621. * Note that NN features are local by definition (RFC 4340, 6.3.2).
  622. */
  623. static int __feat_register_nn(struct list_head *fn, u8 feat,
  624. u8 mandatory, u64 nn_val)
  625. {
  626. dccp_feat_val fval = { .nn = nn_val };
  627. if (dccp_feat_type(feat) != FEAT_NN ||
  628. !dccp_feat_is_valid_nn_val(feat, nn_val))
  629. return -EINVAL;
  630. /* Don't bother with default values, they will be activated anyway. */
  631. if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
  632. return 0;
  633. return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
  634. }
  635. /**
  636. * __feat_register_sp - Register new SP value/list on socket
  637. * @fn: feature-negotiation list to register with
  638. * @feat: an SP feature from %dccp_feature_numbers
  639. * @is_local: whether the local (1) or the remote (0) @feat is meant
  640. * @mandatory: use Mandatory option if 1
  641. * @sp_val: SP value followed by optional preference list
  642. * @sp_len: length of @sp_val in bytes
  643. */
  644. static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
  645. u8 mandatory, u8 const *sp_val, u8 sp_len)
  646. {
  647. dccp_feat_val fval;
  648. if (dccp_feat_type(feat) != FEAT_SP ||
  649. !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
  650. return -EINVAL;
  651. /* Avoid negotiating alien CCIDs by only advertising supported ones */
  652. if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
  653. return -EOPNOTSUPP;
  654. if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
  655. return -ENOMEM;
  656. if (dccp_feat_push_change(fn, feat, is_local, mandatory, &fval)) {
  657. kfree(fval.sp.vec);
  658. return -ENOMEM;
  659. }
  660. return 0;
  661. }
  662. /**
  663. * dccp_feat_register_sp - Register requests to change SP feature values
  664. * @sk: client or listening socket
  665. * @feat: one of %dccp_feature_numbers
  666. * @is_local: whether the local (1) or remote (0) @feat is meant
  667. * @list: array of preferred values, in descending order of preference
  668. * @len: length of @list in bytes
  669. */
  670. int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
  671. u8 const *list, u8 len)
  672. { /* any changes must be registered before establishing the connection */
  673. if (sk->sk_state != DCCP_CLOSED)
  674. return -EISCONN;
  675. if (dccp_feat_type(feat) != FEAT_SP)
  676. return -EINVAL;
  677. return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
  678. 0, list, len);
  679. }
  680. /**
  681. * dccp_feat_nn_get - Query current/pending value of NN feature
  682. * @sk: DCCP socket of an established connection
  683. * @feat: NN feature number from %dccp_feature_numbers
  684. *
  685. * For a known NN feature, returns value currently being negotiated, or
  686. * current (confirmed) value if no negotiation is going on.
  687. */
  688. u64 dccp_feat_nn_get(struct sock *sk, u8 feat)
  689. {
  690. if (dccp_feat_type(feat) == FEAT_NN) {
  691. struct dccp_sock *dp = dccp_sk(sk);
  692. struct dccp_feat_entry *entry;
  693. entry = dccp_feat_list_lookup(&dp->dccps_featneg, feat, 1);
  694. if (entry != NULL)
  695. return entry->val.nn;
  696. switch (feat) {
  697. case DCCPF_ACK_RATIO:
  698. return dp->dccps_l_ack_ratio;
  699. case DCCPF_SEQUENCE_WINDOW:
  700. return dp->dccps_l_seq_win;
  701. }
  702. }
  703. DCCP_BUG("attempt to look up unsupported feature %u", feat);
  704. return 0;
  705. }
  706. EXPORT_SYMBOL_GPL(dccp_feat_nn_get);
  707. /**
  708. * dccp_feat_signal_nn_change - Update NN values for an established connection
  709. * @sk: DCCP socket of an established connection
  710. * @feat: NN feature number from %dccp_feature_numbers
  711. * @nn_val: the new value to use
  712. *
  713. * This function is used to communicate NN updates out-of-band.
  714. */
  715. int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
  716. {
  717. struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
  718. dccp_feat_val fval = { .nn = nn_val };
  719. struct dccp_feat_entry *entry;
  720. if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
  721. return 0;
  722. if (dccp_feat_type(feat) != FEAT_NN ||
  723. !dccp_feat_is_valid_nn_val(feat, nn_val))
  724. return -EINVAL;
  725. if (nn_val == dccp_feat_nn_get(sk, feat))
  726. return 0; /* already set or negotiation under way */
  727. entry = dccp_feat_list_lookup(fn, feat, 1);
  728. if (entry != NULL) {
  729. dccp_pr_debug("Clobbering existing NN entry %llu -> %llu\n",
  730. (unsigned long long)entry->val.nn,
  731. (unsigned long long)nn_val);
  732. dccp_feat_list_pop(entry);
  733. }
  734. inet_csk_schedule_ack(sk);
  735. return dccp_feat_push_change(fn, feat, 1, 0, &fval);
  736. }
  737. EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
  738. /*
  739. * Tracking features whose value depend on the choice of CCID
  740. *
  741. * This is designed with an extension in mind so that a list walk could be done
  742. * before activating any features. However, the existing framework was found to
  743. * work satisfactorily up until now, the automatic verification is left open.
  744. * When adding new CCIDs, add a corresponding dependency table here.
  745. */
  746. static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
  747. {
  748. static const struct ccid_dependency ccid2_dependencies[2][2] = {
  749. /*
  750. * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
  751. * feature and Send Ack Vector is an RX feature, `is_local'
  752. * needs to be reversed.
  753. */
  754. { /* Dependencies of the receiver-side (remote) CCID2 */
  755. {
  756. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  757. .is_local = true,
  758. .is_mandatory = true,
  759. .val = 1
  760. },
  761. { 0, 0, 0, 0 }
  762. },
  763. { /* Dependencies of the sender-side (local) CCID2 */
  764. {
  765. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  766. .is_local = false,
  767. .is_mandatory = true,
  768. .val = 1
  769. },
  770. { 0, 0, 0, 0 }
  771. }
  772. };
  773. static const struct ccid_dependency ccid3_dependencies[2][5] = {
  774. { /*
  775. * Dependencies of the receiver-side CCID3
  776. */
  777. { /* locally disable Ack Vectors */
  778. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  779. .is_local = true,
  780. .is_mandatory = false,
  781. .val = 0
  782. },
  783. { /* see below why Send Loss Event Rate is on */
  784. .dependent_feat = DCCPF_SEND_LEV_RATE,
  785. .is_local = true,
  786. .is_mandatory = true,
  787. .val = 1
  788. },
  789. { /* NDP Count is needed as per RFC 4342, 6.1.1 */
  790. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  791. .is_local = false,
  792. .is_mandatory = true,
  793. .val = 1
  794. },
  795. { 0, 0, 0, 0 },
  796. },
  797. { /*
  798. * CCID3 at the TX side: we request that the HC-receiver
  799. * will not send Ack Vectors (they will be ignored, so
  800. * Mandatory is not set); we enable Send Loss Event Rate
  801. * (Mandatory since the implementation does not support
  802. * the Loss Intervals option of RFC 4342, 8.6).
  803. * The last two options are for peer's information only.
  804. */
  805. {
  806. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  807. .is_local = false,
  808. .is_mandatory = false,
  809. .val = 0
  810. },
  811. {
  812. .dependent_feat = DCCPF_SEND_LEV_RATE,
  813. .is_local = false,
  814. .is_mandatory = true,
  815. .val = 1
  816. },
  817. { /* this CCID does not support Ack Ratio */
  818. .dependent_feat = DCCPF_ACK_RATIO,
  819. .is_local = true,
  820. .is_mandatory = false,
  821. .val = 0
  822. },
  823. { /* tell receiver we are sending NDP counts */
  824. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  825. .is_local = true,
  826. .is_mandatory = false,
  827. .val = 1
  828. },
  829. { 0, 0, 0, 0 }
  830. }
  831. };
  832. switch (ccid) {
  833. case DCCPC_CCID2:
  834. return ccid2_dependencies[is_local];
  835. case DCCPC_CCID3:
  836. return ccid3_dependencies[is_local];
  837. default:
  838. return NULL;
  839. }
  840. }
  841. /**
  842. * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
  843. * @fn: feature-negotiation list to update
  844. * @id: CCID number to track
  845. * @is_local: whether TX CCID (1) or RX CCID (0) is meant
  846. *
  847. * This function needs to be called after registering all other features.
  848. */
  849. static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
  850. {
  851. const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
  852. int i, rc = (table == NULL);
  853. for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
  854. if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
  855. rc = __feat_register_sp(fn, table[i].dependent_feat,
  856. table[i].is_local,
  857. table[i].is_mandatory,
  858. &table[i].val, 1);
  859. else
  860. rc = __feat_register_nn(fn, table[i].dependent_feat,
  861. table[i].is_mandatory,
  862. table[i].val);
  863. return rc;
  864. }
  865. /**
  866. * dccp_feat_finalise_settings - Finalise settings before starting negotiation
  867. * @dp: client or listening socket (settings will be inherited)
  868. *
  869. * This is called after all registrations (socket initialisation, sysctls, and
  870. * sockopt calls), and before sending the first packet containing Change options
  871. * (ie. client-Request or server-Response), to ensure internal consistency.
  872. */
  873. int dccp_feat_finalise_settings(struct dccp_sock *dp)
  874. {
  875. struct list_head *fn = &dp->dccps_featneg;
  876. struct dccp_feat_entry *entry;
  877. int i = 2, ccids[2] = { -1, -1 };
  878. /*
  879. * Propagating CCIDs:
  880. * 1) not useful to propagate CCID settings if this host advertises more
  881. * than one CCID: the choice of CCID may still change - if this is
  882. * the client, or if this is the server and the client sends
  883. * singleton CCID values.
  884. * 2) since is that propagate_ccid changes the list, we defer changing
  885. * the sorted list until after the traversal.
  886. */
  887. list_for_each_entry(entry, fn, node)
  888. if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
  889. ccids[entry->is_local] = entry->val.sp.vec[0];
  890. while (i--)
  891. if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
  892. return -1;
  893. dccp_feat_print_fnlist(fn);
  894. return 0;
  895. }
  896. /**
  897. * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
  898. * @dreq: server socket to resolve
  899. *
  900. * It is the server which resolves the dependencies once the CCID has been
  901. * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
  902. */
  903. int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
  904. {
  905. struct list_head *fn = &dreq->dreq_featneg;
  906. struct dccp_feat_entry *entry;
  907. u8 is_local, ccid;
  908. for (is_local = 0; is_local <= 1; is_local++) {
  909. entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
  910. if (entry != NULL && !entry->empty_confirm)
  911. ccid = entry->val.sp.vec[0];
  912. else
  913. ccid = dccp_feat_default_value(DCCPF_CCID);
  914. if (dccp_feat_propagate_ccid(fn, ccid, is_local))
  915. return -1;
  916. }
  917. return 0;
  918. }
  919. /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
  920. static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
  921. {
  922. u8 c, s;
  923. for (s = 0; s < slen; s++)
  924. for (c = 0; c < clen; c++)
  925. if (servlist[s] == clilist[c])
  926. return servlist[s];
  927. return -1;
  928. }
  929. /**
  930. * dccp_feat_prefer - Move preferred entry to the start of array
  931. * @preferred_value: entry to move to start of array
  932. * @array: array of preferred entries
  933. * @array_len: size of the array
  934. *
  935. * Reorder the @array_len elements in @array so that @preferred_value comes
  936. * first. Returns >0 to indicate that @preferred_value does occur in @array.
  937. */
  938. static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
  939. {
  940. u8 i, does_occur = 0;
  941. if (array != NULL) {
  942. for (i = 0; i < array_len; i++)
  943. if (array[i] == preferred_value) {
  944. array[i] = array[0];
  945. does_occur++;
  946. }
  947. if (does_occur)
  948. array[0] = preferred_value;
  949. }
  950. return does_occur;
  951. }
  952. /**
  953. * dccp_feat_reconcile - Reconcile SP preference lists
  954. * @fv: SP list to reconcile into
  955. * @arr: received SP preference list
  956. * @len: length of @arr in bytes
  957. * @is_server: whether this side is the server (and @fv is the server's list)
  958. * @reorder: whether to reorder the list in @fv after reconciling with @arr
  959. * When successful, > 0 is returned and the reconciled list is in @fval.
  960. * A value of 0 means that negotiation failed (no shared entry).
  961. */
  962. static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
  963. bool is_server, bool reorder)
  964. {
  965. int rc;
  966. if (!fv->sp.vec || !arr) {
  967. DCCP_CRIT("NULL feature value or array");
  968. return 0;
  969. }
  970. if (is_server)
  971. rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
  972. else
  973. rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
  974. if (!reorder)
  975. return rc;
  976. if (rc < 0)
  977. return 0;
  978. /*
  979. * Reorder list: used for activating features and in dccp_insert_fn_opt.
  980. */
  981. return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
  982. }
  983. /**
  984. * dccp_feat_change_recv - Process incoming ChangeL/R options
  985. * @fn: feature-negotiation list to update
  986. * @is_mandatory: whether the Change was preceded by a Mandatory option
  987. * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
  988. * @feat: one of %dccp_feature_numbers
  989. * @val: NN value or SP value/preference list
  990. * @len: length of @val in bytes
  991. * @server: whether this node is the server (1) or the client (0)
  992. */
  993. static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
  994. u8 feat, u8 *val, u8 len, const bool server)
  995. {
  996. u8 defval, type = dccp_feat_type(feat);
  997. const bool local = (opt == DCCPO_CHANGE_R);
  998. struct dccp_feat_entry *entry;
  999. dccp_feat_val fval;
  1000. if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
  1001. goto unknown_feature_or_value;
  1002. dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
  1003. /*
  1004. * Negotiation of NN features: Change R is invalid, so there is no
  1005. * simultaneous negotiation; hence we do not look up in the list.
  1006. */
  1007. if (type == FEAT_NN) {
  1008. if (local || len > sizeof(fval.nn))
  1009. goto unknown_feature_or_value;
  1010. /* 6.3.2: "The feature remote MUST accept any valid value..." */
  1011. fval.nn = dccp_decode_value_var(val, len);
  1012. if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
  1013. goto unknown_feature_or_value;
  1014. return dccp_feat_push_confirm(fn, feat, local, &fval);
  1015. }
  1016. /*
  1017. * Unidirectional/simultaneous negotiation of SP features (6.3.1)
  1018. */
  1019. entry = dccp_feat_list_lookup(fn, feat, local);
  1020. if (entry == NULL) {
  1021. /*
  1022. * No particular preferences have been registered. We deal with
  1023. * this situation by assuming that all valid values are equally
  1024. * acceptable, and apply the following checks:
  1025. * - if the peer's list is a singleton, we accept a valid value;
  1026. * - if we are the server, we first try to see if the peer (the
  1027. * client) advertises the default value. If yes, we use it,
  1028. * otherwise we accept the preferred value;
  1029. * - else if we are the client, we use the first list element.
  1030. */
  1031. if (dccp_feat_clone_sp_val(&fval, val, 1))
  1032. return DCCP_RESET_CODE_TOO_BUSY;
  1033. if (len > 1 && server) {
  1034. defval = dccp_feat_default_value(feat);
  1035. if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
  1036. fval.sp.vec[0] = defval;
  1037. } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
  1038. kfree(fval.sp.vec);
  1039. goto unknown_feature_or_value;
  1040. }
  1041. /* Treat unsupported CCIDs like invalid values */
  1042. if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
  1043. kfree(fval.sp.vec);
  1044. goto not_valid_or_not_known;
  1045. }
  1046. if (dccp_feat_push_confirm(fn, feat, local, &fval)) {
  1047. kfree(fval.sp.vec);
  1048. return DCCP_RESET_CODE_TOO_BUSY;
  1049. }
  1050. return 0;
  1051. } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
  1052. return 0;
  1053. }
  1054. if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
  1055. entry->empty_confirm = false;
  1056. } else if (is_mandatory) {
  1057. return DCCP_RESET_CODE_MANDATORY_ERROR;
  1058. } else if (entry->state == FEAT_INITIALISING) {
  1059. /*
  1060. * Failed simultaneous negotiation (server only): try to `save'
  1061. * the connection by checking whether entry contains the default
  1062. * value for @feat. If yes, send an empty Confirm to signal that
  1063. * the received Change was not understood - which implies using
  1064. * the default value.
  1065. * If this also fails, we use Reset as the last resort.
  1066. */
  1067. WARN_ON(!server);
  1068. defval = dccp_feat_default_value(feat);
  1069. if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
  1070. return DCCP_RESET_CODE_OPTION_ERROR;
  1071. entry->empty_confirm = true;
  1072. }
  1073. entry->needs_confirm = true;
  1074. entry->needs_mandatory = false;
  1075. entry->state = FEAT_STABLE;
  1076. return 0;
  1077. unknown_feature_or_value:
  1078. if (!is_mandatory)
  1079. return dccp_push_empty_confirm(fn, feat, local);
  1080. not_valid_or_not_known:
  1081. return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
  1082. : DCCP_RESET_CODE_OPTION_ERROR;
  1083. }
  1084. /**
  1085. * dccp_feat_confirm_recv - Process received Confirm options
  1086. * @fn: feature-negotiation list to update
  1087. * @is_mandatory: whether @opt was preceded by a Mandatory option
  1088. * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
  1089. * @feat: one of %dccp_feature_numbers
  1090. * @val: NN value or SP value/preference list
  1091. * @len: length of @val in bytes
  1092. * @server: whether this node is server (1) or client (0)
  1093. */
  1094. static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
  1095. u8 feat, u8 *val, u8 len, const bool server)
  1096. {
  1097. u8 *plist, plen, type = dccp_feat_type(feat);
  1098. const bool local = (opt == DCCPO_CONFIRM_R);
  1099. struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
  1100. dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
  1101. if (entry == NULL) { /* nothing queued: ignore or handle error */
  1102. if (is_mandatory && type == FEAT_UNKNOWN)
  1103. return DCCP_RESET_CODE_MANDATORY_ERROR;
  1104. if (!local && type == FEAT_NN) /* 6.3.2 */
  1105. goto confirmation_failed;
  1106. return 0;
  1107. }
  1108. if (entry->state != FEAT_CHANGING) /* 6.6.2 */
  1109. return 0;
  1110. if (len == 0) {
  1111. if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
  1112. goto confirmation_failed;
  1113. /*
  1114. * Empty Confirm during connection setup: this means reverting
  1115. * to the `old' value, which in this case is the default. Since
  1116. * we handle default values automatically when no other values
  1117. * have been set, we revert to the old value by removing this
  1118. * entry from the list.
  1119. */
  1120. dccp_feat_list_pop(entry);
  1121. return 0;
  1122. }
  1123. if (type == FEAT_NN) {
  1124. if (len > sizeof(entry->val.nn))
  1125. goto confirmation_failed;
  1126. if (entry->val.nn == dccp_decode_value_var(val, len))
  1127. goto confirmation_succeeded;
  1128. DCCP_WARN("Bogus Confirm for non-existing value\n");
  1129. goto confirmation_failed;
  1130. }
  1131. /*
  1132. * Parsing SP Confirms: the first element of @val is the preferred
  1133. * SP value which the peer confirms, the remainder depends on @len.
  1134. * Note that only the confirmed value need to be a valid SP value.
  1135. */
  1136. if (!dccp_feat_is_valid_sp_val(feat, *val))
  1137. goto confirmation_failed;
  1138. if (len == 1) { /* peer didn't supply a preference list */
  1139. plist = val;
  1140. plen = len;
  1141. } else { /* preferred value + preference list */
  1142. plist = val + 1;
  1143. plen = len - 1;
  1144. }
  1145. /* Check whether the peer got the reconciliation right (6.6.8) */
  1146. if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
  1147. DCCP_WARN("Confirm selected the wrong value %u\n", *val);
  1148. return DCCP_RESET_CODE_OPTION_ERROR;
  1149. }
  1150. entry->val.sp.vec[0] = *val;
  1151. confirmation_succeeded:
  1152. entry->state = FEAT_STABLE;
  1153. return 0;
  1154. confirmation_failed:
  1155. DCCP_WARN("Confirmation failed\n");
  1156. return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
  1157. : DCCP_RESET_CODE_OPTION_ERROR;
  1158. }
  1159. /**
  1160. * dccp_feat_handle_nn_established - Fast-path reception of NN options
  1161. * @sk: socket of an established DCCP connection
  1162. * @mandatory: whether @opt was preceded by a Mandatory option
  1163. * @opt: %DCCPO_CHANGE_L | %DCCPO_CONFIRM_R (NN only)
  1164. * @feat: NN number, one of %dccp_feature_numbers
  1165. * @val: NN value
  1166. * @len: length of @val in bytes
  1167. *
  1168. * This function combines the functionality of change_recv/confirm_recv, with
  1169. * the following differences (reset codes are the same):
  1170. * - cleanup after receiving the Confirm;
  1171. * - values are directly activated after successful parsing;
  1172. * - deliberately restricted to NN features.
  1173. * The restriction to NN features is essential since SP features can have non-
  1174. * predictable outcomes (depending on the remote configuration), and are inter-
  1175. * dependent (CCIDs for instance cause further dependencies).
  1176. */
  1177. static u8 dccp_feat_handle_nn_established(struct sock *sk, u8 mandatory, u8 opt,
  1178. u8 feat, u8 *val, u8 len)
  1179. {
  1180. struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
  1181. const bool local = (opt == DCCPO_CONFIRM_R);
  1182. struct dccp_feat_entry *entry;
  1183. u8 type = dccp_feat_type(feat);
  1184. dccp_feat_val fval;
  1185. dccp_feat_print_opt(opt, feat, val, len, mandatory);
  1186. /* Ignore non-mandatory unknown and non-NN features */
  1187. if (type == FEAT_UNKNOWN) {
  1188. if (local && !mandatory)
  1189. return 0;
  1190. goto fast_path_unknown;
  1191. } else if (type != FEAT_NN) {
  1192. return 0;
  1193. }
  1194. /*
  1195. * We don't accept empty Confirms, since in fast-path feature
  1196. * negotiation the values are enabled immediately after sending
  1197. * the Change option.
  1198. * Empty Changes on the other hand are invalid (RFC 4340, 6.1).
  1199. */
  1200. if (len == 0 || len > sizeof(fval.nn))
  1201. goto fast_path_unknown;
  1202. if (opt == DCCPO_CHANGE_L) {
  1203. fval.nn = dccp_decode_value_var(val, len);
  1204. if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
  1205. goto fast_path_unknown;
  1206. if (dccp_feat_push_confirm(fn, feat, local, &fval) ||
  1207. dccp_feat_activate(sk, feat, local, &fval))
  1208. return DCCP_RESET_CODE_TOO_BUSY;
  1209. /* set the `Ack Pending' flag to piggyback a Confirm */
  1210. inet_csk_schedule_ack(sk);
  1211. } else if (opt == DCCPO_CONFIRM_R) {
  1212. entry = dccp_feat_list_lookup(fn, feat, local);
  1213. if (entry == NULL || entry->state != FEAT_CHANGING)
  1214. return 0;
  1215. fval.nn = dccp_decode_value_var(val, len);
  1216. /*
  1217. * Just ignore a value that doesn't match our current value.
  1218. * If the option changes twice within two RTTs, then at least
  1219. * one CONFIRM will be received for the old value after a
  1220. * new CHANGE was sent.
  1221. */
  1222. if (fval.nn != entry->val.nn)
  1223. return 0;
  1224. /* Only activate after receiving the Confirm option (6.6.1). */
  1225. dccp_feat_activate(sk, feat, local, &fval);
  1226. /* It has been confirmed - so remove the entry */
  1227. dccp_feat_list_pop(entry);
  1228. } else {
  1229. DCCP_WARN("Received illegal option %u\n", opt);
  1230. goto fast_path_failed;
  1231. }
  1232. return 0;
  1233. fast_path_unknown:
  1234. if (!mandatory)
  1235. return dccp_push_empty_confirm(fn, feat, local);
  1236. fast_path_failed:
  1237. return mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
  1238. : DCCP_RESET_CODE_OPTION_ERROR;
  1239. }
  1240. /**
  1241. * dccp_feat_parse_options - Process Feature-Negotiation Options
  1242. * @sk: for general use and used by the client during connection setup
  1243. * @dreq: used by the server during connection setup
  1244. * @mandatory: whether @opt was preceded by a Mandatory option
  1245. * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
  1246. * @feat: one of %dccp_feature_numbers
  1247. * @val: value contents of @opt
  1248. * @len: length of @val in bytes
  1249. *
  1250. * Returns 0 on success, a Reset code for ending the connection otherwise.
  1251. */
  1252. int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
  1253. u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
  1254. {
  1255. struct dccp_sock *dp = dccp_sk(sk);
  1256. struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
  1257. bool server = false;
  1258. switch (sk->sk_state) {
  1259. /*
  1260. * Negotiation during connection setup
  1261. */
  1262. case DCCP_LISTEN:
  1263. server = true;
  1264. fallthrough;
  1265. case DCCP_REQUESTING:
  1266. switch (opt) {
  1267. case DCCPO_CHANGE_L:
  1268. case DCCPO_CHANGE_R:
  1269. return dccp_feat_change_recv(fn, mandatory, opt, feat,
  1270. val, len, server);
  1271. case DCCPO_CONFIRM_R:
  1272. case DCCPO_CONFIRM_L:
  1273. return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
  1274. val, len, server);
  1275. }
  1276. break;
  1277. /*
  1278. * Support for exchanging NN options on an established connection.
  1279. */
  1280. case DCCP_OPEN:
  1281. case DCCP_PARTOPEN:
  1282. return dccp_feat_handle_nn_established(sk, mandatory, opt, feat,
  1283. val, len);
  1284. }
  1285. return 0; /* ignore FN options in all other states */
  1286. }
  1287. /**
  1288. * dccp_feat_init - Seed feature negotiation with host-specific defaults
  1289. * @sk: Socket to initialize.
  1290. *
  1291. * This initialises global defaults, depending on the value of the sysctls.
  1292. * These can later be overridden by registering changes via setsockopt calls.
  1293. * The last link in the chain is finalise_settings, to make sure that between
  1294. * here and the start of actual feature negotiation no inconsistencies enter.
  1295. *
  1296. * All features not appearing below use either defaults or are otherwise
  1297. * later adjusted through dccp_feat_finalise_settings().
  1298. */
  1299. int dccp_feat_init(struct sock *sk)
  1300. {
  1301. struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
  1302. u8 on = 1, off = 0;
  1303. int rc;
  1304. struct {
  1305. u8 *val;
  1306. u8 len;
  1307. } tx, rx;
  1308. /* Non-negotiable (NN) features */
  1309. rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
  1310. sysctl_dccp_sequence_window);
  1311. if (rc)
  1312. return rc;
  1313. /* Server-priority (SP) features */
  1314. /* Advertise that short seqnos are not supported (7.6.1) */
  1315. rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
  1316. if (rc)
  1317. return rc;
  1318. /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
  1319. rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
  1320. if (rc)
  1321. return rc;
  1322. /*
  1323. * We advertise the available list of CCIDs and reorder according to
  1324. * preferences, to avoid failure resulting from negotiating different
  1325. * singleton values (which always leads to failure).
  1326. * These settings can still (later) be overridden via sockopts.
  1327. */
  1328. if (ccid_get_builtin_ccids(&tx.val, &tx.len))
  1329. return -ENOBUFS;
  1330. if (ccid_get_builtin_ccids(&rx.val, &rx.len)) {
  1331. kfree(tx.val);
  1332. return -ENOBUFS;
  1333. }
  1334. if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
  1335. !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
  1336. goto free_ccid_lists;
  1337. rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
  1338. if (rc)
  1339. goto free_ccid_lists;
  1340. rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
  1341. free_ccid_lists:
  1342. kfree(tx.val);
  1343. kfree(rx.val);
  1344. return rc;
  1345. }
  1346. int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
  1347. {
  1348. struct dccp_sock *dp = dccp_sk(sk);
  1349. struct dccp_feat_entry *cur, *next;
  1350. int idx;
  1351. dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
  1352. [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
  1353. };
  1354. list_for_each_entry(cur, fn_list, node) {
  1355. /*
  1356. * An empty Confirm means that either an unknown feature type
  1357. * or an invalid value was present. In the first case there is
  1358. * nothing to activate, in the other the default value is used.
  1359. */
  1360. if (cur->empty_confirm)
  1361. continue;
  1362. idx = dccp_feat_index(cur->feat_num);
  1363. if (idx < 0) {
  1364. DCCP_BUG("Unknown feature %u", cur->feat_num);
  1365. goto activation_failed;
  1366. }
  1367. if (cur->state != FEAT_STABLE) {
  1368. DCCP_CRIT("Negotiation of %s %s failed in state %s",
  1369. cur->is_local ? "local" : "remote",
  1370. dccp_feat_fname(cur->feat_num),
  1371. dccp_feat_sname[cur->state]);
  1372. goto activation_failed;
  1373. }
  1374. fvals[idx][cur->is_local] = &cur->val;
  1375. }
  1376. /*
  1377. * Activate in decreasing order of index, so that the CCIDs are always
  1378. * activated as the last feature. This avoids the case where a CCID
  1379. * relies on the initialisation of one or more features that it depends
  1380. * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
  1381. */
  1382. for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
  1383. if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
  1384. __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
  1385. DCCP_CRIT("Could not activate %d", idx);
  1386. goto activation_failed;
  1387. }
  1388. /* Clean up Change options which have been confirmed already */
  1389. list_for_each_entry_safe(cur, next, fn_list, node)
  1390. if (!cur->needs_confirm)
  1391. dccp_feat_list_pop(cur);
  1392. dccp_pr_debug("Activation OK\n");
  1393. return 0;
  1394. activation_failed:
  1395. /*
  1396. * We clean up everything that may have been allocated, since
  1397. * it is difficult to track at which stage negotiation failed.
  1398. * This is ok, since all allocation functions below are robust
  1399. * against NULL arguments.
  1400. */
  1401. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  1402. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  1403. dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  1404. dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
  1405. dp->dccps_hc_rx_ackvec = NULL;
  1406. return -1;
  1407. }