feat.c 47 KB

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