sctp/ipv6.c: use ipv6_addr_copy
[safe/jmp/linux-2.6] / net / sctp / auth.c
index 2a29409..56935bb 100644 (file)
@@ -1,15 +1,15 @@
-/* SCTP kernel reference Implementation
+/* SCTP kernel implementation
  * (C) Copyright 2007 Hewlett-Packard Development Company, L.P.
  *
- * This file is part of the SCTP kernel reference Implementation
+ * This file is part of the SCTP kernel implementation
  *
- * The SCTP reference implementation is free software;
+ * This SCTP implementation is free software;
  * you can redistribute it and/or modify it under the terms of
  * the GNU General Public License as published by
  * the Free Software Foundation; either version 2, or (at your option)
  * any later version.
  *
- * The SCTP reference implementation is distributed in the hope that it
+ * This SCTP implementation is distributed in the hope that it
  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  *                 ************************
  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
@@ -54,11 +54,13 @@ static struct sctp_hmac sctp_hmac_list[SCTP_AUTH_NUM_HMACS] = {
                /* id 2 is reserved as well */
                .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_2,
        },
+#if defined (CONFIG_CRYPTO_SHA256) || defined (CONFIG_CRYPTO_SHA256_MODULE)
        {
                .hmac_id = SCTP_AUTH_HMAC_ID_SHA256,
                .hmac_name="hmac(sha256)",
                .hmac_len = SCTP_SHA256_SIG_SIZE,
        }
+#endif
 };
 
 
@@ -78,6 +80,10 @@ static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp)
 {
        struct sctp_auth_bytes *key;
 
+       /* Verify that we are not going to overflow INT_MAX */
+       if ((INT_MAX - key_len) < sizeof(struct sctp_auth_bytes))
+               return NULL;
+
        /* Allocate the shared key */
        key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp);
        if (!key)
@@ -107,7 +113,7 @@ struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp)
 }
 
 /* Free the shared key stucture */
