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>
44 #include <net/netlabel.h>
45 #include <net/cipso_ipv4.h>
46 #include <asm/atomic.h>
49 struct cipso_v4_domhsh_entry
{
52 struct list_head list
;
56 /* List of available DOI definitions */
57 /* XXX - Updates should be minimal so having a single lock for the
58 * cipso_v4_doi_list and the cipso_v4_doi_list->dom_list should be
60 /* XXX - This currently assumes a minimal number of different DOIs in use,
61 * if in practice there are a lot of different DOIs this list should
62 * probably be turned into a hash table or something similar so we
63 * can do quick lookups. */
64 static DEFINE_SPINLOCK(cipso_v4_doi_list_lock
);
65 static struct list_head cipso_v4_doi_list
= LIST_HEAD_INIT(cipso_v4_doi_list
);
67 /* Label mapping cache */
68 int cipso_v4_cache_enabled
= 1;
69 int cipso_v4_cache_bucketsize
= 10;
70 #define CIPSO_V4_CACHE_BUCKETBITS 7
71 #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
72 #define CIPSO_V4_CACHE_REORDERLIMIT 10
73 struct cipso_v4_map_cache_bkt
{
76 struct list_head list
;
78 struct cipso_v4_map_cache_entry
{
83 struct netlbl_lsm_cache
*lsm_data
;
86 struct list_head list
;
88 static struct cipso_v4_map_cache_bkt
*cipso_v4_cache
= NULL
;
90 /* Restricted bitmap (tag #1) flags */
91 int cipso_v4_rbm_optfmt
= 0;
92 int cipso_v4_rbm_strictvalid
= 1;
99 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
100 * @bitmap: the bitmap
101 * @bitmap_len: length in bits
102 * @offset: starting offset
103 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
106 * Starting at @offset, walk the bitmap from left to right until either the
107 * desired bit is found or we reach the end. Return the bit offset, -1 if
108 * not found, or -2 if error.
110 static int cipso_v4_bitmap_walk(const unsigned char *bitmap
,
117 unsigned char bitmask
;
120 /* gcc always rounds to zero when doing integer division */
121 byte_offset
= offset
/ 8;
122 byte
= bitmap
[byte_offset
];
124 bitmask
= 0x80 >> (offset
% 8);
126 while (bit_spot
< bitmap_len
) {
127 if ((state
&& (byte
& bitmask
) == bitmask
) ||
128 (state
== 0 && (byte
& bitmask
) == 0))
134 byte
= bitmap
[++byte_offset
];
143 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
144 * @bitmap: the bitmap
146 * @state: if non-zero, set the bit (1) else clear the bit (0)
149 * Set a single bit in the bitmask. Returns zero on success, negative values
152 static void cipso_v4_bitmap_setbit(unsigned char *bitmap
,
159 /* gcc always rounds to zero when doing integer division */
161 bitmask
= 0x80 >> (bit
% 8);
163 bitmap
[byte_spot
] |= bitmask
;
165 bitmap
[byte_spot
] &= ~bitmask
;
169 * cipso_v4_doi_domhsh_free - Frees a domain list entry
170 * @entry: the entry's RCU field
173 * This function is designed to be used as a callback to the call_rcu()
174 * function so that the memory allocated to a domain list entry can be released
178 static void cipso_v4_doi_domhsh_free(struct rcu_head
*entry
)
180 struct cipso_v4_domhsh_entry
*ptr
;
182 ptr
= container_of(entry
, struct cipso_v4_domhsh_entry
, rcu
);
188 * cipso_v4_cache_entry_free - Frees a cache entry
189 * @entry: the entry to free
192 * This function frees the memory associated with a cache entry including the
193 * LSM cache data if there are no longer any users, i.e. reference count == 0.
196 static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry
*entry
)
199 netlbl_secattr_cache_free(entry
->lsm_data
);
205 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
207 * @key_len: the length of the key in bytes
210 * The CIPSO tag hashing function. Returns a 32-bit hash value.
213 static u32
cipso_v4_map_cache_hash(const unsigned char *key
, u32 key_len
)
215 return jhash(key
, key_len
, 0);
219 * Label Mapping Cache Functions
223 * cipso_v4_cache_init - Initialize the CIPSO cache
226 * Initializes the CIPSO label mapping cache, this function should be called
227 * before any of the other functions defined in this file. Returns zero on
228 * success, negative values on error.
231 static int cipso_v4_cache_init(void)
235 cipso_v4_cache
= kcalloc(CIPSO_V4_CACHE_BUCKETS
,
236 sizeof(struct cipso_v4_map_cache_bkt
),
238 if (cipso_v4_cache
== NULL
)
241 for (iter
= 0; iter
< CIPSO_V4_CACHE_BUCKETS
; iter
++) {
242 spin_lock_init(&cipso_v4_cache
[iter
].lock
);
243 cipso_v4_cache
[iter
].size
= 0;
244 INIT_LIST_HEAD(&cipso_v4_cache
[iter
].list
);
251 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
254 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
255 * success and negative values on failure.
258 void cipso_v4_cache_invalidate(void)
260 struct cipso_v4_map_cache_entry
*entry
, *tmp_entry
;
263 for (iter
= 0; iter
< CIPSO_V4_CACHE_BUCKETS
; iter
++) {
264 spin_lock_bh(&cipso_v4_cache
[iter
].lock
);
265 list_for_each_entry_safe(entry
,
267 &cipso_v4_cache
[iter
].list
, list
) {
268 list_del(&entry
->list
);
269 cipso_v4_cache_entry_free(entry
);
271 cipso_v4_cache
[iter
].size
= 0;
272 spin_unlock_bh(&cipso_v4_cache
[iter
].lock
);
279 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
280 * @key: the buffer to check
281 * @key_len: buffer length in bytes
282 * @secattr: the security attribute struct to use
285 * This function checks the cache to see if a label mapping already exists for
286 * the given key. If there is a match then the cache is adjusted and the
287 * @secattr struct is populated with the correct LSM security attributes. The
288 * cache is adjusted in the following manner if the entry is not already the
289 * first in the cache bucket:
291 * 1. The cache entry's activity counter is incremented
292 * 2. The previous (higher ranking) entry's activity counter is decremented
293 * 3. If the difference between the two activity counters is geater than
294 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
296 * Returns zero on success, -ENOENT for a cache miss, and other negative values
300 static int cipso_v4_cache_check(const unsigned char *key
,
302 struct netlbl_lsm_secattr
*secattr
)
305 struct cipso_v4_map_cache_entry
*entry
;
306 struct cipso_v4_map_cache_entry
*prev_entry
= NULL
;
309 if (!cipso_v4_cache_enabled
)
312 hash
= cipso_v4_map_cache_hash(key
, key_len
);
313 bkt
= hash
& (CIPSO_V4_CACHE_BUCKETBITS
- 1);
314 spin_lock_bh(&cipso_v4_cache
[bkt
].lock
);
315 list_for_each_entry(entry
, &cipso_v4_cache
[bkt
].list
, list
) {
316 if (entry
->hash
== hash
&&
317 entry
->key_len
== key_len
&&
318 memcmp(entry
->key
, key
, key_len
) == 0) {
319 entry
->activity
+= 1;
320 atomic_inc(&entry
->lsm_data
->refcount
);
321 secattr
->cache
= entry
->lsm_data
;
322 secattr
->flags
|= NETLBL_SECATTR_CACHE
;
323 if (prev_entry
== NULL
) {
324 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
328 if (prev_entry
->activity
> 0)
329 prev_entry
->activity
-= 1;
330 if (entry
->activity
> prev_entry
->activity
&&
331 entry
->activity
- prev_entry
->activity
>
332 CIPSO_V4_CACHE_REORDERLIMIT
) {
333 __list_del(entry
->list
.prev
, entry
->list
.next
);
334 __list_add(&entry
->list
,
335 prev_entry
->list
.prev
,
339 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
344 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
350 * cipso_v4_cache_add - Add an entry to the CIPSO cache
352 * @secattr: the packet's security attributes
355 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
356 * head of the cache bucket's list, if the cache bucket is out of room remove
357 * the last entry in the list first. It is important to note that there is
358 * currently no checking for duplicate keys. Returns zero on success,
359 * negative values on failure.
362 int cipso_v4_cache_add(const struct sk_buff
*skb
,
363 const struct netlbl_lsm_secattr
*secattr
)
365 int ret_val
= -EPERM
;
367 struct cipso_v4_map_cache_entry
*entry
= NULL
;
368 struct cipso_v4_map_cache_entry
*old_entry
= NULL
;
369 unsigned char *cipso_ptr
;
372 if (!cipso_v4_cache_enabled
|| cipso_v4_cache_bucketsize
<= 0)
375 cipso_ptr
= CIPSO_V4_OPTPTR(skb
);
376 cipso_ptr_len
= cipso_ptr
[1];
378 entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
381 entry
->key
= kmemdup(cipso_ptr
, cipso_ptr_len
, GFP_ATOMIC
);
382 if (entry
->key
== NULL
) {
384 goto cache_add_failure
;
386 entry
->key_len
= cipso_ptr_len
;
387 entry
->hash
= cipso_v4_map_cache_hash(cipso_ptr
, cipso_ptr_len
);
388 atomic_inc(&secattr
->cache
->refcount
);
389 entry
->lsm_data
= secattr
->cache
;
391 bkt
= entry
->hash
& (CIPSO_V4_CACHE_BUCKETBITS
- 1);
392 spin_lock_bh(&cipso_v4_cache
[bkt
].lock
);
393 if (cipso_v4_cache
[bkt
].size
< cipso_v4_cache_bucketsize
) {
394 list_add(&entry
->list
, &cipso_v4_cache
[bkt
].list
);
395 cipso_v4_cache
[bkt
].size
+= 1;
397 old_entry
= list_entry(cipso_v4_cache
[bkt
].list
.prev
,
398 struct cipso_v4_map_cache_entry
, list
);
399 list_del(&old_entry
->list
);
400 list_add(&entry
->list
, &cipso_v4_cache
[bkt
].list
);
401 cipso_v4_cache_entry_free(old_entry
);
403 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
409 cipso_v4_cache_entry_free(entry
);
418 * cipso_v4_doi_search - Searches for a DOI definition
419 * @doi: the DOI to search for
422 * Search the DOI definition list for a DOI definition with a DOI value that
423 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
424 * Returns a pointer to the DOI definition on success and NULL on failure.
426 static struct cipso_v4_doi
*cipso_v4_doi_search(u32 doi
)
428 struct cipso_v4_doi
*iter
;
430 list_for_each_entry_rcu(iter
, &cipso_v4_doi_list
, list
)
431 if (iter
->doi
== doi
&& iter
->valid
)
437 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
438 * @doi_def: the DOI structure
441 * The caller defines a new DOI for use by the CIPSO engine and calls this
442 * function to add it to the list of acceptable domains. The caller must
443 * ensure that the mapping table specified in @doi_def->map meets all of the
444 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
445 * zero on success and non-zero on failure.
448 int cipso_v4_doi_add(struct cipso_v4_doi
*doi_def
)
452 if (doi_def
== NULL
|| doi_def
->doi
== CIPSO_V4_DOI_UNKNOWN
)
454 for (iter
= 0; iter
< CIPSO_V4_TAG_MAXCNT
; iter
++) {
455 switch (doi_def
->tags
[iter
]) {
456 case CIPSO_V4_TAG_RBITMAP
:
458 case CIPSO_V4_TAG_INVALID
:
462 case CIPSO_V4_TAG_ENUM
:
463 if (doi_def
->type
!= CIPSO_V4_MAP_PASS
)
472 INIT_RCU_HEAD(&doi_def
->rcu
);
473 INIT_LIST_HEAD(&doi_def
->dom_list
);
476 if (cipso_v4_doi_search(doi_def
->doi
) != NULL
)
477 goto doi_add_failure_rlock
;
478 spin_lock(&cipso_v4_doi_list_lock
);
479 if (cipso_v4_doi_search(doi_def
->doi
) != NULL
)
480 goto doi_add_failure_slock
;
481 list_add_tail_rcu(&doi_def
->list
, &cipso_v4_doi_list
);
482 spin_unlock(&cipso_v4_doi_list_lock
);
487 doi_add_failure_slock
:
488 spin_unlock(&cipso_v4_doi_list_lock
);
489 doi_add_failure_rlock
:
495 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
496 * @doi: the DOI value
497 * @audit_secid: the LSM secid to use in the audit message
498 * @callback: the DOI cleanup/free callback
501 * Removes a DOI definition from the CIPSO engine, @callback is called to
502 * free any memory. The NetLabel routines will be called to release their own
503 * LSM domain mappings as well as our own domain list. Returns zero on
504 * success and negative values on failure.
507 int cipso_v4_doi_remove(u32 doi
,
508 struct netlbl_audit
*audit_info
,
509 void (*callback
) (struct rcu_head
* head
))
511 struct cipso_v4_doi
*doi_def
;
512 struct cipso_v4_domhsh_entry
*dom_iter
;
515 if (cipso_v4_doi_search(doi
) != NULL
) {
516 spin_lock(&cipso_v4_doi_list_lock
);
517 doi_def
= cipso_v4_doi_search(doi
);
518 if (doi_def
== NULL
) {
519 spin_unlock(&cipso_v4_doi_list_lock
);
524 list_del_rcu(&doi_def
->list
);
525 spin_unlock(&cipso_v4_doi_list_lock
);
526 list_for_each_entry_rcu(dom_iter
, &doi_def
->dom_list
, list
)
528 netlbl_domhsh_remove(dom_iter
->domain
,
530 cipso_v4_cache_invalidate();
533 call_rcu(&doi_def
->rcu
, callback
);
542 * cipso_v4_doi_getdef - Returns a pointer to a valid DOI definition
543 * @doi: the DOI value
546 * Searches for a valid DOI definition and if one is found it is returned to
547 * the caller. Otherwise NULL is returned. The caller must ensure that
548 * rcu_read_lock() is held while accessing the returned definition.
551 struct cipso_v4_doi
*cipso_v4_doi_getdef(u32 doi
)
553 return cipso_v4_doi_search(doi
);
557 * cipso_v4_doi_walk - Iterate through the DOI definitions
558 * @skip_cnt: skip past this number of DOI definitions, updated
559 * @callback: callback for each DOI definition
560 * @cb_arg: argument for the callback function
563 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
564 * For each entry call @callback, if @callback returns a negative value stop
565 * 'walking' through the list and return. Updates the value in @skip_cnt upon
566 * return. Returns zero on success, negative values on failure.
569 int cipso_v4_doi_walk(u32
*skip_cnt
,
570 int (*callback
) (struct cipso_v4_doi
*doi_def
, void *arg
),
573 int ret_val
= -ENOENT
;
575 struct cipso_v4_doi
*iter_doi
;
578 list_for_each_entry_rcu(iter_doi
, &cipso_v4_doi_list
, list
)
579 if (iter_doi
->valid
) {
580 if (doi_cnt
++ < *skip_cnt
)
582 ret_val
= callback(iter_doi
, cb_arg
);
585 goto doi_walk_return
;
596 * cipso_v4_doi_domhsh_add - Adds a domain entry to a DOI definition
597 * @doi_def: the DOI definition
598 * @domain: the domain to add
601 * Adds the @domain to the the DOI specified by @doi_def, this function
602 * should only be called by external functions (i.e. NetLabel). This function
603 * does allocate memory. Returns zero on success, negative values on failure.
606 int cipso_v4_doi_domhsh_add(struct cipso_v4_doi
*doi_def
, const char *domain
)
608 struct cipso_v4_domhsh_entry
*iter
;
609 struct cipso_v4_domhsh_entry
*new_dom
;
611 new_dom
= kzalloc(sizeof(*new_dom
), GFP_KERNEL
);
615 new_dom
->domain
= kstrdup(domain
, GFP_KERNEL
);
616 if (new_dom
->domain
== NULL
) {
622 INIT_RCU_HEAD(&new_dom
->rcu
);
625 spin_lock(&cipso_v4_doi_list_lock
);
626 list_for_each_entry_rcu(iter
, &doi_def
->dom_list
, list
)
628 ((domain
!= NULL
&& iter
->domain
!= NULL
&&
629 strcmp(iter
->domain
, domain
) == 0) ||
630 (domain
== NULL
&& iter
->domain
== NULL
))) {
631 spin_unlock(&cipso_v4_doi_list_lock
);
633 kfree(new_dom
->domain
);
637 list_add_tail_rcu(&new_dom
->list
, &doi_def
->dom_list
);
638 spin_unlock(&cipso_v4_doi_list_lock
);
645 * cipso_v4_doi_domhsh_remove - Removes a domain entry from a DOI definition
646 * @doi_def: the DOI definition
647 * @domain: the domain to remove
650 * Removes the @domain from the DOI specified by @doi_def, this function
651 * should only be called by external functions (i.e. NetLabel). Returns zero
652 * on success and negative values on error.
655 int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi
*doi_def
,
658 struct cipso_v4_domhsh_entry
*iter
;
661 spin_lock(&cipso_v4_doi_list_lock
);
662 list_for_each_entry_rcu(iter
, &doi_def
->dom_list
, list
)
664 ((domain
!= NULL
&& iter
->domain
!= NULL
&&
665 strcmp(iter
->domain
, domain
) == 0) ||
666 (domain
== NULL
&& iter
->domain
== NULL
))) {
668 list_del_rcu(&iter
->list
);
669 spin_unlock(&cipso_v4_doi_list_lock
);
671 call_rcu(&iter
->rcu
, cipso_v4_doi_domhsh_free
);
675 spin_unlock(&cipso_v4_doi_list_lock
);
682 * Label Mapping Functions
686 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
687 * @doi_def: the DOI definition
688 * @level: the level to check
691 * Checks the given level against the given DOI definition and returns a
692 * negative value if the level does not have a valid mapping and a zero value
693 * if the level is defined by the DOI.
696 static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi
*doi_def
, u8 level
)
698 switch (doi_def
->type
) {
699 case CIPSO_V4_MAP_PASS
:
701 case CIPSO_V4_MAP_STD
:
702 if (doi_def
->map
.std
->lvl
.cipso
[level
] < CIPSO_V4_INV_LVL
)
711 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
712 * @doi_def: the DOI definition
713 * @host_lvl: the host MLS level
714 * @net_lvl: the network/CIPSO MLS level
717 * Perform a label mapping to translate a local MLS level to the correct
718 * CIPSO level using the given DOI definition. Returns zero on success,
719 * negative values otherwise.
722 static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi
*doi_def
,
726 switch (doi_def
->type
) {
727 case CIPSO_V4_MAP_PASS
:
730 case CIPSO_V4_MAP_STD
:
731 if (host_lvl
< doi_def
->map
.std
->lvl
.local_size
) {
732 *net_lvl
= doi_def
->map
.std
->lvl
.local
[host_lvl
];
742 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
743 * @doi_def: the DOI definition
744 * @net_lvl: the network/CIPSO MLS level
745 * @host_lvl: the host MLS level
748 * Perform a label mapping to translate a CIPSO level to the correct local MLS
749 * level using the given DOI definition. Returns zero on success, negative
753 static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi
*doi_def
,
757 struct cipso_v4_std_map_tbl
*map_tbl
;
759 switch (doi_def
->type
) {
760 case CIPSO_V4_MAP_PASS
:
763 case CIPSO_V4_MAP_STD
:
764 map_tbl
= doi_def
->map
.std
;
765 if (net_lvl
< map_tbl
->lvl
.cipso_size
&&
766 map_tbl
->lvl
.cipso
[net_lvl
] < CIPSO_V4_INV_LVL
) {
767 *host_lvl
= doi_def
->map
.std
->lvl
.cipso
[net_lvl
];
777 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
778 * @doi_def: the DOI definition
779 * @bitmap: category bitmap
780 * @bitmap_len: bitmap length in bytes
783 * Checks the given category bitmap against the given DOI definition and
784 * returns a negative value if any of the categories in the bitmap do not have
785 * a valid mapping and a zero value if all of the categories are valid.
788 static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi
*doi_def
,
789 const unsigned char *bitmap
,
793 u32 bitmap_len_bits
= bitmap_len
* 8;
797 switch (doi_def
->type
) {
798 case CIPSO_V4_MAP_PASS
:
800 case CIPSO_V4_MAP_STD
:
801 cipso_cat_size
= doi_def
->map
.std
->cat
.cipso_size
;
802 cipso_array
= doi_def
->map
.std
->cat
.cipso
;
804 cat
= cipso_v4_bitmap_walk(bitmap
,
810 if (cat
>= cipso_cat_size
||
811 cipso_array
[cat
] >= CIPSO_V4_INV_CAT
)
824 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
825 * @doi_def: the DOI definition
826 * @secattr: the security attributes
827 * @net_cat: the zero'd out category bitmap in network/CIPSO format
828 * @net_cat_len: the length of the CIPSO bitmap in bytes
831 * Perform a label mapping to translate a local MLS category bitmap to the
832 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
833 * size in bytes of the network bitmap on success, negative values otherwise.
836 static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi
*doi_def
,
837 const struct netlbl_lsm_secattr
*secattr
,
838 unsigned char *net_cat
,
842 u32 net_spot
= CIPSO_V4_INV_CAT
;
843 u32 net_spot_max
= 0;
844 u32 net_clen_bits
= net_cat_len
* 8;
845 u32 host_cat_size
= 0;
846 u32
*host_cat_array
= NULL
;
848 if (doi_def
->type
== CIPSO_V4_MAP_STD
) {
849 host_cat_size
= doi_def
->map
.std
->cat
.local_size
;
850 host_cat_array
= doi_def
->map
.std
->cat
.local
;
854 host_spot
= netlbl_secattr_catmap_walk(secattr
->mls_cat
,
859 switch (doi_def
->type
) {
860 case CIPSO_V4_MAP_PASS
:
861 net_spot
= host_spot
;
863 case CIPSO_V4_MAP_STD
:
864 if (host_spot
>= host_cat_size
)
866 net_spot
= host_cat_array
[host_spot
];
867 if (net_spot
>= CIPSO_V4_INV_CAT
)
871 if (net_spot
>= net_clen_bits
)
873 cipso_v4_bitmap_setbit(net_cat
, net_spot
, 1);
875 if (net_spot
> net_spot_max
)
876 net_spot_max
= net_spot
;
879 if (++net_spot_max
% 8)
880 return net_spot_max
/ 8 + 1;
881 return net_spot_max
/ 8;
885 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
886 * @doi_def: the DOI definition
887 * @net_cat: the category bitmap in network/CIPSO format
888 * @net_cat_len: the length of the CIPSO bitmap in bytes
889 * @secattr: the security attributes
892 * Perform a label mapping to translate a CIPSO bitmap to the correct local
893 * MLS category bitmap using the given DOI definition. Returns zero on
894 * success, negative values on failure.
897 static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi
*doi_def
,
898 const unsigned char *net_cat
,
900 struct netlbl_lsm_secattr
*secattr
)
904 u32 host_spot
= CIPSO_V4_INV_CAT
;
905 u32 net_clen_bits
= net_cat_len
* 8;
906 u32 net_cat_size
= 0;
907 u32
*net_cat_array
= NULL
;
909 if (doi_def
->type
== CIPSO_V4_MAP_STD
) {
910 net_cat_size
= doi_def
->map
.std
->cat
.cipso_size
;
911 net_cat_array
= doi_def
->map
.std
->cat
.cipso
;
915 net_spot
= cipso_v4_bitmap_walk(net_cat
,
925 switch (doi_def
->type
) {
926 case CIPSO_V4_MAP_PASS
:
927 host_spot
= net_spot
;
929 case CIPSO_V4_MAP_STD
:
930 if (net_spot
>= net_cat_size
)
932 host_spot
= net_cat_array
[net_spot
];
933 if (host_spot
>= CIPSO_V4_INV_CAT
)
937 ret_val
= netlbl_secattr_catmap_setbit(secattr
->mls_cat
,
948 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
949 * @doi_def: the DOI definition
950 * @enumcat: category list
951 * @enumcat_len: length of the category list in bytes
954 * Checks the given categories against the given DOI definition and returns a
955 * negative value if any of the categories do not have a valid mapping and a
956 * zero value if all of the categories are valid.
959 static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi
*doi_def
,
960 const unsigned char *enumcat
,
967 if (doi_def
->type
!= CIPSO_V4_MAP_PASS
|| enumcat_len
& 0x01)
970 for (iter
= 0; iter
< enumcat_len
; iter
+= 2) {
971 cat
= ntohs(*((__be16
*)&enumcat
[iter
]));
981 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
982 * @doi_def: the DOI definition
983 * @secattr: the security attributes
984 * @net_cat: the zero'd out category list in network/CIPSO format
985 * @net_cat_len: the length of the CIPSO category list in bytes
988 * Perform a label mapping to translate a local MLS category bitmap to the
989 * correct CIPSO category list using the given DOI definition. Returns the
990 * size in bytes of the network category bitmap on success, negative values
994 static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi
*doi_def
,
995 const struct netlbl_lsm_secattr
*secattr
,
996 unsigned char *net_cat
,
1003 cat
= netlbl_secattr_catmap_walk(secattr
->mls_cat
, cat
+ 1);
1006 if ((cat_iter
+ 2) > net_cat_len
)
1009 *((__be16
*)&net_cat
[cat_iter
]) = htons(cat
);
1017 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1018 * @doi_def: the DOI definition
1019 * @net_cat: the category list in network/CIPSO format
1020 * @net_cat_len: the length of the CIPSO bitmap in bytes
1021 * @secattr: the security attributes
1024 * Perform a label mapping to translate a CIPSO category list to the correct
1025 * local MLS category bitmap using the given DOI definition. Returns zero on
1026 * success, negative values on failure.
1029 static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi
*doi_def
,
1030 const unsigned char *net_cat
,
1032 struct netlbl_lsm_secattr
*secattr
)
1037 for (iter
= 0; iter
< net_cat_len
; iter
+= 2) {
1038 ret_val
= netlbl_secattr_catmap_setbit(secattr
->mls_cat
,
1039 ntohs(*((__be16
*)&net_cat
[iter
])),
1049 * Protocol Handling Functions
1052 #define CIPSO_V4_OPT_LEN_MAX 40
1053 #define CIPSO_V4_HDR_LEN 6
1056 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1057 * @doi_def: the DOI definition
1058 * @len: the total tag length in bytes, not including this header
1059 * @buf: the CIPSO option buffer
1062 * Write a CIPSO header into the beginning of @buffer.
1065 static void cipso_v4_gentag_hdr(const struct cipso_v4_doi
*doi_def
,
1069 buf
[0] = IPOPT_CIPSO
;
1070 buf
[1] = CIPSO_V4_HDR_LEN
+ len
;
1071 *(__be32
*)&buf
[2] = htonl(doi_def
->doi
);
1075 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1076 * @doi_def: the DOI definition
1077 * @secattr: the security attributes
1078 * @buffer: the option buffer
1079 * @buffer_len: length of buffer in bytes
1082 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1083 * actual buffer length may be larger than the indicated size due to
1084 * translation between host and network category bitmaps. Returns the size of
1085 * the tag on success, negative values on failure.
1088 static int cipso_v4_gentag_rbm(const struct cipso_v4_doi
*doi_def
,
1089 const struct netlbl_lsm_secattr
*secattr
,
1090 unsigned char *buffer
,
1097 if ((secattr
->flags
& NETLBL_SECATTR_MLS_LVL
) == 0)
1100 ret_val
= cipso_v4_map_lvl_hton(doi_def
, secattr
->mls_lvl
, &level
);
1104 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
1105 ret_val
= cipso_v4_map_cat_rbm_hton(doi_def
,
1112 /* This will send packets using the "optimized" format when
1113 * possibile as specified in section 3.4.2.6 of the
1115 if (cipso_v4_rbm_optfmt
&& ret_val
> 0 && ret_val
<= 10)
1118 tag_len
= 4 + ret_val
;
1123 buffer
[1] = tag_len
;
1130 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1131 * @doi_def: the DOI definition
1132 * @tag: the CIPSO tag
1133 * @secattr: the security attributes
1136 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1137 * attributes in @secattr. Return zero on success, negatives values on
1141 static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi
*doi_def
,
1142 const unsigned char *tag
,
1143 struct netlbl_lsm_secattr
*secattr
)
1146 u8 tag_len
= tag
[1];
1149 ret_val
= cipso_v4_map_lvl_ntoh(doi_def
, tag
[3], &level
);
1152 secattr
->mls_lvl
= level
;
1153 secattr
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1156 secattr
->mls_cat
= netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1157 if (secattr
->mls_cat
== NULL
)
1160 ret_val
= cipso_v4_map_cat_rbm_ntoh(doi_def
,
1165 netlbl_secattr_catmap_free(secattr
->mls_cat
);
1169 secattr
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1176 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1177 * @doi_def: the DOI definition
1178 * @secattr: the security attributes
1179 * @buffer: the option buffer
1180 * @buffer_len: length of buffer in bytes
1183 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1184 * size of the tag on success, negative values on failure.
1187 static int cipso_v4_gentag_enum(const struct cipso_v4_doi
*doi_def
,
1188 const struct netlbl_lsm_secattr
*secattr
,
1189 unsigned char *buffer
,
1196 if (!(secattr
->flags
& NETLBL_SECATTR_MLS_LVL
))
1199 ret_val
= cipso_v4_map_lvl_hton(doi_def
, secattr
->mls_lvl
, &level
);
1203 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
1204 ret_val
= cipso_v4_map_cat_enum_hton(doi_def
,
1211 tag_len
= 4 + ret_val
;
1216 buffer
[1] = tag_len
;
1223 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1224 * @doi_def: the DOI definition
1225 * @tag: the CIPSO tag
1226 * @secattr: the security attributes
1229 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1230 * attributes in @secattr. Return zero on success, negatives values on
1234 static int cipso_v4_parsetag_enum(const struct cipso_v4_doi
*doi_def
,
1235 const unsigned char *tag
,
1236 struct netlbl_lsm_secattr
*secattr
)
1239 u8 tag_len
= tag
[1];
1242 ret_val
= cipso_v4_map_lvl_ntoh(doi_def
, tag
[3], &level
);
1245 secattr
->mls_lvl
= level
;
1246 secattr
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1249 secattr
->mls_cat
= netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1250 if (secattr
->mls_cat
== NULL
)
1253 ret_val
= cipso_v4_map_cat_enum_ntoh(doi_def
,
1258 netlbl_secattr_catmap_free(secattr
->mls_cat
);
1262 secattr
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1269 * cipso_v4_validate - Validate a CIPSO option
1270 * @option: the start of the option, on error it is set to point to the error
1273 * This routine is called to validate a CIPSO option, it checks all of the
1274 * fields to ensure that they are at least valid, see the draft snippet below
1275 * for details. If the option is valid then a zero value is returned and
1276 * the value of @option is unchanged. If the option is invalid then a
1277 * non-zero value is returned and @option is adjusted to point to the
1278 * offending portion of the option. From the IETF draft ...
1280 * "If any field within the CIPSO options, such as the DOI identifier, is not
1281 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1282 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1283 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1284 * that is unrecognized."
1287 int cipso_v4_validate(unsigned char **option
)
1289 unsigned char *opt
= *option
;
1291 unsigned char opt_iter
;
1292 unsigned char err_offset
= 0;
1295 struct cipso_v4_doi
*doi_def
= NULL
;
1298 /* caller already checks for length values that are too large */
1302 goto validate_return
;
1306 doi_def
= cipso_v4_doi_search(ntohl(*((__be32
*)&opt
[2])));
1307 if (doi_def
== NULL
) {
1309 goto validate_return_locked
;
1313 tag
= opt
+ opt_iter
;
1314 while (opt_iter
< opt_len
) {
1315 for (tag_iter
= 0; doi_def
->tags
[tag_iter
] != tag
[0];)
1316 if (doi_def
->tags
[tag_iter
] == CIPSO_V4_TAG_INVALID
||
1317 ++tag_iter
== CIPSO_V4_TAG_MAXCNT
) {
1318 err_offset
= opt_iter
;
1319 goto validate_return_locked
;
1323 if (tag_len
> (opt_len
- opt_iter
)) {
1324 err_offset
= opt_iter
+ 1;
1325 goto validate_return_locked
;
1329 case CIPSO_V4_TAG_RBITMAP
:
1331 err_offset
= opt_iter
+ 1;
1332 goto validate_return_locked
;
1335 /* We are already going to do all the verification
1336 * necessary at the socket layer so from our point of
1337 * view it is safe to turn these checks off (and less
1338 * work), however, the CIPSO draft says we should do
1339 * all the CIPSO validations here but it doesn't
1340 * really specify _exactly_ what we need to validate
1341 * ... so, just make it a sysctl tunable. */
1342 if (cipso_v4_rbm_strictvalid
) {
1343 if (cipso_v4_map_lvl_valid(doi_def
,
1345 err_offset
= opt_iter
+ 3;
1346 goto validate_return_locked
;
1349 cipso_v4_map_cat_rbm_valid(doi_def
,
1352 err_offset
= opt_iter
+ 4;
1353 goto validate_return_locked
;
1357 case CIPSO_V4_TAG_ENUM
:
1359 err_offset
= opt_iter
+ 1;
1360 goto validate_return_locked
;
1363 if (cipso_v4_map_lvl_valid(doi_def
,
1365 err_offset
= opt_iter
+ 3;
1366 goto validate_return_locked
;
1369 cipso_v4_map_cat_enum_valid(doi_def
,
1372 err_offset
= opt_iter
+ 4;
1373 goto validate_return_locked
;
1377 err_offset
= opt_iter
;
1378 goto validate_return_locked
;
1382 opt_iter
+= tag_len
;
1385 validate_return_locked
:
1388 *option
= opt
+ err_offset
;
1393 * cipso_v4_error - Send the correct reponse for a bad packet
1395 * @error: the error code
1396 * @gateway: CIPSO gateway flag
1399 * Based on the error code given in @error, send an ICMP error message back to
1400 * the originating host. From the IETF draft ...
1402 * "If the contents of the CIPSO [option] are valid but the security label is
1403 * outside of the configured host or port label range, the datagram is
1404 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1405 * returned. The code field of the ICMP is set to 'communication with
1406 * destination network administratively prohibited' (code 9) or to
1407 * 'communication with destination host administratively prohibited'
1408 * (code 10). The value of the code is dependent on whether the originator
1409 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1410 * recipient of the ICMP message MUST be able to handle either value. The
1411 * same procedure is performed if a CIPSO [option] can not be added to an
1412 * IP packet because it is too large to fit in the IP options area."
1414 * "If the error is triggered by receipt of an ICMP message, the message is
1415 * discarded and no response is permitted (consistent with general ICMP
1416 * processing rules)."
1419 void cipso_v4_error(struct sk_buff
*skb
, int error
, u32 gateway
)
1421 if (skb
->nh
.iph
->protocol
== IPPROTO_ICMP
|| error
!= -EACCES
)
1425 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_NET_ANO
, 0);
1427 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_HOST_ANO
, 0);
1431 * cipso_v4_socket_setattr - Add a CIPSO option to a socket
1433 * @doi_def: the CIPSO DOI to use
1434 * @secattr: the specific security attributes of the socket
1437 * Set the CIPSO option on the given socket using the DOI definition and
1438 * security attributes passed to the function. This function requires
1439 * exclusive access to @sock->sk, which means it either needs to be in the
1440 * process of being created or locked via lock_sock(sock->sk). Returns zero on
1441 * success and negative values on failure.
1444 int cipso_v4_socket_setattr(const struct socket
*sock
,
1445 const struct cipso_v4_doi
*doi_def
,
1446 const struct netlbl_lsm_secattr
*secattr
)
1448 int ret_val
= -EPERM
;
1453 struct ip_options
*opt
= NULL
;
1455 struct inet_sock
*sk_inet
;
1456 struct inet_connection_sock
*sk_conn
;
1458 /* In the case of sock_create_lite(), the sock->sk field is not
1459 * defined yet but it is not a problem as the only users of these
1460 * "lite" PF_INET sockets are functions which do an accept() call
1461 * afterwards so we will label the socket as part of the accept(). */
1466 /* We allocate the maximum CIPSO option size here so we are probably
1467 * being a little wasteful, but it makes our life _much_ easier later
1468 * on and after all we are only talking about 40 bytes. */
1469 buf_len
= CIPSO_V4_OPT_LEN_MAX
;
1470 buf
= kmalloc(buf_len
, GFP_ATOMIC
);
1473 goto socket_setattr_failure
;
1476 /* XXX - This code assumes only one tag per CIPSO option which isn't
1477 * really a good assumption to make but since we only support the MAC
1478 * tags right now it is a safe assumption. */
1481 memset(buf
, 0, buf_len
);
1482 switch (doi_def
->tags
[iter
]) {
1483 case CIPSO_V4_TAG_RBITMAP
:
1484 ret_val
= cipso_v4_gentag_rbm(doi_def
,
1486 &buf
[CIPSO_V4_HDR_LEN
],
1487 buf_len
- CIPSO_V4_HDR_LEN
);
1489 case CIPSO_V4_TAG_ENUM
:
1490 ret_val
= cipso_v4_gentag_enum(doi_def
,
1492 &buf
[CIPSO_V4_HDR_LEN
],
1493 buf_len
- CIPSO_V4_HDR_LEN
);
1497 goto socket_setattr_failure
;
1501 } while (ret_val
< 0 &&
1502 iter
< CIPSO_V4_TAG_MAXCNT
&&
1503 doi_def
->tags
[iter
] != CIPSO_V4_TAG_INVALID
);
1505 goto socket_setattr_failure
;
1506 cipso_v4_gentag_hdr(doi_def
, buf
, ret_val
);
1507 buf_len
= CIPSO_V4_HDR_LEN
+ ret_val
;
1509 /* We can't use ip_options_get() directly because it makes a call to
1510 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1511 * we won't always have CAP_NET_RAW even though we _always_ want to
1512 * set the IPOPT_CIPSO option. */
1513 opt_len
= (buf_len
+ 3) & ~3;
1514 opt
= kzalloc(sizeof(*opt
) + opt_len
, GFP_ATOMIC
);
1517 goto socket_setattr_failure
;
1519 memcpy(opt
->__data
, buf
, buf_len
);
1520 opt
->optlen
= opt_len
;
1522 opt
->cipso
= sizeof(struct iphdr
);
1526 sk_inet
= inet_sk(sk
);
1527 if (sk_inet
->is_icsk
) {
1528 sk_conn
= inet_csk(sk
);
1530 sk_conn
->icsk_ext_hdr_len
-= sk_inet
->opt
->optlen
;
1531 sk_conn
->icsk_ext_hdr_len
+= opt
->optlen
;
1532 sk_conn
->icsk_sync_mss(sk
, sk_conn
->icsk_pmtu_cookie
);
1534 opt
= xchg(&sk_inet
->opt
, opt
);
1539 socket_setattr_failure
:
1546 * cipso_v4_sock_getattr - Get the security attributes from a sock
1548 * @secattr: the security attributes
1551 * Query @sk to see if there is a CIPSO option attached to the sock and if
1552 * there is return the CIPSO security attributes in @secattr. This function
1553 * requires that @sk be locked, or privately held, but it does not do any
1554 * locking itself. Returns zero on success and negative values on failure.
1557 int cipso_v4_sock_getattr(struct sock
*sk
, struct netlbl_lsm_secattr
*secattr
)
1559 int ret_val
= -ENOMSG
;
1560 struct inet_sock
*sk_inet
;
1561 unsigned char *cipso_ptr
;
1563 struct cipso_v4_doi
*doi_def
;
1565 sk_inet
= inet_sk(sk
);
1566 if (sk_inet
->opt
== NULL
|| sk_inet
->opt
->cipso
== 0)
1568 cipso_ptr
= sk_inet
->opt
->__data
+ sk_inet
->opt
->cipso
-
1569 sizeof(struct iphdr
);
1570 ret_val
= cipso_v4_cache_check(cipso_ptr
, cipso_ptr
[1], secattr
);
1574 doi
= ntohl(*(__be32
*)&cipso_ptr
[2]);
1576 doi_def
= cipso_v4_doi_search(doi
);
1577 if (doi_def
== NULL
) {
1582 /* XXX - This code assumes only one tag per CIPSO option which isn't
1583 * really a good assumption to make but since we only support the MAC
1584 * tags right now it is a safe assumption. */
1585 switch (cipso_ptr
[6]) {
1586 case CIPSO_V4_TAG_RBITMAP
:
1587 ret_val
= cipso_v4_parsetag_rbm(doi_def
,
1591 case CIPSO_V4_TAG_ENUM
:
1592 ret_val
= cipso_v4_parsetag_enum(doi_def
,
1603 * cipso_v4_socket_getattr - Get the security attributes from a socket
1605 * @secattr: the security attributes
1608 * Query @sock to see if there is a CIPSO option attached to the socket and if
1609 * there is return the CIPSO security attributes in @secattr. Returns zero on
1610 * success and negative values on failure.
1613 int cipso_v4_socket_getattr(const struct socket
*sock
,
1614 struct netlbl_lsm_secattr
*secattr
)
1618 lock_sock(sock
->sk
);
1619 ret_val
= cipso_v4_sock_getattr(sock
->sk
, secattr
);
1620 release_sock(sock
->sk
);
1626 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
1628 * @secattr: the security attributes
1631 * Parse the given packet's CIPSO option and return the security attributes.
1632 * Returns zero on success and negative values on failure.
1635 int cipso_v4_skbuff_getattr(const struct sk_buff
*skb
,
1636 struct netlbl_lsm_secattr
*secattr
)
1638 int ret_val
= -ENOMSG
;
1639 unsigned char *cipso_ptr
;
1641 struct cipso_v4_doi
*doi_def
;
1643 cipso_ptr
= CIPSO_V4_OPTPTR(skb
);
1644 if (cipso_v4_cache_check(cipso_ptr
, cipso_ptr
[1], secattr
) == 0)
1647 doi
= ntohl(*(__be32
*)&cipso_ptr
[2]);
1649 doi_def
= cipso_v4_doi_search(doi
);
1650 if (doi_def
== NULL
)
1651 goto skbuff_getattr_return
;
1653 /* XXX - This code assumes only one tag per CIPSO option which isn't
1654 * really a good assumption to make but since we only support the MAC
1655 * tags right now it is a safe assumption. */
1656 switch (cipso_ptr
[6]) {
1657 case CIPSO_V4_TAG_RBITMAP
:
1658 ret_val
= cipso_v4_parsetag_rbm(doi_def
,
1662 case CIPSO_V4_TAG_ENUM
:
1663 ret_val
= cipso_v4_parsetag_enum(doi_def
,
1669 skbuff_getattr_return
:
1679 * cipso_v4_init - Initialize the CIPSO module
1682 * Initialize the CIPSO module and prepare it for use. Returns zero on success
1683 * and negative values on failure.
1686 static int __init
cipso_v4_init(void)
1690 ret_val
= cipso_v4_cache_init();
1692 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
1698 subsys_initcall(cipso_v4_init
);