xref: /linux/net/ipv4/cipso_ipv4.c (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2446fda4fSPaul Moore /*
3446fda4fSPaul Moore  * CIPSO - Commercial IP Security Option
4446fda4fSPaul Moore  *
5446fda4fSPaul Moore  * This is an implementation of the CIPSO 2.2 protocol as specified in
6446fda4fSPaul Moore  * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
7586c2500SPaul Moore  * FIPS-188.  While CIPSO never became a full IETF RFC standard many vendors
8446fda4fSPaul Moore  * have chosen to adopt the protocol and over the years it has become a
9446fda4fSPaul Moore  * de-facto standard for labeled networking.
10446fda4fSPaul Moore  *
11586c2500SPaul Moore  * The CIPSO draft specification can be found in the kernel's Documentation
12586c2500SPaul Moore  * directory as well as the following URL:
137a6498ebSAlexander A. Klimov  *   https://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt
14586c2500SPaul Moore  * The FIPS-188 specification can be found at the following URL:
157a6498ebSAlexander A. Klimov  *   https://www.itl.nist.gov/fipspubs/fip188.htm
16586c2500SPaul Moore  *
17446fda4fSPaul Moore  * Author: Paul Moore <paul.moore@hp.com>
18446fda4fSPaul Moore  */
19446fda4fSPaul Moore 
20446fda4fSPaul Moore /*
21948bf85cSPaul Moore  * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
22446fda4fSPaul Moore  */
23446fda4fSPaul Moore 
24446fda4fSPaul Moore #include <linux/init.h>
25446fda4fSPaul Moore #include <linux/types.h>
26446fda4fSPaul Moore #include <linux/rcupdate.h>
27446fda4fSPaul Moore #include <linux/list.h>
28446fda4fSPaul Moore #include <linux/spinlock.h>
29446fda4fSPaul Moore #include <linux/string.h>
30446fda4fSPaul Moore #include <linux/jhash.h>
316c2e8ac0SPaul Moore #include <linux/audit.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33446fda4fSPaul Moore #include <net/ip.h>
34446fda4fSPaul Moore #include <net/icmp.h>
35446fda4fSPaul Moore #include <net/tcp.h>
36446fda4fSPaul Moore #include <net/netlabel.h>
37446fda4fSPaul Moore #include <net/cipso_ipv4.h>
3860063497SArun Sharma #include <linux/atomic.h>
394c787b16SFabian Frederick #include <linux/bug.h>
40*5f60d5f6SAl Viro #include <linux/unaligned.h>
41446fda4fSPaul Moore 
42446fda4fSPaul Moore /* List of available DOI definitions */
43446fda4fSPaul Moore /* XXX - This currently assumes a minimal number of different DOIs in use,
44446fda4fSPaul Moore  * if in practice there are a lot of different DOIs this list should
45446fda4fSPaul Moore  * probably be turned into a hash table or something similar so we
46446fda4fSPaul Moore  * can do quick lookups. */
478ce11e6aSAdrian Bunk static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
481596c97aSDenis Cheng static LIST_HEAD(cipso_v4_doi_list);
49446fda4fSPaul Moore 
50446fda4fSPaul Moore /* Label mapping cache */
51446fda4fSPaul Moore int cipso_v4_cache_enabled = 1;
52446fda4fSPaul Moore int cipso_v4_cache_bucketsize = 10;
53446fda4fSPaul Moore #define CIPSO_V4_CACHE_BUCKETBITS     7
54446fda4fSPaul Moore #define CIPSO_V4_CACHE_BUCKETS        (1 << CIPSO_V4_CACHE_BUCKETBITS)
55446fda4fSPaul Moore #define CIPSO_V4_CACHE_REORDERLIMIT   10
56446fda4fSPaul Moore struct cipso_v4_map_cache_bkt {
57446fda4fSPaul Moore 	spinlock_t lock;
58446fda4fSPaul Moore 	u32 size;
59446fda4fSPaul Moore 	struct list_head list;
60446fda4fSPaul Moore };
61988b1343SFabian Frederick 
62446fda4fSPaul Moore struct cipso_v4_map_cache_entry {
63446fda4fSPaul Moore 	u32 hash;
64446fda4fSPaul Moore 	unsigned char *key;
65446fda4fSPaul Moore 	size_t key_len;
66446fda4fSPaul Moore 
67ffb733c6Spaul.moore@hp.com 	struct netlbl_lsm_cache *lsm_data;
68446fda4fSPaul Moore 
69446fda4fSPaul Moore 	u32 activity;
70446fda4fSPaul Moore 	struct list_head list;
71446fda4fSPaul Moore };
72988b1343SFabian Frederick 
73988b1343SFabian Frederick static struct cipso_v4_map_cache_bkt *cipso_v4_cache;
74446fda4fSPaul Moore 
75446fda4fSPaul Moore /* Restricted bitmap (tag #1) flags */
76db9c8e2bSwangzhitong int cipso_v4_rbm_optfmt;
77446fda4fSPaul Moore int cipso_v4_rbm_strictvalid = 1;
78446fda4fSPaul Moore 
79446fda4fSPaul Moore /*
80f998e8cbSPaul Moore  * Protocol Constants
81f998e8cbSPaul Moore  */
82f998e8cbSPaul Moore 
83f998e8cbSPaul Moore /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
84f998e8cbSPaul Moore  * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
85f998e8cbSPaul Moore #define CIPSO_V4_OPT_LEN_MAX          40
86f998e8cbSPaul Moore 
87f998e8cbSPaul Moore /* Length of the base CIPSO option, this includes the option type (1 byte), the
88f998e8cbSPaul Moore  * option length (1 byte), and the DOI (4 bytes). */
89f998e8cbSPaul Moore #define CIPSO_V4_HDR_LEN              6
90f998e8cbSPaul Moore 
91f998e8cbSPaul Moore /* Base length of the restrictive category bitmap tag (tag #1). */
92f998e8cbSPaul Moore #define CIPSO_V4_TAG_RBM_BLEN         4
93f998e8cbSPaul Moore 
94f998e8cbSPaul Moore /* Base length of the enumerated category tag (tag #2). */
95f998e8cbSPaul Moore #define CIPSO_V4_TAG_ENUM_BLEN        4
96f998e8cbSPaul Moore 
97f998e8cbSPaul Moore /* Base length of the ranged categories bitmap tag (tag #5). */
98f998e8cbSPaul Moore #define CIPSO_V4_TAG_RNG_BLEN         4
99f998e8cbSPaul Moore /* The maximum number of category ranges permitted in the ranged category tag
100f998e8cbSPaul Moore  * (tag #5).  You may note that the IETF draft states that the maximum number
101f998e8cbSPaul Moore  * of category ranges is 7, but if the low end of the last category range is
10225985edcSLucas De Marchi  * zero then it is possible to fit 8 category ranges because the zero should
103f998e8cbSPaul Moore  * be omitted. */
104f998e8cbSPaul Moore #define CIPSO_V4_TAG_RNG_CAT_MAX      8
105f998e8cbSPaul Moore 
10615c45f7bSPaul Moore /* Base length of the local tag (non-standard tag).
10715c45f7bSPaul Moore  *  Tag definition (may change between kernel versions)
10815c45f7bSPaul Moore  *
10915c45f7bSPaul Moore  * 0          8          16         24         32
11015c45f7bSPaul Moore  * +----------+----------+----------+----------+
11115c45f7bSPaul Moore  * | 10000000 | 00000110 | 32-bit secid value  |
11215c45f7bSPaul Moore  * +----------+----------+----------+----------+
11315c45f7bSPaul Moore  * | in (host byte order)|
11415c45f7bSPaul Moore  * +----------+----------+
11515c45f7bSPaul Moore  *
11615c45f7bSPaul Moore  */
11715c45f7bSPaul Moore #define CIPSO_V4_TAG_LOC_BLEN         6
11815c45f7bSPaul Moore 
119f998e8cbSPaul Moore /*
120446fda4fSPaul Moore  * Helper Functions
121446fda4fSPaul Moore  */
122446fda4fSPaul Moore 
123446fda4fSPaul Moore /**
124446fda4fSPaul Moore  * cipso_v4_cache_entry_free - Frees a cache entry
125446fda4fSPaul Moore  * @entry: the entry to free
126446fda4fSPaul Moore  *
127446fda4fSPaul Moore  * Description:
128ffb733c6Spaul.moore@hp.com  * This function frees the memory associated with a cache entry including the
129ffb733c6Spaul.moore@hp.com  * LSM cache data if there are no longer any users, i.e. reference count == 0.
130446fda4fSPaul Moore  *
131446fda4fSPaul Moore  */
cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry * entry)132446fda4fSPaul Moore static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
133446fda4fSPaul Moore {
134ffb733c6Spaul.moore@hp.com 	if (entry->lsm_data)
135ffb733c6Spaul.moore@hp.com 		netlbl_secattr_cache_free(entry->lsm_data);
136446fda4fSPaul Moore 	kfree(entry->key);
137446fda4fSPaul Moore 	kfree(entry);
138446fda4fSPaul Moore }
139446fda4fSPaul Moore 
140446fda4fSPaul Moore /**
141446fda4fSPaul Moore  * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
142446fda4fSPaul Moore  * @key: the hash key
143446fda4fSPaul Moore  * @key_len: the length of the key in bytes
144446fda4fSPaul Moore  *
145446fda4fSPaul Moore  * Description:
146446fda4fSPaul Moore  * The CIPSO tag hashing function.  Returns a 32-bit hash value.
147446fda4fSPaul Moore  *
148446fda4fSPaul Moore  */
cipso_v4_map_cache_hash(const unsigned char * key,u32 key_len)149446fda4fSPaul Moore static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
150446fda4fSPaul Moore {
151446fda4fSPaul Moore 	return jhash(key, key_len, 0);
152446fda4fSPaul Moore }
153446fda4fSPaul Moore 
154446fda4fSPaul Moore /*
155446fda4fSPaul Moore  * Label Mapping Cache Functions
156446fda4fSPaul Moore  */
157446fda4fSPaul Moore 
158446fda4fSPaul Moore /**
159446fda4fSPaul Moore  * cipso_v4_cache_init - Initialize the CIPSO cache
160446fda4fSPaul Moore  *
161446fda4fSPaul Moore  * Description:
162446fda4fSPaul Moore  * Initializes the CIPSO label mapping cache, this function should be called
163446fda4fSPaul Moore  * before any of the other functions defined in this file.  Returns zero on
164446fda4fSPaul Moore  * success, negative values on error.
165446fda4fSPaul Moore  *
166446fda4fSPaul Moore  */
cipso_v4_cache_init(void)167cb57659aSFabian Frederick static int __init cipso_v4_cache_init(void)
168446fda4fSPaul Moore {
169446fda4fSPaul Moore 	u32 iter;
170446fda4fSPaul Moore 
171446fda4fSPaul Moore 	cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
172446fda4fSPaul Moore 				 sizeof(struct cipso_v4_map_cache_bkt),
173446fda4fSPaul Moore 				 GFP_KERNEL);
17451456b29SIan Morris 	if (!cipso_v4_cache)
175446fda4fSPaul Moore 		return -ENOMEM;
176446fda4fSPaul Moore 
177446fda4fSPaul Moore 	for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
178446fda4fSPaul Moore 		spin_lock_init(&cipso_v4_cache[iter].lock);
179446fda4fSPaul Moore 		cipso_v4_cache[iter].size = 0;
180446fda4fSPaul Moore 		INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
181446fda4fSPaul Moore 	}
182446fda4fSPaul Moore 
183446fda4fSPaul Moore 	return 0;
184446fda4fSPaul Moore }
185446fda4fSPaul Moore 
186446fda4fSPaul Moore /**
187446fda4fSPaul Moore  * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
188446fda4fSPaul Moore  *
189446fda4fSPaul Moore  * Description:
1904ac9e23cSZheng Yejian  * Invalidates and frees any entries in the CIPSO cache.
191446fda4fSPaul Moore  *
192446fda4fSPaul Moore  */
cipso_v4_cache_invalidate(void)193446fda4fSPaul Moore void cipso_v4_cache_invalidate(void)
194446fda4fSPaul Moore {
195446fda4fSPaul Moore 	struct cipso_v4_map_cache_entry *entry, *tmp_entry;
196446fda4fSPaul Moore 	u32 iter;
197446fda4fSPaul Moore 
198446fda4fSPaul Moore 	for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
199609c92feSPaul Moore 		spin_lock_bh(&cipso_v4_cache[iter].lock);
200446fda4fSPaul Moore 		list_for_each_entry_safe(entry,
201446fda4fSPaul Moore 					 tmp_entry,
202446fda4fSPaul Moore 					 &cipso_v4_cache[iter].list, list) {
203446fda4fSPaul Moore 			list_del(&entry->list);
204446fda4fSPaul Moore 			cipso_v4_cache_entry_free(entry);
205446fda4fSPaul Moore 		}
206446fda4fSPaul Moore 		cipso_v4_cache[iter].size = 0;
207609c92feSPaul Moore 		spin_unlock_bh(&cipso_v4_cache[iter].lock);
208446fda4fSPaul Moore 	}
209446fda4fSPaul Moore }
210446fda4fSPaul Moore 
211446fda4fSPaul Moore /**
212446fda4fSPaul Moore  * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
213446fda4fSPaul Moore  * @key: the buffer to check
214446fda4fSPaul Moore  * @key_len: buffer length in bytes
215446fda4fSPaul Moore  * @secattr: the security attribute struct to use
216446fda4fSPaul Moore  *
217446fda4fSPaul Moore  * Description:
218446fda4fSPaul Moore  * This function checks the cache to see if a label mapping already exists for
219446fda4fSPaul Moore  * the given key.  If there is a match then the cache is adjusted and the
220446fda4fSPaul Moore  * @secattr struct is populated with the correct LSM security attributes.  The
221446fda4fSPaul Moore  * cache is adjusted in the following manner if the entry is not already the
222446fda4fSPaul Moore  * first in the cache bucket:
223446fda4fSPaul Moore  *
224446fda4fSPaul Moore  *  1. The cache entry's activity counter is incremented
225446fda4fSPaul Moore  *  2. The previous (higher ranking) entry's activity counter is decremented
226446fda4fSPaul Moore  *  3. If the difference between the two activity counters is geater than
227446fda4fSPaul Moore  *     CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
228446fda4fSPaul Moore  *
229446fda4fSPaul Moore  * Returns zero on success, -ENOENT for a cache miss, and other negative values
230446fda4fSPaul Moore  * on error.
231446fda4fSPaul Moore  *
232446fda4fSPaul Moore  */
cipso_v4_cache_check(const unsigned char * key,u32 key_len,struct netlbl_lsm_secattr * secattr)233446fda4fSPaul Moore static int cipso_v4_cache_check(const unsigned char *key,
234446fda4fSPaul Moore 				u32 key_len,
235446fda4fSPaul Moore 				struct netlbl_lsm_secattr *secattr)
236446fda4fSPaul Moore {
237446fda4fSPaul Moore 	u32 bkt;
238446fda4fSPaul Moore 	struct cipso_v4_map_cache_entry *entry;
239446fda4fSPaul Moore 	struct cipso_v4_map_cache_entry *prev_entry = NULL;
240446fda4fSPaul Moore 	u32 hash;
241446fda4fSPaul Moore 
242dd44f04bSKuniyuki Iwashima 	if (!READ_ONCE(cipso_v4_cache_enabled))
243446fda4fSPaul Moore 		return -ENOENT;
244446fda4fSPaul Moore 
245446fda4fSPaul Moore 	hash = cipso_v4_map_cache_hash(key, key_len);
2465e0f8923SPavel Emelyanov 	bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
247609c92feSPaul Moore 	spin_lock_bh(&cipso_v4_cache[bkt].lock);
248446fda4fSPaul Moore 	list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
249446fda4fSPaul Moore 		if (entry->hash == hash &&
250446fda4fSPaul Moore 		    entry->key_len == key_len &&
251446fda4fSPaul Moore 		    memcmp(entry->key, key, key_len) == 0) {
252446fda4fSPaul Moore 			entry->activity += 1;
253b4217b82SReshetova, Elena 			refcount_inc(&entry->lsm_data->refcount);
254ffb733c6Spaul.moore@hp.com 			secattr->cache = entry->lsm_data;
255701a90baSPaul Moore 			secattr->flags |= NETLBL_SECATTR_CACHE;
25616efd454SPaul Moore 			secattr->type = NETLBL_NLTYPE_CIPSOV4;
25751456b29SIan Morris 			if (!prev_entry) {
258609c92feSPaul Moore 				spin_unlock_bh(&cipso_v4_cache[bkt].lock);
259446fda4fSPaul Moore 				return 0;
260446fda4fSPaul Moore 			}
261446fda4fSPaul Moore 
262446fda4fSPaul Moore 			if (prev_entry->activity > 0)
263446fda4fSPaul Moore 				prev_entry->activity -= 1;
264446fda4fSPaul Moore 			if (entry->activity > prev_entry->activity &&
265446fda4fSPaul Moore 			    entry->activity - prev_entry->activity >
266446fda4fSPaul Moore 			    CIPSO_V4_CACHE_REORDERLIMIT) {
267446fda4fSPaul Moore 				__list_del(entry->list.prev, entry->list.next);
268446fda4fSPaul Moore 				__list_add(&entry->list,
269446fda4fSPaul Moore 					   prev_entry->list.prev,
270446fda4fSPaul Moore 					   &prev_entry->list);
271446fda4fSPaul Moore 			}
272446fda4fSPaul Moore 
273609c92feSPaul Moore 			spin_unlock_bh(&cipso_v4_cache[bkt].lock);
274446fda4fSPaul Moore 			return 0;
275446fda4fSPaul Moore 		}
276446fda4fSPaul Moore 		prev_entry = entry;
277446fda4fSPaul Moore 	}
278609c92feSPaul Moore 	spin_unlock_bh(&cipso_v4_cache[bkt].lock);
279446fda4fSPaul Moore 
280446fda4fSPaul Moore 	return -ENOENT;
281446fda4fSPaul Moore }
282446fda4fSPaul Moore 
283446fda4fSPaul Moore /**
284446fda4fSPaul Moore  * cipso_v4_cache_add - Add an entry to the CIPSO cache
2853628e3cbSAndrew Lunn  * @cipso_ptr: pointer to CIPSO IP option
286446fda4fSPaul Moore  * @secattr: the packet's security attributes
287446fda4fSPaul Moore  *
288446fda4fSPaul Moore  * Description:
289446fda4fSPaul Moore  * Add a new entry into the CIPSO label mapping cache.  Add the new entry to
290446fda4fSPaul Moore  * head of the cache bucket's list, if the cache bucket is out of room remove
291446fda4fSPaul Moore  * the last entry in the list first.  It is important to note that there is
292446fda4fSPaul Moore  * currently no checking for duplicate keys.  Returns zero on success,
293446fda4fSPaul Moore  * negative values on failure.
294446fda4fSPaul Moore  *
295446fda4fSPaul Moore  */
cipso_v4_cache_add(const unsigned char * cipso_ptr,const struct netlbl_lsm_secattr * secattr)29604f81f01SPaul Moore int cipso_v4_cache_add(const unsigned char *cipso_ptr,
297446fda4fSPaul Moore 		       const struct netlbl_lsm_secattr *secattr)
298446fda4fSPaul Moore {
299dd44f04bSKuniyuki Iwashima 	int bkt_size = READ_ONCE(cipso_v4_cache_bucketsize);
300446fda4fSPaul Moore 	int ret_val = -EPERM;
301446fda4fSPaul Moore 	u32 bkt;
302446fda4fSPaul Moore 	struct cipso_v4_map_cache_entry *entry = NULL;
303446fda4fSPaul Moore 	struct cipso_v4_map_cache_entry *old_entry = NULL;
304446fda4fSPaul Moore 	u32 cipso_ptr_len;
305446fda4fSPaul Moore 
306dd44f04bSKuniyuki Iwashima 	if (!READ_ONCE(cipso_v4_cache_enabled) || bkt_size <= 0)
307446fda4fSPaul Moore 		return 0;
308446fda4fSPaul Moore 
309446fda4fSPaul Moore 	cipso_ptr_len = cipso_ptr[1];
310446fda4fSPaul Moore 
311446fda4fSPaul Moore 	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
31251456b29SIan Morris 	if (!entry)
313446fda4fSPaul Moore 		return -ENOMEM;
314fac5d731SArnaldo Carvalho de Melo 	entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
31551456b29SIan Morris 	if (!entry->key) {
316446fda4fSPaul Moore 		ret_val = -ENOMEM;
317446fda4fSPaul Moore 		goto cache_add_failure;
318446fda4fSPaul Moore 	}
319446fda4fSPaul Moore 	entry->key_len = cipso_ptr_len;
320446fda4fSPaul Moore 	entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
321b4217b82SReshetova, Elena 	refcount_inc(&secattr->cache->refcount);
322ffb733c6Spaul.moore@hp.com 	entry->lsm_data = secattr->cache;
323446fda4fSPaul Moore 
3245e0f8923SPavel Emelyanov 	bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
325609c92feSPaul Moore 	spin_lock_bh(&cipso_v4_cache[bkt].lock);
326dd44f04bSKuniyuki Iwashima 	if (cipso_v4_cache[bkt].size < bkt_size) {
327446fda4fSPaul Moore 		list_add(&entry->list, &cipso_v4_cache[bkt].list);
328446fda4fSPaul Moore 		cipso_v4_cache[bkt].size += 1;
329446fda4fSPaul Moore 	} else {
330446fda4fSPaul Moore 		old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
331446fda4fSPaul Moore 				       struct cipso_v4_map_cache_entry, list);
332446fda4fSPaul Moore 		list_del(&old_entry->list);
333446fda4fSPaul Moore 		list_add(&entry->list, &cipso_v4_cache[bkt].list);
334446fda4fSPaul Moore 		cipso_v4_cache_entry_free(old_entry);
335446fda4fSPaul Moore 	}
336609c92feSPaul Moore 	spin_unlock_bh(&cipso_v4_cache[bkt].lock);
337446fda4fSPaul Moore 
338446fda4fSPaul Moore 	return 0;
339446fda4fSPaul Moore 
340446fda4fSPaul Moore cache_add_failure:
341446fda4fSPaul Moore 	if (entry)
342446fda4fSPaul Moore 		cipso_v4_cache_entry_free(entry);
343446fda4fSPaul Moore 	return ret_val;
344446fda4fSPaul Moore }
345446fda4fSPaul Moore 
346446fda4fSPaul Moore /*
347446fda4fSPaul Moore  * DOI List Functions
348446fda4fSPaul Moore  */
349446fda4fSPaul Moore 
350446fda4fSPaul Moore /**
351446fda4fSPaul Moore  * cipso_v4_doi_search - Searches for a DOI definition
352446fda4fSPaul Moore  * @doi: the DOI to search for
353446fda4fSPaul Moore  *
354446fda4fSPaul Moore  * Description:
355446fda4fSPaul Moore  * Search the DOI definition list for a DOI definition with a DOI value that
35625985edcSLucas De Marchi  * matches @doi.  The caller is responsible for calling rcu_read_[un]lock().
357446fda4fSPaul Moore  * Returns a pointer to the DOI definition on success and NULL on failure.
358446fda4fSPaul Moore  */
cipso_v4_doi_search(u32 doi)359446fda4fSPaul Moore static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
360446fda4fSPaul Moore {
361446fda4fSPaul Moore 	struct cipso_v4_doi *iter;
362446fda4fSPaul Moore 
363446fda4fSPaul Moore 	list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
364f6a6fedeSReshetova, Elena 		if (iter->doi == doi && refcount_read(&iter->refcount))
365446fda4fSPaul Moore 			return iter;
366446fda4fSPaul Moore 	return NULL;
367446fda4fSPaul Moore }
368446fda4fSPaul Moore 
369446fda4fSPaul Moore /**
370446fda4fSPaul Moore  * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
371446fda4fSPaul Moore  * @doi_def: the DOI structure
3726c2e8ac0SPaul Moore  * @audit_info: NetLabel audit information
373446fda4fSPaul Moore  *
374446fda4fSPaul Moore  * Description:
375446fda4fSPaul Moore  * The caller defines a new DOI for use by the CIPSO engine and calls this
376446fda4fSPaul Moore  * function to add it to the list of acceptable domains.  The caller must
377446fda4fSPaul Moore  * ensure that the mapping table specified in @doi_def->map meets all of the
378446fda4fSPaul Moore  * requirements of the mapping type (see cipso_ipv4.h for details).  Returns
379446fda4fSPaul Moore  * zero on success and non-zero on failure.
380446fda4fSPaul Moore  *
381446fda4fSPaul Moore  */
cipso_v4_doi_add(struct cipso_v4_doi * doi_def,struct netlbl_audit * audit_info)3826c2e8ac0SPaul Moore int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
3836c2e8ac0SPaul Moore 		     struct netlbl_audit *audit_info)
384446fda4fSPaul Moore {
3856c2e8ac0SPaul Moore 	int ret_val = -EINVAL;
3866ce61a7cSPaul Moore 	u32 iter;
3876c2e8ac0SPaul Moore 	u32 doi;
3886c2e8ac0SPaul Moore 	u32 doi_type;
3896c2e8ac0SPaul Moore 	struct audit_buffer *audit_buf;
3906c2e8ac0SPaul Moore 
3916c2e8ac0SPaul Moore 	doi = doi_def->doi;
3926c2e8ac0SPaul Moore 	doi_type = doi_def->type;
3936ce61a7cSPaul Moore 
39456755924SDan Carpenter 	if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
3956c2e8ac0SPaul Moore 		goto doi_add_return;
3966ce61a7cSPaul Moore 	for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
3976ce61a7cSPaul Moore 		switch (doi_def->tags[iter]) {
3986ce61a7cSPaul Moore 		case CIPSO_V4_TAG_RBITMAP:
3996ce61a7cSPaul Moore 			break;
400484b3669SPaul Moore 		case CIPSO_V4_TAG_RANGE:
401654bbc2aSPaul Moore 		case CIPSO_V4_TAG_ENUM:
402654bbc2aSPaul Moore 			if (doi_def->type != CIPSO_V4_MAP_PASS)
4036c2e8ac0SPaul Moore 				goto doi_add_return;
404654bbc2aSPaul Moore 			break;
40515c45f7bSPaul Moore 		case CIPSO_V4_TAG_LOCAL:
40615c45f7bSPaul Moore 			if (doi_def->type != CIPSO_V4_MAP_LOCAL)
4076c2e8ac0SPaul Moore 				goto doi_add_return;
4086c2e8ac0SPaul Moore 			break;
4096c2e8ac0SPaul Moore 		case CIPSO_V4_TAG_INVALID:
4106c2e8ac0SPaul Moore 			if (iter == 0)
4116c2e8ac0SPaul Moore 				goto doi_add_return;
41215c45f7bSPaul Moore 			break;
4136ce61a7cSPaul Moore 		default:
4146c2e8ac0SPaul Moore 			goto doi_add_return;
4156ce61a7cSPaul Moore 		}
4166ce61a7cSPaul Moore 	}
417446fda4fSPaul Moore 
418f6a6fedeSReshetova, Elena 	refcount_set(&doi_def->refcount, 1);
419446fda4fSPaul Moore 
420446fda4fSPaul Moore 	spin_lock(&cipso_v4_doi_list_lock);
42100db4124SIan Morris 	if (cipso_v4_doi_search(doi_def->doi)) {
4226c2e8ac0SPaul Moore 		spin_unlock(&cipso_v4_doi_list_lock);
4236c2e8ac0SPaul Moore 		ret_val = -EEXIST;
4246c2e8ac0SPaul Moore 		goto doi_add_return;
4256c2e8ac0SPaul Moore 	}
426446fda4fSPaul Moore 	list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
427446fda4fSPaul Moore 	spin_unlock(&cipso_v4_doi_list_lock);
4286c2e8ac0SPaul Moore 	ret_val = 0;
429446fda4fSPaul Moore 
4306c2e8ac0SPaul Moore doi_add_return:
4316c2e8ac0SPaul Moore 	audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
43200db4124SIan Morris 	if (audit_buf) {
4336c2e8ac0SPaul Moore 		const char *type_str;
4346c2e8ac0SPaul Moore 		switch (doi_type) {
4356c2e8ac0SPaul Moore 		case CIPSO_V4_MAP_TRANS:
4366c2e8ac0SPaul Moore 			type_str = "trans";
4376c2e8ac0SPaul Moore 			break;
4386c2e8ac0SPaul Moore 		case CIPSO_V4_MAP_PASS:
4396c2e8ac0SPaul Moore 			type_str = "pass";
4406c2e8ac0SPaul Moore 			break;
4416c2e8ac0SPaul Moore 		case CIPSO_V4_MAP_LOCAL:
4426c2e8ac0SPaul Moore 			type_str = "local";
4436c2e8ac0SPaul Moore 			break;
4446c2e8ac0SPaul Moore 		default:
4456c2e8ac0SPaul Moore 			type_str = "(unknown)";
4466c2e8ac0SPaul Moore 		}
4476c2e8ac0SPaul Moore 		audit_log_format(audit_buf,
4486c2e8ac0SPaul Moore 				 " cipso_doi=%u cipso_type=%s res=%u",
4496c2e8ac0SPaul Moore 				 doi, type_str, ret_val == 0 ? 1 : 0);
4506c2e8ac0SPaul Moore 		audit_log_end(audit_buf);
4516c2e8ac0SPaul Moore 	}
452446fda4fSPaul Moore 
4536c2e8ac0SPaul Moore 	return ret_val;
454446fda4fSPaul Moore }
455446fda4fSPaul Moore 
456446fda4fSPaul Moore /**
457b1edeb10SPaul Moore  * cipso_v4_doi_free - Frees a DOI definition
4584973404fSFabian Frederick  * @doi_def: the DOI definition
459446fda4fSPaul Moore  *
460446fda4fSPaul Moore  * Description:
461b1edeb10SPaul Moore  * This function frees all of the memory associated with a DOI definition.
462446fda4fSPaul Moore  *
463446fda4fSPaul Moore  */
cipso_v4_doi_free(struct cipso_v4_doi * doi_def)464b1edeb10SPaul Moore void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
465446fda4fSPaul Moore {
46651456b29SIan Morris 	if (!doi_def)
467b1edeb10SPaul Moore 		return;
468446fda4fSPaul Moore 
469b1edeb10SPaul Moore 	switch (doi_def->type) {
47015c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
471b1edeb10SPaul Moore 		kfree(doi_def->map.std->lvl.cipso);
472b1edeb10SPaul Moore 		kfree(doi_def->map.std->lvl.local);
473b1edeb10SPaul Moore 		kfree(doi_def->map.std->cat.cipso);
474b1edeb10SPaul Moore 		kfree(doi_def->map.std->cat.local);
475d612c3f3SNanyong Sun 		kfree(doi_def->map.std);
476b1edeb10SPaul Moore 		break;
477446fda4fSPaul Moore 	}
478b1edeb10SPaul Moore 	kfree(doi_def);
479446fda4fSPaul Moore }
480446fda4fSPaul Moore 
481446fda4fSPaul Moore /**
482b1edeb10SPaul Moore  * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
483b1edeb10SPaul Moore  * @entry: the entry's RCU field
484b1edeb10SPaul Moore  *
485b1edeb10SPaul Moore  * Description:
486b1edeb10SPaul Moore  * This function is designed to be used as a callback to the call_rcu()
487b1edeb10SPaul Moore  * function so that the memory allocated to the DOI definition can be released
488b1edeb10SPaul Moore  * safely.
489b1edeb10SPaul Moore  *
490b1edeb10SPaul Moore  */
cipso_v4_doi_free_rcu(struct rcu_head * entry)491b1edeb10SPaul Moore static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
492b1edeb10SPaul Moore {
493b1edeb10SPaul Moore 	struct cipso_v4_doi *doi_def;
494b1edeb10SPaul Moore 
495b1edeb10SPaul Moore 	doi_def = container_of(entry, struct cipso_v4_doi, rcu);
496b1edeb10SPaul Moore 	cipso_v4_doi_free(doi_def);
497b1edeb10SPaul Moore }
498b1edeb10SPaul Moore 
499b1edeb10SPaul Moore /**
500b1edeb10SPaul Moore  * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
501b1edeb10SPaul Moore  * @doi: the DOI value
5027edce636SWang Hai  * @audit_info: NetLabel audit information
503b1edeb10SPaul Moore  *
504b1edeb10SPaul Moore  * Description:
505b1edeb10SPaul Moore  * Removes a DOI definition from the CIPSO engine.  The NetLabel routines will
506b1edeb10SPaul Moore  * be called to release their own LSM domain mappings as well as our own
507b1edeb10SPaul Moore  * domain list.  Returns zero on success and negative values on failure.
508b1edeb10SPaul Moore  *
509b1edeb10SPaul Moore  */
cipso_v4_doi_remove(u32 doi,struct netlbl_audit * audit_info)510b1edeb10SPaul Moore int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
511b1edeb10SPaul Moore {
5126c2e8ac0SPaul Moore 	int ret_val;
513b1edeb10SPaul Moore 	struct cipso_v4_doi *doi_def;
5146c2e8ac0SPaul Moore 	struct audit_buffer *audit_buf;
515b1edeb10SPaul Moore 
516b1edeb10SPaul Moore 	spin_lock(&cipso_v4_doi_list_lock);
517b1edeb10SPaul Moore 	doi_def = cipso_v4_doi_search(doi);
51851456b29SIan Morris 	if (!doi_def) {
519b1edeb10SPaul Moore 		spin_unlock(&cipso_v4_doi_list_lock);
5206c2e8ac0SPaul Moore 		ret_val = -ENOENT;
5216c2e8ac0SPaul Moore 		goto doi_remove_return;
522b1edeb10SPaul Moore 	}
523b1edeb10SPaul Moore 	list_del_rcu(&doi_def->list);
524b1edeb10SPaul Moore 	spin_unlock(&cipso_v4_doi_list_lock);
525b1edeb10SPaul Moore 
526ad5d07f4SPaul Moore 	cipso_v4_doi_putdef(doi_def);
5276c2e8ac0SPaul Moore 	ret_val = 0;
528b1edeb10SPaul Moore 
5296c2e8ac0SPaul Moore doi_remove_return:
5306c2e8ac0SPaul Moore 	audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info);
53100db4124SIan Morris 	if (audit_buf) {
5326c2e8ac0SPaul Moore 		audit_log_format(audit_buf,
5336c2e8ac0SPaul Moore 				 " cipso_doi=%u res=%u",
5346c2e8ac0SPaul Moore 				 doi, ret_val == 0 ? 1 : 0);
5356c2e8ac0SPaul Moore 		audit_log_end(audit_buf);
5366c2e8ac0SPaul Moore 	}
5376c2e8ac0SPaul Moore 
5386c2e8ac0SPaul Moore 	return ret_val;
539b1edeb10SPaul Moore }
540b1edeb10SPaul Moore 
541b1edeb10SPaul Moore /**
542b1edeb10SPaul Moore  * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
543446fda4fSPaul Moore  * @doi: the DOI value
544446fda4fSPaul Moore  *
545446fda4fSPaul Moore  * Description:
546446fda4fSPaul Moore  * Searches for a valid DOI definition and if one is found it is returned to
547446fda4fSPaul Moore  * the caller.  Otherwise NULL is returned.  The caller must ensure that
548b1edeb10SPaul Moore  * rcu_read_lock() is held while accessing the returned definition and the DOI
549b1edeb10SPaul Moore  * definition reference count is decremented when the caller is done.
550446fda4fSPaul Moore  *
551446fda4fSPaul Moore  */
cipso_v4_doi_getdef(u32 doi)552446fda4fSPaul Moore struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
553446fda4fSPaul Moore {
554b1edeb10SPaul Moore 	struct cipso_v4_doi *doi_def;
555b1edeb10SPaul Moore 
556b1edeb10SPaul Moore 	rcu_read_lock();
557b1edeb10SPaul Moore 	doi_def = cipso_v4_doi_search(doi);
55851456b29SIan Morris 	if (!doi_def)
559b1edeb10SPaul Moore 		goto doi_getdef_return;
560f6a6fedeSReshetova, Elena 	if (!refcount_inc_not_zero(&doi_def->refcount))
561b1edeb10SPaul Moore 		doi_def = NULL;
562b1edeb10SPaul Moore 
563b1edeb10SPaul Moore doi_getdef_return:
564b1edeb10SPaul Moore 	rcu_read_unlock();
565b1edeb10SPaul Moore 	return doi_def;
566b1edeb10SPaul Moore }
567b1edeb10SPaul Moore 
568b1edeb10SPaul Moore /**
569b1edeb10SPaul Moore  * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
570b1edeb10SPaul Moore  * @doi_def: the DOI definition
571b1edeb10SPaul Moore  *
572b1edeb10SPaul Moore  * Description:
573b1edeb10SPaul Moore  * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
574b1edeb10SPaul Moore  *
575b1edeb10SPaul Moore  */
cipso_v4_doi_putdef(struct cipso_v4_doi * doi_def)576b1edeb10SPaul Moore void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
577b1edeb10SPaul Moore {
57851456b29SIan Morris 	if (!doi_def)
579b1edeb10SPaul Moore 		return;
580b1edeb10SPaul Moore 
581f6a6fedeSReshetova, Elena 	if (!refcount_dec_and_test(&doi_def->refcount))
582b1edeb10SPaul Moore 		return;
583b1edeb10SPaul Moore 
584b1edeb10SPaul Moore 	cipso_v4_cache_invalidate();
585b1edeb10SPaul Moore 	call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
586446fda4fSPaul Moore }
587446fda4fSPaul Moore 
588446fda4fSPaul Moore /**
589fcd48280SPaul Moore  * cipso_v4_doi_walk - Iterate through the DOI definitions
590fcd48280SPaul Moore  * @skip_cnt: skip past this number of DOI definitions, updated
591fcd48280SPaul Moore  * @callback: callback for each DOI definition
592fcd48280SPaul Moore  * @cb_arg: argument for the callback function
593446fda4fSPaul Moore  *
594446fda4fSPaul Moore  * Description:
595fcd48280SPaul Moore  * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
596fcd48280SPaul Moore  * For each entry call @callback, if @callback returns a negative value stop
597fcd48280SPaul Moore  * 'walking' through the list and return.  Updates the value in @skip_cnt upon
598fcd48280SPaul Moore  * return.  Returns zero on success, negative values on failure.
599446fda4fSPaul Moore  *
600446fda4fSPaul Moore  */
cipso_v4_doi_walk(u32 * skip_cnt,int (* callback)(struct cipso_v4_doi * doi_def,void * arg),void * cb_arg)601fcd48280SPaul Moore int cipso_v4_doi_walk(u32 *skip_cnt,
602fcd48280SPaul Moore 		     int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
603fcd48280SPaul Moore 		     void *cb_arg)
604446fda4fSPaul Moore {
605fcd48280SPaul Moore 	int ret_val = -ENOENT;
606446fda4fSPaul Moore 	u32 doi_cnt = 0;
607fcd48280SPaul Moore 	struct cipso_v4_doi *iter_doi;
608446fda4fSPaul Moore 
609446fda4fSPaul Moore 	rcu_read_lock();
610fcd48280SPaul Moore 	list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
611f6a6fedeSReshetova, Elena 		if (refcount_read(&iter_doi->refcount) > 0) {
612fcd48280SPaul Moore 			if (doi_cnt++ < *skip_cnt)
613fcd48280SPaul Moore 				continue;
614fcd48280SPaul Moore 			ret_val = callback(iter_doi, cb_arg);
615fcd48280SPaul Moore 			if (ret_val < 0) {
616fcd48280SPaul Moore 				doi_cnt--;
617fcd48280SPaul Moore 				goto doi_walk_return;
618446fda4fSPaul Moore 			}
619446fda4fSPaul Moore 		}
620446fda4fSPaul Moore 
621fcd48280SPaul Moore doi_walk_return:
622446fda4fSPaul Moore 	rcu_read_unlock();
623fcd48280SPaul Moore 	*skip_cnt = doi_cnt;
624fcd48280SPaul Moore 	return ret_val;
625446fda4fSPaul Moore }
626446fda4fSPaul Moore 
627446fda4fSPaul Moore /*
628446fda4fSPaul Moore  * Label Mapping Functions
629446fda4fSPaul Moore  */
630446fda4fSPaul Moore 
631446fda4fSPaul Moore /**
632446fda4fSPaul Moore  * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
633446fda4fSPaul Moore  * @doi_def: the DOI definition
634446fda4fSPaul Moore  * @level: the level to check
635446fda4fSPaul Moore  *
636446fda4fSPaul Moore  * Description:
637446fda4fSPaul Moore  * Checks the given level against the given DOI definition and returns a
638446fda4fSPaul Moore  * negative value if the level does not have a valid mapping and a zero value
639446fda4fSPaul Moore  * if the level is defined by the DOI.
640446fda4fSPaul Moore  *
641446fda4fSPaul Moore  */
cipso_v4_map_lvl_valid(const struct cipso_v4_doi * doi_def,u8 level)642446fda4fSPaul Moore static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
643446fda4fSPaul Moore {
644446fda4fSPaul Moore 	switch (doi_def->type) {
645446fda4fSPaul Moore 	case CIPSO_V4_MAP_PASS:
646446fda4fSPaul Moore 		return 0;
64715c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
6485578de48SPaul Moore 		if ((level < doi_def->map.std->lvl.cipso_size) &&
6495578de48SPaul Moore 		    (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL))
650446fda4fSPaul Moore 			return 0;
651446fda4fSPaul Moore 		break;
652446fda4fSPaul Moore 	}
653446fda4fSPaul Moore 
654446fda4fSPaul Moore 	return -EFAULT;
655446fda4fSPaul Moore }
656446fda4fSPaul Moore 
657446fda4fSPaul Moore /**
658446fda4fSPaul Moore  * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
659446fda4fSPaul Moore  * @doi_def: the DOI definition
660446fda4fSPaul Moore  * @host_lvl: the host MLS level
661446fda4fSPaul Moore  * @net_lvl: the network/CIPSO MLS level
662446fda4fSPaul Moore  *
663446fda4fSPaul Moore  * Description:
664446fda4fSPaul Moore  * Perform a label mapping to translate a local MLS level to the correct
665446fda4fSPaul Moore  * CIPSO level using the given DOI definition.  Returns zero on success,
666446fda4fSPaul Moore  * negative values otherwise.
667446fda4fSPaul Moore  *
668446fda4fSPaul Moore  */
cipso_v4_map_lvl_hton(const struct cipso_v4_doi * doi_def,u32 host_lvl,u32 * net_lvl)669446fda4fSPaul Moore static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
670446fda4fSPaul Moore 				 u32 host_lvl,
671446fda4fSPaul Moore 				 u32 *net_lvl)
672446fda4fSPaul Moore {
673446fda4fSPaul Moore 	switch (doi_def->type) {
674446fda4fSPaul Moore 	case CIPSO_V4_MAP_PASS:
675446fda4fSPaul Moore 		*net_lvl = host_lvl;
676446fda4fSPaul Moore 		return 0;
67715c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
678c6387a86SPaul Moore 		if (host_lvl < doi_def->map.std->lvl.local_size &&
679c6387a86SPaul Moore 		    doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
680446fda4fSPaul Moore 			*net_lvl = doi_def->map.std->lvl.local[host_lvl];
681446fda4fSPaul Moore 			return 0;
682446fda4fSPaul Moore 		}
683c6387a86SPaul Moore 		return -EPERM;
684446fda4fSPaul Moore 	}
685446fda4fSPaul Moore 
686446fda4fSPaul Moore 	return -EINVAL;
687446fda4fSPaul Moore }
688446fda4fSPaul Moore 
689446fda4fSPaul Moore /**
690446fda4fSPaul Moore  * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
691446fda4fSPaul Moore  * @doi_def: the DOI definition
692446fda4fSPaul Moore  * @net_lvl: the network/CIPSO MLS level
693446fda4fSPaul Moore  * @host_lvl: the host MLS level
694446fda4fSPaul Moore  *
695446fda4fSPaul Moore  * Description:
696446fda4fSPaul Moore  * Perform a label mapping to translate a CIPSO level to the correct local MLS
697446fda4fSPaul Moore  * level using the given DOI definition.  Returns zero on success, negative
698446fda4fSPaul Moore  * values otherwise.
699446fda4fSPaul Moore  *
700446fda4fSPaul Moore  */
cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi * doi_def,u32 net_lvl,u32 * host_lvl)701446fda4fSPaul Moore static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
702446fda4fSPaul Moore 				 u32 net_lvl,
703446fda4fSPaul Moore 				 u32 *host_lvl)
704446fda4fSPaul Moore {
705446fda4fSPaul Moore 	struct cipso_v4_std_map_tbl *map_tbl;
706446fda4fSPaul Moore 
707446fda4fSPaul Moore 	switch (doi_def->type) {
708446fda4fSPaul Moore 	case CIPSO_V4_MAP_PASS:
709446fda4fSPaul Moore 		*host_lvl = net_lvl;
710446fda4fSPaul Moore 		return 0;
71115c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
712446fda4fSPaul Moore 		map_tbl = doi_def->map.std;
713446fda4fSPaul Moore 		if (net_lvl < map_tbl->lvl.cipso_size &&
714446fda4fSPaul Moore 		    map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
715446fda4fSPaul Moore 			*host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
716446fda4fSPaul Moore 			return 0;
717446fda4fSPaul Moore 		}
718c6387a86SPaul Moore 		return -EPERM;
719446fda4fSPaul Moore 	}
720446fda4fSPaul Moore 
721446fda4fSPaul Moore 	return -EINVAL;
722446fda4fSPaul Moore }
723446fda4fSPaul Moore 
724446fda4fSPaul Moore /**
725446fda4fSPaul Moore  * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
726446fda4fSPaul Moore  * @doi_def: the DOI definition
727446fda4fSPaul Moore  * @bitmap: category bitmap
728446fda4fSPaul Moore  * @bitmap_len: bitmap length in bytes
729446fda4fSPaul Moore  *
730446fda4fSPaul Moore  * Description:
731446fda4fSPaul Moore  * Checks the given category bitmap against the given DOI definition and
732446fda4fSPaul Moore  * returns a negative value if any of the categories in the bitmap do not have
733446fda4fSPaul Moore  * a valid mapping and a zero value if all of the categories are valid.
734446fda4fSPaul Moore  *
735446fda4fSPaul Moore  */
cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi * doi_def,const unsigned char * bitmap,u32 bitmap_len)736446fda4fSPaul Moore static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
737446fda4fSPaul Moore 				      const unsigned char *bitmap,
738446fda4fSPaul Moore 				      u32 bitmap_len)
739446fda4fSPaul Moore {
740446fda4fSPaul Moore 	int cat = -1;
741446fda4fSPaul Moore 	u32 bitmap_len_bits = bitmap_len * 8;
742044a68edSPaul Moore 	u32 cipso_cat_size;
743044a68edSPaul Moore 	u32 *cipso_array;
744446fda4fSPaul Moore 
745446fda4fSPaul Moore 	switch (doi_def->type) {
746446fda4fSPaul Moore 	case CIPSO_V4_MAP_PASS:
747446fda4fSPaul Moore 		return 0;
74815c45f7bSPaul Moore 	case CIPSO_V4_MAP_TRANS:
749044a68edSPaul Moore 		cipso_cat_size = doi_def->map.std->cat.cipso_size;
750044a68edSPaul Moore 		cipso_array = doi_def->map.std->cat.cipso;
751446fda4fSPaul Moore 		for (;;) {
7523faa8f98SHuw Davies 			cat = netlbl_bitmap_walk(bitmap,
753446fda4fSPaul Moore 						 bitmap_len_bits,
754446fda4fSPaul Moore 						 cat + 1,
755446fda4fSPaul Moore 						 1);
756446fda4fSPaul Moore 			if (cat < 0)
757446fda4fSPaul Moore 				break;
758446fda4fSPaul Moore 			if (cat >= cipso_cat_size ||
759446fda4fSPaul Moore 			    cipso_array[cat] >= CIPSO_V4_INV_CAT)
760446fda4fSPaul Moore 				return -EFAULT;
761446fda4fSPaul Moore 		}
762446fda4fSPaul Moore 
763446fda4fSPaul Moore 		if (cat == -1)
764446fda4fSPaul Moore 			return 0;
765446fda4fSPaul Moore 		break;
766446fda4fSPaul Moore 	}
767446fda4fSPaul Moore 
768446fda4fSPaul Moore 	return -EFAULT;
769446fda4fSPaul Moore }
770446fda4fSPaul Moore 
771446fda4fSPaul Moore /**
772446fda4fSPaul Moore  * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
773446fda4fSPaul Moore  * @doi_def: the DOI definition
77402752760SPaul Moore  * @secattr: the security attributes
775446fda4fSPaul Moore  * @net_cat: the zero'd out category bitmap in network/CIPSO format
776446fda4fSPaul Moore  * @net_cat_len: the length of the CIPSO bitmap in bytes
777446fda4fSPaul Moore  *
778446fda4fSPaul Moore  * Description:
779446fda4fSPaul Moore  * Perform a label mapping to translate a local MLS category bitmap to the
780446fda4fSPaul Moore  * correct CIPSO bitmap using the given DOI definition.  Returns the minimum
781446fda4fSPaul Moore  * size in bytes of the network bitmap on success, negative values otherwise.
782446fda4fSPaul Moore  *
783446fda4fSPaul Moore  */
cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * net_cat,u32 net_cat_len)784446fda4fSPaul Moore static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
78502752760SPaul Moore 				     const struct netlbl_lsm_secattr *secattr,
786446fda4fSPaul Moore 				     unsigned char *net_cat,
787446fda4fSPaul Moore 				     u32 net_cat_len)
788446fda4fSPaul Moore {
789446fda4fSPaul Moore 	int host_spot = -1;
79002752760SPaul Moore 	u32 net_spot = CIPSO_V4_INV_CAT;
791446fda4fSPaul Moore 	u32 net_spot_max = 0;
792446fda4fSPaul Moore 	u32 net_clen_bits = net_cat_len * 8;
79302752760SPaul Moore 	u32 host_cat_size = 0;
79402752760SPaul Moore 	u32 *host_cat_array = NULL;
79502752760SPaul Moore 
79615c45f7bSPaul Moore 	if (doi_def->type == CIPSO_V4_MAP_TRANS) {
79702752760SPaul Moore 		host_cat_size = doi_def->map.std->cat.local_size;
79802752760SPaul Moore 		host_cat_array = doi_def->map.std->cat.local;
79902752760SPaul Moore 	}
80002752760SPaul Moore 
80102752760SPaul Moore 	for (;;) {
8024fbe63d1SPaul Moore 		host_spot = netlbl_catmap_walk(secattr->attr.mls.cat,
80302752760SPaul Moore 					       host_spot + 1);
80402752760SPaul Moore 		if (host_spot < 0)
80502752760SPaul Moore 			break;
806446fda4fSPaul Moore 
807446fda4fSPaul Moore 		switch (doi_def->type) {
808446fda4fSPaul Moore 		case CIPSO_V4_MAP_PASS:
80902752760SPaul Moore 			net_spot = host_spot;
810446fda4fSPaul Moore 			break;
81115c45f7bSPaul Moore 		case CIPSO_V4_MAP_TRANS:
812446fda4fSPaul Moore 			if (host_spot >= host_cat_size)
813446fda4fSPaul Moore 				return -EPERM;
814446fda4fSPaul Moore 			net_spot = host_cat_array[host_spot];
8159fade4bfSPaul Moore 			if (net_spot >= CIPSO_V4_INV_CAT)
8169fade4bfSPaul Moore 				return -EPERM;
81702752760SPaul Moore 			break;
81802752760SPaul Moore 		}
819446fda4fSPaul Moore 		if (net_spot >= net_clen_bits)
820446fda4fSPaul Moore 			return -ENOSPC;
8213faa8f98SHuw Davies 		netlbl_bitmap_setbit(net_cat, net_spot, 1);
822446fda4fSPaul Moore 
823446fda4fSPaul Moore 		if (net_spot > net_spot_max)
824446fda4fSPaul Moore 			net_spot_max = net_spot;
825446fda4fSPaul Moore 	}
826446fda4fSPaul Moore 
827446fda4fSPaul Moore 	if (++net_spot_max % 8)
828446fda4fSPaul Moore 		return net_spot_max / 8 + 1;
829446fda4fSPaul Moore 	return net_spot_max / 8;
830446fda4fSPaul Moore }
831446fda4fSPaul Moore 
832446fda4fSPaul Moore /**
833446fda4fSPaul Moore  * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
834446fda4fSPaul Moore  * @doi_def: the DOI definition
835446fda4fSPaul Moore  * @net_cat: the category bitmap in network/CIPSO format
836446fda4fSPaul Moore  * @net_cat_len: the length of the CIPSO bitmap in bytes
83702752760SPaul Moore  * @secattr: the security attributes
838446fda4fSPaul Moore  *
839446fda4fSPaul Moore  * Description:
840446fda4fSPaul Moore  * Perform a label mapping to translate a CIPSO bitmap to the correct local
84102752760SPaul Moore  * MLS category bitmap using the given DOI definition.  Returns zero on
84202752760SPaul Moore  * success, negative values on failure.
843446fda4fSPaul Moore  *
844446fda4fSPaul Moore  */
cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi * doi_def,const unsigned char * net_cat,u32 net_cat_len,struct netlbl_lsm_secattr * secattr)845446fda4fSPaul Moore static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
846446fda4fSPaul Moore 				     const unsigned char *net_cat,
847446fda4fSPaul Moore 				     u32 net_cat_len,
84802752760SPaul Moore 				     struct netlbl_lsm_secattr *secattr)
849446fda4fSPaul Moore {
85002752760SPaul Moore 	int ret_val;
851446fda4fSPaul Moore 	int net_spot = -1;
85202752760SPaul Moore 	u32 host_spot = CIPSO_V4_INV_CAT;
853446fda4fSPaul Moore 	u32 net_clen_bits = net_cat_len * 8;
85402752760SPaul Moore 	u32 net_cat_size = 0;
85502752760SPaul Moore 	u32 *net_cat_array = NULL;
856446fda4fSPaul Moore 
85715c45f7bSPaul Moore 	if (doi_def->type == CIPSO_V4_MAP_TRANS) {
858044a68edSPaul Moore 		net_cat_size = doi_def->map.std->cat.cipso_size;
859044a68edSPaul Moore 		net_cat_array = doi_def->map.std->cat.cipso;
86002752760SPaul Moore 	}
86102752760SPaul Moore 
862446fda4fSPaul Moore 	for (;;) {
8633faa8f98SHuw Davies 		net_spot = netlbl_bitmap_walk(net_cat,
864446fda4fSPaul Moore 					      net_clen_bits,
865446fda4fSPaul Moore 					      net_spot + 1,
866446fda4fSPaul Moore 					      1);
8679ff74d77SZhengchao Shao 		if (net_spot < 0)
86802752760SPaul Moore 			return 0;
869446fda4fSPaul Moore 
87002752760SPaul Moore 		switch (doi_def->type) {
87102752760SPaul Moore 		case CIPSO_V4_MAP_PASS:
87202752760SPaul Moore 			host_spot = net_spot;
87302752760SPaul Moore 			break;
87415c45f7bSPaul Moore 		case CIPSO_V4_MAP_TRANS:
87502752760SPaul Moore 			if (net_spot >= net_cat_size)
87602752760SPaul Moore 				return -EPERM;
877446fda4fSPaul Moore 			host_spot = net_cat_array[net_spot];
8789fade4bfSPaul Moore 			if (host_spot >= CIPSO_V4_INV_CAT)
8799fade4bfSPaul Moore 				return -EPERM;
88002752760SPaul Moore 			break;
881446fda4fSPaul Moore 		}
8824fbe63d1SPaul Moore 		ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
88302752760SPaul Moore 						       host_spot,
88402752760SPaul Moore 						       GFP_ATOMIC);
88502752760SPaul Moore 		if (ret_val != 0)
88602752760SPaul Moore 			return ret_val;
887446fda4fSPaul Moore 	}
888446fda4fSPaul Moore 
889446fda4fSPaul Moore 	return -EINVAL;
890446fda4fSPaul Moore }
891446fda4fSPaul Moore 
892654bbc2aSPaul Moore /**
893654bbc2aSPaul Moore  * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
894654bbc2aSPaul Moore  * @doi_def: the DOI definition
895654bbc2aSPaul Moore  * @enumcat: category list
896654bbc2aSPaul Moore  * @enumcat_len: length of the category list in bytes
897654bbc2aSPaul Moore  *
898654bbc2aSPaul Moore  * Description:
899654bbc2aSPaul Moore  * Checks the given categories against the given DOI definition and returns a
900654bbc2aSPaul Moore  * negative value if any of the categories do not have a valid mapping and a
901654bbc2aSPaul Moore  * zero value if all of the categories are valid.
902654bbc2aSPaul Moore  *
903654bbc2aSPaul Moore  */
cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi * doi_def,const unsigned char * enumcat,u32 enumcat_len)904654bbc2aSPaul Moore static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
905654bbc2aSPaul Moore 				       const unsigned char *enumcat,
906654bbc2aSPaul Moore 				       u32 enumcat_len)
907654bbc2aSPaul Moore {
908654bbc2aSPaul Moore 	u16 cat;
909654bbc2aSPaul Moore 	int cat_prev = -1;
910654bbc2aSPaul Moore 	u32 iter;
911654bbc2aSPaul Moore 
912654bbc2aSPaul Moore 	if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
913654bbc2aSPaul Moore 		return -EFAULT;
914654bbc2aSPaul Moore 
915654bbc2aSPaul Moore 	for (iter = 0; iter < enumcat_len; iter += 2) {
916d3e2ce3bSHarvey Harrison 		cat = get_unaligned_be16(&enumcat[iter]);
917654bbc2aSPaul Moore 		if (cat <= cat_prev)
918654bbc2aSPaul Moore 			return -EFAULT;
919654bbc2aSPaul Moore 		cat_prev = cat;
920654bbc2aSPaul Moore 	}
921654bbc2aSPaul Moore 
922654bbc2aSPaul Moore 	return 0;
923654bbc2aSPaul Moore }
924654bbc2aSPaul Moore 
925654bbc2aSPaul Moore /**
926654bbc2aSPaul Moore  * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
927654bbc2aSPaul Moore  * @doi_def: the DOI definition
928654bbc2aSPaul Moore  * @secattr: the security attributes
929654bbc2aSPaul Moore  * @net_cat: the zero'd out category list in network/CIPSO format
930654bbc2aSPaul Moore  * @net_cat_len: the length of the CIPSO category list in bytes
931654bbc2aSPaul Moore  *
932654bbc2aSPaul Moore  * Description:
933654bbc2aSPaul Moore  * Perform a label mapping to translate a local MLS category bitmap to the
934654bbc2aSPaul Moore  * correct CIPSO category list using the given DOI definition.   Returns the
935654bbc2aSPaul Moore  * size in bytes of the network category bitmap on success, negative values
936654bbc2aSPaul Moore  * otherwise.
937654bbc2aSPaul Moore  *
938654bbc2aSPaul Moore  */
cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * net_cat,u32 net_cat_len)939654bbc2aSPaul Moore static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
940654bbc2aSPaul Moore 				      const struct netlbl_lsm_secattr *secattr,
941654bbc2aSPaul Moore 				      unsigned char *net_cat,
942654bbc2aSPaul Moore 				      u32 net_cat_len)
943654bbc2aSPaul Moore {
944654bbc2aSPaul Moore 	int cat = -1;
945654bbc2aSPaul Moore 	u32 cat_iter = 0;
946654bbc2aSPaul Moore 
947654bbc2aSPaul Moore 	for (;;) {
9484fbe63d1SPaul Moore 		cat = netlbl_catmap_walk(secattr->attr.mls.cat, cat + 1);
949654bbc2aSPaul Moore 		if (cat < 0)
950654bbc2aSPaul Moore 			break;
951654bbc2aSPaul Moore 		if ((cat_iter + 2) > net_cat_len)
952654bbc2aSPaul Moore 			return -ENOSPC;
953654bbc2aSPaul Moore 
954654bbc2aSPaul Moore 		*((__be16 *)&net_cat[cat_iter]) = htons(cat);
955654bbc2aSPaul Moore 		cat_iter += 2;
956654bbc2aSPaul Moore 	}
957654bbc2aSPaul Moore 
958654bbc2aSPaul Moore 	return cat_iter;
959654bbc2aSPaul Moore }
960654bbc2aSPaul Moore 
961654bbc2aSPaul Moore /**
962654bbc2aSPaul Moore  * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
963654bbc2aSPaul Moore  * @doi_def: the DOI definition
964654bbc2aSPaul Moore  * @net_cat: the category list in network/CIPSO format
965654bbc2aSPaul Moore  * @net_cat_len: the length of the CIPSO bitmap in bytes
966654bbc2aSPaul Moore  * @secattr: the security attributes
967654bbc2aSPaul Moore  *
968654bbc2aSPaul Moore  * Description:
969654bbc2aSPaul Moore  * Perform a label mapping to translate a CIPSO category list to the correct
970654bbc2aSPaul Moore  * local MLS category bitmap using the given DOI definition.  Returns zero on
971654bbc2aSPaul Moore  * success, negative values on failure.
972654bbc2aSPaul Moore  *
973654bbc2aSPaul Moore  */
cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi * doi_def,const unsigned char * net_cat,u32 net_cat_len,struct netlbl_lsm_secattr * secattr)974654bbc2aSPaul Moore static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
975654bbc2aSPaul Moore 				      const unsigned char *net_cat,
976654bbc2aSPaul Moore 				      u32 net_cat_len,
977654bbc2aSPaul Moore 				      struct netlbl_lsm_secattr *secattr)
978654bbc2aSPaul Moore {
979654bbc2aSPaul Moore 	int ret_val;
980654bbc2aSPaul Moore 	u32 iter;
981654bbc2aSPaul Moore 
982654bbc2aSPaul Moore 	for (iter = 0; iter < net_cat_len; iter += 2) {
9834fbe63d1SPaul Moore 		ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
984d3e2ce3bSHarvey Harrison 					     get_unaligned_be16(&net_cat[iter]),
985654bbc2aSPaul Moore 					     GFP_ATOMIC);
986654bbc2aSPaul Moore 		if (ret_val != 0)
987654bbc2aSPaul Moore 			return ret_val;
988654bbc2aSPaul Moore 	}
989654bbc2aSPaul Moore 
990654bbc2aSPaul Moore 	return 0;
991654bbc2aSPaul Moore }
992654bbc2aSPaul Moore 
993484b3669SPaul Moore /**
994484b3669SPaul Moore  * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
995484b3669SPaul Moore  * @doi_def: the DOI definition
996484b3669SPaul Moore  * @rngcat: category list
997484b3669SPaul Moore  * @rngcat_len: length of the category list in bytes
998484b3669SPaul Moore  *
999484b3669SPaul Moore  * Description:
1000484b3669SPaul Moore  * Checks the given categories against the given DOI definition and returns a
1001484b3669SPaul Moore  * negative value if any of the categories do not have a valid mapping and a
1002484b3669SPaul Moore  * zero value if all of the categories are valid.
1003484b3669SPaul Moore  *
1004484b3669SPaul Moore  */
cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi * doi_def,const unsigned char * rngcat,u32 rngcat_len)1005484b3669SPaul Moore static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1006484b3669SPaul Moore 				      const unsigned char *rngcat,
1007484b3669SPaul Moore 				      u32 rngcat_len)
1008484b3669SPaul Moore {
1009484b3669SPaul Moore 	u16 cat_high;
1010484b3669SPaul Moore 	u16 cat_low;
1011484b3669SPaul Moore 	u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1012484b3669SPaul Moore 	u32 iter;
1013484b3669SPaul Moore 
1014484b3669SPaul Moore 	if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1015484b3669SPaul Moore 		return -EFAULT;
1016484b3669SPaul Moore 
1017484b3669SPaul Moore 	for (iter = 0; iter < rngcat_len; iter += 4) {
1018d3e2ce3bSHarvey Harrison 		cat_high = get_unaligned_be16(&rngcat[iter]);
1019484b3669SPaul Moore 		if ((iter + 4) <= rngcat_len)
1020d3e2ce3bSHarvey Harrison 			cat_low = get_unaligned_be16(&rngcat[iter + 2]);
1021484b3669SPaul Moore 		else
1022484b3669SPaul Moore 			cat_low = 0;
1023484b3669SPaul Moore 
1024484b3669SPaul Moore 		if (cat_high > cat_prev)
1025484b3669SPaul Moore 			return -EFAULT;
1026484b3669SPaul Moore 
1027484b3669SPaul Moore 		cat_prev = cat_low;
1028484b3669SPaul Moore 	}
1029484b3669SPaul Moore 
1030484b3669SPaul Moore 	return 0;
1031484b3669SPaul Moore }
1032484b3669SPaul Moore 
1033484b3669SPaul Moore /**
1034484b3669SPaul Moore  * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1035484b3669SPaul Moore  * @doi_def: the DOI definition
1036484b3669SPaul Moore  * @secattr: the security attributes
1037484b3669SPaul Moore  * @net_cat: the zero'd out category list in network/CIPSO format
1038484b3669SPaul Moore  * @net_cat_len: the length of the CIPSO category list in bytes
1039484b3669SPaul Moore  *
1040484b3669SPaul Moore  * Description:
1041484b3669SPaul Moore  * Perform a label mapping to translate a local MLS category bitmap to the
1042484b3669SPaul Moore  * correct CIPSO category list using the given DOI definition.   Returns the
1043484b3669SPaul Moore  * size in bytes of the network category bitmap on success, negative values
1044484b3669SPaul Moore  * otherwise.
1045484b3669SPaul Moore  *
1046484b3669SPaul Moore  */
cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * net_cat,u32 net_cat_len)1047484b3669SPaul Moore static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1048484b3669SPaul Moore 				     const struct netlbl_lsm_secattr *secattr,
1049484b3669SPaul Moore 				     unsigned char *net_cat,
1050484b3669SPaul Moore 				     u32 net_cat_len)
1051484b3669SPaul Moore {
1052484b3669SPaul Moore 	int iter = -1;
1053f998e8cbSPaul Moore 	u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
1054484b3669SPaul Moore 	u32 array_cnt = 0;
1055484b3669SPaul Moore 	u32 cat_size = 0;
1056484b3669SPaul Moore 
1057f998e8cbSPaul Moore 	/* make sure we don't overflow the 'array[]' variable */
1058128c6b6cSPaul Moore 	if (net_cat_len >
1059128c6b6cSPaul Moore 	    (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1060128c6b6cSPaul Moore 		return -ENOSPC;
1061484b3669SPaul Moore 
1062484b3669SPaul Moore 	for (;;) {
10634fbe63d1SPaul Moore 		iter = netlbl_catmap_walk(secattr->attr.mls.cat, iter + 1);
1064484b3669SPaul Moore 		if (iter < 0)
1065484b3669SPaul Moore 			break;
1066484b3669SPaul Moore 		cat_size += (iter == 0 ? 0 : sizeof(u16));
1067484b3669SPaul Moore 		if (cat_size > net_cat_len)
1068484b3669SPaul Moore 			return -ENOSPC;
1069484b3669SPaul Moore 		array[array_cnt++] = iter;
1070484b3669SPaul Moore 
10714fbe63d1SPaul Moore 		iter = netlbl_catmap_walkrng(secattr->attr.mls.cat, iter);
1072484b3669SPaul Moore 		if (iter < 0)
1073484b3669SPaul Moore 			return -EFAULT;
1074484b3669SPaul Moore 		cat_size += sizeof(u16);
1075484b3669SPaul Moore 		if (cat_size > net_cat_len)
1076484b3669SPaul Moore 			return -ENOSPC;
1077484b3669SPaul Moore 		array[array_cnt++] = iter;
1078484b3669SPaul Moore 	}
1079484b3669SPaul Moore 
1080484b3669SPaul Moore 	for (iter = 0; array_cnt > 0;) {
1081484b3669SPaul Moore 		*((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1082484b3669SPaul Moore 		iter += 2;
1083484b3669SPaul Moore 		array_cnt--;
1084484b3669SPaul Moore 		if (array[array_cnt] != 0) {
1085484b3669SPaul Moore 			*((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1086484b3669SPaul Moore 			iter += 2;
1087484b3669SPaul Moore 		}
1088484b3669SPaul Moore 	}
1089484b3669SPaul Moore 
1090484b3669SPaul Moore 	return cat_size;
1091484b3669SPaul Moore }
1092484b3669SPaul Moore 
1093484b3669SPaul Moore /**
1094484b3669SPaul Moore  * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1095484b3669SPaul Moore  * @doi_def: the DOI definition
1096484b3669SPaul Moore  * @net_cat: the category list in network/CIPSO format
1097484b3669SPaul Moore  * @net_cat_len: the length of the CIPSO bitmap in bytes
1098484b3669SPaul Moore  * @secattr: the security attributes
1099484b3669SPaul Moore  *
1100484b3669SPaul Moore  * Description:
1101484b3669SPaul Moore  * Perform a label mapping to translate a CIPSO category list to the correct
1102484b3669SPaul Moore  * local MLS category bitmap using the given DOI definition.  Returns zero on
1103484b3669SPaul Moore  * success, negative values on failure.
1104484b3669SPaul Moore  *
1105484b3669SPaul Moore  */
cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi * doi_def,const unsigned char * net_cat,u32 net_cat_len,struct netlbl_lsm_secattr * secattr)1106484b3669SPaul Moore static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1107484b3669SPaul Moore 				     const unsigned char *net_cat,
1108484b3669SPaul Moore 				     u32 net_cat_len,
1109484b3669SPaul Moore 				     struct netlbl_lsm_secattr *secattr)
1110484b3669SPaul Moore {
1111484b3669SPaul Moore 	int ret_val;
1112484b3669SPaul Moore 	u32 net_iter;
1113484b3669SPaul Moore 	u16 cat_low;
1114484b3669SPaul Moore 	u16 cat_high;
1115484b3669SPaul Moore 
1116484b3669SPaul Moore 	for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
1117d3e2ce3bSHarvey Harrison 		cat_high = get_unaligned_be16(&net_cat[net_iter]);
1118484b3669SPaul Moore 		if ((net_iter + 4) <= net_cat_len)
1119d3e2ce3bSHarvey Harrison 			cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
1120484b3669SPaul Moore 		else
1121484b3669SPaul Moore 			cat_low = 0;
1122484b3669SPaul Moore 
11234fbe63d1SPaul Moore 		ret_val = netlbl_catmap_setrng(&secattr->attr.mls.cat,
1124484b3669SPaul Moore 					       cat_low,
1125484b3669SPaul Moore 					       cat_high,
1126484b3669SPaul Moore 					       GFP_ATOMIC);
1127484b3669SPaul Moore 		if (ret_val != 0)
1128484b3669SPaul Moore 			return ret_val;
1129484b3669SPaul Moore 	}
1130484b3669SPaul Moore 
1131484b3669SPaul Moore 	return 0;
1132484b3669SPaul Moore }
1133484b3669SPaul Moore 
1134446fda4fSPaul Moore /*
1135446fda4fSPaul Moore  * Protocol Handling Functions
1136446fda4fSPaul Moore  */
1137446fda4fSPaul Moore 
1138446fda4fSPaul Moore /**
1139446fda4fSPaul Moore  * cipso_v4_gentag_hdr - Generate a CIPSO option header
1140446fda4fSPaul Moore  * @doi_def: the DOI definition
114191b1ed0aSPaul Moore  * @len: the total tag length in bytes, not including this header
1142446fda4fSPaul Moore  * @buf: the CIPSO option buffer
1143446fda4fSPaul Moore  *
1144446fda4fSPaul Moore  * Description:
114591b1ed0aSPaul Moore  * Write a CIPSO header into the beginning of @buffer.
1146446fda4fSPaul Moore  *
1147446fda4fSPaul Moore  */
cipso_v4_gentag_hdr(const struct cipso_v4_doi * doi_def,unsigned char * buf,u32 len)114891b1ed0aSPaul Moore static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
114991b1ed0aSPaul Moore 				unsigned char *buf,
115091b1ed0aSPaul Moore 				u32 len)
1151446fda4fSPaul Moore {
1152446fda4fSPaul Moore 	buf[0] = IPOPT_CIPSO;
1153446fda4fSPaul Moore 	buf[1] = CIPSO_V4_HDR_LEN + len;
1154e233febdSSergey Nazarov 	put_unaligned_be32(doi_def->doi, &buf[2]);
1155446fda4fSPaul Moore }
1156446fda4fSPaul Moore 
1157446fda4fSPaul Moore /**
1158446fda4fSPaul Moore  * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1159446fda4fSPaul Moore  * @doi_def: the DOI definition
1160446fda4fSPaul Moore  * @secattr: the security attributes
1161446fda4fSPaul Moore  * @buffer: the option buffer
1162446fda4fSPaul Moore  * @buffer_len: length of buffer in bytes
1163446fda4fSPaul Moore  *
1164446fda4fSPaul Moore  * Description:
1165446fda4fSPaul Moore  * Generate a CIPSO option using the restricted bitmap tag, tag type #1.  The
1166446fda4fSPaul Moore  * actual buffer length may be larger than the indicated size due to
116791b1ed0aSPaul Moore  * translation between host and network category bitmaps.  Returns the size of
116891b1ed0aSPaul Moore  * the tag on success, negative values on failure.
1169446fda4fSPaul Moore  *
1170446fda4fSPaul Moore  */
cipso_v4_gentag_rbm(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * buffer,u32 buffer_len)1171446fda4fSPaul Moore static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1172446fda4fSPaul Moore 			       const struct netlbl_lsm_secattr *secattr,
117391b1ed0aSPaul Moore 			       unsigned char *buffer,
117491b1ed0aSPaul Moore 			       u32 buffer_len)
1175446fda4fSPaul Moore {
1176701a90baSPaul Moore 	int ret_val;
117791b1ed0aSPaul Moore 	u32 tag_len;
1178446fda4fSPaul Moore 	u32 level;
1179446fda4fSPaul Moore 
1180701a90baSPaul Moore 	if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1181701a90baSPaul Moore 		return -EPERM;
1182701a90baSPaul Moore 
118316efd454SPaul Moore 	ret_val = cipso_v4_map_lvl_hton(doi_def,
118416efd454SPaul Moore 					secattr->attr.mls.lvl,
118516efd454SPaul Moore 					&level);
118691b1ed0aSPaul Moore 	if (ret_val != 0)
118791b1ed0aSPaul Moore 		return ret_val;
1188446fda4fSPaul Moore 
118991b1ed0aSPaul Moore 	if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1190446fda4fSPaul Moore 		ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
119102752760SPaul Moore 						    secattr,
119291b1ed0aSPaul Moore 						    &buffer[4],
119391b1ed0aSPaul Moore 						    buffer_len - 4);
1194446fda4fSPaul Moore 		if (ret_val < 0)
119591b1ed0aSPaul Moore 			return ret_val;
1196446fda4fSPaul Moore 
1197446fda4fSPaul Moore 		/* This will send packets using the "optimized" format when
119825985edcSLucas De Marchi 		 * possible as specified in  section 3.4.2.6 of the
1199446fda4fSPaul Moore 		 * CIPSO draft. */
1200dd44f04bSKuniyuki Iwashima 		if (READ_ONCE(cipso_v4_rbm_optfmt) && ret_val > 0 &&
1201dd44f04bSKuniyuki Iwashima 		    ret_val <= 10)
120291b1ed0aSPaul Moore 			tag_len = 14;
1203701a90baSPaul Moore 		else
120491b1ed0aSPaul Moore 			tag_len = 4 + ret_val;
120591b1ed0aSPaul Moore 	} else
120691b1ed0aSPaul Moore 		tag_len = 4;
1207446fda4fSPaul Moore 
120815c45f7bSPaul Moore 	buffer[0] = CIPSO_V4_TAG_RBITMAP;
120991b1ed0aSPaul Moore 	buffer[1] = tag_len;
121091b1ed0aSPaul Moore 	buffer[3] = level;
1211446fda4fSPaul Moore 
121291b1ed0aSPaul Moore 	return tag_len;
1213446fda4fSPaul Moore }
1214446fda4fSPaul Moore 
1215446fda4fSPaul Moore /**
1216446fda4fSPaul Moore  * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1217446fda4fSPaul Moore  * @doi_def: the DOI definition
1218446fda4fSPaul Moore  * @tag: the CIPSO tag
1219446fda4fSPaul Moore  * @secattr: the security attributes
1220446fda4fSPaul Moore  *
1221446fda4fSPaul Moore  * Description:
1222446fda4fSPaul Moore  * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1223446fda4fSPaul Moore  * attributes in @secattr.  Return zero on success, negatives values on
1224446fda4fSPaul Moore  * failure.
1225446fda4fSPaul Moore  *
1226446fda4fSPaul Moore  */
cipso_v4_parsetag_rbm(const struct cipso_v4_doi * doi_def,const unsigned char * tag,struct netlbl_lsm_secattr * secattr)1227446fda4fSPaul Moore static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1228446fda4fSPaul Moore 				 const unsigned char *tag,
1229446fda4fSPaul Moore 				 struct netlbl_lsm_secattr *secattr)
1230446fda4fSPaul Moore {
1231446fda4fSPaul Moore 	int ret_val;
1232446fda4fSPaul Moore 	u8 tag_len = tag[1];
1233446fda4fSPaul Moore 	u32 level;
1234446fda4fSPaul Moore 
1235446fda4fSPaul Moore 	ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1236446fda4fSPaul Moore 	if (ret_val != 0)
1237446fda4fSPaul Moore 		return ret_val;
123816efd454SPaul Moore 	secattr->attr.mls.lvl = level;
1239701a90baSPaul Moore 	secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1240446fda4fSPaul Moore 
1241446fda4fSPaul Moore 	if (tag_len > 4) {
1242446fda4fSPaul Moore 		ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1243446fda4fSPaul Moore 						    &tag[4],
1244446fda4fSPaul Moore 						    tag_len - 4,
124502752760SPaul Moore 						    secattr);
124602752760SPaul Moore 		if (ret_val != 0) {
12474fbe63d1SPaul Moore 			netlbl_catmap_free(secattr->attr.mls.cat);
1248446fda4fSPaul Moore 			return ret_val;
1249701a90baSPaul Moore 		}
125002752760SPaul Moore 
1251eead1c2eSPaolo Abeni 		if (secattr->attr.mls.cat)
125202752760SPaul Moore 			secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1253446fda4fSPaul Moore 	}
1254446fda4fSPaul Moore 
1255446fda4fSPaul Moore 	return 0;
1256446fda4fSPaul Moore }
1257446fda4fSPaul Moore 
1258446fda4fSPaul Moore /**
1259654bbc2aSPaul Moore  * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1260654bbc2aSPaul Moore  * @doi_def: the DOI definition
1261654bbc2aSPaul Moore  * @secattr: the security attributes
1262654bbc2aSPaul Moore  * @buffer: the option buffer
1263654bbc2aSPaul Moore  * @buffer_len: length of buffer in bytes
1264654bbc2aSPaul Moore  *
1265654bbc2aSPaul Moore  * Description:
1266654bbc2aSPaul Moore  * Generate a CIPSO option using the enumerated tag, tag type #2.  Returns the
1267654bbc2aSPaul Moore  * size of the tag on success, negative values on failure.
1268654bbc2aSPaul Moore  *
1269654bbc2aSPaul Moore  */
cipso_v4_gentag_enum(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * buffer,u32 buffer_len)1270654bbc2aSPaul Moore static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1271654bbc2aSPaul Moore 				const struct netlbl_lsm_secattr *secattr,
1272654bbc2aSPaul Moore 				unsigned char *buffer,
1273654bbc2aSPaul Moore 				u32 buffer_len)
1274654bbc2aSPaul Moore {
1275654bbc2aSPaul Moore 	int ret_val;
1276654bbc2aSPaul Moore 	u32 tag_len;
1277654bbc2aSPaul Moore 	u32 level;
1278654bbc2aSPaul Moore 
1279654bbc2aSPaul Moore 	if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1280654bbc2aSPaul Moore 		return -EPERM;
1281654bbc2aSPaul Moore 
128216efd454SPaul Moore 	ret_val = cipso_v4_map_lvl_hton(doi_def,
128316efd454SPaul Moore 					secattr->attr.mls.lvl,
128416efd454SPaul Moore 					&level);
1285654bbc2aSPaul Moore 	if (ret_val != 0)
1286654bbc2aSPaul Moore 		return ret_val;
1287654bbc2aSPaul Moore 
1288654bbc2aSPaul Moore 	if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1289654bbc2aSPaul Moore 		ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1290654bbc2aSPaul Moore 						     secattr,
1291654bbc2aSPaul Moore 						     &buffer[4],
1292654bbc2aSPaul Moore 						     buffer_len - 4);
1293654bbc2aSPaul Moore 		if (ret_val < 0)
1294654bbc2aSPaul Moore 			return ret_val;
1295654bbc2aSPaul Moore 
1296654bbc2aSPaul Moore 		tag_len = 4 + ret_val;
1297654bbc2aSPaul Moore 	} else
1298654bbc2aSPaul Moore 		tag_len = 4;
1299654bbc2aSPaul Moore 
130015c45f7bSPaul Moore 	buffer[0] = CIPSO_V4_TAG_ENUM;
1301654bbc2aSPaul Moore 	buffer[1] = tag_len;
1302654bbc2aSPaul Moore 	buffer[3] = level;
1303654bbc2aSPaul Moore 
1304654bbc2aSPaul Moore 	return tag_len;
1305654bbc2aSPaul Moore }
1306654bbc2aSPaul Moore 
1307654bbc2aSPaul Moore /**
1308654bbc2aSPaul Moore  * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1309654bbc2aSPaul Moore  * @doi_def: the DOI definition
1310654bbc2aSPaul Moore  * @tag: the CIPSO tag
1311654bbc2aSPaul Moore  * @secattr: the security attributes
1312654bbc2aSPaul Moore  *
1313654bbc2aSPaul Moore  * Description:
1314654bbc2aSPaul Moore  * Parse a CIPSO enumerated tag (tag type #2) and return the security
1315654bbc2aSPaul Moore  * attributes in @secattr.  Return zero on success, negatives values on
1316654bbc2aSPaul Moore  * failure.
1317654bbc2aSPaul Moore  *
1318654bbc2aSPaul Moore  */
cipso_v4_parsetag_enum(const struct cipso_v4_doi * doi_def,const unsigned char * tag,struct netlbl_lsm_secattr * secattr)1319654bbc2aSPaul Moore static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1320654bbc2aSPaul Moore 				  const unsigned char *tag,
1321654bbc2aSPaul Moore 				  struct netlbl_lsm_secattr *secattr)
1322654bbc2aSPaul Moore {
1323654bbc2aSPaul Moore 	int ret_val;
1324654bbc2aSPaul Moore 	u8 tag_len = tag[1];
1325654bbc2aSPaul Moore 	u32 level;
1326654bbc2aSPaul Moore 
1327654bbc2aSPaul Moore 	ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1328654bbc2aSPaul Moore 	if (ret_val != 0)
1329654bbc2aSPaul Moore 		return ret_val;
133016efd454SPaul Moore 	secattr->attr.mls.lvl = level;
1331654bbc2aSPaul Moore 	secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1332654bbc2aSPaul Moore 
1333654bbc2aSPaul Moore 	if (tag_len > 4) {
1334654bbc2aSPaul Moore 		ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1335654bbc2aSPaul Moore 						     &tag[4],
1336654bbc2aSPaul Moore 						     tag_len - 4,
1337654bbc2aSPaul Moore 						     secattr);
1338654bbc2aSPaul Moore 		if (ret_val != 0) {
13394fbe63d1SPaul Moore 			netlbl_catmap_free(secattr->attr.mls.cat);
1340654bbc2aSPaul Moore 			return ret_val;
1341654bbc2aSPaul Moore 		}
1342654bbc2aSPaul Moore 
1343654bbc2aSPaul Moore 		secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1344654bbc2aSPaul Moore 	}
1345654bbc2aSPaul Moore 
1346654bbc2aSPaul Moore 	return 0;
1347654bbc2aSPaul Moore }
1348654bbc2aSPaul Moore 
1349654bbc2aSPaul Moore /**
1350484b3669SPaul Moore  * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1351484b3669SPaul Moore  * @doi_def: the DOI definition
1352484b3669SPaul Moore  * @secattr: the security attributes
1353484b3669SPaul Moore  * @buffer: the option buffer
1354484b3669SPaul Moore  * @buffer_len: length of buffer in bytes
1355484b3669SPaul Moore  *
1356484b3669SPaul Moore  * Description:
1357484b3669SPaul Moore  * Generate a CIPSO option using the ranged tag, tag type #5.  Returns the
1358484b3669SPaul Moore  * size of the tag on success, negative values on failure.
1359484b3669SPaul Moore  *
1360484b3669SPaul Moore  */
cipso_v4_gentag_rng(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * buffer,u32 buffer_len)1361484b3669SPaul Moore static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1362484b3669SPaul Moore 			       const struct netlbl_lsm_secattr *secattr,
1363484b3669SPaul Moore 			       unsigned char *buffer,
1364484b3669SPaul Moore 			       u32 buffer_len)
1365484b3669SPaul Moore {
1366484b3669SPaul Moore 	int ret_val;
1367484b3669SPaul Moore 	u32 tag_len;
1368484b3669SPaul Moore 	u32 level;
1369484b3669SPaul Moore 
1370484b3669SPaul Moore 	if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1371484b3669SPaul Moore 		return -EPERM;
1372484b3669SPaul Moore 
137316efd454SPaul Moore 	ret_val = cipso_v4_map_lvl_hton(doi_def,
137416efd454SPaul Moore 					secattr->attr.mls.lvl,
137516efd454SPaul Moore 					&level);
1376484b3669SPaul Moore 	if (ret_val != 0)
1377484b3669SPaul Moore 		return ret_val;
1378484b3669SPaul Moore 
1379484b3669SPaul Moore 	if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1380484b3669SPaul Moore 		ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1381484b3669SPaul Moore 						    secattr,
1382484b3669SPaul Moore 						    &buffer[4],
1383484b3669SPaul Moore 						    buffer_len - 4);
1384484b3669SPaul Moore 		if (ret_val < 0)
1385484b3669SPaul Moore 			return ret_val;
1386484b3669SPaul Moore 
1387484b3669SPaul Moore 		tag_len = 4 + ret_val;
1388484b3669SPaul Moore 	} else
1389484b3669SPaul Moore 		tag_len = 4;
1390484b3669SPaul Moore 
139115c45f7bSPaul Moore 	buffer[0] = CIPSO_V4_TAG_RANGE;
1392484b3669SPaul Moore 	buffer[1] = tag_len;
1393484b3669SPaul Moore 	buffer[3] = level;
1394484b3669SPaul Moore 
1395484b3669SPaul Moore 	return tag_len;
1396484b3669SPaul Moore }
1397484b3669SPaul Moore 
1398484b3669SPaul Moore /**
1399484b3669SPaul Moore  * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1400484b3669SPaul Moore  * @doi_def: the DOI definition
1401484b3669SPaul Moore  * @tag: the CIPSO tag
1402484b3669SPaul Moore  * @secattr: the security attributes
1403484b3669SPaul Moore  *
1404484b3669SPaul Moore  * Description:
1405484b3669SPaul Moore  * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1406484b3669SPaul Moore  * in @secattr.  Return zero on success, negatives values on failure.
1407484b3669SPaul Moore  *
1408484b3669SPaul Moore  */
cipso_v4_parsetag_rng(const struct cipso_v4_doi * doi_def,const unsigned char * tag,struct netlbl_lsm_secattr * secattr)1409484b3669SPaul Moore static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1410484b3669SPaul Moore 				 const unsigned char *tag,
1411484b3669SPaul Moore 				 struct netlbl_lsm_secattr *secattr)
1412484b3669SPaul Moore {
1413484b3669SPaul Moore 	int ret_val;
1414484b3669SPaul Moore 	u8 tag_len = tag[1];
1415484b3669SPaul Moore 	u32 level;
1416484b3669SPaul Moore 
1417484b3669SPaul Moore 	ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1418484b3669SPaul Moore 	if (ret_val != 0)
1419484b3669SPaul Moore 		return ret_val;
142016efd454SPaul Moore 	secattr->attr.mls.lvl = level;
1421484b3669SPaul Moore 	secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1422484b3669SPaul Moore 
1423484b3669SPaul Moore 	if (tag_len > 4) {
1424484b3669SPaul Moore 		ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1425484b3669SPaul Moore 						    &tag[4],
1426484b3669SPaul Moore 						    tag_len - 4,
1427484b3669SPaul Moore 						    secattr);
1428484b3669SPaul Moore 		if (ret_val != 0) {
14294fbe63d1SPaul Moore 			netlbl_catmap_free(secattr->attr.mls.cat);
1430484b3669SPaul Moore 			return ret_val;
1431484b3669SPaul Moore 		}
1432484b3669SPaul Moore 
1433eead1c2eSPaolo Abeni 		if (secattr->attr.mls.cat)
1434484b3669SPaul Moore 			secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1435484b3669SPaul Moore 	}
1436484b3669SPaul Moore 
1437484b3669SPaul Moore 	return 0;
1438484b3669SPaul Moore }
1439484b3669SPaul Moore 
1440484b3669SPaul Moore /**
144115c45f7bSPaul Moore  * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
144215c45f7bSPaul Moore  * @doi_def: the DOI definition
144315c45f7bSPaul Moore  * @secattr: the security attributes
144415c45f7bSPaul Moore  * @buffer: the option buffer
144515c45f7bSPaul Moore  * @buffer_len: length of buffer in bytes
144615c45f7bSPaul Moore  *
144715c45f7bSPaul Moore  * Description:
144815c45f7bSPaul Moore  * Generate a CIPSO option using the local tag.  Returns the size of the tag
144915c45f7bSPaul Moore  * on success, negative values on failure.
145015c45f7bSPaul Moore  *
145115c45f7bSPaul Moore  */
cipso_v4_gentag_loc(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * buffer,u32 buffer_len)145215c45f7bSPaul Moore static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
145315c45f7bSPaul Moore 			       const struct netlbl_lsm_secattr *secattr,
145415c45f7bSPaul Moore 			       unsigned char *buffer,
145515c45f7bSPaul Moore 			       u32 buffer_len)
145615c45f7bSPaul Moore {
145715c45f7bSPaul Moore 	if (!(secattr->flags & NETLBL_SECATTR_SECID))
145815c45f7bSPaul Moore 		return -EPERM;
145915c45f7bSPaul Moore 
146015c45f7bSPaul Moore 	buffer[0] = CIPSO_V4_TAG_LOCAL;
146115c45f7bSPaul Moore 	buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
146215c45f7bSPaul Moore 	*(u32 *)&buffer[2] = secattr->attr.secid;
146315c45f7bSPaul Moore 
146415c45f7bSPaul Moore 	return CIPSO_V4_TAG_LOC_BLEN;
146515c45f7bSPaul Moore }
146615c45f7bSPaul Moore 
146715c45f7bSPaul Moore /**
146815c45f7bSPaul Moore  * cipso_v4_parsetag_loc - Parse a CIPSO local tag
146915c45f7bSPaul Moore  * @doi_def: the DOI definition
147015c45f7bSPaul Moore  * @tag: the CIPSO tag
147115c45f7bSPaul Moore  * @secattr: the security attributes
147215c45f7bSPaul Moore  *
147315c45f7bSPaul Moore  * Description:
147415c45f7bSPaul Moore  * Parse a CIPSO local tag and return the security attributes in @secattr.
147515c45f7bSPaul Moore  * Return zero on success, negatives values on failure.
147615c45f7bSPaul Moore  *
147715c45f7bSPaul Moore  */
cipso_v4_parsetag_loc(const struct cipso_v4_doi * doi_def,const unsigned char * tag,struct netlbl_lsm_secattr * secattr)147815c45f7bSPaul Moore static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
147915c45f7bSPaul Moore 				 const unsigned char *tag,
148015c45f7bSPaul Moore 				 struct netlbl_lsm_secattr *secattr)
148115c45f7bSPaul Moore {
148215c45f7bSPaul Moore 	secattr->attr.secid = *(u32 *)&tag[2];
148315c45f7bSPaul Moore 	secattr->flags |= NETLBL_SECATTR_SECID;
148415c45f7bSPaul Moore 
148515c45f7bSPaul Moore 	return 0;
148615c45f7bSPaul Moore }
148715c45f7bSPaul Moore 
148815c45f7bSPaul Moore /**
148904f81f01SPaul Moore  * cipso_v4_optptr - Find the CIPSO option in the packet
149004f81f01SPaul Moore  * @skb: the packet
149104f81f01SPaul Moore  *
149204f81f01SPaul Moore  * Description:
149304f81f01SPaul Moore  * Parse the packet's IP header looking for a CIPSO option.  Returns a pointer
1494076ed3daSStefan Nuernberger  * to the start of the CIPSO option on success, NULL if one is not found.
149504f81f01SPaul Moore  *
149604f81f01SPaul Moore  */
cipso_v4_optptr(const struct sk_buff * skb)149704f81f01SPaul Moore unsigned char *cipso_v4_optptr(const struct sk_buff *skb)
149804f81f01SPaul Moore {
149904f81f01SPaul Moore 	const struct iphdr *iph = ip_hdr(skb);
150004f81f01SPaul Moore 	unsigned char *optptr = (unsigned char *)&(ip_hdr(skb)[1]);
150104f81f01SPaul Moore 	int optlen;
150204f81f01SPaul Moore 	int taglen;
150304f81f01SPaul Moore 
1504076ed3daSStefan Nuernberger 	for (optlen = iph->ihl*4 - sizeof(struct iphdr); optlen > 1; ) {
150540413955Syujuan.qi 		switch (optptr[0]) {
150640413955Syujuan.qi 		case IPOPT_END:
150740413955Syujuan.qi 			return NULL;
150840413955Syujuan.qi 		case IPOPT_NOOP:
150940413955Syujuan.qi 			taglen = 1;
151040413955Syujuan.qi 			break;
151140413955Syujuan.qi 		default:
151204f81f01SPaul Moore 			taglen = optptr[1];
151340413955Syujuan.qi 		}
1514076ed3daSStefan Nuernberger 		if (!taglen || taglen > optlen)
1515076ed3daSStefan Nuernberger 			return NULL;
1516076ed3daSStefan Nuernberger 		if (optptr[0] == IPOPT_CIPSO)
1517076ed3daSStefan Nuernberger 			return optptr;
1518076ed3daSStefan Nuernberger 
151904f81f01SPaul Moore 		optlen -= taglen;
152004f81f01SPaul Moore 		optptr += taglen;
152104f81f01SPaul Moore 	}
152204f81f01SPaul Moore 
152304f81f01SPaul Moore 	return NULL;
152404f81f01SPaul Moore }
152504f81f01SPaul Moore 
152604f81f01SPaul Moore /**
1527446fda4fSPaul Moore  * cipso_v4_validate - Validate a CIPSO option
15283628e3cbSAndrew Lunn  * @skb: the packet
1529446fda4fSPaul Moore  * @option: the start of the option, on error it is set to point to the error
1530446fda4fSPaul Moore  *
1531446fda4fSPaul Moore  * Description:
1532446fda4fSPaul Moore  * This routine is called to validate a CIPSO option, it checks all of the
1533446fda4fSPaul Moore  * fields to ensure that they are at least valid, see the draft snippet below
1534446fda4fSPaul Moore  * for details.  If the option is valid then a zero value is returned and
1535446fda4fSPaul Moore  * the value of @option is unchanged.  If the option is invalid then a
1536446fda4fSPaul Moore  * non-zero value is returned and @option is adjusted to point to the
1537446fda4fSPaul Moore  * offending portion of the option.  From the IETF draft ...
1538446fda4fSPaul Moore  *
1539446fda4fSPaul Moore  *  "If any field within the CIPSO options, such as the DOI identifier, is not
1540446fda4fSPaul Moore  *   recognized the IP datagram is discarded and an ICMP 'parameter problem'
1541446fda4fSPaul Moore  *   (type 12) is generated and returned.  The ICMP code field is set to 'bad
1542446fda4fSPaul Moore  *   parameter' (code 0) and the pointer is set to the start of the CIPSO field
1543446fda4fSPaul Moore  *   that is unrecognized."
1544446fda4fSPaul Moore  *
1545446fda4fSPaul Moore  */
cipso_v4_validate(const struct sk_buff * skb,unsigned char ** option)154615c45f7bSPaul Moore int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
1547446fda4fSPaul Moore {
1548446fda4fSPaul Moore 	unsigned char *opt = *option;
1549446fda4fSPaul Moore 	unsigned char *tag;
1550446fda4fSPaul Moore 	unsigned char opt_iter;
1551446fda4fSPaul Moore 	unsigned char err_offset = 0;
1552446fda4fSPaul Moore 	u8 opt_len;
1553446fda4fSPaul Moore 	u8 tag_len;
1554446fda4fSPaul Moore 	struct cipso_v4_doi *doi_def = NULL;
1555446fda4fSPaul Moore 	u32 tag_iter;
1556446fda4fSPaul Moore 
1557446fda4fSPaul Moore 	/* caller already checks for length values that are too large */
1558446fda4fSPaul Moore 	opt_len = opt[1];
1559446fda4fSPaul Moore 	if (opt_len < 8) {
1560446fda4fSPaul Moore 		err_offset = 1;
1561446fda4fSPaul Moore 		goto validate_return;
1562446fda4fSPaul Moore 	}
1563446fda4fSPaul Moore 
1564446fda4fSPaul Moore 	rcu_read_lock();
1565d3e2ce3bSHarvey Harrison 	doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
156651456b29SIan Morris 	if (!doi_def) {
1567446fda4fSPaul Moore 		err_offset = 2;
1568446fda4fSPaul Moore 		goto validate_return_locked;
1569446fda4fSPaul Moore 	}
1570446fda4fSPaul Moore 
157115c45f7bSPaul Moore 	opt_iter = CIPSO_V4_HDR_LEN;
1572446fda4fSPaul Moore 	tag = opt + opt_iter;
1573446fda4fSPaul Moore 	while (opt_iter < opt_len) {
1574446fda4fSPaul Moore 		for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1575446fda4fSPaul Moore 			if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1576446fda4fSPaul Moore 			    ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1577446fda4fSPaul Moore 				err_offset = opt_iter;
1578446fda4fSPaul Moore 				goto validate_return_locked;
1579446fda4fSPaul Moore 			}
1580446fda4fSPaul Moore 
1581d71b7896SEric Dumazet 		if (opt_iter + 1 == opt_len) {
1582d71b7896SEric Dumazet 			err_offset = opt_iter;
1583d71b7896SEric Dumazet 			goto validate_return_locked;
1584d71b7896SEric Dumazet 		}
1585446fda4fSPaul Moore 		tag_len = tag[1];
1586446fda4fSPaul Moore 		if (tag_len > (opt_len - opt_iter)) {
1587446fda4fSPaul Moore 			err_offset = opt_iter + 1;
1588446fda4fSPaul Moore 			goto validate_return_locked;
1589446fda4fSPaul Moore 		}
1590446fda4fSPaul Moore 
1591446fda4fSPaul Moore 		switch (tag[0]) {
1592446fda4fSPaul Moore 		case CIPSO_V4_TAG_RBITMAP:
159315c45f7bSPaul Moore 			if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
1594446fda4fSPaul Moore 				err_offset = opt_iter + 1;
1595446fda4fSPaul Moore 				goto validate_return_locked;
1596446fda4fSPaul Moore 			}
1597446fda4fSPaul Moore 
1598446fda4fSPaul Moore 			/* We are already going to do all the verification
1599446fda4fSPaul Moore 			 * necessary at the socket layer so from our point of
1600446fda4fSPaul Moore 			 * view it is safe to turn these checks off (and less
1601446fda4fSPaul Moore 			 * work), however, the CIPSO draft says we should do
1602446fda4fSPaul Moore 			 * all the CIPSO validations here but it doesn't
1603446fda4fSPaul Moore 			 * really specify _exactly_ what we need to validate
1604446fda4fSPaul Moore 			 * ... so, just make it a sysctl tunable. */
1605dd44f04bSKuniyuki Iwashima 			if (READ_ONCE(cipso_v4_rbm_strictvalid)) {
1606446fda4fSPaul Moore 				if (cipso_v4_map_lvl_valid(doi_def,
1607446fda4fSPaul Moore 							   tag[3]) < 0) {
1608446fda4fSPaul Moore 					err_offset = opt_iter + 3;
1609446fda4fSPaul Moore 					goto validate_return_locked;
1610446fda4fSPaul Moore 				}
161115c45f7bSPaul Moore 				if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
1612446fda4fSPaul Moore 				    cipso_v4_map_cat_rbm_valid(doi_def,
1613446fda4fSPaul Moore 							    &tag[4],
1614446fda4fSPaul Moore 							    tag_len - 4) < 0) {
1615446fda4fSPaul Moore 					err_offset = opt_iter + 4;
1616446fda4fSPaul Moore 					goto validate_return_locked;
1617446fda4fSPaul Moore 				}
1618446fda4fSPaul Moore 			}
1619446fda4fSPaul Moore 			break;
1620654bbc2aSPaul Moore 		case CIPSO_V4_TAG_ENUM:
162115c45f7bSPaul Moore 			if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
1622654bbc2aSPaul Moore 				err_offset = opt_iter + 1;
1623654bbc2aSPaul Moore 				goto validate_return_locked;
1624654bbc2aSPaul Moore 			}
1625654bbc2aSPaul Moore 
1626654bbc2aSPaul Moore 			if (cipso_v4_map_lvl_valid(doi_def,
1627654bbc2aSPaul Moore 						   tag[3]) < 0) {
1628654bbc2aSPaul Moore 				err_offset = opt_iter + 3;
1629654bbc2aSPaul Moore 				goto validate_return_locked;
1630654bbc2aSPaul Moore 			}
163115c45f7bSPaul Moore 			if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
1632654bbc2aSPaul Moore 			    cipso_v4_map_cat_enum_valid(doi_def,
1633654bbc2aSPaul Moore 							&tag[4],
1634654bbc2aSPaul Moore 							tag_len - 4) < 0) {
1635654bbc2aSPaul Moore 				err_offset = opt_iter + 4;
1636654bbc2aSPaul Moore 				goto validate_return_locked;
1637654bbc2aSPaul Moore 			}
1638654bbc2aSPaul Moore 			break;
1639484b3669SPaul Moore 		case CIPSO_V4_TAG_RANGE:
164015c45f7bSPaul Moore 			if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
1641484b3669SPaul Moore 				err_offset = opt_iter + 1;
1642484b3669SPaul Moore 				goto validate_return_locked;
1643484b3669SPaul Moore 			}
1644484b3669SPaul Moore 
1645484b3669SPaul Moore 			if (cipso_v4_map_lvl_valid(doi_def,
1646484b3669SPaul Moore 						   tag[3]) < 0) {
1647484b3669SPaul Moore 				err_offset = opt_iter + 3;
1648484b3669SPaul Moore 				goto validate_return_locked;
1649484b3669SPaul Moore 			}
165015c45f7bSPaul Moore 			if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
1651484b3669SPaul Moore 			    cipso_v4_map_cat_rng_valid(doi_def,
1652484b3669SPaul Moore 						       &tag[4],
1653484b3669SPaul Moore 						       tag_len - 4) < 0) {
1654484b3669SPaul Moore 				err_offset = opt_iter + 4;
1655484b3669SPaul Moore 				goto validate_return_locked;
1656484b3669SPaul Moore 			}
1657484b3669SPaul Moore 			break;
165815c45f7bSPaul Moore 		case CIPSO_V4_TAG_LOCAL:
165915c45f7bSPaul Moore 			/* This is a non-standard tag that we only allow for
166015c45f7bSPaul Moore 			 * local connections, so if the incoming interface is
166189d7ae34SPaul Moore 			 * not the loopback device drop the packet. Further,
166289d7ae34SPaul Moore 			 * there is no legitimate reason for setting this from
166389d7ae34SPaul Moore 			 * userspace so reject it if skb is NULL. */
166451456b29SIan Morris 			if (!skb || !(skb->dev->flags & IFF_LOOPBACK)) {
166515c45f7bSPaul Moore 				err_offset = opt_iter;
166615c45f7bSPaul Moore 				goto validate_return_locked;
166715c45f7bSPaul Moore 			}
166815c45f7bSPaul Moore 			if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
166915c45f7bSPaul Moore 				err_offset = opt_iter + 1;
167015c45f7bSPaul Moore 				goto validate_return_locked;
167115c45f7bSPaul Moore 			}
167215c45f7bSPaul Moore 			break;
1673446fda4fSPaul Moore 		default:
1674446fda4fSPaul Moore 			err_offset = opt_iter;
1675446fda4fSPaul Moore 			goto validate_return_locked;
1676446fda4fSPaul Moore 		}
1677446fda4fSPaul Moore 
1678446fda4fSPaul Moore 		tag += tag_len;
1679446fda4fSPaul Moore 		opt_iter += tag_len;
1680446fda4fSPaul Moore 	}
1681446fda4fSPaul Moore 
1682446fda4fSPaul Moore validate_return_locked:
1683446fda4fSPaul Moore 	rcu_read_unlock();
1684446fda4fSPaul Moore validate_return:
1685446fda4fSPaul Moore 	*option = opt + err_offset;
1686446fda4fSPaul Moore 	return err_offset;
1687446fda4fSPaul Moore }
1688446fda4fSPaul Moore 
1689446fda4fSPaul Moore /**
169025985edcSLucas De Marchi  * cipso_v4_error - Send the correct response for a bad packet
1691446fda4fSPaul Moore  * @skb: the packet
1692446fda4fSPaul Moore  * @error: the error code
1693446fda4fSPaul Moore  * @gateway: CIPSO gateway flag
1694446fda4fSPaul Moore  *
1695446fda4fSPaul Moore  * Description:
1696446fda4fSPaul Moore  * Based on the error code given in @error, send an ICMP error message back to
1697446fda4fSPaul Moore  * the originating host.  From the IETF draft ...
1698446fda4fSPaul Moore  *
1699446fda4fSPaul Moore  *  "If the contents of the CIPSO [option] are valid but the security label is
1700446fda4fSPaul Moore  *   outside of the configured host or port label range, the datagram is
1701446fda4fSPaul Moore  *   discarded and an ICMP 'destination unreachable' (type 3) is generated and
1702446fda4fSPaul Moore  *   returned.  The code field of the ICMP is set to 'communication with
1703446fda4fSPaul Moore  *   destination network administratively prohibited' (code 9) or to
1704446fda4fSPaul Moore  *   'communication with destination host administratively prohibited'
1705446fda4fSPaul Moore  *   (code 10).  The value of the code is dependent on whether the originator
1706446fda4fSPaul Moore  *   of the ICMP message is acting as a CIPSO host or a CIPSO gateway.  The
1707446fda4fSPaul Moore  *   recipient of the ICMP message MUST be able to handle either value.  The
1708446fda4fSPaul Moore  *   same procedure is performed if a CIPSO [option] can not be added to an
1709446fda4fSPaul Moore  *   IP packet because it is too large to fit in the IP options area."
1710446fda4fSPaul Moore  *
1711446fda4fSPaul Moore  *  "If the error is triggered by receipt of an ICMP message, the message is
1712446fda4fSPaul Moore  *   discarded and no response is permitted (consistent with general ICMP
1713446fda4fSPaul Moore  *   processing rules)."
1714446fda4fSPaul Moore  *
1715446fda4fSPaul Moore  */
cipso_v4_error(struct sk_buff * skb,int error,u32 gateway)1716446fda4fSPaul Moore void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1717446fda4fSPaul Moore {
17183da1ed7aSNazarov Sergey 	unsigned char optbuf[sizeof(struct ip_options) + 40];
17193da1ed7aSNazarov Sergey 	struct ip_options *opt = (struct ip_options *)optbuf;
17203e72dfdfSMatteo Croce 	int res;
17213da1ed7aSNazarov Sergey 
1722eddc9ec5SArnaldo Carvalho de Melo 	if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
1723446fda4fSPaul Moore 		return;
1724446fda4fSPaul Moore 
17253da1ed7aSNazarov Sergey 	/*
17263da1ed7aSNazarov Sergey 	 * We might be called above the IP layer,
17273da1ed7aSNazarov Sergey 	 * so we can not use icmp_send and IPCB here.
17283da1ed7aSNazarov Sergey 	 */
17293da1ed7aSNazarov Sergey 
17303da1ed7aSNazarov Sergey 	memset(opt, 0, sizeof(struct ip_options));
17313da1ed7aSNazarov Sergey 	opt->optlen = ip_hdr(skb)->ihl*4 - sizeof(struct iphdr);
17323e72dfdfSMatteo Croce 	rcu_read_lock();
17333e72dfdfSMatteo Croce 	res = __ip_options_compile(dev_net(skb->dev), opt, skb, NULL);
17343e72dfdfSMatteo Croce 	rcu_read_unlock();
17353e72dfdfSMatteo Croce 
17363e72dfdfSMatteo Croce 	if (res)
17373da1ed7aSNazarov Sergey 		return;
17383da1ed7aSNazarov Sergey 
1739446fda4fSPaul Moore 	if (gateway)
17403da1ed7aSNazarov Sergey 		__icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0, opt);
1741446fda4fSPaul Moore 	else
17423da1ed7aSNazarov Sergey 		__icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0, opt);
1743446fda4fSPaul Moore }
1744446fda4fSPaul Moore 
1745446fda4fSPaul Moore /**
1746948bf85cSPaul Moore  * cipso_v4_genopt - Generate a CIPSO option
1747948bf85cSPaul Moore  * @buf: the option buffer
1748948bf85cSPaul Moore  * @buf_len: the size of opt_buf
1749446fda4fSPaul Moore  * @doi_def: the CIPSO DOI to use
1750948bf85cSPaul Moore  * @secattr: the security attributes
1751446fda4fSPaul Moore  *
1752446fda4fSPaul Moore  * Description:
1753948bf85cSPaul Moore  * Generate a CIPSO option using the DOI definition and security attributes
1754948bf85cSPaul Moore  * passed to the function.  Returns the length of the option on success and
1755948bf85cSPaul Moore  * negative values on failure.
1756446fda4fSPaul Moore  *
1757446fda4fSPaul Moore  */
cipso_v4_genopt(unsigned char * buf,u32 buf_len,const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr)1758948bf85cSPaul Moore static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
1759446fda4fSPaul Moore 			   const struct cipso_v4_doi *doi_def,
1760446fda4fSPaul Moore 			   const struct netlbl_lsm_secattr *secattr)
1761446fda4fSPaul Moore {
1762948bf85cSPaul Moore 	int ret_val;
1763446fda4fSPaul Moore 	u32 iter;
1764446fda4fSPaul Moore 
1765948bf85cSPaul Moore 	if (buf_len <= CIPSO_V4_HDR_LEN)
1766948bf85cSPaul Moore 		return -ENOSPC;
176791b1ed0aSPaul Moore 
1768446fda4fSPaul Moore 	/* XXX - This code assumes only one tag per CIPSO option which isn't
1769446fda4fSPaul Moore 	 * really a good assumption to make but since we only support the MAC
1770446fda4fSPaul Moore 	 * tags right now it is a safe assumption. */
1771446fda4fSPaul Moore 	iter = 0;
1772446fda4fSPaul Moore 	do {
177391b1ed0aSPaul Moore 		memset(buf, 0, buf_len);
1774446fda4fSPaul Moore 		switch (doi_def->tags[iter]) {
1775446fda4fSPaul Moore 		case CIPSO_V4_TAG_RBITMAP:
1776446fda4fSPaul Moore 			ret_val = cipso_v4_gentag_rbm(doi_def,
1777446fda4fSPaul Moore 						   secattr,
177891b1ed0aSPaul Moore 						   &buf[CIPSO_V4_HDR_LEN],
177991b1ed0aSPaul Moore 						   buf_len - CIPSO_V4_HDR_LEN);
1780446fda4fSPaul Moore 			break;
1781654bbc2aSPaul Moore 		case CIPSO_V4_TAG_ENUM:
1782654bbc2aSPaul Moore 			ret_val = cipso_v4_gentag_enum(doi_def,
1783654bbc2aSPaul Moore 						   secattr,
1784654bbc2aSPaul Moore 						   &buf[CIPSO_V4_HDR_LEN],
1785654bbc2aSPaul Moore 						   buf_len - CIPSO_V4_HDR_LEN);
1786654bbc2aSPaul Moore 			break;
1787484b3669SPaul Moore 		case CIPSO_V4_TAG_RANGE:
1788484b3669SPaul Moore 			ret_val = cipso_v4_gentag_rng(doi_def,
1789484b3669SPaul Moore 						   secattr,
1790484b3669SPaul Moore 						   &buf[CIPSO_V4_HDR_LEN],
1791484b3669SPaul Moore 						   buf_len - CIPSO_V4_HDR_LEN);
1792484b3669SPaul Moore 			break;
179315c45f7bSPaul Moore 		case CIPSO_V4_TAG_LOCAL:
179415c45f7bSPaul Moore 			ret_val = cipso_v4_gentag_loc(doi_def,
179515c45f7bSPaul Moore 						   secattr,
179615c45f7bSPaul Moore 						   &buf[CIPSO_V4_HDR_LEN],
179715c45f7bSPaul Moore 						   buf_len - CIPSO_V4_HDR_LEN);
179815c45f7bSPaul Moore 			break;
1799446fda4fSPaul Moore 		default:
1800948bf85cSPaul Moore 			return -EPERM;
1801446fda4fSPaul Moore 		}
1802446fda4fSPaul Moore 
1803446fda4fSPaul Moore 		iter++;
180491b1ed0aSPaul Moore 	} while (ret_val < 0 &&
1805446fda4fSPaul Moore 		 iter < CIPSO_V4_TAG_MAXCNT &&
1806446fda4fSPaul Moore 		 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
180791b1ed0aSPaul Moore 	if (ret_val < 0)
1808948bf85cSPaul Moore 		return ret_val;
180991b1ed0aSPaul Moore 	cipso_v4_gentag_hdr(doi_def, buf, ret_val);
1810948bf85cSPaul Moore 	return CIPSO_V4_HDR_LEN + ret_val;
1811948bf85cSPaul Moore }
1812948bf85cSPaul Moore 
cipso_v4_get_actual_opt_len(const unsigned char * data,int len)181389aa3619SOndrej Mosnacek static int cipso_v4_get_actual_opt_len(const unsigned char *data, int len)
181489aa3619SOndrej Mosnacek {
181589aa3619SOndrej Mosnacek 	int iter = 0, optlen = 0;
181689aa3619SOndrej Mosnacek 
181789aa3619SOndrej Mosnacek 	/* determining the new total option length is tricky because of
181889aa3619SOndrej Mosnacek 	 * the padding necessary, the only thing i can think to do at
181989aa3619SOndrej Mosnacek 	 * this point is walk the options one-by-one, skipping the
182089aa3619SOndrej Mosnacek 	 * padding at the end to determine the actual option size and
182189aa3619SOndrej Mosnacek 	 * from there we can determine the new total option length
182289aa3619SOndrej Mosnacek 	 */
182389aa3619SOndrej Mosnacek 	while (iter < len) {
182489aa3619SOndrej Mosnacek 		if (data[iter] == IPOPT_END) {
182589aa3619SOndrej Mosnacek 			break;
182689aa3619SOndrej Mosnacek 		} else if (data[iter] == IPOPT_NOP) {
182789aa3619SOndrej Mosnacek 			iter++;
182889aa3619SOndrej Mosnacek 		} else {
182989aa3619SOndrej Mosnacek 			iter += data[iter + 1];
183089aa3619SOndrej Mosnacek 			optlen = iter;
183189aa3619SOndrej Mosnacek 		}
183289aa3619SOndrej Mosnacek 	}
183389aa3619SOndrej Mosnacek 	return optlen;
183489aa3619SOndrej Mosnacek }
183589aa3619SOndrej Mosnacek 
1836948bf85cSPaul Moore /**
1837948bf85cSPaul Moore  * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1838948bf85cSPaul Moore  * @sk: the socket
1839948bf85cSPaul Moore  * @doi_def: the CIPSO DOI to use
1840948bf85cSPaul Moore  * @secattr: the specific security attributes of the socket
18418ec9897eSDavide Caratti  * @sk_locked: true if caller holds the socket lock
1842948bf85cSPaul Moore  *
1843948bf85cSPaul Moore  * Description:
1844948bf85cSPaul Moore  * Set the CIPSO option on the given socket using the DOI definition and
1845948bf85cSPaul Moore  * security attributes passed to the function.  This function requires
1846948bf85cSPaul Moore  * exclusive access to @sk, which means it either needs to be in the
1847948bf85cSPaul Moore  * process of being created or locked.  Returns zero on success and negative
1848948bf85cSPaul Moore  * values on failure.
1849948bf85cSPaul Moore  *
1850948bf85cSPaul Moore  */
cipso_v4_sock_setattr(struct sock * sk,const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,bool sk_locked)1851948bf85cSPaul Moore int cipso_v4_sock_setattr(struct sock *sk,
1852948bf85cSPaul Moore 			  const struct cipso_v4_doi *doi_def,
18538ec9897eSDavide Caratti 			  const struct netlbl_lsm_secattr *secattr,
18548ec9897eSDavide Caratti 			  bool sk_locked)
1855948bf85cSPaul Moore {
1856948bf85cSPaul Moore 	int ret_val = -EPERM;
1857948bf85cSPaul Moore 	unsigned char *buf = NULL;
1858948bf85cSPaul Moore 	u32 buf_len;
1859948bf85cSPaul Moore 	u32 opt_len;
1860f6d8bd05SEric Dumazet 	struct ip_options_rcu *old, *opt = NULL;
1861948bf85cSPaul Moore 	struct inet_sock *sk_inet;
1862948bf85cSPaul Moore 	struct inet_connection_sock *sk_conn;
1863948bf85cSPaul Moore 
1864948bf85cSPaul Moore 	/* In the case of sock_create_lite(), the sock->sk field is not
1865948bf85cSPaul Moore 	 * defined yet but it is not a problem as the only users of these
1866948bf85cSPaul Moore 	 * "lite" PF_INET sockets are functions which do an accept() call
1867948bf85cSPaul Moore 	 * afterwards so we will label the socket as part of the accept(). */
186851456b29SIan Morris 	if (!sk)
1869948bf85cSPaul Moore 		return 0;
1870948bf85cSPaul Moore 
1871948bf85cSPaul Moore 	/* We allocate the maximum CIPSO option size here so we are probably
1872948bf85cSPaul Moore 	 * being a little wasteful, but it makes our life _much_ easier later
1873948bf85cSPaul Moore 	 * on and after all we are only talking about 40 bytes. */
1874948bf85cSPaul Moore 	buf_len = CIPSO_V4_OPT_LEN_MAX;
1875948bf85cSPaul Moore 	buf = kmalloc(buf_len, GFP_ATOMIC);
187651456b29SIan Morris 	if (!buf) {
1877948bf85cSPaul Moore 		ret_val = -ENOMEM;
1878948bf85cSPaul Moore 		goto socket_setattr_failure;
1879948bf85cSPaul Moore 	}
1880948bf85cSPaul Moore 
1881948bf85cSPaul Moore 	ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1882948bf85cSPaul Moore 	if (ret_val < 0)
1883948bf85cSPaul Moore 		goto socket_setattr_failure;
1884948bf85cSPaul Moore 	buf_len = ret_val;
1885446fda4fSPaul Moore 
1886446fda4fSPaul Moore 	/* We can't use ip_options_get() directly because it makes a call to
1887446fda4fSPaul Moore 	 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1888f8687afeSPaul Moore 	 * we won't always have CAP_NET_RAW even though we _always_ want to
1889f8687afeSPaul Moore 	 * set the IPOPT_CIPSO option. */
1890446fda4fSPaul Moore 	opt_len = (buf_len + 3) & ~3;
1891446fda4fSPaul Moore 	opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
189251456b29SIan Morris 	if (!opt) {
1893446fda4fSPaul Moore 		ret_val = -ENOMEM;
1894446fda4fSPaul Moore 		goto socket_setattr_failure;
1895446fda4fSPaul Moore 	}
1896f6d8bd05SEric Dumazet 	memcpy(opt->opt.__data, buf, buf_len);
1897f6d8bd05SEric Dumazet 	opt->opt.optlen = opt_len;
1898f6d8bd05SEric Dumazet 	opt->opt.cipso = sizeof(struct iphdr);
1899446fda4fSPaul Moore 	kfree(buf);
1900446fda4fSPaul Moore 	buf = NULL;
1901446fda4fSPaul Moore 
1902446fda4fSPaul Moore 	sk_inet = inet_sk(sk);
1903f6d8bd05SEric Dumazet 
19048ec9897eSDavide Caratti 	old = rcu_dereference_protected(sk_inet->inet_opt, sk_locked);
1905b1c0356aSEric Dumazet 	if (inet_test_bit(IS_ICSK, sk)) {
1906446fda4fSPaul Moore 		sk_conn = inet_csk(sk);
1907f6d8bd05SEric Dumazet 		if (old)
1908f6d8bd05SEric Dumazet 			sk_conn->icsk_ext_hdr_len -= old->opt.optlen;
1909f6d8bd05SEric Dumazet 		sk_conn->icsk_ext_hdr_len += opt->opt.optlen;
1910446fda4fSPaul Moore 		sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1911446fda4fSPaul Moore 	}
1912f6d8bd05SEric Dumazet 	rcu_assign_pointer(sk_inet->inet_opt, opt);
1913f6d8bd05SEric Dumazet 	if (old)
19144f9c8c1bSPaul E. McKenney 		kfree_rcu(old, rcu);
1915446fda4fSPaul Moore 
1916446fda4fSPaul Moore 	return 0;
1917446fda4fSPaul Moore 
1918446fda4fSPaul Moore socket_setattr_failure:
1919446fda4fSPaul Moore 	kfree(buf);
1920446fda4fSPaul Moore 	kfree(opt);
1921446fda4fSPaul Moore 	return ret_val;
1922446fda4fSPaul Moore }
1923446fda4fSPaul Moore 
1924446fda4fSPaul Moore /**
1925389fb800SPaul Moore  * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket
1926389fb800SPaul Moore  * @req: the connection request socket
1927389fb800SPaul Moore  * @doi_def: the CIPSO DOI to use
1928389fb800SPaul Moore  * @secattr: the specific security attributes of the socket
1929014ab19aSPaul Moore  *
1930014ab19aSPaul Moore  * Description:
1931389fb800SPaul Moore  * Set the CIPSO option on the given socket using the DOI definition and
1932389fb800SPaul Moore  * security attributes passed to the function.  Returns zero on success and
1933389fb800SPaul Moore  * negative values on failure.
1934014ab19aSPaul Moore  *
1935014ab19aSPaul Moore  */
cipso_v4_req_setattr(struct request_sock * req,const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr)1936389fb800SPaul Moore int cipso_v4_req_setattr(struct request_sock *req,
1937389fb800SPaul Moore 			 const struct cipso_v4_doi *doi_def,
1938389fb800SPaul Moore 			 const struct netlbl_lsm_secattr *secattr)
1939014ab19aSPaul Moore {
1940389fb800SPaul Moore 	int ret_val = -EPERM;
1941389fb800SPaul Moore 	unsigned char *buf = NULL;
1942389fb800SPaul Moore 	u32 buf_len;
1943389fb800SPaul Moore 	u32 opt_len;
1944f6d8bd05SEric Dumazet 	struct ip_options_rcu *opt = NULL;
1945389fb800SPaul Moore 	struct inet_request_sock *req_inet;
1946014ab19aSPaul Moore 
1947389fb800SPaul Moore 	/* We allocate the maximum CIPSO option size here so we are probably
1948389fb800SPaul Moore 	 * being a little wasteful, but it makes our life _much_ easier later
1949389fb800SPaul Moore 	 * on and after all we are only talking about 40 bytes. */
1950389fb800SPaul Moore 	buf_len = CIPSO_V4_OPT_LEN_MAX;
1951389fb800SPaul Moore 	buf = kmalloc(buf_len, GFP_ATOMIC);
195251456b29SIan Morris 	if (!buf) {
1953389fb800SPaul Moore 		ret_val = -ENOMEM;
1954389fb800SPaul Moore 		goto req_setattr_failure;
1955389fb800SPaul Moore 	}
1956389fb800SPaul Moore 
1957389fb800SPaul Moore 	ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1958389fb800SPaul Moore 	if (ret_val < 0)
1959389fb800SPaul Moore 		goto req_setattr_failure;
1960389fb800SPaul Moore 	buf_len = ret_val;
1961389fb800SPaul Moore 
1962389fb800SPaul Moore 	/* We can't use ip_options_get() directly because it makes a call to
1963389fb800SPaul Moore 	 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1964389fb800SPaul Moore 	 * we won't always have CAP_NET_RAW even though we _always_ want to
1965389fb800SPaul Moore 	 * set the IPOPT_CIPSO option. */
1966389fb800SPaul Moore 	opt_len = (buf_len + 3) & ~3;
1967389fb800SPaul Moore 	opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
196851456b29SIan Morris 	if (!opt) {
1969389fb800SPaul Moore 		ret_val = -ENOMEM;
1970389fb800SPaul Moore 		goto req_setattr_failure;
1971389fb800SPaul Moore 	}
1972f6d8bd05SEric Dumazet 	memcpy(opt->opt.__data, buf, buf_len);
1973f6d8bd05SEric Dumazet 	opt->opt.optlen = opt_len;
1974f6d8bd05SEric Dumazet 	opt->opt.cipso = sizeof(struct iphdr);
1975389fb800SPaul Moore 	kfree(buf);
1976389fb800SPaul Moore 	buf = NULL;
1977389fb800SPaul Moore 
1978389fb800SPaul Moore 	req_inet = inet_rsk(req);
1979b4cb4a13SEric Dumazet 	opt = unrcu_pointer(xchg(&req_inet->ireq_opt, RCU_INITIALIZER(opt)));
1980f6d8bd05SEric Dumazet 	if (opt)
19814f9c8c1bSPaul E. McKenney 		kfree_rcu(opt, rcu);
1982389fb800SPaul Moore 
1983389fb800SPaul Moore 	return 0;
1984389fb800SPaul Moore 
1985389fb800SPaul Moore req_setattr_failure:
1986389fb800SPaul Moore 	kfree(buf);
1987389fb800SPaul Moore 	kfree(opt);
1988389fb800SPaul Moore 	return ret_val;
1989389fb800SPaul Moore }
1990389fb800SPaul Moore 
1991389fb800SPaul Moore /**
1992389fb800SPaul Moore  * cipso_v4_delopt - Delete the CIPSO option from a set of IP options
1993389fb800SPaul Moore  * @opt_ptr: IP option pointer
1994389fb800SPaul Moore  *
1995389fb800SPaul Moore  * Description:
1996389fb800SPaul Moore  * Deletes the CIPSO IP option from a set of IP options and makes the necessary
1997389fb800SPaul Moore  * adjustments to the IP option structure.  Returns zero on success, negative
1998389fb800SPaul Moore  * values on failure.
1999389fb800SPaul Moore  *
2000389fb800SPaul Moore  */
cipso_v4_delopt(struct ip_options_rcu __rcu ** opt_ptr)2001c92e8c02SEric Dumazet static int cipso_v4_delopt(struct ip_options_rcu __rcu **opt_ptr)
2002389fb800SPaul Moore {
2003c92e8c02SEric Dumazet 	struct ip_options_rcu *opt = rcu_dereference_protected(*opt_ptr, 1);
2004389fb800SPaul Moore 	int hdr_delta = 0;
2005014ab19aSPaul Moore 
2006c92e8c02SEric Dumazet 	if (!opt || opt->opt.cipso == 0)
2007c92e8c02SEric Dumazet 		return 0;
2008f6d8bd05SEric Dumazet 	if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) {
2009014ab19aSPaul Moore 		u8 cipso_len;
2010014ab19aSPaul Moore 		u8 cipso_off;
2011014ab19aSPaul Moore 		unsigned char *cipso_ptr;
2012014ab19aSPaul Moore 		int optlen_new;
2013014ab19aSPaul Moore 
2014f6d8bd05SEric Dumazet 		cipso_off = opt->opt.cipso - sizeof(struct iphdr);
2015f6d8bd05SEric Dumazet 		cipso_ptr = &opt->opt.__data[cipso_off];
2016014ab19aSPaul Moore 		cipso_len = cipso_ptr[1];
2017014ab19aSPaul Moore 
2018f6d8bd05SEric Dumazet 		if (opt->opt.srr > opt->opt.cipso)
2019f6d8bd05SEric Dumazet 			opt->opt.srr -= cipso_len;
2020f6d8bd05SEric Dumazet 		if (opt->opt.rr > opt->opt.cipso)
2021f6d8bd05SEric Dumazet 			opt->opt.rr -= cipso_len;
2022f6d8bd05SEric Dumazet 		if (opt->opt.ts > opt->opt.cipso)
2023f6d8bd05SEric Dumazet 			opt->opt.ts -= cipso_len;
2024f6d8bd05SEric Dumazet 		if (opt->opt.router_alert > opt->opt.cipso)
2025f6d8bd05SEric Dumazet 			opt->opt.router_alert -= cipso_len;
2026f6d8bd05SEric Dumazet 		opt->opt.cipso = 0;
2027014ab19aSPaul Moore 
2028014ab19aSPaul Moore 		memmove(cipso_ptr, cipso_ptr + cipso_len,
2029f6d8bd05SEric Dumazet 			opt->opt.optlen - cipso_off - cipso_len);
2030014ab19aSPaul Moore 
203189aa3619SOndrej Mosnacek 		optlen_new = cipso_v4_get_actual_opt_len(opt->opt.__data,
203289aa3619SOndrej Mosnacek 							 opt->opt.optlen);
2033f6d8bd05SEric Dumazet 		hdr_delta = opt->opt.optlen;
2034f6d8bd05SEric Dumazet 		opt->opt.optlen = (optlen_new + 3) & ~3;
2035f6d8bd05SEric Dumazet 		hdr_delta -= opt->opt.optlen;
2036014ab19aSPaul Moore 	} else {
2037014ab19aSPaul Moore 		/* only the cipso option was present on the socket so we can
2038014ab19aSPaul Moore 		 * remove the entire option struct */
2039389fb800SPaul Moore 		*opt_ptr = NULL;
2040f6d8bd05SEric Dumazet 		hdr_delta = opt->opt.optlen;
20414f9c8c1bSPaul E. McKenney 		kfree_rcu(opt, rcu);
2042014ab19aSPaul Moore 	}
2043014ab19aSPaul Moore 
2044389fb800SPaul Moore 	return hdr_delta;
2045389fb800SPaul Moore }
2046389fb800SPaul Moore 
2047389fb800SPaul Moore /**
2048389fb800SPaul Moore  * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
2049389fb800SPaul Moore  * @sk: the socket
2050389fb800SPaul Moore  *
2051389fb800SPaul Moore  * Description:
2052389fb800SPaul Moore  * Removes the CIPSO option from a socket, if present.
2053389fb800SPaul Moore  *
2054389fb800SPaul Moore  */
cipso_v4_sock_delattr(struct sock * sk)2055389fb800SPaul Moore void cipso_v4_sock_delattr(struct sock *sk)
2056389fb800SPaul Moore {
2057389fb800SPaul Moore 	struct inet_sock *sk_inet;
2058c92e8c02SEric Dumazet 	int hdr_delta;
2059389fb800SPaul Moore 
2060389fb800SPaul Moore 	sk_inet = inet_sk(sk);
2061389fb800SPaul Moore 
2062f6d8bd05SEric Dumazet 	hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
2063b1c0356aSEric Dumazet 	if (inet_test_bit(IS_ICSK, sk) && hdr_delta > 0) {
2064014ab19aSPaul Moore 		struct inet_connection_sock *sk_conn = inet_csk(sk);
2065014ab19aSPaul Moore 		sk_conn->icsk_ext_hdr_len -= hdr_delta;
2066014ab19aSPaul Moore 		sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
2067014ab19aSPaul Moore 	}
2068014ab19aSPaul Moore }
2069014ab19aSPaul Moore 
2070014ab19aSPaul Moore /**
2071389fb800SPaul Moore  * cipso_v4_req_delattr - Delete the CIPSO option from a request socket
20723628e3cbSAndrew Lunn  * @req: the request socket
2073389fb800SPaul Moore  *
2074389fb800SPaul Moore  * Description:
2075389fb800SPaul Moore  * Removes the CIPSO option from a request socket, if present.
2076389fb800SPaul Moore  *
2077389fb800SPaul Moore  */
cipso_v4_req_delattr(struct request_sock * req)2078389fb800SPaul Moore void cipso_v4_req_delattr(struct request_sock *req)
2079389fb800SPaul Moore {
2080c92e8c02SEric Dumazet 	cipso_v4_delopt(&inet_rsk(req)->ireq_opt);
2081389fb800SPaul Moore }
2082389fb800SPaul Moore 
2083389fb800SPaul Moore /**
208463d804eaSPaul Moore  * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
208563d804eaSPaul Moore  * @cipso: the CIPSO v4 option
208663d804eaSPaul Moore  * @secattr: the security attributes
208763d804eaSPaul Moore  *
208863d804eaSPaul Moore  * Description:
208963d804eaSPaul Moore  * Inspect @cipso and return the security attributes in @secattr.  Returns zero
209063d804eaSPaul Moore  * on success and negative values on failure.
209163d804eaSPaul Moore  *
209263d804eaSPaul Moore  */
cipso_v4_getattr(const unsigned char * cipso,struct netlbl_lsm_secattr * secattr)209304f81f01SPaul Moore int cipso_v4_getattr(const unsigned char *cipso,
209463d804eaSPaul Moore 		     struct netlbl_lsm_secattr *secattr)
209563d804eaSPaul Moore {
209663d804eaSPaul Moore 	int ret_val = -ENOMSG;
209763d804eaSPaul Moore 	u32 doi;
209863d804eaSPaul Moore 	struct cipso_v4_doi *doi_def;
209963d804eaSPaul Moore 
210063d804eaSPaul Moore 	if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
210163d804eaSPaul Moore 		return 0;
210263d804eaSPaul Moore 
2103d3e2ce3bSHarvey Harrison 	doi = get_unaligned_be32(&cipso[2]);
210463d804eaSPaul Moore 	rcu_read_lock();
210563d804eaSPaul Moore 	doi_def = cipso_v4_doi_search(doi);
210651456b29SIan Morris 	if (!doi_def)
210763d804eaSPaul Moore 		goto getattr_return;
210863d804eaSPaul Moore 	/* XXX - This code assumes only one tag per CIPSO option which isn't
210963d804eaSPaul Moore 	 * really a good assumption to make but since we only support the MAC
211063d804eaSPaul Moore 	 * tags right now it is a safe assumption. */
211163d804eaSPaul Moore 	switch (cipso[6]) {
211263d804eaSPaul Moore 	case CIPSO_V4_TAG_RBITMAP:
211363d804eaSPaul Moore 		ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
211463d804eaSPaul Moore 		break;
211563d804eaSPaul Moore 	case CIPSO_V4_TAG_ENUM:
211663d804eaSPaul Moore 		ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
211763d804eaSPaul Moore 		break;
211863d804eaSPaul Moore 	case CIPSO_V4_TAG_RANGE:
211963d804eaSPaul Moore 		ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
212063d804eaSPaul Moore 		break;
212115c45f7bSPaul Moore 	case CIPSO_V4_TAG_LOCAL:
212215c45f7bSPaul Moore 		ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
212315c45f7bSPaul Moore 		break;
212463d804eaSPaul Moore 	}
212516efd454SPaul Moore 	if (ret_val == 0)
212616efd454SPaul Moore 		secattr->type = NETLBL_NLTYPE_CIPSOV4;
212763d804eaSPaul Moore 
212863d804eaSPaul Moore getattr_return:
212963d804eaSPaul Moore 	rcu_read_unlock();
213063d804eaSPaul Moore 	return ret_val;
213163d804eaSPaul Moore }
213263d804eaSPaul Moore 
213363d804eaSPaul Moore /**
213414a72f53SPaul Moore  * cipso_v4_sock_getattr - Get the security attributes from a sock
213514a72f53SPaul Moore  * @sk: the sock
213614a72f53SPaul Moore  * @secattr: the security attributes
213714a72f53SPaul Moore  *
213814a72f53SPaul Moore  * Description:
213914a72f53SPaul Moore  * Query @sk to see if there is a CIPSO option attached to the sock and if
214014a72f53SPaul Moore  * there is return the CIPSO security attributes in @secattr.  This function
214114a72f53SPaul Moore  * requires that @sk be locked, or privately held, but it does not do any
214214a72f53SPaul Moore  * locking itself.  Returns zero on success and negative values on failure.
214314a72f53SPaul Moore  *
214414a72f53SPaul Moore  */
cipso_v4_sock_getattr(struct sock * sk,struct netlbl_lsm_secattr * secattr)214514a72f53SPaul Moore int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
214614a72f53SPaul Moore {
2147f6d8bd05SEric Dumazet 	struct ip_options_rcu *opt;
2148f6d8bd05SEric Dumazet 	int res = -ENOMSG;
214914a72f53SPaul Moore 
2150f6d8bd05SEric Dumazet 	rcu_read_lock();
2151f6d8bd05SEric Dumazet 	opt = rcu_dereference(inet_sk(sk)->inet_opt);
2152f6d8bd05SEric Dumazet 	if (opt && opt->opt.cipso)
2153f6d8bd05SEric Dumazet 		res = cipso_v4_getattr(opt->opt.__data +
2154f6d8bd05SEric Dumazet 						opt->opt.cipso -
2155f6d8bd05SEric Dumazet 						sizeof(struct iphdr),
215614a72f53SPaul Moore 				       secattr);
2157f6d8bd05SEric Dumazet 	rcu_read_unlock();
2158f6d8bd05SEric Dumazet 	return res;
215914a72f53SPaul Moore }
216014a72f53SPaul Moore 
216114a72f53SPaul Moore /**
2162948bf85cSPaul Moore  * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
2163948bf85cSPaul Moore  * @skb: the packet
21643628e3cbSAndrew Lunn  * @doi_def: the DOI structure
2165948bf85cSPaul Moore  * @secattr: the security attributes
2166948bf85cSPaul Moore  *
2167948bf85cSPaul Moore  * Description:
2168948bf85cSPaul Moore  * Set the CIPSO option on the given packet based on the security attributes.
2169948bf85cSPaul Moore  * Returns a pointer to the IP header on success and NULL on failure.
2170948bf85cSPaul Moore  *
2171948bf85cSPaul Moore  */
cipso_v4_skbuff_setattr(struct sk_buff * skb,const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr)2172948bf85cSPaul Moore int cipso_v4_skbuff_setattr(struct sk_buff *skb,
2173948bf85cSPaul Moore 			    const struct cipso_v4_doi *doi_def,
2174948bf85cSPaul Moore 			    const struct netlbl_lsm_secattr *secattr)
2175948bf85cSPaul Moore {
2176948bf85cSPaul Moore 	int ret_val;
2177948bf85cSPaul Moore 	struct iphdr *iph;
2178948bf85cSPaul Moore 	struct ip_options *opt = &IPCB(skb)->opt;
2179948bf85cSPaul Moore 	unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
2180948bf85cSPaul Moore 	u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
2181948bf85cSPaul Moore 	u32 opt_len;
2182948bf85cSPaul Moore 	int len_delta;
2183948bf85cSPaul Moore 
218400af5c69Sroel kluin 	ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
218500af5c69Sroel kluin 	if (ret_val < 0)
218600af5c69Sroel kluin 		return ret_val;
218700af5c69Sroel kluin 	buf_len = ret_val;
2188948bf85cSPaul Moore 	opt_len = (buf_len + 3) & ~3;
2189948bf85cSPaul Moore 
2190948bf85cSPaul Moore 	/* we overwrite any existing options to ensure that we have enough
2191948bf85cSPaul Moore 	 * room for the CIPSO option, the reason is that we _need_ to guarantee
2192948bf85cSPaul Moore 	 * that the security label is applied to the packet - we do the same
2193948bf85cSPaul Moore 	 * thing when using the socket options and it hasn't caused a problem,
2194948bf85cSPaul Moore 	 * if we need to we can always revisit this choice later */
2195948bf85cSPaul Moore 
2196948bf85cSPaul Moore 	len_delta = opt_len - opt->optlen;
2197948bf85cSPaul Moore 	/* if we don't ensure enough headroom we could panic on the skb_push()
2198948bf85cSPaul Moore 	 * call below so make sure we have enough, we are also "mangling" the
2199948bf85cSPaul Moore 	 * packet so we should probably do a copy-on-write call anyway */
2200948bf85cSPaul Moore 	ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
2201948bf85cSPaul Moore 	if (ret_val < 0)
2202948bf85cSPaul Moore 		return ret_val;
2203948bf85cSPaul Moore 
2204948bf85cSPaul Moore 	if (len_delta > 0) {
2205948bf85cSPaul Moore 		/* we assume that the header + opt->optlen have already been
2206948bf85cSPaul Moore 		 * "pushed" in ip_options_build() or similar */
2207948bf85cSPaul Moore 		iph = ip_hdr(skb);
2208948bf85cSPaul Moore 		skb_push(skb, len_delta);
2209948bf85cSPaul Moore 		memmove((char *)iph - len_delta, iph, iph->ihl << 2);
2210948bf85cSPaul Moore 		skb_reset_network_header(skb);
2211948bf85cSPaul Moore 		iph = ip_hdr(skb);
2212948bf85cSPaul Moore 	} else if (len_delta < 0) {
2213948bf85cSPaul Moore 		iph = ip_hdr(skb);
2214948bf85cSPaul Moore 		memset(iph + 1, IPOPT_NOP, opt->optlen);
2215948bf85cSPaul Moore 	} else
2216948bf85cSPaul Moore 		iph = ip_hdr(skb);
2217948bf85cSPaul Moore 
2218948bf85cSPaul Moore 	if (opt->optlen > 0)
2219948bf85cSPaul Moore 		memset(opt, 0, sizeof(*opt));
2220948bf85cSPaul Moore 	opt->optlen = opt_len;
2221948bf85cSPaul Moore 	opt->cipso = sizeof(struct iphdr);
2222948bf85cSPaul Moore 	opt->is_changed = 1;
2223948bf85cSPaul Moore 
2224948bf85cSPaul Moore 	/* we have to do the following because we are being called from a
2225948bf85cSPaul Moore 	 * netfilter hook which means the packet already has had the header
2226948bf85cSPaul Moore 	 * fields populated and the checksum calculated - yes this means we
2227948bf85cSPaul Moore 	 * are doing more work than needed but we do it to keep the core
2228948bf85cSPaul Moore 	 * stack clean and tidy */
2229948bf85cSPaul Moore 	memcpy(iph + 1, buf, buf_len);
2230948bf85cSPaul Moore 	if (opt_len > buf_len)
2231948bf85cSPaul Moore 		memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
2232948bf85cSPaul Moore 	if (len_delta != 0) {
2233948bf85cSPaul Moore 		iph->ihl = 5 + (opt_len >> 2);
22347eb072beSXin Long 		iph_set_totlen(iph, skb->len);
2235948bf85cSPaul Moore 	}
2236948bf85cSPaul Moore 	ip_send_check(iph);
2237948bf85cSPaul Moore 
2238948bf85cSPaul Moore 	return 0;
2239948bf85cSPaul Moore }
2240948bf85cSPaul Moore 
2241948bf85cSPaul Moore /**
2242948bf85cSPaul Moore  * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
2243948bf85cSPaul Moore  * @skb: the packet
2244948bf85cSPaul Moore  *
2245948bf85cSPaul Moore  * Description:
2246948bf85cSPaul Moore  * Removes any and all CIPSO options from the given packet.  Returns zero on
2247948bf85cSPaul Moore  * success, negative values on failure.
2248948bf85cSPaul Moore  *
2249948bf85cSPaul Moore  */
cipso_v4_skbuff_delattr(struct sk_buff * skb)2250948bf85cSPaul Moore int cipso_v4_skbuff_delattr(struct sk_buff *skb)
2251948bf85cSPaul Moore {
225289aa3619SOndrej Mosnacek 	int ret_val, cipso_len, hdr_len_actual, new_hdr_len_actual, new_hdr_len,
225389aa3619SOndrej Mosnacek 	    hdr_len_delta;
2254948bf85cSPaul Moore 	struct iphdr *iph;
2255948bf85cSPaul Moore 	struct ip_options *opt = &IPCB(skb)->opt;
2256948bf85cSPaul Moore 	unsigned char *cipso_ptr;
2257948bf85cSPaul Moore 
2258948bf85cSPaul Moore 	if (opt->cipso == 0)
2259948bf85cSPaul Moore 		return 0;
2260948bf85cSPaul Moore 
2261948bf85cSPaul Moore 	/* since we are changing the packet we should make a copy */
2262948bf85cSPaul Moore 	ret_val = skb_cow(skb, skb_headroom(skb));
2263948bf85cSPaul Moore 	if (ret_val < 0)
2264948bf85cSPaul Moore 		return ret_val;
2265948bf85cSPaul Moore 
2266948bf85cSPaul Moore 	iph = ip_hdr(skb);
2267948bf85cSPaul Moore 	cipso_ptr = (unsigned char *)iph + opt->cipso;
226889aa3619SOndrej Mosnacek 	cipso_len = cipso_ptr[1];
226989aa3619SOndrej Mosnacek 
227089aa3619SOndrej Mosnacek 	hdr_len_actual = sizeof(struct iphdr) +
227189aa3619SOndrej Mosnacek 			 cipso_v4_get_actual_opt_len((unsigned char *)(iph + 1),
227289aa3619SOndrej Mosnacek 						     opt->optlen);
227389aa3619SOndrej Mosnacek 	new_hdr_len_actual = hdr_len_actual - cipso_len;
227489aa3619SOndrej Mosnacek 	new_hdr_len = (new_hdr_len_actual + 3) & ~3;
227589aa3619SOndrej Mosnacek 	hdr_len_delta = (iph->ihl << 2) - new_hdr_len;
227689aa3619SOndrej Mosnacek 
227789aa3619SOndrej Mosnacek 	/* 1. shift any options after CIPSO to the left */
227889aa3619SOndrej Mosnacek 	memmove(cipso_ptr, cipso_ptr + cipso_len,
227989aa3619SOndrej Mosnacek 		new_hdr_len_actual - opt->cipso);
228089aa3619SOndrej Mosnacek 	/* 2. move the whole IP header to its new place */
228189aa3619SOndrej Mosnacek 	memmove((unsigned char *)iph + hdr_len_delta, iph, new_hdr_len_actual);
228289aa3619SOndrej Mosnacek 	/* 3. adjust the skb layout */
228389aa3619SOndrej Mosnacek 	skb_pull(skb, hdr_len_delta);
228489aa3619SOndrej Mosnacek 	skb_reset_network_header(skb);
228589aa3619SOndrej Mosnacek 	iph = ip_hdr(skb);
228689aa3619SOndrej Mosnacek 	/* 4. re-fill new padding with IPOPT_END (may now be longer) */
228789aa3619SOndrej Mosnacek 	memset((unsigned char *)iph + new_hdr_len_actual, IPOPT_END,
228889aa3619SOndrej Mosnacek 	       new_hdr_len - new_hdr_len_actual);
228989aa3619SOndrej Mosnacek 
229089aa3619SOndrej Mosnacek 	opt->optlen -= hdr_len_delta;
2291948bf85cSPaul Moore 	opt->cipso = 0;
2292948bf85cSPaul Moore 	opt->is_changed = 1;
229389aa3619SOndrej Mosnacek 	if (hdr_len_delta != 0) {
229489aa3619SOndrej Mosnacek 		iph->ihl = new_hdr_len >> 2;
229589aa3619SOndrej Mosnacek 		iph_set_totlen(iph, skb->len);
229689aa3619SOndrej Mosnacek 	}
2297948bf85cSPaul Moore 	ip_send_check(iph);
2298948bf85cSPaul Moore 
2299948bf85cSPaul Moore 	return 0;
2300948bf85cSPaul Moore }
2301948bf85cSPaul Moore 
2302446fda4fSPaul Moore /*
2303446fda4fSPaul Moore  * Setup Functions
2304446fda4fSPaul Moore  */
2305446fda4fSPaul Moore 
2306446fda4fSPaul Moore /**
2307446fda4fSPaul Moore  * cipso_v4_init - Initialize the CIPSO module
2308446fda4fSPaul Moore  *
2309446fda4fSPaul Moore  * Description:
2310446fda4fSPaul Moore  * Initialize the CIPSO module and prepare it for use.  Returns zero on success
2311446fda4fSPaul Moore  * and negative values on failure.
2312446fda4fSPaul Moore  *
2313446fda4fSPaul Moore  */
cipso_v4_init(void)2314446fda4fSPaul Moore static int __init cipso_v4_init(void)
2315446fda4fSPaul Moore {
2316446fda4fSPaul Moore 	int ret_val;
2317446fda4fSPaul Moore 
2318446fda4fSPaul Moore 	ret_val = cipso_v4_cache_init();
2319446fda4fSPaul Moore 	if (ret_val != 0)
2320446fda4fSPaul Moore 		panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
2321446fda4fSPaul Moore 		      ret_val);
2322446fda4fSPaul Moore 
2323446fda4fSPaul Moore 	return 0;
2324446fda4fSPaul Moore }
2325446fda4fSPaul Moore 
2326446fda4fSPaul Moore subsys_initcall(cipso_v4_init);
2327