feat.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /*
  2. * net/dccp/feat.c
  3. *
  4. * Feature negotiation for the DCCP protocol (RFC 4340, section 6)
  5. *
  6. * Copyright (c) 2008 Gerrit Renker <gerrit@erg.abdn.ac.uk>
  7. * Rewrote from scratch, some bits from earlier code by
  8. * Copyright (c) 2005 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
  9. *
  10. *
  11. * ASSUMPTIONS
  12. * -----------
  13. * o Feature negotiation is coordinated with connection setup (as in TCP), wild
  14. * changes of parameters of an established connection are not supported.
  15. * o All currently known SP features have 1-byte quantities. If in the future
  16. * extensions of RFCs 4340..42 define features with item lengths larger than
  17. * one byte, a feature-specific extension of the code will be required.
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License
  21. * as published by the Free Software Foundation; either version
  22. * 2 of the License, or (at your option) any later version.
  23. */
  24. #include <linux/module.h>
  25. #include "ccid.h"
  26. #include "feat.h"
  27. /*
  28. * Feature activation handlers.
  29. *
  30. * These all use an u64 argument, to provide enough room for NN/SP features. At
  31. * this stage the negotiated values have been checked to be within their range.
  32. */
  33. static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
  34. {
  35. struct dccp_sock *dp = dccp_sk(sk);
  36. struct ccid *new_ccid = ccid_new(ccid, sk, rx);
  37. if (new_ccid == NULL)
  38. return -ENOMEM;
  39. if (rx) {
  40. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  41. dp->dccps_hc_rx_ccid = new_ccid;
  42. } else {
  43. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  44. dp->dccps_hc_tx_ccid = new_ccid;
  45. }
  46. return 0;
  47. }
  48. static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
  49. {
  50. struct dccp_sock *dp = dccp_sk(sk);
  51. if (rx) {
  52. dp->dccps_r_seq_win = seq_win;
  53. /* propagate changes to update SWL/SWH */
  54. dccp_update_gsr(sk, dp->dccps_gsr);
  55. } else {
  56. dp->dccps_l_seq_win = seq_win;
  57. /* propagate changes to update AWL */
  58. dccp_update_gss(sk, dp->dccps_gss);
  59. }
  60. return 0;
  61. }
  62. static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
  63. {
  64. if (rx)
  65. dccp_sk(sk)->dccps_r_ack_ratio = ratio;
  66. else
  67. dccp_sk(sk)->dccps_l_ack_ratio = ratio;
  68. return 0;
  69. }
  70. static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
  71. {
  72. struct dccp_sock *dp = dccp_sk(sk);
  73. if (rx) {
  74. if (enable && dp->dccps_hc_rx_ackvec == NULL) {
  75. dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
  76. if (dp->dccps_hc_rx_ackvec == NULL)
  77. return -ENOMEM;
  78. } else if (!enable) {
  79. dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
  80. dp->dccps_hc_rx_ackvec = NULL;
  81. }
  82. }
  83. return 0;
  84. }
  85. static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
  86. {
  87. if (!rx)
  88. dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
  89. return 0;
  90. }
  91. /*
  92. * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
  93. * `rx' holds when the sending peer informs about his partial coverage via a
  94. * ChangeR() option. In the other case, we are the sender and the receiver
  95. * announces its coverage via ChangeL() options. The policy here is to honour
  96. * such communication by enabling the corresponding partial coverage - but only
  97. * if it has not been set manually before; the warning here means that all
  98. * packets will be dropped.
  99. */
  100. static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
  101. {
  102. struct dccp_sock *dp = dccp_sk(sk);
  103. if (rx)
  104. dp->dccps_pcrlen = cscov;
  105. else {
  106. if (dp->dccps_pcslen == 0)
  107. dp->dccps_pcslen = cscov;
  108. else if (cscov > dp->dccps_pcslen)
  109. DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
  110. dp->dccps_pcslen, (u8)cscov);
  111. }
  112. return 0;
  113. }
  114. static const struct {
  115. u8 feat_num; /* DCCPF_xxx */
  116. enum dccp_feat_type rxtx; /* RX or TX */
  117. enum dccp_feat_type reconciliation; /* SP or NN */
  118. u8 default_value; /* as in 6.4 */
  119. int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
  120. /*
  121. * Lookup table for location and type of features (from RFC 4340/4342)
  122. * +--------------------------+----+-----+----+----+---------+-----------+
  123. * | Feature | Location | Reconc. | Initial | Section |
  124. * | | RX | TX | SP | NN | Value | Reference |
  125. * +--------------------------+----+-----+----+----+---------+-----------+
  126. * | DCCPF_CCID | | X | X | | 2 | 10 |
  127. * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
  128. * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
  129. * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
  130. * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
  131. * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
  132. * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
  133. * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
  134. * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
  135. * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
  136. * +--------------------------+----+-----+----+----+---------+-----------+
  137. */
  138. } dccp_feat_table[] = {
  139. { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
  140. { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
  141. { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
  142. { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
  143. { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
  144. { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
  145. { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
  146. { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
  147. { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
  148. { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
  149. };
  150. #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
  151. /**
  152. * dccp_feat_index - Hash function to map feature number into array position
  153. * Returns consecutive array index or -1 if the feature is not understood.
  154. */
  155. static int dccp_feat_index(u8 feat_num)
  156. {
  157. /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
  158. if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
  159. return feat_num - 1;
  160. /*
  161. * Other features: add cases for new feature types here after adding
  162. * them to the above table.
  163. */
  164. switch (feat_num) {
  165. case DCCPF_SEND_LEV_RATE:
  166. return DCCP_FEAT_SUPPORTED_MAX - 1;
  167. }
  168. return -1;
  169. }
  170. static u8 dccp_feat_type(u8 feat_num)
  171. {
  172. int idx = dccp_feat_index(feat_num);
  173. if (idx < 0)
  174. return FEAT_UNKNOWN;
  175. return dccp_feat_table[idx].reconciliation;
  176. }
  177. static int dccp_feat_default_value(u8 feat_num)
  178. {
  179. int idx = dccp_feat_index(feat_num);
  180. /*
  181. * There are no default values for unknown features, so encountering a
  182. * negative index here indicates a serious problem somewhere else.
  183. */
  184. DCCP_BUG_ON(idx < 0);
  185. return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
  186. }
  187. static int __dccp_feat_activate(struct sock *sk, const int idx,
  188. const bool is_local, dccp_feat_val const *fval)
  189. {
  190. bool rx;
  191. u64 val;
  192. if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
  193. return -1;
  194. if (dccp_feat_table[idx].activation_hdlr == NULL)
  195. return 0;
  196. if (fval == NULL) {
  197. val = dccp_feat_table[idx].default_value;
  198. } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
  199. if (fval->sp.vec == NULL) {
  200. /*
  201. * This can happen when an empty Confirm is sent
  202. * for an SP (i.e. known) feature. In this case
  203. * we would be using the default anyway.
  204. */
  205. DCCP_CRIT("Feature #%d undefined: using default", idx);
  206. val = dccp_feat_table[idx].default_value;
  207. } else {
  208. val = fval->sp.vec[0];
  209. }
  210. } else {
  211. val = fval->nn;
  212. }
  213. /* Location is RX if this is a local-RX or remote-TX feature */
  214. rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
  215. return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
  216. }
  217. /* Test for "Req'd" feature (RFC 4340, 6.4) */
  218. static inline int dccp_feat_must_be_understood(u8 feat_num)
  219. {
  220. return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
  221. feat_num == DCCPF_SEQUENCE_WINDOW;
  222. }
  223. /* copy constructor, fval must not already contain allocated memory */
  224. static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
  225. {
  226. fval->sp.len = len;
  227. if (fval->sp.len > 0) {
  228. fval->sp.vec = kmemdup(val, len, gfp_any());
  229. if (fval->sp.vec == NULL) {
  230. fval->sp.len = 0;
  231. return -ENOBUFS;
  232. }
  233. }
  234. return 0;
  235. }
  236. static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
  237. {
  238. if (unlikely(val == NULL))
  239. return;
  240. if (dccp_feat_type(feat_num) == FEAT_SP)
  241. kfree(val->sp.vec);
  242. memset(val, 0, sizeof(*val));
  243. }
  244. static struct dccp_feat_entry *
  245. dccp_feat_clone_entry(struct dccp_feat_entry const *original)
  246. {
  247. struct dccp_feat_entry *new;
  248. u8 type = dccp_feat_type(original->feat_num);
  249. if (type == FEAT_UNKNOWN)
  250. return NULL;
  251. new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
  252. if (new == NULL)
  253. return NULL;
  254. if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
  255. original->val.sp.vec,
  256. original->val.sp.len)) {
  257. kfree(new);
  258. return NULL;
  259. }
  260. return new;
  261. }
  262. static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
  263. {
  264. if (entry != NULL) {
  265. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  266. kfree(entry);
  267. }
  268. }
  269. /*
  270. * List management functions
  271. *
  272. * Feature negotiation lists rely on and maintain the following invariants:
  273. * - each feat_num in the list is known, i.e. we know its type and default value
  274. * - each feat_num/is_local combination is unique (old entries are overwritten)
  275. * - SP values are always freshly allocated
  276. * - list is sorted in increasing order of feature number (faster lookup)
  277. */
  278. static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
  279. u8 feat_num, bool is_local)
  280. {
  281. struct dccp_feat_entry *entry;
  282. list_for_each_entry(entry, fn_list, node) {
  283. if (entry->feat_num == feat_num && entry->is_local == is_local)
  284. return entry;
  285. else if (entry->feat_num > feat_num)
  286. break;
  287. }
  288. return NULL;
  289. }
  290. /**
  291. * dccp_feat_entry_new - Central list update routine (called by all others)
  292. * @head: list to add to
  293. * @feat: feature number
  294. * @local: whether the local (1) or remote feature with number @feat is meant
  295. * This is the only constructor and serves to ensure the above invariants.
  296. */
  297. static struct dccp_feat_entry *
  298. dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
  299. {
  300. struct dccp_feat_entry *entry;
  301. list_for_each_entry(entry, head, node)
  302. if (entry->feat_num == feat && entry->is_local == local) {
  303. dccp_feat_val_destructor(entry->feat_num, &entry->val);
  304. return entry;
  305. } else if (entry->feat_num > feat) {
  306. head = &entry->node;
  307. break;
  308. }
  309. entry = kmalloc(sizeof(*entry), gfp_any());
  310. if (entry != NULL) {
  311. entry->feat_num = feat;
  312. entry->is_local = local;
  313. list_add_tail(&entry->node, head);
  314. }
  315. return entry;
  316. }
  317. /**
  318. * dccp_feat_push_change - Add/overwrite a Change option in the list
  319. * @fn_list: feature-negotiation list to update
  320. * @feat: one of %dccp_feature_numbers
  321. * @local: whether local (1) or remote (0) @feat_num is meant
  322. * @needs_mandatory: whether to use Mandatory feature negotiation options
  323. * @fval: pointer to NN/SP value to be inserted (will be copied)
  324. */
  325. static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
  326. u8 mandatory, dccp_feat_val *fval)
  327. {
  328. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  329. if (new == NULL)
  330. return -ENOMEM;
  331. new->feat_num = feat;
  332. new->is_local = local;
  333. new->state = FEAT_INITIALISING;
  334. new->needs_confirm = 0;
  335. new->empty_confirm = 0;
  336. new->val = *fval;
  337. new->needs_mandatory = mandatory;
  338. return 0;
  339. }
  340. /**
  341. * dccp_feat_push_confirm - Add a Confirm entry to the FN list
  342. * @fn_list: feature-negotiation list to add to
  343. * @feat: one of %dccp_feature_numbers
  344. * @local: whether local (1) or remote (0) @feat_num is being confirmed
  345. * @fval: pointer to NN/SP value to be inserted or NULL
  346. * Returns 0 on success, a Reset code for further processing otherwise.
  347. */
  348. static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
  349. dccp_feat_val *fval)
  350. {
  351. struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
  352. if (new == NULL)
  353. return DCCP_RESET_CODE_TOO_BUSY;
  354. new->feat_num = feat;
  355. new->is_local = local;
  356. new->state = FEAT_STABLE; /* transition in 6.6.2 */
  357. new->needs_confirm = 1;
  358. new->empty_confirm = (fval == NULL);
  359. new->val.nn = 0; /* zeroes the whole structure */
  360. if (!new->empty_confirm)
  361. new->val = *fval;
  362. new->needs_mandatory = 0;
  363. return 0;
  364. }
  365. static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
  366. {
  367. return dccp_feat_push_confirm(fn_list, feat, local, NULL);
  368. }
  369. static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
  370. {
  371. list_del(&entry->node);
  372. dccp_feat_entry_destructor(entry);
  373. }
  374. void dccp_feat_list_purge(struct list_head *fn_list)
  375. {
  376. struct dccp_feat_entry *entry, *next;
  377. list_for_each_entry_safe(entry, next, fn_list, node)
  378. dccp_feat_entry_destructor(entry);
  379. INIT_LIST_HEAD(fn_list);
  380. }
  381. EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
  382. /* generate @to as full clone of @from - @to must not contain any nodes */
  383. int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
  384. {
  385. struct dccp_feat_entry *entry, *new;
  386. INIT_LIST_HEAD(to);
  387. list_for_each_entry(entry, from, node) {
  388. new = dccp_feat_clone_entry(entry);
  389. if (new == NULL)
  390. goto cloning_failed;
  391. list_add_tail(&new->node, to);
  392. }
  393. return 0;
  394. cloning_failed:
  395. dccp_feat_list_purge(to);
  396. return -ENOMEM;
  397. }
  398. /**
  399. * dccp_feat_valid_nn_length - Enforce length constraints on NN options
  400. * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
  401. * incoming options are accepted as long as their values are valid.
  402. */
  403. static u8 dccp_feat_valid_nn_length(u8 feat_num)
  404. {
  405. if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
  406. return 2;
  407. if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
  408. return 6;
  409. return 0;
  410. }
  411. static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
  412. {
  413. switch (feat_num) {
  414. case DCCPF_ACK_RATIO:
  415. return val <= DCCPF_ACK_RATIO_MAX;
  416. case DCCPF_SEQUENCE_WINDOW:
  417. return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
  418. }
  419. return 0; /* feature unknown - so we can't tell */
  420. }
  421. /* check that SP values are within the ranges defined in RFC 4340 */
  422. static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
  423. {
  424. switch (feat_num) {
  425. case DCCPF_CCID:
  426. return val == DCCPC_CCID2 || val == DCCPC_CCID3;
  427. /* Type-check Boolean feature values: */
  428. case DCCPF_SHORT_SEQNOS:
  429. case DCCPF_ECN_INCAPABLE:
  430. case DCCPF_SEND_ACK_VECTOR:
  431. case DCCPF_SEND_NDP_COUNT:
  432. case DCCPF_DATA_CHECKSUM:
  433. case DCCPF_SEND_LEV_RATE:
  434. return val < 2;
  435. case DCCPF_MIN_CSUM_COVER:
  436. return val < 16;
  437. }
  438. return 0; /* feature unknown */
  439. }
  440. static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
  441. {
  442. if (sp_list == NULL || sp_len < 1)
  443. return 0;
  444. while (sp_len--)
  445. if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
  446. return 0;
  447. return 1;
  448. }
  449. /**
  450. * dccp_feat_insert_opts - Generate FN options from current list state
  451. * @skb: next sk_buff to be sent to the peer
  452. * @dp: for client during handshake and general negotiation
  453. * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
  454. */
  455. int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
  456. struct sk_buff *skb)
  457. {
  458. struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
  459. struct dccp_feat_entry *pos, *next;
  460. u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
  461. bool rpt;
  462. /* put entries into @skb in the order they appear in the list */
  463. list_for_each_entry_safe_reverse(pos, next, fn, node) {
  464. opt = dccp_feat_genopt(pos);
  465. type = dccp_feat_type(pos->feat_num);
  466. rpt = false;
  467. if (pos->empty_confirm) {
  468. len = 0;
  469. ptr = NULL;
  470. } else {
  471. if (type == FEAT_SP) {
  472. len = pos->val.sp.len;
  473. ptr = pos->val.sp.vec;
  474. rpt = pos->needs_confirm;
  475. } else if (type == FEAT_NN) {
  476. len = dccp_feat_valid_nn_length(pos->feat_num);
  477. ptr = nn_in_nbo;
  478. dccp_encode_value_var(pos->val.nn, ptr, len);
  479. } else {
  480. DCCP_BUG("unknown feature %u", pos->feat_num);
  481. return -1;
  482. }
  483. }
  484. if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
  485. return -1;
  486. if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
  487. return -1;
  488. /*
  489. * Enter CHANGING after transmitting the Change option (6.6.2).
  490. */
  491. if (pos->state == FEAT_INITIALISING)
  492. pos->state = FEAT_CHANGING;
  493. }
  494. return 0;
  495. }
  496. /**
  497. * __feat_register_nn - Register new NN value on socket
  498. * @fn: feature-negotiation list to register with
  499. * @feat: an NN feature from %dccp_feature_numbers
  500. * @mandatory: use Mandatory option if 1
  501. * @nn_val: value to register (restricted to 4 bytes)
  502. * Note that NN features are local by definition (RFC 4340, 6.3.2).
  503. */
  504. static int __feat_register_nn(struct list_head *fn, u8 feat,
  505. u8 mandatory, u64 nn_val)
  506. {
  507. dccp_feat_val fval = { .nn = nn_val };
  508. if (dccp_feat_type(feat) != FEAT_NN ||
  509. !dccp_feat_is_valid_nn_val(feat, nn_val))
  510. return -EINVAL;
  511. /* Don't bother with default values, they will be activated anyway. */
  512. if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
  513. return 0;
  514. return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
  515. }
  516. /**
  517. * __feat_register_sp - Register new SP value/list on socket
  518. * @fn: feature-negotiation list to register with
  519. * @feat: an SP feature from %dccp_feature_numbers
  520. * @is_local: whether the local (1) or the remote (0) @feat is meant
  521. * @mandatory: use Mandatory option if 1
  522. * @sp_val: SP value followed by optional preference list
  523. * @sp_len: length of @sp_val in bytes
  524. */
  525. static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
  526. u8 mandatory, u8 const *sp_val, u8 sp_len)
  527. {
  528. dccp_feat_val fval;
  529. if (dccp_feat_type(feat) != FEAT_SP ||
  530. !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
  531. return -EINVAL;
  532. /* Avoid negotiating alien CCIDs by only advertising supported ones */
  533. if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
  534. return -EOPNOTSUPP;
  535. if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
  536. return -ENOMEM;
  537. return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
  538. }
  539. /**
  540. * dccp_feat_register_sp - Register requests to change SP feature values
  541. * @sk: client or listening socket
  542. * @feat: one of %dccp_feature_numbers
  543. * @is_local: whether the local (1) or remote (0) @feat is meant
  544. * @list: array of preferred values, in descending order of preference
  545. * @len: length of @list in bytes
  546. */
  547. int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
  548. u8 const *list, u8 len)
  549. { /* any changes must be registered before establishing the connection */
  550. if (sk->sk_state != DCCP_CLOSED)
  551. return -EISCONN;
  552. if (dccp_feat_type(feat) != FEAT_SP)
  553. return -EINVAL;
  554. return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
  555. 0, list, len);
  556. }
  557. /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
  558. int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
  559. {
  560. /* any changes must be registered before establishing the connection */
  561. if (sk->sk_state != DCCP_CLOSED)
  562. return -EISCONN;
  563. if (dccp_feat_type(feat) != FEAT_NN)
  564. return -EINVAL;
  565. return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
  566. }
  567. /*
  568. * Tracking features whose value depend on the choice of CCID
  569. *
  570. * This is designed with an extension in mind so that a list walk could be done
  571. * before activating any features. However, the existing framework was found to
  572. * work satisfactorily up until now, the automatic verification is left open.
  573. * When adding new CCIDs, add a corresponding dependency table here.
  574. */
  575. static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
  576. {
  577. static const struct ccid_dependency ccid2_dependencies[2][2] = {
  578. /*
  579. * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
  580. * feature and Send Ack Vector is an RX feature, `is_local'
  581. * needs to be reversed.
  582. */
  583. { /* Dependencies of the receiver-side (remote) CCID2 */
  584. {
  585. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  586. .is_local = true,
  587. .is_mandatory = true,
  588. .val = 1
  589. },
  590. { 0, 0, 0, 0 }
  591. },
  592. { /* Dependencies of the sender-side (local) CCID2 */
  593. {
  594. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  595. .is_local = false,
  596. .is_mandatory = true,
  597. .val = 1
  598. },
  599. { 0, 0, 0, 0 }
  600. }
  601. };
  602. static const struct ccid_dependency ccid3_dependencies[2][5] = {
  603. { /*
  604. * Dependencies of the receiver-side CCID3
  605. */
  606. { /* locally disable Ack Vectors */
  607. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  608. .is_local = true,
  609. .is_mandatory = false,
  610. .val = 0
  611. },
  612. { /* see below why Send Loss Event Rate is on */
  613. .dependent_feat = DCCPF_SEND_LEV_RATE,
  614. .is_local = true,
  615. .is_mandatory = true,
  616. .val = 1
  617. },
  618. { /* NDP Count is needed as per RFC 4342, 6.1.1 */
  619. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  620. .is_local = false,
  621. .is_mandatory = true,
  622. .val = 1
  623. },
  624. { 0, 0, 0, 0 },
  625. },
  626. { /*
  627. * CCID3 at the TX side: we request that the HC-receiver
  628. * will not send Ack Vectors (they will be ignored, so
  629. * Mandatory is not set); we enable Send Loss Event Rate
  630. * (Mandatory since the implementation does not support
  631. * the Loss Intervals option of RFC 4342, 8.6).
  632. * The last two options are for peer's information only.
  633. */
  634. {
  635. .dependent_feat = DCCPF_SEND_ACK_VECTOR,
  636. .is_local = false,
  637. .is_mandatory = false,
  638. .val = 0
  639. },
  640. {
  641. .dependent_feat = DCCPF_SEND_LEV_RATE,
  642. .is_local = false,
  643. .is_mandatory = true,
  644. .val = 1
  645. },
  646. { /* this CCID does not support Ack Ratio */
  647. .dependent_feat = DCCPF_ACK_RATIO,
  648. .is_local = true,
  649. .is_mandatory = false,
  650. .val = 0
  651. },
  652. { /* tell receiver we are sending NDP counts */
  653. .dependent_feat = DCCPF_SEND_NDP_COUNT,
  654. .is_local = true,
  655. .is_mandatory = false,
  656. .val = 1
  657. },
  658. { 0, 0, 0, 0 }
  659. }
  660. };
  661. switch (ccid) {
  662. case DCCPC_CCID2:
  663. return ccid2_dependencies[is_local];
  664. case DCCPC_CCID3:
  665. return ccid3_dependencies[is_local];
  666. default:
  667. return NULL;
  668. }
  669. }
  670. /**
  671. * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
  672. * @fn: feature-negotiation list to update
  673. * @id: CCID number to track
  674. * @is_local: whether TX CCID (1) or RX CCID (0) is meant
  675. * This function needs to be called after registering all other features.
  676. */
  677. static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
  678. {
  679. const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
  680. int i, rc = (table == NULL);
  681. for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
  682. if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
  683. rc = __feat_register_sp(fn, table[i].dependent_feat,
  684. table[i].is_local,
  685. table[i].is_mandatory,
  686. &table[i].val, 1);
  687. else
  688. rc = __feat_register_nn(fn, table[i].dependent_feat,
  689. table[i].is_mandatory,
  690. table[i].val);
  691. return rc;
  692. }
  693. /**
  694. * dccp_feat_finalise_settings - Finalise settings before starting negotiation
  695. * @dp: client or listening socket (settings will be inherited)
  696. * This is called after all registrations (socket initialisation, sysctls, and
  697. * sockopt calls), and before sending the first packet containing Change options
  698. * (ie. client-Request or server-Response), to ensure internal consistency.
  699. */
  700. int dccp_feat_finalise_settings(struct dccp_sock *dp)
  701. {
  702. struct list_head *fn = &dp->dccps_featneg;
  703. struct dccp_feat_entry *entry;
  704. int i = 2, ccids[2] = { -1, -1 };
  705. /*
  706. * Propagating CCIDs:
  707. * 1) not useful to propagate CCID settings if this host advertises more
  708. * than one CCID: the choice of CCID may still change - if this is
  709. * the client, or if this is the server and the client sends
  710. * singleton CCID values.
  711. * 2) since is that propagate_ccid changes the list, we defer changing
  712. * the sorted list until after the traversal.
  713. */
  714. list_for_each_entry(entry, fn, node)
  715. if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
  716. ccids[entry->is_local] = entry->val.sp.vec[0];
  717. while (i--)
  718. if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
  719. return -1;
  720. return 0;
  721. }
  722. /**
  723. * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
  724. * It is the server which resolves the dependencies once the CCID has been
  725. * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
  726. */
  727. int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
  728. {
  729. struct list_head *fn = &dreq->dreq_featneg;
  730. struct dccp_feat_entry *entry;
  731. u8 is_local, ccid;
  732. for (is_local = 0; is_local <= 1; is_local++) {
  733. entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
  734. if (entry != NULL && !entry->empty_confirm)
  735. ccid = entry->val.sp.vec[0];
  736. else
  737. ccid = dccp_feat_default_value(DCCPF_CCID);
  738. if (dccp_feat_propagate_ccid(fn, ccid, is_local))
  739. return -1;
  740. }
  741. return 0;
  742. }
  743. /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
  744. static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
  745. {
  746. u8 c, s;
  747. for (s = 0; s < slen; s++)
  748. for (c = 0; c < clen; c++)
  749. if (servlist[s] == clilist[c])
  750. return servlist[s];
  751. return -1;
  752. }
  753. /**
  754. * dccp_feat_prefer - Move preferred entry to the start of array
  755. * Reorder the @array_len elements in @array so that @preferred_value comes
  756. * first. Returns >0 to indicate that @preferred_value does occur in @array.
  757. */
  758. static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
  759. {
  760. u8 i, does_occur = 0;
  761. if (array != NULL) {
  762. for (i = 0; i < array_len; i++)
  763. if (array[i] == preferred_value) {
  764. array[i] = array[0];
  765. does_occur++;
  766. }
  767. if (does_occur)
  768. array[0] = preferred_value;
  769. }
  770. return does_occur;
  771. }
  772. /**
  773. * dccp_feat_reconcile - Reconcile SP preference lists
  774. * @fval: SP list to reconcile into
  775. * @arr: received SP preference list
  776. * @len: length of @arr in bytes
  777. * @is_server: whether this side is the server (and @fv is the server's list)
  778. * @reorder: whether to reorder the list in @fv after reconciling with @arr
  779. * When successful, > 0 is returned and the reconciled list is in @fval.
  780. * A value of 0 means that negotiation failed (no shared entry).
  781. */
  782. static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
  783. bool is_server, bool reorder)
  784. {
  785. int rc;
  786. if (!fv->sp.vec || !arr) {
  787. DCCP_CRIT("NULL feature value or array");
  788. return 0;
  789. }
  790. if (is_server)
  791. rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
  792. else
  793. rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
  794. if (!reorder)
  795. return rc;
  796. if (rc < 0)
  797. return 0;
  798. /*
  799. * Reorder list: used for activating features and in dccp_insert_fn_opt.
  800. */
  801. return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
  802. }
  803. /**
  804. * dccp_feat_change_recv - Process incoming ChangeL/R options
  805. * @fn: feature-negotiation list to update
  806. * @is_mandatory: whether the Change was preceded by a Mandatory option
  807. * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
  808. * @feat: one of %dccp_feature_numbers
  809. * @val: NN value or SP value/preference list
  810. * @len: length of @val in bytes
  811. * @server: whether this node is the server (1) or the client (0)
  812. */
  813. static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
  814. u8 feat, u8 *val, u8 len, const bool server)
  815. {
  816. u8 defval, type = dccp_feat_type(feat);
  817. const bool local = (opt == DCCPO_CHANGE_R);
  818. struct dccp_feat_entry *entry;
  819. dccp_feat_val fval;
  820. if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
  821. goto unknown_feature_or_value;
  822. /*
  823. * Negotiation of NN features: Change R is invalid, so there is no
  824. * simultaneous negotiation; hence we do not look up in the list.
  825. */
  826. if (type == FEAT_NN) {
  827. if (local || len > sizeof(fval.nn))
  828. goto unknown_feature_or_value;
  829. /* 6.3.2: "The feature remote MUST accept any valid value..." */
  830. fval.nn = dccp_decode_value_var(val, len);
  831. if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
  832. goto unknown_feature_or_value;
  833. return dccp_feat_push_confirm(fn, feat, local, &fval);
  834. }
  835. /*
  836. * Unidirectional/simultaneous negotiation of SP features (6.3.1)
  837. */
  838. entry = dccp_feat_list_lookup(fn, feat, local);
  839. if (entry == NULL) {
  840. /*
  841. * No particular preferences have been registered. We deal with
  842. * this situation by assuming that all valid values are equally
  843. * acceptable, and apply the following checks:
  844. * - if the peer's list is a singleton, we accept a valid value;
  845. * - if we are the server, we first try to see if the peer (the
  846. * client) advertises the default value. If yes, we use it,
  847. * otherwise we accept the preferred value;
  848. * - else if we are the client, we use the first list element.
  849. */
  850. if (dccp_feat_clone_sp_val(&fval, val, 1))
  851. return DCCP_RESET_CODE_TOO_BUSY;
  852. if (len > 1 && server) {
  853. defval = dccp_feat_default_value(feat);
  854. if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
  855. fval.sp.vec[0] = defval;
  856. } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
  857. kfree(fval.sp.vec);
  858. goto unknown_feature_or_value;
  859. }
  860. /* Treat unsupported CCIDs like invalid values */
  861. if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
  862. kfree(fval.sp.vec);
  863. goto not_valid_or_not_known;
  864. }
  865. return dccp_feat_push_confirm(fn, feat, local, &fval);
  866. } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
  867. return 0;
  868. }
  869. if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
  870. entry->empty_confirm = 0;
  871. } else if (is_mandatory) {
  872. return DCCP_RESET_CODE_MANDATORY_ERROR;
  873. } else if (entry->state == FEAT_INITIALISING) {
  874. /*
  875. * Failed simultaneous negotiation (server only): try to `save'
  876. * the connection by checking whether entry contains the default
  877. * value for @feat. If yes, send an empty Confirm to signal that
  878. * the received Change was not understood - which implies using
  879. * the default value.
  880. * If this also fails, we use Reset as the last resort.
  881. */
  882. WARN_ON(!server);
  883. defval = dccp_feat_default_value(feat);
  884. if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
  885. return DCCP_RESET_CODE_OPTION_ERROR;
  886. entry->empty_confirm = 1;
  887. }
  888. entry->needs_confirm = 1;
  889. entry->needs_mandatory = 0;
  890. entry->state = FEAT_STABLE;
  891. return 0;
  892. unknown_feature_or_value:
  893. if (!is_mandatory)
  894. return dccp_push_empty_confirm(fn, feat, local);
  895. not_valid_or_not_known:
  896. return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
  897. : DCCP_RESET_CODE_OPTION_ERROR;
  898. }
  899. /**
  900. * dccp_feat_confirm_recv - Process received Confirm options
  901. * @fn: feature-negotiation list to update
  902. * @is_mandatory: whether @opt was preceded by a Mandatory option
  903. * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
  904. * @feat: one of %dccp_feature_numbers
  905. * @val: NN value or SP value/preference list
  906. * @len: length of @val in bytes
  907. * @server: whether this node is server (1) or client (0)
  908. */
  909. static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
  910. u8 feat, u8 *val, u8 len, const bool server)
  911. {
  912. u8 *plist, plen, type = dccp_feat_type(feat);
  913. const bool local = (opt == DCCPO_CONFIRM_R);
  914. struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
  915. if (entry == NULL) { /* nothing queued: ignore or handle error */
  916. if (is_mandatory && type == FEAT_UNKNOWN)
  917. return DCCP_RESET_CODE_MANDATORY_ERROR;
  918. if (!local && type == FEAT_NN) /* 6.3.2 */
  919. goto confirmation_failed;
  920. return 0;
  921. }
  922. if (entry->state != FEAT_CHANGING) /* 6.6.2 */
  923. return 0;
  924. if (len == 0) {
  925. if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
  926. goto confirmation_failed;
  927. /*
  928. * Empty Confirm during connection setup: this means reverting
  929. * to the `old' value, which in this case is the default. Since
  930. * we handle default values automatically when no other values
  931. * have been set, we revert to the old value by removing this
  932. * entry from the list.
  933. */
  934. dccp_feat_list_pop(entry);
  935. return 0;
  936. }
  937. if (type == FEAT_NN) {
  938. if (len > sizeof(entry->val.nn))
  939. goto confirmation_failed;
  940. if (entry->val.nn == dccp_decode_value_var(val, len))
  941. goto confirmation_succeeded;
  942. DCCP_WARN("Bogus Confirm for non-existing value\n");
  943. goto confirmation_failed;
  944. }
  945. /*
  946. * Parsing SP Confirms: the first element of @val is the preferred
  947. * SP value which the peer confirms, the remainder depends on @len.
  948. * Note that only the confirmed value need to be a valid SP value.
  949. */
  950. if (!dccp_feat_is_valid_sp_val(feat, *val))
  951. goto confirmation_failed;
  952. if (len == 1) { /* peer didn't supply a preference list */
  953. plist = val;
  954. plen = len;
  955. } else { /* preferred value + preference list */
  956. plist = val + 1;
  957. plen = len - 1;
  958. }
  959. /* Check whether the peer got the reconciliation right (6.6.8) */
  960. if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
  961. DCCP_WARN("Confirm selected the wrong value %u\n", *val);
  962. return DCCP_RESET_CODE_OPTION_ERROR;
  963. }
  964. entry->val.sp.vec[0] = *val;
  965. confirmation_succeeded:
  966. entry->state = FEAT_STABLE;
  967. return 0;
  968. confirmation_failed:
  969. DCCP_WARN("Confirmation failed\n");
  970. return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
  971. : DCCP_RESET_CODE_OPTION_ERROR;
  972. }
  973. /**
  974. * dccp_feat_parse_options - Process Feature-Negotiation Options
  975. * @sk: for general use and used by the client during connection setup
  976. * @dreq: used by the server during connection setup
  977. * @mandatory: whether @opt was preceded by a Mandatory option
  978. * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
  979. * @feat: one of %dccp_feature_numbers
  980. * @val: value contents of @opt
  981. * @len: length of @val in bytes
  982. * Returns 0 on success, a Reset code for ending the connection otherwise.
  983. */
  984. int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
  985. u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
  986. {
  987. struct dccp_sock *dp = dccp_sk(sk);
  988. struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
  989. bool server = false;
  990. switch (sk->sk_state) {
  991. /*
  992. * Negotiation during connection setup
  993. */
  994. case DCCP_LISTEN:
  995. server = true; /* fall through */
  996. case DCCP_REQUESTING:
  997. switch (opt) {
  998. case DCCPO_CHANGE_L:
  999. case DCCPO_CHANGE_R:
  1000. return dccp_feat_change_recv(fn, mandatory, opt, feat,
  1001. val, len, server);
  1002. case DCCPO_CONFIRM_R:
  1003. case DCCPO_CONFIRM_L:
  1004. return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
  1005. val, len, server);
  1006. }
  1007. }
  1008. return 0; /* ignore FN options in all other states */
  1009. }
  1010. /**
  1011. * dccp_feat_init - Seed feature negotiation with host-specific defaults
  1012. * This initialises global defaults, depending on the value of the sysctls.
  1013. * These can later be overridden by registering changes via setsockopt calls.
  1014. * The last link in the chain is finalise_settings, to make sure that between
  1015. * here and the start of actual feature negotiation no inconsistencies enter.
  1016. *
  1017. * All features not appearing below use either defaults or are otherwise
  1018. * later adjusted through dccp_feat_finalise_settings().
  1019. */
  1020. int dccp_feat_init(struct sock *sk)
  1021. {
  1022. struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
  1023. u8 on = 1, off = 0;
  1024. int rc;
  1025. struct {
  1026. u8 *val;
  1027. u8 len;
  1028. } tx, rx;
  1029. /* Non-negotiable (NN) features */
  1030. rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
  1031. sysctl_dccp_feat_sequence_window);
  1032. if (rc)
  1033. return rc;
  1034. /* Server-priority (SP) features */
  1035. /* Advertise that short seqnos are not supported (7.6.1) */
  1036. rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
  1037. if (rc)
  1038. return rc;
  1039. /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
  1040. rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
  1041. if (rc)
  1042. return rc;
  1043. /*
  1044. * We advertise the available list of CCIDs and reorder according to
  1045. * preferences, to avoid failure resulting from negotiating different
  1046. * singleton values (which always leads to failure).
  1047. * These settings can still (later) be overridden via sockopts.
  1048. */
  1049. if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
  1050. ccid_get_builtin_ccids(&rx.val, &rx.len))
  1051. return -ENOBUFS;
  1052. if (!dccp_feat_prefer(sysctl_dccp_feat_tx_ccid, tx.val, tx.len) ||
  1053. !dccp_feat_prefer(sysctl_dccp_feat_rx_ccid, rx.val, rx.len))
  1054. goto free_ccid_lists;
  1055. rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
  1056. if (rc)
  1057. goto free_ccid_lists;
  1058. rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
  1059. free_ccid_lists:
  1060. kfree(tx.val);
  1061. kfree(rx.val);
  1062. return rc;
  1063. }
  1064. int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
  1065. {
  1066. struct dccp_sock *dp = dccp_sk(sk);
  1067. struct dccp_feat_entry *cur, *next;
  1068. int idx;
  1069. dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
  1070. [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
  1071. };
  1072. list_for_each_entry(cur, fn_list, node) {
  1073. /*
  1074. * An empty Confirm means that either an unknown feature type
  1075. * or an invalid value was present. In the first case there is
  1076. * nothing to activate, in the other the default value is used.
  1077. */
  1078. if (cur->empty_confirm)
  1079. continue;
  1080. idx = dccp_feat_index(cur->feat_num);
  1081. if (idx < 0) {
  1082. DCCP_BUG("Unknown feature %u", cur->feat_num);
  1083. goto activation_failed;
  1084. }
  1085. if (cur->state != FEAT_STABLE) {
  1086. DCCP_CRIT("Negotiation of %s %u failed in state %u",
  1087. cur->is_local ? "local" : "remote",
  1088. cur->feat_num, cur->state);
  1089. goto activation_failed;
  1090. }
  1091. fvals[idx][cur->is_local] = &cur->val;
  1092. }
  1093. /*
  1094. * Activate in decreasing order of index, so that the CCIDs are always
  1095. * activated as the last feature. This avoids the case where a CCID
  1096. * relies on the initialisation of one or more features that it depends
  1097. * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
  1098. */
  1099. for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
  1100. if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
  1101. __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
  1102. DCCP_CRIT("Could not activate %d", idx);
  1103. goto activation_failed;
  1104. }
  1105. /* Clean up Change options which have been confirmed already */
  1106. list_for_each_entry_safe(cur, next, fn_list, node)
  1107. if (!cur->needs_confirm)
  1108. dccp_feat_list_pop(cur);
  1109. dccp_pr_debug("Activation OK\n");
  1110. return 0;
  1111. activation_failed:
  1112. /*
  1113. * We clean up everything that may have been allocated, since
  1114. * it is difficult to track at which stage negotiation failed.
  1115. * This is ok, since all allocation functions below are robust
  1116. * against NULL arguments.
  1117. */
  1118. ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
  1119. ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
  1120. dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
  1121. dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
  1122. dp->dccps_hc_rx_ackvec = NULL;
  1123. return -1;
  1124. }
  1125. #ifdef CONFIG_IP_DCCP_DEBUG
  1126. const char *dccp_feat_typename(const u8 type)
  1127. {
  1128. switch(type) {
  1129. case DCCPO_CHANGE_L: return("ChangeL");
  1130. case DCCPO_CONFIRM_L: return("ConfirmL");
  1131. case DCCPO_CHANGE_R: return("ChangeR");
  1132. case DCCPO_CONFIRM_R: return("ConfirmR");
  1133. /* the following case must not appear in feature negotation */
  1134. default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
  1135. }
  1136. return NULL;
  1137. }
  1138. const char *dccp_feat_name(const u8 feat)
  1139. {
  1140. static const char *feature_names[] = {
  1141. [DCCPF_RESERVED] = "Reserved",
  1142. [DCCPF_CCID] = "CCID",
  1143. [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
  1144. [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
  1145. [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
  1146. [DCCPF_ACK_RATIO] = "Ack Ratio",
  1147. [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
  1148. [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
  1149. [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
  1150. [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
  1151. };
  1152. if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
  1153. return feature_names[DCCPF_RESERVED];
  1154. if (feat == DCCPF_SEND_LEV_RATE)
  1155. return "Send Loss Event Rate";
  1156. if (feat >= DCCPF_MIN_CCID_SPECIFIC)
  1157. return "CCID-specific";
  1158. return feature_names[feat];
  1159. }
  1160. #endif /* CONFIG_IP_DCCP_DEBUG */