feat.c 39 KB

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