feat.c 37 KB

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