Merge tag 'drm-vc4-fixes-2016-09-14' of https://github.com/anholt/linux into drm...
[linux-2.6-block.git] / net / ipv6 / calipso.c
index 959db5268b3899e2b201cc6e82a8188cfce4c5d8..37ac9de713c69af30ae50d03e53ee472a7520b98 100644 (file)
  */
 #define CALIPSO_OPT_LEN_MAX_WITH_PAD (3 + CALIPSO_OPT_LEN_MAX + 7)
 
+ /* Maximium size of u32 aligned buffer required to hold calipso
+  * option.  Max of 3 initial pad bytes starting from buffer + 3.
+  * i.e. the worst case is when the previous tlv finishes on 4n + 3.
+  */
+#define CALIPSO_MAX_BUFFER (6 + CALIPSO_OPT_LEN_MAX)
 
 /* List of available DOI definitions */
 static DEFINE_SPINLOCK(calipso_doi_list_lock);
 static LIST_HEAD(calipso_doi_list);
 
+/* Label mapping cache */
+int calipso_cache_enabled = 1;
+int calipso_cache_bucketsize = 10;
+#define CALIPSO_CACHE_BUCKETBITS     7
+#define CALIPSO_CACHE_BUCKETS        BIT(CALIPSO_CACHE_BUCKETBITS)
+#define CALIPSO_CACHE_REORDERLIMIT   10
+struct calipso_map_cache_bkt {
+       spinlock_t lock;
+       u32 size;
+       struct list_head list;
+};
+
+struct calipso_map_cache_entry {
+       u32 hash;
+       unsigned char *key;
+       size_t key_len;
+
+       struct netlbl_lsm_cache *lsm_data;
+
+       u32 activity;
+       struct list_head list;
+};
+
+static struct calipso_map_cache_bkt *calipso_cache;
+
+/* Label Mapping Cache Functions
+ */
+
+/**
+ * calipso_cache_entry_free - Frees a cache entry
+ * @entry: the entry to free
+ *
+ * Description:
+ * This function frees the memory associated with a cache entry including the
+ * LSM cache data if there are no longer any users, i.e. reference count == 0.
+ *
+ */
+static void calipso_cache_entry_free(struct calipso_map_cache_entry *entry)
+{
+       if (entry->lsm_data)
+               netlbl_secattr_cache_free(entry->lsm_data);
+       kfree(entry->key);
+       kfree(entry);
+}
+
+/**
+ * calipso_map_cache_hash - Hashing function for the CALIPSO cache
+ * @key: the hash key
+ * @key_len: the length of the key in bytes
+ *
+ * Description:
+ * The CALIPSO tag hashing function.  Returns a 32-bit hash value.
+ *
+ */
+static u32 calipso_map_cache_hash(const unsigned char *key, u32 key_len)
+{
+       return jhash(key, key_len, 0);
+}
+
+/**
+ * calipso_cache_init - Initialize the CALIPSO cache
+ *
+ * Description:
+ * Initializes the CALIPSO label mapping cache, this function should be called
+ * before any of the other functions defined in this file.  Returns zero on
+ * success, negative values on error.
+ *
+ */
+static int __init calipso_cache_init(void)
+{
+       u32 iter;
+
+       calipso_cache = kcalloc(CALIPSO_CACHE_BUCKETS,
+                               sizeof(struct calipso_map_cache_bkt),
+                               GFP_KERNEL);
+       if (!calipso_cache)
+               return -ENOMEM;
+
+       for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
+               spin_lock_init(&calipso_cache[iter].lock);
+               calipso_cache[iter].size = 0;
+               INIT_LIST_HEAD(&calipso_cache[iter].list);
+       }
+
+       return 0;
+}
+
+/**
+ * calipso_cache_invalidate - Invalidates the current CALIPSO cache
+ *
+ * Description:
+ * Invalidates and frees any entries in the CALIPSO cache.  Returns zero on
+ * success and negative values on failure.
+ *
+ */
+static void calipso_cache_invalidate(void)
+{
+       struct calipso_map_cache_entry *entry, *tmp_entry;
+       u32 iter;
+
+       for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
+               spin_lock_bh(&calipso_cache[iter].lock);
+               list_for_each_entry_safe(entry,
+                                        tmp_entry,
+                                        &calipso_cache[iter].list, list) {
+                       list_del(&entry->list);
+                       calipso_cache_entry_free(entry);
+               }
+               calipso_cache[iter].size = 0;
+               spin_unlock_bh(&calipso_cache[iter].lock);
+       }
+}
+
+/**
+ * calipso_cache_check - Check the CALIPSO cache for a label mapping
+ * @key: the buffer to check
+ * @key_len: buffer length in bytes
+ * @secattr: the security attribute struct to use
+ *
+ * Description:
+ * This function checks the cache to see if a label mapping already exists for
+ * the given key.  If there is a match then the cache is adjusted and the
+ * @secattr struct is populated with the correct LSM security attributes.  The
+ * cache is adjusted in the following manner if the entry is not already the
+ * first in the cache bucket:
+ *
+ *  1. The cache entry's activity counter is incremented
+ *  2. The previous (higher ranking) entry's activity counter is decremented
+ *  3. If the difference between the two activity counters is geater than
+ *     CALIPSO_CACHE_REORDERLIMIT the two entries are swapped
+ *
+ * Returns zero on success, -ENOENT for a cache miss, and other negative values
+ * on error.
+ *
+ */
+static int calipso_cache_check(const unsigned char *key,
+                              u32 key_len,
+                              struct netlbl_lsm_secattr *secattr)
+{
+       u32 bkt;
+       struct calipso_map_cache_entry *entry;
+       struct calipso_map_cache_entry *prev_entry = NULL;
+       u32 hash;
+
+       if (!calipso_cache_enabled)
+               return -ENOENT;
+
+       hash = calipso_map_cache_hash(key, key_len);
+       bkt = hash & (CALIPSO_CACHE_BUCKETS - 1);
+       spin_lock_bh(&calipso_cache[bkt].lock);
+       list_for_each_entry(entry, &calipso_cache[bkt].list, list) {
+               if (entry->hash == hash &&
+                   entry->key_len == key_len &&
+                   memcmp(entry->key, key, key_len) == 0) {
+                       entry->activity += 1;
+                       atomic_inc(&entry->lsm_data->refcount);
+                       secattr->cache = entry->lsm_data;
+                       secattr->flags |= NETLBL_SECATTR_CACHE;
+                       secattr->type = NETLBL_NLTYPE_CALIPSO;
+                       if (!prev_entry) {
+                               spin_unlock_bh(&calipso_cache[bkt].lock);
+                               return 0;
+                       }
+
+                       if (prev_entry->activity > 0)
+                               prev_entry->activity -= 1;
+                       if (entry->activity > prev_entry->activity &&
+                           entry->activity - prev_entry->activity >
+                           CALIPSO_CACHE_REORDERLIMIT) {
+                               __list_del(entry->list.prev, entry->list.next);
+                               __list_add(&entry->list,
+                                          prev_entry->list.prev,
+                                          &prev_entry->list);
+                       }
+
+                       spin_unlock_bh(&calipso_cache[bkt].lock);
+                       return 0;
+               }
+               prev_entry = entry;
+       }
+       spin_unlock_bh(&calipso_cache[bkt].lock);
+
+       return -ENOENT;
+}
+
+/**
+ * calipso_cache_add - Add an entry to the CALIPSO cache
+ * @calipso_ptr: the CALIPSO option
+ * @secattr: the packet's security attributes
+ *
+ * Description:
+ * Add a new entry into the CALIPSO label mapping cache.  Add the new entry to
+ * head of the cache bucket's list, if the cache bucket is out of room remove
+ * the last entry in the list first.  It is important to note that there is
+ * currently no checking for duplicate keys.  Returns zero on success,
+ * negative values on failure.  The key stored starts at calipso_ptr + 2,
+ * i.e. the type and length bytes are not stored, this corresponds to
+ * calipso_ptr[1] bytes of data.
+ *
+ */
+static int calipso_cache_add(const unsigned char *calipso_ptr,
+                            const struct netlbl_lsm_secattr *secattr)
+{
+       int ret_val = -EPERM;
+       u32 bkt;
+       struct calipso_map_cache_entry *entry = NULL;
+       struct calipso_map_cache_entry *old_entry = NULL;
+       u32 calipso_ptr_len;
+
+       if (!calipso_cache_enabled || calipso_cache_bucketsize <= 0)
+               return 0;
+
+       calipso_ptr_len = calipso_ptr[1];
+
+       entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
+       if (!entry)
+               return -ENOMEM;
+       entry->key = kmemdup(calipso_ptr + 2, calipso_ptr_len, GFP_ATOMIC);
+       if (!entry->key) {
+               ret_val = -ENOMEM;
+               goto cache_add_failure;
+       }
+       entry->key_len = calipso_ptr_len;
+       entry->hash = calipso_map_cache_hash(calipso_ptr, calipso_ptr_len);
+       atomic_inc(&secattr->cache->refcount);
+       entry->lsm_data = secattr->cache;
+
+       bkt = entry->hash & (CALIPSO_CACHE_BUCKETS - 1);
+       spin_lock_bh(&calipso_cache[bkt].lock);
+       if (calipso_cache[bkt].size < calipso_cache_bucketsize) {
+               list_add(&entry->list, &calipso_cache[bkt].list);
+               calipso_cache[bkt].size += 1;
+       } else {
+               old_entry = list_entry(calipso_cache[bkt].list.prev,
+                                      struct calipso_map_cache_entry, list);
+               list_del(&old_entry->list);
+               list_add(&entry->list, &calipso_cache[bkt].list);
+               calipso_cache_entry_free(old_entry);
+       }
+       spin_unlock_bh(&calipso_cache[bkt].lock);
+
+       return 0;
+
+cache_add_failure:
+       if (entry)
+               calipso_cache_entry_free(entry);
+       return ret_val;
+}
+
 /* DOI List Functions
  */
 
