xref: /openbmc/linux/net/ipv6/calipso.c (revision c37fe6aff89cb0d842993fe2f69e48bf3ebe0ab0)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2cb72d382SHuw Davies /*
3cb72d382SHuw Davies  * CALIPSO - Common Architecture Label IPv6 Security Option
4cb72d382SHuw Davies  *
5cb72d382SHuw Davies  * This is an implementation of the CALIPSO protocol as specified in
6cb72d382SHuw Davies  * RFC 5570.
7cb72d382SHuw Davies  *
8cb72d382SHuw Davies  * Authors: Paul Moore <paul.moore@hp.com>
9cb72d382SHuw Davies  *          Huw Davies <huw@codeweavers.com>
10cb72d382SHuw Davies  */
11cb72d382SHuw Davies 
12cb72d382SHuw Davies /* (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
13cb72d382SHuw Davies  * (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
14cb72d382SHuw Davies  */
15cb72d382SHuw Davies 
16cb72d382SHuw Davies #include <linux/init.h>
17cb72d382SHuw Davies #include <linux/types.h>
18cb72d382SHuw Davies #include <linux/rcupdate.h>
19cb72d382SHuw Davies #include <linux/list.h>
20cb72d382SHuw Davies #include <linux/spinlock.h>
21cb72d382SHuw Davies #include <linux/string.h>
22cb72d382SHuw Davies #include <linux/jhash.h>
23cb72d382SHuw Davies #include <linux/audit.h>
24cb72d382SHuw Davies #include <linux/slab.h>
25cb72d382SHuw Davies #include <net/ip.h>
26cb72d382SHuw Davies #include <net/icmp.h>
27cb72d382SHuw Davies #include <net/tcp.h>
28cb72d382SHuw Davies #include <net/netlabel.h>
29cb72d382SHuw Davies #include <net/calipso.h>
30cb72d382SHuw Davies #include <linux/atomic.h>
31cb72d382SHuw Davies #include <linux/bug.h>
32cb72d382SHuw Davies #include <asm/unaligned.h>
33ceba1832SHuw Davies #include <linux/crc-ccitt.h>
34ceba1832SHuw Davies 
35ceba1832SHuw Davies /* Maximium size of the calipso option including
36ceba1832SHuw Davies  * the two-byte TLV header.
37ceba1832SHuw Davies  */
38ceba1832SHuw Davies #define CALIPSO_OPT_LEN_MAX (2 + 252)
39ceba1832SHuw Davies 
40ceba1832SHuw Davies /* Size of the minimum calipso option including
41ceba1832SHuw Davies  * the two-byte TLV header.
42ceba1832SHuw Davies  */
43ceba1832SHuw Davies #define CALIPSO_HDR_LEN (2 + 8)
44ceba1832SHuw Davies 
45ceba1832SHuw Davies /* Maximium size of the calipso option including
46ceba1832SHuw Davies  * the two-byte TLV header and upto 3 bytes of
47ceba1832SHuw Davies  * leading pad and 7 bytes of trailing pad.
48ceba1832SHuw Davies  */
49ceba1832SHuw Davies #define CALIPSO_OPT_LEN_MAX_WITH_PAD (3 + CALIPSO_OPT_LEN_MAX + 7)
50ceba1832SHuw Davies 
512917f57bSHuw Davies  /* Maximium size of u32 aligned buffer required to hold calipso
522917f57bSHuw Davies   * option.  Max of 3 initial pad bytes starting from buffer + 3.
532917f57bSHuw Davies   * i.e. the worst case is when the previous tlv finishes on 4n + 3.
542917f57bSHuw Davies   */
552917f57bSHuw Davies #define CALIPSO_MAX_BUFFER (6 + CALIPSO_OPT_LEN_MAX)
56cb72d382SHuw Davies 
57cb72d382SHuw Davies /* List of available DOI definitions */
58cb72d382SHuw Davies static DEFINE_SPINLOCK(calipso_doi_list_lock);
59cb72d382SHuw Davies static LIST_HEAD(calipso_doi_list);
60cb72d382SHuw Davies 
614fee5242SHuw Davies /* Label mapping cache */
624fee5242SHuw Davies int calipso_cache_enabled = 1;
634fee5242SHuw Davies int calipso_cache_bucketsize = 10;
644fee5242SHuw Davies #define CALIPSO_CACHE_BUCKETBITS     7
654fee5242SHuw Davies #define CALIPSO_CACHE_BUCKETS        BIT(CALIPSO_CACHE_BUCKETBITS)
664fee5242SHuw Davies #define CALIPSO_CACHE_REORDERLIMIT   10
674fee5242SHuw Davies struct calipso_map_cache_bkt {
684fee5242SHuw Davies 	spinlock_t lock;
694fee5242SHuw Davies 	u32 size;
704fee5242SHuw Davies 	struct list_head list;
714fee5242SHuw Davies };
724fee5242SHuw Davies 
734fee5242SHuw Davies struct calipso_map_cache_entry {
744fee5242SHuw Davies 	u32 hash;
754fee5242SHuw Davies 	unsigned char *key;
764fee5242SHuw Davies 	size_t key_len;
774fee5242SHuw Davies 
784fee5242SHuw Davies 	struct netlbl_lsm_cache *lsm_data;
794fee5242SHuw Davies 
804fee5242SHuw Davies 	u32 activity;
814fee5242SHuw Davies 	struct list_head list;
824fee5242SHuw Davies };
834fee5242SHuw Davies 
844fee5242SHuw Davies static struct calipso_map_cache_bkt *calipso_cache;
854fee5242SHuw Davies 
86*ad5d07f4SPaul Moore static void calipso_cache_invalidate(void);
87*ad5d07f4SPaul Moore static void calipso_doi_putdef(struct calipso_doi *doi_def);
88*ad5d07f4SPaul Moore 
894fee5242SHuw Davies /* Label Mapping Cache Functions
904fee5242SHuw Davies  */
914fee5242SHuw Davies 
924fee5242SHuw Davies /**
934fee5242SHuw Davies  * calipso_cache_entry_free - Frees a cache entry
944fee5242SHuw Davies  * @entry: the entry to free
954fee5242SHuw Davies  *
964fee5242SHuw Davies  * Description:
974fee5242SHuw Davies  * This function frees the memory associated with a cache entry including the
984fee5242SHuw Davies  * LSM cache data if there are no longer any users, i.e. reference count == 0.
994fee5242SHuw Davies  *
1004fee5242SHuw Davies  */
calipso_cache_entry_free(struct calipso_map_cache_entry * entry)1014fee5242SHuw Davies static void calipso_cache_entry_free(struct calipso_map_cache_entry *entry)
1024fee5242SHuw Davies {
1034fee5242SHuw Davies 	if (entry->lsm_data)
1044fee5242SHuw Davies 		netlbl_secattr_cache_free(entry->lsm_data);
1054fee5242SHuw Davies 	kfree(entry->key);
1064fee5242SHuw Davies 	kfree(entry);
1074fee5242SHuw Davies }
1084fee5242SHuw Davies 
1094fee5242SHuw Davies /**
1104fee5242SHuw Davies  * calipso_map_cache_hash - Hashing function for the CALIPSO cache
1114fee5242SHuw Davies  * @key: the hash key
1124fee5242SHuw Davies  * @key_len: the length of the key in bytes
1134fee5242SHuw Davies  *
1144fee5242SHuw Davies  * Description:
1154fee5242SHuw Davies  * The CALIPSO tag hashing function.  Returns a 32-bit hash value.
1164fee5242SHuw Davies  *
1174fee5242SHuw Davies  */
calipso_map_cache_hash(const unsigned char * key,u32 key_len)1184fee5242SHuw Davies static u32 calipso_map_cache_hash(const unsigned char *key, u32 key_len)
1194fee5242SHuw Davies {
1204fee5242SHuw Davies 	return jhash(key, key_len, 0);
1214fee5242SHuw Davies }
1224fee5242SHuw Davies 
1234fee5242SHuw Davies /**
1244fee5242SHuw Davies  * calipso_cache_init - Initialize the CALIPSO cache
1254fee5242SHuw Davies  *
1264fee5242SHuw Davies  * Description:
1274fee5242SHuw Davies  * Initializes the CALIPSO label mapping cache, this function should be called
1284fee5242SHuw Davies  * before any of the other functions defined in this file.  Returns zero on
1294fee5242SHuw Davies  * success, negative values on error.
1304fee5242SHuw Davies  *
1314fee5242SHuw Davies  */
calipso_cache_init(void)1324fee5242SHuw Davies static int __init calipso_cache_init(void)
1334fee5242SHuw Davies {
1344fee5242SHuw Davies 	u32 iter;
1354fee5242SHuw Davies 
1364fee5242SHuw Davies 	calipso_cache = kcalloc(CALIPSO_CACHE_BUCKETS,
1374fee5242SHuw Davies 				sizeof(struct calipso_map_cache_bkt),
1384fee5242SHuw Davies 				GFP_KERNEL);
1394fee5242SHuw Davies 	if (!calipso_cache)
1404fee5242SHuw Davies 		return -ENOMEM;
1414fee5242SHuw Davies 
1424fee5242SHuw Davies 	for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
1434fee5242SHuw Davies 		spin_lock_init(&calipso_cache[iter].lock);
1444fee5242SHuw Davies 		calipso_cache[iter].size = 0;
1454fee5242SHuw Davies 		INIT_LIST_HEAD(&calipso_cache[iter].list);
1464fee5242SHuw Davies 	}
1474fee5242SHuw Davies 
1484fee5242SHuw Davies 	return 0;
1494fee5242SHuw Davies }
1504fee5242SHuw Davies 
1514fee5242SHuw Davies /**
1524fee5242SHuw Davies  * calipso_cache_invalidate - Invalidates the current CALIPSO cache
1534fee5242SHuw Davies  *
1544fee5242SHuw Davies  * Description:
1554fee5242SHuw Davies  * Invalidates and frees any entries in the CALIPSO cache.  Returns zero on
1564fee5242SHuw Davies  * success and negative values on failure.
1574fee5242SHuw Davies  *
1584fee5242SHuw Davies  */
calipso_cache_invalidate(void)1594fee5242SHuw Davies static void calipso_cache_invalidate(void)
1604fee5242SHuw Davies {
1614fee5242SHuw Davies 	struct calipso_map_cache_entry *entry, *tmp_entry;
1624fee5242SHuw Davies 	u32 iter;
1634fee5242SHuw Davies 
1644fee5242SHuw Davies 	for (iter = 0; iter < CALIPSO_CACHE_BUCKETS; iter++) {
1654fee5242SHuw Davies 		spin_lock_bh(&calipso_cache[iter].lock);
1664fee5242SHuw Davies 		list_for_each_entry_safe(entry,
1674fee5242SHuw Davies 					 tmp_entry,
1684fee5242SHuw Davies 					 &calipso_cache[iter].list, list) {
1694fee5242SHuw Davies 			list_del(&entry->list);
1704fee5242SHuw Davies 			calipso_cache_entry_free(entry);
1714fee5242SHuw Davies 		}
1724fee5242SHuw Davies 		calipso_cache[iter].size = 0;
1734fee5242SHuw Davies 		spin_unlock_bh(&calipso_cache[iter].lock);
1744fee5242SHuw Davies 	}
1754fee5242SHuw Davies }
1764fee5242SHuw Davies 
1774fee5242SHuw Davies /**
1784fee5242SHuw Davies  * calipso_cache_check - Check the CALIPSO cache for a label mapping
1794fee5242SHuw Davies  * @key: the buffer to check
1804fee5242SHuw Davies  * @key_len: buffer length in bytes
1814fee5242SHuw Davies  * @secattr: the security attribute struct to use
1824fee5242SHuw Davies  *
1834fee5242SHuw Davies  * Description:
1844fee5242SHuw Davies  * This function checks the cache to see if a label mapping already exists for
1854fee5242SHuw Davies  * the given key.  If there is a match then the cache is adjusted and the
1864fee5242SHuw Davies  * @secattr struct is populated with the correct LSM security attributes.  The
1874fee5242SHuw Davies  * cache is adjusted in the following manner if the entry is not already the
1884fee5242SHuw Davies  * first in the cache bucket:
1894fee5242SHuw Davies  *
1904fee5242SHuw Davies  *  1. The cache entry's activity counter is incremented
1914fee5242SHuw Davies  *  2. The previous (higher ranking) entry's activity counter is decremented
1924fee5242SHuw Davies  *  3. If the difference between the two activity counters is geater than
1934fee5242SHuw Davies  *     CALIPSO_CACHE_REORDERLIMIT the two entries are swapped
1944fee5242SHuw Davies  *
1954fee5242SHuw Davies  * Returns zero on success, -ENOENT for a cache miss, and other negative values
1964fee5242SHuw Davies  * on error.
1974fee5242SHuw Davies  *
1984fee5242SHuw Davies  */
calipso_cache_check(const unsigned char * key,u32 key_len,struct netlbl_lsm_secattr * secattr)1994fee5242SHuw Davies static int calipso_cache_check(const unsigned char *key,
2004fee5242SHuw Davies 			       u32 key_len,
2014fee5242SHuw Davies 			       struct netlbl_lsm_secattr *secattr)
2024fee5242SHuw Davies {
2034fee5242SHuw Davies 	u32 bkt;
2044fee5242SHuw Davies 	struct calipso_map_cache_entry *entry;
2054fee5242SHuw Davies 	struct calipso_map_cache_entry *prev_entry = NULL;
2064fee5242SHuw Davies 	u32 hash;
2074fee5242SHuw Davies 
2084fee5242SHuw Davies 	if (!calipso_cache_enabled)
2094fee5242SHuw Davies 		return -ENOENT;
2104fee5242SHuw Davies 
2114fee5242SHuw Davies 	hash = calipso_map_cache_hash(key, key_len);
2124fee5242SHuw Davies 	bkt = hash & (CALIPSO_CACHE_BUCKETS - 1);
2134fee5242SHuw Davies 	spin_lock_bh(&calipso_cache[bkt].lock);
2144fee5242SHuw Davies 	list_for_each_entry(entry, &calipso_cache[bkt].list, list) {
2154fee5242SHuw Davies 		if (entry->hash == hash &&
2164fee5242SHuw Davies 		    entry->key_len == key_len &&
2174fee5242SHuw Davies 		    memcmp(entry->key, key, key_len) == 0) {
2184fee5242SHuw Davies 			entry->activity += 1;
219b4217b82SReshetova, Elena 			refcount_inc(&entry->lsm_data->refcount);
2204fee5242SHuw Davies 			secattr->cache = entry->lsm_data;
2214fee5242SHuw Davies 			secattr->flags |= NETLBL_SECATTR_CACHE;
2224fee5242SHuw Davies 			secattr->type = NETLBL_NLTYPE_CALIPSO;
2234fee5242SHuw Davies 			if (!prev_entry) {
2244fee5242SHuw Davies 				spin_unlock_bh(&calipso_cache[bkt].lock);
2254fee5242SHuw Davies 				return 0;
2264fee5242SHuw Davies 			}
2274fee5242SHuw Davies 
2284fee5242SHuw Davies 			if (prev_entry->activity > 0)
2294fee5242SHuw Davies 				prev_entry->activity -= 1;
2304fee5242SHuw Davies 			if (entry->activity > prev_entry->activity &&
2314fee5242SHuw Davies 			    entry->activity - prev_entry->activity >
2324fee5242SHuw Davies 			    CALIPSO_CACHE_REORDERLIMIT) {
2334fee5242SHuw Davies 				__list_del(entry->list.prev, entry->list.next);
2344fee5242SHuw Davies 				__list_add(&entry->list,
2354fee5242SHuw Davies 					   prev_entry->list.prev,
2364fee5242SHuw Davies 					   &prev_entry->list);
2374fee5242SHuw Davies 			}
2384fee5242SHuw Davies 
2394fee5242SHuw Davies 			spin_unlock_bh(&calipso_cache[bkt].lock);
2404fee5242SHuw Davies 			return 0;
2414fee5242SHuw Davies 		}
2424fee5242SHuw Davies 		prev_entry = entry;
2434fee5242SHuw Davies 	}
2444fee5242SHuw Davies 	spin_unlock_bh(&calipso_cache[bkt].lock);
2454fee5242SHuw Davies 
2464fee5242SHuw Davies 	return -ENOENT;
2474fee5242SHuw Davies }
2484fee5242SHuw Davies 
2494fee5242SHuw Davies /**
2504fee5242SHuw Davies  * calipso_cache_add - Add an entry to the CALIPSO cache
2514fee5242SHuw Davies  * @calipso_ptr: the CALIPSO option
2524fee5242SHuw Davies  * @secattr: the packet's security attributes
2534fee5242SHuw Davies  *
2544fee5242SHuw Davies  * Description:
2554fee5242SHuw Davies  * Add a new entry into the CALIPSO label mapping cache.  Add the new entry to
2564fee5242SHuw Davies  * head of the cache bucket's list, if the cache bucket is out of room remove
2574fee5242SHuw Davies  * the last entry in the list first.  It is important to note that there is
2584fee5242SHuw Davies  * currently no checking for duplicate keys.  Returns zero on success,
2594fee5242SHuw Davies  * negative values on failure.  The key stored starts at calipso_ptr + 2,
2604fee5242SHuw Davies  * i.e. the type and length bytes are not stored, this corresponds to
2614fee5242SHuw Davies  * calipso_ptr[1] bytes of data.
2624fee5242SHuw Davies  *
2634fee5242SHuw Davies  */
calipso_cache_add(const unsigned char * calipso_ptr,const struct netlbl_lsm_secattr * secattr)2644fee5242SHuw Davies static int calipso_cache_add(const unsigned char *calipso_ptr,
2654fee5242SHuw Davies 			     const struct netlbl_lsm_secattr *secattr)
2664fee5242SHuw Davies {
2674fee5242SHuw Davies 	int ret_val = -EPERM;
2684fee5242SHuw Davies 	u32 bkt;
2694fee5242SHuw Davies 	struct calipso_map_cache_entry *entry = NULL;
2704fee5242SHuw Davies 	struct calipso_map_cache_entry *old_entry = NULL;
2714fee5242SHuw Davies 	u32 calipso_ptr_len;
2724fee5242SHuw Davies 
2734fee5242SHuw Davies 	if (!calipso_cache_enabled || calipso_cache_bucketsize <= 0)
2744fee5242SHuw Davies 		return 0;
2754fee5242SHuw Davies 
2764fee5242SHuw Davies 	calipso_ptr_len = calipso_ptr[1];
2774fee5242SHuw Davies 
2784fee5242SHuw Davies 	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2794fee5242SHuw Davies 	if (!entry)
2804fee5242SHuw Davies 		return -ENOMEM;
2814fee5242SHuw Davies 	entry->key = kmemdup(calipso_ptr + 2, calipso_ptr_len, GFP_ATOMIC);
2824fee5242SHuw Davies 	if (!entry->key) {
2834fee5242SHuw Davies 		ret_val = -ENOMEM;
2844fee5242SHuw Davies 		goto cache_add_failure;
2854fee5242SHuw Davies 	}
2864fee5242SHuw Davies 	entry->key_len = calipso_ptr_len;
2874fee5242SHuw Davies 	entry->hash = calipso_map_cache_hash(calipso_ptr, calipso_ptr_len);
288b4217b82SReshetova, Elena 	refcount_inc(&secattr->cache->refcount);
2894fee5242SHuw Davies 	entry->lsm_data = secattr->cache;
2904fee5242SHuw Davies 
2914fee5242SHuw Davies 	bkt = entry->hash & (CALIPSO_CACHE_BUCKETS - 1);
2924fee5242SHuw Davies 	spin_lock_bh(&calipso_cache[bkt].lock);
2934fee5242SHuw Davies 	if (calipso_cache[bkt].size < calipso_cache_bucketsize) {
2944fee5242SHuw Davies 		list_add(&entry->list, &calipso_cache[bkt].list);
2954fee5242SHuw Davies 		calipso_cache[bkt].size += 1;
2964fee5242SHuw Davies 	} else {
2974fee5242SHuw Davies 		old_entry = list_entry(calipso_cache[bkt].list.prev,
2984fee5242SHuw Davies 				       struct calipso_map_cache_entry, list);
2994fee5242SHuw Davies 		list_del(&old_entry->list);
3004fee5242SHuw Davies 		list_add(&entry->list, &calipso_cache[bkt].list);
3014fee5242SHuw Davies 		calipso_cache_entry_free(old_entry);
3024fee5242SHuw Davies 	}
3034fee5242SHuw Davies 	spin_unlock_bh(&calipso_cache[bkt].lock);
3044fee5242SHuw Davies 
3054fee5242SHuw Davies 	return 0;
3064fee5242SHuw Davies 
3074fee5242SHuw Davies cache_add_failure:
3084fee5242SHuw Davies 	if (entry)
3094fee5242SHuw Davies 		calipso_cache_entry_free(entry);
3104fee5242SHuw Davies 	return ret_val;
3114fee5242SHuw Davies }
3124fee5242SHuw Davies 
313cb72d382SHuw Davies /* DOI List Functions
314cb72d382SHuw Davies  */
315cb72d382SHuw Davies 
316cb72d382SHuw Davies /**
317cb72d382SHuw Davies  * calipso_doi_search - Searches for a DOI definition
318cb72d382SHuw Davies  * @doi: the DOI to search for
319cb72d382SHuw Davies  *
320cb72d382SHuw Davies  * Description:
321cb72d382SHuw Davies  * Search the DOI definition list for a DOI definition with a DOI value that
322cb72d382SHuw Davies  * matches @doi.  The caller is responsible for calling rcu_read_[un]lock().
323cb72d382SHuw Davies  * Returns a pointer to the DOI definition on success and NULL on failure.
324cb72d382SHuw Davies  */
calipso_doi_search(u32 doi)325cb72d382SHuw Davies static struct calipso_doi *calipso_doi_search(u32 doi)
326cb72d382SHuw Davies {
327cb72d382SHuw Davies 	struct calipso_doi *iter;
328cb72d382SHuw Davies 
329cb72d382SHuw Davies 	list_for_each_entry_rcu(iter, &calipso_doi_list, list)
330edcd9270SReshetova, Elena 		if (iter->doi == doi && refcount_read(&iter->refcount))
331cb72d382SHuw Davies 			return iter;
332cb72d382SHuw Davies 	return NULL;
333cb72d382SHuw Davies }
334cb72d382SHuw Davies 
335cb72d382SHuw Davies /**
336cb72d382SHuw Davies  * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
337cb72d382SHuw Davies  * @doi_def: the DOI structure
338cb72d382SHuw Davies  * @audit_info: NetLabel audit information
339cb72d382SHuw Davies  *
340cb72d382SHuw Davies  * Description:
341cb72d382SHuw Davies  * The caller defines a new DOI for use by the CALIPSO engine and calls this
342cb72d382SHuw Davies  * function to add it to the list of acceptable domains.  The caller must
343cb72d382SHuw Davies  * ensure that the mapping table specified in @doi_def->map meets all of the
344cb72d382SHuw Davies  * requirements of the mapping type (see calipso.h for details).  Returns
345cb72d382SHuw Davies  * zero on success and non-zero on failure.
346cb72d382SHuw Davies  *
347cb72d382SHuw Davies  */
calipso_doi_add(struct calipso_doi * doi_def,struct netlbl_audit * audit_info)348cb72d382SHuw Davies static int calipso_doi_add(struct calipso_doi *doi_def,
349cb72d382SHuw Davies 			   struct netlbl_audit *audit_info)
350cb72d382SHuw Davies {
351cb72d382SHuw Davies 	int ret_val = -EINVAL;
352cb72d382SHuw Davies 	u32 doi;
353cb72d382SHuw Davies 	u32 doi_type;
354cb72d382SHuw Davies 	struct audit_buffer *audit_buf;
355cb72d382SHuw Davies 
356cb72d382SHuw Davies 	doi = doi_def->doi;
357cb72d382SHuw Davies 	doi_type = doi_def->type;
358cb72d382SHuw Davies 
359cb72d382SHuw Davies 	if (doi_def->doi == CALIPSO_DOI_UNKNOWN)
360cb72d382SHuw Davies 		goto doi_add_return;
361cb72d382SHuw Davies 
362edcd9270SReshetova, Elena 	refcount_set(&doi_def->refcount, 1);
363cb72d382SHuw Davies 
364cb72d382SHuw Davies 	spin_lock(&calipso_doi_list_lock);
365cb72d382SHuw Davies 	if (calipso_doi_search(doi_def->doi)) {
366cb72d382SHuw Davies 		spin_unlock(&calipso_doi_list_lock);
367cb72d382SHuw Davies 		ret_val = -EEXIST;
368cb72d382SHuw Davies 		goto doi_add_return;
369cb72d382SHuw Davies 	}
370cb72d382SHuw Davies 	list_add_tail_rcu(&doi_def->list, &calipso_doi_list);
371cb72d382SHuw Davies 	spin_unlock(&calipso_doi_list_lock);
372cb72d382SHuw Davies 	ret_val = 0;
373cb72d382SHuw Davies 
374cb72d382SHuw Davies doi_add_return:
375cb72d382SHuw Davies 	audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_ADD, audit_info);
376cb72d382SHuw Davies 	if (audit_buf) {
377cb72d382SHuw Davies 		const char *type_str;
378cb72d382SHuw Davies 
379cb72d382SHuw Davies 		switch (doi_type) {
380cb72d382SHuw Davies 		case CALIPSO_MAP_PASS:
381cb72d382SHuw Davies 			type_str = "pass";
382cb72d382SHuw Davies 			break;
383cb72d382SHuw Davies 		default:
384cb72d382SHuw Davies 			type_str = "(unknown)";
385cb72d382SHuw Davies 		}
386cb72d382SHuw Davies 		audit_log_format(audit_buf,
387cb72d382SHuw Davies 				 " calipso_doi=%u calipso_type=%s res=%u",
388cb72d382SHuw Davies 				 doi, type_str, ret_val == 0 ? 1 : 0);
389cb72d382SHuw Davies 		audit_log_end(audit_buf);
390cb72d382SHuw Davies 	}
391cb72d382SHuw Davies 
392cb72d382SHuw Davies 	return ret_val;
393cb72d382SHuw Davies }
394cb72d382SHuw Davies 
395cb72d382SHuw Davies /**
396cb72d382SHuw Davies  * calipso_doi_free - Frees a DOI definition
397cb72d382SHuw Davies  * @doi_def: the DOI definition
398cb72d382SHuw Davies  *
399cb72d382SHuw Davies  * Description:
400cb72d382SHuw Davies  * This function frees all of the memory associated with a DOI definition.
401cb72d382SHuw Davies  *
402cb72d382SHuw Davies  */
calipso_doi_free(struct calipso_doi * doi_def)403cb72d382SHuw Davies static void calipso_doi_free(struct calipso_doi *doi_def)
404cb72d382SHuw Davies {
405cb72d382SHuw Davies 	kfree(doi_def);
406cb72d382SHuw Davies }
407cb72d382SHuw Davies 
408a5e34490SHuw Davies /**
409a5e34490SHuw Davies  * calipso_doi_free_rcu - Frees a DOI definition via the RCU pointer
410a5e34490SHuw Davies  * @entry: the entry's RCU field
411a5e34490SHuw Davies  *
412a5e34490SHuw Davies  * Description:
413a5e34490SHuw Davies  * This function is designed to be used as a callback to the call_rcu()
414a5e34490SHuw Davies  * function so that the memory allocated to the DOI definition can be released
415a5e34490SHuw Davies  * safely.
416a5e34490SHuw Davies  *
417a5e34490SHuw Davies  */
calipso_doi_free_rcu(struct rcu_head * entry)418a5e34490SHuw Davies static void calipso_doi_free_rcu(struct rcu_head *entry)
419a5e34490SHuw Davies {
420a5e34490SHuw Davies 	struct calipso_doi *doi_def;
421a5e34490SHuw Davies 
422a5e34490SHuw Davies 	doi_def = container_of(entry, struct calipso_doi, rcu);
423a5e34490SHuw Davies 	calipso_doi_free(doi_def);
424a5e34490SHuw Davies }
425a5e34490SHuw Davies 
426a5e34490SHuw Davies /**
427d7cce015SHuw Davies  * calipso_doi_remove - Remove an existing DOI from the CALIPSO protocol engine
428d7cce015SHuw Davies  * @doi: the DOI value
4298da1f442SAndrew Lunn  * @audit_info: NetLabel audit information
430d7cce015SHuw Davies  *
431d7cce015SHuw Davies  * Description:
432d7cce015SHuw Davies  * Removes a DOI definition from the CALIPSO engine.  The NetLabel routines will
433d7cce015SHuw Davies  * be called to release their own LSM domain mappings as well as our own
434d7cce015SHuw Davies  * domain list.  Returns zero on success and negative values on failure.
435d7cce015SHuw Davies  *
436d7cce015SHuw Davies  */
calipso_doi_remove(u32 doi,struct netlbl_audit * audit_info)437d7cce015SHuw Davies static int calipso_doi_remove(u32 doi, struct netlbl_audit *audit_info)
438d7cce015SHuw Davies {
439d7cce015SHuw Davies 	int ret_val;
440d7cce015SHuw Davies 	struct calipso_doi *doi_def;
441d7cce015SHuw Davies 	struct audit_buffer *audit_buf;
442d7cce015SHuw Davies 
443d7cce015SHuw Davies 	spin_lock(&calipso_doi_list_lock);
444d7cce015SHuw Davies 	doi_def = calipso_doi_search(doi);
445d7cce015SHuw Davies 	if (!doi_def) {
446d7cce015SHuw Davies 		spin_unlock(&calipso_doi_list_lock);
447d7cce015SHuw Davies 		ret_val = -ENOENT;
448d7cce015SHuw Davies 		goto doi_remove_return;
449d7cce015SHuw Davies 	}
450d7cce015SHuw Davies 	list_del_rcu(&doi_def->list);
451d7cce015SHuw Davies 	spin_unlock(&calipso_doi_list_lock);
452d7cce015SHuw Davies 
453*ad5d07f4SPaul Moore 	calipso_doi_putdef(doi_def);
454d7cce015SHuw Davies 	ret_val = 0;
455d7cce015SHuw Davies 
456d7cce015SHuw Davies doi_remove_return:
457d7cce015SHuw Davies 	audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_DEL, audit_info);
458d7cce015SHuw Davies 	if (audit_buf) {
459d7cce015SHuw Davies 		audit_log_format(audit_buf,
460d7cce015SHuw Davies 				 " calipso_doi=%u res=%u",
461d7cce015SHuw Davies 				 doi, ret_val == 0 ? 1 : 0);
462d7cce015SHuw Davies 		audit_log_end(audit_buf);
463d7cce015SHuw Davies 	}
464d7cce015SHuw Davies 
465d7cce015SHuw Davies 	return ret_val;
466d7cce015SHuw Davies }
467d7cce015SHuw Davies 
468d7cce015SHuw Davies /**
469a5e34490SHuw Davies  * calipso_doi_getdef - Returns a reference to a valid DOI definition
470a5e34490SHuw Davies  * @doi: the DOI value
471a5e34490SHuw Davies  *
472a5e34490SHuw Davies  * Description:
473a5e34490SHuw Davies  * Searches for a valid DOI definition and if one is found it is returned to
474a5e34490SHuw Davies  * the caller.  Otherwise NULL is returned.  The caller must ensure that
475a5e34490SHuw Davies  * calipso_doi_putdef() is called when the caller is done.
476a5e34490SHuw Davies  *
477a5e34490SHuw Davies  */
calipso_doi_getdef(u32 doi)478a5e34490SHuw Davies static struct calipso_doi *calipso_doi_getdef(u32 doi)
479a5e34490SHuw Davies {
480a5e34490SHuw Davies 	struct calipso_doi *doi_def;
481a5e34490SHuw Davies 
482a5e34490SHuw Davies 	rcu_read_lock();
483a5e34490SHuw Davies 	doi_def = calipso_doi_search(doi);
484a5e34490SHuw Davies 	if (!doi_def)
485a5e34490SHuw Davies 		goto doi_getdef_return;
486edcd9270SReshetova, Elena 	if (!refcount_inc_not_zero(&doi_def->refcount))
487a5e34490SHuw Davies 		doi_def = NULL;
488a5e34490SHuw Davies 
489a5e34490SHuw Davies doi_getdef_return:
490a5e34490SHuw Davies 	rcu_read_unlock();
491a5e34490SHuw Davies 	return doi_def;
492a5e34490SHuw Davies }
493a5e34490SHuw Davies 
494a5e34490SHuw Davies /**
495a5e34490SHuw Davies  * calipso_doi_putdef - Releases a reference for the given DOI definition
496a5e34490SHuw Davies  * @doi_def: the DOI definition
497a5e34490SHuw Davies  *
498a5e34490SHuw Davies  * Description:
499a5e34490SHuw Davies  * Releases a DOI definition reference obtained from calipso_doi_getdef().
500a5e34490SHuw Davies  *
501a5e34490SHuw Davies  */
calipso_doi_putdef(struct calipso_doi * doi_def)502a5e34490SHuw Davies static void calipso_doi_putdef(struct calipso_doi *doi_def)
503a5e34490SHuw Davies {
504a5e34490SHuw Davies 	if (!doi_def)
505a5e34490SHuw Davies 		return;
506a5e34490SHuw Davies 
507edcd9270SReshetova, Elena 	if (!refcount_dec_and_test(&doi_def->refcount))
508a5e34490SHuw Davies 		return;
509a5e34490SHuw Davies 
510*ad5d07f4SPaul Moore 	calipso_cache_invalidate();
511a5e34490SHuw Davies 	call_rcu(&doi_def->rcu, calipso_doi_free_rcu);
512a5e34490SHuw Davies }
513a5e34490SHuw Davies 
514e1ce69dfSHuw Davies /**
515e1ce69dfSHuw Davies  * calipso_doi_walk - Iterate through the DOI definitions
516e1ce69dfSHuw Davies  * @skip_cnt: skip past this number of DOI definitions, updated
517e1ce69dfSHuw Davies  * @callback: callback for each DOI definition
518e1ce69dfSHuw Davies  * @cb_arg: argument for the callback function
519e1ce69dfSHuw Davies  *
520e1ce69dfSHuw Davies  * Description:
521e1ce69dfSHuw Davies  * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
522e1ce69dfSHuw Davies  * For each entry call @callback, if @callback returns a negative value stop
523e1ce69dfSHuw Davies  * 'walking' through the list and return.  Updates the value in @skip_cnt upon
524e1ce69dfSHuw Davies  * return.  Returns zero on success, negative values on failure.
525e1ce69dfSHuw Davies  *
526e1ce69dfSHuw Davies  */
calipso_doi_walk(u32 * skip_cnt,int (* callback)(struct calipso_doi * doi_def,void * arg),void * cb_arg)527e1ce69dfSHuw Davies static int calipso_doi_walk(u32 *skip_cnt,
528e1ce69dfSHuw Davies 			    int (*callback)(struct calipso_doi *doi_def,
529e1ce69dfSHuw Davies 					    void *arg),
530e1ce69dfSHuw Davies 			    void *cb_arg)
531e1ce69dfSHuw Davies {
532e1ce69dfSHuw Davies 	int ret_val = -ENOENT;
533e1ce69dfSHuw Davies 	u32 doi_cnt = 0;
534e1ce69dfSHuw Davies 	struct calipso_doi *iter_doi;
535e1ce69dfSHuw Davies 
536e1ce69dfSHuw Davies 	rcu_read_lock();
537e1ce69dfSHuw Davies 	list_for_each_entry_rcu(iter_doi, &calipso_doi_list, list)
538edcd9270SReshetova, Elena 		if (refcount_read(&iter_doi->refcount) > 0) {
539e1ce69dfSHuw Davies 			if (doi_cnt++ < *skip_cnt)
540e1ce69dfSHuw Davies 				continue;
541e1ce69dfSHuw Davies 			ret_val = callback(iter_doi, cb_arg);
542e1ce69dfSHuw Davies 			if (ret_val < 0) {
543e1ce69dfSHuw Davies 				doi_cnt--;
544e1ce69dfSHuw Davies 				goto doi_walk_return;
545e1ce69dfSHuw Davies 			}
546e1ce69dfSHuw Davies 		}
547e1ce69dfSHuw Davies 
548e1ce69dfSHuw Davies doi_walk_return:
549e1ce69dfSHuw Davies 	rcu_read_unlock();
550e1ce69dfSHuw Davies 	*skip_cnt = doi_cnt;
551e1ce69dfSHuw Davies 	return ret_val;
552e1ce69dfSHuw Davies }
553e1ce69dfSHuw Davies 
554ceba1832SHuw Davies /**
5552e532b70SHuw Davies  * calipso_validate - Validate a CALIPSO option
5562e532b70SHuw Davies  * @skb: the packet
5572e532b70SHuw Davies  * @option: the start of the option
5582e532b70SHuw Davies  *
5592e532b70SHuw Davies  * Description:
5602e532b70SHuw Davies  * This routine is called to validate a CALIPSO option.
5612e532b70SHuw Davies  * If the option is valid then %true is returned, otherwise
5622e532b70SHuw Davies  * %false is returned.
5632e532b70SHuw Davies  *
5642e532b70SHuw Davies  * The caller should have already checked that the length of the
5652e532b70SHuw Davies  * option (including the TLV header) is >= 10 and that the catmap
5662e532b70SHuw Davies  * length is consistent with the option length.
5672e532b70SHuw Davies  *
5682e532b70SHuw Davies  * We leave checks on the level and categories to the socket layer.
5692e532b70SHuw Davies  */
calipso_validate(const struct sk_buff * skb,const unsigned char * option)5702e532b70SHuw Davies bool calipso_validate(const struct sk_buff *skb, const unsigned char *option)
5712e532b70SHuw Davies {
5722e532b70SHuw Davies 	struct calipso_doi *doi_def;
5732e532b70SHuw Davies 	bool ret_val;
5742e532b70SHuw Davies 	u16 crc, len = option[1] + 2;
5752e532b70SHuw Davies 	static const u8 zero[2];
5762e532b70SHuw Davies 
5772e532b70SHuw Davies 	/* The original CRC runs over the option including the TLV header
5782e532b70SHuw Davies 	 * with the CRC-16 field (at offset 8) zeroed out. */
5792e532b70SHuw Davies 	crc = crc_ccitt(0xffff, option, 8);
5802e532b70SHuw Davies 	crc = crc_ccitt(crc, zero, sizeof(zero));
5812e532b70SHuw Davies 	if (len > 10)
5822e532b70SHuw Davies 		crc = crc_ccitt(crc, option + 10, len - 10);
5832e532b70SHuw Davies 	crc = ~crc;
5842e532b70SHuw Davies 	if (option[8] != (crc & 0xff) || option[9] != ((crc >> 8) & 0xff))
5852e532b70SHuw Davies 		return false;
5862e532b70SHuw Davies 
5872e532b70SHuw Davies 	rcu_read_lock();
5882e532b70SHuw Davies 	doi_def = calipso_doi_search(get_unaligned_be32(option + 2));
5892e532b70SHuw Davies 	ret_val = !!doi_def;
5902e532b70SHuw Davies 	rcu_read_unlock();
5912e532b70SHuw Davies 
5922e532b70SHuw Davies 	return ret_val;
5932e532b70SHuw Davies }
5942e532b70SHuw Davies 
5952e532b70SHuw Davies /**
596ceba1832SHuw Davies  * calipso_map_cat_hton - Perform a category mapping from host to network
597ceba1832SHuw Davies  * @doi_def: the DOI definition
598ceba1832SHuw Davies  * @secattr: the security attributes
599ceba1832SHuw Davies  * @net_cat: the zero'd out category bitmap in network/CALIPSO format
600ceba1832SHuw Davies  * @net_cat_len: the length of the CALIPSO bitmap in bytes
601ceba1832SHuw Davies  *
602ceba1832SHuw Davies  * Description:
603ceba1832SHuw Davies  * Perform a label mapping to translate a local MLS category bitmap to the
604ceba1832SHuw Davies  * correct CALIPSO bitmap using the given DOI definition.  Returns the minimum
605ceba1832SHuw Davies  * size in bytes of the network bitmap on success, negative values otherwise.
606ceba1832SHuw Davies  *
607ceba1832SHuw Davies  */
calipso_map_cat_hton(const struct calipso_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * net_cat,u32 net_cat_len)608ceba1832SHuw Davies static int calipso_map_cat_hton(const struct calipso_doi *doi_def,
609ceba1832SHuw Davies 				const struct netlbl_lsm_secattr *secattr,
610ceba1832SHuw Davies 				unsigned char *net_cat,
611ceba1832SHuw Davies 				u32 net_cat_len)
612ceba1832SHuw Davies {
613ceba1832SHuw Davies 	int spot = -1;
614ceba1832SHuw Davies 	u32 net_spot_max = 0;
615ceba1832SHuw Davies 	u32 net_clen_bits = net_cat_len * 8;
616ceba1832SHuw Davies 
617ceba1832SHuw Davies 	for (;;) {
618ceba1832SHuw Davies 		spot = netlbl_catmap_walk(secattr->attr.mls.cat,
619ceba1832SHuw Davies 					  spot + 1);
620ceba1832SHuw Davies 		if (spot < 0)
621ceba1832SHuw Davies 			break;
622ceba1832SHuw Davies 		if (spot >= net_clen_bits)
623ceba1832SHuw Davies 			return -ENOSPC;
624ceba1832SHuw Davies 		netlbl_bitmap_setbit(net_cat, spot, 1);
625ceba1832SHuw Davies 
626ceba1832SHuw Davies 		if (spot > net_spot_max)
627ceba1832SHuw Davies 			net_spot_max = spot;
628ceba1832SHuw Davies 	}
629ceba1832SHuw Davies 
630ceba1832SHuw Davies 	return (net_spot_max / 32 + 1) * 4;
631ceba1832SHuw Davies }
632ceba1832SHuw Davies 
633ceba1832SHuw Davies /**
634ceba1832SHuw Davies  * calipso_map_cat_ntoh - Perform a category mapping from network to host
635ceba1832SHuw Davies  * @doi_def: the DOI definition
636ceba1832SHuw Davies  * @net_cat: the category bitmap in network/CALIPSO format
637ceba1832SHuw Davies  * @net_cat_len: the length of the CALIPSO bitmap in bytes
638ceba1832SHuw Davies  * @secattr: the security attributes
639ceba1832SHuw Davies  *
640ceba1832SHuw Davies  * Description:
641ceba1832SHuw Davies  * Perform a label mapping to translate a CALIPSO bitmap to the correct local
642ceba1832SHuw Davies  * MLS category bitmap using the given DOI definition.  Returns zero on
643ceba1832SHuw Davies  * success, negative values on failure.
644ceba1832SHuw Davies  *
645ceba1832SHuw Davies  */
calipso_map_cat_ntoh(const struct calipso_doi * doi_def,const unsigned char * net_cat,u32 net_cat_len,struct netlbl_lsm_secattr * secattr)646ceba1832SHuw Davies static int calipso_map_cat_ntoh(const struct calipso_doi *doi_def,
647ceba1832SHuw Davies 				const unsigned char *net_cat,
648ceba1832SHuw Davies 				u32 net_cat_len,
649ceba1832SHuw Davies 				struct netlbl_lsm_secattr *secattr)
650ceba1832SHuw Davies {
651ceba1832SHuw Davies 	int ret_val;
652ceba1832SHuw Davies 	int spot = -1;
653ceba1832SHuw Davies 	u32 net_clen_bits = net_cat_len * 8;
654ceba1832SHuw Davies 
655ceba1832SHuw Davies 	for (;;) {
656ceba1832SHuw Davies 		spot = netlbl_bitmap_walk(net_cat,
657ceba1832SHuw Davies 					  net_clen_bits,
658ceba1832SHuw Davies 					  spot + 1,
659ceba1832SHuw Davies 					  1);
660ceba1832SHuw Davies 		if (spot < 0) {
661ceba1832SHuw Davies 			if (spot == -2)
662ceba1832SHuw Davies 				return -EFAULT;
663ceba1832SHuw Davies 			return 0;
664ceba1832SHuw Davies 		}
665ceba1832SHuw Davies 
666ceba1832SHuw Davies 		ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
667ceba1832SHuw Davies 					       spot,
668ceba1832SHuw Davies 					       GFP_ATOMIC);
669ceba1832SHuw Davies 		if (ret_val != 0)
670ceba1832SHuw Davies 			return ret_val;
671ceba1832SHuw Davies 	}
672ceba1832SHuw Davies 
673ceba1832SHuw Davies 	return -EINVAL;
674ceba1832SHuw Davies }
675ceba1832SHuw Davies 
676ceba1832SHuw Davies /**
677ceba1832SHuw Davies  * calipso_pad_write - Writes pad bytes in TLV format
678ceba1832SHuw Davies  * @buf: the buffer
679ceba1832SHuw Davies  * @offset: offset from start of buffer to write padding
680ceba1832SHuw Davies  * @count: number of pad bytes to write
681ceba1832SHuw Davies  *
682ceba1832SHuw Davies  * Description:
683ceba1832SHuw Davies  * Write @count bytes of TLV padding into @buffer starting at offset @offset.
684ceba1832SHuw Davies  * @count should be less than 8 - see RFC 4942.
685ceba1832SHuw Davies  *
686ceba1832SHuw Davies  */
calipso_pad_write(unsigned char * buf,unsigned int offset,unsigned int count)687ceba1832SHuw Davies static int calipso_pad_write(unsigned char *buf, unsigned int offset,
688ceba1832SHuw Davies 			     unsigned int count)
689ceba1832SHuw Davies {
690ceba1832SHuw Davies 	if (WARN_ON_ONCE(count >= 8))
691ceba1832SHuw Davies 		return -EINVAL;
692ceba1832SHuw Davies 
693ceba1832SHuw Davies 	switch (count) {
694ceba1832SHuw Davies 	case 0:
695ceba1832SHuw Davies 		break;
696ceba1832SHuw Davies 	case 1:
697ceba1832SHuw Davies 		buf[offset] = IPV6_TLV_PAD1;
698ceba1832SHuw Davies 		break;
699ceba1832SHuw Davies 	default:
700ceba1832SHuw Davies 		buf[offset] = IPV6_TLV_PADN;
701ceba1832SHuw Davies 		buf[offset + 1] = count - 2;
702ceba1832SHuw Davies 		if (count > 2)
703ceba1832SHuw Davies 			memset(buf + offset + 2, 0, count - 2);
704ceba1832SHuw Davies 		break;
705ceba1832SHuw Davies 	}
706ceba1832SHuw Davies 	return 0;
707ceba1832SHuw Davies }
708ceba1832SHuw Davies 
709ceba1832SHuw Davies /**
710ceba1832SHuw Davies  * calipso_genopt - Generate a CALIPSO option
711ceba1832SHuw Davies  * @buf: the option buffer
712ceba1832SHuw Davies  * @start: offset from which to write
713ceba1832SHuw Davies  * @buf_len: the size of opt_buf
714ceba1832SHuw Davies  * @doi_def: the CALIPSO DOI to use
715ceba1832SHuw Davies  * @secattr: the security attributes
716ceba1832SHuw Davies  *
717ceba1832SHuw Davies  * Description:
718ceba1832SHuw Davies  * Generate a CALIPSO option using the DOI definition and security attributes
719ceba1832SHuw Davies  * passed to the function. This also generates upto three bytes of leading
720ceba1832SHuw Davies  * padding that ensures that the option is 4n + 2 aligned.  It returns the
721ceba1832SHuw Davies  * number of bytes written (including any initial padding).
722ceba1832SHuw Davies  */
calipso_genopt(unsigned char * buf,u32 start,u32 buf_len,const struct calipso_doi * doi_def,const struct netlbl_lsm_secattr * secattr)723ceba1832SHuw Davies static int calipso_genopt(unsigned char *buf, u32 start, u32 buf_len,
724ceba1832SHuw Davies 			  const struct calipso_doi *doi_def,
725ceba1832SHuw Davies 			  const struct netlbl_lsm_secattr *secattr)
726ceba1832SHuw Davies {
727ceba1832SHuw Davies 	int ret_val;
728ceba1832SHuw Davies 	u32 len, pad;
729ceba1832SHuw Davies 	u16 crc;
730ceba1832SHuw Davies 	static const unsigned char padding[4] = {2, 1, 0, 3};
731ceba1832SHuw Davies 	unsigned char *calipso;
732ceba1832SHuw Davies 
733ceba1832SHuw Davies 	/* CALIPSO has 4n + 2 alignment */
734ceba1832SHuw Davies 	pad = padding[start & 3];
735ceba1832SHuw Davies 	if (buf_len <= start + pad + CALIPSO_HDR_LEN)
736ceba1832SHuw Davies 		return -ENOSPC;
737ceba1832SHuw Davies 
738ceba1832SHuw Davies 	if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
739ceba1832SHuw Davies 		return -EPERM;
740ceba1832SHuw Davies 
741ceba1832SHuw Davies 	len = CALIPSO_HDR_LEN;
742ceba1832SHuw Davies 
743ceba1832SHuw Davies 	if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
744ceba1832SHuw Davies 		ret_val = calipso_map_cat_hton(doi_def,
745ceba1832SHuw Davies 					       secattr,
746ceba1832SHuw Davies 					       buf + start + pad + len,
747ceba1832SHuw Davies 					       buf_len - start - pad - len);
748ceba1832SHuw Davies 		if (ret_val < 0)
749ceba1832SHuw Davies 			return ret_val;
750ceba1832SHuw Davies 		len += ret_val;
751ceba1832SHuw Davies 	}
752ceba1832SHuw Davies 
753ceba1832SHuw Davies 	calipso_pad_write(buf, start, pad);
754ceba1832SHuw Davies 	calipso = buf + start + pad;
755ceba1832SHuw Davies 
756ceba1832SHuw Davies 	calipso[0] = IPV6_TLV_CALIPSO;
757ceba1832SHuw Davies 	calipso[1] = len - 2;
758ceba1832SHuw Davies 	*(__be32 *)(calipso + 2) = htonl(doi_def->doi);
759ceba1832SHuw Davies 	calipso[6] = (len - CALIPSO_HDR_LEN) / 4;
7606159e963SJulia Lawall 	calipso[7] = secattr->attr.mls.lvl;
761ceba1832SHuw Davies 	crc = ~crc_ccitt(0xffff, calipso, len);
762ceba1832SHuw Davies 	calipso[8] = crc & 0xff;
763ceba1832SHuw Davies 	calipso[9] = (crc >> 8) & 0xff;
764ceba1832SHuw Davies 	return pad + len;
765ceba1832SHuw Davies }
766ceba1832SHuw Davies 
767ceba1832SHuw Davies /* Hop-by-hop hdr helper functions
768ceba1832SHuw Davies  */
769ceba1832SHuw Davies 
770ceba1832SHuw Davies /**
771ceba1832SHuw Davies  * calipso_opt_update - Replaces socket's hop options with a new set
772ceba1832SHuw Davies  * @sk: the socket
773ceba1832SHuw Davies  * @hop: new hop options
774ceba1832SHuw Davies  *
775ceba1832SHuw Davies  * Description:
776ceba1832SHuw Davies  * Replaces @sk's hop options with @hop.  @hop may be NULL to leave
777ceba1832SHuw Davies  * the socket with no hop options.
778ceba1832SHuw Davies  *
779ceba1832SHuw Davies  */
calipso_opt_update(struct sock * sk,struct ipv6_opt_hdr * hop)780ceba1832SHuw Davies static int calipso_opt_update(struct sock *sk, struct ipv6_opt_hdr *hop)
781ceba1832SHuw Davies {
782ceba1832SHuw Davies 	struct ipv6_txoptions *old = txopt_get(inet6_sk(sk)), *txopts;
783ceba1832SHuw Davies 
784a9ba23d4SPaul Moore 	txopts = ipv6_renew_options(sk, old, IPV6_HOPOPTS, hop);
785ceba1832SHuw Davies 	txopt_put(old);
786ceba1832SHuw Davies 	if (IS_ERR(txopts))
787ceba1832SHuw Davies 		return PTR_ERR(txopts);
788ceba1832SHuw Davies 
789ceba1832SHuw Davies 	txopts = ipv6_update_options(sk, txopts);
790ceba1832SHuw Davies 	if (txopts) {
791ceba1832SHuw Davies 		atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
792ceba1832SHuw Davies 		txopt_put(txopts);
793ceba1832SHuw Davies 	}
794ceba1832SHuw Davies 
795ceba1832SHuw Davies 	return 0;
796ceba1832SHuw Davies }
797ceba1832SHuw Davies 
798ceba1832SHuw Davies /**
799ceba1832SHuw Davies  * calipso_tlv_len - Returns the length of the TLV
800ceba1832SHuw Davies  * @opt: the option header
801ceba1832SHuw Davies  * @offset: offset of the TLV within the header
802ceba1832SHuw Davies  *
803ceba1832SHuw Davies  * Description:
804ceba1832SHuw Davies  * Returns the length of the TLV option at offset @offset within
805ceba1832SHuw Davies  * the option header @opt.  Checks that the entire TLV fits inside
806ceba1832SHuw Davies  * the option header, returns a negative value if this is not the case.
807ceba1832SHuw Davies  */
calipso_tlv_len(struct ipv6_opt_hdr * opt,unsigned int offset)808ceba1832SHuw Davies static int calipso_tlv_len(struct ipv6_opt_hdr *opt, unsigned int offset)
809ceba1832SHuw Davies {
810ceba1832SHuw Davies 	unsigned char *tlv = (unsigned char *)opt;
811ceba1832SHuw Davies 	unsigned int opt_len = ipv6_optlen(opt), tlv_len;
812ceba1832SHuw Davies 
813ceba1832SHuw Davies 	if (offset < sizeof(*opt) || offset >= opt_len)
814ceba1832SHuw Davies 		return -EINVAL;
815ceba1832SHuw Davies 	if (tlv[offset] == IPV6_TLV_PAD1)
816ceba1832SHuw Davies 		return 1;
817ceba1832SHuw Davies 	if (offset + 1 >= opt_len)
818ceba1832SHuw Davies 		return -EINVAL;
819ceba1832SHuw Davies 	tlv_len = tlv[offset + 1] + 2;
820ceba1832SHuw Davies 	if (offset + tlv_len > opt_len)
821ceba1832SHuw Davies 		return -EINVAL;
822ceba1832SHuw Davies 	return tlv_len;
823ceba1832SHuw Davies }
824ceba1832SHuw Davies 
825ceba1832SHuw Davies /**
826ceba1832SHuw Davies  * calipso_opt_find - Finds the CALIPSO option in an IPv6 hop options header
827ceba1832SHuw Davies  * @hop: the hop options header
828ceba1832SHuw Davies  * @start: on return holds the offset of any leading padding
829ceba1832SHuw Davies  * @end: on return holds the offset of the first non-pad TLV after CALIPSO
830ceba1832SHuw Davies  *
831ceba1832SHuw Davies  * Description:
832ceba1832SHuw Davies  * Finds the space occupied by a CALIPSO option (including any leading and
833ceba1832SHuw Davies  * trailing padding).
834ceba1832SHuw Davies  *
835ceba1832SHuw Davies  * If a CALIPSO option exists set @start and @end to the
836ceba1832SHuw Davies  * offsets within @hop of the start of padding before the first
837ceba1832SHuw Davies  * CALIPSO option and the end of padding after the first CALIPSO
838ceba1832SHuw Davies  * option.  In this case the function returns 0.
839ceba1832SHuw Davies  *
840ceba1832SHuw Davies  * In the absence of a CALIPSO option, @start and @end will be
841ceba1832SHuw Davies  * set to the start and end of any trailing padding in the header.
842ceba1832SHuw Davies  * This is useful when appending a new option, as the caller may want
843ceba1832SHuw Davies  * to overwrite some of this padding.  In this case the function will
844ceba1832SHuw Davies  * return -ENOENT.
845ceba1832SHuw Davies  */
calipso_opt_find(struct ipv6_opt_hdr * hop,unsigned int * start,unsigned int * end)846ceba1832SHuw Davies static int calipso_opt_find(struct ipv6_opt_hdr *hop, unsigned int *start,
847ceba1832SHuw Davies 			    unsigned int *end)
848ceba1832SHuw Davies {
849ceba1832SHuw Davies 	int ret_val = -ENOENT, tlv_len;
850ceba1832SHuw Davies 	unsigned int opt_len, offset, offset_s = 0, offset_e = 0;
851ceba1832SHuw Davies 	unsigned char *opt = (unsigned char *)hop;
852ceba1832SHuw Davies 
853ceba1832SHuw Davies 	opt_len = ipv6_optlen(hop);
854ceba1832SHuw Davies 	offset = sizeof(*hop);
855ceba1832SHuw Davies 
856ceba1832SHuw Davies 	while (offset < opt_len) {
857ceba1832SHuw Davies 		tlv_len = calipso_tlv_len(hop, offset);
858ceba1832SHuw Davies 		if (tlv_len < 0)
859ceba1832SHuw Davies 			return tlv_len;
860ceba1832SHuw Davies 
861ceba1832SHuw Davies 		switch (opt[offset]) {
862ceba1832SHuw Davies 		case IPV6_TLV_PAD1:
863ceba1832SHuw Davies 		case IPV6_TLV_PADN:
864ceba1832SHuw Davies 			if (offset_e)
865ceba1832SHuw Davies 				offset_e = offset;
866ceba1832SHuw Davies 			break;
867ceba1832SHuw Davies 		case IPV6_TLV_CALIPSO:
868ceba1832SHuw Davies 			ret_val = 0;
869ceba1832SHuw Davies 			offset_e = offset;
870ceba1832SHuw Davies 			break;
871ceba1832SHuw Davies 		default:
872ceba1832SHuw Davies 			if (offset_e == 0)
873ceba1832SHuw Davies 				offset_s = offset;
874ceba1832SHuw Davies 			else
875ceba1832SHuw Davies 				goto out;
876ceba1832SHuw Davies 		}
877ceba1832SHuw Davies 		offset += tlv_len;
878ceba1832SHuw Davies 	}
879ceba1832SHuw Davies 
880ceba1832SHuw Davies out:
881ceba1832SHuw Davies 	if (offset_s)
882ceba1832SHuw Davies 		*start = offset_s + calipso_tlv_len(hop, offset_s);
883ceba1832SHuw Davies 	else
884ceba1832SHuw Davies 		*start = sizeof(*hop);
885ceba1832SHuw Davies 	if (offset_e)
886ceba1832SHuw Davies 		*end = offset_e + calipso_tlv_len(hop, offset_e);
887ceba1832SHuw Davies 	else
888ceba1832SHuw Davies 		*end = opt_len;
889ceba1832SHuw Davies 
890ceba1832SHuw Davies 	return ret_val;
891ceba1832SHuw Davies }
892ceba1832SHuw Davies 
893ceba1832SHuw Davies /**
894ceba1832SHuw Davies  * calipso_opt_insert - Inserts a CALIPSO option into an IPv6 hop opt hdr
895ceba1832SHuw Davies  * @hop: the original hop options header
896ceba1832SHuw Davies  * @doi_def: the CALIPSO DOI to use
897ceba1832SHuw Davies  * @secattr: the specific security attributes of the socket
898ceba1832SHuw Davies  *
899ceba1832SHuw Davies  * Description:
900ceba1832SHuw Davies  * Creates a new hop options header based on @hop with a
901ceba1832SHuw Davies  * CALIPSO option added to it.  If @hop already contains a CALIPSO
902ceba1832SHuw Davies  * option this is overwritten, otherwise the new option is appended
903ceba1832SHuw Davies  * after any existing options.  If @hop is NULL then the new header
904ceba1832SHuw Davies  * will contain just the CALIPSO option and any needed padding.
905ceba1832SHuw Davies  *
906ceba1832SHuw Davies  */
907ceba1832SHuw Davies static struct ipv6_opt_hdr *
calipso_opt_insert(struct ipv6_opt_hdr * hop,const struct calipso_doi * doi_def,const struct netlbl_lsm_secattr * secattr)908ceba1832SHuw Davies calipso_opt_insert(struct ipv6_opt_hdr *hop,
909ceba1832SHuw Davies 		   const struct calipso_doi *doi_def,
910ceba1832SHuw Davies 		   const struct netlbl_lsm_secattr *secattr)
911ceba1832SHuw Davies {
912ceba1832SHuw Davies 	unsigned int start, end, buf_len, pad, hop_len;
913ceba1832SHuw Davies 	struct ipv6_opt_hdr *new;
914ceba1832SHuw Davies 	int ret_val;
915ceba1832SHuw Davies 
916ceba1832SHuw Davies 	if (hop) {
917ceba1832SHuw Davies 		hop_len = ipv6_optlen(hop);
918ceba1832SHuw Davies 		ret_val = calipso_opt_find(hop, &start, &end);
919ceba1832SHuw Davies 		if (ret_val && ret_val != -ENOENT)
920ceba1832SHuw Davies 			return ERR_PTR(ret_val);
921ceba1832SHuw Davies 	} else {
922ceba1832SHuw Davies 		hop_len = 0;
923ceba1832SHuw Davies 		start = sizeof(*hop);
924ceba1832SHuw Davies 		end = 0;
925ceba1832SHuw Davies 	}
926ceba1832SHuw Davies 
927ceba1832SHuw Davies 	buf_len = hop_len + start - end + CALIPSO_OPT_LEN_MAX_WITH_PAD;
928ceba1832SHuw Davies 	new = kzalloc(buf_len, GFP_ATOMIC);
929ceba1832SHuw Davies 	if (!new)
930ceba1832SHuw Davies 		return ERR_PTR(-ENOMEM);
931ceba1832SHuw Davies 
932ceba1832SHuw Davies 	if (start > sizeof(*hop))
933ceba1832SHuw Davies 		memcpy(new, hop, start);
934ceba1832SHuw Davies 	ret_val = calipso_genopt((unsigned char *)new, start, buf_len, doi_def,
935ceba1832SHuw Davies 				 secattr);
936b4c0e0c6SColin Ian King 	if (ret_val < 0) {
937b4c0e0c6SColin Ian King 		kfree(new);
938ceba1832SHuw Davies 		return ERR_PTR(ret_val);
939b4c0e0c6SColin Ian King 	}
940ceba1832SHuw Davies 
941ceba1832SHuw Davies 	buf_len = start + ret_val;
942ceba1832SHuw Davies 	/* At this point buf_len aligns to 4n, so (buf_len & 4) pads to 8n */
943ceba1832SHuw Davies 	pad = ((buf_len & 4) + (end & 7)) & 7;
944ceba1832SHuw Davies 	calipso_pad_write((unsigned char *)new, buf_len, pad);
945ceba1832SHuw Davies 	buf_len += pad;
946ceba1832SHuw Davies 
947ceba1832SHuw Davies 	if (end != hop_len) {
948ceba1832SHuw Davies 		memcpy((char *)new + buf_len, (char *)hop + end, hop_len - end);
949ceba1832SHuw Davies 		buf_len += hop_len - end;
950ceba1832SHuw Davies 	}
951ceba1832SHuw Davies 	new->nexthdr = 0;
952ceba1832SHuw Davies 	new->hdrlen = buf_len / 8 - 1;
953ceba1832SHuw Davies 
954ceba1832SHuw Davies 	return new;
955ceba1832SHuw Davies }
956ceba1832SHuw Davies 
957ceba1832SHuw Davies /**
958ceba1832SHuw Davies  * calipso_opt_del - Removes the CALIPSO option from an option header
959ceba1832SHuw Davies  * @hop: the original header
960ceba1832SHuw Davies  * @new: the new header
961ceba1832SHuw Davies  *
962ceba1832SHuw Davies  * Description:
963ceba1832SHuw Davies  * Creates a new header based on @hop without any CALIPSO option.  If @hop
964ceba1832SHuw Davies  * doesn't contain a CALIPSO option it returns -ENOENT.  If @hop contains
965ceba1832SHuw Davies  * no other non-padding options, it returns zero with @new set to NULL.
966ceba1832SHuw Davies  * Otherwise it returns zero, creates a new header without the CALIPSO
967ceba1832SHuw Davies  * option (and removing as much padding as possible) and returns with
968ceba1832SHuw Davies  * @new set to that header.
969ceba1832SHuw Davies  *
970ceba1832SHuw Davies  */
calipso_opt_del(struct ipv6_opt_hdr * hop,struct ipv6_opt_hdr ** new)971ceba1832SHuw Davies static int calipso_opt_del(struct ipv6_opt_hdr *hop,
972ceba1832SHuw Davies 			   struct ipv6_opt_hdr **new)
973ceba1832SHuw Davies {
974ceba1832SHuw Davies 	int ret_val;
975ceba1832SHuw Davies 	unsigned int start, end, delta, pad, hop_len;
976ceba1832SHuw Davies 
977ceba1832SHuw Davies 	ret_val = calipso_opt_find(hop, &start, &end);
978ceba1832SHuw Davies 	if (ret_val)
979ceba1832SHuw Davies 		return ret_val;
980ceba1832SHuw Davies 
981ceba1832SHuw Davies 	hop_len = ipv6_optlen(hop);
982ceba1832SHuw Davies 	if (start == sizeof(*hop) && end == hop_len) {
983ceba1832SHuw Davies 		/* There's no other option in the header so return NULL */
984ceba1832SHuw Davies 		*new = NULL;
985ceba1832SHuw Davies 		return 0;
986ceba1832SHuw Davies 	}
987ceba1832SHuw Davies 
988ceba1832SHuw Davies 	delta = (end - start) & ~7;
989ceba1832SHuw Davies 	*new = kzalloc(hop_len - delta, GFP_ATOMIC);
990ceba1832SHuw Davies 	if (!*new)
991ceba1832SHuw Davies 		return -ENOMEM;
992ceba1832SHuw Davies 
993ceba1832SHuw Davies 	memcpy(*new, hop, start);
994ceba1832SHuw Davies 	(*new)->hdrlen -= delta / 8;
995ceba1832SHuw Davies 	pad = (end - start) & 7;
996ceba1832SHuw Davies 	calipso_pad_write((unsigned char *)*new, start, pad);
997ceba1832SHuw Davies 	if (end != hop_len)
998ceba1832SHuw Davies 		memcpy((char *)*new + start + pad, (char *)hop + end,
999ceba1832SHuw Davies 		       hop_len - end);
1000ceba1832SHuw Davies 
1001ceba1832SHuw Davies 	return 0;
1002ceba1832SHuw Davies }
1003ceba1832SHuw Davies 
1004ceba1832SHuw Davies /**
1005ceba1832SHuw Davies  * calipso_opt_getattr - Get the security attributes from a memory block
1006ceba1832SHuw Davies  * @calipso: the CALIPSO option
1007ceba1832SHuw Davies  * @secattr: the security attributes
1008ceba1832SHuw Davies  *
1009ceba1832SHuw Davies  * Description:
1010ceba1832SHuw Davies  * Inspect @calipso and return the security attributes in @secattr.
1011ceba1832SHuw Davies  * Returns zero on success and negative values on failure.
1012ceba1832SHuw Davies  *
1013ceba1832SHuw Davies  */
calipso_opt_getattr(const unsigned char * calipso,struct netlbl_lsm_secattr * secattr)1014ceba1832SHuw Davies static int calipso_opt_getattr(const unsigned char *calipso,
1015ceba1832SHuw Davies 			       struct netlbl_lsm_secattr *secattr)
1016ceba1832SHuw Davies {
1017ceba1832SHuw Davies 	int ret_val = -ENOMSG;
1018ceba1832SHuw Davies 	u32 doi, len = calipso[1], cat_len = calipso[6] * 4;
1019ceba1832SHuw Davies 	struct calipso_doi *doi_def;
1020ceba1832SHuw Davies 
1021ceba1832SHuw Davies 	if (cat_len + 8 > len)
1022ceba1832SHuw Davies 		return -EINVAL;
1023ceba1832SHuw Davies 
10244fee5242SHuw Davies 	if (calipso_cache_check(calipso + 2, calipso[1], secattr) == 0)
10254fee5242SHuw Davies 		return 0;
10264fee5242SHuw Davies 
1027ceba1832SHuw Davies 	doi = get_unaligned_be32(calipso + 2);
1028ceba1832SHuw Davies 	rcu_read_lock();
1029ceba1832SHuw Davies 	doi_def = calipso_doi_search(doi);
1030ceba1832SHuw Davies 	if (!doi_def)
1031ceba1832SHuw Davies 		goto getattr_return;
1032ceba1832SHuw Davies 
1033ceba1832SHuw Davies 	secattr->attr.mls.lvl = calipso[7];
1034ceba1832SHuw Davies 	secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1035ceba1832SHuw Davies 
1036ceba1832SHuw Davies 	if (cat_len) {
1037ceba1832SHuw Davies 		ret_val = calipso_map_cat_ntoh(doi_def,
1038ceba1832SHuw Davies 					       calipso + 10,
1039ceba1832SHuw Davies 					       cat_len,
1040ceba1832SHuw Davies 					       secattr);
1041ceba1832SHuw Davies 		if (ret_val != 0) {
1042ceba1832SHuw Davies 			netlbl_catmap_free(secattr->attr.mls.cat);
1043ceba1832SHuw Davies 			goto getattr_return;
1044ceba1832SHuw Davies 		}
1045ceba1832SHuw Davies 
1046eead1c2eSPaolo Abeni 		if (secattr->attr.mls.cat)
1047ceba1832SHuw Davies 			secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1048ceba1832SHuw Davies 	}
1049ceba1832SHuw Davies 
1050ceba1832SHuw Davies 	secattr->type = NETLBL_NLTYPE_CALIPSO;
1051ceba1832SHuw Davies 
1052ceba1832SHuw Davies getattr_return:
1053ceba1832SHuw Davies 	rcu_read_unlock();
1054ceba1832SHuw Davies 	return ret_val;
1055ceba1832SHuw Davies }
1056ceba1832SHuw Davies 
1057ceba1832SHuw Davies /* sock functions.
1058ceba1832SHuw Davies  */
1059ceba1832SHuw Davies 
1060ceba1832SHuw Davies /**
1061ceba1832SHuw Davies  * calipso_sock_getattr - Get the security attributes from a sock
1062ceba1832SHuw Davies  * @sk: the sock
1063ceba1832SHuw Davies  * @secattr: the security attributes
1064ceba1832SHuw Davies  *
1065ceba1832SHuw Davies  * Description:
1066ceba1832SHuw Davies  * Query @sk to see if there is a CALIPSO option attached to the sock and if
1067ceba1832SHuw Davies  * there is return the CALIPSO security attributes in @secattr.  This function
1068ceba1832SHuw Davies  * requires that @sk be locked, or privately held, but it does not do any
1069ceba1832SHuw Davies  * locking itself.  Returns zero on success and negative values on failure.
1070ceba1832SHuw Davies  *
1071ceba1832SHuw Davies  */
calipso_sock_getattr(struct sock * sk,struct netlbl_lsm_secattr * secattr)1072ceba1832SHuw Davies static int calipso_sock_getattr(struct sock *sk,
1073ceba1832SHuw Davies 				struct netlbl_lsm_secattr *secattr)
1074ceba1832SHuw Davies {
1075ceba1832SHuw Davies 	struct ipv6_opt_hdr *hop;
1076ceba1832SHuw Davies 	int opt_len, len, ret_val = -ENOMSG, offset;
1077ceba1832SHuw Davies 	unsigned char *opt;
1078ceba1832SHuw Davies 	struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
1079ceba1832SHuw Davies 
1080ceba1832SHuw Davies 	if (!txopts || !txopts->hopopt)
1081ceba1832SHuw Davies 		goto done;
1082ceba1832SHuw Davies 
1083ceba1832SHuw Davies 	hop = txopts->hopopt;
1084ceba1832SHuw Davies 	opt = (unsigned char *)hop;
1085ceba1832SHuw Davies 	opt_len = ipv6_optlen(hop);
1086ceba1832SHuw Davies 	offset = sizeof(*hop);
1087ceba1832SHuw Davies 	while (offset < opt_len) {
1088ceba1832SHuw Davies 		len = calipso_tlv_len(hop, offset);
1089ceba1832SHuw Davies 		if (len < 0) {
1090ceba1832SHuw Davies 			ret_val = len;
1091ceba1832SHuw Davies 			goto done;
1092ceba1832SHuw Davies 		}
1093ceba1832SHuw Davies 		switch (opt[offset]) {
1094ceba1832SHuw Davies 		case IPV6_TLV_CALIPSO:
1095ceba1832SHuw Davies 			if (len < CALIPSO_HDR_LEN)
1096ceba1832SHuw Davies 				ret_val = -EINVAL;
1097ceba1832SHuw Davies 			else
1098ceba1832SHuw Davies 				ret_val = calipso_opt_getattr(&opt[offset],
1099ceba1832SHuw Davies 							      secattr);
1100ceba1832SHuw Davies 			goto done;
1101ceba1832SHuw Davies 		default:
1102ceba1832SHuw Davies 			offset += len;
1103ceba1832SHuw Davies 			break;
1104ceba1832SHuw Davies 		}
1105ceba1832SHuw Davies 	}
1106ceba1832SHuw Davies done:
1107ceba1832SHuw Davies 	txopt_put(txopts);
1108ceba1832SHuw Davies 	return ret_val;
1109ceba1832SHuw Davies }
1110ceba1832SHuw Davies 
1111ceba1832SHuw Davies /**
1112ceba1832SHuw Davies  * calipso_sock_setattr - Add a CALIPSO option to a socket
1113ceba1832SHuw Davies  * @sk: the socket
1114ceba1832SHuw Davies  * @doi_def: the CALIPSO DOI to use
1115ceba1832SHuw Davies  * @secattr: the specific security attributes of the socket
1116ceba1832SHuw Davies  *
1117ceba1832SHuw Davies  * Description:
1118ceba1832SHuw Davies  * Set the CALIPSO option on the given socket using the DOI definition and
1119ceba1832SHuw Davies  * security attributes passed to the function.  This function requires
1120ceba1832SHuw Davies  * exclusive access to @sk, which means it either needs to be in the
1121ceba1832SHuw Davies  * process of being created or locked.  Returns zero on success and negative
1122ceba1832SHuw Davies  * values on failure.
1123ceba1832SHuw Davies  *
1124ceba1832SHuw Davies  */
calipso_sock_setattr(struct sock * sk,const struct calipso_doi * doi_def,const struct netlbl_lsm_secattr * secattr)1125ceba1832SHuw Davies static int calipso_sock_setattr(struct sock *sk,
1126ceba1832SHuw Davies 				const struct calipso_doi *doi_def,
1127ceba1832SHuw Davies 				const struct netlbl_lsm_secattr *secattr)
1128ceba1832SHuw Davies {
1129ceba1832SHuw Davies 	int ret_val;
1130ceba1832SHuw Davies 	struct ipv6_opt_hdr *old, *new;
1131ceba1832SHuw Davies 	struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
1132ceba1832SHuw Davies 
1133ceba1832SHuw Davies 	old = NULL;
1134ceba1832SHuw Davies 	if (txopts)
1135ceba1832SHuw Davies 		old = txopts->hopopt;
1136ceba1832SHuw Davies 
1137ceba1832SHuw Davies 	new = calipso_opt_insert(old, doi_def, secattr);
1138ceba1832SHuw Davies 	txopt_put(txopts);
1139ceba1832SHuw Davies 	if (IS_ERR(new))
1140ceba1832SHuw Davies 		return PTR_ERR(new);
1141ceba1832SHuw Davies 
1142ceba1832SHuw Davies 	ret_val = calipso_opt_update(sk, new);
1143ceba1832SHuw Davies 
1144ceba1832SHuw Davies 	kfree(new);
1145ceba1832SHuw Davies 	return ret_val;
1146ceba1832SHuw Davies }
1147ceba1832SHuw Davies 
1148ceba1832SHuw Davies /**
1149ceba1832SHuw Davies  * calipso_sock_delattr - Delete the CALIPSO option from a socket
1150ceba1832SHuw Davies  * @sk: the socket
1151ceba1832SHuw Davies  *
1152ceba1832SHuw Davies  * Description:
1153ceba1832SHuw Davies  * Removes the CALIPSO option from a socket, if present.
1154ceba1832SHuw Davies  *
1155ceba1832SHuw Davies  */
calipso_sock_delattr(struct sock * sk)1156ceba1832SHuw Davies static void calipso_sock_delattr(struct sock *sk)
1157ceba1832SHuw Davies {
1158ceba1832SHuw Davies 	struct ipv6_opt_hdr *new_hop;
1159ceba1832SHuw Davies 	struct ipv6_txoptions *txopts = txopt_get(inet6_sk(sk));
1160ceba1832SHuw Davies 
1161ceba1832SHuw Davies 	if (!txopts || !txopts->hopopt)
1162ceba1832SHuw Davies 		goto done;
1163ceba1832SHuw Davies 
1164ceba1832SHuw Davies 	if (calipso_opt_del(txopts->hopopt, &new_hop))
1165ceba1832SHuw Davies 		goto done;
1166ceba1832SHuw Davies 
1167ceba1832SHuw Davies 	calipso_opt_update(sk, new_hop);
1168ceba1832SHuw Davies 	kfree(new_hop);
1169ceba1832SHuw Davies 
1170ceba1832SHuw Davies done:
1171ceba1832SHuw Davies 	txopt_put(txopts);
1172ceba1832SHuw Davies }
1173ceba1832SHuw Davies 
1174e1adea92SHuw Davies /* request sock functions.
1175e1adea92SHuw Davies  */
1176e1adea92SHuw Davies 
1177e1adea92SHuw Davies /**
1178e1adea92SHuw Davies  * calipso_req_setattr - Add a CALIPSO option to a connection request socket
1179e1adea92SHuw Davies  * @req: the connection request socket
1180e1adea92SHuw Davies  * @doi_def: the CALIPSO DOI to use
1181e1adea92SHuw Davies  * @secattr: the specific security attributes of the socket
1182e1adea92SHuw Davies  *
1183e1adea92SHuw Davies  * Description:
1184e1adea92SHuw Davies  * Set the CALIPSO option on the given socket using the DOI definition and
1185e1adea92SHuw Davies  * security attributes passed to the function.  Returns zero on success and
1186e1adea92SHuw Davies  * negative values on failure.
1187e1adea92SHuw Davies  *
1188e1adea92SHuw Davies  */
calipso_req_setattr(struct request_sock * req,const struct calipso_doi * doi_def,const struct netlbl_lsm_secattr * secattr)1189e1adea92SHuw Davies static int calipso_req_setattr(struct request_sock *req,
1190e1adea92SHuw Davies 			       const struct calipso_doi *doi_def,
1191e1adea92SHuw Davies 			       const struct netlbl_lsm_secattr *secattr)
1192e1adea92SHuw Davies {
1193e1adea92SHuw Davies 	struct ipv6_txoptions *txopts;
1194e1adea92SHuw Davies 	struct inet_request_sock *req_inet = inet_rsk(req);
1195e1adea92SHuw Davies 	struct ipv6_opt_hdr *old, *new;
1196e1adea92SHuw Davies 	struct sock *sk = sk_to_full_sk(req_to_sk(req));
1197e1adea92SHuw Davies 
1198e1adea92SHuw Davies 	if (req_inet->ipv6_opt && req_inet->ipv6_opt->hopopt)
1199e1adea92SHuw Davies 		old = req_inet->ipv6_opt->hopopt;
1200e1adea92SHuw Davies 	else
1201e1adea92SHuw Davies 		old = NULL;
1202e1adea92SHuw Davies 
1203e1adea92SHuw Davies 	new = calipso_opt_insert(old, doi_def, secattr);
1204e1adea92SHuw Davies 	if (IS_ERR(new))
1205e1adea92SHuw Davies 		return PTR_ERR(new);
1206e1adea92SHuw Davies 
1207a9ba23d4SPaul Moore 	txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new);
1208e1adea92SHuw Davies 
1209e1adea92SHuw Davies 	kfree(new);
1210e1adea92SHuw Davies 
1211e1adea92SHuw Davies 	if (IS_ERR(txopts))
1212e1adea92SHuw Davies 		return PTR_ERR(txopts);
1213e1adea92SHuw Davies 
1214e1adea92SHuw Davies 	txopts = xchg(&req_inet->ipv6_opt, txopts);
1215e1adea92SHuw Davies 	if (txopts) {
1216e1adea92SHuw Davies 		atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
1217e1adea92SHuw Davies 		txopt_put(txopts);
1218e1adea92SHuw Davies 	}
1219e1adea92SHuw Davies 
1220e1adea92SHuw Davies 	return 0;
1221e1adea92SHuw Davies }
1222e1adea92SHuw Davies 
1223e1adea92SHuw Davies /**
1224e1adea92SHuw Davies  * calipso_req_delattr - Delete the CALIPSO option from a request socket
12258da1f442SAndrew Lunn  * @req: the request socket
1226e1adea92SHuw Davies  *
1227e1adea92SHuw Davies  * Description:
1228e1adea92SHuw Davies  * Removes the CALIPSO option from a request socket, if present.
1229e1adea92SHuw Davies  *
1230e1adea92SHuw Davies  */
calipso_req_delattr(struct request_sock * req)1231e1adea92SHuw Davies static void calipso_req_delattr(struct request_sock *req)
1232e1adea92SHuw Davies {
1233e1adea92SHuw Davies 	struct inet_request_sock *req_inet = inet_rsk(req);
1234e1adea92SHuw Davies 	struct ipv6_opt_hdr *new;
1235e1adea92SHuw Davies 	struct ipv6_txoptions *txopts;
1236e1adea92SHuw Davies 	struct sock *sk = sk_to_full_sk(req_to_sk(req));
1237e1adea92SHuw Davies 
1238e1adea92SHuw Davies 	if (!req_inet->ipv6_opt || !req_inet->ipv6_opt->hopopt)
1239e1adea92SHuw Davies 		return;
1240e1adea92SHuw Davies 
1241e1adea92SHuw Davies 	if (calipso_opt_del(req_inet->ipv6_opt->hopopt, &new))
1242e1adea92SHuw Davies 		return; /* Nothing to do */
1243e1adea92SHuw Davies 
1244a9ba23d4SPaul Moore 	txopts = ipv6_renew_options(sk, req_inet->ipv6_opt, IPV6_HOPOPTS, new);
1245e1adea92SHuw Davies 
1246e1adea92SHuw Davies 	if (!IS_ERR(txopts)) {
1247e1adea92SHuw Davies 		txopts = xchg(&req_inet->ipv6_opt, txopts);
1248e1adea92SHuw Davies 		if (txopts) {
1249e1adea92SHuw Davies 			atomic_sub(txopts->tot_len, &sk->sk_omem_alloc);
1250e1adea92SHuw Davies 			txopt_put(txopts);
1251e1adea92SHuw Davies 		}
1252e1adea92SHuw Davies 	}
1253e1adea92SHuw Davies 	kfree(new);
1254e1adea92SHuw Davies }
1255e1adea92SHuw Davies 
12562917f57bSHuw Davies /* skbuff functions.
12572917f57bSHuw Davies  */
12582917f57bSHuw Davies 
12592917f57bSHuw Davies /**
12602917f57bSHuw Davies  * calipso_skbuff_optptr - Find the CALIPSO option in the packet
12612917f57bSHuw Davies  * @skb: the packet
12622917f57bSHuw Davies  *
12632917f57bSHuw Davies  * Description:
12642917f57bSHuw Davies  * Parse the packet's IP header looking for a CALIPSO option.  Returns a pointer
12652917f57bSHuw Davies  * to the start of the CALIPSO option on success, NULL if one if not found.
12662917f57bSHuw Davies  *
12672917f57bSHuw Davies  */
calipso_skbuff_optptr(const struct sk_buff * skb)12682917f57bSHuw Davies static unsigned char *calipso_skbuff_optptr(const struct sk_buff *skb)
12692917f57bSHuw Davies {
12702917f57bSHuw Davies 	const struct ipv6hdr *ip6_hdr = ipv6_hdr(skb);
12712917f57bSHuw Davies 	int offset;
12722917f57bSHuw Davies 
12732917f57bSHuw Davies 	if (ip6_hdr->nexthdr != NEXTHDR_HOP)
12742917f57bSHuw Davies 		return NULL;
12752917f57bSHuw Davies 
12762917f57bSHuw Davies 	offset = ipv6_find_tlv(skb, sizeof(*ip6_hdr), IPV6_TLV_CALIPSO);
12772917f57bSHuw Davies 	if (offset >= 0)
12782917f57bSHuw Davies 		return (unsigned char *)ip6_hdr + offset;
12792917f57bSHuw Davies 
12802917f57bSHuw Davies 	return NULL;
12812917f57bSHuw Davies }
12822917f57bSHuw Davies 
12832917f57bSHuw Davies /**
12842917f57bSHuw Davies  * calipso_skbuff_setattr - Set the CALIPSO option on a packet
12852917f57bSHuw Davies  * @skb: the packet
12862917f57bSHuw Davies  * @doi_def: the CALIPSO DOI to use
12872917f57bSHuw Davies  * @secattr: the security attributes
12882917f57bSHuw Davies  *
12892917f57bSHuw Davies  * Description:
12902917f57bSHuw Davies  * Set the CALIPSO option on the given packet based on the security attributes.
12912917f57bSHuw Davies  * Returns a pointer to the IP header on success and NULL on failure.
12922917f57bSHuw Davies  *
12932917f57bSHuw Davies  */
calipso_skbuff_setattr(struct sk_buff * skb,const struct calipso_doi * doi_def,const struct netlbl_lsm_secattr * secattr)12942917f57bSHuw Davies static int calipso_skbuff_setattr(struct sk_buff *skb,
12952917f57bSHuw Davies 				  const struct calipso_doi *doi_def,
12962917f57bSHuw Davies 				  const struct netlbl_lsm_secattr *secattr)
12972917f57bSHuw Davies {
12982917f57bSHuw Davies 	int ret_val;
12992917f57bSHuw Davies 	struct ipv6hdr *ip6_hdr;
13002917f57bSHuw Davies 	struct ipv6_opt_hdr *hop;
13012917f57bSHuw Davies 	unsigned char buf[CALIPSO_MAX_BUFFER];
1302e3ebdb20SRichard Haines 	int len_delta, new_end, pad, payload;
13032917f57bSHuw Davies 	unsigned int start, end;
13042917f57bSHuw Davies 
13052917f57bSHuw Davies 	ip6_hdr = ipv6_hdr(skb);
13062917f57bSHuw Davies 	if (ip6_hdr->nexthdr == NEXTHDR_HOP) {
13072917f57bSHuw Davies 		hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
13082917f57bSHuw Davies 		ret_val = calipso_opt_find(hop, &start, &end);
13092917f57bSHuw Davies 		if (ret_val && ret_val != -ENOENT)
13102917f57bSHuw Davies 			return ret_val;
13112917f57bSHuw Davies 	} else {
13122917f57bSHuw Davies 		start = 0;
13132917f57bSHuw Davies 		end = 0;
13142917f57bSHuw Davies 	}
13152917f57bSHuw Davies 
13162917f57bSHuw Davies 	memset(buf, 0, sizeof(buf));
13172917f57bSHuw Davies 	ret_val = calipso_genopt(buf, start & 3, sizeof(buf), doi_def, secattr);
13182917f57bSHuw Davies 	if (ret_val < 0)
13192917f57bSHuw Davies 		return ret_val;
13202917f57bSHuw Davies 
13212917f57bSHuw Davies 	new_end = start + ret_val;
13222917f57bSHuw Davies 	/* At this point new_end aligns to 4n, so (new_end & 4) pads to 8n */
13232917f57bSHuw Davies 	pad = ((new_end & 4) + (end & 7)) & 7;
13242917f57bSHuw Davies 	len_delta = new_end - (int)end + pad;
13252917f57bSHuw Davies 	ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
13262917f57bSHuw Davies 	if (ret_val < 0)
13272917f57bSHuw Davies 		return ret_val;
13282917f57bSHuw Davies 
1329e3ebdb20SRichard Haines 	ip6_hdr = ipv6_hdr(skb); /* Reset as skb_cow() may have moved it */
1330e3ebdb20SRichard Haines 
13312917f57bSHuw Davies 	if (len_delta) {
13322917f57bSHuw Davies 		if (len_delta > 0)
13332917f57bSHuw Davies 			skb_push(skb, len_delta);
13342917f57bSHuw Davies 		else
13352917f57bSHuw Davies 			skb_pull(skb, -len_delta);
13362917f57bSHuw Davies 		memmove((char *)ip6_hdr - len_delta, ip6_hdr,
13372917f57bSHuw Davies 			sizeof(*ip6_hdr) + start);
13382917f57bSHuw Davies 		skb_reset_network_header(skb);
13392917f57bSHuw Davies 		ip6_hdr = ipv6_hdr(skb);
1340e3ebdb20SRichard Haines 		payload = ntohs(ip6_hdr->payload_len);
1341e3ebdb20SRichard Haines 		ip6_hdr->payload_len = htons(payload + len_delta);
13422917f57bSHuw Davies 	}
13432917f57bSHuw Davies 
13442917f57bSHuw Davies 	hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
13452917f57bSHuw Davies 	if (start == 0) {
13462917f57bSHuw Davies 		struct ipv6_opt_hdr *new_hop = (struct ipv6_opt_hdr *)buf;
13472917f57bSHuw Davies 
13482917f57bSHuw Davies 		new_hop->nexthdr = ip6_hdr->nexthdr;
13492917f57bSHuw Davies 		new_hop->hdrlen = len_delta / 8 - 1;
13502917f57bSHuw Davies 		ip6_hdr->nexthdr = NEXTHDR_HOP;
13512917f57bSHuw Davies 	} else {
13522917f57bSHuw Davies 		hop->hdrlen += len_delta / 8;
13532917f57bSHuw Davies 	}
13542917f57bSHuw Davies 	memcpy((char *)hop + start, buf + (start & 3), new_end - start);
13552917f57bSHuw Davies 	calipso_pad_write((unsigned char *)hop, new_end, pad);
13562917f57bSHuw Davies 
13572917f57bSHuw Davies 	return 0;
13582917f57bSHuw Davies }
13592917f57bSHuw Davies 
13602917f57bSHuw Davies /**
13612917f57bSHuw Davies  * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
13622917f57bSHuw Davies  * @skb: the packet
13632917f57bSHuw Davies  *
13642917f57bSHuw Davies  * Description:
13652917f57bSHuw Davies  * Removes any and all CALIPSO options from the given packet.  Returns zero on
13662917f57bSHuw Davies  * success, negative values on failure.
13672917f57bSHuw Davies  *
13682917f57bSHuw Davies  */
calipso_skbuff_delattr(struct sk_buff * skb)13692917f57bSHuw Davies static int calipso_skbuff_delattr(struct sk_buff *skb)
13702917f57bSHuw Davies {
13712917f57bSHuw Davies 	int ret_val;
13722917f57bSHuw Davies 	struct ipv6hdr *ip6_hdr;
13732917f57bSHuw Davies 	struct ipv6_opt_hdr *old_hop;
13742917f57bSHuw Davies 	u32 old_hop_len, start = 0, end = 0, delta, size, pad;
13752917f57bSHuw Davies 
13762917f57bSHuw Davies 	if (!calipso_skbuff_optptr(skb))
13772917f57bSHuw Davies 		return 0;
13782917f57bSHuw Davies 
13792917f57bSHuw Davies 	/* since we are changing the packet we should make a copy */
13802917f57bSHuw Davies 	ret_val = skb_cow(skb, skb_headroom(skb));
13812917f57bSHuw Davies 	if (ret_val < 0)
13822917f57bSHuw Davies 		return ret_val;
13832917f57bSHuw Davies 
13842917f57bSHuw Davies 	ip6_hdr = ipv6_hdr(skb);
13852917f57bSHuw Davies 	old_hop = (struct ipv6_opt_hdr *)(ip6_hdr + 1);
13862917f57bSHuw Davies 	old_hop_len = ipv6_optlen(old_hop);
13872917f57bSHuw Davies 
13882917f57bSHuw Davies 	ret_val = calipso_opt_find(old_hop, &start, &end);
13892917f57bSHuw Davies 	if (ret_val)
13902917f57bSHuw Davies 		return ret_val;
13912917f57bSHuw Davies 
13922917f57bSHuw Davies 	if (start == sizeof(*old_hop) && end == old_hop_len) {
13932917f57bSHuw Davies 		/* There's no other option in the header so we delete
13942917f57bSHuw Davies 		 * the whole thing. */
13952917f57bSHuw Davies 		delta = old_hop_len;
13962917f57bSHuw Davies 		size = sizeof(*ip6_hdr);
13972917f57bSHuw Davies 		ip6_hdr->nexthdr = old_hop->nexthdr;
13982917f57bSHuw Davies 	} else {
13992917f57bSHuw Davies 		delta = (end - start) & ~7;
14002917f57bSHuw Davies 		if (delta)
14012917f57bSHuw Davies 			old_hop->hdrlen -= delta / 8;
14022917f57bSHuw Davies 		pad = (end - start) & 7;
14032917f57bSHuw Davies 		size = sizeof(*ip6_hdr) + start + pad;
14042917f57bSHuw Davies 		calipso_pad_write((unsigned char *)old_hop, start, pad);
14052917f57bSHuw Davies 	}
14062917f57bSHuw Davies 
14072917f57bSHuw Davies 	if (delta) {
14082917f57bSHuw Davies 		skb_pull(skb, delta);
14092917f57bSHuw Davies 		memmove((char *)ip6_hdr + delta, ip6_hdr, size);
14102917f57bSHuw Davies 		skb_reset_network_header(skb);
14112917f57bSHuw Davies 	}
14122917f57bSHuw Davies 
14132917f57bSHuw Davies 	return 0;
14142917f57bSHuw Davies }
14152917f57bSHuw Davies 
1416cb72d382SHuw Davies static const struct netlbl_calipso_ops ops = {
1417cb72d382SHuw Davies 	.doi_add          = calipso_doi_add,
1418cb72d382SHuw Davies 	.doi_free         = calipso_doi_free,
1419d7cce015SHuw Davies 	.doi_remove       = calipso_doi_remove,
1420a5e34490SHuw Davies 	.doi_getdef       = calipso_doi_getdef,
1421a5e34490SHuw Davies 	.doi_putdef       = calipso_doi_putdef,
1422e1ce69dfSHuw Davies 	.doi_walk         = calipso_doi_walk,
1423ceba1832SHuw Davies 	.sock_getattr     = calipso_sock_getattr,
1424ceba1832SHuw Davies 	.sock_setattr     = calipso_sock_setattr,
1425ceba1832SHuw Davies 	.sock_delattr     = calipso_sock_delattr,
1426e1adea92SHuw Davies 	.req_setattr      = calipso_req_setattr,
1427e1adea92SHuw Davies 	.req_delattr      = calipso_req_delattr,
14282917f57bSHuw Davies 	.opt_getattr      = calipso_opt_getattr,
14292917f57bSHuw Davies 	.skbuff_optptr    = calipso_skbuff_optptr,
14302917f57bSHuw Davies 	.skbuff_setattr   = calipso_skbuff_setattr,
14312917f57bSHuw Davies 	.skbuff_delattr   = calipso_skbuff_delattr,
14324fee5242SHuw Davies 	.cache_invalidate = calipso_cache_invalidate,
14334fee5242SHuw Davies 	.cache_add        = calipso_cache_add
1434cb72d382SHuw Davies };
1435cb72d382SHuw Davies 
1436cb72d382SHuw Davies /**
1437cb72d382SHuw Davies  * calipso_init - Initialize the CALIPSO module
1438cb72d382SHuw Davies  *
1439cb72d382SHuw Davies  * Description:
1440cb72d382SHuw Davies  * Initialize the CALIPSO module and prepare it for use.  Returns zero on
1441cb72d382SHuw Davies  * success and negative values on failure.
1442cb72d382SHuw Davies  *
1443cb72d382SHuw Davies  */
calipso_init(void)1444cb72d382SHuw Davies int __init calipso_init(void)
1445cb72d382SHuw Davies {
14464fee5242SHuw Davies 	int ret_val;
14474fee5242SHuw Davies 
14484fee5242SHuw Davies 	ret_val = calipso_cache_init();
14494fee5242SHuw Davies 	if (!ret_val)
1450cb72d382SHuw Davies 		netlbl_calipso_ops_register(&ops);
14514fee5242SHuw Davies 	return ret_val;
1452cb72d382SHuw Davies }
1453cb72d382SHuw Davies 
calipso_exit(void)1454cb72d382SHuw Davies void calipso_exit(void)
1455cb72d382SHuw Davies {
1456cb72d382SHuw Davies 	netlbl_calipso_ops_register(NULL);
14574fee5242SHuw Davies 	calipso_cache_invalidate();
14584fee5242SHuw Davies 	kfree(calipso_cache);
1459cb72d382SHuw Davies }
1460