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. While CIPSO never became a full IETF RFC standard many vendors
7 * have chosen to adopt the protocol and over the years it has become a
8 * de-facto standard for labeled networking.
10 * The CIPSO draft specification can be found in the kernel's Documentation
11 * directory as well as the following URL:
12 * http://netlabel.sourceforge.net/files/draft-ietf-cipso-ipsecurity-01.txt
13 * The FIPS-188 specification can be found at the following URL:
14 * http://www.itl.nist.gov/fipspubs/fip188.htm
16 * Author: Paul Moore <paul.moore@hp.com>
21 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
31 * the GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #include <linux/init.h>
40 #include <linux/types.h>
41 #include <linux/rcupdate.h>
42 #include <linux/list.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include <linux/jhash.h>
46 #include <linux/audit.h>
47 #include <linux/slab.h>
51 #include <net/netlabel.h>
52 #include <net/cipso_ipv4.h>
53 #include <asm/atomic.h>
55 #include <asm/unaligned.h>
57 /* List of available DOI definitions */
58 /* XXX - This currently assumes a minimal number of different DOIs in use,
59 * if in practice there are a lot of different DOIs this list should
60 * probably be turned into a hash table or something similar so we
61 * can do quick lookups. */
62 static DEFINE_SPINLOCK(cipso_v4_doi_list_lock
);
63 static LIST_HEAD(cipso_v4_doi_list
);
65 /* Label mapping cache */
66 int cipso_v4_cache_enabled
= 1;
67 int cipso_v4_cache_bucketsize
= 10;
68 #define CIPSO_V4_CACHE_BUCKETBITS 7
69 #define CIPSO_V4_CACHE_BUCKETS (1 << CIPSO_V4_CACHE_BUCKETBITS)
70 #define CIPSO_V4_CACHE_REORDERLIMIT 10
71 struct cipso_v4_map_cache_bkt
{
74 struct list_head list
;
76 struct cipso_v4_map_cache_entry
{
81 struct netlbl_lsm_cache
*lsm_data
;
84 struct list_head list
;
86 static struct cipso_v4_map_cache_bkt
*cipso_v4_cache
= NULL
;
88 /* Restricted bitmap (tag #1) flags */
89 int cipso_v4_rbm_optfmt
= 0;
90 int cipso_v4_rbm_strictvalid
= 1;
96 /* Maximum size of the CIPSO IP option, derived from the fact that the maximum
97 * IPv4 header size is 60 bytes and the base IPv4 header is 20 bytes long. */
98 #define CIPSO_V4_OPT_LEN_MAX 40
100 /* Length of the base CIPSO option, this includes the option type (1 byte), the
101 * option length (1 byte), and the DOI (4 bytes). */
102 #define CIPSO_V4_HDR_LEN 6
104 /* Base length of the restrictive category bitmap tag (tag #1). */
105 #define CIPSO_V4_TAG_RBM_BLEN 4
107 /* Base length of the enumerated category tag (tag #2). */
108 #define CIPSO_V4_TAG_ENUM_BLEN 4
110 /* Base length of the ranged categories bitmap tag (tag #5). */
111 #define CIPSO_V4_TAG_RNG_BLEN 4
112 /* The maximum number of category ranges permitted in the ranged category tag
113 * (tag #5). You may note that the IETF draft states that the maximum number
114 * of category ranges is 7, but if the low end of the last category range is
115 * zero then it is possibile to fit 8 category ranges because the zero should
117 #define CIPSO_V4_TAG_RNG_CAT_MAX 8
119 /* Base length of the local tag (non-standard tag).
120 * Tag definition (may change between kernel versions)
123 * +----------+----------+----------+----------+
124 * | 10000000 | 00000110 | 32-bit secid value |
125 * +----------+----------+----------+----------+
126 * | in (host byte order)|
127 * +----------+----------+
130 #define CIPSO_V4_TAG_LOC_BLEN 6
137 * cipso_v4_bitmap_walk - Walk a bitmap looking for a bit
138 * @bitmap: the bitmap
139 * @bitmap_len: length in bits
140 * @offset: starting offset
141 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
144 * Starting at @offset, walk the bitmap from left to right until either the
145 * desired bit is found or we reach the end. Return the bit offset, -1 if
146 * not found, or -2 if error.
148 static int cipso_v4_bitmap_walk(const unsigned char *bitmap
,
155 unsigned char bitmask
;
158 /* gcc always rounds to zero when doing integer division */
159 byte_offset
= offset
/ 8;
160 byte
= bitmap
[byte_offset
];
162 bitmask
= 0x80 >> (offset
% 8);
164 while (bit_spot
< bitmap_len
) {
165 if ((state
&& (byte
& bitmask
) == bitmask
) ||
166 (state
== 0 && (byte
& bitmask
) == 0))
172 byte
= bitmap
[++byte_offset
];
181 * cipso_v4_bitmap_setbit - Sets a single bit in a bitmap
182 * @bitmap: the bitmap
184 * @state: if non-zero, set the bit (1) else clear the bit (0)
187 * Set a single bit in the bitmask. Returns zero on success, negative values
190 static void cipso_v4_bitmap_setbit(unsigned char *bitmap
,
197 /* gcc always rounds to zero when doing integer division */
199 bitmask
= 0x80 >> (bit
% 8);
201 bitmap
[byte_spot
] |= bitmask
;
203 bitmap
[byte_spot
] &= ~bitmask
;
207 * cipso_v4_cache_entry_free - Frees a cache entry
208 * @entry: the entry to free
211 * This function frees the memory associated with a cache entry including the
212 * LSM cache data if there are no longer any users, i.e. reference count == 0.
215 static void cipso_v4_cache_entry_free(struct cipso_v4_map_cache_entry
*entry
)
218 netlbl_secattr_cache_free(entry
->lsm_data
);
224 * cipso_v4_map_cache_hash - Hashing function for the CIPSO cache
226 * @key_len: the length of the key in bytes
229 * The CIPSO tag hashing function. Returns a 32-bit hash value.
232 static u32
cipso_v4_map_cache_hash(const unsigned char *key
, u32 key_len
)
234 return jhash(key
, key_len
, 0);
238 * Label Mapping Cache Functions
242 * cipso_v4_cache_init - Initialize the CIPSO cache
245 * Initializes the CIPSO label mapping cache, this function should be called
246 * before any of the other functions defined in this file. Returns zero on
247 * success, negative values on error.
250 static int cipso_v4_cache_init(void)
254 cipso_v4_cache
= kcalloc(CIPSO_V4_CACHE_BUCKETS
,
255 sizeof(struct cipso_v4_map_cache_bkt
),
257 if (cipso_v4_cache
== NULL
)
260 for (iter
= 0; iter
< CIPSO_V4_CACHE_BUCKETS
; iter
++) {
261 spin_lock_init(&cipso_v4_cache
[iter
].lock
);
262 cipso_v4_cache
[iter
].size
= 0;
263 INIT_LIST_HEAD(&cipso_v4_cache
[iter
].list
);
270 * cipso_v4_cache_invalidate - Invalidates the current CIPSO cache
273 * Invalidates and frees any entries in the CIPSO cache. Returns zero on
274 * success and negative values on failure.
277 void cipso_v4_cache_invalidate(void)
279 struct cipso_v4_map_cache_entry
*entry
, *tmp_entry
;
282 for (iter
= 0; iter
< CIPSO_V4_CACHE_BUCKETS
; iter
++) {
283 spin_lock_bh(&cipso_v4_cache
[iter
].lock
);
284 list_for_each_entry_safe(entry
,
286 &cipso_v4_cache
[iter
].list
, list
) {
287 list_del(&entry
->list
);
288 cipso_v4_cache_entry_free(entry
);
290 cipso_v4_cache
[iter
].size
= 0;
291 spin_unlock_bh(&cipso_v4_cache
[iter
].lock
);
298 * cipso_v4_cache_check - Check the CIPSO cache for a label mapping
299 * @key: the buffer to check
300 * @key_len: buffer length in bytes
301 * @secattr: the security attribute struct to use
304 * This function checks the cache to see if a label mapping already exists for
305 * the given key. If there is a match then the cache is adjusted and the
306 * @secattr struct is populated with the correct LSM security attributes. The
307 * cache is adjusted in the following manner if the entry is not already the
308 * first in the cache bucket:
310 * 1. The cache entry's activity counter is incremented
311 * 2. The previous (higher ranking) entry's activity counter is decremented
312 * 3. If the difference between the two activity counters is geater than
313 * CIPSO_V4_CACHE_REORDERLIMIT the two entries are swapped
315 * Returns zero on success, -ENOENT for a cache miss, and other negative values
319 static int cipso_v4_cache_check(const unsigned char *key
,
321 struct netlbl_lsm_secattr
*secattr
)
324 struct cipso_v4_map_cache_entry
*entry
;
325 struct cipso_v4_map_cache_entry
*prev_entry
= NULL
;
328 if (!cipso_v4_cache_enabled
)
331 hash
= cipso_v4_map_cache_hash(key
, key_len
);
332 bkt
= hash
& (CIPSO_V4_CACHE_BUCKETS
- 1);
333 spin_lock_bh(&cipso_v4_cache
[bkt
].lock
);
334 list_for_each_entry(entry
, &cipso_v4_cache
[bkt
].list
, list
) {
335 if (entry
->hash
== hash
&&
336 entry
->key_len
== key_len
&&
337 memcmp(entry
->key
, key
, key_len
) == 0) {
338 entry
->activity
+= 1;
339 atomic_inc(&entry
->lsm_data
->refcount
);
340 secattr
->cache
= entry
->lsm_data
;
341 secattr
->flags
|= NETLBL_SECATTR_CACHE
;
342 secattr
->type
= NETLBL_NLTYPE_CIPSOV4
;
343 if (prev_entry
== NULL
) {
344 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
348 if (prev_entry
->activity
> 0)
349 prev_entry
->activity
-= 1;
350 if (entry
->activity
> prev_entry
->activity
&&
351 entry
->activity
- prev_entry
->activity
>
352 CIPSO_V4_CACHE_REORDERLIMIT
) {
353 __list_del(entry
->list
.prev
, entry
->list
.next
);
354 __list_add(&entry
->list
,
355 prev_entry
->list
.prev
,
359 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
364 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
370 * cipso_v4_cache_add - Add an entry to the CIPSO cache
372 * @secattr: the packet's security attributes
375 * Add a new entry into the CIPSO label mapping cache. Add the new entry to
376 * head of the cache bucket's list, if the cache bucket is out of room remove
377 * the last entry in the list first. It is important to note that there is
378 * currently no checking for duplicate keys. Returns zero on success,
379 * negative values on failure.
382 int cipso_v4_cache_add(const struct sk_buff
*skb
,
383 const struct netlbl_lsm_secattr
*secattr
)
385 int ret_val
= -EPERM
;
387 struct cipso_v4_map_cache_entry
*entry
= NULL
;
388 struct cipso_v4_map_cache_entry
*old_entry
= NULL
;
389 unsigned char *cipso_ptr
;
392 if (!cipso_v4_cache_enabled
|| cipso_v4_cache_bucketsize
<= 0)
395 cipso_ptr
= CIPSO_V4_OPTPTR(skb
);
396 cipso_ptr_len
= cipso_ptr
[1];
398 entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
401 entry
->key
= kmemdup(cipso_ptr
, cipso_ptr_len
, GFP_ATOMIC
);
402 if (entry
->key
== NULL
) {
404 goto cache_add_failure
;
406 entry
->key_len
= cipso_ptr_len
;
407 entry
->hash
= cipso_v4_map_cache_hash(cipso_ptr
, cipso_ptr_len
);
408 atomic_inc(&secattr
->cache
->refcount
);
409 entry
->lsm_data
= secattr
->cache
;
411 bkt
= entry
->hash
& (CIPSO_V4_CACHE_BUCKETS
- 1);
412 spin_lock_bh(&cipso_v4_cache
[bkt
].lock
);
413 if (cipso_v4_cache
[bkt
].size
< cipso_v4_cache_bucketsize
) {
414 list_add(&entry
->list
, &cipso_v4_cache
[bkt
].list
);
415 cipso_v4_cache
[bkt
].size
+= 1;
417 old_entry
= list_entry(cipso_v4_cache
[bkt
].list
.prev
,
418 struct cipso_v4_map_cache_entry
, list
);
419 list_del(&old_entry
->list
);
420 list_add(&entry
->list
, &cipso_v4_cache
[bkt
].list
);
421 cipso_v4_cache_entry_free(old_entry
);
423 spin_unlock_bh(&cipso_v4_cache
[bkt
].lock
);
429 cipso_v4_cache_entry_free(entry
);
438 * cipso_v4_doi_search - Searches for a DOI definition
439 * @doi: the DOI to search for
442 * Search the DOI definition list for a DOI definition with a DOI value that
443 * matches @doi. The caller is responsibile for calling rcu_read_[un]lock().
444 * Returns a pointer to the DOI definition on success and NULL on failure.
446 static struct cipso_v4_doi
*cipso_v4_doi_search(u32 doi
)
448 struct cipso_v4_doi
*iter
;
450 list_for_each_entry_rcu(iter
, &cipso_v4_doi_list
, list
)
451 if (iter
->doi
== doi
&& atomic_read(&iter
->refcount
))
457 * cipso_v4_doi_add - Add a new DOI to the CIPSO protocol engine
458 * @doi_def: the DOI structure
459 * @audit_info: NetLabel audit information
462 * The caller defines a new DOI for use by the CIPSO engine and calls this
463 * function to add it to the list of acceptable domains. The caller must
464 * ensure that the mapping table specified in @doi_def->map meets all of the
465 * requirements of the mapping type (see cipso_ipv4.h for details). Returns
466 * zero on success and non-zero on failure.
469 int cipso_v4_doi_add(struct cipso_v4_doi
*doi_def
,
470 struct netlbl_audit
*audit_info
)
472 int ret_val
= -EINVAL
;
476 struct audit_buffer
*audit_buf
;
479 doi_type
= doi_def
->type
;
481 if (doi_def
== NULL
|| doi_def
->doi
== CIPSO_V4_DOI_UNKNOWN
)
483 for (iter
= 0; iter
< CIPSO_V4_TAG_MAXCNT
; iter
++) {
484 switch (doi_def
->tags
[iter
]) {
485 case CIPSO_V4_TAG_RBITMAP
:
487 case CIPSO_V4_TAG_RANGE
:
488 case CIPSO_V4_TAG_ENUM
:
489 if (doi_def
->type
!= CIPSO_V4_MAP_PASS
)
492 case CIPSO_V4_TAG_LOCAL
:
493 if (doi_def
->type
!= CIPSO_V4_MAP_LOCAL
)
496 case CIPSO_V4_TAG_INVALID
:
505 atomic_set(&doi_def
->refcount
, 1);
507 spin_lock(&cipso_v4_doi_list_lock
);
508 if (cipso_v4_doi_search(doi_def
->doi
) != NULL
) {
509 spin_unlock(&cipso_v4_doi_list_lock
);
513 list_add_tail_rcu(&doi_def
->list
, &cipso_v4_doi_list
);
514 spin_unlock(&cipso_v4_doi_list_lock
);
518 audit_buf
= netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD
, audit_info
);
519 if (audit_buf
!= NULL
) {
520 const char *type_str
;
522 case CIPSO_V4_MAP_TRANS
:
525 case CIPSO_V4_MAP_PASS
:
528 case CIPSO_V4_MAP_LOCAL
:
532 type_str
= "(unknown)";
534 audit_log_format(audit_buf
,
535 " cipso_doi=%u cipso_type=%s res=%u",
536 doi
, type_str
, ret_val
== 0 ? 1 : 0);
537 audit_log_end(audit_buf
);
544 * cipso_v4_doi_free - Frees a DOI definition
545 * @entry: the entry's RCU field
548 * This function frees all of the memory associated with a DOI definition.
551 void cipso_v4_doi_free(struct cipso_v4_doi
*doi_def
)
556 switch (doi_def
->type
) {
557 case CIPSO_V4_MAP_TRANS
:
558 kfree(doi_def
->map
.std
->lvl
.cipso
);
559 kfree(doi_def
->map
.std
->lvl
.local
);
560 kfree(doi_def
->map
.std
->cat
.cipso
);
561 kfree(doi_def
->map
.std
->cat
.local
);
568 * cipso_v4_doi_free_rcu - Frees a DOI definition via the RCU pointer
569 * @entry: the entry's RCU field
572 * This function is designed to be used as a callback to the call_rcu()
573 * function so that the memory allocated to the DOI definition can be released
577 static void cipso_v4_doi_free_rcu(struct rcu_head
*entry
)
579 struct cipso_v4_doi
*doi_def
;
581 doi_def
= container_of(entry
, struct cipso_v4_doi
, rcu
);
582 cipso_v4_doi_free(doi_def
);
586 * cipso_v4_doi_remove - Remove an existing DOI from the CIPSO protocol engine
587 * @doi: the DOI value
588 * @audit_secid: the LSM secid to use in the audit message
591 * Removes a DOI definition from the CIPSO engine. The NetLabel routines will
592 * be called to release their own LSM domain mappings as well as our own
593 * domain list. Returns zero on success and negative values on failure.
596 int cipso_v4_doi_remove(u32 doi
, struct netlbl_audit
*audit_info
)
599 struct cipso_v4_doi
*doi_def
;
600 struct audit_buffer
*audit_buf
;
602 spin_lock(&cipso_v4_doi_list_lock
);
603 doi_def
= cipso_v4_doi_search(doi
);
604 if (doi_def
== NULL
) {
605 spin_unlock(&cipso_v4_doi_list_lock
);
607 goto doi_remove_return
;
609 if (!atomic_dec_and_test(&doi_def
->refcount
)) {
610 spin_unlock(&cipso_v4_doi_list_lock
);
612 goto doi_remove_return
;
614 list_del_rcu(&doi_def
->list
);
615 spin_unlock(&cipso_v4_doi_list_lock
);
617 cipso_v4_cache_invalidate();
618 call_rcu(&doi_def
->rcu
, cipso_v4_doi_free_rcu
);
622 audit_buf
= netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL
, audit_info
);
623 if (audit_buf
!= NULL
) {
624 audit_log_format(audit_buf
,
625 " cipso_doi=%u res=%u",
626 doi
, ret_val
== 0 ? 1 : 0);
627 audit_log_end(audit_buf
);
634 * cipso_v4_doi_getdef - Returns a reference to a valid DOI definition
635 * @doi: the DOI value
638 * Searches for a valid DOI definition and if one is found it is returned to
639 * the caller. Otherwise NULL is returned. The caller must ensure that
640 * rcu_read_lock() is held while accessing the returned definition and the DOI
641 * definition reference count is decremented when the caller is done.
644 struct cipso_v4_doi
*cipso_v4_doi_getdef(u32 doi
)
646 struct cipso_v4_doi
*doi_def
;
649 doi_def
= cipso_v4_doi_search(doi
);
651 goto doi_getdef_return
;
652 if (!atomic_inc_not_zero(&doi_def
->refcount
))
661 * cipso_v4_doi_putdef - Releases a reference for the given DOI definition
662 * @doi_def: the DOI definition
665 * Releases a DOI definition reference obtained from cipso_v4_doi_getdef().
668 void cipso_v4_doi_putdef(struct cipso_v4_doi
*doi_def
)
673 if (!atomic_dec_and_test(&doi_def
->refcount
))
675 spin_lock(&cipso_v4_doi_list_lock
);
676 list_del_rcu(&doi_def
->list
);
677 spin_unlock(&cipso_v4_doi_list_lock
);
679 cipso_v4_cache_invalidate();
680 call_rcu(&doi_def
->rcu
, cipso_v4_doi_free_rcu
);
684 * cipso_v4_doi_walk - Iterate through the DOI definitions
685 * @skip_cnt: skip past this number of DOI definitions, updated
686 * @callback: callback for each DOI definition
687 * @cb_arg: argument for the callback function
690 * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
691 * For each entry call @callback, if @callback returns a negative value stop
692 * 'walking' through the list and return. Updates the value in @skip_cnt upon
693 * return. Returns zero on success, negative values on failure.
696 int cipso_v4_doi_walk(u32
*skip_cnt
,
697 int (*callback
) (struct cipso_v4_doi
*doi_def
, void *arg
),
700 int ret_val
= -ENOENT
;
702 struct cipso_v4_doi
*iter_doi
;
705 list_for_each_entry_rcu(iter_doi
, &cipso_v4_doi_list
, list
)
706 if (atomic_read(&iter_doi
->refcount
) > 0) {
707 if (doi_cnt
++ < *skip_cnt
)
709 ret_val
= callback(iter_doi
, cb_arg
);
712 goto doi_walk_return
;
723 * Label Mapping Functions
727 * cipso_v4_map_lvl_valid - Checks to see if the given level is understood
728 * @doi_def: the DOI definition
729 * @level: the level to check
732 * Checks the given level against the given DOI definition and returns a
733 * negative value if the level does not have a valid mapping and a zero value
734 * if the level is defined by the DOI.
737 static int cipso_v4_map_lvl_valid(const struct cipso_v4_doi
*doi_def
, u8 level
)
739 switch (doi_def
->type
) {
740 case CIPSO_V4_MAP_PASS
:
742 case CIPSO_V4_MAP_TRANS
:
743 if (doi_def
->map
.std
->lvl
.cipso
[level
] < CIPSO_V4_INV_LVL
)
752 * cipso_v4_map_lvl_hton - Perform a level mapping from the host to the network
753 * @doi_def: the DOI definition
754 * @host_lvl: the host MLS level
755 * @net_lvl: the network/CIPSO MLS level
758 * Perform a label mapping to translate a local MLS level to the correct
759 * CIPSO level using the given DOI definition. Returns zero on success,
760 * negative values otherwise.
763 static int cipso_v4_map_lvl_hton(const struct cipso_v4_doi
*doi_def
,
767 switch (doi_def
->type
) {
768 case CIPSO_V4_MAP_PASS
:
771 case CIPSO_V4_MAP_TRANS
:
772 if (host_lvl
< doi_def
->map
.std
->lvl
.local_size
&&
773 doi_def
->map
.std
->lvl
.local
[host_lvl
] < CIPSO_V4_INV_LVL
) {
774 *net_lvl
= doi_def
->map
.std
->lvl
.local
[host_lvl
];
784 * cipso_v4_map_lvl_ntoh - Perform a level mapping from the network to the host
785 * @doi_def: the DOI definition
786 * @net_lvl: the network/CIPSO MLS level
787 * @host_lvl: the host MLS level
790 * Perform a label mapping to translate a CIPSO level to the correct local MLS
791 * level using the given DOI definition. Returns zero on success, negative
795 static int cipso_v4_map_lvl_ntoh(const struct cipso_v4_doi
*doi_def
,
799 struct cipso_v4_std_map_tbl
*map_tbl
;
801 switch (doi_def
->type
) {
802 case CIPSO_V4_MAP_PASS
:
805 case CIPSO_V4_MAP_TRANS
:
806 map_tbl
= doi_def
->map
.std
;
807 if (net_lvl
< map_tbl
->lvl
.cipso_size
&&
808 map_tbl
->lvl
.cipso
[net_lvl
] < CIPSO_V4_INV_LVL
) {
809 *host_lvl
= doi_def
->map
.std
->lvl
.cipso
[net_lvl
];
819 * cipso_v4_map_cat_rbm_valid - Checks to see if the category bitmap is valid
820 * @doi_def: the DOI definition
821 * @bitmap: category bitmap
822 * @bitmap_len: bitmap length in bytes
825 * Checks the given category bitmap against the given DOI definition and
826 * returns a negative value if any of the categories in the bitmap do not have
827 * a valid mapping and a zero value if all of the categories are valid.
830 static int cipso_v4_map_cat_rbm_valid(const struct cipso_v4_doi
*doi_def
,
831 const unsigned char *bitmap
,
835 u32 bitmap_len_bits
= bitmap_len
* 8;
839 switch (doi_def
->type
) {
840 case CIPSO_V4_MAP_PASS
:
842 case CIPSO_V4_MAP_TRANS
:
843 cipso_cat_size
= doi_def
->map
.std
->cat
.cipso_size
;
844 cipso_array
= doi_def
->map
.std
->cat
.cipso
;
846 cat
= cipso_v4_bitmap_walk(bitmap
,
852 if (cat
>= cipso_cat_size
||
853 cipso_array
[cat
] >= CIPSO_V4_INV_CAT
)
866 * cipso_v4_map_cat_rbm_hton - Perform a category mapping from host to network
867 * @doi_def: the DOI definition
868 * @secattr: the security attributes
869 * @net_cat: the zero'd out category bitmap in network/CIPSO format
870 * @net_cat_len: the length of the CIPSO bitmap in bytes
873 * Perform a label mapping to translate a local MLS category bitmap to the
874 * correct CIPSO bitmap using the given DOI definition. Returns the minimum
875 * size in bytes of the network bitmap on success, negative values otherwise.
878 static int cipso_v4_map_cat_rbm_hton(const struct cipso_v4_doi
*doi_def
,
879 const struct netlbl_lsm_secattr
*secattr
,
880 unsigned char *net_cat
,
884 u32 net_spot
= CIPSO_V4_INV_CAT
;
885 u32 net_spot_max
= 0;
886 u32 net_clen_bits
= net_cat_len
* 8;
887 u32 host_cat_size
= 0;
888 u32
*host_cat_array
= NULL
;
890 if (doi_def
->type
== CIPSO_V4_MAP_TRANS
) {
891 host_cat_size
= doi_def
->map
.std
->cat
.local_size
;
892 host_cat_array
= doi_def
->map
.std
->cat
.local
;
896 host_spot
= netlbl_secattr_catmap_walk(secattr
->attr
.mls
.cat
,
901 switch (doi_def
->type
) {
902 case CIPSO_V4_MAP_PASS
:
903 net_spot
= host_spot
;
905 case CIPSO_V4_MAP_TRANS
:
906 if (host_spot
>= host_cat_size
)
908 net_spot
= host_cat_array
[host_spot
];
909 if (net_spot
>= CIPSO_V4_INV_CAT
)
913 if (net_spot
>= net_clen_bits
)
915 cipso_v4_bitmap_setbit(net_cat
, net_spot
, 1);
917 if (net_spot
> net_spot_max
)
918 net_spot_max
= net_spot
;
921 if (++net_spot_max
% 8)
922 return net_spot_max
/ 8 + 1;
923 return net_spot_max
/ 8;
927 * cipso_v4_map_cat_rbm_ntoh - Perform a category mapping from network to host
928 * @doi_def: the DOI definition
929 * @net_cat: the category bitmap in network/CIPSO format
930 * @net_cat_len: the length of the CIPSO bitmap in bytes
931 * @secattr: the security attributes
934 * Perform a label mapping to translate a CIPSO bitmap to the correct local
935 * MLS category bitmap using the given DOI definition. Returns zero on
936 * success, negative values on failure.
939 static int cipso_v4_map_cat_rbm_ntoh(const struct cipso_v4_doi
*doi_def
,
940 const unsigned char *net_cat
,
942 struct netlbl_lsm_secattr
*secattr
)
946 u32 host_spot
= CIPSO_V4_INV_CAT
;
947 u32 net_clen_bits
= net_cat_len
* 8;
948 u32 net_cat_size
= 0;
949 u32
*net_cat_array
= NULL
;
951 if (doi_def
->type
== CIPSO_V4_MAP_TRANS
) {
952 net_cat_size
= doi_def
->map
.std
->cat
.cipso_size
;
953 net_cat_array
= doi_def
->map
.std
->cat
.cipso
;
957 net_spot
= cipso_v4_bitmap_walk(net_cat
,
967 switch (doi_def
->type
) {
968 case CIPSO_V4_MAP_PASS
:
969 host_spot
= net_spot
;
971 case CIPSO_V4_MAP_TRANS
:
972 if (net_spot
>= net_cat_size
)
974 host_spot
= net_cat_array
[net_spot
];
975 if (host_spot
>= CIPSO_V4_INV_CAT
)
979 ret_val
= netlbl_secattr_catmap_setbit(secattr
->attr
.mls
.cat
,
990 * cipso_v4_map_cat_enum_valid - Checks to see if the categories are valid
991 * @doi_def: the DOI definition
992 * @enumcat: category list
993 * @enumcat_len: length of the category list in bytes
996 * Checks the given categories against the given DOI definition and returns a
997 * negative value if any of the categories do not have a valid mapping and a
998 * zero value if all of the categories are valid.
1001 static int cipso_v4_map_cat_enum_valid(const struct cipso_v4_doi
*doi_def
,
1002 const unsigned char *enumcat
,
1009 if (doi_def
->type
!= CIPSO_V4_MAP_PASS
|| enumcat_len
& 0x01)
1012 for (iter
= 0; iter
< enumcat_len
; iter
+= 2) {
1013 cat
= get_unaligned_be16(&enumcat
[iter
]);
1014 if (cat
<= cat_prev
)
1023 * cipso_v4_map_cat_enum_hton - Perform a category mapping from host to network
1024 * @doi_def: the DOI definition
1025 * @secattr: the security attributes
1026 * @net_cat: the zero'd out category list in network/CIPSO format
1027 * @net_cat_len: the length of the CIPSO category list in bytes
1030 * Perform a label mapping to translate a local MLS category bitmap to the
1031 * correct CIPSO category list using the given DOI definition. Returns the
1032 * size in bytes of the network category bitmap on success, negative values
1036 static int cipso_v4_map_cat_enum_hton(const struct cipso_v4_doi
*doi_def
,
1037 const struct netlbl_lsm_secattr
*secattr
,
1038 unsigned char *net_cat
,
1045 cat
= netlbl_secattr_catmap_walk(secattr
->attr
.mls
.cat
,
1049 if ((cat_iter
+ 2) > net_cat_len
)
1052 *((__be16
*)&net_cat
[cat_iter
]) = htons(cat
);
1060 * cipso_v4_map_cat_enum_ntoh - Perform a category mapping from network to host
1061 * @doi_def: the DOI definition
1062 * @net_cat: the category list in network/CIPSO format
1063 * @net_cat_len: the length of the CIPSO bitmap in bytes
1064 * @secattr: the security attributes
1067 * Perform a label mapping to translate a CIPSO category list to the correct
1068 * local MLS category bitmap using the given DOI definition. Returns zero on
1069 * success, negative values on failure.
1072 static int cipso_v4_map_cat_enum_ntoh(const struct cipso_v4_doi
*doi_def
,
1073 const unsigned char *net_cat
,
1075 struct netlbl_lsm_secattr
*secattr
)
1080 for (iter
= 0; iter
< net_cat_len
; iter
+= 2) {
1081 ret_val
= netlbl_secattr_catmap_setbit(secattr
->attr
.mls
.cat
,
1082 get_unaligned_be16(&net_cat
[iter
]),
1092 * cipso_v4_map_cat_rng_valid - Checks to see if the categories are valid
1093 * @doi_def: the DOI definition
1094 * @rngcat: category list
1095 * @rngcat_len: length of the category list in bytes
1098 * Checks the given categories against the given DOI definition and returns a
1099 * negative value if any of the categories do not have a valid mapping and a
1100 * zero value if all of the categories are valid.
1103 static int cipso_v4_map_cat_rng_valid(const struct cipso_v4_doi
*doi_def
,
1104 const unsigned char *rngcat
,
1109 u32 cat_prev
= CIPSO_V4_MAX_REM_CATS
+ 1;
1112 if (doi_def
->type
!= CIPSO_V4_MAP_PASS
|| rngcat_len
& 0x01)
1115 for (iter
= 0; iter
< rngcat_len
; iter
+= 4) {
1116 cat_high
= get_unaligned_be16(&rngcat
[iter
]);
1117 if ((iter
+ 4) <= rngcat_len
)
1118 cat_low
= get_unaligned_be16(&rngcat
[iter
+ 2]);
1122 if (cat_high
> cat_prev
)
1132 * cipso_v4_map_cat_rng_hton - Perform a category mapping from host to network
1133 * @doi_def: the DOI definition
1134 * @secattr: the security attributes
1135 * @net_cat: the zero'd out category list in network/CIPSO format
1136 * @net_cat_len: the length of the CIPSO category list in bytes
1139 * Perform a label mapping to translate a local MLS category bitmap to the
1140 * correct CIPSO category list using the given DOI definition. Returns the
1141 * size in bytes of the network category bitmap on success, negative values
1145 static int cipso_v4_map_cat_rng_hton(const struct cipso_v4_doi
*doi_def
,
1146 const struct netlbl_lsm_secattr
*secattr
,
1147 unsigned char *net_cat
,
1151 u16 array
[CIPSO_V4_TAG_RNG_CAT_MAX
* 2];
1155 /* make sure we don't overflow the 'array[]' variable */
1157 (CIPSO_V4_OPT_LEN_MAX
- CIPSO_V4_HDR_LEN
- CIPSO_V4_TAG_RNG_BLEN
))
1161 iter
= netlbl_secattr_catmap_walk(secattr
->attr
.mls
.cat
,
1165 cat_size
+= (iter
== 0 ? 0 : sizeof(u16
));
1166 if (cat_size
> net_cat_len
)
1168 array
[array_cnt
++] = iter
;
1170 iter
= netlbl_secattr_catmap_walk_rng(secattr
->attr
.mls
.cat
,
1174 cat_size
+= sizeof(u16
);
1175 if (cat_size
> net_cat_len
)
1177 array
[array_cnt
++] = iter
;
1180 for (iter
= 0; array_cnt
> 0;) {
1181 *((__be16
*)&net_cat
[iter
]) = htons(array
[--array_cnt
]);
1184 if (array
[array_cnt
] != 0) {
1185 *((__be16
*)&net_cat
[iter
]) = htons(array
[array_cnt
]);
1194 * cipso_v4_map_cat_rng_ntoh - Perform a category mapping from network to host
1195 * @doi_def: the DOI definition
1196 * @net_cat: the category list in network/CIPSO format
1197 * @net_cat_len: the length of the CIPSO bitmap in bytes
1198 * @secattr: the security attributes
1201 * Perform a label mapping to translate a CIPSO category list to the correct
1202 * local MLS category bitmap using the given DOI definition. Returns zero on
1203 * success, negative values on failure.
1206 static int cipso_v4_map_cat_rng_ntoh(const struct cipso_v4_doi
*doi_def
,
1207 const unsigned char *net_cat
,
1209 struct netlbl_lsm_secattr
*secattr
)
1216 for (net_iter
= 0; net_iter
< net_cat_len
; net_iter
+= 4) {
1217 cat_high
= get_unaligned_be16(&net_cat
[net_iter
]);
1218 if ((net_iter
+ 4) <= net_cat_len
)
1219 cat_low
= get_unaligned_be16(&net_cat
[net_iter
+ 2]);
1223 ret_val
= netlbl_secattr_catmap_setrng(secattr
->attr
.mls
.cat
,
1235 * Protocol Handling Functions
1239 * cipso_v4_gentag_hdr - Generate a CIPSO option header
1240 * @doi_def: the DOI definition
1241 * @len: the total tag length in bytes, not including this header
1242 * @buf: the CIPSO option buffer
1245 * Write a CIPSO header into the beginning of @buffer.
1248 static void cipso_v4_gentag_hdr(const struct cipso_v4_doi
*doi_def
,
1252 buf
[0] = IPOPT_CIPSO
;
1253 buf
[1] = CIPSO_V4_HDR_LEN
+ len
;
1254 *(__be32
*)&buf
[2] = htonl(doi_def
->doi
);
1258 * cipso_v4_gentag_rbm - Generate a CIPSO restricted bitmap tag (type #1)
1259 * @doi_def: the DOI definition
1260 * @secattr: the security attributes
1261 * @buffer: the option buffer
1262 * @buffer_len: length of buffer in bytes
1265 * Generate a CIPSO option using the restricted bitmap tag, tag type #1. The
1266 * actual buffer length may be larger than the indicated size due to
1267 * translation between host and network category bitmaps. Returns the size of
1268 * the tag on success, negative values on failure.
1271 static int cipso_v4_gentag_rbm(const struct cipso_v4_doi
*doi_def
,
1272 const struct netlbl_lsm_secattr
*secattr
,
1273 unsigned char *buffer
,
1280 if ((secattr
->flags
& NETLBL_SECATTR_MLS_LVL
) == 0)
1283 ret_val
= cipso_v4_map_lvl_hton(doi_def
,
1284 secattr
->attr
.mls
.lvl
,
1289 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
1290 ret_val
= cipso_v4_map_cat_rbm_hton(doi_def
,
1297 /* This will send packets using the "optimized" format when
1298 * possibile as specified in section 3.4.2.6 of the
1300 if (cipso_v4_rbm_optfmt
&& ret_val
> 0 && ret_val
<= 10)
1303 tag_len
= 4 + ret_val
;
1307 buffer
[0] = CIPSO_V4_TAG_RBITMAP
;
1308 buffer
[1] = tag_len
;
1315 * cipso_v4_parsetag_rbm - Parse a CIPSO restricted bitmap tag
1316 * @doi_def: the DOI definition
1317 * @tag: the CIPSO tag
1318 * @secattr: the security attributes
1321 * Parse a CIPSO restricted bitmap tag (tag type #1) and return the security
1322 * attributes in @secattr. Return zero on success, negatives values on
1326 static int cipso_v4_parsetag_rbm(const struct cipso_v4_doi
*doi_def
,
1327 const unsigned char *tag
,
1328 struct netlbl_lsm_secattr
*secattr
)
1331 u8 tag_len
= tag
[1];
1334 ret_val
= cipso_v4_map_lvl_ntoh(doi_def
, tag
[3], &level
);
1337 secattr
->attr
.mls
.lvl
= level
;
1338 secattr
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1341 secattr
->attr
.mls
.cat
=
1342 netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1343 if (secattr
->attr
.mls
.cat
== NULL
)
1346 ret_val
= cipso_v4_map_cat_rbm_ntoh(doi_def
,
1351 netlbl_secattr_catmap_free(secattr
->attr
.mls
.cat
);
1355 secattr
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1362 * cipso_v4_gentag_enum - Generate a CIPSO enumerated tag (type #2)
1363 * @doi_def: the DOI definition
1364 * @secattr: the security attributes
1365 * @buffer: the option buffer
1366 * @buffer_len: length of buffer in bytes
1369 * Generate a CIPSO option using the enumerated tag, tag type #2. Returns the
1370 * size of the tag on success, negative values on failure.
1373 static int cipso_v4_gentag_enum(const struct cipso_v4_doi
*doi_def
,
1374 const struct netlbl_lsm_secattr
*secattr
,
1375 unsigned char *buffer
,
1382 if (!(secattr
->flags
& NETLBL_SECATTR_MLS_LVL
))
1385 ret_val
= cipso_v4_map_lvl_hton(doi_def
,
1386 secattr
->attr
.mls
.lvl
,
1391 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
1392 ret_val
= cipso_v4_map_cat_enum_hton(doi_def
,
1399 tag_len
= 4 + ret_val
;
1403 buffer
[0] = CIPSO_V4_TAG_ENUM
;
1404 buffer
[1] = tag_len
;
1411 * cipso_v4_parsetag_enum - Parse a CIPSO enumerated tag
1412 * @doi_def: the DOI definition
1413 * @tag: the CIPSO tag
1414 * @secattr: the security attributes
1417 * Parse a CIPSO enumerated tag (tag type #2) and return the security
1418 * attributes in @secattr. Return zero on success, negatives values on
1422 static int cipso_v4_parsetag_enum(const struct cipso_v4_doi
*doi_def
,
1423 const unsigned char *tag
,
1424 struct netlbl_lsm_secattr
*secattr
)
1427 u8 tag_len
= tag
[1];
1430 ret_val
= cipso_v4_map_lvl_ntoh(doi_def
, tag
[3], &level
);
1433 secattr
->attr
.mls
.lvl
= level
;
1434 secattr
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1437 secattr
->attr
.mls
.cat
=
1438 netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1439 if (secattr
->attr
.mls
.cat
== NULL
)
1442 ret_val
= cipso_v4_map_cat_enum_ntoh(doi_def
,
1447 netlbl_secattr_catmap_free(secattr
->attr
.mls
.cat
);
1451 secattr
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1458 * cipso_v4_gentag_rng - Generate a CIPSO ranged tag (type #5)
1459 * @doi_def: the DOI definition
1460 * @secattr: the security attributes
1461 * @buffer: the option buffer
1462 * @buffer_len: length of buffer in bytes
1465 * Generate a CIPSO option using the ranged tag, tag type #5. Returns the
1466 * size of the tag on success, negative values on failure.
1469 static int cipso_v4_gentag_rng(const struct cipso_v4_doi
*doi_def
,
1470 const struct netlbl_lsm_secattr
*secattr
,
1471 unsigned char *buffer
,
1478 if (!(secattr
->flags
& NETLBL_SECATTR_MLS_LVL
))
1481 ret_val
= cipso_v4_map_lvl_hton(doi_def
,
1482 secattr
->attr
.mls
.lvl
,
1487 if (secattr
->flags
& NETLBL_SECATTR_MLS_CAT
) {
1488 ret_val
= cipso_v4_map_cat_rng_hton(doi_def
,
1495 tag_len
= 4 + ret_val
;
1499 buffer
[0] = CIPSO_V4_TAG_RANGE
;
1500 buffer
[1] = tag_len
;
1507 * cipso_v4_parsetag_rng - Parse a CIPSO ranged tag
1508 * @doi_def: the DOI definition
1509 * @tag: the CIPSO tag
1510 * @secattr: the security attributes
1513 * Parse a CIPSO ranged tag (tag type #5) and return the security attributes
1514 * in @secattr. Return zero on success, negatives values on failure.
1517 static int cipso_v4_parsetag_rng(const struct cipso_v4_doi
*doi_def
,
1518 const unsigned char *tag
,
1519 struct netlbl_lsm_secattr
*secattr
)
1522 u8 tag_len
= tag
[1];
1525 ret_val
= cipso_v4_map_lvl_ntoh(doi_def
, tag
[3], &level
);
1528 secattr
->attr
.mls
.lvl
= level
;
1529 secattr
->flags
|= NETLBL_SECATTR_MLS_LVL
;
1532 secattr
->attr
.mls
.cat
=
1533 netlbl_secattr_catmap_alloc(GFP_ATOMIC
);
1534 if (secattr
->attr
.mls
.cat
== NULL
)
1537 ret_val
= cipso_v4_map_cat_rng_ntoh(doi_def
,
1542 netlbl_secattr_catmap_free(secattr
->attr
.mls
.cat
);
1546 secattr
->flags
|= NETLBL_SECATTR_MLS_CAT
;
1553 * cipso_v4_gentag_loc - Generate a CIPSO local tag (non-standard)
1554 * @doi_def: the DOI definition
1555 * @secattr: the security attributes
1556 * @buffer: the option buffer
1557 * @buffer_len: length of buffer in bytes
1560 * Generate a CIPSO option using the local tag. Returns the size of the tag
1561 * on success, negative values on failure.
1564 static int cipso_v4_gentag_loc(const struct cipso_v4_doi
*doi_def
,
1565 const struct netlbl_lsm_secattr
*secattr
,
1566 unsigned char *buffer
,
1569 if (!(secattr
->flags
& NETLBL_SECATTR_SECID
))
1572 buffer
[0] = CIPSO_V4_TAG_LOCAL
;
1573 buffer
[1] = CIPSO_V4_TAG_LOC_BLEN
;
1574 *(u32
*)&buffer
[2] = secattr
->attr
.secid
;
1576 return CIPSO_V4_TAG_LOC_BLEN
;
1580 * cipso_v4_parsetag_loc - Parse a CIPSO local tag
1581 * @doi_def: the DOI definition
1582 * @tag: the CIPSO tag
1583 * @secattr: the security attributes
1586 * Parse a CIPSO local tag and return the security attributes in @secattr.
1587 * Return zero on success, negatives values on failure.
1590 static int cipso_v4_parsetag_loc(const struct cipso_v4_doi
*doi_def
,
1591 const unsigned char *tag
,
1592 struct netlbl_lsm_secattr
*secattr
)
1594 secattr
->attr
.secid
= *(u32
*)&tag
[2];
1595 secattr
->flags
|= NETLBL_SECATTR_SECID
;
1601 * cipso_v4_validate - Validate a CIPSO option
1602 * @option: the start of the option, on error it is set to point to the error
1605 * This routine is called to validate a CIPSO option, it checks all of the
1606 * fields to ensure that they are at least valid, see the draft snippet below
1607 * for details. If the option is valid then a zero value is returned and
1608 * the value of @option is unchanged. If the option is invalid then a
1609 * non-zero value is returned and @option is adjusted to point to the
1610 * offending portion of the option. From the IETF draft ...
1612 * "If any field within the CIPSO options, such as the DOI identifier, is not
1613 * recognized the IP datagram is discarded and an ICMP 'parameter problem'
1614 * (type 12) is generated and returned. The ICMP code field is set to 'bad
1615 * parameter' (code 0) and the pointer is set to the start of the CIPSO field
1616 * that is unrecognized."
1619 int cipso_v4_validate(const struct sk_buff
*skb
, unsigned char **option
)
1621 unsigned char *opt
= *option
;
1623 unsigned char opt_iter
;
1624 unsigned char err_offset
= 0;
1627 struct cipso_v4_doi
*doi_def
= NULL
;
1630 /* caller already checks for length values that are too large */
1634 goto validate_return
;
1638 doi_def
= cipso_v4_doi_search(get_unaligned_be32(&opt
[2]));
1639 if (doi_def
== NULL
) {
1641 goto validate_return_locked
;
1644 opt_iter
= CIPSO_V4_HDR_LEN
;
1645 tag
= opt
+ opt_iter
;
1646 while (opt_iter
< opt_len
) {
1647 for (tag_iter
= 0; doi_def
->tags
[tag_iter
] != tag
[0];)
1648 if (doi_def
->tags
[tag_iter
] == CIPSO_V4_TAG_INVALID
||
1649 ++tag_iter
== CIPSO_V4_TAG_MAXCNT
) {
1650 err_offset
= opt_iter
;
1651 goto validate_return_locked
;
1655 if (tag_len
> (opt_len
- opt_iter
)) {
1656 err_offset
= opt_iter
+ 1;
1657 goto validate_return_locked
;
1661 case CIPSO_V4_TAG_RBITMAP
:
1662 if (tag_len
< CIPSO_V4_TAG_RBM_BLEN
) {
1663 err_offset
= opt_iter
+ 1;
1664 goto validate_return_locked
;
1667 /* We are already going to do all the verification
1668 * necessary at the socket layer so from our point of
1669 * view it is safe to turn these checks off (and less
1670 * work), however, the CIPSO draft says we should do
1671 * all the CIPSO validations here but it doesn't
1672 * really specify _exactly_ what we need to validate
1673 * ... so, just make it a sysctl tunable. */
1674 if (cipso_v4_rbm_strictvalid
) {
1675 if (cipso_v4_map_lvl_valid(doi_def
,
1677 err_offset
= opt_iter
+ 3;
1678 goto validate_return_locked
;
1680 if (tag_len
> CIPSO_V4_TAG_RBM_BLEN
&&
1681 cipso_v4_map_cat_rbm_valid(doi_def
,
1684 err_offset
= opt_iter
+ 4;
1685 goto validate_return_locked
;
1689 case CIPSO_V4_TAG_ENUM
:
1690 if (tag_len
< CIPSO_V4_TAG_ENUM_BLEN
) {
1691 err_offset
= opt_iter
+ 1;
1692 goto validate_return_locked
;
1695 if (cipso_v4_map_lvl_valid(doi_def
,
1697 err_offset
= opt_iter
+ 3;
1698 goto validate_return_locked
;
1700 if (tag_len
> CIPSO_V4_TAG_ENUM_BLEN
&&
1701 cipso_v4_map_cat_enum_valid(doi_def
,
1704 err_offset
= opt_iter
+ 4;
1705 goto validate_return_locked
;
1708 case CIPSO_V4_TAG_RANGE
:
1709 if (tag_len
< CIPSO_V4_TAG_RNG_BLEN
) {
1710 err_offset
= opt_iter
+ 1;
1711 goto validate_return_locked
;
1714 if (cipso_v4_map_lvl_valid(doi_def
,
1716 err_offset
= opt_iter
+ 3;
1717 goto validate_return_locked
;
1719 if (tag_len
> CIPSO_V4_TAG_RNG_BLEN
&&
1720 cipso_v4_map_cat_rng_valid(doi_def
,
1723 err_offset
= opt_iter
+ 4;
1724 goto validate_return_locked
;
1727 case CIPSO_V4_TAG_LOCAL
:
1728 /* This is a non-standard tag that we only allow for
1729 * local connections, so if the incoming interface is
1730 * not the loopback device drop the packet. */
1731 if (!(skb
->dev
->flags
& IFF_LOOPBACK
)) {
1732 err_offset
= opt_iter
;
1733 goto validate_return_locked
;
1735 if (tag_len
!= CIPSO_V4_TAG_LOC_BLEN
) {
1736 err_offset
= opt_iter
+ 1;
1737 goto validate_return_locked
;
1741 err_offset
= opt_iter
;
1742 goto validate_return_locked
;
1746 opt_iter
+= tag_len
;
1749 validate_return_locked
:
1752 *option
= opt
+ err_offset
;
1757 * cipso_v4_error - Send the correct reponse for a bad packet
1759 * @error: the error code
1760 * @gateway: CIPSO gateway flag
1763 * Based on the error code given in @error, send an ICMP error message back to
1764 * the originating host. From the IETF draft ...
1766 * "If the contents of the CIPSO [option] are valid but the security label is
1767 * outside of the configured host or port label range, the datagram is
1768 * discarded and an ICMP 'destination unreachable' (type 3) is generated and
1769 * returned. The code field of the ICMP is set to 'communication with
1770 * destination network administratively prohibited' (code 9) or to
1771 * 'communication with destination host administratively prohibited'
1772 * (code 10). The value of the code is dependent on whether the originator
1773 * of the ICMP message is acting as a CIPSO host or a CIPSO gateway. The
1774 * recipient of the ICMP message MUST be able to handle either value. The
1775 * same procedure is performed if a CIPSO [option] can not be added to an
1776 * IP packet because it is too large to fit in the IP options area."
1778 * "If the error is triggered by receipt of an ICMP message, the message is
1779 * discarded and no response is permitted (consistent with general ICMP
1780 * processing rules)."
1783 void cipso_v4_error(struct sk_buff
*skb
, int error
, u32 gateway
)
1785 if (ip_hdr(skb
)->protocol
== IPPROTO_ICMP
|| error
!= -EACCES
)
1789 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_NET_ANO
, 0);
1791 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_HOST_ANO
, 0);
1795 * cipso_v4_genopt - Generate a CIPSO option
1796 * @buf: the option buffer
1797 * @buf_len: the size of opt_buf
1798 * @doi_def: the CIPSO DOI to use
1799 * @secattr: the security attributes
1802 * Generate a CIPSO option using the DOI definition and security attributes
1803 * passed to the function. Returns the length of the option on success and
1804 * negative values on failure.
1807 static int cipso_v4_genopt(unsigned char *buf
, u32 buf_len
,
1808 const struct cipso_v4_doi
*doi_def
,
1809 const struct netlbl_lsm_secattr
*secattr
)
1814 if (buf_len
<= CIPSO_V4_HDR_LEN
)
1817 /* XXX - This code assumes only one tag per CIPSO option which isn't
1818 * really a good assumption to make but since we only support the MAC
1819 * tags right now it is a safe assumption. */
1822 memset(buf
, 0, buf_len
);
1823 switch (doi_def
->tags
[iter
]) {
1824 case CIPSO_V4_TAG_RBITMAP
:
1825 ret_val
= cipso_v4_gentag_rbm(doi_def
,
1827 &buf
[CIPSO_V4_HDR_LEN
],
1828 buf_len
- CIPSO_V4_HDR_LEN
);
1830 case CIPSO_V4_TAG_ENUM
:
1831 ret_val
= cipso_v4_gentag_enum(doi_def
,
1833 &buf
[CIPSO_V4_HDR_LEN
],
1834 buf_len
- CIPSO_V4_HDR_LEN
);
1836 case CIPSO_V4_TAG_RANGE
:
1837 ret_val
= cipso_v4_gentag_rng(doi_def
,
1839 &buf
[CIPSO_V4_HDR_LEN
],
1840 buf_len
- CIPSO_V4_HDR_LEN
);
1842 case CIPSO_V4_TAG_LOCAL
:
1843 ret_val
= cipso_v4_gentag_loc(doi_def
,
1845 &buf
[CIPSO_V4_HDR_LEN
],
1846 buf_len
- CIPSO_V4_HDR_LEN
);
1853 } while (ret_val
< 0 &&
1854 iter
< CIPSO_V4_TAG_MAXCNT
&&
1855 doi_def
->tags
[iter
] != CIPSO_V4_TAG_INVALID
);
1858 cipso_v4_gentag_hdr(doi_def
, buf
, ret_val
);
1859 return CIPSO_V4_HDR_LEN
+ ret_val
;
1863 * cipso_v4_sock_setattr - Add a CIPSO option to a socket
1865 * @doi_def: the CIPSO DOI to use
1866 * @secattr: the specific security attributes of the socket
1869 * Set the CIPSO option on the given socket using the DOI definition and
1870 * security attributes passed to the function. This function requires
1871 * exclusive access to @sk, which means it either needs to be in the
1872 * process of being created or locked. Returns zero on success and negative
1873 * values on failure.
1876 int cipso_v4_sock_setattr(struct sock
*sk
,
1877 const struct cipso_v4_doi
*doi_def
,
1878 const struct netlbl_lsm_secattr
*secattr
)
1880 int ret_val
= -EPERM
;
1881 unsigned char *buf
= NULL
;
1884 struct ip_options
*opt
= NULL
;
1885 struct inet_sock
*sk_inet
;
1886 struct inet_connection_sock
*sk_conn
;
1888 /* In the case of sock_create_lite(), the sock->sk field is not
1889 * defined yet but it is not a problem as the only users of these
1890 * "lite" PF_INET sockets are functions which do an accept() call
1891 * afterwards so we will label the socket as part of the accept(). */
1895 /* We allocate the maximum CIPSO option size here so we are probably
1896 * being a little wasteful, but it makes our life _much_ easier later
1897 * on and after all we are only talking about 40 bytes. */
1898 buf_len
= CIPSO_V4_OPT_LEN_MAX
;
1899 buf
= kmalloc(buf_len
, GFP_ATOMIC
);
1902 goto socket_setattr_failure
;
1905 ret_val
= cipso_v4_genopt(buf
, buf_len
, doi_def
, secattr
);
1907 goto socket_setattr_failure
;
1910 /* We can't use ip_options_get() directly because it makes a call to
1911 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1912 * we won't always have CAP_NET_RAW even though we _always_ want to
1913 * set the IPOPT_CIPSO option. */
1914 opt_len
= (buf_len
+ 3) & ~3;
1915 opt
= kzalloc(sizeof(*opt
) + opt_len
, GFP_ATOMIC
);
1918 goto socket_setattr_failure
;
1920 memcpy(opt
->__data
, buf
, buf_len
);
1921 opt
->optlen
= opt_len
;
1922 opt
->cipso
= sizeof(struct iphdr
);
1926 sk_inet
= inet_sk(sk
);
1927 if (sk_inet
->is_icsk
) {
1928 sk_conn
= inet_csk(sk
);
1930 sk_conn
->icsk_ext_hdr_len
-= sk_inet
->opt
->optlen
;
1931 sk_conn
->icsk_ext_hdr_len
+= opt
->optlen
;
1932 sk_conn
->icsk_sync_mss(sk
, sk_conn
->icsk_pmtu_cookie
);
1934 opt
= xchg(&sk_inet
->opt
, opt
);
1939 socket_setattr_failure
:
1946 * cipso_v4_req_setattr - Add a CIPSO option to a connection request socket
1947 * @req: the connection request socket
1948 * @doi_def: the CIPSO DOI to use
1949 * @secattr: the specific security attributes of the socket
1952 * Set the CIPSO option on the given socket using the DOI definition and
1953 * security attributes passed to the function. Returns zero on success and
1954 * negative values on failure.
1957 int cipso_v4_req_setattr(struct request_sock
*req
,
1958 const struct cipso_v4_doi
*doi_def
,
1959 const struct netlbl_lsm_secattr
*secattr
)
1961 int ret_val
= -EPERM
;
1962 unsigned char *buf
= NULL
;
1965 struct ip_options
*opt
= NULL
;
1966 struct inet_request_sock
*req_inet
;
1968 /* We allocate the maximum CIPSO option size here so we are probably
1969 * being a little wasteful, but it makes our life _much_ easier later
1970 * on and after all we are only talking about 40 bytes. */
1971 buf_len
= CIPSO_V4_OPT_LEN_MAX
;
1972 buf
= kmalloc(buf_len
, GFP_ATOMIC
);
1975 goto req_setattr_failure
;
1978 ret_val
= cipso_v4_genopt(buf
, buf_len
, doi_def
, secattr
);
1980 goto req_setattr_failure
;
1983 /* We can't use ip_options_get() directly because it makes a call to
1984 * ip_options_get_alloc() which allocates memory with GFP_KERNEL and
1985 * we won't always have CAP_NET_RAW even though we _always_ want to
1986 * set the IPOPT_CIPSO option. */
1987 opt_len
= (buf_len
+ 3) & ~3;
1988 opt
= kzalloc(sizeof(*opt
) + opt_len
, GFP_ATOMIC
);
1991 goto req_setattr_failure
;
1993 memcpy(opt
->__data
, buf
, buf_len
);
1994 opt
->optlen
= opt_len
;
1995 opt
->cipso
= sizeof(struct iphdr
);
1999 req_inet
= inet_rsk(req
);
2000 opt
= xchg(&req_inet
->opt
, opt
);
2005 req_setattr_failure
:
2012 * cipso_v4_delopt - Delete the CIPSO option from a set of IP options
2013 * @opt_ptr: IP option pointer
2016 * Deletes the CIPSO IP option from a set of IP options and makes the necessary
2017 * adjustments to the IP option structure. Returns zero on success, negative
2018 * values on failure.
2021 static int cipso_v4_delopt(struct ip_options
**opt_ptr
)
2024 struct ip_options
*opt
= *opt_ptr
;
2026 if (opt
->srr
|| opt
->rr
|| opt
->ts
|| opt
->router_alert
) {
2029 unsigned char *cipso_ptr
;
2033 cipso_off
= opt
->cipso
- sizeof(struct iphdr
);
2034 cipso_ptr
= &opt
->__data
[cipso_off
];
2035 cipso_len
= cipso_ptr
[1];
2037 if (opt
->srr
> opt
->cipso
)
2038 opt
->srr
-= cipso_len
;
2039 if (opt
->rr
> opt
->cipso
)
2040 opt
->rr
-= cipso_len
;
2041 if (opt
->ts
> opt
->cipso
)
2042 opt
->ts
-= cipso_len
;
2043 if (opt
->router_alert
> opt
->cipso
)
2044 opt
->router_alert
-= cipso_len
;
2047 memmove(cipso_ptr
, cipso_ptr
+ cipso_len
,
2048 opt
->optlen
- cipso_off
- cipso_len
);
2050 /* determining the new total option length is tricky because of
2051 * the padding necessary, the only thing i can think to do at
2052 * this point is walk the options one-by-one, skipping the
2053 * padding at the end to determine the actual option size and
2054 * from there we can determine the new total option length */
2057 while (iter
< opt
->optlen
)
2058 if (opt
->__data
[iter
] != IPOPT_NOP
) {
2059 iter
+= opt
->__data
[iter
+ 1];
2063 hdr_delta
= opt
->optlen
;
2064 opt
->optlen
= (optlen_new
+ 3) & ~3;
2065 hdr_delta
-= opt
->optlen
;
2067 /* only the cipso option was present on the socket so we can
2068 * remove the entire option struct */
2070 hdr_delta
= opt
->optlen
;
2078 * cipso_v4_sock_delattr - Delete the CIPSO option from a socket
2082 * Removes the CIPSO option from a socket, if present.
2085 void cipso_v4_sock_delattr(struct sock
*sk
)
2088 struct ip_options
*opt
;
2089 struct inet_sock
*sk_inet
;
2091 sk_inet
= inet_sk(sk
);
2093 if (opt
== NULL
|| opt
->cipso
== 0)
2096 hdr_delta
= cipso_v4_delopt(&sk_inet
->opt
);
2097 if (sk_inet
->is_icsk
&& hdr_delta
> 0) {
2098 struct inet_connection_sock
*sk_conn
= inet_csk(sk
);
2099 sk_conn
->icsk_ext_hdr_len
-= hdr_delta
;
2100 sk_conn
->icsk_sync_mss(sk
, sk_conn
->icsk_pmtu_cookie
);
2105 * cipso_v4_req_delattr - Delete the CIPSO option from a request socket
2106 * @reg: the request socket
2109 * Removes the CIPSO option from a request socket, if present.
2112 void cipso_v4_req_delattr(struct request_sock
*req
)
2114 struct ip_options
*opt
;
2115 struct inet_request_sock
*req_inet
;
2117 req_inet
= inet_rsk(req
);
2118 opt
= req_inet
->opt
;
2119 if (opt
== NULL
|| opt
->cipso
== 0)
2122 cipso_v4_delopt(&req_inet
->opt
);
2126 * cipso_v4_getattr - Helper function for the cipso_v4_*_getattr functions
2127 * @cipso: the CIPSO v4 option
2128 * @secattr: the security attributes
2131 * Inspect @cipso and return the security attributes in @secattr. Returns zero
2132 * on success and negative values on failure.
2135 static int cipso_v4_getattr(const unsigned char *cipso
,
2136 struct netlbl_lsm_secattr
*secattr
)
2138 int ret_val
= -ENOMSG
;
2140 struct cipso_v4_doi
*doi_def
;
2142 if (cipso_v4_cache_check(cipso
, cipso
[1], secattr
) == 0)
2145 doi
= get_unaligned_be32(&cipso
[2]);
2147 doi_def
= cipso_v4_doi_search(doi
);
2148 if (doi_def
== NULL
)
2149 goto getattr_return
;
2150 /* XXX - This code assumes only one tag per CIPSO option which isn't
2151 * really a good assumption to make but since we only support the MAC
2152 * tags right now it is a safe assumption. */
2154 case CIPSO_V4_TAG_RBITMAP
:
2155 ret_val
= cipso_v4_parsetag_rbm(doi_def
, &cipso
[6], secattr
);
2157 case CIPSO_V4_TAG_ENUM
:
2158 ret_val
= cipso_v4_parsetag_enum(doi_def
, &cipso
[6], secattr
);
2160 case CIPSO_V4_TAG_RANGE
:
2161 ret_val
= cipso_v4_parsetag_rng(doi_def
, &cipso
[6], secattr
);
2163 case CIPSO_V4_TAG_LOCAL
:
2164 ret_val
= cipso_v4_parsetag_loc(doi_def
, &cipso
[6], secattr
);
2168 secattr
->type
= NETLBL_NLTYPE_CIPSOV4
;
2176 * cipso_v4_sock_getattr - Get the security attributes from a sock
2178 * @secattr: the security attributes
2181 * Query @sk to see if there is a CIPSO option attached to the sock and if
2182 * there is return the CIPSO security attributes in @secattr. This function
2183 * requires that @sk be locked, or privately held, but it does not do any
2184 * locking itself. Returns zero on success and negative values on failure.
2187 int cipso_v4_sock_getattr(struct sock
*sk
, struct netlbl_lsm_secattr
*secattr
)
2189 struct ip_options
*opt
;
2191 opt
= inet_sk(sk
)->opt
;
2192 if (opt
== NULL
|| opt
->cipso
== 0)
2195 return cipso_v4_getattr(opt
->__data
+ opt
->cipso
- sizeof(struct iphdr
),
2200 * cipso_v4_skbuff_setattr - Set the CIPSO option on a packet
2202 * @secattr: the security attributes
2205 * Set the CIPSO option on the given packet based on the security attributes.
2206 * Returns a pointer to the IP header on success and NULL on failure.
2209 int cipso_v4_skbuff_setattr(struct sk_buff
*skb
,
2210 const struct cipso_v4_doi
*doi_def
,
2211 const struct netlbl_lsm_secattr
*secattr
)
2215 struct ip_options
*opt
= &IPCB(skb
)->opt
;
2216 unsigned char buf
[CIPSO_V4_OPT_LEN_MAX
];
2217 u32 buf_len
= CIPSO_V4_OPT_LEN_MAX
;
2221 ret_val
= cipso_v4_genopt(buf
, buf_len
, doi_def
, secattr
);
2225 opt_len
= (buf_len
+ 3) & ~3;
2227 /* we overwrite any existing options to ensure that we have enough
2228 * room for the CIPSO option, the reason is that we _need_ to guarantee
2229 * that the security label is applied to the packet - we do the same
2230 * thing when using the socket options and it hasn't caused a problem,
2231 * if we need to we can always revisit this choice later */
2233 len_delta
= opt_len
- opt
->optlen
;
2234 /* if we don't ensure enough headroom we could panic on the skb_push()
2235 * call below so make sure we have enough, we are also "mangling" the
2236 * packet so we should probably do a copy-on-write call anyway */
2237 ret_val
= skb_cow(skb
, skb_headroom(skb
) + len_delta
);
2241 if (len_delta
> 0) {
2242 /* we assume that the header + opt->optlen have already been
2243 * "pushed" in ip_options_build() or similar */
2245 skb_push(skb
, len_delta
);
2246 memmove((char *)iph
- len_delta
, iph
, iph
->ihl
<< 2);
2247 skb_reset_network_header(skb
);
2249 } else if (len_delta
< 0) {
2251 memset(iph
+ 1, IPOPT_NOP
, opt
->optlen
);
2255 if (opt
->optlen
> 0)
2256 memset(opt
, 0, sizeof(*opt
));
2257 opt
->optlen
= opt_len
;
2258 opt
->cipso
= sizeof(struct iphdr
);
2259 opt
->is_changed
= 1;
2261 /* we have to do the following because we are being called from a
2262 * netfilter hook which means the packet already has had the header
2263 * fields populated and the checksum calculated - yes this means we
2264 * are doing more work than needed but we do it to keep the core
2265 * stack clean and tidy */
2266 memcpy(iph
+ 1, buf
, buf_len
);
2267 if (opt_len
> buf_len
)
2268 memset((char *)(iph
+ 1) + buf_len
, 0, opt_len
- buf_len
);
2269 if (len_delta
!= 0) {
2270 iph
->ihl
= 5 + (opt_len
>> 2);
2271 iph
->tot_len
= htons(skb
->len
);
2279 * cipso_v4_skbuff_delattr - Delete any CIPSO options from a packet
2283 * Removes any and all CIPSO options from the given packet. Returns zero on
2284 * success, negative values on failure.
2287 int cipso_v4_skbuff_delattr(struct sk_buff
*skb
)
2291 struct ip_options
*opt
= &IPCB(skb
)->opt
;
2292 unsigned char *cipso_ptr
;
2294 if (opt
->cipso
== 0)
2297 /* since we are changing the packet we should make a copy */
2298 ret_val
= skb_cow(skb
, skb_headroom(skb
));
2302 /* the easiest thing to do is just replace the cipso option with noop
2303 * options since we don't change the size of the packet, although we
2304 * still need to recalculate the checksum */
2307 cipso_ptr
= (unsigned char *)iph
+ opt
->cipso
;
2308 memset(cipso_ptr
, IPOPT_NOOP
, cipso_ptr
[1]);
2310 opt
->is_changed
= 1;
2318 * cipso_v4_skbuff_getattr - Get the security attributes from the CIPSO option
2320 * @secattr: the security attributes
2323 * Parse the given packet's CIPSO option and return the security attributes.
2324 * Returns zero on success and negative values on failure.
2327 int cipso_v4_skbuff_getattr(const struct sk_buff
*skb
,
2328 struct netlbl_lsm_secattr
*secattr
)
2330 return cipso_v4_getattr(CIPSO_V4_OPTPTR(skb
), secattr
);
2338 * cipso_v4_init - Initialize the CIPSO module
2341 * Initialize the CIPSO module and prepare it for use. Returns zero on success
2342 * and negative values on failure.
2345 static int __init
cipso_v4_init(void)
2349 ret_val
= cipso_v4_cache_init();
2351 panic("Failed to initialize the CIPSO/IPv4 cache (%d)\n",
2357 subsys_initcall(cipso_v4_init
);