feat.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. /**
  541. * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
  542. * It is the server which resolves the dependencies once the CCID has been
  543. * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
  544. */
  545. int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
  546. {
  547. struct list_head *fn = &dreq->dreq_featneg;
  548. struct dccp_feat_entry *entry;
  549. u8 is_local, ccid;
  550. for (is_local = 0; is_local <= 1; is_local++) {
  551. entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
  552. if (entry != NULL && !entry->empty_confirm)
  553. ccid = entry->val.sp.vec[0];
  554. else
  555. ccid = dccp_feat_default_value(DCCPF_CCID);
  556. if (dccp_feat_propagate_ccid(fn, ccid, is_local))
  557. return -1;
  558. }
  559. return 0;
  560. }
  561. static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
  562. {
  563. struct dccp_sock *dp = dccp_sk(sk);
  564. struct dccp_minisock *dmsk = dccp_msk(sk);
  565. /* figure out if we are changing our CCID or the peer's */
  566. const int rx = type == DCCPO_CHANGE_R;
  567. const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
  568. struct ccid *new_ccid;
  569. /* Check if nothing is being changed. */
  570. if (ccid_nr == new_ccid_nr)
  571. return 0;
  572. new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
  573. if (new_ccid == NULL)
  574. return -ENOMEM;
  575. if (rx) {
  576. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  577. dp->dccps_hc_rx_ccid = new_ccid;
  578. dmsk->dccpms_rx_ccid = new_ccid_nr;
  579. } else {
  580. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  581. dp->dccps_hc_tx_ccid = new_ccid;
  582. dmsk->dccpms_tx_ccid = new_ccid_nr;
  583. }
  584. return 0;
  585. }
  586. static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
  587. {
  588. dccp_feat_debug(type, feat, val);
  589. switch (feat) {
  590. case DCCPF_CCID:
  591. return dccp_feat_update_ccid(sk, type, val);
  592. default:
  593. dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
  594. dccp_feat_typename(type), feat);
  595. break;
  596. }
  597. return 0;
  598. }
  599. static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
  600. u8 *rpref, u8 rlen)
  601. {
  602. struct dccp_sock *dp = dccp_sk(sk);
  603. u8 *spref, slen, *res = NULL;
  604. int i, j, rc, agree = 1;
  605. BUG_ON(rpref == NULL);
  606. /* check if we are the black sheep */
  607. if (dp->dccps_role == DCCP_ROLE_CLIENT) {
  608. spref = rpref;
  609. slen = rlen;
  610. rpref = opt->dccpop_val;
  611. rlen = opt->dccpop_len;
  612. } else {
  613. spref = opt->dccpop_val;
  614. slen = opt->dccpop_len;
  615. }
  616. /*
  617. * Now we have server preference list in spref and client preference in
  618. * rpref
  619. */
  620. BUG_ON(spref == NULL);
  621. BUG_ON(rpref == NULL);
  622. /* FIXME sanity check vals */
  623. /* Are values in any order? XXX Lame "algorithm" here */
  624. for (i = 0; i < slen; i++) {
  625. for (j = 0; j < rlen; j++) {
  626. if (spref[i] == rpref[j]) {
  627. res = &spref[i];
  628. break;
  629. }
  630. }
  631. if (res)
  632. break;
  633. }
  634. /* we didn't agree on anything */
  635. if (res == NULL) {
  636. /* confirm previous value */
  637. switch (opt->dccpop_feat) {
  638. case DCCPF_CCID:
  639. /* XXX did i get this right? =P */
  640. if (opt->dccpop_type == DCCPO_CHANGE_L)
  641. res = &dccp_msk(sk)->dccpms_tx_ccid;
  642. else
  643. res = &dccp_msk(sk)->dccpms_rx_ccid;
  644. break;
  645. default:
  646. DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
  647. /* XXX implement res */
  648. return -EFAULT;
  649. }
  650. dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
  651. agree = 0; /* this is used for mandatory options... */
  652. }
  653. /* need to put result and our preference list */
  654. rlen = 1 + opt->dccpop_len;
  655. rpref = kmalloc(rlen, GFP_ATOMIC);
  656. if (rpref == NULL)
  657. return -ENOMEM;
  658. *rpref = *res;
  659. memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
  660. /* put it in the "confirm queue" */
  661. if (opt->dccpop_sc == NULL) {
  662. opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
  663. if (opt->dccpop_sc == NULL) {
  664. kfree(rpref);
  665. return -ENOMEM;
  666. }
  667. } else {
  668. /* recycle the confirm slot */
  669. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  670. kfree(opt->dccpop_sc->dccpoc_val);
  671. dccp_pr_debug("recycling confirm slot\n");
  672. }
  673. memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
  674. opt->dccpop_sc->dccpoc_val = rpref;
  675. opt->dccpop_sc->dccpoc_len = rlen;
  676. /* update the option on our side [we are about to send the confirm] */
  677. rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
  678. if (rc) {
  679. kfree(opt->dccpop_sc->dccpoc_val);
  680. kfree(opt->dccpop_sc);
  681. opt->dccpop_sc = NULL;
  682. return rc;
  683. }
  684. dccp_pr_debug("Will confirm %d\n", *rpref);
  685. /* say we want to change to X but we just got a confirm X, suppress our
  686. * change
  687. */
  688. if (!opt->dccpop_conf) {
  689. if (*opt->dccpop_val == *res)
  690. opt->dccpop_conf = 1;
  691. dccp_pr_debug("won't ask for change of same feature\n");
  692. }
  693. return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
  694. }
  695. static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  696. {
  697. struct dccp_minisock *dmsk = dccp_msk(sk);
  698. struct dccp_opt_pend *opt;
  699. int rc = 1;
  700. u8 t;
  701. /*
  702. * We received a CHANGE. We gotta match it against our own preference
  703. * list. If we got a CHANGE_R it means it's a change for us, so we need
  704. * to compare our CHANGE_L list.
  705. */
  706. if (type == DCCPO_CHANGE_L)
  707. t = DCCPO_CHANGE_R;
  708. else
  709. t = DCCPO_CHANGE_L;
  710. /* find our preference list for this feature */
  711. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  712. if (opt->dccpop_type != t || opt->dccpop_feat != feature)
  713. continue;
  714. /* find the winner from the two preference lists */
  715. rc = dccp_feat_reconcile(sk, opt, val, len);
  716. break;
  717. }
  718. /* We didn't deal with the change. This can happen if we have no
  719. * preference list for the feature. In fact, it just shouldn't
  720. * happen---if we understand a feature, we should have a preference list
  721. * with at least the default value.
  722. */
  723. BUG_ON(rc == 1);
  724. return rc;
  725. }
  726. static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  727. {
  728. struct dccp_opt_pend *opt;
  729. struct dccp_minisock *dmsk = dccp_msk(sk);
  730. u8 *copy;
  731. int rc;
  732. /* NN features must be Change L (sec. 6.3.2) */
  733. if (type != DCCPO_CHANGE_L) {
  734. dccp_pr_debug("received %s for NN feature %d\n",
  735. dccp_feat_typename(type), feature);
  736. return -EFAULT;
  737. }
  738. /* XXX sanity check opt val */
  739. /* copy option so we can confirm it */
  740. opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  741. if (opt == NULL)
  742. return -ENOMEM;
  743. copy = kmemdup(val, len, GFP_ATOMIC);
  744. if (copy == NULL) {
  745. kfree(opt);
  746. return -ENOMEM;
  747. }
  748. opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
  749. opt->dccpop_feat = feature;
  750. opt->dccpop_val = copy;
  751. opt->dccpop_len = len;
  752. /* change feature */
  753. rc = dccp_feat_update(sk, type, feature, *val);
  754. if (rc) {
  755. kfree(opt->dccpop_val);
  756. kfree(opt);
  757. return rc;
  758. }
  759. dccp_feat_debug(type, feature, *copy);
  760. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  761. return 0;
  762. }
  763. static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
  764. u8 type, u8 feature)
  765. {
  766. /* XXX check if other confirms for that are queued and recycle slot */
  767. struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  768. if (opt == NULL) {
  769. /* XXX what do we do? Ignoring should be fine. It's a change
  770. * after all =P
  771. */
  772. return;
  773. }
  774. switch (type) {
  775. case DCCPO_CHANGE_L:
  776. opt->dccpop_type = DCCPO_CONFIRM_R;
  777. break;
  778. case DCCPO_CHANGE_R:
  779. opt->dccpop_type = DCCPO_CONFIRM_L;
  780. break;
  781. default:
  782. DCCP_WARN("invalid type %d\n", type);
  783. kfree(opt);
  784. return;
  785. }
  786. opt->dccpop_feat = feature;
  787. opt->dccpop_val = NULL;
  788. opt->dccpop_len = 0;
  789. /* change feature */
  790. dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
  791. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  792. }
  793. static void dccp_feat_flush_confirm(struct sock *sk)
  794. {
  795. struct dccp_minisock *dmsk = dccp_msk(sk);
  796. /* Check if there is anything to confirm in the first place */
  797. int yes = !list_empty(&dmsk->dccpms_conf);
  798. if (!yes) {
  799. struct dccp_opt_pend *opt;
  800. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  801. if (opt->dccpop_conf) {
  802. yes = 1;
  803. break;
  804. }
  805. }
  806. }
  807. if (!yes)
  808. return;
  809. /* OK there is something to confirm... */
  810. /* XXX check if packet is in flight? Send delayed ack?? */
  811. if (sk->sk_state == DCCP_OPEN)
  812. dccp_send_ack(sk);
  813. }
  814. int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  815. {
  816. int rc;
  817. /* Ignore Change requests other than during connection setup */
  818. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  819. return 0;
  820. dccp_feat_debug(type, feature, *val);
  821. /* figure out if it's SP or NN feature */
  822. switch (feature) {
  823. /* deal with SP features */
  824. case DCCPF_CCID:
  825. rc = dccp_feat_sp(sk, type, feature, val, len);
  826. break;
  827. /* deal with NN features */
  828. case DCCPF_ACK_RATIO:
  829. rc = dccp_feat_nn(sk, type, feature, val, len);
  830. break;
  831. /* XXX implement other features */
  832. default:
  833. dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
  834. dccp_feat_typename(type), feature);
  835. rc = -EFAULT;
  836. break;
  837. }
  838. /* check if there were problems changing features */
  839. if (rc) {
  840. /* If we don't agree on SP, we sent a confirm for old value.
  841. * However we propagate rc to caller in case option was
  842. * mandatory
  843. */
  844. if (rc != DCCP_FEAT_SP_NOAGREE)
  845. dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
  846. }
  847. /* generate the confirm [if required] */
  848. dccp_feat_flush_confirm(sk);
  849. return rc;
  850. }
  851. EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
  852. int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
  853. u8 *val, u8 len)
  854. {
  855. u8 t;
  856. struct dccp_opt_pend *opt;
  857. struct dccp_minisock *dmsk = dccp_msk(sk);
  858. int found = 0;
  859. int all_confirmed = 1;
  860. /* Ignore Confirm options other than during connection setup */
  861. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  862. return 0;
  863. dccp_feat_debug(type, feature, *val);
  864. /* locate our change request */
  865. switch (type) {
  866. case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
  867. case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
  868. default: DCCP_WARN("invalid type %d\n", type);
  869. return 1;
  870. }
  871. /* XXX sanity check feature value */
  872. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  873. if (!opt->dccpop_conf && opt->dccpop_type == t &&
  874. opt->dccpop_feat == feature) {
  875. found = 1;
  876. dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
  877. /* XXX do sanity check */
  878. opt->dccpop_conf = 1;
  879. /* We got a confirmation---change the option */
  880. dccp_feat_update(sk, opt->dccpop_type,
  881. opt->dccpop_feat, *val);
  882. /* XXX check the return value of dccp_feat_update */
  883. break;
  884. }
  885. if (!opt->dccpop_conf)
  886. all_confirmed = 0;
  887. }
  888. if (!found)
  889. dccp_pr_debug("%s(%d, ...) never requested\n",
  890. dccp_feat_typename(type), feature);
  891. return 0;
  892. }
  893. EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
  894. void dccp_feat_clean(struct dccp_minisock *dmsk)
  895. {
  896. struct dccp_opt_pend *opt, *next;
  897. list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
  898. dccpop_node) {
  899. BUG_ON(opt->dccpop_val == NULL);
  900. kfree(opt->dccpop_val);
  901. if (opt->dccpop_sc != NULL) {
  902. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  903. kfree(opt->dccpop_sc->dccpoc_val);
  904. kfree(opt->dccpop_sc);
  905. }
  906. kfree(opt);
  907. }
  908. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  909. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  910. BUG_ON(opt == NULL);
  911. if (opt->dccpop_val != NULL)
  912. kfree(opt->dccpop_val);
  913. kfree(opt);
  914. }
  915. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  916. }
  917. EXPORT_SYMBOL_GPL(dccp_feat_clean);
  918. /* this is to be called only when a listening sock creates its child. It is
  919. * assumed by the function---the confirm is not duplicated, but rather it is
  920. * "passed on".
  921. */
  922. int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
  923. {
  924. struct dccp_minisock *olddmsk = dccp_msk(oldsk);
  925. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  926. struct dccp_opt_pend *opt;
  927. int rc = 0;
  928. INIT_LIST_HEAD(&newdmsk->dccpms_pending);
  929. INIT_LIST_HEAD(&newdmsk->dccpms_conf);
  930. list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
  931. struct dccp_opt_pend *newopt;
  932. /* copy the value of the option */
  933. u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
  934. if (val == NULL)
  935. goto out_clean;
  936. newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
  937. if (newopt == NULL) {
  938. kfree(val);
  939. goto out_clean;
  940. }
  941. /* insert the option */
  942. newopt->dccpop_val = val;
  943. list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
  944. /* XXX what happens with backlogs and multiple connections at
  945. * once...
  946. */
  947. /* the master socket no longer needs to worry about confirms */
  948. opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
  949. /* reset state for a new socket */
  950. opt->dccpop_conf = 0;
  951. }
  952. /* XXX not doing anything about the conf queue */
  953. out:
  954. return rc;
  955. out_clean:
  956. dccp_feat_clean(newdmsk);
  957. rc = -ENOMEM;
  958. goto out;
  959. }
  960. EXPORT_SYMBOL_GPL(dccp_feat_clone);
  961. int dccp_feat_init(struct sock *sk)
  962. {
  963. struct dccp_sock *dp = dccp_sk(sk);
  964. struct dccp_minisock *dmsk = dccp_msk(sk);
  965. int rc;
  966. INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
  967. INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
  968. /* CCID L */
  969. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
  970. &dmsk->dccpms_tx_ccid, 1);
  971. if (rc)
  972. goto out;
  973. /* CCID R */
  974. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
  975. &dmsk->dccpms_rx_ccid, 1);
  976. if (rc)
  977. goto out;
  978. /* Ack ratio */
  979. rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
  980. dmsk->dccpms_ack_ratio);
  981. out:
  982. return rc;
  983. }
  984. EXPORT_SYMBOL_GPL(dccp_feat_init);
  985. #ifdef CONFIG_IP_DCCP_DEBUG
  986. const char *dccp_feat_typename(const u8 type)
  987. {
  988. switch(type) {
  989. case DCCPO_CHANGE_L: return("ChangeL");
  990. case DCCPO_CONFIRM_L: return("ConfirmL");
  991. case DCCPO_CHANGE_R: return("ChangeR");
  992. case DCCPO_CONFIRM_R: return("ConfirmR");
  993. /* the following case must not appear in feature negotation */
  994. default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
  995. }
  996. return NULL;
  997. }
  998. EXPORT_SYMBOL_GPL(dccp_feat_typename);
  999. const char *dccp_feat_name(const u8 feat)
  1000. {
  1001. static const char *feature_names[] = {
  1002. [DCCPF_RESERVED] = "Reserved",
  1003. [DCCPF_CCID] = "CCID",
  1004. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  1005. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  1006. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  1007. [DCCPF_ACK_RATIO] = "Ack Ratio",
  1008. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  1009. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  1010. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  1011. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  1012. };
  1013. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  1014. return feature_names[DCCPF_RESERVED];
  1015. if (feat == DCCPF_SEND_LEV_RATE)
  1016. return "Send Loss Event Rate";
  1017. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  1018. return "CCID-specific";
  1019. return feature_names[feat];
  1020. }
  1021. EXPORT_SYMBOL_GPL(dccp_feat_name);
  1022. #endif /* CONFIG_IP_DCCP_DEBUG */