feat.c 30 KB

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