ACPI: thinkpad-acpi: allow use of CMOS NVRAM for brightness control
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / cipso_ipv4.c
blobab56a052ce31cb53e873df670a4afdc8dc68972a
1 /*
2 * CIPSO - Commercial IP Security Option
4 * This is an implementation of the CIPSO 2.2 protocol as specified in
5 * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
6 * FIPS-188, copies of both documents can be found in the Documentation
7 * directory. While CIPSO never became a full IETF RFC standard many vendors
8 * have chosen to adopt the protocol and over the years it has become a
9 * de-facto standard for labeled networking.
11 * Author: Paul Moore <paul.moore@hp.com>
16 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
26 * the GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/rcupdate.h>
37 #include <linux/list.h>
38 #include <linux/spinlock.h>
39 #include <linux/string.h>
40 #include <linux/jhash.h>
41 #include <net/ip.h>
42 #include <net/icmp.h>
43 #include <net/tcp.h>
44 #include <net/netlabel.h>
45 #include <net/cipso_ipv4.h>
46 #include <asm/atomic.h>
47 #include <asm/bug.h>
48 #include <asm/unaligned.h>
50 struct cipso_v4_domhsh_entry {
51 char *domain;
52 u32 valid;
53 struct list_head list;
54 struct rcu_head rcu;
57 /* List of available DOI definitions */
58 /* XXX - Updates should be minimal so having a single lock for the
59 * cipso_v4_doi_list and the cipso_v4_doi_list->dom_list should be
60 * okay. */
61 /* XXX - This currently assumes a minimal number of different DOIs in use,
62 * if in practice there are a lot of different DOIs this list should
63 * probably be turned into a hash table or something similar so we
64 * can do quick lookups. */
65 static DEFINE_SPINLOCK(cipso_v4_doi_list_lock);
66 static struct list_head cipso_v4_doi_list = LIST_HEAD_INIT(cipso_v4_doi_list);
68 /* Label mapping cache */
69 int cipso_v4_cache_enabled = 1;
70 int cipso_v4_cache_bucketsize = 10;
71 #define CIPSO_V4_CACHE_BUCKETBITS 7
72 #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
73 #define CIPSO_V4_CACHE_REORDERLIMIT 10
74 struct cipso_v4_map_cache_bkt {
75 spinlock_t lock;
76 u32 size;
77 struct list_head list;
79 struct cipso_v4_map_cache_entry {
80 u32 hash;
81 unsigned char *key;
82 size_t key_len;
84 struct netlbl_lsm_cache *lsm_data;
86 u32 activity;
87 struct list_head list;
89 static struct cipso_v4_map_cache_bkt *cipso_v4_cache = NULL;
91 /* Restricted bitmap (tag #1) flags */
92 int cipso_v4_rbm_optfmt = 0;
93 int cipso_v4_rbm_strictvalid = 1;
96 * Protocol Constants
99 /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
100 * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
101 #define CIPSO_V4_OPT_LEN_MAX 40
103 /* Length of the base CIPSO option, this includes the option type (1 byte), the
104 * option length (1 byte), and the DOI (4 bytes). */
105 #define CIPSO_V4_HDR_LEN 6
107 /* Base length of the restrictive category bitmap tag (tag #1). */
108 #define CIPSO_V4_TAG_RBM_BLEN 4
110 /* Base length of the enumerated category tag (tag #2). */
111 #define CIPSO_V4_TAG_ENUM_BLEN 4
113 /* Base length of the ranged categories bitmap tag (tag #5). */
114 #define CIPSO_V4_TAG_RNG_BLEN 4
115 /* The maximum number of category ranges permitted in the ranged category tag
116 * (tag #5). You may note that the IETF draft states that the maximum number
117 * of category ranges is 7, but if the low end of the last category range is
118 * zero then it is possibile to fit 8 category ranges because the zero should
119 * be omitted. */
120 #define CIPSO_V4_TAG_RNG_CAT_MAX 8
123 * Helper Functions
127 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
128 * @bitmap: the bitmap
129 * @bitmap_len: length in bits
130 * @offset: starting offset
131 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
133 * Description:
134 * Starting at @offset, walk the bitmap from left to right until either the
135 * desired bit is found or we reach the end. Return the bit offset, -1 if
136 * not found, or -2 if error.
138 static int cipso_v4_bitmap_walk(const unsigned char *bitmap,
139 u32 bitmap_len,
140 u32 offset,
141 u8 state)
143 u32 bit_spot;
144 u32 byte_offset;
145 unsigned char bitmask;
146 unsigned char byte;
148 /* gcc always rounds to zero when doing integer division */
149 byte_offset = offset / 8;
150 byte = bitmap[byte_offset];
151 bit_spot = offset;
152 bitmask = 0x80 >> (offset % 8);
154 while (bit_spot < bitmap_len) {
155 if ((state && (byte & bitmask) == bitmask) ||
156 (state == 0 && (byte & bitmask) == 0))
157 return bit_spot;
159 bit_spot++;
160 bitmask >>= 1;
161 if (bitmask == 0) {
162 byte = bitmap[++byte_offset];
163 bitmask = 0x80;
167 return -1;
171 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
172 * @bitmap: the bitmap
173 * @bit: the bit
174 * @state: if non-zero, set the bit (1) else clear the bit (0)
176 * Description:
177 * Set a single bit in the bitmask. Returns zero on success, negative values
178 * on error.
180 static void cipso_v4_bitmap_setbit(unsigned char *bitmap,
181 u32 bit,
182 u8 state)
184 u32 byte_spot;
185 u8 bitmask;
187 /* gcc always rounds to zero when doing integer division */
188 byte_spot = bit / 8;
189 bitmask = 0x80 >> (bit % 8);
190 if (state)
191 bitmap[byte_spot] |= bitmask;
192 else
193 bitmap[byte_spot] &= ~bitmask;
197 * cipso_v4_doi_domhsh_free - Frees a domain list entry
198 * @entry: the entry's RCU field
200 * Description:
201 * This function is designed to be used as a callback to the call_rcu()
202 * function so that the memory allocated to a domain list entry can be released
203 * safely.
206 static void cipso_v4_doi_domhsh_free(struct rcu_head *entry)
208 struct cipso_v4_domhsh_entry *ptr;
210 ptr = container_of(entry, struct cipso_v4_domhsh_entry, rcu);
211 kfree(ptr->domain);
212 kfree(ptr);
216 * cipso_v4_cache_entry_free - Frees a cache entry
217 * @entry: the entry to free
219 * Description:
220 * This function frees the memory associated with a cache entry including the
221 * LSM cache data if there are no longer any users, i.e. reference count == 0.
224 static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry *entry)
226 if (entry->lsm_data)
227 netlbl_secattr_cache_free(entry->lsm_data);
228 kfree(entry->key);
229 kfree(entry);
233 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
234 * @key: the hash key
235 * @key_len: the length of the key in bytes
237 * Description:
238 * The CIPSO tag hashing function. Returns a 32-bit hash value.
241 static u32 cipso_v4_map_cache_hash(const unsigned char *key, u32 key_len)
243 return jhash(key, key_len, 0);
247 * Label Mapping Cache Functions
251 * cipso_v4_cache_init - Initialize the CIPSO cache
253 * Description:
254 * Initializes the CIPSO label mapping cache, this function should be called
255 * before any of the other functions defined in this file. Returns zero on
256 * success, negative values on error.
259 static int cipso_v4_cache_init(void)
261 u32 iter;
263 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
264 sizeof(struct cipso_v4_map_cache_bkt),
265 GFP_KERNEL);
266 if (cipso_v4_cache == NULL)
267 return -ENOMEM;
269 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
270 spin_lock_init(&cipso_v4_cache[iter].lock);
271 cipso_v4_cache[iter].size = 0;
272 INIT_LIST_HEAD(&cipso_v4_cache[iter].list);
275 return 0;
279 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
281 * Description:
282 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
283 * success and negative values on failure.
286 void cipso_v4_cache_invalidate(void)
288 struct cipso_v4_map_cache_entry *entry, *tmp_entry;
289 u32 iter;
291 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
292 spin_lock_bh(&cipso_v4_cache[iter].lock);
293 list_for_each_entry_safe(entry,
294 tmp_entry,
295 &cipso_v4_cache[iter].list, list) {
296 list_del(&entry->list);
297 cipso_v4_cache_entry_free(entry);
299 cipso_v4_cache[iter].size = 0;
300 spin_unlock_bh(&cipso_v4_cache[iter].lock);
303 return;
307 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
308 * @key: the buffer to check
309 * @key_len: buffer length in bytes
310 * @secattr: the security attribute struct to use
312 * Description:
313 * This function checks the cache to see if a label mapping already exists for
314 * the given key. If there is a match then the cache is adjusted and the
315 * @secattr struct is populated with the correct LSM security attributes. The
316 * cache is adjusted in the following manner if the entry is not already the
317 * first in the cache bucket:
319 * 1. The cache entry's activity counter is incremented
320 * 2. The previous (higher ranking) entry's activity counter is decremented
321 * 3. If the difference between the two activity counters is geater than
322 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
324 * Returns zero on success, -ENOENT for a cache miss, and other negative values
325 * on error.
328 static int cipso_v4_cache_check(const unsigned char *key,
329 u32 key_len,
330 struct netlbl_lsm_secattr *secattr)
332 u32 bkt;
333 struct cipso_v4_map_cache_entry *entry;
334 struct cipso_v4_map_cache_entry *prev_entry = NULL;
335 u32 hash;
337 if (!cipso_v4_cache_enabled)
338 return -ENOENT;
340 hash = cipso_v4_map_cache_hash(key, key_len);
341 bkt = hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
342 spin_lock_bh(&cipso_v4_cache[bkt].lock);
343 list_for_each_entry(entry, &cipso_v4_cache[bkt].list, list) {
344 if (entry->hash == hash &&
345 entry->key_len == key_len &&
346 memcmp(entry->key, key, key_len) == 0) {
347 entry->activity += 1;
348 atomic_inc(&entry->lsm_data->refcount);
349 secattr->cache = entry->lsm_data;
350 secattr->flags |= NETLBL_SECATTR_CACHE;
351 if (prev_entry == NULL) {
352 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
353 return 0;
356 if (prev_entry->activity > 0)
357 prev_entry->activity -= 1;
358 if (entry->activity > prev_entry->activity &&
359 entry->activity - prev_entry->activity >
360 CIPSO_V4_CACHE_REORDERLIMIT) {
361 __list_del(entry->list.prev, entry->list.next);
362 __list_add(&entry->list,
363 prev_entry->list.prev,
364 &prev_entry->list);
367 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
368 return 0;
370 prev_entry = entry;
372 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
374 return -ENOENT;
378 * cipso_v4_cache_add - Add an entry to the CIPSO cache
379 * @skb: the packet
380 * @secattr: the packet's security attributes
382 * Description:
383 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
384 * head of the cache bucket's list, if the cache bucket is out of room remove
385 * the last entry in the list first. It is important to note that there is
386 * currently no checking for duplicate keys. Returns zero on success,
387 * negative values on failure.
390 int cipso_v4_cache_add(const struct sk_buff *skb,
391 const struct netlbl_lsm_secattr *secattr)
393 int ret_val = -EPERM;
394 u32 bkt;
395 struct cipso_v4_map_cache_entry *entry = NULL;
396 struct cipso_v4_map_cache_entry *old_entry = NULL;
397 unsigned char *cipso_ptr;
398 u32 cipso_ptr_len;
400 if (!cipso_v4_cache_enabled || cipso_v4_cache_bucketsize <= 0)
401 return 0;
403 cipso_ptr = CIPSO_V4_OPTPTR(skb);
404 cipso_ptr_len = cipso_ptr[1];
406 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
407 if (entry == NULL)
408 return -ENOMEM;
409 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
410 if (entry->key == NULL) {
411 ret_val = -ENOMEM;
412 goto cache_add_failure;
414 entry->key_len = cipso_ptr_len;
415 entry->hash = cipso_v4_map_cache_hash(cipso_ptr, cipso_ptr_len);
416 atomic_inc(&secattr->cache->refcount);
417 entry->lsm_data = secattr->cache;
419 bkt = entry->hash & (CIPSO_V4_CACHE_BUCKETBITS - 1);
420 spin_lock_bh(&cipso_v4_cache[bkt].lock);
421 if (cipso_v4_cache[bkt].size < cipso_v4_cache_bucketsize) {
422 list_add(&entry->list, &cipso_v4_cache[bkt].list);
423 cipso_v4_cache[bkt].size += 1;
424 } else {
425 old_entry = list_entry(cipso_v4_cache[bkt].list.prev,
426 struct cipso_v4_map_cache_entry, list);
427 list_del(&old_entry->list);
428 list_add(&entry->list, &cipso_v4_cache[bkt].list);
429 cipso_v4_cache_entry_free(old_entry);
431 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
433 return 0;
435 cache_add_failure:
436 if (entry)
437 cipso_v4_cache_entry_free(entry);
438 return ret_val;
442 * DOI List Functions
446 * cipso_v4_doi_search - Searches for a DOI definition
447 * @doi: the DOI to search for
449 * Description:
450 * Search the DOI definition list for a DOI definition with a DOI value that
451 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
452 * Returns a pointer to the DOI definition on success and NULL on failure.
454 static struct cipso_v4_doi *cipso_v4_doi_search(u32 doi)
456 struct cipso_v4_doi *iter;
458 list_for_each_entry_rcu(iter, &cipso_v4_doi_list, list)
459 if (iter->doi == doi && iter->valid)
460 return iter;
461 return NULL;
465 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
466 * @doi_def: the DOI structure
468 * Description:
469 * The caller defines a new DOI for use by the CIPSO engine and calls this
470 * function to add it to the list of acceptable domains. The caller must
471 * ensure that the mapping table specified in @doi_def->map meets all of the
472 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
473 * zero on success and non-zero on failure.
476 int cipso_v4_doi_add(struct cipso_v4_doi *doi_def)
478 u32 iter;
480 if (doi_def == NULL || doi_def->doi == CIPSO_V4_DOI_UNKNOWN)
481 return -EINVAL;
482 for (iter = 0; iter < CIPSO_V4_TAG_MAXCNT; iter++) {
483 switch (doi_def->tags[iter]) {
484 case CIPSO_V4_TAG_RBITMAP:
485 break;
486 case CIPSO_V4_TAG_RANGE:
487 if (doi_def->type != CIPSO_V4_MAP_PASS)
488 return -EINVAL;
489 break;
490 case CIPSO_V4_TAG_INVALID:
491 if (iter == 0)
492 return -EINVAL;
493 break;
494 case CIPSO_V4_TAG_ENUM:
495 if (doi_def->type != CIPSO_V4_MAP_PASS)
496 return -EINVAL;
497 break;
498 default:
499 return -EINVAL;
503 doi_def->valid = 1;
504 INIT_RCU_HEAD(&doi_def->rcu);
505 INIT_LIST_HEAD(&doi_def->dom_list);
507 rcu_read_lock();
508 if (cipso_v4_doi_search(doi_def->doi) != NULL)
509 goto doi_add_failure_rlock;
510 spin_lock(&cipso_v4_doi_list_lock);
511 if (cipso_v4_doi_search(doi_def->doi) != NULL)
512 goto doi_add_failure_slock;
513 list_add_tail_rcu(&doi_def->list, &cipso_v4_doi_list);
514 spin_unlock(&cipso_v4_doi_list_lock);
515 rcu_read_unlock();
517 return 0;
519 doi_add_failure_slock:
520 spin_unlock(&cipso_v4_doi_list_lock);
521 doi_add_failure_rlock:
522 rcu_read_unlock();
523 return -EEXIST;
527 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
528 * @doi: the DOI value
529 * @audit_secid: the LSM secid to use in the audit message
530 * @callback: the DOI cleanup/free callback
532 * Description:
533 * Removes a DOI definition from the CIPSO engine, @callback is called to
534 * free any memory. The NetLabel routines will be called to release their own
535 * LSM domain mappings as well as our own domain list. Returns zero on
536 * success and negative values on failure.
539 int cipso_v4_doi_remove(u32 doi,
540 struct netlbl_audit *audit_info,
541 void (*callback) (struct rcu_head * head))
543 struct cipso_v4_doi *doi_def;
544 struct cipso_v4_domhsh_entry *dom_iter;
546 rcu_read_lock();
547 if (cipso_v4_doi_search(doi) != NULL) {
548 spin_lock(&cipso_v4_doi_list_lock);
549 doi_def = cipso_v4_doi_search(doi);
550 if (doi_def == NULL) {
551 spin_unlock(&cipso_v4_doi_list_lock);
552 rcu_read_unlock();
553 return -ENOENT;
555 doi_def->valid = 0;
556 list_del_rcu(&doi_def->list);
557 spin_unlock(&cipso_v4_doi_list_lock);
558 list_for_each_entry_rcu(dom_iter, &doi_def->dom_list, list)
559 if (dom_iter->valid)
560 netlbl_domhsh_remove(dom_iter->domain,
561 audit_info);
562 cipso_v4_cache_invalidate();
563 rcu_read_unlock();
565 call_rcu(&doi_def->rcu, callback);
566 return 0;
568 rcu_read_unlock();
570 return -ENOENT;
574 * cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition
575 * @doi: the DOI value
577 * Description:
578 * Searches for a valid DOI definition and if one is found it is returned to
579 * the caller. Otherwise NULL is returned. The caller must ensure that
580 * rcu_read_lock() is held while accessing the returned definition.
583 struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
585 return cipso_v4_doi_search(doi);
589 * cipso_v4_doi_walk - Iterate through the DOI definitions
590 * @skip_cnt: skip past this number of DOI definitions, updated
591 * @callback: callback for each DOI definition
592 * @cb_arg: argument for the callback function
594 * Description:
595 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
596 * For each entry call @callback, if @callback returns a negative value stop
597 * 'walking' through the list and return. Updates the value in @skip_cnt upon
598 * return. Returns zero on success, negative values on failure.
601 int cipso_v4_doi_walk(u32 *skip_cnt,
602 int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
603 void *cb_arg)
605 int ret_val = -ENOENT;
606 u32 doi_cnt = 0;
607 struct cipso_v4_doi *iter_doi;
609 rcu_read_lock();
610 list_for_each_entry_rcu(iter_doi, &cipso_v4_doi_list, list)
611 if (iter_doi->valid) {
612 if (doi_cnt++ < *skip_cnt)
613 continue;
614 ret_val = callback(iter_doi, cb_arg);
615 if (ret_val < 0) {
616 doi_cnt--;
617 goto doi_walk_return;
621 doi_walk_return:
622 rcu_read_unlock();
623 *skip_cnt = doi_cnt;
624 return ret_val;
628 * cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition
629 * @doi_def: the DOI definition
630 * @domain: the domain to add
632 * Description:
633 * Adds the @domain to the DOI specified by @doi_def, this function
634 * should only be called by external functions (i.e. NetLabel). This function
635 * does allocate memory. Returns zero on success, negative values on failure.
638 int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def, const char *domain)
640 struct cipso_v4_domhsh_entry *iter;
641 struct cipso_v4_domhsh_entry *new_dom;
643 new_dom = kzalloc(sizeof(*new_dom), GFP_KERNEL);
644 if (new_dom == NULL)
645 return -ENOMEM;
646 if (domain) {
647 new_dom->domain = kstrdup(domain, GFP_KERNEL);
648 if (new_dom->domain == NULL) {
649 kfree(new_dom);
650 return -ENOMEM;
653 new_dom->valid = 1;
654 INIT_RCU_HEAD(&new_dom->rcu);
656 rcu_read_lock();
657 spin_lock(&cipso_v4_doi_list_lock);
658 list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
659 if (iter->valid &&
660 ((domain != NULL && iter->domain != NULL &&
661 strcmp(iter->domain, domain) == 0) ||
662 (domain == NULL && iter->domain == NULL))) {
663 spin_unlock(&cipso_v4_doi_list_lock);
664 rcu_read_unlock();
665 kfree(new_dom->domain);
666 kfree(new_dom);
667 return -EEXIST;
669 list_add_tail_rcu(&new_dom->list, &doi_def->dom_list);
670 spin_unlock(&cipso_v4_doi_list_lock);
671 rcu_read_unlock();
673 return 0;
677 * cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition
678 * @doi_def: the DOI definition
679 * @domain: the domain to remove
681 * Description:
682 * Removes the @domain from the DOI specified by @doi_def, this function
683 * should only be called by external functions (i.e. NetLabel). Returns zero
684 * on success and negative values on error.
687 int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
688 const char *domain)
690 struct cipso_v4_domhsh_entry *iter;
692 rcu_read_lock();
693 spin_lock(&cipso_v4_doi_list_lock);
694 list_for_each_entry_rcu(iter, &doi_def->dom_list, list)
695 if (iter->valid &&
696 ((domain != NULL && iter->domain != NULL &&
697 strcmp(iter->domain, domain) == 0) ||
698 (domain == NULL && iter->domain == NULL))) {
699 iter->valid = 0;
700 list_del_rcu(&iter->list);
701 spin_unlock(&cipso_v4_doi_list_lock);
702 rcu_read_unlock();
703 call_rcu(&iter->rcu, cipso_v4_doi_domhsh_free);
705 return 0;
707 spin_unlock(&cipso_v4_doi_list_lock);
708 rcu_read_unlock();
710 return -ENOENT;
714 * Label Mapping Functions
718 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
719 * @doi_def: the DOI definition
720 * @level: the level to check
722 * Description:
723 * Checks the given level against the given DOI definition and returns a
724 * negative value if the level does not have a valid mapping and a zero value
725 * if the level is defined by the DOI.
728 static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi *doi_def, u8 level)
730 switch (doi_def->type) {
731 case CIPSO_V4_MAP_PASS:
732 return 0;
733 case CIPSO_V4_MAP_STD:
734 if (doi_def->map.std->lvl.cipso[level] < CIPSO_V4_INV_LVL)
735 return 0;
736 break;
739 return -EFAULT;
743 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
744 * @doi_def: the DOI definition
745 * @host_lvl: the host MLS level
746 * @net_lvl: the network/CIPSO MLS level
748 * Description:
749 * Perform a label mapping to translate a local MLS level to the correct
750 * CIPSO level using the given DOI definition. Returns zero on success,
751 * negative values otherwise.
754 static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi *doi_def,
755 u32 host_lvl,
756 u32 *net_lvl)
758 switch (doi_def->type) {
759 case CIPSO_V4_MAP_PASS:
760 *net_lvl = host_lvl;
761 return 0;
762 case CIPSO_V4_MAP_STD:
763 if (host_lvl < doi_def->map.std->lvl.local_size &&
764 doi_def->map.std->lvl.local[host_lvl] < CIPSO_V4_INV_LVL) {
765 *net_lvl = doi_def->map.std->lvl.local[host_lvl];
766 return 0;
768 return -EPERM;
771 return -EINVAL;
775 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
776 * @doi_def: the DOI definition
777 * @net_lvl: the network/CIPSO MLS level
778 * @host_lvl: the host MLS level
780 * Description:
781 * Perform a label mapping to translate a CIPSO level to the correct local MLS
782 * level using the given DOI definition. Returns zero on success, negative
783 * values otherwise.
786 static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi *doi_def,
787 u32 net_lvl,
788 u32 *host_lvl)
790 struct cipso_v4_std_map_tbl *map_tbl;
792 switch (doi_def->type) {
793 case CIPSO_V4_MAP_PASS:
794 *host_lvl = net_lvl;
795 return 0;
796 case CIPSO_V4_MAP_STD:
797 map_tbl = doi_def->map.std;
798 if (net_lvl < map_tbl->lvl.cipso_size &&
799 map_tbl->lvl.cipso[net_lvl] < CIPSO_V4_INV_LVL) {
800 *host_lvl = doi_def->map.std->lvl.cipso[net_lvl];
801 return 0;
803 return -EPERM;
806 return -EINVAL;
810 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
811 * @doi_def: the DOI definition
812 * @bitmap: category bitmap
813 * @bitmap_len: bitmap length in bytes
815 * Description:
816 * Checks the given category bitmap against the given DOI definition and
817 * returns a negative value if any of the categories in the bitmap do not have
818 * a valid mapping and a zero value if all of the categories are valid.
821 static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi *doi_def,
822 const unsigned char *bitmap,
823 u32 bitmap_len)
825 int cat = -1;
826 u32 bitmap_len_bits = bitmap_len * 8;
827 u32 cipso_cat_size;
828 u32 *cipso_array;
830 switch (doi_def->type) {
831 case CIPSO_V4_MAP_PASS:
832 return 0;
833 case CIPSO_V4_MAP_STD:
834 cipso_cat_size = doi_def->map.std->cat.cipso_size;
835 cipso_array = doi_def->map.std->cat.cipso;
836 for (;;) {
837 cat = cipso_v4_bitmap_walk(bitmap,
838 bitmap_len_bits,
839 cat + 1,
841 if (cat < 0)
842 break;
843 if (cat >= cipso_cat_size ||
844 cipso_array[cat] >= CIPSO_V4_INV_CAT)
845 return -EFAULT;
848 if (cat == -1)
849 return 0;
850 break;
853 return -EFAULT;
857 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
858 * @doi_def: the DOI definition
859 * @secattr: the security attributes
860 * @net_cat: the zero'd out category bitmap in network/CIPSO format
861 * @net_cat_len: the length of the CIPSO bitmap in bytes
863 * Description:
864 * Perform a label mapping to translate a local MLS category bitmap to the
865 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
866 * size in bytes of the network bitmap on success, negative values otherwise.
869 static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi *doi_def,
870 const struct netlbl_lsm_secattr *secattr,
871 unsigned char *net_cat,
872 u32 net_cat_len)
874 int host_spot = -1;
875 u32 net_spot = CIPSO_V4_INV_CAT;
876 u32 net_spot_max = 0;
877 u32 net_clen_bits = net_cat_len * 8;
878 u32 host_cat_size = 0;
879 u32 *host_cat_array = NULL;
881 if (doi_def->type == CIPSO_V4_MAP_STD) {
882 host_cat_size = doi_def->map.std->cat.local_size;
883 host_cat_array = doi_def->map.std->cat.local;
886 for (;;) {
887 host_spot = netlbl_secattr_catmap_walk(secattr->mls_cat,
888 host_spot + 1);
889 if (host_spot < 0)
890 break;
892 switch (doi_def->type) {
893 case CIPSO_V4_MAP_PASS:
894 net_spot = host_spot;
895 break;
896 case CIPSO_V4_MAP_STD:
897 if (host_spot >= host_cat_size)
898 return -EPERM;
899 net_spot = host_cat_array[host_spot];
900 if (net_spot >= CIPSO_V4_INV_CAT)
901 return -EPERM;
902 break;
904 if (net_spot >= net_clen_bits)
905 return -ENOSPC;
906 cipso_v4_bitmap_setbit(net_cat, net_spot, 1);
908 if (net_spot > net_spot_max)
909 net_spot_max = net_spot;
912 if (++net_spot_max % 8)
913 return net_spot_max / 8 + 1;
914 return net_spot_max / 8;
918 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
919 * @doi_def: the DOI definition
920 * @net_cat: the category bitmap in network/CIPSO format
921 * @net_cat_len: the length of the CIPSO bitmap in bytes
922 * @secattr: the security attributes
924 * Description:
925 * Perform a label mapping to translate a CIPSO bitmap to the correct local
926 * MLS category bitmap using the given DOI definition. Returns zero on
927 * success, negative values on failure.
930 static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi *doi_def,
931 const unsigned char *net_cat,
932 u32 net_cat_len,
933 struct netlbl_lsm_secattr *secattr)
935 int ret_val;
936 int net_spot = -1;
937 u32 host_spot = CIPSO_V4_INV_CAT;
938 u32 net_clen_bits = net_cat_len * 8;
939 u32 net_cat_size = 0;
940 u32 *net_cat_array = NULL;
942 if (doi_def->type == CIPSO_V4_MAP_STD) {
943 net_cat_size = doi_def->map.std->cat.cipso_size;
944 net_cat_array = doi_def->map.std->cat.cipso;
947 for (;;) {
948 net_spot = cipso_v4_bitmap_walk(net_cat,
949 net_clen_bits,
950 net_spot + 1,
952 if (net_spot < 0) {
953 if (net_spot == -2)
954 return -EFAULT;
955 return 0;
958 switch (doi_def->type) {
959 case CIPSO_V4_MAP_PASS:
960 host_spot = net_spot;
961 break;
962 case CIPSO_V4_MAP_STD:
963 if (net_spot >= net_cat_size)
964 return -EPERM;
965 host_spot = net_cat_array[net_spot];
966 if (host_spot >= CIPSO_V4_INV_CAT)
967 return -EPERM;
968 break;
970 ret_val = netlbl_secattr_catmap_setbit(secattr->mls_cat,
971 host_spot,
972 GFP_ATOMIC);
973 if (ret_val != 0)
974 return ret_val;
977 return -EINVAL;
981 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
982 * @doi_def: the DOI definition
983 * @enumcat: category list
984 * @enumcat_len: length of the category list in bytes
986 * Description:
987 * Checks the given categories against the given DOI definition and returns a
988 * negative value if any of the categories do not have a valid mapping and a
989 * zero value if all of the categories are valid.
992 static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi *doi_def,
993 const unsigned char *enumcat,
994 u32 enumcat_len)
996 u16 cat;
997 int cat_prev = -1;
998 u32 iter;
1000 if (doi_def->type != CIPSO_V4_MAP_PASS || enumcat_len & 0x01)
1001 return -EFAULT;
1003 for (iter = 0; iter < enumcat_len; iter += 2) {
1004 cat = ntohs(get_unaligned((__be16 *)&enumcat[iter]));
1005 if (cat <= cat_prev)
1006 return -EFAULT;
1007 cat_prev = cat;
1010 return 0;
1014 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
1015 * @doi_def: the DOI definition
1016 * @secattr: the security attributes
1017 * @net_cat: the zero'd out category list in network/CIPSO format
1018 * @net_cat_len: the length of the CIPSO category list in bytes
1020 * Description:
1021 * Perform a label mapping to translate a local MLS category bitmap to the
1022 * correct CIPSO category list using the given DOI definition. Returns the
1023 * size in bytes of the network category bitmap on success, negative values
1024 * otherwise.
1027 static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi *doi_def,
1028 const struct netlbl_lsm_secattr *secattr,
1029 unsigned char *net_cat,
1030 u32 net_cat_len)
1032 int cat = -1;
1033 u32 cat_iter = 0;
1035 for (;;) {
1036 cat = netlbl_secattr_catmap_walk(secattr->mls_cat, cat + 1);
1037 if (cat < 0)
1038 break;
1039 if ((cat_iter + 2) > net_cat_len)
1040 return -ENOSPC;
1042 *((__be16 *)&net_cat[cat_iter]) = htons(cat);
1043 cat_iter += 2;
1046 return cat_iter;
1050 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1051 * @doi_def: the DOI definition
1052 * @net_cat: the category list in network/CIPSO format
1053 * @net_cat_len: the length of the CIPSO bitmap in bytes
1054 * @secattr: the security attributes
1056 * Description:
1057 * Perform a label mapping to translate a CIPSO category list to the correct
1058 * local MLS category bitmap using the given DOI definition. Returns zero on
1059 * success, negative values on failure.
1062 static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi *doi_def,
1063 const unsigned char *net_cat,
1064 u32 net_cat_len,
1065 struct netlbl_lsm_secattr *secattr)
1067 int ret_val;
1068 u32 iter;
1070 for (iter = 0; iter < net_cat_len; iter += 2) {
1071 ret_val = netlbl_secattr_catmap_setbit(secattr->mls_cat,
1072 ntohs(get_unaligned((__be16 *)&net_cat[iter])),
1073 GFP_ATOMIC);
1074 if (ret_val != 0)
1075 return ret_val;
1078 return 0;
1082 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1083 * @doi_def: the DOI definition
1084 * @rngcat: category list
1085 * @rngcat_len: length of the category list in bytes
1087 * Description:
1088 * Checks the given categories against the given DOI definition and returns a
1089 * negative value if any of the categories do not have a valid mapping and a
1090 * zero value if all of the categories are valid.
1093 static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi *doi_def,
1094 const unsigned char *rngcat,
1095 u32 rngcat_len)
1097 u16 cat_high;
1098 u16 cat_low;
1099 u32 cat_prev = CIPSO_V4_MAX_REM_CATS + 1;
1100 u32 iter;
1102 if (doi_def->type != CIPSO_V4_MAP_PASS || rngcat_len & 0x01)
1103 return -EFAULT;
1105 for (iter = 0; iter < rngcat_len; iter += 4) {
1106 cat_high = ntohs(get_unaligned((__be16 *)&rngcat[iter]));
1107 if ((iter + 4) <= rngcat_len)
1108 cat_low = ntohs(
1109 get_unaligned((__be16 *)&rngcat[iter + 2]));
1110 else
1111 cat_low = 0;
1113 if (cat_high > cat_prev)
1114 return -EFAULT;
1116 cat_prev = cat_low;
1119 return 0;
1123 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1124 * @doi_def: the DOI definition
1125 * @secattr: the security attributes
1126 * @net_cat: the zero'd out category list in network/CIPSO format
1127 * @net_cat_len: the length of the CIPSO category list in bytes
1129 * Description:
1130 * Perform a label mapping to translate a local MLS category bitmap to the
1131 * correct CIPSO category list using the given DOI definition. Returns the
1132 * size in bytes of the network category bitmap on success, negative values
1133 * otherwise.
1136 static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi *doi_def,
1137 const struct netlbl_lsm_secattr *secattr,
1138 unsigned char *net_cat,
1139 u32 net_cat_len)
1141 int iter = -1;
1142 u16 array[CIPSO_V4_TAG_RNG_CAT_MAX * 2];
1143 u32 array_cnt = 0;
1144 u32 cat_size = 0;
1146 /* make sure we don't overflow the 'array[]' variable */
1147 if (net_cat_len >
1148 (CIPSO_V4_OPT_LEN_MAX - CIPSO_V4_HDR_LEN - CIPSO_V4_TAG_RNG_BLEN))
1149 return -ENOSPC;
1151 for (;;) {
1152 iter = netlbl_secattr_catmap_walk(secattr->mls_cat, iter + 1);
1153 if (iter < 0)
1154 break;
1155 cat_size += (iter == 0 ? 0 : sizeof(u16));
1156 if (cat_size > net_cat_len)
1157 return -ENOSPC;
1158 array[array_cnt++] = iter;
1160 iter = netlbl_secattr_catmap_walk_rng(secattr->mls_cat, iter);
1161 if (iter < 0)
1162 return -EFAULT;
1163 cat_size += sizeof(u16);
1164 if (cat_size > net_cat_len)
1165 return -ENOSPC;
1166 array[array_cnt++] = iter;
1169 for (iter = 0; array_cnt > 0;) {
1170 *((__be16 *)&net_cat[iter]) = htons(array[--array_cnt]);
1171 iter += 2;
1172 array_cnt--;
1173 if (array[array_cnt] != 0) {
1174 *((__be16 *)&net_cat[iter]) = htons(array[array_cnt]);
1175 iter += 2;
1179 return cat_size;
1183 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1184 * @doi_def: the DOI definition
1185 * @net_cat: the category list in network/CIPSO format
1186 * @net_cat_len: the length of the CIPSO bitmap in bytes
1187 * @secattr: the security attributes
1189 * Description:
1190 * Perform a label mapping to translate a CIPSO category list to the correct
1191 * local MLS category bitmap using the given DOI definition. Returns zero on
1192 * success, negative values on failure.
1195 static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi *doi_def,
1196 const unsigned char *net_cat,
1197 u32 net_cat_len,
1198 struct netlbl_lsm_secattr *secattr)
1200 int ret_val;
1201 u32 net_iter;
1202 u16 cat_low;
1203 u16 cat_high;
1205 for (net_iter = 0; net_iter < net_cat_len; net_iter += 4) {
1206 cat_high = ntohs(get_unaligned((__be16 *)&net_cat[net_iter]));
1207 if ((net_iter + 4) <= net_cat_len)
1208 cat_low = ntohs(
1209 get_unaligned((__be16 *)&net_cat[net_iter + 2]));
1210 else
1211 cat_low = 0;
1213 ret_val = netlbl_secattr_catmap_setrng(secattr->mls_cat,
1214 cat_low,
1215 cat_high,
1216 GFP_ATOMIC);
1217 if (ret_val != 0)
1218 return ret_val;
1221 return 0;
1225 * Protocol Handling Functions
1229 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1230 * @doi_def: the DOI definition
1231 * @len: the total tag length in bytes, not including this header
1232 * @buf: the CIPSO option buffer
1234 * Description:
1235 * Write a CIPSO header into the beginning of @buffer.
1238 static void cipso_v4_gentag_hdr(const struct cipso_v4_doi *doi_def,
1239 unsigned char *buf,
1240 u32 len)
1242 buf[0] = IPOPT_CIPSO;
1243 buf[1] = CIPSO_V4_HDR_LEN + len;
1244 *(__be32 *)&buf[2] = htonl(doi_def->doi);
1248 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1249 * @doi_def: the DOI definition
1250 * @secattr: the security attributes
1251 * @buffer: the option buffer
1252 * @buffer_len: length of buffer in bytes
1254 * Description:
1255 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1256 * actual buffer length may be larger than the indicated size due to
1257 * translation between host and network category bitmaps. Returns the size of
1258 * the tag on success, negative values on failure.
1261 static int cipso_v4_gentag_rbm(const struct cipso_v4_doi *doi_def,
1262 const struct netlbl_lsm_secattr *secattr,
1263 unsigned char *buffer,
1264 u32 buffer_len)
1266 int ret_val;
1267 u32 tag_len;
1268 u32 level;
1270 if ((secattr->flags & NETLBL_SECATTR_MLS_LVL) == 0)
1271 return -EPERM;
1273 ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
1274 if (ret_val != 0)
1275 return ret_val;
1277 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1278 ret_val = cipso_v4_map_cat_rbm_hton(doi_def,
1279 secattr,
1280 &buffer[4],
1281 buffer_len - 4);
1282 if (ret_val < 0)
1283 return ret_val;
1285 /* This will send packets using the "optimized" format when
1286 * possibile as specified in section 3.4.2.6 of the
1287 * CIPSO draft. */
1288 if (cipso_v4_rbm_optfmt && ret_val > 0 && ret_val <= 10)
1289 tag_len = 14;
1290 else
1291 tag_len = 4 + ret_val;
1292 } else
1293 tag_len = 4;
1295 buffer[0] = 0x01;
1296 buffer[1] = tag_len;
1297 buffer[3] = level;
1299 return tag_len;
1303 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1304 * @doi_def: the DOI definition
1305 * @tag: the CIPSO tag
1306 * @secattr: the security attributes
1308 * Description:
1309 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1310 * attributes in @secattr. Return zero on success, negatives values on
1311 * failure.
1314 static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi *doi_def,
1315 const unsigned char *tag,
1316 struct netlbl_lsm_secattr *secattr)
1318 int ret_val;
1319 u8 tag_len = tag[1];
1320 u32 level;
1322 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1323 if (ret_val != 0)
1324 return ret_val;
1325 secattr->mls_lvl = level;
1326 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1328 if (tag_len > 4) {
1329 secattr->mls_cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1330 if (secattr->mls_cat == NULL)
1331 return -ENOMEM;
1333 ret_val = cipso_v4_map_cat_rbm_ntoh(doi_def,
1334 &tag[4],
1335 tag_len - 4,
1336 secattr);
1337 if (ret_val != 0) {
1338 netlbl_secattr_catmap_free(secattr->mls_cat);
1339 return ret_val;
1342 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1345 return 0;
1349 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1350 * @doi_def: the DOI definition
1351 * @secattr: the security attributes
1352 * @buffer: the option buffer
1353 * @buffer_len: length of buffer in bytes
1355 * Description:
1356 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1357 * size of the tag on success, negative values on failure.
1360 static int cipso_v4_gentag_enum(const struct cipso_v4_doi *doi_def,
1361 const struct netlbl_lsm_secattr *secattr,
1362 unsigned char *buffer,
1363 u32 buffer_len)
1365 int ret_val;
1366 u32 tag_len;
1367 u32 level;
1369 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1370 return -EPERM;
1372 ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
1373 if (ret_val != 0)
1374 return ret_val;
1376 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1377 ret_val = cipso_v4_map_cat_enum_hton(doi_def,
1378 secattr,
1379 &buffer[4],
1380 buffer_len - 4);
1381 if (ret_val < 0)
1382 return ret_val;
1384 tag_len = 4 + ret_val;
1385 } else
1386 tag_len = 4;
1388 buffer[0] = 0x02;
1389 buffer[1] = tag_len;
1390 buffer[3] = level;
1392 return tag_len;
1396 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1397 * @doi_def: the DOI definition
1398 * @tag: the CIPSO tag
1399 * @secattr: the security attributes
1401 * Description:
1402 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1403 * attributes in @secattr. Return zero on success, negatives values on
1404 * failure.
1407 static int cipso_v4_parsetag_enum(const struct cipso_v4_doi *doi_def,
1408 const unsigned char *tag,
1409 struct netlbl_lsm_secattr *secattr)
1411 int ret_val;
1412 u8 tag_len = tag[1];
1413 u32 level;
1415 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1416 if (ret_val != 0)
1417 return ret_val;
1418 secattr->mls_lvl = level;
1419 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1421 if (tag_len > 4) {
1422 secattr->mls_cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1423 if (secattr->mls_cat == NULL)
1424 return -ENOMEM;
1426 ret_val = cipso_v4_map_cat_enum_ntoh(doi_def,
1427 &tag[4],
1428 tag_len - 4,
1429 secattr);
1430 if (ret_val != 0) {
1431 netlbl_secattr_catmap_free(secattr->mls_cat);
1432 return ret_val;
1435 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1438 return 0;
1442 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1443 * @doi_def: the DOI definition
1444 * @secattr: the security attributes
1445 * @buffer: the option buffer
1446 * @buffer_len: length of buffer in bytes
1448 * Description:
1449 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1450 * size of the tag on success, negative values on failure.
1453 static int cipso_v4_gentag_rng(const struct cipso_v4_doi *doi_def,
1454 const struct netlbl_lsm_secattr *secattr,
1455 unsigned char *buffer,
1456 u32 buffer_len)
1458 int ret_val;
1459 u32 tag_len;
1460 u32 level;
1462 if (!(secattr->flags & NETLBL_SECATTR_MLS_LVL))
1463 return -EPERM;
1465 ret_val = cipso_v4_map_lvl_hton(doi_def, secattr->mls_lvl, &level);
1466 if (ret_val != 0)
1467 return ret_val;
1469 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
1470 ret_val = cipso_v4_map_cat_rng_hton(doi_def,
1471 secattr,
1472 &buffer[4],
1473 buffer_len - 4);
1474 if (ret_val < 0)
1475 return ret_val;
1477 tag_len = 4 + ret_val;
1478 } else
1479 tag_len = 4;
1481 buffer[0] = 0x05;
1482 buffer[1] = tag_len;
1483 buffer[3] = level;
1485 return tag_len;
1489 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1490 * @doi_def: the DOI definition
1491 * @tag: the CIPSO tag
1492 * @secattr: the security attributes
1494 * Description:
1495 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1496 * in @secattr. Return zero on success, negatives values on failure.
1499 static int cipso_v4_parsetag_rng(const struct cipso_v4_doi *doi_def,
1500 const unsigned char *tag,
1501 struct netlbl_lsm_secattr *secattr)
1503 int ret_val;
1504 u8 tag_len = tag[1];
1505 u32 level;
1507 ret_val = cipso_v4_map_lvl_ntoh(doi_def, tag[3], &level);
1508 if (ret_val != 0)
1509 return ret_val;
1510 secattr->mls_lvl = level;
1511 secattr->flags |= NETLBL_SECATTR_MLS_LVL;
1513 if (tag_len > 4) {
1514 secattr->mls_cat = netlbl_secattr_catmap_alloc(GFP_ATOMIC);
1515 if (secattr->mls_cat == NULL)
1516 return -ENOMEM;
1518 ret_val = cipso_v4_map_cat_rng_ntoh(doi_def,
1519 &tag[4],
1520 tag_len - 4,
1521 secattr);
1522 if (ret_val != 0) {
1523 netlbl_secattr_catmap_free(secattr->mls_cat);
1524 return ret_val;
1527 secattr->flags |= NETLBL_SECATTR_MLS_CAT;
1530 return 0;
1534 * cipso_v4_validate - Validate a CIPSO option
1535 * @option: the start of the option, on error it is set to point to the error
1537 * Description:
1538 * This routine is called to validate a CIPSO option, it checks all of the
1539 * fields to ensure that they are at least valid, see the draft snippet below
1540 * for details. If the option is valid then a zero value is returned and
1541 * the value of @option is unchanged. If the option is invalid then a
1542 * non-zero value is returned and @option is adjusted to point to the
1543 * offending portion of the option. From the IETF draft ...
1545 * "If any field within the CIPSO options, such as the DOI identifier, is not
1546 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1547 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1548 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1549 * that is unrecognized."
1552 int cipso_v4_validate(unsigned char **option)
1554 unsigned char *opt = *option;
1555 unsigned char *tag;
1556 unsigned char opt_iter;
1557 unsigned char err_offset = 0;
1558 u8 opt_len;
1559 u8 tag_len;
1560 struct cipso_v4_doi *doi_def = NULL;
1561 u32 tag_iter;
1563 /* caller already checks for length values that are too large */
1564 opt_len = opt[1];
1565 if (opt_len < 8) {
1566 err_offset = 1;
1567 goto validate_return;
1570 rcu_read_lock();
1571 doi_def = cipso_v4_doi_search(ntohl(get_unaligned((__be32 *)&opt[2])));
1572 if (doi_def == NULL) {
1573 err_offset = 2;
1574 goto validate_return_locked;
1577 opt_iter = 6;
1578 tag = opt + opt_iter;
1579 while (opt_iter < opt_len) {
1580 for (tag_iter = 0; doi_def->tags[tag_iter] != tag[0];)
1581 if (doi_def->tags[tag_iter] == CIPSO_V4_TAG_INVALID ||
1582 ++tag_iter == CIPSO_V4_TAG_MAXCNT) {
1583 err_offset = opt_iter;
1584 goto validate_return_locked;
1587 tag_len = tag[1];
1588 if (tag_len > (opt_len - opt_iter)) {
1589 err_offset = opt_iter + 1;
1590 goto validate_return_locked;
1593 switch (tag[0]) {
1594 case CIPSO_V4_TAG_RBITMAP:
1595 if (tag_len < 4) {
1596 err_offset = opt_iter + 1;
1597 goto validate_return_locked;
1600 /* We are already going to do all the verification
1601 * necessary at the socket layer so from our point of
1602 * view it is safe to turn these checks off (and less
1603 * work), however, the CIPSO draft says we should do
1604 * all the CIPSO validations here but it doesn't
1605 * really specify _exactly_ what we need to validate
1606 * ... so, just make it a sysctl tunable. */
1607 if (cipso_v4_rbm_strictvalid) {
1608 if (cipso_v4_map_lvl_valid(doi_def,
1609 tag[3]) < 0) {
1610 err_offset = opt_iter + 3;
1611 goto validate_return_locked;
1613 if (tag_len > 4 &&
1614 cipso_v4_map_cat_rbm_valid(doi_def,
1615 &tag[4],
1616 tag_len - 4) < 0) {
1617 err_offset = opt_iter + 4;
1618 goto validate_return_locked;
1621 break;
1622 case CIPSO_V4_TAG_ENUM:
1623 if (tag_len < 4) {
1624 err_offset = opt_iter + 1;
1625 goto validate_return_locked;
1628 if (cipso_v4_map_lvl_valid(doi_def,
1629 tag[3]) < 0) {
1630 err_offset = opt_iter + 3;
1631 goto validate_return_locked;
1633 if (tag_len > 4 &&
1634 cipso_v4_map_cat_enum_valid(doi_def,
1635 &tag[4],
1636 tag_len - 4) < 0) {
1637 err_offset = opt_iter + 4;
1638 goto validate_return_locked;
1640 break;
1641 case CIPSO_V4_TAG_RANGE:
1642 if (tag_len < 4) {
1643 err_offset = opt_iter + 1;
1644 goto validate_return_locked;
1647 if (cipso_v4_map_lvl_valid(doi_def,
1648 tag[3]) < 0) {
1649 err_offset = opt_iter + 3;
1650 goto validate_return_locked;
1652 if (tag_len > 4 &&
1653 cipso_v4_map_cat_rng_valid(doi_def,
1654 &tag[4],
1655 tag_len - 4) < 0) {
1656 err_offset = opt_iter + 4;
1657 goto validate_return_locked;
1659 break;
1660 default:
1661 err_offset = opt_iter;
1662 goto validate_return_locked;
1665 tag += tag_len;
1666 opt_iter += tag_len;
1669 validate_return_locked:
1670 rcu_read_unlock();
1671 validate_return:
1672 *option = opt + err_offset;
1673 return err_offset;
1677 * cipso_v4_error - Send the correct reponse for a bad packet
1678 * @skb: the packet
1679 * @error: the error code
1680 * @gateway: CIPSO gateway flag
1682 * Description:
1683 * Based on the error code given in @error, send an ICMP error message back to
1684 * the originating host. From the IETF draft ...
1686 * "If the contents of the CIPSO [option] are valid but the security label is
1687 * outside of the configured host or port label range, the datagram is
1688 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1689 * returned. The code field of the ICMP is set to 'communication with
1690 * destination network administratively prohibited' (code 9) or to
1691 * 'communication with destination host administratively prohibited'
1692 * (code 10). The value of the code is dependent on whether the originator
1693 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1694 * recipient of the ICMP message MUST be able to handle either value. The
1695 * same procedure is performed if a CIPSO [option] can not be added to an
1696 * IP packet because it is too large to fit in the IP options area."
1698 * "If the error is triggered by receipt of an ICMP message, the message is
1699 * discarded and no response is permitted (consistent with general ICMP
1700 * processing rules)."
1703 void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway)
1705 if (ip_hdr(skb)->protocol == IPPROTO_ICMP || error != -EACCES)
1706 return;
1708 if (gateway)
1709 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_NET_ANO, 0);
1710 else
1711 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_ANO, 0);
1715 * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1716 * @sk: the socket
1717 * @doi_def: the CIPSO DOI to use
1718 * @secattr: the specific security attributes of the socket
1720 * Description:
1721 * Set the CIPSO option on the given socket using the DOI definition and
1722 * security attributes passed to the function. This function requires
1723 * exclusive access to @sk, which means it either needs to be in the
1724 * process of being created or locked. Returns zero on success and negative
1725 * values on failure.
1728 int cipso_v4_sock_setattr(struct sock *sk,
1729 const struct cipso_v4_doi *doi_def,
1730 const struct netlbl_lsm_secattr *secattr)
1732 int ret_val = -EPERM;
1733 u32 iter;
1734 unsigned char *buf;
1735 u32 buf_len = 0;
1736 u32 opt_len;
1737 struct ip_options *opt = NULL;
1738 struct inet_sock *sk_inet;
1739 struct inet_connection_sock *sk_conn;
1741 /* In the case of sock_create_lite(), the sock->sk field is not
1742 * defined yet but it is not a problem as the only users of these
1743 * "lite" PF_INET sockets are functions which do an accept() call
1744 * afterwards so we will label the socket as part of the accept(). */
1745 if (sk == NULL)
1746 return 0;
1748 /* We allocate the maximum CIPSO option size here so we are probably
1749 * being a little wasteful, but it makes our life _much_ easier later
1750 * on and after all we are only talking about 40 bytes. */
1751 buf_len = CIPSO_V4_OPT_LEN_MAX;
1752 buf = kmalloc(buf_len, GFP_ATOMIC);
1753 if (buf == NULL) {
1754 ret_val = -ENOMEM;
1755 goto socket_setattr_failure;
1758 /* XXX - This code assumes only one tag per CIPSO option which isn't
1759 * really a good assumption to make but since we only support the MAC
1760 * tags right now it is a safe assumption. */
1761 iter = 0;
1762 do {
1763 memset(buf, 0, buf_len);
1764 switch (doi_def->tags[iter]) {
1765 case CIPSO_V4_TAG_RBITMAP:
1766 ret_val = cipso_v4_gentag_rbm(doi_def,
1767 secattr,
1768 &buf[CIPSO_V4_HDR_LEN],
1769 buf_len - CIPSO_V4_HDR_LEN);
1770 break;
1771 case CIPSO_V4_TAG_ENUM:
1772 ret_val = cipso_v4_gentag_enum(doi_def,
1773 secattr,
1774 &buf[CIPSO_V4_HDR_LEN],
1775 buf_len - CIPSO_V4_HDR_LEN);
1776 break;
1777 case CIPSO_V4_TAG_RANGE:
1778 ret_val = cipso_v4_gentag_rng(doi_def,
1779 secattr,
1780 &buf[CIPSO_V4_HDR_LEN],
1781 buf_len - CIPSO_V4_HDR_LEN);
1782 break;
1783 default:
1784 ret_val = -EPERM;
1785 goto socket_setattr_failure;
1788 iter++;
1789 } while (ret_val < 0 &&
1790 iter < CIPSO_V4_TAG_MAXCNT &&
1791 doi_def->tags[iter] != CIPSO_V4_TAG_INVALID);
1792 if (ret_val < 0)
1793 goto socket_setattr_failure;
1794 cipso_v4_gentag_hdr(doi_def, buf, ret_val);
1795 buf_len = CIPSO_V4_HDR_LEN + ret_val;
1797 /* We can't use ip_options_get() directly because it makes a call to
1798 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1799 * we won't always have CAP_NET_RAW even though we _always_ want to
1800 * set the IPOPT_CIPSO option. */
1801 opt_len = (buf_len + 3) & ~3;
1802 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1803 if (opt == NULL) {
1804 ret_val = -ENOMEM;
1805 goto socket_setattr_failure;
1807 memcpy(opt->__data, buf, buf_len);
1808 opt->optlen = opt_len;
1809 opt->is_data = 1;
1810 opt->cipso = sizeof(struct iphdr);
1811 kfree(buf);
1812 buf = NULL;
1814 sk_inet = inet_sk(sk);
1815 if (sk_inet->is_icsk) {
1816 sk_conn = inet_csk(sk);
1817 if (sk_inet->opt)
1818 sk_conn->icsk_ext_hdr_len -= sk_inet->opt->optlen;
1819 sk_conn->icsk_ext_hdr_len += opt->optlen;
1820 sk_conn->icsk_sync_mss(sk, sk_conn->icsk_pmtu_cookie);
1822 opt = xchg(&sk_inet->opt, opt);
1823 kfree(opt);
1825 return 0;
1827 socket_setattr_failure:
1828 kfree(buf);
1829 kfree(opt);
1830 return ret_val;
1834 * cipso_v4_sock_getattr - Get the security attributes from a sock
1835 * @sk: the sock
1836 * @secattr: the security attributes
1838 * Description:
1839 * Query @sk to see if there is a CIPSO option attached to the sock and if
1840 * there is return the CIPSO security attributes in @secattr. This function
1841 * requires that @sk be locked, or privately held, but it does not do any
1842 * locking itself. Returns zero on success and negative values on failure.
1845 int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
1847 int ret_val = -ENOMSG;
1848 struct inet_sock *sk_inet;
1849 unsigned char *cipso_ptr;
1850 u32 doi;
1851 struct cipso_v4_doi *doi_def;
1853 sk_inet = inet_sk(sk);
1854 if (sk_inet->opt == NULL || sk_inet->opt->cipso == 0)
1855 return -ENOMSG;
1856 cipso_ptr = sk_inet->opt->__data + sk_inet->opt->cipso -
1857 sizeof(struct iphdr);
1858 ret_val = cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr);
1859 if (ret_val == 0)
1860 return ret_val;
1862 doi = ntohl(get_unaligned((__be32 *)&cipso_ptr[2]));
1863 rcu_read_lock();
1864 doi_def = cipso_v4_doi_search(doi);
1865 if (doi_def == NULL) {
1866 rcu_read_unlock();
1867 return -ENOMSG;
1870 /* XXX - This code assumes only one tag per CIPSO option which isn't
1871 * really a good assumption to make but since we only support the MAC
1872 * tags right now it is a safe assumption. */
1873 switch (cipso_ptr[6]) {
1874 case CIPSO_V4_TAG_RBITMAP:
1875 ret_val = cipso_v4_parsetag_rbm(doi_def,
1876 &cipso_ptr[6],
1877 secattr);
1878 break;
1879 case CIPSO_V4_TAG_ENUM:
1880 ret_val = cipso_v4_parsetag_enum(doi_def,
1881 &cipso_ptr[6],
1882 secattr);
1883 break;
1884 case CIPSO_V4_TAG_RANGE:
1885 ret_val = cipso_v4_parsetag_rng(doi_def,
1886 &cipso_ptr[6],
1887 secattr);
1888 break;
1890 rcu_read_unlock();
1892 return ret_val;
1896 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
1897 * @skb: the packet
1898 * @secattr: the security attributes
1900 * Description:
1901 * Parse the given packet's CIPSO option and return the security attributes.
1902 * Returns zero on success and negative values on failure.
1905 int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
1906 struct netlbl_lsm_secattr *secattr)
1908 int ret_val = -ENOMSG;
1909 unsigned char *cipso_ptr;
1910 u32 doi;
1911 struct cipso_v4_doi *doi_def;
1913 cipso_ptr = CIPSO_V4_OPTPTR(skb);
1914 if (cipso_v4_cache_check(cipso_ptr, cipso_ptr[1], secattr) == 0)
1915 return 0;
1917 doi = ntohl(get_unaligned((__be32 *)&cipso_ptr[2]));
1918 rcu_read_lock();
1919 doi_def = cipso_v4_doi_search(doi);
1920 if (doi_def == NULL)
1921 goto skbuff_getattr_return;
1923 /* XXX - This code assumes only one tag per CIPSO option which isn't
1924 * really a good assumption to make but since we only support the MAC
1925 * tags right now it is a safe assumption. */
1926 switch (cipso_ptr[6]) {
1927 case CIPSO_V4_TAG_RBITMAP:
1928 ret_val = cipso_v4_parsetag_rbm(doi_def,
1929 &cipso_ptr[6],
1930 secattr);
1931 break;
1932 case CIPSO_V4_TAG_ENUM:
1933 ret_val = cipso_v4_parsetag_enum(doi_def,
1934 &cipso_ptr[6],
1935 secattr);
1936 break;
1937 case CIPSO_V4_TAG_RANGE:
1938 ret_val = cipso_v4_parsetag_rng(doi_def,
1939 &cipso_ptr[6],
1940 secattr);
1941 break;
1944 skbuff_getattr_return:
1945 rcu_read_unlock();
1946 return ret_val;
1950 * Setup Functions
1954 * cipso_v4_init - Initialize the CIPSO module
1956 * Description:
1957 * Initialize the CIPSO module and prepare it for use. Returns zero on success
1958 * and negative values on failure.
1961 static int __init cipso_v4_init(void)
1963 int ret_val;
1965 ret_val = cipso_v4_cache_init();
1966 if (ret_val != 0)
1967 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
1968 ret_val);
1970 return 0;
1973 subsys_initcall(cipso_v4_init);