feat.c 41 KB

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