feat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. /*
  136. * List management functions
  137. *
  138. * Feature negotiation lists rely on and maintain the following invariants:
  139. * - each feat_num in the list is known, i.e. we know its type and default value
  140. * - each feat_num/is_local combination is unique (old entries are overwritten)
  141. * - SP values are always freshly allocated
  142. * - list is sorted in increasing order of feature number (faster lookup)
  143. */
  144. static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
  145. u8 feat_num, bool is_local)
  146. {
  147. struct dccp_feat_entry *entry;
  148. list_for_each_entry(entry, fn_list, node)
  149. if (entry->feat_num == feat_num && entry->is_local == is_local)
  150. return entry;
  151. else if (entry->feat_num > feat_num)
  152. break;
  153. return NULL;
  154. }
  155. /**
  156. * dccp_feat_entry_new - Central list update routine (called by all others)
  157. * @head: list to add to
  158. * @feat: feature number
  159. * @local: whether the local (1) or remote feature with number @feat is meant
  160. * This is the only constructor and serves to ensure the above invariants.
  161. */
  162. static struct dccp_feat_entry *
  163. dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
  164. {
  165. struct dccp_feat_entry *entry;
  166. list_for_each_entry(entry, head, node)
  167. if (entry->feat_num == feat && entry->is_local == local) {
  168. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  169. return entry;
  170. } else if (entry->feat_num > feat) {
  171. head = &entry->node;
  172. break;
  173. }
  174. entry = kmalloc(sizeof(*entry), gfp_any());
  175. if (entry != NULL) {
  176. entry->feat_num = feat;
  177. entry->is_local = local;
  178. list_add_tail(&entry->node, head);
  179. }
  180. return entry;
  181. }
  182. /**
  183. * dccp_feat_push_change - Add/overwrite a Change option in the list
  184. * @fn_list: feature-negotiation list to update
  185. * @feat: one of %dccp_feature_numbers
  186. * @local: whether local (1) or remote (0) @feat_num is meant
  187. * @needs_mandatory: whether to use Mandatory feature negotiation options
  188. * @fval: pointer to NN/SP value to be inserted (will be copied)
  189. */
  190. static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
  191. u8 mandatory, dccp_feat_val *fval)
  192. {
  193. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  194. if (new == NULL)
  195. return -ENOMEM;
  196. new->feat_num = feat;
  197. new->is_local = local;
  198. new->state = FEAT_INITIALISING;
  199. new->needs_confirm = 0;
  200. new->empty_confirm = 0;
  201. new->val = *fval;
  202. new->needs_mandatory = mandatory;
  203. return 0;
  204. }
  205. /**
  206. * dccp_feat_push_confirm - Add a Confirm entry to the FN list
  207. * @fn_list: feature-negotiation list to add to
  208. * @feat: one of %dccp_feature_numbers
  209. * @local: whether local (1) or remote (0) @feat_num is being confirmed
  210. * @fval: pointer to NN/SP value to be inserted or NULL
  211. * Returns 0 on success, a Reset code for further processing otherwise.
  212. */
  213. static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
  214. dccp_feat_val *fval)
  215. {
  216. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  217. if (new == NULL)
  218. return DCCP_RESET_CODE_TOO_BUSY;
  219. new->feat_num = feat;
  220. new->is_local = local;
  221. new->state = FEAT_STABLE; /* transition in 6.6.2 */
  222. new->needs_confirm = 1;
  223. new->empty_confirm = (fval == NULL);
  224. new->val.nn = 0; /* zeroes the whole structure */
  225. if (!new->empty_confirm)
  226. new->val = *fval;
  227. new->needs_mandatory = 0;
  228. return 0;
  229. }
  230. static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
  231. {
  232. return dccp_feat_push_confirm(fn_list, feat, local, NULL);
  233. }
  234. static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
  235. {
  236. list_del(&entry->node);
  237. dccp_feat_entry_destructor(entry);
  238. }
  239. void dccp_feat_list_purge(struct list_head *fn_list)
  240. {
  241. struct dccp_feat_entry *entry, *next;
  242. list_for_each_entry_safe(entry, next, fn_list, node)
  243. dccp_feat_entry_destructor(entry);
  244. INIT_LIST_HEAD(fn_list);
  245. }
  246. EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
  247. int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
  248. u8 *val, u8 len, gfp_t gfp)
  249. {
  250. struct dccp_opt_pend *opt;
  251. dccp_feat_debug(type, feature, *val);
  252. if (len > 3) {
  253. DCCP_WARN("invalid length %d\n", len);
  254. return -EINVAL;
  255. }
  256. /* XXX add further sanity checks */
  257. /* check if that feature is already being negotiated */
  258. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  259. /* ok we found a negotiation for this option already */
  260. if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
  261. dccp_pr_debug("Replacing old\n");
  262. /* replace */
  263. BUG_ON(opt->dccpop_val == NULL);
  264. kfree(opt->dccpop_val);
  265. opt->dccpop_val = val;
  266. opt->dccpop_len = len;
  267. opt->dccpop_conf = 0;
  268. return 0;
  269. }
  270. }
  271. /* negotiation for a new feature */
  272. opt = kmalloc(sizeof(*opt), gfp);
  273. if (opt == NULL)
  274. return -ENOMEM;
  275. opt->dccpop_type = type;
  276. opt->dccpop_feat = feature;
  277. opt->dccpop_len = len;
  278. opt->dccpop_val = val;
  279. opt->dccpop_conf = 0;
  280. opt->dccpop_sc = NULL;
  281. BUG_ON(opt->dccpop_val == NULL);
  282. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
  283. return 0;
  284. }
  285. EXPORT_SYMBOL_GPL(dccp_feat_change);
  286. static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
  287. {
  288. struct dccp_sock *dp = dccp_sk(sk);
  289. struct dccp_minisock *dmsk = dccp_msk(sk);
  290. /* figure out if we are changing our CCID or the peer's */
  291. const int rx = type == DCCPO_CHANGE_R;
  292. const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
  293. struct ccid *new_ccid;
  294. /* Check if nothing is being changed. */
  295. if (ccid_nr == new_ccid_nr)
  296. return 0;
  297. new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
  298. if (new_ccid == NULL)
  299. return -ENOMEM;
  300. if (rx) {
  301. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  302. dp->dccps_hc_rx_ccid = new_ccid;
  303. dmsk->dccpms_rx_ccid = new_ccid_nr;
  304. } else {
  305. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  306. dp->dccps_hc_tx_ccid = new_ccid;
  307. dmsk->dccpms_tx_ccid = new_ccid_nr;
  308. }
  309. return 0;
  310. }
  311. static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
  312. {
  313. dccp_feat_debug(type, feat, val);
  314. switch (feat) {
  315. case DCCPF_CCID:
  316. return dccp_feat_update_ccid(sk, type, val);
  317. default:
  318. dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
  319. dccp_feat_typename(type), feat);
  320. break;
  321. }
  322. return 0;
  323. }
  324. static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
  325. u8 *rpref, u8 rlen)
  326. {
  327. struct dccp_sock *dp = dccp_sk(sk);
  328. u8 *spref, slen, *res = NULL;
  329. int i, j, rc, agree = 1;
  330. BUG_ON(rpref == NULL);
  331. /* check if we are the black sheep */
  332. if (dp->dccps_role == DCCP_ROLE_CLIENT) {
  333. spref = rpref;
  334. slen = rlen;
  335. rpref = opt->dccpop_val;
  336. rlen = opt->dccpop_len;
  337. } else {
  338. spref = opt->dccpop_val;
  339. slen = opt->dccpop_len;
  340. }
  341. /*
  342. * Now we have server preference list in spref and client preference in
  343. * rpref
  344. */
  345. BUG_ON(spref == NULL);
  346. BUG_ON(rpref == NULL);
  347. /* FIXME sanity check vals */
  348. /* Are values in any order? XXX Lame "algorithm" here */
  349. for (i = 0; i < slen; i++) {
  350. for (j = 0; j < rlen; j++) {
  351. if (spref[i] == rpref[j]) {
  352. res = &spref[i];
  353. break;
  354. }
  355. }
  356. if (res)
  357. break;
  358. }
  359. /* we didn't agree on anything */
  360. if (res == NULL) {
  361. /* confirm previous value */
  362. switch (opt->dccpop_feat) {
  363. case DCCPF_CCID:
  364. /* XXX did i get this right? =P */
  365. if (opt->dccpop_type == DCCPO_CHANGE_L)
  366. res = &dccp_msk(sk)->dccpms_tx_ccid;
  367. else
  368. res = &dccp_msk(sk)->dccpms_rx_ccid;
  369. break;
  370. default:
  371. DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
  372. /* XXX implement res */
  373. return -EFAULT;
  374. }
  375. dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
  376. agree = 0; /* this is used for mandatory options... */
  377. }
  378. /* need to put result and our preference list */
  379. rlen = 1 + opt->dccpop_len;
  380. rpref = kmalloc(rlen, GFP_ATOMIC);
  381. if (rpref == NULL)
  382. return -ENOMEM;
  383. *rpref = *res;
  384. memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
  385. /* put it in the "confirm queue" */
  386. if (opt->dccpop_sc == NULL) {
  387. opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
  388. if (opt->dccpop_sc == NULL) {
  389. kfree(rpref);
  390. return -ENOMEM;
  391. }
  392. } else {
  393. /* recycle the confirm slot */
  394. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  395. kfree(opt->dccpop_sc->dccpoc_val);
  396. dccp_pr_debug("recycling confirm slot\n");
  397. }
  398. memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
  399. opt->dccpop_sc->dccpoc_val = rpref;
  400. opt->dccpop_sc->dccpoc_len = rlen;
  401. /* update the option on our side [we are about to send the confirm] */
  402. rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
  403. if (rc) {
  404. kfree(opt->dccpop_sc->dccpoc_val);
  405. kfree(opt->dccpop_sc);
  406. opt->dccpop_sc = NULL;
  407. return rc;
  408. }
  409. dccp_pr_debug("Will confirm %d\n", *rpref);
  410. /* say we want to change to X but we just got a confirm X, suppress our
  411. * change
  412. */
  413. if (!opt->dccpop_conf) {
  414. if (*opt->dccpop_val == *res)
  415. opt->dccpop_conf = 1;
  416. dccp_pr_debug("won't ask for change of same feature\n");
  417. }
  418. return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
  419. }
  420. static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  421. {
  422. struct dccp_minisock *dmsk = dccp_msk(sk);
  423. struct dccp_opt_pend *opt;
  424. int rc = 1;
  425. u8 t;
  426. /*
  427. * We received a CHANGE. We gotta match it against our own preference
  428. * list. If we got a CHANGE_R it means it's a change for us, so we need
  429. * to compare our CHANGE_L list.
  430. */
  431. if (type == DCCPO_CHANGE_L)
  432. t = DCCPO_CHANGE_R;
  433. else
  434. t = DCCPO_CHANGE_L;
  435. /* find our preference list for this feature */
  436. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  437. if (opt->dccpop_type != t || opt->dccpop_feat != feature)
  438. continue;
  439. /* find the winner from the two preference lists */
  440. rc = dccp_feat_reconcile(sk, opt, val, len);
  441. break;
  442. }
  443. /* We didn't deal with the change. This can happen if we have no
  444. * preference list for the feature. In fact, it just shouldn't
  445. * happen---if we understand a feature, we should have a preference list
  446. * with at least the default value.
  447. */
  448. BUG_ON(rc == 1);
  449. return rc;
  450. }
  451. static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  452. {
  453. struct dccp_opt_pend *opt;
  454. struct dccp_minisock *dmsk = dccp_msk(sk);
  455. u8 *copy;
  456. int rc;
  457. /* NN features must be Change L (sec. 6.3.2) */
  458. if (type != DCCPO_CHANGE_L) {
  459. dccp_pr_debug("received %s for NN feature %d\n",
  460. dccp_feat_typename(type), feature);
  461. return -EFAULT;
  462. }
  463. /* XXX sanity check opt val */
  464. /* copy option so we can confirm it */
  465. opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  466. if (opt == NULL)
  467. return -ENOMEM;
  468. copy = kmemdup(val, len, GFP_ATOMIC);
  469. if (copy == NULL) {
  470. kfree(opt);
  471. return -ENOMEM;
  472. }
  473. opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
  474. opt->dccpop_feat = feature;
  475. opt->dccpop_val = copy;
  476. opt->dccpop_len = len;
  477. /* change feature */
  478. rc = dccp_feat_update(sk, type, feature, *val);
  479. if (rc) {
  480. kfree(opt->dccpop_val);
  481. kfree(opt);
  482. return rc;
  483. }
  484. dccp_feat_debug(type, feature, *copy);
  485. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  486. return 0;
  487. }
  488. static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
  489. u8 type, u8 feature)
  490. {
  491. /* XXX check if other confirms for that are queued and recycle slot */
  492. struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  493. if (opt == NULL) {
  494. /* XXX what do we do? Ignoring should be fine. It's a change
  495. * after all =P
  496. */
  497. return;
  498. }
  499. switch (type) {
  500. case DCCPO_CHANGE_L:
  501. opt->dccpop_type = DCCPO_CONFIRM_R;
  502. break;
  503. case DCCPO_CHANGE_R:
  504. opt->dccpop_type = DCCPO_CONFIRM_L;
  505. break;
  506. default:
  507. DCCP_WARN("invalid type %d\n", type);
  508. kfree(opt);
  509. return;
  510. }
  511. opt->dccpop_feat = feature;
  512. opt->dccpop_val = NULL;
  513. opt->dccpop_len = 0;
  514. /* change feature */
  515. dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
  516. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  517. }
  518. static void dccp_feat_flush_confirm(struct sock *sk)
  519. {
  520. struct dccp_minisock *dmsk = dccp_msk(sk);
  521. /* Check if there is anything to confirm in the first place */
  522. int yes = !list_empty(&dmsk->dccpms_conf);
  523. if (!yes) {
  524. struct dccp_opt_pend *opt;
  525. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  526. if (opt->dccpop_conf) {
  527. yes = 1;
  528. break;
  529. }
  530. }
  531. }
  532. if (!yes)
  533. return;
  534. /* OK there is something to confirm... */
  535. /* XXX check if packet is in flight? Send delayed ack?? */
  536. if (sk->sk_state == DCCP_OPEN)
  537. dccp_send_ack(sk);
  538. }
  539. int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  540. {
  541. int rc;
  542. dccp_feat_debug(type, feature, *val);
  543. /* figure out if it's SP or NN feature */
  544. switch (feature) {
  545. /* deal with SP features */
  546. case DCCPF_CCID:
  547. rc = dccp_feat_sp(sk, type, feature, val, len);
  548. break;
  549. /* deal with NN features */
  550. case DCCPF_ACK_RATIO:
  551. rc = dccp_feat_nn(sk, type, feature, val, len);
  552. break;
  553. /* XXX implement other features */
  554. default:
  555. dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
  556. dccp_feat_typename(type), feature);
  557. rc = -EFAULT;
  558. break;
  559. }
  560. /* check if there were problems changing features */
  561. if (rc) {
  562. /* If we don't agree on SP, we sent a confirm for old value.
  563. * However we propagate rc to caller in case option was
  564. * mandatory
  565. */
  566. if (rc != DCCP_FEAT_SP_NOAGREE)
  567. dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
  568. }
  569. /* generate the confirm [if required] */
  570. dccp_feat_flush_confirm(sk);
  571. return rc;
  572. }
  573. EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
  574. int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
  575. u8 *val, u8 len)
  576. {
  577. u8 t;
  578. struct dccp_opt_pend *opt;
  579. struct dccp_minisock *dmsk = dccp_msk(sk);
  580. int found = 0;
  581. int all_confirmed = 1;
  582. dccp_feat_debug(type, feature, *val);
  583. /* locate our change request */
  584. switch (type) {
  585. case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
  586. case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
  587. default: DCCP_WARN("invalid type %d\n", type);
  588. return 1;
  589. }
  590. /* XXX sanity check feature value */
  591. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  592. if (!opt->dccpop_conf && opt->dccpop_type == t &&
  593. opt->dccpop_feat == feature) {
  594. found = 1;
  595. dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
  596. /* XXX do sanity check */
  597. opt->dccpop_conf = 1;
  598. /* We got a confirmation---change the option */
  599. dccp_feat_update(sk, opt->dccpop_type,
  600. opt->dccpop_feat, *val);
  601. /* XXX check the return value of dccp_feat_update */
  602. break;
  603. }
  604. if (!opt->dccpop_conf)
  605. all_confirmed = 0;
  606. }
  607. /* fix re-transmit timer */
  608. /* XXX gotta make sure that no option negotiation occurs during
  609. * connection shutdown. Consider that the CLOSEREQ is sent and timer is
  610. * on. if all options are confirmed it might kill timer which should
  611. * remain alive until close is received.
  612. */
  613. if (all_confirmed) {
  614. dccp_pr_debug("clear feat negotiation timer %p\n", sk);
  615. inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
  616. }
  617. if (!found)
  618. dccp_pr_debug("%s(%d, ...) never requested\n",
  619. dccp_feat_typename(type), feature);
  620. return 0;
  621. }
  622. EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
  623. void dccp_feat_clean(struct dccp_minisock *dmsk)
  624. {
  625. struct dccp_opt_pend *opt, *next;
  626. list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
  627. dccpop_node) {
  628. BUG_ON(opt->dccpop_val == NULL);
  629. kfree(opt->dccpop_val);
  630. if (opt->dccpop_sc != NULL) {
  631. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  632. kfree(opt->dccpop_sc->dccpoc_val);
  633. kfree(opt->dccpop_sc);
  634. }
  635. kfree(opt);
  636. }
  637. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  638. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  639. BUG_ON(opt == NULL);
  640. if (opt->dccpop_val != NULL)
  641. kfree(opt->dccpop_val);
  642. kfree(opt);
  643. }
  644. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  645. }
  646. EXPORT_SYMBOL_GPL(dccp_feat_clean);
  647. /* this is to be called only when a listening sock creates its child. It is
  648. * assumed by the function---the confirm is not duplicated, but rather it is
  649. * "passed on".
  650. */
  651. int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
  652. {
  653. struct dccp_minisock *olddmsk = dccp_msk(oldsk);
  654. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  655. struct dccp_opt_pend *opt;
  656. int rc = 0;
  657. INIT_LIST_HEAD(&newdmsk->dccpms_pending);
  658. INIT_LIST_HEAD(&newdmsk->dccpms_conf);
  659. list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
  660. struct dccp_opt_pend *newopt;
  661. /* copy the value of the option */
  662. u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
  663. if (val == NULL)
  664. goto out_clean;
  665. newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
  666. if (newopt == NULL) {
  667. kfree(val);
  668. goto out_clean;
  669. }
  670. /* insert the option */
  671. newopt->dccpop_val = val;
  672. list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
  673. /* XXX what happens with backlogs and multiple connections at
  674. * once...
  675. */
  676. /* the master socket no longer needs to worry about confirms */
  677. opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
  678. /* reset state for a new socket */
  679. opt->dccpop_conf = 0;
  680. }
  681. /* XXX not doing anything about the conf queue */
  682. out:
  683. return rc;
  684. out_clean:
  685. dccp_feat_clean(newdmsk);
  686. rc = -ENOMEM;
  687. goto out;
  688. }
  689. EXPORT_SYMBOL_GPL(dccp_feat_clone);
  690. static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
  691. u8 *val, u8 len)
  692. {
  693. int rc = -ENOMEM;
  694. u8 *copy = kmemdup(val, len, GFP_KERNEL);
  695. if (copy != NULL) {
  696. rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
  697. if (rc)
  698. kfree(copy);
  699. }
  700. return rc;
  701. }
  702. int dccp_feat_init(struct dccp_minisock *dmsk)
  703. {
  704. int rc;
  705. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  706. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  707. /* CCID L */
  708. rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
  709. &dmsk->dccpms_tx_ccid, 1);
  710. if (rc)
  711. goto out;
  712. /* CCID R */
  713. rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
  714. &dmsk->dccpms_rx_ccid, 1);
  715. if (rc)
  716. goto out;
  717. /* Ack ratio */
  718. rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
  719. &dmsk->dccpms_ack_ratio, 1);
  720. out:
  721. return rc;
  722. }
  723. EXPORT_SYMBOL_GPL(dccp_feat_init);
  724. #ifdef CONFIG_IP_DCCP_DEBUG
  725. const char *dccp_feat_typename(const u8 type)
  726. {
  727. switch(type) {
  728. case DCCPO_CHANGE_L: return("ChangeL");
  729. case DCCPO_CONFIRM_L: return("ConfirmL");
  730. case DCCPO_CHANGE_R: return("ChangeR");
  731. case DCCPO_CONFIRM_R: return("ConfirmR");
  732. /* the following case must not appear in feature negotation */
  733. default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
  734. }
  735. return NULL;
  736. }
  737. EXPORT_SYMBOL_GPL(dccp_feat_typename);
  738. const char *dccp_feat_name(const u8 feat)
  739. {
  740. static const char *feature_names[] = {
  741. [DCCPF_RESERVED] = "Reserved",
  742. [DCCPF_CCID] = "CCID",
  743. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  744. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  745. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  746. [DCCPF_ACK_RATIO] = "Ack Ratio",
  747. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  748. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  749. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  750. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  751. };
  752. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  753. return feature_names[DCCPF_RESERVED];
  754. if (feat == DCCPF_SEND_LEV_RATE)
  755. return "Send Loss Event Rate";
  756. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  757. return "CCID-specific";
  758. return feature_names[feat];
  759. }
  760. EXPORT_SYMBOL_GPL(dccp_feat_name);
  761. #endif /* CONFIG_IP_DCCP_DEBUG */