-void sctp_auth_shkey_free(struct sctp_shared_key *sh_key)
+static void sctp_auth_shkey_free(struct sctp_shared_key *sh_key)
 {
        BUG_ON(!list_empty(&sh_key->key_list));
        sctp_auth_key_put(sh_key->key);
@@ -135,8 +141,8 @@ void sctp_auth_destroy_keys(struct list_head *keys)
 /* Compare two byte vectors as numbers.  Return values
  * are:
  *       0 - vectors are equal
- *     < 0 - vector 1 is smaller then vector2
- *     > 0 - vector 1 is greater then vector2
+ *     < 0 - vector 1 is smaller than vector2
+ *     > 0 - vector 1 is greater than vector2
  *
  * Algorithm is:
  *     This is performed by selecting the numerically smaller key vector...
@@ -220,7 +226,7 @@ static struct sctp_auth_bytes *sctp_auth_make_key_vector(
 
 
 /* Make a key vector based on our local parameters */
-struct sctp_auth_bytes *sctp_auth_make_local_vector(
+static struct sctp_auth_bytes *sctp_auth_make_local_vector(
                                    const struct sctp_association *asoc,
                                    gfp_t gfp)
 {
@@ -232,7 +238,7 @@ struct sctp_auth_bytes *sctp_auth_make_local_vector(
 }
 
 /* Make a key vector based on peer's parameters */
-struct sctp_auth_bytes *sctp_auth_make_peer_vector(
+static struct sctp_auth_bytes *sctp_auth_make_peer_vector(
                                    const struct sctp_association *asoc,
                                    gfp_t gfp)
 {
@@ -418,15 +424,15 @@ struct sctp_shared_key *sctp_auth_get_shkey(
                                const struct sctp_association *asoc,
                                __u16 key_id)
 {
-       struct sctp_shared_key *key = NULL;
+       struct sctp_shared_key *key;
 
        /* First search associations set of endpoint pair shared keys */
        key_for_each(key, &asoc->endpoint_shared_keys) {
                if (key->key_id == key_id)
-                       break;
+                       return key;
        }
 
-       return key;
+       return NULL;
 }
 
 /*
@@ -483,7 +489,7 @@ int sctp_auth_init_hmacs(struct sctp_endpoint *ep, gfp_t gfp)
        return 0;
 
 out_err:
-       /* Clean up any successfull allocations */
+       /* Clean up any successful allocations */
        sctp_auth_destroy_hmacs(ep->auth_hmacs);
        return -ENOMEM;
 }
@@ -556,7 +562,7 @@ struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc)
        return &sctp_hmac_list[id];
 }
 
-static int __sctp_auth_find_hmacid(__u16 *hmacs, int n_elts, __u16 hmac_id)
+static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id)
 {
        int  found = 0;
        int  i;
@@ -573,7 +579,7 @@ static int __sctp_auth_find_hmacid(__u16 *hmacs, int n_elts, __u16 hmac_id)
 
 /* See if the HMAC_ID is one that we claim as supported */
 int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc,
-                                   __u16 hmac_id)
+                                   __be16 hmac_id)
 {
        struct sctp_hmac_algo_param *hmacs;
        __u16 n_elt;
@@ -631,7 +637,7 @@ static int __sctp_auth_cid(sctp_cid_t chunk, struct sctp_chunks_param *param)
        int found = 0;
        int i;
 
-       if (!param)
+       if (!param || param->param_hdr.length == 0)
                return 0;
 
        len = ntohs(param->param_hdr.length) - sizeof(sctp_paramhdr_t);
@@ -726,9 +732,7 @@ void sctp_auth_calculate_hmac(const struct sctp_association *asoc,
 
        /* set up scatter list */
        end = skb_tail_pointer(skb);
-       sg.page = virt_to_page(auth);
-       sg.offset = (unsigned long)(auth) % PAGE_SIZE;
-       sg.length = end - (unsigned char *)auth;
+       sg_init_one(&sg, auth, end - (unsigned char *)auth);
 
        desc.tfm = asoc->ep->auth_hmacs[hmac_id];
        desc.flags = 0;
@@ -743,3 +747,199 @@ free:
        if (free_key)
                sctp_auth_key_put(asoc_key);
 }
+
+/* API Helpers */
+
+/* Add a chunk to the endpoint authenticated chunk list */
+int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id)
+{
+       struct sctp_chunks_param *p = ep->auth_chunk_list;
+       __u16 nchunks;
+       __u16 param_len;
+
+       /* If this chunk is already specified, we are done */
+       if (__sctp_auth_cid(chunk_id, p))
+               return 0;
+
+       /* Check if we can add this chunk to the array */
+       param_len = ntohs(p->param_hdr.length);
+       nchunks = param_len - sizeof(sctp_paramhdr_t);
+       if (nchunks == SCTP_NUM_CHUNK_TYPES)
+               return -EINVAL;
+
+       p->chunks[nchunks] = chunk_id;
+       p->param_hdr.length = htons(param_len + 1);
+       return 0;
+}
+
+/* Add hmac identifires to the endpoint list of supported hmac ids */
+int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep,
+                          struct sctp_hmacalgo *hmacs)
+{
+       int has_sha1 = 0;
+       __u16 id;
+       int i;
+
+       /* Scan the list looking for unsupported id.  Also make sure that
+        * SHA1 is specified.
+        */
+       for (i = 0; i < hmacs->shmac_num_idents; i++) {
+               id = hmacs->shmac_idents[i];
+
+               if (id > SCTP_AUTH_HMAC_ID_MAX)
+                       return -EOPNOTSUPP;
+
+               if (SCTP_AUTH_HMAC_ID_SHA1 == id)
+                       has_sha1 = 1;
+
+               if (!sctp_hmac_list[id].hmac_name)
+                       return -EOPNOTSUPP;
+       }
+
+       if (!has_sha1)
+               return -EINVAL;
+
+       memcpy(ep->auth_hmacs_list->hmac_ids, &hmacs->shmac_idents[0],
+               hmacs->shmac_num_idents * sizeof(__u16));
+       ep->auth_hmacs_list->param_hdr.length = htons(sizeof(sctp_paramhdr_t) +
+                               hmacs->shmac_num_idents * sizeof(__u16));
+       return 0;
+}
+
+/* Set a new shared key on either endpoint or association.  If the
+ * the key with a same ID already exists, replace the key (remove the
+ * old key and add a new one).
+ */
+int sctp_auth_set_key(struct sctp_endpoint *ep,
+                     struct sctp_association *asoc,
+                     struct sctp_authkey *auth_key)
+{
+       struct sctp_shared_key *cur_key = NULL;
+       struct sctp_auth_bytes *key;
+       struct list_head *sh_keys;
+       int replace = 0;
+
+       /* Try to find the given key id to see if
+        * we are doing a replace, or adding a new key
+        */
+       if (asoc)
+               sh_keys = &asoc->endpoint_shared_keys;
+       else
+               sh_keys = &ep->endpoint_shared_keys;
+
+       key_for_each(cur_key, sh_keys) {
+               if (cur_key->key_id == auth_key->sca_keynumber) {
+                       replace = 1;
+                       break;
+               }
+       }
+
+       /* If we are not replacing a key id, we need to allocate
+        * a shared key.
+        */
+       if (!replace) {
+               cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber,
+                                                GFP_KERNEL);
+               if (!cur_key)
+                       return -ENOMEM;
+       }
+
+       /* Create a new key data based on the info passed in */
+       key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL);
+       if (!key)
+               goto nomem;
+
+       memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength);
+
+       /* If we are replacing, remove the old keys data from the
+        * key id.  If we are adding new key id, add it to the
+        * list.
+        */
+       if (replace)
+               sctp_auth_key_put(cur_key->key);
+       else
+               list_add(&cur_key->key_list, sh_keys);
+
+       cur_key->key = key;
+       sctp_auth_key_hold(key);
+
+       return 0;
+nomem:
+       if (!replace)
+               sctp_auth_shkey_free(cur_key);
+
+       return -ENOMEM;
+}
+
+int sctp_auth_set_active_key(struct sctp_endpoint *ep,
+                            struct sctp_association *asoc,
+                            __u16  key_id)
+{
+       struct sctp_shared_key *key;
+       struct list_head *sh_keys;
+       int found = 0;
+
+       /* The key identifier MUST correst to an existing key */
+       if (asoc)
+               sh_keys = &asoc->endpoint_shared_keys;
+       else
+               sh_keys = &ep->endpoint_shared_keys;
+
+       key_for_each(key, sh_keys) {
+               if (key->key_id == key_id) {
+                       found = 1;
+                       break;
+               }
+       }
+
+       if (!found)
+               return -EINVAL;
+
+       if (asoc) {
+               asoc->active_key_id = key_id;
+               sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL);
+       } else
+               ep->active_key_id = key_id;
+
+       return 0;
+}
+
+int sctp_auth_del_key_id(struct sctp_endpoint *ep,
+                        struct sctp_association *asoc,
+                        __u16  key_id)
+{
+       struct sctp_shared_key *key;
+       struct list_head *sh_keys;
+       int found = 0;
+
+       /* The key identifier MUST NOT be the current active key
+        * The key identifier MUST correst to an existing key
+        */
+       if (asoc) {
+               if (asoc->active_key_id == key_id)
+                       return -EINVAL;
+
+               sh_keys = &asoc->endpoint_shared_keys;
+       } else {
+               if (ep->active_key_id == key_id)
+                       return -EINVAL;
+
+               sh_keys = &ep->endpoint_shared_keys;
+       }
+
+       key_for_each(key, sh_keys) {
+               if (key->key_id == key_id) {
+                       found = 1;
+                       break;
+               }
+       }
+
+       if (!found)
+               return -EINVAL;
+
+       /* Delete the shared key */
+       list_del_init(&key->key_list);
+       sctp_auth_shkey_free(key);
+
+       return 0;
+}