feat.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 Feature negotiation is coordinated with connection setup (as in TCP), wild
  10. * changes of parameters of an established connection are not supported.
  11. * o All currently known SP features have 1-byte quantities. If in the future
  12. * extensions of RFCs 4340..42 define features with item lengths larger than
  13. * one byte, a feature-specific extension of the code will be required.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/module.h>
  21. #include "ccid.h"
  22. #include "feat.h"
  23. #define DCCP_FEAT_SP_NOAGREE (-123)
  24. static const struct {
  25. u8 feat_num; /* DCCPF_xxx */
  26. enum dccp_feat_type rxtx; /* RX or TX */
  27. enum dccp_feat_type reconciliation; /* SP or NN */
  28. u8 default_value; /* as in 6.4 */
  29. /*
  30. * Lookup table for location and type of features (from RFC 4340/4342)
  31. * +--------------------------+----+-----+----+----+---------+-----------+
  32. * | Feature | Location | Reconc. | Initial | Section |
  33. * | | RX | TX | SP | NN | Value | Reference |
  34. * +--------------------------+----+-----+----+----+---------+-----------+
  35. * | DCCPF_CCID | | X | X | | 2 | 10 |
  36. * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
  37. * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
  38. * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
  39. * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
  40. * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
  41. * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
  42. * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
  43. * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
  44. * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
  45. * +--------------------------+----+-----+----+----+---------+-----------+
  46. */
  47. } dccp_feat_table[] = {
  48. { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2 },
  49. { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0 },
  50. { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100 },
  51. { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0 },
  52. { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2 },
  53. { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0 },
  54. { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0 },
  55. { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0 },
  56. { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0 },
  57. { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0 },
  58. };
  59. #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
  60. /**
  61. * dccp_feat_index - Hash function to map feature number into array position
  62. * Returns consecutive array index or -1 if the feature is not understood.
  63. */
  64. static int dccp_feat_index(u8 feat_num)
  65. {
  66. /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
  67. if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
  68. return feat_num - 1;
  69. /*
  70. * Other features: add cases for new feature types here after adding
  71. * them to the above table.
  72. */
  73. switch (feat_num) {
  74. case DCCPF_SEND_LEV_RATE:
  75. return DCCP_FEAT_SUPPORTED_MAX - 1;
  76. }
  77. return -1;
  78. }
  79. static u8 dccp_feat_type(u8 feat_num)
  80. {
  81. int idx = dccp_feat_index(feat_num);
  82. if (idx < 0)
  83. return FEAT_UNKNOWN;
  84. return dccp_feat_table[idx].reconciliation;
  85. }
  86. static int dccp_feat_default_value(u8 feat_num)
  87. {
  88. int idx = dccp_feat_index(feat_num);
  89. return idx < 0 ? : dccp_feat_table[idx].default_value;
  90. }
  91. /* copy constructor, fval must not already contain allocated memory */
  92. static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
  93. {
  94. fval->sp.len = len;
  95. if (fval->sp.len > 0) {
  96. fval->sp.vec = kmemdup(val, len, gfp_any());
  97. if (fval->sp.vec == NULL) {
  98. fval->sp.len = 0;
  99. return -ENOBUFS;
  100. }
  101. }
  102. return 0;
  103. }
  104. static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
  105. {
  106. if (unlikely(val == NULL))
  107. return;
  108. if (dccp_feat_type(feat_num) == FEAT_SP)
  109. kfree(val->sp.vec);
  110. memset(val, 0, sizeof(*val));
  111. }
  112. static struct dccp_feat_entry *
  113. dccp_feat_clone_entry(struct dccp_feat_entry const *original)
  114. {
  115. struct dccp_feat_entry *new;
  116. u8 type = dccp_feat_type(original->feat_num);
  117. if (type == FEAT_UNKNOWN)
  118. return NULL;
  119. new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
  120. if (new == NULL)
  121. return NULL;
  122. if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
  123. original->val.sp.vec,
  124. original->val.sp.len)) {
  125. kfree(new);
  126. return NULL;
  127. }
  128. return new;
  129. }
  130. static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
  131. {
  132. if (entry != NULL) {
  133. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  134. kfree(entry);
  135. }
  136. }
  137. /*
  138. * List management functions
  139. *
  140. * Feature negotiation lists rely on and maintain the following invariants:
  141. * - each feat_num in the list is known, i.e. we know its type and default value
  142. * - each feat_num/is_local combination is unique (old entries are overwritten)
  143. * - SP values are always freshly allocated
  144. * - list is sorted in increasing order of feature number (faster lookup)
  145. */
  146. static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
  147. u8 feat_num, bool is_local)
  148. {
  149. struct dccp_feat_entry *entry;
  150. list_for_each_entry(entry, fn_list, node)
  151. if (entry->feat_num == feat_num && entry->is_local == is_local)
  152. return entry;
  153. else if (entry->feat_num > feat_num)
  154. break;
  155. return NULL;
  156. }
  157. /**
  158. * dccp_feat_entry_new - Central list update routine (called by all others)
  159. * @head: list to add to
  160. * @feat: feature number
  161. * @local: whether the local (1) or remote feature with number @feat is meant
  162. * This is the only constructor and serves to ensure the above invariants.
  163. */
  164. static struct dccp_feat_entry *
  165. dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
  166. {
  167. struct dccp_feat_entry *entry;
  168. list_for_each_entry(entry, head, node)
  169. if (entry->feat_num == feat && entry->is_local == local) {
  170. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  171. return entry;
  172. } else if (entry->feat_num > feat) {
  173. head = &entry->node;
  174. break;
  175. }
  176. entry = kmalloc(sizeof(*entry), gfp_any());
  177. if (entry != NULL) {
  178. entry->feat_num = feat;
  179. entry->is_local = local;
  180. list_add_tail(&entry->node, head);
  181. }
  182. return entry;
  183. }
  184. /**
  185. * dccp_feat_push_change - Add/overwrite a Change option in the list
  186. * @fn_list: feature-negotiation list to update
  187. * @feat: one of %dccp_feature_numbers
  188. * @local: whether local (1) or remote (0) @feat_num is meant
  189. * @needs_mandatory: whether to use Mandatory feature negotiation options
  190. * @fval: pointer to NN/SP value to be inserted (will be copied)
  191. */
  192. static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
  193. u8 mandatory, dccp_feat_val *fval)
  194. {
  195. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  196. if (new == NULL)
  197. return -ENOMEM;
  198. new->feat_num = feat;
  199. new->is_local = local;
  200. new->state = FEAT_INITIALISING;
  201. new->needs_confirm = 0;
  202. new->empty_confirm = 0;
  203. new->val = *fval;
  204. new->needs_mandatory = mandatory;
  205. return 0;
  206. }
  207. /**
  208. * dccp_feat_push_confirm - Add a Confirm entry to the FN list
  209. * @fn_list: feature-negotiation list to add to
  210. * @feat: one of %dccp_feature_numbers
  211. * @local: whether local (1) or remote (0) @feat_num is being confirmed
  212. * @fval: pointer to NN/SP value to be inserted or NULL
  213. * Returns 0 on success, a Reset code for further processing otherwise.
  214. */
  215. static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
  216. dccp_feat_val *fval)
  217. {
  218. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  219. if (new == NULL)
  220. return DCCP_RESET_CODE_TOO_BUSY;
  221. new->feat_num = feat;
  222. new->is_local = local;
  223. new->state = FEAT_STABLE; /* transition in 6.6.2 */
  224. new->needs_confirm = 1;
  225. new->empty_confirm = (fval == NULL);
  226. new->val.nn = 0; /* zeroes the whole structure */
  227. if (!new->empty_confirm)
  228. new->val = *fval;
  229. new->needs_mandatory = 0;
  230. return 0;
  231. }
  232. static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
  233. {
  234. return dccp_feat_push_confirm(fn_list, feat, local, NULL);
  235. }
  236. static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
  237. {
  238. list_del(&entry->node);
  239. dccp_feat_entry_destructor(entry);
  240. }
  241. void dccp_feat_list_purge(struct list_head *fn_list)
  242. {
  243. struct dccp_feat_entry *entry, *next;
  244. list_for_each_entry_safe(entry, next, fn_list, node)
  245. dccp_feat_entry_destructor(entry);
  246. INIT_LIST_HEAD(fn_list);
  247. }
  248. EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
  249. /* generate @to as full clone of @from - @to must not contain any nodes */
  250. int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
  251. {
  252. struct dccp_feat_entry *entry, *new;
  253. INIT_LIST_HEAD(to);
  254. list_for_each_entry(entry, from, node) {
  255. new = dccp_feat_clone_entry(entry);
  256. if (new == NULL)
  257. goto cloning_failed;
  258. list_add_tail(&new->node, to);
  259. }
  260. return 0;
  261. cloning_failed:
  262. dccp_feat_list_purge(to);
  263. return -ENOMEM;
  264. }
  265. static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
  266. {
  267. switch (feat_num) {
  268. case DCCPF_ACK_RATIO:
  269. return val <= DCCPF_ACK_RATIO_MAX;
  270. case DCCPF_SEQUENCE_WINDOW:
  271. return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
  272. }
  273. return 0; /* feature unknown - so we can't tell */
  274. }
  275. /* check that SP values are within the ranges defined in RFC 4340 */
  276. static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
  277. {
  278. switch (feat_num) {
  279. case DCCPF_CCID:
  280. return val == DCCPC_CCID2 || val == DCCPC_CCID3;
  281. /* Type-check Boolean feature values: */
  282. case DCCPF_SHORT_SEQNOS:
  283. case DCCPF_ECN_INCAPABLE:
  284. case DCCPF_SEND_ACK_VECTOR:
  285. case DCCPF_SEND_NDP_COUNT:
  286. case DCCPF_DATA_CHECKSUM:
  287. case DCCPF_SEND_LEV_RATE:
  288. return val < 2;
  289. case DCCPF_MIN_CSUM_COVER:
  290. return val < 16;
  291. }
  292. return 0; /* feature unknown */
  293. }
  294. static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
  295. {
  296. if (sp_list == NULL || sp_len < 1)
  297. return 0;
  298. while (sp_len--)
  299. if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
  300. return 0;
  301. return 1;
  302. }
  303. /**
  304. * __feat_register_nn - Register new NN value on socket
  305. * @fn: feature-negotiation list to register with
  306. * @feat: an NN feature from %dccp_feature_numbers
  307. * @mandatory: use Mandatory option if 1
  308. * @nn_val: value to register (restricted to 4 bytes)
  309. * Note that NN features are local by definition (RFC 4340, 6.3.2).
  310. */
  311. static int __feat_register_nn(struct list_head *fn, u8 feat,
  312. u8 mandatory, u64 nn_val)
  313. {
  314. dccp_feat_val fval = { .nn = nn_val };
  315. if (dccp_feat_type(feat) != FEAT_NN ||
  316. !dccp_feat_is_valid_nn_val(feat, nn_val))
  317. return -EINVAL;
  318. /* Don't bother with default values, they will be activated anyway. */
  319. if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
  320. return 0;
  321. return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
  322. }
  323. /**
  324. * __feat_register_sp - Register new SP value/list on socket
  325. * @fn: feature-negotiation list to register with
  326. * @feat: an SP feature from %dccp_feature_numbers
  327. * @is_local: whether the local (1) or the remote (0) @feat is meant
  328. * @mandatory: use Mandatory option if 1
  329. * @sp_val: SP value followed by optional preference list
  330. * @sp_len: length of @sp_val in bytes
  331. */
  332. static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
  333. u8 mandatory, u8 const *sp_val, u8 sp_len)
  334. {
  335. dccp_feat_val fval;
  336. if (dccp_feat_type(feat) != FEAT_SP ||
  337. !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
  338. return -EINVAL;
  339. /* Avoid negotiating alien CCIDs by only advertising supported ones */
  340. if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
  341. return -EOPNOTSUPP;
  342. if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
  343. return -ENOMEM;
  344. return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
  345. }
  346. int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
  347. u8 *val, u8 len, gfp_t gfp)
  348. {
  349. struct dccp_opt_pend *opt;
  350. dccp_feat_debug(type, feature, *val);
  351. if (len > 3) {
  352. DCCP_WARN("invalid length %d\n", len);
  353. return -EINVAL;
  354. }
  355. /* XXX add further sanity checks */
  356. /* check if that feature is already being negotiated */
  357. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  358. /* ok we found a negotiation for this option already */
  359. if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
  360. dccp_pr_debug("Replacing old\n");
  361. /* replace */
  362. BUG_ON(opt->dccpop_val == NULL);
  363. kfree(opt->dccpop_val);
  364. opt->dccpop_val = val;
  365. opt->dccpop_len = len;
  366. opt->dccpop_conf = 0;
  367. return 0;
  368. }
  369. }
  370. /* negotiation for a new feature */
  371. opt = kmalloc(sizeof(*opt), gfp);
  372. if (opt == NULL)
  373. return -ENOMEM;
  374. opt->dccpop_type = type;
  375. opt->dccpop_feat = feature;
  376. opt->dccpop_len = len;
  377. opt->dccpop_val = val;
  378. opt->dccpop_conf = 0;
  379. opt->dccpop_sc = NULL;
  380. BUG_ON(opt->dccpop_val == NULL);
  381. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(dccp_feat_change);
  385. static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
  386. {
  387. struct dccp_sock *dp = dccp_sk(sk);
  388. struct dccp_minisock *dmsk = dccp_msk(sk);
  389. /* figure out if we are changing our CCID or the peer's */
  390. const int rx = type == DCCPO_CHANGE_R;
  391. const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
  392. struct ccid *new_ccid;
  393. /* Check if nothing is being changed. */
  394. if (ccid_nr == new_ccid_nr)
  395. return 0;
  396. new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
  397. if (new_ccid == NULL)
  398. return -ENOMEM;
  399. if (rx) {
  400. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  401. dp->dccps_hc_rx_ccid = new_ccid;
  402. dmsk->dccpms_rx_ccid = new_ccid_nr;
  403. } else {
  404. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  405. dp->dccps_hc_tx_ccid = new_ccid;
  406. dmsk->dccpms_tx_ccid = new_ccid_nr;
  407. }
  408. return 0;
  409. }
  410. static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
  411. {
  412. dccp_feat_debug(type, feat, val);
  413. switch (feat) {
  414. case DCCPF_CCID:
  415. return dccp_feat_update_ccid(sk, type, val);
  416. default:
  417. dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
  418. dccp_feat_typename(type), feat);
  419. break;
  420. }
  421. return 0;
  422. }
  423. static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
  424. u8 *rpref, u8 rlen)
  425. {
  426. struct dccp_sock *dp = dccp_sk(sk);
  427. u8 *spref, slen, *res = NULL;
  428. int i, j, rc, agree = 1;
  429. BUG_ON(rpref == NULL);
  430. /* check if we are the black sheep */
  431. if (dp->dccps_role == DCCP_ROLE_CLIENT) {
  432. spref = rpref;
  433. slen = rlen;
  434. rpref = opt->dccpop_val;
  435. rlen = opt->dccpop_len;
  436. } else {
  437. spref = opt->dccpop_val;
  438. slen = opt->dccpop_len;
  439. }
  440. /*
  441. * Now we have server preference list in spref and client preference in
  442. * rpref
  443. */
  444. BUG_ON(spref == NULL);
  445. BUG_ON(rpref == NULL);
  446. /* FIXME sanity check vals */
  447. /* Are values in any order? XXX Lame "algorithm" here */
  448. for (i = 0; i < slen; i++) {
  449. for (j = 0; j < rlen; j++) {
  450. if (spref[i] == rpref[j]) {
  451. res = &spref[i];
  452. break;
  453. }
  454. }
  455. if (res)
  456. break;
  457. }
  458. /* we didn't agree on anything */
  459. if (res == NULL) {
  460. /* confirm previous value */
  461. switch (opt->dccpop_feat) {
  462. case DCCPF_CCID:
  463. /* XXX did i get this right? =P */
  464. if (opt->dccpop_type == DCCPO_CHANGE_L)
  465. res = &dccp_msk(sk)->dccpms_tx_ccid;
  466. else
  467. res = &dccp_msk(sk)->dccpms_rx_ccid;
  468. break;
  469. default:
  470. DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
  471. /* XXX implement res */
  472. return -EFAULT;
  473. }
  474. dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
  475. agree = 0; /* this is used for mandatory options... */
  476. }
  477. /* need to put result and our preference list */
  478. rlen = 1 + opt->dccpop_len;
  479. rpref = kmalloc(rlen, GFP_ATOMIC);
  480. if (rpref == NULL)
  481. return -ENOMEM;
  482. *rpref = *res;
  483. memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
  484. /* put it in the "confirm queue" */
  485. if (opt->dccpop_sc == NULL) {
  486. opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
  487. if (opt->dccpop_sc == NULL) {
  488. kfree(rpref);
  489. return -ENOMEM;
  490. }
  491. } else {
  492. /* recycle the confirm slot */
  493. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  494. kfree(opt->dccpop_sc->dccpoc_val);
  495. dccp_pr_debug("recycling confirm slot\n");
  496. }
  497. memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
  498. opt->dccpop_sc->dccpoc_val = rpref;
  499. opt->dccpop_sc->dccpoc_len = rlen;
  500. /* update the option on our side [we are about to send the confirm] */
  501. rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
  502. if (rc) {
  503. kfree(opt->dccpop_sc->dccpoc_val);
  504. kfree(opt->dccpop_sc);
  505. opt->dccpop_sc = NULL;
  506. return rc;
  507. }
  508. dccp_pr_debug("Will confirm %d\n", *rpref);
  509. /* say we want to change to X but we just got a confirm X, suppress our
  510. * change
  511. */
  512. if (!opt->dccpop_conf) {
  513. if (*opt->dccpop_val == *res)
  514. opt->dccpop_conf = 1;
  515. dccp_pr_debug("won't ask for change of same feature\n");
  516. }
  517. return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
  518. }
  519. static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  520. {
  521. struct dccp_minisock *dmsk = dccp_msk(sk);
  522. struct dccp_opt_pend *opt;
  523. int rc = 1;
  524. u8 t;
  525. /*
  526. * We received a CHANGE. We gotta match it against our own preference
  527. * list. If we got a CHANGE_R it means it's a change for us, so we need
  528. * to compare our CHANGE_L list.
  529. */
  530. if (type == DCCPO_CHANGE_L)
  531. t = DCCPO_CHANGE_R;
  532. else
  533. t = DCCPO_CHANGE_L;
  534. /* find our preference list for this feature */
  535. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  536. if (opt->dccpop_type != t || opt->dccpop_feat != feature)
  537. continue;
  538. /* find the winner from the two preference lists */
  539. rc = dccp_feat_reconcile(sk, opt, val, len);
  540. break;
  541. }
  542. /* We didn't deal with the change. This can happen if we have no
  543. * preference list for the feature. In fact, it just shouldn't
  544. * happen---if we understand a feature, we should have a preference list
  545. * with at least the default value.
  546. */
  547. BUG_ON(rc == 1);
  548. return rc;
  549. }
  550. static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  551. {
  552. struct dccp_opt_pend *opt;
  553. struct dccp_minisock *dmsk = dccp_msk(sk);
  554. u8 *copy;
  555. int rc;
  556. /* NN features must be Change L (sec. 6.3.2) */
  557. if (type != DCCPO_CHANGE_L) {
  558. dccp_pr_debug("received %s for NN feature %d\n",
  559. dccp_feat_typename(type), feature);
  560. return -EFAULT;
  561. }
  562. /* XXX sanity check opt val */
  563. /* copy option so we can confirm it */
  564. opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  565. if (opt == NULL)
  566. return -ENOMEM;
  567. copy = kmemdup(val, len, GFP_ATOMIC);
  568. if (copy == NULL) {
  569. kfree(opt);
  570. return -ENOMEM;
  571. }
  572. opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
  573. opt->dccpop_feat = feature;
  574. opt->dccpop_val = copy;
  575. opt->dccpop_len = len;
  576. /* change feature */
  577. rc = dccp_feat_update(sk, type, feature, *val);
  578. if (rc) {
  579. kfree(opt->dccpop_val);
  580. kfree(opt);
  581. return rc;
  582. }
  583. dccp_feat_debug(type, feature, *copy);
  584. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  585. return 0;
  586. }
  587. static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
  588. u8 type, u8 feature)
  589. {
  590. /* XXX check if other confirms for that are queued and recycle slot */
  591. struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  592. if (opt == NULL) {
  593. /* XXX what do we do? Ignoring should be fine. It's a change
  594. * after all =P
  595. */
  596. return;
  597. }
  598. switch (type) {
  599. case DCCPO_CHANGE_L:
  600. opt->dccpop_type = DCCPO_CONFIRM_R;
  601. break;
  602. case DCCPO_CHANGE_R:
  603. opt->dccpop_type = DCCPO_CONFIRM_L;
  604. break;
  605. default:
  606. DCCP_WARN("invalid type %d\n", type);
  607. kfree(opt);
  608. return;
  609. }
  610. opt->dccpop_feat = feature;
  611. opt->dccpop_val = NULL;
  612. opt->dccpop_len = 0;
  613. /* change feature */
  614. dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
  615. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  616. }
  617. static void dccp_feat_flush_confirm(struct sock *sk)
  618. {
  619. struct dccp_minisock *dmsk = dccp_msk(sk);
  620. /* Check if there is anything to confirm in the first place */
  621. int yes = !list_empty(&dmsk->dccpms_conf);
  622. if (!yes) {
  623. struct dccp_opt_pend *opt;
  624. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  625. if (opt->dccpop_conf) {
  626. yes = 1;
  627. break;
  628. }
  629. }
  630. }
  631. if (!yes)
  632. return;
  633. /* OK there is something to confirm... */
  634. /* XXX check if packet is in flight? Send delayed ack?? */
  635. if (sk->sk_state == DCCP_OPEN)
  636. dccp_send_ack(sk);
  637. }
  638. int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  639. {
  640. int rc;
  641. /* Ignore Change requests other than during connection setup */
  642. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  643. return 0;
  644. dccp_feat_debug(type, feature, *val);
  645. /* figure out if it's SP or NN feature */
  646. switch (feature) {
  647. /* deal with SP features */
  648. case DCCPF_CCID:
  649. rc = dccp_feat_sp(sk, type, feature, val, len);
  650. break;
  651. /* deal with NN features */
  652. case DCCPF_ACK_RATIO:
  653. rc = dccp_feat_nn(sk, type, feature, val, len);
  654. break;
  655. /* XXX implement other features */
  656. default:
  657. dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
  658. dccp_feat_typename(type), feature);
  659. rc = -EFAULT;
  660. break;
  661. }
  662. /* check if there were problems changing features */
  663. if (rc) {
  664. /* If we don't agree on SP, we sent a confirm for old value.
  665. * However we propagate rc to caller in case option was
  666. * mandatory
  667. */
  668. if (rc != DCCP_FEAT_SP_NOAGREE)
  669. dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
  670. }
  671. /* generate the confirm [if required] */
  672. dccp_feat_flush_confirm(sk);
  673. return rc;
  674. }
  675. EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
  676. int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
  677. u8 *val, u8 len)
  678. {
  679. u8 t;
  680. struct dccp_opt_pend *opt;
  681. struct dccp_minisock *dmsk = dccp_msk(sk);
  682. int found = 0;
  683. int all_confirmed = 1;
  684. /* Ignore Confirm options other than during connection setup */
  685. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  686. return 0;
  687. dccp_feat_debug(type, feature, *val);
  688. /* locate our change request */
  689. switch (type) {
  690. case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
  691. case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
  692. default: DCCP_WARN("invalid type %d\n", type);
  693. return 1;
  694. }
  695. /* XXX sanity check feature value */
  696. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  697. if (!opt->dccpop_conf && opt->dccpop_type == t &&
  698. opt->dccpop_feat == feature) {
  699. found = 1;
  700. dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
  701. /* XXX do sanity check */
  702. opt->dccpop_conf = 1;
  703. /* We got a confirmation---change the option */
  704. dccp_feat_update(sk, opt->dccpop_type,
  705. opt->dccpop_feat, *val);
  706. /* XXX check the return value of dccp_feat_update */
  707. break;
  708. }
  709. if (!opt->dccpop_conf)
  710. all_confirmed = 0;
  711. }
  712. if (!found)
  713. dccp_pr_debug("%s(%d, ...) never requested\n",
  714. dccp_feat_typename(type), feature);
  715. return 0;
  716. }
  717. EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
  718. void dccp_feat_clean(struct dccp_minisock *dmsk)
  719. {
  720. struct dccp_opt_pend *opt, *next;
  721. list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
  722. dccpop_node) {
  723. BUG_ON(opt->dccpop_val == NULL);
  724. kfree(opt->dccpop_val);
  725. if (opt->dccpop_sc != NULL) {
  726. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  727. kfree(opt->dccpop_sc->dccpoc_val);
  728. kfree(opt->dccpop_sc);
  729. }
  730. kfree(opt);
  731. }
  732. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  733. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  734. BUG_ON(opt == NULL);
  735. if (opt->dccpop_val != NULL)
  736. kfree(opt->dccpop_val);
  737. kfree(opt);
  738. }
  739. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  740. }
  741. EXPORT_SYMBOL_GPL(dccp_feat_clean);
  742. /* this is to be called only when a listening sock creates its child. It is
  743. * assumed by the function---the confirm is not duplicated, but rather it is
  744. * "passed on".
  745. */
  746. int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
  747. {
  748. struct dccp_minisock *olddmsk = dccp_msk(oldsk);
  749. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  750. struct dccp_opt_pend *opt;
  751. int rc = 0;
  752. INIT_LIST_HEAD(&newdmsk->dccpms_pending);
  753. INIT_LIST_HEAD(&newdmsk->dccpms_conf);
  754. list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
  755. struct dccp_opt_pend *newopt;
  756. /* copy the value of the option */
  757. u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
  758. if (val == NULL)
  759. goto out_clean;
  760. newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
  761. if (newopt == NULL) {
  762. kfree(val);
  763. goto out_clean;
  764. }
  765. /* insert the option */
  766. newopt->dccpop_val = val;
  767. list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
  768. /* XXX what happens with backlogs and multiple connections at
  769. * once...
  770. */
  771. /* the master socket no longer needs to worry about confirms */
  772. opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
  773. /* reset state for a new socket */
  774. opt->dccpop_conf = 0;
  775. }
  776. /* XXX not doing anything about the conf queue */
  777. out:
  778. return rc;
  779. out_clean:
  780. dccp_feat_clean(newdmsk);
  781. rc = -ENOMEM;
  782. goto out;
  783. }
  784. EXPORT_SYMBOL_GPL(dccp_feat_clone);
  785. int dccp_feat_init(struct sock *sk)
  786. {
  787. struct dccp_sock *dp = dccp_sk(sk);
  788. struct dccp_minisock *dmsk = dccp_msk(sk);
  789. int rc;
  790. INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
  791. INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
  792. /* CCID L */
  793. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
  794. &dmsk->dccpms_tx_ccid, 1);
  795. if (rc)
  796. goto out;
  797. /* CCID R */
  798. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
  799. &dmsk->dccpms_rx_ccid, 1);
  800. if (rc)
  801. goto out;
  802. /* Ack ratio */
  803. rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
  804. dmsk->dccpms_ack_ratio);
  805. out:
  806. return rc;
  807. }
  808. EXPORT_SYMBOL_GPL(dccp_feat_init);
  809. #ifdef CONFIG_IP_DCCP_DEBUG
  810. const char *dccp_feat_typename(const u8 type)
  811. {
  812. switch(type) {
  813. case DCCPO_CHANGE_L: return("ChangeL");
  814. case DCCPO_CONFIRM_L: return("ConfirmL");
  815. case DCCPO_CHANGE_R: return("ChangeR");
  816. case DCCPO_CONFIRM_R: return("ConfirmR");
  817. /* the following case must not appear in feature negotation */
  818. default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
  819. }
  820. return NULL;
  821. }
  822. EXPORT_SYMBOL_GPL(dccp_feat_typename);
  823. const char *dccp_feat_name(const u8 feat)
  824. {
  825. static const char *feature_names[] = {
  826. [DCCPF_RESERVED] = "Reserved",
  827. [DCCPF_CCID] = "CCID",
  828. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  829. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  830. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  831. [DCCPF_ACK_RATIO] = "Ack Ratio",
  832. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  833. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  834. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  835. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  836. };
  837. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  838. return feature_names[DCCPF_RESERVED];
  839. if (feat == DCCPF_SEND_LEV_RATE)
  840. return "Send Loss Event Rate";
  841. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  842. return "CCID-specific";
  843. return feature_names[feat];
  844. }
  845. EXPORT_SYMBOL_GPL(dccp_feat_name);
  846. #endif /* CONFIG_IP_DCCP_DEBUG */