feat.c 31 KB

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