feat.c 41 KB

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