dccp: Resolve dependencies of features on choice of CCID
[safe/jmp/linux-2.6] / net / dccp / feat.c
index 2ec2cd1..4c41441 100644 (file)
@@ -6,6 +6,8 @@
  *
  *  ASSUMPTIONS
  *  -----------
+ *  o Feature negotiation is coordinated with connection setup (as in TCP), wild
+ *    changes of parameters of an established connection are not supported.
  *  o All currently known SP features have 1-byte quantities. If in the future
  *    extensions of RFCs 4340..42 define features with item lengths larger than
  *    one byte, a feature-specific extension of the code will be required.
@@ -93,8 +95,13 @@ static u8 dccp_feat_type(u8 feat_num)
 static int dccp_feat_default_value(u8 feat_num)
 {
        int idx = dccp_feat_index(feat_num);
+       /*
+        * There are no default values for unknown features, so encountering a
+        * negative index here indicates a serious problem somewhere else.
+        */
+       DCCP_BUG_ON(idx < 0);
 
-       return idx < 0 ? : dccp_feat_table[idx].default_value;
+       return idx < 0 ? : dccp_feat_table[idx].default_value;
 }
 
 /* copy constructor, fval must not already contain allocated memory */
@@ -159,18 +166,6 @@ static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
  * - SP values are always freshly allocated
  * - list is sorted in increasing order of feature number (faster lookup)
  */
-static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
-                                                    u8 feat_num, bool is_local)
-{
-       struct dccp_feat_entry *entry;
-
-       list_for_each_entry(entry, fn_list, node)
-               if (entry->feat_num == feat_num && entry->is_local == is_local)
-                       return entry;
-               else if (entry->feat_num > feat_num)
-                       break;
-       return NULL;
-}
 
 /**
  * dccp_feat_entry_new  -  Central list update routine (called by all others)
@@ -229,55 +224,133 @@ static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
        return 0;
 }
 
-/**
- * dccp_feat_push_confirm  -  Add a Confirm entry to the FN list
- * @fn_list: feature-negotiation list to add to
- * @feat: one of %dccp_feature_numbers
- * @local: whether local (1) or remote (0) @feat_num is being confirmed
- * @fval: pointer to NN/SP value to be inserted or NULL
- * Returns 0 on success, a Reset code for further processing otherwise.
- */
-static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
-                                 dccp_feat_val *fval)
+static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
 {
-       struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
+       list_del(&entry->node);
+       dccp_feat_entry_destructor(entry);
+}
 
-       if (new == NULL)
-               return DCCP_RESET_CODE_TOO_BUSY;
+void dccp_feat_list_purge(struct list_head *fn_list)
+{
+       struct dccp_feat_entry *entry, *next;
 
-       new->feat_num        = feat;
-       new->is_local        = local;
-       new->state           = FEAT_STABLE;     /* transition in 6.6.2 */
-       new->needs_confirm   = 1;
-       new->empty_confirm   = (fval == NULL);
-       new->val.nn          = 0;               /* zeroes the whole structure */
-       if (!new->empty_confirm)
-               new->val     = *fval;
-       new->needs_mandatory = 0;
+       list_for_each_entry_safe(entry, next, fn_list, node)
+               dccp_feat_entry_destructor(entry);
+       INIT_LIST_HEAD(fn_list);
+}
+EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
 
+/* generate @to as full clone of @from - @to must not contain any nodes */
+int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
+{
+       struct dccp_feat_entry *entry, *new;
+
+       INIT_LIST_HEAD(to);
+       list_for_each_entry(entry, from, node) {
+               new = dccp_feat_clone_entry(entry);
+               if (new == NULL)
+                       goto cloning_failed;
+               list_add_tail(&new->node, to);
+       }
        return 0;
+
+cloning_failed:
+       dccp_feat_list_purge(to);
+       return -ENOMEM;
 }
 
