feat.c 47 KB

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