feat.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * net/dccp/feat.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
  6. *
  7. * ASSUMPTIONS
  8. * -----------
  9. * o Feature negotiation is coordinated with connection setup (as in TCP), wild
  10. * changes of parameters of an established connection are not supported.
  11. * o All currently known SP features have 1-byte quantities. If in the future
  12. * extensions of RFCs 4340..42 define features with item lengths larger than
  13. * one byte, a feature-specific extension of the code will be required.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/module.h>
  21. #include "ccid.h"
  22. #include "feat.h"
  23. #define DCCP_FEAT_SP_NOAGREE (-123)
  24. static const struct {
  25. u8 feat_num; /* DCCPF_xxx */
  26. enum dccp_feat_type rxtx; /* RX or TX */
  27. enum dccp_feat_type reconciliation; /* SP or NN */
  28. u8 default_value; /* as in 6.4 */
  29. /*
  30. * Lookup table for location and type of features (from RFC 4340/4342)
  31. * +--------------------------+----+-----+----+----+---------+-----------+
  32. * | Feature | Location | Reconc. | Initial | Section |
  33. * | | RX | TX | SP | NN | Value | Reference |
  34. * +--------------------------+----+-----+----+----+---------+-----------+
  35. * | DCCPF_CCID | | X | X | | 2 | 10 |
  36. * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
  37. * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
  38. * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
  39. * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
  40. * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
  41. * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
  42. * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
  43. * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
  44. * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
  45. * +--------------------------+----+-----+----+----+---------+-----------+
  46. */
  47. } dccp_feat_table[] = {
  48. { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2 },
  49. { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0 },
  50. { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100 },
  51. { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0 },
  52. { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2 },
  53. { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0 },
  54. { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0 },
  55. { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0 },
  56. { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0 },
  57. { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0 },
  58. };
  59. #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
  60. /**
  61. * dccp_feat_index - Hash function to map feature number into array position
  62. * Returns consecutive array index or -1 if the feature is not understood.
  63. */
  64. static int dccp_feat_index(u8 feat_num)
  65. {
  66. /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
  67. if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
  68. return feat_num - 1;
  69. /*
  70. * Other features: add cases for new feature types here after adding
  71. * them to the above table.
  72. */
  73. switch (feat_num) {
  74. case DCCPF_SEND_LEV_RATE:
  75. return DCCP_FEAT_SUPPORTED_MAX - 1;
  76. }
  77. return -1;
  78. }
  79. static u8 dccp_feat_type(u8 feat_num)
  80. {
  81. int idx = dccp_feat_index(feat_num);
  82. if (idx < 0)
  83. return FEAT_UNKNOWN;
  84. return dccp_feat_table[idx].reconciliation;
  85. }
  86. static int dccp_feat_default_value(u8 feat_num)
  87. {
  88. int idx = dccp_feat_index(feat_num);
  89. return idx < 0 ? : dccp_feat_table[idx].default_value;
  90. }
  91. /* copy constructor, fval must not already contain allocated memory */
  92. static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
  93. {
  94. fval->sp.len = len;
  95. if (fval->sp.len > 0) {
  96. fval->sp.vec = kmemdup(val, len, gfp_any());
  97. if (fval->sp.vec == NULL) {
  98. fval->sp.len = 0;
  99. return -ENOBUFS;
  100. }
  101. }
  102. return 0;
  103. }
  104. static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
  105. {
  106. if (unlikely(val == NULL))
  107. return;
  108. if (dccp_feat_type(feat_num) == FEAT_SP)
  109. kfree(val->sp.vec);
  110. memset(val, 0, sizeof(*val));
  111. }
  112. static struct dccp_feat_entry *
  113. dccp_feat_clone_entry(struct dccp_feat_entry const *original)
  114. {
  115. struct dccp_feat_entry *new;
  116. u8 type = dccp_feat_type(original->feat_num);
  117. if (type == FEAT_UNKNOWN)
  118. return NULL;
  119. new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
  120. if (new == NULL)
  121. return NULL;
  122. if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
  123. original->val.sp.vec,
  124. original->val.sp.len)) {
  125. kfree(new);
  126. return NULL;
  127. }
  128. return new;
  129. }
  130. static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
  131. {
  132. if (entry != NULL) {
  133. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  134. kfree(entry);
  135. }
  136. }
  137. /*
  138. * List management functions
  139. *
  140. * Feature negotiation lists rely on and maintain the following invariants:
  141. * - each feat_num in the list is known, i.e. we know its type and default value
  142. * - each feat_num/is_local combination is unique (old entries are overwritten)
  143. * - SP values are always freshly allocated
  144. * - list is sorted in increasing order of feature number (faster lookup)
  145. */
  146. static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
  147. u8 feat_num, bool is_local)
  148. {
  149. struct dccp_feat_entry *entry;
  150. list_for_each_entry(entry, fn_list, node)
  151. if (entry->feat_num == feat_num && entry->is_local == is_local)
  152. return entry;
  153. else if (entry->feat_num > feat_num)
  154. break;
  155. return NULL;
  156. }
  157. /**
  158. * dccp_feat_entry_new - Central list update routine (called by all others)
  159. * @head: list to add to
  160. * @feat: feature number
  161. * @local: whether the local (1) or remote feature with number @feat is meant
  162. * This is the only constructor and serves to ensure the above invariants.
  163. */
  164. static struct dccp_feat_entry *
  165. dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
  166. {
  167. struct dccp_feat_entry *entry;
  168. list_for_each_entry(entry, head, node)
  169. if (entry->feat_num == feat && entry->is_local == local) {
  170. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  171. return entry;
  172. } else if (entry->feat_num > feat) {
  173. head = &entry->node;
  174. break;
  175. }
  176. entry = kmalloc(sizeof(*entry), gfp_any());
  177. if (entry != NULL) {
  178. entry->feat_num = feat;
  179. entry->is_local = local;
  180. list_add_tail(&entry->node, head);
  181. }
  182. return entry;
  183. }
  184. /**
  185. * dccp_feat_push_change - Add/overwrite a Change option in the list
  186. * @fn_list: feature-negotiation list to update
  187. * @feat: one of %dccp_feature_numbers
  188. * @local: whether local (1) or remote (0) @feat_num is meant
  189. * @needs_mandatory: whether to use Mandatory feature negotiation options
  190. * @fval: pointer to NN/SP value to be inserted (will be copied)
  191. */
  192. static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
  193. u8 mandatory, dccp_feat_val *fval)
  194. {
  195. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  196. if (new == NULL)
  197. return -ENOMEM;
  198. new->feat_num = feat;
  199. new->is_local = local;
  200. new->state = FEAT_INITIALISING;
  201. new->needs_confirm = 0;
  202. new->empty_confirm = 0;
  203. new->val = *fval;
  204. new->needs_mandatory = mandatory;
  205. return 0;
  206. }
  207. /**
  208. * dccp_feat_push_confirm - Add a Confirm entry to the FN list
  209. * @fn_list: feature-negotiation list to add to
  210. * @feat: one of %dccp_feature_numbers
  211. * @local: whether local (1) or remote (0) @feat_num is being confirmed
  212. * @fval: pointer to NN/SP value to be inserted or NULL
  213. * Returns 0 on success, a Reset code for further processing otherwise.
  214. */
  215. static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
  216. dccp_feat_val *fval)
  217. {
  218. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  219. if (new == NULL)
  220. return DCCP_RESET_CODE_TOO_BUSY;
  221. new->feat_num = feat;
  222. new->is_local = local;
  223. new->state = FEAT_STABLE; /* transition in 6.6.2 */
  224. new->needs_confirm = 1;
  225. new->empty_confirm = (fval == NULL);
  226. new->val.nn = 0; /* zeroes the whole structure */
  227. if (!new->empty_confirm)
  228. new->val = *fval;
  229. new->needs_mandatory = 0;
  230. return 0;
  231. }
  232. static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
  233. {
  234. return dccp_feat_push_confirm(fn_list, feat, local, NULL);
  235. }
  236. static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
  237. {
  238. list_del(&entry->node);
  239. dccp_feat_entry_destructor(entry);
  240. }
  241. void dccp_feat_list_purge(struct list_head *fn_list)
  242. {
  243. struct dccp_feat_entry *entry, *next;
  244. list_for_each_entry_safe(entry, next, fn_list, node)
  245. dccp_feat_entry_destructor(entry);
  246. INIT_LIST_HEAD(fn_list);
  247. }
  248. EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
  249. /* generate @to as full clone of @from - @to must not contain any nodes */
  250. int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
  251. {
  252. struct dccp_feat_entry *entry, *new;
  253. INIT_LIST_HEAD(to);
  254. list_for_each_entry(entry, from, node) {
  255. new = dccp_feat_clone_entry(entry);
  256. if (new == NULL)
  257. goto cloning_failed;
  258. list_add_tail(&new->node, to);
  259. }
  260. return 0;
  261. cloning_failed:
  262. dccp_feat_list_purge(to);
  263. return -ENOMEM;
  264. }
  265. static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
  266. {
  267. switch (feat_num) {
  268. case DCCPF_ACK_RATIO:
  269. return val <= DCCPF_ACK_RATIO_MAX;
  270. case DCCPF_SEQUENCE_WINDOW:
  271. return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
  272. }
  273. return 0; /* feature unknown - so we can't tell */
  274. }
  275. /* check that SP values are within the ranges defined in RFC 4340 */
  276. static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
  277. {
  278. switch (feat_num) {
  279. case DCCPF_CCID:
  280. return val == DCCPC_CCID2 || val == DCCPC_CCID3;
  281. /* Type-check Boolean feature values: */
  282. case DCCPF_SHORT_SEQNOS:
  283. case DCCPF_ECN_INCAPABLE:
  284. case DCCPF_SEND_ACK_VECTOR:
  285. case DCCPF_SEND_NDP_COUNT:
  286. case DCCPF_DATA_CHECKSUM:
  287. case DCCPF_SEND_LEV_RATE:
  288. return val < 2;
  289. case DCCPF_MIN_CSUM_COVER:
  290. return val < 16;
  291. }
  292. return 0; /* feature unknown */
  293. }
  294. static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
  295. {
  296. if (sp_list == NULL || sp_len < 1)
  297. return 0;
  298. while (sp_len--)
  299. if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
  300. return 0;
  301. return 1;
  302. }
  303. /**
  304. * __feat_register_nn - Register new NN value on socket
  305. * @fn: feature-negotiation list to register with
  306. * @feat: an NN feature from %dccp_feature_numbers
  307. * @mandatory: use Mandatory option if 1
  308. * @nn_val: value to register (restricted to 4 bytes)
  309. * Note that NN features are local by definition (RFC 4340, 6.3.2).
  310. */
  311. static int __feat_register_nn(struct list_head *fn, u8 feat,
  312. u8 mandatory, u64 nn_val)
  313. {
  314. dccp_feat_val fval = { .nn = nn_val };
  315. if (dccp_feat_type(feat) != FEAT_NN ||
  316. !dccp_feat_is_valid_nn_val(feat, nn_val))
  317. return -EINVAL;
  318. /* Don't bother with default values, they will be activated anyway. */
  319. if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
  320. return 0;
  321. return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
  322. }
  323. /**
  324. * __feat_register_sp - Register new SP value/list on socket
  325. * @fn: feature-negotiation list to register with
  326. * @feat: an SP feature from %dccp_feature_numbers
  327. * @is_local: whether the local (1) or the remote (0) @feat is meant
  328. * @mandatory: use Mandatory option if 1
  329. * @sp_val: SP value followed by optional preference list
  330. * @sp_len: length of @sp_val in bytes
  331. */
  332. static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
  333. u8 mandatory, u8 const *sp_val, u8 sp_len)
  334. {
  335. dccp_feat_val fval;
  336. if (dccp_feat_type(feat) != FEAT_SP ||
  337. !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
  338. return -EINVAL;
  339. /* Avoid negotiating alien CCIDs by only advertising supported ones */
  340. if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
  341. return -EOPNOTSUPP;
  342. if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
  343. return -ENOMEM;
  344. return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
  345. }
  346. int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
  347. u8 *val, u8 len, gfp_t gfp)
  348. {
  349. struct dccp_opt_pend *opt;
  350. dccp_feat_debug(type, feature, *val);
  351. if (len > 3) {
  352. DCCP_WARN("invalid length %d\n", len);
  353. return -EINVAL;
  354. }
  355. /* XXX add further sanity checks */
  356. /* check if that feature is already being negotiated */
  357. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  358. /* ok we found a negotiation for this option already */
  359. if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
  360. dccp_pr_debug("Replacing old\n");
  361. /* replace */
  362. BUG_ON(opt->dccpop_val == NULL);
  363. kfree(opt->dccpop_val);
  364. opt->dccpop_val = val;
  365. opt->dccpop_len = len;
  366. opt->dccpop_conf = 0;
  367. return 0;
  368. }
  369. }
  370. /* negotiation for a new feature */
  371. opt = kmalloc(sizeof(*opt), gfp);
  372. if (opt == NULL)
  373. return -ENOMEM;
  374. opt->dccpop_type = type;
  375. opt->dccpop_feat = feature;
  376. opt->dccpop_len = len;
  377. opt->dccpop_val = val;
  378. opt->dccpop_conf = 0;
  379. opt->dccpop_sc = NULL;
  380. BUG_ON(opt->dccpop_val == NULL);
  381. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(dccp_feat_change);
  385. /*
  386. * Tracking features whose value depend on the choice of CCID
  387. *
  388. * This is designed with an extension in mind so that a list walk could be done
  389. * before activating any features. However, the existing framework was found to
  390. * work satisfactorily up until now, the automatic verification is left open.
  391. * When adding new CCIDs, add a corresponding dependency table here.
  392. */
  393. static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
  394. {
  395. static const struct ccid_dependency ccid2_dependencies[2][2] = {
  396. /*
  397. * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
  398. * feature and Send Ack Vector is an RX feature, `is_local'
  399. * needs to be reversed.
  400. */
  401. { /* Dependencies of the receiver-side (remote) CCID2 */
  402. {
  403. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  404. .is_local = true,
  405. .is_mandatory = true,
  406. .val = 1
  407. },
  408. { 0, 0, 0, 0 }
  409. },
  410. { /* Dependencies of the sender-side (local) CCID2 */
  411. {
  412. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  413. .is_local = false,
  414. .is_mandatory = true,
  415. .val = 1
  416. },
  417. { 0, 0, 0, 0 }
  418. }
  419. };
  420. static const struct ccid_dependency ccid3_dependencies[2][5] = {
  421. { /*
  422. * Dependencies of the receiver-side CCID3
  423. */
  424. { /* locally disable Ack Vectors */
  425. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  426. .is_local = true,
  427. .is_mandatory = false,
  428. .val = 0
  429. },
  430. { /* see below why Send Loss Event Rate is on */
  431. .dependent_feat = DCCPF_SEND_LEV_RATE,
  432. .is_local = true,
  433. .is_mandatory = true,
  434. .val = 1
  435. },
  436. { /* NDP Count is needed as per RFC 4342, 6.1.1 */
  437. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  438. .is_local = false,
  439. .is_mandatory = true,
  440. .val = 1
  441. },
  442. { 0, 0, 0, 0 },
  443. },
  444. { /*
  445. * CCID3 at the TX side: we request that the HC-receiver
  446. * will not send Ack Vectors (they will be ignored, so
  447. * Mandatory is not set); we enable Send Loss Event Rate
  448. * (Mandatory since the implementation does not support
  449. * the Loss Intervals option of RFC 4342, 8.6).
  450. * The last two options are for peer's information only.
  451. */
  452. {
  453. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  454. .is_local = false,
  455. .is_mandatory = false,
  456. .val = 0
  457. },
  458. {
  459. .dependent_feat = DCCPF_SEND_LEV_RATE,
  460. .is_local = false,
  461. .is_mandatory = true,
  462. .val = 1
  463. },
  464. { /* this CCID does not support Ack Ratio */
  465. .dependent_feat = DCCPF_ACK_RATIO,
  466. .is_local = true,
  467. .is_mandatory = false,
  468. .val = 0
  469. },
  470. { /* tell receiver we are sending NDP counts */
  471. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  472. .is_local = true,
  473. .is_mandatory = false,
  474. .val = 1
  475. },
  476. { 0, 0, 0, 0 }
  477. }
  478. };
  479. switch (ccid) {
  480. case DCCPC_CCID2:
  481. return ccid2_dependencies[is_local];
  482. case DCCPC_CCID3:
  483. return ccid3_dependencies[is_local];
  484. default:
  485. return NULL;
  486. }
  487. }
  488. /**
  489. * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
  490. * @fn: feature-negotiation list to update
  491. * @id: CCID number to track
  492. * @is_local: whether TX CCID (1) or RX CCID (0) is meant
  493. * This function needs to be called after registering all other features.
  494. */
  495. static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
  496. {
  497. const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
  498. int i, rc = (table == NULL);
  499. for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
  500. if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
  501. rc = __feat_register_sp(fn, table[i].dependent_feat,
  502. table[i].is_local,
  503. table[i].is_mandatory,
  504. &table[i].val, 1);
  505. else
  506. rc = __feat_register_nn(fn, table[i].dependent_feat,
  507. table[i].is_mandatory,
  508. table[i].val);
  509. return rc;
  510. }
  511. /**
  512. * dccp_feat_finalise_settings - Finalise settings before starting negotiation
  513. * @dp: client or listening socket (settings will be inherited)
  514. * This is called after all registrations (socket initialisation, sysctls, and
  515. * sockopt calls), and before sending the first packet containing Change options
  516. * (ie. client-Request or server-Response), to ensure internal consistency.
  517. */
  518. int dccp_feat_finalise_settings(struct dccp_sock *dp)
  519. {
  520. struct list_head *fn = &dp->dccps_featneg;
  521. struct dccp_feat_entry *entry;
  522. int i = 2, ccids[2] = { -1, -1 };
  523. /*
  524. * Propagating CCIDs:
  525. * 1) not useful to propagate CCID settings if this host advertises more
  526. * than one CCID: the choice of CCID may still change - if this is
  527. * the client, or if this is the server and the client sends
  528. * singleton CCID values.
  529. * 2) since is that propagate_ccid changes the list, we defer changing
  530. * the sorted list until after the traversal.
  531. */
  532. list_for_each_entry(entry, fn, node)
  533. if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
  534. ccids[entry->is_local] = entry->val.sp.vec[0];
  535. while (i--)
  536. if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
  537. return -1;
  538. return 0;
  539. }
  540. static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
  541. {
  542. struct dccp_sock *dp = dccp_sk(sk);
  543. struct dccp_minisock *dmsk = dccp_msk(sk);
  544. /* figure out if we are changing our CCID or the peer's */
  545. const int rx = type == DCCPO_CHANGE_R;
  546. const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
  547. struct ccid *new_ccid;
  548. /* Check if nothing is being changed. */
  549. if (ccid_nr == new_ccid_nr)
  550. return 0;
  551. new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
  552. if (new_ccid == NULL)
  553. return -ENOMEM;
  554. if (rx) {
  555. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  556. dp->dccps_hc_rx_ccid = new_ccid;
  557. dmsk->dccpms_rx_ccid = new_ccid_nr;
  558. } else {
  559. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  560. dp->dccps_hc_tx_ccid = new_ccid;
  561. dmsk->dccpms_tx_ccid = new_ccid_nr;
  562. }
  563. return 0;
  564. }
  565. static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
  566. {
  567. dccp_feat_debug(type, feat, val);
  568. switch (feat) {
  569. case DCCPF_CCID:
  570. return dccp_feat_update_ccid(sk, type, val);
  571. default:
  572. dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
  573. dccp_feat_typename(type), feat);
  574. break;
  575. }
  576. return 0;
  577. }
  578. static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
  579. u8 *rpref, u8 rlen)
  580. {
  581. struct dccp_sock *dp = dccp_sk(sk);
  582. u8 *spref, slen, *res = NULL;
  583. int i, j, rc, agree = 1;
  584. BUG_ON(rpref == NULL);
  585. /* check if we are the black sheep */
  586. if (dp->dccps_role == DCCP_ROLE_CLIENT) {
  587. spref = rpref;
  588. slen = rlen;
  589. rpref = opt->dccpop_val;
  590. rlen = opt->dccpop_len;
  591. } else {
  592. spref = opt->dccpop_val;
  593. slen = opt->dccpop_len;
  594. }
  595. /*
  596. * Now we have server preference list in spref and client preference in
  597. * rpref
  598. */
  599. BUG_ON(spref == NULL);
  600. BUG_ON(rpref == NULL);
  601. /* FIXME sanity check vals */
  602. /* Are values in any order? XXX Lame "algorithm" here */
  603. for (i = 0; i < slen; i++) {
  604. for (j = 0; j < rlen; j++) {
  605. if (spref[i] == rpref[j]) {
  606. res = &spref[i];
  607. break;
  608. }
  609. }
  610. if (res)
  611. break;
  612. }
  613. /* we didn't agree on anything */
  614. if (res == NULL) {
  615. /* confirm previous value */
  616. switch (opt->dccpop_feat) {
  617. case DCCPF_CCID:
  618. /* XXX did i get this right? =P */
  619. if (opt->dccpop_type == DCCPO_CHANGE_L)
  620. res = &dccp_msk(sk)->dccpms_tx_ccid;
  621. else
  622. res = &dccp_msk(sk)->dccpms_rx_ccid;
  623. break;
  624. default:
  625. DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
  626. /* XXX implement res */
  627. return -EFAULT;
  628. }
  629. dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
  630. agree = 0; /* this is used for mandatory options... */
  631. }
  632. /* need to put result and our preference list */
  633. rlen = 1 + opt->dccpop_len;
  634. rpref = kmalloc(rlen, GFP_ATOMIC);
  635. if (rpref == NULL)
  636. return -ENOMEM;
  637. *rpref = *res;
  638. memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
  639. /* put it in the "confirm queue" */
  640. if (opt->dccpop_sc == NULL) {
  641. opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
  642. if (opt->dccpop_sc == NULL) {
  643. kfree(rpref);
  644. return -ENOMEM;
  645. }
  646. } else {
  647. /* recycle the confirm slot */
  648. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  649. kfree(opt->dccpop_sc->dccpoc_val);
  650. dccp_pr_debug("recycling confirm slot\n");
  651. }
  652. memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
  653. opt->dccpop_sc->dccpoc_val = rpref;
  654. opt->dccpop_sc->dccpoc_len = rlen;
  655. /* update the option on our side [we are about to send the confirm] */
  656. rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
  657. if (rc) {
  658. kfree(opt->dccpop_sc->dccpoc_val);
  659. kfree(opt->dccpop_sc);
  660. opt->dccpop_sc = NULL;
  661. return rc;
  662. }
  663. dccp_pr_debug("Will confirm %d\n", *rpref);
  664. /* say we want to change to X but we just got a confirm X, suppress our
  665. * change
  666. */
  667. if (!opt->dccpop_conf) {
  668. if (*opt->dccpop_val == *res)
  669. opt->dccpop_conf = 1;
  670. dccp_pr_debug("won't ask for change of same feature\n");
  671. }
  672. return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
  673. }
  674. static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  675. {
  676. struct dccp_minisock *dmsk = dccp_msk(sk);
  677. struct dccp_opt_pend *opt;
  678. int rc = 1;
  679. u8 t;
  680. /*
  681. * We received a CHANGE. We gotta match it against our own preference
  682. * list. If we got a CHANGE_R it means it's a change for us, so we need
  683. * to compare our CHANGE_L list.
  684. */
  685. if (type == DCCPO_CHANGE_L)
  686. t = DCCPO_CHANGE_R;
  687. else
  688. t = DCCPO_CHANGE_L;
  689. /* find our preference list for this feature */
  690. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  691. if (opt->dccpop_type != t || opt->dccpop_feat != feature)
  692. continue;
  693. /* find the winner from the two preference lists */
  694. rc = dccp_feat_reconcile(sk, opt, val, len);
  695. break;
  696. }
  697. /* We didn't deal with the change. This can happen if we have no
  698. * preference list for the feature. In fact, it just shouldn't
  699. * happen---if we understand a feature, we should have a preference list
  700. * with at least the default value.
  701. */
  702. BUG_ON(rc == 1);
  703. return rc;
  704. }
  705. static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  706. {
  707. struct dccp_opt_pend *opt;
  708. struct dccp_minisock *dmsk = dccp_msk(sk);
  709. u8 *copy;
  710. int rc;
  711. /* NN features must be Change L (sec. 6.3.2) */
  712. if (type != DCCPO_CHANGE_L) {
  713. dccp_pr_debug("received %s for NN feature %d\n",
  714. dccp_feat_typename(type), feature);
  715. return -EFAULT;
  716. }
  717. /* XXX sanity check opt val */
  718. /* copy option so we can confirm it */
  719. opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  720. if (opt == NULL)
  721. return -ENOMEM;
  722. copy = kmemdup(val, len, GFP_ATOMIC);
  723. if (copy == NULL) {
  724. kfree(opt);
  725. return -ENOMEM;
  726. }
  727. opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
  728. opt->dccpop_feat = feature;
  729. opt->dccpop_val = copy;
  730. opt->dccpop_len = len;
  731. /* change feature */
  732. rc = dccp_feat_update(sk, type, feature, *val);
  733. if (rc) {
  734. kfree(opt->dccpop_val);
  735. kfree(opt);
  736. return rc;
  737. }
  738. dccp_feat_debug(type, feature, *copy);
  739. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  740. return 0;
  741. }
  742. static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
  743. u8 type, u8 feature)
  744. {
  745. /* XXX check if other confirms for that are queued and recycle slot */
  746. struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  747. if (opt == NULL) {
  748. /* XXX what do we do? Ignoring should be fine. It's a change
  749. * after all =P
  750. */
  751. return;
  752. }
  753. switch (type) {
  754. case DCCPO_CHANGE_L:
  755. opt->dccpop_type = DCCPO_CONFIRM_R;
  756. break;
  757. case DCCPO_CHANGE_R:
  758. opt->dccpop_type = DCCPO_CONFIRM_L;
  759. break;
  760. default:
  761. DCCP_WARN("invalid type %d\n", type);
  762. kfree(opt);
  763. return;
  764. }
  765. opt->dccpop_feat = feature;
  766. opt->dccpop_val = NULL;
  767. opt->dccpop_len = 0;
  768. /* change feature */
  769. dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
  770. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  771. }
  772. static void dccp_feat_flush_confirm(struct sock *sk)
  773. {
  774. struct dccp_minisock *dmsk = dccp_msk(sk);
  775. /* Check if there is anything to confirm in the first place */
  776. int yes = !list_empty(&dmsk->dccpms_conf);
  777. if (!yes) {
  778. struct dccp_opt_pend *opt;
  779. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  780. if (opt->dccpop_conf) {
  781. yes = 1;
  782. break;
  783. }
  784. }
  785. }
  786. if (!yes)
  787. return;
  788. /* OK there is something to confirm... */
  789. /* XXX check if packet is in flight? Send delayed ack?? */
  790. if (sk->sk_state == DCCP_OPEN)
  791. dccp_send_ack(sk);
  792. }
  793. int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  794. {
  795. int rc;
  796. /* Ignore Change requests other than during connection setup */
  797. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  798. return 0;
  799. dccp_feat_debug(type, feature, *val);
  800. /* figure out if it's SP or NN feature */
  801. switch (feature) {
  802. /* deal with SP features */
  803. case DCCPF_CCID:
  804. rc = dccp_feat_sp(sk, type, feature, val, len);
  805. break;
  806. /* deal with NN features */
  807. case DCCPF_ACK_RATIO:
  808. rc = dccp_feat_nn(sk, type, feature, val, len);
  809. break;
  810. /* XXX implement other features */
  811. default:
  812. dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
  813. dccp_feat_typename(type), feature);
  814. rc = -EFAULT;
  815. break;
  816. }
  817. /* check if there were problems changing features */
  818. if (rc) {
  819. /* If we don't agree on SP, we sent a confirm for old value.
  820. * However we propagate rc to caller in case option was
  821. * mandatory
  822. */
  823. if (rc != DCCP_FEAT_SP_NOAGREE)
  824. dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
  825. }
  826. /* generate the confirm [if required] */
  827. dccp_feat_flush_confirm(sk);
  828. return rc;
  829. }
  830. EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
  831. int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
  832. u8 *val, u8 len)
  833. {
  834. u8 t;
  835. struct dccp_opt_pend *opt;
  836. struct dccp_minisock *dmsk = dccp_msk(sk);
  837. int found = 0;
  838. int all_confirmed = 1;
  839. /* Ignore Confirm options other than during connection setup */
  840. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  841. return 0;
  842. dccp_feat_debug(type, feature, *val);
  843. /* locate our change request */
  844. switch (type) {
  845. case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
  846. case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
  847. default: DCCP_WARN("invalid type %d\n", type);
  848. return 1;
  849. }
  850. /* XXX sanity check feature value */
  851. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  852. if (!opt->dccpop_conf && opt->dccpop_type == t &&
  853. opt->dccpop_feat == feature) {
  854. found = 1;
  855. dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
  856. /* XXX do sanity check */
  857. opt->dccpop_conf = 1;
  858. /* We got a confirmation---change the option */
  859. dccp_feat_update(sk, opt->dccpop_type,
  860. opt->dccpop_feat, *val);
  861. /* XXX check the return value of dccp_feat_update */
  862. break;
  863. }
  864. if (!opt->dccpop_conf)
  865. all_confirmed = 0;
  866. }
  867. if (!found)
  868. dccp_pr_debug("%s(%d, ...) never requested\n",
  869. dccp_feat_typename(type), feature);
  870. return 0;
  871. }
  872. EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
  873. void dccp_feat_clean(struct dccp_minisock *dmsk)
  874. {
  875. struct dccp_opt_pend *opt, *next;
  876. list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
  877. dccpop_node) {
  878. BUG_ON(opt->dccpop_val == NULL);
  879. kfree(opt->dccpop_val);
  880. if (opt->dccpop_sc != NULL) {
  881. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  882. kfree(opt->dccpop_sc->dccpoc_val);
  883. kfree(opt->dccpop_sc);
  884. }
  885. kfree(opt);
  886. }
  887. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  888. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  889. BUG_ON(opt == NULL);
  890. if (opt->dccpop_val != NULL)
  891. kfree(opt->dccpop_val);
  892. kfree(opt);
  893. }
  894. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  895. }
  896. EXPORT_SYMBOL_GPL(dccp_feat_clean);
  897. /* this is to be called only when a listening sock creates its child. It is
  898. * assumed by the function---the confirm is not duplicated, but rather it is
  899. * "passed on".
  900. */
  901. int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
  902. {
  903. struct dccp_minisock *olddmsk = dccp_msk(oldsk);
  904. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  905. struct dccp_opt_pend *opt;
  906. int rc = 0;
  907. INIT_LIST_HEAD(&newdmsk->dccpms_pending);
  908. INIT_LIST_HEAD(&newdmsk->dccpms_conf);
  909. list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
  910. struct dccp_opt_pend *newopt;
  911. /* copy the value of the option */
  912. u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
  913. if (val == NULL)
  914. goto out_clean;
  915. newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
  916. if (newopt == NULL) {
  917. kfree(val);
  918. goto out_clean;
  919. }
  920. /* insert the option */
  921. newopt->dccpop_val = val;
  922. list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
  923. /* XXX what happens with backlogs and multiple connections at
  924. * once...
  925. */
  926. /* the master socket no longer needs to worry about confirms */
  927. opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
  928. /* reset state for a new socket */
  929. opt->dccpop_conf = 0;
  930. }
  931. /* XXX not doing anything about the conf queue */
  932. out:
  933. return rc;
  934. out_clean:
  935. dccp_feat_clean(newdmsk);
  936. rc = -ENOMEM;
  937. goto out;
  938. }
  939. EXPORT_SYMBOL_GPL(dccp_feat_clone);
  940. int dccp_feat_init(struct sock *sk)
  941. {
  942. struct dccp_sock *dp = dccp_sk(sk);
  943. struct dccp_minisock *dmsk = dccp_msk(sk);
  944. int rc;
  945. INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
  946. INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
  947. /* CCID L */
  948. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
  949. &dmsk->dccpms_tx_ccid, 1);
  950. if (rc)
  951. goto out;
  952. /* CCID R */
  953. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
  954. &dmsk->dccpms_rx_ccid, 1);
  955. if (rc)
  956. goto out;
  957. /* Ack ratio */
  958. rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
  959. dmsk->dccpms_ack_ratio);
  960. out:
  961. return rc;
  962. }
  963. EXPORT_SYMBOL_GPL(dccp_feat_init);
  964. #ifdef CONFIG_IP_DCCP_DEBUG
  965. const char *dccp_feat_typename(const u8 type)
  966. {
  967. switch(type) {
  968. case DCCPO_CHANGE_L: return("ChangeL");
  969. case DCCPO_CONFIRM_L: return("ConfirmL");
  970. case DCCPO_CHANGE_R: return("ChangeR");
  971. case DCCPO_CONFIRM_R: return("ConfirmR");
  972. /* the following case must not appear in feature negotation */
  973. default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
  974. }
  975. return NULL;
  976. }
  977. EXPORT_SYMBOL_GPL(dccp_feat_typename);
  978. const char *dccp_feat_name(const u8 feat)
  979. {
  980. static const char *feature_names[] = {
  981. [DCCPF_RESERVED] = "Reserved",
  982. [DCCPF_CCID] = "CCID",
  983. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  984. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  985. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  986. [DCCPF_ACK_RATIO] = "Ack Ratio",
  987. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  988. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  989. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  990. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  991. };
  992. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  993. return feature_names[DCCPF_RESERVED];
  994. if (feat == DCCPF_SEND_LEV_RATE)
  995. return "Send Loss Event Rate";
  996. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  997. return "CCID-specific";
  998. return feature_names[feat];
  999. }
  1000. EXPORT_SYMBOL_GPL(dccp_feat_name);
  1001. #endif /* CONFIG_IP_DCCP_DEBUG */