@@ -315,6 +569,47 @@ doi_walk_return:
        return ret_val;
 }
 
+/**
+ * calipso_validate - Validate a CALIPSO option
+ * @skb: the packet
+ * @option: the start of the option
+ *
+ * Description:
+ * This routine is called to validate a CALIPSO option.
+ * If the option is valid then %true is returned, otherwise
+ * %false is returned.
+ *
+ * The caller should have already checked that the length of the
+ * option (including the TLV header) is >= 10 and that the catmap
+ * length is consistent with the option length.
+ *
+ * We leave checks on the level and categories to the socket layer.
+ */
+bool calipso_validate(const struct sk_buff *skb, const unsigned char *option)
+{
+       struct calipso_doi *doi_def;
+       bool ret_val;
+       u16 crc, len = option[1] + 2;
+       static const u8 zero[2];
+
+       /* The original CRC runs over the option including the TLV header
+        * with the CRC-16 field (at offset 8) zeroed out. */
+       crc = crc_ccitt(0xffff, option, 8);
+       crc = crc_ccitt(crc, zero, sizeof(zero));
+       if (len > 10)
+               crc = crc_ccitt(crc, option + 10, len - 10);
+       crc = ~crc;
+       if (option[8] != (crc & 0xff) || option[9] != ((crc >> 8) & 0xff))
+               return false;
+
+       rcu_read_lock();
+       doi_def = calipso_doi_search(get_unaligned_be32(option + 2));
+       ret_val = !!doi_def;
+       rcu_read_unlock();
+
+       return ret_val;
+}
+
 /**
  * calipso_map_cat_hton - Perform a category mapping from host to network
  * @doi_def: the DOI definition
@@ -657,8 +952,10 @@ calipso_opt_insert(struct ipv6_opt_hdr *hop,
                memcpy(new, hop, start);
        ret_val = calipso_genopt((unsigned char *)new, start, buf_len, doi_def,
                                 secattr);
-       if (ret_val < 0)
+       if (ret_val < 0) {
+               kfree(new);
                return ERR_PTR(ret_val);
+       }
 
        buf_len = start + ret_val;
        /* At this point buf_len aligns to 4n, so (buf_len & 4) pads to 8n */