-static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
+static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
 {
-       return dccp_feat_push_confirm(fn_list, feat, local, NULL);
+       switch (feat_num) {
+       case DCCPF_ACK_RATIO:
+               return val <= DCCPF_ACK_RATIO_MAX;
+       case DCCPF_SEQUENCE_WINDOW:
+               return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
+       }
+       return 0;       /* feature unknown - so we can't tell */
 }
 
-static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
+/* check that SP values are within the ranges defined in RFC 4340 */
+static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
 {
-       list_del(&entry->node);
-       dccp_feat_entry_destructor(entry);
+       switch (feat_num) {
+       case DCCPF_CCID:
+               return val == DCCPC_CCID2 || val == DCCPC_CCID3;
+       /* Type-check Boolean feature values: */
+       case DCCPF_SHORT_SEQNOS:
+       case DCCPF_ECN_INCAPABLE:
+       case DCCPF_SEND_ACK_VECTOR:
+       case DCCPF_SEND_NDP_COUNT:
+       case DCCPF_DATA_CHECKSUM:
+       case DCCPF_SEND_LEV_RATE:
+               return val < 2;
+       case DCCPF_MIN_CSUM_COVER:
+               return val < 16;
+       }
+       return 0;                       /* feature unknown */
 }
 
-void dccp_feat_list_purge(struct list_head *fn_list)
+static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
 {
-       struct dccp_feat_entry *entry, *next;
+       if (sp_list == NULL || sp_len < 1)
+               return 0;
+       while (sp_len--)
+               if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
+                       return 0;
+       return 1;
+}
 
-       list_for_each_entry_safe(entry, next, fn_list, node)
-               dccp_feat_entry_destructor(entry);
-       INIT_LIST_HEAD(fn_list);
+/**
+ * __feat_register_nn  -  Register new NN value on socket
+ * @fn: feature-negotiation list to register with
+ * @feat: an NN feature from %dccp_feature_numbers
+ * @mandatory: use Mandatory option if 1
+ * @nn_val: value to register (restricted to 4 bytes)
+ * Note that NN features are local by definition (RFC 4340, 6.3.2).
+ */
+static int __feat_register_nn(struct list_head *fn, u8 feat,
+                             u8 mandatory, u64 nn_val)
+{
+       dccp_feat_val fval = { .nn = nn_val };
+
+       if (dccp_feat_type(feat) != FEAT_NN ||
+           !dccp_feat_is_valid_nn_val(feat, nn_val))
+               return -EINVAL;
+
+       /* Don't bother with default values, they will be activated anyway. */
+       if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
+               return 0;
+
+       return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
+}
+
+/**
+ * __feat_register_sp  -  Register new SP value/list on socket
+ * @fn: feature-negotiation list to register with
+ * @feat: an SP feature from %dccp_feature_numbers
+ * @is_local: whether the local (1) or the remote (0) @feat is meant
+ * @mandatory: use Mandatory option if 1
+ * @sp_val: SP value followed by optional preference list
+ * @sp_len: length of @sp_val in bytes
+ */
+static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
+                             u8 mandatory, u8 const *sp_val, u8 sp_len)
+{
+       dccp_feat_val fval;
+
+       if (dccp_feat_type(feat) != FEAT_SP ||
+           !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
+               return -EINVAL;
+
+       /* Avoid negotiating alien CCIDs by only advertising supported ones */
+       if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
+               return -EOPNOTSUPP;
+
+       if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
+               return -ENOMEM;
+
+       return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
 }
-EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
 
 int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
                     u8 *val, u8 len, gfp_t gfp)
@@ -327,6 +400,166 @@ int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
 
 EXPORT_SYMBOL_GPL(dccp_feat_change);
 
