12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2afe00251SAndrea Bittau /* 3afe00251SAndrea Bittau * net/dccp/feat.c 4afe00251SAndrea Bittau * 563b8e286SGerrit Renker * Feature negotiation for the DCCP protocol (RFC 4340, section 6) 663b8e286SGerrit Renker * 763b8e286SGerrit Renker * Copyright (c) 2008 Gerrit Renker <gerrit@erg.abdn.ac.uk> 863b8e286SGerrit Renker * Rewrote from scratch, some bits from earlier code by 963b8e286SGerrit Renker * Copyright (c) 2005 Andrea Bittau <a.bittau@cs.ucl.ac.uk> 1063b8e286SGerrit Renker * 115cdae198SGerrit Renker * ASSUMPTIONS 125cdae198SGerrit Renker * ----------- 13f74e91b6SGerrit Renker * o Feature negotiation is coordinated with connection setup (as in TCP), wild 14f74e91b6SGerrit Renker * changes of parameters of an established connection are not supported. 15d6916f87SGerrit Renker * o Changing non-negotiable (NN) values is supported in state OPEN/PARTOPEN. 165cdae198SGerrit Renker * o All currently known SP features have 1-byte quantities. If in the future 175cdae198SGerrit Renker * extensions of RFCs 4340..42 define features with item lengths larger than 185cdae198SGerrit Renker * one byte, a feature-specific extension of the code will be required. 19afe00251SAndrea Bittau */ 20afe00251SAndrea Bittau #include <linux/module.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 226ffd30fbSAndrea Bittau #include "ccid.h" 23afe00251SAndrea Bittau #include "feat.h" 24afe00251SAndrea Bittau 25883ca833SGerrit Renker /* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */ 26883ca833SGerrit Renker unsigned long sysctl_dccp_sequence_window __read_mostly = 100; 27883ca833SGerrit Renker int sysctl_dccp_rx_ccid __read_mostly = 2, 28883ca833SGerrit Renker sysctl_dccp_tx_ccid __read_mostly = 2; 29883ca833SGerrit Renker 30422d9cdcSGerrit Renker /* 31422d9cdcSGerrit Renker * Feature activation handlers. 32422d9cdcSGerrit Renker * 33422d9cdcSGerrit Renker * These all use an u64 argument, to provide enough room for NN/SP features. At 34422d9cdcSGerrit Renker * this stage the negotiated values have been checked to be within their range. 35422d9cdcSGerrit Renker */ 36422d9cdcSGerrit Renker static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx) 37422d9cdcSGerrit Renker { 38422d9cdcSGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 39e5fd56caSGerrit Renker struct ccid *new_ccid = ccid_new(ccid, sk, rx); 40422d9cdcSGerrit Renker 41422d9cdcSGerrit Renker if (new_ccid == NULL) 42422d9cdcSGerrit Renker return -ENOMEM; 43422d9cdcSGerrit Renker 44422d9cdcSGerrit Renker if (rx) { 45422d9cdcSGerrit Renker ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 46422d9cdcSGerrit Renker dp->dccps_hc_rx_ccid = new_ccid; 47422d9cdcSGerrit Renker } else { 48422d9cdcSGerrit Renker ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 49422d9cdcSGerrit Renker dp->dccps_hc_tx_ccid = new_ccid; 50422d9cdcSGerrit Renker } 51422d9cdcSGerrit Renker return 0; 52422d9cdcSGerrit Renker } 53422d9cdcSGerrit Renker 54422d9cdcSGerrit Renker static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx) 55422d9cdcSGerrit Renker { 56792b4878SGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 57792b4878SGerrit Renker 58792b4878SGerrit Renker if (rx) { 59792b4878SGerrit Renker dp->dccps_r_seq_win = seq_win; 60792b4878SGerrit Renker /* propagate changes to update SWL/SWH */ 61792b4878SGerrit Renker dccp_update_gsr(sk, dp->dccps_gsr); 62792b4878SGerrit Renker } else { 63792b4878SGerrit Renker dp->dccps_l_seq_win = seq_win; 64792b4878SGerrit Renker /* propagate changes to update AWL */ 65792b4878SGerrit Renker dccp_update_gss(sk, dp->dccps_gss); 66792b4878SGerrit Renker } 67422d9cdcSGerrit Renker return 0; 68422d9cdcSGerrit Renker } 69422d9cdcSGerrit Renker 70422d9cdcSGerrit Renker static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx) 71422d9cdcSGerrit Renker { 72422d9cdcSGerrit Renker if (rx) 73422d9cdcSGerrit Renker dccp_sk(sk)->dccps_r_ack_ratio = ratio; 74422d9cdcSGerrit Renker else 75422d9cdcSGerrit Renker dccp_sk(sk)->dccps_l_ack_ratio = ratio; 76422d9cdcSGerrit Renker return 0; 77422d9cdcSGerrit Renker } 78422d9cdcSGerrit Renker 79422d9cdcSGerrit Renker static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx) 80422d9cdcSGerrit Renker { 81422d9cdcSGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 82422d9cdcSGerrit Renker 83422d9cdcSGerrit Renker if (rx) { 84422d9cdcSGerrit Renker if (enable && dp->dccps_hc_rx_ackvec == NULL) { 85422d9cdcSGerrit Renker dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any()); 86422d9cdcSGerrit Renker if (dp->dccps_hc_rx_ackvec == NULL) 87422d9cdcSGerrit Renker return -ENOMEM; 88422d9cdcSGerrit Renker } else if (!enable) { 89422d9cdcSGerrit Renker dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 90422d9cdcSGerrit Renker dp->dccps_hc_rx_ackvec = NULL; 91422d9cdcSGerrit Renker } 92422d9cdcSGerrit Renker } 93422d9cdcSGerrit Renker return 0; 94422d9cdcSGerrit Renker } 95422d9cdcSGerrit Renker 96422d9cdcSGerrit Renker static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx) 97422d9cdcSGerrit Renker { 98422d9cdcSGerrit Renker if (!rx) 994098dce5SGerrit Renker dccp_sk(sk)->dccps_send_ndp_count = (enable > 0); 100422d9cdcSGerrit Renker return 0; 101422d9cdcSGerrit Renker } 102422d9cdcSGerrit Renker 103422d9cdcSGerrit Renker /* 104422d9cdcSGerrit Renker * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that 105422d9cdcSGerrit Renker * `rx' holds when the sending peer informs about his partial coverage via a 106422d9cdcSGerrit Renker * ChangeR() option. In the other case, we are the sender and the receiver 107422d9cdcSGerrit Renker * announces its coverage via ChangeL() options. The policy here is to honour 108422d9cdcSGerrit Renker * such communication by enabling the corresponding partial coverage - but only 109422d9cdcSGerrit Renker * if it has not been set manually before; the warning here means that all 110422d9cdcSGerrit Renker * packets will be dropped. 111422d9cdcSGerrit Renker */ 112422d9cdcSGerrit Renker static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx) 113422d9cdcSGerrit Renker { 114422d9cdcSGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 115422d9cdcSGerrit Renker 116422d9cdcSGerrit Renker if (rx) 117422d9cdcSGerrit Renker dp->dccps_pcrlen = cscov; 118422d9cdcSGerrit Renker else { 119422d9cdcSGerrit Renker if (dp->dccps_pcslen == 0) 120422d9cdcSGerrit Renker dp->dccps_pcslen = cscov; 121422d9cdcSGerrit Renker else if (cscov > dp->dccps_pcslen) 122422d9cdcSGerrit Renker DCCP_WARN("CsCov %u too small, peer requires >= %u\n", 123422d9cdcSGerrit Renker dp->dccps_pcslen, (u8)cscov); 124422d9cdcSGerrit Renker } 125422d9cdcSGerrit Renker return 0; 126422d9cdcSGerrit Renker } 127422d9cdcSGerrit Renker 1287d43d1a0SGerrit Renker static const struct { 1297d43d1a0SGerrit Renker u8 feat_num; /* DCCPF_xxx */ 1307d43d1a0SGerrit Renker enum dccp_feat_type rxtx; /* RX or TX */ 1317d43d1a0SGerrit Renker enum dccp_feat_type reconciliation; /* SP or NN */ 1327d43d1a0SGerrit Renker u8 default_value; /* as in 6.4 */ 133422d9cdcSGerrit Renker int (*activation_hdlr)(struct sock *sk, u64 val, bool rx); 1347d43d1a0SGerrit Renker /* 1357d43d1a0SGerrit Renker * Lookup table for location and type of features (from RFC 4340/4342) 1367d43d1a0SGerrit Renker * +--------------------------+----+-----+----+----+---------+-----------+ 1377d43d1a0SGerrit Renker * | Feature | Location | Reconc. | Initial | Section | 1387d43d1a0SGerrit Renker * | | RX | TX | SP | NN | Value | Reference | 1397d43d1a0SGerrit Renker * +--------------------------+----+-----+----+----+---------+-----------+ 1407d43d1a0SGerrit Renker * | DCCPF_CCID | | X | X | | 2 | 10 | 1417d43d1a0SGerrit Renker * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 | 1427d43d1a0SGerrit Renker * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 | 1437d43d1a0SGerrit Renker * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 | 1447d43d1a0SGerrit Renker * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 | 1457d43d1a0SGerrit Renker * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 | 1467d43d1a0SGerrit Renker * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 | 1477d43d1a0SGerrit Renker * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 | 1487d43d1a0SGerrit Renker * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 | 1497d43d1a0SGerrit Renker * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 | 1507d43d1a0SGerrit Renker * +--------------------------+----+-----+----+----+---------+-----------+ 1517d43d1a0SGerrit Renker */ 1527d43d1a0SGerrit Renker } dccp_feat_table[] = { 153422d9cdcSGerrit Renker { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid }, 154422d9cdcSGerrit Renker { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL }, 155422d9cdcSGerrit Renker { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win }, 156422d9cdcSGerrit Renker { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL }, 157422d9cdcSGerrit Renker { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio}, 158422d9cdcSGerrit Renker { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec }, 159422d9cdcSGerrit Renker { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp }, 160422d9cdcSGerrit Renker { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov}, 161422d9cdcSGerrit Renker { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL }, 162422d9cdcSGerrit Renker { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL }, 1637d43d1a0SGerrit Renker }; 1647d43d1a0SGerrit Renker #define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table) 1657d43d1a0SGerrit Renker 16661e6473eSGerrit Renker /** 16761e6473eSGerrit Renker * dccp_feat_index - Hash function to map feature number into array position 168d0b1101bSAndrew Lunn * @feat_num: feature to hash, one of %dccp_feature_numbers 169d0b1101bSAndrew Lunn * 17061e6473eSGerrit Renker * Returns consecutive array index or -1 if the feature is not understood. 17161e6473eSGerrit Renker */ 17261e6473eSGerrit Renker static int dccp_feat_index(u8 feat_num) 17361e6473eSGerrit Renker { 17461e6473eSGerrit Renker /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */ 17561e6473eSGerrit Renker if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM) 17661e6473eSGerrit Renker return feat_num - 1; 17761e6473eSGerrit Renker 17861e6473eSGerrit Renker /* 17961e6473eSGerrit Renker * Other features: add cases for new feature types here after adding 18061e6473eSGerrit Renker * them to the above table. 18161e6473eSGerrit Renker */ 18261e6473eSGerrit Renker switch (feat_num) { 18361e6473eSGerrit Renker case DCCPF_SEND_LEV_RATE: 18461e6473eSGerrit Renker return DCCP_FEAT_SUPPORTED_MAX - 1; 18561e6473eSGerrit Renker } 18661e6473eSGerrit Renker return -1; 18761e6473eSGerrit Renker } 18861e6473eSGerrit Renker 18961e6473eSGerrit Renker static u8 dccp_feat_type(u8 feat_num) 19061e6473eSGerrit Renker { 19161e6473eSGerrit Renker int idx = dccp_feat_index(feat_num); 19261e6473eSGerrit Renker 19361e6473eSGerrit Renker if (idx < 0) 19461e6473eSGerrit Renker return FEAT_UNKNOWN; 19561e6473eSGerrit Renker return dccp_feat_table[idx].reconciliation; 19661e6473eSGerrit Renker } 19761e6473eSGerrit Renker 198e8ef967aSGerrit Renker static int dccp_feat_default_value(u8 feat_num) 199e8ef967aSGerrit Renker { 200e8ef967aSGerrit Renker int idx = dccp_feat_index(feat_num); 201e8ef967aSGerrit Renker /* 202e8ef967aSGerrit Renker * There are no default values for unknown features, so encountering a 203e8ef967aSGerrit Renker * negative index here indicates a serious problem somewhere else. 204e8ef967aSGerrit Renker */ 205e8ef967aSGerrit Renker DCCP_BUG_ON(idx < 0); 206e8ef967aSGerrit Renker 207e8ef967aSGerrit Renker return idx < 0 ? 0 : dccp_feat_table[idx].default_value; 208e8ef967aSGerrit Renker } 209e8ef967aSGerrit Renker 210f3f3abb6SGerrit Renker /* 211f3f3abb6SGerrit Renker * Debugging and verbose-printing section 212f3f3abb6SGerrit Renker */ 213f3f3abb6SGerrit Renker static const char *dccp_feat_fname(const u8 feat) 214f3f3abb6SGerrit Renker { 21536cbd3dcSJan Engelhardt static const char *const feature_names[] = { 216f3f3abb6SGerrit Renker [DCCPF_RESERVED] = "Reserved", 217f3f3abb6SGerrit Renker [DCCPF_CCID] = "CCID", 218f3f3abb6SGerrit Renker [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos", 219f3f3abb6SGerrit Renker [DCCPF_SEQUENCE_WINDOW] = "Sequence Window", 220f3f3abb6SGerrit Renker [DCCPF_ECN_INCAPABLE] = "ECN Incapable", 221f3f3abb6SGerrit Renker [DCCPF_ACK_RATIO] = "Ack Ratio", 222f3f3abb6SGerrit Renker [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector", 223f3f3abb6SGerrit Renker [DCCPF_SEND_NDP_COUNT] = "Send NDP Count", 224f3f3abb6SGerrit Renker [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage", 225f3f3abb6SGerrit Renker [DCCPF_DATA_CHECKSUM] = "Send Data Checksum", 226f3f3abb6SGerrit Renker }; 227f3f3abb6SGerrit Renker if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC) 228f3f3abb6SGerrit Renker return feature_names[DCCPF_RESERVED]; 229f3f3abb6SGerrit Renker 230f3f3abb6SGerrit Renker if (feat == DCCPF_SEND_LEV_RATE) 231f3f3abb6SGerrit Renker return "Send Loss Event Rate"; 232f3f3abb6SGerrit Renker if (feat >= DCCPF_MIN_CCID_SPECIFIC) 233f3f3abb6SGerrit Renker return "CCID-specific"; 234f3f3abb6SGerrit Renker 235f3f3abb6SGerrit Renker return feature_names[feat]; 236f3f3abb6SGerrit Renker } 237f3f3abb6SGerrit Renker 23836cbd3dcSJan Engelhardt static const char *const dccp_feat_sname[] = { 23936cbd3dcSJan Engelhardt "DEFAULT", "INITIALISING", "CHANGING", "UNSTABLE", "STABLE", 24036cbd3dcSJan Engelhardt }; 241f3f3abb6SGerrit Renker 242f3f3abb6SGerrit Renker #ifdef CONFIG_IP_DCCP_DEBUG 243f3f3abb6SGerrit Renker static const char *dccp_feat_oname(const u8 opt) 244f3f3abb6SGerrit Renker { 245f3f3abb6SGerrit Renker switch (opt) { 246f3f3abb6SGerrit Renker case DCCPO_CHANGE_L: return "Change_L"; 247f3f3abb6SGerrit Renker case DCCPO_CONFIRM_L: return "Confirm_L"; 248f3f3abb6SGerrit Renker case DCCPO_CHANGE_R: return "Change_R"; 249f3f3abb6SGerrit Renker case DCCPO_CONFIRM_R: return "Confirm_R"; 250f3f3abb6SGerrit Renker } 251f3f3abb6SGerrit Renker return NULL; 252f3f3abb6SGerrit Renker } 253f3f3abb6SGerrit Renker 254f3f3abb6SGerrit Renker static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val) 255f3f3abb6SGerrit Renker { 256f3f3abb6SGerrit Renker u8 i, type = dccp_feat_type(feat_num); 257f3f3abb6SGerrit Renker 258f3f3abb6SGerrit Renker if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL)) 259f3f3abb6SGerrit Renker dccp_pr_debug_cat("(NULL)"); 260f3f3abb6SGerrit Renker else if (type == FEAT_SP) 261f3f3abb6SGerrit Renker for (i = 0; i < val->sp.len; i++) 262f3f3abb6SGerrit Renker dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]); 263f3f3abb6SGerrit Renker else if (type == FEAT_NN) 264f3f3abb6SGerrit Renker dccp_pr_debug_cat("%llu", (unsigned long long)val->nn); 265f3f3abb6SGerrit Renker else 266f3f3abb6SGerrit Renker dccp_pr_debug_cat("unknown type %u", type); 267f3f3abb6SGerrit Renker } 268f3f3abb6SGerrit Renker 269f3f3abb6SGerrit Renker static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len) 270f3f3abb6SGerrit Renker { 271f3f3abb6SGerrit Renker u8 type = dccp_feat_type(feat_num); 272f3f3abb6SGerrit Renker dccp_feat_val fval = { .sp.vec = list, .sp.len = len }; 273f3f3abb6SGerrit Renker 274f3f3abb6SGerrit Renker if (type == FEAT_NN) 275f3f3abb6SGerrit Renker fval.nn = dccp_decode_value_var(list, len); 276f3f3abb6SGerrit Renker dccp_feat_printval(feat_num, &fval); 277f3f3abb6SGerrit Renker } 278f3f3abb6SGerrit Renker 279f3f3abb6SGerrit Renker static void dccp_feat_print_entry(struct dccp_feat_entry const *entry) 280f3f3abb6SGerrit Renker { 281f3f3abb6SGerrit Renker dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote", 282f3f3abb6SGerrit Renker dccp_feat_fname(entry->feat_num)); 283f3f3abb6SGerrit Renker dccp_feat_printval(entry->feat_num, &entry->val); 284f3f3abb6SGerrit Renker dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state], 285f3f3abb6SGerrit Renker entry->needs_confirm ? "(Confirm pending)" : ""); 286f3f3abb6SGerrit Renker } 287f3f3abb6SGerrit Renker 288f3f3abb6SGerrit Renker #define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \ 289f3f3abb6SGerrit Renker dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\ 290f3f3abb6SGerrit Renker dccp_feat_printvals(feat, val, len); \ 291f3f3abb6SGerrit Renker dccp_pr_debug_cat(") %s\n", mandatory ? "!" : ""); } while (0) 292f3f3abb6SGerrit Renker 293f3f3abb6SGerrit Renker #define dccp_feat_print_fnlist(fn_list) { \ 294f3f3abb6SGerrit Renker const struct dccp_feat_entry *___entry; \ 295f3f3abb6SGerrit Renker \ 296f3f3abb6SGerrit Renker dccp_pr_debug("List Dump:\n"); \ 297f3f3abb6SGerrit Renker list_for_each_entry(___entry, fn_list, node) \ 298f3f3abb6SGerrit Renker dccp_feat_print_entry(___entry); \ 299f3f3abb6SGerrit Renker } 300f3f3abb6SGerrit Renker #else /* ! CONFIG_IP_DCCP_DEBUG */ 301f3f3abb6SGerrit Renker #define dccp_feat_print_opt(opt, feat, val, len, mandatory) 302f3f3abb6SGerrit Renker #define dccp_feat_print_fnlist(fn_list) 303f3f3abb6SGerrit Renker #endif 304f3f3abb6SGerrit Renker 305422d9cdcSGerrit Renker static int __dccp_feat_activate(struct sock *sk, const int idx, 306422d9cdcSGerrit Renker const bool is_local, dccp_feat_val const *fval) 307422d9cdcSGerrit Renker { 308422d9cdcSGerrit Renker bool rx; 309422d9cdcSGerrit Renker u64 val; 310422d9cdcSGerrit Renker 311422d9cdcSGerrit Renker if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX) 312422d9cdcSGerrit Renker return -1; 313422d9cdcSGerrit Renker if (dccp_feat_table[idx].activation_hdlr == NULL) 314422d9cdcSGerrit Renker return 0; 315422d9cdcSGerrit Renker 316422d9cdcSGerrit Renker if (fval == NULL) { 317422d9cdcSGerrit Renker val = dccp_feat_table[idx].default_value; 318422d9cdcSGerrit Renker } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) { 319422d9cdcSGerrit Renker if (fval->sp.vec == NULL) { 320422d9cdcSGerrit Renker /* 321422d9cdcSGerrit Renker * This can happen when an empty Confirm is sent 322422d9cdcSGerrit Renker * for an SP (i.e. known) feature. In this case 323422d9cdcSGerrit Renker * we would be using the default anyway. 324422d9cdcSGerrit Renker */ 325422d9cdcSGerrit Renker DCCP_CRIT("Feature #%d undefined: using default", idx); 326422d9cdcSGerrit Renker val = dccp_feat_table[idx].default_value; 327422d9cdcSGerrit Renker } else { 328422d9cdcSGerrit Renker val = fval->sp.vec[0]; 329422d9cdcSGerrit Renker } 330422d9cdcSGerrit Renker } else { 331422d9cdcSGerrit Renker val = fval->nn; 332422d9cdcSGerrit Renker } 333422d9cdcSGerrit Renker 334422d9cdcSGerrit Renker /* Location is RX if this is a local-RX or remote-TX feature */ 335422d9cdcSGerrit Renker rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX)); 336422d9cdcSGerrit Renker 337f3f3abb6SGerrit Renker dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX", 338f3f3abb6SGerrit Renker dccp_feat_fname(dccp_feat_table[idx].feat_num), 339f3f3abb6SGerrit Renker fval ? "" : "default ", (unsigned long long)val); 340f3f3abb6SGerrit Renker 341422d9cdcSGerrit Renker return dccp_feat_table[idx].activation_hdlr(sk, val, rx); 342422d9cdcSGerrit Renker } 343422d9cdcSGerrit Renker 34444e6fd9eSGerrit Renker /** 34544e6fd9eSGerrit Renker * dccp_feat_activate - Activate feature value on socket 34644e6fd9eSGerrit Renker * @sk: fully connected DCCP socket (after handshake is complete) 34744e6fd9eSGerrit Renker * @feat_num: feature to activate, one of %dccp_feature_numbers 34844e6fd9eSGerrit Renker * @local: whether local (1) or remote (0) @feat_num is meant 34944e6fd9eSGerrit Renker * @fval: the value (SP or NN) to activate, or NULL to use the default value 3502c53040fSBen Hutchings * 35144e6fd9eSGerrit Renker * For general use this function is preferable over __dccp_feat_activate(). 35244e6fd9eSGerrit Renker */ 35344e6fd9eSGerrit Renker static int dccp_feat_activate(struct sock *sk, u8 feat_num, bool local, 35444e6fd9eSGerrit Renker dccp_feat_val const *fval) 35544e6fd9eSGerrit Renker { 35644e6fd9eSGerrit Renker return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval); 35744e6fd9eSGerrit Renker } 35844e6fd9eSGerrit Renker 359b1ad0042SGerrit Renker /* Test for "Req'd" feature (RFC 4340, 6.4) */ 360b1ad0042SGerrit Renker static inline int dccp_feat_must_be_understood(u8 feat_num) 361b1ad0042SGerrit Renker { 362b1ad0042SGerrit Renker return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS || 363b1ad0042SGerrit Renker feat_num == DCCPF_SEQUENCE_WINDOW; 364b1ad0042SGerrit Renker } 365b1ad0042SGerrit Renker 366ac75773cSGerrit Renker /* copy constructor, fval must not already contain allocated memory */ 367ac75773cSGerrit Renker static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len) 368ac75773cSGerrit Renker { 369ac75773cSGerrit Renker fval->sp.len = len; 370ac75773cSGerrit Renker if (fval->sp.len > 0) { 371ac75773cSGerrit Renker fval->sp.vec = kmemdup(val, len, gfp_any()); 372ac75773cSGerrit Renker if (fval->sp.vec == NULL) { 373ac75773cSGerrit Renker fval->sp.len = 0; 374ac75773cSGerrit Renker return -ENOBUFS; 375ac75773cSGerrit Renker } 376ac75773cSGerrit Renker } 377ac75773cSGerrit Renker return 0; 378ac75773cSGerrit Renker } 379ac75773cSGerrit Renker 38061e6473eSGerrit Renker static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val) 38161e6473eSGerrit Renker { 38261e6473eSGerrit Renker if (unlikely(val == NULL)) 38361e6473eSGerrit Renker return; 38461e6473eSGerrit Renker if (dccp_feat_type(feat_num) == FEAT_SP) 38561e6473eSGerrit Renker kfree(val->sp.vec); 38661e6473eSGerrit Renker memset(val, 0, sizeof(*val)); 38761e6473eSGerrit Renker } 38861e6473eSGerrit Renker 389ac75773cSGerrit Renker static struct dccp_feat_entry * 390ac75773cSGerrit Renker dccp_feat_clone_entry(struct dccp_feat_entry const *original) 391ac75773cSGerrit Renker { 392ac75773cSGerrit Renker struct dccp_feat_entry *new; 393ac75773cSGerrit Renker u8 type = dccp_feat_type(original->feat_num); 394ac75773cSGerrit Renker 395ac75773cSGerrit Renker if (type == FEAT_UNKNOWN) 396ac75773cSGerrit Renker return NULL; 397ac75773cSGerrit Renker 398ac75773cSGerrit Renker new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any()); 399ac75773cSGerrit Renker if (new == NULL) 400ac75773cSGerrit Renker return NULL; 401ac75773cSGerrit Renker 402ac75773cSGerrit Renker if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val, 403ac75773cSGerrit Renker original->val.sp.vec, 404ac75773cSGerrit Renker original->val.sp.len)) { 405ac75773cSGerrit Renker kfree(new); 406ac75773cSGerrit Renker return NULL; 407ac75773cSGerrit Renker } 408ac75773cSGerrit Renker return new; 409ac75773cSGerrit Renker } 410ac75773cSGerrit Renker 41161e6473eSGerrit Renker static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry) 41261e6473eSGerrit Renker { 41361e6473eSGerrit Renker if (entry != NULL) { 41461e6473eSGerrit Renker dccp_feat_val_destructor(entry->feat_num, &entry->val); 41561e6473eSGerrit Renker kfree(entry); 41661e6473eSGerrit Renker } 41761e6473eSGerrit Renker } 41861e6473eSGerrit Renker 41961e6473eSGerrit Renker /* 42061e6473eSGerrit Renker * List management functions 42161e6473eSGerrit Renker * 42261e6473eSGerrit Renker * Feature negotiation lists rely on and maintain the following invariants: 42361e6473eSGerrit Renker * - each feat_num in the list is known, i.e. we know its type and default value 42461e6473eSGerrit Renker * - each feat_num/is_local combination is unique (old entries are overwritten) 42561e6473eSGerrit Renker * - SP values are always freshly allocated 42661e6473eSGerrit Renker * - list is sorted in increasing order of feature number (faster lookup) 42761e6473eSGerrit Renker */ 4280c116839SGerrit Renker static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list, 4290c116839SGerrit Renker u8 feat_num, bool is_local) 4300c116839SGerrit Renker { 4310c116839SGerrit Renker struct dccp_feat_entry *entry; 4320c116839SGerrit Renker 4333d3e35aaSGerrit Renker list_for_each_entry(entry, fn_list, node) { 4340c116839SGerrit Renker if (entry->feat_num == feat_num && entry->is_local == is_local) 4350c116839SGerrit Renker return entry; 4360c116839SGerrit Renker else if (entry->feat_num > feat_num) 4370c116839SGerrit Renker break; 4383d3e35aaSGerrit Renker } 4390c116839SGerrit Renker return NULL; 4400c116839SGerrit Renker } 44161e6473eSGerrit Renker 442e8ef967aSGerrit Renker /** 443e8ef967aSGerrit Renker * dccp_feat_entry_new - Central list update routine (called by all others) 444e8ef967aSGerrit Renker * @head: list to add to 445e8ef967aSGerrit Renker * @feat: feature number 446e8ef967aSGerrit Renker * @local: whether the local (1) or remote feature with number @feat is meant 4472c53040fSBen Hutchings * 448e8ef967aSGerrit Renker * This is the only constructor and serves to ensure the above invariants. 449e8ef967aSGerrit Renker */ 450e8ef967aSGerrit Renker static struct dccp_feat_entry * 451e8ef967aSGerrit Renker dccp_feat_entry_new(struct list_head *head, u8 feat, bool local) 452e8ef967aSGerrit Renker { 453e8ef967aSGerrit Renker struct dccp_feat_entry *entry; 454e8ef967aSGerrit Renker 455e8ef967aSGerrit Renker list_for_each_entry(entry, head, node) 456e8ef967aSGerrit Renker if (entry->feat_num == feat && entry->is_local == local) { 457e8ef967aSGerrit Renker dccp_feat_val_destructor(entry->feat_num, &entry->val); 458e8ef967aSGerrit Renker return entry; 459e8ef967aSGerrit Renker } else if (entry->feat_num > feat) { 460e8ef967aSGerrit Renker head = &entry->node; 461e8ef967aSGerrit Renker break; 462e8ef967aSGerrit Renker } 463e8ef967aSGerrit Renker 464e8ef967aSGerrit Renker entry = kmalloc(sizeof(*entry), gfp_any()); 465e8ef967aSGerrit Renker if (entry != NULL) { 466e8ef967aSGerrit Renker entry->feat_num = feat; 467e8ef967aSGerrit Renker entry->is_local = local; 468e8ef967aSGerrit Renker list_add_tail(&entry->node, head); 469e8ef967aSGerrit Renker } 470e8ef967aSGerrit Renker return entry; 471e8ef967aSGerrit Renker } 472e8ef967aSGerrit Renker 473e8ef967aSGerrit Renker /** 474e8ef967aSGerrit Renker * dccp_feat_push_change - Add/overwrite a Change option in the list 475e8ef967aSGerrit Renker * @fn_list: feature-negotiation list to update 476e8ef967aSGerrit Renker * @feat: one of %dccp_feature_numbers 477e8ef967aSGerrit Renker * @local: whether local (1) or remote (0) @feat_num is meant 4786d80c473SFabian Frederick * @mandatory: whether to use Mandatory feature negotiation options 479e8ef967aSGerrit Renker * @fval: pointer to NN/SP value to be inserted (will be copied) 480e8ef967aSGerrit Renker */ 481e8ef967aSGerrit Renker static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local, 482e8ef967aSGerrit Renker u8 mandatory, dccp_feat_val *fval) 483e8ef967aSGerrit Renker { 484e8ef967aSGerrit Renker struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local); 485e8ef967aSGerrit Renker 486e8ef967aSGerrit Renker if (new == NULL) 487e8ef967aSGerrit Renker return -ENOMEM; 488e8ef967aSGerrit Renker 489e8ef967aSGerrit Renker new->feat_num = feat; 490e8ef967aSGerrit Renker new->is_local = local; 491e8ef967aSGerrit Renker new->state = FEAT_INITIALISING; 4923db1cd5cSRusty Russell new->needs_confirm = false; 4933db1cd5cSRusty Russell new->empty_confirm = false; 494e8ef967aSGerrit Renker new->val = *fval; 495e8ef967aSGerrit Renker new->needs_mandatory = mandatory; 496e8ef967aSGerrit Renker 497e8ef967aSGerrit Renker return 0; 498e8ef967aSGerrit Renker } 499e8ef967aSGerrit Renker 500e77b8363SGerrit Renker /** 501e77b8363SGerrit Renker * dccp_feat_push_confirm - Add a Confirm entry to the FN list 502e77b8363SGerrit Renker * @fn_list: feature-negotiation list to add to 503e77b8363SGerrit Renker * @feat: one of %dccp_feature_numbers 504e77b8363SGerrit Renker * @local: whether local (1) or remote (0) @feat_num is being confirmed 505e77b8363SGerrit Renker * @fval: pointer to NN/SP value to be inserted or NULL 5062c53040fSBen Hutchings * 507e77b8363SGerrit Renker * Returns 0 on success, a Reset code for further processing otherwise. 508e77b8363SGerrit Renker */ 509e77b8363SGerrit Renker static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local, 510e77b8363SGerrit Renker dccp_feat_val *fval) 511e77b8363SGerrit Renker { 512e77b8363SGerrit Renker struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local); 513e77b8363SGerrit Renker 514e77b8363SGerrit Renker if (new == NULL) 515e77b8363SGerrit Renker return DCCP_RESET_CODE_TOO_BUSY; 516e77b8363SGerrit Renker 517e77b8363SGerrit Renker new->feat_num = feat; 518e77b8363SGerrit Renker new->is_local = local; 519e77b8363SGerrit Renker new->state = FEAT_STABLE; /* transition in 6.6.2 */ 5203db1cd5cSRusty Russell new->needs_confirm = true; 521e77b8363SGerrit Renker new->empty_confirm = (fval == NULL); 522e77b8363SGerrit Renker new->val.nn = 0; /* zeroes the whole structure */ 523e77b8363SGerrit Renker if (!new->empty_confirm) 524e77b8363SGerrit Renker new->val = *fval; 5253db1cd5cSRusty Russell new->needs_mandatory = false; 526e77b8363SGerrit Renker 527e77b8363SGerrit Renker return 0; 528e77b8363SGerrit Renker } 529e77b8363SGerrit Renker 530e77b8363SGerrit Renker static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local) 531e77b8363SGerrit Renker { 532e77b8363SGerrit Renker return dccp_feat_push_confirm(fn_list, feat, local, NULL); 533e77b8363SGerrit Renker } 534e77b8363SGerrit Renker 53561e6473eSGerrit Renker static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry) 53661e6473eSGerrit Renker { 53761e6473eSGerrit Renker list_del(&entry->node); 53861e6473eSGerrit Renker dccp_feat_entry_destructor(entry); 53961e6473eSGerrit Renker } 54061e6473eSGerrit Renker 54161e6473eSGerrit Renker void dccp_feat_list_purge(struct list_head *fn_list) 54261e6473eSGerrit Renker { 54361e6473eSGerrit Renker struct dccp_feat_entry *entry, *next; 54461e6473eSGerrit Renker 54561e6473eSGerrit Renker list_for_each_entry_safe(entry, next, fn_list, node) 54661e6473eSGerrit Renker dccp_feat_entry_destructor(entry); 54761e6473eSGerrit Renker INIT_LIST_HEAD(fn_list); 54861e6473eSGerrit Renker } 54961e6473eSGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_list_purge); 55061e6473eSGerrit Renker 551ac75773cSGerrit Renker /* generate @to as full clone of @from - @to must not contain any nodes */ 552ac75773cSGerrit Renker int dccp_feat_clone_list(struct list_head const *from, struct list_head *to) 553ac75773cSGerrit Renker { 554ac75773cSGerrit Renker struct dccp_feat_entry *entry, *new; 555ac75773cSGerrit Renker 556ac75773cSGerrit Renker INIT_LIST_HEAD(to); 557ac75773cSGerrit Renker list_for_each_entry(entry, from, node) { 558ac75773cSGerrit Renker new = dccp_feat_clone_entry(entry); 559ac75773cSGerrit Renker if (new == NULL) 560ac75773cSGerrit Renker goto cloning_failed; 561ac75773cSGerrit Renker list_add_tail(&new->node, to); 562ac75773cSGerrit Renker } 563ac75773cSGerrit Renker return 0; 564ac75773cSGerrit Renker 565ac75773cSGerrit Renker cloning_failed: 566ac75773cSGerrit Renker dccp_feat_list_purge(to); 567ac75773cSGerrit Renker return -ENOMEM; 568ac75773cSGerrit Renker } 569ac75773cSGerrit Renker 5700971d17cSGerrit Renker /** 5710971d17cSGerrit Renker * dccp_feat_valid_nn_length - Enforce length constraints on NN options 572d0b1101bSAndrew Lunn * @feat_num: feature to return length of, one of %dccp_feature_numbers 573d0b1101bSAndrew Lunn * 5740971d17cSGerrit Renker * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only, 5750971d17cSGerrit Renker * incoming options are accepted as long as their values are valid. 5760971d17cSGerrit Renker */ 5770971d17cSGerrit Renker static u8 dccp_feat_valid_nn_length(u8 feat_num) 5780971d17cSGerrit Renker { 5790971d17cSGerrit Renker if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */ 5800971d17cSGerrit Renker return 2; 5810971d17cSGerrit Renker if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */ 5820971d17cSGerrit Renker return 6; 5830971d17cSGerrit Renker return 0; 5840971d17cSGerrit Renker } 5850971d17cSGerrit Renker 586e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val) 587e8ef967aSGerrit Renker { 588e8ef967aSGerrit Renker switch (feat_num) { 589e8ef967aSGerrit Renker case DCCPF_ACK_RATIO: 590e8ef967aSGerrit Renker return val <= DCCPF_ACK_RATIO_MAX; 591e8ef967aSGerrit Renker case DCCPF_SEQUENCE_WINDOW: 592e8ef967aSGerrit Renker return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX; 593e8ef967aSGerrit Renker } 594e8ef967aSGerrit Renker return 0; /* feature unknown - so we can't tell */ 595e8ef967aSGerrit Renker } 596e8ef967aSGerrit Renker 597e8ef967aSGerrit Renker /* check that SP values are within the ranges defined in RFC 4340 */ 598e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val) 599e8ef967aSGerrit Renker { 600e8ef967aSGerrit Renker switch (feat_num) { 601e8ef967aSGerrit Renker case DCCPF_CCID: 602e8ef967aSGerrit Renker return val == DCCPC_CCID2 || val == DCCPC_CCID3; 603e8ef967aSGerrit Renker /* Type-check Boolean feature values: */ 604e8ef967aSGerrit Renker case DCCPF_SHORT_SEQNOS: 605e8ef967aSGerrit Renker case DCCPF_ECN_INCAPABLE: 606e8ef967aSGerrit Renker case DCCPF_SEND_ACK_VECTOR: 607e8ef967aSGerrit Renker case DCCPF_SEND_NDP_COUNT: 608e8ef967aSGerrit Renker case DCCPF_DATA_CHECKSUM: 609e8ef967aSGerrit Renker case DCCPF_SEND_LEV_RATE: 610e8ef967aSGerrit Renker return val < 2; 611e8ef967aSGerrit Renker case DCCPF_MIN_CSUM_COVER: 612e8ef967aSGerrit Renker return val < 16; 613e8ef967aSGerrit Renker } 614e8ef967aSGerrit Renker return 0; /* feature unknown */ 615e8ef967aSGerrit Renker } 616e8ef967aSGerrit Renker 617e8ef967aSGerrit Renker static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len) 618e8ef967aSGerrit Renker { 619e8ef967aSGerrit Renker if (sp_list == NULL || sp_len < 1) 620e8ef967aSGerrit Renker return 0; 621e8ef967aSGerrit Renker while (sp_len--) 622e8ef967aSGerrit Renker if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++)) 623e8ef967aSGerrit Renker return 0; 624e8ef967aSGerrit Renker return 1; 625e8ef967aSGerrit Renker } 626e8ef967aSGerrit Renker 627e8ef967aSGerrit Renker /** 6280971d17cSGerrit Renker * dccp_feat_insert_opts - Generate FN options from current list state 6290971d17cSGerrit Renker * @skb: next sk_buff to be sent to the peer 6300971d17cSGerrit Renker * @dp: for client during handshake and general negotiation 6310971d17cSGerrit Renker * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND) 6320971d17cSGerrit Renker */ 6330971d17cSGerrit Renker int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq, 6340971d17cSGerrit Renker struct sk_buff *skb) 6350971d17cSGerrit Renker { 6360971d17cSGerrit Renker struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg; 6370971d17cSGerrit Renker struct dccp_feat_entry *pos, *next; 6380971d17cSGerrit Renker u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN]; 6390971d17cSGerrit Renker bool rpt; 6400971d17cSGerrit Renker 6410971d17cSGerrit Renker /* put entries into @skb in the order they appear in the list */ 6420971d17cSGerrit Renker list_for_each_entry_safe_reverse(pos, next, fn, node) { 6430971d17cSGerrit Renker opt = dccp_feat_genopt(pos); 6440971d17cSGerrit Renker type = dccp_feat_type(pos->feat_num); 6450971d17cSGerrit Renker rpt = false; 6460971d17cSGerrit Renker 6470971d17cSGerrit Renker if (pos->empty_confirm) { 6480971d17cSGerrit Renker len = 0; 6490971d17cSGerrit Renker ptr = NULL; 6500971d17cSGerrit Renker } else { 6510971d17cSGerrit Renker if (type == FEAT_SP) { 6520971d17cSGerrit Renker len = pos->val.sp.len; 6530971d17cSGerrit Renker ptr = pos->val.sp.vec; 6540971d17cSGerrit Renker rpt = pos->needs_confirm; 6550971d17cSGerrit Renker } else if (type == FEAT_NN) { 6560971d17cSGerrit Renker len = dccp_feat_valid_nn_length(pos->feat_num); 6570971d17cSGerrit Renker ptr = nn_in_nbo; 6580971d17cSGerrit Renker dccp_encode_value_var(pos->val.nn, ptr, len); 6590971d17cSGerrit Renker } else { 6600971d17cSGerrit Renker DCCP_BUG("unknown feature %u", pos->feat_num); 6610971d17cSGerrit Renker return -1; 6620971d17cSGerrit Renker } 6630971d17cSGerrit Renker } 664f3f3abb6SGerrit Renker dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0); 6650971d17cSGerrit Renker 6660971d17cSGerrit Renker if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt)) 6670971d17cSGerrit Renker return -1; 6680971d17cSGerrit Renker if (pos->needs_mandatory && dccp_insert_option_mandatory(skb)) 6690971d17cSGerrit Renker return -1; 670a6444f42SSamuel Jero 671a6444f42SSamuel Jero if (skb->sk->sk_state == DCCP_OPEN && 672a6444f42SSamuel Jero (opt == DCCPO_CONFIRM_R || opt == DCCPO_CONFIRM_L)) { 6730971d17cSGerrit Renker /* 674a6444f42SSamuel Jero * Confirms don't get retransmitted (6.6.3) once the 675a6444f42SSamuel Jero * connection is in state OPEN 676a6444f42SSamuel Jero */ 677a6444f42SSamuel Jero dccp_feat_list_pop(pos); 678a6444f42SSamuel Jero } else { 679a6444f42SSamuel Jero /* 680a6444f42SSamuel Jero * Enter CHANGING after transmitting the Change 681a6444f42SSamuel Jero * option (6.6.2). 6820971d17cSGerrit Renker */ 6830971d17cSGerrit Renker if (pos->state == FEAT_INITIALISING) 6840971d17cSGerrit Renker pos->state = FEAT_CHANGING; 6850971d17cSGerrit Renker } 686a6444f42SSamuel Jero } 6870971d17cSGerrit Renker return 0; 6880971d17cSGerrit Renker } 6890971d17cSGerrit Renker 6900971d17cSGerrit Renker /** 691e8ef967aSGerrit Renker * __feat_register_nn - Register new NN value on socket 692e8ef967aSGerrit Renker * @fn: feature-negotiation list to register with 693e8ef967aSGerrit Renker * @feat: an NN feature from %dccp_feature_numbers 694e8ef967aSGerrit Renker * @mandatory: use Mandatory option if 1 695e8ef967aSGerrit Renker * @nn_val: value to register (restricted to 4 bytes) 6962c53040fSBen Hutchings * 697e8ef967aSGerrit Renker * Note that NN features are local by definition (RFC 4340, 6.3.2). 698e8ef967aSGerrit Renker */ 699e8ef967aSGerrit Renker static int __feat_register_nn(struct list_head *fn, u8 feat, 700e8ef967aSGerrit Renker u8 mandatory, u64 nn_val) 701e8ef967aSGerrit Renker { 702e8ef967aSGerrit Renker dccp_feat_val fval = { .nn = nn_val }; 703e8ef967aSGerrit Renker 704e8ef967aSGerrit Renker if (dccp_feat_type(feat) != FEAT_NN || 705e8ef967aSGerrit Renker !dccp_feat_is_valid_nn_val(feat, nn_val)) 706e8ef967aSGerrit Renker return -EINVAL; 707e8ef967aSGerrit Renker 708e8ef967aSGerrit Renker /* Don't bother with default values, they will be activated anyway. */ 709e8ef967aSGerrit Renker if (nn_val - (u64)dccp_feat_default_value(feat) == 0) 710e8ef967aSGerrit Renker return 0; 711e8ef967aSGerrit Renker 712e8ef967aSGerrit Renker return dccp_feat_push_change(fn, feat, 1, mandatory, &fval); 713e8ef967aSGerrit Renker } 714e8ef967aSGerrit Renker 715e8ef967aSGerrit Renker /** 716e8ef967aSGerrit Renker * __feat_register_sp - Register new SP value/list on socket 717e8ef967aSGerrit Renker * @fn: feature-negotiation list to register with 718e8ef967aSGerrit Renker * @feat: an SP feature from %dccp_feature_numbers 719e8ef967aSGerrit Renker * @is_local: whether the local (1) or the remote (0) @feat is meant 720e8ef967aSGerrit Renker * @mandatory: use Mandatory option if 1 721e8ef967aSGerrit Renker * @sp_val: SP value followed by optional preference list 722e8ef967aSGerrit Renker * @sp_len: length of @sp_val in bytes 723e8ef967aSGerrit Renker */ 724e8ef967aSGerrit Renker static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local, 725e8ef967aSGerrit Renker u8 mandatory, u8 const *sp_val, u8 sp_len) 726e8ef967aSGerrit Renker { 727e8ef967aSGerrit Renker dccp_feat_val fval; 728e8ef967aSGerrit Renker 729e8ef967aSGerrit Renker if (dccp_feat_type(feat) != FEAT_SP || 730e8ef967aSGerrit Renker !dccp_feat_sp_list_ok(feat, sp_val, sp_len)) 731e8ef967aSGerrit Renker return -EINVAL; 732e8ef967aSGerrit Renker 733d90ebcbfSGerrit Renker /* Avoid negotiating alien CCIDs by only advertising supported ones */ 734d90ebcbfSGerrit Renker if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len)) 735d90ebcbfSGerrit Renker return -EOPNOTSUPP; 736d90ebcbfSGerrit Renker 737e8ef967aSGerrit Renker if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len)) 738e8ef967aSGerrit Renker return -ENOMEM; 739e8ef967aSGerrit Renker 7401d3ff095SYueHaibing if (dccp_feat_push_change(fn, feat, is_local, mandatory, &fval)) { 7411d3ff095SYueHaibing kfree(fval.sp.vec); 7421d3ff095SYueHaibing return -ENOMEM; 7431d3ff095SYueHaibing } 7441d3ff095SYueHaibing 7451d3ff095SYueHaibing return 0; 746e8ef967aSGerrit Renker } 747e8ef967aSGerrit Renker 74849aebc66SGerrit Renker /** 74949aebc66SGerrit Renker * dccp_feat_register_sp - Register requests to change SP feature values 75049aebc66SGerrit Renker * @sk: client or listening socket 75149aebc66SGerrit Renker * @feat: one of %dccp_feature_numbers 75249aebc66SGerrit Renker * @is_local: whether the local (1) or remote (0) @feat is meant 75349aebc66SGerrit Renker * @list: array of preferred values, in descending order of preference 75449aebc66SGerrit Renker * @len: length of @list in bytes 75549aebc66SGerrit Renker */ 75649aebc66SGerrit Renker int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local, 75749aebc66SGerrit Renker u8 const *list, u8 len) 75849aebc66SGerrit Renker { /* any changes must be registered before establishing the connection */ 75949aebc66SGerrit Renker if (sk->sk_state != DCCP_CLOSED) 76049aebc66SGerrit Renker return -EISCONN; 76149aebc66SGerrit Renker if (dccp_feat_type(feat) != FEAT_SP) 76219443178SChris Wright return -EINVAL; 76349aebc66SGerrit Renker return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local, 76449aebc66SGerrit Renker 0, list, len); 765afe00251SAndrea Bittau } 766afe00251SAndrea Bittau 767d6916f87SGerrit Renker /** 768d6916f87SGerrit Renker * dccp_feat_nn_get - Query current/pending value of NN feature 769d6916f87SGerrit Renker * @sk: DCCP socket of an established connection 770d6916f87SGerrit Renker * @feat: NN feature number from %dccp_feature_numbers 7712c53040fSBen Hutchings * 772d6916f87SGerrit Renker * For a known NN feature, returns value currently being negotiated, or 773d6916f87SGerrit Renker * current (confirmed) value if no negotiation is going on. 774d6916f87SGerrit Renker */ 775d6916f87SGerrit Renker u64 dccp_feat_nn_get(struct sock *sk, u8 feat) 776d6916f87SGerrit Renker { 777d6916f87SGerrit Renker if (dccp_feat_type(feat) == FEAT_NN) { 778d6916f87SGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 779d6916f87SGerrit Renker struct dccp_feat_entry *entry; 780d6916f87SGerrit Renker 781d6916f87SGerrit Renker entry = dccp_feat_list_lookup(&dp->dccps_featneg, feat, 1); 782d6916f87SGerrit Renker if (entry != NULL) 783d6916f87SGerrit Renker return entry->val.nn; 784d6916f87SGerrit Renker 785d6916f87SGerrit Renker switch (feat) { 786d6916f87SGerrit Renker case DCCPF_ACK_RATIO: 787d6916f87SGerrit Renker return dp->dccps_l_ack_ratio; 788d6916f87SGerrit Renker case DCCPF_SEQUENCE_WINDOW: 789d6916f87SGerrit Renker return dp->dccps_l_seq_win; 790d6916f87SGerrit Renker } 791d6916f87SGerrit Renker } 792d6916f87SGerrit Renker DCCP_BUG("attempt to look up unsupported feature %u", feat); 793d6916f87SGerrit Renker return 0; 794d6916f87SGerrit Renker } 795d6916f87SGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_nn_get); 796d6916f87SGerrit Renker 797d6916f87SGerrit Renker /** 798d6916f87SGerrit Renker * dccp_feat_signal_nn_change - Update NN values for an established connection 799d6916f87SGerrit Renker * @sk: DCCP socket of an established connection 800d6916f87SGerrit Renker * @feat: NN feature number from %dccp_feature_numbers 801d6916f87SGerrit Renker * @nn_val: the new value to use 8022c53040fSBen Hutchings * 803d6916f87SGerrit Renker * This function is used to communicate NN updates out-of-band. 804d6916f87SGerrit Renker */ 805d6916f87SGerrit Renker int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val) 806d6916f87SGerrit Renker { 807d6916f87SGerrit Renker struct list_head *fn = &dccp_sk(sk)->dccps_featneg; 808d6916f87SGerrit Renker dccp_feat_val fval = { .nn = nn_val }; 809d6916f87SGerrit Renker struct dccp_feat_entry *entry; 810d6916f87SGerrit Renker 811d6916f87SGerrit Renker if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN) 812d6916f87SGerrit Renker return 0; 813d6916f87SGerrit Renker 814d6916f87SGerrit Renker if (dccp_feat_type(feat) != FEAT_NN || 815d6916f87SGerrit Renker !dccp_feat_is_valid_nn_val(feat, nn_val)) 816d6916f87SGerrit Renker return -EINVAL; 817d6916f87SGerrit Renker 818d6916f87SGerrit Renker if (nn_val == dccp_feat_nn_get(sk, feat)) 819d6916f87SGerrit Renker return 0; /* already set or negotiation under way */ 820d6916f87SGerrit Renker 821d6916f87SGerrit Renker entry = dccp_feat_list_lookup(fn, feat, 1); 822d6916f87SGerrit Renker if (entry != NULL) { 823d6916f87SGerrit Renker dccp_pr_debug("Clobbering existing NN entry %llu -> %llu\n", 824d6916f87SGerrit Renker (unsigned long long)entry->val.nn, 825d6916f87SGerrit Renker (unsigned long long)nn_val); 826d6916f87SGerrit Renker dccp_feat_list_pop(entry); 827d6916f87SGerrit Renker } 828d6916f87SGerrit Renker 829d6916f87SGerrit Renker inet_csk_schedule_ack(sk); 830d6916f87SGerrit Renker return dccp_feat_push_change(fn, feat, 1, 0, &fval); 831d6916f87SGerrit Renker } 832d6916f87SGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change); 833afe00251SAndrea Bittau 8349eca0a47SGerrit Renker /* 8359eca0a47SGerrit Renker * Tracking features whose value depend on the choice of CCID 8369eca0a47SGerrit Renker * 8379eca0a47SGerrit Renker * This is designed with an extension in mind so that a list walk could be done 8389eca0a47SGerrit Renker * before activating any features. However, the existing framework was found to 8399eca0a47SGerrit Renker * work satisfactorily up until now, the automatic verification is left open. 8409eca0a47SGerrit Renker * When adding new CCIDs, add a corresponding dependency table here. 8419eca0a47SGerrit Renker */ 8429eca0a47SGerrit Renker static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local) 8439eca0a47SGerrit Renker { 8449eca0a47SGerrit Renker static const struct ccid_dependency ccid2_dependencies[2][2] = { 8459eca0a47SGerrit Renker /* 8469eca0a47SGerrit Renker * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX 8479eca0a47SGerrit Renker * feature and Send Ack Vector is an RX feature, `is_local' 8489eca0a47SGerrit Renker * needs to be reversed. 8499eca0a47SGerrit Renker */ 8509eca0a47SGerrit Renker { /* Dependencies of the receiver-side (remote) CCID2 */ 8519eca0a47SGerrit Renker { 8529eca0a47SGerrit Renker .dependent_feat = DCCPF_SEND_ACK_VECTOR, 8539eca0a47SGerrit Renker .is_local = true, 8549eca0a47SGerrit Renker .is_mandatory = true, 8559eca0a47SGerrit Renker .val = 1 8569eca0a47SGerrit Renker }, 8579eca0a47SGerrit Renker { 0, 0, 0, 0 } 8589eca0a47SGerrit Renker }, 8599eca0a47SGerrit Renker { /* Dependencies of the sender-side (local) CCID2 */ 8609eca0a47SGerrit Renker { 8619eca0a47SGerrit Renker .dependent_feat = DCCPF_SEND_ACK_VECTOR, 8629eca0a47SGerrit Renker .is_local = false, 8639eca0a47SGerrit Renker .is_mandatory = true, 8649eca0a47SGerrit Renker .val = 1 8659eca0a47SGerrit Renker }, 8669eca0a47SGerrit Renker { 0, 0, 0, 0 } 8679eca0a47SGerrit Renker } 8689eca0a47SGerrit Renker }; 8699eca0a47SGerrit Renker static const struct ccid_dependency ccid3_dependencies[2][5] = { 8709eca0a47SGerrit Renker { /* 8719eca0a47SGerrit Renker * Dependencies of the receiver-side CCID3 8729eca0a47SGerrit Renker */ 8739eca0a47SGerrit Renker { /* locally disable Ack Vectors */ 8749eca0a47SGerrit Renker .dependent_feat = DCCPF_SEND_ACK_VECTOR, 8759eca0a47SGerrit Renker .is_local = true, 8769eca0a47SGerrit Renker .is_mandatory = false, 8779eca0a47SGerrit Renker .val = 0 8789eca0a47SGerrit Renker }, 8799eca0a47SGerrit Renker { /* see below why Send Loss Event Rate is on */ 8809eca0a47SGerrit Renker .dependent_feat = DCCPF_SEND_LEV_RATE, 8819eca0a47SGerrit Renker .is_local = true, 8829eca0a47SGerrit Renker .is_mandatory = true, 8839eca0a47SGerrit Renker .val = 1 8849eca0a47SGerrit Renker }, 8859eca0a47SGerrit Renker { /* NDP Count is needed as per RFC 4342, 6.1.1 */ 8869eca0a47SGerrit Renker .dependent_feat = DCCPF_SEND_NDP_COUNT, 8879eca0a47SGerrit Renker .is_local = false, 8889eca0a47SGerrit Renker .is_mandatory = true, 8899eca0a47SGerrit Renker .val = 1 8909eca0a47SGerrit Renker }, 8919eca0a47SGerrit Renker { 0, 0, 0, 0 }, 8929eca0a47SGerrit Renker }, 8939eca0a47SGerrit Renker { /* 8949eca0a47SGerrit Renker * CCID3 at the TX side: we request that the HC-receiver 8959eca0a47SGerrit Renker * will not send Ack Vectors (they will be ignored, so 8969eca0a47SGerrit Renker * Mandatory is not set); we enable Send Loss Event Rate 8979eca0a47SGerrit Renker * (Mandatory since the implementation does not support 8989eca0a47SGerrit Renker * the Loss Intervals option of RFC 4342, 8.6). 8999eca0a47SGerrit Renker * The last two options are for peer's information only. 9009eca0a47SGerrit Renker */ 9019eca0a47SGerrit Renker { 9029eca0a47SGerrit Renker .dependent_feat = DCCPF_SEND_ACK_VECTOR, 9039eca0a47SGerrit Renker .is_local = false, 9049eca0a47SGerrit Renker .is_mandatory = false, 9059eca0a47SGerrit Renker .val = 0 9069eca0a47SGerrit Renker }, 9079eca0a47SGerrit Renker { 9089eca0a47SGerrit Renker .dependent_feat = DCCPF_SEND_LEV_RATE, 9099eca0a47SGerrit Renker .is_local = false, 9109eca0a47SGerrit Renker .is_mandatory = true, 9119eca0a47SGerrit Renker .val = 1 9129eca0a47SGerrit Renker }, 9139eca0a47SGerrit Renker { /* this CCID does not support Ack Ratio */ 9149eca0a47SGerrit Renker .dependent_feat = DCCPF_ACK_RATIO, 9159eca0a47SGerrit Renker .is_local = true, 9169eca0a47SGerrit Renker .is_mandatory = false, 9179eca0a47SGerrit Renker .val = 0 9189eca0a47SGerrit Renker }, 9199eca0a47SGerrit Renker { /* tell receiver we are sending NDP counts */ 9209eca0a47SGerrit Renker .dependent_feat = DCCPF_SEND_NDP_COUNT, 9219eca0a47SGerrit Renker .is_local = true, 9229eca0a47SGerrit Renker .is_mandatory = false, 9239eca0a47SGerrit Renker .val = 1 9249eca0a47SGerrit Renker }, 9259eca0a47SGerrit Renker { 0, 0, 0, 0 } 9269eca0a47SGerrit Renker } 9279eca0a47SGerrit Renker }; 9289eca0a47SGerrit Renker switch (ccid) { 9299eca0a47SGerrit Renker case DCCPC_CCID2: 9309eca0a47SGerrit Renker return ccid2_dependencies[is_local]; 9319eca0a47SGerrit Renker case DCCPC_CCID3: 9329eca0a47SGerrit Renker return ccid3_dependencies[is_local]; 9339eca0a47SGerrit Renker default: 9349eca0a47SGerrit Renker return NULL; 9359eca0a47SGerrit Renker } 9369eca0a47SGerrit Renker } 9379eca0a47SGerrit Renker 9389eca0a47SGerrit Renker /** 9399eca0a47SGerrit Renker * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID 9409eca0a47SGerrit Renker * @fn: feature-negotiation list to update 9419eca0a47SGerrit Renker * @id: CCID number to track 9429eca0a47SGerrit Renker * @is_local: whether TX CCID (1) or RX CCID (0) is meant 9432c53040fSBen Hutchings * 9449eca0a47SGerrit Renker * This function needs to be called after registering all other features. 9459eca0a47SGerrit Renker */ 9469eca0a47SGerrit Renker static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local) 9479eca0a47SGerrit Renker { 9489eca0a47SGerrit Renker const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local); 9499eca0a47SGerrit Renker int i, rc = (table == NULL); 9509eca0a47SGerrit Renker 9519eca0a47SGerrit Renker for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++) 9529eca0a47SGerrit Renker if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP) 9539eca0a47SGerrit Renker rc = __feat_register_sp(fn, table[i].dependent_feat, 9549eca0a47SGerrit Renker table[i].is_local, 9559eca0a47SGerrit Renker table[i].is_mandatory, 9569eca0a47SGerrit Renker &table[i].val, 1); 9579eca0a47SGerrit Renker else 9589eca0a47SGerrit Renker rc = __feat_register_nn(fn, table[i].dependent_feat, 9599eca0a47SGerrit Renker table[i].is_mandatory, 9609eca0a47SGerrit Renker table[i].val); 9619eca0a47SGerrit Renker return rc; 9629eca0a47SGerrit Renker } 9639eca0a47SGerrit Renker 9649eca0a47SGerrit Renker /** 9659eca0a47SGerrit Renker * dccp_feat_finalise_settings - Finalise settings before starting negotiation 9669eca0a47SGerrit Renker * @dp: client or listening socket (settings will be inherited) 9672c53040fSBen Hutchings * 9689eca0a47SGerrit Renker * This is called after all registrations (socket initialisation, sysctls, and 9699eca0a47SGerrit Renker * sockopt calls), and before sending the first packet containing Change options 9709eca0a47SGerrit Renker * (ie. client-Request or server-Response), to ensure internal consistency. 9719eca0a47SGerrit Renker */ 9729eca0a47SGerrit Renker int dccp_feat_finalise_settings(struct dccp_sock *dp) 9739eca0a47SGerrit Renker { 9749eca0a47SGerrit Renker struct list_head *fn = &dp->dccps_featneg; 9759eca0a47SGerrit Renker struct dccp_feat_entry *entry; 9769eca0a47SGerrit Renker int i = 2, ccids[2] = { -1, -1 }; 9779eca0a47SGerrit Renker 9789eca0a47SGerrit Renker /* 9799eca0a47SGerrit Renker * Propagating CCIDs: 9809eca0a47SGerrit Renker * 1) not useful to propagate CCID settings if this host advertises more 9819eca0a47SGerrit Renker * than one CCID: the choice of CCID may still change - if this is 9829eca0a47SGerrit Renker * the client, or if this is the server and the client sends 9839eca0a47SGerrit Renker * singleton CCID values. 9849eca0a47SGerrit Renker * 2) since is that propagate_ccid changes the list, we defer changing 9859eca0a47SGerrit Renker * the sorted list until after the traversal. 9869eca0a47SGerrit Renker */ 9879eca0a47SGerrit Renker list_for_each_entry(entry, fn, node) 9889eca0a47SGerrit Renker if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1) 9899eca0a47SGerrit Renker ccids[entry->is_local] = entry->val.sp.vec[0]; 9909eca0a47SGerrit Renker while (i--) 9919eca0a47SGerrit Renker if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i)) 9929eca0a47SGerrit Renker return -1; 993f3f3abb6SGerrit Renker dccp_feat_print_fnlist(fn); 9949eca0a47SGerrit Renker return 0; 9959eca0a47SGerrit Renker } 9969eca0a47SGerrit Renker 9970c116839SGerrit Renker /** 9980c116839SGerrit Renker * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features 999*0b609b55SAndrew Lunn * @dreq: server socket to resolve 1000*0b609b55SAndrew Lunn * 10010c116839SGerrit Renker * It is the server which resolves the dependencies once the CCID has been 10020c116839SGerrit Renker * fully negotiated. If no CCID has been negotiated, it uses the default CCID. 10030c116839SGerrit Renker */ 10040c116839SGerrit Renker int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq) 10050c116839SGerrit Renker { 10060c116839SGerrit Renker struct list_head *fn = &dreq->dreq_featneg; 10070c116839SGerrit Renker struct dccp_feat_entry *entry; 10080c116839SGerrit Renker u8 is_local, ccid; 10090c116839SGerrit Renker 10100c116839SGerrit Renker for (is_local = 0; is_local <= 1; is_local++) { 10110c116839SGerrit Renker entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local); 10120c116839SGerrit Renker 10130c116839SGerrit Renker if (entry != NULL && !entry->empty_confirm) 10140c116839SGerrit Renker ccid = entry->val.sp.vec[0]; 10150c116839SGerrit Renker else 10160c116839SGerrit Renker ccid = dccp_feat_default_value(DCCPF_CCID); 10170c116839SGerrit Renker 10180c116839SGerrit Renker if (dccp_feat_propagate_ccid(fn, ccid, is_local)) 10190c116839SGerrit Renker return -1; 10200c116839SGerrit Renker } 10210c116839SGerrit Renker return 0; 10220c116839SGerrit Renker } 10230c116839SGerrit Renker 102475757a7dSGerrit Renker /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */ 102575757a7dSGerrit Renker static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen) 102675757a7dSGerrit Renker { 102775757a7dSGerrit Renker u8 c, s; 102875757a7dSGerrit Renker 102975757a7dSGerrit Renker for (s = 0; s < slen; s++) 103075757a7dSGerrit Renker for (c = 0; c < clen; c++) 103175757a7dSGerrit Renker if (servlist[s] == clilist[c]) 103275757a7dSGerrit Renker return servlist[s]; 103375757a7dSGerrit Renker return -1; 103475757a7dSGerrit Renker } 103575757a7dSGerrit Renker 103675757a7dSGerrit Renker /** 103775757a7dSGerrit Renker * dccp_feat_prefer - Move preferred entry to the start of array 1038*0b609b55SAndrew Lunn * @preferred_value: entry to move to start of array 1039*0b609b55SAndrew Lunn * @array: array of preferred entries 1040*0b609b55SAndrew Lunn * @array_len: size of the array 1041*0b609b55SAndrew Lunn * 104275757a7dSGerrit Renker * Reorder the @array_len elements in @array so that @preferred_value comes 104375757a7dSGerrit Renker * first. Returns >0 to indicate that @preferred_value does occur in @array. 104475757a7dSGerrit Renker */ 104575757a7dSGerrit Renker static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len) 104675757a7dSGerrit Renker { 104775757a7dSGerrit Renker u8 i, does_occur = 0; 104875757a7dSGerrit Renker 104975757a7dSGerrit Renker if (array != NULL) { 105075757a7dSGerrit Renker for (i = 0; i < array_len; i++) 105175757a7dSGerrit Renker if (array[i] == preferred_value) { 105275757a7dSGerrit Renker array[i] = array[0]; 105375757a7dSGerrit Renker does_occur++; 105475757a7dSGerrit Renker } 105575757a7dSGerrit Renker if (does_occur) 105675757a7dSGerrit Renker array[0] = preferred_value; 105775757a7dSGerrit Renker } 105875757a7dSGerrit Renker return does_occur; 105975757a7dSGerrit Renker } 106075757a7dSGerrit Renker 106175757a7dSGerrit Renker /** 106275757a7dSGerrit Renker * dccp_feat_reconcile - Reconcile SP preference lists 10636d80c473SFabian Frederick * @fv: SP list to reconcile into 106475757a7dSGerrit Renker * @arr: received SP preference list 106575757a7dSGerrit Renker * @len: length of @arr in bytes 106675757a7dSGerrit Renker * @is_server: whether this side is the server (and @fv is the server's list) 106775757a7dSGerrit Renker * @reorder: whether to reorder the list in @fv after reconciling with @arr 106875757a7dSGerrit Renker * When successful, > 0 is returned and the reconciled list is in @fval. 106975757a7dSGerrit Renker * A value of 0 means that negotiation failed (no shared entry). 107075757a7dSGerrit Renker */ 107175757a7dSGerrit Renker static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len, 107275757a7dSGerrit Renker bool is_server, bool reorder) 107375757a7dSGerrit Renker { 107475757a7dSGerrit Renker int rc; 107575757a7dSGerrit Renker 107675757a7dSGerrit Renker if (!fv->sp.vec || !arr) { 107775757a7dSGerrit Renker DCCP_CRIT("NULL feature value or array"); 107875757a7dSGerrit Renker return 0; 107975757a7dSGerrit Renker } 108075757a7dSGerrit Renker 108175757a7dSGerrit Renker if (is_server) 108275757a7dSGerrit Renker rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len); 108375757a7dSGerrit Renker else 108475757a7dSGerrit Renker rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len); 108575757a7dSGerrit Renker 108675757a7dSGerrit Renker if (!reorder) 108775757a7dSGerrit Renker return rc; 108875757a7dSGerrit Renker if (rc < 0) 108975757a7dSGerrit Renker return 0; 109075757a7dSGerrit Renker 109175757a7dSGerrit Renker /* 109275757a7dSGerrit Renker * Reorder list: used for activating features and in dccp_insert_fn_opt. 109375757a7dSGerrit Renker */ 109475757a7dSGerrit Renker return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len); 109575757a7dSGerrit Renker } 109675757a7dSGerrit Renker 1097e77b8363SGerrit Renker /** 1098e77b8363SGerrit Renker * dccp_feat_change_recv - Process incoming ChangeL/R options 1099e77b8363SGerrit Renker * @fn: feature-negotiation list to update 1100e77b8363SGerrit Renker * @is_mandatory: whether the Change was preceded by a Mandatory option 1101e77b8363SGerrit Renker * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R 1102e77b8363SGerrit Renker * @feat: one of %dccp_feature_numbers 1103e77b8363SGerrit Renker * @val: NN value or SP value/preference list 1104e77b8363SGerrit Renker * @len: length of @val in bytes 1105e77b8363SGerrit Renker * @server: whether this node is the server (1) or the client (0) 1106e77b8363SGerrit Renker */ 1107e77b8363SGerrit Renker static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt, 1108e77b8363SGerrit Renker u8 feat, u8 *val, u8 len, const bool server) 1109e77b8363SGerrit Renker { 1110e77b8363SGerrit Renker u8 defval, type = dccp_feat_type(feat); 1111e77b8363SGerrit Renker const bool local = (opt == DCCPO_CHANGE_R); 1112e77b8363SGerrit Renker struct dccp_feat_entry *entry; 1113e77b8363SGerrit Renker dccp_feat_val fval; 1114e77b8363SGerrit Renker 1115e77b8363SGerrit Renker if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */ 1116e77b8363SGerrit Renker goto unknown_feature_or_value; 1117e77b8363SGerrit Renker 1118f3f3abb6SGerrit Renker dccp_feat_print_opt(opt, feat, val, len, is_mandatory); 1119f3f3abb6SGerrit Renker 1120e77b8363SGerrit Renker /* 1121e77b8363SGerrit Renker * Negotiation of NN features: Change R is invalid, so there is no 1122e77b8363SGerrit Renker * simultaneous negotiation; hence we do not look up in the list. 1123e77b8363SGerrit Renker */ 1124e77b8363SGerrit Renker if (type == FEAT_NN) { 1125e77b8363SGerrit Renker if (local || len > sizeof(fval.nn)) 1126e77b8363SGerrit Renker goto unknown_feature_or_value; 1127e77b8363SGerrit Renker 1128e77b8363SGerrit Renker /* 6.3.2: "The feature remote MUST accept any valid value..." */ 1129e77b8363SGerrit Renker fval.nn = dccp_decode_value_var(val, len); 1130e77b8363SGerrit Renker if (!dccp_feat_is_valid_nn_val(feat, fval.nn)) 1131e77b8363SGerrit Renker goto unknown_feature_or_value; 1132e77b8363SGerrit Renker 1133e77b8363SGerrit Renker return dccp_feat_push_confirm(fn, feat, local, &fval); 1134e77b8363SGerrit Renker } 1135e77b8363SGerrit Renker 1136e77b8363SGerrit Renker /* 1137e77b8363SGerrit Renker * Unidirectional/simultaneous negotiation of SP features (6.3.1) 1138e77b8363SGerrit Renker */ 1139e77b8363SGerrit Renker entry = dccp_feat_list_lookup(fn, feat, local); 1140e77b8363SGerrit Renker if (entry == NULL) { 1141e77b8363SGerrit Renker /* 1142e77b8363SGerrit Renker * No particular preferences have been registered. We deal with 1143e77b8363SGerrit Renker * this situation by assuming that all valid values are equally 1144e77b8363SGerrit Renker * acceptable, and apply the following checks: 1145e77b8363SGerrit Renker * - if the peer's list is a singleton, we accept a valid value; 1146e77b8363SGerrit Renker * - if we are the server, we first try to see if the peer (the 1147e77b8363SGerrit Renker * client) advertises the default value. If yes, we use it, 1148e77b8363SGerrit Renker * otherwise we accept the preferred value; 1149e77b8363SGerrit Renker * - else if we are the client, we use the first list element. 1150e77b8363SGerrit Renker */ 1151e77b8363SGerrit Renker if (dccp_feat_clone_sp_val(&fval, val, 1)) 1152e77b8363SGerrit Renker return DCCP_RESET_CODE_TOO_BUSY; 1153e77b8363SGerrit Renker 1154e77b8363SGerrit Renker if (len > 1 && server) { 1155e77b8363SGerrit Renker defval = dccp_feat_default_value(feat); 1156e77b8363SGerrit Renker if (dccp_feat_preflist_match(&defval, 1, val, len) > -1) 1157e77b8363SGerrit Renker fval.sp.vec[0] = defval; 1158e77b8363SGerrit Renker } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) { 1159e77b8363SGerrit Renker kfree(fval.sp.vec); 1160e77b8363SGerrit Renker goto unknown_feature_or_value; 1161e77b8363SGerrit Renker } 1162e77b8363SGerrit Renker 1163e77b8363SGerrit Renker /* Treat unsupported CCIDs like invalid values */ 1164e77b8363SGerrit Renker if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) { 1165e77b8363SGerrit Renker kfree(fval.sp.vec); 1166e77b8363SGerrit Renker goto not_valid_or_not_known; 1167e77b8363SGerrit Renker } 1168e77b8363SGerrit Renker 1169e77b8363SGerrit Renker return dccp_feat_push_confirm(fn, feat, local, &fval); 1170e77b8363SGerrit Renker 1171e77b8363SGerrit Renker } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */ 1172e77b8363SGerrit Renker return 0; 1173e77b8363SGerrit Renker } 1174e77b8363SGerrit Renker 1175e77b8363SGerrit Renker if (dccp_feat_reconcile(&entry->val, val, len, server, true)) { 11763db1cd5cSRusty Russell entry->empty_confirm = false; 1177e77b8363SGerrit Renker } else if (is_mandatory) { 1178e77b8363SGerrit Renker return DCCP_RESET_CODE_MANDATORY_ERROR; 1179e77b8363SGerrit Renker } else if (entry->state == FEAT_INITIALISING) { 1180e77b8363SGerrit Renker /* 1181e77b8363SGerrit Renker * Failed simultaneous negotiation (server only): try to `save' 1182e77b8363SGerrit Renker * the connection by checking whether entry contains the default 1183e77b8363SGerrit Renker * value for @feat. If yes, send an empty Confirm to signal that 1184e77b8363SGerrit Renker * the received Change was not understood - which implies using 1185e77b8363SGerrit Renker * the default value. 1186e77b8363SGerrit Renker * If this also fails, we use Reset as the last resort. 1187e77b8363SGerrit Renker */ 1188e77b8363SGerrit Renker WARN_ON(!server); 1189e77b8363SGerrit Renker defval = dccp_feat_default_value(feat); 1190e77b8363SGerrit Renker if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true)) 1191e77b8363SGerrit Renker return DCCP_RESET_CODE_OPTION_ERROR; 11923db1cd5cSRusty Russell entry->empty_confirm = true; 1193e77b8363SGerrit Renker } 11943db1cd5cSRusty Russell entry->needs_confirm = true; 11953db1cd5cSRusty Russell entry->needs_mandatory = false; 1196e77b8363SGerrit Renker entry->state = FEAT_STABLE; 1197e77b8363SGerrit Renker return 0; 1198e77b8363SGerrit Renker 1199e77b8363SGerrit Renker unknown_feature_or_value: 1200e77b8363SGerrit Renker if (!is_mandatory) 1201e77b8363SGerrit Renker return dccp_push_empty_confirm(fn, feat, local); 1202e77b8363SGerrit Renker 1203e77b8363SGerrit Renker not_valid_or_not_known: 1204e77b8363SGerrit Renker return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR 1205e77b8363SGerrit Renker : DCCP_RESET_CODE_OPTION_ERROR; 1206e77b8363SGerrit Renker } 1207e77b8363SGerrit Renker 1208e77b8363SGerrit Renker /** 1209b1ad0042SGerrit Renker * dccp_feat_confirm_recv - Process received Confirm options 1210b1ad0042SGerrit Renker * @fn: feature-negotiation list to update 1211b1ad0042SGerrit Renker * @is_mandatory: whether @opt was preceded by a Mandatory option 1212b1ad0042SGerrit Renker * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R 1213b1ad0042SGerrit Renker * @feat: one of %dccp_feature_numbers 1214b1ad0042SGerrit Renker * @val: NN value or SP value/preference list 1215b1ad0042SGerrit Renker * @len: length of @val in bytes 1216b1ad0042SGerrit Renker * @server: whether this node is server (1) or client (0) 1217b1ad0042SGerrit Renker */ 1218b1ad0042SGerrit Renker static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt, 1219b1ad0042SGerrit Renker u8 feat, u8 *val, u8 len, const bool server) 1220b1ad0042SGerrit Renker { 1221b1ad0042SGerrit Renker u8 *plist, plen, type = dccp_feat_type(feat); 1222b1ad0042SGerrit Renker const bool local = (opt == DCCPO_CONFIRM_R); 1223b1ad0042SGerrit Renker struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local); 1224b1ad0042SGerrit Renker 1225f3f3abb6SGerrit Renker dccp_feat_print_opt(opt, feat, val, len, is_mandatory); 1226f3f3abb6SGerrit Renker 1227b1ad0042SGerrit Renker if (entry == NULL) { /* nothing queued: ignore or handle error */ 1228b1ad0042SGerrit Renker if (is_mandatory && type == FEAT_UNKNOWN) 1229b1ad0042SGerrit Renker return DCCP_RESET_CODE_MANDATORY_ERROR; 1230b1ad0042SGerrit Renker 1231b1ad0042SGerrit Renker if (!local && type == FEAT_NN) /* 6.3.2 */ 1232b1ad0042SGerrit Renker goto confirmation_failed; 1233b1ad0042SGerrit Renker return 0; 1234b1ad0042SGerrit Renker } 1235b1ad0042SGerrit Renker 1236b1ad0042SGerrit Renker if (entry->state != FEAT_CHANGING) /* 6.6.2 */ 1237b1ad0042SGerrit Renker return 0; 1238b1ad0042SGerrit Renker 1239b1ad0042SGerrit Renker if (len == 0) { 1240b1ad0042SGerrit Renker if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */ 1241b1ad0042SGerrit Renker goto confirmation_failed; 1242b1ad0042SGerrit Renker /* 1243b1ad0042SGerrit Renker * Empty Confirm during connection setup: this means reverting 1244b1ad0042SGerrit Renker * to the `old' value, which in this case is the default. Since 1245b1ad0042SGerrit Renker * we handle default values automatically when no other values 1246b1ad0042SGerrit Renker * have been set, we revert to the old value by removing this 1247b1ad0042SGerrit Renker * entry from the list. 1248b1ad0042SGerrit Renker */ 1249b1ad0042SGerrit Renker dccp_feat_list_pop(entry); 1250b1ad0042SGerrit Renker return 0; 1251b1ad0042SGerrit Renker } 1252b1ad0042SGerrit Renker 1253b1ad0042SGerrit Renker if (type == FEAT_NN) { 1254b1ad0042SGerrit Renker if (len > sizeof(entry->val.nn)) 1255b1ad0042SGerrit Renker goto confirmation_failed; 1256b1ad0042SGerrit Renker 1257b1ad0042SGerrit Renker if (entry->val.nn == dccp_decode_value_var(val, len)) 1258b1ad0042SGerrit Renker goto confirmation_succeeded; 1259b1ad0042SGerrit Renker 1260b1ad0042SGerrit Renker DCCP_WARN("Bogus Confirm for non-existing value\n"); 1261b1ad0042SGerrit Renker goto confirmation_failed; 1262b1ad0042SGerrit Renker } 1263b1ad0042SGerrit Renker 1264b1ad0042SGerrit Renker /* 1265b1ad0042SGerrit Renker * Parsing SP Confirms: the first element of @val is the preferred 1266b1ad0042SGerrit Renker * SP value which the peer confirms, the remainder depends on @len. 1267b1ad0042SGerrit Renker * Note that only the confirmed value need to be a valid SP value. 1268b1ad0042SGerrit Renker */ 1269b1ad0042SGerrit Renker if (!dccp_feat_is_valid_sp_val(feat, *val)) 1270b1ad0042SGerrit Renker goto confirmation_failed; 1271b1ad0042SGerrit Renker 1272b1ad0042SGerrit Renker if (len == 1) { /* peer didn't supply a preference list */ 1273b1ad0042SGerrit Renker plist = val; 1274b1ad0042SGerrit Renker plen = len; 1275b1ad0042SGerrit Renker } else { /* preferred value + preference list */ 1276b1ad0042SGerrit Renker plist = val + 1; 1277b1ad0042SGerrit Renker plen = len - 1; 1278b1ad0042SGerrit Renker } 1279b1ad0042SGerrit Renker 1280b1ad0042SGerrit Renker /* Check whether the peer got the reconciliation right (6.6.8) */ 1281b1ad0042SGerrit Renker if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) { 1282b1ad0042SGerrit Renker DCCP_WARN("Confirm selected the wrong value %u\n", *val); 1283b1ad0042SGerrit Renker return DCCP_RESET_CODE_OPTION_ERROR; 1284b1ad0042SGerrit Renker } 1285b1ad0042SGerrit Renker entry->val.sp.vec[0] = *val; 1286b1ad0042SGerrit Renker 1287b1ad0042SGerrit Renker confirmation_succeeded: 1288b1ad0042SGerrit Renker entry->state = FEAT_STABLE; 1289b1ad0042SGerrit Renker return 0; 1290b1ad0042SGerrit Renker 1291b1ad0042SGerrit Renker confirmation_failed: 1292b1ad0042SGerrit Renker DCCP_WARN("Confirmation failed\n"); 1293b1ad0042SGerrit Renker return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR 1294b1ad0042SGerrit Renker : DCCP_RESET_CODE_OPTION_ERROR; 1295b1ad0042SGerrit Renker } 1296b1ad0042SGerrit Renker 1297b1ad0042SGerrit Renker /** 129844e6fd9eSGerrit Renker * dccp_feat_handle_nn_established - Fast-path reception of NN options 129944e6fd9eSGerrit Renker * @sk: socket of an established DCCP connection 130044e6fd9eSGerrit Renker * @mandatory: whether @opt was preceded by a Mandatory option 130144e6fd9eSGerrit Renker * @opt: %DCCPO_CHANGE_L | %DCCPO_CONFIRM_R (NN only) 130244e6fd9eSGerrit Renker * @feat: NN number, one of %dccp_feature_numbers 130344e6fd9eSGerrit Renker * @val: NN value 130444e6fd9eSGerrit Renker * @len: length of @val in bytes 13052c53040fSBen Hutchings * 130644e6fd9eSGerrit Renker * This function combines the functionality of change_recv/confirm_recv, with 130744e6fd9eSGerrit Renker * the following differences (reset codes are the same): 130844e6fd9eSGerrit Renker * - cleanup after receiving the Confirm; 130944e6fd9eSGerrit Renker * - values are directly activated after successful parsing; 131044e6fd9eSGerrit Renker * - deliberately restricted to NN features. 131144e6fd9eSGerrit Renker * The restriction to NN features is essential since SP features can have non- 131244e6fd9eSGerrit Renker * predictable outcomes (depending on the remote configuration), and are inter- 131344e6fd9eSGerrit Renker * dependent (CCIDs for instance cause further dependencies). 131444e6fd9eSGerrit Renker */ 131544e6fd9eSGerrit Renker static u8 dccp_feat_handle_nn_established(struct sock *sk, u8 mandatory, u8 opt, 131644e6fd9eSGerrit Renker u8 feat, u8 *val, u8 len) 131744e6fd9eSGerrit Renker { 131844e6fd9eSGerrit Renker struct list_head *fn = &dccp_sk(sk)->dccps_featneg; 131944e6fd9eSGerrit Renker const bool local = (opt == DCCPO_CONFIRM_R); 132044e6fd9eSGerrit Renker struct dccp_feat_entry *entry; 132144e6fd9eSGerrit Renker u8 type = dccp_feat_type(feat); 132244e6fd9eSGerrit Renker dccp_feat_val fval; 132344e6fd9eSGerrit Renker 132444e6fd9eSGerrit Renker dccp_feat_print_opt(opt, feat, val, len, mandatory); 132544e6fd9eSGerrit Renker 132644e6fd9eSGerrit Renker /* Ignore non-mandatory unknown and non-NN features */ 132744e6fd9eSGerrit Renker if (type == FEAT_UNKNOWN) { 132844e6fd9eSGerrit Renker if (local && !mandatory) 132944e6fd9eSGerrit Renker return 0; 133044e6fd9eSGerrit Renker goto fast_path_unknown; 133144e6fd9eSGerrit Renker } else if (type != FEAT_NN) { 133244e6fd9eSGerrit Renker return 0; 133344e6fd9eSGerrit Renker } 133444e6fd9eSGerrit Renker 133544e6fd9eSGerrit Renker /* 133644e6fd9eSGerrit Renker * We don't accept empty Confirms, since in fast-path feature 133744e6fd9eSGerrit Renker * negotiation the values are enabled immediately after sending 133844e6fd9eSGerrit Renker * the Change option. 133944e6fd9eSGerrit Renker * Empty Changes on the other hand are invalid (RFC 4340, 6.1). 134044e6fd9eSGerrit Renker */ 134144e6fd9eSGerrit Renker if (len == 0 || len > sizeof(fval.nn)) 134244e6fd9eSGerrit Renker goto fast_path_unknown; 134344e6fd9eSGerrit Renker 134444e6fd9eSGerrit Renker if (opt == DCCPO_CHANGE_L) { 134544e6fd9eSGerrit Renker fval.nn = dccp_decode_value_var(val, len); 134644e6fd9eSGerrit Renker if (!dccp_feat_is_valid_nn_val(feat, fval.nn)) 134744e6fd9eSGerrit Renker goto fast_path_unknown; 134844e6fd9eSGerrit Renker 134944e6fd9eSGerrit Renker if (dccp_feat_push_confirm(fn, feat, local, &fval) || 135044e6fd9eSGerrit Renker dccp_feat_activate(sk, feat, local, &fval)) 135144e6fd9eSGerrit Renker return DCCP_RESET_CODE_TOO_BUSY; 135244e6fd9eSGerrit Renker 135344e6fd9eSGerrit Renker /* set the `Ack Pending' flag to piggyback a Confirm */ 135444e6fd9eSGerrit Renker inet_csk_schedule_ack(sk); 135544e6fd9eSGerrit Renker 135644e6fd9eSGerrit Renker } else if (opt == DCCPO_CONFIRM_R) { 135744e6fd9eSGerrit Renker entry = dccp_feat_list_lookup(fn, feat, local); 135844e6fd9eSGerrit Renker if (entry == NULL || entry->state != FEAT_CHANGING) 135944e6fd9eSGerrit Renker return 0; 136044e6fd9eSGerrit Renker 136144e6fd9eSGerrit Renker fval.nn = dccp_decode_value_var(val, len); 136244e6fd9eSGerrit Renker /* 136344e6fd9eSGerrit Renker * Just ignore a value that doesn't match our current value. 136444e6fd9eSGerrit Renker * If the option changes twice within two RTTs, then at least 136544e6fd9eSGerrit Renker * one CONFIRM will be received for the old value after a 136644e6fd9eSGerrit Renker * new CHANGE was sent. 136744e6fd9eSGerrit Renker */ 136844e6fd9eSGerrit Renker if (fval.nn != entry->val.nn) 136944e6fd9eSGerrit Renker return 0; 137044e6fd9eSGerrit Renker 137144e6fd9eSGerrit Renker /* Only activate after receiving the Confirm option (6.6.1). */ 137244e6fd9eSGerrit Renker dccp_feat_activate(sk, feat, local, &fval); 137344e6fd9eSGerrit Renker 137444e6fd9eSGerrit Renker /* It has been confirmed - so remove the entry */ 137544e6fd9eSGerrit Renker dccp_feat_list_pop(entry); 137644e6fd9eSGerrit Renker 137744e6fd9eSGerrit Renker } else { 137844e6fd9eSGerrit Renker DCCP_WARN("Received illegal option %u\n", opt); 137944e6fd9eSGerrit Renker goto fast_path_failed; 138044e6fd9eSGerrit Renker } 138144e6fd9eSGerrit Renker return 0; 138244e6fd9eSGerrit Renker 138344e6fd9eSGerrit Renker fast_path_unknown: 138444e6fd9eSGerrit Renker if (!mandatory) 138544e6fd9eSGerrit Renker return dccp_push_empty_confirm(fn, feat, local); 138644e6fd9eSGerrit Renker 138744e6fd9eSGerrit Renker fast_path_failed: 138844e6fd9eSGerrit Renker return mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR 138944e6fd9eSGerrit Renker : DCCP_RESET_CODE_OPTION_ERROR; 139044e6fd9eSGerrit Renker } 139144e6fd9eSGerrit Renker 139244e6fd9eSGerrit Renker /** 1393e77b8363SGerrit Renker * dccp_feat_parse_options - Process Feature-Negotiation Options 1394e77b8363SGerrit Renker * @sk: for general use and used by the client during connection setup 1395e77b8363SGerrit Renker * @dreq: used by the server during connection setup 1396e77b8363SGerrit Renker * @mandatory: whether @opt was preceded by a Mandatory option 1397e77b8363SGerrit Renker * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R 1398e77b8363SGerrit Renker * @feat: one of %dccp_feature_numbers 1399e77b8363SGerrit Renker * @val: value contents of @opt 1400e77b8363SGerrit Renker * @len: length of @val in bytes 14012c53040fSBen Hutchings * 1402e77b8363SGerrit Renker * Returns 0 on success, a Reset code for ending the connection otherwise. 1403e77b8363SGerrit Renker */ 1404e77b8363SGerrit Renker int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq, 1405e77b8363SGerrit Renker u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len) 1406e77b8363SGerrit Renker { 1407e77b8363SGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 1408e77b8363SGerrit Renker struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg; 1409e77b8363SGerrit Renker bool server = false; 1410e77b8363SGerrit Renker 1411e77b8363SGerrit Renker switch (sk->sk_state) { 1412e77b8363SGerrit Renker /* 1413e77b8363SGerrit Renker * Negotiation during connection setup 1414e77b8363SGerrit Renker */ 1415e77b8363SGerrit Renker case DCCP_LISTEN: 1416df561f66SGustavo A. R. Silva server = true; 1417df561f66SGustavo A. R. Silva fallthrough; 1418e77b8363SGerrit Renker case DCCP_REQUESTING: 1419e77b8363SGerrit Renker switch (opt) { 1420e77b8363SGerrit Renker case DCCPO_CHANGE_L: 1421e77b8363SGerrit Renker case DCCPO_CHANGE_R: 1422e77b8363SGerrit Renker return dccp_feat_change_recv(fn, mandatory, opt, feat, 1423e77b8363SGerrit Renker val, len, server); 1424b1ad0042SGerrit Renker case DCCPO_CONFIRM_R: 1425b1ad0042SGerrit Renker case DCCPO_CONFIRM_L: 1426b1ad0042SGerrit Renker return dccp_feat_confirm_recv(fn, mandatory, opt, feat, 1427b1ad0042SGerrit Renker val, len, server); 1428e77b8363SGerrit Renker } 142944e6fd9eSGerrit Renker break; 143044e6fd9eSGerrit Renker /* 143144e6fd9eSGerrit Renker * Support for exchanging NN options on an established connection. 143244e6fd9eSGerrit Renker */ 143344e6fd9eSGerrit Renker case DCCP_OPEN: 143444e6fd9eSGerrit Renker case DCCP_PARTOPEN: 143544e6fd9eSGerrit Renker return dccp_feat_handle_nn_established(sk, mandatory, opt, feat, 143644e6fd9eSGerrit Renker val, len); 1437e77b8363SGerrit Renker } 1438e77b8363SGerrit Renker return 0; /* ignore FN options in all other states */ 1439e77b8363SGerrit Renker } 1440e77b8363SGerrit Renker 1441f90f92eeSGerrit Renker /** 1442f90f92eeSGerrit Renker * dccp_feat_init - Seed feature negotiation with host-specific defaults 1443d0b1101bSAndrew Lunn * @sk: Socket to initialize. 1444d0b1101bSAndrew Lunn * 1445f90f92eeSGerrit Renker * This initialises global defaults, depending on the value of the sysctls. 1446f90f92eeSGerrit Renker * These can later be overridden by registering changes via setsockopt calls. 1447f90f92eeSGerrit Renker * The last link in the chain is finalise_settings, to make sure that between 1448f90f92eeSGerrit Renker * here and the start of actual feature negotiation no inconsistencies enter. 1449f90f92eeSGerrit Renker * 1450f90f92eeSGerrit Renker * All features not appearing below use either defaults or are otherwise 1451f90f92eeSGerrit Renker * later adjusted through dccp_feat_finalise_settings(). 1452f90f92eeSGerrit Renker */ 1453e8ef967aSGerrit Renker int dccp_feat_init(struct sock *sk) 1454afe00251SAndrea Bittau { 1455f90f92eeSGerrit Renker struct list_head *fn = &dccp_sk(sk)->dccps_featneg; 1456f90f92eeSGerrit Renker u8 on = 1, off = 0; 1457afe00251SAndrea Bittau int rc; 1458f90f92eeSGerrit Renker struct { 1459f90f92eeSGerrit Renker u8 *val; 1460f90f92eeSGerrit Renker u8 len; 1461f90f92eeSGerrit Renker } tx, rx; 1462afe00251SAndrea Bittau 1463f90f92eeSGerrit Renker /* Non-negotiable (NN) features */ 1464f90f92eeSGerrit Renker rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0, 1465883ca833SGerrit Renker sysctl_dccp_sequence_window); 1466f90f92eeSGerrit Renker if (rc) 1467f90f92eeSGerrit Renker return rc; 1468afe00251SAndrea Bittau 1469f90f92eeSGerrit Renker /* Server-priority (SP) features */ 1470f90f92eeSGerrit Renker 1471f90f92eeSGerrit Renker /* Advertise that short seqnos are not supported (7.6.1) */ 1472f90f92eeSGerrit Renker rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1); 1473f90f92eeSGerrit Renker if (rc) 1474f90f92eeSGerrit Renker return rc; 1475f90f92eeSGerrit Renker 1476f90f92eeSGerrit Renker /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */ 1477f90f92eeSGerrit Renker rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1); 1478f90f92eeSGerrit Renker if (rc) 1479f90f92eeSGerrit Renker return rc; 1480f90f92eeSGerrit Renker 1481f90f92eeSGerrit Renker /* 1482f90f92eeSGerrit Renker * We advertise the available list of CCIDs and reorder according to 1483f90f92eeSGerrit Renker * preferences, to avoid failure resulting from negotiating different 1484f90f92eeSGerrit Renker * singleton values (which always leads to failure). 1485f90f92eeSGerrit Renker * These settings can still (later) be overridden via sockopts. 1486f90f92eeSGerrit Renker */ 1487e90ce2fcSXin Long if (ccid_get_builtin_ccids(&tx.val, &tx.len)) 1488f90f92eeSGerrit Renker return -ENOBUFS; 1489e90ce2fcSXin Long if (ccid_get_builtin_ccids(&rx.val, &rx.len)) { 1490e90ce2fcSXin Long kfree(tx.val); 1491e90ce2fcSXin Long return -ENOBUFS; 1492e90ce2fcSXin Long } 1493f90f92eeSGerrit Renker 1494883ca833SGerrit Renker if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) || 1495883ca833SGerrit Renker !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len)) 1496f90f92eeSGerrit Renker goto free_ccid_lists; 1497f90f92eeSGerrit Renker 1498f90f92eeSGerrit Renker rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len); 1499f90f92eeSGerrit Renker if (rc) 1500f90f92eeSGerrit Renker goto free_ccid_lists; 1501f90f92eeSGerrit Renker 1502f90f92eeSGerrit Renker rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len); 1503f90f92eeSGerrit Renker 1504f90f92eeSGerrit Renker free_ccid_lists: 1505f90f92eeSGerrit Renker kfree(tx.val); 1506f90f92eeSGerrit Renker kfree(rx.val); 1507afe00251SAndrea Bittau return rc; 1508afe00251SAndrea Bittau } 1509afe00251SAndrea Bittau 1510422d9cdcSGerrit Renker int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list) 1511422d9cdcSGerrit Renker { 1512422d9cdcSGerrit Renker struct dccp_sock *dp = dccp_sk(sk); 1513422d9cdcSGerrit Renker struct dccp_feat_entry *cur, *next; 1514422d9cdcSGerrit Renker int idx; 1515422d9cdcSGerrit Renker dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = { 1516422d9cdcSGerrit Renker [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL } 1517422d9cdcSGerrit Renker }; 1518422d9cdcSGerrit Renker 1519422d9cdcSGerrit Renker list_for_each_entry(cur, fn_list, node) { 1520422d9cdcSGerrit Renker /* 1521422d9cdcSGerrit Renker * An empty Confirm means that either an unknown feature type 1522422d9cdcSGerrit Renker * or an invalid value was present. In the first case there is 1523422d9cdcSGerrit Renker * nothing to activate, in the other the default value is used. 1524422d9cdcSGerrit Renker */ 1525422d9cdcSGerrit Renker if (cur->empty_confirm) 1526422d9cdcSGerrit Renker continue; 1527422d9cdcSGerrit Renker 1528422d9cdcSGerrit Renker idx = dccp_feat_index(cur->feat_num); 1529422d9cdcSGerrit Renker if (idx < 0) { 1530422d9cdcSGerrit Renker DCCP_BUG("Unknown feature %u", cur->feat_num); 1531422d9cdcSGerrit Renker goto activation_failed; 1532422d9cdcSGerrit Renker } 1533422d9cdcSGerrit Renker if (cur->state != FEAT_STABLE) { 1534f3f3abb6SGerrit Renker DCCP_CRIT("Negotiation of %s %s failed in state %s", 1535422d9cdcSGerrit Renker cur->is_local ? "local" : "remote", 1536f3f3abb6SGerrit Renker dccp_feat_fname(cur->feat_num), 1537f3f3abb6SGerrit Renker dccp_feat_sname[cur->state]); 1538422d9cdcSGerrit Renker goto activation_failed; 1539422d9cdcSGerrit Renker } 1540422d9cdcSGerrit Renker fvals[idx][cur->is_local] = &cur->val; 1541422d9cdcSGerrit Renker } 1542422d9cdcSGerrit Renker 1543422d9cdcSGerrit Renker /* 1544422d9cdcSGerrit Renker * Activate in decreasing order of index, so that the CCIDs are always 1545422d9cdcSGerrit Renker * activated as the last feature. This avoids the case where a CCID 1546422d9cdcSGerrit Renker * relies on the initialisation of one or more features that it depends 1547422d9cdcSGerrit Renker * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features). 1548422d9cdcSGerrit Renker */ 1549422d9cdcSGerrit Renker for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;) 1550422d9cdcSGerrit Renker if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) || 1551422d9cdcSGerrit Renker __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) { 1552422d9cdcSGerrit Renker DCCP_CRIT("Could not activate %d", idx); 1553422d9cdcSGerrit Renker goto activation_failed; 1554422d9cdcSGerrit Renker } 1555422d9cdcSGerrit Renker 1556422d9cdcSGerrit Renker /* Clean up Change options which have been confirmed already */ 1557422d9cdcSGerrit Renker list_for_each_entry_safe(cur, next, fn_list, node) 1558422d9cdcSGerrit Renker if (!cur->needs_confirm) 1559422d9cdcSGerrit Renker dccp_feat_list_pop(cur); 1560422d9cdcSGerrit Renker 1561422d9cdcSGerrit Renker dccp_pr_debug("Activation OK\n"); 1562422d9cdcSGerrit Renker return 0; 1563422d9cdcSGerrit Renker 1564422d9cdcSGerrit Renker activation_failed: 1565422d9cdcSGerrit Renker /* 1566422d9cdcSGerrit Renker * We clean up everything that may have been allocated, since 1567422d9cdcSGerrit Renker * it is difficult to track at which stage negotiation failed. 1568422d9cdcSGerrit Renker * This is ok, since all allocation functions below are robust 1569422d9cdcSGerrit Renker * against NULL arguments. 1570422d9cdcSGerrit Renker */ 1571422d9cdcSGerrit Renker ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 1572422d9cdcSGerrit Renker ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 1573422d9cdcSGerrit Renker dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL; 1574422d9cdcSGerrit Renker dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 1575422d9cdcSGerrit Renker dp->dccps_hc_rx_ackvec = NULL; 1576422d9cdcSGerrit Renker return -1; 1577422d9cdcSGerrit Renker } 1578