feat.c 37 KB

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