feat.c 47 KB

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