@@ -743,6 +1040,9 @@ static int calipso_opt_getattr(const unsigned char *calipso,
        if (cat_len + 8 > len)
                return -EINVAL;
 
+       if (calipso_cache_check(calipso + 2, calipso[1], secattr) == 0)
+               return 0;
+
        doi = get_unaligned_be32(calipso + 2);
        rcu_read_lock();
        doi_def = calipso_doi_search(doi);
@@ -889,6 +1189,246 @@ done:
        txopt_put(txopts);
 }
 
+/* request sock functions.
+ */
+
+/**
+ * calipso_req_setattr - Add a CALIPSO option to a connection request socket
+ * @req: the connection request socket
+ * @doi_def: the CALIPSO DOI to use
+ * @secattr: the specific security attributes of the socket
+ *
+ * Description:
+ * Set the CALIPSO option on the given socket using the DOI definition and
+ * security attributes passed to the function.  Returns zero on success and
+ * negative values on failure.
+ *
+ */
+static int calipso_req_setattr(struct request_sock *req,
+                              const struct calipso_doi *doi_def,
+                              const struct netlbl_lsm_secattr *secattr)
+{
+       struct ipv6_txoptions *txopts;
+       struct inet_request_sock *req_inet = inet_rsk(req);
+       struct ipv6_opt_hdr *old, *new;
+       struct sock *sk = sk_to_full_sk(req_to_sk(req));
+
+       if (req_inet->ipv6_opt && req_inet->ipv6_opt->hopopt)
+               old = req_inet->ipv6_opt->hopopt;
+       else
+               old = NULL;
+
+       new = calipso_opt_insert(old, doi_def, secattr);
+       if (IS_ERR(new))
+               return PTR_ERR(new);
+
+       txopts = ipv6_renew_options_kern(sk, req_inet->ipv6_opt, IPV6_HOPOPTS,
+                                        new, new ? ipv6_optlen(new) : 0);
+
+       kfree(new);
+
+       if (IS_ERR(txopts))
+               return PTR_ERR(txopts);
+
+       txopts = xchg(&req_inet->ipv6_opt, txopts);
+       if (txopts) {
+               atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
+               txopt_put(txopts);
+       }
+
+       return 0;
+}
+
+/**
+ * calipso_req_delattr - Delete the CALIPSO option from a request socket
+ * @reg: the request socket
+ *
+ * Description:
+ * Removes the CALIPSO option from a request socket, if present.
+ *
+ */
+static void calipso_req_delattr(struct request_sock *req)
+{
+       struct inet_request_sock *req_inet = inet_rsk(req);
+       struct ipv6_opt_hdr *new;
+       struct ipv6_txoptions *txopts;
+       struct sock *sk = sk_to_full_sk(req_to_sk(req));
+
+       if (!req_inet->ipv6_opt || !req_inet->ipv6_opt->hopopt)
+               return;
+
+       if (calipso_opt_del(req_inet->ipv6_opt->hopopt, &new))
+               return; /* Nothing to do */
+
+       txopts = ipv6_renew_options_kern(sk, req_inet->ipv6_opt, IPV6_HOPOPTS,
+                                        new, new ? ipv6_optlen(new) : 0);
+
+       if (!IS_ERR(txopts)) {
+               txopts = xchg(&req_inet->ipv6_opt, txopts);
+               if (txopts) {
+                       atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
+                       txopt_put(txopts);
+               }
+       }
+       kfree(new);
+}
+
+/* skbuff functions.
+ */
+
+/**
+ * calipso_skbuff_optptr - Find the CALIPSO option in the packet
+ * @skb: the packet
+ *
+ * Description:
+ * Parse the packet's IP header looking for a CALIPSO option.  Returns a pointer
+ * to the start of the CALIPSO option on success, NULL if one if not found.
+ *
+ */
+static unsigned char *calipso_skbuff_optptr(const struct sk_buff *skb)
+{
+       const struct ipv6hdr *ip6_hdr = ipv6_hdr(skb);
+       int offset;
+
+       if (ip6_hdr->nexthdr != NEXTHDR_HOP)
+               return NULL;
+
+       offset = ipv6_find_tlv(skb, sizeof(*ip6_hdr), IPV6_TLV_CALIPSO);
+       if (offset >= 0)
+               return (unsigned char *)ip6_hdr + offset;
+
+       return NULL;
+}
+
+/**
+ * calipso_skbuff_setattr - Set the CALIPSO option on a packet
+ * @skb: the packet
+ * @doi_def: the CALIPSO DOI to use
+ * @secattr: the security attributes
+ *
+ * Description:
+ * Set the CALIPSO option on the given packet based on the security attributes.
+ * Returns a pointer to the IP header on success and NULL on failure.
+ *
+ */
+static int calipso_skbuff_setattr(struct sk_buff *skb,
+                                 const struct calipso_doi *doi_def,
+                                 const struct netlbl_lsm_secattr *secattr)
+{
+       int ret_val;
+       struct ipv6hdr *ip6_hdr;
+       struct ipv6_opt_hdr *hop;
+       unsigned char buf[CALIPSO_MAX_BUFFER];
+       int len_delta, new_end, pad;
+       unsigned int start, end;
+
+       ip6_hdr = ipv6_hdr(skb);
+       if (ip6_hdr->nexthdr == NEXTHDR_HOP) {
+               hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
+               ret_val = calipso_opt_find(hop, &start, &end);
+               if (ret_val && ret_val != -ENOENT)
+                       return ret_val;
+       } else {
+               start = 0;
+               end = 0;
+       }
+
+       memset(buf, 0, sizeof(buf));
+       ret_val = calipso_genopt(buf, start & 3, sizeof(buf), doi_def, secattr);
+       if (ret_val < 0)
+               return ret_val;
+
+       new_end = start + ret_val;
+       /* At this point new_end aligns to 4n, so (new_end & 4) pads to 8n */
+       pad = ((new_end & 4) + (end & 7)) & 7;
+       len_delta = new_end - (int)end + pad;
+       ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
+       if (ret_val < 0)
+               return ret_val;
+
+       if (len_delta) {
+               if (len_delta > 0)
+                       skb_push(skb, len_delta);
+               else
+                       skb_pull(skb, -len_delta);
+               memmove((char *)ip6_hdr - len_delta, ip6_hdr,
+                       sizeof(*ip6_hdr) + start);
+               skb_reset_network_header(skb);
+               ip6_hdr = ipv6_hdr(skb);
+       }
+
+       hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
+       if (start == 0) {
+               struct ipv6_opt_hdr *new_hop = (struct ipv6_opt_hdr *)buf;
+
+               new_hop->nexthdr = ip6_hdr->nexthdr;
+               new_hop->hdrlen = len_delta / 8 - 1;
+               ip6_hdr->nexthdr = NEXTHDR_HOP;
+       } else {
+               hop->hdrlen += len_delta / 8;
+       }
+       memcpy((char *)hop + start, buf + (start & 3), new_end - start);
+       calipso_pad_write((unsigned char *)hop, new_end, pad);
+
+       return 0;
+}
+
+/**
+ * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
+ * @skb: the packet
+ *
+ * Description:
+ * Removes any and all CALIPSO options from the given packet.  Returns zero on
+ * success, negative values on failure.
+ *
+ */
+static int calipso_skbuff_delattr(struct sk_buff *skb)
+{
+       int ret_val;
+       struct ipv6hdr *ip6_hdr;
+       struct ipv6_opt_hdr *old_hop;
+       u32 old_hop_len, start = 0, end = 0, delta, size, pad;
+
+       if (!calipso_skbuff_optptr(skb))
+               return 0;
+
+       /* since we are changing the packet we should make a copy */
+       ret_val = skb_cow(skb, skb_headroom(skb));
+       if (ret_val < 0)
+               return ret_val;
+
+       ip6_hdr = ipv6_hdr(skb);
+       old_hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
+       old_hop_len = ipv6_optlen(old_hop);
+
+       ret_val = calipso_opt_find(old_hop, &start, &end);
+       if (ret_val)
+               return ret_val;
+
+       if (start == sizeof(*old_hop) && end == old_hop_len) {
+               /* There's no other option in the header so we delete
+                * the whole thing. */
+               delta = old_hop_len;
+               size = sizeof(*ip6_hdr);
+               ip6_hdr->nexthdr = old_hop->nexthdr;
+       } else {
+               delta = (end - start) & ~7;
+               if (delta)
+                       old_hop->hdrlen -= delta / 8;
+               pad = (end - start) & 7;
+               size = sizeof(*ip6_hdr) + start + pad;
+               calipso_pad_write((unsigned char *)old_hop, start, pad);
+       }
+
+       if (delta) {
+               skb_pull(skb, delta);
+               memmove((char *)ip6_hdr + delta, ip6_hdr, size);
+               skb_reset_network_header(skb);
+       }
+
+       return 0;
+}
+
 static const struct netlbl_calipso_ops ops = {
        .doi_add          = calipso_doi_add,
        .doi_free         = calipso_doi_free,
@@ -899,6 +1439,14 @@ static const struct netlbl_calipso_ops ops = {
        .sock_getattr     = calipso_sock_getattr,
        .sock_setattr     = calipso_sock_setattr,
        .sock_delattr     = calipso_sock_delattr,
+       .req_setattr      = calipso_req_setattr,
+       .req_delattr      = calipso_req_delattr,
+       .opt_getattr      = calipso_opt_getattr,
+       .skbuff_optptr    = calipso_skbuff_optptr,
+       .skbuff_setattr   = calipso_skbuff_setattr,
+       .skbuff_delattr   = calipso_skbuff_delattr,
+       .cache_invalidate = calipso_cache_invalidate,
+       .cache_add        = calipso_cache_add
 };
 
 /**
@@ -911,11 +1459,17 @@ static const struct netlbl_calipso_ops ops = {
  */
 int __init calipso_init(void)
 {
-       netlbl_calipso_ops_register(&ops);
-       return 0;
+       int ret_val;
+
+       ret_val = calipso_cache_init();
+       if (!ret_val)
+               netlbl_calipso_ops_register(&ops);
+       return ret_val;
 }
 
 void calipso_exit(void)
 {
        netlbl_calipso_ops_register(NULL);
+       calipso_cache_invalidate();
+       kfree(calipso_cache);
 }