feat.c 14 KB

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