feat.c 47 KB

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