feat.c 14 KB

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