feat.c 41 KB

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