feat.c 47 KB

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