feat.c 41 KB

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