feat.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. /* Test for "Req'd" feature (RFC 4340, 6.4) */
  310. static inline int dccp_feat_must_be_understood(u8 feat_num)
  311. {
  312. return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
  313. feat_num == DCCPF_SEQUENCE_WINDOW;
  314. }
  315. /* copy constructor, fval must not already contain allocated memory */
  316. static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
  317. {
  318. fval->sp.len = len;
  319. if (fval->sp.len > 0) {
  320. fval->sp.vec = kmemdup(val, len, gfp_any());
  321. if (fval->sp.vec == NULL) {
  322. fval->sp.len = 0;
  323. return -ENOBUFS;
  324. }
  325. }
  326. return 0;
  327. }
  328. static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
  329. {
  330. if (unlikely(val == NULL))
  331. return;
  332. if (dccp_feat_type(feat_num) == FEAT_SP)
  333. kfree(val->sp.vec);
  334. memset(val, 0, sizeof(*val));
  335. }
  336. static struct dccp_feat_entry *
  337. dccp_feat_clone_entry(struct dccp_feat_entry const *original)
  338. {
  339. struct dccp_feat_entry *new;
  340. u8 type = dccp_feat_type(original->feat_num);
  341. if (type == FEAT_UNKNOWN)
  342. return NULL;
  343. new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
  344. if (new == NULL)
  345. return NULL;
  346. if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
  347. original->val.sp.vec,
  348. original->val.sp.len)) {
  349. kfree(new);
  350. return NULL;
  351. }
  352. return new;
  353. }
  354. static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
  355. {
  356. if (entry != NULL) {
  357. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  358. kfree(entry);
  359. }
  360. }
  361. /*
  362. * List management functions
  363. *
  364. * Feature negotiation lists rely on and maintain the following invariants:
  365. * - each feat_num in the list is known, i.e. we know its type and default value
  366. * - each feat_num/is_local combination is unique (old entries are overwritten)
  367. * - SP values are always freshly allocated
  368. * - list is sorted in increasing order of feature number (faster lookup)
  369. */
  370. static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
  371. u8 feat_num, bool is_local)
  372. {
  373. struct dccp_feat_entry *entry;
  374. list_for_each_entry(entry, fn_list, node) {
  375. if (entry->feat_num == feat_num && entry->is_local == is_local)
  376. return entry;
  377. else if (entry->feat_num > feat_num)
  378. break;
  379. }
  380. return NULL;
  381. }
  382. /**
  383. * dccp_feat_entry_new - Central list update routine (called by all others)
  384. * @head: list to add to
  385. * @feat: feature number
  386. * @local: whether the local (1) or remote feature with number @feat is meant
  387. * This is the only constructor and serves to ensure the above invariants.
  388. */
  389. static struct dccp_feat_entry *
  390. dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
  391. {
  392. struct dccp_feat_entry *entry;
  393. list_for_each_entry(entry, head, node)
  394. if (entry->feat_num == feat && entry->is_local == local) {
  395. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  396. return entry;
  397. } else if (entry->feat_num > feat) {
  398. head = &entry->node;
  399. break;
  400. }
  401. entry = kmalloc(sizeof(*entry), gfp_any());
  402. if (entry != NULL) {
  403. entry->feat_num = feat;
  404. entry->is_local = local;
  405. list_add_tail(&entry->node, head);
  406. }
  407. return entry;
  408. }
  409. /**
  410. * dccp_feat_push_change - Add/overwrite a Change option in the list
  411. * @fn_list: feature-negotiation list to update
  412. * @feat: one of %dccp_feature_numbers
  413. * @local: whether local (1) or remote (0) @feat_num is meant
  414. * @needs_mandatory: whether to use Mandatory feature negotiation options
  415. * @fval: pointer to NN/SP value to be inserted (will be copied)
  416. */
  417. static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
  418. u8 mandatory, dccp_feat_val *fval)
  419. {
  420. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  421. if (new == NULL)
  422. return -ENOMEM;
  423. new->feat_num = feat;
  424. new->is_local = local;
  425. new->state = FEAT_INITIALISING;
  426. new->needs_confirm = 0;
  427. new->empty_confirm = 0;
  428. new->val = *fval;
  429. new->needs_mandatory = mandatory;
  430. return 0;
  431. }
  432. /**
  433. * dccp_feat_push_confirm - Add a Confirm entry to the FN list
  434. * @fn_list: feature-negotiation list to add to
  435. * @feat: one of %dccp_feature_numbers
  436. * @local: whether local (1) or remote (0) @feat_num is being confirmed
  437. * @fval: pointer to NN/SP value to be inserted or NULL
  438. * Returns 0 on success, a Reset code for further processing otherwise.
  439. */
  440. static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
  441. dccp_feat_val *fval)
  442. {
  443. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  444. if (new == NULL)
  445. return DCCP_RESET_CODE_TOO_BUSY;
  446. new->feat_num = feat;
  447. new->is_local = local;
  448. new->state = FEAT_STABLE; /* transition in 6.6.2 */
  449. new->needs_confirm = 1;
  450. new->empty_confirm = (fval == NULL);
  451. new->val.nn = 0; /* zeroes the whole structure */
  452. if (!new->empty_confirm)
  453. new->val = *fval;
  454. new->needs_mandatory = 0;
  455. return 0;
  456. }
  457. static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
  458. {
  459. return dccp_feat_push_confirm(fn_list, feat, local, NULL);
  460. }
  461. static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
  462. {
  463. list_del(&entry->node);
  464. dccp_feat_entry_destructor(entry);
  465. }
  466. void dccp_feat_list_purge(struct list_head *fn_list)
  467. {
  468. struct dccp_feat_entry *entry, *next;
  469. list_for_each_entry_safe(entry, next, fn_list, node)
  470. dccp_feat_entry_destructor(entry);
  471. INIT_LIST_HEAD(fn_list);
  472. }
  473. EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
  474. /* generate @to as full clone of @from - @to must not contain any nodes */
  475. int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
  476. {
  477. struct dccp_feat_entry *entry, *new;
  478. INIT_LIST_HEAD(to);
  479. list_for_each_entry(entry, from, node) {
  480. new = dccp_feat_clone_entry(entry);
  481. if (new == NULL)
  482. goto cloning_failed;
  483. list_add_tail(&new->node, to);
  484. }
  485. return 0;
  486. cloning_failed:
  487. dccp_feat_list_purge(to);
  488. return -ENOMEM;
  489. }
  490. /**
  491. * dccp_feat_valid_nn_length - Enforce length constraints on NN options
  492. * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
  493. * incoming options are accepted as long as their values are valid.
  494. */
  495. static u8 dccp_feat_valid_nn_length(u8 feat_num)
  496. {
  497. if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
  498. return 2;
  499. if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
  500. return 6;
  501. return 0;
  502. }
  503. static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
  504. {
  505. switch (feat_num) {
  506. case DCCPF_ACK_RATIO:
  507. return val <= DCCPF_ACK_RATIO_MAX;
  508. case DCCPF_SEQUENCE_WINDOW:
  509. return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
  510. }
  511. return 0; /* feature unknown - so we can't tell */
  512. }
  513. /* check that SP values are within the ranges defined in RFC 4340 */
  514. static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
  515. {
  516. switch (feat_num) {
  517. case DCCPF_CCID:
  518. return val == DCCPC_CCID2 || val == DCCPC_CCID3;
  519. /* Type-check Boolean feature values: */
  520. case DCCPF_SHORT_SEQNOS:
  521. case DCCPF_ECN_INCAPABLE:
  522. case DCCPF_SEND_ACK_VECTOR:
  523. case DCCPF_SEND_NDP_COUNT:
  524. case DCCPF_DATA_CHECKSUM:
  525. case DCCPF_SEND_LEV_RATE:
  526. return val < 2;
  527. case DCCPF_MIN_CSUM_COVER:
  528. return val < 16;
  529. }
  530. return 0; /* feature unknown */
  531. }
  532. static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
  533. {
  534. if (sp_list == NULL || sp_len < 1)
  535. return 0;
  536. while (sp_len--)
  537. if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
  538. return 0;
  539. return 1;
  540. }
  541. /**
  542. * dccp_feat_insert_opts - Generate FN options from current list state
  543. * @skb: next sk_buff to be sent to the peer
  544. * @dp: for client during handshake and general negotiation
  545. * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
  546. */
  547. int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
  548. struct sk_buff *skb)
  549. {
  550. struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
  551. struct dccp_feat_entry *pos, *next;
  552. u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
  553. bool rpt;
  554. /* put entries into @skb in the order they appear in the list */
  555. list_for_each_entry_safe_reverse(pos, next, fn, node) {
  556. opt = dccp_feat_genopt(pos);
  557. type = dccp_feat_type(pos->feat_num);
  558. rpt = false;
  559. if (pos->empty_confirm) {
  560. len = 0;
  561. ptr = NULL;
  562. } else {
  563. if (type == FEAT_SP) {
  564. len = pos->val.sp.len;
  565. ptr = pos->val.sp.vec;
  566. rpt = pos->needs_confirm;
  567. } else if (type == FEAT_NN) {
  568. len = dccp_feat_valid_nn_length(pos->feat_num);
  569. ptr = nn_in_nbo;
  570. dccp_encode_value_var(pos->val.nn, ptr, len);
  571. } else {
  572. DCCP_BUG("unknown feature %u", pos->feat_num);
  573. return -1;
  574. }
  575. }
  576. dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
  577. if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
  578. return -1;
  579. if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
  580. return -1;
  581. /*
  582. * Enter CHANGING after transmitting the Change option (6.6.2).
  583. */
  584. if (pos->state == FEAT_INITIALISING)
  585. pos->state = FEAT_CHANGING;
  586. }
  587. return 0;
  588. }
  589. /**
  590. * __feat_register_nn - Register new NN value on socket
  591. * @fn: feature-negotiation list to register with
  592. * @feat: an NN feature from %dccp_feature_numbers
  593. * @mandatory: use Mandatory option if 1
  594. * @nn_val: value to register (restricted to 4 bytes)
  595. * Note that NN features are local by definition (RFC 4340, 6.3.2).
  596. */
  597. static int __feat_register_nn(struct list_head *fn, u8 feat,
  598. u8 mandatory, u64 nn_val)
  599. {
  600. dccp_feat_val fval = { .nn = nn_val };
  601. if (dccp_feat_type(feat) != FEAT_NN ||
  602. !dccp_feat_is_valid_nn_val(feat, nn_val))
  603. return -EINVAL;
  604. /* Don't bother with default values, they will be activated anyway. */
  605. if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
  606. return 0;
  607. return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
  608. }
  609. /**
  610. * __feat_register_sp - Register new SP value/list on socket
  611. * @fn: feature-negotiation list to register with
  612. * @feat: an SP feature from %dccp_feature_numbers
  613. * @is_local: whether the local (1) or the remote (0) @feat is meant
  614. * @mandatory: use Mandatory option if 1
  615. * @sp_val: SP value followed by optional preference list
  616. * @sp_len: length of @sp_val in bytes
  617. */
  618. static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
  619. u8 mandatory, u8 const *sp_val, u8 sp_len)
  620. {
  621. dccp_feat_val fval;
  622. if (dccp_feat_type(feat) != FEAT_SP ||
  623. !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
  624. return -EINVAL;
  625. /* Avoid negotiating alien CCIDs by only advertising supported ones */
  626. if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
  627. return -EOPNOTSUPP;
  628. if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
  629. return -ENOMEM;
  630. return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
  631. }
  632. /**
  633. * dccp_feat_register_sp - Register requests to change SP feature values
  634. * @sk: client or listening socket
  635. * @feat: one of %dccp_feature_numbers
  636. * @is_local: whether the local (1) or remote (0) @feat is meant
  637. * @list: array of preferred values, in descending order of preference
  638. * @len: length of @list in bytes
  639. */
  640. int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
  641. u8 const *list, u8 len)
  642. { /* any changes must be registered before establishing the connection */
  643. if (sk->sk_state != DCCP_CLOSED)
  644. return -EISCONN;
  645. if (dccp_feat_type(feat) != FEAT_SP)
  646. return -EINVAL;
  647. return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
  648. 0, list, len);
  649. }
  650. /**
  651. * dccp_feat_nn_get - Query current/pending value of NN feature
  652. * @sk: DCCP socket of an established connection
  653. * @feat: NN feature number from %dccp_feature_numbers
  654. * For a known NN feature, returns value currently being negotiated, or
  655. * current (confirmed) value if no negotiation is going on.
  656. */
  657. u64 dccp_feat_nn_get(struct sock *sk, u8 feat)
  658. {
  659. if (dccp_feat_type(feat) == FEAT_NN) {
  660. struct dccp_sock *dp = dccp_sk(sk);
  661. struct dccp_feat_entry *entry;
  662. entry = dccp_feat_list_lookup(&dp->dccps_featneg, feat, 1);
  663. if (entry != NULL)
  664. return entry->val.nn;
  665. switch (feat) {
  666. case DCCPF_ACK_RATIO:
  667. return dp->dccps_l_ack_ratio;
  668. case DCCPF_SEQUENCE_WINDOW:
  669. return dp->dccps_l_seq_win;
  670. }
  671. }
  672. DCCP_BUG("attempt to look up unsupported feature %u", feat);
  673. return 0;
  674. }
  675. EXPORT_SYMBOL_GPL(dccp_feat_nn_get);
  676. /**
  677. * dccp_feat_signal_nn_change - Update NN values for an established connection
  678. * @sk: DCCP socket of an established connection
  679. * @feat: NN feature number from %dccp_feature_numbers
  680. * @nn_val: the new value to use
  681. * This function is used to communicate NN updates out-of-band.
  682. */
  683. int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
  684. {
  685. struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
  686. dccp_feat_val fval = { .nn = nn_val };
  687. struct dccp_feat_entry *entry;
  688. if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
  689. return 0;
  690. if (dccp_feat_type(feat) != FEAT_NN ||
  691. !dccp_feat_is_valid_nn_val(feat, nn_val))
  692. return -EINVAL;
  693. if (nn_val == dccp_feat_nn_get(sk, feat))
  694. return 0; /* already set or negotiation under way */
  695. entry = dccp_feat_list_lookup(fn, feat, 1);
  696. if (entry != NULL) {
  697. dccp_pr_debug("Clobbering existing NN entry %llu -> %llu\n",
  698. (unsigned long long)entry->val.nn,
  699. (unsigned long long)nn_val);
  700. dccp_feat_list_pop(entry);
  701. }
  702. inet_csk_schedule_ack(sk);
  703. return dccp_feat_push_change(fn, feat, 1, 0, &fval);
  704. }
  705. EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
  706. /*
  707. * Tracking features whose value depend on the choice of CCID
  708. *
  709. * This is designed with an extension in mind so that a list walk could be done
  710. * before activating any features. However, the existing framework was found to
  711. * work satisfactorily up until now, the automatic verification is left open.
  712. * When adding new CCIDs, add a corresponding dependency table here.
  713. */
  714. static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
  715. {
  716. static const struct ccid_dependency ccid2_dependencies[2][2] = {
  717. /*
  718. * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
  719. * feature and Send Ack Vector is an RX feature, `is_local'
  720. * needs to be reversed.
  721. */
  722. { /* Dependencies of the receiver-side (remote) CCID2 */
  723. {
  724. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  725. .is_local = true,
  726. .is_mandatory = true,
  727. .val = 1
  728. },
  729. { 0, 0, 0, 0 }
  730. },
  731. { /* Dependencies of the sender-side (local) CCID2 */
  732. {
  733. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  734. .is_local = false,
  735. .is_mandatory = true,
  736. .val = 1
  737. },
  738. { 0, 0, 0, 0 }
  739. }
  740. };
  741. static const struct ccid_dependency ccid3_dependencies[2][5] = {
  742. { /*
  743. * Dependencies of the receiver-side CCID3
  744. */
  745. { /* locally disable Ack Vectors */
  746. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  747. .is_local = true,
  748. .is_mandatory = false,
  749. .val = 0
  750. },
  751. { /* see below why Send Loss Event Rate is on */
  752. .dependent_feat = DCCPF_SEND_LEV_RATE,
  753. .is_local = true,
  754. .is_mandatory = true,
  755. .val = 1
  756. },
  757. { /* NDP Count is needed as per RFC 4342, 6.1.1 */
  758. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  759. .is_local = false,
  760. .is_mandatory = true,
  761. .val = 1
  762. },
  763. { 0, 0, 0, 0 },
  764. },
  765. { /*
  766. * CCID3 at the TX side: we request that the HC-receiver
  767. * will not send Ack Vectors (they will be ignored, so
  768. * Mandatory is not set); we enable Send Loss Event Rate
  769. * (Mandatory since the implementation does not support
  770. * the Loss Intervals option of RFC 4342, 8.6).
  771. * The last two options are for peer's information only.
  772. */
  773. {
  774. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  775. .is_local = false,
  776. .is_mandatory = false,
  777. .val = 0
  778. },
  779. {
  780. .dependent_feat = DCCPF_SEND_LEV_RATE,
  781. .is_local = false,
  782. .is_mandatory = true,
  783. .val = 1
  784. },
  785. { /* this CCID does not support Ack Ratio */
  786. .dependent_feat = DCCPF_ACK_RATIO,
  787. .is_local = true,
  788. .is_mandatory = false,
  789. .val = 0
  790. },
  791. { /* tell receiver we are sending NDP counts */
  792. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  793. .is_local = true,
  794. .is_mandatory = false,
  795. .val = 1
  796. },
  797. { 0, 0, 0, 0 }
  798. }
  799. };
  800. switch (ccid) {
  801. case DCCPC_CCID2:
  802. return ccid2_dependencies[is_local];
  803. case DCCPC_CCID3:
  804. return ccid3_dependencies[is_local];
  805. default:
  806. return NULL;
  807. }
  808. }
  809. /**
  810. * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
  811. * @fn: feature-negotiation list to update
  812. * @id: CCID number to track
  813. * @is_local: whether TX CCID (1) or RX CCID (0) is meant
  814. * This function needs to be called after registering all other features.
  815. */
  816. static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
  817. {
  818. const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
  819. int i, rc = (table == NULL);
  820. for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
  821. if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
  822. rc = __feat_register_sp(fn, table[i].dependent_feat,
  823. table[i].is_local,
  824. table[i].is_mandatory,
  825. &table[i].val, 1);
  826. else
  827. rc = __feat_register_nn(fn, table[i].dependent_feat,
  828. table[i].is_mandatory,
  829. table[i].val);
  830. return rc;
  831. }
  832. /**
  833. * dccp_feat_finalise_settings - Finalise settings before starting negotiation
  834. * @dp: client or listening socket (settings will be inherited)
  835. * This is called after all registrations (socket initialisation, sysctls, and
  836. * sockopt calls), and before sending the first packet containing Change options
  837. * (ie. client-Request or server-Response), to ensure internal consistency.
  838. */
  839. int dccp_feat_finalise_settings(struct dccp_sock *dp)
  840. {
  841. struct list_head *fn = &dp->dccps_featneg;
  842. struct dccp_feat_entry *entry;
  843. int i = 2, ccids[2] = { -1, -1 };
  844. /*
  845. * Propagating CCIDs:
  846. * 1) not useful to propagate CCID settings if this host advertises more
  847. * than one CCID: the choice of CCID may still change - if this is
  848. * the client, or if this is the server and the client sends
  849. * singleton CCID values.
  850. * 2) since is that propagate_ccid changes the list, we defer changing
  851. * the sorted list until after the traversal.
  852. */
  853. list_for_each_entry(entry, fn, node)
  854. if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
  855. ccids[entry->is_local] = entry->val.sp.vec[0];
  856. while (i--)
  857. if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
  858. return -1;
  859. dccp_feat_print_fnlist(fn);
  860. return 0;
  861. }
  862. /**
  863. * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
  864. * It is the server which resolves the dependencies once the CCID has been
  865. * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
  866. */
  867. int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
  868. {
  869. struct list_head *fn = &dreq->dreq_featneg;
  870. struct dccp_feat_entry *entry;
  871. u8 is_local, ccid;
  872. for (is_local = 0; is_local <= 1; is_local++) {
  873. entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
  874. if (entry != NULL && !entry->empty_confirm)
  875. ccid = entry->val.sp.vec[0];
  876. else
  877. ccid = dccp_feat_default_value(DCCPF_CCID);
  878. if (dccp_feat_propagate_ccid(fn, ccid, is_local))
  879. return -1;
  880. }
  881. return 0;
  882. }
  883. /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
  884. static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
  885. {
  886. u8 c, s;
  887. for (s = 0; s < slen; s++)
  888. for (c = 0; c < clen; c++)
  889. if (servlist[s] == clilist[c])
  890. return servlist[s];
  891. return -1;
  892. }
  893. /**
  894. * dccp_feat_prefer - Move preferred entry to the start of array
  895. * Reorder the @array_len elements in @array so that @preferred_value comes
  896. * first. Returns >0 to indicate that @preferred_value does occur in @array.
  897. */
  898. static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
  899. {
  900. u8 i, does_occur = 0;
  901. if (array != NULL) {
  902. for (i = 0; i < array_len; i++)
  903. if (array[i] == preferred_value) {
  904. array[i] = array[0];
  905. does_occur++;
  906. }
  907. if (does_occur)
  908. array[0] = preferred_value;
  909. }
  910. return does_occur;
  911. }
  912. /**
  913. * dccp_feat_reconcile - Reconcile SP preference lists
  914. * @fval: SP list to reconcile into
  915. * @arr: received SP preference list
  916. * @len: length of @arr in bytes
  917. * @is_server: whether this side is the server (and @fv is the server's list)
  918. * @reorder: whether to reorder the list in @fv after reconciling with @arr
  919. * When successful, > 0 is returned and the reconciled list is in @fval.
  920. * A value of 0 means that negotiation failed (no shared entry).
  921. */
  922. static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
  923. bool is_server, bool reorder)
  924. {
  925. int rc;
  926. if (!fv->sp.vec || !arr) {
  927. DCCP_CRIT("NULL feature value or array");
  928. return 0;
  929. }
  930. if (is_server)
  931. rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
  932. else
  933. rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
  934. if (!reorder)
  935. return rc;
  936. if (rc < 0)
  937. return 0;
  938. /*
  939. * Reorder list: used for activating features and in dccp_insert_fn_opt.
  940. */
  941. return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
  942. }
  943. /**
  944. * dccp_feat_change_recv - Process incoming ChangeL/R options
  945. * @fn: feature-negotiation list to update
  946. * @is_mandatory: whether the Change was preceded by a Mandatory option
  947. * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
  948. * @feat: one of %dccp_feature_numbers
  949. * @val: NN value or SP value/preference list
  950. * @len: length of @val in bytes
  951. * @server: whether this node is the server (1) or the client (0)
  952. */
  953. static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
  954. u8 feat, u8 *val, u8 len, const bool server)
  955. {
  956. u8 defval, type = dccp_feat_type(feat);
  957. const bool local = (opt == DCCPO_CHANGE_R);
  958. struct dccp_feat_entry *entry;
  959. dccp_feat_val fval;
  960. if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
  961. goto unknown_feature_or_value;
  962. dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
  963. /*
  964. * Negotiation of NN features: Change R is invalid, so there is no
  965. * simultaneous negotiation; hence we do not look up in the list.
  966. */
  967. if (type == FEAT_NN) {
  968. if (local || len > sizeof(fval.nn))
  969. goto unknown_feature_or_value;
  970. /* 6.3.2: "The feature remote MUST accept any valid value..." */
  971. fval.nn = dccp_decode_value_var(val, len);
  972. if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
  973. goto unknown_feature_or_value;
  974. return dccp_feat_push_confirm(fn, feat, local, &fval);
  975. }
  976. /*
  977. * Unidirectional/simultaneous negotiation of SP features (6.3.1)
  978. */
  979. entry = dccp_feat_list_lookup(fn, feat, local);
  980. if (entry == NULL) {
  981. /*
  982. * No particular preferences have been registered. We deal with
  983. * this situation by assuming that all valid values are equally
  984. * acceptable, and apply the following checks:
  985. * - if the peer's list is a singleton, we accept a valid value;
  986. * - if we are the server, we first try to see if the peer (the
  987. * client) advertises the default value. If yes, we use it,
  988. * otherwise we accept the preferred value;
  989. * - else if we are the client, we use the first list element.
  990. */
  991. if (dccp_feat_clone_sp_val(&fval, val, 1))
  992. return DCCP_RESET_CODE_TOO_BUSY;
  993. if (len > 1 && server) {
  994. defval = dccp_feat_default_value(feat);
  995. if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
  996. fval.sp.vec[0] = defval;
  997. } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
  998. kfree(fval.sp.vec);
  999. goto unknown_feature_or_value;
  1000. }
  1001. /* Treat unsupported CCIDs like invalid values */
  1002. if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
  1003. kfree(fval.sp.vec);
  1004. goto not_valid_or_not_known;
  1005. }
  1006. return dccp_feat_push_confirm(fn, feat, local, &fval);
  1007. } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
  1008. return 0;
  1009. }
  1010. if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
  1011. entry->empty_confirm = 0;
  1012. } else if (is_mandatory) {
  1013. return DCCP_RESET_CODE_MANDATORY_ERROR;
  1014. } else if (entry->state == FEAT_INITIALISING) {
  1015. /*
  1016. * Failed simultaneous negotiation (server only): try to `save'
  1017. * the connection by checking whether entry contains the default
  1018. * value for @feat. If yes, send an empty Confirm to signal that
  1019. * the received Change was not understood - which implies using
  1020. * the default value.
  1021. * If this also fails, we use Reset as the last resort.
  1022. */
  1023. WARN_ON(!server);
  1024. defval = dccp_feat_default_value(feat);
  1025. if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
  1026. return DCCP_RESET_CODE_OPTION_ERROR;
  1027. entry->empty_confirm = 1;
  1028. }
  1029. entry->needs_confirm = 1;
  1030. entry->needs_mandatory = 0;
  1031. entry->state = FEAT_STABLE;
  1032. return 0;
  1033. unknown_feature_or_value:
  1034. if (!is_mandatory)
  1035. return dccp_push_empty_confirm(fn, feat, local);
  1036. not_valid_or_not_known:
  1037. return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
  1038. : DCCP_RESET_CODE_OPTION_ERROR;
  1039. }
  1040. /**
  1041. * dccp_feat_confirm_recv - Process received Confirm options
  1042. * @fn: feature-negotiation list to update
  1043. * @is_mandatory: whether @opt was preceded by a Mandatory option
  1044. * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
  1045. * @feat: one of %dccp_feature_numbers
  1046. * @val: NN value or SP value/preference list
  1047. * @len: length of @val in bytes
  1048. * @server: whether this node is server (1) or client (0)
  1049. */
  1050. static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
  1051. u8 feat, u8 *val, u8 len, const bool server)
  1052. {
  1053. u8 *plist, plen, type = dccp_feat_type(feat);
  1054. const bool local = (opt == DCCPO_CONFIRM_R);
  1055. struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
  1056. dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
  1057. if (entry == NULL) { /* nothing queued: ignore or handle error */
  1058. if (is_mandatory && type == FEAT_UNKNOWN)
  1059. return DCCP_RESET_CODE_MANDATORY_ERROR;
  1060. if (!local && type == FEAT_NN) /* 6.3.2 */
  1061. goto confirmation_failed;
  1062. return 0;
  1063. }
  1064. if (entry->state != FEAT_CHANGING) /* 6.6.2 */
  1065. return 0;
  1066. if (len == 0) {
  1067. if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
  1068. goto confirmation_failed;
  1069. /*
  1070. * Empty Confirm during connection setup: this means reverting
  1071. * to the `old' value, which in this case is the default. Since
  1072. * we handle default values automatically when no other values
  1073. * have been set, we revert to the old value by removing this
  1074. * entry from the list.
  1075. */
  1076. dccp_feat_list_pop(entry);
  1077. return 0;
  1078. }
  1079. if (type == FEAT_NN) {
  1080. if (len > sizeof(entry->val.nn))
  1081. goto confirmation_failed;
  1082. if (entry->val.nn == dccp_decode_value_var(val, len))
  1083. goto confirmation_succeeded;
  1084. DCCP_WARN("Bogus Confirm for non-existing value\n");
  1085. goto confirmation_failed;
  1086. }
  1087. /*
  1088. * Parsing SP Confirms: the first element of @val is the preferred
  1089. * SP value which the peer confirms, the remainder depends on @len.
  1090. * Note that only the confirmed value need to be a valid SP value.
  1091. */
  1092. if (!dccp_feat_is_valid_sp_val(feat, *val))
  1093. goto confirmation_failed;
  1094. if (len == 1) { /* peer didn't supply a preference list */
  1095. plist = val;
  1096. plen = len;
  1097. } else { /* preferred value + preference list */
  1098. plist = val + 1;
  1099. plen = len - 1;
  1100. }
  1101. /* Check whether the peer got the reconciliation right (6.6.8) */
  1102. if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
  1103. DCCP_WARN("Confirm selected the wrong value %u\n", *val);
  1104. return DCCP_RESET_CODE_OPTION_ERROR;
  1105. }
  1106. entry->val.sp.vec[0] = *val;
  1107. confirmation_succeeded:
  1108. entry->state = FEAT_STABLE;
  1109. return 0;
  1110. confirmation_failed:
  1111. DCCP_WARN("Confirmation failed\n");
  1112. return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
  1113. : DCCP_RESET_CODE_OPTION_ERROR;
  1114. }
  1115. /**
  1116. * dccp_feat_parse_options - Process Feature-Negotiation Options
  1117. * @sk: for general use and used by the client during connection setup
  1118. * @dreq: used by the server during connection setup
  1119. * @mandatory: whether @opt was preceded by a Mandatory option
  1120. * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
  1121. * @feat: one of %dccp_feature_numbers
  1122. * @val: value contents of @opt
  1123. * @len: length of @val in bytes
  1124. * Returns 0 on success, a Reset code for ending the connection otherwise.
  1125. */
  1126. int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
  1127. u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
  1128. {
  1129. struct dccp_sock *dp = dccp_sk(sk);
  1130. struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
  1131. bool server = false;
  1132. switch (sk->sk_state) {
  1133. /*
  1134. * Negotiation during connection setup
  1135. */
  1136. case DCCP_LISTEN:
  1137. server = true; /* fall through */
  1138. case DCCP_REQUESTING:
  1139. switch (opt) {
  1140. case DCCPO_CHANGE_L:
  1141. case DCCPO_CHANGE_R:
  1142. return dccp_feat_change_recv(fn, mandatory, opt, feat,
  1143. val, len, server);
  1144. case DCCPO_CONFIRM_R:
  1145. case DCCPO_CONFIRM_L:
  1146. return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
  1147. val, len, server);
  1148. }
  1149. }
  1150. return 0; /* ignore FN options in all other states */
  1151. }
  1152. /**
  1153. * dccp_feat_init - Seed feature negotiation with host-specific defaults
  1154. * This initialises global defaults, depending on the value of the sysctls.
  1155. * These can later be overridden by registering changes via setsockopt calls.
  1156. * The last link in the chain is finalise_settings, to make sure that between
  1157. * here and the start of actual feature negotiation no inconsistencies enter.
  1158. *
  1159. * All features not appearing below use either defaults or are otherwise
  1160. * later adjusted through dccp_feat_finalise_settings().
  1161. */
  1162. int dccp_feat_init(struct sock *sk)
  1163. {
  1164. struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
  1165. u8 on = 1, off = 0;
  1166. int rc;
  1167. struct {
  1168. u8 *val;
  1169. u8 len;
  1170. } tx, rx;
  1171. /* Non-negotiable (NN) features */
  1172. rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
  1173. sysctl_dccp_sequence_window);
  1174. if (rc)
  1175. return rc;
  1176. /* Server-priority (SP) features */
  1177. /* Advertise that short seqnos are not supported (7.6.1) */
  1178. rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
  1179. if (rc)
  1180. return rc;
  1181. /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
  1182. rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
  1183. if (rc)
  1184. return rc;
  1185. /*
  1186. * We advertise the available list of CCIDs and reorder according to
  1187. * preferences, to avoid failure resulting from negotiating different
  1188. * singleton values (which always leads to failure).
  1189. * These settings can still (later) be overridden via sockopts.
  1190. */
  1191. if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
  1192. ccid_get_builtin_ccids(&rx.val, &rx.len))
  1193. return -ENOBUFS;
  1194. if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
  1195. !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
  1196. goto free_ccid_lists;
  1197. rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
  1198. if (rc)
  1199. goto free_ccid_lists;
  1200. rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
  1201. free_ccid_lists:
  1202. kfree(tx.val);
  1203. kfree(rx.val);
  1204. return rc;
  1205. }
  1206. int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
  1207. {
  1208. struct dccp_sock *dp = dccp_sk(sk);
  1209. struct dccp_feat_entry *cur, *next;
  1210. int idx;
  1211. dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
  1212. [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
  1213. };
  1214. list_for_each_entry(cur, fn_list, node) {
  1215. /*
  1216. * An empty Confirm means that either an unknown feature type
  1217. * or an invalid value was present. In the first case there is
  1218. * nothing to activate, in the other the default value is used.
  1219. */
  1220. if (cur->empty_confirm)
  1221. continue;
  1222. idx = dccp_feat_index(cur->feat_num);
  1223. if (idx < 0) {
  1224. DCCP_BUG("Unknown feature %u", cur->feat_num);
  1225. goto activation_failed;
  1226. }
  1227. if (cur->state != FEAT_STABLE) {
  1228. DCCP_CRIT("Negotiation of %s %s failed in state %s",
  1229. cur->is_local ? "local" : "remote",
  1230. dccp_feat_fname(cur->feat_num),
  1231. dccp_feat_sname[cur->state]);
  1232. goto activation_failed;
  1233. }
  1234. fvals[idx][cur->is_local] = &cur->val;
  1235. }
  1236. /*
  1237. * Activate in decreasing order of index, so that the CCIDs are always
  1238. * activated as the last feature. This avoids the case where a CCID
  1239. * relies on the initialisation of one or more features that it depends
  1240. * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
  1241. */
  1242. for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
  1243. if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
  1244. __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
  1245. DCCP_CRIT("Could not activate %d", idx);
  1246. goto activation_failed;
  1247. }
  1248. /* Clean up Change options which have been confirmed already */
  1249. list_for_each_entry_safe(cur, next, fn_list, node)
  1250. if (!cur->needs_confirm)
  1251. dccp_feat_list_pop(cur);
  1252. dccp_pr_debug("Activation OK\n");
  1253. return 0;
  1254. activation_failed:
  1255. /*
  1256. * We clean up everything that may have been allocated, since
  1257. * it is difficult to track at which stage negotiation failed.
  1258. * This is ok, since all allocation functions below are robust
  1259. * against NULL arguments.
  1260. */
  1261. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  1262. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  1263. dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  1264. dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
  1265. dp->dccps_hc_rx_ackvec = NULL;
  1266. return -1;
  1267. }