feat.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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. /*
  90. * There are no default values for unknown features, so encountering a
  91. * negative index here indicates a serious problem somewhere else.
  92. */
  93. DCCP_BUG_ON(idx < 0);
  94. return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
  95. }
  96. /* copy constructor, fval must not already contain allocated memory */
  97. static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
  98. {
  99. fval->sp.len = len;
  100. if (fval->sp.len > 0) {
  101. fval->sp.vec = kmemdup(val, len, gfp_any());
  102. if (fval->sp.vec == NULL) {
  103. fval->sp.len = 0;
  104. return -ENOBUFS;
  105. }
  106. }
  107. return 0;
  108. }
  109. static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
  110. {
  111. if (unlikely(val == NULL))
  112. return;
  113. if (dccp_feat_type(feat_num) == FEAT_SP)
  114. kfree(val->sp.vec);
  115. memset(val, 0, sizeof(*val));
  116. }
  117. static struct dccp_feat_entry *
  118. dccp_feat_clone_entry(struct dccp_feat_entry const *original)
  119. {
  120. struct dccp_feat_entry *new;
  121. u8 type = dccp_feat_type(original->feat_num);
  122. if (type == FEAT_UNKNOWN)
  123. return NULL;
  124. new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
  125. if (new == NULL)
  126. return NULL;
  127. if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
  128. original->val.sp.vec,
  129. original->val.sp.len)) {
  130. kfree(new);
  131. return NULL;
  132. }
  133. return new;
  134. }
  135. static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
  136. {
  137. if (entry != NULL) {
  138. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  139. kfree(entry);
  140. }
  141. }
  142. /*
  143. * List management functions
  144. *
  145. * Feature negotiation lists rely on and maintain the following invariants:
  146. * - each feat_num in the list is known, i.e. we know its type and default value
  147. * - each feat_num/is_local combination is unique (old entries are overwritten)
  148. * - SP values are always freshly allocated
  149. * - list is sorted in increasing order of feature number (faster lookup)
  150. */
  151. static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
  152. u8 feat_num, bool is_local)
  153. {
  154. struct dccp_feat_entry *entry;
  155. list_for_each_entry(entry, fn_list, node)
  156. if (entry->feat_num == feat_num && entry->is_local == is_local)
  157. return entry;
  158. else if (entry->feat_num > feat_num)
  159. break;
  160. return NULL;
  161. }
  162. /**
  163. * dccp_feat_entry_new - Central list update routine (called by all others)
  164. * @head: list to add to
  165. * @feat: feature number
  166. * @local: whether the local (1) or remote feature with number @feat is meant
  167. * This is the only constructor and serves to ensure the above invariants.
  168. */
  169. static struct dccp_feat_entry *
  170. dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
  171. {
  172. struct dccp_feat_entry *entry;
  173. list_for_each_entry(entry, head, node)
  174. if (entry->feat_num == feat && entry->is_local == local) {
  175. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  176. return entry;
  177. } else if (entry->feat_num > feat) {
  178. head = &entry->node;
  179. break;
  180. }
  181. entry = kmalloc(sizeof(*entry), gfp_any());
  182. if (entry != NULL) {
  183. entry->feat_num = feat;
  184. entry->is_local = local;
  185. list_add_tail(&entry->node, head);
  186. }
  187. return entry;
  188. }
  189. /**
  190. * dccp_feat_push_change - Add/overwrite a Change option in the list
  191. * @fn_list: feature-negotiation list to update
  192. * @feat: one of %dccp_feature_numbers
  193. * @local: whether local (1) or remote (0) @feat_num is meant
  194. * @needs_mandatory: whether to use Mandatory feature negotiation options
  195. * @fval: pointer to NN/SP value to be inserted (will be copied)
  196. */
  197. static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
  198. u8 mandatory, dccp_feat_val *fval)
  199. {
  200. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  201. if (new == NULL)
  202. return -ENOMEM;
  203. new->feat_num = feat;
  204. new->is_local = local;
  205. new->state = FEAT_INITIALISING;
  206. new->needs_confirm = 0;
  207. new->empty_confirm = 0;
  208. new->val = *fval;
  209. new->needs_mandatory = mandatory;
  210. return 0;
  211. }
  212. static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
  213. {
  214. list_del(&entry->node);
  215. dccp_feat_entry_destructor(entry);
  216. }
  217. void dccp_feat_list_purge(struct list_head *fn_list)
  218. {
  219. struct dccp_feat_entry *entry, *next;
  220. list_for_each_entry_safe(entry, next, fn_list, node)
  221. dccp_feat_entry_destructor(entry);
  222. INIT_LIST_HEAD(fn_list);
  223. }
  224. EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
  225. /* generate @to as full clone of @from - @to must not contain any nodes */
  226. int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
  227. {
  228. struct dccp_feat_entry *entry, *new;
  229. INIT_LIST_HEAD(to);
  230. list_for_each_entry(entry, from, node) {
  231. new = dccp_feat_clone_entry(entry);
  232. if (new == NULL)
  233. goto cloning_failed;
  234. list_add_tail(&new->node, to);
  235. }
  236. return 0;
  237. cloning_failed:
  238. dccp_feat_list_purge(to);
  239. return -ENOMEM;
  240. }
  241. static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
  242. {
  243. switch (feat_num) {
  244. case DCCPF_ACK_RATIO:
  245. return val <= DCCPF_ACK_RATIO_MAX;
  246. case DCCPF_SEQUENCE_WINDOW:
  247. return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
  248. }
  249. return 0; /* feature unknown - so we can't tell */
  250. }
  251. /* check that SP values are within the ranges defined in RFC 4340 */
  252. static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
  253. {
  254. switch (feat_num) {
  255. case DCCPF_CCID:
  256. return val == DCCPC_CCID2 || val == DCCPC_CCID3;
  257. /* Type-check Boolean feature values: */
  258. case DCCPF_SHORT_SEQNOS:
  259. case DCCPF_ECN_INCAPABLE:
  260. case DCCPF_SEND_ACK_VECTOR:
  261. case DCCPF_SEND_NDP_COUNT:
  262. case DCCPF_DATA_CHECKSUM:
  263. case DCCPF_SEND_LEV_RATE:
  264. return val < 2;
  265. case DCCPF_MIN_CSUM_COVER:
  266. return val < 16;
  267. }
  268. return 0; /* feature unknown */
  269. }
  270. static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
  271. {
  272. if (sp_list == NULL || sp_len < 1)
  273. return 0;
  274. while (sp_len--)
  275. if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
  276. return 0;
  277. return 1;
  278. }
  279. /**
  280. * __feat_register_nn - Register new NN value on socket
  281. * @fn: feature-negotiation list to register with
  282. * @feat: an NN feature from %dccp_feature_numbers
  283. * @mandatory: use Mandatory option if 1
  284. * @nn_val: value to register (restricted to 4 bytes)
  285. * Note that NN features are local by definition (RFC 4340, 6.3.2).
  286. */
  287. static int __feat_register_nn(struct list_head *fn, u8 feat,
  288. u8 mandatory, u64 nn_val)
  289. {
  290. dccp_feat_val fval = { .nn = nn_val };
  291. if (dccp_feat_type(feat) != FEAT_NN ||
  292. !dccp_feat_is_valid_nn_val(feat, nn_val))
  293. return -EINVAL;
  294. /* Don't bother with default values, they will be activated anyway. */
  295. if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
  296. return 0;
  297. return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
  298. }
  299. /**
  300. * __feat_register_sp - Register new SP value/list on socket
  301. * @fn: feature-negotiation list to register with
  302. * @feat: an SP feature from %dccp_feature_numbers
  303. * @is_local: whether the local (1) or the remote (0) @feat is meant
  304. * @mandatory: use Mandatory option if 1
  305. * @sp_val: SP value followed by optional preference list
  306. * @sp_len: length of @sp_val in bytes
  307. */
  308. static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
  309. u8 mandatory, u8 const *sp_val, u8 sp_len)
  310. {
  311. dccp_feat_val fval;
  312. if (dccp_feat_type(feat) != FEAT_SP ||
  313. !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
  314. return -EINVAL;
  315. /* Avoid negotiating alien CCIDs by only advertising supported ones */
  316. if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
  317. return -EOPNOTSUPP;
  318. if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
  319. return -ENOMEM;
  320. return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
  321. }
  322. /**
  323. * dccp_feat_register_sp - Register requests to change SP feature values
  324. * @sk: client or listening socket
  325. * @feat: one of %dccp_feature_numbers
  326. * @is_local: whether the local (1) or remote (0) @feat is meant
  327. * @list: array of preferred values, in descending order of preference
  328. * @len: length of @list in bytes
  329. */
  330. int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
  331. u8 const *list, u8 len)
  332. { /* any changes must be registered before establishing the connection */
  333. if (sk->sk_state != DCCP_CLOSED)
  334. return -EISCONN;
  335. if (dccp_feat_type(feat) != FEAT_SP)
  336. return -EINVAL;
  337. return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
  338. 0, list, len);
  339. }
  340. /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
  341. int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
  342. {
  343. /* any changes must be registered before establishing the connection */
  344. if (sk->sk_state != DCCP_CLOSED)
  345. return -EISCONN;
  346. if (dccp_feat_type(feat) != FEAT_NN)
  347. return -EINVAL;
  348. return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
  349. }
  350. /*
  351. * Tracking features whose value depend on the choice of CCID
  352. *
  353. * This is designed with an extension in mind so that a list walk could be done
  354. * before activating any features. However, the existing framework was found to
  355. * work satisfactorily up until now, the automatic verification is left open.
  356. * When adding new CCIDs, add a corresponding dependency table here.
  357. */
  358. static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
  359. {
  360. static const struct ccid_dependency ccid2_dependencies[2][2] = {
  361. /*
  362. * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
  363. * feature and Send Ack Vector is an RX feature, `is_local'
  364. * needs to be reversed.
  365. */
  366. { /* Dependencies of the receiver-side (remote) CCID2 */
  367. {
  368. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  369. .is_local = true,
  370. .is_mandatory = true,
  371. .val = 1
  372. },
  373. { 0, 0, 0, 0 }
  374. },
  375. { /* Dependencies of the sender-side (local) CCID2 */
  376. {
  377. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  378. .is_local = false,
  379. .is_mandatory = true,
  380. .val = 1
  381. },
  382. { 0, 0, 0, 0 }
  383. }
  384. };
  385. static const struct ccid_dependency ccid3_dependencies[2][5] = {
  386. { /*
  387. * Dependencies of the receiver-side CCID3
  388. */
  389. { /* locally disable Ack Vectors */
  390. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  391. .is_local = true,
  392. .is_mandatory = false,
  393. .val = 0
  394. },
  395. { /* see below why Send Loss Event Rate is on */
  396. .dependent_feat = DCCPF_SEND_LEV_RATE,
  397. .is_local = true,
  398. .is_mandatory = true,
  399. .val = 1
  400. },
  401. { /* NDP Count is needed as per RFC 4342, 6.1.1 */
  402. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  403. .is_local = false,
  404. .is_mandatory = true,
  405. .val = 1
  406. },
  407. { 0, 0, 0, 0 },
  408. },
  409. { /*
  410. * CCID3 at the TX side: we request that the HC-receiver
  411. * will not send Ack Vectors (they will be ignored, so
  412. * Mandatory is not set); we enable Send Loss Event Rate
  413. * (Mandatory since the implementation does not support
  414. * the Loss Intervals option of RFC 4342, 8.6).
  415. * The last two options are for peer's information only.
  416. */
  417. {
  418. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  419. .is_local = false,
  420. .is_mandatory = false,
  421. .val = 0
  422. },
  423. {
  424. .dependent_feat = DCCPF_SEND_LEV_RATE,
  425. .is_local = false,
  426. .is_mandatory = true,
  427. .val = 1
  428. },
  429. { /* this CCID does not support Ack Ratio */
  430. .dependent_feat = DCCPF_ACK_RATIO,
  431. .is_local = true,
  432. .is_mandatory = false,
  433. .val = 0
  434. },
  435. { /* tell receiver we are sending NDP counts */
  436. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  437. .is_local = true,
  438. .is_mandatory = false,
  439. .val = 1
  440. },
  441. { 0, 0, 0, 0 }
  442. }
  443. };
  444. switch (ccid) {
  445. case DCCPC_CCID2:
  446. return ccid2_dependencies[is_local];
  447. case DCCPC_CCID3:
  448. return ccid3_dependencies[is_local];
  449. default:
  450. return NULL;
  451. }
  452. }
  453. /**
  454. * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
  455. * @fn: feature-negotiation list to update
  456. * @id: CCID number to track
  457. * @is_local: whether TX CCID (1) or RX CCID (0) is meant
  458. * This function needs to be called after registering all other features.
  459. */
  460. static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
  461. {
  462. const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
  463. int i, rc = (table == NULL);
  464. for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
  465. if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
  466. rc = __feat_register_sp(fn, table[i].dependent_feat,
  467. table[i].is_local,
  468. table[i].is_mandatory,
  469. &table[i].val, 1);
  470. else
  471. rc = __feat_register_nn(fn, table[i].dependent_feat,
  472. table[i].is_mandatory,
  473. table[i].val);
  474. return rc;
  475. }
  476. /**
  477. * dccp_feat_finalise_settings - Finalise settings before starting negotiation
  478. * @dp: client or listening socket (settings will be inherited)
  479. * This is called after all registrations (socket initialisation, sysctls, and
  480. * sockopt calls), and before sending the first packet containing Change options
  481. * (ie. client-Request or server-Response), to ensure internal consistency.
  482. */
  483. int dccp_feat_finalise_settings(struct dccp_sock *dp)
  484. {
  485. struct list_head *fn = &dp->dccps_featneg;
  486. struct dccp_feat_entry *entry;
  487. int i = 2, ccids[2] = { -1, -1 };
  488. /*
  489. * Propagating CCIDs:
  490. * 1) not useful to propagate CCID settings if this host advertises more
  491. * than one CCID: the choice of CCID may still change - if this is
  492. * the client, or if this is the server and the client sends
  493. * singleton CCID values.
  494. * 2) since is that propagate_ccid changes the list, we defer changing
  495. * the sorted list until after the traversal.
  496. */
  497. list_for_each_entry(entry, fn, node)
  498. if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
  499. ccids[entry->is_local] = entry->val.sp.vec[0];
  500. while (i--)
  501. if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
  502. return -1;
  503. return 0;
  504. }
  505. /**
  506. * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
  507. * It is the server which resolves the dependencies once the CCID has been
  508. * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
  509. */
  510. int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
  511. {
  512. struct list_head *fn = &dreq->dreq_featneg;
  513. struct dccp_feat_entry *entry;
  514. u8 is_local, ccid;
  515. for (is_local = 0; is_local <= 1; is_local++) {
  516. entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
  517. if (entry != NULL && !entry->empty_confirm)
  518. ccid = entry->val.sp.vec[0];
  519. else
  520. ccid = dccp_feat_default_value(DCCPF_CCID);
  521. if (dccp_feat_propagate_ccid(fn, ccid, is_local))
  522. return -1;
  523. }
  524. return 0;
  525. }
  526. static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
  527. {
  528. struct dccp_sock *dp = dccp_sk(sk);
  529. struct dccp_minisock *dmsk = dccp_msk(sk);
  530. /* figure out if we are changing our CCID or the peer's */
  531. const int rx = type == DCCPO_CHANGE_R;
  532. const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
  533. struct ccid *new_ccid;
  534. /* Check if nothing is being changed. */
  535. if (ccid_nr == new_ccid_nr)
  536. return 0;
  537. new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
  538. if (new_ccid == NULL)
  539. return -ENOMEM;
  540. if (rx) {
  541. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  542. dp->dccps_hc_rx_ccid = new_ccid;
  543. dmsk->dccpms_rx_ccid = new_ccid_nr;
  544. } else {
  545. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  546. dp->dccps_hc_tx_ccid = new_ccid;
  547. dmsk->dccpms_tx_ccid = new_ccid_nr;
  548. }
  549. return 0;
  550. }
  551. static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
  552. {
  553. dccp_feat_debug(type, feat, val);
  554. switch (feat) {
  555. case DCCPF_CCID:
  556. return dccp_feat_update_ccid(sk, type, val);
  557. default:
  558. dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
  559. dccp_feat_typename(type), feat);
  560. break;
  561. }
  562. return 0;
  563. }
  564. static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
  565. u8 *rpref, u8 rlen)
  566. {
  567. struct dccp_sock *dp = dccp_sk(sk);
  568. u8 *spref, slen, *res = NULL;
  569. int i, j, rc, agree = 1;
  570. BUG_ON(rpref == NULL);
  571. /* check if we are the black sheep */
  572. if (dp->dccps_role == DCCP_ROLE_CLIENT) {
  573. spref = rpref;
  574. slen = rlen;
  575. rpref = opt->dccpop_val;
  576. rlen = opt->dccpop_len;
  577. } else {
  578. spref = opt->dccpop_val;
  579. slen = opt->dccpop_len;
  580. }
  581. /*
  582. * Now we have server preference list in spref and client preference in
  583. * rpref
  584. */
  585. BUG_ON(spref == NULL);
  586. BUG_ON(rpref == NULL);
  587. /* FIXME sanity check vals */
  588. /* Are values in any order? XXX Lame "algorithm" here */
  589. for (i = 0; i < slen; i++) {
  590. for (j = 0; j < rlen; j++) {
  591. if (spref[i] == rpref[j]) {
  592. res = &spref[i];
  593. break;
  594. }
  595. }
  596. if (res)
  597. break;
  598. }
  599. /* we didn't agree on anything */
  600. if (res == NULL) {
  601. /* confirm previous value */
  602. switch (opt->dccpop_feat) {
  603. case DCCPF_CCID:
  604. /* XXX did i get this right? =P */
  605. if (opt->dccpop_type == DCCPO_CHANGE_L)
  606. res = &dccp_msk(sk)->dccpms_tx_ccid;
  607. else
  608. res = &dccp_msk(sk)->dccpms_rx_ccid;
  609. break;
  610. default:
  611. DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
  612. /* XXX implement res */
  613. return -EFAULT;
  614. }
  615. dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
  616. agree = 0; /* this is used for mandatory options... */
  617. }
  618. /* need to put result and our preference list */
  619. rlen = 1 + opt->dccpop_len;
  620. rpref = kmalloc(rlen, GFP_ATOMIC);
  621. if (rpref == NULL)
  622. return -ENOMEM;
  623. *rpref = *res;
  624. memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
  625. /* put it in the "confirm queue" */
  626. if (opt->dccpop_sc == NULL) {
  627. opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
  628. if (opt->dccpop_sc == NULL) {
  629. kfree(rpref);
  630. return -ENOMEM;
  631. }
  632. } else {
  633. /* recycle the confirm slot */
  634. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  635. kfree(opt->dccpop_sc->dccpoc_val);
  636. dccp_pr_debug("recycling confirm slot\n");
  637. }
  638. memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
  639. opt->dccpop_sc->dccpoc_val = rpref;
  640. opt->dccpop_sc->dccpoc_len = rlen;
  641. /* update the option on our side [we are about to send the confirm] */
  642. rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
  643. if (rc) {
  644. kfree(opt->dccpop_sc->dccpoc_val);
  645. kfree(opt->dccpop_sc);
  646. opt->dccpop_sc = NULL;
  647. return rc;
  648. }
  649. dccp_pr_debug("Will confirm %d\n", *rpref);
  650. /* say we want to change to X but we just got a confirm X, suppress our
  651. * change
  652. */
  653. if (!opt->dccpop_conf) {
  654. if (*opt->dccpop_val == *res)
  655. opt->dccpop_conf = 1;
  656. dccp_pr_debug("won't ask for change of same feature\n");
  657. }
  658. return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
  659. }
  660. static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  661. {
  662. struct dccp_minisock *dmsk = dccp_msk(sk);
  663. struct dccp_opt_pend *opt;
  664. int rc = 1;
  665. u8 t;
  666. /*
  667. * We received a CHANGE. We gotta match it against our own preference
  668. * list. If we got a CHANGE_R it means it's a change for us, so we need
  669. * to compare our CHANGE_L list.
  670. */
  671. if (type == DCCPO_CHANGE_L)
  672. t = DCCPO_CHANGE_R;
  673. else
  674. t = DCCPO_CHANGE_L;
  675. /* find our preference list for this feature */
  676. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  677. if (opt->dccpop_type != t || opt->dccpop_feat != feature)
  678. continue;
  679. /* find the winner from the two preference lists */
  680. rc = dccp_feat_reconcile(sk, opt, val, len);
  681. break;
  682. }
  683. /* We didn't deal with the change. This can happen if we have no
  684. * preference list for the feature. In fact, it just shouldn't
  685. * happen---if we understand a feature, we should have a preference list
  686. * with at least the default value.
  687. */
  688. BUG_ON(rc == 1);
  689. return rc;
  690. }
  691. static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  692. {
  693. struct dccp_opt_pend *opt;
  694. struct dccp_minisock *dmsk = dccp_msk(sk);
  695. u8 *copy;
  696. int rc;
  697. /* NN features must be Change L (sec. 6.3.2) */
  698. if (type != DCCPO_CHANGE_L) {
  699. dccp_pr_debug("received %s for NN feature %d\n",
  700. dccp_feat_typename(type), feature);
  701. return -EFAULT;
  702. }
  703. /* XXX sanity check opt val */
  704. /* copy option so we can confirm it */
  705. opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  706. if (opt == NULL)
  707. return -ENOMEM;
  708. copy = kmemdup(val, len, GFP_ATOMIC);
  709. if (copy == NULL) {
  710. kfree(opt);
  711. return -ENOMEM;
  712. }
  713. opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
  714. opt->dccpop_feat = feature;
  715. opt->dccpop_val = copy;
  716. opt->dccpop_len = len;
  717. /* change feature */
  718. rc = dccp_feat_update(sk, type, feature, *val);
  719. if (rc) {
  720. kfree(opt->dccpop_val);
  721. kfree(opt);
  722. return rc;
  723. }
  724. dccp_feat_debug(type, feature, *copy);
  725. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  726. return 0;
  727. }
  728. static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
  729. u8 type, u8 feature)
  730. {
  731. /* XXX check if other confirms for that are queued and recycle slot */
  732. struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  733. if (opt == NULL) {
  734. /* XXX what do we do? Ignoring should be fine. It's a change
  735. * after all =P
  736. */
  737. return;
  738. }
  739. switch (type) {
  740. case DCCPO_CHANGE_L:
  741. opt->dccpop_type = DCCPO_CONFIRM_R;
  742. break;
  743. case DCCPO_CHANGE_R:
  744. opt->dccpop_type = DCCPO_CONFIRM_L;
  745. break;
  746. default:
  747. DCCP_WARN("invalid type %d\n", type);
  748. kfree(opt);
  749. return;
  750. }
  751. opt->dccpop_feat = feature;
  752. opt->dccpop_val = NULL;
  753. opt->dccpop_len = 0;
  754. /* change feature */
  755. dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
  756. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  757. }
  758. static void dccp_feat_flush_confirm(struct sock *sk)
  759. {
  760. struct dccp_minisock *dmsk = dccp_msk(sk);
  761. /* Check if there is anything to confirm in the first place */
  762. int yes = !list_empty(&dmsk->dccpms_conf);
  763. if (!yes) {
  764. struct dccp_opt_pend *opt;
  765. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  766. if (opt->dccpop_conf) {
  767. yes = 1;
  768. break;
  769. }
  770. }
  771. }
  772. if (!yes)
  773. return;
  774. /* OK there is something to confirm... */
  775. /* XXX check if packet is in flight? Send delayed ack?? */
  776. if (sk->sk_state == DCCP_OPEN)
  777. dccp_send_ack(sk);
  778. }
  779. int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  780. {
  781. int rc;
  782. /* Ignore Change requests other than during connection setup */
  783. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  784. return 0;
  785. dccp_feat_debug(type, feature, *val);
  786. /* figure out if it's SP or NN feature */
  787. switch (feature) {
  788. /* deal with SP features */
  789. case DCCPF_CCID:
  790. rc = dccp_feat_sp(sk, type, feature, val, len);
  791. break;
  792. /* deal with NN features */
  793. case DCCPF_ACK_RATIO:
  794. rc = dccp_feat_nn(sk, type, feature, val, len);
  795. break;
  796. /* XXX implement other features */
  797. default:
  798. dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
  799. dccp_feat_typename(type), feature);
  800. rc = -EFAULT;
  801. break;
  802. }
  803. /* check if there were problems changing features */
  804. if (rc) {
  805. /* If we don't agree on SP, we sent a confirm for old value.
  806. * However we propagate rc to caller in case option was
  807. * mandatory
  808. */
  809. if (rc != DCCP_FEAT_SP_NOAGREE)
  810. dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
  811. }
  812. /* generate the confirm [if required] */
  813. dccp_feat_flush_confirm(sk);
  814. return rc;
  815. }
  816. EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
  817. int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
  818. u8 *val, u8 len)
  819. {
  820. u8 t;
  821. struct dccp_opt_pend *opt;
  822. struct dccp_minisock *dmsk = dccp_msk(sk);
  823. int found = 0;
  824. int all_confirmed = 1;
  825. /* Ignore Confirm options other than during connection setup */
  826. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  827. return 0;
  828. dccp_feat_debug(type, feature, *val);
  829. /* locate our change request */
  830. switch (type) {
  831. case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
  832. case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
  833. default: DCCP_WARN("invalid type %d\n", type);
  834. return 1;
  835. }
  836. /* XXX sanity check feature value */
  837. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  838. if (!opt->dccpop_conf && opt->dccpop_type == t &&
  839. opt->dccpop_feat == feature) {
  840. found = 1;
  841. dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
  842. /* XXX do sanity check */
  843. opt->dccpop_conf = 1;
  844. /* We got a confirmation---change the option */
  845. dccp_feat_update(sk, opt->dccpop_type,
  846. opt->dccpop_feat, *val);
  847. /* XXX check the return value of dccp_feat_update */
  848. break;
  849. }
  850. if (!opt->dccpop_conf)
  851. all_confirmed = 0;
  852. }
  853. if (!found)
  854. dccp_pr_debug("%s(%d, ...) never requested\n",
  855. dccp_feat_typename(type), feature);
  856. return 0;
  857. }
  858. EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
  859. void dccp_feat_clean(struct dccp_minisock *dmsk)
  860. {
  861. struct dccp_opt_pend *opt, *next;
  862. list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
  863. dccpop_node) {
  864. BUG_ON(opt->dccpop_val == NULL);
  865. kfree(opt->dccpop_val);
  866. if (opt->dccpop_sc != NULL) {
  867. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  868. kfree(opt->dccpop_sc->dccpoc_val);
  869. kfree(opt->dccpop_sc);
  870. }
  871. kfree(opt);
  872. }
  873. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  874. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  875. BUG_ON(opt == NULL);
  876. if (opt->dccpop_val != NULL)
  877. kfree(opt->dccpop_val);
  878. kfree(opt);
  879. }
  880. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  881. }
  882. EXPORT_SYMBOL_GPL(dccp_feat_clean);
  883. /* this is to be called only when a listening sock creates its child. It is
  884. * assumed by the function---the confirm is not duplicated, but rather it is
  885. * "passed on".
  886. */
  887. int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
  888. {
  889. struct dccp_minisock *olddmsk = dccp_msk(oldsk);
  890. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  891. struct dccp_opt_pend *opt;
  892. int rc = 0;
  893. INIT_LIST_HEAD(&newdmsk->dccpms_pending);
  894. INIT_LIST_HEAD(&newdmsk->dccpms_conf);
  895. list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
  896. struct dccp_opt_pend *newopt;
  897. /* copy the value of the option */
  898. u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
  899. if (val == NULL)
  900. goto out_clean;
  901. newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
  902. if (newopt == NULL) {
  903. kfree(val);
  904. goto out_clean;
  905. }
  906. /* insert the option */
  907. newopt->dccpop_val = val;
  908. list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
  909. /* XXX what happens with backlogs and multiple connections at
  910. * once...
  911. */
  912. /* the master socket no longer needs to worry about confirms */
  913. opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
  914. /* reset state for a new socket */
  915. opt->dccpop_conf = 0;
  916. }
  917. /* XXX not doing anything about the conf queue */
  918. out:
  919. return rc;
  920. out_clean:
  921. dccp_feat_clean(newdmsk);
  922. rc = -ENOMEM;
  923. goto out;
  924. }
  925. EXPORT_SYMBOL_GPL(dccp_feat_clone);
  926. int dccp_feat_init(struct sock *sk)
  927. {
  928. struct dccp_sock *dp = dccp_sk(sk);
  929. struct dccp_minisock *dmsk = dccp_msk(sk);
  930. int rc;
  931. INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
  932. INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
  933. /* CCID L */
  934. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
  935. &dmsk->dccpms_tx_ccid, 1);
  936. if (rc)
  937. goto out;
  938. /* CCID R */
  939. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
  940. &dmsk->dccpms_rx_ccid, 1);
  941. if (rc)
  942. goto out;
  943. /* Ack ratio */
  944. rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
  945. dp->dccps_l_ack_ratio);
  946. out:
  947. return rc;
  948. }
  949. EXPORT_SYMBOL_GPL(dccp_feat_init);
  950. #ifdef CONFIG_IP_DCCP_DEBUG
  951. const char *dccp_feat_typename(const u8 type)
  952. {
  953. switch(type) {
  954. case DCCPO_CHANGE_L: return("ChangeL");
  955. case DCCPO_CONFIRM_L: return("ConfirmL");
  956. case DCCPO_CHANGE_R: return("ChangeR");
  957. case DCCPO_CONFIRM_R: return("ConfirmR");
  958. /* the following case must not appear in feature negotation */
  959. default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
  960. }
  961. return NULL;
  962. }
  963. EXPORT_SYMBOL_GPL(dccp_feat_typename);
  964. const char *dccp_feat_name(const u8 feat)
  965. {
  966. static const char *feature_names[] = {
  967. [DCCPF_RESERVED] = "Reserved",
  968. [DCCPF_CCID] = "CCID",
  969. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  970. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  971. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  972. [DCCPF_ACK_RATIO] = "Ack Ratio",
  973. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  974. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  975. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  976. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  977. };
  978. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  979. return feature_names[DCCPF_RESERVED];
  980. if (feat == DCCPF_SEND_LEV_RATE)
  981. return "Send Loss Event Rate";
  982. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  983. return "CCID-specific";
  984. return feature_names[feat];
  985. }
  986. EXPORT_SYMBOL_GPL(dccp_feat_name);
  987. #endif /* CONFIG_IP_DCCP_DEBUG */