feat.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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. /* Test for "Req'd" feature (RFC 4340, 6.4) */
  97. static inline int dccp_feat_must_be_understood(u8 feat_num)
  98. {
  99. return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
  100. feat_num == DCCPF_SEQUENCE_WINDOW;
  101. }
  102. /* copy constructor, fval must not already contain allocated memory */
  103. static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
  104. {
  105. fval->sp.len = len;
  106. if (fval->sp.len > 0) {
  107. fval->sp.vec = kmemdup(val, len, gfp_any());
  108. if (fval->sp.vec == NULL) {
  109. fval->sp.len = 0;
  110. return -ENOBUFS;
  111. }
  112. }
  113. return 0;
  114. }
  115. static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
  116. {
  117. if (unlikely(val == NULL))
  118. return;
  119. if (dccp_feat_type(feat_num) == FEAT_SP)
  120. kfree(val->sp.vec);
  121. memset(val, 0, sizeof(*val));
  122. }
  123. static struct dccp_feat_entry *
  124. dccp_feat_clone_entry(struct dccp_feat_entry const *original)
  125. {
  126. struct dccp_feat_entry *new;
  127. u8 type = dccp_feat_type(original->feat_num);
  128. if (type == FEAT_UNKNOWN)
  129. return NULL;
  130. new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
  131. if (new == NULL)
  132. return NULL;
  133. if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
  134. original->val.sp.vec,
  135. original->val.sp.len)) {
  136. kfree(new);
  137. return NULL;
  138. }
  139. return new;
  140. }
  141. static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
  142. {
  143. if (entry != NULL) {
  144. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  145. kfree(entry);
  146. }
  147. }
  148. /*
  149. * List management functions
  150. *
  151. * Feature negotiation lists rely on and maintain the following invariants:
  152. * - each feat_num in the list is known, i.e. we know its type and default value
  153. * - each feat_num/is_local combination is unique (old entries are overwritten)
  154. * - SP values are always freshly allocated
  155. * - list is sorted in increasing order of feature number (faster lookup)
  156. */
  157. static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
  158. u8 feat_num, bool is_local)
  159. {
  160. struct dccp_feat_entry *entry;
  161. list_for_each_entry(entry, fn_list, node) {
  162. if (entry->feat_num == feat_num && entry->is_local == is_local)
  163. return entry;
  164. else if (entry->feat_num > feat_num)
  165. break;
  166. }
  167. return NULL;
  168. }
  169. /**
  170. * dccp_feat_entry_new - Central list update routine (called by all others)
  171. * @head: list to add to
  172. * @feat: feature number
  173. * @local: whether the local (1) or remote feature with number @feat is meant
  174. * This is the only constructor and serves to ensure the above invariants.
  175. */
  176. static struct dccp_feat_entry *
  177. dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
  178. {
  179. struct dccp_feat_entry *entry;
  180. list_for_each_entry(entry, head, node)
  181. if (entry->feat_num == feat && entry->is_local == local) {
  182. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  183. return entry;
  184. } else if (entry->feat_num > feat) {
  185. head = &entry->node;
  186. break;
  187. }
  188. entry = kmalloc(sizeof(*entry), gfp_any());
  189. if (entry != NULL) {
  190. entry->feat_num = feat;
  191. entry->is_local = local;
  192. list_add_tail(&entry->node, head);
  193. }
  194. return entry;
  195. }
  196. /**
  197. * dccp_feat_push_change - Add/overwrite a Change option in the list
  198. * @fn_list: feature-negotiation list to update
  199. * @feat: one of %dccp_feature_numbers
  200. * @local: whether local (1) or remote (0) @feat_num is meant
  201. * @needs_mandatory: whether to use Mandatory feature negotiation options
  202. * @fval: pointer to NN/SP value to be inserted (will be copied)
  203. */
  204. static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
  205. u8 mandatory, dccp_feat_val *fval)
  206. {
  207. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  208. if (new == NULL)
  209. return -ENOMEM;
  210. new->feat_num = feat;
  211. new->is_local = local;
  212. new->state = FEAT_INITIALISING;
  213. new->needs_confirm = 0;
  214. new->empty_confirm = 0;
  215. new->val = *fval;
  216. new->needs_mandatory = mandatory;
  217. return 0;
  218. }
  219. /**
  220. * dccp_feat_push_confirm - Add a Confirm entry to the FN list
  221. * @fn_list: feature-negotiation list to add to
  222. * @feat: one of %dccp_feature_numbers
  223. * @local: whether local (1) or remote (0) @feat_num is being confirmed
  224. * @fval: pointer to NN/SP value to be inserted or NULL
  225. * Returns 0 on success, a Reset code for further processing otherwise.
  226. */
  227. static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
  228. dccp_feat_val *fval)
  229. {
  230. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  231. if (new == NULL)
  232. return DCCP_RESET_CODE_TOO_BUSY;
  233. new->feat_num = feat;
  234. new->is_local = local;
  235. new->state = FEAT_STABLE; /* transition in 6.6.2 */
  236. new->needs_confirm = 1;
  237. new->empty_confirm = (fval == NULL);
  238. new->val.nn = 0; /* zeroes the whole structure */
  239. if (!new->empty_confirm)
  240. new->val = *fval;
  241. new->needs_mandatory = 0;
  242. return 0;
  243. }
  244. static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
  245. {
  246. return dccp_feat_push_confirm(fn_list, feat, local, NULL);
  247. }
  248. static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
  249. {
  250. list_del(&entry->node);
  251. dccp_feat_entry_destructor(entry);
  252. }
  253. void dccp_feat_list_purge(struct list_head *fn_list)
  254. {
  255. struct dccp_feat_entry *entry, *next;
  256. list_for_each_entry_safe(entry, next, fn_list, node)
  257. dccp_feat_entry_destructor(entry);
  258. INIT_LIST_HEAD(fn_list);
  259. }
  260. EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
  261. /* generate @to as full clone of @from - @to must not contain any nodes */
  262. int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
  263. {
  264. struct dccp_feat_entry *entry, *new;
  265. INIT_LIST_HEAD(to);
  266. list_for_each_entry(entry, from, node) {
  267. new = dccp_feat_clone_entry(entry);
  268. if (new == NULL)
  269. goto cloning_failed;
  270. list_add_tail(&new->node, to);
  271. }
  272. return 0;
  273. cloning_failed:
  274. dccp_feat_list_purge(to);
  275. return -ENOMEM;
  276. }
  277. /**
  278. * dccp_feat_valid_nn_length - Enforce length constraints on NN options
  279. * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
  280. * incoming options are accepted as long as their values are valid.
  281. */
  282. static u8 dccp_feat_valid_nn_length(u8 feat_num)
  283. {
  284. if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
  285. return 2;
  286. if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
  287. return 6;
  288. return 0;
  289. }
  290. static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
  291. {
  292. switch (feat_num) {
  293. case DCCPF_ACK_RATIO:
  294. return val <= DCCPF_ACK_RATIO_MAX;
  295. case DCCPF_SEQUENCE_WINDOW:
  296. return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
  297. }
  298. return 0; /* feature unknown - so we can't tell */
  299. }
  300. /* check that SP values are within the ranges defined in RFC 4340 */
  301. static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
  302. {
  303. switch (feat_num) {
  304. case DCCPF_CCID:
  305. return val == DCCPC_CCID2 || val == DCCPC_CCID3;
  306. /* Type-check Boolean feature values: */
  307. case DCCPF_SHORT_SEQNOS:
  308. case DCCPF_ECN_INCAPABLE:
  309. case DCCPF_SEND_ACK_VECTOR:
  310. case DCCPF_SEND_NDP_COUNT:
  311. case DCCPF_DATA_CHECKSUM:
  312. case DCCPF_SEND_LEV_RATE:
  313. return val < 2;
  314. case DCCPF_MIN_CSUM_COVER:
  315. return val < 16;
  316. }
  317. return 0; /* feature unknown */
  318. }
  319. static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
  320. {
  321. if (sp_list == NULL || sp_len < 1)
  322. return 0;
  323. while (sp_len--)
  324. if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
  325. return 0;
  326. return 1;
  327. }
  328. /**
  329. * dccp_feat_insert_opts - Generate FN options from current list state
  330. * @skb: next sk_buff to be sent to the peer
  331. * @dp: for client during handshake and general negotiation
  332. * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
  333. */
  334. int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
  335. struct sk_buff *skb)
  336. {
  337. struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
  338. struct dccp_feat_entry *pos, *next;
  339. u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
  340. bool rpt;
  341. /* put entries into @skb in the order they appear in the list */
  342. list_for_each_entry_safe_reverse(pos, next, fn, node) {
  343. opt = dccp_feat_genopt(pos);
  344. type = dccp_feat_type(pos->feat_num);
  345. rpt = false;
  346. if (pos->empty_confirm) {
  347. len = 0;
  348. ptr = NULL;
  349. } else {
  350. if (type == FEAT_SP) {
  351. len = pos->val.sp.len;
  352. ptr = pos->val.sp.vec;
  353. rpt = pos->needs_confirm;
  354. } else if (type == FEAT_NN) {
  355. len = dccp_feat_valid_nn_length(pos->feat_num);
  356. ptr = nn_in_nbo;
  357. dccp_encode_value_var(pos->val.nn, ptr, len);
  358. } else {
  359. DCCP_BUG("unknown feature %u", pos->feat_num);
  360. return -1;
  361. }
  362. }
  363. if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
  364. return -1;
  365. if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
  366. return -1;
  367. /*
  368. * Enter CHANGING after transmitting the Change option (6.6.2).
  369. */
  370. if (pos->state == FEAT_INITIALISING)
  371. pos->state = FEAT_CHANGING;
  372. }
  373. return 0;
  374. }
  375. /**
  376. * __feat_register_nn - Register new NN value on socket
  377. * @fn: feature-negotiation list to register with
  378. * @feat: an NN feature from %dccp_feature_numbers
  379. * @mandatory: use Mandatory option if 1
  380. * @nn_val: value to register (restricted to 4 bytes)
  381. * Note that NN features are local by definition (RFC 4340, 6.3.2).
  382. */
  383. static int __feat_register_nn(struct list_head *fn, u8 feat,
  384. u8 mandatory, u64 nn_val)
  385. {
  386. dccp_feat_val fval = { .nn = nn_val };
  387. if (dccp_feat_type(feat) != FEAT_NN ||
  388. !dccp_feat_is_valid_nn_val(feat, nn_val))
  389. return -EINVAL;
  390. /* Don't bother with default values, they will be activated anyway. */
  391. if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
  392. return 0;
  393. return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
  394. }
  395. /**
  396. * __feat_register_sp - Register new SP value/list on socket
  397. * @fn: feature-negotiation list to register with
  398. * @feat: an SP feature from %dccp_feature_numbers
  399. * @is_local: whether the local (1) or the remote (0) @feat is meant
  400. * @mandatory: use Mandatory option if 1
  401. * @sp_val: SP value followed by optional preference list
  402. * @sp_len: length of @sp_val in bytes
  403. */
  404. static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
  405. u8 mandatory, u8 const *sp_val, u8 sp_len)
  406. {
  407. dccp_feat_val fval;
  408. if (dccp_feat_type(feat) != FEAT_SP ||
  409. !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
  410. return -EINVAL;
  411. /* Avoid negotiating alien CCIDs by only advertising supported ones */
  412. if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
  413. return -EOPNOTSUPP;
  414. if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
  415. return -ENOMEM;
  416. return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
  417. }
  418. /**
  419. * dccp_feat_register_sp - Register requests to change SP feature values
  420. * @sk: client or listening socket
  421. * @feat: one of %dccp_feature_numbers
  422. * @is_local: whether the local (1) or remote (0) @feat is meant
  423. * @list: array of preferred values, in descending order of preference
  424. * @len: length of @list in bytes
  425. */
  426. int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
  427. u8 const *list, u8 len)
  428. { /* any changes must be registered before establishing the connection */
  429. if (sk->sk_state != DCCP_CLOSED)
  430. return -EISCONN;
  431. if (dccp_feat_type(feat) != FEAT_SP)
  432. return -EINVAL;
  433. return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
  434. 0, list, len);
  435. }
  436. /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
  437. int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
  438. {
  439. /* any changes must be registered before establishing the connection */
  440. if (sk->sk_state != DCCP_CLOSED)
  441. return -EISCONN;
  442. if (dccp_feat_type(feat) != FEAT_NN)
  443. return -EINVAL;
  444. return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
  445. }
  446. /*
  447. * Tracking features whose value depend on the choice of CCID
  448. *
  449. * This is designed with an extension in mind so that a list walk could be done
  450. * before activating any features. However, the existing framework was found to
  451. * work satisfactorily up until now, the automatic verification is left open.
  452. * When adding new CCIDs, add a corresponding dependency table here.
  453. */
  454. static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
  455. {
  456. static const struct ccid_dependency ccid2_dependencies[2][2] = {
  457. /*
  458. * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
  459. * feature and Send Ack Vector is an RX feature, `is_local'
  460. * needs to be reversed.
  461. */
  462. { /* Dependencies of the receiver-side (remote) CCID2 */
  463. {
  464. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  465. .is_local = true,
  466. .is_mandatory = true,
  467. .val = 1
  468. },
  469. { 0, 0, 0, 0 }
  470. },
  471. { /* Dependencies of the sender-side (local) CCID2 */
  472. {
  473. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  474. .is_local = false,
  475. .is_mandatory = true,
  476. .val = 1
  477. },
  478. { 0, 0, 0, 0 }
  479. }
  480. };
  481. static const struct ccid_dependency ccid3_dependencies[2][5] = {
  482. { /*
  483. * Dependencies of the receiver-side CCID3
  484. */
  485. { /* locally disable Ack Vectors */
  486. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  487. .is_local = true,
  488. .is_mandatory = false,
  489. .val = 0
  490. },
  491. { /* see below why Send Loss Event Rate is on */
  492. .dependent_feat = DCCPF_SEND_LEV_RATE,
  493. .is_local = true,
  494. .is_mandatory = true,
  495. .val = 1
  496. },
  497. { /* NDP Count is needed as per RFC 4342, 6.1.1 */
  498. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  499. .is_local = false,
  500. .is_mandatory = true,
  501. .val = 1
  502. },
  503. { 0, 0, 0, 0 },
  504. },
  505. { /*
  506. * CCID3 at the TX side: we request that the HC-receiver
  507. * will not send Ack Vectors (they will be ignored, so
  508. * Mandatory is not set); we enable Send Loss Event Rate
  509. * (Mandatory since the implementation does not support
  510. * the Loss Intervals option of RFC 4342, 8.6).
  511. * The last two options are for peer's information only.
  512. */
  513. {
  514. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  515. .is_local = false,
  516. .is_mandatory = false,
  517. .val = 0
  518. },
  519. {
  520. .dependent_feat = DCCPF_SEND_LEV_RATE,
  521. .is_local = false,
  522. .is_mandatory = true,
  523. .val = 1
  524. },
  525. { /* this CCID does not support Ack Ratio */
  526. .dependent_feat = DCCPF_ACK_RATIO,
  527. .is_local = true,
  528. .is_mandatory = false,
  529. .val = 0
  530. },
  531. { /* tell receiver we are sending NDP counts */
  532. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  533. .is_local = true,
  534. .is_mandatory = false,
  535. .val = 1
  536. },
  537. { 0, 0, 0, 0 }
  538. }
  539. };
  540. switch (ccid) {
  541. case DCCPC_CCID2:
  542. return ccid2_dependencies[is_local];
  543. case DCCPC_CCID3:
  544. return ccid3_dependencies[is_local];
  545. default:
  546. return NULL;
  547. }
  548. }
  549. /**
  550. * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
  551. * @fn: feature-negotiation list to update
  552. * @id: CCID number to track
  553. * @is_local: whether TX CCID (1) or RX CCID (0) is meant
  554. * This function needs to be called after registering all other features.
  555. */
  556. static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
  557. {
  558. const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
  559. int i, rc = (table == NULL);
  560. for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
  561. if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
  562. rc = __feat_register_sp(fn, table[i].dependent_feat,
  563. table[i].is_local,
  564. table[i].is_mandatory,
  565. &table[i].val, 1);
  566. else
  567. rc = __feat_register_nn(fn, table[i].dependent_feat,
  568. table[i].is_mandatory,
  569. table[i].val);
  570. return rc;
  571. }
  572. /**
  573. * dccp_feat_finalise_settings - Finalise settings before starting negotiation
  574. * @dp: client or listening socket (settings will be inherited)
  575. * This is called after all registrations (socket initialisation, sysctls, and
  576. * sockopt calls), and before sending the first packet containing Change options
  577. * (ie. client-Request or server-Response), to ensure internal consistency.
  578. */
  579. int dccp_feat_finalise_settings(struct dccp_sock *dp)
  580. {
  581. struct list_head *fn = &dp->dccps_featneg;
  582. struct dccp_feat_entry *entry;
  583. int i = 2, ccids[2] = { -1, -1 };
  584. /*
  585. * Propagating CCIDs:
  586. * 1) not useful to propagate CCID settings if this host advertises more
  587. * than one CCID: the choice of CCID may still change - if this is
  588. * the client, or if this is the server and the client sends
  589. * singleton CCID values.
  590. * 2) since is that propagate_ccid changes the list, we defer changing
  591. * the sorted list until after the traversal.
  592. */
  593. list_for_each_entry(entry, fn, node)
  594. if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
  595. ccids[entry->is_local] = entry->val.sp.vec[0];
  596. while (i--)
  597. if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
  598. return -1;
  599. return 0;
  600. }
  601. /**
  602. * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
  603. * It is the server which resolves the dependencies once the CCID has been
  604. * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
  605. */
  606. int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
  607. {
  608. struct list_head *fn = &dreq->dreq_featneg;
  609. struct dccp_feat_entry *entry;
  610. u8 is_local, ccid;
  611. for (is_local = 0; is_local <= 1; is_local++) {
  612. entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
  613. if (entry != NULL && !entry->empty_confirm)
  614. ccid = entry->val.sp.vec[0];
  615. else
  616. ccid = dccp_feat_default_value(DCCPF_CCID);
  617. if (dccp_feat_propagate_ccid(fn, ccid, is_local))
  618. return -1;
  619. }
  620. return 0;
  621. }
  622. static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
  623. {
  624. struct dccp_sock *dp = dccp_sk(sk);
  625. struct dccp_minisock *dmsk = dccp_msk(sk);
  626. /* figure out if we are changing our CCID or the peer's */
  627. const int rx = type == DCCPO_CHANGE_R;
  628. const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
  629. struct ccid *new_ccid;
  630. /* Check if nothing is being changed. */
  631. if (ccid_nr == new_ccid_nr)
  632. return 0;
  633. new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
  634. if (new_ccid == NULL)
  635. return -ENOMEM;
  636. if (rx) {
  637. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  638. dp->dccps_hc_rx_ccid = new_ccid;
  639. dmsk->dccpms_rx_ccid = new_ccid_nr;
  640. } else {
  641. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  642. dp->dccps_hc_tx_ccid = new_ccid;
  643. dmsk->dccpms_tx_ccid = new_ccid_nr;
  644. }
  645. return 0;
  646. }
  647. static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
  648. {
  649. dccp_feat_debug(type, feat, val);
  650. switch (feat) {
  651. case DCCPF_CCID:
  652. return dccp_feat_update_ccid(sk, type, val);
  653. default:
  654. dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
  655. dccp_feat_typename(type), feat);
  656. break;
  657. }
  658. return 0;
  659. }
  660. /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
  661. static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
  662. {
  663. u8 c, s;
  664. for (s = 0; s < slen; s++)
  665. for (c = 0; c < clen; c++)
  666. if (servlist[s] == clilist[c])
  667. return servlist[s];
  668. return -1;
  669. }
  670. /**
  671. * dccp_feat_prefer - Move preferred entry to the start of array
  672. * Reorder the @array_len elements in @array so that @preferred_value comes
  673. * first. Returns >0 to indicate that @preferred_value does occur in @array.
  674. */
  675. static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
  676. {
  677. u8 i, does_occur = 0;
  678. if (array != NULL) {
  679. for (i = 0; i < array_len; i++)
  680. if (array[i] == preferred_value) {
  681. array[i] = array[0];
  682. does_occur++;
  683. }
  684. if (does_occur)
  685. array[0] = preferred_value;
  686. }
  687. return does_occur;
  688. }
  689. /**
  690. * dccp_feat_reconcile - Reconcile SP preference lists
  691. * @fval: SP list to reconcile into
  692. * @arr: received SP preference list
  693. * @len: length of @arr in bytes
  694. * @is_server: whether this side is the server (and @fv is the server's list)
  695. * @reorder: whether to reorder the list in @fv after reconciling with @arr
  696. * When successful, > 0 is returned and the reconciled list is in @fval.
  697. * A value of 0 means that negotiation failed (no shared entry).
  698. */
  699. static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
  700. bool is_server, bool reorder)
  701. {
  702. int rc;
  703. if (!fv->sp.vec || !arr) {
  704. DCCP_CRIT("NULL feature value or array");
  705. return 0;
  706. }
  707. if (is_server)
  708. rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
  709. else
  710. rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
  711. if (!reorder)
  712. return rc;
  713. if (rc < 0)
  714. return 0;
  715. /*
  716. * Reorder list: used for activating features and in dccp_insert_fn_opt.
  717. */
  718. return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
  719. }
  720. #ifdef __this_is_the_old_framework_and_will_be_removed_later_in_a_subsequent_patch
  721. static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
  722. u8 *rpref, u8 rlen)
  723. {
  724. struct dccp_sock *dp = dccp_sk(sk);
  725. u8 *spref, slen, *res = NULL;
  726. int i, j, rc, agree = 1;
  727. BUG_ON(rpref == NULL);
  728. /* check if we are the black sheep */
  729. if (dp->dccps_role == DCCP_ROLE_CLIENT) {
  730. spref = rpref;
  731. slen = rlen;
  732. rpref = opt->dccpop_val;
  733. rlen = opt->dccpop_len;
  734. } else {
  735. spref = opt->dccpop_val;
  736. slen = opt->dccpop_len;
  737. }
  738. /*
  739. * Now we have server preference list in spref and client preference in
  740. * rpref
  741. */
  742. BUG_ON(spref == NULL);
  743. BUG_ON(rpref == NULL);
  744. /* FIXME sanity check vals */
  745. /* Are values in any order? XXX Lame "algorithm" here */
  746. for (i = 0; i < slen; i++) {
  747. for (j = 0; j < rlen; j++) {
  748. if (spref[i] == rpref[j]) {
  749. res = &spref[i];
  750. break;
  751. }
  752. }
  753. if (res)
  754. break;
  755. }
  756. /* we didn't agree on anything */
  757. if (res == NULL) {
  758. /* confirm previous value */
  759. switch (opt->dccpop_feat) {
  760. case DCCPF_CCID:
  761. /* XXX did i get this right? =P */
  762. if (opt->dccpop_type == DCCPO_CHANGE_L)
  763. res = &dccp_msk(sk)->dccpms_tx_ccid;
  764. else
  765. res = &dccp_msk(sk)->dccpms_rx_ccid;
  766. break;
  767. default:
  768. DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
  769. /* XXX implement res */
  770. return -EFAULT;
  771. }
  772. dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
  773. agree = 0; /* this is used for mandatory options... */
  774. }
  775. /* need to put result and our preference list */
  776. rlen = 1 + opt->dccpop_len;
  777. rpref = kmalloc(rlen, GFP_ATOMIC);
  778. if (rpref == NULL)
  779. return -ENOMEM;
  780. *rpref = *res;
  781. memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
  782. /* put it in the "confirm queue" */
  783. if (opt->dccpop_sc == NULL) {
  784. opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
  785. if (opt->dccpop_sc == NULL) {
  786. kfree(rpref);
  787. return -ENOMEM;
  788. }
  789. } else {
  790. /* recycle the confirm slot */
  791. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  792. kfree(opt->dccpop_sc->dccpoc_val);
  793. dccp_pr_debug("recycling confirm slot\n");
  794. }
  795. memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
  796. opt->dccpop_sc->dccpoc_val = rpref;
  797. opt->dccpop_sc->dccpoc_len = rlen;
  798. /* update the option on our side [we are about to send the confirm] */
  799. rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
  800. if (rc) {
  801. kfree(opt->dccpop_sc->dccpoc_val);
  802. kfree(opt->dccpop_sc);
  803. opt->dccpop_sc = NULL;
  804. return rc;
  805. }
  806. dccp_pr_debug("Will confirm %d\n", *rpref);
  807. /* say we want to change to X but we just got a confirm X, suppress our
  808. * change
  809. */
  810. if (!opt->dccpop_conf) {
  811. if (*opt->dccpop_val == *res)
  812. opt->dccpop_conf = 1;
  813. dccp_pr_debug("won't ask for change of same feature\n");
  814. }
  815. return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
  816. }
  817. static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  818. {
  819. struct dccp_minisock *dmsk = dccp_msk(sk);
  820. struct dccp_opt_pend *opt;
  821. int rc = 1;
  822. u8 t;
  823. /*
  824. * We received a CHANGE. We gotta match it against our own preference
  825. * list. If we got a CHANGE_R it means it's a change for us, so we need
  826. * to compare our CHANGE_L list.
  827. */
  828. if (type == DCCPO_CHANGE_L)
  829. t = DCCPO_CHANGE_R;
  830. else
  831. t = DCCPO_CHANGE_L;
  832. /* find our preference list for this feature */
  833. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  834. if (opt->dccpop_type != t || opt->dccpop_feat != feature)
  835. continue;
  836. /* find the winner from the two preference lists */
  837. rc = dccp_feat_reconcile(sk, opt, val, len);
  838. break;
  839. }
  840. /* We didn't deal with the change. This can happen if we have no
  841. * preference list for the feature. In fact, it just shouldn't
  842. * happen---if we understand a feature, we should have a preference list
  843. * with at least the default value.
  844. */
  845. BUG_ON(rc == 1);
  846. return rc;
  847. }
  848. static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  849. {
  850. struct dccp_opt_pend *opt;
  851. struct dccp_minisock *dmsk = dccp_msk(sk);
  852. u8 *copy;
  853. int rc;
  854. /* NN features must be Change L (sec. 6.3.2) */
  855. if (type != DCCPO_CHANGE_L) {
  856. dccp_pr_debug("received %s for NN feature %d\n",
  857. dccp_feat_typename(type), feature);
  858. return -EFAULT;
  859. }
  860. /* XXX sanity check opt val */
  861. /* copy option so we can confirm it */
  862. opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  863. if (opt == NULL)
  864. return -ENOMEM;
  865. copy = kmemdup(val, len, GFP_ATOMIC);
  866. if (copy == NULL) {
  867. kfree(opt);
  868. return -ENOMEM;
  869. }
  870. opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
  871. opt->dccpop_feat = feature;
  872. opt->dccpop_val = copy;
  873. opt->dccpop_len = len;
  874. /* change feature */
  875. rc = dccp_feat_update(sk, type, feature, *val);
  876. if (rc) {
  877. kfree(opt->dccpop_val);
  878. kfree(opt);
  879. return rc;
  880. }
  881. dccp_feat_debug(type, feature, *copy);
  882. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  883. return 0;
  884. }
  885. static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
  886. u8 type, u8 feature)
  887. {
  888. /* XXX check if other confirms for that are queued and recycle slot */
  889. struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
  890. if (opt == NULL) {
  891. /* XXX what do we do? Ignoring should be fine. It's a change
  892. * after all =P
  893. */
  894. return;
  895. }
  896. switch (type) {
  897. case DCCPO_CHANGE_L:
  898. opt->dccpop_type = DCCPO_CONFIRM_R;
  899. break;
  900. case DCCPO_CHANGE_R:
  901. opt->dccpop_type = DCCPO_CONFIRM_L;
  902. break;
  903. default:
  904. DCCP_WARN("invalid type %d\n", type);
  905. kfree(opt);
  906. return;
  907. }
  908. opt->dccpop_feat = feature;
  909. opt->dccpop_val = NULL;
  910. opt->dccpop_len = 0;
  911. /* change feature */
  912. dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
  913. list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
  914. }
  915. static void dccp_feat_flush_confirm(struct sock *sk)
  916. {
  917. struct dccp_minisock *dmsk = dccp_msk(sk);
  918. /* Check if there is anything to confirm in the first place */
  919. int yes = !list_empty(&dmsk->dccpms_conf);
  920. if (!yes) {
  921. struct dccp_opt_pend *opt;
  922. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  923. if (opt->dccpop_conf) {
  924. yes = 1;
  925. break;
  926. }
  927. }
  928. }
  929. if (!yes)
  930. return;
  931. /* OK there is something to confirm... */
  932. /* XXX check if packet is in flight? Send delayed ack?? */
  933. if (sk->sk_state == DCCP_OPEN)
  934. dccp_send_ack(sk);
  935. }
  936. int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
  937. {
  938. int rc;
  939. /* Ignore Change requests other than during connection setup */
  940. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  941. return 0;
  942. dccp_feat_debug(type, feature, *val);
  943. /* figure out if it's SP or NN feature */
  944. switch (feature) {
  945. /* deal with SP features */
  946. case DCCPF_CCID:
  947. /* XXX Obsoleted by next patch
  948. rc = dccp_feat_sp(sk, type, feature, val, len); */
  949. break;
  950. /* deal with NN features */
  951. case DCCPF_ACK_RATIO:
  952. /* XXX Obsoleted by next patch
  953. rc = dccp_feat_nn(sk, type, feature, val, len); */
  954. break;
  955. /* XXX implement other features */
  956. default:
  957. dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
  958. dccp_feat_typename(type), feature);
  959. rc = -EFAULT;
  960. break;
  961. }
  962. /* check if there were problems changing features */
  963. if (rc) {
  964. /* If we don't agree on SP, we sent a confirm for old value.
  965. * However we propagate rc to caller in case option was
  966. * mandatory
  967. */
  968. if (rc != DCCP_FEAT_SP_NOAGREE)
  969. dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
  970. }
  971. /* generate the confirm [if required] */
  972. dccp_feat_flush_confirm(sk);
  973. return rc;
  974. }
  975. EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
  976. int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
  977. u8 *val, u8 len)
  978. {
  979. u8 t;
  980. struct dccp_opt_pend *opt;
  981. struct dccp_minisock *dmsk = dccp_msk(sk);
  982. int found = 0;
  983. int all_confirmed = 1;
  984. /* Ignore Confirm options other than during connection setup */
  985. if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
  986. return 0;
  987. dccp_feat_debug(type, feature, *val);
  988. /* locate our change request */
  989. switch (type) {
  990. case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
  991. case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
  992. default: DCCP_WARN("invalid type %d\n", type);
  993. return 1;
  994. }
  995. /* XXX sanity check feature value */
  996. list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
  997. if (!opt->dccpop_conf && opt->dccpop_type == t &&
  998. opt->dccpop_feat == feature) {
  999. found = 1;
  1000. dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
  1001. /* XXX do sanity check */
  1002. opt->dccpop_conf = 1;
  1003. /* We got a confirmation---change the option */
  1004. dccp_feat_update(sk, opt->dccpop_type,
  1005. opt->dccpop_feat, *val);
  1006. /* XXX check the return value of dccp_feat_update */
  1007. break;
  1008. }
  1009. if (!opt->dccpop_conf)
  1010. all_confirmed = 0;
  1011. }
  1012. if (!found)
  1013. dccp_pr_debug("%s(%d, ...) never requested\n",
  1014. dccp_feat_typename(type), feature);
  1015. return 0;
  1016. }
  1017. EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
  1018. #endif /* (later) */
  1019. void dccp_feat_clean(struct dccp_minisock *dmsk)
  1020. {
  1021. struct dccp_opt_pend *opt, *next;
  1022. list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
  1023. dccpop_node) {
  1024. BUG_ON(opt->dccpop_val == NULL);
  1025. kfree(opt->dccpop_val);
  1026. if (opt->dccpop_sc != NULL) {
  1027. BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
  1028. kfree(opt->dccpop_sc->dccpoc_val);
  1029. kfree(opt->dccpop_sc);
  1030. }
  1031. kfree(opt);
  1032. }
  1033. INIT_LIST_HEAD(&dmsk->dccpms_pending);
  1034. list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
  1035. BUG_ON(opt == NULL);
  1036. if (opt->dccpop_val != NULL)
  1037. kfree(opt->dccpop_val);
  1038. kfree(opt);
  1039. }
  1040. INIT_LIST_HEAD(&dmsk->dccpms_conf);
  1041. }
  1042. EXPORT_SYMBOL_GPL(dccp_feat_clean);
  1043. /* this is to be called only when a listening sock creates its child. It is
  1044. * assumed by the function---the confirm is not duplicated, but rather it is
  1045. * "passed on".
  1046. */
  1047. int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
  1048. {
  1049. struct dccp_minisock *olddmsk = dccp_msk(oldsk);
  1050. struct dccp_minisock *newdmsk = dccp_msk(newsk);
  1051. struct dccp_opt_pend *opt;
  1052. int rc = 0;
  1053. INIT_LIST_HEAD(&newdmsk->dccpms_pending);
  1054. INIT_LIST_HEAD(&newdmsk->dccpms_conf);
  1055. list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
  1056. struct dccp_opt_pend *newopt;
  1057. /* copy the value of the option */
  1058. u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
  1059. if (val == NULL)
  1060. goto out_clean;
  1061. newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
  1062. if (newopt == NULL) {
  1063. kfree(val);
  1064. goto out_clean;
  1065. }
  1066. /* insert the option */
  1067. newopt->dccpop_val = val;
  1068. list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
  1069. /* XXX what happens with backlogs and multiple connections at
  1070. * once...
  1071. */
  1072. /* the master socket no longer needs to worry about confirms */
  1073. opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
  1074. /* reset state for a new socket */
  1075. opt->dccpop_conf = 0;
  1076. }
  1077. /* XXX not doing anything about the conf queue */
  1078. out:
  1079. return rc;
  1080. out_clean:
  1081. dccp_feat_clean(newdmsk);
  1082. rc = -ENOMEM;
  1083. goto out;
  1084. }
  1085. EXPORT_SYMBOL_GPL(dccp_feat_clone);
  1086. /**
  1087. * dccp_feat_change_recv - Process incoming ChangeL/R options
  1088. * @fn: feature-negotiation list to update
  1089. * @is_mandatory: whether the Change was preceded by a Mandatory option
  1090. * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
  1091. * @feat: one of %dccp_feature_numbers
  1092. * @val: NN value or SP value/preference list
  1093. * @len: length of @val in bytes
  1094. * @server: whether this node is the server (1) or the client (0)
  1095. */
  1096. static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
  1097. u8 feat, u8 *val, u8 len, const bool server)
  1098. {
  1099. u8 defval, type = dccp_feat_type(feat);
  1100. const bool local = (opt == DCCPO_CHANGE_R);
  1101. struct dccp_feat_entry *entry;
  1102. dccp_feat_val fval;
  1103. if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
  1104. goto unknown_feature_or_value;
  1105. /*
  1106. * Negotiation of NN features: Change R is invalid, so there is no
  1107. * simultaneous negotiation; hence we do not look up in the list.
  1108. */
  1109. if (type == FEAT_NN) {
  1110. if (local || len > sizeof(fval.nn))
  1111. goto unknown_feature_or_value;
  1112. /* 6.3.2: "The feature remote MUST accept any valid value..." */
  1113. fval.nn = dccp_decode_value_var(val, len);
  1114. if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
  1115. goto unknown_feature_or_value;
  1116. return dccp_feat_push_confirm(fn, feat, local, &fval);
  1117. }
  1118. /*
  1119. * Unidirectional/simultaneous negotiation of SP features (6.3.1)
  1120. */
  1121. entry = dccp_feat_list_lookup(fn, feat, local);
  1122. if (entry == NULL) {
  1123. /*
  1124. * No particular preferences have been registered. We deal with
  1125. * this situation by assuming that all valid values are equally
  1126. * acceptable, and apply the following checks:
  1127. * - if the peer's list is a singleton, we accept a valid value;
  1128. * - if we are the server, we first try to see if the peer (the
  1129. * client) advertises the default value. If yes, we use it,
  1130. * otherwise we accept the preferred value;
  1131. * - else if we are the client, we use the first list element.
  1132. */
  1133. if (dccp_feat_clone_sp_val(&fval, val, 1))
  1134. return DCCP_RESET_CODE_TOO_BUSY;
  1135. if (len > 1 && server) {
  1136. defval = dccp_feat_default_value(feat);
  1137. if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
  1138. fval.sp.vec[0] = defval;
  1139. } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
  1140. kfree(fval.sp.vec);
  1141. goto unknown_feature_or_value;
  1142. }
  1143. /* Treat unsupported CCIDs like invalid values */
  1144. if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
  1145. kfree(fval.sp.vec);
  1146. goto not_valid_or_not_known;
  1147. }
  1148. return dccp_feat_push_confirm(fn, feat, local, &fval);
  1149. } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
  1150. return 0;
  1151. }
  1152. if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
  1153. entry->empty_confirm = 0;
  1154. } else if (is_mandatory) {
  1155. return DCCP_RESET_CODE_MANDATORY_ERROR;
  1156. } else if (entry->state == FEAT_INITIALISING) {
  1157. /*
  1158. * Failed simultaneous negotiation (server only): try to `save'
  1159. * the connection by checking whether entry contains the default
  1160. * value for @feat. If yes, send an empty Confirm to signal that
  1161. * the received Change was not understood - which implies using
  1162. * the default value.
  1163. * If this also fails, we use Reset as the last resort.
  1164. */
  1165. WARN_ON(!server);
  1166. defval = dccp_feat_default_value(feat);
  1167. if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
  1168. return DCCP_RESET_CODE_OPTION_ERROR;
  1169. entry->empty_confirm = 1;
  1170. }
  1171. entry->needs_confirm = 1;
  1172. entry->needs_mandatory = 0;
  1173. entry->state = FEAT_STABLE;
  1174. return 0;
  1175. unknown_feature_or_value:
  1176. if (!is_mandatory)
  1177. return dccp_push_empty_confirm(fn, feat, local);
  1178. not_valid_or_not_known:
  1179. return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
  1180. : DCCP_RESET_CODE_OPTION_ERROR;
  1181. }
  1182. /**
  1183. * dccp_feat_confirm_recv - Process received Confirm options
  1184. * @fn: feature-negotiation list to update
  1185. * @is_mandatory: whether @opt was preceded by a Mandatory option
  1186. * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
  1187. * @feat: one of %dccp_feature_numbers
  1188. * @val: NN value or SP value/preference list
  1189. * @len: length of @val in bytes
  1190. * @server: whether this node is server (1) or client (0)
  1191. */
  1192. static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
  1193. u8 feat, u8 *val, u8 len, const bool server)
  1194. {
  1195. u8 *plist, plen, type = dccp_feat_type(feat);
  1196. const bool local = (opt == DCCPO_CONFIRM_R);
  1197. struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
  1198. if (entry == NULL) { /* nothing queued: ignore or handle error */
  1199. if (is_mandatory && type == FEAT_UNKNOWN)
  1200. return DCCP_RESET_CODE_MANDATORY_ERROR;
  1201. if (!local && type == FEAT_NN) /* 6.3.2 */
  1202. goto confirmation_failed;
  1203. return 0;
  1204. }
  1205. if (entry->state != FEAT_CHANGING) /* 6.6.2 */
  1206. return 0;
  1207. if (len == 0) {
  1208. if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
  1209. goto confirmation_failed;
  1210. /*
  1211. * Empty Confirm during connection setup: this means reverting
  1212. * to the `old' value, which in this case is the default. Since
  1213. * we handle default values automatically when no other values
  1214. * have been set, we revert to the old value by removing this
  1215. * entry from the list.
  1216. */
  1217. dccp_feat_list_pop(entry);
  1218. return 0;
  1219. }
  1220. if (type == FEAT_NN) {
  1221. if (len > sizeof(entry->val.nn))
  1222. goto confirmation_failed;
  1223. if (entry->val.nn == dccp_decode_value_var(val, len))
  1224. goto confirmation_succeeded;
  1225. DCCP_WARN("Bogus Confirm for non-existing value\n");
  1226. goto confirmation_failed;
  1227. }
  1228. /*
  1229. * Parsing SP Confirms: the first element of @val is the preferred
  1230. * SP value which the peer confirms, the remainder depends on @len.
  1231. * Note that only the confirmed value need to be a valid SP value.
  1232. */
  1233. if (!dccp_feat_is_valid_sp_val(feat, *val))
  1234. goto confirmation_failed;
  1235. if (len == 1) { /* peer didn't supply a preference list */
  1236. plist = val;
  1237. plen = len;
  1238. } else { /* preferred value + preference list */
  1239. plist = val + 1;
  1240. plen = len - 1;
  1241. }
  1242. /* Check whether the peer got the reconciliation right (6.6.8) */
  1243. if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
  1244. DCCP_WARN("Confirm selected the wrong value %u\n", *val);
  1245. return DCCP_RESET_CODE_OPTION_ERROR;
  1246. }
  1247. entry->val.sp.vec[0] = *val;
  1248. confirmation_succeeded:
  1249. entry->state = FEAT_STABLE;
  1250. return 0;
  1251. confirmation_failed:
  1252. DCCP_WARN("Confirmation failed\n");
  1253. return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
  1254. : DCCP_RESET_CODE_OPTION_ERROR;
  1255. }
  1256. /**
  1257. * dccp_feat_parse_options - Process Feature-Negotiation Options
  1258. * @sk: for general use and used by the client during connection setup
  1259. * @dreq: used by the server during connection setup
  1260. * @mandatory: whether @opt was preceded by a Mandatory option
  1261. * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
  1262. * @feat: one of %dccp_feature_numbers
  1263. * @val: value contents of @opt
  1264. * @len: length of @val in bytes
  1265. * Returns 0 on success, a Reset code for ending the connection otherwise.
  1266. */
  1267. int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
  1268. u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
  1269. {
  1270. struct dccp_sock *dp = dccp_sk(sk);
  1271. struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
  1272. bool server = false;
  1273. switch (sk->sk_state) {
  1274. /*
  1275. * Negotiation during connection setup
  1276. */
  1277. case DCCP_LISTEN:
  1278. server = true; /* fall through */
  1279. case DCCP_REQUESTING:
  1280. switch (opt) {
  1281. case DCCPO_CHANGE_L:
  1282. case DCCPO_CHANGE_R:
  1283. return dccp_feat_change_recv(fn, mandatory, opt, feat,
  1284. val, len, server);
  1285. case DCCPO_CONFIRM_R:
  1286. case DCCPO_CONFIRM_L:
  1287. return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
  1288. val, len, server);
  1289. }
  1290. }
  1291. return 0; /* ignore FN options in all other states */
  1292. }
  1293. int dccp_feat_init(struct sock *sk)
  1294. {
  1295. struct dccp_sock *dp = dccp_sk(sk);
  1296. struct dccp_minisock *dmsk = dccp_msk(sk);
  1297. int rc;
  1298. INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
  1299. INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
  1300. /* CCID L */
  1301. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
  1302. &dmsk->dccpms_tx_ccid, 1);
  1303. if (rc)
  1304. goto out;
  1305. /* CCID R */
  1306. rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
  1307. &dmsk->dccpms_rx_ccid, 1);
  1308. if (rc)
  1309. goto out;
  1310. /* Ack ratio */
  1311. rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
  1312. dp->dccps_l_ack_ratio);
  1313. out:
  1314. return rc;
  1315. }
  1316. EXPORT_SYMBOL_GPL(dccp_feat_init);
  1317. #ifdef CONFIG_IP_DCCP_DEBUG
  1318. const char *dccp_feat_typename(const u8 type)
  1319. {
  1320. switch(type) {
  1321. case DCCPO_CHANGE_L: return("ChangeL");
  1322. case DCCPO_CONFIRM_L: return("ConfirmL");
  1323. case DCCPO_CHANGE_R: return("ChangeR");
  1324. case DCCPO_CONFIRM_R: return("ConfirmR");
  1325. /* the following case must not appear in feature negotation */
  1326. default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
  1327. }
  1328. return NULL;
  1329. }
  1330. EXPORT_SYMBOL_GPL(dccp_feat_typename);
  1331. const char *dccp_feat_name(const u8 feat)
  1332. {
  1333. static const char *feature_names[] = {
  1334. [DCCPF_RESERVED] = "Reserved",
  1335. [DCCPF_CCID] = "CCID",
  1336. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  1337. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  1338. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  1339. [DCCPF_ACK_RATIO] = "Ack Ratio",
  1340. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  1341. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  1342. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  1343. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  1344. };
  1345. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  1346. return feature_names[DCCPF_RESERVED];
  1347. if (feat == DCCPF_SEND_LEV_RATE)
  1348. return "Send Loss Event Rate";
  1349. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  1350. return "CCID-specific";
  1351. return feature_names[feat];
  1352. }
  1353. EXPORT_SYMBOL_GPL(dccp_feat_name);
  1354. #endif /* CONFIG_IP_DCCP_DEBUG */