feat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include "dccp.h"
  15. #include "ccid.h"
  16. #include "feat.h"
  17. #define DCCP_FEAT_SP_NOAGREE (-123)
  18. int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
  19. u8 *val, u8 len, gfp_t gfp)
  20. {
  21. struct dccp_opt_pend *opt;
  22. dccp_pr_debug("feat change type=%d feat=%d\n", type, feature);
  23. /* XXX sanity check feat change request */
  24. /* check if that feature is already being negotiated */
  25. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  26. /* ok we found a negotiation for this option already */
  27. if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
  28. dccp_pr_debug("Replacing old\n");
  29. /* replace */
  30. BUG_ON(opt->dccpop_val == NULL);
  31. kfree(opt->dccpop_val);
  32. opt->dccpop_val = val;
  33. opt->dccpop_len = len;
  34. opt->dccpop_conf = 0;
  35. return 0;
  36. }
  37. }
  38. /* negotiation for a new feature */
  39. opt = kmalloc(sizeof(*opt), gfp);
  40. if (opt == NULL)
  41. return -ENOMEM;
  42. opt->dccpop_type = type;
  43. opt->dccpop_feat = feature;
  44. opt->dccpop_len = len;
  45. opt->dccpop_val = val;
  46. opt->dccpop_conf = 0;
  47. opt->dccpop_sc = NULL;
  48. BUG_ON(opt->dccpop_val == NULL);
  49. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(dccp_feat_change);
  53. static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
  54. {
  55. struct dccp_sock *dp = dccp_sk(sk);
  56. struct dccp_minisock *dmsk = dccp_msk(sk);
  57. /* figure out if we are changing our CCID or the peer's */
  58. const int rx = type == DCCPO_CHANGE_R;
  59. const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
  60. struct ccid *new_ccid;
  61. /* Check if nothing is being changed. */
  62. if (ccid_nr == new_ccid_nr)
  63. return 0;
  64. new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
  65. if (new_ccid == NULL)
  66. return -ENOMEM;
  67. if (rx) {
  68. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  69. dp->dccps_hc_rx_ccid = new_ccid;
  70. dmsk->dccpms_rx_ccid = new_ccid_nr;
  71. } else {
  72. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  73. dp->dccps_hc_tx_ccid = new_ccid;
  74. dmsk->dccpms_tx_ccid = new_ccid_nr;
  75. }
  76. return 0;
  77. }
  78. /* XXX taking only u8 vals */
  79. static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
  80. {
  81. dccp_pr_debug("changing [%d] feat %d to %d\n", type, feat, val);
  82. switch (feat) {
  83. case DCCPF_CCID:
  84. return dccp_feat_update_ccid(sk, type, val);
  85. default:
  86. dccp_pr_debug("IMPLEMENT changing [%d] feat %d to %d\n",
  87. type, feat, val);
  88. break;
  89. }
  90. return 0;
  91. }
  92. static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
  93. u8 *rpref, u8 rlen)
  94. {
  95. struct dccp_sock *dp = dccp_sk(sk);
  96. u8 *spref, slen, *res = NULL;
  97. int i, j, rc, agree = 1;
  98. BUG_ON(rpref == NULL);
  99. /* check if we are the black sheep */
  100. if (dp->dccps_role == DCCP_ROLE_CLIENT) {
  101. spref = rpref;
  102. slen = rlen;
  103. rpref = opt->dccpop_val;
  104. rlen = opt->dccpop_len;
  105. } else {
  106. spref = opt->dccpop_val;
  107. slen = opt->dccpop_len;
  108. }
  109. /*
  110. * Now we have server preference list in spref and client preference in
  111. * rpref
  112. */
  113. BUG_ON(spref == NULL);
  114. BUG_ON(rpref == NULL);
  115. /* FIXME sanity check vals */
  116. /* Are values in any order? XXX Lame "algorithm" here */
  117. /* XXX assume values are 1 byte */
  118. for (i = 0; i < slen; i++) {
  119. for (j = 0; j < rlen; j++) {
  120. if (spref[i] == rpref[j]) {
  121. res = &spref[i];
  122. break;
  123. }
  124. }
  125. if (res)
  126. break;
  127. }
  128. /* we didn't agree on anything */
  129. if (res == NULL) {
  130. /* confirm previous value */
  131. switch (opt->dccpop_feat) {
  132. case DCCPF_CCID:
  133. /* XXX did i get this right? =P */
  134. if (opt->dccpop_type == DCCPO_CHANGE_L)
  135. res = &dccp_msk(sk)->dccpms_tx_ccid;
  136. else
  137. res = &dccp_msk(sk)->dccpms_rx_ccid;
  138. break;
  139. default:
  140. WARN_ON(1); /* XXX implement res */
  141. return -EFAULT;
  142. }
  143. dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
  144. agree = 0; /* this is used for mandatory options... */
  145. }
  146. /* need to put result and our preference list */
  147. /* XXX assume 1 byte vals */
  148. rlen = 1 + opt->dccpop_len;
  149. rpref = kmalloc(rlen, GFP_ATOMIC);
  150. if (rpref == NULL)
  151. return -ENOMEM;
  152. *rpref = *res;
  153. memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
  154. /* put it in the "confirm queue" */
  155. if (opt->dccpop_sc == NULL) {
  156. opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
  157. if (opt->dccpop_sc == NULL) {
  158. kfree(rpref);
  159. return -ENOMEM;
  160. }
  161. } else {
  162. /* recycle the confirm slot */
  163. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  164. kfree(opt->dccpop_sc->dccpoc_val);
  165. dccp_pr_debug("recycling confirm slot\n");
  166. }
  167. memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
  168. opt->dccpop_sc->dccpoc_val = rpref;
  169. opt->dccpop_sc->dccpoc_len = rlen;
  170. /* update the option on our side [we are about to send the confirm] */
  171. rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
  172. if (rc) {
  173. kfree(opt->dccpop_sc->dccpoc_val);
  174. kfree(opt->dccpop_sc);
  175. opt->dccpop_sc = NULL;
  176. return rc;
  177. }
  178. dccp_pr_debug("Will confirm %d\n", *rpref);
  179. /* say we want to change to X but we just got a confirm X, suppress our
  180. * change
  181. */
  182. if (!opt->dccpop_conf) {
  183. if (*opt->dccpop_val == *res)
  184. opt->dccpop_conf = 1;
  185. dccp_pr_debug("won't ask for change of same feature\n");
  186. }
  187. return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
  188. }
  189. static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  190. {
  191. struct dccp_minisock *dmsk = dccp_msk(sk);
  192. struct dccp_opt_pend *opt;
  193. int rc = 1;
  194. u8 t;
  195. /*
  196. * We received a CHANGE. We gotta match it against our own preference
  197. * list. If we got a CHANGE_R it means it's a change for us, so we need
  198. * to compare our CHANGE_L list.
  199. */
  200. if (type == DCCPO_CHANGE_L)
  201. t = DCCPO_CHANGE_R;
  202. else
  203. t = DCCPO_CHANGE_L;
  204. /* find our preference list for this feature */
  205. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  206. if (opt->dccpop_type != t || opt->dccpop_feat != feature)
  207. continue;
  208. /* find the winner from the two preference lists */
  209. rc = dccp_feat_reconcile(sk, opt, val, len);
  210. break;
  211. }
  212. /* We didn't deal with the change. This can happen if we have no
  213. * preference list for the feature. In fact, it just shouldn't
  214. * happen---if we understand a feature, we should have a preference list
  215. * with at least the default value.
  216. */
  217. BUG_ON(rc == 1);
  218. return rc;
  219. }
  220. static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  221. {
  222. struct dccp_opt_pend *opt;
  223. struct dccp_minisock *dmsk = dccp_msk(sk);
  224. u8 *copy;
  225. int rc;
  226. /* NN features must be change L */
  227. if (type == DCCPO_CHANGE_R) {
  228. dccp_pr_debug("received CHANGE_R %d for NN feat %d\n",
  229. type, feature);
  230. return -EFAULT;
  231. }
  232. /* XXX sanity check opt val */
  233. /* copy option so we can confirm it */
  234. opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  235. if (opt == NULL)
  236. return -ENOMEM;
  237. copy = kmalloc(len, GFP_ATOMIC);
  238. if (copy == NULL) {
  239. kfree(opt);
  240. return -ENOMEM;
  241. }
  242. memcpy(copy, val, len);
  243. opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
  244. opt->dccpop_feat = feature;
  245. opt->dccpop_val = copy;
  246. opt->dccpop_len = len;
  247. /* change feature */
  248. rc = dccp_feat_update(sk, type, feature, *val);
  249. if (rc) {
  250. kfree(opt->dccpop_val);
  251. kfree(opt);
  252. return rc;
  253. }
  254. dccp_pr_debug("Confirming NN feature %d (val=%d)\n", feature, *copy);
  255. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  256. return 0;
  257. }
  258. static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
  259. u8 type, u8 feature)
  260. {
  261. /* XXX check if other confirms for that are queued and recycle slot */
  262. struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  263. if (opt == NULL) {
  264. /* XXX what do we do? Ignoring should be fine. It's a change
  265. * after all =P
  266. */
  267. return;
  268. }
  269. opt->dccpop_type = type == DCCPO_CHANGE_L ? DCCPO_CONFIRM_R :
  270. DCCPO_CONFIRM_L;
  271. opt->dccpop_feat = feature;
  272. opt->dccpop_val = NULL;
  273. opt->dccpop_len = 0;
  274. /* change feature */
  275. dccp_pr_debug("Empty confirm feature %d type %d\n", feature, type);
  276. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  277. }
  278. static void dccp_feat_flush_confirm(struct sock *sk)
  279. {
  280. struct dccp_minisock *dmsk = dccp_msk(sk);
  281. /* Check if there is anything to confirm in the first place */
  282. int yes = !list_empty(&dmsk->dccpms_conf);
  283. if (!yes) {
  284. struct dccp_opt_pend *opt;
  285. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  286. if (opt->dccpop_conf) {
  287. yes = 1;
  288. break;
  289. }
  290. }
  291. }
  292. if (!yes)
  293. return;
  294. /* OK there is something to confirm... */
  295. /* XXX check if packet is in flight? Send delayed ack?? */
  296. if (sk->sk_state == DCCP_OPEN)
  297. dccp_send_ack(sk);
  298. }
  299. int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  300. {
  301. int rc;
  302. dccp_pr_debug("got feat change type=%d feat=%d\n", type, feature);
  303. /* figure out if it's SP or NN feature */
  304. switch (feature) {
  305. /* deal with SP features */
  306. case DCCPF_CCID:
  307. rc = dccp_feat_sp(sk, type, feature, val, len);
  308. break;
  309. /* deal with NN features */
  310. case DCCPF_ACK_RATIO:
  311. rc = dccp_feat_nn(sk, type, feature, val, len);
  312. break;
  313. /* XXX implement other features */
  314. default:
  315. rc = -EFAULT;
  316. break;
  317. }
  318. /* check if there were problems changing features */
  319. if (rc) {
  320. /* If we don't agree on SP, we sent a confirm for old value.
  321. * However we propagate rc to caller in case option was
  322. * mandatory
  323. */
  324. if (rc != DCCP_FEAT_SP_NOAGREE)
  325. dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
  326. }
  327. /* generate the confirm [if required] */
  328. dccp_feat_flush_confirm(sk);
  329. return rc;
  330. }
  331. EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
  332. int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
  333. u8 *val, u8 len)
  334. {
  335. u8 t;
  336. struct dccp_opt_pend *opt;
  337. struct dccp_minisock *dmsk = dccp_msk(sk);
  338. int rc = 1;
  339. int all_confirmed = 1;
  340. dccp_pr_debug("got feat confirm type=%d feat=%d\n", type, feature);
  341. /* XXX sanity check type & feat */
  342. /* locate our change request */
  343. t = type == DCCPO_CONFIRM_L ? DCCPO_CHANGE_R : DCCPO_CHANGE_L;
  344. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  345. if (!opt->dccpop_conf && opt->dccpop_type == t &&
  346. opt->dccpop_feat == feature) {
  347. /* we found it */
  348. /* XXX do sanity check */
  349. opt->dccpop_conf = 1;
  350. /* We got a confirmation---change the option */
  351. dccp_feat_update(sk, opt->dccpop_type,
  352. opt->dccpop_feat, *val);
  353. dccp_pr_debug("feat %d type %d confirmed %d\n",
  354. feature, type, *val);
  355. rc = 0;
  356. break;
  357. }
  358. if (!opt->dccpop_conf)
  359. all_confirmed = 0;
  360. }
  361. /* fix re-transmit timer */
  362. /* XXX gotta make sure that no option negotiation occurs during
  363. * connection shutdown. Consider that the CLOSEREQ is sent and timer is
  364. * on. if all options are confirmed it might kill timer which should
  365. * remain alive until close is received.
  366. */
  367. if (all_confirmed) {
  368. dccp_pr_debug("clear feat negotiation timer %p\n", sk);
  369. inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
  370. }
  371. if (rc)
  372. dccp_pr_debug("feat %d type %d never requested\n",
  373. feature, type);
  374. return 0;
  375. }
  376. EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
  377. void dccp_feat_clean(struct dccp_minisock *dmsk)
  378. {
  379. struct dccp_opt_pend *opt, *next;
  380. list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
  381. dccpop_node) {
  382. BUG_ON(opt->dccpop_val == NULL);
  383. kfree(opt->dccpop_val);
  384. if (opt->dccpop_sc != NULL) {
  385. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  386. kfree(opt->dccpop_sc->dccpoc_val);
  387. kfree(opt->dccpop_sc);
  388. }
  389. kfree(opt);
  390. }
  391. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  392. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  393. BUG_ON(opt == NULL);
  394. if (opt->dccpop_val != NULL)
  395. kfree(opt->dccpop_val);
  396. kfree(opt);
  397. }
  398. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  399. }
  400. EXPORT_SYMBOL_GPL(dccp_feat_clean);
  401. /* this is to be called only when a listening sock creates its child. It is
  402. * assumed by the function---the confirm is not duplicated, but rather it is
  403. * "passed on".
  404. */
  405. int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
  406. {
  407. struct dccp_minisock *olddmsk = dccp_msk(oldsk);
  408. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  409. struct dccp_opt_pend *opt;
  410. int rc = 0;
  411. INIT_LIST_HEAD(&newdmsk->dccpms_pending);
  412. INIT_LIST_HEAD(&newdmsk->dccpms_conf);
  413. list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
  414. struct dccp_opt_pend *newopt;
  415. /* copy the value of the option */
  416. u8 *val = kmalloc(opt->dccpop_len, GFP_ATOMIC);
  417. if (val == NULL)
  418. goto out_clean;
  419. memcpy(val, opt->dccpop_val, opt->dccpop_len);
  420. newopt = kmalloc(sizeof(*newopt), GFP_ATOMIC);
  421. if (newopt == NULL) {
  422. kfree(val);
  423. goto out_clean;
  424. }
  425. /* insert the option */
  426. memcpy(newopt, opt, sizeof(*newopt));
  427. newopt->dccpop_val = val;
  428. list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
  429. /* XXX what happens with backlogs and multiple connections at
  430. * once...
  431. */
  432. /* the master socket no longer needs to worry about confirms */
  433. opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
  434. /* reset state for a new socket */
  435. opt->dccpop_conf = 0;
  436. }
  437. /* XXX not doing anything about the conf queue */
  438. out:
  439. return rc;
  440. out_clean:
  441. dccp_feat_clean(newdmsk);
  442. rc = -ENOMEM;
  443. goto out;
  444. }
  445. EXPORT_SYMBOL_GPL(dccp_feat_clone);
  446. static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
  447. u8 *val, u8 len)
  448. {
  449. int rc = -ENOMEM;
  450. u8 *copy = kmalloc(len, GFP_KERNEL);
  451. if (copy != NULL) {
  452. memcpy(copy, val, len);
  453. rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
  454. if (rc)
  455. kfree(copy);
  456. }
  457. return rc;
  458. }
  459. int dccp_feat_init(struct dccp_minisock *dmsk)
  460. {
  461. int rc;
  462. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  463. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  464. /* CCID L */
  465. rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
  466. &dmsk->dccpms_tx_ccid, 1);
  467. if (rc)
  468. goto out;
  469. /* CCID R */
  470. rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
  471. &dmsk->dccpms_rx_ccid, 1);
  472. if (rc)
  473. goto out;
  474. /* Ack ratio */
  475. rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
  476. &dmsk->dccpms_ack_ratio, 1);
  477. out:
  478. return rc;
  479. }
  480. EXPORT_SYMBOL_GPL(dccp_feat_init);