feat.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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 All currently known SP features have 1-byte quantities. If in the future
  10. * extensions of RFCs 4340..42 define features with item lengths larger than
  11. * one byte, a feature-specific extension of the code will be required.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/module.h>
  19. #include "ccid.h"
  20. #include "feat.h"
  21. #define DCCP_FEAT_SP_NOAGREE (-123)
  22. static const struct {
  23. u8 feat_num; /* DCCPF_xxx */
  24. enum dccp_feat_type rxtx; /* RX or TX */
  25. enum dccp_feat_type reconciliation; /* SP or NN */
  26. u8 default_value; /* as in 6.4 */
  27. /*
  28. * Lookup table for location and type of features (from RFC 4340/4342)
  29. * +--------------------------+----+-----+----+----+---------+-----------+
  30. * | Feature | Location | Reconc. | Initial | Section |
  31. * | | RX | TX | SP | NN | Value | Reference |
  32. * +--------------------------+----+-----+----+----+---------+-----------+
  33. * | DCCPF_CCID | | X | X | | 2 | 10 |
  34. * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
  35. * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
  36. * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
  37. * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
  38. * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
  39. * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
  40. * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
  41. * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
  42. * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
  43. * +--------------------------+----+-----+----+----+---------+-----------+
  44. */
  45. } dccp_feat_table[] = {
  46. { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2 },
  47. { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0 },
  48. { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100 },
  49. { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0 },
  50. { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2 },
  51. { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0 },
  52. { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0 },
  53. { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0 },
  54. { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0 },
  55. { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0 },
  56. };
  57. #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
  58. /**
  59. * dccp_feat_index - Hash function to map feature number into array position
  60. * Returns consecutive array index or -1 if the feature is not understood.
  61. */
  62. static int dccp_feat_index(u8 feat_num)
  63. {
  64. /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
  65. if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
  66. return feat_num - 1;
  67. /*
  68. * Other features: add cases for new feature types here after adding
  69. * them to the above table.
  70. */
  71. switch (feat_num) {
  72. case DCCPF_SEND_LEV_RATE:
  73. return DCCP_FEAT_SUPPORTED_MAX - 1;
  74. }
  75. return -1;
  76. }
  77. static u8 dccp_feat_type(u8 feat_num)
  78. {
  79. int idx = dccp_feat_index(feat_num);
  80. if (idx < 0)
  81. return FEAT_UNKNOWN;
  82. return dccp_feat_table[idx].reconciliation;
  83. }
  84. static int dccp_feat_default_value(u8 feat_num)
  85. {
  86. int idx = dccp_feat_index(feat_num);
  87. return idx < 0 ? : dccp_feat_table[idx].default_value;
  88. }
  89. /* copy constructor, fval must not already contain allocated memory */
  90. static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
  91. {
  92. fval->sp.len = len;
  93. if (fval->sp.len > 0) {
  94. fval->sp.vec = kmemdup(val, len, gfp_any());
  95. if (fval->sp.vec == NULL) {
  96. fval->sp.len = 0;
  97. return -ENOBUFS;
  98. }
  99. }
  100. return 0;
  101. }
  102. static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
  103. {
  104. if (unlikely(val == NULL))
  105. return;
  106. if (dccp_feat_type(feat_num) == FEAT_SP)
  107. kfree(val->sp.vec);
  108. memset(val, 0, sizeof(*val));
  109. }
  110. static struct dccp_feat_entry *
  111. dccp_feat_clone_entry(struct dccp_feat_entry const *original)
  112. {
  113. struct dccp_feat_entry *new;
  114. u8 type = dccp_feat_type(original->feat_num);
  115. if (type == FEAT_UNKNOWN)
  116. return NULL;
  117. new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
  118. if (new == NULL)
  119. return NULL;
  120. if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
  121. original->val.sp.vec,
  122. original->val.sp.len)) {
  123. kfree(new);
  124. return NULL;
  125. }
  126. return new;
  127. }
  128. static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
  129. {
  130. if (entry != NULL) {
  131. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  132. kfree(entry);
  133. }
  134. }
  135. int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
  136. u8 *val, u8 len, gfp_t gfp)
  137. {
  138. struct dccp_opt_pend *opt;
  139. dccp_feat_debug(type, feature, *val);
  140. if (len > 3) {
  141. DCCP_WARN("invalid length %d\n", len);
  142. return -EINVAL;
  143. }
  144. /* XXX add further sanity checks */
  145. /* check if that feature is already being negotiated */
  146. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  147. /* ok we found a negotiation for this option already */
  148. if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
  149. dccp_pr_debug("Replacing old\n");
  150. /* replace */
  151. BUG_ON(opt->dccpop_val == NULL);
  152. kfree(opt->dccpop_val);
  153. opt->dccpop_val = val;
  154. opt->dccpop_len = len;
  155. opt->dccpop_conf = 0;
  156. return 0;
  157. }
  158. }
  159. /* negotiation for a new feature */
  160. opt = kmalloc(sizeof(*opt), gfp);
  161. if (opt == NULL)
  162. return -ENOMEM;
  163. opt->dccpop_type = type;
  164. opt->dccpop_feat = feature;
  165. opt->dccpop_len = len;
  166. opt->dccpop_val = val;
  167. opt->dccpop_conf = 0;
  168. opt->dccpop_sc = NULL;
  169. BUG_ON(opt->dccpop_val == NULL);
  170. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
  171. return 0;
  172. }
  173. EXPORT_SYMBOL_GPL(dccp_feat_change);
  174. static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
  175. {
  176. struct dccp_sock *dp = dccp_sk(sk);
  177. struct dccp_minisock *dmsk = dccp_msk(sk);
  178. /* figure out if we are changing our CCID or the peer's */
  179. const int rx = type == DCCPO_CHANGE_R;
  180. const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
  181. struct ccid *new_ccid;
  182. /* Check if nothing is being changed. */
  183. if (ccid_nr == new_ccid_nr)
  184. return 0;
  185. new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
  186. if (new_ccid == NULL)
  187. return -ENOMEM;
  188. if (rx) {
  189. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  190. dp->dccps_hc_rx_ccid = new_ccid;
  191. dmsk->dccpms_rx_ccid = new_ccid_nr;
  192. } else {
  193. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  194. dp->dccps_hc_tx_ccid = new_ccid;
  195. dmsk->dccpms_tx_ccid = new_ccid_nr;
  196. }
  197. return 0;
  198. }
  199. static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
  200. {
  201. dccp_feat_debug(type, feat, val);
  202. switch (feat) {
  203. case DCCPF_CCID:
  204. return dccp_feat_update_ccid(sk, type, val);
  205. default:
  206. dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
  207. dccp_feat_typename(type), feat);
  208. break;
  209. }
  210. return 0;
  211. }
  212. static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
  213. u8 *rpref, u8 rlen)
  214. {
  215. struct dccp_sock *dp = dccp_sk(sk);
  216. u8 *spref, slen, *res = NULL;
  217. int i, j, rc, agree = 1;
  218. BUG_ON(rpref == NULL);
  219. /* check if we are the black sheep */
  220. if (dp->dccps_role == DCCP_ROLE_CLIENT) {
  221. spref = rpref;
  222. slen = rlen;
  223. rpref = opt->dccpop_val;
  224. rlen = opt->dccpop_len;
  225. } else {
  226. spref = opt->dccpop_val;
  227. slen = opt->dccpop_len;
  228. }
  229. /*
  230. * Now we have server preference list in spref and client preference in
  231. * rpref
  232. */
  233. BUG_ON(spref == NULL);
  234. BUG_ON(rpref == NULL);
  235. /* FIXME sanity check vals */
  236. /* Are values in any order? XXX Lame "algorithm" here */
  237. for (i = 0; i < slen; i++) {
  238. for (j = 0; j < rlen; j++) {
  239. if (spref[i] == rpref[j]) {
  240. res = &spref[i];
  241. break;
  242. }
  243. }
  244. if (res)
  245. break;
  246. }
  247. /* we didn't agree on anything */
  248. if (res == NULL) {
  249. /* confirm previous value */
  250. switch (opt->dccpop_feat) {
  251. case DCCPF_CCID:
  252. /* XXX did i get this right? =P */
  253. if (opt->dccpop_type == DCCPO_CHANGE_L)
  254. res = &dccp_msk(sk)->dccpms_tx_ccid;
  255. else
  256. res = &dccp_msk(sk)->dccpms_rx_ccid;
  257. break;
  258. default:
  259. DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
  260. /* XXX implement res */
  261. return -EFAULT;
  262. }
  263. dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
  264. agree = 0; /* this is used for mandatory options... */
  265. }
  266. /* need to put result and our preference list */
  267. rlen = 1 + opt->dccpop_len;
  268. rpref = kmalloc(rlen, GFP_ATOMIC);
  269. if (rpref == NULL)
  270. return -ENOMEM;
  271. *rpref = *res;
  272. memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
  273. /* put it in the "confirm queue" */
  274. if (opt->dccpop_sc == NULL) {
  275. opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
  276. if (opt->dccpop_sc == NULL) {
  277. kfree(rpref);
  278. return -ENOMEM;
  279. }
  280. } else {
  281. /* recycle the confirm slot */
  282. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  283. kfree(opt->dccpop_sc->dccpoc_val);
  284. dccp_pr_debug("recycling confirm slot\n");
  285. }
  286. memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
  287. opt->dccpop_sc->dccpoc_val = rpref;
  288. opt->dccpop_sc->dccpoc_len = rlen;
  289. /* update the option on our side [we are about to send the confirm] */
  290. rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
  291. if (rc) {
  292. kfree(opt->dccpop_sc->dccpoc_val);
  293. kfree(opt->dccpop_sc);
  294. opt->dccpop_sc = NULL;
  295. return rc;
  296. }
  297. dccp_pr_debug("Will confirm %d\n", *rpref);
  298. /* say we want to change to X but we just got a confirm X, suppress our
  299. * change
  300. */
  301. if (!opt->dccpop_conf) {
  302. if (*opt->dccpop_val == *res)
  303. opt->dccpop_conf = 1;
  304. dccp_pr_debug("won't ask for change of same feature\n");
  305. }
  306. return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
  307. }
  308. static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  309. {
  310. struct dccp_minisock *dmsk = dccp_msk(sk);
  311. struct dccp_opt_pend *opt;
  312. int rc = 1;
  313. u8 t;
  314. /*
  315. * We received a CHANGE. We gotta match it against our own preference
  316. * list. If we got a CHANGE_R it means it's a change for us, so we need
  317. * to compare our CHANGE_L list.
  318. */
  319. if (type == DCCPO_CHANGE_L)
  320. t = DCCPO_CHANGE_R;
  321. else
  322. t = DCCPO_CHANGE_L;
  323. /* find our preference list for this feature */
  324. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  325. if (opt->dccpop_type != t || opt->dccpop_feat != feature)
  326. continue;
  327. /* find the winner from the two preference lists */
  328. rc = dccp_feat_reconcile(sk, opt, val, len);
  329. break;
  330. }
  331. /* We didn't deal with the change. This can happen if we have no
  332. * preference list for the feature. In fact, it just shouldn't
  333. * happen---if we understand a feature, we should have a preference list
  334. * with at least the default value.
  335. */
  336. BUG_ON(rc == 1);
  337. return rc;
  338. }
  339. static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  340. {
  341. struct dccp_opt_pend *opt;
  342. struct dccp_minisock *dmsk = dccp_msk(sk);
  343. u8 *copy;
  344. int rc;
  345. /* NN features must be Change L (sec. 6.3.2) */
  346. if (type != DCCPO_CHANGE_L) {
  347. dccp_pr_debug("received %s for NN feature %d\n",
  348. dccp_feat_typename(type), feature);
  349. return -EFAULT;
  350. }
  351. /* XXX sanity check opt val */
  352. /* copy option so we can confirm it */
  353. opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  354. if (opt == NULL)
  355. return -ENOMEM;
  356. copy = kmemdup(val, len, GFP_ATOMIC);
  357. if (copy == NULL) {
  358. kfree(opt);
  359. return -ENOMEM;
  360. }
  361. opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
  362. opt->dccpop_feat = feature;
  363. opt->dccpop_val = copy;
  364. opt->dccpop_len = len;
  365. /* change feature */
  366. rc = dccp_feat_update(sk, type, feature, *val);
  367. if (rc) {
  368. kfree(opt->dccpop_val);
  369. kfree(opt);
  370. return rc;
  371. }
  372. dccp_feat_debug(type, feature, *copy);
  373. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  374. return 0;
  375. }
  376. static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
  377. u8 type, u8 feature)
  378. {
  379. /* XXX check if other confirms for that are queued and recycle slot */
  380. struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  381. if (opt == NULL) {
  382. /* XXX what do we do? Ignoring should be fine. It's a change
  383. * after all =P
  384. */
  385. return;
  386. }
  387. switch (type) {
  388. case DCCPO_CHANGE_L:
  389. opt->dccpop_type = DCCPO_CONFIRM_R;
  390. break;
  391. case DCCPO_CHANGE_R:
  392. opt->dccpop_type = DCCPO_CONFIRM_L;
  393. break;
  394. default:
  395. DCCP_WARN("invalid type %d\n", type);
  396. kfree(opt);
  397. return;
  398. }
  399. opt->dccpop_feat = feature;
  400. opt->dccpop_val = NULL;
  401. opt->dccpop_len = 0;
  402. /* change feature */
  403. dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
  404. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  405. }
  406. static void dccp_feat_flush_confirm(struct sock *sk)
  407. {
  408. struct dccp_minisock *dmsk = dccp_msk(sk);
  409. /* Check if there is anything to confirm in the first place */
  410. int yes = !list_empty(&dmsk->dccpms_conf);
  411. if (!yes) {
  412. struct dccp_opt_pend *opt;
  413. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  414. if (opt->dccpop_conf) {
  415. yes = 1;
  416. break;
  417. }
  418. }
  419. }
  420. if (!yes)
  421. return;
  422. /* OK there is something to confirm... */
  423. /* XXX check if packet is in flight? Send delayed ack?? */
  424. if (sk->sk_state == DCCP_OPEN)
  425. dccp_send_ack(sk);
  426. }
  427. int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  428. {
  429. int rc;
  430. dccp_feat_debug(type, feature, *val);
  431. /* figure out if it's SP or NN feature */
  432. switch (feature) {
  433. /* deal with SP features */
  434. case DCCPF_CCID:
  435. rc = dccp_feat_sp(sk, type, feature, val, len);
  436. break;
  437. /* deal with NN features */
  438. case DCCPF_ACK_RATIO:
  439. rc = dccp_feat_nn(sk, type, feature, val, len);
  440. break;
  441. /* XXX implement other features */
  442. default:
  443. dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
  444. dccp_feat_typename(type), feature);
  445. rc = -EFAULT;
  446. break;
  447. }
  448. /* check if there were problems changing features */
  449. if (rc) {
  450. /* If we don't agree on SP, we sent a confirm for old value.
  451. * However we propagate rc to caller in case option was
  452. * mandatory
  453. */
  454. if (rc != DCCP_FEAT_SP_NOAGREE)
  455. dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
  456. }
  457. /* generate the confirm [if required] */
  458. dccp_feat_flush_confirm(sk);
  459. return rc;
  460. }
  461. EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
  462. int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
  463. u8 *val, u8 len)
  464. {
  465. u8 t;
  466. struct dccp_opt_pend *opt;
  467. struct dccp_minisock *dmsk = dccp_msk(sk);
  468. int found = 0;
  469. int all_confirmed = 1;
  470. dccp_feat_debug(type, feature, *val);
  471. /* locate our change request */
  472. switch (type) {
  473. case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
  474. case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
  475. default: DCCP_WARN("invalid type %d\n", type);
  476. return 1;
  477. }
  478. /* XXX sanity check feature value */
  479. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  480. if (!opt->dccpop_conf && opt->dccpop_type == t &&
  481. opt->dccpop_feat == feature) {
  482. found = 1;
  483. dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
  484. /* XXX do sanity check */
  485. opt->dccpop_conf = 1;
  486. /* We got a confirmation---change the option */
  487. dccp_feat_update(sk, opt->dccpop_type,
  488. opt->dccpop_feat, *val);
  489. /* XXX check the return value of dccp_feat_update */
  490. break;
  491. }
  492. if (!opt->dccpop_conf)
  493. all_confirmed = 0;
  494. }
  495. /* fix re-transmit timer */
  496. /* XXX gotta make sure that no option negotiation occurs during
  497. * connection shutdown. Consider that the CLOSEREQ is sent and timer is
  498. * on. if all options are confirmed it might kill timer which should
  499. * remain alive until close is received.
  500. */
  501. if (all_confirmed) {
  502. dccp_pr_debug("clear feat negotiation timer %p\n", sk);
  503. inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
  504. }
  505. if (!found)
  506. dccp_pr_debug("%s(%d, ...) never requested\n",
  507. dccp_feat_typename(type), feature);
  508. return 0;
  509. }
  510. EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
  511. void dccp_feat_clean(struct dccp_minisock *dmsk)
  512. {
  513. struct dccp_opt_pend *opt, *next;
  514. list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
  515. dccpop_node) {
  516. BUG_ON(opt->dccpop_val == NULL);
  517. kfree(opt->dccpop_val);
  518. if (opt->dccpop_sc != NULL) {
  519. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  520. kfree(opt->dccpop_sc->dccpoc_val);
  521. kfree(opt->dccpop_sc);
  522. }
  523. kfree(opt);
  524. }
  525. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  526. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  527. BUG_ON(opt == NULL);
  528. if (opt->dccpop_val != NULL)
  529. kfree(opt->dccpop_val);
  530. kfree(opt);
  531. }
  532. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  533. }
  534. EXPORT_SYMBOL_GPL(dccp_feat_clean);
  535. /* this is to be called only when a listening sock creates its child. It is
  536. * assumed by the function---the confirm is not duplicated, but rather it is
  537. * "passed on".
  538. */
  539. int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
  540. {
  541. struct dccp_minisock *olddmsk = dccp_msk(oldsk);
  542. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  543. struct dccp_opt_pend *opt;
  544. int rc = 0;
  545. INIT_LIST_HEAD(&newdmsk->dccpms_pending);
  546. INIT_LIST_HEAD(&newdmsk->dccpms_conf);
  547. list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
  548. struct dccp_opt_pend *newopt;
  549. /* copy the value of the option */
  550. u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
  551. if (val == NULL)
  552. goto out_clean;
  553. newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
  554. if (newopt == NULL) {
  555. kfree(val);
  556. goto out_clean;
  557. }
  558. /* insert the option */
  559. newopt->dccpop_val = val;
  560. list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
  561. /* XXX what happens with backlogs and multiple connections at
  562. * once...
  563. */
  564. /* the master socket no longer needs to worry about confirms */
  565. opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
  566. /* reset state for a new socket */
  567. opt->dccpop_conf = 0;
  568. }
  569. /* XXX not doing anything about the conf queue */
  570. out:
  571. return rc;
  572. out_clean:
  573. dccp_feat_clean(newdmsk);
  574. rc = -ENOMEM;
  575. goto out;
  576. }
  577. EXPORT_SYMBOL_GPL(dccp_feat_clone);
  578. static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
  579. u8 *val, u8 len)
  580. {
  581. int rc = -ENOMEM;
  582. u8 *copy = kmemdup(val, len, GFP_KERNEL);
  583. if (copy != NULL) {
  584. rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
  585. if (rc)
  586. kfree(copy);
  587. }
  588. return rc;
  589. }
  590. int dccp_feat_init(struct dccp_minisock *dmsk)
  591. {
  592. int rc;
  593. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  594. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  595. /* CCID L */
  596. rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
  597. &dmsk->dccpms_tx_ccid, 1);
  598. if (rc)
  599. goto out;
  600. /* CCID R */
  601. rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
  602. &dmsk->dccpms_rx_ccid, 1);
  603. if (rc)
  604. goto out;
  605. /* Ack ratio */
  606. rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
  607. &dmsk->dccpms_ack_ratio, 1);
  608. out:
  609. return rc;
  610. }
  611. EXPORT_SYMBOL_GPL(dccp_feat_init);
  612. #ifdef CONFIG_IP_DCCP_DEBUG
  613. const char *dccp_feat_typename(const u8 type)
  614. {
  615. switch(type) {
  616. case DCCPO_CHANGE_L: return("ChangeL");
  617. case DCCPO_CONFIRM_L: return("ConfirmL");
  618. case DCCPO_CHANGE_R: return("ChangeR");
  619. case DCCPO_CONFIRM_R: return("ConfirmR");
  620. /* the following case must not appear in feature negotation */
  621. default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
  622. }
  623. return NULL;
  624. }
  625. EXPORT_SYMBOL_GPL(dccp_feat_typename);
  626. const char *dccp_feat_name(const u8 feat)
  627. {
  628. static const char *feature_names[] = {
  629. [DCCPF_RESERVED] = "Reserved",
  630. [DCCPF_CCID] = "CCID",
  631. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  632. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  633. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  634. [DCCPF_ACK_RATIO] = "Ack Ratio",
  635. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  636. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  637. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  638. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  639. };
  640. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  641. return feature_names[DCCPF_RESERVED];
  642. if (feat == DCCPF_SEND_LEV_RATE)
  643. return "Send Loss Event Rate";
  644. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  645. return "CCID-specific";
  646. return feature_names[feat];
  647. }
  648. EXPORT_SYMBOL_GPL(dccp_feat_name);
  649. #endif /* CONFIG_IP_DCCP_DEBUG */