feat.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
  340. return -ENOMEM;
  341. return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
  342. }
  343. int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
  344. u8 *val, u8 len, gfp_t gfp)
  345. {
  346. struct dccp_opt_pend *opt;
  347. dccp_feat_debug(type, feature, *val);
  348. if (len > 3) {
  349. DCCP_WARN("invalid length %d\n", len);
  350. return -EINVAL;
  351. }
  352. /* XXX add further sanity checks */
  353. /* check if that feature is already being negotiated */
  354. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  355. /* ok we found a negotiation for this option already */
  356. if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
  357. dccp_pr_debug("Replacing old\n");
  358. /* replace */
  359. BUG_ON(opt->dccpop_val == NULL);
  360. kfree(opt->dccpop_val);
  361. opt->dccpop_val = val;
  362. opt->dccpop_len = len;
  363. opt->dccpop_conf = 0;
  364. return 0;
  365. }
  366. }
  367. /* negotiation for a new feature */
  368. opt = kmalloc(sizeof(*opt), gfp);
  369. if (opt == NULL)
  370. return -ENOMEM;
  371. opt->dccpop_type = type;
  372. opt->dccpop_feat = feature;
  373. opt->dccpop_len = len;
  374. opt->dccpop_val = val;
  375. opt->dccpop_conf = 0;
  376. opt->dccpop_sc = NULL;
  377. BUG_ON(opt->dccpop_val == NULL);
  378. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
  379. return 0;
  380. }
  381. EXPORT_SYMBOL_GPL(dccp_feat_change);
  382. static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
  383. {
  384. struct dccp_sock *dp = dccp_sk(sk);
  385. struct dccp_minisock *dmsk = dccp_msk(sk);
  386. /* figure out if we are changing our CCID or the peer's */
  387. const int rx = type == DCCPO_CHANGE_R;
  388. const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
  389. struct ccid *new_ccid;
  390. /* Check if nothing is being changed. */
  391. if (ccid_nr == new_ccid_nr)
  392. return 0;
  393. new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
  394. if (new_ccid == NULL)
  395. return -ENOMEM;
  396. if (rx) {
  397. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  398. dp->dccps_hc_rx_ccid = new_ccid;
  399. dmsk->dccpms_rx_ccid = new_ccid_nr;
  400. } else {
  401. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  402. dp->dccps_hc_tx_ccid = new_ccid;
  403. dmsk->dccpms_tx_ccid = new_ccid_nr;
  404. }
  405. return 0;
  406. }
  407. static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
  408. {
  409. dccp_feat_debug(type, feat, val);
  410. switch (feat) {
  411. case DCCPF_CCID:
  412. return dccp_feat_update_ccid(sk, type, val);
  413. default:
  414. dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
  415. dccp_feat_typename(type), feat);
  416. break;
  417. }
  418. return 0;
  419. }
  420. static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
  421. u8 *rpref, u8 rlen)
  422. {
  423. struct dccp_sock *dp = dccp_sk(sk);
  424. u8 *spref, slen, *res = NULL;
  425. int i, j, rc, agree = 1;
  426. BUG_ON(rpref == NULL);
  427. /* check if we are the black sheep */
  428. if (dp->dccps_role == DCCP_ROLE_CLIENT) {
  429. spref = rpref;
  430. slen = rlen;
  431. rpref = opt->dccpop_val;
  432. rlen = opt->dccpop_len;
  433. } else {
  434. spref = opt->dccpop_val;
  435. slen = opt->dccpop_len;
  436. }
  437. /*
  438. * Now we have server preference list in spref and client preference in
  439. * rpref
  440. */
  441. BUG_ON(spref == NULL);
  442. BUG_ON(rpref == NULL);
  443. /* FIXME sanity check vals */
  444. /* Are values in any order? XXX Lame "algorithm" here */
  445. for (i = 0; i < slen; i++) {
  446. for (j = 0; j < rlen; j++) {
  447. if (spref[i] == rpref[j]) {
  448. res = &spref[i];
  449. break;
  450. }
  451. }
  452. if (res)
  453. break;
  454. }
  455. /* we didn't agree on anything */
  456. if (res == NULL) {
  457. /* confirm previous value */
  458. switch (opt->dccpop_feat) {
  459. case DCCPF_CCID:
  460. /* XXX did i get this right? =P */
  461. if (opt->dccpop_type == DCCPO_CHANGE_L)
  462. res = &dccp_msk(sk)->dccpms_tx_ccid;
  463. else
  464. res = &dccp_msk(sk)->dccpms_rx_ccid;
  465. break;
  466. default:
  467. DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
  468. /* XXX implement res */
  469. return -EFAULT;
  470. }
  471. dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
  472. agree = 0; /* this is used for mandatory options... */
  473. }
  474. /* need to put result and our preference list */
  475. rlen = 1 + opt->dccpop_len;
  476. rpref = kmalloc(rlen, GFP_ATOMIC);
  477. if (rpref == NULL)
  478. return -ENOMEM;
  479. *rpref = *res;
  480. memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
  481. /* put it in the "confirm queue" */
  482. if (opt->dccpop_sc == NULL) {
  483. opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
  484. if (opt->dccpop_sc == NULL) {
  485. kfree(rpref);
  486. return -ENOMEM;
  487. }
  488. } else {
  489. /* recycle the confirm slot */
  490. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  491. kfree(opt->dccpop_sc->dccpoc_val);
  492. dccp_pr_debug("recycling confirm slot\n");
  493. }
  494. memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
  495. opt->dccpop_sc->dccpoc_val = rpref;
  496. opt->dccpop_sc->dccpoc_len = rlen;
  497. /* update the option on our side [we are about to send the confirm] */
  498. rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
  499. if (rc) {
  500. kfree(opt->dccpop_sc->dccpoc_val);
  501. kfree(opt->dccpop_sc);
  502. opt->dccpop_sc = NULL;
  503. return rc;
  504. }
  505. dccp_pr_debug("Will confirm %d\n", *rpref);
  506. /* say we want to change to X but we just got a confirm X, suppress our
  507. * change
  508. */
  509. if (!opt->dccpop_conf) {
  510. if (*opt->dccpop_val == *res)
  511. opt->dccpop_conf = 1;
  512. dccp_pr_debug("won't ask for change of same feature\n");
  513. }
  514. return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
  515. }
  516. static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  517. {
  518. struct dccp_minisock *dmsk = dccp_msk(sk);
  519. struct dccp_opt_pend *opt;
  520. int rc = 1;
  521. u8 t;
  522. /*
  523. * We received a CHANGE. We gotta match it against our own preference
  524. * list. If we got a CHANGE_R it means it's a change for us, so we need
  525. * to compare our CHANGE_L list.
  526. */
  527. if (type == DCCPO_CHANGE_L)
  528. t = DCCPO_CHANGE_R;
  529. else
  530. t = DCCPO_CHANGE_L;
  531. /* find our preference list for this feature */
  532. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  533. if (opt->dccpop_type != t || opt->dccpop_feat != feature)
  534. continue;
  535. /* find the winner from the two preference lists */
  536. rc = dccp_feat_reconcile(sk, opt, val, len);
  537. break;
  538. }
  539. /* We didn't deal with the change. This can happen if we have no
  540. * preference list for the feature. In fact, it just shouldn't
  541. * happen---if we understand a feature, we should have a preference list
  542. * with at least the default value.
  543. */
  544. BUG_ON(rc == 1);
  545. return rc;
  546. }
  547. static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  548. {
  549. struct dccp_opt_pend *opt;
  550. struct dccp_minisock *dmsk = dccp_msk(sk);
  551. u8 *copy;
  552. int rc;
  553. /* NN features must be Change L (sec. 6.3.2) */
  554. if (type != DCCPO_CHANGE_L) {
  555. dccp_pr_debug("received %s for NN feature %d\n",
  556. dccp_feat_typename(type), feature);
  557. return -EFAULT;
  558. }
  559. /* XXX sanity check opt val */
  560. /* copy option so we can confirm it */
  561. opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  562. if (opt == NULL)
  563. return -ENOMEM;
  564. copy = kmemdup(val, len, GFP_ATOMIC);
  565. if (copy == NULL) {
  566. kfree(opt);
  567. return -ENOMEM;
  568. }
  569. opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
  570. opt->dccpop_feat = feature;
  571. opt->dccpop_val = copy;
  572. opt->dccpop_len = len;
  573. /* change feature */
  574. rc = dccp_feat_update(sk, type, feature, *val);
  575. if (rc) {
  576. kfree(opt->dccpop_val);
  577. kfree(opt);
  578. return rc;
  579. }
  580. dccp_feat_debug(type, feature, *copy);
  581. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  582. return 0;
  583. }
  584. static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
  585. u8 type, u8 feature)
  586. {
  587. /* XXX check if other confirms for that are queued and recycle slot */
  588. struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  589. if (opt == NULL) {
  590. /* XXX what do we do? Ignoring should be fine. It's a change
  591. * after all =P
  592. */
  593. return;
  594. }
  595. switch (type) {
  596. case DCCPO_CHANGE_L:
  597. opt->dccpop_type = DCCPO_CONFIRM_R;
  598. break;
  599. case DCCPO_CHANGE_R:
  600. opt->dccpop_type = DCCPO_CONFIRM_L;
  601. break;
  602. default:
  603. DCCP_WARN("invalid type %d\n", type);
  604. kfree(opt);
  605. return;
  606. }
  607. opt->dccpop_feat = feature;
  608. opt->dccpop_val = NULL;
  609. opt->dccpop_len = 0;
  610. /* change feature */
  611. dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
  612. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  613. }
  614. static void dccp_feat_flush_confirm(struct sock *sk)
  615. {
  616. struct dccp_minisock *dmsk = dccp_msk(sk);
  617. /* Check if there is anything to confirm in the first place */
  618. int yes = !list_empty(&dmsk->dccpms_conf);
  619. if (!yes) {
  620. struct dccp_opt_pend *opt;
  621. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  622. if (opt->dccpop_conf) {
  623. yes = 1;
  624. break;
  625. }
  626. }
  627. }
  628. if (!yes)
  629. return;
  630. /* OK there is something to confirm... */
  631. /* XXX check if packet is in flight? Send delayed ack?? */
  632. if (sk->sk_state == DCCP_OPEN)
  633. dccp_send_ack(sk);
  634. }
  635. int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  636. {
  637. int rc;
  638. /* Ignore Change requests other than during connection setup */
  639. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  640. return 0;
  641. dccp_feat_debug(type, feature, *val);
  642. /* figure out if it's SP or NN feature */
  643. switch (feature) {
  644. /* deal with SP features */
  645. case DCCPF_CCID:
  646. rc = dccp_feat_sp(sk, type, feature, val, len);
  647. break;
  648. /* deal with NN features */
  649. case DCCPF_ACK_RATIO:
  650. rc = dccp_feat_nn(sk, type, feature, val, len);
  651. break;
  652. /* XXX implement other features */
  653. default:
  654. dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
  655. dccp_feat_typename(type), feature);
  656. rc = -EFAULT;
  657. break;
  658. }
  659. /* check if there were problems changing features */
  660. if (rc) {
  661. /* If we don't agree on SP, we sent a confirm for old value.
  662. * However we propagate rc to caller in case option was
  663. * mandatory
  664. */
  665. if (rc != DCCP_FEAT_SP_NOAGREE)
  666. dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
  667. }
  668. /* generate the confirm [if required] */
  669. dccp_feat_flush_confirm(sk);
  670. return rc;
  671. }
  672. EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
  673. int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
  674. u8 *val, u8 len)
  675. {
  676. u8 t;
  677. struct dccp_opt_pend *opt;
  678. struct dccp_minisock *dmsk = dccp_msk(sk);
  679. int found = 0;
  680. int all_confirmed = 1;
  681. /* Ignore Confirm options other than during connection setup */
  682. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  683. return 0;
  684. dccp_feat_debug(type, feature, *val);
  685. /* locate our change request */
  686. switch (type) {
  687. case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
  688. case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
  689. default: DCCP_WARN("invalid type %d\n", type);
  690. return 1;
  691. }
  692. /* XXX sanity check feature value */
  693. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  694. if (!opt->dccpop_conf && opt->dccpop_type == t &&
  695. opt->dccpop_feat == feature) {
  696. found = 1;
  697. dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
  698. /* XXX do sanity check */
  699. opt->dccpop_conf = 1;
  700. /* We got a confirmation---change the option */
  701. dccp_feat_update(sk, opt->dccpop_type,
  702. opt->dccpop_feat, *val);
  703. /* XXX check the return value of dccp_feat_update */
  704. break;
  705. }
  706. if (!opt->dccpop_conf)
  707. all_confirmed = 0;
  708. }
  709. if (!found)
  710. dccp_pr_debug("%s(%d, ...) never requested\n",
  711. dccp_feat_typename(type), feature);
  712. return 0;
  713. }
  714. EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
  715. void dccp_feat_clean(struct dccp_minisock *dmsk)
  716. {
  717. struct dccp_opt_pend *opt, *next;
  718. list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
  719. dccpop_node) {
  720. BUG_ON(opt->dccpop_val == NULL);
  721. kfree(opt->dccpop_val);
  722. if (opt->dccpop_sc != NULL) {
  723. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  724. kfree(opt->dccpop_sc->dccpoc_val);
  725. kfree(opt->dccpop_sc);
  726. }
  727. kfree(opt);
  728. }
  729. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  730. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  731. BUG_ON(opt == NULL);
  732. if (opt->dccpop_val != NULL)
  733. kfree(opt->dccpop_val);
  734. kfree(opt);
  735. }
  736. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  737. }
  738. EXPORT_SYMBOL_GPL(dccp_feat_clean);
  739. /* this is to be called only when a listening sock creates its child. It is
  740. * assumed by the function---the confirm is not duplicated, but rather it is
  741. * "passed on".
  742. */
  743. int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
  744. {
  745. struct dccp_minisock *olddmsk = dccp_msk(oldsk);
  746. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  747. struct dccp_opt_pend *opt;
  748. int rc = 0;
  749. INIT_LIST_HEAD(&newdmsk->dccpms_pending);
  750. INIT_LIST_HEAD(&newdmsk->dccpms_conf);
  751. list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
  752. struct dccp_opt_pend *newopt;
  753. /* copy the value of the option */
  754. u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
  755. if (val == NULL)
  756. goto out_clean;
  757. newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
  758. if (newopt == NULL) {
  759. kfree(val);
  760. goto out_clean;
  761. }
  762. /* insert the option */
  763. newopt->dccpop_val = val;
  764. list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
  765. /* XXX what happens with backlogs and multiple connections at
  766. * once...
  767. */
  768. /* the master socket no longer needs to worry about confirms */
  769. opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
  770. /* reset state for a new socket */
  771. opt->dccpop_conf = 0;
  772. }
  773. /* XXX not doing anything about the conf queue */
  774. out:
  775. return rc;
  776. out_clean:
  777. dccp_feat_clean(newdmsk);
  778. rc = -ENOMEM;
  779. goto out;
  780. }
  781. EXPORT_SYMBOL_GPL(dccp_feat_clone);
  782. int dccp_feat_init(struct sock *sk)
  783. {
  784. struct dccp_sock *dp = dccp_sk(sk);
  785. struct dccp_minisock *dmsk = dccp_msk(sk);
  786. int rc;
  787. INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
  788. INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
  789. /* CCID L */
  790. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
  791. &dmsk->dccpms_tx_ccid, 1);
  792. if (rc)
  793. goto out;
  794. /* CCID R */
  795. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
  796. &dmsk->dccpms_rx_ccid, 1);
  797. if (rc)
  798. goto out;
  799. /* Ack ratio */
  800. rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
  801. dmsk->dccpms_ack_ratio);
  802. out:
  803. return rc;
  804. }
  805. EXPORT_SYMBOL_GPL(dccp_feat_init);
  806. #ifdef CONFIG_IP_DCCP_DEBUG
  807. const char *dccp_feat_typename(const u8 type)
  808. {
  809. switch(type) {
  810. case DCCPO_CHANGE_L: return("ChangeL");
  811. case DCCPO_CONFIRM_L: return("ConfirmL");
  812. case DCCPO_CHANGE_R: return("ChangeR");
  813. case DCCPO_CONFIRM_R: return("ConfirmR");
  814. /* the following case must not appear in feature negotation */
  815. default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
  816. }
  817. return NULL;
  818. }
  819. EXPORT_SYMBOL_GPL(dccp_feat_typename);
  820. const char *dccp_feat_name(const u8 feat)
  821. {
  822. static const char *feature_names[] = {
  823. [DCCPF_RESERVED] = "Reserved",
  824. [DCCPF_CCID] = "CCID",
  825. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  826. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  827. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  828. [DCCPF_ACK_RATIO] = "Ack Ratio",
  829. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  830. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  831. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  832. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  833. };
  834. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  835. return feature_names[DCCPF_RESERVED];
  836. if (feat == DCCPF_SEND_LEV_RATE)
  837. return "Send Loss Event Rate";
  838. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  839. return "CCID-specific";
  840. return feature_names[feat];
  841. }
  842. EXPORT_SYMBOL_GPL(dccp_feat_name);
  843. #endif /* CONFIG_IP_DCCP_DEBUG */