1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * CIPSO - Commercial IP Security Option
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This is an implementation of the CIPSO 2.2 protocol as specified in
6*4882a593Smuzhiyun * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
7*4882a593Smuzhiyun * FIPS-188. While CIPSO never became a full IETF RFC standard many vendors
8*4882a593Smuzhiyun * have chosen to adopt the protocol and over the years it has become a
9*4882a593Smuzhiyun * de-facto standard for labeled networking.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The CIPSO draft specification can be found in the kernel's Documentation
12*4882a593Smuzhiyun * directory as well as the following URL:
13*4882a593Smuzhiyun * https://tools.ietf.org/id/draft-ietf-cipso-ipsecurity-01.txt
14*4882a593Smuzhiyun * The FIPS-188 specification can be found at the following URL:
15*4882a593Smuzhiyun * https://www.itl.nist.gov/fipspubs/fip188.htm
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Author: Paul Moore <paul.moore@hp.com>
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /*
21*4882a593Smuzhiyun * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include <linux/init.h>
25*4882a593Smuzhiyun #include <linux/types.h>
26*4882a593Smuzhiyun #include <linux/rcupdate.h>
27*4882a593Smuzhiyun #include <linux/list.h>
28*4882a593Smuzhiyun #include <linux/spinlock.h>
29*4882a593Smuzhiyun #include <linux/string.h>
30*4882a593Smuzhiyun #include <linux/jhash.h>
31*4882a593Smuzhiyun #include <linux/audit.h>
32*4882a593Smuzhiyun #include <linux/slab.h>
33*4882a593Smuzhiyun #include <net/ip.h>
34*4882a593Smuzhiyun #include <net/icmp.h>
35*4882a593Smuzhiyun #include <net/tcp.h>
36*4882a593Smuzhiyun #include <net/netlabel.h>
37*4882a593Smuzhiyun #include <net/cipso_ipv4.h>
38*4882a593Smuzhiyun #include <linux/atomic.h>
39*4882a593Smuzhiyun #include <linux/bug.h>
40*4882a593Smuzhiyun #include <asm/unaligned.h>
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* List of available DOI definitions */
43*4882a593Smuzhiyun /* XXX - This currently assumes a minimal number of different DOIs in use,
44*4882a593Smuzhiyun * if in practice there are a lot of different DOIs this list should
45*4882a593Smuzhiyun * probably be turned into a hash table or something similar so we
46*4882a593Smuzhiyun * can do quick lookups. */
47*4882a593Smuzhiyun static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
48*4882a593Smuzhiyun static LIST_HEAD(cipso_v4_doi_list);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* Label mapping cache */
51*4882a593Smuzhiyun int cipso_v4_cache_enabled = 1;
52*4882a593Smuzhiyun int cipso_v4_cache_bucketsize = 10;
53*4882a593Smuzhiyun #define CIPSO_V4_CACHE_BUCKETBITS 7
54*4882a593Smuzhiyun #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
55*4882a593Smuzhiyun #define CIPSO_V4_CACHE_REORDERLIMIT 10
56*4882a593Smuzhiyun struct cipso_v4_map_cache_bkt {
57*4882a593Smuzhiyun spinlock_t lock;
58*4882a593Smuzhiyun u32 size;
59*4882a593Smuzhiyun struct list_head list;
60*4882a593Smuzhiyun };
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun struct cipso_v4_map_cache_entry {
63*4882a593Smuzhiyun u32 hash;
64*4882a593Smuzhiyun unsigned char *key;
65*4882a593Smuzhiyun size_t key_len;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun struct netlbl_lsm_cache *lsm_data;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun u32 activity;
70*4882a593Smuzhiyun struct list_head list;
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun static struct cipso_v4_map_cache_bkt *cipso_v4_cache;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* Restricted bitmap (tag #1) flags */
76*4882a593Smuzhiyun int cipso_v4_rbm_optfmt = 0;
77*4882a593Smuzhiyun int cipso_v4_rbm_strictvalid = 1;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun * Protocol Constants
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
84*4882a593Smuzhiyun * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
85*4882a593Smuzhiyun #define CIPSO_V4_OPT_LEN_MAX 40
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Length of the base CIPSO option, this includes the option type (1 byte), the
88*4882a593Smuzhiyun * option length (1 byte), and the DOI (4 bytes). */
89*4882a593Smuzhiyun #define CIPSO_V4_HDR_LEN 6
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* Base length of the restrictive category bitmap tag (tag #1). */
92*4882a593Smuzhiyun #define CIPSO_V4_TAG_RBM_BLEN 4
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* Base length of the enumerated category tag (tag #2). */
95*4882a593Smuzhiyun #define CIPSO_V4_TAG_ENUM_BLEN 4
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* Base length of the ranged categories bitmap tag (tag #5). */
98*4882a593Smuzhiyun #define CIPSO_V4_TAG_RNG_BLEN 4
99*4882a593Smuzhiyun /* The maximum number of category ranges permitted in the ranged category tag
100*4882a593Smuzhiyun * (tag #5). You may note that the IETF draft states that the maximum number
101*4882a593Smuzhiyun * of category ranges is 7, but if the low end of the last category range is
102*4882a593Smuzhiyun * zero then it is possible to fit 8 category ranges because the zero should
103*4882a593Smuzhiyun * be omitted. */
104*4882a593Smuzhiyun #define CIPSO_V4_TAG_RNG_CAT_MAX 8
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /* Base length of the local tag (non-standard tag).
107*4882a593Smuzhiyun * Tag definition (may change between kernel versions)
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * 0 8 16 24 32
110*4882a593Smuzhiyun * +----------+----------+----------+----------+
111*4882a593Smuzhiyun * | 10000000 | 00000110 | 32-bit secid value |
112*4882a593Smuzhiyun * +----------+----------+----------+----------+
113*4882a593Smuzhiyun * | in (host byte order)|
114*4882a593Smuzhiyun * +----------+----------+
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun #define CIPSO_V4_TAG_LOC_BLEN 6
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * Helper Functions
121*4882a593Smuzhiyun */
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /**
124*4882a593Smuzhiyun * cipso_v4_cache_entry_free - Frees a cache entry
125*4882a593Smuzhiyun * @entry: the entry to free
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * Description:
128*4882a593Smuzhiyun * This function frees the memory associated with a cache entry including the
129*4882a593Smuzhiyun * LSM cache data if there are no longer any users, i.e. reference count == 0.
130*4882a593Smuzhiyun *
131*4882a593Smuzhiyun */
cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry * entry)132*4882a593Smuzhiyun static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun if (entry->lsm_data)
135*4882a593Smuzhiyun netlbl_secattr_cache_free(entry->lsm_data);
136*4882a593Smuzhiyun kfree(entry->key);
137*4882a593Smuzhiyun kfree(entry);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun /**
141*4882a593Smuzhiyun * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
142*4882a593Smuzhiyun * @key: the hash key
143*4882a593Smuzhiyun * @key_len: the length of the key in bytes
144*4882a593Smuzhiyun *
145*4882a593Smuzhiyun * Description:
146*4882a593Smuzhiyun * The CIPSO tag hashing function. Returns a 32-bit hash value.
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun */
cipso_v4_map_cache_hash(const unsigned char * key,u32 key_len)149*4882a593Smuzhiyun static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun return jhash(key, key_len, 0);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun * Label Mapping Cache Functions
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /**
159*4882a593Smuzhiyun * cipso_v4_cache_init - Initialize the CIPSO cache
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * Description:
162*4882a593Smuzhiyun * Initializes the CIPSO label mapping cache, this function should be called
163*4882a593Smuzhiyun * before any of the other functions defined in this file. Returns zero on
164*4882a593Smuzhiyun * success, negative values on error.
165*4882a593Smuzhiyun *
166*4882a593Smuzhiyun */
cipso_v4_cache_init(void)167*4882a593Smuzhiyun static int __init cipso_v4_cache_init(void)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun u32 iter;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
172*4882a593Smuzhiyun sizeof(struct cipso_v4_map_cache_bkt),
173*4882a593Smuzhiyun GFP_KERNEL);
174*4882a593Smuzhiyun if (!cipso_v4_cache)
175*4882a593Smuzhiyun return -ENOMEM;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
178*4882a593Smuzhiyun spin_lock_init(&cipso_v4_cache[iter].lock);
179*4882a593Smuzhiyun cipso_v4_cache[iter].size = 0;
180*4882a593Smuzhiyun INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun return 0;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /**
187*4882a593Smuzhiyun * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
188*4882a593Smuzhiyun *
189*4882a593Smuzhiyun * Description:
190*4882a593Smuzhiyun * Invalidates and frees any entries in the CIPSO cache. Returns zero on
191*4882a593Smuzhiyun * success and negative values on failure.
192*4882a593Smuzhiyun *
193*4882a593Smuzhiyun */
cipso_v4_cache_invalidate(void)194*4882a593Smuzhiyun void cipso_v4_cache_invalidate(void)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun struct cipso_v4_map_cache_entry *entry, *tmp_entry;
197*4882a593Smuzhiyun u32 iter;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
200*4882a593Smuzhiyun spin_lock_bh(&cipso_v4_cache[iter].lock);
201*4882a593Smuzhiyun list_for_each_entry_safe(entry,
202*4882a593Smuzhiyun tmp_entry,
203*4882a593Smuzhiyun &cipso_v4_cache[iter].list, list) {
204*4882a593Smuzhiyun list_del(&entry->list);
205*4882a593Smuzhiyun cipso_v4_cache_entry_free(entry);
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun cipso_v4_cache[iter].size = 0;
208*4882a593Smuzhiyun spin_unlock_bh(&cipso_v4_cache[iter].lock);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /**
213*4882a593Smuzhiyun * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
214*4882a593Smuzhiyun * @key: the buffer to check
215*4882a593Smuzhiyun * @key_len: buffer length in bytes
216*4882a593Smuzhiyun * @secattr: the security attribute struct to use
217*4882a593Smuzhiyun *
218*4882a593Smuzhiyun * Description:
219*4882a593Smuzhiyun * This function checks the cache to see if a label mapping already exists for
220*4882a593Smuzhiyun * the given key. If there is a match then the cache is adjusted and the
221*4882a593Smuzhiyun * @secattr struct is populated with the correct LSM security attributes. The
222*4882a593Smuzhiyun * cache is adjusted in the following manner if the entry is not already the
223*4882a593Smuzhiyun * first in the cache bucket:
224*4882a593Smuzhiyun *
225*4882a593Smuzhiyun * 1. The cache entry's activity counter is incremented
226*4882a593Smuzhiyun * 2. The previous (higher ranking) entry's activity counter is decremented
227*4882a593Smuzhiyun * 3. If the difference between the two activity counters is geater than
228*4882a593Smuzhiyun * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
229*4882a593Smuzhiyun *
230*4882a593Smuzhiyun * Returns zero on success, -ENOENT for a cache miss, and other negative values
231*4882a593Smuzhiyun * on error.
232*4882a593Smuzhiyun *
233*4882a593Smuzhiyun */
cipso_v4_cache_check(const unsigned char * key,u32 key_len,struct netlbl_lsm_secattr * secattr)234*4882a593Smuzhiyun static int cipso_v4_cache_check(const unsigned char *key,
235*4882a593Smuzhiyun u32 key_len,
236*4882a593Smuzhiyun struct netlbl_lsm_secattr *secattr)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun u32 bkt;
239*4882a593Smuzhiyun struct cipso_v4_map_cache_entry *entry;
240*4882a593Smuzhiyun struct cipso_v4_map_cache_entry *prev_entry = NULL;
241*4882a593Smuzhiyun u32 hash;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun if (!READ_ONCE(cipso_v4_cache_enabled))
244*4882a593Smuzhiyun return -ENOENT;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun hash = cipso_v4_map_cache_hash(key, key_len);
247*4882a593Smuzhiyun bkt = hash & (CIPSO_V4_CACHE_BUCKETS - 1);
248*4882a593Smuzhiyun spin_lock_bh(&cipso_v4_cache[bkt].lock);
249*4882a593Smuzhiyun list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
250*4882a593Smuzhiyun if (entry->hash == hash &&
251*4882a593Smuzhiyun entry->key_len == key_len &&
252*4882a593Smuzhiyun memcmp(entry->key, key, key_len) == 0) {
253*4882a593Smuzhiyun entry->activity += 1;
254*4882a593Smuzhiyun refcount_inc(&entry->lsm_data->refcount);
255*4882a593Smuzhiyun secattr->cache = entry->lsm_data;
256*4882a593Smuzhiyun secattr->flags |= NETLBL_SECATTR_CACHE;
257*4882a593Smuzhiyun secattr->type = NETLBL_NLTYPE_CIPSOV4;
258*4882a593Smuzhiyun if (!prev_entry) {
259*4882a593Smuzhiyun spin_unlock_bh(&cipso_v4_cache[bkt].lock);
260*4882a593Smuzhiyun return 0;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun if (prev_entry->activity > 0)
264*4882a593Smuzhiyun prev_entry->activity -= 1;
265*4882a593Smuzhiyun if (entry->activity > prev_entry->activity &&
266*4882a593Smuzhiyun entry->activity - prev_entry->activity >
267*4882a593Smuzhiyun CIPSO_V4_CACHE_REORDERLIMIT) {
268*4882a593Smuzhiyun __list_del(entry->list.prev, entry->list.next);
269*4882a593Smuzhiyun __list_add(&entry->list,
270*4882a593Smuzhiyun prev_entry->list.prev,
271*4882a593Smuzhiyun &prev_entry->list);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun spin_unlock_bh(&cipso_v4_cache[bkt].lock);
275*4882a593Smuzhiyun return 0;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun prev_entry = entry;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun spin_unlock_bh(&cipso_v4_cache[bkt].lock);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun return -ENOENT;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /**
285*4882a593Smuzhiyun * cipso_v4_cache_add - Add an entry to the CIPSO cache
286*4882a593Smuzhiyun * @cipso_ptr: pointer to CIPSO IP option
287*4882a593Smuzhiyun * @secattr: the packet's security attributes
288*4882a593Smuzhiyun *
289*4882a593Smuzhiyun * Description:
290*4882a593Smuzhiyun * Add a new entry into the CIPSO label mapping cache. Add the new entry to
291*4882a593Smuzhiyun * head of the cache bucket's list, if the cache bucket is out of room remove
292*4882a593Smuzhiyun * the last entry in the list first. It is important to note that there is
293*4882a593Smuzhiyun * currently no checking for duplicate keys. Returns zero on success,
294*4882a593Smuzhiyun * negative values on failure.
295*4882a593Smuzhiyun *
296*4882a593Smuzhiyun */
cipso_v4_cache_add(const unsigned char * cipso_ptr,const struct netlbl_lsm_secattr * secattr)297*4882a593Smuzhiyun int cipso_v4_cache_add(const unsigned char *cipso_ptr,
298*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun int bkt_size = READ_ONCE(cipso_v4_cache_bucketsize);
301*4882a593Smuzhiyun int ret_val = -EPERM;
302*4882a593Smuzhiyun u32 bkt;
303*4882a593Smuzhiyun struct cipso_v4_map_cache_entry *entry = NULL;
304*4882a593Smuzhiyun struct cipso_v4_map_cache_entry *old_entry = NULL;
305*4882a593Smuzhiyun u32 cipso_ptr_len;
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun if (!READ_ONCE(cipso_v4_cache_enabled) || bkt_size <= 0)
308*4882a593Smuzhiyun return 0;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun cipso_ptr_len = cipso_ptr[1];
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
313*4882a593Smuzhiyun if (!entry)
314*4882a593Smuzhiyun return -ENOMEM;
315*4882a593Smuzhiyun entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
316*4882a593Smuzhiyun if (!entry->key) {
317*4882a593Smuzhiyun ret_val = -ENOMEM;
318*4882a593Smuzhiyun goto cache_add_failure;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun entry->key_len = cipso_ptr_len;
321*4882a593Smuzhiyun entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
322*4882a593Smuzhiyun refcount_inc(&secattr->cache->refcount);
323*4882a593Smuzhiyun entry->lsm_data = secattr->cache;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETS - 1);
326*4882a593Smuzhiyun spin_lock_bh(&cipso_v4_cache[bkt].lock);
327*4882a593Smuzhiyun if (cipso_v4_cache[bkt].size < bkt_size) {
328*4882a593Smuzhiyun list_add(&entry->list, &cipso_v4_cache[bkt].list);
329*4882a593Smuzhiyun cipso_v4_cache[bkt].size += 1;
330*4882a593Smuzhiyun } else {
331*4882a593Smuzhiyun old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
332*4882a593Smuzhiyun struct cipso_v4_map_cache_entry, list);
333*4882a593Smuzhiyun list_del(&old_entry->list);
334*4882a593Smuzhiyun list_add(&entry->list, &cipso_v4_cache[bkt].list);
335*4882a593Smuzhiyun cipso_v4_cache_entry_free(old_entry);
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun spin_unlock_bh(&cipso_v4_cache[bkt].lock);
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun return 0;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun cache_add_failure:
342*4882a593Smuzhiyun if (entry)
343*4882a593Smuzhiyun cipso_v4_cache_entry_free(entry);
344*4882a593Smuzhiyun return ret_val;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /*
348*4882a593Smuzhiyun * DOI List Functions
349*4882a593Smuzhiyun */
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /**
352*4882a593Smuzhiyun * cipso_v4_doi_search - Searches for a DOI definition
353*4882a593Smuzhiyun * @doi: the DOI to search for
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * Description:
356*4882a593Smuzhiyun * Search the DOI definition list for a DOI definition with a DOI value that
357*4882a593Smuzhiyun * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
358*4882a593Smuzhiyun * Returns a pointer to the DOI definition on success and NULL on failure.
359*4882a593Smuzhiyun */
cipso_v4_doi_search(u32 doi)360*4882a593Smuzhiyun static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun struct cipso_v4_doi *iter;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
365*4882a593Smuzhiyun if (iter->doi == doi && refcount_read(&iter->refcount))
366*4882a593Smuzhiyun return iter;
367*4882a593Smuzhiyun return NULL;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun /**
371*4882a593Smuzhiyun * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
372*4882a593Smuzhiyun * @doi_def: the DOI structure
373*4882a593Smuzhiyun * @audit_info: NetLabel audit information
374*4882a593Smuzhiyun *
375*4882a593Smuzhiyun * Description:
376*4882a593Smuzhiyun * The caller defines a new DOI for use by the CIPSO engine and calls this
377*4882a593Smuzhiyun * function to add it to the list of acceptable domains. The caller must
378*4882a593Smuzhiyun * ensure that the mapping table specified in @doi_def->map meets all of the
379*4882a593Smuzhiyun * requirements of the mapping type (see cipso_ipv4.h for details). Returns
380*4882a593Smuzhiyun * zero on success and non-zero on failure.
381*4882a593Smuzhiyun *
382*4882a593Smuzhiyun */
cipso_v4_doi_add(struct cipso_v4_doi * doi_def,struct netlbl_audit * audit_info)383*4882a593Smuzhiyun int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
384*4882a593Smuzhiyun struct netlbl_audit *audit_info)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun int ret_val = -EINVAL;
387*4882a593Smuzhiyun u32 iter;
388*4882a593Smuzhiyun u32 doi;
389*4882a593Smuzhiyun u32 doi_type;
390*4882a593Smuzhiyun struct audit_buffer *audit_buf;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun doi = doi_def->doi;
393*4882a593Smuzhiyun doi_type = doi_def->type;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if (doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
396*4882a593Smuzhiyun goto doi_add_return;
397*4882a593Smuzhiyun for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
398*4882a593Smuzhiyun switch (doi_def->tags[iter]) {
399*4882a593Smuzhiyun case CIPSO_V4_TAG_RBITMAP:
400*4882a593Smuzhiyun break;
401*4882a593Smuzhiyun case CIPSO_V4_TAG_RANGE:
402*4882a593Smuzhiyun case CIPSO_V4_TAG_ENUM:
403*4882a593Smuzhiyun if (doi_def->type != CIPSO_V4_MAP_PASS)
404*4882a593Smuzhiyun goto doi_add_return;
405*4882a593Smuzhiyun break;
406*4882a593Smuzhiyun case CIPSO_V4_TAG_LOCAL:
407*4882a593Smuzhiyun if (doi_def->type != CIPSO_V4_MAP_LOCAL)
408*4882a593Smuzhiyun goto doi_add_return;
409*4882a593Smuzhiyun break;
410*4882a593Smuzhiyun case CIPSO_V4_TAG_INVALID:
411*4882a593Smuzhiyun if (iter == 0)
412*4882a593Smuzhiyun goto doi_add_return;
413*4882a593Smuzhiyun break;
414*4882a593Smuzhiyun default:
415*4882a593Smuzhiyun goto doi_add_return;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun refcount_set(&doi_def->refcount, 1);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun spin_lock(&cipso_v4_doi_list_lock);
422*4882a593Smuzhiyun if (cipso_v4_doi_search(doi_def->doi)) {
423*4882a593Smuzhiyun spin_unlock(&cipso_v4_doi_list_lock);
424*4882a593Smuzhiyun ret_val = -EEXIST;
425*4882a593Smuzhiyun goto doi_add_return;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
428*4882a593Smuzhiyun spin_unlock(&cipso_v4_doi_list_lock);
429*4882a593Smuzhiyun ret_val = 0;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun doi_add_return:
432*4882a593Smuzhiyun audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
433*4882a593Smuzhiyun if (audit_buf) {
434*4882a593Smuzhiyun const char *type_str;
435*4882a593Smuzhiyun switch (doi_type) {
436*4882a593Smuzhiyun case CIPSO_V4_MAP_TRANS:
437*4882a593Smuzhiyun type_str = "trans";
438*4882a593Smuzhiyun break;
439*4882a593Smuzhiyun case CIPSO_V4_MAP_PASS:
440*4882a593Smuzhiyun type_str = "pass";
441*4882a593Smuzhiyun break;
442*4882a593Smuzhiyun case CIPSO_V4_MAP_LOCAL:
443*4882a593Smuzhiyun type_str = "local";
444*4882a593Smuzhiyun break;
445*4882a593Smuzhiyun default:
446*4882a593Smuzhiyun type_str = "(unknown)";
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun audit_log_format(audit_buf,
449*4882a593Smuzhiyun " cipso_doi=%u cipso_type=%s res=%u",
450*4882a593Smuzhiyun doi, type_str, ret_val == 0 ? 1 : 0);
451*4882a593Smuzhiyun audit_log_end(audit_buf);
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun return ret_val;
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun /**
458*4882a593Smuzhiyun * cipso_v4_doi_free - Frees a DOI definition
459*4882a593Smuzhiyun * @doi_def: the DOI definition
460*4882a593Smuzhiyun *
461*4882a593Smuzhiyun * Description:
462*4882a593Smuzhiyun * This function frees all of the memory associated with a DOI definition.
463*4882a593Smuzhiyun *
464*4882a593Smuzhiyun */
cipso_v4_doi_free(struct cipso_v4_doi * doi_def)465*4882a593Smuzhiyun void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
466*4882a593Smuzhiyun {
467*4882a593Smuzhiyun if (!doi_def)
468*4882a593Smuzhiyun return;
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun switch (doi_def->type) {
471*4882a593Smuzhiyun case CIPSO_V4_MAP_TRANS:
472*4882a593Smuzhiyun kfree(doi_def->map.std->lvl.cipso);
473*4882a593Smuzhiyun kfree(doi_def->map.std->lvl.local);
474*4882a593Smuzhiyun kfree(doi_def->map.std->cat.cipso);
475*4882a593Smuzhiyun kfree(doi_def->map.std->cat.local);
476*4882a593Smuzhiyun kfree(doi_def->map.std);
477*4882a593Smuzhiyun break;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun kfree(doi_def);
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun /**
483*4882a593Smuzhiyun * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
484*4882a593Smuzhiyun * @entry: the entry's RCU field
485*4882a593Smuzhiyun *
486*4882a593Smuzhiyun * Description:
487*4882a593Smuzhiyun * This function is designed to be used as a callback to the call_rcu()
488*4882a593Smuzhiyun * function so that the memory allocated to the DOI definition can be released
489*4882a593Smuzhiyun * safely.
490*4882a593Smuzhiyun *
491*4882a593Smuzhiyun */
cipso_v4_doi_free_rcu(struct rcu_head * entry)492*4882a593Smuzhiyun static void cipso_v4_doi_free_rcu(struct rcu_head *entry)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun struct cipso_v4_doi *doi_def;
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun doi_def = container_of(entry, struct cipso_v4_doi, rcu);
497*4882a593Smuzhiyun cipso_v4_doi_free(doi_def);
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun /**
501*4882a593Smuzhiyun * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
502*4882a593Smuzhiyun * @doi: the DOI value
503*4882a593Smuzhiyun * @audit_info: NetLabel audit information
504*4882a593Smuzhiyun *
505*4882a593Smuzhiyun * Description:
506*4882a593Smuzhiyun * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
507*4882a593Smuzhiyun * be called to release their own LSM domain mappings as well as our own
508*4882a593Smuzhiyun * domain list. Returns zero on success and negative values on failure.
509*4882a593Smuzhiyun *
510*4882a593Smuzhiyun */
cipso_v4_doi_remove(u32 doi,struct netlbl_audit * audit_info)511*4882a593Smuzhiyun int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
512*4882a593Smuzhiyun {
513*4882a593Smuzhiyun int ret_val;
514*4882a593Smuzhiyun struct cipso_v4_doi *doi_def;
515*4882a593Smuzhiyun struct audit_buffer *audit_buf;
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun spin_lock(&cipso_v4_doi_list_lock);
518*4882a593Smuzhiyun doi_def = cipso_v4_doi_search(doi);
519*4882a593Smuzhiyun if (!doi_def) {
520*4882a593Smuzhiyun spin_unlock(&cipso_v4_doi_list_lock);
521*4882a593Smuzhiyun ret_val = -ENOENT;
522*4882a593Smuzhiyun goto doi_remove_return;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun list_del_rcu(&doi_def->list);
525*4882a593Smuzhiyun spin_unlock(&cipso_v4_doi_list_lock);
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun cipso_v4_doi_putdef(doi_def);
528*4882a593Smuzhiyun ret_val = 0;
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun doi_remove_return:
531*4882a593Smuzhiyun audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info);
532*4882a593Smuzhiyun if (audit_buf) {
533*4882a593Smuzhiyun audit_log_format(audit_buf,
534*4882a593Smuzhiyun " cipso_doi=%u res=%u",
535*4882a593Smuzhiyun doi, ret_val == 0 ? 1 : 0);
536*4882a593Smuzhiyun audit_log_end(audit_buf);
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun return ret_val;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun /**
543*4882a593Smuzhiyun * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
544*4882a593Smuzhiyun * @doi: the DOI value
545*4882a593Smuzhiyun *
546*4882a593Smuzhiyun * Description:
547*4882a593Smuzhiyun * Searches for a valid DOI definition and if one is found it is returned to
548*4882a593Smuzhiyun * the caller. Otherwise NULL is returned. The caller must ensure that
549*4882a593Smuzhiyun * rcu_read_lock() is held while accessing the returned definition and the DOI
550*4882a593Smuzhiyun * definition reference count is decremented when the caller is done.
551*4882a593Smuzhiyun *
552*4882a593Smuzhiyun */
cipso_v4_doi_getdef(u32 doi)553*4882a593Smuzhiyun struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun struct cipso_v4_doi *doi_def;
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun rcu_read_lock();
558*4882a593Smuzhiyun doi_def = cipso_v4_doi_search(doi);
559*4882a593Smuzhiyun if (!doi_def)
560*4882a593Smuzhiyun goto doi_getdef_return;
561*4882a593Smuzhiyun if (!refcount_inc_not_zero(&doi_def->refcount))
562*4882a593Smuzhiyun doi_def = NULL;
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun doi_getdef_return:
565*4882a593Smuzhiyun rcu_read_unlock();
566*4882a593Smuzhiyun return doi_def;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /**
570*4882a593Smuzhiyun * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
571*4882a593Smuzhiyun * @doi_def: the DOI definition
572*4882a593Smuzhiyun *
573*4882a593Smuzhiyun * Description:
574*4882a593Smuzhiyun * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
575*4882a593Smuzhiyun *
576*4882a593Smuzhiyun */
cipso_v4_doi_putdef(struct cipso_v4_doi * doi_def)577*4882a593Smuzhiyun void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
578*4882a593Smuzhiyun {
579*4882a593Smuzhiyun if (!doi_def)
580*4882a593Smuzhiyun return;
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun if (!refcount_dec_and_test(&doi_def->refcount))
583*4882a593Smuzhiyun return;
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun cipso_v4_cache_invalidate();
586*4882a593Smuzhiyun call_rcu(&doi_def->rcu, cipso_v4_doi_free_rcu);
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /**
590*4882a593Smuzhiyun * cipso_v4_doi_walk - Iterate through the DOI definitions
591*4882a593Smuzhiyun * @skip_cnt: skip past this number of DOI definitions, updated
592*4882a593Smuzhiyun * @callback: callback for each DOI definition
593*4882a593Smuzhiyun * @cb_arg: argument for the callback function
594*4882a593Smuzhiyun *
595*4882a593Smuzhiyun * Description:
596*4882a593Smuzhiyun * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
597*4882a593Smuzhiyun * For each entry call @callback, if @callback returns a negative value stop
598*4882a593Smuzhiyun * 'walking' through the list and return. Updates the value in @skip_cnt upon
599*4882a593Smuzhiyun * return. Returns zero on success, negative values on failure.
600*4882a593Smuzhiyun *
601*4882a593Smuzhiyun */
cipso_v4_doi_walk(u32 * skip_cnt,int (* callback)(struct cipso_v4_doi * doi_def,void * arg),void * cb_arg)602*4882a593Smuzhiyun int cipso_v4_doi_walk(u32 *skip_cnt,
603*4882a593Smuzhiyun int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
604*4882a593Smuzhiyun void *cb_arg)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun int ret_val = -ENOENT;
607*4882a593Smuzhiyun u32 doi_cnt = 0;
608*4882a593Smuzhiyun struct cipso_v4_doi *iter_doi;
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun rcu_read_lock();
611*4882a593Smuzhiyun list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
612*4882a593Smuzhiyun if (refcount_read(&iter_doi->refcount) > 0) {
613*4882a593Smuzhiyun if (doi_cnt++ < *skip_cnt)
614*4882a593Smuzhiyun continue;
615*4882a593Smuzhiyun ret_val = callback(iter_doi, cb_arg);
616*4882a593Smuzhiyun if (ret_val < 0) {
617*4882a593Smuzhiyun doi_cnt--;
618*4882a593Smuzhiyun goto doi_walk_return;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun doi_walk_return:
623*4882a593Smuzhiyun rcu_read_unlock();
624*4882a593Smuzhiyun *skip_cnt = doi_cnt;
625*4882a593Smuzhiyun return ret_val;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun /*
629*4882a593Smuzhiyun * Label Mapping Functions
630*4882a593Smuzhiyun */
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun /**
633*4882a593Smuzhiyun * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
634*4882a593Smuzhiyun * @doi_def: the DOI definition
635*4882a593Smuzhiyun * @level: the level to check
636*4882a593Smuzhiyun *
637*4882a593Smuzhiyun * Description:
638*4882a593Smuzhiyun * Checks the given level against the given DOI definition and returns a
639*4882a593Smuzhiyun * negative value if the level does not have a valid mapping and a zero value
640*4882a593Smuzhiyun * if the level is defined by the DOI.
641*4882a593Smuzhiyun *
642*4882a593Smuzhiyun */
cipso_v4_map_lvl_valid(const struct cipso_v4_doi * doi_def,u8 level)643*4882a593Smuzhiyun static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
644*4882a593Smuzhiyun {
645*4882a593Smuzhiyun switch (doi_def->type) {
646*4882a593Smuzhiyun case CIPSO_V4_MAP_PASS:
647*4882a593Smuzhiyun return 0;
648*4882a593Smuzhiyun case CIPSO_V4_MAP_TRANS:
649*4882a593Smuzhiyun if ((level < doi_def->map.std->lvl.cipso_size) &&
650*4882a593Smuzhiyun (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL))
651*4882a593Smuzhiyun return 0;
652*4882a593Smuzhiyun break;
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun return -EFAULT;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun /**
659*4882a593Smuzhiyun * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
660*4882a593Smuzhiyun * @doi_def: the DOI definition
661*4882a593Smuzhiyun * @host_lvl: the host MLS level
662*4882a593Smuzhiyun * @net_lvl: the network/CIPSO MLS level
663*4882a593Smuzhiyun *
664*4882a593Smuzhiyun * Description:
665*4882a593Smuzhiyun * Perform a label mapping to translate a local MLS level to the correct
666*4882a593Smuzhiyun * CIPSO level using the given DOI definition. Returns zero on success,
667*4882a593Smuzhiyun * negative values otherwise.
668*4882a593Smuzhiyun *
669*4882a593Smuzhiyun */
cipso_v4_map_lvl_hton(const struct cipso_v4_doi * doi_def,u32 host_lvl,u32 * net_lvl)670*4882a593Smuzhiyun static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
671*4882a593Smuzhiyun u32 host_lvl,
672*4882a593Smuzhiyun u32 *net_lvl)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun switch (doi_def->type) {
675*4882a593Smuzhiyun case CIPSO_V4_MAP_PASS:
676*4882a593Smuzhiyun *net_lvl = host_lvl;
677*4882a593Smuzhiyun return 0;
678*4882a593Smuzhiyun case CIPSO_V4_MAP_TRANS:
679*4882a593Smuzhiyun if (host_lvl < doi_def->map.std->lvl.local_size &&
680*4882a593Smuzhiyun doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
681*4882a593Smuzhiyun *net_lvl = doi_def->map.std->lvl.local[host_lvl];
682*4882a593Smuzhiyun return 0;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun return -EPERM;
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun return -EINVAL;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun /**
691*4882a593Smuzhiyun * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
692*4882a593Smuzhiyun * @doi_def: the DOI definition
693*4882a593Smuzhiyun * @net_lvl: the network/CIPSO MLS level
694*4882a593Smuzhiyun * @host_lvl: the host MLS level
695*4882a593Smuzhiyun *
696*4882a593Smuzhiyun * Description:
697*4882a593Smuzhiyun * Perform a label mapping to translate a CIPSO level to the correct local MLS
698*4882a593Smuzhiyun * level using the given DOI definition. Returns zero on success, negative
699*4882a593Smuzhiyun * values otherwise.
700*4882a593Smuzhiyun *
701*4882a593Smuzhiyun */
cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi * doi_def,u32 net_lvl,u32 * host_lvl)702*4882a593Smuzhiyun static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
703*4882a593Smuzhiyun u32 net_lvl,
704*4882a593Smuzhiyun u32 *host_lvl)
705*4882a593Smuzhiyun {
706*4882a593Smuzhiyun struct cipso_v4_std_map_tbl *map_tbl;
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun switch (doi_def->type) {
709*4882a593Smuzhiyun case CIPSO_V4_MAP_PASS:
710*4882a593Smuzhiyun *host_lvl = net_lvl;
711*4882a593Smuzhiyun return 0;
712*4882a593Smuzhiyun case CIPSO_V4_MAP_TRANS:
713*4882a593Smuzhiyun map_tbl = doi_def->map.std;
714*4882a593Smuzhiyun if (net_lvl < map_tbl->lvl.cipso_size &&
715*4882a593Smuzhiyun map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
716*4882a593Smuzhiyun *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
717*4882a593Smuzhiyun return 0;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun return -EPERM;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun return -EINVAL;
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun /**
726*4882a593Smuzhiyun * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
727*4882a593Smuzhiyun * @doi_def: the DOI definition
728*4882a593Smuzhiyun * @bitmap: category bitmap
729*4882a593Smuzhiyun * @bitmap_len: bitmap length in bytes
730*4882a593Smuzhiyun *
731*4882a593Smuzhiyun * Description:
732*4882a593Smuzhiyun * Checks the given category bitmap against the given DOI definition and
733*4882a593Smuzhiyun * returns a negative value if any of the categories in the bitmap do not have
734*4882a593Smuzhiyun * a valid mapping and a zero value if all of the categories are valid.
735*4882a593Smuzhiyun *
736*4882a593Smuzhiyun */
cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi * doi_def,const unsigned char * bitmap,u32 bitmap_len)737*4882a593Smuzhiyun static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
738*4882a593Smuzhiyun const unsigned char *bitmap,
739*4882a593Smuzhiyun u32 bitmap_len)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun int cat = -1;
742*4882a593Smuzhiyun u32 bitmap_len_bits = bitmap_len * 8;
743*4882a593Smuzhiyun u32 cipso_cat_size;
744*4882a593Smuzhiyun u32 *cipso_array;
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun switch (doi_def->type) {
747*4882a593Smuzhiyun case CIPSO_V4_MAP_PASS:
748*4882a593Smuzhiyun return 0;
749*4882a593Smuzhiyun case CIPSO_V4_MAP_TRANS:
750*4882a593Smuzhiyun cipso_cat_size = doi_def->map.std->cat.cipso_size;
751*4882a593Smuzhiyun cipso_array = doi_def->map.std->cat.cipso;
752*4882a593Smuzhiyun for (;;) {
753*4882a593Smuzhiyun cat = netlbl_bitmap_walk(bitmap,
754*4882a593Smuzhiyun bitmap_len_bits,
755*4882a593Smuzhiyun cat + 1,
756*4882a593Smuzhiyun 1);
757*4882a593Smuzhiyun if (cat < 0)
758*4882a593Smuzhiyun break;
759*4882a593Smuzhiyun if (cat >= cipso_cat_size ||
760*4882a593Smuzhiyun cipso_array[cat] >= CIPSO_V4_INV_CAT)
761*4882a593Smuzhiyun return -EFAULT;
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun if (cat == -1)
765*4882a593Smuzhiyun return 0;
766*4882a593Smuzhiyun break;
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun return -EFAULT;
770*4882a593Smuzhiyun }
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun /**
773*4882a593Smuzhiyun * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
774*4882a593Smuzhiyun * @doi_def: the DOI definition
775*4882a593Smuzhiyun * @secattr: the security attributes
776*4882a593Smuzhiyun * @net_cat: the zero'd out category bitmap in network/CIPSO format
777*4882a593Smuzhiyun * @net_cat_len: the length of the CIPSO bitmap in bytes
778*4882a593Smuzhiyun *
779*4882a593Smuzhiyun * Description:
780*4882a593Smuzhiyun * Perform a label mapping to translate a local MLS category bitmap to the
781*4882a593Smuzhiyun * correct CIPSO bitmap using the given DOI definition. Returns the minimum
782*4882a593Smuzhiyun * size in bytes of the network bitmap on success, negative values otherwise.
783*4882a593Smuzhiyun *
784*4882a593Smuzhiyun */
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)785*4882a593Smuzhiyun static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
786*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr,
787*4882a593Smuzhiyun unsigned char *net_cat,
788*4882a593Smuzhiyun u32 net_cat_len)
789*4882a593Smuzhiyun {
790*4882a593Smuzhiyun int host_spot = -1;
791*4882a593Smuzhiyun u32 net_spot = CIPSO_V4_INV_CAT;
792*4882a593Smuzhiyun u32 net_spot_max = 0;
793*4882a593Smuzhiyun u32 net_clen_bits = net_cat_len * 8;
794*4882a593Smuzhiyun u32 host_cat_size = 0;
795*4882a593Smuzhiyun u32 *host_cat_array = NULL;
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun if (doi_def->type == CIPSO_V4_MAP_TRANS) {
798*4882a593Smuzhiyun host_cat_size = doi_def->map.std->cat.local_size;
799*4882a593Smuzhiyun host_cat_array = doi_def->map.std->cat.local;
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun for (;;) {
803*4882a593Smuzhiyun host_spot = netlbl_catmap_walk(secattr->attr.mls.cat,
804*4882a593Smuzhiyun host_spot + 1);
805*4882a593Smuzhiyun if (host_spot < 0)
806*4882a593Smuzhiyun break;
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun switch (doi_def->type) {
809*4882a593Smuzhiyun case CIPSO_V4_MAP_PASS:
810*4882a593Smuzhiyun net_spot = host_spot;
811*4882a593Smuzhiyun break;
812*4882a593Smuzhiyun case CIPSO_V4_MAP_TRANS:
813*4882a593Smuzhiyun if (host_spot >= host_cat_size)
814*4882a593Smuzhiyun return -EPERM;
815*4882a593Smuzhiyun net_spot = host_cat_array[host_spot];
816*4882a593Smuzhiyun if (net_spot >= CIPSO_V4_INV_CAT)
817*4882a593Smuzhiyun return -EPERM;
818*4882a593Smuzhiyun break;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun if (net_spot >= net_clen_bits)
821*4882a593Smuzhiyun return -ENOSPC;
822*4882a593Smuzhiyun netlbl_bitmap_setbit(net_cat, net_spot, 1);
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun if (net_spot > net_spot_max)
825*4882a593Smuzhiyun net_spot_max = net_spot;
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun if (++net_spot_max % 8)
829*4882a593Smuzhiyun return net_spot_max / 8 + 1;
830*4882a593Smuzhiyun return net_spot_max / 8;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /**
834*4882a593Smuzhiyun * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
835*4882a593Smuzhiyun * @doi_def: the DOI definition
836*4882a593Smuzhiyun * @net_cat: the category bitmap in network/CIPSO format
837*4882a593Smuzhiyun * @net_cat_len: the length of the CIPSO bitmap in bytes
838*4882a593Smuzhiyun * @secattr: the security attributes
839*4882a593Smuzhiyun *
840*4882a593Smuzhiyun * Description:
841*4882a593Smuzhiyun * Perform a label mapping to translate a CIPSO bitmap to the correct local
842*4882a593Smuzhiyun * MLS category bitmap using the given DOI definition. Returns zero on
843*4882a593Smuzhiyun * success, negative values on failure.
844*4882a593Smuzhiyun *
845*4882a593Smuzhiyun */
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)846*4882a593Smuzhiyun static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
847*4882a593Smuzhiyun const unsigned char *net_cat,
848*4882a593Smuzhiyun u32 net_cat_len,
849*4882a593Smuzhiyun struct netlbl_lsm_secattr *secattr)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun int ret_val;
852*4882a593Smuzhiyun int net_spot = -1;
853*4882a593Smuzhiyun u32 host_spot = CIPSO_V4_INV_CAT;
854*4882a593Smuzhiyun u32 net_clen_bits = net_cat_len * 8;
855*4882a593Smuzhiyun u32 net_cat_size = 0;
856*4882a593Smuzhiyun u32 *net_cat_array = NULL;
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun if (doi_def->type == CIPSO_V4_MAP_TRANS) {
859*4882a593Smuzhiyun net_cat_size = doi_def->map.std->cat.cipso_size;
860*4882a593Smuzhiyun net_cat_array = doi_def->map.std->cat.cipso;
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun for (;;) {
864*4882a593Smuzhiyun net_spot = netlbl_bitmap_walk(net_cat,
865*4882a593Smuzhiyun net_clen_bits,
866*4882a593Smuzhiyun net_spot + 1,
867*4882a593Smuzhiyun 1);
868*4882a593Smuzhiyun if (net_spot < 0) {
869*4882a593Smuzhiyun if (net_spot == -2)
870*4882a593Smuzhiyun return -EFAULT;
871*4882a593Smuzhiyun return 0;
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun switch (doi_def->type) {
875*4882a593Smuzhiyun case CIPSO_V4_MAP_PASS:
876*4882a593Smuzhiyun host_spot = net_spot;
877*4882a593Smuzhiyun break;
878*4882a593Smuzhiyun case CIPSO_V4_MAP_TRANS:
879*4882a593Smuzhiyun if (net_spot >= net_cat_size)
880*4882a593Smuzhiyun return -EPERM;
881*4882a593Smuzhiyun host_spot = net_cat_array[net_spot];
882*4882a593Smuzhiyun if (host_spot >= CIPSO_V4_INV_CAT)
883*4882a593Smuzhiyun return -EPERM;
884*4882a593Smuzhiyun break;
885*4882a593Smuzhiyun }
886*4882a593Smuzhiyun ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
887*4882a593Smuzhiyun host_spot,
888*4882a593Smuzhiyun GFP_ATOMIC);
889*4882a593Smuzhiyun if (ret_val != 0)
890*4882a593Smuzhiyun return ret_val;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun return -EINVAL;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun /**
897*4882a593Smuzhiyun * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
898*4882a593Smuzhiyun * @doi_def: the DOI definition
899*4882a593Smuzhiyun * @enumcat: category list
900*4882a593Smuzhiyun * @enumcat_len: length of the category list in bytes
901*4882a593Smuzhiyun *
902*4882a593Smuzhiyun * Description:
903*4882a593Smuzhiyun * Checks the given categories against the given DOI definition and returns a
904*4882a593Smuzhiyun * negative value if any of the categories do not have a valid mapping and a
905*4882a593Smuzhiyun * zero value if all of the categories are valid.
906*4882a593Smuzhiyun *
907*4882a593Smuzhiyun */
cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi * doi_def,const unsigned char * enumcat,u32 enumcat_len)908*4882a593Smuzhiyun static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
909*4882a593Smuzhiyun const unsigned char *enumcat,
910*4882a593Smuzhiyun u32 enumcat_len)
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun u16 cat;
913*4882a593Smuzhiyun int cat_prev = -1;
914*4882a593Smuzhiyun u32 iter;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
917*4882a593Smuzhiyun return -EFAULT;
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun for (iter = 0; iter < enumcat_len; iter += 2) {
920*4882a593Smuzhiyun cat = get_unaligned_be16(&enumcat[iter]);
921*4882a593Smuzhiyun if (cat <= cat_prev)
922*4882a593Smuzhiyun return -EFAULT;
923*4882a593Smuzhiyun cat_prev = cat;
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun
926*4882a593Smuzhiyun return 0;
927*4882a593Smuzhiyun }
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun /**
930*4882a593Smuzhiyun * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
931*4882a593Smuzhiyun * @doi_def: the DOI definition
932*4882a593Smuzhiyun * @secattr: the security attributes
933*4882a593Smuzhiyun * @net_cat: the zero'd out category list in network/CIPSO format
934*4882a593Smuzhiyun * @net_cat_len: the length of the CIPSO category list in bytes
935*4882a593Smuzhiyun *
936*4882a593Smuzhiyun * Description:
937*4882a593Smuzhiyun * Perform a label mapping to translate a local MLS category bitmap to the
938*4882a593Smuzhiyun * correct CIPSO category list using the given DOI definition. Returns the
939*4882a593Smuzhiyun * size in bytes of the network category bitmap on success, negative values
940*4882a593Smuzhiyun * otherwise.
941*4882a593Smuzhiyun *
942*4882a593Smuzhiyun */
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)943*4882a593Smuzhiyun static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
944*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr,
945*4882a593Smuzhiyun unsigned char *net_cat,
946*4882a593Smuzhiyun u32 net_cat_len)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun int cat = -1;
949*4882a593Smuzhiyun u32 cat_iter = 0;
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun for (;;) {
952*4882a593Smuzhiyun cat = netlbl_catmap_walk(secattr->attr.mls.cat, cat + 1);
953*4882a593Smuzhiyun if (cat < 0)
954*4882a593Smuzhiyun break;
955*4882a593Smuzhiyun if ((cat_iter + 2) > net_cat_len)
956*4882a593Smuzhiyun return -ENOSPC;
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun *((__be16 *)&net_cat[cat_iter]) = htons(cat);
959*4882a593Smuzhiyun cat_iter += 2;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun
962*4882a593Smuzhiyun return cat_iter;
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun /**
966*4882a593Smuzhiyun * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
967*4882a593Smuzhiyun * @doi_def: the DOI definition
968*4882a593Smuzhiyun * @net_cat: the category list in network/CIPSO format
969*4882a593Smuzhiyun * @net_cat_len: the length of the CIPSO bitmap in bytes
970*4882a593Smuzhiyun * @secattr: the security attributes
971*4882a593Smuzhiyun *
972*4882a593Smuzhiyun * Description:
973*4882a593Smuzhiyun * Perform a label mapping to translate a CIPSO category list to the correct
974*4882a593Smuzhiyun * local MLS category bitmap using the given DOI definition. Returns zero on
975*4882a593Smuzhiyun * success, negative values on failure.
976*4882a593Smuzhiyun *
977*4882a593Smuzhiyun */
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)978*4882a593Smuzhiyun static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
979*4882a593Smuzhiyun const unsigned char *net_cat,
980*4882a593Smuzhiyun u32 net_cat_len,
981*4882a593Smuzhiyun struct netlbl_lsm_secattr *secattr)
982*4882a593Smuzhiyun {
983*4882a593Smuzhiyun int ret_val;
984*4882a593Smuzhiyun u32 iter;
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun for (iter = 0; iter < net_cat_len; iter += 2) {
987*4882a593Smuzhiyun ret_val = netlbl_catmap_setbit(&secattr->attr.mls.cat,
988*4882a593Smuzhiyun get_unaligned_be16(&net_cat[iter]),
989*4882a593Smuzhiyun GFP_ATOMIC);
990*4882a593Smuzhiyun if (ret_val != 0)
991*4882a593Smuzhiyun return ret_val;
992*4882a593Smuzhiyun }
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun return 0;
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun /**
998*4882a593Smuzhiyun * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
999*4882a593Smuzhiyun * @doi_def: the DOI definition
1000*4882a593Smuzhiyun * @rngcat: category list
1001*4882a593Smuzhiyun * @rngcat_len: length of the category list in bytes
1002*4882a593Smuzhiyun *
1003*4882a593Smuzhiyun * Description:
1004*4882a593Smuzhiyun * Checks the given categories against the given DOI definition and returns a
1005*4882a593Smuzhiyun * negative value if any of the categories do not have a valid mapping and a
1006*4882a593Smuzhiyun * zero value if all of the categories are valid.
1007*4882a593Smuzhiyun *
1008*4882a593Smuzhiyun */
cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi * doi_def,const unsigned char * rngcat,u32 rngcat_len)1009*4882a593Smuzhiyun static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1010*4882a593Smuzhiyun const unsigned char *rngcat,
1011*4882a593Smuzhiyun u32 rngcat_len)
1012*4882a593Smuzhiyun {
1013*4882a593Smuzhiyun u16 cat_high;
1014*4882a593Smuzhiyun u16 cat_low;
1015*4882a593Smuzhiyun u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1016*4882a593Smuzhiyun u32 iter;
1017*4882a593Smuzhiyun
1018*4882a593Smuzhiyun if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1019*4882a593Smuzhiyun return -EFAULT;
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun for (iter = 0; iter < rngcat_len; iter += 4) {
1022*4882a593Smuzhiyun cat_high = get_unaligned_be16(&rngcat[iter]);
1023*4882a593Smuzhiyun if ((iter + 4) <= rngcat_len)
1024*4882a593Smuzhiyun cat_low = get_unaligned_be16(&rngcat[iter + 2]);
1025*4882a593Smuzhiyun else
1026*4882a593Smuzhiyun cat_low = 0;
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun if (cat_high > cat_prev)
1029*4882a593Smuzhiyun return -EFAULT;
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun cat_prev = cat_low;
1032*4882a593Smuzhiyun }
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun return 0;
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun /**
1038*4882a593Smuzhiyun * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1039*4882a593Smuzhiyun * @doi_def: the DOI definition
1040*4882a593Smuzhiyun * @secattr: the security attributes
1041*4882a593Smuzhiyun * @net_cat: the zero'd out category list in network/CIPSO format
1042*4882a593Smuzhiyun * @net_cat_len: the length of the CIPSO category list in bytes
1043*4882a593Smuzhiyun *
1044*4882a593Smuzhiyun * Description:
1045*4882a593Smuzhiyun * Perform a label mapping to translate a local MLS category bitmap to the
1046*4882a593Smuzhiyun * correct CIPSO category list using the given DOI definition. Returns the
1047*4882a593Smuzhiyun * size in bytes of the network category bitmap on success, negative values
1048*4882a593Smuzhiyun * otherwise.
1049*4882a593Smuzhiyun *
1050*4882a593Smuzhiyun */
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)1051*4882a593Smuzhiyun static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1052*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr,
1053*4882a593Smuzhiyun unsigned char *net_cat,
1054*4882a593Smuzhiyun u32 net_cat_len)
1055*4882a593Smuzhiyun {
1056*4882a593Smuzhiyun int iter = -1;
1057*4882a593Smuzhiyun u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
1058*4882a593Smuzhiyun u32 array_cnt = 0;
1059*4882a593Smuzhiyun u32 cat_size = 0;
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun /* make sure we don't overflow the 'array[]' variable */
1062*4882a593Smuzhiyun if (net_cat_len >
1063*4882a593Smuzhiyun (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1064*4882a593Smuzhiyun return -ENOSPC;
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun for (;;) {
1067*4882a593Smuzhiyun iter = netlbl_catmap_walk(secattr->attr.mls.cat, iter + 1);
1068*4882a593Smuzhiyun if (iter < 0)
1069*4882a593Smuzhiyun break;
1070*4882a593Smuzhiyun cat_size += (iter == 0 ? 0 : sizeof(u16));
1071*4882a593Smuzhiyun if (cat_size > net_cat_len)
1072*4882a593Smuzhiyun return -ENOSPC;
1073*4882a593Smuzhiyun array[array_cnt++] = iter;
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun iter = netlbl_catmap_walkrng(secattr->attr.mls.cat, iter);
1076*4882a593Smuzhiyun if (iter < 0)
1077*4882a593Smuzhiyun return -EFAULT;
1078*4882a593Smuzhiyun cat_size += sizeof(u16);
1079*4882a593Smuzhiyun if (cat_size > net_cat_len)
1080*4882a593Smuzhiyun return -ENOSPC;
1081*4882a593Smuzhiyun array[array_cnt++] = iter;
1082*4882a593Smuzhiyun }
1083*4882a593Smuzhiyun
1084*4882a593Smuzhiyun for (iter = 0; array_cnt > 0;) {
1085*4882a593Smuzhiyun *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1086*4882a593Smuzhiyun iter += 2;
1087*4882a593Smuzhiyun array_cnt--;
1088*4882a593Smuzhiyun if (array[array_cnt] != 0) {
1089*4882a593Smuzhiyun *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1090*4882a593Smuzhiyun iter += 2;
1091*4882a593Smuzhiyun }
1092*4882a593Smuzhiyun }
1093*4882a593Smuzhiyun
1094*4882a593Smuzhiyun return cat_size;
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun /**
1098*4882a593Smuzhiyun * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1099*4882a593Smuzhiyun * @doi_def: the DOI definition
1100*4882a593Smuzhiyun * @net_cat: the category list in network/CIPSO format
1101*4882a593Smuzhiyun * @net_cat_len: the length of the CIPSO bitmap in bytes
1102*4882a593Smuzhiyun * @secattr: the security attributes
1103*4882a593Smuzhiyun *
1104*4882a593Smuzhiyun * Description:
1105*4882a593Smuzhiyun * Perform a label mapping to translate a CIPSO category list to the correct
1106*4882a593Smuzhiyun * local MLS category bitmap using the given DOI definition. Returns zero on
1107*4882a593Smuzhiyun * success, negative values on failure.
1108*4882a593Smuzhiyun *
1109*4882a593Smuzhiyun */
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)1110*4882a593Smuzhiyun static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1111*4882a593Smuzhiyun const unsigned char *net_cat,
1112*4882a593Smuzhiyun u32 net_cat_len,
1113*4882a593Smuzhiyun struct netlbl_lsm_secattr *secattr)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun int ret_val;
1116*4882a593Smuzhiyun u32 net_iter;
1117*4882a593Smuzhiyun u16 cat_low;
1118*4882a593Smuzhiyun u16 cat_high;
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
1121*4882a593Smuzhiyun cat_high = get_unaligned_be16(&net_cat[net_iter]);
1122*4882a593Smuzhiyun if ((net_iter + 4) <= net_cat_len)
1123*4882a593Smuzhiyun cat_low = get_unaligned_be16(&net_cat[net_iter + 2]);
1124*4882a593Smuzhiyun else
1125*4882a593Smuzhiyun cat_low = 0;
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun ret_val = netlbl_catmap_setrng(&secattr->attr.mls.cat,
1128*4882a593Smuzhiyun cat_low,
1129*4882a593Smuzhiyun cat_high,
1130*4882a593Smuzhiyun GFP_ATOMIC);
1131*4882a593Smuzhiyun if (ret_val != 0)
1132*4882a593Smuzhiyun return ret_val;
1133*4882a593Smuzhiyun }
1134*4882a593Smuzhiyun
1135*4882a593Smuzhiyun return 0;
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun /*
1139*4882a593Smuzhiyun * Protocol Handling Functions
1140*4882a593Smuzhiyun */
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun /**
1143*4882a593Smuzhiyun * cipso_v4_gentag_hdr - Generate a CIPSO option header
1144*4882a593Smuzhiyun * @doi_def: the DOI definition
1145*4882a593Smuzhiyun * @len: the total tag length in bytes, not including this header
1146*4882a593Smuzhiyun * @buf: the CIPSO option buffer
1147*4882a593Smuzhiyun *
1148*4882a593Smuzhiyun * Description:
1149*4882a593Smuzhiyun * Write a CIPSO header into the beginning of @buffer.
1150*4882a593Smuzhiyun *
1151*4882a593Smuzhiyun */
cipso_v4_gentag_hdr(const struct cipso_v4_doi * doi_def,unsigned char * buf,u32 len)1152*4882a593Smuzhiyun static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1153*4882a593Smuzhiyun unsigned char *buf,
1154*4882a593Smuzhiyun u32 len)
1155*4882a593Smuzhiyun {
1156*4882a593Smuzhiyun buf[0] = IPOPT_CIPSO;
1157*4882a593Smuzhiyun buf[1] = CIPSO_V4_HDR_LEN + len;
1158*4882a593Smuzhiyun *(__be32 *)&buf[2] = htonl(doi_def->doi);
1159*4882a593Smuzhiyun }
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun /**
1162*4882a593Smuzhiyun * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1163*4882a593Smuzhiyun * @doi_def: the DOI definition
1164*4882a593Smuzhiyun * @secattr: the security attributes
1165*4882a593Smuzhiyun * @buffer: the option buffer
1166*4882a593Smuzhiyun * @buffer_len: length of buffer in bytes
1167*4882a593Smuzhiyun *
1168*4882a593Smuzhiyun * Description:
1169*4882a593Smuzhiyun * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1170*4882a593Smuzhiyun * actual buffer length may be larger than the indicated size due to
1171*4882a593Smuzhiyun * translation between host and network category bitmaps. Returns the size of
1172*4882a593Smuzhiyun * the tag on success, negative values on failure.
1173*4882a593Smuzhiyun *
1174*4882a593Smuzhiyun */
cipso_v4_gentag_rbm(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * buffer,u32 buffer_len)1175*4882a593Smuzhiyun static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1176*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr,
1177*4882a593Smuzhiyun unsigned char *buffer,
1178*4882a593Smuzhiyun u32 buffer_len)
1179*4882a593Smuzhiyun {
1180*4882a593Smuzhiyun int ret_val;
1181*4882a593Smuzhiyun u32 tag_len;
1182*4882a593Smuzhiyun u32 level;
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1185*4882a593Smuzhiyun return -EPERM;
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun ret_val = cipso_v4_map_lvl_hton(doi_def,
1188*4882a593Smuzhiyun secattr->attr.mls.lvl,
1189*4882a593Smuzhiyun &level);
1190*4882a593Smuzhiyun if (ret_val != 0)
1191*4882a593Smuzhiyun return ret_val;
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1194*4882a593Smuzhiyun ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
1195*4882a593Smuzhiyun secattr,
1196*4882a593Smuzhiyun &buffer[4],
1197*4882a593Smuzhiyun buffer_len - 4);
1198*4882a593Smuzhiyun if (ret_val < 0)
1199*4882a593Smuzhiyun return ret_val;
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun /* This will send packets using the "optimized" format when
1202*4882a593Smuzhiyun * possible as specified in section 3.4.2.6 of the
1203*4882a593Smuzhiyun * CIPSO draft. */
1204*4882a593Smuzhiyun if (READ_ONCE(cipso_v4_rbm_optfmt) && ret_val > 0 &&
1205*4882a593Smuzhiyun ret_val <= 10)
1206*4882a593Smuzhiyun tag_len = 14;
1207*4882a593Smuzhiyun else
1208*4882a593Smuzhiyun tag_len = 4 + ret_val;
1209*4882a593Smuzhiyun } else
1210*4882a593Smuzhiyun tag_len = 4;
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun buffer[0] = CIPSO_V4_TAG_RBITMAP;
1213*4882a593Smuzhiyun buffer[1] = tag_len;
1214*4882a593Smuzhiyun buffer[3] = level;
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun return tag_len;
1217*4882a593Smuzhiyun }
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun /**
1220*4882a593Smuzhiyun * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1221*4882a593Smuzhiyun * @doi_def: the DOI definition
1222*4882a593Smuzhiyun * @tag: the CIPSO tag
1223*4882a593Smuzhiyun * @secattr: the security attributes
1224*4882a593Smuzhiyun *
1225*4882a593Smuzhiyun * Description:
1226*4882a593Smuzhiyun * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1227*4882a593Smuzhiyun * attributes in @secattr. Return zero on success, negatives values on
1228*4882a593Smuzhiyun * failure.
1229*4882a593Smuzhiyun *
1230*4882a593Smuzhiyun */
cipso_v4_parsetag_rbm(const struct cipso_v4_doi * doi_def,const unsigned char * tag,struct netlbl_lsm_secattr * secattr)1231*4882a593Smuzhiyun static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1232*4882a593Smuzhiyun const unsigned char *tag,
1233*4882a593Smuzhiyun struct netlbl_lsm_secattr *secattr)
1234*4882a593Smuzhiyun {
1235*4882a593Smuzhiyun int ret_val;
1236*4882a593Smuzhiyun u8 tag_len = tag[1];
1237*4882a593Smuzhiyun u32 level;
1238*4882a593Smuzhiyun
1239*4882a593Smuzhiyun ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1240*4882a593Smuzhiyun if (ret_val != 0)
1241*4882a593Smuzhiyun return ret_val;
1242*4882a593Smuzhiyun secattr->attr.mls.lvl = level;
1243*4882a593Smuzhiyun secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun if (tag_len > 4) {
1246*4882a593Smuzhiyun ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1247*4882a593Smuzhiyun &tag[4],
1248*4882a593Smuzhiyun tag_len - 4,
1249*4882a593Smuzhiyun secattr);
1250*4882a593Smuzhiyun if (ret_val != 0) {
1251*4882a593Smuzhiyun netlbl_catmap_free(secattr->attr.mls.cat);
1252*4882a593Smuzhiyun return ret_val;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun if (secattr->attr.mls.cat)
1256*4882a593Smuzhiyun secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun return 0;
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun
1262*4882a593Smuzhiyun /**
1263*4882a593Smuzhiyun * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1264*4882a593Smuzhiyun * @doi_def: the DOI definition
1265*4882a593Smuzhiyun * @secattr: the security attributes
1266*4882a593Smuzhiyun * @buffer: the option buffer
1267*4882a593Smuzhiyun * @buffer_len: length of buffer in bytes
1268*4882a593Smuzhiyun *
1269*4882a593Smuzhiyun * Description:
1270*4882a593Smuzhiyun * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1271*4882a593Smuzhiyun * size of the tag on success, negative values on failure.
1272*4882a593Smuzhiyun *
1273*4882a593Smuzhiyun */
cipso_v4_gentag_enum(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * buffer,u32 buffer_len)1274*4882a593Smuzhiyun static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1275*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr,
1276*4882a593Smuzhiyun unsigned char *buffer,
1277*4882a593Smuzhiyun u32 buffer_len)
1278*4882a593Smuzhiyun {
1279*4882a593Smuzhiyun int ret_val;
1280*4882a593Smuzhiyun u32 tag_len;
1281*4882a593Smuzhiyun u32 level;
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1284*4882a593Smuzhiyun return -EPERM;
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun ret_val = cipso_v4_map_lvl_hton(doi_def,
1287*4882a593Smuzhiyun secattr->attr.mls.lvl,
1288*4882a593Smuzhiyun &level);
1289*4882a593Smuzhiyun if (ret_val != 0)
1290*4882a593Smuzhiyun return ret_val;
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1293*4882a593Smuzhiyun ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1294*4882a593Smuzhiyun secattr,
1295*4882a593Smuzhiyun &buffer[4],
1296*4882a593Smuzhiyun buffer_len - 4);
1297*4882a593Smuzhiyun if (ret_val < 0)
1298*4882a593Smuzhiyun return ret_val;
1299*4882a593Smuzhiyun
1300*4882a593Smuzhiyun tag_len = 4 + ret_val;
1301*4882a593Smuzhiyun } else
1302*4882a593Smuzhiyun tag_len = 4;
1303*4882a593Smuzhiyun
1304*4882a593Smuzhiyun buffer[0] = CIPSO_V4_TAG_ENUM;
1305*4882a593Smuzhiyun buffer[1] = tag_len;
1306*4882a593Smuzhiyun buffer[3] = level;
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun return tag_len;
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun /**
1312*4882a593Smuzhiyun * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1313*4882a593Smuzhiyun * @doi_def: the DOI definition
1314*4882a593Smuzhiyun * @tag: the CIPSO tag
1315*4882a593Smuzhiyun * @secattr: the security attributes
1316*4882a593Smuzhiyun *
1317*4882a593Smuzhiyun * Description:
1318*4882a593Smuzhiyun * Parse a CIPSO enumerated tag (tag type #2) and return the security
1319*4882a593Smuzhiyun * attributes in @secattr. Return zero on success, negatives values on
1320*4882a593Smuzhiyun * failure.
1321*4882a593Smuzhiyun *
1322*4882a593Smuzhiyun */
cipso_v4_parsetag_enum(const struct cipso_v4_doi * doi_def,const unsigned char * tag,struct netlbl_lsm_secattr * secattr)1323*4882a593Smuzhiyun static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1324*4882a593Smuzhiyun const unsigned char *tag,
1325*4882a593Smuzhiyun struct netlbl_lsm_secattr *secattr)
1326*4882a593Smuzhiyun {
1327*4882a593Smuzhiyun int ret_val;
1328*4882a593Smuzhiyun u8 tag_len = tag[1];
1329*4882a593Smuzhiyun u32 level;
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1332*4882a593Smuzhiyun if (ret_val != 0)
1333*4882a593Smuzhiyun return ret_val;
1334*4882a593Smuzhiyun secattr->attr.mls.lvl = level;
1335*4882a593Smuzhiyun secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1336*4882a593Smuzhiyun
1337*4882a593Smuzhiyun if (tag_len > 4) {
1338*4882a593Smuzhiyun ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1339*4882a593Smuzhiyun &tag[4],
1340*4882a593Smuzhiyun tag_len - 4,
1341*4882a593Smuzhiyun secattr);
1342*4882a593Smuzhiyun if (ret_val != 0) {
1343*4882a593Smuzhiyun netlbl_catmap_free(secattr->attr.mls.cat);
1344*4882a593Smuzhiyun return ret_val;
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun
1347*4882a593Smuzhiyun secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1348*4882a593Smuzhiyun }
1349*4882a593Smuzhiyun
1350*4882a593Smuzhiyun return 0;
1351*4882a593Smuzhiyun }
1352*4882a593Smuzhiyun
1353*4882a593Smuzhiyun /**
1354*4882a593Smuzhiyun * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1355*4882a593Smuzhiyun * @doi_def: the DOI definition
1356*4882a593Smuzhiyun * @secattr: the security attributes
1357*4882a593Smuzhiyun * @buffer: the option buffer
1358*4882a593Smuzhiyun * @buffer_len: length of buffer in bytes
1359*4882a593Smuzhiyun *
1360*4882a593Smuzhiyun * Description:
1361*4882a593Smuzhiyun * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1362*4882a593Smuzhiyun * size of the tag on success, negative values on failure.
1363*4882a593Smuzhiyun *
1364*4882a593Smuzhiyun */
cipso_v4_gentag_rng(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * buffer,u32 buffer_len)1365*4882a593Smuzhiyun static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1366*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr,
1367*4882a593Smuzhiyun unsigned char *buffer,
1368*4882a593Smuzhiyun u32 buffer_len)
1369*4882a593Smuzhiyun {
1370*4882a593Smuzhiyun int ret_val;
1371*4882a593Smuzhiyun u32 tag_len;
1372*4882a593Smuzhiyun u32 level;
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1375*4882a593Smuzhiyun return -EPERM;
1376*4882a593Smuzhiyun
1377*4882a593Smuzhiyun ret_val = cipso_v4_map_lvl_hton(doi_def,
1378*4882a593Smuzhiyun secattr->attr.mls.lvl,
1379*4882a593Smuzhiyun &level);
1380*4882a593Smuzhiyun if (ret_val != 0)
1381*4882a593Smuzhiyun return ret_val;
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1384*4882a593Smuzhiyun ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1385*4882a593Smuzhiyun secattr,
1386*4882a593Smuzhiyun &buffer[4],
1387*4882a593Smuzhiyun buffer_len - 4);
1388*4882a593Smuzhiyun if (ret_val < 0)
1389*4882a593Smuzhiyun return ret_val;
1390*4882a593Smuzhiyun
1391*4882a593Smuzhiyun tag_len = 4 + ret_val;
1392*4882a593Smuzhiyun } else
1393*4882a593Smuzhiyun tag_len = 4;
1394*4882a593Smuzhiyun
1395*4882a593Smuzhiyun buffer[0] = CIPSO_V4_TAG_RANGE;
1396*4882a593Smuzhiyun buffer[1] = tag_len;
1397*4882a593Smuzhiyun buffer[3] = level;
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun return tag_len;
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun /**
1403*4882a593Smuzhiyun * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1404*4882a593Smuzhiyun * @doi_def: the DOI definition
1405*4882a593Smuzhiyun * @tag: the CIPSO tag
1406*4882a593Smuzhiyun * @secattr: the security attributes
1407*4882a593Smuzhiyun *
1408*4882a593Smuzhiyun * Description:
1409*4882a593Smuzhiyun * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1410*4882a593Smuzhiyun * in @secattr. Return zero on success, negatives values on failure.
1411*4882a593Smuzhiyun *
1412*4882a593Smuzhiyun */
cipso_v4_parsetag_rng(const struct cipso_v4_doi * doi_def,const unsigned char * tag,struct netlbl_lsm_secattr * secattr)1413*4882a593Smuzhiyun static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1414*4882a593Smuzhiyun const unsigned char *tag,
1415*4882a593Smuzhiyun struct netlbl_lsm_secattr *secattr)
1416*4882a593Smuzhiyun {
1417*4882a593Smuzhiyun int ret_val;
1418*4882a593Smuzhiyun u8 tag_len = tag[1];
1419*4882a593Smuzhiyun u32 level;
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1422*4882a593Smuzhiyun if (ret_val != 0)
1423*4882a593Smuzhiyun return ret_val;
1424*4882a593Smuzhiyun secattr->attr.mls.lvl = level;
1425*4882a593Smuzhiyun secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun if (tag_len > 4) {
1428*4882a593Smuzhiyun ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1429*4882a593Smuzhiyun &tag[4],
1430*4882a593Smuzhiyun tag_len - 4,
1431*4882a593Smuzhiyun secattr);
1432*4882a593Smuzhiyun if (ret_val != 0) {
1433*4882a593Smuzhiyun netlbl_catmap_free(secattr->attr.mls.cat);
1434*4882a593Smuzhiyun return ret_val;
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun
1437*4882a593Smuzhiyun if (secattr->attr.mls.cat)
1438*4882a593Smuzhiyun secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun
1441*4882a593Smuzhiyun return 0;
1442*4882a593Smuzhiyun }
1443*4882a593Smuzhiyun
1444*4882a593Smuzhiyun /**
1445*4882a593Smuzhiyun * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
1446*4882a593Smuzhiyun * @doi_def: the DOI definition
1447*4882a593Smuzhiyun * @secattr: the security attributes
1448*4882a593Smuzhiyun * @buffer: the option buffer
1449*4882a593Smuzhiyun * @buffer_len: length of buffer in bytes
1450*4882a593Smuzhiyun *
1451*4882a593Smuzhiyun * Description:
1452*4882a593Smuzhiyun * Generate a CIPSO option using the local tag. Returns the size of the tag
1453*4882a593Smuzhiyun * on success, negative values on failure.
1454*4882a593Smuzhiyun *
1455*4882a593Smuzhiyun */
cipso_v4_gentag_loc(const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr,unsigned char * buffer,u32 buffer_len)1456*4882a593Smuzhiyun static int cipso_v4_gentag_loc(const struct cipso_v4_doi *doi_def,
1457*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr,
1458*4882a593Smuzhiyun unsigned char *buffer,
1459*4882a593Smuzhiyun u32 buffer_len)
1460*4882a593Smuzhiyun {
1461*4882a593Smuzhiyun if (!(secattr->flags & NETLBL_SECATTR_SECID))
1462*4882a593Smuzhiyun return -EPERM;
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun buffer[0] = CIPSO_V4_TAG_LOCAL;
1465*4882a593Smuzhiyun buffer[1] = CIPSO_V4_TAG_LOC_BLEN;
1466*4882a593Smuzhiyun *(u32 *)&buffer[2] = secattr->attr.secid;
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun return CIPSO_V4_TAG_LOC_BLEN;
1469*4882a593Smuzhiyun }
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun /**
1472*4882a593Smuzhiyun * cipso_v4_parsetag_loc - Parse a CIPSO local tag
1473*4882a593Smuzhiyun * @doi_def: the DOI definition
1474*4882a593Smuzhiyun * @tag: the CIPSO tag
1475*4882a593Smuzhiyun * @secattr: the security attributes
1476*4882a593Smuzhiyun *
1477*4882a593Smuzhiyun * Description:
1478*4882a593Smuzhiyun * Parse a CIPSO local tag and return the security attributes in @secattr.
1479*4882a593Smuzhiyun * Return zero on success, negatives values on failure.
1480*4882a593Smuzhiyun *
1481*4882a593Smuzhiyun */
cipso_v4_parsetag_loc(const struct cipso_v4_doi * doi_def,const unsigned char * tag,struct netlbl_lsm_secattr * secattr)1482*4882a593Smuzhiyun static int cipso_v4_parsetag_loc(const struct cipso_v4_doi *doi_def,
1483*4882a593Smuzhiyun const unsigned char *tag,
1484*4882a593Smuzhiyun struct netlbl_lsm_secattr *secattr)
1485*4882a593Smuzhiyun {
1486*4882a593Smuzhiyun secattr->attr.secid = *(u32 *)&tag[2];
1487*4882a593Smuzhiyun secattr->flags |= NETLBL_SECATTR_SECID;
1488*4882a593Smuzhiyun
1489*4882a593Smuzhiyun return 0;
1490*4882a593Smuzhiyun }
1491*4882a593Smuzhiyun
1492*4882a593Smuzhiyun /**
1493*4882a593Smuzhiyun * cipso_v4_optptr - Find the CIPSO option in the packet
1494*4882a593Smuzhiyun * @skb: the packet
1495*4882a593Smuzhiyun *
1496*4882a593Smuzhiyun * Description:
1497*4882a593Smuzhiyun * Parse the packet's IP header looking for a CIPSO option. Returns a pointer
1498*4882a593Smuzhiyun * to the start of the CIPSO option on success, NULL if one is not found.
1499*4882a593Smuzhiyun *
1500*4882a593Smuzhiyun */
cipso_v4_optptr(const struct sk_buff * skb)1501*4882a593Smuzhiyun unsigned char *cipso_v4_optptr(const struct sk_buff *skb)
1502*4882a593Smuzhiyun {
1503*4882a593Smuzhiyun const struct iphdr *iph = ip_hdr(skb);
1504*4882a593Smuzhiyun unsigned char *optptr = (unsigned char *)&(ip_hdr(skb)[1]);
1505*4882a593Smuzhiyun int optlen;
1506*4882a593Smuzhiyun int taglen;
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun for (optlen = iph->ihl*4 - sizeof(struct iphdr); optlen > 1; ) {
1509*4882a593Smuzhiyun switch (optptr[0]) {
1510*4882a593Smuzhiyun case IPOPT_END:
1511*4882a593Smuzhiyun return NULL;
1512*4882a593Smuzhiyun case IPOPT_NOOP:
1513*4882a593Smuzhiyun taglen = 1;
1514*4882a593Smuzhiyun break;
1515*4882a593Smuzhiyun default:
1516*4882a593Smuzhiyun taglen = optptr[1];
1517*4882a593Smuzhiyun }
1518*4882a593Smuzhiyun if (!taglen || taglen > optlen)
1519*4882a593Smuzhiyun return NULL;
1520*4882a593Smuzhiyun if (optptr[0] == IPOPT_CIPSO)
1521*4882a593Smuzhiyun return optptr;
1522*4882a593Smuzhiyun
1523*4882a593Smuzhiyun optlen -= taglen;
1524*4882a593Smuzhiyun optptr += taglen;
1525*4882a593Smuzhiyun }
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun return NULL;
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun /**
1531*4882a593Smuzhiyun * cipso_v4_validate - Validate a CIPSO option
1532*4882a593Smuzhiyun * @skb: the packet
1533*4882a593Smuzhiyun * @option: the start of the option, on error it is set to point to the error
1534*4882a593Smuzhiyun *
1535*4882a593Smuzhiyun * Description:
1536*4882a593Smuzhiyun * This routine is called to validate a CIPSO option, it checks all of the
1537*4882a593Smuzhiyun * fields to ensure that they are at least valid, see the draft snippet below
1538*4882a593Smuzhiyun * for details. If the option is valid then a zero value is returned and
1539*4882a593Smuzhiyun * the value of @option is unchanged. If the option is invalid then a
1540*4882a593Smuzhiyun * non-zero value is returned and @option is adjusted to point to the
1541*4882a593Smuzhiyun * offending portion of the option. From the IETF draft ...
1542*4882a593Smuzhiyun *
1543*4882a593Smuzhiyun * "If any field within the CIPSO options, such as the DOI identifier, is not
1544*4882a593Smuzhiyun * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1545*4882a593Smuzhiyun * (type 12) is generated and returned. The ICMP code field is set to 'bad
1546*4882a593Smuzhiyun * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1547*4882a593Smuzhiyun * that is unrecognized."
1548*4882a593Smuzhiyun *
1549*4882a593Smuzhiyun */
cipso_v4_validate(const struct sk_buff * skb,unsigned char ** option)1550*4882a593Smuzhiyun int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
1551*4882a593Smuzhiyun {
1552*4882a593Smuzhiyun unsigned char *opt = *option;
1553*4882a593Smuzhiyun unsigned char *tag;
1554*4882a593Smuzhiyun unsigned char opt_iter;
1555*4882a593Smuzhiyun unsigned char err_offset = 0;
1556*4882a593Smuzhiyun u8 opt_len;
1557*4882a593Smuzhiyun u8 tag_len;
1558*4882a593Smuzhiyun struct cipso_v4_doi *doi_def = NULL;
1559*4882a593Smuzhiyun u32 tag_iter;
1560*4882a593Smuzhiyun
1561*4882a593Smuzhiyun /* caller already checks for length values that are too large */
1562*4882a593Smuzhiyun opt_len = opt[1];
1563*4882a593Smuzhiyun if (opt_len < 8) {
1564*4882a593Smuzhiyun err_offset = 1;
1565*4882a593Smuzhiyun goto validate_return;
1566*4882a593Smuzhiyun }
1567*4882a593Smuzhiyun
1568*4882a593Smuzhiyun rcu_read_lock();
1569*4882a593Smuzhiyun doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
1570*4882a593Smuzhiyun if (!doi_def) {
1571*4882a593Smuzhiyun err_offset = 2;
1572*4882a593Smuzhiyun goto validate_return_locked;
1573*4882a593Smuzhiyun }
1574*4882a593Smuzhiyun
1575*4882a593Smuzhiyun opt_iter = CIPSO_V4_HDR_LEN;
1576*4882a593Smuzhiyun tag = opt + opt_iter;
1577*4882a593Smuzhiyun while (opt_iter < opt_len) {
1578*4882a593Smuzhiyun for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1579*4882a593Smuzhiyun if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1580*4882a593Smuzhiyun ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1581*4882a593Smuzhiyun err_offset = opt_iter;
1582*4882a593Smuzhiyun goto validate_return_locked;
1583*4882a593Smuzhiyun }
1584*4882a593Smuzhiyun
1585*4882a593Smuzhiyun if (opt_iter + 1 == opt_len) {
1586*4882a593Smuzhiyun err_offset = opt_iter;
1587*4882a593Smuzhiyun goto validate_return_locked;
1588*4882a593Smuzhiyun }
1589*4882a593Smuzhiyun tag_len = tag[1];
1590*4882a593Smuzhiyun if (tag_len > (opt_len - opt_iter)) {
1591*4882a593Smuzhiyun err_offset = opt_iter + 1;
1592*4882a593Smuzhiyun goto validate_return_locked;
1593*4882a593Smuzhiyun }
1594*4882a593Smuzhiyun
1595*4882a593Smuzhiyun switch (tag[0]) {
1596*4882a593Smuzhiyun case CIPSO_V4_TAG_RBITMAP:
1597*4882a593Smuzhiyun if (tag_len < CIPSO_V4_TAG_RBM_BLEN) {
1598*4882a593Smuzhiyun err_offset = opt_iter + 1;
1599*4882a593Smuzhiyun goto validate_return_locked;
1600*4882a593Smuzhiyun }
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun /* We are already going to do all the verification
1603*4882a593Smuzhiyun * necessary at the socket layer so from our point of
1604*4882a593Smuzhiyun * view it is safe to turn these checks off (and less
1605*4882a593Smuzhiyun * work), however, the CIPSO draft says we should do
1606*4882a593Smuzhiyun * all the CIPSO validations here but it doesn't
1607*4882a593Smuzhiyun * really specify _exactly_ what we need to validate
1608*4882a593Smuzhiyun * ... so, just make it a sysctl tunable. */
1609*4882a593Smuzhiyun if (READ_ONCE(cipso_v4_rbm_strictvalid)) {
1610*4882a593Smuzhiyun if (cipso_v4_map_lvl_valid(doi_def,
1611*4882a593Smuzhiyun tag[3]) < 0) {
1612*4882a593Smuzhiyun err_offset = opt_iter + 3;
1613*4882a593Smuzhiyun goto validate_return_locked;
1614*4882a593Smuzhiyun }
1615*4882a593Smuzhiyun if (tag_len > CIPSO_V4_TAG_RBM_BLEN &&
1616*4882a593Smuzhiyun cipso_v4_map_cat_rbm_valid(doi_def,
1617*4882a593Smuzhiyun &tag[4],
1618*4882a593Smuzhiyun tag_len - 4) < 0) {
1619*4882a593Smuzhiyun err_offset = opt_iter + 4;
1620*4882a593Smuzhiyun goto validate_return_locked;
1621*4882a593Smuzhiyun }
1622*4882a593Smuzhiyun }
1623*4882a593Smuzhiyun break;
1624*4882a593Smuzhiyun case CIPSO_V4_TAG_ENUM:
1625*4882a593Smuzhiyun if (tag_len < CIPSO_V4_TAG_ENUM_BLEN) {
1626*4882a593Smuzhiyun err_offset = opt_iter + 1;
1627*4882a593Smuzhiyun goto validate_return_locked;
1628*4882a593Smuzhiyun }
1629*4882a593Smuzhiyun
1630*4882a593Smuzhiyun if (cipso_v4_map_lvl_valid(doi_def,
1631*4882a593Smuzhiyun tag[3]) < 0) {
1632*4882a593Smuzhiyun err_offset = opt_iter + 3;
1633*4882a593Smuzhiyun goto validate_return_locked;
1634*4882a593Smuzhiyun }
1635*4882a593Smuzhiyun if (tag_len > CIPSO_V4_TAG_ENUM_BLEN &&
1636*4882a593Smuzhiyun cipso_v4_map_cat_enum_valid(doi_def,
1637*4882a593Smuzhiyun &tag[4],
1638*4882a593Smuzhiyun tag_len - 4) < 0) {
1639*4882a593Smuzhiyun err_offset = opt_iter + 4;
1640*4882a593Smuzhiyun goto validate_return_locked;
1641*4882a593Smuzhiyun }
1642*4882a593Smuzhiyun break;
1643*4882a593Smuzhiyun case CIPSO_V4_TAG_RANGE:
1644*4882a593Smuzhiyun if (tag_len < CIPSO_V4_TAG_RNG_BLEN) {
1645*4882a593Smuzhiyun err_offset = opt_iter + 1;
1646*4882a593Smuzhiyun goto validate_return_locked;
1647*4882a593Smuzhiyun }
1648*4882a593Smuzhiyun
1649*4882a593Smuzhiyun if (cipso_v4_map_lvl_valid(doi_def,
1650*4882a593Smuzhiyun tag[3]) < 0) {
1651*4882a593Smuzhiyun err_offset = opt_iter + 3;
1652*4882a593Smuzhiyun goto validate_return_locked;
1653*4882a593Smuzhiyun }
1654*4882a593Smuzhiyun if (tag_len > CIPSO_V4_TAG_RNG_BLEN &&
1655*4882a593Smuzhiyun cipso_v4_map_cat_rng_valid(doi_def,
1656*4882a593Smuzhiyun &tag[4],
1657*4882a593Smuzhiyun tag_len - 4) < 0) {
1658*4882a593Smuzhiyun err_offset = opt_iter + 4;
1659*4882a593Smuzhiyun goto validate_return_locked;
1660*4882a593Smuzhiyun }
1661*4882a593Smuzhiyun break;
1662*4882a593Smuzhiyun case CIPSO_V4_TAG_LOCAL:
1663*4882a593Smuzhiyun /* This is a non-standard tag that we only allow for
1664*4882a593Smuzhiyun * local connections, so if the incoming interface is
1665*4882a593Smuzhiyun * not the loopback device drop the packet. Further,
1666*4882a593Smuzhiyun * there is no legitimate reason for setting this from
1667*4882a593Smuzhiyun * userspace so reject it if skb is NULL. */
1668*4882a593Smuzhiyun if (!skb || !(skb->dev->flags & IFF_LOOPBACK)) {
1669*4882a593Smuzhiyun err_offset = opt_iter;
1670*4882a593Smuzhiyun goto validate_return_locked;
1671*4882a593Smuzhiyun }
1672*4882a593Smuzhiyun if (tag_len != CIPSO_V4_TAG_LOC_BLEN) {
1673*4882a593Smuzhiyun err_offset = opt_iter + 1;
1674*4882a593Smuzhiyun goto validate_return_locked;
1675*4882a593Smuzhiyun }
1676*4882a593Smuzhiyun break;
1677*4882a593Smuzhiyun default:
1678*4882a593Smuzhiyun err_offset = opt_iter;
1679*4882a593Smuzhiyun goto validate_return_locked;
1680*4882a593Smuzhiyun }
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun tag += tag_len;
1683*4882a593Smuzhiyun opt_iter += tag_len;
1684*4882a593Smuzhiyun }
1685*4882a593Smuzhiyun
1686*4882a593Smuzhiyun validate_return_locked:
1687*4882a593Smuzhiyun rcu_read_unlock();
1688*4882a593Smuzhiyun validate_return:
1689*4882a593Smuzhiyun *option = opt + err_offset;
1690*4882a593Smuzhiyun return err_offset;
1691*4882a593Smuzhiyun }
1692*4882a593Smuzhiyun
1693*4882a593Smuzhiyun /**
1694*4882a593Smuzhiyun * cipso_v4_error - Send the correct response for a bad packet
1695*4882a593Smuzhiyun * @skb: the packet
1696*4882a593Smuzhiyun * @error: the error code
1697*4882a593Smuzhiyun * @gateway: CIPSO gateway flag
1698*4882a593Smuzhiyun *
1699*4882a593Smuzhiyun * Description:
1700*4882a593Smuzhiyun * Based on the error code given in @error, send an ICMP error message back to
1701*4882a593Smuzhiyun * the originating host. From the IETF draft ...
1702*4882a593Smuzhiyun *
1703*4882a593Smuzhiyun * "If the contents of the CIPSO [option] are valid but the security label is
1704*4882a593Smuzhiyun * outside of the configured host or port label range, the datagram is
1705*4882a593Smuzhiyun * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1706*4882a593Smuzhiyun * returned. The code field of the ICMP is set to 'communication with
1707*4882a593Smuzhiyun * destination network administratively prohibited' (code 9) or to
1708*4882a593Smuzhiyun * 'communication with destination host administratively prohibited'
1709*4882a593Smuzhiyun * (code 10). The value of the code is dependent on whether the originator
1710*4882a593Smuzhiyun * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1711*4882a593Smuzhiyun * recipient of the ICMP message MUST be able to handle either value. The
1712*4882a593Smuzhiyun * same procedure is performed if a CIPSO [option] can not be added to an
1713*4882a593Smuzhiyun * IP packet because it is too large to fit in the IP options area."
1714*4882a593Smuzhiyun *
1715*4882a593Smuzhiyun * "If the error is triggered by receipt of an ICMP message, the message is
1716*4882a593Smuzhiyun * discarded and no response is permitted (consistent with general ICMP
1717*4882a593Smuzhiyun * processing rules)."
1718*4882a593Smuzhiyun *
1719*4882a593Smuzhiyun */
cipso_v4_error(struct sk_buff * skb,int error,u32 gateway)1720*4882a593Smuzhiyun void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1721*4882a593Smuzhiyun {
1722*4882a593Smuzhiyun unsigned char optbuf[sizeof(struct ip_options) + 40];
1723*4882a593Smuzhiyun struct ip_options *opt = (struct ip_options *)optbuf;
1724*4882a593Smuzhiyun int res;
1725*4882a593Smuzhiyun
1726*4882a593Smuzhiyun if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
1727*4882a593Smuzhiyun return;
1728*4882a593Smuzhiyun
1729*4882a593Smuzhiyun /*
1730*4882a593Smuzhiyun * We might be called above the IP layer,
1731*4882a593Smuzhiyun * so we can not use icmp_send and IPCB here.
1732*4882a593Smuzhiyun */
1733*4882a593Smuzhiyun
1734*4882a593Smuzhiyun memset(opt, 0, sizeof(struct ip_options));
1735*4882a593Smuzhiyun opt->optlen = ip_hdr(skb)->ihl*4 - sizeof(struct iphdr);
1736*4882a593Smuzhiyun rcu_read_lock();
1737*4882a593Smuzhiyun res = __ip_options_compile(dev_net(skb->dev), opt, skb, NULL);
1738*4882a593Smuzhiyun rcu_read_unlock();
1739*4882a593Smuzhiyun
1740*4882a593Smuzhiyun if (res)
1741*4882a593Smuzhiyun return;
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun if (gateway)
1744*4882a593Smuzhiyun __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0, opt);
1745*4882a593Smuzhiyun else
1746*4882a593Smuzhiyun __icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0, opt);
1747*4882a593Smuzhiyun }
1748*4882a593Smuzhiyun
1749*4882a593Smuzhiyun /**
1750*4882a593Smuzhiyun * cipso_v4_genopt - Generate a CIPSO option
1751*4882a593Smuzhiyun * @buf: the option buffer
1752*4882a593Smuzhiyun * @buf_len: the size of opt_buf
1753*4882a593Smuzhiyun * @doi_def: the CIPSO DOI to use
1754*4882a593Smuzhiyun * @secattr: the security attributes
1755*4882a593Smuzhiyun *
1756*4882a593Smuzhiyun * Description:
1757*4882a593Smuzhiyun * Generate a CIPSO option using the DOI definition and security attributes
1758*4882a593Smuzhiyun * passed to the function. Returns the length of the option on success and
1759*4882a593Smuzhiyun * negative values on failure.
1760*4882a593Smuzhiyun *
1761*4882a593Smuzhiyun */
cipso_v4_genopt(unsigned char * buf,u32 buf_len,const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr)1762*4882a593Smuzhiyun static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
1763*4882a593Smuzhiyun const struct cipso_v4_doi *doi_def,
1764*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr)
1765*4882a593Smuzhiyun {
1766*4882a593Smuzhiyun int ret_val;
1767*4882a593Smuzhiyun u32 iter;
1768*4882a593Smuzhiyun
1769*4882a593Smuzhiyun if (buf_len <= CIPSO_V4_HDR_LEN)
1770*4882a593Smuzhiyun return -ENOSPC;
1771*4882a593Smuzhiyun
1772*4882a593Smuzhiyun /* XXX - This code assumes only one tag per CIPSO option which isn't
1773*4882a593Smuzhiyun * really a good assumption to make but since we only support the MAC
1774*4882a593Smuzhiyun * tags right now it is a safe assumption. */
1775*4882a593Smuzhiyun iter = 0;
1776*4882a593Smuzhiyun do {
1777*4882a593Smuzhiyun memset(buf, 0, buf_len);
1778*4882a593Smuzhiyun switch (doi_def->tags[iter]) {
1779*4882a593Smuzhiyun case CIPSO_V4_TAG_RBITMAP:
1780*4882a593Smuzhiyun ret_val = cipso_v4_gentag_rbm(doi_def,
1781*4882a593Smuzhiyun secattr,
1782*4882a593Smuzhiyun &buf[CIPSO_V4_HDR_LEN],
1783*4882a593Smuzhiyun buf_len - CIPSO_V4_HDR_LEN);
1784*4882a593Smuzhiyun break;
1785*4882a593Smuzhiyun case CIPSO_V4_TAG_ENUM:
1786*4882a593Smuzhiyun ret_val = cipso_v4_gentag_enum(doi_def,
1787*4882a593Smuzhiyun secattr,
1788*4882a593Smuzhiyun &buf[CIPSO_V4_HDR_LEN],
1789*4882a593Smuzhiyun buf_len - CIPSO_V4_HDR_LEN);
1790*4882a593Smuzhiyun break;
1791*4882a593Smuzhiyun case CIPSO_V4_TAG_RANGE:
1792*4882a593Smuzhiyun ret_val = cipso_v4_gentag_rng(doi_def,
1793*4882a593Smuzhiyun secattr,
1794*4882a593Smuzhiyun &buf[CIPSO_V4_HDR_LEN],
1795*4882a593Smuzhiyun buf_len - CIPSO_V4_HDR_LEN);
1796*4882a593Smuzhiyun break;
1797*4882a593Smuzhiyun case CIPSO_V4_TAG_LOCAL:
1798*4882a593Smuzhiyun ret_val = cipso_v4_gentag_loc(doi_def,
1799*4882a593Smuzhiyun secattr,
1800*4882a593Smuzhiyun &buf[CIPSO_V4_HDR_LEN],
1801*4882a593Smuzhiyun buf_len - CIPSO_V4_HDR_LEN);
1802*4882a593Smuzhiyun break;
1803*4882a593Smuzhiyun default:
1804*4882a593Smuzhiyun return -EPERM;
1805*4882a593Smuzhiyun }
1806*4882a593Smuzhiyun
1807*4882a593Smuzhiyun iter++;
1808*4882a593Smuzhiyun } while (ret_val < 0 &&
1809*4882a593Smuzhiyun iter < CIPSO_V4_TAG_MAXCNT &&
1810*4882a593Smuzhiyun doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
1811*4882a593Smuzhiyun if (ret_val < 0)
1812*4882a593Smuzhiyun return ret_val;
1813*4882a593Smuzhiyun cipso_v4_gentag_hdr(doi_def, buf, ret_val);
1814*4882a593Smuzhiyun return CIPSO_V4_HDR_LEN + ret_val;
1815*4882a593Smuzhiyun }
1816*4882a593Smuzhiyun
1817*4882a593Smuzhiyun /**
1818*4882a593Smuzhiyun * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1819*4882a593Smuzhiyun * @sk: the socket
1820*4882a593Smuzhiyun * @doi_def: the CIPSO DOI to use
1821*4882a593Smuzhiyun * @secattr: the specific security attributes of the socket
1822*4882a593Smuzhiyun *
1823*4882a593Smuzhiyun * Description:
1824*4882a593Smuzhiyun * Set the CIPSO option on the given socket using the DOI definition and
1825*4882a593Smuzhiyun * security attributes passed to the function. This function requires
1826*4882a593Smuzhiyun * exclusive access to @sk, which means it either needs to be in the
1827*4882a593Smuzhiyun * process of being created or locked. Returns zero on success and negative
1828*4882a593Smuzhiyun * values on failure.
1829*4882a593Smuzhiyun *
1830*4882a593Smuzhiyun */
cipso_v4_sock_setattr(struct sock * sk,const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr)1831*4882a593Smuzhiyun int cipso_v4_sock_setattr(struct sock *sk,
1832*4882a593Smuzhiyun const struct cipso_v4_doi *doi_def,
1833*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr)
1834*4882a593Smuzhiyun {
1835*4882a593Smuzhiyun int ret_val = -EPERM;
1836*4882a593Smuzhiyun unsigned char *buf = NULL;
1837*4882a593Smuzhiyun u32 buf_len;
1838*4882a593Smuzhiyun u32 opt_len;
1839*4882a593Smuzhiyun struct ip_options_rcu *old, *opt = NULL;
1840*4882a593Smuzhiyun struct inet_sock *sk_inet;
1841*4882a593Smuzhiyun struct inet_connection_sock *sk_conn;
1842*4882a593Smuzhiyun
1843*4882a593Smuzhiyun /* In the case of sock_create_lite(), the sock->sk field is not
1844*4882a593Smuzhiyun * defined yet but it is not a problem as the only users of these
1845*4882a593Smuzhiyun * "lite" PF_INET sockets are functions which do an accept() call
1846*4882a593Smuzhiyun * afterwards so we will label the socket as part of the accept(). */
1847*4882a593Smuzhiyun if (!sk)
1848*4882a593Smuzhiyun return 0;
1849*4882a593Smuzhiyun
1850*4882a593Smuzhiyun /* We allocate the maximum CIPSO option size here so we are probably
1851*4882a593Smuzhiyun * being a little wasteful, but it makes our life _much_ easier later
1852*4882a593Smuzhiyun * on and after all we are only talking about 40 bytes. */
1853*4882a593Smuzhiyun buf_len = CIPSO_V4_OPT_LEN_MAX;
1854*4882a593Smuzhiyun buf = kmalloc(buf_len, GFP_ATOMIC);
1855*4882a593Smuzhiyun if (!buf) {
1856*4882a593Smuzhiyun ret_val = -ENOMEM;
1857*4882a593Smuzhiyun goto socket_setattr_failure;
1858*4882a593Smuzhiyun }
1859*4882a593Smuzhiyun
1860*4882a593Smuzhiyun ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1861*4882a593Smuzhiyun if (ret_val < 0)
1862*4882a593Smuzhiyun goto socket_setattr_failure;
1863*4882a593Smuzhiyun buf_len = ret_val;
1864*4882a593Smuzhiyun
1865*4882a593Smuzhiyun /* We can't use ip_options_get() directly because it makes a call to
1866*4882a593Smuzhiyun * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1867*4882a593Smuzhiyun * we won't always have CAP_NET_RAW even though we _always_ want to
1868*4882a593Smuzhiyun * set the IPOPT_CIPSO option. */
1869*4882a593Smuzhiyun opt_len = (buf_len + 3) & ~3;
1870*4882a593Smuzhiyun opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1871*4882a593Smuzhiyun if (!opt) {
1872*4882a593Smuzhiyun ret_val = -ENOMEM;
1873*4882a593Smuzhiyun goto socket_setattr_failure;
1874*4882a593Smuzhiyun }
1875*4882a593Smuzhiyun memcpy(opt->opt.__data, buf, buf_len);
1876*4882a593Smuzhiyun opt->opt.optlen = opt_len;
1877*4882a593Smuzhiyun opt->opt.cipso = sizeof(struct iphdr);
1878*4882a593Smuzhiyun kfree(buf);
1879*4882a593Smuzhiyun buf = NULL;
1880*4882a593Smuzhiyun
1881*4882a593Smuzhiyun sk_inet = inet_sk(sk);
1882*4882a593Smuzhiyun
1883*4882a593Smuzhiyun old = rcu_dereference_protected(sk_inet->inet_opt,
1884*4882a593Smuzhiyun lockdep_sock_is_held(sk));
1885*4882a593Smuzhiyun if (sk_inet->is_icsk) {
1886*4882a593Smuzhiyun sk_conn = inet_csk(sk);
1887*4882a593Smuzhiyun if (old)
1888*4882a593Smuzhiyun sk_conn->icsk_ext_hdr_len -= old->opt.optlen;
1889*4882a593Smuzhiyun sk_conn->icsk_ext_hdr_len += opt->opt.optlen;
1890*4882a593Smuzhiyun sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1891*4882a593Smuzhiyun }
1892*4882a593Smuzhiyun rcu_assign_pointer(sk_inet->inet_opt, opt);
1893*4882a593Smuzhiyun if (old)
1894*4882a593Smuzhiyun kfree_rcu(old, rcu);
1895*4882a593Smuzhiyun
1896*4882a593Smuzhiyun return 0;
1897*4882a593Smuzhiyun
1898*4882a593Smuzhiyun socket_setattr_failure:
1899*4882a593Smuzhiyun kfree(buf);
1900*4882a593Smuzhiyun kfree(opt);
1901*4882a593Smuzhiyun return ret_val;
1902*4882a593Smuzhiyun }
1903*4882a593Smuzhiyun
1904*4882a593Smuzhiyun /**
1905*4882a593Smuzhiyun * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket
1906*4882a593Smuzhiyun * @req: the connection request socket
1907*4882a593Smuzhiyun * @doi_def: the CIPSO DOI to use
1908*4882a593Smuzhiyun * @secattr: the specific security attributes of the socket
1909*4882a593Smuzhiyun *
1910*4882a593Smuzhiyun * Description:
1911*4882a593Smuzhiyun * Set the CIPSO option on the given socket using the DOI definition and
1912*4882a593Smuzhiyun * security attributes passed to the function. Returns zero on success and
1913*4882a593Smuzhiyun * negative values on failure.
1914*4882a593Smuzhiyun *
1915*4882a593Smuzhiyun */
cipso_v4_req_setattr(struct request_sock * req,const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr)1916*4882a593Smuzhiyun int cipso_v4_req_setattr(struct request_sock *req,
1917*4882a593Smuzhiyun const struct cipso_v4_doi *doi_def,
1918*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr)
1919*4882a593Smuzhiyun {
1920*4882a593Smuzhiyun int ret_val = -EPERM;
1921*4882a593Smuzhiyun unsigned char *buf = NULL;
1922*4882a593Smuzhiyun u32 buf_len;
1923*4882a593Smuzhiyun u32 opt_len;
1924*4882a593Smuzhiyun struct ip_options_rcu *opt = NULL;
1925*4882a593Smuzhiyun struct inet_request_sock *req_inet;
1926*4882a593Smuzhiyun
1927*4882a593Smuzhiyun /* We allocate the maximum CIPSO option size here so we are probably
1928*4882a593Smuzhiyun * being a little wasteful, but it makes our life _much_ easier later
1929*4882a593Smuzhiyun * on and after all we are only talking about 40 bytes. */
1930*4882a593Smuzhiyun buf_len = CIPSO_V4_OPT_LEN_MAX;
1931*4882a593Smuzhiyun buf = kmalloc(buf_len, GFP_ATOMIC);
1932*4882a593Smuzhiyun if (!buf) {
1933*4882a593Smuzhiyun ret_val = -ENOMEM;
1934*4882a593Smuzhiyun goto req_setattr_failure;
1935*4882a593Smuzhiyun }
1936*4882a593Smuzhiyun
1937*4882a593Smuzhiyun ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
1938*4882a593Smuzhiyun if (ret_val < 0)
1939*4882a593Smuzhiyun goto req_setattr_failure;
1940*4882a593Smuzhiyun buf_len = ret_val;
1941*4882a593Smuzhiyun
1942*4882a593Smuzhiyun /* We can't use ip_options_get() directly because it makes a call to
1943*4882a593Smuzhiyun * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1944*4882a593Smuzhiyun * we won't always have CAP_NET_RAW even though we _always_ want to
1945*4882a593Smuzhiyun * set the IPOPT_CIPSO option. */
1946*4882a593Smuzhiyun opt_len = (buf_len + 3) & ~3;
1947*4882a593Smuzhiyun opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1948*4882a593Smuzhiyun if (!opt) {
1949*4882a593Smuzhiyun ret_val = -ENOMEM;
1950*4882a593Smuzhiyun goto req_setattr_failure;
1951*4882a593Smuzhiyun }
1952*4882a593Smuzhiyun memcpy(opt->opt.__data, buf, buf_len);
1953*4882a593Smuzhiyun opt->opt.optlen = opt_len;
1954*4882a593Smuzhiyun opt->opt.cipso = sizeof(struct iphdr);
1955*4882a593Smuzhiyun kfree(buf);
1956*4882a593Smuzhiyun buf = NULL;
1957*4882a593Smuzhiyun
1958*4882a593Smuzhiyun req_inet = inet_rsk(req);
1959*4882a593Smuzhiyun opt = xchg((__force struct ip_options_rcu **)&req_inet->ireq_opt, opt);
1960*4882a593Smuzhiyun if (opt)
1961*4882a593Smuzhiyun kfree_rcu(opt, rcu);
1962*4882a593Smuzhiyun
1963*4882a593Smuzhiyun return 0;
1964*4882a593Smuzhiyun
1965*4882a593Smuzhiyun req_setattr_failure:
1966*4882a593Smuzhiyun kfree(buf);
1967*4882a593Smuzhiyun kfree(opt);
1968*4882a593Smuzhiyun return ret_val;
1969*4882a593Smuzhiyun }
1970*4882a593Smuzhiyun
1971*4882a593Smuzhiyun /**
1972*4882a593Smuzhiyun * cipso_v4_delopt - Delete the CIPSO option from a set of IP options
1973*4882a593Smuzhiyun * @opt_ptr: IP option pointer
1974*4882a593Smuzhiyun *
1975*4882a593Smuzhiyun * Description:
1976*4882a593Smuzhiyun * Deletes the CIPSO IP option from a set of IP options and makes the necessary
1977*4882a593Smuzhiyun * adjustments to the IP option structure. Returns zero on success, negative
1978*4882a593Smuzhiyun * values on failure.
1979*4882a593Smuzhiyun *
1980*4882a593Smuzhiyun */
cipso_v4_delopt(struct ip_options_rcu __rcu ** opt_ptr)1981*4882a593Smuzhiyun static int cipso_v4_delopt(struct ip_options_rcu __rcu **opt_ptr)
1982*4882a593Smuzhiyun {
1983*4882a593Smuzhiyun struct ip_options_rcu *opt = rcu_dereference_protected(*opt_ptr, 1);
1984*4882a593Smuzhiyun int hdr_delta = 0;
1985*4882a593Smuzhiyun
1986*4882a593Smuzhiyun if (!opt || opt->opt.cipso == 0)
1987*4882a593Smuzhiyun return 0;
1988*4882a593Smuzhiyun if (opt->opt.srr || opt->opt.rr || opt->opt.ts || opt->opt.router_alert) {
1989*4882a593Smuzhiyun u8 cipso_len;
1990*4882a593Smuzhiyun u8 cipso_off;
1991*4882a593Smuzhiyun unsigned char *cipso_ptr;
1992*4882a593Smuzhiyun int iter;
1993*4882a593Smuzhiyun int optlen_new;
1994*4882a593Smuzhiyun
1995*4882a593Smuzhiyun cipso_off = opt->opt.cipso - sizeof(struct iphdr);
1996*4882a593Smuzhiyun cipso_ptr = &opt->opt.__data[cipso_off];
1997*4882a593Smuzhiyun cipso_len = cipso_ptr[1];
1998*4882a593Smuzhiyun
1999*4882a593Smuzhiyun if (opt->opt.srr > opt->opt.cipso)
2000*4882a593Smuzhiyun opt->opt.srr -= cipso_len;
2001*4882a593Smuzhiyun if (opt->opt.rr > opt->opt.cipso)
2002*4882a593Smuzhiyun opt->opt.rr -= cipso_len;
2003*4882a593Smuzhiyun if (opt->opt.ts > opt->opt.cipso)
2004*4882a593Smuzhiyun opt->opt.ts -= cipso_len;
2005*4882a593Smuzhiyun if (opt->opt.router_alert > opt->opt.cipso)
2006*4882a593Smuzhiyun opt->opt.router_alert -= cipso_len;
2007*4882a593Smuzhiyun opt->opt.cipso = 0;
2008*4882a593Smuzhiyun
2009*4882a593Smuzhiyun memmove(cipso_ptr, cipso_ptr + cipso_len,
2010*4882a593Smuzhiyun opt->opt.optlen - cipso_off - cipso_len);
2011*4882a593Smuzhiyun
2012*4882a593Smuzhiyun /* determining the new total option length is tricky because of
2013*4882a593Smuzhiyun * the padding necessary, the only thing i can think to do at
2014*4882a593Smuzhiyun * this point is walk the options one-by-one, skipping the
2015*4882a593Smuzhiyun * padding at the end to determine the actual option size and
2016*4882a593Smuzhiyun * from there we can determine the new total option length */
2017*4882a593Smuzhiyun iter = 0;
2018*4882a593Smuzhiyun optlen_new = 0;
2019*4882a593Smuzhiyun while (iter < opt->opt.optlen)
2020*4882a593Smuzhiyun if (opt->opt.__data[iter] != IPOPT_NOP) {
2021*4882a593Smuzhiyun iter += opt->opt.__data[iter + 1];
2022*4882a593Smuzhiyun optlen_new = iter;
2023*4882a593Smuzhiyun } else
2024*4882a593Smuzhiyun iter++;
2025*4882a593Smuzhiyun hdr_delta = opt->opt.optlen;
2026*4882a593Smuzhiyun opt->opt.optlen = (optlen_new + 3) & ~3;
2027*4882a593Smuzhiyun hdr_delta -= opt->opt.optlen;
2028*4882a593Smuzhiyun } else {
2029*4882a593Smuzhiyun /* only the cipso option was present on the socket so we can
2030*4882a593Smuzhiyun * remove the entire option struct */
2031*4882a593Smuzhiyun *opt_ptr = NULL;
2032*4882a593Smuzhiyun hdr_delta = opt->opt.optlen;
2033*4882a593Smuzhiyun kfree_rcu(opt, rcu);
2034*4882a593Smuzhiyun }
2035*4882a593Smuzhiyun
2036*4882a593Smuzhiyun return hdr_delta;
2037*4882a593Smuzhiyun }
2038*4882a593Smuzhiyun
2039*4882a593Smuzhiyun /**
2040*4882a593Smuzhiyun * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
2041*4882a593Smuzhiyun * @sk: the socket
2042*4882a593Smuzhiyun *
2043*4882a593Smuzhiyun * Description:
2044*4882a593Smuzhiyun * Removes the CIPSO option from a socket, if present.
2045*4882a593Smuzhiyun *
2046*4882a593Smuzhiyun */
cipso_v4_sock_delattr(struct sock * sk)2047*4882a593Smuzhiyun void cipso_v4_sock_delattr(struct sock *sk)
2048*4882a593Smuzhiyun {
2049*4882a593Smuzhiyun struct inet_sock *sk_inet;
2050*4882a593Smuzhiyun int hdr_delta;
2051*4882a593Smuzhiyun
2052*4882a593Smuzhiyun sk_inet = inet_sk(sk);
2053*4882a593Smuzhiyun
2054*4882a593Smuzhiyun hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
2055*4882a593Smuzhiyun if (sk_inet->is_icsk && hdr_delta > 0) {
2056*4882a593Smuzhiyun struct inet_connection_sock *sk_conn = inet_csk(sk);
2057*4882a593Smuzhiyun sk_conn->icsk_ext_hdr_len -= hdr_delta;
2058*4882a593Smuzhiyun sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
2059*4882a593Smuzhiyun }
2060*4882a593Smuzhiyun }
2061*4882a593Smuzhiyun
2062*4882a593Smuzhiyun /**
2063*4882a593Smuzhiyun * cipso_v4_req_delattr - Delete the CIPSO option from a request socket
2064*4882a593Smuzhiyun * @req: the request socket
2065*4882a593Smuzhiyun *
2066*4882a593Smuzhiyun * Description:
2067*4882a593Smuzhiyun * Removes the CIPSO option from a request socket, if present.
2068*4882a593Smuzhiyun *
2069*4882a593Smuzhiyun */
cipso_v4_req_delattr(struct request_sock * req)2070*4882a593Smuzhiyun void cipso_v4_req_delattr(struct request_sock *req)
2071*4882a593Smuzhiyun {
2072*4882a593Smuzhiyun cipso_v4_delopt(&inet_rsk(req)->ireq_opt);
2073*4882a593Smuzhiyun }
2074*4882a593Smuzhiyun
2075*4882a593Smuzhiyun /**
2076*4882a593Smuzhiyun * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
2077*4882a593Smuzhiyun * @cipso: the CIPSO v4 option
2078*4882a593Smuzhiyun * @secattr: the security attributes
2079*4882a593Smuzhiyun *
2080*4882a593Smuzhiyun * Description:
2081*4882a593Smuzhiyun * Inspect @cipso and return the security attributes in @secattr. Returns zero
2082*4882a593Smuzhiyun * on success and negative values on failure.
2083*4882a593Smuzhiyun *
2084*4882a593Smuzhiyun */
cipso_v4_getattr(const unsigned char * cipso,struct netlbl_lsm_secattr * secattr)2085*4882a593Smuzhiyun int cipso_v4_getattr(const unsigned char *cipso,
2086*4882a593Smuzhiyun struct netlbl_lsm_secattr *secattr)
2087*4882a593Smuzhiyun {
2088*4882a593Smuzhiyun int ret_val = -ENOMSG;
2089*4882a593Smuzhiyun u32 doi;
2090*4882a593Smuzhiyun struct cipso_v4_doi *doi_def;
2091*4882a593Smuzhiyun
2092*4882a593Smuzhiyun if (cipso_v4_cache_check(cipso, cipso[1], secattr) == 0)
2093*4882a593Smuzhiyun return 0;
2094*4882a593Smuzhiyun
2095*4882a593Smuzhiyun doi = get_unaligned_be32(&cipso[2]);
2096*4882a593Smuzhiyun rcu_read_lock();
2097*4882a593Smuzhiyun doi_def = cipso_v4_doi_search(doi);
2098*4882a593Smuzhiyun if (!doi_def)
2099*4882a593Smuzhiyun goto getattr_return;
2100*4882a593Smuzhiyun /* XXX - This code assumes only one tag per CIPSO option which isn't
2101*4882a593Smuzhiyun * really a good assumption to make but since we only support the MAC
2102*4882a593Smuzhiyun * tags right now it is a safe assumption. */
2103*4882a593Smuzhiyun switch (cipso[6]) {
2104*4882a593Smuzhiyun case CIPSO_V4_TAG_RBITMAP:
2105*4882a593Smuzhiyun ret_val = cipso_v4_parsetag_rbm(doi_def, &cipso[6], secattr);
2106*4882a593Smuzhiyun break;
2107*4882a593Smuzhiyun case CIPSO_V4_TAG_ENUM:
2108*4882a593Smuzhiyun ret_val = cipso_v4_parsetag_enum(doi_def, &cipso[6], secattr);
2109*4882a593Smuzhiyun break;
2110*4882a593Smuzhiyun case CIPSO_V4_TAG_RANGE:
2111*4882a593Smuzhiyun ret_val = cipso_v4_parsetag_rng(doi_def, &cipso[6], secattr);
2112*4882a593Smuzhiyun break;
2113*4882a593Smuzhiyun case CIPSO_V4_TAG_LOCAL:
2114*4882a593Smuzhiyun ret_val = cipso_v4_parsetag_loc(doi_def, &cipso[6], secattr);
2115*4882a593Smuzhiyun break;
2116*4882a593Smuzhiyun }
2117*4882a593Smuzhiyun if (ret_val == 0)
2118*4882a593Smuzhiyun secattr->type = NETLBL_NLTYPE_CIPSOV4;
2119*4882a593Smuzhiyun
2120*4882a593Smuzhiyun getattr_return:
2121*4882a593Smuzhiyun rcu_read_unlock();
2122*4882a593Smuzhiyun return ret_val;
2123*4882a593Smuzhiyun }
2124*4882a593Smuzhiyun
2125*4882a593Smuzhiyun /**
2126*4882a593Smuzhiyun * cipso_v4_sock_getattr - Get the security attributes from a sock
2127*4882a593Smuzhiyun * @sk: the sock
2128*4882a593Smuzhiyun * @secattr: the security attributes
2129*4882a593Smuzhiyun *
2130*4882a593Smuzhiyun * Description:
2131*4882a593Smuzhiyun * Query @sk to see if there is a CIPSO option attached to the sock and if
2132*4882a593Smuzhiyun * there is return the CIPSO security attributes in @secattr. This function
2133*4882a593Smuzhiyun * requires that @sk be locked, or privately held, but it does not do any
2134*4882a593Smuzhiyun * locking itself. Returns zero on success and negative values on failure.
2135*4882a593Smuzhiyun *
2136*4882a593Smuzhiyun */
cipso_v4_sock_getattr(struct sock * sk,struct netlbl_lsm_secattr * secattr)2137*4882a593Smuzhiyun int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
2138*4882a593Smuzhiyun {
2139*4882a593Smuzhiyun struct ip_options_rcu *opt;
2140*4882a593Smuzhiyun int res = -ENOMSG;
2141*4882a593Smuzhiyun
2142*4882a593Smuzhiyun rcu_read_lock();
2143*4882a593Smuzhiyun opt = rcu_dereference(inet_sk(sk)->inet_opt);
2144*4882a593Smuzhiyun if (opt && opt->opt.cipso)
2145*4882a593Smuzhiyun res = cipso_v4_getattr(opt->opt.__data +
2146*4882a593Smuzhiyun opt->opt.cipso -
2147*4882a593Smuzhiyun sizeof(struct iphdr),
2148*4882a593Smuzhiyun secattr);
2149*4882a593Smuzhiyun rcu_read_unlock();
2150*4882a593Smuzhiyun return res;
2151*4882a593Smuzhiyun }
2152*4882a593Smuzhiyun
2153*4882a593Smuzhiyun /**
2154*4882a593Smuzhiyun * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
2155*4882a593Smuzhiyun * @skb: the packet
2156*4882a593Smuzhiyun * @doi_def: the DOI structure
2157*4882a593Smuzhiyun * @secattr: the security attributes
2158*4882a593Smuzhiyun *
2159*4882a593Smuzhiyun * Description:
2160*4882a593Smuzhiyun * Set the CIPSO option on the given packet based on the security attributes.
2161*4882a593Smuzhiyun * Returns a pointer to the IP header on success and NULL on failure.
2162*4882a593Smuzhiyun *
2163*4882a593Smuzhiyun */
cipso_v4_skbuff_setattr(struct sk_buff * skb,const struct cipso_v4_doi * doi_def,const struct netlbl_lsm_secattr * secattr)2164*4882a593Smuzhiyun int cipso_v4_skbuff_setattr(struct sk_buff *skb,
2165*4882a593Smuzhiyun const struct cipso_v4_doi *doi_def,
2166*4882a593Smuzhiyun const struct netlbl_lsm_secattr *secattr)
2167*4882a593Smuzhiyun {
2168*4882a593Smuzhiyun int ret_val;
2169*4882a593Smuzhiyun struct iphdr *iph;
2170*4882a593Smuzhiyun struct ip_options *opt = &IPCB(skb)->opt;
2171*4882a593Smuzhiyun unsigned char buf[CIPSO_V4_OPT_LEN_MAX];
2172*4882a593Smuzhiyun u32 buf_len = CIPSO_V4_OPT_LEN_MAX;
2173*4882a593Smuzhiyun u32 opt_len;
2174*4882a593Smuzhiyun int len_delta;
2175*4882a593Smuzhiyun
2176*4882a593Smuzhiyun ret_val = cipso_v4_genopt(buf, buf_len, doi_def, secattr);
2177*4882a593Smuzhiyun if (ret_val < 0)
2178*4882a593Smuzhiyun return ret_val;
2179*4882a593Smuzhiyun buf_len = ret_val;
2180*4882a593Smuzhiyun opt_len = (buf_len + 3) & ~3;
2181*4882a593Smuzhiyun
2182*4882a593Smuzhiyun /* we overwrite any existing options to ensure that we have enough
2183*4882a593Smuzhiyun * room for the CIPSO option, the reason is that we _need_ to guarantee
2184*4882a593Smuzhiyun * that the security label is applied to the packet - we do the same
2185*4882a593Smuzhiyun * thing when using the socket options and it hasn't caused a problem,
2186*4882a593Smuzhiyun * if we need to we can always revisit this choice later */
2187*4882a593Smuzhiyun
2188*4882a593Smuzhiyun len_delta = opt_len - opt->optlen;
2189*4882a593Smuzhiyun /* if we don't ensure enough headroom we could panic on the skb_push()
2190*4882a593Smuzhiyun * call below so make sure we have enough, we are also "mangling" the
2191*4882a593Smuzhiyun * packet so we should probably do a copy-on-write call anyway */
2192*4882a593Smuzhiyun ret_val = skb_cow(skb, skb_headroom(skb) + len_delta);
2193*4882a593Smuzhiyun if (ret_val < 0)
2194*4882a593Smuzhiyun return ret_val;
2195*4882a593Smuzhiyun
2196*4882a593Smuzhiyun if (len_delta > 0) {
2197*4882a593Smuzhiyun /* we assume that the header + opt->optlen have already been
2198*4882a593Smuzhiyun * "pushed" in ip_options_build() or similar */
2199*4882a593Smuzhiyun iph = ip_hdr(skb);
2200*4882a593Smuzhiyun skb_push(skb, len_delta);
2201*4882a593Smuzhiyun memmove((char *)iph - len_delta, iph, iph->ihl << 2);
2202*4882a593Smuzhiyun skb_reset_network_header(skb);
2203*4882a593Smuzhiyun iph = ip_hdr(skb);
2204*4882a593Smuzhiyun } else if (len_delta < 0) {
2205*4882a593Smuzhiyun iph = ip_hdr(skb);
2206*4882a593Smuzhiyun memset(iph + 1, IPOPT_NOP, opt->optlen);
2207*4882a593Smuzhiyun } else
2208*4882a593Smuzhiyun iph = ip_hdr(skb);
2209*4882a593Smuzhiyun
2210*4882a593Smuzhiyun if (opt->optlen > 0)
2211*4882a593Smuzhiyun memset(opt, 0, sizeof(*opt));
2212*4882a593Smuzhiyun opt->optlen = opt_len;
2213*4882a593Smuzhiyun opt->cipso = sizeof(struct iphdr);
2214*4882a593Smuzhiyun opt->is_changed = 1;
2215*4882a593Smuzhiyun
2216*4882a593Smuzhiyun /* we have to do the following because we are being called from a
2217*4882a593Smuzhiyun * netfilter hook which means the packet already has had the header
2218*4882a593Smuzhiyun * fields populated and the checksum calculated - yes this means we
2219*4882a593Smuzhiyun * are doing more work than needed but we do it to keep the core
2220*4882a593Smuzhiyun * stack clean and tidy */
2221*4882a593Smuzhiyun memcpy(iph + 1, buf, buf_len);
2222*4882a593Smuzhiyun if (opt_len > buf_len)
2223*4882a593Smuzhiyun memset((char *)(iph + 1) + buf_len, 0, opt_len - buf_len);
2224*4882a593Smuzhiyun if (len_delta != 0) {
2225*4882a593Smuzhiyun iph->ihl = 5 + (opt_len >> 2);
2226*4882a593Smuzhiyun iph->tot_len = htons(skb->len);
2227*4882a593Smuzhiyun }
2228*4882a593Smuzhiyun ip_send_check(iph);
2229*4882a593Smuzhiyun
2230*4882a593Smuzhiyun return 0;
2231*4882a593Smuzhiyun }
2232*4882a593Smuzhiyun
2233*4882a593Smuzhiyun /**
2234*4882a593Smuzhiyun * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
2235*4882a593Smuzhiyun * @skb: the packet
2236*4882a593Smuzhiyun *
2237*4882a593Smuzhiyun * Description:
2238*4882a593Smuzhiyun * Removes any and all CIPSO options from the given packet. Returns zero on
2239*4882a593Smuzhiyun * success, negative values on failure.
2240*4882a593Smuzhiyun *
2241*4882a593Smuzhiyun */
cipso_v4_skbuff_delattr(struct sk_buff * skb)2242*4882a593Smuzhiyun int cipso_v4_skbuff_delattr(struct sk_buff *skb)
2243*4882a593Smuzhiyun {
2244*4882a593Smuzhiyun int ret_val;
2245*4882a593Smuzhiyun struct iphdr *iph;
2246*4882a593Smuzhiyun struct ip_options *opt = &IPCB(skb)->opt;
2247*4882a593Smuzhiyun unsigned char *cipso_ptr;
2248*4882a593Smuzhiyun
2249*4882a593Smuzhiyun if (opt->cipso == 0)
2250*4882a593Smuzhiyun return 0;
2251*4882a593Smuzhiyun
2252*4882a593Smuzhiyun /* since we are changing the packet we should make a copy */
2253*4882a593Smuzhiyun ret_val = skb_cow(skb, skb_headroom(skb));
2254*4882a593Smuzhiyun if (ret_val < 0)
2255*4882a593Smuzhiyun return ret_val;
2256*4882a593Smuzhiyun
2257*4882a593Smuzhiyun /* the easiest thing to do is just replace the cipso option with noop
2258*4882a593Smuzhiyun * options since we don't change the size of the packet, although we
2259*4882a593Smuzhiyun * still need to recalculate the checksum */
2260*4882a593Smuzhiyun
2261*4882a593Smuzhiyun iph = ip_hdr(skb);
2262*4882a593Smuzhiyun cipso_ptr = (unsigned char *)iph + opt->cipso;
2263*4882a593Smuzhiyun memset(cipso_ptr, IPOPT_NOOP, cipso_ptr[1]);
2264*4882a593Smuzhiyun opt->cipso = 0;
2265*4882a593Smuzhiyun opt->is_changed = 1;
2266*4882a593Smuzhiyun
2267*4882a593Smuzhiyun ip_send_check(iph);
2268*4882a593Smuzhiyun
2269*4882a593Smuzhiyun return 0;
2270*4882a593Smuzhiyun }
2271*4882a593Smuzhiyun
2272*4882a593Smuzhiyun /*
2273*4882a593Smuzhiyun * Setup Functions
2274*4882a593Smuzhiyun */
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun /**
2277*4882a593Smuzhiyun * cipso_v4_init - Initialize the CIPSO module
2278*4882a593Smuzhiyun *
2279*4882a593Smuzhiyun * Description:
2280*4882a593Smuzhiyun * Initialize the CIPSO module and prepare it for use. Returns zero on success
2281*4882a593Smuzhiyun * and negative values on failure.
2282*4882a593Smuzhiyun *
2283*4882a593Smuzhiyun */
cipso_v4_init(void)2284*4882a593Smuzhiyun static int __init cipso_v4_init(void)
2285*4882a593Smuzhiyun {
2286*4882a593Smuzhiyun int ret_val;
2287*4882a593Smuzhiyun
2288*4882a593Smuzhiyun ret_val = cipso_v4_cache_init();
2289*4882a593Smuzhiyun if (ret_val != 0)
2290*4882a593Smuzhiyun panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
2291*4882a593Smuzhiyun ret_val);
2292*4882a593Smuzhiyun
2293*4882a593Smuzhiyun return 0;
2294*4882a593Smuzhiyun }
2295*4882a593Smuzhiyun
2296*4882a593Smuzhiyun subsys_initcall(cipso_v4_init);
2297