+/*
+ *     Tracking features whose value depend on the choice of CCID
+ *
+ * This is designed with an extension in mind so that a list walk could be done
+ * before activating any features. However, the existing framework was found to
+ * work satisfactorily up until now, the automatic verification is left open.
+ * When adding new CCIDs, add a corresponding dependency table here.
+ */
+static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
+{
+       static const struct ccid_dependency ccid2_dependencies[2][2] = {
+               /*
+                * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
+                * feature and Send Ack Vector is an RX feature, `is_local'
+                * needs to be reversed.
+                */
+               {       /* Dependencies of the receiver-side (remote) CCID2 */
+                       {
+                               .dependent_feat = DCCPF_SEND_ACK_VECTOR,
+                               .is_local       = true,
+                               .is_mandatory   = true,
+                               .val            = 1
+                       },
+                       { 0, 0, 0, 0 }
+               },
+               {       /* Dependencies of the sender-side (local) CCID2 */
+                       {
+                               .dependent_feat = DCCPF_SEND_ACK_VECTOR,
+                               .is_local       = false,
+                               .is_mandatory   = true,
+                               .val            = 1
+                       },
+                       { 0, 0, 0, 0 }
+               }
+       };
+       static const struct ccid_dependency ccid3_dependencies[2][5] = {
+               {       /*
+                        * Dependencies of the receiver-side CCID3
+                        */
+                       {       /* locally disable Ack Vectors */
+                               .dependent_feat = DCCPF_SEND_ACK_VECTOR,
+                               .is_local       = true,
+                               .is_mandatory   = false,
+                               .val            = 0
+                       },
+                       {       /* see below why Send Loss Event Rate is on */
+                               .dependent_feat = DCCPF_SEND_LEV_RATE,
+                               .is_local       = true,
+                               .is_mandatory   = true,
+                               .val            = 1
+                       },
+                       {       /* NDP Count is needed as per RFC 4342, 6.1.1 */
+                               .dependent_feat = DCCPF_SEND_NDP_COUNT,
+                               .is_local       = false,
+                               .is_mandatory   = true,
+                               .val            = 1
+                       },
+                       { 0, 0, 0, 0 },
+               },
+               {       /*
+                        * CCID3 at the TX side: we request that the HC-receiver
+                        * will not send Ack Vectors (they will be ignored, so
+                        * Mandatory is not set); we enable Send Loss Event Rate
+                        * (Mandatory since the implementation does not support
+                        * the Loss Intervals option of RFC 4342, 8.6).
+                        * The last two options are for peer's information only.
+                       */
+                       {
+                               .dependent_feat = DCCPF_SEND_ACK_VECTOR,
+                               .is_local       = false,
+                               .is_mandatory   = false,
+                               .val            = 0
+                       },
+                       {
+                               .dependent_feat = DCCPF_SEND_LEV_RATE,
+                               .is_local       = false,
+                               .is_mandatory   = true,
+                               .val            = 1
+                       },
+                       {       /* this CCID does not support Ack Ratio */
+                               .dependent_feat = DCCPF_ACK_RATIO,
+                               .is_local       = true,
+                               .is_mandatory   = false,
+                               .val            = 0
+                       },
+                       {       /* tell receiver we are sending NDP counts */
+                               .dependent_feat = DCCPF_SEND_NDP_COUNT,
+                               .is_local       = true,
+                               .is_mandatory   = false,
+                               .val            = 1
+                       },
+                       { 0, 0, 0, 0 }
+               }
+       };
+       switch (ccid) {
+       case DCCPC_CCID2:
+               return ccid2_dependencies[is_local];
+       case DCCPC_CCID3:
+               return ccid3_dependencies[is_local];
+       default:
+               return NULL;
+       }
+}
+
+/**
+ * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
+ * @fn: feature-negotiation list to update
+ * @id: CCID number to track
+ * @is_local: whether TX CCID (1) or RX CCID (0) is meant
+ * This function needs to be called after registering all other features.
+ */
+static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
+{
+       const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
+       int i, rc = (table == NULL);
+
+       for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
+               if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
+                       rc = __feat_register_sp(fn, table[i].dependent_feat,
+                                                   table[i].is_local,
+                                                   table[i].is_mandatory,
+                                                   &table[i].val, 1);
+               else
+                       rc = __feat_register_nn(fn, table[i].dependent_feat,
+                                                   table[i].is_mandatory,
+                                                   table[i].val);
+       return rc;
+}
+
+/**
+ * dccp_feat_finalise_settings  -  Finalise settings before starting negotiation
+ * @dp: client or listening socket (settings will be inherited)
+ * This is called after all registrations (socket initialisation, sysctls, and
+ * sockopt calls), and before sending the first packet containing Change options
+ * (ie. client-Request or server-Response), to ensure internal consistency.
+ */
+int dccp_feat_finalise_settings(struct dccp_sock *dp)
+{
+       struct list_head *fn = &dp->dccps_featneg;
+       struct dccp_feat_entry *entry;
+       int i = 2, ccids[2] = { -1, -1 };
+
+       /*
+        * Propagating CCIDs:
+        * 1) not useful to propagate CCID settings if this host advertises more
+        *    than one CCID: the choice of CCID  may still change - if this is
+        *    the client, or if this is the server and the client sends
+        *    singleton CCID values.
+        * 2) since is that propagate_ccid changes the list, we defer changing
+        *    the sorted list until after the traversal.
+        */
+       list_for_each_entry(entry, fn, node)
+               if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
+                       ccids[entry->is_local] = entry->val.sp.vec[0];
+       while (i--)
+               if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
+                       return -1;
+       return 0;
+}
+
 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
 {
        struct dccp_sock *dp = dccp_sk(sk);
@@ -633,6 +866,9 @@ int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
 {
        int rc;
 
+       /* Ignore Change requests other than during connection setup */
+       if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
+               return 0;
        dccp_feat_debug(type, feature, *val);
 
        /* figure out if it's SP or NN feature */
@@ -682,6 +918,9 @@ int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
        int found = 0;
        int all_confirmed = 1;
 
+       /* Ignore Confirm options other than during connection setup */
+       if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
+               return 0;
        dccp_feat_debug(type, feature, *val);
 
        /* locate our change request */
@@ -716,17 +955,6 @@ int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
                        all_confirmed = 0;
        }
 
-       /* fix re-transmit timer */
-       /* XXX gotta make sure that no option negotiation occurs during
-        * connection shutdown.  Consider that the CLOSEREQ is sent and timer is
-        * on.  if all options are confirmed it might kill timer which should
-        * remain alive until close is received.
-        */
-       if (all_confirmed) {
-               dccp_pr_debug("clear feat negotiation timer %p\n", sk);
-               inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
-       }
-
        if (!found)
                dccp_pr_debug("%s(%d, ...) never requested\n",
                              dccp_feat_typename(type), feature);
@@ -820,42 +1048,30 @@ out_clean:
 
 EXPORT_SYMBOL_GPL(dccp_feat_clone);
 
-static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
-                           u8 *val, u8 len)
-{
-       int rc = -ENOMEM;
-       u8 *copy = kmemdup(val, len, GFP_KERNEL);
-
-       if (copy != NULL) {
-               rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
-               if (rc)
-                       kfree(copy);
-       }
-       return rc;
-}
-
-int dccp_feat_init(struct dccp_minisock *dmsk)
+int dccp_feat_init(struct sock *sk)
 {
+       struct dccp_sock *dp = dccp_sk(sk);
+       struct dccp_minisock *dmsk = dccp_msk(sk);
        int rc;
 
-       INIT_LIST_HEAD(&dmsk->dccpms_pending);
-       INIT_LIST_HEAD(&dmsk->dccpms_conf);
+       INIT_LIST_HEAD(&dmsk->dccpms_pending);  /* XXX no longer used */
+       INIT_LIST_HEAD(&dmsk->dccpms_conf);     /* XXX no longer used */
 
        /* CCID L */
-       rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
-                             &dmsk->dccpms_tx_ccid, 1);
+       rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
+                               &dmsk->dccpms_tx_ccid, 1);
        if (rc)
                goto out;
 
        /* CCID R */
-       rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
-                             &dmsk->dccpms_rx_ccid, 1);
+       rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
+                               &dmsk->dccpms_rx_ccid, 1);
        if (rc)
                goto out;
 
        /* Ack ratio */
-       rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
-                             &dmsk->dccpms_ack_ratio, 1);
+       rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
+                               dmsk->dccpms_ack_ratio);
 out:
        return rc;
 }