feat.c 39 KB

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