feat.c 47 KB

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