/net/dccp/feat.c

http://github.com/mirrors/linux · C · 1564 lines · 980 code · 174 blank · 410 comment · 303 complexity · d60f3826eccf183a2418b9b4205559bf MD5 · raw file

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